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

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