root/drivers/block/cdu31a.c

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

DEFINITIONS

This source file includes following definitions.
  1. scd_disk_change
  2. enable_interrupts
  3. disable_interrupts
  4. cdu31a_interrupt
  5. sony_sleep
  6. is_attention
  7. is_busy
  8. is_data_ready
  9. is_data_requested
  10. is_result_ready
  11. is_param_write_rdy
  12. 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. set_drive_params
  23. restart_on_error
  24. write_params
  25. get_result
  26. read_data_dma
  27. read_data_block
  28. get_data
  29. do_sony_cd_cmd
  30. handle_sony_cd_attention
  31. int_to_bcd
  32. bcd_to_int
  33. log_to_msf
  34. msf_to_log
  35. size_to_buf
  36. do_cdu31a_request
  37. sony_get_toc
  38. find_track
  39. read_subcode
  40. sony_get_subchnl_info
  41. scd_ioctl
  42. scd_open
  43. scd_release
  44. get_drive_configuration
  45. cdu31a_init

   1 /*
   2  * Sony CDU-31A CDROM interface device driver.
   3  *
   4  * Corey Minyard (minyard@wf-rch.cirr.com)
   5  *
   6  * Colossians 3:17
   7  *
   8  * The Sony interface device driver handles Sony interface CDROM
   9  * drives and provides a complete block-level interface as well as an
  10  * ioctl() interface compatible with the Sun (as specified in
  11  * include/linux/cdrom.h).  With this interface, CDROMs can be
  12  * accessed and standard audio CDs can be played back normally.
  13  *
  14  * This interface is (unfortunately) a polled interface.  This is
  15  * because most Sony interfaces are set up with DMA and interrupts
  16  * disables.  Some (like mine) do not even have the capability to
  17  * handle interrupts or DMA.  For this reason you will see a lot of
  18  * the following:
  19  *
  20  *   retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
  21  *   while ((retry_count > jiffies) && (! <some condition to wait for))
  22  *   {
  23  *      while (handle_sony_cd_attention())
  24  *         ;
  25  *
  26  *      sony_sleep();
  27  *   }
  28  *   if (the condition not met)
  29  *   {
  30  *      return an error;
  31  *   }
  32  *
  33  * This ugly hack waits for something to happen, sleeping a little
  34  * between every try.  it also handles attentions, which are
  35  * asynchronous events from the drive informing the driver that a disk
  36  * has been inserted, removed, etc.
  37  *
  38  * NEWS FLASH - The driver now supports interrupts and DMA, but they are
  39  * turned off by default.  Use of interrupts is highly encouraged, it
  40  * cuts CPU usage down to a reasonable level.  For a single-speed drive,
  41  * DMA is ok, but the 8-bit DMA cannot keep up with the double speed
  42  * drives.
  43  *
  44  * One thing about these drives: They talk in MSF (Minute Second Frame) format.
  45  * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
  46  * disk.  The funny thing is that these are sent to the drive in BCD, but the
  47  * interface wants to see them in decimal.  A lot of conversion goes on.
  48  *
  49  *  Copyright (C) 1993  Corey Minyard
  50  *
  51  *  This program is free software; you can redistribute it and/or modify
  52  *  it under the terms of the GNU General Public License as published by
  53  *  the Free Software Foundation; either version 2 of the License, or
  54  *  (at your option) any later version.
  55  *
  56  *  This program is distributed in the hope that it will be useful,
  57  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  58  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  59  *  GNU General Public License for more details.
  60  *
  61  *  You should have received a copy of the GNU General Public License
  62  *  along with this program; if not, write to the Free Software
  63  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  64  *
  65  */
  66 
  67 /*
  68  *
  69  * Setting up the Sony CDU31A/CDU33A drive interface card.  If
  70  * You have another card, you are on your own.
  71  * 
  72  *      +----------+-----------------+----------------------+
  73  *      |  JP1     |  34 Pin Conn    |                      |
  74  *      |  JP2     +-----------------+                      |
  75  *      |  JP3                                              |
  76  *      |  JP4                                              |
  77  *      |                                                   +--+
  78  *      |                                                   |  +-+
  79  *      |                                                   |  | |  External
  80  *      |                                                   |  | |  Connector
  81  *      |                                                   |  | |
  82  *      |                                                   |  +-+
  83  *      |                                                   +--+
  84  *      |                                                   |
  85  *      |                                          +--------+
  86  *      |                                          |
  87  *      +------------------------------------------+
  88  * 
  89  *    JP1 sets the Base Address, using the following settings:
  90  * 
  91  *      Address         Pin 1           Pin 2
  92  *      -------         -----           -----
  93  *      0x320           Short           Short
  94  *      0x330           Short           Open
  95  *      0x340           Open            Short
  96  *      0x360           Open            Open
  97  * 
  98  *    JP2 and JP3 configure the DMA channel; they must be set the same.
  99  * 
 100  *      DMA             Pin 1           Pin 2           Pin 3
 101  *      ---             -----           -----           -----
 102  *      1               On              Off             On
 103  *      2               Off             On              Off
 104  *      3               Off             Off             On
 105  * 
 106  *    JP4 Configures the IRQ:
 107  * 
 108  *      IRQ     Pin 1           Pin 2           Pin 3           Pin 4
 109  *      ---     -----           -----           -----           -----
 110  *      3       Off             Off             On              Off
 111  *      4       Off             Off*            Off             On
 112  *      5       On              Off             Off             Off
 113  *      6       Off             On              Off             Off
 114  * 
 115  *              * The documentation states to set this for interrupt
 116  *                4, but I think that is a mistake.
 117  */
 118 
 119 #include <linux/errno.h>
 120 #include <linux/signal.h>
 121 #include <linux/sched.h>
 122 #include <linux/timer.h>
 123 #include <linux/fs.h>
 124 #include <linux/kernel.h>
 125 #include <linux/hdreg.h>
 126 #include <linux/genhd.h>
 127 #include <linux/ioport.h>
 128 #include <linux/string.h>
 129 
 130 #include <asm/system.h>
 131 #include <asm/io.h>
 132 #include <asm/segment.h>
 133 #include <asm/dma.h>
 134 
 135 #include <linux/cdrom.h>
 136 #include <linux/cdu31a.h>
 137 
 138 #define MAJOR_NR CDU31A_CDROM_MAJOR
 139 #include "blk.h"
 140 
 141 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
 142 
 143 /* Define the following if you have data corruption problems. */
 144 #undef SONY_POLL_EACH_BYTE
 145 
 146 /*
 147 ** Edit the following data to change interrupts, DMA channels, etc.
 148 ** Default is polled and no DMA.  DMA is not recommended for double-speed
 149 ** drives.
 150 */
 151 static struct
 152 {
 153    unsigned short base;         /* I/O Base Address */
 154    short          dma_num;      /* DMA Number (-1 means no DMA) */
 155    short          int_num;      /* Interrupt Number (-1 means scan for it,
 156                                    0 means don't use) */
 157 } cdu31a_addresses[] =
 158 {
 159    { 0x340,     -1,     0 },    /* Standard configuration Sony Interface */
 160    { 0x1f88,    -1,     0 },    /* Fusion CD-16 */
 161    { 0x230,     -1,     0 },    /* SoundBlaster 16 card */
 162    { 0x360,     -1,     0 },    /* Secondary standard Sony Interface */
 163    { 0x320,     -1,     0 },    /* Secondary standard Sony Interface */
 164    { 0x330,     -1,     0 },    /* Secondary standard Sony Interface */
 165    { 0x634,     -1,     0 },    /* Sound FX SC400 */
 166    { 0x654,     -1,     0 },    /* Sound FX SC400 */
 167    { 0 }
 168 };
 169 
 170 static int handle_sony_cd_attention(void);
 171 static int read_subcode(void);
 172 static void sony_get_toc(void);
 173 static int scd_open(struct inode *inode, struct file *filp);
 174 static void do_sony_cd_cmd(unsigned char cmd,
 175                            unsigned char *params,
 176                            unsigned int num_params,
 177                            unsigned char *result_buffer,
 178                            unsigned int *result_size);
 179 static void size_to_buf(unsigned int size,
 180                         unsigned char *buf);
 181 
 182 
 183 /* The base I/O address of the Sony Interface.  This is a variable (not a
 184    #define) so it can be easily changed via some future ioctl() */
 185 static unsigned short sony_cd_base_io = 0;
 186 
 187 /*
 188  * The following are I/O addresses of the various registers for the drive.  The
 189  * comment for the base address also applies here.
 190  */
 191 static volatile unsigned short sony_cd_cmd_reg;
 192 static volatile unsigned short sony_cd_param_reg;
 193 static volatile unsigned short sony_cd_write_reg;
 194 static volatile unsigned short sony_cd_control_reg;
 195 static volatile unsigned short sony_cd_status_reg;
 196 static volatile unsigned short sony_cd_result_reg;
 197 static volatile unsigned short sony_cd_read_reg;
 198 static volatile unsigned short sony_cd_fifost_reg;
 199 
 200 
 201 static int sony_disc_changed = 1;          /* Has the disk been changed
 202                                               since the last check? */
 203 static int sony_toc_read = 0;              /* Has the table of contents been
 204                                               read? */
 205 static int sony_spun_up = 0;               /* Has the drive been spun up? */
 206 static unsigned int sony_buffer_size;      /* Size in bytes of the read-ahead
 207                                               buffer. */
 208 static unsigned int sony_buffer_sectors;   /* Size (in 2048 byte records) of
 209                                               the read-ahead buffer. */
 210 static unsigned int sony_usage = 0;        /* How many processes have the
 211                                               drive open. */
 212 
 213 static volatile int sony_first_block = -1; /* First OS block (512 byte) in
 214                                               the read-ahead buffer */
 215 static volatile int sony_last_block = -1;  /* Last OS block (512 byte) in
 216                                               the read-ahead buffer */
 217 
 218 static struct s_sony_toc *sony_toc;              /* Points to the table of
 219                                                     contents. */
 220 static struct s_sony_subcode * volatile last_sony_subcode; /* Points to the last
 221                                                     subcode address read */
 222 static unsigned char * volatile sony_buffer;     /* Points to the read-ahead
 223                                                     buffer */
 224 
 225 static volatile int sony_inuse = 0;  /* Is the drive in use?  Only one operation at a time
 226                                         allowed */
 227 
 228 static struct wait_queue * sony_wait = NULL;
 229 
 230 static struct task_struct *has_cd_task = NULL;  /* The task that is currently using the
 231                                                    CDROM drive, or NULL if none. */
 232 
 233 static int is_double_speed = 0; /* Is the drive a CDU33A? */
 234 
 235 /*
 236  * The audio status uses the values from read subchannel data as specified
 237  * in include/linux/cdrom.h.
 238  */
 239 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
 240 
 241 /*
 242  * The following are a hack for pausing and resuming audio play.  The drive
 243  * does not work as I would expect it, if you stop it then start it again,
 244  * the drive seeks back to the beginning and starts over.  This holds the
 245  * position during a pause so a resume can restart it.  It uses the
 246  * audio status variable above to tell if it is paused.
 247  */
 248 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
 249 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
 250 
 251 static int irq_used = -1;
 252 static int dma_channel = -1;
 253 static struct wait_queue *cdu31a_irq_wait = NULL;
 254 
 255 static int curr_control_reg = 0; /* Current value of the control register */
 256 
 257 /*
 258  * This routine returns 1 if the disk has been changed since the last
 259  * check or 0 if it hasn't.
 260  */
 261 static int
 262 scd_disk_change(dev_t full_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264    int retval, target;
 265 
 266 
 267    target = MINOR(full_dev);
 268 
 269    if (target > 0) {
 270       printk("Sony CD-ROM request error: invalid device.\n");
 271       return 0;
 272    }
 273 
 274    retval = sony_disc_changed;
 275    sony_disc_changed = 0;
 276 
 277    return retval;
 278 }
 279 
 280 static inline void
 281 enable_interrupts(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283    curr_control_reg |= (  SONY_ATTN_INT_EN_BIT
 284                         | SONY_RES_RDY_INT_EN_BIT
 285                         | SONY_DATA_RDY_INT_EN_BIT);
 286    outb(curr_control_reg, sony_cd_control_reg);
 287 }
 288 
 289 static inline void
 290 disable_interrupts(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292    curr_control_reg &= ~(  SONY_ATTN_INT_EN_BIT
 293                          | SONY_RES_RDY_INT_EN_BIT
 294                          | SONY_DATA_RDY_INT_EN_BIT);
 295    outb(curr_control_reg, sony_cd_control_reg);
 296 }
 297 
 298 static void
 299 cdu31a_interrupt(int unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301    disable_interrupts();
 302    wake_up(&cdu31a_irq_wait);
 303 }
 304 
 305 /*
 306  * Wait a little while (used for polling the drive).  If in initialization,
 307  * setting a timeout doesn't work, so just loop for a while.
 308  */
 309 static inline void
 310 sony_sleep(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 311 {
 312    if (irq_used <= 0)
 313    {
 314       current->state = TASK_INTERRUPTIBLE;
 315       current->timeout = jiffies;
 316       schedule();
 317    }
 318    else /* Interrupt driven */
 319    {
 320       cli();
 321       enable_interrupts();
 322       interruptible_sleep_on(&cdu31a_irq_wait);
 323       sti();
 324    }
 325 }
 326 
 327 
 328 /*
 329  * The following are convenience routine to read various status and set
 330  * various conditions in the drive.
 331  */
 332 static inline int
 333 is_attention(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 334 {
 335    return((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
 336 }
 337 
 338 static inline int
 339 is_busy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 340 {
 341    return((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
 342 }
 343 
 344 static inline int
 345 is_data_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {
 347    return((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
 348 }
 349 
 350 static inline int
 351 is_data_requested(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 352 {
 353    return((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
 354 }
 355 
 356 static inline int
 357 is_result_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 358 {
 359    return((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
 360 }
 361 
 362 static inline int
 363 is_param_write_rdy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 364 {
 365    return((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
 366 }
 367 
 368 static inline void
 369 reset_drive(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 370 {
 371    curr_control_reg = 0;
 372    outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
 373 }
 374 
 375 static inline void
 376 clear_attention(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 377 {
 378    outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
 379 }
 380 
 381 static inline void
 382 clear_result_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 383 {
 384    outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
 385 }
 386 
 387 static inline void
 388 clear_data_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 389 {
 390    outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT, sony_cd_control_reg);
 391 }
 392 
 393 static inline void
 394 clear_param_reg(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 395 {
 396    outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
 397 }
 398 
 399 static inline unsigned char
 400 read_status_register(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402    return(inb(sony_cd_status_reg));
 403 }
 404 
 405 static inline unsigned char
 406 read_result_register(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 407 {
 408    return(inb(sony_cd_result_reg));
 409 }
 410 
 411 static inline unsigned char
 412 read_data_register(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 413 {
 414    return(inb(sony_cd_read_reg));
 415 }
 416 
 417 static inline void
 418 write_param(unsigned char param)
     /* [previous][next][first][last][top][bottom][index][help] */
 419 {
 420    outb(param, sony_cd_param_reg);
 421 }
 422 
 423 static inline void
 424 write_cmd(unsigned char cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 425 {
 426    outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT, sony_cd_control_reg);
 427    outb(cmd, sony_cd_cmd_reg);
 428 }
 429 
 430 /*
 431  * Set the drive parameters so the drive will auto-spin-up when a
 432  * disk is inserted.
 433  */
 434 static void
 435 set_drive_params(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 436 {
 437    unsigned char res_reg[2];
 438    unsigned int res_size;
 439    unsigned char params[3];
 440 
 441 
 442    params[0] = SONY_SD_MECH_CONTROL;
 443    params[1] = 0x03; /* Set auto spin up and auto eject */
 444    if (is_double_speed)
 445    {
 446       params[1] |= 0x04; /* Set the drive to double speed if possible */
 447    }
 448    do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
 449                   params,
 450                   2,
 451                   res_reg,
 452                   &res_size);
 453    if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
 454    {
 455       printk("  Unable to set mechanical parameters: 0x%2.2x\n", res_reg[1]);
 456    }
 457 }
 458 
 459 /*
 460  * This code will reset the drive and attempt to restore sane parameters.
 461  */
 462 static void
 463 restart_on_error(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 464 {
 465    unsigned char res_reg[2];
 466    unsigned int res_size;
 467    unsigned int retry_count;
 468 
 469 
 470    printk("cdu31a: Resetting drive on error\n");
 471    reset_drive();
 472    retry_count = jiffies + SONY_RESET_TIMEOUT;
 473    while ((retry_count > jiffies) && (!is_attention()))
 474    {
 475       sony_sleep();
 476    }
 477    set_drive_params();
 478    do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
 479    if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
 480    {
 481       printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg[1]);
 482    }
 483 
 484    current->state = TASK_INTERRUPTIBLE;
 485    current->timeout = jiffies + 200;
 486    schedule();
 487 
 488    do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
 489    if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
 490    {
 491       printk("cdu31a: Unable to read TOC: 0x%2.2x\n", res_reg[1]);
 492    }
 493    sony_get_toc();
 494    if (!sony_toc_read)
 495    {
 496       printk("cdu31a: Unable to get TOC data\n");
 497    }
 498 }
 499 
 500 /*
 501  * This routine writes data to the parameter register.  Since this should
 502  * happen fairly fast, it is polled with no OS waits between.
 503  */
 504 static int
 505 write_params(unsigned char *params,
     /* [previous][next][first][last][top][bottom][index][help] */
 506              int num_params)
 507 {
 508    unsigned int retry_count;
 509 
 510 
 511    retry_count = SONY_READY_RETRIES;
 512    while ((retry_count > 0) && (!is_param_write_rdy()))
 513    {
 514       retry_count--;
 515    }
 516    if (!is_param_write_rdy())
 517    {
 518       return -EIO;
 519    }
 520 
 521    while (num_params > 0)
 522    {
 523       write_param(*params);
 524       params++;
 525       num_params--;
 526    }
 527 
 528    return 0;
 529 }
 530 
 531 
 532 /*
 533  * The following reads data from the command result register.  It is a
 534  * fairly complex routine, all status info flows back through this
 535  * interface.  The algorithm is stolen directly from the flowcharts in
 536  * the drive manual.
 537  */
 538 static void
 539 get_result(unsigned char *result_buffer,
     /* [previous][next][first][last][top][bottom][index][help] */
 540            unsigned int *result_size)
 541 {
 542    unsigned char a, b;
 543    int i;
 544    unsigned int retry_count;
 545 
 546 
 547    while (handle_sony_cd_attention())
 548       ;
 549    /* Wait for the result data to be ready */
 550    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 551    while ((retry_count > jiffies) && (is_busy() || (!(is_result_ready()))))
 552    {
 553       sony_sleep();
 554 
 555       while (handle_sony_cd_attention())
 556          ;
 557    }
 558    if (is_busy() || (!(is_result_ready())))
 559    {
 560       result_buffer[0] = 0x20;
 561       result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 562       *result_size = 2;
 563       return;
 564    }
 565 
 566    /*
 567     * Get the first two bytes.  This determines what else needs
 568     * to be done.
 569     */
 570    clear_result_ready();
 571    a = read_result_register();
 572    *result_buffer = a;
 573    result_buffer++;
 574    b = read_result_register();
 575    *result_buffer = b;
 576    result_buffer++;
 577    *result_size = 2;
 578 
 579    /*
 580     * 0x20 means an error occured.  Byte 2 will have the error code.
 581     * Otherwise, the command succeeded, byte 2 will have the count of
 582     * how many more status bytes are coming.
 583     *
 584     * The result register can be read 10 bytes at a time, a wait for
 585     * result ready to be asserted must be done between every 10 bytes.
 586     */
 587    if ((a & 0xf0) != 0x20)
 588    {
 589       if (b > 8)
 590       {
 591          for (i=0; i<8; i++)
 592          {
 593             *result_buffer = read_result_register();
 594             result_buffer++;
 595             (*result_size)++;
 596          }
 597          b = b - 8;
 598 
 599          while (b > 10)
 600          {
 601             retry_count = SONY_READY_RETRIES;
 602             while ((retry_count > 0) && (!is_result_ready()))
 603             {
 604                retry_count--;
 605             }
 606             if (!is_result_ready())
 607             {
 608                result_buffer[0] = 0x20;
 609                result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 610                *result_size = 2;
 611                return;
 612             }
 613 
 614             clear_result_ready();
 615                                 
 616             for (i=0; i<10; i++)
 617             {
 618                *result_buffer = read_result_register();
 619                result_buffer++;
 620                (*result_size)++;
 621             }
 622             b = b - 10;
 623          }
 624 
 625          if (b > 0)
 626          {
 627             retry_count = SONY_READY_RETRIES;
 628             while ((retry_count > 0) && (!is_result_ready()))
 629             {
 630                retry_count--;
 631             }
 632             if (!is_result_ready())
 633             {
 634                result_buffer[0] = 0x20;
 635                result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 636                *result_size = 2;
 637                return;
 638             }
 639          }
 640       }
 641 
 642       while (b > 0)
 643       {
 644          *result_buffer = read_result_register();
 645          result_buffer++;
 646          (*result_size)++;
 647          b--;
 648       }
 649    }
 650 }
 651 
 652 static void
 653 read_data_dma(unsigned char *data,
     /* [previous][next][first][last][top][bottom][index][help] */
 654               unsigned int  data_size,
 655               unsigned char *result_buffer,
 656               unsigned int  *result_size)
 657 {
 658    unsigned int retry_count;
 659 
 660 
 661    cli();
 662    disable_dma(dma_channel);
 663    clear_dma_ff(dma_channel);
 664    set_dma_mode(dma_channel, DMA_MODE_READ);
 665    set_dma_addr(dma_channel, (int) data);
 666    set_dma_count(dma_channel, data_size);
 667    enable_dma(dma_channel);
 668    sti();
 669 
 670    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 671    while (   (retry_count > jiffies)
 672           && (!is_data_ready())
 673           && (!is_result_ready()))
 674    {
 675       while (handle_sony_cd_attention())
 676          ;
 677          
 678       sony_sleep();
 679    }
 680    if (!is_data_requested())
 681    {
 682       result_buffer[0] = 0x20;
 683       result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 684       *result_size = 2;
 685       return;
 686    }
 687 }
 688 
 689 /*
 690  * Read in a 2048 byte block of data.
 691  */
 692 static void
 693 read_data_block(unsigned char *data,
     /* [previous][next][first][last][top][bottom][index][help] */
 694                 unsigned int  data_size,
 695                 unsigned char *result_buffer,
 696                 unsigned int  *result_size)
 697 {
 698 #ifdef SONY_POLL_EACH_BYTE
 699    int i;
 700    unsigned int retry_count;
 701 
 702    for (i=0; i<data_size; i++)
 703    {
 704       retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 705       while ((retry_count > jiffies) && (!is_data_requested()))
 706       {
 707          while (handle_sony_cd_attention())
 708             ;
 709          
 710          sony_sleep();
 711       }
 712       if (!is_data_requested())
 713       {
 714          result_buffer[0] = 0x20;
 715          result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 716          *result_size = 2;
 717          return;
 718       }
 719       
 720       *data = read_data_register();
 721       data++;
 722    }
 723 #else
 724    insb(sony_cd_read_reg, data, data_size);
 725 #endif
 726 }
 727 
 728 /*
 729  * This routine issues a read data command and gets the data.  I don't
 730  * really like the way this is done (I would prefer for do_sony_cmd() to
 731  * handle it automatically) but I found that the drive returns status
 732  * when it finishes reading (not when the host has read all the data)
 733  * or after it gets an error.  This means that the status can be
 734  * received at any time and should be handled immediately (at least
 735  * between every 2048 byte block) to check for errors, we can't wait
 736  * until all the data is read.
 737  *
 738  * This routine returns the total number of sectors read.  It will
 739  * not return an error if it reads at least one sector successfully.
 740  */
 741 static unsigned int
 742 get_data(unsigned char *orig_data,
     /* [previous][next][first][last][top][bottom][index][help] */
 743          unsigned char *params,         /* 6 bytes with the MSF start address
 744                                            and number of sectors to read. */
 745          unsigned int orig_data_size,
 746          unsigned char *result_buffer,
 747          unsigned int *result_size)
 748 {
 749    unsigned int cur_offset;
 750    unsigned int retry_count;
 751    int result_read;
 752    int num_retries;
 753    unsigned int num_sectors_read = 0;
 754    unsigned char *data = orig_data;
 755    unsigned int data_size = orig_data_size;
 756 
 757 
 758    num_retries = 0;
 759 retry_data_operation:
 760    result_buffer[0] = 0;
 761    result_buffer[1] = 0;
 762 
 763    /*
 764     * Clear any outstanding attentions and wait for the drive to
 765     * complete any pending operations.
 766     */
 767    while (handle_sony_cd_attention())
 768       ;
 769 
 770    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 771    while ((retry_count > jiffies) && (is_busy()))
 772    {
 773       sony_sleep();
 774       
 775       while (handle_sony_cd_attention())
 776          ;
 777    }
 778 
 779    if (is_busy())
 780    {
 781       result_buffer[0] = 0x20;
 782       result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 783       *result_size = 2;
 784    }
 785    else
 786    {
 787       /* Issue the command */
 788       clear_result_ready();
 789       clear_param_reg();
 790 
 791       write_params(params, 6);
 792       write_cmd(SONY_READ_CMD);
 793 
 794       /*
 795        * Read the data from the drive one 2048 byte sector at a time.  Handle
 796        * any results received between sectors, if an error result is returned
 797        * terminate the operation immediately.
 798        */
 799       cur_offset = 0;
 800       result_read = 0;
 801       while ((data_size > 0) && (result_buffer[0] == 0))
 802       {
 803          /* Wait for the drive to tell us we have something */
 804          retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 805          while ((retry_count > jiffies) && (!(is_result_ready() || is_data_ready())))
 806          {
 807             while (handle_sony_cd_attention())
 808                ;
 809 
 810             sony_sleep();
 811          }
 812          if (!(is_result_ready() || is_data_ready()))
 813          {
 814             result_buffer[0] = 0x20;
 815             result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 816             *result_size = 2;
 817          }
 818       
 819          /* Handle results first */
 820          else if (is_result_ready())
 821          {
 822             result_read = 1;
 823             get_result(result_buffer, result_size);
 824          }
 825          /* Handle data next */
 826          else if (dma_channel > 0)
 827          {
 828             clear_data_ready();
 829             read_data_dma(data, 2048, result_buffer, result_size);
 830             data += 2048;
 831             data_size -= 2048;
 832             cur_offset = cur_offset + 2048;
 833             num_sectors_read++;
 834          }
 835          else
 836          {
 837             /*
 838              * The drive has to be polled for status on a byte-by-byte basis
 839              * to know if the data is ready.  Yuck.  I really wish I could use
 840              * DMA all the time.
 841              *
 842              * NEWS FLASH - I am no longer polling on a byte-by-byte basis.
 843              * It seems to work ok, but the spec says you shouldn't.
 844              */
 845             clear_data_ready();
 846             read_data_block(data, 2048, result_buffer, result_size);
 847             data += 2048;
 848             data_size -= 2048;
 849             cur_offset = cur_offset + 2048;
 850             num_sectors_read++;
 851          }
 852       }
 853 
 854       /* Make sure the result has been read */
 855       if (!result_read)
 856       {
 857          get_result(result_buffer, result_size);
 858       }
 859    }
 860 
 861    if (   ((result_buffer[0] & 0x20) == 0x20)
 862        && (result_buffer[1] != SONY_NOT_SPIN_ERR) /* No retry when not spin */
 863        && (num_retries < MAX_CDU31A_RETRIES))
 864    {
 865       /*
 866        * If an error occurs, go back and only read one sector at the
 867        * given location.  Hopefully the error occurred on an unused
 868        * sector after the first one.  It is hard to say which sector
 869        * the error occurred on because the drive returns status before
 870        * the data transfer is finished and doesn't say which sector.
 871        */
 872       data_size = 2048;
 873       data = orig_data;
 874       num_sectors_read = 0;
 875       size_to_buf(1, &params[3]);
 876 
 877       num_retries++;
 878       /* Issue a reset on an error (the second time), otherwise just delay */
 879       if (num_retries == 2)
 880       {
 881          restart_on_error();
 882       }
 883       else
 884       {
 885          current->state = TASK_INTERRUPTIBLE;
 886          current->timeout = jiffies + 10;
 887          schedule();
 888       }
 889 
 890       /* Restart the operation. */
 891       goto retry_data_operation;
 892    }
 893 
 894    return(num_sectors_read);
 895 }
 896 
 897 
 898 /*
 899  * Do a command that does not involve data transfer.  This routine must
 900  * be re-entrant from the same task to support being called from the
 901  * data operation code when an error occurs.
 902  */
 903 static void
 904 do_sony_cd_cmd(unsigned char cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
 905                unsigned char *params,
 906                unsigned int num_params,
 907                unsigned char *result_buffer,
 908                unsigned int *result_size)
 909 {
 910    unsigned int retry_count;
 911    int num_retries;
 912    int recursive_call;
 913 
 914 
 915    cli();
 916    if (current != has_cd_task) /* Allow recursive calls to this routine */
 917    {
 918       while (sony_inuse)
 919       {
 920          interruptible_sleep_on(&sony_wait);
 921          if (current->signal & ~current->blocked)
 922          {
 923             result_buffer[0] = 0x20;
 924             result_buffer[1] = SONY_SIGNAL_OP_ERR;
 925             *result_size = 2;
 926             return;
 927          }
 928       }
 929       sony_inuse = 1;
 930       has_cd_task = current;
 931       recursive_call = 0;
 932    }
 933    else
 934    {
 935       recursive_call = 1;
 936    }
 937    sti();
 938 
 939    num_retries = 0;
 940 retry_cd_operation:
 941 
 942    while (handle_sony_cd_attention())
 943       ;
 944    
 945    retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 946    while ((retry_count > jiffies) && (is_busy()))
 947    {
 948       sony_sleep();
 949       
 950       while (handle_sony_cd_attention())
 951          ;
 952    }
 953    if (is_busy())
 954    {
 955       result_buffer[0] = 0x20;
 956       result_buffer[1] = SONY_TIMEOUT_OP_ERR;
 957       *result_size = 2;
 958    }
 959    else
 960    {
 961       clear_result_ready();
 962       clear_param_reg();
 963 
 964       write_params(params, num_params);
 965       write_cmd(cmd);
 966 
 967       get_result(result_buffer, result_size);
 968    }
 969 
 970    if (   ((result_buffer[0] & 0x20) == 0x20)
 971        && (num_retries < MAX_CDU31A_RETRIES))
 972    {
 973       num_retries++;
 974       current->state = TASK_INTERRUPTIBLE;
 975       current->timeout = jiffies + 10; /* Wait .1 seconds on retries */
 976       schedule();
 977       goto retry_cd_operation;
 978    }
 979 
 980    if (!recursive_call)
 981    {
 982       has_cd_task = NULL;
 983       sony_inuse = 0;
 984       wake_up_interruptible(&sony_wait);
 985    }
 986 }
 987 
 988 
 989 /*
 990  * Handle an attention from the drive.  This will return 1 if it found one
 991  * or 0 if not (if one is found, the caller might want to call again).
 992  *
 993  * This routine counts the number of consecutive times it is called
 994  * (since this is always called from a while loop until it returns
 995  * a 0), and returns a 0 if it happens too many times.  This will help
 996  * prevent a lockup.
 997  */
 998 static int
 999 handle_sony_cd_attention(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1000 {
1001    unsigned char atten_code;
1002    static int num_consecutive_attentions = 0;
1003 
1004 
1005    if (is_attention())
1006    {
1007       if (num_consecutive_attentions > CDU31A_MAX_CONSECUTIVE_ATTENTIONS)
1008       {
1009          printk("cdu31a: Too many consecutive attentions: %d\n",
1010                 num_consecutive_attentions);
1011          num_consecutive_attentions = 0;
1012          return(0);
1013       }
1014 
1015       clear_attention();
1016       atten_code = read_result_register();
1017 
1018       switch (atten_code)
1019       {
1020        /* Someone changed the CD.  Mark it as changed */
1021       case SONY_MECH_LOADED_ATTN:
1022          sony_disc_changed = 1;
1023          sony_toc_read = 0;
1024          sony_audio_status = CDROM_AUDIO_NO_STATUS;
1025          sony_first_block = -1;
1026          sony_last_block = -1;
1027          break;
1028 
1029       case SONY_AUDIO_PLAY_DONE_ATTN:
1030          sony_audio_status = CDROM_AUDIO_COMPLETED;
1031          read_subcode();
1032          break;
1033 
1034       case SONY_EJECT_PUSHED_ATTN:
1035          sony_audio_status = CDROM_AUDIO_INVALID;
1036          break;
1037 
1038       case SONY_LEAD_IN_ERR_ATTN:
1039       case SONY_LEAD_OUT_ERR_ATTN:
1040       case SONY_DATA_TRACK_ERR_ATTN:
1041       case SONY_AUDIO_PLAYBACK_ERR_ATTN:
1042          sony_audio_status = CDROM_AUDIO_ERROR;
1043          break;
1044       }
1045 
1046       num_consecutive_attentions++;
1047       return(1);
1048    }
1049 
1050    num_consecutive_attentions = 0;
1051    return(0);
1052 }
1053 
1054 
1055 /* Convert from an integer 0-99 to BCD */
1056 static inline unsigned int
1057 int_to_bcd(unsigned int val)
     /* [previous][next][first][last][top][bottom][index][help] */
1058 {
1059    int retval;
1060 
1061 
1062    retval = (val / 10) << 4;
1063    retval = retval | val % 10;
1064    return(retval);
1065 }
1066 
1067 
1068 /* Convert from BCD to an integer from 0-99 */
1069 static unsigned int
1070 bcd_to_int(unsigned int bcd)
     /* [previous][next][first][last][top][bottom][index][help] */
1071 {
1072    return((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1073 }
1074 
1075 
1076 /*
1077  * Convert a logical sector value (like the OS would want to use for
1078  * a block device) to an MSF format.
1079  */
1080 static void
1081 log_to_msf(unsigned int log, unsigned char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
1082 {
1083    log = log + LOG_START_OFFSET;
1084    msf[0] = int_to_bcd(log / 4500);
1085    log = log % 4500;
1086    msf[1] = int_to_bcd(log / 75);
1087    msf[2] = int_to_bcd(log % 75);
1088 }
1089 
1090 
1091 /*
1092  * Convert an MSF format to a logical sector.
1093  */
1094 static unsigned int
1095 msf_to_log(unsigned char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
1096 {
1097    unsigned int log;
1098 
1099 
1100    log = bcd_to_int(msf[2]);
1101    log += bcd_to_int(msf[1]) * 75;
1102    log += bcd_to_int(msf[0]) * 4500;
1103    log = log - LOG_START_OFFSET;
1104 
1105    return log;
1106 }
1107 
1108 
1109 /*
1110  * Take in integer size value and put it into a buffer like
1111  * the drive would want to see a number-of-sector value.
1112  */
1113 static void
1114 size_to_buf(unsigned int size,
     /* [previous][next][first][last][top][bottom][index][help] */
1115             unsigned char *buf)
1116 {
1117    buf[0] = size / 65536;
1118    size = size % 65536;
1119    buf[1] = size / 256;
1120    buf[2] = size % 256;
1121 }
1122 
1123 
1124 /*
1125  * The OS calls this to perform a read or write operation to the drive.
1126  * Write obviously fail.  Reads to a read ahead of sony_buffer_size
1127  * bytes to help speed operations.  This especially helps since the OS
1128  * uses 1024 byte blocks and the drive uses 2048 byte blocks.  Since most
1129  * data access on a CD is done sequentially, this saves a lot of operations.
1130  */
1131 static void
1132 do_cdu31a_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1133 {
1134    int block;
1135    unsigned int dev;
1136    int nsect;
1137    unsigned char params[10];
1138    unsigned char res_reg[12];
1139    unsigned int res_size;
1140    int copyoff;
1141    int spin_up_retry;
1142    unsigned int read_size;
1143 
1144 
1145    /* 
1146     * Make sure no one else is using the driver; wait for them
1147     * to finish if it is so.
1148     */
1149    cli();
1150    while (sony_inuse)
1151    {
1152       interruptible_sleep_on(&sony_wait);
1153       if (current->signal & ~current->blocked)
1154       {
1155          return;
1156       }
1157    }
1158    sony_inuse = 1;
1159    has_cd_task = current;
1160    sti();
1161 
1162    if (!sony_spun_up)
1163    {
1164       scd_open (NULL,NULL);
1165    }
1166 
1167    while (1)
1168    {
1169 cdu31a_request_startover:
1170       /*
1171        * The beginning here is stolen from the hard disk driver.  I hope
1172        * its right.
1173        */
1174       if (!(CURRENT) || CURRENT->dev < 0)
1175       {
1176          goto end_do_cdu31a_request;
1177       }
1178 
1179       INIT_REQUEST;
1180       dev = MINOR(CURRENT->dev);
1181       block = CURRENT->sector;
1182       nsect = CURRENT->nr_sectors;
1183       if (dev != 0)
1184       {
1185          end_request(0);
1186          goto cdu31a_request_startover;
1187       }
1188 
1189       switch(CURRENT->cmd)
1190       {
1191       case READ:
1192          /*
1193           * If the block address is invalid or the request goes beyond the end of
1194           * the media, return an error.
1195           */
1196          if ((block / 4) >= sony_toc->lead_out_start_lba)
1197          {
1198             end_request(0);
1199             goto cdu31a_request_startover;
1200          }
1201          if (((block + nsect) / 4) >= sony_toc->lead_out_start_lba)
1202          {
1203             end_request(0);
1204             goto cdu31a_request_startover;
1205          }
1206 
1207          while (nsect > 0)
1208          {
1209             /*
1210              * If the requested sector is not currently in the read-ahead buffer,
1211              * it must be read in.
1212              */
1213             if ((block < sony_first_block) || (block > sony_last_block))
1214             {
1215                sony_first_block = (block / 4) * 4;
1216                log_to_msf(block/4, params);
1217 
1218                /*
1219                 * If the full read-ahead would go beyond the end of the media, trim
1220                 * it back to read just till the end of the media.
1221                 */
1222                if (((block / 4) + sony_buffer_sectors) >= sony_toc->lead_out_start_lba)
1223                {
1224                   read_size = sony_toc->lead_out_start_lba - (block / 4);
1225                }
1226                else
1227                {
1228                   read_size = sony_buffer_sectors;
1229                }
1230                size_to_buf(read_size, &params[3]);
1231 
1232                /*
1233                 * Read the data.  If the drive was not spinning, spin it up and try
1234                 * once more.  I know, the goto is ugly, but I am too lazy to fix it.
1235                 */
1236                spin_up_retry = 0;
1237 try_read_again:
1238                sony_last_block =   sony_first_block
1239                                  + (get_data(sony_buffer,
1240                                              params,
1241                                              (read_size * 2048),
1242                                              res_reg,
1243                                              &res_size) * 4) - 1;
1244                if ((res_size < 2) || (res_reg[0] != 0))
1245                {
1246                   if ((res_reg[1] == SONY_NOT_SPIN_ERR) && (!spin_up_retry))
1247                   {
1248                      do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1249                      spin_up_retry = 1;
1250                      goto try_read_again;
1251                   }
1252 
1253                   printk("Sony CDROM Read error: 0x%2.2x\n", res_reg[1]);
1254                   sony_first_block = -1;
1255                   sony_last_block = -1;
1256                   end_request(0);
1257                   goto cdu31a_request_startover;
1258                }
1259             }
1260    
1261             /*
1262              * The data is in memory now, copy it to the buffer and advance to the
1263              * next block to read.
1264              */
1265             copyoff = (block - sony_first_block) * 512;
1266             /*
1267              * Bugfix: get_data calls handle_sony_cd_attention
1268              *         there the buffer may be declared invalid
1269              *         if the CD ist changed by setting sony_first_block = -1
1270              *         This would cause a segfault in memcpy
1271              */ 
1272             if(sony_first_block <0) goto cdu31a_request_startover;
1273  
1274             memcpy(CURRENT->buffer, sony_buffer+copyoff, 512);
1275                
1276             block += 1;
1277             nsect -= 1;
1278             CURRENT->buffer += 512;
1279          }
1280                
1281          end_request(1);
1282          break;
1283             
1284       case WRITE:
1285          end_request(0);
1286          break;
1287             
1288       default:
1289          panic("Unknown SONY CD cmd");
1290       }
1291    }
1292 
1293 end_do_cdu31a_request:
1294    has_cd_task = NULL;
1295    sony_inuse = 0;
1296    wake_up_interruptible(&sony_wait);
1297 }
1298 
1299 
1300 /*
1301  * Read the table of contents from the drive and set sony_toc_read if
1302  * successful.
1303  */
1304 static void
1305 sony_get_toc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1306 {
1307    unsigned int res_size;
1308 
1309 
1310    if (!sony_toc_read)
1311    {
1312       do_sony_cd_cmd(SONY_REQ_TOC_DATA_CMD,
1313                      NULL,
1314                      0, 
1315                      (unsigned char *) sony_toc, 
1316                      &res_size);
1317       if ((res_size < 2) || ((sony_toc->exec_status[0] & 0x20) == 0x20))
1318       {
1319          return;
1320       }
1321       sony_toc->lead_out_start_lba = msf_to_log(sony_toc->lead_out_start_msf);
1322       sony_toc_read = 1;
1323    }
1324 }
1325 
1326 
1327 /*
1328  * Search for a specific track in the table of contents.
1329  */
1330 static int
1331 find_track(int track)
     /* [previous][next][first][last][top][bottom][index][help] */
1332 {
1333    int i;
1334    int num_tracks;
1335 
1336 
1337    num_tracks = sony_toc->last_track_num + sony_toc->first_track_num + 1;
1338    for (i = 0; i < num_tracks; i++)
1339    {
1340       if (sony_toc->tracks[i].track == track)
1341       {
1342          return i;
1343       }
1344    }
1345 
1346    return -1;
1347 }
1348 
1349 
1350 /*
1351  * Read the subcode and put it int last_sony_subcode for future use.
1352  */
1353 static int
1354 read_subcode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1355 {
1356    unsigned int res_size;
1357 
1358 
1359    do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
1360                   NULL,
1361                   0, 
1362                   (unsigned char *) last_sony_subcode, 
1363                   &res_size);
1364    if ((res_size < 2) || ((last_sony_subcode->exec_status[0] & 0x20) == 0x20))
1365    {
1366       printk("Sony CDROM error 0x%2.2x (read_subcode)\n",
1367              last_sony_subcode->exec_status[1]);
1368       return -EIO;
1369    }
1370 
1371    return 0;
1372 }
1373 
1374 
1375 /*
1376  * Get the subchannel info like the CDROMSUBCHNL command wants to see it.  If
1377  * the drive is playing, the subchannel needs to be read (since it would be
1378  * changing).  If the drive is paused or completed, the subcode information has
1379  * already been stored, just use that.  The ioctl call wants things in decimal
1380  * (not BCD), so all the conversions are done.
1381  */
1382 static int
1383 sony_get_subchnl_info(long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1384 {
1385    struct cdrom_subchnl schi;
1386 
1387 
1388    /* Get attention stuff */
1389    while (handle_sony_cd_attention())
1390       ;
1391 
1392    sony_get_toc();
1393    if (!sony_toc_read)
1394    {
1395       return -EIO;
1396    }
1397 
1398    verify_area(VERIFY_READ, (char *) arg, sizeof(schi));
1399    verify_area(VERIFY_WRITE, (char *) arg, sizeof(schi));
1400 
1401    memcpy_fromfs(&schi, (char *) arg, sizeof(schi));
1402    
1403    switch (sony_audio_status)
1404    {
1405    case CDROM_AUDIO_PLAY:
1406       if (read_subcode() < 0)
1407       {
1408          return -EIO;
1409       }
1410       break;
1411 
1412    case CDROM_AUDIO_PAUSED:
1413    case CDROM_AUDIO_COMPLETED:
1414       break;
1415 
1416    case CDROM_AUDIO_NO_STATUS:
1417       schi.cdsc_audiostatus = sony_audio_status;
1418       memcpy_tofs((char *) arg, &schi, sizeof(schi));
1419       return 0;
1420       break;
1421 
1422    case CDROM_AUDIO_INVALID:
1423    case CDROM_AUDIO_ERROR:
1424    default:
1425       return -EIO;
1426    }
1427 
1428    schi.cdsc_audiostatus = sony_audio_status;
1429    schi.cdsc_adr = last_sony_subcode->address;
1430    schi.cdsc_ctrl = last_sony_subcode->control;
1431    schi.cdsc_trk = bcd_to_int(last_sony_subcode->track_num);
1432    schi.cdsc_ind = bcd_to_int(last_sony_subcode->index_num);
1433    if (schi.cdsc_format == CDROM_MSF)
1434    {
1435       schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode->abs_msf[0]);
1436       schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode->abs_msf[1]);
1437       schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode->abs_msf[2]);
1438 
1439       schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode->rel_msf[0]);
1440       schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode->rel_msf[1]);
1441       schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode->rel_msf[2]);
1442    }
1443    else if (schi.cdsc_format == CDROM_LBA)
1444    {
1445       schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode->abs_msf);
1446       schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode->rel_msf);
1447    }
1448    
1449    memcpy_tofs((char *) arg, &schi, sizeof(schi));
1450    return 0;
1451 }
1452 
1453 
1454 /*
1455  * The big ugly ioctl handler.
1456  */
1457 static int
1458 scd_ioctl(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1459           struct file  *file,
1460           unsigned int  cmd,
1461           unsigned long arg)
1462 {
1463    unsigned int dev;
1464    unsigned char res_reg[2];
1465    unsigned int res_size;
1466    unsigned char params[7];
1467    int i;
1468 
1469 
1470    if (!inode)
1471    {
1472       return -EINVAL;
1473    }
1474    dev = MINOR(inode->i_rdev) >> 6;
1475    if (dev != 0)
1476    {
1477       return -EINVAL;
1478    }
1479 
1480    switch (cmd)
1481    {
1482    case CDROMSTART:     /* Spin up the drive */
1483       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1484       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1485       {
1486          printk("Sony CDROM error 0x%2.2x (CDROMSTART)\n", res_reg[1]);
1487          return -EIO;
1488       }
1489       return 0;
1490       break;
1491       
1492    case CDROMSTOP:      /* Spin down the drive */
1493       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
1494 
1495       /*
1496        * Spin the drive down, ignoring the error if the disk was
1497        * already not spinning.
1498        */
1499       sony_audio_status = CDROM_AUDIO_NO_STATUS;
1500       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1501       if (   ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1502           && (res_reg[1] != SONY_NOT_SPIN_ERR))
1503       {
1504          printk("Sony CDROM error 0x%2.2x (CDROMSTOP)\n", res_reg[1]);
1505          return -EIO;
1506       }
1507       
1508       return 0;
1509       break;
1510 
1511    case CDROMPAUSE:     /* Pause the drive */
1512       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
1513       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1514       {
1515          printk("Sony CDROM error 0x%2.2x (CDROMPAUSE)\n", res_reg[1]);
1516          return -EIO;
1517       }
1518 
1519       /* Get the current position and save it for resuming */
1520       if (read_subcode() < 0)
1521       {
1522          return -EIO;
1523       }
1524       cur_pos_msf[0] = last_sony_subcode->abs_msf[0];
1525       cur_pos_msf[1] = last_sony_subcode->abs_msf[1];
1526       cur_pos_msf[2] = last_sony_subcode->abs_msf[2];
1527       sony_audio_status = CDROM_AUDIO_PAUSED;
1528       return 0;
1529       break;
1530 
1531    case CDROMRESUME:    /* Start the drive after being paused */
1532       if (sony_audio_status != CDROM_AUDIO_PAUSED)
1533       {
1534          return -EINVAL;
1535       }
1536       
1537       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1538       
1539       /* Start the drive at the saved position. */
1540       params[1] = cur_pos_msf[0];
1541       params[2] = cur_pos_msf[1];
1542       params[3] = cur_pos_msf[2];
1543       params[4] = final_pos_msf[0];
1544       params[5] = final_pos_msf[1];
1545       params[6] = final_pos_msf[2];
1546       params[0] = 0x03;
1547       do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
1548       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1549       {
1550          printk("Sony CDROM error 0x%2.2x (CDROMRESUME)\n", res_reg[1]);
1551          return -EIO;
1552       }
1553       sony_audio_status = CDROM_AUDIO_PLAY;
1554       return 0;
1555       break;
1556 
1557    case CDROMPLAYMSF:   /* Play starting at the given MSF address. */
1558       verify_area(VERIFY_READ, (char *) arg, 6);
1559       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1560       memcpy_fromfs(&(params[1]), (void *) arg, 6);
1561       
1562       /* The parameters are given in int, must be converted */
1563       for (i=1; i<7; i++)
1564       {
1565          params[i] = int_to_bcd(params[i]);
1566       }
1567       params[0] = 0x03;
1568       do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
1569       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1570       {
1571          printk("Sony CDROM error 0x%2.2x (CDROMPLAYMSF)\n", res_reg[1]);
1572          return -EIO;
1573       }
1574       
1575       /* Save the final position for pauses and resumes */
1576       final_pos_msf[0] = params[4];
1577       final_pos_msf[1] = params[5];
1578       final_pos_msf[2] = params[6];
1579       sony_audio_status = CDROM_AUDIO_PLAY;
1580       return 0;
1581       break;
1582 
1583    case CDROMREADTOCHDR:        /* Read the table of contents header */
1584       {
1585          struct cdrom_tochdr *hdr;
1586          struct cdrom_tochdr loc_hdr;
1587          
1588          sony_get_toc();
1589          if (!sony_toc_read)
1590          {
1591             return -EIO;
1592          }
1593          
1594          hdr = (struct cdrom_tochdr *) arg;
1595          verify_area(VERIFY_WRITE, hdr, sizeof(*hdr));
1596          loc_hdr.cdth_trk0 = bcd_to_int(sony_toc->first_track_num);
1597          loc_hdr.cdth_trk1 = bcd_to_int(sony_toc->last_track_num);
1598          memcpy_tofs(hdr, &loc_hdr, sizeof(*hdr));
1599       }
1600       return 0;
1601       break;
1602 
1603    case CDROMREADTOCENTRY:      /* Read a given table of contents entry */
1604       {
1605          struct cdrom_tocentry *entry;
1606          struct cdrom_tocentry loc_entry;
1607          int track_idx;
1608          unsigned char *msf_val = NULL;
1609          
1610          sony_get_toc();
1611          if (!sony_toc_read)
1612          {
1613             return -EIO;
1614          }
1615          
1616          entry = (struct cdrom_tocentry *) arg;
1617          verify_area(VERIFY_READ, entry, sizeof(*entry));
1618          verify_area(VERIFY_WRITE, entry, sizeof(*entry));
1619          
1620          memcpy_fromfs(&loc_entry, entry, sizeof(loc_entry));
1621          
1622          /* Lead out is handled separately since it is special. */
1623          if (loc_entry.cdte_track == CDROM_LEADOUT)
1624          {
1625             loc_entry.cdte_adr = sony_toc->address2;
1626             loc_entry.cdte_ctrl = sony_toc->control2;
1627             msf_val = sony_toc->lead_out_start_msf;
1628          }
1629          else
1630          {
1631             track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
1632             if (track_idx < 0)
1633             {
1634                return -EINVAL;
1635             }
1636             
1637             loc_entry.cdte_adr = sony_toc->tracks[track_idx].address;
1638             loc_entry.cdte_ctrl = sony_toc->tracks[track_idx].control;
1639             msf_val = sony_toc->tracks[track_idx].track_start_msf;
1640          }
1641          
1642          /* Logical buffer address or MSF format requested? */
1643          if (loc_entry.cdte_format == CDROM_LBA)
1644          {
1645             loc_entry.cdte_addr.lba = msf_to_log(msf_val);
1646          }
1647          else if (loc_entry.cdte_format == CDROM_MSF)
1648          {
1649             loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
1650             loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val+1));
1651             loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val+2));
1652          }
1653          memcpy_tofs(entry, &loc_entry, sizeof(*entry));
1654       }
1655       return 0;
1656       break;
1657 
1658    case CDROMPLAYTRKIND:     /* Play a track.  This currently ignores index. */
1659       {
1660          struct cdrom_ti ti;
1661          int track_idx;
1662          
1663          sony_get_toc();
1664          if (!sony_toc_read)
1665          {
1666             return -EIO;
1667          }
1668          
1669          verify_area(VERIFY_READ, (char *) arg, sizeof(ti));
1670          
1671          memcpy_fromfs(&ti, (char *) arg, sizeof(ti));
1672          if (   (ti.cdti_trk0 < sony_toc->first_track_num)
1673              || (ti.cdti_trk0 > sony_toc->last_track_num)
1674              || (ti.cdti_trk1 < ti.cdti_trk0))
1675          {
1676             return -EINVAL;
1677          }
1678          
1679          track_idx = find_track(int_to_bcd(ti.cdti_trk0));
1680          if (track_idx < 0)
1681          {
1682             return -EINVAL;
1683          }
1684          params[1] = sony_toc->tracks[track_idx].track_start_msf[0];
1685          params[2] = sony_toc->tracks[track_idx].track_start_msf[1];
1686          params[3] = sony_toc->tracks[track_idx].track_start_msf[2];
1687          
1688          /*
1689           * If we want to stop after the last track, use the lead-out
1690           * MSF to do that.
1691           */
1692          if (ti.cdti_trk1 >= bcd_to_int(sony_toc->last_track_num))
1693          {
1694             log_to_msf(msf_to_log(sony_toc->lead_out_start_msf)-1,
1695                        &(params[4]));
1696          }
1697          else
1698          {
1699             track_idx = find_track(int_to_bcd(ti.cdti_trk1+1));
1700             if (track_idx < 0)
1701             {
1702                return -EINVAL;
1703             }
1704             log_to_msf(msf_to_log(sony_toc->tracks[track_idx].track_start_msf)-1,
1705                        &(params[4]));
1706          }
1707          params[0] = 0x03;
1708          
1709          do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1710          
1711          do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
1712          if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1713          {
1714             printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
1715                    params[2], params[3], params[4], params[5], params[6]);
1716             printk("Sony CDROM error 0x%2.2x (CDROMPLAYTRKIND\n", res_reg[1]);
1717             return -EIO;
1718          }
1719          
1720          /* Save the final position for pauses and resumes */
1721          final_pos_msf[0] = params[4];
1722          final_pos_msf[1] = params[5];
1723          final_pos_msf[2] = params[6];
1724          sony_audio_status = CDROM_AUDIO_PLAY;
1725          return 0;
1726       }
1727      
1728    case CDROMSUBCHNL:   /* Get subchannel info */
1729       return sony_get_subchnl_info(arg);
1730 
1731    case CDROMVOLCTRL:   /* Volume control.  What volume does this change, anyway? */
1732       {
1733          struct cdrom_volctrl volctrl;
1734          
1735          verify_area(VERIFY_READ, (char *) arg, sizeof(volctrl));
1736          
1737          memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
1738          params[0] = SONY_SD_AUDIO_VOLUME;
1739          params[1] = volctrl.channel0;
1740          params[2] = volctrl.channel1;
1741          do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
1742          if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1743          {
1744             printk("Sony CDROM error 0x%2.2x (CDROMVOLCTRL)\n", res_reg[1]);
1745             return -EIO;
1746          }
1747       }
1748       return 0;
1749 
1750    case CDROMEJECT:     /* Eject the drive */
1751       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
1752       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1753 
1754       sony_audio_status = CDROM_AUDIO_INVALID;
1755       do_sony_cd_cmd(SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
1756       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1757       {
1758          printk("Sony CDROM error 0x%2.2x (CDROMEJECT)\n", res_reg[1]);
1759          return -EIO;
1760       }
1761       return 0;
1762       break;
1763      
1764    default:
1765       return -EINVAL;
1766    }
1767 }
1768 
1769 
1770 /*
1771  * Open the drive for operations.  Spin the drive up and read the table of
1772  * contents if these have not already been done.
1773  */
1774 static int
1775 scd_open(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1776          struct file *filp)
1777 {
1778    unsigned char res_reg[2];
1779    unsigned int res_size;
1780    int num_spin_ups;
1781 
1782 
1783    if (filp->f_mode & 2)
1784       return -EROFS;
1785 
1786    if (!sony_spun_up)
1787    {
1788       num_spin_ups = 0;
1789 
1790 respinup_on_open:
1791       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1792 
1793       /* The drive sometimes returns error 0.  I don't know why, but ignore
1794          it.  It seems to mean the drive has already done the operation. */
1795       if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1796       {
1797          printk("Sony CDROM error 0x%2.2x (scd_open, spin up)\n", res_reg[1]);
1798          return -EIO;
1799       }
1800       
1801       do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
1802 
1803       /* The drive sometimes returns error 0.  I don't know why, but ignore
1804          it.  It seems to mean the drive has already done the operation. */
1805       if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1806       {
1807          /* If the drive is already playing, its ok.  */
1808          if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
1809          {
1810             goto drive_spinning;
1811          }
1812 
1813          /* If the drive says it is not spun up (even though we just did it!)
1814             then retry the operation at least a few times. */
1815          if (   (res_reg[1] == SONY_NOT_SPIN_ERR)
1816              && (num_spin_ups < MAX_CDU31A_RETRIES))
1817          {
1818             num_spin_ups++;
1819             goto respinup_on_open;
1820          }
1821 
1822          printk("Sony CDROM error 0x%2.2x (scd_open, read toc)\n", res_reg[1]);
1823          do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1824          
1825          return -EIO;
1826       }
1827 
1828       sony_get_toc();
1829       if (!sony_toc_read)
1830       {
1831          do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1832          return -EIO;
1833       }
1834 
1835       sony_spun_up = 1;
1836    }
1837 
1838 drive_spinning:
1839 
1840    if (inode)
1841    {
1842       check_disk_change(inode->i_rdev);
1843    }
1844 
1845    sony_usage++;
1846 
1847    return 0;
1848 }
1849 
1850 
1851 /*
1852  * Close the drive.  Spin it down if no task is using it.  The spin
1853  * down will fail if playing audio, so audio play is OK.
1854  */
1855 static void
1856 scd_release(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1857          struct file *filp)
1858 {
1859    unsigned char res_reg[2];
1860    unsigned int  res_size;
1861 
1862 
1863    if (sony_usage > 0)
1864    {
1865       sony_usage--;
1866    }
1867    if (sony_usage == 0)
1868    {
1869       sync_dev(inode->i_rdev);
1870       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1871 
1872       sony_spun_up = 0;
1873    }
1874 }
1875 
1876 
1877 static struct file_operations scd_fops = {
1878    NULL,                   /* lseek - default */
1879    block_read,             /* read - general block-dev read */
1880    block_write,            /* write - general block-dev write */
1881    NULL,                   /* readdir - bad */
1882    NULL,                   /* select */
1883    scd_ioctl,              /* ioctl */
1884    NULL,                   /* mmap */
1885    scd_open,               /* open */
1886    scd_release,            /* release */
1887    NULL,                   /* fsync */
1888    NULL,                   /* fasync */
1889    scd_disk_change,        /* media_change */
1890    NULL                    /* revalidate */
1891 };
1892 
1893 
1894 /* The different types of disc loading mechanisms supported */
1895 static char *load_mech[] = { "caddy", "tray", "pop-up", "unknown" };
1896 
1897 /* Read-ahead buffer sizes for different drives.  These are just arbitrary
1898    values, I don't know what is really optimum. */
1899 static unsigned int mem_size[] = { 16384, 16384, 16384, 2048 };
1900 
1901 void
1902 get_drive_configuration(unsigned short base_io,
     /* [previous][next][first][last][top][bottom][index][help] */
1903                         unsigned char res_reg[],
1904                         unsigned int *res_size)
1905 {
1906    int retry_count;
1907 
1908 
1909    /* Set the base address */
1910    sony_cd_base_io = base_io;
1911 
1912    /* Set up all the register locations */
1913    sony_cd_cmd_reg = sony_cd_base_io + SONY_CMD_REG_OFFSET;
1914    sony_cd_param_reg = sony_cd_base_io + SONY_PARAM_REG_OFFSET;
1915    sony_cd_write_reg = sony_cd_base_io + SONY_WRITE_REG_OFFSET;
1916    sony_cd_control_reg = sony_cd_base_io + SONY_CONTROL_REG_OFFSET;
1917    sony_cd_status_reg = sony_cd_base_io + SONY_STATUS_REG_OFFSET;
1918    sony_cd_result_reg = sony_cd_base_io + SONY_RESULT_REG_OFFSET;
1919    sony_cd_read_reg = sony_cd_base_io + SONY_READ_REG_OFFSET;
1920    sony_cd_fifost_reg = sony_cd_base_io + SONY_FIFOST_REG_OFFSET;
1921 
1922    /*
1923     * Check to see if anything exists at the status register location.
1924     * I don't know if this is a good way to check, but it seems to work
1925     * ok for me.
1926     */
1927    if (read_status_register() != 0xff)
1928    {
1929       /*
1930        * Reset the drive and wait for attention from it (to say its reset).
1931        * If you don't wait, the next operation will probably fail.
1932        */
1933       reset_drive();
1934       retry_count = jiffies + SONY_RESET_TIMEOUT;
1935       while ((retry_count > jiffies) && (!is_attention()))
1936       {
1937          sony_sleep();
1938       }
1939 
1940       /* If attention is never seen probably not a CDU31a present */
1941       if (!is_attention())
1942       {
1943          res_reg[0] = 0x20;
1944          return;
1945       }
1946 
1947       /*
1948        * Get the drive configuration.
1949        */
1950       do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
1951                      NULL,
1952                      0,
1953                      (unsigned char *) res_reg,
1954                      res_size);
1955       return;
1956    }
1957 
1958    /* Return an error */
1959    res_reg[0] = 0x20;
1960 }
1961 
1962 
1963 static int cdu31a_block_size;
1964 
1965 /*
1966  * Initialize the driver.
1967  */
1968 unsigned long
1969 cdu31a_init(unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1970 {
1971    struct s_sony_drive_config drive_config;
1972    unsigned int res_size;
1973    int i;
1974    int drive_found;
1975    int tmp_irq;
1976 
1977 
1978    /*
1979     * According to Alex Freed (freed@europa.orion.adobe.com), this is
1980     * required for the Fusion CD-16 package.  If the sound driver is
1981     * loaded, it should work fine, but just in case...
1982     *
1983     * The following turn on the CD-ROM interface for a Fusion CD-16.
1984     */
1985    outb(0xbc, 0x9a01);
1986    outb(0xe2, 0x9a01);
1987 
1988    i = 0;
1989    drive_found = 0;
1990    while (   (cdu31a_addresses[i].base != 0)
1991           && (!drive_found))
1992    {
1993       if (check_region(cdu31a_addresses[i].base, 4)) {
1994           i++;
1995           continue;
1996       }
1997       get_drive_configuration(cdu31a_addresses[i].base,
1998                                drive_config.exec_status,
1999                                &res_size);
2000       if ((res_size > 2) && ((drive_config.exec_status[0] & 0x20) == 0x00))
2001       {
2002          drive_found = 1;
2003          snarf_region(cdu31a_addresses[i].base, 4);
2004 
2005          if (register_blkdev(MAJOR_NR,"cdu31a",&scd_fops))
2006          {
2007             printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
2008             return mem_start;
2009          }
2010 
2011          if (SONY_HWC_DOUBLE_SPEED(drive_config))
2012          {
2013             is_double_speed = 1;
2014          }
2015 
2016          tmp_irq = cdu31a_addresses[i].int_num;
2017          if (tmp_irq < 0)
2018          {
2019             autoirq_setup(0);
2020             enable_interrupts();
2021             reset_drive();
2022             tmp_irq = autoirq_report(10);
2023             disable_interrupts();
2024 
2025             set_drive_params();
2026             irq_used = tmp_irq;
2027          }
2028          else
2029          {
2030             set_drive_params();
2031             irq_used = tmp_irq;
2032          }
2033 
2034          if (irq_used > 0)
2035          {
2036             if (request_irq(irq_used, cdu31a_interrupt, SA_INTERRUPT, "cdu31a"))
2037             {
2038                irq_used = 0;
2039                printk("Unable to grab IRQ%d for the CDU31A driver\n", irq_used);
2040             }
2041          }
2042 
2043          dma_channel = cdu31a_addresses[i].dma_num;
2044          if (dma_channel > 0)
2045          {
2046             if (request_dma(dma_channel,"cdu31a"))
2047             {
2048                dma_channel = -1;
2049                printk("Unable to grab DMA%d for the CDU31A driver\n",
2050                       dma_channel);
2051             }
2052          }
2053 
2054          sony_buffer_size = mem_size[SONY_HWC_GET_BUF_MEM_SIZE(drive_config)];
2055          sony_buffer_sectors = sony_buffer_size / 2048;
2056 
2057          printk("Sony I/F CDROM : %8.8s %16.16s %8.8s with %s load mechanism\n",
2058                 drive_config.vendor_id,
2059                 drive_config.product_id,
2060                 drive_config.product_rev_level,
2061                 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
2062          printk("  using %d byte buffer", sony_buffer_size);
2063          if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
2064          {
2065             printk(", audio");
2066          }
2067          if (SONY_HWC_EJECT(drive_config))
2068          {
2069            printk(", eject");
2070          }
2071          if (SONY_HWC_LED_SUPPORT(drive_config))
2072          {
2073            printk(", LED");
2074          }
2075          if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
2076          {
2077            printk(", elec. Vol");
2078          }
2079          if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
2080          {
2081            printk(", sep. Vol");
2082          }
2083          if (is_double_speed)
2084          {
2085             printk(", double speed");
2086          }
2087          if (irq_used > 0)
2088          {
2089             printk(", irq %d", irq_used);
2090          }
2091          if (dma_channel > 0)
2092          {
2093             printk(", drq %d", dma_channel);
2094          }
2095          printk("\n");
2096 
2097          blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
2098          read_ahead[MAJOR_NR] = 32; /* 32 sector (16kB) read-ahead */
2099          cdu31a_block_size = 2048; /* 2kB block size */
2100                                    /* use 'mount -o block=2048' */
2101          blksize_size[MAJOR_NR] = &cdu31a_block_size;
2102 
2103          sony_toc = (struct s_sony_toc *) mem_start;
2104          mem_start += sizeof(*sony_toc);
2105          last_sony_subcode = (struct s_sony_subcode *) mem_start;
2106          mem_start += sizeof(*last_sony_subcode);
2107 
2108          /* If memory will not fit into the current 64KB block, align it
2109             so the block will not cross a 64KB boundary.  This is
2110             because DMA cannot cross 64KB boundaries. */
2111          if (   (dma_channel > 0)
2112              && (   ((mem_start) & (~0xffff))
2113                  != (((mem_start) + sony_buffer_size) & (~0xffff))))
2114          {
2115             mem_start = (((int)mem_start) + 0x10000) & (~0xffff);
2116          }
2117 
2118          sony_buffer = (unsigned char *) mem_start;
2119          mem_start += sony_buffer_size;
2120       }
2121 
2122       i++;
2123    }
2124    
2125    return mem_start;
2126 }
2127 

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