2 * linux/drivers/block/ide-cd.c
3 * Copyright (C) 1994, 1995, 1996 scott snyder <snyder@fnald0.fnal.gov>
4 * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
5 * Copyright (C) 1998, 1999 Jens Axboe
7 * May be copied or modified under the terms of the GNU General Public
8 * License. See linux/COPYING for more information.
10 * ATAPI CD-ROM driver. To be used with ide.c.
11 * See Documentation/cdrom/ide-cd for usage information.
13 * Suggestions are welcome. Patches that work are more welcome though. ;-)
14 * For those wishing to work on this driver, please be sure you download
15 * and comply with the latest ATAPI standard. This document can be
16 * obtained by anonymous ftp from:
17 * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
19 * Drives that deviate from the ATAPI standard will be accomodated as much
20 * as possible via compile time or command-line options. Since I only have
21 * a few drives, you generally need to send me patches...
23 * ----------------------------------
25 * -Implement Microsoft Media Status Notification per the spec at
26 * http://www.microsoft.com/hwdev/respec/storspec.htm
27 * This will allow us to get automagically notified when the media changes
28 * on ATAPI drives (something the stock ATAPI spec is lacking). Looks
29 * very cool. I discovered its existance the other day at work...
30 * -Query the drive to find what features are available before trying to
31 * use them (like trying to close the tray in drives that can't).
32 * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
34 * -Integrate DVD-ROM support in driver. Thanks to Merete Gotsæd-Petersen
35 * of Pioneer Denmark for providing me with a drive for testing.
36 * -Implement Features and Profiles.
39 * ----------------------------------
40 * 1.00 Oct 31, 1994 -- Initial version.
41 * 1.01 Nov 2, 1994 -- Fixed problem with starting request in
43 * 1.03 Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
44 * (from mlord) -- minor changes to cdrom_setup()
45 * -- renamed ide_dev_s to ide_drive_t, enable irq on command
46 * 2.00 Nov 27, 1994 -- Generalize packet command interface;
48 * 2.01 Dec 3, 1994 -- Rework packet command interface to handle devices
49 * which send an interrupt when ready for a command.
50 * 2.02 Dec 11, 1994 -- Cache the TOC in the driver.
51 * Don't use SCMD_PLAYAUDIO_TI; it's not included
52 * in the current version of ATAPI.
53 * Try to use LBA instead of track or MSF addressing
55 * Don't wait for READY_STAT.
56 * 2.03 Jan 10, 1995 -- Rewrite block read routines to handle block sizes
57 * other than 2k and to move multiple sectors in a
59 * 2.04 Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
60 * Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
61 * help in figuring this out. Ditto for Acer and
62 * Aztech drives, which seem to have the same problem.
63 * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
64 * 2.05 Jun 8, 1995 -- Don't attempt to retry after an illegal request
65 * or data protect error.
66 * Use HWIF and DEV_HWIF macros as in ide.c.
67 * Always try to do a request_sense after
69 * Include an option to give textual descriptions
71 * Fix a bug in handling the sector cache which
72 * showed up if the drive returned data in 512 byte
73 * blocks (like Pioneer drives). Thanks to
74 * Richard Hirst <srh@gpt.co.uk> for diagnosing this.
75 * Properly supply the page number field in the
76 * MODE_SELECT command.
77 * PLAYAUDIO12 is broken on the Aztech; work around it.
78 * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
79 * (my apologies to Scott, but now ide-cd.c is independent)
80 * 3.00 Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
81 * Implement CDROMREADAUDIO ioctl (UNTESTED).
82 * Use input_ide_data() and output_ide_data().
84 * Fix usage count leak in cdrom_open, which happened
85 * when a read-write mount was attempted.
86 * Try to load the disk on open.
87 * Implement CDROMEJECT_SW ioctl (off by default).
88 * Read total cdrom capacity during open.
89 * Rearrange logic in cdrom_decode_status. Issue
90 * request sense commands for failed packet commands
91 * from here instead of from cdrom_queue_packet_command.
92 * Fix a race condition in retrieving error information.
93 * Suppress printing normal unit attention errors and
94 * some drive not ready errors.
95 * Implement CDROMVOLREAD ioctl.
96 * Implement CDROMREADMODE1/2 ioctls.
97 * Fix race condition in setting up interrupt handlers
98 * when the `serialize' option is used.
99 * 3.01 Sep 2, 1995 -- Fix ordering of reenabling interrupts in
100 * cdrom_queue_request.
101 * Another try at using ide_[input,output]_data.
102 * 3.02 Sep 16, 1995 -- Stick total disk capacity in partition table as well.
103 * Make VERBOSE_IDE_CD_ERRORS dump failed command again.
104 * Dump out more information for ILLEGAL REQUEST errs.
105 * Fix handling of errors occurring before the
106 * packet command is transferred.
107 * Fix transfers with odd bytelengths.
108 * 3.03 Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
109 * `DCI-2S10' drives are broken too.
110 * 3.04 Nov 20, 1995 -- So are Vertos drives.
111 * 3.05 Dec 1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
112 * 3.06 Dec 16, 1995 -- Add support needed for partitions.
113 * More workarounds for Vertos bugs (based on patches
114 * from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
115 * Try to eliminate byteorder assumptions.
116 * Use atapi_cdrom_subchnl struct definition.
117 * Add STANDARD_ATAPI compilation option.
118 * 3.07 Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
120 * Add NO_DOOR_LOCKING configuration option.
121 * Handle drive_cmd requests w/NULL args (for hdparm -t).
122 * Work around sporadic Sony55e audio play problem.
123 * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
124 * problem with "hde=cdrom" with no drive present. -ml
125 * 3.08 Mar 6, 1996 -- More Vertos workarounds.
126 * 3.09 Apr 5, 1996 -- Add CDROMCLOSETRAY ioctl.
127 * Switch to using MSF addressing for audio commands.
128 * Reformat to match kernel tabbing style.
129 * Add CDROM_GET_UPC ioctl.
130 * 3.10 Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
131 * 3.11 Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
132 * to remove redundant verify_area calls.
133 * 3.12 May 7, 1996 -- Rudimentary changer support. Based on patches
134 * from Gerhard Zuber <zuber@berlin.snafu.de>.
135 * Let open succeed even if there's no loaded disc.
136 * 3.13 May 19, 1996 -- Fixes for changer code.
137 * 3.14 May 29, 1996 -- Add work-around for Vertos 600.
138 * (From Hennus Bergman <hennus@sky.ow.nl>.)
139 * 3.15 July 2, 1996 -- Added support for Sanyo 3 CD changers
140 * from Ben Galliart <bgallia@luc.edu> with
141 * special help from Jeff Lightfoot
142 * <jeffml@netcom.com>
143 * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
144 * 3.16 Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
145 * 3.17 Sep 17, 1996 -- Tweak audio reads for some drives.
146 * Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
147 * 3.18 Oct 31, 1996 -- Added module and DMA support.
150 * 4.00 Nov 5, 1996 -- New ide-cd maintainer,
151 * Erik B. Andersen <andersee@debian.org>
152 * -- Newer Creative drives don't always set the error
153 * register correctly. Make sure we see media changes
155 * -- Integrate with generic cdrom driver.
156 * -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
157 * a patch from Ciro Cattuto <>.
158 * -- Call set_device_ro.
159 * -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
160 * ioctls, based on patch by Erik Andersen
161 * -- Add some probes of drive capability during setup.
163 * 4.01 Nov 11, 1996 -- Split into ide-cd.c and ide-cd.h
164 * -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE
165 * ioctls in favor of a generalized approach
166 * using the generic cdrom driver.
167 * -- Fully integrated with the 2.1.X kernel.
168 * -- Other stuff that I forgot (lots of changes)
170 * 4.02 Dec 01, 1996 -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
171 * to fix the drive door locking problems.
173 * 4.03 Dec 04, 1996 -- Added DSC overlap support.
174 * 4.04 Dec 29, 1996 -- Added CDROMREADRAW ioclt based on patch
175 * by Ales Makarov (xmakarov@sun.felk.cvut.cz)
177 * 4.05 Nov 20, 1997 -- Modified to print more drive info on init
178 * Minor other changes
179 * Fix errors on CDROMSTOP (If you have a "Dolphin",
180 * you must define IHAVEADOLPHIN)
181 * Added identifier so new Sanyo CD-changer works
182 * Better detection if door locking isn't supported
184 * 4.06 Dec 17, 1997 -- fixed endless "tray open" messages -ml
185 * 4.07 Dec 17, 1997 -- fallback to set pc->stat on "tray open"
186 * 4.08 Dec 18, 1997 -- spew less noise when tray is empty
187 * -- fix speed display for ACER 24X, 18X
188 * 4.09 Jan 04, 1998 -- fix handling of the last block so we return
189 * an end of file instead of an I/O error (Gadi)
190 * 4.10 Jan 24, 1998 -- fixed a bug so now changers can change to a new
191 * slot when there is no disc in the current slot.
192 * -- Fixed a memory leak where info->changer_info was
193 * malloc'ed but never free'd when closing the device.
194 * -- Cleaned up the global namespace a bit by making more
195 * functions static that should already have been.
196 * 4.11 Mar 12, 1998 -- Added support for the CDROM_SELECT_SPEED ioctl
197 * based on a patch for 2.0.33 by Jelle Foks
198 * <jelle@scintilla.utwente.nl>, a patch for 2.0.33
199 * by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
200 * version, and my own efforts. -erik
201 * -- Fixed a stupid bug which egcs was kind enough to
202 * inform me of where "Illegal mode for this track"
203 * was never returned due to a comparison on data
204 * types of limited range.
205 * 4.12 Mar 29, 1998 -- Fixed bug in CDROM_SELECT_SPEED so write speed is
206 * now set ionly for CD-R and CD-RW drives. I had
207 * removed this support because it produced errors.
208 * It produced errors _only_ for non-writers. duh.
209 * 4.13 May 05, 1998 -- Suppress useless "in progress of becoming ready"
210 * messages, since this is not an error.
211 * -- Change error messages to be const
212 * -- Remove a "\t" which looks ugly in the syslogs
213 * 4.14 July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
214 * since the .pdf version doesn't seem to work...
215 * -- Updated the TODO list to something more current.
217 * 4.15 Aug 25, 1998 -- Updated ide-cd.h to respect mechine endianess,
218 * patch thanks to "Eddie C. Dost" <ecd@skynet.be>
220 * 4.50 Oct 19, 1998 -- New maintainers!
221 * Jens Axboe <axboe@image.dk>
222 * Chris Zwilling <chris@cloudnet.com>
224 * 4.51 Dec 23, 1998 -- Jens Axboe <axboe@image.dk>
225 * - ide_cdrom_reset enabled since the ide subsystem
226 * handles resets fine now. <axboe@image.dk>
227 * - Transfer size fix for Samsung CD-ROMs, thanks to
228 * "Ville Hallik" <ville.hallik@mail.ee>.
229 * - other minor stuff.
231 * 4.52 Jan 19, 1999 -- Jens Axboe <axboe@image.dk>
232 * - Detect DVD-ROM/RAM drives
234 * 4.53 Feb 22, 1999 - Include other model Samsung and one Goldstar
235 * drive in transfer size limit.
236 * - Fix the I/O error when doing eject without a medium
237 * loaded on some drives.
238 * - CDROMREADMODE2 is now implemented through
239 * CDROMREADRAW, since many drives don't support
240 * MODE2 (even though ATAPI 2.6 says they must).
241 * - Added ignore parameter to ide-cd (as a module), eg
242 * insmod ide-cd ignore='hda hdb'
243 * Useful when using ide-cd in conjunction with
244 * ide-scsi. TODO: non-modular way of doing the
248 *************************************************************************/
250 #define IDECD_VERSION "4.53"
252 #include <linux/config.h>
253 #include <linux/module.h>
254 #include <linux/types.h>
255 #include <linux/kernel.h>
256 #include <linux/delay.h>
257 #include <linux/timer.h>
258 #include <linux/malloc.h>
259 #include <linux/interrupt.h>
260 #include <linux/errno.h>
261 #include <linux/cdrom.h>
262 #include <linux/ide.h>
266 #include <asm/byteorder.h>
267 #include <asm/uaccess.h>
268 #include <asm/unaligned.h>
272 /****************************************************************************
273 * Generic packet command support and error handling routines.
277 /* Mark that we've seen a media change, and invalidate our internal
279 static void cdrom_saw_media_change (ide_drive_t
*drive
)
281 struct cdrom_info
*info
= drive
->driver_data
;
283 CDROM_STATE_FLAGS (drive
)->media_changed
= 1;
284 CDROM_STATE_FLAGS (drive
)->toc_valid
= 0;
285 info
->nsectors_buffered
= 0;
290 void cdrom_analyze_sense_data (ide_drive_t
*drive
,
291 struct atapi_request_sense
*reqbuf
,
292 struct packet_command
*failed_command
)
294 if (reqbuf
->sense_key
== NOT_READY
||
295 reqbuf
->sense_key
== UNIT_ATTENTION
) {
296 /* Make good and sure we've seen this potential media change.
297 Some drives (i.e. Creative) fail to present the correct
298 sense key in the error register. */
299 cdrom_saw_media_change (drive
);
302 /* Don't print not ready or unit attention errors for
303 READ_SUBCHANNEL. Workman (and probably other programs)
304 uses this command to poll the drive, and we don't want
305 to fill the syslog with useless errors. */
306 if (failed_command
&&
307 failed_command
->c
[0] == SCMD_READ_SUBCHANNEL
)
310 if (reqbuf
->error_code
== 0x70 && reqbuf
->sense_key
== 0x02
311 && ((reqbuf
->asc
== 0x3a && reqbuf
->ascq
== 0x00) ||
312 (reqbuf
->asc
== 0x04 && reqbuf
->ascq
== 0x01)))
315 * Suppress the following errors:
316 * "Medium not present", and "in progress of becoming ready",
317 * to keep the noise level down to a dull roar.
322 #if VERBOSE_IDE_CD_ERRORS
328 printk ("ATAPI device %s:\n", drive
->name
);
329 if (reqbuf
->error_code
==0x70)
331 else if (reqbuf
->error_code
==0x71)
332 printk(" Deferred Error: ");
334 printk(" Unknown Error Type: ");
336 if ( reqbuf
->sense_key
< ARY_LEN (sense_key_texts
))
337 s
= sense_key_texts
[reqbuf
->sense_key
];
339 s
= "bad sense key!";
341 printk ("%s -- (Sense key=0x%02x)\n", s
, reqbuf
->sense_key
);
343 if (reqbuf
->asc
== 0x40) {
344 sprintf (buf
, "Diagnostic failure on component 0x%02x",
348 int lo
=0, mid
, hi
=ARY_LEN (sense_data_texts
);
349 unsigned short key
= (reqbuf
->asc
<< 8);
350 if ( ! (reqbuf
->ascq
>= 0x80 && reqbuf
->ascq
<= 0xdd) )
357 if (sense_data_texts
[mid
].asc_ascq
== key
) {
358 s
= sense_data_texts
[mid
].text
;
361 else if (sense_data_texts
[mid
].asc_ascq
> key
)
369 if (reqbuf
->asc
> 0x80)
370 s
= "(vendor-specific error)";
372 s
= "(reserved error code)";
375 printk (" %s -- (asc=0x%02x, ascq=0x%02x)\n",
376 s
, reqbuf
->asc
, reqbuf
->ascq
);
378 if (failed_command
!= NULL
) {
380 int lo
=0, mid
, hi
= ARY_LEN (packet_command_texts
);
385 if (packet_command_texts
[mid
].packet_command
== failed_command
->c
[0]) {
386 s
= packet_command_texts
[mid
].text
;
389 else if (packet_command_texts
[mid
].packet_command
> failed_command
->c
[0])
395 printk (" The failed \"%s\" packet command was: \n \"", s
);
396 for (i
=0; i
<sizeof (failed_command
->c
); i
++)
397 printk ("%02x ", failed_command
->c
[i
]);
401 if (reqbuf
->sense_key
== ILLEGAL_REQUEST
&&
402 (reqbuf
->sense_key_specific
[0] & 0x80) != 0) {
403 printk (" Error in %s byte %d",
404 (reqbuf
->sense_key_specific
[0] & 0x40) != 0
407 (reqbuf
->sense_key_specific
[1] << 8) +
408 reqbuf
->sense_key_specific
[2]);
410 if ((reqbuf
->sense_key_specific
[0] & 0x40) != 0) {
412 reqbuf
->sense_key_specific
[0] & 0x07);
419 #else /* not VERBOSE_IDE_CD_ERRORS */
421 /* Suppress printing unit attention and `in progress of becoming ready'
422 errors when we're not being verbose. */
424 if (reqbuf
->sense_key
== UNIT_ATTENTION
||
425 (reqbuf
->sense_key
== NOT_READY
&& (reqbuf
->asc
== 4 ||
426 reqbuf
->asc
== 0x3a)))
429 printk ("%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
431 reqbuf
->error_code
, reqbuf
->sense_key
,
432 reqbuf
->asc
, reqbuf
->ascq
);
433 #endif /* not VERBOSE_IDE_CD_ERRORS */
437 /* Fix up a possibly partially-processed request so that we can
438 start it over entirely, or even put it back on the request queue. */
439 static void restore_request (struct request
*rq
)
441 if (rq
->buffer
!= rq
->bh
->b_data
) {
442 int n
= (rq
->buffer
- rq
->bh
->b_data
) / SECTOR_SIZE
;
443 rq
->buffer
= rq
->bh
->b_data
;
447 rq
->current_nr_sectors
= rq
->bh
->b_size
>> SECTOR_BITS
;
451 static void cdrom_queue_request_sense (ide_drive_t
*drive
,
452 struct semaphore
*sem
,
453 struct atapi_request_sense
*reqbuf
,
454 struct packet_command
*failed_command
)
456 struct cdrom_info
*info
= drive
->driver_data
;
458 struct packet_command
*pc
;
461 /* If the request didn't explicitly specify where
462 to put the sense data, use the statically allocated structure. */
464 reqbuf
= &info
->sense_data
;
466 /* Make up a new request to retrieve sense information. */
468 pc
= &info
->request_sense_pc
;
469 memset (pc
, 0, sizeof (*pc
));
471 /* The request_sense structure has an odd number of (16-bit) words,
472 which won't work well with 32-bit transfers. However, we don't care
473 about the last two bytes, so just truncate the structure down
474 to an even length. */
475 len
= sizeof (*reqbuf
) / 4;
478 pc
->c
[0] = REQUEST_SENSE
;
479 pc
->c
[4] = (unsigned char) len
;
480 pc
->buffer
= (char *)reqbuf
;
482 pc
->sense_data
= (struct atapi_request_sense
*)failed_command
;
484 /* stuff the sense request in front of our current request */
486 rq
= &info
->request_sense_request
;
487 ide_init_drive_cmd (rq
);
488 rq
->cmd
= REQUEST_SENSE_COMMAND
;
489 rq
->buffer
= (char *)pc
;
491 (void) ide_do_drive_cmd (drive
, rq
, ide_preempt
);
495 static void cdrom_end_request (int uptodate
, ide_drive_t
*drive
)
497 struct request
*rq
= HWGROUP(drive
)->rq
;
499 if (rq
->cmd
== REQUEST_SENSE_COMMAND
&& uptodate
) {
500 struct packet_command
*pc
= (struct packet_command
*)
502 cdrom_analyze_sense_data (drive
,
503 (struct atapi_request_sense
*)
504 (pc
->buffer
- pc
->c
[4]),
505 (struct packet_command
*)
508 if (rq
->cmd
== READ
&& !rq
->current_nr_sectors
)
510 ide_end_request (uptodate
, HWGROUP(drive
));
514 /* Returns 0 if the request should be continued.
515 Returns 1 if the request was ended. */
516 static int cdrom_decode_status (ide_drive_t
*drive
, int good_stat
,
519 struct request
*rq
= HWGROUP(drive
)->rq
;
520 int stat
, err
, sense_key
, cmd
;
522 /* Check for errors. */
526 if (OK_STAT (stat
, good_stat
, BAD_R_STAT
))
530 err
= IN_BYTE (IDE_ERROR_REG
);
531 sense_key
= err
>> 4;
534 printk ("%s: missing request in cdrom_decode_status\n",
539 if (cmd
== REQUEST_SENSE_COMMAND
) {
540 /* We got an error trying to get sense info
541 from the drive (probably while trying
542 to recover from a former error). Just give up. */
544 struct packet_command
*pc
= (struct packet_command
*)
547 cdrom_end_request (1, drive
);
548 ide_error (drive
, "request sense failure", stat
);
551 } else if (cmd
== PACKET_COMMAND
) {
552 /* All other functions, except for READ. */
554 struct packet_command
*pc
= (struct packet_command
*)
556 struct semaphore
*sem
= NULL
;
558 /* Check for tray open. */
559 if (sense_key
== NOT_READY
) {
560 cdrom_saw_media_change (drive
);
561 #if 0 /* let the upper layers do the complaining */
562 /* Print an error message to the syslog.
563 Exception: don't print anything if this
564 is a read subchannel command. This is
565 because workman constantly polls the drive
566 with this command, and we don't want
567 to uselessly fill up the syslog. */
568 if (pc
->c
[0] != SCMD_READ_SUBCHANNEL
)
569 printk ("%s: tray open or drive not ready\n", drive
->name
);
571 } else if (sense_key
== UNIT_ATTENTION
) {
572 /* Check for media change. */
573 cdrom_saw_media_change (drive
);
574 /*printk("%s: media changed\n",drive->name);*/
577 /* Otherwise, print an error. */
578 ide_dump_status (drive
, "packet command error",
582 /* Set the error flag and complete the request.
583 Then, if we have a CHECK CONDITION status,
584 queue a request sense command. We must be careful,
585 though: we don't want the thread in
586 cdrom_queue_packet_command to wake up until
587 the request sense has completed. We do this
588 by transferring the semaphore from the packet
589 command request to the request sense request. */
591 if ((stat
& ERR_STAT
) != 0) {
597 cdrom_end_request (1, drive
);
599 if ((stat
& ERR_STAT
) != 0)
600 cdrom_queue_request_sense (drive
, sem
,
603 /* Handle errors from READ requests. */
605 if (sense_key
== NOT_READY
) {
607 cdrom_saw_media_change (drive
);
609 /* Fail the request. */
610 printk ("%s: tray open\n", drive
->name
);
611 cdrom_end_request (0, drive
);
612 } else if (sense_key
== UNIT_ATTENTION
) {
614 cdrom_saw_media_change (drive
);
616 /* Arrange to retry the request.
617 But be sure to give up if we've retried
619 if (++rq
->errors
> ERROR_MAX
)
620 cdrom_end_request (0, drive
);
621 } else if (sense_key
== ILLEGAL_REQUEST
||
622 sense_key
== DATA_PROTECT
) {
623 /* No point in retrying after an illegal
624 request or data protect error.*/
625 ide_dump_status (drive
, "command error", stat
);
626 cdrom_end_request (0, drive
);
627 } else if ((err
& ~ABRT_ERR
) != 0) {
628 /* Go to the default handler
630 ide_error (drive
, "cdrom_decode_status", stat
);
632 } else if ((++rq
->errors
> ERROR_MAX
)) {
633 /* We've racked up too many retries. Abort. */
634 cdrom_end_request (0, drive
);
637 /* If we got a CHECK_CONDITION status,
638 queue a request sense command. */
639 if ((stat
& ERR_STAT
) != 0)
640 cdrom_queue_request_sense (drive
,
645 /* Retry, or handle the next request. */
650 /* Set up the device registers for transferring a packet command on DEV,
651 expecting to later transfer XFERLEN bytes. HANDLER is the routine
652 which actually transfers the command to the drive. If this is a
653 drq_interrupt device, this routine will arrange for HANDLER to be
654 called when the interrupt from the drive arrives. Otherwise, HANDLER
655 will be called immediately after the drive is prepared for the transfer. */
657 static int cdrom_start_packet_command (ide_drive_t
*drive
, int xferlen
,
658 ide_handler_t
*handler
)
660 struct cdrom_info
*info
= drive
->driver_data
;
662 /* Wait for the controller to be idle. */
663 if (ide_wait_stat (drive
, 0, BUSY_STAT
, WAIT_READY
)) return 1;
666 info
->dma
= !HWIF(drive
)->dmaproc(ide_dma_read
, drive
);
668 /* Set up the controller registers. */
669 OUT_BYTE (info
->dma
, IDE_FEATURE_REG
);
670 OUT_BYTE (0, IDE_NSECTOR_REG
);
671 OUT_BYTE (0, IDE_SECTOR_REG
);
673 OUT_BYTE (xferlen
& 0xff, IDE_LCYL_REG
);
674 OUT_BYTE (xferlen
>> 8 , IDE_HCYL_REG
);
676 OUT_BYTE (drive
->ctl
, IDE_CONTROL_REG
);
679 (void) (HWIF(drive
)->dmaproc(ide_dma_begin
, drive
));
681 if (CDROM_CONFIG_FLAGS (drive
)->drq_interrupt
) {
682 ide_set_handler (drive
, handler
, WAIT_CMD
);
683 OUT_BYTE (WIN_PACKETCMD
, IDE_COMMAND_REG
); /* packet command */
685 OUT_BYTE (WIN_PACKETCMD
, IDE_COMMAND_REG
); /* packet command */
693 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
694 The device registers must have already been prepared
695 by cdrom_start_packet_command.
696 HANDLER is the interrupt handler to call when the command completes
697 or there's data ready. */
698 static int cdrom_transfer_packet_command (ide_drive_t
*drive
,
699 char *cmd_buf
, int cmd_len
,
700 ide_handler_t
*handler
)
702 if (CDROM_CONFIG_FLAGS (drive
)->drq_interrupt
) {
703 /* Here we should have been called after receiving an interrupt
704 from the device. DRQ should how be set. */
707 /* Check for errors. */
708 if (cdrom_decode_status (drive
, DRQ_STAT
, &stat_dum
))
711 /* Otherwise, we must wait for DRQ to get set. */
712 if (ide_wait_stat (drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
))
716 /* Arm the interrupt handler. */
717 ide_set_handler (drive
, handler
, WAIT_CMD
);
719 /* Send the command to the device. */
720 atapi_output_bytes (drive
, cmd_buf
, cmd_len
);
727 /****************************************************************************
728 * Block read functions.
732 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
733 * buffer. Once the first sector is added, any subsequent sectors are
734 * assumed to be continuous (until the buffer is cleared). For the first
735 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
736 * the buffer is cleared.)
738 static void cdrom_buffer_sectors (ide_drive_t
*drive
, unsigned long sector
,
739 int sectors_to_transfer
)
741 struct cdrom_info
*info
= drive
->driver_data
;
743 /* Number of sectors to read into the buffer. */
744 int sectors_to_buffer
= MIN (sectors_to_transfer
,
745 (SECTOR_BUFFER_SIZE
>> SECTOR_BITS
) -
746 info
->nsectors_buffered
);
750 /* If we don't yet have a sector buffer, try to allocate one.
751 If we can't get one atomically, it's not fatal -- we'll just throw
752 the data away rather than caching it. */
753 if (info
->sector_buffer
== NULL
) {
754 info
->sector_buffer
= (char *) kmalloc (SECTOR_BUFFER_SIZE
,
757 /* If we couldn't get a buffer,
758 don't try to buffer anything... */
759 if (info
->sector_buffer
== NULL
)
760 sectors_to_buffer
= 0;
763 /* If this is the first sector in the buffer, remember its number. */
764 if (info
->nsectors_buffered
== 0)
765 info
->sector_buffered
= sector
;
767 /* Read the data into the buffer. */
768 dest
= info
->sector_buffer
+ info
->nsectors_buffered
* SECTOR_SIZE
;
769 while (sectors_to_buffer
> 0) {
770 atapi_input_bytes (drive
, dest
, SECTOR_SIZE
);
772 --sectors_to_transfer
;
773 ++info
->nsectors_buffered
;
777 /* Throw away any remaining data. */
778 while (sectors_to_transfer
> 0) {
779 char dum
[SECTOR_SIZE
];
780 atapi_input_bytes (drive
, dum
, sizeof (dum
));
781 --sectors_to_transfer
;
787 * Check the contents of the interrupt reason register from the cdrom
788 * and attempt to recover if there are problems. Returns 0 if everything's
789 * ok; nonzero if the request has been terminated.
792 int cdrom_read_check_ireason (ide_drive_t
*drive
, int len
, int ireason
)
795 if (ireason
== 2) return 0;
798 /* Whoops... The drive is expecting to receive data from us! */
799 printk ("%s: cdrom_read_intr: "
800 "Drive wants to transfer data the wrong way!\n",
803 /* Throw some data at the drive so it doesn't hang
804 and quit this request. */
807 atapi_output_bytes (drive
, &dum
, sizeof (dum
));
811 /* Drive wants a command packet, or invalid ireason... */
812 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
813 drive
->name
, ireason
);
816 cdrom_end_request (0, drive
);
822 * Interrupt routine. Called when a read request has completed.
824 static void cdrom_read_intr (ide_drive_t
*drive
)
827 int ireason
, len
, sectors_to_transfer
, nskip
;
828 struct cdrom_info
*info
= drive
->driver_data
;
829 int i
, dma
= info
->dma
, dma_error
= 0;
831 struct request
*rq
= HWGROUP(drive
)->rq
;
833 /* Check for errors. */
836 if ((dma_error
= HWIF(drive
)->dmaproc(ide_dma_end
, drive
)))
837 HWIF(drive
)->dmaproc(ide_dma_off
, drive
);
840 if (cdrom_decode_status (drive
, 0, &stat
))
845 for (i
= rq
->nr_sectors
; i
> 0;) {
846 i
-= rq
->current_nr_sectors
;
847 ide_end_request(1, HWGROUP(drive
));
850 ide_error (drive
, "dma error", stat
);
854 /* Read the interrupt reason and the transfer length. */
855 ireason
= IN_BYTE (IDE_NSECTOR_REG
);
856 len
= IN_BYTE (IDE_LCYL_REG
) + 256 * IN_BYTE (IDE_HCYL_REG
);
858 /* If DRQ is clear, the command has completed. */
859 if ((stat
& DRQ_STAT
) == 0) {
860 /* If we're not done filling the current buffer, complain.
861 Otherwise, complete the command normally. */
862 if (rq
->current_nr_sectors
> 0) {
863 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
864 drive
->name
, rq
->current_nr_sectors
);
865 cdrom_end_request (0, drive
);
867 cdrom_end_request (1, drive
);
871 /* Check that the drive is expecting to do the same thing we are. */
872 if (cdrom_read_check_ireason (drive
, len
, ireason
)) return;
874 /* Assume that the drive will always provide data in multiples
875 of at least SECTOR_SIZE, as it gets hairy to keep track
876 of the transfers otherwise. */
877 if ((len
% SECTOR_SIZE
) != 0) {
878 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
880 if (CDROM_CONFIG_FLAGS (drive
)->limit_nframes
)
881 printk (" This drive is not supported by this version of the driver\n");
883 printk (" Trying to limit transfer sizes\n");
884 CDROM_CONFIG_FLAGS (drive
)->limit_nframes
= 1;
886 cdrom_end_request (0, drive
);
890 /* The number of sectors we need to read from the drive. */
891 sectors_to_transfer
= len
/ SECTOR_SIZE
;
893 /* First, figure out if we need to bit-bucket
894 any of the leading sectors. */
895 nskip
= MIN ((int)(rq
->current_nr_sectors
-
896 (rq
->bh
->b_size
>> SECTOR_BITS
)),
897 sectors_to_transfer
);
900 /* We need to throw away a sector. */
901 char dum
[SECTOR_SIZE
];
902 atapi_input_bytes (drive
, dum
, sizeof (dum
));
904 --rq
->current_nr_sectors
;
906 --sectors_to_transfer
;
909 /* Now loop while we still have data to read from the drive. */
910 while (sectors_to_transfer
> 0) {
913 /* If we've filled the present buffer but there's another
914 chained buffer after it, move on. */
915 if (rq
->current_nr_sectors
== 0 &&
917 cdrom_end_request (1, drive
);
919 /* If the buffers are full, cache the rest of the data in our
921 if (rq
->current_nr_sectors
== 0) {
922 cdrom_buffer_sectors (drive
,
923 rq
->sector
, sectors_to_transfer
);
924 sectors_to_transfer
= 0;
926 /* Transfer data to the buffers.
927 Figure out how many sectors we can transfer
928 to the current buffer. */
929 this_transfer
= MIN (sectors_to_transfer
,
930 rq
->current_nr_sectors
);
932 /* Read this_transfer sectors
933 into the current buffer. */
934 while (this_transfer
> 0) {
935 atapi_input_bytes (drive
,
936 rq
->buffer
, SECTOR_SIZE
);
937 rq
->buffer
+= SECTOR_SIZE
;
939 --rq
->current_nr_sectors
;
942 --sectors_to_transfer
;
948 Wait for another interrupt. */
949 ide_set_handler (drive
, &cdrom_read_intr
, WAIT_CMD
);
954 * Try to satisfy some of the current read request from our cached data.
955 * Returns nonzero if the request has been completed, zero otherwise.
957 static int cdrom_read_from_buffer (ide_drive_t
*drive
)
959 struct cdrom_info
*info
= drive
->driver_data
;
960 struct request
*rq
= HWGROUP(drive
)->rq
;
962 /* Can't do anything if there's no buffer. */
963 if (info
->sector_buffer
== NULL
) return 0;
965 /* Loop while this request needs data and the next block is present
967 while (rq
->nr_sectors
> 0 &&
968 rq
->sector
>= info
->sector_buffered
&&
969 rq
->sector
< info
->sector_buffered
+ info
->nsectors_buffered
) {
970 if (rq
->current_nr_sectors
== 0)
971 cdrom_end_request (1, drive
);
974 info
->sector_buffer
+
975 (rq
->sector
- info
->sector_buffered
) * SECTOR_SIZE
,
977 rq
->buffer
+= SECTOR_SIZE
;
978 --rq
->current_nr_sectors
;
983 /* If we've satisfied the current request,
984 terminate it successfully. */
985 if (rq
->nr_sectors
== 0) {
986 cdrom_end_request (1, drive
);
990 /* Move on to the next buffer if needed. */
991 if (rq
->current_nr_sectors
== 0)
992 cdrom_end_request (1, drive
);
994 /* If this condition does not hold, then the kluge i use to
995 represent the number of sectors to skip at the start of a transfer
996 will fail. I think that this will never happen, but let's be
997 paranoid and check. */
998 if (rq
->current_nr_sectors
< (rq
->bh
->b_size
>> SECTOR_BITS
) &&
999 (rq
->sector
% SECTORS_PER_FRAME
) != 0) {
1000 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1001 drive
->name
, rq
->sector
);
1002 cdrom_end_request (0, drive
);
1012 * Routine to send a read packet command to the drive.
1013 * This is usually called directly from cdrom_start_read.
1014 * However, for drq_interrupt devices, it is called from an interrupt
1015 * when the drive is ready to accept the command.
1017 static void cdrom_start_read_continuation (ide_drive_t
*drive
)
1019 struct packet_command pc
;
1020 struct request
*rq
= HWGROUP(drive
)->rq
;
1021 int nsect
, sector
, nframes
, frame
, nskip
;
1023 /* Number of sectors to transfer. */
1024 nsect
= rq
->nr_sectors
;
1026 /* Starting sector. */
1027 sector
= rq
->sector
;
1029 /* If the requested sector doesn't start on a cdrom block boundary,
1030 we must adjust the start of the transfer so that it does,
1031 and remember to skip the first few sectors.
1032 If the CURRENT_NR_SECTORS field is larger than the size
1033 of the buffer, it will mean that we're to skip a number
1034 of sectors equal to the amount by which CURRENT_NR_SECTORS
1035 is larger than the buffer size. */
1036 nskip
= (sector
% SECTORS_PER_FRAME
);
1038 /* Sanity check... */
1039 if (rq
->current_nr_sectors
!=
1040 (rq
->bh
->b_size
>> SECTOR_BITS
)) {
1041 printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1042 drive
->name
, rq
->current_nr_sectors
);
1043 cdrom_end_request (0, drive
);
1049 rq
->current_nr_sectors
+= nskip
;
1052 /* Convert from sectors to cdrom blocks, rounding up the transfer
1053 length if needed. */
1054 nframes
= (nsect
+ SECTORS_PER_FRAME
-1) / SECTORS_PER_FRAME
;
1055 frame
= sector
/ SECTORS_PER_FRAME
;
1057 /* Largest number of frames was can transfer at once is 64k-1. For
1058 some drives we need to limit this even more. */
1059 nframes
= MIN (nframes
, (CDROM_CONFIG_FLAGS (drive
)->limit_nframes
) ?
1060 (65534 / CD_FRAMESIZE
) : 65535);
1062 /* Set up the command */
1063 memset (&pc
.c
, 0, sizeof (pc
.c
));
1065 pc
.c
[7] = (nframes
>> 8);
1066 pc
.c
[8] = (nframes
& 0xff);
1067 put_unaligned(htonl (frame
), (unsigned int *) &pc
.c
[2]);
1069 /* Send the command to the drive and return. */
1070 (void) cdrom_transfer_packet_command (drive
, pc
.c
, sizeof (pc
.c
),
1074 #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
1075 #define IDECD_SEEK_TIMER (2 * WAIT_MIN_SLEEP) /* 40 ms */
1076 #define IDECD_SEEK_TIMEOUT WAIT_CMD /* 10 sec */
1078 static void cdrom_seek_intr (ide_drive_t
*drive
)
1080 struct cdrom_info
*info
= drive
->driver_data
;
1082 static int retry
= 10;
1084 if (cdrom_decode_status (drive
, 0, &stat
))
1086 CDROM_CONFIG_FLAGS(drive
)->seeking
= 1;
1088 if (retry
&& jiffies
- info
->start_seek
> IDECD_SEEK_TIMER
) {
1090 printk ("%s: disabled DSC seek overlap\n", drive
->name
);
1091 drive
->dsc_overlap
= 0;
1096 static void cdrom_start_seek_continuation (ide_drive_t
*drive
)
1098 struct packet_command pc
;
1099 struct request
*rq
= HWGROUP(drive
)->rq
;
1100 int sector
, frame
, nskip
;
1102 sector
= rq
->sector
;
1103 nskip
= (sector
% SECTORS_PER_FRAME
);
1106 frame
= sector
/ SECTORS_PER_FRAME
;
1108 memset (&pc
.c
, 0, sizeof (pc
.c
));
1110 put_unaligned(htonl (frame
), (unsigned int *) &pc
.c
[2]);
1111 (void) cdrom_transfer_packet_command (drive
, pc
.c
, sizeof (pc
.c
), &cdrom_seek_intr
);
1114 static void cdrom_start_seek (ide_drive_t
*drive
, unsigned int block
)
1116 struct cdrom_info
*info
= drive
->driver_data
;
1119 info
->start_seek
= jiffies
;
1120 cdrom_start_packet_command (drive
, 0, cdrom_start_seek_continuation
);
1124 * Start a read request from the CD-ROM.
1126 static void cdrom_start_read (ide_drive_t
*drive
, unsigned int block
)
1128 struct cdrom_info
*info
= drive
->driver_data
;
1129 struct request
*rq
= HWGROUP(drive
)->rq
;
1130 int minor
= MINOR (rq
->rq_dev
);
1132 /* If the request is relative to a partition, fix it up to refer to the
1133 absolute address. */
1134 if ((minor
& PARTN_MASK
) != 0) {
1136 minor
&= ~PARTN_MASK
;
1137 rq
->rq_dev
= MKDEV (MAJOR(rq
->rq_dev
), minor
);
1140 /* We may be retrying this request after an error. Fix up
1141 any weirdness which might be present in the request packet. */
1142 restore_request (rq
);
1144 /* Satisfy whatever we can of this request from our cached sector. */
1145 if (cdrom_read_from_buffer (drive
))
1148 /* Clear the local sector buffer. */
1149 info
->nsectors_buffered
= 0;
1151 if (drive
->using_dma
&& (rq
->sector
% SECTORS_PER_FRAME
== 0) && (rq
->nr_sectors
% SECTORS_PER_FRAME
== 0))
1156 /* Start sending the read request to the drive. */
1157 cdrom_start_packet_command (drive
, 32768,
1158 cdrom_start_read_continuation
);
1164 /****************************************************************************
1165 * Execute all other packet commands.
1168 /* Forward declarations. */
1170 cdrom_lockdoor (ide_drive_t
*drive
, int lockflag
,
1171 struct atapi_request_sense
*reqbuf
);
1175 /* Interrupt routine for packet command completion. */
1176 static void cdrom_pc_intr (ide_drive_t
*drive
)
1178 int ireason
, len
, stat
, thislen
;
1179 struct request
*rq
= HWGROUP(drive
)->rq
;
1180 struct packet_command
*pc
= (struct packet_command
*)rq
->buffer
;
1182 /* Check for errors. */
1183 if (cdrom_decode_status (drive
, 0, &stat
))
1186 /* Read the interrupt reason and the transfer length. */
1187 ireason
= IN_BYTE (IDE_NSECTOR_REG
);
1188 len
= IN_BYTE (IDE_LCYL_REG
) + 256 * IN_BYTE (IDE_HCYL_REG
);
1190 /* If DRQ is clear, the command has completed.
1191 Complain if we still have data left to transfer. */
1192 if ((stat
& DRQ_STAT
) == 0) {
1193 /* Some of the trailing request sense fields are optional, and
1194 some drives don't send them. Sigh. */
1195 if (pc
->c
[0] == REQUEST_SENSE
&&
1198 while (pc
->buflen
> 0) {
1204 if (pc
->buflen
== 0)
1205 cdrom_end_request (1, drive
);
1207 /* Comment this out, because this always happens
1208 right after a reset occurs, and it is annoying to
1209 always print expected stuff. */
1211 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1212 drive->name, pc->buflen);
1215 cdrom_end_request (1, drive
);
1220 /* Figure out how much data to transfer. */
1221 thislen
= pc
->buflen
;
1222 if (thislen
< 0) thislen
= -thislen
;
1223 if (thislen
> len
) thislen
= len
;
1225 /* The drive wants to be written to. */
1226 if ((ireason
& 3) == 0) {
1227 /* Check that we want to write. */
1228 if (pc
->buflen
> 0) {
1229 printk ("%s: cdrom_pc_intr: Drive wants "
1230 "to transfer data the wrong way!\n",
1236 /* Transfer the data. */
1237 atapi_output_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
) {
1243 atapi_output_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
;
1252 /* Same drill for reading. */
1253 else if ((ireason
& 3) == 2) {
1254 /* Check that we want to read. */
1255 if (pc
->buflen
< 0) {
1256 printk ("%s: cdrom_pc_intr: Drive wants to "
1257 "transfer data the wrong way!\n",
1263 /* Transfer the data. */
1264 atapi_input_bytes (drive
, pc
->buffer
, thislen
);
1266 /* If we haven't moved enough data to satisfy the drive,
1267 add some padding. */
1268 while (len
> thislen
) {
1270 atapi_input_bytes (drive
, &dum
, sizeof (dum
));
1271 len
-= sizeof (dum
);
1274 /* Keep count of how much data we've moved. */
1275 pc
->buffer
+= thislen
;
1276 pc
->buflen
-= thislen
;
1278 printk ("%s: cdrom_pc_intr: The drive "
1279 "appears confused (ireason = 0x%2x)\n",
1280 drive
->name
, ireason
);
1284 /* Now we wait for another interrupt. */
1285 ide_set_handler (drive
, &cdrom_pc_intr
, WAIT_CMD
);
1289 static void cdrom_do_pc_continuation (ide_drive_t
*drive
)
1291 struct request
*rq
= HWGROUP(drive
)->rq
;
1292 struct packet_command
*pc
= (struct packet_command
*)rq
->buffer
;
1294 /* Send the command to the drive and return. */
1295 cdrom_transfer_packet_command (drive
, pc
->c
,
1296 sizeof (pc
->c
), &cdrom_pc_intr
);
1300 static void cdrom_do_packet_command (ide_drive_t
*drive
)
1303 struct request
*rq
= HWGROUP(drive
)->rq
;
1304 struct packet_command
*pc
= (struct packet_command
*)rq
->buffer
;
1305 struct cdrom_info
*info
= drive
->driver_data
;
1310 if (len
< 0) len
= -len
;
1314 /* Start sending the command to the drive. */
1315 cdrom_start_packet_command (drive
, len
, cdrom_do_pc_continuation
);
1319 /* Sleep for TIME jiffies.
1320 Not to be called from an interrupt handler. */
1322 void cdrom_sleep (int time
)
1324 current
->state
= TASK_INTERRUPTIBLE
;
1325 schedule_timeout(time
);
1329 int cdrom_queue_packet_command (ide_drive_t
*drive
, struct packet_command
*pc
)
1331 struct atapi_request_sense my_reqbuf
;
1335 /* If our caller has not provided a place to stick any sense data,
1336 use our own area. */
1337 if (pc
->sense_data
== NULL
)
1338 pc
->sense_data
= &my_reqbuf
;
1339 pc
->sense_data
->sense_key
= 0;
1341 /* Start of retry loop. */
1343 ide_init_drive_cmd (&req
);
1344 req
.cmd
= PACKET_COMMAND
;
1345 req
.buffer
= (char *)pc
;
1346 if (ide_do_drive_cmd (drive
, &req
, ide_wait
)) {
1347 printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
1348 drive
->name
, req
.buffer
[0], req
.buffer
[1]);
1349 /* FIXME: we should probably abort/retry or something */
1351 if (pc
->stat
!= 0) {
1352 /* The request failed. Retry if it was due to a unit
1354 (usually means media was changed). */
1355 struct atapi_request_sense
*reqbuf
= pc
->sense_data
;
1357 if (reqbuf
->sense_key
== UNIT_ATTENTION
)
1358 cdrom_saw_media_change (drive
);
1359 else if (reqbuf
->sense_key
== NOT_READY
&&
1361 /* The drive is in the process of loading
1362 a disk. Retry, but wait a little to give
1363 the drive time to complete the load. */
1366 /* Otherwise, don't retry. */
1372 /* End of retry loop. */
1373 } while (pc
->stat
!= 0 && retries
>= 0);
1376 /* Return an error if the command failed. */
1380 /* The command succeeded. If it was anything other than
1381 a request sense, eject, or door lock command,
1382 and we think that the door is presently, lock it again.
1383 (The door was probably unlocked via an explicit
1384 CDROMEJECT ioctl.) */
1385 if (CDROM_STATE_FLAGS (drive
)->door_locked
== 0 && drive
->usage
&&
1386 (pc
->c
[0] != REQUEST_SENSE
&&
1387 pc
->c
[0] != ALLOW_MEDIUM_REMOVAL
&&
1388 pc
->c
[0] != START_STOP
)) {
1389 (void) cdrom_lockdoor (drive
, 1, NULL
);
1396 /****************************************************************************
1397 * cdrom driver request routine.
1400 void ide_do_rw_cdrom (ide_drive_t
*drive
, struct request
*rq
, unsigned long block
)
1402 if (rq
-> cmd
== PACKET_COMMAND
|| rq
-> cmd
== REQUEST_SENSE_COMMAND
)
1403 cdrom_do_packet_command (drive
);
1404 else if (rq
-> cmd
== RESET_DRIVE_COMMAND
) {
1405 cdrom_end_request (1, drive
);
1406 ide_do_reset (drive
);
1408 } else if (rq
-> cmd
!= READ
) {
1409 printk ("ide-cd: bad cmd %d\n", rq
-> cmd
);
1410 cdrom_end_request (0, drive
);
1412 struct cdrom_info
*info
= drive
->driver_data
;
1414 if (CDROM_CONFIG_FLAGS(drive
)->seeking
) {
1415 unsigned long elpased
= jiffies
- info
->start_seek
;
1416 int stat
= GET_STAT();
1418 if ((stat
& SEEK_STAT
) != SEEK_STAT
) {
1419 if (elpased
< IDECD_SEEK_TIMEOUT
) {
1420 ide_stall_queue (drive
, IDECD_SEEK_TIMER
);
1423 printk ("%s: DSC timeout\n", drive
->name
);
1425 CDROM_CONFIG_FLAGS(drive
)->seeking
= 0;
1427 if (IDE_LARGE_SEEK(info
->last_block
, block
, IDECD_SEEK_THRESHOLD
) && drive
->dsc_overlap
)
1428 cdrom_start_seek (drive
, block
);
1430 cdrom_start_read (drive
, block
);
1431 info
->last_block
= block
;
1437 /****************************************************************************
1440 * Routines which queue packet commands take as a final argument a pointer
1441 * to an atapi_request_sense struct. If execution of the command results
1442 * in an error with a CHECK CONDITION status, this structure will be filled
1443 * with the results of the subsequent request sense command. The pointer
1444 * can also be NULL, in which case no sense information is returned.
1447 #if ! STANDARD_ATAPI
1451 return (x
%10) | ((x
/10) << 4);
1458 return (x
>> 4) * 10 + (x
& 0x0f);
1462 void msf_from_bcd (struct atapi_msf
*msf
)
1464 msf
->minute
= bcd2bin (msf
->minute
);
1465 msf
->second
= bcd2bin (msf
->second
);
1466 msf
->frame
= bcd2bin (msf
->frame
);
1469 #endif /* not STANDARD_ATAPI */
1473 void lba_to_msf (int lba
, byte
*m
, byte
*s
, byte
*f
)
1475 lba
+= CD_MSF_OFFSET
;
1476 lba
&= 0xffffff; /* negative lbas use only 24 bits */
1477 *m
= lba
/ (CD_SECS
* CD_FRAMES
);
1478 lba
%= (CD_SECS
* CD_FRAMES
);
1479 *s
= lba
/ CD_FRAMES
;
1480 *f
= lba
% CD_FRAMES
;
1485 int msf_to_lba (byte m
, byte s
, byte f
)
1487 return (((m
* CD_SECS
) + s
) * CD_FRAMES
+ f
) - CD_MSF_OFFSET
;
1492 cdrom_check_status (ide_drive_t
*drive
,
1493 struct atapi_request_sense
*reqbuf
)
1495 struct packet_command pc
;
1497 memset (&pc
, 0, sizeof (pc
));
1499 pc
.sense_data
= reqbuf
;
1500 pc
.c
[0] = TEST_UNIT_READY
;
1502 #if ! STANDARD_ATAPI
1503 /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1504 switch CDs instead of supporting the LOAD_UNLOAD opcode */
1506 pc
.c
[7] = CDROM_STATE_FLAGS (drive
)->sanyo_slot
% 3;
1507 #endif /* not STANDARD_ATAPI */
1509 return cdrom_queue_packet_command (drive
, &pc
);
1513 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1515 cdrom_lockdoor (ide_drive_t
*drive
, int lockflag
,
1516 struct atapi_request_sense
*reqbuf
)
1518 struct atapi_request_sense my_reqbuf
;
1520 struct packet_command pc
;
1523 reqbuf
= &my_reqbuf
;
1525 /* If the drive cannot lock the door, just pretend. */
1526 if (CDROM_CONFIG_FLAGS (drive
)->no_doorlock
)
1529 memset (&pc
, 0, sizeof (pc
));
1530 pc
.sense_data
= reqbuf
;
1532 pc
.c
[0] = ALLOW_MEDIUM_REMOVAL
;
1533 pc
.c
[4] = (lockflag
!= 0);
1534 stat
= cdrom_queue_packet_command (drive
, &pc
);
1537 /* If we got an illegal field error, the drive
1538 probably cannot lock the door. */
1540 reqbuf
->sense_key
== ILLEGAL_REQUEST
&&
1541 (reqbuf
->asc
== 0x24 || reqbuf
->asc
== 0x20)) {
1542 printk ("%s: door locking not supported\n",
1544 CDROM_CONFIG_FLAGS (drive
)->no_doorlock
= 1;
1548 /* no medium, that's alright. */
1549 if (stat
!= 0 && reqbuf
->sense_key
== NOT_READY
&& reqbuf
->asc
== 0x3a)
1553 CDROM_STATE_FLAGS (drive
)->door_locked
= lockflag
;
1559 /* Eject the disk if EJECTFLAG is 0.
1560 If EJECTFLAG is 1, try to reload the disk. */
1562 cdrom_eject (ide_drive_t
*drive
, int ejectflag
,
1563 struct atapi_request_sense
*reqbuf
)
1565 struct packet_command pc
;
1567 if (CDROM_CONFIG_FLAGS (drive
)->no_eject
==1 && ejectflag
==0)
1568 return -EDRIVE_CANT_DO_THIS
;
1570 memset (&pc
, 0, sizeof (pc
));
1571 pc
.sense_data
= reqbuf
;
1573 pc
.c
[0] = START_STOP
;
1574 pc
.c
[4] = 2 + (ejectflag
!= 0);
1575 return cdrom_queue_packet_command (drive
, &pc
);
1580 cdrom_pause (ide_drive_t
*drive
, int pauseflag
,
1581 struct atapi_request_sense
*reqbuf
)
1583 struct packet_command pc
;
1585 memset (&pc
, 0, sizeof (pc
));
1586 pc
.sense_data
= reqbuf
;
1588 pc
.c
[0] = SCMD_PAUSE_RESUME
;
1589 pc
.c
[8] = !pauseflag
;
1590 return cdrom_queue_packet_command (drive
, &pc
);
1595 cdrom_startstop (ide_drive_t
*drive
, int startflag
,
1596 struct atapi_request_sense
*reqbuf
)
1598 struct packet_command pc
;
1600 memset (&pc
, 0, sizeof (pc
));
1601 pc
.sense_data
= reqbuf
;
1603 pc
.c
[0] = START_STOP
;
1605 pc
.c
[4] = startflag
;
1606 return cdrom_queue_packet_command (drive
, &pc
);
1610 cdrom_read_capacity (ide_drive_t
*drive
, unsigned *capacity
,
1611 struct atapi_request_sense
*reqbuf
)
1619 struct packet_command pc
;
1621 memset (&pc
, 0, sizeof (pc
));
1622 pc
.sense_data
= reqbuf
;
1624 pc
.c
[0] = READ_CAPACITY
;
1625 pc
.buffer
= (char *)&capbuf
;
1626 pc
.buflen
= sizeof (capbuf
);
1628 stat
= cdrom_queue_packet_command (drive
, &pc
);
1630 *capacity
= ntohl (capbuf
.lba
);
1637 cdrom_read_tocentry (ide_drive_t
*drive
, int trackno
, int msf_flag
,
1638 int format
, char *buf
, int buflen
,
1639 struct atapi_request_sense
*reqbuf
)
1641 struct packet_command pc
;
1643 memset (&pc
, 0, sizeof (pc
));
1644 pc
.sense_data
= reqbuf
;
1648 pc
.c
[0] = SCMD_READ_TOC
;
1650 pc
.c
[7] = (buflen
>> 8);
1651 pc
.c
[8] = (buflen
& 0xff);
1652 pc
.c
[9] = (format
<< 6);
1653 if (msf_flag
) pc
.c
[1] = 2;
1654 return cdrom_queue_packet_command (drive
, &pc
);
1658 /* Try to read the entire TOC for the disk into our internal buffer. */
1660 cdrom_read_toc (ide_drive_t
*drive
,
1661 struct atapi_request_sense
*reqbuf
)
1663 int stat
, ntracks
, i
;
1664 struct cdrom_info
*info
= drive
->driver_data
;
1665 struct atapi_toc
*toc
= info
->toc
;
1667 struct atapi_toc_header hdr
;
1668 struct atapi_toc_entry ent
;
1672 /* Try to allocate space. */
1673 toc
= (struct atapi_toc
*) kmalloc (sizeof (struct atapi_toc
),
1679 printk ("%s: No cdrom TOC buffer!\n", drive
->name
);
1683 /* Check to see if the existing data is still valid.
1684 If it is, just return. */
1685 if (CDROM_STATE_FLAGS (drive
)->toc_valid
)
1686 (void) cdrom_check_status (drive
, NULL
);
1688 if (CDROM_STATE_FLAGS (drive
)->toc_valid
) return 0;
1690 /* First read just the header, so we know how long the TOC is. */
1691 stat
= cdrom_read_tocentry (drive
, 0, 1, 0, (char *)&toc
->hdr
,
1692 sizeof (struct atapi_toc_header
) +
1693 sizeof (struct atapi_toc_entry
),
1695 if (stat
) return stat
;
1697 #if ! STANDARD_ATAPI
1698 if (CDROM_CONFIG_FLAGS (drive
)->toctracks_as_bcd
) {
1699 toc
->hdr
.first_track
= bcd2bin (toc
->hdr
.first_track
);
1700 toc
->hdr
.last_track
= bcd2bin (toc
->hdr
.last_track
);
1702 #endif /* not STANDARD_ATAPI */
1704 ntracks
= toc
->hdr
.last_track
- toc
->hdr
.first_track
+ 1;
1705 if (ntracks
<= 0) return -EIO
;
1706 if (ntracks
> MAX_TRACKS
) ntracks
= MAX_TRACKS
;
1708 /* Now read the whole schmeer. */
1709 stat
= cdrom_read_tocentry (drive
, 0, 1, 0, (char *)&toc
->hdr
,
1710 sizeof (struct atapi_toc_header
) +
1712 sizeof (struct atapi_toc_entry
),
1714 if (stat
) return stat
;
1715 toc
->hdr
.toc_length
= ntohs (toc
->hdr
.toc_length
);
1717 #if ! STANDARD_ATAPI
1718 if (CDROM_CONFIG_FLAGS (drive
)->toctracks_as_bcd
) {
1719 toc
->hdr
.first_track
= bcd2bin (toc
->hdr
.first_track
);
1720 toc
->hdr
.last_track
= bcd2bin (toc
->hdr
.last_track
);
1722 #endif /* not STANDARD_ATAPI */
1724 for (i
=0; i
<=ntracks
; i
++) {
1725 #if ! STANDARD_ATAPI
1726 if (CDROM_CONFIG_FLAGS (drive
)->tocaddr_as_bcd
) {
1727 if (CDROM_CONFIG_FLAGS (drive
)->toctracks_as_bcd
)
1728 toc
->ent
[i
].track
= bcd2bin (toc
->ent
[i
].track
);
1729 msf_from_bcd (&toc
->ent
[i
].addr
.msf
);
1731 #endif /* not STANDARD_ATAPI */
1732 toc
->ent
[i
].addr
.lba
= msf_to_lba (toc
->ent
[i
].addr
.msf
.minute
,
1733 toc
->ent
[i
].addr
.msf
.second
,
1734 toc
->ent
[i
].addr
.msf
.frame
);
1737 /* Read the multisession information. */
1738 stat
= cdrom_read_tocentry (drive
, 0, 1, 1,
1739 (char *)&ms_tmp
, sizeof (ms_tmp
),
1741 if (stat
) return stat
;
1743 #if ! STANDARD_ATAPI
1744 if (CDROM_CONFIG_FLAGS (drive
)->tocaddr_as_bcd
)
1745 msf_from_bcd (&ms_tmp
.ent
.addr
.msf
);
1746 #endif /* not STANDARD_ATAPI */
1748 toc
->last_session_lba
= msf_to_lba (ms_tmp
.ent
.addr
.msf
.minute
,
1749 ms_tmp
.ent
.addr
.msf
.second
,
1750 ms_tmp
.ent
.addr
.msf
.frame
);
1752 toc
->xa_flag
= (ms_tmp
.hdr
.first_track
!= ms_tmp
.hdr
.last_track
);
1754 /* Now try to get the total cdrom capacity. */
1755 stat
= cdrom_read_capacity (drive
, &toc
->capacity
, reqbuf
);
1756 if (stat
) toc
->capacity
= 0x1fffff;
1758 HWIF(drive
)->gd
->sizes
[drive
->select
.b
.unit
<< PARTN_BITS
]
1759 = (toc
->capacity
* SECTORS_PER_FRAME
) >> (BLOCK_SIZE_BITS
- 9);
1760 drive
->part
[0].nr_sects
= toc
->capacity
* SECTORS_PER_FRAME
;
1762 /* Remember that we've read this stuff. */
1763 CDROM_STATE_FLAGS (drive
)->toc_valid
= 1;
1770 cdrom_read_subchannel (ide_drive_t
*drive
, int format
,
1771 char *buf
, int buflen
,
1772 struct atapi_request_sense
*reqbuf
)
1774 struct packet_command pc
;
1776 memset (&pc
, 0, sizeof (pc
));
1777 pc
.sense_data
= reqbuf
;
1781 pc
.c
[0] = SCMD_READ_SUBCHANNEL
;
1782 pc
.c
[1] = 2; /* MSF addressing */
1783 pc
.c
[2] = 0x40; /* request subQ data */
1785 pc
.c
[7] = (buflen
>> 8);
1786 pc
.c
[8] = (buflen
& 0xff);
1787 return cdrom_queue_packet_command (drive
, &pc
);
1791 /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1793 cdrom_mode_sense (ide_drive_t
*drive
, int pageno
, int modeflag
,
1794 char *buf
, int buflen
,
1795 struct atapi_request_sense
*reqbuf
)
1797 struct packet_command pc
;
1799 memset (&pc
, 0, sizeof (pc
));
1800 pc
.sense_data
= reqbuf
;
1804 pc
.c
[0] = MODE_SENSE_10
;
1805 pc
.c
[2] = pageno
| (modeflag
<< 6);
1806 pc
.c
[7] = (buflen
>> 8);
1807 pc
.c
[8] = (buflen
& 0xff);
1808 return cdrom_queue_packet_command (drive
, &pc
);
1812 cdrom_mode_select (ide_drive_t
*drive
, int pageno
, char *buf
, int buflen
,
1813 struct atapi_request_sense
*reqbuf
)
1815 struct packet_command pc
;
1817 memset (&pc
, 0, sizeof (pc
));
1818 pc
.sense_data
= reqbuf
;
1821 pc
.buflen
= - buflen
;
1822 pc
.c
[0] = MODE_SELECT_10
;
1825 pc
.c
[7] = (buflen
>> 8);
1826 pc
.c
[8] = (buflen
& 0xff);
1827 return cdrom_queue_packet_command (drive
, &pc
);
1830 /* ATAPI cdrom drives are free to select the speed you request or any slower
1831 rate :-( Requesting too fast a speed will _not_ produce an error. */
1833 cdrom_select_speed (ide_drive_t
*drive
, int speed
,
1834 struct atapi_request_sense
*reqbuf
)
1836 struct packet_command pc
;
1837 memset (&pc
, 0, sizeof (pc
));
1838 pc
.sense_data
= reqbuf
;
1841 speed
= 0xffff; /* set to max */
1843 speed
*= 177; /* Nx to kbytes/s */
1845 pc
.c
[0] = SET_CD_SPEED
;
1846 /* Read Drive speed in kbytes/second MSB */
1847 pc
.c
[2] = (speed
>> 8) & 0xff;
1848 /* Read Drive speed in kbytes/second LSB */
1849 pc
.c
[3] = speed
& 0xff;
1850 if ( CDROM_CONFIG_FLAGS(drive
)->cd_r
||
1851 CDROM_CONFIG_FLAGS(drive
)->cd_rw
) {
1852 /* Write Drive speed in kbytes/second MSB */
1853 pc
.c
[4] = (speed
>> 8) & 0xff;
1854 /* Write Drive speed in kbytes/second LSB */
1855 pc
.c
[5] = speed
& 0xff;
1858 return cdrom_queue_packet_command (drive
, &pc
);
1862 cdrom_play_lba_range_1 (ide_drive_t
*drive
, int lba_start
, int lba_end
,
1863 struct atapi_request_sense
*reqbuf
)
1865 struct packet_command pc
;
1867 memset (&pc
, 0, sizeof (pc
));
1868 pc
.sense_data
= reqbuf
;
1870 pc
.c
[0] = SCMD_PLAYAUDIO_MSF
;
1871 lba_to_msf (lba_start
, &pc
.c
[3], &pc
.c
[4], &pc
.c
[5]);
1872 lba_to_msf (lba_end
-1, &pc
.c
[6], &pc
.c
[7], &pc
.c
[8]);
1874 #if ! STANDARD_ATAPI
1875 if (CDROM_CONFIG_FLAGS (drive
)->playmsf_as_bcd
) {
1876 pc
.c
[3] = bin2bcd (pc
.c
[3]);
1877 pc
.c
[4] = bin2bcd (pc
.c
[4]);
1878 pc
.c
[5] = bin2bcd (pc
.c
[5]);
1879 pc
.c
[6] = bin2bcd (pc
.c
[6]);
1880 pc
.c
[7] = bin2bcd (pc
.c
[7]);
1881 pc
.c
[8] = bin2bcd (pc
.c
[8]);
1883 #endif /* not STANDARD_ATAPI */
1885 return cdrom_queue_packet_command (drive
, &pc
);
1889 /* Play audio starting at LBA LBA_START and finishing with the
1890 LBA before LBA_END. */
1892 cdrom_play_lba_range (ide_drive_t
*drive
, int lba_start
, int lba_end
,
1893 struct atapi_request_sense
*reqbuf
)
1896 struct atapi_request_sense my_reqbuf
;
1899 reqbuf
= &my_reqbuf
;
1901 /* Some drives, will, for certain audio cds,
1902 give an error if you ask them to play the entire cd using the
1903 values which are returned in the TOC. The play will succeed,
1904 however, if the ending address is adjusted downwards
1906 for (i
=0; i
<75; i
++) {
1907 stat
= cdrom_play_lba_range_1 (drive
, lba_start
, lba_end
,
1911 !(reqbuf
->sense_key
== ILLEGAL_REQUEST
&&
1912 reqbuf
->asc
== 0x24))
1916 if (lba_end
<= lba_start
) break;
1924 int cdrom_get_toc_entry (ide_drive_t
*drive
, int track
,
1925 struct atapi_toc_entry
**ent
,
1926 struct atapi_request_sense
*reqbuf
)
1928 struct cdrom_info
*info
= drive
->driver_data
;
1930 struct atapi_toc
*toc
;
1932 /* Make sure our saved TOC is valid. */
1933 stat
= cdrom_read_toc (drive
, reqbuf
);
1934 if (stat
) return stat
;
1938 /* Check validity of requested track number. */
1939 ntracks
= toc
->hdr
.last_track
- toc
->hdr
.first_track
+ 1;
1940 if (track
== CDROM_LEADOUT
)
1941 *ent
= &toc
->ent
[ntracks
];
1942 else if (track
< toc
->hdr
.first_track
||
1943 track
> toc
->hdr
.last_track
)
1946 *ent
= &toc
->ent
[track
- toc
->hdr
.first_track
];
1953 cdrom_read_block (ide_drive_t
*drive
, int format
, int lba
, int nblocks
,
1954 char *buf
, int buflen
,
1955 struct atapi_request_sense
*reqbuf
)
1957 struct packet_command pc
;
1958 struct atapi_request_sense my_reqbuf
;
1961 reqbuf
= &my_reqbuf
;
1963 memset (&pc
, 0, sizeof (pc
));
1964 pc
.sense_data
= reqbuf
;
1969 #if ! STANDARD_ATAPI
1970 if (CDROM_CONFIG_FLAGS (drive
)->nec260
)
1973 #endif /* not STANDARD_ATAPI */
1976 pc
.c
[1] = (format
<< 2);
1977 put_unaligned(htonl(lba
), (unsigned int *) &pc
.c
[2]);
1978 pc
.c
[8] = (nblocks
& 0xff);
1979 pc
.c
[7] = ((nblocks
>>8) & 0xff);
1980 pc
.c
[6] = ((nblocks
>>16) & 0xff);
1982 pc
.c
[9] = 0xf8; /* returns 2352 for any format */
1986 return cdrom_queue_packet_command (drive
, &pc
);
1990 /* If SLOT<0, unload the current slot. Otherwise, try to load SLOT. */
1992 cdrom_load_unload (ide_drive_t
*drive
, int slot
,
1993 struct atapi_request_sense
*reqbuf
)
1995 #if ! STANDARD_ATAPI
1996 /* if the drive is a Sanyo 3 CD changer then TEST_UNIT_READY
1997 (used in the cdrom_check_status function) is used to
1998 switch CDs instead of LOAD_UNLOAD */
2000 if (CDROM_STATE_FLAGS (drive
)->sanyo_slot
> 0) {
2002 if ((slot
== 1) || (slot
== 2))
2003 CDROM_STATE_FLAGS (drive
)->sanyo_slot
= slot
;
2005 CDROM_STATE_FLAGS (drive
)->sanyo_slot
= 3;
2009 return cdrom_check_status (drive
, reqbuf
);
2013 #endif /*not STANDARD_ATAPI */
2016 /* ATAPI Rev. 2.2+ standard for requesting switching of
2017 CDs in a multiplatter device */
2019 struct packet_command pc
;
2021 memset (&pc
, 0, sizeof (pc
));
2022 pc
.sense_data
= reqbuf
;
2024 pc
.c
[0] = LOAD_UNLOAD
;
2025 pc
.c
[4] = 2 + (slot
>= 0);
2027 return cdrom_queue_packet_command (drive
, &pc
);
2033 /* This gets the mechanism status per ATAPI draft spec 2.6 */
2035 cdrom_read_mech_status (ide_drive_t
*drive
, char *buf
, int buflen
,
2036 struct atapi_request_sense
*reqbuf
)
2038 struct packet_command pc
;
2040 memset (&pc
, 0, sizeof (pc
));
2041 pc
.sense_data
= reqbuf
;
2045 pc
.c
[0] = MECHANISM_STATUS
;
2046 pc
.c
[8] = (buflen
>> 8);
2047 pc
.c
[9] = (buflen
& 0xff);
2048 return cdrom_queue_packet_command (drive
, &pc
);
2052 /* Read the drive mechanism status and slot table into our internal buffer.
2053 If the buffer does not yet exist, allocate it. */
2055 cdrom_read_changer_info (ide_drive_t
*drive
)
2058 struct cdrom_info
*info
= drive
->driver_data
;
2060 if (info
->changer_info
)
2061 nslots
= info
->changer_info
->hdr
.nslots
;
2064 struct atapi_mechstat_header mechbuf
;
2067 stat
= cdrom_read_mech_status (drive
,
2074 nslots
= mechbuf
.nslots
;
2075 info
->changer_info
=
2076 (struct atapi_changer_info
*)
2077 kmalloc (sizeof (struct atapi_changer_info
) +
2078 nslots
* sizeof (struct atapi_slot
),
2081 if (info
->changer_info
== NULL
)
2085 return cdrom_read_mech_status
2087 (char *)&info
->changer_info
->hdr
,
2088 sizeof (struct atapi_mechstat_header
) +
2089 nslots
* sizeof (struct atapi_slot
),
2095 int ide_cdrom_dev_ioctl (struct cdrom_device_info
*cdi
,
2096 unsigned int cmd
, unsigned long arg
)
2099 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2100 struct cdrom_info
*info
= drive
->driver_data
;
2105 case CDROMREADMODE1
:
2106 case CDROMREADMODE2
: {
2107 struct cdrom_msf msf
;
2108 int blocksize
, format
, stat
, lba
;
2109 struct atapi_toc
*toc
;
2112 if (cmd
== CDROMREADMODE1
) {
2113 blocksize
= CD_FRAMESIZE
;
2115 } else { /* for RAW and MODE2. */
2116 blocksize
= CD_FRAMESIZE_RAW
;
2120 copy_from_user_ret(&msf
, (void *)arg
, sizeof (msf
), -EFAULT
);
2122 lba
= msf_to_lba(msf
.cdmsf_min0
,
2126 /* Make sure the TOC is up to date. */
2127 stat
= cdrom_read_toc (drive
, NULL
);
2128 if (stat
) return stat
;
2132 if (lba
< 0 || lba
>= toc
->capacity
)
2135 buf
= (char *) kmalloc (blocksize
, GFP_KERNEL
);
2139 stat
= cdrom_read_block (drive
, format
, lba
, 1, buf
, blocksize
,
2143 if (cmd
== CDROMREADMODE2
) {
2144 /* For Mode2, skip the Sync, Header, and Subheader */
2145 copy_to_user_ret((char *)arg
, buf
+16, CD_FRAMESIZE_RAW0
, -EFAULT
);
2147 copy_to_user_ret((char *)arg
, buf
, blocksize
, -EFAULT
);
2155 /* Read 2352 byte blocks from audio tracks. */
2156 case CDROMREADAUDIO
: {
2158 struct atapi_toc
*toc
;
2159 struct cdrom_read_audio ra
;
2162 /* Make sure the TOC is up to date. */
2163 stat
= cdrom_read_toc (drive
, NULL
);
2164 if (stat
) return stat
;
2168 stat
= verify_area (VERIFY_READ
, (char *)arg
, sizeof (ra
));
2169 if (stat
) return stat
;
2171 copy_from_user (&ra
, (void *)arg
, sizeof (ra
));
2173 if (ra
.nframes
< 0 || ra
.nframes
> toc
->capacity
)
2175 else if (ra
.nframes
== 0)
2178 stat
= verify_area (VERIFY_WRITE
, (char *)ra
.buf
,
2179 ra
.nframes
* CD_FRAMESIZE_RAW
);
2180 if (stat
) return stat
;
2182 if (ra
.addr_format
== CDROM_MSF
)
2183 lba
= msf_to_lba (ra
.addr
.msf
.minute
,
2186 else if (ra
.addr_format
== CDROM_LBA
)
2191 if (lba
< 0 || lba
>= toc
->capacity
)
2194 buf
= (char *) kmalloc (CDROM_NBLOCKS_BUFFER
*CD_FRAMESIZE_RAW
,
2199 while (ra
.nframes
> 0) {
2200 int this_nblocks
= ra
.nframes
;
2201 if (this_nblocks
> CDROM_NBLOCKS_BUFFER
)
2202 this_nblocks
= CDROM_NBLOCKS_BUFFER
;
2203 stat
= cdrom_read_block
2204 (drive
, 1, lba
, this_nblocks
,
2205 buf
, this_nblocks
* CD_FRAMESIZE_RAW
, NULL
);
2208 copy_to_user (ra
.buf
, buf
,
2209 this_nblocks
* CD_FRAMESIZE_RAW
);
2210 ra
.buf
+= this_nblocks
* CD_FRAMESIZE_RAW
;
2211 ra
.nframes
-= this_nblocks
;
2212 lba
+= this_nblocks
;
2219 case CDROMSETSPINDOWN
: {
2224 stat
= verify_area (VERIFY_READ
, (void *) arg
,
2226 if (stat
) return stat
;
2228 copy_from_user (&spindown
, (void *) arg
, sizeof(char));
2230 stat
= cdrom_mode_sense (drive
, PAGE_CDROM
, 0, buffer
,
2231 sizeof (buffer
), NULL
);
2232 if (stat
) return stat
;
2234 buffer
[11] = (buffer
[11] & 0xf0) | (spindown
& 0x0f);
2236 return cdrom_mode_select (drive
, PAGE_CDROM
, buffer
,
2237 sizeof (buffer
), NULL
);
2240 case CDROMGETSPINDOWN
: {
2245 stat
= verify_area (VERIFY_WRITE
, (void *) arg
,
2247 if (stat
) return stat
;
2249 stat
= cdrom_mode_sense (drive
, PAGE_CDROM
, 0, buffer
,
2250 sizeof (buffer
), NULL
);
2251 if (stat
) return stat
;
2253 spindown
= buffer
[11] & 0x0f;
2255 copy_to_user ((void *) arg
, &spindown
, sizeof (char));
2260 #ifdef ALLOW_TEST_PACKETS
2263 struct packet_command pc
;
2266 memset (&pc
, 0, sizeof (pc
));
2268 stat
= verify_area (VERIFY_READ
, (void *) arg
, sizeof (pc
.c
));
2269 if (stat
) return stat
;
2270 copy_from_user (&pc
.c
, (void *) arg
, sizeof (pc
.c
));
2271 arg
+= sizeof (pc
.c
);
2273 stat
= verify_area (VERIFY_READ
, (void *) arg
, sizeof (len
));
2274 if (stat
) return stat
;
2275 copy_from_user (&len
, (void *) arg
, sizeof (len
));
2276 arg
+= sizeof (len
);
2279 if (lena
< 0) lena
= -lena
;
2284 stat
= verify_area (VERIFY_WRITE
,
2286 if (stat
) return stat
;
2289 stat
= verify_area (VERIFY_READ
,
2290 (void *) arg
, -len
);
2291 if (stat
) return stat
;
2292 copy_from_user (buf
, (void*)arg
, -len
);
2300 stat
= cdrom_queue_packet_command (drive
, &pc
);
2303 copy_to_user ((void *)arg
, buf
, len
);
2319 int ide_cdrom_audio_ioctl (struct cdrom_device_info
*cdi
,
2320 unsigned int cmd
, void *arg
)
2323 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2324 struct cdrom_info
*info
= drive
->driver_data
;
2327 case CDROMSUBCHNL
: {
2328 struct atapi_cdrom_subchnl scbuf
;
2330 struct cdrom_subchnl
*subchnl
= (struct cdrom_subchnl
*)arg
;
2332 stat
= cdrom_read_subchannel (drive
, 1, /* current position */
2333 (char *)&scbuf
, sizeof (scbuf
),
2335 if (stat
) return stat
;
2337 #if ! STANDARD_ATAPI
2338 if (CDROM_CONFIG_FLAGS (drive
)->subchan_as_bcd
) {
2339 msf_from_bcd (&scbuf
.acdsc_absaddr
.msf
);
2340 msf_from_bcd (&scbuf
.acdsc_reladdr
.msf
);
2342 if (CDROM_CONFIG_FLAGS (drive
)->tocaddr_as_bcd
)
2343 scbuf
.acdsc_trk
= bcd2bin (scbuf
.acdsc_trk
);
2344 #endif /* not STANDARD_ATAPI */
2346 subchnl
->cdsc_absaddr
.msf
.minute
=
2347 scbuf
.acdsc_absaddr
.msf
.minute
;
2348 subchnl
->cdsc_absaddr
.msf
.second
=
2349 scbuf
.acdsc_absaddr
.msf
.second
;
2350 subchnl
->cdsc_absaddr
.msf
.frame
=
2351 scbuf
.acdsc_absaddr
.msf
.frame
;
2353 subchnl
->cdsc_reladdr
.msf
.minute
=
2354 scbuf
.acdsc_reladdr
.msf
.minute
;
2355 subchnl
->cdsc_reladdr
.msf
.second
=
2356 scbuf
.acdsc_reladdr
.msf
.second
;
2357 subchnl
->cdsc_reladdr
.msf
.frame
=
2358 scbuf
.acdsc_reladdr
.msf
.frame
;
2360 subchnl
->cdsc_audiostatus
= scbuf
.acdsc_audiostatus
;
2361 subchnl
->cdsc_ctrl
= scbuf
.acdsc_ctrl
;
2362 subchnl
->cdsc_trk
= scbuf
.acdsc_trk
;
2363 subchnl
->cdsc_ind
= scbuf
.acdsc_ind
;
2368 case CDROMREADTOCHDR
: {
2370 struct cdrom_tochdr
*tochdr
= (struct cdrom_tochdr
*) arg
;
2371 struct atapi_toc
*toc
;
2373 /* Make sure our saved TOC is valid. */
2374 stat
= cdrom_read_toc (drive
, NULL
);
2375 if (stat
) return stat
;
2378 tochdr
->cdth_trk0
= toc
->hdr
.first_track
;
2379 tochdr
->cdth_trk1
= toc
->hdr
.last_track
;
2384 case CDROMREADTOCENTRY
: {
2386 struct cdrom_tocentry
*tocentry
= (struct cdrom_tocentry
*) arg
;
2387 struct atapi_toc_entry
*toce
;
2389 stat
= cdrom_get_toc_entry (drive
, tocentry
->cdte_track
, &toce
,
2391 if (stat
) return stat
;
2393 tocentry
->cdte_ctrl
= toce
->control
;
2394 tocentry
->cdte_adr
= toce
->adr
;
2395 tocentry
->cdte_format
= CDROM_LBA
;
2396 tocentry
->cdte_addr
.lba
= toce
->addr
.lba
;
2401 case CDROMPLAYMSF
: {
2402 struct cdrom_msf
*msf
= (struct cdrom_msf
*) arg
;
2403 int lba_start
, lba_end
;
2405 lba_start
= msf_to_lba (msf
->cdmsf_min0
, msf
->cdmsf_sec0
,
2407 lba_end
= msf_to_lba (msf
->cdmsf_min1
, msf
->cdmsf_sec1
,
2408 msf
->cdmsf_frame1
) + 1;
2410 if (lba_end
<= lba_start
) return -EINVAL
;
2412 return cdrom_play_lba_range (drive
, lba_start
, lba_end
, NULL
);
2415 /* Like just about every other Linux cdrom driver, we ignore the
2416 index part of the request here. */
2417 case CDROMPLAYTRKIND
: {
2418 int stat
, lba_start
, lba_end
;
2419 struct cdrom_ti
*ti
= (struct cdrom_ti
*)arg
;
2420 struct atapi_toc_entry
*first_toc
, *last_toc
;
2422 stat
= cdrom_get_toc_entry (drive
, ti
->cdti_trk0
, &first_toc
,
2424 if (stat
) return stat
;
2425 stat
= cdrom_get_toc_entry (drive
, ti
->cdti_trk1
, &last_toc
,
2427 if (stat
) return stat
;
2429 if (ti
->cdti_trk1
!= CDROM_LEADOUT
) ++last_toc
;
2430 lba_start
= first_toc
->addr
.lba
;
2431 lba_end
= last_toc
->addr
.lba
;
2433 if (lba_end
<= lba_start
) return -EINVAL
;
2435 return cdrom_play_lba_range (drive
, lba_start
, lba_end
, NULL
);
2438 case CDROMVOLCTRL
: {
2439 struct cdrom_volctrl
*volctrl
= (struct cdrom_volctrl
*) arg
;
2440 char buffer
[24], mask
[24];
2443 stat
= cdrom_mode_sense (drive
, PAGE_AUDIO
, 0, buffer
,
2444 sizeof (buffer
), NULL
);
2445 if (stat
) return stat
;
2446 stat
= cdrom_mode_sense (drive
, PAGE_AUDIO
, 1, mask
,
2447 sizeof (buffer
), NULL
);
2448 if (stat
) return stat
;
2450 buffer
[1] = buffer
[2] = 0;
2452 buffer
[17] = volctrl
->channel0
& mask
[17];
2453 buffer
[19] = volctrl
->channel1
& mask
[19];
2454 buffer
[21] = volctrl
->channel2
& mask
[21];
2455 buffer
[23] = volctrl
->channel3
& mask
[23];
2457 return cdrom_mode_select (drive
, PAGE_AUDIO
, buffer
,
2458 sizeof (buffer
), NULL
);
2461 case CDROMVOLREAD
: {
2462 struct cdrom_volctrl
*volctrl
= (struct cdrom_volctrl
*) arg
;
2466 stat
= cdrom_mode_sense (drive
, PAGE_AUDIO
, 0, buffer
,
2467 sizeof (buffer
), NULL
);
2468 if (stat
) return stat
;
2470 volctrl
->channel0
= buffer
[17];
2471 volctrl
->channel1
= buffer
[19];
2472 volctrl
->channel2
= buffer
[21];
2473 volctrl
->channel3
= buffer
[23];
2479 return cdrom_startstop (drive
, 1, NULL
);
2482 #ifdef IHAVEADOLPHIN
2483 /* Certain Drives require this. Most don't
2484 and will produce errors upon CDROMSTOP
2485 pit says the Dolphin needs this. If you
2486 own a dolphin, just define IHAVEADOLPHIN somewhere */
2488 stat
= cdrom_startstop (drive
, 0, NULL
);
2489 if (stat
) return stat
;
2490 return cdrom_eject (drive
, 1, NULL
);
2491 #endif /* end of IHAVEADOLPHIN */
2492 return cdrom_startstop (drive
, 0, NULL
);
2496 return cdrom_pause (drive
, 1, NULL
);
2499 return cdrom_pause (drive
, 0, NULL
);
2509 int ide_cdrom_reset (struct cdrom_device_info
*cdi
)
2511 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2514 ide_init_drive_cmd (&req
);
2515 req
.cmd
= RESET_DRIVE_COMMAND
;
2516 return ide_do_drive_cmd (drive
, &req
, ide_wait
);
2521 int ide_cdrom_tray_move (struct cdrom_device_info
*cdi
, int position
)
2523 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2524 struct atapi_request_sense rq
;
2527 int stat
= cdrom_lockdoor (drive
, 0, &rq
);
2528 if (stat
) return stat
;
2531 return cdrom_eject (drive
, !position
, NULL
);
2536 int ide_cdrom_lock_door (struct cdrom_device_info
*cdi
, int lock
)
2538 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2539 return cdrom_lockdoor (drive
, lock
, NULL
);
2543 int ide_cdrom_select_speed (struct cdrom_device_info
*cdi
, int speed
)
2545 int stat
, attempts
= 3;
2548 struct atapi_capabilities_page cap
;
2550 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2551 struct atapi_request_sense reqbuf
;
2552 stat
=cdrom_select_speed (drive
, speed
, &reqbuf
);
2556 /* Now with that done, update the speed fields */
2557 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2558 if (attempts
-- <= 0)
2560 stat
= cdrom_mode_sense (drive
, PAGE_CAPABILITIES
, 0,
2561 (char *)&buf
, sizeof (buf
), NULL
);
2564 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2565 if (drive
->id
&& !drive
->id
->model
[0] && !strncmp(drive
->id
->fw_rev
, "241N", 4)) {
2566 CDROM_STATE_FLAGS (drive
)->current_speed
=
2567 (((unsigned int)buf
.cap
.curspeed
) + (176/2)) / 176;
2568 CDROM_CONFIG_FLAGS (drive
)->max_speed
=
2569 (((unsigned int)buf
.cap
.maxspeed
) + (176/2)) / 176;
2571 CDROM_STATE_FLAGS (drive
)->current_speed
=
2572 (ntohs(buf
.cap
.curspeed
) + (176/2)) / 176;
2573 CDROM_CONFIG_FLAGS (drive
)->max_speed
=
2574 (ntohs(buf
.cap
.maxspeed
) + (176/2)) / 176;
2576 cdi
->speed
= CDROM_STATE_FLAGS (drive
)->current_speed
;
2582 int ide_cdrom_select_disc (struct cdrom_device_info
*cdi
, int slot
)
2584 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2585 struct cdrom_info
*info
= drive
->driver_data
;
2587 struct atapi_request_sense my_reqbuf
;
2589 int nslots
, curslot
;
2591 if ( ! CDROM_CONFIG_FLAGS (drive
)->is_changer
)
2592 return -EDRIVE_CANT_DO_THIS
;
2594 #if ! STANDARD_ATAPI
2595 if (CDROM_STATE_FLAGS (drive
)->sanyo_slot
> 0) {
2597 curslot
= CDROM_STATE_FLAGS (drive
)->sanyo_slot
;
2601 #endif /* not STANDARD_ATAPI */
2603 stat
= cdrom_read_changer_info (drive
);
2607 nslots
= info
->changer_info
->hdr
.nslots
;
2608 curslot
= info
->changer_info
->hdr
.curslot
;
2611 if (slot
== curslot
)
2614 if (slot
== CDSL_CURRENT
)
2617 if (slot
!= CDSL_NONE
&& (slot
< 0 || slot
>= nslots
))
2620 if (drive
->usage
> 1)
2623 if (slot
== CDSL_NONE
) {
2624 (void) cdrom_load_unload (drive
, -1, NULL
);
2625 cdrom_saw_media_change (drive
);
2626 (void) cdrom_lockdoor (drive
, 0, NULL
);
2633 #if ! STANDARD_ATAPI
2634 CDROM_STATE_FLAGS (drive
)->sanyo_slot
== 0 &&
2636 info
->changer_info
->slots
[slot
].disc_present
== 0) {
2640 was_locked
= CDROM_STATE_FLAGS (drive
)->door_locked
;
2642 (void) cdrom_lockdoor (drive
, 0, NULL
);
2644 stat
= cdrom_load_unload (drive
, slot
, NULL
);
2645 cdrom_saw_media_change (drive
);
2649 stat
= cdrom_check_status (drive
, &my_reqbuf
);
2650 if (stat
&& my_reqbuf
.sense_key
== NOT_READY
)
2653 if (stat
== 0 || my_reqbuf
.sense_key
== UNIT_ATTENTION
) {
2654 stat
= cdrom_read_toc (drive
, &my_reqbuf
);
2660 (void) cdrom_lockdoor (drive
, 1, NULL
);
2668 int ide_cdrom_drive_status (struct cdrom_device_info
*cdi
, int slot_nr
)
2670 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2671 struct cdrom_info
*info
= drive
->driver_data
;
2673 if (slot_nr
== CDSL_CURRENT
) {
2675 struct atapi_request_sense my_reqbuf
;
2676 int stat
= cdrom_check_status (drive
, &my_reqbuf
);
2677 if (stat
== 0 || my_reqbuf
.sense_key
== UNIT_ATTENTION
)
2680 if (my_reqbuf
.sense_key
== NOT_READY
) {
2681 /* ATAPI doesn't have anything that can help
2682 us decide whether the drive is really
2683 emtpy or the tray is just open. irk. */
2684 return CDS_TRAY_OPEN
;
2687 return CDS_DRIVE_NOT_READY
;
2690 #if ! STANDARD_ATAPI
2691 else if (CDROM_STATE_FLAGS (drive
)->sanyo_slot
> 0)
2693 #endif /* not STANDARD_ATAPI */
2696 struct atapi_changer_info
*ci
;
2697 int stat
= cdrom_read_changer_info (drive
);
2700 ci
= info
->changer_info
;
2702 if (ci
->slots
[slot_nr
].disc_present
)
2710 int ide_cdrom_get_last_session (struct cdrom_device_info
*cdi
,
2711 struct cdrom_multisession
*ms_info
)
2714 struct atapi_toc
*toc
;
2715 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2716 struct cdrom_info
*info
= drive
->driver_data
;
2718 /* Make sure the TOC information is valid. */
2719 stat
= cdrom_read_toc (drive
, NULL
);
2720 if (stat
) return stat
;
2723 ms_info
->addr
.lba
= toc
->last_session_lba
;
2724 ms_info
->xa_flag
= toc
->xa_flag
;
2731 int ide_cdrom_get_mcn (struct cdrom_device_info
*cdi
,
2732 struct cdrom_mcn
*mcn_info
)
2736 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2738 stat
= cdrom_read_subchannel (drive
, 2, /* get MCN */
2739 mcnbuf
, sizeof (mcnbuf
),
2741 if (stat
) return stat
;
2743 memcpy (mcn_info
->medium_catalog_number
, mcnbuf
+9,
2744 sizeof (mcn_info
->medium_catalog_number
)-1);
2745 mcn_info
->medium_catalog_number
[sizeof (mcn_info
->medium_catalog_number
)-1]
2753 /****************************************************************************
2754 * Other driver requests (open, close, check media change).
2758 int ide_cdrom_check_media_change_real (struct cdrom_device_info
*cdi
,
2761 ide_drive_t
*drive
= (ide_drive_t
*) cdi
->handle
;
2762 struct cdrom_info
*info
= drive
->driver_data
;
2766 if (slot_nr
== CDSL_CURRENT
) {
2767 (void) cdrom_check_status (drive
, NULL
);
2768 retval
= CDROM_STATE_FLAGS (drive
)->media_changed
;
2769 CDROM_STATE_FLAGS (drive
)->media_changed
= 0;
2772 #if ! STANDARD_ATAPI
2773 else if (CDROM_STATE_FLAGS (drive
)->sanyo_slot
> 0) {
2776 #endif /* not STANDARD_ATAPI */
2779 struct atapi_changer_info
*ci
;
2780 int stat
= cdrom_read_changer_info (drive
);
2783 ci
= info
->changer_info
;
2785 /* This test may be redundant with cdrom.c. */
2786 if (slot_nr
< 0 || slot_nr
>= ci
->hdr
.nslots
)
2789 retval
= ci
->slots
[slot_nr
].change
;
2797 int ide_cdrom_open_real (struct cdrom_device_info
*cdi
, int purpose
)
2804 * Close down the device. Invalidate all cached blocks.
2808 void ide_cdrom_release_real (struct cdrom_device_info
*cdi
)
2814 /****************************************************************************
2815 * Device initialization.
2819 struct cdrom_device_ops ide_cdrom_dops
= {
2820 ide_cdrom_open_real
, /* open */
2821 ide_cdrom_release_real
, /* release */
2822 ide_cdrom_drive_status
, /* drive_status */
2823 ide_cdrom_check_media_change_real
, /* media_changed */
2824 ide_cdrom_tray_move
, /* tray_move */
2825 ide_cdrom_lock_door
, /* lock_door */
2826 ide_cdrom_select_speed
, /* select_speed */
2827 ide_cdrom_select_disc
, /* select_disc */
2828 ide_cdrom_get_last_session
, /* get_last_session */
2829 ide_cdrom_get_mcn
, /* get_mcn */
2830 ide_cdrom_reset
, /* reset */
2831 ide_cdrom_audio_ioctl
, /* audio_ioctl */
2832 ide_cdrom_dev_ioctl
, /* dev_ioctl */
2833 CDC_CLOSE_TRAY
| CDC_OPEN_TRAY
| CDC_LOCK
| CDC_SELECT_SPEED
2834 | CDC_SELECT_DISC
| CDC_MULTI_SESSION
| CDC_MCN
2835 | CDC_MEDIA_CHANGED
| CDC_PLAY_AUDIO
| CDC_RESET
2836 | CDC_IOCTLS
| CDC_DRIVE_STATUS
, /* capability */
2840 static int ide_cdrom_register (ide_drive_t
*drive
, int nslots
)
2842 struct cdrom_info
*info
= drive
->driver_data
;
2843 struct cdrom_device_info
*devinfo
= &info
->devinfo
;
2844 int minor
= (drive
->select
.b
.unit
)<<PARTN_BITS
;
2846 devinfo
->dev
= MKDEV (HWIF(drive
)->major
, minor
);
2847 devinfo
->ops
= &ide_cdrom_dops
;
2849 *(int *)&devinfo
->speed
= CDROM_STATE_FLAGS (drive
)->current_speed
;
2850 *(int *)&devinfo
->capacity
= nslots
;
2851 devinfo
->handle
= (void *) drive
;
2852 strcpy(devinfo
->name
, drive
->name
);
2853 return register_cdrom (devinfo
);
2858 int ide_cdrom_probe_capabilities (ide_drive_t
*drive
)
2860 int stat
, nslots
= 0, attempts
= 3;
2863 struct atapi_capabilities_page cap
;
2866 if (CDROM_CONFIG_FLAGS (drive
)->nec260
)
2869 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2870 if (attempts
-- <= 0)
2872 stat
= cdrom_mode_sense (drive
, PAGE_CAPABILITIES
, 0,
2873 (char *)&buf
, sizeof (buf
), NULL
);
2876 if (buf
.cap
.lock
== 0)
2877 CDROM_CONFIG_FLAGS (drive
)->no_doorlock
= 1;
2879 CDROM_CONFIG_FLAGS (drive
)->no_eject
= 0;
2880 if (buf
.cap
.cd_r_write
)
2881 CDROM_CONFIG_FLAGS (drive
)->cd_r
= 1;
2882 if (buf
.cap
.cd_rw_write
)
2883 CDROM_CONFIG_FLAGS (drive
)->cd_rw
= 1;
2884 if (buf
.cap
.test_write
)
2885 CDROM_CONFIG_FLAGS (drive
)->test_write
= 1;
2886 if (buf
.cap
.dvd_ram_read
|| buf
.cap
.dvd_r_read
|| buf
.cap
.dvd_rom
)
2887 CDROM_CONFIG_FLAGS (drive
)->dvd
= 1;
2888 if (buf
.cap
.dvd_ram_write
)
2889 CDROM_CONFIG_FLAGS (drive
)->dvd_r
= 1;
2890 if (buf
.cap
.dvd_r_write
)
2891 CDROM_CONFIG_FLAGS (drive
)->dvd_rw
= 1;
2893 #if ! STANDARD_ATAPI
2894 if (CDROM_STATE_FLAGS (drive
)->sanyo_slot
> 0) {
2895 CDROM_CONFIG_FLAGS (drive
)->is_changer
= 1;
2900 #endif /* not STANDARD_ATAPI */
2901 if (buf
.cap
.mechtype
== mechtype_individual_changer
||
2902 buf
.cap
.mechtype
== mechtype_cartridge_changer
) {
2903 struct atapi_mechstat_header mechbuf
;
2905 stat
= cdrom_read_mech_status (drive
, (char*)&mechbuf
,
2906 sizeof (mechbuf
), NULL
);
2908 CDROM_CONFIG_FLAGS (drive
)->is_changer
= 1;
2909 CDROM_CONFIG_FLAGS (drive
)->supp_disc_present
= 1;
2910 nslots
= mechbuf
.nslots
;
2914 /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2915 if (drive
->id
&& !drive
->id
->model
[0] && !strncmp(drive
->id
->fw_rev
, "241N", 4)) {
2916 CDROM_STATE_FLAGS (drive
)->current_speed
=
2917 (((unsigned int)buf
.cap
.curspeed
) + (176/2)) / 176;
2918 CDROM_CONFIG_FLAGS (drive
)->max_speed
=
2919 (((unsigned int)buf
.cap
.maxspeed
) + (176/2)) / 176;
2921 CDROM_STATE_FLAGS (drive
)->current_speed
=
2922 (ntohs(buf
.cap
.curspeed
) + (176/2)) / 176;
2923 CDROM_CONFIG_FLAGS (drive
)->max_speed
=
2924 (ntohs(buf
.cap
.maxspeed
) + (176/2)) / 176;
2927 printk ("%s: ATAPI %dX %s",
2928 drive
->name
, CDROM_CONFIG_FLAGS (drive
)->max_speed
,
2929 (CDROM_CONFIG_FLAGS (drive
)->dvd
) ? "DVD-ROM" : "CD-ROM");
2931 if (CDROM_CONFIG_FLAGS (drive
)->dvd_r
|CDROM_CONFIG_FLAGS (drive
)->dvd_rw
)
2933 (CDROM_CONFIG_FLAGS (drive
)->dvd_r
)? "-RAM" : "",
2934 (CDROM_CONFIG_FLAGS (drive
)->dvd_rw
)? "/RW" : "");
2936 if (CDROM_CONFIG_FLAGS (drive
)->cd_r
|CDROM_CONFIG_FLAGS (drive
)->cd_rw
)
2938 (CDROM_CONFIG_FLAGS (drive
)->cd_r
)? "-R" : "",
2939 (CDROM_CONFIG_FLAGS (drive
)->cd_rw
)? "/RW" : "");
2941 if (CDROM_CONFIG_FLAGS (drive
)->is_changer
)
2942 printk (" changer w/%d slots", nslots
);
2946 printk (", %dkB Cache", ntohs(buf
.cap
.buffer_size
));
2948 if (drive
->using_dma
) {
2949 if ((drive
->id
->field_valid
& 4) &&
2950 (drive
->id
->dma_ultra
& (drive
->id
->dma_ultra
>> 8) & 7)) {
2951 printk(", UDMA"); /* UDMA BIOS-enabled! */
2952 } else if (drive
->id
->field_valid
& 4) {
2953 printk(", (U)DMA"); /* Can be BIOS-enabled! */
2963 static void ide_cdrom_add_settings(ide_drive_t
*drive
)
2965 int major
= HWIF(drive
)->major
;
2966 int minor
= drive
->select
.b
.unit
<< PARTN_BITS
;
2968 ide_add_setting(drive
, "breada_readahead", SETTING_RW
, BLKRAGET
, BLKRASET
, TYPE_INT
, 0, 255, 1, 2, &read_ahead
[major
], NULL
);
2969 ide_add_setting(drive
, "file_readahead", SETTING_RW
, BLKFRAGET
, BLKFRASET
, TYPE_INTA
, 0, INT_MAX
, 1, 1024, &max_readahead
[major
][minor
], NULL
);
2970 ide_add_setting(drive
, "max_kb_per_request", SETTING_RW
, BLKSECTGET
, BLKSECTSET
, TYPE_INTA
, 1, 255, 1, 2, &max_sectors
[major
][minor
], NULL
);
2971 ide_add_setting(drive
, "dsc_overlap", SETTING_RW
, -1, -1, TYPE_BYTE
, 0, 1, 1, 1, &drive
->dsc_overlap
, NULL
);
2974 #ifdef CONFIG_IDECD_SLOTS
2975 static void ide_cdrom_slot_check (ide_drive_t
*drive
, int nslots
)
2978 struct cdrom_info
*info
= drive
->driver_data
;
2979 struct cdrom_device_info
*devinfo
= &info
->devinfo
;
2980 int slot_count
= 0, drive_stat
= 0, tmp
;
2982 for (slot_count
=0;slot_count
<nslots
;slot_count
++) {
2983 (void) ide_cdrom_select_disc(devinfo
, slot_count
);
2984 printk(" CD Slot %d ", slot_count
+1);
2986 drive_stat
= ide_cdrom_drive_status(devinfo
, slot_count
);
2989 } else switch(drive_stat
) {
2991 /* use routine in Uniform CD-ROM driver */
2992 cdrom_count_tracks(devinfo
, &tracks
);
2993 tmp
= tracks
.audio
+ tracks
.data
+
2994 tracks
.cdi
+ tracks
.xa
;
2995 printk(": Disc has %d track%s: ", tmp
,
2996 (tmp
== 1)? "" : "s");
2997 printk("%d=data %d=audio %d=Cd-I %d=XA\n",
2998 tracks
.data
, tracks
.audio
,
2999 tracks
.cdi
, tracks
.xa
);
3002 printk("Empty slot.\n");
3005 printk("CD-ROM tray open.\n");
3007 case CDS_DRIVE_NOT_READY
:
3008 printk("CD-ROM drive not ready.\n");
3011 printk("No Information available.\n");
3014 printk("This Should not happen!\n");
3018 (void) ide_cdrom_select_disc(devinfo
, 0);
3020 #endif /* CONFIG_IDECD_SLOTS */
3023 int ide_cdrom_setup (ide_drive_t
*drive
)
3025 struct cdrom_info
*info
= drive
->driver_data
;
3028 kdev_t dev
= MKDEV (HWIF (drive
)->major
,
3029 drive
->select
.b
.unit
<< PARTN_BITS
);
3031 set_device_ro (dev
, 1);
3032 blksize_size
[HWIF(drive
)->major
][drive
->select
.b
.unit
<< PARTN_BITS
] =
3035 drive
->special
.all
= 0;
3036 drive
->ready_stat
= 0;
3038 CDROM_STATE_FLAGS (drive
)->media_changed
= 0;
3039 CDROM_STATE_FLAGS (drive
)->toc_valid
= 0;
3040 CDROM_STATE_FLAGS (drive
)->door_locked
= 0;
3043 CDROM_CONFIG_FLAGS (drive
)->no_doorlock
= 1;
3045 CDROM_CONFIG_FLAGS (drive
)->no_doorlock
= 0;
3048 if (drive
->id
!= NULL
)
3049 CDROM_CONFIG_FLAGS (drive
)->drq_interrupt
=
3050 ((drive
->id
->config
& 0x0060) == 0x20);
3052 CDROM_CONFIG_FLAGS (drive
)->drq_interrupt
= 0;
3054 CDROM_CONFIG_FLAGS (drive
)->is_changer
= 0;
3055 CDROM_CONFIG_FLAGS (drive
)->cd_r
= 0;
3056 CDROM_CONFIG_FLAGS (drive
)->cd_rw
= 0;
3057 CDROM_CONFIG_FLAGS (drive
)->test_write
= 0;
3058 CDROM_CONFIG_FLAGS (drive
)->dvd
= 0;
3059 CDROM_CONFIG_FLAGS (drive
)->dvd_r
= 0;
3060 CDROM_CONFIG_FLAGS (drive
)->dvd_rw
= 0;
3061 CDROM_CONFIG_FLAGS (drive
)->no_eject
= 1;
3062 CDROM_CONFIG_FLAGS (drive
)->supp_disc_present
= 0;
3064 /* limit transfer size per interrupt. */
3065 CDROM_CONFIG_FLAGS (drive
)->limit_nframes
= 0;
3066 if (drive
->id
!= NULL
) {
3067 if (!strcmp(drive
->id
->model
, "SAMSUNG CD-ROM SCR-2430"))
3068 CDROM_CONFIG_FLAGS (drive
)->limit_nframes
= 1;
3069 else if (!strcmp(drive
->id
->model
, "SAMSUNG CD-ROM SCR-2432"))
3070 CDROM_CONFIG_FLAGS (drive
)->limit_nframes
= 1;
3073 #if ! STANDARD_ATAPI
3074 /* by default Sanyo 3 CD changer support is turned off and
3075 ATAPI Rev 2.2+ standard support for CD changers is used */
3076 CDROM_STATE_FLAGS (drive
)->sanyo_slot
= 0;
3078 CDROM_CONFIG_FLAGS (drive
)->nec260
= 0;
3079 CDROM_CONFIG_FLAGS (drive
)->toctracks_as_bcd
= 0;
3080 CDROM_CONFIG_FLAGS (drive
)->tocaddr_as_bcd
= 0;
3081 CDROM_CONFIG_FLAGS (drive
)->playmsf_as_bcd
= 0;
3082 CDROM_CONFIG_FLAGS (drive
)->subchan_as_bcd
= 0;
3084 if (drive
->id
!= NULL
) {
3085 if (strcmp (drive
->id
->model
, "V003S0DS") == 0 &&
3086 drive
->id
->fw_rev
[4] == '1' &&
3087 drive
->id
->fw_rev
[6] <= '2') {
3089 Some versions of this drive like to talk BCD. */
3090 CDROM_CONFIG_FLAGS (drive
)->toctracks_as_bcd
= 1;
3091 CDROM_CONFIG_FLAGS (drive
)->tocaddr_as_bcd
= 1;
3092 CDROM_CONFIG_FLAGS (drive
)->playmsf_as_bcd
= 1;
3093 CDROM_CONFIG_FLAGS (drive
)->subchan_as_bcd
= 1;
3096 else if (strcmp (drive
->id
->model
, "V006E0DS") == 0 &&
3097 drive
->id
->fw_rev
[4] == '1' &&
3098 drive
->id
->fw_rev
[6] <= '2') {
3099 /* Vertos 600 ESD. */
3100 CDROM_CONFIG_FLAGS (drive
)->toctracks_as_bcd
= 1;
3103 else if (strcmp (drive
->id
->model
,
3104 "NEC CD-ROM DRIVE:260") == 0 &&
3105 strncmp (drive
->id
->fw_rev
, "1.01", 4) == 0) { /* FIXME */
3106 /* Old NEC260 (not R).
3107 This drive was released before the 1.2 version
3109 CDROM_CONFIG_FLAGS (drive
)->tocaddr_as_bcd
= 1;
3110 CDROM_CONFIG_FLAGS (drive
)->playmsf_as_bcd
= 1;
3111 CDROM_CONFIG_FLAGS (drive
)->subchan_as_bcd
= 1;
3112 CDROM_CONFIG_FLAGS (drive
)->nec260
= 1;
3115 else if (strcmp (drive
->id
->model
, "WEARNES CDD-120") == 0 &&
3116 strncmp (drive
->id
->fw_rev
, "A1.1", 4) == 0) { /* FIXME */
3118 CDROM_CONFIG_FLAGS (drive
)->playmsf_as_bcd
= 1;
3119 CDROM_CONFIG_FLAGS (drive
)->subchan_as_bcd
= 1;
3122 /* Sanyo 3 CD changer uses a non-standard command
3124 else if ((strcmp(drive
->id
->model
, "CD-ROM CDR-C3 G") == 0) ||
3125 (strcmp(drive
->id
->model
, "CD-ROM CDR-C3G") == 0) ||
3126 (strcmp(drive
->id
->model
, "CD-ROM CDR_C36") == 0)) {
3127 /* uses CD in slot 0 when value is set to 3 */
3128 CDROM_STATE_FLAGS (drive
)->sanyo_slot
= 3;
3133 #endif /* not STANDARD_ATAPI */
3136 info
->sector_buffer
= NULL
;
3137 info
->sector_buffered
= 0;
3138 info
->nsectors_buffered
= 0;
3139 info
->changer_info
= NULL
;
3141 nslots
= ide_cdrom_probe_capabilities (drive
);
3143 if (ide_cdrom_register (drive
, nslots
)) {
3144 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive
->name
);
3145 info
->devinfo
.handle
= NULL
;
3148 ide_cdrom_add_settings(drive
);
3149 #ifdef CONFIG_IDECD_SLOTS
3150 if (CDROM_CONFIG_FLAGS (drive
)->is_changer
) {
3151 ide_cdrom_slot_check(drive
, nslots
);
3153 #endif /* CONFIG_IDECD_SLOTS */
3157 /* Forwarding functions to generic routines. */
3159 int ide_cdrom_ioctl (ide_drive_t
*drive
,
3160 struct inode
*inode
, struct file
*file
,
3161 unsigned int cmd
, unsigned long arg
)
3163 return cdrom_fops
.ioctl (inode
, file
, cmd
, arg
);
3167 int ide_cdrom_open (struct inode
*ip
, struct file
*fp
, ide_drive_t
*drive
)
3172 rc
= cdrom_fops
.open (ip
, fp
);
3181 void ide_cdrom_release (struct inode
*inode
, struct file
*file
,
3184 cdrom_fops
.release (inode
, file
);
3189 int ide_cdrom_check_media_change (ide_drive_t
*drive
)
3191 return cdrom_fops
.check_media_change
3192 (MKDEV (HWIF (drive
)->major
,
3193 (drive
->select
.b
.unit
)<<PARTN_BITS
));
3198 int ide_cdrom_cleanup(ide_drive_t
*drive
)
3200 struct cdrom_info
*info
= drive
->driver_data
;
3201 struct cdrom_device_info
*devinfo
= &info
->devinfo
;
3203 if (ide_unregister_subdriver (drive
))
3205 if (info
->sector_buffer
!= NULL
)
3206 kfree (info
->sector_buffer
);
3207 if (info
->toc
!= NULL
)
3209 if (info
->changer_info
!= NULL
)
3210 kfree (info
->changer_info
);
3211 if (devinfo
->handle
== drive
&& unregister_cdrom (devinfo
))
3212 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive
->name
);
3214 drive
->driver_data
= NULL
;
3218 static ide_driver_t ide_cdrom_driver
= {
3219 "ide-cdrom", /* name */
3220 IDECD_VERSION
, /* version */
3221 ide_cdrom
, /* media */
3223 1, /* supports_dma */
3224 1, /* supports_dsc_overlap */
3225 ide_cdrom_cleanup
, /* cleanup */
3226 ide_do_rw_cdrom
, /* do_request */
3227 NULL
, /* ??? or perhaps cdrom_end_request? */
3228 ide_cdrom_ioctl
, /* ioctl */
3229 ide_cdrom_open
, /* open */
3230 ide_cdrom_release
, /* release */
3231 ide_cdrom_check_media_change
, /* media_change */
3232 NULL
, /* pre_reset */
3233 NULL
, /* capacity */
3238 int ide_cdrom_init (void);
3239 static ide_module_t ide_cdrom_module
= {
3247 char *ignore
= NULL
;
3250 MODULE_PARM(ignore
, "s");
3251 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3253 int init_module (void)
3255 return ide_cdrom_init();
3258 void cleanup_module(void)
3263 while ((drive
= ide_scan_devices (ide_cdrom
, ide_cdrom_driver
.name
, &ide_cdrom_driver
, failed
)) != NULL
)
3264 if (ide_cdrom_cleanup (drive
)) {
3265 printk ("%s: cleanup_module() called while still busy\n", drive
->name
);
3268 ide_unregister_module (&ide_cdrom_module
);
3272 int ide_cdrom_init (void)
3275 struct cdrom_info
*info
;
3279 while ((drive
= ide_scan_devices (ide_cdrom
, ide_cdrom_driver
.name
, NULL
, failed
++)) != NULL
) {
3280 /* skip drives that we were told to ignore */
3282 if (strstr(ignore
, drive
->name
)) {
3283 printk("ide-cd: ignoring drive %s\n", drive
->name
);
3286 info
= (struct cdrom_info
*) kmalloc (sizeof (struct cdrom_info
), GFP_KERNEL
);
3288 printk ("%s: Can't allocate a cdrom structure\n", drive
->name
);
3291 if (ide_register_subdriver (drive
, &ide_cdrom_driver
, IDE_SUBDRIVER_VERSION
)) {
3292 printk ("%s: Failed to register the driver with ide.c\n", drive
->name
);
3296 memset (info
, 0, sizeof (struct cdrom_info
));
3297 drive
->driver_data
= info
;
3298 DRIVER(drive
)->busy
++;
3299 if (ide_cdrom_setup (drive
)) {
3300 DRIVER(drive
)->busy
--;
3301 if (ide_cdrom_cleanup (drive
))
3302 printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive
->name
);
3305 DRIVER(drive
)->busy
--;
3308 ide_register_module(&ide_cdrom_module
);
3314 /*==========================================================================*/