initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / ide / ide-cd.c
blobcc6b66e34e37cfac5c6663d1b49070638a42cdfc
1 /*
2 * linux/drivers/ide/ide-cd.c
4 * Copyright (C) 1994, 1995, 1996 scott snyder <snyder@fnald0.fnal.gov>
5 * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
6 * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
8 * May be copied or modified under the terms of the GNU General Public
9 * License. See linux/COPYING for more information.
11 * ATAPI CD-ROM driver. To be used with ide.c.
12 * See Documentation/cdrom/ide-cd for usage information.
14 * Suggestions are welcome. Patches that work are more welcome though. ;-)
15 * For those wishing to work on this driver, please be sure you download
16 * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
17 * (SFF-8020i rev 2.6) standards. These documents can be obtained by
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 * Drives that deviate from these standards will be accommodated as much
23 * as possible via compile time or command-line options. Since I only have
24 * a few drives, you generally need to send me patches...
26 * ----------------------------------
27 * TO DO LIST:
28 * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
29 * boot
31 * ----------------------------------
32 * 1.00 Oct 31, 1994 -- Initial version.
33 * 1.01 Nov 2, 1994 -- Fixed problem with starting request in
34 * cdrom_check_status.
35 * 1.03 Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
36 * (from mlord) -- minor changes to cdrom_setup()
37 * -- renamed ide_dev_s to ide_drive_t, enable irq on command
38 * 2.00 Nov 27, 1994 -- Generalize packet command interface;
39 * add audio ioctls.
40 * 2.01 Dec 3, 1994 -- Rework packet command interface to handle devices
41 * which send an interrupt when ready for a command.
42 * 2.02 Dec 11, 1994 -- Cache the TOC in the driver.
43 * Don't use SCMD_PLAYAUDIO_TI; it's not included
44 * in the current version of ATAPI.
45 * Try to use LBA instead of track or MSF addressing
46 * when possible.
47 * Don't wait for READY_STAT.
48 * 2.03 Jan 10, 1995 -- Rewrite block read routines to handle block sizes
49 * other than 2k and to move multiple sectors in a
50 * single transaction.
51 * 2.04 Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
52 * Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
53 * help in figuring this out. Ditto for Acer and
54 * Aztech drives, which seem to have the same problem.
55 * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
56 * 2.05 Jun 8, 1995 -- Don't attempt to retry after an illegal request
57 * or data protect error.
58 * Use HWIF and DEV_HWIF macros as in ide.c.
59 * Always try to do a request_sense after
60 * a failed command.
61 * Include an option to give textual descriptions
62 * of ATAPI errors.
63 * Fix a bug in handling the sector cache which
64 * showed up if the drive returned data in 512 byte
65 * blocks (like Pioneer drives). Thanks to
66 * Richard Hirst <srh@gpt.co.uk> for diagnosing this.
67 * Properly supply the page number field in the
68 * MODE_SELECT command.
69 * PLAYAUDIO12 is broken on the Aztech; work around it.
70 * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
71 * (my apologies to Scott, but now ide-cd.c is independent)
72 * 3.00 Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
73 * Implement CDROMREADAUDIO ioctl (UNTESTED).
74 * Use input_ide_data() and output_ide_data().
75 * Add door locking.
76 * Fix usage count leak in cdrom_open, which happened
77 * when a read-write mount was attempted.
78 * Try to load the disk on open.
79 * Implement CDROMEJECT_SW ioctl (off by default).
80 * Read total cdrom capacity during open.
81 * Rearrange logic in cdrom_decode_status. Issue
82 * request sense commands for failed packet commands
83 * from here instead of from cdrom_queue_packet_command.
84 * Fix a race condition in retrieving error information.
85 * Suppress printing normal unit attention errors and
86 * some drive not ready errors.
87 * Implement CDROMVOLREAD ioctl.
88 * Implement CDROMREADMODE1/2 ioctls.
89 * Fix race condition in setting up interrupt handlers
90 * when the `serialize' option is used.
91 * 3.01 Sep 2, 1995 -- Fix ordering of reenabling interrupts in
92 * cdrom_queue_request.
93 * Another try at using ide_[input,output]_data.
94 * 3.02 Sep 16, 1995 -- Stick total disk capacity in partition table as well.
95 * Make VERBOSE_IDE_CD_ERRORS dump failed command again.
96 * Dump out more information for ILLEGAL REQUEST errs.
97 * Fix handling of errors occurring before the
98 * packet command is transferred.
99 * Fix transfers with odd bytelengths.
100 * 3.03 Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
101 * `DCI-2S10' drives are broken too.
102 * 3.04 Nov 20, 1995 -- So are Vertos drives.
103 * 3.05 Dec 1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
104 * 3.06 Dec 16, 1995 -- Add support needed for partitions.
105 * More workarounds for Vertos bugs (based on patches
106 * from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
107 * Try to eliminate byteorder assumptions.
108 * Use atapi_cdrom_subchnl struct definition.
109 * Add STANDARD_ATAPI compilation option.
110 * 3.07 Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
111 * Vertos 300.
112 * Add NO_DOOR_LOCKING configuration option.
113 * Handle drive_cmd requests w/NULL args (for hdparm -t).
114 * Work around sporadic Sony55e audio play problem.
115 * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
116 * problem with "hde=cdrom" with no drive present. -ml
117 * 3.08 Mar 6, 1996 -- More Vertos workarounds.
118 * 3.09 Apr 5, 1996 -- Add CDROMCLOSETRAY ioctl.
119 * Switch to using MSF addressing for audio commands.
120 * Reformat to match kernel tabbing style.
121 * Add CDROM_GET_UPC ioctl.
122 * 3.10 Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
123 * 3.11 Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
124 * to remove redundant verify_area calls.
125 * 3.12 May 7, 1996 -- Rudimentary changer support. Based on patches
126 * from Gerhard Zuber <zuber@berlin.snafu.de>.
127 * Let open succeed even if there's no loaded disc.
128 * 3.13 May 19, 1996 -- Fixes for changer code.
129 * 3.14 May 29, 1996 -- Add work-around for Vertos 600.
130 * (From Hennus Bergman <hennus@sky.ow.nl>.)
131 * 3.15 July 2, 1996 -- Added support for Sanyo 3 CD changers
132 * from Ben Galliart <bgallia@luc.edu> with
133 * special help from Jeff Lightfoot
134 * <jeffml@pobox.com>
135 * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
136 * 3.16 Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
137 * 3.17 Sep 17, 1996 -- Tweak audio reads for some drives.
138 * Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
139 * 3.18 Oct 31, 1996 -- Added module and DMA support.
142 * 4.00 Nov 5, 1996 -- New ide-cd maintainer,
143 * Erik B. Andersen <andersee@debian.org>
144 * -- Newer Creative drives don't always set the error
145 * register correctly. Make sure we see media changes
146 * regardless.
147 * -- Integrate with generic cdrom driver.
148 * -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
149 * a patch from Ciro Cattuto <>.
150 * -- Call set_device_ro.
151 * -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
152 * ioctls, based on patch by Erik Andersen
153 * -- Add some probes of drive capability during setup.
155 * 4.01 Nov 11, 1996 -- Split into ide-cd.c and ide-cd.h
156 * -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE
157 * ioctls in favor of a generalized approach
158 * using the generic cdrom driver.
159 * -- Fully integrated with the 2.1.X kernel.
160 * -- Other stuff that I forgot (lots of changes)
162 * 4.02 Dec 01, 1996 -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
163 * to fix the drive door locking problems.
165 * 4.03 Dec 04, 1996 -- Added DSC overlap support.
166 * 4.04 Dec 29, 1996 -- Added CDROMREADRAW ioclt based on patch
167 * by Ales Makarov (xmakarov@sun.felk.cvut.cz)
169 * 4.05 Nov 20, 1997 -- Modified to print more drive info on init
170 * Minor other changes
171 * Fix errors on CDROMSTOP (If you have a "Dolphin",
172 * you must define IHAVEADOLPHIN)
173 * Added identifier so new Sanyo CD-changer works
174 * Better detection if door locking isn't supported
176 * 4.06 Dec 17, 1997 -- fixed endless "tray open" messages -ml
177 * 4.07 Dec 17, 1997 -- fallback to set pc->stat on "tray open"
178 * 4.08 Dec 18, 1997 -- spew less noise when tray is empty
179 * -- fix speed display for ACER 24X, 18X
180 * 4.09 Jan 04, 1998 -- fix handling of the last block so we return
181 * an end of file instead of an I/O error (Gadi)
182 * 4.10 Jan 24, 1998 -- fixed a bug so now changers can change to a new
183 * slot when there is no disc in the current slot.
184 * -- Fixed a memory leak where info->changer_info was
185 * malloc'ed but never free'd when closing the device.
186 * -- Cleaned up the global namespace a bit by making more
187 * functions static that should already have been.
188 * 4.11 Mar 12, 1998 -- Added support for the CDROM_SELECT_SPEED ioctl
189 * based on a patch for 2.0.33 by Jelle Foks
190 * <jelle@scintilla.utwente.nl>, a patch for 2.0.33
191 * by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
192 * version, and my own efforts. -erik
193 * -- Fixed a stupid bug which egcs was kind enough to
194 * inform me of where "Illegal mode for this track"
195 * was never returned due to a comparison on data
196 * types of limited range.
197 * 4.12 Mar 29, 1998 -- Fixed bug in CDROM_SELECT_SPEED so write speed is
198 * now set ionly for CD-R and CD-RW drives. I had
199 * removed this support because it produced errors.
200 * It produced errors _only_ for non-writers. duh.
201 * 4.13 May 05, 1998 -- Suppress useless "in progress of becoming ready"
202 * messages, since this is not an error.
203 * -- Change error messages to be const
204 * -- Remove a "\t" which looks ugly in the syslogs
205 * 4.14 July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
206 * since the .pdf version doesn't seem to work...
207 * -- Updated the TODO list to something more current.
209 * 4.15 Aug 25, 1998 -- Updated ide-cd.h to respect mechine endianess,
210 * patch thanks to "Eddie C. Dost" <ecd@skynet.be>
212 * 4.50 Oct 19, 1998 -- New maintainers!
213 * Jens Axboe <axboe@image.dk>
214 * Chris Zwilling <chris@cloudnet.com>
216 * 4.51 Dec 23, 1998 -- Jens Axboe <axboe@image.dk>
217 * - ide_cdrom_reset enabled since the ide subsystem
218 * handles resets fine now. <axboe@image.dk>
219 * - Transfer size fix for Samsung CD-ROMs, thanks to
220 * "Ville Hallik" <ville.hallik@mail.ee>.
221 * - other minor stuff.
223 * 4.52 Jan 19, 1999 -- Jens Axboe <axboe@image.dk>
224 * - Detect DVD-ROM/RAM drives
226 * 4.53 Feb 22, 1999 - Include other model Samsung and one Goldstar
227 * drive in transfer size limit.
228 * - Fix the I/O error when doing eject without a medium
229 * loaded on some drives.
230 * - CDROMREADMODE2 is now implemented through
231 * CDROMREADRAW, since many drives don't support
232 * MODE2 (even though ATAPI 2.6 says they must).
233 * - Added ignore parameter to ide-cd (as a module), eg
234 * insmod ide-cd ignore='hda hdb'
235 * Useful when using ide-cd in conjunction with
236 * ide-scsi. TODO: non-modular way of doing the
237 * same.
239 * 4.54 Aug 5, 1999 - Support for MMC2 class commands through the generic
240 * packet interface to cdrom.c.
241 * - Unified audio ioctl support, most of it.
242 * - cleaned up various deprecated verify_area().
243 * - Added ide_cdrom_packet() as the interface for
244 * the Uniform generic_packet().
245 * - bunch of other stuff, will fill in logs later.
246 * - report 1 slot for non-changers, like the other
247 * cd-rom drivers. don't report select disc for
248 * non-changers as well.
249 * - mask out audio playing, if the device can't do it.
251 * 4.55 Sep 1, 1999 - Eliminated the rest of the audio ioctls, except
252 * for CDROMREADTOC[ENTRY|HEADER]. Some of the drivers
253 * use this independently of the actual audio handling.
254 * They will disappear later when I get the time to
255 * do it cleanly.
256 * - Minimize the TOC reading - only do it when we
257 * know a media change has occurred.
258 * - Moved all the CDROMREADx ioctls to the Uniform layer.
259 * - Heiko Eissfeldt <heiko@colossus.escape.de> supplied
260 * some fixes for CDI.
261 * - CD-ROM leaving door locked fix from Andries
262 * Brouwer <Andries.Brouwer@cwi.nl>
263 * - Erik Andersen <andersen@xmission.com> unified
264 * commands across the various drivers and how
265 * sense errors are handled.
267 * 4.56 Sep 12, 1999 - Removed changer support - it is now in the
268 * Uniform layer.
269 * - Added partition based multisession handling.
270 * - Mode sense and mode select moved to the
271 * Uniform layer.
272 * - Fixed a problem with WPI CDS-32X drive - it
273 * failed the capabilities
275 * 4.57 Apr 7, 2000 - Fixed sense reporting.
276 * - Fixed possible oops in ide_cdrom_get_last_session()
277 * - Fix locking mania and make ide_cdrom_reset relock
278 * - Stop spewing errors to log when magicdev polls with
279 * TEST_UNIT_READY on some drives.
280 * - Various fixes from Tobias Ringstrom:
281 * tray if it was locked prior to the reset.
282 * - cdrom_read_capacity returns one frame too little.
283 * - Fix real capacity reporting.
285 * 4.58 May 1, 2000 - Clean up ACER50 stuff.
286 * - Fix small problem with ide_cdrom_capacity
288 * 4.59 Aug 11, 2000 - Fix changer problem in cdrom_read_toc, we weren't
289 * correctly sensing a disc change.
290 * - Rearranged some code
291 * - Use extended sense on drives that support it for
292 * correctly reporting tray status -- from
293 * Michael D Johnson <johnsom@orst.edu>
294 * 4.60 Dec 17, 2003 - Add mt rainier support
295 * - Bump timeout for packet commands, matches sr
296 * - Odd stuff
297 * 4.61 Jan 22, 2004 - support hardware sector sizes other than 2kB,
298 * Pascal Schmidt <der.eremit@email.de>
300 *************************************************************************/
302 #define IDECD_VERSION "4.61"
304 #include <linux/config.h>
305 #include <linux/module.h>
306 #include <linux/types.h>
307 #include <linux/kernel.h>
308 #include <linux/delay.h>
309 #include <linux/timer.h>
310 #include <linux/slab.h>
311 #include <linux/interrupt.h>
312 #include <linux/errno.h>
313 #include <linux/cdrom.h>
314 #include <linux/ide.h>
315 #include <linux/completion.h>
317 #include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
319 #include <asm/irq.h>
320 #include <asm/io.h>
321 #include <asm/byteorder.h>
322 #include <asm/uaccess.h>
323 #include <asm/unaligned.h>
325 #include "ide-cd.h"
327 /****************************************************************************
328 * Generic packet command support and error handling routines.
331 /* Mark that we've seen a media change, and invalidate our internal
332 buffers. */
333 static void cdrom_saw_media_change (ide_drive_t *drive)
335 struct cdrom_info *info = drive->driver_data;
337 CDROM_STATE_FLAGS (drive)->media_changed = 1;
338 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
339 info->nsectors_buffered = 0;
342 static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
343 struct request_sense *sense)
345 int log = 0;
347 if (!sense || !rq || (rq->flags & REQ_QUIET))
348 return 0;
350 switch (sense->sense_key) {
351 case NO_SENSE: case RECOVERED_ERROR:
352 break;
353 case NOT_READY:
355 * don't care about tray state messages for
356 * e.g. capacity commands or in-progress or
357 * becoming ready
359 if (sense->asc == 0x3a || sense->asc == 0x04)
360 break;
361 log = 1;
362 break;
363 case ILLEGAL_REQUEST:
365 * don't log START_STOP unit with LoEj set, since
366 * we cannot reliably check if drive can auto-close
368 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
369 log = 0;
370 break;
371 case UNIT_ATTENTION:
373 * Make good and sure we've seen this potential media
374 * change. Some drives (i.e. Creative) fail to present
375 * the correct sense key in the error register.
377 cdrom_saw_media_change(drive);
378 break;
379 default:
380 log = 1;
381 break;
383 return log;
386 static
387 void cdrom_analyze_sense_data(ide_drive_t *drive,
388 struct request *failed_command,
389 struct request_sense *sense)
391 if (!cdrom_log_sense(drive, failed_command, sense))
392 return;
395 * If a read toc is executed for a CD-R or CD-RW medium where
396 * the first toc has not been recorded yet, it will fail with
397 * 05/24/00 (which is a confusing error)
399 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
400 if (sense->sense_key == 0x05 && sense->asc == 0x24)
401 return;
403 #if VERBOSE_IDE_CD_ERRORS
405 int i;
406 const char *s;
407 char buf[80];
409 printk ("ATAPI device %s:\n", drive->name);
410 if (sense->error_code==0x70)
411 printk(" Error: ");
412 else if (sense->error_code==0x71)
413 printk(" Deferred Error: ");
414 else if (sense->error_code == 0x7f)
415 printk(" Vendor-specific Error: ");
416 else
417 printk(" Unknown Error Type: ");
419 if (sense->sense_key < ARY_LEN(sense_key_texts))
420 s = sense_key_texts[sense->sense_key];
421 else
422 s = "bad sense key!";
424 printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
426 if (sense->asc == 0x40) {
427 sprintf(buf, "Diagnostic failure on component 0x%02x",
428 sense->ascq);
429 s = buf;
430 } else {
431 int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
432 unsigned long key = (sense->sense_key << 16);
433 key |= (sense->asc << 8);
434 if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
435 key |= sense->ascq;
436 s = NULL;
438 while (hi > lo) {
439 mid = (lo + hi) / 2;
440 if (sense_data_texts[mid].asc_ascq == key ||
441 sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
442 s = sense_data_texts[mid].text;
443 break;
445 else if (sense_data_texts[mid].asc_ascq > key)
446 hi = mid;
447 else
448 lo = mid+1;
452 if (s == NULL) {
453 if (sense->asc > 0x80)
454 s = "(vendor-specific error)";
455 else
456 s = "(reserved error code)";
459 printk(" %s -- (asc=0x%02x, ascq=0x%02x)\n",
460 s, sense->asc, sense->ascq);
462 if (failed_command != NULL) {
464 int lo=0, mid, hi= ARY_LEN (packet_command_texts);
465 s = NULL;
467 while (hi > lo) {
468 mid = (lo + hi) / 2;
469 if (packet_command_texts[mid].packet_command ==
470 failed_command->cmd[0]) {
471 s = packet_command_texts[mid].text;
472 break;
474 if (packet_command_texts[mid].packet_command >
475 failed_command->cmd[0])
476 hi = mid;
477 else
478 lo = mid+1;
481 printk (" The failed \"%s\" packet command was: \n \"", s);
482 for (i=0; i<sizeof (failed_command->cmd); i++)
483 printk ("%02x ", failed_command->cmd[i]);
484 printk ("\"\n");
487 /* The SKSV bit specifies validity of the sense_key_specific
488 * in the next two commands. It is bit 7 of the first byte.
489 * In the case of NOT_READY, if SKSV is set the drive can
490 * give us nice ETA readings.
492 if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
493 int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
494 printk(" Command is %02d%% complete\n", progress / 0xffff);
498 if (sense->sense_key == ILLEGAL_REQUEST &&
499 (sense->sks[0] & 0x80) != 0) {
500 printk(" Error in %s byte %d",
501 (sense->sks[0] & 0x40) != 0 ?
502 "command packet" : "command data",
503 (sense->sks[1] << 8) + sense->sks[2]);
505 if ((sense->sks[0] & 0x40) != 0)
506 printk (" bit %d", sense->sks[0] & 0x07);
508 printk ("\n");
512 #else /* not VERBOSE_IDE_CD_ERRORS */
514 /* Suppress printing unit attention and `in progress of becoming ready'
515 errors when we're not being verbose. */
517 if (sense->sense_key == UNIT_ATTENTION ||
518 (sense->sense_key == NOT_READY && (sense->asc == 4 ||
519 sense->asc == 0x3a)))
520 return;
522 printk("%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
523 drive->name,
524 sense->error_code, sense->sense_key,
525 sense->asc, sense->ascq);
526 #endif /* not VERBOSE_IDE_CD_ERRORS */
530 * Initialize a ide-cd packet command request
532 static void cdrom_prepare_request(struct request *rq)
534 ide_init_drive_cmd(rq);
535 rq->flags = REQ_PC;
538 static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
539 struct request *failed_command)
541 struct cdrom_info *info = drive->driver_data;
542 struct request *rq = &info->request_sense_request;
544 if (sense == NULL)
545 sense = &info->sense_data;
547 /* stuff the sense request in front of our current request */
548 cdrom_prepare_request(rq);
550 rq->data = sense;
551 rq->cmd[0] = GPCMD_REQUEST_SENSE;
552 rq->cmd[4] = rq->data_len = 18;
554 rq->flags = REQ_SENSE;
556 /* NOTE! Save the failed command in "rq->buffer" */
557 rq->buffer = (void *) failed_command;
559 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
563 * ide_error() takes action based on the error returned by the drive.
565 ide_startstop_t ide_cdrom_error (ide_drive_t *drive, const char *msg, byte stat)
567 struct request *rq;
568 byte err;
570 err = ide_dump_atapi_status(drive, msg, stat);
571 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
572 return ide_stopped;
573 /* retry only "normal" I/O: */
574 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
575 rq->errors = 1;
576 ide_end_drive_cmd(drive, stat, err);
577 return ide_stopped;
580 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
581 /* other bits are useless when BUSY */
582 rq->errors |= ERROR_RESET;
583 } else {
584 /* add decoding error stuff */
586 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
587 /* force an abort */
588 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
589 if (rq->errors >= ERROR_MAX) {
590 DRIVER(drive)->end_request(drive, 0, 0);
591 } else {
592 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
593 ++rq->errors;
594 return ide_do_reset(drive);
596 ++rq->errors;
598 return ide_stopped;
601 ide_startstop_t ide_cdrom_abort (ide_drive_t *drive, const char *msg)
603 struct request *rq;
605 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
606 return ide_stopped;
607 /* retry only "normal" I/O: */
608 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
609 rq->errors = 1;
610 ide_end_drive_cmd(drive, BUSY_STAT, 0);
611 return ide_stopped;
613 rq->errors |= ERROR_RESET;
614 DRIVER(drive)->end_request(drive, 0, 0);
615 return ide_stopped;
618 static void cdrom_end_request (ide_drive_t *drive, int uptodate)
620 struct request *rq = HWGROUP(drive)->rq;
621 int nsectors = rq->hard_cur_sectors;
623 if ((rq->flags & REQ_SENSE) && uptodate) {
625 * For REQ_SENSE, "rq->buffer" points to the original failed
626 * request
628 struct request *failed = (struct request *) rq->buffer;
629 struct cdrom_info *info = drive->driver_data;
630 void *sense = &info->sense_data;
631 unsigned long flags;
633 if (failed) {
634 if (failed->sense) {
635 sense = failed->sense;
636 failed->sense_len = rq->sense_len;
640 * now end failed request
642 spin_lock_irqsave(&ide_lock, flags);
643 end_that_request_chunk(failed, 0, failed->data_len);
644 end_that_request_last(failed);
645 spin_unlock_irqrestore(&ide_lock, flags);
648 cdrom_analyze_sense_data(drive, failed, sense);
651 if (!rq->current_nr_sectors && blk_fs_request(rq))
652 uptodate = 1;
653 /* make sure it's fully ended */
654 if (blk_pc_request(rq))
655 nsectors = (rq->data_len + 511) >> 9;
656 if (!nsectors)
657 nsectors = 1;
659 ide_end_request(drive, uptodate, nsectors);
662 /* Returns 0 if the request should be continued.
663 Returns 1 if the request was ended. */
664 static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
666 struct request *rq = HWGROUP(drive)->rq;
667 int stat, err, sense_key;
669 /* Check for errors. */
670 stat = HWIF(drive)->INB(IDE_STATUS_REG);
671 if (stat_ret)
672 *stat_ret = stat;
674 if (OK_STAT(stat, good_stat, BAD_R_STAT))
675 return 0;
677 /* Get the IDE error register. */
678 err = HWIF(drive)->INB(IDE_ERROR_REG);
679 sense_key = err >> 4;
681 if (rq == NULL) {
682 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
683 return 1;
686 if (rq->flags & REQ_SENSE) {
687 /* We got an error trying to get sense info
688 from the drive (probably while trying
689 to recover from a former error). Just give up. */
691 rq->flags |= REQ_FAILED;
692 cdrom_end_request(drive, 0);
693 DRIVER(drive)->error(drive, "request sense failure", stat);
694 return 1;
696 } else if (rq->flags & (REQ_PC | REQ_BLOCK_PC)) {
697 /* All other functions, except for READ. */
698 unsigned long flags;
701 * if we have an error, pass back CHECK_CONDITION as the
702 * scsi status byte
704 if ((rq->flags & REQ_BLOCK_PC) && !rq->errors)
705 rq->errors = SAM_STAT_CHECK_CONDITION;
707 /* Check for tray open. */
708 if (sense_key == NOT_READY) {
709 cdrom_saw_media_change (drive);
710 } else if (sense_key == UNIT_ATTENTION) {
711 /* Check for media change. */
712 cdrom_saw_media_change (drive);
713 /*printk("%s: media changed\n",drive->name);*/
714 return 0;
715 } else if (!(rq->flags & REQ_QUIET)) {
716 /* Otherwise, print an error. */
717 ide_dump_status(drive, "packet command error", stat);
720 rq->flags |= REQ_FAILED;
723 * instead of playing games with moving completions around,
724 * remove failed request completely and end it when the
725 * request sense has completed
727 if (stat & ERR_STAT) {
728 spin_lock_irqsave(&ide_lock, flags);
729 blkdev_dequeue_request(rq);
730 HWGROUP(drive)->rq = NULL;
731 spin_unlock_irqrestore(&ide_lock, flags);
733 cdrom_queue_request_sense(drive, rq->sense, rq);
734 } else
735 cdrom_end_request(drive, 0);
737 } else if (blk_fs_request(rq)) {
738 int do_end_request = 0;
740 /* Handle errors from READ and WRITE requests. */
742 if (blk_noretry_request(rq))
743 do_end_request = 1;
745 if (sense_key == NOT_READY) {
746 /* Tray open. */
747 if (rq_data_dir(rq) == READ) {
748 cdrom_saw_media_change (drive);
750 /* Fail the request. */
751 printk ("%s: tray open\n", drive->name);
752 do_end_request = 1;
753 } else {
754 struct cdrom_info *info = drive->driver_data;
756 /* allow the drive 5 seconds to recover, some
757 * devices will return this error while flushing
758 * data from cache */
759 if (!rq->errors)
760 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
761 rq->errors = 1;
762 if (time_after(jiffies, info->write_timeout))
763 do_end_request = 1;
764 else {
765 unsigned long flags;
768 * take a breather relying on the
769 * unplug timer to kick us again
771 spin_lock_irqsave(&ide_lock, flags);
772 blk_plug_device(drive->queue);
773 spin_unlock_irqrestore(&ide_lock,flags);
774 return 1;
777 } else if (sense_key == UNIT_ATTENTION) {
778 /* Media change. */
779 cdrom_saw_media_change (drive);
781 /* Arrange to retry the request.
782 But be sure to give up if we've retried
783 too many times. */
784 if (++rq->errors > ERROR_MAX)
785 do_end_request = 1;
786 } else if (sense_key == ILLEGAL_REQUEST ||
787 sense_key == DATA_PROTECT) {
788 /* No point in retrying after an illegal
789 request or data protect error.*/
790 ide_dump_status (drive, "command error", stat);
791 do_end_request = 1;
792 } else if (sense_key == MEDIUM_ERROR) {
793 /* No point in re-trying a zillion times on a bad
794 * sector... If we got here the error is not correctable */
795 ide_dump_status (drive, "media error (bad sector)", stat);
796 do_end_request = 1;
797 } else if (sense_key == BLANK_CHECK) {
798 /* Disk appears blank ?? */
799 ide_dump_status (drive, "media error (blank)", stat);
800 do_end_request = 1;
801 } else if ((err & ~ABRT_ERR) != 0) {
802 /* Go to the default handler
803 for other errors. */
804 DRIVER(drive)->error(drive, "cdrom_decode_status",stat);
805 return 1;
806 } else if ((++rq->errors > ERROR_MAX)) {
807 /* We've racked up too many retries. Abort. */
808 do_end_request = 1;
811 if (do_end_request)
812 cdrom_end_request(drive, 0);
814 /* If we got a CHECK_CONDITION status,
815 queue a request sense command. */
816 if ((stat & ERR_STAT) != 0)
817 cdrom_queue_request_sense(drive, NULL, NULL);
818 } else {
819 blk_dump_rq_flags(rq, "ide-cd: bad rq");
820 cdrom_end_request(drive, 0);
823 /* Retry, or handle the next request. */
824 return 1;
827 static int cdrom_timer_expiry(ide_drive_t *drive)
829 struct request *rq = HWGROUP(drive)->rq;
830 unsigned long wait = 0;
833 * Some commands are *slow* and normally take a long time to
834 * complete. Usually we can use the ATAPI "disconnect" to bypass
835 * this, but not all commands/drives support that. Let
836 * ide_timer_expiry keep polling us for these.
838 switch (rq->cmd[0]) {
839 case GPCMD_BLANK:
840 case GPCMD_FORMAT_UNIT:
841 case GPCMD_RESERVE_RZONE_TRACK:
842 case GPCMD_CLOSE_TRACK:
843 case GPCMD_FLUSH_CACHE:
844 wait = ATAPI_WAIT_PC;
845 break;
846 default:
847 if (!(rq->flags & REQ_QUIET))
848 printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
849 wait = 0;
850 break;
852 return wait;
855 /* Set up the device registers for transferring a packet command on DEV,
856 expecting to later transfer XFERLEN bytes. HANDLER is the routine
857 which actually transfers the command to the drive. If this is a
858 drq_interrupt device, this routine will arrange for HANDLER to be
859 called when the interrupt from the drive arrives. Otherwise, HANDLER
860 will be called immediately after the drive is prepared for the transfer. */
862 static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
863 int xferlen,
864 ide_handler_t *handler)
866 ide_startstop_t startstop;
867 struct cdrom_info *info = drive->driver_data;
869 /* Wait for the controller to be idle. */
870 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
871 return startstop;
873 if (info->dma) {
874 if (info->cmd == READ) {
875 info->dma = !HWIF(drive)->ide_dma_read(drive);
876 } else if (info->cmd == WRITE) {
877 info->dma = !HWIF(drive)->ide_dma_write(drive);
878 } else {
879 printk("ide-cd: DMA set, but not allowed\n");
883 /* Set up the controller registers. */
884 /* FIXME: for Virtual DMA we must check harder */
885 HWIF(drive)->OUTB(info->dma, IDE_FEATURE_REG);
886 HWIF(drive)->OUTB(0, IDE_IREASON_REG);
887 HWIF(drive)->OUTB(0, IDE_SECTOR_REG);
889 HWIF(drive)->OUTB(xferlen & 0xff, IDE_BCOUNTL_REG);
890 HWIF(drive)->OUTB(xferlen >> 8 , IDE_BCOUNTH_REG);
891 if (IDE_CONTROL_REG)
892 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
894 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
895 /* packet command */
896 ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
897 return ide_started;
898 } else {
899 /* packet command */
900 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
901 return (*handler) (drive);
905 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
906 The device registers must have already been prepared
907 by cdrom_start_packet_command.
908 HANDLER is the interrupt handler to call when the command completes
909 or there's data ready. */
911 * changed 5 parameters to 3 for dvd-ram
912 * struct packet_command *pc; now packet_command_t *pc;
914 #define ATAPI_MIN_CDB_BYTES 12
915 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
916 struct request *rq,
917 ide_handler_t *handler)
919 int cmd_len;
920 struct cdrom_info *info = drive->driver_data;
921 ide_startstop_t startstop;
923 if (CDROM_CONFIG_FLAGS(drive)->drq_interrupt) {
924 /* Here we should have been called after receiving an interrupt
925 from the device. DRQ should how be set. */
927 /* Check for errors. */
928 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
929 return ide_stopped;
930 } else {
931 /* Otherwise, we must wait for DRQ to get set. */
932 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
933 BUSY_STAT, WAIT_READY))
934 return startstop;
937 /* Arm the interrupt handler. */
938 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
940 /* ATAPI commands get padded out to 12 bytes minimum */
941 cmd_len = COMMAND_SIZE(rq->cmd[0]);
942 if (cmd_len < ATAPI_MIN_CDB_BYTES)
943 cmd_len = ATAPI_MIN_CDB_BYTES;
945 /* Send the command to the device. */
946 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
948 /* Start the DMA if need be */
949 if (info->dma)
950 (void) HWIF(drive)->ide_dma_begin(drive);
952 return ide_started;
955 /****************************************************************************
956 * Block read functions.
960 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
961 * buffer. Once the first sector is added, any subsequent sectors are
962 * assumed to be continuous (until the buffer is cleared). For the first
963 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
964 * the buffer is cleared.)
966 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
967 int sectors_to_transfer)
969 struct cdrom_info *info = drive->driver_data;
971 /* Number of sectors to read into the buffer. */
972 int sectors_to_buffer = min_t(int, sectors_to_transfer,
973 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
974 info->nsectors_buffered);
976 char *dest;
978 /* If we couldn't get a buffer, don't try to buffer anything... */
979 if (info->buffer == NULL)
980 sectors_to_buffer = 0;
982 /* If this is the first sector in the buffer, remember its number. */
983 if (info->nsectors_buffered == 0)
984 info->sector_buffered = sector;
986 /* Read the data into the buffer. */
987 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
988 while (sectors_to_buffer > 0) {
989 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
990 --sectors_to_buffer;
991 --sectors_to_transfer;
992 ++info->nsectors_buffered;
993 dest += SECTOR_SIZE;
996 /* Throw away any remaining data. */
997 while (sectors_to_transfer > 0) {
998 static char dum[SECTOR_SIZE];
999 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
1000 --sectors_to_transfer;
1005 * Check the contents of the interrupt reason register from the cdrom
1006 * and attempt to recover if there are problems. Returns 0 if everything's
1007 * ok; nonzero if the request has been terminated.
1009 static inline
1010 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
1012 if (ireason == 2)
1013 return 0;
1014 else if (ireason == 0) {
1015 /* Whoops... The drive is expecting to receive data from us! */
1016 printk("%s: read_intr: Drive wants to transfer data the "
1017 "wrong way!\n", drive->name);
1019 /* Throw some data at the drive so it doesn't hang
1020 and quit this request. */
1021 while (len > 0) {
1022 int dum = 0;
1023 HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof (dum));
1024 len -= sizeof (dum);
1026 } else if (ireason == 1) {
1027 /* Some drives (ASUS) seem to tell us that status
1028 * info is available. just get it and ignore.
1030 (void) HWIF(drive)->INB(IDE_STATUS_REG);
1031 return 0;
1032 } else {
1033 /* Drive wants a command packet, or invalid ireason... */
1034 printk("%s: read_intr: bad interrupt reason %x\n", drive->name,
1035 ireason);
1038 cdrom_end_request(drive, 0);
1039 return -1;
1043 * Interrupt routine. Called when a read request has completed.
1045 static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
1047 int stat;
1048 int ireason, len, sectors_to_transfer, nskip;
1049 struct cdrom_info *info = drive->driver_data;
1050 u8 lowcyl = 0, highcyl = 0;
1051 int dma = info->dma, dma_error = 0;
1053 struct request *rq = HWGROUP(drive)->rq;
1056 * handle dma case
1058 if (dma) {
1059 info->dma = 0;
1060 if ((dma_error = HWIF(drive)->ide_dma_end(drive)))
1061 __ide_dma_off(drive);
1064 if (cdrom_decode_status(drive, 0, &stat))
1065 return ide_stopped;
1067 if (dma) {
1068 if (!dma_error) {
1069 ide_end_request(drive, 1, rq->nr_sectors);
1070 return ide_stopped;
1071 } else
1072 return DRIVER(drive)->error(drive, "dma error", stat);
1075 /* Read the interrupt reason and the transfer length. */
1076 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1077 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1078 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1080 len = lowcyl + (256 * highcyl);
1082 /* If DRQ is clear, the command has completed. */
1083 if ((stat & DRQ_STAT) == 0) {
1084 /* If we're not done filling the current buffer, complain.
1085 Otherwise, complete the command normally. */
1086 if (rq->current_nr_sectors > 0) {
1087 printk ("%s: cdrom_read_intr: data underrun (%d blocks)\n",
1088 drive->name, rq->current_nr_sectors);
1089 rq->flags |= REQ_FAILED;
1090 cdrom_end_request(drive, 0);
1091 } else
1092 cdrom_end_request(drive, 1);
1093 return ide_stopped;
1096 /* Check that the drive is expecting to do the same thing we are. */
1097 if (cdrom_read_check_ireason (drive, len, ireason))
1098 return ide_stopped;
1100 /* Assume that the drive will always provide data in multiples
1101 of at least SECTOR_SIZE, as it gets hairy to keep track
1102 of the transfers otherwise. */
1103 if ((len % SECTOR_SIZE) != 0) {
1104 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
1105 drive->name, len);
1106 if (CDROM_CONFIG_FLAGS(drive)->limit_nframes)
1107 printk (" This drive is not supported by this version of the driver\n");
1108 else {
1109 printk (" Trying to limit transfer sizes\n");
1110 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
1112 cdrom_end_request(drive, 0);
1113 return ide_stopped;
1116 /* The number of sectors we need to read from the drive. */
1117 sectors_to_transfer = len / SECTOR_SIZE;
1119 /* First, figure out if we need to bit-bucket
1120 any of the leading sectors. */
1121 nskip = min_t(int, rq->current_nr_sectors - bio_cur_sectors(rq->bio), sectors_to_transfer);
1123 while (nskip > 0) {
1124 /* We need to throw away a sector. */
1125 static char dum[SECTOR_SIZE];
1126 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
1128 --rq->current_nr_sectors;
1129 --nskip;
1130 --sectors_to_transfer;
1133 /* Now loop while we still have data to read from the drive. */
1134 while (sectors_to_transfer > 0) {
1135 int this_transfer;
1137 /* If we've filled the present buffer but there's another
1138 chained buffer after it, move on. */
1139 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1140 cdrom_end_request(drive, 1);
1142 /* If the buffers are full, cache the rest of the data in our
1143 internal buffer. */
1144 if (rq->current_nr_sectors == 0) {
1145 cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
1146 sectors_to_transfer = 0;
1147 } else {
1148 /* Transfer data to the buffers.
1149 Figure out how many sectors we can transfer
1150 to the current buffer. */
1151 this_transfer = min_t(int, sectors_to_transfer,
1152 rq->current_nr_sectors);
1154 /* Read this_transfer sectors
1155 into the current buffer. */
1156 while (this_transfer > 0) {
1157 HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
1158 rq->buffer += SECTOR_SIZE;
1159 --rq->nr_sectors;
1160 --rq->current_nr_sectors;
1161 ++rq->sector;
1162 --this_transfer;
1163 --sectors_to_transfer;
1168 /* Done moving data! Wait for another interrupt. */
1169 ide_set_handler(drive, &cdrom_read_intr, ATAPI_WAIT_PC, NULL);
1170 return ide_started;
1174 * Try to satisfy some of the current read request from our cached data.
1175 * Returns nonzero if the request has been completed, zero otherwise.
1177 static int cdrom_read_from_buffer (ide_drive_t *drive)
1179 struct cdrom_info *info = drive->driver_data;
1180 struct request *rq = HWGROUP(drive)->rq;
1181 unsigned short sectors_per_frame;
1183 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1185 /* Can't do anything if there's no buffer. */
1186 if (info->buffer == NULL) return 0;
1188 /* Loop while this request needs data and the next block is present
1189 in our cache. */
1190 while (rq->nr_sectors > 0 &&
1191 rq->sector >= info->sector_buffered &&
1192 rq->sector < info->sector_buffered + info->nsectors_buffered) {
1193 if (rq->current_nr_sectors == 0)
1194 cdrom_end_request(drive, 1);
1196 memcpy (rq->buffer,
1197 info->buffer +
1198 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1199 SECTOR_SIZE);
1200 rq->buffer += SECTOR_SIZE;
1201 --rq->current_nr_sectors;
1202 --rq->nr_sectors;
1203 ++rq->sector;
1206 /* If we've satisfied the current request,
1207 terminate it successfully. */
1208 if (rq->nr_sectors == 0) {
1209 cdrom_end_request(drive, 1);
1210 return -1;
1213 /* Move on to the next buffer if needed. */
1214 if (rq->current_nr_sectors == 0)
1215 cdrom_end_request(drive, 1);
1217 /* If this condition does not hold, then the kluge i use to
1218 represent the number of sectors to skip at the start of a transfer
1219 will fail. I think that this will never happen, but let's be
1220 paranoid and check. */
1221 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
1222 (rq->sector & (sectors_per_frame - 1))) {
1223 printk("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1224 drive->name, (long)rq->sector);
1225 cdrom_end_request(drive, 0);
1226 return -1;
1229 return 0;
1233 * Routine to send a read packet command to the drive.
1234 * This is usually called directly from cdrom_start_read.
1235 * However, for drq_interrupt devices, it is called from an interrupt
1236 * when the drive is ready to accept the command.
1238 static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1240 struct request *rq = HWGROUP(drive)->rq;
1241 unsigned short sectors_per_frame;
1242 int nskip;
1244 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1246 /* If the requested sector doesn't start on a cdrom block boundary,
1247 we must adjust the start of the transfer so that it does,
1248 and remember to skip the first few sectors.
1249 If the CURRENT_NR_SECTORS field is larger than the size
1250 of the buffer, it will mean that we're to skip a number
1251 of sectors equal to the amount by which CURRENT_NR_SECTORS
1252 is larger than the buffer size. */
1253 nskip = rq->sector & (sectors_per_frame - 1);
1254 if (nskip > 0) {
1255 /* Sanity check... */
1256 if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
1257 (rq->sector & (sectors_per_frame - 1))) {
1258 printk ("%s: cdrom_start_read_continuation: buffer botch (%u)\n",
1259 drive->name, rq->current_nr_sectors);
1260 cdrom_end_request(drive, 0);
1261 return ide_stopped;
1263 rq->current_nr_sectors += nskip;
1266 /* Set up the command */
1267 rq->timeout = ATAPI_WAIT_PC;
1269 /* Send the command to the drive and return. */
1270 return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
1274 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1275 #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
1276 #define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
1278 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1280 struct cdrom_info *info = drive->driver_data;
1281 int stat;
1282 static int retry = 10;
1284 if (cdrom_decode_status(drive, 0, &stat))
1285 return ide_stopped;
1286 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1288 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
1289 if (--retry == 0) {
1291 * this condition is far too common, to bother
1292 * users about it
1294 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
1295 drive->dsc_overlap = 0;
1298 return ide_stopped;
1301 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1303 struct request *rq = HWGROUP(drive)->rq;
1304 sector_t frame = rq->sector;
1306 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
1308 memset(rq->cmd, 0, sizeof(rq->cmd));
1309 rq->cmd[0] = GPCMD_SEEK;
1310 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
1312 rq->timeout = ATAPI_WAIT_PC;
1313 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
1316 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1318 struct cdrom_info *info = drive->driver_data;
1320 info->dma = 0;
1321 info->cmd = 0;
1322 info->start_seek = jiffies;
1323 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
1326 /* Fix up a possibly partially-processed request so that we can
1327 start it over entirely, or even put it back on the request queue. */
1328 static void restore_request (struct request *rq)
1330 if (rq->buffer != bio_data(rq->bio)) {
1331 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
1333 rq->buffer = bio_data(rq->bio);
1334 rq->nr_sectors += n;
1335 rq->sector -= n;
1337 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
1338 rq->hard_nr_sectors = rq->nr_sectors;
1339 rq->hard_sector = rq->sector;
1340 rq->q->prep_rq_fn(rq->q, rq);
1344 * Start a read request from the CD-ROM.
1346 static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1348 struct cdrom_info *info = drive->driver_data;
1349 struct request *rq = HWGROUP(drive)->rq;
1350 unsigned short sectors_per_frame;
1352 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1354 /* We may be retrying this request after an error. Fix up
1355 any weirdness which might be present in the request packet. */
1356 restore_request(rq);
1358 /* Satisfy whatever we can of this request from our cached sector. */
1359 if (cdrom_read_from_buffer(drive))
1360 return ide_stopped;
1362 blk_attempt_remerge(drive->queue, rq);
1364 /* Clear the local sector buffer. */
1365 info->nsectors_buffered = 0;
1367 /* use dma, if possible. */
1368 info->dma = drive->using_dma;
1369 if ((rq->sector & (sectors_per_frame - 1)) ||
1370 (rq->nr_sectors & (sectors_per_frame - 1)))
1371 info->dma = 0;
1373 info->cmd = READ;
1375 /* Start sending the read request to the drive. */
1376 return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1379 /****************************************************************************
1380 * Execute all other packet commands.
1383 /* Interrupt routine for packet command completion. */
1384 static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1386 int ireason, len, thislen;
1387 struct request *rq = HWGROUP(drive)->rq;
1388 u8 lowcyl = 0, highcyl = 0;
1389 int stat;
1391 /* Check for errors. */
1392 if (cdrom_decode_status(drive, 0, &stat))
1393 return ide_stopped;
1395 /* Read the interrupt reason and the transfer length. */
1396 ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1397 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1398 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1400 len = lowcyl + (256 * highcyl);
1402 /* If DRQ is clear, the command has completed.
1403 Complain if we still have data left to transfer. */
1404 if ((stat & DRQ_STAT) == 0) {
1405 /* Some of the trailing request sense fields are optional, and
1406 some drives don't send them. Sigh. */
1407 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
1408 rq->data_len > 0 &&
1409 rq->data_len <= 5) {
1410 while (rq->data_len > 0) {
1411 *(unsigned char *)rq->data++ = 0;
1412 --rq->data_len;
1416 if (rq->data_len == 0)
1417 cdrom_end_request(drive, 1);
1418 else {
1419 /* Comment this out, because this always happens
1420 right after a reset occurs, and it is annoying to
1421 always print expected stuff. */
1423 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1424 drive->name, pc->buflen);
1426 rq->flags |= REQ_FAILED;
1427 cdrom_end_request(drive, 0);
1429 return ide_stopped;
1432 /* Figure out how much data to transfer. */
1433 thislen = rq->data_len;
1434 if (thislen > len) thislen = len;
1436 /* The drive wants to be written to. */
1437 if ((ireason & 3) == 0) {
1438 if (!rq->data) {
1439 blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1440 goto confused;
1442 /* Transfer the data. */
1443 HWIF(drive)->atapi_output_bytes(drive, rq->data, thislen);
1445 /* If we haven't moved enough data to satisfy the drive,
1446 add some padding. */
1447 while (len > thislen) {
1448 int dum = 0;
1449 HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof(dum));
1450 len -= sizeof(dum);
1453 /* Keep count of how much data we've moved. */
1454 rq->data += thislen;
1455 rq->data_len -= thislen;
1458 /* Same drill for reading. */
1459 else if ((ireason & 3) == 2) {
1460 if (!rq->data) {
1461 blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1462 goto confused;
1464 /* Transfer the data. */
1465 HWIF(drive)->atapi_input_bytes(drive, rq->data, thislen);
1467 /* If we haven't moved enough data to satisfy the drive,
1468 add some padding. */
1469 while (len > thislen) {
1470 int dum = 0;
1471 HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1472 len -= sizeof(dum);
1475 /* Keep count of how much data we've moved. */
1476 rq->data += thislen;
1477 rq->data_len -= thislen;
1479 if (rq->flags & REQ_SENSE)
1480 rq->sense_len += thislen;
1481 } else {
1482 confused:
1483 printk ("%s: cdrom_pc_intr: The drive "
1484 "appears confused (ireason = 0x%02x)\n",
1485 drive->name, ireason);
1486 rq->flags |= REQ_FAILED;
1489 /* Now we wait for another interrupt. */
1490 ide_set_handler(drive, &cdrom_pc_intr, ATAPI_WAIT_PC, cdrom_timer_expiry);
1491 return ide_started;
1494 static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1496 struct request *rq = HWGROUP(drive)->rq;
1498 if (!rq->timeout)
1499 rq->timeout = ATAPI_WAIT_PC;
1501 /* Send the command to the drive and return. */
1502 return cdrom_transfer_packet_command(drive, rq, &cdrom_pc_intr);
1506 static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1508 int len;
1509 struct request *rq = HWGROUP(drive)->rq;
1510 struct cdrom_info *info = drive->driver_data;
1512 info->dma = 0;
1513 info->cmd = 0;
1514 rq->flags &= ~REQ_FAILED;
1515 len = rq->data_len;
1517 /* Start sending the command to the drive. */
1518 return cdrom_start_packet_command(drive, len, cdrom_do_pc_continuation);
1522 /* Sleep for TIME jiffies.
1523 Not to be called from an interrupt handler. */
1524 static
1525 void cdrom_sleep (int time)
1527 int sleep = time;
1529 do {
1530 set_current_state(TASK_INTERRUPTIBLE);
1531 sleep = schedule_timeout(sleep);
1532 } while (sleep);
1535 static
1536 int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
1538 struct request_sense sense;
1539 int retries = 10;
1540 unsigned int flags = rq->flags;
1542 if (rq->sense == NULL)
1543 rq->sense = &sense;
1545 /* Start of retry loop. */
1546 do {
1547 int error;
1548 unsigned long time = jiffies;
1549 rq->flags = flags;
1551 error = ide_do_drive_cmd(drive, rq, ide_wait);
1552 time = jiffies - time;
1554 /* FIXME: we should probably abort/retry or something
1555 * in case of failure */
1556 if (rq->flags & REQ_FAILED) {
1557 /* The request failed. Retry if it was due to a unit
1558 attention status
1559 (usually means media was changed). */
1560 struct request_sense *reqbuf = rq->sense;
1562 if (reqbuf->sense_key == UNIT_ATTENTION)
1563 cdrom_saw_media_change(drive);
1564 else if (reqbuf->sense_key == NOT_READY &&
1565 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1566 /* The drive is in the process of loading
1567 a disk. Retry, but wait a little to give
1568 the drive time to complete the load. */
1569 cdrom_sleep(2 * HZ);
1570 } else {
1571 /* Otherwise, don't retry. */
1572 retries = 0;
1574 --retries;
1577 /* End of retry loop. */
1578 } while ((rq->flags & REQ_FAILED) && retries >= 0);
1580 /* Return an error if the command failed. */
1581 return (rq->flags & REQ_FAILED) ? -EIO : 0;
1585 * Write handling
1587 static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1589 /* Two notes about IDE interrupt reason here - 0 means that
1590 * the drive wants to receive data from us, 2 means that
1591 * the drive is expecting to transfer data to us.
1593 if (ireason == 0)
1594 return 0;
1595 else if (ireason == 2) {
1596 /* Whoops... The drive wants to send data. */
1597 printk("%s: write_intr: wrong transfer direction!\n",
1598 drive->name);
1600 while (len > 0) {
1601 int dum = 0;
1602 HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1603 len -= sizeof(dum);
1605 } else {
1606 /* Drive wants a command packet, or invalid ireason... */
1607 printk("%s: write_intr: bad interrupt reason %x\n",
1608 drive->name, ireason);
1611 cdrom_end_request(drive, 0);
1612 return 1;
1615 static void post_transform_command(struct request *req)
1617 u8 *c = req->cmd;
1618 char *ibuf;
1620 if (!blk_pc_request(req))
1621 return;
1623 if (req->bio)
1624 ibuf = bio_data(req->bio);
1625 else
1626 ibuf = req->data;
1628 if (!ibuf)
1629 return;
1632 * set ansi-revision and response data as atapi
1634 if (c[0] == GPCMD_INQUIRY) {
1635 ibuf[2] |= 2;
1636 ibuf[3] = (ibuf[3] & 0xf0) | 2;
1640 typedef void (xfer_func_t)(ide_drive_t *, void *, u32);
1643 * best way to deal with dma that is not sector aligned right now... note
1644 * that in this path we are not using ->data or ->buffer at all. this irs
1645 * can replace cdrom_pc_intr, cdrom_read_intr, and cdrom_write_intr in the
1646 * future.
1648 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1650 struct cdrom_info *info = drive->driver_data;
1651 struct request *rq = HWGROUP(drive)->rq;
1652 int dma_error, dma, stat, ireason, len, thislen;
1653 u8 lowcyl, highcyl;
1654 xfer_func_t *xferfunc;
1655 unsigned long flags;
1657 /* Check for errors. */
1658 dma_error = 0;
1659 dma = info->dma;
1660 if (dma) {
1661 info->dma = 0;
1662 dma_error = HWIF(drive)->ide_dma_end(drive);
1665 if (cdrom_decode_status(drive, 0, &stat))
1666 return ide_stopped;
1669 * using dma, transfer is complete now
1671 if (dma) {
1672 if (dma_error) {
1673 printk("ide-cd: dma error\n");
1674 __ide_dma_off(drive);
1675 return DRIVER(drive)->error(drive, "dma error", stat);
1678 end_that_request_chunk(rq, 1, rq->data_len);
1679 rq->data_len = 0;
1680 goto end_request;
1684 * ok we fall to pio :/
1686 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1687 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1688 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1690 len = lowcyl + (256 * highcyl);
1691 thislen = rq->data_len;
1692 if (thislen > len)
1693 thislen = len;
1696 * If DRQ is clear, the command has completed.
1698 if ((stat & DRQ_STAT) == 0)
1699 goto end_request;
1702 * check which way to transfer data
1704 if (rq_data_dir(rq) == WRITE) {
1706 * write to drive
1708 if (cdrom_write_check_ireason(drive, len, ireason))
1709 return ide_stopped;
1711 xferfunc = HWIF(drive)->atapi_output_bytes;
1712 } else {
1714 * read from drive
1716 if (cdrom_read_check_ireason(drive, len, ireason))
1717 return ide_stopped;
1719 xferfunc = HWIF(drive)->atapi_input_bytes;
1723 * transfer data
1725 while (thislen > 0) {
1726 int blen = blen = rq->data_len;
1727 char *ptr = rq->data;
1730 * bio backed?
1732 if (rq->bio) {
1733 ptr = bio_data(rq->bio);
1734 blen = bio_iovec(rq->bio)->bv_len;
1737 if (!ptr) {
1738 printk("%s: confused, missing data\n", drive->name);
1739 break;
1742 if (blen > thislen)
1743 blen = thislen;
1745 xferfunc(drive, ptr, blen);
1747 thislen -= blen;
1748 len -= blen;
1749 rq->data_len -= blen;
1751 if (rq->bio)
1752 end_that_request_chunk(rq, 1, blen);
1753 else
1754 rq->data += blen;
1758 * pad, if necessary
1760 if (len > 0) {
1761 while (len > 0) {
1762 int pad = 0;
1764 xferfunc(drive, &pad, sizeof(pad));
1765 len -= sizeof(pad);
1769 if (HWGROUP(drive)->handler != NULL)
1770 BUG();
1772 ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, NULL);
1773 return ide_started;
1775 end_request:
1776 if (!rq->data_len)
1777 post_transform_command(rq);
1779 spin_lock_irqsave(&ide_lock, flags);
1780 blkdev_dequeue_request(rq);
1781 end_that_request_last(rq);
1782 HWGROUP(drive)->rq = NULL;
1783 spin_unlock_irqrestore(&ide_lock, flags);
1784 return ide_stopped;
1787 static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1789 int stat, ireason, len, sectors_to_transfer, uptodate;
1790 struct cdrom_info *info = drive->driver_data;
1791 int dma_error = 0, dma = info->dma;
1792 u8 lowcyl = 0, highcyl = 0;
1794 struct request *rq = HWGROUP(drive)->rq;
1796 /* Check for errors. */
1797 if (dma) {
1798 info->dma = 0;
1799 if ((dma_error = HWIF(drive)->ide_dma_end(drive))) {
1800 printk("ide-cd: write dma error\n");
1801 __ide_dma_off(drive);
1805 if (cdrom_decode_status(drive, 0, &stat))
1806 return ide_stopped;
1809 * using dma, transfer is complete now
1811 if (dma) {
1812 if (dma_error)
1813 return DRIVER(drive)->error(drive, "dma error", stat);
1815 ide_end_request(drive, 1, rq->nr_sectors);
1816 return ide_stopped;
1819 /* Read the interrupt reason and the transfer length. */
1820 ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1821 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1822 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1824 len = lowcyl + (256 * highcyl);
1826 /* If DRQ is clear, the command has completed. */
1827 if ((stat & DRQ_STAT) == 0) {
1828 /* If we're not done writing, complain.
1829 * Otherwise, complete the command normally.
1831 uptodate = 1;
1832 if (rq->current_nr_sectors > 0) {
1833 printk("%s: write_intr: data underrun (%d blocks)\n",
1834 drive->name, rq->current_nr_sectors);
1835 uptodate = 0;
1837 cdrom_end_request(drive, uptodate);
1838 return ide_stopped;
1841 /* Check that the drive is expecting to do the same thing we are. */
1842 if (cdrom_write_check_ireason(drive, len, ireason))
1843 return ide_stopped;
1845 sectors_to_transfer = len / SECTOR_SIZE;
1848 * now loop and write out the data
1850 while (sectors_to_transfer > 0) {
1851 int this_transfer;
1853 if (!rq->current_nr_sectors) {
1854 printk("ide-cd: write_intr: oops\n");
1855 break;
1859 * Figure out how many sectors we can transfer
1861 this_transfer = min_t(int, sectors_to_transfer, rq->current_nr_sectors);
1863 while (this_transfer > 0) {
1864 HWIF(drive)->atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
1865 rq->buffer += SECTOR_SIZE;
1866 --rq->nr_sectors;
1867 --rq->current_nr_sectors;
1868 ++rq->sector;
1869 --this_transfer;
1870 --sectors_to_transfer;
1874 * current buffer complete, move on
1876 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1877 cdrom_end_request(drive, 1);
1880 /* re-arm handler */
1881 ide_set_handler(drive, &cdrom_write_intr, ATAPI_WAIT_PC, NULL);
1882 return ide_started;
1885 static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1887 struct request *rq = HWGROUP(drive)->rq;
1889 #if 0 /* the immediate bit */
1890 rq->cmd[1] = 1 << 3;
1891 #endif
1892 rq->timeout = ATAPI_WAIT_PC;
1894 return cdrom_transfer_packet_command(drive, rq, cdrom_write_intr);
1897 static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
1899 struct cdrom_info *info = drive->driver_data;
1900 struct gendisk *g = drive->disk;
1901 unsigned short sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1904 * writes *must* be hardware frame aligned
1906 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1907 (rq->sector & (sectors_per_frame - 1))) {
1908 cdrom_end_request(drive, 0);
1909 return ide_stopped;
1913 * disk has become write protected
1915 if (g->policy) {
1916 cdrom_end_request(drive, 0);
1917 return ide_stopped;
1921 * for dvd-ram and such media, it's a really big deal to get
1922 * big writes all the time. so scour the queue and attempt to
1923 * remerge requests, often the plugging will not have had time
1924 * to do this properly
1926 blk_attempt_remerge(drive->queue, rq);
1928 info->nsectors_buffered = 0;
1930 /* use dma, if possible. we don't need to check more, since we
1931 * know that the transfer is always (at least!) frame aligned */
1932 info->dma = drive->using_dma ? 1 : 0;
1933 info->cmd = WRITE;
1935 /* Start sending the write request to the drive. */
1936 return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1939 static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1941 struct request *rq = HWGROUP(drive)->rq;
1943 if (!rq->timeout)
1944 rq->timeout = ATAPI_WAIT_PC;
1946 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1949 static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1951 struct cdrom_info *info = drive->driver_data;
1953 rq->flags |= REQ_QUIET;
1955 info->dma = 0;
1956 info->cmd = 0;
1959 * sg request
1961 if (rq->bio) {
1962 int mask = drive->queue->dma_alignment;
1963 unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1965 info->cmd = rq_data_dir(rq);
1966 info->dma = drive->using_dma;
1969 * check if dma is safe
1971 if ((rq->data_len & mask) || (addr & mask))
1972 info->dma = 0;
1975 /* Start sending the command to the drive. */
1976 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1979 /****************************************************************************
1980 * cdrom driver request routine.
1982 static ide_startstop_t
1983 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1985 ide_startstop_t action;
1986 struct cdrom_info *info = drive->driver_data;
1988 if (blk_fs_request(rq)) {
1989 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1990 unsigned long elapsed = jiffies - info->start_seek;
1991 int stat = HWIF(drive)->INB(IDE_STATUS_REG);
1993 if ((stat & SEEK_STAT) != SEEK_STAT) {
1994 if (elapsed < IDECD_SEEK_TIMEOUT) {
1995 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1996 return ide_stopped;
1998 printk ("%s: DSC timeout\n", drive->name);
2000 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
2002 if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
2003 action = cdrom_start_seek(drive, block);
2004 } else {
2005 if (rq_data_dir(rq) == READ)
2006 action = cdrom_start_read(drive, block);
2007 else
2008 action = cdrom_start_write(drive, rq);
2010 info->last_block = block;
2011 return action;
2012 } else if (rq->flags & (REQ_PC | REQ_SENSE)) {
2013 return cdrom_do_packet_command(drive);
2014 } else if (rq->flags & REQ_BLOCK_PC) {
2015 return cdrom_do_block_pc(drive, rq);
2016 } else if (rq->flags & REQ_SPECIAL) {
2018 * right now this can only be a reset...
2020 cdrom_end_request(drive, 1);
2021 return ide_stopped;
2024 blk_dump_rq_flags(rq, "ide-cd bad flags");
2025 cdrom_end_request(drive, 0);
2026 return ide_stopped;
2031 /****************************************************************************
2032 * Ioctl handling.
2034 * Routines which queue packet commands take as a final argument a pointer
2035 * to a request_sense struct. If execution of the command results
2036 * in an error with a CHECK CONDITION status, this structure will be filled
2037 * with the results of the subsequent request sense command. The pointer
2038 * can also be NULL, in which case no sense information is returned.
2041 #if ! STANDARD_ATAPI
2042 static inline
2043 int bin2bcd (int x)
2045 return (x%10) | ((x/10) << 4);
2049 static inline
2050 int bcd2bin (int x)
2052 return (x >> 4) * 10 + (x & 0x0f);
2055 static
2056 void msf_from_bcd (struct atapi_msf *msf)
2058 msf->minute = bcd2bin (msf->minute);
2059 msf->second = bcd2bin (msf->second);
2060 msf->frame = bcd2bin (msf->frame);
2063 #endif /* not STANDARD_ATAPI */
2066 static inline
2067 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
2069 lba += CD_MSF_OFFSET;
2070 lba &= 0xffffff; /* negative lbas use only 24 bits */
2071 *m = lba / (CD_SECS * CD_FRAMES);
2072 lba %= (CD_SECS * CD_FRAMES);
2073 *s = lba / CD_FRAMES;
2074 *f = lba % CD_FRAMES;
2078 static inline
2079 int msf_to_lba (byte m, byte s, byte f)
2081 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
2084 static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
2086 struct request req;
2087 struct cdrom_info *info = drive->driver_data;
2088 struct cdrom_device_info *cdi = &info->devinfo;
2090 cdrom_prepare_request(&req);
2092 req.sense = sense;
2093 req.cmd[0] = GPCMD_TEST_UNIT_READY;
2094 req.flags |= REQ_QUIET;
2096 #if ! STANDARD_ATAPI
2097 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
2098 switch CDs instead of supporting the LOAD_UNLOAD opcode */
2100 req.cmd[7] = cdi->sanyo_slot % 3;
2101 #endif /* not STANDARD_ATAPI */
2103 return cdrom_queue_packet_command(drive, &req);
2107 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
2108 static int
2109 cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
2111 struct request_sense my_sense;
2112 struct request req;
2113 int stat;
2115 if (sense == NULL)
2116 sense = &my_sense;
2118 /* If the drive cannot lock the door, just pretend. */
2119 if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
2120 stat = 0;
2121 } else {
2122 cdrom_prepare_request(&req);
2123 req.sense = sense;
2124 req.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2125 req.cmd[4] = lockflag ? 1 : 0;
2126 stat = cdrom_queue_packet_command(drive, &req);
2129 /* If we got an illegal field error, the drive
2130 probably cannot lock the door. */
2131 if (stat != 0 &&
2132 sense->sense_key == ILLEGAL_REQUEST &&
2133 (sense->asc == 0x24 || sense->asc == 0x20)) {
2134 printk ("%s: door locking not supported\n",
2135 drive->name);
2136 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2137 stat = 0;
2140 /* no medium, that's alright. */
2141 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
2142 stat = 0;
2144 if (stat == 0)
2145 CDROM_STATE_FLAGS(drive)->door_locked = lockflag;
2147 return stat;
2151 /* Eject the disk if EJECTFLAG is 0.
2152 If EJECTFLAG is 1, try to reload the disk. */
2153 static int cdrom_eject(ide_drive_t *drive, int ejectflag,
2154 struct request_sense *sense)
2156 struct request req;
2157 char loej = 0x02;
2159 if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
2160 return -EDRIVE_CANT_DO_THIS;
2162 /* reload fails on some drives, if the tray is locked */
2163 if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
2164 return 0;
2166 cdrom_prepare_request(&req);
2168 /* only tell drive to close tray if open, if it can do that */
2169 if (ejectflag && !CDROM_CONFIG_FLAGS(drive)->close_tray)
2170 loej = 0;
2172 req.sense = sense;
2173 req.cmd[0] = GPCMD_START_STOP_UNIT;
2174 req.cmd[4] = loej | (ejectflag != 0);
2175 return cdrom_queue_packet_command(drive, &req);
2178 static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
2179 unsigned long *sectors_per_frame,
2180 struct request_sense *sense)
2182 struct {
2183 __u32 lba;
2184 __u32 blocklen;
2185 } capbuf;
2187 int stat;
2188 struct request req;
2190 cdrom_prepare_request(&req);
2192 req.sense = sense;
2193 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
2194 req.data = (char *)&capbuf;
2195 req.data_len = sizeof(capbuf);
2197 stat = cdrom_queue_packet_command(drive, &req);
2198 if (stat == 0) {
2199 *capacity = 1 + be32_to_cpu(capbuf.lba);
2200 *sectors_per_frame =
2201 be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
2204 return stat;
2207 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
2208 int format, char *buf, int buflen,
2209 struct request_sense *sense)
2211 struct request req;
2213 cdrom_prepare_request(&req);
2215 req.sense = sense;
2216 req.data = buf;
2217 req.data_len = buflen;
2218 req.flags |= REQ_QUIET;
2219 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2220 req.cmd[6] = trackno;
2221 req.cmd[7] = (buflen >> 8);
2222 req.cmd[8] = (buflen & 0xff);
2223 req.cmd[9] = (format << 6);
2225 if (msf_flag)
2226 req.cmd[1] = 2;
2228 return cdrom_queue_packet_command(drive, &req);
2232 /* Try to read the entire TOC for the disk into our internal buffer. */
2233 static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
2235 int stat, ntracks, i;
2236 struct cdrom_info *info = drive->driver_data;
2237 struct cdrom_device_info *cdi = &info->devinfo;
2238 struct atapi_toc *toc = info->toc;
2239 struct {
2240 struct atapi_toc_header hdr;
2241 struct atapi_toc_entry ent;
2242 } ms_tmp;
2243 long last_written;
2244 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
2246 if (toc == NULL) {
2247 /* Try to allocate space. */
2248 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
2249 GFP_KERNEL);
2250 info->toc = toc;
2251 if (toc == NULL) {
2252 printk ("%s: No cdrom TOC buffer!\n", drive->name);
2253 return -ENOMEM;
2257 /* Check to see if the existing data is still valid.
2258 If it is, just return. */
2259 (void) cdrom_check_status(drive, sense);
2261 if (CDROM_STATE_FLAGS(drive)->toc_valid)
2262 return 0;
2264 /* Try to get the total cdrom capacity and sector size. */
2265 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
2266 sense);
2267 if (stat)
2268 toc->capacity = 0x1fffff;
2270 set_capacity(drive->disk, toc->capacity * sectors_per_frame);
2271 blk_queue_hardsect_size(drive->queue,
2272 sectors_per_frame << SECTOR_BITS);
2274 /* First read just the header, so we know how long the TOC is. */
2275 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
2276 sizeof(struct atapi_toc_header), sense);
2277 if (stat) return stat;
2279 #if ! STANDARD_ATAPI
2280 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2281 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2282 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2284 #endif /* not STANDARD_ATAPI */
2286 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2287 if (ntracks <= 0)
2288 return -EIO;
2289 if (ntracks > MAX_TRACKS)
2290 ntracks = MAX_TRACKS;
2292 /* Now read the whole schmeer. */
2293 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
2294 (char *)&toc->hdr,
2295 sizeof(struct atapi_toc_header) +
2296 (ntracks + 1) *
2297 sizeof(struct atapi_toc_entry), sense);
2299 if (stat && toc->hdr.first_track > 1) {
2300 /* Cds with CDI tracks only don't have any TOC entries,
2301 despite of this the returned values are
2302 first_track == last_track = number of CDI tracks + 1,
2303 so that this case is indistinguishable from the same
2304 layout plus an additional audio track.
2305 If we get an error for the regular case, we assume
2306 a CDI without additional audio tracks. In this case
2307 the readable TOC is empty (CDI tracks are not included)
2308 and only holds the Leadout entry. Heiko Eißfeldt */
2309 ntracks = 0;
2310 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
2311 (char *)&toc->hdr,
2312 sizeof(struct atapi_toc_header) +
2313 (ntracks + 1) *
2314 sizeof(struct atapi_toc_entry),
2315 sense);
2316 if (stat) {
2317 return stat;
2319 #if ! STANDARD_ATAPI
2320 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2321 toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
2322 toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
2323 } else
2324 #endif /* not STANDARD_ATAPI */
2326 toc->hdr.first_track = CDROM_LEADOUT;
2327 toc->hdr.last_track = CDROM_LEADOUT;
2331 if (stat)
2332 return stat;
2334 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
2336 #if ! STANDARD_ATAPI
2337 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2338 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2339 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2341 #endif /* not STANDARD_ATAPI */
2343 for (i=0; i<=ntracks; i++) {
2344 #if ! STANDARD_ATAPI
2345 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
2346 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd)
2347 toc->ent[i].track = bcd2bin(toc->ent[i].track);
2348 msf_from_bcd(&toc->ent[i].addr.msf);
2350 #endif /* not STANDARD_ATAPI */
2351 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
2352 toc->ent[i].addr.msf.second,
2353 toc->ent[i].addr.msf.frame);
2356 /* Read the multisession information. */
2357 if (toc->hdr.first_track != CDROM_LEADOUT) {
2358 /* Read the multisession information. */
2359 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
2360 sizeof(ms_tmp), sense);
2361 if (stat) return stat;
2362 } else {
2363 ms_tmp.ent.addr.msf.minute = 0;
2364 ms_tmp.ent.addr.msf.second = 2;
2365 ms_tmp.ent.addr.msf.frame = 0;
2366 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
2369 #if ! STANDARD_ATAPI
2370 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd)
2371 msf_from_bcd (&ms_tmp.ent.addr.msf);
2372 #endif /* not STANDARD_ATAPI */
2374 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
2375 ms_tmp.ent.addr.msf.second,
2376 ms_tmp.ent.addr.msf.frame);
2378 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
2380 /* Now try to get the total cdrom capacity. */
2381 stat = cdrom_get_last_written(cdi, &last_written);
2382 if (!stat && (last_written > toc->capacity)) {
2383 toc->capacity = last_written;
2384 set_capacity(drive->disk, toc->capacity * sectors_per_frame);
2387 /* Remember that we've read this stuff. */
2388 CDROM_STATE_FLAGS(drive)->toc_valid = 1;
2390 return 0;
2394 static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2395 int buflen, struct request_sense *sense)
2397 struct request req;
2399 cdrom_prepare_request(&req);
2401 req.sense = sense;
2402 req.data = buf;
2403 req.data_len = buflen;
2404 req.cmd[0] = GPCMD_READ_SUBCHANNEL;
2405 req.cmd[1] = 2; /* MSF addressing */
2406 req.cmd[2] = 0x40; /* request subQ data */
2407 req.cmd[3] = format;
2408 req.cmd[7] = (buflen >> 8);
2409 req.cmd[8] = (buflen & 0xff);
2410 return cdrom_queue_packet_command(drive, &req);
2413 /* ATAPI cdrom drives are free to select the speed you request or any slower
2414 rate :-( Requesting too fast a speed will _not_ produce an error. */
2415 static int cdrom_select_speed(ide_drive_t *drive, int speed,
2416 struct request_sense *sense)
2418 struct request req;
2419 cdrom_prepare_request(&req);
2421 req.sense = sense;
2422 if (speed == 0)
2423 speed = 0xffff; /* set to max */
2424 else
2425 speed *= 177; /* Nx to kbytes/s */
2427 req.cmd[0] = GPCMD_SET_SPEED;
2428 /* Read Drive speed in kbytes/second MSB */
2429 req.cmd[2] = (speed >> 8) & 0xff;
2430 /* Read Drive speed in kbytes/second LSB */
2431 req.cmd[3] = speed & 0xff;
2432 if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
2433 CDROM_CONFIG_FLAGS(drive)->cd_rw ||
2434 CDROM_CONFIG_FLAGS(drive)->dvd_r) {
2435 /* Write Drive speed in kbytes/second MSB */
2436 req.cmd[4] = (speed >> 8) & 0xff;
2437 /* Write Drive speed in kbytes/second LSB */
2438 req.cmd[5] = speed & 0xff;
2441 return cdrom_queue_packet_command(drive, &req);
2444 static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2446 struct request_sense sense;
2447 struct request req;
2449 cdrom_prepare_request(&req);
2451 req.sense = &sense;
2452 req.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2453 lba_to_msf(lba_start, &req.cmd[3], &req.cmd[4], &req.cmd[5]);
2454 lba_to_msf(lba_end-1, &req.cmd[6], &req.cmd[7], &req.cmd[8]);
2456 return cdrom_queue_packet_command(drive, &req);
2459 static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2460 struct atapi_toc_entry **ent)
2462 struct cdrom_info *info = drive->driver_data;
2463 struct atapi_toc *toc = info->toc;
2464 int ntracks;
2467 * don't serve cached data, if the toc isn't valid
2469 if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2470 return -EINVAL;
2472 /* Check validity of requested track number. */
2473 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2474 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2475 if (track == CDROM_LEADOUT)
2476 *ent = &toc->ent[ntracks];
2477 else if (track < toc->hdr.first_track ||
2478 track > toc->hdr.last_track)
2479 return -EINVAL;
2480 else
2481 *ent = &toc->ent[track - toc->hdr.first_track];
2483 return 0;
2486 /* the generic packet interface to cdrom.c */
2487 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2488 struct packet_command *cgc)
2490 struct request req;
2491 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2493 if (cgc->timeout <= 0)
2494 cgc->timeout = ATAPI_WAIT_PC;
2496 /* here we queue the commands from the uniform CD-ROM
2497 layer. the packet must be complete, as we do not
2498 touch it at all. */
2499 cdrom_prepare_request(&req);
2500 memcpy(req.cmd, cgc->cmd, CDROM_PACKET_SIZE);
2501 if (cgc->sense)
2502 memset(cgc->sense, 0, sizeof(struct request_sense));
2503 req.data = cgc->buffer;
2504 req.data_len = cgc->buflen;
2505 req.timeout = cgc->timeout;
2507 if (cgc->quiet)
2508 req.flags |= REQ_QUIET;
2510 req.sense = cgc->sense;
2511 cgc->stat = cdrom_queue_packet_command(drive, &req);
2512 if (!cgc->stat)
2513 cgc->buflen -= req.data_len;
2514 return cgc->stat;
2517 static
2518 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
2519 unsigned int cmd, unsigned long arg)
2521 struct packet_command cgc;
2522 char buffer[16];
2523 int stat;
2525 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2527 /* These will be moved into the Uniform layer shortly... */
2528 switch (cmd) {
2529 case CDROMSETSPINDOWN: {
2530 char spindown;
2532 if (copy_from_user(&spindown, (void __user *) arg, sizeof(char)))
2533 return -EFAULT;
2535 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2536 return stat;
2538 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2540 return cdrom_mode_select(cdi, &cgc);
2543 case CDROMGETSPINDOWN: {
2544 char spindown;
2546 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2547 return stat;
2549 spindown = buffer[11] & 0x0f;
2551 if (copy_to_user((void __user *) arg, &spindown, sizeof (char)))
2552 return -EFAULT;
2554 return 0;
2557 default:
2558 return -EINVAL;
2563 static
2564 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2565 unsigned int cmd, void *arg)
2568 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2569 struct cdrom_info *info = drive->driver_data;
2570 int stat;
2572 switch (cmd) {
2574 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2575 * atapi doesn't support it
2577 case CDROMPLAYTRKIND: {
2578 unsigned long lba_start, lba_end;
2579 struct cdrom_ti *ti = (struct cdrom_ti *)arg;
2580 struct atapi_toc_entry *first_toc, *last_toc;
2582 stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2583 if (stat)
2584 return stat;
2586 stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2587 if (stat)
2588 return stat;
2590 if (ti->cdti_trk1 != CDROM_LEADOUT)
2591 ++last_toc;
2592 lba_start = first_toc->addr.lba;
2593 lba_end = last_toc->addr.lba;
2595 if (lba_end <= lba_start)
2596 return -EINVAL;
2598 return cdrom_play_audio(drive, lba_start, lba_end);
2601 case CDROMREADTOCHDR: {
2602 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2603 struct atapi_toc *toc;
2605 /* Make sure our saved TOC is valid. */
2606 stat = cdrom_read_toc(drive, NULL);
2607 if (stat) return stat;
2609 toc = info->toc;
2610 tochdr->cdth_trk0 = toc->hdr.first_track;
2611 tochdr->cdth_trk1 = toc->hdr.last_track;
2613 return 0;
2616 case CDROMREADTOCENTRY: {
2617 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
2618 struct atapi_toc_entry *toce;
2620 stat = cdrom_get_toc_entry(drive, tocentry->cdte_track, &toce);
2621 if (stat) return stat;
2623 tocentry->cdte_ctrl = toce->control;
2624 tocentry->cdte_adr = toce->adr;
2625 if (tocentry->cdte_format == CDROM_MSF) {
2626 lba_to_msf (toce->addr.lba,
2627 &tocentry->cdte_addr.msf.minute,
2628 &tocentry->cdte_addr.msf.second,
2629 &tocentry->cdte_addr.msf.frame);
2630 } else
2631 tocentry->cdte_addr.lba = toce->addr.lba;
2633 return 0;
2636 default:
2637 return -EINVAL;
2641 static
2642 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2644 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2645 struct request_sense sense;
2646 struct request req;
2647 int ret;
2649 cdrom_prepare_request(&req);
2650 req.flags = REQ_SPECIAL | REQ_QUIET;
2651 ret = ide_do_drive_cmd(drive, &req, ide_wait);
2654 * A reset will unlock the door. If it was previously locked,
2655 * lock it again.
2657 if (CDROM_STATE_FLAGS(drive)->door_locked)
2658 (void) cdrom_lockdoor(drive, 1, &sense);
2660 return ret;
2664 static
2665 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2667 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2668 struct request_sense sense;
2670 if (position) {
2671 int stat = cdrom_lockdoor(drive, 0, &sense);
2672 if (stat) return stat;
2675 return cdrom_eject(drive, !position, &sense);
2678 static
2679 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2681 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2682 return cdrom_lockdoor(drive, lock, NULL);
2685 static
2686 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2688 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2689 struct request_sense sense;
2690 int stat;
2692 if ((stat = cdrom_select_speed(drive, speed, &sense)) < 0)
2693 return stat;
2695 cdi->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2696 return 0;
2700 * add logic to try GET_EVENT command first to check for media and tray
2701 * status. this should be supported by newer cd-r/w and all DVD etc
2702 * drives
2704 static
2705 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2707 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2708 struct media_event_desc med;
2709 struct request_sense sense;
2710 int stat;
2712 if (slot_nr != CDSL_CURRENT)
2713 return -EINVAL;
2715 stat = cdrom_check_status(drive, &sense);
2716 if (!stat || sense.sense_key == UNIT_ATTENTION)
2717 return CDS_DISC_OK;
2719 if (!cdrom_get_media_event(cdi, &med)) {
2720 if (med.media_present)
2721 return CDS_DISC_OK;
2722 else if (med.door_open)
2723 return CDS_TRAY_OPEN;
2724 else
2725 return CDS_NO_DISC;
2728 if (sense.sense_key == NOT_READY && sense.asc == 0x04 && sense.ascq == 0x04)
2729 return CDS_DISC_OK;
2732 * If not using Mt Fuji extended media tray reports,
2733 * just return TRAY_OPEN since ATAPI doesn't provide
2734 * any other way to detect this...
2736 if (sense.sense_key == NOT_READY) {
2737 if (sense.asc == 0x3a) {
2738 if (sense.ascq == 0 || sense.ascq == 1)
2739 return CDS_NO_DISC;
2740 else if (sense.ascq == 2)
2741 return CDS_TRAY_OPEN;
2745 return CDS_DRIVE_NOT_READY;
2748 static
2749 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2750 struct cdrom_multisession *ms_info)
2752 struct atapi_toc *toc;
2753 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2754 struct cdrom_info *info = drive->driver_data;
2755 struct request_sense sense;
2756 int ret;
2758 if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2759 if ((ret = cdrom_read_toc(drive, &sense)))
2760 return ret;
2762 toc = info->toc;
2763 ms_info->addr.lba = toc->last_session_lba;
2764 ms_info->xa_flag = toc->xa_flag;
2766 return 0;
2769 static
2770 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2771 struct cdrom_mcn *mcn_info)
2773 int stat;
2774 char mcnbuf[24];
2775 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2777 /* get MCN */
2778 if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2779 return stat;
2781 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2782 sizeof (mcn_info->medium_catalog_number)-1);
2783 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2784 = '\0';
2786 return 0;
2791 /****************************************************************************
2792 * Other driver requests (open, close, check media change).
2795 static
2796 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2797 int slot_nr)
2799 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2800 int retval;
2802 if (slot_nr == CDSL_CURRENT) {
2803 (void) cdrom_check_status(drive, NULL);
2804 retval = CDROM_STATE_FLAGS(drive)->media_changed;
2805 CDROM_STATE_FLAGS(drive)->media_changed = 0;
2806 return retval;
2807 } else {
2808 return -EINVAL;
2813 static
2814 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2816 return 0;
2820 * Close down the device. Invalidate all cached blocks.
2823 static
2824 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2826 ide_drive_t *drive = cdi->handle;
2828 if (!cdi->use_count)
2829 CDROM_STATE_FLAGS(drive)->toc_valid = 0;
2834 /****************************************************************************
2835 * Device initialization.
2837 static struct cdrom_device_ops ide_cdrom_dops = {
2838 .open = ide_cdrom_open_real,
2839 .release = ide_cdrom_release_real,
2840 .drive_status = ide_cdrom_drive_status,
2841 .media_changed = ide_cdrom_check_media_change_real,
2842 .tray_move = ide_cdrom_tray_move,
2843 .lock_door = ide_cdrom_lock_door,
2844 .select_speed = ide_cdrom_select_speed,
2845 .get_last_session = ide_cdrom_get_last_session,
2846 .get_mcn = ide_cdrom_get_mcn,
2847 .reset = ide_cdrom_reset,
2848 .audio_ioctl = ide_cdrom_audio_ioctl,
2849 .dev_ioctl = ide_cdrom_dev_ioctl,
2850 .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2851 CDC_SELECT_SPEED | CDC_SELECT_DISC |
2852 CDC_MULTI_SESSION | CDC_MCN |
2853 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
2854 CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
2855 CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2856 CDC_GENERIC_PACKET | CDC_MO_DRIVE | CDC_MRW |
2857 CDC_MRW_W | CDC_RAM,
2858 .generic_packet = ide_cdrom_packet,
2861 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2863 struct cdrom_info *info = drive->driver_data;
2864 struct cdrom_device_info *devinfo = &info->devinfo;
2866 devinfo->ops = &ide_cdrom_dops;
2867 devinfo->mask = 0;
2868 devinfo->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2869 devinfo->capacity = nslots;
2870 devinfo->handle = (void *) drive;
2871 strcpy(devinfo->name, drive->name);
2873 /* set capability mask to match the probe. */
2874 if (!CDROM_CONFIG_FLAGS(drive)->cd_r)
2875 devinfo->mask |= CDC_CD_R;
2876 if (!CDROM_CONFIG_FLAGS(drive)->cd_rw)
2877 devinfo->mask |= CDC_CD_RW;
2878 if (!CDROM_CONFIG_FLAGS(drive)->dvd)
2879 devinfo->mask |= CDC_DVD;
2880 if (!CDROM_CONFIG_FLAGS(drive)->dvd_r)
2881 devinfo->mask |= CDC_DVD_R;
2882 if (!CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2883 devinfo->mask |= CDC_DVD_RAM;
2884 if (!CDROM_CONFIG_FLAGS(drive)->is_changer)
2885 devinfo->mask |= CDC_SELECT_DISC;
2886 if (!CDROM_CONFIG_FLAGS(drive)->audio_play)
2887 devinfo->mask |= CDC_PLAY_AUDIO;
2888 if (!CDROM_CONFIG_FLAGS(drive)->close_tray)
2889 devinfo->mask |= CDC_CLOSE_TRAY;
2890 if (!CDROM_CONFIG_FLAGS(drive)->mo_drive)
2891 devinfo->mask |= CDC_MO_DRIVE;
2893 devinfo->disk = drive->disk;
2894 return register_cdrom(devinfo);
2897 static
2898 int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2900 struct cdrom_info *info = drive->driver_data;
2901 struct cdrom_device_info *cdi = &info->devinfo;
2902 struct packet_command cgc;
2903 int stat, attempts = 3, size = sizeof(*cap);
2906 * ACER50 (and others?) require the full spec length mode sense
2907 * page capabilities size, but older drives break.
2909 if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2910 !strcmp(drive->id->model, "WPI CDS-32X")))
2911 size -= sizeof(cap->pad);
2913 init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2914 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2915 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2916 if (!stat)
2917 break;
2918 } while (--attempts);
2919 return stat;
2922 static
2923 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2925 struct cdrom_info *info = drive->driver_data;
2926 struct cdrom_device_info *cdi = &info->devinfo;
2927 struct atapi_capabilities_page cap;
2928 int nslots = 1;
2930 if (drive->media == ide_optical) {
2931 CDROM_CONFIG_FLAGS(drive)->mo_drive = 1;
2932 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2933 printk("%s: ATAPI magneto-optical drive\n", drive->name);
2934 return nslots;
2937 if (CDROM_CONFIG_FLAGS(drive)->nec260 ||
2938 !strcmp(drive->id->model,"STINGRAY 8422 IDE 8X CD-ROM 7-27-95")) {
2939 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2940 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2941 return nslots;
2945 * we have to cheat a little here. the packet will eventually
2946 * be queued with ide_cdrom_packet(), which extracts the
2947 * drive from cdi->handle. Since this device hasn't been
2948 * registered with the Uniform layer yet, it can't do this.
2949 * Same goes for cdi->ops.
2951 cdi->handle = (ide_drive_t *) drive;
2952 cdi->ops = &ide_cdrom_dops;
2954 if (ide_cdrom_get_capabilities(drive, &cap))
2955 return 0;
2957 if (cap.lock == 0)
2958 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2959 if (cap.eject)
2960 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2961 if (cap.cd_r_write)
2962 CDROM_CONFIG_FLAGS(drive)->cd_r = 1;
2963 if (cap.cd_rw_write)
2964 CDROM_CONFIG_FLAGS(drive)->cd_rw = 1;
2965 if (cap.test_write)
2966 CDROM_CONFIG_FLAGS(drive)->test_write = 1;
2967 if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2968 CDROM_CONFIG_FLAGS(drive)->dvd = 1;
2969 if (cap.dvd_ram_write) {
2970 CDROM_CONFIG_FLAGS(drive)->dvd_ram = 1;
2971 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2973 if (cap.dvd_r_write)
2974 CDROM_CONFIG_FLAGS(drive)->dvd_r = 1;
2975 if (cap.audio_play)
2976 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2977 if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2978 CDROM_CONFIG_FLAGS(drive)->close_tray = 0;
2980 /* Some drives used by Apple don't advertise audio play
2981 * but they do support reading TOC & audio datas
2983 if (strcmp(drive->id->model, "MATSHITADVD-ROM SR-8187") == 0 ||
2984 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8186") == 0 ||
2985 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8176") == 0 ||
2986 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8174") == 0)
2987 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2989 #if ! STANDARD_ATAPI
2990 if (cdi->sanyo_slot > 0) {
2991 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2992 nslots = 3;
2995 else
2996 #endif /* not STANDARD_ATAPI */
2997 if (cap.mechtype == mechtype_individual_changer ||
2998 cap.mechtype == mechtype_cartridge_changer) {
2999 if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
3000 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
3001 CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 1;
3005 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
3006 if (!drive->id->model[0] &&
3007 !strncmp(drive->id->fw_rev, "241N", 4)) {
3008 CDROM_STATE_FLAGS(drive)->current_speed =
3009 (((unsigned int)cap.curspeed) + (176/2)) / 176;
3010 CDROM_CONFIG_FLAGS(drive)->max_speed =
3011 (((unsigned int)cap.maxspeed) + (176/2)) / 176;
3012 } else {
3013 CDROM_STATE_FLAGS(drive)->current_speed =
3014 (ntohs(cap.curspeed) + (176/2)) / 176;
3015 CDROM_CONFIG_FLAGS(drive)->max_speed =
3016 (ntohs(cap.maxspeed) + (176/2)) / 176;
3019 /* don't print speed if the drive reported 0.
3021 printk("%s: ATAPI", drive->name);
3022 if (CDROM_CONFIG_FLAGS(drive)->max_speed)
3023 printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
3024 printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
3026 if (CDROM_CONFIG_FLAGS(drive)->dvd_r|CDROM_CONFIG_FLAGS(drive)->dvd_ram)
3027 printk(" DVD%s%s",
3028 (CDROM_CONFIG_FLAGS(drive)->dvd_r)? "-R" : "",
3029 (CDROM_CONFIG_FLAGS(drive)->dvd_ram)? "-RAM" : "");
3031 if (CDROM_CONFIG_FLAGS(drive)->cd_r|CDROM_CONFIG_FLAGS(drive)->cd_rw)
3032 printk(" CD%s%s",
3033 (CDROM_CONFIG_FLAGS(drive)->cd_r)? "-R" : "",
3034 (CDROM_CONFIG_FLAGS(drive)->cd_rw)? "/RW" : "");
3036 if (CDROM_CONFIG_FLAGS(drive)->is_changer)
3037 printk(" changer w/%d slots", nslots);
3038 else
3039 printk(" drive");
3041 printk(", %dkB Cache", be16_to_cpu(cap.buffer_size));
3043 #ifdef CONFIG_BLK_DEV_IDEDMA
3044 if (drive->using_dma)
3045 (void) HWIF(drive)->ide_dma_verbose(drive);
3046 #endif /* CONFIG_BLK_DEV_IDEDMA */
3047 printk("\n");
3049 return nslots;
3052 static void ide_cdrom_add_settings(ide_drive_t *drive)
3054 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3058 * standard prep_rq_fn that builds 10 byte cmds
3060 static int ide_cdrom_prep_fs(request_queue_t *q, struct request *rq)
3062 int hard_sect = queue_hardsect_size(q);
3063 long block = (long)rq->hard_sector / (hard_sect >> 9);
3064 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
3066 memset(rq->cmd, 0, sizeof(rq->cmd));
3068 if (rq_data_dir(rq) == READ)
3069 rq->cmd[0] = GPCMD_READ_10;
3070 else
3071 rq->cmd[0] = GPCMD_WRITE_10;
3074 * fill in lba
3076 rq->cmd[2] = (block >> 24) & 0xff;
3077 rq->cmd[3] = (block >> 16) & 0xff;
3078 rq->cmd[4] = (block >> 8) & 0xff;
3079 rq->cmd[5] = block & 0xff;
3082 * and transfer length
3084 rq->cmd[7] = (blocks >> 8) & 0xff;
3085 rq->cmd[8] = blocks & 0xff;
3086 rq->cmd_len = 10;
3087 return BLKPREP_OK;
3091 * Most of the SCSI commands are supported directly by ATAPI devices.
3092 * This transform handles the few exceptions.
3094 static int ide_cdrom_prep_pc(struct request *rq)
3096 u8 *c = rq->cmd;
3099 * Transform 6-byte read/write commands to the 10-byte version
3101 if (c[0] == READ_6 || c[0] == WRITE_6) {
3102 c[8] = c[4];
3103 c[5] = c[3];
3104 c[4] = c[2];
3105 c[3] = c[1] & 0x1f;
3106 c[2] = 0;
3107 c[1] &= 0xe0;
3108 c[0] += (READ_10 - READ_6);
3109 rq->cmd_len = 10;
3110 return BLKPREP_OK;
3114 * it's silly to pretend we understand 6-byte sense commands, just
3115 * reject with ILLEGAL_REQUEST and the caller should take the
3116 * appropriate action
3118 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
3119 rq->errors = ILLEGAL_REQUEST;
3120 return BLKPREP_KILL;
3123 return BLKPREP_OK;
3126 static int ide_cdrom_prep_fn(request_queue_t *q, struct request *rq)
3128 if (rq->flags & REQ_CMD)
3129 return ide_cdrom_prep_fs(q, rq);
3130 else if (rq->flags & REQ_BLOCK_PC)
3131 return ide_cdrom_prep_pc(rq);
3133 return 0;
3136 static
3137 int ide_cdrom_setup (ide_drive_t *drive)
3139 struct cdrom_info *info = drive->driver_data;
3140 struct cdrom_device_info *cdi = &info->devinfo;
3141 int nslots;
3143 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
3144 blk_queue_dma_alignment(drive->queue, 31);
3145 drive->queue->unplug_delay = (1 * HZ) / 1000;
3146 if (!drive->queue->unplug_delay)
3147 drive->queue->unplug_delay = 1;
3149 drive->special.all = 0;
3150 drive->ready_stat = 0;
3152 CDROM_STATE_FLAGS(drive)->media_changed = 1;
3153 CDROM_STATE_FLAGS(drive)->toc_valid = 0;
3154 CDROM_STATE_FLAGS(drive)->door_locked = 0;
3156 #if NO_DOOR_LOCKING
3157 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
3158 #else
3159 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 0;
3160 #endif
3162 CDROM_CONFIG_FLAGS(drive)->drq_interrupt = ((drive->id->config & 0x0060) == 0x20);
3163 CDROM_CONFIG_FLAGS(drive)->is_changer = 0;
3164 CDROM_CONFIG_FLAGS(drive)->cd_r = 0;
3165 CDROM_CONFIG_FLAGS(drive)->cd_rw = 0;
3166 CDROM_CONFIG_FLAGS(drive)->test_write = 0;
3167 CDROM_CONFIG_FLAGS(drive)->dvd = 0;
3168 CDROM_CONFIG_FLAGS(drive)->dvd_r = 0;
3169 CDROM_CONFIG_FLAGS(drive)->dvd_ram = 0;
3170 CDROM_CONFIG_FLAGS(drive)->no_eject = 1;
3171 CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 0;
3172 CDROM_CONFIG_FLAGS(drive)->audio_play = 0;
3173 CDROM_CONFIG_FLAGS(drive)->close_tray = 1;
3175 /* limit transfer size per interrupt. */
3176 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 0;
3177 /* a testament to the nice quality of Samsung drives... */
3178 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
3179 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
3180 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
3181 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
3182 /* the 3231 model does not support the SET_CD_SPEED command */
3183 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
3184 cdi->mask |= CDC_SELECT_SPEED;
3186 #if ! STANDARD_ATAPI
3187 /* by default Sanyo 3 CD changer support is turned off and
3188 ATAPI Rev 2.2+ standard support for CD changers is used */
3189 cdi->sanyo_slot = 0;
3191 CDROM_CONFIG_FLAGS(drive)->nec260 = 0;
3192 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 0;
3193 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 0;
3194 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 0;
3195 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 0;
3197 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
3198 drive->id->fw_rev[4] == '1' &&
3199 drive->id->fw_rev[6] <= '2') {
3200 /* Vertos 300.
3201 Some versions of this drive like to talk BCD. */
3202 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
3203 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
3204 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3205 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3208 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
3209 drive->id->fw_rev[4] == '1' &&
3210 drive->id->fw_rev[6] <= '2') {
3211 /* Vertos 600 ESD. */
3212 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
3214 else if (strcmp(drive->id->model, "NEC CD-ROM DRIVE:260") == 0 &&
3215 strncmp(drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
3216 /* Old NEC260 (not R).
3217 This drive was released before the 1.2 version
3218 of the spec. */
3219 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
3220 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3221 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3222 CDROM_CONFIG_FLAGS(drive)->nec260 = 1;
3224 else if (strcmp(drive->id->model, "WEARNES CDD-120") == 0 &&
3225 strncmp(drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
3226 /* Wearnes */
3227 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3228 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3230 /* Sanyo 3 CD changer uses a non-standard command
3231 for CD changing */
3232 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
3233 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
3234 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
3235 /* uses CD in slot 0 when value is set to 3 */
3236 cdi->sanyo_slot = 3;
3238 #endif /* not STANDARD_ATAPI */
3240 info->toc = NULL;
3241 info->buffer = NULL;
3242 info->sector_buffered = 0;
3243 info->nsectors_buffered = 0;
3244 info->changer_info = NULL;
3245 info->last_block = 0;
3246 info->start_seek = 0;
3248 nslots = ide_cdrom_probe_capabilities (drive);
3251 * set correct block size
3253 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
3255 #if 0
3256 drive->dsc_overlap = (HWIF(drive)->no_dsc) ? 0 : 1;
3257 if (HWIF(drive)->no_dsc) {
3258 printk(KERN_INFO "ide-cd: %s: disabling DSC overlap\n",
3259 drive->name);
3260 drive->dsc_overlap = 0;
3262 #endif
3264 if (ide_cdrom_register(drive, nslots)) {
3265 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
3266 info->devinfo.handle = NULL;
3267 return 1;
3269 ide_cdrom_add_settings(drive);
3270 return 0;
3273 static
3274 sector_t ide_cdrom_capacity (ide_drive_t *drive)
3276 unsigned long capacity, sectors_per_frame;
3278 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
3279 return 0;
3281 return capacity * sectors_per_frame;
3284 static
3285 int ide_cdrom_cleanup(ide_drive_t *drive)
3287 struct cdrom_info *info = drive->driver_data;
3288 struct cdrom_device_info *devinfo = &info->devinfo;
3289 struct gendisk *g = drive->disk;
3291 if (ide_unregister_subdriver(drive)) {
3292 printk("%s: %s: failed to ide_unregister_subdriver\n",
3293 __FUNCTION__, drive->name);
3294 return 1;
3296 if (info->buffer != NULL)
3297 kfree(info->buffer);
3298 if (info->toc != NULL)
3299 kfree(info->toc);
3300 if (info->changer_info != NULL)
3301 kfree(info->changer_info);
3302 if (devinfo->handle == drive && unregister_cdrom(devinfo))
3303 printk("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3304 kfree(info);
3305 drive->driver_data = NULL;
3306 blk_queue_prep_rq(drive->queue, NULL);
3307 del_gendisk(g);
3308 g->fops = ide_fops;
3309 return 0;
3312 static int ide_cdrom_attach (ide_drive_t *drive);
3315 * Power Management state machine.
3317 * We don't do much for CDs right now.
3320 static void ide_cdrom_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
3324 static ide_startstop_t ide_cdrom_start_power_step (ide_drive_t *drive, struct request *rq)
3326 ide_task_t *args = rq->special;
3328 memset(args, 0, sizeof(*args));
3330 switch (rq->pm->pm_step) {
3331 case ide_pm_state_start_suspend:
3332 break;
3334 case ide_pm_state_start_resume: /* Resume step 1 (restore DMA) */
3336 * Right now, all we do is call hwif->ide_dma_check(drive),
3337 * we could be smarter and check for current xfer_speed
3338 * in struct drive etc...
3339 * Also, this step could be implemented as a generic helper
3340 * as most subdrivers will use it.
3342 if ((drive->id->capability & 1) == 0)
3343 break;
3344 if (HWIF(drive)->ide_dma_check == NULL)
3345 break;
3346 HWIF(drive)->ide_dma_check(drive);
3347 break;
3349 rq->pm->pm_step = ide_pm_state_completed;
3350 return ide_stopped;
3353 static ide_driver_t ide_cdrom_driver = {
3354 .owner = THIS_MODULE,
3355 .name = "ide-cdrom",
3356 .version = IDECD_VERSION,
3357 .media = ide_cdrom,
3358 .busy = 0,
3359 .supports_dsc_overlap = 1,
3360 .cleanup = ide_cdrom_cleanup,
3361 .do_request = ide_do_rw_cdrom,
3362 .sense = ide_dump_atapi_status,
3363 .error = ide_cdrom_error,
3364 .abort = ide_cdrom_abort,
3365 .capacity = ide_cdrom_capacity,
3366 .attach = ide_cdrom_attach,
3367 .drives = LIST_HEAD_INIT(ide_cdrom_driver.drives),
3368 .start_power_step = ide_cdrom_start_power_step,
3369 .complete_power_step = ide_cdrom_complete_power_step,
3372 static int idecd_open(struct inode * inode, struct file * file)
3374 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
3375 struct cdrom_info *info = drive->driver_data;
3376 int rc = -ENOMEM;
3377 drive->usage++;
3379 if (!info->buffer)
3380 info->buffer = kmalloc(SECTOR_BUFFER_SIZE,
3381 GFP_KERNEL|__GFP_REPEAT);
3382 if (!info->buffer || (rc = cdrom_open(&info->devinfo, inode, file)))
3383 drive->usage--;
3384 return rc;
3387 static int idecd_release(struct inode * inode, struct file * file)
3389 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
3390 struct cdrom_info *info = drive->driver_data;
3392 cdrom_release (&info->devinfo, file);
3393 drive->usage--;
3394 return 0;
3397 static int idecd_ioctl (struct inode *inode, struct file *file,
3398 unsigned int cmd, unsigned long arg)
3400 struct block_device *bdev = inode->i_bdev;
3401 ide_drive_t *drive = bdev->bd_disk->private_data;
3402 int err = generic_ide_ioctl(file, bdev, cmd, arg);
3403 if (err == -EINVAL) {
3404 struct cdrom_info *info = drive->driver_data;
3405 err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
3407 return err;
3410 static int idecd_media_changed(struct gendisk *disk)
3412 ide_drive_t *drive = disk->private_data;
3413 struct cdrom_info *info = drive->driver_data;
3414 return cdrom_media_changed(&info->devinfo);
3417 static int idecd_revalidate_disk(struct gendisk *disk)
3419 ide_drive_t *drive = disk->private_data;
3420 struct request_sense sense;
3421 cdrom_read_toc(drive, &sense);
3422 return 0;
3425 static struct block_device_operations idecd_ops = {
3426 .owner = THIS_MODULE,
3427 .open = idecd_open,
3428 .release = idecd_release,
3429 .ioctl = idecd_ioctl,
3430 .media_changed = idecd_media_changed,
3431 .revalidate_disk= idecd_revalidate_disk
3434 /* options */
3435 char *ignore = NULL;
3437 MODULE_PARM(ignore, "s");
3438 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3440 static int ide_cdrom_attach (ide_drive_t *drive)
3442 struct cdrom_info *info;
3443 struct gendisk *g = drive->disk;
3444 struct request_sense sense;
3446 if (!strstr("ide-cdrom", drive->driver_req))
3447 goto failed;
3448 if (!drive->present)
3449 goto failed;
3450 if (drive->media != ide_cdrom && drive->media != ide_optical)
3451 goto failed;
3452 /* skip drives that we were told to ignore */
3453 if (ignore != NULL) {
3454 if (strstr(ignore, drive->name)) {
3455 printk("ide-cd: ignoring drive %s\n", drive->name);
3456 goto failed;
3459 if (drive->scsi) {
3460 printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
3461 goto failed;
3463 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
3464 if (info == NULL) {
3465 printk("%s: Can't allocate a cdrom structure\n", drive->name);
3466 goto failed;
3468 if (ide_register_subdriver(drive, &ide_cdrom_driver)) {
3469 printk("%s: Failed to register the driver with ide.c\n",
3470 drive->name);
3471 kfree(info);
3472 goto failed;
3474 memset(info, 0, sizeof (struct cdrom_info));
3475 drive->driver_data = info;
3476 DRIVER(drive)->busy++;
3477 g->minors = 1;
3478 snprintf(g->devfs_name, sizeof(g->devfs_name),
3479 "%s/cd", drive->devfs_name);
3480 g->driverfs_dev = &drive->gendev;
3481 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
3482 if (ide_cdrom_setup(drive)) {
3483 struct cdrom_device_info *devinfo = &info->devinfo;
3484 DRIVER(drive)->busy--;
3485 ide_unregister_subdriver(drive);
3486 if (info->buffer != NULL)
3487 kfree(info->buffer);
3488 if (info->toc != NULL)
3489 kfree(info->toc);
3490 if (info->changer_info != NULL)
3491 kfree(info->changer_info);
3492 if (devinfo->handle == drive && unregister_cdrom(devinfo))
3493 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3494 kfree(info);
3495 drive->driver_data = NULL;
3496 goto failed;
3498 DRIVER(drive)->busy--;
3500 cdrom_read_toc(drive, &sense);
3501 g->fops = &idecd_ops;
3502 g->flags |= GENHD_FL_REMOVABLE;
3503 add_disk(g);
3504 return 0;
3505 failed:
3506 return 1;
3509 static void __exit ide_cdrom_exit(void)
3511 ide_unregister_driver(&ide_cdrom_driver);
3514 static int ide_cdrom_init(void)
3516 ide_register_driver(&ide_cdrom_driver);
3517 return 0;
3520 module_init(ide_cdrom_init);
3521 module_exit(ide_cdrom_exit);
3522 MODULE_LICENSE("GPL");