>> Btw, I've been looking at why Andrea thinks he's patches are needed,
[davej-history.git] / drivers / block / ide-cd.c
blob2231f56937b1f5ee00204b0696e2ccf209160ca1
1 #define VERBOSE_IDE_CD_ERRORS 1
2 /*
3 * linux/drivers/block/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 Jens Axboe and Chris Zwilling
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 ATAPI standard. This document can be
17 * obtained by anonymous ftp from:
18 * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
20 * Drives that deviate from the ATAPI standard will be accomodated as much
21 * as possable via compile time or command-line options. Since I only have
22 * a few drives, you generally need to send me patches...
24 * ----------------------------------
25 * TO DO LIST:
26 * -Implement Microsoft Media Status Notification per the spec at
27 * http://www.microsoft.com/hwdev/respec/storspec.htm
28 * This will allow us to get automagically notified when the media changes
29 * on ATAPI drives (something the stock ATAPI spec is lacking). Looks
30 * very cool. I discovered its existance the other day at work...
31 * -Fix ide_cdrom_reset so that it works (it does nothing right now)
32 * -Query the drive to find what features are available before trying to
33 * use them (like trying to close the tray in drives that can't).
34 * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
35 * boot
36 * -Handle older drives that can't report their speed. (i.e. check if they
37 * support a version of ATAPI where they can report their speed before
38 * checking their speed and believing what they return).
39 * -It seems we do not always honor it when Uniform gets a request to change
40 * the cdi->options. We should _always_ check the options before doing stuff.
41 * This must be fixed.
44 * ----------------------------------
45 * 1.00 Oct 31, 1994 -- Initial version.
46 * 1.01 Nov 2, 1994 -- Fixed problem with starting request in
47 * cdrom_check_status.
48 * 1.03 Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
49 * (from mlord) -- minor changes to cdrom_setup()
50 * -- renamed ide_dev_s to ide_drive_t, enable irq on command
51 * 2.00 Nov 27, 1994 -- Generalize packet command interface;
52 * add audio ioctls.
53 * 2.01 Dec 3, 1994 -- Rework packet command interface to handle devices
54 * which send an interrupt when ready for a command.
55 * 2.02 Dec 11, 1994 -- Cache the TOC in the driver.
56 * Don't use SCMD_PLAYAUDIO_TI; it's not included
57 * in the current version of ATAPI.
58 * Try to use LBA instead of track or MSF addressing
59 * when possible.
60 * Don't wait for READY_STAT.
61 * 2.03 Jan 10, 1995 -- Rewrite block read routines to handle block sizes
62 * other than 2k and to move multiple sectors in a
63 * single transaction.
64 * 2.04 Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
65 * Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
66 * help in figuring this out. Ditto for Acer and
67 * Aztech drives, which seem to have the same problem.
68 * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
69 * 2.05 Jun 8, 1995 -- Don't attempt to retry after an illegal request
70 * or data protect error.
71 * Use HWIF and DEV_HWIF macros as in ide.c.
72 * Always try to do a request_sense after
73 * a failed command.
74 * Include an option to give textual descriptions
75 * of ATAPI errors.
76 * Fix a bug in handling the sector cache which
77 * showed up if the drive returned data in 512 byte
78 * blocks (like Pioneer drives). Thanks to
79 * Richard Hirst <srh@gpt.co.uk> for diagnosing this.
80 * Properly supply the page number field in the
81 * MODE_SELECT command.
82 * PLAYAUDIO12 is broken on the Aztech; work around it.
83 * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
84 * (my apologies to Scott, but now ide-cd.c is independent)
85 * 3.00 Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
86 * Implement CDROMREADAUDIO ioctl (UNTESTED).
87 * Use input_ide_data() and output_ide_data().
88 * Add door locking.
89 * Fix usage count leak in cdrom_open, which happened
90 * when a read-write mount was attempted.
91 * Try to load the disk on open.
92 * Implement CDROMEJECT_SW ioctl (off by default).
93 * Read total cdrom capacity during open.
94 * Rearrange logic in cdrom_decode_status. Issue
95 * request sense commands for failed packet commands
96 * from here instead of from cdrom_queue_packet_command.
97 * Fix a race condition in retrieving error information.
98 * Suppress printing normal unit attention errors and
99 * some drive not ready errors.
100 * Implement CDROMVOLREAD ioctl.
101 * Implement CDROMREADMODE1/2 ioctls.
102 * Fix race condition in setting up interrupt handlers
103 * when the `serialize' option is used.
104 * 3.01 Sep 2, 1995 -- Fix ordering of reenabling interrupts in
105 * cdrom_queue_request.
106 * Another try at using ide_[input,output]_data.
107 * 3.02 Sep 16, 1995 -- Stick total disk capacity in partition table as well.
108 * Make VERBOSE_IDE_CD_ERRORS dump failed command again.
109 * Dump out more information for ILLEGAL REQUEST errs.
110 * Fix handling of errors occurring before the
111 * packet command is transferred.
112 * Fix transfers with odd bytelengths.
113 * 3.03 Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
114 * `DCI-2S10' drives are broken too.
115 * 3.04 Nov 20, 1995 -- So are Vertos drives.
116 * 3.05 Dec 1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
117 * 3.06 Dec 16, 1995 -- Add support needed for partitions.
118 * More workarounds for Vertos bugs (based on patches
119 * from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
120 * Try to eliminate byteorder assumptions.
121 * Use atapi_cdrom_subchnl struct definition.
122 * Add STANDARD_ATAPI compilation option.
123 * 3.07 Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
124 * Vertos 300.
125 * Add NO_DOOR_LOCKING configuration option.
126 * Handle drive_cmd requests w/NULL args (for hdparm -t).
127 * Work around sporadic Sony55e audio play problem.
128 * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
129 * problem with "hde=cdrom" with no drive present. -ml
130 * 3.08 Mar 6, 1996 -- More Vertos workarounds.
131 * 3.09 Apr 5, 1996 -- Add CDROMCLOSETRAY ioctl.
132 * Switch to using MSF addressing for audio commands.
133 * Reformat to match kernel tabbing style.
134 * Add CDROM_GET_UPC ioctl.
135 * 3.10 Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
136 * 3.11 Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
137 * to remove redundant verify_area calls.
138 * 3.12 May 7, 1996 -- Rudimentary changer support. Based on patches
139 * from Gerhard Zuber <zuber@berlin.snafu.de>.
140 * Let open succeed even if there's no loaded disc.
141 * 3.13 May 19, 1996 -- Fixes for changer code.
142 * 3.14 May 29, 1996 -- Add work-around for Vertos 600.
143 * (From Hennus Bergman <hennus@sky.ow.nl>.)
144 * 3.15 July 2, 1996 -- Added support for Sanyo 3 CD changers
145 * from Ben Galliart <bgallia@luc.edu> with
146 * special help from Jeff Lightfoot
147 * <jeffml@netcom.com>
148 * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
149 * 3.16 Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
150 * 3.17 Sep 17, 1996 -- Tweak audio reads for some drives.
151 * Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
152 * 3.18 Oct 31, 1996 -- Added module and DMA support.
155 * 4.00 Nov 5, 1996 -- New ide-cd maintainer,
156 * Erik B. Andersen <andersee@debian.org>
157 * -- Newer Creative drives don't always set the error
158 * register correctly. Make sure we see media changes
159 * regardless.
160 * -- Integrate with generic cdrom driver.
161 * -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
162 * a patch from Ciro Cattuto <>.
163 * -- Call set_device_ro.
164 * -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
165 * ioctls, based on patch by Erik Andersen
166 * -- Add some probes of drive capability during setup.
168 * 4.01 Nov 11, 1996 -- Split into ide-cd.c and ide-cd.h
169 * -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE
170 * ioctls in favor of a generalized approach
171 * using the generic cdrom driver.
172 * -- Fully integrated with the 2.1.X kernel.
173 * -- Other stuff that I forgot (lots of changes)
175 * 4.02 Dec 01, 1996 -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
176 * to fix the drive door locking problems.
178 * 4.03 Dec 04, 1996 -- Added DSC overlap support.
179 * 4.04 Dec 29, 1996 -- Added CDROMREADRAW ioclt based on patch
180 * by Ales Makarov (xmakarov@sun.felk.cvut.cz)
182 * 4.05 Nov 20, 1997 -- Modified to print more drive info on init
183 * Minor other changes
184 * Fix errors on CDROMSTOP (If you have a "Dolphin",
185 * you must define IHAVEADOLPHIN)
186 * Added identifier so new Sanyo CD-changer works
187 * Better detection if door locking isn't supported
189 * 4.06 Dec 17, 1997 -- fixed endless "tray open" messages -ml
190 * 4.07 Dec 17, 1997 -- fallback to set pc->stat on "tray open"
191 * 4.08 Dec 18, 1997 -- spew less noise when tray is empty
192 * -- fix speed display for ACER 24X, 18X
193 * 4.09 Jan 04, 1998 -- fix handling of the last block so we return
194 * an end of file instead of an I/O error (Gadi)
195 * 4.10 Jan 24, 1998 -- fixed a bug so now changers can change to a new
196 * slot when there is no disc in the current slot.
197 * -- Fixed a memory leak where info->changer_info was
198 * malloc'ed but never free'd when closing the device.
199 * -- Cleaned up the global namespace a bit by making more
200 * functions static that should already have been.
201 * 4.11 Mar 12, 1998 -- Added support for the CDROM_SELECT_SPEED ioctl
202 * based on a patch for 2.0.33 by Jelle Foks
203 * <jelle@scintilla.utwente.nl>, a patch for 2.0.33
204 * by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
205 * version, and my own efforts. -erik
206 * -- Fixed a stupid bug which egcs was kind enough to
207 * inform me of where "Illegal mode for this track"
208 * was never returned due to a comparison on data
209 * types of limited range.
210 * 4.12 Mar 29, 1998 -- Fixed bug in CDROM_SELECT_SPEED so write speed is
211 * now set ionly for CD-R and CD-RW drives. I had
212 * removed this support because it produced errors.
213 * It produced errors _only_ for non-writers. duh.
214 * 4.13 May 05, 1998 -- Suppress useless "in progress of becoming ready"
215 * messages, since this is not an error.
216 * -- Change error messages to be const
217 * -- Remove a "\t" which looks ugly in the syslogs
218 * 4.14 July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
219 * since the .pdf version doesn't seem to work...
220 * -- Updated the TODO list to something more current.
222 * 4.15 Aug 25, 1998 -- Updated ide-cd.h to respect mechine endianess,
223 * patch thanks to "Eddie C. Dost" <ecd@skynet.be>
225 * 4.50 Oct 19, 1998 -- New maintainers!
226 * Jens Axboe <axboe@image.dk>
227 * Chris Zwilling <chris@cloudnet.com>
229 *************************************************************************/
231 #define IDECD_VERSION "4.50"
233 #include <linux/module.h>
234 #include <linux/types.h>
235 #include <linux/kernel.h>
236 #include <linux/delay.h>
237 #include <linux/timer.h>
238 #include <linux/malloc.h>
239 #include <linux/interrupt.h>
240 #include <linux/errno.h>
241 #include <linux/cdrom.h>
242 #include <asm/irq.h>
243 #include <asm/io.h>
244 #include <asm/byteorder.h>
245 #include <asm/uaccess.h>
246 #include <asm/unaligned.h>
248 #include "ide.h"
249 #include "ide-cd.h"
252 /****************************************************************************
253 * Generic packet command support and error handling routines.
257 /* Mark that we've seen a media change, and invalidate our internal
258 buffers. */
259 static void cdrom_saw_media_change (ide_drive_t *drive)
261 struct cdrom_info *info = drive->driver_data;
263 CDROM_STATE_FLAGS (drive)->media_changed = 1;
264 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
265 info->nsectors_buffered = 0;
269 static
270 void cdrom_analyze_sense_data (ide_drive_t *drive,
271 struct atapi_request_sense *reqbuf,
272 struct packet_command *failed_command)
274 if (reqbuf->sense_key == NOT_READY ||
275 reqbuf->sense_key == UNIT_ATTENTION) {
276 /* Make good and sure we've seen this potential media change.
277 Some drives (i.e. Creative) fail to present the correct
278 sense key in the error register. */
279 cdrom_saw_media_change (drive);
282 /* Don't print not ready or unit attention errors for
283 READ_SUBCHANNEL. Workman (and probably other programs)
284 uses this command to poll the drive, and we don't want
285 to fill the syslog with useless errors. */
286 if (failed_command &&
287 failed_command->c[0] == SCMD_READ_SUBCHANNEL)
288 return;
290 if (reqbuf->error_code == 0x70 && reqbuf->sense_key == 0x02
291 && ((reqbuf->asc == 0x3a && reqbuf->ascq == 0x00) ||
292 (reqbuf->asc == 0x04 && reqbuf->ascq == 0x01)))
295 * Suppress the following errors:
296 * "Medium not present", and "in progress of becoming ready",
297 * to keep the noise level down to a dull roar.
299 return;
302 #if VERBOSE_IDE_CD_ERRORS
304 int i;
305 const char *s;
306 char buf[80];
308 printk ("ATAPI device %s:\n", drive->name);
309 if (reqbuf->error_code==0x70)
310 printk(" Error: ");
311 else if (reqbuf->error_code==0x71)
312 printk(" Deferred Error: ");
313 else
314 printk(" Unknown Error Type: ");
316 if ( reqbuf->sense_key < ARY_LEN (sense_key_texts))
317 s = sense_key_texts[reqbuf->sense_key];
318 else
319 s = "bad sense key!";
321 printk ("%s -- (Sense key=0x%02x)\n", s, reqbuf->sense_key);
323 if (reqbuf->asc == 0x40) {
324 sprintf (buf, "Diagnostic failure on component 0x%02x",
325 reqbuf->ascq);
326 s = buf;
327 } else {
328 int lo=0, mid, hi=ARY_LEN (sense_data_texts);
329 unsigned short key = (reqbuf->asc << 8);
330 if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
331 key |= reqbuf->ascq;
333 s = NULL;
335 while (hi > lo) {
336 mid = (lo + hi) / 2;
337 if (sense_data_texts[mid].asc_ascq == key) {
338 s = sense_data_texts[mid].text;
339 break;
341 else if (sense_data_texts[mid].asc_ascq > key)
342 hi = mid;
343 else
344 lo = mid+1;
348 if (s == NULL) {
349 if (reqbuf->asc > 0x80)
350 s = "(vendor-specific error)";
351 else
352 s = "(reserved error code)";
355 printk (" %s -- (asc=0x%02x, ascq=0x%02x)\n",
356 s, reqbuf->asc, reqbuf->ascq);
358 if (failed_command != NULL) {
360 int lo=0, mid, hi= ARY_LEN (packet_command_texts);
361 s = NULL;
363 while (hi > lo) {
364 mid = (lo + hi) / 2;
365 if (packet_command_texts[mid].packet_command == failed_command->c[0]) {
366 s = packet_command_texts[mid].text;
367 break;
369 else if (packet_command_texts[mid].packet_command > failed_command->c[0])
370 hi = mid;
371 else
372 lo = mid+1;
375 printk (" The failed \"%s\" packet command was: \n \"", s);
376 for (i=0; i<sizeof (failed_command->c); i++)
377 printk ("%02x ", failed_command->c[i]);
378 printk ("\"\n");
381 if (reqbuf->sense_key == ILLEGAL_REQUEST &&
382 (reqbuf->sense_key_specific[0] & 0x80) != 0) {
383 printk (" Error in %s byte %d",
384 (reqbuf->sense_key_specific[0] & 0x40) != 0
385 ? "command packet"
386 : "command data",
387 (reqbuf->sense_key_specific[1] << 8) +
388 reqbuf->sense_key_specific[2]);
390 if ((reqbuf->sense_key_specific[0] & 0x40) != 0) {
391 printk (" bit %d",
392 reqbuf->sense_key_specific[0] & 0x07);
395 printk ("\n");
399 #else /* not VERBOSE_IDE_CD_ERRORS */
401 /* Suppress printing unit attention and `in progress of becoming ready'
402 errors when we're not being verbose. */
404 if (reqbuf->sense_key == UNIT_ATTENTION ||
405 (reqbuf->sense_key == NOT_READY && (reqbuf->asc == 4 ||
406 reqbuf->asc == 0x3a)))
407 return;
409 printk ("%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
410 drive->name,
411 reqbuf->error_code, reqbuf->sense_key,
412 reqbuf->asc, reqbuf->ascq);
413 #endif /* not VERBOSE_IDE_CD_ERRORS */
417 /* Fix up a possibly partially-processed request so that we can
418 start it over entirely, or even put it back on the request queue. */
419 static void restore_request (struct request *rq)
421 if (rq->buffer != rq->bh->b_data) {
422 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
423 rq->buffer = rq->bh->b_data;
424 rq->nr_sectors += n;
425 rq->sector -= n;
427 rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
431 static void cdrom_queue_request_sense (ide_drive_t *drive,
432 struct semaphore *sem,
433 struct atapi_request_sense *reqbuf,
434 struct packet_command *failed_command)
436 struct cdrom_info *info = drive->driver_data;
437 struct request *rq;
438 struct packet_command *pc;
439 int len;
441 /* If the request didn't explicitly specify where
442 to put the sense data, use the statically allocated structure. */
443 if (reqbuf == NULL)
444 reqbuf = &info->sense_data;
446 /* Make up a new request to retrieve sense information. */
448 pc = &info->request_sense_pc;
449 memset (pc, 0, sizeof (*pc));
451 /* The request_sense structure has an odd number of (16-bit) words,
452 which won't work well with 32-bit transfers. However, we don't care
453 about the last two bytes, so just truncate the structure down
454 to an even length. */
455 len = sizeof (*reqbuf) / 4;
456 len *= 4;
458 pc->c[0] = REQUEST_SENSE;
459 pc->c[4] = len;
460 pc->buffer = (char *)reqbuf;
461 pc->buflen = len;
462 pc->sense_data = (struct atapi_request_sense *)failed_command;
464 /* stuff the sense request in front of our current request */
466 rq = &info->request_sense_request;
467 ide_init_drive_cmd (rq);
468 rq->cmd = REQUEST_SENSE_COMMAND;
469 rq->buffer = (char *)pc;
470 rq->sem = sem;
471 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
475 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
477 struct request *rq = HWGROUP(drive)->rq;
479 if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
480 struct packet_command *pc = (struct packet_command *)
481 rq->buffer;
482 cdrom_analyze_sense_data (drive,
483 (struct atapi_request_sense *)
484 (pc->buffer - pc->c[4]),
485 (struct packet_command *)
486 pc->sense_data);
488 if (rq->cmd == READ && !rq->current_nr_sectors)
489 uptodate = 1;
490 ide_end_request (uptodate, HWGROUP(drive));
494 /* Returns 0 if the request should be continued.
495 Returns 1 if the request was ended. */
496 static int cdrom_decode_status (ide_drive_t *drive, int good_stat,
497 int *stat_ret)
499 struct request *rq = HWGROUP(drive)->rq;
500 int stat, err, sense_key, cmd;
502 /* Check for errors. */
503 stat = GET_STAT();
504 *stat_ret = stat;
506 if (OK_STAT (stat, good_stat, BAD_R_STAT))
507 return 0;
509 /* Got an error. */
510 err = IN_BYTE (IDE_ERROR_REG);
511 sense_key = err >> 4;
513 if (rq == NULL)
514 printk ("%s: missing request in cdrom_decode_status\n",
515 drive->name);
516 else {
517 cmd = rq->cmd;
519 if (cmd == REQUEST_SENSE_COMMAND) {
520 /* We got an error trying to get sense info
521 from the drive (probably while trying
522 to recover from a former error). Just give up. */
524 struct packet_command *pc = (struct packet_command *)
525 rq->buffer;
526 pc->stat = 1;
527 cdrom_end_request (1, drive);
528 ide_error (drive, "request sense failure", stat);
529 return 1;
531 } else if (cmd == PACKET_COMMAND) {
532 /* All other functions, except for READ. */
534 struct packet_command *pc = (struct packet_command *)
535 rq->buffer;
536 struct semaphore *sem = NULL;
538 /* Check for tray open. */
539 if (sense_key == NOT_READY) {
540 cdrom_saw_media_change (drive);
541 #if 0 /* let the upper layers do the complaining */
542 /* Print an error message to the syslog.
543 Exception: don't print anything if this
544 is a read subchannel command. This is
545 because workman constantly polls the drive
546 with this command, and we don't want
547 to uselessly fill up the syslog. */
548 if (pc->c[0] != SCMD_READ_SUBCHANNEL)
549 printk ("%s: tray open or drive not ready\n", drive->name);
550 #endif
551 } else if (sense_key == UNIT_ATTENTION) {
552 /* Check for media change. */
553 cdrom_saw_media_change (drive);
554 /*printk("%s: media changed\n",drive->name);*/
555 return 0;
556 } else {
557 /* Otherwise, print an error. */
558 ide_dump_status (drive, "packet command error",
559 stat);
562 /* Set the error flag and complete the request.
563 Then, if we have a CHECK CONDITION status,
564 queue a request sense command. We must be careful,
565 though: we don't want the thread in
566 cdrom_queue_packet_command to wake up until
567 the request sense has completed. We do this
568 by transferring the semaphore from the packet
569 command request to the request sense request. */
571 if ((stat & ERR_STAT) != 0) {
572 sem = rq->sem;
573 rq->sem = NULL;
576 pc->stat = 1;
577 cdrom_end_request (1, drive);
579 if ((stat & ERR_STAT) != 0)
580 cdrom_queue_request_sense (drive, sem,
581 pc->sense_data, pc);
582 } else {
583 /* Handle errors from READ requests. */
585 if (sense_key == NOT_READY) {
586 /* Tray open. */
587 cdrom_saw_media_change (drive);
589 /* Fail the request. */
590 printk ("%s: tray open\n", drive->name);
591 cdrom_end_request (0, drive);
592 } else if (sense_key == UNIT_ATTENTION) {
593 /* Media change. */
594 cdrom_saw_media_change (drive);
596 /* Arrange to retry the request.
597 But be sure to give up if we've retried
598 too many times. */
599 if (++rq->errors > ERROR_MAX)
600 cdrom_end_request (0, drive);
601 } else if (sense_key == ILLEGAL_REQUEST ||
602 sense_key == DATA_PROTECT) {
603 /* No point in retrying after an illegal
604 request or data protect error.*/
605 ide_dump_status (drive, "command error", stat);
606 cdrom_end_request (0, drive);
607 } else if ((err & ~ABRT_ERR) != 0) {
608 /* Go to the default handler
609 for other errors. */
610 ide_error (drive, "cdrom_decode_status", stat);
611 return 1;
612 } else if ((++rq->errors > ERROR_MAX)) {
613 /* We've racked up too many retries. Abort. */
614 cdrom_end_request (0, drive);
617 /* If we got a CHECK_CONDITION status,
618 queue a request sense command. */
619 if ((stat & ERR_STAT) != 0)
620 cdrom_queue_request_sense (drive,
621 NULL, NULL, NULL);
625 /* Retry, or handle the next request. */
626 return 1;
630 /* Set up the device registers for transferring a packet command on DEV,
631 expecting to later transfer XFERLEN bytes. HANDLER is the routine
632 which actually transfers the command to the drive. If this is a
633 drq_interrupt device, this routine will arrange for HANDLER to be
634 called when the interrupt from the drive arrives. Otherwise, HANDLER
635 will be called immediately after the drive is prepared for the transfer. */
637 static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
638 ide_handler_t *handler)
640 struct cdrom_info *info = drive->driver_data;
642 /* Wait for the controller to be idle. */
643 if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
645 if (info->dma)
646 info->dma = !HWIF(drive)->dmaproc(ide_dma_read, drive);
648 /* Set up the controller registers. */
649 OUT_BYTE (info->dma, IDE_FEATURE_REG);
650 OUT_BYTE (0, IDE_NSECTOR_REG);
651 OUT_BYTE (0, IDE_SECTOR_REG);
653 OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
654 OUT_BYTE (xferlen >> 8 , IDE_HCYL_REG);
655 OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
657 if (info->dma)
658 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
660 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
661 ide_set_handler (drive, handler, WAIT_CMD);
662 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
663 } else {
664 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
665 (*handler) (drive);
668 return 0;
672 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
673 The device registers must have already been prepared
674 by cdrom_start_packet_command.
675 HANDLER is the interrupt handler to call when the command completes
676 or there's data ready. */
677 static int cdrom_transfer_packet_command (ide_drive_t *drive,
678 char *cmd_buf, int cmd_len,
679 ide_handler_t *handler)
681 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
682 /* Here we should have been called after receiving an interrupt
683 from the device. DRQ should how be set. */
684 int stat_dum;
686 /* Check for errors. */
687 if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum))
688 return 1;
689 } else {
690 /* Otherwise, we must wait for DRQ to get set. */
691 if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
692 return 1;
695 /* Arm the interrupt handler. */
696 ide_set_handler (drive, handler, WAIT_CMD);
698 /* Send the command to the device. */
699 atapi_output_bytes (drive, cmd_buf, cmd_len);
701 return 0;
706 /****************************************************************************
707 * Block read functions.
711 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
712 * buffer. Once the first sector is added, any subsequent sectors are
713 * assumed to be continuous (until the buffer is cleared). For the first
714 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
715 * the buffer is cleared.)
717 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
718 int sectors_to_transfer)
720 struct cdrom_info *info = drive->driver_data;
722 /* Number of sectors to read into the buffer. */
723 int sectors_to_buffer = MIN (sectors_to_transfer,
724 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
725 info->nsectors_buffered);
727 char *dest;
729 /* If we don't yet have a sector buffer, try to allocate one.
730 If we can't get one atomically, it's not fatal -- we'll just throw
731 the data away rather than caching it. */
732 if (info->sector_buffer == NULL) {
733 info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE,
734 GFP_ATOMIC);
736 /* If we couldn't get a buffer,
737 don't try to buffer anything... */
738 if (info->sector_buffer == NULL)
739 sectors_to_buffer = 0;
742 /* If this is the first sector in the buffer, remember its number. */
743 if (info->nsectors_buffered == 0)
744 info->sector_buffered = sector;
746 /* Read the data into the buffer. */
747 dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
748 while (sectors_to_buffer > 0) {
749 atapi_input_bytes (drive, dest, SECTOR_SIZE);
750 --sectors_to_buffer;
751 --sectors_to_transfer;
752 ++info->nsectors_buffered;
753 dest += SECTOR_SIZE;
756 /* Throw away any remaining data. */
757 while (sectors_to_transfer > 0) {
758 char dum[SECTOR_SIZE];
759 atapi_input_bytes (drive, dum, sizeof (dum));
760 --sectors_to_transfer;
766 * Check the contents of the interrupt reason register from the cdrom
767 * and attempt to recover if there are problems. Returns 0 if everything's
768 * ok; nonzero if the request has been terminated.
770 static inline
771 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
773 ireason &= 3;
774 if (ireason == 2) return 0;
776 if (ireason == 0) {
777 /* Whoops... The drive is expecting to receive data from us! */
778 printk ("%s: cdrom_read_intr: "
779 "Drive wants to transfer data the wrong way!\n",
780 drive->name);
782 /* Throw some data at the drive so it doesn't hang
783 and quit this request. */
784 while (len > 0) {
785 int dum = 0;
786 atapi_output_bytes (drive, &dum, sizeof (dum));
787 len -= sizeof (dum);
789 } else {
790 /* Drive wants a command packet, or invalid ireason... */
791 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
792 drive->name, ireason);
795 cdrom_end_request (0, drive);
796 return -1;
801 * Interrupt routine. Called when a read request has completed.
803 static void cdrom_read_intr (ide_drive_t *drive)
805 int stat;
806 int ireason, len, sectors_to_transfer, nskip;
807 struct cdrom_info *info = drive->driver_data;
808 int i, dma = info->dma, dma_error = 0;
810 struct request *rq = HWGROUP(drive)->rq;
812 /* Check for errors. */
813 if (dma) {
814 info->dma = 0;
815 if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))
816 HWIF(drive)->dmaproc(ide_dma_off, drive);
819 if (cdrom_decode_status (drive, 0, &stat))
820 return;
822 if (dma) {
823 if (!dma_error) {
824 for (i = rq->nr_sectors; i > 0;) {
825 i -= rq->current_nr_sectors;
826 ide_end_request(1, HWGROUP(drive));
828 } else
829 ide_error (drive, "dma error", stat);
830 return;
833 /* Read the interrupt reason and the transfer length. */
834 ireason = IN_BYTE (IDE_NSECTOR_REG);
835 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
837 /* If DRQ is clear, the command has completed. */
838 if ((stat & DRQ_STAT) == 0) {
839 /* If we're not done filling the current buffer, complain.
840 Otherwise, complete the command normally. */
841 if (rq->current_nr_sectors > 0) {
842 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
843 drive->name, rq->current_nr_sectors);
844 cdrom_end_request (0, drive);
845 } else
846 cdrom_end_request (1, drive);
847 return;
850 /* Check that the drive is expecting to do the same thing we are. */
851 if (cdrom_read_check_ireason (drive, len, ireason)) return;
853 /* Assume that the drive will always provide data in multiples
854 of at least SECTOR_SIZE, as it gets hairy to keep track
855 of the transfers otherwise. */
856 if ((len % SECTOR_SIZE) != 0) {
857 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
858 drive->name, len);
859 printk (" This drive is not supported by this version of the driver\n");
860 cdrom_end_request (0, drive);
861 return;
864 /* The number of sectors we need to read from the drive. */
865 sectors_to_transfer = len / SECTOR_SIZE;
867 /* First, figure out if we need to bit-bucket
868 any of the leading sectors. */
869 nskip = MIN ((int)(rq->current_nr_sectors -
870 (rq->bh->b_size >> SECTOR_BITS)),
871 sectors_to_transfer);
873 while (nskip > 0) {
874 /* We need to throw away a sector. */
875 char dum[SECTOR_SIZE];
876 atapi_input_bytes (drive, dum, sizeof (dum));
878 --rq->current_nr_sectors;
879 --nskip;
880 --sectors_to_transfer;
883 /* Now loop while we still have data to read from the drive. */
884 while (sectors_to_transfer > 0) {
885 int this_transfer;
887 /* If we've filled the present buffer but there's another
888 chained buffer after it, move on. */
889 if (rq->current_nr_sectors == 0 &&
890 rq->nr_sectors > 0)
891 cdrom_end_request (1, drive);
893 /* If the buffers are full, cache the rest of the data in our
894 internal buffer. */
895 if (rq->current_nr_sectors == 0) {
896 cdrom_buffer_sectors (drive,
897 rq->sector, sectors_to_transfer);
898 sectors_to_transfer = 0;
899 } else {
900 /* Transfer data to the buffers.
901 Figure out how many sectors we can transfer
902 to the current buffer. */
903 this_transfer = MIN (sectors_to_transfer,
904 rq->current_nr_sectors);
906 /* Read this_transfer sectors
907 into the current buffer. */
908 while (this_transfer > 0) {
909 atapi_input_bytes (drive,
910 rq->buffer, SECTOR_SIZE);
911 rq->buffer += SECTOR_SIZE;
912 --rq->nr_sectors;
913 --rq->current_nr_sectors;
914 ++rq->sector;
915 --this_transfer;
916 --sectors_to_transfer;
921 /* Done moving data!
922 Wait for another interrupt. */
923 ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);
928 * Try to satisfy some of the current read request from our cached data.
929 * Returns nonzero if the request has been completed, zero otherwise.
931 static int cdrom_read_from_buffer (ide_drive_t *drive)
933 struct cdrom_info *info = drive->driver_data;
934 struct request *rq = HWGROUP(drive)->rq;
936 /* Can't do anything if there's no buffer. */
937 if (info->sector_buffer == NULL) return 0;
939 /* Loop while this request needs data and the next block is present
940 in our cache. */
941 while (rq->nr_sectors > 0 &&
942 rq->sector >= info->sector_buffered &&
943 rq->sector < info->sector_buffered + info->nsectors_buffered) {
944 if (rq->current_nr_sectors == 0)
945 cdrom_end_request (1, drive);
947 memcpy (rq->buffer,
948 info->sector_buffer +
949 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
950 SECTOR_SIZE);
951 rq->buffer += SECTOR_SIZE;
952 --rq->current_nr_sectors;
953 --rq->nr_sectors;
954 ++rq->sector;
957 /* If we've satisfied the current request,
958 terminate it successfully. */
959 if (rq->nr_sectors == 0) {
960 cdrom_end_request (1, drive);
961 return -1;
964 /* Move on to the next buffer if needed. */
965 if (rq->current_nr_sectors == 0)
966 cdrom_end_request (1, drive);
968 /* If this condition does not hold, then the kluge i use to
969 represent the number of sectors to skip at the start of a transfer
970 will fail. I think that this will never happen, but let's be
971 paranoid and check. */
972 if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
973 (rq->sector % SECTORS_PER_FRAME) != 0) {
974 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
975 drive->name, rq->sector);
976 cdrom_end_request (0, drive);
977 return -1;
980 return 0;
986 * Routine to send a read packet command to the drive.
987 * This is usually called directly from cdrom_start_read.
988 * However, for drq_interrupt devices, it is called from an interrupt
989 * when the drive is ready to accept the command.
991 static void cdrom_start_read_continuation (ide_drive_t *drive)
993 struct packet_command pc;
994 struct request *rq = HWGROUP(drive)->rq;
996 int nsect, sector, nframes, frame, nskip;
998 /* Number of sectors to transfer. */
999 nsect = rq->nr_sectors;
1001 /* Starting sector. */
1002 sector = rq->sector;
1004 /* If the requested sector doesn't start on a cdrom block boundary,
1005 we must adjust the start of the transfer so that it does,
1006 and remember to skip the first few sectors.
1007 If the CURRENT_NR_SECTORS field is larger than the size
1008 of the buffer, it will mean that we're to skip a number
1009 of sectors equal to the amount by which CURRENT_NR_SECTORS
1010 is larger than the buffer size. */
1011 nskip = (sector % SECTORS_PER_FRAME);
1012 if (nskip > 0) {
1013 /* Sanity check... */
1014 if (rq->current_nr_sectors !=
1015 (rq->bh->b_size >> SECTOR_BITS)) {
1016 printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1017 drive->name, rq->current_nr_sectors);
1018 cdrom_end_request (0, drive);
1019 return;
1022 sector -= nskip;
1023 nsect += nskip;
1024 rq->current_nr_sectors += nskip;
1027 /* Convert from sectors to cdrom blocks, rounding up the transfer
1028 length if needed. */
1029 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1030 frame = sector / SECTORS_PER_FRAME;
1032 /* Largest number of frames was can transfer at once is 64k-1. */
1033 nframes = MIN (nframes, 65535);
1035 /* Set up the command */
1036 memset (&pc.c, 0, sizeof (pc.c));
1037 pc.c[0] = READ_10;
1038 pc.c[7] = (nframes >> 8);
1039 pc.c[8] = (nframes & 0xff);
1040 put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]);
1042 /* Send the command to the drive and return. */
1043 (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c),
1044 &cdrom_read_intr);
1047 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1048 #define IDECD_SEEK_TIMER (2 * WAIT_MIN_SLEEP) /* 40 ms */
1049 #define IDECD_SEEK_TIMEOUT WAIT_CMD /* 10 sec */
1051 static void cdrom_seek_intr (ide_drive_t *drive)
1053 struct cdrom_info *info = drive->driver_data;
1054 int stat;
1055 static int retry = 10;
1057 if (cdrom_decode_status (drive, 0, &stat))
1058 return;
1059 CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1061 if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {
1062 if (--retry == 0) {
1063 printk ("%s: disabled DSC seek overlap\n", drive->name);
1064 drive->dsc_overlap = 0;
1069 static void cdrom_start_seek_continuation (ide_drive_t *drive)
1071 struct packet_command pc;
1072 struct request *rq = HWGROUP(drive)->rq;
1073 int sector, frame, nskip;
1075 sector = rq->sector;
1076 nskip = (sector % SECTORS_PER_FRAME);
1077 if (nskip > 0)
1078 sector -= nskip;
1079 frame = sector / SECTORS_PER_FRAME;
1081 memset (&pc.c, 0, sizeof (pc.c));
1082 pc.c[0] = SEEK;
1083 put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]);
1084 (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c), &cdrom_seek_intr);
1087 static void cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1089 struct cdrom_info *info = drive->driver_data;
1091 info->dma = 0;
1092 info->start_seek = jiffies;
1093 cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);
1097 * Start a read request from the CD-ROM.
1099 static void cdrom_start_read (ide_drive_t *drive, unsigned int block)
1101 struct cdrom_info *info = drive->driver_data;
1102 struct request *rq = HWGROUP(drive)->rq;
1103 int minor = MINOR (rq->rq_dev);
1105 /* If the request is relative to a partition, fix it up to refer to the
1106 absolute address. */
1107 if ((minor & PARTN_MASK) != 0) {
1108 rq->sector = block;
1109 minor &= ~PARTN_MASK;
1110 rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1113 /* We may be retrying this request after an error. Fix up
1114 any weirdness which might be present in the request packet. */
1115 restore_request (rq);
1117 /* Satisfy whatever we can of this request from our cached sector. */
1118 if (cdrom_read_from_buffer (drive))
1119 return;
1121 /* Clear the local sector buffer. */
1122 info->nsectors_buffered = 0;
1124 if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) && (rq->nr_sectors % SECTORS_PER_FRAME == 0))
1125 info->dma = 1;
1126 else
1127 info->dma = 0;
1129 /* Start sending the read request to the drive. */
1130 cdrom_start_packet_command (drive, 32768,
1131 cdrom_start_read_continuation);
1137 /****************************************************************************
1138 * Execute all other packet commands.
1141 /* Forward declarations. */
1142 static int
1143 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1144 struct atapi_request_sense *reqbuf);
1148 /* Interrupt routine for packet command completion. */
1149 static void cdrom_pc_intr (ide_drive_t *drive)
1151 int ireason, len, stat, thislen;
1152 struct request *rq = HWGROUP(drive)->rq;
1153 struct packet_command *pc = (struct packet_command *)rq->buffer;
1155 /* Check for errors. */
1156 if (cdrom_decode_status (drive, 0, &stat))
1157 return;
1159 /* Read the interrupt reason and the transfer length. */
1160 ireason = IN_BYTE (IDE_NSECTOR_REG);
1161 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1163 /* If DRQ is clear, the command has completed.
1164 Complain if we still have data left to transfer. */
1165 if ((stat & DRQ_STAT) == 0) {
1166 /* Some of the trailing request sense fields are optional, and
1167 some drives don't send them. Sigh. */
1168 if (pc->c[0] == REQUEST_SENSE &&
1169 pc->buflen > 0 &&
1170 pc->buflen <= 5) {
1171 while (pc->buflen > 0) {
1172 *pc->buffer++ = 0;
1173 --pc->buflen;
1177 if (pc->buflen == 0)
1178 cdrom_end_request (1, drive);
1179 else {
1180 /* Comment this out, because this always happens
1181 right after a reset occurs, and it is annoying to
1182 always print expected stuff. */
1184 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1185 drive->name, pc->buflen);
1187 pc->stat = 1;
1188 cdrom_end_request (1, drive);
1190 return;
1193 /* Figure out how much data to transfer. */
1194 thislen = pc->buflen;
1195 if (thislen < 0) thislen = -thislen;
1196 if (thislen > len) thislen = len;
1198 /* The drive wants to be written to. */
1199 if ((ireason & 3) == 0) {
1200 /* Check that we want to write. */
1201 if (pc->buflen > 0) {
1202 printk ("%s: cdrom_pc_intr: Drive wants "
1203 "to transfer data the wrong way!\n",
1204 drive->name);
1205 pc->stat = 1;
1206 thislen = 0;
1209 /* Transfer the data. */
1210 atapi_output_bytes (drive, pc->buffer, thislen);
1212 /* If we haven't moved enough data to satisfy the drive,
1213 add some padding. */
1214 while (len > thislen) {
1215 int dum = 0;
1216 atapi_output_bytes (drive, &dum, sizeof (dum));
1217 len -= sizeof (dum);
1220 /* Keep count of how much data we've moved. */
1221 pc->buffer += thislen;
1222 pc->buflen += thislen;
1225 /* Same drill for reading. */
1226 else if ((ireason & 3) == 2) {
1227 /* Check that we want to read. */
1228 if (pc->buflen < 0) {
1229 printk ("%s: cdrom_pc_intr: Drive wants to "
1230 "transfer data the wrong way!\n",
1231 drive->name);
1232 pc->stat = 1;
1233 thislen = 0;
1236 /* Transfer the data. */
1237 atapi_input_bytes (drive, pc->buffer, thislen);
1239 /* If we haven't moved enough data to satisfy the drive,
1240 add some padding. */
1241 while (len > thislen) {
1242 int dum = 0;
1243 atapi_input_bytes (drive, &dum, sizeof (dum));
1244 len -= sizeof (dum);
1247 /* Keep count of how much data we've moved. */
1248 pc->buffer += thislen;
1249 pc->buflen -= thislen;
1250 } else {
1251 printk ("%s: cdrom_pc_intr: The drive "
1252 "appears confused (ireason = 0x%2x)\n",
1253 drive->name, ireason);
1254 pc->stat = 1;
1257 /* Now we wait for another interrupt. */
1258 ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);
1262 static void cdrom_do_pc_continuation (ide_drive_t *drive)
1264 struct request *rq = HWGROUP(drive)->rq;
1265 struct packet_command *pc = (struct packet_command *)rq->buffer;
1267 /* Send the command to the drive and return. */
1268 cdrom_transfer_packet_command (drive, pc->c,
1269 sizeof (pc->c), &cdrom_pc_intr);
1273 static void cdrom_do_packet_command (ide_drive_t *drive)
1275 int len;
1276 struct request *rq = HWGROUP(drive)->rq;
1277 struct packet_command *pc = (struct packet_command *)rq->buffer;
1278 struct cdrom_info *info = drive->driver_data;
1280 info->dma = 0;
1282 len = pc->buflen;
1283 if (len < 0) len = -len;
1285 pc->stat = 0;
1287 /* Start sending the command to the drive. */
1288 cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1292 /* Sleep for TIME jiffies.
1293 Not to be called from an interrupt handler. */
1294 static
1295 void cdrom_sleep (int time)
1297 current->state = TASK_INTERRUPTIBLE;
1298 schedule_timeout(time);
1301 static
1302 int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc)
1304 struct atapi_request_sense my_reqbuf;
1305 int retries = 10;
1306 struct request req;
1308 /* If our caller has not provided a place to stick any sense data,
1309 use our own area. */
1310 if (pc->sense_data == NULL)
1311 pc->sense_data = &my_reqbuf;
1312 pc->sense_data->sense_key = 0;
1314 /* Start of retry loop. */
1315 do {
1316 ide_init_drive_cmd (&req);
1317 req.cmd = PACKET_COMMAND;
1318 req.buffer = (char *)pc;
1319 if (ide_do_drive_cmd (drive, &req, ide_wait)) {
1320 printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
1321 drive->name, req.buffer[0], req.buffer[1]);
1322 /* FIXME: we should probably abort/retry or something */
1324 if (pc->stat != 0) {
1325 /* The request failed. Retry if it was due to a unit
1326 attention status
1327 (usually means media was changed). */
1328 struct atapi_request_sense *reqbuf = pc->sense_data;
1330 if (reqbuf->sense_key == UNIT_ATTENTION)
1332 else if (reqbuf->sense_key == NOT_READY &&
1333 reqbuf->asc == 4) {
1334 /* The drive is in the process of loading
1335 a disk. Retry, but wait a little to give
1336 the drive time to complete the load. */
1337 cdrom_sleep (HZ);
1338 } else
1339 /* Otherwise, don't retry. */
1340 retries = 0;
1342 --retries;
1345 /* End of retry loop. */
1346 } while (pc->stat != 0 && retries >= 0);
1349 /* Return an error if the command failed. */
1350 if (pc->stat != 0)
1351 return -EIO;
1352 else {
1353 /* The command succeeded. If it was anything other than
1354 a request sense, eject, or door lock command,
1355 and we think that the door is presently, lock it again.
1356 (The door was probably unlocked via an explicit
1357 CDROMEJECT ioctl.) */
1358 if (CDROM_STATE_FLAGS (drive)->door_locked == 0 && drive->usage &&
1359 (pc->c[0] != REQUEST_SENSE &&
1360 pc->c[0] != ALLOW_MEDIUM_REMOVAL &&
1361 pc->c[0] != START_STOP)) {
1362 (void) cdrom_lockdoor (drive, 1, NULL);
1364 return 0;
1369 /****************************************************************************
1370 * cdrom driver request routine.
1372 static
1373 void ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, unsigned long block)
1375 if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1376 cdrom_do_packet_command (drive);
1377 else if (rq -> cmd == RESET_DRIVE_COMMAND) {
1378 cdrom_end_request (1, drive);
1379 ide_do_reset (drive);
1380 return;
1381 } else if (rq -> cmd != READ) {
1382 printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1383 cdrom_end_request (0, drive);
1384 } else {
1385 struct cdrom_info *info = drive->driver_data;
1387 if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1388 unsigned long elpased = jiffies - info->start_seek;
1389 int stat = GET_STAT();
1391 if ((stat & SEEK_STAT) != SEEK_STAT) {
1392 if (elpased < IDECD_SEEK_TIMEOUT) {
1393 ide_stall_queue (drive, IDECD_SEEK_TIMER);
1394 return;
1396 printk ("%s: DSC timeout\n", drive->name);
1398 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1400 if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap)
1401 cdrom_start_seek (drive, block);
1402 else
1403 cdrom_start_read (drive, block);
1404 info->last_block = block;
1410 /****************************************************************************
1411 * Ioctl handling.
1413 * Routines which queue packet commands take as a final argument a pointer
1414 * to an atapi_request_sense struct. If execution of the command results
1415 * in an error with a CHECK CONDITION status, this structure will be filled
1416 * with the results of the subsequent request sense command. The pointer
1417 * can also be NULL, in which case no sense information is returned.
1420 #if ! STANDARD_ATAPI
1421 static inline
1422 int bin2bcd (int x)
1424 return (x%10) | ((x/10) << 4);
1428 static inline
1429 int bcd2bin (int x)
1431 return (x >> 4) * 10 + (x & 0x0f);
1434 static
1435 void msf_from_bcd (struct atapi_msf *msf)
1437 msf->minute = bcd2bin (msf->minute);
1438 msf->second = bcd2bin (msf->second);
1439 msf->frame = bcd2bin (msf->frame);
1442 #endif /* not STANDARD_ATAPI */
1445 static inline
1446 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1448 lba += CD_MSF_OFFSET;
1449 lba &= 0xffffff; /* negative lbas use only 24 bits */
1450 *m = lba / (CD_SECS * CD_FRAMES);
1451 lba %= (CD_SECS * CD_FRAMES);
1452 *s = lba / CD_FRAMES;
1453 *f = lba % CD_FRAMES;
1457 static inline
1458 int msf_to_lba (byte m, byte s, byte f)
1460 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1464 static int
1465 cdrom_check_status (ide_drive_t *drive,
1466 struct atapi_request_sense *reqbuf)
1468 struct packet_command pc;
1470 memset (&pc, 0, sizeof (pc));
1472 pc.sense_data = reqbuf;
1473 pc.c[0] = TEST_UNIT_READY;
1475 #if ! STANDARD_ATAPI
1476 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1477 switch CDs instead of supporting the LOAD_UNLOAD opcode */
1479 pc.c[7] = CDROM_STATE_FLAGS (drive)->sanyo_slot % 3;
1480 #endif /* not STANDARD_ATAPI */
1482 return cdrom_queue_packet_command (drive, &pc);
1486 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1487 static int
1488 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1489 struct atapi_request_sense *reqbuf)
1491 struct atapi_request_sense my_reqbuf;
1492 int stat;
1493 struct packet_command pc;
1495 if (reqbuf == NULL)
1496 reqbuf = &my_reqbuf;
1498 /* If the drive cannot lock the door, just pretend. */
1499 if (CDROM_CONFIG_FLAGS (drive)->no_doorlock)
1500 stat = 0;
1501 else {
1502 memset (&pc, 0, sizeof (pc));
1503 pc.sense_data = reqbuf;
1505 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1506 pc.c[4] = (lockflag != 0);
1507 stat = cdrom_queue_packet_command (drive, &pc);
1510 /* If we got an illegal field error, the drive
1511 probably cannot lock the door. */
1512 if (stat != 0 &&
1513 reqbuf->sense_key == ILLEGAL_REQUEST &&
1514 (reqbuf->asc == 0x24 || reqbuf->asc == 0x20)) {
1515 printk ("%s: door locking not supported\n",
1516 drive->name);
1517 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1518 stat = 0;
1521 if (stat == 0)
1522 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1524 return stat;
1528 /* Eject the disk if EJECTFLAG is 0.
1529 If EJECTFLAG is 1, try to reload the disk. */
1530 static int
1531 cdrom_eject (ide_drive_t *drive, int ejectflag,
1532 struct atapi_request_sense *reqbuf)
1534 struct packet_command pc;
1536 if (CDROM_CONFIG_FLAGS (drive)->no_eject==1 && ejectflag==0)
1537 return -EDRIVE_CANT_DO_THIS;
1539 memset (&pc, 0, sizeof (pc));
1540 pc.sense_data = reqbuf;
1542 pc.c[0] = START_STOP;
1543 pc.c[4] = 2 + (ejectflag != 0);
1544 return cdrom_queue_packet_command (drive, &pc);
1548 static int
1549 cdrom_pause (ide_drive_t *drive, int pauseflag,
1550 struct atapi_request_sense *reqbuf)
1552 struct packet_command pc;
1554 memset (&pc, 0, sizeof (pc));
1555 pc.sense_data = reqbuf;
1557 pc.c[0] = SCMD_PAUSE_RESUME;
1558 pc.c[8] = !pauseflag;
1559 return cdrom_queue_packet_command (drive, &pc);
1563 static int
1564 cdrom_startstop (ide_drive_t *drive, int startflag,
1565 struct atapi_request_sense *reqbuf)
1567 struct packet_command pc;
1569 memset (&pc, 0, sizeof (pc));
1570 pc.sense_data = reqbuf;
1572 pc.c[0] = START_STOP;
1573 pc.c[1] = 1;
1574 pc.c[4] = startflag;
1575 return cdrom_queue_packet_command (drive, &pc);
1578 static int
1579 cdrom_read_capacity (ide_drive_t *drive, unsigned *capacity,
1580 struct atapi_request_sense *reqbuf)
1582 struct {
1583 unsigned lba;
1584 unsigned blocklen;
1585 } capbuf;
1587 int stat;
1588 struct packet_command pc;
1590 memset (&pc, 0, sizeof (pc));
1591 pc.sense_data = reqbuf;
1593 pc.c[0] = READ_CAPACITY;
1594 pc.buffer = (char *)&capbuf;
1595 pc.buflen = sizeof (capbuf);
1597 stat = cdrom_queue_packet_command (drive, &pc);
1598 if (stat == 0)
1599 *capacity = ntohl (capbuf.lba);
1601 return stat;
1605 static int
1606 cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
1607 int format, char *buf, int buflen,
1608 struct atapi_request_sense *reqbuf)
1610 struct packet_command pc;
1612 memset (&pc, 0, sizeof (pc));
1613 pc.sense_data = reqbuf;
1615 pc.buffer = buf;
1616 pc.buflen = buflen;
1617 pc.c[0] = SCMD_READ_TOC;
1618 pc.c[6] = trackno;
1619 pc.c[7] = (buflen >> 8);
1620 pc.c[8] = (buflen & 0xff);
1621 pc.c[9] = (format << 6);
1622 if (msf_flag) pc.c[1] = 2;
1623 return cdrom_queue_packet_command (drive, &pc);
1627 /* Try to read the entire TOC for the disk into our internal buffer. */
1628 static int
1629 cdrom_read_toc (ide_drive_t *drive,
1630 struct atapi_request_sense *reqbuf)
1632 int stat, ntracks, i;
1633 struct cdrom_info *info = drive->driver_data;
1634 struct atapi_toc *toc = info->toc;
1635 struct {
1636 struct atapi_toc_header hdr;
1637 struct atapi_toc_entry ent;
1638 } ms_tmp;
1640 if (toc == NULL) {
1641 /* Try to allocate space. */
1642 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1643 GFP_KERNEL);
1644 info->toc = toc;
1647 if (toc == NULL) {
1648 printk ("%s: No cdrom TOC buffer!\n", drive->name);
1649 return -EIO;
1652 /* Check to see if the existing data is still valid.
1653 If it is, just return. */
1654 if (CDROM_STATE_FLAGS (drive)->toc_valid)
1655 (void) cdrom_check_status (drive, NULL);
1657 if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1659 /* First read just the header, so we know how long the TOC is. */
1660 stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1661 sizeof (struct atapi_toc_header) +
1662 sizeof (struct atapi_toc_entry),
1663 reqbuf);
1664 if (stat) return stat;
1666 #if ! STANDARD_ATAPI
1667 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1668 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1669 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1671 #endif /* not STANDARD_ATAPI */
1673 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1674 if (ntracks <= 0) return -EIO;
1675 if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1677 /* Now read the whole schmeer. */
1678 stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1679 sizeof (struct atapi_toc_header) +
1680 (ntracks+1) *
1681 sizeof (struct atapi_toc_entry),
1682 reqbuf);
1683 if (stat) return stat;
1684 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1686 #if ! STANDARD_ATAPI
1687 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1688 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1689 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1691 #endif /* not STANDARD_ATAPI */
1693 for (i=0; i<=ntracks; i++) {
1694 #if ! STANDARD_ATAPI
1695 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1696 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1697 toc->ent[i].track = bcd2bin (toc->ent[i].track);
1698 msf_from_bcd (&toc->ent[i].addr.msf);
1700 #endif /* not STANDARD_ATAPI */
1701 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1702 toc->ent[i].addr.msf.second,
1703 toc->ent[i].addr.msf.frame);
1706 /* Read the multisession information. */
1707 stat = cdrom_read_tocentry (drive, 0, 1, 1,
1708 (char *)&ms_tmp, sizeof (ms_tmp),
1709 reqbuf);
1710 if (stat) return stat;
1712 #if ! STANDARD_ATAPI
1713 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1714 msf_from_bcd (&ms_tmp.ent.addr.msf);
1715 #endif /* not STANDARD_ATAPI */
1717 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1718 ms_tmp.ent.addr.msf.second,
1719 ms_tmp.ent.addr.msf.frame);
1721 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1723 /* Now try to get the total cdrom capacity. */
1724 stat = cdrom_read_capacity (drive, &toc->capacity, reqbuf);
1725 if (stat) toc->capacity = 0x1fffff;
1727 HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS]
1728 = (toc->capacity * SECTORS_PER_FRAME) >> (BLOCK_SIZE_BITS - 9);
1729 drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1731 /* Remember that we've read this stuff. */
1732 CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1734 return 0;
1738 static int
1739 cdrom_read_subchannel (ide_drive_t *drive, int format,
1740 char *buf, int buflen,
1741 struct atapi_request_sense *reqbuf)
1743 struct packet_command pc;
1745 memset (&pc, 0, sizeof (pc));
1746 pc.sense_data = reqbuf;
1748 pc.buffer = buf;
1749 pc.buflen = buflen;
1750 pc.c[0] = SCMD_READ_SUBCHANNEL;
1751 pc.c[1] = 2; /* MSF addressing */
1752 pc.c[2] = 0x40; /* request subQ data */
1753 pc.c[3] = format;
1754 pc.c[7] = (buflen >> 8);
1755 pc.c[8] = (buflen & 0xff);
1756 return cdrom_queue_packet_command (drive, &pc);
1760 /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1761 static int
1762 cdrom_mode_sense (ide_drive_t *drive, int pageno, int modeflag,
1763 char *buf, int buflen,
1764 struct atapi_request_sense *reqbuf)
1766 struct packet_command pc;
1768 memset (&pc, 0, sizeof (pc));
1769 pc.sense_data = reqbuf;
1771 pc.buffer = buf;
1772 pc.buflen = buflen;
1773 pc.c[0] = MODE_SENSE_10;
1774 pc.c[2] = pageno | (modeflag << 6);
1775 pc.c[7] = (buflen >> 8);
1776 pc.c[8] = (buflen & 0xff);
1777 return cdrom_queue_packet_command (drive, &pc);
1780 static int
1781 cdrom_mode_select (ide_drive_t *drive, int pageno, char *buf, int buflen,
1782 struct atapi_request_sense *reqbuf)
1784 struct packet_command pc;
1786 memset (&pc, 0, sizeof (pc));
1787 pc.sense_data = reqbuf;
1789 pc.buffer = buf;
1790 pc.buflen = - buflen;
1791 pc.c[0] = MODE_SELECT_10;
1792 pc.c[1] = 0x10;
1793 pc.c[2] = pageno;
1794 pc.c[7] = (buflen >> 8);
1795 pc.c[8] = (buflen & 0xff);
1796 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
1803 cdrom_select_speed (ide_drive_t *drive, int speed,
1804 struct atapi_request_sense *reqbuf)
1806 struct packet_command pc;
1807 memset (&pc, 0, sizeof (pc));
1808 pc.sense_data = reqbuf;
1810 if (speed == 0)
1811 speed = 0xffff; /* set to max */
1812 else
1813 speed *= 177; /* Nx to kbytes/s */
1815 pc.c[0] = SET_CD_SPEED;
1816 /* Read Drive speed in kbytes/second MSB */
1817 pc.c[2] = (speed >> 8) & 0xff;
1818 /* Read Drive speed in kbytes/second LSB */
1819 pc.c[3] = speed & 0xff;
1820 if ( CDROM_CONFIG_FLAGS(drive)->cd_r ||
1821 CDROM_CONFIG_FLAGS(drive)->cd_rw ) {
1822 /* Write Drive speed in kbytes/second MSB */
1823 pc.c[4] = (speed >> 8) & 0xff;
1824 /* Write Drive speed in kbytes/second LSB */
1825 pc.c[5] = speed & 0xff;
1828 return cdrom_queue_packet_command (drive, &pc);
1831 static int
1832 cdrom_play_lba_range_1 (ide_drive_t *drive, int lba_start, int lba_end,
1833 struct atapi_request_sense *reqbuf)
1835 struct packet_command pc;
1837 memset (&pc, 0, sizeof (pc));
1838 pc.sense_data = reqbuf;
1840 pc.c[0] = SCMD_PLAYAUDIO_MSF;
1841 lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1842 lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1844 #if ! STANDARD_ATAPI
1845 if (CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd) {
1846 pc.c[3] = bin2bcd (pc.c[3]);
1847 pc.c[4] = bin2bcd (pc.c[4]);
1848 pc.c[5] = bin2bcd (pc.c[5]);
1849 pc.c[6] = bin2bcd (pc.c[6]);
1850 pc.c[7] = bin2bcd (pc.c[7]);
1851 pc.c[8] = bin2bcd (pc.c[8]);
1853 #endif /* not STANDARD_ATAPI */
1855 return cdrom_queue_packet_command (drive, &pc);
1859 /* Play audio starting at LBA LBA_START and finishing with the
1860 LBA before LBA_END. */
1861 static int
1862 cdrom_play_lba_range (ide_drive_t *drive, int lba_start, int lba_end,
1863 struct atapi_request_sense *reqbuf)
1865 int i, stat;
1866 struct atapi_request_sense my_reqbuf;
1868 if (reqbuf == NULL)
1869 reqbuf = &my_reqbuf;
1871 /* Some drives, will, for certain audio cds,
1872 give an error if you ask them to play the entire cd using the
1873 values which are returned in the TOC. The play will succeed,
1874 however, if the ending address is adjusted downwards
1875 by a few frames. */
1876 for (i=0; i<75; i++) {
1877 stat = cdrom_play_lba_range_1 (drive, lba_start, lba_end,
1878 reqbuf);
1880 if (stat == 0 ||
1881 !(reqbuf->sense_key == ILLEGAL_REQUEST &&
1882 reqbuf->asc == 0x24))
1883 return stat;
1885 --lba_end;
1886 if (lba_end <= lba_start) break;
1889 return stat;
1893 static
1894 int cdrom_get_toc_entry (ide_drive_t *drive, int track,
1895 struct atapi_toc_entry **ent,
1896 struct atapi_request_sense *reqbuf)
1898 struct cdrom_info *info = drive->driver_data;
1899 int stat, ntracks;
1900 struct atapi_toc *toc;
1902 /* Make sure our saved TOC is valid. */
1903 stat = cdrom_read_toc (drive, reqbuf);
1904 if (stat) return stat;
1906 toc = info->toc;
1908 /* Check validity of requested track number. */
1909 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1910 if (track == CDROM_LEADOUT)
1911 *ent = &toc->ent[ntracks];
1912 else if (track < toc->hdr.first_track ||
1913 track > toc->hdr.last_track)
1914 return -EINVAL;
1915 else
1916 *ent = &toc->ent[track - toc->hdr.first_track];
1918 return 0;
1922 static int
1923 cdrom_read_block (ide_drive_t *drive, int format, int lba, int nblocks,
1924 char *buf, int buflen,
1925 struct atapi_request_sense *reqbuf)
1927 struct packet_command pc;
1928 struct atapi_request_sense my_reqbuf;
1930 if (reqbuf == NULL)
1931 reqbuf = &my_reqbuf;
1933 memset (&pc, 0, sizeof (pc));
1934 pc.sense_data = reqbuf;
1936 pc.buffer = buf;
1937 pc.buflen = buflen;
1939 #if ! STANDARD_ATAPI
1940 if (CDROM_CONFIG_FLAGS (drive)->nec260)
1941 pc.c[0] = 0xd4;
1942 else
1943 #endif /* not STANDARD_ATAPI */
1944 pc.c[0] = READ_CD;
1946 pc.c[1] = (format << 2);
1947 put_unaligned(htonl(lba), (unsigned int *) &pc.c[2]);
1948 pc.c[8] = (nblocks & 0xff);
1949 pc.c[7] = ((nblocks>>8) & 0xff);
1950 pc.c[6] = ((nblocks>>16) & 0xff);
1951 if (format <= 1)
1952 pc.c[9] = 0xf8; /* returns 2352 for any format */
1953 else
1954 pc.c[9] = 0x10;
1956 return cdrom_queue_packet_command (drive, &pc);
1960 /* If SLOT<0, unload the current slot. Otherwise, try to load SLOT. */
1961 static int
1962 cdrom_load_unload (ide_drive_t *drive, int slot,
1963 struct atapi_request_sense *reqbuf)
1965 #if ! STANDARD_ATAPI
1966 /* if the drive is a Sanyo 3 CD changer then TEST_UNIT_READY
1967 (used in the cdrom_check_status function) is used to
1968 switch CDs instead of LOAD_UNLOAD */
1970 if (CDROM_STATE_FLAGS (drive)->sanyo_slot > 0) {
1972 if ((slot == 1) || (slot == 2))
1973 CDROM_STATE_FLAGS (drive)->sanyo_slot = slot;
1974 else if (slot >= 0)
1975 CDROM_STATE_FLAGS (drive)->sanyo_slot = 3;
1976 else
1977 return 0;
1979 return cdrom_check_status (drive, reqbuf);
1982 else
1983 #endif /*not STANDARD_ATAPI */
1986 /* ATAPI Rev. 2.2+ standard for requesting switching of
1987 CDs in a multiplatter device */
1989 struct packet_command pc;
1991 memset (&pc, 0, sizeof (pc));
1992 pc.sense_data = reqbuf;
1994 pc.c[0] = LOAD_UNLOAD;
1995 pc.c[4] = 2 + (slot >= 0);
1996 pc.c[8] = slot;
1997 return cdrom_queue_packet_command (drive, &pc);
2003 /* This gets the mechanism status per ATAPI draft spec 2.6 */
2004 static int
2005 cdrom_read_mech_status (ide_drive_t *drive, char *buf, int buflen,
2006 struct atapi_request_sense *reqbuf)
2008 struct packet_command pc;
2010 memset (&pc, 0, sizeof (pc));
2011 pc.sense_data = reqbuf;
2013 pc.buffer = buf;
2014 pc.buflen = buflen;
2015 pc.c[0] = MECHANISM_STATUS;
2016 pc.c[8] = (buflen >> 8);
2017 pc.c[9] = (buflen & 0xff);
2018 return cdrom_queue_packet_command (drive, &pc);
2022 /* Read the drive mechanism status and slot table into our internal buffer.
2023 If the buffer does not yet exist, allocate it. */
2024 static int
2025 cdrom_read_changer_info (ide_drive_t *drive)
2027 int nslots;
2028 struct cdrom_info *info = drive->driver_data;
2030 if (info->changer_info)
2031 nslots = info->changer_info->hdr.nslots;
2033 else {
2034 struct atapi_mechstat_header mechbuf;
2035 int stat;
2037 stat = cdrom_read_mech_status (drive,
2038 (char *)&mechbuf,
2039 sizeof (mechbuf),
2040 NULL);
2041 if (stat)
2042 return stat;
2044 nslots = mechbuf.nslots;
2045 info->changer_info =
2046 (struct atapi_changer_info *)
2047 kmalloc (sizeof (struct atapi_changer_info) +
2048 nslots * sizeof (struct atapi_slot),
2049 GFP_KERNEL);
2051 if (info->changer_info == NULL)
2052 return -ENOMEM;
2055 return cdrom_read_mech_status
2056 (drive,
2057 (char *)&info->changer_info->hdr,
2058 sizeof (struct atapi_mechstat_header) +
2059 nslots * sizeof (struct atapi_slot),
2060 NULL);
2064 static
2065 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
2066 unsigned int cmd, unsigned long arg)
2069 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2070 struct cdrom_info *info = drive->driver_data;
2073 switch (cmd) {
2074 case CDROMREADRAW:
2075 case CDROMREADMODE1:
2076 case CDROMREADMODE2: {
2077 struct cdrom_msf msf;
2078 int blocksize, format, stat, lba;
2079 struct atapi_toc *toc;
2080 char *buf;
2082 if (cmd == CDROMREADMODE1) {
2083 blocksize = CD_FRAMESIZE;
2084 format = 2;
2085 } else if (cmd == CDROMREADMODE2) {
2086 blocksize = CD_FRAMESIZE_RAW0;
2087 format = 3;
2088 } else {
2089 blocksize = CD_FRAMESIZE_RAW;
2090 format = 0;
2092 stat = verify_area (VERIFY_WRITE, (char *)arg, blocksize);
2093 if (stat) return stat;
2095 copy_from_user (&msf, (void *)arg, sizeof (msf));
2097 lba = msf_to_lba (msf.cdmsf_min0,
2098 msf.cdmsf_sec0,
2099 msf.cdmsf_frame0);
2101 /* Make sure the TOC is up to date. */
2102 stat = cdrom_read_toc (drive, NULL);
2103 if (stat) return stat;
2105 toc = info->toc;
2107 if (lba < 0 || lba >= toc->capacity)
2108 return -EINVAL;
2110 buf = (char *) kmalloc (CD_FRAMESIZE_RAW, GFP_KERNEL);
2111 if (buf == NULL)
2112 return -ENOMEM;
2114 stat = cdrom_read_block (drive, format, lba, 1, buf, blocksize,
2115 NULL);
2116 if (stat == 0)
2117 copy_to_user ((char *)arg, buf, blocksize);
2119 kfree (buf);
2120 return stat;
2123 /* Read 2352 byte blocks from audio tracks. */
2124 case CDROMREADAUDIO: {
2125 int stat, lba;
2126 struct atapi_toc *toc;
2127 struct cdrom_read_audio ra;
2128 char *buf;
2130 /* Make sure the TOC is up to date. */
2131 stat = cdrom_read_toc (drive, NULL);
2132 if (stat) return stat;
2134 toc = info->toc;
2136 stat = verify_area (VERIFY_READ, (char *)arg, sizeof (ra));
2137 if (stat) return stat;
2139 copy_from_user (&ra, (void *)arg, sizeof (ra));
2141 if (ra.nframes < 0 || ra.nframes > toc->capacity)
2142 return -EINVAL;
2143 else if (ra.nframes == 0)
2144 return 0;
2146 stat = verify_area (VERIFY_WRITE, (char *)ra.buf,
2147 ra.nframes * CD_FRAMESIZE_RAW);
2148 if (stat) return stat;
2150 if (ra.addr_format == CDROM_MSF)
2151 lba = msf_to_lba (ra.addr.msf.minute,
2152 ra.addr.msf.second,
2153 ra.addr.msf.frame);
2154 else if (ra.addr_format == CDROM_LBA)
2155 lba = ra.addr.lba;
2156 else
2157 return -EINVAL;
2159 if (lba < 0 || lba >= toc->capacity)
2160 return -EINVAL;
2162 buf = (char *) kmalloc (CDROM_NBLOCKS_BUFFER*CD_FRAMESIZE_RAW,
2163 GFP_KERNEL);
2164 if (buf == NULL)
2165 return -ENOMEM;
2167 while (ra.nframes > 0) {
2168 int this_nblocks = ra.nframes;
2169 if (this_nblocks > CDROM_NBLOCKS_BUFFER)
2170 this_nblocks = CDROM_NBLOCKS_BUFFER;
2171 stat = cdrom_read_block
2172 (drive, 1, lba, this_nblocks,
2173 buf, this_nblocks * CD_FRAMESIZE_RAW, NULL);
2174 if (stat) break;
2176 copy_to_user (ra.buf, buf,
2177 this_nblocks * CD_FRAMESIZE_RAW);
2178 ra.buf += this_nblocks * CD_FRAMESIZE_RAW;
2179 ra.nframes -= this_nblocks;
2180 lba += this_nblocks;
2183 kfree (buf);
2184 return stat;
2187 case CDROMSETSPINDOWN: {
2188 char spindown;
2189 char buffer[16];
2190 int stat;
2192 stat = verify_area (VERIFY_READ, (void *) arg,
2193 sizeof (char));
2194 if (stat) return stat;
2196 copy_from_user (&spindown, (void *) arg, sizeof(char));
2198 stat = cdrom_mode_sense (drive, PAGE_CDROM, 0, buffer,
2199 sizeof (buffer), NULL);
2200 if (stat) return stat;
2202 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2204 return cdrom_mode_select (drive, PAGE_CDROM, buffer,
2205 sizeof (buffer), NULL);
2208 case CDROMGETSPINDOWN: {
2209 char spindown;
2210 char buffer[16];
2211 int stat;
2213 stat = verify_area (VERIFY_WRITE, (void *) arg,
2214 sizeof (char));
2215 if (stat) return stat;
2217 stat = cdrom_mode_sense (drive, PAGE_CDROM, 0, buffer,
2218 sizeof (buffer), NULL);
2219 if (stat) return stat;
2221 spindown = buffer[11] & 0x0f;
2223 copy_to_user ((void *) arg, &spindown, sizeof (char));
2225 return 0;
2228 #ifdef ALLOW_TEST_PACKETS
2229 case 0x1234: {
2230 int stat;
2231 struct packet_command pc;
2232 int len, lena;
2234 memset (&pc, 0, sizeof (pc));
2236 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
2237 if (stat) return stat;
2238 copy_from_user (&pc.c, (void *) arg, sizeof (pc.c));
2239 arg += sizeof (pc.c);
2241 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (len));
2242 if (stat) return stat;
2243 copy_from_user (&len, (void *) arg , sizeof (len));
2244 arg += sizeof (len);
2246 lena = len;
2247 if (lena < 0) lena = -lena;
2250 char buf[lena];
2251 if (len > 0) {
2252 stat = verify_area (VERIFY_WRITE,
2253 (void *) arg, len);
2254 if (stat) return stat;
2256 else if (len < 0) {
2257 stat = verify_area (VERIFY_READ,
2258 (void *) arg, -len);
2259 if (stat) return stat;
2260 copy_from_user (buf, (void*)arg, -len);
2263 if (len != 0) {
2264 pc.buflen = len;
2265 pc.buffer = buf;
2268 stat = cdrom_queue_packet_command (drive, &pc);
2270 if (len > 0)
2271 copy_to_user ((void *)arg, buf, len);
2274 return stat;
2276 #endif
2278 default:
2279 return -EINVAL;
2286 static
2287 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2288 unsigned int cmd, void *arg)
2291 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2292 struct cdrom_info *info = drive->driver_data;
2294 switch (cmd) {
2295 case CDROMSUBCHNL: {
2296 struct atapi_cdrom_subchnl scbuf;
2297 int stat;
2298 struct cdrom_subchnl *subchnl = (struct cdrom_subchnl *)arg;
2300 stat = cdrom_read_subchannel (drive, 1, /* current position */
2301 (char *)&scbuf, sizeof (scbuf),
2302 NULL);
2303 if (stat) return stat;
2305 #if ! STANDARD_ATAPI
2306 if (CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd) {
2307 msf_from_bcd (&scbuf.acdsc_absaddr.msf);
2308 msf_from_bcd (&scbuf.acdsc_reladdr.msf);
2310 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
2311 scbuf.acdsc_trk = bcd2bin (scbuf.acdsc_trk);
2312 #endif /* not STANDARD_ATAPI */
2314 subchnl->cdsc_absaddr.msf.minute =
2315 scbuf.acdsc_absaddr.msf.minute;
2316 subchnl->cdsc_absaddr.msf.second =
2317 scbuf.acdsc_absaddr.msf.second;
2318 subchnl->cdsc_absaddr.msf.frame =
2319 scbuf.acdsc_absaddr.msf.frame;
2321 subchnl->cdsc_reladdr.msf.minute =
2322 scbuf.acdsc_reladdr.msf.minute;
2323 subchnl->cdsc_reladdr.msf.second =
2324 scbuf.acdsc_reladdr.msf.second;
2325 subchnl->cdsc_reladdr.msf.frame =
2326 scbuf.acdsc_reladdr.msf.frame;
2328 subchnl->cdsc_audiostatus = scbuf.acdsc_audiostatus;
2329 subchnl->cdsc_ctrl = scbuf.acdsc_ctrl;
2330 subchnl->cdsc_trk = scbuf.acdsc_trk;
2331 subchnl->cdsc_ind = scbuf.acdsc_ind;
2333 return 0;
2336 case CDROMREADTOCHDR: {
2337 int stat;
2338 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2339 struct atapi_toc *toc;
2341 /* Make sure our saved TOC is valid. */
2342 stat = cdrom_read_toc (drive, NULL);
2343 if (stat) return stat;
2345 toc = info->toc;
2346 tochdr->cdth_trk0 = toc->hdr.first_track;
2347 tochdr->cdth_trk1 = toc->hdr.last_track;
2349 return 0;
2352 case CDROMREADTOCENTRY: {
2353 int stat;
2354 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
2355 struct atapi_toc_entry *toce;
2357 stat = cdrom_get_toc_entry (drive, tocentry->cdte_track, &toce,
2358 NULL);
2359 if (stat) return stat;
2361 tocentry->cdte_ctrl = toce->control;
2362 tocentry->cdte_adr = toce->adr;
2363 tocentry->cdte_format = CDROM_LBA;
2364 tocentry->cdte_addr.lba = toce->addr.lba;
2366 return 0;
2369 case CDROMPLAYMSF: {
2370 struct cdrom_msf *msf = (struct cdrom_msf *) arg;
2371 int lba_start, lba_end;
2373 lba_start = msf_to_lba (msf->cdmsf_min0, msf->cdmsf_sec0,
2374 msf->cdmsf_frame0);
2375 lba_end = msf_to_lba (msf->cdmsf_min1, msf->cdmsf_sec1,
2376 msf->cdmsf_frame1) + 1;
2378 if (lba_end <= lba_start) return -EINVAL;
2380 return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2383 /* Like just about every other Linux cdrom driver, we ignore the
2384 index part of the request here. */
2385 case CDROMPLAYTRKIND: {
2386 int stat, lba_start, lba_end;
2387 struct cdrom_ti *ti = (struct cdrom_ti *)arg;
2388 struct atapi_toc_entry *first_toc, *last_toc;
2390 stat = cdrom_get_toc_entry (drive, ti->cdti_trk0, &first_toc,
2391 NULL);
2392 if (stat) return stat;
2393 stat = cdrom_get_toc_entry (drive, ti->cdti_trk1, &last_toc,
2394 NULL);
2395 if (stat) return stat;
2397 if (ti->cdti_trk1 != CDROM_LEADOUT) ++last_toc;
2398 lba_start = first_toc->addr.lba;
2399 lba_end = last_toc->addr.lba;
2401 if (lba_end <= lba_start) return -EINVAL;
2403 return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2406 case CDROMVOLCTRL: {
2407 struct cdrom_volctrl *volctrl = (struct cdrom_volctrl *) arg;
2408 char buffer[24], mask[24];
2409 int stat;
2411 stat = cdrom_mode_sense (drive, PAGE_AUDIO, 0, buffer,
2412 sizeof (buffer), NULL);
2413 if (stat) return stat;
2414 stat = cdrom_mode_sense (drive, PAGE_AUDIO, 1, mask,
2415 sizeof (buffer), NULL);
2416 if (stat) return stat;
2418 buffer[1] = buffer[2] = 0;
2420 buffer[17] = volctrl->channel0 & mask[17];
2421 buffer[19] = volctrl->channel1 & mask[19];
2422 buffer[21] = volctrl->channel2 & mask[21];
2423 buffer[23] = volctrl->channel3 & mask[23];
2425 return cdrom_mode_select (drive, PAGE_AUDIO, buffer,
2426 sizeof (buffer), NULL);
2429 case CDROMVOLREAD: {
2430 struct cdrom_volctrl *volctrl = (struct cdrom_volctrl *) arg;
2431 char buffer[24];
2432 int stat;
2434 stat = cdrom_mode_sense (drive, PAGE_AUDIO, 0, buffer,
2435 sizeof (buffer), NULL);
2436 if (stat) return stat;
2438 volctrl->channel0 = buffer[17];
2439 volctrl->channel1 = buffer[19];
2440 volctrl->channel2 = buffer[21];
2441 volctrl->channel3 = buffer[23];
2443 return 0;
2446 case CDROMSTART:
2447 return cdrom_startstop (drive, 1, NULL);
2449 case CDROMSTOP: {
2450 #ifdef IHAVEADOLPHIN
2451 /* Certain Drives require this. Most don't
2452 and will produce errors upon CDROMSTOP
2453 pit says the Dolphin needs this. If you
2454 own a dolphin, just define IHAVEADOLPHIN somewhere */
2455 int stat;
2456 stat = cdrom_startstop (drive, 0, NULL);
2457 if (stat) return stat;
2458 return cdrom_eject (drive, 1, NULL);
2459 #endif /* end of IHAVEADOLPHIN */
2460 return cdrom_startstop (drive, 0, NULL);
2463 case CDROMPAUSE:
2464 return cdrom_pause (drive, 1, NULL);
2466 case CDROMRESUME:
2467 return cdrom_pause (drive, 0, NULL);
2470 default:
2471 return -EINVAL;
2476 static
2477 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2480 /* This doesn't work reliably yet, and so it is currently just a stub. */
2482 #if 0
2483 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2484 struct request req;
2485 ide_init_drive_cmd (&req);
2486 req.cmd = RESET_DRIVE_COMMAND;
2487 return ide_do_drive_cmd (drive, &req, ide_wait);
2488 #endif
2490 /* For now, just return 0, as if things had worked... */
2491 return 0;
2497 static
2498 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2500 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2502 if (position) {
2503 int stat = cdrom_lockdoor (drive, 0, NULL);
2504 if (stat) return stat;
2507 return cdrom_eject (drive, !position, NULL);
2511 static
2512 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2514 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2515 return cdrom_lockdoor (drive, lock, NULL);
2518 static
2519 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2521 int stat, attempts = 3;
2522 struct {
2523 char pad[8];
2524 struct atapi_capabilities_page cap;
2525 } buf;
2526 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2527 struct atapi_request_sense reqbuf;
2528 stat=cdrom_select_speed (drive, speed, &reqbuf);
2529 if (stat<0)
2530 return stat;
2532 /* Now with that done, update the speed fields */
2533 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2534 if (attempts-- <= 0)
2535 return 0;
2536 stat = cdrom_mode_sense (drive, PAGE_CAPABILITIES, 0,
2537 (char *)&buf, sizeof (buf), NULL);
2538 } while (stat);
2540 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2541 if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2542 CDROM_STATE_FLAGS (drive)->current_speed =
2543 (((unsigned int)buf.cap.curspeed) + (176/2)) / 176;
2544 CDROM_CONFIG_FLAGS (drive)->max_speed =
2545 (((unsigned int)buf.cap.maxspeed) + (176/2)) / 176;
2546 } else {
2547 CDROM_STATE_FLAGS (drive)->current_speed =
2548 (ntohs(buf.cap.curspeed) + (176/2)) / 176;
2549 CDROM_CONFIG_FLAGS (drive)->max_speed =
2550 (ntohs(buf.cap.maxspeed) + (176/2)) / 176;
2552 cdi->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2553 return 0;
2557 static
2558 int ide_cdrom_select_disc (struct cdrom_device_info *cdi, int slot)
2560 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2561 struct cdrom_info *info = drive->driver_data;
2563 struct atapi_request_sense my_reqbuf;
2564 int stat;
2565 int nslots, curslot;
2567 if ( ! CDROM_CONFIG_FLAGS (drive)->is_changer)
2568 return -EDRIVE_CANT_DO_THIS;
2570 #if ! STANDARD_ATAPI
2571 if (CDROM_STATE_FLAGS (drive)->sanyo_slot > 0) {
2572 nslots = 3;
2573 curslot = CDROM_STATE_FLAGS (drive)->sanyo_slot;
2574 if (curslot == 3)
2575 curslot = 0;
2577 else
2578 #endif /* not STANDARD_ATAPI */
2580 stat = cdrom_read_changer_info (drive);
2581 if (stat)
2582 return stat;
2584 nslots = info->changer_info->hdr.nslots;
2585 curslot = info->changer_info->hdr.curslot;
2588 if (slot == curslot)
2589 return curslot;
2591 if (slot == CDSL_CURRENT)
2592 return curslot;
2594 if (slot != CDSL_NONE && (slot < 0 || slot >= nslots))
2595 return -EINVAL;
2597 if (drive->usage > 1)
2598 return -EBUSY;
2600 if (slot == CDSL_NONE) {
2601 (void) cdrom_load_unload (drive, -1, NULL);
2602 cdrom_saw_media_change (drive);
2603 (void) cdrom_lockdoor (drive, 0, NULL);
2604 return 0;
2606 else {
2607 int was_locked;
2609 if (
2610 #if ! STANDARD_ATAPI
2611 CDROM_STATE_FLAGS (drive)->sanyo_slot == 0 &&
2612 #endif
2613 info->changer_info->slots[slot].disc_present == 0) {
2614 return -ENOMEDIUM;
2617 was_locked = CDROM_STATE_FLAGS (drive)->door_locked;
2618 if (was_locked)
2619 (void) cdrom_lockdoor (drive, 0, NULL);
2621 stat = cdrom_load_unload (drive, slot, NULL);
2622 cdrom_saw_media_change (drive);
2623 if (stat)
2624 return stat;
2626 stat = cdrom_check_status (drive, &my_reqbuf);
2627 if (stat && my_reqbuf.sense_key == NOT_READY)
2628 return -ENOENT;
2630 if (stat == 0 || my_reqbuf.sense_key == UNIT_ATTENTION) {
2631 stat = cdrom_read_toc (drive, &my_reqbuf);
2632 if (stat)
2633 return stat;
2636 if (was_locked)
2637 (void) cdrom_lockdoor (drive, 1, NULL);
2639 return slot;
2644 static
2645 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2647 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2648 struct cdrom_info *info = drive->driver_data;
2650 if (slot_nr == CDSL_CURRENT) {
2652 struct atapi_request_sense my_reqbuf;
2653 int stat = cdrom_check_status (drive, &my_reqbuf);
2654 if (stat == 0 || my_reqbuf.sense_key == UNIT_ATTENTION)
2655 return CDS_DISC_OK;
2657 if (my_reqbuf.sense_key == NOT_READY) {
2658 /* With my NEC260, at least, we can't distinguish
2659 between tray open and tray closed but no disc
2660 inserted. */
2661 return CDS_TRAY_OPEN;
2664 return CDS_DRIVE_NOT_READY;
2667 #if ! STANDARD_ATAPI
2668 else if (CDROM_STATE_FLAGS (drive)->sanyo_slot > 0)
2669 return CDS_NO_INFO;
2670 #endif /* not STANDARD_ATAPI */
2672 else {
2673 struct atapi_changer_info *ci;
2674 int stat = cdrom_read_changer_info (drive);
2675 if (stat < 0)
2676 return stat;
2677 ci = info->changer_info;
2679 if (ci->slots[slot_nr].disc_present)
2680 return CDS_DISC_OK;
2681 else
2682 return CDS_NO_DISC;
2686 static
2687 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2688 struct cdrom_multisession *ms_info)
2690 int stat;
2691 struct atapi_toc *toc;
2692 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2693 struct cdrom_info *info = drive->driver_data;
2695 /* Make sure the TOC information is valid. */
2696 stat = cdrom_read_toc (drive, NULL);
2697 if (stat) return stat;
2699 toc = info->toc;
2700 ms_info->addr.lba = toc->last_session_lba;
2701 ms_info->xa_flag = toc->xa_flag;
2703 return 0;
2707 static
2708 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2709 struct cdrom_mcn *mcn_info)
2711 int stat;
2712 char mcnbuf[24];
2713 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2715 stat = cdrom_read_subchannel (drive, 2, /* get MCN */
2716 mcnbuf, sizeof (mcnbuf),
2717 NULL);
2718 if (stat) return stat;
2720 memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2721 sizeof (mcn_info->medium_catalog_number)-1);
2722 mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2723 = '\0';
2725 return 0;
2730 /****************************************************************************
2731 * Other driver requests (open, close, check media change).
2734 static
2735 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2736 int slot_nr)
2738 ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2739 struct cdrom_info *info = drive->driver_data;
2741 int retval;
2743 if (slot_nr == CDSL_CURRENT) {
2744 (void) cdrom_check_status (drive, NULL);
2745 retval = CDROM_STATE_FLAGS (drive)->media_changed;
2746 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2749 #if ! STANDARD_ATAPI
2750 else if (CDROM_STATE_FLAGS (drive)->sanyo_slot > 0) {
2751 retval = 0;
2753 #endif /* not STANDARD_ATAPI */
2755 else {
2756 struct atapi_changer_info *ci;
2757 int stat = cdrom_read_changer_info (drive);
2758 if (stat < 0)
2759 return stat;
2760 ci = info->changer_info;
2762 /* This test may be redundant with cdrom.c. */
2763 if (slot_nr < 0 || slot_nr >= ci->hdr.nslots)
2764 return -EINVAL;
2766 retval = ci->slots[slot_nr].change;
2769 return retval;
2773 static
2774 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2776 return 0;
2781 * Close down the device. Invalidate all cached blocks.
2784 static
2785 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2791 /****************************************************************************
2792 * Device initialization.
2795 static
2796 struct cdrom_device_ops ide_cdrom_dops = {
2797 ide_cdrom_open_real, /* open */
2798 ide_cdrom_release_real, /* release */
2799 ide_cdrom_drive_status, /* drive_status */
2800 ide_cdrom_check_media_change_real, /* media_changed */
2801 ide_cdrom_tray_move, /* tray_move */
2802 ide_cdrom_lock_door, /* lock_door */
2803 ide_cdrom_select_speed, /* select_speed */
2804 ide_cdrom_select_disc, /* select_disc */
2805 ide_cdrom_get_last_session, /* get_last_session */
2806 ide_cdrom_get_mcn, /* get_mcn */
2807 ide_cdrom_reset, /* reset */
2808 ide_cdrom_audio_ioctl, /* audio_ioctl */
2809 ide_cdrom_dev_ioctl, /* dev_ioctl */
2810 CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED
2811 | CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN
2812 | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET
2813 | CDC_IOCTLS | CDC_DRIVE_STATUS, /* capability */
2814 0 /* n_minors */
2817 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2819 struct cdrom_info *info = drive->driver_data;
2820 struct cdrom_device_info *devinfo = &info->devinfo;
2821 int minor = (drive->select.b.unit)<<PARTN_BITS;
2823 devinfo->dev = MKDEV (HWIF(drive)->major, minor);
2824 devinfo->ops = &ide_cdrom_dops;
2825 devinfo->mask = 0;
2826 *(int *)&devinfo->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2827 *(int *)&devinfo->capacity = nslots;
2828 devinfo->handle = (void *) drive;
2829 strcpy(devinfo->name, drive->name);
2830 return register_cdrom (devinfo);
2834 static
2835 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2837 int stat, nslots, attempts = 3;
2838 struct {
2839 char pad[8];
2840 struct atapi_capabilities_page cap;
2841 } buf;
2843 nslots = 0;
2845 if (CDROM_CONFIG_FLAGS (drive)->nec260)
2846 return nslots;
2848 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2849 if (attempts-- <= 0)
2850 return 0;
2851 stat = cdrom_mode_sense (drive, PAGE_CAPABILITIES, 0,
2852 (char *)&buf, sizeof (buf), NULL);
2853 } while (stat);
2855 if (buf.cap.lock == 0)
2856 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2857 if (buf.cap.eject)
2858 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2859 if (buf.cap.cd_r_write)
2860 CDROM_CONFIG_FLAGS (drive)->cd_r = 1;
2861 if (buf.cap.cd_rw_write)
2862 CDROM_CONFIG_FLAGS (drive)->cd_rw = 1;
2864 #if ! STANDARD_ATAPI
2865 if (CDROM_STATE_FLAGS (drive)->sanyo_slot > 0) {
2866 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2867 nslots = 3;
2870 else
2871 #endif /* not STANDARD_ATAPI */
2872 if (buf.cap.mechtype == mechtype_individual_changer ||
2873 buf.cap.mechtype == mechtype_cartridge_changer) {
2874 struct atapi_mechstat_header mechbuf;
2876 stat = cdrom_read_mech_status (drive, (char*)&mechbuf,
2877 sizeof (mechbuf), NULL);
2878 if (!stat) {
2879 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2880 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 1;
2881 nslots = mechbuf.nslots;
2885 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2886 if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2887 CDROM_STATE_FLAGS (drive)->current_speed =
2888 (((unsigned int)buf.cap.curspeed) + (176/2)) / 176;
2889 CDROM_CONFIG_FLAGS (drive)->max_speed =
2890 (((unsigned int)buf.cap.maxspeed) + (176/2)) / 176;
2891 } else {
2892 CDROM_STATE_FLAGS (drive)->current_speed =
2893 (ntohs(buf.cap.curspeed) + (176/2)) / 176;
2894 CDROM_CONFIG_FLAGS (drive)->max_speed =
2895 (ntohs(buf.cap.maxspeed) + (176/2)) / 176;
2898 printk ("%s: ATAPI %dX CDROM",
2899 drive->name, CDROM_CONFIG_FLAGS (drive)->max_speed);
2900 if (CDROM_CONFIG_FLAGS (drive)->cd_r|CDROM_CONFIG_FLAGS (drive)->cd_rw)
2901 printk (" CD%s%s",
2902 (CDROM_CONFIG_FLAGS (drive)->cd_r)? "-R" : "",
2903 (CDROM_CONFIG_FLAGS (drive)->cd_rw)? "/RW" : "");
2904 if (CDROM_CONFIG_FLAGS (drive)->is_changer)
2905 printk (" changer w/%d slots", nslots);
2906 else
2907 printk (" drive");
2908 printk (", %dkB Cache\n",
2909 ntohs(buf.cap.buffer_size) );
2911 return nslots;
2914 static void ide_cdrom_add_settings(ide_drive_t *drive)
2916 int major = HWIF(drive)->major;
2917 int minor = drive->select.b.unit << PARTN_BITS;
2919 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
2920 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
2921 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
2922 ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2925 static
2926 int ide_cdrom_setup (ide_drive_t *drive)
2928 struct cdrom_info *info = drive->driver_data;
2929 int nslots;
2931 kdev_t dev = MKDEV (HWIF (drive)->major,
2932 drive->select.b.unit << PARTN_BITS);
2934 set_device_ro (dev, 1);
2935 blksize_size[HWIF(drive)->major][drive->select.b.unit << PARTN_BITS] =
2936 CD_FRAMESIZE;
2938 drive->special.all = 0;
2939 drive->ready_stat = 0;
2941 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2942 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
2943 CDROM_STATE_FLAGS (drive)->door_locked = 0;
2945 #if NO_DOOR_LOCKING
2946 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2947 #else
2948 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2949 #endif
2951 if (drive->id != NULL)
2952 CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2953 ((drive->id->config & 0x0060) == 0x20);
2954 else
2955 CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2957 CDROM_CONFIG_FLAGS (drive)->is_changer = 0;
2958 CDROM_CONFIG_FLAGS (drive)->cd_r = 0;
2959 CDROM_CONFIG_FLAGS (drive)->cd_rw = 0;
2960 CDROM_CONFIG_FLAGS (drive)->no_eject = 1;
2961 CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 0;
2963 #if ! STANDARD_ATAPI
2964 /* by default Sanyo 3 CD changer support is turned off and
2965 ATAPI Rev 2.2+ standard support for CD changers is used */
2966 CDROM_STATE_FLAGS (drive)->sanyo_slot = 0;
2968 CDROM_CONFIG_FLAGS (drive)->nec260 = 0;
2969 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2970 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2971 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2972 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2974 if (drive->id != NULL) {
2975 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2976 drive->id->fw_rev[4] == '1' &&
2977 drive->id->fw_rev[6] <= '2') {
2978 /* Vertos 300.
2979 Some versions of this drive like to talk BCD. */
2980 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2981 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2982 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2983 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2986 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2987 drive->id->fw_rev[4] == '1' &&
2988 drive->id->fw_rev[6] <= '2') {
2989 /* Vertos 600 ESD. */
2990 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2993 else if (strcmp (drive->id->model,
2994 "NEC CD-ROM DRIVE:260") == 0 &&
2995 strncmp (drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
2996 /* Old NEC260 (not R).
2997 This drive was released before the 1.2 version
2998 of the spec. */
2999 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
3000 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
3001 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
3002 CDROM_CONFIG_FLAGS (drive)->nec260 = 1;
3005 else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
3006 strncmp (drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
3007 /* Wearnes */
3008 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
3009 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
3012 /* Sanyo 3 CD changer uses a non-standard command
3013 for CD changing */
3014 else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
3015 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
3016 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
3017 /* uses CD in slot 0 when value is set to 3 */
3018 CDROM_STATE_FLAGS (drive)->sanyo_slot = 3;
3023 #endif /* not STANDARD_ATAPI */
3025 info->toc = NULL;
3026 info->sector_buffer = NULL;
3027 info->sector_buffered = 0;
3028 info->nsectors_buffered = 0;
3029 info->changer_info = NULL;
3031 nslots = ide_cdrom_probe_capabilities (drive);
3033 if (ide_cdrom_register (drive, nslots)) {
3034 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
3035 info->devinfo.handle = NULL;
3036 return 1;
3038 ide_cdrom_add_settings(drive);
3039 return 0;
3042 /* Forwarding functions to generic routines. */
3043 static
3044 int ide_cdrom_ioctl (ide_drive_t *drive,
3045 struct inode *inode, struct file *file,
3046 unsigned int cmd, unsigned long arg)
3048 return cdrom_fops.ioctl (inode, file, cmd, arg);
3051 static
3052 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
3054 int rc;
3056 MOD_INC_USE_COUNT;
3057 rc = cdrom_fops.open (ip, fp);
3058 if (rc) {
3059 drive->usage--;
3060 MOD_DEC_USE_COUNT;
3062 return rc;
3065 static
3066 void ide_cdrom_release (struct inode *inode, struct file *file,
3067 ide_drive_t *drive)
3069 cdrom_fops.release (inode, file);
3070 MOD_DEC_USE_COUNT;
3073 static
3074 int ide_cdrom_check_media_change (ide_drive_t *drive)
3076 return cdrom_fops.check_media_change
3077 (MKDEV (HWIF (drive)->major,
3078 (drive->select.b.unit)<<PARTN_BITS));
3082 static
3083 int ide_cdrom_cleanup(ide_drive_t *drive)
3085 struct cdrom_info *info = drive->driver_data;
3086 struct cdrom_device_info *devinfo = &info->devinfo;
3088 if (ide_unregister_subdriver (drive))
3089 return 1;
3090 if (info->sector_buffer != NULL)
3091 kfree (info->sector_buffer);
3092 if (info->toc != NULL)
3093 kfree (info->toc);
3094 if (info->changer_info != NULL)
3095 kfree (info->changer_info);
3096 if (devinfo->handle == drive && unregister_cdrom (devinfo))
3097 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3098 kfree (info);
3099 drive->driver_data = NULL;
3100 return 0;
3103 static ide_driver_t ide_cdrom_driver = {
3104 "ide-cdrom", /* name */
3105 IDECD_VERSION, /* version */
3106 ide_cdrom, /* media */
3107 0, /* busy */
3108 1, /* supports_dma */
3109 1, /* supports_dsc_overlap */
3110 ide_cdrom_cleanup, /* cleanup */
3111 ide_do_rw_cdrom, /* do_request */
3112 NULL, /* ??? or perhaps cdrom_end_request? */
3113 ide_cdrom_ioctl, /* ioctl */
3114 ide_cdrom_open, /* open */
3115 ide_cdrom_release, /* release */
3116 ide_cdrom_check_media_change, /* media_change */
3117 NULL, /* pre_reset */
3118 NULL, /* capacity */
3119 NULL, /* special */
3120 NULL /* proc */
3123 int ide_cdrom_init (void);
3124 static ide_module_t ide_cdrom_module = {
3125 IDE_DRIVER_MODULE,
3126 ide_cdrom_init,
3127 &ide_cdrom_driver,
3128 NULL
3131 #ifdef MODULE
3132 int init_module (void)
3134 return ide_cdrom_init();
3137 void cleanup_module(void)
3139 ide_drive_t *drive;
3140 int failed = 0;
3142 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, &ide_cdrom_driver, failed)) != NULL)
3143 if (ide_cdrom_cleanup (drive)) {
3144 printk ("%s: cleanup_module() called while still busy\n", drive->name);
3145 failed++;
3147 ide_unregister_module (&ide_cdrom_module);
3149 #endif /* MODULE */
3151 int ide_cdrom_init (void)
3153 ide_drive_t *drive;
3154 struct cdrom_info *info;
3155 int failed = 0;
3157 MOD_INC_USE_COUNT;
3158 while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, NULL, failed++)) != NULL) {
3159 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
3160 if (info == NULL) {
3161 printk ("%s: Can't allocate a cdrom structure\n", drive->name);
3162 continue;
3164 if (ide_register_subdriver (drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
3165 printk ("%s: Failed to register the driver with ide.c\n", drive->name);
3166 kfree (info);
3167 continue;
3169 memset (info, 0, sizeof (struct cdrom_info));
3170 drive->driver_data = info;
3171 DRIVER(drive)->busy++;
3172 if (ide_cdrom_setup (drive)) {
3173 DRIVER(drive)->busy--;
3174 if (ide_cdrom_cleanup (drive))
3175 printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive->name);
3176 continue;
3178 DRIVER(drive)->busy--;
3179 failed--;
3181 ide_register_module(&ide_cdrom_module);
3182 MOD_DEC_USE_COUNT;
3183 return 0;
3187 /*==========================================================================*/
3189 * Local variables:
3190 * c-basic-offset: 8
3191 * End: