V4L/DVB: af9015: add new USB ID for KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ide / ide-cd.c
blob6a9a769bffc1993736064c54570aeeeef0c1b989
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-2009 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. ;-)
16 * Documentation:
17 * Mt. Fuji (SFF8090 version 4) and ATAPI (SFF-8020i rev 2.6) standards.
19 * For historical changelog please see:
20 * Documentation/ide/ChangeLog.ide-cd.1994-2004
23 #define DRV_NAME "ide-cd"
24 #define PFX DRV_NAME ": "
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 /* For SCSI -> ATAPI command conversion */
43 #include <scsi/scsi.h>
45 #include <linux/irq.h>
46 #include <linux/io.h>
47 #include <asm/byteorder.h>
48 #include <linux/uaccess.h>
49 #include <asm/unaligned.h>
51 #include "ide-cd.h"
53 static DEFINE_MUTEX(idecd_ref_mutex);
55 static void ide_cd_release(struct device *);
57 static struct cdrom_info *ide_cd_get(struct gendisk *disk)
59 struct cdrom_info *cd = NULL;
61 mutex_lock(&idecd_ref_mutex);
62 cd = ide_drv_g(disk, cdrom_info);
63 if (cd) {
64 if (ide_device_get(cd->drive))
65 cd = NULL;
66 else
67 get_device(&cd->dev);
70 mutex_unlock(&idecd_ref_mutex);
71 return cd;
74 static void ide_cd_put(struct cdrom_info *cd)
76 ide_drive_t *drive = cd->drive;
78 mutex_lock(&idecd_ref_mutex);
79 put_device(&cd->dev);
80 ide_device_put(drive);
81 mutex_unlock(&idecd_ref_mutex);
85 * Generic packet command support and error handling routines.
88 /* Mark that we've seen a media change and invalidate our internal buffers. */
89 static void cdrom_saw_media_change(ide_drive_t *drive)
91 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
92 drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
95 static int cdrom_log_sense(ide_drive_t *drive, struct request *rq)
97 struct request_sense *sense = &drive->sense_data;
98 int log = 0;
100 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
101 return 0;
103 ide_debug_log(IDE_DBG_SENSE, "sense_key: 0x%x", sense->sense_key);
105 switch (sense->sense_key) {
106 case NO_SENSE:
107 case RECOVERED_ERROR:
108 break;
109 case NOT_READY:
111 * don't care about tray state messages for e.g. capacity
112 * commands or in-progress or becoming ready
114 if (sense->asc == 0x3a || sense->asc == 0x04)
115 break;
116 log = 1;
117 break;
118 case ILLEGAL_REQUEST:
120 * don't log START_STOP unit with LoEj set, since we cannot
121 * reliably check if drive can auto-close
123 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
124 break;
125 log = 1;
126 break;
127 case UNIT_ATTENTION:
129 * Make good and sure we've seen this potential media change.
130 * Some drives (i.e. Creative) fail to present the correct sense
131 * key in the error register.
133 cdrom_saw_media_change(drive);
134 break;
135 default:
136 log = 1;
137 break;
139 return log;
142 static void cdrom_analyze_sense_data(ide_drive_t *drive,
143 struct request *failed_command)
145 struct request_sense *sense = &drive->sense_data;
146 struct cdrom_info *info = drive->driver_data;
147 unsigned long sector;
148 unsigned long bio_sectors;
150 ide_debug_log(IDE_DBG_SENSE, "error_code: 0x%x, sense_key: 0x%x",
151 sense->error_code, sense->sense_key);
153 if (failed_command)
154 ide_debug_log(IDE_DBG_SENSE, "failed cmd: 0x%x",
155 failed_command->cmd[0]);
157 if (!cdrom_log_sense(drive, failed_command))
158 return;
161 * If a read toc is executed for a CD-R or CD-RW medium where the first
162 * toc has not been recorded yet, it will fail with 05/24/00 (which is a
163 * confusing error)
165 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
166 if (sense->sense_key == 0x05 && sense->asc == 0x24)
167 return;
169 /* current error */
170 if (sense->error_code == 0x70) {
171 switch (sense->sense_key) {
172 case MEDIUM_ERROR:
173 case VOLUME_OVERFLOW:
174 case ILLEGAL_REQUEST:
175 if (!sense->valid)
176 break;
177 if (failed_command == NULL ||
178 !blk_fs_request(failed_command))
179 break;
180 sector = (sense->information[0] << 24) |
181 (sense->information[1] << 16) |
182 (sense->information[2] << 8) |
183 (sense->information[3]);
185 if (queue_logical_block_size(drive->queue) == 2048)
186 /* device sector size is 2K */
187 sector <<= 2;
189 bio_sectors = max(bio_sectors(failed_command->bio), 4U);
190 sector &= ~(bio_sectors - 1);
193 * The SCSI specification allows for the value
194 * returned by READ CAPACITY to be up to 75 2K
195 * sectors past the last readable block.
196 * Therefore, if we hit a medium error within the
197 * last 75 2K sectors, we decrease the saved size
198 * value.
200 if (sector < get_capacity(info->disk) &&
201 drive->probed_capacity - sector < 4 * 75)
202 set_capacity(info->disk, sector);
206 ide_cd_log_error(drive->name, failed_command, sense);
209 static void ide_cd_complete_failed_rq(ide_drive_t *drive, struct request *rq)
212 * For REQ_TYPE_SENSE, "rq->special" points to the original
213 * failed request. Also, the sense data should be read
214 * directly from rq which might be different from the original
215 * sense buffer if it got copied during mapping.
217 struct request *failed = (struct request *)rq->special;
218 void *sense = bio_data(rq->bio);
220 if (failed) {
221 if (failed->sense) {
223 * Sense is always read into drive->sense_data.
224 * Copy back if the failed request has its
225 * sense pointer set.
227 memcpy(failed->sense, sense, 18);
228 failed->sense_len = rq->sense_len;
230 cdrom_analyze_sense_data(drive, failed);
232 if (ide_end_rq(drive, failed, -EIO, blk_rq_bytes(failed)))
233 BUG();
234 } else
235 cdrom_analyze_sense_data(drive, NULL);
240 * Allow the drive 5 seconds to recover; some devices will return NOT_READY
241 * while flushing data from cache.
243 * returns: 0 failed (write timeout expired)
244 * 1 success
246 static int ide_cd_breathe(ide_drive_t *drive, struct request *rq)
249 struct cdrom_info *info = drive->driver_data;
251 if (!rq->errors)
252 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
254 rq->errors = 1;
256 if (time_after(jiffies, info->write_timeout))
257 return 0;
258 else {
259 struct request_queue *q = drive->queue;
260 unsigned long flags;
263 * take a breather relying on the unplug timer to kick us again
266 spin_lock_irqsave(q->queue_lock, flags);
267 blk_plug_device(q);
268 spin_unlock_irqrestore(q->queue_lock, flags);
270 return 1;
275 * Returns:
276 * 0: if the request should be continued.
277 * 1: if the request will be going through error recovery.
278 * 2: if the request should be ended.
280 static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
282 ide_hwif_t *hwif = drive->hwif;
283 struct request *rq = hwif->rq;
284 int err, sense_key, do_end_request = 0;
286 /* get the IDE error register */
287 err = ide_read_error(drive);
288 sense_key = err >> 4;
290 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, rq->cmd_type: 0x%x, err: 0x%x, "
291 "stat 0x%x",
292 rq->cmd[0], rq->cmd_type, err, stat);
294 if (blk_sense_request(rq)) {
296 * We got an error trying to get sense info from the drive
297 * (probably while trying to recover from a former error).
298 * Just give up.
300 rq->cmd_flags |= REQ_FAILED;
301 return 2;
304 /* if we have an error, pass CHECK_CONDITION as the SCSI status byte */
305 if (blk_pc_request(rq) && !rq->errors)
306 rq->errors = SAM_STAT_CHECK_CONDITION;
308 if (blk_noretry_request(rq))
309 do_end_request = 1;
311 switch (sense_key) {
312 case NOT_READY:
313 if (blk_fs_request(rq) && rq_data_dir(rq) == WRITE) {
314 if (ide_cd_breathe(drive, rq))
315 return 1;
316 } else {
317 cdrom_saw_media_change(drive);
319 if (blk_fs_request(rq) && !blk_rq_quiet(rq))
320 printk(KERN_ERR PFX "%s: tray open\n",
321 drive->name);
323 do_end_request = 1;
324 break;
325 case UNIT_ATTENTION:
326 cdrom_saw_media_change(drive);
328 if (blk_fs_request(rq) == 0)
329 return 0;
332 * Arrange to retry the request but be sure to give up if we've
333 * retried too many times.
335 if (++rq->errors > ERROR_MAX)
336 do_end_request = 1;
337 break;
338 case ILLEGAL_REQUEST:
340 * Don't print error message for this condition -- SFF8090i
341 * indicates that 5/24/00 is the correct response to a request
342 * to close the tray if the drive doesn't have that capability.
344 * cdrom_log_sense() knows this!
346 if (rq->cmd[0] == GPCMD_START_STOP_UNIT)
347 break;
348 /* fall-through */
349 case DATA_PROTECT:
351 * No point in retrying after an illegal request or data
352 * protect error.
354 if (!blk_rq_quiet(rq))
355 ide_dump_status(drive, "command error", stat);
356 do_end_request = 1;
357 break;
358 case MEDIUM_ERROR:
360 * No point in re-trying a zillion times on a bad sector.
361 * If we got here the error is not correctable.
363 if (!blk_rq_quiet(rq))
364 ide_dump_status(drive, "media error "
365 "(bad sector)", stat);
366 do_end_request = 1;
367 break;
368 case BLANK_CHECK:
369 /* disk appears blank? */
370 if (!blk_rq_quiet(rq))
371 ide_dump_status(drive, "media error (blank)",
372 stat);
373 do_end_request = 1;
374 break;
375 default:
376 if (blk_fs_request(rq) == 0)
377 break;
378 if (err & ~ATA_ABORTED) {
379 /* go to the default handler for other errors */
380 ide_error(drive, "cdrom_decode_status", stat);
381 return 1;
382 } else if (++rq->errors > ERROR_MAX)
383 /* we've racked up too many retries, abort */
384 do_end_request = 1;
387 if (blk_fs_request(rq) == 0) {
388 rq->cmd_flags |= REQ_FAILED;
389 do_end_request = 1;
393 * End a request through request sense analysis when we have sense data.
394 * We need this in order to perform end of media processing.
396 if (do_end_request)
397 goto end_request;
399 /* if we got a CHECK_CONDITION status, queue a request sense command */
400 if (stat & ATA_ERR)
401 return ide_queue_sense_rq(drive, NULL) ? 2 : 1;
402 return 1;
404 end_request:
405 if (stat & ATA_ERR) {
406 hwif->rq = NULL;
407 return ide_queue_sense_rq(drive, rq) ? 2 : 1;
408 } else
409 return 2;
412 static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct ide_cmd *cmd)
414 struct request *rq = cmd->rq;
416 ide_debug_log(IDE_DBG_FUNC, "rq->cmd[0]: 0x%x", rq->cmd[0]);
419 * Some of the trailing request sense fields are optional,
420 * and some drives don't send them. Sigh.
422 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
423 cmd->nleft > 0 && cmd->nleft <= 5)
424 cmd->nleft = 0;
427 int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
428 int write, void *buffer, unsigned *bufflen,
429 struct request_sense *sense, int timeout,
430 unsigned int cmd_flags)
432 struct cdrom_info *info = drive->driver_data;
433 struct request_sense local_sense;
434 int retries = 10;
435 unsigned int flags = 0;
437 if (!sense)
438 sense = &local_sense;
440 ide_debug_log(IDE_DBG_PC, "cmd[0]: 0x%x, write: 0x%x, timeout: %d, "
441 "cmd_flags: 0x%x",
442 cmd[0], write, timeout, cmd_flags);
444 /* start of retry loop */
445 do {
446 struct request *rq;
447 int error;
449 rq = blk_get_request(drive->queue, write, __GFP_WAIT);
451 memcpy(rq->cmd, cmd, BLK_MAX_CDB);
452 rq->cmd_type = REQ_TYPE_ATA_PC;
453 rq->sense = sense;
454 rq->cmd_flags |= cmd_flags;
455 rq->timeout = timeout;
456 if (buffer) {
457 error = blk_rq_map_kern(drive->queue, rq, buffer,
458 *bufflen, GFP_NOIO);
459 if (error) {
460 blk_put_request(rq);
461 return error;
465 error = blk_execute_rq(drive->queue, info->disk, rq, 0);
467 if (buffer)
468 *bufflen = rq->resid_len;
470 flags = rq->cmd_flags;
471 blk_put_request(rq);
474 * FIXME: we should probably abort/retry or something in case of
475 * failure.
477 if (flags & REQ_FAILED) {
479 * The request failed. Retry if it was due to a unit
480 * attention status (usually means media was changed).
482 struct request_sense *reqbuf = sense;
484 if (reqbuf->sense_key == UNIT_ATTENTION)
485 cdrom_saw_media_change(drive);
486 else if (reqbuf->sense_key == NOT_READY &&
487 reqbuf->asc == 4 && reqbuf->ascq != 4) {
489 * The drive is in the process of loading
490 * a disk. Retry, but wait a little to give
491 * the drive time to complete the load.
493 ssleep(2);
494 } else {
495 /* otherwise, don't retry */
496 retries = 0;
498 --retries;
501 /* end of retry loop */
502 } while ((flags & REQ_FAILED) && retries >= 0);
504 /* return an error if the command failed */
505 return (flags & REQ_FAILED) ? -EIO : 0;
508 static void ide_cd_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
510 unsigned int nr_bytes = cmd->nbytes - cmd->nleft;
512 if (cmd->tf_flags & IDE_TFLAG_WRITE)
513 nr_bytes -= cmd->last_xfer_len;
515 if (nr_bytes > 0)
516 ide_complete_rq(drive, 0, nr_bytes);
519 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
521 ide_hwif_t *hwif = drive->hwif;
522 struct ide_cmd *cmd = &hwif->cmd;
523 struct request *rq = hwif->rq;
524 ide_expiry_t *expiry = NULL;
525 int dma_error = 0, dma, thislen, uptodate = 0;
526 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0, rc = 0;
527 int sense = blk_sense_request(rq);
528 unsigned int timeout;
529 u16 len;
530 u8 ireason, stat;
532 ide_debug_log(IDE_DBG_PC, "cmd: 0x%x, write: 0x%x", rq->cmd[0], write);
534 /* check for errors */
535 dma = drive->dma;
536 if (dma) {
537 drive->dma = 0;
538 drive->waiting_for_dma = 0;
539 dma_error = hwif->dma_ops->dma_end(drive);
540 ide_dma_unmap_sg(drive, cmd);
541 if (dma_error) {
542 printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
543 write ? "write" : "read");
544 ide_dma_off(drive);
548 /* check status */
549 stat = hwif->tp_ops->read_status(hwif);
551 if (!OK_STAT(stat, 0, BAD_R_STAT)) {
552 rc = cdrom_decode_status(drive, stat);
553 if (rc) {
554 if (rc == 2)
555 goto out_end;
556 return ide_stopped;
560 /* using dma, transfer is complete now */
561 if (dma) {
562 if (dma_error)
563 return ide_error(drive, "dma error", stat);
564 uptodate = 1;
565 goto out_end;
568 ide_read_bcount_and_ireason(drive, &len, &ireason);
570 thislen = blk_fs_request(rq) ? len : cmd->nleft;
571 if (thislen > len)
572 thislen = len;
574 ide_debug_log(IDE_DBG_PC, "DRQ: stat: 0x%x, thislen: %d",
575 stat, thislen);
577 /* If DRQ is clear, the command has completed. */
578 if ((stat & ATA_DRQ) == 0) {
579 if (blk_fs_request(rq)) {
581 * If we're not done reading/writing, complain.
582 * Otherwise, complete the command normally.
584 uptodate = 1;
585 if (cmd->nleft > 0) {
586 printk(KERN_ERR PFX "%s: %s: data underrun "
587 "(%u bytes)\n", drive->name, __func__,
588 cmd->nleft);
589 if (!write)
590 rq->cmd_flags |= REQ_FAILED;
591 uptodate = 0;
593 } else if (!blk_pc_request(rq)) {
594 ide_cd_request_sense_fixup(drive, cmd);
596 uptodate = cmd->nleft ? 0 : 1;
599 * suck out the remaining bytes from the drive in an
600 * attempt to complete the data xfer. (see BZ#13399)
602 if (!(stat & ATA_ERR) && !uptodate && thislen) {
603 ide_pio_bytes(drive, cmd, write, thislen);
604 uptodate = cmd->nleft ? 0 : 1;
607 if (!uptodate)
608 rq->cmd_flags |= REQ_FAILED;
610 goto out_end;
613 rc = ide_check_ireason(drive, rq, len, ireason, write);
614 if (rc)
615 goto out_end;
617 cmd->last_xfer_len = 0;
619 ide_debug_log(IDE_DBG_PC, "data transfer, rq->cmd_type: 0x%x, "
620 "ireason: 0x%x",
621 rq->cmd_type, ireason);
623 /* transfer data */
624 while (thislen > 0) {
625 int blen = min_t(int, thislen, cmd->nleft);
627 if (cmd->nleft == 0)
628 break;
630 ide_pio_bytes(drive, cmd, write, blen);
631 cmd->last_xfer_len += blen;
633 thislen -= blen;
634 len -= blen;
636 if (sense && write == 0)
637 rq->sense_len += blen;
640 /* pad, if necessary */
641 if (len > 0) {
642 if (blk_fs_request(rq) == 0 || write == 0)
643 ide_pad_transfer(drive, write, len);
644 else {
645 printk(KERN_ERR PFX "%s: confused, missing data\n",
646 drive->name);
647 blk_dump_rq_flags(rq, "cdrom_newpc_intr");
651 if (blk_pc_request(rq)) {
652 timeout = rq->timeout;
653 } else {
654 timeout = ATAPI_WAIT_PC;
655 if (!blk_fs_request(rq))
656 expiry = ide_cd_expiry;
659 hwif->expiry = expiry;
660 ide_set_handler(drive, cdrom_newpc_intr, timeout);
661 return ide_started;
663 out_end:
664 if (blk_pc_request(rq) && rc == 0) {
665 rq->resid_len = 0;
666 blk_end_request_all(rq, 0);
667 hwif->rq = NULL;
668 } else {
669 if (sense && uptodate)
670 ide_cd_complete_failed_rq(drive, rq);
672 if (blk_fs_request(rq)) {
673 if (cmd->nleft == 0)
674 uptodate = 1;
675 } else {
676 if (uptodate <= 0 && rq->errors == 0)
677 rq->errors = -EIO;
680 if (uptodate == 0 && rq->bio)
681 ide_cd_error_cmd(drive, cmd);
683 /* make sure it's fully ended */
684 if (blk_fs_request(rq) == 0) {
685 rq->resid_len -= cmd->nbytes - cmd->nleft;
686 if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE))
687 rq->resid_len += cmd->last_xfer_len;
690 ide_complete_rq(drive, uptodate ? 0 : -EIO, blk_rq_bytes(rq));
692 if (sense && rc == 2)
693 ide_error(drive, "request sense failure", stat);
695 return ide_stopped;
698 static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
700 struct cdrom_info *cd = drive->driver_data;
701 struct request_queue *q = drive->queue;
702 int write = rq_data_dir(rq) == WRITE;
703 unsigned short sectors_per_frame =
704 queue_logical_block_size(q) >> SECTOR_BITS;
706 ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, rq->cmd_flags: 0x%x, "
707 "secs_per_frame: %u",
708 rq->cmd[0], rq->cmd_flags, sectors_per_frame);
710 if (write) {
711 /* disk has become write protected */
712 if (get_disk_ro(cd->disk))
713 return ide_stopped;
714 } else {
716 * We may be retrying this request after an error. Fix up any
717 * weirdness which might be present in the request packet.
719 q->prep_rq_fn(q, rq);
722 /* fs requests *must* be hardware frame aligned */
723 if ((blk_rq_sectors(rq) & (sectors_per_frame - 1)) ||
724 (blk_rq_pos(rq) & (sectors_per_frame - 1)))
725 return ide_stopped;
727 /* use DMA, if possible */
728 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
730 if (write)
731 cd->devinfo.media_written = 1;
733 rq->timeout = ATAPI_WAIT_PC;
735 return ide_started;
738 static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
741 ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x",
742 rq->cmd[0], rq->cmd_type);
744 if (blk_pc_request(rq))
745 rq->cmd_flags |= REQ_QUIET;
746 else
747 rq->cmd_flags &= ~REQ_FAILED;
749 drive->dma = 0;
751 /* sg request */
752 if (rq->bio) {
753 struct request_queue *q = drive->queue;
754 char *buf = bio_data(rq->bio);
755 unsigned int alignment;
757 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
760 * check if dma is safe
762 * NOTE! The "len" and "addr" checks should possibly have
763 * separate masks.
765 alignment = queue_dma_alignment(q) | q->dma_pad_mask;
766 if ((unsigned long)buf & alignment
767 || blk_rq_bytes(rq) & q->dma_pad_mask
768 || object_is_on_stack(buf))
769 drive->dma = 0;
773 static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
774 sector_t block)
776 struct ide_cmd cmd;
777 int uptodate = 0, nsectors;
779 ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, block: %llu",
780 rq->cmd[0], (unsigned long long)block);
782 if (drive->debug_mask & IDE_DBG_RQ)
783 blk_dump_rq_flags(rq, "ide_cd_do_request");
785 if (blk_fs_request(rq)) {
786 if (cdrom_start_rw(drive, rq) == ide_stopped)
787 goto out_end;
788 } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
789 rq->cmd_type == REQ_TYPE_ATA_PC) {
790 if (!rq->timeout)
791 rq->timeout = ATAPI_WAIT_PC;
793 cdrom_do_block_pc(drive, rq);
794 } else if (blk_special_request(rq)) {
795 /* right now this can only be a reset... */
796 uptodate = 1;
797 goto out_end;
798 } else
799 BUG();
801 /* prepare sense request for this command */
802 ide_prep_sense(drive, rq);
804 memset(&cmd, 0, sizeof(cmd));
806 if (rq_data_dir(rq))
807 cmd.tf_flags |= IDE_TFLAG_WRITE;
809 cmd.rq = rq;
811 if (blk_fs_request(rq) || blk_rq_bytes(rq)) {
812 ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
813 ide_map_sg(drive, &cmd);
816 return ide_issue_pc(drive, &cmd);
817 out_end:
818 nsectors = blk_rq_sectors(rq);
820 if (nsectors == 0)
821 nsectors = 1;
823 ide_complete_rq(drive, uptodate ? 0 : -EIO, nsectors << 9);
825 return ide_stopped;
829 * Ioctl handling.
831 * Routines which queue packet commands take as a final argument a pointer to a
832 * request_sense struct. If execution of the command results in an error with a
833 * CHECK CONDITION status, this structure will be filled with the results of the
834 * subsequent request sense command. The pointer can also be NULL, in which case
835 * no sense information is returned.
837 static void msf_from_bcd(struct atapi_msf *msf)
839 msf->minute = bcd2bin(msf->minute);
840 msf->second = bcd2bin(msf->second);
841 msf->frame = bcd2bin(msf->frame);
844 int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
846 struct cdrom_info *info = drive->driver_data;
847 struct cdrom_device_info *cdi = &info->devinfo;
848 unsigned char cmd[BLK_MAX_CDB];
850 ide_debug_log(IDE_DBG_FUNC, "enter");
852 memset(cmd, 0, BLK_MAX_CDB);
853 cmd[0] = GPCMD_TEST_UNIT_READY;
856 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs
857 * instead of supporting the LOAD_UNLOAD opcode.
859 cmd[7] = cdi->sanyo_slot % 3;
861 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, REQ_QUIET);
864 static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
865 unsigned long *sectors_per_frame,
866 struct request_sense *sense)
868 struct {
869 __be32 lba;
870 __be32 blocklen;
871 } capbuf;
873 int stat;
874 unsigned char cmd[BLK_MAX_CDB];
875 unsigned len = sizeof(capbuf);
876 u32 blocklen;
878 ide_debug_log(IDE_DBG_FUNC, "enter");
880 memset(cmd, 0, BLK_MAX_CDB);
881 cmd[0] = GPCMD_READ_CDVD_CAPACITY;
883 stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, sense, 0,
884 REQ_QUIET);
885 if (stat)
886 return stat;
889 * Sanity check the given block size, in so far as making
890 * sure the sectors_per_frame we give to the caller won't
891 * end up being bogus.
893 blocklen = be32_to_cpu(capbuf.blocklen);
894 blocklen = (blocklen >> SECTOR_BITS) << SECTOR_BITS;
895 switch (blocklen) {
896 case 512:
897 case 1024:
898 case 2048:
899 case 4096:
900 break;
901 default:
902 printk_once(KERN_ERR PFX "%s: weird block size %u; "
903 "setting default block size to 2048\n",
904 drive->name, blocklen);
905 blocklen = 2048;
906 break;
909 *capacity = 1 + be32_to_cpu(capbuf.lba);
910 *sectors_per_frame = blocklen >> SECTOR_BITS;
912 ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu",
913 *capacity, *sectors_per_frame);
915 return 0;
918 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
919 int format, char *buf, int buflen,
920 struct request_sense *sense)
922 unsigned char cmd[BLK_MAX_CDB];
924 ide_debug_log(IDE_DBG_FUNC, "enter");
926 memset(cmd, 0, BLK_MAX_CDB);
928 cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
929 cmd[6] = trackno;
930 cmd[7] = (buflen >> 8);
931 cmd[8] = (buflen & 0xff);
932 cmd[9] = (format << 6);
934 if (msf_flag)
935 cmd[1] = 2;
937 return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, sense, 0, REQ_QUIET);
940 /* Try to read the entire TOC for the disk into our internal buffer. */
941 int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
943 int stat, ntracks, i;
944 struct cdrom_info *info = drive->driver_data;
945 struct cdrom_device_info *cdi = &info->devinfo;
946 struct atapi_toc *toc = info->toc;
947 struct {
948 struct atapi_toc_header hdr;
949 struct atapi_toc_entry ent;
950 } ms_tmp;
951 long last_written;
952 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
954 ide_debug_log(IDE_DBG_FUNC, "enter");
956 if (toc == NULL) {
957 /* try to allocate space */
958 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
959 if (toc == NULL) {
960 printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n",
961 drive->name);
962 return -ENOMEM;
964 info->toc = toc;
968 * Check to see if the existing data is still valid. If it is,
969 * just return.
971 (void) cdrom_check_status(drive, sense);
973 if (drive->atapi_flags & IDE_AFLAG_TOC_VALID)
974 return 0;
976 /* try to get the total cdrom capacity and sector size */
977 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
978 sense);
979 if (stat)
980 toc->capacity = 0x1fffff;
982 set_capacity(info->disk, toc->capacity * sectors_per_frame);
983 /* save a private copy of the TOC capacity for error handling */
984 drive->probed_capacity = toc->capacity * sectors_per_frame;
986 blk_queue_logical_block_size(drive->queue,
987 sectors_per_frame << SECTOR_BITS);
989 /* first read just the header, so we know how long the TOC is */
990 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
991 sizeof(struct atapi_toc_header), sense);
992 if (stat)
993 return stat;
995 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
996 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
997 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
1000 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1001 if (ntracks <= 0)
1002 return -EIO;
1003 if (ntracks > MAX_TRACKS)
1004 ntracks = MAX_TRACKS;
1006 /* now read the whole schmeer */
1007 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1008 (char *)&toc->hdr,
1009 sizeof(struct atapi_toc_header) +
1010 (ntracks + 1) *
1011 sizeof(struct atapi_toc_entry), sense);
1013 if (stat && toc->hdr.first_track > 1) {
1015 * Cds with CDI tracks only don't have any TOC entries, despite
1016 * of this the returned values are
1017 * first_track == last_track = number of CDI tracks + 1,
1018 * so that this case is indistinguishable from the same layout
1019 * plus an additional audio track. If we get an error for the
1020 * regular case, we assume a CDI without additional audio
1021 * tracks. In this case the readable TOC is empty (CDI tracks
1022 * are not included) and only holds the Leadout entry.
1024 * Heiko Eißfeldt.
1026 ntracks = 0;
1027 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1028 (char *)&toc->hdr,
1029 sizeof(struct atapi_toc_header) +
1030 (ntracks + 1) *
1031 sizeof(struct atapi_toc_entry),
1032 sense);
1033 if (stat)
1034 return stat;
1036 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
1037 toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT);
1038 toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT);
1039 } else {
1040 toc->hdr.first_track = CDROM_LEADOUT;
1041 toc->hdr.last_track = CDROM_LEADOUT;
1045 if (stat)
1046 return stat;
1048 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
1050 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
1051 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1052 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
1055 for (i = 0; i <= ntracks; i++) {
1056 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
1057 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD)
1058 toc->ent[i].track = bcd2bin(toc->ent[i].track);
1059 msf_from_bcd(&toc->ent[i].addr.msf);
1061 toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute,
1062 toc->ent[i].addr.msf.second,
1063 toc->ent[i].addr.msf.frame);
1066 if (toc->hdr.first_track != CDROM_LEADOUT) {
1067 /* read the multisession information */
1068 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
1069 sizeof(ms_tmp), sense);
1070 if (stat)
1071 return stat;
1073 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1074 } else {
1075 ms_tmp.hdr.last_track = CDROM_LEADOUT;
1076 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
1077 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1080 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
1081 /* re-read multisession information using MSF format */
1082 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1083 sizeof(ms_tmp), sense);
1084 if (stat)
1085 return stat;
1087 msf_from_bcd(&ms_tmp.ent.addr.msf);
1088 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
1089 ms_tmp.ent.addr.msf.second,
1090 ms_tmp.ent.addr.msf.frame);
1093 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1095 /* now try to get the total cdrom capacity */
1096 stat = cdrom_get_last_written(cdi, &last_written);
1097 if (!stat && (last_written > toc->capacity)) {
1098 toc->capacity = last_written;
1099 set_capacity(info->disk, toc->capacity * sectors_per_frame);
1100 drive->probed_capacity = toc->capacity * sectors_per_frame;
1103 /* Remember that we've read this stuff. */
1104 drive->atapi_flags |= IDE_AFLAG_TOC_VALID;
1106 return 0;
1109 int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
1111 struct cdrom_info *info = drive->driver_data;
1112 struct cdrom_device_info *cdi = &info->devinfo;
1113 struct packet_command cgc;
1114 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
1116 ide_debug_log(IDE_DBG_FUNC, "enter");
1118 if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
1119 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
1121 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
1122 do {
1123 /* we seem to get stat=0x01,err=0x00 the first time (??) */
1124 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1125 if (!stat)
1126 break;
1127 } while (--attempts);
1128 return stat;
1131 void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
1133 struct cdrom_info *cd = drive->driver_data;
1134 u16 curspeed, maxspeed;
1136 ide_debug_log(IDE_DBG_FUNC, "enter");
1138 if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
1139 curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
1140 maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]);
1141 } else {
1142 curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]);
1143 maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
1146 ide_debug_log(IDE_DBG_PROBE, "curspeed: %u, maxspeed: %u",
1147 curspeed, maxspeed);
1149 cd->current_speed = (curspeed + (176/2)) / 176;
1150 cd->max_speed = (maxspeed + (176/2)) / 176;
1153 #define IDE_CD_CAPABILITIES \
1154 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1155 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1156 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1157 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1158 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
1160 static struct cdrom_device_ops ide_cdrom_dops = {
1161 .open = ide_cdrom_open_real,
1162 .release = ide_cdrom_release_real,
1163 .drive_status = ide_cdrom_drive_status,
1164 .media_changed = ide_cdrom_check_media_change_real,
1165 .tray_move = ide_cdrom_tray_move,
1166 .lock_door = ide_cdrom_lock_door,
1167 .select_speed = ide_cdrom_select_speed,
1168 .get_last_session = ide_cdrom_get_last_session,
1169 .get_mcn = ide_cdrom_get_mcn,
1170 .reset = ide_cdrom_reset,
1171 .audio_ioctl = ide_cdrom_audio_ioctl,
1172 .capability = IDE_CD_CAPABILITIES,
1173 .generic_packet = ide_cdrom_packet,
1176 static int ide_cdrom_register(ide_drive_t *drive, int nslots)
1178 struct cdrom_info *info = drive->driver_data;
1179 struct cdrom_device_info *devinfo = &info->devinfo;
1181 ide_debug_log(IDE_DBG_PROBE, "nslots: %d", nslots);
1183 devinfo->ops = &ide_cdrom_dops;
1184 devinfo->speed = info->current_speed;
1185 devinfo->capacity = nslots;
1186 devinfo->handle = drive;
1187 strcpy(devinfo->name, drive->name);
1189 if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT)
1190 devinfo->mask |= CDC_SELECT_SPEED;
1192 devinfo->disk = info->disk;
1193 return register_cdrom(devinfo);
1196 static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
1198 struct cdrom_info *cd = drive->driver_data;
1199 struct cdrom_device_info *cdi = &cd->devinfo;
1200 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1201 mechtype_t mechtype;
1202 int nslots = 1;
1204 ide_debug_log(IDE_DBG_PROBE, "media: 0x%x, atapi_flags: 0x%lx",
1205 drive->media, drive->atapi_flags);
1207 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1208 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1209 CDC_MO_DRIVE | CDC_RAM);
1211 if (drive->media == ide_optical) {
1212 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
1213 printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n",
1214 drive->name);
1215 return nslots;
1218 if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) {
1219 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
1220 cdi->mask &= ~CDC_PLAY_AUDIO;
1221 return nslots;
1225 * We have to cheat a little here. the packet will eventually be queued
1226 * with ide_cdrom_packet(), which extracts the drive from cdi->handle.
1227 * Since this device hasn't been registered with the Uniform layer yet,
1228 * it can't do this. Same goes for cdi->ops.
1230 cdi->handle = drive;
1231 cdi->ops = &ide_cdrom_dops;
1233 if (ide_cdrom_get_capabilities(drive, buf))
1234 return 0;
1236 if ((buf[8 + 6] & 0x01) == 0)
1237 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1238 if (buf[8 + 6] & 0x08)
1239 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
1240 if (buf[8 + 3] & 0x01)
1241 cdi->mask &= ~CDC_CD_R;
1242 if (buf[8 + 3] & 0x02)
1243 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
1244 if (buf[8 + 2] & 0x38)
1245 cdi->mask &= ~CDC_DVD;
1246 if (buf[8 + 3] & 0x20)
1247 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
1248 if (buf[8 + 3] & 0x10)
1249 cdi->mask &= ~CDC_DVD_R;
1250 if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK))
1251 cdi->mask &= ~CDC_PLAY_AUDIO;
1253 mechtype = buf[8 + 6] >> 5;
1254 if (mechtype == mechtype_caddy ||
1255 mechtype == mechtype_popup ||
1256 (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE))
1257 cdi->mask |= CDC_CLOSE_TRAY;
1259 if (cdi->sanyo_slot > 0) {
1260 cdi->mask &= ~CDC_SELECT_DISC;
1261 nslots = 3;
1262 } else if (mechtype == mechtype_individual_changer ||
1263 mechtype == mechtype_cartridge_changer) {
1264 nslots = cdrom_number_of_slots(cdi);
1265 if (nslots > 1)
1266 cdi->mask &= ~CDC_SELECT_DISC;
1269 ide_cdrom_update_speed(drive, buf);
1271 printk(KERN_INFO PFX "%s: ATAPI", drive->name);
1273 /* don't print speed if the drive reported 0 */
1274 if (cd->max_speed)
1275 printk(KERN_CONT " %dX", cd->max_speed);
1277 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
1279 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1280 printk(KERN_CONT " DVD%s%s",
1281 (cdi->mask & CDC_DVD_R) ? "" : "-R",
1282 (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM");
1284 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1285 printk(KERN_CONT " CD%s%s",
1286 (cdi->mask & CDC_CD_R) ? "" : "-R",
1287 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
1289 if ((cdi->mask & CDC_SELECT_DISC) == 0)
1290 printk(KERN_CONT " changer w/%d slots", nslots);
1291 else
1292 printk(KERN_CONT " drive");
1294 printk(KERN_CONT ", %dkB Cache\n",
1295 be16_to_cpup((__be16 *)&buf[8 + 12]));
1297 return nslots;
1300 /* standard prep_rq_fn that builds 10 byte cmds */
1301 static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
1303 int hard_sect = queue_logical_block_size(q);
1304 long block = (long)blk_rq_pos(rq) / (hard_sect >> 9);
1305 unsigned long blocks = blk_rq_sectors(rq) / (hard_sect >> 9);
1307 memset(rq->cmd, 0, BLK_MAX_CDB);
1309 if (rq_data_dir(rq) == READ)
1310 rq->cmd[0] = GPCMD_READ_10;
1311 else
1312 rq->cmd[0] = GPCMD_WRITE_10;
1315 * fill in lba
1317 rq->cmd[2] = (block >> 24) & 0xff;
1318 rq->cmd[3] = (block >> 16) & 0xff;
1319 rq->cmd[4] = (block >> 8) & 0xff;
1320 rq->cmd[5] = block & 0xff;
1323 * and transfer length
1325 rq->cmd[7] = (blocks >> 8) & 0xff;
1326 rq->cmd[8] = blocks & 0xff;
1327 rq->cmd_len = 10;
1328 return BLKPREP_OK;
1332 * Most of the SCSI commands are supported directly by ATAPI devices.
1333 * This transform handles the few exceptions.
1335 static int ide_cdrom_prep_pc(struct request *rq)
1337 u8 *c = rq->cmd;
1339 /* transform 6-byte read/write commands to the 10-byte version */
1340 if (c[0] == READ_6 || c[0] == WRITE_6) {
1341 c[8] = c[4];
1342 c[5] = c[3];
1343 c[4] = c[2];
1344 c[3] = c[1] & 0x1f;
1345 c[2] = 0;
1346 c[1] &= 0xe0;
1347 c[0] += (READ_10 - READ_6);
1348 rq->cmd_len = 10;
1349 return BLKPREP_OK;
1353 * it's silly to pretend we understand 6-byte sense commands, just
1354 * reject with ILLEGAL_REQUEST and the caller should take the
1355 * appropriate action
1357 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
1358 rq->errors = ILLEGAL_REQUEST;
1359 return BLKPREP_KILL;
1362 return BLKPREP_OK;
1365 static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
1367 if (blk_fs_request(rq))
1368 return ide_cdrom_prep_fs(q, rq);
1369 else if (blk_pc_request(rq))
1370 return ide_cdrom_prep_pc(rq);
1372 return 0;
1375 struct cd_list_entry {
1376 const char *id_model;
1377 const char *id_firmware;
1378 unsigned int cd_flags;
1381 #ifdef CONFIG_IDE_PROC_FS
1382 static sector_t ide_cdrom_capacity(ide_drive_t *drive)
1384 unsigned long capacity, sectors_per_frame;
1386 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
1387 return 0;
1389 return capacity * sectors_per_frame;
1392 static int proc_idecd_read_capacity(char *page, char **start, off_t off,
1393 int count, int *eof, void *data)
1395 ide_drive_t *drive = data;
1396 int len;
1398 len = sprintf(page, "%llu\n", (long long)ide_cdrom_capacity(drive));
1399 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1402 static ide_proc_entry_t idecd_proc[] = {
1403 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
1404 { NULL, 0, NULL, NULL }
1407 static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
1409 return idecd_proc;
1412 static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
1414 return NULL;
1416 #endif
1418 static const struct cd_list_entry ide_cd_quirks_list[] = {
1419 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
1420 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT },
1421 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
1422 { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
1423 IDE_AFLAG_PRE_ATAPI12, },
1424 /* Vertos 300, some versions of this drive like to talk BCD. */
1425 { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, },
1426 /* Vertos 600 ESD. */
1427 { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, },
1429 * Sanyo 3 CD changer uses a non-standard command for CD changing
1430 * (by default standard ATAPI support for CD changers is used).
1432 { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD },
1433 { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD },
1434 { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD },
1435 /* Stingray 8X CD-ROM. */
1436 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
1438 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1439 * mode sense page capabilities size, but older drives break.
1441 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
1442 { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
1443 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
1444 { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS },
1446 * Some drives used by Apple don't advertise audio play
1447 * but they do support reading TOC & audio datas.
1449 { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1450 { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1451 { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1452 { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1453 { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1454 { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1455 { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE },
1456 { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE },
1457 { NULL, NULL, 0 }
1460 static unsigned int ide_cd_flags(u16 *id)
1462 const struct cd_list_entry *cle = ide_cd_quirks_list;
1464 while (cle->id_model) {
1465 if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
1466 (cle->id_firmware == NULL ||
1467 strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
1468 return cle->cd_flags;
1469 cle++;
1472 return 0;
1475 static int ide_cdrom_setup(ide_drive_t *drive)
1477 struct cdrom_info *cd = drive->driver_data;
1478 struct cdrom_device_info *cdi = &cd->devinfo;
1479 struct request_queue *q = drive->queue;
1480 u16 *id = drive->id;
1481 char *fw_rev = (char *)&id[ATA_ID_FW_REV];
1482 int nslots;
1484 ide_debug_log(IDE_DBG_PROBE, "enter");
1486 blk_queue_prep_rq(q, ide_cdrom_prep_fn);
1487 blk_queue_dma_alignment(q, 31);
1488 blk_queue_update_dma_pad(q, 15);
1490 q->unplug_delay = max((1 * HZ) / 1000, 1);
1492 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
1493 drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
1495 if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
1496 fw_rev[4] == '1' && fw_rev[6] <= '2')
1497 drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
1498 IDE_AFLAG_TOCADDR_AS_BCD);
1499 else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
1500 fw_rev[4] == '1' && fw_rev[6] <= '2')
1501 drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
1502 else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
1503 /* 3 => use CD in slot 0 */
1504 cdi->sanyo_slot = 3;
1506 nslots = ide_cdrom_probe_capabilities(drive);
1508 blk_queue_logical_block_size(q, CD_FRAMESIZE);
1510 if (ide_cdrom_register(drive, nslots)) {
1511 printk(KERN_ERR PFX "%s: %s failed to register device with the"
1512 " cdrom driver.\n", drive->name, __func__);
1513 cd->devinfo.handle = NULL;
1514 return 1;
1517 ide_proc_register_driver(drive, cd->driver);
1518 return 0;
1521 static void ide_cd_remove(ide_drive_t *drive)
1523 struct cdrom_info *info = drive->driver_data;
1525 ide_debug_log(IDE_DBG_FUNC, "enter");
1527 ide_proc_unregister_driver(drive, info->driver);
1528 device_del(&info->dev);
1529 del_gendisk(info->disk);
1531 mutex_lock(&idecd_ref_mutex);
1532 put_device(&info->dev);
1533 mutex_unlock(&idecd_ref_mutex);
1536 static void ide_cd_release(struct device *dev)
1538 struct cdrom_info *info = to_ide_drv(dev, cdrom_info);
1539 struct cdrom_device_info *devinfo = &info->devinfo;
1540 ide_drive_t *drive = info->drive;
1541 struct gendisk *g = info->disk;
1543 ide_debug_log(IDE_DBG_FUNC, "enter");
1545 kfree(info->toc);
1546 if (devinfo->handle == drive)
1547 unregister_cdrom(devinfo);
1548 drive->driver_data = NULL;
1549 blk_queue_prep_rq(drive->queue, NULL);
1550 g->private_data = NULL;
1551 put_disk(g);
1552 kfree(info);
1555 static int ide_cd_probe(ide_drive_t *);
1557 static struct ide_driver ide_cdrom_driver = {
1558 .gen_driver = {
1559 .owner = THIS_MODULE,
1560 .name = "ide-cdrom",
1561 .bus = &ide_bus_type,
1563 .probe = ide_cd_probe,
1564 .remove = ide_cd_remove,
1565 .version = IDECD_VERSION,
1566 .do_request = ide_cd_do_request,
1567 #ifdef CONFIG_IDE_PROC_FS
1568 .proc_entries = ide_cd_proc_entries,
1569 .proc_devsets = ide_cd_proc_devsets,
1570 #endif
1573 static int idecd_open(struct block_device *bdev, fmode_t mode)
1575 struct cdrom_info *info = ide_cd_get(bdev->bd_disk);
1576 int rc = -ENOMEM;
1578 if (!info)
1579 return -ENXIO;
1581 rc = cdrom_open(&info->devinfo, bdev, mode);
1583 if (rc < 0)
1584 ide_cd_put(info);
1586 return rc;
1589 static int idecd_release(struct gendisk *disk, fmode_t mode)
1591 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
1593 cdrom_release(&info->devinfo, mode);
1595 ide_cd_put(info);
1597 return 0;
1600 static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1602 struct packet_command cgc;
1603 char buffer[16];
1604 int stat;
1605 char spindown;
1607 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
1608 return -EFAULT;
1610 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1612 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1613 if (stat)
1614 return stat;
1616 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1617 return cdrom_mode_select(cdi, &cgc);
1620 static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1622 struct packet_command cgc;
1623 char buffer[16];
1624 int stat;
1625 char spindown;
1627 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1629 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1630 if (stat)
1631 return stat;
1633 spindown = buffer[11] & 0x0f;
1634 if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
1635 return -EFAULT;
1636 return 0;
1639 static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
1640 unsigned int cmd, unsigned long arg)
1642 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
1643 int err;
1645 switch (cmd) {
1646 case CDROMSETSPINDOWN:
1647 return idecd_set_spindown(&info->devinfo, arg);
1648 case CDROMGETSPINDOWN:
1649 return idecd_get_spindown(&info->devinfo, arg);
1650 default:
1651 break;
1654 err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
1655 if (err == -EINVAL)
1656 err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
1658 return err;
1661 static int idecd_media_changed(struct gendisk *disk)
1663 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
1664 return cdrom_media_changed(&info->devinfo);
1667 static int idecd_revalidate_disk(struct gendisk *disk)
1669 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
1670 struct request_sense sense;
1672 ide_cd_read_toc(info->drive, &sense);
1674 return 0;
1677 static struct block_device_operations idecd_ops = {
1678 .owner = THIS_MODULE,
1679 .open = idecd_open,
1680 .release = idecd_release,
1681 .locked_ioctl = idecd_ioctl,
1682 .media_changed = idecd_media_changed,
1683 .revalidate_disk = idecd_revalidate_disk
1686 /* module options */
1687 static unsigned long debug_mask;
1688 module_param(debug_mask, ulong, 0644);
1690 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
1692 static int ide_cd_probe(ide_drive_t *drive)
1694 struct cdrom_info *info;
1695 struct gendisk *g;
1696 struct request_sense sense;
1698 ide_debug_log(IDE_DBG_PROBE, "driver_req: %s, media: 0x%x",
1699 drive->driver_req, drive->media);
1701 if (!strstr("ide-cdrom", drive->driver_req))
1702 goto failed;
1704 if (drive->media != ide_cdrom && drive->media != ide_optical)
1705 goto failed;
1707 drive->debug_mask = debug_mask;
1708 drive->irq_handler = cdrom_newpc_intr;
1710 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
1711 if (info == NULL) {
1712 printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
1713 drive->name);
1714 goto failed;
1717 g = alloc_disk(1 << PARTN_BITS);
1718 if (!g)
1719 goto out_free_cd;
1721 ide_init_disk(g, drive);
1723 info->dev.parent = &drive->gendev;
1724 info->dev.release = ide_cd_release;
1725 dev_set_name(&info->dev, dev_name(&drive->gendev));
1727 if (device_register(&info->dev))
1728 goto out_free_disk;
1730 info->drive = drive;
1731 info->driver = &ide_cdrom_driver;
1732 info->disk = g;
1734 g->private_data = &info->driver;
1736 drive->driver_data = info;
1738 g->minors = 1;
1739 g->driverfs_dev = &drive->gendev;
1740 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
1741 if (ide_cdrom_setup(drive)) {
1742 put_device(&info->dev);
1743 goto failed;
1746 ide_cd_read_toc(drive, &sense);
1747 g->fops = &idecd_ops;
1748 g->flags |= GENHD_FL_REMOVABLE;
1749 add_disk(g);
1750 return 0;
1752 out_free_disk:
1753 put_disk(g);
1754 out_free_cd:
1755 kfree(info);
1756 failed:
1757 return -ENODEV;
1760 static void __exit ide_cdrom_exit(void)
1762 driver_unregister(&ide_cdrom_driver.gen_driver);
1765 static int __init ide_cdrom_init(void)
1767 printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
1768 return driver_register(&ide_cdrom_driver.gen_driver);
1771 MODULE_ALIAS("ide:*m-cdrom*");
1772 MODULE_ALIAS("ide-cd");
1773 module_init(ide_cdrom_init);
1774 module_exit(ide_cdrom_exit);
1775 MODULE_LICENSE("GPL");