root/drivers/block/ide-cd.c

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

DEFINITIONS

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

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