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. sony_sleep
  5. is_attention
  6. is_busy
  7. is_data_ready
  8. is_data_requested
  9. is_result_ready
  10. is_param_write_rdy
  11. is_result_reg_not_empty
  12. reset_drive
  13. clear_attention
  14. clear_result_ready
  15. clear_data_ready
  16. clear_param_reg
  17. read_status_register
  18. read_result_register
  19. read_data_register
  20. write_param
  21. write_cmd
  22. cdu31a_interrupt
  23. set_drive_params
  24. restart_on_error
  25. write_params
  26. get_result
  27. do_sony_cd_cmd
  28. handle_sony_cd_attention
  29. int_to_bcd
  30. bcd_to_int
  31. log_to_msf
  32. msf_to_log
  33. size_to_buf
  34. start_request
  35. abort_read
  36. handle_abort_timeout
  37. input_data
  38. read_data_block
  39. do_cdu31a_request
  40. mcovlp
  41. sony_get_toc
  42. find_track
  43. read_subcode
  44. sony_get_subchnl_info
  45. read_audio_data
  46. read_audio
  47. do_sony_cd_cmd_chk
  48. scd_ioctl
  49. scd_open
  50. scd_release
  51. get_drive_configuration
  52. cdu31a_setup
  53. cdu31a_init
  54. cleanup_module

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

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