Make HZ_TO_STD macro name lowercase.
[linux-2.6/linux-mips.git] / drivers / ide / ide-cd.c
blobe8e67d5492a80bebc1f3ec7874d2ff422d78eafa
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 *************************************************************************/
290 #define IDECD_VERSION "4.58"
292 #include <linux/config.h>
293 #include <linux/module.h>
294 #include <linux/types.h>
295 #include <linux/kernel.h>
296 #include <linux/delay.h>
297 #include <linux/timer.h>
298 #include <linux/malloc.h>
299 #include <linux/interrupt.h>
300 #include <linux/errno.h>
301 #include <linux/cdrom.h>
302 #include <linux/ide.h>
304 #include <asm/irq.h>
305 #include <asm/io.h>
306 #include <asm/byteorder.h>
307 #include <asm/uaccess.h>
308 #include <asm/unaligned.h>
310 #include "ide-cd.h"
312 /****************************************************************************
313 * Generic packet command support and error handling routines.
316 /* Mark that we've seen a media change, and invalidate our internal
317 buffers. */
318 static void cdrom_saw_media_change (ide_drive_t *drive)
320 struct cdrom_info *info = drive->driver_data;
322 CDROM_STATE_FLAGS (drive)->media_changed = 1;
323 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
324 info->nsectors_buffered = 0;
328 static
329 void cdrom_analyze_sense_data(ide_drive_t *drive,
330 struct packet_command *failed_command,
331 struct request_sense *sense)
333 if (sense->sense_key == NOT_READY ||
334 sense->sense_key == UNIT_ATTENTION) {
335 /* Make good and sure we've seen this potential media change.
336 Some drives (i.e. Creative) fail to present the correct
337 sense key in the error register. */
338 cdrom_saw_media_change (drive);
341 /* Don't print not ready or unit attention errors for
342 READ_SUBCHANNEL. Workman (and probably other programs)
343 uses this command to poll the drive, and we don't want
344 to fill the syslog with useless errors. */
345 if (failed_command &&
346 (failed_command->c[0] == GPCMD_READ_SUBCHANNEL ||
347 failed_command->c[0] == GPCMD_TEST_UNIT_READY))
348 return;
351 if (sense->error_code == 0x70 && sense->sense_key == 0x02
352 && ((sense->asc == 0x3a && sense->ascq == 0x00) ||
353 (sense->asc == 0x04 && sense->ascq == 0x01)))
356 * Suppress the following errors:
357 * "Medium not present", "in progress of becoming ready",
358 * and "writing" to keep the noise level down to a dull roar.
360 return;
363 #if VERBOSE_IDE_CD_ERRORS
365 int i;
366 const char *s;
367 char buf[80];
369 printk ("ATAPI device %s:\n", drive->name);
370 if (sense->error_code==0x70)
371 printk(" Error: ");
372 else if (sense->error_code==0x71)
373 printk(" Deferred Error: ");
374 else if (sense->error_code == 0x7f)
375 printk(" Vendor-specific Error: ");
376 else
377 printk(" Unknown Error Type: ");
379 if (sense->sense_key < ARY_LEN(sense_key_texts))
380 s = sense_key_texts[sense->sense_key];
381 else
382 s = "bad sense key!";
384 printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
386 if (sense->asc == 0x40) {
387 sprintf(buf, "Diagnostic failure on component 0x%02x",
388 sense->ascq);
389 s = buf;
390 } else {
391 int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
392 unsigned long key = (sense->sense_key << 16);
393 key |= (sense->asc << 8);
394 if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
395 key |= sense->ascq;
396 s = NULL;
398 while (hi > lo) {
399 mid = (lo + hi) / 2;
400 if (sense_data_texts[mid].asc_ascq == key ||
401 sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
402 s = sense_data_texts[mid].text;
403 break;
405 else if (sense_data_texts[mid].asc_ascq > key)
406 hi = mid;
407 else
408 lo = mid+1;
412 if (s == NULL) {
413 if (sense->asc > 0x80)
414 s = "(vendor-specific error)";
415 else
416 s = "(reserved error code)";
419 printk(" %s -- (asc=0x%02x, ascq=0x%02x)\n",
420 s, sense->asc, sense->ascq);
422 if (failed_command != NULL) {
424 int lo=0, mid, hi= ARY_LEN (packet_command_texts);
425 s = NULL;
427 while (hi > lo) {
428 mid = (lo + hi) / 2;
429 if (packet_command_texts[mid].packet_command ==
430 failed_command->c[0]) {
431 s = packet_command_texts[mid].text;
432 break;
434 if (packet_command_texts[mid].packet_command >
435 failed_command->c[0])
436 hi = mid;
437 else
438 lo = mid+1;
441 printk (" The failed \"%s\" packet command was: \n \"", s);
442 for (i=0; i<sizeof (failed_command->c); i++)
443 printk ("%02x ", failed_command->c[i]);
444 printk ("\"\n");
447 /* The SKSV bit specifies validity of the sense_key_specific
448 * in the next two commands. It is bit 7 of the first byte.
449 * In the case of NOT_READY, if SKSV is set the drive can
450 * give us nice ETA readings.
452 if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
453 int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
454 printk(" Command is %02d%% complete\n", progress / 0xffff);
458 if (sense->sense_key == ILLEGAL_REQUEST &&
459 (sense->sks[0] & 0x80) != 0) {
460 printk(" Error in %s byte %d",
461 (sense->sks[0] & 0x40) != 0 ?
462 "command packet" : "command data",
463 (sense->sks[1] << 8) + sense->sks[2]);
465 if ((sense->sks[0] & 0x40) != 0)
466 printk (" bit %d", sense->sks[0] & 0x07);
468 printk ("\n");
472 #else /* not VERBOSE_IDE_CD_ERRORS */
474 /* Suppress printing unit attention and `in progress of becoming ready'
475 errors when we're not being verbose. */
477 if (sense->sense_key == UNIT_ATTENTION ||
478 (sense->sense_key == NOT_READY && (sense->asc == 4 ||
479 sense->asc == 0x3a)))
480 return;
482 printk("%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
483 drive->name,
484 sense->error_code, sense->sense_key,
485 sense->asc, sense->ascq);
486 #endif /* not VERBOSE_IDE_CD_ERRORS */
489 static void cdrom_queue_request_sense(ide_drive_t *drive,
490 struct semaphore *sem,
491 struct request_sense *sense,
492 struct packet_command *failed_command)
494 struct cdrom_info *info = drive->driver_data;
495 struct request *rq;
496 struct packet_command *pc = &info->request_sense_pc;
498 if (sense == NULL)
499 sense = &info->sense_data;
501 memset(pc, 0, sizeof(struct packet_command));
502 pc->c[0] = GPCMD_REQUEST_SENSE;
503 pc->c[4] = pc->buflen = 18;
504 pc->buffer = (char *) sense;
505 pc->sense = (struct request_sense *) failed_command;
507 /* stuff the sense request in front of our current request */
508 rq = &info->request_sense_request;
509 ide_init_drive_cmd(rq);
510 rq->cmd = REQUEST_SENSE_COMMAND;
511 rq->buffer = (char *) pc;
512 rq->sem = sem;
513 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
517 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
519 struct request *rq = HWGROUP(drive)->rq;
521 if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
522 struct packet_command *pc = (struct packet_command *)rq->buffer;
523 cdrom_analyze_sense_data(drive,
524 (struct packet_command *) pc->sense,
525 (struct request_sense *) (pc->buffer - pc->c[4]));
527 if (rq->cmd == READ && !rq->current_nr_sectors)
528 uptodate = 1;
530 ide_end_request (uptodate, HWGROUP(drive));
534 /* Returns 0 if the request should be continued.
535 Returns 1 if the request was ended. */
536 static int cdrom_decode_status (ide_startstop_t *startstop, ide_drive_t *drive, int good_stat,
537 int *stat_ret)
539 struct request *rq = HWGROUP(drive)->rq;
540 int stat, cmd, err, sense_key;
541 struct packet_command *pc;
543 /* Check for errors. */
544 stat = GET_STAT();
545 *stat_ret = stat;
547 if (OK_STAT (stat, good_stat, BAD_R_STAT))
548 return 0;
550 /* Get the IDE error register. */
551 err = GET_ERR();
552 sense_key = err >> 4;
554 if (rq == NULL)
555 printk ("%s: missing request in cdrom_decode_status\n",
556 drive->name);
557 else {
558 cmd = rq->cmd;
560 if (cmd == REQUEST_SENSE_COMMAND) {
561 /* We got an error trying to get sense info
562 from the drive (probably while trying
563 to recover from a former error). Just give up. */
565 pc = (struct packet_command *) rq->buffer;
566 pc->stat = 1;
567 cdrom_end_request (1, drive);
568 *startstop = ide_error (drive, "request sense failure", stat);
569 return 1;
571 } else if (cmd == PACKET_COMMAND) {
572 /* All other functions, except for READ. */
574 struct semaphore *sem = NULL;
575 pc = (struct packet_command *) rq->buffer;
577 /* Check for tray open. */
578 if (sense_key == NOT_READY) {
579 cdrom_saw_media_change (drive);
580 } else if (sense_key == UNIT_ATTENTION) {
581 /* Check for media change. */
582 cdrom_saw_media_change (drive);
583 /*printk("%s: media changed\n",drive->name);*/
584 return 0;
585 } else {
586 /* Otherwise, print an error. */
587 ide_dump_status (drive, "packet command error",
588 stat);
591 /* Set the error flag and complete the request.
592 Then, if we have a CHECK CONDITION status,
593 queue a request sense command. We must be careful,
594 though: we don't want the thread in
595 cdrom_queue_packet_command to wake up until
596 the request sense has completed. We do this
597 by transferring the semaphore from the packet
598 command request to the request sense request. */
600 if ((stat & ERR_STAT) != 0) {
601 sem = rq->sem;
602 rq->sem = NULL;
605 pc->stat = 1;
606 cdrom_end_request (1, drive);
608 if ((stat & ERR_STAT) != 0)
609 cdrom_queue_request_sense(drive, sem, pc->sense,
610 pc);
611 } else {
612 /* Handle errors from READ requests. */
614 if (sense_key == NOT_READY) {
615 /* Tray open. */
616 cdrom_saw_media_change (drive);
618 /* Fail the request. */
619 printk ("%s: tray open\n", drive->name);
620 cdrom_end_request (0, drive);
621 } else if (sense_key == UNIT_ATTENTION) {
622 /* Media change. */
623 cdrom_saw_media_change (drive);
625 /* Arrange to retry the request.
626 But be sure to give up if we've retried
627 too many times. */
628 if (++rq->errors > ERROR_MAX)
629 cdrom_end_request (0, drive);
630 } else if (sense_key == ILLEGAL_REQUEST ||
631 sense_key == DATA_PROTECT) {
632 /* No point in retrying after an illegal
633 request or data protect error.*/
634 ide_dump_status (drive, "command error", stat);
635 cdrom_end_request (0, drive);
636 } else if ((err & ~ABRT_ERR) != 0) {
637 /* Go to the default handler
638 for other errors. */
639 *startstop = ide_error (drive, "cdrom_decode_status", stat);
640 return 1;
641 } else if ((++rq->errors > ERROR_MAX)) {
642 /* We've racked up too many retries. Abort. */
643 cdrom_end_request (0, drive);
646 /* If we got a CHECK_CONDITION status,
647 queue a request sense command. */
648 if ((stat & ERR_STAT) != 0)
649 cdrom_queue_request_sense(drive, NULL, NULL, NULL);
653 /* Retry, or handle the next request. */
654 *startstop = ide_stopped;
655 return 1;
658 static int cdrom_timer_expiry(ide_drive_t *drive)
660 struct request *rq = HWGROUP(drive)->rq;
661 struct packet_command *pc = (struct packet_command *) rq->buffer;
662 unsigned long wait = 0;
664 /* blank and format can take an extremly long time to
665 * complete, if the IMMED bit was not set.
667 if (pc->c[0] == GPCMD_BLANK || pc->c[0] == GPCMD_FORMAT_UNIT)
668 wait = 60*60*HZ;
670 return wait;
673 /* Set up the device registers for transferring a packet command on DEV,
674 expecting to later transfer XFERLEN bytes. HANDLER is the routine
675 which actually transfers the command to the drive. If this is a
676 drq_interrupt device, this routine will arrange for HANDLER to be
677 called when the interrupt from the drive arrives. Otherwise, HANDLER
678 will be called immediately after the drive is prepared for the transfer. */
680 static ide_startstop_t cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
681 ide_handler_t *handler)
683 ide_startstop_t startstop;
684 struct cdrom_info *info = drive->driver_data;
686 /* Wait for the controller to be idle. */
687 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
688 return startstop;
690 if (info->dma)
691 info->dma = !HWIF(drive)->dmaproc(ide_dma_read, drive);
693 /* Set up the controller registers. */
694 OUT_BYTE (info->dma, IDE_FEATURE_REG);
695 OUT_BYTE (0, IDE_NSECTOR_REG);
696 OUT_BYTE (0, IDE_SECTOR_REG);
698 OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
699 OUT_BYTE (xferlen >> 8 , IDE_HCYL_REG);
700 if (IDE_CONTROL_REG)
701 OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
703 if (info->dma)
704 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
706 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
707 ide_set_handler (drive, handler, WAIT_CMD, cdrom_timer_expiry);
708 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
709 return ide_started;
710 } else {
711 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
712 return (*handler) (drive);
716 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
717 The device registers must have already been prepared
718 by cdrom_start_packet_command.
719 HANDLER is the interrupt handler to call when the command completes
720 or there's data ready. */
721 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
722 unsigned char *cmd_buf, int cmd_len,
723 ide_handler_t *handler)
725 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
726 /* Here we should have been called after receiving an interrupt
727 from the device. DRQ should how be set. */
728 int stat_dum;
729 ide_startstop_t startstop;
731 /* Check for errors. */
732 if (cdrom_decode_status (&startstop, drive, DRQ_STAT, &stat_dum))
733 return startstop;
734 } else {
735 ide_startstop_t startstop;
736 /* Otherwise, we must wait for DRQ to get set. */
737 if (ide_wait_stat (&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
738 return startstop;
741 /* Arm the interrupt handler. */
742 ide_set_handler (drive, handler, WAIT_CMD, cdrom_timer_expiry);
744 /* Send the command to the device. */
745 atapi_output_bytes (drive, cmd_buf, cmd_len);
747 return ide_started;
752 /****************************************************************************
753 * Block read functions.
757 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
758 * buffer. Once the first sector is added, any subsequent sectors are
759 * assumed to be continuous (until the buffer is cleared). For the first
760 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
761 * the buffer is cleared.)
763 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
764 int sectors_to_transfer)
766 struct cdrom_info *info = drive->driver_data;
768 /* Number of sectors to read into the buffer. */
769 int sectors_to_buffer = MIN (sectors_to_transfer,
770 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
771 info->nsectors_buffered);
773 char *dest;
775 /* If we couldn't get a buffer, don't try to buffer anything... */
776 if (info->buffer == NULL)
777 sectors_to_buffer = 0;
779 /* If this is the first sector in the buffer, remember its number. */
780 if (info->nsectors_buffered == 0)
781 info->sector_buffered = sector;
783 /* Read the data into the buffer. */
784 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
785 while (sectors_to_buffer > 0) {
786 atapi_input_bytes (drive, dest, SECTOR_SIZE);
787 --sectors_to_buffer;
788 --sectors_to_transfer;
789 ++info->nsectors_buffered;
790 dest += SECTOR_SIZE;
793 /* Throw away any remaining data. */
794 while (sectors_to_transfer > 0) {
795 char dum[SECTOR_SIZE];
796 atapi_input_bytes (drive, dum, sizeof (dum));
797 --sectors_to_transfer;
802 * Check the contents of the interrupt reason register from the cdrom
803 * and attempt to recover if there are problems. Returns 0 if everything's
804 * ok; nonzero if the request has been terminated.
806 static inline
807 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
809 ireason &= 3;
810 if (ireason == 2) return 0;
812 if (ireason == 0) {
813 /* Whoops... The drive is expecting to receive data from us! */
814 printk ("%s: cdrom_read_intr: "
815 "Drive wants to transfer data the wrong way!\n",
816 drive->name);
818 /* Throw some data at the drive so it doesn't hang
819 and quit this request. */
820 while (len > 0) {
821 int dum = 0;
822 atapi_output_bytes (drive, &dum, sizeof (dum));
823 len -= sizeof (dum);
825 } else if (ireason == 1) {
826 /* Some drives (ASUS) seem to tell us that status
827 * info is available. just get it and ignore.
829 GET_STAT();
830 return 0;
831 } else {
832 /* Drive wants a command packet, or invalid ireason... */
833 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
834 drive->name, ireason);
837 cdrom_end_request (0, drive);
838 return -1;
842 * Interrupt routine. Called when a read request has completed.
844 static ide_startstop_t 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;
850 ide_startstop_t startstop;
852 struct request *rq = HWGROUP(drive)->rq;
854 /* Check for errors. */
855 if (dma) {
856 info->dma = 0;
857 if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))
858 HWIF(drive)->dmaproc(ide_dma_off, drive);
861 if (cdrom_decode_status (&startstop, drive, 0, &stat))
862 return startstop;
864 if (dma) {
865 if (!dma_error) {
866 for (i = rq->nr_sectors; i > 0;) {
867 i -= rq->current_nr_sectors;
868 ide_end_request(1, HWGROUP(drive));
870 return ide_stopped;
871 } else
872 return ide_error (drive, "dma error", stat);
875 /* Read the interrupt reason and the transfer length. */
876 ireason = IN_BYTE (IDE_NSECTOR_REG);
877 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
879 /* If DRQ is clear, the command has completed. */
880 if ((stat & DRQ_STAT) == 0) {
881 /* If we're not done filling the current buffer, complain.
882 Otherwise, complete the command normally. */
883 if (rq->current_nr_sectors > 0) {
884 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
885 drive->name, rq->current_nr_sectors);
886 cdrom_end_request (0, drive);
887 } else
888 cdrom_end_request (1, drive);
889 return ide_stopped;
892 /* Check that the drive is expecting to do the same thing we are. */
893 if (cdrom_read_check_ireason (drive, len, ireason))
894 return ide_stopped;
896 /* Assume that the drive will always provide data in multiples
897 of at least SECTOR_SIZE, as it gets hairy to keep track
898 of the transfers otherwise. */
899 if ((len % SECTOR_SIZE) != 0) {
900 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
901 drive->name, len);
902 if (CDROM_CONFIG_FLAGS (drive)->limit_nframes)
903 printk (" This drive is not supported by this version of the driver\n");
904 else {
905 printk (" Trying to limit transfer sizes\n");
906 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
908 cdrom_end_request (0, drive);
909 return ide_stopped;
912 /* The number of sectors we need to read from the drive. */
913 sectors_to_transfer = len / SECTOR_SIZE;
915 /* First, figure out if we need to bit-bucket
916 any of the leading sectors. */
917 nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
918 sectors_to_transfer);
920 while (nskip > 0) {
921 /* We need to throw away a sector. */
922 char dum[SECTOR_SIZE];
923 atapi_input_bytes (drive, dum, sizeof (dum));
925 --rq->current_nr_sectors;
926 --nskip;
927 --sectors_to_transfer;
930 /* Now loop while we still have data to read from the drive. */
931 while (sectors_to_transfer > 0) {
932 int this_transfer;
934 /* If we've filled the present buffer but there's another
935 chained buffer after it, move on. */
936 if (rq->current_nr_sectors == 0 &&
937 rq->nr_sectors > 0)
938 cdrom_end_request (1, drive);
940 /* If the buffers are full, cache the rest of the data in our
941 internal buffer. */
942 if (rq->current_nr_sectors == 0) {
943 cdrom_buffer_sectors(drive, 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, rq->buffer, SECTOR_SIZE);
956 rq->buffer += SECTOR_SIZE;
957 --rq->nr_sectors;
958 --rq->current_nr_sectors;
959 ++rq->sector;
960 --this_transfer;
961 --sectors_to_transfer;
966 /* Done moving data!
967 Wait for another interrupt. */
968 ide_set_handler(drive, &cdrom_read_intr, WAIT_CMD, NULL);
969 return ide_started;
973 * Try to satisfy some of the current read request from our cached data.
974 * Returns nonzero if the request has been completed, zero otherwise.
976 static int cdrom_read_from_buffer (ide_drive_t *drive)
978 struct cdrom_info *info = drive->driver_data;
979 struct request *rq = HWGROUP(drive)->rq;
981 /* Can't do anything if there's no buffer. */
982 if (info->buffer == NULL) return 0;
984 /* Loop while this request needs data and the next block is present
985 in our cache. */
986 while (rq->nr_sectors > 0 &&
987 rq->sector >= info->sector_buffered &&
988 rq->sector < info->sector_buffered + info->nsectors_buffered) {
989 if (rq->current_nr_sectors == 0)
990 cdrom_end_request (1, drive);
992 memcpy (rq->buffer,
993 info->buffer +
994 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
995 SECTOR_SIZE);
996 rq->buffer += SECTOR_SIZE;
997 --rq->current_nr_sectors;
998 --rq->nr_sectors;
999 ++rq->sector;
1002 /* If we've satisfied the current request,
1003 terminate it successfully. */
1004 if (rq->nr_sectors == 0) {
1005 cdrom_end_request (1, drive);
1006 return -1;
1009 /* Move on to the next buffer if needed. */
1010 if (rq->current_nr_sectors == 0)
1011 cdrom_end_request (1, drive);
1013 /* If this condition does not hold, then the kluge i use to
1014 represent the number of sectors to skip at the start of a transfer
1015 will fail. I think that this will never happen, but let's be
1016 paranoid and check. */
1017 if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1018 (rq->sector % SECTORS_PER_FRAME) != 0) {
1019 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1020 drive->name, rq->sector);
1021 cdrom_end_request (0, drive);
1022 return -1;
1025 return 0;
1029 * Routine to send a read packet command to the drive.
1030 * This is usually called directly from cdrom_start_read.
1031 * However, for drq_interrupt devices, it is called from an interrupt
1032 * when the drive is ready to accept the command.
1034 static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1036 struct packet_command pc;
1037 struct request *rq = HWGROUP(drive)->rq;
1038 int nsect, sector, nframes, frame, nskip;
1040 /* Number of sectors to transfer. */
1041 nsect = rq->nr_sectors;
1043 /* Starting sector. */
1044 sector = rq->sector;
1046 /* If the requested sector doesn't start on a cdrom block boundary,
1047 we must adjust the start of the transfer so that it does,
1048 and remember to skip the first few sectors.
1049 If the CURRENT_NR_SECTORS field is larger than the size
1050 of the buffer, it will mean that we're to skip a number
1051 of sectors equal to the amount by which CURRENT_NR_SECTORS
1052 is larger than the buffer size. */
1053 nskip = (sector % SECTORS_PER_FRAME);
1054 if (nskip > 0) {
1055 /* Sanity check... */
1056 if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS) &&
1057 (rq->sector % CD_FRAMESIZE != 0)) {
1058 printk ("%s: cdrom_start_read_continuation: buffer botch (%lu)\n",
1059 drive->name, rq->current_nr_sectors);
1060 cdrom_end_request (0, drive);
1061 return ide_stopped;
1063 sector -= nskip;
1064 nsect += nskip;
1065 rq->current_nr_sectors += nskip;
1068 /* Convert from sectors to cdrom blocks, rounding up the transfer
1069 length if needed. */
1070 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1071 frame = sector / SECTORS_PER_FRAME;
1073 /* Largest number of frames was can transfer at once is 64k-1. For
1074 some drives we need to limit this even more. */
1075 nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ?
1076 (65534 / CD_FRAMESIZE) : 65535);
1078 /* Set up the command */
1079 memset (&pc.c, 0, sizeof (pc.c));
1080 pc.c[0] = GPCMD_READ_10;
1081 pc.c[7] = (nframes >> 8);
1082 pc.c[8] = (nframes & 0xff);
1083 put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1085 /* Send the command to the drive and return. */
1086 return cdrom_transfer_packet_command(drive, pc.c, sizeof(pc.c),
1087 &cdrom_read_intr);
1091 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1092 #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
1093 #define IDECD_SEEK_TIMEOUT WAIT_CMD /* 10 sec */
1095 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1097 struct cdrom_info *info = drive->driver_data;
1098 int stat;
1099 static int retry = 10;
1100 ide_startstop_t startstop;
1102 if (cdrom_decode_status (&startstop, drive, 0, &stat))
1103 return startstop;
1104 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1106 if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {
1107 if (--retry == 0) {
1108 printk("%s: disabled DSC seek overlap\n", drive->name);
1109 drive->dsc_overlap = 0;
1112 return ide_stopped;
1115 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1117 struct packet_command pc;
1118 struct request *rq = HWGROUP(drive)->rq;
1119 int sector, frame, nskip;
1121 sector = rq->sector;
1122 nskip = (sector % SECTORS_PER_FRAME);
1123 if (nskip > 0)
1124 sector -= nskip;
1125 frame = sector / SECTORS_PER_FRAME;
1127 memset (&pc.c, 0, sizeof (pc.c));
1128 pc.c[0] = GPCMD_SEEK;
1129 put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1130 return cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c), &cdrom_seek_intr);
1133 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1135 struct cdrom_info *info = drive->driver_data;
1137 info->dma = 0;
1138 info->start_seek = jiffies;
1139 return cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);
1142 /* Fix up a possibly partially-processed request so that we can
1143 start it over entirely, or even put it back on the request queue. */
1144 static void restore_request (struct request *rq)
1146 if (rq->buffer != rq->bh->b_data) {
1147 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
1148 rq->buffer = rq->bh->b_data;
1149 rq->nr_sectors += n;
1150 rq->sector -= n;
1152 rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
1156 * Start a read request from the CD-ROM.
1158 static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1160 struct cdrom_info *info = drive->driver_data;
1161 struct request *rq = HWGROUP(drive)->rq;
1162 int minor = MINOR (rq->rq_dev);
1164 /* If the request is relative to a partition, fix it up to refer to the
1165 absolute address. */
1166 if ((minor & PARTN_MASK) != 0) {
1167 rq->sector = block;
1168 minor &= ~PARTN_MASK;
1169 rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1172 /* We may be retrying this request after an error. Fix up
1173 any weirdness which might be present in the request packet. */
1174 restore_request (rq);
1176 /* Satisfy whatever we can of this request from our cached sector. */
1177 if (cdrom_read_from_buffer(drive))
1178 return ide_stopped;
1180 /* Clear the local sector buffer. */
1181 info->nsectors_buffered = 0;
1183 /* use dma, if possible. */
1184 if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) &&
1185 (rq->nr_sectors % SECTORS_PER_FRAME == 0))
1186 info->dma = 1;
1187 else
1188 info->dma = 0;
1190 /* Start sending the read request to the drive. */
1191 return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1194 /****************************************************************************
1195 * Execute all other packet commands.
1198 /* Forward declarations. */
1199 static int cdrom_lockdoor(ide_drive_t *drive, int lockflag,
1200 struct request_sense *sense);
1202 /* Interrupt routine for packet command completion. */
1203 static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1205 int ireason, len, stat, thislen;
1206 struct request *rq = HWGROUP(drive)->rq;
1207 struct packet_command *pc = (struct packet_command *)rq->buffer;
1208 ide_startstop_t startstop;
1210 /* Check for errors. */
1211 if (cdrom_decode_status (&startstop, drive, 0, &stat))
1212 return startstop;
1214 /* Read the interrupt reason and the transfer length. */
1215 ireason = IN_BYTE (IDE_NSECTOR_REG);
1216 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1218 /* If DRQ is clear, the command has completed.
1219 Complain if we still have data left to transfer. */
1220 if ((stat & DRQ_STAT) == 0) {
1221 /* Some of the trailing request sense fields are optional, and
1222 some drives don't send them. Sigh. */
1223 if (pc->c[0] == GPCMD_REQUEST_SENSE &&
1224 pc->buflen > 0 &&
1225 pc->buflen <= 5) {
1226 while (pc->buflen > 0) {
1227 *pc->buffer++ = 0;
1228 --pc->buflen;
1232 if (pc->buflen == 0)
1233 cdrom_end_request (1, drive);
1234 else {
1235 /* Comment this out, because this always happens
1236 right after a reset occurs, and it is annoying to
1237 always print expected stuff. */
1239 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1240 drive->name, pc->buflen);
1242 pc->stat = 1;
1243 cdrom_end_request (1, drive);
1245 return ide_stopped;
1248 /* Figure out how much data to transfer. */
1249 thislen = pc->buflen;
1250 if (thislen > len) thislen = len;
1252 /* The drive wants to be written to. */
1253 if ((ireason & 3) == 0) {
1254 /* Transfer the data. */
1255 atapi_output_bytes (drive, pc->buffer, thislen);
1257 /* If we haven't moved enough data to satisfy the drive,
1258 add some padding. */
1259 while (len > thislen) {
1260 int dum = 0;
1261 atapi_output_bytes (drive, &dum, sizeof (dum));
1262 len -= sizeof (dum);
1265 /* Keep count of how much data we've moved. */
1266 pc->buffer += thislen;
1267 pc->buflen -= thislen;
1270 /* Same drill for reading. */
1271 else if ((ireason & 3) == 2) {
1273 /* Transfer the data. */
1274 atapi_input_bytes (drive, pc->buffer, thislen);
1276 /* If we haven't moved enough data to satisfy the drive,
1277 add some padding. */
1278 while (len > thislen) {
1279 int dum = 0;
1280 atapi_input_bytes (drive, &dum, sizeof (dum));
1281 len -= sizeof (dum);
1284 /* Keep count of how much data we've moved. */
1285 pc->buffer += thislen;
1286 pc->buflen -= thislen;
1287 } else {
1288 printk ("%s: cdrom_pc_intr: The drive "
1289 "appears confused (ireason = 0x%2x)\n",
1290 drive->name, ireason);
1291 pc->stat = 1;
1294 /* Now we wait for another interrupt. */
1295 ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD, cdrom_timer_expiry);
1296 return ide_started;
1300 static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1302 struct request *rq = HWGROUP(drive)->rq;
1303 struct packet_command *pc = (struct packet_command *)rq->buffer;
1305 /* Send the command to the drive and return. */
1306 return cdrom_transfer_packet_command (drive, pc->c,
1307 sizeof (pc->c), &cdrom_pc_intr);
1311 static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1313 int len;
1314 struct request *rq = HWGROUP(drive)->rq;
1315 struct packet_command *pc = (struct packet_command *)rq->buffer;
1316 struct cdrom_info *info = drive->driver_data;
1318 info->dma = 0;
1319 pc->stat = 0;
1320 len = pc->buflen;
1322 /* Start sending the command to the drive. */
1323 return cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1327 /* Sleep for TIME jiffies.
1328 Not to be called from an interrupt handler. */
1329 static
1330 void cdrom_sleep (int time)
1332 current->state = TASK_INTERRUPTIBLE;
1333 schedule_timeout(time);
1336 static
1337 int cdrom_queue_packet_command(ide_drive_t *drive, struct packet_command *pc)
1339 struct request_sense sense;
1340 struct request req;
1341 int retries = 10;
1343 if (pc->sense == NULL)
1344 pc->sense = &sense;
1346 /* Start of retry loop. */
1347 do {
1348 ide_init_drive_cmd (&req);
1349 req.cmd = PACKET_COMMAND;
1350 req.buffer = (char *)pc;
1351 if (ide_do_drive_cmd (drive, &req, ide_wait)) {
1352 printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
1353 drive->name, req.buffer[0], req.buffer[1]);
1354 /* FIXME: we should probably abort/retry or something */
1356 if (pc->stat != 0) {
1357 /* The request failed. Retry if it was due to a unit
1358 attention status
1359 (usually means media was changed). */
1360 struct request_sense *reqbuf = pc->sense;
1362 if (reqbuf->sense_key == UNIT_ATTENTION)
1363 cdrom_saw_media_change (drive);
1364 else if (reqbuf->sense_key == NOT_READY &&
1365 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1366 /* The drive is in the process of loading
1367 a disk. Retry, but wait a little to give
1368 the drive time to complete the load. */
1369 cdrom_sleep (HZ);
1370 } else {
1371 /* Otherwise, don't retry. */
1372 retries = 0;
1374 --retries;
1377 /* End of retry loop. */
1378 } while (pc->stat != 0 && retries >= 0);
1380 /* Return an error if the command failed. */
1381 return pc->stat ? -EIO : 0;
1384 /****************************************************************************
1385 * cdrom driver request routine.
1387 static ide_startstop_t
1388 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, unsigned long block)
1390 ide_startstop_t action;
1391 struct cdrom_info *info = drive->driver_data;
1393 switch (rq->cmd) {
1394 case READ: {
1395 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1396 unsigned long elpased = jiffies - info->start_seek;
1397 int stat = GET_STAT();
1399 if ((stat & SEEK_STAT) != SEEK_STAT) {
1400 if (elpased < IDECD_SEEK_TIMEOUT) {
1401 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1402 return ide_stopped;
1404 printk ("%s: DSC timeout\n", drive->name);
1406 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1408 if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap)
1409 action = cdrom_start_seek (drive, block);
1410 else
1411 action = cdrom_start_read (drive, block);
1412 info->last_block = block;
1413 return action;
1416 case PACKET_COMMAND:
1417 case REQUEST_SENSE_COMMAND: {
1418 return cdrom_do_packet_command(drive);
1421 case RESET_DRIVE_COMMAND: {
1422 cdrom_end_request(1, drive);
1423 return ide_do_reset(drive);
1426 default: {
1427 printk("ide-cd: bad cmd %d\n", rq -> cmd);
1428 cdrom_end_request(0, drive);
1429 return ide_stopped;
1436 /****************************************************************************
1437 * Ioctl handling.
1439 * Routines which queue packet commands take as a final argument a pointer
1440 * to a request_sense struct. If execution of the command results
1441 * in an error with a CHECK CONDITION status, this structure will be filled
1442 * with the results of the subsequent request sense command. The pointer
1443 * can also be NULL, in which case no sense information is returned.
1446 #if ! STANDARD_ATAPI
1447 static inline
1448 int bin2bcd (int x)
1450 return (x%10) | ((x/10) << 4);
1454 static inline
1455 int bcd2bin (int x)
1457 return (x >> 4) * 10 + (x & 0x0f);
1460 static
1461 void msf_from_bcd (struct atapi_msf *msf)
1463 msf->minute = bcd2bin (msf->minute);
1464 msf->second = bcd2bin (msf->second);
1465 msf->frame = bcd2bin (msf->frame);
1468 #endif /* not STANDARD_ATAPI */
1471 static inline
1472 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1474 lba += CD_MSF_OFFSET;
1475 lba &= 0xffffff; /* negative lbas use only 24 bits */
1476 *m = lba / (CD_SECS * CD_FRAMES);
1477 lba %= (CD_SECS * CD_FRAMES);
1478 *s = lba / CD_FRAMES;
1479 *f = lba % CD_FRAMES;
1483 static inline
1484 int msf_to_lba (byte m, byte s, byte f)
1486 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1489 static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1491 struct packet_command pc;
1492 struct cdrom_info *info = drive->driver_data;
1493 struct cdrom_device_info *cdi = &info->devinfo;
1495 memset(&pc, 0, sizeof(pc));
1496 pc.sense = sense;
1498 pc.c[0] = GPCMD_TEST_UNIT_READY;
1500 #if ! STANDARD_ATAPI
1501 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1502 switch CDs instead of supporting the LOAD_UNLOAD opcode */
1504 pc.c[7] = cdi->sanyo_slot % 3;
1505 #endif /* not STANDARD_ATAPI */
1507 return cdrom_queue_packet_command(drive, &pc);
1511 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1512 static int
1513 cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
1515 struct request_sense my_sense;
1516 struct packet_command pc;
1517 int stat;
1519 if (sense == NULL)
1520 sense = &my_sense;
1522 /* If the drive cannot lock the door, just pretend. */
1523 if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
1524 stat = 0;
1525 } else {
1526 memset(&pc, 0, sizeof(pc));
1527 pc.sense = sense;
1528 pc.c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1529 pc.c[4] = lockflag ? 1 : 0;
1530 stat = cdrom_queue_packet_command (drive, &pc);
1533 /* If we got an illegal field error, the drive
1534 probably cannot lock the door. */
1535 if (stat != 0 &&
1536 sense->sense_key == ILLEGAL_REQUEST &&
1537 (sense->asc == 0x24 || sense->asc == 0x20)) {
1538 printk ("%s: door locking not supported\n",
1539 drive->name);
1540 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1541 stat = 0;
1544 /* no medium, that's alright. */
1545 if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
1546 stat = 0;
1548 if (stat == 0)
1549 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1551 return stat;
1555 /* Eject the disk if EJECTFLAG is 0.
1556 If EJECTFLAG is 1, try to reload the disk. */
1557 static int cdrom_eject(ide_drive_t *drive, int ejectflag,
1558 struct request_sense *sense)
1560 struct packet_command pc;
1562 if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
1563 return -EDRIVE_CANT_DO_THIS;
1565 /* reload fails on some drives, if the tray is locked */
1566 if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
1567 return 0;
1569 memset(&pc, 0, sizeof (pc));
1570 pc.sense = sense;
1572 pc.c[0] = GPCMD_START_STOP_UNIT;
1573 pc.c[4] = 0x02 + (ejectflag != 0);
1574 return cdrom_queue_packet_command (drive, &pc);
1577 static int cdrom_read_capacity(ide_drive_t *drive, unsigned *capacity,
1578 struct request_sense *sense)
1580 struct {
1581 __u32 lba;
1582 __u32 blocklen;
1583 } capbuf;
1585 int stat;
1586 struct packet_command pc;
1588 memset(&pc, 0, sizeof(pc));
1589 pc.sense = sense;
1591 pc.c[0] = GPCMD_READ_CDVD_CAPACITY;
1592 pc.buffer = (char *)&capbuf;
1593 pc.buflen = sizeof(capbuf);
1595 stat = cdrom_queue_packet_command(drive, &pc);
1596 if (stat == 0)
1597 *capacity = 1 + be32_to_cpu(capbuf.lba);
1599 return stat;
1602 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1603 int format, char *buf, int buflen,
1604 struct request_sense *sense)
1606 struct packet_command pc;
1608 memset(&pc, 0, sizeof(pc));
1609 pc.sense = sense;
1611 pc.buffer = buf;
1612 pc.buflen = buflen;
1613 pc.c[0] = GPCMD_READ_TOC_PMA_ATIP;
1614 pc.c[6] = trackno;
1615 pc.c[7] = (buflen >> 8);
1616 pc.c[8] = (buflen & 0xff);
1617 pc.c[9] = (format << 6);
1619 if (msf_flag)
1620 pc.c[1] = 2;
1622 return cdrom_queue_packet_command (drive, &pc);
1626 /* Try to read the entire TOC for the disk into our internal buffer. */
1627 static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
1629 int minor, stat, ntracks, i;
1630 kdev_t dev;
1631 struct cdrom_info *info = drive->driver_data;
1632 struct atapi_toc *toc = info->toc;
1633 struct {
1634 struct atapi_toc_header hdr;
1635 struct atapi_toc_entry ent;
1636 } ms_tmp;
1638 if (toc == NULL) {
1639 /* Try to allocate space. */
1640 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1641 GFP_KERNEL);
1642 info->toc = toc;
1643 if (toc == NULL) {
1644 printk ("%s: No cdrom TOC buffer!\n", drive->name);
1645 return -ENOMEM;
1649 /* Check to see if the existing data is still valid.
1650 If it is, just return. */
1651 if (CDROM_STATE_FLAGS (drive)->toc_valid)
1652 (void) cdrom_check_status(drive, sense);
1654 if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1656 /* First read just the header, so we know how long the TOC is. */
1657 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1658 sizeof(struct atapi_toc_header), sense);
1659 if (stat) return stat;
1661 #if ! STANDARD_ATAPI
1662 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1663 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1664 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1666 #endif /* not STANDARD_ATAPI */
1668 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1669 if (ntracks <= 0)
1670 return -EIO;
1671 if (ntracks > MAX_TRACKS)
1672 ntracks = MAX_TRACKS;
1674 /* Now read the whole schmeer. */
1675 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1676 (char *)&toc->hdr,
1677 sizeof(struct atapi_toc_header) +
1678 (ntracks + 1) *
1679 sizeof(struct atapi_toc_entry), sense);
1681 if (stat && toc->hdr.first_track > 1) {
1682 /* Cds with CDI tracks only don't have any TOC entries,
1683 despite of this the returned values are
1684 first_track == last_track = number of CDI tracks + 1,
1685 so that this case is indistinguishable from the same
1686 layout plus an additional audio track.
1687 If we get an error for the regular case, we assume
1688 a CDI without additional audio tracks. In this case
1689 the readable TOC is empty (CDI tracks are not included)
1690 and only holds the Leadout entry. Heiko Eißfeldt */
1691 ntracks = 0;
1692 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1693 (char *)&toc->hdr,
1694 sizeof(struct atapi_toc_header) +
1695 (ntracks + 1) *
1696 sizeof(struct atapi_toc_entry),
1697 sense);
1698 if (stat) {
1699 return stat;
1701 #if ! STANDARD_ATAPI
1702 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1703 toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
1704 toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
1705 } else
1706 #endif /* not STANDARD_ATAPI */
1708 toc->hdr.first_track = CDROM_LEADOUT;
1709 toc->hdr.last_track = CDROM_LEADOUT;
1711 } else if (stat) {
1712 return stat;
1714 if (stat) return stat;
1716 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1718 #if ! STANDARD_ATAPI
1719 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1720 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1721 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1723 #endif /* not STANDARD_ATAPI */
1725 for (i=0; i<=ntracks; i++) {
1726 #if ! STANDARD_ATAPI
1727 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1728 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1729 toc->ent[i].track = bcd2bin (toc->ent[i].track);
1730 msf_from_bcd (&toc->ent[i].addr.msf);
1732 #endif /* not STANDARD_ATAPI */
1733 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1734 toc->ent[i].addr.msf.second,
1735 toc->ent[i].addr.msf.frame);
1738 /* Read the multisession information. */
1739 if (toc->hdr.first_track != CDROM_LEADOUT) {
1740 /* Read the multisession information. */
1741 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1742 sizeof(ms_tmp), sense);
1743 if (stat) return stat;
1744 } else {
1745 ms_tmp.ent.addr.msf.minute = 0;
1746 ms_tmp.ent.addr.msf.second = 2;
1747 ms_tmp.ent.addr.msf.frame = 0;
1748 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
1751 #if ! STANDARD_ATAPI
1752 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1753 msf_from_bcd (&ms_tmp.ent.addr.msf);
1754 #endif /* not STANDARD_ATAPI */
1756 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1757 ms_tmp.ent.addr.msf.second,
1758 ms_tmp.ent.addr.msf.frame);
1760 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1762 /* Now try to get the total cdrom capacity. */
1763 minor = (drive->select.b.unit) << PARTN_BITS;
1764 dev = MKDEV(HWIF(drive)->major, minor);
1765 stat = cdrom_get_last_written(dev, (long *)&toc->capacity);
1766 if (stat)
1767 stat = cdrom_read_capacity(drive, &toc->capacity, sense);
1768 if (stat)
1769 toc->capacity = 0x1fffff;
1771 HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS] = (toc->capacity * SECTORS_PER_FRAME) >> (BLOCK_SIZE_BITS - 9);
1772 drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1774 /* Remember that we've read this stuff. */
1775 CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1777 return 0;
1781 static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
1782 int buflen, struct request_sense *sense)
1784 struct packet_command pc;
1786 memset(&pc, 0, sizeof(pc));
1787 pc.sense = sense;
1789 pc.buffer = buf;
1790 pc.buflen = buflen;
1791 pc.c[0] = GPCMD_READ_SUBCHANNEL;
1792 pc.c[1] = 2; /* MSF addressing */
1793 pc.c[2] = 0x40; /* request subQ data */
1794 pc.c[3] = format;
1795 pc.c[7] = (buflen >> 8);
1796 pc.c[8] = (buflen & 0xff);
1797 return cdrom_queue_packet_command(drive, &pc);
1800 /* ATAPI cdrom drives are free to select the speed you request or any slower
1801 rate :-( Requesting too fast a speed will _not_ produce an error. */
1802 static int cdrom_select_speed(ide_drive_t *drive, int speed,
1803 struct request_sense *sense)
1805 struct packet_command pc;
1806 memset(&pc, 0, sizeof(pc));
1807 pc.sense = sense;
1809 if (speed == 0)
1810 speed = 0xffff; /* set to max */
1811 else
1812 speed *= 177; /* Nx to kbytes/s */
1814 pc.c[0] = GPCMD_SET_SPEED;
1815 /* Read Drive speed in kbytes/second MSB */
1816 pc.c[2] = (speed >> 8) & 0xff;
1817 /* Read Drive speed in kbytes/second LSB */
1818 pc.c[3] = speed & 0xff;
1819 if ( CDROM_CONFIG_FLAGS(drive)->cd_r ||
1820 CDROM_CONFIG_FLAGS(drive)->cd_rw ) {
1821 /* Write Drive speed in kbytes/second MSB */
1822 pc.c[4] = (speed >> 8) & 0xff;
1823 /* Write Drive speed in kbytes/second LSB */
1824 pc.c[5] = speed & 0xff;
1827 return cdrom_queue_packet_command(drive, &pc);
1831 static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
1832 struct atapi_toc_entry **ent)
1834 struct cdrom_info *info = drive->driver_data;
1835 struct atapi_toc *toc = info->toc;
1836 int ntracks;
1838 /* Check validity of requested track number. */
1839 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1840 if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
1841 if (track == CDROM_LEADOUT)
1842 *ent = &toc->ent[ntracks];
1843 else if (track < toc->hdr.first_track ||
1844 track > toc->hdr.last_track)
1845 return -EINVAL;
1846 else
1847 *ent = &toc->ent[track - toc->hdr.first_track];
1849 return 0;
1856 /* the generic packet interface to cdrom.c */
1857 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
1858 struct cdrom_generic_command *cgc)
1860 struct packet_command pc;
1861 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
1863 /* here we queue the commands from the uniform CD-ROM
1864 layer. the packet must be complete, as we do not
1865 touch it at all. */
1866 memset(&pc, 0, sizeof(pc));
1867 memcpy(pc.c, cgc->cmd, CDROM_PACKET_SIZE);
1868 pc.buffer = cgc->buffer;
1869 pc.buflen = cgc->buflen;
1870 cgc->stat = cdrom_queue_packet_command(drive, &pc);
1873 * FIXME: copy sense, don't just assign pointer!!
1875 cgc->sense = pc.sense;
1877 return cgc->stat;
1880 static
1881 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
1882 unsigned int cmd, unsigned long arg)
1884 struct cdrom_generic_command cgc;
1885 char buffer[16];
1886 int stat;
1888 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1890 /* These will be moved into the Uniform layer shortly... */
1891 switch (cmd) {
1892 case CDROMSETSPINDOWN: {
1893 char spindown;
1895 if (copy_from_user(&spindown, (void *) arg, sizeof(char)))
1896 return -EFAULT;
1898 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
1899 return stat;
1901 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1903 return cdrom_mode_select(cdi, &cgc);
1906 case CDROMGETSPINDOWN: {
1907 char spindown;
1909 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
1910 return stat;
1912 spindown = buffer[11] & 0x0f;
1914 if (copy_to_user((void *) arg, &spindown, sizeof (char)))
1915 return -EFAULT;
1917 return 0;
1920 default:
1921 return -EINVAL;
1926 static
1927 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
1928 unsigned int cmd, void *arg)
1931 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
1932 struct cdrom_info *info = drive->driver_data;
1934 switch (cmd) {
1935 case CDROMREADTOCHDR: {
1936 int stat;
1937 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
1938 struct atapi_toc *toc;
1940 /* Make sure our saved TOC is valid. */
1941 stat = cdrom_read_toc(drive, NULL);
1942 if (stat) return stat;
1944 toc = info->toc;
1945 tochdr->cdth_trk0 = toc->hdr.first_track;
1946 tochdr->cdth_trk1 = toc->hdr.last_track;
1948 return 0;
1951 case CDROMREADTOCENTRY: {
1952 int stat;
1953 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
1954 struct atapi_toc_entry *toce;
1956 stat = cdrom_get_toc_entry (drive, tocentry->cdte_track, &toce);
1957 if (stat) return stat;
1959 tocentry->cdte_ctrl = toce->control;
1960 tocentry->cdte_adr = toce->adr;
1961 if (tocentry->cdte_format == CDROM_MSF) {
1962 lba_to_msf (toce->addr.lba,
1963 &tocentry->cdte_addr.msf.minute,
1964 &tocentry->cdte_addr.msf.second,
1965 &tocentry->cdte_addr.msf.frame);
1966 } else
1967 tocentry->cdte_addr.lba = toce->addr.lba;
1969 return 0;
1972 default:
1973 return -EINVAL;
1977 static
1978 int ide_cdrom_reset (struct cdrom_device_info *cdi)
1980 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
1981 struct request_sense sense;
1982 struct request req;
1983 int ret;
1985 ide_init_drive_cmd (&req);
1986 req.cmd = RESET_DRIVE_COMMAND;
1987 ret = ide_do_drive_cmd(drive, &req, ide_wait);
1990 * A reset will unlock the door. If it was previously locked,
1991 * lock it again.
1993 if (CDROM_STATE_FLAGS(drive)->door_locked)
1994 (void) cdrom_lockdoor(drive, 1, &sense);
1996 return ret;
2000 static
2001 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2003 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2004 struct request_sense sense;
2006 if (position) {
2007 int stat = cdrom_lockdoor(drive, 0, &sense);
2008 if (stat) return stat;
2011 return cdrom_eject(drive, !position, &sense);
2014 static
2015 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2017 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2018 return cdrom_lockdoor(drive, lock, NULL);
2021 static
2022 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2024 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2025 struct request_sense sense;
2026 int stat;
2028 if ((stat = cdrom_select_speed (drive, speed, &sense)) < 0)
2029 return stat;
2031 cdi->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2032 return 0;
2035 static
2036 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2038 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2040 if (slot_nr == CDSL_CURRENT) {
2041 struct request_sense sense;
2042 int stat = cdrom_check_status(drive, &sense);
2043 if (stat == 0 || sense.sense_key == UNIT_ATTENTION)
2044 return CDS_DISC_OK;
2046 if (sense.sense_key == NOT_READY && sense.asc == 0x04 &&
2047 sense.ascq == 0x04)
2048 return CDS_DISC_OK;
2050 if (sense.sense_key == NOT_READY) {
2051 /* ATAPI doesn't have anything that can help
2052 us decide whether the drive is really
2053 emtpy or the tray is just open. irk. */
2054 return CDS_TRAY_OPEN;
2057 return CDS_DRIVE_NOT_READY;
2059 return -EINVAL;
2062 static
2063 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2064 struct cdrom_multisession *ms_info)
2066 struct atapi_toc *toc;
2067 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2068 struct cdrom_info *info = drive->driver_data;
2069 struct request_sense sense;
2070 int ret;
2072 if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2073 if ((ret = cdrom_read_toc(drive, &sense)))
2074 return ret;
2076 toc = info->toc;
2077 ms_info->addr.lba = toc->last_session_lba;
2078 ms_info->xa_flag = toc->xa_flag;
2080 return 0;
2083 static
2084 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2085 struct cdrom_mcn *mcn_info)
2087 int stat;
2088 char mcnbuf[24];
2089 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2091 /* get MCN */
2092 if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2093 return stat;
2095 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2096 sizeof (mcn_info->medium_catalog_number)-1);
2097 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2098 = '\0';
2100 return 0;
2105 /****************************************************************************
2106 * Other driver requests (open, close, check media change).
2109 static
2110 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2111 int slot_nr)
2113 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2114 int retval;
2116 if (slot_nr == CDSL_CURRENT) {
2117 (void) cdrom_check_status(drive, NULL);
2118 retval = CDROM_STATE_FLAGS (drive)->media_changed;
2119 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2120 return retval;
2121 } else {
2122 return -EINVAL;
2127 static
2128 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2130 return 0;
2135 * Close down the device. Invalidate all cached blocks.
2138 static
2139 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2145 /****************************************************************************
2146 * Device initialization.
2149 static
2150 struct cdrom_device_ops ide_cdrom_dops = {
2151 ide_cdrom_open_real, /* open */
2152 ide_cdrom_release_real, /* release */
2153 ide_cdrom_drive_status, /* drive_status */
2154 ide_cdrom_check_media_change_real, /* media_changed */
2155 ide_cdrom_tray_move, /* tray_move */
2156 ide_cdrom_lock_door, /* lock_door */
2157 ide_cdrom_select_speed, /* select_speed */
2158 NULL, /* select_disc */
2159 ide_cdrom_get_last_session, /* get_last_session */
2160 ide_cdrom_get_mcn, /* get_mcn */
2161 ide_cdrom_reset, /* reset */
2162 ide_cdrom_audio_ioctl, /* audio_ioctl */
2163 ide_cdrom_dev_ioctl, /* dev_ioctl */
2164 CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED
2165 | CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN
2166 | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET | CDC_IOCTLS
2167 | CDC_DRIVE_STATUS | CDC_CD_R | CDC_CD_RW | CDC_DVD
2168 | CDC_DVD_R| CDC_DVD_RAM | CDC_GENERIC_PACKET, /* capability */
2169 0, /* n_minors */
2170 ide_cdrom_packet
2173 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2175 struct cdrom_info *info = drive->driver_data;
2176 struct cdrom_device_info *devinfo = &info->devinfo;
2177 int minor = (drive->select.b.unit) << PARTN_BITS;
2179 devinfo->dev = MKDEV (HWIF(drive)->major, minor);
2180 devinfo->ops = &ide_cdrom_dops;
2181 devinfo->mask = 0;
2182 *(int *)&devinfo->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2183 *(int *)&devinfo->capacity = nslots;
2184 devinfo->handle = (void *) drive;
2185 strcpy(devinfo->name, drive->name);
2187 /* set capability mask to match the probe. */
2188 if (!CDROM_CONFIG_FLAGS (drive)->cd_r)
2189 devinfo->mask |= CDC_CD_R;
2190 if (!CDROM_CONFIG_FLAGS (drive)->cd_rw)
2191 devinfo->mask |= CDC_CD_RW;
2192 if (!CDROM_CONFIG_FLAGS (drive)->dvd)
2193 devinfo->mask |= CDC_DVD;
2194 if (!CDROM_CONFIG_FLAGS (drive)->dvd_r)
2195 devinfo->mask |= CDC_DVD_R;
2196 if (!CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2197 devinfo->mask |= CDC_DVD_RAM;
2198 if (!CDROM_CONFIG_FLAGS (drive)->is_changer)
2199 devinfo->mask |= CDC_SELECT_DISC;
2200 if (!CDROM_CONFIG_FLAGS (drive)->audio_play)
2201 devinfo->mask |= CDC_PLAY_AUDIO;
2202 if (!CDROM_CONFIG_FLAGS (drive)->close_tray)
2203 devinfo->mask |= CDC_CLOSE_TRAY;
2205 devinfo->de = devfs_register(drive->de, "cd", DEVFS_FL_DEFAULT,
2206 HWIF(drive)->major, minor,
2207 S_IFBLK | S_IRUGO | S_IWUGO,
2208 ide_fops, NULL);
2210 return register_cdrom(devinfo);
2213 static
2214 int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2216 struct cdrom_info *info = drive->driver_data;
2217 struct cdrom_device_info *cdi = &info->devinfo;
2218 struct cdrom_generic_command cgc;
2219 int stat, attempts = 3, size = sizeof(*cap);
2222 * ACER50 (and others?) require the full spec length mode sense
2223 * page capabilities size, but older drives break.
2225 if (drive->id) {
2226 if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2227 !strcmp(drive->id->model, "WPI CDS-32X")))
2228 size -= sizeof(cap->pad);
2231 /* we have to cheat a little here. the packet will eventually
2232 * be queued with ide_cdrom_packet(), which extracts the
2233 * drive from cdi->handle. Since this device hasn't been
2234 * registered with the Uniform layer yet, it can't do this.
2235 * Same goes for cdi->ops.
2237 cdi->handle = (ide_drive_t *) drive;
2238 cdi->ops = &ide_cdrom_dops;
2239 init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2240 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2241 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2242 if (!stat)
2243 break;
2244 } while (--attempts);
2245 return stat;
2248 static
2249 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2251 struct cdrom_info *info = drive->driver_data;
2252 struct cdrom_device_info *cdi = &info->devinfo;
2253 struct atapi_capabilities_page cap;
2254 int nslots = 1;
2256 if (CDROM_CONFIG_FLAGS (drive)->nec260) {
2257 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2258 CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
2259 return nslots;
2262 if (ide_cdrom_get_capabilities(drive, &cap))
2263 return 0;
2265 if (cap.lock == 0)
2266 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2267 if (cap.eject)
2268 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2269 if (cap.cd_r_write)
2270 CDROM_CONFIG_FLAGS (drive)->cd_r = 1;
2271 if (cap.cd_rw_write)
2272 CDROM_CONFIG_FLAGS (drive)->cd_rw = 1;
2273 if (cap.test_write)
2274 CDROM_CONFIG_FLAGS (drive)->test_write = 1;
2275 if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2276 CDROM_CONFIG_FLAGS (drive)->dvd = 1;
2277 if (cap.dvd_ram_write)
2278 CDROM_CONFIG_FLAGS (drive)->dvd_r = 1;
2279 if (cap.dvd_r_write)
2280 CDROM_CONFIG_FLAGS (drive)->dvd_ram = 1;
2281 if (cap.audio_play)
2282 CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
2283 if (cap.mechtype == 0)
2284 CDROM_CONFIG_FLAGS (drive)->close_tray = 0;
2286 #if ! STANDARD_ATAPI
2287 if (cdi->sanyo_slot > 0) {
2288 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2289 nslots = 3;
2292 else
2293 #endif /* not STANDARD_ATAPI */
2294 if (cap.mechtype == mechtype_individual_changer ||
2295 cap.mechtype == mechtype_cartridge_changer) {
2296 if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2297 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2298 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 1;
2302 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2303 if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2304 CDROM_STATE_FLAGS (drive)->current_speed =
2305 (((unsigned int)cap.curspeed) + (176/2)) / 176;
2306 CDROM_CONFIG_FLAGS (drive)->max_speed =
2307 (((unsigned int)cap.maxspeed) + (176/2)) / 176;
2308 } else {
2309 CDROM_STATE_FLAGS (drive)->current_speed =
2310 (ntohs(cap.curspeed) + (176/2)) / 176;
2311 CDROM_CONFIG_FLAGS (drive)->max_speed =
2312 (ntohs(cap.maxspeed) + (176/2)) / 176;
2315 /* don't print speed if the drive reported 0.
2317 printk("%s: ATAPI", drive->name);
2318 if (CDROM_CONFIG_FLAGS(drive)->max_speed)
2319 printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
2320 printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
2322 if (CDROM_CONFIG_FLAGS (drive)->dvd_r|CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2323 printk (" DVD%s%s",
2324 (CDROM_CONFIG_FLAGS (drive)->dvd_r)? "-R" : "",
2325 (CDROM_CONFIG_FLAGS (drive)->dvd_ram)? "AM" : "");
2327 if (CDROM_CONFIG_FLAGS (drive)->cd_r|CDROM_CONFIG_FLAGS (drive)->cd_rw)
2328 printk (" CD%s%s",
2329 (CDROM_CONFIG_FLAGS (drive)->cd_r)? "-R" : "",
2330 (CDROM_CONFIG_FLAGS (drive)->cd_rw)? "/RW" : "");
2332 if (CDROM_CONFIG_FLAGS (drive)->is_changer)
2333 printk (" changer w/%d slots", nslots);
2334 else
2335 printk (" drive");
2337 printk (", %dkB Cache", be16_to_cpu(cap.buffer_size));
2339 #ifdef CONFIG_BLK_DEV_IDEDMA
2340 if (drive->using_dma)
2341 (void) HWIF(drive)->dmaproc(ide_dma_verbose, drive);
2342 #endif /* CONFIG_BLK_DEV_IDEDMA */
2343 printk("\n");
2345 return nslots;
2348 static void ide_cdrom_add_settings(ide_drive_t *drive)
2350 int major = HWIF(drive)->major;
2351 int minor = drive->select.b.unit << PARTN_BITS;
2353 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
2354 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
2355 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
2356 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2359 static
2360 int ide_cdrom_setup (ide_drive_t *drive)
2362 struct cdrom_info *info = drive->driver_data;
2363 struct cdrom_device_info *cdi = &info->devinfo;
2364 int minor = drive->select.b.unit << PARTN_BITS;
2365 int nslots;
2367 set_device_ro(MKDEV(HWIF(drive)->major, minor), 1);
2368 set_blocksize(MKDEV(HWIF(drive)->major, minor), CD_FRAMESIZE);
2370 drive->special.all = 0;
2371 drive->ready_stat = 0;
2373 CDROM_STATE_FLAGS (drive)->media_changed = 1;
2374 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
2375 CDROM_STATE_FLAGS (drive)->door_locked = 0;
2377 #if NO_DOOR_LOCKING
2378 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2379 #else
2380 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2381 #endif
2383 if (drive->id != NULL)
2384 CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2385 ((drive->id->config & 0x0060) == 0x20);
2386 else
2387 CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2389 CDROM_CONFIG_FLAGS (drive)->is_changer = 0;
2390 CDROM_CONFIG_FLAGS (drive)->cd_r = 0;
2391 CDROM_CONFIG_FLAGS (drive)->cd_rw = 0;
2392 CDROM_CONFIG_FLAGS (drive)->test_write = 0;
2393 CDROM_CONFIG_FLAGS (drive)->dvd = 0;
2394 CDROM_CONFIG_FLAGS (drive)->dvd_r = 0;
2395 CDROM_CONFIG_FLAGS (drive)->dvd_ram = 0;
2396 CDROM_CONFIG_FLAGS (drive)->no_eject = 1;
2397 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 0;
2398 CDROM_CONFIG_FLAGS (drive)->audio_play = 0;
2399 CDROM_CONFIG_FLAGS (drive)->close_tray = 1;
2401 /* limit transfer size per interrupt. */
2402 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 0;
2403 if (drive->id != NULL) {
2404 /* a testament to the nice quality of Samsung drives... */
2405 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
2406 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2407 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
2408 CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2409 /* the 3231 model does not support the SET_CD_SPEED command */
2410 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
2411 cdi->mask |= CDC_SELECT_SPEED;
2414 #if ! STANDARD_ATAPI
2415 /* by default Sanyo 3 CD changer support is turned off and
2416 ATAPI Rev 2.2+ standard support for CD changers is used */
2417 cdi->sanyo_slot = 0;
2419 CDROM_CONFIG_FLAGS (drive)->nec260 = 0;
2420 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2421 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2422 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2423 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2425 if (drive->id != NULL) {
2426 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2427 drive->id->fw_rev[4] == '1' &&
2428 drive->id->fw_rev[6] <= '2') {
2429 /* Vertos 300.
2430 Some versions of this drive like to talk BCD. */
2431 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2432 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2433 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2434 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2437 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2438 drive->id->fw_rev[4] == '1' &&
2439 drive->id->fw_rev[6] <= '2') {
2440 /* Vertos 600 ESD. */
2441 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2444 else if (strcmp (drive->id->model,
2445 "NEC CD-ROM DRIVE:260") == 0 &&
2446 strncmp (drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
2447 /* Old NEC260 (not R).
2448 This drive was released before the 1.2 version
2449 of the spec. */
2450 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2451 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2452 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2453 CDROM_CONFIG_FLAGS (drive)->nec260 = 1;
2456 else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
2457 strncmp (drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
2458 /* Wearnes */
2459 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2460 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2463 /* Sanyo 3 CD changer uses a non-standard command
2464 for CD changing */
2465 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
2466 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
2467 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
2468 /* uses CD in slot 0 when value is set to 3 */
2469 cdi->sanyo_slot = 3;
2474 #endif /* not STANDARD_ATAPI */
2476 info->toc = NULL;
2477 info->buffer = NULL;
2478 info->sector_buffered = 0;
2479 info->nsectors_buffered = 0;
2480 info->changer_info = NULL;
2481 info->last_block = 0;
2482 info->start_seek = 0;
2484 nslots = ide_cdrom_probe_capabilities (drive);
2486 if (ide_cdrom_register (drive, nslots)) {
2487 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2488 info->devinfo.handle = NULL;
2489 return 1;
2491 ide_cdrom_add_settings(drive);
2492 return 0;
2495 /* Forwarding functions to generic routines. */
2496 static
2497 int ide_cdrom_ioctl (ide_drive_t *drive,
2498 struct inode *inode, struct file *file,
2499 unsigned int cmd, unsigned long arg)
2501 return cdrom_fops.ioctl (inode, file, cmd, arg);
2504 static
2505 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2507 struct cdrom_info *info = drive->driver_data;
2508 int rc;
2510 MOD_INC_USE_COUNT;
2511 if (info->buffer == NULL)
2512 info->buffer = (char *) kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL);
2513 rc = cdrom_fops.open (ip, fp);
2514 if (rc) {
2515 drive->usage--;
2516 MOD_DEC_USE_COUNT;
2518 return rc;
2521 static
2522 void ide_cdrom_release (struct inode *inode, struct file *file,
2523 ide_drive_t *drive)
2525 cdrom_fops.release (inode, file);
2526 MOD_DEC_USE_COUNT;
2529 static
2530 int ide_cdrom_check_media_change (ide_drive_t *drive)
2532 return cdrom_fops.check_media_change(MKDEV (HWIF (drive)->major,
2533 (drive->select.b.unit) << PARTN_BITS));
2536 static
2537 void ide_cdrom_revalidate (ide_drive_t *drive)
2539 struct cdrom_info *info = drive->driver_data;
2540 struct atapi_toc *toc;
2541 int minor = drive->select.b.unit << PARTN_BITS;
2542 struct request_sense sense;
2544 cdrom_read_toc(drive, &sense);
2546 if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2547 return;
2549 toc = info->toc;
2551 /* for general /dev/cdrom like mounting, one big disc */
2552 drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
2553 HWIF(drive)->gd->sizes[minor] = toc->capacity * BLOCKS_PER_FRAME;
2555 blk_size[HWIF(drive)->major] = HWIF(drive)->gd->sizes;
2558 static
2559 unsigned long ide_cdrom_capacity (ide_drive_t *drive)
2561 unsigned capacity;
2563 return cdrom_read_capacity(drive, &capacity, NULL)
2564 ? 0 : capacity * SECTORS_PER_FRAME;
2567 static
2568 int ide_cdrom_cleanup(ide_drive_t *drive)
2570 struct cdrom_info *info = drive->driver_data;
2571 struct cdrom_device_info *devinfo = &info->devinfo;
2573 if (ide_unregister_subdriver (drive))
2574 return 1;
2575 if (info->buffer != NULL)
2576 kfree(info->buffer);
2577 if (info->toc != NULL)
2578 kfree(info->toc);
2579 if (info->changer_info != NULL)
2580 kfree(info->changer_info);
2581 if (devinfo->handle == drive && unregister_cdrom (devinfo))
2582 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
2583 kfree(info);
2584 drive->driver_data = NULL;
2585 return 0;
2588 static ide_driver_t ide_cdrom_driver = {
2589 "ide-cdrom", /* name */
2590 IDECD_VERSION, /* version */
2591 ide_cdrom, /* media */
2592 0, /* busy */
2593 1, /* supports_dma */
2594 1, /* supports_dsc_overlap */
2595 ide_cdrom_cleanup, /* cleanup */
2596 ide_do_rw_cdrom, /* do_request */
2597 NULL, /* ??? or perhaps cdrom_end_request? */
2598 ide_cdrom_ioctl, /* ioctl */
2599 ide_cdrom_open, /* open */
2600 ide_cdrom_release, /* release */
2601 ide_cdrom_check_media_change, /* media_change */
2602 ide_cdrom_revalidate, /* revalidate */
2603 NULL, /* pre_reset */
2604 ide_cdrom_capacity, /* capacity */
2605 NULL, /* special */
2606 NULL /* proc */
2609 int ide_cdrom_init(void);
2610 static ide_module_t ide_cdrom_module = {
2611 IDE_DRIVER_MODULE,
2612 ide_cdrom_init,
2613 &ide_cdrom_driver,
2614 NULL
2617 /* options */
2618 char *ignore = NULL;
2620 MODULE_PARM(ignore, "s");
2621 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2623 static void __exit ide_cdrom_exit(void)
2625 ide_drive_t *drive;
2626 int failed = 0;
2628 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, &ide_cdrom_driver, failed)) != NULL)
2629 if (ide_cdrom_cleanup (drive)) {
2630 printk ("%s: cleanup_module() called while still busy\n", drive->name);
2631 failed++;
2633 ide_unregister_module (&ide_cdrom_module);
2636 int ide_cdrom_init(void)
2638 ide_drive_t *drive;
2639 struct cdrom_info *info;
2640 int failed = 0;
2642 MOD_INC_USE_COUNT;
2643 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, NULL, failed++)) != NULL) {
2644 /* skip drives that we were told to ignore */
2645 if (ignore != NULL) {
2646 if (strstr(ignore, drive->name)) {
2647 printk("ide-cd: ignoring drive %s\n", drive->name);
2648 continue;
2651 if (drive->scsi) {
2652 printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
2653 continue;
2655 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
2656 if (info == NULL) {
2657 printk ("%s: Can't allocate a cdrom structure\n", drive->name);
2658 continue;
2660 if (ide_register_subdriver (drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
2661 printk ("%s: Failed to register the driver with ide.c\n", drive->name);
2662 kfree (info);
2663 continue;
2665 memset (info, 0, sizeof (struct cdrom_info));
2666 drive->driver_data = info;
2667 DRIVER(drive)->busy++;
2668 if (ide_cdrom_setup (drive)) {
2669 DRIVER(drive)->busy--;
2670 if (ide_cdrom_cleanup (drive))
2671 printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive->name);
2672 continue;
2674 DRIVER(drive)->busy--;
2675 failed--;
2677 ide_register_module(&ide_cdrom_module);
2678 MOD_DEC_USE_COUNT;
2679 return 0;
2682 module_init(ide_cdrom_init);
2683 module_exit(ide_cdrom_exit);