root/drivers/block/ide-cd.c

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

DEFINITIONS

This source file includes following definitions.
  1. cdrom_in_bytes
  2. cdrom_out_bytes
  3. cdrom_analyze_sense_data
  4. restore_request
  5. cdrom_queue_request_sense
  6. cdrom_end_request
  7. cdrom_saw_media_change
  8. cdrom_decode_status
  9. cdrom_start_packet_command
  10. cdrom_transfer_packet_command
  11. cdrom_buffer_sectors
  12. cdrom_read_check_ireason
  13. cdrom_read_intr
  14. cdrom_read_from_buffer
  15. cdrom_start_read_continuation
  16. cdrom_start_read
  17. cdrom_pc_intr
  18. cdrom_do_pc_continuation
  19. cdrom_do_packet_command
  20. cdrom_sleep
  21. cdrom_queue_packet_command
  22. ide_do_rw_cdrom
  23. bin2bcd
  24. bcd2bin
  25. lba_to_msf
  26. msf_to_lba
  27. cdrom_check_status
  28. cdrom_lockdoor
  29. cdrom_eject
  30. cdrom_pause
  31. cdrom_startstop
  32. cdrom_read_capacity
  33. cdrom_read_tocentry
  34. cdrom_read_toc
  35. cdrom_read_subchannel
  36. cdrom_mode_sense
  37. cdrom_mode_select
  38. cdrom_play_lba_range_play12
  39. cdrom_play_lba_range_msf
  40. cdrom_play_lba_range
  41. cdrom_get_toc_entry
  42. cdrom_read_block
  43. ide_cdrom_ioctl
  44. ide_cdrom_check_media_change
  45. ide_cdrom_open
  46. ide_cdrom_release
  47. ide_cdrom_setup

   1 /*
   2  * linux/drivers/block/ide-cd.c
   3  *
   4  * 1.00  Oct 31, 1994 -- Initial version.
   5  * 1.01  Nov  2, 1994 -- Fixed problem with starting request in
   6  *                       cdrom_check_status.
   7  * 1.03  Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
   8  * (from mlord)       -- minor changes to cdrom_setup()
   9  *                    -- renamed ide_dev_s to ide_drive_t, enable irq on command
  10  * 2.00  Nov 27, 1994 -- Generalize packet command interface;
  11  *                       add audio ioctls.
  12  * 2.01  Dec  3, 1994 -- Rework packet command interface to handle devices
  13  *                       which send an interrupt when ready for a command.
  14  * 2.02  Dec 11, 1994 -- Cache the TOC in the driver.
  15  *                       Don't use SCMD_PLAYAUDIO_TI; it's not included
  16  *                       in the current version of ATAPI.
  17  *                       Try to use LBA instead of track or MSF addressing
  18  *                       when possible.
  19  *                       Don't wait for READY_STAT.
  20  * 2.03  Jan 10, 1995 -- Rewrite block read routines to handle block sizes
  21  *                       other than 2k and to move multiple sectors in a
  22  *                       single transaction.
  23  * 2.04  Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
  24  *                       Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
  25  *                       help in figuring this out.  Ditto for Acer and
  26  *                       Aztech drives, which seem to have the same problem.
  27  * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
  28  * 2.05  Jun  8, 1995 -- Don't attempt to retry after an illegal request
  29  *                        or data protect error.
  30  *                       Use HWIF and DEV_HWIF macros as in ide.c.
  31  *                       Always try to do a request_sense after
  32  *                        a failed command.
  33  *                       Include an option to give textual descriptions
  34  *                        of ATAPI errors.
  35  *                       Fix a bug in handling the sector cache which
  36  *                        showed up if the drive returned data in 512 byte
  37  *                        blocks (like Pioneer drives).  Thanks to
  38  *                        Richard Hirst <srh@gpt.co.uk> for diagnosing this.
  39  *                       Properly supply the page number field in the
  40  *                        MODE_SELECT command.
  41  *                       PLAYAUDIO12 is broken on the Aztech; work around it.
  42  * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
  43  *                       (my apologies to Scott, but now ide-cd.c is independent)
  44  * 3.00  Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
  45  *                       Implement CDROMREADAUDIO ioctl (UNTESTED).
  46  *                       Use input_ide_data() and output_ide_data().
  47  *                       Add door locking.
  48  *                       Fix usage count leak in cdrom_open, which happened
  49  *                        when a read-write mount was attempted.
  50  *                       Try to load the disk on open.
  51  *                       Implement CDROMEJECT_SW ioctl (off by default).
  52  *                       Read total cdrom capacity during open.
  53  *                       Rearrange logic in cdrom_decode_status.  Issue
  54  *                        request sense commands for failed packet commands
  55  *                        from here instead of from cdrom_queue_packet_command.
  56  *                        Fix a race condition in retrieving error information.
  57  *                       Suppress printing normal unit attention errors and
  58  *                        some drive not ready errors.
  59  *                       Implement CDROMVOLREAD ioctl.
  60  *                       Implement CDROMREADMODE1/2 ioctls.
  61  *                       Fix race condition in setting up interrupt handlers
  62  *                        when the `serialize' option is used.
  63  * 3.01  Sep  2, 1995 -- Fix ordering of reenabling interrupts in
  64  *                        cdrom_queue_request.
  65  *                       Another try at using ide_[input,output]_data.
  66  * 3.02  Sep 16, 1995 -- Stick total disk capacity in partition table as well.
  67  *                       Make VERBOSE_IDE_CD_ERRORS dump failed command again.
  68  *                       Dump out more information for ILLEGAL REQUEST errs.
  69  *                       Fix handling of errors occuring before the
  70  *                        packet command is transferred.
  71  *                       Fix transfers with odd bytelengths.
  72  * 3.03  Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
  73  *                       `DCI-2S10' drives are broken too.
  74  * 3.04  Nov 20, 1995 -- So are Vertos drives.
  75  * 3.05  Dec  1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
  76  * 3.06  Dec 16, 1995 -- Add support needed for partitions.
  77  *                       More workarounds for Vertos bugs (based on patches
  78  *                        from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
  79  *                       Try to eliminate byteorder assumptions.
  80  *                       Use atapi_cdrom_subchnl struct definition.
  81  *                       Add STANDARD_ATAPI compilation option.
  82  *
  83  * NOTE: Direct audio reads will only work on some types of drive.
  84  * So far, i've received reports of success for Sony and Toshiba drives.
  85  *
  86  * ATAPI cd-rom driver.  To be used with ide.c.
  87  *
  88  * Copyright (C) 1994, 1995  scott snyder  <snyder@fnald0.fnal.gov>
  89  * May be copied or modified under the terms of the GNU General Public License
  90  * (../../COPYING).
  91  */
  92 
  93 
  94 /***************************************************************************/
  95 
  96 #include <linux/types.h>
  97 #include <linux/kernel.h>
  98 #include <linux/delay.h>
  99 #include <linux/timer.h>
 100 #include <linux/malloc.h>
 101 #include <linux/ioport.h>
 102 #include <linux/interrupt.h>
 103 #include <linux/blkdev.h>
 104 #include <linux/errno.h>
 105 #include <linux/hdreg.h>
 106 #include <linux/cdrom.h>
 107 #include <asm/irq.h>
 108 #include <asm/io.h>
 109 #include <asm/byteorder.h>
 110 
 111 #include "ide.h"
 112 
 113 
 114 
 115 /* Turn this on to have the driver print out the meanings of the
 116    ATAPI error codes.  This will use up additional kernel-space
 117    memory, though. */
 118 
 119 #ifndef VERBOSE_IDE_CD_ERRORS
 120 #define VERBOSE_IDE_CD_ERRORS 0
 121 #endif
 122 
 123 
 124 /* Turning this on will remove code to work around various nonstandard
 125    ATAPI implementations.  If you know your drive follows the standard,
 126    this will give you a slightly smaller kernel. */
 127 
 128 #ifndef STANDARD_ATAPI
 129 #define STANDARD_ATAPI 0
 130 #endif
 131 
 132 
 133 /************************************************************************/
 134 
 135 #define SECTOR_SIZE 512
 136 #define SECTOR_BITS 9
 137 #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
 138 
 139 #define MIN(a,b) ((a) < (b) ? (a) : (b))
 140 
 141 /* special command codes for strategy routine. */
 142 #define PACKET_COMMAND        4315
 143 #define REQUEST_SENSE_COMMAND 4316
 144 #define RESET_DRIVE_COMMAND   4317
 145 
 146 /* Some ATAPI command opcodes (just like SCSI).
 147    (Some other cdrom-specific codes are in cdrom.h.) */
 148 #define TEST_UNIT_READY         0x00
 149 #define REQUEST_SENSE           0x03
 150 #define START_STOP              0x1b
 151 #define ALLOW_MEDIUM_REMOVAL    0x1e
 152 #define READ_CAPACITY           0x25
 153 #define READ_10                 0x28
 154 #define MODE_SENSE_10           0x5a
 155 #define MODE_SELECT_10          0x55
 156 #define READ_CD                 0xbe
 157 
 158 
 159 /* ATAPI sense keys (mostly copied from scsi.h). */
 160 
 161 #define NO_SENSE                0x00
 162 #define RECOVERED_ERROR         0x01
 163 #define NOT_READY               0x02
 164 #define MEDIUM_ERROR            0x03
 165 #define HARDWARE_ERROR          0x04
 166 #define ILLEGAL_REQUEST         0x05
 167 #define UNIT_ATTENTION          0x06
 168 #define DATA_PROTECT            0x07
 169 #define ABORTED_COMMAND         0x0b
 170 #define MISCOMPARE              0x0e
 171 
 172 /* We want some additional flags for cd-rom drives.
 173    To save space in the ide_drive_t struct, use some fields which
 174    doesn't make sense for cd-roms -- `bios_sect' and `bios_head'. */
 175 
 176 /* Configuration flags.  These describe the capabilities of the drive.
 177    They generally do not change after initialization, unless we learn
 178    more about the drive from stuff failing. */
 179 struct ide_cd_config_flags {
 180   unsigned drq_interrupt : 1; /* Device sends an interrupt when ready
 181                                  for a packet command. */
 182   unsigned no_doorlock   : 1; /* Drive cannot lock the door. */
 183 #if ! STANDARD_ATAPI
 184   unsigned no_playaudio12: 1; /* The PLAYAUDIO12 command is not supported. */
 185  
 186   unsigned no_lba_toc    : 1; /* Drive cannot return TOC info in LBA format. */
 187   unsigned playmsf_uses_bcd : 1; /* Drive uses BCD in PLAYAUDIO_MSF. */
 188   unsigned old_readcd    : 1; /* Drive uses old READ CD opcode. */
 189   unsigned vertos_lossage: 1; /* Drive is a Vertos 300,
 190                                  and likes to speak BCD. */
 191 #endif  /* not STANDARD_ATAPI */
 192   unsigned reserved : 1;
 193 };
 194 #define CDROM_CONFIG_FLAGS(drive) ((struct ide_cd_config_flags *)&((drive)->bios_sect))
 195 
 196  
 197 /* State flags.  These give information about the current state of the
 198    drive, and will change during normal operation. */
 199 struct ide_cd_state_flags {
 200   unsigned media_changed : 1; /* Driver has noticed a media change. */
 201   unsigned toc_valid     : 1; /* Saved TOC information is current. */
 202   unsigned door_locked   : 1; /* We think that the drive door is locked. */
 203   unsigned eject_on_close: 1; /* Drive should eject when device is closed. */
 204   unsigned reserved : 4;
 205 };
 206 #define CDROM_STATE_FLAGS(drive)  ((struct ide_cd_state_flags *)&((drive)->bios_head))
 207 
 208 
 209 #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
 210 
 211 
 212 
 213 /****************************************************************************
 214  * Routines to read and write data from/to the drive, using
 215  * the routines input_ide_data() and output_ide_data() from ide.c.
 216  *
 217  * These routines will round up any request for an odd number of bytes,
 218  * so if an odd bytecount is specified, be sure that there's at least one
 219  * extra byte allocated for the buffer.
 220  */
 221 
 222 
 223 static inline
 224 void cdrom_in_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226   ++bytecount;
 227   ide_input_data (drive, buffer, bytecount / 4);
 228   if ((bytecount & 0x03) >= 2)
 229     {
 230       insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
 231     }
 232 }
 233 
 234 
 235 static inline
 236 void cdrom_out_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
     /* [previous][next][first][last][top][bottom][index][help] */
 237 {
 238   ++bytecount;
 239   ide_output_data (drive, buffer, bytecount / 4);
 240   if ((bytecount & 0x03) >= 2)
 241     {
 242       outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
 243     }
 244 }
 245 
 246 
 247 
 248 /****************************************************************************
 249  * Descriptions of ATAPI error codes.
 250  */
 251 
 252 #define ARY_LEN(a) ((sizeof(a) / sizeof(a[0])))
 253 
 254 #if VERBOSE_IDE_CD_ERRORS
 255 
 256 /* From Table 124 of the ATAPI 1.2 spec. */
 257 
 258 char *sense_key_texts[16] = {
 259   "No sense data",
 260   "Recovered error",
 261   "Not ready",
 262   "Medium error",
 263   "Hardware error",
 264   "Illegal request",
 265   "Unit attention",
 266   "Data protect",
 267   "(reserved)",
 268   "(reserved)",
 269   "(reserved)",
 270   "Aborted command",
 271   "(reserved)",
 272   "(reserved)",
 273   "Miscompare",
 274   "(reserved)",
 275 };
 276 
 277 
 278 /* From Table 125 of the ATAPI 1.2 spec. */
 279 
 280 struct {
 281   short asc_ascq;
 282   char *text;
 283 } sense_data_texts[] = {
 284   { 0x0000, "No additional sense information" },
 285   { 0x0011, "Audio play operation in progress" },
 286   { 0x0012, "Audio play operation paused" },
 287   { 0x0013, "Audio play operation successfully completed" },
 288   { 0x0014, "Audio play operation stopped due to error" },
 289   { 0x0015, "No current audio status to return" },
 290 
 291   { 0x0200, "No seek complete" },
 292 
 293   { 0x0400, "Logical unit not ready - cause not reportable" },
 294   { 0x0401, "Logical unit not ready - in progress (sic) of becoming ready" },
 295   { 0x0402, "Logical unit not ready - initializing command required" },
 296   { 0x0403, "Logical unit not ready - manual intervention required" },
 297 
 298   { 0x0600, "No reference position found" },
 299 
 300   { 0x0900, "Track following error" },
 301   { 0x0901, "Tracking servo failure" },
 302   { 0x0902, "Focus servo failure" },
 303   { 0x0903, "Spindle servo failure" },
 304 
 305   { 0x1100, "Unrecovered read error" },
 306   { 0x1106, "CIRC unrecovered error" },
 307 
 308   { 0x1500, "Random positioning error" },
 309   { 0x1501, "Mechanical positioning error" },
 310   { 0x1502, "Positioning error detected by read of medium" },
 311 
 312   { 0x1700, "Recovered data with no error correction applied" },
 313   { 0x1701, "Recovered data with retries" },
 314   { 0x1702, "Recovered data with positive head offset" },
 315   { 0x1703, "Recovered data with negative head offset" },
 316   { 0x1704, "Recovered data with retries and/or CIRC applied" },
 317   { 0x1705, "Recovered data using previous sector ID" },
 318 
 319   { 0x1800, "Recovered data with error correction applied" },
 320   { 0x1801, "Recovered data with error correction and retries applied" },
 321   { 0x1802, "Recovered data - the data was auto-reallocated" },
 322   { 0x1803, "Recovered data with CIRC" },
 323   { 0x1804, "Recovered data with L-EC" },
 324   { 0x1805, "Recovered data - recommend reassignment" },
 325   { 0x1806, "Recovered data - recommend rewrite" },
 326 
 327   { 0x1a00, "Parameter list length error" },
 328 
 329   { 0x2000, "Invalid command operation code" },
 330 
 331   { 0x2100, "Logical block address out of range" },
 332 
 333   { 0x2400, "Invalid field in command packet" },
 334 
 335   { 0x2600, "Invalid field in parameter list" },
 336   { 0x2601, "Parameter not supported" },
 337   { 0x2602, "Parameter value invalid" },
 338   { 0x2603, "Threshold parameters not supported" },
 339 
 340   { 0x2800, "Not ready to ready transition, medium may have changed" },
 341 
 342   { 0x2900, "Power on, reset or bus device reset occurred" },
 343 
 344   { 0x2a00, "Parameters changed" },
 345   { 0x2a01, "Mode parameters changed" },
 346 
 347   { 0x3000, "Incompatible medium installed" },
 348   { 0x3001, "Cannot read medium - unknown format" },
 349   { 0x3002, "Cannot read medium - incompatible format" },
 350 
 351   { 0x3700, "Rounded parameter" },
 352 
 353   { 0x3900, "Saving parameters not supported" },
 354 
 355   { 0x3a00, "Medium not present" },
 356 
 357   { 0x3f00, "ATAPI CD-ROM drive operating conditions have changed" },
 358   { 0x3f01, "Microcode has been changed" },
 359   { 0x3f02, "Changed operating definition" },
 360   { 0x3f03, "Inquiry data has changed" },
 361 
 362   { 0x4000, "Diagnostic failure on component (ASCQ)" },
 363 
 364   { 0x4400, "Internal ATAPI CD-ROM drive failure" },
 365 
 366   { 0x4e00, "Overlapped commands attempted" },
 367 
 368   { 0x5300, "Media load or eject failed" },
 369   { 0x5302, "Medium removal prevented" },
 370 
 371   { 0x5700, "Unable to recover table of contents" },
 372 
 373   { 0x5a00, "Operator request or state change input (unspecified)" },
 374   { 0x5a01, "Operator medium removal request" },
 375 
 376   { 0x5b00, "Threshold condition met" },
 377 
 378   { 0x5c00, "Status change" },
 379 
 380   { 0x6300, "End of user area encountered on this track" },
 381 
 382   { 0x6400, "Illegal mode for this track" },
 383 
 384   { 0xbf00, "Loss of streaming" },
 385 };
 386 #endif
 387 
 388 
 389 
 390 /****************************************************************************
 391  * Generic packet command support and error handling routines.
 392  */
 393 
 394 
 395 static
 396 void cdrom_analyze_sense_data (ide_drive_t *drive, 
     /* [previous][next][first][last][top][bottom][index][help] */
 397                                struct atapi_request_sense *reqbuf,
 398                                struct packet_command *failed_command)
 399 {
 400   /* Don't print not ready or unit attention errors for READ_SUBCHANNEL.
 401      Workman (and probably other programs) uses this command to poll
 402      the drive, and we don't want to fill the syslog with useless errors. */
 403   if (failed_command &&
 404       failed_command->c[0] == SCMD_READ_SUBCHANNEL &&
 405       (reqbuf->sense_key == NOT_READY || reqbuf->sense_key == UNIT_ATTENTION))
 406     return;
 407 
 408 #if VERBOSE_IDE_CD_ERRORS
 409   {
 410     int i;
 411     char *s;
 412     char buf[80];
 413 
 414     printk ("ATAPI device %s:\n", drive->name);
 415 
 416     printk ("  Error code: 0x%02x\n", reqbuf->error_code);
 417 
 418     if (reqbuf->sense_key >= 0 &&
 419         reqbuf->sense_key < ARY_LEN (sense_key_texts))
 420       s = sense_key_texts[reqbuf->sense_key];
 421     else
 422       s = "(bad sense key)";
 423 
 424     printk ("  Sense key: 0x%02x - %s\n", reqbuf->sense_key, s);
 425 
 426     if (reqbuf->asc == 0x40) {
 427       sprintf (buf, "Diagnostic failure on component 0x%02x", reqbuf->ascq);
 428       s = buf;
 429     }
 430 
 431     else {
 432       int lo, hi;
 433       int key = (reqbuf->asc << 8);
 434       if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
 435         key |= reqbuf->ascq;
 436 
 437       lo = 0;
 438       hi = ARY_LEN (sense_data_texts);
 439       s = NULL;
 440 
 441       while (hi > lo) {
 442         int mid = (lo + hi) / 2;
 443         if (sense_data_texts[mid].asc_ascq == key) {
 444           s = sense_data_texts[mid].text;
 445           break;
 446         }
 447         else if (sense_data_texts[mid].asc_ascq > key)
 448           hi = mid;
 449         else
 450           lo = mid+1;
 451       }
 452     }
 453 
 454     if (s == NULL) {
 455       if (reqbuf->asc > 0x80)
 456         s = "(vendor-specific error)";
 457       else
 458         s = "(reserved error code)";
 459     }
 460 
 461     printk ("  Additional sense data: 0x%02x, 0x%02x  - %s\n",
 462             reqbuf->asc, reqbuf->ascq, s);
 463 
 464     if (failed_command != NULL) {
 465       printk ("  Failed packet command: ");
 466       for (i=0; i<sizeof (failed_command->c); i++)
 467         printk ("%02x ", failed_command->c[i]);
 468       printk ("\n");
 469     }
 470 
 471     if (reqbuf->sense_key == ILLEGAL_REQUEST &&
 472         (reqbuf->sense_key_specific[0] & 0x80) != 0)
 473       {
 474         printk ("  Error in %s byte %d",
 475                 (reqbuf->sense_key_specific[0] & 0x40) != 0
 476                   ? "command packet"
 477                   : "command data",
 478                 (reqbuf->sense_key_specific[1] << 8) +
 479                 reqbuf->sense_key_specific[2]);
 480 
 481         if ((reqbuf->sense_key_specific[0] & 0x40) != 0)
 482           {
 483             printk (" bit %d", reqbuf->sense_key_specific[0] & 0x07);
 484           }
 485 
 486         printk ("\n");
 487       }
 488   }
 489 
 490 #else
 491 
 492   /* Suppress printing unit attention and `in progress of becoming ready'
 493      errors when we're not being verbose. */
 494 
 495   if (reqbuf->sense_key == UNIT_ATTENTION ||
 496       (reqbuf->sense_key == NOT_READY && (reqbuf->asc == 4 ||
 497                                           reqbuf->asc == 0x3a)))
 498     return;
 499 
 500   printk ("%s: code: 0x%02x  key: 0x%02x  asc: 0x%02x  ascq: 0x%02x\n",
 501           drive->name,
 502           reqbuf->error_code, reqbuf->sense_key, reqbuf->asc, reqbuf->ascq);
 503 #endif
 504 }
 505 
 506 
 507 /* Fix up a possibly partially-processed request so that we can
 508    start it over entirely, or even put it back on the request queue. */
 509 static void restore_request (struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
 510 {
 511   if (rq->buffer != rq->bh->b_data)
 512     {
 513       int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
 514       rq->buffer = rq->bh->b_data;
 515       rq->nr_sectors += n;
 516       rq->sector -= n;
 517     }
 518   rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
 519 }
 520 
 521 
 522 static void cdrom_queue_request_sense (ide_drive_t *drive, 
     /* [previous][next][first][last][top][bottom][index][help] */
 523                                        struct semaphore *sem,
 524                                        struct atapi_request_sense *reqbuf,
 525                                        struct packet_command *failed_command)
 526 {
 527   struct request *rq;
 528   struct packet_command *pc;
 529   int len;
 530 
 531   /* If the request didn't explicitly specify where to put the sense data,
 532      use the statically allocated structure. */
 533   if (reqbuf == NULL)
 534     reqbuf = &drive->cdrom_info.sense_data;
 535 
 536   /* Make up a new request to retrieve sense information. */
 537 
 538   pc = &HWIF(drive)->request_sense_pc;
 539   memset (pc, 0, sizeof (*pc));
 540 
 541   /* The request_sense structure has an odd number of (16-bit) words,
 542      which won't work well with 32-bit transfers.  However, we don't care
 543      about the last two bytes, so just truncate the structure down
 544      to an even length. */
 545   len = sizeof (*reqbuf) / 4;
 546   len *= 4;
 547 
 548   pc->c[0] = REQUEST_SENSE;
 549   pc->c[4] = len;
 550   pc->buffer = (char *)reqbuf;
 551   pc->buflen = len;
 552   pc->sense_data = (struct atapi_request_sense *)failed_command;
 553 
 554   /* stuff the sense request in front of our current request */
 555 
 556   rq = &HWIF(drive)->request_sense_request;
 557   ide_init_drive_cmd (rq);
 558   rq->cmd = REQUEST_SENSE_COMMAND;
 559   rq->buffer = (char *)pc;
 560   rq->sem = sem;
 561   (void) ide_do_drive_cmd (drive, rq, ide_preempt);
 562 }
 563 
 564 
 565 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 566 {
 567   struct request *rq = HWGROUP(drive)->rq;
 568 
 569   /* The code in blk.h can screw us up on error recovery if the block
 570      size is larger than 1k.  Fix that up here. */
 571   if (!uptodate && rq->bh != 0)
 572     {
 573       int adj = rq->current_nr_sectors - 1;
 574       rq->current_nr_sectors -= adj;
 575       rq->sector += adj;
 576     }
 577 
 578   if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate)
 579     {
 580       struct packet_command *pc = (struct packet_command *)rq->buffer;
 581       cdrom_analyze_sense_data (drive,
 582                                 (struct atapi_request_sense *)(pc->buffer - pc->c[4]), 
 583                                 (struct packet_command *)pc->sense_data);
 584     }
 585 
 586   ide_end_request (uptodate, HWGROUP(drive));
 587 }
 588 
 589 
 590 /* Mark that we've seen a media change, and invalidate our internal
 591    buffers. */
 592 static void cdrom_saw_media_change (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 593 {
 594   CDROM_STATE_FLAGS (drive)->media_changed = 1;
 595   CDROM_STATE_FLAGS (drive)->toc_valid = 0;
 596   drive->cdrom_info.nsectors_buffered = 0;
 597 }
 598 
 599 
 600 /* Returns 0 if the request should be continued.
 601    Returns 1 if the request was ended. */
 602 static int cdrom_decode_status (ide_drive_t *drive, int good_stat, int *stat_ret)
     /* [previous][next][first][last][top][bottom][index][help] */
 603 {
 604   struct request *rq = HWGROUP(drive)->rq;
 605   int stat, err, sense_key, cmd;
 606 
 607   /* Check for errors. */
 608   stat = GET_STAT();
 609   *stat_ret = stat;
 610 
 611   if (OK_STAT (stat, good_stat, BAD_R_STAT))
 612     return 0;
 613 
 614   /* Got an error. */
 615   err = IN_BYTE (IDE_ERROR_REG);
 616   sense_key = err >> 4;
 617 
 618   if (rq == NULL)
 619     printk ("%s : missing request in cdrom_decode_status\n", drive->name);
 620   else
 621     {
 622       cmd = rq->cmd;
 623 
 624       if (cmd == REQUEST_SENSE_COMMAND)
 625         {
 626           /* We got an error trying to get sense info from the drive
 627              (probably while trying to recover from a former error).
 628              Just give up. */
 629 
 630           struct packet_command *pc = (struct packet_command *)rq->buffer;
 631           pc->stat = 1;
 632           cdrom_end_request (1, drive);
 633           ide_error (drive, "request sense failure", stat);
 634           return 1;
 635         }
 636 
 637       else if (cmd == PACKET_COMMAND)
 638         {
 639           /* All other functions, except for READ. */
 640 
 641           struct packet_command *pc = (struct packet_command *)rq->buffer;
 642           struct semaphore *sem = NULL;
 643 
 644           /* Check for tray open. */
 645           if (sense_key == NOT_READY)
 646             {
 647               cdrom_saw_media_change (drive);
 648 
 649               /* Print an error message to the syslog.
 650                  Exception: don't print anything if this is a read subchannel
 651                  command.  This is because workman constantly polls the drive
 652                  with this command, and we don't want to uselessly fill up
 653                  the syslog. */
 654               if (pc->c[0] != SCMD_READ_SUBCHANNEL)
 655                 printk ("%s : tray open or drive not ready\n", drive->name);
 656             }
 657 
 658           /* Check for media change. */
 659           else if (sense_key == UNIT_ATTENTION)
 660             {
 661               cdrom_saw_media_change (drive);
 662               printk ("%s: media changed\n", drive->name);
 663             }
 664 
 665           /* Otherwise, print an error. */
 666           else
 667             {
 668               ide_dump_status (drive, "packet command error", stat);
 669             }
 670 
 671           /* Set the error flag and complete the request.
 672              Then, if we have a CHECK CONDITION status, queue a request
 673              sense command.  We must be careful, though: we don't want
 674              the thread in cdrom_queue_packet_command to wake up until
 675              the request sense has completed.  We do this by transferring
 676              the semaphore from the packet command request to the
 677              request sense request. */
 678 
 679           if ((stat & ERR_STAT) != 0)
 680             {
 681               sem = rq->sem;
 682               rq->sem = NULL;
 683             }
 684 
 685           pc->stat = 1;
 686           cdrom_end_request (1, drive);
 687 
 688           if ((stat & ERR_STAT) != 0)
 689             cdrom_queue_request_sense (drive, sem, pc->sense_data, pc);
 690         }
 691 
 692       else
 693         {
 694           /* Handle errors from READ requests. */
 695 
 696           /* Check for tray open. */
 697           if (sense_key == NOT_READY)
 698             {
 699               cdrom_saw_media_change (drive);
 700 
 701               /* Fail the request. */
 702               printk ("%s : tray open\n", drive->name);
 703               cdrom_end_request (0, drive);
 704             }
 705 
 706           /* Check for media change. */
 707           else if (sense_key == UNIT_ATTENTION)
 708             {
 709               cdrom_saw_media_change (drive);
 710 
 711               /* Arrange to retry the request.
 712                  But be sure to give up if we've retried too many times. */
 713               if (++rq->errors > ERROR_MAX)
 714                 {
 715                   cdrom_end_request (0, drive);
 716                 }
 717             }
 718           /* No point in retrying after an illegal request or
 719              data protect error.*/
 720           else if (sense_key == ILLEGAL_REQUEST || sense_key == DATA_PROTECT)
 721             {
 722               ide_dump_status (drive, "command error", stat);
 723               cdrom_end_request (0, drive);
 724             }
 725 
 726           /* If there were other errors, go to the default handler. */
 727           else if ((err & ~ABRT_ERR) != 0)
 728             {
 729               ide_error (drive, "cdrom_decode_status", stat);
 730               return 1;
 731             }
 732 
 733           /* Else, abort if we've racked up too many retries. */
 734           else if ((++rq->errors > ERROR_MAX))
 735             {
 736               cdrom_end_request (0, drive);
 737             }
 738 
 739           /* If we got a CHECK_CONDITION status, queue a request sense
 740              command. */
 741           if ((stat & ERR_STAT) != 0)
 742             cdrom_queue_request_sense (drive, NULL, NULL, NULL);
 743         }
 744     }
 745 
 746   /* Retry, or handle the next request. */
 747   return 1;
 748 }
 749 
 750 
 751 /* Set up the device registers for transferring a packet command on DEV,
 752    expecting to later transfer XFERLEN bytes.  HANDLER is the routine
 753    which actually transfers the command to the drive.  If this is a
 754    drq_interrupt device, this routine will arrange for HANDLER to be
 755    called when the interrupt from the drive arrives.  Otherwise, HANDLER
 756    will be called immediately after the drive is prepared for the transfer. */
 757 
 758 static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
     /* [previous][next][first][last][top][bottom][index][help] */
 759                                        ide_handler_t *handler)
 760 {
 761   /* Wait for the controller to be idle. */
 762   if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
 763 
 764   /* Set up the controller registers. */
 765   OUT_BYTE (0, IDE_FEATURE_REG);
 766   OUT_BYTE (0, IDE_NSECTOR_REG);
 767   OUT_BYTE (0, IDE_SECTOR_REG);
 768 
 769   OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
 770   OUT_BYTE (xferlen >> 8  , IDE_HCYL_REG);
 771   OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
 772 
 773   if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt)
 774     {
 775       ide_set_handler (drive, handler, WAIT_CMD);
 776       OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
 777     }
 778   else
 779     {
 780       OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
 781       (*handler) (drive);
 782     }
 783 
 784   return 0;
 785 }
 786 
 787 
 788 /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
 789    The device registers must have already been prepared
 790    by cdrom_start_packet_command.
 791    HANDLER is the interrupt handler to call when the command completes
 792    or there's data ready. */
 793 static int cdrom_transfer_packet_command (ide_drive_t *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
 794                                           char *cmd_buf, int cmd_len,
 795                                           ide_handler_t *handler)
 796 {
 797   if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt)
 798     {
 799       /* Here we should have been called after receiving an interrupt
 800          from the device.  DRQ should how be set. */
 801       int stat_dum;
 802 
 803       /* Check for errors. */
 804       if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum)) return 1;
 805     }
 806   else
 807     {
 808       /* Otherwise, we must wait for DRQ to get set. */
 809       if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) return 1;
 810     }
 811 
 812   /* Arm the interrupt handler. */
 813   ide_set_handler (drive, handler, WAIT_CMD);
 814 
 815   /* Send the command to the device. */
 816   cdrom_out_bytes (drive, cmd_buf, cmd_len);
 817 
 818   return 0;
 819 }
 820 
 821 
 822 
 823 /****************************************************************************
 824  * Block read functions.
 825  */
 826 
 827 /*
 828  * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
 829  * buffer.  Once the first sector is added, any subsequent sectors are
 830  * assumed to be continuous (until the buffer is cleared).  For the first
 831  * sector added, SECTOR is its sector number.  (SECTOR is then ignored until
 832  * the buffer is cleared.)
 833  */
 834 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
     /* [previous][next][first][last][top][bottom][index][help] */
 835                                   int sectors_to_transfer)
 836 {
 837   struct cdrom_info *info = &drive->cdrom_info;
 838 
 839   /* Number of sectors to read into the buffer. */
 840   int sectors_to_buffer = MIN (sectors_to_transfer,
 841                                (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
 842                                  info->nsectors_buffered);
 843 
 844   char *dest;
 845 
 846   /* If we don't yet have a sector buffer, try to allocate one.
 847      If we can't get one atomically, it's not fatal -- we'll just throw
 848      the data away rather than caching it. */
 849   if (info->sector_buffer == NULL)
 850     {
 851       info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE, GFP_ATOMIC);
 852 
 853       /* If we couldn't get a buffer, don't try to buffer anything... */
 854       if (info->sector_buffer == NULL)
 855         sectors_to_buffer = 0;
 856     }
 857 
 858   /* If this is the first sector in the buffer, remember its number. */
 859   if (info->nsectors_buffered == 0)
 860     info->sector_buffered = sector;
 861 
 862   /* Read the data into the buffer. */
 863   dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
 864   while (sectors_to_buffer > 0)
 865     {
 866       cdrom_in_bytes (drive, dest, SECTOR_SIZE);
 867       --sectors_to_buffer;
 868       --sectors_to_transfer;
 869       ++info->nsectors_buffered;
 870       dest += SECTOR_SIZE;
 871     }
 872 
 873   /* Throw away any remaining data. */
 874   while (sectors_to_transfer > 0)
 875     {
 876       char dum[SECTOR_SIZE];
 877       cdrom_in_bytes (drive, dum, sizeof (dum));
 878       --sectors_to_transfer;
 879     }
 880 }
 881 
 882 
 883 /*
 884  * Check the contents of the interrupt reason register from the cdrom
 885  * and attempt to recover if there are problems.  Returns  0 if everything's
 886  * ok; nonzero if the request has been terminated.
 887  */
 888 static inline
 889 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
     /* [previous][next][first][last][top][bottom][index][help] */
 890 {
 891   ireason &= 3;
 892   if (ireason == 2) return 0;
 893 
 894   if (ireason == 0)
 895     {
 896       /* Whoops... The drive is expecting to receive data from us! */
 897       printk ("%s: cdrom_read_intr: "
 898               "Drive wants to transfer data the wrong way!\n",
 899               drive->name);
 900 
 901       /* Throw some data at the drive so it doesn't hang
 902          and quit this request. */
 903       while (len > 0)
 904         {
 905           int dum = 0;
 906           cdrom_out_bytes (drive, &dum, sizeof (dum));
 907           len -= sizeof (dum);
 908         }
 909     }
 910 
 911   else
 912     {
 913       /* Drive wants a command packet, or invalid ireason... */
 914       printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
 915               drive->name, ireason);
 916     }
 917 
 918   cdrom_end_request (0, drive);
 919   return -1;
 920 }
 921 
 922 
 923 /*
 924  * Interrupt routine.  Called when a read request has completed.
 925  */
 926 static void cdrom_read_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 927 {
 928   int stat;
 929   int ireason, len, sectors_to_transfer, nskip;
 930 
 931   struct request *rq = HWGROUP(drive)->rq;
 932 
 933   /* Check for errors. */
 934   if (cdrom_decode_status (drive, 0, &stat)) return;
 935 
 936   /* Read the interrupt reason and the transfer length. */
 937   ireason = IN_BYTE (IDE_NSECTOR_REG);
 938   len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
 939 
 940   /* If DRQ is clear, the command has completed. */
 941   if ((stat & DRQ_STAT) == 0)
 942     {
 943       /* If we're not done filling the current buffer, complain.
 944          Otherwise, complete the command normally. */
 945       if (rq->current_nr_sectors > 0)
 946         {
 947           printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
 948                   drive->name, rq->current_nr_sectors);
 949           cdrom_end_request (0, drive);
 950         }
 951       else
 952         cdrom_end_request (1, drive);
 953 
 954       return;
 955     }
 956 
 957   /* Check that the drive is expecting to do the same thing that we are. */
 958   if (cdrom_read_check_ireason (drive, len, ireason)) return;
 959 
 960   /* Assume that the drive will always provide data in multiples of at least
 961      SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise. */
 962   if ((len % SECTOR_SIZE) != 0)
 963     {
 964       printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
 965               drive->name, len);
 966       printk ("  This drive is not supported by this version of the driver\n");
 967       cdrom_end_request (0, drive);
 968       return;
 969     }
 970 
 971   /* The number of sectors we need to read from the drive. */
 972   sectors_to_transfer = len / SECTOR_SIZE;
 973 
 974   /* First, figure out if we need to bit-bucket any of the leading sectors. */
 975   nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
 976                sectors_to_transfer);
 977 
 978   while (nskip > 0)
 979     {
 980       /* We need to throw away a sector. */
 981       char dum[SECTOR_SIZE];
 982       cdrom_in_bytes (drive, dum, sizeof (dum));
 983 
 984       --rq->current_nr_sectors;
 985       --nskip;
 986       --sectors_to_transfer;
 987     }
 988 
 989   /* Now loop while we still have data to read from the drive. */
 990   while (sectors_to_transfer > 0)
 991     {
 992       int this_transfer;
 993 
 994       /* If we've filled the present buffer but there's another chained
 995          buffer after it, move on. */
 996       if (rq->current_nr_sectors == 0 &&
 997           rq->nr_sectors > 0)
 998         cdrom_end_request (1, drive);
 999 
1000       /* If the buffers are full, cache the rest of the data in our
1001          internal buffer. */
1002       if (rq->current_nr_sectors == 0)
1003         {
1004           cdrom_buffer_sectors (drive, rq->sector, sectors_to_transfer);
1005           sectors_to_transfer = 0;
1006         }
1007       else
1008         {
1009           /* Transfer data to the buffers.
1010              Figure out how many sectors we can transfer
1011              to the current buffer. */
1012           this_transfer = MIN (sectors_to_transfer,
1013                                rq->current_nr_sectors);
1014 
1015           /* Read this_transfer sectors into the current buffer. */
1016           while (this_transfer > 0)
1017             {
1018               cdrom_in_bytes (drive, rq->buffer, SECTOR_SIZE);
1019               rq->buffer += SECTOR_SIZE;
1020               --rq->nr_sectors;
1021               --rq->current_nr_sectors;
1022               ++rq->sector;
1023               --this_transfer;
1024               --sectors_to_transfer;
1025             }
1026         }
1027     }
1028 
1029   /* Done moving data!
1030      Wait for another interrupt. */
1031   ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);
1032 }
1033 
1034 
1035 /*
1036  * Try to satisfy some of the current read request from our cached data.
1037  * Returns nonzero if the request has been completed, zero otherwise.
1038  */
1039 static int cdrom_read_from_buffer (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1040 {
1041   struct cdrom_info *info = &drive->cdrom_info;
1042   struct request *rq = HWGROUP(drive)->rq;
1043 
1044   /* Can't do anything if there's no buffer. */
1045   if (info->sector_buffer == NULL) return 0;
1046 
1047   /* Loop while this request needs data and the next block is present
1048      in our cache. */
1049   while (rq->nr_sectors > 0 &&
1050          rq->sector >= info->sector_buffered &&
1051          rq->sector < info->sector_buffered + info->nsectors_buffered)
1052     {
1053       if (rq->current_nr_sectors == 0)
1054         cdrom_end_request (1, drive);
1055 
1056       memcpy (rq->buffer,
1057               info->sector_buffer +
1058                 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1059               SECTOR_SIZE);
1060       rq->buffer += SECTOR_SIZE;
1061       --rq->current_nr_sectors;
1062       --rq->nr_sectors;
1063       ++rq->sector;
1064     }
1065 
1066   /* If we've satisfied the current request, terminate it successfully. */
1067   if (rq->nr_sectors == 0)
1068     {
1069       cdrom_end_request (1, drive);
1070       return -1;
1071     }
1072 
1073   /* Move on to the next buffer if needed. */
1074   if (rq->current_nr_sectors == 0)
1075     cdrom_end_request (1, drive);
1076 
1077   /* If this condition does not hold, then the kluge i use to
1078      represent the number of sectors to skip at the start of a transfer
1079      will fail.  I think that this will never happen, but let's be
1080      paranoid and check. */
1081   if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1082       (rq->sector % SECTORS_PER_FRAME) != 0)
1083     {
1084       printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1085               drive->name, rq->sector);
1086       cdrom_end_request (0, drive);
1087       return -1;
1088     }
1089 
1090   return 0;
1091 }
1092 
1093 
1094 
1095 /*
1096  * Routine to send a read packet command to the drive.
1097  * This is usually called directly from cdrom_start_read.
1098  * However, for drq_interrupt devices, it is called from an interrupt
1099  * when the drive is ready to accept the command.
1100  */
1101 static void cdrom_start_read_continuation (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1102 {
1103   struct packet_command pc;
1104   struct request *rq = HWGROUP(drive)->rq;
1105 
1106   int nsect, sector, nframes, frame, nskip;
1107 
1108   /* Number of sectors to transfer. */
1109   nsect = rq->nr_sectors;
1110 
1111   /* Starting sector. */
1112   sector = rq->sector;
1113 
1114   /* If the requested sector doesn't start on a cdrom block boundary,
1115      we must adjust the start of the transfer so that it does,
1116      and remember to skip the first few sectors.  If the CURRENT_NR_SECTORS
1117      field is larger than the size of the buffer, it will mean that
1118      we're to skip a number of sectors equal to the amount by which
1119      CURRENT_NR_SECTORS is larger than the buffer size. */
1120   nskip = (sector % SECTORS_PER_FRAME);
1121   if (nskip > 0)
1122     {
1123       /* Sanity check... */
1124       if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS))
1125         {
1126           printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1127                   drive->name, rq->current_nr_sectors);
1128           cdrom_end_request (0, drive);
1129           return;
1130         }
1131 
1132       sector -= nskip;
1133       nsect += nskip;
1134       rq->current_nr_sectors += nskip;
1135     }
1136 
1137   /* Convert from sectors to cdrom blocks, rounding up the transfer
1138      length if needed. */
1139   nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1140   frame = sector / SECTORS_PER_FRAME;
1141 
1142   /* Largest number of frames was can transfer at once is 64k-1. */
1143   nframes = MIN (nframes, 65535);
1144 
1145   /* Set up the command */
1146   memset (&pc.c, 0, sizeof (pc.c));
1147   pc.c[0] = READ_10;
1148   pc.c[7] = (nframes >> 8);
1149   pc.c[8] = (nframes & 0xff);
1150   *(int *)(&pc.c[2]) = htonl (frame);
1151 
1152   /* Send the command to the drive and return. */
1153   (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c),
1154                                         &cdrom_read_intr);
1155 }
1156 
1157 
1158 /*
1159  * Start a read request from the CD-ROM.
1160  */
1161 static void cdrom_start_read (ide_drive_t *drive, unsigned int block)
     /* [previous][next][first][last][top][bottom][index][help] */
1162 {
1163   struct request *rq = HWGROUP(drive)->rq;
1164   int minor = MINOR (rq->rq_dev);
1165 
1166   /* If the request is relative to a partition, fix it up to refer to the
1167      absolute address.  */
1168   if ((minor & PARTN_MASK) != 0) {
1169     rq->sector = block;
1170     minor &= ~PARTN_MASK;
1171     rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1172   }
1173 
1174   /* We may be retrying this request after an error.
1175      Fix up any weirdness which might be present in the request packet. */
1176   restore_request (rq);
1177 
1178   /* Satisfy whatever we can of this request from our cached sector. */
1179   if (cdrom_read_from_buffer (drive))
1180     return;
1181 
1182   /* Clear the local sector buffer. */
1183   drive->cdrom_info.nsectors_buffered = 0;
1184 
1185   /* Start sending the read request to the drive. */
1186   cdrom_start_packet_command (drive, 32768, cdrom_start_read_continuation);
1187 }
1188 
1189 
1190 
1191 
1192 /****************************************************************************
1193  * Execute all other packet commands.
1194  */
1195 
1196 /* Forward declarations. */
1197 static int
1198 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1199                 struct atapi_request_sense *reqbuf);
1200 
1201 
1202 
1203 /* Interrupt routine for packet command completion. */
1204 static void cdrom_pc_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1205 {
1206   int ireason, len, stat, thislen;
1207   struct request *rq = HWGROUP(drive)->rq;
1208   struct packet_command *pc = (struct packet_command *)rq->buffer;
1209 
1210   /* Check for errors. */
1211   if (cdrom_decode_status (drive, 0, &stat)) return;
1212 
1213   /* Read the interrupt reason and the transfer length. */
1214   ireason = IN_BYTE (IDE_NSECTOR_REG);
1215   len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1216 
1217   /* If DRQ is clear, the command has completed.
1218      Complain if we still have data left to transfer. */
1219   if ((stat & DRQ_STAT) == 0)
1220     {
1221       /* Some of the trailing request sense fields are optional, and
1222          some drives don't send them.  Sigh. */
1223       if (pc->c[0] == REQUEST_SENSE && pc->buflen > 0 && pc->buflen <= 5) {
1224         while (pc->buflen > 0) {
1225           *pc->buffer++ = 0;
1226           --pc->buflen;
1227         }
1228       }
1229 
1230       if (pc->buflen == 0)
1231         cdrom_end_request (1, drive);
1232       else
1233         {
1234           printk ("%s: cdrom_pc_intr: data underrun %d\n",
1235                   drive->name, pc->buflen);
1236           pc->stat = 1;
1237           cdrom_end_request (1, drive);
1238         }
1239       return;
1240     }
1241 
1242   /* Figure out how much data to transfer. */
1243   thislen = pc->buflen;
1244   if (thislen < 0) thislen = -thislen;
1245   if (thislen > len) thislen = len;
1246 
1247   /* The drive wants to be written to. */
1248   if ((ireason & 3) == 0)
1249     {
1250       /* Check that we want to write. */
1251       if (pc->buflen > 0)
1252         {
1253           printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
1254                   drive->name);
1255           pc->stat = 1;
1256           thislen = 0;
1257         }
1258 
1259       /* Transfer the data. */
1260       cdrom_out_bytes (drive, pc->buffer, thislen);
1261 
1262       /* If we haven't moved enough data to satisfy the drive,
1263          add some padding. */
1264       while (len > thislen)
1265         {
1266           int dum = 0;
1267           cdrom_out_bytes (drive, &dum, sizeof (dum));
1268           len -= sizeof (dum);
1269         }
1270 
1271       /* Keep count of how much data we've moved. */
1272       pc->buffer += thislen;
1273       pc->buflen += thislen;
1274     }
1275 
1276   /* Same drill for reading. */
1277   else if ((ireason & 3) == 2)
1278     {
1279       /* Check that we want to read. */
1280       if (pc->buflen < 0)
1281         {
1282           printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
1283                   drive->name);
1284           pc->stat = 1;
1285           thislen = 0;
1286         }
1287 
1288       /* Transfer the data. */
1289       cdrom_in_bytes (drive, pc->buffer, thislen);
1290 
1291       /* If we haven't moved enough data to satisfy the drive,
1292          add some padding. */
1293       while (len > thislen)
1294         {
1295           int dum = 0;
1296           cdrom_in_bytes (drive, &dum, sizeof (dum));
1297           len -= sizeof (dum);
1298         }
1299 
1300       /* Keep count of how much data we've moved. */
1301       pc->buffer += thislen;
1302       pc->buflen -= thislen;
1303     }
1304 
1305   else
1306     {
1307       printk ("%s: cdrom_pc_intr: The drive appears confused (ireason = 0x%2x)\n",
1308               drive->name, ireason);
1309       pc->stat = 1;
1310     }
1311 
1312   /* Now we wait for another interrupt. */
1313   ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);
1314 }
1315 
1316 
1317 static void cdrom_do_pc_continuation (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1318 {
1319   struct request *rq = HWGROUP(drive)->rq;
1320   struct packet_command *pc = (struct packet_command *)rq->buffer;
1321 
1322   /* Send the command to the drive and return. */
1323   cdrom_transfer_packet_command (drive, pc->c, sizeof (pc->c), &cdrom_pc_intr);
1324 }
1325 
1326 
1327 static void cdrom_do_packet_command (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1328 {
1329   int len;
1330   struct request *rq = HWGROUP(drive)->rq;
1331   struct packet_command *pc = (struct packet_command *)rq->buffer;
1332 
1333   len = pc->buflen;
1334   if (len < 0) len = -len;
1335 
1336   pc->stat = 0;
1337 
1338   /* Start sending the command to the drive. */
1339   cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1340 }
1341 
1342 
1343 /* Sleep for TIME jiffies.
1344    Not to be called from an interrupt handler. */
1345 static
1346 void cdrom_sleep (int time)
     /* [previous][next][first][last][top][bottom][index][help] */
1347 {
1348   current->state = TASK_INTERRUPTIBLE;
1349   current->timeout = jiffies + time;
1350   schedule ();
1351 }
1352 
1353 static
1354 int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
1355 {
1356   struct atapi_request_sense my_reqbuf;
1357   int retries = 10;
1358   struct request req;
1359 
1360   /* If our caller has not provided a place to stick any sense data,
1361      use our own area. */
1362   if (pc->sense_data == NULL)
1363     pc->sense_data = &my_reqbuf;
1364   pc->sense_data->sense_key = 0;
1365 
1366   /* Start of retry loop. */
1367   do {
1368     ide_init_drive_cmd (&req);
1369     req.cmd = PACKET_COMMAND;
1370     req.buffer = (char *)pc;
1371     (void) ide_do_drive_cmd (drive, &req, ide_wait);
1372 
1373     if (pc->stat != 0)
1374       {
1375         /* The request failed.  Retry if it was due to a unit attention status
1376            (usually means media was changed). */
1377         struct atapi_request_sense *reqbuf = pc->sense_data;
1378 
1379         if (reqbuf->sense_key == UNIT_ATTENTION)
1380           ;
1381 
1382         /* Also retry if the drive is in the process of loading a disk.
1383            This time, however, wait a little between retries to give
1384            the drive time. */
1385         else if (reqbuf->sense_key == NOT_READY && reqbuf->asc == 4)
1386           {
1387             cdrom_sleep (HZ);
1388           }
1389 
1390         /* Otherwise, don't retry. */
1391         else
1392           retries = 0;
1393 
1394         --retries;
1395       }
1396 
1397     /* End of retry loop. */
1398   } while (pc->stat != 0 && retries >= 0);
1399 
1400 
1401   /* Return an error if the command failed. */
1402   if (pc->stat != 0)
1403     return -EIO;
1404 
1405   else
1406     {
1407       /* The command succeeded.  If it was anything other than a request sense,
1408          eject, or door lock command, and we think that the door is presently
1409          unlocked, lock it again.  (The door was probably unlocked via
1410          an explicit CDROMEJECT ioctl.) */
1411       if (CDROM_STATE_FLAGS (drive)->door_locked == 0 &&
1412           (pc->c[0] != REQUEST_SENSE &&
1413            pc->c[0] != ALLOW_MEDIUM_REMOVAL &&
1414            pc->c[0] != START_STOP))
1415         {
1416           (void) cdrom_lockdoor (drive, 1, NULL);
1417         }
1418       return 0;
1419     }
1420 }
1421 
1422 
1423 
1424 /****************************************************************************
1425  * cdrom driver request routine.
1426  */
1427 
1428 void ide_do_rw_cdrom (ide_drive_t *drive, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
1429 {
1430   struct request *rq = HWGROUP(drive)->rq;
1431 
1432   if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1433     cdrom_do_packet_command (drive);
1434 
1435   else if (rq -> cmd == RESET_DRIVE_COMMAND)
1436     {
1437       cdrom_end_request (1, drive);
1438       ide_do_reset (drive);
1439       return;
1440     }
1441 
1442   else if (rq -> cmd != READ)
1443     {
1444       printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1445       cdrom_end_request (0, drive);
1446     }
1447   else
1448     cdrom_start_read (drive, block);
1449 }
1450 
1451 
1452 
1453 /****************************************************************************
1454  * Ioctl handling.
1455  *
1456  * Routines which queue packet commands take as a final argument a pointer
1457  * to an atapi_request_sense struct.  If execution of the command results
1458  * in an error with a CHECK CONDITION status, this structure will be filled
1459  * with the results of the subsequent request sense command.  The pointer
1460  * can also be NULL, in which case no sense information is returned.
1461  */
1462 
1463 #if ! STANDARD_ATAPI
1464 static
1465 int bin2bcd (int x)
     /* [previous][next][first][last][top][bottom][index][help] */
1466 {
1467   return (x%10) | ((x/10) << 4);
1468 }
1469 
1470 
1471 static
1472 int bcd2bin (int x)
     /* [previous][next][first][last][top][bottom][index][help] */
1473 {
1474   return (x >> 4) * 10 + (x & 0x0f);
1475 }
1476 #endif /* not STANDARD_ATAPI */
1477 
1478 
1479 static inline
1480 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
     /* [previous][next][first][last][top][bottom][index][help] */
1481 {
1482   lba += CD_BLOCK_OFFSET;
1483   lba &= 0xffffff;  /* negative lbas use only 24 bits */
1484   *m = lba / (CD_SECS * CD_FRAMES);
1485   lba %= (CD_SECS * CD_FRAMES);
1486   *s = lba / CD_FRAMES;
1487   *f = lba % CD_FRAMES;
1488 }
1489 
1490 
1491 static inline
1492 int msf_to_lba (byte m, byte s, byte f)
     /* [previous][next][first][last][top][bottom][index][help] */
1493 {
1494   return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
1495 }
1496 
1497 
1498 static int
1499 cdrom_check_status (ide_drive_t  *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
1500                     struct atapi_request_sense *reqbuf)
1501 {
1502   struct packet_command pc;
1503 
1504   memset (&pc, 0, sizeof (pc));
1505 
1506   pc.sense_data = reqbuf;
1507   pc.c[0] = TEST_UNIT_READY;
1508 
1509   return cdrom_queue_packet_command (drive, &pc);
1510 }
1511 
1512 
1513 /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1514 static int
1515 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1516                 struct atapi_request_sense *reqbuf)
1517 {
1518   struct atapi_request_sense my_reqbuf;
1519   int stat;
1520   struct packet_command pc;
1521 
1522   if (reqbuf == NULL)
1523     reqbuf = &my_reqbuf;
1524 
1525   /* If the drive cannot lock the door, just pretend. */
1526   if (CDROM_CONFIG_FLAGS (drive)->no_doorlock)
1527     stat = 0;
1528   else
1529     {
1530       memset (&pc, 0, sizeof (pc));
1531       pc.sense_data = reqbuf;
1532 
1533       pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1534       pc.c[4] = (lockflag != 0);
1535       stat = cdrom_queue_packet_command (drive, &pc);
1536     }
1537 
1538   if (stat == 0)
1539     CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1540   else
1541     {
1542       /* If we got an illegal field error, the drive
1543          probably cannot lock the door. */
1544       if (reqbuf->sense_key == ILLEGAL_REQUEST && reqbuf->asc == 0x24)
1545         {
1546           printk ("%s: door locking not supported\n", drive->name);
1547           CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1548           stat = 0;
1549           CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1550         }
1551     }
1552   return stat;
1553 }
1554 
1555 
1556 /* Eject the disk if EJECTFLAG is 0.
1557    If EJECTFLAG is 1, try to reload the disk. */
1558 static int
1559 cdrom_eject (ide_drive_t *drive, int ejectflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1560              struct atapi_request_sense *reqbuf)
1561 {
1562   struct packet_command pc;
1563 
1564   memset (&pc, 0, sizeof (pc));
1565   pc.sense_data = reqbuf;
1566 
1567   pc.c[0] = START_STOP;
1568   pc.c[4] = 2 + (ejectflag != 0);
1569   return cdrom_queue_packet_command (drive, &pc);
1570 }
1571 
1572 
1573 static int
1574 cdrom_pause (ide_drive_t *drive, int pauseflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1575              struct atapi_request_sense *reqbuf)
1576 {
1577   struct packet_command pc;
1578 
1579   memset (&pc, 0, sizeof (pc));
1580   pc.sense_data = reqbuf;
1581 
1582   pc.c[0] = SCMD_PAUSE_RESUME;
1583   pc.c[8] = !pauseflag;
1584   return cdrom_queue_packet_command (drive, &pc);
1585 }
1586 
1587 
1588 static int
1589 cdrom_startstop (ide_drive_t *drive, int startflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1590                  struct atapi_request_sense *reqbuf)
1591 {
1592   struct packet_command pc;
1593 
1594   memset (&pc, 0, sizeof (pc));
1595   pc.sense_data = reqbuf;
1596 
1597   pc.c[0] = START_STOP;
1598   pc.c[1] = 1;
1599   pc.c[4] = startflag;
1600   return cdrom_queue_packet_command (drive, &pc);
1601 }
1602 
1603 
1604 static int
1605 cdrom_read_capacity (ide_drive_t *drive, unsigned *capacity,
     /* [previous][next][first][last][top][bottom][index][help] */
1606                      struct atapi_request_sense *reqbuf)
1607 {
1608   struct {
1609     unsigned lba;
1610     unsigned blocklen;
1611   } capbuf;
1612 
1613   int stat;
1614   struct packet_command pc;
1615 
1616   memset (&pc, 0, sizeof (pc));
1617   pc.sense_data = reqbuf;
1618 
1619   pc.c[0] = READ_CAPACITY;
1620   pc.buffer = (char *)&capbuf;
1621   pc.buflen = sizeof (capbuf);
1622 
1623   stat = cdrom_queue_packet_command (drive, &pc);
1624   if (stat == 0)
1625     {
1626       *capacity = ntohl (capbuf.lba);
1627     }
1628 
1629   return stat;
1630 }
1631 
1632 
1633 static int
1634 cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
     /* [previous][next][first][last][top][bottom][index][help] */
1635                      int format, char *buf, int buflen,
1636                      struct atapi_request_sense *reqbuf)
1637 {
1638   struct packet_command pc;
1639 
1640   memset (&pc, 0, sizeof (pc));
1641   pc.sense_data = reqbuf;
1642 
1643   pc.buffer =  buf;
1644   pc.buflen = buflen;
1645   pc.c[0] = SCMD_READ_TOC;
1646   pc.c[6] = trackno;
1647   pc.c[7] = (buflen >> 8);
1648   pc.c[8] = (buflen & 0xff);
1649   pc.c[9] = (format << 6);
1650   if (msf_flag) pc.c[1] = 2;
1651   return cdrom_queue_packet_command (drive, &pc);
1652 }
1653 
1654 
1655 /* Try to read the entire TOC for the disk into our internal buffer. */
1656 static int
1657 cdrom_read_toc (ide_drive_t *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
1658                 struct atapi_request_sense *reqbuf)
1659 {
1660   int msf_flag;
1661   int stat, ntracks, i;
1662   struct atapi_toc *toc = drive->cdrom_info.toc;
1663   struct {
1664     struct atapi_toc_header hdr;
1665     struct atapi_toc_entry  ent;
1666   } ms_tmp;
1667 
1668   if (toc == NULL)
1669     {
1670       /* Try to allocate space. */
1671       toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1672                                           GFP_KERNEL);
1673       drive->cdrom_info.toc = toc;
1674     }
1675 
1676   if (toc == NULL)
1677     {
1678       printk ("%s: No cdrom TOC buffer!\n", drive->name);
1679       return -EIO;
1680     }
1681 
1682   /* Check to see if the existing data is still valid.
1683      If it is, just return. */
1684   if (CDROM_STATE_FLAGS (drive)->toc_valid)
1685     (void) cdrom_check_status (drive, NULL);
1686 
1687   if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1688 
1689 #if STANDARD_ATAPI
1690   msf_flag = 0;
1691 #else  /* not STANDARD_ATAPI */
1692   /* Some drives can't return TOC data in LBA format. */
1693   msf_flag = (CDROM_CONFIG_FLAGS (drive)->no_lba_toc);
1694 #endif  /* not STANDARD_ATAPI */
1695 
1696   /* First read just the header, so we know how long the TOC is. */
1697   stat = cdrom_read_tocentry (drive, 0, msf_flag, 0, (char *)&toc->hdr,
1698                               sizeof (struct atapi_toc_header) +
1699                               sizeof (struct atapi_toc_entry),
1700                               reqbuf);
1701   if (stat) return stat;
1702 
1703 #if ! STANDARD_ATAPI
1704   if (CDROM_CONFIG_FLAGS (drive)->vertos_lossage)
1705     {
1706       toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1707       toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
1708       /* hopefully the length is not BCD, too ;-| */
1709     }
1710 #endif  /* not STANDARD_ATAPI */
1711 
1712   ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1713   if (ntracks <= 0) return -EIO;
1714   if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1715 
1716   /* Now read the whole schmeer. */
1717   stat = cdrom_read_tocentry (drive, 0, msf_flag, 0, (char *)&toc->hdr,
1718                               sizeof (struct atapi_toc_header) +
1719                               (ntracks+1) * sizeof (struct atapi_toc_entry),
1720                               reqbuf);
1721   if (stat) return stat;
1722   toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1723 
1724 #if ! STANDARD_ATAPI
1725   if (CDROM_CONFIG_FLAGS (drive)->vertos_lossage)
1726     {
1727       toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1728       toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
1729       /* hopefully the length is not BCD, too ;-| */
1730     }
1731 #endif  /* not STANDARD_ATAPI */
1732 
1733   for (i=0; i<=ntracks; i++)
1734     {
1735 #if ! STANDARD_ATAPI
1736       if (msf_flag)
1737         {
1738           if (CDROM_CONFIG_FLAGS (drive)->vertos_lossage)
1739             {
1740               toc->ent[i].track = bcd2bin (toc->ent[i].track);
1741               toc->ent[i].addr.msf.m = bcd2bin (toc->ent[i].addr.msf.m);
1742               toc->ent[i].addr.msf.s = bcd2bin (toc->ent[i].addr.msf.s);
1743               toc->ent[i].addr.msf.f = bcd2bin (toc->ent[i].addr.msf.f);
1744             }
1745           toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.m,
1746                                              toc->ent[i].addr.msf.s,
1747                                              toc->ent[i].addr.msf.f);
1748         }
1749       else
1750 #endif  /* not STANDARD_ATAPI */
1751         toc->ent[i].addr.lba = ntohl (toc->ent[i].addr.lba);
1752     }
1753 
1754   /* Read the multisession information. */
1755   stat = cdrom_read_tocentry (drive, 0, msf_flag, 1,
1756                               (char *)&ms_tmp, sizeof (ms_tmp),
1757                               reqbuf);
1758   if (stat) return stat;
1759 #if ! STANDARD_ATAPI
1760   if (msf_flag)
1761     toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.m,
1762                                         ms_tmp.ent.addr.msf.s,
1763                                         ms_tmp.ent.addr.msf.f);
1764   else
1765 #endif  /* not STANDARD_ATAPI */
1766     toc->last_session_lba = ntohl (ms_tmp.ent.addr.lba);
1767 
1768   toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1769 
1770   /* Now try to get the total cdrom capacity. */
1771   stat = cdrom_read_capacity (drive, &toc->capacity, reqbuf);
1772   if (stat) toc->capacity = 0x1fffff;
1773 
1774   HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS]
1775     = toc->capacity * SECTORS_PER_FRAME;
1776   drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1777 
1778   /* Remember that we've read this stuff. */
1779   CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1780 
1781   return 0;
1782 }
1783 
1784 
1785 static int
1786 cdrom_read_subchannel (ide_drive_t *drive,
     /* [previous][next][first][last][top][bottom][index][help] */
1787                        char *buf, int buflen,
1788                        struct atapi_request_sense *reqbuf)
1789 {
1790   struct packet_command pc;
1791 
1792   memset (&pc, 0, sizeof (pc));
1793   pc.sense_data = reqbuf;
1794 
1795   pc.buffer =  buf;
1796   pc.buflen = buflen;
1797   pc.c[0] = SCMD_READ_SUBCHANNEL;
1798   pc.c[2] = 0x40;  /* request subQ data */
1799   pc.c[3] = 0x01;  /* Format 1: current position */
1800   pc.c[7] = (buflen >> 8);
1801   pc.c[8] = (buflen & 0xff);
1802   return cdrom_queue_packet_command (drive, &pc);
1803 }
1804 
1805 
1806 /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1807 static int
1808 cdrom_mode_sense (ide_drive_t *drive, int pageno, int modeflag,
     /* [previous][next][first][last][top][bottom][index][help] */
1809                   char *buf, int buflen,
1810                   struct atapi_request_sense *reqbuf)
1811 {
1812   struct packet_command pc;
1813 
1814   memset (&pc, 0, sizeof (pc));
1815   pc.sense_data = reqbuf;
1816 
1817   pc.buffer =  buf;
1818   pc.buflen = buflen;
1819   pc.c[0] = MODE_SENSE_10;
1820   pc.c[2] = pageno | (modeflag << 6);
1821   pc.c[7] = (buflen >> 8);
1822   pc.c[8] = (buflen & 0xff);
1823   return cdrom_queue_packet_command (drive, &pc);
1824 }
1825 
1826 
1827 static int
1828 cdrom_mode_select (ide_drive_t *drive, int pageno, char *buf, int buflen,
     /* [previous][next][first][last][top][bottom][index][help] */
1829                    struct atapi_request_sense *reqbuf)
1830 {
1831   struct packet_command pc;
1832 
1833   memset (&pc, 0, sizeof (pc));
1834   pc.sense_data = reqbuf;
1835 
1836   pc.buffer =  buf;
1837   pc.buflen = - buflen;
1838   pc.c[0] = MODE_SELECT_10;
1839   pc.c[1] = 0x10;
1840   pc.c[2] = pageno;
1841   pc.c[7] = (buflen >> 8);
1842   pc.c[8] = (buflen & 0xff);
1843   return cdrom_queue_packet_command (drive, &pc);
1844 }
1845 
1846 
1847 static int
1848 cdrom_play_lba_range_play12 (ide_drive_t *drive, int lba_start, int lba_end,
     /* [previous][next][first][last][top][bottom][index][help] */
1849                              struct atapi_request_sense *reqbuf)
1850 {
1851   struct packet_command pc;
1852 
1853   memset (&pc, 0, sizeof (pc));
1854   pc.sense_data = reqbuf;
1855 
1856   pc.c[0] = SCMD_PLAYAUDIO12;
1857   *(int *)(&pc.c[2]) = htonl (lba_start);
1858   *(int *)(&pc.c[6]) = htonl (lba_end - lba_start);
1859 
1860   return cdrom_queue_packet_command (drive, &pc);
1861 }
1862 
1863 
1864 #if !  STANDARD_ATAPI
1865 static int
1866 cdrom_play_lba_range_msf (ide_drive_t *drive, int lba_start, int lba_end,
     /* [previous][next][first][last][top][bottom][index][help] */
1867                           struct atapi_request_sense *reqbuf)
1868 {
1869   struct packet_command pc;
1870 
1871   memset (&pc, 0, sizeof (pc));
1872   pc.sense_data = reqbuf;
1873 
1874   pc.c[0] = SCMD_PLAYAUDIO_MSF;
1875   lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1876   lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1877 
1878   if (CDROM_CONFIG_FLAGS (drive)->playmsf_uses_bcd)
1879     {
1880       pc.c[3] = bin2bcd (pc.c[3]);
1881       pc.c[4] = bin2bcd (pc.c[4]);
1882       pc.c[5] = bin2bcd (pc.c[5]);
1883       pc.c[6] = bin2bcd (pc.c[6]);
1884       pc.c[7] = bin2bcd (pc.c[7]);
1885       pc.c[8] = bin2bcd (pc.c[8]);
1886     }
1887 
1888   return cdrom_queue_packet_command (drive, &pc);
1889 }
1890 #endif  /* not STANDARD_ATAPI */
1891 
1892 
1893 /* Play audio starting at LBA LBA_START and finishing with the
1894    LBA before LBA_END. */
1895 static int
1896 cdrom_play_lba_range (ide_drive_t *drive, int lba_start, int lba_end,
     /* [previous][next][first][last][top][bottom][index][help] */
1897                       struct atapi_request_sense *reqbuf)
1898 {
1899   /* This is rather annoying.
1900      My NEC-260 won't recognize group 5 commands such as PLAYAUDIO12;
1901      the only way to get it to play more than 64k of blocks at once
1902      seems to be the PLAYAUDIO_MSF command.  However, the parameters
1903      the NEC 260 wants for the PLAYMSF command are incompatible with
1904      the new version of the spec.
1905 
1906      So what i'll try is this.  First try for PLAYAUDIO12.  If it works,
1907      great.  Otherwise, if the drive reports an illegal command code,
1908      try PLAYAUDIO_MSF using the NEC 260-style bcd parameters. */
1909 
1910 #if ! STANDARD_ATAPI
1911   if (CDROM_CONFIG_FLAGS (drive)->no_playaudio12)
1912     return cdrom_play_lba_range_msf (drive, lba_start, lba_end, reqbuf);
1913   else
1914 #endif  /* not STANDARD_ATAPI */
1915     {
1916       int stat;
1917       struct atapi_request_sense my_reqbuf;
1918 
1919       if (reqbuf == NULL)
1920         reqbuf = &my_reqbuf;
1921 
1922       stat = cdrom_play_lba_range_play12 (drive, lba_start, lba_end, reqbuf);
1923       if (stat == 0) return 0;
1924 
1925 #if ! STANDARD_ATAPI
1926       /* It failed.  Try to find out why. */
1927       if (reqbuf->sense_key == ILLEGAL_REQUEST && reqbuf->asc == 0x20)
1928         {
1929           /* The drive didn't recognize the command.
1930              Retry with the MSF variant. */
1931           printk ("%s: Drive does not support PLAYAUDIO12; "
1932                   "trying PLAYAUDIO_MSF\n", drive->name);
1933           CDROM_CONFIG_FLAGS (drive)->no_playaudio12 = 1;
1934           CDROM_CONFIG_FLAGS (drive)->playmsf_uses_bcd = 1;
1935           return cdrom_play_lba_range_msf (drive, lba_start, lba_end, reqbuf);
1936         }
1937 #endif  /* not STANDARD_ATAPI */
1938 
1939       /* Failed for some other reason.  Give up. */
1940       return stat;
1941     }
1942 }
1943 
1944 
1945 static
1946 int cdrom_get_toc_entry (ide_drive_t *drive, int track,
     /* [previous][next][first][last][top][bottom][index][help] */
1947                          struct atapi_toc_entry **ent,
1948                          struct atapi_request_sense *reqbuf)
1949 {
1950   int stat, ntracks;
1951   struct atapi_toc *toc;
1952 
1953   /* Make sure our saved TOC is valid. */
1954   stat = cdrom_read_toc (drive, reqbuf);
1955   if (stat) return stat;
1956 
1957   toc = drive->cdrom_info.toc;
1958 
1959   /* Check validity of requested track number. */
1960   ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1961   if (track == CDROM_LEADOUT)
1962     *ent = &toc->ent[ntracks];
1963   else if (track < toc->hdr.first_track ||
1964            track > toc->hdr.last_track)
1965     return -EINVAL;
1966   else
1967     *ent = &toc->ent[track - toc->hdr.first_track];
1968 
1969   return 0;
1970 }
1971 
1972 
1973 static int
1974 cdrom_read_block (ide_drive_t *drive, int format, int lba,
     /* [previous][next][first][last][top][bottom][index][help] */
1975                   char *buf, int buflen,
1976                   struct atapi_request_sense *reqbuf)
1977 {
1978   struct packet_command pc;
1979   struct atapi_request_sense my_reqbuf;
1980   int stat;
1981 
1982   if (reqbuf == NULL)
1983     reqbuf = &my_reqbuf;
1984 
1985   memset (&pc, 0, sizeof (pc));
1986   pc.sense_data = reqbuf;
1987 
1988   pc.buffer = buf;
1989   pc.buflen = buflen;
1990 
1991 #if ! STANDARD_ATAPI
1992   if (CDROM_CONFIG_FLAGS (drive)->old_readcd)
1993     pc.c[0] = 0xd4;
1994   else
1995 #endif  /* not STANDARD_ATAPI */
1996     pc.c[0] = READ_CD;
1997 
1998   pc.c[1] = (format << 2);
1999   *(int *)(&pc.c[2]) = htonl (lba);
2000   pc.c[8] = 1;  /* one block */
2001   pc.c[9] = 0x10;
2002 
2003   stat = cdrom_queue_packet_command (drive, &pc);
2004 
2005 #if ! STANDARD_ATAPI
2006   /* If the drive doesn't recognize the READ CD opcode, retry the command
2007      with an older opcode for that command. */
2008   if (stat && reqbuf->sense_key == ILLEGAL_REQUEST && reqbuf->asc == 0x20 &&
2009       CDROM_CONFIG_FLAGS (drive)->old_readcd == 0)
2010     {
2011       printk ("%s: Drive does not recognize READ_CD; trying opcode 0xd4\n",
2012               drive->name);
2013       CDROM_CONFIG_FLAGS (drive)->old_readcd = 1;
2014       return cdrom_read_block (drive, format, lba, buf, buflen, reqbuf);
2015     }
2016 #endif  /* not STANDARD_ATAPI */
2017 
2018   return stat;
2019 }
2020 
2021 
2022 int ide_cdrom_ioctl (ide_drive_t *drive, struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
2023                      struct file *file, unsigned int cmd, unsigned long arg)
2024 {
2025   switch (cmd)
2026     {
2027     case CDROMEJECT:
2028       {
2029         int stat;
2030 
2031         if (drive->usage > 1)
2032           return -EBUSY;
2033 
2034         stat = cdrom_lockdoor (drive, 0, NULL);
2035         if (stat) return stat;
2036 
2037         return cdrom_eject (drive, 0, NULL);
2038       }
2039 
2040     case CDROMEJECT_SW:
2041       {
2042         CDROM_STATE_FLAGS (drive)->eject_on_close = arg;
2043         return 0;
2044       }
2045 
2046     case CDROMPAUSE:
2047       return cdrom_pause (drive, 1, NULL);
2048 
2049     case CDROMRESUME:
2050       return cdrom_pause (drive, 0, NULL);
2051 
2052     case CDROMSTART:
2053       return cdrom_startstop (drive, 1, NULL);
2054 
2055     case CDROMSTOP:
2056       {
2057         int stat;
2058 
2059         stat = cdrom_startstop (drive, 0, NULL);
2060         if (stat) return stat;
2061         /* pit says the Dolphin needs this. */
2062         return cdrom_eject (drive, 1, NULL);
2063       }
2064 
2065     case CDROMPLAYMSF:
2066       {
2067         struct cdrom_msf msf;
2068         int stat, lba_start, lba_end;
2069 
2070         stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
2071         if (stat) return stat;
2072 
2073         memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
2074 
2075         lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
2076                                 msf.cdmsf_frame0);
2077         lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
2078                               msf.cdmsf_frame1) + 1;
2079 
2080         if (lba_end <= lba_start) return -EINVAL;
2081 
2082         return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2083       }
2084 
2085     /* Like just about every other Linux cdrom driver, we ignore the
2086        index part of the request here. */
2087     case CDROMPLAYTRKIND:
2088       {
2089         int stat, lba_start, lba_end;
2090         struct cdrom_ti ti;
2091         struct atapi_toc_entry *first_toc, *last_toc;
2092 
2093         stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
2094         if (stat) return stat;
2095 
2096         memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
2097 
2098         stat = cdrom_get_toc_entry (drive, ti.cdti_trk0, &first_toc, NULL);
2099         if (stat) return stat;
2100         stat = cdrom_get_toc_entry (drive, ti.cdti_trk1, &last_toc, NULL);
2101         if (stat) return stat;
2102 
2103         if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
2104         lba_start = first_toc->addr.lba;
2105         lba_end   = last_toc->addr.lba;
2106 
2107         if (lba_end <= lba_start) return -EINVAL;
2108 
2109         return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2110       }
2111 
2112     case CDROMREADTOCHDR:
2113       {
2114         int stat;
2115         struct cdrom_tochdr tochdr;
2116         struct atapi_toc *toc;
2117 
2118         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tochdr));
2119         if (stat) return stat;
2120 
2121         /* Make sure our saved TOC is valid. */
2122         stat = cdrom_read_toc (drive, NULL);
2123         if (stat) return stat;
2124 
2125         toc = drive->cdrom_info.toc;
2126         tochdr.cdth_trk0 = toc->hdr.first_track;
2127         tochdr.cdth_trk1 = toc->hdr.last_track;
2128 
2129         memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
2130 
2131         return stat;
2132       }
2133 
2134     case CDROMREADTOCENTRY:
2135       {
2136         int stat;
2137         struct cdrom_tocentry tocentry;
2138         struct atapi_toc_entry *toce;
2139 
2140         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (tocentry));
2141         if (stat) return stat;
2142         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tocentry));
2143         if (stat) return stat;
2144 
2145         memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
2146 
2147         stat = cdrom_get_toc_entry (drive, tocentry.cdte_track, &toce, NULL);
2148         if (stat) return stat;
2149 
2150         tocentry.cdte_ctrl = toce->control;
2151         tocentry.cdte_adr  = toce->adr;
2152 
2153         if (tocentry.cdte_format == CDROM_MSF)
2154           {
2155             /* convert to MSF */
2156             lba_to_msf (toce->addr.lba,
2157                         &tocentry.cdte_addr.msf.minute,
2158                         &tocentry.cdte_addr.msf.second,
2159                         &tocentry.cdte_addr.msf.frame);
2160           }
2161         else
2162           tocentry.cdte_addr.lba = toce->addr.lba;
2163 
2164         memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
2165 
2166         return stat;
2167       }
2168 
2169     case CDROMSUBCHNL:
2170       {
2171         struct atapi_cdrom_subchnl scbuf;
2172         int stat, abs_lba, rel_lba;
2173         struct cdrom_subchnl subchnl;
2174 
2175         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (subchnl));
2176         if (stat) return stat;
2177         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (subchnl));
2178         if (stat) return stat;
2179 
2180         memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
2181 
2182         stat = cdrom_read_subchannel (drive, (char *)&scbuf, sizeof (scbuf),
2183                                       NULL);
2184         if (stat) return stat;
2185 
2186 #if ! STANDARD_ATAPI
2187         if (CDROM_CONFIG_FLAGS (drive)->vertos_lossage)
2188           {
2189             abs_lba = msf_to_lba (bcd2bin (scbuf.acdsc_absaddr.msf.minute),
2190                                   bcd2bin (scbuf.acdsc_absaddr.msf.second),
2191                                   bcd2bin (scbuf.acdsc_absaddr.msf.frame));
2192             rel_lba = msf_to_lba (bcd2bin (scbuf.acdsc_reladdr.msf.minute),
2193                                   bcd2bin (scbuf.acdsc_reladdr.msf.second),
2194                                   bcd2bin (scbuf.acdsc_reladdr.msf.frame));
2195             scbuf.acdsc_trk = bcd2bin (scbuf.acdsc_trk);
2196           }
2197         else
2198 #endif /* not STANDARD_ATAPI */
2199           {
2200             abs_lba = ntohl (scbuf.acdsc_absaddr.lba);
2201             rel_lba = ntohl (scbuf.acdsc_reladdr.lba);
2202           }
2203 
2204         if (subchnl.cdsc_format == CDROM_MSF)
2205           {
2206             lba_to_msf (abs_lba,
2207                         &subchnl.cdsc_absaddr.msf.minute,
2208                         &subchnl.cdsc_absaddr.msf.second,
2209                         &subchnl.cdsc_absaddr.msf.frame);
2210             lba_to_msf (rel_lba,
2211                         &subchnl.cdsc_reladdr.msf.minute,
2212                         &subchnl.cdsc_reladdr.msf.second,
2213                         &subchnl.cdsc_reladdr.msf.frame);
2214           }
2215         else
2216           {
2217             subchnl.cdsc_absaddr.lba = abs_lba;
2218             subchnl.cdsc_reladdr.lba = rel_lba;
2219           }
2220 
2221         subchnl.cdsc_audiostatus = scbuf.acdsc_audiostatus;
2222         subchnl.cdsc_ctrl = scbuf.acdsc_ctrl;
2223         subchnl.cdsc_trk  = scbuf.acdsc_trk;
2224         subchnl.cdsc_ind  = scbuf.acdsc_ind;
2225 
2226         memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
2227 
2228         return stat;
2229       }
2230 
2231     case CDROMVOLCTRL:
2232       {
2233         struct cdrom_volctrl volctrl;
2234         char buffer[24], mask[24];
2235         int stat;
2236 
2237         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (volctrl));
2238         if (stat) return stat;
2239         memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
2240 
2241         stat = cdrom_mode_sense (drive, 0x0e, 0, buffer, sizeof (buffer),NULL);
2242         if (stat) return stat;
2243         stat = cdrom_mode_sense (drive, 0x0e, 1, mask  , sizeof (buffer),NULL);
2244         if (stat) return stat;
2245 
2246         buffer[1] = buffer[2] = 0;
2247 
2248         buffer[17] = volctrl.channel0 & mask[17];
2249         buffer[19] = volctrl.channel1 & mask[19];
2250         buffer[21] = volctrl.channel2 & mask[21];
2251         buffer[23] = volctrl.channel3 & mask[23];
2252 
2253         return cdrom_mode_select (drive, 0x0e, buffer, sizeof (buffer), NULL);
2254       }
2255 
2256     case CDROMVOLREAD:
2257       {
2258         struct cdrom_volctrl volctrl;
2259         char buffer[24];
2260         int stat;
2261 
2262         stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (volctrl));
2263         if (stat) return stat;
2264 
2265         stat = cdrom_mode_sense (drive, 0x0e, 0, buffer, sizeof (buffer), NULL);
2266         if (stat) return stat;
2267 
2268         volctrl.channel0 = buffer[17];
2269         volctrl.channel1 = buffer[19];
2270         volctrl.channel2 = buffer[21];
2271         volctrl.channel3 = buffer[23];
2272 
2273         memcpy_tofs ((void *) arg, &volctrl, sizeof (volctrl));
2274 
2275         return 0;
2276       }
2277 
2278     case CDROMMULTISESSION:
2279       {
2280         struct cdrom_multisession ms_info;
2281         struct atapi_toc *toc;
2282         int stat;
2283 
2284         stat = verify_area (VERIFY_READ,  (void *)arg, sizeof (ms_info));
2285         if (stat) return stat;
2286         stat = verify_area (VERIFY_WRITE, (void *)arg, sizeof (ms_info));
2287         if (stat) return stat;
2288 
2289         memcpy_fromfs (&ms_info, (void *)arg, sizeof (ms_info));
2290 
2291         /* Make sure the TOC information is valid. */
2292         stat = cdrom_read_toc (drive, NULL);
2293         if (stat) return stat;
2294 
2295         toc = drive->cdrom_info.toc;
2296 
2297         if (ms_info.addr_format == CDROM_MSF)
2298           lba_to_msf (toc->last_session_lba,
2299                       &ms_info.addr.msf.minute,
2300                       &ms_info.addr.msf.second,
2301                       &ms_info.addr.msf.frame);
2302 
2303         else if (ms_info.addr_format == CDROM_LBA)
2304           ms_info.addr.lba = toc->last_session_lba;
2305 
2306         else
2307           return -EINVAL;
2308 
2309         ms_info.xa_flag = toc->xa_flag;
2310 
2311         memcpy_tofs ((void *)arg, &ms_info, sizeof (ms_info));
2312 
2313         return 0;
2314       }
2315 
2316     /* Read 2352 byte blocks from audio tracks. */
2317     case CDROMREADAUDIO:
2318       {
2319         int stat, lba;
2320         struct atapi_toc *toc;
2321         struct cdrom_read_audio ra;
2322         char buf[CD_FRAMESIZE_RAW];
2323 
2324         /* Make sure the TOC is up to date. */
2325         stat = cdrom_read_toc (drive, NULL);
2326         if (stat) return stat;
2327 
2328         toc = drive->cdrom_info.toc;
2329 
2330         stat = verify_area (VERIFY_READ, (char *)arg, sizeof (ra));
2331         if (stat) return stat;
2332 
2333         memcpy_fromfs (&ra, (void *)arg, sizeof (ra));
2334 
2335         if (ra.nframes < 0 || ra.nframes > toc->capacity)
2336           return -EINVAL;
2337         else if (ra.nframes == 0)
2338           return 0;
2339 
2340         stat = verify_area (VERIFY_WRITE, (char *)ra.buf,
2341                                           ra.nframes * CD_FRAMESIZE_RAW);
2342         if (stat) return stat;
2343 
2344         if (ra.addr_format == CDROM_MSF)
2345           lba = msf_to_lba (ra.addr.msf.minute, ra.addr.msf.second,
2346                             ra.addr.msf.frame);
2347         
2348         else if (ra.addr_format == CDROM_LBA)
2349           lba = ra.addr.lba;
2350 
2351         else
2352           return -EINVAL;
2353 
2354         if (lba < 0 || lba >= toc->capacity)
2355           return -EINVAL;
2356 
2357         while (ra.nframes > 0)
2358           {
2359             stat = cdrom_read_block (drive, 1, lba, buf,
2360                                      CD_FRAMESIZE_RAW, NULL);
2361             if (stat) return stat;
2362             memcpy_tofs (ra.buf, buf, CD_FRAMESIZE_RAW);
2363             ra.buf += CD_FRAMESIZE_RAW;
2364             --ra.nframes;
2365             ++lba;
2366           }
2367 
2368         return 0;
2369       }
2370 
2371     case CDROMREADMODE1:
2372     case CDROMREADMODE2:
2373       {
2374         struct cdrom_msf msf;
2375         int blocksize, format, stat, lba;
2376         struct atapi_toc *toc;
2377         char buf[CD_FRAMESIZE_RAW0];
2378 
2379         if (cmd == CDROMREADMODE1)
2380           {
2381             blocksize = CD_FRAMESIZE;
2382             format = 2;
2383           }
2384         else
2385           {
2386             blocksize = CD_FRAMESIZE_RAW0;
2387             format = 3;
2388           }
2389 
2390         stat = verify_area (VERIFY_READ, (char *)arg, sizeof (msf));
2391         if (stat) return stat;
2392         stat = verify_area (VERIFY_WRITE, (char *)arg, blocksize);
2393         if (stat) return stat;
2394 
2395         memcpy_fromfs (&msf, (void *)arg, sizeof (msf));
2396 
2397         lba = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0);
2398         
2399         /* Make sure the TOC is up to date. */
2400         stat = cdrom_read_toc (drive, NULL);
2401         if (stat) return stat;
2402 
2403         toc = drive->cdrom_info.toc;
2404 
2405         if (lba < 0 || lba >= toc->capacity)
2406           return -EINVAL;
2407 
2408         stat = cdrom_read_block (drive, format, lba, buf, blocksize, NULL);
2409         if (stat) return stat;
2410 
2411         memcpy_tofs ((char *)arg, buf, blocksize);
2412         return 0;
2413       }
2414 
2415 #if 0 /* Doesn't work reliably yet. */
2416     case CDROMRESET:
2417       {
2418         struct request req;
2419         ide_init_drive_cmd (&req);
2420         req.cmd = RESET_DRIVE_COMMAND;
2421         return ide_do_drive_cmd (drive, &req, ide_wait);
2422       }
2423 #endif
2424 
2425  
2426 #ifdef TEST
2427     case 0x1234:
2428       {
2429         int stat;
2430         struct packet_command pc;
2431         int len, lena;
2432 
2433         memset (&pc, 0, sizeof (pc));
2434 
2435         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
2436         if (stat) return stat;
2437         memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
2438         arg += sizeof (pc.c);
2439 
2440         stat = verify_area (VERIFY_READ, (void *) arg, sizeof (len));
2441         if (stat) return stat;
2442         memcpy_fromfs (&len, (void *) arg , sizeof (len));
2443         arg += sizeof (len);
2444 
2445         if (len > 0) {
2446           stat = verify_area (VERIFY_WRITE, (void *) arg, len);
2447           if (stat) return stat;
2448         }
2449 
2450         lena = len;
2451         if (lena  < 0) lena = 0;
2452 
2453         {
2454           char buf[lena];
2455           if (len > 0) {
2456             pc.buflen = len;
2457             pc.buffer = buf;
2458           }
2459 
2460           stat = cdrom_queue_packet_command (drive, &pc);
2461 
2462           if (len > 0)
2463             memcpy_tofs ((void *)arg, buf, len);
2464         }
2465 
2466         return stat;
2467       }
2468 #endif
2469 
2470     default:
2471       return -EPERM;
2472     }
2473 
2474 }
2475 
2476 
2477 
2478 /****************************************************************************
2479  * Other driver requests (open, close, check media change).
2480  */
2481 
2482 int ide_cdrom_check_media_change (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2483 {
2484   int retval;
2485 
2486   (void) cdrom_check_status (drive, NULL);
2487 
2488   retval = CDROM_STATE_FLAGS (drive)->media_changed;
2489   CDROM_STATE_FLAGS (drive)->media_changed = 0;
2490 
2491   return retval;
2492 }
2493 
2494 
2495 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2496 {
2497   /* no write access */
2498   if (fp->f_mode & 2)
2499     {
2500       --drive->usage;
2501       return -EROFS;
2502     }
2503 
2504   /* If this is the first open, check the drive status. */
2505   if (drive->usage == 1)
2506     {
2507       int stat;
2508       struct atapi_request_sense my_reqbuf;
2509       my_reqbuf.sense_key = 0;
2510 
2511       /* Get the drive status. */
2512       stat = cdrom_check_status (drive, &my_reqbuf);
2513 
2514       /* If the tray is open, try to close it. */
2515       if (stat && my_reqbuf.sense_key == NOT_READY)
2516         {
2517           cdrom_eject (drive, 1, &my_reqbuf);
2518           stat = cdrom_check_status (drive, &my_reqbuf);
2519         }
2520 
2521       /* Return an error if there are still problems. */
2522       if (stat && my_reqbuf.sense_key != UNIT_ATTENTION)
2523         {
2524           --drive->usage;
2525           return -ENXIO;
2526         }
2527 
2528       /* Now lock the door. */
2529       (void) cdrom_lockdoor (drive, 1, &my_reqbuf);
2530 
2531       /* And try to read the TOC information now. */
2532       (void) cdrom_read_toc (drive, &my_reqbuf);
2533     }
2534 
2535   return 0;
2536 }
2537 
2538 
2539 /*
2540  * Close down the device.  Invalidate all cached blocks.
2541  */
2542 
2543 void ide_cdrom_release (struct inode *inode, struct file *file, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2544 {
2545   if (drive->usage == 0)
2546     {
2547       invalidate_buffers (inode->i_rdev);
2548 
2549       /* Unlock the door. */
2550       (void) cdrom_lockdoor (drive, 0, NULL);
2551 
2552       /* Do an eject if we were requested to do so. */
2553       if (CDROM_STATE_FLAGS (drive)->eject_on_close)
2554         (void) cdrom_eject (drive, 0, NULL);
2555     }
2556 }
2557 
2558 
2559 
2560 /****************************************************************************
2561  * Device initialization.
2562  */
2563 
2564 void ide_cdrom_setup (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2565 {
2566   blksize_size[HWIF(drive)->major][drive->select.b.unit << PARTN_BITS] = CD_FRAMESIZE;
2567 
2568   drive->special.all = 0;
2569   drive->ready_stat = 0;
2570 
2571   CDROM_STATE_FLAGS (drive)->media_changed = 0;
2572   CDROM_STATE_FLAGS (drive)->toc_valid     = 0;
2573   CDROM_STATE_FLAGS (drive)->door_locked   = 0;
2574 
2575   /* Turn this off by default, since many people don't like it. */
2576   CDROM_STATE_FLAGS (drive)->eject_on_close= 0;
2577 
2578   CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2579   CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2580     ((drive->id->config & 0x0060) == 0x20);
2581 
2582 #if ! STANDARD_ATAPI
2583   CDROM_CONFIG_FLAGS (drive)->no_playaudio12 = 0;
2584   CDROM_CONFIG_FLAGS (drive)->old_readcd = 0;
2585   CDROM_CONFIG_FLAGS (drive)->no_lba_toc = 0;
2586   CDROM_CONFIG_FLAGS (drive)->playmsf_uses_bcd = 0;
2587   CDROM_CONFIG_FLAGS (drive)->vertos_lossage = 0;
2588 
2589   /* Accommodate some broken drives... */
2590   if (strcmp (drive->id->model, "CD220E") == 0 ||
2591       strcmp (drive->id->model, "CD")     == 0)        /* Creative Labs */
2592     CDROM_CONFIG_FLAGS (drive)->no_lba_toc = 1;
2593 
2594   else if (strcmp (drive->id->model, "TO-ICSLYAL") == 0 ||  /* Acer CD525E */
2595            strcmp (drive->id->model, "OTI-SCYLLA") == 0)
2596     CDROM_CONFIG_FLAGS (drive)->no_lba_toc = 1;
2597 
2598   /* I don't know who makes this.
2599      Francesco Messineo <sidera@ccii.unipi.it> says this one's broken too. */
2600   else if (strcmp (drive->id->model, "DCI-2S10") == 0)
2601     CDROM_CONFIG_FLAGS (drive)->no_lba_toc = 1;
2602 
2603   else if (strcmp (drive->id->model, "CDA26803I SE") == 0) /* Aztech */
2604     {
2605       CDROM_CONFIG_FLAGS (drive)->no_lba_toc = 1;
2606 
2607       /* This drive _also_ does not implement PLAYAUDIO12 correctly. */
2608       CDROM_CONFIG_FLAGS (drive)->no_playaudio12 = 1;
2609     }
2610 
2611   else if (strcmp (drive->id->model, "V003S0DS") == 0 ||  /* Vertos */
2612            strcmp (drive->id->model, "0V300SSD") == 0)
2613     {
2614       CDROM_CONFIG_FLAGS (drive)->vertos_lossage = 1;
2615       CDROM_CONFIG_FLAGS (drive)->playmsf_uses_bcd = 1;
2616       CDROM_CONFIG_FLAGS (drive)->no_lba_toc = 1;
2617     }
2618 
2619   else if (strcmp (drive->id->model, "V004E0DT") == 0 ||
2620            strcmp (drive->id->model, "0V400ETD") == 0)
2621     CDROM_CONFIG_FLAGS (drive)->no_lba_toc = 1;
2622 #endif  /* not STANDARD_ATAPI */
2623 
2624   drive->cdrom_info.toc               = NULL;
2625   drive->cdrom_info.sector_buffer     = NULL;
2626   drive->cdrom_info.sector_buffered   = 0;
2627   drive->cdrom_info.nsectors_buffered = 0;
2628 }
2629 
2630 
2631 
2632 /*
2633  * TODO:
2634  *  CDROM_GET_UPC
2635  *  CDROMRESET
2636  *  Lock the door when a read request completes successfully and the
2637  *   door is not already locked.  Also try to reorganize to reduce
2638  *   duplicated functionality between read and ioctl paths?
2639  *  Establish interfaces for an IDE port driver, and break out the cdrom
2640  *   code into a loadable module.
2641  *  Support changers.
2642  *  Write some real documentation.
2643  */

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