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

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