sr_vendor.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <scsi/scsi.h>
  41. #include <scsi/scsi_cmnd.h>
  42. #include <scsi/scsi_device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_ioctl.h>
  45. #include "sr.h"
  46. #if 0
  47. #define DEBUG
  48. #endif
  49. /* here are some constants to sort the vendors into groups */
  50. #define VENDOR_SCSI3 1 /* default: scsi-3 mmc */
  51. #define VENDOR_NEC 2
  52. #define VENDOR_TOSHIBA 3
  53. #define VENDOR_WRITER 4 /* pre-scsi3 writers */
  54. #define VENDOR_TIMEOUT 30*HZ
  55. void sr_vendor_init(Scsi_CD *cd)
  56. {
  57. #ifndef CONFIG_BLK_DEV_SR_VENDOR
  58. cd->vendor = VENDOR_SCSI3;
  59. #else
  60. const char *vendor = cd->device->vendor;
  61. const char *model = cd->device->model;
  62. /* default */
  63. cd->vendor = VENDOR_SCSI3;
  64. if (cd->readcd_known)
  65. /* this is true for scsi3/mmc drives - no more checks */
  66. return;
  67. if (cd->device->type == TYPE_WORM) {
  68. cd->vendor = VENDOR_WRITER;
  69. } else if (!strncmp(vendor, "NEC", 3)) {
  70. cd->vendor = VENDOR_NEC;
  71. if (!strncmp(model, "CD-ROM DRIVE:25", 15) ||
  72. !strncmp(model, "CD-ROM DRIVE:36", 15) ||
  73. !strncmp(model, "CD-ROM DRIVE:83", 15) ||
  74. !strncmp(model, "CD-ROM DRIVE:84 ", 16)
  75. #if 0
  76. /* my NEC 3x returns the read-raw data if a read-raw
  77. is followed by a read for the same sector - aeb */
  78. || !strncmp(model, "CD-ROM DRIVE:500", 16)
  79. #endif
  80. )
  81. /* these can't handle multisession, may hang */
  82. cd->cdi.mask |= CDC_MULTI_SESSION;
  83. } else if (!strncmp(vendor, "TOSHIBA", 7)) {
  84. cd->vendor = VENDOR_TOSHIBA;
  85. }
  86. #endif
  87. }
  88. /* small handy function for switching block length using MODE SELECT,
  89. * used by sr_read_sector() */
  90. int sr_set_blocklength(Scsi_CD *cd, int blocklength)
  91. {
  92. unsigned char *buffer; /* the buffer for the ioctl */
  93. struct packet_command cgc;
  94. struct ccs_modesel_head *modesel;
  95. int rc, density = 0;
  96. #ifdef CONFIG_BLK_DEV_SR_VENDOR
  97. if (cd->vendor == VENDOR_TOSHIBA)
  98. density = (blocklength > 2048) ? 0x81 : 0x83;
  99. #endif
  100. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  101. if (!buffer)
  102. return -ENOMEM;
  103. #ifdef DEBUG
  104. printk("%s: MODE SELECT 0x%x/%d\n", cd->cdi.name, density, blocklength);
  105. #endif
  106. memset(&cgc, 0, sizeof(struct packet_command));
  107. cgc.cmd[0] = MODE_SELECT;
  108. cgc.cmd[1] = (1 << 4);
  109. cgc.cmd[4] = 12;
  110. modesel = (struct ccs_modesel_head *) buffer;
  111. memset(modesel, 0, sizeof(*modesel));
  112. modesel->block_desc_length = 0x08;
  113. modesel->density = density;
  114. modesel->block_length_med = (blocklength >> 8) & 0xff;
  115. modesel->block_length_lo = blocklength & 0xff;
  116. cgc.buffer = buffer;
  117. cgc.buflen = sizeof(*modesel);
  118. cgc.data_direction = DMA_TO_DEVICE;
  119. cgc.timeout = VENDOR_TIMEOUT;
  120. if (0 == (rc = sr_do_ioctl(cd, &cgc))) {
  121. cd->device->sector_size = blocklength;
  122. }
  123. #ifdef DEBUG
  124. else
  125. printk("%s: switching blocklength to %d bytes failed\n",
  126. cd->cdi.name, blocklength);
  127. #endif
  128. kfree(buffer);
  129. return rc;
  130. }
  131. /* This function gets called after a media change. Checks if the CD is
  132. multisession, asks for offset etc. */
  133. int sr_cd_check(struct cdrom_device_info *cdi)
  134. {
  135. Scsi_CD *cd = cdi->handle;
  136. unsigned long sector;
  137. unsigned char *buffer; /* the buffer for the ioctl */
  138. struct packet_command cgc;
  139. int rc, no_multi;
  140. if (cd->cdi.mask & CDC_MULTI_SESSION)
  141. return 0;
  142. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  143. if (!buffer)
  144. return -ENOMEM;
  145. sector = 0; /* the multisession sector offset goes here */
  146. no_multi = 0; /* flag: the drive can't handle multisession */
  147. rc = 0;
  148. memset(&cgc, 0, sizeof(struct packet_command));
  149. switch (cd->vendor) {
  150. case VENDOR_SCSI3:
  151. cgc.cmd[0] = READ_TOC;
  152. cgc.cmd[8] = 12;
  153. cgc.cmd[9] = 0x40;
  154. cgc.buffer = buffer;
  155. cgc.buflen = 12;
  156. cgc.quiet = 1;
  157. cgc.data_direction = DMA_FROM_DEVICE;
  158. cgc.timeout = VENDOR_TIMEOUT;
  159. rc = sr_do_ioctl(cd, &cgc);
  160. if (rc != 0)
  161. break;
  162. if ((buffer[0] << 8) + buffer[1] < 0x0a) {
  163. printk(KERN_INFO "%s: Hmm, seems the drive "
  164. "doesn't support multisession CD's\n", cd->cdi.name);
  165. no_multi = 1;
  166. break;
  167. }
  168. sector = buffer[11] + (buffer[10] << 8) +
  169. (buffer[9] << 16) + (buffer[8] << 24);
  170. if (buffer[6] <= 1) {
  171. /* ignore sector offsets from first track */
  172. sector = 0;
  173. }
  174. break;
  175. #ifdef CONFIG_BLK_DEV_SR_VENDOR
  176. case VENDOR_NEC:{
  177. unsigned long min, sec, frame;
  178. cgc.cmd[0] = 0xde;
  179. cgc.cmd[1] = 0x03;
  180. cgc.cmd[2] = 0xb0;
  181. cgc.buffer = buffer;
  182. cgc.buflen = 0x16;
  183. cgc.quiet = 1;
  184. cgc.data_direction = DMA_FROM_DEVICE;
  185. cgc.timeout = VENDOR_TIMEOUT;
  186. rc = sr_do_ioctl(cd, &cgc);
  187. if (rc != 0)
  188. break;
  189. if (buffer[14] != 0 && buffer[14] != 0xb0) {
  190. printk(KERN_INFO "%s: Hmm, seems the cdrom "
  191. "doesn't support multisession CD's\n",
  192. cd->cdi.name);
  193. no_multi = 1;
  194. break;
  195. }
  196. min = bcd2bin(buffer[15]);
  197. sec = bcd2bin(buffer[16]);
  198. frame = bcd2bin(buffer[17]);
  199. sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
  200. break;
  201. }
  202. case VENDOR_TOSHIBA:{
  203. unsigned long min, sec, frame;
  204. /* we request some disc information (is it a XA-CD ?,
  205. * where starts the last session ?) */
  206. cgc.cmd[0] = 0xc7;
  207. cgc.cmd[1] = 0x03;
  208. cgc.buffer = buffer;
  209. cgc.buflen = 4;
  210. cgc.quiet = 1;
  211. cgc.data_direction = DMA_FROM_DEVICE;
  212. cgc.timeout = VENDOR_TIMEOUT;
  213. rc = sr_do_ioctl(cd, &cgc);
  214. if (rc == -EINVAL) {
  215. printk(KERN_INFO "%s: Hmm, seems the drive "
  216. "doesn't support multisession CD's\n",
  217. cd->cdi.name);
  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. printk(KERN_WARNING
  247. "%s: No finished session\n", cd->cdi.name);
  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. printk(KERN_WARNING
  270. "%s: unknown vendor code (%i), not initialized ?\n",
  271. cd->cdi.name, 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. printk(KERN_DEBUG "%s: multisession offset=%lu\n",
  288. cd->cdi.name, sector);
  289. #endif
  290. kfree(buffer);
  291. return rc;
  292. }