root/drivers/block/ide-cd.c

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

DEFINITIONS

This source file includes following definitions.
  1. cdrom_analyze_sense_data
  2. restore_request
  3. cdrom_queue_request_sense
  4. cdrom_end_request
  5. cdrom_saw_media_change
  6. cdrom_decode_status
  7. cdrom_start_packet_command
  8. cdrom_transfer_packet_command
  9. cdrom_buffer_sectors
  10. cdrom_read_check_ireason
  11. cdrom_read_intr
  12. cdrom_read_from_buffer
  13. cdrom_start_read_continuation
  14. cdrom_start_read
  15. cdrom_pc_intr
  16. cdrom_do_pc_continuation
  17. cdrom_do_packet_command
  18. cdrom_queue_packet_command
  19. ide_do_rw_cdrom
  20. byte_swap_word
  21. byte_swap_long
  22. bin2bcd
  23. lba_to_msf
  24. msf_to_lba
  25. cdrom_check_status
  26. cdrom_request_sense
  27. cdrom_lockdoor
  28. cdrom_eject
  29. cdrom_pause
  30. cdrom_startstop
  31. cdrom_read_tocentry
  32. cdrom_read_toc
  33. cdrom_read_subchannel
  34. cdrom_mode_sense
  35. cdrom_mode_select
  36. cdrom_play_lba_range_play12
  37. cdrom_play_lba_range_msf
  38. cdrom_play_lba_range
  39. cdrom_get_toc_entry
  40. ide_cdrom_ioctl
  41. ide_cdrom_check_media_change
  42. ide_cdrom_open
  43. ide_cdrom_release
  44. 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  *
  45  * FIX ME!!  A day-one bug exists when the ide.c "serialize" option is used.
  46  * For this to always work correctly, ide_set_handler() must be called
  47  * *just before* the final trigger is given to the drive (to cause it to go
  48  * off and get data and then interrupt us again).  Otherwise, we may get the
  49  * interrupt before set_handler() has actually run, resulting in "unexpected_intr".
  50  *
  51  * This can only happen in scenarios where we handle a "final" interrupt
  52  * for one IDE port on, say irq14, and then initiate a new request for the
  53  * other port on, say irq15, from the irq14 interrupt handler.  If we are
  54  * running with "unmask" on, or have done sti(), then Whammo -- we're exposed.
  55  *
  56  * Places where this needs fixing have been identified in the code with "BUG".
  57  * -ml  August 11, 1995
  58  *
  59  *
  60  * ATAPI cd-rom driver.  To be used with ide.c.
  61  *
  62  * Copyright (C) 1994, 1995  scott snyder  <snyder@fnald0.fnal.gov>
  63  * May be copied or modified under the terms of the GNU General Public License
  64  * (../../COPYING).
  65  */
  66 
  67 
  68 /***************************************************************************/
  69 
  70 #include <linux/config.h>
  71 #include <linux/types.h>
  72 #include <linux/kernel.h>
  73 #include <linux/delay.h>
  74 #include <linux/timer.h>
  75 #include <linux/malloc.h>
  76 #include <linux/ioport.h>
  77 #include <linux/interrupt.h>
  78 #include <linux/blkdev.h>
  79 #include <linux/errno.h>
  80 #include <linux/hdreg.h>
  81 #include <linux/cdrom.h>
  82 #include <asm/irq.h>
  83 
  84 #define _IDE_CD_C       /* used in blk.h */
  85 #include "ide.h"
  86 
  87 /* Turn this on to have the driver print out the meanings of the
  88    ATAPI error codes.  This will use up additional kernel-space
  89    memory, though. */
  90 
  91 #ifndef VERBOSE_IDE_CD_ERRORS
  92 #define VERBOSE_IDE_CD_ERRORS 0
  93 #endif
  94 
  95 #define SECTOR_SIZE 512
  96 #define SECTOR_BITS 9
  97 #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
  98 
  99 #define MIN(a,b) ((a) < (b) ? (a) : (b))
 100 
 101 #if 1   /* "old" method */
 102 #define OUT_WORDS(b,n)  outsw (IDE_DATA_REG, (b), (n))
 103 #define IN_WORDS(b,n)   insw  (IDE_DATA_REG, (b), (n))
 104 #else   /* "new" method -- should really fix each instance instead of this */
 105 #define OUT_WORDS(b,n)  output_ide_data(drive,b,(n)/2)
 106 #define IN_WORDS(b,n)   input_ide_data(drive,b,(n)/2)
 107 #endif
 108 
 109 /* special command codes for strategy routine. */
 110 #define PACKET_COMMAND 4315
 111 #define REQUEST_SENSE_COMMAND 4316
 112 
 113 #define WIN_PACKETCMD 0xa0  /* Send a packet command. */
 114 
 115 /* Some ATAPI command opcodes (just like SCSI).
 116    (Some other cdrom-specific codes are in cdrom.h.) */
 117 #define TEST_UNIT_READY         0x00
 118 #define REQUEST_SENSE           0x03
 119 #define START_STOP              0x1b
 120 #define ALLOW_MEDIUM_REMOVAL    0x1e
 121 #define READ_10                 0x28
 122 #define MODE_SENSE_10           0x5a
 123 #define MODE_SELECT_10          0x55
 124 
 125 
 126 /* ATAPI sense keys (mostly copied from scsi.h). */
 127 
 128 #define NO_SENSE                0x00
 129 #define RECOVERED_ERROR         0x01
 130 #define NOT_READY               0x02
 131 #define MEDIUM_ERROR            0x03
 132 #define HARDWARE_ERROR          0x04
 133 #define ILLEGAL_REQUEST         0x05
 134 #define UNIT_ATTENTION          0x06
 135 #define DATA_PROTECT            0x07
 136 #define ABORTED_COMMAND         0x0b
 137 #define MISCOMPARE              0x0e
 138 
 139 /* We want some additional flags for cd-rom drives.
 140    To save space in the ide_drive_t struct, use one of the fields which
 141    doesn't make sense for cd-roms -- `bios_sect'. */
 142 
 143 struct ide_cd_flags {
 144   unsigned drq_interrupt : 1; /* Device sends an interrupt when ready
 145                                  for a packet command. */
 146   unsigned no_playaudio12: 1; /* The PLAYAUDIO12 command is not supported. */
 147 
 148   unsigned media_changed : 1; /* Driver has noticed a media change. */
 149   unsigned toc_valid     : 1; /* Saved TOC information is current. */
 150   unsigned no_lba_toc    : 1; /* Drive cannot return TOC info in LBA format. */
 151   unsigned msf_as_bcd    : 1; /* Drive uses BCD in PLAYAUDIO_MSF. */
 152   unsigned reserved : 2;
 153 };
 154 
 155 #define CDROM_FLAGS(drive) ((struct ide_cd_flags *)&((drive)->bios_sect))
 156 
 157 #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
 158 
 159 
 160 /****************************************************************************
 161  * Descriptions of ATAPI error codes.
 162  */
 163 
 164 #define ARY_LEN(a) ((sizeof(a) / sizeof(a[0])))
 165 
 166 #if VERBOSE_IDE_CD_ERRORS
 167 
 168 /* From Table 124 of the ATAPI 1.2 spec. */
 169 
 170 char *sense_key_texts[16] = {
 171   "No sense data",
 172   "Recovered error",
 173   "Not ready",
 174   "Medium error",
 175   "Hardware error",
 176   "Illegal request",
 177   "Unit attention",
 178   "Data protect",
 179   "(reserved)",
 180   "(reserved)",
 181   "(reserved)",
 182   "Aborted command",
 183   "(reserved)",
 184   "(reserved)",
 185   "Miscompare",
 186   "(reserved)",
 187 };
 188 
 189 
 190 /* From Table 125 of the ATAPI 1.2 spec. */
 191 
 192 struct {
 193   short asc_ascq;
 194   char *text;
 195 } sense_data_texts[] = {
 196   { 0x0000, "No additional sense information" },
 197   { 0x0011, "Audio play operation in progress" },
 198   { 0x0012, "Audio play operation paused" },
 199   { 0x0013, "Audio play operation successfully completed" },
 200   { 0x0014, "Audio play operation stopped due to error" },
 201   { 0x0015, "No current audio status to return" },
 202 
 203   { 0x0200, "No seek complete" },
 204 
 205   { 0x0400, "Logical unit not ready - cause not reportable" },
 206   { 0x0401, "Logical unit not ready - in progress (sic) of becoming ready" },
 207   { 0x0402, "Logical unit not ready - initializing command required" },
 208   { 0x0403, "Logical unit not ready - manual intervention required" },
 209 
 210   { 0x0600, "No reference position found" },
 211 
 212   { 0x0900, "Track following error" },
 213   { 0x0901, "Tracking servo failure" },
 214   { 0x0902, "Focus servo failure" },
 215   { 0x0903, "Spindle servo failure" },
 216 
 217   { 0x1100, "Unrecovered read error" },
 218   { 0x1106, "CIRC unrecovered error" },
 219 
 220   { 0x1500, "Random positioning error" },
 221   { 0x1501, "Mechanical positioning error" },
 222   { 0x1502, "Positioning error detected by read of medium" },
 223 
 224   { 0x1700, "Recovered data with no error correction applied" },
 225   { 0x1701, "Recovered data with retries" },
 226   { 0x1702, "Recovered data with positive head offset" },
 227   { 0x1703, "Recovered data with negative head offset" },
 228   { 0x1704, "Recovered data with retries and/or CIRC applied" },
 229   { 0x1705, "Recovered data using previous sector ID" },
 230 
 231   { 0x1800, "Recovered data with error correction applied" },
 232   { 0x1801, "Recovered data with error correction and retries applied" },
 233   { 0x1802, "Recovered data - the data was auto-reallocated" },
 234   { 0x1803, "Recovered data with CIRC" },
 235   { 0x1804, "Recovered data with L-EC" },
 236   { 0x1805, "Recovered data - recommend reassignment" },
 237   { 0x1806, "Recovered data - recommend rewrite" },
 238 
 239   { 0x1a00, "Parameter list length error" },
 240 
 241   { 0x2000, "Invalid command operation code" },
 242 
 243   { 0x2100, "Logical block address out of range" },
 244 
 245   { 0x2400, "Invalid field in command packet" },
 246 
 247   { 0x2600, "Invalid field in parameter list" },
 248   { 0x2601, "Parameter not supported" },
 249   { 0x2602, "Parameter value invalid" },
 250   { 0x2603, "Threshold parameters not supported" },
 251 
 252   { 0x2800, "Not ready to ready transition, medium may have changed" },
 253 
 254   { 0x2900, "Power on, reset or bus device reset occurred" },
 255 
 256   { 0x2a00, "Parameters changed" },
 257   { 0x2a01, "Mode parameters changed" },
 258 
 259   { 0x3000, "Incompatible medium installed" },
 260   { 0x3001, "Cannot read medium - unknown format" },
 261   { 0x3002, "Cannot read medium - incompatible format" },
 262 
 263   { 0x3700, "Rounded parameter" },
 264 
 265   { 0x3900, "Saving parameters not supported" },
 266 
 267   { 0x3a00, "Medium not present" },
 268 
 269   { 0x3f00, "ATAPI CD-ROM drive operating conditions have changed" },
 270   { 0x3f01, "Microcode has been changed" },
 271   { 0x3f02, "Changed operating definition" },
 272   { 0x3f03, "Inquiry data has changed" },
 273 
 274   { 0x4000, "Diagnostic failure on component (ASCQ)" },
 275 
 276   { 0x4400, "Internal ATAPI CD-ROM drive failure" },
 277 
 278   { 0x4e00, "Overlapped commands attempted" },
 279 
 280   { 0x5300, "Media load or eject failed" },
 281   { 0x5302, "Medium removal prevented" },
 282 
 283   { 0x5700, "Unable to recover table of contents" },
 284 
 285   { 0x5a00, "Operator request or state change input (unspecified)" },
 286   { 0x5a01, "Operator medium removal request" },
 287 
 288   { 0x5b00, "Threshold condition met" },
 289 
 290   { 0x5c00, "Status change" },
 291 
 292   { 0x6300, "End of user area encountered on this track" },
 293 
 294   { 0x6400, "Illegal mode for this track" },
 295 
 296   { 0xbf00, "Loss of streaming" },
 297 };
 298 #endif
 299 
 300 
 301 
 302 /****************************************************************************
 303  * Generic packet command support routines.
 304  */
 305 
 306 
 307 static
 308 void cdrom_analyze_sense_data (ide_drive_t *drive, 
     /* [previous][next][first][last][top][bottom][index][help] */
 309                                struct atapi_request_sense *reqbuf,
 310                                struct packet_command *failed_command)
 311 {
 312   /* Don't print not ready or unit attention errors for READ_SUBCHANNEL.
 313      Workman (and probably other programs) uses this command to poll
 314      the drive, and we don't want to fill the syslog with useless errors. */
 315   if (failed_command &&
 316       failed_command->c[0] == SCMD_READ_SUBCHANNEL &&
 317       (reqbuf->sense_key == 2 || reqbuf->sense_key == 6))
 318     return;
 319 
 320 #if VERBOSE_IDE_CD_ERRORS
 321   {
 322     int i;
 323     char *s;
 324     char buf[80];
 325 
 326     printk ("ATAPI device %s:\n", drive->name);
 327 
 328     printk ("  Error code: %x\n", reqbuf->error_code);
 329 
 330     if (reqbuf->sense_key >= 0 &&
 331         reqbuf->sense_key < ARY_LEN (sense_key_texts))
 332       s = sense_key_texts[reqbuf->sense_key];
 333     else
 334       s = "(bad sense key)";
 335 
 336     printk ("  Sense key: %x - %s\n", reqbuf->sense_key, s);
 337 
 338     if (reqbuf->asc == 0x40) {
 339       sprintf (buf, "Diagnostic failure on component %x", reqbuf->ascq);
 340       s = buf;
 341     }
 342 
 343     else {
 344       int lo, hi;
 345       int key = (reqbuf->asc << 8);
 346       if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
 347         key |= reqbuf->ascq;
 348 
 349       lo = 0;
 350       hi = ARY_LEN (sense_data_texts);
 351       s = NULL;
 352 
 353       while (hi > lo) {
 354         int mid = (lo + hi) / 2;
 355         if (sense_data_texts[mid].asc_ascq == key) {
 356           s = sense_data_texts[mid].text;
 357           break;
 358         }
 359         else if (sense_data_texts[mid].asc_ascq > key)
 360           hi = mid;
 361         else
 362           lo = mid+1;
 363       }
 364     }
 365 
 366     if (s == NULL) {
 367       if (reqbuf->asc > 0x80)
 368         s = "(vendor-specific error)";
 369       else
 370         s = "(reserved error code)";
 371     }
 372 
 373     printk ("  Additional sense data: %x, %x  - %s\n",
 374             reqbuf->asc, reqbuf->ascq, s);
 375 
 376     if (failed_command != NULL) {
 377       printk ("  Failed packet command: ");
 378       for (i=0; i<sizeof (failed_command->c); i++)
 379         printk ("%02x ", failed_command->c[i]);
 380       printk ("\n");
 381     }
 382   }
 383 
 384 #else
 385   printk ("%s: code: %x  key: %x  asc: %x  ascq: %x\n",
 386           drive->name,
 387           reqbuf->error_code, reqbuf->sense_key, reqbuf->asc, reqbuf->ascq);
 388 #endif
 389 }
 390 
 391 
 392 /* Fix up a possibly partially-processed request so that we can
 393    start it over entirely, or even put it back on the request queue. */
 394 static void restore_request (struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
 395 {
 396   if (rq->buffer != rq->bh->b_data)
 397     {
 398       int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
 399       rq->buffer = rq->bh->b_data;
 400       rq->nr_sectors += n;
 401       rq->sector -= n;
 402     }
 403   rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
 404 }
 405 
 406 
 407 static void cdrom_queue_request_sense (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409   struct request *rq;
 410   struct packet_command *pc;
 411   struct atapi_request_sense *reqbuf;
 412   unsigned long flags;
 413 
 414   int major = HWIF(drive)->major;
 415 
 416   save_flags (flags);
 417   cli ();  /* safety */
 418 
 419   rq = HWGROUP(drive)->rq;
 420 
 421   /* If we're processing a request, put it back on the request queue. */
 422   if (rq != NULL)
 423     {
 424       restore_request (rq);
 425       rq->next = blk_dev[major].current_request;
 426       blk_dev[major].current_request = rq;
 427       HWGROUP(drive)->rq = NULL;
 428     }
 429 
 430   restore_flags (flags);
 431 
 432   /* Make up a new request to retrieve sense information. */
 433   reqbuf = &drive->cdrom_info.sense_data;
 434 
 435   pc = &HWIF(drive)->request_sense_pc;
 436   memset (pc, 0, sizeof (*pc));
 437 
 438   pc->c[0] = REQUEST_SENSE;
 439   pc->c[4] = sizeof (*reqbuf);
 440   pc->buffer = (char *)reqbuf;
 441   pc->buflen = sizeof (*reqbuf);
 442   
 443   rq = &HWIF(drive)->request_sense_request;
 444   rq->dev = MKDEV (major, (drive->select.b.unit) << PARTN_BITS);
 445   rq->cmd = REQUEST_SENSE_COMMAND;
 446   rq->errors = 0;
 447   rq->sector = 0;
 448   rq->nr_sectors = 0;
 449   rq->current_nr_sectors = 0;
 450   rq->buffer = (char *)pc;
 451   rq->sem = NULL;
 452   rq->bh = NULL;
 453   rq->bhtail = NULL;
 454   rq->next = NULL;
 455 
 456   save_flags (flags);
 457   cli ();  /* safety */
 458 
 459   /* Stick it onto the front of the queue. */
 460   rq->next = blk_dev[major].current_request;
 461   blk_dev[major].current_request = rq;
 462 
 463   restore_flags (flags);
 464 }
 465 
 466 
 467 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 468 {
 469   struct request *rq = HWGROUP(drive)->rq;
 470 
 471   /* The code in blk.h can screw us up on error recovery if the block
 472      size is larger than 1k.  Fix that up here. */
 473   if (!uptodate && rq->bh != 0)
 474     {
 475       int adj = rq->current_nr_sectors - 1;
 476       rq->current_nr_sectors -= adj;
 477       rq->sector += adj;
 478     }
 479 
 480   if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate)
 481     {
 482       struct atapi_request_sense *reqbuf;
 483       reqbuf = &drive->cdrom_info.sense_data;
 484       cdrom_analyze_sense_data (drive, reqbuf, NULL);
 485     }
 486 
 487   ide_end_request (uptodate, HWGROUP(drive));
 488 }
 489 
 490 
 491 /* Mark that we've seen a media change, and invalidate our internal
 492    buffers. */
 493 static void cdrom_saw_media_change (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 494 {
 495   CDROM_FLAGS (drive)->media_changed = 1;
 496   CDROM_FLAGS (drive)->toc_valid = 0;
 497   drive->cdrom_info.nsectors_buffered = 0;
 498 }
 499 
 500 
 501 /* Returns 0 if the request should be continued.
 502    Returns 1 if the request was ended. */
 503 static int cdrom_decode_status (ide_drive_t *drive, int good_stat, int *stat_ret)
     /* [previous][next][first][last][top][bottom][index][help] */
 504 {
 505   struct request *rq = HWGROUP(drive)->rq;
 506   int stat, err, sense_key, cmd;
 507 
 508   /* Check for errors. */
 509   stat = GET_STAT();
 510   *stat_ret = stat;
 511 
 512   if (OK_STAT (stat, good_stat, BAD_R_STAT))
 513     return 0;
 514 
 515   /* Got an error. */
 516   err = IN_BYTE (IDE_ERROR_REG);
 517   sense_key = err >> 4;
 518 
 519   if (rq == NULL)
 520     printk ("%s : missing request in cdrom_decode_status\n", drive->name);
 521   else
 522     {
 523       cmd = rq->cmd;
 524 
 525       /* Check for tray open */
 526       if (sense_key == NOT_READY)
 527         {
 528           struct packet_command *pc;
 529           cdrom_saw_media_change (drive);
 530 
 531           /* Fail the request if this is a read command. */
 532           if (cmd == READ)
 533             {
 534               printk ("%s : tray open\n", drive->name);
 535               cdrom_end_request (0, drive);
 536             }
 537 
 538           else
 539             {
 540               /* Otherwise, it's some other packet command.
 541                  Print an error message to the syslog.
 542                  Exception: don't print anything if this is a read subchannel
 543                  command.  This is because workman constantly polls the drive
 544                  with this command, and we don't want to uselessly fill up
 545                  the syslog. */
 546               pc = (struct packet_command *)rq->buffer;
 547               if (pc->c[0] != SCMD_READ_SUBCHANNEL)
 548                 printk ("%s : tray open\n", drive->name);
 549 
 550               /* Set the error flag and complete the request. */
 551               pc->stat = 1;
 552               cdrom_end_request (1, drive);
 553             }
 554         }
 555 
 556       /* Check for media change. */
 557       else if (sense_key == UNIT_ATTENTION)
 558         {
 559           cdrom_saw_media_change (drive);
 560           printk ("%s: media changed\n", drive->name);
 561 
 562           /* Return failure for a packet command, so that
 563              cdrom_queue_packet_command can do a request sense before
 564              the command gets retried. */
 565 
 566           if (cmd == PACKET_COMMAND)
 567             {
 568               struct packet_command *pc = (struct packet_command *)rq->buffer;
 569               pc->stat = 1;
 570               cdrom_end_request (1, drive);
 571             }
 572 
 573           /* Otherwise, it's a block read.  Arrange to retry it.
 574              But be sure to give up if we've retried too many times. */
 575           else if ((++rq->errors > ERROR_MAX))
 576             {
 577               cdrom_end_request (0, drive);
 578             }
 579         }
 580 
 581       /* Don't attempt to retry if this was a packet command. */
 582       else if (cmd == PACKET_COMMAND)
 583         {
 584           struct packet_command *pc = (struct packet_command *)rq->buffer;
 585           ide_dump_status (drive, "packet command error", stat);
 586           pc->stat = 1;  /* signal error */
 587           cdrom_end_request (1, drive);
 588         }
 589 
 590       /* No point in retrying after an illegal request or data protect error.*/
 591       else if (sense_key == ILLEGAL_REQUEST || sense_key == DATA_PROTECT)
 592         {
 593           ide_dump_status (drive, "command error", stat);
 594           cdrom_end_request (0, drive);
 595         }
 596 
 597       /* If there were other errors, go to the default handler. */
 598       else if ((err & ~ABRT_ERR) != 0)
 599         {
 600           if (ide_error (drive, "cdrom_decode_status", stat))
 601             return 1;
 602         }
 603 
 604       /* Else, abort if we've racked up too many retries. */
 605       else if ((++rq->errors > ERROR_MAX))
 606         {
 607           cdrom_end_request (0, drive);
 608         }
 609 
 610       /* If we got a CHECK_STATUS condition, and this was a READ request,
 611          queue a request sense command to try to find out more about
 612          what went wrong (and clear a unit attention)?  For packet commands,
 613          this is done separately in cdrom_queue_packet_command. */
 614       if ((stat & ERR_STAT) != 0 && cmd == READ)
 615         cdrom_queue_request_sense (drive);
 616     }
 617 
 618   /* Retry, or handle the next request. */
 619   IDE_DO_REQUEST;
 620   return 1;
 621 }
 622 
 623 
 624 /* Set up the device registers for transferring a packet command on DEV,
 625    expecting to later transfer XFERLEN bytes.  This should be followed
 626    by a call to cdrom_transfer_packet_command; however, if this is a
 627    drq_interrupt device, one must wait for an interrupt first. */
 628 static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 629 {
 630   /* Wait for the controller to be idle. */
 631   if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
 632 
 633   /* Set up the controller registers. */
 634   OUT_BYTE (0, IDE_FEATURE_REG);
 635   OUT_BYTE (0, IDE_NSECTOR_REG);
 636   OUT_BYTE (0, IDE_SECTOR_REG);
 637 
 638   OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
 639   OUT_BYTE (xferlen >> 8  , IDE_HCYL_REG);
 640   OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
 641   OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
 642 
 643   return 0;
 644 }
 645 
 646 
 647 /* Send a packet command to DEV described by CMD_BUF and CMD_LEN.
 648    The device registers must have already been prepared
 649    by cdrom_start_packet_command. */
 650 static int cdrom_transfer_packet_command (ide_drive_t *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
 651                                           char *cmd_buf, int cmd_len)
 652 {
 653   if (CDROM_FLAGS (drive)->drq_interrupt)
 654     {
 655       /* Here we should have been called after receiving an interrupt
 656          from the device.  DRQ should how be set. */
 657       int stat_dum;
 658 
 659       /* Check for errors. */
 660       if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum)) return 1;
 661     }
 662   else
 663     {
 664       /* Otherwise, we must wait for DRQ to get set. */
 665       if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) return 1;
 666     }
 667 
 668   /* Send the command to the device. */
 669   OUT_WORDS (cmd_buf, cmd_len/2);
 670 
 671   return 0;
 672 }
 673 
 674 
 675 
 676 /****************************************************************************
 677  * Block read functions.
 678  */
 679 
 680 /*
 681  * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
 682  * buffer.  Once the first sector is added, any subsequent sectors are
 683  * assumed to be continuous (until the buffer is cleared).  For the first
 684  * sector added, SECTOR is its sector number.  (SECTOR is then ignored until
 685  * the buffer is cleared.)
 686  */
 687 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
     /* [previous][next][first][last][top][bottom][index][help] */
 688                                   int sectors_to_transfer)
 689 {
 690   struct cdrom_info *info = &drive->cdrom_info;
 691 
 692   /* Number of sectors to read into the buffer. */
 693   int sectors_to_buffer = MIN (sectors_to_transfer,
 694                                (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
 695                                  info->nsectors_buffered);
 696 
 697   char *dest;
 698 
 699   /* If we don't yet have a sector buffer, try to allocate one.
 700      If we can't get one atomically, it's not fatal -- we'll just throw
 701      the data away rather than caching it. */
 702   if (info->sector_buffer == NULL)
 703     {
 704       info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE, GFP_ATOMIC);
 705 
 706       /* If we couldn't get a buffer, don't try to buffer anything... */
 707       if (info->sector_buffer == NULL)
 708         sectors_to_buffer = 0;
 709     }
 710 
 711   /* If this is the first sector in the buffer, remember its number. */
 712   if (info->nsectors_buffered == 0)
 713     info->sector_buffered = sector;
 714 
 715   /* Read the data into the buffer. */
 716   dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
 717   while (sectors_to_buffer > 0)
 718     {
 719       IN_WORDS (dest, SECTOR_SIZE / 2);
 720       --sectors_to_buffer;
 721       --sectors_to_transfer;
 722       ++info->nsectors_buffered;
 723       dest += SECTOR_SIZE;
 724     }
 725 
 726   /* Throw away any remaining data. */
 727   while (sectors_to_transfer > 0)
 728     {
 729       char dum[SECTOR_SIZE];
 730       IN_WORDS (dum, sizeof (dum) / 2);
 731       --sectors_to_transfer;
 732     }
 733 }
 734 
 735 
 736 /*
 737  * Check the contents of the interrupt reason register from the cdrom
 738  * and attempt to recover if there are problems.  Returns  0 if everything's
 739  * ok; nonzero if the request has been terminated.
 740  */
 741 static inline
 742 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
     /* [previous][next][first][last][top][bottom][index][help] */
 743 {
 744   ireason &= 3;
 745   if (ireason == 2) return 0;
 746 
 747   if (ireason == 0)
 748     {
 749       /* Whoops... The drive is expecting to receive data from us! */
 750       printk ("%s: cdrom_read_intr: "
 751               "Drive wants to transfer data the wrong way!\n",
 752               drive->name);
 753 
 754       /* Throw some data at the drive so it doesn't hang
 755          and quit this request. */
 756       while (len > 0)
 757         {
 758           short dum = 0;
 759           OUT_WORDS (&dum, 1);
 760           len -= 2;
 761         }
 762     }
 763 
 764   else
 765     {
 766       /* Drive wants a command packet, or invalid ireason... */
 767       printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
 768               drive->name, ireason);
 769     }
 770 
 771   cdrom_end_request (0, drive);
 772   IDE_DO_REQUEST;
 773   return -1;
 774 }
 775 
 776 
 777 /*
 778  * Interrupt routine.  Called when a read request has completed.
 779  */
 780 static void cdrom_read_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 781 {
 782   int stat;
 783   int ireason, len, sectors_to_transfer, nskip;
 784 
 785   struct request *rq = HWGROUP(drive)->rq;
 786 
 787   /* Check for errors. */
 788   if (cdrom_decode_status (drive, 0, &stat)) return;
 789 
 790   /* Read the interrupt reason and the transfer length. */
 791   ireason = IN_BYTE (IDE_NSECTOR_REG);
 792   len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
 793 
 794   /* If DRQ is clear, the command has completed. */
 795   if ((stat & DRQ_STAT) == 0)
 796     {
 797       /* If we're not done filling the current buffer, complain.
 798          Otherwise, complete the command normally. */
 799       if (rq->current_nr_sectors > 0)
 800         {
 801           printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
 802                   drive->name, rq->current_nr_sectors);
 803           cdrom_end_request (0, drive);
 804         }
 805       else
 806         cdrom_end_request (1, drive);
 807 
 808       IDE_DO_REQUEST;
 809       return;
 810     }
 811 
 812   /* Check that the drive is expecting to do the same thing that we are. */
 813   if (cdrom_read_check_ireason (drive, len, ireason)) return;
 814 
 815   /* Assume that the drive will always provide data in multiples of at least
 816      SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise. */
 817   if ((len % SECTOR_SIZE) != 0)
 818     {
 819       printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
 820               drive->name, len);
 821       printk ("  This drive is not supported by this version of the driver\n");
 822       cdrom_end_request (0, drive);
 823       IDE_DO_REQUEST;
 824       return;
 825     }
 826 
 827   /* The number of sectors we need to read from the drive. */
 828   sectors_to_transfer = len / SECTOR_SIZE;
 829 
 830   /* First, figure out if we need to bit-bucket any of the leading sectors. */
 831   nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
 832                sectors_to_transfer);
 833 
 834   while (nskip > 0)
 835     {
 836       /* We need to throw away a sector. */
 837       char dum[SECTOR_SIZE];
 838       IN_WORDS (dum, sizeof (dum) / 2);
 839 
 840       --rq->current_nr_sectors;
 841       --nskip;
 842       --sectors_to_transfer;
 843     }
 844 
 845   /* Now loop while we still have data to read from the drive. */
 846   while (sectors_to_transfer > 0)
 847     {
 848       int this_transfer;
 849 
 850       /* If we've filled the present buffer but there's another chained
 851          buffer after it, move on. */
 852       if (rq->current_nr_sectors == 0 &&
 853           rq->nr_sectors > 0)
 854         cdrom_end_request (1, drive);
 855 
 856       /* If the buffers are full, cache the rest of the data in our
 857          internal buffer. */
 858       if (rq->current_nr_sectors == 0)
 859         {
 860           cdrom_buffer_sectors (drive, rq->sector, sectors_to_transfer);
 861           sectors_to_transfer = 0;
 862         }
 863       else
 864         {
 865           /* Transfer data to the buffers.
 866              Figure out how many sectors we can transfer
 867              to the current buffer. */
 868           this_transfer = MIN (sectors_to_transfer,
 869                                rq->current_nr_sectors);
 870 
 871           /* Read this_transfer sectors into the current buffer. */
 872           while (this_transfer > 0)
 873             {
 874               IN_WORDS (rq->buffer, SECTOR_SIZE / 2);
 875               rq->buffer += SECTOR_SIZE;
 876               --rq->nr_sectors;
 877               --rq->current_nr_sectors;
 878               ++rq->sector;
 879               --this_transfer;
 880               --sectors_to_transfer;
 881             }
 882         }
 883     }
 884 
 885   /* Done moving data!
 886      Wait for another interrupt. */
 887   ide_set_handler(drive, &cdrom_read_intr);     /* this one is okay */
 888 }
 889 
 890 
 891 /*
 892  * Try to satisfy some of the current read request from our cached data.
 893  * Returns nonzero if the request has been completed, zero otherwise.
 894  */
 895 static int cdrom_read_from_buffer (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 896 {
 897   struct cdrom_info *info = &drive->cdrom_info;
 898   struct request *rq = HWGROUP(drive)->rq;
 899 
 900   /* Can't do anything if there's no buffer. */
 901   if (info->sector_buffer == NULL) return 0;
 902 
 903   /* Loop while this request needs data and the next block is present
 904      in our cache. */
 905   while (rq->nr_sectors > 0 &&
 906          rq->sector >= info->sector_buffered &&
 907          rq->sector < info->sector_buffered + info->nsectors_buffered)
 908     {
 909       if (rq->current_nr_sectors == 0)
 910         cdrom_end_request (1, drive);
 911 
 912       memcpy (rq->buffer,
 913               info->sector_buffer +
 914                 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
 915               SECTOR_SIZE);
 916       rq->buffer += SECTOR_SIZE;
 917       --rq->current_nr_sectors;
 918       --rq->nr_sectors;
 919       ++rq->sector;
 920     }
 921 
 922   /* If we've satisfied the current request, terminate it successfully. */
 923   if (rq->nr_sectors == 0)
 924     {
 925       cdrom_end_request (1, drive);
 926       return -1;
 927     }
 928 
 929   /* Move on to the next buffer if needed. */
 930   if (rq->current_nr_sectors == 0)
 931     cdrom_end_request (1, drive);
 932 
 933   /* If this condition does not hold, then the kluge i use to
 934      represent the number of sectors to skip at the start of a transfer
 935      will fail.  I think that this will never happen, but let's be
 936      paranoid and check. */
 937   if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
 938       (rq->sector % SECTORS_PER_FRAME) != 0)
 939     {
 940       printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
 941               drive->name, rq->sector);
 942       cdrom_end_request (0, drive);
 943       return -1;
 944     }
 945 
 946   return 0;
 947 }
 948 
 949 
 950 
 951 /*
 952  * Routine to send a read packet command to the drive.
 953  * This is usually called directly from cdrom_start_read.
 954  * However, for drq_interrupt devices, it is called from an interrupt
 955  * when the drive is ready to accept the command.
 956  */
 957 static int cdrom_start_read_continuation (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 958 {
 959   struct packet_command pc;
 960   struct request *rq = HWGROUP(drive)->rq;
 961 
 962   int nsect, sector, nframes, frame, nskip;
 963 
 964   /* Number of sectors to transfer. */
 965   nsect = rq->nr_sectors;
 966 
 967   /* Starting sector. */
 968   sector = rq->sector;
 969 
 970   /* If the requested sector doesn't start on a cdrom block boundary,
 971      we must adjust the start of the transfer so that it does,
 972      and remember to skip the first few sectors.  If the CURRENT_NR_SECTORS
 973      field is larger than the size of the buffer, it will mean that
 974      we're to skip a number of sectors equal to the amount by which
 975      CURRENT_NR_SECTORS is larger than the buffer size. */
 976   nskip = (sector % SECTORS_PER_FRAME);
 977   if (nskip > 0)
 978     {
 979       /* Sanity check... */
 980       if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS))
 981         {
 982           printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
 983                   drive->name, rq->current_nr_sectors);
 984           cdrom_end_request (0, drive);
 985           IDE_DO_REQUEST;
 986           return 1;
 987         }
 988 
 989       sector -= nskip;
 990       nsect += nskip;
 991       rq->current_nr_sectors += nskip;
 992     }
 993 
 994   /* Convert from sectors to cdrom blocks, rounding up the transfer
 995      length if needed. */
 996   nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
 997   frame = sector / SECTORS_PER_FRAME;
 998 
 999   /* Largest number of frames was can transfer at once is 64k-1. */
1000   nframes = MIN (nframes, 65535);
1001 
1002   /* Set up the command */
1003   memset (&pc.c, 0, sizeof (pc.c));
1004   pc.c[0] = READ_10;
1005   pc.c[7] = (nframes >> 8);
1006   pc.c[8] = (nframes & 0xff);
1007 
1008   /* Write the sector address into the command image. */
1009   {
1010     union {
1011       struct {unsigned char b0, b1, b2, b3;} b;
1012       struct {unsigned long l0;} l;
1013     } conv;
1014     conv.l.l0 = frame;
1015     pc.c[2] = conv.b.b3;
1016     pc.c[3] = conv.b.b2;
1017     pc.c[4] = conv.b.b1;
1018     pc.c[5] = conv.b.b0;
1019   }
1020 
1021   if (cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c)))
1022     return 1;
1023 
1024   /* Set up our interrupt handler and return. */
1025   ide_set_handler(drive, &cdrom_read_intr); /* BUG: do this BEFORE triggering drive */
1026 
1027   return 0;
1028 }
1029 
1030 
1031 /*
1032  * Start a read request from the CD-ROM.
1033  * Returns 0 if the request was started successfully,
1034  *  1 if there was an error and we should either retry or move on to the
1035  *  next request.
1036  */
1037 static int cdrom_start_read (ide_drive_t *drive, unsigned int block)
     /* [previous][next][first][last][top][bottom][index][help] */
1038 {
1039   struct request *rq = HWGROUP(drive)->rq;
1040 
1041   /* We may be retrying this request after an error.
1042      Fix up any weirdness which might be present in the request packet. */
1043   restore_request (rq);
1044 
1045   /* Satisfy whatever we can of this request from our cached sector. */
1046   if (cdrom_read_from_buffer (drive))
1047     return 1;
1048 
1049   /* Clear the local sector buffer. */
1050   drive->cdrom_info.nsectors_buffered = 0;
1051 
1052   if (cdrom_start_packet_command (drive, 32768))
1053     return 1;
1054 
1055   if (CDROM_FLAGS (drive)->drq_interrupt)
1056     ide_set_handler(drive, (ide_handler_t *)&cdrom_start_read_continuation); /* BUG: do this BEFORE triggering drive */
1057   else
1058     {
1059       if (cdrom_start_read_continuation (drive))
1060         return 1;
1061     }
1062 
1063   return 0;
1064 }
1065 
1066 
1067 
1068 
1069 /****************************************************************************
1070  * Execute all other packet commands.
1071  */
1072 
1073 /* Forward declaration */
1074 static int
1075 cdrom_request_sense (ide_drive_t *drive, struct atapi_request_sense *reqbuf);
1076 
1077 
1078 /* Interrupt routine for packet command completion. */
1079 static void cdrom_pc_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1080 {
1081   int ireason, len, stat, thislen;
1082   struct request *rq = HWGROUP(drive)->rq;
1083   struct packet_command *pc = (struct packet_command *)rq->buffer;
1084 
1085   /* Check for errors. */
1086   if (cdrom_decode_status (drive, 0, &stat)) return;
1087 
1088   /* Read the interrupt reason and the transfer length. */
1089   ireason = IN_BYTE (IDE_NSECTOR_REG);
1090   len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1091 
1092   /* If DRQ is clear, the command has completed.
1093      Complain if we still have data left to transfer. */
1094   if ((stat & DRQ_STAT) == 0)
1095     {
1096       /* Some of the trailing request sense fields are optional, and
1097          some drives don't send them.  Sigh. */
1098       if (pc->c[0] == REQUEST_SENSE && pc->buflen > 0 && pc->buflen <= 5) {
1099         while (pc->buflen > 0) {
1100           *pc->buffer++ = 0;
1101           --pc->buflen;
1102         }
1103       }
1104 
1105       if (pc->buflen == 0)
1106         cdrom_end_request (1, drive);
1107       else
1108         {
1109           printk ("%s: cdrom_pc_intr: data underrun %d\n",
1110                   drive->name, pc->buflen);
1111           pc->stat = 1;
1112           cdrom_end_request (1, drive);
1113         }
1114       IDE_DO_REQUEST;
1115       return;
1116     }
1117 
1118   /* Figure out how much data to transfer. */
1119   thislen = pc->buflen;
1120   if (thislen < 0) thislen = -thislen;
1121   if (thislen > len) thislen = len;
1122 
1123   /* The drive wants to be written to. */
1124   if ((ireason & 3) == 0)
1125     {
1126       /* Check that we want to write. */
1127       if (pc->buflen > 0)
1128         {
1129           printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
1130                   drive->name);
1131           pc->stat = 1;
1132           thislen = 0;
1133         }
1134 
1135       /* Transfer the data. */
1136       OUT_WORDS (pc->buffer, thislen / 2);
1137 
1138       /* If we haven't moved enough data to satisfy the drive,
1139          add some padding. */
1140       while (len > thislen)
1141         {
1142           short dum = 0;
1143           OUT_WORDS (&dum, 1);
1144           len -= 2;
1145         }
1146 
1147       /* Keep count of how much data we've moved. */
1148       pc->buffer += thislen;
1149       pc->buflen += thislen;
1150     }
1151 
1152   /* Same drill for reading. */
1153   else if ((ireason & 3) == 2)
1154     {
1155       /* Check that we want to read. */
1156       if (pc->buflen < 0)
1157         {
1158           printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
1159                   drive->name);
1160           pc->stat = 1;
1161           thislen = 0;
1162         }
1163 
1164       /* Transfer the data. */
1165       IN_WORDS (pc->buffer, thislen / 2);
1166 
1167       /* If we haven't moved enough data to satisfy the drive,
1168          add some padding. */
1169       while (len > thislen)
1170         {
1171           short dum = 0;
1172           IN_WORDS (&dum, 1);
1173           len -= 2;
1174         }
1175 
1176       /* Keep count of how much data we've moved. */
1177       pc->buffer += thislen;
1178       pc->buflen -= thislen;
1179     }
1180 
1181   else
1182     {
1183       printk ("%s: cdrom_pc_intr: The drive appears confused (ireason = 0x%2x)\n",
1184               drive->name, ireason);
1185       pc->stat = 1;
1186     }
1187 
1188   /* Now we wait for another interrupt. */
1189   ide_set_handler(drive, &cdrom_pc_intr);       /* this one is okay */
1190 }
1191 
1192 
1193 static int cdrom_do_pc_continuation (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1194 {
1195   struct request *rq = HWGROUP(drive)->rq;
1196   struct packet_command *pc = (struct packet_command *)rq->buffer;
1197 
1198   if (cdrom_transfer_packet_command (drive, pc->c, sizeof (pc->c)))
1199     return 1;
1200 
1201   /* Set up our interrupt handler and return. */
1202   ide_set_handler(drive, &cdrom_pc_intr); /* BUG: do this BEFORE triggering drive */
1203 
1204   return 0;
1205 }
1206 
1207 
1208 static int cdrom_do_packet_command (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1209 {
1210   int len;
1211   struct request *rq = HWGROUP(drive)->rq;
1212   struct packet_command *pc = (struct packet_command *)rq->buffer;
1213 
1214   len = pc->buflen;
1215   if (len < 0) len = -len;
1216 
1217   pc->stat = 0;
1218 
1219   if (cdrom_start_packet_command (drive, len))
1220     return 1;
1221 
1222   if (CDROM_FLAGS (drive)->drq_interrupt)
1223     ide_set_handler(drive, (ide_handler_t *)&cdrom_do_pc_continuation); /* BUG: do this BEFORE triggering drive */
1224   else
1225     {
1226       if (cdrom_do_pc_continuation (drive))
1227         return 1;
1228     }
1229 
1230   return 0;
1231 }
1232 
1233 
1234 static
1235 int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
1236 {
1237   int retries = 3;
1238   unsigned long flags;
1239   struct request req, **p, **pfirst;
1240   struct semaphore sem = MUTEX_LOCKED;
1241   int major = HWIF(drive)->major;
1242 
1243  retry:
1244   req.dev = MKDEV (major, (drive->select.b.unit) << PARTN_BITS);
1245   req.cmd = PACKET_COMMAND;
1246   req.errors = 0;
1247   req.sector = 0;
1248   req.nr_sectors = 0;
1249   req.current_nr_sectors = 0;
1250   req.buffer = (char *)pc;
1251   req.sem = &sem;
1252   req.bh = NULL;
1253   req.bhtail = NULL;
1254   req.next = NULL;
1255 
1256   save_flags (flags);
1257   cli ();
1258 
1259   p = &blk_dev[major].current_request;
1260   pfirst = p;
1261   while ((*p) != NULL)
1262     {
1263       p = &((*p)->next);
1264     }
1265   *p = &req;
1266   if (p == pfirst)
1267     blk_dev[major].request_fn ();
1268 
1269   restore_flags (flags);
1270 
1271   down (&sem);
1272 
1273   if (pc->stat != 0)
1274     {
1275       /* The request failed.  Try to do a request sense to get more information
1276          about the error; store the result in the cdrom_info struct
1277          for this drive.  Check to be sure that it wasn't a request sense
1278          request that failed, though, to prevent infinite loops. */
1279       
1280       struct atapi_request_sense *reqbuf = &drive->cdrom_info.sense_data;
1281 
1282       if (pc->c[0] == REQUEST_SENSE || cdrom_request_sense (drive, reqbuf))
1283         {
1284           memset (reqbuf, 0, sizeof (*reqbuf));
1285           reqbuf->asc = 0xff;
1286         }
1287       cdrom_analyze_sense_data (drive, reqbuf, pc);
1288 
1289       /* If the error was a unit attention (usually means media was changed),
1290          retry the command. */
1291       if (reqbuf->sense_key == UNIT_ATTENTION && retries > 0)
1292         {
1293           --retries;
1294           goto retry;
1295         }
1296 
1297       return -EIO;
1298     }
1299   else
1300     return 0;
1301 }
1302 
1303 
1304 
1305 /****************************************************************************
1306  * cdrom driver request routine.
1307  */
1308 
1309 void ide_do_rw_cdrom (ide_drive_t *drive, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
1310 {
1311   struct request *rq = HWGROUP(drive)->rq;
1312 
1313   if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1314     cdrom_do_packet_command (drive);
1315   else if (rq -> cmd != READ)
1316     {
1317       printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1318       cdrom_end_request (0, drive);
1319     }
1320   else
1321     cdrom_start_read (drive, block);
1322 }
1323 
1324 
1325 
1326 /****************************************************************************
1327  * ioctl handling.
1328  */
1329 
1330 static inline
1331 void byte_swap_word (unsigned short *x)
     /* [previous][next][first][last][top][bottom][index][help] */
1332 {
1333   char *c = (char *)x;
1334   char d = c[0];
1335   c[0] = c[1];
1336   c[1] = d;
1337 }
1338 
1339 
1340 static inline
1341 void byte_swap_long (unsigned *x)
     /* [previous][next][first][last][top][bottom][index][help] */
1342 {
1343   char *c = (char *)x;
1344   char d = c[0];
1345   c[0] = c[3];
1346   c[3] = d;
1347   d = c[1];
1348   c[1] = c[2];
1349   c[2] = d;
1350 }
1351 
1352 
1353 static
1354 int bin2bcd (int x)
     /* [previous][next][first][last][top][bottom][index][help] */
1355 {
1356   return (x%10) | ((x/10) << 4);
1357 }
1358 
1359 
1360 static inline
1361 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
     /* [previous][next][first][last][top][bottom][index][help] */
1362 {
1363   lba += CD_BLOCK_OFFSET;
1364   lba &= 0xffffff;  /* negative lbas use only 24 bits */
1365   *m = lba / (CD_SECS * CD_FRAMES);
1366   lba %= (CD_SECS * CD_FRAMES);
1367   *s = lba / CD_FRAMES;
1368   *f = lba % CD_FRAMES;
1369 }
1370 
1371 
1372 static inline
1373 int msf_to_lba (byte m, byte s, byte f)
     /* [previous][next][first][last][top][bottom][index][help] */
1374 {
1375   return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
1376 }
1377 
1378 
1379 static void
1380 cdrom_check_status (ide_drive_t  *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1381 {
1382   struct packet_command pc;
1383 
1384   memset (&pc, 0, sizeof (pc));
1385 
1386   pc.c[0] = TEST_UNIT_READY;
1387 
1388   (void) cdrom_queue_packet_command (drive, &pc);
1389 }
1390 
1391 
1392 static int
1393 cdrom_request_sense (ide_drive_t *drive, struct atapi_request_sense *reqbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
1394 {
1395   struct packet_command pc;
1396 
1397   memset (&pc, 0, sizeof (pc));
1398 
1399   pc.c[0] = REQUEST_SENSE;
1400   pc.c[4] = sizeof (*reqbuf);
1401   pc.buffer = (char *)reqbuf;
1402   pc.buflen = sizeof (*reqbuf);
1403 
1404   return cdrom_queue_packet_command (drive, &pc);
1405 }
1406 
1407 
1408 #if 0
1409 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1410 static int
1411 cdrom_lockdoor (ide_drive_t *drive, int lockflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1412 {
1413   struct packet_command pc;
1414 
1415   memset (&pc, 0, sizeof (pc));
1416 
1417   pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1418   pc.c[4] = (lockflag != 0);
1419   return cdrom_queue_packet_command (drive, &pc);
1420 }
1421 #endif
1422 
1423 
1424 /* Eject the disk if EJECTFLAG is 0.
1425    If EJECTFLAG is 1, try to reload the disk. */
1426 static int
1427 cdrom_eject (ide_drive_t *drive, int ejectflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1428 {
1429   struct packet_command pc;
1430 
1431   memset (&pc, 0, sizeof (pc));
1432 
1433   pc.c[0] = START_STOP;
1434   pc.c[4] = 2 + (ejectflag != 0);
1435   return cdrom_queue_packet_command (drive, &pc);
1436 }
1437 
1438 
1439 static int
1440 cdrom_pause (ide_drive_t *drive, int pauseflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1441 {
1442   struct packet_command pc;
1443 
1444   memset (&pc, 0, sizeof (pc));
1445 
1446   pc.c[0] = SCMD_PAUSE_RESUME;
1447   pc.c[8] = !pauseflag;
1448   return cdrom_queue_packet_command (drive, &pc);
1449 }
1450 
1451 
1452 static int
1453 cdrom_startstop (ide_drive_t *drive, int startflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1454 {
1455   struct packet_command pc;
1456 
1457   memset (&pc, 0, sizeof (pc));
1458 
1459   pc.c[0] = START_STOP;
1460   pc.c[1] = 1;
1461   pc.c[4] = startflag;
1462   return cdrom_queue_packet_command (drive, &pc);
1463 }
1464 
1465 
1466 static int
1467 cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
     /* [previous][next][first][last][top][bottom][index][help] */
1468                      char *buf, int buflen)
1469 {
1470   struct packet_command pc;
1471 
1472   memset (&pc, 0, sizeof (pc));
1473 
1474   pc.buffer =  buf;
1475   pc.buflen = buflen;
1476   pc.c[0] = SCMD_READ_TOC;
1477   pc.c[6] = trackno;
1478   pc.c[7] = (buflen >> 8);
1479   pc.c[8] = (buflen & 0xff);
1480   if (msf_flag) pc.c[1] = 2;
1481   return cdrom_queue_packet_command (drive, &pc);
1482 }
1483 
1484 
1485 /* Try to read the entire TOC for the disk into our internal buffer. */
1486 static int
1487 cdrom_read_toc (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1488 {
1489   int msf_flag;
1490   int stat, ntracks, i;
1491   struct atapi_toc *toc = drive->cdrom_info.toc;
1492 
1493   if (toc == NULL)
1494     {
1495       /* Try to allocate space. */
1496       toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1497                                           GFP_KERNEL);
1498       drive->cdrom_info.toc = toc;
1499     }
1500 
1501   if (toc == NULL)
1502     {
1503       printk ("%s: No cdrom TOC buffer!\n", drive->name);
1504       return -EIO;
1505     }
1506 
1507   /* Check to see if the existing data is still valid.
1508      If it is, just return. */
1509   if (CDROM_FLAGS (drive)->toc_valid)
1510     cdrom_check_status (drive);
1511 
1512   if (CDROM_FLAGS (drive)->toc_valid) return 0;
1513 
1514   /* Some drives can't return TOC data in LBA format. */
1515   msf_flag = (CDROM_FLAGS (drive)->no_lba_toc);
1516 
1517   /* First read just the header, so we know how long the TOC is. */
1518   stat = cdrom_read_tocentry (drive, 0, msf_flag, (char *)toc,
1519                               sizeof (struct atapi_toc_header) +
1520                               sizeof (struct atapi_toc_entry));
1521   if (stat) return stat;
1522 
1523   ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1524   if (ntracks <= 0) return -EIO;
1525   if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1526 
1527   /* Now read the whole schmeer. */
1528   stat = cdrom_read_tocentry (drive, 0, msf_flag, (char *)toc,
1529                               sizeof (struct atapi_toc_header) +
1530                               (ntracks+1) * sizeof (struct atapi_toc_entry));
1531   if (stat) return stat;
1532   byte_swap_word (&toc->hdr.toc_length);
1533   for (i=0; i<=ntracks; i++)
1534     {
1535       if (msf_flag)
1536         {
1537           byte *adr = (byte *)&(toc->ent[i].lba);
1538           toc->ent[i].lba = msf_to_lba (adr[1], adr[2], adr[3]);
1539         }
1540       else
1541         byte_swap_long (&toc->ent[i].lba);
1542     }
1543 
1544   /* Remember that we've read this stuff. */
1545   CDROM_FLAGS (drive)->toc_valid = 1;
1546 
1547   return 0;
1548 }
1549 
1550 
1551 static int
1552 cdrom_read_subchannel (ide_drive_t *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
1553                        char *buf, int buflen)
1554 {
1555   struct packet_command pc;
1556 
1557   memset (&pc, 0, sizeof (pc));
1558 
1559   pc.buffer =  buf;
1560   pc.buflen = buflen;
1561   pc.c[0] = SCMD_READ_SUBCHANNEL;
1562   pc.c[2] = 0x40;  /* request subQ data */
1563   pc.c[3] = 0x01;  /* Format 1: current position */
1564   pc.c[7] = (buflen >> 8);
1565   pc.c[8] = (buflen & 0xff);
1566   return cdrom_queue_packet_command (drive, &pc);
1567 }
1568 
1569 
1570 /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1571 static int
1572 cdrom_mode_sense (ide_drive_t *drive, int pageno, int modeflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1573                   char *buf, int buflen)
1574 {
1575   struct packet_command pc;
1576 
1577   memset (&pc, 0, sizeof (pc));
1578 
1579   pc.buffer =  buf;
1580   pc.buflen = buflen;
1581   pc.c[0] = MODE_SENSE_10;
1582   pc.c[2] = pageno | (modeflag << 6);
1583   pc.c[7] = (buflen >> 8);
1584   pc.c[8] = (buflen & 0xff);
1585   return cdrom_queue_packet_command (drive, &pc);
1586 }
1587 
1588 
1589 static int
1590 cdrom_mode_select (ide_drive_t *drive, int pageno, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help] */
1591 {
1592   struct packet_command pc;
1593 
1594   memset (&pc, 0, sizeof (pc));
1595 
1596   pc.buffer =  buf;
1597   pc.buflen = - buflen;
1598   pc.c[0] = MODE_SELECT_10;
1599   pc.c[1] = 0x10;
1600   pc.c[2] = pageno;
1601   pc.c[7] = (buflen >> 8);
1602   pc.c[8] = (buflen & 0xff);
1603   return cdrom_queue_packet_command (drive, &pc);
1604 }
1605 
1606 
1607 static int
1608 cdrom_play_lba_range_play12 (ide_drive_t *drive, int lba_start, int lba_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1609 {
1610   struct packet_command pc;
1611 
1612   memset (&pc, 0, sizeof (pc));
1613 
1614   pc.c[0] = SCMD_PLAYAUDIO12;
1615   *(int *)(&pc.c[2]) = lba_start;
1616   *(int *)(&pc.c[6]) = lba_end - lba_start;
1617   byte_swap_long ((int *)(&pc.c[2]));
1618   byte_swap_long ((int *)(&pc.c[6]));
1619 
1620   return cdrom_queue_packet_command (drive, &pc);
1621 }
1622 
1623 
1624 static int
1625 cdrom_play_lba_range_msf (ide_drive_t *drive, int lba_start, int lba_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1626 {
1627   struct packet_command pc;
1628 
1629   memset (&pc, 0, sizeof (pc));
1630 
1631   pc.c[0] = SCMD_PLAYAUDIO_MSF;
1632   lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1633   lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1634 
1635   if (CDROM_FLAGS (drive)->msf_as_bcd)
1636     {
1637       pc.c[3] = bin2bcd (pc.c[3]);
1638       pc.c[4] = bin2bcd (pc.c[4]);
1639       pc.c[5] = bin2bcd (pc.c[5]);
1640       pc.c[6] = bin2bcd (pc.c[6]);
1641       pc.c[7] = bin2bcd (pc.c[7]);
1642       pc.c[8] = bin2bcd (pc.c[8]);
1643     }
1644 
1645   return cdrom_queue_packet_command (drive, &pc);
1646 }
1647 
1648 
1649 /* Play audio starting at LBA LBA_START and finishing with the
1650    LBA before LBA_END. */
1651 static int
1652 cdrom_play_lba_range (ide_drive_t *drive, int lba_start, int lba_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1653 {
1654   /* This is rather annoying.
1655      My NEC-260 won't recognize group 5 commands such as PLAYAUDIO12;
1656      the only way to get it to play more than 64k of blocks at once
1657      seems to be the PLAYAUDIO_MSF command.  However, the parameters
1658      the NEC 260 wants for the PLAYMSF command are incompatible with
1659      the new version of the spec.
1660 
1661      So what i'll try is this.  First try for PLAYAUDIO12.  If it works,
1662      great.  Otherwise, if the drive reports an illegal command code,
1663      try PLAYAUDIO_MSF using the NEC 260-style bcd parameters. */
1664 
1665   if (CDROM_FLAGS (drive)->no_playaudio12)
1666     return cdrom_play_lba_range_msf (drive, lba_start, lba_end);
1667   else
1668     {
1669       int stat;
1670       struct atapi_request_sense *reqbuf;
1671 
1672       stat = cdrom_play_lba_range_play12 (drive, lba_start, lba_end);
1673       if (stat == 0) return 0;
1674 
1675       /* It failed.  Try to find out why. */
1676       reqbuf = &drive->cdrom_info.sense_data;
1677       if (reqbuf->sense_key == 0x05 && reqbuf->asc == 0x20)
1678         {
1679           /* The drive didn't recognize the command.
1680              Retry with the MSF variant. */
1681           printk ("%s: Drive does not support PLAYAUDIO12; "
1682                   "trying PLAYAUDIO_MSF\n", drive->name);
1683           CDROM_FLAGS (drive)->no_playaudio12 = 1;
1684           CDROM_FLAGS (drive)->msf_as_bcd = 1;
1685           return cdrom_play_lba_range_msf (drive, lba_start, lba_end);
1686         }
1687 
1688       /* Failed for some other reason.  Give up. */
1689       return stat;
1690     }
1691 }
1692 
1693 
1694 static
1695 int cdrom_get_toc_entry (ide_drive_t *drive, int track,
     /* [previous][next][first][last][top][bottom][index][help] */
1696                          struct atapi_toc_entry **ent)
1697 {
1698   int stat, ntracks;
1699   struct atapi_toc *toc;
1700 
1701   /* Make sure our saved TOC is valid. */
1702   stat = cdrom_read_toc (drive);
1703   if (stat) return stat;
1704 
1705   toc = drive->cdrom_info.toc;
1706 
1707   /* Check validity of requested track number. */
1708   ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1709   if (track == CDROM_LEADOUT)
1710     *ent = &toc->ent[ntracks];
1711   else if (track < toc->hdr.first_track ||
1712            track > toc->hdr.last_track)
1713     return -EINVAL;
1714   else
1715     *ent = &toc->ent[track - toc->hdr.first_track];
1716 
1717   return 0;
1718 }
1719 
1720 
1721 int ide_cdrom_ioctl (ide_drive_t *drive, struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1722                         struct file *file, unsigned int cmd, unsigned long arg)
1723 {
1724   switch (cmd)
1725     {
1726     case CDROMEJECT:
1727       return cdrom_eject (drive, 0);
1728 
1729     case CDROMPAUSE:
1730       return cdrom_pause (drive, 1);
1731 
1732     case CDROMRESUME:
1733       return cdrom_pause (drive, 0);
1734 
1735     case CDROMSTART:
1736       return cdrom_startstop (drive, 1);
1737 
1738     case CDROMSTOP:
1739       return cdrom_startstop (drive, 0);
1740 
1741     case CDROMPLAYMSF:
1742       {
1743         struct cdrom_msf msf;
1744         int stat, lba_start, lba_end;
1745 
1746         stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
1747         if (stat) return stat;
1748 
1749         memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
1750 
1751         lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
1752                                 msf.cdmsf_frame0);
1753         lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
1754                               msf.cdmsf_frame1) + 1;
1755 
1756         if (lba_end <= lba_start) return -EINVAL;
1757 
1758         return cdrom_play_lba_range (drive, lba_start, lba_end);
1759       }
1760 
1761     /* Like just about every other Linux cdrom driver, we ignore the
1762        index part of the request here. */
1763     case CDROMPLAYTRKIND:
1764       {
1765         int stat, lba_start, lba_end;
1766         struct cdrom_ti ti;
1767         struct atapi_toc_entry *first_toc, *last_toc;
1768 
1769         stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
1770         if (stat) return stat;
1771 
1772         memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
1773 
1774         stat = cdrom_get_toc_entry (drive, ti.cdti_trk0, &first_toc);
1775         if (stat) return stat;
1776         stat = cdrom_get_toc_entry (drive, ti.cdti_trk1, &last_toc);
1777         if (stat) return stat;
1778 
1779         if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
1780         lba_start = first_toc->lba;
1781         lba_end   = last_toc->lba;
1782 
1783         if (lba_end <= lba_start) return -EINVAL;
1784 
1785         return cdrom_play_lba_range (drive, lba_start, lba_end);
1786       }
1787 
1788     case CDROMREADTOCHDR:
1789       {
1790         int stat;
1791         struct cdrom_tochdr tochdr;
1792         struct atapi_toc *toc;
1793 
1794         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tochdr));
1795         if (stat) return stat;
1796 
1797         /* Make sure our saved TOC is valid. */
1798         stat = cdrom_read_toc (drive);
1799         if (stat) return stat;
1800 
1801         toc = drive->cdrom_info.toc;
1802         tochdr.cdth_trk0 = toc->hdr.first_track;
1803         tochdr.cdth_trk1 = toc->hdr.last_track;
1804 
1805         memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
1806 
1807         return stat;
1808       }
1809 
1810     case CDROMREADTOCENTRY:
1811       {
1812         int stat;
1813         struct cdrom_tocentry tocentry;
1814         struct atapi_toc_entry *toce;
1815 
1816         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (tocentry));
1817         if (stat) return stat;
1818         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tocentry));
1819         if (stat) return stat;
1820 
1821         memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
1822 
1823         stat = cdrom_get_toc_entry (drive, tocentry.cdte_track, &toce);
1824         if (stat) return stat;
1825 
1826         tocentry.cdte_ctrl = toce->control;
1827         tocentry.cdte_adr  = toce->adr;
1828 
1829         if (tocentry.cdte_format == CDROM_MSF)
1830           {
1831             /* convert to MSF */
1832             lba_to_msf (toce->lba,
1833                         &tocentry.cdte_addr.msf.minute,
1834                         &tocentry.cdte_addr.msf.second,
1835                         &tocentry.cdte_addr.msf.frame);
1836           }
1837         else
1838           tocentry.cdte_addr.lba = toce->lba;
1839 
1840         memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
1841 
1842         return stat;
1843       }
1844 
1845     case CDROMSUBCHNL:
1846       {
1847         char buffer[16];
1848         int stat, abs_lba, rel_lba;
1849         struct cdrom_subchnl subchnl;
1850 
1851         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (subchnl));
1852         if (stat) return stat;
1853         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (subchnl));
1854         if (stat) return stat;
1855 
1856         memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
1857 
1858         stat = cdrom_read_subchannel (drive, buffer, sizeof (buffer));
1859         if (stat) return stat;
1860 
1861         abs_lba = *(int *)&buffer[8];
1862         rel_lba = *(int *)&buffer[12];
1863         byte_swap_long (&abs_lba);
1864         byte_swap_long (&rel_lba);
1865 
1866         if (subchnl.cdsc_format == CDROM_MSF)
1867           {
1868             lba_to_msf (abs_lba,
1869                         &subchnl.cdsc_absaddr.msf.minute,
1870                         &subchnl.cdsc_absaddr.msf.second,
1871                         &subchnl.cdsc_absaddr.msf.frame);
1872             lba_to_msf (rel_lba,
1873                         &subchnl.cdsc_reladdr.msf.minute,
1874                         &subchnl.cdsc_reladdr.msf.second,
1875                         &subchnl.cdsc_reladdr.msf.frame);
1876           }
1877         else
1878           {
1879             subchnl.cdsc_absaddr.lba = abs_lba;
1880             subchnl.cdsc_reladdr.lba = rel_lba;
1881           }
1882 
1883         subchnl.cdsc_audiostatus = buffer[1];
1884         subchnl.cdsc_ctrl = buffer[5] & 0xf;
1885         subchnl.cdsc_trk = buffer[6];
1886         subchnl.cdsc_ind = buffer[7];
1887 
1888         memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
1889 
1890         return stat;
1891       }
1892 
1893     case CDROMVOLCTRL:
1894       {
1895         struct cdrom_volctrl volctrl;
1896         char buffer[24], mask[24];
1897         int stat;
1898 
1899         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (volctrl));
1900         if (stat) return stat;
1901         memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
1902 
1903         stat = cdrom_mode_sense (drive, 0x0e, 0, buffer, sizeof (buffer));
1904         if (stat) return stat;
1905         stat = cdrom_mode_sense (drive, 0x0e, 1, mask  , sizeof (buffer));
1906         if (stat) return stat;
1907 
1908         buffer[1] = buffer[2] = 0;
1909 
1910         buffer[17] = volctrl.channel0 & mask[17];
1911         buffer[19] = volctrl.channel1 & mask[19];
1912         buffer[21] = volctrl.channel2 & mask[21];
1913         buffer[23] = volctrl.channel3 & mask[23];
1914 
1915         return cdrom_mode_select (drive, 0x0e, buffer, sizeof (buffer));
1916       }
1917 
1918 #ifdef TEST
1919     case 0x1234:
1920       {
1921         int stat;
1922         struct packet_command pc;
1923 
1924         memset (&pc, 0, sizeof (pc));
1925 
1926         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
1927         if (stat) return stat;
1928         memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
1929 
1930         return cdrom_queue_packet_command (drive, &pc);
1931       }
1932 
1933     case 0x1235:
1934       {
1935         int stat;
1936         struct atapi_request_sense reqbuf;
1937 
1938         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (reqbuf));
1939         if (stat) return stat;
1940 
1941         stat = cdrom_request_sense (drive, &reqbuf);
1942 
1943         memcpy_tofs ((void *) arg, &reqbuf, sizeof (reqbuf));
1944 
1945         return stat;
1946       }
1947 #endif
1948 
1949     default:
1950       return -EPERM;
1951     }
1952 
1953 }
1954 
1955 
1956 
1957 /****************************************************************************
1958  * Other driver requests (open, close, check media change).
1959  */
1960 
1961 int ide_cdrom_check_media_change (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1962 {
1963   int retval;
1964 
1965   cdrom_check_status (drive);
1966 
1967   retval = CDROM_FLAGS (drive)->media_changed;
1968   CDROM_FLAGS (drive)->media_changed = 0;
1969 
1970   return retval;
1971 }
1972 
1973 
1974 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1975 {
1976   /* no write access */
1977   if (fp->f_mode & 2) return -EROFS;
1978 
1979 #if 0 /* With this, one cannot eject a disk with workman */
1980   /* If this is the first open, lock the door. */
1981   if (drive->usage == 1)
1982     (void) cdrom_lockdoor (drive, 1);
1983 #endif
1984 
1985   /* Should check that there's a disk in the drive? */
1986   return 0;
1987 }
1988 
1989 
1990 /*
1991  * Close down the device.  Invalidate all cached blocks.
1992  */
1993 
1994 void ide_cdrom_release (struct inode *inode, struct file *file, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1995 {
1996   if (drive->usage == 0)
1997     {
1998       invalidate_buffers (inode->i_rdev);
1999 
2000 #if 0
2001       /* Unlock the door. */
2002       (void) cdrom_lockdoor (drive, 0);
2003 #endif
2004     }
2005 }
2006 
2007 
2008 
2009 /****************************************************************************
2010  * Device initialization.
2011  */
2012 
2013 void ide_cdrom_setup (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2014 {
2015   blksize_size[HWIF(drive)->major][drive->select.b.unit << PARTN_BITS] = CD_FRAMESIZE;
2016 
2017   drive->special.all = 0;
2018   drive->ready_stat = 0;
2019 
2020   CDROM_FLAGS (drive)->media_changed = 0;
2021   CDROM_FLAGS (drive)->toc_valid     = 0;
2022 
2023   CDROM_FLAGS (drive)->no_playaudio12 = 0;
2024   CDROM_FLAGS (drive)->no_lba_toc = 0;
2025   CDROM_FLAGS (drive)->msf_as_bcd = 0;
2026   CDROM_FLAGS (drive)->drq_interrupt = ((drive->id->config & 0x0060) == 0x20);
2027 
2028   /* Accommodate some broken drives... */
2029   if (strcmp (drive->id->model, "CD220E") == 0)  /* Creative Labs */
2030     CDROM_FLAGS (drive)->no_lba_toc = 1;
2031 
2032   else if (strcmp (drive->id->model, "TO-ICSLYAL") == 0 ||  /* Acer CD525E */
2033            strcmp (drive->id->model, "OTI-SCYLLA") == 0)
2034     CDROM_FLAGS (drive)->no_lba_toc = 1;
2035 
2036   else if (strcmp (drive->id->model, "CDA26803I SE") == 0) /* Aztech */
2037     {
2038       CDROM_FLAGS (drive)->no_lba_toc = 1;
2039 
2040       /* This drive _also_ does not implement PLAYAUDIO12 correctly. */
2041       CDROM_FLAGS (drive)->no_playaudio12 = 1;
2042     }
2043 
2044   drive->cdrom_info.toc               = NULL;
2045   drive->cdrom_info.sector_buffer     = NULL;
2046   drive->cdrom_info.sector_buffered   = 0;
2047   drive->cdrom_info.nsectors_buffered = 0;
2048 }
2049 
2050 
2051 #undef MIN
2052 #undef SECTOR_SIZE
2053 #undef SECTOR_BITS
2054 
2055 
2056 /*
2057  * TODO:
2058  *  Read actual disk capacity.
2059  *  Multisession support.
2060  *  Direct reading of audio data.
2061  *  Eject-on-dismount.
2062  *  Lock door while there's a mounted volume.
2063  *  Establish interfaces for an IDE port driver, and break out the cdrom
2064  *   code into a loadable module.
2065  */
2066 

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