sr_vendor.c 8.3 KB

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