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

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