root/drivers/block/ide-cd.c

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

DEFINITIONS

This source file includes following definitions.
  1. cdrom_end_request
  2. cdrom_saw_media_change
  3. cdrom_decode_status
  4. cdrom_start_packet_command
  5. cdrom_transfer_packet_command
  6. cdrom_buffer_sectors
  7. cdrom_read_check_ireason
  8. cdrom_read_intr
  9. cdrom_read_from_buffer
  10. cdrom_start_read_continuation
  11. cdrom_start_read
  12. cdrom_pc_intr
  13. cdrom_do_pc_continuation
  14. cdrom_do_packet_command
  15. cdrom_queue_packet_command
  16. do_rw_cdrom
  17. byte_swap_word
  18. byte_swap_long
  19. bin2bcd
  20. lba_to_msf
  21. msf_to_lba
  22. cdrom_check_status
  23. cdrom_request_sense
  24. cdrom_lockdoor
  25. cdrom_eject
  26. cdrom_pause
  27. cdrom_startstop
  28. cdrom_read_tocentry
  29. cdrom_read_toc
  30. cdrom_read_subchannel
  31. cdrom_mode_sense
  32. cdrom_mode_select
  33. cdrom_play_lba_range_play12
  34. cdrom_play_lba_range_msf
  35. cdrom_play_lba_range
  36. cdrom_get_toc_entry
  37. ide_cdrom_ioctl
  38. cdrom_check_media_change
  39. cdrom_open
  40. cdrom_release
  41. 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  *                       
  28  *
  29  * ATAPI cd-rom driver.  To be used with ide.c.
  30  *
  31  * Copyright (C) 1994, 1995  scott snyder  <snyder@fnald0.fnal.gov>
  32  * May be copied or modified under the terms of the GNU General Public License
  33  * (../../COPYING).
  34  */
  35 
  36 #include <linux/cdrom.h>
  37 
  38 #define SECTOR_SIZE 512
  39 #define SECTOR_BITS 9
  40 #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
  41 
  42 #define MIN(a,b) ((a) < (b) ? (a) : (b))
  43 
  44 #define OUT_WORDS(b,n)  outsw (IDE_PORT (HD_DATA, dev->hwif), (b), (n))
  45 #define IN_WORDS(b,n)   insw  (IDE_PORT (HD_DATA, dev->hwif), (b), (n))
  46 
  47 /* special command codes for strategy routine. */
  48 #define PACKET_COMMAND 4315
  49 
  50 #define WIN_PACKETCMD 0xa0  /* Send a packet command. */
  51 
  52 /* Some ATAPI command opcodes (just like SCSI).
  53    (Some other cdrom-specific codes are in cdrom.h.) */
  54 #define TEST_UNIT_READY         0x00
  55 #define REQUEST_SENSE           0x03
  56 #define START_STOP              0x1b
  57 #define ALLOW_MEDIUM_REMOVAL    0x1e
  58 #define READ_10                 0x28
  59 #define MODE_SENSE_10           0x5a
  60 #define MODE_SELECT_10          0x55
  61 
  62 struct packet_command {
  63   char *buffer;
  64   int buflen;
  65   int stat;
  66   unsigned char c[12];
  67 };
  68 
  69 
  70 struct atapi_request_sense {
  71   unsigned char error_code : 7;
  72   unsigned char valid      : 1;
  73   byte reserved1;
  74   unsigned char sense_key  : 4;
  75   unsigned char reserved2  : 1;
  76   unsigned char ili        : 1;
  77   unsigned char reserved3  : 2;
  78   byte info[4];
  79   byte sense_len;
  80   byte command_info[4];
  81   byte asc;
  82   byte ascq;
  83   byte fru;
  84   byte sense_key_specific[3];
  85 };
  86 
  87 /* We want some additional flags for cd-rom drives.
  88    To save space in the ide_dev_t struct, use one of the fields which
  89    doesn't make sense for cd-roms -- `bios_sect'. */
  90 
  91 struct ide_cd_flags {
  92   unsigned drq_interrupt : 1; /* Device sends an interrupt when ready
  93                                  for a packet command. */
  94   unsigned no_playaudio12: 1; /* The PLAYAUDIO12 command is not supported. */
  95 
  96   unsigned media_changed : 1; /* Driver has noticed a media change. */
  97   unsigned toc_valid     : 1; /* Saved TOC information is current. */
  98   unsigned no_lba_toc    : 1; /* Drive cannot return TOC info in LBA format */
  99   unsigned reserved : 3;
 100 };
 101 
 102 #define CDROM_FLAGS(dev) ((struct ide_cd_flags *)&((dev)->bios_sect))
 103 
 104 
 105 /* Space to hold the disk TOC. */
 106 
 107 #define MAX_TRACKS 99
 108 struct atapi_toc_header {
 109   unsigned short toc_length;
 110   byte first_track;
 111   byte last_track;
 112 };
 113 
 114 struct atapi_toc_entry {
 115   byte reserved1;
 116   unsigned control : 4;
 117   unsigned adr     : 4;
 118   byte track;
 119   byte reserved2;
 120   unsigned lba;
 121 };
 122 
 123 struct atapi_toc {
 124   struct atapi_toc_header hdr;
 125   struct atapi_toc_entry  ent[MAX_TRACKS+1];  /* One extra for the leadout. */
 126 };
 127 
 128 
 129 #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
 130 
 131 /* Extra per-device info for cdrom drives. */
 132 struct cdrom_info {
 133 
 134   /* Buffer for table of contents.  NULL if we haven't allocated
 135      a TOC buffer for this device yet. */
 136 
 137   struct atapi_toc *toc;
 138 
 139   /* Sector buffer.  If a read request wants only the first part of a cdrom
 140      block, we cache the rest of the block here, in the expectation that that
 141      data is going to be wanted soon.  SECTOR_BUFFERED is the number of the
 142      first buffered sector, and NSECTORS_BUFFERED is the number of sectors
 143      in the buffer.  Before the buffer is allocated, we should have
 144      SECTOR_BUFFER == NULL and NSECTORS_BUFFERED == 0. */
 145 
 146   unsigned long sector_buffered;
 147   unsigned long nsectors_buffered;
 148   char *sector_buffer;
 149 };
 150 
 151 
 152 static struct cdrom_info cdrom_info[2][MAX_DRIVES];
 153 
 154 
 155 
 156 /****************************************************************************
 157  * Generic packet command support routines.
 158  */
 159 
 160 
 161 static void cdrom_end_request (int uptodate, ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163   struct request *rq = ide_cur_rq[dev->hwif];
 164 
 165   /* The code in blk.h can screw us up on error recovery if the block
 166      size is larger than 1k.  Fix that up here. */
 167   if (!uptodate && rq->bh != 0)
 168     {
 169       int adj = rq->current_nr_sectors - 1;
 170       rq->current_nr_sectors -= adj;
 171       rq->sector += adj;
 172     }
 173 
 174   end_request (uptodate, dev->hwif);
 175 }
 176 
 177 
 178 /* Mark that we've seen a media change, and invalidate our internal
 179    buffers. */
 180 static void cdrom_saw_media_change (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182   CDROM_FLAGS (dev)->media_changed = 1;
 183   CDROM_FLAGS (dev)->toc_valid = 0;
 184   cdrom_info[dev->hwif][dev->select.b.drive].nsectors_buffered = 0;
 185 }
 186 
 187 
 188 /* Returns 0 if the request should be continued.
 189    Returns 1 if the request was ended. */
 190 static int cdrom_decode_status (ide_dev_t *dev, int good_stat, int *stat_ret)
     /* [previous][next][first][last][top][bottom][index][help] */
 191 {
 192   struct request *rq = ide_cur_rq[dev->hwif];
 193   int stat, err;
 194 
 195   /* Check for errors. */
 196   stat = GET_STAT (dev->hwif);
 197   *stat_ret = stat;
 198 
 199   if (OK_STAT (stat, good_stat, BAD_RW_STAT))
 200     return 0;
 201 
 202   /* Got an error. */
 203   err = IN_BYTE (HD_ERROR, dev->hwif);
 204 
 205   /* Check for tray open */
 206   if ((err & 0xf0) == 0x20)
 207     {
 208       struct packet_command *pc;
 209       cdrom_saw_media_change (dev);
 210 
 211       /* Fail the request if this is a read command. */
 212       if (rq->cmd == READ)
 213         {
 214           printk ("%s : tray open\n", dev->name);
 215           cdrom_end_request (0, dev);
 216         }
 217 
 218       else
 219         {
 220           /* Otherwise, it's some other packet command.
 221              Print an error message to the syslog.
 222              Exception: don't print anything if this is a read subchannel
 223              command.  This is because workman constantly polls the drive
 224              with this command, and we don't want to uselessly fill up
 225              the syslog. */
 226           pc = (struct packet_command *)rq->buffer;
 227           if (pc->c[0] != SCMD_READ_SUBCHANNEL)
 228             printk ("%s : tray open\n", dev->name);
 229 
 230           /* Set the error flag and complete the request. */
 231           pc->stat = 1;
 232           cdrom_end_request (1, dev);
 233         }
 234     }
 235 
 236   /* Check for media change. */
 237   else if ((err & 0xf0) == 0x60)
 238     {
 239       cdrom_saw_media_change (dev);
 240       printk ("%s: media changed\n", dev->name);
 241 
 242       /* We're going to retry this command.
 243          But be sure to give up if we've retried too many times. */
 244       if ((++rq->errors > ERROR_MAX))
 245         {
 246           cdrom_end_request (0, dev);
 247         }
 248     }
 249 
 250   /* Don't attempt to retry if this was a packet command. */
 251   else if (rq->cmd == PACKET_COMMAND)
 252     {
 253       struct packet_command *pc = (struct packet_command *)rq->buffer;
 254       dump_status (dev->hwif, "packet command error", stat);
 255       pc->stat = 1;  /* signal error */
 256       cdrom_end_request (1, dev);
 257     }
 258 
 259   /* If there were other errors, go to the default handler. */
 260   else if ((err & ~ABRT_ERR) != 0)
 261     {
 262       ide_error (dev, "cdrom_decode_status", stat);
 263     }
 264 
 265   /* Else, abort if we've racked up too many retries. */
 266   else if ((++rq->errors > ERROR_MAX))
 267     {
 268       cdrom_end_request (0, dev);
 269     }
 270 
 271   /* Retry, or handle the next request. */
 272   DO_REQUEST;
 273   return 1;
 274 }
 275 
 276 
 277 /* Set up the device registers for transferring a packet command on DEV,
 278    expecting to later transfer XFERLEN bytes.  This should be followed
 279    by a call to cdrom_transfer_packet_command; however, if this is a
 280    drq_interrupt device, one must wait for an interrupt first. */
 281 static int cdrom_start_packet_command (ide_dev_t *dev, int xferlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283   /* Wait for the controller to be idle. */
 284   if (wait_stat (dev, 0, BUSY_STAT, WAIT_READY)) return 1;
 285 
 286   /* Set up the controller registers. */
 287   OUT_BYTE (0, HD_FEATURE);
 288   OUT_BYTE (0, HD_NSECTOR);
 289   OUT_BYTE (0, HD_SECTOR);
 290 
 291   OUT_BYTE (xferlen & 0xff, HD_LCYL);
 292   OUT_BYTE (xferlen >> 8  , HD_HCYL);
 293   OUT_BYTE (dev->ctl, HD_CMD);
 294   OUT_BYTE (WIN_PACKETCMD, HD_COMMAND); /* packet command */
 295 
 296   return 0;
 297 }
 298 
 299 
 300 /* Send a packet command to DEV described by CMD_BUF and CMD_LEN.
 301    The device registers must have already been prepared
 302    by cdrom_start_packet_command. */
 303 static int cdrom_transfer_packet_command (ide_dev_t *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 304                                           char *cmd_buf, int cmd_len)
 305 {
 306   if (CDROM_FLAGS (dev)->drq_interrupt)
 307     {
 308       /* Here we should have been called after receiving an interrupt
 309          from the device.  DRQ should how be set. */
 310       int stat_dum;
 311 
 312       /* Check for errors. */
 313       if (cdrom_decode_status (dev, DRQ_STAT, &stat_dum)) return 1;
 314     }
 315   else
 316     {
 317       /* Otherwise, we must wait for DRQ to get set. */
 318       if (wait_stat (dev, DRQ_STAT, BAD_STAT, WAIT_READY)) return 1;
 319     }
 320 
 321   /* Send the command to the device. */
 322   OUT_WORDS (cmd_buf, cmd_len/2);
 323 
 324   return 0;
 325 }
 326 
 327 
 328 
 329 /****************************************************************************
 330  * Block read functions.
 331  */
 332 
 333 /*
 334  * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
 335  * buffer.  SECTOR is the number of the first sector to be buffered.
 336  */
 337 static void cdrom_buffer_sectors (ide_dev_t *dev, unsigned long sector,
     /* [previous][next][first][last][top][bottom][index][help] */
 338                                   int sectors_to_transfer)
 339 {
 340   struct cdrom_info *info = &cdrom_info[dev->hwif][dev->select.b.drive];
 341 
 342   /* Number of sectors to read into the buffer. */
 343   int sectors_to_buffer = MIN (sectors_to_transfer,
 344                                (SECTOR_BUFFER_SIZE >> SECTOR_BITS));
 345 
 346   char *dest;
 347 
 348   /* If we don't yet have a sector buffer, try to allocate one.
 349      If we can't get one atomically, it's not fatal -- we'll just throw
 350      the data away rather than caching it. */
 351   if (info->sector_buffer == NULL)
 352     {
 353       info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE, GFP_ATOMIC);
 354 
 355       /* If we couldn't get a buffer, don't try to buffer anything... */
 356       if (info->sector_buffer == NULL)
 357         sectors_to_buffer = 0;
 358     }
 359 
 360   /* Remember the sector number and the number of sectors we're storing. */
 361   info->sector_buffered = sector;
 362   info->nsectors_buffered = sectors_to_buffer;
 363 
 364   /* Read the data into the buffer. */
 365   dest = info->sector_buffer;
 366   while (sectors_to_buffer > 0)
 367     {
 368       IN_WORDS (dest, SECTOR_SIZE / 2);
 369       --sectors_to_buffer;
 370       --sectors_to_transfer;
 371       dest += SECTOR_SIZE;
 372     }
 373 
 374   /* Throw away any remaining data. */
 375   while (sectors_to_transfer > 0)
 376     {
 377       char dum[SECTOR_SIZE];
 378       IN_WORDS (dest, sizeof (dum) / 2);
 379       --sectors_to_transfer;
 380     }
 381 }
 382 
 383 
 384 /*
 385  * Check the contents of the interrupt reason register from the cdrom
 386  * and attempt to recover if there are problems.  Returns  0 if everything's
 387  * ok; nonzero if the request has been terminated.
 388  */
 389 static inline
 390 int cdrom_read_check_ireason (ide_dev_t *dev, int len, int ireason)
     /* [previous][next][first][last][top][bottom][index][help] */
 391 {
 392   ireason &= 3;
 393   if (ireason == 2) return 0;
 394 
 395   if (ireason == 0)
 396     {
 397       /* Whoops... The drive is expecting to receive data from us! */
 398       printk ("%s: cdrom_read_intr: "
 399               "Drive wants to transfer data the wrong way!\n",
 400               dev->name);
 401 
 402       /* Throw some data at the drive so it doesn't hang
 403          and quit this request. */
 404       while (len > 0)
 405         {
 406           short dum = 0;
 407           OUT_WORDS (&dum, 1);
 408           len -= 2;
 409         }
 410     }
 411 
 412   else
 413     {
 414       /* Drive wants a command packet, or invalid ireason... */
 415       printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
 416               dev->name, ireason);
 417     }
 418 
 419   cdrom_end_request (0, dev);
 420   DO_REQUEST;
 421   return -1;
 422 }
 423 
 424 
 425 /*
 426  * Interrupt routine.  Called when a read request has completed.
 427  */
 428 static void cdrom_read_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430   int stat;
 431   int ireason, len, sectors_to_transfer, nskip;
 432 
 433   struct request *rq = ide_cur_rq[dev->hwif];
 434 
 435   /* Check for errors. */
 436   if (cdrom_decode_status (dev, 0, &stat)) return;
 437 
 438   /* Read the interrupt reason and the transfer length. */
 439   ireason = IN_BYTE (HD_NSECTOR, dev->hwif);
 440   len = IN_BYTE (HD_LCYL, dev->hwif) + 256 * IN_BYTE (HD_HCYL, dev->hwif);
 441 
 442   /* If DRQ is clear, the command has completed. */
 443   if ((stat & DRQ_STAT) == 0)
 444     {
 445       /* If we're not done filling the current buffer, complain.
 446          Otherwise, complete the command normally. */
 447       if (rq->current_nr_sectors > 0)
 448         {
 449           printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
 450                   dev->name, rq->current_nr_sectors);
 451           cdrom_end_request (0, dev);
 452         }
 453       else
 454         cdrom_end_request (1, dev);
 455 
 456       DO_REQUEST;
 457       return;
 458     }
 459 
 460   /* Check that the drive is expecting to do the same thing that we are. */
 461   if (cdrom_read_check_ireason (dev, len, ireason)) return;
 462 
 463   /* Assume that the drive will always provide data in multiples of at least
 464      SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise. */
 465   if ((len % SECTOR_SIZE) != 0)
 466     {
 467       printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
 468               dev->name, len);
 469       printk ("  This drive is not supported by this version of the driver\n");
 470       cdrom_end_request (0, dev);
 471       DO_REQUEST;
 472       return;
 473     }
 474 
 475   /* The number of sectors we need to read from the drive. */
 476   sectors_to_transfer = len / SECTOR_SIZE;
 477 
 478   /* First, figure out if we need to bit-bucket any of the leading sectors. */
 479   nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
 480                sectors_to_transfer);
 481 
 482   while (nskip > 0)
 483     {
 484       /* We need to throw away a sector. */
 485       char dum[SECTOR_SIZE];
 486       IN_WORDS (dum, sizeof (dum) / 2);
 487 
 488       --rq->current_nr_sectors;
 489       --nskip;
 490       --sectors_to_transfer;
 491     }
 492 
 493   /* Now loop while we still have data to read from the drive. */
 494   while (sectors_to_transfer > 0)
 495     {
 496       int this_transfer;
 497 
 498       /* If we've filled the present buffer but there's another chained
 499          buffer after it, move on. */
 500       if (rq->current_nr_sectors == 0 &&
 501           rq->nr_sectors > 0)
 502         cdrom_end_request (1, dev);
 503 
 504       /* If the buffers are full, cache the rest of the data in our
 505          internal buffer. */
 506       if (rq->current_nr_sectors == 0)
 507         {
 508           cdrom_buffer_sectors (dev, rq->sector, sectors_to_transfer);
 509           sectors_to_transfer = 0;
 510         }
 511       else
 512         {
 513           /* Transfer data to the buffers.
 514              Figure out how many sectors we can transfer
 515              to the current buffer. */
 516           this_transfer = MIN (sectors_to_transfer,
 517                                rq->current_nr_sectors);
 518 
 519           /* Read this_transfer sectors into the current buffer. */
 520           while (this_transfer > 0)
 521             {
 522               IN_WORDS (rq->buffer, SECTOR_SIZE / 2);
 523               rq->buffer += SECTOR_SIZE;
 524               --rq->nr_sectors;
 525               --rq->current_nr_sectors;
 526               ++rq->sector;
 527               --this_transfer;
 528               --sectors_to_transfer;
 529             }
 530         }
 531     }
 532 
 533   /* Done moving data!
 534      Wait for another interrupt. */
 535   ide_handler[dev->hwif] = cdrom_read_intr;
 536 }
 537 
 538 
 539 /*
 540  * Try to satisfy some of the current read request from our cached data.
 541  * Returns nonzero if the request has been completed, zero otherwise.
 542  */
 543 static int cdrom_read_from_buffer (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 544 {
 545   struct cdrom_info *info = &cdrom_info[dev->hwif][dev->select.b.drive];
 546   struct request *rq = ide_cur_rq[dev->hwif];
 547 
 548   /* Can't do anything if there's no buffer. */
 549   if (info->sector_buffer == NULL) return 0;
 550 
 551   /* Loop while this request needs data and the next block is present
 552      in our cache. */
 553   while (rq->nr_sectors > 0 &&
 554          rq->sector >= info->sector_buffered &&
 555          rq->sector < info->sector_buffered + info->nsectors_buffered)
 556     {
 557       if (rq->current_nr_sectors == 0)
 558         cdrom_end_request (1, dev);
 559 
 560       memcpy (rq->buffer,
 561               info->sector_buffer +
 562                 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
 563               SECTOR_SIZE);
 564       rq->buffer += SECTOR_SIZE;
 565       --rq->current_nr_sectors;
 566       --rq->nr_sectors;
 567       ++rq->sector;
 568     }
 569 
 570   /* If we've satisfied the current request, terminate it successfully. */
 571   if (rq->nr_sectors == 0)
 572     {
 573       cdrom_end_request (1, dev);
 574       return -1;
 575     }
 576 
 577   /* Move on to the next buffer if needed. */
 578   if (rq->current_nr_sectors == 0)
 579     cdrom_end_request (1, dev);
 580 
 581   /* If this condition does not hold, then the kluge i use to
 582      represent the number of sectors to skip at the start of a transfer
 583      will fail.  I think that this will never happen, but let's be
 584      paranoid and check. */
 585   if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
 586       (rq->sector % SECTORS_PER_FRAME) != 0)
 587     {
 588       printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
 589               dev->name, rq->sector);
 590       cdrom_end_request (0, dev);
 591       return -1;
 592     }
 593 
 594   return 0;
 595 }
 596 
 597 
 598 
 599 /*
 600  * Routine to send a read packet command to the drive.
 601  * This is usually called directly from cdrom_start_read.
 602  * However, for drq_interrupt devices, it is called from an interrupt
 603  * when the drive is ready to accept the command.
 604  */
 605 static int cdrom_start_read_continuation (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 606 {
 607   struct packet_command pc;
 608   struct request *rq = ide_cur_rq[dev->hwif];
 609 
 610   int nsect, sector, nframes, frame, nskip;
 611 
 612   /* Number of sectors to transfer. */
 613   nsect = rq->nr_sectors;
 614 
 615   /* Starting sector. */
 616   sector = rq->sector;
 617 
 618   /* If the requested sector doesn't start on a cdrom block boundary,
 619      we must adjust the start of the transfer so that it does,
 620      and remember to skip the first few sectors.  If the CURRENT_NR_SECTORS
 621      field is larger than the size of the buffer, it will mean that
 622      we're to skip a number of sectors equal to the amount by which
 623      CURRENT_NR_SECTORS is larger than the buffer size. */
 624   nskip = (sector % SECTORS_PER_FRAME);
 625   if (nskip > 0)
 626     {
 627       /* Sanity check... */
 628       if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS))
 629         {
 630           printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
 631                   dev->name, rq->current_nr_sectors);
 632           cdrom_end_request (0, dev);
 633           DO_REQUEST;
 634           return 1;
 635         }
 636 
 637       sector -= nskip;
 638       nsect += nskip;
 639       rq->current_nr_sectors += nskip;
 640     }
 641 
 642   /* Convert from sectors to cdrom blocks, rounding up the transfer
 643      length if needed. */
 644   nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
 645   frame = sector / SECTORS_PER_FRAME;
 646 
 647   /* Largest number of frames was can transfer at once is 64k-1. */
 648   nframes = MIN (nframes, 65535);
 649 
 650   /* Set up the command */
 651   memset (&pc.c, 0, sizeof (pc.c));
 652   pc.c[0] = READ_10;
 653   pc.c[7] = (nframes >> 8);
 654   pc.c[8] = (nframes & 0xff);
 655 
 656   /* Write the sector address into the command image. */
 657   {
 658     union {
 659       struct {unsigned char b0, b1, b2, b3;} b;
 660       struct {unsigned long l0;} l;
 661     } conv;
 662     conv.l.l0 = frame;
 663     pc.c[2] = conv.b.b3;
 664     pc.c[3] = conv.b.b2;
 665     pc.c[4] = conv.b.b1;
 666     pc.c[5] = conv.b.b0;
 667   }
 668 
 669   if (cdrom_transfer_packet_command (dev, pc.c, sizeof (pc.c)))
 670     return 1;
 671 
 672   /* Set up our interrupt handler and return. */
 673   ide_handler[dev->hwif] = cdrom_read_intr;
 674 
 675   return 0;
 676 }
 677 
 678 
 679 /*
 680  * Start a read request from the CD-ROM.
 681  * Returns 0 if the request was started successfully,
 682  *  1 if there was an error and we should either retry or move on to the
 683  *  next request.
 684  */
 685 static int cdrom_start_read (ide_dev_t *dev, unsigned int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 686 {
 687   struct request *rq = ide_cur_rq[dev->hwif];
 688 
 689   /* We may be retrying this request after an error.
 690      Fix up any weirdness which might be present in the request packet. */
 691   if (rq->buffer != rq->bh->b_data)
 692     {
 693       int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
 694       rq->buffer = rq->bh->b_data;
 695       rq->nr_sectors += n;
 696       rq->current_nr_sectors += n;
 697       rq->sector -= n;
 698     }
 699 
 700   if (rq->current_nr_sectors > (rq->bh->b_size >> SECTOR_BITS))
 701     rq->current_nr_sectors = rq->bh->b_size;
 702 
 703   /* Satisfy whatever we can of this request from our cached sector. */
 704   if (cdrom_read_from_buffer (dev))
 705     return 1;
 706 
 707   if (cdrom_start_packet_command (dev, 32768))
 708     return 1;
 709 
 710   if (CDROM_FLAGS (dev)->drq_interrupt)
 711     ide_handler[dev->hwif] = (void (*)(ide_dev_t *))cdrom_start_read_continuation;
 712   else
 713     {
 714       if (cdrom_start_read_continuation (dev))
 715         return 1;
 716     }
 717 
 718   return 0;
 719 }
 720 
 721 
 722 
 723 
 724 /****************************************************************************
 725  * Execute all other packet commands.
 726  */
 727 
 728 static void cdrom_pc_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 729 {
 730   int ireason, len, stat, thislen;
 731   struct request *rq = ide_cur_rq[dev->hwif];
 732   struct packet_command *pc = (struct packet_command *)rq->buffer;
 733 
 734   /* Check for errors. */
 735   if (cdrom_decode_status (dev, 0, &stat)) return;
 736 
 737   /* Read the interrupt reason and the transfer length. */
 738   ireason = IN_BYTE (HD_NSECTOR, dev->hwif);
 739   len = IN_BYTE (HD_LCYL, dev->hwif) + 256 * IN_BYTE (HD_HCYL, dev->hwif);
 740 
 741   /* If DRQ is clear, the command has completed.
 742      Complain if we still have data left to transfer. */
 743   if ((stat & DRQ_STAT) == 0)
 744     {
 745       if (pc->buflen == 0)
 746         cdrom_end_request (1, dev);
 747       else
 748         {
 749           printk ("%s: cdrom_pc_intr: data underrun %d\n",
 750                   dev->name, pc->buflen);
 751           pc->stat = 1;
 752           cdrom_end_request (1, dev);
 753         }
 754       DO_REQUEST;
 755       return;
 756     }
 757 
 758   /* Figure out how much data to transfer. */
 759   thislen = pc->buflen;
 760   if (thislen < 0) thislen = -thislen;
 761   if (thislen > len) thislen = len;
 762 
 763   /* The drive wants to be written to. */
 764   if ((ireason & 3) == 0)
 765     {
 766       /* Check that we want to write. */
 767       if (pc->buflen > 0)
 768         {
 769           printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
 770                   dev->name);
 771           pc->stat = 1;
 772           thislen = 0;
 773         }
 774 
 775       /* Transfer the data. */
 776       OUT_WORDS (pc->buffer, thislen / 2);
 777 
 778       /* If we haven't moved enough data to satisfy the drive,
 779          add some padding. */
 780       while (len > thislen)
 781         {
 782           short dum = 0;
 783           OUT_WORDS (&dum, 1);
 784           len -= 2;
 785         }
 786 
 787       /* Keep count of how much data we've moved. */
 788       pc->buffer += thislen;
 789       pc->buflen += thislen;
 790     }
 791 
 792   /* Same drill for reading. */
 793   else if ((ireason & 3) == 2)
 794     {
 795       /* Check that we want to read. */
 796       if (pc->buflen < 0)
 797         {
 798           printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
 799                   dev->name);
 800           pc->stat = 1;
 801           thislen = 0;
 802         }
 803 
 804       /* Transfer the data. */
 805       IN_WORDS (pc->buffer, thislen / 2);
 806 
 807       /* If we haven't moved enough data to satisfy the drive,
 808          add some padding. */
 809       while (len > thislen)
 810         {
 811           short dum = 0;
 812           IN_WORDS (&dum, 1);
 813           len -= 2;
 814         }
 815 
 816       /* Keep count of how much data we've moved. */
 817       pc->buffer += thislen;
 818       pc->buflen -= thislen;
 819     }
 820 
 821   else
 822     {
 823       printk ("%s: cdrom_pc_intr: The drive appears confused (ireason = 0x%2x)\n",
 824               dev->name, ireason);
 825       pc->stat = 1;
 826     }
 827 
 828   /* Now we wait for another interrupt. */
 829   ide_handler[dev->hwif] = cdrom_pc_intr;
 830 }
 831 
 832 
 833 static int cdrom_do_pc_continuation (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 834 {
 835   struct request *rq = ide_cur_rq[dev->hwif];
 836   struct packet_command *pc = (struct packet_command *)rq->buffer;
 837 
 838   if (cdrom_transfer_packet_command (dev, pc->c, sizeof (pc->c)))
 839     return 1;
 840 
 841   /* Set up our interrupt handler and return. */
 842   ide_handler[dev->hwif] = cdrom_pc_intr;
 843 
 844   return 0;
 845 }
 846 
 847 
 848 static int cdrom_do_packet_command (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 849 {
 850   int len;
 851   struct request *rq = ide_cur_rq[dev->hwif];
 852   struct packet_command *pc = (struct packet_command *)rq->buffer;
 853 
 854   len = pc->buflen;
 855   if (len < 0) len = -len;
 856 
 857   pc->stat = 0;
 858 
 859   if (cdrom_start_packet_command (dev, len))
 860     return 1;
 861 
 862   if (CDROM_FLAGS (dev)->drq_interrupt)
 863     ide_handler[dev->hwif] = (void (*)(ide_dev_t *))cdrom_do_pc_continuation;
 864   else
 865     {
 866       if (cdrom_do_pc_continuation (dev))
 867         return 1;
 868     }
 869 
 870   return 0;
 871 }
 872 
 873 
 874 static
 875 int cdrom_queue_packet_command (ide_dev_t *dev, struct packet_command *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877   unsigned long flags;
 878   struct request req, **p, **pfirst;
 879   struct semaphore sem = MUTEX_LOCKED;
 880   int major = ide_major[dev->hwif];
 881 
 882   req.dev = MKDEV (major, (dev->select.b.drive) << PARTN_BITS);
 883   req.cmd = PACKET_COMMAND;
 884   req.errors = 0;
 885   req.sector = 0;
 886   req.nr_sectors = 0;
 887   req.current_nr_sectors = 0;
 888   req.buffer = (char *)pc;
 889   req.sem = &sem;
 890   req.bh = NULL;
 891   req.bhtail = NULL;
 892   req.next = NULL;
 893 
 894   save_flags (flags);
 895   cli ();
 896 
 897   p = &blk_dev[major].current_request;
 898   pfirst = p;
 899   while ((*p) != NULL)
 900     {
 901       p = &((*p)->next);
 902     }
 903   *p = &req;
 904   if (p == pfirst)
 905     blk_dev[major].request_fn ();
 906 
 907   restore_flags (flags);
 908 
 909   down (&sem);
 910 
 911   if (pc->stat != 0)
 912     return -EIO;
 913   else
 914     return 0;
 915 }
 916 
 917 
 918 
 919 /****************************************************************************
 920  * cdrom driver request routine.
 921  */
 922 
 923 static int do_rw_cdrom (ide_dev_t *dev, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
 924 {
 925   struct request *rq = ide_cur_rq[dev->hwif];
 926 
 927   if (rq -> cmd == PACKET_COMMAND)
 928     return cdrom_do_packet_command (dev);
 929 
 930   if (rq -> cmd != READ)
 931     {
 932       printk ("ide-cd: bad cmd %d\n", rq -> cmd);
 933       cdrom_end_request (0, dev);
 934       return 1;
 935     }
 936 
 937   return cdrom_start_read (dev, block);
 938 }
 939 
 940 
 941 
 942 /****************************************************************************
 943  * ioctl handling.
 944  */
 945 
 946 static inline
 947 void byte_swap_word (unsigned short *x)
     /* [previous][next][first][last][top][bottom][index][help] */
 948 {
 949   char *c = (char *)x;
 950   char d = c[0];
 951   c[0] = c[1];
 952   c[1] = d;
 953 }
 954 
 955 
 956 static inline
 957 void byte_swap_long (unsigned *x)
     /* [previous][next][first][last][top][bottom][index][help] */
 958 {
 959   char *c = (char *)x;
 960   char d = c[0];
 961   c[0] = c[3];
 962   c[3] = d;
 963   d = c[1];
 964   c[1] = c[2];
 965   c[2] = d;
 966 }
 967 
 968 
 969 static
 970 int bin2bcd (int x)
     /* [previous][next][first][last][top][bottom][index][help] */
 971 {
 972   return (x%10) | ((x/10) << 4);
 973 }
 974 
 975 
 976 static inline
 977 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
     /* [previous][next][first][last][top][bottom][index][help] */
 978 {
 979   lba += CD_BLOCK_OFFSET;
 980   lba &= 0xffffff;  /* negative lbas use only 24 bits */
 981   *m = lba / (CD_SECS * CD_FRAMES);
 982   lba %= (CD_SECS * CD_FRAMES);
 983   *s = lba / CD_FRAMES;
 984   *f = lba % CD_FRAMES;
 985 }
 986 
 987 
 988 static inline
 989 int msf_to_lba (byte m, byte s, byte f)
     /* [previous][next][first][last][top][bottom][index][help] */
 990 {
 991   return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
 992 }
 993 
 994 
 995 static void
 996 cdrom_check_status (ide_dev_t  *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 997 {
 998   struct packet_command pc;
 999 
1000   memset (&pc, 0, sizeof (pc));
1001 
1002   pc.c[0] = TEST_UNIT_READY;
1003 
1004   (void) cdrom_queue_packet_command (dev, &pc);
1005 }
1006 
1007 
1008 static int
1009 cdrom_request_sense (ide_dev_t *dev, struct atapi_request_sense *reqbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
1010 {
1011   struct packet_command pc;
1012 
1013   memset (&pc, 0, sizeof (pc));
1014 
1015   pc.c[0] = REQUEST_SENSE;
1016   pc.c[4] = sizeof (*reqbuf);
1017   pc.buffer = (char *)reqbuf;
1018   pc.buflen = sizeof (*reqbuf);
1019 
1020   return cdrom_queue_packet_command (dev, &pc);
1021 }
1022 
1023 
1024 #if 0
1025 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1026 static int
1027 cdrom_lockdoor (ide_dev_t *dev, int lockflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1028 {
1029   struct packet_command pc;
1030 
1031   memset (&pc, 0, sizeof (pc));
1032 
1033   pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1034   pc.c[4] = (lockflag != 0);
1035   return cdrom_queue_packet_command (dev, &pc);
1036 }
1037 #endif
1038 
1039 
1040 /* Eject the disk if EJECTFLAG is 0.
1041    If EJECTFLAG is 1, try to reload the disk. */
1042 static int
1043 cdrom_eject (ide_dev_t *dev, int ejectflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1044 {
1045   struct packet_command pc;
1046 
1047   memset (&pc, 0, sizeof (pc));
1048 
1049   pc.c[0] = START_STOP;
1050   pc.c[4] = 2 + (ejectflag != 0);
1051   return cdrom_queue_packet_command (dev, &pc);
1052 }
1053 
1054 
1055 static int
1056 cdrom_pause (ide_dev_t *dev, int pauseflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1057 {
1058   struct packet_command pc;
1059 
1060   memset (&pc, 0, sizeof (pc));
1061 
1062   pc.c[0] = SCMD_PAUSE_RESUME;
1063   pc.c[8] = !pauseflag;
1064   return cdrom_queue_packet_command (dev, &pc);
1065 }
1066 
1067 
1068 static int
1069 cdrom_startstop (ide_dev_t *dev, int startflag)
     /* [previous][next][first][last][top][bottom][index][help] */
1070 {
1071   struct packet_command pc;
1072 
1073   memset (&pc, 0, sizeof (pc));
1074 
1075   pc.c[0] = START_STOP;
1076   pc.c[1] = 1;
1077   pc.c[4] = startflag;
1078   return cdrom_queue_packet_command (dev, &pc);
1079 }
1080 
1081 
1082 static int
1083 cdrom_read_tocentry (ide_dev_t *dev, int trackno, int msf_flag,
     /* [previous][next][first][last][top][bottom][index][help] */
1084                      char *buf, int buflen)
1085 {
1086   struct packet_command pc;
1087 
1088   memset (&pc, 0, sizeof (pc));
1089 
1090   pc.buffer =  buf;
1091   pc.buflen = buflen;
1092   pc.c[0] = SCMD_READ_TOC;
1093   pc.c[6] = trackno;
1094   pc.c[7] = (buflen >> 8);
1095   pc.c[8] = (buflen & 0xff);
1096   if (msf_flag) pc.c[1] = 2;
1097   return cdrom_queue_packet_command (dev, &pc);
1098 }
1099 
1100 
1101 /* Try to read the entire TOC for the disk into our internal buffer. */
1102 static int
1103 cdrom_read_toc (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1104 {
1105   int msf_flag;
1106   int stat, ntracks, i;
1107   struct atapi_toc *toc = cdrom_info[dev->hwif][dev->select.b.drive].toc;
1108 
1109   if (toc == NULL)
1110     {
1111       /* Try to allocate space. */
1112       toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1113                                           GFP_KERNEL);
1114       cdrom_info[dev->hwif][dev->select.b.drive].toc = toc;
1115     }
1116 
1117   if (toc == NULL)
1118     {
1119       printk ("%s: No cdrom TOC buffer!\n", dev->name);
1120       return -EIO;
1121     }
1122 
1123   /* Check to see if the existing data is still valid.
1124      If it is, just return. */
1125   if (CDROM_FLAGS (dev)->toc_valid)
1126     cdrom_check_status (dev);
1127 
1128   if (CDROM_FLAGS (dev)->toc_valid) return 0;
1129 
1130   /* Some drives can't return TOC data in LBA format. */
1131   msf_flag = (CDROM_FLAGS (dev)->no_lba_toc);
1132 
1133   /* First read just the header, so we know how long the TOC is. */
1134   stat = cdrom_read_tocentry (dev, 0, msf_flag, (char *)toc,
1135                               sizeof (struct atapi_toc_header) +
1136                               sizeof (struct atapi_toc_entry));
1137   if (stat) return stat;
1138 
1139   ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1140   if (ntracks <= 0) return -EIO;
1141   if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1142 
1143   /* Now read the whole schmeer. */
1144   stat = cdrom_read_tocentry (dev, 0, msf_flag, (char *)toc,
1145                               sizeof (struct atapi_toc_header) +
1146                               (ntracks+1) * sizeof (struct atapi_toc_entry));
1147   if (stat) return stat;
1148   byte_swap_word (&toc->hdr.toc_length);
1149   for (i=0; i<=ntracks; i++)
1150     {
1151       if (msf_flag)
1152         {
1153           byte *adr = (byte *)&(toc->ent[i].lba);
1154           toc->ent[i].lba = msf_to_lba (adr[1], adr[2], adr[3]);
1155         }
1156       else
1157         byte_swap_long (&toc->ent[i].lba);
1158     }
1159 
1160   /* Remember that we've read this stuff. */
1161   CDROM_FLAGS (dev)->toc_valid = 1;
1162 
1163   return 0;
1164 }
1165 
1166 
1167 static int
1168 cdrom_read_subchannel (ide_dev_t *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
1169                        char *buf, int buflen)
1170 {
1171   struct packet_command pc;
1172 
1173   memset (&pc, 0, sizeof (pc));
1174 
1175   pc.buffer =  buf;
1176   pc.buflen = buflen;
1177   pc.c[0] = SCMD_READ_SUBCHANNEL;
1178   pc.c[2] = 0x40;  /* request subQ data */
1179   pc.c[3] = 0x01;  /* Format 1: current position */
1180   pc.c[7] = (buflen >> 8);
1181   pc.c[8] = (buflen & 0xff);
1182   return cdrom_queue_packet_command (dev, &pc);
1183 }
1184 
1185 
1186 /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1187 static int
1188 cdrom_mode_sense (ide_dev_t *dev, int pageno, int modeflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1189                   char *buf, int buflen)
1190 {
1191   struct packet_command pc;
1192 
1193   memset (&pc, 0, sizeof (pc));
1194 
1195   pc.buffer =  buf;
1196   pc.buflen = buflen;
1197   pc.c[0] = MODE_SENSE_10;
1198   pc.c[2] = pageno | (modeflag << 6);
1199   pc.c[7] = (buflen >> 8);
1200   pc.c[8] = (buflen & 0xff);
1201   return cdrom_queue_packet_command (dev, &pc);
1202 }
1203 
1204 
1205 static int
1206 cdrom_mode_select (ide_dev_t *dev, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help] */
1207 {
1208   struct packet_command pc;
1209 
1210   memset (&pc, 0, sizeof (pc));
1211 
1212   pc.buffer =  buf;
1213   pc.buflen = - buflen;
1214   pc.c[0] = MODE_SELECT_10;
1215   pc.c[1] = 0x10;
1216   pc.c[7] = (buflen >> 8);
1217   pc.c[8] = (buflen & 0xff);
1218   return cdrom_queue_packet_command (dev, &pc);
1219 }
1220 
1221 
1222 static int
1223 cdrom_play_lba_range_play12 (ide_dev_t *dev, int lba_start, int lba_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1224 {
1225   struct packet_command pc;
1226 
1227   memset (&pc, 0, sizeof (pc));
1228 
1229   pc.c[0] = SCMD_PLAYAUDIO12;
1230   *(int *)(&pc.c[2]) = lba_start;
1231   *(int *)(&pc.c[6]) = lba_end - lba_start;
1232   byte_swap_long ((int *)(&pc.c[2]));
1233   byte_swap_long ((int *)(&pc.c[6]));
1234 
1235   return cdrom_queue_packet_command (dev, &pc);
1236 }
1237 
1238 
1239 static int
1240 cdrom_play_lba_range_msf (ide_dev_t *dev, int lba_start, int lba_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1241 {
1242   struct packet_command pc;
1243 
1244   memset (&pc, 0, sizeof (pc));
1245 
1246   pc.c[0] = SCMD_PLAYAUDIO_MSF;
1247   lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1248   lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1249 
1250   pc.c[3] = bin2bcd (pc.c[3]);
1251   pc.c[4] = bin2bcd (pc.c[4]);
1252   pc.c[5] = bin2bcd (pc.c[5]);
1253   pc.c[6] = bin2bcd (pc.c[6]);
1254   pc.c[7] = bin2bcd (pc.c[7]);
1255   pc.c[8] = bin2bcd (pc.c[8]);
1256 
1257   return cdrom_queue_packet_command (dev, &pc);
1258 }
1259 
1260 
1261 /* Play audio starting at LBA LBA_START and finishing with the
1262    LBA before LBA_END. */
1263 static int
1264 cdrom_play_lba_range (ide_dev_t *dev, int lba_start, int lba_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1265 {
1266   /* This is rather annoying.
1267      My NEC-260 won't recognize group 5 commands such as PLAYAUDIO12;
1268      the only way to get it to play more than 64k of blocks at once
1269      seems to be the PLAYAUDIO_MSF command.  However, the parameters
1270      the NEC 260 wants for the PLAYMSF command are incompatible with
1271      the new version of the spec.
1272 
1273      So what i'll try is this.  First try for PLAYAUDIO12.  If it works,
1274      great.  Otherwise, if the drive reports an illegal command code,
1275      try PLAYAUDIO_MSF using the NEC 260-style bcd parameters. */
1276 
1277   if (CDROM_FLAGS (dev)->no_playaudio12)
1278     return cdrom_play_lba_range_msf (dev, lba_start, lba_end);
1279   else
1280     {
1281       int stat, stat2;
1282       struct atapi_request_sense reqbuf;
1283 
1284       stat = cdrom_play_lba_range_play12 (dev, lba_start, lba_end);
1285       if (stat == 0) return 0;
1286 
1287       /* It failed.  Try to find out why. */
1288       stat2 = cdrom_request_sense (dev, &reqbuf);
1289       if (stat2) return stat;
1290 
1291       if (reqbuf.sense_key == 0x05 && reqbuf.asc == 0x20)
1292         {
1293           /* The drive didn't recognize the command.
1294              Retry with the MSF variant. */
1295           printk ("%s: Drive does not support PLAYAUDIO12; "
1296                   "trying PLAYAUDIO_MSF\n", dev->name);
1297           CDROM_FLAGS (dev)->no_playaudio12 = 1;
1298           return cdrom_play_lba_range_msf (dev, lba_start, lba_end);
1299         }
1300 
1301       /* Failed for some other reason.  Give up. */
1302       return stat;
1303     }
1304 }
1305 
1306 
1307 static
1308 int cdrom_get_toc_entry (ide_dev_t *dev, int track,
     /* [previous][next][first][last][top][bottom][index][help] */
1309                          struct atapi_toc_entry **ent)
1310 {
1311   int stat, ntracks;
1312   struct atapi_toc *toc;
1313 
1314   /* Make sure our saved TOC is valid. */
1315   stat = cdrom_read_toc (dev);
1316   if (stat) return stat;
1317 
1318   toc = cdrom_info[dev->hwif][dev->select.b.drive].toc;
1319 
1320   /* Check validity of requested track number. */
1321   ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1322   if (track == CDROM_LEADOUT)
1323     *ent = &toc->ent[ntracks];
1324   else if (track < toc->hdr.first_track ||
1325            track > toc->hdr.last_track)
1326     return -EINVAL;
1327   else
1328     *ent = &toc->ent[track - toc->hdr.first_track];
1329 
1330   return 0;
1331 }
1332 
1333 
1334 static int ide_cdrom_ioctl (ide_dev_t *dev, struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1335                         struct file *file, unsigned int cmd, unsigned long arg)
1336 {
1337   switch (cmd)
1338     {
1339     case CDROMEJECT:
1340       return cdrom_eject (dev, 0);
1341 
1342     case CDROMPAUSE:
1343       return cdrom_pause (dev, 1);
1344 
1345     case CDROMRESUME:
1346       return cdrom_pause (dev, 0);
1347 
1348     case CDROMSTART:
1349       return cdrom_startstop (dev, 1);
1350 
1351     case CDROMSTOP:
1352       return cdrom_startstop (dev, 0);
1353 
1354     case CDROMPLAYMSF:
1355       {
1356         struct cdrom_msf msf;
1357         int stat, lba_start, lba_end;
1358 
1359         stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
1360         if (stat) return stat;
1361 
1362         memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
1363 
1364         lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
1365                                 msf.cdmsf_frame0);
1366         lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
1367                               msf.cdmsf_frame1) + 1;
1368 
1369         if (lba_end <= lba_start) return -EINVAL;
1370 
1371         return cdrom_play_lba_range (dev, lba_start, lba_end);
1372       }
1373 
1374     /* Like just about every other Linux cdrom driver, we ignore the
1375        index part of the request here. */
1376     case CDROMPLAYTRKIND:
1377       {
1378         int stat, lba_start, lba_end;
1379         struct cdrom_ti ti;
1380         struct atapi_toc_entry *first_toc, *last_toc;
1381 
1382         stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
1383         if (stat) return stat;
1384 
1385         memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
1386 
1387         stat = cdrom_get_toc_entry (dev, ti.cdti_trk0, &first_toc);
1388         if (stat) return stat;
1389         stat = cdrom_get_toc_entry (dev, ti.cdti_trk1, &last_toc);
1390         if (stat) return stat;
1391 
1392         if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
1393         lba_start = first_toc->lba;
1394         lba_end   = last_toc->lba;
1395 
1396         if (lba_end <= lba_start) return -EINVAL;
1397 
1398         return cdrom_play_lba_range (dev, lba_start, lba_end);
1399       }
1400 
1401     case CDROMREADTOCHDR:
1402       {
1403         int stat;
1404         struct cdrom_tochdr tochdr;
1405         struct atapi_toc *toc;
1406 
1407         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tochdr));
1408         if (stat) return stat;
1409 
1410         /* Make sure our saved TOC is valid. */
1411         stat = cdrom_read_toc (dev);
1412         if (stat) return stat;
1413 
1414         toc = cdrom_info[dev->hwif][dev->select.b.drive].toc;
1415         tochdr.cdth_trk0 = toc->hdr.first_track;
1416         tochdr.cdth_trk1 = toc->hdr.last_track;
1417 
1418         memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
1419 
1420         return stat;
1421       }
1422 
1423     case CDROMREADTOCENTRY:
1424       {
1425         int stat;
1426         struct cdrom_tocentry tocentry;
1427         struct atapi_toc_entry *toce;
1428 
1429         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (tocentry));
1430         if (stat) return stat;
1431         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tocentry));
1432         if (stat) return stat;
1433 
1434         memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
1435 
1436         stat = cdrom_get_toc_entry (dev, tocentry.cdte_track, &toce);
1437         if (stat) return stat;
1438 
1439         tocentry.cdte_ctrl = toce->control;
1440         tocentry.cdte_adr  = toce->adr;
1441 
1442         if (tocentry.cdte_format == CDROM_MSF)
1443           {
1444             /* convert to MSF */
1445             lba_to_msf (toce->lba,
1446                         &tocentry.cdte_addr.msf.minute,
1447                         &tocentry.cdte_addr.msf.second,
1448                         &tocentry.cdte_addr.msf.frame);
1449           }
1450         else
1451           tocentry.cdte_addr.lba = toce->lba;
1452 
1453         memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
1454 
1455         return stat;
1456       }
1457 
1458     case CDROMSUBCHNL:
1459       {
1460         char buffer[16];
1461         int stat, abs_lba, rel_lba;
1462         struct cdrom_subchnl subchnl;
1463 
1464         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (subchnl));
1465         if (stat) return stat;
1466         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (subchnl));
1467         if (stat) return stat;
1468 
1469         memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
1470 
1471         stat = cdrom_read_subchannel (dev, buffer, sizeof (buffer));
1472         if (stat) return stat;
1473 
1474         abs_lba = *(int *)&buffer[8];
1475         rel_lba = *(int *)&buffer[12];
1476         byte_swap_long (&abs_lba);
1477         byte_swap_long (&rel_lba);
1478 
1479         if (subchnl.cdsc_format == CDROM_MSF)
1480           {
1481             lba_to_msf (abs_lba,
1482                         &subchnl.cdsc_absaddr.msf.minute,
1483                         &subchnl.cdsc_absaddr.msf.second,
1484                         &subchnl.cdsc_absaddr.msf.frame);
1485             lba_to_msf (rel_lba,
1486                         &subchnl.cdsc_reladdr.msf.minute,
1487                         &subchnl.cdsc_reladdr.msf.second,
1488                         &subchnl.cdsc_reladdr.msf.frame);
1489           }
1490         else
1491           {
1492             subchnl.cdsc_absaddr.lba = abs_lba;
1493             subchnl.cdsc_reladdr.lba = rel_lba;
1494           }
1495 
1496         subchnl.cdsc_audiostatus = buffer[1];
1497         subchnl.cdsc_ctrl = buffer[5] & 0xf;
1498         subchnl.cdsc_trk = buffer[6];
1499         subchnl.cdsc_ind = buffer[7];
1500 
1501         memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
1502 
1503         return stat;
1504       }
1505 
1506     case CDROMVOLCTRL:
1507       {
1508         struct cdrom_volctrl volctrl;
1509         char buffer[24], mask[24];
1510         int stat;
1511 
1512         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (volctrl));
1513         if (stat) return stat;
1514         memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
1515 
1516         stat = cdrom_mode_sense (dev, 0x0e, 0, buffer, sizeof (buffer));
1517         if (stat) return stat;
1518         stat = cdrom_mode_sense (dev, 0x0e, 1, mask  , sizeof (buffer));
1519         if (stat) return stat;
1520 
1521         buffer[1] = buffer[2] = 0;
1522 
1523         buffer[17] = volctrl.channel0 & mask[17];
1524         buffer[19] = volctrl.channel1 & mask[19];
1525         buffer[21] = volctrl.channel2 & mask[21];
1526         buffer[23] = volctrl.channel3 & mask[23];
1527 
1528         return cdrom_mode_select (dev, buffer, sizeof (buffer));
1529       }
1530 
1531 #ifdef TEST
1532     case 0x1234:
1533       {
1534         int stat;
1535         struct packet_command pc;
1536 
1537         memset (&pc, 0, sizeof (pc));
1538 
1539         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
1540         if (stat) return stat;
1541         memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
1542 
1543         return cdrom_queue_packet_command (dev, &pc);
1544       }
1545 
1546     case 0x1235:
1547       {
1548         int stat;
1549         struct atapi_request_sense reqbuf;
1550 
1551         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (reqbuf));
1552         if (stat) return stat;
1553 
1554         stat = cdrom_request_sense (dev, &reqbuf);
1555 
1556         memcpy_tofs ((void *) arg, &reqbuf, sizeof (reqbuf));
1557 
1558         return stat;
1559       }
1560 #endif
1561 
1562     default:
1563       return -EPERM;
1564     }
1565 
1566 }
1567 
1568 
1569 
1570 /****************************************************************************
1571  * Other driver requests (open, close, check media change).
1572  */
1573 
1574 static int cdrom_check_media_change (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1575 {
1576   int retval;
1577 
1578   cdrom_check_status (dev);
1579 
1580   retval = CDROM_FLAGS (dev)->media_changed;
1581   CDROM_FLAGS (dev)->media_changed = 0;
1582 
1583   return retval;
1584 }
1585 
1586 
1587 static int
1588 cdrom_open (struct inode *ip, struct file *fp, ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1589 {
1590   /* no write access */
1591   if (fp->f_mode & 2) return -EROFS;
1592 
1593 #if 0 /* With this, one cannot eject a disk with workman */
1594   /* If this is the first open, lock the door. */
1595   if (dev->usage == 1)
1596     (void) cdrom_lockdoor (dev, 1);
1597 #endif
1598 
1599   /* Should check that there's a disk in the drive? */
1600   return 0;
1601 }
1602 
1603 
1604 /*
1605  * Close down the device.  Invalidate all cached blocks.
1606  */
1607 
1608 static void
1609 cdrom_release (struct inode *inode, struct file *file, ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1610 {
1611   if (dev->usage == 0)
1612     {
1613       invalidate_buffers (inode->i_rdev);
1614 
1615 #if 0
1616       /* Unlock the door. */
1617       (void) cdrom_lockdoor (dev, 0);
1618 #endif
1619     }
1620 }
1621 
1622 
1623 
1624 /****************************************************************************
1625  * Device initialization.
1626  */
1627 
1628 static void cdrom_setup (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1629 {
1630   /* Just guess at capacity for now. */
1631   ide_capacity[dev->hwif][dev->select.b.drive] = 0x1fffff;
1632 
1633   ide_blksizes[dev->hwif][dev->select.b.drive << PARTN_BITS] = CD_FRAMESIZE;
1634 
1635   dev->special.all = 0;
1636 
1637   CDROM_FLAGS (dev)->media_changed = 0;
1638   CDROM_FLAGS (dev)->toc_valid     = 0;
1639 
1640   CDROM_FLAGS (dev)->no_playaudio12 = 0;
1641   CDROM_FLAGS (dev)->no_lba_toc = 0;
1642   CDROM_FLAGS (dev)->drq_interrupt = ((dev->id->config & 0x0060) == 0x20);
1643 
1644   /* Accommodate some broken drives... */
1645   if (strcmp (dev->id->model, "CD220E") == 0)  /* Creative Labs */
1646     CDROM_FLAGS (dev)->no_lba_toc = 1;
1647 
1648   else if (strcmp (dev->id->model, "TO-ICSLYAL") == 0 ||  /* Acer CD525E */
1649            strcmp (dev->id->model, "OTI-SCYLLA") == 0)
1650     CDROM_FLAGS (dev)->no_lba_toc = 1;
1651 
1652   else if (strcmp (dev->id->model, "CDA26803I SE") == 0) /* Aztech */
1653     CDROM_FLAGS (dev)->no_lba_toc = 1;
1654 
1655   cdrom_info[dev->hwif][dev->select.b.drive].toc               = NULL;
1656   cdrom_info[dev->hwif][dev->select.b.drive].sector_buffer     = NULL;
1657   cdrom_info[dev->hwif][dev->select.b.drive].sector_buffered   = 0;
1658   cdrom_info[dev->hwif][dev->select.b.drive].nsectors_buffered = 0;
1659 }
1660 
1661 
1662 #undef MIN
1663 #undef SECTOR_SIZE
1664 #undef SECTOR_BITS
1665 
1666 
1667 /*
1668  * TODO:
1669  *  Retrieve and interpret extended ATAPI error codes.
1670  *  Read actual disk capacity.
1671  *  Multisession support.
1672  *  Direct reading of audio data.
1673  *  Eject-on-dismount.
1674  *  Lock door while there's a mounted volume.
1675  */
1676 

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