root/drivers/block/cdu31a.c

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

DEFINITIONS

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

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