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

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