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

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