More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / ide-cd.c
blob7dac5c708fabdde564220f47f4be4ac530672166
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>
295 *************************************************************************/
297 #define IDECD_VERSION "4.59-ac1"
299 #include <linux/config.h>
300 #include <linux/module.h>
301 #include <linux/types.h>
302 #include <linux/kernel.h>
303 #include <linux/delay.h>
304 #include <linux/timer.h>
305 #include <linux/slab.h>
306 #include <linux/interrupt.h>
307 #include <linux/errno.h>
308 #include <linux/cdrom.h>
309 #include <linux/ide.h>
310 #include <linux/completion.h>
312 #include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
314 #include <asm/irq.h>
315 #include <asm/io.h>
316 #include <asm/byteorder.h>
317 #include <asm/uaccess.h>
318 #include <asm/unaligned.h>
320 #include "ide-cd.h"
322 /****************************************************************************
323 * Generic packet command support and error handling routines.
326 /* Mark that we've seen a media change, and invalidate our internal
327 buffers. */
328 static void cdrom_saw_media_change (ide_drive_t *drive)
330 struct cdrom_info *info = drive->driver_data;
332 CDROM_STATE_FLAGS (drive)->media_changed = 1;
333 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
334 info->nsectors_buffered = 0;
337 static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
338 struct request_sense *sense)
340 int log = 0;
342 if (!sense || !rq || (rq->flags & REQ_QUIET))
343 return 0;
345 switch (sense->sense_key) {
346 case NO_SENSE: case RECOVERED_ERROR:
347 break;
348 case NOT_READY:
350 * don't care about tray state messages for
351 * e.g. capacity commands or in-progress or
352 * becoming ready
354 if (sense->asc == 0x3a || sense->asc == 0x04)
355 break;
356 log = 1;
357 break;
358 case UNIT_ATTENTION:
360 * Make good and sure we've seen this potential media
361 * change. Some drives (i.e. Creative) fail to present
362 * the correct sense key in the error register.
364 cdrom_saw_media_change(drive);
365 break;
366 default:
367 log = 1;
368 break;
370 return log;
373 static
374 void cdrom_analyze_sense_data(ide_drive_t *drive,
375 struct request *failed_command,
376 struct request_sense *sense)
378 if (!cdrom_log_sense(drive, failed_command, sense))
379 return;
382 * If a read toc is executed for a CD-R or CD-RW medium where
383 * the first toc has not been recorded yet, it will fail with
384 * 05/24/00 (which is a confusing error)
386 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
387 if (sense->sense_key == 0x05 && sense->asc == 0x24)
388 return;
390 #if VERBOSE_IDE_CD_ERRORS
392 int i;
393 const char *s;
394 char buf[80];
396 printk ("ATAPI device %s:\n", drive->name);
397 if (sense->error_code==0x70)
398 printk(" Error: ");
399 else if (sense->error_code==0x71)
400 printk(" Deferred Error: ");
401 else if (sense->error_code == 0x7f)
402 printk(" Vendor-specific Error: ");
403 else
404 printk(" Unknown Error Type: ");
406 if (sense->sense_key < ARY_LEN(sense_key_texts))
407 s = sense_key_texts[sense->sense_key];
408 else
409 s = "bad sense key!";
411 printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
413 if (sense->asc == 0x40) {
414 sprintf(buf, "Diagnostic failure on component 0x%02x",
415 sense->ascq);
416 s = buf;
417 } else {
418 int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
419 unsigned long key = (sense->sense_key << 16);
420 key |= (sense->asc << 8);
421 if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
422 key |= sense->ascq;
423 s = NULL;
425 while (hi > lo) {
426 mid = (lo + hi) / 2;
427 if (sense_data_texts[mid].asc_ascq == key ||
428 sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
429 s = sense_data_texts[mid].text;
430 break;
432 else if (sense_data_texts[mid].asc_ascq > key)
433 hi = mid;
434 else
435 lo = mid+1;
439 if (s == NULL) {
440 if (sense->asc > 0x80)
441 s = "(vendor-specific error)";
442 else
443 s = "(reserved error code)";
446 printk(" %s -- (asc=0x%02x, ascq=0x%02x)\n",
447 s, sense->asc, sense->ascq);
449 if (failed_command != NULL) {
451 int lo=0, mid, hi= ARY_LEN (packet_command_texts);
452 s = NULL;
454 while (hi > lo) {
455 mid = (lo + hi) / 2;
456 if (packet_command_texts[mid].packet_command ==
457 failed_command->cmd[0]) {
458 s = packet_command_texts[mid].text;
459 break;
461 if (packet_command_texts[mid].packet_command >
462 failed_command->cmd[0])
463 hi = mid;
464 else
465 lo = mid+1;
468 printk (" The failed \"%s\" packet command was: \n \"", s);
469 for (i=0; i<sizeof (failed_command->cmd); i++)
470 printk ("%02x ", failed_command->cmd[i]);
471 printk ("\"\n");
474 /* The SKSV bit specifies validity of the sense_key_specific
475 * in the next two commands. It is bit 7 of the first byte.
476 * In the case of NOT_READY, if SKSV is set the drive can
477 * give us nice ETA readings.
479 if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
480 int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
481 printk(" Command is %02d%% complete\n", progress / 0xffff);
485 if (sense->sense_key == ILLEGAL_REQUEST &&
486 (sense->sks[0] & 0x80) != 0) {
487 printk(" Error in %s byte %d",
488 (sense->sks[0] & 0x40) != 0 ?
489 "command packet" : "command data",
490 (sense->sks[1] << 8) + sense->sks[2]);
492 if ((sense->sks[0] & 0x40) != 0)
493 printk (" bit %d", sense->sks[0] & 0x07);
495 printk ("\n");
499 #else /* not VERBOSE_IDE_CD_ERRORS */
501 /* Suppress printing unit attention and `in progress of becoming ready'
502 errors when we're not being verbose. */
504 if (sense->sense_key == UNIT_ATTENTION ||
505 (sense->sense_key == NOT_READY && (sense->asc == 4 ||
506 sense->asc == 0x3a)))
507 return;
509 printk("%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
510 drive->name,
511 sense->error_code, sense->sense_key,
512 sense->asc, sense->ascq);
513 #endif /* not VERBOSE_IDE_CD_ERRORS */
517 * Initialize a ide-cd packet command request
519 static void cdrom_prepare_request(struct request *rq)
521 ide_init_drive_cmd(rq);
522 rq->flags = REQ_PC;
525 static void cdrom_queue_request_sense(ide_drive_t *drive,
526 struct completion *wait,
527 void *sense,
528 struct request *failed_command)
530 struct cdrom_info *info = drive->driver_data;
531 struct request *rq = &info->request_sense_request;
533 if (sense == NULL)
534 sense = &info->sense_data;
536 /* stuff the sense request in front of our current request */
537 cdrom_prepare_request(rq);
539 rq->data = sense;
540 rq->cmd[0] = GPCMD_REQUEST_SENSE;
541 rq->cmd[4] = rq->data_len = 18;
543 rq->flags = REQ_SENSE;
544 rq->waiting = wait;
546 /* NOTE! Save the failed command in "rq->buffer" */
547 rq->buffer = (void *) failed_command;
549 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
554 * Error reporting, in human readable form (luxurious, but a memory hog).
556 byte ide_cdrom_dump_status (ide_drive_t *drive, const char *msg, byte stat)
558 unsigned long flags;
560 atapi_status_t status;
561 atapi_error_t error;
563 status.all = stat;
564 local_irq_set(flags);
565 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
566 #if FANCY_STATUS_DUMPS
567 printk(" { ");
568 if (status.b.bsy)
569 printk("Busy ");
570 else {
571 if (status.b.drdy) printk("DriveReady ");
572 if (status.b.df) printk("DeviceFault ");
573 if (status.b.dsc) printk("SeekComplete ");
574 if (status.b.drq) printk("DataRequest ");
575 if (status.b.corr) printk("CorrectedError ");
576 if (status.b.idx) printk("Index ");
577 if (status.b.check) printk("Error ");
579 printk("}");
580 #endif /* FANCY_STATUS_DUMPS */
581 printk("\n");
582 if ((status.all & (status.b.bsy|status.b.check)) == status.b.check) {
583 error.all = HWIF(drive)->INB(IDE_ERROR_REG);
584 printk("%s: %s: error=0x%02x", drive->name, msg, error.all);
585 #if FANCY_STATUS_DUMPS
586 if (error.b.ili) printk("IllegalLengthIndication ");
587 if (error.b.eom) printk("EndOfMedia ");
588 if (error.b.abrt) printk("Aborted Command ");
589 if (error.b.mcr) printk("MediaChangeRequested ");
590 if (error.b.sense_key) printk("LastFailedSense 0x%02x ",
591 error.b.sense_key);
592 #endif /* FANCY_STATUS_DUMPS */
593 printk("\n");
595 local_irq_restore(flags);
596 return error.all;
600 * ide_error() takes action based on the error returned by the drive.
602 ide_startstop_t ide_cdrom_error (ide_drive_t *drive, const char *msg, byte stat)
604 struct request *rq;
605 byte err;
607 err = ide_cdrom_dump_status(drive, msg, stat);
608 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
609 return ide_stopped;
610 /* retry only "normal" I/O: */
611 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
612 rq->errors = 1;
613 ide_end_drive_cmd(drive, stat, err);
614 return ide_stopped;
617 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
618 /* other bits are useless when BUSY */
619 rq->errors |= ERROR_RESET;
620 } else {
621 /* add decoding error stuff */
623 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
624 /* force an abort */
625 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
626 if (rq->errors >= ERROR_MAX) {
627 DRIVER(drive)->end_request(drive, 0, 0);
628 } else {
629 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
630 ++rq->errors;
631 return ide_do_reset(drive);
633 ++rq->errors;
635 return ide_stopped;
638 ide_startstop_t ide_cdrom_abort (ide_drive_t *drive, const char *msg)
640 struct request *rq;
642 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
643 return ide_stopped;
644 /* retry only "normal" I/O: */
645 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
646 rq->errors = 1;
647 ide_end_drive_cmd(drive, BUSY_STAT, 0);
648 return ide_stopped;
650 rq->errors |= ERROR_RESET;
651 DRIVER(drive)->end_request(drive, 0, 0);
652 return ide_stopped;
655 static void cdrom_end_request (ide_drive_t *drive, int uptodate)
657 struct request *rq = HWGROUP(drive)->rq;
658 int nsectors = rq->hard_cur_sectors;
660 if ((rq->flags & REQ_SENSE) && uptodate) {
662 * For REQ_SENSE, "rq->buffer" points to the original failed
663 * request
665 struct request *failed = (struct request *) rq->buffer;
666 struct cdrom_info *info = drive->driver_data;
667 void *sense = &info->sense_data;
669 if (failed && failed->sense) {
670 sense = failed->sense;
671 failed->sense_len = rq->sense_len;
674 cdrom_analyze_sense_data(drive, failed, sense);
677 if (!rq->current_nr_sectors && blk_fs_request(rq))
678 uptodate = 1;
679 if (!nsectors)
680 nsectors = 1;
682 ide_end_request(drive, uptodate, nsectors);
685 /* Returns 0 if the request should be continued.
686 Returns 1 if the request was ended. */
687 static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
689 struct request *rq = HWGROUP(drive)->rq;
690 int stat, err, sense_key;
692 /* Check for errors. */
693 stat = HWIF(drive)->INB(IDE_STATUS_REG);
694 if (stat_ret)
695 *stat_ret = stat;
697 if (OK_STAT(stat, good_stat, BAD_R_STAT))
698 return 0;
700 /* Get the IDE error register. */
701 err = HWIF(drive)->INB(IDE_ERROR_REG);
702 sense_key = err >> 4;
704 if (rq == NULL) {
705 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
706 return 1;
709 if (rq->flags & REQ_SENSE) {
710 /* We got an error trying to get sense info
711 from the drive (probably while trying
712 to recover from a former error). Just give up. */
714 rq->flags |= REQ_FAILED;
715 cdrom_end_request(drive, 0);
716 DRIVER(drive)->error(drive, "request sense failure", stat);
717 return 1;
719 } else if (rq->flags & (REQ_PC | REQ_BLOCK_PC)) {
720 /* All other functions, except for READ. */
721 struct completion *wait = NULL;
724 * if we have an error, pass back CHECK_CONDITION as the
725 * scsi status byte
727 if ((rq->flags & REQ_BLOCK_PC) && !rq->errors)
728 rq->errors = SAM_STAT_CHECK_CONDITION;
730 /* Check for tray open. */
731 if (sense_key == NOT_READY) {
732 cdrom_saw_media_change (drive);
733 } else if (sense_key == UNIT_ATTENTION) {
734 /* Check for media change. */
735 cdrom_saw_media_change (drive);
736 /*printk("%s: media changed\n",drive->name);*/
737 return 0;
738 } else if (!(rq->flags & REQ_QUIET)) {
739 /* Otherwise, print an error. */
740 ide_dump_status(drive, "packet command error", stat);
743 /* Set the error flag and complete the request.
744 Then, if we have a CHECK CONDITION status,
745 queue a request sense command. We must be careful,
746 though: we don't want the thread in
747 cdrom_queue_packet_command to wake up until
748 the request sense has completed. We do this
749 by transferring the semaphore from the packet
750 command request to the request sense request. */
752 if ((stat & ERR_STAT) != 0) {
753 wait = rq->waiting;
754 rq->waiting = NULL;
757 rq->flags |= REQ_FAILED;
758 cdrom_end_request(drive, 0);
760 if ((stat & ERR_STAT) != 0)
761 cdrom_queue_request_sense(drive, wait, rq->sense, rq);
762 } else if (blk_fs_request(rq)) {
763 /* Handle errors from READ and WRITE requests. */
765 if (sense_key == NOT_READY) {
766 /* Tray open. */
767 cdrom_saw_media_change (drive);
769 /* Fail the request. */
770 printk ("%s: tray open\n", drive->name);
771 cdrom_end_request(drive, 0);
772 } else if (sense_key == UNIT_ATTENTION) {
773 /* Media change. */
774 cdrom_saw_media_change (drive);
776 /* Arrange to retry the request.
777 But be sure to give up if we've retried
778 too many times. */
779 if (++rq->errors > ERROR_MAX)
780 cdrom_end_request(drive, 0);
781 } else if (sense_key == ILLEGAL_REQUEST ||
782 sense_key == DATA_PROTECT) {
783 /* No point in retrying after an illegal
784 request or data protect error.*/
785 ide_dump_status (drive, "command error", stat);
786 cdrom_end_request(drive, 0);
787 } else if ((err & ~ABRT_ERR) != 0) {
788 /* Go to the default handler
789 for other errors. */
790 DRIVER(drive)->error(drive, "cdrom_decode_status",stat);
791 return 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 cdrom_end_request(drive, 0);
797 } else if ((++rq->errors > ERROR_MAX)) {
798 /* We've racked up too many retries. Abort. */
799 cdrom_end_request(drive, 0);
802 /* If we got a CHECK_CONDITION status,
803 queue a request sense command. */
804 if ((stat & ERR_STAT) != 0)
805 cdrom_queue_request_sense(drive, NULL, NULL, NULL);
806 } else {
807 blk_dump_rq_flags(rq, "ide-cd: bad rq");
808 cdrom_end_request(drive, 0);
811 /* Retry, or handle the next request. */
812 return 1;
815 static int cdrom_timer_expiry(ide_drive_t *drive)
817 struct request *rq = HWGROUP(drive)->rq;
818 unsigned long wait = 0;
821 * Some commands are *slow* and normally take a long time to
822 * complete. Usually we can use the ATAPI "disconnect" to bypass
823 * this, but not all commands/drives support that. Let
824 * ide_timer_expiry keep polling us for these.
826 switch (rq->cmd[0]) {
827 case GPCMD_BLANK:
828 case GPCMD_FORMAT_UNIT:
829 case GPCMD_RESERVE_RZONE_TRACK:
830 wait = WAIT_CMD;
831 break;
832 default:
833 wait = 0;
834 break;
836 return wait;
839 /* Set up the device registers for transferring a packet command on DEV,
840 expecting to later transfer XFERLEN bytes. HANDLER is the routine
841 which actually transfers the command to the drive. If this is a
842 drq_interrupt device, this routine will arrange for HANDLER to be
843 called when the interrupt from the drive arrives. Otherwise, HANDLER
844 will be called immediately after the drive is prepared for the transfer. */
846 static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
847 int xferlen,
848 ide_handler_t *handler)
850 ide_startstop_t startstop;
851 struct cdrom_info *info = drive->driver_data;
853 /* Wait for the controller to be idle. */
854 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
855 return startstop;
857 if (info->dma) {
858 if (info->cmd == READ) {
859 info->dma = !HWIF(drive)->ide_dma_read(drive);
860 } else if (info->cmd == WRITE) {
861 info->dma = !HWIF(drive)->ide_dma_write(drive);
862 } else {
863 printk("ide-cd: DMA set, but not allowed\n");
867 /* Set up the controller registers. */
868 /* FIXME: for Virtual DMA we must check harder */
869 HWIF(drive)->OUTB(info->dma, IDE_FEATURE_REG);
870 HWIF(drive)->OUTB(0, IDE_IREASON_REG);
871 HWIF(drive)->OUTB(0, IDE_SECTOR_REG);
873 HWIF(drive)->OUTB(xferlen & 0xff, IDE_BCOUNTL_REG);
874 HWIF(drive)->OUTB(xferlen >> 8 , IDE_BCOUNTH_REG);
875 if (IDE_CONTROL_REG)
876 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
878 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
879 /* packet command */
880 ide_execute_command(drive, WIN_PACKETCMD, handler, WAIT_CMD, cdrom_timer_expiry);
881 return ide_started;
882 } else {
883 /* packet command */
884 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
885 return (*handler) (drive);
889 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
890 The device registers must have already been prepared
891 by cdrom_start_packet_command.
892 HANDLER is the interrupt handler to call when the command completes
893 or there's data ready. */
895 * changed 5 parameters to 3 for dvd-ram
896 * struct packet_command *pc; now packet_command_t *pc;
898 #define ATAPI_MIN_CDB_BYTES 12
899 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
900 struct request *rq,
901 ide_handler_t *handler)
903 int cmd_len;
904 struct cdrom_info *info = drive->driver_data;
905 ide_startstop_t startstop;
907 if (CDROM_CONFIG_FLAGS(drive)->drq_interrupt) {
908 /* Here we should have been called after receiving an interrupt
909 from the device. DRQ should how be set. */
911 /* Check for errors. */
912 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
913 return ide_stopped;
914 } else {
915 /* Otherwise, we must wait for DRQ to get set. */
916 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
917 BUSY_STAT, WAIT_READY))
918 return startstop;
921 /* Arm the interrupt handler. */
922 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
924 /* ATAPI commands get padded out to 12 bytes minimum */
925 cmd_len = COMMAND_SIZE(rq->cmd[0]);
926 if (cmd_len < ATAPI_MIN_CDB_BYTES)
927 cmd_len = ATAPI_MIN_CDB_BYTES;
929 /* Send the command to the device. */
930 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
932 /* Start the DMA if need be */
933 if (info->dma)
934 (void) HWIF(drive)->ide_dma_begin(drive);
936 return ide_started;
939 /****************************************************************************
940 * Block read functions.
944 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
945 * buffer. Once the first sector is added, any subsequent sectors are
946 * assumed to be continuous (until the buffer is cleared). For the first
947 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
948 * the buffer is cleared.)
950 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
951 int sectors_to_transfer)
953 struct cdrom_info *info = drive->driver_data;
955 /* Number of sectors to read into the buffer. */
956 int sectors_to_buffer = MIN (sectors_to_transfer,
957 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
958 info->nsectors_buffered);
960 char *dest;
962 /* If we couldn't get a buffer, don't try to buffer anything... */
963 if (info->buffer == NULL)
964 sectors_to_buffer = 0;
966 /* If this is the first sector in the buffer, remember its number. */
967 if (info->nsectors_buffered == 0)
968 info->sector_buffered = sector;
970 /* Read the data into the buffer. */
971 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
972 while (sectors_to_buffer > 0) {
973 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
974 --sectors_to_buffer;
975 --sectors_to_transfer;
976 ++info->nsectors_buffered;
977 dest += SECTOR_SIZE;
980 /* Throw away any remaining data. */
981 while (sectors_to_transfer > 0) {
982 char dum[SECTOR_SIZE];
983 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
984 --sectors_to_transfer;
989 * Check the contents of the interrupt reason register from the cdrom
990 * and attempt to recover if there are problems. Returns 0 if everything's
991 * ok; nonzero if the request has been terminated.
993 static inline
994 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
996 if (ireason == 2)
997 return 0;
998 else if (ireason == 0) {
999 /* Whoops... The drive is expecting to receive data from us! */
1000 printk("%s: read_intr: Drive wants to transfer data the "
1001 "wrong way!\n", drive->name);
1003 /* Throw some data at the drive so it doesn't hang
1004 and quit this request. */
1005 while (len > 0) {
1006 int dum = 0;
1007 HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof (dum));
1008 len -= sizeof (dum);
1010 } else if (ireason == 1) {
1011 /* Some drives (ASUS) seem to tell us that status
1012 * info is available. just get it and ignore.
1014 (void) HWIF(drive)->INB(IDE_STATUS_REG);
1015 return 0;
1016 } else {
1017 /* Drive wants a command packet, or invalid ireason... */
1018 printk("%s: read_intr: bad interrupt reason %x\n", drive->name,
1019 ireason);
1022 cdrom_end_request(drive, 0);
1023 return -1;
1027 * Interrupt routine. Called when a read request has completed.
1029 static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
1031 int stat;
1032 int ireason, len, sectors_to_transfer, nskip;
1033 struct cdrom_info *info = drive->driver_data;
1034 u8 lowcyl = 0, highcyl = 0;
1035 int dma = info->dma, dma_error = 0;
1037 struct request *rq = HWGROUP(drive)->rq;
1040 * handle dma case
1042 if (dma) {
1043 info->dma = 0;
1044 if ((dma_error = HWIF(drive)->ide_dma_end(drive)))
1045 HWIF(drive)->ide_dma_off(drive);
1048 if (cdrom_decode_status(drive, 0, &stat))
1049 return ide_stopped;
1051 if (dma) {
1052 if (!dma_error) {
1053 ide_end_request(drive, 1, rq->nr_sectors);
1054 return ide_stopped;
1055 } else
1056 return DRIVER(drive)->error(drive, "dma error", stat);
1059 /* Read the interrupt reason and the transfer length. */
1060 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1061 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1062 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1064 len = lowcyl + (256 * highcyl);
1066 /* If DRQ is clear, the command has completed. */
1067 if ((stat & DRQ_STAT) == 0) {
1068 /* If we're not done filling the current buffer, complain.
1069 Otherwise, complete the command normally. */
1070 if (rq->current_nr_sectors > 0) {
1071 printk ("%s: cdrom_read_intr: data underrun (%d blocks)\n",
1072 drive->name, rq->current_nr_sectors);
1073 rq->flags |= REQ_FAILED;
1074 cdrom_end_request(drive, 0);
1075 } else
1076 cdrom_end_request(drive, 1);
1077 return ide_stopped;
1080 /* Check that the drive is expecting to do the same thing we are. */
1081 if (cdrom_read_check_ireason (drive, len, ireason))
1082 return ide_stopped;
1084 /* Assume that the drive will always provide data in multiples
1085 of at least SECTOR_SIZE, as it gets hairy to keep track
1086 of the transfers otherwise. */
1087 if ((len % SECTOR_SIZE) != 0) {
1088 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
1089 drive->name, len);
1090 if (CDROM_CONFIG_FLAGS(drive)->limit_nframes)
1091 printk (" This drive is not supported by this version of the driver\n");
1092 else {
1093 printk (" Trying to limit transfer sizes\n");
1094 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
1096 cdrom_end_request(drive, 0);
1097 return ide_stopped;
1100 /* The number of sectors we need to read from the drive. */
1101 sectors_to_transfer = len / SECTOR_SIZE;
1103 /* First, figure out if we need to bit-bucket
1104 any of the leading sectors. */
1105 nskip = MIN((int)(rq->current_nr_sectors - bio_cur_sectors(rq->bio)), sectors_to_transfer);
1107 while (nskip > 0) {
1108 /* We need to throw away a sector. */
1109 char dum[SECTOR_SIZE];
1110 HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
1112 --rq->current_nr_sectors;
1113 --nskip;
1114 --sectors_to_transfer;
1117 /* Now loop while we still have data to read from the drive. */
1118 while (sectors_to_transfer > 0) {
1119 int this_transfer;
1121 /* If we've filled the present buffer but there's another
1122 chained buffer after it, move on. */
1123 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1124 cdrom_end_request(drive, 1);
1126 /* If the buffers are full, cache the rest of the data in our
1127 internal buffer. */
1128 if (rq->current_nr_sectors == 0) {
1129 cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
1130 sectors_to_transfer = 0;
1131 } else {
1132 /* Transfer data to the buffers.
1133 Figure out how many sectors we can transfer
1134 to the current buffer. */
1135 this_transfer = MIN (sectors_to_transfer,
1136 rq->current_nr_sectors);
1138 /* Read this_transfer sectors
1139 into the current buffer. */
1140 while (this_transfer > 0) {
1141 HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
1142 rq->buffer += SECTOR_SIZE;
1143 --rq->nr_sectors;
1144 --rq->current_nr_sectors;
1145 ++rq->sector;
1146 --this_transfer;
1147 --sectors_to_transfer;
1152 /* Done moving data! Wait for another interrupt. */
1153 ide_set_handler(drive, &cdrom_read_intr, WAIT_CMD, NULL);
1154 return ide_started;
1158 * Try to satisfy some of the current read request from our cached data.
1159 * Returns nonzero if the request has been completed, zero otherwise.
1161 static int cdrom_read_from_buffer (ide_drive_t *drive)
1163 struct cdrom_info *info = drive->driver_data;
1164 struct request *rq = HWGROUP(drive)->rq;
1166 /* Can't do anything if there's no buffer. */
1167 if (info->buffer == NULL) return 0;
1169 /* Loop while this request needs data and the next block is present
1170 in our cache. */
1171 while (rq->nr_sectors > 0 &&
1172 rq->sector >= info->sector_buffered &&
1173 rq->sector < info->sector_buffered + info->nsectors_buffered) {
1174 if (rq->current_nr_sectors == 0)
1175 cdrom_end_request(drive, 1);
1177 memcpy (rq->buffer,
1178 info->buffer +
1179 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1180 SECTOR_SIZE);
1181 rq->buffer += SECTOR_SIZE;
1182 --rq->current_nr_sectors;
1183 --rq->nr_sectors;
1184 ++rq->sector;
1187 /* If we've satisfied the current request,
1188 terminate it successfully. */
1189 if (rq->nr_sectors == 0) {
1190 cdrom_end_request(drive, 1);
1191 return -1;
1194 /* Move on to the next buffer if needed. */
1195 if (rq->current_nr_sectors == 0)
1196 cdrom_end_request(drive, 1);
1198 /* If this condition does not hold, then the kluge i use to
1199 represent the number of sectors to skip at the start of a transfer
1200 will fail. I think that this will never happen, but let's be
1201 paranoid and check. */
1202 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
1203 (rq->sector % SECTORS_PER_FRAME) != 0) {
1204 printk("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1205 drive->name, (long)rq->sector);
1206 cdrom_end_request(drive, 0);
1207 return -1;
1210 return 0;
1214 * Routine to send a read packet command to the drive.
1215 * This is usually called directly from cdrom_start_read.
1216 * However, for drq_interrupt devices, it is called from an interrupt
1217 * when the drive is ready to accept the command.
1219 static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1221 struct request *rq = HWGROUP(drive)->rq;
1222 int nsect, sector, nframes, frame, nskip;
1224 /* Number of sectors to transfer. */
1225 nsect = rq->nr_sectors;
1227 /* Starting sector. */
1228 sector = rq->sector;
1230 /* If the requested sector doesn't start on a cdrom block boundary,
1231 we must adjust the start of the transfer so that it does,
1232 and remember to skip the first few sectors.
1233 If the CURRENT_NR_SECTORS field is larger than the size
1234 of the buffer, it will mean that we're to skip a number
1235 of sectors equal to the amount by which CURRENT_NR_SECTORS
1236 is larger than the buffer size. */
1237 nskip = (sector % SECTORS_PER_FRAME);
1238 if (nskip > 0) {
1239 /* Sanity check... */
1240 if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
1241 (rq->sector % CD_FRAMESIZE != 0)) {
1242 printk ("%s: cdrom_start_read_continuation: buffer botch (%u)\n",
1243 drive->name, rq->current_nr_sectors);
1244 cdrom_end_request(drive, 0);
1245 return ide_stopped;
1247 sector -= nskip;
1248 nsect += nskip;
1249 rq->current_nr_sectors += nskip;
1252 /* Convert from sectors to cdrom blocks, rounding up the transfer
1253 length if needed. */
1254 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1255 frame = sector / SECTORS_PER_FRAME;
1257 /* Largest number of frames was can transfer at once is 64k-1. For
1258 some drives we need to limit this even more. */
1259 nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ?
1260 (65534 / CD_FRAMESIZE) : 65535);
1262 /* Set up the command */
1263 rq->timeout = WAIT_CMD;
1265 /* Send the command to the drive and return. */
1266 return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
1270 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1271 #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
1272 #define IDECD_SEEK_TIMEOUT WAIT_CMD /* 10 sec */
1274 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1276 struct cdrom_info *info = drive->driver_data;
1277 int stat;
1278 static int retry = 10;
1280 if (cdrom_decode_status(drive, 0, &stat))
1281 return ide_stopped;
1282 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1284 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
1285 if (--retry == 0) {
1287 * this condition is far too common, to bother
1288 * users about it
1290 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
1291 drive->dsc_overlap = 0;
1294 return ide_stopped;
1297 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1299 struct request *rq = HWGROUP(drive)->rq;
1300 int sector, frame, nskip;
1302 sector = rq->sector;
1303 nskip = (sector % SECTORS_PER_FRAME);
1304 if (nskip > 0)
1305 sector -= nskip;
1306 frame = sector / SECTORS_PER_FRAME;
1308 memset(rq->cmd, 0, sizeof(rq->cmd));
1309 rq->cmd[0] = GPCMD_SEEK;
1310 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
1312 rq->timeout = WAIT_CMD;
1313 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
1316 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1318 struct cdrom_info *info = drive->driver_data;
1320 info->dma = 0;
1321 info->cmd = 0;
1322 info->start_seek = jiffies;
1323 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
1326 /* Fix up a possibly partially-processed request so that we can
1327 start it over entirely, or even put it back on the request queue. */
1328 static void restore_request (struct request *rq)
1330 if (rq->buffer != bio_data(rq->bio)) {
1331 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
1333 rq->buffer = bio_data(rq->bio);
1334 rq->nr_sectors += n;
1335 rq->sector -= n;
1337 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
1338 rq->hard_nr_sectors = rq->nr_sectors;
1339 rq->hard_sector = rq->sector;
1340 rq->q->prep_rq_fn(rq->q, rq);
1344 * Start a read request from the CD-ROM.
1346 static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1348 struct cdrom_info *info = drive->driver_data;
1349 struct request *rq = HWGROUP(drive)->rq;
1351 /* We may be retrying this request after an error. Fix up
1352 any weirdness which might be present in the request packet. */
1353 restore_request(rq);
1355 /* Satisfy whatever we can of this request from our cached sector. */
1356 if (cdrom_read_from_buffer(drive))
1357 return ide_stopped;
1359 blk_attempt_remerge(&drive->queue, rq);
1361 /* Clear the local sector buffer. */
1362 info->nsectors_buffered = 0;
1364 /* use dma, if possible. */
1365 if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) &&
1366 (rq->nr_sectors % SECTORS_PER_FRAME == 0))
1367 info->dma = 1;
1368 else
1369 info->dma = 0;
1371 info->cmd = READ;
1373 /* Start sending the read request to the drive. */
1374 return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1377 /****************************************************************************
1378 * Execute all other packet commands.
1381 /* Interrupt routine for packet command completion. */
1382 static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1384 int ireason, len, thislen;
1385 struct request *rq = HWGROUP(drive)->rq;
1386 u8 lowcyl = 0, highcyl = 0;
1387 int stat;
1389 /* Check for errors. */
1390 if (cdrom_decode_status(drive, 0, &stat))
1391 return ide_stopped;
1393 /* Read the interrupt reason and the transfer length. */
1394 ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1395 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1396 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1398 len = lowcyl + (256 * highcyl);
1400 /* If DRQ is clear, the command has completed.
1401 Complain if we still have data left to transfer. */
1402 if ((stat & DRQ_STAT) == 0) {
1403 /* Some of the trailing request sense fields are optional, and
1404 some drives don't send them. Sigh. */
1405 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
1406 rq->data_len > 0 &&
1407 rq->data_len <= 5) {
1408 while (rq->data_len > 0) {
1409 *(unsigned char *)rq->data++ = 0;
1410 --rq->data_len;
1414 if (rq->data_len == 0)
1415 cdrom_end_request(drive, 1);
1416 else {
1417 /* Comment this out, because this always happens
1418 right after a reset occurs, and it is annoying to
1419 always print expected stuff. */
1421 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1422 drive->name, pc->buflen);
1424 rq->flags |= REQ_FAILED;
1425 cdrom_end_request(drive, 0);
1427 return ide_stopped;
1430 /* Figure out how much data to transfer. */
1431 thislen = rq->data_len;
1432 if (thislen > len) thislen = len;
1434 /* The drive wants to be written to. */
1435 if ((ireason & 3) == 0) {
1436 if (!rq->data) {
1437 blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1438 goto confused;
1440 /* Transfer the data. */
1441 HWIF(drive)->atapi_output_bytes(drive, rq->data, thislen);
1443 /* If we haven't moved enough data to satisfy the drive,
1444 add some padding. */
1445 while (len > thislen) {
1446 int dum = 0;
1447 HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof(dum));
1448 len -= sizeof(dum);
1451 /* Keep count of how much data we've moved. */
1452 rq->data += thislen;
1453 rq->data_len -= thislen;
1456 /* Same drill for reading. */
1457 else if ((ireason & 3) == 2) {
1458 if (!rq->data) {
1459 blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1460 goto confused;
1462 /* Transfer the data. */
1463 HWIF(drive)->atapi_input_bytes(drive, rq->data, thislen);
1465 /* If we haven't moved enough data to satisfy the drive,
1466 add some padding. */
1467 while (len > thislen) {
1468 int dum = 0;
1469 HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1470 len -= sizeof(dum);
1473 /* Keep count of how much data we've moved. */
1474 rq->data += thislen;
1475 rq->data_len -= thislen;
1477 if (rq->flags & REQ_SENSE)
1478 rq->sense_len += thislen;
1479 } else {
1480 confused:
1481 printk ("%s: cdrom_pc_intr: The drive "
1482 "appears confused (ireason = 0x%02x)\n",
1483 drive->name, ireason);
1484 rq->flags |= REQ_FAILED;
1487 /* Now we wait for another interrupt. */
1488 ide_set_handler(drive, &cdrom_pc_intr, WAIT_CMD, cdrom_timer_expiry);
1489 return ide_started;
1492 static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1494 struct request *rq = HWGROUP(drive)->rq;
1496 if (!rq->timeout)
1497 rq->timeout = WAIT_CMD;
1499 /* Send the command to the drive and return. */
1500 return cdrom_transfer_packet_command(drive, rq, &cdrom_pc_intr);
1504 static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1506 int len;
1507 struct request *rq = HWGROUP(drive)->rq;
1508 struct cdrom_info *info = drive->driver_data;
1510 info->dma = 0;
1511 info->cmd = 0;
1512 rq->flags &= ~REQ_FAILED;
1513 len = rq->data_len;
1515 /* Start sending the command to the drive. */
1516 return cdrom_start_packet_command(drive, len, cdrom_do_pc_continuation);
1520 /* Sleep for TIME jiffies.
1521 Not to be called from an interrupt handler. */
1522 static
1523 void cdrom_sleep (int time)
1525 int sleep = time;
1527 do {
1528 set_current_state(TASK_INTERRUPTIBLE);
1529 sleep = schedule_timeout(sleep);
1530 } while (sleep);
1533 static
1534 int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
1536 struct request_sense sense;
1537 int retries = 10;
1538 unsigned int flags = rq->flags;
1540 if (rq->sense == NULL)
1541 rq->sense = &sense;
1543 /* Start of retry loop. */
1544 do {
1545 int error;
1546 unsigned long time = jiffies;
1547 rq->flags = flags;
1549 error = ide_do_drive_cmd(drive, rq, ide_wait);
1550 time = jiffies - time;
1552 /* FIXME: we should probably abort/retry or something
1553 * in case of failure */
1554 if (rq->flags & REQ_FAILED) {
1555 /* The request failed. Retry if it was due to a unit
1556 attention status
1557 (usually means media was changed). */
1558 struct request_sense *reqbuf = rq->sense;
1560 if (reqbuf->sense_key == UNIT_ATTENTION)
1561 cdrom_saw_media_change(drive);
1562 else if (reqbuf->sense_key == NOT_READY &&
1563 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1564 /* The drive is in the process of loading
1565 a disk. Retry, but wait a little to give
1566 the drive time to complete the load. */
1567 cdrom_sleep(2 * HZ);
1568 } else {
1569 /* Otherwise, don't retry. */
1570 retries = 0;
1572 --retries;
1575 /* End of retry loop. */
1576 } while ((rq->flags & REQ_FAILED) && retries >= 0);
1578 /* Return an error if the command failed. */
1579 return (rq->flags & REQ_FAILED) ? -EIO : 0;
1583 * Write handling
1585 static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1587 /* Two notes about IDE interrupt reason here - 0 means that
1588 * the drive wants to receive data from us, 2 means that
1589 * the drive is expecting to transfer data to us.
1591 if (ireason == 0)
1592 return 0;
1593 else if (ireason == 2) {
1594 /* Whoops... The drive wants to send data. */
1595 printk("%s: write_intr: wrong transfer direction!\n",
1596 drive->name);
1598 while (len > 0) {
1599 int dum = 0;
1600 HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1601 len -= sizeof(dum);
1603 } else {
1604 /* Drive wants a command packet, or invalid ireason... */
1605 printk("%s: write_intr: bad interrupt reason %x\n",
1606 drive->name, ireason);
1609 cdrom_end_request(drive, 0);
1610 return 1;
1613 static void post_transform_command(struct request *req)
1615 u8 *c = req->cmd;
1616 char *ibuf;
1618 if (!blk_pc_request(req))
1619 return;
1621 if (req->bio)
1622 ibuf = bio_data(req->bio);
1623 else
1624 ibuf = req->data;
1626 if (!ibuf)
1627 return;
1630 * set ansi-revision and response data as atapi
1632 if (c[0] == GPCMD_INQUIRY) {
1633 ibuf[2] |= 2;
1634 ibuf[3] = (ibuf[3] & 0xf0) | 2;
1638 typedef void (xfer_func_t)(ide_drive_t *, void *, u32);
1641 * best way to deal with dma that is not sector aligned right now... note
1642 * that in this path we are not using ->data or ->buffer at all. this irs
1643 * can replace cdrom_pc_intr, cdrom_read_intr, and cdrom_write_intr in the
1644 * future.
1646 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1648 struct cdrom_info *info = drive->driver_data;
1649 struct request *rq = HWGROUP(drive)->rq;
1650 int dma_error, dma, stat, ireason, len, thislen;
1651 u8 lowcyl, highcyl;
1652 xfer_func_t *xferfunc;
1653 unsigned long flags;
1655 /* Check for errors. */
1656 dma_error = 0;
1657 dma = info->dma;
1658 if (dma) {
1659 info->dma = 0;
1660 if ((dma_error = HWIF(drive)->ide_dma_end(drive))) {
1661 printk("ide-cd: dma error\n");
1662 HWIF(drive)->ide_dma_off(drive);
1666 if (cdrom_decode_status(drive, 0, &stat)) {
1667 end_that_request_chunk(rq, 1, rq->data_len);
1668 return ide_stopped;
1672 * using dma, transfer is complete now
1674 if (dma) {
1675 if (dma_error)
1676 return DRIVER(drive)->error(drive, "dma error", stat);
1678 end_that_request_chunk(rq, 1, rq->data_len);
1679 rq->data_len = 0;
1680 goto end_request;
1684 * ok we fall to pio :/
1686 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1687 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1688 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1690 len = lowcyl + (256 * highcyl);
1691 thislen = rq->data_len;
1692 if (thislen > len)
1693 thislen = len;
1696 * If DRQ is clear, the command has completed.
1698 if ((stat & DRQ_STAT) == 0) {
1699 if (rq->data_len)
1700 printk("%s: %u residual after xfer\n", __FUNCTION__, rq->data_len);
1701 goto end_request;
1705 * check which way to transfer data
1707 if (rq_data_dir(rq) == WRITE) {
1709 * write to drive
1711 if (cdrom_write_check_ireason(drive, len, ireason))
1712 return ide_stopped;
1714 xferfunc = HWIF(drive)->atapi_output_bytes;
1715 } else {
1717 * read from drive
1719 if (cdrom_read_check_ireason(drive, len, ireason))
1720 return ide_stopped;
1722 xferfunc = HWIF(drive)->atapi_input_bytes;
1726 * transfer data
1728 while (thislen > 0) {
1729 int blen = blen = rq->data_len;
1730 char *ptr = rq->data;
1733 * bio backed?
1735 if (rq->bio) {
1736 ptr = bio_data(rq->bio);
1737 blen = bio_iovec(rq->bio)->bv_len;
1740 if (!ptr) {
1741 printk("%s: confused, missing data\n", drive->name);
1742 break;
1745 if (blen > thislen)
1746 blen = thislen;
1748 xferfunc(drive, ptr, blen);
1750 thislen -= blen;
1751 len -= blen;
1752 rq->data_len -= blen;
1754 if (rq->bio)
1755 end_that_request_chunk(rq, 1, blen);
1756 else
1757 rq->data += blen;
1761 * pad, if necessary
1763 if (len > 0) {
1764 while (len > 0) {
1765 int pad = 0;
1767 xferfunc(drive, &pad, sizeof(pad));
1768 len -= sizeof(pad);
1772 if (HWGROUP(drive)->handler != NULL)
1773 BUG();
1775 ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, NULL);
1776 return ide_started;
1778 end_request:
1779 if (!rq->data_len)
1780 post_transform_command(rq);
1782 spin_lock_irqsave(&ide_lock, flags);
1783 blkdev_dequeue_request(rq);
1784 end_that_request_last(rq);
1785 HWGROUP(drive)->rq = NULL;
1786 spin_unlock_irqrestore(&ide_lock, flags);
1787 return ide_stopped;
1790 static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1792 int stat, ireason, len, sectors_to_transfer, uptodate;
1793 struct cdrom_info *info = drive->driver_data;
1794 int dma_error = 0, dma = info->dma;
1795 u8 lowcyl = 0, highcyl = 0;
1797 struct request *rq = HWGROUP(drive)->rq;
1799 /* Check for errors. */
1800 if (dma) {
1801 info->dma = 0;
1802 if ((dma_error = HWIF(drive)->ide_dma_end(drive))) {
1803 printk("ide-cd: write dma error\n");
1804 HWIF(drive)->ide_dma_off(drive);
1808 if (cdrom_decode_status(drive, 0, &stat)) {
1809 printk("ide-cd: write_intr decode_status bad\n");
1810 return ide_stopped;
1814 * using dma, transfer is complete now
1816 if (dma) {
1817 if (dma_error)
1818 return DRIVER(drive)->error(drive, "dma error", stat);
1820 ide_end_request(drive, 1, rq->nr_sectors);
1821 return ide_stopped;
1824 /* Read the interrupt reason and the transfer length. */
1825 ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1826 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1827 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1829 len = lowcyl + (256 * highcyl);
1831 /* If DRQ is clear, the command has completed. */
1832 if ((stat & DRQ_STAT) == 0) {
1833 /* If we're not done writing, complain.
1834 * Otherwise, complete the command normally.
1836 uptodate = 1;
1837 if (rq->current_nr_sectors > 0) {
1838 printk("%s: write_intr: data underrun (%d blocks)\n",
1839 drive->name, rq->current_nr_sectors);
1840 uptodate = 0;
1842 cdrom_end_request(drive, uptodate);
1843 return ide_stopped;
1846 /* Check that the drive is expecting to do the same thing we are. */
1847 if (cdrom_write_check_ireason(drive, len, ireason))
1848 return ide_stopped;
1850 sectors_to_transfer = len / SECTOR_SIZE;
1853 * now loop and write out the data
1855 while (sectors_to_transfer > 0) {
1856 int this_transfer;
1858 if (!rq->current_nr_sectors) {
1859 printk("ide-cd: write_intr: oops\n");
1860 break;
1864 * Figure out how many sectors we can transfer
1866 this_transfer = MIN(sectors_to_transfer,rq->current_nr_sectors);
1868 while (this_transfer > 0) {
1869 HWIF(drive)->atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
1870 rq->buffer += SECTOR_SIZE;
1871 --rq->nr_sectors;
1872 --rq->current_nr_sectors;
1873 ++rq->sector;
1874 --this_transfer;
1875 --sectors_to_transfer;
1879 * current buffer complete, move on
1881 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1882 cdrom_end_request(drive, 1);
1885 /* re-arm handler */
1886 ide_set_handler(drive, &cdrom_write_intr, 5 * WAIT_CMD, NULL);
1887 return ide_started;
1890 static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1892 struct request *rq = HWGROUP(drive)->rq;
1894 #if 0 /* the immediate bit */
1895 rq->cmd[1] = 1 << 3;
1896 #endif
1897 rq->timeout = 2 * WAIT_CMD;
1899 return cdrom_transfer_packet_command(drive, rq, cdrom_write_intr);
1902 static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
1904 struct cdrom_info *info = drive->driver_data;
1907 * writes *must* be 2kB frame aligned
1909 if ((rq->nr_sectors & 3) || (rq->sector & 3)) {
1910 cdrom_end_request(drive, 0);
1911 return ide_stopped;
1915 * for dvd-ram and such media, it's a really big deal to get
1916 * big writes all the time. so scour the queue and attempt to
1917 * remerge requests, often the plugging will not have had time
1918 * to do this properly
1920 blk_attempt_remerge(&drive->queue, rq);
1922 info->nsectors_buffered = 0;
1924 /* use dma, if possible. we don't need to check more, since we
1925 * know that the transfer is always (at least!) 2KB aligned */
1926 info->dma = drive->using_dma ? 1 : 0;
1927 info->cmd = WRITE;
1929 /* Start sending the read request to the drive. */
1930 return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1933 static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1935 struct request *rq = HWGROUP(drive)->rq;
1937 if (!rq->timeout)
1938 rq->timeout = WAIT_CMD;
1940 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1943 static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1945 struct cdrom_info *info = drive->driver_data;
1947 rq->flags |= REQ_QUIET;
1949 info->dma = 0;
1950 info->cmd = 0;
1953 * sg request
1955 if (rq->bio) {
1956 if (rq->data_len & 3) {
1957 printk("%s: block pc not aligned, len=%d\n", drive->name, rq->data_len);
1958 cdrom_end_request(drive, 0);
1959 return ide_stopped;
1961 info->dma = drive->using_dma;
1962 info->cmd = rq_data_dir(rq);
1965 /* Start sending the command to the drive. */
1966 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1969 /****************************************************************************
1970 * cdrom driver request routine.
1972 static ide_startstop_t
1973 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1975 ide_startstop_t action;
1976 struct cdrom_info *info = drive->driver_data;
1978 if (blk_fs_request(rq)) {
1979 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1980 unsigned long elapsed = jiffies - info->start_seek;
1981 int stat = HWIF(drive)->INB(IDE_STATUS_REG);
1983 if ((stat & SEEK_STAT) != SEEK_STAT) {
1984 if (elapsed < IDECD_SEEK_TIMEOUT) {
1985 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1986 return ide_stopped;
1988 printk ("%s: DSC timeout\n", drive->name);
1990 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1992 if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
1993 action = cdrom_start_seek(drive, block);
1994 } else {
1995 if (rq_data_dir(rq) == READ)
1996 action = cdrom_start_read(drive, block);
1997 else
1998 action = cdrom_start_write(drive, rq);
2000 info->last_block = block;
2001 return action;
2002 } else if (rq->flags & (REQ_PC | REQ_SENSE)) {
2003 return cdrom_do_packet_command(drive);
2004 } else if (rq->flags & REQ_BLOCK_PC) {
2005 return cdrom_do_block_pc(drive, rq);
2006 } else if (rq->flags & REQ_SPECIAL) {
2008 * right now this can only be a reset...
2010 cdrom_end_request(drive, 1);
2011 return ide_stopped;
2014 blk_dump_rq_flags(rq, "ide-cd bad flags");
2015 cdrom_end_request(drive, 0);
2016 return ide_stopped;
2021 /****************************************************************************
2022 * Ioctl handling.
2024 * Routines which queue packet commands take as a final argument a pointer
2025 * to a request_sense struct. If execution of the command results
2026 * in an error with a CHECK CONDITION status, this structure will be filled
2027 * with the results of the subsequent request sense command. The pointer
2028 * can also be NULL, in which case no sense information is returned.
2031 #if ! STANDARD_ATAPI
2032 static inline
2033 int bin2bcd (int x)
2035 return (x%10) | ((x/10) << 4);
2039 static inline
2040 int bcd2bin (int x)
2042 return (x >> 4) * 10 + (x & 0x0f);
2045 static
2046 void msf_from_bcd (struct atapi_msf *msf)
2048 msf->minute = bcd2bin (msf->minute);
2049 msf->second = bcd2bin (msf->second);
2050 msf->frame = bcd2bin (msf->frame);
2053 #endif /* not STANDARD_ATAPI */
2056 static inline
2057 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
2059 lba += CD_MSF_OFFSET;
2060 lba &= 0xffffff; /* negative lbas use only 24 bits */
2061 *m = lba / (CD_SECS * CD_FRAMES);
2062 lba %= (CD_SECS * CD_FRAMES);
2063 *s = lba / CD_FRAMES;
2064 *f = lba % CD_FRAMES;
2068 static inline
2069 int msf_to_lba (byte m, byte s, byte f)
2071 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
2074 static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
2076 struct request req;
2077 struct cdrom_info *info = drive->driver_data;
2078 struct cdrom_device_info *cdi = &info->devinfo;
2080 cdrom_prepare_request(&req);
2082 req.sense = sense;
2083 req.cmd[0] = GPCMD_TEST_UNIT_READY;
2084 req.flags |= REQ_QUIET;
2086 #if ! STANDARD_ATAPI
2087 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
2088 switch CDs instead of supporting the LOAD_UNLOAD opcode */
2090 req.cmd[7] = cdi->sanyo_slot % 3;
2091 #endif /* not STANDARD_ATAPI */
2093 return cdrom_queue_packet_command(drive, &req);
2097 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
2098 static int
2099 cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
2101 struct request_sense my_sense;
2102 struct request req;
2103 int stat;
2105 if (sense == NULL)
2106 sense = &my_sense;
2108 /* If the drive cannot lock the door, just pretend. */
2109 if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
2110 stat = 0;
2111 } else {
2112 cdrom_prepare_request(&req);
2113 req.sense = sense;
2114 req.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
2115 req.cmd[4] = lockflag ? 1 : 0;
2116 stat = cdrom_queue_packet_command(drive, &req);
2119 /* If we got an illegal field error, the drive
2120 probably cannot lock the door. */
2121 if (stat != 0 &&
2122 sense->sense_key == ILLEGAL_REQUEST &&
2123 (sense->asc == 0x24 || sense->asc == 0x20)) {
2124 printk ("%s: door locking not supported\n",
2125 drive->name);
2126 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2127 stat = 0;
2130 /* no medium, that's alright. */
2131 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
2132 stat = 0;
2134 if (stat == 0)
2135 CDROM_STATE_FLAGS(drive)->door_locked = lockflag;
2137 return stat;
2141 /* Eject the disk if EJECTFLAG is 0.
2142 If EJECTFLAG is 1, try to reload the disk. */
2143 static int cdrom_eject(ide_drive_t *drive, int ejectflag,
2144 struct request_sense *sense)
2146 struct request req;
2148 if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
2149 return -EDRIVE_CANT_DO_THIS;
2151 /* reload fails on some drives, if the tray is locked */
2152 if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
2153 return 0;
2155 cdrom_prepare_request(&req);
2157 req.sense = sense;
2158 req.cmd[0] = GPCMD_START_STOP_UNIT;
2159 req.cmd[4] = 0x02 + (ejectflag != 0);
2160 return cdrom_queue_packet_command(drive, &req);
2163 static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
2164 struct request_sense *sense)
2166 struct {
2167 __u32 lba;
2168 __u32 blocklen;
2169 } capbuf;
2171 int stat;
2172 struct request req;
2174 cdrom_prepare_request(&req);
2176 req.sense = sense;
2177 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
2178 req.data = (char *)&capbuf;
2179 req.data_len = sizeof(capbuf);
2181 stat = cdrom_queue_packet_command(drive, &req);
2182 if (stat == 0)
2183 *capacity = 1 + be32_to_cpu(capbuf.lba);
2185 return stat;
2188 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
2189 int format, char *buf, int buflen,
2190 struct request_sense *sense)
2192 struct request req;
2194 cdrom_prepare_request(&req);
2196 req.sense = sense;
2197 req.data = buf;
2198 req.data_len = buflen;
2199 req.flags |= REQ_QUIET;
2200 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
2201 req.cmd[6] = trackno;
2202 req.cmd[7] = (buflen >> 8);
2203 req.cmd[8] = (buflen & 0xff);
2204 req.cmd[9] = (format << 6);
2206 if (msf_flag)
2207 req.cmd[1] = 2;
2209 return cdrom_queue_packet_command(drive, &req);
2213 /* Try to read the entire TOC for the disk into our internal buffer. */
2214 static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
2216 int stat, ntracks, i;
2217 struct cdrom_info *info = drive->driver_data;
2218 struct cdrom_device_info *cdi = &info->devinfo;
2219 struct atapi_toc *toc = info->toc;
2220 struct {
2221 struct atapi_toc_header hdr;
2222 struct atapi_toc_entry ent;
2223 } ms_tmp;
2225 if (toc == NULL) {
2226 /* Try to allocate space. */
2227 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
2228 GFP_KERNEL);
2229 info->toc = toc;
2230 if (toc == NULL) {
2231 printk ("%s: No cdrom TOC buffer!\n", drive->name);
2232 return -ENOMEM;
2236 /* Check to see if the existing data is still valid.
2237 If it is, just return. */
2238 (void) cdrom_check_status(drive, sense);
2240 if (CDROM_STATE_FLAGS(drive)->toc_valid)
2241 return 0;
2243 /* First read just the header, so we know how long the TOC is. */
2244 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
2245 sizeof(struct atapi_toc_header), sense);
2246 if (stat) return stat;
2248 #if ! STANDARD_ATAPI
2249 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2250 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2251 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2253 #endif /* not STANDARD_ATAPI */
2255 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2256 if (ntracks <= 0)
2257 return -EIO;
2258 if (ntracks > MAX_TRACKS)
2259 ntracks = MAX_TRACKS;
2261 /* Now read the whole schmeer. */
2262 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
2263 (char *)&toc->hdr,
2264 sizeof(struct atapi_toc_header) +
2265 (ntracks + 1) *
2266 sizeof(struct atapi_toc_entry), sense);
2268 if (stat && toc->hdr.first_track > 1) {
2269 /* Cds with CDI tracks only don't have any TOC entries,
2270 despite of this the returned values are
2271 first_track == last_track = number of CDI tracks + 1,
2272 so that this case is indistinguishable from the same
2273 layout plus an additional audio track.
2274 If we get an error for the regular case, we assume
2275 a CDI without additional audio tracks. In this case
2276 the readable TOC is empty (CDI tracks are not included)
2277 and only holds the Leadout entry. Heiko Eißfeldt */
2278 ntracks = 0;
2279 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
2280 (char *)&toc->hdr,
2281 sizeof(struct atapi_toc_header) +
2282 (ntracks + 1) *
2283 sizeof(struct atapi_toc_entry),
2284 sense);
2285 if (stat) {
2286 return stat;
2288 #if ! STANDARD_ATAPI
2289 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2290 toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
2291 toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
2292 } else
2293 #endif /* not STANDARD_ATAPI */
2295 toc->hdr.first_track = CDROM_LEADOUT;
2296 toc->hdr.last_track = CDROM_LEADOUT;
2300 if (stat)
2301 return stat;
2303 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
2305 #if ! STANDARD_ATAPI
2306 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2307 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2308 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
2310 #endif /* not STANDARD_ATAPI */
2312 for (i=0; i<=ntracks; i++) {
2313 #if ! STANDARD_ATAPI
2314 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
2315 if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd)
2316 toc->ent[i].track = bcd2bin(toc->ent[i].track);
2317 msf_from_bcd(&toc->ent[i].addr.msf);
2319 #endif /* not STANDARD_ATAPI */
2320 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
2321 toc->ent[i].addr.msf.second,
2322 toc->ent[i].addr.msf.frame);
2325 /* Read the multisession information. */
2326 if (toc->hdr.first_track != CDROM_LEADOUT) {
2327 /* Read the multisession information. */
2328 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
2329 sizeof(ms_tmp), sense);
2330 if (stat) return stat;
2331 } else {
2332 ms_tmp.ent.addr.msf.minute = 0;
2333 ms_tmp.ent.addr.msf.second = 2;
2334 ms_tmp.ent.addr.msf.frame = 0;
2335 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
2338 #if ! STANDARD_ATAPI
2339 if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd)
2340 msf_from_bcd (&ms_tmp.ent.addr.msf);
2341 #endif /* not STANDARD_ATAPI */
2343 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
2344 ms_tmp.ent.addr.msf.second,
2345 ms_tmp.ent.addr.msf.frame);
2347 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
2349 /* Now try to get the total cdrom capacity. */
2350 stat = cdrom_get_last_written(cdi, (long *) &toc->capacity);
2351 if (stat)
2352 stat = cdrom_read_capacity(drive, &toc->capacity, sense);
2353 if (stat)
2354 toc->capacity = 0x1fffff;
2356 set_capacity(drive->disk, toc->capacity * SECTORS_PER_FRAME);
2358 /* Remember that we've read this stuff. */
2359 CDROM_STATE_FLAGS(drive)->toc_valid = 1;
2361 return 0;
2365 static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2366 int buflen, struct request_sense *sense)
2368 struct request req;
2370 cdrom_prepare_request(&req);
2372 req.sense = sense;
2373 req.data = buf;
2374 req.data_len = buflen;
2375 req.cmd[0] = GPCMD_READ_SUBCHANNEL;
2376 req.cmd[1] = 2; /* MSF addressing */
2377 req.cmd[2] = 0x40; /* request subQ data */
2378 req.cmd[3] = format;
2379 req.cmd[7] = (buflen >> 8);
2380 req.cmd[8] = (buflen & 0xff);
2381 return cdrom_queue_packet_command(drive, &req);
2384 /* ATAPI cdrom drives are free to select the speed you request or any slower
2385 rate :-( Requesting too fast a speed will _not_ produce an error. */
2386 static int cdrom_select_speed(ide_drive_t *drive, int speed,
2387 struct request_sense *sense)
2389 struct request req;
2390 cdrom_prepare_request(&req);
2392 req.sense = sense;
2393 if (speed == 0)
2394 speed = 0xffff; /* set to max */
2395 else
2396 speed *= 177; /* Nx to kbytes/s */
2398 req.cmd[0] = GPCMD_SET_SPEED;
2399 /* Read Drive speed in kbytes/second MSB */
2400 req.cmd[2] = (speed >> 8) & 0xff;
2401 /* Read Drive speed in kbytes/second LSB */
2402 req.cmd[3] = speed & 0xff;
2403 if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
2404 CDROM_CONFIG_FLAGS(drive)->cd_rw ||
2405 CDROM_CONFIG_FLAGS(drive)->dvd_r) {
2406 /* Write Drive speed in kbytes/second MSB */
2407 req.cmd[4] = (speed >> 8) & 0xff;
2408 /* Write Drive speed in kbytes/second LSB */
2409 req.cmd[5] = speed & 0xff;
2412 return cdrom_queue_packet_command(drive, &req);
2415 static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2417 struct request_sense sense;
2418 struct request req;
2420 cdrom_prepare_request(&req);
2422 req.sense = &sense;
2423 req.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2424 lba_to_msf(lba_start, &req.cmd[3], &req.cmd[4], &req.cmd[5]);
2425 lba_to_msf(lba_end-1, &req.cmd[6], &req.cmd[7], &req.cmd[8]);
2427 return cdrom_queue_packet_command(drive, &req);
2430 static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2431 struct atapi_toc_entry **ent)
2433 struct cdrom_info *info = drive->driver_data;
2434 struct atapi_toc *toc = info->toc;
2435 int ntracks;
2438 * don't serve cached data, if the toc isn't valid
2440 if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2441 return -EINVAL;
2443 /* Check validity of requested track number. */
2444 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2445 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2446 if (track == CDROM_LEADOUT)
2447 *ent = &toc->ent[ntracks];
2448 else if (track < toc->hdr.first_track ||
2449 track > toc->hdr.last_track)
2450 return -EINVAL;
2451 else
2452 *ent = &toc->ent[track - toc->hdr.first_track];
2454 return 0;
2457 /* the generic packet interface to cdrom.c */
2458 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2459 struct cdrom_generic_command *cgc)
2461 struct request req;
2462 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2464 if (cgc->timeout <= 0)
2465 cgc->timeout = WAIT_CMD;
2467 /* here we queue the commands from the uniform CD-ROM
2468 layer. the packet must be complete, as we do not
2469 touch it at all. */
2470 cdrom_prepare_request(&req);
2471 memcpy(req.cmd, cgc->cmd, CDROM_PACKET_SIZE);
2472 if (cgc->sense)
2473 memset(cgc->sense, 0, sizeof(struct request_sense));
2474 req.data = cgc->buffer;
2475 req.data_len = cgc->buflen;
2476 req.timeout = cgc->timeout;
2478 if (cgc->quiet)
2479 req.flags |= REQ_QUIET;
2481 req.sense = cgc->sense;
2482 cgc->stat = cdrom_queue_packet_command(drive, &req);
2483 if (!cgc->stat)
2484 cgc->buflen = req.data_len;
2485 return cgc->stat;
2488 static
2489 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
2490 unsigned int cmd, unsigned long arg)
2492 struct cdrom_generic_command cgc;
2493 char buffer[16];
2494 int stat;
2496 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2498 /* These will be moved into the Uniform layer shortly... */
2499 switch (cmd) {
2500 case CDROMSETSPINDOWN: {
2501 char spindown;
2503 if (copy_from_user(&spindown, (void *) arg, sizeof(char)))
2504 return -EFAULT;
2506 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2507 return stat;
2509 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2511 return cdrom_mode_select(cdi, &cgc);
2514 case CDROMGETSPINDOWN: {
2515 char spindown;
2517 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2518 return stat;
2520 spindown = buffer[11] & 0x0f;
2522 if (copy_to_user((void *) arg, &spindown, sizeof (char)))
2523 return -EFAULT;
2525 return 0;
2528 default:
2529 return -EINVAL;
2534 static
2535 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2536 unsigned int cmd, void *arg)
2539 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2540 struct cdrom_info *info = drive->driver_data;
2541 int stat;
2543 switch (cmd) {
2545 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2546 * atapi doesn't support it
2548 case CDROMPLAYTRKIND: {
2549 unsigned long lba_start, lba_end;
2550 struct cdrom_ti *ti = (struct cdrom_ti *)arg;
2551 struct atapi_toc_entry *first_toc, *last_toc;
2553 stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2554 if (stat)
2555 return stat;
2557 stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2558 if (stat)
2559 return stat;
2561 if (ti->cdti_trk1 != CDROM_LEADOUT)
2562 ++last_toc;
2563 lba_start = first_toc->addr.lba;
2564 lba_end = last_toc->addr.lba;
2566 if (lba_end <= lba_start)
2567 return -EINVAL;
2569 return cdrom_play_audio(drive, lba_start, lba_end);
2572 case CDROMREADTOCHDR: {
2573 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2574 struct atapi_toc *toc;
2576 /* Make sure our saved TOC is valid. */
2577 stat = cdrom_read_toc(drive, NULL);
2578 if (stat) return stat;
2580 toc = info->toc;
2581 tochdr->cdth_trk0 = toc->hdr.first_track;
2582 tochdr->cdth_trk1 = toc->hdr.last_track;
2584 return 0;
2587 case CDROMREADTOCENTRY: {
2588 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
2589 struct atapi_toc_entry *toce;
2591 stat = cdrom_get_toc_entry(drive, tocentry->cdte_track, &toce);
2592 if (stat) return stat;
2594 tocentry->cdte_ctrl = toce->control;
2595 tocentry->cdte_adr = toce->adr;
2596 if (tocentry->cdte_format == CDROM_MSF) {
2597 lba_to_msf (toce->addr.lba,
2598 &tocentry->cdte_addr.msf.minute,
2599 &tocentry->cdte_addr.msf.second,
2600 &tocentry->cdte_addr.msf.frame);
2601 } else
2602 tocentry->cdte_addr.lba = toce->addr.lba;
2604 return 0;
2607 default:
2608 return -EINVAL;
2612 static
2613 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2615 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2616 struct request_sense sense;
2617 struct request req;
2618 int ret;
2620 cdrom_prepare_request(&req);
2621 req.flags = REQ_SPECIAL | REQ_QUIET;
2622 ret = ide_do_drive_cmd(drive, &req, ide_wait);
2625 * A reset will unlock the door. If it was previously locked,
2626 * lock it again.
2628 if (CDROM_STATE_FLAGS(drive)->door_locked)
2629 (void) cdrom_lockdoor(drive, 1, &sense);
2631 return ret;
2635 static
2636 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2638 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2639 struct request_sense sense;
2641 if (position) {
2642 int stat = cdrom_lockdoor(drive, 0, &sense);
2643 if (stat) return stat;
2646 return cdrom_eject(drive, !position, &sense);
2649 static
2650 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2652 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2653 return cdrom_lockdoor(drive, lock, NULL);
2656 static
2657 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2659 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2660 struct request_sense sense;
2661 int stat;
2663 if ((stat = cdrom_select_speed(drive, speed, &sense)) < 0)
2664 return stat;
2666 cdi->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2667 return 0;
2670 static
2671 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2673 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2675 if (slot_nr == CDSL_CURRENT) {
2676 struct request_sense sense;
2677 int stat = cdrom_check_status(drive, &sense);
2678 if (stat == 0 || sense.sense_key == UNIT_ATTENTION)
2679 return CDS_DISC_OK;
2681 if (sense.sense_key == NOT_READY && sense.asc == 0x04 &&
2682 sense.ascq == 0x04)
2683 return CDS_DISC_OK;
2687 * If not using Mt Fuji extended media tray reports,
2688 * just return TRAY_OPEN since ATAPI doesn't provide
2689 * any other way to detect this...
2691 if (sense.sense_key == NOT_READY) {
2692 if (sense.asc == 0x3a && sense.ascq == 1)
2693 return CDS_NO_DISC;
2694 else
2695 return CDS_TRAY_OPEN;
2698 return CDS_DRIVE_NOT_READY;
2700 return -EINVAL;
2703 static
2704 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2705 struct cdrom_multisession *ms_info)
2707 struct atapi_toc *toc;
2708 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2709 struct cdrom_info *info = drive->driver_data;
2710 struct request_sense sense;
2711 int ret;
2713 if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2714 if ((ret = cdrom_read_toc(drive, &sense)))
2715 return ret;
2717 toc = info->toc;
2718 ms_info->addr.lba = toc->last_session_lba;
2719 ms_info->xa_flag = toc->xa_flag;
2721 return 0;
2724 static
2725 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2726 struct cdrom_mcn *mcn_info)
2728 int stat;
2729 char mcnbuf[24];
2730 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2732 /* get MCN */
2733 if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2734 return stat;
2736 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2737 sizeof (mcn_info->medium_catalog_number)-1);
2738 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2739 = '\0';
2741 return 0;
2746 /****************************************************************************
2747 * Other driver requests (open, close, check media change).
2750 static
2751 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2752 int slot_nr)
2754 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2755 int retval;
2757 if (slot_nr == CDSL_CURRENT) {
2758 (void) cdrom_check_status(drive, NULL);
2759 retval = CDROM_STATE_FLAGS(drive)->media_changed;
2760 CDROM_STATE_FLAGS(drive)->media_changed = 0;
2761 return retval;
2762 } else {
2763 return -EINVAL;
2768 static
2769 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2771 return 0;
2776 * Close down the device. Invalidate all cached blocks.
2779 static
2780 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2786 /****************************************************************************
2787 * Device initialization.
2789 static struct cdrom_device_ops ide_cdrom_dops = {
2790 .open = ide_cdrom_open_real,
2791 .release = ide_cdrom_release_real,
2792 .drive_status = ide_cdrom_drive_status,
2793 .media_changed = ide_cdrom_check_media_change_real,
2794 .tray_move = ide_cdrom_tray_move,
2795 .lock_door = ide_cdrom_lock_door,
2796 .select_speed = ide_cdrom_select_speed,
2797 .get_last_session = ide_cdrom_get_last_session,
2798 .get_mcn = ide_cdrom_get_mcn,
2799 .reset = ide_cdrom_reset,
2800 .audio_ioctl = ide_cdrom_audio_ioctl,
2801 .dev_ioctl = ide_cdrom_dev_ioctl,
2802 .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2803 CDC_SELECT_SPEED | CDC_SELECT_DISC |
2804 CDC_MULTI_SESSION | CDC_MCN |
2805 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
2806 CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
2807 CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2808 CDC_GENERIC_PACKET,
2809 .generic_packet = ide_cdrom_packet,
2812 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2814 struct cdrom_info *info = drive->driver_data;
2815 struct cdrom_device_info *devinfo = &info->devinfo;
2817 devinfo->ops = &ide_cdrom_dops;
2818 devinfo->mask = 0;
2819 devinfo->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2820 devinfo->capacity = nslots;
2821 devinfo->handle = (void *) drive;
2822 strcpy(devinfo->name, drive->name);
2824 /* set capability mask to match the probe. */
2825 if (!CDROM_CONFIG_FLAGS(drive)->cd_r)
2826 devinfo->mask |= CDC_CD_R;
2827 if (!CDROM_CONFIG_FLAGS(drive)->cd_rw)
2828 devinfo->mask |= CDC_CD_RW;
2829 if (!CDROM_CONFIG_FLAGS(drive)->dvd)
2830 devinfo->mask |= CDC_DVD;
2831 if (!CDROM_CONFIG_FLAGS(drive)->dvd_r)
2832 devinfo->mask |= CDC_DVD_R;
2833 if (!CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2834 devinfo->mask |= CDC_DVD_RAM;
2835 if (!CDROM_CONFIG_FLAGS(drive)->is_changer)
2836 devinfo->mask |= CDC_SELECT_DISC;
2837 if (!CDROM_CONFIG_FLAGS(drive)->audio_play)
2838 devinfo->mask |= CDC_PLAY_AUDIO;
2839 if (!CDROM_CONFIG_FLAGS(drive)->close_tray)
2840 devinfo->mask |= CDC_CLOSE_TRAY;
2842 return register_cdrom(devinfo);
2845 static
2846 int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2848 struct cdrom_info *info = drive->driver_data;
2849 struct cdrom_device_info *cdi = &info->devinfo;
2850 struct cdrom_generic_command cgc;
2851 int stat, attempts = 3, size = sizeof(*cap);
2854 * ACER50 (and others?) require the full spec length mode sense
2855 * page capabilities size, but older drives break.
2857 if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2858 !strcmp(drive->id->model, "WPI CDS-32X")))
2859 size -= sizeof(cap->pad);
2861 /* we have to cheat a little here. the packet will eventually
2862 * be queued with ide_cdrom_packet(), which extracts the
2863 * drive from cdi->handle. Since this device hasn't been
2864 * registered with the Uniform layer yet, it can't do this.
2865 * Same goes for cdi->ops.
2867 cdi->handle = (ide_drive_t *) drive;
2868 cdi->ops = &ide_cdrom_dops;
2869 init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2870 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2871 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2872 if (!stat)
2873 break;
2874 } while (--attempts);
2875 return stat;
2878 static
2879 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2881 struct cdrom_info *info = drive->driver_data;
2882 struct cdrom_device_info *cdi = &info->devinfo;
2883 struct atapi_capabilities_page cap;
2884 int nslots = 1;
2886 if (drive->media == ide_optical) {
2887 printk("%s: ATAPI magneto-optical drive\n", drive->name);
2888 return nslots;
2891 if (CDROM_CONFIG_FLAGS(drive)->nec260) {
2892 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2893 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2894 return nslots;
2897 if (ide_cdrom_get_capabilities(drive, &cap))
2898 return 0;
2900 if (cap.lock == 0)
2901 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2902 if (cap.eject)
2903 CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2904 if (cap.cd_r_write)
2905 CDROM_CONFIG_FLAGS(drive)->cd_r = 1;
2906 if (cap.cd_rw_write)
2907 CDROM_CONFIG_FLAGS(drive)->cd_rw = 1;
2908 if (cap.test_write)
2909 CDROM_CONFIG_FLAGS(drive)->test_write = 1;
2910 if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2911 CDROM_CONFIG_FLAGS(drive)->dvd = 1;
2912 if (cap.dvd_ram_write)
2913 CDROM_CONFIG_FLAGS(drive)->dvd_ram = 1;
2914 if (cap.dvd_r_write)
2915 CDROM_CONFIG_FLAGS(drive)->dvd_r = 1;
2916 if (cap.audio_play)
2917 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2918 if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2919 CDROM_CONFIG_FLAGS(drive)->close_tray = 0;
2921 /* Some drives used by Apple don't advertise audio play
2922 * but they do support reading TOC & audio datas
2924 if (strcmp(drive->id->model, "MATSHITADVD-ROM SR-8187") == 0 ||
2925 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8186") == 0 ||
2926 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8176") == 0 ||
2927 strcmp(drive->id->model, "MATSHITADVD-ROM SR-8174") == 0)
2928 CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2930 #if ! STANDARD_ATAPI
2931 if (cdi->sanyo_slot > 0) {
2932 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2933 nslots = 3;
2936 else
2937 #endif /* not STANDARD_ATAPI */
2938 if (cap.mechtype == mechtype_individual_changer ||
2939 cap.mechtype == mechtype_cartridge_changer) {
2940 if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2941 CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2942 CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 1;
2946 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2947 if (!drive->id->model[0] &&
2948 !strncmp(drive->id->fw_rev, "241N", 4)) {
2949 CDROM_STATE_FLAGS(drive)->current_speed =
2950 (((unsigned int)cap.curspeed) + (176/2)) / 176;
2951 CDROM_CONFIG_FLAGS(drive)->max_speed =
2952 (((unsigned int)cap.maxspeed) + (176/2)) / 176;
2953 } else {
2954 CDROM_STATE_FLAGS(drive)->current_speed =
2955 (ntohs(cap.curspeed) + (176/2)) / 176;
2956 CDROM_CONFIG_FLAGS(drive)->max_speed =
2957 (ntohs(cap.maxspeed) + (176/2)) / 176;
2960 /* don't print speed if the drive reported 0.
2962 printk("%s: ATAPI", drive->name);
2963 if (CDROM_CONFIG_FLAGS(drive)->max_speed)
2964 printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
2965 printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
2967 if (CDROM_CONFIG_FLAGS(drive)->dvd_r|CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2968 printk(" DVD%s%s",
2969 (CDROM_CONFIG_FLAGS(drive)->dvd_r)? "-R" : "",
2970 (CDROM_CONFIG_FLAGS(drive)->dvd_ram)? "-RAM" : "");
2972 if (CDROM_CONFIG_FLAGS(drive)->cd_r|CDROM_CONFIG_FLAGS(drive)->cd_rw)
2973 printk(" CD%s%s",
2974 (CDROM_CONFIG_FLAGS(drive)->cd_r)? "-R" : "",
2975 (CDROM_CONFIG_FLAGS(drive)->cd_rw)? "/RW" : "");
2977 if (CDROM_CONFIG_FLAGS(drive)->is_changer)
2978 printk(" changer w/%d slots", nslots);
2979 else
2980 printk(" drive");
2982 printk(", %dkB Cache", be16_to_cpu(cap.buffer_size));
2984 #ifdef CONFIG_BLK_DEV_IDEDMA
2985 if (drive->using_dma)
2986 (void) HWIF(drive)->ide_dma_verbose(drive);
2987 #endif /* CONFIG_BLK_DEV_IDEDMA */
2988 printk("\n");
2990 return nslots;
2993 static void ide_cdrom_add_settings(ide_drive_t *drive)
2995 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2999 * standard prep_rq_fn that builds 10 byte cmds
3001 static int ide_cdrom_prep_fs(request_queue_t *q, struct request *rq)
3003 int hard_sect = queue_hardsect_size(q);
3004 long block = (long)rq->hard_sector / (hard_sect >> 9);
3005 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
3007 memset(rq->cmd, 0, sizeof(rq->cmd));
3009 if (rq_data_dir(rq) == READ)
3010 rq->cmd[0] = GPCMD_READ_10;
3011 else
3012 rq->cmd[0] = GPCMD_WRITE_10;
3015 * fill in lba
3017 rq->cmd[2] = (block >> 24) & 0xff;
3018 rq->cmd[3] = (block >> 16) & 0xff;
3019 rq->cmd[4] = (block >> 8) & 0xff;
3020 rq->cmd[5] = block & 0xff;
3023 * and transfer length
3025 rq->cmd[7] = (blocks >> 8) & 0xff;
3026 rq->cmd[8] = blocks & 0xff;
3027 rq->cmd_len = 10;
3028 return BLKPREP_OK;
3032 * Most of the SCSI commands are supported directly by ATAPI devices.
3033 * This transform handles the few exceptions.
3035 static int ide_cdrom_prep_pc(struct request *rq)
3037 u8 *c = rq->cmd;
3040 * Transform 6-byte read/write commands to the 10-byte version
3042 if (c[0] == READ_6 || c[0] == WRITE_6) {
3043 c[8] = c[4];
3044 c[5] = c[3];
3045 c[4] = c[2];
3046 c[3] = c[1] & 0x1f;
3047 c[2] = 0;
3048 c[1] &= 0xe0;
3049 c[0] += (READ_10 - READ_6);
3050 rq->cmd_len = 10;
3051 return BLKPREP_OK;
3055 * it's silly to pretend we understand 6-byte sense commands, just
3056 * reject with ILLEGAL_REQUEST and the caller should take the
3057 * appropriate action
3059 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
3060 rq->errors = ILLEGAL_REQUEST;
3061 return BLKPREP_KILL;
3064 return BLKPREP_OK;
3067 static int ide_cdrom_prep_fn(request_queue_t *q, struct request *rq)
3069 if (rq->flags & REQ_CMD)
3070 return ide_cdrom_prep_fs(q, rq);
3071 else if (rq->flags & REQ_BLOCK_PC)
3072 return ide_cdrom_prep_pc(rq);
3074 return 0;
3077 static
3078 int ide_cdrom_setup (ide_drive_t *drive)
3080 struct cdrom_info *info = drive->driver_data;
3081 struct cdrom_device_info *cdi = &info->devinfo;
3082 int nslots;
3085 * default to read-only always and fix latter at the bottom
3087 set_disk_ro(drive->disk, 1);
3088 blk_queue_hardsect_size(&drive->queue, CD_FRAMESIZE);
3090 blk_queue_prep_rq(&drive->queue, ide_cdrom_prep_fn);
3091 blk_queue_dma_alignment(&drive->queue, 3);
3093 drive->special.all = 0;
3094 drive->ready_stat = 0;
3096 CDROM_STATE_FLAGS(drive)->media_changed = 1;
3097 CDROM_STATE_FLAGS(drive)->toc_valid = 0;
3098 CDROM_STATE_FLAGS(drive)->door_locked = 0;
3100 #if NO_DOOR_LOCKING
3101 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
3102 #else
3103 CDROM_CONFIG_FLAGS(drive)->no_doorlock = 0;
3104 #endif
3106 CDROM_CONFIG_FLAGS(drive)->drq_interrupt = ((drive->id->config & 0x0060) == 0x20);
3107 CDROM_CONFIG_FLAGS(drive)->is_changer = 0;
3108 CDROM_CONFIG_FLAGS(drive)->cd_r = 0;
3109 CDROM_CONFIG_FLAGS(drive)->cd_rw = 0;
3110 CDROM_CONFIG_FLAGS(drive)->test_write = 0;
3111 CDROM_CONFIG_FLAGS(drive)->dvd = 0;
3112 CDROM_CONFIG_FLAGS(drive)->dvd_r = 0;
3113 CDROM_CONFIG_FLAGS(drive)->dvd_ram = 0;
3114 CDROM_CONFIG_FLAGS(drive)->no_eject = 1;
3115 CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 0;
3116 CDROM_CONFIG_FLAGS(drive)->audio_play = 0;
3117 CDROM_CONFIG_FLAGS(drive)->close_tray = 1;
3119 /* limit transfer size per interrupt. */
3120 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 0;
3121 /* a testament to the nice quality of Samsung drives... */
3122 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
3123 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
3124 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
3125 CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
3126 /* the 3231 model does not support the SET_CD_SPEED command */
3127 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
3128 cdi->mask |= CDC_SELECT_SPEED;
3130 #if ! STANDARD_ATAPI
3131 /* by default Sanyo 3 CD changer support is turned off and
3132 ATAPI Rev 2.2+ standard support for CD changers is used */
3133 cdi->sanyo_slot = 0;
3135 CDROM_CONFIG_FLAGS(drive)->nec260 = 0;
3136 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 0;
3137 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 0;
3138 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 0;
3139 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 0;
3141 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
3142 drive->id->fw_rev[4] == '1' &&
3143 drive->id->fw_rev[6] <= '2') {
3144 /* Vertos 300.
3145 Some versions of this drive like to talk BCD. */
3146 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
3147 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
3148 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3149 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3152 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
3153 drive->id->fw_rev[4] == '1' &&
3154 drive->id->fw_rev[6] <= '2') {
3155 /* Vertos 600 ESD. */
3156 CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
3158 else if (strcmp(drive->id->model, "NEC CD-ROM DRIVE:260") == 0 &&
3159 strncmp(drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
3160 /* Old NEC260 (not R).
3161 This drive was released before the 1.2 version
3162 of the spec. */
3163 CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
3164 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3165 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3166 CDROM_CONFIG_FLAGS(drive)->nec260 = 1;
3168 else if (strcmp(drive->id->model, "WEARNES CDD-120") == 0 &&
3169 strncmp(drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
3170 /* Wearnes */
3171 CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
3172 CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
3174 /* Sanyo 3 CD changer uses a non-standard command
3175 for CD changing */
3176 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
3177 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
3178 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
3179 /* uses CD in slot 0 when value is set to 3 */
3180 cdi->sanyo_slot = 3;
3182 #endif /* not STANDARD_ATAPI */
3184 info->toc = NULL;
3185 info->buffer = NULL;
3186 info->sector_buffered = 0;
3187 info->nsectors_buffered = 0;
3188 info->changer_info = NULL;
3189 info->last_block = 0;
3190 info->start_seek = 0;
3192 nslots = ide_cdrom_probe_capabilities (drive);
3194 if (CDROM_CONFIG_FLAGS(drive)->dvd_ram)
3195 set_disk_ro(drive->disk, 0);
3197 #if 0
3198 drive->dsc_overlap = (HWIF(drive)->no_dsc) ? 0 : 1;
3199 if (HWIF(drive)->no_dsc) {
3200 printk(KERN_INFO "ide-cd: %s: disabling DSC overlap\n",
3201 drive->name);
3202 drive->dsc_overlap = 0;
3204 #endif
3206 if (ide_cdrom_register(drive, nslots)) {
3207 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
3208 info->devinfo.handle = NULL;
3209 return 1;
3211 ide_cdrom_add_settings(drive);
3212 return 0;
3215 static
3216 unsigned long ide_cdrom_capacity (ide_drive_t *drive)
3218 unsigned long capacity;
3220 if (cdrom_read_capacity(drive, &capacity, NULL))
3221 return 0;
3223 return capacity * SECTORS_PER_FRAME;
3226 static
3227 int ide_cdrom_cleanup(ide_drive_t *drive)
3229 struct cdrom_info *info = drive->driver_data;
3230 struct cdrom_device_info *devinfo = &info->devinfo;
3231 struct gendisk *g = drive->disk;
3233 if (ide_unregister_subdriver(drive)) {
3234 printk("%s: %s: failed to ide_unregister_subdriver\n",
3235 __FUNCTION__, drive->name);
3236 return 1;
3238 if (info->buffer != NULL)
3239 kfree(info->buffer);
3240 if (info->toc != NULL)
3241 kfree(info->toc);
3242 if (info->changer_info != NULL)
3243 kfree(info->changer_info);
3244 if (devinfo->handle == drive && unregister_cdrom(devinfo))
3245 printk("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3246 kfree(info);
3247 drive->driver_data = NULL;
3248 blk_queue_prep_rq(&drive->queue, NULL);
3249 del_gendisk(g);
3250 g->fops = ide_fops;
3251 return 0;
3254 static int ide_cdrom_attach (ide_drive_t *drive);
3257 * Power Management state machine.
3259 * We don't do much for CDs right now.
3262 static void ide_cdrom_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
3266 static ide_startstop_t ide_cdrom_start_power_step (ide_drive_t *drive, struct request *rq)
3268 ide_task_t *args = rq->special;
3270 memset(args, 0, sizeof(*args));
3272 switch (rq->pm->pm_step) {
3273 case ide_pm_state_start_suspend:
3274 break;
3276 case ide_pm_state_start_resume: /* Resume step 1 (restore DMA) */
3278 * Right now, all we do is call hwif->ide_dma_check(drive),
3279 * we could be smarter and check for current xfer_speed
3280 * in struct drive etc...
3281 * Also, this step could be implemented as a generic helper
3282 * as most subdrivers will use it.
3284 if ((drive->id->capability & 1) == 0)
3285 break;
3286 if (HWIF(drive)->ide_dma_check == NULL)
3287 break;
3288 HWIF(drive)->ide_dma_check(drive);
3289 break;
3291 rq->pm->pm_step = ide_pm_state_completed;
3292 return ide_stopped;
3295 static ide_driver_t ide_cdrom_driver = {
3296 .owner = THIS_MODULE,
3297 .name = "ide-cdrom",
3298 .version = IDECD_VERSION,
3299 .media = ide_cdrom,
3300 .busy = 0,
3301 .supports_dma = 1,
3302 .supports_dsc_overlap = 1,
3303 .cleanup = ide_cdrom_cleanup,
3304 .do_request = ide_do_rw_cdrom,
3305 .sense = ide_cdrom_dump_status,
3306 .error = ide_cdrom_error,
3307 .abort = ide_cdrom_abort,
3308 .capacity = ide_cdrom_capacity,
3309 .attach = ide_cdrom_attach,
3310 .drives = LIST_HEAD_INIT(ide_cdrom_driver.drives),
3311 .start_power_step = ide_cdrom_start_power_step,
3312 .complete_power_step = ide_cdrom_complete_power_step,
3313 .gen_driver = {
3314 .suspend = generic_ide_suspend,
3315 .resume = generic_ide_resume,
3319 static int idecd_open(struct inode * inode, struct file * file)
3321 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
3322 struct cdrom_info *info = drive->driver_data;
3323 int rc = -ENOMEM;
3324 drive->usage++;
3326 if (!info->buffer)
3327 info->buffer = (char *) kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL);
3328 if (!info->buffer || (rc = cdrom_open(&info->devinfo, inode, file)))
3329 drive->usage--;
3330 return rc;
3333 static int idecd_release(struct inode * inode, struct file * file)
3335 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
3336 struct cdrom_info *info = drive->driver_data;
3338 cdrom_release (&info->devinfo, file);
3339 drive->usage--;
3340 return 0;
3343 static int idecd_ioctl (struct inode *inode, struct file *file,
3344 unsigned int cmd, unsigned long arg)
3346 struct block_device *bdev = inode->i_bdev;
3347 ide_drive_t *drive = bdev->bd_disk->private_data;
3348 int err = generic_ide_ioctl(bdev, cmd, arg);
3349 if (err == -EINVAL) {
3350 struct cdrom_info *info = drive->driver_data;
3351 err = cdrom_ioctl(&info->devinfo, inode, cmd, arg);
3353 return err;
3356 static int idecd_media_changed(struct gendisk *disk)
3358 ide_drive_t *drive = disk->private_data;
3359 struct cdrom_info *info = drive->driver_data;
3360 return cdrom_media_changed(&info->devinfo);
3363 static int idecd_revalidate_disk(struct gendisk *disk)
3365 ide_drive_t *drive = disk->private_data;
3366 struct request_sense sense;
3367 cdrom_read_toc(drive, &sense);
3368 return 0;
3371 static struct block_device_operations idecd_ops = {
3372 .owner = THIS_MODULE,
3373 .open = idecd_open,
3374 .release = idecd_release,
3375 .ioctl = idecd_ioctl,
3376 .media_changed = idecd_media_changed,
3377 .revalidate_disk= idecd_revalidate_disk
3380 /* options */
3381 char *ignore = NULL;
3383 MODULE_PARM(ignore, "s");
3384 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3386 static int ide_cdrom_attach (ide_drive_t *drive)
3388 struct cdrom_info *info;
3389 struct gendisk *g = drive->disk;
3390 struct request_sense sense;
3392 if (!strstr("ide-cdrom", drive->driver_req))
3393 goto failed;
3394 if (!drive->present)
3395 goto failed;
3396 if (drive->media != ide_cdrom && drive->media != ide_optical)
3397 goto failed;
3398 /* skip drives that we were told to ignore */
3399 if (ignore != NULL) {
3400 if (strstr(ignore, drive->name)) {
3401 printk("ide-cd: ignoring drive %s\n", drive->name);
3402 goto failed;
3405 if (drive->scsi) {
3406 printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
3407 goto failed;
3409 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
3410 if (info == NULL) {
3411 printk("%s: Can't allocate a cdrom structure\n", drive->name);
3412 goto failed;
3414 if (ide_register_subdriver(drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
3415 printk("%s: Failed to register the driver with ide.c\n",
3416 drive->name);
3417 kfree(info);
3418 goto failed;
3420 memset(info, 0, sizeof (struct cdrom_info));
3421 drive->driver_data = info;
3422 DRIVER(drive)->busy++;
3423 g->minors = 1;
3424 snprintf(g->devfs_name, sizeof(g->devfs_name),
3425 "%s/cd", drive->devfs_name);
3426 g->driverfs_dev = &drive->gendev;
3427 g->flags = GENHD_FL_CD;
3428 if (ide_cdrom_setup(drive)) {
3429 struct cdrom_device_info *devinfo = &info->devinfo;
3430 DRIVER(drive)->busy--;
3431 ide_unregister_subdriver(drive);
3432 if (info->buffer != NULL)
3433 kfree(info->buffer);
3434 if (info->toc != NULL)
3435 kfree(info->toc);
3436 if (info->changer_info != NULL)
3437 kfree(info->changer_info);
3438 if (devinfo->handle == drive && unregister_cdrom(devinfo))
3439 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3440 kfree(info);
3441 drive->driver_data = NULL;
3442 goto failed;
3444 DRIVER(drive)->busy--;
3446 cdrom_read_toc(drive, &sense);
3447 g->fops = &idecd_ops;
3448 add_disk(g);
3449 return 0;
3450 failed:
3451 return 1;
3454 static void __exit ide_cdrom_exit(void)
3456 ide_unregister_driver(&ide_cdrom_driver);
3459 static int ide_cdrom_init(void)
3461 ide_register_driver(&ide_cdrom_driver);
3462 return 0;
3465 module_init(ide_cdrom_init);
3466 module_exit(ide_cdrom_exit);
3467 MODULE_LICENSE("GPL");