[PATCH] v4l: keep tvaudio driver away from saa7146
[linux-2.6/history.git] / drivers / ide / ide-cd.c
blob64c452e039d9f482493a443d94aa7099923bad37
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;
868 ide_hwif_t *hwif = drive->hwif;
870 /* Wait for the controller to be idle. */
871 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
872 return startstop;
874 if (info->dma)
875 info->dma = !hwif->dma_setup(drive);
877 /* Set up the controller registers. */
878 /* FIXME: for Virtual DMA we must check harder */
879 HWIF(drive)->OUTB(info->dma, IDE_FEATURE_REG);
880 HWIF(drive)->OUTB(0, IDE_IREASON_REG);
881 HWIF(drive)->OUTB(0, IDE_SECTOR_REG);
883 HWIF(drive)->OUTB(xferlen & 0xff, IDE_BCOUNTL_REG);
884 HWIF(drive)->OUTB(xferlen >> 8 , IDE_BCOUNTH_REG);
885 if (IDE_CONTROL_REG)
886 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
888 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
889 /* packet command */
890 ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
891 return ide_started;
892 } else {
893 /* packet command */
894 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
895 return (*handler) (drive);
899 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
900 The device registers must have already been prepared
901 by cdrom_start_packet_command.
902 HANDLER is the interrupt handler to call when the command completes
903 or there's data ready. */
905 * changed 5 parameters to 3 for dvd-ram
906 * struct packet_command *pc; now packet_command_t *pc;
908 #define ATAPI_MIN_CDB_BYTES 12
909 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
910 struct request *rq,
911 ide_handler_t *handler)
913 ide_hwif_t *hwif = drive->hwif;
914 int cmd_len;
915 struct cdrom_info *info = drive->driver_data;
916 ide_startstop_t startstop;
918 if (CDROM_CONFIG_FLAGS(drive)->drq_interrupt) {
919 /* Here we should have been called after receiving an interrupt
920 from the device. DRQ should how be set. */
922 /* Check for errors. */
923 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
924 return ide_stopped;
925 } else {
926 /* Otherwise, we must wait for DRQ to get set. */
927 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
928 BUSY_STAT, WAIT_READY))
929 return startstop;
932 /* Arm the interrupt handler. */
933 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
935 /* ATAPI commands get padded out to 12 bytes minimum */
936 cmd_len = COMMAND_SIZE(rq->cmd[0]);
937 if (cmd_len < ATAPI_MIN_CDB_BYTES)
938 cmd_len = ATAPI_MIN_CDB_BYTES;
940 /* Send the command to the device. */
941 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
943 /* Start the DMA if need be */
944 if (info->dma)
945 hwif->dma_start(drive);
947 return ide_started;
950 /****************************************************************************
951 * Block read functions.
955 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
956 * buffer. Once the first sector is added, any subsequent sectors are
957 * assumed to be continuous (until the buffer is cleared). For the first
958 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
959 * the buffer is cleared.)
961 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
962 int sectors_to_transfer)
964 struct cdrom_info *info = drive->driver_data;
966 /* Number of sectors to read into the buffer. */
967 int sectors_to_buffer = min_t(int, sectors_to_transfer,
968 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
969 info->nsectors_buffered);
971 char *dest;
973 /* If we couldn't get a buffer, don't try to buffer anything... */
974 if (info->buffer == NULL)
975 sectors_to_buffer = 0;
977 /* If this is the first sector in the buffer, remember its number. */
978 if (info->nsectors_buffered == 0)
979 info->sector_buffered = sector;
981 /* Read the data into the buffer. */
982 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
983 while (sectors_to_buffer > 0) {
984 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
985 --sectors_to_buffer;
986 --sectors_to_transfer;
987 ++info->nsectors_buffered;
988 dest += SECTOR_SIZE;
991 /* Throw away any remaining data. */
992 while (sectors_to_transfer > 0) {
993 static char dum[SECTOR_SIZE];
994 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
995 --sectors_to_transfer;
1000 * Check the contents of the interrupt reason register from the cdrom
1001 * and attempt to recover if there are problems. Returns 0 if everything's
1002 * ok; nonzero if the request has been terminated.
1004 static inline
1005 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
1007 if (ireason == 2)
1008 return 0;
1009 else if (ireason == 0) {
1010 /* Whoops... The drive is expecting to receive data from us! */
1011 printk("%s: read_intr: Drive wants to transfer data the "
1012 "wrong way!\n", drive->name);
1014 /* Throw some data at the drive so it doesn't hang
1015 and quit this request. */
1016 while (len > 0) {
1017 int dum = 0;
1018 HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof (dum));
1019 len -= sizeof (dum);
1021 } else if (ireason == 1) {
1022 /* Some drives (ASUS) seem to tell us that status
1023 * info is available. just get it and ignore.
1025 (void) HWIF(drive)->INB(IDE_STATUS_REG);
1026 return 0;
1027 } else {
1028 /* Drive wants a command packet, or invalid ireason... */
1029 printk("%s: read_intr: bad interrupt reason %x\n", drive->name,
1030 ireason);
1033 cdrom_end_request(drive, 0);
1034 return -1;
1038 * Interrupt routine. Called when a read request has completed.
1040 static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
1042 int stat;
1043 int ireason, len, sectors_to_transfer, nskip;
1044 struct cdrom_info *info = drive->driver_data;
1045 u8 lowcyl = 0, highcyl = 0;
1046 int dma = info->dma, dma_error = 0;
1048 struct request *rq = HWGROUP(drive)->rq;
1051 * handle dma case
1053 if (dma) {
1054 info->dma = 0;
1055 if ((dma_error = HWIF(drive)->ide_dma_end(drive)))
1056 __ide_dma_off(drive);
1059 if (cdrom_decode_status(drive, 0, &stat))
1060 return ide_stopped;
1062 if (dma) {
1063 if (!dma_error) {
1064 ide_end_request(drive, 1, rq->nr_sectors);
1065 return ide_stopped;
1066 } else
1067 return DRIVER(drive)->error(drive, "dma error", stat);
1070 /* Read the interrupt reason and the transfer length. */
1071 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1072 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1073 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1075 len = lowcyl + (256 * highcyl);
1077 /* If DRQ is clear, the command has completed. */
1078 if ((stat & DRQ_STAT) == 0) {
1079 /* If we're not done filling the current buffer, complain.
1080 Otherwise, complete the command normally. */
1081 if (rq->current_nr_sectors > 0) {
1082 printk ("%s: cdrom_read_intr: data underrun (%d blocks)\n",
1083 drive->name, rq->current_nr_sectors);
1084 rq->flags |= REQ_FAILED;
1085 cdrom_end_request(drive, 0);
1086 } else
1087 cdrom_end_request(drive, 1);
1088 return ide_stopped;
1091 /* Check that the drive is expecting to do the same thing we are. */
1092 if (cdrom_read_check_ireason (drive, len, ireason))
1093 return ide_stopped;
1095 /* Assume that the drive will always provide data in multiples
1096 of at least SECTOR_SIZE, as it gets hairy to keep track
1097 of the transfers otherwise. */
1098 if ((len % SECTOR_SIZE) != 0) {
1099 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
1100 drive->name, len);
1101 if (CDROM_CONFIG_FLAGS(drive)->limit_nframes)
1102 printk (" This drive is not supported by this version of the driver\n");
1103 else {
1104 printk (" Trying to limit transfer sizes\n");
1105 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
1107 cdrom_end_request(drive, 0);
1108 return ide_stopped;
1111 /* The number of sectors we need to read from the drive. */
1112 sectors_to_transfer = len / SECTOR_SIZE;
1114 /* First, figure out if we need to bit-bucket
1115 any of the leading sectors. */
1116 nskip = min_t(int, rq->current_nr_sectors - bio_cur_sectors(rq->bio), sectors_to_transfer);
1118 while (nskip > 0) {
1119 /* We need to throw away a sector. */
1120 static char dum[SECTOR_SIZE];
1121 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
1123 --rq->current_nr_sectors;
1124 --nskip;
1125 --sectors_to_transfer;
1128 /* Now loop while we still have data to read from the drive. */
1129 while (sectors_to_transfer > 0) {
1130 int this_transfer;
1132 /* If we've filled the present buffer but there's another
1133 chained buffer after it, move on. */
1134 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1135 cdrom_end_request(drive, 1);
1137 /* If the buffers are full, cache the rest of the data in our
1138 internal buffer. */
1139 if (rq->current_nr_sectors == 0) {
1140 cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
1141 sectors_to_transfer = 0;
1142 } else {
1143 /* Transfer data to the buffers.
1144 Figure out how many sectors we can transfer
1145 to the current buffer. */
1146 this_transfer = min_t(int, sectors_to_transfer,
1147 rq->current_nr_sectors);
1149 /* Read this_transfer sectors
1150 into the current buffer. */
1151 while (this_transfer > 0) {
1152 HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
1153 rq->buffer += SECTOR_SIZE;
1154 --rq->nr_sectors;
1155 --rq->current_nr_sectors;
1156 ++rq->sector;
1157 --this_transfer;
1158 --sectors_to_transfer;
1163 /* Done moving data! Wait for another interrupt. */
1164 ide_set_handler(drive, &cdrom_read_intr, ATAPI_WAIT_PC, NULL);
1165 return ide_started;
1169 * Try to satisfy some of the current read request from our cached data.
1170 * Returns nonzero if the request has been completed, zero otherwise.
1172 static int cdrom_read_from_buffer (ide_drive_t *drive)
1174 struct cdrom_info *info = drive->driver_data;
1175 struct request *rq = HWGROUP(drive)->rq;
1176 unsigned short sectors_per_frame;
1178 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1180 /* Can't do anything if there's no buffer. */
1181 if (info->buffer == NULL) return 0;
1183 /* Loop while this request needs data and the next block is present
1184 in our cache. */
1185 while (rq->nr_sectors > 0 &&
1186 rq->sector >= info->sector_buffered &&
1187 rq->sector < info->sector_buffered + info->nsectors_buffered) {
1188 if (rq->current_nr_sectors == 0)
1189 cdrom_end_request(drive, 1);
1191 memcpy (rq->buffer,
1192 info->buffer +
1193 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1194 SECTOR_SIZE);
1195 rq->buffer += SECTOR_SIZE;
1196 --rq->current_nr_sectors;
1197 --rq->nr_sectors;
1198 ++rq->sector;
1201 /* If we've satisfied the current request,
1202 terminate it successfully. */
1203 if (rq->nr_sectors == 0) {
1204 cdrom_end_request(drive, 1);
1205 return -1;
1208 /* Move on to the next buffer if needed. */
1209 if (rq->current_nr_sectors == 0)
1210 cdrom_end_request(drive, 1);
1212 /* If this condition does not hold, then the kluge i use to
1213 represent the number of sectors to skip at the start of a transfer
1214 will fail. I think that this will never happen, but let's be
1215 paranoid and check. */
1216 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
1217 (rq->sector & (sectors_per_frame - 1))) {
1218 printk("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1219 drive->name, (long)rq->sector);
1220 cdrom_end_request(drive, 0);
1221 return -1;
1224 return 0;
1228 * Routine to send a read packet command to the drive.
1229 * This is usually called directly from cdrom_start_read.
1230 * However, for drq_interrupt devices, it is called from an interrupt
1231 * when the drive is ready to accept the command.
1233 static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1235 struct request *rq = HWGROUP(drive)->rq;
1236 unsigned short sectors_per_frame;
1237 int nskip;
1239 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1241 /* If the requested sector doesn't start on a cdrom block boundary,
1242 we must adjust the start of the transfer so that it does,
1243 and remember to skip the first few sectors.
1244 If the CURRENT_NR_SECTORS field is larger than the size
1245 of the buffer, it will mean that we're to skip a number
1246 of sectors equal to the amount by which CURRENT_NR_SECTORS
1247 is larger than the buffer size. */
1248 nskip = rq->sector & (sectors_per_frame - 1);
1249 if (nskip > 0) {
1250 /* Sanity check... */
1251 if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
1252 (rq->sector & (sectors_per_frame - 1))) {
1253 printk ("%s: cdrom_start_read_continuation: buffer botch (%u)\n",
1254 drive->name, rq->current_nr_sectors);
1255 cdrom_end_request(drive, 0);
1256 return ide_stopped;
1258 rq->current_nr_sectors += nskip;
1261 /* Set up the command */
1262 rq->timeout = ATAPI_WAIT_PC;
1264 /* Send the command to the drive and return. */
1265 return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
1269 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1270 #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
1271 #define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
1273 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1275 struct cdrom_info *info = drive->driver_data;
1276 int stat;
1277 static int retry = 10;
1279 if (cdrom_decode_status(drive, 0, &stat))
1280 return ide_stopped;
1281 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1283 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
1284 if (--retry == 0) {
1286 * this condition is far too common, to bother
1287 * users about it
1289 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
1290 drive->dsc_overlap = 0;
1293 return ide_stopped;
1296 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1298 struct request *rq = HWGROUP(drive)->rq;
1299 sector_t frame = rq->sector;
1301 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
1303 memset(rq->cmd, 0, sizeof(rq->cmd));
1304 rq->cmd[0] = GPCMD_SEEK;
1305 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
1307 rq->timeout = ATAPI_WAIT_PC;
1308 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
1311 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1313 struct cdrom_info *info = drive->driver_data;
1315 info->dma = 0;
1316 info->cmd = 0;
1317 info->start_seek = jiffies;
1318 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
1321 /* Fix up a possibly partially-processed request so that we can
1322 start it over entirely, or even put it back on the request queue. */
1323 static void restore_request (struct request *rq)
1325 if (rq->buffer != bio_data(rq->bio)) {
1326 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
1328 rq->buffer = bio_data(rq->bio);
1329 rq->nr_sectors += n;
1330 rq->sector -= n;
1332 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
1333 rq->hard_nr_sectors = rq->nr_sectors;
1334 rq->hard_sector = rq->sector;
1335 rq->q->prep_rq_fn(rq->q, rq);
1339 * Start a read request from the CD-ROM.
1341 static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1343 struct cdrom_info *info = drive->driver_data;
1344 struct request *rq = HWGROUP(drive)->rq;
1345 unsigned short sectors_per_frame;
1347 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1349 /* We may be retrying this request after an error. Fix up
1350 any weirdness which might be present in the request packet. */
1351 restore_request(rq);
1353 /* Satisfy whatever we can of this request from our cached sector. */
1354 if (cdrom_read_from_buffer(drive))
1355 return ide_stopped;
1357 blk_attempt_remerge(drive->queue, rq);
1359 /* Clear the local sector buffer. */
1360 info->nsectors_buffered = 0;
1362 /* use dma, if possible. */
1363 info->dma = drive->using_dma;
1364 if ((rq->sector & (sectors_per_frame - 1)) ||
1365 (rq->nr_sectors & (sectors_per_frame - 1)))
1366 info->dma = 0;
1368 info->cmd = READ;
1370 /* Start sending the read request to the drive. */
1371 return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1374 /****************************************************************************
1375 * Execute all other packet commands.
1378 /* Interrupt routine for packet command completion. */
1379 static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1381 int ireason, len, thislen;
1382 struct request *rq = HWGROUP(drive)->rq;
1383 u8 lowcyl = 0, highcyl = 0;
1384 int stat;
1386 /* Check for errors. */
1387 if (cdrom_decode_status(drive, 0, &stat))
1388 return ide_stopped;
1390 /* Read the interrupt reason and the transfer length. */
1391 ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1392 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1393 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1395 len = lowcyl + (256 * highcyl);
1397 /* If DRQ is clear, the command has completed.
1398 Complain if we still have data left to transfer. */
1399 if ((stat & DRQ_STAT) == 0) {
1400 /* Some of the trailing request sense fields are optional, and
1401 some drives don't send them. Sigh. */
1402 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
1403 rq->data_len > 0 &&
1404 rq->data_len <= 5) {
1405 while (rq->data_len > 0) {
1406 *(unsigned char *)rq->data++ = 0;
1407 --rq->data_len;
1411 if (rq->data_len == 0)
1412 cdrom_end_request(drive, 1);
1413 else {
1414 /* Comment this out, because this always happens
1415 right after a reset occurs, and it is annoying to
1416 always print expected stuff. */
1418 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1419 drive->name, pc->buflen);
1421 rq->flags |= REQ_FAILED;
1422 cdrom_end_request(drive, 0);
1424 return ide_stopped;
1427 /* Figure out how much data to transfer. */
1428 thislen = rq->data_len;
1429 if (thislen > len) thislen = len;
1431 /* The drive wants to be written to. */
1432 if ((ireason & 3) == 0) {
1433 if (!rq->data) {
1434 blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1435 goto confused;
1437 /* Transfer the data. */
1438 HWIF(drive)->atapi_output_bytes(drive, rq->data, thislen);
1440 /* If we haven't moved enough data to satisfy the drive,
1441 add some padding. */
1442 while (len > thislen) {
1443 int dum = 0;
1444 HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof(dum));
1445 len -= sizeof(dum);
1448 /* Keep count of how much data we've moved. */
1449 rq->data += thislen;
1450 rq->data_len -= thislen;
1453 /* Same drill for reading. */
1454 else if ((ireason & 3) == 2) {
1455 if (!rq->data) {
1456 blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1457 goto confused;
1459 /* Transfer the data. */
1460 HWIF(drive)->atapi_input_bytes(drive, rq->data, thislen);
1462 /* If we haven't moved enough data to satisfy the drive,
1463 add some padding. */
1464 while (len > thislen) {
1465 int dum = 0;
1466 HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1467 len -= sizeof(dum);
1470 /* Keep count of how much data we've moved. */
1471 rq->data += thislen;
1472 rq->data_len -= thislen;
1474 if (rq->flags & REQ_SENSE)
1475 rq->sense_len += thislen;
1476 } else {
1477 confused:
1478 printk ("%s: cdrom_pc_intr: The drive "
1479 "appears confused (ireason = 0x%02x)\n",
1480 drive->name, ireason);
1481 rq->flags |= REQ_FAILED;
1484 /* Now we wait for another interrupt. */
1485 ide_set_handler(drive, &cdrom_pc_intr, ATAPI_WAIT_PC, cdrom_timer_expiry);
1486 return ide_started;
1489 static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1491 struct request *rq = HWGROUP(drive)->rq;
1493 if (!rq->timeout)
1494 rq->timeout = ATAPI_WAIT_PC;
1496 /* Send the command to the drive and return. */
1497 return cdrom_transfer_packet_command(drive, rq, &cdrom_pc_intr);
1501 static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1503 int len;
1504 struct request *rq = HWGROUP(drive)->rq;
1505 struct cdrom_info *info = drive->driver_data;
1507 info->dma = 0;
1508 info->cmd = 0;
1509 rq->flags &= ~REQ_FAILED;
1510 len = rq->data_len;
1512 /* Start sending the command to the drive. */
1513 return cdrom_start_packet_command(drive, len, cdrom_do_pc_continuation);
1517 /* Sleep for TIME jiffies.
1518 Not to be called from an interrupt handler. */
1519 static
1520 void cdrom_sleep (int time)
1522 int sleep = time;
1524 do {
1525 set_current_state(TASK_INTERRUPTIBLE);
1526 sleep = schedule_timeout(sleep);
1527 } while (sleep);
1530 static
1531 int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
1533 struct request_sense sense;
1534 int retries = 10;
1535 unsigned int flags = rq->flags;
1537 if (rq->sense == NULL)
1538 rq->sense = &sense;
1540 /* Start of retry loop. */
1541 do {
1542 int error;
1543 unsigned long time = jiffies;
1544 rq->flags = flags;
1546 error = ide_do_drive_cmd(drive, rq, ide_wait);
1547 time = jiffies - time;
1549 /* FIXME: we should probably abort/retry or something
1550 * in case of failure */
1551 if (rq->flags & REQ_FAILED) {
1552 /* The request failed. Retry if it was due to a unit
1553 attention status
1554 (usually means media was changed). */
1555 struct request_sense *reqbuf = rq->sense;
1557 if (reqbuf->sense_key == UNIT_ATTENTION)
1558 cdrom_saw_media_change(drive);
1559 else if (reqbuf->sense_key == NOT_READY &&
1560 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1561 /* The drive is in the process of loading
1562 a disk. Retry, but wait a little to give
1563 the drive time to complete the load. */
1564 cdrom_sleep(2 * HZ);
1565 } else {
1566 /* Otherwise, don't retry. */
1567 retries = 0;
1569 --retries;
1572 /* End of retry loop. */
1573 } while ((rq->flags & REQ_FAILED) && retries >= 0);
1575 /* Return an error if the command failed. */
1576 return (rq->flags & REQ_FAILED) ? -EIO : 0;
1580 * Write handling
1582 static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1584 /* Two notes about IDE interrupt reason here - 0 means that
1585 * the drive wants to receive data from us, 2 means that
1586 * the drive is expecting to transfer data to us.
1588 if (ireason == 0)
1589 return 0;
1590 else if (ireason == 2) {
1591 /* Whoops... The drive wants to send data. */
1592 printk("%s: write_intr: wrong transfer direction!\n",
1593 drive->name);
1595 while (len > 0) {
1596 int dum = 0;
1597 HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1598 len -= sizeof(dum);
1600 } else {
1601 /* Drive wants a command packet, or invalid ireason... */
1602 printk("%s: write_intr: bad interrupt reason %x\n",
1603 drive->name, ireason);
1606 cdrom_end_request(drive, 0);
1607 return 1;
1610 static void post_transform_command(struct request *req)
1612 u8 *c = req->cmd;
1613 char *ibuf;
1615 if (!blk_pc_request(req))
1616 return;
1618 if (req->bio)
1619 ibuf = bio_data(req->bio);
1620 else
1621 ibuf = req->data;
1623 if (!ibuf)
1624 return;
1627 * set ansi-revision and response data as atapi
1629 if (c[0] == GPCMD_INQUIRY) {
1630 ibuf[2] |= 2;
1631 ibuf[3] = (ibuf[3] & 0xf0) | 2;
1635 typedef void (xfer_func_t)(ide_drive_t *, void *, u32);
1638 * best way to deal with dma that is not sector aligned right now... note
1639 * that in this path we are not using ->data or ->buffer at all. this irs
1640 * can replace cdrom_pc_intr, cdrom_read_intr, and cdrom_write_intr in the
1641 * future.
1643 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1645 struct cdrom_info *info = drive->driver_data;
1646 struct request *rq = HWGROUP(drive)->rq;
1647 int dma_error, dma, stat, ireason, len, thislen;
1648 u8 lowcyl, highcyl;
1649 xfer_func_t *xferfunc;
1650 unsigned long flags;
1652 /* Check for errors. */
1653 dma_error = 0;
1654 dma = info->dma;
1655 if (dma) {
1656 info->dma = 0;
1657 dma_error = HWIF(drive)->ide_dma_end(drive);
1660 if (cdrom_decode_status(drive, 0, &stat))
1661 return ide_stopped;
1664 * using dma, transfer is complete now
1666 if (dma) {
1667 if (dma_error) {
1668 printk("ide-cd: dma error\n");
1669 __ide_dma_off(drive);
1670 return DRIVER(drive)->error(drive, "dma error", stat);
1673 end_that_request_chunk(rq, 1, rq->data_len);
1674 rq->data_len = 0;
1675 goto end_request;
1679 * ok we fall to pio :/
1681 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1682 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1683 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1685 len = lowcyl + (256 * highcyl);
1686 thislen = rq->data_len;
1687 if (thislen > len)
1688 thislen = len;
1691 * If DRQ is clear, the command has completed.
1693 if ((stat & DRQ_STAT) == 0)
1694 goto end_request;
1697 * check which way to transfer data
1699 if (rq_data_dir(rq) == WRITE) {
1701 * write to drive
1703 if (cdrom_write_check_ireason(drive, len, ireason))
1704 return ide_stopped;
1706 xferfunc = HWIF(drive)->atapi_output_bytes;
1707 } else {
1709 * read from drive
1711 if (cdrom_read_check_ireason(drive, len, ireason))
1712 return ide_stopped;
1714 xferfunc = HWIF(drive)->atapi_input_bytes;
1718 * transfer data
1720 while (thislen > 0) {
1721 int blen = blen = rq->data_len;
1722 char *ptr = rq->data;
1725 * bio backed?
1727 if (rq->bio) {
1728 ptr = bio_data(rq->bio);
1729 blen = bio_iovec(rq->bio)->bv_len;
1732 if (!ptr) {
1733 printk("%s: confused, missing data\n", drive->name);
1734 break;
1737 if (blen > thislen)
1738 blen = thislen;
1740 xferfunc(drive, ptr, blen);
1742 thislen -= blen;
1743 len -= blen;
1744 rq->data_len -= blen;
1746 if (rq->bio)
1747 end_that_request_chunk(rq, 1, blen);
1748 else
1749 rq->data += blen;
1753 * pad, if necessary
1755 if (len > 0) {
1756 while (len > 0) {
1757 int pad = 0;
1759 xferfunc(drive, &pad, sizeof(pad));
1760 len -= sizeof(pad);
1764 if (HWGROUP(drive)->handler != NULL)
1765 BUG();
1767 ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, NULL);
1768 return ide_started;
1770 end_request:
1771 if (!rq->data_len)
1772 post_transform_command(rq);
1774 spin_lock_irqsave(&ide_lock, flags);
1775 blkdev_dequeue_request(rq);
1776 end_that_request_last(rq);
1777 HWGROUP(drive)->rq = NULL;
1778 spin_unlock_irqrestore(&ide_lock, flags);
1779 return ide_stopped;
1782 static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1784 int stat, ireason, len, sectors_to_transfer, uptodate;
1785 struct cdrom_info *info = drive->driver_data;
1786 int dma_error = 0, dma = info->dma;
1787 u8 lowcyl = 0, highcyl = 0;
1789 struct request *rq = HWGROUP(drive)->rq;
1791 /* Check for errors. */
1792 if (dma) {
1793 info->dma = 0;
1794 if ((dma_error = HWIF(drive)->ide_dma_end(drive))) {
1795 printk("ide-cd: write dma error\n");
1796 __ide_dma_off(drive);
1800 if (cdrom_decode_status(drive, 0, &stat))
1801 return ide_stopped;
1804 * using dma, transfer is complete now
1806 if (dma) {
1807 if (dma_error)
1808 return DRIVER(drive)->error(drive, "dma error", stat);
1810 ide_end_request(drive, 1, rq->nr_sectors);
1811 return ide_stopped;
1814 /* Read the interrupt reason and the transfer length. */
1815 ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1816 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1817 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1819 len = lowcyl + (256 * highcyl);
1821 /* If DRQ is clear, the command has completed. */
1822 if ((stat & DRQ_STAT) == 0) {
1823 /* If we're not done writing, complain.
1824 * Otherwise, complete the command normally.
1826 uptodate = 1;
1827 if (rq->current_nr_sectors > 0) {
1828 printk("%s: write_intr: data underrun (%d blocks)\n",
1829 drive->name, rq->current_nr_sectors);
1830 uptodate = 0;
1832 cdrom_end_request(drive, uptodate);
1833 return ide_stopped;
1836 /* Check that the drive is expecting to do the same thing we are. */
1837 if (cdrom_write_check_ireason(drive, len, ireason))
1838 return ide_stopped;
1840 sectors_to_transfer = len / SECTOR_SIZE;
1843 * now loop and write out the data
1845 while (sectors_to_transfer > 0) {
1846 int this_transfer;
1848 if (!rq->current_nr_sectors) {
1849 printk("ide-cd: write_intr: oops\n");
1850 break;
1854 * Figure out how many sectors we can transfer
1856 this_transfer = min_t(int, sectors_to_transfer, rq->current_nr_sectors);
1858 while (this_transfer > 0) {
1859 HWIF(drive)->atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
1860 rq->buffer += SECTOR_SIZE;
1861 --rq->nr_sectors;
1862 --rq->current_nr_sectors;
1863 ++rq->sector;
1864 --this_transfer;
1865 --sectors_to_transfer;
1869 * current buffer complete, move on
1871 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1872 cdrom_end_request(drive, 1);
1875 /* re-arm handler */
1876 ide_set_handler(drive, &cdrom_write_intr, ATAPI_WAIT_PC, NULL);
1877 return ide_started;
1880 static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1882 struct request *rq = HWGROUP(drive)->rq;
1884 #if 0 /* the immediate bit */
1885 rq->cmd[1] = 1 << 3;
1886 #endif
1887 rq->timeout = ATAPI_WAIT_PC;
1889 return cdrom_transfer_packet_command(drive, rq, cdrom_write_intr);
1892 static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
1894 struct cdrom_info *info = drive->driver_data;
1895 struct gendisk *g = drive->disk;
1896 unsigned short sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1899 * writes *must* be hardware frame aligned
1901 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1902 (rq->sector & (sectors_per_frame - 1))) {
1903 cdrom_end_request(drive, 0);
1904 return ide_stopped;
1908 * disk has become write protected
1910 if (g->policy) {
1911 cdrom_end_request(drive, 0);
1912 return ide_stopped;
1916 * for dvd-ram and such media, it's a really big deal to get
1917 * big writes all the time. so scour the queue and attempt to
1918 * remerge requests, often the plugging will not have had time
1919 * to do this properly
1921 blk_attempt_remerge(drive->queue, rq);
1923 info->nsectors_buffered = 0;
1925 /* use dma, if possible. we don't need to check more, since we
1926 * know that the transfer is always (at least!) frame aligned */
1927 info->dma = drive->using_dma ? 1 : 0;
1928 info->cmd = WRITE;
1930 info->devinfo.media_written = 1;
1932 /* Start sending the write request to the drive. */
1933 return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1936 static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1938 struct request *rq = HWGROUP(drive)->rq;
1940 if (!rq->timeout)
1941 rq->timeout = ATAPI_WAIT_PC;
1943 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1946 static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1948 struct cdrom_info *info = drive->driver_data;
1950 rq->flags |= REQ_QUIET;
1952 info->dma = 0;
1953 info->cmd = 0;
1956 * sg request
1958 if (rq->bio) {
1959 int mask = drive->queue->dma_alignment;
1960 unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1962 info->cmd = rq_data_dir(rq);
1963 info->dma = drive->using_dma;
1966 * check if dma is safe
1968 if ((rq->data_len & mask) || (addr & mask))
1969 info->dma = 0;
1972 /* Start sending the command to the drive. */
1973 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1976 /****************************************************************************
1977 * cdrom driver request routine.
1979 static ide_startstop_t
1980 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1982 ide_startstop_t action;
1983 struct cdrom_info *info = drive->driver_data;
1985 if (blk_fs_request(rq)) {
1986 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1987 unsigned long elapsed = jiffies - info->start_seek;
1988 int stat = HWIF(drive)->INB(IDE_STATUS_REG);
1990 if ((stat & SEEK_STAT) != SEEK_STAT) {
1991 if (elapsed < IDECD_SEEK_TIMEOUT) {
1992 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1993 return ide_stopped;
1995 printk ("%s: DSC timeout\n", drive->name);
1997 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1999 if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
2000 action = cdrom_start_seek(drive, block);
2001 } else {
2002 if (rq_data_dir(rq) == READ)
2003 action = cdrom_start_read(drive, block);
2004 else
2005 action = cdrom_start_write(drive, rq);
2007 info->last_block = block;
2008 return action;
2009 } else if (rq->flags & (REQ_PC | REQ_SENSE)) {
2010 return cdrom_do_packet_command(drive);
2011 } else if (rq->flags & REQ_BLOCK_PC) {
2012 return cdrom_do_block_pc(drive, rq);
2013 } else if (rq->flags & REQ_SPECIAL) {
2015 * right now this can only be a reset...
2017 cdrom_end_request(drive, 1);
2018 return ide_stopped;
2021 blk_dump_rq_flags(rq, "ide-cd bad flags");
2022 cdrom_end_request(drive, 0);
2023 return ide_stopped;
2028 /****************************************************************************
2029 * Ioctl handling.
2031 * Routines which queue packet commands take as a final argument a pointer
2032 * to a request_sense struct. If execution of the command results
2033 * in an error with a CHECK CONDITION status, this structure will be filled
2034 * with the results of the subsequent request sense command. The pointer
2035 * can also be NULL, in which case no sense information is returned.
2038 #if ! STANDARD_ATAPI
2039 static inline
2040 int bin2bcd (int x)
2042 return (x%10) | ((x/10) << 4);
2046 static inline
2047 int bcd2bin (int x)
2049 return (x >> 4) * 10 + (x & 0x0f);
2052 static
2053 void msf_from_bcd (struct atapi_msf *msf)
2055 msf->minute = bcd2bin (msf->minute);
2056 msf->second = bcd2bin (msf->second);
2057 msf->frame = bcd2bin (msf->frame);
2060 #endif /* not STANDARD_ATAPI */
2063 static inline
2064 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
2066 lba += CD_MSF_OFFSET;
2067 lba &= 0xffffff; /* negative lbas use only 24 bits */
2068 *m = lba / (CD_SECS * CD_FRAMES);
2069 lba %= (CD_SECS * CD_FRAMES);
2070 *s = lba / CD_FRAMES;
2071 *f = lba % CD_FRAMES;
2075 static inline
2076 int msf_to_lba (byte m, byte s, byte f)
2078 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
2081 static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
2083 struct request req;
2084 struct cdrom_info *info = drive->driver_data;
2085 struct cdrom_device_info *cdi = &info->devinfo;
2087 cdrom_prepare_request(&req);
2089 req.sense = sense;
2090 req.cmd[0] = GPCMD_TEST_UNIT_READY;
2091 req.flags |= REQ_QUIET;
2093 #if ! STANDARD_ATAPI
2094 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
2095 switch CDs instead of supporting the LOAD_UNLOAD opcode */
2097 req.cmd[7] = cdi->sanyo_slot % 3;
2098 #endif /* not STANDARD_ATAPI */
2100 return cdrom_queue_packet_command(drive, &req);
2104 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
2105 static int
2106 cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
2108 struct request_sense my_sense;
2109 struct request req;
2110 int stat;
2112 if (sense == NULL)
2113 sense = &my_sense;
2115 /* If the drive cannot lock the door, just pretend. */
2116 if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
2117 stat = 0;
2118 } else {
2119 cdrom_prepare_request(&req);
2120 req.sense = sense;
2121 req.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2122 req.cmd[4] = lockflag ? 1 : 0;
2123 stat = cdrom_queue_packet_command(drive, &req);
2126 /* If we got an illegal field error, the drive
2127 probably cannot lock the door. */
2128 if (stat != 0 &&
2129 sense->sense_key == ILLEGAL_REQUEST &&
2130 (sense->asc == 0x24 || sense->asc == 0x20)) {
2131 printk ("%s: door locking not supported\n",
2132 drive->name);
2133 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2134 stat = 0;
2137 /* no medium, that's alright. */
2138 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
2139 stat = 0;
2141 if (stat == 0)
2142 CDROM_STATE_FLAGS(drive)->door_locked = lockflag;
2144 return stat;
2148 /* Eject the disk if EJECTFLAG is 0.
2149 If EJECTFLAG is 1, try to reload the disk. */
2150 static int cdrom_eject(ide_drive_t *drive, int ejectflag,
2151 struct request_sense *sense)
2153 struct request req;
2154 char loej = 0x02;
2156 if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
2157 return -EDRIVE_CANT_DO_THIS;
2159 /* reload fails on some drives, if the tray is locked */
2160 if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
2161 return 0;
2163 cdrom_prepare_request(&req);
2165 /* only tell drive to close tray if open, if it can do that */
2166 if (ejectflag && !CDROM_CONFIG_FLAGS(drive)->close_tray)
2167 loej = 0;
2169 req.sense = sense;
2170 req.cmd[0] = GPCMD_START_STOP_UNIT;
2171 req.cmd[4] = loej | (ejectflag != 0);
2172 return cdrom_queue_packet_command(drive, &req);
2175 static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
2176 unsigned long *sectors_per_frame,
2177 struct request_sense *sense)
2179 struct {
2180 __u32 lba;
2181 __u32 blocklen;
2182 } capbuf;
2184 int stat;
2185 struct request req;
2187 cdrom_prepare_request(&req);
2189 req.sense = sense;
2190 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
2191 req.data = (char *)&capbuf;
2192 req.data_len = sizeof(capbuf);
2194 stat = cdrom_queue_packet_command(drive, &req);
2195 if (stat == 0) {
2196 *capacity = 1 + be32_to_cpu(capbuf.lba);
2197 *sectors_per_frame =
2198 be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
2201 return stat;
2204 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
2205 int format, char *buf, int buflen,
2206 struct request_sense *sense)
2208 struct request req;
2210 cdrom_prepare_request(&req);
2212 req.sense = sense;
2213 req.data = buf;
2214 req.data_len = buflen;
2215 req.flags |= REQ_QUIET;
2216 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2217 req.cmd[6] = trackno;
2218 req.cmd[7] = (buflen >> 8);
2219 req.cmd[8] = (buflen & 0xff);
2220 req.cmd[9] = (format << 6);
2222 if (msf_flag)
2223 req.cmd[1] = 2;
2225 return cdrom_queue_packet_command(drive, &req);
2229 /* Try to read the entire TOC for the disk into our internal buffer. */
2230 static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
2232 int stat, ntracks, i;
2233 struct cdrom_info *info = drive->driver_data;
2234 struct cdrom_device_info *cdi = &info->devinfo;
2235 struct atapi_toc *toc = info->toc;
2236 struct {
2237 struct atapi_toc_header hdr;
2238 struct atapi_toc_entry ent;
2239 } ms_tmp;
2240 long last_written;
2241 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
2243 if (toc == NULL) {
2244 /* Try to allocate space. */
2245 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
2246 GFP_KERNEL);
2247 info->toc = toc;
2248 if (toc == NULL) {
2249 printk ("%s: No cdrom TOC buffer!\n", drive->name);
2250 return -ENOMEM;
2254 /* Check to see if the existing data is still valid.
2255 If it is, just return. */
2256 (void) cdrom_check_status(drive, sense);
2258 if (CDROM_STATE_FLAGS(drive)->toc_valid)
2259 return 0;
2261 /* Try to get the total cdrom capacity and sector size. */
2262 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
2263 sense);
2264 if (stat)
2265 toc->capacity = 0x1fffff;
2267 set_capacity(drive->disk, toc->capacity * sectors_per_frame);
2268 blk_queue_hardsect_size(drive->queue,
2269 sectors_per_frame << SECTOR_BITS);
2271 /* First read just the header, so we know how long the TOC is. */
2272 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
2273 sizeof(struct atapi_toc_header), sense);
2274 if (stat) return stat;
2276 #if ! STANDARD_ATAPI
2277 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2278 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2279 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2281 #endif /* not STANDARD_ATAPI */
2283 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2284 if (ntracks <= 0)
2285 return -EIO;
2286 if (ntracks > MAX_TRACKS)
2287 ntracks = MAX_TRACKS;
2289 /* Now read the whole schmeer. */
2290 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
2291 (char *)&toc->hdr,
2292 sizeof(struct atapi_toc_header) +
2293 (ntracks + 1) *
2294 sizeof(struct atapi_toc_entry), sense);
2296 if (stat && toc->hdr.first_track > 1) {
2297 /* Cds with CDI tracks only don't have any TOC entries,
2298 despite of this the returned values are
2299 first_track == last_track = number of CDI tracks + 1,
2300 so that this case is indistinguishable from the same
2301 layout plus an additional audio track.
2302 If we get an error for the regular case, we assume
2303 a CDI without additional audio tracks. In this case
2304 the readable TOC is empty (CDI tracks are not included)
2305 and only holds the Leadout entry. Heiko Eißfeldt */
2306 ntracks = 0;
2307 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
2308 (char *)&toc->hdr,
2309 sizeof(struct atapi_toc_header) +
2310 (ntracks + 1) *
2311 sizeof(struct atapi_toc_entry),
2312 sense);
2313 if (stat) {
2314 return stat;
2316 #if ! STANDARD_ATAPI
2317 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2318 toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
2319 toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
2320 } else
2321 #endif /* not STANDARD_ATAPI */
2323 toc->hdr.first_track = CDROM_LEADOUT;
2324 toc->hdr.last_track = CDROM_LEADOUT;
2328 if (stat)
2329 return stat;
2331 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
2333 #if ! STANDARD_ATAPI
2334 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2335 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2336 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2338 #endif /* not STANDARD_ATAPI */
2340 for (i=0; i<=ntracks; i++) {
2341 #if ! STANDARD_ATAPI
2342 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
2343 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd)
2344 toc->ent[i].track = bcd2bin(toc->ent[i].track);
2345 msf_from_bcd(&toc->ent[i].addr.msf);
2347 #endif /* not STANDARD_ATAPI */
2348 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
2349 toc->ent[i].addr.msf.second,
2350 toc->ent[i].addr.msf.frame);
2353 /* Read the multisession information. */
2354 if (toc->hdr.first_track != CDROM_LEADOUT) {
2355 /* Read the multisession information. */
2356 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
2357 sizeof(ms_tmp), sense);
2358 if (stat) return stat;
2359 } else {
2360 ms_tmp.ent.addr.msf.minute = 0;
2361 ms_tmp.ent.addr.msf.second = 2;
2362 ms_tmp.ent.addr.msf.frame = 0;
2363 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
2366 #if ! STANDARD_ATAPI
2367 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd)
2368 msf_from_bcd (&ms_tmp.ent.addr.msf);
2369 #endif /* not STANDARD_ATAPI */
2371 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
2372 ms_tmp.ent.addr.msf.second,
2373 ms_tmp.ent.addr.msf.frame);
2375 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
2377 /* Now try to get the total cdrom capacity. */
2378 stat = cdrom_get_last_written(cdi, &last_written);
2379 if (!stat && (last_written > toc->capacity)) {
2380 toc->capacity = last_written;
2381 set_capacity(drive->disk, toc->capacity * sectors_per_frame);
2384 /* Remember that we've read this stuff. */
2385 CDROM_STATE_FLAGS(drive)->toc_valid = 1;
2387 return 0;
2391 static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2392 int buflen, struct request_sense *sense)
2394 struct request req;
2396 cdrom_prepare_request(&req);
2398 req.sense = sense;
2399 req.data = buf;
2400 req.data_len = buflen;
2401 req.cmd[0] = GPCMD_READ_SUBCHANNEL;
2402 req.cmd[1] = 2; /* MSF addressing */
2403 req.cmd[2] = 0x40; /* request subQ data */
2404 req.cmd[3] = format;
2405 req.cmd[7] = (buflen >> 8);
2406 req.cmd[8] = (buflen & 0xff);
2407 return cdrom_queue_packet_command(drive, &req);
2410 /* ATAPI cdrom drives are free to select the speed you request or any slower
2411 rate :-( Requesting too fast a speed will _not_ produce an error. */
2412 static int cdrom_select_speed(ide_drive_t *drive, int speed,
2413 struct request_sense *sense)
2415 struct request req;
2416 cdrom_prepare_request(&req);
2418 req.sense = sense;
2419 if (speed == 0)
2420 speed = 0xffff; /* set to max */
2421 else
2422 speed *= 177; /* Nx to kbytes/s */
2424 req.cmd[0] = GPCMD_SET_SPEED;
2425 /* Read Drive speed in kbytes/second MSB */
2426 req.cmd[2] = (speed >> 8) & 0xff;
2427 /* Read Drive speed in kbytes/second LSB */
2428 req.cmd[3] = speed & 0xff;
2429 if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
2430 CDROM_CONFIG_FLAGS(drive)->cd_rw ||
2431 CDROM_CONFIG_FLAGS(drive)->dvd_r) {
2432 /* Write Drive speed in kbytes/second MSB */
2433 req.cmd[4] = (speed >> 8) & 0xff;
2434 /* Write Drive speed in kbytes/second LSB */
2435 req.cmd[5] = speed & 0xff;
2438 return cdrom_queue_packet_command(drive, &req);
2441 static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2443 struct request_sense sense;
2444 struct request req;
2446 cdrom_prepare_request(&req);
2448 req.sense = &sense;
2449 req.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2450 lba_to_msf(lba_start, &req.cmd[3], &req.cmd[4], &req.cmd[5]);
2451 lba_to_msf(lba_end-1, &req.cmd[6], &req.cmd[7], &req.cmd[8]);
2453 return cdrom_queue_packet_command(drive, &req);
2456 static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2457 struct atapi_toc_entry **ent)
2459 struct cdrom_info *info = drive->driver_data;
2460 struct atapi_toc *toc = info->toc;
2461 int ntracks;
2464 * don't serve cached data, if the toc isn't valid
2466 if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2467 return -EINVAL;
2469 /* Check validity of requested track number. */
2470 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2471 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2472 if (track == CDROM_LEADOUT)
2473 *ent = &toc->ent[ntracks];
2474 else if (track < toc->hdr.first_track ||
2475 track > toc->hdr.last_track)
2476 return -EINVAL;
2477 else
2478 *ent = &toc->ent[track - toc->hdr.first_track];
2480 return 0;
2483 /* the generic packet interface to cdrom.c */
2484 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2485 struct packet_command *cgc)
2487 struct request req;
2488 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2490 if (cgc->timeout <= 0)
2491 cgc->timeout = ATAPI_WAIT_PC;
2493 /* here we queue the commands from the uniform CD-ROM
2494 layer. the packet must be complete, as we do not
2495 touch it at all. */
2496 cdrom_prepare_request(&req);
2497 memcpy(req.cmd, cgc->cmd, CDROM_PACKET_SIZE);
2498 if (cgc->sense)
2499 memset(cgc->sense, 0, sizeof(struct request_sense));
2500 req.data = cgc->buffer;
2501 req.data_len = cgc->buflen;
2502 req.timeout = cgc->timeout;
2504 if (cgc->quiet)
2505 req.flags |= REQ_QUIET;
2507 req.sense = cgc->sense;
2508 cgc->stat = cdrom_queue_packet_command(drive, &req);
2509 if (!cgc->stat)
2510 cgc->buflen -= req.data_len;
2511 return cgc->stat;
2514 static
2515 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
2516 unsigned int cmd, unsigned long arg)
2518 struct packet_command cgc;
2519 char buffer[16];
2520 int stat;
2522 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2524 /* These will be moved into the Uniform layer shortly... */
2525 switch (cmd) {
2526 case CDROMSETSPINDOWN: {
2527 char spindown;
2529 if (copy_from_user(&spindown, (void __user *) arg, sizeof(char)))
2530 return -EFAULT;
2532 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2533 return stat;
2535 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2537 return cdrom_mode_select(cdi, &cgc);
2540 case CDROMGETSPINDOWN: {
2541 char spindown;
2543 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2544 return stat;
2546 spindown = buffer[11] & 0x0f;
2548 if (copy_to_user((void __user *) arg, &spindown, sizeof (char)))
2549 return -EFAULT;
2551 return 0;
2554 default:
2555 return -EINVAL;
2560 static
2561 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2562 unsigned int cmd, void *arg)
2565 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2566 struct cdrom_info *info = drive->driver_data;
2567 int stat;
2569 switch (cmd) {
2571 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2572 * atapi doesn't support it
2574 case CDROMPLAYTRKIND: {
2575 unsigned long lba_start, lba_end;
2576 struct cdrom_ti *ti = (struct cdrom_ti *)arg;
2577 struct atapi_toc_entry *first_toc, *last_toc;
2579 stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2580 if (stat)
2581 return stat;
2583 stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2584 if (stat)
2585 return stat;
2587 if (ti->cdti_trk1 != CDROM_LEADOUT)
2588 ++last_toc;
2589 lba_start = first_toc->addr.lba;
2590 lba_end = last_toc->addr.lba;
2592 if (lba_end <= lba_start)
2593 return -EINVAL;
2595 return cdrom_play_audio(drive, lba_start, lba_end);
2598 case CDROMREADTOCHDR: {
2599 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2600 struct atapi_toc *toc;
2602 /* Make sure our saved TOC is valid. */
2603 stat = cdrom_read_toc(drive, NULL);
2604 if (stat) return stat;
2606 toc = info->toc;
2607 tochdr->cdth_trk0 = toc->hdr.first_track;
2608 tochdr->cdth_trk1 = toc->hdr.last_track;
2610 return 0;
2613 case CDROMREADTOCENTRY: {
2614 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
2615 struct atapi_toc_entry *toce;
2617 stat = cdrom_get_toc_entry(drive, tocentry->cdte_track, &toce);
2618 if (stat) return stat;
2620 tocentry->cdte_ctrl = toce->control;
2621 tocentry->cdte_adr = toce->adr;
2622 if (tocentry->cdte_format == CDROM_MSF) {
2623 lba_to_msf (toce->addr.lba,
2624 &tocentry->cdte_addr.msf.minute,
2625 &tocentry->cdte_addr.msf.second,
2626 &tocentry->cdte_addr.msf.frame);
2627 } else
2628 tocentry->cdte_addr.lba = toce->addr.lba;
2630 return 0;
2633 default:
2634 return -EINVAL;
2638 static
2639 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2641 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2642 struct request_sense sense;
2643 struct request req;
2644 int ret;
2646 cdrom_prepare_request(&req);
2647 req.flags = REQ_SPECIAL | REQ_QUIET;
2648 ret = ide_do_drive_cmd(drive, &req, ide_wait);
2651 * A reset will unlock the door. If it was previously locked,
2652 * lock it again.
2654 if (CDROM_STATE_FLAGS(drive)->door_locked)
2655 (void) cdrom_lockdoor(drive, 1, &sense);
2657 return ret;
2661 static
2662 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2664 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2665 struct request_sense sense;
2667 if (position) {
2668 int stat = cdrom_lockdoor(drive, 0, &sense);
2669 if (stat) return stat;
2672 return cdrom_eject(drive, !position, &sense);
2675 static
2676 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2678 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2679 return cdrom_lockdoor(drive, lock, NULL);
2682 static
2683 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2685 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2686 struct request_sense sense;
2687 int stat;
2689 if ((stat = cdrom_select_speed(drive, speed, &sense)) < 0)
2690 return stat;
2692 cdi->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2693 return 0;
2697 * add logic to try GET_EVENT command first to check for media and tray
2698 * status. this should be supported by newer cd-r/w and all DVD etc
2699 * drives
2701 static
2702 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2704 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2705 struct media_event_desc med;
2706 struct request_sense sense;
2707 int stat;
2709 if (slot_nr != CDSL_CURRENT)
2710 return -EINVAL;
2712 stat = cdrom_check_status(drive, &sense);
2713 if (!stat || sense.sense_key == UNIT_ATTENTION)
2714 return CDS_DISC_OK;
2716 if (!cdrom_get_media_event(cdi, &med)) {
2717 if (med.media_present)
2718 return CDS_DISC_OK;
2719 else if (med.door_open)
2720 return CDS_TRAY_OPEN;
2721 else
2722 return CDS_NO_DISC;
2725 if (sense.sense_key == NOT_READY && sense.asc == 0x04 && sense.ascq == 0x04)
2726 return CDS_DISC_OK;
2729 * If not using Mt Fuji extended media tray reports,
2730 * just return TRAY_OPEN since ATAPI doesn't provide
2731 * any other way to detect this...
2733 if (sense.sense_key == NOT_READY) {
2734 if (sense.asc == 0x3a) {
2735 if (sense.ascq == 0 || sense.ascq == 1)
2736 return CDS_NO_DISC;
2737 else if (sense.ascq == 2)
2738 return CDS_TRAY_OPEN;
2742 return CDS_DRIVE_NOT_READY;
2745 static
2746 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2747 struct cdrom_multisession *ms_info)
2749 struct atapi_toc *toc;
2750 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2751 struct cdrom_info *info = drive->driver_data;
2752 struct request_sense sense;
2753 int ret;
2755 if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2756 if ((ret = cdrom_read_toc(drive, &sense)))
2757 return ret;
2759 toc = info->toc;
2760 ms_info->addr.lba = toc->last_session_lba;
2761 ms_info->xa_flag = toc->xa_flag;
2763 return 0;
2766 static
2767 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2768 struct cdrom_mcn *mcn_info)
2770 int stat;
2771 char mcnbuf[24];
2772 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2774 /* get MCN */
2775 if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2776 return stat;
2778 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2779 sizeof (mcn_info->medium_catalog_number)-1);
2780 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2781 = '\0';
2783 return 0;
2788 /****************************************************************************
2789 * Other driver requests (open, close, check media change).
2792 static
2793 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2794 int slot_nr)
2796 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2797 int retval;
2799 if (slot_nr == CDSL_CURRENT) {
2800 (void) cdrom_check_status(drive, NULL);
2801 retval = CDROM_STATE_FLAGS(drive)->media_changed;
2802 CDROM_STATE_FLAGS(drive)->media_changed = 0;
2803 return retval;
2804 } else {
2805 return -EINVAL;
2810 static
2811 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2813 return 0;
2817 * Close down the device. Invalidate all cached blocks.
2820 static
2821 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2823 ide_drive_t *drive = cdi->handle;
2825 if (!cdi->use_count)
2826 CDROM_STATE_FLAGS(drive)->toc_valid = 0;
2831 /****************************************************************************
2832 * Device initialization.
2834 static struct cdrom_device_ops ide_cdrom_dops = {
2835 .open = ide_cdrom_open_real,
2836 .release = ide_cdrom_release_real,
2837 .drive_status = ide_cdrom_drive_status,
2838 .media_changed = ide_cdrom_check_media_change_real,
2839 .tray_move = ide_cdrom_tray_move,
2840 .lock_door = ide_cdrom_lock_door,
2841 .select_speed = ide_cdrom_select_speed,
2842 .get_last_session = ide_cdrom_get_last_session,
2843 .get_mcn = ide_cdrom_get_mcn,
2844 .reset = ide_cdrom_reset,
2845 .audio_ioctl = ide_cdrom_audio_ioctl,
2846 .dev_ioctl = ide_cdrom_dev_ioctl,
2847 .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2848 CDC_SELECT_SPEED | CDC_SELECT_DISC |
2849 CDC_MULTI_SESSION | CDC_MCN |
2850 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
2851 CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
2852 CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2853 CDC_GENERIC_PACKET | CDC_MO_DRIVE | CDC_MRW |
2854 CDC_MRW_W | CDC_RAM,
2855 .generic_packet = ide_cdrom_packet,
2858 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2860 struct cdrom_info *info = drive->driver_data;
2861 struct cdrom_device_info *devinfo = &info->devinfo;
2863 devinfo->ops = &ide_cdrom_dops;
2864 devinfo->mask = 0;
2865 devinfo->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2866 devinfo->capacity = nslots;
2867 devinfo->handle = (void *) drive;
2868 strcpy(devinfo->name, drive->name);
2870 /* set capability mask to match the probe. */
2871 if (!CDROM_CONFIG_FLAGS(drive)->cd_r)
2872 devinfo->mask |= CDC_CD_R;
2873 if (!CDROM_CONFIG_FLAGS(drive)->cd_rw)
2874 devinfo->mask |= CDC_CD_RW;
2875 if (!CDROM_CONFIG_FLAGS(drive)->dvd)
2876 devinfo->mask |= CDC_DVD;
2877 if (!CDROM_CONFIG_FLAGS(drive)->dvd_r)
2878 devinfo->mask |= CDC_DVD_R;
2879 if (!CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2880 devinfo->mask |= CDC_DVD_RAM;
2881 if (!CDROM_CONFIG_FLAGS(drive)->is_changer)
2882 devinfo->mask |= CDC_SELECT_DISC;
2883 if (!CDROM_CONFIG_FLAGS(drive)->audio_play)
2884 devinfo->mask |= CDC_PLAY_AUDIO;
2885 if (!CDROM_CONFIG_FLAGS(drive)->close_tray)
2886 devinfo->mask |= CDC_CLOSE_TRAY;
2887 if (!CDROM_CONFIG_FLAGS(drive)->mo_drive)
2888 devinfo->mask |= CDC_MO_DRIVE;
2890 devinfo->disk = drive->disk;
2891 return register_cdrom(devinfo);
2894 static
2895 int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2897 struct cdrom_info *info = drive->driver_data;
2898 struct cdrom_device_info *cdi = &info->devinfo;
2899 struct packet_command cgc;
2900 int stat, attempts = 3, size = sizeof(*cap);
2903 * ACER50 (and others?) require the full spec length mode sense
2904 * page capabilities size, but older drives break.
2906 if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2907 !strcmp(drive->id->model, "WPI CDS-32X")))
2908 size -= sizeof(cap->pad);
2910 init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2911 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2912 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2913 if (!stat)
2914 break;
2915 } while (--attempts);
2916 return stat;
2919 static
2920 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2922 struct cdrom_info *info = drive->driver_data;
2923 struct cdrom_device_info *cdi = &info->devinfo;
2924 struct atapi_capabilities_page cap;
2925 int nslots = 1;
2927 if (drive->media == ide_optical) {
2928 CDROM_CONFIG_FLAGS(drive)->mo_drive = 1;
2929 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2930 printk("%s: ATAPI magneto-optical drive\n", drive->name);
2931 return nslots;
2934 if (CDROM_CONFIG_FLAGS(drive)->nec260 ||
2935 !strcmp(drive->id->model,"STINGRAY 8422 IDE 8X CD-ROM 7-27-95")) {
2936 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2937 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2938 return nslots;
2942 * we have to cheat a little here. the packet will eventually
2943 * be queued with ide_cdrom_packet(), which extracts the
2944 * drive from cdi->handle. Since this device hasn't been
2945 * registered with the Uniform layer yet, it can't do this.
2946 * Same goes for cdi->ops.
2948 cdi->handle = (ide_drive_t *) drive;
2949 cdi->ops = &ide_cdrom_dops;
2951 if (ide_cdrom_get_capabilities(drive, &cap))
2952 return 0;
2954 if (cap.lock == 0)
2955 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2956 if (cap.eject)
2957 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2958 if (cap.cd_r_write)
2959 CDROM_CONFIG_FLAGS(drive)->cd_r = 1;
2960 if (cap.cd_rw_write) {
2961 CDROM_CONFIG_FLAGS(drive)->cd_rw = 1;
2962 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2964 if (cap.test_write)
2965 CDROM_CONFIG_FLAGS(drive)->test_write = 1;
2966 if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2967 CDROM_CONFIG_FLAGS(drive)->dvd = 1;
2968 if (cap.dvd_ram_write) {
2969 CDROM_CONFIG_FLAGS(drive)->dvd_ram = 1;
2970 CDROM_CONFIG_FLAGS(drive)->ram = 1;
2972 if (cap.dvd_r_write)
2973 CDROM_CONFIG_FLAGS(drive)->dvd_r = 1;
2974 if (cap.audio_play)
2975 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2976 if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2977 CDROM_CONFIG_FLAGS(drive)->close_tray = 0;
2979 /* Some drives used by Apple don't advertise audio play
2980 * but they do support reading TOC & audio datas
2982 if (strcmp(drive->id->model, "MATSHITADVD-ROM SR-8187") == 0 ||
2983 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8186") == 0 ||
2984 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8176") == 0 ||
2985 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8174") == 0)
2986 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2988 #if ! STANDARD_ATAPI
2989 if (cdi->sanyo_slot > 0) {
2990 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2991 nslots = 3;
2994 else
2995 #endif /* not STANDARD_ATAPI */
2996 if (cap.mechtype == mechtype_individual_changer ||
2997 cap.mechtype == mechtype_cartridge_changer) {
2998 if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2999 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
3000 CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 1;
3004 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
3005 if (!drive->id->model[0] &&
3006 !strncmp(drive->id->fw_rev, "241N", 4)) {
3007 CDROM_STATE_FLAGS(drive)->current_speed =
3008 (((unsigned int)cap.curspeed) + (176/2)) / 176;
3009 CDROM_CONFIG_FLAGS(drive)->max_speed =
3010 (((unsigned int)cap.maxspeed) + (176/2)) / 176;
3011 } else {
3012 CDROM_STATE_FLAGS(drive)->current_speed =
3013 (ntohs(cap.curspeed) + (176/2)) / 176;
3014 CDROM_CONFIG_FLAGS(drive)->max_speed =
3015 (ntohs(cap.maxspeed) + (176/2)) / 176;
3018 /* don't print speed if the drive reported 0.
3020 printk("%s: ATAPI", drive->name);
3021 if (CDROM_CONFIG_FLAGS(drive)->max_speed)
3022 printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
3023 printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
3025 if (CDROM_CONFIG_FLAGS(drive)->dvd_r|CDROM_CONFIG_FLAGS(drive)->dvd_ram)
3026 printk(" DVD%s%s",
3027 (CDROM_CONFIG_FLAGS(drive)->dvd_r)? "-R" : "",
3028 (CDROM_CONFIG_FLAGS(drive)->dvd_ram)? "-RAM" : "");
3030 if (CDROM_CONFIG_FLAGS(drive)->cd_r|CDROM_CONFIG_FLAGS(drive)->cd_rw)
3031 printk(" CD%s%s",
3032 (CDROM_CONFIG_FLAGS(drive)->cd_r)? "-R" : "",
3033 (CDROM_CONFIG_FLAGS(drive)->cd_rw)? "/RW" : "");
3035 if (CDROM_CONFIG_FLAGS(drive)->is_changer)
3036 printk(" changer w/%d slots", nslots);
3037 else
3038 printk(" drive");
3040 printk(", %dkB Cache", be16_to_cpu(cap.buffer_size));
3042 if (drive->using_dma)
3043 ide_dma_verbose(drive);
3045 printk("\n");
3047 return nslots;
3050 static void ide_cdrom_add_settings(ide_drive_t *drive)
3052 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
3056 * standard prep_rq_fn that builds 10 byte cmds
3058 static int ide_cdrom_prep_fs(request_queue_t *q, struct request *rq)
3060 int hard_sect = queue_hardsect_size(q);
3061 long block = (long)rq->hard_sector / (hard_sect >> 9);
3062 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
3064 memset(rq->cmd, 0, sizeof(rq->cmd));
3066 if (rq_data_dir(rq) == READ)
3067 rq->cmd[0] = GPCMD_READ_10;
3068 else
3069 rq->cmd[0] = GPCMD_WRITE_10;
3072 * fill in lba
3074 rq->cmd[2] = (block >> 24) & 0xff;
3075 rq->cmd[3] = (block >> 16) & 0xff;
3076 rq->cmd[4] = (block >> 8) & 0xff;
3077 rq->cmd[5] = block & 0xff;
3080 * and transfer length
3082 rq->cmd[7] = (blocks >> 8) & 0xff;
3083 rq->cmd[8] = blocks & 0xff;
3084 rq->cmd_len = 10;
3085 return BLKPREP_OK;
3089 * Most of the SCSI commands are supported directly by ATAPI devices.
3090 * This transform handles the few exceptions.
3092 static int ide_cdrom_prep_pc(struct request *rq)
3094 u8 *c = rq->cmd;
3097 * Transform 6-byte read/write commands to the 10-byte version
3099 if (c[0] == READ_6 || c[0] == WRITE_6) {
3100 c[8] = c[4];
3101 c[5] = c[3];
3102 c[4] = c[2];
3103 c[3] = c[1] & 0x1f;
3104 c[2] = 0;
3105 c[1] &= 0xe0;
3106 c[0] += (READ_10 - READ_6);
3107 rq->cmd_len = 10;
3108 return BLKPREP_OK;
3112 * it's silly to pretend we understand 6-byte sense commands, just
3113 * reject with ILLEGAL_REQUEST and the caller should take the
3114 * appropriate action
3116 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
3117 rq->errors = ILLEGAL_REQUEST;
3118 return BLKPREP_KILL;
3121 return BLKPREP_OK;
3124 static int ide_cdrom_prep_fn(request_queue_t *q, struct request *rq)
3126 if (rq->flags & REQ_CMD)
3127 return ide_cdrom_prep_fs(q, rq);
3128 else if (rq->flags & REQ_BLOCK_PC)
3129 return ide_cdrom_prep_pc(rq);
3131 return 0;
3134 static
3135 int ide_cdrom_setup (ide_drive_t *drive)
3137 struct cdrom_info *info = drive->driver_data;
3138 struct cdrom_device_info *cdi = &info->devinfo;
3139 int nslots;
3141 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
3142 blk_queue_dma_alignment(drive->queue, 31);
3143 drive->queue->unplug_delay = (1 * HZ) / 1000;
3144 if (!drive->queue->unplug_delay)
3145 drive->queue->unplug_delay = 1;
3147 drive->special.all = 0;
3148 drive->ready_stat = 0;
3150 CDROM_STATE_FLAGS(drive)->media_changed = 1;
3151 CDROM_STATE_FLAGS(drive)->toc_valid = 0;
3152 CDROM_STATE_FLAGS(drive)->door_locked = 0;
3154 #if NO_DOOR_LOCKING
3155 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
3156 #else
3157 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 0;
3158 #endif
3160 CDROM_CONFIG_FLAGS(drive)->drq_interrupt = ((drive->id->config & 0x0060) == 0x20);
3161 CDROM_CONFIG_FLAGS(drive)->is_changer = 0;
3162 CDROM_CONFIG_FLAGS(drive)->cd_r = 0;
3163 CDROM_CONFIG_FLAGS(drive)->cd_rw = 0;
3164 CDROM_CONFIG_FLAGS(drive)->test_write = 0;
3165 CDROM_CONFIG_FLAGS(drive)->dvd = 0;
3166 CDROM_CONFIG_FLAGS(drive)->dvd_r = 0;
3167 CDROM_CONFIG_FLAGS(drive)->dvd_ram = 0;
3168 CDROM_CONFIG_FLAGS(drive)->no_eject = 1;
3169 CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 0;
3170 CDROM_CONFIG_FLAGS(drive)->audio_play = 0;
3171 CDROM_CONFIG_FLAGS(drive)->close_tray = 1;
3173 /* limit transfer size per interrupt. */
3174 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 0;
3175 /* a testament to the nice quality of Samsung drives... */
3176 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
3177 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
3178 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
3179 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
3180 /* the 3231 model does not support the SET_CD_SPEED command */
3181 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
3182 cdi->mask |= CDC_SELECT_SPEED;
3184 #if ! STANDARD_ATAPI
3185 /* by default Sanyo 3 CD changer support is turned off and
3186 ATAPI Rev 2.2+ standard support for CD changers is used */
3187 cdi->sanyo_slot = 0;
3189 CDROM_CONFIG_FLAGS(drive)->nec260 = 0;
3190 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 0;
3191 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 0;
3192 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 0;
3193 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 0;
3195 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
3196 drive->id->fw_rev[4] == '1' &&
3197 drive->id->fw_rev[6] <= '2') {
3198 /* Vertos 300.
3199 Some versions of this drive like to talk BCD. */
3200 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
3201 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
3202 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3203 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3206 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
3207 drive->id->fw_rev[4] == '1' &&
3208 drive->id->fw_rev[6] <= '2') {
3209 /* Vertos 600 ESD. */
3210 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
3212 else if (strcmp(drive->id->model, "NEC CD-ROM DRIVE:260") == 0 &&
3213 strncmp(drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
3214 /* Old NEC260 (not R).
3215 This drive was released before the 1.2 version
3216 of the spec. */
3217 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
3218 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3219 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3220 CDROM_CONFIG_FLAGS(drive)->nec260 = 1;
3222 else if (strcmp(drive->id->model, "WEARNES CDD-120") == 0 &&
3223 strncmp(drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
3224 /* Wearnes */
3225 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3226 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3228 /* Sanyo 3 CD changer uses a non-standard command
3229 for CD changing */
3230 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
3231 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
3232 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
3233 /* uses CD in slot 0 when value is set to 3 */
3234 cdi->sanyo_slot = 3;
3236 #endif /* not STANDARD_ATAPI */
3238 info->toc = NULL;
3239 info->buffer = NULL;
3240 info->sector_buffered = 0;
3241 info->nsectors_buffered = 0;
3242 info->changer_info = NULL;
3243 info->last_block = 0;
3244 info->start_seek = 0;
3246 nslots = ide_cdrom_probe_capabilities (drive);
3249 * set correct block size
3251 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
3253 #if 0
3254 drive->dsc_overlap = (HWIF(drive)->no_dsc) ? 0 : 1;
3255 if (HWIF(drive)->no_dsc) {
3256 printk(KERN_INFO "ide-cd: %s: disabling DSC overlap\n",
3257 drive->name);
3258 drive->dsc_overlap = 0;
3260 #endif
3262 if (ide_cdrom_register(drive, nslots)) {
3263 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
3264 info->devinfo.handle = NULL;
3265 return 1;
3267 ide_cdrom_add_settings(drive);
3268 return 0;
3271 static
3272 sector_t ide_cdrom_capacity (ide_drive_t *drive)
3274 unsigned long capacity, sectors_per_frame;
3276 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
3277 return 0;
3279 return capacity * sectors_per_frame;
3282 static
3283 int ide_cdrom_cleanup(ide_drive_t *drive)
3285 struct cdrom_info *info = drive->driver_data;
3286 struct cdrom_device_info *devinfo = &info->devinfo;
3287 struct gendisk *g = drive->disk;
3289 if (ide_unregister_subdriver(drive)) {
3290 printk("%s: %s: failed to ide_unregister_subdriver\n",
3291 __FUNCTION__, drive->name);
3292 return 1;
3294 if (info->buffer != NULL)
3295 kfree(info->buffer);
3296 if (info->toc != NULL)
3297 kfree(info->toc);
3298 if (info->changer_info != NULL)
3299 kfree(info->changer_info);
3300 if (devinfo->handle == drive && unregister_cdrom(devinfo))
3301 printk("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3302 kfree(info);
3303 drive->driver_data = NULL;
3304 blk_queue_prep_rq(drive->queue, NULL);
3305 del_gendisk(g);
3306 g->fops = ide_fops;
3307 return 0;
3310 static int ide_cdrom_attach (ide_drive_t *drive);
3313 * Power Management state machine.
3315 * We don't do much for CDs right now.
3318 static void ide_cdrom_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
3322 static ide_startstop_t ide_cdrom_start_power_step (ide_drive_t *drive, struct request *rq)
3324 ide_task_t *args = rq->special;
3326 memset(args, 0, sizeof(*args));
3328 switch (rq->pm->pm_step) {
3329 case ide_pm_state_start_suspend:
3330 break;
3332 case ide_pm_state_start_resume: /* Resume step 1 (restore DMA) */
3334 * Right now, all we do is call hwif->ide_dma_check(drive),
3335 * we could be smarter and check for current xfer_speed
3336 * in struct drive etc...
3337 * Also, this step could be implemented as a generic helper
3338 * as most subdrivers will use it.
3340 if ((drive->id->capability & 1) == 0)
3341 break;
3342 if (HWIF(drive)->ide_dma_check == NULL)
3343 break;
3344 HWIF(drive)->ide_dma_check(drive);
3345 break;
3347 rq->pm->pm_step = ide_pm_state_completed;
3348 return ide_stopped;
3351 static ide_driver_t ide_cdrom_driver = {
3352 .owner = THIS_MODULE,
3353 .name = "ide-cdrom",
3354 .version = IDECD_VERSION,
3355 .media = ide_cdrom,
3356 .busy = 0,
3357 .supports_dsc_overlap = 1,
3358 .cleanup = ide_cdrom_cleanup,
3359 .do_request = ide_do_rw_cdrom,
3360 .sense = ide_dump_atapi_status,
3361 .error = ide_cdrom_error,
3362 .abort = ide_cdrom_abort,
3363 .capacity = ide_cdrom_capacity,
3364 .attach = ide_cdrom_attach,
3365 .drives = LIST_HEAD_INIT(ide_cdrom_driver.drives),
3366 .start_power_step = ide_cdrom_start_power_step,
3367 .complete_power_step = ide_cdrom_complete_power_step,
3370 static int idecd_open(struct inode * inode, struct file * file)
3372 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
3373 struct cdrom_info *info = drive->driver_data;
3374 int rc = -ENOMEM;
3375 drive->usage++;
3377 if (!info->buffer)
3378 info->buffer = kmalloc(SECTOR_BUFFER_SIZE,
3379 GFP_KERNEL|__GFP_REPEAT);
3380 if (!info->buffer || (rc = cdrom_open(&info->devinfo, inode, file)))
3381 drive->usage--;
3382 return rc;
3385 static int idecd_release(struct inode * inode, struct file * file)
3387 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
3388 struct cdrom_info *info = drive->driver_data;
3390 cdrom_release (&info->devinfo, file);
3391 drive->usage--;
3392 return 0;
3395 static int idecd_ioctl (struct inode *inode, struct file *file,
3396 unsigned int cmd, unsigned long arg)
3398 struct block_device *bdev = inode->i_bdev;
3399 ide_drive_t *drive = bdev->bd_disk->private_data;
3400 int err = generic_ide_ioctl(file, bdev, cmd, arg);
3401 if (err == -EINVAL) {
3402 struct cdrom_info *info = drive->driver_data;
3403 err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
3405 return err;
3408 static int idecd_media_changed(struct gendisk *disk)
3410 ide_drive_t *drive = disk->private_data;
3411 struct cdrom_info *info = drive->driver_data;
3412 return cdrom_media_changed(&info->devinfo);
3415 static int idecd_revalidate_disk(struct gendisk *disk)
3417 ide_drive_t *drive = disk->private_data;
3418 struct request_sense sense;
3419 cdrom_read_toc(drive, &sense);
3420 return 0;
3423 static struct block_device_operations idecd_ops = {
3424 .owner = THIS_MODULE,
3425 .open = idecd_open,
3426 .release = idecd_release,
3427 .ioctl = idecd_ioctl,
3428 .media_changed = idecd_media_changed,
3429 .revalidate_disk= idecd_revalidate_disk
3432 /* options */
3433 char *ignore = NULL;
3435 module_param(ignore, charp, 0400);
3436 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3438 static int ide_cdrom_attach (ide_drive_t *drive)
3440 struct cdrom_info *info;
3441 struct gendisk *g = drive->disk;
3442 struct request_sense sense;
3444 if (!strstr("ide-cdrom", drive->driver_req))
3445 goto failed;
3446 if (!drive->present)
3447 goto failed;
3448 if (drive->media != ide_cdrom && drive->media != ide_optical)
3449 goto failed;
3450 /* skip drives that we were told to ignore */
3451 if (ignore != NULL) {
3452 if (strstr(ignore, drive->name)) {
3453 printk("ide-cd: ignoring drive %s\n", drive->name);
3454 goto failed;
3457 if (drive->scsi) {
3458 printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
3459 goto failed;
3461 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
3462 if (info == NULL) {
3463 printk("%s: Can't allocate a cdrom structure\n", drive->name);
3464 goto failed;
3466 if (ide_register_subdriver(drive, &ide_cdrom_driver)) {
3467 printk("%s: Failed to register the driver with ide.c\n",
3468 drive->name);
3469 kfree(info);
3470 goto failed;
3472 memset(info, 0, sizeof (struct cdrom_info));
3473 drive->driver_data = info;
3474 DRIVER(drive)->busy++;
3475 g->minors = 1;
3476 snprintf(g->devfs_name, sizeof(g->devfs_name),
3477 "%s/cd", drive->devfs_name);
3478 g->driverfs_dev = &drive->gendev;
3479 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
3480 if (ide_cdrom_setup(drive)) {
3481 struct cdrom_device_info *devinfo = &info->devinfo;
3482 DRIVER(drive)->busy--;
3483 ide_unregister_subdriver(drive);
3484 if (info->buffer != NULL)
3485 kfree(info->buffer);
3486 if (info->toc != NULL)
3487 kfree(info->toc);
3488 if (info->changer_info != NULL)
3489 kfree(info->changer_info);
3490 if (devinfo->handle == drive && unregister_cdrom(devinfo))
3491 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3492 kfree(info);
3493 drive->driver_data = NULL;
3494 goto failed;
3496 DRIVER(drive)->busy--;
3498 cdrom_read_toc(drive, &sense);
3499 g->fops = &idecd_ops;
3500 g->flags |= GENHD_FL_REMOVABLE;
3501 add_disk(g);
3502 return 0;
3503 failed:
3504 return 1;
3507 static void __exit ide_cdrom_exit(void)
3509 ide_unregister_driver(&ide_cdrom_driver);
3512 static int ide_cdrom_init(void)
3514 ide_register_driver(&ide_cdrom_driver);
3515 return 0;
3518 module_init(ide_cdrom_init);
3519 module_exit(ide_cdrom_exit);
3520 MODULE_LICENSE("GPL");