root/drivers/cdrom/cdu31a.c

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

DEFINITIONS

This source file includes following definitions.
  1. scd_disk_change
  2. enable_interrupts
  3. disable_interrupts
  4. sony_sleep
  5. is_attention
  6. is_busy
  7. is_data_ready
  8. is_data_requested
  9. is_result_ready
  10. is_param_write_rdy
  11. is_result_reg_not_empty
  12. reset_drive
  13. clear_attention
  14. clear_result_ready
  15. clear_data_ready
  16. clear_param_reg
  17. read_status_register
  18. read_result_register
  19. read_data_register
  20. write_param
  21. write_cmd
  22. cdu31a_interrupt
  23. set_drive_params
  24. restart_on_error
  25. write_params
  26. get_result
  27. do_sony_cd_cmd
  28. handle_sony_cd_attention
  29. int_to_bcd
  30. bcd_to_int
  31. log_to_msf
  32. msf_to_log
  33. size_to_buf
  34. start_request
  35. abort_read
  36. handle_abort_timeout
  37. input_data
  38. read_data_block
  39. do_cdu31a_request
  40. mcovlp
  41. sony_get_toc
  42. find_track
  43. read_subcode
  44. sony_get_subchnl_info
  45. read_audio_data
  46. read_audio
  47. do_sony_cd_cmd_chk
  48. scd_ioctl
  49. scd_open
  50. scd_release
  51. get_drive_configuration
  52. cdu31a_setup
  53. cdu31a_init
  54. cleanup_module

   1 /*
   2 * Sony CDU-31A CDROM interface device driver.
   3 *
   4 * Corey Minyard (minyard@wf-rch.cirr.com)
   5 *
   6 * Colossians 3:17
   7 *
   8 * The Sony interface device driver handles Sony interface CDROM
   9 * drives and provides a complete block-level interface as well as an
  10 * ioctl() interface compatible with the Sun (as specified in
  11 * include/linux/cdrom.h).  With this interface, CDROMs can be
  12 * accessed and standard audio CDs can be played back normally.
  13 *
  14 * WARNING -     All autoprobes have been removed from the driver.
  15 *               You MUST configure the CDU31A via a LILO config
  16 *               at boot time or in lilo.conf.  I have the
  17 *               following in my lilo.conf:
  18 *
  19 *                append="cdu31a=0x1f88,0,PAS"
  20 *
  21 *               The first number is the I/O base address of the
  22 *               card.  The second is the interrupt (0 means none).
  23  *              The third should be "PAS" if on a Pro-Audio
  24  *              spectrum, or nothing if on something else.
  25  *
  26  * This interface is (unfortunately) a polled interface.  This is
  27  * because most Sony interfaces are set up with DMA and interrupts
  28  * disables.  Some (like mine) do not even have the capability to
  29  * handle interrupts or DMA.  For this reason you will see a lot of
  30  * the following:
  31  *
  32  *   retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
  33  *   while ((retry_count > jiffies) && (! <some condition to wait for))
  34  *   {
  35  *      while (handle_sony_cd_attention())
  36  *         ;
  37  *
  38  *      sony_sleep();
  39  *   }
  40  *   if (the condition not met)
  41  *   {
  42  *      return an error;
  43  *   }
  44  *
  45  * This ugly hack waits for something to happen, sleeping a little
  46  * between every try.  it also handles attentions, which are
  47  * asynchronous events from the drive informing the driver that a disk
  48  * has been inserted, removed, etc.
  49  *
  50  * NEWS FLASH - The driver now supports interrupts but they are
  51  * turned off by default.  Use of interrupts is highly encouraged, it
  52  * cuts CPU usage down to a reasonable level.  I had DMA in for a while
  53  * but PC DMA is just too slow.  Better to just insb() it.
  54  *
  55  * One thing about these drives: They talk in MSF (Minute Second Frame) format.
  56  * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
  57  * disk.  The funny thing is that these are sent to the drive in BCD, but the
  58  * interface wants to see them in decimal.  A lot of conversion goes on.
  59  *
  60  * DRIVER SPECIAL FEATURES
  61  * -----------------------
  62  *
  63  * This section describes features beyond the normal audio and CD-ROM
  64  * functions of the drive.
  65  *
  66  * 2048 byte buffer mode
  67  *
  68  * If a disk is mounted with -o block=2048, data is copied straight
  69  * from the drive data port to the buffer.  Otherwise, the readahead
  70  * buffer must be involved to hold the other 1K of data when a 1K
  71  * block operation is done.  Note that with 2048 byte blocks you
  72  * cannot execute files from the CD.
  73  *
  74  * XA compatibility
  75  *
  76  * The driver should support XA disks for both the CDU31A and CDU33A.
  77  * It does this transparently, the using program doesn't need to set it.
  78  *
  79  * Multi-Session
  80  *
  81  * A multi-session disk looks just like a normal disk to the user.
  82  * Just mount one normally, and all the data should be there.
  83  * A special thanks to Koen for help with this!
  84  * 
  85  * Raw sector I/O
  86  *
  87  * Using the CDROMREADAUDIO it is possible to read raw audio and data
  88  * tracks.  Both operations return 2352 bytes per sector.  On the data
  89  * tracks, the first 12 bytes is not returned by the drive and the value
  90  * of that data is indeterminate.
  91  *
  92  *
  93  *  Copyright (C) 1993  Corey Minyard
  94  *
  95  *  This program is free software; you can redistribute it and/or modify
  96  *  it under the terms of the GNU General Public License as published by
  97  *  the Free Software Foundation; either version 2 of the License, or
  98  *  (at your option) any later version.
  99  *
 100  *  This program is distributed in the hope that it will be useful,
 101  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 102  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 103  *  GNU General Public License for more details.
 104  *
 105  *  You should have received a copy of the GNU General Public License
 106  *  along with this program; if not, write to the Free Software
 107  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 108  *
 109  */
 110 
 111 /*
 112  *
 113  * Setting up the Sony CDU31A/CDU33A drive interface card.  If
 114  * You have another card, you are on your own.
 115  * 
 116  *      +----------+-----------------+----------------------+
 117  *      |  JP1     |  34 Pin Conn    |                      |
 118  *      |  JP2     +-----------------+                      |
 119  *      |  JP3                                              |
 120  *      |  JP4                                              |
 121  *      |                                                   +--+
 122  *      |                                                   |  +-+
 123  *      |                                                   |  | |  External
 124  *      |                                                   |  | |  Connector
 125  *      |                                                   |  | |
 126  *      |                                                   |  +-+
 127  *      |                                                   +--+
 128  *      |                                                   |
 129  *      |                                          +--------+
 130  *      |                                          |
 131  *      +------------------------------------------+
 132  * 
 133  *    JP1 sets the Base Address, using the following settings:
 134  * 
 135  *      Address         Pin 1           Pin 2
 136  *      -------         -----           -----
 137  *      0x320           Short           Short
 138  *      0x330           Short           Open
 139  *      0x340           Open            Short
 140  *      0x360           Open            Open
 141  * 
 142  *    JP2 and JP3 configure the DMA channel; they must be set the same.
 143  * 
 144  *      DMA             Pin 1           Pin 2           Pin 3
 145  *      ---             -----           -----           -----
 146  *      1               On              Off             On
 147  *      2               Off             On              Off
 148  *      3               Off             Off             On
 149  * 
 150  *    JP4 Configures the IRQ:
 151  * 
 152  *      IRQ     Pin 1           Pin 2           Pin 3           Pin 4
 153  *      ---     -----           -----           -----           -----
 154  *      3       Off             Off             On              Off
 155  *      4       Off             Off*            Off             On
 156  *      5       On              Off             Off             Off
 157  *      6       Off             On              Off             Off
 158  * 
 159  *              * The documentation states to set this for interrupt
 160  *                4, but I think that is a mistake.
 161  *
 162  *  It probably a little late to be adding a history, but I guess I
 163  *  will start.
 164  *
 165  *  10/24/95 - Added support for disabling the eject button when the
 166  *             drive is open.  Note that there is a small problem
 167  *             still here, if the eject button is pushed while the
 168  *             drive light is flashing, the drive will return a bad
 169  *             status and be reset.  It recovers, though.
 170  */
 171 
 172 #include <linux/major.h>
 173 #include <linux/config.h>
 174 
 175 #ifdef MODULE
 176 # include <linux/module.h>
 177 # include <linux/version.h>
 178 char kernel_version[]= UTS_RELEASE;
 179 #endif
 180 
 181 #include <linux/errno.h>
 182 #include <linux/signal.h>
 183 #include <linux/sched.h>
 184 #include <linux/timer.h>
 185 #include <linux/fs.h>
 186 #include <linux/kernel.h>
 187 #include <linux/hdreg.h>
 188 #include <linux/genhd.h>
 189 #include <linux/ioport.h>
 190 #include <linux/string.h>
 191 #include <linux/malloc.h>
 192 
 193 #include <asm/system.h>
 194 #include <asm/io.h>
 195 #include <asm/segment.h>
 196 #include <asm/dma.h>
 197 
 198 #include <linux/cdrom.h>
 199 #include <linux/cdu31a.h>
 200 
 201 #define MAJOR_NR CDU31A_CDROM_MAJOR
 202 #include <linux/blk.h>
 203 
 204 #define DEBUG 0
 205 
 206 #define CDU31A_READAHEAD 128  /* 128 sector, 64kB, 32 reads read-ahead */
 207 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
 208 
 209 /* Define the following if you have data corruption problems. */
 210 #undef SONY_POLL_EACH_BYTE
 211 
 212 /*
 213 ** Edit the following data to change interrupts, DMA channels, etc.
 214 ** Default is polled and no DMA.  DMA is not recommended for double-speed
 215 ** drives.
 216 */
 217 static struct
 218 {
 219    unsigned short base;         /* I/O Base Address */
 220    short          int_num;      /* Interrupt Number (-1 means scan for it,
 221                                    0 means don't use) */
 222 } cdu31a_addresses[] =
 223 {
 224 #if 0   /* No autoconfig any more. See Note at beginning
 225            of this file. */
 226    { 0x340,     0 },    /* Standard configuration Sony Interface */
 227    { 0x1f88,    0 },    /* Fusion CD-16 */
 228    { 0x230,     0 },    /* SoundBlaster 16 card */
 229    { 0x360,     0 },    /* Secondary standard Sony Interface */
 230    { 0x320,     0 },    /* Secondary standard Sony Interface */
 231    { 0x330,     0 },    /* Secondary standard Sony Interface */
 232    { 0x634,     0 },    /* Sound FX SC400 */
 233    { 0x654,     0 },    /* Sound FX SC400 */
 234 #endif
 235    { 0 }
 236 };
 237 
 238 static int handle_sony_cd_attention(void);
 239 static int read_subcode(void);
 240 static void sony_get_toc(void);
 241 static int scd_open(struct inode *inode, struct file *filp);
 242 static void do_sony_cd_cmd(unsigned char cmd,
 243                            unsigned char *params,
 244                            unsigned int num_params,
 245                            unsigned char *result_buffer,
 246                            unsigned int *result_size);
 247 static void size_to_buf(unsigned int size,
 248                         unsigned char *buf);
 249 
 250 /* Parameters for the read-ahead. */
 251 static unsigned int sony_next_block;      /* Next 512 byte block offset */
 252 static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
 253                                              in the current read command. */
 254 
 255 
 256 /* The base I/O address of the Sony Interface.  This is a variable (not a
 257    #define) so it can be easily changed via some future ioctl() */
 258 static unsigned int cdu31a_port = 0;
 259 
 260 /*
 261  * The following are I/O addresses of the various registers for the drive.  The
 262  * comment for the base address also applies here.
 263  */
 264 static volatile unsigned short sony_cd_cmd_reg;
 265 static volatile unsigned short sony_cd_param_reg;
 266 static volatile unsigned short sony_cd_write_reg;
 267 static volatile unsigned short sony_cd_control_reg;
 268 static volatile unsigned short sony_cd_status_reg;
 269 static volatile unsigned short sony_cd_result_reg;
 270 static volatile unsigned short sony_cd_read_reg;
 271 static volatile unsigned short sony_cd_fifost_reg;
 272 
 273 
 274 static int sony_spun_up = 0;               /* Has the drive been spun up? */
 275 
 276 static int sony_xa_mode = 0;               /* Is an XA disk in the drive
 277                                               and the drive a CDU31A? */
 278 
 279 static int sony_raw_data_mode = 1;         /* 1 if data tracks, 0 if audio.
 280                                               For raw data reads. */
 281 
 282 static unsigned int sony_usage = 0;        /* How many processes have the
 283                                               drive open. */
 284 
 285 static int sony_pas_init = 0;              /* Initialize the Pro-Audio
 286                                               Spectrum card? */
 287 
 288 static struct s_sony_session_toc sony_toc;  /* Holds the
 289                                                table of
 290                                                contents. */
 291 
 292 static int sony_toc_read = 0;              /* Has the TOC been read for
 293                                               the drive? */
 294 
 295 static struct s_sony_subcode last_sony_subcode; /* Points to the last
 296                                                    subcode address read */
 297 
 298 static volatile int sony_inuse = 0;  /* Is the drive in use?  Only one operation
 299                                         at a time allowed */
 300 
 301 static struct wait_queue * sony_wait = NULL;    /* Things waiting for the drive */
 302 
 303 static struct task_struct *has_cd_task = NULL;  /* The task that is currently
 304                                                    using the CDROM drive, or
 305                                                    NULL if none. */
 306 
 307 static int is_double_speed = 0; /* Is the drive a CDU33A? */
 308 
 309 static int is_auto_eject = 1;   /* Door has been locked? 1=No/0=Yes */
 310 
 311 /*
 312  * The audio status uses the values from read subchannel data as specified
 313  * in include/linux/cdrom.h.
 314  */
 315 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
 316 
 317 /*
 318  * The following are a hack for pausing and resuming audio play.  The drive
 319  * does not work as I would expect it, if you stop it then start it again,
 320  * the drive seeks back to the beginning and starts over.  This holds the
 321  * position during a pause so a resume can restart it.  It uses the
 322  * audio status variable above to tell if it is paused.
 323  */
 324 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
 325 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
 326 
 327 /* What IRQ is the drive using?  0 if none. */
 328 static int cdu31a_irq = 0;
 329 
 330 /* The interrupt handler will wake this queue up when it gets an
 331    interrupts. */
 332 static struct wait_queue *cdu31a_irq_wait = NULL;
 333 
 334 static int curr_control_reg = 0; /* Current value of the control register */
 335 
 336 /* A disk changed variable.  When a disk change is detected, it will
 337    all be set to TRUE.  As the upper layers ask for disk_changed status
 338    it will be cleared. */
 339 static char disk_changed;
 340 
 341 /* Variable for using the readahead buffer.  The readahead buffer
 342    is used for raw sector reads and for blocksizes that are smaller
 343    than 2048 bytes. */
 344 static char readahead_buffer[CD_FRAMESIZE_RAW];
 345 static int readahead_dataleft = 0;
 346 static int readahead_bad = 0;
 347 
 348 /* Used to time a short period to abort an operation after the
 349    drive has been idle for a while.  This keeps the light on
 350    the drive from flashing for very long. */
 351 static struct timer_list cdu31a_abort_timer;
 352 
 353 /* Marks if the timeout has started an abort read.  This is used
 354    on entry to the drive to tell the code to read out the status
 355    from the abort read. */
 356 static int abort_read_started = 0;
 357 
 358 
 359 /*
 360  * This routine returns 1 if the disk has been changed since the last
 361  * check or 0 if it hasn't.
 362  */
 363 static int
 364 scd_disk_change(kdev_t full_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 365 {
 366    int retval;
 367 
 368    retval = disk_changed;
 369    disk_changed = 0;
 370 
 371    return retval;
 372 }
 373 
 374 static inline void
 375 enable_interrupts(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377    curr_control_reg |= (  SONY_ATTN_INT_EN_BIT
 378                         | SONY_RES_RDY_INT_EN_BIT
 379                         | SONY_DATA_RDY_INT_EN_BIT);
 380    outb(curr_control_reg, sony_cd_control_reg);
 381 }
 382 
 383 static inline void
 384 disable_interrupts(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 {
 386    curr_control_reg &= ~(  SONY_ATTN_INT_EN_BIT
 387                          | SONY_RES_RDY_INT_EN_BIT
 388                          | SONY_DATA_RDY_INT_EN_BIT);
 389    outb(curr_control_reg, sony_cd_control_reg);
 390 }
 391 
 392 /*
 393  * Wait a little while (used for polling the drive).  If in initialization,
 394  * setting a timeout doesn't work, so just loop for a while.
 395  */
 396 static inline void
 397 sony_sleep(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 398 {
 399    unsigned long flags;
 400 
 401    if (cdu31a_irq <= 0)
 402    {
 403       current->state = TASK_INTERRUPTIBLE;
 404       current->timeout = jiffies;
 405       schedule();
 406    }
 407    else /* Interrupt driven */
 408    {
 409       save_flags(flags);
 410       cli();
 411       enable_interrupts();
 412       interruptible_sleep_on(&cdu31a_irq_wait);
 413       restore_flags(flags);
 414    }
 415 }
 416 
 417 
 418 /*
 419  * The following are convenience routine to read various status and set
 420  * various conditions in the drive.
 421  */
 422 static inline int
 423 is_attention(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 424 {
 425    return((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
 426 }
 427 
 428 static inline int
 429 is_busy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 430 {
 431    return((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
 432 }
 433 
 434 static inline int
 435 is_data_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 436 {
 437    return((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
 438 }
 439 
 440 static inline int
 441 is_data_requested(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 442 {
 443    return((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
 444 }
 445 
 446 static inline int
 447 is_result_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 448 {
 449    return((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
 450 }
 451 
 452 static inline int
 453 is_param_write_rdy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 454 {
 455    return((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
 456 }
 457 
 458 static inline int
 459 is_result_reg_not_empty(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 460 {
 461    return((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
 462 }
 463 
 464 static inline void
 465 reset_drive(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467    curr_control_reg = 0;
 468    readahead_dataleft = 0;
 469    sony_toc_read = 0;
 470    outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
 471 }
 472 
 473 static inline void
 474 clear_attention(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 475 {
 476    outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
 477 }
 478 
 479 static inline void
 480 clear_result_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 481 {
 482    outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
 483 }
 484 
 485 static inline void
 486 clear_data_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 487 {
 488    outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT, sony_cd_control_reg);
 489 }
 490 
 491 static inline void
 492 clear_param_reg(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 493 {
 494    outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
 495 }
 496 
 497 static inline unsigned char
 498 read_status_register(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 499 {
 500    return(inb(sony_cd_status_reg));
 501 }
 502 
 503 static inline unsigned char
 504 read_result_register(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 505 {
 506    return(inb(sony_cd_result_reg));
 507 }
 508 
 509 static inline unsigned char
 510 read_data_register(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 511 {
 512    return(inb(sony_cd_read_reg));
 513 }
 514 
 515 static inline void
 516 write_param(unsigned char param)
     /* [previous][next][first][last][top][bottom][index][help] */
 517 {
 518    outb(param, sony_cd_param_reg);
 519 }
 520 
 521 static inline void
 522 write_cmd(unsigned char cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 523 {
 524    outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT, sony_cd_control_reg);
 525    outb(cmd, sony_cd_cmd_reg);
 526 }
 527 
 528 static void
 529 cdu31a_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 530 {
 531    unsigned char val;
 532 
 533    if (abort_read_started)
 534    {
 535       /* We might be waiting for an abort to finish.  Don't
 536          disable interrupts yet, though, because we handle
 537          this one here. */
 538       /* Clear out the result registers. */
 539       while (is_result_reg_not_empty())
 540       {
 541          val = read_result_register();
 542       }
 543       clear_data_ready();
 544       clear_result_ready();
 545 
 546       /* Clear out the data */
 547       while (is_data_requested())
 548       {
 549          val = read_data_register();
 550       }
 551       abort_read_started = 0;
 552 
 553       /* If something was waiting, wake it up now. */
 554       if (cdu31a_irq_wait != NULL)
 555       {
 556          disable_interrupts();
 557          wake_up(&cdu31a_irq_wait);
 558       }
 559    }
 560    else if (cdu31a_irq_wait != NULL)
 561    {
 562       disable_interrupts();
 563       wake_up(&cdu31a_irq_wait);
 564    }
 565    else
 566    {
 567       disable_interrupts();
 568       printk("CDU31A: Got an interrupt but nothing was waiting\n");
 569    }
 570 }
 571 
 572 /*
 573  * Set the drive parameters so the drive will auto-spin-up when a
 574  * disk is inserted.
 575  */
 576 static void
 577 set_drive_params(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 578 {
 579    unsigned char res_reg[12];
 580    unsigned int res_size;
 581    unsigned char params[3];
 582 
 583 
 584    params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
 585    params[1] = 0x00; /* Never spin down the drive. */
 586    do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
 587                   params,
 588                   2,
 589                   res_reg,
 590                   &res_size);
 591    if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
 592    {
 593       printk("  Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
 594    }
 595 
 596    params[0] = SONY_SD_MECH_CONTROL;
 597    params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
 598 
 599    if (is_auto_eject) params[1] |= SONY_AUTO_EJECT_BIT;
 600    
 601    if (is_double_speed)
 602    {
 603       params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if 
 604                                              possible */
 605    }
 606    do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
 607                   params,
 608                   2,
 609                   res_reg,
 610                   &res_size);
 611    if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
 612    {
 613       printk("  Unable to set mechanical parameters: 0x%2.2x\n", res_reg[1]);
 614    }
 615 }
 616 
 617 /*
 618  * This code will reset the drive and attempt to restore sane parameters.
 619  */
 620 static void
 621 restart_on_error(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 622 {
 623    unsigned char res_reg[12];
 624    unsigned int res_size;
 625    unsigned int retry_count;
 626 
 627 
 628    printk("cdu31a: Resetting drive on error\n");
 629    reset_drive();
 630    retry_count = jiffies + SONY_RESET_TIMEOUT;
 631    while ((retry_count > jiffies) && (!is_attention()))
 632    {
 633       sony_sleep();
 634    }
 635    set_drive_params();
 636    do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
 637    if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
 638    {
 639       printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg[1]);
 640    }
 641 
 642    current->state = TASK_INTERRUPTIBLE;
 643    current->timeout = jiffies + 2*HZ;
 644    schedule();
 645 
 646    sony_get_toc();
 647 }
 648 
 649 /*
 650  * This routine writes data to the parameter register.  Since this should
 651  * happen fairly fast, it is polled with no OS waits between.
 652  */
 653 static int
 654 write_params(unsigned char *params,
     /* [previous][next][first][last][top][bottom][index][help] */
 655              int num_params)
 656 {
 657    unsigned int retry_count;
 658 
 659 
 660    retry_count = SONY_READY_RETRIES;
 661    while ((retry_count > 0) && (!is_param_write_rdy()))
 662    {
 663       retry_count--;
 664    }
 665    if (!is_param_write_rdy())
 666    {
 667       return -EIO;
 668    }
 669 
 670    while (num_params > 0)
 671    {
 672       write_param(*params);
 673       params++;
 674       num_params--;
 675    }
 676 
 677    return 0;
 678 }
 679 
 680 
 681 /*
 682  * The following reads data from the command result register.  It is a
 683  * fairly complex routine, all status info flows back through this
 684  * interface.  The algorithm is stolen directly from the flowcharts in
 685  * the drive manual.
 686  */
 687 static void
 688 get_result(unsigned char *result_buffer,
     /* [previous][next][first][last][top][bottom][index][help] */
 689            unsigned int *result_size)
 690 {
 691    unsigned char a, b;
 692    int i;
 693    unsigned int retry_count;
 694 
 695 
 696    while (handle_sony_cd_attention())
 697       ;
 698    /* Wait for the result data to be ready */
 699    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 700    while ((retry_count > jiffies) && (is_busy() || (!(is_result_ready()))))
 701    {
 702       sony_sleep();
 703 
 704       while (handle_sony_cd_attention())
 705          ;
 706    }
 707    if (is_busy() || (!(is_result_ready())))
 708    {
 709 #if DEBUG
 710       printk("CDU31A timeout out %d\n", __LINE__);
 711 #endif
 712       result_buffer[0] = 0x20;
 713       result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 714       *result_size = 2;
 715       return;
 716    }
 717 
 718    /*
 719     * Get the first two bytes.  This determines what else needs
 720     * to be done.
 721     */
 722    clear_result_ready();
 723    a = read_result_register();
 724    *result_buffer = a;
 725    result_buffer++;
 726 
 727    /* Check for block error status result. */
 728    if ((a & 0xf0) == 0x50)
 729    {
 730       *result_size = 1;
 731       return;
 732    }
 733 
 734    b = read_result_register();
 735    *result_buffer = b;
 736    result_buffer++;
 737    *result_size = 2;
 738 
 739    /*
 740     * 0x20 means an error occurred.  Byte 2 will have the error code.
 741     * Otherwise, the command succeeded, byte 2 will have the count of
 742     * how many more status bytes are coming.
 743     *
 744     * The result register can be read 10 bytes at a time, a wait for
 745     * result ready to be asserted must be done between every 10 bytes.
 746     */
 747    if ((a & 0xf0) != 0x20)
 748    {
 749       if (b > 8)
 750       {
 751          for (i=0; i<8; i++)
 752          {
 753             *result_buffer = read_result_register();
 754             result_buffer++;
 755             (*result_size)++;
 756          }
 757          b = b - 8;
 758 
 759          while (b > 10)
 760          {
 761             retry_count = SONY_READY_RETRIES;
 762             while ((retry_count > 0) && (!is_result_ready()))
 763             {
 764                retry_count--;
 765             }
 766             if (!is_result_ready())
 767             {
 768 #if DEBUG
 769                printk("CDU31A timeout out %d\n", __LINE__);
 770 #endif
 771                result_buffer[0] = 0x20;
 772                result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 773                *result_size = 2;
 774                return;
 775             }
 776 
 777             clear_result_ready();
 778                                 
 779             for (i=0; i<10; i++)
 780             {
 781                *result_buffer = read_result_register();
 782                result_buffer++;
 783                (*result_size)++;
 784             }
 785             b = b - 10;
 786          }
 787 
 788          if (b > 0)
 789          {
 790             retry_count = SONY_READY_RETRIES;
 791             while ((retry_count > 0) && (!is_result_ready()))
 792             {
 793                retry_count--;
 794             }
 795             if (!is_result_ready())
 796             {
 797 #if DEBUG
 798                printk("CDU31A timeout out %d\n", __LINE__);
 799 #endif
 800                result_buffer[0] = 0x20;
 801                result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 802                *result_size = 2;
 803                return;
 804             }
 805          }
 806       }
 807 
 808       while (b > 0)
 809       {
 810          *result_buffer = read_result_register();
 811          result_buffer++;
 812          (*result_size)++;
 813          b--;
 814       }
 815    }
 816 }
 817 
 818 /*
 819  * Do a command that does not involve data transfer.  This routine must
 820  * be re-entrant from the same task to support being called from the
 821  * data operation code when an error occurs.
 822  */
 823 static void
 824 do_sony_cd_cmd(unsigned char cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
 825                unsigned char *params,
 826                unsigned int num_params,
 827                unsigned char *result_buffer,
 828                unsigned int *result_size)
 829 {
 830    unsigned int retry_count;
 831    int num_retries;
 832    int recursive_call;
 833    unsigned long flags;
 834 
 835 
 836    save_flags(flags);
 837    cli();
 838    if (current != has_cd_task) /* Allow recursive calls to this routine */
 839    {
 840       while (sony_inuse)
 841       {
 842          interruptible_sleep_on(&sony_wait);
 843          if (current->signal & ~current->blocked)
 844          {
 845             result_buffer[0] = 0x20;
 846             result_buffer[1] = SONY_SIGNAL_OP_ERR;
 847             *result_size = 2;
 848             restore_flags(flags);
 849             return;
 850          }
 851       }
 852       sony_inuse = 1;
 853       has_cd_task = current;
 854       recursive_call = 0;
 855    }
 856    else
 857    {
 858       recursive_call = 1;
 859    }
 860 
 861    num_retries = 0;
 862 retry_cd_operation:
 863 
 864    while (handle_sony_cd_attention())
 865       ;
 866 
 867    sti();
 868    
 869    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 870    while ((retry_count > jiffies) && (is_busy()))
 871    {
 872       sony_sleep();
 873       
 874       while (handle_sony_cd_attention())
 875          ;
 876    }
 877    if (is_busy())
 878    {
 879 #if DEBUG
 880       printk("CDU31A timeout out %d\n", __LINE__);
 881 #endif
 882       result_buffer[0] = 0x20;
 883       result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 884       *result_size = 2;
 885    }
 886    else
 887    {
 888       clear_result_ready();
 889       clear_param_reg();
 890 
 891       write_params(params, num_params);
 892       write_cmd(cmd);
 893 
 894       get_result(result_buffer, result_size);
 895    }
 896 
 897    if (   ((result_buffer[0] & 0xf0) == 0x20)
 898        && (num_retries < MAX_CDU31A_RETRIES))
 899    {
 900       num_retries++;
 901       current->state = TASK_INTERRUPTIBLE;
 902       current->timeout = jiffies + HZ/10; /* Wait .1 seconds on retries */
 903       schedule();
 904       goto retry_cd_operation;
 905    }
 906 
 907    if (!recursive_call)
 908    {
 909       has_cd_task = NULL;
 910       sony_inuse = 0;
 911       wake_up_interruptible(&sony_wait);
 912    }
 913 
 914    restore_flags(flags);
 915 }
 916 
 917 
 918 /*
 919  * Handle an attention from the drive.  This will return 1 if it found one
 920  * or 0 if not (if one is found, the caller might want to call again).
 921  *
 922  * This routine counts the number of consecutive times it is called
 923  * (since this is always called from a while loop until it returns
 924  * a 0), and returns a 0 if it happens too many times.  This will help
 925  * prevent a lockup.
 926  */
 927 static int
 928 handle_sony_cd_attention(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 929 {
 930    unsigned char atten_code;
 931    static int num_consecutive_attentions = 0;
 932    volatile int val;
 933 
 934 
 935    if (is_attention())
 936    {
 937       if (num_consecutive_attentions > CDU31A_MAX_CONSECUTIVE_ATTENTIONS)
 938       {
 939          printk("cdu31a: Too many consecutive attentions: %d\n",
 940                 num_consecutive_attentions);
 941          num_consecutive_attentions = 0;
 942          return(0);
 943       }
 944 
 945       clear_attention();
 946       atten_code = read_result_register();
 947 
 948       switch (atten_code)
 949       {
 950        /* Someone changed the CD.  Mark it as changed */
 951       case SONY_MECH_LOADED_ATTN:
 952          disk_changed = 1;
 953          sony_toc_read = 0;
 954          sony_audio_status = CDROM_AUDIO_NO_STATUS;
 955          sony_blocks_left = 0;
 956          break;
 957 
 958       case SONY_SPIN_DOWN_COMPLETE_ATTN:
 959          /* Mark the disk as spun down. */
 960          sony_spun_up = 0;
 961          break;
 962 
 963       case SONY_AUDIO_PLAY_DONE_ATTN:
 964          sony_audio_status = CDROM_AUDIO_COMPLETED;
 965          read_subcode();
 966          break;
 967 
 968       case SONY_EJECT_PUSHED_ATTN:
 969          if (is_auto_eject)
 970          {
 971             sony_audio_status = CDROM_AUDIO_INVALID;
 972          }
 973          break;
 974 
 975       case SONY_LEAD_IN_ERR_ATTN:
 976       case SONY_LEAD_OUT_ERR_ATTN:
 977       case SONY_DATA_TRACK_ERR_ATTN:
 978       case SONY_AUDIO_PLAYBACK_ERR_ATTN:
 979          sony_audio_status = CDROM_AUDIO_ERROR;
 980          break;
 981       }
 982 
 983       num_consecutive_attentions++;
 984       return(1);
 985    }
 986    else if (abort_read_started)
 987    {
 988       while (is_result_reg_not_empty())
 989       {
 990          val = read_result_register();
 991       }
 992       clear_data_ready();
 993       clear_result_ready();
 994       /* Clear out the data */
 995       while (is_data_requested())
 996       {
 997          val = read_data_register();
 998       }
 999       abort_read_started = 0;
1000       return(1);
1001    }
1002 
1003    num_consecutive_attentions = 0;
1004    return(0);
1005 }
1006 
1007 
1008 /* Convert from an integer 0-99 to BCD */
1009 static inline unsigned int
1010 int_to_bcd(unsigned int val)
     /* [previous][next][first][last][top][bottom][index][help] */
1011 {
1012    int retval;
1013 
1014 
1015    retval = (val / 10) << 4;
1016    retval = retval | val % 10;
1017    return(retval);
1018 }
1019 
1020 
1021 /* Convert from BCD to an integer from 0-99 */
1022 static unsigned int
1023 bcd_to_int(unsigned int bcd)
     /* [previous][next][first][last][top][bottom][index][help] */
1024 {
1025    return((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1026 }
1027 
1028 
1029 /*
1030  * Convert a logical sector value (like the OS would want to use for
1031  * a block device) to an MSF format.
1032  */
1033 static void
1034 log_to_msf(unsigned int log, unsigned char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
1035 {
1036    log = log + LOG_START_OFFSET;
1037    msf[0] = int_to_bcd(log / 4500);
1038    log = log % 4500;
1039    msf[1] = int_to_bcd(log / 75);
1040    msf[2] = int_to_bcd(log % 75);
1041 }
1042 
1043 
1044 /*
1045  * Convert an MSF format to a logical sector.
1046  */
1047 static unsigned int
1048 msf_to_log(unsigned char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
1049 {
1050    unsigned int log;
1051 
1052 
1053    log = bcd_to_int(msf[2]);
1054    log += bcd_to_int(msf[1]) * 75;
1055    log += bcd_to_int(msf[0]) * 4500;
1056    log = log - LOG_START_OFFSET;
1057 
1058    return log;
1059 }
1060 
1061 
1062 /*
1063  * Take in integer size value and put it into a buffer like
1064  * the drive would want to see a number-of-sector value.
1065  */
1066 static void
1067 size_to_buf(unsigned int size,
     /* [previous][next][first][last][top][bottom][index][help] */
1068             unsigned char *buf)
1069 {
1070    buf[0] = size / 65536;
1071    size = size % 65536;
1072    buf[1] = size / 256;
1073    buf[2] = size % 256;
1074 }
1075 
1076 /* Starts a read operation. Returns 0 on success and 1 on failure. 
1077    The read operation used here allows multiple sequential sectors 
1078    to be read and status returned for each sector.  The driver will
1079    read the out one at a time as the requests come and abort the
1080    operation if the requested sector is not the next one from the
1081    drive. */
1082 static int
1083 start_request(unsigned int sector,
     /* [previous][next][first][last][top][bottom][index][help] */
1084               unsigned int nsect,
1085               int          read_nsect_only)
1086 {
1087    unsigned char params[6];
1088    unsigned int read_size;
1089    unsigned int retry_count;
1090 
1091 
1092    log_to_msf(sector, params);
1093    /* If requested, read exactly what was asked. */
1094    if (read_nsect_only)
1095    {
1096       read_size = nsect;
1097    }
1098    /*
1099     * If the full read-ahead would go beyond the end of the media, trim
1100     * it back to read just till the end of the media.
1101     */
1102    else if ((sector + nsect) >= sony_toc.lead_out_start_lba)
1103    {
1104       read_size = sony_toc.lead_out_start_lba - sector;
1105    }
1106    /* Read the full readahead amount. */
1107    else
1108    {
1109       read_size = CDU31A_READAHEAD;
1110    }
1111    size_to_buf(read_size, &params[3]);
1112 
1113    /*
1114     * Clear any outstanding attentions and wait for the drive to
1115     * complete any pending operations.
1116     */
1117    while (handle_sony_cd_attention())
1118       ;
1119 
1120    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1121    while ((retry_count > jiffies) && (is_busy()))
1122    {
1123       sony_sleep();
1124       
1125       while (handle_sony_cd_attention())
1126          ;
1127    }
1128 
1129    if (is_busy())
1130    {
1131       printk("CDU31A: Timeout while waiting to issue command\n");
1132       return(1);
1133    }
1134    else
1135    {
1136       /* Issue the command */
1137       clear_result_ready();
1138       clear_param_reg();
1139 
1140       write_params(params, 6);
1141       write_cmd(SONY_READ_BLKERR_STAT_CMD);
1142 
1143       sony_blocks_left = read_size * 4;
1144       sony_next_block = sector * 4;
1145       readahead_dataleft = 0;
1146       readahead_bad = 0;
1147       return(0);
1148    }
1149 }
1150 
1151 /* Abort a pending read operation.  Clear all the drive status and
1152    readahead variables. */
1153 static void
1154 abort_read(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1155 {
1156    unsigned char result_reg[2];
1157    int           result_size;
1158    volatile int  val;
1159 
1160 
1161    do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1162    if ((result_reg[0] & 0xf0) == 0x20)
1163    {
1164       printk("CDU31A: Error aborting read, error = 0x%2.2x\n",
1165              result_reg[1]);
1166    }
1167 
1168    while (is_result_reg_not_empty())
1169    {
1170       val = read_result_register();
1171    }
1172    clear_data_ready();
1173    clear_result_ready();
1174    /* Clear out the data */
1175    while (is_data_requested())
1176    {
1177       val = read_data_register();
1178    }
1179 
1180    sony_blocks_left = 0;
1181    readahead_dataleft = 0;
1182    readahead_bad = 0;
1183 }
1184 
1185 /* Called when the timer times out.  This will abort the
1186    pending read operation. */
1187 static void
1188 handle_abort_timeout(unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1189 {
1190    /* If it is in use, ignore it. */
1191    if (!sony_inuse)
1192    {
1193       /* We can't use abort_read(), because it will sleep
1194          or schedule in the timer interrupt.  Just start
1195          the operation, finish it on the next access to
1196          the drive. */
1197       clear_result_ready();
1198       clear_param_reg();
1199       write_cmd(SONY_ABORT_CMD);
1200 
1201       sony_blocks_left = 0;
1202       readahead_dataleft = 0;
1203       readahead_bad = 0;
1204       abort_read_started = 1;
1205    }
1206 }
1207 
1208 /* Actually get data and status from the drive. */
1209 static void
1210 input_data(char         *buffer,
     /* [previous][next][first][last][top][bottom][index][help] */
1211            unsigned int bytesleft,
1212            unsigned int nblocks,
1213            unsigned int offset,
1214            unsigned int skip)
1215 {
1216    int i;
1217    volatile unsigned char val;
1218 
1219 
1220    /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1221       the disk.  The real data is after that. */
1222    if (sony_xa_mode)
1223    {
1224       for(i=0; i<CD_XA_HEAD; i++)
1225       {
1226          val = read_data_register();
1227       }
1228    }
1229 
1230    clear_data_ready();
1231 
1232    if (bytesleft == 2048) /* 2048 byte direct buffer transfer */
1233    {
1234       insb(sony_cd_read_reg, buffer, 2048);
1235       readahead_dataleft = 0;
1236    }
1237    else
1238    {
1239       /* If the input read did not align with the beginning of the block,
1240          skip the necessary bytes. */
1241       if (skip != 0)
1242       {
1243          insb(sony_cd_read_reg, readahead_buffer, skip);
1244       }
1245 
1246       /* Get the data into the buffer. */
1247       insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1248 
1249       /* Get the rest of the data into the readahead buffer at the
1250          proper location. */
1251       readahead_dataleft = (2048 - skip) - bytesleft;
1252       insb(sony_cd_read_reg,
1253            readahead_buffer + bytesleft,
1254            readahead_dataleft);
1255    }
1256    sony_blocks_left -= nblocks;
1257    sony_next_block += nblocks; 
1258 
1259    /* If an XA disk, we have to clear out the rest of the unused
1260       error correction data. */
1261    if (sony_xa_mode)
1262    {
1263       for(i=0; i<CD_XA_TAIL; i++)
1264       {
1265          val = read_data_register();
1266       }
1267    }
1268 }
1269 
1270 /* read data from the drive.  Note the nsect must be <= 4. */
1271 static void
1272 read_data_block(char          *buffer,
     /* [previous][next][first][last][top][bottom][index][help] */
1273                 unsigned int  block,
1274                 unsigned int  nblocks,
1275                 unsigned char res_reg[],
1276                 int           *res_size)
1277 {
1278    unsigned int retry_count;
1279    unsigned int bytesleft;
1280    unsigned int offset;
1281    unsigned int skip;
1282 
1283 
1284    res_reg[0] = 0;
1285    res_reg[1] = 0;
1286    *res_size = 0;
1287    bytesleft = nblocks * 512;
1288    offset = 0;
1289 
1290    /* If the data in the read-ahead does not match the block offset,
1291       then fix things up. */
1292    if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048))
1293    {
1294       sony_next_block += block % 4;
1295       sony_blocks_left -= block % 4;
1296       skip = (block % 4) * 512;
1297    }
1298    else
1299    {
1300       skip = 0;
1301    }
1302 
1303    /* We have readahead data in the buffer, get that first before we
1304       decide if a read is necessary. */
1305    if (readahead_dataleft != 0)
1306    {
1307       if (bytesleft > readahead_dataleft)
1308       {
1309          /* The readahead will not fill the requested buffer, but
1310             get the data out of the readahead into the buffer. */
1311          memcpy(buffer,
1312                 readahead_buffer + (2048 - readahead_dataleft),
1313                 readahead_dataleft);
1314          readahead_dataleft = 0;
1315          bytesleft -= readahead_dataleft;
1316          offset += readahead_dataleft;
1317       }
1318       else
1319       {
1320          /* The readahead will fill the whole buffer, get the data
1321             and return. */
1322          memcpy(buffer,
1323                 readahead_buffer + (2048 - readahead_dataleft),
1324                 bytesleft);
1325          readahead_dataleft -= bytesleft;
1326          bytesleft = 0;
1327          sony_blocks_left -= nblocks;
1328          sony_next_block += nblocks;
1329 
1330          /* If the data in the readahead is bad, return an error so the
1331             driver will abort the buffer. */
1332          if (readahead_bad)
1333          {
1334             res_reg[0] = 0x20;
1335             res_reg[1] = SONY_BAD_DATA_ERR;
1336             *res_size = 2;
1337          }
1338 
1339          if (readahead_dataleft == 0)
1340          {
1341             readahead_bad = 0;
1342          }
1343 
1344          /* Final transfer is done for read command, get final result. */
1345          if (sony_blocks_left == 0)
1346          {
1347             get_result(res_reg, res_size);
1348          }
1349          return;
1350       }
1351    }
1352 
1353    /* Wait for the drive to tell us we have something */
1354    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1355    while ((retry_count > jiffies) && !(is_data_ready()))
1356    {
1357       while (handle_sony_cd_attention())
1358          ;
1359 
1360       sony_sleep();
1361    }
1362    if (!(is_data_ready()))
1363    {
1364       if (is_result_ready())
1365       {
1366          get_result(res_reg, res_size);
1367          if ((res_reg[0] & 0xf0) != 0x20)
1368          {
1369             printk("CDU31A: Got result that should have been error: %d\n",
1370                    res_reg[0]);
1371             res_reg[0] = 0x20;
1372             res_reg[1] = SONY_BAD_DATA_ERR;
1373             *res_size = 2;
1374          }
1375          abort_read();
1376       }
1377       else
1378       {
1379 #if DEBUG
1380          printk("CDU31A timeout out %d\n", __LINE__);
1381 #endif
1382          res_reg[0] = 0x20;
1383          res_reg[1] = SONY_TIMEOUT_OP_ERR;
1384          *res_size = 2;
1385          abort_read();
1386       }
1387    }
1388    else
1389    {
1390       input_data(buffer, bytesleft, nblocks, offset, skip);
1391 
1392       /* Wait for the status from the drive. */
1393       retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1394       while ((retry_count > jiffies) && !(is_result_ready()))
1395       {
1396          while (handle_sony_cd_attention())
1397             ;
1398 
1399          sony_sleep();
1400       }
1401 
1402       if (!is_result_ready())
1403       {
1404 #if DEBUG
1405          printk("CDU31A timeout out %d\n", __LINE__);
1406 #endif
1407          res_reg[0] = 0x20;
1408          res_reg[1] = SONY_TIMEOUT_OP_ERR;
1409          *res_size = 2;
1410          abort_read();
1411       }
1412       else
1413       {
1414          get_result(res_reg, res_size);
1415 
1416          /* If we got a buffer status, handle that. */
1417          if ((res_reg[0] & 0xf0) == 0x50)
1418          {
1419 
1420             if (   (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
1421                 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
1422                 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT))
1423             {
1424                /* The data was successful, but if data was read from
1425                   the readahead  and it was bad, set the whole
1426                   buffer as bad. */
1427                if (readahead_bad)
1428                {
1429                   readahead_bad = 0;
1430                   res_reg[0] = 0x20;
1431                   res_reg[1] = SONY_BAD_DATA_ERR;
1432                   *res_size = 2;
1433                }
1434             }
1435             else
1436             {
1437                printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
1438                res_reg[0] = 0x20;
1439                res_reg[1] = SONY_BAD_DATA_ERR;
1440                *res_size = 2;
1441 
1442                /* Data is in the readahead buffer but an error was returned.
1443                   Make sure future requests don't use the data. */
1444                if (bytesleft != 2048)
1445                {
1446                   readahead_bad = 1;
1447                }
1448             }
1449 
1450             /* Final transfer is done for read command, get final result. */
1451             if (sony_blocks_left == 0)
1452             {
1453                get_result(res_reg, res_size);
1454             }
1455          }
1456          else if ((res_reg[0] & 0xf0) != 0x20)
1457          {
1458             /* The drive gave me bad status, I don't know what to do.
1459                Reset the driver and return an error. */
1460             printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
1461             restart_on_error();
1462             res_reg[0] = 0x20;
1463             res_reg[1] = SONY_BAD_DATA_ERR;
1464             *res_size = 2;
1465          }
1466       }
1467    }
1468 }
1469 
1470 /*
1471  * The OS calls this to perform a read or write operation to the drive.
1472  * Write obviously fail.  Reads to a read ahead of sony_buffer_size
1473  * bytes to help speed operations.  This especially helps since the OS
1474  * uses 1024 byte blocks and the drive uses 2048 byte blocks.  Since most
1475  * data access on a CD is done sequentially, this saves a lot of operations.
1476  */
1477 static void
1478 do_cdu31a_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1479 {
1480    int block;
1481    int nblock;
1482    unsigned char res_reg[12];
1483    unsigned int res_size;
1484    int num_retries;
1485    unsigned long flags;
1486 
1487 
1488    /* 
1489     * Make sure no one else is using the driver; wait for them
1490     * to finish if it is so.
1491     */
1492    save_flags(flags);
1493    cli();
1494    while (sony_inuse)
1495    {
1496       interruptible_sleep_on(&sony_wait);
1497       if (current->signal & ~current->blocked)
1498       {
1499          restore_flags(flags);
1500          if (CURRENT && CURRENT->rq_status != RQ_INACTIVE)
1501          {
1502             end_request(0);
1503          }
1504          restore_flags(flags);
1505          return;
1506       }
1507    }
1508    sony_inuse = 1;
1509    has_cd_task = current;
1510 
1511    /* Get drive status before doing anything. */
1512    while (handle_sony_cd_attention())
1513       ;
1514 
1515    /* Make sure we have a valid TOC. */
1516    sony_get_toc(); 
1517 
1518    sti();
1519 
1520    /* If the timer is running, cancel it. */
1521    if (cdu31a_abort_timer.next != NULL)
1522    {
1523       del_timer(&cdu31a_abort_timer);
1524    }
1525 
1526    while (1)
1527    {
1528 cdu31a_request_startover:
1529       /*
1530        * The beginning here is stolen from the hard disk driver.  I hope
1531        * it's right.
1532        */
1533       if (!(CURRENT) || CURRENT->rq_status == RQ_INACTIVE)
1534       {
1535          goto end_do_cdu31a_request;
1536       }
1537 
1538       if (!sony_spun_up)
1539       {
1540          struct inode in;
1541 
1542          /* This is a kludge to get a valid dev in an inode that
1543             scd_open can take.  That's the only thing scd_open()
1544             uses the inode for. */
1545          in.i_rdev = CURRENT->rq_dev;
1546          scd_open(&in,NULL);
1547       }
1548 
1549       /* I don't use INIT_REQUEST because it calls return, which would
1550          return without unlocking the device.  It shouldn't matter,
1551          but just to be safe... */
1552       if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1553       {
1554          panic(DEVICE_NAME ": request list destroyed");
1555       }
1556       if (CURRENT->bh)
1557       {
1558          if (!CURRENT->bh->b_lock)
1559          {
1560             panic(DEVICE_NAME ": block not locked");
1561          }
1562       }
1563 
1564       block = CURRENT->sector;
1565       nblock = CURRENT->nr_sectors;
1566 
1567       if (!sony_toc_read)
1568       {
1569          printk("CDU31A: TOC not read\n");
1570          end_request(0);
1571          goto cdu31a_request_startover;
1572       }
1573 
1574       /* Check for base read of multi-session disk.  This will still work
1575          for single session disks, so just do it.  Blocks less than 80
1576          are for the volume info, so offset them by the start track (which
1577          should be zero for a single-session disk). */
1578       if (block < 80)
1579       {
1580          /* Offset the request into the session. */
1581          block += (sony_toc.start_track_lba * 4);
1582       }
1583 
1584       switch(CURRENT->cmd)
1585       {
1586       case READ:
1587          /*
1588           * If the block address is invalid or the request goes beyond the end of
1589           * the media, return an error.
1590           */
1591 #if 0
1592          if ((block / 4) < sony_toc.start_track_lba)
1593          {
1594             printk("CDU31A: Request before beginning of media\n");
1595             end_request(0);
1596             goto cdu31a_request_startover;
1597          }
1598 #endif
1599          if ((block / 4) >= sony_toc.lead_out_start_lba)
1600          {
1601             printk("CDU31A: Request past end of media\n");
1602             end_request(0);
1603             goto cdu31a_request_startover;
1604          }
1605          if (((block + nblock) / 4) >= sony_toc.lead_out_start_lba)
1606          {
1607             printk("CDU31A: Request past end of media\n");
1608             end_request(0);
1609             goto cdu31a_request_startover;
1610          }
1611 
1612          num_retries = 0;
1613 
1614 try_read_again:
1615          while (handle_sony_cd_attention())
1616             ;
1617 
1618          if (!sony_toc_read)
1619          {
1620             printk("CDU31A: TOC not read\n");
1621             end_request(0);
1622             goto cdu31a_request_startover;
1623          }
1624 
1625          /* If no data is left to be read from the drive, start the
1626             next request. */
1627          if (sony_blocks_left == 0)
1628          {
1629             if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1630             {
1631                end_request(0);
1632                goto cdu31a_request_startover;
1633             }
1634          }
1635          /* If the requested block is not the next one waiting in
1636             the driver, abort the current operation and start a
1637             new one. */
1638          else if (block != sony_next_block)
1639          {
1640 #if DEBUG
1641             printk("CDU31A Warning: Read for block %d, expected %d\n",
1642                    block,
1643                    sony_next_block);
1644 #endif
1645             abort_read();
1646             if (!sony_toc_read)
1647             {
1648                printk("CDU31A: TOC not read\n");
1649                end_request(0);
1650                goto cdu31a_request_startover;
1651             }
1652             if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1653             {
1654                printk("CDU31a: start request failed\n");
1655                end_request(0);
1656                goto cdu31a_request_startover;
1657             }
1658          }
1659 
1660          read_data_block(CURRENT->buffer, block, nblock, res_reg, &res_size);
1661          if (res_reg[0] == 0x20)
1662          {
1663             if (num_retries > MAX_CDU31A_RETRIES)
1664             {
1665                end_request(0);
1666                goto cdu31a_request_startover;
1667             }
1668 
1669             num_retries++;
1670             if (res_reg[1] == SONY_NOT_SPIN_ERR)
1671             {
1672                do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1673             }
1674             else
1675             {
1676                printk("CDU31A: Read error: 0x%2.2x\n", res_reg[1]);
1677             }
1678             goto try_read_again;
1679          }
1680          else
1681          {
1682             end_request(1);
1683          }
1684          break;
1685             
1686       case WRITE:
1687          end_request(0);
1688          break;
1689             
1690       default:
1691          panic("CDU31A: Unknown cmd");
1692       }
1693    }
1694 
1695 end_do_cdu31a_request:
1696 #if 0
1697    /* After finished, cancel any pending operations. */
1698    abort_read();
1699 #else
1700    /* Start a timer to time out after a while to disable
1701       the read. */
1702    cdu31a_abort_timer.expires = jiffies + 2*HZ; /* Wait 2 seconds */
1703    add_timer(&cdu31a_abort_timer);
1704 #endif
1705 
1706    has_cd_task = NULL;
1707    sony_inuse = 0;
1708    wake_up_interruptible(&sony_wait);
1709    restore_flags(flags);
1710 }
1711 
1712 /* Copy overlapping buffers. */
1713 static void
1714 mcovlp(char *dst,
     /* [previous][next][first][last][top][bottom][index][help] */
1715        char *src,
1716        int  size)
1717 {
1718    src += (size - 1);
1719    dst += (size - 1);
1720    while (size > 0)
1721    {
1722       *dst = *src;
1723       size--;
1724       dst--;
1725       src--;
1726    }
1727 }
1728 
1729 
1730 /*
1731  * Read the table of contents from the drive and set up TOC if
1732  * successful.
1733  */
1734 static void
1735 sony_get_toc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1736 {
1737    unsigned char res_reg[2];
1738    unsigned int res_size;
1739    unsigned char parms[1];
1740    int session;
1741    int num_spin_ups;
1742 
1743 
1744 #if DEBUG
1745    printk("Entering sony_get_toc\n");
1746 #endif
1747 
1748    num_spin_ups = 0;
1749    if (!sony_toc_read)
1750    {
1751 respinup_on_gettoc:
1752       /* Ignore the result, since it might error if spinning already. */
1753       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1754 
1755       do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
1756 
1757       /* The drive sometimes returns error 0.  I don't know why, but ignore
1758          it.  It seems to mean the drive has already done the operation. */
1759       if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1760       {
1761          /* If the drive is already playing, it's ok.  */
1762          if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
1763          {
1764             goto gettoc_drive_spinning;
1765          }
1766 
1767          /* If the drive says it is not spun up (even though we just did it!)
1768             then retry the operation at least a few times. */
1769          if (   (res_reg[1] == SONY_NOT_SPIN_ERR)
1770              && (num_spin_ups < MAX_CDU31A_RETRIES))
1771          {
1772             num_spin_ups++;
1773             goto respinup_on_gettoc;
1774          }
1775 
1776          printk("cdu31a: Error reading TOC: %x %x\n",
1777                 sony_toc.exec_status[0],
1778                 sony_toc.exec_status[1]);
1779          return;
1780       }
1781 
1782 gettoc_drive_spinning:
1783 
1784       /* The idea here is we keep asking for sessions until the command
1785          fails.  Then we know what the last valid session on the disk is.
1786          No need to check session 0, since session 0 is the same as session
1787          1; the command returns different information if you give it 0. 
1788          Don't check session 1 because that is the first session, it must
1789          be there. */
1790       session = 2;
1791       while (1)
1792       {
1793 #if DEBUG
1794          printk("Trying session %d\n", session);
1795 #endif
1796          parms[0] = session;
1797          do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1798                         parms,
1799                         1, 
1800                         res_reg,
1801                         &res_size);
1802 
1803 #if DEBUG
1804          printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1805 #endif
1806 
1807          if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
1808          {
1809             /* An error reading the TOC, this must be past the last session. */
1810             break;
1811          }
1812 
1813          session++;
1814 
1815          /* Let's not get carried away... */
1816          if (session > 20)
1817          {
1818             printk("cdu31a: too many sessions: %d\n", session);
1819             return;
1820          }
1821       }
1822 
1823       session--;
1824 
1825 #if DEBUG
1826       printk("Reading session %d\n", session);
1827 #endif
1828 
1829       parms[0] = session;
1830       do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1831                      parms,
1832                      1, 
1833                      (unsigned char *) &sony_toc, 
1834                      &res_size);
1835       if ((res_size < 2) || ((sony_toc.exec_status[0] & 0xf0) == 0x20))
1836       {
1837          printk("cdu31a: Error reading session %d: %x %x\n",
1838                 session,
1839                 sony_toc.exec_status[0],
1840                 sony_toc.exec_status[1]);
1841          /* An error reading the TOC.  Return without sony_toc_read
1842             set. */
1843          return;
1844       }
1845       
1846       sony_toc_read = 1;
1847 
1848       /* For points that do not exist, move the data over them
1849          to the right location. */
1850       if (sony_toc.pointb0 != 0xb0)
1851       {
1852          mcovlp(((char *) &sony_toc) + 27,
1853                 ((char *) &sony_toc) + 18,
1854                 res_size - 18);
1855          res_size += 9;
1856       }
1857       if (sony_toc.pointb1 != 0xb1)
1858       {
1859          mcovlp(((char *) &sony_toc) + 36,
1860                 ((char *) &sony_toc) + 27,
1861                 res_size - 27);
1862          res_size += 9;
1863       }
1864       if (sony_toc.pointb2 != 0xb2)
1865       {
1866          mcovlp(((char *) &sony_toc) + 45,
1867                 ((char *) &sony_toc) + 36,
1868                 res_size - 36);
1869          res_size += 9;
1870       }
1871       if (sony_toc.pointb3 != 0xb3)
1872       {
1873          mcovlp(((char *) &sony_toc) + 54,
1874                 ((char *) &sony_toc) + 45,
1875                 res_size - 45);
1876          res_size += 9;
1877       }
1878       if (sony_toc.pointb4 != 0xb4)
1879       {
1880          mcovlp(((char *) &sony_toc) + 63,
1881                 ((char *) &sony_toc) + 54,
1882                 res_size - 54);
1883          res_size += 9;
1884       }
1885       if (sony_toc.pointc0 != 0xc0)
1886       {
1887          mcovlp(((char *) &sony_toc) + 72,
1888                 ((char *) &sony_toc) + 63,
1889                 res_size - 63);
1890          res_size += 9;
1891       }
1892 
1893       sony_toc.start_track_lba = msf_to_log(sony_toc.tracks[0].track_start_msf);
1894       sony_toc.lead_out_start_lba = msf_to_log(sony_toc.lead_out_start_msf);
1895 
1896 #if DEBUG
1897    printk("Disk session %d, start track: %d, stop track: %d\n",
1898           session,
1899           sony_toc.start_track_lba,
1900           sony_toc.lead_out_start_lba);
1901 #endif
1902    }
1903 #if DEBUG
1904    printk("Leaving sony_get_toc\n");
1905 #endif
1906 }
1907 
1908 
1909 /*
1910  * Search for a specific track in the table of contents.
1911  */
1912 static int
1913 find_track(int track)
     /* [previous][next][first][last][top][bottom][index][help] */
1914 {
1915    int i;
1916    int num_tracks;
1917 
1918 
1919    num_tracks = sony_toc.last_track_num - sony_toc.first_track_num + 1;
1920    for (i = 0; i < num_tracks; i++)
1921    {
1922       if (sony_toc.tracks[i].track == track)
1923       {
1924          return i;
1925       }
1926    }
1927 
1928    return -1;
1929 }
1930 
1931 
1932 /*
1933  * Read the subcode and put it int last_sony_subcode for future use.
1934  */
1935 static int
1936 read_subcode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1937 {
1938    unsigned int res_size;
1939 
1940 
1941    do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
1942                   NULL,
1943                   0, 
1944                   (unsigned char *) &last_sony_subcode, 
1945                   &res_size);
1946    if ((res_size < 2) || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20))
1947    {
1948       printk("Sony CDROM error 0x%2.2x (read_subcode)\n",
1949              last_sony_subcode.exec_status[1]);
1950       return -EIO;
1951    }
1952 
1953    return 0;
1954 }
1955 
1956 
1957 /*
1958  * Get the subchannel info like the CDROMSUBCHNL command wants to see it.  If
1959  * the drive is playing, the subchannel needs to be read (since it would be
1960  * changing).  If the drive is paused or completed, the subcode information has
1961  * already been stored, just use that.  The ioctl call wants things in decimal
1962  * (not BCD), so all the conversions are done.
1963  */
1964 static int
1965 sony_get_subchnl_info(long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1966 {
1967    struct cdrom_subchnl schi;
1968 
1969 
1970    /* Get attention stuff */
1971    while (handle_sony_cd_attention())
1972       ;
1973 
1974    sony_get_toc();
1975    if (!sony_toc_read)
1976    {
1977       return -EIO;
1978    }
1979 
1980    verify_area(VERIFY_READ, (char *) arg, sizeof(schi));
1981    verify_area(VERIFY_WRITE, (char *) arg, sizeof(schi));
1982 
1983    memcpy_fromfs(&schi, (char *) arg, sizeof(schi));
1984    
1985    switch (sony_audio_status)
1986    {
1987    case CDROM_AUDIO_PLAY:
1988       if (read_subcode() < 0)
1989       {
1990          return -EIO;
1991       }
1992       break;
1993 
1994    case CDROM_AUDIO_PAUSED:
1995    case CDROM_AUDIO_COMPLETED:
1996       break;
1997 
1998    case CDROM_AUDIO_NO_STATUS:
1999       schi.cdsc_audiostatus = sony_audio_status;
2000       memcpy_tofs((char *) arg, &schi, sizeof(schi));
2001       return 0;
2002       break;
2003 
2004    case CDROM_AUDIO_INVALID:
2005    case CDROM_AUDIO_ERROR:
2006    default:
2007       return -EIO;
2008    }
2009 
2010    schi.cdsc_audiostatus = sony_audio_status;
2011    schi.cdsc_adr = last_sony_subcode.address;
2012    schi.cdsc_ctrl = last_sony_subcode.control;
2013    schi.cdsc_trk = bcd_to_int(last_sony_subcode.track_num);
2014    schi.cdsc_ind = bcd_to_int(last_sony_subcode.index_num);
2015    if (schi.cdsc_format == CDROM_MSF)
2016    {
2017       schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode.abs_msf[0]);
2018       schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode.abs_msf[1]);
2019       schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode.abs_msf[2]);
2020 
2021       schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode.rel_msf[0]);
2022       schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode.rel_msf[1]);
2023       schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode.rel_msf[2]);
2024    }
2025    else if (schi.cdsc_format == CDROM_LBA)
2026    {
2027       schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode.abs_msf);
2028       schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode.rel_msf);
2029    }
2030    
2031    memcpy_tofs((char *) arg, &schi, sizeof(schi));
2032    return 0;
2033 }
2034 
2035 /* Get audio data from the drive.  This is fairly complex because I
2036    am looking for status and data at the same time, but if I get status
2037    then I just look for data.  I need to get the status immediately so
2038    the switch from audio to data tracks will happen quickly. */
2039 static void
2040 read_audio_data(char          *buffer,
     /* [previous][next][first][last][top][bottom][index][help] */
2041                 unsigned char res_reg[],
2042                 int           *res_size)
2043 {
2044    unsigned int retry_count;
2045    int result_read;
2046 
2047 
2048    res_reg[0] = 0;
2049    res_reg[1] = 0;
2050    *res_size = 0;
2051    result_read = 0;
2052 
2053    /* Wait for the drive to tell us we have something */
2054    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2055 continue_read_audio_wait:
2056    while (   (retry_count > jiffies)
2057           && !(is_data_ready())
2058           && !(is_result_ready() || result_read))
2059    {
2060       while (handle_sony_cd_attention())
2061          ;
2062 
2063       sony_sleep();
2064    }
2065    if (!(is_data_ready()))
2066    {
2067       if (is_result_ready() && !result_read)
2068       {
2069          get_result(res_reg, res_size);
2070 
2071          /* Read block status and continue waiting for data. */
2072          if ((res_reg[0] & 0xf0) == 0x50)
2073          {
2074             result_read = 1;
2075             goto continue_read_audio_wait;
2076          }
2077          /* Invalid data from the drive.  Shut down the operation. */
2078          else if ((res_reg[0] & 0xf0) != 0x20)
2079          {
2080             printk("CDU31A: Got result that should have been error: %d\n",
2081                    res_reg[0]);
2082             res_reg[0] = 0x20;
2083             res_reg[1] = SONY_BAD_DATA_ERR;
2084             *res_size = 2;
2085          }
2086          abort_read();
2087       }
2088       else
2089       {
2090 #if DEBUG
2091          printk("CDU31A timeout out %d\n", __LINE__);
2092 #endif
2093          res_reg[0] = 0x20;
2094          res_reg[1] = SONY_TIMEOUT_OP_ERR;
2095          *res_size = 2;
2096          abort_read();
2097       }
2098    }
2099    else
2100    {
2101       clear_data_ready();
2102 
2103       /* If data block, then get 2340 bytes offset by 12. */
2104       if (sony_raw_data_mode)
2105       {
2106          insb(sony_cd_read_reg, buffer + CD_XA_HEAD, CD_FRAMESIZE_XA);
2107       }
2108       else
2109       {
2110          /* Audio gets the whole 2352 bytes. */
2111          insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2112       }
2113 
2114       /* If I haven't already gotten the result, get it now. */
2115       if (!result_read)
2116       {
2117          /* Wait for the drive to tell us we have something */
2118          retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2119          while ((retry_count > jiffies) && !(is_result_ready()))
2120          {
2121             while (handle_sony_cd_attention())
2122                ;
2123 
2124             sony_sleep();
2125          }
2126 
2127          if (!is_result_ready())
2128          {
2129 #if DEBUG
2130             printk("CDU31A timeout out %d\n", __LINE__);
2131 #endif
2132             res_reg[0] = 0x20;
2133             res_reg[1] = SONY_TIMEOUT_OP_ERR;
2134             *res_size = 2;
2135             abort_read();
2136             return;
2137          }
2138          else
2139          {
2140             get_result(res_reg, res_size);
2141          }
2142       }
2143 
2144       if ((res_reg[0] & 0xf0) == 0x50)
2145       {
2146          if (   (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2147              || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2148              || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2149              || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT))
2150          {
2151             /* Ok, nothing to do. */
2152          }
2153          else
2154          {
2155             printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
2156             res_reg[0] = 0x20;
2157             res_reg[1] = SONY_BAD_DATA_ERR;
2158             *res_size = 2;
2159          }
2160       }
2161       else if ((res_reg[0] & 0xf0) != 0x20)
2162       {
2163          /* The drive gave me bad status, I don't know what to do.
2164             Reset the driver and return an error. */
2165          printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
2166          restart_on_error();
2167          res_reg[0] = 0x20;
2168          res_reg[1] = SONY_BAD_DATA_ERR;
2169          *res_size = 2;
2170       }
2171    }
2172 }
2173 
2174 /* Perform a raw data read.  This will automatically detect the
2175    track type and read the proper data (audio or data). */
2176 static int
2177 read_audio(struct cdrom_read_audio *ra,
     /* [previous][next][first][last][top][bottom][index][help] */
2178            struct inode            *inode)
2179 {
2180    int retval;
2181    unsigned char params[2];
2182    unsigned char res_reg[12];
2183    unsigned int res_size;
2184    unsigned int cframe;
2185    unsigned long flags;
2186 
2187    /* 
2188     * Make sure no one else is using the driver; wait for them
2189     * to finish if it is so.
2190     */
2191    save_flags(flags);
2192    cli();
2193    while (sony_inuse)
2194    {
2195       interruptible_sleep_on(&sony_wait);
2196       if (current->signal & ~current->blocked)
2197       {
2198          restore_flags(flags);
2199          return -EAGAIN;
2200       }
2201    }
2202    sony_inuse = 1;
2203    has_cd_task = current;
2204    restore_flags(flags);
2205 
2206    if (!sony_spun_up)
2207    {
2208       scd_open (inode, NULL);
2209    }
2210 
2211    /* Set the drive to do raw operations. */
2212    params[0] = SONY_SD_DECODE_PARAM;
2213    params[1] = 0x06 | sony_raw_data_mode;
2214    do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2215                   params,
2216                   2,
2217                   res_reg,
2218                   &res_size);
2219    if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2220    {
2221       printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2222       return -EIO;
2223    }
2224 
2225    /* From here down, we have to goto exit_read_audio instead of returning
2226       because the drive parameters have to be set back to data before
2227       return. */
2228 
2229    retval = 0;
2230    /* start_request clears out any readahead data, so it should be safe. */
2231    if (start_request(ra->addr.lba, ra->nframes, 1))
2232    {
2233       retval = -EIO;
2234       goto exit_read_audio;
2235    }
2236 
2237    /* For every requested frame. */
2238    cframe = 0;
2239    while (cframe < ra->nframes)
2240    {
2241       read_audio_data(readahead_buffer, res_reg, &res_size);
2242       if ((res_reg[0] & 0xf0) == 0x20)
2243       {
2244          if (res_reg[1] == SONY_BAD_DATA_ERR)
2245          {
2246             printk("CDU31A: Data error on audio sector %d\n",
2247                    ra->addr.lba + cframe);
2248          }
2249          else if (res_reg[1] == SONY_ILL_TRACK_R_ERR)
2250          {
2251             /* Illegal track type, change track types and start over. */
2252             sony_raw_data_mode = (sony_raw_data_mode) ? 0 : 1;
2253 
2254             /* Set the drive mode. */
2255             params[0] = SONY_SD_DECODE_PARAM;
2256             params[1] = 0x06 | sony_raw_data_mode;
2257             do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2258                            params,
2259                            2,
2260                            res_reg,
2261                            &res_size);
2262             if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2263             {
2264                printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2265                retval = -EIO;
2266                goto exit_read_audio;
2267             }
2268 
2269             /* Restart the request on the current frame. */
2270             if (start_request(ra->addr.lba + cframe, ra->nframes - cframe, 1))
2271             {
2272                retval = -EIO;
2273                goto exit_read_audio;
2274             }
2275 
2276             /* Don't go back to the top because don't want to get into
2277                and infinite loop.  A lot of code gets duplicated, but
2278                that's no big deal, I don't guess. */
2279             read_audio_data(readahead_buffer, res_reg, &res_size);
2280             if ((res_reg[0] & 0xf0) == 0x20)
2281             {
2282                if (res_reg[1] == SONY_BAD_DATA_ERR)
2283                {
2284                   printk("CDU31A: Data error on audio sector %d\n",
2285                          ra->addr.lba + cframe);
2286                }
2287                else
2288                {
2289                   printk("CDU31A: Error reading audio data on sector %d: 0x%x\n",
2290                          ra->addr.lba + cframe,
2291                          res_reg[1]);
2292                   retval = -EIO;
2293                   goto exit_read_audio;
2294                }
2295             }
2296             else
2297             {
2298                memcpy_tofs((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2299                            (char *) readahead_buffer,
2300                            CD_FRAMESIZE_RAW);
2301             }
2302          }
2303          else
2304          {
2305             printk("CDU31A: Error reading audio data on sector %d: 0x%x\n",
2306                    ra->addr.lba + cframe,
2307                    res_reg[1]);
2308             retval = -EIO;
2309             goto exit_read_audio;
2310          }
2311       }
2312       else
2313       {
2314          memcpy_tofs((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2315                      (char *) readahead_buffer,
2316                      CD_FRAMESIZE_RAW);
2317       }
2318 
2319       cframe++;
2320    }
2321 
2322    get_result(res_reg, &res_size);
2323    if ((res_reg[0] & 0xf0) == 0x20)
2324    {
2325       printk("CDU31A: Error return from audio read: 0x%x\n",
2326              res_reg[1]);
2327       retval = -EIO;
2328       goto exit_read_audio;
2329    }
2330 
2331 exit_read_audio:
2332 
2333    /* Set the drive mode back to the proper one for the disk. */
2334    params[0] = SONY_SD_DECODE_PARAM;
2335    if (!sony_xa_mode)
2336    {
2337       params[1] = 0x0f;
2338    }
2339    else
2340    {
2341       params[1] = 0x07;
2342    }
2343    do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2344                   params,
2345                   2,
2346                   res_reg,
2347                   &res_size);
2348    if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2349    {
2350       printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", res_reg[1]);
2351       return -EIO;
2352    }
2353 
2354    has_cd_task = NULL;
2355    sony_inuse = 0;
2356    wake_up_interruptible(&sony_wait);
2357 
2358    return(retval);
2359 }
2360 
2361 static int
2362 do_sony_cd_cmd_chk(const char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
2363                    unsigned char cmd,
2364                    unsigned char *params,
2365                    unsigned int num_params,
2366                    unsigned char *result_buffer,
2367                    unsigned int *result_size)
2368 {      
2369       do_sony_cd_cmd(cmd, params, num_params, result_buffer, result_size);
2370       if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20))
2371       {
2372          printk("Sony CDROM error 0x%2.2x (CDROM%s)\n", result_buffer[1], name);
2373          return -EIO;
2374       }
2375       return 0;
2376 }
2377  
2378 /*
2379  * The big ugly ioctl handler.
2380  */
2381 static int scd_ioctl(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
2382           struct file  *file,
2383           unsigned int  cmd,
2384           unsigned long arg)
2385 {
2386    unsigned char res_reg[12];
2387    unsigned int res_size;
2388    unsigned char params[7];
2389    int i;
2390 
2391 
2392    if (!inode)
2393    {
2394       return -EINVAL;
2395    }
2396 
2397    switch (cmd)
2398    {
2399    case CDROMSTART:     /* Spin up the drive */
2400       return do_sony_cd_cmd_chk("START",SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2401       return 0;
2402       break;
2403       
2404    case CDROMSTOP:      /* Spin down the drive */
2405       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2406 
2407       /*
2408        * Spin the drive down, ignoring the error if the disk was
2409        * already not spinning.
2410        */
2411       sony_audio_status = CDROM_AUDIO_NO_STATUS;
2412       return do_sony_cd_cmd_chk("STOP",SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2413 
2414    case CDROMPAUSE:     /* Pause the drive */
2415       if(do_sony_cd_cmd_chk("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size))
2416         return -EIO;
2417       /* Get the current position and save it for resuming */
2418       if (read_subcode() < 0)
2419       {
2420          return -EIO;
2421       }
2422       cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2423       cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2424       cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2425       sony_audio_status = CDROM_AUDIO_PAUSED;
2426       return 0;
2427       break;
2428 
2429    case CDROMRESUME:    /* Start the drive after being paused */
2430       if (sony_audio_status != CDROM_AUDIO_PAUSED)
2431       {
2432          return -EINVAL;
2433       }
2434       
2435       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2436       
2437       /* Start the drive at the saved position. */
2438       params[1] = cur_pos_msf[0];
2439       params[2] = cur_pos_msf[1];
2440       params[3] = cur_pos_msf[2];
2441       params[4] = final_pos_msf[0];
2442       params[5] = final_pos_msf[1];
2443       params[6] = final_pos_msf[2];
2444       params[0] = 0x03;
2445       if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2446         return -EIO;
2447       sony_audio_status = CDROM_AUDIO_PLAY;
2448       return 0;
2449 
2450    case CDROMPLAYMSF:   /* Play starting at the given MSF address. */
2451       i=verify_area(VERIFY_READ, (char *) arg, 6);
2452       if(i)
2453         return i;
2454       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2455       memcpy_fromfs(&(params[1]), (void *) arg, 6);
2456       
2457       /* The parameters are given in int, must be converted */
2458       for (i=1; i<7; i++)
2459       {
2460          params[i] = int_to_bcd(params[i]);
2461       }
2462       params[0] = 0x03;
2463       if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2464         return -EIO;
2465       
2466       /* Save the final position for pauses and resumes */
2467       final_pos_msf[0] = params[4];
2468       final_pos_msf[1] = params[5];
2469       final_pos_msf[2] = params[6];
2470       sony_audio_status = CDROM_AUDIO_PLAY;
2471       return 0;
2472 
2473    case CDROMREADTOCHDR:        /* Read the table of contents header */
2474       {
2475          struct cdrom_tochdr *hdr;
2476          struct cdrom_tochdr loc_hdr;
2477          
2478          sony_get_toc();
2479          if (!sony_toc_read)
2480          {
2481             return -EIO;
2482          }
2483          
2484          hdr = (struct cdrom_tochdr *) arg;
2485          i=verify_area(VERIFY_WRITE, hdr, sizeof(*hdr));
2486          if(i<0)
2487                 return i;
2488          loc_hdr.cdth_trk0 = bcd_to_int(sony_toc.first_track_num);
2489          loc_hdr.cdth_trk1 = bcd_to_int(sony_toc.last_track_num);
2490          memcpy_tofs(hdr, &loc_hdr, sizeof(*hdr));
2491       }
2492       return 0;
2493 
2494    case CDROMREADTOCENTRY:      /* Read a given table of contents entry */
2495       {
2496          struct cdrom_tocentry *entry;
2497          struct cdrom_tocentry loc_entry;
2498          int track_idx;
2499          unsigned char *msf_val = NULL;
2500          
2501          sony_get_toc();
2502          if (!sony_toc_read)
2503          {
2504             return -EIO;
2505          }
2506          
2507          entry = (struct cdrom_tocentry *) arg;
2508          i=verify_area(VERIFY_READ, entry, sizeof(*entry));
2509          if(i<0)
2510                 return i;
2511          i=verify_area(VERIFY_WRITE, entry, sizeof(*entry));
2512          if(i<0)
2513                 return i;
2514          
2515          memcpy_fromfs(&loc_entry, entry, sizeof(loc_entry));
2516          
2517          /* Lead out is handled separately since it is special. */
2518          if (loc_entry.cdte_track == CDROM_LEADOUT)
2519          {
2520             loc_entry.cdte_adr = sony_toc.address2;
2521             loc_entry.cdte_ctrl = sony_toc.control2;
2522             msf_val = sony_toc.lead_out_start_msf;
2523          }
2524          else
2525          {
2526             track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
2527             if (track_idx < 0)
2528             {
2529                return -EINVAL;
2530             }
2531             
2532             loc_entry.cdte_adr = sony_toc.tracks[track_idx].address;
2533             loc_entry.cdte_ctrl = sony_toc.tracks[track_idx].control;
2534             msf_val = sony_toc.tracks[track_idx].track_start_msf;
2535          }
2536          
2537          /* Logical buffer address or MSF format requested? */
2538          if (loc_entry.cdte_format == CDROM_LBA)
2539          {
2540             loc_entry.cdte_addr.lba = msf_to_log(msf_val);
2541          }
2542          else if (loc_entry.cdte_format == CDROM_MSF)
2543          {
2544             loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
2545             loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val+1));
2546             loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val+2));
2547          }
2548          memcpy_tofs(entry, &loc_entry, sizeof(*entry));
2549       }
2550       return 0;
2551       break;
2552 
2553    case CDROMPLAYTRKIND:     /* Play a track.  This currently ignores index. */
2554       {
2555          struct cdrom_ti ti;
2556          int track_idx;
2557          
2558          sony_get_toc();
2559          if (!sony_toc_read)
2560          {
2561             return -EIO;
2562          }
2563          
2564          i=verify_area(VERIFY_READ, (char *) arg, sizeof(ti));
2565          if(i<0)
2566                 return i;
2567          
2568          memcpy_fromfs(&ti, (char *) arg, sizeof(ti));
2569          if (   (ti.cdti_trk0 < sony_toc.first_track_num)
2570              || (ti.cdti_trk0 > sony_toc.last_track_num)
2571              || (ti.cdti_trk1 < ti.cdti_trk0))
2572          {
2573             return -EINVAL;
2574          }
2575          
2576          track_idx = find_track(int_to_bcd(ti.cdti_trk0));
2577          if (track_idx < 0)
2578          {
2579             return -EINVAL;
2580          }
2581          params[1] = sony_toc.tracks[track_idx].track_start_msf[0];
2582          params[2] = sony_toc.tracks[track_idx].track_start_msf[1];
2583          params[3] = sony_toc.tracks[track_idx].track_start_msf[2];
2584          
2585          /*
2586           * If we want to stop after the last track, use the lead-out
2587           * MSF to do that.
2588           */
2589          if (ti.cdti_trk1 >= bcd_to_int(sony_toc.last_track_num))
2590          {
2591             log_to_msf(msf_to_log(sony_toc.lead_out_start_msf)-1,
2592                        &(params[4]));
2593          }
2594          else
2595          {
2596             track_idx = find_track(int_to_bcd(ti.cdti_trk1+1));
2597             if (track_idx < 0)
2598             {
2599                return -EINVAL;
2600             }
2601             log_to_msf(msf_to_log(sony_toc.tracks[track_idx].track_start_msf)-1,
2602                        &(params[4]));
2603          }
2604          params[0] = 0x03;
2605          
2606          do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2607          
2608          do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
2609          
2610          if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2611          {
2612             printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
2613                    params[2], params[3], params[4], params[5], params[6]);
2614             printk("Sony CDROM error 0x%2.2x (CDROMPLAYTRKIND\n", res_reg[1]);
2615             return -EIO;
2616          }
2617          
2618          /* Save the final position for pauses and resumes */
2619          final_pos_msf[0] = params[4];
2620          final_pos_msf[1] = params[5];
2621          final_pos_msf[2] = params[6];
2622          sony_audio_status = CDROM_AUDIO_PLAY;
2623          return 0;
2624       }
2625      
2626    case CDROMSUBCHNL:   /* Get subchannel info */
2627       return sony_get_subchnl_info(arg);
2628 
2629    case CDROMVOLCTRL:   /* Volume control.  What volume does this change, anyway? */
2630       {
2631          struct cdrom_volctrl volctrl;
2632          
2633          i=verify_area(VERIFY_READ, (char *) arg, sizeof(volctrl));
2634          if(i<0)
2635                 return i;
2636          
2637          memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
2638          params[0] = SONY_SD_AUDIO_VOLUME;
2639          params[1] = volctrl.channel0;
2640          params[2] = volctrl.channel1;
2641          return do_sony_cd_cmd_chk("VOLCTRL",SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
2642       }
2643    case CDROMEJECT:     /* Eject the drive */
2644       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2645       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2646 
2647       sony_audio_status = CDROM_AUDIO_INVALID;
2648       return do_sony_cd_cmd_chk("EJECT",SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
2649      
2650     case CDROMREADAUDIO:      /* Read 2352 byte audio tracks and 2340 byte
2651                                  raw data tracks. */
2652       {
2653          struct cdrom_read_audio ra;
2654 
2655 
2656          sony_get_toc();
2657          if (!sony_toc_read)
2658          {
2659             return -EIO;
2660          }
2661          
2662          i=verify_area(VERIFY_READ, (char *) arg, sizeof(ra));
2663          if(i<0)
2664                 return i;
2665          memcpy_fromfs(&ra, (char *) arg, sizeof(ra));
2666 
2667          i=verify_area(VERIFY_WRITE, ra.buf, CD_FRAMESIZE_RAW * ra.nframes);
2668          if(i<0)
2669                 return i;
2670 
2671          if (ra.addr_format == CDROM_LBA)
2672          {
2673             if (   (ra.addr.lba >= sony_toc.lead_out_start_lba)
2674                 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
2675             {
2676                return -EINVAL;
2677             }
2678          }
2679          else if (ra.addr_format == CDROM_MSF)
2680          {
2681             if (   (ra.addr.msf.minute >= 75)
2682                 || (ra.addr.msf.second >= 60)
2683                 || (ra.addr.msf.frame >= 75))
2684             {
2685                return -EINVAL;
2686             }
2687 
2688             ra.addr.lba = (  (ra.addr.msf.minute * 4500)
2689                            + (ra.addr.msf.second * 75)
2690                            + ra.addr.msf.frame);
2691             if (   (ra.addr.lba >= sony_toc.lead_out_start_lba)
2692                 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
2693             {
2694                return -EINVAL;
2695             }
2696 
2697             /* I know, this can go negative on an unsigned.  However,
2698                the first thing done to the data is to add this value,
2699                so this should compensate and allow direct msf access. */
2700             ra.addr.lba -= LOG_START_OFFSET;
2701          }
2702          else
2703          {
2704             return -EINVAL;
2705          }
2706 
2707          return(read_audio(&ra, inode));
2708       }
2709       return 0;
2710       break;
2711 
2712    case CDROMEJECT_SW:
2713       is_auto_eject = arg;
2714       set_drive_params();
2715       return 0;
2716       break;
2717 
2718    default:
2719       return -EINVAL;
2720    }
2721 }
2722 
2723 
2724 /*
2725  * Open the drive for operations.  Spin the drive up and read the table of
2726  * contents if these have not already been done.
2727  */
2728 static int
2729 scd_open(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
2730          struct file *filp)
2731 {
2732    unsigned char res_reg[12];
2733    unsigned int res_size;
2734    int num_spin_ups;
2735    unsigned char params[2];
2736 
2737 
2738    if ((filp) && filp->f_mode & 2)
2739       return -EROFS;
2740 
2741    if (!sony_spun_up)
2742    {
2743       num_spin_ups = 0;
2744 
2745 respinup_on_open:
2746       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2747 
2748       /* The drive sometimes returns error 0.  I don't know why, but ignore
2749          it.  It seems to mean the drive has already done the operation. */
2750       if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
2751       {
2752          printk("Sony CDROM error 0x%2.2x (scd_open, spin up)\n", res_reg[1]);
2753          return -EIO;
2754       }
2755       
2756       do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
2757 
2758       /* The drive sometimes returns error 0.  I don't know why, but ignore
2759          it.  It seems to mean the drive has already done the operation. */
2760       if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
2761       {
2762          /* If the drive is already playing, it's ok.  */
2763          if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
2764          {
2765             goto drive_spinning;
2766          }
2767 
2768          /* If the drive says it is not spun up (even though we just did it!)
2769             then retry the operation at least a few times. */
2770          if (   (res_reg[1] == SONY_NOT_SPIN_ERR)
2771              && (num_spin_ups < MAX_CDU31A_RETRIES))
2772          {
2773             num_spin_ups++;
2774             goto respinup_on_open;
2775          }
2776 
2777          printk("Sony CDROM error 0x%2.2x (scd_open, read toc)\n", res_reg[1]);
2778          do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2779          
2780          return -EIO;
2781       }
2782 
2783       sony_get_toc();
2784       if (!sony_toc_read)
2785       {
2786          do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2787          return -EIO;
2788       }
2789 
2790       /* For XA on the CDU31A only, we have to do special reads.
2791          The CDU33A handles XA automagically. */
2792       if (   (sony_toc.disk_type == SONY_XA_DISK_TYPE)
2793           && (!is_double_speed))
2794       {
2795          params[0] = SONY_SD_DECODE_PARAM;
2796          params[1] = 0x07;
2797          do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2798                         params,
2799                         2,
2800                         res_reg,
2801                         &res_size);
2802          if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2803          {
2804             printk("CDU31A: Unable to set XA params: 0x%2.2x\n", res_reg[1]);
2805          }
2806          sony_xa_mode = 1;
2807       }
2808       /* A non-XA disk.  Set the parms back if necessary. */
2809       else if (sony_xa_mode)
2810       {
2811          params[0] = SONY_SD_DECODE_PARAM;
2812          params[1] = 0x0f;
2813          do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2814                         params,
2815                         2,
2816                         res_reg,
2817                         &res_size);
2818          if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2819          {
2820             printk("CDU31A: Unable to reset XA params: 0x%2.2x\n", res_reg[1]);
2821          }
2822          sony_xa_mode = 0;
2823       }
2824 
2825       sony_spun_up = 1;
2826    }
2827 
2828 drive_spinning:
2829 
2830    /* If filp is not NULL (standard open), try a disk change. */
2831    if (filp)
2832    {
2833       check_disk_change(inode->i_rdev);
2834    }
2835 
2836    sony_usage++;
2837    MOD_INC_USE_COUNT;
2838 
2839    /* If all is OK (until now...), then lock the door */
2840    is_auto_eject = 0;
2841    set_drive_params();
2842 
2843    return 0;
2844 }
2845 
2846 
2847 /*
2848  * Close the drive.  Spin it down if no task is using it.  The spin
2849  * down will fail if playing audio, so audio play is OK.
2850  */
2851 static void
2852 scd_release(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
2853          struct file *filp)
2854 {
2855    unsigned char res_reg[12];
2856    unsigned int  res_size;
2857 
2858 
2859    if (sony_usage > 0)
2860    {
2861       sony_usage--;
2862       MOD_DEC_USE_COUNT;
2863    }
2864    if (sony_usage == 0)
2865    {
2866       sync_dev(inode->i_rdev);
2867 
2868       /* Unlock the door, only if nobody is using the drive */
2869       is_auto_eject = 1;
2870       set_drive_params();
2871 
2872       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2873 
2874       sony_spun_up = 0;
2875    }
2876 }
2877 
2878 
2879 static struct file_operations scd_fops = {
2880    NULL,                   /* lseek - default */
2881    block_read,             /* read - general block-dev read */
2882    block_write,            /* write - general block-dev write */
2883    NULL,                   /* readdir - bad */
2884    NULL,                   /* select */
2885    scd_ioctl,              /* ioctl */
2886    NULL,                   /* mmap */
2887    scd_open,               /* open */
2888    scd_release,            /* release */
2889    NULL,                   /* fsync */
2890    NULL,                   /* fasync */
2891    scd_disk_change,        /* media_change */
2892    NULL                    /* revalidate */
2893 };
2894 
2895 
2896 /* The different types of disc loading mechanisms supported */
2897 static const char *load_mech[] = { "caddy", "tray", "pop-up", "unknown" };
2898 
2899 static void
2900 get_drive_configuration(unsigned short base_io,
     /* [previous][next][first][last][top][bottom][index][help] */
2901                         unsigned char res_reg[],
2902                         unsigned int *res_size)
2903 {
2904    int retry_count;
2905 
2906 
2907    /* Set the base address */
2908    cdu31a_port = base_io;
2909 
2910    /* Set up all the register locations */
2911    sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
2912    sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
2913    sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
2914    sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
2915    sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
2916    sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
2917    sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
2918    sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
2919 
2920    /*
2921     * Check to see if anything exists at the status register location.
2922     * I don't know if this is a good way to check, but it seems to work
2923     * ok for me.
2924     */
2925    if (read_status_register() != 0xff)
2926    {
2927       /*
2928        * Reset the drive and wait for attention from it (to say it's reset).
2929        * If you don't wait, the next operation will probably fail.
2930        */
2931       reset_drive();
2932       retry_count = jiffies + SONY_RESET_TIMEOUT;
2933       while ((retry_count > jiffies) && (!is_attention()))
2934       {
2935          sony_sleep();
2936       }
2937 
2938 #if 0
2939       /* If attention is never seen probably not a CDU31a present */
2940       if (!is_attention())
2941       {
2942          res_reg[0] = 0x20;
2943          return;
2944       }
2945 #endif
2946 
2947       /*
2948        * Get the drive configuration.
2949        */
2950       do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
2951                      NULL,
2952                      0,
2953                      (unsigned char *) res_reg,
2954                      res_size);
2955       return;
2956    }
2957 
2958    /* Return an error */
2959    res_reg[0] = 0x20;
2960 }
2961 
2962 #ifndef MODULE
2963 /*
2964  * Set up base I/O and interrupts, called from main.c.
2965  */
2966 void
2967 cdu31a_setup(char *strings,
     /* [previous][next][first][last][top][bottom][index][help] */
2968              int  *ints)
2969 {
2970    if (ints[0] > 0)
2971    {
2972       cdu31a_port = ints[1];
2973    }
2974    if (ints[0] > 1)
2975    {
2976       cdu31a_irq = ints[2];
2977    }
2978    if ((strings != NULL) && (*strings != '\0'))
2979    {
2980       if (strcmp(strings, "PAS") == 0)
2981       {
2982          sony_pas_init = 1;
2983       }
2984       else
2985       {
2986          printk("CDU31A: Unknown interface type: %s\n", strings);
2987       }
2988    }
2989 }
2990 #endif
2991 
2992 static int cdu31a_block_size;
2993 
2994 /*
2995  * Initialize the driver.
2996  */
2997 #ifdef MODULE
2998 #define cdu31a_init init_module
2999 #endif
3000 
3001 int
3002 cdu31a_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3003 {
3004    struct s_sony_drive_config drive_config;
3005    unsigned int res_size;
3006    int i;
3007    int drive_found;
3008    int tmp_irq;
3009 
3010 
3011    /*
3012     * According to Alex Freed (freed@europa.orion.adobe.com), this is
3013     * required for the Fusion CD-16 package.  If the sound driver is
3014     * loaded, it should work fine, but just in case...
3015     *
3016     * The following turn on the CD-ROM interface for a Fusion CD-16.
3017     */
3018    if (sony_pas_init)
3019    {
3020       outb(0xbc, 0x9a01);
3021       outb(0xe2, 0x9a01);
3022    }
3023 
3024    drive_found = 0;
3025 
3026    /* Setting the base I/O address to 0xffff will disable it. */
3027    if (cdu31a_port == 0xffff)
3028    {
3029    }
3030    else if (cdu31a_port != 0)
3031    {
3032       tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3033       cdu31a_irq = 0;
3034 
3035       get_drive_configuration(cdu31a_port,
3036                               drive_config.exec_status,
3037                               &res_size);
3038       if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3039       {
3040          drive_found = 1;
3041       }
3042 
3043       cdu31a_irq = tmp_irq;
3044    }
3045    else
3046    {
3047       cdu31a_irq = 0;
3048       i = 0;
3049       while (   (cdu31a_addresses[i].base != 0)
3050              && (!drive_found))
3051       {
3052          if (check_region(cdu31a_addresses[i].base, 4)) {
3053             i++;
3054             continue;
3055          }
3056          get_drive_configuration(cdu31a_addresses[i].base,
3057                                  drive_config.exec_status,
3058                                  &res_size);
3059          if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3060          {
3061             drive_found = 1;
3062             cdu31a_irq = cdu31a_addresses[i].int_num;
3063          }
3064          else
3065          {
3066             i++;
3067          }
3068       }
3069    }
3070 
3071    if (drive_found)
3072    {
3073       request_region(cdu31a_port, 4,"cdu31a");
3074       
3075       if (register_blkdev(MAJOR_NR,"cdu31a",&scd_fops))
3076       {
3077          printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
3078          return -EIO;
3079       }
3080 
3081       if (SONY_HWC_DOUBLE_SPEED(drive_config))
3082       {
3083          is_double_speed = 1;
3084       }
3085 
3086       tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3087       cdu31a_irq = 0;
3088 
3089       set_drive_params();
3090 
3091       cdu31a_irq = tmp_irq;
3092       
3093       if (cdu31a_irq > 0)
3094       {
3095          if (request_irq(cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT, "cdu31a"))
3096          {
3097             printk("Unable to grab IRQ%d for the CDU31A driver\n", cdu31a_irq);
3098             cdu31a_irq = 0;
3099          }
3100       }
3101       
3102       printk("Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3103              drive_config.vendor_id,
3104              drive_config.product_id,
3105              drive_config.product_rev_level);
3106       printk("  Capabilities: %s",
3107              load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3108       if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3109       {
3110          printk(", audio");
3111       }
3112       if (SONY_HWC_EJECT(drive_config))
3113       {
3114          printk(", eject");
3115       }
3116       if (SONY_HWC_LED_SUPPORT(drive_config))
3117       {
3118          printk(", LED");
3119       }
3120       if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
3121       {
3122          printk(", elec. Vol");
3123       }
3124       if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
3125       {
3126          printk(", sep. Vol");
3127       }
3128       if (is_double_speed)
3129       {
3130          printk(", double speed");
3131       }
3132       if (cdu31a_irq > 0)
3133       {
3134          printk(", irq %d", cdu31a_irq);
3135       }
3136       printk("\n");
3137 
3138       blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
3139       read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3140       cdu31a_block_size = 1024; /* 1kB default block size */
3141       /* use 'mount -o block=2048' */
3142       blksize_size[MAJOR_NR] = &cdu31a_block_size;
3143       
3144       cdu31a_abort_timer.next = NULL;
3145       cdu31a_abort_timer.prev = NULL;
3146       cdu31a_abort_timer.function = handle_abort_timeout;
3147    }
3148 
3149 
3150    disk_changed = 1;
3151    
3152    if (drive_found)
3153    {
3154       return(0);
3155    }
3156    else
3157    {
3158       return -EIO;
3159    }
3160 }
3161 
3162 #ifdef MODULE
3163 void
3164 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3165 {
3166    if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL))    
3167    {
3168       printk("Can't unregister cdu31a\n");
3169       return;
3170    }
3171    release_region(cdu31a_port,4);
3172    printk("cdu31a module released.\n");
3173 }   
3174 #endif MODULE

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