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

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