root/drivers/block/ide-cd.c

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

DEFINITIONS

This source file includes following definitions.
  1. cdrom_in_bytes
  2. cdrom_out_bytes
  3. cdrom_analyze_sense_data
  4. restore_request
  5. cdrom_queue_request_sense
  6. cdrom_end_request
  7. cdrom_saw_media_change
  8. cdrom_decode_status
  9. cdrom_start_packet_command
  10. cdrom_transfer_packet_command
  11. cdrom_buffer_sectors
  12. cdrom_read_check_ireason
  13. cdrom_read_intr
  14. cdrom_read_from_buffer
  15. cdrom_start_read_continuation
  16. cdrom_start_read
  17. cdrom_pc_intr
  18. cdrom_do_pc_continuation
  19. cdrom_do_packet_command
  20. cdrom_sleep
  21. cdrom_queue_packet_command
  22. ide_do_rw_cdrom
  23. bin2bcd
  24. bcd2bin
  25. lba_to_msf
  26. msf_to_lba
  27. cdrom_check_status
  28. cdrom_lockdoor
  29. cdrom_eject
  30. cdrom_pause
  31. cdrom_startstop
  32. cdrom_read_capacity
  33. cdrom_read_tocentry
  34. cdrom_read_toc
  35. cdrom_read_subchannel
  36. cdrom_mode_sense
  37. cdrom_mode_select
  38. cdrom_play_lba_range_play12
  39. cdrom_play_lba_range_msf
  40. cdrom_play_lba_range
  41. cdrom_get_toc_entry
  42. cdrom_read_block
  43. ide_cdrom_ioctl
  44. ide_cdrom_check_media_change
  45. ide_cdrom_open
  46. ide_cdrom_release
  47. ide_cdrom_setup

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

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