1 // SPDX-License-Identifier: GPL-2.0
3 * cdrom.c IOCTLs handling for ide-cd driver.
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>
10 #include <linux/kernel.h>
11 #include <linux/cdrom.h>
12 #include <linux/gfp.h>
13 #include <linux/ide.h>
14 #include <scsi/scsi.h>
18 /****************************************************************************
19 * Other driver requests (open, close, check media change).
21 int ide_cdrom_open_real(struct cdrom_device_info
*cdi
, int purpose
)
27 * Close down the device. Invalidate all cached blocks.
29 void ide_cdrom_release_real(struct cdrom_device_info
*cdi
)
31 ide_drive_t
*drive
= cdi
->handle
;
34 drive
->atapi_flags
&= ~IDE_AFLAG_TOC_VALID
;
38 * add logic to try GET_EVENT command first to check for media and tray
39 * status. this should be supported by newer cd-r/w and all DVD etc
42 int ide_cdrom_drive_status(struct cdrom_device_info
*cdi
, int slot_nr
)
44 ide_drive_t
*drive
= cdi
->handle
;
45 struct media_event_desc med
;
46 struct request_sense sense
;
49 if (slot_nr
!= CDSL_CURRENT
)
52 stat
= cdrom_check_status(drive
, &sense
);
53 if (!stat
|| sense
.sense_key
== UNIT_ATTENTION
)
56 if (!cdrom_get_media_event(cdi
, &med
)) {
57 if (med
.media_present
)
59 else if (med
.door_open
)
65 if (sense
.sense_key
== NOT_READY
&& sense
.asc
== 0x04
66 && sense
.ascq
== 0x04)
70 * If not using Mt Fuji extended media tray reports,
71 * just return TRAY_OPEN since ATAPI doesn't provide
72 * any other way to detect this...
74 if (sense
.sense_key
== NOT_READY
) {
75 if (sense
.asc
== 0x3a && sense
.ascq
== 1)
80 return CDS_DRIVE_NOT_READY
;
84 * ide-cd always generates media changed event if media is missing, which
85 * makes it impossible to use for proper event reporting, so disk->events
86 * is cleared to 0 and the following function is used only to trigger
87 * revalidation and never propagated to userland.
89 unsigned int ide_cdrom_check_events_real(struct cdrom_device_info
*cdi
,
90 unsigned int clearing
, int slot_nr
)
92 ide_drive_t
*drive
= cdi
->handle
;
95 if (slot_nr
== CDSL_CURRENT
) {
96 (void) cdrom_check_status(drive
, NULL
);
97 retval
= (drive
->dev_flags
& IDE_DFLAG_MEDIA_CHANGED
) ? 1 : 0;
98 drive
->dev_flags
&= ~IDE_DFLAG_MEDIA_CHANGED
;
99 return retval
? DISK_EVENT_MEDIA_CHANGE
: 0;
105 /* Eject the disk if EJECTFLAG is 0.
106 If EJECTFLAG is 1, try to reload the disk. */
108 int cdrom_eject(ide_drive_t
*drive
, int ejectflag
,
109 struct request_sense
*sense
)
111 struct cdrom_info
*cd
= drive
->driver_data
;
112 struct cdrom_device_info
*cdi
= &cd
->devinfo
;
114 unsigned char cmd
[BLK_MAX_CDB
];
116 if ((drive
->atapi_flags
& IDE_AFLAG_NO_EJECT
) && !ejectflag
)
117 return -EDRIVE_CANT_DO_THIS
;
119 /* reload fails on some drives, if the tray is locked */
120 if ((drive
->atapi_flags
& IDE_AFLAG_DOOR_LOCKED
) && ejectflag
)
123 /* only tell drive to close tray if open, if it can do that */
124 if (ejectflag
&& (cdi
->mask
& CDC_CLOSE_TRAY
))
127 memset(cmd
, 0, BLK_MAX_CDB
);
129 cmd
[0] = GPCMD_START_STOP_UNIT
;
130 cmd
[4] = loej
| (ejectflag
!= 0);
132 return ide_cd_queue_pc(drive
, cmd
, 0, NULL
, NULL
, sense
, 0, 0);
135 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
137 int ide_cd_lockdoor(ide_drive_t
*drive
, int lockflag
,
138 struct request_sense
*sense
)
140 struct request_sense my_sense
;
146 /* If the drive cannot lock the door, just pretend. */
147 if ((drive
->dev_flags
& IDE_DFLAG_DOORLOCKING
) == 0) {
150 unsigned char cmd
[BLK_MAX_CDB
];
152 memset(cmd
, 0, BLK_MAX_CDB
);
154 cmd
[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL
;
155 cmd
[4] = lockflag
? 1 : 0;
157 stat
= ide_cd_queue_pc(drive
, cmd
, 0, NULL
, NULL
,
161 /* If we got an illegal field error, the drive
162 probably cannot lock the door. */
164 sense
->sense_key
== ILLEGAL_REQUEST
&&
165 (sense
->asc
== 0x24 || sense
->asc
== 0x20)) {
166 printk(KERN_ERR
"%s: door locking not supported\n",
168 drive
->dev_flags
&= ~IDE_DFLAG_DOORLOCKING
;
172 /* no medium, that's alright. */
173 if (stat
!= 0 && sense
->sense_key
== NOT_READY
&& sense
->asc
== 0x3a)
178 drive
->atapi_flags
|= IDE_AFLAG_DOOR_LOCKED
;
180 drive
->atapi_flags
&= ~IDE_AFLAG_DOOR_LOCKED
;
186 int ide_cdrom_tray_move(struct cdrom_device_info
*cdi
, int position
)
188 ide_drive_t
*drive
= cdi
->handle
;
189 struct request_sense sense
;
192 int stat
= ide_cd_lockdoor(drive
, 0, &sense
);
198 return cdrom_eject(drive
, !position
, &sense
);
201 int ide_cdrom_lock_door(struct cdrom_device_info
*cdi
, int lock
)
203 ide_drive_t
*drive
= cdi
->handle
;
205 return ide_cd_lockdoor(drive
, lock
, NULL
);
209 * ATAPI devices are free to select the speed you request or any slower
210 * rate. :-( Requesting too fast a speed will _not_ produce an error.
212 int ide_cdrom_select_speed(struct cdrom_device_info
*cdi
, int speed
)
214 ide_drive_t
*drive
= cdi
->handle
;
215 struct cdrom_info
*cd
= drive
->driver_data
;
216 struct request_sense sense
;
217 u8 buf
[ATAPI_CAPABILITIES_PAGE_SIZE
];
219 unsigned char cmd
[BLK_MAX_CDB
];
222 speed
= 0xffff; /* set to max */
224 speed
*= 177; /* Nx to kbytes/s */
226 memset(cmd
, 0, BLK_MAX_CDB
);
228 cmd
[0] = GPCMD_SET_SPEED
;
229 /* Read Drive speed in kbytes/second MSB/LSB */
230 cmd
[2] = (speed
>> 8) & 0xff;
231 cmd
[3] = speed
& 0xff;
232 if ((cdi
->mask
& (CDC_CD_R
| CDC_CD_RW
| CDC_DVD_R
)) !=
233 (CDC_CD_R
| CDC_CD_RW
| CDC_DVD_R
)) {
234 /* Write Drive speed in kbytes/second MSB/LSB */
235 cmd
[4] = (speed
>> 8) & 0xff;
236 cmd
[5] = speed
& 0xff;
239 stat
= ide_cd_queue_pc(drive
, cmd
, 0, NULL
, NULL
, &sense
, 0, 0);
241 if (!ide_cdrom_get_capabilities(drive
, buf
)) {
242 ide_cdrom_update_speed(drive
, buf
);
243 cdi
->speed
= cd
->current_speed
;
249 int ide_cdrom_get_last_session(struct cdrom_device_info
*cdi
,
250 struct cdrom_multisession
*ms_info
)
252 struct atapi_toc
*toc
;
253 ide_drive_t
*drive
= cdi
->handle
;
254 struct cdrom_info
*info
= drive
->driver_data
;
255 struct request_sense sense
;
258 if ((drive
->atapi_flags
& IDE_AFLAG_TOC_VALID
) == 0 || !info
->toc
) {
259 ret
= ide_cd_read_toc(drive
, &sense
);
265 ms_info
->addr
.lba
= toc
->last_session_lba
;
266 ms_info
->xa_flag
= toc
->xa_flag
;
271 int ide_cdrom_get_mcn(struct cdrom_device_info
*cdi
,
272 struct cdrom_mcn
*mcn_info
)
274 ide_drive_t
*drive
= cdi
->handle
;
277 unsigned char cmd
[BLK_MAX_CDB
];
278 unsigned len
= sizeof(buf
);
280 memset(cmd
, 0, BLK_MAX_CDB
);
282 cmd
[0] = GPCMD_READ_SUBCHANNEL
;
283 cmd
[1] = 2; /* MSF addressing */
284 cmd
[2] = 0x40; /* request subQ data */
285 cmd
[3] = 2; /* format */
288 stat
= ide_cd_queue_pc(drive
, cmd
, 0, buf
, &len
, NULL
, 0, 0);
292 mcnlen
= sizeof(mcn_info
->medium_catalog_number
) - 1;
293 memcpy(mcn_info
->medium_catalog_number
, buf
+ 9, mcnlen
);
294 mcn_info
->medium_catalog_number
[mcnlen
] = '\0';
299 int ide_cdrom_reset(struct cdrom_device_info
*cdi
)
301 ide_drive_t
*drive
= cdi
->handle
;
302 struct cdrom_info
*cd
= drive
->driver_data
;
303 struct request_sense sense
;
307 rq
= blk_get_request(drive
->queue
, REQ_OP_DRV_IN
, 0);
308 ide_req(rq
)->type
= ATA_PRIV_MISC
;
309 rq
->rq_flags
= RQF_QUIET
;
310 blk_execute_rq(drive
->queue
, cd
->disk
, rq
, 0);
311 ret
= scsi_req(rq
)->result
? -EIO
: 0;
314 * A reset will unlock the door. If it was previously locked,
317 if (drive
->atapi_flags
& IDE_AFLAG_DOOR_LOCKED
)
318 (void)ide_cd_lockdoor(drive
, 1, &sense
);
323 static int ide_cd_get_toc_entry(ide_drive_t
*drive
, int track
,
324 struct atapi_toc_entry
**ent
)
326 struct cdrom_info
*info
= drive
->driver_data
;
327 struct atapi_toc
*toc
= info
->toc
;
331 * don't serve cached data, if the toc isn't valid
333 if ((drive
->atapi_flags
& IDE_AFLAG_TOC_VALID
) == 0)
336 /* Check validity of requested track number. */
337 ntracks
= toc
->hdr
.last_track
- toc
->hdr
.first_track
+ 1;
339 if (toc
->hdr
.first_track
== CDROM_LEADOUT
)
342 if (track
== CDROM_LEADOUT
)
343 *ent
= &toc
->ent
[ntracks
];
344 else if (track
< toc
->hdr
.first_track
|| track
> toc
->hdr
.last_track
)
347 *ent
= &toc
->ent
[track
- toc
->hdr
.first_track
];
352 static int ide_cd_fake_play_trkind(ide_drive_t
*drive
, void *arg
)
354 struct cdrom_ti
*ti
= arg
;
355 struct atapi_toc_entry
*first_toc
, *last_toc
;
356 unsigned long lba_start
, lba_end
;
358 struct request_sense sense
;
359 unsigned char cmd
[BLK_MAX_CDB
];
361 stat
= ide_cd_get_toc_entry(drive
, ti
->cdti_trk0
, &first_toc
);
365 stat
= ide_cd_get_toc_entry(drive
, ti
->cdti_trk1
, &last_toc
);
369 if (ti
->cdti_trk1
!= CDROM_LEADOUT
)
371 lba_start
= first_toc
->addr
.lba
;
372 lba_end
= last_toc
->addr
.lba
;
374 if (lba_end
<= lba_start
)
377 memset(cmd
, 0, BLK_MAX_CDB
);
379 cmd
[0] = GPCMD_PLAY_AUDIO_MSF
;
380 lba_to_msf(lba_start
, &cmd
[3], &cmd
[4], &cmd
[5]);
381 lba_to_msf(lba_end
- 1, &cmd
[6], &cmd
[7], &cmd
[8]);
383 return ide_cd_queue_pc(drive
, cmd
, 0, NULL
, NULL
, &sense
, 0, 0);
386 static int ide_cd_read_tochdr(ide_drive_t
*drive
, void *arg
)
388 struct cdrom_info
*cd
= drive
->driver_data
;
389 struct cdrom_tochdr
*tochdr
= arg
;
390 struct atapi_toc
*toc
;
393 /* Make sure our saved TOC is valid. */
394 stat
= ide_cd_read_toc(drive
, NULL
);
399 tochdr
->cdth_trk0
= toc
->hdr
.first_track
;
400 tochdr
->cdth_trk1
= toc
->hdr
.last_track
;
405 static int ide_cd_read_tocentry(ide_drive_t
*drive
, void *arg
)
407 struct cdrom_tocentry
*tocentry
= arg
;
408 struct atapi_toc_entry
*toce
;
411 stat
= ide_cd_get_toc_entry(drive
, tocentry
->cdte_track
, &toce
);
415 tocentry
->cdte_ctrl
= toce
->control
;
416 tocentry
->cdte_adr
= toce
->adr
;
417 if (tocentry
->cdte_format
== CDROM_MSF
) {
418 lba_to_msf(toce
->addr
.lba
,
419 &tocentry
->cdte_addr
.msf
.minute
,
420 &tocentry
->cdte_addr
.msf
.second
,
421 &tocentry
->cdte_addr
.msf
.frame
);
423 tocentry
->cdte_addr
.lba
= toce
->addr
.lba
;
428 int ide_cdrom_audio_ioctl(struct cdrom_device_info
*cdi
,
429 unsigned int cmd
, void *arg
)
431 ide_drive_t
*drive
= cdi
->handle
;
435 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
436 * atapi doesn't support it
438 case CDROMPLAYTRKIND
:
439 return ide_cd_fake_play_trkind(drive
, arg
);
440 case CDROMREADTOCHDR
:
441 return ide_cd_read_tochdr(drive
, arg
);
442 case CDROMREADTOCENTRY
:
443 return ide_cd_read_tocentry(drive
, arg
);
449 /* the generic packet interface to cdrom.c */
450 int ide_cdrom_packet(struct cdrom_device_info
*cdi
,
451 struct packet_command
*cgc
)
453 ide_drive_t
*drive
= cdi
->handle
;
454 req_flags_t flags
= 0;
455 unsigned len
= cgc
->buflen
;
457 if (cgc
->timeout
<= 0)
458 cgc
->timeout
= ATAPI_WAIT_PC
;
460 /* here we queue the commands from the uniform CD-ROM
461 layer. the packet must be complete, as we do not
465 memset(cgc
->sense
, 0, sizeof(struct request_sense
));
470 cgc
->stat
= ide_cd_queue_pc(drive
, cgc
->cmd
,
471 cgc
->data_direction
== CGC_DATA_WRITE
,
473 cgc
->sense
, cgc
->timeout
, flags
);