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

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