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

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