sr_vendor.c 8.4 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. printk("%s: MODE SELECT 0x%x/%d\n", cd->cdi.name, 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. printk("%s: switching blocklength to %d bytes failed\n",
  127. cd->cdi.name, blocklength);
  128. #endif
  129. kfree(buffer);
  130. return rc;
  131. }
  132. /* This function gets called after a media change. Checks if the CD is
  133. multisession, asks for offset etc. */
  134. int sr_cd_check(struct cdrom_device_info *cdi)
  135. {
  136. Scsi_CD *cd = cdi->handle;
  137. unsigned long sector;
  138. unsigned char *buffer; /* the buffer for the ioctl */
  139. struct packet_command cgc;
  140. int rc, no_multi;
  141. if (cd->cdi.mask & CDC_MULTI_SESSION)
  142. return 0;
  143. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  144. if (!buffer)
  145. return -ENOMEM;
  146. sector = 0; /* the multisession sector offset goes here */
  147. no_multi = 0; /* flag: the drive can't handle multisession */
  148. rc = 0;
  149. memset(&cgc, 0, sizeof(struct packet_command));
  150. switch (cd->vendor) {
  151. case VENDOR_SCSI3:
  152. cgc.cmd[0] = READ_TOC;
  153. cgc.cmd[8] = 12;
  154. cgc.cmd[9] = 0x40;
  155. cgc.buffer = buffer;
  156. cgc.buflen = 12;
  157. cgc.quiet = 1;
  158. cgc.data_direction = DMA_FROM_DEVICE;
  159. cgc.timeout = VENDOR_TIMEOUT;
  160. rc = sr_do_ioctl(cd, &cgc);
  161. if (rc != 0)
  162. break;
  163. if ((buffer[0] << 8) + buffer[1] < 0x0a) {
  164. printk(KERN_INFO "%s: Hmm, seems the drive "
  165. "doesn't support multisession CD's\n", cd->cdi.name);
  166. no_multi = 1;
  167. break;
  168. }
  169. sector = buffer[11] + (buffer[10] << 8) +
  170. (buffer[9] << 16) + (buffer[8] << 24);
  171. if (buffer[6] <= 1) {
  172. /* ignore sector offsets from first track */
  173. sector = 0;
  174. }
  175. break;
  176. #ifdef CONFIG_BLK_DEV_SR_VENDOR
  177. case VENDOR_NEC:{
  178. unsigned long min, sec, frame;
  179. cgc.cmd[0] = 0xde;
  180. cgc.cmd[1] = 0x03;
  181. cgc.cmd[2] = 0xb0;
  182. cgc.buffer = buffer;
  183. cgc.buflen = 0x16;
  184. cgc.quiet = 1;
  185. cgc.data_direction = DMA_FROM_DEVICE;
  186. cgc.timeout = VENDOR_TIMEOUT;
  187. rc = sr_do_ioctl(cd, &cgc);
  188. if (rc != 0)
  189. break;
  190. if (buffer[14] != 0 && buffer[14] != 0xb0) {
  191. printk(KERN_INFO "%s: Hmm, seems the cdrom "
  192. "doesn't support multisession CD's\n",
  193. cd->cdi.name);
  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. printk(KERN_INFO "%s: Hmm, seems the drive "
  217. "doesn't support multisession CD's\n",
  218. cd->cdi.name);
  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. printk(KERN_WARNING
  248. "%s: No finished session\n", cd->cdi.name);
  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. printk(KERN_WARNING
  271. "%s: unknown vendor code (%i), not initialized ?\n",
  272. cd->cdi.name, 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. printk(KERN_DEBUG "%s: multisession offset=%lu\n",
  289. cd->cdi.name, sector);
  290. #endif
  291. kfree(buffer);
  292. return rc;
  293. }