ide-cd_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * cdrom.c IOCTLs handling for ide-cd driver.
  3. *
  4. * Copyright (C) 1994-1996 Scott Snyder <snyder@fnald0.fnal.gov>
  5. * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
  6. * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/cdrom.h>
  10. #include <linux/gfp.h>
  11. #include <linux/ide.h>
  12. #include <scsi/scsi.h>
  13. #include "ide-cd.h"
  14. /****************************************************************************
  15. * Other driver requests (open, close, check media change).
  16. */
  17. int ide_cdrom_open_real(struct cdrom_device_info *cdi, int purpose)
  18. {
  19. return 0;
  20. }
  21. /*
  22. * Close down the device. Invalidate all cached blocks.
  23. */
  24. void ide_cdrom_release_real(struct cdrom_device_info *cdi)
  25. {
  26. ide_drive_t *drive = cdi->handle;
  27. if (!cdi->use_count)
  28. drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
  29. }
  30. /*
  31. * add logic to try GET_EVENT command first to check for media and tray
  32. * status. this should be supported by newer cd-r/w and all DVD etc
  33. * drives
  34. */
  35. int ide_cdrom_drive_status(struct cdrom_device_info *cdi, int slot_nr)
  36. {
  37. ide_drive_t *drive = cdi->handle;
  38. struct media_event_desc med;
  39. struct request_sense sense;
  40. int stat;
  41. if (slot_nr != CDSL_CURRENT)
  42. return -EINVAL;
  43. stat = cdrom_check_status(drive, &sense);
  44. if (!stat || sense.sense_key == UNIT_ATTENTION)
  45. return CDS_DISC_OK;
  46. if (!cdrom_get_media_event(cdi, &med)) {
  47. if (med.media_present)
  48. return CDS_DISC_OK;
  49. else if (med.door_open)
  50. return CDS_TRAY_OPEN;
  51. else
  52. return CDS_NO_DISC;
  53. }
  54. if (sense.sense_key == NOT_READY && sense.asc == 0x04
  55. && sense.ascq == 0x04)
  56. return CDS_DISC_OK;
  57. /*
  58. * If not using Mt Fuji extended media tray reports,
  59. * just return TRAY_OPEN since ATAPI doesn't provide
  60. * any other way to detect this...
  61. */
  62. if (sense.sense_key == NOT_READY) {
  63. if (sense.asc == 0x3a && sense.ascq == 1)
  64. return CDS_NO_DISC;
  65. else
  66. return CDS_TRAY_OPEN;
  67. }
  68. return CDS_DRIVE_NOT_READY;
  69. }
  70. /*
  71. * ide-cd always generates media changed event if media is missing, which
  72. * makes it impossible to use for proper event reporting, so disk->events
  73. * is cleared to 0 and the following function is used only to trigger
  74. * revalidation and never propagated to userland.
  75. */
  76. unsigned int ide_cdrom_check_events_real(struct cdrom_device_info *cdi,
  77. unsigned int clearing, int slot_nr)
  78. {
  79. ide_drive_t *drive = cdi->handle;
  80. int retval;
  81. if (slot_nr == CDSL_CURRENT) {
  82. (void) cdrom_check_status(drive, NULL);
  83. retval = (drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED) ? 1 : 0;
  84. drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
  85. return retval ? DISK_EVENT_MEDIA_CHANGE : 0;
  86. } else {
  87. return 0;
  88. }
  89. }
  90. /* Eject the disk if EJECTFLAG is 0.
  91. If EJECTFLAG is 1, try to reload the disk. */
  92. static
  93. int cdrom_eject(ide_drive_t *drive, int ejectflag,
  94. struct request_sense *sense)
  95. {
  96. struct cdrom_info *cd = drive->driver_data;
  97. struct cdrom_device_info *cdi = &cd->devinfo;
  98. char loej = 0x02;
  99. unsigned char cmd[BLK_MAX_CDB];
  100. if ((drive->atapi_flags & IDE_AFLAG_NO_EJECT) && !ejectflag)
  101. return -EDRIVE_CANT_DO_THIS;
  102. /* reload fails on some drives, if the tray is locked */
  103. if ((drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED) && ejectflag)
  104. return 0;
  105. /* only tell drive to close tray if open, if it can do that */
  106. if (ejectflag && (cdi->mask & CDC_CLOSE_TRAY))
  107. loej = 0;
  108. memset(cmd, 0, BLK_MAX_CDB);
  109. cmd[0] = GPCMD_START_STOP_UNIT;
  110. cmd[4] = loej | (ejectflag != 0);
  111. return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, 0);
  112. }
  113. /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
  114. static
  115. int ide_cd_lockdoor(ide_drive_t *drive, int lockflag,
  116. struct request_sense *sense)
  117. {
  118. struct request_sense my_sense;
  119. int stat;
  120. if (sense == NULL)
  121. sense = &my_sense;
  122. /* If the drive cannot lock the door, just pretend. */
  123. if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0) {
  124. stat = 0;
  125. } else {
  126. unsigned char cmd[BLK_MAX_CDB];
  127. memset(cmd, 0, BLK_MAX_CDB);
  128. cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  129. cmd[4] = lockflag ? 1 : 0;
  130. stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL,
  131. sense, 0, 0);
  132. }
  133. /* If we got an illegal field error, the drive
  134. probably cannot lock the door. */
  135. if (stat != 0 &&
  136. sense->sense_key == ILLEGAL_REQUEST &&
  137. (sense->asc == 0x24 || sense->asc == 0x20)) {
  138. printk(KERN_ERR "%s: door locking not supported\n",
  139. drive->name);
  140. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  141. stat = 0;
  142. }
  143. /* no medium, that's alright. */
  144. if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
  145. stat = 0;
  146. if (stat == 0) {
  147. if (lockflag)
  148. drive->atapi_flags |= IDE_AFLAG_DOOR_LOCKED;
  149. else
  150. drive->atapi_flags &= ~IDE_AFLAG_DOOR_LOCKED;
  151. }
  152. return stat;
  153. }
  154. int ide_cdrom_tray_move(struct cdrom_device_info *cdi, int position)
  155. {
  156. ide_drive_t *drive = cdi->handle;
  157. struct request_sense sense;
  158. if (position) {
  159. int stat = ide_cd_lockdoor(drive, 0, &sense);
  160. if (stat)
  161. return stat;
  162. }
  163. return cdrom_eject(drive, !position, &sense);
  164. }
  165. int ide_cdrom_lock_door(struct cdrom_device_info *cdi, int lock)
  166. {
  167. ide_drive_t *drive = cdi->handle;
  168. return ide_cd_lockdoor(drive, lock, NULL);
  169. }
  170. /*
  171. * ATAPI devices are free to select the speed you request or any slower
  172. * rate. :-( Requesting too fast a speed will _not_ produce an error.
  173. */
  174. int ide_cdrom_select_speed(struct cdrom_device_info *cdi, int speed)
  175. {
  176. ide_drive_t *drive = cdi->handle;
  177. struct cdrom_info *cd = drive->driver_data;
  178. struct request_sense sense;
  179. u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
  180. int stat;
  181. unsigned char cmd[BLK_MAX_CDB];
  182. if (speed == 0)
  183. speed = 0xffff; /* set to max */
  184. else
  185. speed *= 177; /* Nx to kbytes/s */
  186. memset(cmd, 0, BLK_MAX_CDB);
  187. cmd[0] = GPCMD_SET_SPEED;
  188. /* Read Drive speed in kbytes/second MSB/LSB */
  189. cmd[2] = (speed >> 8) & 0xff;
  190. cmd[3] = speed & 0xff;
  191. if ((cdi->mask & (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) !=
  192. (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) {
  193. /* Write Drive speed in kbytes/second MSB/LSB */
  194. cmd[4] = (speed >> 8) & 0xff;
  195. cmd[5] = speed & 0xff;
  196. }
  197. stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, &sense, 0, 0);
  198. if (!ide_cdrom_get_capabilities(drive, buf)) {
  199. ide_cdrom_update_speed(drive, buf);
  200. cdi->speed = cd->current_speed;
  201. }
  202. return 0;
  203. }
  204. int ide_cdrom_get_last_session(struct cdrom_device_info *cdi,
  205. struct cdrom_multisession *ms_info)
  206. {
  207. struct atapi_toc *toc;
  208. ide_drive_t *drive = cdi->handle;
  209. struct cdrom_info *info = drive->driver_data;
  210. struct request_sense sense;
  211. int ret;
  212. if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0 || !info->toc) {
  213. ret = ide_cd_read_toc(drive, &sense);
  214. if (ret)
  215. return ret;
  216. }
  217. toc = info->toc;
  218. ms_info->addr.lba = toc->last_session_lba;
  219. ms_info->xa_flag = toc->xa_flag;
  220. return 0;
  221. }
  222. int ide_cdrom_get_mcn(struct cdrom_device_info *cdi,
  223. struct cdrom_mcn *mcn_info)
  224. {
  225. ide_drive_t *drive = cdi->handle;
  226. int stat, mcnlen;
  227. char buf[24];
  228. unsigned char cmd[BLK_MAX_CDB];
  229. unsigned len = sizeof(buf);
  230. memset(cmd, 0, BLK_MAX_CDB);
  231. cmd[0] = GPCMD_READ_SUBCHANNEL;
  232. cmd[1] = 2; /* MSF addressing */
  233. cmd[2] = 0x40; /* request subQ data */
  234. cmd[3] = 2; /* format */
  235. cmd[8] = len;
  236. stat = ide_cd_queue_pc(drive, cmd, 0, buf, &len, NULL, 0, 0);
  237. if (stat)
  238. return stat;
  239. mcnlen = sizeof(mcn_info->medium_catalog_number) - 1;
  240. memcpy(mcn_info->medium_catalog_number, buf + 9, mcnlen);
  241. mcn_info->medium_catalog_number[mcnlen] = '\0';
  242. return 0;
  243. }
  244. int ide_cdrom_reset(struct cdrom_device_info *cdi)
  245. {
  246. ide_drive_t *drive = cdi->handle;
  247. struct cdrom_info *cd = drive->driver_data;
  248. struct request_sense sense;
  249. struct request *rq;
  250. int ret;
  251. rq = blk_get_request(drive->queue, READ, __GFP_RECLAIM);
  252. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  253. rq->cmd_flags = REQ_QUIET;
  254. ret = blk_execute_rq(drive->queue, cd->disk, rq, 0);
  255. blk_put_request(rq);
  256. /*
  257. * A reset will unlock the door. If it was previously locked,
  258. * lock it again.
  259. */
  260. if (drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED)
  261. (void)ide_cd_lockdoor(drive, 1, &sense);
  262. return ret;
  263. }
  264. static int ide_cd_get_toc_entry(ide_drive_t *drive, int track,
  265. struct atapi_toc_entry **ent)
  266. {
  267. struct cdrom_info *info = drive->driver_data;
  268. struct atapi_toc *toc = info->toc;
  269. int ntracks;
  270. /*
  271. * don't serve cached data, if the toc isn't valid
  272. */
  273. if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0)
  274. return -EINVAL;
  275. /* Check validity of requested track number. */
  276. ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
  277. if (toc->hdr.first_track == CDROM_LEADOUT)
  278. ntracks = 0;
  279. if (track == CDROM_LEADOUT)
  280. *ent = &toc->ent[ntracks];
  281. else if (track < toc->hdr.first_track || track > toc->hdr.last_track)
  282. return -EINVAL;
  283. else
  284. *ent = &toc->ent[track - toc->hdr.first_track];
  285. return 0;
  286. }
  287. static int ide_cd_fake_play_trkind(ide_drive_t *drive, void *arg)
  288. {
  289. struct cdrom_ti *ti = arg;
  290. struct atapi_toc_entry *first_toc, *last_toc;
  291. unsigned long lba_start, lba_end;
  292. int stat;
  293. struct request_sense sense;
  294. unsigned char cmd[BLK_MAX_CDB];
  295. stat = ide_cd_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
  296. if (stat)
  297. return stat;
  298. stat = ide_cd_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
  299. if (stat)
  300. return stat;
  301. if (ti->cdti_trk1 != CDROM_LEADOUT)
  302. ++last_toc;
  303. lba_start = first_toc->addr.lba;
  304. lba_end = last_toc->addr.lba;
  305. if (lba_end <= lba_start)
  306. return -EINVAL;
  307. memset(cmd, 0, BLK_MAX_CDB);
  308. cmd[0] = GPCMD_PLAY_AUDIO_MSF;
  309. lba_to_msf(lba_start, &cmd[3], &cmd[4], &cmd[5]);
  310. lba_to_msf(lba_end - 1, &cmd[6], &cmd[7], &cmd[8]);
  311. return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, &sense, 0, 0);
  312. }
  313. static int ide_cd_read_tochdr(ide_drive_t *drive, void *arg)
  314. {
  315. struct cdrom_info *cd = drive->driver_data;
  316. struct cdrom_tochdr *tochdr = arg;
  317. struct atapi_toc *toc;
  318. int stat;
  319. /* Make sure our saved TOC is valid. */
  320. stat = ide_cd_read_toc(drive, NULL);
  321. if (stat)
  322. return stat;
  323. toc = cd->toc;
  324. tochdr->cdth_trk0 = toc->hdr.first_track;
  325. tochdr->cdth_trk1 = toc->hdr.last_track;
  326. return 0;
  327. }
  328. static int ide_cd_read_tocentry(ide_drive_t *drive, void *arg)
  329. {
  330. struct cdrom_tocentry *tocentry = arg;
  331. struct atapi_toc_entry *toce;
  332. int stat;
  333. stat = ide_cd_get_toc_entry(drive, tocentry->cdte_track, &toce);
  334. if (stat)
  335. return stat;
  336. tocentry->cdte_ctrl = toce->control;
  337. tocentry->cdte_adr = toce->adr;
  338. if (tocentry->cdte_format == CDROM_MSF) {
  339. lba_to_msf(toce->addr.lba,
  340. &tocentry->cdte_addr.msf.minute,
  341. &tocentry->cdte_addr.msf.second,
  342. &tocentry->cdte_addr.msf.frame);
  343. } else
  344. tocentry->cdte_addr.lba = toce->addr.lba;
  345. return 0;
  346. }
  347. int ide_cdrom_audio_ioctl(struct cdrom_device_info *cdi,
  348. unsigned int cmd, void *arg)
  349. {
  350. ide_drive_t *drive = cdi->handle;
  351. switch (cmd) {
  352. /*
  353. * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
  354. * atapi doesn't support it
  355. */
  356. case CDROMPLAYTRKIND:
  357. return ide_cd_fake_play_trkind(drive, arg);
  358. case CDROMREADTOCHDR:
  359. return ide_cd_read_tochdr(drive, arg);
  360. case CDROMREADTOCENTRY:
  361. return ide_cd_read_tocentry(drive, arg);
  362. default:
  363. return -EINVAL;
  364. }
  365. }
  366. /* the generic packet interface to cdrom.c */
  367. int ide_cdrom_packet(struct cdrom_device_info *cdi,
  368. struct packet_command *cgc)
  369. {
  370. ide_drive_t *drive = cdi->handle;
  371. unsigned int flags = 0;
  372. unsigned len = cgc->buflen;
  373. if (cgc->timeout <= 0)
  374. cgc->timeout = ATAPI_WAIT_PC;
  375. /* here we queue the commands from the uniform CD-ROM
  376. layer. the packet must be complete, as we do not
  377. touch it at all. */
  378. if (cgc->data_direction == CGC_DATA_WRITE)
  379. flags |= REQ_WRITE;
  380. if (cgc->sense)
  381. memset(cgc->sense, 0, sizeof(struct request_sense));
  382. if (cgc->quiet)
  383. flags |= REQ_QUIET;
  384. cgc->stat = ide_cd_queue_pc(drive, cgc->cmd,
  385. cgc->data_direction == CGC_DATA_WRITE,
  386. cgc->buffer, &len,
  387. cgc->sense, cgc->timeout, flags);
  388. if (!cgc->stat)
  389. cgc->buflen -= len;
  390. return cgc->stat;
  391. }