~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/ide/ide-cd.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * linux/drivers/ide/ide-cd.c
  3  *
  4  * Copyright (C) 1994, 1995, 1996  scott snyder  <snyder@fnald0.fnal.gov>
  5  * Copyright (C) 1996-1998  Erik Andersen <andersee@debian.org>
  6  * Copyright (C) 1998-2000  Jens Axboe <axboe@suse.de>
  7  *
  8  * May be copied or modified under the terms of the GNU General Public
  9  * License.  See linux/COPYING for more information.
 10  *
 11  * ATAPI CD-ROM driver.  To be used with ide.c.
 12  * See Documentation/cdrom/ide-cd for usage information.
 13  *
 14  * Suggestions are welcome. Patches that work are more welcome though. ;-)
 15  * For those wishing to work on this driver, please be sure you download
 16  * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI 
 17  * (SFF-8020i rev 2.6) standards. These documents can be obtained by 
 18  * anonymous ftp from:
 19  * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
 20  * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
 21  *
 22  * Drives that deviate from these standards will be accomodated as much
 23  * as possible via compile time or command-line options.  Since I only have
 24  * a few drives, you generally need to send me patches...
 25  *
 26  * ----------------------------------
 27  * TO DO LIST:
 28  * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
 29  *   boot
 30  *
 31  * ----------------------------------
 32  * 1.00  Oct 31, 1994 -- Initial version.
 33  * 1.01  Nov  2, 1994 -- Fixed problem with starting request in
 34  *                       cdrom_check_status.
 35  * 1.03  Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
 36  * (from mlord)       -- minor changes to cdrom_setup()
 37  *                    -- renamed ide_dev_s to ide_drive_t, enable irq on command
 38  * 2.00  Nov 27, 1994 -- Generalize packet command interface;
 39  *                       add audio ioctls.
 40  * 2.01  Dec  3, 1994 -- Rework packet command interface to handle devices
 41  *                       which send an interrupt when ready for a command.
 42  * 2.02  Dec 11, 1994 -- Cache the TOC in the driver.
 43  *                       Don't use SCMD_PLAYAUDIO_TI; it's not included
 44  *                       in the current version of ATAPI.
 45  *                       Try to use LBA instead of track or MSF addressing
 46  *                       when possible.
 47  *                       Don't wait for READY_STAT.
 48  * 2.03  Jan 10, 1995 -- Rewrite block read routines to handle block sizes
 49  *                       other than 2k and to move multiple sectors in a
 50  *                       single transaction.
 51  * 2.04  Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
 52  *                       Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
 53  *                       help in figuring this out.  Ditto for Acer and
 54  *                       Aztech drives, which seem to have the same problem.
 55  * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
 56  * 2.05  Jun  8, 1995 -- Don't attempt to retry after an illegal request
 57  *                        or data protect error.
 58  *                       Use HWIF and DEV_HWIF macros as in ide.c.
 59  *                       Always try to do a request_sense after
 60  *                        a failed command.
 61  *                       Include an option to give textual descriptions
 62  *                        of ATAPI errors.
 63  *                       Fix a bug in handling the sector cache which
 64  *                        showed up if the drive returned data in 512 byte
 65  *                        blocks (like Pioneer drives).  Thanks to
 66  *                        Richard Hirst <srh@gpt.co.uk> for diagnosing this.
 67  *                       Properly supply the page number field in the
 68  *                        MODE_SELECT command.
 69  *                       PLAYAUDIO12 is broken on the Aztech; work around it.
 70  * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
 71  *                       (my apologies to Scott, but now ide-cd.c is independent)
 72  * 3.00  Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
 73  *                       Implement CDROMREADAUDIO ioctl (UNTESTED).
 74  *                       Use input_ide_data() and output_ide_data().
 75  *                       Add door locking.
 76  *                       Fix usage count leak in cdrom_open, which happened
 77  *                        when a read-write mount was attempted.
 78  *                       Try to load the disk on open.
 79  *                       Implement CDROMEJECT_SW ioctl (off by default).
 80  *                       Read total cdrom capacity during open.
 81  *                       Rearrange logic in cdrom_decode_status.  Issue
 82  *                        request sense commands for failed packet commands
 83  *                        from here instead of from cdrom_queue_packet_command.
 84  *                        Fix a race condition in retrieving error information.
 85  *                       Suppress printing normal unit attention errors and
 86  *                        some drive not ready errors.
 87  *                       Implement CDROMVOLREAD ioctl.
 88  *                       Implement CDROMREADMODE1/2 ioctls.
 89  *                       Fix race condition in setting up interrupt handlers
 90  *                        when the `serialize' option is used.
 91  * 3.01  Sep  2, 1995 -- Fix ordering of reenabling interrupts in
 92  *                        cdrom_queue_request.
 93  *                       Another try at using ide_[input,output]_data.
 94  * 3.02  Sep 16, 1995 -- Stick total disk capacity in partition table as well.
 95  *                       Make VERBOSE_IDE_CD_ERRORS dump failed command again.
 96  *                       Dump out more information for ILLEGAL REQUEST errs.
 97  *                       Fix handling of errors occurring before the
 98  *                        packet command is transferred.
 99  *                       Fix transfers with odd bytelengths.
100  * 3.03  Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
101  *                       `DCI-2S10' drives are broken too.
102  * 3.04  Nov 20, 1995 -- So are Vertos drives.
103  * 3.05  Dec  1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
104  * 3.06  Dec 16, 1995 -- Add support needed for partitions.
105  *                       More workarounds for Vertos bugs (based on patches
106  *                        from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
107  *                       Try to eliminate byteorder assumptions.
108  *                       Use atapi_cdrom_subchnl struct definition.
109  *                       Add STANDARD_ATAPI compilation option.
110  * 3.07  Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
111  *                        Vertos 300.
112  *                       Add NO_DOOR_LOCKING configuration option.
113  *                       Handle drive_cmd requests w/NULL args (for hdparm -t).
114  *                       Work around sporadic Sony55e audio play problem.
115  * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
116  *                        problem with "hde=cdrom" with no drive present.  -ml
117  * 3.08  Mar  6, 1996 -- More Vertos workarounds.
118  * 3.09  Apr  5, 1996 -- Add CDROMCLOSETRAY ioctl.
119  *                       Switch to using MSF addressing for audio commands.
120  *                       Reformat to match kernel tabbing style.
121  *                       Add CDROM_GET_UPC ioctl.
122  * 3.10  Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
123  * 3.11  Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
124  *                       to remove redundant verify_area calls.
125  * 3.12  May  7, 1996 -- Rudimentary changer support.  Based on patches
126  *                        from Gerhard Zuber <zuber@berlin.snafu.de>.
127  *                       Let open succeed even if there's no loaded disc.
128  * 3.13  May 19, 1996 -- Fixes for changer code.
129  * 3.14  May 29, 1996 -- Add work-around for Vertos 600.
130  *                        (From Hennus Bergman <hennus@sky.ow.nl>.)
131  * 3.15  July 2, 1996 -- Added support for Sanyo 3 CD changers
132  *                        from Ben Galliart <bgallia@luc.edu> with 
133  *                        special help from Jeff Lightfoot 
134  *                        <jeffml@pobox.com>
135  * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
136  * 3.16  Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
137  * 3.17  Sep 17, 1996 -- Tweak audio reads for some drives.
138  *                       Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
139  * 3.18  Oct 31, 1996 -- Added module and DMA support.
140  *                       
141  *                       
142  * 4.00  Nov 5, 1996   -- New ide-cd maintainer,
143  *                                 Erik B. Andersen <andersee@debian.org>
144  *                     -- Newer Creative drives don't always set the error
145  *                          register correctly.  Make sure we see media changes
146  *                          regardless.
147  *                     -- Integrate with generic cdrom driver.
148  *                     -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
149  *                          a patch from Ciro Cattuto <>.
150  *                     -- Call set_device_ro.
151  *                     -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
152  *                          ioctls, based on patch by Erik Andersen
153  *                     -- Add some probes of drive capability during setup.
154  *
155  * 4.01  Nov 11, 1996  -- Split into ide-cd.c and ide-cd.h
156  *                     -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE 
157  *                          ioctls in favor of a generalized approach 
158  *                          using the generic cdrom driver.
159  *                     -- Fully integrated with the 2.1.X kernel.
160  *                     -- Other stuff that I forgot (lots of changes)
161  *
162  * 4.02  Dec 01, 1996  -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
163  *                          to fix the drive door locking problems.
164  *
165  * 4.03  Dec 04, 1996  -- Added DSC overlap support.
166  * 4.04  Dec 29, 1996  -- Added CDROMREADRAW ioclt based on patch 
167  *                          by Ales Makarov (xmakarov@sun.felk.cvut.cz)
168  *
169  * 4.05  Nov 20, 1997  -- Modified to print more drive info on init
170  *                        Minor other changes
171  *                        Fix errors on CDROMSTOP (If you have a "Dolphin",
172  *                          you must define IHAVEADOLPHIN)
173  *                        Added identifier so new Sanyo CD-changer works
174  *                        Better detection if door locking isn't supported
175  *
176  * 4.06  Dec 17, 1997  -- fixed endless "tray open" messages  -ml
177  * 4.07  Dec 17, 1997  -- fallback to set pc->stat on "tray open"
178  * 4.08  Dec 18, 1997  -- spew less noise when tray is empty
179  *                     -- fix speed display for ACER 24X, 18X
180  * 4.09  Jan 04, 1998  -- fix handling of the last block so we return
181  *                         an end of file instead of an I/O error (Gadi)
182  * 4.10  Jan 24, 1998  -- fixed a bug so now changers can change to a new
183  *                         slot when there is no disc in the current slot.
184  *                     -- Fixed a memory leak where info->changer_info was
185  *                         malloc'ed but never free'd when closing the device.
186  *                     -- Cleaned up the global namespace a bit by making more
187  *                         functions static that should already have been.
188  * 4.11  Mar 12, 1998  -- Added support for the CDROM_SELECT_SPEED ioctl
189  *                         based on a patch for 2.0.33 by Jelle Foks 
190  *                         <jelle@scintilla.utwente.nl>, a patch for 2.0.33
191  *                         by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
192  *                         version, and my own efforts.  -erik
193  *                     -- Fixed a stupid bug which egcs was kind enough to
194  *                         inform me of where "Illegal mode for this track"
195  *                         was never returned due to a comparison on data
196  *                         types of limited range.
197  * 4.12  Mar 29, 1998  -- Fixed bug in CDROM_SELECT_SPEED so write speed is 
198  *                         now set ionly for CD-R and CD-RW drives.  I had 
199  *                         removed this support because it produced errors.
200  *                         It produced errors _only_ for non-writers. duh.
201  * 4.13  May 05, 1998  -- Suppress useless "in progress of becoming ready"
202  *                         messages, since this is not an error.
203  *                     -- Change error messages to be const
204  *                     -- Remove a "\t" which looks ugly in the syslogs
205  * 4.14  July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
206  *                         since the .pdf version doesn't seem to work...
207  *                     -- Updated the TODO list to something more current.
208  *
209  * 4.15  Aug 25, 1998  -- Updated ide-cd.h to respect mechine endianess, 
210  *                         patch thanks to "Eddie C. Dost" <ecd@skynet.be>
211  *
212  * 4.50  Oct 19, 1998  -- New maintainers!
213  *                         Jens Axboe <axboe@image.dk>
214  *                         Chris Zwilling <chris@cloudnet.com>
215  *
216  * 4.51  Dec 23, 1998  -- Jens Axboe <axboe@image.dk>
217  *                      - ide_cdrom_reset enabled since the ide subsystem
218  *                         handles resets fine now. <axboe@image.dk>
219  *                      - Transfer size fix for Samsung CD-ROMs, thanks to
220  *                        "Ville Hallik" <ville.hallik@mail.ee>.
221  *                      - other minor stuff.
222  *
223  * 4.52  Jan 19, 1999  -- Jens Axboe <axboe@image.dk>
224  *                      - Detect DVD-ROM/RAM drives
225  *
226  * 4.53  Feb 22, 1999   - Include other model Samsung and one Goldstar
227  *                         drive in transfer size limit.
228  *                      - Fix the I/O error when doing eject without a medium
229  *                         loaded on some drives.
230  *                      - CDROMREADMODE2 is now implemented through
231  *                         CDROMREADRAW, since many drives don't support
232  *                         MODE2 (even though ATAPI 2.6 says they must).
233  *                      - Added ignore parameter to ide-cd (as a module), eg
234  *                              insmod ide-cd ignore='hda hdb'
235  *                         Useful when using ide-cd in conjunction with
236  *                         ide-scsi. TODO: non-modular way of doing the
237  *                         same.
238  *
239  * 4.54  Aug 5, 1999    - Support for MMC2 class commands through the generic
240  *                        packet interface to cdrom.c.
241  *                      - Unified audio ioctl support, most of it.
242  *                      - cleaned up various deprecated verify_area().
243  *                      - Added ide_cdrom_packet() as the interface for
244  *                        the Uniform generic_packet().
245  *                      - bunch of other stuff, will fill in logs later.
246  *                      - report 1 slot for non-changers, like the other
247  *                        cd-rom drivers. don't report select disc for
248  *                        non-changers as well.
249  *                      - mask out audio playing, if the device can't do it.
250  *
251  * 4.55  Sep 1, 1999    - Eliminated the rest of the audio ioctls, except
252  *                        for CDROMREADTOC[ENTRY|HEADER]. Some of the drivers
253  *                        use this independently of the actual audio handling.
254  *                        They will disappear later when I get the time to
255  *                        do it cleanly.
256  *                      - Minimize the TOC reading - only do it when we
257  *                        know a media change has occured.
258  *                      - Moved all the CDROMREADx ioctls to the Uniform layer.
259  *                      - Heiko Eissfeldt <heiko@colossus.escape.de> supplied
260  *                        some fixes for CDI.
261  *                      - CD-ROM leaving door locked fix from Andries
262  *                        Brouwer <Andries.Brouwer@cwi.nl>
263  *                      - Erik Andersen <andersen@xmission.com> unified
264  *                        commands across the various drivers and how
265  *                        sense errors are handled.
266  *
267  * 4.56  Sep 12, 1999   - Removed changer support - it is now in the
268  *                        Uniform layer.
269  *                      - Added partition based multisession handling.
270  *                      - Mode sense and mode select moved to the
271  *                        Uniform layer.
272  *                      - Fixed a problem with WPI CDS-32X drive - it
273  *                        failed the capabilities 
274  *
275  * 4.57  Apr 7, 2000    - Fixed sense reporting.
276  *                      - Fixed possible oops in ide_cdrom_get_last_session()
277  *                      - Fix locking mania and make ide_cdrom_reset relock
278  *                      - Stop spewing errors to log when magicdev polls with
279  *                        TEST_UNIT_READY on some drives.
280  *                      - Various fixes from Tobias Ringstrom:
281  *                        tray if it was locked prior to the reset.
282  *                        - cdrom_read_capacity returns one frame too little.
283  *                        - Fix real capacity reporting.
284  *
285  * 4.58  May 1, 2000    - Clean up ACER50 stuff.
286  *                      - Fix small problem with ide_cdrom_capacity
287  *
288  * 4.59  Aug 11, 2000   - Fix changer problem in cdrom_read_toc, we weren't
289  *                        correctly sensing a disc change.
290  *                      - Rearranged some code
291  *                      - Use extended sense on drives that support it for
292  *                        correctly reporting tray status -- from
293  *                        Michael D Johnson <johnsom@orst.edu>
294  *
295  *************************************************************************/
296  
297 #define IDECD_VERSION "4.59"
298 
299 #include <linux/config.h>
300 #include <linux/module.h>
301 #include <linux/types.h>
302 #include <linux/kernel.h>
303 #include <linux/delay.h>
304 #include <linux/timer.h>
305 #include <linux/malloc.h>
306 #include <linux/interrupt.h>
307 #include <linux/errno.h>
308 #include <linux/cdrom.h>
309 #include <linux/ide.h>
310 
311 #include <asm/irq.h>
312 #include <asm/io.h>
313 #include <asm/byteorder.h>
314 #include <asm/uaccess.h>
315 #include <asm/unaligned.h>
316 
317 #include "ide-cd.h"
318 
319 /****************************************************************************
320  * Generic packet command support and error handling routines.
321  */
322 
323 /* Mark that we've seen a media change, and invalidate our internal
324    buffers. */
325 static void cdrom_saw_media_change (ide_drive_t *drive)
326 {
327         struct cdrom_info *info = drive->driver_data;
328         
329         CDROM_STATE_FLAGS (drive)->media_changed = 1;
330         CDROM_STATE_FLAGS (drive)->toc_valid = 0;
331         info->nsectors_buffered = 0;
332 }
333 
334 static int cdrom_log_sense(ide_drive_t *drive, struct packet_command *pc,
335                            struct request_sense *sense)
336 {
337         int log = 0;
338 
339         if (sense == NULL || pc == NULL || pc->quiet)
340                 return 0;
341 
342         switch (sense->sense_key) {
343                 case NO_SENSE: case RECOVERED_ERROR:
344                         break;
345                 case NOT_READY:
346                         /*
347                          * don't care about tray state messages for
348                          * e.g. capacity commands or in-progress or
349                          * becoming ready
350                          */
351                         if (sense->asc == 0x3a || sense->asc == 0x04)
352                                 break;
353                         log = 1;
354                         break;
355                 case UNIT_ATTENTION:
356                         /*
357                          * Make good and sure we've seen this potential media
358                          * change. Some drives (i.e. Creative) fail to present
359                          * the correct sense key in the error register.
360                          */
361                         cdrom_saw_media_change(drive);
362                         break;
363                 default:
364                         log = 1;
365                         break;
366         }
367         return log;
368 }
369 
370 static
371 void cdrom_analyze_sense_data(ide_drive_t *drive,
372                               struct packet_command *failed_command,
373                               struct request_sense *sense)
374 {
375 
376         if (!cdrom_log_sense(drive, failed_command, sense))
377                 return;
378 
379         /*
380          * If a read toc is executed for a CD-R or CD-RW medium where
381          * the first toc has not been recorded yet, it will fail with
382          * 05/24/00 (which is a confusing error)
383          */
384         if (failed_command && failed_command->c[0] == GPCMD_READ_TOC_PMA_ATIP)
385                 if (sense->sense_key == 0x05 && sense->asc == 0x24)
386                         return;
387 
388 #if VERBOSE_IDE_CD_ERRORS
389         {
390                 int i;
391                 const char *s;
392                 char buf[80];
393 
394                 printk ("ATAPI device %s:\n", drive->name);
395                 if (sense->error_code==0x70)
396                         printk("  Error: ");
397                 else if (sense->error_code==0x71)
398                         printk("  Deferred Error: ");
399                 else if (sense->error_code == 0x7f)
400                         printk("  Vendor-specific Error: ");
401                 else
402                         printk("  Unknown Error Type: ");
403 
404                 if (sense->sense_key < ARY_LEN(sense_key_texts))
405                         s = sense_key_texts[sense->sense_key];
406                 else
407                         s = "bad sense key!";
408 
409                 printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
410 
411                 if (sense->asc == 0x40) {
412                         sprintf(buf, "Diagnostic failure on component 0x%02x",
413                                  sense->ascq);
414                         s = buf;
415                 } else {
416                         int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
417                         unsigned long key = (sense->sense_key << 16);
418                         key |= (sense->asc << 8);
419                         if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
420                                 key |= sense->ascq;
421                         s = NULL;
422 
423                         while (hi > lo) {
424                                 mid = (lo + hi) / 2;
425                                 if (sense_data_texts[mid].asc_ascq == key ||
426                                     sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
427                                         s = sense_data_texts[mid].text;
428                                         break;
429                                 }
430                                 else if (sense_data_texts[mid].asc_ascq > key)
431                                         hi = mid;
432                                 else
433                                         lo = mid+1;
434                         }
435                 }
436 
437                 if (s == NULL) {
438                         if (sense->asc > 0x80)
439                                 s = "(vendor-specific error)";
440                         else
441                                 s = "(reserved error code)";
442                 }
443 
444                 printk("  %s -- (asc=0x%02x, ascq=0x%02x)\n",
445                         s, sense->asc, sense->ascq);
446 
447                 if (failed_command != NULL) {
448 
449                         int lo=0, mid, hi= ARY_LEN (packet_command_texts);
450                         s = NULL;
451 
452                         while (hi > lo) {
453                                 mid = (lo + hi) / 2;
454                                 if (packet_command_texts[mid].packet_command ==
455                                     failed_command->c[0]) {
456                                         s = packet_command_texts[mid].text;
457                                         break;
458                                 }
459                                 if (packet_command_texts[mid].packet_command >
460                                     failed_command->c[0])
461                                         hi = mid;
462                                 else
463                                         lo = mid+1;
464                         }
465 
466                         printk ("  The failed \"%s\" packet command was: \n  \"", s);
467                         for (i=0; i<sizeof (failed_command->c); i++)
468                                 printk ("%02x ", failed_command->c[i]);
469                         printk ("\"\n");
470                 }
471 
472                 /* The SKSV bit specifies validity of the sense_key_specific
473                  * in the next two commands. It is bit 7 of the first byte.
474                  * In the case of NOT_READY, if SKSV is set the drive can
475                  * give us nice ETA readings.
476                  */
477                 if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
478                         int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
479                         printk("  Command is %02d%% complete\n", progress / 0xffff);
480 
481                 }
482 
483                 if (sense->sense_key == ILLEGAL_REQUEST &&
484                     (sense->sks[0] & 0x80) != 0) {
485                         printk("  Error in %s byte %d",
486                                 (sense->sks[0] & 0x40) != 0 ?
487                                 "command packet" : "command data",
488                                 (sense->sks[1] << 8) + sense->sks[2]);
489 
490                         if ((sense->sks[0] & 0x40) != 0)
491                                 printk (" bit %d", sense->sks[0] & 0x07);
492 
493                         printk ("\n");
494                 }
495         }
496 
497 #else /* not VERBOSE_IDE_CD_ERRORS */
498 
499         /* Suppress printing unit attention and `in progress of becoming ready'
500            errors when we're not being verbose. */
501 
502         if (sense->sense_key == UNIT_ATTENTION ||
503             (sense->sense_key == NOT_READY && (sense->asc == 4 ||
504                                                 sense->asc == 0x3a)))
505                 return;
506 
507         printk("%s: error code: 0x%02x  sense_key: 0x%02x  asc: 0x%02x  ascq: 0x%02x\n",
508                 drive->name,
509                 sense->error_code, sense->sense_key,
510                 sense->asc, sense->ascq);
511 #endif /* not VERBOSE_IDE_CD_ERRORS */
512 }
513 
514 static void cdrom_queue_request_sense(ide_drive_t *drive, 
515                                       struct semaphore *sem,
516                                       struct request_sense *sense,
517                                       struct packet_command *failed_command)
518 {
519         struct cdrom_info *info         = drive->driver_data;
520         struct packet_command *pc       = &info->request_sense_pc;
521         struct request *rq;
522 
523         if (sense == NULL)
524                 sense = &info->sense_data;
525 
526         memset(pc, 0, sizeof(struct packet_command));
527         pc->c[0] = GPCMD_REQUEST_SENSE;
528         pc->c[4] = pc->buflen = 18;
529         pc->buffer = (char *) sense;
530         pc->sense = (struct request_sense *) failed_command;
531 
532         /* stuff the sense request in front of our current request */
533         rq = &info->request_sense_request;
534         ide_init_drive_cmd(rq);
535         rq->cmd = REQUEST_SENSE_COMMAND;
536         rq->buffer = (char *) pc;
537         rq->sem = sem;
538         (void) ide_do_drive_cmd(drive, rq, ide_preempt);
539 }
540 
541 
542 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
543 {
544         struct request *rq = HWGROUP(drive)->rq;
545 
546         if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
547                 struct packet_command *pc = (struct packet_command *) rq->buffer;
548                 cdrom_analyze_sense_data(drive,
549                         (struct packet_command *) pc->sense,
550                         (struct request_sense *) (pc->buffer - pc->c[4]));
551         }
552         if (rq->cmd == READ || rq->cmd == WRITE)
553                 if (!rq->current_nr_sectors)
554                         uptodate = 1;
555 
556         ide_end_request (uptodate, HWGROUP(drive));
557 }
558 
559 
560 /* Returns 0 if the request should be continued.
561    Returns 1 if the request was ended. */
562 static int cdrom_decode_status (ide_startstop_t *startstop, ide_drive_t *drive,
563                                 int good_stat, int *stat_ret)
564 {
565         struct request *rq = HWGROUP(drive)->rq;
566         int stat, err, sense_key;
567         struct packet_command *pc;
568         
569         /* Check for errors. */
570         stat = GET_STAT();
571         *stat_ret = stat;
572 
573         if (OK_STAT (stat, good_stat, BAD_R_STAT))
574                 return 0;
575 
576         /* Get the IDE error register. */
577         err = GET_ERR();
578         sense_key = err >> 4;
579 
580         if (rq == NULL) {
581                 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
582                 *startstop = ide_stopped;
583                 return 1;
584         }
585 
586         if (rq->cmd == REQUEST_SENSE_COMMAND) {
587                 /* We got an error trying to get sense info
588                    from the drive (probably while trying
589                    to recover from a former error).  Just give up. */
590 
591                 pc = (struct packet_command *) rq->buffer;
592                 pc->stat = 1;
593                 cdrom_end_request (1, drive);
594                 *startstop = ide_error (drive, "request sense failure", stat);
595                 return 1;
596 
597         } else if (rq->cmd == PACKET_COMMAND) {
598                 /* All other functions, except for READ. */
599                 struct semaphore *sem = NULL;
600                 pc = (struct packet_command *) rq->buffer;
601 
602                 /* Check for tray open. */
603                 if (sense_key == NOT_READY) {
604                         cdrom_saw_media_change (drive);
605                 } else if (sense_key == UNIT_ATTENTION) {
606                         /* Check for media change. */
607                         cdrom_saw_media_change (drive);
608                         /*printk("%s: media changed\n",drive->name);*/
609                         return 0;
610                 } else if (!pc->quiet) {
611                         /* Otherwise, print an error. */
612                         ide_dump_status(drive, "packet command error", stat);
613                 }
614                 
615                 /* Set the error flag and complete the request.
616                    Then, if we have a CHECK CONDITION status,
617                    queue a request sense command.  We must be careful,
618                    though: we don't want the thread in
619                    cdrom_queue_packet_command to wake up until
620                    the request sense has completed.  We do this
621                    by transferring the semaphore from the packet
622                    command request to the request sense request. */
623 
624                 if ((stat & ERR_STAT) != 0) {
625                         sem = rq->sem;
626                         rq->sem = NULL;
627                 }
628 
629                 pc->stat = 1;
630                 cdrom_end_request (1, drive);
631 
632                 if ((stat & ERR_STAT) != 0)
633                         cdrom_queue_request_sense(drive, sem, pc->sense, pc);
634         } else {
635                 /* Handle errors from READ and WRITE requests. */
636 
637                 if (sense_key == NOT_READY) {
638                         /* Tray open. */
639                         cdrom_saw_media_change (drive);
640 
641                         /* Fail the request. */
642                         printk ("%s: tray open\n", drive->name);
643                         cdrom_end_request (0, drive);
644                 } else if (sense_key == UNIT_ATTENTION) {
645                         /* Media change. */
646                         cdrom_saw_media_change (drive);
647 
648                         /* Arrange to retry the request.
649                            But be sure to give up if we've retried
650                            too many times. */
651                         if (++rq->errors > ERROR_MAX)
652                                 cdrom_end_request (0, drive);
653                 } else if (sense_key == ILLEGAL_REQUEST ||
654                            sense_key == DATA_PROTECT) {
655                         /* No point in retrying after an illegal
656                            request or data protect error.*/
657                         ide_dump_status (drive, "command error", stat);
658                         cdrom_end_request (0, drive);
659                 } else if ((err & ~ABRT_ERR) != 0) {
660                         /* Go to the default handler
661                            for other errors. */
662                         *startstop = ide_error (drive, "cdrom_decode_status", stat);
663                         return 1;
664                 } else if ((++rq->errors > ERROR_MAX)) {
665                         /* We've racked up too many retries.  Abort. */
666                         cdrom_end_request (0, drive);
667                 }
668 
669                 /* If we got a CHECK_CONDITION status,
670                    queue a request sense command. */
671                 if ((stat & ERR_STAT) != 0)
672                         cdrom_queue_request_sense(drive, NULL, NULL, NULL);
673         }
674 
675         /* Retry, or handle the next request. */
676         *startstop = ide_stopped;
677         return 1;
678 }
679 
680 static int cdrom_timer_expiry(ide_drive_t *drive)
681 {
682         struct request *rq = HWGROUP(drive)->rq;
683         struct packet_command *pc = (struct packet_command *) rq->buffer;
684         unsigned long wait = 0;
685 
686         /*
687          * Some commands are *slow* and normally take a long time to
688          * complete. Usually we can use the ATAPI "disconnect" to bypass
689          * this, but not all commands/drives support that. Let
690          * ide_timer_expiry keep polling us for these.
691          */
692         switch (pc->c[0]) {
693                 case GPCMD_BLANK:
694                 case GPCMD_FORMAT_UNIT:
695                 case GPCMD_RESERVE_RZONE_TRACK:
696                         wait = WAIT_CMD;
697                         break;
698                 default:
699                         wait = 0;
700                         break;
701         }
702         return wait;
703 }
704 
705 /* Set up the device registers for transferring a packet command on DEV,
706    expecting to later transfer XFERLEN bytes.  HANDLER is the routine
707    which actually transfers the command to the drive.  If this is a
708    drq_interrupt device, this routine will arrange for HANDLER to be
709    called when the interrupt from the drive arrives.  Otherwise, HANDLER
710    will be called immediately after the drive is prepared for the transfer. */
711 
712 static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
713                                                   int xferlen,
714                                                   ide_handler_t *handler)
715 {
716         ide_startstop_t startstop;
717         struct cdrom_info *info = drive->driver_data;
718 
719         /* Wait for the controller to be idle. */
720         if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
721                 return startstop;
722 
723         if (info->dma) {
724                 if (info->cmd == READ) {
725                         info->dma = !HWIF(drive)->dmaproc(ide_dma_read, drive);
726                 } else if (info->cmd == WRITE) {
727                         info->dma = !HWIF(drive)->dmaproc(ide_dma_write, drive);
728                 } else {
729                         printk("ide-cd: DMA set, but not allowed\n");
730                 }
731         }
732 
733         /* Set up the controller registers. */
734         OUT_BYTE (info->dma, IDE_FEATURE_REG);
735         OUT_BYTE (0, IDE_NSECTOR_REG);
736         OUT_BYTE (0, IDE_SECTOR_REG);
737 
738         OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
739         OUT_BYTE (xferlen >> 8  , IDE_HCYL_REG);
740         if (IDE_CONTROL_REG)
741                 OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
742  
743         if (info->dma)
744                 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
745 
746         if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
747                 ide_set_handler (drive, handler, WAIT_CMD, cdrom_timer_expiry);
748                 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
749                 return ide_started;
750         } else {
751                 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
752                 return (*handler) (drive);
753         }
754 }
755 
756 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
757    The device registers must have already been prepared
758    by cdrom_start_packet_command.
759    HANDLER is the interrupt handler to call when the command completes
760    or there's data ready. */
761 /*
762  * changed 5 parameters to 3 for dvd-ram
763  * struct packet_command *pc; now packet_command_t *pc;
764  */
765 static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
766                                           struct packet_command *pc,
767                                           ide_handler_t *handler)
768 {
769         unsigned char *cmd_buf  = pc->c;
770         int cmd_len             = sizeof(pc->c);
771         unsigned int timeout    = pc->timeout;
772         ide_startstop_t startstop;
773 
774         if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
775                 /* Here we should have been called after receiving an interrupt
776                    from the device.  DRQ should how be set. */
777                 int stat_dum;
778 
779                 /* Check for errors. */
780                 if (cdrom_decode_status (&startstop, drive, DRQ_STAT, &stat_dum))
781                         return startstop;
782         } else {
783                 /* Otherwise, we must wait for DRQ to get set. */
784                 if (ide_wait_stat (&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
785                         return startstop;
786         }
787 
788         /* Arm the interrupt handler. */
789         ide_set_handler (drive, handler, timeout, cdrom_timer_expiry);
790 
791         /* Send the command to the device. */
792         atapi_output_bytes (drive, cmd_buf, cmd_len);
793         return ide_started;
794 }
795 
796 /****************************************************************************
797  * Block read functions.
798  */
799 
800 /*
801  * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
802  * buffer.  Once the first sector is added, any subsequent sectors are
803  * assumed to be continuous (until the buffer is cleared).  For the first
804  * sector added, SECTOR is its sector number.  (SECTOR is then ignored until
805  * the buffer is cleared.)
806  */
807 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
808                                   int sectors_to_transfer)
809 {
810         struct cdrom_info *info = drive->driver_data;
811 
812         /* Number of sectors to read into the buffer. */
813         int sectors_to_buffer = MIN (sectors_to_transfer,
814                                      (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
815                                        info->nsectors_buffered);
816 
817         char *dest;
818 
819         /* If we couldn't get a buffer, don't try to buffer anything... */
820         if (info->buffer == NULL)
821                 sectors_to_buffer = 0;
822 
823         /* If this is the first sector in the buffer, remember its number. */
824         if (info->nsectors_buffered == 0)
825                 info->sector_buffered = sector;
826 
827         /* Read the data into the buffer. */
828         dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
829         while (sectors_to_buffer > 0) {
830                 atapi_input_bytes (drive, dest, SECTOR_SIZE);
831                 --sectors_to_buffer;
832                 --sectors_to_transfer;
833                 ++info->nsectors_buffered;
834                 dest += SECTOR_SIZE;
835         }
836 
837         /* Throw away any remaining data. */
838         while (sectors_to_transfer > 0) {
839                 char dum[SECTOR_SIZE];
840                 atapi_input_bytes (drive, dum, sizeof (dum));
841                 --sectors_to_transfer;
842         }
843 }
844 
845 /*
846  * Check the contents of the interrupt reason register from the cdrom
847  * and attempt to recover if there are problems.  Returns  0 if everything's
848  * ok; nonzero if the request has been terminated.
849  */
850 static inline
851 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
852 {
853         ireason &= 3;
854         if (ireason == 2) return 0;
855 
856         if (ireason == 0) {
857                 /* Whoops... The drive is expecting to receive data from us! */
858                 printk ("%s: cdrom_read_intr: "
859                         "Drive wants to transfer data the wrong way!\n",
860                         drive->name);
861 
862                 /* Throw some data at the drive so it doesn't hang
863                    and quit this request. */
864                 while (len > 0) {
865                         int dum = 0;
866                         atapi_output_bytes (drive, &dum, sizeof (dum));
867                         len -= sizeof (dum);
868                 }
869         } else  if (ireason == 1) {
870                 /* Some drives (ASUS) seem to tell us that status
871                  * info is available. just get it and ignore.
872                  */
873                 GET_STAT();
874                 return 0;
875         } else {
876                 /* Drive wants a command packet, or invalid ireason... */
877                 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
878                         drive->name, ireason);
879         }
880 
881         cdrom_end_request (0, drive);
882         return -1;
883 }
884 
885 /*
886  * Interrupt routine.  Called when a read request has completed.
887  */
888 static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
889 {
890         int stat;
891         int ireason, len, sectors_to_transfer, nskip;
892         struct cdrom_info *info = drive->driver_data;
893         int i, dma = info->dma, dma_error = 0;
894         ide_startstop_t startstop;
895 
896         struct request *rq = HWGROUP(drive)->rq;
897 
898         /* Check for errors. */
899         if (dma) {
900                 info->dma = 0;
901                 if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))
902                         HWIF(drive)->dmaproc(ide_dma_off, drive);
903         }
904 
905         if (cdrom_decode_status (&startstop, drive, 0, &stat))
906                 return startstop;
907  
908         if (dma) {
909                 if (!dma_error) {
910                         for (i = rq->nr_sectors; i > 0;) {
911                                 i -= rq->current_nr_sectors;
912                                 ide_end_request(1, HWGROUP(drive));
913                         }
914                         return ide_stopped;
915                 } else
916                         return ide_error (drive, "dma error", stat);
917         }
918 
919         /* Read the interrupt reason and the transfer length. */
920         ireason = IN_BYTE (IDE_NSECTOR_REG);
921         len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
922 
923         /* If DRQ is clear, the command has completed. */
924         if ((stat & DRQ_STAT) == 0) {
925                 /* If we're not done filling the current buffer, complain.
926                    Otherwise, complete the command normally. */
927                 if (rq->current_nr_sectors > 0) {
928                         printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
929                                 drive->name, rq->current_nr_sectors);
930                         cdrom_end_request (0, drive);
931                 } else
932                         cdrom_end_request (1, drive);
933                 return ide_stopped;
934         }
935 
936         /* Check that the drive is expecting to do the same thing we are. */
937         if (cdrom_read_check_ireason (drive, len, ireason))
938                 return ide_stopped;
939 
940         /* Assume that the drive will always provide data in multiples
941            of at least SECTOR_SIZE, as it gets hairy to keep track
942            of the transfers otherwise. */
943         if ((len % SECTOR_SIZE) != 0) {
944                 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
945                         drive->name, len);
946                 if (CDROM_CONFIG_FLAGS (drive)->limit_nframes)
947                         printk ("  This drive is not supported by this version of the driver\n");
948                 else {
949                         printk ("  Trying to limit transfer sizes\n");
950                         CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
951                 }
952                 cdrom_end_request (0, drive);
953                 return ide_stopped;
954         }
955 
956         /* The number of sectors we need to read from the drive. */
957         sectors_to_transfer = len / SECTOR_SIZE;
958 
959         /* First, figure out if we need to bit-bucket
960            any of the leading sectors. */
961         nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
962                      sectors_to_transfer);
963 
964         while (nskip > 0) {
965                 /* We need to throw away a sector. */
966                 char dum[SECTOR_SIZE];
967                 atapi_input_bytes (drive, dum, sizeof (dum));
968 
969                 --rq->current_nr_sectors;
970                 --nskip;
971                 --sectors_to_transfer;
972         }
973 
974         /* Now loop while we still have data to read from the drive. */
975         while (sectors_to_transfer > 0) {
976                 int this_transfer;
977 
978                 /* If we've filled the present buffer but there's another
979                    chained buffer after it, move on. */
980                 if (rq->current_nr_sectors == 0 &&
981                     rq->nr_sectors > 0)
982                         cdrom_end_request (1, drive);
983 
984                 /* If the buffers are full, cache the rest of the data in our
985                    internal buffer. */
986                 if (rq->current_nr_sectors == 0) {
987                         cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
988                         sectors_to_transfer = 0;
989                 } else {
990                         /* Transfer data to the buffers.
991                            Figure out how many sectors we can transfer
992                            to the current buffer. */
993                         this_transfer = MIN (sectors_to_transfer,
994                                              rq->current_nr_sectors);
995 
996                         /* Read this_transfer sectors
997                            into the current buffer. */
998                         while (this_transfer > 0) {
999                                 atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
1000                                 rq->buffer += SECTOR_SIZE;
1001                                 --rq->nr_sectors;
1002                                 --rq->current_nr_sectors;
1003                                 ++rq->sector;
1004                                 --this_transfer;
1005                                 --sectors_to_transfer;
1006                         }
1007                 }
1008         }
1009 
1010         /* Done moving data!
1011            Wait for another interrupt. */
1012         ide_set_handler(drive, &cdrom_read_intr, WAIT_CMD, NULL);
1013         return ide_started;
1014 }
1015 
1016 /*
1017  * Try to satisfy some of the current read request from our cached data.
1018  * Returns nonzero if the request has been completed, zero otherwise.
1019  */
1020 static int cdrom_read_from_buffer (ide_drive_t *drive)
1021 {
1022         struct cdrom_info *info = drive->driver_data;
1023         struct request *rq = HWGROUP(drive)->rq;
1024 
1025         /* Can't do anything if there's no buffer. */
1026         if (info->buffer == NULL) return 0;
1027 
1028         /* Loop while this request needs data and the next block is present
1029            in our cache. */
1030         while (rq->nr_sectors > 0 &&
1031                rq->sector >= info->sector_buffered &&
1032                rq->sector < info->sector_buffered + info->nsectors_buffered) {
1033                 if (rq->current_nr_sectors == 0)
1034                         cdrom_end_request (1, drive);
1035 
1036                 memcpy (rq->buffer,
1037                         info->buffer +
1038                         (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1039                         SECTOR_SIZE);
1040                 rq->buffer += SECTOR_SIZE;
1041                 --rq->current_nr_sectors;
1042                 --rq->nr_sectors;
1043                 ++rq->sector;
1044         }
1045 
1046         /* If we've satisfied the current request,
1047            terminate it successfully. */
1048         if (rq->nr_sectors == 0) {
1049                 cdrom_end_request (1, drive);
1050                 return -1;
1051         }
1052 
1053         /* Move on to the next buffer if needed. */
1054         if (rq->current_nr_sectors == 0)
1055                 cdrom_end_request (1, drive);
1056 
1057         /* If this condition does not hold, then the kluge i use to
1058            represent the number of sectors to skip at the start of a transfer
1059            will fail.  I think that this will never happen, but let's be
1060            paranoid and check. */
1061         if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1062             (rq->sector % SECTORS_PER_FRAME) != 0) {
1063                 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1064                         drive->name, rq->sector);
1065                 cdrom_end_request (0, drive);
1066                 return -1;
1067         }
1068 
1069         return 0;
1070 }
1071 
1072 /*
1073  * Routine to send a read packet command to the drive.
1074  * This is usually called directly from cdrom_start_read.
1075  * However, for drq_interrupt devices, it is called from an interrupt
1076  * when the drive is ready to accept the command.
1077  */
1078 static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1079 {
1080         struct packet_command pc;
1081         struct request *rq = HWGROUP(drive)->rq;
1082         int nsect, sector, nframes, frame, nskip;
1083 
1084         /* Number of sectors to transfer. */
1085         nsect = rq->nr_sectors;
1086 
1087         /* Starting sector. */
1088         sector = rq->sector;
1089 
1090         /* If the requested sector doesn't start on a cdrom block boundary,
1091            we must adjust the start of the transfer so that it does,
1092            and remember to skip the first few sectors.
1093            If the CURRENT_NR_SECTORS field is larger than the size
1094            of the buffer, it will mean that we're to skip a number
1095            of sectors equal to the amount by which CURRENT_NR_SECTORS
1096            is larger than the buffer size. */
1097         nskip = (sector % SECTORS_PER_FRAME);
1098         if (nskip > 0) {
1099                 /* Sanity check... */
1100                 if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS) &&
1101                         (rq->sector % CD_FRAMESIZE != 0)) {
1102                         printk ("%s: cdrom_start_read_continuation: buffer botch (%lu)\n",
1103                                 drive->name, rq->current_nr_sectors);
1104                         cdrom_end_request (0, drive);
1105                         return ide_stopped;
1106                 }
1107                 sector -= nskip;
1108                 nsect += nskip;
1109                 rq->current_nr_sectors += nskip;
1110         }
1111 
1112         /* Convert from sectors to cdrom blocks, rounding up the transfer
1113            length if needed. */
1114         nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1115         frame = sector / SECTORS_PER_FRAME;
1116 
1117         /* Largest number of frames was can transfer at once is 64k-1. For
1118            some drives we need to limit this even more. */
1119         nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ?
1120                 (65534 / CD_FRAMESIZE) : 65535);
1121 
1122         /* Set up the command */
1123         memset (&pc.c, 0, sizeof (pc.c));
1124         pc.c[0] = GPCMD_READ_10;
1125         pc.c[7] = (nframes >> 8);
1126         pc.c[8] = (nframes & 0xff);
1127         put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1128         pc.timeout = WAIT_CMD;
1129 
1130         /* Send the command to the drive and return. */
1131         return cdrom_transfer_packet_command(drive, &pc, &cdrom_read_intr);
1132 }
1133 
1134 
1135 #define IDECD_SEEK_THRESHOLD    (1000)                  /* 1000 blocks */
1136 #define IDECD_SEEK_TIMER        (5 * WAIT_MIN_SLEEP)    /* 100 ms */
1137 #define IDECD_SEEK_TIMEOUT     WAIT_CMD                 /* 10 sec */
1138 
1139 static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1140 {
1141         struct cdrom_info *info = drive->driver_data;
1142         int stat;
1143         static int retry = 10;
1144         ide_startstop_t startstop;
1145 
1146         if (cdrom_decode_status (&startstop, drive, 0, &stat))
1147                 return startstop;
1148         CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1149 
1150         if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {
1151                 if (--retry == 0) {
1152                         /*
1153                          * this condition is far too common, to bother
1154                          * users about it
1155                          */
1156 #if 0
1157                         printk("%s: disabled DSC seek overlap\n", drive->name);
1158 #endif
1159                         drive->dsc_overlap = 0;
1160                 }
1161         }
1162         return ide_stopped;
1163 }
1164 
1165 static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1166 {
1167         struct packet_command pc;
1168         struct request *rq = HWGROUP(drive)->rq;
1169         int sector, frame, nskip;
1170 
1171         sector = rq->sector;
1172         nskip = (sector % SECTORS_PER_FRAME);
1173         if (nskip > 0)
1174                 sector -= nskip;
1175         frame = sector / SECTORS_PER_FRAME;
1176 
1177         memset (&pc.c, 0, sizeof (pc.c));
1178         pc.c[0] = GPCMD_SEEK;
1179         put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1180 
1181         pc.timeout = WAIT_CMD;
1182         return cdrom_transfer_packet_command(drive, &pc, &cdrom_seek_intr);
1183 }
1184 
1185 static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1186 {
1187         struct cdrom_info *info = drive->driver_data;
1188 
1189         info->dma = 0;
1190         info->cmd = 0;
1191         info->start_seek = jiffies;
1192         return cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);
1193 }
1194 
1195 /* Fix up a possibly partially-processed request so that we can
1196    start it over entirely, or even put it back on the request queue. */
1197 static void restore_request (struct request *rq)
1198 {
1199         if (rq->buffer != rq->bh->b_data) {
1200                 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
1201                 rq->buffer = rq->bh->b_data;
1202                 rq->nr_sectors += n;
1203                 rq->sector -= n;
1204         }
1205         rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
1206 }
1207 
1208 /*
1209  * Start a read request from the CD-ROM.
1210  */
1211 static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1212 {
1213         struct cdrom_info *info = drive->driver_data;
1214         struct request *rq = HWGROUP(drive)->rq;
1215         int minor = MINOR (rq->rq_dev);
1216 
1217         /* If the request is relative to a partition, fix it up to refer to the
1218            absolute address.  */
1219         if ((minor & PARTN_MASK) != 0) {
1220                 rq->sector = block;
1221                 minor &= ~PARTN_MASK;
1222                 rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1223         }
1224 
1225         /* We may be retrying this request after an error.  Fix up
1226            any weirdness which might be present in the request packet. */
1227         restore_request (rq);
1228 
1229         /* Satisfy whatever we can of this request from our cached sector. */
1230         if (cdrom_read_from_buffer(drive))
1231                 return ide_stopped;
1232 
1233         /* Clear the local sector buffer. */
1234         info->nsectors_buffered = 0;
1235 
1236         /* use dma, if possible. */
1237         if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) &&
1238                                 (rq->nr_sectors % SECTORS_PER_FRAME == 0))
1239                 info->dma = 1;
1240         else
1241                 info->dma = 0;
1242 
1243         info->cmd = READ;
1244         /* Start sending the read request to the drive. */
1245         return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1246 }
1247 
1248 /****************************************************************************
1249  * Execute all other packet commands.
1250  */
1251 
1252 /* Forward declarations. */
1253 static int cdrom_lockdoor(ide_drive_t *drive, int lockflag,
1254                           struct request_sense *sense);
1255 
1256 /* Interrupt routine for packet command completion. */
1257 static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1258 {
1259         int ireason, len, stat, thislen;
1260         struct request *rq = HWGROUP(drive)->rq;
1261         struct packet_command *pc = (struct packet_command *)rq->buffer;
1262         ide_startstop_t startstop;
1263 
1264         /* Check for errors. */
1265         if (cdrom_decode_status (&startstop, drive, 0, &stat))
1266                 return startstop;
1267 
1268         /* Read the interrupt reason and the transfer length. */
1269         ireason = IN_BYTE (IDE_NSECTOR_REG);
1270         len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1271 
1272         /* If DRQ is clear, the command has completed.
1273            Complain if we still have data left to transfer. */
1274         if ((stat & DRQ_STAT) == 0) {
1275                 /* Some of the trailing request sense fields are optional, and
1276                    some drives don't send them.  Sigh. */
1277                 if (pc->c[0] == GPCMD_REQUEST_SENSE &&
1278                     pc->buflen > 0 &&
1279                     pc->buflen <= 5) {
1280                         while (pc->buflen > 0) {
1281                                 *pc->buffer++ = 0;
1282                                 --pc->buflen;
1283                         }
1284                 }
1285 
1286                 if (pc->buflen == 0)
1287                         cdrom_end_request (1, drive);
1288                 else {
1289                         /* Comment this out, because this always happens 
1290                            right after a reset occurs, and it is annoying to 
1291                            always print expected stuff.  */
1292                         /*
1293                         printk ("%s: cdrom_pc_intr: data underrun %d\n",
1294                                 drive->name, pc->buflen);
1295                         */
1296                         pc->stat = 1;
1297                         cdrom_end_request (1, drive);
1298                 }
1299                 return ide_stopped;
1300         }
1301 
1302         /* Figure out how much data to transfer. */
1303         thislen = pc->buflen;
1304         if (thislen > len) thislen = len;
1305 
1306         /* The drive wants to be written to. */
1307         if ((ireason & 3) == 0) {
1308                 /* Transfer the data. */
1309                 atapi_output_bytes (drive, pc->buffer, thislen);
1310 
1311                 /* If we haven't moved enough data to satisfy the drive,
1312                    add some padding. */
1313                 while (len > thislen) {
1314                         int dum = 0;
1315                         atapi_output_bytes (drive, &dum, sizeof (dum));
1316                         len -= sizeof (dum);
1317                 }
1318 
1319                 /* Keep count of how much data we've moved. */
1320                 pc->buffer += thislen;
1321                 pc->buflen -= thislen;
1322         }
1323 
1324         /* Same drill for reading. */
1325         else if ((ireason & 3) == 2) {
1326 
1327                 /* Transfer the data. */
1328                 atapi_input_bytes (drive, pc->buffer, thislen);
1329 
1330                 /* If we haven't moved enough data to satisfy the drive,
1331                    add some padding. */
1332                 while (len > thislen) {
1333                         int dum = 0;
1334                         atapi_input_bytes (drive, &dum, sizeof (dum));
1335                         len -= sizeof (dum);
1336                 }
1337 
1338                 /* Keep count of how much data we've moved. */
1339                 pc->buffer += thislen;
1340                 pc->buflen -= thislen;
1341         } else {
1342                 printk ("%s: cdrom_pc_intr: The drive "
1343                         "appears confused (ireason = 0x%2x)\n",
1344                         drive->name, ireason);
1345                 pc->stat = 1;
1346         }
1347 
1348         /* Now we wait for another interrupt. */
1349         ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD, cdrom_timer_expiry);
1350         return ide_started;
1351 }
1352 
1353 
1354 static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1355 {
1356         struct request *rq = HWGROUP(drive)->rq;
1357         struct packet_command *pc = (struct packet_command *)rq->buffer;
1358 
1359         if (!pc->timeout)
1360                 pc->timeout = WAIT_CMD;
1361 
1362         /* Send the command to the drive and return. */
1363         return cdrom_transfer_packet_command(drive, pc, &cdrom_pc_intr);
1364 }
1365 
1366 
1367 static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1368 {
1369         int len;
1370         struct request *rq = HWGROUP(drive)->rq;
1371         struct packet_command *pc = (struct packet_command *)rq->buffer;
1372         struct cdrom_info *info = drive->driver_data;
1373 
1374         info->dma = 0;
1375         info->cmd = 0;
1376         pc->stat = 0;
1377         len = pc->buflen;
1378 
1379         /* Start sending the command to the drive. */
1380         return cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1381 }
1382 
1383 
1384 /* Sleep for TIME jiffies.
1385    Not to be called from an interrupt handler. */
1386 static
1387 void cdrom_sleep (int time)
1388 {
1389         int sleep = time;
1390 
1391         do {
1392                 set_current_state(TASK_INTERRUPTIBLE);
1393                 sleep = schedule_timeout(sleep);
1394         } while (sleep);
1395 }
1396 
1397 static
1398 int cdrom_queue_packet_command(ide_drive_t *drive, struct packet_command *pc)
1399 {
1400         struct request_sense sense;
1401         struct request req;
1402         int retries = 10;
1403 
1404         if (pc->sense == NULL)
1405                 pc->sense = &sense;
1406 
1407         /* Start of retry loop. */
1408         do {
1409                 ide_init_drive_cmd (&req);
1410                 req.cmd = PACKET_COMMAND;
1411                 req.buffer = (char *)pc;
1412                 if (ide_do_drive_cmd (drive, &req, ide_wait)) {
1413                         printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
1414                                 drive->name, req.buffer[0], req.buffer[1]);
1415                         /* FIXME: we should probably abort/retry or something */
1416                 }
1417                 if (pc->stat != 0) {
1418                         /* The request failed.  Retry if it was due to a unit
1419                            attention status
1420                            (usually means media was changed). */
1421                         struct request_sense *reqbuf = pc->sense;
1422 
1423                         if (reqbuf->sense_key == UNIT_ATTENTION)
1424                                 cdrom_saw_media_change (drive);
1425                         else if (reqbuf->sense_key == NOT_READY &&
1426                                  reqbuf->asc == 4 && reqbuf->ascq != 4) {
1427                                 /* The drive is in the process of loading
1428                                    a disk.  Retry, but wait a little to give
1429                                    the drive time to complete the load. */
1430                                 cdrom_sleep(2 * HZ);
1431                         } else {
1432                                 /* Otherwise, don't retry. */
1433                                 retries = 0;
1434                         }
1435                         --retries;
1436                 }
1437 
1438                 /* End of retry loop. */
1439         } while (pc->stat != 0 && retries >= 0);
1440 
1441         /* Return an error if the command failed. */
1442         return pc->stat ? -EIO : 0;
1443 }
1444 
1445 /*
1446  * Write handling
1447  */
1448 static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1449 {
1450         /* Two notes about IDE interrupt reason here - 0 means that
1451          * the drive wants to receive data from us, 2 means that
1452          * the drive is expecting data from us.
1453          */
1454         ireason &= 3;
1455 
1456         if (ireason == 2) {
1457                 /* Whoops... The drive wants to send data. */
1458                 printk("%s: cdrom_write_intr: wrong transfer direction!\n",
1459                         drive->name);
1460 
1461                 /* Throw some data at the drive so it doesn't hang
1462                    and quit this request. */
1463                 while (len > 0) {
1464                         int dum = 0;
1465                         atapi_output_bytes(drive, &dum, sizeof(dum));
1466                         len -= sizeof(dum);
1467                 }
1468         } else {
1469                 /* Drive wants a command packet, or invalid ireason... */
1470                 printk("%s: cdrom_write_intr: bad interrupt reason %d\n",
1471                         drive->name, ireason);
1472         }
1473 
1474         cdrom_end_request(0, drive);
1475         return 1;
1476 }
1477 
1478 static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1479 {
1480         int stat, ireason, len, sectors_to_transfer;
1481         struct cdrom_info *info = drive->driver_data;
1482         int i, dma_error = 0, dma = info->dma;
1483         ide_startstop_t startstop;
1484 
1485         struct request *rq = HWGROUP(drive)->rq;
1486 
1487         /* Check for errors. */
1488         if (dma) {
1489                 info->dma = 0;
1490                 if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive))) {
1491                         printk("ide-cd: write dma error\n");
1492                         HWIF(drive)->dmaproc(ide_dma_off, drive);
1493                 }
1494         }
1495 
1496         if (cdrom_decode_status(&startstop, drive, 0, &stat)) {
1497                 printk("ide-cd: write_intr decode_status bad\n");
1498                 return startstop;
1499         }
1500  
1501         if (dma) {
1502                 if (dma_error)
1503                         return ide_error(drive, "dma error", stat);
1504 
1505                 rq = HWGROUP(drive)->rq;
1506                 for (i = rq->nr_sectors; i > 0;) {
1507                         i -= rq->current_nr_sectors;
1508                         ide_end_request(1, HWGROUP(drive));
1509                 }
1510                 return ide_stopped;
1511         }
1512 
1513         /* Read the interrupt reason and the transfer length. */
1514         ireason = IN_BYTE(IDE_NSECTOR_REG);
1515         len = IN_BYTE(IDE_LCYL_REG) + 256 * IN_BYTE(IDE_HCYL_REG);
1516 
1517         /* If DRQ is clear, the command has completed. */
1518         if ((stat & DRQ_STAT) == 0) {
1519                 /* If we're not done writing, complain.
1520                  * Otherwise, complete the command normally.
1521                  */
1522                 if (rq->current_nr_sectors > 0) {
1523                         printk("%s: write_intr: data underrun (%ld blocks)\n",
1524                                 drive->name, rq->current_nr_sectors);
1525                         cdrom_end_request(0, drive);
1526                 } else
1527                         cdrom_end_request(1, drive);
1528                 return ide_stopped;
1529         }
1530 
1531         /* Check that the drive is expecting to do the same thing we are. */
1532         if (ireason & 3)
1533                 if (cdrom_write_check_ireason(drive, len, ireason))
1534                         return ide_stopped;
1535 
1536         /* The number of sectors we need to read from the drive. */
1537         sectors_to_transfer = len / SECTOR_SIZE;
1538 
1539         /* Now loop while we still have data to read from the drive. DMA
1540          * transfers will already have been complete
1541          */
1542         while (sectors_to_transfer > 0) {
1543                 /* If we've filled the present buffer but there's another
1544                    chained buffer after it, move on. */
1545                 if (rq->current_nr_sectors == 0 && rq->nr_sectors > 0)
1546                         cdrom_end_request(1, drive);
1547 
1548                 atapi_output_bytes(drive, rq->buffer, rq->current_nr_sectors);
1549                 rq->nr_sectors -= rq->current_nr_sectors;
1550                 rq->current_nr_sectors = 0;
1551                 rq->sector += rq->current_nr_sectors;
1552                 sectors_to_transfer -= rq->current_nr_sectors;
1553         }
1554 
1555         /* arm handler */
1556         ide_set_handler(drive, &cdrom_write_intr, 5 * WAIT_CMD, NULL);
1557         return ide_started;
1558 }
1559 
1560 static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1561 {
1562         struct packet_command pc;       /* packet_command_t pc; */
1563         struct request *rq = HWGROUP(drive)->rq;
1564         unsigned nframes, frame;
1565 
1566         nframes = rq->nr_sectors >> 2;
1567         frame = rq->sector >> 2;
1568 
1569         memset(&pc.c, 0, sizeof(pc.c));
1570         /*
1571          * we might as well use WRITE_12, but none of the device I have
1572          * support the streaming feature anyway, so who cares.
1573          */
1574         pc.c[0] = GPCMD_WRITE_10;
1575 #if 0   /* the immediate bit */
1576         pc.c[1] = 1 << 3;
1577 #endif
1578         pc.c[7] = (nframes >> 8) & 0xff;
1579         pc.c[8] = nframes & 0xff;
1580         put_unaligned(cpu_to_be32(frame), (unsigned int *)&pc.c[2]);
1581         pc.timeout = 2 * WAIT_CMD;
1582 
1583         return cdrom_transfer_packet_command(drive, &pc, cdrom_write_intr);
1584 }
1585 
1586 static ide_startstop_t cdrom_start_write(ide_drive_t *drive)
1587 {
1588         struct cdrom_info *info = drive->driver_data;
1589 
1590         info->nsectors_buffered = 0;
1591 
1592         /* use dma, if possible. we don't need to check more, since we
1593          * know that the transfer is always (at least!) 2KB aligned */
1594         info->dma = drive->using_dma ? 1 : 0;
1595         info->cmd = WRITE;
1596 
1597         /* Start sending the read request to the drive. */
1598         return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1599 }
1600 
1601 /****************************************************************************
1602  * cdrom driver request routine.
1603  */
1604 static ide_startstop_t
1605 ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, unsigned long block)
1606 {
1607         ide_startstop_t action;
1608         struct cdrom_info *info = drive->driver_data;
1609 
1610         switch (rq->cmd) {
1611                 case WRITE:
1612                 case READ: {
1613                         if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1614                                 unsigned long elpased = jiffies - info->start_seek;
1615                                 int stat = GET_STAT();
1616 
1617                                 if ((stat & SEEK_STAT) != SEEK_STAT) {
1618                                         if (elpased < IDECD_SEEK_TIMEOUT) {
1619                                                 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1620                                                 return ide_stopped;
1621                                         }
1622                                         printk ("%s: DSC timeout\n", drive->name);
1623                                 }
1624                                 CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1625                         }
1626                         if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap)
1627                                 action = cdrom_start_seek (drive, block);
1628                         else {
1629                                 if (rq->cmd == READ)
1630                                         action = cdrom_start_read(drive, block);
1631                                 else
1632                                         action = cdrom_start_write(drive);
1633                         }
1634                         info->last_block = block;
1635                         return action;
1636                 }
1637 
1638                 case PACKET_COMMAND:
1639                 case REQUEST_SENSE_COMMAND: {
1640                         return cdrom_do_packet_command(drive);
1641                 }
1642 
1643                 case RESET_DRIVE_COMMAND: {
1644                         cdrom_end_request(1, drive);
1645                         return ide_do_reset(drive);
1646                 }
1647 
1648                 default: {
1649                         printk("ide-cd: bad cmd %d\n", rq->cmd);
1650                         cdrom_end_request(0, drive);
1651                         return ide_stopped;
1652                 }
1653         }
1654 }
1655 
1656 
1657 
1658 /****************************************************************************
1659  * Ioctl handling.
1660  *
1661  * Routines which queue packet commands take as a final argument a pointer
1662  * to a request_sense struct.  If execution of the command results
1663  * in an error with a CHECK CONDITION status, this structure will be filled
1664  * with the results of the subsequent request sense command.  The pointer
1665  * can also be NULL, in which case no sense information is returned.
1666  */
1667 
1668 #if ! STANDARD_ATAPI
1669 static inline
1670 int bin2bcd (int x)
1671 {
1672         return (x%10) | ((x/10) << 4);
1673 }
1674 
1675 
1676 static inline
1677 int bcd2bin (int x)
1678 {
1679         return (x >> 4) * 10 + (x & 0x0f);
1680 }
1681 
1682 static
1683 void msf_from_bcd (struct atapi_msf *msf)
1684 {
1685         msf->minute = bcd2bin (msf->minute);
1686         msf->second = bcd2bin (msf->second);
1687         msf->frame  = bcd2bin (msf->frame);
1688 }
1689 
1690 #endif /* not STANDARD_ATAPI */
1691 
1692 
1693 static inline
1694 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1695 {
1696         lba += CD_MSF_OFFSET;
1697         lba &= 0xffffff;  /* negative lbas use only 24 bits */
1698         *m = lba / (CD_SECS * CD_FRAMES);
1699         lba %= (CD_SECS * CD_FRAMES);
1700         *s = lba / CD_FRAMES;
1701         *f = lba % CD_FRAMES;
1702 }
1703 
1704 
1705 static inline
1706 int msf_to_lba (byte m, byte s, byte f)
1707 {
1708         return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1709 }
1710 
1711 static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1712 {
1713         struct packet_command pc;
1714         struct cdrom_info *info = drive->driver_data;
1715         struct cdrom_device_info *cdi = &info->devinfo;
1716 
1717         memset(&pc, 0, sizeof(pc));
1718         pc.sense = sense;
1719 
1720         pc.c[0] = GPCMD_TEST_UNIT_READY;
1721 
1722 #if ! STANDARD_ATAPI
1723         /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to 
1724            switch CDs instead of supporting the LOAD_UNLOAD opcode   */
1725 
1726         pc.c[7] = cdi->sanyo_slot % 3;
1727 #endif /* not STANDARD_ATAPI */
1728 
1729         return cdrom_queue_packet_command(drive, &pc);
1730 }
1731 
1732 
1733 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1734 static int
1735 cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
1736 {
1737         struct request_sense my_sense;
1738         struct packet_command pc;
1739         int stat;
1740 
1741         if (sense == NULL)
1742                 sense = &my_sense;
1743 
1744         /* If the drive cannot lock the door, just pretend. */
1745         if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
1746                 stat = 0;
1747         } else {
1748                 memset(&pc, 0, sizeof(pc));
1749                 pc.sense = sense;
1750                 pc.c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1751                 pc.c[4] = lockflag ? 1 : 0;
1752                 stat = cdrom_queue_packet_command (drive, &pc);
1753         }
1754 
1755         /* If we got an illegal field error, the drive
1756            probably cannot lock the door. */
1757         if (stat != 0 &&
1758             sense->sense_key == ILLEGAL_REQUEST &&
1759             (sense->asc == 0x24 || sense->asc == 0x20)) {
1760                 printk ("%s: door locking not supported\n",
1761                         drive->name);
1762                 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1763                 stat = 0;
1764         }
1765         
1766         /* no medium, that's alright. */
1767         if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
1768                 stat = 0;
1769 
1770         if (stat == 0)
1771                 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1772 
1773         return stat;
1774 }
1775 
1776 
1777 /* Eject the disk if EJECTFLAG is 0.
1778    If EJECTFLAG is 1, try to reload the disk. */
1779 static int cdrom_eject(ide_drive_t *drive, int ejectflag,
1780                        struct request_sense *sense)
1781 {
1782         struct packet_command pc;
1783 
1784         if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
1785                 return -EDRIVE_CANT_DO_THIS;
1786         
1787         /* reload fails on some drives, if the tray is locked */
1788         if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
1789                 return 0;
1790 
1791         memset(&pc, 0, sizeof (pc));
1792         pc.sense = sense;
1793 
1794         pc.c[0] = GPCMD_START_STOP_UNIT;
1795         pc.c[4] = 0x02 + (ejectflag != 0);
1796         return cdrom_queue_packet_command (drive, &pc);
1797 }
1798 
1799 static int cdrom_read_capacity(ide_drive_t *drive, unsigned *capacity,
1800                                struct request_sense *sense)
1801 {
1802         struct {
1803                 __u32 lba;
1804                 __u32 blocklen;
1805         } capbuf;
1806 
1807         int stat;
1808         struct packet_command pc;
1809 
1810         memset(&pc, 0, sizeof(pc));
1811         pc.sense = sense;
1812 
1813         pc.c[0] = GPCMD_READ_CDVD_CAPACITY;
1814         pc.buffer = (char *)&capbuf;
1815         pc.buflen = sizeof(capbuf);
1816 
1817         stat = cdrom_queue_packet_command(drive, &pc);
1818         if (stat == 0)
1819                 *capacity = 1 + be32_to_cpu(capbuf.lba);
1820 
1821         return stat;
1822 }
1823 
1824 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1825                                 int format, char *buf, int buflen,
1826                                 struct request_sense *sense)
1827 {
1828         struct packet_command pc;
1829 
1830         memset(&pc, 0, sizeof(pc));
1831         pc.sense = sense;
1832 
1833         pc.buffer =  buf;
1834         pc.buflen = buflen;
1835         pc.c[0] = GPCMD_READ_TOC_PMA_ATIP;
1836         pc.c[6] = trackno;
1837         pc.c[7] = (buflen >> 8);
1838         pc.c[8] = (buflen & 0xff);
1839         pc.c[9] = (format << 6);
1840 
1841         if (msf_flag)
1842                 pc.c[1] = 2;
1843 
1844         return cdrom_queue_packet_command (drive, &pc);
1845 }
1846 
1847 
1848 /* Try to read the entire TOC for the disk into our internal buffer. */
1849 static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
1850 {
1851         int minor, stat, ntracks, i;
1852         kdev_t dev;
1853         struct cdrom_info *info = drive->driver_data;
1854         struct atapi_toc *toc = info->toc;
1855         struct {
1856                 struct atapi_toc_header hdr;
1857                 struct atapi_toc_entry  ent;
1858         } ms_tmp;
1859 
1860         if (toc == NULL) {
1861                 /* Try to allocate space. */
1862                 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1863                                                     GFP_KERNEL);
1864                 info->toc = toc;
1865                 if (toc == NULL) {
1866                         printk ("%s: No cdrom TOC buffer!\n", drive->name);
1867                         return -ENOMEM;
1868                 }
1869         }
1870 
1871         /* Check to see if the existing data is still valid.
1872            If it is, just return. */
1873         (void) cdrom_check_status(drive, sense);
1874 
1875         /* First read just the header, so we know how long the TOC is. */
1876         stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1877                                     sizeof(struct atapi_toc_header), sense);
1878         if (stat) return stat;
1879 
1880 #if ! STANDARD_ATAPI
1881         if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1882                 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1883                 toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
1884         }
1885 #endif  /* not STANDARD_ATAPI */
1886 
1887         ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1888         if (ntracks <= 0)
1889                 return -EIO;
1890         if (ntracks > MAX_TRACKS)
1891                 ntracks = MAX_TRACKS;
1892 
1893         /* Now read the whole schmeer. */
1894         stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1895                                   (char *)&toc->hdr,
1896                                    sizeof(struct atapi_toc_header) +
1897                                    (ntracks + 1) *
1898                                    sizeof(struct atapi_toc_entry), sense);
1899 
1900         if (stat && toc->hdr.first_track > 1) {
1901                 /* Cds with CDI tracks only don't have any TOC entries,
1902                    despite of this the returned values are
1903                    first_track == last_track = number of CDI tracks + 1,
1904                    so that this case is indistinguishable from the same
1905                    layout plus an additional audio track.
1906                    If we get an error for the regular case, we assume
1907                    a CDI without additional audio tracks. In this case
1908                    the readable TOC is empty (CDI tracks are not included)
1909                    and only holds the Leadout entry. Heiko Eißfeldt */
1910                 ntracks = 0;
1911                 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1912                                            (char *)&toc->hdr,
1913                                            sizeof(struct atapi_toc_header) +
1914                                            (ntracks + 1) *
1915                                            sizeof(struct atapi_toc_entry),
1916                                            sense);
1917                 if (stat) {
1918                         return stat;
1919                 }
1920 #if ! STANDARD_ATAPI
1921                 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1922                         toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
1923                         toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
1924                 } else
1925 #endif  /* not STANDARD_ATAPI */
1926                 {
1927                         toc->hdr.first_track = CDROM_LEADOUT;
1928                         toc->hdr.last_track = CDROM_LEADOUT;
1929                 }
1930         }
1931 
1932         if (stat)
1933                 return stat;
1934 
1935         toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1936 
1937 #if ! STANDARD_ATAPI
1938         if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1939                 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1940                 toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
1941         }
1942 #endif  /* not STANDARD_ATAPI */
1943 
1944         for (i=0; i<=ntracks; i++) {
1945 #if ! STANDARD_ATAPI
1946                 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1947                         if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1948                                 toc->ent[i].track = bcd2bin (toc->ent[i].track);
1949                         msf_from_bcd (&toc->ent[i].addr.msf);
1950                 }
1951 #endif  /* not STANDARD_ATAPI */
1952                 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1953                                                    toc->ent[i].addr.msf.second,
1954                                                    toc->ent[i].addr.msf.frame);
1955         }
1956 
1957         /* Read the multisession information. */
1958         if (toc->hdr.first_track != CDROM_LEADOUT) {
1959                 /* Read the multisession information. */
1960                 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1961                                            sizeof(ms_tmp), sense);
1962                 if (stat) return stat;
1963         } else {
1964                 ms_tmp.ent.addr.msf.minute = 0;
1965                 ms_tmp.ent.addr.msf.second = 2;
1966                 ms_tmp.ent.addr.msf.frame  = 0;
1967                 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
1968         }
1969 
1970 #if ! STANDARD_ATAPI
1971         if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1972                 msf_from_bcd (&ms_tmp.ent.addr.msf);
1973 #endif  /* not STANDARD_ATAPI */
1974 
1975         toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1976                                             ms_tmp.ent.addr.msf.second,
1977                                             ms_tmp.ent.addr.msf.frame);
1978 
1979         toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1980 
1981         /* Now try to get the total cdrom capacity. */
1982         minor = (drive->select.b.unit) << PARTN_BITS;
1983         dev = MKDEV(HWIF(drive)->major, minor);
1984         stat = cdrom_get_last_written(dev, (long *)&toc->capacity);
1985         if (stat)
1986                 stat = cdrom_read_capacity(drive, &toc->capacity, sense);
1987         if (stat)
1988                 toc->capacity = 0x1fffff;
1989 
1990         HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS] = (toc->capacity * SECTORS_PER_FRAME) >> (BLOCK_SIZE_BITS - 9);
1991         drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1992 
1993         /* Remember that we've read this stuff. */
1994         CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1995 
1996         return 0;
1997 }
1998 
1999 
2000 static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2001                                  int buflen, struct request_sense *sense)
2002 {
2003         struct packet_command pc;
2004 
2005         memset(&pc, 0, sizeof(pc));
2006         pc.sense = sense;
2007 
2008         pc.buffer = buf;
2009         pc.buflen = buflen;
2010         pc.c[0] = GPCMD_READ_SUBCHANNEL;
2011         pc.c[1] = 2;     /* MSF addressing */
2012         pc.c[2] = 0x40;  /* request subQ data */
2013         pc.c[3] = format;
2014         pc.c[7] = (buflen >> 8);
2015         pc.c[8] = (buflen & 0xff);
2016         return cdrom_queue_packet_command(drive, &pc);
2017 }
2018 
2019 /* ATAPI cdrom drives are free to select the speed you request or any slower
2020    rate :-( Requesting too fast a speed will _not_ produce an error. */
2021 static int cdrom_select_speed(ide_drive_t *drive, int speed,
2022                               struct request_sense *sense)
2023 {
2024         struct packet_command pc;
2025         memset(&pc, 0, sizeof(pc));
2026         pc.sense = sense;
2027 
2028         if (speed == 0)
2029                 speed = 0xffff; /* set to max */
2030         else
2031                 speed *= 177;   /* Nx to kbytes/s */
2032 
2033         pc.c[0] = GPCMD_SET_SPEED;
2034         /* Read Drive speed in kbytes/second MSB */
2035         pc.c[2] = (speed >> 8) & 0xff;  
2036         /* Read Drive speed in kbytes/second LSB */
2037         pc.c[3] = speed & 0xff;
2038         if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
2039             CDROM_CONFIG_FLAGS(drive)->cd_rw ||
2040             CDROM_CONFIG_FLAGS(drive)->dvd_r) {
2041                 /* Write Drive speed in kbytes/second MSB */
2042                 pc.c[4] = (speed >> 8) & 0xff;
2043                 /* Write Drive speed in kbytes/second LSB */
2044                 pc.c[5] = speed & 0xff;
2045        }
2046 
2047         return cdrom_queue_packet_command(drive, &pc);
2048 }
2049 
2050 static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2051 {
2052         struct request_sense sense;
2053         struct packet_command pc;
2054 
2055         memset(&pc, 0, sizeof (pc));
2056         pc.sense = &sense;
2057 
2058         pc.c[0] = GPCMD_PLAY_AUDIO_MSF;
2059         lba_to_msf(lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
2060         lba_to_msf(lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
2061 
2062         return cdrom_queue_packet_command(drive, &pc);
2063 }
2064 
2065 static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2066                                 struct atapi_toc_entry **ent)
2067 {
2068         struct cdrom_info *info = drive->driver_data;
2069         struct atapi_toc *toc = info->toc;
2070         int ntracks;
2071 
2072         /*
2073          * don't serve cached data, if the toc isn't valid
2074          */
2075         if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2076                 return -EINVAL;
2077 
2078         /* Check validity of requested track number. */
2079         ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2080         if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2081         if (track == CDROM_LEADOUT)
2082                 *ent = &toc->ent[ntracks];
2083         else if (track < toc->hdr.first_track ||
2084                  track > toc->hdr.last_track)
2085                 return -EINVAL;
2086         else
2087                 *ent = &toc->ent[track - toc->hdr.first_track];
2088 
2089         return 0;
2090 }
2091 
2092 /* the generic packet interface to cdrom.c */
2093 static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2094                             struct cdrom_generic_command *cgc)
2095 {
2096         struct packet_command pc;
2097         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2098 
2099         if (cgc->timeout <= 0)
2100                 cgc->timeout = WAIT_CMD;
2101 
2102         /* here we queue the commands from the uniform CD-ROM
2103            layer. the packet must be complete, as we do not
2104            touch it at all. */
2105         memset(&pc, 0, sizeof(pc));
2106         memcpy(pc.c, cgc->cmd, CDROM_PACKET_SIZE);
2107         pc.buffer = cgc->buffer;
2108         pc.buflen = cgc->buflen;
2109         pc.quiet = cgc->quiet;
2110         pc.timeout = cgc->timeout;
2111         pc.sense = cgc->sense;
2112         return cgc->stat = cdrom_queue_packet_command(drive, &pc);
2113 }
2114 
2115 static
2116 int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
2117                          unsigned int cmd, unsigned long arg)
2118 {
2119         struct cdrom_generic_command cgc;
2120         char buffer[16];
2121         int stat;
2122 
2123         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2124 
2125         /* These will be moved into the Uniform layer shortly... */
2126         switch (cmd) {
2127         case CDROMSETSPINDOWN: {
2128                 char spindown;
2129  
2130                 if (copy_from_user(&spindown, (void *) arg, sizeof(char)))
2131                         return -EFAULT;
2132  
2133                 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2134                         return stat;
2135 
2136                 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2137 
2138                 return cdrom_mode_select(cdi, &cgc);
2139         } 
2140  
2141         case CDROMGETSPINDOWN: {
2142                 char spindown;
2143  
2144                 if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2145                         return stat;
2146  
2147                 spindown = buffer[11] & 0x0f;
2148  
2149                 if (copy_to_user((void *) arg, &spindown, sizeof (char)))
2150                         return -EFAULT;
2151  
2152                 return 0;
2153         }
2154   
2155         default:
2156                 return -EINVAL;
2157         }
2158 
2159 }
2160 
2161 static
2162 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2163                            unsigned int cmd, void *arg)
2164                            
2165 {
2166         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2167         struct cdrom_info *info = drive->driver_data;
2168         int stat;
2169 
2170         switch (cmd) {
2171         /*
2172          * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2173          * atapi doesn't support it
2174          */
2175         case CDROMPLAYTRKIND: {
2176                 unsigned long lba_start, lba_end;
2177                 struct cdrom_ti *ti = (struct cdrom_ti *)arg;
2178                 struct atapi_toc_entry *first_toc, *last_toc;
2179 
2180                 stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2181                 if (stat)
2182                         return stat;
2183 
2184                 stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2185                 if (stat)
2186                         return stat;
2187 
2188                 if (ti->cdti_trk1 != CDROM_LEADOUT)
2189                         ++last_toc;
2190                 lba_start = first_toc->addr.lba;
2191                 lba_end   = last_toc->addr.lba;
2192 
2193                 if (lba_end <= lba_start)
2194                         return -EINVAL;
2195 
2196                 return cdrom_play_audio(drive, lba_start, lba_end);
2197         }
2198 
2199         case CDROMREADTOCHDR: {
2200                 struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2201                 struct atapi_toc *toc;
2202 
2203                 /* Make sure our saved TOC is valid. */
2204                 stat = cdrom_read_toc(drive, NULL);
2205                 if (stat) return stat;
2206 
2207                 toc = info->toc;
2208                 tochdr->cdth_trk0 = toc->hdr.first_track;
2209                 tochdr->cdth_trk1 = toc->hdr.last_track;
2210 
2211                 return 0;
2212         }
2213 
2214         case CDROMREADTOCENTRY: {
2215                 struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
2216                 struct atapi_toc_entry *toce;
2217 
2218                 stat = cdrom_get_toc_entry (drive, tocentry->cdte_track, &toce);
2219                 if (stat) return stat;
2220 
2221                 tocentry->cdte_ctrl = toce->control;
2222                 tocentry->cdte_adr  = toce->adr;
2223                 if (tocentry->cdte_format == CDROM_MSF) {
2224                         lba_to_msf (toce->addr.lba,
2225                                    &tocentry->cdte_addr.msf.minute,
2226                                    &tocentry->cdte_addr.msf.second,
2227                                    &tocentry->cdte_addr.msf.frame);
2228                 } else
2229                         tocentry->cdte_addr.lba = toce->addr.lba;
2230 
2231                 return 0;
2232         }
2233 
2234         default:
2235                 return -EINVAL;
2236         }
2237 }
2238 
2239 static
2240 int ide_cdrom_reset (struct cdrom_device_info *cdi)
2241 {
2242         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2243         struct request_sense sense;
2244         struct request req;
2245         int ret;
2246 
2247         ide_init_drive_cmd (&req);
2248         req.cmd = RESET_DRIVE_COMMAND;
2249         ret = ide_do_drive_cmd(drive, &req, ide_wait);
2250 
2251         /*
2252          * A reset will unlock the door. If it was previously locked,
2253          * lock it again.
2254          */
2255         if (CDROM_STATE_FLAGS(drive)->door_locked)
2256                 (void) cdrom_lockdoor(drive, 1, &sense);
2257 
2258         return ret;
2259 }
2260 
2261 
2262 static
2263 int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2264 {
2265         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2266         struct request_sense sense;
2267 
2268         if (position) {
2269                 int stat = cdrom_lockdoor(drive, 0, &sense);
2270                 if (stat) return stat;
2271         }
2272 
2273         return cdrom_eject(drive, !position, &sense);
2274 }
2275 
2276 static
2277 int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2278 {
2279         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2280         return cdrom_lockdoor(drive, lock, NULL);
2281 }
2282 
2283 static
2284 int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2285 {
2286         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2287         struct request_sense sense;
2288         int stat;
2289 
2290         if ((stat = cdrom_select_speed (drive, speed, &sense)) < 0)
2291                 return stat;
2292 
2293         cdi->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2294         return 0;
2295 }
2296 
2297 static
2298 int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2299 {
2300         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2301 
2302         if (slot_nr == CDSL_CURRENT) {
2303                 struct request_sense sense;
2304                 int stat = cdrom_check_status(drive, &sense);
2305                 if (stat == 0 || sense.sense_key == UNIT_ATTENTION)
2306                         return CDS_DISC_OK;
2307 
2308                 if (sense.sense_key == NOT_READY && sense.asc == 0x04 &&
2309                     sense.ascq == 0x04)
2310                         return CDS_DISC_OK;
2311 
2312 
2313                 /*
2314                  * If not using Mt Fuji extended media tray reports,
2315                  * just return TRAY_OPEN since ATAPI doesn't provide
2316                  * any other way to detect this...
2317                  */
2318                 if (sense.sense_key == NOT_READY) {
2319                         if (sense.asc == 0x3a && (!sense.ascq||sense.ascq == 1))
2320                                 return CDS_NO_DISC;
2321                         else
2322                                 return CDS_TRAY_OPEN;
2323                 }
2324 
2325                 return CDS_DRIVE_NOT_READY;
2326         }
2327         return -EINVAL;
2328 }
2329 
2330 static
2331 int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2332                                 struct cdrom_multisession *ms_info)
2333 {
2334         struct atapi_toc *toc;
2335         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2336         struct cdrom_info *info = drive->driver_data;
2337         struct request_sense sense;
2338         int ret;
2339 
2340         if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2341                 if ((ret = cdrom_read_toc(drive, &sense)))
2342                         return ret;
2343 
2344         toc = info->toc;
2345         ms_info->addr.lba = toc->last_session_lba;
2346         ms_info->xa_flag = toc->xa_flag;
2347 
2348         return 0;
2349 }
2350 
2351 static
2352 int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2353                        struct cdrom_mcn *mcn_info)
2354 {
2355         int stat;
2356         char mcnbuf[24];
2357         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2358 
2359 /* get MCN */
2360         if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2361                 return stat;
2362 
2363         memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2364                 sizeof (mcn_info->medium_catalog_number)-1);
2365         mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2366                 = '\0';
2367 
2368         return 0;
2369 }
2370 
2371 
2372 
2373 /****************************************************************************
2374  * Other driver requests (open, close, check media change).
2375  */
2376 
2377 static
2378 int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2379                                        int slot_nr)
2380 {
2381         ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2382         int retval;
2383         
2384         if (slot_nr == CDSL_CURRENT) {
2385                 (void) cdrom_check_status(drive, NULL);
2386                 retval = CDROM_STATE_FLAGS (drive)->media_changed;
2387                 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2388                 return retval;
2389         } else {
2390                 return -EINVAL;
2391         }
2392 }
2393 
2394 
2395 static
2396 int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2397 {
2398         return 0;
2399 }
2400 
2401 
2402 /*
2403  * Close down the device.  Invalidate all cached blocks.
2404  */
2405 
2406 static
2407 void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2408 {
2409 }
2410 
2411 
2412 
2413 /****************************************************************************
2414  * Device initialization.
2415  */
2416 static struct cdrom_device_ops ide_cdrom_dops = {
2417         open:                   ide_cdrom_open_real,
2418         release:                ide_cdrom_release_real,
2419         drive_status:           ide_cdrom_drive_status,
2420         media_changed:          ide_cdrom_check_media_change_real,
2421         tray_move:              ide_cdrom_tray_move,
2422         lock_door:              ide_cdrom_lock_door,
2423         select_speed:           ide_cdrom_select_speed,
2424         get_last_session:       ide_cdrom_get_last_session,
2425         get_mcn:                ide_cdrom_get_mcn,
2426         reset:                  ide_cdrom_reset,
2427         audio_ioctl:            ide_cdrom_audio_ioctl,
2428         dev_ioctl:              ide_cdrom_dev_ioctl,
2429         capability:             CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2430                                 CDC_SELECT_SPEED | CDC_SELECT_DISC |
2431                                 CDC_MULTI_SESSION | CDC_MCN |
2432                                 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
2433                                 CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
2434                                 CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2435                                 CDC_GENERIC_PACKET,
2436         generic_packet:         ide_cdrom_packet,
2437 };
2438 
2439 static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2440 {
2441         struct cdrom_info *info = drive->driver_data;
2442         struct cdrom_device_info *devinfo = &info->devinfo;
2443         int minor = (drive->select.b.unit) << PARTN_BITS;
2444 
2445         devinfo->dev = MKDEV (HWIF(drive)->major, minor);
2446         devinfo->ops = &ide_cdrom_dops;
2447         devinfo->mask = 0;
2448         *(int *)&devinfo->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2449         *(int *)&devinfo->capacity = nslots;
2450         devinfo->handle = (void *) drive;
2451         strcpy(devinfo->name, drive->name);
2452         
2453         /* set capability mask to match the probe. */
2454         if (!CDROM_CONFIG_FLAGS (drive)->cd_r)
2455                 devinfo->mask |= CDC_CD_R;
2456         if (!CDROM_CONFIG_FLAGS (drive)->cd_rw)
2457                 devinfo->mask |= CDC_CD_RW;
2458         if (!CDROM_CONFIG_FLAGS (drive)->dvd)
2459                 devinfo->mask |= CDC_DVD;
2460         if (!CDROM_CONFIG_FLAGS (drive)->dvd_r)
2461                 devinfo->mask |= CDC_DVD_R;
2462         if (!CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2463                 devinfo->mask |= CDC_DVD_RAM;
2464         if (!CDROM_CONFIG_FLAGS (drive)->is_changer)
2465                 devinfo->mask |= CDC_SELECT_DISC;
2466         if (!CDROM_CONFIG_FLAGS (drive)->audio_play)
2467                 devinfo->mask |= CDC_PLAY_AUDIO;
2468         if (!CDROM_CONFIG_FLAGS (drive)->close_tray)
2469                 devinfo->mask |= CDC_CLOSE_TRAY;
2470 
2471         devinfo->de = devfs_register(drive->de, "cd", DEVFS_FL_DEFAULT,
2472                                      HWIF(drive)->major, minor,
2473                                      S_IFBLK | S_IRUGO | S_IWUGO,
2474                                      ide_fops, NULL);
2475 
2476         return register_cdrom(devinfo);
2477 }
2478 
2479 static
2480 int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2481 {
2482         struct cdrom_info *info = drive->driver_data;
2483         struct cdrom_device_info *cdi = &info->devinfo;
2484         struct cdrom_generic_command cgc;
2485         int stat, attempts = 3, size = sizeof(*cap);
2486 
2487         /*
2488          * ACER50 (and others?) require the full spec length mode sense
2489          * page capabilities size, but older drives break.
2490          */
2491         if (drive->id) {
2492                 if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2493                     !strcmp(drive->id->model, "WPI CDS-32X")))
2494                         size -= sizeof(cap->pad);
2495         }
2496 
2497         /* we have to cheat a little here. the packet will eventually
2498          * be queued with ide_cdrom_packet(), which extracts the
2499          * drive from cdi->handle. Since this device hasn't been
2500          * registered with the Uniform layer yet, it can't do this.
2501          * Same goes for cdi->ops.
2502          */
2503         cdi->handle = (ide_drive_t *) drive;
2504         cdi->ops = &ide_cdrom_dops;
2505         init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2506         do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2507                 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2508                 if (!stat)
2509                         break;
2510         } while (--attempts);
2511         return stat;
2512 }
2513 
2514 static
2515 int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2516 {
2517         struct cdrom_info *info = drive->driver_data;
2518         struct cdrom_device_info *cdi = &info->devinfo;
2519         struct atapi_capabilities_page cap;
2520         int nslots = 1;
2521 
2522         if (CDROM_CONFIG_FLAGS (drive)->nec260) {
2523                 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;                       
2524                 CDROM_CONFIG_FLAGS (drive)->audio_play = 1;       
2525                 return nslots;
2526         }
2527 
2528         if (ide_cdrom_get_capabilities(drive, &cap))
2529                 return 0;
2530 
2531         if (cap.lock == 0)
2532                 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2533         if (cap.eject)
2534                 CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2535         if (cap.cd_r_write)
2536                 CDROM_CONFIG_FLAGS (drive)->cd_r = 1;
2537         if (cap.cd_rw_write)
2538                 CDROM_CONFIG_FLAGS (drive)->cd_rw = 1;
2539         if (cap.test_write)
2540                 CDROM_CONFIG_FLAGS (drive)->test_write = 1;
2541         if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2542                 CDROM_CONFIG_FLAGS (drive)->dvd = 1;
2543         if (cap.dvd_ram_write)
2544                 CDROM_CONFIG_FLAGS (drive)->dvd_ram = 1;
2545         if (cap.dvd_r_write)
2546                 CDROM_CONFIG_FLAGS (drive)->dvd_r = 1;
2547         if (cap.audio_play)
2548                 CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
2549         if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2550                 CDROM_CONFIG_FLAGS (drive)->close_tray = 0;
2551 
2552 #if ! STANDARD_ATAPI
2553         if (cdi->sanyo_slot > 0) {
2554                 CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2555                 nslots = 3;
2556         }
2557 
2558         else
2559 #endif /* not STANDARD_ATAPI */
2560         if (cap.mechtype == mechtype_individual_changer ||
2561             cap.mechtype == mechtype_cartridge_changer) {
2562                 if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2563                         CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2564                         CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 1;
2565                 }
2566         }
2567 
2568         /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2569         if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2570                 CDROM_STATE_FLAGS (drive)->current_speed  = 
2571                         (((unsigned int)cap.curspeed) + (176/2)) / 176;
2572                 CDROM_CONFIG_FLAGS (drive)->max_speed = 
2573                         (((unsigned int)cap.maxspeed) + (176/2)) / 176;
2574         } else {
2575                 CDROM_STATE_FLAGS (drive)->current_speed  = 
2576                         (ntohs(cap.curspeed) + (176/2)) / 176;
2577                 CDROM_CONFIG_FLAGS (drive)->max_speed = 
2578                         (ntohs(cap.maxspeed) + (176/2)) / 176;
2579         }
2580 
2581         /* don't print speed if the drive reported 0.
2582          */
2583         printk("%s: ATAPI", drive->name);
2584         if (CDROM_CONFIG_FLAGS(drive)->max_speed)
2585                 printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
2586         printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
2587 
2588         if (CDROM_CONFIG_FLAGS (drive)->dvd_r|CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2589                 printk (" DVD%s%s", 
2590                 (CDROM_CONFIG_FLAGS (drive)->dvd_r)? "-R" : "", 
2591                 (CDROM_CONFIG_FLAGS (drive)->dvd_ram)? "-RAM" : "");
2592 
2593         if (CDROM_CONFIG_FLAGS (drive)->cd_r|CDROM_CONFIG_FLAGS (drive)->cd_rw) 
2594                 printk (" CD%s%s", 
2595                 (CDROM_CONFIG_FLAGS (drive)->cd_r)? "-R" : "", 
2596                 (CDROM_CONFIG_FLAGS (drive)->cd_rw)? "/RW" : "");
2597 
2598         if (CDROM_CONFIG_FLAGS (drive)->is_changer) 
2599                 printk (" changer w/%d slots", nslots);
2600         else    
2601                 printk (" drive");
2602 
2603         printk (", %dkB Cache", be16_to_cpu(cap.buffer_size));
2604 
2605 #ifdef CONFIG_BLK_DEV_IDEDMA
2606         if (drive->using_dma)
2607                 (void) HWIF(drive)->dmaproc(ide_dma_verbose, drive);
2608 #endif /* CONFIG_BLK_DEV_IDEDMA */
2609         printk("\n");
2610 
2611         return nslots;
2612 }
2613 
2614 static void ide_cdrom_add_settings(ide_drive_t *drive)
2615 {
2616         int major = HWIF(drive)->major;
2617         int minor = drive->select.b.unit << PARTN_BITS;
2618 
2619         ide_add_setting(drive,  "breada_readahead",     SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
2620         ide_add_setting(drive,  "file_readahead",       SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor], NULL);
2621         ide_add_setting(drive,  "max_kb_per_request",   SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
2622         ide_add_setting(drive,  "dsc_overlap",          SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2623 }
2624 
2625 static
2626 int ide_cdrom_setup (ide_drive_t *drive)
2627 {
2628         struct cdrom_info *info = drive->driver_data;
2629         struct cdrom_device_info *cdi = &info->devinfo;
2630         int minor = drive->select.b.unit << PARTN_BITS;
2631         int nslots;
2632 
2633         /*
2634          * default to read-only always and fix latter at the bottom
2635          */
2636         set_device_ro(MKDEV(HWIF(drive)->major, minor), 1);
2637         set_blocksize(MKDEV(HWIF(drive)->major, minor), CD_FRAMESIZE);
2638 
2639         drive->special.all      = 0;
2640         drive->ready_stat       = 0;
2641 
2642         CDROM_STATE_FLAGS (drive)->media_changed = 1;
2643         CDROM_STATE_FLAGS (drive)->toc_valid     = 0;
2644         CDROM_STATE_FLAGS (drive)->door_locked   = 0;
2645 
2646 #if NO_DOOR_LOCKING
2647         CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2648 #else
2649         CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2650 #endif
2651 
2652         if (drive->id != NULL)
2653                 CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2654                         ((drive->id->config & 0x0060) == 0x20);
2655         else
2656                 CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2657 
2658         CDROM_CONFIG_FLAGS (drive)->is_changer = 0;
2659         CDROM_CONFIG_FLAGS (drive)->cd_r = 0;
2660         CDROM_CONFIG_FLAGS (drive)->cd_rw = 0;
2661         CDROM_CONFIG_FLAGS (drive)->test_write = 0;
2662         CDROM_CONFIG_FLAGS (drive)->dvd = 0;
2663         CDROM_CONFIG_FLAGS (drive)->dvd_r = 0;
2664         CDROM_CONFIG_FLAGS (drive)->dvd_ram = 0;
2665         CDROM_CONFIG_FLAGS (drive)->no_eject = 1;
2666         CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 0;
2667         CDROM_CONFIG_FLAGS (drive)->audio_play = 0;
2668         CDROM_CONFIG_FLAGS (drive)->close_tray = 1;
2669         
2670         /* limit transfer size per interrupt. */
2671         CDROM_CONFIG_FLAGS (drive)->limit_nframes = 0;
2672         if (drive->id != NULL) {
2673                 /* a testament to the nice quality of Samsung drives... */
2674                 if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
2675                         CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2676                 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
2677                         CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2678                 /* the 3231 model does not support the SET_CD_SPEED command */
2679                 else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
2680                         cdi->mask |= CDC_SELECT_SPEED;
2681         }
2682 
2683 #if ! STANDARD_ATAPI
2684         /* by default Sanyo 3 CD changer support is turned off and
2685            ATAPI Rev 2.2+ standard support for CD changers is used */
2686         cdi->sanyo_slot = 0;
2687 
2688         CDROM_CONFIG_FLAGS (drive)->nec260 = 0;
2689         CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2690         CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2691         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2692         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2693 
2694         if (drive->id != NULL) {
2695                 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2696                     drive->id->fw_rev[4] == '1' &&
2697                     drive->id->fw_rev[6] <= '2') {
2698                         /* Vertos 300.
2699                            Some versions of this drive like to talk BCD. */
2700                         CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2701                         CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2702                         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2703                         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2704                 }
2705 
2706                 else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2707                     drive->id->fw_rev[4] == '1' &&
2708                     drive->id->fw_rev[6] <= '2') {
2709                         /* Vertos 600 ESD. */
2710                         CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2711                 }
2712 
2713                 else if (strcmp (drive->id->model,
2714                                  "NEC CD-ROM DRIVE:260") == 0 &&
2715                          strncmp (drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
2716                         /* Old NEC260 (not R).
2717                            This drive was released before the 1.2 version
2718                            of the spec. */
2719                         CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2720                         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2721                         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2722                         CDROM_CONFIG_FLAGS (drive)->nec260         = 1;
2723                 }
2724 
2725                 else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
2726                          strncmp (drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
2727                         /* Wearnes */
2728                         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2729                         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2730                 }
2731 
2732                 /* Sanyo 3 CD changer uses a non-standard command
2733                     for CD changing */
2734                  else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
2735                          (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
2736                          (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
2737                         /* uses CD in slot 0 when value is set to 3 */
2738                         cdi->sanyo_slot = 3;
2739                 }
2740 
2741 
2742         }
2743 #endif /* not STANDARD_ATAPI */
2744 
2745         info->toc               = NULL;
2746         info->buffer            = NULL;
2747         info->sector_buffered   = 0;
2748         info->nsectors_buffered = 0;
2749         info->changer_info      = NULL;
2750         info->last_block        = 0;
2751         info->start_seek        = 0;
2752 
2753         nslots = ide_cdrom_probe_capabilities (drive);
2754 
2755         if (CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2756                 set_device_ro(MKDEV(HWIF(drive)->major, minor), 0);
2757 
2758         if (ide_cdrom_register (drive, nslots)) {
2759                 printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2760                 info->devinfo.handle = NULL;
2761                 return 1;
2762         }
2763         ide_cdrom_add_settings(drive);
2764         return 0;
2765 }
2766 
2767 /* Forwarding functions to generic routines. */
2768 static
2769 int ide_cdrom_ioctl (ide_drive_t *drive,
2770                      struct inode *inode, struct file *file,
2771                      unsigned int cmd, unsigned long arg)
2772 {
2773         return cdrom_fops.ioctl (inode, file, cmd, arg);
2774 }
2775 
2776 static
2777 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2778 {
2779         struct cdrom_info *info = drive->driver_data;
2780         int rc;
2781 
2782         MOD_INC_USE_COUNT;
2783         if (info->buffer == NULL)
2784                 info->buffer = (char *) kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL);
2785         if ((rc = cdrom_fops.open(ip, fp))) {
2786                 drive->usage--;
2787                 MOD_DEC_USE_COUNT;
2788         }
2789         return rc;
2790 }
2791 
2792 static
2793 void ide_cdrom_release (struct inode *inode, struct file *file,
2794                         ide_drive_t *drive)
2795 {
2796         cdrom_fops.release (inode, file);
2797         MOD_DEC_USE_COUNT;
2798 }
2799 
2800 static
2801 int ide_cdrom_check_media_change (ide_drive_t *drive)
2802 {
2803         return cdrom_fops.check_media_change(MKDEV (HWIF (drive)->major,
2804                         (drive->select.b.unit) << PARTN_BITS));
2805 }
2806 
2807 static
2808 void ide_cdrom_revalidate (ide_drive_t *drive)
2809 {
2810         struct cdrom_info *info = drive->driver_data;
2811         struct atapi_toc *toc;
2812         int minor = drive->select.b.unit << PARTN_BITS;
2813         struct request_sense sense;
2814 
2815         cdrom_read_toc(drive, &sense);
2816 
2817         if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2818                 return;
2819 
2820         toc = info->toc;
2821 
2822         /* for general /dev/cdrom like mounting, one big disc */
2823         drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
2824         HWIF(drive)->gd->sizes[minor] = toc->capacity * BLOCKS_PER_FRAME;
2825 
2826         blk_size[HWIF(drive)->major] = HWIF(drive)->gd->sizes;
2827 }
2828 
2829 static
2830 unsigned long ide_cdrom_capacity (ide_drive_t *drive)
2831 {
2832         unsigned capacity;
2833 
2834         if (cdrom_read_capacity(drive, &capacity, NULL))
2835                 return 0;
2836 
2837         return capacity * SECTORS_PER_FRAME;
2838 }
2839 
2840 static
2841 int ide_cdrom_cleanup(ide_drive_t *drive)
2842 {
2843         struct cdrom_info *info = drive->driver_data;
2844         struct cdrom_device_info *devinfo = &info->devinfo;
2845 
2846         if (ide_unregister_subdriver (drive))
2847                 return 1;
2848         if (info->buffer != NULL)
2849                 kfree(info->buffer);
2850         if (info->toc != NULL)
2851                 kfree(info->toc);
2852         if (info->changer_info != NULL)
2853                 kfree(info->changer_info);
2854         if (devinfo->handle == drive && unregister_cdrom (devinfo))
2855                 printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
2856         kfree(info);
2857         drive->driver_data = NULL;
2858         return 0;
2859 }
2860 
2861 static ide_driver_t ide_cdrom_driver = {
2862         name:                   "ide-cdrom",
2863         version:                IDECD_VERSION,
2864         media:                  ide_cdrom,
2865         supports_dma:           1,
2866         supports_dsc_overlap:   1,
2867         cleanup:                ide_cdrom_cleanup,
2868         do_request:             ide_do_rw_cdrom,
2869         ioctl:                  ide_cdrom_ioctl,
2870         open:                   ide_cdrom_open,
2871         release:                ide_cdrom_release,
2872         media_change:           ide_cdrom_check_media_change,
2873         revalidate:             ide_cdrom_revalidate,
2874         capacity:               ide_cdrom_capacity,
2875 };
2876 
2877 int ide_cdrom_init(void);
2878 static ide_module_t ide_cdrom_module = {
2879         IDE_DRIVER_MODULE,
2880         ide_cdrom_init,
2881         &ide_cdrom_driver,
2882         NULL
2883 };
2884 
2885 /* options */
2886 char *ignore = NULL;
2887 
2888 MODULE_PARM(ignore, "s");
2889 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2890 
2891 static void __exit ide_cdrom_exit(void)
2892 {
2893         ide_drive_t *drive;
2894         int failed = 0;
2895 
2896         while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, &ide_cdrom_driver, failed)) != NULL)
2897                 if (ide_cdrom_cleanup (drive)) {
2898                         printk ("%s: cleanup_module() called while still busy\n", drive->name);
2899                         failed++;
2900                 }
2901         ide_unregister_module (&ide_cdrom_module);
2902 }
2903  
2904 int ide_cdrom_init(void)
2905 {
2906         ide_drive_t *drive;
2907         struct cdrom_info *info;
2908         int failed = 0;
2909 
2910         MOD_INC_USE_COUNT;
2911         while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, NULL, failed++)) != NULL) {
2912                 /* skip drives that we were told to ignore */
2913                 if (ignore != NULL) {
2914                         if (strstr(ignore, drive->name)) {
2915                                 printk("ide-cd: ignoring drive %s\n", drive->name);
2916                                 continue;
2917                         }
2918                 }
2919                 if (drive->scsi) {
2920                         printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
2921                         continue;
2922                 }
2923                 info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
2924                 if (info == NULL) {
2925                         printk ("%s: Can't allocate a cdrom structure\n", drive->name);
2926                         continue;
2927                 }
2928                 if (ide_register_subdriver (drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
2929                         printk ("%s: Failed to register the driver with ide.c\n", drive->name);
2930                         kfree (info);
2931                         continue;
2932                 }
2933                 memset (info, 0, sizeof (struct cdrom_info));
2934                 drive->driver_data = info;
2935                 DRIVER(drive)->busy++;
2936                 if (ide_cdrom_setup (drive)) {
2937                         DRIVER(drive)->busy--;
2938                         if (ide_cdrom_cleanup (drive))
2939                                 printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive->name);
2940                         continue;
2941                 }
2942                 DRIVER(drive)->busy--;
2943                 failed--;
2944         }
2945         ide_register_module(&ide_cdrom_module);
2946         MOD_DEC_USE_COUNT;
2947         return 0;
2948 }
2949 
2950 module_init(ide_cdrom_init);
2951 module_exit(ide_cdrom_exit);
2952 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.