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

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