ide-cd: remove needless zeroing of 'info' fields from ide_cdrom_setup()
[deliverable/linux.git] / drivers / ide / ide-cd.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/ide/ide-cd.c
3 *
4 * Copyright (C) 1994, 1995, 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 * May be copied or modified under the terms of the GNU General Public
9 * License. See linux/COPYING for more information.
10 *
11 * ATAPI CD-ROM driver. To be used with ide.c.
12 * See Documentation/cdrom/ide-cd for usage information.
13 *
14 * Suggestions are welcome. Patches that work are more welcome though. ;-)
15 * For those wishing to work on this driver, please be sure you download
16 * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
17 * (SFF-8020i rev 2.6) standards. These documents can be obtained by
18 * anonymous ftp from:
19 * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
20 * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
21 *
22 * Drives that deviate from these standards will be accommodated as much
23 * as possible via compile time or command-line options. Since I only have
24 * a few drives, you generally need to send me patches...
25 *
26 * ----------------------------------
27 * TO DO LIST:
28 * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
29 * boot
30 *
03553353
BZ
31 * For historical changelog please see:
32 * Documentation/ide/ChangeLog.ide-cd.1994-2004
33 */
34
1da177e4
LT
35#define IDECD_VERSION "4.61"
36
1da177e4
LT
37#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/timer.h>
42#include <linux/slab.h>
43#include <linux/interrupt.h>
44#include <linux/errno.h>
45#include <linux/cdrom.h>
46#include <linux/ide.h>
47#include <linux/completion.h>
cf8b8975 48#include <linux/mutex.h>
1da177e4
LT
49
50#include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
51
52#include <asm/irq.h>
53#include <asm/io.h>
54#include <asm/byteorder.h>
55#include <asm/uaccess.h>
56#include <asm/unaligned.h>
57
58#include "ide-cd.h"
59
cf8b8975 60static DEFINE_MUTEX(idecd_ref_mutex);
1da177e4
LT
61
62#define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref)
63
64#define ide_cd_g(disk) \
65 container_of((disk)->private_data, struct cdrom_info, driver)
66
67static struct cdrom_info *ide_cd_get(struct gendisk *disk)
68{
69 struct cdrom_info *cd = NULL;
70
cf8b8975 71 mutex_lock(&idecd_ref_mutex);
1da177e4
LT
72 cd = ide_cd_g(disk);
73 if (cd)
74 kref_get(&cd->kref);
cf8b8975 75 mutex_unlock(&idecd_ref_mutex);
1da177e4
LT
76 return cd;
77}
78
79static void ide_cd_release(struct kref *);
80
81static void ide_cd_put(struct cdrom_info *cd)
82{
cf8b8975 83 mutex_lock(&idecd_ref_mutex);
1da177e4 84 kref_put(&cd->kref, ide_cd_release);
cf8b8975 85 mutex_unlock(&idecd_ref_mutex);
1da177e4
LT
86}
87
88/****************************************************************************
89 * Generic packet command support and error handling routines.
90 */
91
92/* Mark that we've seen a media change, and invalidate our internal
93 buffers. */
94static void cdrom_saw_media_change (ide_drive_t *drive)
95{
96 struct cdrom_info *info = drive->driver_data;
97
98 CDROM_STATE_FLAGS (drive)->media_changed = 1;
99 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
100 info->nsectors_buffered = 0;
101}
102
103static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
104 struct request_sense *sense)
105{
106 int log = 0;
107
4aff5e23 108 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
1da177e4
LT
109 return 0;
110
111 switch (sense->sense_key) {
112 case NO_SENSE: case RECOVERED_ERROR:
113 break;
114 case NOT_READY:
115 /*
116 * don't care about tray state messages for
117 * e.g. capacity commands or in-progress or
118 * becoming ready
119 */
120 if (sense->asc == 0x3a || sense->asc == 0x04)
121 break;
122 log = 1;
123 break;
124 case ILLEGAL_REQUEST:
125 /*
126 * don't log START_STOP unit with LoEj set, since
127 * we cannot reliably check if drive can auto-close
128 */
129 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
dbe217af
AC
130 break;
131 log = 1;
1da177e4
LT
132 break;
133 case UNIT_ATTENTION:
134 /*
135 * Make good and sure we've seen this potential media
136 * change. Some drives (i.e. Creative) fail to present
137 * the correct sense key in the error register.
138 */
139 cdrom_saw_media_change(drive);
140 break;
141 default:
142 log = 1;
143 break;
144 }
145 return log;
146}
147
148static
149void cdrom_analyze_sense_data(ide_drive_t *drive,
150 struct request *failed_command,
151 struct request_sense *sense)
152{
dbe217af
AC
153 unsigned long sector;
154 unsigned long bio_sectors;
155 unsigned long valid;
156 struct cdrom_info *info = drive->driver_data;
157
1da177e4
LT
158 if (!cdrom_log_sense(drive, failed_command, sense))
159 return;
160
161 /*
162 * If a read toc is executed for a CD-R or CD-RW medium where
163 * the first toc has not been recorded yet, it will fail with
164 * 05/24/00 (which is a confusing error)
165 */
166 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
167 if (sense->sense_key == 0x05 && sense->asc == 0x24)
168 return;
169
dbe217af
AC
170 if (sense->error_code == 0x70) { /* Current Error */
171 switch(sense->sense_key) {
172 case MEDIUM_ERROR:
173 case VOLUME_OVERFLOW:
174 case ILLEGAL_REQUEST:
175 if (!sense->valid)
176 break;
177 if (failed_command == NULL ||
178 !blk_fs_request(failed_command))
179 break;
180 sector = (sense->information[0] << 24) |
181 (sense->information[1] << 16) |
182 (sense->information[2] << 8) |
183 (sense->information[3]);
184
185 bio_sectors = bio_sectors(failed_command->bio);
186 if (bio_sectors < 4)
187 bio_sectors = 4;
188 if (drive->queue->hardsect_size == 2048)
189 sector <<= 2; /* Device sector size is 2K */
190 sector &= ~(bio_sectors -1);
191 valid = (sector - failed_command->sector) << 9;
192
193 if (valid < 0)
194 valid = 0;
195 if (sector < get_capacity(info->disk) &&
196 drive->probed_capacity - sector < 4 * 75) {
197 set_capacity(info->disk, sector);
198 }
199 }
200 }
1da177e4
LT
201#if VERBOSE_IDE_CD_ERRORS
202 {
203 int i;
70d1d47c 204 const char *s = "bad sense key!";
1da177e4
LT
205 char buf[80];
206
207 printk ("ATAPI device %s:\n", drive->name);
208 if (sense->error_code==0x70)
209 printk(" Error: ");
210 else if (sense->error_code==0x71)
211 printk(" Deferred Error: ");
212 else if (sense->error_code == 0x7f)
213 printk(" Vendor-specific Error: ");
214 else
215 printk(" Unknown Error Type: ");
216
74c8f97a 217 if (sense->sense_key < ARRAY_SIZE(sense_key_texts))
1da177e4 218 s = sense_key_texts[sense->sense_key];
1da177e4
LT
219
220 printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
221
222 if (sense->asc == 0x40) {
223 sprintf(buf, "Diagnostic failure on component 0x%02x",
224 sense->ascq);
225 s = buf;
226 } else {
74c8f97a 227 int lo = 0, mid, hi = ARRAY_SIZE(sense_data_texts);
1da177e4
LT
228 unsigned long key = (sense->sense_key << 16);
229 key |= (sense->asc << 8);
230 if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
231 key |= sense->ascq;
232 s = NULL;
233
234 while (hi > lo) {
235 mid = (lo + hi) / 2;
236 if (sense_data_texts[mid].asc_ascq == key ||
237 sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
238 s = sense_data_texts[mid].text;
239 break;
240 }
241 else if (sense_data_texts[mid].asc_ascq > key)
242 hi = mid;
243 else
244 lo = mid+1;
245 }
246 }
247
248 if (s == NULL) {
249 if (sense->asc > 0x80)
250 s = "(vendor-specific error)";
251 else
252 s = "(reserved error code)";
253 }
254
255 printk(KERN_ERR " %s -- (asc=0x%02x, ascq=0x%02x)\n",
256 s, sense->asc, sense->ascq);
257
258 if (failed_command != NULL) {
259
74c8f97a 260 int lo=0, mid, hi= ARRAY_SIZE(packet_command_texts);
1da177e4
LT
261 s = NULL;
262
263 while (hi > lo) {
264 mid = (lo + hi) / 2;
265 if (packet_command_texts[mid].packet_command ==
266 failed_command->cmd[0]) {
267 s = packet_command_texts[mid].text;
268 break;
269 }
270 if (packet_command_texts[mid].packet_command >
271 failed_command->cmd[0])
272 hi = mid;
273 else
274 lo = mid+1;
275 }
276
277 printk (KERN_ERR " The failed \"%s\" packet command was: \n \"", s);
278 for (i=0; i<sizeof (failed_command->cmd); i++)
279 printk ("%02x ", failed_command->cmd[i]);
280 printk ("\"\n");
281 }
282
283 /* The SKSV bit specifies validity of the sense_key_specific
284 * in the next two commands. It is bit 7 of the first byte.
285 * In the case of NOT_READY, if SKSV is set the drive can
286 * give us nice ETA readings.
287 */
288 if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
289 int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
290 printk(KERN_ERR " Command is %02d%% complete\n", progress / 0xffff);
291
292 }
293
294 if (sense->sense_key == ILLEGAL_REQUEST &&
295 (sense->sks[0] & 0x80) != 0) {
296 printk(KERN_ERR " Error in %s byte %d",
297 (sense->sks[0] & 0x40) != 0 ?
298 "command packet" : "command data",
299 (sense->sks[1] << 8) + sense->sks[2]);
300
301 if ((sense->sks[0] & 0x40) != 0)
302 printk (" bit %d", sense->sks[0] & 0x07);
303
304 printk ("\n");
305 }
306 }
307
308#else /* not VERBOSE_IDE_CD_ERRORS */
309
310 /* Suppress printing unit attention and `in progress of becoming ready'
311 errors when we're not being verbose. */
312
313 if (sense->sense_key == UNIT_ATTENTION ||
314 (sense->sense_key == NOT_READY && (sense->asc == 4 ||
315 sense->asc == 0x3a)))
316 return;
317
318 printk(KERN_ERR "%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
319 drive->name,
320 sense->error_code, sense->sense_key,
321 sense->asc, sense->ascq);
322#endif /* not VERBOSE_IDE_CD_ERRORS */
323}
324
325/*
326 * Initialize a ide-cd packet command request
327 */
328static void cdrom_prepare_request(ide_drive_t *drive, struct request *rq)
329{
330 struct cdrom_info *cd = drive->driver_data;
331
332 ide_init_drive_cmd(rq);
cea2885a 333 rq->cmd_type = REQ_TYPE_ATA_PC;
1da177e4
LT
334 rq->rq_disk = cd->disk;
335}
336
337static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
338 struct request *failed_command)
339{
340 struct cdrom_info *info = drive->driver_data;
341 struct request *rq = &info->request_sense_request;
342
343 if (sense == NULL)
344 sense = &info->sense_data;
345
346 /* stuff the sense request in front of our current request */
347 cdrom_prepare_request(drive, rq);
348
349 rq->data = sense;
350 rq->cmd[0] = GPCMD_REQUEST_SENSE;
351 rq->cmd[4] = rq->data_len = 18;
352
4aff5e23 353 rq->cmd_type = REQ_TYPE_SENSE;
1da177e4
LT
354
355 /* NOTE! Save the failed command in "rq->buffer" */
356 rq->buffer = (void *) failed_command;
357
358 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
359}
360
361static void cdrom_end_request (ide_drive_t *drive, int uptodate)
362{
363 struct request *rq = HWGROUP(drive)->rq;
364 int nsectors = rq->hard_cur_sectors;
365
4aff5e23 366 if (blk_sense_request(rq) && uptodate) {
1da177e4 367 /*
4aff5e23
JA
368 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
369 * failed request
1da177e4
LT
370 */
371 struct request *failed = (struct request *) rq->buffer;
372 struct cdrom_info *info = drive->driver_data;
373 void *sense = &info->sense_data;
374 unsigned long flags;
375
376 if (failed) {
377 if (failed->sense) {
378 sense = failed->sense;
379 failed->sense_len = rq->sense_len;
380 }
dbe217af 381 cdrom_analyze_sense_data(drive, failed, sense);
1da177e4
LT
382 /*
383 * now end failed request
384 */
dbe217af
AC
385 if (blk_fs_request(failed)) {
386 if (ide_end_dequeued_request(drive, failed, 0,
387 failed->hard_nr_sectors))
388 BUG();
389 } else {
390 spin_lock_irqsave(&ide_lock, flags);
5e36bb6e
KU
391 if (__blk_end_request(failed, -EIO,
392 failed->data_len))
393 BUG();
dbe217af
AC
394 spin_unlock_irqrestore(&ide_lock, flags);
395 }
396 } else
397 cdrom_analyze_sense_data(drive, NULL, sense);
1da177e4
LT
398 }
399
400 if (!rq->current_nr_sectors && blk_fs_request(rq))
401 uptodate = 1;
402 /* make sure it's fully ended */
403 if (blk_pc_request(rq))
404 nsectors = (rq->data_len + 511) >> 9;
405 if (!nsectors)
406 nsectors = 1;
407
408 ide_end_request(drive, uptodate, nsectors);
409}
410
dbe217af
AC
411static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 stat)
412{
413 if (stat & 0x80)
414 return;
415 ide_dump_status(drive, msg, stat);
416}
417
1da177e4
LT
418/* Returns 0 if the request should be continued.
419 Returns 1 if the request was ended. */
420static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
421{
422 struct request *rq = HWGROUP(drive)->rq;
423 int stat, err, sense_key;
424
425 /* Check for errors. */
426 stat = HWIF(drive)->INB(IDE_STATUS_REG);
427 if (stat_ret)
428 *stat_ret = stat;
429
430 if (OK_STAT(stat, good_stat, BAD_R_STAT))
431 return 0;
432
433 /* Get the IDE error register. */
434 err = HWIF(drive)->INB(IDE_ERROR_REG);
435 sense_key = err >> 4;
436
437 if (rq == NULL) {
438 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
439 return 1;
440 }
441
4aff5e23 442 if (blk_sense_request(rq)) {
1da177e4
LT
443 /* We got an error trying to get sense info
444 from the drive (probably while trying
445 to recover from a former error). Just give up. */
446
4aff5e23 447 rq->cmd_flags |= REQ_FAILED;
1da177e4
LT
448 cdrom_end_request(drive, 0);
449 ide_error(drive, "request sense failure", stat);
450 return 1;
451
8770c018 452 } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
1da177e4
LT
453 /* All other functions, except for READ. */
454 unsigned long flags;
455
456 /*
457 * if we have an error, pass back CHECK_CONDITION as the
458 * scsi status byte
459 */
b7156731 460 if (blk_pc_request(rq) && !rq->errors)
1da177e4
LT
461 rq->errors = SAM_STAT_CHECK_CONDITION;
462
463 /* Check for tray open. */
464 if (sense_key == NOT_READY) {
465 cdrom_saw_media_change (drive);
466 } else if (sense_key == UNIT_ATTENTION) {
467 /* Check for media change. */
468 cdrom_saw_media_change (drive);
469 /*printk("%s: media changed\n",drive->name);*/
470 return 0;
76ca1af1
SH
471 } else if ((sense_key == ILLEGAL_REQUEST) &&
472 (rq->cmd[0] == GPCMD_START_STOP_UNIT)) {
473 /*
474 * Don't print error message for this condition--
475 * SFF8090i indicates that 5/24/00 is the correct
476 * response to a request to close the tray if the
477 * drive doesn't have that capability.
478 * cdrom_log_sense() knows this!
479 */
4aff5e23 480 } else if (!(rq->cmd_flags & REQ_QUIET)) {
1da177e4
LT
481 /* Otherwise, print an error. */
482 ide_dump_status(drive, "packet command error", stat);
483 }
484
4aff5e23 485 rq->cmd_flags |= REQ_FAILED;
1da177e4
LT
486
487 /*
488 * instead of playing games with moving completions around,
489 * remove failed request completely and end it when the
490 * request sense has completed
491 */
492 if (stat & ERR_STAT) {
493 spin_lock_irqsave(&ide_lock, flags);
494 blkdev_dequeue_request(rq);
495 HWGROUP(drive)->rq = NULL;
496 spin_unlock_irqrestore(&ide_lock, flags);
497
498 cdrom_queue_request_sense(drive, rq->sense, rq);
499 } else
500 cdrom_end_request(drive, 0);
501
502 } else if (blk_fs_request(rq)) {
503 int do_end_request = 0;
504
505 /* Handle errors from READ and WRITE requests. */
506
507 if (blk_noretry_request(rq))
508 do_end_request = 1;
509
510 if (sense_key == NOT_READY) {
511 /* Tray open. */
512 if (rq_data_dir(rq) == READ) {
513 cdrom_saw_media_change (drive);
514
515 /* Fail the request. */
516 printk ("%s: tray open\n", drive->name);
517 do_end_request = 1;
518 } else {
519 struct cdrom_info *info = drive->driver_data;
520
521 /* allow the drive 5 seconds to recover, some
522 * devices will return this error while flushing
523 * data from cache */
524 if (!rq->errors)
525 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
526 rq->errors = 1;
527 if (time_after(jiffies, info->write_timeout))
528 do_end_request = 1;
529 else {
530 unsigned long flags;
531
532 /*
533 * take a breather relying on the
534 * unplug timer to kick us again
535 */
536 spin_lock_irqsave(&ide_lock, flags);
537 blk_plug_device(drive->queue);
538 spin_unlock_irqrestore(&ide_lock,flags);
539 return 1;
540 }
541 }
542 } else if (sense_key == UNIT_ATTENTION) {
543 /* Media change. */
544 cdrom_saw_media_change (drive);
545
546 /* Arrange to retry the request.
547 But be sure to give up if we've retried
548 too many times. */
549 if (++rq->errors > ERROR_MAX)
550 do_end_request = 1;
551 } else if (sense_key == ILLEGAL_REQUEST ||
552 sense_key == DATA_PROTECT) {
553 /* No point in retrying after an illegal
554 request or data protect error.*/
dbe217af 555 ide_dump_status_no_sense (drive, "command error", stat);
1da177e4
LT
556 do_end_request = 1;
557 } else if (sense_key == MEDIUM_ERROR) {
558 /* No point in re-trying a zillion times on a bad
559 * sector... If we got here the error is not correctable */
dbe217af 560 ide_dump_status_no_sense (drive, "media error (bad sector)", stat);
1da177e4
LT
561 do_end_request = 1;
562 } else if (sense_key == BLANK_CHECK) {
563 /* Disk appears blank ?? */
dbe217af 564 ide_dump_status_no_sense (drive, "media error (blank)", stat);
1da177e4
LT
565 do_end_request = 1;
566 } else if ((err & ~ABRT_ERR) != 0) {
567 /* Go to the default handler
568 for other errors. */
569 ide_error(drive, "cdrom_decode_status", stat);
570 return 1;
571 } else if ((++rq->errors > ERROR_MAX)) {
572 /* We've racked up too many retries. Abort. */
573 do_end_request = 1;
574 }
575
dbe217af
AC
576 /* End a request through request sense analysis when we have
577 sense data. We need this in order to perform end of media
578 processing */
579
580 if (do_end_request) {
581 if (stat & ERR_STAT) {
582 unsigned long flags;
583 spin_lock_irqsave(&ide_lock, flags);
584 blkdev_dequeue_request(rq);
585 HWGROUP(drive)->rq = NULL;
586 spin_unlock_irqrestore(&ide_lock, flags);
587
588 cdrom_queue_request_sense(drive, rq->sense, rq);
589 } else
590 cdrom_end_request(drive, 0);
591 } else {
592 /* If we got a CHECK_CONDITION status,
593 queue a request sense command. */
594 if (stat & ERR_STAT)
595 cdrom_queue_request_sense(drive, NULL, NULL);
596 }
1da177e4
LT
597 } else {
598 blk_dump_rq_flags(rq, "ide-cd: bad rq");
599 cdrom_end_request(drive, 0);
600 }
601
602 /* Retry, or handle the next request. */
603 return 1;
604}
605
606static int cdrom_timer_expiry(ide_drive_t *drive)
607{
608 struct request *rq = HWGROUP(drive)->rq;
609 unsigned long wait = 0;
610
611 /*
612 * Some commands are *slow* and normally take a long time to
613 * complete. Usually we can use the ATAPI "disconnect" to bypass
614 * this, but not all commands/drives support that. Let
615 * ide_timer_expiry keep polling us for these.
616 */
617 switch (rq->cmd[0]) {
618 case GPCMD_BLANK:
619 case GPCMD_FORMAT_UNIT:
620 case GPCMD_RESERVE_RZONE_TRACK:
621 case GPCMD_CLOSE_TRACK:
622 case GPCMD_FLUSH_CACHE:
623 wait = ATAPI_WAIT_PC;
624 break;
625 default:
4aff5e23 626 if (!(rq->cmd_flags & REQ_QUIET))
1da177e4
LT
627 printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
628 wait = 0;
629 break;
630 }
631 return wait;
632}
633
634/* Set up the device registers for transferring a packet command on DEV,
635 expecting to later transfer XFERLEN bytes. HANDLER is the routine
636 which actually transfers the command to the drive. If this is a
637 drq_interrupt device, this routine will arrange for HANDLER to be
638 called when the interrupt from the drive arrives. Otherwise, HANDLER
639 will be called immediately after the drive is prepared for the transfer. */
640
641static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
642 int xferlen,
643 ide_handler_t *handler)
644{
645 ide_startstop_t startstop;
646 struct cdrom_info *info = drive->driver_data;
647 ide_hwif_t *hwif = drive->hwif;
648
649 /* Wait for the controller to be idle. */
650 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
651 return startstop;
652
3a6a3549 653 /* FIXME: for Virtual DMA we must check harder */
1da177e4
LT
654 if (info->dma)
655 info->dma = !hwif->dma_setup(drive);
656
657 /* Set up the controller registers. */
2fc57388
BZ
658 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL |
659 IDE_TFLAG_NO_SELECT_MASK, xferlen, info->dma);
1da177e4
LT
660
661 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
f0dd8712
AL
662 /* waiting for CDB interrupt, not DMA yet. */
663 if (info->dma)
664 drive->waiting_for_dma = 0;
665
1da177e4
LT
666 /* packet command */
667 ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
668 return ide_started;
669 } else {
670 unsigned long flags;
671
672 /* packet command */
673 spin_lock_irqsave(&ide_lock, flags);
674 hwif->OUTBSYNC(drive, WIN_PACKETCMD, IDE_COMMAND_REG);
675 ndelay(400);
676 spin_unlock_irqrestore(&ide_lock, flags);
677
678 return (*handler) (drive);
679 }
680}
681
682/* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
683 The device registers must have already been prepared
684 by cdrom_start_packet_command.
685 HANDLER is the interrupt handler to call when the command completes
686 or there's data ready. */
1da177e4
LT
687#define ATAPI_MIN_CDB_BYTES 12
688static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
689 struct request *rq,
690 ide_handler_t *handler)
691{
692 ide_hwif_t *hwif = drive->hwif;
693 int cmd_len;
694 struct cdrom_info *info = drive->driver_data;
695 ide_startstop_t startstop;
696
697 if (CDROM_CONFIG_FLAGS(drive)->drq_interrupt) {
698 /* Here we should have been called after receiving an interrupt
699 from the device. DRQ should how be set. */
700
701 /* Check for errors. */
702 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
703 return ide_stopped;
f0dd8712
AL
704
705 /* Ok, next interrupt will be DMA interrupt. */
706 if (info->dma)
707 drive->waiting_for_dma = 1;
1da177e4
LT
708 } else {
709 /* Otherwise, we must wait for DRQ to get set. */
710 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
711 BUSY_STAT, WAIT_READY))
712 return startstop;
713 }
714
715 /* Arm the interrupt handler. */
716 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
717
718 /* ATAPI commands get padded out to 12 bytes minimum */
719 cmd_len = COMMAND_SIZE(rq->cmd[0]);
720 if (cmd_len < ATAPI_MIN_CDB_BYTES)
721 cmd_len = ATAPI_MIN_CDB_BYTES;
722
723 /* Send the command to the device. */
724 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
725
726 /* Start the DMA if need be */
727 if (info->dma)
728 hwif->dma_start(drive);
729
730 return ide_started;
731}
732
733/****************************************************************************
734 * Block read functions.
735 */
736
68661c53
BZ
737typedef void (xfer_func_t)(ide_drive_t *, void *, u32);
738
5a5222d9
BZ
739static void ide_cd_pad_transfer(ide_drive_t *drive, xfer_func_t *xf, int len)
740{
741 while (len > 0) {
742 int dum = 0;
743 xf(drive, &dum, sizeof(dum));
744 len -= sizeof(dum);
745 }
746}
747
1da177e4
LT
748/*
749 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
750 * buffer. Once the first sector is added, any subsequent sectors are
751 * assumed to be continuous (until the buffer is cleared). For the first
752 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
753 * the buffer is cleared.)
754 */
755static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
756 int sectors_to_transfer)
757{
758 struct cdrom_info *info = drive->driver_data;
759
760 /* Number of sectors to read into the buffer. */
761 int sectors_to_buffer = min_t(int, sectors_to_transfer,
762 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
763 info->nsectors_buffered);
764
765 char *dest;
766
767 /* If we couldn't get a buffer, don't try to buffer anything... */
768 if (info->buffer == NULL)
769 sectors_to_buffer = 0;
770
771 /* If this is the first sector in the buffer, remember its number. */
772 if (info->nsectors_buffered == 0)
773 info->sector_buffered = sector;
774
775 /* Read the data into the buffer. */
776 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
777 while (sectors_to_buffer > 0) {
778 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
779 --sectors_to_buffer;
780 --sectors_to_transfer;
781 ++info->nsectors_buffered;
782 dest += SECTOR_SIZE;
783 }
784
785 /* Throw away any remaining data. */
786 while (sectors_to_transfer > 0) {
787 static char dum[SECTOR_SIZE];
788 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
789 --sectors_to_transfer;
790 }
791}
792
793/*
794 * Check the contents of the interrupt reason register from the cdrom
795 * and attempt to recover if there are problems. Returns 0 if everything's
796 * ok; nonzero if the request has been terminated.
797 */
858119e1 798static
1da177e4
LT
799int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
800{
801 if (ireason == 2)
802 return 0;
803 else if (ireason == 0) {
5a5222d9
BZ
804 ide_hwif_t *hwif = drive->hwif;
805
1da177e4 806 /* Whoops... The drive is expecting to receive data from us! */
35379c07
BZ
807 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
808 drive->name, __FUNCTION__);
1da177e4
LT
809
810 /* Throw some data at the drive so it doesn't hang
811 and quit this request. */
5a5222d9 812 ide_cd_pad_transfer(drive, hwif->atapi_output_bytes, len);
1da177e4
LT
813 } else if (ireason == 1) {
814 /* Some drives (ASUS) seem to tell us that status
815 * info is available. just get it and ignore.
816 */
817 (void) HWIF(drive)->INB(IDE_STATUS_REG);
818 return 0;
819 } else {
820 /* Drive wants a command packet, or invalid ireason... */
35379c07
BZ
821 printk(KERN_ERR "%s: %s: bad interrupt reason 0x%02x\n",
822 drive->name, __FUNCTION__, ireason);
1da177e4
LT
823 }
824
825 cdrom_end_request(drive, 0);
826 return -1;
827}
828
829/*
830 * Interrupt routine. Called when a read request has completed.
831 */
832static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
833{
834 int stat;
835 int ireason, len, sectors_to_transfer, nskip;
836 struct cdrom_info *info = drive->driver_data;
837 u8 lowcyl = 0, highcyl = 0;
838 int dma = info->dma, dma_error = 0;
839
840 struct request *rq = HWGROUP(drive)->rq;
841
842 /*
843 * handle dma case
844 */
845 if (dma) {
846 info->dma = 0;
52ef2ed0
BZ
847 dma_error = HWIF(drive)->ide_dma_end(drive);
848 if (dma_error) {
849 printk(KERN_ERR "%s: DMA read error\n", drive->name);
7469aaf6 850 ide_dma_off(drive);
52ef2ed0 851 }
1da177e4
LT
852 }
853
854 if (cdrom_decode_status(drive, 0, &stat))
855 return ide_stopped;
856
857 if (dma) {
858 if (!dma_error) {
859 ide_end_request(drive, 1, rq->nr_sectors);
860 return ide_stopped;
861 } else
862 return ide_error(drive, "dma error", stat);
863 }
864
865 /* Read the interrupt reason and the transfer length. */
866 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
867 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
868 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
869
870 len = lowcyl + (256 * highcyl);
871
872 /* If DRQ is clear, the command has completed. */
873 if ((stat & DRQ_STAT) == 0) {
874 /* If we're not done filling the current buffer, complain.
875 Otherwise, complete the command normally. */
876 if (rq->current_nr_sectors > 0) {
877 printk (KERN_ERR "%s: cdrom_read_intr: data underrun (%d blocks)\n",
878 drive->name, rq->current_nr_sectors);
4aff5e23 879 rq->cmd_flags |= REQ_FAILED;
1da177e4
LT
880 cdrom_end_request(drive, 0);
881 } else
882 cdrom_end_request(drive, 1);
883 return ide_stopped;
884 }
885
886 /* Check that the drive is expecting to do the same thing we are. */
887 if (cdrom_read_check_ireason (drive, len, ireason))
888 return ide_stopped;
889
890 /* Assume that the drive will always provide data in multiples
891 of at least SECTOR_SIZE, as it gets hairy to keep track
892 of the transfers otherwise. */
893 if ((len % SECTOR_SIZE) != 0) {
894 printk (KERN_ERR "%s: cdrom_read_intr: Bad transfer size %d\n",
895 drive->name, len);
896 if (CDROM_CONFIG_FLAGS(drive)->limit_nframes)
897 printk (KERN_ERR " This drive is not supported by this version of the driver\n");
898 else {
899 printk (KERN_ERR " Trying to limit transfer sizes\n");
900 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
901 }
902 cdrom_end_request(drive, 0);
903 return ide_stopped;
904 }
905
906 /* The number of sectors we need to read from the drive. */
907 sectors_to_transfer = len / SECTOR_SIZE;
908
909 /* First, figure out if we need to bit-bucket
910 any of the leading sectors. */
911 nskip = min_t(int, rq->current_nr_sectors - bio_cur_sectors(rq->bio), sectors_to_transfer);
912
913 while (nskip > 0) {
914 /* We need to throw away a sector. */
915 static char dum[SECTOR_SIZE];
916 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
917
918 --rq->current_nr_sectors;
919 --nskip;
920 --sectors_to_transfer;
921 }
922
923 /* Now loop while we still have data to read from the drive. */
924 while (sectors_to_transfer > 0) {
925 int this_transfer;
926
927 /* If we've filled the present buffer but there's another
928 chained buffer after it, move on. */
929 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
930 cdrom_end_request(drive, 1);
931
932 /* If the buffers are full, cache the rest of the data in our
933 internal buffer. */
934 if (rq->current_nr_sectors == 0) {
935 cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
936 sectors_to_transfer = 0;
937 } else {
938 /* Transfer data to the buffers.
939 Figure out how many sectors we can transfer
940 to the current buffer. */
941 this_transfer = min_t(int, sectors_to_transfer,
942 rq->current_nr_sectors);
943
944 /* Read this_transfer sectors
945 into the current buffer. */
946 while (this_transfer > 0) {
947 HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
948 rq->buffer += SECTOR_SIZE;
949 --rq->nr_sectors;
950 --rq->current_nr_sectors;
951 ++rq->sector;
952 --this_transfer;
953 --sectors_to_transfer;
954 }
955 }
956 }
957
958 /* Done moving data! Wait for another interrupt. */
959 ide_set_handler(drive, &cdrom_read_intr, ATAPI_WAIT_PC, NULL);
960 return ide_started;
961}
962
963/*
964 * Try to satisfy some of the current read request from our cached data.
965 * Returns nonzero if the request has been completed, zero otherwise.
966 */
967static int cdrom_read_from_buffer (ide_drive_t *drive)
968{
969 struct cdrom_info *info = drive->driver_data;
970 struct request *rq = HWGROUP(drive)->rq;
971 unsigned short sectors_per_frame;
972
973 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
974
975 /* Can't do anything if there's no buffer. */
976 if (info->buffer == NULL) return 0;
977
978 /* Loop while this request needs data and the next block is present
979 in our cache. */
980 while (rq->nr_sectors > 0 &&
981 rq->sector >= info->sector_buffered &&
982 rq->sector < info->sector_buffered + info->nsectors_buffered) {
983 if (rq->current_nr_sectors == 0)
984 cdrom_end_request(drive, 1);
985
986 memcpy (rq->buffer,
987 info->buffer +
988 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
989 SECTOR_SIZE);
990 rq->buffer += SECTOR_SIZE;
991 --rq->current_nr_sectors;
992 --rq->nr_sectors;
993 ++rq->sector;
994 }
995
996 /* If we've satisfied the current request,
997 terminate it successfully. */
998 if (rq->nr_sectors == 0) {
999 cdrom_end_request(drive, 1);
1000 return -1;
1001 }
1002
1003 /* Move on to the next buffer if needed. */
1004 if (rq->current_nr_sectors == 0)
1005 cdrom_end_request(drive, 1);
1006
1007 /* If this condition does not hold, then the kluge i use to
1008 represent the number of sectors to skip at the start of a transfer
1009 will fail. I think that this will never happen, but let's be
1010 paranoid and check. */
1011 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
1012 (rq->sector & (sectors_per_frame - 1))) {
1013 printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1014 drive->name, (long)rq->sector);
1015 cdrom_end_request(drive, 0);
1016 return -1;
1017 }
1018
1019 return 0;
1020}
1021
1022/*
1023 * Routine to send a read packet command to the drive.
1024 * This is usually called directly from cdrom_start_read.
1025 * However, for drq_interrupt devices, it is called from an interrupt
1026 * when the drive is ready to accept the command.
1027 */
1028static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1029{
1030 struct request *rq = HWGROUP(drive)->rq;
1031 unsigned short sectors_per_frame;
1032 int nskip;
1033
1034 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1035
1036 /* If the requested sector doesn't start on a cdrom block boundary,
1037 we must adjust the start of the transfer so that it does,
1038 and remember to skip the first few sectors.
1039 If the CURRENT_NR_SECTORS field is larger than the size
1040 of the buffer, it will mean that we're to skip a number
1041 of sectors equal to the amount by which CURRENT_NR_SECTORS
1042 is larger than the buffer size. */
1043 nskip = rq->sector & (sectors_per_frame - 1);
1044 if (nskip > 0) {
1045 /* Sanity check... */
1046 if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
1047 (rq->sector & (sectors_per_frame - 1))) {
1048 printk(KERN_ERR "%s: cdrom_start_read_continuation: buffer botch (%u)\n",
1049 drive->name, rq->current_nr_sectors);
1050 cdrom_end_request(drive, 0);
1051 return ide_stopped;
1052 }
1053 rq->current_nr_sectors += nskip;
1054 }
1055
1056 /* Set up the command */
1057 rq->timeout = ATAPI_WAIT_PC;
1058
1059 /* Send the command to the drive and return. */
1060 return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
1061}
1062
1063
1064#define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1065#define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
1066#define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
1067
1068static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1069{
1070 struct cdrom_info *info = drive->driver_data;
1071 int stat;
1072 static int retry = 10;
1073
1074 if (cdrom_decode_status(drive, 0, &stat))
1075 return ide_stopped;
1076 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1077
1078 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
1079 if (--retry == 0) {
1080 /*
1081 * this condition is far too common, to bother
1082 * users about it
1083 */
1084 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
1085 drive->dsc_overlap = 0;
1086 }
1087 }
1088 return ide_stopped;
1089}
1090
1091static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1092{
1093 struct request *rq = HWGROUP(drive)->rq;
1094 sector_t frame = rq->sector;
1095
1096 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
1097
1098 memset(rq->cmd, 0, sizeof(rq->cmd));
1099 rq->cmd[0] = GPCMD_SEEK;
1100 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
1101
1102 rq->timeout = ATAPI_WAIT_PC;
1103 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
1104}
1105
1106static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1107{
1108 struct cdrom_info *info = drive->driver_data;
1109
1110 info->dma = 0;
1da177e4
LT
1111 info->start_seek = jiffies;
1112 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
1113}
1114
1115/* Fix up a possibly partially-processed request so that we can
1116 start it over entirely, or even put it back on the request queue. */
1117static void restore_request (struct request *rq)
1118{
1119 if (rq->buffer != bio_data(rq->bio)) {
1120 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
1121
1122 rq->buffer = bio_data(rq->bio);
1123 rq->nr_sectors += n;
1124 rq->sector -= n;
1125 }
1126 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
1127 rq->hard_nr_sectors = rq->nr_sectors;
1128 rq->hard_sector = rq->sector;
1129 rq->q->prep_rq_fn(rq->q, rq);
1130}
1131
1132/*
1133 * Start a read request from the CD-ROM.
1134 */
1135static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1136{
1137 struct cdrom_info *info = drive->driver_data;
1138 struct request *rq = HWGROUP(drive)->rq;
1139 unsigned short sectors_per_frame;
1140
1141 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1142
1143 /* We may be retrying this request after an error. Fix up
1144 any weirdness which might be present in the request packet. */
1145 restore_request(rq);
1146
1147 /* Satisfy whatever we can of this request from our cached sector. */
1148 if (cdrom_read_from_buffer(drive))
1149 return ide_stopped;
1150
1da177e4
LT
1151 /* Clear the local sector buffer. */
1152 info->nsectors_buffered = 0;
1153
1154 /* use dma, if possible. */
1155 info->dma = drive->using_dma;
1156 if ((rq->sector & (sectors_per_frame - 1)) ||
1157 (rq->nr_sectors & (sectors_per_frame - 1)))
1158 info->dma = 0;
1159
1da177e4
LT
1160 /* Start sending the read request to the drive. */
1161 return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1162}
1163
1164/****************************************************************************
1165 * Execute all other packet commands.
1166 */
1167
1168/* Interrupt routine for packet command completion. */
1169static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1170{
1da177e4 1171 struct request *rq = HWGROUP(drive)->rq;
68661c53
BZ
1172 xfer_func_t *xferfunc = NULL;
1173 int stat, ireason, len, thislen, write;
1da177e4 1174 u8 lowcyl = 0, highcyl = 0;
1da177e4
LT
1175
1176 /* Check for errors. */
1177 if (cdrom_decode_status(drive, 0, &stat))
1178 return ide_stopped;
1179
1180 /* Read the interrupt reason and the transfer length. */
8606ab09 1181 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1da177e4
LT
1182 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1183 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1184
1185 len = lowcyl + (256 * highcyl);
1186
1187 /* If DRQ is clear, the command has completed.
1188 Complain if we still have data left to transfer. */
1189 if ((stat & DRQ_STAT) == 0) {
1190 /* Some of the trailing request sense fields are optional, and
1191 some drives don't send them. Sigh. */
1192 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
1193 rq->data_len > 0 &&
1194 rq->data_len <= 5) {
1195 while (rq->data_len > 0) {
1196 *(unsigned char *)rq->data++ = 0;
1197 --rq->data_len;
1198 }
1199 }
1200
1201 if (rq->data_len == 0)
1202 cdrom_end_request(drive, 1);
1203 else {
4aff5e23 1204 rq->cmd_flags |= REQ_FAILED;
1da177e4
LT
1205 cdrom_end_request(drive, 0);
1206 }
1207 return ide_stopped;
1208 }
1209
1210 /* Figure out how much data to transfer. */
1211 thislen = rq->data_len;
68661c53
BZ
1212 if (thislen > len)
1213 thislen = len;
1da177e4 1214
8606ab09 1215 if (ireason == 0) {
68661c53
BZ
1216 write = 1;
1217 xferfunc = HWIF(drive)->atapi_output_bytes;
1218 } else if (ireason == 2) {
1219 write = 0;
1220 xferfunc = HWIF(drive)->atapi_input_bytes;
1da177e4
LT
1221 }
1222
68661c53 1223 if (xferfunc) {
1da177e4 1224 if (!rq->data) {
f1071e62
BZ
1225 printk(KERN_ERR "%s: confused, missing data\n",
1226 drive->name);
68661c53
BZ
1227 blk_dump_rq_flags(rq, write ? "cdrom_pc_intr, write"
1228 : "cdrom_pc_intr, read");
f1071e62 1229 goto pad;
1da177e4
LT
1230 }
1231 /* Transfer the data. */
68661c53 1232 xferfunc(drive, rq->data, thislen);
1da177e4 1233
1da177e4 1234 /* Keep count of how much data we've moved. */
5a5222d9 1235 len -= thislen;
1da177e4
LT
1236 rq->data += thislen;
1237 rq->data_len -= thislen;
1238
68661c53 1239 if (write && blk_sense_request(rq))
1da177e4
LT
1240 rq->sense_len += thislen;
1241 } else {
1da177e4 1242 printk (KERN_ERR "%s: cdrom_pc_intr: The drive "
1ad55440
RK
1243 "appears confused (ireason = 0x%02x). "
1244 "Trying to recover by ending request.\n",
1da177e4 1245 drive->name, ireason);
4aff5e23 1246 rq->cmd_flags |= REQ_FAILED;
1ad55440
RK
1247 cdrom_end_request(drive, 0);
1248 return ide_stopped;
1da177e4 1249 }
f1071e62 1250pad:
5a5222d9
BZ
1251 /*
1252 * If we haven't moved enough data to satisfy the drive,
1253 * add some padding.
1254 */
1255 if (len > 0)
1256 ide_cd_pad_transfer(drive, xferfunc, len);
1257
1da177e4
LT
1258 /* Now we wait for another interrupt. */
1259 ide_set_handler(drive, &cdrom_pc_intr, ATAPI_WAIT_PC, cdrom_timer_expiry);
1260 return ide_started;
1261}
1262
1263static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1264{
1265 struct request *rq = HWGROUP(drive)->rq;
1266
1267 if (!rq->timeout)
1268 rq->timeout = ATAPI_WAIT_PC;
1269
1270 /* Send the command to the drive and return. */
1271 return cdrom_transfer_packet_command(drive, rq, &cdrom_pc_intr);
1272}
1273
1274
1275static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1276{
1277 int len;
1278 struct request *rq = HWGROUP(drive)->rq;
1279 struct cdrom_info *info = drive->driver_data;
1280
1281 info->dma = 0;
4aff5e23 1282 rq->cmd_flags &= ~REQ_FAILED;
1da177e4
LT
1283 len = rq->data_len;
1284
1285 /* Start sending the command to the drive. */
1286 return cdrom_start_packet_command(drive, len, cdrom_do_pc_continuation);
1287}
1288
1289
dbe217af 1290static int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
1da177e4
LT
1291{
1292 struct request_sense sense;
1293 int retries = 10;
4aff5e23 1294 unsigned int flags = rq->cmd_flags;
1da177e4
LT
1295
1296 if (rq->sense == NULL)
1297 rq->sense = &sense;
1298
1299 /* Start of retry loop. */
1300 do {
1301 int error;
1302 unsigned long time = jiffies;
4aff5e23 1303 rq->cmd_flags = flags;
1da177e4
LT
1304
1305 error = ide_do_drive_cmd(drive, rq, ide_wait);
1306 time = jiffies - time;
1307
1308 /* FIXME: we should probably abort/retry or something
1309 * in case of failure */
4aff5e23 1310 if (rq->cmd_flags & REQ_FAILED) {
1da177e4
LT
1311 /* The request failed. Retry if it was due to a unit
1312 attention status
1313 (usually means media was changed). */
1314 struct request_sense *reqbuf = rq->sense;
1315
1316 if (reqbuf->sense_key == UNIT_ATTENTION)
1317 cdrom_saw_media_change(drive);
1318 else if (reqbuf->sense_key == NOT_READY &&
1319 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1320 /* The drive is in the process of loading
1321 a disk. Retry, but wait a little to give
1322 the drive time to complete the load. */
1323 ssleep(2);
1324 } else {
1325 /* Otherwise, don't retry. */
1326 retries = 0;
1327 }
1328 --retries;
1329 }
1330
1331 /* End of retry loop. */
4aff5e23 1332 } while ((rq->cmd_flags & REQ_FAILED) && retries >= 0);
1da177e4
LT
1333
1334 /* Return an error if the command failed. */
4aff5e23 1335 return (rq->cmd_flags & REQ_FAILED) ? -EIO : 0;
1da177e4
LT
1336}
1337
1338/*
1339 * Write handling
1340 */
858119e1 1341static int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1da177e4
LT
1342{
1343 /* Two notes about IDE interrupt reason here - 0 means that
1344 * the drive wants to receive data from us, 2 means that
1345 * the drive is expecting to transfer data to us.
1346 */
1347 if (ireason == 0)
1348 return 0;
1349 else if (ireason == 2) {
5a5222d9
BZ
1350 ide_hwif_t *hwif = drive->hwif;
1351
1da177e4 1352 /* Whoops... The drive wants to send data. */
35379c07
BZ
1353 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
1354 drive->name, __FUNCTION__);
1da177e4 1355
5a5222d9 1356 ide_cd_pad_transfer(drive, hwif->atapi_input_bytes, len);
1da177e4
LT
1357 } else {
1358 /* Drive wants a command packet, or invalid ireason... */
35379c07
BZ
1359 printk(KERN_ERR "%s: %s: bad interrupt reason 0x%02x\n",
1360 drive->name, __FUNCTION__, ireason);
1da177e4
LT
1361 }
1362
1363 cdrom_end_request(drive, 0);
1364 return 1;
1365}
1366
aaa04c28
KU
1367/*
1368 * Called from blk_end_request_callback() after the data of the request
1369 * is completed and before the request is completed.
1370 * By returning value '1', blk_end_request_callback() returns immediately
1371 * without completing the request.
1372 */
1373static int cdrom_newpc_intr_dummy_cb(struct request *rq)
1374{
1375 return 1;
1376}
1377
1da177e4
LT
1378/*
1379 * best way to deal with dma that is not sector aligned right now... note
1380 * that in this path we are not using ->data or ->buffer at all. this irs
1381 * can replace cdrom_pc_intr, cdrom_read_intr, and cdrom_write_intr in the
1382 * future.
1383 */
1384static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1385{
1386 struct cdrom_info *info = drive->driver_data;
1387 struct request *rq = HWGROUP(drive)->rq;
1388 int dma_error, dma, stat, ireason, len, thislen;
1389 u8 lowcyl, highcyl;
1390 xfer_func_t *xferfunc;
1391 unsigned long flags;
1392
1393 /* Check for errors. */
1394 dma_error = 0;
1395 dma = info->dma;
1396 if (dma) {
1397 info->dma = 0;
1398 dma_error = HWIF(drive)->ide_dma_end(drive);
eba15fba
BZ
1399 if (dma_error) {
1400 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
1401 rq_data_dir(rq) ? "write" : "read");
1402 ide_dma_off(drive);
1403 }
1da177e4
LT
1404 }
1405
1406 if (cdrom_decode_status(drive, 0, &stat))
1407 return ide_stopped;
1408
1409 /*
1410 * using dma, transfer is complete now
1411 */
1412 if (dma) {
eba15fba 1413 if (dma_error)
1da177e4 1414 return ide_error(drive, "dma error", stat);
1da177e4 1415
aaa04c28
KU
1416 spin_lock_irqsave(&ide_lock, flags);
1417 if (__blk_end_request(rq, 0, rq->data_len))
1418 BUG();
1419 HWGROUP(drive)->rq = NULL;
1420 spin_unlock_irqrestore(&ide_lock, flags);
1421
1422 return ide_stopped;
1da177e4
LT
1423 }
1424
1425 /*
1426 * ok we fall to pio :/
1427 */
1428 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1429 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1430 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1431
1432 len = lowcyl + (256 * highcyl);
1433 thislen = rq->data_len;
1434 if (thislen > len)
1435 thislen = len;
1436
1437 /*
1438 * If DRQ is clear, the command has completed.
1439 */
aaa04c28
KU
1440 if ((stat & DRQ_STAT) == 0) {
1441 spin_lock_irqsave(&ide_lock, flags);
4f4f6c25 1442 if (__blk_end_request(rq, 0, rq->data_len))
aaa04c28
KU
1443 BUG();
1444 HWGROUP(drive)->rq = NULL;
1445 spin_unlock_irqrestore(&ide_lock, flags);
1446
1447 return ide_stopped;
1448 }
1da177e4
LT
1449
1450 /*
1451 * check which way to transfer data
1452 */
1453 if (rq_data_dir(rq) == WRITE) {
1454 /*
1455 * write to drive
1456 */
1457 if (cdrom_write_check_ireason(drive, len, ireason))
1458 return ide_stopped;
1459
1460 xferfunc = HWIF(drive)->atapi_output_bytes;
1461 } else {
1462 /*
1463 * read from drive
1464 */
1465 if (cdrom_read_check_ireason(drive, len, ireason))
1466 return ide_stopped;
1467
1468 xferfunc = HWIF(drive)->atapi_input_bytes;
1469 }
1470
1471 /*
1472 * transfer data
1473 */
1474 while (thislen > 0) {
1475 int blen = blen = rq->data_len;
1476 char *ptr = rq->data;
1477
1478 /*
1479 * bio backed?
1480 */
1481 if (rq->bio) {
1482 ptr = bio_data(rq->bio);
1483 blen = bio_iovec(rq->bio)->bv_len;
1484 }
1485
1486 if (!ptr) {
1487 printk(KERN_ERR "%s: confused, missing data\n", drive->name);
1488 break;
1489 }
1490
1491 if (blen > thislen)
1492 blen = thislen;
1493
1494 xferfunc(drive, ptr, blen);
1495
1496 thislen -= blen;
1497 len -= blen;
1498 rq->data_len -= blen;
1499
1500 if (rq->bio)
aaa04c28
KU
1501 /*
1502 * The request can't be completed until DRQ is cleared.
1503 * So complete the data, but don't complete the request
1504 * using the dummy function for the callback feature
1505 * of blk_end_request_callback().
1506 */
1507 blk_end_request_callback(rq, 0, blen,
1508 cdrom_newpc_intr_dummy_cb);
1da177e4
LT
1509 else
1510 rq->data += blen;
1511 }
1512
1513 /*
1514 * pad, if necessary
1515 */
5a5222d9
BZ
1516 if (len > 0)
1517 ide_cd_pad_transfer(drive, xferfunc, len);
1da177e4 1518
125e1874 1519 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
1520
1521 ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, NULL);
1522 return ide_started;
1da177e4
LT
1523}
1524
1525static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1526{
1527 int stat, ireason, len, sectors_to_transfer, uptodate;
1528 struct cdrom_info *info = drive->driver_data;
1529 int dma_error = 0, dma = info->dma;
1530 u8 lowcyl = 0, highcyl = 0;
1531
1532 struct request *rq = HWGROUP(drive)->rq;
1533
1534 /* Check for errors. */
1535 if (dma) {
1536 info->dma = 0;
b481b238
BZ
1537 dma_error = HWIF(drive)->ide_dma_end(drive);
1538 if (dma_error) {
1539 printk(KERN_ERR "%s: DMA write error\n", drive->name);
7469aaf6 1540 ide_dma_off(drive);
1da177e4
LT
1541 }
1542 }
1543
1544 if (cdrom_decode_status(drive, 0, &stat))
1545 return ide_stopped;
1546
1547 /*
1548 * using dma, transfer is complete now
1549 */
1550 if (dma) {
1551 if (dma_error)
1552 return ide_error(drive, "dma error", stat);
1553
1554 ide_end_request(drive, 1, rq->nr_sectors);
1555 return ide_stopped;
1556 }
1557
1558 /* Read the interrupt reason and the transfer length. */
31a71191 1559 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1da177e4
LT
1560 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1561 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1562
1563 len = lowcyl + (256 * highcyl);
1564
1565 /* If DRQ is clear, the command has completed. */
1566 if ((stat & DRQ_STAT) == 0) {
1567 /* If we're not done writing, complain.
1568 * Otherwise, complete the command normally.
1569 */
1570 uptodate = 1;
1571 if (rq->current_nr_sectors > 0) {
b481b238
BZ
1572 printk(KERN_ERR "%s: %s: data underrun (%d blocks)\n",
1573 drive->name, __FUNCTION__,
1574 rq->current_nr_sectors);
1da177e4
LT
1575 uptodate = 0;
1576 }
1577 cdrom_end_request(drive, uptodate);
1578 return ide_stopped;
1579 }
1580
1581 /* Check that the drive is expecting to do the same thing we are. */
1582 if (cdrom_write_check_ireason(drive, len, ireason))
1583 return ide_stopped;
1584
1585 sectors_to_transfer = len / SECTOR_SIZE;
1586
1587 /*
1588 * now loop and write out the data
1589 */
1590 while (sectors_to_transfer > 0) {
1591 int this_transfer;
1592
1593 if (!rq->current_nr_sectors) {
b481b238
BZ
1594 printk(KERN_ERR "%s: %s: confused, missing data\n",
1595 drive->name, __FUNCTION__);
1da177e4
LT
1596 break;
1597 }
1598
1599 /*
1600 * Figure out how many sectors we can transfer
1601 */
1602 this_transfer = min_t(int, sectors_to_transfer, rq->current_nr_sectors);
1603
1604 while (this_transfer > 0) {
1605 HWIF(drive)->atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
1606 rq->buffer += SECTOR_SIZE;
1607 --rq->nr_sectors;
1608 --rq->current_nr_sectors;
1609 ++rq->sector;
1610 --this_transfer;
1611 --sectors_to_transfer;
1612 }
1613
1614 /*
1615 * current buffer complete, move on
1616 */
1617 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1618 cdrom_end_request(drive, 1);
1619 }
1620
1621 /* re-arm handler */
1622 ide_set_handler(drive, &cdrom_write_intr, ATAPI_WAIT_PC, NULL);
1623 return ide_started;
1624}
1625
1626static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1627{
1628 struct request *rq = HWGROUP(drive)->rq;
1629
1630#if 0 /* the immediate bit */
1631 rq->cmd[1] = 1 << 3;
1632#endif
1633 rq->timeout = ATAPI_WAIT_PC;
1634
1635 return cdrom_transfer_packet_command(drive, rq, cdrom_write_intr);
1636}
1637
1638static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
1639{
1640 struct cdrom_info *info = drive->driver_data;
1641 struct gendisk *g = info->disk;
1642 unsigned short sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1643
1644 /*
1645 * writes *must* be hardware frame aligned
1646 */
1647 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1648 (rq->sector & (sectors_per_frame - 1))) {
1649 cdrom_end_request(drive, 0);
1650 return ide_stopped;
1651 }
1652
1653 /*
1654 * disk has become write protected
1655 */
1656 if (g->policy) {
1657 cdrom_end_request(drive, 0);
1658 return ide_stopped;
1659 }
1660
1da177e4
LT
1661 info->nsectors_buffered = 0;
1662
1663 /* use dma, if possible. we don't need to check more, since we
1664 * know that the transfer is always (at least!) frame aligned */
1665 info->dma = drive->using_dma ? 1 : 0;
1da177e4
LT
1666
1667 info->devinfo.media_written = 1;
1668
1669 /* Start sending the write request to the drive. */
1670 return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1671}
1672
1673static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1674{
1675 struct request *rq = HWGROUP(drive)->rq;
1676
1677 if (!rq->timeout)
1678 rq->timeout = ATAPI_WAIT_PC;
1679
1680 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1681}
1682
1683static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1684{
1685 struct cdrom_info *info = drive->driver_data;
1686
4aff5e23 1687 rq->cmd_flags |= REQ_QUIET;
1da177e4
LT
1688
1689 info->dma = 0;
1da177e4
LT
1690
1691 /*
1692 * sg request
1693 */
1694 if (rq->bio) {
1695 int mask = drive->queue->dma_alignment;
1696 unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1697
1da177e4
LT
1698 info->dma = drive->using_dma;
1699
1700 /*
1701 * check if dma is safe
5d9e4ea5
LT
1702 *
1703 * NOTE! The "len" and "addr" checks should possibly have
1704 * separate masks.
1da177e4 1705 */
4e7c6816 1706 if ((rq->data_len & 15) || (addr & mask))
1da177e4
LT
1707 info->dma = 0;
1708 }
1709
1710 /* Start sending the command to the drive. */
1711 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1712}
1713
1714/****************************************************************************
1715 * cdrom driver request routine.
1716 */
1717static ide_startstop_t
1718ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1719{
1720 ide_startstop_t action;
1721 struct cdrom_info *info = drive->driver_data;
1722
1723 if (blk_fs_request(rq)) {
1724 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1725 unsigned long elapsed = jiffies - info->start_seek;
1726 int stat = HWIF(drive)->INB(IDE_STATUS_REG);
1727
1728 if ((stat & SEEK_STAT) != SEEK_STAT) {
1729 if (elapsed < IDECD_SEEK_TIMEOUT) {
1730 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1731 return ide_stopped;
1732 }
1733 printk (KERN_ERR "%s: DSC timeout\n", drive->name);
1734 }
1735 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1736 }
1737 if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
1738 action = cdrom_start_seek(drive, block);
1739 } else {
1740 if (rq_data_dir(rq) == READ)
1741 action = cdrom_start_read(drive, block);
1742 else
1743 action = cdrom_start_write(drive, rq);
1744 }
1745 info->last_block = block;
1746 return action;
cea2885a
JA
1747 } else if (rq->cmd_type == REQ_TYPE_SENSE ||
1748 rq->cmd_type == REQ_TYPE_ATA_PC) {
1da177e4 1749 return cdrom_do_packet_command(drive);
4aff5e23 1750 } else if (blk_pc_request(rq)) {
1da177e4 1751 return cdrom_do_block_pc(drive, rq);
4aff5e23 1752 } else if (blk_special_request(rq)) {
1da177e4
LT
1753 /*
1754 * right now this can only be a reset...
1755 */
1756 cdrom_end_request(drive, 1);
1757 return ide_stopped;
1758 }
1759
1760 blk_dump_rq_flags(rq, "ide-cd bad flags");
1761 cdrom_end_request(drive, 0);
1762 return ide_stopped;
1763}
1764
1765
1766
1767/****************************************************************************
1768 * Ioctl handling.
1769 *
1770 * Routines which queue packet commands take as a final argument a pointer
1771 * to a request_sense struct. If execution of the command results
1772 * in an error with a CHECK CONDITION status, this structure will be filled
1773 * with the results of the subsequent request sense command. The pointer
1774 * can also be NULL, in which case no sense information is returned.
1775 */
1776
1777#if ! STANDARD_ATAPI
1778static inline
1779int bin2bcd (int x)
1780{
1781 return (x%10) | ((x/10) << 4);
1782}
1783
1784
1785static inline
1786int bcd2bin (int x)
1787{
1788 return (x >> 4) * 10 + (x & 0x0f);
1789}
1790
1791static
1792void msf_from_bcd (struct atapi_msf *msf)
1793{
1794 msf->minute = bcd2bin (msf->minute);
1795 msf->second = bcd2bin (msf->second);
1796 msf->frame = bcd2bin (msf->frame);
1797}
1798
1799#endif /* not STANDARD_ATAPI */
1800
1801
1802static inline
1803void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1804{
1805 lba += CD_MSF_OFFSET;
1806 lba &= 0xffffff; /* negative lbas use only 24 bits */
1807 *m = lba / (CD_SECS * CD_FRAMES);
1808 lba %= (CD_SECS * CD_FRAMES);
1809 *s = lba / CD_FRAMES;
1810 *f = lba % CD_FRAMES;
1811}
1812
1813
1814static inline
1815int msf_to_lba (byte m, byte s, byte f)
1816{
1817 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1818}
1819
1820static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1821{
1822 struct request req;
1823 struct cdrom_info *info = drive->driver_data;
1824 struct cdrom_device_info *cdi = &info->devinfo;
1825
1826 cdrom_prepare_request(drive, &req);
1827
1828 req.sense = sense;
1829 req.cmd[0] = GPCMD_TEST_UNIT_READY;
4aff5e23 1830 req.cmd_flags |= REQ_QUIET;
1da177e4
LT
1831
1832#if ! STANDARD_ATAPI
1833 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1834 switch CDs instead of supporting the LOAD_UNLOAD opcode */
1835
1836 req.cmd[7] = cdi->sanyo_slot % 3;
1837#endif /* not STANDARD_ATAPI */
1838
1839 return cdrom_queue_packet_command(drive, &req);
1840}
1841
1842
1843/* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1844static int
1845cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
1846{
1847 struct request_sense my_sense;
1848 struct request req;
1849 int stat;
1850
1851 if (sense == NULL)
1852 sense = &my_sense;
1853
1854 /* If the drive cannot lock the door, just pretend. */
1855 if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
1856 stat = 0;
1857 } else {
1858 cdrom_prepare_request(drive, &req);
1859 req.sense = sense;
1860 req.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1861 req.cmd[4] = lockflag ? 1 : 0;
1862 stat = cdrom_queue_packet_command(drive, &req);
1863 }
1864
1865 /* If we got an illegal field error, the drive
1866 probably cannot lock the door. */
1867 if (stat != 0 &&
1868 sense->sense_key == ILLEGAL_REQUEST &&
1869 (sense->asc == 0x24 || sense->asc == 0x20)) {
1870 printk (KERN_ERR "%s: door locking not supported\n",
1871 drive->name);
1872 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
1873 stat = 0;
1874 }
1875
1876 /* no medium, that's alright. */
1877 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
1878 stat = 0;
1879
1880 if (stat == 0)
1881 CDROM_STATE_FLAGS(drive)->door_locked = lockflag;
1882
1883 return stat;
1884}
1885
1886
1887/* Eject the disk if EJECTFLAG is 0.
1888 If EJECTFLAG is 1, try to reload the disk. */
1889static int cdrom_eject(ide_drive_t *drive, int ejectflag,
1890 struct request_sense *sense)
1891{
1892 struct request req;
1893 char loej = 0x02;
1894
1895 if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
1896 return -EDRIVE_CANT_DO_THIS;
1897
1898 /* reload fails on some drives, if the tray is locked */
1899 if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
1900 return 0;
1901
1902 cdrom_prepare_request(drive, &req);
1903
1904 /* only tell drive to close tray if open, if it can do that */
1905 if (ejectflag && !CDROM_CONFIG_FLAGS(drive)->close_tray)
1906 loej = 0;
1907
1908 req.sense = sense;
1909 req.cmd[0] = GPCMD_START_STOP_UNIT;
1910 req.cmd[4] = loej | (ejectflag != 0);
1911 return cdrom_queue_packet_command(drive, &req);
1912}
1913
1914static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1915 unsigned long *sectors_per_frame,
1916 struct request_sense *sense)
1917{
1918 struct {
1919 __u32 lba;
1920 __u32 blocklen;
1921 } capbuf;
1922
1923 int stat;
1924 struct request req;
1925
1926 cdrom_prepare_request(drive, &req);
1927
1928 req.sense = sense;
1929 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
1930 req.data = (char *)&capbuf;
1931 req.data_len = sizeof(capbuf);
4aff5e23 1932 req.cmd_flags |= REQ_QUIET;
1da177e4
LT
1933
1934 stat = cdrom_queue_packet_command(drive, &req);
1935 if (stat == 0) {
1936 *capacity = 1 + be32_to_cpu(capbuf.lba);
1937 *sectors_per_frame =
1938 be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
1939 }
1940
1941 return stat;
1942}
1943
1944static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1945 int format, char *buf, int buflen,
1946 struct request_sense *sense)
1947{
1948 struct request req;
1949
1950 cdrom_prepare_request(drive, &req);
1951
1952 req.sense = sense;
1953 req.data = buf;
1954 req.data_len = buflen;
4aff5e23 1955 req.cmd_flags |= REQ_QUIET;
1da177e4
LT
1956 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1957 req.cmd[6] = trackno;
1958 req.cmd[7] = (buflen >> 8);
1959 req.cmd[8] = (buflen & 0xff);
1960 req.cmd[9] = (format << 6);
1961
1962 if (msf_flag)
1963 req.cmd[1] = 2;
1964
1965 return cdrom_queue_packet_command(drive, &req);
1966}
1967
1968
1969/* Try to read the entire TOC for the disk into our internal buffer. */
1970static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
1971{
1972 int stat, ntracks, i;
1973 struct cdrom_info *info = drive->driver_data;
1974 struct cdrom_device_info *cdi = &info->devinfo;
1975 struct atapi_toc *toc = info->toc;
1976 struct {
1977 struct atapi_toc_header hdr;
1978 struct atapi_toc_entry ent;
1979 } ms_tmp;
1980 long last_written;
1981 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1982
1983 if (toc == NULL) {
1984 /* Try to allocate space. */
2a91f3e5 1985 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
1da177e4
LT
1986 if (toc == NULL) {
1987 printk (KERN_ERR "%s: No cdrom TOC buffer!\n", drive->name);
1988 return -ENOMEM;
1989 }
2a91f3e5 1990 info->toc = toc;
1da177e4
LT
1991 }
1992
1993 /* Check to see if the existing data is still valid.
1994 If it is, just return. */
1995 (void) cdrom_check_status(drive, sense);
1996
1997 if (CDROM_STATE_FLAGS(drive)->toc_valid)
1998 return 0;
1999
2000 /* Try to get the total cdrom capacity and sector size. */
2001 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
2002 sense);
2003 if (stat)
2004 toc->capacity = 0x1fffff;
2005
2006 set_capacity(info->disk, toc->capacity * sectors_per_frame);
dbe217af
AC
2007 /* Save a private copy of te TOC capacity for error handling */
2008 drive->probed_capacity = toc->capacity * sectors_per_frame;
2009
1da177e4
LT
2010 blk_queue_hardsect_size(drive->queue,
2011 sectors_per_frame << SECTOR_BITS);
2012
2013 /* First read just the header, so we know how long the TOC is. */
2014 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
2015 sizeof(struct atapi_toc_header), sense);
2a91f3e5
JJ
2016 if (stat)
2017 return stat;
1da177e4
LT
2018
2019#if ! STANDARD_ATAPI
2020 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2021 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2022 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2023 }
2024#endif /* not STANDARD_ATAPI */
2025
2026 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2027 if (ntracks <= 0)
2028 return -EIO;
2029 if (ntracks > MAX_TRACKS)
2030 ntracks = MAX_TRACKS;
2031
2032 /* Now read the whole schmeer. */
2033 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
2034 (char *)&toc->hdr,
2035 sizeof(struct atapi_toc_header) +
2036 (ntracks + 1) *
2037 sizeof(struct atapi_toc_entry), sense);
2038
2039 if (stat && toc->hdr.first_track > 1) {
2040 /* Cds with CDI tracks only don't have any TOC entries,
2041 despite of this the returned values are
2042 first_track == last_track = number of CDI tracks + 1,
2043 so that this case is indistinguishable from the same
2044 layout plus an additional audio track.
2045 If we get an error for the regular case, we assume
2046 a CDI without additional audio tracks. In this case
2047 the readable TOC is empty (CDI tracks are not included)
96de0e25 2048 and only holds the Leadout entry. Heiko Eißfeldt */
1da177e4
LT
2049 ntracks = 0;
2050 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
2051 (char *)&toc->hdr,
2052 sizeof(struct atapi_toc_header) +
2053 (ntracks + 1) *
2054 sizeof(struct atapi_toc_entry),
2055 sense);
2056 if (stat) {
2057 return stat;
2058 }
2059#if ! STANDARD_ATAPI
2060 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2061 toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
2062 toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
2063 } else
2064#endif /* not STANDARD_ATAPI */
2065 {
2066 toc->hdr.first_track = CDROM_LEADOUT;
2067 toc->hdr.last_track = CDROM_LEADOUT;
2068 }
2069 }
2070
2071 if (stat)
2072 return stat;
2073
2074 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
2075
2076#if ! STANDARD_ATAPI
2077 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2078 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2079 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2080 }
2081#endif /* not STANDARD_ATAPI */
2082
2083 for (i=0; i<=ntracks; i++) {
2084#if ! STANDARD_ATAPI
2085 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
2086 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd)
2087 toc->ent[i].track = bcd2bin(toc->ent[i].track);
2088 msf_from_bcd(&toc->ent[i].addr.msf);
2089 }
2090#endif /* not STANDARD_ATAPI */
2091 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
2092 toc->ent[i].addr.msf.second,
2093 toc->ent[i].addr.msf.frame);
2094 }
2095
2096 /* Read the multisession information. */
2097 if (toc->hdr.first_track != CDROM_LEADOUT) {
2098 /* Read the multisession information. */
2099 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
2100 sizeof(ms_tmp), sense);
2a91f3e5
JJ
2101 if (stat)
2102 return stat;
1da177e4
LT
2103
2104 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
2105 } else {
2106 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
2107 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
2108 }
2109
2110#if ! STANDARD_ATAPI
2111 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
2112 /* Re-read multisession information using MSF format */
2113 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
2114 sizeof(ms_tmp), sense);
2115 if (stat)
2116 return stat;
2117
2118 msf_from_bcd (&ms_tmp.ent.addr.msf);
2119 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
2120 ms_tmp.ent.addr.msf.second,
2121 ms_tmp.ent.addr.msf.frame);
2122 }
2123#endif /* not STANDARD_ATAPI */
2124
2125 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
2126
2127 /* Now try to get the total cdrom capacity. */
2128 stat = cdrom_get_last_written(cdi, &last_written);
2129 if (!stat && (last_written > toc->capacity)) {
2130 toc->capacity = last_written;
2131 set_capacity(info->disk, toc->capacity * sectors_per_frame);
dbe217af 2132 drive->probed_capacity = toc->capacity * sectors_per_frame;
1da177e4
LT
2133 }
2134
2135 /* Remember that we've read this stuff. */
2136 CDROM_STATE_FLAGS(drive)->toc_valid = 1;
2137
2138 return 0;
2139}
2140
2141
2142static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2143 int buflen, struct request_sense *sense)
2144{
2145 struct request req;
2146
2147 cdrom_prepare_request(drive, &req);
2148
2149 req.sense = sense;
2150 req.data = buf;
2151 req.data_len = buflen;
2152 req.cmd[0] = GPCMD_READ_SUBCHANNEL;
2153 req.cmd[1] = 2; /* MSF addressing */
2154 req.cmd[2] = 0x40; /* request subQ data */
2155 req.cmd[3] = format;
2156 req.cmd[7] = (buflen >> 8);
2157 req.cmd[8] = (buflen & 0xff);
2158 return cdrom_queue_packet_command(drive, &req);
2159}
2160
2161/* ATAPI cdrom drives are free to select the speed you request or any slower
2162 rate :-( Requesting too fast a speed will _not_ produce an error. */
2163static int cdrom_select_speed(ide_drive_t *drive, int speed,
2164 struct request_sense *sense)
2165{
2166 struct request req;
2167 cdrom_prepare_request(drive, &req);
2168
2169 req.sense = sense;
2170 if (speed == 0)
2171 speed = 0xffff; /* set to max */
2172 else
2173 speed *= 177; /* Nx to kbytes/s */
2174
2175 req.cmd[0] = GPCMD_SET_SPEED;
2176 /* Read Drive speed in kbytes/second MSB */
2177 req.cmd[2] = (speed >> 8) & 0xff;
2178 /* Read Drive speed in kbytes/second LSB */
2179 req.cmd[3] = speed & 0xff;
2180 if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
2181 CDROM_CONFIG_FLAGS(drive)->cd_rw ||
2182 CDROM_CONFIG_FLAGS(drive)->dvd_r) {
2183 /* Write Drive speed in kbytes/second MSB */
2184 req.cmd[4] = (speed >> 8) & 0xff;
2185 /* Write Drive speed in kbytes/second LSB */
2186 req.cmd[5] = speed & 0xff;
2187 }
2188
2189 return cdrom_queue_packet_command(drive, &req);
2190}
2191
2192static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2193{
2194 struct request_sense sense;
2195 struct request req;
2196
2197 cdrom_prepare_request(drive, &req);
2198
2199 req.sense = &sense;
2200 req.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2201 lba_to_msf(lba_start, &req.cmd[3], &req.cmd[4], &req.cmd[5]);
2202 lba_to_msf(lba_end-1, &req.cmd[6], &req.cmd[7], &req.cmd[8]);
2203
2204 return cdrom_queue_packet_command(drive, &req);
2205}
2206
2207static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2208 struct atapi_toc_entry **ent)
2209{
2210 struct cdrom_info *info = drive->driver_data;
2211 struct atapi_toc *toc = info->toc;
2212 int ntracks;
2213
2214 /*
2215 * don't serve cached data, if the toc isn't valid
2216 */
2217 if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2218 return -EINVAL;
2219
2220 /* Check validity of requested track number. */
2221 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2222 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2223 if (track == CDROM_LEADOUT)
2224 *ent = &toc->ent[ntracks];
2225 else if (track < toc->hdr.first_track ||
2226 track > toc->hdr.last_track)
2227 return -EINVAL;
2228 else
2229 *ent = &toc->ent[track - toc->hdr.first_track];
2230
2231 return 0;
2232}
2233
2234/* the generic packet interface to cdrom.c */
2235static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2236 struct packet_command *cgc)
2237{
2238 struct request req;
2a91f3e5 2239 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2240
2241 if (cgc->timeout <= 0)
2242 cgc->timeout = ATAPI_WAIT_PC;
2243
2244 /* here we queue the commands from the uniform CD-ROM
2245 layer. the packet must be complete, as we do not
2246 touch it at all. */
2247 cdrom_prepare_request(drive, &req);
2248 memcpy(req.cmd, cgc->cmd, CDROM_PACKET_SIZE);
2249 if (cgc->sense)
2250 memset(cgc->sense, 0, sizeof(struct request_sense));
2251 req.data = cgc->buffer;
2252 req.data_len = cgc->buflen;
2253 req.timeout = cgc->timeout;
2254
2255 if (cgc->quiet)
4aff5e23 2256 req.cmd_flags |= REQ_QUIET;
1da177e4
LT
2257
2258 req.sense = cgc->sense;
2259 cgc->stat = cdrom_queue_packet_command(drive, &req);
2260 if (!cgc->stat)
2261 cgc->buflen -= req.data_len;
2262 return cgc->stat;
2263}
2264
1da177e4
LT
2265static
2266int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2267 unsigned int cmd, void *arg)
2268
2269{
2a91f3e5 2270 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2271 struct cdrom_info *info = drive->driver_data;
2272 int stat;
2273
2274 switch (cmd) {
2275 /*
2276 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2277 * atapi doesn't support it
2278 */
2279 case CDROMPLAYTRKIND: {
2280 unsigned long lba_start, lba_end;
2a91f3e5 2281 struct cdrom_ti *ti = arg;
1da177e4
LT
2282 struct atapi_toc_entry *first_toc, *last_toc;
2283
2284 stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2285 if (stat)
2286 return stat;
2287
2288 stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2289 if (stat)
2290 return stat;
2291
2292 if (ti->cdti_trk1 != CDROM_LEADOUT)
2293 ++last_toc;
2294 lba_start = first_toc->addr.lba;
2295 lba_end = last_toc->addr.lba;
2296
2297 if (lba_end <= lba_start)
2298 return -EINVAL;
2299
2300 return cdrom_play_audio(drive, lba_start, lba_end);
2301 }
2302
2303 case CDROMREADTOCHDR: {
2a91f3e5 2304 struct cdrom_tochdr *tochdr = arg;
1da177e4
LT
2305 struct atapi_toc *toc;
2306
2307 /* Make sure our saved TOC is valid. */
2308 stat = cdrom_read_toc(drive, NULL);
2a91f3e5
JJ
2309 if (stat)
2310 return stat;
1da177e4
LT
2311
2312 toc = info->toc;
2313 tochdr->cdth_trk0 = toc->hdr.first_track;
2314 tochdr->cdth_trk1 = toc->hdr.last_track;
2315
2316 return 0;
2317 }
2318
2319 case CDROMREADTOCENTRY: {
2a91f3e5 2320 struct cdrom_tocentry *tocentry = arg;
1da177e4
LT
2321 struct atapi_toc_entry *toce;
2322
2323 stat = cdrom_get_toc_entry(drive, tocentry->cdte_track, &toce);
2a91f3e5
JJ
2324 if (stat)
2325 return stat;
1da177e4
LT
2326
2327 tocentry->cdte_ctrl = toce->control;
2328 tocentry->cdte_adr = toce->adr;
2329 if (tocentry->cdte_format == CDROM_MSF) {
2330 lba_to_msf (toce->addr.lba,
2331 &tocentry->cdte_addr.msf.minute,
2332 &tocentry->cdte_addr.msf.second,
2333 &tocentry->cdte_addr.msf.frame);
2334 } else
2335 tocentry->cdte_addr.lba = toce->addr.lba;
2336
2337 return 0;
2338 }
2339
2340 default:
2341 return -EINVAL;
2342 }
2343}
2344
2345static
2346int ide_cdrom_reset (struct cdrom_device_info *cdi)
2347{
2a91f3e5 2348 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2349 struct request_sense sense;
2350 struct request req;
2351 int ret;
2352
2353 cdrom_prepare_request(drive, &req);
4aff5e23
JA
2354 req.cmd_type = REQ_TYPE_SPECIAL;
2355 req.cmd_flags = REQ_QUIET;
1da177e4
LT
2356 ret = ide_do_drive_cmd(drive, &req, ide_wait);
2357
2358 /*
2359 * A reset will unlock the door. If it was previously locked,
2360 * lock it again.
2361 */
2362 if (CDROM_STATE_FLAGS(drive)->door_locked)
2363 (void) cdrom_lockdoor(drive, 1, &sense);
2364
2365 return ret;
2366}
2367
2368
2369static
2370int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2371{
2a91f3e5 2372 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2373 struct request_sense sense;
2374
2375 if (position) {
2376 int stat = cdrom_lockdoor(drive, 0, &sense);
2a91f3e5
JJ
2377 if (stat)
2378 return stat;
1da177e4
LT
2379 }
2380
2381 return cdrom_eject(drive, !position, &sense);
2382}
2383
2384static
2385int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2386{
2a91f3e5 2387 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2388 return cdrom_lockdoor(drive, lock, NULL);
2389}
2390
9235e68b
EP
2391static
2392int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2393{
2394 struct cdrom_info *info = drive->driver_data;
2395 struct cdrom_device_info *cdi = &info->devinfo;
2396 struct packet_command cgc;
2397 int stat, attempts = 3, size = sizeof(*cap);
2398
2399 /*
2400 * ACER50 (and others?) require the full spec length mode sense
2401 * page capabilities size, but older drives break.
2402 */
2403 if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2404 !strcmp(drive->id->model, "WPI CDS-32X")))
2405 size -= sizeof(cap->pad);
2406
2407 init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2408 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2409 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2410 if (!stat)
2411 break;
2412 } while (--attempts);
2413 return stat;
2414}
2415
2416static
2417void ide_cdrom_update_speed (ide_drive_t *drive, struct atapi_capabilities_page *cap)
2418{
2419 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2420 if (!drive->id->model[0] &&
2421 !strncmp(drive->id->fw_rev, "241N", 4)) {
2422 CDROM_STATE_FLAGS(drive)->current_speed =
aa5dc8eb 2423 (le16_to_cpu(cap->curspeed) + (176/2)) / 176;
9235e68b 2424 CDROM_CONFIG_FLAGS(drive)->max_speed =
aa5dc8eb 2425 (le16_to_cpu(cap->maxspeed) + (176/2)) / 176;
9235e68b
EP
2426 } else {
2427 CDROM_STATE_FLAGS(drive)->current_speed =
aa5dc8eb 2428 (be16_to_cpu(cap->curspeed) + (176/2)) / 176;
9235e68b 2429 CDROM_CONFIG_FLAGS(drive)->max_speed =
aa5dc8eb 2430 (be16_to_cpu(cap->maxspeed) + (176/2)) / 176;
9235e68b
EP
2431 }
2432}
2433
1da177e4
LT
2434static
2435int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2436{
2a91f3e5 2437 ide_drive_t *drive = cdi->handle;
1da177e4 2438 struct request_sense sense;
9235e68b 2439 struct atapi_capabilities_page cap;
1da177e4
LT
2440 int stat;
2441
2442 if ((stat = cdrom_select_speed(drive, speed, &sense)) < 0)
2443 return stat;
2444
9235e68b
EP
2445 if (!ide_cdrom_get_capabilities(drive, &cap)) {
2446 ide_cdrom_update_speed(drive, &cap);
2447 cdi->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2448 }
1da177e4
LT
2449 return 0;
2450}
2451
2452/*
2453 * add logic to try GET_EVENT command first to check for media and tray
2454 * status. this should be supported by newer cd-r/w and all DVD etc
2455 * drives
2456 */
2457static
2458int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2459{
2a91f3e5 2460 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2461 struct media_event_desc med;
2462 struct request_sense sense;
2463 int stat;
2464
2465 if (slot_nr != CDSL_CURRENT)
2466 return -EINVAL;
2467
2468 stat = cdrom_check_status(drive, &sense);
2469 if (!stat || sense.sense_key == UNIT_ATTENTION)
2470 return CDS_DISC_OK;
2471
2472 if (!cdrom_get_media_event(cdi, &med)) {
2473 if (med.media_present)
2474 return CDS_DISC_OK;
2475 else if (med.door_open)
2476 return CDS_TRAY_OPEN;
2477 else
2478 return CDS_NO_DISC;
2479 }
2480
2481 if (sense.sense_key == NOT_READY && sense.asc == 0x04 && sense.ascq == 0x04)
2482 return CDS_DISC_OK;
2483
2484 /*
2485 * If not using Mt Fuji extended media tray reports,
2486 * just return TRAY_OPEN since ATAPI doesn't provide
2487 * any other way to detect this...
2488 */
2489 if (sense.sense_key == NOT_READY) {
dbe217af
AC
2490 if (sense.asc == 0x3a && sense.ascq == 1)
2491 return CDS_NO_DISC;
2492 else
2493 return CDS_TRAY_OPEN;
1da177e4 2494 }
1da177e4
LT
2495 return CDS_DRIVE_NOT_READY;
2496}
2497
2498static
2499int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2500 struct cdrom_multisession *ms_info)
2501{
2502 struct atapi_toc *toc;
2a91f3e5 2503 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2504 struct cdrom_info *info = drive->driver_data;
2505 struct request_sense sense;
2506 int ret;
2507
2508 if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2509 if ((ret = cdrom_read_toc(drive, &sense)))
2510 return ret;
2511
2512 toc = info->toc;
2513 ms_info->addr.lba = toc->last_session_lba;
2514 ms_info->xa_flag = toc->xa_flag;
2515
2516 return 0;
2517}
2518
2519static
2520int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2521 struct cdrom_mcn *mcn_info)
2522{
2523 int stat;
2524 char mcnbuf[24];
2a91f3e5 2525 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2526
2527/* get MCN */
2528 if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2529 return stat;
2530
2531 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2532 sizeof (mcn_info->medium_catalog_number)-1);
2533 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2534 = '\0';
2535
2536 return 0;
2537}
2538
2539
2540
2541/****************************************************************************
2542 * Other driver requests (open, close, check media change).
2543 */
2544
2545static
2546int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2547 int slot_nr)
2548{
2a91f3e5 2549 ide_drive_t *drive = cdi->handle;
1da177e4
LT
2550 int retval;
2551
2552 if (slot_nr == CDSL_CURRENT) {
2553 (void) cdrom_check_status(drive, NULL);
2554 retval = CDROM_STATE_FLAGS(drive)->media_changed;
2555 CDROM_STATE_FLAGS(drive)->media_changed = 0;
2556 return retval;
2557 } else {
2558 return -EINVAL;
2559 }
2560}
2561
2562
2563static
2564int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2565{
2566 return 0;
2567}
2568
2569/*
2570 * Close down the device. Invalidate all cached blocks.
2571 */
2572
2573static
2574void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2575{
2576 ide_drive_t *drive = cdi->handle;
2577
2578 if (!cdi->use_count)
2579 CDROM_STATE_FLAGS(drive)->toc_valid = 0;
2580}
2581
2582
2583
2584/****************************************************************************
2585 * Device initialization.
2586 */
2587static struct cdrom_device_ops ide_cdrom_dops = {
2588 .open = ide_cdrom_open_real,
2589 .release = ide_cdrom_release_real,
2590 .drive_status = ide_cdrom_drive_status,
2591 .media_changed = ide_cdrom_check_media_change_real,
2592 .tray_move = ide_cdrom_tray_move,
2593 .lock_door = ide_cdrom_lock_door,
2594 .select_speed = ide_cdrom_select_speed,
2595 .get_last_session = ide_cdrom_get_last_session,
2596 .get_mcn = ide_cdrom_get_mcn,
2597 .reset = ide_cdrom_reset,
2598 .audio_ioctl = ide_cdrom_audio_ioctl,
1da177e4
LT
2599 .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2600 CDC_SELECT_SPEED | CDC_SELECT_DISC |
2601 CDC_MULTI_SESSION | CDC_MCN |
2602 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
6a2900b6 2603 CDC_DRIVE_STATUS | CDC_CD_R |
1da177e4
LT
2604 CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2605 CDC_GENERIC_PACKET | CDC_MO_DRIVE | CDC_MRW |
2606 CDC_MRW_W | CDC_RAM,
2607 .generic_packet = ide_cdrom_packet,
2608};
2609
2610static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2611{
2612 struct cdrom_info *info = drive->driver_data;
2613 struct cdrom_device_info *devinfo = &info->devinfo;
2614
2615 devinfo->ops = &ide_cdrom_dops;
2616 devinfo->mask = 0;
2617 devinfo->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2618 devinfo->capacity = nslots;
2a91f3e5 2619 devinfo->handle = drive;
1da177e4
LT
2620 strcpy(devinfo->name, drive->name);
2621
2622 /* set capability mask to match the probe. */
2623 if (!CDROM_CONFIG_FLAGS(drive)->cd_r)
2624 devinfo->mask |= CDC_CD_R;
2625 if (!CDROM_CONFIG_FLAGS(drive)->cd_rw)
2626 devinfo->mask |= CDC_CD_RW;
2627 if (!CDROM_CONFIG_FLAGS(drive)->dvd)
2628 devinfo->mask |= CDC_DVD;
2629 if (!CDROM_CONFIG_FLAGS(drive)->dvd_r)
2630 devinfo->mask |= CDC_DVD_R;
2631 if (!CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2632 devinfo->mask |= CDC_DVD_RAM;
2633 if (!CDROM_CONFIG_FLAGS(drive)->is_changer)
2634 devinfo->mask |= CDC_SELECT_DISC;
2635 if (!CDROM_CONFIG_FLAGS(drive)->audio_play)
2636 devinfo->mask |= CDC_PLAY_AUDIO;
2637 if (!CDROM_CONFIG_FLAGS(drive)->close_tray)
2638 devinfo->mask |= CDC_CLOSE_TRAY;
2639 if (!CDROM_CONFIG_FLAGS(drive)->mo_drive)
2640 devinfo->mask |= CDC_MO_DRIVE;
c0400dc5
AB
2641 if (!CDROM_CONFIG_FLAGS(drive)->ram)
2642 devinfo->mask |= CDC_RAM;
1da177e4 2643
3cbd814e
BZ
2644 if (CDROM_CONFIG_FLAGS(drive)->no_speed_select)
2645 devinfo->mask |= CDC_SELECT_SPEED;
2646
1da177e4
LT
2647 devinfo->disk = info->disk;
2648 return register_cdrom(devinfo);
2649}
2650
1da177e4
LT
2651static
2652int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2653{
2654 struct cdrom_info *info = drive->driver_data;
2655 struct cdrom_device_info *cdi = &info->devinfo;
2656 struct atapi_capabilities_page cap;
2657 int nslots = 1;
2658
2659 if (drive->media == ide_optical) {
2660 CDROM_CONFIG_FLAGS(drive)->mo_drive = 1;
2661 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2662 printk(KERN_ERR "%s: ATAPI magneto-optical drive\n", drive->name);
2663 return nslots;
2664 }
2665
2666 if (CDROM_CONFIG_FLAGS(drive)->nec260 ||
2667 !strcmp(drive->id->model,"STINGRAY 8422 IDE 8X CD-ROM 7-27-95")) {
2668 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2669 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2670 return nslots;
2671 }
2672
2673 /*
2674 * we have to cheat a little here. the packet will eventually
2675 * be queued with ide_cdrom_packet(), which extracts the
2676 * drive from cdi->handle. Since this device hasn't been
2677 * registered with the Uniform layer yet, it can't do this.
2678 * Same goes for cdi->ops.
2679 */
2a91f3e5 2680 cdi->handle = drive;
1da177e4
LT
2681 cdi->ops = &ide_cdrom_dops;
2682
2683 if (ide_cdrom_get_capabilities(drive, &cap))
2684 return 0;
2685
2686 if (cap.lock == 0)
2687 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2688 if (cap.eject)
2689 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2690 if (cap.cd_r_write)
2691 CDROM_CONFIG_FLAGS(drive)->cd_r = 1;
2692 if (cap.cd_rw_write) {
2693 CDROM_CONFIG_FLAGS(drive)->cd_rw = 1;
2694 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2695 }
2696 if (cap.test_write)
2697 CDROM_CONFIG_FLAGS(drive)->test_write = 1;
2698 if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2699 CDROM_CONFIG_FLAGS(drive)->dvd = 1;
2700 if (cap.dvd_ram_write) {
2701 CDROM_CONFIG_FLAGS(drive)->dvd_ram = 1;
2702 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2703 }
2704 if (cap.dvd_r_write)
2705 CDROM_CONFIG_FLAGS(drive)->dvd_r = 1;
2706 if (cap.audio_play)
2707 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2708 if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2709 CDROM_CONFIG_FLAGS(drive)->close_tray = 0;
2710
2711 /* Some drives used by Apple don't advertise audio play
2712 * but they do support reading TOC & audio datas
2713 */
2714 if (strcmp(drive->id->model, "MATSHITADVD-ROM SR-8187") == 0 ||
2715 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8186") == 0 ||
2716 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8176") == 0 ||
2717 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8174") == 0)
2718 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2719
2720#if ! STANDARD_ATAPI
2721 if (cdi->sanyo_slot > 0) {
2722 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2723 nslots = 3;
2724 }
2725
2726 else
2727#endif /* not STANDARD_ATAPI */
2728 if (cap.mechtype == mechtype_individual_changer ||
2729 cap.mechtype == mechtype_cartridge_changer) {
2730 if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2731 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2732 CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 1;
2733 }
2734 }
2735
9235e68b 2736 ide_cdrom_update_speed(drive, &cap);
1da177e4
LT
2737 /* don't print speed if the drive reported 0.
2738 */
2739 printk(KERN_INFO "%s: ATAPI", drive->name);
2740 if (CDROM_CONFIG_FLAGS(drive)->max_speed)
2741 printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
2742 printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
2743
2744 if (CDROM_CONFIG_FLAGS(drive)->dvd_r|CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2745 printk(" DVD%s%s",
2746 (CDROM_CONFIG_FLAGS(drive)->dvd_r)? "-R" : "",
2747 (CDROM_CONFIG_FLAGS(drive)->dvd_ram)? "-RAM" : "");
2748
2749 if (CDROM_CONFIG_FLAGS(drive)->cd_r|CDROM_CONFIG_FLAGS(drive)->cd_rw)
2750 printk(" CD%s%s",
2751 (CDROM_CONFIG_FLAGS(drive)->cd_r)? "-R" : "",
2752 (CDROM_CONFIG_FLAGS(drive)->cd_rw)? "/RW" : "");
2753
2754 if (CDROM_CONFIG_FLAGS(drive)->is_changer)
2755 printk(" changer w/%d slots", nslots);
2756 else
2757 printk(" drive");
2758
3ab7efe8 2759 printk(KERN_CONT ", %dkB Cache\n", be16_to_cpu(cap.buffer_size));
1da177e4
LT
2760
2761 return nslots;
2762}
2763
7662d046 2764#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
2765static void ide_cdrom_add_settings(ide_drive_t *drive)
2766{
1497943e 2767 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
1da177e4 2768}
7662d046
BZ
2769#else
2770static inline void ide_cdrom_add_settings(ide_drive_t *drive) { ; }
2771#endif
1da177e4
LT
2772
2773/*
2774 * standard prep_rq_fn that builds 10 byte cmds
2775 */
165125e1 2776static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
1da177e4
LT
2777{
2778 int hard_sect = queue_hardsect_size(q);
2779 long block = (long)rq->hard_sector / (hard_sect >> 9);
2780 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
2781
2782 memset(rq->cmd, 0, sizeof(rq->cmd));
2783
2784 if (rq_data_dir(rq) == READ)
2785 rq->cmd[0] = GPCMD_READ_10;
2786 else
2787 rq->cmd[0] = GPCMD_WRITE_10;
2788
2789 /*
2790 * fill in lba
2791 */
2792 rq->cmd[2] = (block >> 24) & 0xff;
2793 rq->cmd[3] = (block >> 16) & 0xff;
2794 rq->cmd[4] = (block >> 8) & 0xff;
2795 rq->cmd[5] = block & 0xff;
2796
2797 /*
2798 * and transfer length
2799 */
2800 rq->cmd[7] = (blocks >> 8) & 0xff;
2801 rq->cmd[8] = blocks & 0xff;
2802 rq->cmd_len = 10;
2803 return BLKPREP_OK;
2804}
2805
2806/*
2807 * Most of the SCSI commands are supported directly by ATAPI devices.
2808 * This transform handles the few exceptions.
2809 */
2810static int ide_cdrom_prep_pc(struct request *rq)
2811{
2812 u8 *c = rq->cmd;
2813
2814 /*
2815 * Transform 6-byte read/write commands to the 10-byte version
2816 */
2817 if (c[0] == READ_6 || c[0] == WRITE_6) {
2818 c[8] = c[4];
2819 c[5] = c[3];
2820 c[4] = c[2];
2821 c[3] = c[1] & 0x1f;
2822 c[2] = 0;
2823 c[1] &= 0xe0;
2824 c[0] += (READ_10 - READ_6);
2825 rq->cmd_len = 10;
2826 return BLKPREP_OK;
2827 }
2828
2829 /*
2830 * it's silly to pretend we understand 6-byte sense commands, just
2831 * reject with ILLEGAL_REQUEST and the caller should take the
2832 * appropriate action
2833 */
2834 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
2835 rq->errors = ILLEGAL_REQUEST;
2836 return BLKPREP_KILL;
2837 }
2838
2839 return BLKPREP_OK;
2840}
2841
165125e1 2842static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
1da177e4 2843{
4aff5e23 2844 if (blk_fs_request(rq))
1da177e4 2845 return ide_cdrom_prep_fs(q, rq);
4aff5e23 2846 else if (blk_pc_request(rq))
1da177e4
LT
2847 return ide_cdrom_prep_pc(rq);
2848
2849 return 0;
2850}
2851
2852static
2853int ide_cdrom_setup (ide_drive_t *drive)
2854{
2855 struct cdrom_info *info = drive->driver_data;
2856 struct cdrom_device_info *cdi = &info->devinfo;
2857 int nslots;
2858
2859 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
2860 blk_queue_dma_alignment(drive->queue, 31);
2861 drive->queue->unplug_delay = (1 * HZ) / 1000;
2862 if (!drive->queue->unplug_delay)
2863 drive->queue->unplug_delay = 1;
2864
2865 drive->special.all = 0;
2866
2867 CDROM_STATE_FLAGS(drive)->media_changed = 1;
1da177e4
LT
2868
2869#if NO_DOOR_LOCKING
2870 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
1da177e4 2871#endif
b8d25dee
BZ
2872 if ((drive->id->config & 0x0060) == 0x20)
2873 CDROM_CONFIG_FLAGS(drive)->drq_interrupt = 1;
1da177e4 2874 CDROM_CONFIG_FLAGS(drive)->no_eject = 1;
1da177e4 2875 CDROM_CONFIG_FLAGS(drive)->close_tray = 1;
b8d25dee 2876
1da177e4 2877 /* limit transfer size per interrupt. */
1da177e4
LT
2878 /* a testament to the nice quality of Samsung drives... */
2879 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
2880 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
2881 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
2882 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
2883 /* the 3231 model does not support the SET_CD_SPEED command */
2884 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
3cbd814e 2885 CDROM_CONFIG_FLAGS(drive)->no_speed_select = 1;
1da177e4
LT
2886
2887#if ! STANDARD_ATAPI
1da177e4
LT
2888 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2889 drive->id->fw_rev[4] == '1' &&
2890 drive->id->fw_rev[6] <= '2') {
2891 /* Vertos 300.
2892 Some versions of this drive like to talk BCD. */
2893 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
2894 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
2895 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
2896 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
2897 }
2898
2899 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2900 drive->id->fw_rev[4] == '1' &&
2901 drive->id->fw_rev[6] <= '2') {
2902 /* Vertos 600 ESD. */
2903 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
2904 }
2905 else if (strcmp(drive->id->model, "NEC CD-ROM DRIVE:260") == 0 &&
2906 strncmp(drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
2907 /* Old NEC260 (not R).
2908 This drive was released before the 1.2 version
2909 of the spec. */
2910 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
2911 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
2912 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
2913 CDROM_CONFIG_FLAGS(drive)->nec260 = 1;
2914 }
2915 else if (strcmp(drive->id->model, "WEARNES CDD-120") == 0 &&
2916 strncmp(drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
2917 /* Wearnes */
2918 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
2919 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
2920 }
b8d25dee
BZ
2921 /*
2922 * Sanyo 3 CD changer uses a non-standard command for CD changing
2923 * (by default standard ATAPI support for CD changers is used).
2924 */
1da177e4
LT
2925 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
2926 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
2927 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
2928 /* uses CD in slot 0 when value is set to 3 */
2929 cdi->sanyo_slot = 3;
2930 }
2931#endif /* not STANDARD_ATAPI */
2932
1da177e4
LT
2933 nslots = ide_cdrom_probe_capabilities (drive);
2934
2935 /*
2936 * set correct block size
2937 */
2938 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
2939
2940 if (drive->autotune == IDE_TUNE_DEFAULT ||
2941 drive->autotune == IDE_TUNE_AUTO)
2942 drive->dsc_overlap = (drive->next != drive);
1da177e4
LT
2943
2944 if (ide_cdrom_register(drive, nslots)) {
2945 printk (KERN_ERR "%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2946 info->devinfo.handle = NULL;
2947 return 1;
2948 }
2949 ide_cdrom_add_settings(drive);
2950 return 0;
2951}
2952
ecfd80e4 2953#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
2954static
2955sector_t ide_cdrom_capacity (ide_drive_t *drive)
2956{
2957 unsigned long capacity, sectors_per_frame;
2958
2959 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
2960 return 0;
2961
2962 return capacity * sectors_per_frame;
2963}
d97b3214 2964#endif
1da177e4 2965
4031bbe4 2966static void ide_cd_remove(ide_drive_t *drive)
1da177e4
LT
2967{
2968 struct cdrom_info *info = drive->driver_data;
2969
7662d046 2970 ide_proc_unregister_driver(drive, info->driver);
1da177e4
LT
2971
2972 del_gendisk(info->disk);
2973
2974 ide_cd_put(info);
1da177e4
LT
2975}
2976
2977static void ide_cd_release(struct kref *kref)
2978{
2979 struct cdrom_info *info = to_ide_cd(kref);
2980 struct cdrom_device_info *devinfo = &info->devinfo;
2981 ide_drive_t *drive = info->drive;
2982 struct gendisk *g = info->disk;
2983
6044ec88
JJ
2984 kfree(info->buffer);
2985 kfree(info->toc);
2986 kfree(info->changer_info);
1da177e4
LT
2987 if (devinfo->handle == drive && unregister_cdrom(devinfo))
2988 printk(KERN_ERR "%s: %s failed to unregister device from the cdrom "
2989 "driver.\n", __FUNCTION__, drive->name);
2990 drive->dsc_overlap = 0;
2991 drive->driver_data = NULL;
2992 blk_queue_prep_rq(drive->queue, NULL);
2993 g->private_data = NULL;
2994 put_disk(g);
2995 kfree(info);
2996}
2997
4031bbe4 2998static int ide_cd_probe(ide_drive_t *);
1da177e4 2999
ecfd80e4 3000#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
3001static int proc_idecd_read_capacity
3002 (char *page, char **start, off_t off, int count, int *eof, void *data)
3003{
2a91f3e5 3004 ide_drive_t *drive = data;
1da177e4
LT
3005 int len;
3006
3007 len = sprintf(page,"%llu\n", (long long)ide_cdrom_capacity(drive));
3008 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
3009}
3010
3011static ide_proc_entry_t idecd_proc[] = {
3012 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
3013 { NULL, 0, NULL, NULL }
3014};
1da177e4
LT
3015#endif
3016
3017static ide_driver_t ide_cdrom_driver = {
8604affd 3018 .gen_driver = {
4ef3b8f4 3019 .owner = THIS_MODULE,
8604affd
BZ
3020 .name = "ide-cdrom",
3021 .bus = &ide_bus_type,
8604affd 3022 },
4031bbe4
RK
3023 .probe = ide_cd_probe,
3024 .remove = ide_cd_remove,
1da177e4
LT
3025 .version = IDECD_VERSION,
3026 .media = ide_cdrom,
1da177e4 3027 .supports_dsc_overlap = 1,
1da177e4
LT
3028 .do_request = ide_do_rw_cdrom,
3029 .end_request = ide_end_request,
3030 .error = __ide_error,
3031 .abort = __ide_abort,
7662d046 3032#ifdef CONFIG_IDE_PROC_FS
1da177e4 3033 .proc = idecd_proc,
7662d046 3034#endif
1da177e4
LT
3035};
3036
3037static int idecd_open(struct inode * inode, struct file * file)
3038{
3039 struct gendisk *disk = inode->i_bdev->bd_disk;
3040 struct cdrom_info *info;
1da177e4
LT
3041 int rc = -ENOMEM;
3042
3043 if (!(info = ide_cd_get(disk)))
3044 return -ENXIO;
3045
1da177e4 3046 if (!info->buffer)
c94964a4
BZ
3047 info->buffer = kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL|__GFP_REPEAT);
3048
3049 if (info->buffer)
3050 rc = cdrom_open(&info->devinfo, inode, file);
1da177e4
LT
3051
3052 if (rc < 0)
3053 ide_cd_put(info);
3054
3055 return rc;
3056}
3057
3058static int idecd_release(struct inode * inode, struct file * file)
3059{
3060 struct gendisk *disk = inode->i_bdev->bd_disk;
3061 struct cdrom_info *info = ide_cd_g(disk);
1da177e4
LT
3062
3063 cdrom_release (&info->devinfo, file);
1da177e4
LT
3064
3065 ide_cd_put(info);
3066
3067 return 0;
3068}
3069
6a2900b6
CH
3070static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
3071{
3072 struct packet_command cgc;
3073 char buffer[16];
3074 int stat;
3075 char spindown;
3076
3077 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
3078 return -EFAULT;
3079
3080 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
3081
3082 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
3083 if (stat)
3084 return stat;
3085
3086 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
3087 return cdrom_mode_select(cdi, &cgc);
3088}
3089
3090static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
3091{
3092 struct packet_command cgc;
3093 char buffer[16];
3094 int stat;
3095 char spindown;
3096
3097 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
3098
3099 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
3100 if (stat)
3101 return stat;
3102
3103 spindown = buffer[11] & 0x0f;
3104 if (copy_to_user((void __user *)arg, &spindown, sizeof (char)))
3105 return -EFAULT;
3106 return 0;
3107}
3108
1da177e4
LT
3109static int idecd_ioctl (struct inode *inode, struct file *file,
3110 unsigned int cmd, unsigned long arg)
3111{
3112 struct block_device *bdev = inode->i_bdev;
3113 struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
3114 int err;
3115
6a2900b6
CH
3116 switch (cmd) {
3117 case CDROMSETSPINDOWN:
3118 return idecd_set_spindown(&info->devinfo, arg);
3119 case CDROMGETSPINDOWN:
3120 return idecd_get_spindown(&info->devinfo, arg);
3121 default:
3122 break;
3123 }
3124
3125 err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
1da177e4
LT
3126 if (err == -EINVAL)
3127 err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
3128
3129 return err;
3130}
3131
3132static int idecd_media_changed(struct gendisk *disk)
3133{
3134 struct cdrom_info *info = ide_cd_g(disk);
3135 return cdrom_media_changed(&info->devinfo);
3136}
3137
3138static int idecd_revalidate_disk(struct gendisk *disk)
3139{
3140 struct cdrom_info *info = ide_cd_g(disk);
3141 struct request_sense sense;
3142 cdrom_read_toc(info->drive, &sense);
3143 return 0;
3144}
3145
3146static struct block_device_operations idecd_ops = {
3147 .owner = THIS_MODULE,
3148 .open = idecd_open,
3149 .release = idecd_release,
3150 .ioctl = idecd_ioctl,
3151 .media_changed = idecd_media_changed,
3152 .revalidate_disk= idecd_revalidate_disk
3153};
3154
3155/* options */
3156static char *ignore = NULL;
3157
3158module_param(ignore, charp, 0400);
3159MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3160
4031bbe4 3161static int ide_cd_probe(ide_drive_t *drive)
1da177e4
LT
3162{
3163 struct cdrom_info *info;
3164 struct gendisk *g;
3165 struct request_sense sense;
3166
3167 if (!strstr("ide-cdrom", drive->driver_req))
3168 goto failed;
3169 if (!drive->present)
3170 goto failed;
3171 if (drive->media != ide_cdrom && drive->media != ide_optical)
3172 goto failed;
3173 /* skip drives that we were told to ignore */
3174 if (ignore != NULL) {
3175 if (strstr(ignore, drive->name)) {
3176 printk(KERN_INFO "ide-cd: ignoring drive %s\n", drive->name);
3177 goto failed;
3178 }
3179 }
3180 if (drive->scsi) {
3181 printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
3182 goto failed;
3183 }
f5e3c2fa 3184 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
1da177e4
LT
3185 if (info == NULL) {
3186 printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
3187 goto failed;
3188 }
3189
3190 g = alloc_disk(1 << PARTN_BITS);
3191 if (!g)
3192 goto out_free_cd;
3193
3194 ide_init_disk(g, drive);
3195
7662d046 3196 ide_proc_register_driver(drive, &ide_cdrom_driver);
8604affd 3197
1da177e4
LT
3198 kref_init(&info->kref);
3199
3200 info->drive = drive;
3201 info->driver = &ide_cdrom_driver;
3202 info->disk = g;
3203
3204 g->private_data = &info->driver;
3205
3206 drive->driver_data = info;
3207
1da177e4 3208 g->minors = 1;
1da177e4
LT
3209 g->driverfs_dev = &drive->gendev;
3210 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
3211 if (ide_cdrom_setup(drive)) {
7662d046 3212 ide_proc_unregister_driver(drive, &ide_cdrom_driver);
05017db3 3213 ide_cd_release(&info->kref);
1da177e4
LT
3214 goto failed;
3215 }
1da177e4
LT
3216
3217 cdrom_read_toc(drive, &sense);
3218 g->fops = &idecd_ops;
3219 g->flags |= GENHD_FL_REMOVABLE;
3220 add_disk(g);
3221 return 0;
3222
1da177e4
LT
3223out_free_cd:
3224 kfree(info);
3225failed:
8604affd 3226 return -ENODEV;
1da177e4
LT
3227}
3228
3229static void __exit ide_cdrom_exit(void)
3230{
8604affd 3231 driver_unregister(&ide_cdrom_driver.gen_driver);
1da177e4 3232}
17514e8a
BZ
3233
3234static int __init ide_cdrom_init(void)
1da177e4 3235{
8604affd 3236 return driver_register(&ide_cdrom_driver.gen_driver);
1da177e4
LT
3237}
3238
263756ec 3239MODULE_ALIAS("ide:*m-cdrom*");
1da177e4
LT
3240module_init(ide_cdrom_init);
3241module_exit(ide_cdrom_exit);
3242MODULE_LICENSE("GPL");
This page took 0.465415 seconds and 5 git commands to generate.