sr_vendor.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* -*-linux-c-*-
  3. * vendor-specific code for SCSI CD-ROM's goes here.
  4. *
  5. * This is needed becauce most of the new features (multisession and
  6. * the like) are too new to be included into the SCSI-II standard (to
  7. * be exact: there is'nt anything in my draft copy).
  8. *
  9. * Aug 1997: Ha! Got a SCSI-3 cdrom spec across my fingers. SCSI-3 does
  10. * multisession using the READ TOC command (like SONY).
  11. *
  12. * Rearranged stuff here: SCSI-3 is included allways, support
  13. * for NEC/TOSHIBA/HP commands is optional.
  14. *
  15. * Gerd Knorr <kraxel@cs.tu-berlin.de>
  16. *
  17. * --------------------------------------------------------------------------
  18. *
  19. * support for XA/multisession-CD's
  20. *
  21. * - NEC: Detection and support of multisession CD's.
  22. *
  23. * - TOSHIBA: Detection and support of multisession CD's.
  24. * Some XA-Sector tweaking, required for older drives.
  25. *
  26. * - SONY: Detection and support of multisession CD's.
  27. * added by Thomas Quinot <thomas@cuivre.freenix.fr>
  28. *
  29. * - PIONEER, HITACHI, PLEXTOR, MATSHITA, TEAC, PHILIPS: known to
  30. * work with SONY (SCSI3 now) code.
  31. *
  32. * - HP: Much like SONY, but a little different... (Thomas)
  33. * HP-Writers only ??? Maybe other CD-Writers work with this too ?
  34. * HP 6020 writers now supported.
  35. */
  36. #include <linux/cdrom.h>
  37. #include <linux/errno.h>
  38. #include <linux/string.h>
  39. #include <linux/bcd.h>
  40. #include <linux/blkdev.h>
  41. #include <linux/slab.h>
  42. #include <scsi/scsi.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <scsi/scsi_device.h>
  45. #include <scsi/scsi_host.h>
  46. #include <scsi/scsi_ioctl.h>
  47. #include "sr.h"
  48. #if 0
  49. #define DEBUG
  50. #endif
  51. /* here are some constants to sort the vendors into groups */
  52. #define VENDOR_SCSI3 1 /* default: scsi-3 mmc */
  53. #define VENDOR_NEC 2
  54. #define VENDOR_TOSHIBA 3
  55. #define VENDOR_WRITER 4 /* pre-scsi3 writers */
  56. #define VENDOR_TIMEOUT 30*HZ
  57. void sr_vendor_init(Scsi_CD *cd)
  58. {
  59. #ifndef CONFIG_BLK_DEV_SR_VENDOR
  60. cd->vendor = VENDOR_SCSI3;
  61. #else
  62. const char *vendor = cd->device->vendor;
  63. const char *model = cd->device->model;
  64. /* default */
  65. cd->vendor = VENDOR_SCSI3;
  66. if (cd->readcd_known)
  67. /* this is true for scsi3/mmc drives - no more checks */
  68. return;
  69. if (cd->device->type == TYPE_WORM) {
  70. cd->vendor = VENDOR_WRITER;
  71. } else if (!strncmp(vendor, "NEC", 3)) {
  72. cd->vendor = VENDOR_NEC;
  73. if (!strncmp(model, "CD-ROM DRIVE:25", 15) ||
  74. !strncmp(model, "CD-ROM DRIVE:36", 15) ||
  75. !strncmp(model, "CD-ROM DRIVE:83", 15) ||
  76. !strncmp(model, "CD-ROM DRIVE:84 ", 16)
  77. #if 0
  78. /* my NEC 3x returns the read-raw data if a read-raw
  79. is followed by a read for the same sector - aeb */
  80. || !strncmp(model, "CD-ROM DRIVE:500", 16)
  81. #endif
  82. )
  83. /* these can't handle multisession, may hang */
  84. cd->cdi.mask |= CDC_MULTI_SESSION;
  85. } else if (!strncmp(vendor, "TOSHIBA", 7)) {
  86. cd->vendor = VENDOR_TOSHIBA;
  87. }
  88. #endif
  89. }
  90. /* small handy function for switching block length using MODE SELECT,
  91. * used by sr_read_sector() */
  92. int sr_set_blocklength(Scsi_CD *cd, int blocklength)
  93. {
  94. unsigned char *buffer; /* the buffer for the ioctl */
  95. struct packet_command cgc;
  96. struct ccs_modesel_head *modesel;
  97. int rc, density = 0;
  98. #ifdef CONFIG_BLK_DEV_SR_VENDOR
  99. if (cd->vendor == VENDOR_TOSHIBA)
  100. density = (blocklength > 2048) ? 0x81 : 0x83;
  101. #endif
  102. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  103. if (!buffer)
  104. return -ENOMEM;
  105. #ifdef DEBUG
  106. sr_printk(KERN_INFO, cd, "MODE SELECT 0x%x/%d\n", density, blocklength);
  107. #endif
  108. memset(&cgc, 0, sizeof(struct packet_command));
  109. cgc.cmd[0] = MODE_SELECT;
  110. cgc.cmd[1] = (1 << 4);
  111. cgc.cmd[4] = 12;
  112. modesel = (struct ccs_modesel_head *) buffer;
  113. memset(modesel, 0, sizeof(*modesel));
  114. modesel->block_desc_length = 0x08;
  115. modesel->density = density;
  116. modesel->block_length_med = (blocklength >> 8) & 0xff;
  117. modesel->block_length_lo = blocklength & 0xff;
  118. cgc.buffer = buffer;
  119. cgc.buflen = sizeof(*modesel);
  120. cgc.data_direction = DMA_TO_DEVICE;
  121. cgc.timeout = VENDOR_TIMEOUT;
  122. if (0 == (rc = sr_do_ioctl(cd, &cgc))) {
  123. cd->device->sector_size = blocklength;
  124. }
  125. #ifdef DEBUG
  126. else
  127. sr_printk(KERN_INFO, cd,
  128. "switching blocklength to %d bytes failed\n",
  129. blocklength);
  130. #endif
  131. kfree(buffer);
  132. return rc;
  133. }
  134. /* This function gets called after a media change. Checks if the CD is
  135. multisession, asks for offset etc. */
  136. int sr_cd_check(struct cdrom_device_info *cdi)
  137. {
  138. Scsi_CD *cd = cdi->handle;
  139. unsigned long sector;
  140. unsigned char *buffer; /* the buffer for the ioctl */
  141. struct packet_command cgc;
  142. int rc, no_multi;
  143. if (cd->cdi.mask & CDC_MULTI_SESSION)
  144. return 0;
  145. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  146. if (!buffer)
  147. return -ENOMEM;
  148. sector = 0; /* the multisession sector offset goes here */
  149. no_multi = 0; /* flag: the drive can't handle multisession */
  150. rc = 0;
  151. memset(&cgc, 0, sizeof(struct packet_command));
  152. switch (cd->vendor) {
  153. case VENDOR_SCSI3:
  154. cgc.cmd[0] = READ_TOC;
  155. cgc.cmd[8] = 12;
  156. cgc.cmd[9] = 0x40;
  157. cgc.buffer = buffer;
  158. cgc.buflen = 12;
  159. cgc.quiet = 1;
  160. cgc.data_direction = DMA_FROM_DEVICE;
  161. cgc.timeout = VENDOR_TIMEOUT;
  162. rc = sr_do_ioctl(cd, &cgc);
  163. if (rc != 0)
  164. break;
  165. if ((buffer[0] << 8) + buffer[1] < 0x0a) {
  166. sr_printk(KERN_INFO, cd, "Hmm, seems the drive "
  167. "doesn't support multisession CD's\n");
  168. no_multi = 1;
  169. break;
  170. }
  171. sector = buffer[11] + (buffer[10] << 8) +
  172. (buffer[9] << 16) + (buffer[8] << 24);
  173. if (buffer[6] <= 1) {
  174. /* ignore sector offsets from first track */
  175. sector = 0;
  176. }
  177. break;
  178. #ifdef CONFIG_BLK_DEV_SR_VENDOR
  179. case VENDOR_NEC:{
  180. unsigned long min, sec, frame;
  181. cgc.cmd[0] = 0xde;
  182. cgc.cmd[1] = 0x03;
  183. cgc.cmd[2] = 0xb0;
  184. cgc.buffer = buffer;
  185. cgc.buflen = 0x16;
  186. cgc.quiet = 1;
  187. cgc.data_direction = DMA_FROM_DEVICE;
  188. cgc.timeout = VENDOR_TIMEOUT;
  189. rc = sr_do_ioctl(cd, &cgc);
  190. if (rc != 0)
  191. break;
  192. if (buffer[14] != 0 && buffer[14] != 0xb0) {
  193. sr_printk(KERN_INFO, cd, "Hmm, seems the cdrom "
  194. "doesn't support multisession CD's\n");
  195. no_multi = 1;
  196. break;
  197. }
  198. min = bcd2bin(buffer[15]);
  199. sec = bcd2bin(buffer[16]);
  200. frame = bcd2bin(buffer[17]);
  201. sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
  202. break;
  203. }
  204. case VENDOR_TOSHIBA:{
  205. unsigned long min, sec, frame;
  206. /* we request some disc information (is it a XA-CD ?,
  207. * where starts the last session ?) */
  208. cgc.cmd[0] = 0xc7;
  209. cgc.cmd[1] = 0x03;
  210. cgc.buffer = buffer;
  211. cgc.buflen = 4;
  212. cgc.quiet = 1;
  213. cgc.data_direction = DMA_FROM_DEVICE;
  214. cgc.timeout = VENDOR_TIMEOUT;
  215. rc = sr_do_ioctl(cd, &cgc);
  216. if (rc == -EINVAL) {
  217. sr_printk(KERN_INFO, cd, "Hmm, seems the drive "
  218. "doesn't support multisession CD's\n");
  219. no_multi = 1;
  220. break;
  221. }
  222. if (rc != 0)
  223. break;
  224. min = bcd2bin(buffer[1]);
  225. sec = bcd2bin(buffer[2]);
  226. frame = bcd2bin(buffer[3]);
  227. sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
  228. if (sector)
  229. sector -= CD_MSF_OFFSET;
  230. sr_set_blocklength(cd, 2048);
  231. break;
  232. }
  233. case VENDOR_WRITER:
  234. cgc.cmd[0] = READ_TOC;
  235. cgc.cmd[8] = 0x04;
  236. cgc.cmd[9] = 0x40;
  237. cgc.buffer = buffer;
  238. cgc.buflen = 0x04;
  239. cgc.quiet = 1;
  240. cgc.data_direction = DMA_FROM_DEVICE;
  241. cgc.timeout = VENDOR_TIMEOUT;
  242. rc = sr_do_ioctl(cd, &cgc);
  243. if (rc != 0) {
  244. break;
  245. }
  246. if ((rc = buffer[2]) == 0) {
  247. sr_printk(KERN_WARNING, cd,
  248. "No finished session\n");
  249. break;
  250. }
  251. cgc.cmd[0] = READ_TOC; /* Read TOC */
  252. cgc.cmd[6] = rc & 0x7f; /* number of last session */
  253. cgc.cmd[8] = 0x0c;
  254. cgc.cmd[9] = 0x40;
  255. cgc.buffer = buffer;
  256. cgc.buflen = 12;
  257. cgc.quiet = 1;
  258. cgc.data_direction = DMA_FROM_DEVICE;
  259. cgc.timeout = VENDOR_TIMEOUT;
  260. rc = sr_do_ioctl(cd, &cgc);
  261. if (rc != 0) {
  262. break;
  263. }
  264. sector = buffer[11] + (buffer[10] << 8) +
  265. (buffer[9] << 16) + (buffer[8] << 24);
  266. break;
  267. #endif /* CONFIG_BLK_DEV_SR_VENDOR */
  268. default:
  269. /* should not happen */
  270. sr_printk(KERN_WARNING, cd,
  271. "unknown vendor code (%i), not initialized ?\n",
  272. cd->vendor);
  273. sector = 0;
  274. no_multi = 1;
  275. break;
  276. }
  277. cd->ms_offset = sector;
  278. cd->xa_flag = 0;
  279. if (CDS_AUDIO != sr_disk_status(cdi) && 1 == sr_is_xa(cd))
  280. cd->xa_flag = 1;
  281. if (2048 != cd->device->sector_size) {
  282. sr_set_blocklength(cd, 2048);
  283. }
  284. if (no_multi)
  285. cdi->mask |= CDC_MULTI_SESSION;
  286. #ifdef DEBUG
  287. if (sector)
  288. sr_printk(KERN_DEBUG, cd, "multisession offset=%lu\n",
  289. sector);
  290. #endif
  291. kfree(buffer);
  292. return rc;
  293. }