root/drivers/block/ide-cd.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cdrom_in_bytes
  2. cdrom_out_bytes
  3. cdrom_analyze_sense_data
  4. restore_request
  5. cdrom_queue_request_sense
  6. cdrom_end_request
  7. cdrom_saw_media_change
  8. cdrom_decode_status
  9. cdrom_start_packet_command
  10. cdrom_transfer_packet_command
  11. cdrom_buffer_sectors
  12. cdrom_read_check_ireason
  13. cdrom_read_intr
  14. cdrom_read_from_buffer
  15. cdrom_start_read_continuation
  16. cdrom_start_read
  17. cdrom_pc_intr
  18. cdrom_do_pc_continuation
  19. cdrom_do_packet_command
  20. cdrom_sleep
  21. cdrom_queue_packet_command
  22. ide_do_rw_cdrom
  23. bin2bcd
  24. bcd2bin
  25. msf_from_bcd
  26. lba_to_msf
  27. msf_to_lba
  28. cdrom_check_status
  29. cdrom_lockdoor
  30. cdrom_eject
  31. cdrom_pause
  32. cdrom_startstop
  33. cdrom_read_capacity
  34. cdrom_read_tocentry
  35. cdrom_read_toc
  36. cdrom_read_subchannel
  37. cdrom_mode_sense
  38. cdrom_mode_select
  39. cdrom_play_lba_range_1
  40. cdrom_play_lba_range
  41. cdrom_get_toc_entry
  42. cdrom_read_block
  43. ide_cdrom_ioctl
  44. ide_cdrom_check_media_change
  45. ide_cdrom_open
  46. ide_cdrom_release
  47. ide_cdrom_setup

   1 /*
   2  * linux/drivers/block/ide-cd.c
   3  *
   4  * 1.00  Oct 31, 1994 -- Initial version.
   5  * 1.01  Nov  2, 1994 -- Fixed problem with starting request in
   6  *                       cdrom_check_status.
   7  * 1.03  Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
   8  * (from mlord)       -- minor changes to cdrom_setup()
   9  *                    -- renamed ide_dev_s to ide_drive_t, enable irq on command
  10  * 2.00  Nov 27, 1994 -- Generalize packet command interface;
  11  *                       add audio ioctls.
  12  * 2.01  Dec  3, 1994 -- Rework packet command interface to handle devices
  13  *                       which send an interrupt when ready for a command.
  14  * 2.02  Dec 11, 1994 -- Cache the TOC in the driver.
  15  *                       Don't use SCMD_PLAYAUDIO_TI; it's not included
  16  *                       in the current version of ATAPI.
  17  *                       Try to use LBA instead of track or MSF addressing
  18  *                       when possible.
  19  *                       Don't wait for READY_STAT.
  20  * 2.03  Jan 10, 1995 -- Rewrite block read routines to handle block sizes
  21  *                       other than 2k and to move multiple sectors in a
  22  *                       single transaction.
  23  * 2.04  Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
  24  *                       Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
  25  *                       help in figuring this out.  Ditto for Acer and
  26  *                       Aztech drives, which seem to have the same problem.
  27  * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
  28  * 2.05  Jun  8, 1995 -- Don't attempt to retry after an illegal request
  29  *                        or data protect error.
  30  *                       Use HWIF and DEV_HWIF macros as in ide.c.
  31  *                       Always try to do a request_sense after
  32  *                        a failed command.
  33  *                       Include an option to give textual descriptions
  34  *                        of ATAPI errors.
  35  *                       Fix a bug in handling the sector cache which
  36  *                        showed up if the drive returned data in 512 byte
  37  *                        blocks (like Pioneer drives).  Thanks to
  38  *                        Richard Hirst <srh@gpt.co.uk> for diagnosing this.
  39  *                       Properly supply the page number field in the
  40  *                        MODE_SELECT command.
  41  *                       PLAYAUDIO12 is broken on the Aztech; work around it.
  42  * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
  43  *                       (my apologies to Scott, but now ide-cd.c is independent)
  44  * 3.00  Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
  45  *                       Implement CDROMREADAUDIO ioctl (UNTESTED).
  46  *                       Use input_ide_data() and output_ide_data().
  47  *                       Add door locking.
  48  *                       Fix usage count leak in cdrom_open, which happened
  49  *                        when a read-write mount was attempted.
  50  *                       Try to load the disk on open.
  51  *                       Implement CDROMEJECT_SW ioctl (off by default).
  52  *                       Read total cdrom capacity during open.
  53  *                       Rearrange logic in cdrom_decode_status.  Issue
  54  *                        request sense commands for failed packet commands
  55  *                        from here instead of from cdrom_queue_packet_command.
  56  *                        Fix a race condition in retrieving error information.
  57  *                       Suppress printing normal unit attention errors and
  58  *                        some drive not ready errors.
  59  *                       Implement CDROMVOLREAD ioctl.
  60  *                       Implement CDROMREADMODE1/2 ioctls.
  61  *                       Fix race condition in setting up interrupt handlers
  62  *                        when the `serialize' option is used.
  63  * 3.01  Sep  2, 1995 -- Fix ordering of reenabling interrupts in
  64  *                        cdrom_queue_request.
  65  *                       Another try at using ide_[input,output]_data.
  66  * 3.02  Sep 16, 1995 -- Stick total disk capacity in partition table as well.
  67  *                       Make VERBOSE_IDE_CD_ERRORS dump failed command again.
  68  *                       Dump out more information for ILLEGAL REQUEST errs.
  69  *                       Fix handling of errors occurring before the
  70  *                        packet command is transferred.
  71  *                       Fix transfers with odd bytelengths.
  72  * 3.03  Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
  73  *                       `DCI-2S10' drives are broken too.
  74  * 3.04  Nov 20, 1995 -- So are Vertos drives.
  75  * 3.05  Dec  1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
  76  * 3.06  Dec 16, 1995 -- Add support needed for partitions.
  77  *                       More workarounds for Vertos bugs (based on patches
  78  *                        from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
  79  *                       Try to eliminate byteorder assumptions.
  80  *                       Use atapi_cdrom_subchnl struct definition.
  81  *                       Add STANDARD_ATAPI compilation option.
  82  * 3.07  Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
  83  *                        Vertos 300.
  84  *                       Add NO_DOOR_LOCKING configuration option.
  85  *                       Handle drive_cmd requests w/NULL args (for hdparm -t).
  86  *                       Work around sporadic Sony55e audio play problem.
  87  * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
  88  *                        problem with "hde=cdrom" with no drive present.  -ml
  89  * 3.08  Mar  6, 1996 -- More Vertos workarounds.
  90  * 3.09  Apr  5, 1996 -- Add CDROMCLOSETRAY ioctl.
  91  *                       Switch to using MSF addressing for audio commands.
  92  *                       Reformat to match kernel tabbing style.
  93  *                       Add CDROM_GET_UPC ioctl.
  94  * 3.10  Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
  95  * 3.11  Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
  96  *                       to remove redundant verify_area calls.
  97  *
  98  * NOTE: Direct audio reads will only work on some types of drive.
  99  * So far, i've received reports of success for Sony and Toshiba drives.
 100  *
 101  * ATAPI cd-rom driver.  To be used with ide.c.
 102  *
 103  * Copyright (C) 1994, 1995, 1996  scott snyder  <snyder@fnald0.fnal.gov>
 104  * May be copied or modified under the terms of the GNU General Public License
 105  * (../../COPYING).
 106  */
 107 
 108 
 109 /***************************************************************************/
 110 
 111 #include <linux/types.h>
 112 #include <linux/kernel.h>
 113 #include <linux/delay.h>
 114 #include <linux/timer.h>
 115 #include <linux/malloc.h>
 116 #include <linux/ioport.h>
 117 #include <linux/interrupt.h>
 118 #include <linux/blkdev.h>
 119 #include <linux/errno.h>
 120 #include <linux/hdreg.h>
 121 #include <linux/cdrom.h>
 122 #include <asm/irq.h>
 123 #include <asm/io.h>
 124 #include <asm/byteorder.h>
 125 #include <asm/segment.h>
 126 #ifdef __alpha__
 127 # include <asm/unaligned.h>
 128 #endif
 129 
 130 #include "ide.h"
 131 
 132 
 133 
 134 /* Turn this on to have the driver print out the meanings of the
 135    ATAPI error codes.  This will use up additional kernel-space
 136    memory, though. */
 137 
 138 #ifndef VERBOSE_IDE_CD_ERRORS
 139 #define VERBOSE_IDE_CD_ERRORS 0
 140 #endif
 141 
 142 
 143 /* Turning this on will remove code to work around various nonstandard
 144    ATAPI implementations.  If you know your drive follows the standard,
 145    this will give you a slightly smaller kernel. */
 146 
 147 #ifndef STANDARD_ATAPI
 148 #define STANDARD_ATAPI 0
 149 #endif
 150 
 151 
 152 /* Turning this on will disable the door-locking functionality.
 153    This is apparently needed for supermount. */
 154 
 155 #ifndef NO_DOOR_LOCKING
 156 #define NO_DOOR_LOCKING 0
 157 #endif
 158 
 159 
 160 /************************************************************************/
 161 
 162 #define SECTOR_SIZE 512
 163 #define SECTOR_BITS 9
 164 #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
 165 
 166 #define MIN(a,b) ((a) < (b) ? (a) : (b))
 167 
 168 /* special command codes for strategy routine. */
 169 #define PACKET_COMMAND        4315
 170 #define REQUEST_SENSE_COMMAND 4316
 171 #define RESET_DRIVE_COMMAND   4317
 172 
 173 /* Some ATAPI command opcodes (just like SCSI).
 174    (Some other cdrom-specific codes are in cdrom.h.) */
 175 #define TEST_UNIT_READY         0x00
 176 #define REQUEST_SENSE           0x03
 177 #define START_STOP              0x1b
 178 #define ALLOW_MEDIUM_REMOVAL    0x1e
 179 #define READ_CAPACITY           0x25
 180 #define READ_10                 0x28
 181 #define MODE_SENSE_10           0x5a
 182 #define MODE_SELECT_10          0x55
 183 #define READ_CD                 0xbe
 184 
 185 
 186 /* ATAPI sense keys (mostly copied from scsi.h). */
 187 
 188 #define NO_SENSE                0x00
 189 #define RECOVERED_ERROR         0x01
 190 #define NOT_READY               0x02
 191 #define MEDIUM_ERROR            0x03
 192 #define HARDWARE_ERROR          0x04
 193 #define ILLEGAL_REQUEST         0x05
 194 #define UNIT_ATTENTION          0x06
 195 #define DATA_PROTECT            0x07
 196 #define ABORTED_COMMAND         0x0b
 197 #define MISCOMPARE              0x0e
 198 
 199 /* We want some additional flags for cd-rom drives.
 200    To save space in the ide_drive_t struct, use some fields which
 201    doesn't make sense for cd-roms -- `bios_sect' and `bios_head'. */
 202 
 203 /* Configuration flags.  These describe the capabilities of the drive.
 204    They generally do not change after initialization, unless we learn
 205    more about the drive from stuff failing. */
 206 struct ide_cd_config_flags {
 207         __u8 drq_interrupt    : 1; /* Device sends an interrupt when ready
 208                                       for a packet command. */
 209         __u8 no_doorlock      : 1; /* Drive cannot lock the door. */
 210 #if ! STANDARD_ATAPI
 211         __u8 old_readcd       : 1; /* Drive uses old READ CD opcode. */
 212         __u8 playmsf_as_bcd   : 1; /* PLAYMSF command takes BCD args. */
 213         __u8 tocaddr_as_bcd   : 1; /* TOC addresses are in BCD. */
 214         __u8 toctracks_as_bcd : 1; /* TOC track numbers are in BCD. */
 215         __u8 subchan_as_bcd   : 1; /* Subchannel info is in BCD. */
 216 #endif  /* not STANDARD_ATAPI */
 217         __u8 reserved         : 1;
 218 };
 219 #define CDROM_CONFIG_FLAGS(drive) ((struct ide_cd_config_flags *)&((drive)->bios_sect))
 220 
 221  
 222 /* State flags.  These give information about the current state of the
 223    drive, and will change during normal operation. */
 224 struct ide_cd_state_flags {
 225         __u8 media_changed : 1; /* Driver has noticed a media change. */
 226         __u8 toc_valid     : 1; /* Saved TOC information is current. */
 227         __u8 door_locked   : 1; /* We think that the drive door is locked. */
 228         __u8 eject_on_close: 1; /* Drive should eject when device is closed. */
 229         __u8 reserved : 4;
 230 };
 231 #define CDROM_STATE_FLAGS(drive)  ((struct ide_cd_state_flags *)&((drive)->bios_head))
 232 
 233 
 234 #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
 235 
 236 
 237 
 238 /****************************************************************************
 239  * Routines to read and write data from/to the drive, using
 240  * the routines input_ide_data() and output_ide_data() from ide.c.
 241  *
 242  * These routines will round up any request for an odd number of bytes,
 243  * so if an odd bytecount is specified, be sure that there's at least one
 244  * extra byte allocated for the buffer.
 245  */
 246 
 247 
 248 static inline
 249 void cdrom_in_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
     /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         ++bytecount;
 252         ide_input_data (drive, buffer, bytecount / 4);
 253         if ((bytecount & 0x03) >= 2) {
 254                 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
 255         }
 256 }
 257 
 258 
 259 static inline
 260 void cdrom_out_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         ++bytecount;
 263         ide_output_data (drive, buffer, bytecount / 4);
 264         if ((bytecount & 0x03) >= 2) {
 265                 outsw (IDE_DATA_REG,
 266                        ((byte *)buffer) + (bytecount & ~0x03), 1);
 267         }
 268 }
 269 
 270 
 271 
 272 /****************************************************************************
 273  * Descriptions of ATAPI error codes.
 274  */
 275 
 276 #define ARY_LEN(a) ((sizeof(a) / sizeof(a[0])))
 277 
 278 #if VERBOSE_IDE_CD_ERRORS
 279 
 280 /* From Table 124 of the ATAPI 1.2 spec. */
 281 
 282 char *sense_key_texts[16] = {
 283         "No sense data",
 284         "Recovered error",
 285         "Not ready",
 286         "Medium error",
 287         "Hardware error",
 288         "Illegal request",
 289         "Unit attention",
 290         "Data protect",
 291         "(reserved)",
 292         "(reserved)",
 293         "(reserved)",
 294         "Aborted command",
 295         "(reserved)",
 296         "(reserved)",
 297         "Miscompare",
 298         "(reserved)",
 299 };
 300 
 301 
 302 /* From Table 125 of the ATAPI 1.2 spec. */
 303 
 304 struct {
 305         short asc_ascq;
 306         char *text;
 307 } sense_data_texts[] = {
 308         { 0x0000, "No additional sense information" },
 309         { 0x0011, "Audio play operation in progress" },
 310         { 0x0012, "Audio play operation paused" },
 311         { 0x0013, "Audio play operation successfully completed" },
 312         { 0x0014, "Audio play operation stopped due to error" },
 313         { 0x0015, "No current audio status to return" },
 314 
 315         { 0x0200, "No seek complete" },
 316 
 317         { 0x0400, "Logical unit not ready - cause not reportable" },
 318         { 0x0401,
 319           "Logical unit not ready - in progress (sic) of becoming ready" },
 320         { 0x0402, "Logical unit not ready - initializing command required" },
 321         { 0x0403, "Logical unit not ready - manual intervention required" },
 322 
 323         { 0x0600, "No reference position found" },
 324 
 325         { 0x0900, "Track following error" },
 326         { 0x0901, "Tracking servo failure" },
 327         { 0x0902, "Focus servo failure" },
 328         { 0x0903, "Spindle servo failure" },
 329 
 330         { 0x1100, "Unrecovered read error" },
 331         { 0x1106, "CIRC unrecovered error" },
 332 
 333         { 0x1500, "Random positioning error" },
 334         { 0x1501, "Mechanical positioning error" },
 335         { 0x1502, "Positioning error detected by read of medium" },
 336 
 337         { 0x1700, "Recovered data with no error correction applied" },
 338         { 0x1701, "Recovered data with retries" },
 339         { 0x1702, "Recovered data with positive head offset" },
 340         { 0x1703, "Recovered data with negative head offset" },
 341         { 0x1704, "Recovered data with retries and/or CIRC applied" },
 342         { 0x1705, "Recovered data using previous sector ID" },
 343 
 344         { 0x1800, "Recovered data with error correction applied" },
 345         { 0x1801, "Recovered data with error correction and retries applied" },
 346         { 0x1802, "Recovered data - the data was auto-reallocated" },
 347         { 0x1803, "Recovered data with CIRC" },
 348         { 0x1804, "Recovered data with L-EC" },
 349         { 0x1805, "Recovered data - recommend reassignment" },
 350         { 0x1806, "Recovered data - recommend rewrite" },
 351 
 352         { 0x1a00, "Parameter list length error" },
 353 
 354         { 0x2000, "Invalid command operation code" },
 355 
 356         { 0x2100, "Logical block address out of range" },
 357 
 358         { 0x2400, "Invalid field in command packet" },
 359 
 360         { 0x2600, "Invalid field in parameter list" },
 361         { 0x2601, "Parameter not supported" },
 362         { 0x2602, "Parameter value invalid" },
 363         { 0x2603, "Threshold parameters not supported" },
 364 
 365         { 0x2800, "Not ready to ready transition, medium may have changed" },
 366 
 367         { 0x2900, "Power on, reset or bus device reset occurred" },
 368 
 369         { 0x2a00, "Parameters changed" },
 370         { 0x2a01, "Mode parameters changed" },
 371 
 372         { 0x3000, "Incompatible medium installed" },
 373         { 0x3001, "Cannot read medium - unknown format" },
 374         { 0x3002, "Cannot read medium - incompatible format" },
 375 
 376         { 0x3700, "Rounded parameter" },
 377 
 378         { 0x3900, "Saving parameters not supported" },
 379 
 380         { 0x3a00, "Medium not present" },
 381 
 382         { 0x3f00, "ATAPI CD-ROM drive operating conditions have changed" },
 383         { 0x3f01, "Microcode has been changed" },
 384         { 0x3f02, "Changed operating definition" },
 385         { 0x3f03, "Inquiry data has changed" },
 386 
 387         { 0x4000, "Diagnostic failure on component (ASCQ)" },
 388 
 389         { 0x4400, "Internal ATAPI CD-ROM drive failure" },
 390 
 391         { 0x4e00, "Overlapped commands attempted" },
 392 
 393         { 0x5300, "Media load or eject failed" },
 394         { 0x5302, "Medium removal prevented" },
 395 
 396         { 0x5700, "Unable to recover table of contents" },
 397 
 398         { 0x5a00, "Operator request or state change input (unspecified)" },
 399         { 0x5a01, "Operator medium removal request" },
 400 
 401         { 0x5b00, "Threshold condition met" },
 402 
 403         { 0x5c00, "Status change" },
 404 
 405         { 0x6300, "End of user area encountered on this track" },
 406 
 407         { 0x6400, "Illegal mode for this track" },
 408 
 409         { 0xbf00, "Loss of streaming" },
 410 };
 411 #endif
 412 
 413 
 414 
 415 /****************************************************************************
 416  * Generic packet command support and error handling routines.
 417  */
 418 
 419 
 420 static
 421 void cdrom_analyze_sense_data (ide_drive_t *drive, 
     /* [previous][next][first][last][top][bottom][index][help] */
 422                                struct atapi_request_sense *reqbuf,
 423                                struct packet_command *failed_command)
 424 {
 425         /* Don't print not ready or unit attention errors for READ_SUBCHANNEL.
 426            Workman (and probably other programs) uses this command to poll
 427            the drive, and we don't want to fill the syslog
 428            with useless errors. */
 429         if (failed_command &&
 430             failed_command->c[0] == SCMD_READ_SUBCHANNEL &&
 431             (reqbuf->sense_key == NOT_READY ||
 432              reqbuf->sense_key == UNIT_ATTENTION))
 433                 return;
 434 
 435 #if VERBOSE_IDE_CD_ERRORS
 436         {
 437                 int i;
 438                 char *s;
 439                 char buf[80];
 440 
 441                 printk ("ATAPI device %s:\n", drive->name);
 442 
 443                 printk ("  Error code: 0x%02x\n", reqbuf->error_code);
 444 
 445                 if (reqbuf->sense_key >= 0 &&
 446                     reqbuf->sense_key < ARY_LEN (sense_key_texts))
 447                         s = sense_key_texts[reqbuf->sense_key];
 448                 else
 449                         s = "(bad sense key)";
 450 
 451                 printk ("  Sense key: 0x%02x - %s\n", reqbuf->sense_key, s);
 452 
 453                 if (reqbuf->asc == 0x40) {
 454                         sprintf (buf, "Diagnostic failure on component 0x%02x",
 455                                  reqbuf->ascq);
 456                         s = buf;
 457                 } else {
 458                         int lo, hi;
 459                         int key = (reqbuf->asc << 8);
 460                         if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
 461                                 key |= reqbuf->ascq;
 462 
 463                         lo = 0;
 464                         hi = ARY_LEN (sense_data_texts);
 465                         s = NULL;
 466 
 467                         while (hi > lo) {
 468                                 int mid = (lo + hi) / 2;
 469                                 if (sense_data_texts[mid].asc_ascq == key) {
 470                                         s = sense_data_texts[mid].text;
 471                                         break;
 472                                 }
 473                                 else if (sense_data_texts[mid].asc_ascq > key)
 474                                         hi = mid;
 475                                 else
 476                                         lo = mid+1;
 477                         }
 478                 }
 479 
 480                 if (s == NULL) {
 481                         if (reqbuf->asc > 0x80)
 482                                 s = "(vendor-specific error)";
 483                         else
 484                                 s = "(reserved error code)";
 485                 }
 486 
 487                 printk ("  Additional sense data: 0x%02x, 0x%02x  - %s\n",
 488                         reqbuf->asc, reqbuf->ascq, s);
 489 
 490                 if (failed_command != NULL) {
 491                         printk ("  Failed packet command: ");
 492                         for (i=0; i<sizeof (failed_command->c); i++)
 493                                 printk ("%02x ", failed_command->c[i]);
 494                         printk ("\n");
 495                 }
 496 
 497                 if (reqbuf->sense_key == ILLEGAL_REQUEST &&
 498                     (reqbuf->sense_key_specific[0] & 0x80) != 0) {
 499                         printk ("  Error in %s byte %d",
 500                                 (reqbuf->sense_key_specific[0] & 0x40) != 0
 501                                 ? "command packet"
 502                                 : "command data",
 503                                 (reqbuf->sense_key_specific[1] << 8) +
 504                                 reqbuf->sense_key_specific[2]);
 505 
 506                         if ((reqbuf->sense_key_specific[0] & 0x40) != 0) {
 507                                 printk (" bit %d",
 508                                         reqbuf->sense_key_specific[0] & 0x07);
 509                         }
 510 
 511                         printk ("\n");
 512                 }
 513         }
 514 
 515 #else /* not VERBOSE_IDE_CD_ERRORS */
 516 
 517         /* Suppress printing unit attention and `in progress of becoming ready'
 518            errors when we're not being verbose. */
 519 
 520         if (reqbuf->sense_key == UNIT_ATTENTION ||
 521             (reqbuf->sense_key == NOT_READY && (reqbuf->asc == 4 ||
 522                                                 reqbuf->asc == 0x3a)))
 523                 return;
 524 
 525         printk ("%s: code: 0x%02x  key: 0x%02x  asc: 0x%02x  ascq: 0x%02x\n",
 526                 drive->name,
 527                 reqbuf->error_code, reqbuf->sense_key,
 528                 reqbuf->asc, reqbuf->ascq);
 529 #endif /* not VERBOSE_IDE_CD_ERRORS */
 530 }
 531 
 532 
 533 /* Fix up a possibly partially-processed request so that we can
 534    start it over entirely, or even put it back on the request queue. */
 535 static void restore_request (struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
 536 {
 537         if (rq->buffer != rq->bh->b_data) {
 538                 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
 539                 rq->buffer = rq->bh->b_data;
 540                 rq->nr_sectors += n;
 541                 rq->sector -= n;
 542         }
 543         rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
 544 }
 545 
 546 
 547 static void cdrom_queue_request_sense (ide_drive_t *drive, 
     /* [previous][next][first][last][top][bottom][index][help] */
 548                                        struct semaphore *sem,
 549                                        struct atapi_request_sense *reqbuf,
 550                                        struct packet_command *failed_command)
 551 {
 552         struct request *rq;
 553         struct packet_command *pc;
 554         int len;
 555 
 556         /* If the request didn't explicitly specify where
 557            to put the sense data, use the statically allocated structure. */
 558         if (reqbuf == NULL)
 559                 reqbuf = &drive->cdrom_info.sense_data;
 560 
 561         /* Make up a new request to retrieve sense information. */
 562 
 563         pc = &HWIF(drive)->request_sense_pc;
 564         memset (pc, 0, sizeof (*pc));
 565 
 566         /* The request_sense structure has an odd number of (16-bit) words,
 567            which won't work well with 32-bit transfers.  However, we don't care
 568            about the last two bytes, so just truncate the structure down
 569            to an even length. */
 570         len = sizeof (*reqbuf) / 4;
 571         len *= 4;
 572 
 573         pc->c[0] = REQUEST_SENSE;
 574         pc->c[4] = len;
 575         pc->buffer = (char *)reqbuf;
 576         pc->buflen = len;
 577         pc->sense_data = (struct atapi_request_sense *)failed_command;
 578 
 579         /* stuff the sense request in front of our current request */
 580 
 581         rq = &HWIF(drive)->request_sense_request;
 582         ide_init_drive_cmd (rq);
 583         rq->cmd = REQUEST_SENSE_COMMAND;
 584         rq->buffer = (char *)pc;
 585         rq->sem = sem;
 586         (void) ide_do_drive_cmd (drive, rq, ide_preempt);
 587 }
 588 
 589 
 590 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 591 {
 592         struct request *rq = HWGROUP(drive)->rq;
 593 
 594         /* The code in blk.h can screw us up on error recovery if the block
 595            size is larger than 1k.  Fix that up here. */
 596         if (!uptodate && rq->bh != 0) {
 597                 int adj = rq->current_nr_sectors - 1;
 598                 rq->current_nr_sectors -= adj;
 599                 rq->sector += adj;
 600         }
 601 
 602         if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
 603                 struct packet_command *pc = (struct packet_command *)
 604                                               rq->buffer;
 605                 cdrom_analyze_sense_data (drive,
 606                                           (struct atapi_request_sense *)
 607                                                 (pc->buffer - pc->c[4]), 
 608                                           (struct packet_command *)
 609                                                 pc->sense_data);
 610         }
 611 
 612         ide_end_request (uptodate, HWGROUP(drive));
 613 }
 614 
 615 
 616 /* Mark that we've seen a media change, and invalidate our internal
 617    buffers. */
 618 static void cdrom_saw_media_change (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 619 {
 620         CDROM_STATE_FLAGS (drive)->media_changed = 1;
 621         CDROM_STATE_FLAGS (drive)->toc_valid = 0;
 622         drive->cdrom_info.nsectors_buffered = 0;
 623 }
 624 
 625 
 626 /* Returns 0 if the request should be continued.
 627    Returns 1 if the request was ended. */
 628 static int cdrom_decode_status (ide_drive_t *drive, int good_stat,
     /* [previous][next][first][last][top][bottom][index][help] */
 629                                 int *stat_ret)
 630 {
 631         struct request *rq = HWGROUP(drive)->rq;
 632         int stat, err, sense_key, cmd;
 633 
 634         /* Check for errors. */
 635         stat = GET_STAT();
 636         *stat_ret = stat;
 637 
 638         if (OK_STAT (stat, good_stat, BAD_R_STAT))
 639                 return 0;
 640 
 641         /* Got an error. */
 642         err = IN_BYTE (IDE_ERROR_REG);
 643         sense_key = err >> 4;
 644 
 645         if (rq == NULL)
 646                 printk ("%s : missing request in cdrom_decode_status\n",
 647                         drive->name);
 648         else {
 649                 cmd = rq->cmd;
 650 
 651                 if (cmd == REQUEST_SENSE_COMMAND) {
 652                         /* We got an error trying to get sense info
 653                            from the drive (probably while trying
 654                            to recover from a former error).  Just give up. */
 655 
 656                         struct packet_command *pc = (struct packet_command *)
 657                                                       rq->buffer;
 658                         pc->stat = 1;
 659                         cdrom_end_request (1, drive);
 660                         ide_error (drive, "request sense failure", stat);
 661                         return 1;
 662 
 663                 } else if (cmd == PACKET_COMMAND) {
 664                         /* All other functions, except for READ. */
 665 
 666                         struct packet_command *pc = (struct packet_command *)
 667                                                       rq->buffer;
 668                         struct semaphore *sem = NULL;
 669 
 670                         /* Check for tray open. */
 671                         if (sense_key == NOT_READY) {
 672                                 cdrom_saw_media_change (drive);
 673 
 674                                 /* Print an error message to the syslog.
 675                                    Exception: don't print anything if this
 676                                    is a read subchannel command.  This is
 677                                    because workman constantly polls the drive
 678                                    with this command, and we don't want
 679                                    to uselessly fill up the syslog. */
 680                                 if (pc->c[0] != SCMD_READ_SUBCHANNEL)
 681                                         printk ("%s : tray open or drive not ready\n",
 682                                                 drive->name);
 683                         } else if (sense_key == UNIT_ATTENTION) {
 684                                 /* Check for media change. */
 685                                 cdrom_saw_media_change (drive);
 686                                 printk ("%s: media changed\n", drive->name);
 687                         } else {
 688                                 /* Otherwise, print an error. */
 689                                 ide_dump_status (drive, "packet command error",
 690                                                  stat);
 691                         }
 692                         
 693                         /* Set the error flag and complete the request.
 694                            Then, if we have a CHECK CONDITION status,
 695                            queue a request sense command.  We must be careful,
 696                            though: we don't want the thread in
 697                            cdrom_queue_packet_command to wake up until
 698                            the request sense has completed.  We do this
 699                            by transferring the semaphore from the packet
 700                            command request to the request sense request. */
 701 
 702                         if ((stat & ERR_STAT) != 0) {
 703                                 sem = rq->sem;
 704                                 rq->sem = NULL;
 705                         }
 706 
 707                         pc->stat = 1;
 708                         cdrom_end_request (1, drive);
 709 
 710                         if ((stat & ERR_STAT) != 0)
 711                                 cdrom_queue_request_sense (drive, sem,
 712                                                            pc->sense_data, pc);
 713                 } else {
 714                         /* Handle errors from READ requests. */
 715 
 716                         if (sense_key == NOT_READY) {
 717                                 /* Tray open. */
 718                                 cdrom_saw_media_change (drive);
 719 
 720                                 /* Fail the request. */
 721                                 printk ("%s : tray open\n", drive->name);
 722                                 cdrom_end_request (0, drive);
 723                         } else if (sense_key == UNIT_ATTENTION) {
 724                                 /* Media change. */
 725                                 cdrom_saw_media_change (drive);
 726 
 727                                 /* Arrange to retry the request.
 728                                    But be sure to give up if we've retried
 729                                    too many times. */
 730                                 if (++rq->errors > ERROR_MAX)
 731                                         cdrom_end_request (0, drive);
 732                         } else if (sense_key == ILLEGAL_REQUEST ||
 733                                    sense_key == DATA_PROTECT) {
 734                                 /* No point in retrying after an illegal
 735                                    request or data protect error.*/
 736                                 ide_dump_status (drive, "command error", stat);
 737                                 cdrom_end_request (0, drive);
 738                         } else if ((err & ~ABRT_ERR) != 0) {
 739                                 /* Go to the default handler
 740                                    for other errors. */
 741                                 ide_error (drive, "cdrom_decode_status", stat);
 742                                 return 1;
 743                         } else if ((++rq->errors > ERROR_MAX)) {
 744                                 /* We've racked up too many retries.  Abort. */
 745                                 cdrom_end_request (0, drive);
 746                         }
 747 
 748                         /* If we got a CHECK_CONDITION status,
 749                            queue a request sense command. */
 750                         if ((stat & ERR_STAT) != 0)
 751                                 cdrom_queue_request_sense (drive,
 752                                                            NULL, NULL, NULL);
 753                 }
 754         }
 755 
 756         /* Retry, or handle the next request. */
 757         return 1;
 758 }
 759 
 760 
 761 /* Set up the device registers for transferring a packet command on DEV,
 762    expecting to later transfer XFERLEN bytes.  HANDLER is the routine
 763    which actually transfers the command to the drive.  If this is a
 764    drq_interrupt device, this routine will arrange for HANDLER to be
 765    called when the interrupt from the drive arrives.  Otherwise, HANDLER
 766    will be called immediately after the drive is prepared for the transfer. */
 767 
 768 static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
     /* [previous][next][first][last][top][bottom][index][help] */
 769                                        ide_handler_t *handler)
 770 {
 771         /* Wait for the controller to be idle. */
 772         if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
 773 
 774         /* Set up the controller registers. */
 775         OUT_BYTE (0, IDE_FEATURE_REG);
 776         OUT_BYTE (0, IDE_NSECTOR_REG);
 777         OUT_BYTE (0, IDE_SECTOR_REG);
 778 
 779         OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
 780         OUT_BYTE (xferlen >> 8  , IDE_HCYL_REG);
 781         OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
 782 
 783         if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
 784                 ide_set_handler (drive, handler, WAIT_CMD);
 785                 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
 786         } else {
 787                 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
 788                 (*handler) (drive);
 789         }
 790 
 791         return 0;
 792 }
 793 
 794 
 795 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
 796    The device registers must have already been prepared
 797    by cdrom_start_packet_command.
 798    HANDLER is the interrupt handler to call when the command completes
 799    or there's data ready. */
 800 static int cdrom_transfer_packet_command (ide_drive_t *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
 801                                           char *cmd_buf, int cmd_len,
 802                                           ide_handler_t *handler)
 803 {
 804         if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
 805                 /* Here we should have been called after receiving an interrupt
 806                    from the device.  DRQ should how be set. */
 807                 int stat_dum;
 808 
 809                 /* Check for errors. */
 810                 if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum)) return 1;
 811         } else {
 812                 /* Otherwise, we must wait for DRQ to get set. */
 813                 if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
 814                         return 1;
 815         }
 816 
 817         /* Arm the interrupt handler. */
 818         ide_set_handler (drive, handler, WAIT_CMD);
 819 
 820         /* Send the command to the device. */
 821         cdrom_out_bytes (drive, cmd_buf, cmd_len);
 822 
 823         return 0;
 824 }
 825 
 826 
 827 
 828 /****************************************************************************
 829  * Block read functions.
 830  */
 831 
 832 /*
 833  * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
 834  * buffer.  Once the first sector is added, any subsequent sectors are
 835  * assumed to be continuous (until the buffer is cleared).  For the first
 836  * sector added, SECTOR is its sector number.  (SECTOR is then ignored until
 837  * the buffer is cleared.)
 838  */
 839 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
     /* [previous][next][first][last][top][bottom][index][help] */
 840                                   int sectors_to_transfer)
 841 {
 842         struct cdrom_info *info = &drive->cdrom_info;
 843 
 844         /* Number of sectors to read into the buffer. */
 845         int sectors_to_buffer = MIN (sectors_to_transfer,
 846                                      (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
 847                                        info->nsectors_buffered);
 848 
 849         char *dest;
 850 
 851         /* If we don't yet have a sector buffer, try to allocate one.
 852            If we can't get one atomically, it's not fatal -- we'll just throw
 853            the data away rather than caching it. */
 854         if (info->sector_buffer == NULL) {
 855                 info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE,
 856                                                         GFP_ATOMIC);
 857 
 858                 /* If we couldn't get a buffer,
 859                    don't try to buffer anything... */
 860                 if (info->sector_buffer == NULL)
 861                         sectors_to_buffer = 0;
 862         }
 863 
 864         /* If this is the first sector in the buffer, remember its number. */
 865         if (info->nsectors_buffered == 0)
 866                 info->sector_buffered = sector;
 867 
 868         /* Read the data into the buffer. */
 869         dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
 870         while (sectors_to_buffer > 0) {
 871                 cdrom_in_bytes (drive, dest, SECTOR_SIZE);
 872                 --sectors_to_buffer;
 873                 --sectors_to_transfer;
 874                 ++info->nsectors_buffered;
 875                 dest += SECTOR_SIZE;
 876         }
 877 
 878         /* Throw away any remaining data. */
 879         while (sectors_to_transfer > 0) {
 880                 char dum[SECTOR_SIZE];
 881                 cdrom_in_bytes (drive, dum, sizeof (dum));
 882                 --sectors_to_transfer;
 883         }
 884 }
 885 
 886 
 887 /*
 888  * Check the contents of the interrupt reason register from the cdrom
 889  * and attempt to recover if there are problems.  Returns  0 if everything's
 890  * ok; nonzero if the request has been terminated.
 891  */
 892 static inline
 893 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
     /* [previous][next][first][last][top][bottom][index][help] */
 894 {
 895         ireason &= 3;
 896         if (ireason == 2) return 0;
 897 
 898         if (ireason == 0) {
 899                 /* Whoops... The drive is expecting to receive data from us! */
 900                 printk ("%s: cdrom_read_intr: "
 901                         "Drive wants to transfer data the wrong way!\n",
 902                         drive->name);
 903 
 904                 /* Throw some data at the drive so it doesn't hang
 905                    and quit this request. */
 906                 while (len > 0) {
 907                         int dum = 0;
 908                         cdrom_out_bytes (drive, &dum, sizeof (dum));
 909                         len -= sizeof (dum);
 910                 }
 911         } else {
 912                 /* Drive wants a command packet, or invalid ireason... */
 913                 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
 914                         drive->name, ireason);
 915         }
 916 
 917         cdrom_end_request (0, drive);
 918         return -1;
 919 }
 920 
 921 
 922 /*
 923  * Interrupt routine.  Called when a read request has completed.
 924  */
 925 static void cdrom_read_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 926 {
 927         int stat;
 928         int ireason, len, sectors_to_transfer, nskip;
 929 
 930         struct request *rq = HWGROUP(drive)->rq;
 931 
 932         /* Check for errors. */
 933         if (cdrom_decode_status (drive, 0, &stat)) return;
 934 
 935         /* Read the interrupt reason and the transfer length. */
 936         ireason = IN_BYTE (IDE_NSECTOR_REG);
 937         len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
 938 
 939         /* If DRQ is clear, the command has completed. */
 940         if ((stat & DRQ_STAT) == 0) {
 941                 /* If we're not done filling the current buffer, complain.
 942                    Otherwise, complete the command normally. */
 943                 if (rq->current_nr_sectors > 0) {
 944                         printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
 945                                 drive->name, rq->current_nr_sectors);
 946                         cdrom_end_request (0, drive);
 947                 } else
 948                         cdrom_end_request (1, drive);
 949 
 950                 return;
 951         }
 952 
 953         /* Check that the drive is expecting to do the same thing we are. */
 954         if (cdrom_read_check_ireason (drive, len, ireason)) return;
 955 
 956         /* Assume that the drive will always provide data in multiples
 957            of at least SECTOR_SIZE, as it gets hairy to keep track
 958            of the transfers otherwise. */
 959         if ((len % SECTOR_SIZE) != 0) {
 960                 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
 961                         drive->name, len);
 962                 printk ("  This drive is not supported by this version of the driver\n");
 963                 cdrom_end_request (0, drive);
 964                 return;
 965         }
 966 
 967         /* The number of sectors we need to read from the drive. */
 968         sectors_to_transfer = len / SECTOR_SIZE;
 969 
 970         /* First, figure out if we need to bit-bucket
 971            any of the leading sectors. */
 972         nskip = MIN ((int)(rq->current_nr_sectors -
 973                            (rq->bh->b_size >> SECTOR_BITS)),
 974                      sectors_to_transfer);
 975 
 976         while (nskip > 0) {
 977                 /* We need to throw away a sector. */
 978                 char dum[SECTOR_SIZE];
 979                 cdrom_in_bytes (drive, dum, sizeof (dum));
 980 
 981                 --rq->current_nr_sectors;
 982                 --nskip;
 983                 --sectors_to_transfer;
 984         }
 985 
 986         /* Now loop while we still have data to read from the drive. */
 987         while (sectors_to_transfer > 0) {
 988                 int this_transfer;
 989 
 990                 /* If we've filled the present buffer but there's another
 991                    chained buffer after it, move on. */
 992                 if (rq->current_nr_sectors == 0 &&
 993                     rq->nr_sectors > 0)
 994                         cdrom_end_request (1, drive);
 995 
 996                 /* If the buffers are full, cache the rest of the data in our
 997                    internal buffer. */
 998                 if (rq->current_nr_sectors == 0) {
 999                         cdrom_buffer_sectors (drive,
1000                                               rq->sector, sectors_to_transfer);
1001                         sectors_to_transfer = 0;
1002                 } else {
1003                         /* Transfer data to the buffers.
1004                            Figure out how many sectors we can transfer
1005                            to the current buffer. */
1006                         this_transfer = MIN (sectors_to_transfer,
1007                                              rq->current_nr_sectors);
1008 
1009                         /* Read this_transfer sectors
1010                            into the current buffer. */
1011                         while (this_transfer > 0) {
1012                                 cdrom_in_bytes (drive
1013                                                 , rq->buffer, SECTOR_SIZE);
1014                                 rq->buffer += SECTOR_SIZE;
1015                                 --rq->nr_sectors;
1016                                 --rq->current_nr_sectors;
1017                                 ++rq->sector;
1018                                 --this_transfer;
1019                                 --sectors_to_transfer;
1020                         }
1021                 }
1022         }
1023 
1024         /* Done moving data!
1025            Wait for another interrupt. */
1026         ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);
1027 }
1028 
1029 
1030 /*
1031  * Try to satisfy some of the current read request from our cached data.
1032  * Returns nonzero if the request has been completed, zero otherwise.
1033  */
1034 static int cdrom_read_from_buffer (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1035 {
1036         struct cdrom_info *info = &drive->cdrom_info;
1037         struct request *rq = HWGROUP(drive)->rq;
1038 
1039         /* Can't do anything if there's no buffer. */
1040         if (info->sector_buffer == NULL) return 0;
1041 
1042         /* Loop while this request needs data and the next block is present
1043            in our cache. */
1044         while (rq->nr_sectors > 0 &&
1045                rq->sector >= info->sector_buffered &&
1046                rq->sector < info->sector_buffered + info->nsectors_buffered) {
1047                 if (rq->current_nr_sectors == 0)
1048                         cdrom_end_request (1, drive);
1049 
1050                 memcpy (rq->buffer,
1051                         info->sector_buffer +
1052                         (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1053                         SECTOR_SIZE);
1054                 rq->buffer += SECTOR_SIZE;
1055                 --rq->current_nr_sectors;
1056                 --rq->nr_sectors;
1057                 ++rq->sector;
1058         }
1059 
1060         /* If we've satisfied the current request,
1061            terminate it successfully. */
1062         if (rq->nr_sectors == 0) {
1063                 cdrom_end_request (1, drive);
1064                 return -1;
1065         }
1066 
1067         /* Move on to the next buffer if needed. */
1068         if (rq->current_nr_sectors == 0)
1069                 cdrom_end_request (1, drive);
1070 
1071         /* If this condition does not hold, then the kluge i use to
1072            represent the number of sectors to skip at the start of a transfer
1073            will fail.  I think that this will never happen, but let's be
1074            paranoid and check. */
1075         if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1076             (rq->sector % SECTORS_PER_FRAME) != 0) {
1077                 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1078                         drive->name, rq->sector);
1079                 cdrom_end_request (0, drive);
1080                 return -1;
1081         }
1082 
1083         return 0;
1084 }
1085 
1086 
1087 
1088 /*
1089  * Routine to send a read packet command to the drive.
1090  * This is usually called directly from cdrom_start_read.
1091  * However, for drq_interrupt devices, it is called from an interrupt
1092  * when the drive is ready to accept the command.
1093  */
1094 static void cdrom_start_read_continuation (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1095 {
1096         struct packet_command pc;
1097         struct request *rq = HWGROUP(drive)->rq;
1098 
1099         int nsect, sector, nframes, frame, nskip;
1100 
1101         /* Number of sectors to transfer. */
1102         nsect = rq->nr_sectors;
1103 
1104         /* Starting sector. */
1105         sector = rq->sector;
1106 
1107         /* If the requested sector doesn't start on a cdrom block boundary,
1108            we must adjust the start of the transfer so that it does,
1109            and remember to skip the first few sectors.
1110            If the CURRENT_NR_SECTORS field is larger than the size
1111            of the buffer, it will mean that we're to skip a number
1112            of sectors equal to the amount by which CURRENT_NR_SECTORS
1113            is larger than the buffer size. */
1114         nskip = (sector % SECTORS_PER_FRAME);
1115         if (nskip > 0) {
1116                 /* Sanity check... */
1117                 if (rq->current_nr_sectors !=
1118                     (rq->bh->b_size >> SECTOR_BITS)) {
1119                         printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1120                                 drive->name, rq->current_nr_sectors);
1121                         cdrom_end_request (0, drive);
1122                         return;
1123                 }
1124 
1125                 sector -= nskip;
1126                 nsect += nskip;
1127                 rq->current_nr_sectors += nskip;
1128         }
1129 
1130         /* Convert from sectors to cdrom blocks, rounding up the transfer
1131            length if needed. */
1132         nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1133         frame = sector / SECTORS_PER_FRAME;
1134 
1135         /* Largest number of frames was can transfer at once is 64k-1. */
1136         nframes = MIN (nframes, 65535);
1137 
1138         /* Set up the command */
1139         memset (&pc.c, 0, sizeof (pc.c));
1140         pc.c[0] = READ_10;
1141         pc.c[7] = (nframes >> 8);
1142         pc.c[8] = (nframes & 0xff);
1143 #ifdef __alpha__
1144         stl_u (htonl (frame), (unsigned int *) &pc.c[2]);
1145 #else
1146         *(int *)(&pc.c[2]) = htonl (frame);
1147 #endif
1148 
1149         /* Send the command to the drive and return. */
1150         (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c),
1151                                               &cdrom_read_intr);
1152 }
1153 
1154 
1155 /*
1156  * Start a read request from the CD-ROM.
1157  */
1158 static void cdrom_start_read (ide_drive_t *drive, unsigned int block)
     /* [previous][next][first][last][top][bottom][index][help] */
1159 {
1160         struct request *rq = HWGROUP(drive)->rq;
1161         int minor = MINOR (rq->rq_dev);
1162 
1163         /* If the request is relative to a partition, fix it up to refer to the
1164            absolute address.  */
1165         if ((minor & PARTN_MASK) != 0) {
1166                 rq->sector = block;
1167                 minor &= ~PARTN_MASK;
1168                 rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1169         }
1170 
1171         /* We may be retrying this request after an error.  Fix up
1172            any weirdness which might be present in the request packet. */
1173         restore_request (rq);
1174 
1175         /* Satisfy whatever we can of this request from our cached sector. */
1176         if (cdrom_read_from_buffer (drive))
1177                 return;
1178 
1179         /* Clear the local sector buffer. */
1180         drive->cdrom_info.nsectors_buffered = 0;
1181 
1182         /* Start sending the read request to the drive. */
1183         cdrom_start_packet_command (drive, 32768,
1184                                     cdrom_start_read_continuation);
1185 }
1186 
1187 
1188 
1189 
1190 /****************************************************************************
1191  * Execute all other packet commands.
1192  */
1193 
1194 /* Forward declarations. */
1195 static int
1196 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1197                 struct atapi_request_sense *reqbuf);
1198 
1199 
1200 
1201 /* Interrupt routine for packet command completion. */
1202 static void cdrom_pc_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1203 {
1204         int ireason, len, stat, thislen;
1205         struct request *rq = HWGROUP(drive)->rq;
1206         struct packet_command *pc = (struct packet_command *)rq->buffer;
1207 
1208         /* Check for errors. */
1209         if (cdrom_decode_status (drive, 0, &stat)) return;
1210 
1211         /* Read the interrupt reason and the transfer length. */
1212         ireason = IN_BYTE (IDE_NSECTOR_REG);
1213         len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1214 
1215         /* If DRQ is clear, the command has completed.
1216            Complain if we still have data left to transfer. */
1217         if ((stat & DRQ_STAT) == 0) {
1218                 /* Some of the trailing request sense fields are optional, and
1219                    some drives don't send them.  Sigh. */
1220                 if (pc->c[0] == REQUEST_SENSE &&
1221                     pc->buflen > 0 &&
1222                     pc->buflen <= 5) {
1223                         while (pc->buflen > 0) {
1224                                 *pc->buffer++ = 0;
1225                                 --pc->buflen;
1226                         }
1227                 }
1228 
1229                 if (pc->buflen == 0)
1230                         cdrom_end_request (1, drive);
1231                 else {
1232                         printk ("%s: cdrom_pc_intr: data underrun %d\n",
1233                                 drive->name, pc->buflen);
1234                         pc->stat = 1;
1235                         cdrom_end_request (1, drive);
1236                 }
1237                 return;
1238         }
1239 
1240         /* Figure out how much data to transfer. */
1241         thislen = pc->buflen;
1242         if (thislen < 0) thislen = -thislen;
1243         if (thislen > len) thislen = len;
1244 
1245         /* The drive wants to be written to. */
1246         if ((ireason & 3) == 0) {
1247                 /* Check that we want to write. */
1248                 if (pc->buflen > 0) {
1249                         printk ("%s: cdrom_pc_intr: Drive wants "
1250                                 "to transfer data the wrong way!\n",
1251                                 drive->name);
1252                         pc->stat = 1;
1253                         thislen = 0;
1254                 }
1255 
1256                 /* Transfer the data. */
1257                 cdrom_out_bytes (drive, pc->buffer, thislen);
1258 
1259                 /* If we haven't moved enough data to satisfy the drive,
1260                    add some padding. */
1261                 while (len > thislen) {
1262                         int dum = 0;
1263                         cdrom_out_bytes (drive, &dum, sizeof (dum));
1264                         len -= sizeof (dum);
1265                 }
1266 
1267                 /* Keep count of how much data we've moved. */
1268                 pc->buffer += thislen;
1269                 pc->buflen += thislen;
1270         }
1271 
1272         /* Same drill for reading. */
1273         else if ((ireason & 3) == 2) {
1274                 /* Check that we want to read. */
1275                 if (pc->buflen < 0) {
1276                         printk ("%s: cdrom_pc_intr: Drive wants to "
1277                                 "transfer data the wrong way!\n",
1278                                 drive->name);
1279                         pc->stat = 1;
1280                         thislen = 0;
1281                 }
1282 
1283                 /* Transfer the data. */
1284                 cdrom_in_bytes (drive, pc->buffer, thislen);
1285 
1286                 /* If we haven't moved enough data to satisfy the drive,
1287                    add some padding. */
1288                 while (len > thislen) {
1289                         int dum = 0;
1290                         cdrom_in_bytes (drive, &dum, sizeof (dum));
1291                         len -= sizeof (dum);
1292                 }
1293 
1294                 /* Keep count of how much data we've moved. */
1295                 pc->buffer += thislen;
1296                 pc->buflen -= thislen;
1297         } else {
1298                 printk ("%s: cdrom_pc_intr: The drive "
1299                         "appears confused (ireason = 0x%2x)\n",
1300                         drive->name, ireason);
1301                 pc->stat = 1;
1302         }
1303 
1304         /* Now we wait for another interrupt. */
1305         ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);
1306 }
1307 
1308 
1309 static void cdrom_do_pc_continuation (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1310 {
1311         struct request *rq = HWGROUP(drive)->rq;
1312         struct packet_command *pc = (struct packet_command *)rq->buffer;
1313 
1314         /* Send the command to the drive and return. */
1315         cdrom_transfer_packet_command (drive, pc->c,
1316                                        sizeof (pc->c), &cdrom_pc_intr);
1317 }
1318 
1319 
1320 static void cdrom_do_packet_command (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1321 {
1322         int len;
1323         struct request *rq = HWGROUP(drive)->rq;
1324         struct packet_command *pc = (struct packet_command *)rq->buffer;
1325 
1326         len = pc->buflen;
1327         if (len < 0) len = -len;
1328 
1329         pc->stat = 0;
1330 
1331         /* Start sending the command to the drive. */
1332         cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1333 }
1334 
1335 
1336 /* Sleep for TIME jiffies.
1337    Not to be called from an interrupt handler. */
1338 static
1339 void cdrom_sleep (int time)
     /* [previous][next][first][last][top][bottom][index][help] */
1340 {
1341         current->state = TASK_INTERRUPTIBLE;
1342         current->timeout = jiffies + time;
1343         schedule ();
1344 }
1345 
1346 static
1347 int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
1348 {
1349         struct atapi_request_sense my_reqbuf;
1350         int retries = 10;
1351         struct request req;
1352 
1353         /* If our caller has not provided a place to stick any sense data,
1354            use our own area. */
1355         if (pc->sense_data == NULL)
1356                 pc->sense_data = &my_reqbuf;
1357         pc->sense_data->sense_key = 0;
1358 
1359         /* Start of retry loop. */
1360         do {
1361                 ide_init_drive_cmd (&req);
1362                 req.cmd = PACKET_COMMAND;
1363                 req.buffer = (char *)pc;
1364                 (void) ide_do_drive_cmd (drive, &req, ide_wait);
1365 
1366                 if (pc->stat != 0) {
1367                         /* The request failed.  Retry if it was due to a unit
1368                            attention status
1369                            (usually means media was changed). */
1370                         struct atapi_request_sense *reqbuf = pc->sense_data;
1371 
1372                         if (reqbuf->sense_key == UNIT_ATTENTION)
1373                                 ;
1374                         else if (reqbuf->sense_key == NOT_READY &&
1375                                  reqbuf->asc == 4) {
1376                                 /* The drive is in the process of loading
1377                                    a disk.  Retry, but wait a little to give
1378                                    the drive time to complete the load. */
1379                                 cdrom_sleep (HZ);
1380                         } else
1381                                 /* Otherwise, don't retry. */
1382                                 retries = 0;
1383 
1384                         --retries;
1385                 }
1386 
1387                 /* End of retry loop. */
1388         } while (pc->stat != 0 && retries >= 0);
1389 
1390 
1391         /* Return an error if the command failed. */
1392         if (pc->stat != 0)
1393                 return -EIO;
1394         else {
1395                 /* The command succeeded.  If it was anything other than
1396                    a request sense, eject, or door lock command,
1397                    and we think that the door is presently, lock it again.
1398                    (The door was probably unlocked via an explicit
1399                    CDROMEJECT ioctl.) */
1400                 if (CDROM_STATE_FLAGS (drive)->door_locked == 0 &&
1401                     (pc->c[0] != REQUEST_SENSE &&
1402                      pc->c[0] != ALLOW_MEDIUM_REMOVAL &&
1403                      pc->c[0] != START_STOP)) {
1404                         (void) cdrom_lockdoor (drive, 1, NULL);
1405                 }
1406                 return 0;
1407         }
1408 }
1409 
1410 
1411 /****************************************************************************
1412  * cdrom driver request routine.
1413  */
1414 
1415 void ide_do_rw_cdrom (ide_drive_t *drive, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
1416 {
1417         struct request *rq = HWGROUP(drive)->rq;
1418 
1419         if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1420                 cdrom_do_packet_command (drive);
1421         else if (rq -> cmd == RESET_DRIVE_COMMAND) {
1422                 cdrom_end_request (1, drive);
1423                 ide_do_reset (drive);
1424                 return;
1425         } else if (rq -> cmd != READ) {
1426                 printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1427                 cdrom_end_request (0, drive);
1428         } else
1429                 cdrom_start_read (drive, block);
1430 }
1431 
1432 
1433 
1434 /****************************************************************************
1435  * Ioctl handling.
1436  *
1437  * Routines which queue packet commands take as a final argument a pointer
1438  * to an atapi_request_sense struct.  If execution of the command results
1439  * in an error with a CHECK CONDITION status, this structure will be filled
1440  * with the results of the subsequent request sense command.  The pointer
1441  * can also be NULL, in which case no sense information is returned.
1442  */
1443 
1444 #if ! STANDARD_ATAPI
1445 static inline
1446 int bin2bcd (int x)
     /* [previous][next][first][last][top][bottom][index][help] */
1447 {
1448         return (x%10) | ((x/10) << 4);
1449 }
1450 
1451 
1452 static inline
1453 int bcd2bin (int x)
     /* [previous][next][first][last][top][bottom][index][help] */
1454 {
1455         return (x >> 4) * 10 + (x & 0x0f);
1456 }
1457 
1458 static
1459 void msf_from_bcd (struct atapi_msf *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
1460 {
1461         msf->minute = bcd2bin (msf->minute);
1462         msf->second = bcd2bin (msf->second);
1463         msf->frame  = bcd2bin (msf->frame);
1464 }
1465 
1466 #endif /* not STANDARD_ATAPI */
1467 
1468 
1469 static inline
1470 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
     /* [previous][next][first][last][top][bottom][index][help] */
1471 {
1472         lba += CD_BLOCK_OFFSET;
1473         lba &= 0xffffff;  /* negative lbas use only 24 bits */
1474         *m = lba / (CD_SECS * CD_FRAMES);
1475         lba %= (CD_SECS * CD_FRAMES);
1476         *s = lba / CD_FRAMES;
1477         *f = lba % CD_FRAMES;
1478 }
1479 
1480 
1481 static inline
1482 int msf_to_lba (byte m, byte s, byte f)
     /* [previous][next][first][last][top][bottom][index][help] */
1483 {
1484         return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
1485 }
1486 
1487 
1488 static int
1489 cdrom_check_status (ide_drive_t  *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
1490                     struct atapi_request_sense *reqbuf)
1491 {
1492         struct packet_command pc;
1493 
1494         memset (&pc, 0, sizeof (pc));
1495 
1496         pc.sense_data = reqbuf;
1497         pc.c[0] = TEST_UNIT_READY;
1498 
1499         return cdrom_queue_packet_command (drive, &pc);
1500 }
1501 
1502 
1503 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1504 static int
1505 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1506                 struct atapi_request_sense *reqbuf)
1507 {
1508         struct atapi_request_sense my_reqbuf;
1509         int stat;
1510         struct packet_command pc;
1511 
1512         if (reqbuf == NULL)
1513                 reqbuf = &my_reqbuf;
1514 
1515         /* If the drive cannot lock the door, just pretend. */
1516         if (CDROM_CONFIG_FLAGS (drive)->no_doorlock)
1517                 stat = 0;
1518         else {
1519                 memset (&pc, 0, sizeof (pc));
1520                 pc.sense_data = reqbuf;
1521 
1522                 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1523                 pc.c[4] = (lockflag != 0);
1524                 stat = cdrom_queue_packet_command (drive, &pc);
1525         }
1526 
1527         if (stat == 0)
1528                 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1529         else {
1530                 /* If we got an illegal field error, the drive
1531                    probably cannot lock the door. */
1532                 if (reqbuf->sense_key == ILLEGAL_REQUEST &&
1533                     reqbuf->asc == 0x24) {
1534                         printk ("%s: door locking not supported\n",
1535                                 drive->name);
1536                         CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1537                         stat = 0;
1538                         CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1539                 }
1540         }
1541   return stat;
1542 }
1543 
1544 
1545 /* Eject the disk if EJECTFLAG is 0.
1546    If EJECTFLAG is 1, try to reload the disk. */
1547 static int
1548 cdrom_eject (ide_drive_t *drive, int ejectflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1549              struct atapi_request_sense *reqbuf)
1550 {
1551         struct packet_command pc;
1552 
1553         memset (&pc, 0, sizeof (pc));
1554         pc.sense_data = reqbuf;
1555 
1556         pc.c[0] = START_STOP;
1557         pc.c[4] = 2 + (ejectflag != 0);
1558         return cdrom_queue_packet_command (drive, &pc);
1559 }
1560 
1561 
1562 static int
1563 cdrom_pause (ide_drive_t *drive, int pauseflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1564              struct atapi_request_sense *reqbuf)
1565 {
1566         struct packet_command pc;
1567 
1568         memset (&pc, 0, sizeof (pc));
1569         pc.sense_data = reqbuf;
1570 
1571         pc.c[0] = SCMD_PAUSE_RESUME;
1572         pc.c[8] = !pauseflag;
1573         return cdrom_queue_packet_command (drive, &pc);
1574 }
1575 
1576 
1577 static int
1578 cdrom_startstop (ide_drive_t *drive, int startflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1579                  struct atapi_request_sense *reqbuf)
1580 {
1581         struct packet_command pc;
1582 
1583         memset (&pc, 0, sizeof (pc));
1584         pc.sense_data = reqbuf;
1585 
1586         pc.c[0] = START_STOP;
1587         pc.c[1] = 1;
1588         pc.c[4] = startflag;
1589         return cdrom_queue_packet_command (drive, &pc);
1590 }
1591 
1592 
1593 static int
1594 cdrom_read_capacity (ide_drive_t *drive, unsigned *capacity,
     /* [previous][next][first][last][top][bottom][index][help] */
1595                      struct atapi_request_sense *reqbuf)
1596 {
1597         struct {
1598                 unsigned lba;
1599                 unsigned blocklen;
1600         } capbuf;
1601 
1602         int stat;
1603         struct packet_command pc;
1604 
1605         memset (&pc, 0, sizeof (pc));
1606         pc.sense_data = reqbuf;
1607 
1608         pc.c[0] = READ_CAPACITY;
1609         pc.buffer = (char *)&capbuf;
1610         pc.buflen = sizeof (capbuf);
1611 
1612         stat = cdrom_queue_packet_command (drive, &pc);
1613         if (stat == 0)
1614                 *capacity = ntohl (capbuf.lba);
1615 
1616         return stat;
1617 }
1618 
1619 
1620 static int
1621 cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
     /* [previous][next][first][last][top][bottom][index][help] */
1622                      int format, char *buf, int buflen,
1623                      struct atapi_request_sense *reqbuf)
1624 {
1625         struct packet_command pc;
1626 
1627         memset (&pc, 0, sizeof (pc));
1628         pc.sense_data = reqbuf;
1629 
1630         pc.buffer =  buf;
1631         pc.buflen = buflen;
1632         pc.c[0] = SCMD_READ_TOC;
1633         pc.c[6] = trackno;
1634         pc.c[7] = (buflen >> 8);
1635         pc.c[8] = (buflen & 0xff);
1636         pc.c[9] = (format << 6);
1637         if (msf_flag) pc.c[1] = 2;
1638         return cdrom_queue_packet_command (drive, &pc);
1639 }
1640 
1641 
1642 /* Try to read the entire TOC for the disk into our internal buffer. */
1643 static int
1644 cdrom_read_toc (ide_drive_t *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
1645                 struct atapi_request_sense *reqbuf)
1646 {
1647         int stat, ntracks, i;
1648         struct atapi_toc *toc = drive->cdrom_info.toc;
1649         struct {
1650                 struct atapi_toc_header hdr;
1651                 struct atapi_toc_entry  ent;
1652         } ms_tmp;
1653 
1654         if (toc == NULL) {
1655                 /* Try to allocate space. */
1656                 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1657                                                     GFP_KERNEL);
1658                 drive->cdrom_info.toc = toc;
1659         }
1660 
1661         if (toc == NULL) {
1662                 printk ("%s: No cdrom TOC buffer!\n", drive->name);
1663                 return -EIO;
1664         }
1665 
1666         /* Check to see if the existing data is still valid.
1667            If it is, just return. */
1668         if (CDROM_STATE_FLAGS (drive)->toc_valid)
1669                 (void) cdrom_check_status (drive, NULL);
1670 
1671         if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1672 
1673         /* First read just the header, so we know how long the TOC is. */
1674         stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1675                                     sizeof (struct atapi_toc_header) +
1676                                     sizeof (struct atapi_toc_entry),
1677                                     reqbuf);
1678         if (stat) return stat;
1679 
1680 #if ! STANDARD_ATAPI
1681         if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1682                 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1683                 toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
1684         }
1685 #endif  /* not STANDARD_ATAPI */
1686 
1687         ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1688         if (ntracks <= 0) return -EIO;
1689         if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1690 
1691         /* Now read the whole schmeer. */
1692         stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1693                                     sizeof (struct atapi_toc_header) +
1694                                     (ntracks+1) *
1695                                       sizeof (struct atapi_toc_entry),
1696                                     reqbuf);
1697         if (stat) return stat;
1698         toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1699 
1700 #if ! STANDARD_ATAPI
1701         if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1702                 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1703                 toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
1704         }
1705 #endif  /* not STANDARD_ATAPI */
1706 
1707         for (i=0; i<=ntracks; i++) {
1708 #if ! STANDARD_ATAPI
1709                 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1710                         if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1711                                 toc->ent[i].track = bcd2bin (toc->ent[i].track);
1712                         msf_from_bcd (&toc->ent[i].addr.msf);
1713                 }
1714 #endif  /* not STANDARD_ATAPI */
1715                 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1716                                                    toc->ent[i].addr.msf.second,
1717                                                    toc->ent[i].addr.msf.frame);
1718         }
1719 
1720         /* Read the multisession information. */
1721         stat = cdrom_read_tocentry (drive, 0, 1, 1,
1722                                     (char *)&ms_tmp, sizeof (ms_tmp),
1723                                     reqbuf);
1724         if (stat) return stat;
1725 
1726 #if ! STANDARD_ATAPI
1727         if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1728                 msf_from_bcd (&ms_tmp.ent.addr.msf);
1729 #endif  /* not STANDARD_ATAPI */
1730 
1731         toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1732                                             ms_tmp.ent.addr.msf.second,
1733                                             ms_tmp.ent.addr.msf.frame);
1734 
1735         toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1736 
1737         /* Now try to get the total cdrom capacity. */
1738         stat = cdrom_read_capacity (drive, &toc->capacity, reqbuf);
1739         if (stat) toc->capacity = 0x1fffff;
1740 
1741         HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS]
1742                 = toc->capacity * SECTORS_PER_FRAME;
1743         drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1744 
1745         /* Remember that we've read this stuff. */
1746         CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1747 
1748         return 0;
1749 }
1750 
1751 
1752 static int
1753 cdrom_read_subchannel (ide_drive_t *drive, int format,
     /* [previous][next][first][last][top][bottom][index][help] */
1754                        char *buf, int buflen,
1755                        struct atapi_request_sense *reqbuf)
1756 {
1757         struct packet_command pc;
1758 
1759         memset (&pc, 0, sizeof (pc));
1760         pc.sense_data = reqbuf;
1761 
1762         pc.buffer =  buf;
1763         pc.buflen = buflen;
1764         pc.c[0] = SCMD_READ_SUBCHANNEL;
1765         pc.c[1] = 2;     /* MSF addressing */
1766         pc.c[2] = 0x40;  /* request subQ data */
1767         pc.c[3] = format,
1768         pc.c[7] = (buflen >> 8);
1769         pc.c[8] = (buflen & 0xff);
1770         return cdrom_queue_packet_command (drive, &pc);
1771 }
1772 
1773 
1774 /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1775 static int
1776 cdrom_mode_sense (ide_drive_t *drive, int pageno, int modeflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1777                   char *buf, int buflen,
1778                   struct atapi_request_sense *reqbuf)
1779 {
1780         struct packet_command pc;
1781 
1782         memset (&pc, 0, sizeof (pc));
1783         pc.sense_data = reqbuf;
1784 
1785         pc.buffer =  buf;
1786         pc.buflen = buflen;
1787         pc.c[0] = MODE_SENSE_10;
1788         pc.c[2] = pageno | (modeflag << 6);
1789         pc.c[7] = (buflen >> 8);
1790         pc.c[8] = (buflen & 0xff);
1791         return cdrom_queue_packet_command (drive, &pc);
1792 }
1793 
1794 
1795 static int
1796 cdrom_mode_select (ide_drive_t *drive, int pageno, char *buf, int buflen,
     /* [previous][next][first][last][top][bottom][index][help] */
1797                    struct atapi_request_sense *reqbuf)
1798 {
1799         struct packet_command pc;
1800 
1801         memset (&pc, 0, sizeof (pc));
1802         pc.sense_data = reqbuf;
1803 
1804         pc.buffer =  buf;
1805         pc.buflen = - buflen;
1806         pc.c[0] = MODE_SELECT_10;
1807         pc.c[1] = 0x10;
1808         pc.c[2] = pageno;
1809         pc.c[7] = (buflen >> 8);
1810         pc.c[8] = (buflen & 0xff);
1811         return cdrom_queue_packet_command (drive, &pc);
1812 }
1813 
1814 
1815 static int
1816 cdrom_play_lba_range_1 (ide_drive_t *drive, int lba_start, int lba_end,
     /* [previous][next][first][last][top][bottom][index][help] */
1817                             struct atapi_request_sense *reqbuf)
1818 {
1819         struct packet_command pc;
1820 
1821         memset (&pc, 0, sizeof (pc));
1822         pc.sense_data = reqbuf;
1823 
1824         pc.c[0] = SCMD_PLAYAUDIO_MSF;
1825         lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1826         lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1827 
1828 #if ! STANDARD_ATAPI
1829         if (CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd) {
1830                 pc.c[3] = bin2bcd (pc.c[3]);
1831                 pc.c[4] = bin2bcd (pc.c[4]);
1832                 pc.c[5] = bin2bcd (pc.c[5]);
1833                 pc.c[6] = bin2bcd (pc.c[6]);
1834                 pc.c[7] = bin2bcd (pc.c[7]);
1835                 pc.c[8] = bin2bcd (pc.c[8]);
1836         }
1837 #endif /* not STANDARD_ATAPI */
1838 
1839         return cdrom_queue_packet_command (drive, &pc);
1840 }
1841 
1842 
1843 /* Play audio starting at LBA LBA_START and finishing with the
1844    LBA before LBA_END. */
1845 static int
1846 cdrom_play_lba_range (ide_drive_t *drive, int lba_start, int lba_end,
     /* [previous][next][first][last][top][bottom][index][help] */
1847                       struct atapi_request_sense *reqbuf)
1848 {
1849         int i, stat;
1850         struct atapi_request_sense my_reqbuf;
1851 
1852         if (reqbuf == NULL)
1853                 reqbuf = &my_reqbuf;
1854 
1855         /* Some drives, will, for certain audio cds,
1856            give an error if you ask them to play the entire cd using the
1857            values which are returned in the TOC.  The play will succeed,
1858            however, if the ending address is adjusted downwards
1859            by a few frames. */
1860         for (i=0; i<75; i++) {
1861                 stat = cdrom_play_lba_range_1 (drive, lba_start, lba_end,
1862                                                reqbuf);
1863 
1864                 if (stat == 0 ||
1865                     !(reqbuf->sense_key == ILLEGAL_REQUEST &&
1866                       reqbuf->asc == 0x24))
1867                         return stat;
1868 
1869                 --lba_end;
1870                 if (lba_end <= lba_start) break;
1871         }
1872 
1873         return stat;
1874 }
1875 
1876 
1877 static
1878 int cdrom_get_toc_entry (ide_drive_t *drive, int track,
     /* [previous][next][first][last][top][bottom][index][help] */
1879                          struct atapi_toc_entry **ent,
1880                          struct atapi_request_sense *reqbuf)
1881 {
1882         int stat, ntracks;
1883         struct atapi_toc *toc;
1884 
1885         /* Make sure our saved TOC is valid. */
1886         stat = cdrom_read_toc (drive, reqbuf);
1887         if (stat) return stat;
1888 
1889         toc = drive->cdrom_info.toc;
1890 
1891         /* Check validity of requested track number. */
1892         ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1893         if (track == CDROM_LEADOUT)
1894                 *ent = &toc->ent[ntracks];
1895         else if (track < toc->hdr.first_track ||
1896                  track > toc->hdr.last_track)
1897                 return -EINVAL;
1898         else
1899                 *ent = &toc->ent[track - toc->hdr.first_track];
1900 
1901         return 0;
1902 }
1903 
1904 
1905 static int
1906 cdrom_read_block (ide_drive_t *drive, int format, int lba,
     /* [previous][next][first][last][top][bottom][index][help] */
1907                   char *buf, int buflen,
1908                   struct atapi_request_sense *reqbuf)
1909 {
1910         struct packet_command pc;
1911         struct atapi_request_sense my_reqbuf;
1912         int stat;
1913 
1914         if (reqbuf == NULL)
1915                 reqbuf = &my_reqbuf;
1916 
1917         memset (&pc, 0, sizeof (pc));
1918         pc.sense_data = reqbuf;
1919 
1920         pc.buffer = buf;
1921         pc.buflen = buflen;
1922 
1923 #if ! STANDARD_ATAPI
1924         if (CDROM_CONFIG_FLAGS (drive)->old_readcd)
1925                 pc.c[0] = 0xd4;
1926         else
1927 #endif  /* not STANDARD_ATAPI */
1928                 pc.c[0] = READ_CD;
1929 
1930         pc.c[1] = (format << 2);
1931 #ifdef __alpha__
1932         stl_u(htonl (lba), (unsigned int *) &pc.c[2]);
1933 #else
1934         *(int *)(&pc.c[2]) = htonl (lba);
1935 #endif
1936         pc.c[8] = 1;  /* one block */
1937         pc.c[9] = 0x10;
1938 
1939         stat = cdrom_queue_packet_command (drive, &pc);
1940 
1941 #if ! STANDARD_ATAPI
1942         /* If the drive doesn't recognize the READ CD opcode, retry the command
1943            with an older opcode for that command. */
1944         if (stat && reqbuf->sense_key == ILLEGAL_REQUEST &&
1945             reqbuf->asc == 0x20 &&
1946             CDROM_CONFIG_FLAGS (drive)->old_readcd == 0) {
1947                 printk ("%s: Drive does not recognize READ_CD;"
1948                         "trying opcode 0xd4\n",
1949                         drive->name);
1950                 CDROM_CONFIG_FLAGS (drive)->old_readcd = 1;
1951                 return cdrom_read_block (drive, format, lba, buf, buflen,
1952                                          reqbuf);
1953         }
1954 #endif  /* not STANDARD_ATAPI */
1955 
1956         return stat;
1957 }
1958 
1959 
1960 int ide_cdrom_ioctl (ide_drive_t *drive, struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1961                      struct file *file, unsigned int cmd, unsigned long arg)
1962 {
1963         switch (cmd) {
1964         case CDROMEJECT: {
1965                 int stat;
1966 
1967                 if (drive->usage > 1)
1968                         return -EBUSY;
1969 
1970                 stat = cdrom_lockdoor (drive, 0, NULL);
1971                 if (stat) return stat;
1972 
1973                 return cdrom_eject (drive, 0, NULL);
1974         }
1975 
1976         case CDROMCLOSETRAY: {
1977                 int stat;
1978                 if (drive->usage > 1)
1979                         return -EBUSY;
1980 
1981                 stat = cdrom_eject (drive, 1, NULL);
1982                 if (stat) return stat;
1983 
1984                 return cdrom_lockdoor (drive, 1, NULL);
1985         }
1986 
1987         case CDROMEJECT_SW: {
1988                 CDROM_STATE_FLAGS (drive)->eject_on_close = arg;
1989                 return 0;
1990         }
1991 
1992         case CDROMPAUSE:
1993                 return cdrom_pause (drive, 1, NULL);
1994 
1995         case CDROMRESUME:
1996                 return cdrom_pause (drive, 0, NULL);
1997 
1998         case CDROMSTART:
1999                 return cdrom_startstop (drive, 1, NULL);
2000 
2001         case CDROMSTOP: {
2002                 int stat;
2003 
2004                 stat = cdrom_startstop (drive, 0, NULL);
2005                 if (stat) return stat;
2006                 /* pit says the Dolphin needs this. */
2007                 return cdrom_eject (drive, 1, NULL);
2008       }
2009 
2010         case CDROMPLAYMSF: {
2011                 struct cdrom_msf msf;
2012                 int stat, lba_start, lba_end;
2013 
2014                 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
2015                 if (stat) return stat;
2016 
2017                 memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
2018 
2019                 lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
2020                                         msf.cdmsf_frame0);
2021                 lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
2022                                       msf.cdmsf_frame1) + 1;
2023 
2024                 if (lba_end <= lba_start) return -EINVAL;
2025 
2026                 return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2027         }
2028 
2029         /* Like just about every other Linux cdrom driver, we ignore the
2030            index part of the request here. */
2031         case CDROMPLAYTRKIND: {
2032                 int stat, lba_start, lba_end;
2033                 struct cdrom_ti ti;
2034                 struct atapi_toc_entry *first_toc, *last_toc;
2035 
2036                 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
2037                 if (stat) return stat;
2038 
2039                 memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
2040 
2041                 stat = cdrom_get_toc_entry (drive, ti.cdti_trk0, &first_toc,
2042                                             NULL);
2043                 if (stat) return stat;
2044                 stat = cdrom_get_toc_entry (drive, ti.cdti_trk1, &last_toc,
2045                                             NULL);
2046                 if (stat) return stat;
2047 
2048                 if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
2049                 lba_start = first_toc->addr.lba;
2050                 lba_end   = last_toc->addr.lba;
2051 
2052                 if (lba_end <= lba_start) return -EINVAL;
2053 
2054                 return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2055         }
2056 
2057         case CDROMREADTOCHDR: {
2058                 int stat;
2059                 struct cdrom_tochdr tochdr;
2060                 struct atapi_toc *toc;
2061 
2062                 stat = verify_area (VERIFY_WRITE, (void *) arg,
2063                                     sizeof (tochdr));
2064                 if (stat) return stat;
2065 
2066                 /* Make sure our saved TOC is valid. */
2067                 stat = cdrom_read_toc (drive, NULL);
2068                 if (stat) return stat;
2069 
2070                 toc = drive->cdrom_info.toc;
2071                 tochdr.cdth_trk0 = toc->hdr.first_track;
2072                 tochdr.cdth_trk1 = toc->hdr.last_track;
2073 
2074                 memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
2075 
2076                 return stat;
2077         }
2078 
2079         case CDROMREADTOCENTRY: {
2080                 int stat;
2081                 struct cdrom_tocentry tocentry;
2082                 struct atapi_toc_entry *toce;
2083 
2084                 stat = verify_area (VERIFY_WRITE, (void *) arg,
2085                                     sizeof (tocentry));
2086                 if (stat) return stat;
2087 
2088                 memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
2089 
2090                 stat = cdrom_get_toc_entry (drive, tocentry.cdte_track, &toce,
2091                                             NULL);
2092                 if (stat) return stat;
2093 
2094                 tocentry.cdte_ctrl = toce->control;
2095                 tocentry.cdte_adr  = toce->adr;
2096 
2097                 if (tocentry.cdte_format == CDROM_MSF) {
2098                         /* convert to MSF */
2099                         lba_to_msf (toce->addr.lba,
2100                                     &tocentry.cdte_addr.msf.minute,
2101                                     &tocentry.cdte_addr.msf.second,
2102                                     &tocentry.cdte_addr.msf.frame);
2103                 } else
2104                         tocentry.cdte_addr.lba = toce->addr.lba;
2105 
2106                 memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
2107 
2108                 return stat;
2109         }
2110 
2111         case CDROMSUBCHNL: {
2112                 struct atapi_cdrom_subchnl scbuf;
2113                 int stat;
2114                 struct cdrom_subchnl subchnl;
2115 
2116                 stat = verify_area (VERIFY_WRITE, (void *) arg,
2117                                     sizeof (subchnl));
2118                 if (stat) return stat;
2119 
2120                 memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
2121 
2122                 stat = cdrom_read_subchannel (drive, 1, /* current position */
2123                                               (char *)&scbuf, sizeof (scbuf),
2124                                               NULL);
2125                 if (stat) return stat;
2126 
2127 #if ! STANDARD_ATAPI
2128                 if (CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd) {
2129                         msf_from_bcd (&scbuf.acdsc_absaddr.msf);
2130                         msf_from_bcd (&scbuf.acdsc_reladdr.msf);
2131                 }
2132                 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
2133                         scbuf.acdsc_trk = bcd2bin (scbuf.acdsc_trk);
2134 #endif /* not STANDARD_ATAPI */
2135 
2136                 if (subchnl.cdsc_format == CDROM_MSF) {
2137                         subchnl.cdsc_absaddr.msf.minute =
2138                                 scbuf.acdsc_absaddr.msf.minute;
2139                         subchnl.cdsc_absaddr.msf.second =
2140                                 scbuf.acdsc_absaddr.msf.second;
2141                         subchnl.cdsc_absaddr.msf.frame =
2142                                 scbuf.acdsc_absaddr.msf.frame;
2143 
2144                         subchnl.cdsc_reladdr.msf.minute =
2145                                 scbuf.acdsc_reladdr.msf.minute;
2146                         subchnl.cdsc_reladdr.msf.second =
2147                                 scbuf.acdsc_reladdr.msf.second;
2148                         subchnl.cdsc_reladdr.msf.frame =
2149                                 scbuf.acdsc_reladdr.msf.frame;
2150                 } else {
2151                         subchnl.cdsc_absaddr.lba =
2152                                 msf_to_lba (scbuf.acdsc_absaddr.msf.minute,
2153                                             scbuf.acdsc_absaddr.msf.second,
2154                                             scbuf.acdsc_absaddr.msf.frame);
2155                         subchnl.cdsc_reladdr.lba =
2156                                 msf_to_lba (scbuf.acdsc_reladdr.msf.minute,
2157                                             scbuf.acdsc_reladdr.msf.second,
2158                                             scbuf.acdsc_reladdr.msf.frame);
2159                 }
2160 
2161                 subchnl.cdsc_audiostatus = scbuf.acdsc_audiostatus;
2162                 subchnl.cdsc_ctrl = scbuf.acdsc_ctrl;
2163                 subchnl.cdsc_trk  = scbuf.acdsc_trk;
2164                 subchnl.cdsc_ind  = scbuf.acdsc_ind;
2165 
2166                 memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
2167 
2168                 return stat;
2169         }
2170 
2171         case CDROMVOLCTRL: {
2172                 struct cdrom_volctrl volctrl;
2173                 char buffer[24], mask[24];
2174                 int stat;
2175 
2176                 stat = verify_area (VERIFY_READ, (void *) arg,
2177                                     sizeof (volctrl));
2178                 if (stat) return stat;
2179                 memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
2180 
2181                 stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2182                                          sizeof (buffer), NULL);
2183                 if (stat) return stat;
2184                 stat = cdrom_mode_sense (drive, 0x0e, 1, mask,
2185                                          sizeof (buffer), NULL);
2186                 if (stat) return stat;
2187 
2188                 buffer[1] = buffer[2] = 0;
2189 
2190                 buffer[17] = volctrl.channel0 & mask[17];
2191                 buffer[19] = volctrl.channel1 & mask[19];
2192                 buffer[21] = volctrl.channel2 & mask[21];
2193                 buffer[23] = volctrl.channel3 & mask[23];
2194 
2195                 return cdrom_mode_select (drive, 0x0e, buffer,
2196                                           sizeof (buffer), NULL);
2197         }
2198 
2199         case CDROMVOLREAD: {
2200                 struct cdrom_volctrl volctrl;
2201                 char buffer[24];
2202                 int stat;
2203 
2204                 stat = verify_area (VERIFY_WRITE, (void *) arg,
2205                                     sizeof (volctrl));
2206                 if (stat) return stat;
2207 
2208                 stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2209                                          sizeof (buffer), NULL);
2210                 if (stat) return stat;
2211 
2212                 volctrl.channel0 = buffer[17];
2213                 volctrl.channel1 = buffer[19];
2214                 volctrl.channel2 = buffer[21];
2215                 volctrl.channel3 = buffer[23];
2216 
2217                 memcpy_tofs ((void *) arg, &volctrl, sizeof (volctrl));
2218 
2219                 return 0;
2220         }
2221 
2222         case CDROMMULTISESSION: {
2223                 struct cdrom_multisession ms_info;
2224                 struct atapi_toc *toc;
2225                 int stat;
2226 
2227                 stat = verify_area (VERIFY_WRITE, (void *)arg,
2228                                     sizeof (ms_info));
2229                 if (stat) return stat;
2230 
2231                 memcpy_fromfs (&ms_info, (void *)arg, sizeof (ms_info));
2232 
2233                 /* Make sure the TOC information is valid. */
2234                 stat = cdrom_read_toc (drive, NULL);
2235                 if (stat) return stat;
2236 
2237                 toc = drive->cdrom_info.toc;
2238 
2239                 if (ms_info.addr_format == CDROM_MSF)
2240                         lba_to_msf (toc->last_session_lba,
2241                                     &ms_info.addr.msf.minute,
2242                                     &ms_info.addr.msf.second,
2243                                     &ms_info.addr.msf.frame);
2244                 else if (ms_info.addr_format == CDROM_LBA)
2245                         ms_info.addr.lba = toc->last_session_lba;
2246                 else
2247                         return -EINVAL;
2248 
2249                 ms_info.xa_flag = toc->xa_flag;
2250 
2251                 memcpy_tofs ((void *)arg, &ms_info, sizeof (ms_info));
2252 
2253                 return 0;
2254         }
2255 
2256         /* Read 2352 byte blocks from audio tracks. */
2257         case CDROMREADAUDIO: {
2258                 int stat, lba;
2259                 struct atapi_toc *toc;
2260                 struct cdrom_read_audio ra;
2261                 char buf[CD_FRAMESIZE_RAW];
2262 
2263                 /* Make sure the TOC is up to date. */
2264                 stat = cdrom_read_toc (drive, NULL);
2265                 if (stat) return stat;
2266 
2267                 toc = drive->cdrom_info.toc;
2268 
2269                 stat = verify_area (VERIFY_READ, (char *)arg, sizeof (ra));
2270                 if (stat) return stat;
2271 
2272                 memcpy_fromfs (&ra, (void *)arg, sizeof (ra));
2273 
2274                 if (ra.nframes < 0 || ra.nframes > toc->capacity)
2275                         return -EINVAL;
2276                 else if (ra.nframes == 0)
2277                         return 0;
2278 
2279                 stat = verify_area (VERIFY_WRITE, (char *)ra.buf,
2280                                     ra.nframes * CD_FRAMESIZE_RAW);
2281                 if (stat) return stat;
2282 
2283                 if (ra.addr_format == CDROM_MSF)
2284                         lba = msf_to_lba (ra.addr.msf.minute,
2285                                           ra.addr.msf.second,
2286                                           ra.addr.msf.frame);
2287                 else if (ra.addr_format == CDROM_LBA)
2288                         lba = ra.addr.lba;
2289                 else
2290                         return -EINVAL;
2291 
2292                 if (lba < 0 || lba >= toc->capacity)
2293                         return -EINVAL;
2294 
2295                 while (ra.nframes > 0) {
2296                         stat = cdrom_read_block (drive, 1, lba, buf,
2297                                                  CD_FRAMESIZE_RAW, NULL);
2298                         if (stat) return stat;
2299                         memcpy_tofs (ra.buf, buf, CD_FRAMESIZE_RAW);
2300                         ra.buf += CD_FRAMESIZE_RAW;
2301                         --ra.nframes;
2302                         ++lba;
2303                 }
2304 
2305                 return 0;
2306         }
2307 
2308         case CDROMREADMODE1:
2309         case CDROMREADMODE2: {
2310                 struct cdrom_msf msf;
2311                 int blocksize, format, stat, lba;
2312                 struct atapi_toc *toc;
2313                 char buf[CD_FRAMESIZE_RAW0];
2314 
2315                 if (cmd == CDROMREADMODE1) {
2316                         blocksize = CD_FRAMESIZE;
2317                         format = 2;
2318                 } else {
2319                         blocksize = CD_FRAMESIZE_RAW0;
2320                         format = 3;
2321                 }
2322 
2323                 stat = verify_area (VERIFY_WRITE, (char *)arg, blocksize);
2324                 if (stat) return stat;
2325 
2326                 memcpy_fromfs (&msf, (void *)arg, sizeof (msf));
2327 
2328                 lba = msf_to_lba (msf.cdmsf_min0,
2329                                   msf.cdmsf_sec0,
2330                                   msf.cdmsf_frame0);
2331         
2332                 /* Make sure the TOC is up to date. */
2333                 stat = cdrom_read_toc (drive, NULL);
2334                 if (stat) return stat;
2335 
2336                 toc = drive->cdrom_info.toc;
2337 
2338                 if (lba < 0 || lba >= toc->capacity)
2339                         return -EINVAL;
2340 
2341                 stat = cdrom_read_block (drive, format, lba, buf, blocksize,
2342                                          NULL);
2343                 if (stat) return stat;
2344 
2345                 memcpy_tofs ((char *)arg, buf, blocksize);
2346                 return 0;
2347         }
2348 
2349         case CDROM_GET_UPC: {
2350                 int stat;
2351                 char mcnbuf[24];
2352                 struct cdrom_mcn mcn;
2353 
2354                 stat = verify_area (VERIFY_WRITE, (void *) arg,
2355                                     sizeof (mcn));
2356                 if (stat) return stat;
2357 
2358                 stat = cdrom_read_subchannel (drive, 2, /* get MCN */
2359                                               mcnbuf, sizeof (mcnbuf),
2360                                               NULL);
2361                 if (stat) return stat;
2362 
2363                 memcpy (mcn.medium_catalog_number, mcnbuf+9,
2364                         sizeof (mcn.medium_catalog_number)-1);
2365                 mcn.medium_catalog_number[sizeof (mcn.medium_catalog_number)-1]
2366                         = '\0';
2367 
2368                 memcpy_tofs ((void *) arg, &mcn, sizeof (mcn));
2369 
2370                 return stat;
2371         }
2372 
2373 #if 0 /* Doesn't work reliably yet. */
2374         case CDROMRESET: {
2375                 struct request req;
2376                 ide_init_drive_cmd (&req);
2377                 req.cmd = RESET_DRIVE_COMMAND;
2378                 return ide_do_drive_cmd (drive, &req, ide_wait);
2379         }
2380 #endif
2381 
2382  
2383 #ifdef TEST
2384         case 0x1234: {
2385                 int stat;
2386                 struct packet_command pc;
2387                 int len, lena;
2388 
2389                 memset (&pc, 0, sizeof (pc));
2390 
2391                 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
2392                 if (stat) return stat;
2393                 memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
2394                 arg += sizeof (pc.c);
2395 
2396                 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (len));
2397                 if (stat) return stat;
2398                 memcpy_fromfs (&len, (void *) arg , sizeof (len));
2399                 arg += sizeof (len);
2400 
2401                 if (len > 0) {
2402                         stat = verify_area (VERIFY_WRITE, (void *) arg, len);
2403                         if (stat) return stat;
2404                 }
2405 
2406                 lena = len;
2407                 if (lena  < 0) lena = 0;
2408 
2409                 {
2410                         char buf[lena];
2411                         if (len > 0) {
2412                                 pc.buflen = len;
2413                                 pc.buffer = buf;
2414                         }
2415 
2416                         stat = cdrom_queue_packet_command (drive, &pc);
2417 
2418                         if (len > 0)
2419                                 memcpy_tofs ((void *)arg, buf, len);
2420                 }
2421 
2422                 return stat;
2423         }
2424 #endif
2425 
2426         default:
2427                 return -EPERM;
2428         }
2429 
2430 }
2431 
2432 
2433 
2434 /****************************************************************************
2435  * Other driver requests (open, close, check media change).
2436  */
2437 
2438 int ide_cdrom_check_media_change (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2439 {
2440         int retval;
2441 
2442         (void) cdrom_check_status (drive, NULL);
2443 
2444         retval = CDROM_STATE_FLAGS (drive)->media_changed;
2445         CDROM_STATE_FLAGS (drive)->media_changed = 0;
2446 
2447         return retval;
2448 }
2449 
2450 
2451 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2452 {
2453   /* no write access */
2454         if (fp->f_mode & 2) {
2455                 --drive->usage;
2456                 return -EROFS;
2457         }
2458 
2459         /* If this is the first open, check the drive status. */
2460         if (drive->usage == 1) {
2461                 int stat;
2462                 struct atapi_request_sense my_reqbuf;
2463                 my_reqbuf.sense_key = 0;
2464 
2465                 /* Get the drive status. */
2466                 stat = cdrom_check_status (drive, &my_reqbuf);
2467 
2468                 /* If the tray is open, try to close it. */
2469                 if (stat && my_reqbuf.sense_key == NOT_READY) {
2470                         cdrom_eject (drive, 1, &my_reqbuf);
2471                         stat = cdrom_check_status (drive, &my_reqbuf);
2472                 }
2473 
2474                 /* Return an error if there are still problems. */
2475                 if (stat && my_reqbuf.sense_key != UNIT_ATTENTION) {
2476                         --drive->usage;
2477                         return -ENXIO;
2478                 }
2479 
2480                 /* Now lock the door. */
2481                 (void) cdrom_lockdoor (drive, 1, &my_reqbuf);
2482 
2483                 /* And try to read the TOC information now. */
2484                 (void) cdrom_read_toc (drive, &my_reqbuf);
2485         }
2486 
2487         return 0;
2488 }
2489 
2490 
2491 /*
2492  * Close down the device.  Invalidate all cached blocks.
2493  */
2494 
2495 void ide_cdrom_release (struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
2496                         ide_drive_t *drive)
2497 {
2498         if (drive->usage == 0) {
2499                 invalidate_buffers (inode->i_rdev);
2500 
2501                 /* Unlock the door. */
2502                 (void) cdrom_lockdoor (drive, 0, NULL);
2503 
2504                 /* Do an eject if we were requested to do so. */
2505                 if (CDROM_STATE_FLAGS (drive)->eject_on_close)
2506                         (void) cdrom_eject (drive, 0, NULL);
2507         }
2508 }
2509 
2510 
2511 
2512 /****************************************************************************
2513  * Device initialization.
2514  */
2515 
2516 void ide_cdrom_setup (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2517 {
2518         blksize_size[HWIF(drive)->major][drive->select.b.unit << PARTN_BITS] =
2519                 CD_FRAMESIZE;
2520 
2521         drive->special.all = 0;
2522         drive->ready_stat = 0;
2523 
2524         CDROM_STATE_FLAGS (drive)->media_changed = 0;
2525         CDROM_STATE_FLAGS (drive)->toc_valid     = 0;
2526         CDROM_STATE_FLAGS (drive)->door_locked   = 0;
2527 
2528         /* Turn this off by default, since many people don't like it. */
2529         CDROM_STATE_FLAGS (drive)->eject_on_close= 0;
2530 
2531 #if NO_DOOR_LOCKING
2532         CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2533 #else
2534         CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2535 #endif
2536 
2537         if (drive->id != NULL)
2538                 CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2539                         ((drive->id->config & 0x0060) == 0x20);
2540         else
2541                 CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2542 
2543 #if ! STANDARD_ATAPI
2544         CDROM_CONFIG_FLAGS (drive)->old_readcd = 0;
2545         CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2546         CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2547         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2548         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2549 
2550         if (drive->id != NULL) {
2551                 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2552                     drive->id->fw_rev[4] == '1' &&
2553                     drive->id->fw_rev[6] <= '2') {
2554                         /* Vertos 300.
2555                            Some versions of this drive like to talk BCD. */
2556                         CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2557                         CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2558                         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2559                         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2560                 }
2561 
2562                 else if (strcmp (drive->id->model,
2563                                  "NEC CD-ROM DRIVE:260") == 0 &&
2564                          strcmp (drive->id->fw_rev, "1.01") == 0) {
2565                         /* Old NEC260 (not R). */
2566                         CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2567                         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2568                         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2569                 }
2570 
2571                 else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
2572                          strcmp (drive->id->fw_rev, "A1.1") == 0) {
2573                         /* Wearnes */
2574                         CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2575                         CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2576                 }
2577         }
2578 #endif /* not STANDARD_ATAPI */
2579 
2580         drive->cdrom_info.toc               = NULL;
2581         drive->cdrom_info.sector_buffer     = NULL;
2582         drive->cdrom_info.sector_buffered   = 0;
2583         drive->cdrom_info.nsectors_buffered = 0;
2584 }
2585 
2586 
2587 
2588 /*
2589  * TODO:
2590  *  CDROMRESET
2591  *  Lock the door when a read request completes successfully and the
2592  *   door is not already locked.  Also try to reorganize to reduce
2593  *   duplicated functionality between read and ioctl paths?
2594  *  Establish interfaces for an IDE port driver, and break out the cdrom
2595  *   code into a loadable module.
2596  *  Support changers.
2597  *  Write some real documentation.
2598  */
2599 
2600 
2601 
2602 /*==========================================================================*/
2603 /*
2604  * Local variables:
2605  * c-basic-offset: 8
2606  * End:
2607  */

/* [previous][next][first][last][top][bottom][index][help] */