- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / ide / ide-cd.c
blob4d7cb52409d422d0002d930f75f14a845b9226f7
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 accomodated 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 occured.
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
292 *************************************************************************/
294 #define IDECD_VERSION "4.59"
296 #include <linux/config.h>
297 #include <linux/module.h>
298 #include <linux/types.h>
299 #include <linux/kernel.h>
300 #include <linux/delay.h>
301 #include <linux/timer.h>
302 #include <linux/malloc.h>
303 #include <linux/interrupt.h>
304 #include <linux/errno.h>
305 #include <linux/cdrom.h>
306 #include <linux/ide.h>
308 #include <asm/irq.h>
309 #include <asm/io.h>
310 #include <asm/byteorder.h>
311 #include <asm/uaccess.h>
312 #include <asm/unaligned.h>
314 #include "ide-cd.h"
316 /****************************************************************************
317 * Generic packet command support and error handling routines.
320 /* Mark that we've seen a media change, and invalidate our internal
321 buffers. */
322 static void cdrom_saw_media_change (ide_drive_t *drive)
324 struct cdrom_info *info = drive->driver_data;
326 CDROM_STATE_FLAGS (drive)->media_changed = 1;
327 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
328 info->nsectors_buffered = 0;
331 static int cdrom_log_sense(ide_drive_t *drive, struct packet_command *pc,
332 struct request_sense *sense)
334 int log = 0;
336 if (sense == NULL || pc->quiet)
337 return 0;
339 switch (sense->sense_key) {
340 case NO_SENSE: case RECOVERED_ERROR:
341 break;
342 case NOT_READY:
344 * don't care about tray state messages for
345 * e.g. capacity commands or in-progress or
346 * becoming ready
348 if (sense->asc == 0x3a || sense->asc == 0x04)
349 break;
350 log = 1;
351 break;
352 case UNIT_ATTENTION:
354 * Make good and sure we've seen this potential media
355 * change. Some drives (i.e. Creative) fail to present
356 * the correct sense key in the error register.
358 cdrom_saw_media_change(drive);
359 break;
360 default:
361 log = 1;
362 break;
364 return log;
367 static
368 void cdrom_analyze_sense_data(ide_drive_t *drive,
369 struct packet_command *failed_command,
370 struct request_sense *sense)
373 if (!cdrom_log_sense(drive, failed_command, sense))
374 return;
377 * If a read toc is executed for a CD-R or CD-RW medium where
378 * the first toc has not been recorded yet, it will fail with
379 * 05/24/00 (which is a confusing error)
381 if (failed_command && failed_command->c[0] == GPCMD_READ_TOC_PMA_ATIP)
382 if (sense->sense_key == 0x05 && sense->asc == 0x24)
383 return;
385 #if VERBOSE_IDE_CD_ERRORS
387 int i;
388 const char *s;
389 char buf[80];
391 printk ("ATAPI device %s:\n", drive->name);
392 if (sense->error_code==0x70)
393 printk(" Error: ");
394 else if (sense->error_code==0x71)
395 printk(" Deferred Error: ");
396 else if (sense->error_code == 0x7f)
397 printk(" Vendor-specific Error: ");
398 else
399 printk(" Unknown Error Type: ");
401 if (sense->sense_key < ARY_LEN(sense_key_texts))
402 s = sense_key_texts[sense->sense_key];
403 else
404 s = "bad sense key!";
406 printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
408 if (sense->asc == 0x40) {
409 sprintf(buf, "Diagnostic failure on component 0x%02x",
410 sense->ascq);
411 s = buf;
412 } else {
413 int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
414 unsigned long key = (sense->sense_key << 16);
415 key |= (sense->asc << 8);
416 if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
417 key |= sense->ascq;
418 s = NULL;
420 while (hi > lo) {
421 mid = (lo + hi) / 2;
422 if (sense_data_texts[mid].asc_ascq == key ||
423 sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
424 s = sense_data_texts[mid].text;
425 break;
427 else if (sense_data_texts[mid].asc_ascq > key)
428 hi = mid;
429 else
430 lo = mid+1;
434 if (s == NULL) {
435 if (sense->asc > 0x80)
436 s = "(vendor-specific error)";
437 else
438 s = "(reserved error code)";
441 printk(" %s -- (asc=0x%02x, ascq=0x%02x)\n",
442 s, sense->asc, sense->ascq);
444 if (failed_command != NULL) {
446 int lo=0, mid, hi= ARY_LEN (packet_command_texts);
447 s = NULL;
449 while (hi > lo) {
450 mid = (lo + hi) / 2;
451 if (packet_command_texts[mid].packet_command ==
452 failed_command->c[0]) {
453 s = packet_command_texts[mid].text;
454 break;
456 if (packet_command_texts[mid].packet_command >
457 failed_command->c[0])
458 hi = mid;
459 else
460 lo = mid+1;
463 printk (" The failed \"%s\" packet command was: \n \"", s);
464 for (i=0; i<sizeof (failed_command->c); i++)
465 printk ("%02x ", failed_command->c[i]);
466 printk ("\"\n");
469 /* The SKSV bit specifies validity of the sense_key_specific
470 * in the next two commands. It is bit 7 of the first byte.
471 * In the case of NOT_READY, if SKSV is set the drive can
472 * give us nice ETA readings.
474 if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
475 int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
476 printk(" Command is %02d%% complete\n", progress / 0xffff);
480 if (sense->sense_key == ILLEGAL_REQUEST &&
481 (sense->sks[0] & 0x80) != 0) {
482 printk(" Error in %s byte %d",
483 (sense->sks[0] & 0x40) != 0 ?
484 "command packet" : "command data",
485 (sense->sks[1] << 8) + sense->sks[2]);
487 if ((sense->sks[0] & 0x40) != 0)
488 printk (" bit %d", sense->sks[0] & 0x07);
490 printk ("\n");
494 #else /* not VERBOSE_IDE_CD_ERRORS */
496 /* Suppress printing unit attention and `in progress of becoming ready'
497 errors when we're not being verbose. */
499 if (sense->sense_key == UNIT_ATTENTION ||
500 (sense->sense_key == NOT_READY && (sense->asc == 4 ||
501 sense->asc == 0x3a)))
502 return;
504 printk("%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
505 drive->name,
506 sense->error_code, sense->sense_key,
507 sense->asc, sense->ascq);
508 #endif /* not VERBOSE_IDE_CD_ERRORS */
511 static void cdrom_queue_request_sense(ide_drive_t *drive,
512 struct semaphore *sem,
513 struct request_sense *sense,
514 struct packet_command *failed_command)
516 struct cdrom_info *info = drive->driver_data;
517 struct request *rq;
518 struct packet_command *pc = &info->request_sense_pc;
520 if (sense == NULL)
521 sense = &info->sense_data;
523 memset(pc, 0, sizeof(struct packet_command));
524 pc->c[0] = GPCMD_REQUEST_SENSE;
525 pc->c[4] = pc->buflen = 18;
526 pc->buffer = (char *) sense;
527 pc->sense = (struct request_sense *) failed_command;
529 /* stuff the sense request in front of our current request */
530 rq = &info->request_sense_request;
531 ide_init_drive_cmd(rq);
532 rq->cmd = REQUEST_SENSE_COMMAND;
533 rq->buffer = (char *) pc;
534 rq->sem = sem;
535 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
539 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
541 struct request *rq = HWGROUP(drive)->rq;
543 if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
544 struct packet_command *pc = (struct packet_command *)rq->buffer;
545 cdrom_analyze_sense_data(drive,
546 (struct packet_command *) pc->sense,
547 (struct request_sense *) (pc->buffer - pc->c[4]));
549 if (rq->cmd == READ && !rq->current_nr_sectors)
550 uptodate = 1;
552 ide_end_request (uptodate, HWGROUP(drive));
556 /* Returns 0 if the request should be continued.
557 Returns 1 if the request was ended. */
558 static int cdrom_decode_status (ide_startstop_t *startstop, ide_drive_t *drive,
559 int good_stat, int *stat_ret)
561 struct request *rq = HWGROUP(drive)->rq;
562 int stat, err, sense_key;
563 struct packet_command *pc;
565 /* Check for errors. */
566 stat = GET_STAT();
567 *stat_ret = stat;
569 if (OK_STAT (stat, good_stat, BAD_R_STAT))
570 return 0;
572 /* Get the IDE error register. */
573 err = GET_ERR();
574 sense_key = err >> 4;
576 if (rq == NULL) {
577 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
578 *startstop = ide_stopped;
579 return 1;
582 if (rq->cmd == REQUEST_SENSE_COMMAND) {
583 /* We got an error trying to get sense info
584 from the drive (probably while trying
585 to recover from a former error). Just give up. */
587 pc = (struct packet_command *) rq->buffer;
588 pc->stat = 1;
589 cdrom_end_request (1, drive);
590 *startstop = ide_error (drive, "request sense failure", stat);
591 return 1;
593 } else if (rq->cmd == PACKET_COMMAND) {
594 /* All other functions, except for READ. */
595 struct semaphore *sem = NULL;
596 pc = (struct packet_command *) rq->buffer;
598 /* Check for tray open. */
599 if (sense_key == NOT_READY) {
600 cdrom_saw_media_change (drive);
601 } else if (sense_key == UNIT_ATTENTION) {
602 /* Check for media change. */
603 cdrom_saw_media_change (drive);
604 /*printk("%s: media changed\n",drive->name);*/
605 return 0;
606 } else if (!pc->quiet) {
607 /* Otherwise, print an error. */
608 ide_dump_status(drive, "packet command error", stat);
611 /* Set the error flag and complete the request.
612 Then, if we have a CHECK CONDITION status,
613 queue a request sense command. We must be careful,
614 though: we don't want the thread in
615 cdrom_queue_packet_command to wake up until
616 the request sense has completed. We do this
617 by transferring the semaphore from the packet
618 command request to the request sense request. */
620 if ((stat & ERR_STAT) != 0) {
621 sem = rq->sem;
622 rq->sem = NULL;
625 pc->stat = 1;
626 cdrom_end_request (1, drive);
628 if ((stat & ERR_STAT) != 0)
629 cdrom_queue_request_sense(drive, sem, pc->sense, pc);
630 } else {
631 /* Handle errors from READ requests. */
633 if (sense_key == NOT_READY) {
634 /* Tray open. */
635 cdrom_saw_media_change (drive);
637 /* Fail the request. */
638 printk ("%s: tray open\n", drive->name);
639 cdrom_end_request (0, drive);
640 } else if (sense_key == UNIT_ATTENTION) {
641 /* Media change. */
642 cdrom_saw_media_change (drive);
644 /* Arrange to retry the request.
645 But be sure to give up if we've retried
646 too many times. */
647 if (++rq->errors > ERROR_MAX)
648 cdrom_end_request (0, drive);
649 } else if (sense_key == ILLEGAL_REQUEST ||
650 sense_key == DATA_PROTECT) {
651 /* No point in retrying after an illegal
652 request or data protect error.*/
653 ide_dump_status (drive, "command error", stat);
654 cdrom_end_request (0, drive);
655 } else if ((err & ~ABRT_ERR) != 0) {
656 /* Go to the default handler
657 for other errors. */
658 *startstop = ide_error (drive, "cdrom_decode_status", stat);
659 return 1;
660 } else if ((++rq->errors > ERROR_MAX)) {
661 /* We've racked up too many retries. Abort. */
662 cdrom_end_request (0, drive);
665 /* If we got a CHECK_CONDITION status,
666 queue a request sense command. */
667 if ((stat & ERR_STAT) != 0)
668 cdrom_queue_request_sense(drive, NULL, NULL, NULL);
671 /* Retry, or handle the next request. */
672 *startstop = ide_stopped;
673 return 1;
676 static int cdrom_timer_expiry(ide_drive_t *drive)
678 struct request *rq = HWGROUP(drive)->rq;
679 struct packet_command *pc = (struct packet_command *) rq->buffer;
680 unsigned long wait = 0;
682 /* blank and format can take an extremly long time to
683 * complete, if the IMMED bit was not set.
685 if (pc->c[0] == GPCMD_BLANK || pc->c[0] == GPCMD_FORMAT_UNIT)
686 wait = 60*60*HZ;
688 return wait;
691 /* Set up the device registers for transferring a packet command on DEV,
692 expecting to later transfer XFERLEN bytes. HANDLER is the routine
693 which actually transfers the command to the drive. If this is a
694 drq_interrupt device, this routine will arrange for HANDLER to be
695 called when the interrupt from the drive arrives. Otherwise, HANDLER
696 will be called immediately after the drive is prepared for the transfer. */
698 static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
699 int xferlen,
700 ide_handler_t *handler)
702 ide_startstop_t startstop;
703 struct cdrom_info *info = drive->driver_data;
705 /* Wait for the controller to be idle. */
706 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
707 return startstop;
709 if (info->dma)
710 info->dma = !HWIF(drive)->dmaproc(ide_dma_read, drive);
712 /* Set up the controller registers. */
713 OUT_BYTE (info->dma, IDE_FEATURE_REG);
714 OUT_BYTE (0, IDE_NSECTOR_REG);
715 OUT_BYTE (0, IDE_SECTOR_REG);
717 OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
718 OUT_BYTE (xferlen >> 8 , IDE_HCYL_REG);
719 if (IDE_CONTROL_REG)
720 OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
722 if (info->dma)
723 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
725 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
726 ide_set_handler (drive, handler, WAIT_CMD, cdrom_timer_expiry);
727 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
728 return ide_started;
729 } else {
730 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
731 return (*handler) (drive);
735 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
736 The device registers must have already been prepared
737 by cdrom_start_packet_command.
738 HANDLER is the interrupt handler to call when the command completes
739 or there's data ready. */
740 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
741 unsigned char *cmd_buf, int cmd_len,
742 ide_handler_t *handler,
743 unsigned int timeout)
745 ide_startstop_t startstop;
747 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
748 /* Here we should have been called after receiving an interrupt
749 from the device. DRQ should how be set. */
750 int stat_dum;
752 /* Check for errors. */
753 if (cdrom_decode_status (&startstop, drive, DRQ_STAT, &stat_dum))
754 return startstop;
755 } else {
756 /* Otherwise, we must wait for DRQ to get set. */
757 if (ide_wait_stat (&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
758 return startstop;
761 /* Arm the interrupt handler. */
762 ide_set_handler (drive, handler, timeout, cdrom_timer_expiry);
764 /* Send the command to the device. */
765 atapi_output_bytes (drive, cmd_buf, cmd_len);
767 return ide_started;
772 /****************************************************************************
773 * Block read functions.
777 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
778 * buffer. Once the first sector is added, any subsequent sectors are
779 * assumed to be continuous (until the buffer is cleared). For the first
780 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
781 * the buffer is cleared.)
783 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
784 int sectors_to_transfer)
786 struct cdrom_info *info = drive->driver_data;
788 /* Number of sectors to read into the buffer. */
789 int sectors_to_buffer = MIN (sectors_to_transfer,
790 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
791 info->nsectors_buffered);
793 char *dest;
795 /* If we couldn't get a buffer, don't try to buffer anything... */
796 if (info->buffer == NULL)
797 sectors_to_buffer = 0;
799 /* If this is the first sector in the buffer, remember its number. */
800 if (info->nsectors_buffered == 0)
801 info->sector_buffered = sector;
803 /* Read the data into the buffer. */
804 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
805 while (sectors_to_buffer > 0) {
806 atapi_input_bytes (drive, dest, SECTOR_SIZE);
807 --sectors_to_buffer;
808 --sectors_to_transfer;
809 ++info->nsectors_buffered;
810 dest += SECTOR_SIZE;
813 /* Throw away any remaining data. */
814 while (sectors_to_transfer > 0) {
815 char dum[SECTOR_SIZE];
816 atapi_input_bytes (drive, dum, sizeof (dum));
817 --sectors_to_transfer;
822 * Check the contents of the interrupt reason register from the cdrom
823 * and attempt to recover if there are problems. Returns 0 if everything's
824 * ok; nonzero if the request has been terminated.
826 static inline
827 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
829 ireason &= 3;
830 if (ireason == 2) return 0;
832 if (ireason == 0) {
833 /* Whoops... The drive is expecting to receive data from us! */
834 printk ("%s: cdrom_read_intr: "
835 "Drive wants to transfer data the wrong way!\n",
836 drive->name);
838 /* Throw some data at the drive so it doesn't hang
839 and quit this request. */
840 while (len > 0) {
841 int dum = 0;
842 atapi_output_bytes (drive, &dum, sizeof (dum));
843 len -= sizeof (dum);
845 } else if (ireason == 1) {
846 /* Some drives (ASUS) seem to tell us that status
847 * info is available. just get it and ignore.
849 GET_STAT();
850 return 0;
851 } else {
852 /* Drive wants a command packet, or invalid ireason... */
853 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
854 drive->name, ireason);
857 cdrom_end_request (0, drive);
858 return -1;
862 * Interrupt routine. Called when a read request has completed.
864 static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
866 int stat;
867 int ireason, len, sectors_to_transfer, nskip;
868 struct cdrom_info *info = drive->driver_data;
869 int i, dma = info->dma, dma_error = 0;
870 ide_startstop_t startstop;
872 struct request *rq = HWGROUP(drive)->rq;
874 /* Check for errors. */
875 if (dma) {
876 info->dma = 0;
877 if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))
878 HWIF(drive)->dmaproc(ide_dma_off, drive);
881 if (cdrom_decode_status (&startstop, drive, 0, &stat))
882 return startstop;
884 if (dma) {
885 if (!dma_error) {
886 for (i = rq->nr_sectors; i > 0;) {
887 i -= rq->current_nr_sectors;
888 ide_end_request(1, HWGROUP(drive));
890 return ide_stopped;
891 } else
892 return ide_error (drive, "dma error", stat);
895 /* Read the interrupt reason and the transfer length. */
896 ireason = IN_BYTE (IDE_NSECTOR_REG);
897 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
899 /* If DRQ is clear, the command has completed. */
900 if ((stat & DRQ_STAT) == 0) {
901 /* If we're not done filling the current buffer, complain.
902 Otherwise, complete the command normally. */
903 if (rq->current_nr_sectors > 0) {
904 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
905 drive->name, rq->current_nr_sectors);
906 cdrom_end_request (0, drive);
907 } else
908 cdrom_end_request (1, drive);
909 return ide_stopped;
912 /* Check that the drive is expecting to do the same thing we are. */
913 if (cdrom_read_check_ireason (drive, len, ireason))
914 return ide_stopped;
916 /* Assume that the drive will always provide data in multiples
917 of at least SECTOR_SIZE, as it gets hairy to keep track
918 of the transfers otherwise. */
919 if ((len % SECTOR_SIZE) != 0) {
920 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
921 drive->name, len);
922 if (CDROM_CONFIG_FLAGS (drive)->limit_nframes)
923 printk (" This drive is not supported by this version of the driver\n");
924 else {
925 printk (" Trying to limit transfer sizes\n");
926 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
928 cdrom_end_request (0, drive);
929 return ide_stopped;
932 /* The number of sectors we need to read from the drive. */
933 sectors_to_transfer = len / SECTOR_SIZE;
935 /* First, figure out if we need to bit-bucket
936 any of the leading sectors. */
937 nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
938 sectors_to_transfer);
940 while (nskip > 0) {
941 /* We need to throw away a sector. */
942 char dum[SECTOR_SIZE];
943 atapi_input_bytes (drive, dum, sizeof (dum));
945 --rq->current_nr_sectors;
946 --nskip;
947 --sectors_to_transfer;
950 /* Now loop while we still have data to read from the drive. */
951 while (sectors_to_transfer > 0) {
952 int this_transfer;
954 /* If we've filled the present buffer but there's another
955 chained buffer after it, move on. */
956 if (rq->current_nr_sectors == 0 &&
957 rq->nr_sectors > 0)
958 cdrom_end_request (1, drive);
960 /* If the buffers are full, cache the rest of the data in our
961 internal buffer. */
962 if (rq->current_nr_sectors == 0) {
963 cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
964 sectors_to_transfer = 0;
965 } else {
966 /* Transfer data to the buffers.
967 Figure out how many sectors we can transfer
968 to the current buffer. */
969 this_transfer = MIN (sectors_to_transfer,
970 rq->current_nr_sectors);
972 /* Read this_transfer sectors
973 into the current buffer. */
974 while (this_transfer > 0) {
975 atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
976 rq->buffer += SECTOR_SIZE;
977 --rq->nr_sectors;
978 --rq->current_nr_sectors;
979 ++rq->sector;
980 --this_transfer;
981 --sectors_to_transfer;
986 /* Done moving data!
987 Wait for another interrupt. */
988 ide_set_handler(drive, &cdrom_read_intr, WAIT_CMD, NULL);
989 return ide_started;
993 * Try to satisfy some of the current read request from our cached data.
994 * Returns nonzero if the request has been completed, zero otherwise.
996 static int cdrom_read_from_buffer (ide_drive_t *drive)
998 struct cdrom_info *info = drive->driver_data;
999 struct request *rq = HWGROUP(drive)->rq;
1001 /* Can't do anything if there's no buffer. */
1002 if (info->buffer == NULL) return 0;
1004 /* Loop while this request needs data and the next block is present
1005 in our cache. */
1006 while (rq->nr_sectors > 0 &&
1007 rq->sector >= info->sector_buffered &&
1008 rq->sector < info->sector_buffered + info->nsectors_buffered) {
1009 if (rq->current_nr_sectors == 0)
1010 cdrom_end_request (1, drive);
1012 memcpy (rq->buffer,
1013 info->buffer +
1014 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1015 SECTOR_SIZE);
1016 rq->buffer += SECTOR_SIZE;
1017 --rq->current_nr_sectors;
1018 --rq->nr_sectors;
1019 ++rq->sector;
1022 /* If we've satisfied the current request,
1023 terminate it successfully. */
1024 if (rq->nr_sectors == 0) {
1025 cdrom_end_request (1, drive);
1026 return -1;
1029 /* Move on to the next buffer if needed. */
1030 if (rq->current_nr_sectors == 0)
1031 cdrom_end_request (1, drive);
1033 /* If this condition does not hold, then the kluge i use to
1034 represent the number of sectors to skip at the start of a transfer
1035 will fail. I think that this will never happen, but let's be
1036 paranoid and check. */
1037 if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1038 (rq->sector % SECTORS_PER_FRAME) != 0) {
1039 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1040 drive->name, rq->sector);
1041 cdrom_end_request (0, drive);
1042 return -1;
1045 return 0;
1049 * Routine to send a read packet command to the drive.
1050 * This is usually called directly from cdrom_start_read.
1051 * However, for drq_interrupt devices, it is called from an interrupt
1052 * when the drive is ready to accept the command.
1054 static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1056 struct packet_command pc;
1057 struct request *rq = HWGROUP(drive)->rq;
1058 int nsect, sector, nframes, frame, nskip;
1060 /* Number of sectors to transfer. */
1061 nsect = rq->nr_sectors;
1063 /* Starting sector. */
1064 sector = rq->sector;
1066 /* If the requested sector doesn't start on a cdrom block boundary,
1067 we must adjust the start of the transfer so that it does,
1068 and remember to skip the first few sectors.
1069 If the CURRENT_NR_SECTORS field is larger than the size
1070 of the buffer, it will mean that we're to skip a number
1071 of sectors equal to the amount by which CURRENT_NR_SECTORS
1072 is larger than the buffer size. */
1073 nskip = (sector % SECTORS_PER_FRAME);
1074 if (nskip > 0) {
1075 /* Sanity check... */
1076 if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS) &&
1077 (rq->sector % CD_FRAMESIZE != 0)) {
1078 printk ("%s: cdrom_start_read_continuation: buffer botch (%lu)\n",
1079 drive->name, rq->current_nr_sectors);
1080 cdrom_end_request (0, drive);
1081 return ide_stopped;
1083 sector -= nskip;
1084 nsect += nskip;
1085 rq->current_nr_sectors += nskip;
1088 /* Convert from sectors to cdrom blocks, rounding up the transfer
1089 length if needed. */
1090 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1091 frame = sector / SECTORS_PER_FRAME;
1093 /* Largest number of frames was can transfer at once is 64k-1. For
1094 some drives we need to limit this even more. */
1095 nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ?
1096 (65534 / CD_FRAMESIZE) : 65535);
1098 /* Set up the command */
1099 memset (&pc.c, 0, sizeof (pc.c));
1100 pc.c[0] = GPCMD_READ_10;
1101 pc.c[7] = (nframes >> 8);
1102 pc.c[8] = (nframes & 0xff);
1103 put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1105 /* Send the command to the drive and return. */
1106 return cdrom_transfer_packet_command(drive, pc.c, sizeof(pc.c),
1107 &cdrom_read_intr, WAIT_CMD);
1111 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1112 #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
1113 #define IDECD_SEEK_TIMEOUT WAIT_CMD /* 10 sec */
1115 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1117 struct cdrom_info *info = drive->driver_data;
1118 int stat;
1119 static int retry = 10;
1120 ide_startstop_t startstop;
1122 if (cdrom_decode_status (&startstop, drive, 0, &stat))
1123 return startstop;
1124 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1126 if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {
1127 if (--retry == 0) {
1129 * this condition is far too common, to bother
1130 * users about it
1132 #if 0
1133 printk("%s: disabled DSC seek overlap\n", drive->name);
1134 #endif
1135 drive->dsc_overlap = 0;
1138 return ide_stopped;
1141 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1143 struct packet_command pc;
1144 struct request *rq = HWGROUP(drive)->rq;
1145 int sector, frame, nskip;
1147 sector = rq->sector;
1148 nskip = (sector % SECTORS_PER_FRAME);
1149 if (nskip > 0)
1150 sector -= nskip;
1151 frame = sector / SECTORS_PER_FRAME;
1153 memset (&pc.c, 0, sizeof (pc.c));
1154 pc.c[0] = GPCMD_SEEK;
1155 put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1156 return cdrom_transfer_packet_command(drive, pc.c, sizeof(pc.c), &cdrom_seek_intr, WAIT_CMD);
1159 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1161 struct cdrom_info *info = drive->driver_data;
1163 info->dma = 0;
1164 info->start_seek = jiffies;
1165 return cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);
1168 /* Fix up a possibly partially-processed request so that we can
1169 start it over entirely, or even put it back on the request queue. */
1170 static void restore_request (struct request *rq)
1172 if (rq->buffer != rq->bh->b_data) {
1173 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
1174 rq->buffer = rq->bh->b_data;
1175 rq->nr_sectors += n;
1176 rq->sector -= n;
1178 rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
1182 * Start a read request from the CD-ROM.
1184 static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1186 struct cdrom_info *info = drive->driver_data;
1187 struct request *rq = HWGROUP(drive)->rq;
1188 int minor = MINOR (rq->rq_dev);
1190 /* If the request is relative to a partition, fix it up to refer to the
1191 absolute address. */
1192 if ((minor & PARTN_MASK) != 0) {
1193 rq->sector = block;
1194 minor &= ~PARTN_MASK;
1195 rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1198 /* We may be retrying this request after an error. Fix up
1199 any weirdness which might be present in the request packet. */
1200 restore_request (rq);
1202 /* Satisfy whatever we can of this request from our cached sector. */
1203 if (cdrom_read_from_buffer(drive))
1204 return ide_stopped;
1206 /* Clear the local sector buffer. */
1207 info->nsectors_buffered = 0;
1209 /* use dma, if possible. */
1210 if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) &&
1211 (rq->nr_sectors % SECTORS_PER_FRAME == 0))
1212 info->dma = 1;
1213 else
1214 info->dma = 0;
1216 /* Start sending the read request to the drive. */
1217 return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1220 /****************************************************************************
1221 * Execute all other packet commands.
1224 /* Forward declarations. */
1225 static int cdrom_lockdoor(ide_drive_t *drive, int lockflag,
1226 struct request_sense *sense);
1228 /* Interrupt routine for packet command completion. */
1229 static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1231 int ireason, len, stat, thislen;
1232 struct request *rq = HWGROUP(drive)->rq;
1233 struct packet_command *pc = (struct packet_command *)rq->buffer;
1234 ide_startstop_t startstop;
1236 /* Check for errors. */
1237 if (cdrom_decode_status (&startstop, drive, 0, &stat))
1238 return startstop;
1240 /* Read the interrupt reason and the transfer length. */
1241 ireason = IN_BYTE (IDE_NSECTOR_REG);
1242 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1244 /* If DRQ is clear, the command has completed.
1245 Complain if we still have data left to transfer. */
1246 if ((stat & DRQ_STAT) == 0) {
1247 /* Some of the trailing request sense fields are optional, and
1248 some drives don't send them. Sigh. */
1249 if (pc->c[0] == GPCMD_REQUEST_SENSE &&
1250 pc->buflen > 0 &&
1251 pc->buflen <= 5) {
1252 while (pc->buflen > 0) {
1253 *pc->buffer++ = 0;
1254 --pc->buflen;
1258 if (pc->buflen == 0)
1259 cdrom_end_request (1, drive);
1260 else {
1261 /* Comment this out, because this always happens
1262 right after a reset occurs, and it is annoying to
1263 always print expected stuff. */
1265 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1266 drive->name, pc->buflen);
1268 pc->stat = 1;
1269 cdrom_end_request (1, drive);
1271 return ide_stopped;
1274 /* Figure out how much data to transfer. */
1275 thislen = pc->buflen;
1276 if (thislen > len) thislen = len;
1278 /* The drive wants to be written to. */
1279 if ((ireason & 3) == 0) {
1280 /* Transfer the data. */
1281 atapi_output_bytes (drive, pc->buffer, thislen);
1283 /* If we haven't moved enough data to satisfy the drive,
1284 add some padding. */
1285 while (len > thislen) {
1286 int dum = 0;
1287 atapi_output_bytes (drive, &dum, sizeof (dum));
1288 len -= sizeof (dum);
1291 /* Keep count of how much data we've moved. */
1292 pc->buffer += thislen;
1293 pc->buflen -= thislen;
1296 /* Same drill for reading. */
1297 else if ((ireason & 3) == 2) {
1299 /* Transfer the data. */
1300 atapi_input_bytes (drive, pc->buffer, thislen);
1302 /* If we haven't moved enough data to satisfy the drive,
1303 add some padding. */
1304 while (len > thislen) {
1305 int dum = 0;
1306 atapi_input_bytes (drive, &dum, sizeof (dum));
1307 len -= sizeof (dum);
1310 /* Keep count of how much data we've moved. */
1311 pc->buffer += thislen;
1312 pc->buflen -= thislen;
1313 } else {
1314 printk ("%s: cdrom_pc_intr: The drive "
1315 "appears confused (ireason = 0x%2x)\n",
1316 drive->name, ireason);
1317 pc->stat = 1;
1320 /* Now we wait for another interrupt. */
1321 ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD, cdrom_timer_expiry);
1322 return ide_started;
1326 static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1328 struct request *rq = HWGROUP(drive)->rq;
1329 struct packet_command *pc = (struct packet_command *)rq->buffer;
1331 if (!pc->timeout)
1332 pc->timeout = WAIT_CMD;
1334 /* Send the command to the drive and return. */
1335 return cdrom_transfer_packet_command(drive, pc->c, sizeof(pc->c),
1336 &cdrom_pc_intr, pc->timeout);
1340 static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1342 int len;
1343 struct request *rq = HWGROUP(drive)->rq;
1344 struct packet_command *pc = (struct packet_command *)rq->buffer;
1345 struct cdrom_info *info = drive->driver_data;
1347 info->dma = 0;
1348 pc->stat = 0;
1349 len = pc->buflen;
1351 /* Start sending the command to the drive. */
1352 return cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1356 /* Sleep for TIME jiffies.
1357 Not to be called from an interrupt handler. */
1358 static
1359 void cdrom_sleep (int time)
1361 int sleep = time;
1363 do {
1364 set_current_state(TASK_INTERRUPTIBLE);
1365 sleep = schedule_timeout(sleep);
1366 } while (sleep);
1369 static
1370 int cdrom_queue_packet_command(ide_drive_t *drive, struct packet_command *pc)
1372 struct request_sense sense;
1373 struct request req;
1374 int retries = 10;
1376 if (pc->sense == NULL)
1377 pc->sense = &sense;
1379 /* Start of retry loop. */
1380 do {
1381 ide_init_drive_cmd (&req);
1382 req.cmd = PACKET_COMMAND;
1383 req.buffer = (char *)pc;
1384 if (ide_do_drive_cmd (drive, &req, ide_wait)) {
1385 printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
1386 drive->name, req.buffer[0], req.buffer[1]);
1387 /* FIXME: we should probably abort/retry or something */
1389 if (pc->stat != 0) {
1390 /* The request failed. Retry if it was due to a unit
1391 attention status
1392 (usually means media was changed). */
1393 struct request_sense *reqbuf = pc->sense;
1395 if (reqbuf->sense_key == UNIT_ATTENTION)
1396 cdrom_saw_media_change (drive);
1397 else if (reqbuf->sense_key == NOT_READY &&
1398 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1399 /* The drive is in the process of loading
1400 a disk. Retry, but wait a little to give
1401 the drive time to complete the load. */
1402 cdrom_sleep(2 * HZ);
1403 } else {
1404 /* Otherwise, don't retry. */
1405 retries = 0;
1407 --retries;
1410 /* End of retry loop. */
1411 } while (pc->stat != 0 && retries >= 0);
1413 /* Return an error if the command failed. */
1414 return pc->stat ? -EIO : 0;
1417 /****************************************************************************
1418 * cdrom driver request routine.
1420 static ide_startstop_t
1421 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, unsigned long block)
1423 ide_startstop_t action;
1424 struct cdrom_info *info = drive->driver_data;
1426 switch (rq->cmd) {
1427 case READ: {
1428 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1429 unsigned long elpased = jiffies - info->start_seek;
1430 int stat = GET_STAT();
1432 if ((stat & SEEK_STAT) != SEEK_STAT) {
1433 if (elpased < IDECD_SEEK_TIMEOUT) {
1434 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1435 return ide_stopped;
1437 printk ("%s: DSC timeout\n", drive->name);
1439 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1441 if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap)
1442 action = cdrom_start_seek (drive, block);
1443 else
1444 action = cdrom_start_read (drive, block);
1445 info->last_block = block;
1446 return action;
1449 case PACKET_COMMAND:
1450 case REQUEST_SENSE_COMMAND: {
1451 return cdrom_do_packet_command(drive);
1454 case RESET_DRIVE_COMMAND: {
1455 cdrom_end_request(1, drive);
1456 return ide_do_reset(drive);
1459 default: {
1460 printk("ide-cd: bad cmd %d\n", rq -> cmd);
1461 cdrom_end_request(0, drive);
1462 return ide_stopped;
1469 /****************************************************************************
1470 * Ioctl handling.
1472 * Routines which queue packet commands take as a final argument a pointer
1473 * to a request_sense struct. If execution of the command results
1474 * in an error with a CHECK CONDITION status, this structure will be filled
1475 * with the results of the subsequent request sense command. The pointer
1476 * can also be NULL, in which case no sense information is returned.
1479 #if ! STANDARD_ATAPI
1480 static inline
1481 int bin2bcd (int x)
1483 return (x%10) | ((x/10) << 4);
1487 static inline
1488 int bcd2bin (int x)
1490 return (x >> 4) * 10 + (x & 0x0f);
1493 static
1494 void msf_from_bcd (struct atapi_msf *msf)
1496 msf->minute = bcd2bin (msf->minute);
1497 msf->second = bcd2bin (msf->second);
1498 msf->frame = bcd2bin (msf->frame);
1501 #endif /* not STANDARD_ATAPI */
1504 static inline
1505 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1507 lba += CD_MSF_OFFSET;
1508 lba &= 0xffffff; /* negative lbas use only 24 bits */
1509 *m = lba / (CD_SECS * CD_FRAMES);
1510 lba %= (CD_SECS * CD_FRAMES);
1511 *s = lba / CD_FRAMES;
1512 *f = lba % CD_FRAMES;
1516 static inline
1517 int msf_to_lba (byte m, byte s, byte f)
1519 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1522 static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1524 struct packet_command pc;
1525 struct cdrom_info *info = drive->driver_data;
1526 struct cdrom_device_info *cdi = &info->devinfo;
1528 memset(&pc, 0, sizeof(pc));
1529 pc.sense = sense;
1531 pc.c[0] = GPCMD_TEST_UNIT_READY;
1533 #if ! STANDARD_ATAPI
1534 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1535 switch CDs instead of supporting the LOAD_UNLOAD opcode */
1537 pc.c[7] = cdi->sanyo_slot % 3;
1538 #endif /* not STANDARD_ATAPI */
1540 return cdrom_queue_packet_command(drive, &pc);
1544 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1545 static int
1546 cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
1548 struct request_sense my_sense;
1549 struct packet_command pc;
1550 int stat;
1552 if (sense == NULL)
1553 sense = &my_sense;
1555 /* If the drive cannot lock the door, just pretend. */
1556 if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
1557 stat = 0;
1558 } else {
1559 memset(&pc, 0, sizeof(pc));
1560 pc.sense = sense;
1561 pc.c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1562 pc.c[4] = lockflag ? 1 : 0;
1563 stat = cdrom_queue_packet_command (drive, &pc);
1566 /* If we got an illegal field error, the drive
1567 probably cannot lock the door. */
1568 if (stat != 0 &&
1569 sense->sense_key == ILLEGAL_REQUEST &&
1570 (sense->asc == 0x24 || sense->asc == 0x20)) {
1571 printk ("%s: door locking not supported\n",
1572 drive->name);
1573 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1574 stat = 0;
1577 /* no medium, that's alright. */
1578 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
1579 stat = 0;
1581 if (stat == 0)
1582 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1584 return stat;
1588 /* Eject the disk if EJECTFLAG is 0.
1589 If EJECTFLAG is 1, try to reload the disk. */
1590 static int cdrom_eject(ide_drive_t *drive, int ejectflag,
1591 struct request_sense *sense)
1593 struct packet_command pc;
1595 if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
1596 return -EDRIVE_CANT_DO_THIS;
1598 /* reload fails on some drives, if the tray is locked */
1599 if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
1600 return 0;
1602 memset(&pc, 0, sizeof (pc));
1603 pc.sense = sense;
1605 pc.c[0] = GPCMD_START_STOP_UNIT;
1606 pc.c[4] = 0x02 + (ejectflag != 0);
1607 return cdrom_queue_packet_command (drive, &pc);
1610 static int cdrom_read_capacity(ide_drive_t *drive, unsigned *capacity,
1611 struct request_sense *sense)
1613 struct {
1614 __u32 lba;
1615 __u32 blocklen;
1616 } capbuf;
1618 int stat;
1619 struct packet_command pc;
1621 memset(&pc, 0, sizeof(pc));
1622 pc.sense = sense;
1624 pc.c[0] = GPCMD_READ_CDVD_CAPACITY;
1625 pc.buffer = (char *)&capbuf;
1626 pc.buflen = sizeof(capbuf);
1628 stat = cdrom_queue_packet_command(drive, &pc);
1629 if (stat == 0)
1630 *capacity = 1 + be32_to_cpu(capbuf.lba);
1632 return stat;
1635 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1636 int format, char *buf, int buflen,
1637 struct request_sense *sense)
1639 struct packet_command pc;
1641 memset(&pc, 0, sizeof(pc));
1642 pc.sense = sense;
1644 pc.buffer = buf;
1645 pc.buflen = buflen;
1646 pc.c[0] = GPCMD_READ_TOC_PMA_ATIP;
1647 pc.c[6] = trackno;
1648 pc.c[7] = (buflen >> 8);
1649 pc.c[8] = (buflen & 0xff);
1650 pc.c[9] = (format << 6);
1652 if (msf_flag)
1653 pc.c[1] = 2;
1655 return cdrom_queue_packet_command (drive, &pc);
1659 /* Try to read the entire TOC for the disk into our internal buffer. */
1660 static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
1662 int minor, stat, ntracks, i;
1663 kdev_t dev;
1664 struct cdrom_info *info = drive->driver_data;
1665 struct atapi_toc *toc = info->toc;
1666 struct {
1667 struct atapi_toc_header hdr;
1668 struct atapi_toc_entry ent;
1669 } ms_tmp;
1671 if (toc == NULL) {
1672 /* Try to allocate space. */
1673 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1674 GFP_KERNEL);
1675 info->toc = toc;
1676 if (toc == NULL) {
1677 printk ("%s: No cdrom TOC buffer!\n", drive->name);
1678 return -ENOMEM;
1682 /* Check to see if the existing data is still valid.
1683 If it is, just return. */
1684 (void) cdrom_check_status(drive, sense);
1686 if (CDROM_STATE_FLAGS(drive)->toc_valid)
1687 return 0;
1689 /* First read just the header, so we know how long the TOC is. */
1690 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1691 sizeof(struct atapi_toc_header), sense);
1692 if (stat) return stat;
1694 #if ! STANDARD_ATAPI
1695 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1696 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1697 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1699 #endif /* not STANDARD_ATAPI */
1701 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1702 if (ntracks <= 0)
1703 return -EIO;
1704 if (ntracks > MAX_TRACKS)
1705 ntracks = MAX_TRACKS;
1707 /* Now read the whole schmeer. */
1708 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1709 (char *)&toc->hdr,
1710 sizeof(struct atapi_toc_header) +
1711 (ntracks + 1) *
1712 sizeof(struct atapi_toc_entry), sense);
1714 if (stat && toc->hdr.first_track > 1) {
1715 /* Cds with CDI tracks only don't have any TOC entries,
1716 despite of this the returned values are
1717 first_track == last_track = number of CDI tracks + 1,
1718 so that this case is indistinguishable from the same
1719 layout plus an additional audio track.
1720 If we get an error for the regular case, we assume
1721 a CDI without additional audio tracks. In this case
1722 the readable TOC is empty (CDI tracks are not included)
1723 and only holds the Leadout entry. Heiko Eißfeldt */
1724 ntracks = 0;
1725 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1726 (char *)&toc->hdr,
1727 sizeof(struct atapi_toc_header) +
1728 (ntracks + 1) *
1729 sizeof(struct atapi_toc_entry),
1730 sense);
1731 if (stat) {
1732 return stat;
1734 #if ! STANDARD_ATAPI
1735 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1736 toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
1737 toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
1738 } else
1739 #endif /* not STANDARD_ATAPI */
1741 toc->hdr.first_track = CDROM_LEADOUT;
1742 toc->hdr.last_track = CDROM_LEADOUT;
1746 if (stat)
1747 return stat;
1749 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1751 #if ! STANDARD_ATAPI
1752 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1753 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1754 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1756 #endif /* not STANDARD_ATAPI */
1758 for (i=0; i<=ntracks; i++) {
1759 #if ! STANDARD_ATAPI
1760 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1761 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1762 toc->ent[i].track = bcd2bin (toc->ent[i].track);
1763 msf_from_bcd (&toc->ent[i].addr.msf);
1765 #endif /* not STANDARD_ATAPI */
1766 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1767 toc->ent[i].addr.msf.second,
1768 toc->ent[i].addr.msf.frame);
1771 /* Read the multisession information. */
1772 if (toc->hdr.first_track != CDROM_LEADOUT) {
1773 /* Read the multisession information. */
1774 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1775 sizeof(ms_tmp), sense);
1776 if (stat) return stat;
1777 } else {
1778 ms_tmp.ent.addr.msf.minute = 0;
1779 ms_tmp.ent.addr.msf.second = 2;
1780 ms_tmp.ent.addr.msf.frame = 0;
1781 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
1784 #if ! STANDARD_ATAPI
1785 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1786 msf_from_bcd (&ms_tmp.ent.addr.msf);
1787 #endif /* not STANDARD_ATAPI */
1789 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1790 ms_tmp.ent.addr.msf.second,
1791 ms_tmp.ent.addr.msf.frame);
1793 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1795 /* Now try to get the total cdrom capacity. */
1796 minor = (drive->select.b.unit) << PARTN_BITS;
1797 dev = MKDEV(HWIF(drive)->major, minor);
1798 stat = cdrom_get_last_written(dev, (long *)&toc->capacity);
1799 if (stat)
1800 stat = cdrom_read_capacity(drive, &toc->capacity, sense);
1801 if (stat)
1802 toc->capacity = 0x1fffff;
1804 HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS] = (toc->capacity * SECTORS_PER_FRAME) >> (BLOCK_SIZE_BITS - 9);
1805 drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1807 /* Remember that we've read this stuff. */
1808 CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1810 return 0;
1814 static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
1815 int buflen, struct request_sense *sense)
1817 struct packet_command pc;
1819 memset(&pc, 0, sizeof(pc));
1820 pc.sense = sense;
1822 pc.buffer = buf;
1823 pc.buflen = buflen;
1824 pc.c[0] = GPCMD_READ_SUBCHANNEL;
1825 pc.c[1] = 2; /* MSF addressing */
1826 pc.c[2] = 0x40; /* request subQ data */
1827 pc.c[3] = format;
1828 pc.c[7] = (buflen >> 8);
1829 pc.c[8] = (buflen & 0xff);
1830 return cdrom_queue_packet_command(drive, &pc);
1833 /* ATAPI cdrom drives are free to select the speed you request or any slower
1834 rate :-( Requesting too fast a speed will _not_ produce an error. */
1835 static int cdrom_select_speed(ide_drive_t *drive, int speed,
1836 struct request_sense *sense)
1838 struct packet_command pc;
1839 memset(&pc, 0, sizeof(pc));
1840 pc.sense = sense;
1842 if (speed == 0)
1843 speed = 0xffff; /* set to max */
1844 else
1845 speed *= 177; /* Nx to kbytes/s */
1847 pc.c[0] = GPCMD_SET_SPEED;
1848 /* Read Drive speed in kbytes/second MSB */
1849 pc.c[2] = (speed >> 8) & 0xff;
1850 /* Read Drive speed in kbytes/second LSB */
1851 pc.c[3] = speed & 0xff;
1852 if ( CDROM_CONFIG_FLAGS(drive)->cd_r ||
1853 CDROM_CONFIG_FLAGS(drive)->cd_rw ) {
1854 /* Write Drive speed in kbytes/second MSB */
1855 pc.c[4] = (speed >> 8) & 0xff;
1856 /* Write Drive speed in kbytes/second LSB */
1857 pc.c[5] = speed & 0xff;
1860 return cdrom_queue_packet_command(drive, &pc);
1863 static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
1865 struct request_sense sense;
1866 struct packet_command pc;
1868 memset(&pc, 0, sizeof (pc));
1869 pc.sense = &sense;
1871 pc.c[0] = GPCMD_PLAY_AUDIO_MSF;
1872 lba_to_msf(lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1873 lba_to_msf(lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1875 return cdrom_queue_packet_command(drive, &pc);
1878 static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
1879 struct atapi_toc_entry **ent)
1881 struct cdrom_info *info = drive->driver_data;
1882 struct atapi_toc *toc = info->toc;
1883 int ntracks;
1886 * don't serve cached data, if the toc isn't valid
1888 if (!CDROM_STATE_FLAGS(drive)->toc_valid)
1889 return -EINVAL;
1891 /* Check validity of requested track number. */
1892 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1893 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
1894 if (track == CDROM_LEADOUT)
1895 *ent = &toc->ent[ntracks];
1896 else if (track < toc->hdr.first_track ||
1897 track > toc->hdr.last_track)
1898 return -EINVAL;
1899 else
1900 *ent = &toc->ent[track - toc->hdr.first_track];
1902 return 0;
1909 /* the generic packet interface to cdrom.c */
1910 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
1911 struct cdrom_generic_command *cgc)
1913 struct packet_command pc;
1914 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
1916 if (cgc->timeout <= 0)
1917 cgc->timeout = WAIT_CMD;
1919 /* here we queue the commands from the uniform CD-ROM
1920 layer. the packet must be complete, as we do not
1921 touch it at all. */
1922 memset(&pc, 0, sizeof(pc));
1923 memcpy(pc.c, cgc->cmd, CDROM_PACKET_SIZE);
1924 pc.buffer = cgc->buffer;
1925 pc.buflen = cgc->buflen;
1926 pc.quiet = cgc->quiet;
1927 pc.timeout = cgc->timeout;
1928 pc.sense = cgc->sense;
1929 return cgc->stat = cdrom_queue_packet_command(drive, &pc);
1932 static
1933 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
1934 unsigned int cmd, unsigned long arg)
1936 struct cdrom_generic_command cgc;
1937 char buffer[16];
1938 int stat;
1940 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1942 /* These will be moved into the Uniform layer shortly... */
1943 switch (cmd) {
1944 case CDROMSETSPINDOWN: {
1945 char spindown;
1947 if (copy_from_user(&spindown, (void *) arg, sizeof(char)))
1948 return -EFAULT;
1950 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
1951 return stat;
1953 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1955 return cdrom_mode_select(cdi, &cgc);
1958 case CDROMGETSPINDOWN: {
1959 char spindown;
1961 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
1962 return stat;
1964 spindown = buffer[11] & 0x0f;
1966 if (copy_to_user((void *) arg, &spindown, sizeof (char)))
1967 return -EFAULT;
1969 return 0;
1972 default:
1973 return -EINVAL;
1978 static
1979 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
1980 unsigned int cmd, void *arg)
1983 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
1984 struct cdrom_info *info = drive->driver_data;
1985 int stat;
1987 switch (cmd) {
1989 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
1990 * atapi doesn't support it
1992 case CDROMPLAYTRKIND: {
1993 unsigned long lba_start, lba_end;
1994 struct cdrom_ti *ti = (struct cdrom_ti *)arg;
1995 struct atapi_toc_entry *first_toc, *last_toc;
1997 stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
1998 if (stat)
1999 return stat;
2001 stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2002 if (stat)
2003 return stat;
2005 if (ti->cdti_trk1 != CDROM_LEADOUT)
2006 ++last_toc;
2007 lba_start = first_toc->addr.lba;
2008 lba_end = last_toc->addr.lba;
2010 if (lba_end <= lba_start)
2011 return -EINVAL;
2013 return cdrom_play_audio(drive, lba_start, lba_end);
2016 case CDROMREADTOCHDR: {
2017 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2018 struct atapi_toc *toc;
2020 /* Make sure our saved TOC is valid. */
2021 stat = cdrom_read_toc(drive, NULL);
2022 if (stat) return stat;
2024 toc = info->toc;
2025 tochdr->cdth_trk0 = toc->hdr.first_track;
2026 tochdr->cdth_trk1 = toc->hdr.last_track;
2028 return 0;
2031 case CDROMREADTOCENTRY: {
2032 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
2033 struct atapi_toc_entry *toce;
2035 stat = cdrom_get_toc_entry (drive, tocentry->cdte_track, &toce);
2036 if (stat) return stat;
2038 tocentry->cdte_ctrl = toce->control;
2039 tocentry->cdte_adr = toce->adr;
2040 if (tocentry->cdte_format == CDROM_MSF) {
2041 lba_to_msf (toce->addr.lba,
2042 &tocentry->cdte_addr.msf.minute,
2043 &tocentry->cdte_addr.msf.second,
2044 &tocentry->cdte_addr.msf.frame);
2045 } else
2046 tocentry->cdte_addr.lba = toce->addr.lba;
2048 return 0;
2051 default:
2052 return -EINVAL;
2056 static
2057 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2059 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2060 struct request_sense sense;
2061 struct request req;
2062 int ret;
2064 ide_init_drive_cmd (&req);
2065 req.cmd = RESET_DRIVE_COMMAND;
2066 ret = ide_do_drive_cmd(drive, &req, ide_wait);
2069 * A reset will unlock the door. If it was previously locked,
2070 * lock it again.
2072 if (CDROM_STATE_FLAGS(drive)->door_locked)
2073 (void) cdrom_lockdoor(drive, 1, &sense);
2075 return ret;
2079 static
2080 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2082 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2083 struct request_sense sense;
2085 if (position) {
2086 int stat = cdrom_lockdoor(drive, 0, &sense);
2087 if (stat) return stat;
2090 return cdrom_eject(drive, !position, &sense);
2093 static
2094 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2096 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2097 return cdrom_lockdoor(drive, lock, NULL);
2100 static
2101 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2103 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2104 struct request_sense sense;
2105 int stat;
2107 if ((stat = cdrom_select_speed (drive, speed, &sense)) < 0)
2108 return stat;
2110 cdi->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2111 return 0;
2114 static
2115 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2117 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2119 if (slot_nr == CDSL_CURRENT) {
2120 struct request_sense sense;
2121 int stat = cdrom_check_status(drive, &sense);
2122 if (stat == 0 || sense.sense_key == UNIT_ATTENTION)
2123 return CDS_DISC_OK;
2125 if (sense.sense_key == NOT_READY && sense.asc == 0x04 &&
2126 sense.ascq == 0x04)
2127 return CDS_DISC_OK;
2129 if (sense.sense_key == NOT_READY) {
2130 /* ATAPI doesn't have anything that can help
2131 us decide whether the drive is really
2132 emtpy or the tray is just open. irk. */
2133 return CDS_TRAY_OPEN;
2136 return CDS_DRIVE_NOT_READY;
2138 return -EINVAL;
2141 static
2142 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2143 struct cdrom_multisession *ms_info)
2145 struct atapi_toc *toc;
2146 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2147 struct cdrom_info *info = drive->driver_data;
2148 struct request_sense sense;
2149 int ret;
2151 if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2152 if ((ret = cdrom_read_toc(drive, &sense)))
2153 return ret;
2155 toc = info->toc;
2156 ms_info->addr.lba = toc->last_session_lba;
2157 ms_info->xa_flag = toc->xa_flag;
2159 return 0;
2162 static
2163 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2164 struct cdrom_mcn *mcn_info)
2166 int stat;
2167 char mcnbuf[24];
2168 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2170 /* get MCN */
2171 if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2172 return stat;
2174 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2175 sizeof (mcn_info->medium_catalog_number)-1);
2176 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2177 = '\0';
2179 return 0;
2184 /****************************************************************************
2185 * Other driver requests (open, close, check media change).
2188 static
2189 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2190 int slot_nr)
2192 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2193 int retval;
2195 if (slot_nr == CDSL_CURRENT) {
2196 (void) cdrom_check_status(drive, NULL);
2197 retval = CDROM_STATE_FLAGS (drive)->media_changed;
2198 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2199 return retval;
2200 } else {
2201 return -EINVAL;
2206 static
2207 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2209 return 0;
2214 * Close down the device. Invalidate all cached blocks.
2217 static
2218 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2224 /****************************************************************************
2225 * Device initialization.
2227 static struct cdrom_device_ops ide_cdrom_dops = {
2228 open: ide_cdrom_open_real,
2229 release: ide_cdrom_release_real,
2230 drive_status: ide_cdrom_drive_status,
2231 media_changed: ide_cdrom_check_media_change_real,
2232 tray_move: ide_cdrom_tray_move,
2233 lock_door: ide_cdrom_lock_door,
2234 select_speed: ide_cdrom_select_speed,
2235 get_last_session: ide_cdrom_get_last_session,
2236 get_mcn: ide_cdrom_get_mcn,
2237 reset: ide_cdrom_reset,
2238 audio_ioctl: ide_cdrom_audio_ioctl,
2239 dev_ioctl: ide_cdrom_dev_ioctl,
2240 capability: CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2241 CDC_SELECT_SPEED | CDC_SELECT_DISC |
2242 CDC_MULTI_SESSION | CDC_MCN |
2243 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
2244 CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
2245 CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2246 CDC_GENERIC_PACKET,
2247 generic_packet: ide_cdrom_packet,
2250 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2252 struct cdrom_info *info = drive->driver_data;
2253 struct cdrom_device_info *devinfo = &info->devinfo;
2254 int minor = (drive->select.b.unit) << PARTN_BITS;
2256 devinfo->dev = MKDEV (HWIF(drive)->major, minor);
2257 devinfo->ops = &ide_cdrom_dops;
2258 devinfo->mask = 0;
2259 *(int *)&devinfo->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2260 *(int *)&devinfo->capacity = nslots;
2261 devinfo->handle = (void *) drive;
2262 strcpy(devinfo->name, drive->name);
2264 /* set capability mask to match the probe. */
2265 if (!CDROM_CONFIG_FLAGS (drive)->cd_r)
2266 devinfo->mask |= CDC_CD_R;
2267 if (!CDROM_CONFIG_FLAGS (drive)->cd_rw)
2268 devinfo->mask |= CDC_CD_RW;
2269 if (!CDROM_CONFIG_FLAGS (drive)->dvd)
2270 devinfo->mask |= CDC_DVD;
2271 if (!CDROM_CONFIG_FLAGS (drive)->dvd_r)
2272 devinfo->mask |= CDC_DVD_R;
2273 if (!CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2274 devinfo->mask |= CDC_DVD_RAM;
2275 if (!CDROM_CONFIG_FLAGS (drive)->is_changer)
2276 devinfo->mask |= CDC_SELECT_DISC;
2277 if (!CDROM_CONFIG_FLAGS (drive)->audio_play)
2278 devinfo->mask |= CDC_PLAY_AUDIO;
2279 if (!CDROM_CONFIG_FLAGS (drive)->close_tray)
2280 devinfo->mask |= CDC_CLOSE_TRAY;
2282 devinfo->de = devfs_register(drive->de, "cd", DEVFS_FL_DEFAULT,
2283 HWIF(drive)->major, minor,
2284 S_IFBLK | S_IRUGO | S_IWUGO,
2285 ide_fops, NULL);
2287 return register_cdrom(devinfo);
2290 static
2291 int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2293 struct cdrom_info *info = drive->driver_data;
2294 struct cdrom_device_info *cdi = &info->devinfo;
2295 struct cdrom_generic_command cgc;
2296 int stat, attempts = 3, size = sizeof(*cap);
2299 * ACER50 (and others?) require the full spec length mode sense
2300 * page capabilities size, but older drives break.
2302 if (drive->id) {
2303 if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2304 !strcmp(drive->id->model, "WPI CDS-32X")))
2305 size -= sizeof(cap->pad);
2308 /* we have to cheat a little here. the packet will eventually
2309 * be queued with ide_cdrom_packet(), which extracts the
2310 * drive from cdi->handle. Since this device hasn't been
2311 * registered with the Uniform layer yet, it can't do this.
2312 * Same goes for cdi->ops.
2314 cdi->handle = (ide_drive_t *) drive;
2315 cdi->ops = &ide_cdrom_dops;
2316 init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2317 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2318 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2319 if (!stat)
2320 break;
2321 } while (--attempts);
2322 return stat;
2325 static
2326 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2328 struct cdrom_info *info = drive->driver_data;
2329 struct cdrom_device_info *cdi = &info->devinfo;
2330 struct atapi_capabilities_page cap;
2331 int nslots = 1;
2333 if (CDROM_CONFIG_FLAGS (drive)->nec260) {
2334 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2335 CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
2336 return nslots;
2339 if (ide_cdrom_get_capabilities(drive, &cap))
2340 return 0;
2342 if (cap.lock == 0)
2343 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2344 if (cap.eject)
2345 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2346 if (cap.cd_r_write)
2347 CDROM_CONFIG_FLAGS (drive)->cd_r = 1;
2348 if (cap.cd_rw_write)
2349 CDROM_CONFIG_FLAGS (drive)->cd_rw = 1;
2350 if (cap.test_write)
2351 CDROM_CONFIG_FLAGS (drive)->test_write = 1;
2352 if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2353 CDROM_CONFIG_FLAGS (drive)->dvd = 1;
2354 if (cap.dvd_ram_write)
2355 CDROM_CONFIG_FLAGS (drive)->dvd_ram = 1;
2356 if (cap.dvd_r_write)
2357 CDROM_CONFIG_FLAGS (drive)->dvd_r = 1;
2358 if (cap.audio_play)
2359 CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
2360 if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2361 CDROM_CONFIG_FLAGS (drive)->close_tray = 0;
2363 #if ! STANDARD_ATAPI
2364 if (cdi->sanyo_slot > 0) {
2365 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2366 nslots = 3;
2369 else
2370 #endif /* not STANDARD_ATAPI */
2371 if (cap.mechtype == mechtype_individual_changer ||
2372 cap.mechtype == mechtype_cartridge_changer) {
2373 if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2374 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2375 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 1;
2379 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2380 if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2381 CDROM_STATE_FLAGS (drive)->current_speed =
2382 (((unsigned int)cap.curspeed) + (176/2)) / 176;
2383 CDROM_CONFIG_FLAGS (drive)->max_speed =
2384 (((unsigned int)cap.maxspeed) + (176/2)) / 176;
2385 } else {
2386 CDROM_STATE_FLAGS (drive)->current_speed =
2387 (ntohs(cap.curspeed) + (176/2)) / 176;
2388 CDROM_CONFIG_FLAGS (drive)->max_speed =
2389 (ntohs(cap.maxspeed) + (176/2)) / 176;
2392 /* don't print speed if the drive reported 0.
2394 printk("%s: ATAPI", drive->name);
2395 if (CDROM_CONFIG_FLAGS(drive)->max_speed)
2396 printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
2397 printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
2399 if (CDROM_CONFIG_FLAGS (drive)->dvd_r|CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2400 printk (" DVD%s%s",
2401 (CDROM_CONFIG_FLAGS (drive)->dvd_r)? "-R" : "",
2402 (CDROM_CONFIG_FLAGS (drive)->dvd_ram)? "AM" : "");
2404 if (CDROM_CONFIG_FLAGS (drive)->cd_r|CDROM_CONFIG_FLAGS (drive)->cd_rw)
2405 printk (" CD%s%s",
2406 (CDROM_CONFIG_FLAGS (drive)->cd_r)? "-R" : "",
2407 (CDROM_CONFIG_FLAGS (drive)->cd_rw)? "/RW" : "");
2409 if (CDROM_CONFIG_FLAGS (drive)->is_changer)
2410 printk (" changer w/%d slots", nslots);
2411 else
2412 printk (" drive");
2414 printk (", %dkB Cache", be16_to_cpu(cap.buffer_size));
2416 #ifdef CONFIG_BLK_DEV_IDEDMA
2417 if (drive->using_dma)
2418 (void) HWIF(drive)->dmaproc(ide_dma_verbose, drive);
2419 #endif /* CONFIG_BLK_DEV_IDEDMA */
2420 printk("\n");
2422 return nslots;
2425 static void ide_cdrom_add_settings(ide_drive_t *drive)
2427 int major = HWIF(drive)->major;
2428 int minor = drive->select.b.unit << PARTN_BITS;
2430 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
2431 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
2432 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
2433 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2436 static
2437 int ide_cdrom_setup (ide_drive_t *drive)
2439 struct cdrom_info *info = drive->driver_data;
2440 struct cdrom_device_info *cdi = &info->devinfo;
2441 int minor = drive->select.b.unit << PARTN_BITS;
2442 int nslots;
2444 set_device_ro(MKDEV(HWIF(drive)->major, minor), 1);
2445 set_blocksize(MKDEV(HWIF(drive)->major, minor), CD_FRAMESIZE);
2447 drive->special.all = 0;
2448 drive->ready_stat = 0;
2450 CDROM_STATE_FLAGS (drive)->media_changed = 1;
2451 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
2452 CDROM_STATE_FLAGS (drive)->door_locked = 0;
2454 #if NO_DOOR_LOCKING
2455 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2456 #else
2457 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2458 #endif
2460 if (drive->id != NULL)
2461 CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2462 ((drive->id->config & 0x0060) == 0x20);
2463 else
2464 CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2466 CDROM_CONFIG_FLAGS (drive)->is_changer = 0;
2467 CDROM_CONFIG_FLAGS (drive)->cd_r = 0;
2468 CDROM_CONFIG_FLAGS (drive)->cd_rw = 0;
2469 CDROM_CONFIG_FLAGS (drive)->test_write = 0;
2470 CDROM_CONFIG_FLAGS (drive)->dvd = 0;
2471 CDROM_CONFIG_FLAGS (drive)->dvd_r = 0;
2472 CDROM_CONFIG_FLAGS (drive)->dvd_ram = 0;
2473 CDROM_CONFIG_FLAGS (drive)->no_eject = 1;
2474 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 0;
2475 CDROM_CONFIG_FLAGS (drive)->audio_play = 0;
2476 CDROM_CONFIG_FLAGS (drive)->close_tray = 1;
2478 /* limit transfer size per interrupt. */
2479 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 0;
2480 if (drive->id != NULL) {
2481 /* a testament to the nice quality of Samsung drives... */
2482 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
2483 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2484 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
2485 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2486 /* the 3231 model does not support the SET_CD_SPEED command */
2487 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
2488 cdi->mask |= CDC_SELECT_SPEED;
2491 #if ! STANDARD_ATAPI
2492 /* by default Sanyo 3 CD changer support is turned off and
2493 ATAPI Rev 2.2+ standard support for CD changers is used */
2494 cdi->sanyo_slot = 0;
2496 CDROM_CONFIG_FLAGS (drive)->nec260 = 0;
2497 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2498 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2499 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2500 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2502 if (drive->id != NULL) {
2503 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2504 drive->id->fw_rev[4] == '1' &&
2505 drive->id->fw_rev[6] <= '2') {
2506 /* Vertos 300.
2507 Some versions of this drive like to talk BCD. */
2508 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2509 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2510 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2511 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2514 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2515 drive->id->fw_rev[4] == '1' &&
2516 drive->id->fw_rev[6] <= '2') {
2517 /* Vertos 600 ESD. */
2518 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2521 else if (strcmp (drive->id->model,
2522 "NEC CD-ROM DRIVE:260") == 0 &&
2523 strncmp (drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
2524 /* Old NEC260 (not R).
2525 This drive was released before the 1.2 version
2526 of the spec. */
2527 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2528 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2529 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2530 CDROM_CONFIG_FLAGS (drive)->nec260 = 1;
2533 else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
2534 strncmp (drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
2535 /* Wearnes */
2536 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2537 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2540 /* Sanyo 3 CD changer uses a non-standard command
2541 for CD changing */
2542 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
2543 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
2544 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
2545 /* uses CD in slot 0 when value is set to 3 */
2546 cdi->sanyo_slot = 3;
2551 #endif /* not STANDARD_ATAPI */
2553 info->toc = NULL;
2554 info->buffer = NULL;
2555 info->sector_buffered = 0;
2556 info->nsectors_buffered = 0;
2557 info->changer_info = NULL;
2558 info->last_block = 0;
2559 info->start_seek = 0;
2561 nslots = ide_cdrom_probe_capabilities (drive);
2563 if (ide_cdrom_register (drive, nslots)) {
2564 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2565 info->devinfo.handle = NULL;
2566 return 1;
2568 ide_cdrom_add_settings(drive);
2569 return 0;
2572 /* Forwarding functions to generic routines. */
2573 static
2574 int ide_cdrom_ioctl (ide_drive_t *drive,
2575 struct inode *inode, struct file *file,
2576 unsigned int cmd, unsigned long arg)
2578 return cdrom_fops.ioctl (inode, file, cmd, arg);
2581 static
2582 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2584 struct cdrom_info *info = drive->driver_data;
2585 int rc;
2587 MOD_INC_USE_COUNT;
2588 if (info->buffer == NULL)
2589 info->buffer = (char *) kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL);
2590 if ((rc = cdrom_fops.open(ip, fp))) {
2591 drive->usage--;
2592 MOD_DEC_USE_COUNT;
2594 return rc;
2597 static
2598 void ide_cdrom_release (struct inode *inode, struct file *file,
2599 ide_drive_t *drive)
2601 cdrom_fops.release (inode, file);
2602 MOD_DEC_USE_COUNT;
2605 static
2606 int ide_cdrom_check_media_change (ide_drive_t *drive)
2608 return cdrom_fops.check_media_change(MKDEV (HWIF (drive)->major,
2609 (drive->select.b.unit) << PARTN_BITS));
2612 static
2613 void ide_cdrom_revalidate (ide_drive_t *drive)
2615 struct cdrom_info *info = drive->driver_data;
2616 struct atapi_toc *toc;
2617 int minor = drive->select.b.unit << PARTN_BITS;
2618 struct request_sense sense;
2620 cdrom_read_toc(drive, &sense);
2622 if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2623 return;
2625 toc = info->toc;
2627 /* for general /dev/cdrom like mounting, one big disc */
2628 drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
2629 HWIF(drive)->gd->sizes[minor] = toc->capacity * BLOCKS_PER_FRAME;
2631 blk_size[HWIF(drive)->major] = HWIF(drive)->gd->sizes;
2634 static
2635 unsigned long ide_cdrom_capacity (ide_drive_t *drive)
2637 unsigned capacity;
2639 if (cdrom_read_capacity(drive, &capacity, NULL))
2640 return 0;
2642 return capacity * SECTORS_PER_FRAME;
2645 static
2646 int ide_cdrom_cleanup(ide_drive_t *drive)
2648 struct cdrom_info *info = drive->driver_data;
2649 struct cdrom_device_info *devinfo = &info->devinfo;
2651 if (ide_unregister_subdriver (drive))
2652 return 1;
2653 if (info->buffer != NULL)
2654 kfree(info->buffer);
2655 if (info->toc != NULL)
2656 kfree(info->toc);
2657 if (info->changer_info != NULL)
2658 kfree(info->changer_info);
2659 if (devinfo->handle == drive && unregister_cdrom (devinfo))
2660 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
2661 kfree(info);
2662 drive->driver_data = NULL;
2663 return 0;
2666 static ide_driver_t ide_cdrom_driver = {
2667 name: "ide-cdrom",
2668 version: IDECD_VERSION,
2669 media: ide_cdrom,
2670 supports_dma: 1,
2671 supports_dsc_overlap: 1,
2672 cleanup: ide_cdrom_cleanup,
2673 do_request: ide_do_rw_cdrom,
2674 ioctl: ide_cdrom_ioctl,
2675 open: ide_cdrom_open,
2676 release: ide_cdrom_release,
2677 media_change: ide_cdrom_check_media_change,
2678 revalidate: ide_cdrom_revalidate,
2679 capacity: ide_cdrom_capacity,
2682 int ide_cdrom_init(void);
2683 static ide_module_t ide_cdrom_module = {
2684 IDE_DRIVER_MODULE,
2685 ide_cdrom_init,
2686 &ide_cdrom_driver,
2687 NULL
2690 /* options */
2691 char *ignore = NULL;
2693 MODULE_PARM(ignore, "s");
2694 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2696 static void __exit ide_cdrom_exit(void)
2698 ide_drive_t *drive;
2699 int failed = 0;
2701 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, &ide_cdrom_driver, failed)) != NULL)
2702 if (ide_cdrom_cleanup (drive)) {
2703 printk ("%s: cleanup_module() called while still busy\n", drive->name);
2704 failed++;
2706 ide_unregister_module (&ide_cdrom_module);
2709 int ide_cdrom_init(void)
2711 ide_drive_t *drive;
2712 struct cdrom_info *info;
2713 int failed = 0;
2715 MOD_INC_USE_COUNT;
2716 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, NULL, failed++)) != NULL) {
2717 /* skip drives that we were told to ignore */
2718 if (ignore != NULL) {
2719 if (strstr(ignore, drive->name)) {
2720 printk("ide-cd: ignoring drive %s\n", drive->name);
2721 continue;
2724 if (drive->scsi) {
2725 printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
2726 continue;
2728 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
2729 if (info == NULL) {
2730 printk ("%s: Can't allocate a cdrom structure\n", drive->name);
2731 continue;
2733 if (ide_register_subdriver (drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
2734 printk ("%s: Failed to register the driver with ide.c\n", drive->name);
2735 kfree (info);
2736 continue;
2738 memset (info, 0, sizeof (struct cdrom_info));
2739 drive->driver_data = info;
2740 DRIVER(drive)->busy++;
2741 if (ide_cdrom_setup (drive)) {
2742 DRIVER(drive)->busy--;
2743 if (ide_cdrom_cleanup (drive))
2744 printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive->name);
2745 continue;
2747 DRIVER(drive)->busy--;
2748 failed--;
2750 ide_register_module(&ide_cdrom_module);
2751 MOD_DEC_USE_COUNT;
2752 return 0;
2755 module_init(ide_cdrom_init);
2756 module_exit(ide_cdrom_exit);