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

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