root/drivers/block/sonycd535.c

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

DEFINITIONS

This source file includes following definitions.
  1. cdu535_check_media_change
  2. enable_interrupts
  3. disable_interrupts
  4. cdu535_interrupt
  5. sony_sleep
  6. select_unit
  7. read_result_reg
  8. read_exec_status
  9. check_drive_status
  10. do_sony_cmd
  11. set_drive_mode
  12. seek_and_read_N_blocks
  13. request_toc_data
  14. spin_up_drive
  15. int_to_bcd
  16. bcd_to_int
  17. log_to_msf
  18. msf_to_log
  19. size_to_buf
  20. do_cdu535_request
  21. sony_get_toc
  22. find_track
  23. read_subcode
  24. sony_get_subchnl_info
  25. cdu_ioctl
  26. cdu_open
  27. cdu_release
  28. sony535_init
  29. sonycd535_setup
  30. cleanup_module

   1 /*
   2  * Sony CDU-535 interface device driver
   3  *
   4  * This is a modified version of the CDU-31A device driver (see below).
   5  * Changes were made using documentation for the CDU-531 (which Sony
   6  * assures me is very similar to the 535) and partial disassembly of the
   7  * DOS driver.  I used Minyard's driver and replaced the the CDU-31A
   8  * commands with the CDU-531 commands.  This was complicated by a different
   9  * interface protocol with the drive.  The driver is still polled.
  10  *
  11  * Data transfer rate is about 110 Kb/sec, theoretical maximum is 150 Kb/sec.
  12  * I tried polling without the sony_sleep during the data transfers but
  13  * it did not speed things up any.
  14  *
  15  *  5/23/93 (rgj) changed the major number to 21 to get rid of conflict
  16  * with CDU-31A driver.  This is the also the number from the Linux
  17  * Device Driver Registry for the Sony Drive.  Hope nobody else is using it.
  18  *
  19  *  8/29/93 (rgj) remove the configuring of the interface board address
  20  * from the top level configuration, you have to modify it in this file.
  21  *
  22  * 1/26/95 Made module-capable (Joel Katz <Stimpson@Panix.COM>)
  23  *
  24  * Things to do:
  25  *  - handle errors and status better, put everything into a single word
  26  *  - use interrupts (code mostly there, but a big hole still missing)
  27  *  - handle multi-session CDs?
  28  *  - use DMA?
  29  *
  30  *  Known Bugs:
  31  *  -
  32  *
  33  *   Ken Pizzini (ken@halcyon.com)
  34  *
  35  * Original by:
  36  *   Ron Jeppesen (ronj.an@site007.saic.com)
  37  *
  38  *
  39  *------------------------------------------------------------------------
  40  * Sony CDROM interface device driver.
  41  *
  42  * Corey Minyard (minyard@wf-rch.cirr.com) (CDU-535 complaints to Ken above)
  43  *
  44  * Colossians 3:17
  45  *
  46  * The Sony interface device driver handles Sony interface CDROM
  47  * drives and provides a complete block-level interface as well as an
  48  * ioctl() interface compatible with the Sun (as specified in
  49  * include/linux/cdrom.h).  With this interface, CDROMs can be
  50  * accessed and standard audio CDs can be played back normally.
  51  *
  52  * This interface is (unfortunately) a polled interface.  This is
  53  * because most Sony interfaces are set up with DMA and interrupts
  54  * disables.  Some (like mine) do not even have the capability to
  55  * handle interrupts or DMA.  For this reason you will see a lot of
  56  * the following:
  57  *
  58  *   retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
  59  *   while ((retry_count > jiffies) && (! <some condition to wait for))
  60  *   {
  61  *      while (handle_sony_cd_attention())
  62  *         ;
  63  *
  64  *      sony_sleep();
  65  *   }
  66  *   if (the condition not met)
  67  *   {
  68  *      return an error;
  69  *   }
  70  *
  71  * This ugly hack waits for something to happen, sleeping a little
  72  * between every try.  it also handles attentions, which are
  73  * asynchronous events from the drive informing the driver that a disk
  74  * has been inserted, removed, etc.
  75  *
  76  * One thing about these drives: They talk in MSF (Minute Second Frame) format.
  77  * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
  78  * disk.  The funny thing is that these are sent to the drive in BCD, but the
  79  * interface wants to see them in decimal.  A lot of conversion goes on.
  80  *
  81  *  Copyright (C) 1993  Corey Minyard
  82  *
  83  *  This program is free software; you can redistribute it and/or modify
  84  *  it under the terms of the GNU General Public License as published by
  85  *  the Free Software Foundation; either version 2 of the License, or
  86  *  (at your option) any later version.
  87  *
  88  *  This program is distributed in the hope that it will be useful,
  89  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  90  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  91  *  GNU General Public License for more details.
  92  *
  93  *  You should have received a copy of the GNU General Public License
  94  *  along with this program; if not, write to the Free Software
  95  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  96  *
  97  */
  98 
  99 
 100 #include <linux/config.h>
 101 #if defined(CONFIG_CDU535) || defined(MODULE)
 102 
 103 #ifdef MODULE
 104 # include <linux/module.h>
 105 # include <linux/malloc.h>
 106 # include <linux/version.h>
 107 # ifndef CONFIG_MODVERSIONS
 108         char kernel_version[]= UTS_RELEASE;
 109 # endif
 110 #endif
 111 
 112 #include <linux/errno.h>
 113 #include <linux/signal.h>
 114 #include <linux/sched.h>
 115 #include <linux/timer.h>
 116 #include <linux/fs.h>
 117 #include <linux/kernel.h>
 118 #include <linux/ioport.h>
 119 #include <linux/hdreg.h>
 120 #include <linux/genhd.h>
 121 #include <linux/mm.h>
 122 
 123 #define REALLY_SLOW_IO
 124 #include <asm/system.h>
 125 #include <asm/io.h>
 126 #include <asm/segment.h>
 127 
 128 #include <linux/cdrom.h>
 129 #include <linux/sonycd535.h>
 130 
 131 #define MAJOR_NR CDU535_CDROM_MAJOR
 132 
 133 #ifdef MODULE
 134 # include "/usr/src/linux/drivers/block/blk.h"
 135 #else
 136 # include "blk.h"
 137 # define MOD_INC_USE_COUNT
 138 # define MOD_DEC_USE_COUNT
 139 #endif
 140 
 141 /*
 142  * this is the base address of the interface card for the Sony CDU-535
 143  * CDROM drive.  If your jumpers are set for an address other than
 144  * this one (the default), change the following line to the
 145  * proper address.
 146  */
 147 #ifndef CDU535_ADDRESS
 148 # define CDU535_ADDRESS                 0x340
 149 #endif
 150 #ifndef CDU535_INTERRUPT
 151 # define CDU535_INTERRUPT               0
 152 #endif
 153 #ifndef CDU535_HANDLE
 154 # define CDU535_HANDLE                  "cdu535"
 155 #endif
 156 #ifndef CDU535_MESSAGE_NAME
 157 # define CDU535_MESSAGE_NAME    "Sony CDU-535"
 158 #endif
 159 
 160 #ifndef DEBUG
 161 # define DEBUG  1
 162 #endif
 163 
 164 /*
 165  *  SONY535_BUFFER_SIZE determines the size of internal buffer used
 166  *  by the drive.  It must be at least 2K and the larger the buffer
 167  *  the better the transfer rate.  It does however take system memory.
 168  *  On my system I get the following transfer rates using dd to read
 169  *  10 Mb off /dev/cdrom.
 170  *
 171  *    8K buffer      43 Kb/sec
 172  *   16K buffer      66 Kb/sec
 173  *   32K buffer      91 Kb/sec
 174  *   64K buffer     111 Kb/sec
 175  *  128K buffer     123 Kb/sec
 176  *  512K buffer     123 Kb/sec
 177  */
 178 #define SONY535_BUFFER_SIZE     (64*1024)
 179 
 180 /*
 181  *  if LOCK_DOORS is defined then the eject button is disabled while
 182  * the device is open.
 183  */
 184 #ifndef NO_LOCK_DOORS
 185 # define LOCK_DOORS
 186 #endif
 187 
 188 static int read_subcode(void);
 189 static void sony_get_toc(void);
 190 static int cdu_open(struct inode *inode, struct file *filp);
 191 static inline unsigned int int_to_bcd(unsigned int val);
 192 static unsigned int bcd_to_int(unsigned int bcd);
 193 static int do_sony_cmd(Byte * cmd, int nCmd, Byte status[2],
 194                                            Byte * response, int n_response, int ignoreStatusBit7);
 195 
 196 /* The base I/O address of the Sony Interface.  This is a variable (not a
 197    #define) so it can be easily changed via some future ioctl() */
 198 #ifndef MODULE
 199 static
 200 #endif
 201 unsigned short sony535_cd_base_io = CDU535_ADDRESS;
 202 
 203 /*
 204  * The following are I/O addresses of the various registers for the drive.  The
 205  * comment for the base address also applies here.
 206  */
 207 static unsigned short select_unit_reg;
 208 static unsigned short result_reg;
 209 static unsigned short command_reg;
 210 static unsigned short read_status_reg;
 211 static unsigned short data_reg;
 212 
 213 static int initialized = 0;                     /* Has the drive been initialized? */
 214 static int sony_disc_changed = 1;       /* Has the disk been changed
 215                                                                            since the last check? */
 216 static int sony_toc_read = 0;           /* Has the table of contents been
 217                                                                            read? */
 218 static unsigned int sony_buffer_size;   /* Size in bytes of the read-ahead
 219                                                                                    buffer. */
 220 static unsigned int sony_buffer_sectors;        /* Size (in 2048 byte records) of
 221                                                                                            the read-ahead buffer. */
 222 static unsigned int sony_usage = 0;     /* How many processes have the
 223                                                                            drive open. */
 224 
 225 static int sony_first_block = -1;       /* First OS block (512 byte) in
 226                                                                            the read-ahead buffer */
 227 static int sony_last_block = -1;        /* Last OS block (512 byte) in
 228                                                                            the read-ahead buffer */
 229 
 230 static struct s535_sony_toc *sony_toc;  /* Points to the table of
 231                                                                                    contents. */
 232 static struct s535_sony_subcode *last_sony_subcode;             /* Points to the last
 233                                                                                                                    subcode address read */
 234 #ifndef MODULE
 235 static Byte *sony_buffer;               /* Points to the read-ahead buffer */
 236 #else
 237 static Byte **sony_buffer;              /* Points to the pointers
 238                                                                    to the sector buffers */
 239 #endif
 240 static int sony_inuse = 0;              /* is the drive in use? Only one
 241                                                                    open at a time allowed */
 242 
 243 /*
 244  * The audio status uses the values from read subchannel data as specified
 245  * in include/linux/cdrom.h.
 246  */
 247 static int sony_audio_status = CDROM_AUDIO_NO_STATUS;
 248 
 249 /*
 250  * The following are a hack for pausing and resuming audio play.  The drive
 251  * does not work as I would expect it, if you stop it then start it again,
 252  * the drive seeks back to the beginning and starts over.  This holds the
 253  * position during a pause so a resume can restart it.  It uses the
 254  * audio status variable above to tell if it is paused.
 255  *   I just kept the CDU-31A driver behavior rather than using the PAUSE
 256  * command on the CDU-535.
 257  */
 258 static Byte cur_pos_msf[3] = {0, 0, 0};
 259 static Byte final_pos_msf[3] = {0, 0, 0};
 260 
 261 /* What IRQ is the drive using?  0 if none. */
 262 #ifndef MODULE
 263 static
 264 #endif
 265 int sony535_irq_used = CDU535_INTERRUPT;
 266 
 267 /* The interrupt handler will wake this queue up when it gets an interrupt. */
 268 static struct wait_queue *cdu535_irq_wait = NULL;
 269 
 270 
 271 /*
 272  * This routine returns 1 if the disk has been changed since the last
 273  * check or 0 if it hasn't.  Setting flag to 0 resets the changed flag.
 274  */
 275 static int
 276 cdu535_check_media_change(dev_t full_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278         int retval;
 279 
 280         if (MINOR(full_dev) != 0) {
 281                 printk(CDU535_MESSAGE_NAME " request error: invalid device.\n");
 282                 return 0;
 283         }
 284 
 285         /* if driver is not initialized, always return 0 */
 286         retval = initialized ? sony_disc_changed : 0;
 287         sony_disc_changed = 0;
 288         return retval;
 289 }
 290 
 291 static inline void
 292 enable_interrupts(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 293 {
 294 #ifdef USE_IRQ
 295         /* this code snarfed from cdu31a.c; it will not
 296          * directly work for the cdu535 as written...
 297          */
 298         curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
 299                                                 | SONY_RES_RDY_INT_EN_BIT
 300                                                 | SONY_DATA_RDY_INT_EN_BIT);
 301         outb(curr_control_reg, sony_cd_control_reg);
 302 #endif
 303 }
 304 
 305 static inline void
 306 disable_interrupts(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308 #ifdef USE_IRQ
 309         /* this code snarfed from cdu31a.c; it will not
 310          * directly work for the cdu535 as written...
 311          */
 312         curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
 313                                                 | SONY_RES_RDY_INT_EN_BIT
 314                                                 | SONY_DATA_RDY_INT_EN_BIT);
 315         outb(curr_control_reg, sony_cd_control_reg);
 316 #endif
 317 }
 318 
 319 static void
 320 cdu535_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322         disable_interrupts();
 323         if (cdu535_irq_wait != NULL)
 324                 wake_up(&cdu535_irq_wait);
 325         else
 326                 printk(CDU535_MESSAGE_NAME
 327                                 ": Got an interrupt but nothing was waiting\n");
 328 }
 329 
 330 
 331 /*
 332  * Wait a little while (used for polling the drive).  If in initialization,
 333  * setting a timeout doesn't work, so just loop for a while.  (We trust
 334  * that the sony_sleep() call is protected by a test for proper jiffies count.)
 335  */
 336 static inline void
 337 sony_sleep(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339         if (sony535_irq_used <= 0) {    /* poll */
 340                 current->state = TASK_INTERRUPTIBLE;
 341                 current->timeout = jiffies;
 342                 schedule();
 343         } else {        /* Interrupt driven */
 344                 cli();
 345                 enable_interrupts();
 346                 interruptible_sleep_on(&cdu535_irq_wait);
 347                 sti();
 348         }
 349 }
 350 
 351 /*------------------start of SONY CDU535 very specific ---------------------*/
 352 
 353 /****************************************************************************
 354  * void select_unit( int unit_no )
 355  *
 356  *  Select the specified unit (0-3) so that subsequent commands reference it
 357  ****************************************************************************/
 358 static void
 359 select_unit(int unit_no)
     /* [previous][next][first][last][top][bottom][index][help] */
 360 {
 361         unsigned int select_mask = ~(1 << unit_no);
 362         outb(select_mask, select_unit_reg);
 363 }
 364 
 365 /***************************************************************************
 366  * int read_result_reg( Byte *data_ptr )
 367  *
 368  *  Read a result byte from the Sony CDU controller, store in location pointed
 369  * to by data_ptr.  Return zero on success, TIME_OUT if we did not receive
 370  * data.
 371  ***************************************************************************/
 372 static int
 373 read_result_reg(Byte *data_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 374 {
 375         int retry_count;
 376         int read_status;
 377 
 378         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 379         while (jiffies < retry_count) {
 380                 if (((read_status = inb(read_status_reg)) & SONY535_RESULT_NOT_READY_BIT) == 0) {
 381 #if DEBUG > 1
 382                         printk(CDU535_MESSAGE_NAME
 383                                         ": read_result_reg(): readStatReg = 0x%x\n", read_status);
 384 #endif
 385                         *data_ptr = inb(result_reg);
 386                         return 0;
 387                 } else {
 388                         sony_sleep();
 389                 }
 390         }
 391         printk(CDU535_MESSAGE_NAME " read_result_reg: TIME OUT!\n");
 392         return TIME_OUT;
 393 }
 394 
 395 /****************************************************************************
 396  * int read_exec_status( Byte status[2] )
 397  *
 398  *  Read the execution status of the last command and put into status.
 399  * Handles reading second status word if available.  Returns 0 on success,
 400  * TIME_OUT on failure.
 401  ****************************************************************************/
 402 static int
 403 read_exec_status(Byte status[2])
     /* [previous][next][first][last][top][bottom][index][help] */
 404 {
 405         status[1] = 0;
 406         if (read_result_reg(&(status[0])) != 0)
 407                 return TIME_OUT;
 408         if ((status[0] & 0x80) != 0) {  /* byte two follows */
 409                 if (read_result_reg(&(status[1])) != 0)
 410                         return TIME_OUT;
 411         }
 412 #if DEBUG > 1
 413         printk(CDU535_MESSAGE_NAME ": read_exec_status: read 0x%x 0x%x\n",
 414                         status[0], status[1]);
 415 #endif
 416         return 0;
 417 }
 418 
 419 /****************************************************************************
 420  * int check_drive_status( void )
 421  *
 422  *  Check the current drive status.  Using this before executing a command
 423  * takes care of the problem of unsolicited drive status-2 messages.
 424  * Add a check of the audio status if we think the disk is playing.
 425  ****************************************************************************/
 426 static int
 427 check_drive_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 428 {
 429         Byte status, e_status[2];
 430         int  CDD, ATN;
 431         Byte cmd;
 432 
 433         select_unit(0);
 434         if (sony_audio_status == CDROM_AUDIO_PLAY) {    /* check status */
 435                 outb(SONY535_REQUEST_AUDIO_STATUS, command_reg);
 436                 if (read_result_reg(&status) == 0) {
 437                         switch (status) {
 438                         case 0x0:
 439                                 break;          /* play in progress */
 440                         case 0x1:
 441                                 break;          /* paused */
 442                         case 0x3:               /* audio play completed */
 443                         case 0x5:               /* play not requested */
 444                                 sony_audio_status = CDROM_AUDIO_COMPLETED;
 445                                 read_subcode();
 446                                 break;
 447                         case 0x4:               /* error during play */
 448                                 sony_audio_status = CDROM_AUDIO_ERROR;
 449                                 break;
 450                         }
 451                 }
 452         }
 453         /* now check drive status */
 454         outb(SONY535_REQUEST_DRIVE_STATUS_2, command_reg);
 455         if (read_result_reg(&status) != 0)
 456                 return TIME_OUT;
 457 
 458 #if DEBUG > 1
 459         printk(CDU535_MESSAGE_NAME ": check_drive_status() got 0x%x\n", status);
 460 #endif
 461 
 462         if (status == 0)
 463                 return 0;
 464 
 465         ATN = status & 0xf;
 466         CDD = (status >> 4) & 0xf;
 467 
 468         switch (ATN) {
 469         case 0x0:
 470                 break;                                  /* go on to CDD stuff */
 471         case SONY535_ATN_BUSY:
 472                 if (initialized)
 473                         printk(CDU535_MESSAGE_NAME " error: drive busy\n");
 474                 return CD_BUSY;
 475         case SONY535_ATN_EJECT_IN_PROGRESS:
 476                 printk(CDU535_MESSAGE_NAME " error: eject in progress\n");
 477                 sony_audio_status = CDROM_AUDIO_INVALID;
 478                 return CD_BUSY;
 479         case SONY535_ATN_RESET_OCCURRED:
 480         case SONY535_ATN_DISC_CHANGED:
 481         case SONY535_ATN_RESET_AND_DISC_CHANGED:
 482 #if DEBUG > 0
 483                 printk(CDU535_MESSAGE_NAME " notice: reset occurred or disc changed\n");
 484 #endif
 485                 sony_disc_changed = 1;
 486                 sony_toc_read = 0;
 487                 sony_audio_status = CDROM_AUDIO_NO_STATUS;
 488                 sony_first_block = -1;
 489                 sony_last_block = -1;
 490                 if (initialized) {
 491                         cmd = SONY535_SPIN_UP;
 492                         do_sony_cmd(&cmd, 1, e_status, NULL, 0, 0);
 493                         sony_get_toc();
 494                 }
 495                 return 0;
 496         default:
 497                 printk(CDU535_MESSAGE_NAME " error: drive busy (ATN=0x%x)\n", ATN);
 498                 return CD_BUSY;
 499         }
 500         switch (CDD) {                  /* the 531 docs are not helpful in decoding this */
 501         case 0x0:                               /* just use the values from the DOS driver */
 502         case 0x2:
 503         case 0xa:
 504                 break;                          /* no error */
 505         case 0xc:
 506                 printk(CDU535_MESSAGE_NAME
 507                                 ": check_drive_status(): CDD = 0xc! Not properly handled!\n");
 508                 return CD_BUSY;         /* ? */
 509         default:
 510                 return CD_BUSY;
 511         }
 512         return 0;
 513 }       /* check_drive_status() */
 514 
 515 /*****************************************************************************
 516  * int do_sony_cmd( Byte *cmd, int n_cmd, Byte status[2],
 517  *                Byte *response, int n_response, int ignore_status_bit7 )
 518  *
 519  *  Generic routine for executing commands.  The command and its parameters
 520  *  should be placed in the cmd[] array, number of bytes in the command is
 521  *  stored in nCmd.  The response from the command will be stored in the
 522  *  response array.  The number of bytes you expect back (excluding status)
 523  *  should be passed in n_response.  Finally, some
 524  *  commands set bit 7 of the return status even when there is no second
 525  *  status byte, on these commands set ignoreStatusBit7 TRUE.
 526  *    If the command was sent and data received back, then we return 0,
 527  *  else we return TIME_OUT.  You still have to check the status yourself.
 528  *    You should call check_drive_status() before calling this routine
 529  *  so that you do not lose notifications of disk changes, etc.
 530  ****************************************************************************/
 531 static int
 532 do_sony_cmd(Byte * cmd, int n_cmd, Byte status[2],
     /* [previous][next][first][last][top][bottom][index][help] */
 533                         Byte * response, int n_response, int ignore_status_bit7)
 534 {
 535         int i;
 536 
 537         /* write out the command */
 538         for (i = 0; i < n_cmd; i++)
 539                 outb(cmd[i], command_reg);
 540 
 541         /* read back the status */
 542         if (read_result_reg(status) != 0)
 543                 return TIME_OUT;
 544         if (!ignore_status_bit7 && ((status[0] & 0x80) != 0)) {
 545                 /* get second status byte */
 546                 if (read_result_reg(status + 1) != 0)
 547                         return TIME_OUT;
 548         } else {
 549                 status[1] = 0;
 550         }
 551 #if DEBUG > 2
 552         printk(CDU535_MESSAGE_NAME ": do_sony_cmd %x: %x %x\n",
 553                         *cmd, status[0], status[1]);
 554 #endif
 555 
 556         /* do not know about when I should read set of data and when not to */
 557         if ((status[0] & ((ignore_status_bit7 ? 0x7f : 0xff) & 0x8f)) != 0)
 558                 return 0;
 559 
 560         /* else, read in rest of data */
 561         for (i = 0; 0 < n_response; n_response--, i++)
 562                 if (read_result_reg(response + i) != 0)
 563                         return TIME_OUT;
 564         return 0;
 565 }       /* do_sony_cmd() */
 566 
 567 /**************************************************************************
 568  * int set_drive_mode( int mode, Byte status[2] )
 569  *
 570  *  Set the drive mode to the specified value (mode=0 is audio, mode=e0
 571  * is mode-1 CDROM
 572  **************************************************************************/
 573 static int
 574 set_drive_mode(int mode, Byte status[2])
     /* [previous][next][first][last][top][bottom][index][help] */
 575 {
 576         Byte cmd_buff[2];
 577         Byte ret_buff[1];
 578 
 579         cmd_buff[0] = SONY535_SET_DRIVE_MODE;
 580         cmd_buff[1] = mode;
 581         return do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1);
 582 }
 583 
 584 /***************************************************************************
 585  * int seek_and_read_N_blocks( Byte params[], int n_blocks, Byte status[2],
 586  *                             Byte *data_buff, int buff_size )
 587  *
 588  *  Read n_blocks of data from the CDROM starting at position params[0:2],
 589  *  number of blocks in stored in params[3:5] -- both these are already
 590  *  int bcd format.
 591  *  Transfer the data into the buffer pointed at by data_buff.  buff_size
 592  *  gives the number of bytes available in the buffer.
 593  *    The routine returns number of bytes read in if successful, otherwise
 594  *  it returns one of the standard error returns.
 595  ***************************************************************************/
 596 static int
 597 #ifndef MODULE
 598 seek_and_read_N_blocks(Byte params[], int n_blocks, Byte status[2],
     /* [previous][next][first][last][top][bottom][index][help] */
 599                                            Byte * data_buff, int buf_size)
 600 #else
 601 seek_and_read_N_blocks(Byte params[], int n_blocks, Byte status[2],
 602                                            Byte **buff, int buf_size)
 603 #endif
 604 {
 605         const int block_size = 2048;
 606         Byte cmd_buff[7];
 607         int  i;
 608         int  read_status;
 609         int  retry_count;
 610 #ifndef MODULE
 611         Byte *start_pos = data_buff;
 612 #else
 613         Byte *data_buff;
 614         int  sector_count = 0;
 615 #endif
 616 
 617         if (buf_size < ((long)block_size) * n_blocks)
 618                 return NO_ROOM;
 619 
 620         set_drive_mode(SONY535_CDROM_DRIVE_MODE, status);
 621 
 622         /* send command to read the data */
 623         cmd_buff[0] = SONY535_SEEK_AND_READ_N_BLOCKS_1;
 624         for (i = 0; i < 6; i++)
 625                 cmd_buff[i + 1] = params[i];
 626         for (i = 0; i < 7; i++)
 627                 outb(cmd_buff[i], command_reg);
 628 
 629         /* read back the data one block at a time */
 630         while (0 < n_blocks--) {
 631                 /* wait for data to be ready */
 632                 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
 633                 while (jiffies < retry_count) {
 634                         read_status = inb(read_status_reg);
 635                         if ((read_status & SONY535_RESULT_NOT_READY_BIT) == 0) {
 636                                 read_exec_status(status);
 637                                 return BAD_STATUS;
 638                         }
 639                         if ((read_status & SONY535_DATA_NOT_READY_BIT) == 0) {
 640                                 /* data is ready, read it */
 641 #ifdef MODULE
 642                                 data_buff = buff[sector_count++];
 643 #endif
 644                                 for (i = 0; i < block_size; i++)
 645                                         *data_buff++ = inb(data_reg);   /* unrolling this loop does not seem to help */
 646                                 break;                  /* exit the timeout loop */
 647                         }
 648                         sony_sleep();           /* data not ready, sleep a while */
 649                 }
 650                 if (retry_count <= jiffies)
 651                         return TIME_OUT;        /* if we reach this stage */
 652         }
 653 
 654         /* read all the data, now read the status */
 655         if ((i = read_exec_status(status)) != 0)
 656                 return i;
 657 #ifndef MODULE
 658         return data_buff - start_pos;
 659 #else
 660         return block_size * sector_count;
 661 #endif
 662 }       /* seek_and_read_N_blocks() */
 663 
 664 /****************************************************************************
 665  * int request_toc_data( Byte status[2], struct s535_sony_toc *toc )
 666  *
 667  *  Read in the table of contents data.  Converts all the bcd data
 668  * into integers in the toc structure.
 669  ****************************************************************************/
 670 static int
 671 request_toc_data(Byte status[2], struct s535_sony_toc *toc)
     /* [previous][next][first][last][top][bottom][index][help] */
 672 {
 673         int  to_status;
 674         int  i, j, n_tracks, track_no;
 675         int  first_track_num, last_track_num;
 676         Byte cmd_no = 0xb2;
 677         Byte track_address_buffer[5];
 678 
 679         /* read the fixed portion of the table of contents */
 680         if ((to_status = do_sony_cmd(&cmd_no, 1, status, (Byte *) toc, 15, 1)) != 0)
 681                 return to_status;
 682 
 683         /* convert the data into integers so we can use them */
 684         first_track_num = bcd_to_int(toc->first_track_num);
 685         last_track_num = bcd_to_int(toc->last_track_num);
 686         n_tracks = last_track_num - first_track_num + 1;
 687 
 688         /* read each of the track address descriptors */
 689         for (i = 0; i < n_tracks; i++) {
 690                 /* read the descriptor into a temporary buffer */
 691                 for (j = 0; j < 5; j++) {
 692                         if (read_result_reg(track_address_buffer + j) != 0)
 693                                 return TIME_OUT;
 694                         if (j == 1)             /* need to convert from bcd */
 695                                 track_no = bcd_to_int(track_address_buffer[j]);
 696                 }
 697                 /* copy the descriptor to proper location - sonycd.c just fills */
 698                 memcpy(toc->tracks + i, track_address_buffer, 5);
 699         }
 700         return 0;
 701 }       /* request_toc_data() */
 702 
 703 /***************************************************************************
 704  * int spin_up_drive( Byte status[2] )
 705  *
 706  *  Spin up the drive (unless it is already spinning).
 707  ***************************************************************************/
 708 static int
 709 spin_up_drive(Byte status[2])
     /* [previous][next][first][last][top][bottom][index][help] */
 710 {
 711         Byte cmd;
 712 
 713         /* first see if the drive is already spinning */
 714         cmd = SONY535_REQUEST_DRIVE_STATUS_1;
 715         if (do_sony_cmd(&cmd, 1, status, NULL, 0, 0) != 0)
 716                 return TIME_OUT;
 717         if ((status[0] & SONY535_STATUS1_NOT_SPINNING) == 0)
 718                 return 0;       /* it's already spinning */
 719 
 720         /* otherwise, give the spin-up command */
 721         cmd = SONY535_SPIN_UP;
 722         return do_sony_cmd(&cmd, 1, status, NULL, 0, 0);
 723 }
 724 
 725 /*--------------------end of SONY CDU535 very specific ---------------------*/
 726 
 727 /* Convert from an integer 0-99 to BCD */
 728 static inline unsigned int
 729 int_to_bcd(unsigned int val)
     /* [previous][next][first][last][top][bottom][index][help] */
 730 {
 731         int retval;
 732 
 733         retval = (val / 10) << 4;
 734         retval = retval | val % 10;
 735         return retval;
 736 }
 737 
 738 
 739 /* Convert from BCD to an integer from 0-99 */
 740 static unsigned int
 741 bcd_to_int(unsigned int bcd)
     /* [previous][next][first][last][top][bottom][index][help] */
 742 {
 743         return (((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f);
 744 }
 745 
 746 
 747 /*
 748  * Convert a logical sector value (like the OS would want to use for
 749  * a block device) to an MSF format.
 750  */
 751 static void
 752 log_to_msf(unsigned int log, Byte *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
 753 {
 754         log = log + LOG_START_OFFSET;
 755         msf[0] = int_to_bcd(log / 4500);
 756         log = log % 4500;
 757         msf[1] = int_to_bcd(log / 75);
 758         msf[2] = int_to_bcd(log % 75);
 759 }
 760 
 761 
 762 /*
 763  * Convert an MSF format to a logical sector.
 764  */
 765 static unsigned int
 766 msf_to_log(Byte *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
 767 {
 768         unsigned int log;
 769 
 770 
 771         log = bcd_to_int(msf[2]);
 772         log += bcd_to_int(msf[1]) * 75;
 773         log += bcd_to_int(msf[0]) * 4500;
 774         log = log - LOG_START_OFFSET;
 775 
 776         return log;
 777 }
 778 
 779 
 780 /*
 781  * Take in integer size value and put it into a buffer like
 782  * the drive would want to see a number-of-sector value.
 783  */
 784 static void
 785 size_to_buf(unsigned int size, Byte *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 786 {
 787         buf[0] = size / 65536;
 788         size = size % 65536;
 789         buf[1] = size / 256;
 790         buf[2] = size % 256;
 791 }
 792 
 793 
 794 /*
 795  * The OS calls this to perform a read or write operation to the drive.
 796  * Write obviously fail.  Reads to a read ahead of sony_buffer_size
 797  * bytes to help speed operations.  This especially helps since the OS
 798  * uses 1024 byte blocks and the drive uses 2048 byte blocks.  Since most
 799  * data access on a CD is done sequentially, this saves a lot of operations.
 800  */
 801 static void
 802 do_cdu535_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 803 {
 804         unsigned int dev;
 805         unsigned int read_size;
 806         int  block;
 807         int  nsect;
 808         int  copyoff;
 809         int  spin_up_retry;
 810         Byte params[10];
 811         Byte status[2];
 812         Byte cmd[2];
 813 
 814 
 815         if (!sony_inuse) {
 816                 cdu_open(NULL, NULL);
 817         }
 818         while (1) {
 819                 /*
 820                  * The beginning here is stolen from the hard disk driver.  I hope
 821                  * it's right.
 822                  */
 823                 if (!(CURRENT) || CURRENT->dev < 0) {
 824                         return;
 825                 }
 826                 INIT_REQUEST;
 827                 dev = MINOR(CURRENT->dev);
 828                 block = CURRENT->sector;
 829                 nsect = CURRENT->nr_sectors;
 830                 if (dev != 0) {
 831                         end_request(0);
 832                         continue;
 833                 }
 834                 switch (CURRENT->cmd) {
 835                 case READ:
 836                         /*
 837                          * If the block address is invalid or the request goes beyond the end of
 838                          * the media, return an error.
 839                          */
 840 
 841                         if (sony_toc->lead_out_start_lba <= (block / 4)) {
 842                                 end_request(0);
 843                                 return;
 844                         }
 845                         if (sony_toc->lead_out_start_lba <= ((block + nsect) / 4)) {
 846                                 end_request(0);
 847                                 return;
 848                         }
 849                         while (0 < nsect) {
 850                                 /*
 851                                  * If the requested sector is not currently in the read-ahead buffer,
 852                                  * it must be read in.
 853                                  */
 854                                 if ((block < sony_first_block) || (sony_last_block < block)) {
 855                                         sony_first_block = (block / 4) * 4;
 856                                         log_to_msf(block / 4, params);
 857 
 858                                         /*
 859                                          * If the full read-ahead would go beyond the end of the media, trim
 860                                          * it back to read just till the end of the media.
 861                                          */
 862                                         if (sony_toc->lead_out_start_lba <= ((block / 4) + sony_buffer_sectors)) {
 863                                                 sony_last_block = (sony_toc->lead_out_start_lba * 4) - 1;
 864                                                 read_size = sony_toc->lead_out_start_lba - (block / 4);
 865                                         } else {
 866                                                 sony_last_block = sony_first_block + (sony_buffer_sectors * 4) - 1;
 867                                                 read_size = sony_buffer_sectors;
 868                                         }
 869                                         size_to_buf(read_size, &params[3]);
 870 
 871                                         /*
 872                                          * Read the data.  If the drive was not spinning,
 873                                          * spin it up and try once more.
 874                                          */
 875                                         spin_up_retry = 0;
 876                                         for (;;) {
 877 #if DEBUG > 1
 878                                                 if (check_drive_status() != 0) {
 879                                                         /* drive not ready */
 880                                                         sony_first_block = -1;
 881                                                         sony_last_block = -1;
 882                                                         end_request(0);
 883                                                         return;
 884                                                 }
 885 #endif
 886                                                 if (0 <= seek_and_read_N_blocks(params, read_size,
 887                                                                         status, sony_buffer, (read_size * 2048)))
 888                                                         break;
 889                                                 if (!(status[0] & SONY535_STATUS1_NOT_SPINNING) ||
 890                                                                                                                         spin_up_retry) {
 891                                                         printk(CDU535_MESSAGE_NAME " Read error: 0x%.2x\n",
 892                                                                         status[0]);
 893                                                         sony_first_block = -1;
 894                                                         sony_last_block = -1;
 895                                                         end_request(0);
 896                                                         return;
 897                                                 }
 898                                                 printk(CDU535_MESSAGE_NAME
 899                                                         " debug: calling spin up when reading data!\n");
 900                                                 cmd[0] = SONY535_SPIN_UP;
 901                                                 do_sony_cmd(cmd, 1, status, NULL, 0, 0);
 902                                                 spin_up_retry = 1;
 903                                         }
 904                                 }
 905                                 /*
 906                                  * The data is in memory now, copy it to the buffer and advance to the
 907                                  * next block to read.
 908                                  */
 909 #ifndef MODULE
 910                                 copyoff = (block - sony_first_block) * 512;
 911                                 memcpy(CURRENT->buffer, sony_buffer + copyoff, 512);
 912 #else
 913                                 copyoff = block - sony_first_block;
 914                                 memcpy(CURRENT->buffer,
 915                                            sony_buffer[copyoff / 4] + 512 * (copyoff % 4), 512);
 916 #endif
 917 
 918                                 block += 1;
 919                                 nsect -= 1;
 920                                 CURRENT->buffer += 512;
 921                         }
 922 
 923                         end_request(1);
 924                         break;
 925 
 926                 case WRITE:
 927                         end_request(0);
 928                         break;
 929 
 930                 default:
 931                         panic("Unknown SONY CD cmd");
 932                 }
 933         }
 934 }
 935 
 936 
 937 /*
 938  * Read the table of contents from the drive and set sony_toc_read if
 939  * successful.
 940  */
 941 static void
 942 sony_get_toc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944         Byte status[2];
 945         if (!sony_toc_read) {
 946                 /* do not call check_drive_status() from here since it can call this routine */
 947                 if (request_toc_data(status, sony_toc) < 0)
 948                         return;
 949                 sony_toc->lead_out_start_lba = msf_to_log(sony_toc->lead_out_start_msf);
 950                 sony_toc_read = 1;
 951         }
 952 }
 953 
 954 
 955 /*
 956  * Search for a specific track in the table of contents.  track is
 957  * passed in bcd format
 958  */
 959 static int
 960 find_track(int track)
     /* [previous][next][first][last][top][bottom][index][help] */
 961 {
 962         int i;
 963         int num_tracks;
 964 
 965 
 966         num_tracks = bcd_to_int(sony_toc->last_track_num) -
 967                 bcd_to_int(sony_toc->first_track_num) + 1;
 968         for (i = 0; i < num_tracks; i++) {
 969                 if (sony_toc->tracks[i].track == track) {
 970                         return i;
 971                 }
 972         }
 973 
 974         return -1;
 975 }
 976 
 977 /*
 978  * Read the subcode and put it int last_sony_subcode for future use.
 979  */
 980 static int
 981 read_subcode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 982 {
 983         Byte cmd = SONY535_REQUEST_SUB_Q_DATA;
 984         Byte status[2];
 985         int  dsc_status;
 986 
 987         if (check_drive_status() != 0)
 988                 return -EIO;
 989 
 990         if ((dsc_status = do_sony_cmd(&cmd, 1, status, (Byte *) last_sony_subcode,
 991                                                            sizeof(struct s535_sony_subcode), 1)) != 0) {
 992                 printk(CDU535_MESSAGE_NAME " error 0x%.2x, %d (read_subcode)\n",
 993                                 status[0], dsc_status);
 994                 return -EIO;
 995         }
 996         return 0;
 997 }
 998 
 999 
1000 /*
1001  * Get the subchannel info like the CDROMSUBCHNL command wants to see it.  If
1002  * the drive is playing, the subchannel needs to be read (since it would be
1003  * changing).  If the drive is paused or completed, the subcode information has
1004  * already been stored, just use that.  The ioctl call wants things in decimal
1005  * (not BCD), so all the conversions are done.
1006  */
1007 static int
1008 sony_get_subchnl_info(long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1009 {
1010         struct cdrom_subchnl schi;
1011 
1012 
1013         /* Get attention stuff */
1014         if (check_drive_status() != 0)
1015                 return -EIO;
1016 
1017         sony_get_toc();
1018         if (!sony_toc_read) {
1019                 return -EIO;
1020         }
1021         verify_area(VERIFY_WRITE /* and read */ , (char *)arg, sizeof schi);
1022 
1023         memcpy_fromfs(&schi, (char *)arg, sizeof schi);
1024 
1025         switch (sony_audio_status) {
1026         case CDROM_AUDIO_PLAY:
1027                 if (read_subcode() < 0) {
1028                         return -EIO;
1029                 }
1030                 break;
1031 
1032         case CDROM_AUDIO_PAUSED:
1033         case CDROM_AUDIO_COMPLETED:
1034                 break;
1035 
1036         case CDROM_AUDIO_NO_STATUS:
1037                 schi.cdsc_audiostatus = sony_audio_status;
1038                 memcpy_tofs((char *)arg, &schi, sizeof schi);
1039                 return 0;
1040                 break;
1041 
1042         case CDROM_AUDIO_INVALID:
1043         case CDROM_AUDIO_ERROR:
1044         default:
1045                 return -EIO;
1046         }
1047 
1048         schi.cdsc_audiostatus = sony_audio_status;
1049         schi.cdsc_adr = last_sony_subcode->address;
1050         schi.cdsc_ctrl = last_sony_subcode->control;
1051         schi.cdsc_trk = bcd_to_int(last_sony_subcode->track_num);
1052         schi.cdsc_ind = bcd_to_int(last_sony_subcode->index_num);
1053         if (schi.cdsc_format == CDROM_MSF) {
1054                 schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode->abs_msf[0]);
1055                 schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode->abs_msf[1]);
1056                 schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode->abs_msf[2]);
1057 
1058                 schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode->rel_msf[0]);
1059                 schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode->rel_msf[1]);
1060                 schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode->rel_msf[2]);
1061         } else if (schi.cdsc_format == CDROM_LBA) {
1062                 schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode->abs_msf);
1063                 schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode->rel_msf);
1064         }
1065         memcpy_tofs((char *)arg, &schi, sizeof schi);
1066         return 0;
1067 }
1068 
1069 
1070 /*
1071  * The big ugly ioctl handler.
1072  */
1073 static int
1074 cdu_ioctl(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1075                   struct file *file,
1076                   unsigned int cmd,
1077                   unsigned long arg)
1078 {
1079         unsigned int dev;
1080         Byte status[2];
1081         Byte cmd_buff[10], params[10];
1082         int  i, dsc_status;
1083 
1084 
1085         if (!inode) {
1086                 return -EINVAL;
1087         }
1088         dev = MINOR(inode->i_rdev) >> 6;
1089         if (dev != 0) {
1090                 return -EINVAL;
1091         }
1092         if (check_drive_status() != 0)
1093                 return -EIO;
1094 
1095         switch (cmd) {
1096         case CDROMSTART:                        /* Spin up the drive */
1097                 if (spin_up_drive(status) < 0) {
1098                         printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTART)\n",
1099                                         status[0]);
1100                         return -EIO;
1101                 }
1102                 return 0;
1103                 break;
1104 
1105         case CDROMSTOP:                 /* Spin down the drive */
1106                 cmd_buff[0] = SONY535_HOLD;
1107                 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1108 
1109                 /*
1110                  * Spin the drive down, ignoring the error if the disk was
1111                  * already not spinning.
1112                  */
1113                 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1114                 cmd_buff[0] = SONY535_SPIN_DOWN;
1115                 dsc_status = do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1116                 if (((dsc_status < 0) && (dsc_status != BAD_STATUS)) ||
1117                         ((status[0] & ~(SONY535_STATUS1_NOT_SPINNING)) != 0)) {
1118                         printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTOP)\n",
1119                                         status[0]);
1120                         return -EIO;
1121                 }
1122                 return 0;
1123                 break;
1124 
1125         case CDROMPAUSE:                        /* Pause the drive */
1126                 cmd_buff[0] = SONY535_HOLD;             /* CDU-31 driver uses AUDIO_STOP, not pause */
1127                 if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
1128                         printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPAUSE)\n",
1129                                         status[0]);
1130                         return -EIO;
1131                 }
1132                 /* Get the current position and save it for resuming */
1133                 if (read_subcode() < 0) {
1134                         return -EIO;
1135                 }
1136                 cur_pos_msf[0] = last_sony_subcode->abs_msf[0];
1137                 cur_pos_msf[1] = last_sony_subcode->abs_msf[1];
1138                 cur_pos_msf[2] = last_sony_subcode->abs_msf[2];
1139                 sony_audio_status = CDROM_AUDIO_PAUSED;
1140                 return 0;
1141                 break;
1142 
1143         case CDROMRESUME:                       /* Start the drive after being paused */
1144                 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1145 
1146                 if (sony_audio_status != CDROM_AUDIO_PAUSED) {
1147                         return -EINVAL;
1148                 }
1149                 spin_up_drive(status);
1150 
1151                 /* Start the drive at the saved position. */
1152                 cmd_buff[0] = SONY535_PLAY_AUDIO;
1153                 cmd_buff[1] = 0;                /* play back starting at this address */
1154                 cmd_buff[2] = cur_pos_msf[0];
1155                 cmd_buff[3] = cur_pos_msf[1];
1156                 cmd_buff[4] = cur_pos_msf[2];
1157                 cmd_buff[5] = SONY535_PLAY_AUDIO;
1158                 cmd_buff[6] = 2;                /* set ending address */
1159                 cmd_buff[7] = final_pos_msf[0];
1160                 cmd_buff[8] = final_pos_msf[1];
1161                 cmd_buff[9] = final_pos_msf[2];
1162                 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1163                         (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1164                         printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMRESUME)\n",
1165                                         status[0]);
1166                         return -EIO;
1167                 }
1168                 sony_audio_status = CDROM_AUDIO_PLAY;
1169                 return 0;
1170                 break;
1171 
1172         case CDROMPLAYMSF:                      /* Play starting at the given MSF address. */
1173                 verify_area(VERIFY_READ, (char *)arg, 6);
1174                 spin_up_drive(status);
1175                 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1176                 memcpy_fromfs(params, (void *)arg, 6);
1177 
1178                 /* The parameters are given in int, must be converted */
1179                 for (i = 0; i < 3; i++) {
1180                         cmd_buff[2 + i] = int_to_bcd(params[i]);
1181                         cmd_buff[7 + i] = int_to_bcd(params[i + 3]);
1182                 }
1183                 cmd_buff[0] = SONY535_PLAY_AUDIO;
1184                 cmd_buff[1] = 0;                /* play back starting at this address */
1185                 /* cmd_buff[2-4] are filled in for loop above */
1186                 cmd_buff[5] = SONY535_PLAY_AUDIO;
1187                 cmd_buff[6] = 2;                /* set ending address */
1188                 /* cmd_buff[7-9] are filled in for loop above */
1189                 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1190                         (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1191                         printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYMSF)\n",
1192                                         status[0]);
1193                         return -EIO;
1194                 }
1195                 /* Save the final position for pauses and resumes */
1196                 final_pos_msf[0] = cmd_buff[7];
1197                 final_pos_msf[1] = cmd_buff[8];
1198                 final_pos_msf[2] = cmd_buff[9];
1199                 sony_audio_status = CDROM_AUDIO_PLAY;
1200                 return 0;
1201                 break;
1202 
1203         case CDROMREADTOCHDR:           /* Read the table of contents header */
1204                 {
1205                         struct cdrom_tochdr *hdr;
1206                         struct cdrom_tochdr loc_hdr;
1207 
1208                         sony_get_toc();
1209                         if (!sony_toc_read)
1210                                 return -EIO;
1211                         hdr = (struct cdrom_tochdr *)arg;
1212                         verify_area(VERIFY_WRITE, hdr, sizeof *hdr);
1213                         loc_hdr.cdth_trk0 = bcd_to_int(sony_toc->first_track_num);
1214                         loc_hdr.cdth_trk1 = bcd_to_int(sony_toc->last_track_num);
1215                         memcpy_tofs(hdr, &loc_hdr, sizeof *hdr);
1216                 }
1217                 return 0;
1218                 break;
1219 
1220         case CDROMREADTOCENTRY: /* Read a given table of contents entry */
1221                 {
1222                         struct cdrom_tocentry *entry;
1223                         struct cdrom_tocentry loc_entry;
1224                         int  track_idx;
1225                         Byte *msf_val = NULL;
1226 
1227                         sony_get_toc();
1228                         if (!sony_toc_read) {
1229                                 return -EIO;
1230                         }
1231                         entry = (struct cdrom_tocentry *)arg;
1232                         verify_area(VERIFY_WRITE /* and read */ , entry, sizeof *entry);
1233 
1234                         memcpy_fromfs(&loc_entry, entry, sizeof loc_entry);
1235 
1236                         /* Lead out is handled separately since it is special. */
1237                         if (loc_entry.cdte_track == CDROM_LEADOUT) {
1238                                 loc_entry.cdte_adr = 0 /*sony_toc->address2 */ ;
1239                                 loc_entry.cdte_ctrl = sony_toc->control2;
1240                                 msf_val = sony_toc->lead_out_start_msf;
1241                         } else {
1242                                 track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
1243                                 if (track_idx < 0)
1244                                         return -EINVAL;
1245                                 loc_entry.cdte_adr = 0 /*sony_toc->tracks[track_idx].address */ ;
1246                                 loc_entry.cdte_ctrl = sony_toc->tracks[track_idx].control;
1247                                 msf_val = sony_toc->tracks[track_idx].track_start_msf;
1248                         }
1249 
1250                         /* Logical buffer address or MSF format requested? */
1251                         if (loc_entry.cdte_format == CDROM_LBA) {
1252                                 loc_entry.cdte_addr.lba = msf_to_log(msf_val);
1253                         } else if (loc_entry.cdte_format == CDROM_MSF) {
1254                                 loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
1255                                 loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val + 1));
1256                                 loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val + 2));
1257                         }
1258                         memcpy_tofs(entry, &loc_entry, sizeof *entry);
1259                 }
1260                 return 0;
1261                 break;
1262 
1263         case CDROMPLAYTRKIND:           /* Play a track.  This currently ignores index. */
1264                 {
1265                         struct cdrom_ti ti;
1266                         int track_idx;
1267 
1268                         sony_get_toc();
1269                         if (!sony_toc_read)
1270                                 return -EIO;
1271                         verify_area(VERIFY_READ, (char *)arg, sizeof ti);
1272 
1273                         memcpy_fromfs(&ti, (char *)arg, sizeof ti);
1274                         if ((ti.cdti_trk0 < sony_toc->first_track_num)
1275                                 || (sony_toc->last_track_num < ti.cdti_trk0)
1276                                 || (ti.cdti_trk1 < ti.cdti_trk0)) {
1277                                 return -EINVAL;
1278                         }
1279                         track_idx = find_track(int_to_bcd(ti.cdti_trk0));
1280                         if (track_idx < 0)
1281                                 return -EINVAL;
1282                         params[1] = sony_toc->tracks[track_idx].track_start_msf[0];
1283                         params[2] = sony_toc->tracks[track_idx].track_start_msf[1];
1284                         params[3] = sony_toc->tracks[track_idx].track_start_msf[2];
1285                         /*
1286                          * If we want to stop after the last track, use the lead-out
1287                          * MSF to do that.
1288                          */
1289                         if (bcd_to_int(sony_toc->last_track_num) <= ti.cdti_trk1) {
1290                                 log_to_msf(msf_to_log(sony_toc->lead_out_start_msf) - 1,
1291                                                    &(params[4]));
1292                         } else {
1293                                 track_idx = find_track(int_to_bcd(ti.cdti_trk1 + 1));
1294                                 if (track_idx < 0)
1295                                         return -EINVAL;
1296                                 log_to_msf(msf_to_log(sony_toc->tracks[track_idx].track_start_msf) - 1,
1297                                                    &(params[4]));
1298                         }
1299                         params[0] = 0x03;
1300 
1301                         spin_up_drive(status);
1302 
1303                         set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1304 
1305                         /* Start the drive at the saved position. */
1306                         cmd_buff[0] = SONY535_PLAY_AUDIO;
1307                         cmd_buff[1] = 0;        /* play back starting at this address */
1308                         cmd_buff[2] = params[1];
1309                         cmd_buff[3] = params[2];
1310                         cmd_buff[4] = params[3];
1311                         cmd_buff[5] = SONY535_PLAY_AUDIO;
1312                         cmd_buff[6] = 2;        /* set ending address */
1313                         cmd_buff[7] = params[4];
1314                         cmd_buff[8] = params[5];
1315                         cmd_buff[9] = params[6];
1316                         if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1317                                 (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1318                                 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYTRKIND)\n",
1319                                                 status[0]);
1320                                 printk("... Params: %x %x %x %x %x %x %x\n",
1321                                                 params[0], params[1], params[2],
1322                                                 params[3], params[4], params[5], params[6]);
1323                                 return -EIO;
1324                         }
1325                         /* Save the final position for pauses and resumes */
1326                         final_pos_msf[0] = params[4];
1327                         final_pos_msf[1] = params[5];
1328                         final_pos_msf[2] = params[6];
1329                         sony_audio_status = CDROM_AUDIO_PLAY;
1330                         return 0;
1331                 }
1332 
1333         case CDROMSUBCHNL:                      /* Get subchannel info */
1334                 return sony_get_subchnl_info(arg);
1335 
1336         case CDROMVOLCTRL:                      /* Volume control.  What volume does this change, anyway? */
1337                 {
1338                         struct cdrom_volctrl volctrl;
1339 
1340                         verify_area(VERIFY_READ, (char *)arg, sizeof volctrl);
1341 
1342                         memcpy_fromfs(&volctrl, (char *)arg, sizeof volctrl);
1343                         cmd_buff[0] = SONY535_SET_VOLUME;
1344                         cmd_buff[1] = volctrl.channel0;
1345                         cmd_buff[2] = volctrl.channel1;
1346                         if (do_sony_cmd(cmd_buff, 3, status, NULL, 0, 0) != 0) {
1347                                 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMVOLCTRL)\n",
1348                                                 status[0]);
1349                                 return -EIO;
1350                         }
1351                 }
1352                 return 0;
1353 
1354         case CDROMEJECT:                        /* Eject the drive */
1355                 cmd_buff[0] = SONY535_STOP;
1356                 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1357                 cmd_buff[0] = SONY535_SPIN_DOWN;
1358                 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1359 
1360                 sony_audio_status = CDROM_AUDIO_INVALID;
1361                 cmd_buff[0] = SONY535_EJECT_CADDY;
1362                 if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
1363                         printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMEJECT)\n",
1364                                         status[0]);
1365                         return -EIO;
1366                 }
1367                 return 0;
1368                 break;
1369 
1370         default:
1371                 return -EINVAL;
1372         }
1373 }
1374 
1375 
1376 /*
1377  * Open the drive for operations.  Spin the drive up and read the table of
1378  * contents if these have not already been done.
1379  */
1380 static int
1381 cdu_open(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1382                  struct file *filp)
1383 {
1384         Byte status[2], cmd_buff[2];
1385 
1386 
1387         if (sony_inuse)
1388                 return -EBUSY;
1389         if (check_drive_status() != 0)
1390                 return -EIO;
1391         sony_inuse = 1;
1392         MOD_INC_USE_COUNT;
1393 
1394         if (spin_up_drive(status) != 0) {
1395                 printk(CDU535_MESSAGE_NAME " error 0x%.2x (cdu_open, spin up)\n",
1396                                 status[0]);
1397                 sony_inuse = 0;
1398                 MOD_DEC_USE_COUNT;
1399                 return -EIO;
1400         }
1401         sony_get_toc();
1402         if (!sony_toc_read) {
1403                 cmd_buff[0] = SONY535_SPIN_DOWN;
1404                 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1405                 sony_inuse = 0;
1406                 MOD_DEC_USE_COUNT;
1407                 return -EIO;
1408         }
1409         if (inode) {
1410                 check_disk_change(inode->i_rdev);
1411         }
1412         sony_usage++;
1413 
1414 #ifdef LOCK_DOORS
1415         /* disable the eject button while mounted */
1416         cmd_buff[0] = SONY535_DISABLE_EJECT_BUTTON;
1417         do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1418 #endif
1419 
1420         return 0;
1421 }
1422 
1423 
1424 /*
1425  * Close the drive.  Spin it down if no task is using it.  The spin
1426  * down will fail if playing audio, so audio play is OK.
1427  */
1428 static void
1429 cdu_release(struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1430                         struct file *filp)
1431 {
1432         Byte status[2], cmd_no;
1433 
1434         sony_inuse = 0;
1435         MOD_DEC_USE_COUNT;
1436 
1437         if (0 < sony_usage) {
1438                 sony_usage--;
1439         }
1440         if (sony_usage == 0) {
1441                 sync_dev(inode->i_rdev);
1442                 check_drive_status();
1443 
1444                 if (sony_audio_status != CDROM_AUDIO_PLAY) {
1445                         cmd_no = SONY535_SPIN_DOWN;
1446                         do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
1447                 }
1448 #ifdef LOCK_DOORS
1449                 /* enable the eject button after umount */
1450                 cmd_no = SONY535_ENABLE_EJECT_BUTTON;
1451                 do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
1452 #endif
1453         }
1454 }
1455 
1456 
1457 static struct file_operations cdu_fops =
1458 {
1459         NULL,                                           /* lseek - default */
1460         block_read,                                     /* read - general block-dev read */
1461         block_write,                            /* write - general block-dev write */
1462         NULL,                                           /* readdir - bad */
1463         NULL,                                           /* select */
1464         cdu_ioctl,                                      /* ioctl */
1465         NULL,                                           /* mmap */
1466         cdu_open,                                       /* open */
1467         cdu_release,                            /* release */
1468         NULL,                                           /* fsync */
1469         NULL,                                           /* fasync */
1470         cdu535_check_media_change,      /* check media change */
1471         NULL                                            /* revalidate */
1472 };
1473 
1474 /*
1475  * Initialize the driver.
1476  */
1477 #ifndef MODULE
1478 unsigned long
1479 sony535_init(unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1480 #else
1481 int
1482 init_module(void)
1483 #endif
1484 {
1485         struct s535_sony_drive_config drive_config;
1486         Byte cmd_buff[3];
1487         Byte ret_buff[2];
1488         Byte status[2];
1489         int  retry_count;
1490         int  tmp_irq;
1491 #ifdef MODULE
1492         int  i;
1493 #endif
1494 
1495         /* Setting the base I/O address to 0xffff will disable it. */
1496         if (sony535_cd_base_io == 0xffff)
1497                 goto bail;
1498 
1499         /* Set up all the register locations */
1500         result_reg = sony535_cd_base_io;
1501         command_reg = sony535_cd_base_io;
1502         data_reg = sony535_cd_base_io + 1;
1503         read_status_reg = sony535_cd_base_io + 2;
1504         select_unit_reg = sony535_cd_base_io + 3;
1505 
1506 #ifndef USE_IRQ
1507         sony535_irq_used = 0;   /* polling only until this is ready... */
1508 #endif
1509         /* we need to poll until things get initialized */
1510         tmp_irq = sony535_irq_used;
1511         sony535_irq_used = 0;
1512 
1513 #if DEBUG > 0
1514         printk(CDU535_MESSAGE_NAME ": probing base address %03X\n",
1515                         sony535_cd_base_io);
1516 #endif
1517         if (check_region(sony535_cd_base_io,4)) {
1518                 printk(CDU535_MESSAGE_NAME ": my base address is not free!\n");
1519 #ifndef MODULE
1520                 return mem_start;
1521 #else
1522                 return -EIO;
1523 #endif
1524         }
1525         /* look for the CD-ROM, follows the procedure in the DOS driver */
1526         inb(select_unit_reg);
1527         retry_count = jiffies + 2 * HZ;
1528         while (jiffies < retry_count)
1529                 sony_sleep();                   /* wait for 40 18 Hz ticks (from DOS driver) */
1530         inb(result_reg);
1531 
1532         outb(0, read_status_reg);       /* does a reset? */
1533         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1534         while (jiffies < retry_count) {
1535                 select_unit(0);
1536                 if (inb(result_reg) != 0xff)
1537                         break;
1538                 sony_sleep();
1539         }
1540 
1541         if ((jiffies < retry_count) && (check_drive_status() != TIME_OUT)) {
1542                 /* CD-ROM drive responded --  get the drive configuration */
1543                 cmd_buff[0] = SONY535_INQUIRY;
1544                 if (do_sony_cmd(cmd_buff, 1, status,
1545                                                 (Byte *)&drive_config, 28, 1) == 0) {
1546                         /* was able to get the configuration,
1547                          * set drive mode as rest of init
1548                          */
1549 #if DEBUG > 0
1550                         /* 0x50 == CADDY_NOT_INSERTED | NOT_SPINNING */
1551                         if ( (status[0] & 0x7f) != 0 && (status[0] & 0x7f) != 0x50 )
1552                                 printk(CDU535_MESSAGE_NAME
1553                                                 "Inquiry command returned status = 0x%x\n", status[0]);
1554 #endif
1555                         /* now ready to use interrupts, if available */
1556                         sony535_irq_used = tmp_irq;
1557 #ifndef MODULE
1558 /* This code is not in MODULEs by default, since the autoirq stuff might
1559  * not be in the module-accessible symbol table.
1560  */
1561                         /* A negative sony535_irq_used will attempt an autoirq. */
1562                         if (sony535_irq_used < 0) {
1563                                 autoirq_setup(0);
1564                                 enable_interrupts();
1565                                 outb(0, read_status_reg);       /* does a reset? */
1566                                 sony535_irq_used = autoirq_report(10);
1567                                 disable_interrupts();
1568                         }
1569 #endif
1570                         if (sony535_irq_used > 0) {
1571                             if (request_irq(sony535_irq_used, cdu535_interrupt,
1572                                                                 SA_INTERRUPT, CDU535_HANDLE)) {
1573                                         printk("Unable to grab IRQ%d for the " CDU535_MESSAGE_NAME
1574                                                         " driver; polling instead.\n", sony535_irq_used);
1575                                         sony535_irq_used = 0;
1576                                 }
1577                         }
1578                         cmd_buff[0] = SONY535_SET_DRIVE_MODE;
1579                         cmd_buff[1] = 0x0;      /* default audio */
1580                         if (do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1) == 0) {
1581                                 /* set the drive mode successful, we are set! */
1582                                 sony_buffer_size = SONY535_BUFFER_SIZE;
1583                                 sony_buffer_sectors = sony_buffer_size / 2048;
1584 
1585                                 printk(CDU535_MESSAGE_NAME " I/F CDROM : %8.8s %16.16s %4.4s",
1586                                            drive_config.vendor_id,
1587                                            drive_config.product_id,
1588                                            drive_config.product_rev_level);
1589                                 printk("  base address %03X, ", sony535_cd_base_io);
1590                                 if (tmp_irq > 0)
1591                                         printk("IRQ%d, ", tmp_irq);
1592                                 printk("using %d byte buffer\n", sony_buffer_size);
1593 
1594                                 if (register_blkdev(MAJOR_NR, CDU535_HANDLE, &cdu_fops)) {
1595                                         printk("Unable to get major %d for %s\n",
1596                                                         MAJOR_NR, CDU535_MESSAGE_NAME);
1597 #ifndef MODULE
1598                                         return mem_start;
1599 #else
1600                                         return -EIO;
1601 #endif
1602                                 }
1603                                 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1604                                 read_ahead[MAJOR_NR] = 8;       /* 8 sector (4kB) read-ahead */
1605 
1606 #ifndef MODULE
1607                                 sony_toc = (struct s535_sony_toc *)mem_start;
1608                                 mem_start += sizeof *sony_toc;
1609                                 last_sony_subcode = (struct s535_sony_subcode *)mem_start;
1610                                 mem_start += sizeof *last_sony_subcode;
1611                                 sony_buffer = (Byte *)mem_start;
1612                                 mem_start += sony_buffer_size;
1613 
1614 #else /* MODULE */
1615                                 sony_toc = (struct s535_sony_toc *)
1616                                         kmalloc(sizeof *sony_toc, GFP_KERNEL);
1617                                 last_sony_subcode = (struct s535_sony_subcode *)
1618                                         kmalloc(sizeof *last_sony_subcode, GFP_KERNEL);
1619                                 sony_buffer = (Byte **)
1620                                         kmalloc(4 * sony_buffer_sectors, GFP_KERNEL);
1621                                 for (i = 0; i < sony_buffer_sectors; i++)
1622                                         sony_buffer[i] = (Byte *)kmalloc(2048, GFP_KERNEL);
1623 #endif /* MODULE */
1624                                 initialized = 1;
1625                         }
1626                 }
1627         }
1628 
1629         if (!initialized) {
1630                 printk("Did not find a " CDU535_MESSAGE_NAME " drive\n");
1631 #ifdef MODULE
1632                 return -EIO;
1633 #endif
1634         } else {
1635                 request_region(sony535_cd_base_io, 4, CDU535_HANDLE);
1636         }
1637 bail:
1638 #ifndef MODULE
1639         return mem_start;
1640 #else
1641         return 0;
1642 #endif
1643 }
1644 
1645 #ifndef MODULE
1646 /*
1647  * accept "kernel command line" parameters
1648  * (added by emoenke@gwdg.de)
1649  *
1650  * use: tell LILO:
1651  *                 sonycd535=0x320
1652  *
1653  * the address value has to be the existing CDROM port address.
1654  */
1655 void
1656 sonycd535_setup(char *strings, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
1657 {
1658         /* if IRQ change and default io base desired,
1659          * then call with io base of 0
1660          */
1661         if (ints[0] > 0)
1662                 if (ints[0] != 0)
1663                         sony535_cd_base_io = ints[1];
1664         if (ints[0] > 1)
1665                 sony535_irq_used = ints[2];
1666         if ((strings != NULL) && (*strings != '\0'))
1667                 printk(CDU535_MESSAGE_NAME
1668                                 ": Warning: Unknown interface type: %s\n", strings);
1669 }
1670 
1671 #else /* MODULE */
1672 
1673 void
1674 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1675 {
1676         int i;
1677         if (MOD_IN_USE) {
1678                 printk(CDU535_HANDLE " module in use, cannot remove\n");
1679                 return;
1680         }
1681         release_region(sony535_cd_base_io, 4);
1682         for (i = 0; i < sony_buffer_sectors; i++)
1683                 kfree_s(sony_buffer[i], 2048);
1684         kfree_s(sony_buffer, 4 * sony_buffer_sectors);
1685         kfree_s(last_sony_subcode, sizeof *last_sony_subcode);
1686         kfree_s(sony_toc, sizeof *sony_toc);
1687         if (unregister_blkdev(MAJOR_NR, CDU535_HANDLE) == -EINVAL)
1688                 printk("Uh oh, couldn't unregister " CDU535_HANDLE "\n");
1689         else
1690                 printk(CDU535_HANDLE " module released\n");
1691 }
1692 #endif  /* MODULE */
1693 
1694 #endif  /* CONFIG_CDU535 */

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