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[2];
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             memcpy(CURRENT->buffer, sony_buffer+copyoff, 512);
1267                
1268             block += 1;
1269             nsect -= 1;
1270             CURRENT->buffer += 512;
1271          }
1272                
1273          end_request(1);
1274          break;
1275             
1276       case WRITE:
1277          end_request(0);
1278          break;
1279             
1280       default:
1281          panic("Unknown SONY CD cmd");
1282       }
1283    }
1284 
1285 end_do_cdu31a_request:
1286    has_cd_task = NULL;
1287    sony_inuse = 0;
1288    wake_up_interruptible(&sony_wait);
1289 }
1290 
1291 
1292 /*
1293  * Read the table of contents from the drive and set sony_toc_read if
1294  * successful.
1295  */
1296 static void
1297 sony_get_toc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1298 {
1299    unsigned int res_size;
1300 
1301 
1302    if (!sony_toc_read)
1303    {
1304       do_sony_cd_cmd(SONY_REQ_TOC_DATA_CMD,
1305                      NULL,
1306                      0, 
1307                      (unsigned char *) sony_toc, 
1308                      &res_size);
1309       if ((res_size < 2) || ((sony_toc->exec_status[0] & 0x20) == 0x20))
1310       {
1311          return;
1312       }
1313       sony_toc->lead_out_start_lba = msf_to_log(sony_toc->lead_out_start_msf);
1314       sony_toc_read = 1;
1315    }
1316 }
1317 
1318 
1319 /*
1320  * Search for a specific track in the table of contents.
1321  */
1322 static int
1323 find_track(int track)
     /* [previous][next][first][last][top][bottom][index][help] */
1324 {
1325    int i;
1326    int num_tracks;
1327 
1328 
1329    num_tracks = sony_toc->last_track_num + sony_toc->first_track_num + 1;
1330    for (i = 0; i < num_tracks; i++)
1331    {
1332       if (sony_toc->tracks[i].track == track)
1333       {
1334          return i;
1335       }
1336    }
1337 
1338    return -1;
1339 }
1340 
1341 
1342 /*
1343  * Read the subcode and put it int last_sony_subcode for future use.
1344  */
1345 static int
1346 read_subcode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1347 {
1348    unsigned int res_size;
1349 
1350 
1351    do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
1352                   NULL,
1353                   0, 
1354                   (unsigned char *) last_sony_subcode, 
1355                   &res_size);
1356    if ((res_size < 2) || ((last_sony_subcode->exec_status[0] & 0x20) == 0x20))
1357    {
1358       printk("Sony CDROM error 0x%2.2x (read_subcode)\n",
1359              last_sony_subcode->exec_status[1]);
1360       return -EIO;
1361    }
1362 
1363    return 0;
1364 }
1365 
1366 
1367 /*
1368  * Get the subchannel info like the CDROMSUBCHNL command wants to see it.  If
1369  * the drive is playing, the subchannel needs to be read (since it would be
1370  * changing).  If the drive is paused or completed, the subcode information has
1371  * already been stored, just use that.  The ioctl call wants things in decimal
1372  * (not BCD), so all the conversions are done.
1373  */
1374 static int
1375 sony_get_subchnl_info(long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1376 {
1377    struct cdrom_subchnl schi;
1378 
1379 
1380    /* Get attention stuff */
1381    while (handle_sony_cd_attention())
1382       ;
1383 
1384    sony_get_toc();
1385    if (!sony_toc_read)
1386    {
1387       return -EIO;
1388    }
1389 
1390    verify_area(VERIFY_READ, (char *) arg, sizeof(schi));
1391    verify_area(VERIFY_WRITE, (char *) arg, sizeof(schi));
1392 
1393    memcpy_fromfs(&schi, (char *) arg, sizeof(schi));
1394    
1395    switch (sony_audio_status)
1396    {
1397    case CDROM_AUDIO_PLAY:
1398       if (read_subcode() < 0)
1399       {
1400          return -EIO;
1401       }
1402       break;
1403 
1404    case CDROM_AUDIO_PAUSED:
1405    case CDROM_AUDIO_COMPLETED:
1406       break;
1407 
1408    case CDROM_AUDIO_NO_STATUS:
1409       schi.cdsc_audiostatus = sony_audio_status;
1410       memcpy_tofs((char *) arg, &schi, sizeof(schi));
1411       return 0;
1412       break;
1413 
1414    case CDROM_AUDIO_INVALID:
1415    case CDROM_AUDIO_ERROR:
1416    default:
1417       return -EIO;
1418    }
1419 
1420    schi.cdsc_audiostatus = sony_audio_status;
1421    schi.cdsc_adr = last_sony_subcode->address;
1422    schi.cdsc_ctrl = last_sony_subcode->control;
1423    schi.cdsc_trk = bcd_to_int(last_sony_subcode->track_num);
1424    schi.cdsc_ind = bcd_to_int(last_sony_subcode->index_num);
1425    if (schi.cdsc_format == CDROM_MSF)
1426    {
1427       schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode->abs_msf[0]);
1428       schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode->abs_msf[1]);
1429       schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode->abs_msf[2]);
1430 
1431       schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode->rel_msf[0]);
1432       schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode->rel_msf[1]);
1433       schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode->rel_msf[2]);
1434    }
1435    else if (schi.cdsc_format == CDROM_LBA)
1436    {
1437       schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode->abs_msf);
1438       schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode->rel_msf);
1439    }
1440    
1441    memcpy_tofs((char *) arg, &schi, sizeof(schi));
1442    return 0;
1443 }
1444 
1445 
1446 /*
1447  * The big ugly ioctl handler.
1448  */
1449 static int
1450 scd_ioctl(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1451           struct file  *file,
1452           unsigned int  cmd,
1453           unsigned long arg)
1454 {
1455    unsigned int dev;
1456    unsigned char res_reg[2];
1457    unsigned int res_size;
1458    unsigned char params[7];
1459    int i;
1460 
1461 
1462    if (!inode)
1463    {
1464       return -EINVAL;
1465    }
1466    dev = MINOR(inode->i_rdev) >> 6;
1467    if (dev != 0)
1468    {
1469       return -EINVAL;
1470    }
1471 
1472    switch (cmd)
1473    {
1474    case CDROMSTART:     /* Spin up the drive */
1475       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1476       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1477       {
1478          printk("Sony CDROM error 0x%2.2x (CDROMSTART)\n", res_reg[1]);
1479          return -EIO;
1480       }
1481       return 0;
1482       break;
1483       
1484    case CDROMSTOP:      /* Spin down the drive */
1485       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
1486 
1487       /*
1488        * Spin the drive down, ignoring the error if the disk was
1489        * already not spinning.
1490        */
1491       sony_audio_status = CDROM_AUDIO_NO_STATUS;
1492       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1493       if (   ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1494           && (res_reg[1] != SONY_NOT_SPIN_ERR))
1495       {
1496          printk("Sony CDROM error 0x%2.2x (CDROMSTOP)\n", res_reg[1]);
1497          return -EIO;
1498       }
1499       
1500       return 0;
1501       break;
1502 
1503    case CDROMPAUSE:     /* Pause the drive */
1504       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
1505       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1506       {
1507          printk("Sony CDROM error 0x%2.2x (CDROMPAUSE)\n", res_reg[1]);
1508          return -EIO;
1509       }
1510 
1511       /* Get the current position and save it for resuming */
1512       if (read_subcode() < 0)
1513       {
1514          return -EIO;
1515       }
1516       cur_pos_msf[0] = last_sony_subcode->abs_msf[0];
1517       cur_pos_msf[1] = last_sony_subcode->abs_msf[1];
1518       cur_pos_msf[2] = last_sony_subcode->abs_msf[2];
1519       sony_audio_status = CDROM_AUDIO_PAUSED;
1520       return 0;
1521       break;
1522 
1523    case CDROMRESUME:    /* Start the drive after being paused */
1524       if (sony_audio_status != CDROM_AUDIO_PAUSED)
1525       {
1526          return -EINVAL;
1527       }
1528       
1529       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1530       
1531       /* Start the drive at the saved position. */
1532       params[1] = cur_pos_msf[0];
1533       params[2] = cur_pos_msf[1];
1534       params[3] = cur_pos_msf[2];
1535       params[4] = final_pos_msf[0];
1536       params[5] = final_pos_msf[1];
1537       params[6] = final_pos_msf[2];
1538       params[0] = 0x03;
1539       do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
1540       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1541       {
1542          printk("Sony CDROM error 0x%2.2x (CDROMRESUME)\n", res_reg[1]);
1543          return -EIO;
1544       }
1545       sony_audio_status = CDROM_AUDIO_PLAY;
1546       return 0;
1547       break;
1548 
1549    case CDROMPLAYMSF:   /* Play starting at the given MSF address. */
1550       verify_area(VERIFY_READ, (char *) arg, 6);
1551       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1552       memcpy_fromfs(&(params[1]), (void *) arg, 6);
1553       
1554       /* The parameters are given in int, must be converted */
1555       for (i=1; i<7; i++)
1556       {
1557          params[i] = int_to_bcd(params[i]);
1558       }
1559       params[0] = 0x03;
1560       do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
1561       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1562       {
1563          printk("Sony CDROM error 0x%2.2x (CDROMPLAYMSF)\n", res_reg[1]);
1564          return -EIO;
1565       }
1566       
1567       /* Save the final position for pauses and resumes */
1568       final_pos_msf[0] = params[4];
1569       final_pos_msf[1] = params[5];
1570       final_pos_msf[2] = params[6];
1571       sony_audio_status = CDROM_AUDIO_PLAY;
1572       return 0;
1573       break;
1574 
1575    case CDROMREADTOCHDR:        /* Read the table of contents header */
1576       {
1577          struct cdrom_tochdr *hdr;
1578          struct cdrom_tochdr loc_hdr;
1579          
1580          sony_get_toc();
1581          if (!sony_toc_read)
1582          {
1583             return -EIO;
1584          }
1585          
1586          hdr = (struct cdrom_tochdr *) arg;
1587          verify_area(VERIFY_WRITE, hdr, sizeof(*hdr));
1588          loc_hdr.cdth_trk0 = bcd_to_int(sony_toc->first_track_num);
1589          loc_hdr.cdth_trk1 = bcd_to_int(sony_toc->last_track_num);
1590          memcpy_tofs(hdr, &loc_hdr, sizeof(*hdr));
1591       }
1592       return 0;
1593       break;
1594 
1595    case CDROMREADTOCENTRY:      /* Read a given table of contents entry */
1596       {
1597          struct cdrom_tocentry *entry;
1598          struct cdrom_tocentry loc_entry;
1599          int track_idx;
1600          unsigned char *msf_val = NULL;
1601          
1602          sony_get_toc();
1603          if (!sony_toc_read)
1604          {
1605             return -EIO;
1606          }
1607          
1608          entry = (struct cdrom_tocentry *) arg;
1609          verify_area(VERIFY_READ, entry, sizeof(*entry));
1610          verify_area(VERIFY_WRITE, entry, sizeof(*entry));
1611          
1612          memcpy_fromfs(&loc_entry, entry, sizeof(loc_entry));
1613          
1614          /* Lead out is handled separately since it is special. */
1615          if (loc_entry.cdte_track == CDROM_LEADOUT)
1616          {
1617             loc_entry.cdte_adr = sony_toc->address2;
1618             loc_entry.cdte_ctrl = sony_toc->control2;
1619             msf_val = sony_toc->lead_out_start_msf;
1620          }
1621          else
1622          {
1623             track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
1624             if (track_idx < 0)
1625             {
1626                return -EINVAL;
1627             }
1628             
1629             loc_entry.cdte_adr = sony_toc->tracks[track_idx].address;
1630             loc_entry.cdte_ctrl = sony_toc->tracks[track_idx].control;
1631             msf_val = sony_toc->tracks[track_idx].track_start_msf;
1632          }
1633          
1634          /* Logical buffer address or MSF format requested? */
1635          if (loc_entry.cdte_format == CDROM_LBA)
1636          {
1637             loc_entry.cdte_addr.lba = msf_to_log(msf_val);
1638          }
1639          else if (loc_entry.cdte_format == CDROM_MSF)
1640          {
1641             loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
1642             loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val+1));
1643             loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val+2));
1644          }
1645          memcpy_tofs(entry, &loc_entry, sizeof(*entry));
1646       }
1647       return 0;
1648       break;
1649 
1650    case CDROMPLAYTRKIND:     /* Play a track.  This currently ignores index. */
1651       {
1652          struct cdrom_ti ti;
1653          int track_idx;
1654          
1655          sony_get_toc();
1656          if (!sony_toc_read)
1657          {
1658             return -EIO;
1659          }
1660          
1661          verify_area(VERIFY_READ, (char *) arg, sizeof(ti));
1662          
1663          memcpy_fromfs(&ti, (char *) arg, sizeof(ti));
1664          if (   (ti.cdti_trk0 < sony_toc->first_track_num)
1665              || (ti.cdti_trk0 > sony_toc->last_track_num)
1666              || (ti.cdti_trk1 < ti.cdti_trk0))
1667          {
1668             return -EINVAL;
1669          }
1670          
1671          track_idx = find_track(int_to_bcd(ti.cdti_trk0));
1672          if (track_idx < 0)
1673          {
1674             return -EINVAL;
1675          }
1676          params[1] = sony_toc->tracks[track_idx].track_start_msf[0];
1677          params[2] = sony_toc->tracks[track_idx].track_start_msf[1];
1678          params[3] = sony_toc->tracks[track_idx].track_start_msf[2];
1679          
1680          /*
1681           * If we want to stop after the last track, use the lead-out
1682           * MSF to do that.
1683           */
1684          if (ti.cdti_trk1 >= bcd_to_int(sony_toc->last_track_num))
1685          {
1686             log_to_msf(msf_to_log(sony_toc->lead_out_start_msf)-1,
1687                        &(params[4]));
1688          }
1689          else
1690          {
1691             track_idx = find_track(int_to_bcd(ti.cdti_trk1+1));
1692             if (track_idx < 0)
1693             {
1694                return -EINVAL;
1695             }
1696             log_to_msf(msf_to_log(sony_toc->tracks[track_idx].track_start_msf)-1,
1697                        &(params[4]));
1698          }
1699          params[0] = 0x03;
1700          
1701          do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1702          
1703          do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
1704          if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1705          {
1706             printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
1707                    params[2], params[3], params[4], params[5], params[6]);
1708             printk("Sony CDROM error 0x%2.2x (CDROMPLAYTRKIND\n", res_reg[1]);
1709             return -EIO;
1710          }
1711          
1712          /* Save the final position for pauses and resumes */
1713          final_pos_msf[0] = params[4];
1714          final_pos_msf[1] = params[5];
1715          final_pos_msf[2] = params[6];
1716          sony_audio_status = CDROM_AUDIO_PLAY;
1717          return 0;
1718       }
1719      
1720    case CDROMSUBCHNL:   /* Get subchannel info */
1721       return sony_get_subchnl_info(arg);
1722 
1723    case CDROMVOLCTRL:   /* Volume control.  What volume does this change, anyway? */
1724       {
1725          struct cdrom_volctrl volctrl;
1726          
1727          verify_area(VERIFY_READ, (char *) arg, sizeof(volctrl));
1728          
1729          memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
1730          params[0] = SONY_SD_AUDIO_VOLUME;
1731          params[1] = volctrl.channel0;
1732          params[2] = volctrl.channel1;
1733          do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
1734          if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1735          {
1736             printk("Sony CDROM error 0x%2.2x (CDROMVOLCTRL)\n", res_reg[1]);
1737             return -EIO;
1738          }
1739       }
1740       return 0;
1741 
1742    case CDROMEJECT:     /* Eject the drive */
1743       do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
1744       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1745 
1746       sony_audio_status = CDROM_AUDIO_INVALID;
1747       do_sony_cd_cmd(SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
1748       if ((res_size < 2) || ((res_reg[0] & 0x20) == 0x20))
1749       {
1750          printk("Sony CDROM error 0x%2.2x (CDROMEJECT)\n", res_reg[1]);
1751          return -EIO;
1752       }
1753       return 0;
1754       break;
1755      
1756    default:
1757       return -EINVAL;
1758    }
1759 }
1760 
1761 
1762 /*
1763  * Open the drive for operations.  Spin the drive up and read the table of
1764  * contents if these have not already been done.
1765  */
1766 static int
1767 scd_open(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1768          struct file *filp)
1769 {
1770    unsigned char res_reg[2];
1771    unsigned int res_size;
1772    int num_spin_ups;
1773 
1774 
1775    if (filp->f_mode & 2)
1776       return -EROFS;
1777 
1778    if (!sony_spun_up)
1779    {
1780       num_spin_ups = 0;
1781 
1782 respinup_on_open:
1783       do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1784 
1785       /* The drive sometimes returns error 0.  I don't know why, but ignore
1786          it.  It seems to mean the drive has already done the operation. */
1787       if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1788       {
1789          printk("Sony CDROM error 0x%2.2x (scd_open, spin up)\n", res_reg[1]);
1790          return -EIO;
1791       }
1792       
1793       do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
1794 
1795       /* The drive sometimes returns error 0.  I don't know why, but ignore
1796          it.  It seems to mean the drive has already done the operation. */
1797       if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1798       {
1799          /* If the drive is already playing, its ok.  */
1800          if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
1801          {
1802             goto drive_spinning;
1803          }
1804 
1805          /* If the drive says it is not spun up (even though we just did it!)
1806             then retry the operation at least a few times. */
1807          if (   (res_reg[1] == SONY_NOT_SPIN_ERR)
1808              && (num_spin_ups < MAX_CDU31A_RETRIES))
1809          {
1810             num_spin_ups++;
1811             goto respinup_on_open;
1812          }
1813 
1814          printk("Sony CDROM error 0x%2.2x (scd_open, read toc)\n", res_reg[1]);
1815          do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1816          
1817          return -EIO;
1818       }
1819 
1820       sony_get_toc();
1821       if (!sony_toc_read)
1822       {
1823          do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1824          return -EIO;
1825       }
1826 
1827       sony_spun_up = 1;
1828    }
1829 
1830 drive_spinning:
1831 
1832    if (inode)
1833    {
1834       check_disk_change(inode->i_rdev);
1835    }
1836 
1837    sony_usage++;
1838 
1839    return 0;
1840 }
1841 
1842 
1843 /*
1844  * Close the drive.  Spin it down if no task is using it.  The spin
1845  * down will fail if playing audio, so audio play is OK.
1846  */
1847 static void
1848 scd_release(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1849          struct file *filp)
1850 {
1851    unsigned char res_reg[2];
1852    unsigned int  res_size;
1853 
1854 
1855    if (sony_usage > 0)
1856    {
1857       sony_usage--;
1858    }
1859    if (sony_usage == 0)
1860    {
1861       sync_dev(inode->i_rdev);
1862       do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
1863 
1864       sony_spun_up = 0;
1865    }
1866 }
1867 
1868 
1869 static struct file_operations scd_fops = {
1870    NULL,                   /* lseek - default */
1871    block_read,             /* read - general block-dev read */
1872    block_write,            /* write - general block-dev write */
1873    NULL,                   /* readdir - bad */
1874    NULL,                   /* select */
1875    scd_ioctl,              /* ioctl */
1876    NULL,                   /* mmap */
1877    scd_open,               /* open */
1878    scd_release,            /* release */
1879    NULL,                   /* fsync */
1880    NULL,                   /* fasync */
1881    scd_disk_change,        /* media_change */
1882    NULL                    /* revalidate */
1883 };
1884 
1885 
1886 /* The different types of disc loading mechanisms supported */
1887 static char *load_mech[] = { "caddy", "tray", "pop-up", "unknown" };
1888 
1889 /* Read-ahead buffer sizes for different drives.  These are just arbitrary
1890    values, I don't know what is really optimum. */
1891 static unsigned int mem_size[] = { 16384, 16384, 16384, 2048 };
1892 
1893 void
1894 get_drive_configuration(unsigned short base_io,
     /* [previous][next][first][last][top][bottom][index][help] */
1895                         unsigned char res_reg[],
1896                         unsigned int *res_size)
1897 {
1898    int retry_count;
1899 
1900 
1901    /* Set the base address */
1902    sony_cd_base_io = base_io;
1903 
1904    /* Set up all the register locations */
1905    sony_cd_cmd_reg = sony_cd_base_io + SONY_CMD_REG_OFFSET;
1906    sony_cd_param_reg = sony_cd_base_io + SONY_PARAM_REG_OFFSET;
1907    sony_cd_write_reg = sony_cd_base_io + SONY_WRITE_REG_OFFSET;
1908    sony_cd_control_reg = sony_cd_base_io + SONY_CONTROL_REG_OFFSET;
1909    sony_cd_status_reg = sony_cd_base_io + SONY_STATUS_REG_OFFSET;
1910    sony_cd_result_reg = sony_cd_base_io + SONY_RESULT_REG_OFFSET;
1911    sony_cd_read_reg = sony_cd_base_io + SONY_READ_REG_OFFSET;
1912    sony_cd_fifost_reg = sony_cd_base_io + SONY_FIFOST_REG_OFFSET;
1913 
1914    /*
1915     * Check to see if anything exists at the status register location.
1916     * I don't know if this is a good way to check, but it seems to work
1917     * ok for me.
1918     */
1919    if (read_status_register() != 0xff)
1920    {
1921       /*
1922        * Reset the drive and wait for attention from it (to say its reset).
1923        * If you don't wait, the next operation will probably fail.
1924        */
1925       reset_drive();
1926       retry_count = jiffies + SONY_RESET_TIMEOUT;
1927       while ((retry_count > jiffies) && (!is_attention()))
1928       {
1929          sony_sleep();
1930       }
1931 
1932       /* If attention is never seen probably not a CDU31a present */
1933       if (!is_attention())
1934       {
1935          res_reg[0] = 0x20;
1936          return;
1937       }
1938 
1939       /*
1940        * Get the drive configuration.
1941        */
1942       do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
1943                      NULL,
1944                      0,
1945                      (unsigned char *) res_reg,
1946                      res_size);
1947       return;
1948    }
1949 
1950    /* Return an error */
1951    res_reg[0] = 0x20;
1952 }
1953 
1954 
1955 static int cdu31a_block_size;
1956 
1957 /*
1958  * Initialize the driver.
1959  */
1960 unsigned long
1961 cdu31a_init(unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1962 {
1963    struct s_sony_drive_config drive_config;
1964    unsigned int res_size;
1965    int i;
1966    int drive_found;
1967    int tmp_irq;
1968 
1969 
1970    /*
1971     * According to Alex Freed (freed@europa.orion.adobe.com), this is
1972     * required for the Fusion CD-16 package.  If the sound driver is
1973     * loaded, it should work fine, but just in case...
1974     *
1975     * The following turn on the CD-ROM interface for a Fusion CD-16.
1976     */
1977    outb(0xbc, 0x9a01);
1978    outb(0xe2, 0x9a01);
1979 
1980    i = 0;
1981    drive_found = 0;
1982    while (   (cdu31a_addresses[i].base != 0)
1983           && (!drive_found))
1984    {
1985       if (check_region(cdu31a_addresses[i].base, 4)) {
1986           i++;
1987           continue;
1988       }
1989       get_drive_configuration(cdu31a_addresses[i].base,
1990                                drive_config.exec_status,
1991                                &res_size);
1992       if ((res_size > 2) && ((drive_config.exec_status[0] & 0x20) == 0x00))
1993       {
1994          drive_found = 1;
1995          snarf_region(cdu31a_addresses[i].base, 4);
1996 
1997          if (register_blkdev(MAJOR_NR,"cdu31a",&scd_fops))
1998          {
1999             printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
2000             return mem_start;
2001          }
2002 
2003          if (SONY_HWC_DOUBLE_SPEED(drive_config))
2004          {
2005             is_double_speed = 1;
2006          }
2007 
2008          tmp_irq = cdu31a_addresses[i].int_num;
2009          if (tmp_irq < 0)
2010          {
2011             autoirq_setup(0);
2012             enable_interrupts();
2013             reset_drive();
2014             tmp_irq = autoirq_report(10);
2015             disable_interrupts();
2016 
2017             set_drive_params();
2018             irq_used = tmp_irq;
2019          }
2020          else
2021          {
2022             set_drive_params();
2023             irq_used = tmp_irq;
2024          }
2025 
2026          if (irq_used > 0)
2027          {
2028             if (request_irq(irq_used, cdu31a_interrupt, SA_INTERRUPT, "cdu31a"))
2029             {
2030                irq_used = 0;
2031                printk("Unable to grab IRQ%d for the CDU31A driver\n", irq_used);
2032             }
2033          }
2034 
2035          dma_channel = cdu31a_addresses[i].dma_num;
2036          if (dma_channel > 0)
2037          {
2038             if (request_dma(dma_channel,"cdu31a"))
2039             {
2040                dma_channel = -1;
2041                printk("Unable to grab DMA%d for the CDU31A driver\n",
2042                       dma_channel);
2043             }
2044          }
2045 
2046          sony_buffer_size = mem_size[SONY_HWC_GET_BUF_MEM_SIZE(drive_config)];
2047          sony_buffer_sectors = sony_buffer_size / 2048;
2048 
2049          printk("Sony I/F CDROM : %8.8s %16.16s %8.8s with %s load mechanism\n",
2050                 drive_config.vendor_id,
2051                 drive_config.product_id,
2052                 drive_config.product_rev_level,
2053                 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
2054          printk("  using %d byte buffer", sony_buffer_size);
2055          if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
2056          {
2057             printk(", audio");
2058          }
2059          if (SONY_HWC_EJECT(drive_config))
2060          {
2061            printk(", eject");
2062          }
2063          if (SONY_HWC_LED_SUPPORT(drive_config))
2064          {
2065            printk(", LED");
2066          }
2067          if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
2068          {
2069            printk(", elec. Vol");
2070          }
2071          if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
2072          {
2073            printk(", sep. Vol");
2074          }
2075          if (is_double_speed)
2076          {
2077             printk(", double speed");
2078          }
2079          if (irq_used > 0)
2080          {
2081             printk(", irq %d", irq_used);
2082          }
2083          if (dma_channel > 0)
2084          {
2085             printk(", drq %d", dma_channel);
2086          }
2087          printk("\n");
2088 
2089          blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
2090          read_ahead[MAJOR_NR] = 32; /* 32 sector (16kB) read-ahead */
2091          cdu31a_block_size = 2048; /* 2kB block size */
2092                                    /* use 'mount -o block=2048' */
2093          blksize_size[MAJOR_NR] = &cdu31a_block_size;
2094 
2095          sony_toc = (struct s_sony_toc *) mem_start;
2096          mem_start += sizeof(*sony_toc);
2097          last_sony_subcode = (struct s_sony_subcode *) mem_start;
2098          mem_start += sizeof(*last_sony_subcode);
2099 
2100          /* If memory will not fit into the current 64KB block, align it
2101             so the block will not cross a 64KB boundary.  This is
2102             because DMA cannot cross 64KB boundaries. */
2103          if (   (dma_channel > 0)
2104              && (   ((mem_start) & (~0xffff))
2105                  != (((mem_start) + sony_buffer_size) & (~0xffff))))
2106          {
2107             mem_start = (((int)mem_start) + 0x10000) & (~0xffff);
2108          }
2109 
2110          sony_buffer = (unsigned char *) mem_start;
2111          mem_start += sony_buffer_size;
2112       }
2113 
2114       i++;
2115    }
2116    
2117    return mem_start;
2118 }
2119 

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