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

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