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. handle_abort_timeout
  37. input_data
  38. read_data_block
  39. do_cdu31a_request
  40. mcovlp
  41. sony_get_toc
  42. find_track
  43. read_subcode
  44. sony_get_subchnl_info
  45. read_audio_data
  46. read_audio
  47. do_sony_cd_cmd_chk
  48. scd_ioctl
  49. scd_open
  50. scd_release
  51. get_drive_configuration
  52. cdu31a_setup
  53. cdu31a_init

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

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