ide-cd_ioctl.c 11 KB

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