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
  74. * DISK_EVENT_FLAG_UEVENT is cleared in disk->event_flags
  75. * and the following function is used only to trigger
  76. * revalidation and never propagated to userland.
  77. */
  78. unsigned int ide_cdrom_check_events_real(struct cdrom_device_info *cdi,
  79. unsigned int clearing, int slot_nr)
  80. {
  81. ide_drive_t *drive = cdi->handle;
  82. int retval;
  83. if (slot_nr == CDSL_CURRENT) {
  84. (void) cdrom_check_status(drive, NULL);
  85. retval = (drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED) ? 1 : 0;
  86. drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
  87. return retval ? DISK_EVENT_MEDIA_CHANGE : 0;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. /* Eject the disk if EJECTFLAG is 0.
  93. If EJECTFLAG is 1, try to reload the disk. */
  94. static
  95. int cdrom_eject(ide_drive_t *drive, int ejectflag)
  96. {
  97. struct cdrom_info *cd = drive->driver_data;
  98. struct cdrom_device_info *cdi = &cd->devinfo;
  99. char loej = 0x02;
  100. unsigned char cmd[BLK_MAX_CDB];
  101. if ((drive->atapi_flags & IDE_AFLAG_NO_EJECT) && !ejectflag)
  102. return -EDRIVE_CANT_DO_THIS;
  103. /* reload fails on some drives, if the tray is locked */
  104. if ((drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED) && ejectflag)
  105. return 0;
  106. /* only tell drive to close tray if open, if it can do that */
  107. if (ejectflag && (cdi->mask & CDC_CLOSE_TRAY))
  108. loej = 0;
  109. memset(cmd, 0, BLK_MAX_CDB);
  110. cmd[0] = GPCMD_START_STOP_UNIT;
  111. cmd[4] = loej | (ejectflag != 0);
  112. return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, NULL, 0, 0);
  113. }
  114. /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
  115. static
  116. int ide_cd_lockdoor(ide_drive_t *drive, int lockflag)
  117. {
  118. struct scsi_sense_hdr sshdr;
  119. int stat;
  120. /* If the drive cannot lock the door, just pretend. */
  121. if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0) {
  122. stat = 0;
  123. } else {
  124. unsigned char cmd[BLK_MAX_CDB];
  125. memset(cmd, 0, BLK_MAX_CDB);
  126. cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  127. cmd[4] = lockflag ? 1 : 0;
  128. stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL,
  129. &sshdr, 0, 0);
  130. }
  131. /* If we got an illegal field error, the drive
  132. probably cannot lock the door. */
  133. if (stat != 0 &&
  134. sshdr.sense_key == ILLEGAL_REQUEST &&
  135. (sshdr.asc == 0x24 || sshdr.asc == 0x20)) {
  136. printk(KERN_ERR "%s: door locking not supported\n",
  137. drive->name);
  138. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  139. stat = 0;
  140. }
  141. /* no medium, that's alright. */
  142. if (stat != 0 && sshdr.sense_key == NOT_READY && sshdr.asc == 0x3a)
  143. stat = 0;
  144. if (stat == 0) {
  145. if (lockflag)
  146. drive->atapi_flags |= IDE_AFLAG_DOOR_LOCKED;
  147. else
  148. drive->atapi_flags &= ~IDE_AFLAG_DOOR_LOCKED;
  149. }
  150. return stat;
  151. }
  152. int ide_cdrom_tray_move(struct cdrom_device_info *cdi, int position)
  153. {
  154. ide_drive_t *drive = cdi->handle;
  155. if (position) {
  156. int stat = ide_cd_lockdoor(drive, 0);
  157. if (stat)
  158. return stat;
  159. }
  160. return cdrom_eject(drive, !position);
  161. }
  162. int ide_cdrom_lock_door(struct cdrom_device_info *cdi, int lock)
  163. {
  164. ide_drive_t *drive = cdi->handle;
  165. return ide_cd_lockdoor(drive, lock);
  166. }
  167. /*
  168. * ATAPI devices are free to select the speed you request or any slower
  169. * rate. :-( Requesting too fast a speed will _not_ produce an error.
  170. */
  171. int ide_cdrom_select_speed(struct cdrom_device_info *cdi, int speed)
  172. {
  173. ide_drive_t *drive = cdi->handle;
  174. struct cdrom_info *cd = drive->driver_data;
  175. u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
  176. int stat;
  177. unsigned char cmd[BLK_MAX_CDB];
  178. if (speed == 0)
  179. speed = 0xffff; /* set to max */
  180. else
  181. speed *= 177; /* Nx to kbytes/s */
  182. memset(cmd, 0, BLK_MAX_CDB);
  183. cmd[0] = GPCMD_SET_SPEED;
  184. /* Read Drive speed in kbytes/second MSB/LSB */
  185. cmd[2] = (speed >> 8) & 0xff;
  186. cmd[3] = speed & 0xff;
  187. if ((cdi->mask & (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) !=
  188. (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) {
  189. /* Write Drive speed in kbytes/second MSB/LSB */
  190. cmd[4] = (speed >> 8) & 0xff;
  191. cmd[5] = speed & 0xff;
  192. }
  193. stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, NULL, 0, 0);
  194. if (!ide_cdrom_get_capabilities(drive, buf)) {
  195. ide_cdrom_update_speed(drive, buf);
  196. cdi->speed = cd->current_speed;
  197. }
  198. return 0;
  199. }
  200. int ide_cdrom_get_last_session(struct cdrom_device_info *cdi,
  201. struct cdrom_multisession *ms_info)
  202. {
  203. struct atapi_toc *toc;
  204. ide_drive_t *drive = cdi->handle;
  205. struct cdrom_info *info = drive->driver_data;
  206. int ret;
  207. if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0 || !info->toc) {
  208. ret = ide_cd_read_toc(drive);
  209. if (ret)
  210. return ret;
  211. }
  212. toc = info->toc;
  213. ms_info->addr.lba = toc->last_session_lba;
  214. ms_info->xa_flag = toc->xa_flag;
  215. return 0;
  216. }
  217. int ide_cdrom_get_mcn(struct cdrom_device_info *cdi,
  218. struct cdrom_mcn *mcn_info)
  219. {
  220. ide_drive_t *drive = cdi->handle;
  221. int stat, mcnlen;
  222. char buf[24];
  223. unsigned char cmd[BLK_MAX_CDB];
  224. unsigned len = sizeof(buf);
  225. memset(cmd, 0, BLK_MAX_CDB);
  226. cmd[0] = GPCMD_READ_SUBCHANNEL;
  227. cmd[1] = 2; /* MSF addressing */
  228. cmd[2] = 0x40; /* request subQ data */
  229. cmd[3] = 2; /* format */
  230. cmd[8] = len;
  231. stat = ide_cd_queue_pc(drive, cmd, 0, buf, &len, NULL, 0, 0);
  232. if (stat)
  233. return stat;
  234. mcnlen = sizeof(mcn_info->medium_catalog_number) - 1;
  235. memcpy(mcn_info->medium_catalog_number, buf + 9, mcnlen);
  236. mcn_info->medium_catalog_number[mcnlen] = '\0';
  237. return 0;
  238. }
  239. int ide_cdrom_reset(struct cdrom_device_info *cdi)
  240. {
  241. ide_drive_t *drive = cdi->handle;
  242. struct cdrom_info *cd = drive->driver_data;
  243. struct request *rq;
  244. int ret;
  245. rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
  246. ide_req(rq)->type = ATA_PRIV_MISC;
  247. rq->rq_flags = RQF_QUIET;
  248. blk_execute_rq(drive->queue, cd->disk, rq, 0);
  249. ret = scsi_req(rq)->result ? -EIO : 0;
  250. blk_put_request(rq);
  251. /*
  252. * A reset will unlock the door. If it was previously locked,
  253. * lock it again.
  254. */
  255. if (drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED)
  256. (void)ide_cd_lockdoor(drive, 1);
  257. return ret;
  258. }
  259. static int ide_cd_get_toc_entry(ide_drive_t *drive, int track,
  260. struct atapi_toc_entry **ent)
  261. {
  262. struct cdrom_info *info = drive->driver_data;
  263. struct atapi_toc *toc = info->toc;
  264. int ntracks;
  265. /*
  266. * don't serve cached data, if the toc isn't valid
  267. */
  268. if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0)
  269. return -EINVAL;
  270. /* Check validity of requested track number. */
  271. ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
  272. if (toc->hdr.first_track == CDROM_LEADOUT)
  273. ntracks = 0;
  274. if (track == CDROM_LEADOUT)
  275. *ent = &toc->ent[ntracks];
  276. else if (track < toc->hdr.first_track || track > toc->hdr.last_track)
  277. return -EINVAL;
  278. else
  279. *ent = &toc->ent[track - toc->hdr.first_track];
  280. return 0;
  281. }
  282. static int ide_cd_fake_play_trkind(ide_drive_t *drive, void *arg)
  283. {
  284. struct cdrom_ti *ti = arg;
  285. struct atapi_toc_entry *first_toc, *last_toc;
  286. unsigned long lba_start, lba_end;
  287. int stat;
  288. unsigned char cmd[BLK_MAX_CDB];
  289. stat = ide_cd_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
  290. if (stat)
  291. return stat;
  292. stat = ide_cd_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
  293. if (stat)
  294. return stat;
  295. if (ti->cdti_trk1 != CDROM_LEADOUT)
  296. ++last_toc;
  297. lba_start = first_toc->addr.lba;
  298. lba_end = last_toc->addr.lba;
  299. if (lba_end <= lba_start)
  300. return -EINVAL;
  301. memset(cmd, 0, BLK_MAX_CDB);
  302. cmd[0] = GPCMD_PLAY_AUDIO_MSF;
  303. lba_to_msf(lba_start, &cmd[3], &cmd[4], &cmd[5]);
  304. lba_to_msf(lba_end - 1, &cmd[6], &cmd[7], &cmd[8]);
  305. return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, NULL, 0, 0);
  306. }
  307. static int ide_cd_read_tochdr(ide_drive_t *drive, void *arg)
  308. {
  309. struct cdrom_info *cd = drive->driver_data;
  310. struct cdrom_tochdr *tochdr = arg;
  311. struct atapi_toc *toc;
  312. int stat;
  313. /* Make sure our saved TOC is valid. */
  314. stat = ide_cd_read_toc(drive);
  315. if (stat)
  316. return stat;
  317. toc = cd->toc;
  318. tochdr->cdth_trk0 = toc->hdr.first_track;
  319. tochdr->cdth_trk1 = toc->hdr.last_track;
  320. return 0;
  321. }
  322. static int ide_cd_read_tocentry(ide_drive_t *drive, void *arg)
  323. {
  324. struct cdrom_tocentry *tocentry = arg;
  325. struct atapi_toc_entry *toce;
  326. int stat;
  327. stat = ide_cd_get_toc_entry(drive, tocentry->cdte_track, &toce);
  328. if (stat)
  329. return stat;
  330. tocentry->cdte_ctrl = toce->control;
  331. tocentry->cdte_adr = toce->adr;
  332. if (tocentry->cdte_format == CDROM_MSF) {
  333. lba_to_msf(toce->addr.lba,
  334. &tocentry->cdte_addr.msf.minute,
  335. &tocentry->cdte_addr.msf.second,
  336. &tocentry->cdte_addr.msf.frame);
  337. } else
  338. tocentry->cdte_addr.lba = toce->addr.lba;
  339. return 0;
  340. }
  341. int ide_cdrom_audio_ioctl(struct cdrom_device_info *cdi,
  342. unsigned int cmd, void *arg)
  343. {
  344. ide_drive_t *drive = cdi->handle;
  345. switch (cmd) {
  346. /*
  347. * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
  348. * atapi doesn't support it
  349. */
  350. case CDROMPLAYTRKIND:
  351. return ide_cd_fake_play_trkind(drive, arg);
  352. case CDROMREADTOCHDR:
  353. return ide_cd_read_tochdr(drive, arg);
  354. case CDROMREADTOCENTRY:
  355. return ide_cd_read_tocentry(drive, arg);
  356. default:
  357. return -EINVAL;
  358. }
  359. }
  360. /* the generic packet interface to cdrom.c */
  361. int ide_cdrom_packet(struct cdrom_device_info *cdi,
  362. struct packet_command *cgc)
  363. {
  364. ide_drive_t *drive = cdi->handle;
  365. req_flags_t flags = 0;
  366. unsigned len = cgc->buflen;
  367. if (cgc->timeout <= 0)
  368. cgc->timeout = ATAPI_WAIT_PC;
  369. /* here we queue the commands from the uniform CD-ROM
  370. layer. the packet must be complete, as we do not
  371. touch it at all. */
  372. if (cgc->sshdr)
  373. memset(cgc->sshdr, 0, sizeof(*cgc->sshdr));
  374. if (cgc->quiet)
  375. flags |= RQF_QUIET;
  376. cgc->stat = ide_cd_queue_pc(drive, cgc->cmd,
  377. cgc->data_direction == CGC_DATA_WRITE,
  378. cgc->buffer, &len,
  379. cgc->sshdr, cgc->timeout, flags);
  380. if (!cgc->stat)
  381. cgc->buflen -= len;
  382. return cgc->stat;
  383. }