Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / ide / ide-cd.c
blobd178ef7c68a06de1d3b3512895ef3533702a4476
1 /*
2 * ATAPI CD-ROM driver.
4 * Copyright (C) 1994-1996 Scott Snyder <snyder@fnald0.fnal.gov>
5 * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
6 * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
7 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
9 * May be copied or modified under the terms of the GNU General Public
10 * License. See linux/COPYING for more information.
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
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
22 * For historical changelog please see:
23 * Documentation/ide/ChangeLog.ide-cd.1994-2004
26 #define IDECD_VERSION "5.00"
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/timer.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/errno.h>
36 #include <linux/cdrom.h>
37 #include <linux/ide.h>
38 #include <linux/completion.h>
39 #include <linux/mutex.h>
40 #include <linux/bcd.h>
42 #include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
44 #include <asm/irq.h>
45 #include <asm/io.h>
46 #include <asm/byteorder.h>
47 #include <asm/uaccess.h>
48 #include <asm/unaligned.h>
50 #include "ide-cd.h"
52 static DEFINE_MUTEX(idecd_ref_mutex);
54 #define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref)
56 #define ide_cd_g(disk) \
57 container_of((disk)->private_data, struct cdrom_info, driver)
59 static struct cdrom_info *ide_cd_get(struct gendisk *disk)
61 struct cdrom_info *cd = NULL;
63 mutex_lock(&idecd_ref_mutex);
64 cd = ide_cd_g(disk);
65 if (cd)
66 kref_get(&cd->kref);
67 mutex_unlock(&idecd_ref_mutex);
68 return cd;
71 static void ide_cd_release(struct kref *);
73 static void ide_cd_put(struct cdrom_info *cd)
75 mutex_lock(&idecd_ref_mutex);
76 kref_put(&cd->kref, ide_cd_release);
77 mutex_unlock(&idecd_ref_mutex);
80 /****************************************************************************
81 * Generic packet command support and error handling routines.
84 /* Mark that we've seen a media change, and invalidate our internal
85 buffers. */
86 static void cdrom_saw_media_change (ide_drive_t *drive)
88 struct cdrom_info *cd = drive->driver_data;
90 cd->cd_flags |= IDE_CD_FLAG_MEDIA_CHANGED;
91 cd->cd_flags &= ~IDE_CD_FLAG_TOC_VALID;
92 cd->nsectors_buffered = 0;
95 static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
96 struct request_sense *sense)
98 int log = 0;
100 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
101 return 0;
103 switch (sense->sense_key) {
104 case NO_SENSE: case RECOVERED_ERROR:
105 break;
106 case NOT_READY:
108 * don't care about tray state messages for
109 * e.g. capacity commands or in-progress or
110 * becoming ready
112 if (sense->asc == 0x3a || sense->asc == 0x04)
113 break;
114 log = 1;
115 break;
116 case ILLEGAL_REQUEST:
118 * don't log START_STOP unit with LoEj set, since
119 * we cannot reliably check if drive can auto-close
121 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
122 break;
123 log = 1;
124 break;
125 case UNIT_ATTENTION:
127 * Make good and sure we've seen this potential media
128 * change. Some drives (i.e. Creative) fail to present
129 * the correct sense key in the error register.
131 cdrom_saw_media_change(drive);
132 break;
133 default:
134 log = 1;
135 break;
137 return log;
140 static
141 void cdrom_analyze_sense_data(ide_drive_t *drive,
142 struct request *failed_command,
143 struct request_sense *sense)
145 unsigned long sector;
146 unsigned long bio_sectors;
147 unsigned long valid;
148 struct cdrom_info *info = drive->driver_data;
150 if (!cdrom_log_sense(drive, failed_command, sense))
151 return;
154 * If a read toc is executed for a CD-R or CD-RW medium where
155 * the first toc has not been recorded yet, it will fail with
156 * 05/24/00 (which is a confusing error)
158 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
159 if (sense->sense_key == 0x05 && sense->asc == 0x24)
160 return;
162 if (sense->error_code == 0x70) { /* Current Error */
163 switch(sense->sense_key) {
164 case MEDIUM_ERROR:
165 case VOLUME_OVERFLOW:
166 case ILLEGAL_REQUEST:
167 if (!sense->valid)
168 break;
169 if (failed_command == NULL ||
170 !blk_fs_request(failed_command))
171 break;
172 sector = (sense->information[0] << 24) |
173 (sense->information[1] << 16) |
174 (sense->information[2] << 8) |
175 (sense->information[3]);
177 bio_sectors = bio_sectors(failed_command->bio);
178 if (bio_sectors < 4)
179 bio_sectors = 4;
180 if (drive->queue->hardsect_size == 2048)
181 sector <<= 2; /* Device sector size is 2K */
182 sector &= ~(bio_sectors -1);
183 valid = (sector - failed_command->sector) << 9;
185 if (valid < 0)
186 valid = 0;
187 if (sector < get_capacity(info->disk) &&
188 drive->probed_capacity - sector < 4 * 75) {
189 set_capacity(info->disk, sector);
194 ide_cd_log_error(drive->name, failed_command, sense);
198 * Initialize a ide-cd packet command request
200 void ide_cd_init_rq(ide_drive_t *drive, struct request *rq)
202 struct cdrom_info *cd = drive->driver_data;
204 ide_init_drive_cmd(rq);
205 rq->cmd_type = REQ_TYPE_ATA_PC;
206 rq->rq_disk = cd->disk;
209 static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
210 struct request *failed_command)
212 struct cdrom_info *info = drive->driver_data;
213 struct request *rq = &info->request_sense_request;
215 if (sense == NULL)
216 sense = &info->sense_data;
218 /* stuff the sense request in front of our current request */
219 ide_cd_init_rq(drive, rq);
221 rq->data = sense;
222 rq->cmd[0] = GPCMD_REQUEST_SENSE;
223 rq->cmd[4] = rq->data_len = 18;
225 rq->cmd_type = REQ_TYPE_SENSE;
227 /* NOTE! Save the failed command in "rq->buffer" */
228 rq->buffer = (void *) failed_command;
230 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
233 static void cdrom_end_request (ide_drive_t *drive, int uptodate)
235 struct request *rq = HWGROUP(drive)->rq;
236 int nsectors = rq->hard_cur_sectors;
238 if (blk_sense_request(rq) && uptodate) {
240 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
241 * failed request
243 struct request *failed = (struct request *) rq->buffer;
244 struct cdrom_info *info = drive->driver_data;
245 void *sense = &info->sense_data;
246 unsigned long flags;
248 if (failed) {
249 if (failed->sense) {
250 sense = failed->sense;
251 failed->sense_len = rq->sense_len;
253 cdrom_analyze_sense_data(drive, failed, sense);
255 * now end failed request
257 if (blk_fs_request(failed)) {
258 if (ide_end_dequeued_request(drive, failed, 0,
259 failed->hard_nr_sectors))
260 BUG();
261 } else {
262 spin_lock_irqsave(&ide_lock, flags);
263 if (__blk_end_request(failed, -EIO,
264 failed->data_len))
265 BUG();
266 spin_unlock_irqrestore(&ide_lock, flags);
268 } else
269 cdrom_analyze_sense_data(drive, NULL, sense);
272 if (!rq->current_nr_sectors && blk_fs_request(rq))
273 uptodate = 1;
274 /* make sure it's fully ended */
275 if (blk_pc_request(rq))
276 nsectors = (rq->data_len + 511) >> 9;
277 if (!nsectors)
278 nsectors = 1;
280 ide_end_request(drive, uptodate, nsectors);
283 static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 stat)
285 if (stat & 0x80)
286 return;
287 ide_dump_status(drive, msg, stat);
290 /* Returns 0 if the request should be continued.
291 Returns 1 if the request was ended. */
292 static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
294 struct request *rq = HWGROUP(drive)->rq;
295 int stat, err, sense_key;
297 /* Check for errors. */
298 stat = ide_read_status(drive);
300 if (stat_ret)
301 *stat_ret = stat;
303 if (OK_STAT(stat, good_stat, BAD_R_STAT))
304 return 0;
306 /* Get the IDE error register. */
307 err = ide_read_error(drive);
308 sense_key = err >> 4;
310 if (rq == NULL) {
311 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
312 return 1;
315 if (blk_sense_request(rq)) {
316 /* We got an error trying to get sense info
317 from the drive (probably while trying
318 to recover from a former error). Just give up. */
320 rq->cmd_flags |= REQ_FAILED;
321 cdrom_end_request(drive, 0);
322 ide_error(drive, "request sense failure", stat);
323 return 1;
325 } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
326 /* All other functions, except for READ. */
329 * if we have an error, pass back CHECK_CONDITION as the
330 * scsi status byte
332 if (blk_pc_request(rq) && !rq->errors)
333 rq->errors = SAM_STAT_CHECK_CONDITION;
335 /* Check for tray open. */
336 if (sense_key == NOT_READY) {
337 cdrom_saw_media_change (drive);
338 } else if (sense_key == UNIT_ATTENTION) {
339 /* Check for media change. */
340 cdrom_saw_media_change (drive);
341 /*printk("%s: media changed\n",drive->name);*/
342 return 0;
343 } else if ((sense_key == ILLEGAL_REQUEST) &&
344 (rq->cmd[0] == GPCMD_START_STOP_UNIT)) {
346 * Don't print error message for this condition--
347 * SFF8090i indicates that 5/24/00 is the correct
348 * response to a request to close the tray if the
349 * drive doesn't have that capability.
350 * cdrom_log_sense() knows this!
352 } else if (!(rq->cmd_flags & REQ_QUIET)) {
353 /* Otherwise, print an error. */
354 ide_dump_status(drive, "packet command error", stat);
357 rq->cmd_flags |= REQ_FAILED;
360 * instead of playing games with moving completions around,
361 * remove failed request completely and end it when the
362 * request sense has completed
364 goto end_request;
366 } else if (blk_fs_request(rq)) {
367 int do_end_request = 0;
369 /* Handle errors from READ and WRITE requests. */
371 if (blk_noretry_request(rq))
372 do_end_request = 1;
374 if (sense_key == NOT_READY) {
375 /* Tray open. */
376 if (rq_data_dir(rq) == READ) {
377 cdrom_saw_media_change (drive);
379 /* Fail the request. */
380 printk ("%s: tray open\n", drive->name);
381 do_end_request = 1;
382 } else {
383 struct cdrom_info *info = drive->driver_data;
385 /* allow the drive 5 seconds to recover, some
386 * devices will return this error while flushing
387 * data from cache */
388 if (!rq->errors)
389 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
390 rq->errors = 1;
391 if (time_after(jiffies, info->write_timeout))
392 do_end_request = 1;
393 else {
394 unsigned long flags;
397 * take a breather relying on the
398 * unplug timer to kick us again
400 spin_lock_irqsave(&ide_lock, flags);
401 blk_plug_device(drive->queue);
402 spin_unlock_irqrestore(&ide_lock,flags);
403 return 1;
406 } else if (sense_key == UNIT_ATTENTION) {
407 /* Media change. */
408 cdrom_saw_media_change (drive);
410 /* Arrange to retry the request.
411 But be sure to give up if we've retried
412 too many times. */
413 if (++rq->errors > ERROR_MAX)
414 do_end_request = 1;
415 } else if (sense_key == ILLEGAL_REQUEST ||
416 sense_key == DATA_PROTECT) {
417 /* No point in retrying after an illegal
418 request or data protect error.*/
419 ide_dump_status_no_sense (drive, "command error", stat);
420 do_end_request = 1;
421 } else if (sense_key == MEDIUM_ERROR) {
422 /* No point in re-trying a zillion times on a bad
423 * sector... If we got here the error is not correctable */
424 ide_dump_status_no_sense (drive, "media error (bad sector)", stat);
425 do_end_request = 1;
426 } else if (sense_key == BLANK_CHECK) {
427 /* Disk appears blank ?? */
428 ide_dump_status_no_sense (drive, "media error (blank)", stat);
429 do_end_request = 1;
430 } else if ((err & ~ABRT_ERR) != 0) {
431 /* Go to the default handler
432 for other errors. */
433 ide_error(drive, "cdrom_decode_status", stat);
434 return 1;
435 } else if ((++rq->errors > ERROR_MAX)) {
436 /* We've racked up too many retries. Abort. */
437 do_end_request = 1;
440 /* End a request through request sense analysis when we have
441 sense data. We need this in order to perform end of media
442 processing */
444 if (do_end_request)
445 goto end_request;
448 * If we got a CHECK_CONDITION status,
449 * queue a request sense command.
451 if (stat & ERR_STAT)
452 cdrom_queue_request_sense(drive, NULL, NULL);
453 } else {
454 blk_dump_rq_flags(rq, "ide-cd: bad rq");
455 cdrom_end_request(drive, 0);
458 /* Retry, or handle the next request. */
459 return 1;
461 end_request:
462 if (stat & ERR_STAT) {
463 unsigned long flags;
465 spin_lock_irqsave(&ide_lock, flags);
466 blkdev_dequeue_request(rq);
467 HWGROUP(drive)->rq = NULL;
468 spin_unlock_irqrestore(&ide_lock, flags);
470 cdrom_queue_request_sense(drive, rq->sense, rq);
471 } else
472 cdrom_end_request(drive, 0);
474 return 1;
477 static int cdrom_timer_expiry(ide_drive_t *drive)
479 struct request *rq = HWGROUP(drive)->rq;
480 unsigned long wait = 0;
483 * Some commands are *slow* and normally take a long time to
484 * complete. Usually we can use the ATAPI "disconnect" to bypass
485 * this, but not all commands/drives support that. Let
486 * ide_timer_expiry keep polling us for these.
488 switch (rq->cmd[0]) {
489 case GPCMD_BLANK:
490 case GPCMD_FORMAT_UNIT:
491 case GPCMD_RESERVE_RZONE_TRACK:
492 case GPCMD_CLOSE_TRACK:
493 case GPCMD_FLUSH_CACHE:
494 wait = ATAPI_WAIT_PC;
495 break;
496 default:
497 if (!(rq->cmd_flags & REQ_QUIET))
498 printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
499 wait = 0;
500 break;
502 return wait;
505 /* Set up the device registers for transferring a packet command on DEV,
506 expecting to later transfer XFERLEN bytes. HANDLER is the routine
507 which actually transfers the command to the drive. If this is a
508 drq_interrupt device, this routine will arrange for HANDLER to be
509 called when the interrupt from the drive arrives. Otherwise, HANDLER
510 will be called immediately after the drive is prepared for the transfer. */
512 static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
513 int xferlen,
514 ide_handler_t *handler)
516 ide_startstop_t startstop;
517 struct cdrom_info *info = drive->driver_data;
518 ide_hwif_t *hwif = drive->hwif;
520 /* Wait for the controller to be idle. */
521 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
522 return startstop;
524 /* FIXME: for Virtual DMA we must check harder */
525 if (info->dma)
526 info->dma = !hwif->dma_setup(drive);
528 /* Set up the controller registers. */
529 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL |
530 IDE_TFLAG_NO_SELECT_MASK, xferlen, info->dma);
532 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
533 /* waiting for CDB interrupt, not DMA yet. */
534 if (info->dma)
535 drive->waiting_for_dma = 0;
537 /* packet command */
538 ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
539 return ide_started;
540 } else {
541 unsigned long flags;
543 /* packet command */
544 spin_lock_irqsave(&ide_lock, flags);
545 hwif->OUTBSYNC(drive, WIN_PACKETCMD, IDE_COMMAND_REG);
546 ndelay(400);
547 spin_unlock_irqrestore(&ide_lock, flags);
549 return (*handler) (drive);
553 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
554 The device registers must have already been prepared
555 by cdrom_start_packet_command.
556 HANDLER is the interrupt handler to call when the command completes
557 or there's data ready. */
558 #define ATAPI_MIN_CDB_BYTES 12
559 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
560 struct request *rq,
561 ide_handler_t *handler)
563 ide_hwif_t *hwif = drive->hwif;
564 int cmd_len;
565 struct cdrom_info *info = drive->driver_data;
566 ide_startstop_t startstop;
568 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
569 /* Here we should have been called after receiving an interrupt
570 from the device. DRQ should how be set. */
572 /* Check for errors. */
573 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
574 return ide_stopped;
576 /* Ok, next interrupt will be DMA interrupt. */
577 if (info->dma)
578 drive->waiting_for_dma = 1;
579 } else {
580 /* Otherwise, we must wait for DRQ to get set. */
581 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
582 BUSY_STAT, WAIT_READY))
583 return startstop;
586 /* Arm the interrupt handler. */
587 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
589 /* ATAPI commands get padded out to 12 bytes minimum */
590 cmd_len = COMMAND_SIZE(rq->cmd[0]);
591 if (cmd_len < ATAPI_MIN_CDB_BYTES)
592 cmd_len = ATAPI_MIN_CDB_BYTES;
594 /* Send the command to the device. */
595 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
597 /* Start the DMA if need be */
598 if (info->dma)
599 hwif->dma_start(drive);
601 return ide_started;
604 /****************************************************************************
605 * Block read functions.
608 static void ide_cd_pad_transfer(ide_drive_t *drive, xfer_func_t *xf, int len)
610 while (len > 0) {
611 int dum = 0;
612 xf(drive, &dum, sizeof(dum));
613 len -= sizeof(dum);
617 static void ide_cd_drain_data(ide_drive_t *drive, int nsects)
619 while (nsects > 0) {
620 static char dum[SECTOR_SIZE];
622 drive->hwif->atapi_input_bytes(drive, dum, sizeof(dum));
623 nsects--;
628 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
629 * buffer. Once the first sector is added, any subsequent sectors are
630 * assumed to be continuous (until the buffer is cleared). For the first
631 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
632 * the buffer is cleared.)
634 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
635 int sectors_to_transfer)
637 struct cdrom_info *info = drive->driver_data;
639 /* Number of sectors to read into the buffer. */
640 int sectors_to_buffer = min_t(int, sectors_to_transfer,
641 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
642 info->nsectors_buffered);
644 char *dest;
646 /* If we couldn't get a buffer, don't try to buffer anything... */
647 if (info->buffer == NULL)
648 sectors_to_buffer = 0;
650 /* If this is the first sector in the buffer, remember its number. */
651 if (info->nsectors_buffered == 0)
652 info->sector_buffered = sector;
654 /* Read the data into the buffer. */
655 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
656 while (sectors_to_buffer > 0) {
657 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
658 --sectors_to_buffer;
659 --sectors_to_transfer;
660 ++info->nsectors_buffered;
661 dest += SECTOR_SIZE;
664 /* Throw away any remaining data. */
665 ide_cd_drain_data(drive, sectors_to_transfer);
669 * Check the contents of the interrupt reason register from the cdrom
670 * and attempt to recover if there are problems. Returns 0 if everything's
671 * ok; nonzero if the request has been terminated.
673 <<<<<<< HEAD:drivers/ide/ide-cd.c
674 static
675 int ide_cd_check_ireason(ide_drive_t *drive, int len, int ireason, int rw)
676 =======
677 static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
678 int len, int ireason, int rw)
679 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
682 * ireason == 0: the drive wants to receive data from us
683 * ireason == 2: the drive is expecting to transfer data to us
685 if (ireason == (!rw << 1))
686 return 0;
687 else if (ireason == (rw << 1)) {
688 ide_hwif_t *hwif = drive->hwif;
689 xfer_func_t *xf;
691 /* Whoops... */
692 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
693 drive->name, __FUNCTION__);
695 xf = rw ? hwif->atapi_output_bytes : hwif->atapi_input_bytes;
696 ide_cd_pad_transfer(drive, xf, len);
697 } else if (rw == 0 && ireason == 1) {
698 /* Some drives (ASUS) seem to tell us that status
699 * info is available. just get it and ignore.
701 (void)ide_read_status(drive);
702 return 0;
703 } else {
704 /* Drive wants a command packet, or invalid ireason... */
705 printk(KERN_ERR "%s: %s: bad interrupt reason 0x%02x\n",
706 drive->name, __FUNCTION__, ireason);
709 <<<<<<< HEAD:drivers/ide/ide-cd.c
710 =======
711 if (rq->cmd_type == REQ_TYPE_ATA_PC)
712 rq->cmd_flags |= REQ_FAILED;
714 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
715 cdrom_end_request(drive, 0);
716 return -1;
720 * Assume that the drive will always provide data in multiples of at least
721 * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
723 static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
725 struct cdrom_info *cd = drive->driver_data;
727 if ((len % SECTOR_SIZE) == 0)
728 return 0;
730 printk(KERN_ERR "%s: %s: Bad transfer size %d\n",
731 drive->name, __FUNCTION__, len);
733 if (cd->cd_flags & IDE_CD_FLAG_LIMIT_NFRAMES)
734 printk(KERN_ERR " This drive is not supported by "
735 "this version of the driver\n");
736 else {
737 printk(KERN_ERR " Trying to limit transfer sizes\n");
738 cd->cd_flags |= IDE_CD_FLAG_LIMIT_NFRAMES;
741 return 1;
745 * Try to satisfy some of the current read request from our cached data.
746 * Returns nonzero if the request has been completed, zero otherwise.
748 static int cdrom_read_from_buffer (ide_drive_t *drive)
750 struct cdrom_info *info = drive->driver_data;
751 struct request *rq = HWGROUP(drive)->rq;
752 unsigned short sectors_per_frame;
754 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
756 /* Can't do anything if there's no buffer. */
757 if (info->buffer == NULL) return 0;
759 /* Loop while this request needs data and the next block is present
760 in our cache. */
761 while (rq->nr_sectors > 0 &&
762 rq->sector >= info->sector_buffered &&
763 rq->sector < info->sector_buffered + info->nsectors_buffered) {
764 if (rq->current_nr_sectors == 0)
765 cdrom_end_request(drive, 1);
767 memcpy (rq->buffer,
768 info->buffer +
769 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
770 SECTOR_SIZE);
771 rq->buffer += SECTOR_SIZE;
772 --rq->current_nr_sectors;
773 --rq->nr_sectors;
774 ++rq->sector;
777 /* If we've satisfied the current request,
778 terminate it successfully. */
779 if (rq->nr_sectors == 0) {
780 cdrom_end_request(drive, 1);
781 return -1;
784 /* Move on to the next buffer if needed. */
785 if (rq->current_nr_sectors == 0)
786 cdrom_end_request(drive, 1);
788 /* If this condition does not hold, then the kluge i use to
789 represent the number of sectors to skip at the start of a transfer
790 will fail. I think that this will never happen, but let's be
791 paranoid and check. */
792 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
793 (rq->sector & (sectors_per_frame - 1))) {
794 printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
795 drive->name, (long)rq->sector);
796 cdrom_end_request(drive, 0);
797 return -1;
800 return 0;
803 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
806 * Routine to send a read/write packet command to the drive.
807 * This is usually called directly from cdrom_start_{read,write}().
808 * However, for drq_interrupt devices, it is called from an interrupt
809 * when the drive is ready to accept the command.
811 static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
813 struct request *rq = HWGROUP(drive)->rq;
815 if (rq_data_dir(rq) == READ) {
816 unsigned short sectors_per_frame =
817 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
818 int nskip = rq->sector & (sectors_per_frame - 1);
821 * If the requested sector doesn't start on a frame boundary,
822 * we must adjust the start of the transfer so that it does,
823 * and remember to skip the first few sectors.
825 * If the rq->current_nr_sectors field is larger than the size
826 * of the buffer, it will mean that we're to skip a number of
827 * sectors equal to the amount by which rq->current_nr_sectors
828 * is larger than the buffer size.
830 if (nskip > 0) {
831 /* Sanity check... */
832 if (rq->current_nr_sectors !=
833 bio_cur_sectors(rq->bio)) {
834 printk(KERN_ERR "%s: %s: buffer botch (%u)\n",
835 drive->name, __FUNCTION__,
836 rq->current_nr_sectors);
837 cdrom_end_request(drive, 0);
838 return ide_stopped;
840 rq->current_nr_sectors += nskip;
843 #if 0
844 else
845 /* the immediate bit */
846 rq->cmd[1] = 1 << 3;
847 #endif
848 /* Set up the command */
849 rq->timeout = ATAPI_WAIT_PC;
851 /* Send the command to the drive and return. */
852 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
855 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
856 #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
857 #define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
859 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
861 struct cdrom_info *info = drive->driver_data;
862 int stat;
863 static int retry = 10;
865 if (cdrom_decode_status(drive, 0, &stat))
866 return ide_stopped;
868 info->cd_flags |= IDE_CD_FLAG_SEEKING;
870 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
871 if (--retry == 0) {
873 * this condition is far too common, to bother
874 * users about it
876 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
877 drive->dsc_overlap = 0;
880 return ide_stopped;
883 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
885 struct request *rq = HWGROUP(drive)->rq;
886 sector_t frame = rq->sector;
888 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
890 memset(rq->cmd, 0, sizeof(rq->cmd));
891 rq->cmd[0] = GPCMD_SEEK;
892 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
894 rq->timeout = ATAPI_WAIT_PC;
895 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
898 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
900 struct cdrom_info *info = drive->driver_data;
902 info->dma = 0;
903 info->start_seek = jiffies;
904 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
907 /* Fix up a possibly partially-processed request so that we can
908 start it over entirely, or even put it back on the request queue. */
909 static void restore_request (struct request *rq)
911 if (rq->buffer != bio_data(rq->bio)) {
912 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
914 rq->buffer = bio_data(rq->bio);
915 rq->nr_sectors += n;
916 rq->sector -= n;
918 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
919 rq->hard_nr_sectors = rq->nr_sectors;
920 rq->hard_sector = rq->sector;
921 rq->q->prep_rq_fn(rq->q, rq);
924 /****************************************************************************
925 * Execute all other packet commands.
928 static void ide_cd_request_sense_fixup(struct request *rq)
931 * Some of the trailing request sense fields are optional,
932 * and some drives don't send them. Sigh.
934 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
935 rq->data_len > 0 && rq->data_len <= 5)
936 while (rq->data_len > 0) {
937 *(u8 *)rq->data++ = 0;
938 --rq->data_len;
942 int ide_cd_queue_pc(ide_drive_t *drive, struct request *rq)
944 struct request_sense sense;
945 int retries = 10;
946 unsigned int flags = rq->cmd_flags;
948 if (rq->sense == NULL)
949 rq->sense = &sense;
951 /* Start of retry loop. */
952 do {
953 int error;
954 unsigned long time = jiffies;
955 rq->cmd_flags = flags;
957 error = ide_do_drive_cmd(drive, rq, ide_wait);
958 time = jiffies - time;
960 /* FIXME: we should probably abort/retry or something
961 * in case of failure */
962 if (rq->cmd_flags & REQ_FAILED) {
963 /* The request failed. Retry if it was due to a unit
964 attention status
965 (usually means media was changed). */
966 struct request_sense *reqbuf = rq->sense;
968 if (reqbuf->sense_key == UNIT_ATTENTION)
969 cdrom_saw_media_change(drive);
970 else if (reqbuf->sense_key == NOT_READY &&
971 reqbuf->asc == 4 && reqbuf->ascq != 4) {
972 /* The drive is in the process of loading
973 a disk. Retry, but wait a little to give
974 the drive time to complete the load. */
975 ssleep(2);
976 } else {
977 /* Otherwise, don't retry. */
978 retries = 0;
980 --retries;
983 /* End of retry loop. */
984 } while ((rq->cmd_flags & REQ_FAILED) && retries >= 0);
986 /* Return an error if the command failed. */
987 return (rq->cmd_flags & REQ_FAILED) ? -EIO : 0;
991 * Called from blk_end_request_callback() after the data of the request
992 * is completed and before the request is completed.
993 * By returning value '1', blk_end_request_callback() returns immediately
994 * without completing the request.
996 static int cdrom_newpc_intr_dummy_cb(struct request *rq)
998 return 1;
1001 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1003 struct cdrom_info *info = drive->driver_data;
1004 struct request *rq = HWGROUP(drive)->rq;
1005 xfer_func_t *xferfunc;
1006 ide_expiry_t *expiry = NULL;
1007 int dma_error = 0, dma, stat, ireason, len, thislen, uptodate = 0;
1008 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
1009 unsigned int timeout;
1010 u8 lowcyl, highcyl;
1012 /* Check for errors. */
1013 dma = info->dma;
1014 if (dma) {
1015 info->dma = 0;
1016 dma_error = HWIF(drive)->ide_dma_end(drive);
1017 if (dma_error) {
1018 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
1019 write ? "write" : "read");
1020 ide_dma_off(drive);
1024 if (cdrom_decode_status(drive, 0, &stat))
1025 return ide_stopped;
1028 * using dma, transfer is complete now
1030 if (dma) {
1031 if (dma_error)
1032 return ide_error(drive, "dma error", stat);
1033 if (blk_fs_request(rq)) {
1034 ide_end_request(drive, 1, rq->nr_sectors);
1035 return ide_stopped;
1037 goto end_request;
1041 * ok we fall to pio :/
1043 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1044 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1045 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1047 len = lowcyl + (256 * highcyl);
1049 thislen = blk_fs_request(rq) ? len : rq->data_len;
1050 if (thislen > len)
1051 thislen = len;
1054 * If DRQ is clear, the command has completed.
1056 if ((stat & DRQ_STAT) == 0) {
1057 if (blk_fs_request(rq)) {
1059 * If we're not done reading/writing, complain.
1060 * Otherwise, complete the command normally.
1062 uptodate = 1;
1063 if (rq->current_nr_sectors > 0) {
1064 printk(KERN_ERR "%s: %s: data underrun "
1065 "(%d blocks)\n",
1066 drive->name, __FUNCTION__,
1067 rq->current_nr_sectors);
1068 if (!write)
1069 rq->cmd_flags |= REQ_FAILED;
1070 uptodate = 0;
1072 cdrom_end_request(drive, uptodate);
1073 return ide_stopped;
1074 } else if (!blk_pc_request(rq)) {
1075 ide_cd_request_sense_fixup(rq);
1076 /* Complain if we still have data left to transfer. */
1077 uptodate = rq->data_len ? 0 : 1;
1079 goto end_request;
1083 * check which way to transfer data
1085 <<<<<<< HEAD:drivers/ide/ide-cd.c
1086 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1087 if (ide_cd_check_ireason(drive, len, ireason, write))
1088 return ide_stopped;
1089 =======
1090 if (ide_cd_check_ireason(drive, rq, len, ireason, write))
1091 return ide_stopped;
1092 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1094 <<<<<<< HEAD:drivers/ide/ide-cd.c
1095 if (blk_fs_request(rq) && write == 0) {
1096 =======
1097 if (blk_fs_request(rq)) {
1098 if (write == 0) {
1099 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1100 int nskip;
1102 if (ide_cd_check_transfer_size(drive, len)) {
1103 cdrom_end_request(drive, 0);
1104 return ide_stopped;
1108 * First, figure out if we need to bit-bucket
1109 * any of the leading sectors.
1111 nskip = min_t(int, rq->current_nr_sectors
1112 - bio_cur_sectors(rq->bio),
1113 thislen >> 9);
1114 if (nskip > 0) {
1115 ide_cd_drain_data(drive, nskip);
1116 rq->current_nr_sectors -= nskip;
1117 thislen -= (nskip << 9);
1122 if (ireason == 0) {
1123 write = 1;
1124 xferfunc = HWIF(drive)->atapi_output_bytes;
1125 <<<<<<< HEAD:drivers/ide/ide-cd.c
1126 } else if (ireason == 2 || (ireason == 1 &&
1127 (blk_fs_request(rq) || blk_pc_request(rq)))) {
1128 =======
1129 } else {
1130 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1131 write = 0;
1132 xferfunc = HWIF(drive)->atapi_input_bytes;
1133 <<<<<<< HEAD:drivers/ide/ide-cd.c
1134 } else {
1135 printk(KERN_ERR "%s: %s: The drive "
1136 "appears confused (ireason = 0x%02x). "
1137 "Trying to recover by ending request.\n",
1138 drive->name, __FUNCTION__, ireason);
1139 goto end_request;
1140 =======
1141 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1145 * transfer data
1147 while (thislen > 0) {
1148 u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
1149 int blen = rq->data_len;
1152 * bio backed?
1154 if (rq->bio) {
1155 if (blk_fs_request(rq)) {
1156 ptr = rq->buffer;
1157 blen = rq->current_nr_sectors << 9;
1158 } else {
1159 ptr = bio_data(rq->bio);
1160 blen = bio_iovec(rq->bio)->bv_len;
1164 if (!ptr) {
1165 if (blk_fs_request(rq) && !write)
1167 * If the buffers are full, cache the rest
1168 * of the data in our internal buffer.
1170 cdrom_buffer_sectors(drive, rq->sector,
1171 thislen >> 9);
1172 else {
1173 printk(KERN_ERR "%s: confused, missing data\n",
1174 drive->name);
1175 blk_dump_rq_flags(rq, rq_data_dir(rq)
1176 ? "cdrom_newpc_intr, write"
1177 : "cdrom_newpc_intr, read");
1179 break;
1182 if (blen > thislen)
1183 blen = thislen;
1185 xferfunc(drive, ptr, blen);
1187 thislen -= blen;
1188 len -= blen;
1190 if (blk_fs_request(rq)) {
1191 rq->buffer += blen;
1192 rq->nr_sectors -= (blen >> 9);
1193 rq->current_nr_sectors -= (blen >> 9);
1194 rq->sector += (blen >> 9);
1196 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1197 cdrom_end_request(drive, 1);
1198 } else {
1199 rq->data_len -= blen;
1202 * The request can't be completed until DRQ is cleared.
1203 * So complete the data, but don't complete the request
1204 * using the dummy function for the callback feature
1205 * of blk_end_request_callback().
1207 if (rq->bio)
1208 blk_end_request_callback(rq, 0, blen,
1209 cdrom_newpc_intr_dummy_cb);
1210 else
1211 rq->data += blen;
1213 <<<<<<< HEAD:drivers/ide/ide-cd.c
1214 =======
1215 if (!write && blk_sense_request(rq))
1216 rq->sense_len += blen;
1217 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1220 <<<<<<< HEAD:drivers/ide/ide-cd.c
1221 if (write && blk_sense_request(rq))
1222 rq->sense_len += thislen;
1224 =======
1225 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1227 * pad, if necessary
1229 if (!blk_fs_request(rq) && len > 0)
1230 ide_cd_pad_transfer(drive, xferfunc, len);
1232 if (blk_pc_request(rq)) {
1233 timeout = rq->timeout;
1234 } else {
1235 timeout = ATAPI_WAIT_PC;
1236 if (!blk_fs_request(rq))
1237 expiry = cdrom_timer_expiry;
1240 ide_set_handler(drive, cdrom_newpc_intr, timeout, expiry);
1241 return ide_started;
1243 end_request:
1244 if (blk_pc_request(rq)) {
1245 unsigned long flags;
1246 <<<<<<< HEAD:drivers/ide/ide-cd.c
1247 =======
1248 unsigned int dlen = rq->data_len;
1250 if (dma)
1251 rq->data_len = 0;
1252 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1254 spin_lock_irqsave(&ide_lock, flags);
1255 <<<<<<< HEAD:drivers/ide/ide-cd.c
1256 if (__blk_end_request(rq, 0, rq->data_len))
1257 =======
1258 if (__blk_end_request(rq, 0, dlen))
1259 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1260 BUG();
1261 HWGROUP(drive)->rq = NULL;
1262 spin_unlock_irqrestore(&ide_lock, flags);
1263 } else {
1264 if (!uptodate)
1265 rq->cmd_flags |= REQ_FAILED;
1266 cdrom_end_request(drive, uptodate);
1268 return ide_stopped;
1271 static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
1273 struct cdrom_info *cd = drive->driver_data;
1274 int write = rq_data_dir(rq) == WRITE;
1275 unsigned short sectors_per_frame =
1276 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1278 if (write) {
1280 * disk has become write protected
1282 if (cd->disk->policy) {
1283 cdrom_end_request(drive, 0);
1284 return ide_stopped;
1286 } else {
1288 * We may be retrying this request after an error. Fix up any
1289 * weirdness which might be present in the request packet.
1291 restore_request(rq);
1293 /* Satisfy whatever we can of this request from our cache. */
1294 if (cdrom_read_from_buffer(drive))
1295 return ide_stopped;
1299 * use DMA, if possible / writes *must* be hardware frame aligned
1301 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1302 (rq->sector & (sectors_per_frame - 1))) {
1303 if (write) {
1304 cdrom_end_request(drive, 0);
1305 return ide_stopped;
1307 cd->dma = 0;
1308 } else
1309 cd->dma = drive->using_dma;
1311 /* Clear the local sector buffer. */
1312 cd->nsectors_buffered = 0;
1314 if (write)
1315 cd->devinfo.media_written = 1;
1317 /* Start sending the read/write request to the drive. */
1318 return cdrom_start_packet_command(drive, 32768, cdrom_start_rw_cont);
1321 static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1323 struct request *rq = HWGROUP(drive)->rq;
1325 if (!rq->timeout)
1326 rq->timeout = ATAPI_WAIT_PC;
1328 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1331 static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1333 struct cdrom_info *info = drive->driver_data;
1335 if (blk_pc_request(rq))
1336 rq->cmd_flags |= REQ_QUIET;
1337 else
1338 rq->cmd_flags &= ~REQ_FAILED;
1340 info->dma = 0;
1343 * sg request
1345 if (rq->bio) {
1346 int mask = drive->queue->dma_alignment;
1347 unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1349 info->dma = drive->using_dma;
1352 * check if dma is safe
1354 * NOTE! The "len" and "addr" checks should possibly have
1355 * separate masks.
1357 if ((rq->data_len & 15) || (addr & mask))
1358 info->dma = 0;
1361 /* Start sending the command to the drive. */
1362 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1365 /****************************************************************************
1366 * cdrom driver request routine.
1368 static ide_startstop_t
1369 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1371 ide_startstop_t action;
1372 struct cdrom_info *info = drive->driver_data;
1374 if (blk_fs_request(rq)) {
1375 if (info->cd_flags & IDE_CD_FLAG_SEEKING) {
1376 unsigned long elapsed = jiffies - info->start_seek;
1377 int stat = ide_read_status(drive);
1379 if ((stat & SEEK_STAT) != SEEK_STAT) {
1380 if (elapsed < IDECD_SEEK_TIMEOUT) {
1381 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1382 return ide_stopped;
1384 printk (KERN_ERR "%s: DSC timeout\n", drive->name);
1386 info->cd_flags &= ~IDE_CD_FLAG_SEEKING;
1388 if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
1389 action = cdrom_start_seek(drive, block);
1390 } else
1391 action = cdrom_start_rw(drive, rq);
1392 info->last_block = block;
1393 return action;
1394 } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
1395 rq->cmd_type == REQ_TYPE_ATA_PC) {
1396 return cdrom_do_block_pc(drive, rq);
1397 } else if (blk_special_request(rq)) {
1399 * right now this can only be a reset...
1401 cdrom_end_request(drive, 1);
1402 return ide_stopped;
1405 blk_dump_rq_flags(rq, "ide-cd bad flags");
1406 cdrom_end_request(drive, 0);
1407 return ide_stopped;
1412 /****************************************************************************
1413 * Ioctl handling.
1415 * Routines which queue packet commands take as a final argument a pointer
1416 * to a request_sense struct. If execution of the command results
1417 * in an error with a CHECK CONDITION status, this structure will be filled
1418 * with the results of the subsequent request sense command. The pointer
1419 * can also be NULL, in which case no sense information is returned.
1422 static
1423 void msf_from_bcd (struct atapi_msf *msf)
1425 msf->minute = BCD2BIN(msf->minute);
1426 msf->second = BCD2BIN(msf->second);
1427 msf->frame = BCD2BIN(msf->frame);
1430 int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1432 struct request req;
1433 struct cdrom_info *info = drive->driver_data;
1434 struct cdrom_device_info *cdi = &info->devinfo;
1436 ide_cd_init_rq(drive, &req);
1438 req.sense = sense;
1439 req.cmd[0] = GPCMD_TEST_UNIT_READY;
1440 req.cmd_flags |= REQ_QUIET;
1443 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1444 * switch CDs instead of supporting the LOAD_UNLOAD opcode.
1446 req.cmd[7] = cdi->sanyo_slot % 3;
1448 return ide_cd_queue_pc(drive, &req);
1451 static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1452 unsigned long *sectors_per_frame,
1453 struct request_sense *sense)
1455 struct {
1456 __u32 lba;
1457 __u32 blocklen;
1458 } capbuf;
1460 int stat;
1461 struct request req;
1463 ide_cd_init_rq(drive, &req);
1465 req.sense = sense;
1466 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
1467 req.data = (char *)&capbuf;
1468 req.data_len = sizeof(capbuf);
1469 req.cmd_flags |= REQ_QUIET;
1471 stat = ide_cd_queue_pc(drive, &req);
1472 if (stat == 0) {
1473 *capacity = 1 + be32_to_cpu(capbuf.lba);
1474 *sectors_per_frame =
1475 be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
1478 return stat;
1481 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1482 int format, char *buf, int buflen,
1483 struct request_sense *sense)
1485 struct request req;
1487 ide_cd_init_rq(drive, &req);
1489 req.sense = sense;
1490 req.data = buf;
1491 req.data_len = buflen;
1492 req.cmd_flags |= REQ_QUIET;
1493 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1494 req.cmd[6] = trackno;
1495 req.cmd[7] = (buflen >> 8);
1496 req.cmd[8] = (buflen & 0xff);
1497 req.cmd[9] = (format << 6);
1499 if (msf_flag)
1500 req.cmd[1] = 2;
1502 return ide_cd_queue_pc(drive, &req);
1505 /* Try to read the entire TOC for the disk into our internal buffer. */
1506 int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
1508 int stat, ntracks, i;
1509 struct cdrom_info *info = drive->driver_data;
1510 struct cdrom_device_info *cdi = &info->devinfo;
1511 struct atapi_toc *toc = info->toc;
1512 struct {
1513 struct atapi_toc_header hdr;
1514 struct atapi_toc_entry ent;
1515 } ms_tmp;
1516 long last_written;
1517 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1519 if (toc == NULL) {
1520 /* Try to allocate space. */
1521 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
1522 if (toc == NULL) {
1523 printk (KERN_ERR "%s: No cdrom TOC buffer!\n", drive->name);
1524 return -ENOMEM;
1526 info->toc = toc;
1529 /* Check to see if the existing data is still valid.
1530 If it is, just return. */
1531 (void) cdrom_check_status(drive, sense);
1533 if (info->cd_flags & IDE_CD_FLAG_TOC_VALID)
1534 return 0;
1536 /* Try to get the total cdrom capacity and sector size. */
1537 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
1538 sense);
1539 if (stat)
1540 toc->capacity = 0x1fffff;
1542 set_capacity(info->disk, toc->capacity * sectors_per_frame);
1543 /* Save a private copy of te TOC capacity for error handling */
1544 drive->probed_capacity = toc->capacity * sectors_per_frame;
1546 blk_queue_hardsect_size(drive->queue,
1547 sectors_per_frame << SECTOR_BITS);
1549 /* First read just the header, so we know how long the TOC is. */
1550 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1551 sizeof(struct atapi_toc_header), sense);
1552 if (stat)
1553 return stat;
1555 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
1556 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
1557 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
1560 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1561 if (ntracks <= 0)
1562 return -EIO;
1563 if (ntracks > MAX_TRACKS)
1564 ntracks = MAX_TRACKS;
1566 /* Now read the whole schmeer. */
1567 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1568 (char *)&toc->hdr,
1569 sizeof(struct atapi_toc_header) +
1570 (ntracks + 1) *
1571 sizeof(struct atapi_toc_entry), sense);
1573 if (stat && toc->hdr.first_track > 1) {
1574 /* Cds with CDI tracks only don't have any TOC entries,
1575 despite of this the returned values are
1576 first_track == last_track = number of CDI tracks + 1,
1577 so that this case is indistinguishable from the same
1578 layout plus an additional audio track.
1579 If we get an error for the regular case, we assume
1580 a CDI without additional audio tracks. In this case
1581 the readable TOC is empty (CDI tracks are not included)
1582 and only holds the Leadout entry. Heiko Eißfeldt */
1583 ntracks = 0;
1584 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1585 (char *)&toc->hdr,
1586 sizeof(struct atapi_toc_header) +
1587 (ntracks + 1) *
1588 sizeof(struct atapi_toc_entry),
1589 sense);
1590 if (stat)
1591 return stat;
1593 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
1594 toc->hdr.first_track = (u8)BIN2BCD(CDROM_LEADOUT);
1595 toc->hdr.last_track = (u8)BIN2BCD(CDROM_LEADOUT);
1596 } else {
1597 toc->hdr.first_track = CDROM_LEADOUT;
1598 toc->hdr.last_track = CDROM_LEADOUT;
1602 if (stat)
1603 return stat;
1605 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
1607 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
1608 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
1609 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
1612 for (i = 0; i <= ntracks; i++) {
1613 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
1614 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD)
1615 toc->ent[i].track = BCD2BIN(toc->ent[i].track);
1616 msf_from_bcd(&toc->ent[i].addr.msf);
1618 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1619 toc->ent[i].addr.msf.second,
1620 toc->ent[i].addr.msf.frame);
1623 /* Read the multisession information. */
1624 if (toc->hdr.first_track != CDROM_LEADOUT) {
1625 /* Read the multisession information. */
1626 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
1627 sizeof(ms_tmp), sense);
1628 if (stat)
1629 return stat;
1631 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1632 } else {
1633 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
1634 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1637 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
1638 /* Re-read multisession information using MSF format */
1639 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1640 sizeof(ms_tmp), sense);
1641 if (stat)
1642 return stat;
1644 msf_from_bcd (&ms_tmp.ent.addr.msf);
1645 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
1646 ms_tmp.ent.addr.msf.second,
1647 ms_tmp.ent.addr.msf.frame);
1650 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1652 /* Now try to get the total cdrom capacity. */
1653 stat = cdrom_get_last_written(cdi, &last_written);
1654 if (!stat && (last_written > toc->capacity)) {
1655 toc->capacity = last_written;
1656 set_capacity(info->disk, toc->capacity * sectors_per_frame);
1657 drive->probed_capacity = toc->capacity * sectors_per_frame;
1660 /* Remember that we've read this stuff. */
1661 info->cd_flags |= IDE_CD_FLAG_TOC_VALID;
1663 return 0;
1666 int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
1668 struct cdrom_info *info = drive->driver_data;
1669 struct cdrom_device_info *cdi = &info->devinfo;
1670 struct packet_command cgc;
1671 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
1673 if ((info->cd_flags & IDE_CD_FLAG_FULL_CAPS_PAGE) == 0)
1674 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
1676 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
1677 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
1678 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1679 if (!stat)
1680 break;
1681 } while (--attempts);
1682 return stat;
1685 void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
1687 struct cdrom_info *cd = drive->driver_data;
1688 u16 curspeed, maxspeed;
1690 curspeed = *(u16 *)&buf[8 + 14];
1691 maxspeed = *(u16 *)&buf[8 + 8];
1693 if (cd->cd_flags & IDE_CD_FLAG_LE_SPEED_FIELDS) {
1694 curspeed = le16_to_cpu(curspeed);
1695 maxspeed = le16_to_cpu(maxspeed);
1696 } else {
1697 curspeed = be16_to_cpu(curspeed);
1698 maxspeed = be16_to_cpu(maxspeed);
1701 cd->current_speed = (curspeed + (176/2)) / 176;
1702 cd->max_speed = (maxspeed + (176/2)) / 176;
1705 #define IDE_CD_CAPABILITIES \
1706 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1707 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1708 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1709 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1710 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
1712 static struct cdrom_device_ops ide_cdrom_dops = {
1713 .open = ide_cdrom_open_real,
1714 .release = ide_cdrom_release_real,
1715 .drive_status = ide_cdrom_drive_status,
1716 .media_changed = ide_cdrom_check_media_change_real,
1717 .tray_move = ide_cdrom_tray_move,
1718 .lock_door = ide_cdrom_lock_door,
1719 .select_speed = ide_cdrom_select_speed,
1720 .get_last_session = ide_cdrom_get_last_session,
1721 .get_mcn = ide_cdrom_get_mcn,
1722 .reset = ide_cdrom_reset,
1723 .audio_ioctl = ide_cdrom_audio_ioctl,
1724 .capability = IDE_CD_CAPABILITIES,
1725 .generic_packet = ide_cdrom_packet,
1728 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
1730 struct cdrom_info *info = drive->driver_data;
1731 struct cdrom_device_info *devinfo = &info->devinfo;
1733 devinfo->ops = &ide_cdrom_dops;
1734 devinfo->speed = info->current_speed;
1735 devinfo->capacity = nslots;
1736 devinfo->handle = drive;
1737 strcpy(devinfo->name, drive->name);
1739 if (info->cd_flags & IDE_CD_FLAG_NO_SPEED_SELECT)
1740 devinfo->mask |= CDC_SELECT_SPEED;
1742 devinfo->disk = info->disk;
1743 return register_cdrom(devinfo);
1746 static
1747 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
1749 struct cdrom_info *cd = drive->driver_data;
1750 struct cdrom_device_info *cdi = &cd->devinfo;
1751 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1752 mechtype_t mechtype;
1753 int nslots = 1;
1755 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1756 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1757 CDC_MO_DRIVE | CDC_RAM);
1759 if (drive->media == ide_optical) {
1760 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
1761 printk(KERN_ERR "%s: ATAPI magneto-optical drive\n", drive->name);
1762 return nslots;
1765 if (cd->cd_flags & IDE_CD_FLAG_PRE_ATAPI12) {
1766 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
1767 cdi->mask &= ~CDC_PLAY_AUDIO;
1768 return nslots;
1772 * we have to cheat a little here. the packet will eventually
1773 * be queued with ide_cdrom_packet(), which extracts the
1774 * drive from cdi->handle. Since this device hasn't been
1775 * registered with the Uniform layer yet, it can't do this.
1776 * Same goes for cdi->ops.
1778 cdi->handle = drive;
1779 cdi->ops = &ide_cdrom_dops;
1781 if (ide_cdrom_get_capabilities(drive, buf))
1782 return 0;
1784 if ((buf[8 + 6] & 0x01) == 0)
1785 cd->cd_flags |= IDE_CD_FLAG_NO_DOORLOCK;
1786 if (buf[8 + 6] & 0x08)
1787 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
1788 if (buf[8 + 3] & 0x01)
1789 cdi->mask &= ~CDC_CD_R;
1790 if (buf[8 + 3] & 0x02)
1791 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
1792 if (buf[8 + 2] & 0x38)
1793 cdi->mask &= ~CDC_DVD;
1794 if (buf[8 + 3] & 0x20)
1795 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
1796 if (buf[8 + 3] & 0x10)
1797 cdi->mask &= ~CDC_DVD_R;
1798 if ((buf[8 + 4] & 0x01) || (cd->cd_flags & IDE_CD_FLAG_PLAY_AUDIO_OK))
1799 cdi->mask &= ~CDC_PLAY_AUDIO;
1801 mechtype = buf[8 + 6] >> 5;
1802 if (mechtype == mechtype_caddy || mechtype == mechtype_popup)
1803 cdi->mask |= CDC_CLOSE_TRAY;
1805 if (cdi->sanyo_slot > 0) {
1806 cdi->mask &= ~CDC_SELECT_DISC;
1807 nslots = 3;
1808 } else if (mechtype == mechtype_individual_changer ||
1809 mechtype == mechtype_cartridge_changer) {
1810 nslots = cdrom_number_of_slots(cdi);
1811 if (nslots > 1)
1812 cdi->mask &= ~CDC_SELECT_DISC;
1815 ide_cdrom_update_speed(drive, buf);
1817 printk(KERN_INFO "%s: ATAPI", drive->name);
1819 /* don't print speed if the drive reported 0 */
1820 if (cd->max_speed)
1821 printk(KERN_CONT " %dX", cd->max_speed);
1823 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
1825 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1826 printk(KERN_CONT " DVD%s%s",
1827 (cdi->mask & CDC_DVD_R) ? "" : "-R",
1828 (cdi->mask & CDC_DVD_RAM) ? "" : "-RAM");
1830 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1831 printk(KERN_CONT " CD%s%s",
1832 (cdi->mask & CDC_CD_R) ? "" : "-R",
1833 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
1835 if ((cdi->mask & CDC_SELECT_DISC) == 0)
1836 printk(KERN_CONT " changer w/%d slots", nslots);
1837 else
1838 printk(KERN_CONT " drive");
1840 printk(KERN_CONT ", %dkB Cache\n", be16_to_cpu(*(u16 *)&buf[8 + 12]));
1842 return nslots;
1845 #ifdef CONFIG_IDE_PROC_FS
1846 static void ide_cdrom_add_settings(ide_drive_t *drive)
1848 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
1850 #else
1851 static inline void ide_cdrom_add_settings(ide_drive_t *drive) { ; }
1852 #endif
1855 * standard prep_rq_fn that builds 10 byte cmds
1857 static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
1859 int hard_sect = queue_hardsect_size(q);
1860 long block = (long)rq->hard_sector / (hard_sect >> 9);
1861 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
1863 memset(rq->cmd, 0, sizeof(rq->cmd));
1865 if (rq_data_dir(rq) == READ)
1866 rq->cmd[0] = GPCMD_READ_10;
1867 else
1868 rq->cmd[0] = GPCMD_WRITE_10;
1871 * fill in lba
1873 rq->cmd[2] = (block >> 24) & 0xff;
1874 rq->cmd[3] = (block >> 16) & 0xff;
1875 rq->cmd[4] = (block >> 8) & 0xff;
1876 rq->cmd[5] = block & 0xff;
1879 * and transfer length
1881 rq->cmd[7] = (blocks >> 8) & 0xff;
1882 rq->cmd[8] = blocks & 0xff;
1883 rq->cmd_len = 10;
1884 return BLKPREP_OK;
1888 * Most of the SCSI commands are supported directly by ATAPI devices.
1889 * This transform handles the few exceptions.
1891 static int ide_cdrom_prep_pc(struct request *rq)
1893 u8 *c = rq->cmd;
1896 * Transform 6-byte read/write commands to the 10-byte version
1898 if (c[0] == READ_6 || c[0] == WRITE_6) {
1899 c[8] = c[4];
1900 c[5] = c[3];
1901 c[4] = c[2];
1902 c[3] = c[1] & 0x1f;
1903 c[2] = 0;
1904 c[1] &= 0xe0;
1905 c[0] += (READ_10 - READ_6);
1906 rq->cmd_len = 10;
1907 return BLKPREP_OK;
1911 * it's silly to pretend we understand 6-byte sense commands, just
1912 * reject with ILLEGAL_REQUEST and the caller should take the
1913 * appropriate action
1915 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
1916 rq->errors = ILLEGAL_REQUEST;
1917 return BLKPREP_KILL;
1920 return BLKPREP_OK;
1923 static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
1925 if (blk_fs_request(rq))
1926 return ide_cdrom_prep_fs(q, rq);
1927 else if (blk_pc_request(rq))
1928 return ide_cdrom_prep_pc(rq);
1930 return 0;
1933 struct cd_list_entry {
1934 const char *id_model;
1935 const char *id_firmware;
1936 unsigned int cd_flags;
1939 static const struct cd_list_entry ide_cd_quirks_list[] = {
1940 /* Limit transfer size per interrupt. */
1941 { "SAMSUNG CD-ROM SCR-2430", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
1942 { "SAMSUNG CD-ROM SCR-2432", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
1943 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
1944 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_CD_FLAG_NO_SPEED_SELECT },
1945 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
1946 { "NEC CD-ROM DRIVE:260", "1.01", IDE_CD_FLAG_TOCADDR_AS_BCD |
1947 IDE_CD_FLAG_PRE_ATAPI12, },
1948 /* Vertos 300, some versions of this drive like to talk BCD. */
1949 { "V003S0DS", NULL, IDE_CD_FLAG_VERTOS_300_SSD, },
1950 /* Vertos 600 ESD. */
1951 { "V006E0DS", NULL, IDE_CD_FLAG_VERTOS_600_ESD, },
1953 * Sanyo 3 CD changer uses a non-standard command for CD changing
1954 * (by default standard ATAPI support for CD changers is used).
1956 { "CD-ROM CDR-C3 G", NULL, IDE_CD_FLAG_SANYO_3CD },
1957 { "CD-ROM CDR-C3G", NULL, IDE_CD_FLAG_SANYO_3CD },
1958 { "CD-ROM CDR_C36", NULL, IDE_CD_FLAG_SANYO_3CD },
1959 /* Stingray 8X CD-ROM. */
1960 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_CD_FLAG_PRE_ATAPI12},
1962 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1963 * mode sense page capabilities size, but older drives break.
1965 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
1966 { "WPI CDS-32X", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
1967 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
1968 { "", "241N", IDE_CD_FLAG_LE_SPEED_FIELDS },
1970 * Some drives used by Apple don't advertise audio play
1971 * but they do support reading TOC & audio datas.
1973 { "MATSHITADVD-ROM SR-8187", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1974 { "MATSHITADVD-ROM SR-8186", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1975 { "MATSHITADVD-ROM SR-8176", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1976 { "MATSHITADVD-ROM SR-8174", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1977 <<<<<<< HEAD:drivers/ide/ide-cd.c
1978 =======
1979 { "Optiarc DVD RW AD-5200A", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1980 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ide/ide-cd.c
1981 { NULL, NULL, 0 }
1984 static unsigned int ide_cd_flags(struct hd_driveid *id)
1986 const struct cd_list_entry *cle = ide_cd_quirks_list;
1988 while (cle->id_model) {
1989 if (strcmp(cle->id_model, id->model) == 0 &&
1990 (cle->id_firmware == NULL ||
1991 strstr(id->fw_rev, cle->id_firmware)))
1992 return cle->cd_flags;
1993 cle++;
1996 return 0;
1999 static
2000 int ide_cdrom_setup (ide_drive_t *drive)
2002 struct cdrom_info *cd = drive->driver_data;
2003 struct cdrom_device_info *cdi = &cd->devinfo;
2004 struct hd_driveid *id = drive->id;
2005 int nslots;
2007 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
2008 blk_queue_dma_alignment(drive->queue, 31);
2009 drive->queue->unplug_delay = (1 * HZ) / 1000;
2010 if (!drive->queue->unplug_delay)
2011 drive->queue->unplug_delay = 1;
2013 drive->special.all = 0;
2015 cd->cd_flags = IDE_CD_FLAG_MEDIA_CHANGED | IDE_CD_FLAG_NO_EJECT |
2016 ide_cd_flags(id);
2018 if ((id->config & 0x0060) == 0x20)
2019 cd->cd_flags |= IDE_CD_FLAG_DRQ_INTERRUPT;
2021 if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_300_SSD) &&
2022 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
2023 cd->cd_flags |= (IDE_CD_FLAG_TOCTRACKS_AS_BCD |
2024 IDE_CD_FLAG_TOCADDR_AS_BCD);
2025 else if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_600_ESD) &&
2026 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
2027 cd->cd_flags |= IDE_CD_FLAG_TOCTRACKS_AS_BCD;
2028 else if (cd->cd_flags & IDE_CD_FLAG_SANYO_3CD)
2029 cdi->sanyo_slot = 3; /* 3 => use CD in slot 0 */
2031 nslots = ide_cdrom_probe_capabilities (drive);
2034 * set correct block size
2036 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
2038 if (drive->autotune == IDE_TUNE_DEFAULT ||
2039 drive->autotune == IDE_TUNE_AUTO)
2040 drive->dsc_overlap = (drive->next != drive);
2042 if (ide_cdrom_register(drive, nslots)) {
2043 printk (KERN_ERR "%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2044 cd->devinfo.handle = NULL;
2045 return 1;
2047 ide_cdrom_add_settings(drive);
2048 return 0;
2051 #ifdef CONFIG_IDE_PROC_FS
2052 static
2053 sector_t ide_cdrom_capacity (ide_drive_t *drive)
2055 unsigned long capacity, sectors_per_frame;
2057 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
2058 return 0;
2060 return capacity * sectors_per_frame;
2062 #endif
2064 static void ide_cd_remove(ide_drive_t *drive)
2066 struct cdrom_info *info = drive->driver_data;
2068 ide_proc_unregister_driver(drive, info->driver);
2070 del_gendisk(info->disk);
2072 ide_cd_put(info);
2075 static void ide_cd_release(struct kref *kref)
2077 struct cdrom_info *info = to_ide_cd(kref);
2078 struct cdrom_device_info *devinfo = &info->devinfo;
2079 ide_drive_t *drive = info->drive;
2080 struct gendisk *g = info->disk;
2082 kfree(info->buffer);
2083 kfree(info->toc);
2084 if (devinfo->handle == drive && unregister_cdrom(devinfo))
2085 printk(KERN_ERR "%s: %s failed to unregister device from the cdrom "
2086 "driver.\n", __FUNCTION__, drive->name);
2087 drive->dsc_overlap = 0;
2088 drive->driver_data = NULL;
2089 blk_queue_prep_rq(drive->queue, NULL);
2090 g->private_data = NULL;
2091 put_disk(g);
2092 kfree(info);
2095 static int ide_cd_probe(ide_drive_t *);
2097 #ifdef CONFIG_IDE_PROC_FS
2098 static int proc_idecd_read_capacity
2099 (char *page, char **start, off_t off, int count, int *eof, void *data)
2101 ide_drive_t *drive = data;
2102 int len;
2104 len = sprintf(page,"%llu\n", (long long)ide_cdrom_capacity(drive));
2105 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
2108 static ide_proc_entry_t idecd_proc[] = {
2109 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
2110 { NULL, 0, NULL, NULL }
2112 #endif
2114 static ide_driver_t ide_cdrom_driver = {
2115 .gen_driver = {
2116 .owner = THIS_MODULE,
2117 .name = "ide-cdrom",
2118 .bus = &ide_bus_type,
2120 .probe = ide_cd_probe,
2121 .remove = ide_cd_remove,
2122 .version = IDECD_VERSION,
2123 .media = ide_cdrom,
2124 .supports_dsc_overlap = 1,
2125 .do_request = ide_do_rw_cdrom,
2126 .end_request = ide_end_request,
2127 .error = __ide_error,
2128 .abort = __ide_abort,
2129 #ifdef CONFIG_IDE_PROC_FS
2130 .proc = idecd_proc,
2131 #endif
2134 static int idecd_open(struct inode * inode, struct file * file)
2136 struct gendisk *disk = inode->i_bdev->bd_disk;
2137 struct cdrom_info *info;
2138 int rc = -ENOMEM;
2140 if (!(info = ide_cd_get(disk)))
2141 return -ENXIO;
2143 if (!info->buffer)
2144 info->buffer = kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL|__GFP_REPEAT);
2146 if (info->buffer)
2147 rc = cdrom_open(&info->devinfo, inode, file);
2149 if (rc < 0)
2150 ide_cd_put(info);
2152 return rc;
2155 static int idecd_release(struct inode * inode, struct file * file)
2157 struct gendisk *disk = inode->i_bdev->bd_disk;
2158 struct cdrom_info *info = ide_cd_g(disk);
2160 cdrom_release (&info->devinfo, file);
2162 ide_cd_put(info);
2164 return 0;
2167 static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2169 struct packet_command cgc;
2170 char buffer[16];
2171 int stat;
2172 char spindown;
2174 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
2175 return -EFAULT;
2177 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2179 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2180 if (stat)
2181 return stat;
2183 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2184 return cdrom_mode_select(cdi, &cgc);
2187 static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2189 struct packet_command cgc;
2190 char buffer[16];
2191 int stat;
2192 char spindown;
2194 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2196 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2197 if (stat)
2198 return stat;
2200 spindown = buffer[11] & 0x0f;
2201 if (copy_to_user((void __user *)arg, &spindown, sizeof (char)))
2202 return -EFAULT;
2203 return 0;
2206 static int idecd_ioctl (struct inode *inode, struct file *file,
2207 unsigned int cmd, unsigned long arg)
2209 struct block_device *bdev = inode->i_bdev;
2210 struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
2211 int err;
2213 switch (cmd) {
2214 case CDROMSETSPINDOWN:
2215 return idecd_set_spindown(&info->devinfo, arg);
2216 case CDROMGETSPINDOWN:
2217 return idecd_get_spindown(&info->devinfo, arg);
2218 default:
2219 break;
2222 err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
2223 if (err == -EINVAL)
2224 err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
2226 return err;
2229 static int idecd_media_changed(struct gendisk *disk)
2231 struct cdrom_info *info = ide_cd_g(disk);
2232 return cdrom_media_changed(&info->devinfo);
2235 static int idecd_revalidate_disk(struct gendisk *disk)
2237 struct cdrom_info *info = ide_cd_g(disk);
2238 struct request_sense sense;
2240 ide_cd_read_toc(info->drive, &sense);
2242 return 0;
2245 static struct block_device_operations idecd_ops = {
2246 .owner = THIS_MODULE,
2247 .open = idecd_open,
2248 .release = idecd_release,
2249 .ioctl = idecd_ioctl,
2250 .media_changed = idecd_media_changed,
2251 .revalidate_disk= idecd_revalidate_disk
2254 /* options */
2255 static char *ignore = NULL;
2257 module_param(ignore, charp, 0400);
2258 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2260 static int ide_cd_probe(ide_drive_t *drive)
2262 struct cdrom_info *info;
2263 struct gendisk *g;
2264 struct request_sense sense;
2266 if (!strstr("ide-cdrom", drive->driver_req))
2267 goto failed;
2268 if (!drive->present)
2269 goto failed;
2270 if (drive->media != ide_cdrom && drive->media != ide_optical)
2271 goto failed;
2272 /* skip drives that we were told to ignore */
2273 if (ignore != NULL) {
2274 if (strstr(ignore, drive->name)) {
2275 printk(KERN_INFO "ide-cd: ignoring drive %s\n", drive->name);
2276 goto failed;
2279 if (drive->scsi) {
2280 printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
2281 goto failed;
2283 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
2284 if (info == NULL) {
2285 printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
2286 goto failed;
2289 g = alloc_disk(1 << PARTN_BITS);
2290 if (!g)
2291 goto out_free_cd;
2293 ide_init_disk(g, drive);
2295 ide_proc_register_driver(drive, &ide_cdrom_driver);
2297 kref_init(&info->kref);
2299 info->drive = drive;
2300 info->driver = &ide_cdrom_driver;
2301 info->disk = g;
2303 g->private_data = &info->driver;
2305 drive->driver_data = info;
2307 g->minors = 1;
2308 g->driverfs_dev = &drive->gendev;
2309 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
2310 if (ide_cdrom_setup(drive)) {
2311 ide_proc_unregister_driver(drive, &ide_cdrom_driver);
2312 ide_cd_release(&info->kref);
2313 goto failed;
2316 ide_cd_read_toc(drive, &sense);
2317 g->fops = &idecd_ops;
2318 g->flags |= GENHD_FL_REMOVABLE;
2319 add_disk(g);
2320 return 0;
2322 out_free_cd:
2323 kfree(info);
2324 failed:
2325 return -ENODEV;
2328 static void __exit ide_cdrom_exit(void)
2330 driver_unregister(&ide_cdrom_driver.gen_driver);
2333 static int __init ide_cdrom_init(void)
2335 return driver_register(&ide_cdrom_driver.gen_driver);
2338 MODULE_ALIAS("ide:*m-cdrom*");
2339 MODULE_ALIAS("ide-cd");
2340 module_init(ide_cdrom_init);
2341 module_exit(ide_cdrom_exit);
2342 MODULE_LICENSE("GPL");