* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / block / ide-cd.c
blobb3ba0356226f61d506e5fa1dfea9ccc712e3e09c
1 /*
2 * linux/drivers/block/ide-cd.c
3 * Copyright (C) 1994, 1995, 1996 scott snyder <snyder@fnald0.fnal.gov>
4 * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
5 * Copyright (C) 1998, 1999 Jens Axboe <axboe@image.dk>
7 * May be copied or modified under the terms of the GNU General Public
8 * License. See linux/COPYING for more information.
10 * ATAPI CD-ROM driver. To be used with ide.c.
11 * See Documentation/cdrom/ide-cd for usage information.
13 * Suggestions are welcome. Patches that work are more welcome though. ;-)
14 * For those wishing to work on this driver, please be sure you download
15 * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
16 * (SFF-8020i rev 2.6) standards. These documents can be obtained by
17 * anonymous ftp from:
18 * ftp://fission.dt.wdc.com/pub/standards/SFF/specs/INF-8020.PDF
19 * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r01.pdf
21 * Drives that deviate from these standards will be accomodated as much
22 * as possible via compile time or command-line options. Since I only have
23 * a few drives, you generally need to send me patches...
25 * ----------------------------------
26 * TO DO LIST:
27 * -Implement Microsoft Media Status Notification per the spec at
28 * http://www.microsoft.com/hwdev/respec/storspec.htm
29 * This will allow us to get automagically notified when the media changes
30 * on ATAPI drives (something the stock ATAPI spec is lacking). Looks
31 * very cool. I discovered its existance the other day at work...
32 * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
33 * boot
35 * ----------------------------------
36 * 1.00 Oct 31, 1994 -- Initial version.
37 * 1.01 Nov 2, 1994 -- Fixed problem with starting request in
38 * cdrom_check_status.
39 * 1.03 Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
40 * (from mlord) -- minor changes to cdrom_setup()
41 * -- renamed ide_dev_s to ide_drive_t, enable irq on command
42 * 2.00 Nov 27, 1994 -- Generalize packet command interface;
43 * add audio ioctls.
44 * 2.01 Dec 3, 1994 -- Rework packet command interface to handle devices
45 * which send an interrupt when ready for a command.
46 * 2.02 Dec 11, 1994 -- Cache the TOC in the driver.
47 * Don't use SCMD_PLAYAUDIO_TI; it's not included
48 * in the current version of ATAPI.
49 * Try to use LBA instead of track or MSF addressing
50 * when possible.
51 * Don't wait for READY_STAT.
52 * 2.03 Jan 10, 1995 -- Rewrite block read routines to handle block sizes
53 * other than 2k and to move multiple sectors in a
54 * single transaction.
55 * 2.04 Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
56 * Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
57 * help in figuring this out. Ditto for Acer and
58 * Aztech drives, which seem to have the same problem.
59 * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
60 * 2.05 Jun 8, 1995 -- Don't attempt to retry after an illegal request
61 * or data protect error.
62 * Use HWIF and DEV_HWIF macros as in ide.c.
63 * Always try to do a request_sense after
64 * a failed command.
65 * Include an option to give textual descriptions
66 * of ATAPI errors.
67 * Fix a bug in handling the sector cache which
68 * showed up if the drive returned data in 512 byte
69 * blocks (like Pioneer drives). Thanks to
70 * Richard Hirst <srh@gpt.co.uk> for diagnosing this.
71 * Properly supply the page number field in the
72 * MODE_SELECT command.
73 * PLAYAUDIO12 is broken on the Aztech; work around it.
74 * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
75 * (my apologies to Scott, but now ide-cd.c is independent)
76 * 3.00 Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
77 * Implement CDROMREADAUDIO ioctl (UNTESTED).
78 * Use input_ide_data() and output_ide_data().
79 * Add door locking.
80 * Fix usage count leak in cdrom_open, which happened
81 * when a read-write mount was attempted.
82 * Try to load the disk on open.
83 * Implement CDROMEJECT_SW ioctl (off by default).
84 * Read total cdrom capacity during open.
85 * Rearrange logic in cdrom_decode_status. Issue
86 * request sense commands for failed packet commands
87 * from here instead of from cdrom_queue_packet_command.
88 * Fix a race condition in retrieving error information.
89 * Suppress printing normal unit attention errors and
90 * some drive not ready errors.
91 * Implement CDROMVOLREAD ioctl.
92 * Implement CDROMREADMODE1/2 ioctls.
93 * Fix race condition in setting up interrupt handlers
94 * when the `serialize' option is used.
95 * 3.01 Sep 2, 1995 -- Fix ordering of reenabling interrupts in
96 * cdrom_queue_request.
97 * Another try at using ide_[input,output]_data.
98 * 3.02 Sep 16, 1995 -- Stick total disk capacity in partition table as well.
99 * Make VERBOSE_IDE_CD_ERRORS dump failed command again.
100 * Dump out more information for ILLEGAL REQUEST errs.
101 * Fix handling of errors occurring before the
102 * packet command is transferred.
103 * Fix transfers with odd bytelengths.
104 * 3.03 Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
105 * `DCI-2S10' drives are broken too.
106 * 3.04 Nov 20, 1995 -- So are Vertos drives.
107 * 3.05 Dec 1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
108 * 3.06 Dec 16, 1995 -- Add support needed for partitions.
109 * More workarounds for Vertos bugs (based on patches
110 * from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
111 * Try to eliminate byteorder assumptions.
112 * Use atapi_cdrom_subchnl struct definition.
113 * Add STANDARD_ATAPI compilation option.
114 * 3.07 Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
115 * Vertos 300.
116 * Add NO_DOOR_LOCKING configuration option.
117 * Handle drive_cmd requests w/NULL args (for hdparm -t).
118 * Work around sporadic Sony55e audio play problem.
119 * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
120 * problem with "hde=cdrom" with no drive present. -ml
121 * 3.08 Mar 6, 1996 -- More Vertos workarounds.
122 * 3.09 Apr 5, 1996 -- Add CDROMCLOSETRAY ioctl.
123 * Switch to using MSF addressing for audio commands.
124 * Reformat to match kernel tabbing style.
125 * Add CDROM_GET_UPC ioctl.
126 * 3.10 Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
127 * 3.11 Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
128 * to remove redundant verify_area calls.
129 * 3.12 May 7, 1996 -- Rudimentary changer support. Based on patches
130 * from Gerhard Zuber <zuber@berlin.snafu.de>.
131 * Let open succeed even if there's no loaded disc.
132 * 3.13 May 19, 1996 -- Fixes for changer code.
133 * 3.14 May 29, 1996 -- Add work-around for Vertos 600.
134 * (From Hennus Bergman <hennus@sky.ow.nl>.)
135 * 3.15 July 2, 1996 -- Added support for Sanyo 3 CD changers
136 * from Ben Galliart <bgallia@luc.edu> with
137 * special help from Jeff Lightfoot
138 * <jeffml@netcom.com>
139 * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
140 * 3.16 Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
141 * 3.17 Sep 17, 1996 -- Tweak audio reads for some drives.
142 * Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
143 * 3.18 Oct 31, 1996 -- Added module and DMA support.
146 * 4.00 Nov 5, 1996 -- New ide-cd maintainer,
147 * Erik B. Andersen <andersee@debian.org>
148 * -- Newer Creative drives don't always set the error
149 * register correctly. Make sure we see media changes
150 * regardless.
151 * -- Integrate with generic cdrom driver.
152 * -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
153 * a patch from Ciro Cattuto <>.
154 * -- Call set_device_ro.
155 * -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
156 * ioctls, based on patch by Erik Andersen
157 * -- Add some probes of drive capability during setup.
159 * 4.01 Nov 11, 1996 -- Split into ide-cd.c and ide-cd.h
160 * -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE
161 * ioctls in favor of a generalized approach
162 * using the generic cdrom driver.
163 * -- Fully integrated with the 2.1.X kernel.
164 * -- Other stuff that I forgot (lots of changes)
166 * 4.02 Dec 01, 1996 -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
167 * to fix the drive door locking problems.
169 * 4.03 Dec 04, 1996 -- Added DSC overlap support.
170 * 4.04 Dec 29, 1996 -- Added CDROMREADRAW ioclt based on patch
171 * by Ales Makarov (xmakarov@sun.felk.cvut.cz)
173 * 4.05 Nov 20, 1997 -- Modified to print more drive info on init
174 * Minor other changes
175 * Fix errors on CDROMSTOP (If you have a "Dolphin",
176 * you must define IHAVEADOLPHIN)
177 * Added identifier so new Sanyo CD-changer works
178 * Better detection if door locking isn't supported
180 * 4.06 Dec 17, 1997 -- fixed endless "tray open" messages -ml
181 * 4.07 Dec 17, 1997 -- fallback to set pc->stat on "tray open"
182 * 4.08 Dec 18, 1997 -- spew less noise when tray is empty
183 * -- fix speed display for ACER 24X, 18X
184 * 4.09 Jan 04, 1998 -- fix handling of the last block so we return
185 * an end of file instead of an I/O error (Gadi)
186 * 4.10 Jan 24, 1998 -- fixed a bug so now changers can change to a new
187 * slot when there is no disc in the current slot.
188 * -- Fixed a memory leak where info->changer_info was
189 * malloc'ed but never free'd when closing the device.
190 * -- Cleaned up the global namespace a bit by making more
191 * functions static that should already have been.
192 * 4.11 Mar 12, 1998 -- Added support for the CDROM_SELECT_SPEED ioctl
193 * based on a patch for 2.0.33 by Jelle Foks
194 * <jelle@scintilla.utwente.nl>, a patch for 2.0.33
195 * by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
196 * version, and my own efforts. -erik
197 * -- Fixed a stupid bug which egcs was kind enough to
198 * inform me of where "Illegal mode for this track"
199 * was never returned due to a comparison on data
200 * types of limited range.
201 * 4.12 Mar 29, 1998 -- Fixed bug in CDROM_SELECT_SPEED so write speed is
202 * now set ionly for CD-R and CD-RW drives. I had
203 * removed this support because it produced errors.
204 * It produced errors _only_ for non-writers. duh.
205 * 4.13 May 05, 1998 -- Suppress useless "in progress of becoming ready"
206 * messages, since this is not an error.
207 * -- Change error messages to be const
208 * -- Remove a "\t" which looks ugly in the syslogs
209 * 4.14 July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
210 * since the .pdf version doesn't seem to work...
211 * -- Updated the TODO list to something more current.
213 * 4.15 Aug 25, 1998 -- Updated ide-cd.h to respect mechine endianess,
214 * patch thanks to "Eddie C. Dost" <ecd@skynet.be>
216 * 4.50 Oct 19, 1998 -- New maintainers!
217 * Jens Axboe <axboe@image.dk>
218 * Chris Zwilling <chris@cloudnet.com>
220 * 4.51 Dec 23, 1998 -- Jens Axboe <axboe@image.dk>
221 * - ide_cdrom_reset enabled since the ide subsystem
222 * handles resets fine now. <axboe@image.dk>
223 * - Transfer size fix for Samsung CD-ROMs, thanks to
224 * "Ville Hallik" <ville.hallik@mail.ee>.
225 * - other minor stuff.
227 * 4.52 Jan 19, 1999 -- Jens Axboe <axboe@image.dk>
228 * - Detect DVD-ROM/RAM drives
230 * 4.53 Feb 22, 1999 - Include other model Samsung and one Goldstar
231 * drive in transfer size limit.
232 * - Fix the I/O error when doing eject without a medium
233 * loaded on some drives.
234 * - CDROMREADMODE2 is now implemented through
235 * CDROMREADRAW, since many drives don't support
236 * MODE2 (even though ATAPI 2.6 says they must).
237 * - Added ignore parameter to ide-cd (as a module), eg
238 * insmod ide-cd ignore='hda hdb'
239 * Useful when using ide-cd in conjunction with
240 * ide-scsi. TODO: non-modular way of doing the
241 * same.
243 * 4.54 Aug 5, 1999 - Support for MMC2 class commands through the generic
244 * packet interface to cdrom.c.
245 * - Unified audio ioctl support, most of it.
246 * - cleaned up various deprecated verify_area().
247 * - Added ide_cdrom_packet() as the interface for
248 * the Uniform generic_packet().
249 * - bunch of other stuff, will fill in logs later.
250 * - report 1 slot for non-changers, like the other
251 * cd-rom drivers. don't report select disc for
252 * non-changers as well.
253 * - mask out audio playing, if the device can't do it.
255 * 4.55 Sep 1, 1999 - Eliminated the rest of the audio ioctls, except
256 * for CDROMREADTOC[ENTRY|HEADER]. Some of the drivers
257 * use this independently of the actual audio handling.
258 * They will disappear later when I get the time to
259 * do it cleanly.
260 * - Minimize the TOC reading - only do it when we
261 * know a media change has occured.
262 * - Moved all the CDROMREADx ioctls to the Uniform layer.
263 * - Heiko Eissfeldt <heiko@colossus.escape.de> supplied
264 * some fixes for CDI.
265 * - CD-ROM leaving door locked fix from Andries
266 * Brouwer <Andries.Brouwer@cwi.nl>
267 * - Erik Andersen <andersen@xmission.com> unified
268 * commands across the various drivers and how
269 * sense errors are handled.
271 * 4.56 Sep 12, 1999 - Removed changer support - it is now in the
272 * Uniform layer.
273 * - Added partition based multisession handling.
274 * - Mode sense and mode select moved to the
275 * Uniform layer.
276 * - Fixed a problem with WPI CDS-32X drive - it
277 * failed the capabilities
280 *************************************************************************/
282 #define IDECD_VERSION "4.56"
284 #include <linux/config.h>
285 #include <linux/module.h>
286 #include <linux/types.h>
287 #include <linux/kernel.h>
288 #include <linux/delay.h>
289 #include <linux/timer.h>
290 #include <linux/malloc.h>
291 #include <linux/interrupt.h>
292 #include <linux/errno.h>
293 #include <linux/cdrom.h>
294 #include <linux/ide.h>
296 #include <asm/irq.h>
297 #include <asm/io.h>
298 #include <asm/byteorder.h>
299 #include <asm/uaccess.h>
300 #include <asm/unaligned.h>
302 #include "ide-cd.h"
304 /****************************************************************************
305 * Generic packet command support and error handling routines.
309 /* Mark that we've seen a media change, and invalidate our internal
310 buffers. */
311 static void cdrom_saw_media_change (ide_drive_t *drive)
313 struct cdrom_info *info = drive->driver_data;
315 CDROM_STATE_FLAGS (drive)->media_changed = 1;
316 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
317 info->nsectors_buffered = 0;
321 static
322 void cdrom_analyze_sense_data (ide_drive_t *drive,
323 struct atapi_request_sense *reqbuf,
324 struct packet_command *failed_command)
326 if (reqbuf->sense_key == NOT_READY ||
327 reqbuf->sense_key == UNIT_ATTENTION) {
328 /* Make good and sure we've seen this potential media change.
329 Some drives (i.e. Creative) fail to present the correct
330 sense key in the error register. */
331 cdrom_saw_media_change (drive);
334 /* Don't print not ready or unit attention errors for
335 READ_SUBCHANNEL. Workman (and probably other programs)
336 uses this command to poll the drive, and we don't want
337 to fill the syslog with useless errors. */
338 if (failed_command &&
339 failed_command->c[0] == GPCMD_READ_SUBCHANNEL)
340 return;
342 if (reqbuf->error_code == 0x70 && reqbuf->sense_key == 0x02
343 && ((reqbuf->asc == 0x3a && reqbuf->ascq == 0x00) ||
344 (reqbuf->asc == 0x04 && reqbuf->ascq == 0x01)))
347 * Suppress the following errors:
348 * "Medium not present", and "in progress of becoming ready",
349 * to keep the noise level down to a dull roar.
351 return;
354 #if VERBOSE_IDE_CD_ERRORS
356 int i;
357 const char *s;
358 char buf[80];
360 printk ("ATAPI device %s:\n", drive->name);
361 if (reqbuf->error_code==0x70)
362 printk(" Error: ");
363 else if (reqbuf->error_code==0x71)
364 printk(" Deferred Error: ");
365 else
366 printk(" Unknown Error Type: ");
368 if ( reqbuf->sense_key < ARY_LEN (sense_key_texts))
369 s = sense_key_texts[reqbuf->sense_key];
370 else
371 s = "bad sense key!";
373 printk ("%s -- (Sense key=0x%02x)\n", s, reqbuf->sense_key);
375 if (reqbuf->asc == 0x40) {
376 sprintf (buf, "Diagnostic failure on component 0x%02x",
377 reqbuf->ascq);
378 s = buf;
379 } else {
380 int lo=0, mid, hi=ARY_LEN (sense_data_texts);
381 unsigned long key = (reqbuf->sense_key << 16);
382 key |= (reqbuf->asc << 8);
383 if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
384 key |= reqbuf->ascq;
385 s = NULL;
387 while (hi > lo) {
388 mid = (lo + hi) / 2;
389 if (sense_data_texts[mid].asc_ascq == key ||
390 sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
391 s = sense_data_texts[mid].text;
392 break;
394 else if (sense_data_texts[mid].asc_ascq > key)
395 hi = mid;
396 else
397 lo = mid+1;
401 if (s == NULL) {
402 if (reqbuf->asc > 0x80)
403 s = "(vendor-specific error)";
404 else
405 s = "(reserved error code)";
408 printk (" %s -- (asc=0x%02x, ascq=0x%02x)\n",
409 s, reqbuf->asc, reqbuf->ascq);
411 if (failed_command != NULL) {
413 int lo=0, mid, hi= ARY_LEN (packet_command_texts);
414 s = NULL;
416 while (hi > lo) {
417 mid = (lo + hi) / 2;
418 if (packet_command_texts[mid].packet_command == failed_command->c[0]) {
419 s = packet_command_texts[mid].text;
420 break;
422 else if (packet_command_texts[mid].packet_command > failed_command->c[0])
423 hi = mid;
424 else
425 lo = mid+1;
428 printk (" The failed \"%s\" packet command was: \n \"", s);
429 for (i=0; i<sizeof (failed_command->c); i++)
430 printk ("%02x ", failed_command->c[i]);
431 printk ("\"\n");
434 if (reqbuf->sense_key == ILLEGAL_REQUEST &&
435 (reqbuf->sense_key_specific[0] & 0x80) != 0) {
436 printk (" Error in %s byte %d",
437 (reqbuf->sense_key_specific[0] & 0x40) != 0
438 ? "command packet"
439 : "command data",
440 (reqbuf->sense_key_specific[1] << 8) +
441 reqbuf->sense_key_specific[2]);
443 if ((reqbuf->sense_key_specific[0] & 0x40) != 0) {
444 printk (" bit %d",
445 reqbuf->sense_key_specific[0] & 0x07);
448 printk ("\n");
452 #else /* not VERBOSE_IDE_CD_ERRORS */
454 /* Suppress printing unit attention and `in progress of becoming ready'
455 errors when we're not being verbose. */
457 if (reqbuf->sense_key == UNIT_ATTENTION ||
458 (reqbuf->sense_key == NOT_READY && (reqbuf->asc == 4 ||
459 reqbuf->asc == 0x3a)))
460 return;
462 printk ("%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
463 drive->name,
464 reqbuf->error_code, reqbuf->sense_key,
465 reqbuf->asc, reqbuf->ascq);
466 #endif /* not VERBOSE_IDE_CD_ERRORS */
470 /* Fix up a possibly partially-processed request so that we can
471 start it over entirely, or even put it back on the request queue. */
472 static void restore_request (struct request *rq)
474 if (rq->buffer != rq->bh->b_data) {
475 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
476 rq->buffer = rq->bh->b_data;
477 rq->nr_sectors += n;
478 rq->sector -= n;
480 rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
484 static void cdrom_queue_request_sense (ide_drive_t *drive,
485 struct semaphore *sem,
486 struct atapi_request_sense *reqbuf,
487 struct packet_command *failed_command)
489 struct cdrom_info *info = drive->driver_data;
490 struct request *rq;
491 struct packet_command *pc;
492 int len;
494 /* If the request didn't explicitly specify where
495 to put the sense data, use the statically allocated structure. */
496 if (reqbuf == NULL)
497 reqbuf = &info->sense_data;
499 /* Make up a new request to retrieve sense information. */
501 pc = &info->request_sense_pc;
502 memset (pc, 0, sizeof (*pc));
504 /* The request_sense structure has an odd number of (16-bit) words,
505 which won't work well with 32-bit transfers. However, we don't care
506 about the last two bytes, so just truncate the structure down
507 to an even length. */
508 len = sizeof (*reqbuf) / 4;
509 len *= 4;
511 pc->c[0] = GPCMD_REQUEST_SENSE;
512 pc->c[4] = (unsigned char) len;
513 pc->buffer = (char *)reqbuf;
514 pc->buflen = len;
515 pc->sense_data = (struct atapi_request_sense *)failed_command;
517 /* stuff the sense request in front of our current request */
519 rq = &info->request_sense_request;
520 ide_init_drive_cmd (rq);
521 rq->cmd = REQUEST_SENSE_COMMAND;
522 rq->buffer = (char *)pc;
523 rq->sem = sem;
524 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
528 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
530 struct request *rq = HWGROUP(drive)->rq;
532 if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
533 struct packet_command *pc = (struct packet_command *)
534 rq->buffer;
535 cdrom_analyze_sense_data (drive,
536 (struct atapi_request_sense *)
537 (pc->buffer - pc->c[4]),
538 (struct packet_command *)
539 pc->sense_data);
541 if (rq->cmd == READ && !rq->current_nr_sectors)
542 uptodate = 1;
543 ide_end_request (uptodate, HWGROUP(drive));
547 /* Returns 0 if the request should be continued.
548 Returns 1 if the request was ended. */
549 static int cdrom_decode_status (ide_drive_t *drive, int good_stat,
550 int *stat_ret)
552 struct request *rq = HWGROUP(drive)->rq;
553 int stat, cmd, err, sense_key;
554 struct packet_command *pc = (struct packet_command *) rq->buffer;
556 /* Check for errors. */
557 stat = GET_STAT();
558 *stat_ret = stat;
560 if (OK_STAT (stat, good_stat, BAD_R_STAT))
561 return 0;
563 /* Get the IDE error register. */
564 err = GET_ERR();
565 sense_key = err >> 4;
567 if (rq == NULL)
568 printk ("%s: missing request in cdrom_decode_status\n",
569 drive->name);
570 else {
571 cmd = rq->cmd;
573 if (cmd == REQUEST_SENSE_COMMAND) {
574 /* We got an error trying to get sense info
575 from the drive (probably while trying
576 to recover from a former error). Just give up. */
578 pc->stat = 1;
579 cdrom_end_request (1, drive);
580 ide_error (drive, "request sense failure", stat);
581 return 1;
583 } else if (cmd == PACKET_COMMAND) {
584 /* All other functions, except for READ. */
586 struct semaphore *sem = NULL;
588 /* Check for tray open. */
589 if (sense_key == NOT_READY) {
590 cdrom_saw_media_change (drive);
591 } else if (sense_key == UNIT_ATTENTION) {
592 /* Check for media change. */
593 cdrom_saw_media_change (drive);
594 /*printk("%s: media changed\n",drive->name);*/
595 return 0;
596 } else {
597 /* Otherwise, print an error. */
598 ide_dump_status (drive, "packet command error",
599 stat);
602 /* Set the error flag and complete the request.
603 Then, if we have a CHECK CONDITION status,
604 queue a request sense command. We must be careful,
605 though: we don't want the thread in
606 cdrom_queue_packet_command to wake up until
607 the request sense has completed. We do this
608 by transferring the semaphore from the packet
609 command request to the request sense request. */
611 if ((stat & ERR_STAT) != 0) {
612 sem = rq->sem;
613 rq->sem = NULL;
616 pc->stat = 1;
617 cdrom_end_request (1, drive);
619 if ((stat & ERR_STAT) != 0)
620 cdrom_queue_request_sense (drive, sem,
621 pc->sense_data, pc);
622 } else {
623 /* Handle errors from READ requests. */
625 if (sense_key == NOT_READY) {
626 /* Tray open. */
627 cdrom_saw_media_change (drive);
629 /* Fail the request. */
630 printk ("%s: tray open\n", drive->name);
631 cdrom_end_request (0, drive);
632 } else if (sense_key == UNIT_ATTENTION) {
633 /* Media change. */
634 cdrom_saw_media_change (drive);
636 /* Arrange to retry the request.
637 But be sure to give up if we've retried
638 too many times. */
639 if (++rq->errors > ERROR_MAX)
640 cdrom_end_request (0, drive);
641 } else if (sense_key == ILLEGAL_REQUEST ||
642 sense_key == DATA_PROTECT) {
643 /* No point in retrying after an illegal
644 request or data protect error.*/
645 ide_dump_status (drive, "command error", stat);
646 cdrom_end_request (0, drive);
647 } else if ((err & ~ABRT_ERR) != 0) {
648 /* Go to the default handler
649 for other errors. */
650 ide_error (drive, "cdrom_decode_status", stat);
651 return 1;
652 } else if ((++rq->errors > ERROR_MAX)) {
653 /* We've racked up too many retries. Abort. */
654 cdrom_end_request (0, drive);
657 /* If we got a CHECK_CONDITION status,
658 queue a request sense command. */
659 if ((stat & ERR_STAT) != 0)
660 cdrom_queue_request_sense (drive,
661 NULL, NULL, NULL);
665 /* Retry, or handle the next request. */
666 return 1;
670 /* Set up the device registers for transferring a packet command on DEV,
671 expecting to later transfer XFERLEN bytes. HANDLER is the routine
672 which actually transfers the command to the drive. If this is a
673 drq_interrupt device, this routine will arrange for HANDLER to be
674 called when the interrupt from the drive arrives. Otherwise, HANDLER
675 will be called immediately after the drive is prepared for the transfer. */
677 static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
678 ide_handler_t *handler)
680 struct cdrom_info *info = drive->driver_data;
682 /* Wait for the controller to be idle. */
683 if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
685 if (info->dma)
686 info->dma = !HWIF(drive)->dmaproc(ide_dma_read, drive);
688 /* Set up the controller registers. */
689 OUT_BYTE (info->dma, IDE_FEATURE_REG);
690 OUT_BYTE (0, IDE_NSECTOR_REG);
691 OUT_BYTE (0, IDE_SECTOR_REG);
693 OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
694 OUT_BYTE (xferlen >> 8 , IDE_HCYL_REG);
695 if (IDE_CONTROL_REG)
696 OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
698 if (info->dma)
699 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
701 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
702 ide_set_handler (drive, handler, WAIT_CMD);
703 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
704 } else {
705 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
706 (*handler) (drive);
709 return 0;
713 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
714 The device registers must have already been prepared
715 by cdrom_start_packet_command.
716 HANDLER is the interrupt handler to call when the command completes
717 or there's data ready. */
718 static int cdrom_transfer_packet_command (ide_drive_t *drive,
719 char *cmd_buf, int cmd_len,
720 ide_handler_t *handler)
722 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
723 /* Here we should have been called after receiving an interrupt
724 from the device. DRQ should how be set. */
725 int stat_dum;
727 /* Check for errors. */
728 if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum))
729 return 1;
730 } else {
731 /* Otherwise, we must wait for DRQ to get set. */
732 if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
733 return 1;
736 /* Arm the interrupt handler. */
737 ide_set_handler (drive, handler, WAIT_CMD);
739 /* Send the command to the device. */
740 atapi_output_bytes (drive, cmd_buf, cmd_len);
742 return 0;
747 /****************************************************************************
748 * Block read functions.
752 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
753 * buffer. Once the first sector is added, any subsequent sectors are
754 * assumed to be continuous (until the buffer is cleared). For the first
755 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
756 * the buffer is cleared.)
758 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
759 int sectors_to_transfer)
761 struct cdrom_info *info = drive->driver_data;
763 /* Number of sectors to read into the buffer. */
764 int sectors_to_buffer = MIN (sectors_to_transfer,
765 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
766 info->nsectors_buffered);
768 char *dest;
770 /* If we don't yet have a sector buffer, try to allocate one.
771 If we can't get one atomically, it's not fatal -- we'll just throw
772 the data away rather than caching it. */
773 if (info->sector_buffer == NULL) {
774 info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE,
775 GFP_ATOMIC);
777 /* If we couldn't get a buffer,
778 don't try to buffer anything... */
779 if (info->sector_buffer == NULL)
780 sectors_to_buffer = 0;
783 /* If this is the first sector in the buffer, remember its number. */
784 if (info->nsectors_buffered == 0)
785 info->sector_buffered = sector;
787 /* Read the data into the buffer. */
788 dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
789 while (sectors_to_buffer > 0) {
790 atapi_input_bytes (drive, dest, SECTOR_SIZE);
791 --sectors_to_buffer;
792 --sectors_to_transfer;
793 ++info->nsectors_buffered;
794 dest += SECTOR_SIZE;
797 /* Throw away any remaining data. */
798 while (sectors_to_transfer > 0) {
799 char dum[SECTOR_SIZE];
800 atapi_input_bytes (drive, dum, sizeof (dum));
801 --sectors_to_transfer;
807 * Check the contents of the interrupt reason register from the cdrom
808 * and attempt to recover if there are problems. Returns 0 if everything's
809 * ok; nonzero if the request has been terminated.
811 static inline
812 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
814 ireason &= 3;
815 if (ireason == 2) return 0;
817 if (ireason == 0) {
818 /* Whoops... The drive is expecting to receive data from us! */
819 printk ("%s: cdrom_read_intr: "
820 "Drive wants to transfer data the wrong way!\n",
821 drive->name);
823 /* Throw some data at the drive so it doesn't hang
824 and quit this request. */
825 while (len > 0) {
826 int dum = 0;
827 atapi_output_bytes (drive, &dum, sizeof (dum));
828 len -= sizeof (dum);
830 } else {
831 /* Drive wants a command packet, or invalid ireason... */
832 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
833 drive->name, ireason);
836 cdrom_end_request (0, drive);
837 return -1;
842 * Interrupt routine. Called when a read request has completed.
844 static void cdrom_read_intr (ide_drive_t *drive)
846 int stat;
847 int ireason, len, sectors_to_transfer, nskip;
848 struct cdrom_info *info = drive->driver_data;
849 int i, dma = info->dma, dma_error = 0;
851 struct request *rq = HWGROUP(drive)->rq;
853 /* Check for errors. */
854 if (dma) {
855 info->dma = 0;
856 if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))
857 HWIF(drive)->dmaproc(ide_dma_off, drive);
860 if (cdrom_decode_status (drive, 0, &stat))
861 return;
863 if (dma) {
864 if (!dma_error) {
865 for (i = rq->nr_sectors; i > 0;) {
866 i -= rq->current_nr_sectors;
867 ide_end_request(1, HWGROUP(drive));
869 } else
870 ide_error (drive, "dma error", stat);
871 return;
874 /* Read the interrupt reason and the transfer length. */
875 ireason = IN_BYTE (IDE_NSECTOR_REG);
876 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
878 /* If DRQ is clear, the command has completed. */
879 if ((stat & DRQ_STAT) == 0) {
880 /* If we're not done filling the current buffer, complain.
881 Otherwise, complete the command normally. */
882 if (rq->current_nr_sectors > 0) {
883 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
884 drive->name, rq->current_nr_sectors);
885 cdrom_end_request (0, drive);
886 } else
887 cdrom_end_request (1, drive);
888 return;
891 /* Check that the drive is expecting to do the same thing we are. */
892 if (cdrom_read_check_ireason (drive, len, ireason)) return;
894 /* Assume that the drive will always provide data in multiples
895 of at least SECTOR_SIZE, as it gets hairy to keep track
896 of the transfers otherwise. */
897 if ((len % SECTOR_SIZE) != 0) {
898 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
899 drive->name, len);
900 if (CDROM_CONFIG_FLAGS (drive)->limit_nframes)
901 printk (" This drive is not supported by this version of the driver\n");
902 else {
903 printk (" Trying to limit transfer sizes\n");
904 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
906 cdrom_end_request (0, drive);
907 return;
910 /* The number of sectors we need to read from the drive. */
911 sectors_to_transfer = len / SECTOR_SIZE;
913 /* First, figure out if we need to bit-bucket
914 any of the leading sectors. */
915 nskip = MIN ((int)(rq->current_nr_sectors -
916 (rq->bh->b_size >> SECTOR_BITS)),
917 sectors_to_transfer);
919 while (nskip > 0) {
920 /* We need to throw away a sector. */
921 char dum[SECTOR_SIZE];
922 atapi_input_bytes (drive, dum, sizeof (dum));
924 --rq->current_nr_sectors;
925 --nskip;
926 --sectors_to_transfer;
929 /* Now loop while we still have data to read from the drive. */
930 while (sectors_to_transfer > 0) {
931 int this_transfer;
933 /* If we've filled the present buffer but there's another
934 chained buffer after it, move on. */
935 if (rq->current_nr_sectors == 0 &&
936 rq->nr_sectors > 0)
937 cdrom_end_request (1, drive);
939 /* If the buffers are full, cache the rest of the data in our
940 internal buffer. */
941 if (rq->current_nr_sectors == 0) {
942 cdrom_buffer_sectors (drive,
943 rq->sector, sectors_to_transfer);
944 sectors_to_transfer = 0;
945 } else {
946 /* Transfer data to the buffers.
947 Figure out how many sectors we can transfer
948 to the current buffer. */
949 this_transfer = MIN (sectors_to_transfer,
950 rq->current_nr_sectors);
952 /* Read this_transfer sectors
953 into the current buffer. */
954 while (this_transfer > 0) {
955 atapi_input_bytes (drive,
956 rq->buffer, SECTOR_SIZE);
957 rq->buffer += SECTOR_SIZE;
958 --rq->nr_sectors;
959 --rq->current_nr_sectors;
960 ++rq->sector;
961 --this_transfer;
962 --sectors_to_transfer;
967 /* Done moving data!
968 Wait for another interrupt. */
969 ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);
974 * Try to satisfy some of the current read request from our cached data.
975 * Returns nonzero if the request has been completed, zero otherwise.
977 static int cdrom_read_from_buffer (ide_drive_t *drive)
979 struct cdrom_info *info = drive->driver_data;
980 struct request *rq = HWGROUP(drive)->rq;
982 /* Can't do anything if there's no buffer. */
983 if (info->sector_buffer == NULL) return 0;
985 /* Loop while this request needs data and the next block is present
986 in our cache. */
987 while (rq->nr_sectors > 0 &&
988 rq->sector >= info->sector_buffered &&
989 rq->sector < info->sector_buffered + info->nsectors_buffered) {
990 if (rq->current_nr_sectors == 0)
991 cdrom_end_request (1, drive);
993 memcpy (rq->buffer,
994 info->sector_buffer +
995 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
996 SECTOR_SIZE);
997 rq->buffer += SECTOR_SIZE;
998 --rq->current_nr_sectors;
999 --rq->nr_sectors;
1000 ++rq->sector;
1003 /* If we've satisfied the current request,
1004 terminate it successfully. */
1005 if (rq->nr_sectors == 0) {
1006 cdrom_end_request (1, drive);
1007 return -1;
1010 /* Move on to the next buffer if needed. */
1011 if (rq->current_nr_sectors == 0)
1012 cdrom_end_request (1, drive);
1014 /* If this condition does not hold, then the kluge i use to
1015 represent the number of sectors to skip at the start of a transfer
1016 will fail. I think that this will never happen, but let's be
1017 paranoid and check. */
1018 if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1019 (rq->sector % SECTORS_PER_FRAME) != 0) {
1020 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1021 drive->name, rq->sector);
1022 cdrom_end_request (0, drive);
1023 return -1;
1026 return 0;
1032 * Routine to send a read packet command to the drive.
1033 * This is usually called directly from cdrom_start_read.
1034 * However, for drq_interrupt devices, it is called from an interrupt
1035 * when the drive is ready to accept the command.
1037 static void cdrom_start_read_continuation (ide_drive_t *drive)
1039 struct packet_command pc;
1040 struct request *rq = HWGROUP(drive)->rq;
1041 int nsect, sector, nframes, frame, nskip;
1043 /* Number of sectors to transfer. */
1044 nsect = rq->nr_sectors;
1046 /* Starting sector. */
1047 sector = rq->sector;
1049 /* If the requested sector doesn't start on a cdrom block boundary,
1050 we must adjust the start of the transfer so that it does,
1051 and remember to skip the first few sectors.
1052 If the CURRENT_NR_SECTORS field is larger than the size
1053 of the buffer, it will mean that we're to skip a number
1054 of sectors equal to the amount by which CURRENT_NR_SECTORS
1055 is larger than the buffer size. */
1056 nskip = (sector % SECTORS_PER_FRAME);
1057 if (nskip > 0) {
1058 /* Sanity check... */
1059 if (rq->current_nr_sectors !=
1060 (rq->bh->b_size >> SECTOR_BITS)) {
1061 printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1062 drive->name, rq->current_nr_sectors);
1063 cdrom_end_request (0, drive);
1064 return;
1067 sector -= nskip;
1068 nsect += nskip;
1069 rq->current_nr_sectors += nskip;
1072 /* Convert from sectors to cdrom blocks, rounding up the transfer
1073 length if needed. */
1074 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1075 frame = sector / SECTORS_PER_FRAME;
1077 /* Largest number of frames was can transfer at once is 64k-1. For
1078 some drives we need to limit this even more. */
1079 nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ?
1080 (65534 / CD_FRAMESIZE) : 65535);
1082 /* Set up the command */
1083 memset (&pc.c, 0, sizeof (pc.c));
1084 pc.c[0] = GPCMD_READ_10;
1085 pc.c[7] = (nframes >> 8);
1086 pc.c[8] = (nframes & 0xff);
1087 put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]);
1089 /* Send the command to the drive and return. */
1090 (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c),
1091 &cdrom_read_intr);
1094 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1095 #define IDECD_SEEK_TIMER (2 * WAIT_MIN_SLEEP) /* 40 ms */
1096 #define IDECD_SEEK_TIMEOUT WAIT_CMD /* 10 sec */
1098 static void cdrom_seek_intr (ide_drive_t *drive)
1100 struct cdrom_info *info = drive->driver_data;
1101 int stat;
1102 static int retry = 10;
1104 if (cdrom_decode_status (drive, 0, &stat))
1105 return;
1106 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1108 if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {
1109 if (--retry == 0) {
1110 printk ("%s: disabled DSC seek overlap\n", drive->name);
1111 drive->dsc_overlap = 0;
1116 static void cdrom_start_seek_continuation (ide_drive_t *drive)
1118 struct packet_command pc;
1119 struct request *rq = HWGROUP(drive)->rq;
1120 int sector, frame, nskip;
1122 sector = rq->sector;
1123 nskip = (sector % SECTORS_PER_FRAME);
1124 if (nskip > 0)
1125 sector -= nskip;
1126 frame = sector / SECTORS_PER_FRAME;
1128 memset (&pc.c, 0, sizeof (pc.c));
1129 pc.c[0] = GPCMD_SEEK;
1130 put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]);
1131 (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c), &cdrom_seek_intr);
1134 static void cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1136 struct cdrom_info *info = drive->driver_data;
1138 info->dma = 0;
1139 info->start_seek = jiffies;
1140 cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);
1144 * Start a read request from the CD-ROM.
1146 static void cdrom_start_read (ide_drive_t *drive, unsigned int block)
1148 struct cdrom_info *info = drive->driver_data;
1149 struct request *rq = HWGROUP(drive)->rq;
1150 int minor = MINOR (rq->rq_dev);
1152 /* If the request is relative to a partition, fix it up to refer to the
1153 absolute address. */
1154 if ((minor & PARTN_MASK) != 0) {
1155 rq->sector = block;
1156 minor &= ~PARTN_MASK;
1157 rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1160 /* We may be retrying this request after an error. Fix up
1161 any weirdness which might be present in the request packet. */
1162 restore_request (rq);
1164 /* Satisfy whatever we can of this request from our cached sector. */
1165 if (cdrom_read_from_buffer (drive))
1166 return;
1168 /* Clear the local sector buffer. */
1169 info->nsectors_buffered = 0;
1171 if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) && (rq->nr_sectors % SECTORS_PER_FRAME == 0))
1172 info->dma = 1;
1173 else
1174 info->dma = 0;
1176 /* Start sending the read request to the drive. */
1177 cdrom_start_packet_command (drive, 32768,
1178 cdrom_start_read_continuation);
1184 /****************************************************************************
1185 * Execute all other packet commands.
1188 /* Forward declarations. */
1189 static int
1190 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1191 struct atapi_request_sense *reqbuf);
1193 /* Interrupt routine for packet command completion. */
1194 static void cdrom_pc_intr (ide_drive_t *drive)
1196 int ireason, len, stat, thislen;
1197 struct request *rq = HWGROUP(drive)->rq;
1198 struct packet_command *pc = (struct packet_command *)rq->buffer;
1200 /* Check for errors. */
1201 if (cdrom_decode_status (drive, 0, &stat))
1202 return;
1204 /* Read the interrupt reason and the transfer length. */
1205 ireason = IN_BYTE (IDE_NSECTOR_REG);
1206 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1208 /* If DRQ is clear, the command has completed.
1209 Complain if we still have data left to transfer. */
1210 if ((stat & DRQ_STAT) == 0) {
1211 /* Some of the trailing request sense fields are optional, and
1212 some drives don't send them. Sigh. */
1213 if (pc->c[0] == GPCMD_REQUEST_SENSE &&
1214 pc->buflen > 0 &&
1215 pc->buflen <= 5) {
1216 while (pc->buflen > 0) {
1217 *pc->buffer++ = 0;
1218 --pc->buflen;
1222 if (pc->buflen == 0)
1223 cdrom_end_request (1, drive);
1224 else {
1225 /* Comment this out, because this always happens
1226 right after a reset occurs, and it is annoying to
1227 always print expected stuff. */
1229 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1230 drive->name, pc->buflen);
1232 pc->stat = 1;
1233 cdrom_end_request (1, drive);
1235 return;
1238 /* Figure out how much data to transfer. */
1239 thislen = pc->buflen;
1240 if (thislen > len) thislen = len;
1242 /* The drive wants to be written to. */
1243 if ((ireason & 3) == 0) {
1244 /* Transfer the data. */
1245 atapi_output_bytes (drive, pc->buffer, thislen);
1247 /* If we haven't moved enough data to satisfy the drive,
1248 add some padding. */
1249 while (len > thislen) {
1250 int dum = 0;
1251 atapi_output_bytes (drive, &dum, sizeof (dum));
1252 len -= sizeof (dum);
1255 /* Keep count of how much data we've moved. */
1256 pc->buffer += thislen;
1257 pc->buflen -= thislen;
1260 /* Same drill for reading. */
1261 else if ((ireason & 3) == 2) {
1263 /* Transfer the data. */
1264 atapi_input_bytes (drive, pc->buffer, thislen);
1266 /* If we haven't moved enough data to satisfy the drive,
1267 add some padding. */
1268 while (len > thislen) {
1269 int dum = 0;
1270 atapi_input_bytes (drive, &dum, sizeof (dum));
1271 len -= sizeof (dum);
1274 /* Keep count of how much data we've moved. */
1275 pc->buffer += thislen;
1276 pc->buflen -= thislen;
1277 } else {
1278 printk ("%s: cdrom_pc_intr: The drive "
1279 "appears confused (ireason = 0x%2x)\n",
1280 drive->name, ireason);
1281 pc->stat = 1;
1284 /* Now we wait for another interrupt. */
1285 ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);
1289 static void cdrom_do_pc_continuation (ide_drive_t *drive)
1291 struct request *rq = HWGROUP(drive)->rq;
1292 struct packet_command *pc = (struct packet_command *)rq->buffer;
1294 /* Send the command to the drive and return. */
1295 cdrom_transfer_packet_command (drive, pc->c,
1296 sizeof (pc->c), &cdrom_pc_intr);
1300 static void cdrom_do_packet_command (ide_drive_t *drive)
1302 int len;
1303 struct request *rq = HWGROUP(drive)->rq;
1304 struct packet_command *pc = (struct packet_command *)rq->buffer;
1305 struct cdrom_info *info = drive->driver_data;
1307 info->dma = 0;
1308 pc->stat = 0;
1309 len = pc->buflen;
1311 /* Start sending the command to the drive. */
1312 cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1316 /* Sleep for TIME jiffies.
1317 Not to be called from an interrupt handler. */
1318 static
1319 void cdrom_sleep (int time)
1321 current->state = TASK_INTERRUPTIBLE;
1322 schedule_timeout(time);
1325 static
1326 int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc)
1328 struct atapi_request_sense my_reqbuf;
1329 int retries = 10;
1330 struct request req;
1332 /* If our caller has not provided a place to stick any sense data,
1333 use our own area. */
1334 if (pc->sense_data == NULL)
1335 pc->sense_data = &my_reqbuf;
1336 pc->sense_data->sense_key = 0;
1337 /* Start of retry loop. */
1338 do {
1339 ide_init_drive_cmd (&req);
1340 req.cmd = PACKET_COMMAND;
1341 req.buffer = (char *)pc;
1342 if (ide_do_drive_cmd (drive, &req, ide_wait)) {
1343 printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
1344 drive->name, req.buffer[0], req.buffer[1]);
1345 /* FIXME: we should probably abort/retry or something */
1347 if (pc->stat != 0) {
1348 /* The request failed. Retry if it was due to a unit
1349 attention status
1350 (usually means media was changed). */
1351 struct atapi_request_sense *reqbuf = pc->sense_data;
1353 if (reqbuf->sense_key == UNIT_ATTENTION)
1354 cdrom_saw_media_change (drive);
1355 else if (reqbuf->sense_key == NOT_READY &&
1356 reqbuf->asc == 4) {
1357 /* The drive is in the process of loading
1358 a disk. Retry, but wait a little to give
1359 the drive time to complete the load. */
1360 cdrom_sleep (HZ);
1361 } else {
1362 /* Otherwise, don't retry. */
1363 retries = 0;
1365 --retries;
1368 /* End of retry loop. */
1369 } while (pc->stat != 0 && retries >= 0);
1372 /* Return an error if the command failed. */
1373 if (pc->stat != 0)
1374 return -EIO;
1375 else {
1376 /* The command succeeded. If it was anything other than
1377 a request sense, eject, or door lock command,
1378 and we think that the door is presently unlocked, lock it
1379 again. (The door was probably unlocked via an explicit
1380 CDROMEJECT ioctl.) */
1381 if (CDROM_STATE_FLAGS (drive)->door_locked == 0 &&
1382 (pc->c[0] != GPCMD_TEST_UNIT_READY &&
1383 pc->c[0] != GPCMD_REQUEST_SENSE &&
1384 pc->c[0] != GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL &&
1385 pc->c[0] != GPCMD_START_STOP_UNIT &&
1386 pc->c[0] != GPCMD_MODE_SENSE_10 &&
1387 pc->c[0] != GPCMD_MODE_SELECT_10)) {
1388 (void) cdrom_lockdoor (drive, 1, NULL);
1390 return 0;
1394 /****************************************************************************
1395 * cdrom driver request routine.
1397 static
1398 void ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, unsigned long block)
1400 if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1401 cdrom_do_packet_command (drive);
1402 else if (rq -> cmd == RESET_DRIVE_COMMAND) {
1403 cdrom_end_request (1, drive);
1404 ide_do_reset (drive);
1405 return;
1406 } else if (rq -> cmd != READ) {
1407 printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1408 cdrom_end_request (0, drive);
1409 } else {
1410 struct cdrom_info *info = drive->driver_data;
1412 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1413 unsigned long elpased = jiffies - info->start_seek;
1414 int stat = GET_STAT();
1416 if ((stat & SEEK_STAT) != SEEK_STAT) {
1417 if (elpased < IDECD_SEEK_TIMEOUT) {
1418 ide_stall_queue (drive, IDECD_SEEK_TIMER);
1419 return;
1421 printk ("%s: DSC timeout\n", drive->name);
1423 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1425 if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap)
1426 cdrom_start_seek (drive, block);
1427 else
1428 cdrom_start_read (drive, block);
1429 info->last_block = block;
1435 /****************************************************************************
1436 * Ioctl handling.
1438 * Routines which queue packet commands take as a final argument a pointer
1439 * to an atapi_request_sense struct. If execution of the command results
1440 * in an error with a CHECK CONDITION status, this structure will be filled
1441 * with the results of the subsequent request sense command. The pointer
1442 * can also be NULL, in which case no sense information is returned.
1445 #if ! STANDARD_ATAPI
1446 static inline
1447 int bin2bcd (int x)
1449 return (x%10) | ((x/10) << 4);
1453 static inline
1454 int bcd2bin (int x)
1456 return (x >> 4) * 10 + (x & 0x0f);
1459 static
1460 void msf_from_bcd (struct atapi_msf *msf)
1462 msf->minute = bcd2bin (msf->minute);
1463 msf->second = bcd2bin (msf->second);
1464 msf->frame = bcd2bin (msf->frame);
1467 #endif /* not STANDARD_ATAPI */
1470 static inline
1471 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1473 lba += CD_MSF_OFFSET;
1474 lba &= 0xffffff; /* negative lbas use only 24 bits */
1475 *m = lba / (CD_SECS * CD_FRAMES);
1476 lba %= (CD_SECS * CD_FRAMES);
1477 *s = lba / CD_FRAMES;
1478 *f = lba % CD_FRAMES;
1482 static inline
1483 int msf_to_lba (byte m, byte s, byte f)
1485 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1489 static int
1490 cdrom_check_status (ide_drive_t *drive,
1491 struct atapi_request_sense *reqbuf)
1493 struct packet_command pc;
1494 struct cdrom_info *info = drive->driver_data;
1495 struct cdrom_device_info *cdi = &info->devinfo;
1497 memset (&pc, 0, sizeof (pc));
1499 pc.sense_data = reqbuf;
1500 pc.c[0] = GPCMD_TEST_UNIT_READY;
1502 #if ! STANDARD_ATAPI
1503 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1504 switch CDs instead of supporting the LOAD_UNLOAD opcode */
1506 pc.c[7] = cdi->sanyo_slot % 3;
1507 #endif /* not STANDARD_ATAPI */
1509 return cdrom_queue_packet_command (drive, &pc);
1513 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1514 static int
1515 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1516 struct atapi_request_sense *reqbuf)
1518 struct atapi_request_sense my_reqbuf;
1519 int stat;
1520 struct packet_command pc;
1522 if (reqbuf == NULL)
1523 reqbuf = &my_reqbuf;
1525 /* If the drive cannot lock the door, just pretend. */
1526 if (CDROM_CONFIG_FLAGS (drive)->no_doorlock)
1527 stat = 0;
1528 else {
1529 memset (&pc, 0, sizeof (pc));
1530 pc.sense_data = reqbuf;
1532 pc.c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1533 pc.c[4] = (lockflag != 0);
1534 stat = cdrom_queue_packet_command (drive, &pc);
1537 /* If we got an illegal field error, the drive
1538 probably cannot lock the door. */
1539 if (stat != 0 &&
1540 reqbuf->sense_key == ILLEGAL_REQUEST &&
1541 (reqbuf->asc == 0x24 || reqbuf->asc == 0x20)) {
1542 printk ("%s: door locking not supported\n",
1543 drive->name);
1544 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1545 stat = 0;
1548 /* no medium, that's alright. */
1549 if (stat != 0 && reqbuf->sense_key == NOT_READY && reqbuf->asc == 0x3a)
1550 stat = 0;
1552 if (stat == 0)
1553 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1555 return stat;
1559 /* Eject the disk if EJECTFLAG is 0.
1560 If EJECTFLAG is 1, try to reload the disk. */
1561 static int
1562 cdrom_eject (ide_drive_t *drive, int ejectflag,
1563 struct atapi_request_sense *reqbuf)
1565 struct packet_command pc;
1567 if (CDROM_CONFIG_FLAGS (drive)->no_eject && !ejectflag)
1568 return -EDRIVE_CANT_DO_THIS;
1570 /* reload fails on some drives, if the tray is locked */
1571 if (CDROM_STATE_FLAGS (drive)->door_locked && ejectflag)
1572 return 0;
1574 memset (&pc, 0, sizeof (pc));
1575 pc.sense_data = reqbuf;
1577 pc.c[0] = GPCMD_START_STOP_UNIT;
1578 pc.c[4] = 0x02 + (ejectflag != 0);
1579 return cdrom_queue_packet_command (drive, &pc);
1582 static int
1583 cdrom_read_capacity (ide_drive_t *drive, unsigned *capacity,
1584 struct atapi_request_sense *reqbuf)
1586 struct {
1587 __u32 lba;
1588 __u32 blocklen;
1589 } capbuf;
1591 int stat;
1592 struct packet_command pc;
1594 memset (&pc, 0, sizeof (pc));
1595 pc.sense_data = reqbuf;
1597 pc.c[0] = GPCMD_READ_CDVD_CAPACITY;
1598 pc.buffer = (char *)&capbuf;
1599 pc.buflen = sizeof (capbuf);
1601 stat = cdrom_queue_packet_command (drive, &pc);
1602 if (stat == 0)
1603 *capacity = ntohl (capbuf.lba);
1605 return stat;
1609 static int
1610 cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
1611 int format, char *buf, int buflen,
1612 struct atapi_request_sense *reqbuf)
1614 struct packet_command pc;
1616 memset (&pc, 0, sizeof (pc));
1617 pc.sense_data = reqbuf;
1619 pc.buffer = buf;
1620 pc.buflen = buflen;
1621 pc.c[0] = GPCMD_READ_TOC_PMA_ATIP;
1622 pc.c[6] = trackno;
1623 pc.c[7] = (buflen >> 8);
1624 pc.c[8] = (buflen & 0xff);
1625 pc.c[9] = (format << 6);
1626 if (msf_flag) pc.c[1] = 2;
1627 return cdrom_queue_packet_command (drive, &pc);
1631 /* Try to read the entire TOC for the disk into our internal buffer. */
1632 static int
1633 cdrom_read_toc (ide_drive_t *drive, struct atapi_request_sense *reqbuf)
1635 int stat, ntracks, i;
1636 struct cdrom_info *info = drive->driver_data;
1637 struct atapi_toc *toc = info->toc;
1638 int minor = drive->select.b.unit << PARTN_BITS;
1639 struct {
1640 struct atapi_toc_header hdr;
1641 struct atapi_toc_entry ent;
1642 } ms_tmp;
1644 if (toc == NULL) {
1645 /* Try to allocate space. */
1646 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1647 GFP_KERNEL);
1648 info->toc = toc;
1649 if (toc == NULL) {
1650 printk ("%s: No cdrom TOC buffer!\n", drive->name);
1651 return -ENOMEM;
1655 /* Check to see if the existing data is still valid.
1656 If it is, just return. */
1657 if (CDROM_STATE_FLAGS (drive)->toc_valid)
1658 (void) cdrom_check_status (drive, NULL);
1660 if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1662 /* First read just the header, so we know how long the TOC is. */
1663 stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1664 sizeof (struct atapi_toc_header), reqbuf);
1665 if (stat) return stat;
1667 #if ! STANDARD_ATAPI
1668 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1669 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1670 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1672 #endif /* not STANDARD_ATAPI */
1674 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1675 if (ntracks <= 0) return -EIO;
1676 if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1678 /* Now read the whole schmeer. */
1679 stat = cdrom_read_tocentry (drive, toc->hdr.first_track, 1, 0, (char *)&toc->hdr,
1680 sizeof (struct atapi_toc_header) +
1681 (ntracks + 1) *
1682 sizeof (struct atapi_toc_entry), reqbuf);
1684 if (stat && toc->hdr.first_track > 1) {
1685 /* Cds with CDI tracks only don't have any TOC entries,
1686 despite of this the returned values are
1687 first_track == last_track = number of CDI tracks + 1,
1688 so that this case is indistinguishable from the same
1689 layout plus an additional audio track.
1690 If we get an error for the regular case, we assume
1691 a CDI without additional audio tracks. In this case
1692 the readable TOC is empty (CDI tracks are not included)
1693 and only holds the Leadout entry. Heiko Eißfeldt */
1694 ntracks = 0;
1695 stat = cdrom_read_tocentry (drive, CDROM_LEADOUT, 1,
1696 0, (char *)&toc->hdr,
1697 sizeof (struct atapi_toc_header) +
1698 (ntracks+1) *
1699 sizeof (struct atapi_toc_entry),
1700 reqbuf);
1701 if (stat) {
1702 return stat;
1704 #if ! STANDARD_ATAPI
1705 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1706 toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
1707 toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
1708 } else
1709 #endif /* not STANDARD_ATAPI */
1711 toc->hdr.first_track = CDROM_LEADOUT;
1712 toc->hdr.last_track = CDROM_LEADOUT;
1714 } else if (stat) {
1715 return stat;
1717 if (stat) return stat;
1719 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1721 #if ! STANDARD_ATAPI
1722 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1723 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1724 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1726 #endif /* not STANDARD_ATAPI */
1728 for (i=0; i<=ntracks; i++) {
1729 #if ! STANDARD_ATAPI
1730 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1731 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1732 toc->ent[i].track = bcd2bin (toc->ent[i].track);
1733 msf_from_bcd (&toc->ent[i].addr.msf);
1735 #endif /* not STANDARD_ATAPI */
1736 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1737 toc->ent[i].addr.msf.second,
1738 toc->ent[i].addr.msf.frame);
1741 /* Read the multisession information. */
1742 if (toc->hdr.first_track != CDROM_LEADOUT) {
1743 /* Read the multisession information. */
1744 stat = cdrom_read_tocentry (drive, 0, 1, 1,
1745 (char *)&ms_tmp, sizeof (ms_tmp),
1746 reqbuf);
1747 if (stat) return stat;
1748 } else {
1749 ms_tmp.ent.addr.msf.minute = 0;
1750 ms_tmp.ent.addr.msf.second = 2;
1751 ms_tmp.ent.addr.msf.frame = 0;
1752 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
1755 #if ! STANDARD_ATAPI
1756 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1757 msf_from_bcd (&ms_tmp.ent.addr.msf);
1758 #endif /* not STANDARD_ATAPI */
1760 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1761 ms_tmp.ent.addr.msf.second,
1762 ms_tmp.ent.addr.msf.frame);
1764 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1766 /* Now try to get the total cdrom capacity. */
1767 #if 0
1768 stat = cdrom_get_last_written(MKDEV(HWIF(drive)->major, minor,
1769 (long *)&toc->capacity);
1770 if (stat)
1771 #endif
1772 stat = cdrom_read_capacity (drive, &toc->capacity, reqbuf);
1773 if (stat) toc->capacity = 0x1fffff;
1775 /* for general /dev/cdrom like mounting, one big disc */
1776 HWIF(drive)->gd->sizes[minor] = (toc->capacity * SECTORS_PER_FRAME) >>
1777 (BLOCK_SIZE_BITS - 9);
1779 /* Remember that we've read this stuff. */
1780 CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1782 /* should be "if multisession", but it does no harm. */
1783 if (ntracks == 1)
1784 return 0;
1786 /* setup each minor to respond to a session */
1787 minor++;
1788 i = toc->hdr.first_track;
1789 while ((i <= ntracks) && ((minor & CD_PART_MASK) < CD_PART_MAX)) {
1790 drive->part[minor & PARTN_MASK].start_sect = 0;
1791 drive->part[minor & PARTN_MASK].nr_sects = (toc->ent[i].addr.lba *
1792 SECTORS_PER_FRAME) << (BLOCK_SIZE_BITS - 9);
1793 HWIF(drive)->gd->sizes[minor] = (toc->ent[i].addr.lba *
1794 SECTORS_PER_FRAME) >> (BLOCK_SIZE_BITS - 9);
1795 blksize_size[HWIF(drive)->major][minor] = CD_FRAMESIZE;
1796 i++;
1797 minor++;
1800 return 0;
1804 static int
1805 cdrom_read_subchannel (ide_drive_t *drive, int format,
1806 char *buf, int buflen,
1807 struct atapi_request_sense *reqbuf)
1809 struct packet_command pc;
1811 memset (&pc, 0, sizeof (pc));
1812 pc.sense_data = reqbuf;
1814 pc.buffer = buf;
1815 pc.buflen = buflen;
1816 pc.c[0] = GPCMD_READ_SUBCHANNEL;
1817 pc.c[1] = 2; /* MSF addressing */
1818 pc.c[2] = 0x40; /* request subQ data */
1819 pc.c[3] = format;
1820 pc.c[7] = (buflen >> 8);
1821 pc.c[8] = (buflen & 0xff);
1822 return cdrom_queue_packet_command (drive, &pc);
1825 /* ATAPI cdrom drives are free to select the speed you request or any slower
1826 rate :-( Requesting too fast a speed will _not_ produce an error. */
1827 static int
1828 cdrom_select_speed (ide_drive_t *drive, int speed,
1829 struct atapi_request_sense *reqbuf)
1831 struct packet_command pc;
1832 memset (&pc, 0, sizeof (pc));
1833 pc.sense_data = reqbuf;
1835 if (speed == 0)
1836 speed = 0xffff; /* set to max */
1837 else
1838 speed *= 177; /* Nx to kbytes/s */
1840 pc.c[0] = GPCMD_SET_SPEED;
1841 /* Read Drive speed in kbytes/second MSB */
1842 pc.c[2] = (speed >> 8) & 0xff;
1843 /* Read Drive speed in kbytes/second LSB */
1844 pc.c[3] = speed & 0xff;
1845 if ( CDROM_CONFIG_FLAGS(drive)->cd_r ||
1846 CDROM_CONFIG_FLAGS(drive)->cd_rw ) {
1847 /* Write Drive speed in kbytes/second MSB */
1848 pc.c[4] = (speed >> 8) & 0xff;
1849 /* Write Drive speed in kbytes/second LSB */
1850 pc.c[5] = speed & 0xff;
1853 return cdrom_queue_packet_command (drive, &pc);
1857 static
1858 int cdrom_get_toc_entry (ide_drive_t *drive, int track,
1859 struct atapi_toc_entry **ent,
1860 struct atapi_request_sense *reqbuf)
1862 struct cdrom_info *info = drive->driver_data;
1863 struct atapi_toc *toc = info->toc;
1864 int ntracks;
1866 /* Check validity of requested track number. */
1867 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1868 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
1869 if (track == CDROM_LEADOUT)
1870 *ent = &toc->ent[ntracks];
1871 else if (track < toc->hdr.first_track ||
1872 track > toc->hdr.last_track)
1873 return -EINVAL;
1874 else
1875 *ent = &toc->ent[track - toc->hdr.first_track];
1877 return 0;
1882 /* This gets the mechanism status per ATAPI draft spec 2.6 */
1883 static int
1884 cdrom_read_mech_status (ide_drive_t *drive, char *buf, int buflen,
1885 struct atapi_request_sense *reqbuf)
1887 struct packet_command pc;
1889 memset (&pc, 0, sizeof (pc));
1890 pc.sense_data = reqbuf;
1892 pc.buffer = buf;
1893 pc.buflen = buflen;
1894 pc.c[0] = GPCMD_MECHANISM_STATUS;
1895 pc.c[8] = (buflen >> 8);
1896 pc.c[9] = (buflen & 0xff);
1897 return cdrom_queue_packet_command (drive, &pc);
1900 /* Read the drive mechanism status and slot table into our internal buffer.
1901 If the buffer does not yet exist, allocate it. */
1902 static int
1903 cdrom_read_changer_info (ide_drive_t *drive)
1905 int nslots;
1906 struct cdrom_info *info = drive->driver_data;
1908 if (info->changer_info)
1909 nslots = info->changer_info->hdr.nslots;
1911 else {
1912 struct atapi_mechstat_header mechbuf;
1913 int stat;
1915 stat = cdrom_read_mech_status (drive,
1916 (char *)&mechbuf,
1917 sizeof (mechbuf),
1918 NULL);
1919 if (stat)
1920 return stat;
1922 nslots = mechbuf.nslots;
1923 info->changer_info =
1924 (struct atapi_changer_info *)
1925 kmalloc (sizeof (struct atapi_changer_info) +
1926 nslots * sizeof (struct atapi_slot),
1927 GFP_KERNEL);
1929 if (info->changer_info == NULL)
1930 return -ENOMEM;
1933 return cdrom_read_mech_status
1934 (drive,
1935 (char *)&info->changer_info->hdr,
1936 sizeof (struct atapi_mechstat_header) +
1937 nslots * sizeof (struct atapi_slot),
1938 NULL);
1941 /* the generic packet interface to cdrom.c */
1942 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
1943 struct cdrom_generic_command *cgc)
1945 struct packet_command pc;
1946 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
1948 /* here we queue the commands from the uniform CD-ROM
1949 layer. the packet must be complete, as we do not
1950 touch it at all. */
1951 memset(&pc, 0, sizeof(pc));
1952 memcpy(pc.c, cgc->cmd, CDROM_PACKET_SIZE);
1953 pc.buffer = cgc->buffer;
1954 pc.buflen = cgc->buflen;
1955 cgc->stat = cdrom_queue_packet_command(drive, &pc);
1956 return cgc->stat;
1959 static
1960 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
1961 unsigned int cmd, unsigned long arg)
1963 struct cdrom_generic_command cgc;
1964 char buffer[16];
1965 int stat;
1967 init_cdrom_command(&cgc, buffer, sizeof(buffer));
1969 /* These will be moved into the Uniform layer shortly... */
1970 switch (cmd) {
1971 case CDROMSETSPINDOWN: {
1972 char spindown;
1974 if (copy_from_user(&spindown, (void *) arg, sizeof(char)))
1975 return -EFAULT;
1977 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
1978 return stat;
1980 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1982 return cdrom_mode_select(cdi, &cgc);
1985 case CDROMGETSPINDOWN: {
1986 char spindown;
1988 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
1989 return stat;
1991 spindown = buffer[11] & 0x0f;
1993 if (copy_to_user((void *) arg, &spindown, sizeof (char)))
1994 return -EFAULT;
1996 return 0;
1999 default:
2000 return -EINVAL;
2005 static
2006 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2007 unsigned int cmd, void *arg)
2010 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2011 struct cdrom_info *info = drive->driver_data;
2013 switch (cmd) {
2014 case CDROMREADTOCHDR: {
2015 int stat;
2016 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2017 struct atapi_toc *toc;
2019 /* Make sure our saved TOC is valid. */
2020 stat = cdrom_read_toc (drive, NULL);
2021 if (stat) return stat;
2023 toc = info->toc;
2024 tochdr->cdth_trk0 = toc->hdr.first_track;
2025 tochdr->cdth_trk1 = toc->hdr.last_track;
2027 return 0;
2030 case CDROMREADTOCENTRY: {
2031 int stat;
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 NULL);
2037 if (stat) return stat;
2039 tocentry->cdte_ctrl = toce->control;
2040 tocentry->cdte_adr = toce->adr;
2041 if (tocentry->cdte_format == CDROM_MSF) {
2042 lba_to_msf (toce->addr.lba,
2043 &tocentry->cdte_addr.msf.minute,
2044 &tocentry->cdte_addr.msf.second,
2045 &tocentry->cdte_addr.msf.frame);
2046 } else
2047 tocentry->cdte_addr.lba = toce->addr.lba;
2049 return 0;
2052 default:
2053 return -EINVAL;
2057 static
2058 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2060 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2061 struct request req;
2063 ide_init_drive_cmd (&req);
2064 req.cmd = RESET_DRIVE_COMMAND;
2065 return ide_do_drive_cmd (drive, &req, ide_wait);
2069 static
2070 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2072 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2073 struct atapi_request_sense rq;
2075 if (position) {
2076 int stat = cdrom_lockdoor (drive, 0, &rq);
2077 if (stat) return stat;
2080 return cdrom_eject (drive, !position, NULL);
2083 static
2084 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2086 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2087 return cdrom_lockdoor (drive, lock, NULL);
2090 static
2091 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2093 int stat, attempts = 3;
2094 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2095 struct atapi_request_sense reqbuf;
2096 struct cdrom_generic_command cgc;
2097 struct {
2098 char pad[8];
2099 struct atapi_capabilities_page cap;
2100 } buf;
2101 stat=cdrom_select_speed (drive, speed, &reqbuf);
2102 if (stat<0)
2103 return stat;
2105 init_cdrom_command(&cgc, &buf, sizeof(buf));
2106 /* Now with that done, update the speed fields */
2107 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2108 if (attempts-- <= 0)
2109 return 0;
2110 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2111 } while (stat);
2113 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2114 if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2115 CDROM_STATE_FLAGS (drive)->current_speed =
2116 (((unsigned int)buf.cap.curspeed) + (176/2)) / 176;
2117 CDROM_CONFIG_FLAGS (drive)->max_speed =
2118 (((unsigned int)buf.cap.maxspeed) + (176/2)) / 176;
2119 } else {
2120 CDROM_STATE_FLAGS (drive)->current_speed =
2121 (ntohs(buf.cap.curspeed) + (176/2)) / 176;
2122 CDROM_CONFIG_FLAGS (drive)->max_speed =
2123 (ntohs(buf.cap.maxspeed) + (176/2)) / 176;
2125 cdi->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2126 return 0;
2129 static
2130 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2132 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2133 struct cdrom_info *info = drive->driver_data;
2135 if (slot_nr == CDSL_CURRENT) {
2137 struct atapi_request_sense my_reqbuf;
2138 int stat = cdrom_check_status (drive, &my_reqbuf);
2139 if (stat == 0 || my_reqbuf.sense_key == UNIT_ATTENTION)
2140 return CDS_DISC_OK;
2142 if (my_reqbuf.sense_key == NOT_READY) {
2143 /* ATAPI doesn't have anything that can help
2144 us decide whether the drive is really
2145 emtpy or the tray is just open. irk. */
2146 return CDS_TRAY_OPEN;
2149 return CDS_DRIVE_NOT_READY;
2152 #if ! STANDARD_ATAPI
2153 else if (cdi->sanyo_slot > 0)
2154 return CDS_NO_INFO;
2155 #endif /* not STANDARD_ATAPI */
2157 else {
2158 struct atapi_changer_info *ci;
2159 int stat = cdrom_read_changer_info (drive);
2160 if (stat < 0)
2161 return stat;
2162 ci = info->changer_info;
2164 if (ci->slots[slot_nr].disc_present)
2165 return CDS_DISC_OK;
2166 else
2167 return CDS_NO_DISC;
2171 static
2172 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2173 struct cdrom_multisession *ms_info)
2175 struct atapi_toc *toc;
2176 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2177 struct cdrom_info *info = drive->driver_data;
2179 toc = info->toc;
2180 ms_info->addr.lba = toc->last_session_lba;
2181 ms_info->xa_flag = toc->xa_flag;
2183 return 0;
2186 static
2187 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2188 struct cdrom_mcn *mcn_info)
2190 int stat;
2191 char mcnbuf[24];
2192 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2194 stat = cdrom_read_subchannel (drive, 2, /* get MCN */
2195 mcnbuf, sizeof (mcnbuf),
2196 NULL);
2197 if (stat) return stat;
2199 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2200 sizeof (mcn_info->medium_catalog_number)-1);
2201 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2202 = '\0';
2204 return 0;
2209 /****************************************************************************
2210 * Other driver requests (open, close, check media change).
2213 static
2214 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2215 int slot_nr)
2217 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2218 struct cdrom_info *info = drive->driver_data;
2219 struct atapi_request_sense reqbuf;
2220 int retval;
2222 if (slot_nr == CDSL_CURRENT) {
2223 (void) cdrom_check_status (drive, NULL);
2224 retval = CDROM_STATE_FLAGS (drive)->media_changed;
2225 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2228 #if ! STANDARD_ATAPI
2229 else if (cdi->sanyo_slot > 0) {
2230 retval = 0;
2232 #endif /* not STANDARD_ATAPI */
2234 else {
2235 struct atapi_changer_info *ci;
2236 int stat = cdrom_read_changer_info (drive);
2237 if (stat < 0)
2238 return stat;
2239 ci = info->changer_info;
2241 /* This test may be redundant with cdrom.c. */
2242 if (slot_nr < 0 || slot_nr >= ci->hdr.nslots)
2243 return -EINVAL;
2245 retval = ci->slots[slot_nr].change;
2248 /* if the media has changed, check if a disc is in the drive
2249 and read the toc info. */
2250 if (retval || !CDROM_STATE_FLAGS (drive)->toc_valid) {
2251 /* if cdrom_read_toc fails, return 1 to indicate
2252 that a disc change has occured. there might not
2253 be a disc in the drive. */
2254 if ((retval = cdrom_read_toc (drive, &reqbuf)))
2255 return 1;
2258 return retval;
2262 static
2263 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2265 return 0;
2270 * Close down the device. Invalidate all cached blocks.
2273 static
2274 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2280 /****************************************************************************
2281 * Device initialization.
2284 static
2285 struct cdrom_device_ops ide_cdrom_dops = {
2286 ide_cdrom_open_real, /* open */
2287 ide_cdrom_release_real, /* release */
2288 ide_cdrom_drive_status, /* drive_status */
2289 ide_cdrom_check_media_change_real, /* media_changed */
2290 ide_cdrom_tray_move, /* tray_move */
2291 ide_cdrom_lock_door, /* lock_door */
2292 ide_cdrom_select_speed, /* select_speed */
2293 NULL, /* select_disc */
2294 ide_cdrom_get_last_session, /* get_last_session */
2295 ide_cdrom_get_mcn, /* get_mcn */
2296 ide_cdrom_reset, /* reset */
2297 ide_cdrom_audio_ioctl, /* audio_ioctl */
2298 ide_cdrom_dev_ioctl, /* dev_ioctl */
2299 CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED
2300 | CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN
2301 | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET | CDC_IOCTLS
2302 | CDC_DRIVE_STATUS | CDC_CD_R | CDC_CD_RW | CDC_DVD
2303 | CDC_DVD_R| CDC_DVD_RAM | CDC_GENERIC_PACKET, /* capability */
2304 0, /* n_minors */
2305 ide_cdrom_packet
2308 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2310 struct cdrom_info *info = drive->driver_data;
2311 struct cdrom_device_info *devinfo = &info->devinfo;
2312 int minor = (drive->select.b.unit)<<PARTN_BITS;
2314 devinfo->dev = MKDEV (HWIF(drive)->major, minor | CD_PART_MASK);
2315 devinfo->ops = &ide_cdrom_dops;
2316 devinfo->mask = 0;
2317 *(int *)&devinfo->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2318 *(int *)&devinfo->capacity = nslots;
2319 devinfo->handle = (void *) drive;
2320 strcpy(devinfo->name, drive->name);
2322 /* set capability mask to match the probe. */
2323 if (!CDROM_CONFIG_FLAGS (drive)->cd_r)
2324 devinfo->mask |= CDC_CD_R;
2325 if (!CDROM_CONFIG_FLAGS (drive)->cd_rw)
2326 devinfo->mask |= CDC_CD_RW;
2327 if (!CDROM_CONFIG_FLAGS (drive)->dvd)
2328 devinfo->mask |= CDC_DVD;
2329 if (!CDROM_CONFIG_FLAGS (drive)->dvd_r)
2330 devinfo->mask |= CDC_DVD_R;
2331 if (!CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2332 devinfo->mask |= CDC_DVD_RAM;
2333 if (!CDROM_CONFIG_FLAGS (drive)->is_changer)
2334 devinfo->mask |= CDC_SELECT_DISC;
2335 if (!CDROM_CONFIG_FLAGS (drive)->audio_play)
2336 devinfo->mask |= CDC_PLAY_AUDIO;
2337 if (!CDROM_CONFIG_FLAGS (drive)->close_tray)
2338 devinfo->mask |= CDC_CLOSE_TRAY;
2340 return register_cdrom (devinfo);
2344 static
2345 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2347 struct cdrom_info *info = drive->driver_data;
2348 struct cdrom_device_info *cdi = &info->devinfo;
2349 int stat, nslots = 1, attempts = 3;
2350 struct cdrom_generic_command cgc;
2351 struct {
2352 char pad[8];
2353 struct atapi_capabilities_page cap;
2354 } buf;
2356 if (CDROM_CONFIG_FLAGS (drive)->nec260)
2357 return nslots;
2359 init_cdrom_command(&cgc, &buf, sizeof(buf));
2360 /* we have to cheat a little here. the packet will eventually
2361 * be queued with ide_cdrom_packet(), which extracts the
2362 * drive from cdi->handle. Since this device hasn't been
2363 * registered with the Uniform layer yet, it can't do this.
2364 * Same goes cdi->ops.
2366 cdi->handle = (ide_drive_t *) drive;
2367 cdi->ops = &ide_cdrom_dops;
2368 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2369 if (attempts-- <= 0)
2370 return 0;
2371 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2372 } while (stat);
2374 if (buf.cap.lock == 0)
2375 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2376 if (buf.cap.eject)
2377 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2378 if (buf.cap.cd_r_write)
2379 CDROM_CONFIG_FLAGS (drive)->cd_r = 1;
2380 if (buf.cap.cd_rw_write)
2381 CDROM_CONFIG_FLAGS (drive)->cd_rw = 1;
2382 if (buf.cap.test_write)
2383 CDROM_CONFIG_FLAGS (drive)->test_write = 1;
2384 if (buf.cap.dvd_ram_read || buf.cap.dvd_r_read || buf.cap.dvd_rom)
2385 CDROM_CONFIG_FLAGS (drive)->dvd = 1;
2386 if (buf.cap.dvd_ram_write)
2387 CDROM_CONFIG_FLAGS (drive)->dvd_r = 1;
2388 if (buf.cap.dvd_r_write)
2389 CDROM_CONFIG_FLAGS (drive)->dvd_ram = 1;
2390 if (buf.cap.audio_play)
2391 CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
2392 if (buf.cap.mechtype == 0)
2393 CDROM_CONFIG_FLAGS (drive)->close_tray = 0;
2395 #if ! STANDARD_ATAPI
2396 if (cdi->sanyo_slot > 0) {
2397 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2398 nslots = 3;
2401 else
2402 #endif /* not STANDARD_ATAPI */
2403 if (buf.cap.mechtype == mechtype_individual_changer ||
2404 buf.cap.mechtype == mechtype_cartridge_changer) {
2405 struct atapi_mechstat_header mechbuf;
2407 stat = cdrom_read_mech_status (drive, (char*)&mechbuf,
2408 sizeof (mechbuf), NULL);
2409 if (!stat) {
2410 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2411 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 1;
2412 nslots = mechbuf.nslots;
2416 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2417 if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2418 CDROM_STATE_FLAGS (drive)->current_speed =
2419 (((unsigned int)buf.cap.curspeed) + (176/2)) / 176;
2420 CDROM_CONFIG_FLAGS (drive)->max_speed =
2421 (((unsigned int)buf.cap.maxspeed) + (176/2)) / 176;
2422 } else {
2423 CDROM_STATE_FLAGS (drive)->current_speed =
2424 (ntohs(buf.cap.curspeed) + (176/2)) / 176;
2425 CDROM_CONFIG_FLAGS (drive)->max_speed =
2426 (ntohs(buf.cap.maxspeed) + (176/2)) / 176;
2429 printk ("%s: ATAPI %dX %s",
2430 drive->name, CDROM_CONFIG_FLAGS (drive)->max_speed,
2431 (CDROM_CONFIG_FLAGS (drive)->dvd) ? "DVD-ROM" : "CD-ROM");
2433 if (CDROM_CONFIG_FLAGS (drive)->dvd_r|CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2434 printk (" DVD%s%s",
2435 (CDROM_CONFIG_FLAGS (drive)->dvd_r)? "-R" : "",
2436 (CDROM_CONFIG_FLAGS (drive)->dvd_ram)? "AM" : "");
2438 if (CDROM_CONFIG_FLAGS (drive)->cd_r|CDROM_CONFIG_FLAGS (drive)->cd_rw)
2439 printk (" CD%s%s",
2440 (CDROM_CONFIG_FLAGS (drive)->cd_r)? "-R" : "",
2441 (CDROM_CONFIG_FLAGS (drive)->cd_rw)? "/RW" : "");
2443 if (CDROM_CONFIG_FLAGS (drive)->is_changer)
2444 printk (" changer w/%d slots", nslots);
2445 else
2446 printk (" drive");
2448 printk (", %dkB Cache", ntohs(buf.cap.buffer_size));
2450 if (drive->using_dma) {
2451 if ((drive->id->field_valid & 4) &&
2452 (drive->id->dma_ultra & (drive->id->dma_ultra >> 8) & 7)) {
2453 printk(", UDMA"); /* UDMA BIOS-enabled! */
2454 } else if (drive->id->field_valid & 4) {
2455 printk(", (U)DMA"); /* Can be BIOS-enabled! */
2456 } else {
2457 printk(", DMA");
2460 printk("\n");
2462 return nslots;
2465 static void ide_cdrom_add_settings(ide_drive_t *drive)
2467 int major = HWIF(drive)->major;
2468 int minor = drive->select.b.unit << PARTN_BITS;
2470 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
2471 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
2472 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
2473 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2477 static
2478 int ide_cdrom_setup (ide_drive_t *drive)
2480 struct cdrom_info *info = drive->driver_data;
2481 struct cdrom_device_info *cdi = &info->devinfo;
2482 int minor = drive->select.b.unit << PARTN_BITS;
2483 int nslots;
2485 kdev_t dev = MKDEV(HWIF(drive)->major, minor);
2487 set_device_ro (dev, 1);
2488 blksize_size[HWIF(drive)->major][minor] = CD_FRAMESIZE;
2490 drive->special.all = 0;
2491 drive->ready_stat = 0;
2493 CDROM_STATE_FLAGS (drive)->media_changed = 1;
2494 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
2495 CDROM_STATE_FLAGS (drive)->door_locked = 0;
2497 #if NO_DOOR_LOCKING
2498 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2499 #else
2500 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2501 #endif
2503 if (drive->id != NULL)
2504 CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2505 ((drive->id->config & 0x0060) == 0x20);
2506 else
2507 CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2509 CDROM_CONFIG_FLAGS (drive)->is_changer = 0;
2510 CDROM_CONFIG_FLAGS (drive)->cd_r = 0;
2511 CDROM_CONFIG_FLAGS (drive)->cd_rw = 0;
2512 CDROM_CONFIG_FLAGS (drive)->test_write = 0;
2513 CDROM_CONFIG_FLAGS (drive)->dvd = 0;
2514 CDROM_CONFIG_FLAGS (drive)->dvd_r = 0;
2515 CDROM_CONFIG_FLAGS (drive)->dvd_ram = 0;
2516 CDROM_CONFIG_FLAGS (drive)->no_eject = 1;
2517 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 0;
2518 CDROM_CONFIG_FLAGS (drive)->audio_play = 0;
2519 CDROM_CONFIG_FLAGS (drive)->close_tray = 1;
2521 /* limit transfer size per interrupt. */
2522 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 0;
2523 if (drive->id != NULL) {
2524 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
2525 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2526 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
2527 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2530 #if ! STANDARD_ATAPI
2531 /* by default Sanyo 3 CD changer support is turned off and
2532 ATAPI Rev 2.2+ standard support for CD changers is used */
2533 cdi->sanyo_slot = 0;
2535 CDROM_CONFIG_FLAGS (drive)->nec260 = 0;
2536 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2537 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2538 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2539 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2541 if (drive->id != NULL) {
2542 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2543 drive->id->fw_rev[4] == '1' &&
2544 drive->id->fw_rev[6] <= '2') {
2545 /* Vertos 300.
2546 Some versions of this drive like to talk BCD. */
2547 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2548 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2549 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2550 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2553 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2554 drive->id->fw_rev[4] == '1' &&
2555 drive->id->fw_rev[6] <= '2') {
2556 /* Vertos 600 ESD. */
2557 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2560 else if (strcmp (drive->id->model,
2561 "NEC CD-ROM DRIVE:260") == 0 &&
2562 strncmp (drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
2563 /* Old NEC260 (not R).
2564 This drive was released before the 1.2 version
2565 of the spec. */
2566 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2567 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2568 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2569 CDROM_CONFIG_FLAGS (drive)->nec260 = 1;
2572 else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
2573 strncmp (drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
2574 /* Wearnes */
2575 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2576 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2579 /* Sanyo 3 CD changer uses a non-standard command
2580 for CD changing */
2581 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
2582 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
2583 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
2584 /* uses CD in slot 0 when value is set to 3 */
2585 cdi->sanyo_slot = 3;
2590 #endif /* not STANDARD_ATAPI */
2592 info->toc = NULL;
2593 info->sector_buffer = NULL;
2594 info->sector_buffered = 0;
2595 info->nsectors_buffered = 0;
2596 info->changer_info = NULL;
2598 nslots = ide_cdrom_probe_capabilities (drive);
2600 if (ide_cdrom_register (drive, nslots)) {
2601 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2602 info->devinfo.handle = NULL;
2603 return 1;
2605 ide_cdrom_add_settings(drive);
2606 return 0;
2609 /* Forwarding functions to generic routines. */
2610 static
2611 int ide_cdrom_ioctl (ide_drive_t *drive,
2612 struct inode *inode, struct file *file,
2613 unsigned int cmd, unsigned long arg)
2615 return cdrom_fops.ioctl (inode, file, cmd, arg);
2618 static
2619 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2621 int rc;
2623 MOD_INC_USE_COUNT;
2624 rc = cdrom_fops.open (ip, fp);
2625 if (rc) {
2626 drive->usage--;
2627 MOD_DEC_USE_COUNT;
2629 return rc;
2632 static
2633 void ide_cdrom_release (struct inode *inode, struct file *file,
2634 ide_drive_t *drive)
2636 cdrom_fops.release (inode, file);
2637 MOD_DEC_USE_COUNT;
2640 static
2641 int ide_cdrom_check_media_change (ide_drive_t *drive)
2643 return cdrom_fops.check_media_change
2644 (MKDEV (HWIF (drive)->major,
2645 (drive->select.b.unit)<<PARTN_BITS));
2648 static
2649 int ide_cdrom_cleanup(ide_drive_t *drive)
2651 struct cdrom_info *info = drive->driver_data;
2652 struct cdrom_device_info *devinfo = &info->devinfo;
2654 if (ide_unregister_subdriver (drive))
2655 return 1;
2656 if (info->sector_buffer != NULL)
2657 kfree (info->sector_buffer);
2658 if (info->toc != NULL)
2659 kfree (info->toc);
2660 if (info->changer_info != NULL)
2661 kfree (info->changer_info);
2662 if (devinfo->handle == drive && unregister_cdrom (devinfo))
2663 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
2664 kfree (info);
2665 drive->driver_data = NULL;
2666 return 0;
2669 static ide_driver_t ide_cdrom_driver = {
2670 "ide-cdrom", /* name */
2671 IDECD_VERSION, /* version */
2672 ide_cdrom, /* media */
2673 0, /* busy */
2674 1, /* supports_dma */
2675 1, /* supports_dsc_overlap */
2676 ide_cdrom_cleanup, /* cleanup */
2677 ide_do_rw_cdrom, /* do_request */
2678 NULL, /* ??? or perhaps cdrom_end_request? */
2679 ide_cdrom_ioctl, /* ioctl */
2680 ide_cdrom_open, /* open */
2681 ide_cdrom_release, /* release */
2682 ide_cdrom_check_media_change, /* media_change */
2683 NULL, /* pre_reset */
2684 NULL, /* capacity */
2685 NULL, /* special */
2686 NULL /* proc */
2689 int ide_cdrom_init (void);
2690 static ide_module_t ide_cdrom_module = {
2691 IDE_DRIVER_MODULE,
2692 ide_cdrom_init,
2693 &ide_cdrom_driver,
2694 NULL
2697 /* options */
2698 char *ignore = NULL;
2700 #ifdef MODULE
2701 MODULE_PARM(ignore, "s");
2702 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2704 int init_module (void)
2706 return ide_cdrom_init();
2709 void cleanup_module(void)
2711 ide_drive_t *drive;
2712 int failed = 0;
2714 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, &ide_cdrom_driver, failed)) != NULL)
2715 if (ide_cdrom_cleanup (drive)) {
2716 printk ("%s: cleanup_module() called while still busy\n", drive->name);
2717 failed++;
2719 ide_unregister_module (&ide_cdrom_module);
2721 #endif /* MODULE */
2723 int ide_cdrom_init (void)
2725 ide_drive_t *drive;
2726 struct cdrom_info *info;
2727 int failed = 0;
2729 MOD_INC_USE_COUNT;
2730 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, NULL, failed++)) != NULL) {
2731 /* skip drives that we were told to ignore */
2732 if (ignore != NULL) {
2733 if (strstr(ignore, drive->name)) {
2734 printk("ide-cd: ignoring drive %s\n", drive->name);
2735 continue;
2738 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
2739 if (info == NULL) {
2740 printk ("%s: Can't allocate a cdrom structure\n", drive->name);
2741 continue;
2743 if (ide_register_subdriver (drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
2744 printk ("%s: Failed to register the driver with ide.c\n", drive->name);
2745 kfree (info);
2746 continue;
2748 memset (info, 0, sizeof (struct cdrom_info));
2749 drive->driver_data = info;
2750 DRIVER(drive)->busy++;
2751 if (ide_cdrom_setup (drive)) {
2752 DRIVER(drive)->busy--;
2753 if (ide_cdrom_cleanup (drive))
2754 printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive->name);
2755 continue;
2757 DRIVER(drive)->busy--;
2758 failed--;
2760 ide_register_module(&ide_cdrom_module);
2761 MOD_DEC_USE_COUNT;
2762 return 0;