2 * cdrom.c IOCTLs handling for ide-cd driver.
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>
9 #include <linux/kernel.h>
10 #include <linux/cdrom.h>
11 #include <linux/ide.h>
12 #include <scsi/scsi.h>
16 /****************************************************************************
17 * Other driver requests (open, close, check media change).
19 int ide_cdrom_open_real(struct cdrom_device_info *cdi, int purpose)
25 * Close down the device. Invalidate all cached blocks.
27 void ide_cdrom_release_real(struct cdrom_device_info *cdi)
29 ide_drive_t *drive = cdi->handle;
30 struct cdrom_info *cd = drive->driver_data;
33 cd->cd_flags &= ~IDE_CD_FLAG_TOC_VALID;
37 * add logic to try GET_EVENT command first to check for media and tray
38 * status. this should be supported by newer cd-r/w and all DVD etc
41 int ide_cdrom_drive_status(struct cdrom_device_info *cdi, int slot_nr)
43 ide_drive_t *drive = cdi->handle;
44 struct media_event_desc med;
45 struct request_sense sense;
48 if (slot_nr != CDSL_CURRENT)
51 stat = cdrom_check_status(drive, &sense);
52 if (!stat || sense.sense_key == UNIT_ATTENTION)
55 if (!cdrom_get_media_event(cdi, &med)) {
56 if (med.media_present)
58 else if (med.door_open)
64 if (sense.sense_key == NOT_READY && sense.asc == 0x04
65 && sense.ascq == 0x04)
69 * If not using Mt Fuji extended media tray reports,
70 * just return TRAY_OPEN since ATAPI doesn't provide
71 * any other way to detect this...
73 if (sense.sense_key == NOT_READY) {
74 if (sense.asc == 0x3a && sense.ascq == 1)
79 return CDS_DRIVE_NOT_READY;
82 int ide_cdrom_check_media_change_real(struct cdrom_device_info *cdi,
85 ide_drive_t *drive = cdi->handle;
86 struct cdrom_info *cd = drive->driver_data;
89 if (slot_nr == CDSL_CURRENT) {
90 (void) cdrom_check_status(drive, NULL);
91 retval = (cd->cd_flags & IDE_CD_FLAG_MEDIA_CHANGED) ? 1 : 0;
92 cd->cd_flags &= ~IDE_CD_FLAG_MEDIA_CHANGED;
99 /* Eject the disk if EJECTFLAG is 0.
100 If EJECTFLAG is 1, try to reload the disk. */
102 int cdrom_eject(ide_drive_t *drive, int ejectflag,
103 struct request_sense *sense)
105 struct cdrom_info *cd = drive->driver_data;
106 struct cdrom_device_info *cdi = &cd->devinfo;
108 unsigned char cmd[BLK_MAX_CDB];
110 if ((cd->cd_flags & IDE_CD_FLAG_NO_EJECT) && !ejectflag)
111 return -EDRIVE_CANT_DO_THIS;
113 /* reload fails on some drives, if the tray is locked */
114 if ((cd->cd_flags & IDE_CD_FLAG_DOOR_LOCKED) && ejectflag)
117 /* only tell drive to close tray if open, if it can do that */
118 if (ejectflag && (cdi->mask & CDC_CLOSE_TRAY))
121 memset(cmd, 0, BLK_MAX_CDB);
123 cmd[0] = GPCMD_START_STOP_UNIT;
124 cmd[4] = loej | (ejectflag != 0);
126 return ide_cd_queue_pc(drive, cmd, 0, NULL, 0, sense, 0, 0);
129 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
131 int ide_cd_lockdoor(ide_drive_t *drive, int lockflag,
132 struct request_sense *sense)
134 struct cdrom_info *cd = drive->driver_data;
135 struct request_sense my_sense;
141 /* If the drive cannot lock the door, just pretend. */
142 if (cd->cd_flags & IDE_CD_FLAG_NO_DOORLOCK) {
145 unsigned char cmd[BLK_MAX_CDB];
147 memset(cmd, 0, BLK_MAX_CDB);
149 cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
150 cmd[4] = lockflag ? 1 : 0;
152 stat = ide_cd_queue_pc(drive, cmd, 0, NULL, 0,
156 /* If we got an illegal field error, the drive
157 probably cannot lock the door. */
159 sense->sense_key == ILLEGAL_REQUEST &&
160 (sense->asc == 0x24 || sense->asc == 0x20)) {
161 printk(KERN_ERR "%s: door locking not supported\n",
163 cd->cd_flags |= IDE_CD_FLAG_NO_DOORLOCK;
167 /* no medium, that's alright. */
168 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
173 cd->cd_flags |= IDE_CD_FLAG_DOOR_LOCKED;
175 cd->cd_flags &= ~IDE_CD_FLAG_DOOR_LOCKED;
181 int ide_cdrom_tray_move(struct cdrom_device_info *cdi, int position)
183 ide_drive_t *drive = cdi->handle;
184 struct request_sense sense;
187 int stat = ide_cd_lockdoor(drive, 0, &sense);
193 return cdrom_eject(drive, !position, &sense);
196 int ide_cdrom_lock_door(struct cdrom_device_info *cdi, int lock)
198 ide_drive_t *drive = cdi->handle;
200 return ide_cd_lockdoor(drive, lock, NULL);
204 * ATAPI devices are free to select the speed you request or any slower
205 * rate. :-( Requesting too fast a speed will _not_ produce an error.
207 int ide_cdrom_select_speed(struct cdrom_device_info *cdi, int speed)
209 ide_drive_t *drive = cdi->handle;
210 struct cdrom_info *cd = drive->driver_data;
211 struct request_sense sense;
212 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
214 unsigned char cmd[BLK_MAX_CDB];
217 speed = 0xffff; /* set to max */
219 speed *= 177; /* Nx to kbytes/s */
221 memset(cmd, 0, BLK_MAX_CDB);
223 cmd[0] = GPCMD_SET_SPEED;
224 /* Read Drive speed in kbytes/second MSB/LSB */
225 cmd[2] = (speed >> 8) & 0xff;
226 cmd[3] = speed & 0xff;
227 if ((cdi->mask & (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) !=
228 (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) {
229 /* Write Drive speed in kbytes/second MSB/LSB */
230 cmd[4] = (speed >> 8) & 0xff;
231 cmd[5] = speed & 0xff;
234 stat = ide_cd_queue_pc(drive, cmd, 0, NULL, 0, &sense, 0, 0);
236 if (!ide_cdrom_get_capabilities(drive, buf)) {
237 ide_cdrom_update_speed(drive, buf);
238 cdi->speed = cd->current_speed;
244 int ide_cdrom_get_last_session(struct cdrom_device_info *cdi,
245 struct cdrom_multisession *ms_info)
247 struct atapi_toc *toc;
248 ide_drive_t *drive = cdi->handle;
249 struct cdrom_info *info = drive->driver_data;
250 struct request_sense sense;
253 if ((info->cd_flags & IDE_CD_FLAG_TOC_VALID) == 0 || !info->toc) {
254 ret = ide_cd_read_toc(drive, &sense);
260 ms_info->addr.lba = toc->last_session_lba;
261 ms_info->xa_flag = toc->xa_flag;
266 int ide_cdrom_get_mcn(struct cdrom_device_info *cdi,
267 struct cdrom_mcn *mcn_info)
269 ide_drive_t *drive = cdi->handle;
272 unsigned char cmd[BLK_MAX_CDB];
273 unsigned len = sizeof(buf);
275 memset(cmd, 0, BLK_MAX_CDB);
277 cmd[0] = GPCMD_READ_SUBCHANNEL;
278 cmd[1] = 2; /* MSF addressing */
279 cmd[2] = 0x40; /* request subQ data */
280 cmd[3] = 2; /* format */
283 stat = ide_cd_queue_pc(drive, cmd, 0, buf, &len, NULL, 0, 0);
287 mcnlen = sizeof(mcn_info->medium_catalog_number) - 1;
288 memcpy(mcn_info->medium_catalog_number, buf + 9, mcnlen);
289 mcn_info->medium_catalog_number[mcnlen] = '\0';
294 int ide_cdrom_reset(struct cdrom_device_info *cdi)
296 ide_drive_t *drive = cdi->handle;
297 struct cdrom_info *cd = drive->driver_data;
298 struct request_sense sense;
302 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
303 rq->cmd_type = REQ_TYPE_SPECIAL;
304 rq->cmd_flags = REQ_QUIET;
305 ret = blk_execute_rq(drive->queue, cd->disk, rq, 0);
308 * A reset will unlock the door. If it was previously locked,
311 if (cd->cd_flags & IDE_CD_FLAG_DOOR_LOCKED)
312 (void)ide_cd_lockdoor(drive, 1, &sense);
317 static int ide_cd_get_toc_entry(ide_drive_t *drive, int track,
318 struct atapi_toc_entry **ent)
320 struct cdrom_info *info = drive->driver_data;
321 struct atapi_toc *toc = info->toc;
325 * don't serve cached data, if the toc isn't valid
327 if ((info->cd_flags & IDE_CD_FLAG_TOC_VALID) == 0)
330 /* Check validity of requested track number. */
331 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
333 if (toc->hdr.first_track == CDROM_LEADOUT)
336 if (track == CDROM_LEADOUT)
337 *ent = &toc->ent[ntracks];
338 else if (track < toc->hdr.first_track || track > toc->hdr.last_track)
341 *ent = &toc->ent[track - toc->hdr.first_track];
346 static int ide_cd_fake_play_trkind(ide_drive_t *drive, void *arg)
348 struct cdrom_ti *ti = arg;
349 struct atapi_toc_entry *first_toc, *last_toc;
350 unsigned long lba_start, lba_end;
352 struct request_sense sense;
353 unsigned char cmd[BLK_MAX_CDB];
355 stat = ide_cd_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
359 stat = ide_cd_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
363 if (ti->cdti_trk1 != CDROM_LEADOUT)
365 lba_start = first_toc->addr.lba;
366 lba_end = last_toc->addr.lba;
368 if (lba_end <= lba_start)
371 memset(cmd, 0, BLK_MAX_CDB);
373 cmd[0] = GPCMD_PLAY_AUDIO_MSF;
374 lba_to_msf(lba_start, &cmd[3], &cmd[4], &cmd[5]);
375 lba_to_msf(lba_end - 1, &cmd[6], &cmd[7], &cmd[8]);
377 return ide_cd_queue_pc(drive, cmd, 0, NULL, 0, &sense, 0, 0);
380 static int ide_cd_read_tochdr(ide_drive_t *drive, void *arg)
382 struct cdrom_info *cd = drive->driver_data;
383 struct cdrom_tochdr *tochdr = arg;
384 struct atapi_toc *toc;
387 /* Make sure our saved TOC is valid. */
388 stat = ide_cd_read_toc(drive, NULL);
393 tochdr->cdth_trk0 = toc->hdr.first_track;
394 tochdr->cdth_trk1 = toc->hdr.last_track;
399 static int ide_cd_read_tocentry(ide_drive_t *drive, void *arg)
401 struct cdrom_tocentry *tocentry = arg;
402 struct atapi_toc_entry *toce;
405 stat = ide_cd_get_toc_entry(drive, tocentry->cdte_track, &toce);
409 tocentry->cdte_ctrl = toce->control;
410 tocentry->cdte_adr = toce->adr;
411 if (tocentry->cdte_format == CDROM_MSF) {
412 lba_to_msf(toce->addr.lba,
413 &tocentry->cdte_addr.msf.minute,
414 &tocentry->cdte_addr.msf.second,
415 &tocentry->cdte_addr.msf.frame);
417 tocentry->cdte_addr.lba = toce->addr.lba;
422 int ide_cdrom_audio_ioctl(struct cdrom_device_info *cdi,
423 unsigned int cmd, void *arg)
425 ide_drive_t *drive = cdi->handle;
429 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
430 * atapi doesn't support it
432 case CDROMPLAYTRKIND:
433 return ide_cd_fake_play_trkind(drive, arg);
434 case CDROMREADTOCHDR:
435 return ide_cd_read_tochdr(drive, arg);
436 case CDROMREADTOCENTRY:
437 return ide_cd_read_tocentry(drive, arg);
443 /* the generic packet interface to cdrom.c */
444 int ide_cdrom_packet(struct cdrom_device_info *cdi,
445 struct packet_command *cgc)
447 ide_drive_t *drive = cdi->handle;
448 unsigned int flags = 0;
449 unsigned len = cgc->buflen;
451 if (cgc->timeout <= 0)
452 cgc->timeout = ATAPI_WAIT_PC;
454 /* here we queue the commands from the uniform CD-ROM
455 layer. the packet must be complete, as we do not
458 if (cgc->data_direction == CGC_DATA_WRITE)
462 memset(cgc->sense, 0, sizeof(struct request_sense));
467 cgc->stat = ide_cd_queue_pc(drive, cgc->cmd,
468 cgc->data_direction == CGC_DATA_WRITE,
470 cgc->sense, cgc->timeout, flags);