2 * linux/drivers/ide/ide-cd.c
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>
8 * May be copied or modified under the terms of the GNU General Public
9 * License. See linux/COPYING for more information.
11 * ATAPI CD-ROM driver. To be used with ide.c.
12 * See Documentation/cdrom/ide-cd for usage information.
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
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
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...
26 * ----------------------------------
28 * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
31 * For historical changelog please see:
32 * Documentation/ide/ChangeLog.ide-cd.1994-2004
35 #define IDECD_VERSION "4.61"
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>
48 #include <linux/mutex.h>
49 #include <linux/bcd.h>
51 #include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
55 #include <asm/byteorder.h>
56 #include <asm/uaccess.h>
57 #include <asm/unaligned.h>
61 static DEFINE_MUTEX(idecd_ref_mutex);
63 #define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref)
65 #define ide_cd_g(disk) \
66 container_of((disk)->private_data, struct cdrom_info, driver)
68 static struct cdrom_info *ide_cd_get(struct gendisk *disk)
70 struct cdrom_info *cd = NULL;
72 mutex_lock(&idecd_ref_mutex);
76 mutex_unlock(&idecd_ref_mutex);
80 static void ide_cd_release(struct kref *);
82 static void ide_cd_put(struct cdrom_info *cd)
84 mutex_lock(&idecd_ref_mutex);
85 kref_put(&cd->kref, ide_cd_release);
86 mutex_unlock(&idecd_ref_mutex);
89 /****************************************************************************
90 * Generic packet command support and error handling routines.
93 /* Mark that we've seen a media change, and invalidate our internal
95 static void cdrom_saw_media_change (ide_drive_t *drive)
97 struct cdrom_info *cd = drive->driver_data;
99 cd->cd_flags |= IDE_CD_FLAG_MEDIA_CHANGED;
100 cd->cd_flags &= ~IDE_CD_FLAG_TOC_VALID;
101 cd->nsectors_buffered = 0;
104 static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
105 struct request_sense *sense)
109 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
112 switch (sense->sense_key) {
113 case NO_SENSE: case RECOVERED_ERROR:
117 * don't care about tray state messages for
118 * e.g. capacity commands or in-progress or
121 if (sense->asc == 0x3a || sense->asc == 0x04)
125 case ILLEGAL_REQUEST:
127 * don't log START_STOP unit with LoEj set, since
128 * we cannot reliably check if drive can auto-close
130 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
136 * Make good and sure we've seen this potential media
137 * change. Some drives (i.e. Creative) fail to present
138 * the correct sense key in the error register.
140 cdrom_saw_media_change(drive);
150 void cdrom_analyze_sense_data(ide_drive_t *drive,
151 struct request *failed_command,
152 struct request_sense *sense)
154 unsigned long sector;
155 unsigned long bio_sectors;
157 struct cdrom_info *info = drive->driver_data;
159 if (!cdrom_log_sense(drive, failed_command, sense))
163 * If a read toc is executed for a CD-R or CD-RW medium where
164 * the first toc has not been recorded yet, it will fail with
165 * 05/24/00 (which is a confusing error)
167 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
168 if (sense->sense_key == 0x05 && sense->asc == 0x24)
171 if (sense->error_code == 0x70) { /* Current Error */
172 switch(sense->sense_key) {
174 case VOLUME_OVERFLOW:
175 case ILLEGAL_REQUEST:
178 if (failed_command == NULL ||
179 !blk_fs_request(failed_command))
181 sector = (sense->information[0] << 24) |
182 (sense->information[1] << 16) |
183 (sense->information[2] << 8) |
184 (sense->information[3]);
186 bio_sectors = bio_sectors(failed_command->bio);
189 if (drive->queue->hardsect_size == 2048)
190 sector <<= 2; /* Device sector size is 2K */
191 sector &= ~(bio_sectors -1);
192 valid = (sector - failed_command->sector) << 9;
196 if (sector < get_capacity(info->disk) &&
197 drive->probed_capacity - sector < 4 * 75) {
198 set_capacity(info->disk, sector);
202 #if VERBOSE_IDE_CD_ERRORS
205 const char *s = "bad sense key!";
208 printk ("ATAPI device %s:\n", drive->name);
209 if (sense->error_code==0x70)
211 else if (sense->error_code==0x71)
212 printk(" Deferred Error: ");
213 else if (sense->error_code == 0x7f)
214 printk(" Vendor-specific Error: ");
216 printk(" Unknown Error Type: ");
218 if (sense->sense_key < ARRAY_SIZE(sense_key_texts))
219 s = sense_key_texts[sense->sense_key];
221 printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
223 if (sense->asc == 0x40) {
224 sprintf(buf, "Diagnostic failure on component 0x%02x",
228 int lo = 0, mid, hi = ARRAY_SIZE(sense_data_texts);
229 unsigned long key = (sense->sense_key << 16);
230 key |= (sense->asc << 8);
231 if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
237 if (sense_data_texts[mid].asc_ascq == key ||
238 sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
239 s = sense_data_texts[mid].text;
242 else if (sense_data_texts[mid].asc_ascq > key)
250 if (sense->asc > 0x80)
251 s = "(vendor-specific error)";
253 s = "(reserved error code)";
256 printk(KERN_ERR " %s -- (asc=0x%02x, ascq=0x%02x)\n",
257 s, sense->asc, sense->ascq);
259 if (failed_command != NULL) {
261 int lo=0, mid, hi= ARRAY_SIZE(packet_command_texts);
266 if (packet_command_texts[mid].packet_command ==
267 failed_command->cmd[0]) {
268 s = packet_command_texts[mid].text;
271 if (packet_command_texts[mid].packet_command >
272 failed_command->cmd[0])
278 printk (KERN_ERR " The failed \"%s\" packet command was: \n \"", s);
279 for (i=0; i<sizeof (failed_command->cmd); i++)
280 printk ("%02x ", failed_command->cmd[i]);
284 /* The SKSV bit specifies validity of the sense_key_specific
285 * in the next two commands. It is bit 7 of the first byte.
286 * In the case of NOT_READY, if SKSV is set the drive can
287 * give us nice ETA readings.
289 if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
290 int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
291 printk(KERN_ERR " Command is %02d%% complete\n", progress / 0xffff);
295 if (sense->sense_key == ILLEGAL_REQUEST &&
296 (sense->sks[0] & 0x80) != 0) {
297 printk(KERN_ERR " Error in %s byte %d",
298 (sense->sks[0] & 0x40) != 0 ?
299 "command packet" : "command data",
300 (sense->sks[1] << 8) + sense->sks[2]);
302 if ((sense->sks[0] & 0x40) != 0)
303 printk (" bit %d", sense->sks[0] & 0x07);
309 #else /* not VERBOSE_IDE_CD_ERRORS */
311 /* Suppress printing unit attention and `in progress of becoming ready'
312 errors when we're not being verbose. */
314 if (sense->sense_key == UNIT_ATTENTION ||
315 (sense->sense_key == NOT_READY && (sense->asc == 4 ||
316 sense->asc == 0x3a)))
319 printk(KERN_ERR "%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
321 sense->error_code, sense->sense_key,
322 sense->asc, sense->ascq);
323 #endif /* not VERBOSE_IDE_CD_ERRORS */
327 * Initialize a ide-cd packet command request
329 static void cdrom_prepare_request(ide_drive_t *drive, struct request *rq)
331 struct cdrom_info *cd = drive->driver_data;
333 ide_init_drive_cmd(rq);
334 rq->cmd_type = REQ_TYPE_ATA_PC;
335 rq->rq_disk = cd->disk;
338 static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
339 struct request *failed_command)
341 struct cdrom_info *info = drive->driver_data;
342 struct request *rq = &info->request_sense_request;
345 sense = &info->sense_data;
347 /* stuff the sense request in front of our current request */
348 cdrom_prepare_request(drive, rq);
351 rq->cmd[0] = GPCMD_REQUEST_SENSE;
352 rq->cmd[4] = rq->data_len = 18;
354 rq->cmd_type = REQ_TYPE_SENSE;
356 /* NOTE! Save the failed command in "rq->buffer" */
357 rq->buffer = (void *) failed_command;
359 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
362 static void cdrom_end_request (ide_drive_t *drive, int uptodate)
364 struct request *rq = HWGROUP(drive)->rq;
365 int nsectors = rq->hard_cur_sectors;
367 if (blk_sense_request(rq) && uptodate) {
369 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
372 struct request *failed = (struct request *) rq->buffer;
373 struct cdrom_info *info = drive->driver_data;
374 void *sense = &info->sense_data;
379 sense = failed->sense;
380 failed->sense_len = rq->sense_len;
382 cdrom_analyze_sense_data(drive, failed, sense);
384 * now end failed request
386 if (blk_fs_request(failed)) {
387 if (ide_end_dequeued_request(drive, failed, 0,
388 failed->hard_nr_sectors))
391 spin_lock_irqsave(&ide_lock, flags);
392 if (__blk_end_request(failed, -EIO,
395 spin_unlock_irqrestore(&ide_lock, flags);
398 cdrom_analyze_sense_data(drive, NULL, sense);
401 if (!rq->current_nr_sectors && blk_fs_request(rq))
403 /* make sure it's fully ended */
404 if (blk_pc_request(rq))
405 nsectors = (rq->data_len + 511) >> 9;
409 ide_end_request(drive, uptodate, nsectors);
412 static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 stat)
416 ide_dump_status(drive, msg, stat);
419 /* Returns 0 if the request should be continued.
420 Returns 1 if the request was ended. */
421 static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
423 struct request *rq = HWGROUP(drive)->rq;
424 int stat, err, sense_key;
426 /* Check for errors. */
427 stat = HWIF(drive)->INB(IDE_STATUS_REG);
431 if (OK_STAT(stat, good_stat, BAD_R_STAT))
434 /* Get the IDE error register. */
435 err = HWIF(drive)->INB(IDE_ERROR_REG);
436 sense_key = err >> 4;
439 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
443 if (blk_sense_request(rq)) {
444 /* We got an error trying to get sense info
445 from the drive (probably while trying
446 to recover from a former error). Just give up. */
448 rq->cmd_flags |= REQ_FAILED;
449 cdrom_end_request(drive, 0);
450 ide_error(drive, "request sense failure", stat);
453 } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
454 /* All other functions, except for READ. */
458 * if we have an error, pass back CHECK_CONDITION as the
461 if (blk_pc_request(rq) && !rq->errors)
462 rq->errors = SAM_STAT_CHECK_CONDITION;
464 /* Check for tray open. */
465 if (sense_key == NOT_READY) {
466 cdrom_saw_media_change (drive);
467 } else if (sense_key == UNIT_ATTENTION) {
468 /* Check for media change. */
469 cdrom_saw_media_change (drive);
470 /*printk("%s: media changed\n",drive->name);*/
472 } else if ((sense_key == ILLEGAL_REQUEST) &&
473 (rq->cmd[0] == GPCMD_START_STOP_UNIT)) {
475 * Don't print error message for this condition--
476 * SFF8090i indicates that 5/24/00 is the correct
477 * response to a request to close the tray if the
478 * drive doesn't have that capability.
479 * cdrom_log_sense() knows this!
481 } else if (!(rq->cmd_flags & REQ_QUIET)) {
482 /* Otherwise, print an error. */
483 ide_dump_status(drive, "packet command error", stat);
486 rq->cmd_flags |= REQ_FAILED;
489 * instead of playing games with moving completions around,
490 * remove failed request completely and end it when the
491 * request sense has completed
493 if (stat & ERR_STAT) {
494 spin_lock_irqsave(&ide_lock, flags);
495 blkdev_dequeue_request(rq);
496 HWGROUP(drive)->rq = NULL;
497 spin_unlock_irqrestore(&ide_lock, flags);
499 cdrom_queue_request_sense(drive, rq->sense, rq);
501 cdrom_end_request(drive, 0);
503 } else if (blk_fs_request(rq)) {
504 int do_end_request = 0;
506 /* Handle errors from READ and WRITE requests. */
508 if (blk_noretry_request(rq))
511 if (sense_key == NOT_READY) {
513 if (rq_data_dir(rq) == READ) {
514 cdrom_saw_media_change (drive);
516 /* Fail the request. */
517 printk ("%s: tray open\n", drive->name);
520 struct cdrom_info *info = drive->driver_data;
522 /* allow the drive 5 seconds to recover, some
523 * devices will return this error while flushing
526 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
528 if (time_after(jiffies, info->write_timeout))
534 * take a breather relying on the
535 * unplug timer to kick us again
537 spin_lock_irqsave(&ide_lock, flags);
538 blk_plug_device(drive->queue);
539 spin_unlock_irqrestore(&ide_lock,flags);
543 } else if (sense_key == UNIT_ATTENTION) {
545 cdrom_saw_media_change (drive);
547 /* Arrange to retry the request.
548 But be sure to give up if we've retried
550 if (++rq->errors > ERROR_MAX)
552 } else if (sense_key == ILLEGAL_REQUEST ||
553 sense_key == DATA_PROTECT) {
554 /* No point in retrying after an illegal
555 request or data protect error.*/
556 ide_dump_status_no_sense (drive, "command error", stat);
558 } else if (sense_key == MEDIUM_ERROR) {
559 /* No point in re-trying a zillion times on a bad
560 * sector... If we got here the error is not correctable */
561 ide_dump_status_no_sense (drive, "media error (bad sector)", stat);
563 } else if (sense_key == BLANK_CHECK) {
564 /* Disk appears blank ?? */
565 ide_dump_status_no_sense (drive, "media error (blank)", stat);
567 } else if ((err & ~ABRT_ERR) != 0) {
568 /* Go to the default handler
570 ide_error(drive, "cdrom_decode_status", stat);
572 } else if ((++rq->errors > ERROR_MAX)) {
573 /* We've racked up too many retries. Abort. */
577 /* End a request through request sense analysis when we have
578 sense data. We need this in order to perform end of media
581 if (do_end_request) {
582 if (stat & ERR_STAT) {
584 spin_lock_irqsave(&ide_lock, flags);
585 blkdev_dequeue_request(rq);
586 HWGROUP(drive)->rq = NULL;
587 spin_unlock_irqrestore(&ide_lock, flags);
589 cdrom_queue_request_sense(drive, rq->sense, rq);
591 cdrom_end_request(drive, 0);
593 /* If we got a CHECK_CONDITION status,
594 queue a request sense command. */
596 cdrom_queue_request_sense(drive, NULL, NULL);
599 blk_dump_rq_flags(rq, "ide-cd: bad rq");
600 cdrom_end_request(drive, 0);
603 /* Retry, or handle the next request. */
607 static int cdrom_timer_expiry(ide_drive_t *drive)
609 struct request *rq = HWGROUP(drive)->rq;
610 unsigned long wait = 0;
613 * Some commands are *slow* and normally take a long time to
614 * complete. Usually we can use the ATAPI "disconnect" to bypass
615 * this, but not all commands/drives support that. Let
616 * ide_timer_expiry keep polling us for these.
618 switch (rq->cmd[0]) {
620 case GPCMD_FORMAT_UNIT:
621 case GPCMD_RESERVE_RZONE_TRACK:
622 case GPCMD_CLOSE_TRACK:
623 case GPCMD_FLUSH_CACHE:
624 wait = ATAPI_WAIT_PC;
627 if (!(rq->cmd_flags & REQ_QUIET))
628 printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
635 /* Set up the device registers for transferring a packet command on DEV,
636 expecting to later transfer XFERLEN bytes. HANDLER is the routine
637 which actually transfers the command to the drive. If this is a
638 drq_interrupt device, this routine will arrange for HANDLER to be
639 called when the interrupt from the drive arrives. Otherwise, HANDLER
640 will be called immediately after the drive is prepared for the transfer. */
642 static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
644 ide_handler_t *handler)
646 ide_startstop_t startstop;
647 struct cdrom_info *info = drive->driver_data;
648 ide_hwif_t *hwif = drive->hwif;
650 /* Wait for the controller to be idle. */
651 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
654 /* FIXME: for Virtual DMA we must check harder */
656 info->dma = !hwif->dma_setup(drive);
658 /* Set up the controller registers. */
659 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL |
660 IDE_TFLAG_NO_SELECT_MASK, xferlen, info->dma);
662 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
663 /* waiting for CDB interrupt, not DMA yet. */
665 drive->waiting_for_dma = 0;
668 ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
674 spin_lock_irqsave(&ide_lock, flags);
675 hwif->OUTBSYNC(drive, WIN_PACKETCMD, IDE_COMMAND_REG);
677 spin_unlock_irqrestore(&ide_lock, flags);
679 return (*handler) (drive);
683 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
684 The device registers must have already been prepared
685 by cdrom_start_packet_command.
686 HANDLER is the interrupt handler to call when the command completes
687 or there's data ready. */
688 #define ATAPI_MIN_CDB_BYTES 12
689 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
691 ide_handler_t *handler)
693 ide_hwif_t *hwif = drive->hwif;
695 struct cdrom_info *info = drive->driver_data;
696 ide_startstop_t startstop;
698 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
699 /* Here we should have been called after receiving an interrupt
700 from the device. DRQ should how be set. */
702 /* Check for errors. */
703 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
706 /* Ok, next interrupt will be DMA interrupt. */
708 drive->waiting_for_dma = 1;
710 /* Otherwise, we must wait for DRQ to get set. */
711 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
712 BUSY_STAT, WAIT_READY))
716 /* Arm the interrupt handler. */
717 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
719 /* ATAPI commands get padded out to 12 bytes minimum */
720 cmd_len = COMMAND_SIZE(rq->cmd[0]);
721 if (cmd_len < ATAPI_MIN_CDB_BYTES)
722 cmd_len = ATAPI_MIN_CDB_BYTES;
724 /* Send the command to the device. */
725 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
727 /* Start the DMA if need be */
729 hwif->dma_start(drive);
734 /****************************************************************************
735 * Block read functions.
738 typedef void (xfer_func_t)(ide_drive_t *, void *, u32);
740 static void ide_cd_pad_transfer(ide_drive_t *drive, xfer_func_t *xf, int len)
744 xf(drive, &dum, sizeof(dum));
750 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
751 * buffer. Once the first sector is added, any subsequent sectors are
752 * assumed to be continuous (until the buffer is cleared). For the first
753 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
754 * the buffer is cleared.)
756 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
757 int sectors_to_transfer)
759 struct cdrom_info *info = drive->driver_data;
761 /* Number of sectors to read into the buffer. */
762 int sectors_to_buffer = min_t(int, sectors_to_transfer,
763 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
764 info->nsectors_buffered);
768 /* If we couldn't get a buffer, don't try to buffer anything... */
769 if (info->buffer == NULL)
770 sectors_to_buffer = 0;
772 /* If this is the first sector in the buffer, remember its number. */
773 if (info->nsectors_buffered == 0)
774 info->sector_buffered = sector;
776 /* Read the data into the buffer. */
777 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
778 while (sectors_to_buffer > 0) {
779 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
781 --sectors_to_transfer;
782 ++info->nsectors_buffered;
786 /* Throw away any remaining data. */
787 while (sectors_to_transfer > 0) {
788 static char dum[SECTOR_SIZE];
789 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
790 --sectors_to_transfer;
795 * Check the contents of the interrupt reason register from the cdrom
796 * and attempt to recover if there are problems. Returns 0 if everything's
797 * ok; nonzero if the request has been terminated.
800 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
804 else if (ireason == 0) {
805 ide_hwif_t *hwif = drive->hwif;
807 /* Whoops... The drive is expecting to receive data from us! */
808 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
809 drive->name, __FUNCTION__);
811 /* Throw some data at the drive so it doesn't hang
812 and quit this request. */
813 ide_cd_pad_transfer(drive, hwif->atapi_output_bytes, len);
814 } else if (ireason == 1) {
815 /* Some drives (ASUS) seem to tell us that status
816 * info is available. just get it and ignore.
818 (void) HWIF(drive)->INB(IDE_STATUS_REG);
821 /* Drive wants a command packet, or invalid ireason... */
822 printk(KERN_ERR "%s: %s: bad interrupt reason 0x%02x\n",
823 drive->name, __FUNCTION__, ireason);
826 cdrom_end_request(drive, 0);
831 * Interrupt routine. Called when a read request has completed.
833 static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
836 int ireason, len, sectors_to_transfer, nskip;
837 struct cdrom_info *info = drive->driver_data;
838 u8 lowcyl = 0, highcyl = 0;
839 int dma = info->dma, dma_error = 0;
841 struct request *rq = HWGROUP(drive)->rq;
848 dma_error = HWIF(drive)->ide_dma_end(drive);
850 printk(KERN_ERR "%s: DMA read error\n", drive->name);
855 if (cdrom_decode_status(drive, 0, &stat))
860 ide_end_request(drive, 1, rq->nr_sectors);
863 return ide_error(drive, "dma error", stat);
866 /* Read the interrupt reason and the transfer length. */
867 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
868 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
869 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
871 len = lowcyl + (256 * highcyl);
873 /* If DRQ is clear, the command has completed. */
874 if ((stat & DRQ_STAT) == 0) {
875 /* If we're not done filling the current buffer, complain.
876 Otherwise, complete the command normally. */
877 if (rq->current_nr_sectors > 0) {
878 printk (KERN_ERR "%s: cdrom_read_intr: data underrun (%d blocks)\n",
879 drive->name, rq->current_nr_sectors);
880 rq->cmd_flags |= REQ_FAILED;
881 cdrom_end_request(drive, 0);
883 cdrom_end_request(drive, 1);
887 /* Check that the drive is expecting to do the same thing we are. */
888 if (cdrom_read_check_ireason (drive, len, ireason))
891 /* Assume that the drive will always provide data in multiples
892 of at least SECTOR_SIZE, as it gets hairy to keep track
893 of the transfers otherwise. */
894 if ((len % SECTOR_SIZE) != 0) {
895 printk (KERN_ERR "%s: cdrom_read_intr: Bad transfer size %d\n",
897 if (info->cd_flags & IDE_CD_FLAG_LIMIT_NFRAMES)
898 printk (KERN_ERR " This drive is not supported by this version of the driver\n");
900 printk (KERN_ERR " Trying to limit transfer sizes\n");
901 info->cd_flags |= IDE_CD_FLAG_LIMIT_NFRAMES;
903 cdrom_end_request(drive, 0);
907 /* The number of sectors we need to read from the drive. */
908 sectors_to_transfer = len / SECTOR_SIZE;
910 /* First, figure out if we need to bit-bucket
911 any of the leading sectors. */
912 nskip = min_t(int, rq->current_nr_sectors - bio_cur_sectors(rq->bio), sectors_to_transfer);
915 /* We need to throw away a sector. */
916 static char dum[SECTOR_SIZE];
917 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
919 --rq->current_nr_sectors;
921 --sectors_to_transfer;
924 /* Now loop while we still have data to read from the drive. */
925 while (sectors_to_transfer > 0) {
928 /* If we've filled the present buffer but there's another
929 chained buffer after it, move on. */
930 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
931 cdrom_end_request(drive, 1);
933 /* If the buffers are full, cache the rest of the data in our
935 if (rq->current_nr_sectors == 0) {
936 cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
937 sectors_to_transfer = 0;
939 /* Transfer data to the buffers.
940 Figure out how many sectors we can transfer
941 to the current buffer. */
942 this_transfer = min_t(int, sectors_to_transfer,
943 rq->current_nr_sectors);
945 /* Read this_transfer sectors
946 into the current buffer. */
947 while (this_transfer > 0) {
948 HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
949 rq->buffer += SECTOR_SIZE;
951 --rq->current_nr_sectors;
954 --sectors_to_transfer;
959 /* Done moving data! Wait for another interrupt. */
960 ide_set_handler(drive, &cdrom_read_intr, ATAPI_WAIT_PC, NULL);
965 * Try to satisfy some of the current read request from our cached data.
966 * Returns nonzero if the request has been completed, zero otherwise.
968 static int cdrom_read_from_buffer (ide_drive_t *drive)
970 struct cdrom_info *info = drive->driver_data;
971 struct request *rq = HWGROUP(drive)->rq;
972 unsigned short sectors_per_frame;
974 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
976 /* Can't do anything if there's no buffer. */
977 if (info->buffer == NULL) return 0;
979 /* Loop while this request needs data and the next block is present
981 while (rq->nr_sectors > 0 &&
982 rq->sector >= info->sector_buffered &&
983 rq->sector < info->sector_buffered + info->nsectors_buffered) {
984 if (rq->current_nr_sectors == 0)
985 cdrom_end_request(drive, 1);
989 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
991 rq->buffer += SECTOR_SIZE;
992 --rq->current_nr_sectors;
997 /* If we've satisfied the current request,
998 terminate it successfully. */
999 if (rq->nr_sectors == 0) {
1000 cdrom_end_request(drive, 1);
1004 /* Move on to the next buffer if needed. */
1005 if (rq->current_nr_sectors == 0)
1006 cdrom_end_request(drive, 1);
1008 /* If this condition does not hold, then the kluge i use to
1009 represent the number of sectors to skip at the start of a transfer
1010 will fail. I think that this will never happen, but let's be
1011 paranoid and check. */
1012 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
1013 (rq->sector & (sectors_per_frame - 1))) {
1014 printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1015 drive->name, (long)rq->sector);
1016 cdrom_end_request(drive, 0);
1024 * Routine to send a read packet command to the drive.
1025 * This is usually called directly from cdrom_start_read.
1026 * However, for drq_interrupt devices, it is called from an interrupt
1027 * when the drive is ready to accept the command.
1029 static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1031 struct request *rq = HWGROUP(drive)->rq;
1032 unsigned short sectors_per_frame;
1035 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1037 /* If the requested sector doesn't start on a cdrom block boundary,
1038 we must adjust the start of the transfer so that it does,
1039 and remember to skip the first few sectors.
1040 If the CURRENT_NR_SECTORS field is larger than the size
1041 of the buffer, it will mean that we're to skip a number
1042 of sectors equal to the amount by which CURRENT_NR_SECTORS
1043 is larger than the buffer size. */
1044 nskip = rq->sector & (sectors_per_frame - 1);
1046 /* Sanity check... */
1047 if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
1048 (rq->sector & (sectors_per_frame - 1))) {
1049 printk(KERN_ERR "%s: cdrom_start_read_continuation: buffer botch (%u)\n",
1050 drive->name, rq->current_nr_sectors);
1051 cdrom_end_request(drive, 0);
1054 rq->current_nr_sectors += nskip;
1057 /* Set up the command */
1058 rq->timeout = ATAPI_WAIT_PC;
1060 /* Send the command to the drive and return. */
1061 return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
1065 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1066 #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
1067 #define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
1069 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1071 struct cdrom_info *info = drive->driver_data;
1073 static int retry = 10;
1075 if (cdrom_decode_status(drive, 0, &stat))
1078 info->cd_flags |= IDE_CD_FLAG_SEEKING;
1080 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
1083 * this condition is far too common, to bother
1086 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
1087 drive->dsc_overlap = 0;
1093 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1095 struct request *rq = HWGROUP(drive)->rq;
1096 sector_t frame = rq->sector;
1098 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
1100 memset(rq->cmd, 0, sizeof(rq->cmd));
1101 rq->cmd[0] = GPCMD_SEEK;
1102 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
1104 rq->timeout = ATAPI_WAIT_PC;
1105 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
1108 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1110 struct cdrom_info *info = drive->driver_data;
1113 info->start_seek = jiffies;
1114 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
1117 /* Fix up a possibly partially-processed request so that we can
1118 start it over entirely, or even put it back on the request queue. */
1119 static void restore_request (struct request *rq)
1121 if (rq->buffer != bio_data(rq->bio)) {
1122 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
1124 rq->buffer = bio_data(rq->bio);
1125 rq->nr_sectors += n;
1128 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
1129 rq->hard_nr_sectors = rq->nr_sectors;
1130 rq->hard_sector = rq->sector;
1131 rq->q->prep_rq_fn(rq->q, rq);
1135 * Start a read request from the CD-ROM.
1137 static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1139 struct cdrom_info *info = drive->driver_data;
1140 struct request *rq = HWGROUP(drive)->rq;
1141 unsigned short sectors_per_frame;
1143 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1145 /* We may be retrying this request after an error. Fix up
1146 any weirdness which might be present in the request packet. */
1147 restore_request(rq);
1149 /* Satisfy whatever we can of this request from our cached sector. */
1150 if (cdrom_read_from_buffer(drive))
1153 /* Clear the local sector buffer. */
1154 info->nsectors_buffered = 0;
1156 /* use dma, if possible. */
1157 info->dma = drive->using_dma;
1158 if ((rq->sector & (sectors_per_frame - 1)) ||
1159 (rq->nr_sectors & (sectors_per_frame - 1)))
1162 /* Start sending the read request to the drive. */
1163 return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1166 /****************************************************************************
1167 * Execute all other packet commands.
1170 /* Interrupt routine for packet command completion. */
1171 static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1173 struct request *rq = HWGROUP(drive)->rq;
1174 xfer_func_t *xferfunc = NULL;
1175 int stat, ireason, len, thislen, write;
1176 u8 lowcyl = 0, highcyl = 0;
1178 /* Check for errors. */
1179 if (cdrom_decode_status(drive, 0, &stat))
1182 /* Read the interrupt reason and the transfer length. */
1183 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1184 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1185 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1187 len = lowcyl + (256 * highcyl);
1189 /* If DRQ is clear, the command has completed.
1190 Complain if we still have data left to transfer. */
1191 if ((stat & DRQ_STAT) == 0) {
1192 /* Some of the trailing request sense fields are optional, and
1193 some drives don't send them. Sigh. */
1194 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
1196 rq->data_len <= 5) {
1197 while (rq->data_len > 0) {
1198 *(unsigned char *)rq->data++ = 0;
1203 if (rq->data_len == 0)
1204 cdrom_end_request(drive, 1);
1206 rq->cmd_flags |= REQ_FAILED;
1207 cdrom_end_request(drive, 0);
1212 /* Figure out how much data to transfer. */
1213 thislen = rq->data_len;
1219 xferfunc = HWIF(drive)->atapi_output_bytes;
1220 } else if (ireason == 2) {
1222 xferfunc = HWIF(drive)->atapi_input_bytes;
1227 printk(KERN_ERR "%s: confused, missing data\n",
1229 blk_dump_rq_flags(rq, write ? "cdrom_pc_intr, write"
1230 : "cdrom_pc_intr, read");
1233 /* Transfer the data. */
1234 xferfunc(drive, rq->data, thislen);
1236 /* Keep count of how much data we've moved. */
1238 rq->data += thislen;
1239 rq->data_len -= thislen;
1241 if (write && blk_sense_request(rq))
1242 rq->sense_len += thislen;
1244 printk (KERN_ERR "%s: cdrom_pc_intr: The drive "
1245 "appears confused (ireason = 0x%02x). "
1246 "Trying to recover by ending request.\n",
1247 drive->name, ireason);
1248 rq->cmd_flags |= REQ_FAILED;
1249 cdrom_end_request(drive, 0);
1254 * If we haven't moved enough data to satisfy the drive,
1258 ide_cd_pad_transfer(drive, xferfunc, len);
1260 /* Now we wait for another interrupt. */
1261 ide_set_handler(drive, &cdrom_pc_intr, ATAPI_WAIT_PC, cdrom_timer_expiry);
1265 static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1267 struct request *rq = HWGROUP(drive)->rq;
1270 rq->timeout = ATAPI_WAIT_PC;
1272 /* Send the command to the drive and return. */
1273 return cdrom_transfer_packet_command(drive, rq, &cdrom_pc_intr);
1277 static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1280 struct request *rq = HWGROUP(drive)->rq;
1281 struct cdrom_info *info = drive->driver_data;
1284 rq->cmd_flags &= ~REQ_FAILED;
1287 /* Start sending the command to the drive. */
1288 return cdrom_start_packet_command(drive, len, cdrom_do_pc_continuation);
1292 static int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
1294 struct request_sense sense;
1296 unsigned int flags = rq->cmd_flags;
1298 if (rq->sense == NULL)
1301 /* Start of retry loop. */
1304 unsigned long time = jiffies;
1305 rq->cmd_flags = flags;
1307 error = ide_do_drive_cmd(drive, rq, ide_wait);
1308 time = jiffies - time;
1310 /* FIXME: we should probably abort/retry or something
1311 * in case of failure */
1312 if (rq->cmd_flags & REQ_FAILED) {
1313 /* The request failed. Retry if it was due to a unit
1315 (usually means media was changed). */
1316 struct request_sense *reqbuf = rq->sense;
1318 if (reqbuf->sense_key == UNIT_ATTENTION)
1319 cdrom_saw_media_change(drive);
1320 else if (reqbuf->sense_key == NOT_READY &&
1321 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1322 /* The drive is in the process of loading
1323 a disk. Retry, but wait a little to give
1324 the drive time to complete the load. */
1327 /* Otherwise, don't retry. */
1333 /* End of retry loop. */
1334 } while ((rq->cmd_flags & REQ_FAILED) && retries >= 0);
1336 /* Return an error if the command failed. */
1337 return (rq->cmd_flags & REQ_FAILED) ? -EIO : 0;
1343 static int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1345 /* Two notes about IDE interrupt reason here - 0 means that
1346 * the drive wants to receive data from us, 2 means that
1347 * the drive is expecting to transfer data to us.
1351 else if (ireason == 2) {
1352 ide_hwif_t *hwif = drive->hwif;
1354 /* Whoops... The drive wants to send data. */
1355 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
1356 drive->name, __FUNCTION__);
1358 ide_cd_pad_transfer(drive, hwif->atapi_input_bytes, len);
1360 /* Drive wants a command packet, or invalid ireason... */
1361 printk(KERN_ERR "%s: %s: bad interrupt reason 0x%02x\n",
1362 drive->name, __FUNCTION__, ireason);
1365 cdrom_end_request(drive, 0);
1370 * Called from blk_end_request_callback() after the data of the request
1371 * is completed and before the request is completed.
1372 * By returning value '1', blk_end_request_callback() returns immediately
1373 * without completing the request.
1375 static int cdrom_newpc_intr_dummy_cb(struct request *rq)
1381 * best way to deal with dma that is not sector aligned right now... note
1382 * that in this path we are not using ->data or ->buffer at all. this irs
1383 * can replace cdrom_pc_intr, cdrom_read_intr, and cdrom_write_intr in the
1386 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1388 struct cdrom_info *info = drive->driver_data;
1389 struct request *rq = HWGROUP(drive)->rq;
1390 int dma_error, dma, stat, ireason, len, thislen;
1392 xfer_func_t *xferfunc;
1393 unsigned long flags;
1395 /* Check for errors. */
1400 dma_error = HWIF(drive)->ide_dma_end(drive);
1402 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
1403 rq_data_dir(rq) ? "write" : "read");
1408 if (cdrom_decode_status(drive, 0, &stat))
1412 * using dma, transfer is complete now
1416 return ide_error(drive, "dma error", stat);
1418 spin_lock_irqsave(&ide_lock, flags);
1419 if (__blk_end_request(rq, 0, rq->data_len))
1421 HWGROUP(drive)->rq = NULL;
1422 spin_unlock_irqrestore(&ide_lock, flags);
1428 * ok we fall to pio :/
1430 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1431 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1432 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1434 len = lowcyl + (256 * highcyl);
1435 thislen = rq->data_len;
1440 * If DRQ is clear, the command has completed.
1442 if ((stat & DRQ_STAT) == 0) {
1443 spin_lock_irqsave(&ide_lock, flags);
1444 if (__blk_end_request(rq, 0, rq->data_len))
1446 HWGROUP(drive)->rq = NULL;
1447 spin_unlock_irqrestore(&ide_lock, flags);
1453 * check which way to transfer data
1455 if (rq_data_dir(rq) == WRITE) {
1459 if (cdrom_write_check_ireason(drive, len, ireason))
1462 xferfunc = HWIF(drive)->atapi_output_bytes;
1467 if (cdrom_read_check_ireason(drive, len, ireason))
1470 xferfunc = HWIF(drive)->atapi_input_bytes;
1476 while (thislen > 0) {
1477 int blen = blen = rq->data_len;
1478 char *ptr = rq->data;
1484 ptr = bio_data(rq->bio);
1485 blen = bio_iovec(rq->bio)->bv_len;
1489 printk(KERN_ERR "%s: confused, missing data\n", drive->name);
1496 xferfunc(drive, ptr, blen);
1500 rq->data_len -= blen;
1504 * The request can't be completed until DRQ is cleared.
1505 * So complete the data, but don't complete the request
1506 * using the dummy function for the callback feature
1507 * of blk_end_request_callback().
1509 blk_end_request_callback(rq, 0, blen,
1510 cdrom_newpc_intr_dummy_cb);
1519 ide_cd_pad_transfer(drive, xferfunc, len);
1521 BUG_ON(HWGROUP(drive)->handler != NULL);
1523 ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, NULL);
1527 static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1529 int stat, ireason, len, sectors_to_transfer, uptodate;
1530 struct cdrom_info *info = drive->driver_data;
1531 int dma_error = 0, dma = info->dma;
1532 u8 lowcyl = 0, highcyl = 0;
1534 struct request *rq = HWGROUP(drive)->rq;
1536 /* Check for errors. */
1539 dma_error = HWIF(drive)->ide_dma_end(drive);
1541 printk(KERN_ERR "%s: DMA write error\n", drive->name);
1546 if (cdrom_decode_status(drive, 0, &stat))
1550 * using dma, transfer is complete now
1554 return ide_error(drive, "dma error", stat);
1556 ide_end_request(drive, 1, rq->nr_sectors);
1560 /* Read the interrupt reason and the transfer length. */
1561 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1562 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1563 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1565 len = lowcyl + (256 * highcyl);
1567 /* If DRQ is clear, the command has completed. */
1568 if ((stat & DRQ_STAT) == 0) {
1569 /* If we're not done writing, complain.
1570 * Otherwise, complete the command normally.
1573 if (rq->current_nr_sectors > 0) {
1574 printk(KERN_ERR "%s: %s: data underrun (%d blocks)\n",
1575 drive->name, __FUNCTION__,
1576 rq->current_nr_sectors);
1579 cdrom_end_request(drive, uptodate);
1583 /* Check that the drive is expecting to do the same thing we are. */
1584 if (cdrom_write_check_ireason(drive, len, ireason))
1587 sectors_to_transfer = len / SECTOR_SIZE;
1590 * now loop and write out the data
1592 while (sectors_to_transfer > 0) {
1595 if (!rq->current_nr_sectors) {
1596 printk(KERN_ERR "%s: %s: confused, missing data\n",
1597 drive->name, __FUNCTION__);
1602 * Figure out how many sectors we can transfer
1604 this_transfer = min_t(int, sectors_to_transfer, rq->current_nr_sectors);
1606 while (this_transfer > 0) {
1607 HWIF(drive)->atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
1608 rq->buffer += SECTOR_SIZE;
1610 --rq->current_nr_sectors;
1613 --sectors_to_transfer;
1617 * current buffer complete, move on
1619 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1620 cdrom_end_request(drive, 1);
1623 /* re-arm handler */
1624 ide_set_handler(drive, &cdrom_write_intr, ATAPI_WAIT_PC, NULL);
1628 static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1630 struct request *rq = HWGROUP(drive)->rq;
1632 #if 0 /* the immediate bit */
1633 rq->cmd[1] = 1 << 3;
1635 rq->timeout = ATAPI_WAIT_PC;
1637 return cdrom_transfer_packet_command(drive, rq, cdrom_write_intr);
1640 static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
1642 struct cdrom_info *info = drive->driver_data;
1643 struct gendisk *g = info->disk;
1644 unsigned short sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1647 * writes *must* be hardware frame aligned
1649 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1650 (rq->sector & (sectors_per_frame - 1))) {
1651 cdrom_end_request(drive, 0);
1656 * disk has become write protected
1659 cdrom_end_request(drive, 0);
1663 info->nsectors_buffered = 0;
1665 /* use dma, if possible. we don't need to check more, since we
1666 * know that the transfer is always (at least!) frame aligned */
1667 info->dma = drive->using_dma ? 1 : 0;
1669 info->devinfo.media_written = 1;
1671 /* Start sending the write request to the drive. */
1672 return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1675 static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1677 struct request *rq = HWGROUP(drive)->rq;
1680 rq->timeout = ATAPI_WAIT_PC;
1682 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1685 static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1687 struct cdrom_info *info = drive->driver_data;
1689 rq->cmd_flags |= REQ_QUIET;
1697 int mask = drive->queue->dma_alignment;
1698 unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1700 info->dma = drive->using_dma;
1703 * check if dma is safe
1705 * NOTE! The "len" and "addr" checks should possibly have
1708 if ((rq->data_len & 15) || (addr & mask))
1712 /* Start sending the command to the drive. */
1713 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1716 /****************************************************************************
1717 * cdrom driver request routine.
1719 static ide_startstop_t
1720 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1722 ide_startstop_t action;
1723 struct cdrom_info *info = drive->driver_data;
1725 if (blk_fs_request(rq)) {
1726 if (info->cd_flags & IDE_CD_FLAG_SEEKING) {
1727 unsigned long elapsed = jiffies - info->start_seek;
1728 int stat = HWIF(drive)->INB(IDE_STATUS_REG);
1730 if ((stat & SEEK_STAT) != SEEK_STAT) {
1731 if (elapsed < IDECD_SEEK_TIMEOUT) {
1732 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1735 printk (KERN_ERR "%s: DSC timeout\n", drive->name);
1737 info->cd_flags &= ~IDE_CD_FLAG_SEEKING;
1739 if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
1740 action = cdrom_start_seek(drive, block);
1742 if (rq_data_dir(rq) == READ)
1743 action = cdrom_start_read(drive, block);
1745 action = cdrom_start_write(drive, rq);
1747 info->last_block = block;
1749 } else if (rq->cmd_type == REQ_TYPE_SENSE ||
1750 rq->cmd_type == REQ_TYPE_ATA_PC) {
1751 return cdrom_do_packet_command(drive);
1752 } else if (blk_pc_request(rq)) {
1753 return cdrom_do_block_pc(drive, rq);
1754 } else if (blk_special_request(rq)) {
1756 * right now this can only be a reset...
1758 cdrom_end_request(drive, 1);
1762 blk_dump_rq_flags(rq, "ide-cd bad flags");
1763 cdrom_end_request(drive, 0);
1769 /****************************************************************************
1772 * Routines which queue packet commands take as a final argument a pointer
1773 * to a request_sense struct. If execution of the command results
1774 * in an error with a CHECK CONDITION status, this structure will be filled
1775 * with the results of the subsequent request sense command. The pointer
1776 * can also be NULL, in which case no sense information is returned.
1780 void msf_from_bcd (struct atapi_msf *msf)
1782 msf->minute = BCD2BIN(msf->minute);
1783 msf->second = BCD2BIN(msf->second);
1784 msf->frame = BCD2BIN(msf->frame);
1788 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1790 lba += CD_MSF_OFFSET;
1791 lba &= 0xffffff; /* negative lbas use only 24 bits */
1792 *m = lba / (CD_SECS * CD_FRAMES);
1793 lba %= (CD_SECS * CD_FRAMES);
1794 *s = lba / CD_FRAMES;
1795 *f = lba % CD_FRAMES;
1800 int msf_to_lba (byte m, byte s, byte f)
1802 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1805 static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1808 struct cdrom_info *info = drive->driver_data;
1809 struct cdrom_device_info *cdi = &info->devinfo;
1811 cdrom_prepare_request(drive, &req);
1814 req.cmd[0] = GPCMD_TEST_UNIT_READY;
1815 req.cmd_flags |= REQ_QUIET;
1818 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1819 * switch CDs instead of supporting the LOAD_UNLOAD opcode.
1821 req.cmd[7] = cdi->sanyo_slot % 3;
1823 return cdrom_queue_packet_command(drive, &req);
1827 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1829 cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
1831 struct cdrom_info *cd = drive->driver_data;
1832 struct request_sense my_sense;
1839 /* If the drive cannot lock the door, just pretend. */
1840 if (cd->cd_flags & IDE_CD_FLAG_NO_DOORLOCK) {
1843 cdrom_prepare_request(drive, &req);
1845 req.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1846 req.cmd[4] = lockflag ? 1 : 0;
1847 stat = cdrom_queue_packet_command(drive, &req);
1850 /* If we got an illegal field error, the drive
1851 probably cannot lock the door. */
1853 sense->sense_key == ILLEGAL_REQUEST &&
1854 (sense->asc == 0x24 || sense->asc == 0x20)) {
1855 printk (KERN_ERR "%s: door locking not supported\n",
1857 cd->cd_flags |= IDE_CD_FLAG_NO_DOORLOCK;
1861 /* no medium, that's alright. */
1862 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
1867 cd->cd_flags |= IDE_CD_FLAG_DOOR_LOCKED;
1869 cd->cd_flags &= ~IDE_CD_FLAG_DOOR_LOCKED;
1876 /* Eject the disk if EJECTFLAG is 0.
1877 If EJECTFLAG is 1, try to reload the disk. */
1878 static int cdrom_eject(ide_drive_t *drive, int ejectflag,
1879 struct request_sense *sense)
1881 struct cdrom_info *cd = drive->driver_data;
1882 struct cdrom_device_info *cdi = &cd->devinfo;
1886 if ((cd->cd_flags & IDE_CD_FLAG_NO_EJECT) && !ejectflag)
1887 return -EDRIVE_CANT_DO_THIS;
1889 /* reload fails on some drives, if the tray is locked */
1890 if ((cd->cd_flags & IDE_CD_FLAG_DOOR_LOCKED) && ejectflag)
1893 cdrom_prepare_request(drive, &req);
1895 /* only tell drive to close tray if open, if it can do that */
1896 if (ejectflag && (cdi->mask & CDC_CLOSE_TRAY))
1900 req.cmd[0] = GPCMD_START_STOP_UNIT;
1901 req.cmd[4] = loej | (ejectflag != 0);
1902 return cdrom_queue_packet_command(drive, &req);
1905 static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1906 unsigned long *sectors_per_frame,
1907 struct request_sense *sense)
1917 cdrom_prepare_request(drive, &req);
1920 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
1921 req.data = (char *)&capbuf;
1922 req.data_len = sizeof(capbuf);
1923 req.cmd_flags |= REQ_QUIET;
1925 stat = cdrom_queue_packet_command(drive, &req);
1927 *capacity = 1 + be32_to_cpu(capbuf.lba);
1928 *sectors_per_frame =
1929 be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
1935 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1936 int format, char *buf, int buflen,
1937 struct request_sense *sense)
1941 cdrom_prepare_request(drive, &req);
1945 req.data_len = buflen;
1946 req.cmd_flags |= REQ_QUIET;
1947 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1948 req.cmd[6] = trackno;
1949 req.cmd[7] = (buflen >> 8);
1950 req.cmd[8] = (buflen & 0xff);
1951 req.cmd[9] = (format << 6);
1956 return cdrom_queue_packet_command(drive, &req);
1960 /* Try to read the entire TOC for the disk into our internal buffer. */
1961 static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
1963 int stat, ntracks, i;
1964 struct cdrom_info *info = drive->driver_data;
1965 struct cdrom_device_info *cdi = &info->devinfo;
1966 struct atapi_toc *toc = info->toc;
1968 struct atapi_toc_header hdr;
1969 struct atapi_toc_entry ent;
1972 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1975 /* Try to allocate space. */
1976 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
1978 printk (KERN_ERR "%s: No cdrom TOC buffer!\n", drive->name);
1984 /* Check to see if the existing data is still valid.
1985 If it is, just return. */
1986 (void) cdrom_check_status(drive, sense);
1988 if (info->cd_flags & IDE_CD_FLAG_TOC_VALID)
1991 /* Try to get the total cdrom capacity and sector size. */
1992 stat = cdrom_read_capacity(drive, &toc->capacity, §ors_per_frame,
1995 toc->capacity = 0x1fffff;
1997 set_capacity(info->disk, toc->capacity * sectors_per_frame);
1998 /* Save a private copy of te TOC capacity for error handling */
1999 drive->probed_capacity = toc->capacity * sectors_per_frame;
2001 blk_queue_hardsect_size(drive->queue,
2002 sectors_per_frame << SECTOR_BITS);
2004 /* First read just the header, so we know how long the TOC is. */
2005 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
2006 sizeof(struct atapi_toc_header), sense);
2010 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
2011 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
2012 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
2015 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2018 if (ntracks > MAX_TRACKS)
2019 ntracks = MAX_TRACKS;
2021 /* Now read the whole schmeer. */
2022 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
2024 sizeof(struct atapi_toc_header) +
2026 sizeof(struct atapi_toc_entry), sense);
2028 if (stat && toc->hdr.first_track > 1) {
2029 /* Cds with CDI tracks only don't have any TOC entries,
2030 despite of this the returned values are
2031 first_track == last_track = number of CDI tracks + 1,
2032 so that this case is indistinguishable from the same
2033 layout plus an additional audio track.
2034 If we get an error for the regular case, we assume
2035 a CDI without additional audio tracks. In this case
2036 the readable TOC is empty (CDI tracks are not included)
2037 and only holds the Leadout entry. Heiko Eißfeldt */
2039 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
2041 sizeof(struct atapi_toc_header) +
2043 sizeof(struct atapi_toc_entry),
2048 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
2049 toc->hdr.first_track = (u8)BIN2BCD(CDROM_LEADOUT);
2050 toc->hdr.last_track = (u8)BIN2BCD(CDROM_LEADOUT);
2052 toc->hdr.first_track = CDROM_LEADOUT;
2053 toc->hdr.last_track = CDROM_LEADOUT;
2060 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
2062 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
2063 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
2064 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
2067 for (i = 0; i <= ntracks; i++) {
2068 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
2069 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD)
2070 toc->ent[i].track = BCD2BIN(toc->ent[i].track);
2071 msf_from_bcd(&toc->ent[i].addr.msf);
2073 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
2074 toc->ent[i].addr.msf.second,
2075 toc->ent[i].addr.msf.frame);
2078 /* Read the multisession information. */
2079 if (toc->hdr.first_track != CDROM_LEADOUT) {
2080 /* Read the multisession information. */
2081 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
2082 sizeof(ms_tmp), sense);
2086 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
2088 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
2089 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
2092 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
2093 /* Re-read multisession information using MSF format */
2094 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
2095 sizeof(ms_tmp), sense);
2099 msf_from_bcd (&ms_tmp.ent.addr.msf);
2100 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
2101 ms_tmp.ent.addr.msf.second,
2102 ms_tmp.ent.addr.msf.frame);
2105 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
2107 /* Now try to get the total cdrom capacity. */
2108 stat = cdrom_get_last_written(cdi, &last_written);
2109 if (!stat && (last_written > toc->capacity)) {
2110 toc->capacity = last_written;
2111 set_capacity(info->disk, toc->capacity * sectors_per_frame);
2112 drive->probed_capacity = toc->capacity * sectors_per_frame;
2115 /* Remember that we've read this stuff. */
2116 info->cd_flags |= IDE_CD_FLAG_TOC_VALID;
2122 static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2123 int buflen, struct request_sense *sense)
2127 cdrom_prepare_request(drive, &req);
2131 req.data_len = buflen;
2132 req.cmd[0] = GPCMD_READ_SUBCHANNEL;
2133 req.cmd[1] = 2; /* MSF addressing */
2134 req.cmd[2] = 0x40; /* request subQ data */
2135 req.cmd[3] = format;
2136 req.cmd[7] = (buflen >> 8);
2137 req.cmd[8] = (buflen & 0xff);
2138 return cdrom_queue_packet_command(drive, &req);
2141 /* ATAPI cdrom drives are free to select the speed you request or any slower
2142 rate :-( Requesting too fast a speed will _not_ produce an error. */
2143 static int cdrom_select_speed(ide_drive_t *drive, int speed,
2144 struct request_sense *sense)
2146 struct cdrom_info *cd = drive->driver_data;
2147 struct cdrom_device_info *cdi = &cd->devinfo;
2149 cdrom_prepare_request(drive, &req);
2153 speed = 0xffff; /* set to max */
2155 speed *= 177; /* Nx to kbytes/s */
2157 req.cmd[0] = GPCMD_SET_SPEED;
2158 /* Read Drive speed in kbytes/second MSB */
2159 req.cmd[2] = (speed >> 8) & 0xff;
2160 /* Read Drive speed in kbytes/second LSB */
2161 req.cmd[3] = speed & 0xff;
2162 if ((cdi->mask & (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) !=
2163 (CDC_CD_R | CDC_CD_RW | CDC_DVD_R)) {
2164 /* Write Drive speed in kbytes/second MSB */
2165 req.cmd[4] = (speed >> 8) & 0xff;
2166 /* Write Drive speed in kbytes/second LSB */
2167 req.cmd[5] = speed & 0xff;
2170 return cdrom_queue_packet_command(drive, &req);
2173 static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2175 struct request_sense sense;
2178 cdrom_prepare_request(drive, &req);
2181 req.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2182 lba_to_msf(lba_start, &req.cmd[3], &req.cmd[4], &req.cmd[5]);
2183 lba_to_msf(lba_end-1, &req.cmd[6], &req.cmd[7], &req.cmd[8]);
2185 return cdrom_queue_packet_command(drive, &req);
2188 static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2189 struct atapi_toc_entry **ent)
2191 struct cdrom_info *info = drive->driver_data;
2192 struct atapi_toc *toc = info->toc;
2196 * don't serve cached data, if the toc isn't valid
2198 if ((info->cd_flags & IDE_CD_FLAG_TOC_VALID) == 0)
2201 /* Check validity of requested track number. */
2202 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2203 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2204 if (track == CDROM_LEADOUT)
2205 *ent = &toc->ent[ntracks];
2206 else if (track < toc->hdr.first_track ||
2207 track > toc->hdr.last_track)
2210 *ent = &toc->ent[track - toc->hdr.first_track];
2215 /* the generic packet interface to cdrom.c */
2216 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2217 struct packet_command *cgc)
2220 ide_drive_t *drive = cdi->handle;
2222 if (cgc->timeout <= 0)
2223 cgc->timeout = ATAPI_WAIT_PC;
2225 /* here we queue the commands from the uniform CD-ROM
2226 layer. the packet must be complete, as we do not
2228 cdrom_prepare_request(drive, &req);
2229 memcpy(req.cmd, cgc->cmd, CDROM_PACKET_SIZE);
2231 memset(cgc->sense, 0, sizeof(struct request_sense));
2232 req.data = cgc->buffer;
2233 req.data_len = cgc->buflen;
2234 req.timeout = cgc->timeout;
2237 req.cmd_flags |= REQ_QUIET;
2239 req.sense = cgc->sense;
2240 cgc->stat = cdrom_queue_packet_command(drive, &req);
2242 cgc->buflen -= req.data_len;
2247 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2248 unsigned int cmd, void *arg)
2251 ide_drive_t *drive = cdi->handle;
2252 struct cdrom_info *info = drive->driver_data;
2257 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2258 * atapi doesn't support it
2260 case CDROMPLAYTRKIND: {
2261 unsigned long lba_start, lba_end;
2262 struct cdrom_ti *ti = arg;
2263 struct atapi_toc_entry *first_toc, *last_toc;
2265 stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2269 stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2273 if (ti->cdti_trk1 != CDROM_LEADOUT)
2275 lba_start = first_toc->addr.lba;
2276 lba_end = last_toc->addr.lba;
2278 if (lba_end <= lba_start)
2281 return cdrom_play_audio(drive, lba_start, lba_end);
2284 case CDROMREADTOCHDR: {
2285 struct cdrom_tochdr *tochdr = arg;
2286 struct atapi_toc *toc;
2288 /* Make sure our saved TOC is valid. */
2289 stat = cdrom_read_toc(drive, NULL);
2294 tochdr->cdth_trk0 = toc->hdr.first_track;
2295 tochdr->cdth_trk1 = toc->hdr.last_track;
2300 case CDROMREADTOCENTRY: {
2301 struct cdrom_tocentry *tocentry = arg;
2302 struct atapi_toc_entry *toce;
2304 stat = cdrom_get_toc_entry(drive, tocentry->cdte_track, &toce);
2308 tocentry->cdte_ctrl = toce->control;
2309 tocentry->cdte_adr = toce->adr;
2310 if (tocentry->cdte_format == CDROM_MSF) {
2311 lba_to_msf (toce->addr.lba,
2312 &tocentry->cdte_addr.msf.minute,
2313 &tocentry->cdte_addr.msf.second,
2314 &tocentry->cdte_addr.msf.frame);
2316 tocentry->cdte_addr.lba = toce->addr.lba;
2327 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2329 ide_drive_t *drive = cdi->handle;
2330 struct cdrom_info *cd = drive->driver_data;
2331 struct request_sense sense;
2335 cdrom_prepare_request(drive, &req);
2336 req.cmd_type = REQ_TYPE_SPECIAL;
2337 req.cmd_flags = REQ_QUIET;
2338 ret = ide_do_drive_cmd(drive, &req, ide_wait);
2341 * A reset will unlock the door. If it was previously locked,
2344 if (cd->cd_flags & IDE_CD_FLAG_DOOR_LOCKED)
2345 (void) cdrom_lockdoor(drive, 1, &sense);
2352 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2354 ide_drive_t *drive = cdi->handle;
2355 struct request_sense sense;
2358 int stat = cdrom_lockdoor(drive, 0, &sense);
2363 return cdrom_eject(drive, !position, &sense);
2367 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2369 ide_drive_t *drive = cdi->handle;
2370 return cdrom_lockdoor(drive, lock, NULL);
2373 static int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
2375 struct cdrom_info *info = drive->driver_data;
2376 struct cdrom_device_info *cdi = &info->devinfo;
2377 struct packet_command cgc;
2378 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
2380 if ((info->cd_flags & IDE_CD_FLAG_FULL_CAPS_PAGE) == 0)
2381 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
2383 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
2384 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2385 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2388 } while (--attempts);
2392 static void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
2394 struct cdrom_info *cd = drive->driver_data;
2395 u16 curspeed, maxspeed;
2397 curspeed = *(u16 *)&buf[8 + 14];
2398 maxspeed = *(u16 *)&buf[8 + 8];
2400 if (cd->cd_flags & IDE_CD_FLAG_LE_SPEED_FIELDS) {
2401 curspeed = le16_to_cpu(curspeed);
2402 maxspeed = le16_to_cpu(maxspeed);
2404 curspeed = be16_to_cpu(curspeed);
2405 maxspeed = be16_to_cpu(maxspeed);
2408 cd->current_speed = (curspeed + (176/2)) / 176;
2409 cd->max_speed = (maxspeed + (176/2)) / 176;
2413 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2415 ide_drive_t *drive = cdi->handle;
2416 struct cdrom_info *cd = drive->driver_data;
2417 struct request_sense sense;
2418 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
2421 if ((stat = cdrom_select_speed(drive, speed, &sense)) < 0)
2424 if (!ide_cdrom_get_capabilities(drive, buf)) {
2425 ide_cdrom_update_speed(drive, buf);
2426 cdi->speed = cd->current_speed;
2432 * add logic to try GET_EVENT command first to check for media and tray
2433 * status. this should be supported by newer cd-r/w and all DVD etc
2437 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2439 ide_drive_t *drive = cdi->handle;
2440 struct media_event_desc med;
2441 struct request_sense sense;
2444 if (slot_nr != CDSL_CURRENT)
2447 stat = cdrom_check_status(drive, &sense);
2448 if (!stat || sense.sense_key == UNIT_ATTENTION)
2451 if (!cdrom_get_media_event(cdi, &med)) {
2452 if (med.media_present)
2454 else if (med.door_open)
2455 return CDS_TRAY_OPEN;
2460 if (sense.sense_key == NOT_READY && sense.asc == 0x04 && sense.ascq == 0x04)
2464 * If not using Mt Fuji extended media tray reports,
2465 * just return TRAY_OPEN since ATAPI doesn't provide
2466 * any other way to detect this...
2468 if (sense.sense_key == NOT_READY) {
2469 if (sense.asc == 0x3a && sense.ascq == 1)
2472 return CDS_TRAY_OPEN;
2474 return CDS_DRIVE_NOT_READY;
2478 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2479 struct cdrom_multisession *ms_info)
2481 struct atapi_toc *toc;
2482 ide_drive_t *drive = cdi->handle;
2483 struct cdrom_info *info = drive->driver_data;
2484 struct request_sense sense;
2487 if ((info->cd_flags & IDE_CD_FLAG_TOC_VALID) == 0 || info->toc == NULL)
2488 if ((ret = cdrom_read_toc(drive, &sense)))
2492 ms_info->addr.lba = toc->last_session_lba;
2493 ms_info->xa_flag = toc->xa_flag;
2499 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2500 struct cdrom_mcn *mcn_info)
2504 ide_drive_t *drive = cdi->handle;
2507 if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2510 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2511 sizeof (mcn_info->medium_catalog_number)-1);
2512 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2520 /****************************************************************************
2521 * Other driver requests (open, close, check media change).
2525 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2528 ide_drive_t *drive = cdi->handle;
2529 struct cdrom_info *cd = drive->driver_data;
2532 if (slot_nr == CDSL_CURRENT) {
2533 (void) cdrom_check_status(drive, NULL);
2534 retval = (cd->cd_flags & IDE_CD_FLAG_MEDIA_CHANGED) ? 1 : 0;
2535 cd->cd_flags &= ~IDE_CD_FLAG_MEDIA_CHANGED;
2544 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2550 * Close down the device. Invalidate all cached blocks.
2554 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2556 ide_drive_t *drive = cdi->handle;
2557 struct cdrom_info *cd = drive->driver_data;
2559 if (!cdi->use_count)
2560 cd->cd_flags &= ~IDE_CD_FLAG_TOC_VALID;
2563 #define IDE_CD_CAPABILITIES \
2564 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
2565 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
2566 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
2567 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
2568 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
2570 static struct cdrom_device_ops ide_cdrom_dops = {
2571 .open = ide_cdrom_open_real,
2572 .release = ide_cdrom_release_real,
2573 .drive_status = ide_cdrom_drive_status,
2574 .media_changed = ide_cdrom_check_media_change_real,
2575 .tray_move = ide_cdrom_tray_move,
2576 .lock_door = ide_cdrom_lock_door,
2577 .select_speed = ide_cdrom_select_speed,
2578 .get_last_session = ide_cdrom_get_last_session,
2579 .get_mcn = ide_cdrom_get_mcn,
2580 .reset = ide_cdrom_reset,
2581 .audio_ioctl = ide_cdrom_audio_ioctl,
2582 .capability = IDE_CD_CAPABILITIES,
2583 .generic_packet = ide_cdrom_packet,
2586 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2588 struct cdrom_info *info = drive->driver_data;
2589 struct cdrom_device_info *devinfo = &info->devinfo;
2591 devinfo->ops = &ide_cdrom_dops;
2592 devinfo->speed = info->current_speed;
2593 devinfo->capacity = nslots;
2594 devinfo->handle = drive;
2595 strcpy(devinfo->name, drive->name);
2597 if (info->cd_flags & IDE_CD_FLAG_NO_SPEED_SELECT)
2598 devinfo->mask |= CDC_SELECT_SPEED;
2600 devinfo->disk = info->disk;
2601 return register_cdrom(devinfo);
2605 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2607 struct cdrom_info *cd = drive->driver_data;
2608 struct cdrom_device_info *cdi = &cd->devinfo;
2609 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
2610 mechtype_t mechtype;
2613 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
2614 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
2615 CDC_MO_DRIVE | CDC_RAM);
2617 if (drive->media == ide_optical) {
2618 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
2619 printk(KERN_ERR "%s: ATAPI magneto-optical drive\n", drive->name);
2623 if (cd->cd_flags & IDE_CD_FLAG_PRE_ATAPI12) {
2624 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
2625 cdi->mask &= ~CDC_PLAY_AUDIO;
2630 * we have to cheat a little here. the packet will eventually
2631 * be queued with ide_cdrom_packet(), which extracts the
2632 * drive from cdi->handle. Since this device hasn't been
2633 * registered with the Uniform layer yet, it can't do this.
2634 * Same goes for cdi->ops.
2636 cdi->handle = drive;
2637 cdi->ops = &ide_cdrom_dops;
2639 if (ide_cdrom_get_capabilities(drive, buf))
2642 if ((buf[8 + 6] & 0x01) == 0)
2643 cd->cd_flags |= IDE_CD_FLAG_NO_DOORLOCK;
2644 if (buf[8 + 6] & 0x08)
2645 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
2646 if (buf[8 + 3] & 0x01)
2647 cdi->mask &= ~CDC_CD_R;
2648 if (buf[8 + 3] & 0x02)
2649 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
2650 if (buf[8 + 2] & 0x38)
2651 cdi->mask &= ~CDC_DVD;
2652 if (buf[8 + 3] & 0x20)
2653 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
2654 if (buf[8 + 3] & 0x10)
2655 cdi->mask &= ~CDC_DVD_R;
2656 if ((buf[8 + 4] & 0x01) || (cd->cd_flags & IDE_CD_FLAG_PLAY_AUDIO_OK))
2657 cdi->mask &= ~CDC_PLAY_AUDIO;
2659 mechtype = buf[8 + 6] >> 5;
2660 if (mechtype == mechtype_caddy || mechtype == mechtype_popup)
2661 cdi->mask |= CDC_CLOSE_TRAY;
2663 if (cdi->sanyo_slot > 0) {
2664 cdi->mask &= ~CDC_SELECT_DISC;
2666 } else if (mechtype == mechtype_individual_changer ||
2667 mechtype == mechtype_cartridge_changer) {
2668 nslots = cdrom_number_of_slots(cdi);
2670 cdi->mask &= ~CDC_SELECT_DISC;
2673 ide_cdrom_update_speed(drive, buf);
2675 printk(KERN_INFO "%s: ATAPI", drive->name);
2677 /* don't print speed if the drive reported 0 */
2679 printk(KERN_CONT " %dX", cd->max_speed);
2681 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
2683 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
2684 printk(KERN_CONT " DVD%s%s",
2685 (cdi->mask & CDC_DVD_R) ? "" : "-R",
2686 (cdi->mask & CDC_DVD_RAM) ? "" : "-RAM");
2688 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
2689 printk(KERN_CONT " CD%s%s",
2690 (cdi->mask & CDC_CD_R) ? "" : "-R",
2691 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
2693 if ((cdi->mask & CDC_SELECT_DISC) == 0)
2694 printk(KERN_CONT " changer w/%d slots", nslots);
2696 printk(KERN_CONT " drive");
2698 printk(KERN_CONT ", %dkB Cache\n", be16_to_cpu(*(u16 *)&buf[8 + 12]));
2703 #ifdef CONFIG_IDE_PROC_FS
2704 static void ide_cdrom_add_settings(ide_drive_t *drive)
2706 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2709 static inline void ide_cdrom_add_settings(ide_drive_t *drive) { ; }
2713 * standard prep_rq_fn that builds 10 byte cmds
2715 static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
2717 int hard_sect = queue_hardsect_size(q);
2718 long block = (long)rq->hard_sector / (hard_sect >> 9);
2719 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
2721 memset(rq->cmd, 0, sizeof(rq->cmd));
2723 if (rq_data_dir(rq) == READ)
2724 rq->cmd[0] = GPCMD_READ_10;
2726 rq->cmd[0] = GPCMD_WRITE_10;
2731 rq->cmd[2] = (block >> 24) & 0xff;
2732 rq->cmd[3] = (block >> 16) & 0xff;
2733 rq->cmd[4] = (block >> 8) & 0xff;
2734 rq->cmd[5] = block & 0xff;
2737 * and transfer length
2739 rq->cmd[7] = (blocks >> 8) & 0xff;
2740 rq->cmd[8] = blocks & 0xff;
2746 * Most of the SCSI commands are supported directly by ATAPI devices.
2747 * This transform handles the few exceptions.
2749 static int ide_cdrom_prep_pc(struct request *rq)
2754 * Transform 6-byte read/write commands to the 10-byte version
2756 if (c[0] == READ_6 || c[0] == WRITE_6) {
2763 c[0] += (READ_10 - READ_6);
2769 * it's silly to pretend we understand 6-byte sense commands, just
2770 * reject with ILLEGAL_REQUEST and the caller should take the
2771 * appropriate action
2773 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
2774 rq->errors = ILLEGAL_REQUEST;
2775 return BLKPREP_KILL;
2781 static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
2783 if (blk_fs_request(rq))
2784 return ide_cdrom_prep_fs(q, rq);
2785 else if (blk_pc_request(rq))
2786 return ide_cdrom_prep_pc(rq);
2791 struct cd_list_entry {
2792 const char *id_model;
2793 const char *id_firmware;
2794 unsigned int cd_flags;
2797 static const struct cd_list_entry ide_cd_quirks_list[] = {
2798 /* Limit transfer size per interrupt. */
2799 { "SAMSUNG CD-ROM SCR-2430", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
2800 { "SAMSUNG CD-ROM SCR-2432", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
2801 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
2802 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_CD_FLAG_NO_SPEED_SELECT },
2803 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
2804 { "NEC CD-ROM DRIVE:260", "1.01", IDE_CD_FLAG_TOCADDR_AS_BCD |
2805 IDE_CD_FLAG_PRE_ATAPI12, },
2806 /* Vertos 300, some versions of this drive like to talk BCD. */
2807 { "V003S0DS", NULL, IDE_CD_FLAG_VERTOS_300_SSD, },
2808 /* Vertos 600 ESD. */
2809 { "V006E0DS", NULL, IDE_CD_FLAG_VERTOS_600_ESD, },
2811 * Sanyo 3 CD changer uses a non-standard command for CD changing
2812 * (by default standard ATAPI support for CD changers is used).
2814 { "CD-ROM CDR-C3 G", NULL, IDE_CD_FLAG_SANYO_3CD },
2815 { "CD-ROM CDR-C3G", NULL, IDE_CD_FLAG_SANYO_3CD },
2816 { "CD-ROM CDR_C36", NULL, IDE_CD_FLAG_SANYO_3CD },
2817 /* Stingray 8X CD-ROM. */
2818 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_CD_FLAG_PRE_ATAPI12},
2820 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
2821 * mode sense page capabilities size, but older drives break.
2823 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
2824 { "WPI CDS-32X", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
2825 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
2826 { "", "241N", IDE_CD_FLAG_LE_SPEED_FIELDS },
2828 * Some drives used by Apple don't advertise audio play
2829 * but they do support reading TOC & audio datas.
2831 { "MATSHITADVD-ROM SR-8187", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
2832 { "MATSHITADVD-ROM SR-8186", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
2833 { "MATSHITADVD-ROM SR-8176", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
2834 { "MATSHITADVD-ROM SR-8174", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
2838 static unsigned int ide_cd_flags(struct hd_driveid *id)
2840 const struct cd_list_entry *cle = ide_cd_quirks_list;
2842 while (cle->id_model) {
2843 if (strcmp(cle->id_model, id->model) == 0 &&
2844 (cle->id_firmware == NULL ||
2845 strstr(id->fw_rev, cle->id_firmware)))
2846 return cle->cd_flags;
2854 int ide_cdrom_setup (ide_drive_t *drive)
2856 struct cdrom_info *cd = drive->driver_data;
2857 struct cdrom_device_info *cdi = &cd->devinfo;
2858 struct hd_driveid *id = drive->id;
2861 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
2862 blk_queue_dma_alignment(drive->queue, 31);
2863 drive->queue->unplug_delay = (1 * HZ) / 1000;
2864 if (!drive->queue->unplug_delay)
2865 drive->queue->unplug_delay = 1;
2867 drive->special.all = 0;
2869 cd->cd_flags = IDE_CD_FLAG_MEDIA_CHANGED | IDE_CD_FLAG_NO_EJECT |
2872 if ((id->config & 0x0060) == 0x20)
2873 cd->cd_flags |= IDE_CD_FLAG_DRQ_INTERRUPT;
2875 if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_300_SSD) &&
2876 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
2877 cd->cd_flags |= (IDE_CD_FLAG_TOCTRACKS_AS_BCD |
2878 IDE_CD_FLAG_TOCADDR_AS_BCD);
2879 else if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_600_ESD) &&
2880 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
2881 cd->cd_flags |= IDE_CD_FLAG_TOCTRACKS_AS_BCD;
2882 else if (cd->cd_flags & IDE_CD_FLAG_SANYO_3CD)
2883 cdi->sanyo_slot = 3; /* 3 => use CD in slot 0 */
2885 nslots = ide_cdrom_probe_capabilities (drive);
2888 * set correct block size
2890 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
2892 if (drive->autotune == IDE_TUNE_DEFAULT ||
2893 drive->autotune == IDE_TUNE_AUTO)
2894 drive->dsc_overlap = (drive->next != drive);
2896 if (ide_cdrom_register(drive, nslots)) {
2897 printk (KERN_ERR "%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2898 cd->devinfo.handle = NULL;
2901 ide_cdrom_add_settings(drive);
2905 #ifdef CONFIG_IDE_PROC_FS
2907 sector_t ide_cdrom_capacity (ide_drive_t *drive)
2909 unsigned long capacity, sectors_per_frame;
2911 if (cdrom_read_capacity(drive, &capacity, §ors_per_frame, NULL))
2914 return capacity * sectors_per_frame;
2918 static void ide_cd_remove(ide_drive_t *drive)
2920 struct cdrom_info *info = drive->driver_data;
2922 ide_proc_unregister_driver(drive, info->driver);
2924 del_gendisk(info->disk);
2929 static void ide_cd_release(struct kref *kref)
2931 struct cdrom_info *info = to_ide_cd(kref);
2932 struct cdrom_device_info *devinfo = &info->devinfo;
2933 ide_drive_t *drive = info->drive;
2934 struct gendisk *g = info->disk;
2936 kfree(info->buffer);
2938 if (devinfo->handle == drive && unregister_cdrom(devinfo))
2939 printk(KERN_ERR "%s: %s failed to unregister device from the cdrom "
2940 "driver.\n", __FUNCTION__, drive->name);
2941 drive->dsc_overlap = 0;
2942 drive->driver_data = NULL;
2943 blk_queue_prep_rq(drive->queue, NULL);
2944 g->private_data = NULL;
2949 static int ide_cd_probe(ide_drive_t *);
2951 #ifdef CONFIG_IDE_PROC_FS
2952 static int proc_idecd_read_capacity
2953 (char *page, char **start, off_t off, int count, int *eof, void *data)
2955 ide_drive_t *drive = data;
2958 len = sprintf(page,"%llu\n", (long long)ide_cdrom_capacity(drive));
2959 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
2962 static ide_proc_entry_t idecd_proc[] = {
2963 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
2964 { NULL, 0, NULL, NULL }
2968 static ide_driver_t ide_cdrom_driver = {
2970 .owner = THIS_MODULE,
2971 .name = "ide-cdrom",
2972 .bus = &ide_bus_type,
2974 .probe = ide_cd_probe,
2975 .remove = ide_cd_remove,
2976 .version = IDECD_VERSION,
2978 .supports_dsc_overlap = 1,
2979 .do_request = ide_do_rw_cdrom,
2980 .end_request = ide_end_request,
2981 .error = __ide_error,
2982 .abort = __ide_abort,
2983 #ifdef CONFIG_IDE_PROC_FS
2988 static int idecd_open(struct inode * inode, struct file * file)
2990 struct gendisk *disk = inode->i_bdev->bd_disk;
2991 struct cdrom_info *info;
2994 if (!(info = ide_cd_get(disk)))
2998 info->buffer = kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL|__GFP_REPEAT);
3001 rc = cdrom_open(&info->devinfo, inode, file);
3009 static int idecd_release(struct inode * inode, struct file * file)
3011 struct gendisk *disk = inode->i_bdev->bd_disk;
3012 struct cdrom_info *info = ide_cd_g(disk);
3014 cdrom_release (&info->devinfo, file);
3021 static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
3023 struct packet_command cgc;
3028 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
3031 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
3033 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
3037 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
3038 return cdrom_mode_select(cdi, &cgc);
3041 static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
3043 struct packet_command cgc;
3048 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
3050 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
3054 spindown = buffer[11] & 0x0f;
3055 if (copy_to_user((void __user *)arg, &spindown, sizeof (char)))
3060 static int idecd_ioctl (struct inode *inode, struct file *file,
3061 unsigned int cmd, unsigned long arg)
3063 struct block_device *bdev = inode->i_bdev;
3064 struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
3068 case CDROMSETSPINDOWN:
3069 return idecd_set_spindown(&info->devinfo, arg);
3070 case CDROMGETSPINDOWN:
3071 return idecd_get_spindown(&info->devinfo, arg);
3076 err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
3078 err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
3083 static int idecd_media_changed(struct gendisk *disk)
3085 struct cdrom_info *info = ide_cd_g(disk);
3086 return cdrom_media_changed(&info->devinfo);
3089 static int idecd_revalidate_disk(struct gendisk *disk)
3091 struct cdrom_info *info = ide_cd_g(disk);
3092 struct request_sense sense;
3093 cdrom_read_toc(info->drive, &sense);
3097 static struct block_device_operations idecd_ops = {
3098 .owner = THIS_MODULE,
3100 .release = idecd_release,
3101 .ioctl = idecd_ioctl,
3102 .media_changed = idecd_media_changed,
3103 .revalidate_disk= idecd_revalidate_disk
3107 static char *ignore = NULL;
3109 module_param(ignore, charp, 0400);
3110 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3112 static int ide_cd_probe(ide_drive_t *drive)
3114 struct cdrom_info *info;
3116 struct request_sense sense;
3118 if (!strstr("ide-cdrom", drive->driver_req))
3120 if (!drive->present)
3122 if (drive->media != ide_cdrom && drive->media != ide_optical)
3124 /* skip drives that we were told to ignore */
3125 if (ignore != NULL) {
3126 if (strstr(ignore, drive->name)) {
3127 printk(KERN_INFO "ide-cd: ignoring drive %s\n", drive->name);
3132 printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
3135 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
3137 printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
3141 g = alloc_disk(1 << PARTN_BITS);
3145 ide_init_disk(g, drive);
3147 ide_proc_register_driver(drive, &ide_cdrom_driver);
3149 kref_init(&info->kref);
3151 info->drive = drive;
3152 info->driver = &ide_cdrom_driver;
3155 g->private_data = &info->driver;
3157 drive->driver_data = info;
3160 g->driverfs_dev = &drive->gendev;
3161 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
3162 if (ide_cdrom_setup(drive)) {
3163 ide_proc_unregister_driver(drive, &ide_cdrom_driver);
3164 ide_cd_release(&info->kref);
3168 cdrom_read_toc(drive, &sense);
3169 g->fops = &idecd_ops;
3170 g->flags |= GENHD_FL_REMOVABLE;
3180 static void __exit ide_cdrom_exit(void)
3182 driver_unregister(&ide_cdrom_driver.gen_driver);
3185 static int __init ide_cdrom_init(void)
3187 return driver_register(&ide_cdrom_driver.gen_driver);
3190 MODULE_ALIAS("ide:*m-cdrom*");
3191 module_init(ide_cdrom_init);
3192 module_exit(ide_cdrom_exit);
3193 MODULE_LICENSE("GPL");