root/drivers/block/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. cdu31a_interrupt
  5. sony_sleep
  6. is_attention
  7. is_busy
  8. is_data_ready
  9. is_data_requested
  10. is_result_ready
  11. is_param_write_rdy
  12. is_result_reg_not_empty
  13. reset_drive
  14. clear_attention
  15. clear_result_ready
  16. clear_data_ready
  17. clear_param_reg
  18. read_status_register
  19. read_result_register
  20. read_data_register
  21. write_param
  22. write_cmd
  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. input_data
  37. read_data_block
  38. do_cdu31a_request
  39. mcovlp
  40. sony_get_toc
  41. find_track
  42. read_subcode
  43. sony_get_subchnl_info
  44. read_audio_data
  45. read_audio
  46. do_sony_cd_cmd_chk
  47. scd_ioctl
  48. scd_open
  49. scd_release
  50. get_drive_configuration
  51. cdu31a_setup
  52. cdu31a_init

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

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