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

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