sr_ioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. #include <linux/kernel.h>
  2. #include <linux/mm.h>
  3. #include <linux/fs.h>
  4. #include <linux/errno.h>
  5. #include <linux/string.h>
  6. #include <linux/blkdev.h>
  7. #include <linux/blkpg.h>
  8. #include <linux/cdrom.h>
  9. #include <linux/delay.h>
  10. #include <asm/io.h>
  11. #include <asm/uaccess.h>
  12. #include <scsi/scsi.h>
  13. #include <scsi/scsi_dbg.h>
  14. #include <scsi/scsi_device.h>
  15. #include <scsi/scsi_eh.h>
  16. #include <scsi/scsi_host.h>
  17. #include <scsi/scsi_ioctl.h>
  18. #include <scsi/scsi_cmnd.h>
  19. #include "sr.h"
  20. #if 0
  21. #define DEBUG
  22. #endif
  23. /* The sr_is_xa() seems to trigger firmware bugs with some drives :-(
  24. * It is off by default and can be turned on with this module parameter */
  25. static int xa_test = 0;
  26. module_param(xa_test, int, S_IRUGO | S_IWUSR);
  27. /* primitive to determine whether we need to have GFP_DMA set based on
  28. * the status of the unchecked_isa_dma flag in the host structure */
  29. #define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
  30. static int sr_read_tochdr(struct cdrom_device_info *cdi,
  31. struct cdrom_tochdr *tochdr)
  32. {
  33. struct scsi_cd *cd = cdi->handle;
  34. struct packet_command cgc;
  35. int result;
  36. unsigned char *buffer;
  37. buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
  38. if (!buffer)
  39. return -ENOMEM;
  40. memset(&cgc, 0, sizeof(struct packet_command));
  41. cgc.timeout = IOCTL_TIMEOUT;
  42. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  43. cgc.cmd[8] = 12; /* LSB of length */
  44. cgc.buffer = buffer;
  45. cgc.buflen = 12;
  46. cgc.quiet = 1;
  47. cgc.data_direction = DMA_FROM_DEVICE;
  48. result = sr_do_ioctl(cd, &cgc);
  49. tochdr->cdth_trk0 = buffer[2];
  50. tochdr->cdth_trk1 = buffer[3];
  51. kfree(buffer);
  52. return result;
  53. }
  54. static int sr_read_tocentry(struct cdrom_device_info *cdi,
  55. struct cdrom_tocentry *tocentry)
  56. {
  57. struct scsi_cd *cd = cdi->handle;
  58. struct packet_command cgc;
  59. int result;
  60. unsigned char *buffer;
  61. buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
  62. if (!buffer)
  63. return -ENOMEM;
  64. memset(&cgc, 0, sizeof(struct packet_command));
  65. cgc.timeout = IOCTL_TIMEOUT;
  66. cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  67. cgc.cmd[1] |= (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0;
  68. cgc.cmd[6] = tocentry->cdte_track;
  69. cgc.cmd[8] = 12; /* LSB of length */
  70. cgc.buffer = buffer;
  71. cgc.buflen = 12;
  72. cgc.data_direction = DMA_FROM_DEVICE;
  73. result = sr_do_ioctl(cd, &cgc);
  74. tocentry->cdte_ctrl = buffer[5] & 0xf;
  75. tocentry->cdte_adr = buffer[5] >> 4;
  76. tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
  77. if (tocentry->cdte_format == CDROM_MSF) {
  78. tocentry->cdte_addr.msf.minute = buffer[9];
  79. tocentry->cdte_addr.msf.second = buffer[10];
  80. tocentry->cdte_addr.msf.frame = buffer[11];
  81. } else
  82. tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
  83. + buffer[10]) << 8) + buffer[11];
  84. kfree(buffer);
  85. return result;
  86. }
  87. #define IOCTL_RETRIES 3
  88. /* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command. When these drives
  89. are emulating a SCSI device via the idescsi module, they need to have
  90. CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
  91. static int sr_fake_playtrkind(struct cdrom_device_info *cdi, struct cdrom_ti *ti)
  92. {
  93. struct cdrom_tocentry trk0_te, trk1_te;
  94. struct cdrom_tochdr tochdr;
  95. struct packet_command cgc;
  96. int ntracks, ret;
  97. ret = sr_read_tochdr(cdi, &tochdr);
  98. if (ret)
  99. return ret;
  100. ntracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
  101. if (ti->cdti_trk1 == ntracks)
  102. ti->cdti_trk1 = CDROM_LEADOUT;
  103. else if (ti->cdti_trk1 != CDROM_LEADOUT)
  104. ti->cdti_trk1 ++;
  105. trk0_te.cdte_track = ti->cdti_trk0;
  106. trk0_te.cdte_format = CDROM_MSF;
  107. trk1_te.cdte_track = ti->cdti_trk1;
  108. trk1_te.cdte_format = CDROM_MSF;
  109. ret = sr_read_tocentry(cdi, &trk0_te);
  110. if (ret)
  111. return ret;
  112. ret = sr_read_tocentry(cdi, &trk1_te);
  113. if (ret)
  114. return ret;
  115. memset(&cgc, 0, sizeof(struct packet_command));
  116. cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
  117. cgc.cmd[3] = trk0_te.cdte_addr.msf.minute;
  118. cgc.cmd[4] = trk0_te.cdte_addr.msf.second;
  119. cgc.cmd[5] = trk0_te.cdte_addr.msf.frame;
  120. cgc.cmd[6] = trk1_te.cdte_addr.msf.minute;
  121. cgc.cmd[7] = trk1_te.cdte_addr.msf.second;
  122. cgc.cmd[8] = trk1_te.cdte_addr.msf.frame;
  123. cgc.data_direction = DMA_NONE;
  124. cgc.timeout = IOCTL_TIMEOUT;
  125. return sr_do_ioctl(cdi->handle, &cgc);
  126. }
  127. static int sr_play_trkind(struct cdrom_device_info *cdi,
  128. struct cdrom_ti *ti)
  129. {
  130. struct scsi_cd *cd = cdi->handle;
  131. struct packet_command cgc;
  132. int result;
  133. memset(&cgc, 0, sizeof(struct packet_command));
  134. cgc.timeout = IOCTL_TIMEOUT;
  135. cgc.cmd[0] = GPCMD_PLAYAUDIO_TI;
  136. cgc.cmd[4] = ti->cdti_trk0;
  137. cgc.cmd[5] = ti->cdti_ind0;
  138. cgc.cmd[7] = ti->cdti_trk1;
  139. cgc.cmd[8] = ti->cdti_ind1;
  140. cgc.data_direction = DMA_NONE;
  141. result = sr_do_ioctl(cd, &cgc);
  142. if (result == -EDRIVE_CANT_DO_THIS)
  143. result = sr_fake_playtrkind(cdi, ti);
  144. return result;
  145. }
  146. /* We do our own retries because we want to know what the specific
  147. error code is. Normally the UNIT_ATTENTION code will automatically
  148. clear after one error */
  149. int sr_do_ioctl(Scsi_CD *cd, struct packet_command *cgc)
  150. {
  151. struct scsi_device *SDev;
  152. struct scsi_sense_hdr sshdr;
  153. int result, err = 0, retries = 0;
  154. struct request_sense *sense = cgc->sense;
  155. SDev = cd->device;
  156. if (!sense) {
  157. sense = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  158. if (!sense) {
  159. err = -ENOMEM;
  160. goto out;
  161. }
  162. }
  163. retry:
  164. if (!scsi_block_when_processing_errors(SDev)) {
  165. err = -ENODEV;
  166. goto out;
  167. }
  168. memset(sense, 0, sizeof(*sense));
  169. result = scsi_execute(SDev, cgc->cmd, cgc->data_direction,
  170. cgc->buffer, cgc->buflen, (char *)sense,
  171. cgc->timeout, IOCTL_RETRIES, 0);
  172. scsi_normalize_sense((char *)sense, sizeof(*sense), &sshdr);
  173. /* Minimal error checking. Ignore cases we know about, and report the rest. */
  174. if (driver_byte(result) != 0) {
  175. switch (sshdr.sense_key) {
  176. case UNIT_ATTENTION:
  177. SDev->changed = 1;
  178. if (!cgc->quiet)
  179. printk(KERN_INFO "%s: disc change detected.\n", cd->cdi.name);
  180. if (retries++ < 10)
  181. goto retry;
  182. err = -ENOMEDIUM;
  183. break;
  184. case NOT_READY: /* This happens if there is no disc in drive */
  185. if (sshdr.asc == 0x04 &&
  186. sshdr.ascq == 0x01) {
  187. /* sense: Logical unit is in process of becoming ready */
  188. if (!cgc->quiet)
  189. printk(KERN_INFO "%s: CDROM not ready yet.\n", cd->cdi.name);
  190. if (retries++ < 10) {
  191. /* sleep 2 sec and try again */
  192. ssleep(2);
  193. goto retry;
  194. } else {
  195. /* 20 secs are enough? */
  196. err = -ENOMEDIUM;
  197. break;
  198. }
  199. }
  200. if (!cgc->quiet)
  201. printk(KERN_INFO "%s: CDROM not ready. Make sure there is a disc in the drive.\n", cd->cdi.name);
  202. #ifdef DEBUG
  203. scsi_print_sense_hdr("sr", &sshdr);
  204. #endif
  205. err = -ENOMEDIUM;
  206. break;
  207. case ILLEGAL_REQUEST:
  208. err = -EIO;
  209. if (sshdr.asc == 0x20 &&
  210. sshdr.ascq == 0x00)
  211. /* sense: Invalid command operation code */
  212. err = -EDRIVE_CANT_DO_THIS;
  213. #ifdef DEBUG
  214. __scsi_print_command(cgc->cmd);
  215. scsi_print_sense_hdr("sr", &sshdr);
  216. #endif
  217. break;
  218. default:
  219. printk(KERN_ERR "%s: CDROM (ioctl) error, command: ", cd->cdi.name);
  220. __scsi_print_command(cgc->cmd);
  221. scsi_print_sense_hdr("sr", &sshdr);
  222. err = -EIO;
  223. }
  224. }
  225. /* Wake up a process waiting for device */
  226. out:
  227. if (!cgc->sense)
  228. kfree(sense);
  229. cgc->stat = err;
  230. return err;
  231. }
  232. /* ---------------------------------------------------------------------- */
  233. /* interface to cdrom.c */
  234. int sr_tray_move(struct cdrom_device_info *cdi, int pos)
  235. {
  236. Scsi_CD *cd = cdi->handle;
  237. struct packet_command cgc;
  238. memset(&cgc, 0, sizeof(struct packet_command));
  239. cgc.cmd[0] = GPCMD_START_STOP_UNIT;
  240. cgc.cmd[4] = (pos == 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
  241. cgc.data_direction = DMA_NONE;
  242. cgc.timeout = IOCTL_TIMEOUT;
  243. return sr_do_ioctl(cd, &cgc);
  244. }
  245. int sr_lock_door(struct cdrom_device_info *cdi, int lock)
  246. {
  247. Scsi_CD *cd = cdi->handle;
  248. return scsi_set_medium_removal(cd->device, lock ?
  249. SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
  250. }
  251. int sr_drive_status(struct cdrom_device_info *cdi, int slot)
  252. {
  253. struct scsi_cd *cd = cdi->handle;
  254. struct scsi_sense_hdr sshdr;
  255. struct media_event_desc med;
  256. if (CDSL_CURRENT != slot) {
  257. /* we have no changer support */
  258. return -EINVAL;
  259. }
  260. if (0 == sr_test_unit_ready(cd->device, &sshdr))
  261. return CDS_DISC_OK;
  262. if (!cdrom_get_media_event(cdi, &med)) {
  263. if (med.media_present)
  264. return CDS_DISC_OK;
  265. else if (med.door_open)
  266. return CDS_TRAY_OPEN;
  267. else
  268. return CDS_NO_DISC;
  269. }
  270. /*
  271. * 0x04 is format in progress .. but there must be a disc present!
  272. */
  273. if (sshdr.sense_key == NOT_READY && sshdr.asc == 0x04)
  274. return CDS_DISC_OK;
  275. /*
  276. * If not using Mt Fuji extended media tray reports,
  277. * just return TRAY_OPEN since ATAPI doesn't provide
  278. * any other way to detect this...
  279. */
  280. if (scsi_sense_valid(&sshdr) &&
  281. /* 0x3a is medium not present */
  282. sshdr.asc == 0x3a)
  283. return CDS_NO_DISC;
  284. else
  285. return CDS_TRAY_OPEN;
  286. return CDS_DRIVE_NOT_READY;
  287. }
  288. int sr_disk_status(struct cdrom_device_info *cdi)
  289. {
  290. Scsi_CD *cd = cdi->handle;
  291. struct cdrom_tochdr toc_h;
  292. struct cdrom_tocentry toc_e;
  293. int i, rc, have_datatracks = 0;
  294. /* look for data tracks */
  295. rc = sr_read_tochdr(cdi, &toc_h);
  296. if (rc)
  297. return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
  298. for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
  299. toc_e.cdte_track = i;
  300. toc_e.cdte_format = CDROM_LBA;
  301. if (sr_read_tocentry(cdi, &toc_e))
  302. return CDS_NO_INFO;
  303. if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
  304. have_datatracks = 1;
  305. break;
  306. }
  307. }
  308. if (!have_datatracks)
  309. return CDS_AUDIO;
  310. if (cd->xa_flag)
  311. return CDS_XA_2_1;
  312. else
  313. return CDS_DATA_1;
  314. }
  315. int sr_get_last_session(struct cdrom_device_info *cdi,
  316. struct cdrom_multisession *ms_info)
  317. {
  318. Scsi_CD *cd = cdi->handle;
  319. ms_info->addr.lba = cd->ms_offset;
  320. ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
  321. return 0;
  322. }
  323. int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
  324. {
  325. Scsi_CD *cd = cdi->handle;
  326. struct packet_command cgc;
  327. char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
  328. int result;
  329. if (!buffer)
  330. return -ENOMEM;
  331. memset(&cgc, 0, sizeof(struct packet_command));
  332. cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
  333. cgc.cmd[2] = 0x40; /* I do want the subchannel info */
  334. cgc.cmd[3] = 0x02; /* Give me medium catalog number info */
  335. cgc.cmd[8] = 24;
  336. cgc.buffer = buffer;
  337. cgc.buflen = 24;
  338. cgc.data_direction = DMA_FROM_DEVICE;
  339. cgc.timeout = IOCTL_TIMEOUT;
  340. result = sr_do_ioctl(cd, &cgc);
  341. memcpy(mcn->medium_catalog_number, buffer + 9, 13);
  342. mcn->medium_catalog_number[13] = 0;
  343. kfree(buffer);
  344. return result;
  345. }
  346. int sr_reset(struct cdrom_device_info *cdi)
  347. {
  348. return 0;
  349. }
  350. int sr_select_speed(struct cdrom_device_info *cdi, int speed)
  351. {
  352. Scsi_CD *cd = cdi->handle;
  353. struct packet_command cgc;
  354. if (speed == 0)
  355. speed = 0xffff; /* set to max */
  356. else
  357. speed *= 177; /* Nx to kbyte/s */
  358. memset(&cgc, 0, sizeof(struct packet_command));
  359. cgc.cmd[0] = GPCMD_SET_SPEED; /* SET CD SPEED */
  360. cgc.cmd[2] = (speed >> 8) & 0xff; /* MSB for speed (in kbytes/sec) */
  361. cgc.cmd[3] = speed & 0xff; /* LSB */
  362. cgc.data_direction = DMA_NONE;
  363. cgc.timeout = IOCTL_TIMEOUT;
  364. if (sr_do_ioctl(cd, &cgc))
  365. return -EIO;
  366. return 0;
  367. }
  368. /* ----------------------------------------------------------------------- */
  369. /* this is called by the generic cdrom driver. arg is a _kernel_ pointer, */
  370. /* because the generic cdrom driver does the user access stuff for us. */
  371. /* only cdromreadtochdr and cdromreadtocentry are left - for use with the */
  372. /* sr_disk_status interface for the generic cdrom driver. */
  373. int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
  374. {
  375. switch (cmd) {
  376. case CDROMREADTOCHDR:
  377. return sr_read_tochdr(cdi, arg);
  378. case CDROMREADTOCENTRY:
  379. return sr_read_tocentry(cdi, arg);
  380. case CDROMPLAYTRKIND:
  381. return sr_play_trkind(cdi, arg);
  382. default:
  383. return -EINVAL;
  384. }
  385. }
  386. /* -----------------------------------------------------------------------
  387. * a function to read all sorts of funny cdrom sectors using the READ_CD
  388. * scsi-3 mmc command
  389. *
  390. * lba: linear block address
  391. * format: 0 = data (anything)
  392. * 1 = audio
  393. * 2 = data (mode 1)
  394. * 3 = data (mode 2)
  395. * 4 = data (mode 2 form1)
  396. * 5 = data (mode 2 form2)
  397. * blksize: 2048 | 2336 | 2340 | 2352
  398. */
  399. static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
  400. {
  401. struct packet_command cgc;
  402. #ifdef DEBUG
  403. printk("%s: sr_read_cd lba=%d format=%d blksize=%d\n",
  404. cd->cdi.name, lba, format, blksize);
  405. #endif
  406. memset(&cgc, 0, sizeof(struct packet_command));
  407. cgc.cmd[0] = GPCMD_READ_CD; /* READ_CD */
  408. cgc.cmd[1] = ((format & 7) << 2);
  409. cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
  410. cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
  411. cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
  412. cgc.cmd[5] = (unsigned char) lba & 0xff;
  413. cgc.cmd[8] = 1;
  414. switch (blksize) {
  415. case 2336:
  416. cgc.cmd[9] = 0x58;
  417. break;
  418. case 2340:
  419. cgc.cmd[9] = 0x78;
  420. break;
  421. case 2352:
  422. cgc.cmd[9] = 0xf8;
  423. break;
  424. default:
  425. cgc.cmd[9] = 0x10;
  426. break;
  427. }
  428. cgc.buffer = dest;
  429. cgc.buflen = blksize;
  430. cgc.data_direction = DMA_FROM_DEVICE;
  431. cgc.timeout = IOCTL_TIMEOUT;
  432. return sr_do_ioctl(cd, &cgc);
  433. }
  434. /*
  435. * read sectors with blocksizes other than 2048
  436. */
  437. static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
  438. {
  439. struct packet_command cgc;
  440. int rc;
  441. /* we try the READ CD command first... */
  442. if (cd->readcd_known) {
  443. rc = sr_read_cd(cd, dest, lba, 0, blksize);
  444. if (-EDRIVE_CANT_DO_THIS != rc)
  445. return rc;
  446. cd->readcd_known = 0;
  447. printk("CDROM does'nt support READ CD (0xbe) command\n");
  448. /* fall & retry the other way */
  449. }
  450. /* ... if this fails, we switch the blocksize using MODE SELECT */
  451. if (blksize != cd->device->sector_size) {
  452. if (0 != (rc = sr_set_blocklength(cd, blksize)))
  453. return rc;
  454. }
  455. #ifdef DEBUG
  456. printk("%s: sr_read_sector lba=%d blksize=%d\n", cd->cdi.name, lba, blksize);
  457. #endif
  458. memset(&cgc, 0, sizeof(struct packet_command));
  459. cgc.cmd[0] = GPCMD_READ_10;
  460. cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
  461. cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
  462. cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
  463. cgc.cmd[5] = (unsigned char) lba & 0xff;
  464. cgc.cmd[8] = 1;
  465. cgc.buffer = dest;
  466. cgc.buflen = blksize;
  467. cgc.data_direction = DMA_FROM_DEVICE;
  468. cgc.timeout = IOCTL_TIMEOUT;
  469. rc = sr_do_ioctl(cd, &cgc);
  470. return rc;
  471. }
  472. /*
  473. * read a sector in raw mode to check the sector format
  474. * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
  475. */
  476. int sr_is_xa(Scsi_CD *cd)
  477. {
  478. unsigned char *raw_sector;
  479. int is_xa;
  480. if (!xa_test)
  481. return 0;
  482. raw_sector = kmalloc(2048, GFP_KERNEL | SR_GFP_DMA(cd));
  483. if (!raw_sector)
  484. return -ENOMEM;
  485. if (0 == sr_read_sector(cd, cd->ms_offset + 16,
  486. CD_FRAMESIZE_RAW1, raw_sector)) {
  487. is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
  488. } else {
  489. /* read a raw sector failed for some reason. */
  490. is_xa = -1;
  491. }
  492. kfree(raw_sector);
  493. #ifdef DEBUG
  494. printk("%s: sr_is_xa: %d\n", cd->cdi.name, is_xa);
  495. #endif
  496. return is_xa;
  497. }