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

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