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_request
  22. cdrom_queue_packet_command
  23. ide_do_rw_cdrom
  24. byte_swap_word
  25. byte_swap_long
  26. bin2bcd
  27. lba_to_msf
  28. msf_to_lba
  29. cdrom_check_status
  30. cdrom_lockdoor
  31. cdrom_eject
  32. cdrom_pause
  33. cdrom_startstop
  34. cdrom_read_capacity
  35. cdrom_read_tocentry
  36. cdrom_read_toc
  37. cdrom_read_subchannel
  38. cdrom_mode_sense
  39. cdrom_mode_select
  40. cdrom_play_lba_range_play12
  41. cdrom_play_lba_range_msf
  42. cdrom_play_lba_range
  43. cdrom_get_toc_entry
  44. cdrom_read_block
  45. ide_cdrom_ioctl
  46. ide_cdrom_check_media_change
  47. ide_cdrom_open
  48. ide_cdrom_release
  49. ide_cdrom_setup

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

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