root/drivers/block/sbpcd.c

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

DEFINITIONS

This source file includes following definitions.
  1. sbpcd_dprintf
  2. sbpcd_dbg_ioctl
  3. sbp_sleep
  4. lba2msf
  5. bin2bcdx
  6. blk2msf
  7. make16
  8. make32
  9. swap_nibbles
  10. byt2bcd
  11. bcd2bin
  12. msf2blk
  13. msf2lba
  14. sta2err
  15. clr_cmdbuf
  16. mark_timeout
  17. flush_status
  18. CDi_stat_loop
  19. ResponseInfo
  20. EvaluateStatus
  21. ResponseStatus
  22. xx_ReadStatus
  23. xx_ReadError
  24. cmd_out
  25. xx_Seek
  26. xx_SpinUp
  27. yy_SpinDown
  28. yy_SetSpeed
  29. xx_SetVolume
  30. GetStatus
  31. xy_DriveReset
  32. SetSpeed
  33. DriveReset
  34. xx_Pause_Resume
  35. yy_LockDoor
  36. xx_ReadSubQ
  37. xx_ModeSense
  38. xx_ModeSelect
  39. xx_TellVolume
  40. xx_ReadCapacity
  41. xx_ReadTocDescr
  42. xx_ReadTocEntry
  43. xx_ReadPacket
  44. convert_UPC
  45. xx_ReadUPC
  46. yy_CheckMultiSession
  47. yy_SubChanInfo
  48. check_datarate
  49. check_version
  50. switch_drive
  51. check_drives
  52. timewait
  53. obey_audio_state
  54. check_allowed1
  55. check_allowed2
  56. check_allowed3
  57. seek_pos_audio_end
  58. ReadToC
  59. DiskInfo
  60. prepare
  61. xx_PlayAudioMSF
  62. sbp_status
  63. sbpcd_ioctl
  64. sbp_transfer
  65. DO_SBPCD_REQUEST
  66. sbp_read_cmd
  67. sbp_data
  68. sbpcd_open
  69. sbpcd_release
  70. SBPCD_SETUP
  71. config_spea
  72. SBPCD_INIT
  73. check_media_change

   1 /*
   2  *  sbpcd.c   CD-ROM device driver for the whole family of IDE-style
   3  *            Kotobuki/Matsushita/Panasonic CR-5xx drives for
   4  *            SoundBlaster ("Pro" or "16 ASP" or compatible) cards
   5  *            and for "no-sound" interfaces like Lasermate and the
   6  *            Panasonic CI-101P.
   7  *
   8  *  NOTE:     This is release 2.2.
   9  *            It works with my SbPro & drive CR-521 V2.11 from 2/92
  10  *            and with the new CR-562-B V0.75 on a "naked" Panasonic
  11  *            CI-101P interface. And vice versa. 
  12  *  
  13  *
  14  *  VERSION HISTORY
  15  *
  16  *  0.1  initial release, April/May 93, after mcd.c (Martin Harriss)
  17  *
  18  *  0.2  the "repeat:"-loop in do_sbpcd_request did not check for
  19  *       end-of-request_queue (resulting in kernel panic).
  20  *       Flow control seems stable, but throughput is not better.  
  21  *
  22  *  0.3  interrupt locking totally eliminated (maybe "inb" and "outb"
  23  *       are still locking) - 0.2 made keyboard-type-ahead losses.
  24  *       check_sbpcd_media_change added (to use by isofs/inode.c)
  25  *       - but it detects almost nothing.
  26  *
  27  *  0.4  use MAJOR 25 definitely.
  28  *       Almost total re-design to support double-speed drives and
  29  *       "naked" (no sound) interface cards.
  30  *       Flow control should be exact now (tell me if not).
  31  *       Don't occupy the SbPro IRQ line (not needed either); will
  32  *       live together with Hannu Savolainen's sndkit now.
  33  *       Speeded up data transfer to 150 kB/sec, with help from Kai
  34  *       Makisara, the "provider" of the "mt" tape utility.
  35  *       Give "SpinUp" command if necessary.
  36  *       First steps to support up to 4 drives (but currently only one).
  37  *       Implemented audio capabilities - workman should work, xcdplayer
  38  *       gives some problems.
  39  *       This version is still consuming too much CPU time, and
  40  *       sleeping still has to be worked on.
  41  *       During "long" implied seeks, it seems possible that a 
  42  *       ReadStatus command gets ignored. That gives the message
  43  *       "ResponseStatus timed out" (happens about 6 times here during
  44  *       a "ls -alR" of the YGGDRASIL LGX-Beta CD). Such a case is
  45  *       handled without data error, but it should get done better.
  46  *
  47  *  0.5  Free CPU during waits (again with help from Kai Makisara).
  48  *       Made it work together with the LILO/kernel setup standard.
  49  *       Included auto-probing code, as suggested by YGGDRASIL.
  50  *       Formal redesign to add DDI debugging.
  51  *       There are still flaws in IOCTL (workman with double speed drive).
  52  *
  53  *  1.0  Added support for all drive ids (0...3, no longer only 0)
  54  *       and up to 4 drives on one controller.
  55  *       Added "#define MANY_SESSION" for "old" multi session CDs.
  56  *
  57  *  1.1  Do SpinUp for new drives, too.
  58  *       Revised for clean compile under "old" kernels (pl9).
  59  *
  60  *  1.2  Found the "workman with double-speed drive" bug: use the driver's
  61  *       audio_state, not what the drive is reporting with ReadSubQ.
  62  *
  63  *  1.3  Minor cleanups.
  64  *       Refinements regarding Workman.
  65  *
  66  *  1.4  Read XA disks (PhotoCDs) with "old" drives, too (but possibly only
  67  *       the first session - I could not try a "multi-session" CD yet).
  68  *       This currently still is too slow (50 kB/sec) - but possibly
  69  *       the old drives won't do it faster.
  70  *       Implemented "door (un)lock" for new drives (still does not work
  71  *       as wanted - no lock possible after an unlock).
  72  *       Added some debugging printout for the UPC/EAN code - but my drives 
  73  *       return only zeroes. Is there no UPC/EAN code written?
  74  *
  75  *  1.5  Laborate with UPC/EAN code (not better yet).
  76  *       Adapt to kernel 1.1.8 change (have to explicitely include
  77  *       <linux/string.h> now).
  78  *
  79  *  1.6  Trying to read audio frames as data. Impossible with the current
  80  *       drive firmware levels, as it seems. Awaiting any hint. ;-)
  81  *       Changed "door unlock": repeat it until success.
  82  *       Changed CDROMSTOP routine (stop somewhat "softer" so that Workman
  83  *       won't get confused).
  84  *       Added a third interface type: Sequoia S-1000, as used with the SPEA
  85  *       Media FX sound card. This interface (useable for Sony and Mitsumi 
  86  *       drives, too) needs a special configuration setup and behaves like a 
  87  *       LaserMate type after that. Still experimental - I do not have such
  88  *       an interface.
  89  *       Use the "variable BLOCK_SIZE" feature (2048). But it does only work
  90  *       if you give the mount option "block=2048".
  91  *       The media_check routine is currently disabled; now that it gets
  92  *       called as it should I fear it must get synchronized for not to
  93  *       disturb the normal driver's activity.
  94  *
  95  *  2.0  Version number bumped - two reasons:
  96  *       - reading audio tracks as data works now with CR-562 and CR-563. We
  97  *       currently do it by an IOCTL (yet has to get standardized), one frame
  98  *       at a time; that is pretty slow. But it works.
  99  *       - we are maintaining now up to 4 interfaces (each up to 4 drives):
 100  *       did it the easy way - a different MAJOR (25, 26, ...) and a different
 101  *       copy of the driver (sbpcd.c, sbpcd2.c, sbpcd3.c, sbpcd4.c - only
 102  *       distinguished by the value of SBPCD_ISSUE and the driver's name),
 103  *       and a common sbpcd.h file.
 104  *       Bettered the "ReadCapacity error" problem with old CR-52x drives (the
 105  *       drives sometimes need a manual "eject/insert" before work): just
 106  *       reset the drive and do again. Needs lots of resets here and sometimes
 107  *       that does not cure, so this can't be the solution.
 108  *
 109  *  2.1  Found bug with multisession CDs (accessing frame 16).
 110  *       "read audio" works now with address type CDROM_MSF, too.
 111  *       Bigger audio frame buffer: allows reading max. 4 frames at time; but
 112  *       reading more than one frame at once gives poor quality.
 113  *
 114  *  2.2  
 115  *
 116  *  TODO
 117  *
 118  *     disk change detection
 119  *     allow & synchronize multi-activity
 120  *        (data + audio + ioctl + disk change, multiple drives)
 121  *     implement multi-controller-support with a single driver
 122  *     implement "read all subchannel data" (96 bytes per frame)
 123  *
 124  *
 125  *     special thanks to Kai Makisara (kai.makisara@vtt.fi) for his fine
 126  *     elaborated speed-up experiments (and the fabulous results!), for
 127  *     the "push" towards load-free wait loops, and for the extensive mail
 128  *     thread which brought additional hints and bug fixes.
 129  * 
 130  *
 131  *   Copyright (C) 1993, 1994  Eberhard Moenkeberg <emoenke@gwdg.de>
 132  *                         or <eberhard_moenkeberg@rollo.central.de>
 133  *
 134  *                  The FTP-home of this driver is 
 135  *                  ftp.gwdg.de:/pub/linux/cdrom/drivers/sbpcd/.
 136  *
 137  *                  If you change this software, you should mail a .diff
 138  *                  file with some description lines to emoenke@gwdg.de.
 139  *                  I want to know about it.
 140  *
 141  *                  If you are the editor of a Linux CD, you should
 142  *                  enable sbpcd.c within your boot floppy kernel and
 143  *                  send me one of your CDs for free.
 144  *
 145  *   This program is free software; you can redistribute it and/or modify
 146  *   it under the terms of the GNU General Public License as published by
 147  *   the Free Software Foundation; either version 2, or (at your option)
 148  *   any later version.
 149  *
 150  *   You should have received a copy of the GNU General Public License
 151  *   (for example /usr/src/linux/COPYING); if not, write to the Free
 152  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 153  *
 154  */
 155 
 156 #define SBPCD_ISSUE 1 /* change to 2, 3, 4 for multiple interface boards */
 157 
 158 #include <linux/config.h>
 159 #include <linux/errno.h>
 160 
 161 #include <linux/sched.h>
 162 /* #undef DS */
 163 #include <linux/timer.h>
 164 #include <linux/fs.h>
 165 #include <linux/kernel.h>
 166 #include <linux/cdrom.h>
 167 #include <linux/ioport.h>
 168 #include <linux/major.h> 
 169 #include <linux/sbpcd.h>
 170 #include <linux/string.h>
 171 #include <asm/system.h>
 172 #include <asm/io.h>
 173 #include <asm/segment.h>
 174 #include <stdarg.h>
 175 
 176 #if !(SBPCD_ISSUE-1)
 177 #define MAJOR_NR MATSUSHITA_CDROM_MAJOR
 178 #endif
 179 #if !(SBPCD_ISSUE-2)
 180 #define MAJOR_NR MATSUSHITA_CDROM2_MAJOR /* second driver issue */
 181 #endif
 182 #if !(SBPCD_ISSUE-3)
 183 #define MAJOR_NR MATSUSHITA_CDROM3_MAJOR /* third driver issue */
 184 #endif
 185 #if !(SBPCD_ISSUE-4)
 186 #define MAJOR_NR MATSUSHITA_CDROM4_MAJOR /* fourth driver issue */
 187 #endif
 188 
 189 #include "blk.h"
 190 
 191 #define VERSION "2.2 Eberhard Moenkeberg <emoenke@gwdg.de>"
 192 
 193 #define SBPCD_DEBUG
 194 
 195 #ifndef CONFIG_ISO9660_FS
 196 #error "SBPCD: \"make config\" again. File system iso9660 is necessary."
 197 #endif
 198 
 199 /*
 200  * This may come back some day..
 201  */
 202 #define DDIOCSDBG       0x9000
 203 
 204 /*
 205  * still testing around...
 206  */
 207 #define LONG_TIMING 0 /* test against timeouts with "gold" CDs on CR-521 */
 208 #define MANY_SESSION 0 /* this will conflict with "true" multi-session! */
 209 #undef  FUTURE
 210 #define WORKMAN 1 /* some testing stuff to make it better */
 211 #define CDMKE /* makes timing independent of processor speed */
 212 
 213 #undef XA_TEST1
 214 #define XA_TEST2
 215 
 216 #define TEST_UPC 0
 217 #define SPEA_TEST 0
 218 #define PRINTK_BUG 0
 219 #define TEST_STI 0
 220 /*==========================================================================*/
 221 /*
 222  * provisions for more than 1 driver issues
 223  * currently up to 4 drivers, expandable
 224  */
 225 #if !(SBPCD_ISSUE-1)
 226 #define DO_SBPCD_REQUEST(a) do_sbpcd_request(a)
 227 #define SBPCD_SETUP(a,b) sbpcd_setup(a,b)
 228 #define SBPCD_INIT(a,b) sbpcd_init(a,b)
 229 #endif
 230 #if !(SBPCD_ISSUE-2)
 231 #define DO_SBPCD_REQUEST(a) do_sbpcd2_request(a)
 232 #define SBPCD_SETUP(a,b) sbpcd2_setup(a,b)
 233 #define SBPCD_INIT(a,b) sbpcd2_init(a,b)
 234 #endif
 235 #if !(SBPCD_ISSUE-3)
 236 #define DO_SBPCD_REQUEST(a) do_sbpcd3_request(a)
 237 #define SBPCD_SETUP(a,b) sbpcd3_setup(a,b)
 238 #define SBPCD_INIT(a,b) sbpcd3_init(a,b)
 239 #endif
 240 #if !(SBPCD_ISSUE-4)
 241 #define DO_SBPCD_REQUEST(a) do_sbpcd4_request(a)
 242 #define SBPCD_SETUP(a,b) sbpcd4_setup(a,b)
 243 #define SBPCD_INIT(a,b) sbpcd4_init(a,b)
 244 #endif
 245 /*==========================================================================*/
 246 #if MANY_SESSION
 247 #undef LONG_TIMING
 248 #define LONG_TIMING 1
 249 #endif
 250 /*==========================================================================*/
 251 #if SBPCD_DIS_IRQ
 252 #define SBPCD_CLI cli()
 253 #define SBPCD_STI sti()
 254 #else
 255 #define SBPCD_CLI
 256 #define SBPCD_STI
 257 #endif SBPCD_DIS_IRQ
 258 /*==========================================================================*/
 259 /*
 260  * auto-probing address list
 261  * inspired by Adam J. Richter from Yggdrasil
 262  *
 263  * still not good enough - can cause a hang.
 264  *   example: a NE 2000 ethernet card at 300 will cause a hang probing 310.
 265  * if that happens, reboot and use the LILO (kernel) command line.
 266  * The possibly conflicting ethernet card addresses get NOT probed 
 267  * by default - to minimize the hang possibilities. 
 268  *
 269  * The SB Pro addresses get "mirrored" at 0x6xx - to avoid a type error,
 270  * the 0x2xx-addresses must get checked before 0x6xx.
 271  *
 272  * send mail to emoenke@gwdg.de if your interface card is not FULLY
 273  * represented here.
 274  */
 275 static int autoprobe[] = 
 276 {
 277   CDROM_PORT, SBPRO, /* probe with user's setup first */
 278   0x230, 1, /* Soundblaster Pro and 16 (default) */
 279   0x300, 0, /* CI-101P (default), WDH-7001C (default),
 280                Galaxy (default), Reveal (one default) */
 281   0x250, 1, /* OmniCD default, Soundblaster Pro and 16 */
 282   0x260, 1, /* OmniCD */
 283   0x320, 0, /* Lasermate, CI-101P, WDH-7001C, Galaxy, Reveal (other default) */
 284   0x340, 0, /* Lasermate, CI-101P */
 285   0x360, 0, /* Lasermate, CI-101P */
 286   0x270, 1, /* Soundblaster 16 */
 287   0x670, 0, /* "sound card #9" */
 288   0x690, 0, /* "sound card #9" */
 289   0x330, 2, /* SPEA Media FX (default) */
 290   0x320, 2, /* SPEA Media FX */
 291   0x340, 2, /* SPEA Media FX */
 292   0x350, 2, /* SPEA Media FX */
 293 #if 0
 294 /* some "hazardous" locations (ethernet cards) */
 295   0x330, 0, /* Lasermate, CI-101P, WDH-7001C */
 296   0x350, 0, /* Lasermate, CI-101P */
 297   0x370, 0, /* Lasermate, CI-101P */
 298   0x290, 1, /* Soundblaster 16 */
 299   0x310, 0, /* Lasermate, CI-101P, WDH-7001C */
 300 /* excluded due to incomplete address decoding of the SbPro card */
 301   0x630, 0, /* "sound card #9" (default) */
 302   0x650, 0, /* "sound card #9" */
 303 #endif
 304 };
 305 
 306 #define NUM_AUTOPROBE  (sizeof(autoprobe) / sizeof(int))
 307 
 308 
 309 /*==========================================================================*/
 310 /*
 311  * the external references:
 312  */
 313 #if !(SBPCD_ISSUE-1)
 314 #ifdef CONFIG_SBPCD2
 315 extern unsigned long sbpcd2_init(unsigned long, unsigned long);
 316 #endif
 317 #ifdef CONFIG_SBPCD3
 318 extern unsigned long sbpcd3_init(unsigned long, unsigned long);
 319 #endif
 320 #ifdef CONFIG_SBPCD4
 321 extern unsigned long sbpcd4_init(unsigned long, unsigned long);
 322 #endif
 323 #endif
 324 
 325 /*==========================================================================*/
 326 /*==========================================================================*/
 327 /*
 328  * the forward references:
 329  */
 330 static void sbp_read_cmd(void);
 331 static int sbp_data(void);
 332 static int cmd_out(void);
 333 static int DiskInfo(void);
 334 static int check_media_change(dev_t);
 335 
 336 /*==========================================================================*/
 337 
 338 /*
 339  * pattern for printk selection:
 340  *
 341  * (1<<DBG_INF)  necessary information
 342  * (1<<DBG_BSZ)  BLOCK_SIZE trace
 343  * (1<<DBG_REA)  "read" status trace
 344  * (1<<DBG_CHK)  "media check" trace
 345  * (1<<DBG_TIM)  datarate timer test
 346  * (1<<DBG_INI)  initialization trace
 347  * (1<<DBG_TOC)  tell TocEntry values
 348  * (1<<DBG_IOC)  ioctl trace
 349  * (1<<DBG_STA)  "ResponseStatus" trace
 350  * (1<<DBG_ERR)  "xx_ReadError" trace
 351  * (1<<DBG_CMD)  "cmd_out" trace
 352  * (1<<DBG_WRN)  give explanation before auto-probing
 353  * (1<<DBG_MUL)  multi session code test
 354  * (1<<DBG_ID)   "drive_id != 0" test code
 355  * (1<<DBG_IOX)  some special information
 356  * (1<<DBG_DID)  drive ID test
 357  * (1<<DBG_RES)  drive reset info
 358  * (1<<DBG_SPI)  SpinUp test info
 359  * (1<<DBG_IOS)  ioctl trace: "subchannel"
 360  * (1<<DBG_IO2)  ioctl trace: general
 361  * (1<<DBG_UPC)  show UPC info
 362  * (1<<DBG_XA)   XA mode debugging
 363  * (1<<DBG_LCK)  door (un)lock info
 364  * (1<<DBG_SQ)   dump SubQ frame
 365  * (1<<DBG_AUD)  "read audio" debugging
 366  * (1<<DBG_SEQ)  Sequoia interface configuration trace
 367  * (1<<DBG_000)  unnecessary information
 368  */
 369 #if 1
 370 static int sbpcd_debug =  (1<<DBG_INF) | (1<<DBG_WRN);
 371 #else
 372 #if SPEA_TEST
 373 static int sbpcd_debug =  (1<<DBG_INF) |
 374                           (1<<DBG_INI) |
 375                           (1<<DBG_ID)  |
 376                           (1<<DBG_SEQ);
 377 #else
 378 static int sbpcd_debug =  (1<<DBG_INF) |
 379                           (1<<DBG_TOC) |
 380                           (1<<DBG_UPC);
 381 #endif
 382 #endif
 383 static int sbpcd_ioaddr = CDROM_PORT;   /* default I/O base address */
 384 static int sbpro_type = SBPRO;
 385 static int CDo_command, CDo_reset;
 386 static int CDo_sel_d_i, CDo_enable;
 387 static int CDi_info, CDi_status, CDi_data;
 388 static int MIXER_addr, MIXER_data;
 389 static struct cdrom_msf msf;
 390 static struct cdrom_ti ti;
 391 static struct cdrom_tochdr tochdr;
 392 static struct cdrom_tocentry tocentry;
 393 static struct cdrom_subchnl SC;
 394 static struct cdrom_volctrl volctrl;
 395 static struct cdrom_read_audio read_audio;
 396 static char *str_sb = "SoundBlaster";
 397 static char *str_lm = "LaserMate";
 398 static char *str_sp = "SPEA";
 399 char *type;
 400 
 401 /*==========================================================================*/
 402 
 403 #if FUTURE
 404 static struct wait_queue *sbp_waitq = NULL;
 405 #endif FUTURE
 406 
 407 /*==========================================================================*/
 408 
 409 #define SBP_BUFFER_FRAMES 4 /* driver's own read_ahead, data mode */
 410 #define SBP_BUFFER_AUDIO_FRAMES 4 /* driver's own read_ahead, read audio mode */
 411 
 412 /*==========================================================================*/
 413 
 414 static u_char drive_family[]="CR-5";
 415 static u_char drive_vendor[]="MATSHITA";
 416 
 417 static u_int response_count=0;
 418 static u_int flags_cmd_out;
 419 static u_char cmd_type=0;
 420 static u_char drvcmd[7];
 421 static u_char infobuf[20];
 422 static u_char xa_head_buf[CD_XA_HEAD];
 423 static u_char xa_tail_buf[CD_XA_TAIL];
 424 
 425 static u_char busy_data=0, busy_audio=0; /* true semaphores would be safer */
 426 static u_char timed_out=0;
 427 static u_int datarate= 1000000;
 428 static u_int maxtim16=16000000;
 429 static u_int maxtim04= 4000000;
 430 static u_int maxtim02= 2000000;
 431 static u_int maxtim_8=   30000;
 432 #if LONG_TIMING
 433 static u_int maxtim_data= 9000;
 434 #else
 435 static u_int maxtim_data= 3000;
 436 #endif LONG_TIMING
 437 
 438 /*==========================================================================*/
 439 
 440 static int ndrives=0;
 441 static u_char drv_pattern[4]={ 0x80, 0x80, 0x80, 0x80 }; /* auto speed */
 442 /*  /X:... drv_pattern[0] |= (sax_n1|sax_n2);         */
 443 /*  /A:... for (i=0;i<4;i++) drv_pattern[i] |= sax_a; */
 444 /*  /N:... ndrives=i-'0';                             */
 445 
 446 static int sbpcd_blocksizes[NR_SBPCD] = {0, };
 447 
 448 /*==========================================================================*/
 449 /*
 450  * drive space begins here (needed separate for each unit) 
 451  */
 452 static int d=0; /* DriveStruct index: drive number */
 453 
 454 static struct {
 455   char drv_minor; /* minor number or -1 */
 456 
 457   char drive_model[4];
 458   char firmware_version[4];
 459   u_char *sbp_buf; /* Pointer to internal data buffer,
 460                            space allocated during sbpcd_init() */
 461   int sbp_first_frame;  /* First frame in buffer */
 462   int sbp_last_frame;   /* Last frame in buffer  */
 463   int sbp_read_frames;   /* Number of frames being read to buffer */
 464   int sbp_current;       /* Frame being currently read */
 465 
 466   u_char mode;           /* read_mode: READ_M1, READ_M2, READ_SC, READ_AU */
 467   u_char *aud_buf;                  /* Pointer to audio data buffer,
 468                                  space allocated during sbpcd_init() */
 469   u_char drv_type;
 470   u_char drv_options;
 471   u_char status_byte;
 472   u_char diskstate_flags;
 473   u_char sense_byte;
 474   
 475   u_char CD_changed;
 476   u_char open_count;
 477   u_char error_byte;
 478   
 479   u_char f_multisession;
 480   u_int lba_multi;
 481   
 482   u_char audio_state;
 483   u_int pos_audio_start;
 484   u_int pos_audio_end;
 485   char vol_chan0;
 486   u_char vol_ctrl0;
 487   char vol_chan1;
 488   u_char vol_ctrl1;
 489 #if 000
 490   char vol_chan2;
 491   u_char vol_ctrl2;
 492   char vol_chan3;
 493   u_char vol_ctrl3;
 494 #endif 000
 495   
 496   u_char SubQ_ctl_adr;
 497   u_char SubQ_trk;
 498   u_char SubQ_pnt_idx;
 499   u_int SubQ_run_tot;
 500   u_int SubQ_run_trk;
 501   u_char SubQ_whatisthis;
 502   
 503   u_char UPC_ctl_adr;
 504   u_char UPC_buf[7];
 505   
 506   int CDsize_blk;
 507   int frame_size;
 508   int CDsize_frm;
 509   
 510   u_char xa_byte; /* 0x20: XA capabilities */
 511   u_char n_first_track; /* binary */
 512   u_char n_last_track; /* binary (not bcd), 0x01...0x63 */
 513   u_int size_msf; /* time of whole CD, position of LeadOut track */
 514   u_int size_blk;
 515   
 516   u_char TocEnt_nixbyte; /* em */
 517   u_char TocEnt_ctl_adr;
 518   u_char TocEnt_number;
 519   u_char TocEnt_format; /* em */
 520   u_int TocEnt_address;
 521   u_char ored_ctl_adr; /* to detect if CDROM contains data tracks */
 522   
 523   struct {
 524     u_char nixbyte; /* em */
 525     u_char ctl_adr; /* 0x4x: data, 0x0x: audio */
 526     u_char number;
 527     u_char format; /* em */ /* 0x00: lba, 0x01: msf */
 528     u_int address;
 529   } TocBuffer[MAX_TRACKS+1]; /* last entry faked */ 
 530   
 531   int in_SpinUp;
 532   
 533 } DriveStruct[NR_SBPCD];
 534 
 535 /*
 536  * drive space ends here (needed separate for each unit)
 537  */
 538 /*==========================================================================*/
 539 /*==========================================================================*/
 540 /*
 541  * DDI interface definitions
 542  */
 543 #ifdef SBPCD_DEBUG
 544 # define DPRINTF(x)     sbpcd_dprintf x
 545 
 546 static void sbpcd_dprintf(int level, char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 547 {
 548   char buff[256];
 549   va_list args;
 550   extern int vsprintf(char *buf, const char *fmt, va_list args);
 551 
 552   if (! (sbpcd_debug & (1 << level))) return;
 553 
 554   va_start(args, fmt);
 555   vsprintf(buff, fmt, args);
 556   va_end(args);
 557   printk(buff);
 558 #if PRINTK_BUG
 559   sti(); /* to avoid possible "printk" bug */
 560 #endif
 561 }
 562 
 563 #else
 564 # define DPRINTF(x)     /* nothing */
 565 
 566 #endif SBPCD_DEBUG
 567 
 568 /*
 569  * maintain trace bit pattern
 570  */
 571 static int sbpcd_dbg_ioctl(unsigned long arg, int level)
     /* [previous][next][first][last][top][bottom][index][help] */
 572 {
 573   int val;
 574 
 575   val = get_fs_long((int *) arg);
 576   switch(val)
 577     {
 578     case 0:     /* OFF */
 579       sbpcd_debug = 0;
 580       break;
 581 
 582     default:
 583       if (val >= 128) sbpcd_debug &= ~(1 << (val - 128));
 584       else sbpcd_debug |= (1 << val);
 585     }
 586   return(0);
 587 }
 588 
 589 
 590 /*==========================================================================*/
 591 /*==========================================================================*/
 592 /*
 593  * Wait a little while (used for polling the drive).  If in initialization,
 594  * setting a timeout doesn't work, so just loop for a while.
 595  */
 596 static inline void sbp_sleep(u_int jifs)
     /* [previous][next][first][last][top][bottom][index][help] */
 597 {
 598    current->state = TASK_INTERRUPTIBLE;
 599    current->timeout = jiffies + jifs;
 600    schedule();
 601 }
 602 
 603 /*==========================================================================*/
 604 /*==========================================================================*/
 605 /*
 606  *  convert logical_block_address to m-s-f_number (3 bytes only)
 607  */
 608 static void lba2msf(int lba, u_char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
 609 {
 610   lba += CD_BLOCK_OFFSET;
 611   msf[0] = lba / (CD_SECS*CD_FRAMES);
 612   lba %= CD_SECS*CD_FRAMES;
 613   msf[1] = lba / CD_FRAMES;
 614   msf[2] = lba % CD_FRAMES;
 615 }
 616 /*==========================================================================*/
 617 /*==========================================================================*/
 618 /*
 619  *  convert msf-bin to msf-bcd
 620  */
 621 static void bin2bcdx(u_char *p)  /* must work only up to 75 or 99 */
     /* [previous][next][first][last][top][bottom][index][help] */
 622 {
 623   *p=((*p/10)<<4)|(*p%10);
 624 }
 625 /*==========================================================================*/
 626 static u_int blk2msf(u_int blk)
     /* [previous][next][first][last][top][bottom][index][help] */
 627 {
 628   MSF msf;
 629   u_int mm;
 630 
 631   msf.c[3] = 0;
 632   msf.c[2] = (blk + CD_BLOCK_OFFSET) / (CD_SECS * CD_FRAMES);
 633   mm = (blk + CD_BLOCK_OFFSET) % (CD_SECS * CD_FRAMES);
 634   msf.c[1] = mm / CD_FRAMES;
 635   msf.c[0] = mm % CD_FRAMES;
 636   return (msf.n);
 637 }
 638 /*==========================================================================*/
 639 static u_int make16(u_char rh, u_char rl)
     /* [previous][next][first][last][top][bottom][index][help] */
 640 {
 641   return ((rh<<8)|rl);
 642 }
 643 /*==========================================================================*/
 644 static u_int make32(u_int rh, u_int rl)
     /* [previous][next][first][last][top][bottom][index][help] */
 645 {
 646   return ((rh<<16)|rl);
 647 }
 648 /*==========================================================================*/
 649 static u_char swap_nibbles(u_char i)
     /* [previous][next][first][last][top][bottom][index][help] */
 650 {
 651   return ((i<<4)|(i>>4));
 652 }
 653 /*==========================================================================*/
 654 static u_char byt2bcd(u_char i)
     /* [previous][next][first][last][top][bottom][index][help] */
 655 {
 656   return (((i/10)<<4)+i%10);
 657 }
 658 /*==========================================================================*/
 659 static u_char bcd2bin(u_char bcd)
     /* [previous][next][first][last][top][bottom][index][help] */
 660 {
 661   return ((bcd>>4)*10+(bcd&0x0F));
 662 }
 663 /*==========================================================================*/
 664 static int msf2blk(int msfx)
     /* [previous][next][first][last][top][bottom][index][help] */
 665 {
 666   MSF msf;
 667   int i;
 668 
 669   msf.n=msfx;
 670   i=(msf.c[2] * CD_SECS + msf.c[1]) * CD_FRAMES + msf.c[0] - CD_BLOCK_OFFSET;
 671   if (i<0) return (0);
 672   return (i);
 673 }
 674 /*==========================================================================*/
 675 /*
 676  *  convert m-s-f_number (3 bytes only) to logical_block_address 
 677  */
 678 static int msf2lba(u_char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
 679 {
 680   int i;
 681 
 682   i=(msf[0] * CD_SECS + msf[1]) * CD_FRAMES + msf[2] - CD_BLOCK_OFFSET;
 683   if (i<0) return (0);
 684   return (i);
 685 }
 686 /*==========================================================================*/
 687 /* evaluate xx_ReadError code (still mysterious) */ 
 688 static int sta2err(int sta)
     /* [previous][next][first][last][top][bottom][index][help] */
 689 {
 690   if (sta<=2) return (sta);
 691   if (sta==0x05) return (-4); /* CRC error */
 692   if (sta==0x06) return (-6); /* seek error */
 693   if (sta==0x0d) return (-6); /* seek error */
 694   if (sta==0x0e) return (-3); /* unknown command */
 695   if (sta==0x14) return (-3); /* unknown command */
 696   if (sta==0x0c) return (-11); /* read fault */
 697   if (sta==0x0f) return (-11); /* read fault */
 698   if (sta==0x10) return (-11); /* read fault */
 699   if (sta>=0x16) return (-12); /* general failure */
 700   DriveStruct[d].CD_changed=0xFF;
 701   if (sta==0x11) return (-15); /* invalid disk change */
 702   return (-2); /* drive not ready */
 703 }
 704 /*==========================================================================*/
 705 static void clr_cmdbuf(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 706 {
 707   int i;
 708 
 709   for (i=0;i<7;i++) drvcmd[i]=0;
 710   cmd_type=0;
 711 }
 712 /*==========================================================================*/
 713 static void mark_timeout(unsigned long i)
     /* [previous][next][first][last][top][bottom][index][help] */
 714 {
 715   timed_out=1;
 716   DPRINTF((DBG_TIM,"SBPCD: timer expired.\n"));
 717 }
 718 /*==========================================================================*/
 719 static struct timer_list delay_timer = { NULL, NULL, 0, 0, mark_timeout};
 720 #if 0
 721 static struct timer_list data_timer = { NULL, NULL, 0, 0, mark_timeout};
 722 static struct timer_list audio_timer = { NULL, NULL, 0, 0, mark_timeout};
 723 #endif
 724 /*==========================================================================*/
 725 static void flush_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 726 {
 727 #ifdef CDMKE
 728   int i;
 729 
 730   if (current == task[0])
 731     for (i=maxtim02;i!=0;i--) inb(CDi_status);
 732   else 
 733     {
 734       sbp_sleep(150);
 735       for (i=maxtim_data;i!=0;i--) inb(CDi_status);
 736     }
 737 #else
 738   timed_out=0;
 739 #if 0
 740   del_timer(&delay_timer);
 741 #endif
 742   delay_timer.expires = 150;
 743   add_timer(&delay_timer);
 744   do { }
 745   while (!timed_out);
 746 #if 0
 747   del_timer(&delay_timer);
 748 #endif 0
 749   inb(CDi_status);
 750 #endif CDMKE
 751 }
 752 /*==========================================================================*/
 753 static int CDi_stat_loop(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 754 {
 755   int i,j;
 756   u_long timeout;
 757   
 758   if (current == task[0])
 759     for(i=maxtim16;i!=0;i--)
 760       {
 761         j=inb(CDi_status);
 762         if (!(j&s_not_data_ready)) return (j);
 763         if (!(j&s_not_result_ready)) return (j);
 764         if (!new_drive) if (j&s_attention) return (j);
 765       }
 766   else
 767     for(timeout = jiffies + 1000, i=maxtim_data; timeout > jiffies; )
 768       {
 769         for ( ;i!=0;i--)
 770           {
 771             j=inb(CDi_status);
 772             if (!(j&s_not_data_ready)) return (j);
 773             if (!(j&s_not_result_ready)) return (j);
 774             if (!new_drive) if (j&s_attention) return (j);
 775           }
 776         sbp_sleep(1);
 777         i = 1;
 778       }
 779   return (-1);
 780 }
 781 /*==========================================================================*/
 782 static int ResponseInfo(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 783 {
 784   int i,j, st=0;
 785   u_long timeout;
 786 
 787   DPRINTF((DBG_000,"SBPCD: ResponseInfo entered.\n"));
 788   if (current == task[0])
 789     for (i=0;i<response_count;i++)
 790       {
 791         for (j=maxtim_8;j!=0;j--)
 792           {
 793             st=inb(CDi_status);
 794             if (!(st&s_not_result_ready)) break;
 795           }
 796         if (j==0)
 797           {
 798             DPRINTF((DBG_SEQ,"SBPCD: ResponseInfo: not_result_ready (got %d of %d bytes).\n", i, response_count));
 799             return (-1);
 800           }
 801         infobuf[i]=inb(CDi_info);
 802       }
 803   else 
 804     {
 805       for (i=0, timeout = jiffies + 100; i < response_count; i++) 
 806         {
 807           for (j=maxtim_data; ; )
 808             {
 809               for ( ;j!=0;j-- )
 810                 {
 811                   st=inb(CDi_status);
 812                   if (!(st&s_not_result_ready)) break;
 813                 }
 814               if (j != 0 || timeout <= jiffies) break;
 815               sbp_sleep(0);
 816               j = 1;
 817             }
 818           if (timeout <= jiffies) return (-1);
 819           infobuf[i]=inb(CDi_info);
 820         }
 821     }
 822   DPRINTF((DBG_000,"SBPCD: ResponseInfo: done.\n"));
 823   return (0);
 824 }
 825 /*==========================================================================*/
 826 static int EvaluateStatus(int st)
     /* [previous][next][first][last][top][bottom][index][help] */
 827 {
 828   if (!new_drive)
 829     {
 830       DriveStruct[d].status_byte=0;
 831       if (st&p_caddin_old) DriveStruct[d].status_byte |= p_door_closed|p_caddy_in;
 832       if (st&p_spinning) DriveStruct[d].status_byte |= p_spinning;
 833       if (st&p_check) DriveStruct[d].status_byte |= p_check;
 834       if (st&p_busy_old) DriveStruct[d].status_byte |= p_busy_new;
 835       if (st&p_disk_ok) DriveStruct[d].status_byte |= p_disk_ok;
 836     }
 837   else { DriveStruct[d].status_byte=st;
 838          st=p_success_old; /* for new drives: fake "successful" bit of old drives */
 839        }
 840   return (st);
 841 }
 842 /*==========================================================================*/
 843 static int ResponseStatus(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 844 {
 845   int i,j;
 846   u_long timeout;
 847 
 848   DPRINTF((DBG_STA,"SBPCD: doing ResponseStatus...\n"));
 849 
 850   if (current == task[0])
 851     {
 852       if (flags_cmd_out & f_respo3) j = maxtim_8;
 853       else if (flags_cmd_out&f_respo2) j=maxtim16;
 854       else j=maxtim04;
 855       for (;j!=0;j--)
 856         {
 857           i=inb(CDi_status);
 858           if (!(i&s_not_result_ready)) break;
 859         }
 860     }
 861   else
 862     {
 863       if (flags_cmd_out & f_respo3) timeout = jiffies;
 864       else if (flags_cmd_out & f_respo2) timeout = jiffies + 1600;
 865       else timeout = jiffies + 400;
 866       j=maxtim_8;
 867       do
 868         {
 869           for ( ;j!=0;j--)
 870             { 
 871               i=inb(CDi_status);
 872               if (!(i&s_not_result_ready)) break;
 873             }
 874           if (j != 0 || timeout <= jiffies) break;
 875           sbp_sleep(0);
 876           j = 1;
 877         }
 878       while (1);
 879     }
 880   if (j==0) 
 881     { if ((flags_cmd_out & f_respo3) == 0)
 882         DPRINTF((DBG_STA,"SBPCD: ResponseStatus: timeout.\n"));
 883       EvaluateStatus(0);
 884       return (-1);
 885     }
 886   i=inb(CDi_info);
 887   i=EvaluateStatus(i);
 888   return (i);
 889 }
 890 /*==========================================================================*/
 891 static void xx_ReadStatus(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 892 {
 893   int i;
 894 
 895   DPRINTF((DBG_STA,"SBPCD: giving xx_ReadStatus command\n"));
 896 
 897   if (!new_drive) OUT(CDo_command,0x81);
 898   else
 899     {
 900       SBPCD_CLI;
 901       OUT(CDo_command,0x05);
 902       for (i=0;i<6;i++) OUT(CDo_command,0);
 903       SBPCD_STI;
 904     }
 905 }
 906 /*==========================================================================*/
 907 static int xx_ReadError(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 908 {
 909   int i;
 910 
 911   clr_cmdbuf();
 912   DPRINTF((DBG_ERR,"SBPCD: giving xx_ReadError command.\n"));
 913   if (new_drive)
 914     {
 915       drvcmd[0]=0x82;
 916       response_count=8;
 917       flags_cmd_out=f_putcmd|f_ResponseStatus;
 918     }
 919   else
 920     {
 921       drvcmd[0]=0x82;
 922       response_count=6;
 923       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus;
 924     }
 925   i=cmd_out();
 926   DriveStruct[d].error_byte=0;
 927   DPRINTF((DBG_ERR,"SBPCD: xx_ReadError: cmd_out(82) returns %d (%02X)\n",i,i));
 928   if (i<0) return (i);
 929   if (new_drive) i=2;
 930   else i=1;
 931   DriveStruct[d].error_byte=infobuf[i];
 932   DPRINTF((DBG_ERR,"SBPCD: xx_ReadError: infobuf[%d] is %d (%02X)\n",i,DriveStruct[d].error_byte,DriveStruct[d].error_byte));
 933   i=sta2err(infobuf[i]);
 934   return (i);
 935 }
 936 /*==========================================================================*/
 937 static int cmd_out(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 938 {
 939   int i=0;
 940 
 941   if (flags_cmd_out&f_putcmd)
 942     { 
 943       DPRINTF((DBG_CMD,"SBPCD: cmd_out: put"));
 944       for (i=0;i<7;i++) DPRINTF((DBG_CMD," %02X",drvcmd[i]));
 945       DPRINTF((DBG_CMD,"\n"));
 946 
 947       SBPCD_CLI;
 948       for (i=0;i<7;i++) OUT(CDo_command,drvcmd[i]);
 949       SBPCD_STI;
 950     }
 951   if (response_count!=0)
 952     {
 953       if (cmd_type!=0)
 954         {
 955           if (sbpro_type==1) OUT(CDo_sel_d_i,0x01);
 956           DPRINTF((DBG_INF,"SBPCD: misleaded to try ResponseData.\n"));
 957           if (sbpro_type==1) OUT(CDo_sel_d_i,0x00);
 958           return (-22);
 959         }
 960       else i=ResponseInfo();
 961       if (i<0) return (-9);
 962     }
 963   if (DriveStruct[d].in_SpinUp != 0) DPRINTF((DBG_SPI,"SBPCD: to CDi_stat_loop.\n"));
 964   if (flags_cmd_out&f_lopsta)
 965     {
 966       i=CDi_stat_loop();
 967       if ((i<0)||!(i&s_attention)) return (-9);
 968     }
 969   if (!(flags_cmd_out&f_getsta)) goto LOC_229;
 970   
 971 LOC_228:
 972   if (DriveStruct[d].in_SpinUp != 0) DPRINTF((DBG_SPI,"SBPCD: to xx_ReadStatus.\n"));
 973   xx_ReadStatus();
 974 
 975 LOC_229:
 976   if (flags_cmd_out&f_ResponseStatus) 
 977     {
 978       if (DriveStruct[d].in_SpinUp != 0) DPRINTF((DBG_SPI,"SBPCD: to ResponseStatus.\n"));
 979       i=ResponseStatus();
 980                    /* builds status_byte, returns orig. status or p_busy_new */
 981       if (i<0) return (-9);
 982       if (flags_cmd_out&(f_bit1|f_wait_if_busy))
 983         {
 984           if (!st_check)
 985             {
 986               if (flags_cmd_out&f_bit1) if (i&p_success_old) goto LOC_232;
 987               if (!(flags_cmd_out&f_wait_if_busy)) goto LOC_228;
 988               if (!st_busy) goto LOC_228;
 989             }
 990         }
 991     }
 992 LOC_232:
 993   if (!(flags_cmd_out&f_obey_p_check)) return (0);
 994   if (!st_check) return (0);
 995   if (DriveStruct[d].in_SpinUp != 0) DPRINTF((DBG_SPI,"SBPCD: to xx_ReadError.\n"));
 996   i=xx_ReadError();
 997   if (DriveStruct[d].in_SpinUp != 0) DPRINTF((DBG_SPI,"SBPCD: to cmd_out OK.\n"));
 998   return (i);
 999 }
1000 /*==========================================================================*/
1001 static int xx_Seek(u_int pos, char f_blk_msf)
     /* [previous][next][first][last][top][bottom][index][help] */
1002 {
1003   int i;
1004 
1005   clr_cmdbuf();
1006   if (f_blk_msf>1) return (-3);
1007   if (!new_drive)
1008     {
1009       if (f_blk_msf==1) pos=msf2blk(pos);
1010       drvcmd[2]=(pos>>16)&0x00FF;
1011       drvcmd[3]=(pos>>8)&0x00FF;
1012       drvcmd[4]=pos&0x00FF;
1013       flags_cmd_out = f_putcmd | f_respo2 | f_lopsta | f_getsta |
1014                        f_ResponseStatus | f_obey_p_check | f_bit1;
1015     }
1016   else
1017     {
1018       if (f_blk_msf==0) pos=blk2msf(pos);
1019       drvcmd[1]=(pos>>16)&0x00FF;
1020       drvcmd[2]=(pos>>8)&0x00FF;
1021       drvcmd[3]=pos&0x00FF;
1022       flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1023     }
1024   drvcmd[0]=0x01;
1025   response_count=0;
1026   i=cmd_out();
1027   return (i);
1028 }
1029 /*==========================================================================*/
1030 static int xx_SpinUp(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1031 {
1032   int i;
1033 
1034   DPRINTF((DBG_SPI,"SBPCD: SpinUp.\n"));
1035   DriveStruct[d].in_SpinUp = 1;
1036   clr_cmdbuf();
1037   if (!new_drive)
1038     {
1039       drvcmd[0]=0x05;
1040       flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check|f_bit1;
1041     }
1042   else
1043     {
1044       drvcmd[0]=0x02;
1045       flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1046     }
1047   response_count=0;
1048   i=cmd_out();
1049   DriveStruct[d].in_SpinUp = 0;
1050   return (i);
1051 }
1052 /*==========================================================================*/
1053 static int yy_SpinDown(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1054 {
1055   int i;
1056 
1057   if (!new_drive) return (-3);
1058   clr_cmdbuf();
1059   drvcmd[0]=0x06;
1060   flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1061   response_count=0;
1062   i=cmd_out();
1063   return (i);
1064 }
1065 /*==========================================================================*/
1066 static int yy_SetSpeed(u_char speed, u_char x1, u_char x2)
     /* [previous][next][first][last][top][bottom][index][help] */
1067 {
1068   int i;
1069 
1070   if (!new_drive) return (-3);
1071   clr_cmdbuf();
1072   drvcmd[0]=0x09;
1073   drvcmd[1]=0x03;
1074   drvcmd[2]=speed;
1075   drvcmd[3]=x1;
1076   drvcmd[4]=x2;
1077   flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1078   response_count=0;
1079   i=cmd_out();
1080   return (i);
1081 }
1082 /*==========================================================================*/
1083 static int xx_SetVolume(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1084 {
1085   int i;
1086   u_char channel0,channel1,volume0,volume1;
1087   u_char control0,value0,control1,value1;
1088 
1089   DriveStruct[d].diskstate_flags &= ~volume_bit;
1090   clr_cmdbuf();
1091   channel0=DriveStruct[d].vol_chan0;
1092   volume0=DriveStruct[d].vol_ctrl0;
1093   channel1=control1=DriveStruct[d].vol_chan1;
1094   volume1=value1=DriveStruct[d].vol_ctrl1;
1095   control0=value0=0;
1096 
1097   if (((DriveStruct[d].drv_options&sax_a)!=0)&&(DriveStruct[d].drv_type>=drv_211))
1098     {
1099       if ((volume0!=0)&&(volume1==0))
1100         {
1101           volume1=volume0;
1102           channel1=channel0;
1103         }
1104       else if ((volume0==0)&&(volume1!=0))
1105         {
1106           volume0=volume1;
1107           channel0=channel1;
1108         }
1109     }
1110   if (channel0>1)
1111     {
1112       channel0=0;
1113       volume0=0;
1114     }
1115   if (channel1>1)
1116     {
1117       channel1=1;
1118       volume1=0;
1119     }
1120   
1121   if (new_drive)
1122     {
1123       control0=channel0+1;
1124       control1=channel1+1;
1125       value0=(volume0>volume1)?volume0:volume1;
1126       value1=value0;
1127       if (volume0==0) control0=0;
1128       if (volume1==0) control1=0;
1129       drvcmd[0]=0x09;
1130       drvcmd[1]=0x05;
1131       drvcmd[3]=control0;
1132       drvcmd[4]=value0;
1133       drvcmd[5]=control1;
1134       drvcmd[6]=value1;
1135       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1136     }
1137   else 
1138     {
1139       if (DriveStruct[d].drv_type>=drv_300)
1140         {
1141           control0=volume0&0xFC;
1142           value0=volume1&0xFC;
1143           if ((volume0!=0)&&(volume0<4)) control0 |= 0x04;
1144           if ((volume1!=0)&&(volume1<4)) value0 |= 0x04;
1145           if (channel0!=0) control0 |= 0x01;
1146           if (channel1==1) value0 |= 0x01;
1147         }
1148       else
1149         {
1150           value0=(volume0>volume1)?volume0:volume1;
1151           if (DriveStruct[d].drv_type<drv_211)
1152             {
1153               if (channel0!=0)
1154                 {
1155                   i=channel1;
1156                   channel1=channel0;
1157                   channel0=i;
1158                   i=volume1;
1159                   volume1=volume0;
1160                   volume0=i;
1161                 }
1162               if (channel0==channel1)
1163                 {
1164                   if (channel0==0)
1165                     {
1166                       channel1=1;
1167                       volume1=0;
1168                       volume0=value0;
1169                     }
1170                   else
1171                     {
1172                       channel0=0;
1173                       volume0=0;
1174                       volume1=value0;
1175                     }
1176                 }
1177             }
1178 
1179           if ((volume0!=0)&&(volume1!=0))
1180             {
1181               if (volume0==0xFF) volume1=0xFF;
1182               else if (volume1==0xFF) volume0=0xFF;
1183             }
1184           else if (DriveStruct[d].drv_type<drv_201) volume0=volume1=value0;
1185 
1186           if (DriveStruct[d].drv_type>=drv_201)
1187             {
1188               if (volume0==0) control0 |= 0x80;
1189               if (volume1==0) control0 |= 0x40;
1190             }
1191           if (DriveStruct[d].drv_type>=drv_211)
1192             {
1193               if (channel0!=0) control0 |= 0x20;
1194               if (channel1!=1) control0 |= 0x10;
1195             }
1196         }
1197       drvcmd[0]=0x84;
1198       drvcmd[1]=0x83;
1199       drvcmd[4]=control0;
1200       drvcmd[5]=value0;
1201       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1202     }
1203 
1204   response_count=0;
1205   i=cmd_out();
1206   if (i>0) return (i);
1207   DriveStruct[d].diskstate_flags |= volume_bit;
1208   return (0);
1209 }
1210 /*==========================================================================*/
1211 static int GetStatus(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1212 {
1213   int i;
1214 
1215   flags_cmd_out=f_getsta|f_ResponseStatus|f_obey_p_check;
1216   response_count=0;
1217   cmd_type=0;
1218   i=cmd_out();
1219   return (i);
1220 }
1221 /*==========================================================================*/
1222 static int xy_DriveReset(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1223 {
1224   int i;
1225 
1226   DPRINTF((DBG_RES,"SBPCD: xy_DriveReset called.\n"));
1227   if (!new_drive) OUT(CDo_reset,0x00);
1228   else
1229     {
1230       clr_cmdbuf();
1231       drvcmd[0]=0x0A;
1232       flags_cmd_out=f_putcmd;
1233       response_count=0;
1234       i=cmd_out();
1235     }
1236   sbp_sleep(100); /* wait a second */
1237   flush_status();
1238   i=GetStatus();
1239   if (i>=0) return -1;
1240   if (DriveStruct[d].error_byte!=aud_12) return -1;
1241   return (0);
1242 }
1243 /*==========================================================================*/
1244 static int SetSpeed(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1245 {
1246   int i, speed;
1247 
1248   if (!(DriveStruct[d].drv_options&(speed_auto|speed_300|speed_150))) return (0);
1249   speed=speed_auto;
1250   if (!(DriveStruct[d].drv_options&speed_auto))
1251     {
1252       speed |= speed_300;
1253       if (!(DriveStruct[d].drv_options&speed_300)) speed=0;
1254     }
1255   i=yy_SetSpeed(speed,0,0);
1256   return (i);
1257 }
1258 /*==========================================================================*/
1259 static int DriveReset(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1260 {
1261   int i;
1262 
1263   i=xy_DriveReset();
1264   if (i<0) return (-2);
1265   do
1266     {
1267       i=GetStatus();
1268       if ((i<0)&&(i!=-15)) return (-2); /* i!=-15 is from sta2err */
1269       if (!st_caddy_in) break;
1270     }
1271   while (!st_diskok);
1272   DriveStruct[d].CD_changed=1;
1273   i=DiskInfo();
1274   if (i<0) return (-2);
1275   return (0);
1276 }
1277 /*==========================================================================*/
1278 static int xx_Pause_Resume(int pau_res)
     /* [previous][next][first][last][top][bottom][index][help] */
1279 {
1280   int i;
1281 
1282   clr_cmdbuf();
1283   if (new_drive)
1284     {
1285       drvcmd[0]=0x0D;
1286       flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1287     }
1288   else
1289     {
1290       drvcmd[0]=0x8D;
1291       flags_cmd_out=f_putcmd|f_respo2|f_getsta|f_ResponseStatus|f_obey_p_check;
1292     }
1293   if (pau_res!=1) drvcmd[1]=0x80;
1294   response_count=0;
1295   i=cmd_out();
1296   return (i);
1297 }
1298 /*==========================================================================*/
1299 static int yy_LockDoor(char lock)
     /* [previous][next][first][last][top][bottom][index][help] */
1300 {
1301   int i;
1302 
1303   if (!new_drive) return (0);
1304   DPRINTF((DBG_LCK,"SBPCD: yy_LockDoor: %d (drive %d)\n", lock, d));
1305   clr_cmdbuf();
1306   drvcmd[0]=0x0C;
1307   if (lock==1) drvcmd[1]=0x01;
1308   flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1309   response_count=0;
1310   i=cmd_out();
1311   return (i);
1312 }
1313 /*==========================================================================*/
1314 static int xx_ReadSubQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1315 {
1316   int i,j;
1317 
1318   DriveStruct[d].diskstate_flags &= ~subq_bit;
1319   for (j=255;j>0;j--)
1320     {
1321       clr_cmdbuf();
1322       if (new_drive)
1323         {
1324           drvcmd[0]=0x87;
1325           flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1326           response_count=11;
1327         }
1328       else
1329         {
1330           drvcmd[0]=0x89;
1331           drvcmd[1]=0x02;
1332           flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1333           response_count=13;
1334         }
1335       i=cmd_out();
1336       if (i<0) return (i);
1337       DPRINTF((DBG_SQ,"SBPCD: xx_ReadSubQ:"));
1338       for (i=0;i<(new_drive?11:13);i++)
1339         {
1340           DPRINTF((DBG_SQ," %02X", infobuf[i]));
1341         }
1342       DPRINTF((DBG_SQ,"\n"));
1343       if (infobuf[0]!=0) break;
1344       if ((!st_spinning) || (j==1))
1345         {
1346           DriveStruct[d].SubQ_ctl_adr=DriveStruct[d].SubQ_trk=DriveStruct[d].SubQ_pnt_idx=DriveStruct[d].SubQ_whatisthis=0;
1347           DriveStruct[d].SubQ_run_tot=DriveStruct[d].SubQ_run_trk=0;
1348           return (0);
1349         }
1350     }
1351   DriveStruct[d].SubQ_ctl_adr=swap_nibbles(infobuf[1]);
1352   DriveStruct[d].SubQ_trk=byt2bcd(infobuf[2]);
1353   DriveStruct[d].SubQ_pnt_idx=byt2bcd(infobuf[3]);
1354   i=4;
1355   if (!new_drive) i=5;
1356   DriveStruct[d].SubQ_run_tot=make32(make16(0,infobuf[i]),make16(infobuf[i+1],infobuf[i+2])); /* msf-bin */
1357   i=7;
1358   if (!new_drive) i=9;
1359   DriveStruct[d].SubQ_run_trk=make32(make16(0,infobuf[i]),make16(infobuf[i+1],infobuf[i+2])); /* msf-bin */
1360   DriveStruct[d].SubQ_whatisthis=infobuf[i+3];
1361   DriveStruct[d].diskstate_flags |= subq_bit;
1362   return (0);
1363 }
1364 /*==========================================================================*/
1365 static int xx_ModeSense(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1366 {
1367   int i;
1368 
1369   DriveStruct[d].diskstate_flags &= ~frame_size_bit;
1370   clr_cmdbuf();
1371   if (new_drive)
1372     {
1373       drvcmd[0]=0x84;
1374       drvcmd[1]=0x00;
1375       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1376       response_count=5;
1377     }
1378   else
1379     {
1380       drvcmd[0]=0x85;
1381       drvcmd[1]=0x00;
1382       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1383       response_count=2;
1384     }
1385   i=cmd_out();
1386   if (i<0) return (i);
1387   i=0;
1388   if (new_drive) DriveStruct[d].sense_byte=infobuf[i++];
1389   DriveStruct[d].frame_size=make16(infobuf[i],infobuf[i+1]);
1390 
1391   DPRINTF((DBG_XA,"SBPCD: xx_ModeSense: "));
1392   for (i=0;i<(new_drive?5:2);i++)
1393     {
1394       DPRINTF((DBG_XA,"%02X ", infobuf[i]));
1395     }
1396   DPRINTF((DBG_XA,"\n"));
1397 
1398   DriveStruct[d].diskstate_flags |= frame_size_bit;
1399   return (0);
1400 }
1401 /*==========================================================================*/
1402 /*==========================================================================*/
1403 static int xx_ModeSelect(int framesize)
     /* [previous][next][first][last][top][bottom][index][help] */
1404 {
1405   int i;
1406 
1407   DriveStruct[d].diskstate_flags &= ~frame_size_bit;
1408   clr_cmdbuf();
1409   DriveStruct[d].frame_size=framesize;
1410   if (framesize==CD_FRAMESIZE_RAW) DriveStruct[d].sense_byte=0x82;
1411   else DriveStruct[d].sense_byte=0x00;
1412 
1413   DPRINTF((DBG_XA,"SBPCD: xx_ModeSelect: %02X %04X\n",
1414            DriveStruct[d].sense_byte, DriveStruct[d].frame_size));
1415 
1416   if (new_drive)
1417     {
1418       drvcmd[0]=0x09;
1419       drvcmd[1]=0x00;
1420       drvcmd[2]=DriveStruct[d].sense_byte;
1421       drvcmd[3]=(DriveStruct[d].frame_size>>8)&0xFF;
1422       drvcmd[4]=DriveStruct[d].frame_size&0xFF;
1423       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1424     }
1425   else
1426     {
1427       drvcmd[0]=0x84;
1428       drvcmd[1]=0x00;
1429       drvcmd[2]=(DriveStruct[d].frame_size>>8)&0xFF;
1430       drvcmd[3]=DriveStruct[d].frame_size&0xFF;
1431       drvcmd[4]=0x00;
1432       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1433     }
1434   response_count=0;
1435   i=cmd_out();
1436   if (i<0) return (i);
1437   DriveStruct[d].diskstate_flags |= frame_size_bit;
1438   return (0);
1439 }
1440 /*==========================================================================*/
1441 #if 0000
1442 static int xx_TellVolume(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1443 {
1444   int i;
1445   u_char switches;
1446   u_char chan0,vol0,chan1,vol1;
1447 
1448   DriveStruct[d].diskstate_flags &= ~volume_bit;
1449   clr_cmdbuf();
1450   if (new_drive)
1451     {
1452       drvcmd[0]=0x84;
1453       drvcmd[1]=0x05;
1454       response_count=5;
1455       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1456     }
1457   else
1458     {
1459       drvcmd[0]=0x85;
1460       drvcmd[1]=0x03;
1461       response_count=2;
1462       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1463     }
1464   i=cmd_out();
1465   if (i<0) return (i);
1466   if (new_drive)
1467     {
1468       chan0=infobuf[1]&0x0F;
1469       vol0=infobuf[2];
1470       chan1=infobuf[3]&0x0F;
1471       vol1=infobuf[4];
1472       if (chan0==0)
1473         {
1474           chan0=1;
1475           vol0=0;
1476         }
1477       if (chan1==0)
1478         {
1479           chan1=2;
1480           vol1=0;
1481         }
1482       chan0 >>= 1;
1483       chan1 >>= 1;
1484     }
1485   else
1486     {
1487       chan0=0;
1488       chan1=1;
1489       vol0=vol1=infobuf[1];
1490       if (DriveStruct[d].drv_type>=drv_201)
1491         {
1492           if (DriveStruct[d].drv_type<drv_300)
1493             {
1494               switches=infobuf[0];
1495               if ((switches&0x80)!=0) vol0=0;
1496               if ((switches&0x40)!=0) vol1=0;
1497               if (DriveStruct[d].drv_type>=drv_211)
1498                 {
1499                   if ((switches&0x20)!=0) chan0=1;
1500                   if ((switches&0x10)!=0) chan1=0;
1501                 }
1502             }
1503           else
1504             {
1505               vol0=infobuf[0];
1506               if ((vol0&0x01)!=0) chan0=1;
1507               if ((vol1&0x01)==0) chan1=0;
1508               vol0 &= 0xFC;
1509               vol1 &= 0xFC;
1510               if (vol0!=0) vol0 += 3;
1511               if (vol1!=0) vol1 += 3;
1512             }
1513         }
1514     }
1515   DriveStruct[d].vol_chan0=chan0;
1516   DriveStruct[d].vol_ctrl0=vol0;
1517   DriveStruct[d].vol_chan1=chan1;
1518   DriveStruct[d].vol_ctrl1=vol1;
1519   DriveStruct[d].vol_chan2=2;
1520   DriveStruct[d].vol_ctrl2=0xFF;
1521   DriveStruct[d].vol_chan3=3;
1522   DriveStruct[d].vol_ctrl3=0xFF;
1523   DriveStruct[d].diskstate_flags |= volume_bit;
1524   return (0);
1525 }
1526 #endif
1527 /*==========================================================================*/
1528 static int xx_ReadCapacity(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1529 {
1530   int i;
1531 
1532   DriveStruct[d].diskstate_flags &= ~cd_size_bit;
1533   clr_cmdbuf();
1534   if (new_drive)
1535     {
1536       drvcmd[0]=0x85;
1537       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1538     }
1539   else
1540     {
1541       drvcmd[0]=0x88;
1542       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1543     }
1544   response_count=5;
1545   i=cmd_out();
1546   if (i<0) return (i);
1547   DriveStruct[d].CDsize_blk=make32(make16(0,infobuf[0]),make16(infobuf[1],infobuf[2]));
1548   if (new_drive) DriveStruct[d].CDsize_blk=msf2blk(DriveStruct[d].CDsize_blk);
1549   DriveStruct[d].CDsize_frm = (DriveStruct[d].CDsize_blk * make16(infobuf[3],infobuf[4])) / CD_FRAMESIZE;
1550   DriveStruct[d].CDsize_blk += 151;
1551   DriveStruct[d].diskstate_flags |= cd_size_bit;
1552   return (0);
1553 }
1554 /*==========================================================================*/
1555 static int xx_ReadTocDescr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1556 {
1557   int i;
1558 
1559   DriveStruct[d].diskstate_flags &= ~toc_bit;
1560   clr_cmdbuf();
1561   if (new_drive)
1562     {
1563       drvcmd[0]=0x8B;
1564       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1565     }
1566   else
1567     {
1568       drvcmd[0]=0x8B;
1569       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1570     }
1571   response_count=6;
1572   i=cmd_out();
1573   if (i<0) return (i);
1574   DriveStruct[d].xa_byte=infobuf[0];
1575   DriveStruct[d].n_first_track=infobuf[1];
1576   DriveStruct[d].n_last_track=infobuf[2];
1577   DriveStruct[d].size_msf=make32(make16(0,infobuf[3]),make16(infobuf[4],infobuf[5]));
1578   DriveStruct[d].size_blk=msf2blk(DriveStruct[d].size_msf);
1579   DriveStruct[d].diskstate_flags |= toc_bit;
1580   DPRINTF((DBG_TOC,"SBPCD: TocDesc: %02X %02X %02X %08X\n",
1581          DriveStruct[d].xa_byte,DriveStruct[d].n_first_track,DriveStruct[d].n_last_track,DriveStruct[d].size_msf));
1582   return (0);
1583 }
1584 /*==========================================================================*/
1585 static int xx_ReadTocEntry(int num)
     /* [previous][next][first][last][top][bottom][index][help] */
1586 {
1587   int i;
1588 
1589   clr_cmdbuf();
1590   if (new_drive)
1591     {
1592       drvcmd[0]=0x8C;
1593       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1594     }
1595   else
1596     {
1597       drvcmd[0]=0x8C;
1598       drvcmd[1]=0x02;
1599       flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1600     }
1601   drvcmd[2]=num;
1602   response_count=8;
1603   i=cmd_out();
1604   if (i<0) return (i);
1605   DriveStruct[d].TocEnt_nixbyte=infobuf[0];
1606   DriveStruct[d].TocEnt_ctl_adr=swap_nibbles(infobuf[1]);
1607   DriveStruct[d].TocEnt_number=infobuf[2];
1608   DriveStruct[d].TocEnt_format=infobuf[3];
1609   if (new_drive) i=4;
1610   else i=5;
1611   DriveStruct[d].TocEnt_address=make32(make16(0,infobuf[i]),make16(infobuf[i+1],infobuf[i+2]));
1612   DPRINTF((DBG_TOC,"SBPCD: TocEntry: %02X %02X %02X %02X %08X\n",
1613            DriveStruct[d].TocEnt_nixbyte,DriveStruct[d].TocEnt_ctl_adr,DriveStruct[d].TocEnt_number,
1614            DriveStruct[d].TocEnt_format,DriveStruct[d].TocEnt_address));
1615   return (0);
1616 }
1617 /*==========================================================================*/
1618 static int xx_ReadPacket(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1619 {
1620   int i;
1621 
1622   clr_cmdbuf();
1623   drvcmd[0]=0x8E;
1624   drvcmd[1]=response_count;
1625   flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1626   i=cmd_out();
1627   return (i);
1628 }
1629 /*==========================================================================*/
1630 static int convert_UPC(u_char *p)
     /* [previous][next][first][last][top][bottom][index][help] */
1631 {
1632   int i;
1633 
1634   p++;
1635   if (!new_drive) p[13]=0;
1636   for (i=0;i<7;i++)
1637     {
1638       if (new_drive) DriveStruct[d].UPC_buf[i]=swap_nibbles(*p++);
1639       else
1640         {
1641           DriveStruct[d].UPC_buf[i]=((*p++)<<4)&0xFF;
1642           DriveStruct[d].UPC_buf[i] |= *p++;
1643         }
1644     }
1645   DriveStruct[d].UPC_buf[6] &= 0xF0;
1646   return (0);
1647 }
1648 /*==========================================================================*/
1649 static int xx_ReadUPC(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1650 {
1651   int i;
1652 #if TEST_UPC
1653   int block, checksum;
1654 #endif TEST_UPC
1655 
1656   DriveStruct[d].diskstate_flags &= ~upc_bit;
1657 #if TEST_UPC
1658   for (block=CD_BLOCK_OFFSET+1;block<CD_BLOCK_OFFSET+200;block++)
1659     {
1660 #endif TEST_UPC
1661       clr_cmdbuf();
1662       if (new_drive)
1663         {
1664           drvcmd[0]=0x88;
1665 #if TEST_UPC
1666           drvcmd[1]=(block>>16)&0xFF;
1667           drvcmd[2]=(block>>8)&0xFF;
1668           drvcmd[3]=block&0xFF;
1669 #endif TEST_UPC
1670           response_count=8;
1671           flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1672         }
1673       else
1674         {
1675           drvcmd[0]=0x08;
1676 #if TEST_UPC
1677           drvcmd[2]=(block>>16)&0xFF;
1678           drvcmd[3]=(block>>8)&0xFF;
1679           drvcmd[4]=block&0xFF;
1680 #endif TEST_UPC
1681           response_count=0;
1682           flags_cmd_out=f_putcmd|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check|f_bit1;
1683         }
1684       i=cmd_out();
1685       if (i<0) return (i);
1686       if (!new_drive)
1687         {
1688           response_count=16;
1689           i=xx_ReadPacket();
1690           if (i<0) return (i);
1691         }
1692 #if TEST_UPC
1693       checksum=0;
1694 #endif TEST_UPC
1695       DPRINTF((DBG_UPC,"SBPCD: UPC info: "));
1696       for (i=0;i<(new_drive?8:16);i++)
1697         {
1698 #if TEST_UPC
1699           checksum |= infobuf[i];
1700 #endif TEST_UPC
1701           DPRINTF((DBG_UPC,"%02X ", infobuf[i]));
1702         }
1703       DPRINTF((DBG_UPC,"\n"));
1704 #if TEST_UPC
1705       if ((checksum&0x7F)!=0) break;
1706     }
1707 #endif TEST_UPC
1708   DriveStruct[d].UPC_ctl_adr=0;
1709   if (new_drive) i=0;
1710   else i=2;
1711   if ((infobuf[i]&0x80)!=0)
1712     {
1713       convert_UPC(&infobuf[i]);
1714       DriveStruct[d].UPC_ctl_adr = (DriveStruct[d].TocEnt_ctl_adr & 0xF0) | 0x02;
1715     }
1716 
1717   DPRINTF((DBG_UPC,"SBPCD: UPC code: "));
1718   DPRINTF((DBG_UPC,"(%02X) ", DriveStruct[d].UPC_ctl_adr));
1719   for (i=0;i<7;i++)
1720     {
1721       DPRINTF((DBG_UPC,"%02X ", DriveStruct[d].UPC_buf[i]));
1722     }
1723   DPRINTF((DBG_UPC,"\n"));
1724 
1725   DriveStruct[d].diskstate_flags |= upc_bit;
1726   return (0);
1727 }
1728 /*==========================================================================*/
1729 static int yy_CheckMultiSession(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1730 {
1731   int i;
1732 
1733   DriveStruct[d].diskstate_flags &= ~multisession_bit;
1734   DriveStruct[d].f_multisession=0;
1735   clr_cmdbuf();
1736   if (new_drive)
1737     {
1738       drvcmd[0]=0x8D;
1739       response_count=6;
1740       flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1741       i=cmd_out();
1742       if (i<0) return (i);
1743       if ((infobuf[0]&0x80)!=0)
1744         {
1745           DPRINTF((DBG_MUL,"SBPCD: MultiSession CD detected: %02X %02X %02X %02X %02X %02X\n",
1746                          infobuf[0], infobuf[1], infobuf[2],
1747                          infobuf[3], infobuf[4], infobuf[5]));
1748           DriveStruct[d].f_multisession=1;
1749           DriveStruct[d].lba_multi=msf2blk(make32(make16(0,infobuf[1]),
1750                                    make16(infobuf[2],infobuf[3])));
1751         }
1752     }
1753   DriveStruct[d].diskstate_flags |= multisession_bit;
1754   return (0);
1755 }
1756 /*==========================================================================*/
1757 #if FUTURE
1758 static int yy_SubChanInfo(int frame, int count, u_char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
1759 /* "frame" is a RED BOOK (msf-bin) address */
1760 {
1761   int i;
1762 
1763   if (!new_drive) return (-ENOSYS); /* drive firmware lacks it */
1764 #if 0
1765   if (DriveStruct[d].audio_state!=audio_playing) return (-ENODATA);
1766 #endif
1767   clr_cmdbuf();
1768   drvcmd[0]=0x11;
1769   drvcmd[1]=(frame>>16)&0xFF;
1770   drvcmd[2]=(frame>>8)&0xFF;
1771   drvcmd[3]=frame&0xFF;
1772   drvcmd[5]=(count>>8)&0xFF;
1773   drvcmd[6]=count&0xFF;
1774   flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1775   cmd_type=READ_SC;
1776   DriveStruct[d].frame_size=CD_FRAMESIZE_SUB;
1777   i=cmd_out(); /* which buffer to use? */
1778   return (i);
1779 }
1780 #endif FUTURE
1781 /*==========================================================================*/
1782 static void check_datarate(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1783 {
1784 #ifdef CDMKE
1785   int i=0;
1786 
1787   DPRINTF((DBG_IOX,"SBPCD: check_datarate entered.\n"));
1788   datarate=0;
1789 #if TEST_STI
1790   for (i=0;i<=1000;i++) printk(".");
1791 #endif
1792   /* set a timer to make (timed_out!=0) after 1.1 seconds */
1793 #if 0
1794   del_timer(&delay_timer);
1795 #endif
1796   delay_timer.expires = 110;
1797   timed_out=0;
1798   add_timer(&delay_timer);
1799   DPRINTF((DBG_TIM,"SBPCD: timer started (110).\n"));
1800   do
1801     {
1802       i=inb(CDi_status);
1803       datarate++;
1804 #if 00000
1805       if (datarate>0x0FFFFFFF) break;
1806 #endif 00000
1807     }
1808   while (!timed_out); /* originally looping for 1.1 seconds */
1809 #if 0
1810   del_timer(&delay_timer);
1811 #endif 0
1812   DPRINTF((DBG_TIM,"SBPCD: datarate: %d\n", datarate));
1813   if (datarate<65536) datarate=65536;
1814 
1815   maxtim16=datarate*16;
1816   maxtim04=datarate*4;
1817   maxtim02=datarate*2;
1818   maxtim_8=datarate/32;
1819 #if LONG_TIMING
1820   maxtim_data=datarate/100;
1821 #else
1822   maxtim_data=datarate/300;
1823 #endif LONG_TIMING
1824   DPRINTF((DBG_TIM,"SBPCD: maxtim_8 %d, maxtim_data %d.\n",
1825            maxtim_8, maxtim_data));
1826 #endif CDMKE
1827 }
1828 /*==========================================================================*/
1829 static int check_version(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1830 {
1831   int i, j;
1832 
1833   DPRINTF((DBG_INI,"SBPCD: check_version entered.\n"));
1834   /* clear any pending error state */
1835   clr_cmdbuf();
1836   drvcmd[0]=0x82;
1837   response_count=9;
1838   flags_cmd_out=f_putcmd;
1839   i=cmd_out();
1840   if (i<0) DPRINTF((DBG_INI,"SBPCD: cmd_82 returns %d (ok anyway).\n",i));
1841 
1842   /* read drive version */
1843   clr_cmdbuf();
1844   for (i=0;i<12;i++) infobuf[i]=0;
1845   drvcmd[0]=0x83;
1846   response_count=12;
1847   flags_cmd_out=f_putcmd;
1848   i=cmd_out();
1849   if (i<0) DPRINTF((DBG_INI,"SBPCD: cmd_83 returns %d\n",i));
1850 
1851   DPRINTF((DBG_INI,"SBPCD: infobuf = \""));
1852   for (i=0;i<12;i++) DPRINTF((DBG_INI,"%c",infobuf[i]));
1853   DPRINTF((DBG_INI,"\"\n"));
1854 
1855   for (i=0;i<4;i++) if (infobuf[i]!=drive_family[i]) break;
1856   if (i==4)
1857     {
1858       DriveStruct[d].drive_model[0]=infobuf[i++];
1859       DriveStruct[d].drive_model[1]=infobuf[i++];
1860       DriveStruct[d].drive_model[2]='-';
1861       DriveStruct[d].drive_model[3]='x';
1862       DriveStruct[d].drv_type=drv_new;
1863     }
1864   else
1865     {
1866       for (i=0;i<8;i++) if (infobuf[i]!=drive_vendor[i]) break;
1867       if (i!=8)
1868         {
1869           DPRINTF((DBG_INI,"SBPCD: check_version: error.\n"));
1870           return (-1);
1871         }
1872       DriveStruct[d].drive_model[0]='2';
1873       DriveStruct[d].drive_model[1]='x';
1874       DriveStruct[d].drive_model[2]='-';
1875       DriveStruct[d].drive_model[3]='x';
1876       DriveStruct[d].drv_type=drv_old;
1877     }
1878   for (j=0;j<4;j++) DriveStruct[d].firmware_version[j]=infobuf[i+j];
1879   j = (DriveStruct[d].firmware_version[0] & 0x0F) * 100 +
1880       (DriveStruct[d].firmware_version[2] & 0x0F) *10 +
1881       (DriveStruct[d].firmware_version[3] & 0x0F);
1882   if (new_drive)
1883     {
1884       if (j<100) DriveStruct[d].drv_type=drv_099;
1885       else DriveStruct[d].drv_type=drv_100;
1886     }
1887   else if (j<200) DriveStruct[d].drv_type=drv_199;
1888   else if (j<201) DriveStruct[d].drv_type=drv_200;
1889   else if (j<210) DriveStruct[d].drv_type=drv_201;
1890   else if (j<211) DriveStruct[d].drv_type=drv_210;
1891   else if (j<300) DriveStruct[d].drv_type=drv_211;
1892   else DriveStruct[d].drv_type=drv_300;
1893   DPRINTF((DBG_INI,"SBPCD: check_version done.\n"));
1894   return (0);
1895 }
1896 /*==========================================================================*/
1897 static int switch_drive(int num)
     /* [previous][next][first][last][top][bottom][index][help] */
1898 {
1899   int i;
1900 
1901   d=num;
1902 
1903   i=num;
1904   if (sbpro_type==1) i=(i&0x01)<<1|(i&0x02)>>1;
1905   OUT(CDo_enable,i);
1906   DPRINTF((DBG_DID,"SBPCD: switch_drive: drive %d activated.\n",DriveStruct[d].drv_minor));
1907   return (0);
1908 }
1909 /*==========================================================================*/
1910 /*
1911  * probe for the presence of drives on the selected controller
1912  */
1913 static int check_drives(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1914 {
1915   int i, j;
1916   char *printk_header="";
1917 
1918   DPRINTF((DBG_INI,"SBPCD: check_drives entered.\n"));
1919 
1920   ndrives=0;
1921   for (j=0;j<NR_SBPCD;j++)
1922     {
1923       DriveStruct[j].drv_minor=j;
1924       switch_drive(j);
1925       DPRINTF((DBG_ID,"SBPCD: check_drives: drive %d activated.\n",j));
1926       i=check_version();
1927       DPRINTF((DBG_ID,"SBPCD: check_version returns %d.\n",i));
1928       if (i>=0)
1929         {
1930           ndrives++;
1931           DriveStruct[d].drv_options=drv_pattern[j];
1932           if (!new_drive)
1933             DriveStruct[d].drv_options&=~(speed_auto|speed_300|speed_150);
1934           printk("%sDrive %d: %s%.4s (%.4s)\n", printk_header,
1935                  DriveStruct[d].drv_minor,
1936                  drive_family,
1937                  DriveStruct[d].drive_model,
1938                  DriveStruct[d].firmware_version);
1939           printk_header="       - ";
1940         }
1941       else DriveStruct[d].drv_minor=-1;
1942     }
1943   if (ndrives==0) return (-1);
1944   return (0);
1945 }
1946 /*==========================================================================*/
1947 #if 000
1948 static void timewait(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1949 {
1950   int i;
1951   for (i=0; i<65500; i++);
1952 }
1953 #endif 000
1954 /*==========================================================================*/
1955 #if FUTURE
1956 /*
1957  *  obtain if requested service disturbs current audio state
1958  */            
1959 static int obey_audio_state(u_char audio_state, u_char func,u_char subfunc)
     /* [previous][next][first][last][top][bottom][index][help] */
1960 {
1961   switch (audio_state)                   /* audio status from controller  */
1962     {
1963     case aud_11: /* "audio play in progress" */
1964     case audx11:
1965       switch (func)                      /* DOS command code */
1966         {
1967         case cmd_07: /* input flush  */
1968         case cmd_0d: /* open device  */
1969         case cmd_0e: /* close device */
1970         case cmd_0c: /* ioctl output */
1971           return (1);
1972         case cmd_03: /* ioctl input  */
1973           switch (subfunc)
1974             /* DOS ioctl input subfunction */
1975             {
1976             case cxi_00:
1977             case cxi_06:
1978             case cxi_09:
1979               return (1);
1980             default:
1981               return (ERROR15);
1982             }
1983           return (1);
1984         default:
1985           return (ERROR15);
1986         }
1987       return (1);
1988     case aud_12:                  /* "audio play paused"      */
1989     case audx12:
1990       return (1);
1991     default:
1992       return (2);
1993     }
1994 }
1995 #endif FUTURE
1996 /*==========================================================================*/
1997 /* allowed is only
1998  * ioctl_o, flush_input, open_device, close_device, 
1999  * tell_address, tell_volume, tell_capabiliti,
2000  * tell_framesize, tell_CD_changed, tell_audio_posi
2001  */
2002 static int check_allowed1(u_char func1, u_char func2)
     /* [previous][next][first][last][top][bottom][index][help] */
2003 {
2004 #if 000
2005   if (func1==ioctl_o) return (0);
2006   if (func1==read_long) return (-1);
2007   if (func1==read_long_prefetch) return (-1);
2008   if (func1==seek) return (-1);
2009   if (func1==audio_play) return (-1);
2010   if (func1==audio_pause) return (-1);
2011   if (func1==audio_resume) return (-1);
2012   if (func1!=ioctl_i) return (0);
2013   if (func2==tell_SubQ_run_tot) return (-1);
2014   if (func2==tell_cdsize) return (-1);
2015   if (func2==tell_TocDescrip) return (-1);
2016   if (func2==tell_TocEntry) return (-1);
2017   if (func2==tell_subQ_info) return (-1);
2018   if (new_drive) if (func2==tell_SubChanInfo) return (-1);
2019   if (func2==tell_UPC) return (-1);
2020 #else
2021   return (0);
2022 #endif 000
2023 }
2024 /*==========================================================================*/
2025 static int check_allowed2(u_char func1, u_char func2)
     /* [previous][next][first][last][top][bottom][index][help] */
2026 {
2027 #if 000
2028   if (func1==read_long) return (-1);
2029   if (func1==read_long_prefetch) return (-1);
2030   if (func1==seek) return (-1);
2031   if (func1==audio_play) return (-1);
2032   if (func1!=ioctl_o) return (0);
2033   if (new_drive)
2034     {
2035       if (func2==EjectDisk) return (-1);
2036       if (func2==CloseTray) return (-1);
2037     }
2038 #else
2039   return (0);
2040 #endif 000
2041 }
2042 /*==========================================================================*/
2043 static int check_allowed3(u_char func1, u_char func2)
     /* [previous][next][first][last][top][bottom][index][help] */
2044 {
2045 #if 000
2046   if (func1==ioctl_i)
2047     {
2048       if (func2==tell_address) return (0);
2049       if (func2==tell_capabiliti) return (0);
2050       if (func2==tell_CD_changed) return (0);
2051       if (!new_drive) if (func2==tell_SubChanInfo) return (0);
2052       return (-1);
2053     }
2054   if (func1==ioctl_o)
2055     {
2056       if (func2==DriveReset) return (0);
2057       if (!new_drive)
2058         {
2059           if (func2==EjectDisk) return (0);
2060           if (func2==LockDoor) return (0);
2061           if (func2==CloseTray) return (0);
2062         }
2063       return (-1);
2064     }
2065   if (func1==flush_input) return (-1);
2066   if (func1==read_long) return (-1);
2067   if (func1==read_long_prefetch) return (-1);
2068   if (func1==seek) return (-1);
2069   if (func1==audio_play) return (-1);
2070   if (func1==audio_pause) return (-1);
2071   if (func1==audio_resume) return (-1);
2072 #else
2073   return (0);
2074 #endif 000
2075 }
2076 /*==========================================================================*/
2077 static int seek_pos_audio_end(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2078 {
2079   int i;
2080 
2081   i=msf2blk(DriveStruct[d].pos_audio_end)-1;
2082   if (i<0) return (-1);
2083   i=xx_Seek(i,0);
2084   return (i);
2085 }
2086 /*==========================================================================*/
2087 static int ReadToC(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2088 {
2089   int i, j;
2090   DriveStruct[d].diskstate_flags &= ~toc_bit;
2091   DriveStruct[d].ored_ctl_adr=0;
2092   for (j=DriveStruct[d].n_first_track;j<=DriveStruct[d].n_last_track;j++)
2093     {
2094       i=xx_ReadTocEntry(j);
2095       if (i<0) return (i);
2096       DriveStruct[d].TocBuffer[j].nixbyte=DriveStruct[d].TocEnt_nixbyte;
2097       DriveStruct[d].TocBuffer[j].ctl_adr=DriveStruct[d].TocEnt_ctl_adr;
2098       DriveStruct[d].TocBuffer[j].number=DriveStruct[d].TocEnt_number;
2099       DriveStruct[d].TocBuffer[j].format=DriveStruct[d].TocEnt_format;
2100       DriveStruct[d].TocBuffer[j].address=DriveStruct[d].TocEnt_address;
2101       DriveStruct[d].ored_ctl_adr |= DriveStruct[d].TocEnt_ctl_adr;
2102     }
2103 /* fake entry for LeadOut Track */
2104   DriveStruct[d].TocBuffer[j].nixbyte=0;
2105   DriveStruct[d].TocBuffer[j].ctl_adr=0;
2106   DriveStruct[d].TocBuffer[j].number=CDROM_LEADOUT;
2107   DriveStruct[d].TocBuffer[j].format=0;
2108   DriveStruct[d].TocBuffer[j].address=DriveStruct[d].size_msf;
2109 
2110   DriveStruct[d].diskstate_flags |= toc_bit;
2111   return (0);
2112 }
2113 /*==========================================================================*/
2114 static int DiskInfo(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2115 {
2116   int i, j;
2117 
2118       DriveStruct[d].mode=READ_M1;
2119 
2120 #undef LOOP_COUNT
2121 #define LOOP_COUNT 20 /* needed for some "old" drives */
2122 
2123   for (j=1;j<LOOP_COUNT;j++)
2124     {
2125       i=SetSpeed();
2126       if (i<0)
2127         {
2128           DPRINTF((DBG_INF,"SBPCD: DiskInfo: SetSpeed returns %d\n", i));
2129           continue;
2130         }
2131       i=xx_ModeSense();
2132       if (i<0)
2133         {
2134           DPRINTF((DBG_INF,"SBPCD: DiskInfo: xx_ModeSense returns %d\n", i));
2135           continue;
2136         }
2137       i=xx_ReadCapacity();
2138       if (i>=0) break;
2139       DPRINTF((DBG_INF,"SBPCD: DiskInfo: ReadCapacity #%d returns %d\n", j, i));
2140       i=DriveReset();
2141     }
2142   if (j==LOOP_COUNT) return (-2); /* give up */
2143 
2144   i=xx_ReadTocDescr();
2145   if (i<0)
2146     {
2147       DPRINTF((DBG_INF,"SBPCD: DiskInfo: ReadTocDescr returns %d\n", i));
2148       return (i);
2149     }
2150   i=ReadToC();
2151   if (i<0)
2152     {
2153       DPRINTF((DBG_INF,"SBPCD: DiskInfo: ReadToC returns %d\n", i));
2154       return (i);
2155     }
2156   i=yy_CheckMultiSession();
2157   if (i<0)
2158     {
2159       DPRINTF((DBG_INF,"SBPCD: DiskInfo: yy_CheckMultiSession returns %d\n", i));
2160       return (i);
2161     }
2162   i=xx_ReadTocEntry(DriveStruct[d].n_first_track);
2163   if (i<0)
2164     {
2165       DPRINTF((DBG_INF,"SBPCD: DiskInfo: xx_ReadTocEntry(1) returns %d\n", i));
2166       return (i);
2167     }
2168   i=xx_ReadUPC();
2169   if (i<0)
2170     {
2171       DPRINTF((DBG_INF,"SBPCD: DiskInfo: xx_ReadUPC returns %d\n", i));
2172       return (i);
2173     }
2174 #ifdef XA_TEST2
2175   if ((!new_drive) && (DriveStruct[d].xa_byte==0x20)) /* XA disk with old drive */
2176       {
2177         xx_ModeSelect(CD_FRAMESIZE_XA);
2178         xx_ModeSense();
2179       }
2180 #endif XA_TEST2
2181 
2182   return (0);
2183 }
2184 /*==========================================================================*/
2185 /*
2186  *  called always if driver gets entered
2187  *  returns 0 or ERROR2 or ERROR15
2188  */
2189 static int prepare(u_char func, u_char subfunc)
     /* [previous][next][first][last][top][bottom][index][help] */
2190 {
2191   int i;
2192 
2193   if (!new_drive)
2194     {
2195       i=inb(CDi_status);
2196       if (i&s_attention) GetStatus();
2197     }
2198   else GetStatus();
2199   if (DriveStruct[d].CD_changed==0xFF)
2200     {
2201 #if MANY_SESSION
2202 #else
2203       DriveStruct[d].diskstate_flags=0;
2204 #endif MANY_SESSION
2205       DriveStruct[d].audio_state=0;
2206       if (!st_diskok)
2207         {
2208           i=check_allowed1(func,subfunc);
2209           if (i<0) return (-2);
2210         }
2211       else 
2212         {
2213           i=check_allowed3(func,subfunc);
2214           if (i<0)
2215             {
2216               DriveStruct[d].CD_changed=1;
2217               return (-15);
2218             }
2219         }
2220     }
2221   else
2222     {
2223       if (!st_diskok)
2224         {
2225 #if MANY_SESSION
2226 #else
2227           DriveStruct[d].diskstate_flags=0;
2228 #endif MANY_SESSION
2229           DriveStruct[d].audio_state=0;
2230           i=check_allowed1(func,subfunc);
2231           if (i<0) return (-2);
2232         }
2233       else
2234         { 
2235           if (st_busy)
2236             {
2237               if (DriveStruct[d].audio_state!=audio_pausing)
2238                 {
2239                   i=check_allowed2(func,subfunc);
2240                   if (i<0) return (-2);
2241                 }
2242             }
2243           else
2244             {
2245               if (DriveStruct[d].audio_state==audio_playing) seek_pos_audio_end();
2246               DriveStruct[d].audio_state=0;
2247             }
2248           if (!frame_size_valid)
2249             {
2250               i=DiskInfo();
2251               if (i<0)
2252                 {
2253 #if MANY_SESSION
2254 #else
2255                   DriveStruct[d].diskstate_flags=0;
2256 #endif MANY_SESSION
2257                   DriveStruct[d].audio_state=0;
2258                   i=check_allowed1(func,subfunc);
2259                   if (i<0) return (-2);
2260                 }
2261             }
2262         }
2263     }
2264   return (0);
2265 }
2266 /*==========================================================================*/
2267 static int xx_PlayAudioMSF(int pos_audio_start,int pos_audio_end)
     /* [previous][next][first][last][top][bottom][index][help] */
2268 {
2269   int i;
2270 
2271   if (DriveStruct[d].audio_state==audio_playing) return (-EINVAL);
2272   clr_cmdbuf();
2273   if (new_drive)
2274     {
2275       drvcmd[0]=0x0E;
2276       flags_cmd_out = f_putcmd | f_respo2 | f_ResponseStatus |
2277                        f_obey_p_check | f_wait_if_busy;
2278     }
2279   else
2280     {
2281       drvcmd[0]=0x0B;
2282       flags_cmd_out = f_putcmd | f_respo2 | f_lopsta | f_getsta |
2283                        f_ResponseStatus | f_obey_p_check | f_wait_if_busy;
2284     }
2285   drvcmd[1]=(pos_audio_start>>16)&0x00FF;
2286   drvcmd[2]=(pos_audio_start>>8)&0x00FF;
2287   drvcmd[3]=pos_audio_start&0x00FF;
2288   drvcmd[4]=(pos_audio_end>>16)&0x00FF;
2289   drvcmd[5]=(pos_audio_end>>8)&0x00FF;
2290   drvcmd[6]=pos_audio_end&0x00FF;
2291   response_count=0;
2292   i=cmd_out();
2293   return (i);
2294 }
2295 /*==========================================================================*/
2296 /*==========================================================================*/
2297 /*==========================================================================*/
2298 /*
2299  * Check the results of the "get status" command.
2300  */
2301 static int sbp_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2302 {
2303   int st;
2304 
2305   st=ResponseStatus();
2306   if (st<0)
2307     {
2308       DPRINTF((DBG_INF,"SBPCD: sbp_status: timeout.\n"));
2309       return (0);
2310     }
2311 
2312   if (!st_spinning) DPRINTF((DBG_SPI,"SBPCD: motor got off - ignoring.\n"));
2313 
2314   if (st_check) 
2315     {
2316       DPRINTF((DBG_INF,"SBPCD: st_check detected - retrying.\n"));
2317       return (0);
2318     }
2319   if (!st_door_closed)
2320     {
2321       DPRINTF((DBG_INF,"SBPCD: door is open - retrying.\n"));
2322       return (0);
2323     }
2324   if (!st_caddy_in)
2325     {
2326       DPRINTF((DBG_INF,"SBPCD: disk removed - retrying.\n"));
2327       return (0);
2328     }
2329   if (!st_diskok) 
2330     {
2331       DPRINTF((DBG_INF,"SBPCD: !st_diskok detected - retrying.\n"));
2332       return (0);
2333     }
2334   if (st_busy) 
2335     {
2336       DPRINTF((DBG_INF,"SBPCD: st_busy detected - retrying.\n"));
2337       return (0);
2338     }
2339   return (1);
2340 }
2341 /*==========================================================================*/
2342 
2343 /*==========================================================================*/
2344 /*==========================================================================*/
2345 /*
2346  * ioctl support, adopted from scsi/sr_ioctl.c and mcd.c
2347  */
2348 static int sbpcd_ioctl(struct inode *inode, struct file *file, u_int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
2349                        u_long arg)
2350 {
2351   int i, st;
2352 
2353   DPRINTF((DBG_IO2,"SBPCD: ioctl(%d, 0x%08lX, 0x%08lX)\n",
2354                                 MINOR(inode->i_rdev), cmd, arg));
2355   if (!inode) return (-EINVAL);
2356   i=MINOR(inode->i_rdev);
2357   if ( (i<0) || (i>=NR_SBPCD) )
2358     {
2359       printk("SBPCD: ioctl: bad device: %d\n", i);
2360       return (-ENODEV);             /* no such drive */
2361     }
2362   switch_drive(i);
2363 
2364   st=GetStatus();
2365   if (st<0) return (-EIO);
2366   
2367   if (!toc_valid)
2368     {
2369       i=DiskInfo();
2370       if (i<0) return (-EIO);   /* error reading TOC */
2371     }
2372   
2373   DPRINTF((DBG_IO2,"SBPCD: ioctl: device %d, request %04X\n",i,cmd));
2374   switch (cmd)          /* Sun-compatible */
2375     {
2376     case DDIOCSDBG:             /* DDI Debug */
2377       if (! suser()) return (-EPERM);
2378       i = verify_area(VERIFY_READ, (int *) arg, sizeof(int));
2379       if (i>=0) i=sbpcd_dbg_ioctl(arg,1);
2380       return (i);
2381 
2382     case CDROMPAUSE:     /* Pause the drive */
2383       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMPAUSE entered.\n"));
2384       /* pause the drive unit when it is currently in PLAY mode,         */
2385       /* or reset the starting and ending locations when in PAUSED mode. */
2386       /* If applicable, at the next stopping point it reaches            */
2387       /* the drive will discontinue playing.                             */
2388       switch (DriveStruct[d].audio_state)
2389         {
2390         case audio_playing:
2391           i=xx_Pause_Resume(1);
2392           if (i<0) return (-EIO);
2393           DriveStruct[d].audio_state=audio_pausing;
2394           i=xx_ReadSubQ();
2395           if (i<0) return (-EIO);
2396           DriveStruct[d].pos_audio_start=DriveStruct[d].SubQ_run_tot;
2397           return (0);
2398         case audio_pausing:
2399           i=xx_Seek(DriveStruct[d].pos_audio_start,1);
2400           if (i<0) return (-EIO);
2401           return (0);
2402         default:
2403           return (-EINVAL);
2404         }
2405       
2406     case CDROMRESUME: /* resume paused audio play */
2407       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMRESUME entered.\n"));
2408       /* resume playing audio tracks when a previous PLAY AUDIO call has  */
2409       /* been paused with a PAUSE command.                                */
2410       /* It will resume playing from the location saved in SubQ_run_tot.  */
2411       if (DriveStruct[d].audio_state!=audio_pausing) return -EINVAL;
2412       i=xx_Pause_Resume(3);
2413       if (i<0) return (-EIO);
2414       DriveStruct[d].audio_state=audio_playing;
2415       return (0);
2416 
2417     case CDROMPLAYMSF:
2418       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMPLAYMSF entered.\n"));
2419       if (DriveStruct[d].audio_state==audio_playing)
2420         {
2421           i=xx_Pause_Resume(1);
2422           if (i<0) return (-EIO);
2423           i=xx_ReadSubQ();
2424           if (i<0) return (-EIO);
2425           DriveStruct[d].pos_audio_start=DriveStruct[d].SubQ_run_tot;
2426           i=xx_Seek(DriveStruct[d].pos_audio_start,1);
2427         }
2428       st=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_msf));
2429       if (st) return (st);
2430       memcpy_fromfs(&msf, (void *) arg, sizeof(struct cdrom_msf));
2431       /* values come as msf-bin */
2432       DriveStruct[d].pos_audio_start = (msf.cdmsf_min0<<16) |
2433                         (msf.cdmsf_sec0<<8) |
2434                         msf.cdmsf_frame0;
2435       DriveStruct[d].pos_audio_end = (msf.cdmsf_min1<<16) |
2436                       (msf.cdmsf_sec1<<8) |
2437                       msf.cdmsf_frame1;
2438       DPRINTF((DBG_IOX,"SBPCD: ioctl: CDROMPLAYMSF %08X %08X\n",
2439                                DriveStruct[d].pos_audio_start,DriveStruct[d].pos_audio_end));
2440       i=xx_PlayAudioMSF(DriveStruct[d].pos_audio_start,DriveStruct[d].pos_audio_end);
2441       DPRINTF((DBG_IOC,"SBPCD: ioctl: xx_PlayAudioMSF returns %d\n",i));
2442 #if 0
2443       if (i<0) return (-EIO);
2444 #endif 0
2445       DriveStruct[d].audio_state=audio_playing;
2446       return (0);
2447 
2448     case CDROMPLAYTRKIND: /* Play a track.  This currently ignores index. */
2449       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMPLAYTRKIND entered.\n"));
2450       if (DriveStruct[d].audio_state==audio_playing)
2451         {
2452           DPRINTF((DBG_IOX,"SBPCD: CDROMPLAYTRKIND: already audio_playing.\n"));
2453           return (0);
2454           return (-EINVAL);
2455         }
2456       st=verify_area(VERIFY_READ,(void *) arg,sizeof(struct cdrom_ti));
2457       if (st<0)
2458         {
2459           DPRINTF((DBG_IOX,"SBPCD: CDROMPLAYTRKIND: verify_area error.\n"));
2460           return (st);
2461         }
2462       memcpy_fromfs(&ti,(void *) arg,sizeof(struct cdrom_ti));
2463       DPRINTF((DBG_IOX,"SBPCD: ioctl: trk0: %d, ind0: %d, trk1:%d, ind1:%d\n",
2464              ti.cdti_trk0,ti.cdti_ind0,ti.cdti_trk1,ti.cdti_ind1));
2465       if (ti.cdti_trk0<DriveStruct[d].n_first_track) return (-EINVAL);
2466       if (ti.cdti_trk0>DriveStruct[d].n_last_track) return (-EINVAL);
2467       if (ti.cdti_trk1<ti.cdti_trk0) ti.cdti_trk1=ti.cdti_trk0;
2468       if (ti.cdti_trk1>DriveStruct[d].n_last_track) ti.cdti_trk1=DriveStruct[d].n_last_track;
2469       DriveStruct[d].pos_audio_start=DriveStruct[d].TocBuffer[ti.cdti_trk0].address;
2470       DriveStruct[d].pos_audio_end=DriveStruct[d].TocBuffer[ti.cdti_trk1+1].address;
2471       i=xx_PlayAudioMSF(DriveStruct[d].pos_audio_start,DriveStruct[d].pos_audio_end);
2472 #if 0
2473       if (i<0) return (-EIO);
2474 #endif 0
2475       DriveStruct[d].audio_state=audio_playing;
2476       return (0);
2477             
2478     case CDROMREADTOCHDR:        /* Read the table of contents header */
2479       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMREADTOCHDR entered.\n"));
2480       tochdr.cdth_trk0=DriveStruct[d].n_first_track;
2481       tochdr.cdth_trk1=DriveStruct[d].n_last_track;
2482       st=verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct cdrom_tochdr));
2483       if (st) return (st);
2484       memcpy_tofs((void *) arg, &tochdr, sizeof(struct cdrom_tochdr));
2485       return (0);
2486 
2487     case CDROMREADTOCENTRY:      /* Read an entry in the table of contents */
2488       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMREADTOCENTRY entered.\n"));
2489       st=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_tocentry));
2490       if (st) return (st);
2491       memcpy_fromfs(&tocentry, (void *) arg, sizeof(struct cdrom_tocentry));
2492       i=tocentry.cdte_track;
2493       if (i==CDROM_LEADOUT) i=DriveStruct[d].n_last_track+1;
2494       else if (i<DriveStruct[d].n_first_track||i>DriveStruct[d].n_last_track) return (-EINVAL);
2495       tocentry.cdte_adr=DriveStruct[d].TocBuffer[i].ctl_adr&0x0F;
2496       tocentry.cdte_ctrl=(DriveStruct[d].TocBuffer[i].ctl_adr>>4)&0x0F;
2497       tocentry.cdte_datamode=DriveStruct[d].TocBuffer[i].format;
2498       if (tocentry.cdte_format==CDROM_MSF) /* MSF-bin required */
2499         { tocentry.cdte_addr.msf.minute=(DriveStruct[d].TocBuffer[i].address>>16)&0x00FF;
2500           tocentry.cdte_addr.msf.second=(DriveStruct[d].TocBuffer[i].address>>8)&0x00FF;
2501           tocentry.cdte_addr.msf.frame=DriveStruct[d].TocBuffer[i].address&0x00FF;
2502         }
2503       else if (tocentry.cdte_format==CDROM_LBA) /* blk required */
2504         tocentry.cdte_addr.lba=msf2blk(DriveStruct[d].TocBuffer[i].address);
2505       else return (-EINVAL);
2506       st=verify_area(VERIFY_WRITE,(void *) arg, sizeof(struct cdrom_tocentry));
2507       if (st) return (st);
2508       memcpy_tofs((void *) arg, &tocentry, sizeof(struct cdrom_tocentry));
2509       return (0);
2510 
2511     case CDROMSTOP:      /* Spin down the drive */
2512       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMSTOP entered.\n"));
2513       i=xx_Pause_Resume(1);
2514       DriveStruct[d].audio_state=0;
2515       return (0);
2516 
2517     case CDROMSTART:  /* Spin up the drive */
2518       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMSTART entered.\n"));
2519       i=xx_SpinUp();
2520       DriveStruct[d].audio_state=0;
2521       return (0);
2522       
2523     case CDROMEJECT:
2524       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMEJECT entered.\n"));
2525       if (!new_drive) return (0);
2526 #if WORKMAN
2527       DriveStruct[d].CD_changed=0xFF;
2528       DriveStruct[d].diskstate_flags=0;
2529 #endif WORKMAN
2530       i=yy_SpinDown();
2531       if (i<0) return (-EIO);
2532       DriveStruct[d].audio_state=0;
2533       return (0);
2534       
2535     case CDROMVOLCTRL:   /* Volume control */
2536       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMVOLCTRL entered.\n"));
2537       st=verify_area(VERIFY_READ,(void *) arg,sizeof(volctrl));
2538       if (st) return (st);
2539       memcpy_fromfs(&volctrl,(char *) arg,sizeof(volctrl));
2540       DriveStruct[d].vol_chan0=0;
2541       DriveStruct[d].vol_ctrl0=volctrl.channel0;
2542       DriveStruct[d].vol_chan1=1;
2543       DriveStruct[d].vol_ctrl1=volctrl.channel1;
2544       i=xx_SetVolume();
2545       return (0);
2546 
2547     case CDROMSUBCHNL:   /* Get subchannel info */
2548       DPRINTF((DBG_IOS,"SBPCD: ioctl: CDROMSUBCHNL entered.\n"));
2549       if ((st_spinning)||(!subq_valid)) { i=xx_ReadSubQ();
2550                                           if (i<0) return (-EIO);
2551                                         }
2552       st=verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct cdrom_subchnl));
2553       if (st)   return (st);
2554       memcpy_fromfs(&SC, (void *) arg, sizeof(struct cdrom_subchnl));
2555       switch (DriveStruct[d].audio_state)
2556         {
2557         case audio_playing:
2558           SC.cdsc_audiostatus=CDROM_AUDIO_PLAY;
2559           break;
2560         case audio_pausing:
2561           SC.cdsc_audiostatus=CDROM_AUDIO_PAUSED;
2562           break;
2563         default:
2564           SC.cdsc_audiostatus=CDROM_AUDIO_NO_STATUS;
2565           break;
2566         }
2567       SC.cdsc_adr=DriveStruct[d].SubQ_ctl_adr;
2568       SC.cdsc_ctrl=DriveStruct[d].SubQ_ctl_adr>>4;
2569       SC.cdsc_trk=bcd2bin(DriveStruct[d].SubQ_trk);
2570       SC.cdsc_ind=bcd2bin(DriveStruct[d].SubQ_pnt_idx);
2571       if (SC.cdsc_format==CDROM_LBA)
2572         {
2573           SC.cdsc_absaddr.lba=msf2blk(DriveStruct[d].SubQ_run_tot);
2574           SC.cdsc_reladdr.lba=msf2blk(DriveStruct[d].SubQ_run_trk);
2575         }
2576       else /* not only if (SC.cdsc_format==CDROM_MSF) */
2577         {
2578           SC.cdsc_absaddr.msf.minute=(DriveStruct[d].SubQ_run_tot>>16)&0x00FF;
2579           SC.cdsc_absaddr.msf.second=(DriveStruct[d].SubQ_run_tot>>8)&0x00FF;
2580           SC.cdsc_absaddr.msf.frame=DriveStruct[d].SubQ_run_tot&0x00FF;
2581           SC.cdsc_reladdr.msf.minute=(DriveStruct[d].SubQ_run_trk>>16)&0x00FF;
2582           SC.cdsc_reladdr.msf.second=(DriveStruct[d].SubQ_run_trk>>8)&0x00FF;
2583           SC.cdsc_reladdr.msf.frame=DriveStruct[d].SubQ_run_trk&0x00FF;
2584         }
2585       memcpy_tofs((void *) arg, &SC, sizeof(struct cdrom_subchnl));
2586       DPRINTF((DBG_IOS,"SBPCD: CDROMSUBCHNL: %1X %02X %08X %08X %02X %02X %06X %06X\n",
2587                SC.cdsc_format,SC.cdsc_audiostatus,
2588                SC.cdsc_adr,SC.cdsc_ctrl,
2589                SC.cdsc_trk,SC.cdsc_ind,
2590                SC.cdsc_absaddr,SC.cdsc_reladdr));
2591       return (0);
2592 
2593     case CDROMREADMODE1:
2594       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMREADMODE1 requested.\n"));
2595       xx_ModeSelect(CD_FRAMESIZE);
2596       xx_ModeSense();
2597       DriveStruct[d].mode=READ_M1;
2598       return (0);
2599 
2600     case CDROMREADMODE2: /* not useable at the moment */
2601       DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMREADMODE2 requested.\n"));
2602       xx_ModeSelect(CD_FRAMESIZE_XA);
2603       xx_ModeSense();
2604       DriveStruct[d].mode=READ_M2;
2605       return (0);
2606 
2607     case CDROMREADAUDIO:
2608       { /* start of CDROMREADAUDIO */
2609         int i=0, j=0, frame, block;
2610         u_int try=0;
2611         u_long timeout;
2612         u_char *p;
2613         u_int data_tries = 0;
2614         u_int data_waits = 0;
2615         u_int data_retrying = 0;
2616         int status_tries;
2617         int error_flag;
2618 
2619         DPRINTF((DBG_IOC,"SBPCD: ioctl: CDROMREADAUDIO requested.\n"));
2620 #if 0
2621         if (!new_drive) return (-EINVAL);
2622 #endif
2623         if (DriveStruct[d].aud_buf==NULL) return (-EINVAL);
2624         i=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_read_audio));
2625         if (i) return (i);
2626         memcpy_fromfs(&read_audio, (void *) arg, sizeof(struct cdrom_read_audio));
2627         if (read_audio.nframes>SBP_BUFFER_AUDIO_FRAMES) return (-EINVAL);
2628         i=verify_area(VERIFY_WRITE, read_audio.buf,
2629                       read_audio.nframes*CD_FRAMESIZE_RAW);
2630         if (i) return (i);
2631 
2632         if (read_audio.addr_format==CDROM_MSF) /* MSF-bin specification of where to start */
2633           block=msf2lba(&read_audio.addr.msf.minute);
2634         else if (read_audio.addr_format==CDROM_LBA) /* lba specification of where to start */
2635           block=read_audio.addr.lba;
2636         else return (-EINVAL);
2637         DPRINTF((DBG_AUD,"SBPCD: read_audio: lba: %d, msf: %06X\n",
2638                  block, blk2msf(block)));
2639         DPRINTF((DBG_AUD,"SBPCD: read_audio: before xx_ReadStatus.\n"));
2640         while (busy_data) sbp_sleep(10); /* wait a bit */
2641         busy_audio=1;
2642         error_flag=0;
2643         for (data_tries=5; data_tries>0; data_tries--)
2644           {
2645             DPRINTF((DBG_AUD,"SBPCD: data_tries=%d ...\n", data_tries));
2646             DriveStruct[d].mode=READ_AU;
2647             xx_ModeSelect(CD_FRAMESIZE_RAW);
2648             xx_ModeSense();
2649             for (status_tries=3; status_tries > 0; status_tries--)
2650               {
2651                 flags_cmd_out |= f_respo3;
2652                 xx_ReadStatus();
2653                 if (sbp_status() != 0) break;
2654                 sbp_sleep(1);    /* wait a bit, try again */
2655               }
2656             if (status_tries == 0)
2657               {
2658                 DPRINTF((DBG_AUD,"SBPCD: read_audio: sbp_status: failed after 3 tries.\n"));
2659                 continue;
2660               }
2661             DPRINTF((DBG_AUD,"SBPCD: read_audio: sbp_status: ok.\n"));
2662             
2663             flags_cmd_out = f_putcmd | f_respo2 | f_ResponseStatus | f_obey_p_check;
2664             if (!new_drive)
2665               {
2666                 flags_cmd_out |= f_lopsta | f_getsta | f_bit1;
2667                 cmd_type=READ_M2;
2668                 drvcmd[0]=0x03;   /* "read XA frames" command for old drives */
2669                 drvcmd[1]=(block>>16)&0x000000ff;
2670                 drvcmd[2]=(block>>8)&0x000000ff;
2671                 drvcmd[3]=block&0x000000ff;
2672                 drvcmd[4]=0;
2673                 drvcmd[5]=read_audio.nframes;   /* # of frames */
2674                 drvcmd[6]=0;
2675               }
2676             else /* if new_drive */
2677               {
2678                 drvcmd[0]=0x10;              /* "read frames" command for new drives */
2679                 lba2msf(block,&drvcmd[1]); /* msf-bin format required */
2680                 drvcmd[4]=0;
2681                 drvcmd[5]=0;
2682                 drvcmd[6]=1;   /* # of frames */
2683               }
2684             DPRINTF((DBG_AUD,"SBPCD: read_audio: before giving \"read\" command.\n"));
2685             for (i=0;i<7;i++) OUT(CDo_command,drvcmd[i]);
2686             sbp_sleep(0);
2687             DPRINTF((DBG_AUD,"SBPCD: read_audio: after giving \"read\" command.\n"));
2688             for (frame=1;frame<2 && !error_flag; frame++)
2689               {
2690                 try=maxtim_data;
2691                 for (timeout=jiffies+900; ; )
2692                   {
2693                     for ( ; try!=0;try--)
2694                       {
2695                         j=inb(CDi_status);
2696                         if (!(j&s_not_data_ready)) break;
2697                         if (!(j&s_not_result_ready)) break;
2698                         if (!new_drive) if (j&s_attention) break;
2699                       }
2700                     if (try != 0 || timeout <= jiffies) break;
2701                     if (data_retrying == 0) data_waits++;
2702                     data_retrying = 1;
2703                     sbp_sleep(1);
2704                     try = 1;
2705                   }
2706                 if (try==0)
2707                   {
2708                     DPRINTF((DBG_INF,"SBPCD: read_audio: sbp_data: CDi_status timeout.\n"));
2709                     error_flag++;
2710                     break;
2711                   }
2712                 DPRINTF((DBG_AUD,"SBPCD: read_audio: sbp_data: CDi_status ok.\n"));
2713                 if (j&s_not_data_ready)
2714                   {
2715                     printk("SBPCD: read_audio: sbp_data: DATA_READY timeout.\n");
2716                     error_flag++;
2717                     break;
2718                   }
2719                 DPRINTF((DBG_AUD,"SBPCD: read_audio: before reading data.\n"));
2720                 error_flag=0;
2721                 p = DriveStruct[d].aud_buf;
2722                 if (sbpro_type==1) OUT(CDo_sel_d_i,0x01);
2723 #if 0
2724                 cli();
2725 #endif
2726                 READ_DATA(CDi_data, p, read_audio.nframes*CD_FRAMESIZE_RAW);
2727 #if 0
2728                 sti();
2729 #endif
2730                 if (sbpro_type==1) OUT(CDo_sel_d_i,0x00);
2731                 data_retrying = 0;
2732               }
2733             DPRINTF((DBG_AUD,"SBPCD: read_audio: after reading data.\n"));
2734             if (error_flag)    /* must have been spurious D_RDY or (ATTN&&!D_RDY) */
2735               {
2736                 DPRINTF((DBG_AUD,"SBPCD: read_audio: read aborted by drive\n"));
2737 #if 0000
2738                 i=DriveReset();                /* ugly fix to prevent a hang */
2739 #endif 0000
2740                 continue;
2741               }
2742             if (!new_drive)
2743               {
2744                 i=maxtim_data;
2745                 for (timeout=jiffies+900; timeout > jiffies; timeout--)
2746                   {
2747                     for ( ;i!=0;i--)
2748                       {
2749                         j=inb(CDi_status);
2750                         if (!(j&s_not_data_ready)) break;
2751                         if (!(j&s_not_result_ready)) break;
2752                         if (j&s_attention) break;
2753                       }
2754                     if (i != 0 || timeout <= jiffies) break;
2755                     sbp_sleep(0);
2756                     i = 1;
2757                   }
2758                 if (i==0) { DPRINTF((DBG_AUD,"SBPCD: read_audio: STATUS TIMEOUT AFTER READ")); }
2759                 if (!(j&s_attention))
2760                   {
2761                     DPRINTF((DBG_AUD,"SBPCD: read_audio: sbp_data: timeout waiting DRV_ATTN - retrying\n"));
2762                     i=DriveReset();  /* ugly fix to prevent a hang */
2763                   continue;
2764                   }
2765               }
2766             do
2767               {
2768                 if (!new_drive) xx_ReadStatus();
2769                 i=ResponseStatus();  /* builds status_byte, returns orig. status (old) or faked p_success_old (new) */
2770                 if (i<0) { DPRINTF((DBG_AUD,
2771                                     "SBPCD: read_audio: xx_ReadStatus error after read: %02X\n",
2772                                     DriveStruct[d].status_byte));
2773                            continue; /* FIXME */
2774                          }
2775               }
2776             while ((!new_drive)&&(!st_check)&&(!(i&p_success_old)));
2777             if (st_check)
2778               {
2779                 i=xx_ReadError();
2780                 DPRINTF((DBG_AUD,"SBPCD: read_audio: xx_ReadError was necessary after read: %02X\n",i));
2781                 continue;
2782               }
2783             memcpy_tofs((u_char *) read_audio.buf,
2784                         (u_char *) DriveStruct[d].aud_buf,
2785                         read_audio.nframes*CD_FRAMESIZE_RAW);
2786             DPRINTF((DBG_AUD,"SBPCD: read_audio: memcpy_tofs done.\n"));
2787             break;
2788           }
2789         xx_ModeSelect(CD_FRAMESIZE);
2790         xx_ModeSense();
2791         DriveStruct[d].mode=READ_M1;
2792         busy_audio=0;
2793         if (data_tries == 0)
2794           {
2795             DPRINTF((DBG_AUD,"SBPCD: read_audio: failed after 5 tries.\n"));
2796             return (-8);
2797           }
2798         DPRINTF((DBG_AUD,"SBPCD: read_audio: successful return.\n"));
2799         return (0);
2800       } /* end of CDROMREADAUDIO */
2801 
2802     case BLKRASET:
2803       if(!suser())  return -EACCES;
2804       if(!inode->i_rdev) return -EINVAL;
2805       if(arg > 0xff) return -EINVAL;
2806       read_ahead[MAJOR(inode->i_rdev)] = arg;
2807       return (0);
2808 
2809     default:
2810       DPRINTF((DBG_IOC,"SBPCD: ioctl: unknown function request %04X\n", cmd));
2811       return (-EINVAL);
2812     } /* end switch(cmd) */
2813 }
2814 /*==========================================================================*/
2815 /*
2816  *  Take care of the different block sizes between cdrom and Linux.
2817  *  When Linux gets variable block sizes this will probably go away.
2818  */
2819 static void sbp_transfer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2820 {
2821   long offs;
2822   
2823   while ( (CURRENT->nr_sectors > 0) &&
2824           (CURRENT->sector/4 >= DriveStruct[d].sbp_first_frame) &&
2825           (CURRENT->sector/4 <= DriveStruct[d].sbp_last_frame) )
2826     {
2827       offs = (CURRENT->sector - DriveStruct[d].sbp_first_frame * 4) * 512;
2828       memcpy(CURRENT->buffer, DriveStruct[d].sbp_buf + offs, 512);
2829       CURRENT->nr_sectors--;
2830       CURRENT->sector++;
2831       CURRENT->buffer += 512;
2832     }
2833 }
2834 /*==========================================================================*/
2835 /*
2836  *  I/O request routine, called from Linux kernel.
2837  */
2838 static void DO_SBPCD_REQUEST(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2839 {
2840   u_int block;
2841   int dev;
2842   u_int nsect;
2843   int i, status_tries, data_tries;
2844   
2845 request_loop:
2846 
2847   sti();
2848 
2849   if ((CURRENT==NULL)||(CURRENT->dev<0)) goto done;
2850   if (CURRENT -> sector == -1) goto done;
2851 
2852   dev = MINOR(CURRENT->dev);
2853   if ( (dev<0) || (dev>=NR_SBPCD) )
2854     {
2855       printk("SBPCD: do_request: bad device: %d\n", dev);
2856       goto done;
2857     }
2858   switch_drive(dev);
2859 
2860   INIT_REQUEST;
2861   block = CURRENT->sector; /* always numbered as 512-byte-pieces */
2862   nsect = CURRENT->nr_sectors; /* always counted as 512-byte-pieces */
2863   if (CURRENT->cmd != READ)
2864     {
2865       printk("SBPCD: bad cmd %d\n", CURRENT->cmd);
2866       end_request(0);
2867       goto request_loop;
2868     }
2869 
2870   DPRINTF((DBG_BSZ,"SBPCD: read sector %d (%d sectors)\n", block, nsect));
2871   DPRINTF((DBG_MUL,"SBPCD: read LBA %d\n", block/4));
2872 
2873   sbp_transfer();
2874   /* if we satisfied the request from the buffer, we're done. */
2875   if (CURRENT->nr_sectors == 0)
2876     {
2877       end_request(1);
2878       goto request_loop;
2879     }
2880 
2881   i=prepare(0,0); /* at moment not really a hassle check, but ... */
2882   if (i!=0)
2883     DPRINTF((DBG_INF,"SBPCD: \"prepare\" tells error %d -- ignored\n", i));
2884 
2885   while (busy_audio) sbp_sleep(100); /* wait a bit */
2886   busy_data=1;
2887 
2888   if (!st_spinning) xx_SpinUp();
2889 
2890 #ifdef XA_TEST1
2891   if ((!new_drive) && (DriveStruct[d].xa_byte==0x20)) /* XA disk with old drive */
2892       {
2893         xx_ModeSelect(CD_FRAMESIZE_XA);
2894         xx_ModeSense();
2895       }
2896 #endif XA_TEST1
2897 
2898   for (data_tries=3; data_tries > 0; data_tries--)
2899     {
2900       for (status_tries=3; status_tries > 0; status_tries--)
2901         {
2902           flags_cmd_out |= f_respo3;
2903           xx_ReadStatus();
2904           if (sbp_status() != 0) break;
2905           sbp_sleep(1);    /* wait a bit, try again */
2906         }
2907       if (status_tries == 0)
2908         {
2909           DPRINTF((DBG_INF,"SBPCD: sbp_status: failed after 3 tries\n"));
2910           break;
2911         }
2912 
2913       sbp_read_cmd();
2914       sbp_sleep(0);
2915       if (sbp_data() != 0)
2916         {
2917           end_request(1);
2918           goto request_loop;
2919         }
2920     }
2921   
2922   end_request(0);
2923   sbp_sleep(10);    /* wait a bit, try again */
2924   goto request_loop;
2925 
2926 done:
2927   busy_data=0;
2928   return;
2929 }
2930 /*==========================================================================*/
2931 /*
2932  *  build and send the READ command.
2933  *  Maybe it would be better to "set mode1" before ...
2934  */
2935 static void sbp_read_cmd(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2936 {
2937   int i;
2938   int block;
2939 
2940   DriveStruct[d].sbp_first_frame=DriveStruct[d].sbp_last_frame=-1;      /* purge buffer */
2941   block=CURRENT->sector/4;
2942 
2943   if (new_drive)
2944     {
2945 #if MANY_SESSION
2946       DPRINTF((DBG_MUL,"SBPCD: read MSF %08X\n", blk2msf(block)));
2947       if ( (DriveStruct[d].f_multisession) && (multisession_valid) )
2948         {
2949           DPRINTF((DBG_MUL,"SBPCD: ManySession: use %08X for %08X (msf)\n",
2950                          blk2msf(DriveStruct[d].lba_multi+block),
2951                          blk2msf(block)));
2952           block=DriveStruct[d].lba_multi+block;
2953         }
2954 #else
2955       if ( (block==16) && (DriveStruct[d].f_multisession) && (multisession_valid) )
2956         {
2957           DPRINTF((DBG_MUL,"SBPCD: MultiSession: use %08X for %08X (msf)\n",
2958                          blk2msf(DriveStruct[d].lba_multi+16),
2959                          blk2msf(block)));
2960           block=DriveStruct[d].lba_multi+16;
2961         }
2962 #endif MANY_SESSION
2963     }
2964 
2965   if (block+SBP_BUFFER_FRAMES <= DriveStruct[d].CDsize_frm)
2966     DriveStruct[d].sbp_read_frames = SBP_BUFFER_FRAMES;
2967   else
2968     {
2969       DriveStruct[d].sbp_read_frames=DriveStruct[d].CDsize_frm-block;
2970                                       /* avoid reading past end of data */
2971       if (DriveStruct[d].sbp_read_frames < 1)
2972         {
2973           DPRINTF((DBG_INF,"SBPCD: requested frame %d, CD size %d ???\n",
2974                         block, DriveStruct[d].CDsize_frm));
2975           DriveStruct[d].sbp_read_frames=1;
2976         }
2977     }
2978   DriveStruct[d].sbp_current = 0;
2979 
2980   flags_cmd_out = f_putcmd |
2981                   f_respo2 |
2982                   f_ResponseStatus |
2983                   f_obey_p_check;
2984 
2985   if (!new_drive)
2986     {
2987       flags_cmd_out |= f_lopsta | f_getsta | f_bit1;
2988       if (DriveStruct[d].xa_byte==0x20)
2989         {
2990           cmd_type=READ_M2;
2991           drvcmd[0]=0x03;   /* "read XA frames" command for old drives */
2992           drvcmd[1]=(block>>16)&0x000000ff;
2993           drvcmd[2]=(block>>8)&0x000000ff;
2994           drvcmd[3]=block&0x000000ff;
2995           drvcmd[4]=0;
2996           drvcmd[5]=DriveStruct[d].sbp_read_frames;
2997           drvcmd[6]=0;
2998         }
2999       else
3000         {
3001           drvcmd[0]=0x02;        /* "read frames" command for old drives */
3002           
3003           if (DriveStruct[d].drv_type>=drv_201)
3004             {
3005               lba2msf(block,&drvcmd[1]); /* msf-bcd format required */
3006               bin2bcdx(&drvcmd[1]);
3007               bin2bcdx(&drvcmd[2]);
3008               bin2bcdx(&drvcmd[3]);
3009             }
3010           else
3011             {
3012               drvcmd[1]=(block>>16)&0x000000ff;
3013               drvcmd[2]=(block>>8)&0x000000ff;
3014               drvcmd[3]=block&0x000000ff;
3015             }
3016           drvcmd[4]=0;
3017           drvcmd[5]=DriveStruct[d].sbp_read_frames;
3018           drvcmd[6]=(DriveStruct[d].drv_type<drv_201)?0:2; /* flag "lba or msf-bcd format" */
3019         }
3020     }
3021   else /* if new_drive */
3022     {
3023       drvcmd[0]=0x10;              /* "read frames" command for new drives */
3024       lba2msf(block,&drvcmd[1]); /* msf-bin format required */
3025       drvcmd[4]=0;
3026       drvcmd[5]=0;
3027       drvcmd[6]=DriveStruct[d].sbp_read_frames;
3028     }
3029   SBPCD_CLI;
3030   for (i=0;i<7;i++) OUT(CDo_command,drvcmd[i]);
3031   SBPCD_STI;
3032 
3033   return;
3034 }
3035 /*==========================================================================*/
3036 /*
3037  *  Check the completion of the read-data command.  On success, read
3038  *  the SBP_BUFFER_FRAMES * 2048 bytes of data from the disk into buffer.
3039  */
3040 static int sbp_data(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3041 {
3042   int i=0, j=0, frame;
3043   u_int try=0;
3044   u_long timeout;
3045   u_char *p;
3046   u_int data_tries = 0;
3047   u_int data_waits = 0;
3048   u_int data_retrying = 0;
3049   int error_flag;
3050   int xa_count;
3051   error_flag=0;
3052 
3053   for (frame=DriveStruct[d].sbp_current;frame<DriveStruct[d].sbp_read_frames&&!error_flag; frame++)
3054     {
3055       SBPCD_CLI;
3056       try=maxtim_data;
3057 #if LONG_TIMING
3058       for (timeout=jiffies+900; ; )
3059 #else
3060       for (timeout=jiffies+100; ; )
3061 #endif
3062         {
3063           for ( ; try!=0;try--)
3064             {
3065               j=inb(CDi_status);
3066               if (!(j&s_not_data_ready)) break;
3067               if (!(j&s_not_result_ready)) break;
3068               if (!new_drive) if (j&s_attention) break;
3069             }
3070           if (try != 0 || timeout <= jiffies) break;
3071           if (data_retrying == 0) data_waits++;
3072           data_retrying = 1;
3073           sbp_sleep(1);
3074           try = 1;
3075         }
3076       if (try==0)
3077         {
3078           DPRINTF((DBG_INF,"SBPCD: sbp_data: CDi_status timeout.\n"));
3079           error_flag++;
3080           break;
3081         }
3082 
3083       if (j&s_not_data_ready)
3084         {
3085           if ((DriveStruct[d].ored_ctl_adr&0x40)==0)
3086             printk("SBPCD: CD contains no data tracks.\n");
3087           else printk("SBPCD: sbp_data: DATA_READY timeout.\n");
3088           error_flag++;
3089           break;
3090         }
3091 
3092       SBPCD_STI;
3093       error_flag=0;
3094       p = DriveStruct[d].sbp_buf + frame *  CD_FRAMESIZE;
3095 
3096       if (sbpro_type==1) OUT(CDo_sel_d_i,0x01);
3097       if (cmd_type==READ_M2) READ_DATA(CDi_data, xa_head_buf, CD_XA_HEAD);
3098       READ_DATA(CDi_data, p, CD_FRAMESIZE);
3099       if (cmd_type==READ_M2) READ_DATA(CDi_data, xa_tail_buf, CD_XA_TAIL);
3100       if (sbpro_type==1) OUT(CDo_sel_d_i,0x00);
3101       DriveStruct[d].sbp_current++;
3102       if (cmd_type==READ_M2)
3103         {
3104           DPRINTF((DBG_XA,"SBPCD: xa_head:"));
3105           for (xa_count=0;xa_count<CD_XA_HEAD;xa_count++)
3106             DPRINTF((DBG_XA," %02X", xa_head_buf[xa_count]));
3107           DPRINTF((DBG_XA,"\n"));
3108         }
3109       data_tries++;
3110       data_retrying = 0;
3111       if (data_tries >= 1000)
3112         {
3113           DPRINTF((DBG_INF,"SBPCD: info: %d waits in %d frames.\n",
3114                         data_waits, data_tries));
3115           data_waits = data_tries = 0;
3116         }
3117     }
3118   SBPCD_STI;
3119   
3120   if (error_flag)    /* must have been spurious D_RDY or (ATTN&&!D_RDY) */
3121     {
3122       DPRINTF((DBG_INF,"SBPCD: read aborted by drive\n"));
3123       i=DriveReset();                /* ugly fix to prevent a hang */
3124       return (0);
3125     }
3126 
3127   if (!new_drive)
3128     {
3129       SBPCD_CLI;
3130       i=maxtim_data;
3131       for (timeout=jiffies+100; timeout > jiffies; timeout--)
3132         {
3133           for ( ;i!=0;i--)
3134             {
3135               j=inb(CDi_status);
3136               if (!(j&s_not_data_ready)) break;
3137               if (!(j&s_not_result_ready)) break;
3138               if (j&s_attention) break;
3139             }
3140           if (i != 0 || timeout <= jiffies) break;
3141           sbp_sleep(0);
3142           i = 1;
3143         }
3144       if (i==0) { DPRINTF((DBG_INF,"SBPCD: STATUS TIMEOUT AFTER READ")); }
3145       if (!(j&s_attention))
3146         {
3147           DPRINTF((DBG_INF,"SBPCD: sbp_data: timeout waiting DRV_ATTN - retrying\n"));
3148           i=DriveReset();  /* ugly fix to prevent a hang */
3149           SBPCD_STI;
3150           return (0);
3151         }
3152       SBPCD_STI;
3153     }
3154 
3155   do
3156     {
3157       if (!new_drive) xx_ReadStatus();
3158       i=ResponseStatus();  /* builds status_byte, returns orig. status (old) or faked p_success_old (new) */
3159       if (i<0) { DPRINTF((DBG_INF,"SBPCD: xx_ReadStatus error after read: %02X\n",
3160                                DriveStruct[d].status_byte));
3161                  return (0);
3162                }
3163     }
3164   while ((!new_drive)&&(!st_check)&&(!(i&p_success_old)));
3165   if (st_check)
3166     {
3167       i=xx_ReadError();
3168       DPRINTF((DBG_INF,"SBPCD: xx_ReadError was necessary after read: %02X\n",i));
3169       return (0);
3170     }
3171 
3172   DriveStruct[d].sbp_first_frame = CURRENT -> sector / 4;
3173   DriveStruct[d].sbp_last_frame = DriveStruct[d].sbp_first_frame + DriveStruct[d].sbp_read_frames - 1;
3174   sbp_transfer();
3175   return (1);
3176 }
3177 /*==========================================================================*/
3178 /*==========================================================================*/
3179 /*
3180  *  Open the device special file.  Check that a disk is in. Read TOC.
3181  */
3182 static int sbpcd_open(struct inode *ip, struct file *fp)
     /* [previous][next][first][last][top][bottom][index][help] */
3183 {
3184   int i;
3185 
3186   if (ndrives==0) return (-ENXIO);             /* no hardware */
3187 
3188   i = MINOR(ip->i_rdev);
3189   if ( (i<0) || (i>=NR_SBPCD) )
3190     {
3191       printk("SBPCD: open: bad device: %d\n", i);
3192       return (-ENODEV);             /* no such drive */
3193     }
3194   switch_drive(i);
3195 
3196   if (!st_spinning) xx_SpinUp();
3197 
3198   flags_cmd_out |= f_respo2;
3199   xx_ReadStatus();                         /* command: give 1-byte status */
3200   i=ResponseStatus();
3201   if (i<0)
3202     {
3203       DPRINTF((DBG_INF,"SBPCD: sbpcd_open: xx_ReadStatus timed out\n"));
3204       return (-EIO);                  /* drive doesn't respond */
3205     }
3206   DPRINTF((DBG_STA,"SBPCD: sbpcd_open: status %02X\n", DriveStruct[d].status_byte));
3207   if (!st_door_closed||!st_caddy_in)
3208     {
3209       printk("SBPCD: sbpcd_open: no disk in drive\n");
3210       return (-EIO);
3211     }
3212 
3213 /*
3214  * try to keep an "open" counter here and lock the door if 0->1.
3215  */
3216   DPRINTF((DBG_LCK,"SBPCD: open_count: %d -> %d\n",
3217            DriveStruct[d].open_count,DriveStruct[d].open_count+1));
3218   if (++DriveStruct[d].open_count==1)
3219     {
3220       do
3221         i=yy_LockDoor(1);
3222       while (i!=0);
3223     }
3224   if (!st_spinning) xx_SpinUp();
3225 
3226   i=DiskInfo();
3227   if ((DriveStruct[d].ored_ctl_adr&0x40)==0)
3228     DPRINTF((DBG_INF,"SBPCD: CD contains no data tracks.\n"));
3229   return (0);
3230 }
3231 /*==========================================================================*/
3232 /*
3233  *  On close, we flush all sbp blocks from the buffer cache.
3234  */
3235 static void sbpcd_release(struct inode * ip, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
3236 {
3237   int i;
3238 
3239   i = MINOR(ip->i_rdev);
3240   if ( (i<0) || (i>=NR_SBPCD) ) 
3241     {
3242       printk("SBPCD: release: bad device: %d\n", i);
3243       return;
3244     }
3245   switch_drive(i);
3246 
3247   DriveStruct[d].sbp_first_frame=DriveStruct[d].sbp_last_frame=-1;
3248   fsync_dev(ip->i_rdev);                   /* nonsense if read only device? */
3249   invalidate_buffers(ip->i_rdev);
3250   DriveStruct[d].diskstate_flags &= ~cd_size_bit;
3251 
3252 /*
3253  * try to keep an "open" counter here and unlock the door if 1->0.
3254  */
3255   DPRINTF((DBG_LCK,"SBPCD: open_count: %d -> %d\n",
3256            DriveStruct[d].open_count,DriveStruct[d].open_count-1));
3257   if (--DriveStruct[d].open_count==0) 
3258     {
3259       do
3260         i=yy_LockDoor(0);
3261       while (i!=0);
3262     }
3263 }
3264 /*==========================================================================*/
3265 /*
3266  *
3267  */
3268 static struct file_operations sbpcd_fops =
3269 {
3270   NULL,                   /* lseek - default */
3271   block_read,             /* read - general block-dev read */
3272   block_write,            /* write - general block-dev write */
3273   NULL,                   /* readdir - bad */
3274   NULL,                   /* select */
3275   sbpcd_ioctl,            /* ioctl */
3276   NULL,                   /* mmap */
3277   sbpcd_open,             /* open */
3278   sbpcd_release,          /* release */
3279   NULL,                   /* fsync */
3280   NULL,                   /* fasync */
3281   check_media_change,     /* media_change */
3282   NULL                    /* revalidate */
3283 };
3284 /*==========================================================================*/
3285 /*
3286  * accept "kernel command line" parameters 
3287  * (suggested by Peter MacDonald with SLS 1.03)
3288  *
3289  * This is only implemented for the first controller. Should be enough to
3290  * allow installing with a "strange" distribution kernel.
3291  *
3292  * use: tell LILO:
3293  *                 sbpcd=0x230,SoundBlaster
3294  *             or
3295  *                 sbpcd=0x300,LaserMate
3296  *             or
3297  *                 sbpcd=0x330,SPEA
3298  *
3299  * (upper/lower case sensitive here!!!).
3300  *
3301  * the address value has to be the TRUE CDROM PORT ADDRESS -
3302  * not the soundcard base address.
3303  *
3304  */
3305 #if (SBPCD_ISSUE-1)
3306 static
3307 #endif
3308 void SBPCD_SETUP(char *s, int *p)
     /* [previous][next][first][last][top][bottom][index][help] */
3309 {
3310   DPRINTF((DBG_INI,"SBPCD: sbpcd_setup called with %04X,%s\n",p[1], s));
3311   sbpro_type=0;
3312   if (!strcmp(s,str_sb)) sbpro_type=1;
3313   else if (!strcmp(s,str_sp)) sbpro_type=2;
3314   if (p[0]>0) sbpcd_ioaddr=p[1];
3315 
3316   CDo_command=sbpcd_ioaddr;
3317   CDi_info=sbpcd_ioaddr;
3318   CDi_status=sbpcd_ioaddr+1;
3319   CDo_reset=sbpcd_ioaddr+2;
3320   CDo_enable=sbpcd_ioaddr+3; 
3321   if (sbpro_type==1)
3322     {
3323       MIXER_addr=sbpcd_ioaddr-0x10+0x04;
3324       MIXER_data=sbpcd_ioaddr-0x10+0x05;
3325       CDo_sel_d_i=sbpcd_ioaddr+1;
3326       CDi_data=sbpcd_ioaddr;
3327     }
3328   else CDi_data=sbpcd_ioaddr+2;
3329 }
3330 /*==========================================================================*/
3331 /*
3332  * Sequoia S-1000 CD-ROM Interface Configuration
3333  * as used within SPEA Media FX card
3334  * The SPEA soundcard has to get jumpered for 
3335  *     -> interface type "Matsushita/Panasonic" (not Sony or Mitsumi)
3336  *     -> I/O base address (0x320, 0x330, 0x340, 0x350)
3337  */
3338 static int config_spea(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3339 {
3340   int n_ports=0x10; /* 2:0x00, 8:0x10, 16:0x20, 32:0x30 */
3341   int irq_number=0; /* 2:0x01, 7:0x03, 12:0x05, 15:0x07, OFF:0x00 */
3342   int dma_channel=0; /* 0:0x08, 1:0x18, 3:0x38, 5:0x58, 6:0x68, 7:0x78, OFF: 0x00 */
3343   int dack_polarity=0; /* L:0x00, H:0x80 */
3344   int drq_polarity=0x40; /* L:0x00, H:0x40 */
3345 
3346   int i;
3347 
3348 #define SPEA_REG_1 sbpcd_ioaddr+4
3349 #define SPEA_REG_2 sbpcd_ioaddr+5
3350 
3351   OUT(SPEA_REG_1,0xFF);
3352   i=inb(SPEA_REG_1);
3353   if (i!=0x0F)
3354     {
3355       DPRINTF((DBG_SEQ,"SBPCD: no SPEA interface at %04X present.\n",
3356                sbpcd_ioaddr));
3357       return (-1); /* no interface found */
3358     }
3359   OUT(SPEA_REG_1,0x04);
3360   OUT(SPEA_REG_2,0xC0);
3361 
3362   OUT(SPEA_REG_1,0x05);
3363   OUT(SPEA_REG_2,0x10|drq_polarity|dack_polarity);
3364 
3365 #if 1
3366 #define SPEA_PATTERN 0x80
3367 #else
3368 #define SPEA_PATTERN 0x00
3369 #endif
3370   OUT(SPEA_REG_1,0x06);
3371   OUT(SPEA_REG_2,dma_channel|irq_number|SPEA_PATTERN);
3372   OUT(SPEA_REG_2,dma_channel|irq_number|SPEA_PATTERN);
3373 
3374   OUT(SPEA_REG_1,0x09);
3375   i=(inb(SPEA_REG_2)&0xCF)|n_ports;
3376   OUT(SPEA_REG_2,i);
3377 
3378   sbpro_type = 0; /* acts like a LaserMate interface now */
3379   DPRINTF((DBG_SEQ,"SBPCD: found SPEA interface at %04X.\n",
3380            sbpcd_ioaddr));
3381   return (0);
3382 }
3383 /*==========================================================================*/
3384 /*
3385  *  Test for presence of drive and initialize it.  Called at boot time.
3386  */
3387 unsigned long SBPCD_INIT(u_long mem_start, u_long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
3388 {
3389   int i=0, j=0;
3390   int addr[2]={1, CDROM_PORT};
3391   int port_index;
3392    
3393   sti(); /* necessary, has consequences for other drivers' init routines */
3394 
3395   DPRINTF((DBG_INF,"SBPCD version %s\n", VERSION));
3396 
3397   DPRINTF((DBG_INF,"SBPCD: Looking for a SoundBlaster/Matsushita CD-ROM drive\n"));
3398   DPRINTF((DBG_WRN,"SBPCD: \n"));
3399   DPRINTF((DBG_WRN,"SBPCD: = = = = = = = = = = W A R N I N G = = = = = = = = = =\n"));
3400   DPRINTF((DBG_WRN,"SBPCD: Auto-Probing can cause a hang (f.e. touching an ethernet card).\n"));
3401   DPRINTF((DBG_WRN,"SBPCD: If that happens, you have to reboot and use the\n"));
3402   DPRINTF((DBG_WRN,"SBPCD: LILO (kernel) command line feature like:\n"));
3403   DPRINTF((DBG_WRN,"SBPCD: \n"));
3404   DPRINTF((DBG_WRN,"SBPCD:    LILO boot: linux sbpcd=0x230,SoundBlaster\n"));
3405   DPRINTF((DBG_WRN,"SBPCD: or like:\n"));
3406   DPRINTF((DBG_WRN,"SBPCD:    LILO boot: linux sbpcd=0x300,LaserMate\n"));
3407   DPRINTF((DBG_WRN,"SBPCD: or like:\n"));
3408   DPRINTF((DBG_WRN,"SBPCD:    LILO boot: linux sbpcd=0x330,SPEA\n"));
3409   DPRINTF((DBG_WRN,"SBPCD: \n"));
3410   DPRINTF((DBG_WRN,"SBPCD: with your REAL address.\n"));
3411   DPRINTF((DBG_WRN,"SBPCD: = = = = = = = = = = END of WARNING = = = = = = = = = =\n"));
3412   DPRINTF((DBG_WRN,"SBPCD: \n"));
3413 
3414   autoprobe[0]=sbpcd_ioaddr; /* possibly changed by kernel command line */
3415   autoprobe[1]=sbpro_type; /* possibly changed by kernel command line */
3416 
3417   for (port_index=0;port_index<NUM_AUTOPROBE;port_index+=2)
3418     {
3419       addr[1]=autoprobe[port_index];
3420       if (check_region(addr[1],4)) continue;
3421       DPRINTF((DBG_INI,"SBPCD: check_region: free.\n"));
3422       if (autoprobe[port_index+1]==0) type=str_lm;
3423       else if (autoprobe[port_index+1]==1) type=str_sb;
3424       else type=str_sp;
3425       SBPCD_SETUP(type, addr);
3426       DPRINTF((DBG_INF,"SBPCD: Trying to detect a %s CD-ROM drive at 0x%X.\n",
3427                      type, CDo_command));
3428 
3429       DPRINTF((DBG_INF,"SBPCD: - "));
3430       if (autoprobe[port_index+1]==2)
3431         {
3432           i=config_spea();
3433           if (i<0)
3434             {
3435               DPRINTF((DBG_INF,"\n"));
3436               continue;
3437             }
3438         }
3439       i=check_drives();
3440       DPRINTF((DBG_INI,"SBPCD: check_drives done.\n"));
3441       if (i>=0) break; /* drive found */
3442       DPRINTF((DBG_INF,"\n"));
3443     } /* end of cycling through the set of possible I/O port addresses */
3444 
3445   if (ndrives==0)
3446     {
3447       printk("SBPCD: No drive found.\n");
3448 #if PRINTK_BUG
3449       sti(); /* to avoid possible "printk" bug */
3450 #endif
3451       goto init_done;
3452     }
3453 
3454   if (port_index>0)
3455     {
3456       printk("SBPCD: You should configure sbpcd.h for your hardware.\n");
3457 #if PRINTK_BUG
3458       sti(); /* to avoid possible "printk" bug */
3459 #endif
3460     }
3461 
3462   printk("SBPCD: %d %s CD-ROM drive(s) at 0x%04X.\n",
3463            ndrives, type, CDo_command);
3464 #if PRINTK_BUG
3465   sti(); /* to avoid possible "printk" bug */
3466 #endif
3467   check_datarate();
3468   DPRINTF((DBG_INI,"SBPCD: check_datarate done.\n"));
3469 
3470   OUT(CDo_reset,0);
3471   sbp_sleep(100);
3472 
3473   for (j=0;j<NR_SBPCD;j++)
3474     {
3475       if (DriveStruct[j].drv_minor==-1) continue;
3476       switch_drive(j);
3477       xy_DriveReset();
3478       if (!st_spinning) xx_SpinUp();
3479       DriveStruct[d].sbp_first_frame = -1;  /* First frame in buffer */
3480       DriveStruct[d].sbp_last_frame = -1;   /* Last frame in buffer  */
3481       DriveStruct[d].sbp_read_frames = 0;   /* Number of frames being read to buffer */
3482       DriveStruct[d].sbp_current = 0;       /* Frame being currently read */
3483       DriveStruct[d].CD_changed=1;
3484       DriveStruct[d].frame_size=CD_FRAMESIZE;
3485 
3486       xx_ReadStatus();
3487       i=ResponseStatus();  /* returns orig. status or p_busy_new */
3488       if (i<0)
3489         DPRINTF((DBG_INF,"SBPCD: init: ResponseStatus returns %02X\n",i));
3490       else
3491         {
3492           if (st_check)
3493             {
3494               i=xx_ReadError();
3495               DPRINTF((DBG_INI,"SBPCD: init: xx_ReadError returns %d\n",i));
3496             }
3497         }
3498       DPRINTF((DBG_INI,"SBPCD: init: first GetStatus: %d\n",i));
3499       if (DriveStruct[d].error_byte==aud_12)
3500         {
3501           do { i=GetStatus();
3502                DPRINTF((DBG_INI,"SBPCD: init: second GetStatus: %02X\n",i));
3503                if (i<0) break;
3504                if (!st_caddy_in) break;
3505              }
3506           while (!st_diskok);
3507         }
3508       i=SetSpeed();
3509       if (i>=0) DriveStruct[d].CD_changed=1;
3510     }
3511 
3512   if (sbpro_type==1)
3513     {
3514       OUT(MIXER_addr,MIXER_CD_Volume);
3515       OUT(MIXER_data,0xCC); /* one nibble per channel */
3516     }
3517   
3518   if (register_blkdev(MAJOR_NR, "sbpcd", &sbpcd_fops) != 0)
3519     {
3520       printk("SBPCD: Can't get MAJOR %d for Matsushita CDROM\n", MAJOR_NR);
3521 #if PRINTK_BUG
3522       sti(); /* to avoid possible "printk" bug */
3523 #endif
3524       goto init_done;
3525     }
3526   blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
3527   read_ahead[MAJOR_NR] = SBP_BUFFER_FRAMES * (CD_FRAMESIZE / 512);
3528   
3529   snarf_region(CDo_command,4);
3530 
3531   for (j=0;j<NR_SBPCD;j++)
3532     {
3533       if (DriveStruct[j].drv_minor==-1) continue;
3534 /*
3535  * allocate memory for the frame buffers
3536  */ 
3537       DriveStruct[j].sbp_buf=(u_char *)mem_start;
3538       mem_start += SBP_BUFFER_FRAMES*CD_FRAMESIZE;
3539       if (new_drive)
3540         {
3541           DriveStruct[j].aud_buf=(u_char *)mem_start;
3542           mem_start += SBP_BUFFER_AUDIO_FRAMES*CD_FRAMESIZE_RAW;
3543         }
3544       else DriveStruct[j].aud_buf=NULL;
3545 /*
3546  * set the block size
3547  */
3548       sbpcd_blocksizes[j]=CD_FRAMESIZE;
3549     }
3550   blksize_size[MAJOR_NR]=sbpcd_blocksizes;
3551 
3552 init_done:
3553 #if !(SBPCD_ISSUE-1)
3554 #ifdef CONFIG_SBPCD2
3555   mem_start=sbpcd2_init(mem_start, mem_end);
3556 #endif
3557 #ifdef CONFIG_SBPCD3
3558   mem_start=sbpcd3_init(mem_start, mem_end);
3559 #endif
3560 #ifdef CONFIG_SBPCD4
3561   mem_start=sbpcd4_init(mem_start, mem_end);
3562 #endif
3563 #endif
3564 #if !(SBPCD_ISSUE-1)
3565   DPRINTF((DBG_INF,"SBPCD: init done.\n"));
3566 #endif
3567   return (mem_start);
3568 }
3569 /*==========================================================================*/
3570 /*
3571  * Check if the media has changed in the CD-ROM drive.
3572  * used externally (isofs/inode.c, fs/buffer.c)
3573  * Currently disabled (has to get "synchronized").
3574  */
3575 static int check_media_change(dev_t full_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
3576 {
3577   int st;
3578 
3579   DPRINTF((DBG_CHK,"SBPCD: media_check (%d) called\n", MINOR(full_dev)));
3580   return (0); /* "busy" test necessary before we really can check */
3581 
3582   if ((MAJOR(full_dev)!=MAJOR_NR)||(MINOR(full_dev)>=NR_SBPCD))
3583     {
3584       printk("SBPCD: media_check: invalid device %04X.\n", full_dev);
3585       return (-1);
3586     }
3587 
3588   switch_drive(MINOR(full_dev));
3589   
3590   xx_ReadStatus();                         /* command: give 1-byte status */
3591   st=ResponseStatus();
3592   DPRINTF((DBG_CHK,"SBPCD: media_check: %02X\n",DriveStruct[d].status_byte));
3593   if (st<0)
3594     {
3595       DPRINTF((DBG_INF,"SBPCD: media_check: ResponseStatus error.\n"));
3596       return (1); /* status not obtainable */
3597     }
3598   if (DriveStruct[d].CD_changed==0xFF) DPRINTF((DBG_CHK,"SBPCD: media_check: \"changed\" assumed.\n"));
3599   if (!st_spinning) DPRINTF((DBG_CHK,"SBPCD: media_check: motor off.\n"));
3600   if (!st_door_closed)
3601     {
3602       DPRINTF((DBG_CHK,"SBPCD: media_check: door open.\n"));
3603       DriveStruct[d].CD_changed=0xFF;
3604     }
3605   if (!st_caddy_in)
3606     {
3607       DPRINTF((DBG_CHK,"SBPCD: media_check: no disk in drive.\n"));
3608       DriveStruct[d].CD_changed=0xFF;
3609     }
3610   if (!st_diskok) DPRINTF((DBG_CHK,"SBPCD: media_check: !st_diskok.\n"));
3611   
3612 #if 0000
3613   if (DriveStruct[d].CD_changed==0xFF)
3614     {
3615       DriveStruct[d].CD_changed=1;
3616       return (1); /* driver had a change detected before */
3617     }
3618 #endif 0000 /* seems to give additional errors at the moment */
3619 
3620   if (!st_diskok) return (1); /* disk not o.k. */
3621   if (!st_caddy_in) return (1); /* disk removed */
3622   if (!st_door_closed) return (1); /* door open */
3623   return (0);
3624 }
3625 /*==========================================================================*/

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