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

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