sr_vendor.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. const char *vendor = cd->device->vendor;
  60. const char *model = cd->device->model;
  61. /* default */
  62. cd->vendor = VENDOR_SCSI3;
  63. if (cd->readcd_known)
  64. /* this is true for scsi3/mmc drives - no more checks */
  65. return;
  66. if (cd->device->type == TYPE_WORM) {
  67. cd->vendor = VENDOR_WRITER;
  68. } else if (!strncmp(vendor, "NEC", 3)) {
  69. cd->vendor = VENDOR_NEC;
  70. if (!strncmp(model, "CD-ROM DRIVE:25", 15) ||
  71. !strncmp(model, "CD-ROM DRIVE:36", 15) ||
  72. !strncmp(model, "CD-ROM DRIVE:83", 15) ||
  73. !strncmp(model, "CD-ROM DRIVE:84 ", 16)
  74. #if 0
  75. /* my NEC 3x returns the read-raw data if a read-raw
  76. is followed by a read for the same sector - aeb */
  77. || !strncmp(model, "CD-ROM DRIVE:500", 16)
  78. #endif
  79. )
  80. /* these can't handle multisession, may hang */
  81. cd->cdi.mask |= CDC_MULTI_SESSION;
  82. } else if (!strncmp(vendor, "TOSHIBA", 7)) {
  83. cd->vendor = VENDOR_TOSHIBA;
  84. }
  85. }
  86. /* small handy function for switching block length using MODE SELECT,
  87. * used by sr_read_sector() */
  88. int sr_set_blocklength(Scsi_CD *cd, int blocklength)
  89. {
  90. unsigned char *buffer; /* the buffer for the ioctl */
  91. struct packet_command cgc;
  92. struct ccs_modesel_head *modesel;
  93. int rc, density = 0;
  94. if (cd->vendor == VENDOR_TOSHIBA)
  95. density = (blocklength > 2048) ? 0x81 : 0x83;
  96. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  97. if (!buffer)
  98. return -ENOMEM;
  99. #ifdef DEBUG
  100. sr_printk(KERN_INFO, cd, "MODE SELECT 0x%x/%d\n", density, blocklength);
  101. #endif
  102. memset(&cgc, 0, sizeof(struct packet_command));
  103. cgc.cmd[0] = MODE_SELECT;
  104. cgc.cmd[1] = (1 << 4);
  105. cgc.cmd[4] = 12;
  106. modesel = (struct ccs_modesel_head *) buffer;
  107. memset(modesel, 0, sizeof(*modesel));
  108. modesel->block_desc_length = 0x08;
  109. modesel->density = density;
  110. modesel->block_length_med = (blocklength >> 8) & 0xff;
  111. modesel->block_length_lo = blocklength & 0xff;
  112. cgc.buffer = buffer;
  113. cgc.buflen = sizeof(*modesel);
  114. cgc.data_direction = DMA_TO_DEVICE;
  115. cgc.timeout = VENDOR_TIMEOUT;
  116. if (0 == (rc = sr_do_ioctl(cd, &cgc))) {
  117. cd->device->sector_size = blocklength;
  118. }
  119. #ifdef DEBUG
  120. else
  121. sr_printk(KERN_INFO, cd,
  122. "switching blocklength to %d bytes failed\n",
  123. blocklength);
  124. #endif
  125. kfree(buffer);
  126. return rc;
  127. }
  128. /* This function gets called after a media change. Checks if the CD is
  129. multisession, asks for offset etc. */
  130. int sr_cd_check(struct cdrom_device_info *cdi)
  131. {
  132. Scsi_CD *cd = cdi->handle;
  133. unsigned long sector;
  134. unsigned char *buffer; /* the buffer for the ioctl */
  135. struct packet_command cgc;
  136. int rc, no_multi;
  137. if (cd->cdi.mask & CDC_MULTI_SESSION)
  138. return 0;
  139. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  140. if (!buffer)
  141. return -ENOMEM;
  142. sector = 0; /* the multisession sector offset goes here */
  143. no_multi = 0; /* flag: the drive can't handle multisession */
  144. rc = 0;
  145. memset(&cgc, 0, sizeof(struct packet_command));
  146. switch (cd->vendor) {
  147. case VENDOR_SCSI3:
  148. cgc.cmd[0] = READ_TOC;
  149. cgc.cmd[8] = 12;
  150. cgc.cmd[9] = 0x40;
  151. cgc.buffer = buffer;
  152. cgc.buflen = 12;
  153. cgc.quiet = 1;
  154. cgc.data_direction = DMA_FROM_DEVICE;
  155. cgc.timeout = VENDOR_TIMEOUT;
  156. rc = sr_do_ioctl(cd, &cgc);
  157. if (rc != 0)
  158. break;
  159. if ((buffer[0] << 8) + buffer[1] < 0x0a) {
  160. sr_printk(KERN_INFO, cd, "Hmm, seems the drive "
  161. "doesn't support multisession CD's\n");
  162. no_multi = 1;
  163. break;
  164. }
  165. sector = buffer[11] + (buffer[10] << 8) +
  166. (buffer[9] << 16) + (buffer[8] << 24);
  167. if (buffer[6] <= 1) {
  168. /* ignore sector offsets from first track */
  169. sector = 0;
  170. }
  171. break;
  172. case VENDOR_NEC:{
  173. unsigned long min, sec, frame;
  174. cgc.cmd[0] = 0xde;
  175. cgc.cmd[1] = 0x03;
  176. cgc.cmd[2] = 0xb0;
  177. cgc.buffer = buffer;
  178. cgc.buflen = 0x16;
  179. cgc.quiet = 1;
  180. cgc.data_direction = DMA_FROM_DEVICE;
  181. cgc.timeout = VENDOR_TIMEOUT;
  182. rc = sr_do_ioctl(cd, &cgc);
  183. if (rc != 0)
  184. break;
  185. if (buffer[14] != 0 && buffer[14] != 0xb0) {
  186. sr_printk(KERN_INFO, cd, "Hmm, seems the cdrom "
  187. "doesn't support multisession CD's\n");
  188. no_multi = 1;
  189. break;
  190. }
  191. min = bcd2bin(buffer[15]);
  192. sec = bcd2bin(buffer[16]);
  193. frame = bcd2bin(buffer[17]);
  194. sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
  195. break;
  196. }
  197. case VENDOR_TOSHIBA:{
  198. unsigned long min, sec, frame;
  199. /* we request some disc information (is it a XA-CD ?,
  200. * where starts the last session ?) */
  201. cgc.cmd[0] = 0xc7;
  202. cgc.cmd[1] = 0x03;
  203. cgc.buffer = buffer;
  204. cgc.buflen = 4;
  205. cgc.quiet = 1;
  206. cgc.data_direction = DMA_FROM_DEVICE;
  207. cgc.timeout = VENDOR_TIMEOUT;
  208. rc = sr_do_ioctl(cd, &cgc);
  209. if (rc == -EINVAL) {
  210. sr_printk(KERN_INFO, cd, "Hmm, seems the drive "
  211. "doesn't support multisession CD's\n");
  212. no_multi = 1;
  213. break;
  214. }
  215. if (rc != 0)
  216. break;
  217. min = bcd2bin(buffer[1]);
  218. sec = bcd2bin(buffer[2]);
  219. frame = bcd2bin(buffer[3]);
  220. sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
  221. if (sector)
  222. sector -= CD_MSF_OFFSET;
  223. sr_set_blocklength(cd, 2048);
  224. break;
  225. }
  226. case VENDOR_WRITER:
  227. cgc.cmd[0] = READ_TOC;
  228. cgc.cmd[8] = 0x04;
  229. cgc.cmd[9] = 0x40;
  230. cgc.buffer = buffer;
  231. cgc.buflen = 0x04;
  232. cgc.quiet = 1;
  233. cgc.data_direction = DMA_FROM_DEVICE;
  234. cgc.timeout = VENDOR_TIMEOUT;
  235. rc = sr_do_ioctl(cd, &cgc);
  236. if (rc != 0) {
  237. break;
  238. }
  239. if ((rc = buffer[2]) == 0) {
  240. sr_printk(KERN_WARNING, cd,
  241. "No finished session\n");
  242. break;
  243. }
  244. cgc.cmd[0] = READ_TOC; /* Read TOC */
  245. cgc.cmd[6] = rc & 0x7f; /* number of last session */
  246. cgc.cmd[8] = 0x0c;
  247. cgc.cmd[9] = 0x40;
  248. cgc.buffer = buffer;
  249. cgc.buflen = 12;
  250. cgc.quiet = 1;
  251. cgc.data_direction = DMA_FROM_DEVICE;
  252. cgc.timeout = VENDOR_TIMEOUT;
  253. rc = sr_do_ioctl(cd, &cgc);
  254. if (rc != 0) {
  255. break;
  256. }
  257. sector = buffer[11] + (buffer[10] << 8) +
  258. (buffer[9] << 16) + (buffer[8] << 24);
  259. break;
  260. default:
  261. /* should not happen */
  262. sr_printk(KERN_WARNING, cd,
  263. "unknown vendor code (%i), not initialized ?\n",
  264. cd->vendor);
  265. sector = 0;
  266. no_multi = 1;
  267. break;
  268. }
  269. cd->ms_offset = sector;
  270. cd->xa_flag = 0;
  271. if (CDS_AUDIO != sr_disk_status(cdi) && 1 == sr_is_xa(cd))
  272. cd->xa_flag = 1;
  273. if (2048 != cd->device->sector_size) {
  274. sr_set_blocklength(cd, 2048);
  275. }
  276. if (no_multi)
  277. cdi->mask |= CDC_MULTI_SESSION;
  278. #ifdef DEBUG
  279. if (sector)
  280. sr_printk(KERN_DEBUG, cd, "multisession offset=%lu\n",
  281. sector);
  282. #endif
  283. kfree(buffer);
  284. return rc;
  285. }