blob: df3df0041eb61b8b0050703bbeb5585500bbc8fd [file] [log] [blame]
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001/*
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
9#include <linux/kernel.h>
10#include <linux/cdrom.h>
11#include <linux/ide.h>
Borislav Petkovf9afd182008-02-01 23:09:29 +010012#include <scsi/scsi.h>
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +010013
14#include "ide-cd.h"
15
Borislav Petkovf9afd182008-02-01 23:09:29 +010016/****************************************************************************
17 * Other driver requests (open, close, check media change).
18 */
19int ide_cdrom_open_real(struct cdrom_device_info *cdi, int purpose)
20{
21 return 0;
22}
23
24/*
25 * Close down the device. Invalidate all cached blocks.
26 */
27void ide_cdrom_release_real(struct cdrom_device_info *cdi)
28{
29 ide_drive_t *drive = cdi->handle;
Borislav Petkovf9afd182008-02-01 23:09:29 +010030
31 if (!cdi->use_count)
Borislav Petkov570f89e2008-07-23 19:56:02 +020032 drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
Borislav Petkovf9afd182008-02-01 23:09:29 +010033}
34
35/*
36 * add logic to try GET_EVENT command first to check for media and tray
37 * status. this should be supported by newer cd-r/w and all DVD etc
38 * drives
39 */
40int ide_cdrom_drive_status(struct cdrom_device_info *cdi, int slot_nr)
41{
42 ide_drive_t *drive = cdi->handle;
43 struct media_event_desc med;
44 struct request_sense sense;
45 int stat;
46
47 if (slot_nr != CDSL_CURRENT)
48 return -EINVAL;
49
50 stat = cdrom_check_status(drive, &sense);
51 if (!stat || sense.sense_key == UNIT_ATTENTION)
52 return CDS_DISC_OK;
53
54 if (!cdrom_get_media_event(cdi, &med)) {
55 if (med.media_present)
56 return CDS_DISC_OK;
57 else if (med.door_open)
58 return CDS_TRAY_OPEN;
59 else
60 return CDS_NO_DISC;
61 }
62
63 if (sense.sense_key == NOT_READY && sense.asc == 0x04
64 && sense.ascq == 0x04)
65 return CDS_DISC_OK;
66
67 /*
68 * If not using Mt Fuji extended media tray reports,
69 * just return TRAY_OPEN since ATAPI doesn't provide
70 * any other way to detect this...
71 */
72 if (sense.sense_key == NOT_READY) {
73 if (sense.asc == 0x3a && sense.ascq == 1)
74 return CDS_NO_DISC;
75 else
76 return CDS_TRAY_OPEN;
77 }
78 return CDS_DRIVE_NOT_READY;
79}
80
81int ide_cdrom_check_media_change_real(struct cdrom_device_info *cdi,
82 int slot_nr)
83{
84 ide_drive_t *drive = cdi->handle;
Borislav Petkovf9afd182008-02-01 23:09:29 +010085 int retval;
86
87 if (slot_nr == CDSL_CURRENT) {
88 (void) cdrom_check_status(drive, NULL);
Bartlomiej Zolnierkiewiczfe11edf2008-10-17 18:09:11 +020089 retval = (drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED) ? 1 : 0;
90 drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
Borislav Petkovf9afd182008-02-01 23:09:29 +010091 return retval;
92 } else {
93 return -EINVAL;
94 }
95}
96
97/* Eject the disk if EJECTFLAG is 0.
98 If EJECTFLAG is 1, try to reload the disk. */
99static
100int cdrom_eject(ide_drive_t *drive, int ejectflag,
101 struct request_sense *sense)
102{
103 struct cdrom_info *cd = drive->driver_data;
104 struct cdrom_device_info *cdi = &cd->devinfo;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100105 char loej = 0x02;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200106 unsigned char cmd[BLK_MAX_CDB];
Borislav Petkovf9afd182008-02-01 23:09:29 +0100107
Borislav Petkov570f89e2008-07-23 19:56:02 +0200108 if ((drive->atapi_flags & IDE_AFLAG_NO_EJECT) && !ejectflag)
Borislav Petkovf9afd182008-02-01 23:09:29 +0100109 return -EDRIVE_CANT_DO_THIS;
110
111 /* reload fails on some drives, if the tray is locked */
Borislav Petkov570f89e2008-07-23 19:56:02 +0200112 if ((drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED) && ejectflag)
Borislav Petkovf9afd182008-02-01 23:09:29 +0100113 return 0;
114
Borislav Petkovf9afd182008-02-01 23:09:29 +0100115 /* only tell drive to close tray if open, if it can do that */
116 if (ejectflag && (cdi->mask & CDC_CLOSE_TRAY))
117 loej = 0;
118
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200119 memset(cmd, 0, BLK_MAX_CDB);
Borislav Petkovf9afd182008-02-01 23:09:29 +0100120
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200121 cmd[0] = GPCMD_START_STOP_UNIT;
122 cmd[4] = loej | (ejectflag != 0);
123
Harvey Harrison1a677ab2008-07-23 19:56:02 +0200124 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, 0);
Borislav Petkovf9afd182008-02-01 23:09:29 +0100125}
126
127/* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
128static
129int ide_cd_lockdoor(ide_drive_t *drive, int lockflag,
130 struct request_sense *sense)
131{
Borislav Petkovf9afd182008-02-01 23:09:29 +0100132 struct request_sense my_sense;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100133 int stat;
134
135 if (sense == NULL)
136 sense = &my_sense;
137
138 /* If the drive cannot lock the door, just pretend. */
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +0200139 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0) {
Borislav Petkovf9afd182008-02-01 23:09:29 +0100140 stat = 0;
141 } else {
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200142 unsigned char cmd[BLK_MAX_CDB];
143
144 memset(cmd, 0, BLK_MAX_CDB);
145
146 cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
147 cmd[4] = lockflag ? 1 : 0;
148
Harvey Harrison1a677ab2008-07-23 19:56:02 +0200149 stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL,
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200150 sense, 0, 0);
Borislav Petkovf9afd182008-02-01 23:09:29 +0100151 }
152
153 /* If we got an illegal field error, the drive
154 probably cannot lock the door. */
155 if (stat != 0 &&
156 sense->sense_key == ILLEGAL_REQUEST &&
157 (sense->asc == 0x24 || sense->asc == 0x20)) {
158 printk(KERN_ERR "%s: door locking not supported\n",
159 drive->name);
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +0200160 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100161 stat = 0;
162 }
163
164 /* no medium, that's alright. */
165 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
166 stat = 0;
167
168 if (stat == 0) {
169 if (lockflag)
Borislav Petkov570f89e2008-07-23 19:56:02 +0200170 drive->atapi_flags |= IDE_AFLAG_DOOR_LOCKED;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100171 else
Borislav Petkov570f89e2008-07-23 19:56:02 +0200172 drive->atapi_flags &= ~IDE_AFLAG_DOOR_LOCKED;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100173 }
174
175 return stat;
176}
177
178int ide_cdrom_tray_move(struct cdrom_device_info *cdi, int position)
179{
180 ide_drive_t *drive = cdi->handle;
181 struct request_sense sense;
182
183 if (position) {
184 int stat = ide_cd_lockdoor(drive, 0, &sense);
185
186 if (stat)
187 return stat;
188 }
189
190 return cdrom_eject(drive, !position, &sense);
191}
192
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100193int ide_cdrom_lock_door(struct cdrom_device_info *cdi, int lock)
194{
195 ide_drive_t *drive = cdi->handle;
196
197 return ide_cd_lockdoor(drive, lock, NULL);
198}
199
200/*
201 * ATAPI devices are free to select the speed you request or any slower
202 * rate. :-( Requesting too fast a speed will _not_ produce an error.
203 */
204int ide_cdrom_select_speed(struct cdrom_device_info *cdi, int speed)
205{
206 ide_drive_t *drive = cdi->handle;
207 struct cdrom_info *cd = drive->driver_data;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100208 struct request_sense sense;
209 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
210 int stat;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200211 unsigned char cmd[BLK_MAX_CDB];
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100212
213 if (speed == 0)
214 speed = 0xffff; /* set to max */
215 else
216 speed *= 177; /* Nx to kbytes/s */
217
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200218 memset(cmd, 0, BLK_MAX_CDB);
219
220 cmd[0] = GPCMD_SET_SPEED;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100221 /* Read Drive speed in kbytes/second MSB/LSB */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200222 cmd[2] = (speed >> 8) & 0xff;
223 cmd[3] = speed & 0xff;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100224 if ((cdi->mask & (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) !=
225 (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) {
226 /* Write Drive speed in kbytes/second MSB/LSB */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200227 cmd[4] = (speed >> 8) & 0xff;
228 cmd[5] = speed & 0xff;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100229 }
230
Harvey Harrison1a677ab2008-07-23 19:56:02 +0200231 stat = ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, &sense, 0, 0);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100232
233 if (!ide_cdrom_get_capabilities(drive, buf)) {
234 ide_cdrom_update_speed(drive, buf);
235 cdi->speed = cd->current_speed;
236 }
237
238 return 0;
239}
240
241int ide_cdrom_get_last_session(struct cdrom_device_info *cdi,
242 struct cdrom_multisession *ms_info)
243{
244 struct atapi_toc *toc;
245 ide_drive_t *drive = cdi->handle;
246 struct cdrom_info *info = drive->driver_data;
247 struct request_sense sense;
248 int ret;
249
Borislav Petkov570f89e2008-07-23 19:56:02 +0200250 if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0 || !info->toc) {
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100251 ret = ide_cd_read_toc(drive, &sense);
252 if (ret)
253 return ret;
254 }
255
256 toc = info->toc;
257 ms_info->addr.lba = toc->last_session_lba;
258 ms_info->xa_flag = toc->xa_flag;
259
260 return 0;
261}
262
263int ide_cdrom_get_mcn(struct cdrom_device_info *cdi,
264 struct cdrom_mcn *mcn_info)
265{
266 ide_drive_t *drive = cdi->handle;
267 int stat, mcnlen;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100268 char buf[24];
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200269 unsigned char cmd[BLK_MAX_CDB];
270 unsigned len = sizeof(buf);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100271
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200272 memset(cmd, 0, BLK_MAX_CDB);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100273
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200274 cmd[0] = GPCMD_READ_SUBCHANNEL;
275 cmd[1] = 2; /* MSF addressing */
276 cmd[2] = 0x40; /* request subQ data */
277 cmd[3] = 2; /* format */
278 cmd[8] = len;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100279
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200280 stat = ide_cd_queue_pc(drive, cmd, 0, buf, &len, NULL, 0, 0);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100281 if (stat)
282 return stat;
283
284 mcnlen = sizeof(mcn_info->medium_catalog_number) - 1;
285 memcpy(mcn_info->medium_catalog_number, buf + 9, mcnlen);
286 mcn_info->medium_catalog_number[mcnlen] = '\0';
287
288 return 0;
289}
290
291int ide_cdrom_reset(struct cdrom_device_info *cdi)
292{
293 ide_drive_t *drive = cdi->handle;
294 struct cdrom_info *cd = drive->driver_data;
295 struct request_sense sense;
FUJITA Tomonori0ef4c4d2008-07-15 21:21:42 +0200296 struct request *rq;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100297 int ret;
298
FUJITA Tomonori0ef4c4d2008-07-15 21:21:42 +0200299 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
300 rq->cmd_type = REQ_TYPE_SPECIAL;
301 rq->cmd_flags = REQ_QUIET;
302 ret = blk_execute_rq(drive->queue, cd->disk, rq, 0);
303 blk_put_request(rq);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100304 /*
305 * A reset will unlock the door. If it was previously locked,
306 * lock it again.
307 */
Borislav Petkov570f89e2008-07-23 19:56:02 +0200308 if (drive->atapi_flags & IDE_AFLAG_DOOR_LOCKED)
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100309 (void)ide_cd_lockdoor(drive, 1, &sense);
310
311 return ret;
312}
313
314static int ide_cd_get_toc_entry(ide_drive_t *drive, int track,
315 struct atapi_toc_entry **ent)
316{
317 struct cdrom_info *info = drive->driver_data;
318 struct atapi_toc *toc = info->toc;
319 int ntracks;
320
321 /*
322 * don't serve cached data, if the toc isn't valid
323 */
Borislav Petkov570f89e2008-07-23 19:56:02 +0200324 if ((drive->atapi_flags & IDE_AFLAG_TOC_VALID) == 0)
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100325 return -EINVAL;
326
327 /* Check validity of requested track number. */
328 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
329
330 if (toc->hdr.first_track == CDROM_LEADOUT)
331 ntracks = 0;
332
333 if (track == CDROM_LEADOUT)
334 *ent = &toc->ent[ntracks];
335 else if (track < toc->hdr.first_track || track > toc->hdr.last_track)
336 return -EINVAL;
337 else
338 *ent = &toc->ent[track - toc->hdr.first_track];
339
340 return 0;
341}
342
343static int ide_cd_fake_play_trkind(ide_drive_t *drive, void *arg)
344{
345 struct cdrom_ti *ti = arg;
346 struct atapi_toc_entry *first_toc, *last_toc;
347 unsigned long lba_start, lba_end;
348 int stat;
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100349 struct request_sense sense;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200350 unsigned char cmd[BLK_MAX_CDB];
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100351
352 stat = ide_cd_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
353 if (stat)
354 return stat;
355
356 stat = ide_cd_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
357 if (stat)
358 return stat;
359
360 if (ti->cdti_trk1 != CDROM_LEADOUT)
361 ++last_toc;
362 lba_start = first_toc->addr.lba;
363 lba_end = last_toc->addr.lba;
364
365 if (lba_end <= lba_start)
366 return -EINVAL;
367
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200368 memset(cmd, 0, BLK_MAX_CDB);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100369
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200370 cmd[0] = GPCMD_PLAY_AUDIO_MSF;
371 lba_to_msf(lba_start, &cmd[3], &cmd[4], &cmd[5]);
372 lba_to_msf(lba_end - 1, &cmd[6], &cmd[7], &cmd[8]);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100373
Harvey Harrison1a677ab2008-07-23 19:56:02 +0200374 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, &sense, 0, 0);
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100375}
376
377static int ide_cd_read_tochdr(ide_drive_t *drive, void *arg)
378{
379 struct cdrom_info *cd = drive->driver_data;
380 struct cdrom_tochdr *tochdr = arg;
381 struct atapi_toc *toc;
382 int stat;
383
384 /* Make sure our saved TOC is valid. */
385 stat = ide_cd_read_toc(drive, NULL);
386 if (stat)
387 return stat;
388
389 toc = cd->toc;
390 tochdr->cdth_trk0 = toc->hdr.first_track;
391 tochdr->cdth_trk1 = toc->hdr.last_track;
392
393 return 0;
394}
395
396static int ide_cd_read_tocentry(ide_drive_t *drive, void *arg)
397{
398 struct cdrom_tocentry *tocentry = arg;
399 struct atapi_toc_entry *toce;
400 int stat;
401
402 stat = ide_cd_get_toc_entry(drive, tocentry->cdte_track, &toce);
403 if (stat)
404 return stat;
405
406 tocentry->cdte_ctrl = toce->control;
407 tocentry->cdte_adr = toce->adr;
408 if (tocentry->cdte_format == CDROM_MSF) {
409 lba_to_msf(toce->addr.lba,
410 &tocentry->cdte_addr.msf.minute,
411 &tocentry->cdte_addr.msf.second,
412 &tocentry->cdte_addr.msf.frame);
413 } else
414 tocentry->cdte_addr.lba = toce->addr.lba;
415
416 return 0;
417}
418
419int ide_cdrom_audio_ioctl(struct cdrom_device_info *cdi,
420 unsigned int cmd, void *arg)
421{
422 ide_drive_t *drive = cdi->handle;
423
424 switch (cmd) {
425 /*
426 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
427 * atapi doesn't support it
428 */
429 case CDROMPLAYTRKIND:
430 return ide_cd_fake_play_trkind(drive, arg);
431 case CDROMREADTOCHDR:
432 return ide_cd_read_tochdr(drive, arg);
433 case CDROMREADTOCENTRY:
434 return ide_cd_read_tocentry(drive, arg);
435 default:
436 return -EINVAL;
437 }
438}
Borislav Petkovf9afd182008-02-01 23:09:29 +0100439
440/* the generic packet interface to cdrom.c */
441int ide_cdrom_packet(struct cdrom_device_info *cdi,
442 struct packet_command *cgc)
443{
Borislav Petkovf9afd182008-02-01 23:09:29 +0100444 ide_drive_t *drive = cdi->handle;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200445 unsigned int flags = 0;
446 unsigned len = cgc->buflen;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100447
448 if (cgc->timeout <= 0)
449 cgc->timeout = ATAPI_WAIT_PC;
450
451 /* here we queue the commands from the uniform CD-ROM
452 layer. the packet must be complete, as we do not
453 touch it at all. */
Bartlomiej Zolnierkiewiczb3484872008-03-07 21:53:49 +0100454
455 if (cgc->data_direction == CGC_DATA_WRITE)
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200456 flags |= REQ_RW;
Bartlomiej Zolnierkiewiczb3484872008-03-07 21:53:49 +0100457
Borislav Petkovf9afd182008-02-01 23:09:29 +0100458 if (cgc->sense)
459 memset(cgc->sense, 0, sizeof(struct request_sense));
Borislav Petkovf9afd182008-02-01 23:09:29 +0100460
461 if (cgc->quiet)
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200462 flags |= REQ_QUIET;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100463
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200464 cgc->stat = ide_cd_queue_pc(drive, cgc->cmd,
465 cgc->data_direction == CGC_DATA_WRITE,
466 cgc->buffer, &len,
467 cgc->sense, cgc->timeout, flags);
Borislav Petkovf9afd182008-02-01 23:09:29 +0100468 if (!cgc->stat)
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200469 cgc->buflen -= len;
Borislav Petkovf9afd182008-02-01 23:09:29 +0100470 return cgc->stat;
471}