root/drivers/block/cdu31a.c

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

DEFINITIONS

This source file includes following definitions.
  1. scd_disk_change
  2. enable_interrupts
  3. disable_interrupts
  4. cdu31a_interrupt
  5. sony_sleep
  6. is_attention
  7. is_busy
  8. is_data_ready
  9. is_data_requested
  10. is_result_ready
  11. is_param_write_rdy
  12. is_result_reg_not_empty
  13. reset_drive
  14. clear_attention
  15. clear_result_ready
  16. clear_data_ready
  17. clear_param_reg
  18. read_status_register
  19. read_result_register
  20. read_data_register
  21. write_param
  22. write_cmd
  23. set_drive_params
  24. restart_on_error
  25. write_params
  26. get_result
  27. do_sony_cd_cmd
  28. handle_sony_cd_attention
  29. int_to_bcd
  30. bcd_to_int
  31. log_to_msf
  32. msf_to_log
  33. size_to_buf
  34. start_request
  35. abort_read
  36. input_data
  37. read_data_block
  38. do_cdu31a_request
  39. mcovlp
  40. sony_get_toc
  41. find_track
  42. read_subcode
  43. sony_get_subchnl_info
  44. read_audio_data
  45. read_audio
  46. scd_ioctl
  47. scd_open
  48. scd_release
  49. get_drive_configuration
  50. cdu31a_setup
  51. cdu31a_init

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

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