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

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