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

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