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

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