sr_vendor.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <[email protected]>
  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 <[email protected]>
  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_CYGNAL_85ED 5 /* CD-on-a-chip */
  57. #define VENDOR_TIMEOUT 30*HZ
  58. void sr_vendor_init(Scsi_CD *cd)
  59. {
  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. } else if (!strncmp(vendor, "Beurer", 6) &&
  86. !strncmp(model, "Gluco Memory", 12)) {
  87. /* The Beurer GL50 evo uses a Cygnal-manufactured CD-on-a-chip
  88. that only accepts a subset of SCSI commands. Most of the
  89. not-implemented commands are fine to fail, but a few,
  90. particularly around the MMC or Audio commands, will put the
  91. device into an unrecoverable state, so they need to be
  92. avoided at all costs.
  93. */
  94. cd->vendor = VENDOR_CYGNAL_85ED;
  95. cd->cdi.mask |= (
  96. CDC_MULTI_SESSION |
  97. CDC_CLOSE_TRAY | CDC_OPEN_TRAY |
  98. CDC_LOCK |
  99. CDC_GENERIC_PACKET |
  100. CDC_PLAY_AUDIO
  101. );
  102. }
  103. }
  104. /* small handy function for switching block length using MODE SELECT,
  105. * used by sr_read_sector() */
  106. int sr_set_blocklength(Scsi_CD *cd, int blocklength)
  107. {
  108. unsigned char *buffer; /* the buffer for the ioctl */
  109. struct packet_command cgc;
  110. struct ccs_modesel_head *modesel;
  111. int rc, density = 0;
  112. if (cd->vendor == VENDOR_TOSHIBA)
  113. density = (blocklength > 2048) ? 0x81 : 0x83;
  114. buffer = kmalloc(512, GFP_KERNEL);
  115. if (!buffer)
  116. return -ENOMEM;
  117. #ifdef DEBUG
  118. sr_printk(KERN_INFO, cd, "MODE SELECT 0x%x/%d\n", density, blocklength);
  119. #endif
  120. memset(&cgc, 0, sizeof(struct packet_command));
  121. cgc.cmd[0] = MODE_SELECT;
  122. cgc.cmd[1] = (1 << 4);
  123. cgc.cmd[4] = 12;
  124. modesel = (struct ccs_modesel_head *) buffer;
  125. memset(modesel, 0, sizeof(*modesel));
  126. modesel->block_desc_length = 0x08;
  127. modesel->density = density;
  128. modesel->block_length_med = (blocklength >> 8) & 0xff;
  129. modesel->block_length_lo = blocklength & 0xff;
  130. cgc.buffer = buffer;
  131. cgc.buflen = sizeof(*modesel);
  132. cgc.data_direction = DMA_TO_DEVICE;
  133. cgc.timeout = VENDOR_TIMEOUT;
  134. if (0 == (rc = sr_do_ioctl(cd, &cgc))) {
  135. cd->device->sector_size = blocklength;
  136. }
  137. #ifdef DEBUG
  138. else
  139. sr_printk(KERN_INFO, cd,
  140. "switching blocklength to %d bytes failed\n",
  141. blocklength);
  142. #endif
  143. kfree(buffer);
  144. return rc;
  145. }
  146. /* This function gets called after a media change. Checks if the CD is
  147. multisession, asks for offset etc. */
  148. int sr_cd_check(struct cdrom_device_info *cdi)
  149. {
  150. Scsi_CD *cd = cdi->handle;
  151. unsigned long sector;
  152. unsigned char *buffer; /* the buffer for the ioctl */
  153. struct packet_command cgc;
  154. int rc, no_multi;
  155. if (cd->cdi.mask & CDC_MULTI_SESSION)
  156. return 0;
  157. buffer = kmalloc(512, GFP_KERNEL);
  158. if (!buffer)
  159. return -ENOMEM;
  160. sector = 0; /* the multisession sector offset goes here */
  161. no_multi = 0; /* flag: the drive can't handle multisession */
  162. rc = 0;
  163. memset(&cgc, 0, sizeof(struct packet_command));
  164. switch (cd->vendor) {
  165. case VENDOR_SCSI3:
  166. cgc.cmd[0] = READ_TOC;
  167. cgc.cmd[8] = 12;
  168. cgc.cmd[9] = 0x40;
  169. cgc.buffer = buffer;
  170. cgc.buflen = 12;
  171. cgc.quiet = 1;
  172. cgc.data_direction = DMA_FROM_DEVICE;
  173. cgc.timeout = VENDOR_TIMEOUT;
  174. rc = sr_do_ioctl(cd, &cgc);
  175. if (rc != 0)
  176. break;
  177. if ((buffer[0] << 8) + buffer[1] < 0x0a) {
  178. sr_printk(KERN_INFO, cd, "Hmm, seems the drive "
  179. "doesn't support multisession CD's\n");
  180. no_multi = 1;
  181. break;
  182. }
  183. sector = buffer[11] + (buffer[10] << 8) +
  184. (buffer[9] << 16) + (buffer[8] << 24);
  185. if (buffer[6] <= 1) {
  186. /* ignore sector offsets from first track */
  187. sector = 0;
  188. }
  189. break;
  190. case VENDOR_NEC:{
  191. unsigned long min, sec, frame;
  192. cgc.cmd[0] = 0xde;
  193. cgc.cmd[1] = 0x03;
  194. cgc.cmd[2] = 0xb0;
  195. cgc.buffer = buffer;
  196. cgc.buflen = 0x16;
  197. cgc.quiet = 1;
  198. cgc.data_direction = DMA_FROM_DEVICE;
  199. cgc.timeout = VENDOR_TIMEOUT;
  200. rc = sr_do_ioctl(cd, &cgc);
  201. if (rc != 0)
  202. break;
  203. if (buffer[14] != 0 && buffer[14] != 0xb0) {
  204. sr_printk(KERN_INFO, cd, "Hmm, seems the cdrom "
  205. "doesn't support multisession CD's\n");
  206. no_multi = 1;
  207. break;
  208. }
  209. min = bcd2bin(buffer[15]);
  210. sec = bcd2bin(buffer[16]);
  211. frame = bcd2bin(buffer[17]);
  212. sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
  213. break;
  214. }
  215. case VENDOR_TOSHIBA:{
  216. unsigned long min, sec, frame;
  217. /* we request some disc information (is it a XA-CD ?,
  218. * where starts the last session ?) */
  219. cgc.cmd[0] = 0xc7;
  220. cgc.cmd[1] = 0x03;
  221. cgc.buffer = buffer;
  222. cgc.buflen = 4;
  223. cgc.quiet = 1;
  224. cgc.data_direction = DMA_FROM_DEVICE;
  225. cgc.timeout = VENDOR_TIMEOUT;
  226. rc = sr_do_ioctl(cd, &cgc);
  227. if (rc == -EINVAL) {
  228. sr_printk(KERN_INFO, cd, "Hmm, seems the drive "
  229. "doesn't support multisession CD's\n");
  230. no_multi = 1;
  231. break;
  232. }
  233. if (rc != 0)
  234. break;
  235. min = bcd2bin(buffer[1]);
  236. sec = bcd2bin(buffer[2]);
  237. frame = bcd2bin(buffer[3]);
  238. sector = min * CD_SECS * CD_FRAMES + sec * CD_FRAMES + frame;
  239. if (sector)
  240. sector -= CD_MSF_OFFSET;
  241. sr_set_blocklength(cd, 2048);
  242. break;
  243. }
  244. case VENDOR_WRITER:
  245. cgc.cmd[0] = READ_TOC;
  246. cgc.cmd[8] = 0x04;
  247. cgc.cmd[9] = 0x40;
  248. cgc.buffer = buffer;
  249. cgc.buflen = 0x04;
  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. if ((rc = buffer[2]) == 0) {
  258. sr_printk(KERN_WARNING, cd,
  259. "No finished session\n");
  260. break;
  261. }
  262. cgc.cmd[0] = READ_TOC; /* Read TOC */
  263. cgc.cmd[6] = rc & 0x7f; /* number of last session */
  264. cgc.cmd[8] = 0x0c;
  265. cgc.cmd[9] = 0x40;
  266. cgc.buffer = buffer;
  267. cgc.buflen = 12;
  268. cgc.quiet = 1;
  269. cgc.data_direction = DMA_FROM_DEVICE;
  270. cgc.timeout = VENDOR_TIMEOUT;
  271. rc = sr_do_ioctl(cd, &cgc);
  272. if (rc != 0) {
  273. break;
  274. }
  275. sector = buffer[11] + (buffer[10] << 8) +
  276. (buffer[9] << 16) + (buffer[8] << 24);
  277. break;
  278. default:
  279. /* should not happen */
  280. sr_printk(KERN_WARNING, cd,
  281. "unknown vendor code (%i), not initialized ?\n",
  282. cd->vendor);
  283. sector = 0;
  284. no_multi = 1;
  285. break;
  286. }
  287. cd->ms_offset = sector;
  288. cd->xa_flag = 0;
  289. if (CDS_AUDIO != sr_disk_status(cdi) && 1 == sr_is_xa(cd))
  290. cd->xa_flag = 1;
  291. if (2048 != cd->device->sector_size) {
  292. sr_set_blocklength(cd, 2048);
  293. }
  294. if (no_multi)
  295. cdi->mask |= CDC_MULTI_SESSION;
  296. #ifdef DEBUG
  297. if (sector)
  298. sr_printk(KERN_DEBUG, cd, "multisession offset=%lu\n",
  299. sector);
  300. #endif
  301. kfree(buffer);
  302. return rc;
  303. }