root/drivers/cdrom/sbpcd.c

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

DEFINITIONS

This source file includes following definitions.
  1. msg
  2. sbpcd_dbg_ioctl
  3. mark_timeout_delay
  4. mark_timeout_data
  5. mark_timeout_audio
  6. sbp_sleep
  7. lba2msf
  8. bin2bcdx
  9. blk2msf
  10. make16
  11. make32
  12. swap_nibbles
  13. byt2bcd
  14. bcd2bin
  15. msf2blk
  16. msf2lba
  17. sta2err
  18. clr_cmdbuf
  19. flush_status
  20. CDi_stat_loop
  21. tst_DataReady
  22. tst_ResultReady
  23. tst_Attention
  24. ResponseInfo
  25. EvaluateStatus
  26. get_state_T
  27. ResponseStatus
  28. cc_ReadStatus
  29. cc_ReadError
  30. cmd_out_T
  31. cmd_out
  32. cc_Seek
  33. cc_SpinUp
  34. cc_SpinDown
  35. cc_get_mode_T
  36. cc_set_mode_T
  37. cc_prep_mode_T
  38. cc_SetSpeed
  39. cc_SetVolume
  40. GetStatus
  41. cc_DriveReset
  42. SetSpeed
  43. DriveReset
  44. cc_PlayAudio
  45. cc_Pause_Resume
  46. cc_LockDoor
  47. UnLockDoor
  48. LockDoor
  49. cc_CloseTray
  50. cc_ReadSubQ
  51. cc_ModeSense
  52. cc_ModeSelect
  53. cc_GetVolume
  54. cc_ReadCapacity
  55. cc_ReadTocDescr
  56. cc_ReadTocEntry
  57. cc_ReadPacket
  58. convert_UPC
  59. cc_ReadUPC
  60. cc_CheckMultiSession
  61. cc_SubChanInfo
  62. check_datarate
  63. c2_ReadError
  64. ask_mail
  65. check_version
  66. switch_drive
  67. check_card
  68. check_drives
  69. obey_audio_state
  70. check_allowed1
  71. check_allowed2
  72. check_allowed3
  73. seek_pos_audio_end
  74. ReadToC
  75. DiskInfo
  76. prepare
  77. sbp_status
  78. sbpcd_ioctl
  79. sbp_transfer
  80. DO_SBPCD_REQUEST
  81. sbp_read_cmd
  82. sbp_data
  83. sbpcd_open
  84. sbpcd_release
  85. sbpcd_setup
  86. config_spea
  87. init_module
  88. cleanup_module
  89. sbpcd_chk_disk_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  *            Also for the Longshine LCS-7260 drive.
   8  *            Also for the IBM "External ISA CD-Rom" drive.
   9  *            Also for the CreativeLabs CD200 drive (but I still need some
  10  *            detailed bug reports).
  11  *            Also for the TEAC CD-55A drive.
  12  *            Not for Sanyo drives (but sjcd is there...).
  13  *            Not for any other Funai drives than E2550UA (="CD200" with "F").
  14  *
  15  *  NOTE:     This is release 4.0
  16  *
  17  *   Copyright (C) 1993, 1994, 1995  Eberhard Moenkeberg <emoenke@gwdg.de>
  18  *
  19  *                  If you change this software, you should mail a .diff
  20  *                  file with some description lines to emoenke@gwdg.de.
  21  *                  I want to know about it.
  22  *
  23  *                  If you are the editor of a Linux CD, you should
  24  *                  enable sbpcd.c within your boot floppy kernel and
  25  *                  send me one of your CDs for free.
  26  *
  27  *   This program is free software; you can redistribute it and/or modify
  28  *   it under the terms of the GNU General Public License as published by
  29  *   the Free Software Foundation; either version 2, or (at your option)
  30  *   any later version.
  31  *
  32  *   You should have received a copy of the GNU General Public License
  33  *   (for example /usr/src/linux/COPYING); if not, write to the Free
  34  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35  *
  36  *  VERSION HISTORY
  37  *
  38  *  0.1  initial release, April/May 93, after mcd.c (Martin Harriss)
  39  *
  40  *  0.2  the "repeat:"-loop in do_sbpcd_request did not check for
  41  *       end-of-request_queue (resulting in kernel panic).
  42  *       Flow control seems stable, but throughput is not better.  
  43  *
  44  *  0.3  interrupt locking totally eliminated (maybe "inb" and "outb"
  45  *       are still locking) - 0.2 made keyboard-type-ahead losses.
  46  *       check_sbpcd_media_change added (to use by isofs/inode.c)
  47  *       - but it detects almost nothing.
  48  *
  49  *  0.4  use MAJOR 25 definitely.
  50  *       Almost total re-design to support double-speed drives and
  51  *       "naked" (no sound) interface cards ("LaserMate" interface type).
  52  *       Flow control should be exact now.
  53  *       Don't occupy the SbPro IRQ line (not needed either); will
  54  *       live together with Hannu Savolainen's sndkit now.
  55  *       Speeded up data transfer to 150 kB/sec, with help from Kai
  56  *       Makisara, the "provider" of the "mt" tape utility.
  57  *       Give "SpinUp" command if necessary.
  58  *       First steps to support up to 4 drives (but currently only one).
  59  *       Implemented audio capabilities - workman should work, xcdplayer
  60  *       gives some problems.
  61  *       This version is still consuming too much CPU time, and
  62  *       sleeping still has to be worked on.
  63  *       During "long" implied seeks, it seems possible that a 
  64  *       ReadStatus command gets ignored. That gives the message
  65  *       "ResponseStatus timed out" (happens about 6 times here during
  66  *       a "ls -alR" of the YGGDRASIL LGX-Beta CD). Such a case is
  67  *       handled without data error, but it should get done better.
  68  *
  69  *  0.5  Free CPU during waits (again with help from Kai Makisara).
  70  *       Made it work together with the LILO/kernel setup standard.
  71  *       Included auto-probing code, as suggested by YGGDRASIL.
  72  *       Formal redesign to add DDI debugging.
  73  *       There are still flaws in IOCTL (workman with double speed drive).
  74  *
  75  *  1.0  Added support for all drive IDs (0...3, no longer only 0)
  76  *       and up to 4 drives on one controller.
  77  *       Added "#define MANY_SESSION" for "old" multi session CDs.
  78  *
  79  *  1.1  Do SpinUp for new drives, too.
  80  *       Revised for clean compile under "old" kernels (0.99pl9).
  81  *
  82  *  1.2  Found the "workman with double-speed drive" bug: use the driver's
  83  *       audio_state, not what the drive is reporting with ReadSubQ.
  84  *
  85  *  1.3  Minor cleanups.
  86  *       Refinements regarding Workman.
  87  *
  88  *  1.4  Read XA disks (PhotoCDs) with "old" drives, too (but only the first
  89  *       session - no chance to fully access a "multi-session" CD).
  90  *       This currently still is too slow (50 kB/sec) - but possibly
  91  *       the old drives won't do it faster.
  92  *       Implemented "door (un)lock" for new drives (still does not work
  93  *       as wanted - no lock possible after an unlock).
  94  *       Added some debugging printout for the UPC/EAN code - but my drives 
  95  *       return only zeroes. Is there no UPC/EAN code written?
  96  *
  97  *  1.5  Laborate with UPC/EAN code (not better yet).
  98  *       Adapt to kernel 1.1.8 change (have to explicitly include
  99  *       <linux/string.h> now).
 100  *
 101  *  1.6  Trying to read audio frames as data. Impossible with the current
 102  *       drive firmware levels, as it seems. Awaiting any hint. ;-)
 103  *       Changed "door unlock": repeat it until success.
 104  *       Changed CDROMSTOP routine (stop somewhat "softer" so that Workman
 105  *       won't get confused).
 106  *       Added a third interface type: Sequoia S-1000, as used with the SPEA
 107  *       Media FX sound card. This interface (usable for Sony and Mitsumi 
 108  *       drives, too) needs a special configuration setup and behaves like a 
 109  *       LaserMate type after that. Still experimental - I do not have such
 110  *       an interface.
 111  *       Use the "variable BLOCK_SIZE" feature (2048). But it does only work
 112  *       if you give the mount option "block=2048".
 113  *       The media_check routine is currently disabled; now that it gets
 114  *       called as it should I fear it must get synchronized for not to
 115  *       disturb the normal driver's activity.
 116  *
 117  *  2.0  Version number bumped - two reasons:
 118  *       - reading audio tracks as data works now with CR-562 and CR-563. We
 119  *       currently do it by an IOCTL (yet has to get standardized), one frame
 120  *       at a time; that is pretty slow. But it works.
 121  *       - we are maintaining now up to 4 interfaces (each up to 4 drives):
 122  *       did it the easy way - a different MAJOR (25, 26, ...) and a different
 123  *       copy of the driver (sbpcd.c, sbpcd2.c, sbpcd3.c, sbpcd4.c - only
 124  *       distinguished by the value of SBPCD_ISSUE and the driver's name),
 125  *       and a common sbpcd.h file.
 126  *       Bettered the "ReadCapacity error" problem with old CR-52x drives (the
 127  *       drives sometimes need a manual "eject/insert" before work): just
 128  *       reset the drive and do again. Needs lots of resets here and sometimes
 129  *       that does not cure, so this can't be the solution.
 130  *
 131  *  2.1  Found bug with multisession CDs (accessing frame 16).
 132  *       "read audio" works now with address type CDROM_MSF, too.
 133  *       Bigger audio frame buffer: allows reading max. 4 frames at time; this
 134  *       gives a significant speedup, but reading more than one frame at once
 135  *       gives missing chunks at each single frame boundary.
 136  *
 137  *  2.2  Kernel interface cleanups: timers, init, setup, media check.
 138  *
 139  *  2.3  Let "door lock" and "eject" live together.
 140  *       Implemented "close tray" (done automatically during open).
 141  *
 142  *  2.4  Use different names for device registering.
 143  *
 144  *  2.5  Added "#if EJECT" code (default: enabled) to automatically eject
 145  *       the tray during last call to "sbpcd_release".
 146  *       Added "#if JUKEBOX" code (default: disabled) to automatically eject
 147  *       the tray during call to "sbpcd_open" if no disk is in.
 148  *       Turn on the CD volume of "compatible" sound cards, too; just define
 149  *       SOUND_BASE (in sbpcd.h) accordingly (default: disabled).
 150  *
 151  *  2.6  Nothing new.  
 152  *
 153  *  2.7  Added CDROMEJECT_SW ioctl to set the "EJECT" behavior on the fly:
 154  *       0 disables, 1 enables auto-ejecting. Useful to keep the tray in
 155  *       during shutdown.
 156  *
 157  *  2.8  Added first support (still BETA, I need feedback or a drive) for
 158  *       the Longshine LCS-7260 drives. They appear as double-speed drives
 159  *       using the "old" command scheme, extended by tray control and door
 160  *       lock functions.
 161  *       Found (and fixed preliminary) a flaw with some multisession CDs: we
 162  *       have to re-direct not only the accesses to frame 16 (the isofs
 163  *       routines drive it up to max. 100), but also those to the continuation
 164  *       (repetition) frames (as far as they exist - currently set fix as
 165  *       16..20).
 166  *       Changed default of the "JUKEBOX" define. If you use this default,
 167  *       your tray will eject if you try to mount without a disk in. Next
 168  *       mount command will insert the tray - so, just fill in a disk. ;-)
 169  *
 170  *  2.9  Fulfilled the Longshine LCS-7260 support; with great help and
 171  *       experiments by Serge Robyns.
 172  *       First attempts to support the TEAC CD-55A drives; but still not
 173  *       usable yet.
 174  *       Implemented the CDROMMULTISESSION ioctl; this is an attempt to handle
 175  *       multi session CDs more "transparent" (redirection handling has to be
 176  *       done within the isofs routines, and only for the special purpose of
 177  *       obtaining the "right" volume descriptor; accesses to the raw device
 178  *       should not get redirected).
 179  *
 180  *  3.0  Just a "normal" increment, with some provisions to do it better. ;-)
 181  *       Introduced "#define READ_AUDIO" to specify the maximum number of 
 182  *       audio frames to grab with one request. This defines a buffer size
 183  *       within kernel space; a value of 0 will reserve no such space and
 184  *       disable the CDROMREADAUDIO ioctl. A value of 75 enables the reading
 185  *       of a whole second with one command, but will use a buffer of more
 186  *       than 172 kB.
 187  *       Started CD200 support. Drive detection should work, but nothing
 188  *       more.
 189  *
 190  *  3.1  Working to support the CD200 and the Teac CD-55A drives.
 191  *       AT-BUS style device numbering no longer used: use SCSI style now.
 192  *       So, the first "found" device has MINOR 0, regardless of the
 193  *       jumpered drive ID. This implies modifications to the /dev/sbpcd*
 194  *       entries for some people, but will help the DAU (german TLA, english:
 195  *       "newbie", maybe ;-) to install his "first" system from a CD.
 196  *
 197  *  3.2  Still testing with CD200 and CD-55A drives.
 198  *
 199  *  3.3  Working with CD200 support.
 200  *
 201  *  3.4  Auto-probing stops if an address of 0 is seen (to be entered with
 202  *       the kernel command line).
 203  *       Made the driver "loadable". If used as a module, "audio copy" is
 204  *       disabled, and the internal read ahead data buffer has a reduced size
 205  *       of 4 kB; so, throughput may be reduced a little bit with slow CPUs.
 206  *
 207  *  3.5  Provisions to handle weird photoCDs which have an interrupted
 208  *       "formatting" immediately after the last frames of some files: simply
 209  *       never "read ahead" with MultiSession CDs. By this, CPU usage may be
 210  *       increased with those CDs, and there may be a loss in speed.
 211  *       Re-structured the messaging system.
 212  *       The "loadable" version no longer has a limited READ_AUDIO buffer
 213  *       size.
 214  *       Removed "MANY_SESSION" handling for "old" multi session CDs.
 215  *       Added "private" IOCTLs CDROMRESET and CDROMVOLREAD.
 216  *       Started again to support the TEAC CD-55A drives, now that I found
 217  *       the money for "my own" drive. ;-)
 218  *       The TEAC CD-55A support is fairly working now.
 219  *       I have measured that the drive "delivers" at 600 kB/sec (even with
 220  *       bigger requests than the drive's 64 kB buffer can satisfy), but
 221  *       the "real" rate does not exceed 520 kB/sec at the moment. 
 222  *       Caused by the various changes to build in TEAC support, the timed
 223  *       loops are de-optimized at the moment (less throughput with CR-52x
 224  *       drives, and the TEAC will give speed only with SBP_BUFFER_FRAMES 64).
 225  *
 226  *  3.6  Fixed TEAC data read problems with SbPro interfaces.
 227  *       Initial size of the READ_AUDIO buffer is 0. Can get set to any size
 228  *       during runtime.
 229  *
 230  *  3.7  Introduced MAX_DRIVES for some poor interface cards (seen with TEAC
 231  *       drives) which allow only one drive (ID 0); this avoids repetitive
 232  *       detection under IDs 1..3. 
 233  *       Elongated cmd_out_T response waiting; necessary for photo CDs with
 234  *       a lot of sessions.
 235  *       Bettered the sbpcd_open() behavior with TEAC drives.
 236  *
 237  *  3.8  Elongated max_latency for CR-56x drives.
 238  *
 239  *  3.9  Finally fixed the long-known SoundScape/SPEA/Sequoia S-1000 interface
 240  *       configuration bug.
 241  *       Now Corey, Heiko, Ken, Leo, Vadim/Eric & Werner are invited to copy
 242  *       the config_spea() routine into their drivers. ;-)
 243  *
 244  *  4.0  No "big step" - normal version increment.
 245  *       Adapted the benefits from 1.3.33.
 246  *       Fiddled with CDROMREADAUDIO flaws.
 247  *       Avoid ReadCapacity command with CD200 drives (the MKE 1.01 version
 248  *       seems not to support it).
 249  *       Fulfilled "read audio" for CD200 drives, with help of Pete Heist
 250  *       (heistp@rpi.edu).
 251  *
 252  *  TODO
 253  *
 254  *     disk change detection
 255  *     synchronize multi-activity
 256  *        (data + audio + ioctl + disk change, multiple drives)
 257  *     implement "read all subchannel data" (96 bytes per frame)
 258  *     check if CDROMPLAYMSF can cause a hang
 259  *
 260  *     special thanks to Kai Makisara (kai.makisara@vtt.fi) for his fine
 261  *     elaborated speed-up experiments (and the fabulous results!), for
 262  *     the "push" towards load-free wait loops, and for the extensive mail
 263  *     thread which brought additional hints and bug fixes.
 264  *
 265  */
 266 
 267 #ifndef SBPCD_ISSUE
 268 #define SBPCD_ISSUE 1
 269 #endif SBPCD_ISSUE
 270 
 271 #include <linux/module.h>
 272 
 273 #include <linux/errno.h>
 274 #include <linux/sched.h>
 275 #include <linux/mm.h>
 276 #include <linux/timer.h>
 277 #include <linux/fs.h>
 278 #include <linux/kernel.h>
 279 #include <linux/cdrom.h>
 280 #include <linux/ioport.h>
 281 #include <linux/major.h> 
 282 #include <linux/string.h>
 283 #include <asm/system.h>
 284 #include <asm/io.h>
 285 #include <asm/segment.h>
 286 #include <stdarg.h>
 287 #include <linux/sbpcd.h>
 288 #include <linux/config.h>
 289 
 290 #if !(SBPCD_ISSUE-1)
 291 #define MAJOR_NR MATSUSHITA_CDROM_MAJOR
 292 #endif
 293 #if !(SBPCD_ISSUE-2)
 294 #define MAJOR_NR MATSUSHITA_CDROM2_MAJOR /* second driver issue */
 295 #endif
 296 #if !(SBPCD_ISSUE-3)
 297 #define MAJOR_NR MATSUSHITA_CDROM3_MAJOR /* third driver issue */
 298 #endif
 299 #if !(SBPCD_ISSUE-4)
 300 #define MAJOR_NR MATSUSHITA_CDROM4_MAJOR /* fourth driver issue */
 301 #endif
 302 
 303 #include <linux/blk.h>
 304 
 305 #define VERSION "v4.0 Eberhard Moenkeberg <emoenke@gwdg.de>"
 306 
 307 /*==========================================================================*/
 308 /*
 309  * provisions for more than 1 driver issues
 310  * currently up to 4 drivers, expandable
 311  */
 312 #if !(SBPCD_ISSUE-1)
 313 #define DO_SBPCD_REQUEST(a) do_sbpcd_request(a)
 314 #define SBPCD_INIT(a) sbpcd_init(a)
 315 #endif
 316 #if !(SBPCD_ISSUE-2)
 317 #define DO_SBPCD_REQUEST(a) do_sbpcd2_request(a)
 318 #define SBPCD_INIT(a) sbpcd2_init(a)
 319 #endif
 320 #if !(SBPCD_ISSUE-3)
 321 #define DO_SBPCD_REQUEST(a) do_sbpcd3_request(a)
 322 #define SBPCD_INIT(a) sbpcd3_init(a)
 323 #endif
 324 #if !(SBPCD_ISSUE-4)
 325 #define DO_SBPCD_REQUEST(a) do_sbpcd4_request(a)
 326 #define SBPCD_INIT(a) sbpcd4_init(a)
 327 #endif
 328 /*==========================================================================*/
 329 #if SBPCD_DIS_IRQ
 330 #define SBPCD_CLI cli()
 331 #define SBPCD_STI sti()
 332 #else
 333 #define SBPCD_CLI
 334 #define SBPCD_STI
 335 #endif SBPCD_DIS_IRQ
 336 /*==========================================================================*/
 337 /*
 338  * auto-probing address list
 339  * inspired by Adam J. Richter from Yggdrasil
 340  *
 341  * still not good enough - can cause a hang.
 342  *   example: a NE 2000 ethernet card at 300 will cause a hang probing 310.
 343  * if that happens, reboot and use the LILO (kernel) command line.
 344  * The possibly conflicting ethernet card addresses get NOT probed 
 345  * by default - to minimize the hang possibilities. 
 346  *
 347  * The SB Pro addresses get "mirrored" at 0x6xx and some more locations - to
 348  * avoid a type error, the 0x2xx-addresses must get checked before 0x6xx.
 349  *
 350  * send mail to emoenke@gwdg.de if your interface card is not FULLY
 351  * represented here.
 352  */
 353 #if !(SBPCD_ISSUE-1)
 354 static int sbpcd[] = 
 355 {
 356         CDROM_PORT, SBPRO, /* probe with user's setup first */
 357 #if DISTRIBUTION
 358         0x230, 1, /* Soundblaster Pro and 16 (default) */
 359         0x300, 0, /* CI-101P (default), WDH-7001C (default),
 360                      Galaxy (default), Reveal (one default) */
 361         0x250, 1, /* OmniCD default, Soundblaster Pro and 16 */
 362         0x260, 1, /* OmniCD */
 363         0x320, 0, /* Lasermate, CI-101P, WDH-7001C, Galaxy, Reveal (other default),
 364                      Longshine LCS-6853 (default) */
 365         0x338, 0, /* Reveal Sound Wave 32 card model #SC600 */
 366         0x340, 0, /* Mozart sound card (default), Lasermate, CI-101P */
 367         0x360, 0, /* Lasermate, CI-101P */
 368         0x270, 1, /* Soundblaster 16 */
 369         0x670, 0, /* "sound card #9" */
 370         0x690, 0, /* "sound card #9" */
 371         0x338, 2, /* SPEA Media FX, Ensonic SoundScape (default) */
 372         0x328, 2, /* SPEA Media FX */
 373         0x348, 2, /* SPEA Media FX */
 374         0x634, 0, /* some newer sound cards */
 375         0x638, 0, /* some newer sound cards */
 376         0x230, 1, /* some newer sound cards */
 377         /* due to incomplete address decoding of the SbPro card, these must be last */
 378         0x630, 0, /* "sound card #9" (default) */
 379         0x650, 0, /* "sound card #9" */
 380 #ifdef MODULE
 381         /*
 382          * some "hazardous" locations (no harm with the loadable version)
 383          * (will stop the bus if a NE2000 ethernet card resides at offset -0x10)
 384          */
 385         0x330, 0, /* Lasermate, CI-101P, WDH-7001C */
 386         0x350, 0, /* Lasermate, CI-101P */
 387         0x358, 2, /* SPEA Media FX */
 388         0x370, 0, /* Lasermate, CI-101P */
 389         0x290, 1, /* Soundblaster 16 */
 390         0x310, 0, /* Lasermate, CI-101P, WDH-7001C */
 391 #endif MODULE
 392 #endif DISTRIBUTION
 393 };
 394 #else
 395 static int sbpcd[] = {CDROM_PORT, SBPRO}; /* probe with user's setup only */
 396 #endif
 397 
 398 #define NUM_PROBE  (sizeof(sbpcd) / sizeof(int))
 399 
 400 /*==========================================================================*/
 401 /*
 402  * the external references:
 403  */
 404 #if !(SBPCD_ISSUE-1)
 405 #ifdef CONFIG_SBPCD2
 406 extern int sbpcd2_init(void);
 407 #endif
 408 #ifdef CONFIG_SBPCD3
 409 extern int sbpcd3_init(void);
 410 #endif
 411 #ifdef CONFIG_SBPCD4
 412 extern int sbpcd4_init(void);
 413 #endif
 414 #endif
 415 
 416 /*==========================================================================*/
 417 
 418 #define INLINE inline
 419 
 420 /*==========================================================================*/
 421 /*
 422  * the forward references:
 423  */
 424 static void sbp_sleep(u_int);
 425 static void mark_timeout_delay(u_long);
 426 static void mark_timeout_data(u_long);
 427 #if 0
 428 static void mark_timeout_audio(u_long);
 429 #endif
 430 static void sbp_read_cmd(void);
 431 static int sbp_data(void);
 432 static int cmd_out(void);
 433 static int DiskInfo(void);
 434 static int sbpcd_chk_disk_change(kdev_t);
 435 
 436 /*==========================================================================*/
 437 
 438 /*
 439  * pattern for printk selection:
 440  *
 441  * (1<<DBG_INF)  necessary information
 442  * (1<<DBG_BSZ)  BLOCK_SIZE trace
 443  * (1<<DBG_REA)  "read" status trace
 444  * (1<<DBG_CHK)  "media check" trace
 445  * (1<<DBG_TIM)  datarate timer test
 446  * (1<<DBG_INI)  initialization trace
 447  * (1<<DBG_TOC)  tell TocEntry values
 448  * (1<<DBG_IOC)  ioctl trace
 449  * (1<<DBG_STA)  "ResponseStatus" trace
 450  * (1<<DBG_ERR)  "cc_ReadError" trace
 451  * (1<<DBG_CMD)  "cmd_out" trace
 452  * (1<<DBG_WRN)  give explanation before auto-probing
 453  * (1<<DBG_MUL)  multi session code test
 454  * (1<<DBG_IDX)  "drive_id != 0" test code
 455  * (1<<DBG_IOX)  some special information
 456  * (1<<DBG_DID)  drive ID test
 457  * (1<<DBG_RES)  drive reset info
 458  * (1<<DBG_SPI)  SpinUp test info
 459  * (1<<DBG_IOS)  ioctl trace: "subchannel"
 460  * (1<<DBG_IO2)  ioctl trace: general
 461  * (1<<DBG_UPC)  show UPC info
 462  * (1<<DBG_XA1)  XA mode debugging
 463  * (1<<DBG_LCK)  door (un)lock info
 464  * (1<<DBG_SQ1)   dump SubQ frame
 465  * (1<<DBG_AUD)  "read audio" debugging
 466  * (1<<DBG_SEQ)  Sequoia interface configuration trace
 467  * (1<<DBG_LCS)  Longshine LCS-7260 debugging trace
 468  * (1<<DBG_CD2)  MKE CD200 debugging trace
 469  * (1<<DBG_TEA)  TEAC CD-55A debugging trace
 470  * (1<<DBG_TE2)  TEAC CD-55A debugging trace, 2nd level
 471  * (1<<DBG_000)  unnecessary information
 472  */
 473 #if DISTRIBUTION
 474 static int sbpcd_debug = (1<<DBG_INF);
 475 #else
 476 static int sbpcd_debug = ((1<<DBG_INF) |
 477                           (1<<DBG_TOC) |
 478                           (1<<DBG_MUL) |
 479                           (1<<DBG_AUD) |
 480                           (1<<DBG_UPC));
 481 #endif DISTRIBUTION
 482 
 483 static int sbpcd_ioaddr = CDROM_PORT;   /* default I/O base address */
 484 static int sbpro_type = SBPRO;
 485 static unsigned char setup_done = 0;
 486 static int CDo_command, CDo_reset;
 487 static int CDo_sel_i_d, CDo_enable;
 488 static int CDi_info, CDi_status, CDi_data;
 489 static int MIXER_addr, MIXER_data;
 490 static struct cdrom_msf msf;
 491 static struct cdrom_ti ti;
 492 static struct cdrom_tochdr tochdr;
 493 static struct cdrom_tocentry tocentry;
 494 static struct cdrom_subchnl SC;
 495 static struct cdrom_volctrl volctrl;
 496 static struct cdrom_read_audio read_audio;
 497 static struct cdrom_multisession ms_info;
 498 
 499 static unsigned char msgnum=0;
 500 static char msgbuf[80];
 501 
 502 static const char *str_sb = "SoundBlaster";
 503 static const char *str_sb_l = "soundblaster";
 504 static const char *str_lm = "LaserMate";
 505 static const char *str_sp = "SPEA";
 506 static const char *str_sp_l = "spea";
 507 static const char *str_ss = "SoundScape";
 508 static const char *str_ss_l = "soundscape";
 509 const char *type;
 510 
 511 #if !(SBPCD_ISSUE-1)
 512 static const char *major_name="sbpcd";
 513 #endif
 514 #if !(SBPCD_ISSUE-2)
 515 static const char *major_name="sbpcd2";
 516 #endif
 517 #if !(SBPCD_ISSUE-3)
 518 static const char *major_name="sbpcd3";
 519 #endif
 520 #if !(SBPCD_ISSUE-4)
 521 static const char *major_name="sbpcd4";
 522 #endif
 523 
 524 /*==========================================================================*/
 525 
 526 #if FUTURE
 527 static struct wait_queue *sbp_waitq = NULL;
 528 #endif FUTURE
 529 
 530 /*==========================================================================*/
 531 #define SBP_BUFFER_FRAMES 8 /* driver's own read_ahead, data mode */
 532 /*==========================================================================*/
 533 
 534 static u_char family0[]="MATSHITA"; /* MKE CR-521, CR-522, CR-523 */
 535 static u_char family1[]="CR-56";    /* MKE CR-562, CR-563 */
 536 static u_char family2[]="CD200";    /* MKE CD200, Funai CD200F */
 537 static u_char familyL[]="LCS-7260"; /* Longshine LCS-7260 */
 538 static u_char familyT[]="CD-55";    /* TEAC CD-55A */
 539 
 540 static u_int recursion=0; /* internal testing only */
 541 static u_int fatal_err=0; /* internal testing only */
 542 static u_int response_count=0;
 543 static u_int flags_cmd_out;
 544 static u_char cmd_type=0;
 545 static u_char drvcmd[10];
 546 static u_char infobuf[20];
 547 static u_char xa_head_buf[CD_XA_HEAD];
 548 static u_char xa_tail_buf[CD_XA_TAIL];
 549 
 550 static volatile u_char busy_data=0;
 551 static volatile u_char busy_audio=0; /* true semaphores would be safer */
 552 static u_long timeout;
 553 static volatile u_char timed_out_delay=0;
 554 static volatile u_char timed_out_data=0;
 555 #if 0
 556 static volatile u_char timed_out_audio=0;
 557 #endif
 558 static u_int datarate= 1000000;
 559 static u_int maxtim16=16000000;
 560 static u_int maxtim04= 4000000;
 561 static u_int maxtim02= 2000000;
 562 static u_int maxtim_8=   30000;
 563 #if LONG_TIMING
 564 static u_int maxtim_data= 9000;
 565 #else
 566 static u_int maxtim_data= 3000;
 567 #endif LONG_TIMING
 568 #if DISTRIBUTION
 569 static int n_retries=3;
 570 #else
 571 static int n_retries=1;
 572 #endif
 573 /*==========================================================================*/
 574 
 575 static int ndrives=0;
 576 static u_char drv_pattern[NR_SBPCD]={speed_auto,speed_auto,speed_auto,speed_auto};
 577 static int sbpcd_blocksizes[NR_SBPCD] = {0, };
 578 
 579 /*==========================================================================*/
 580 /*
 581  * drive space begins here (needed separate for each unit) 
 582  */
 583 static int d=0; /* DriveStruct index: drive number */
 584 
 585 static struct {
 586         char drv_id;           /* "jumpered" drive ID or -1 */
 587         char drv_sel;          /* drive select lines bits */
 588         
 589         char drive_model[9];
 590         u_char firmware_version[4];
 591         char f_eject;          /* auto-eject flag: 0 or 1 */
 592         u_char *sbp_buf;       /* Pointer to internal data buffer,
 593                                   space allocated during sbpcd_init() */
 594         u_int sbp_bufsiz;      /* size of sbp_buf (# of frames) */
 595         int sbp_first_frame;   /* First frame in buffer */
 596         int sbp_last_frame;    /* Last frame in buffer  */
 597         int sbp_read_frames;   /* Number of frames being read to buffer */
 598         int sbp_current;       /* Frame being currently read */
 599         
 600         u_char mode;           /* read_mode: READ_M1, READ_M2, READ_SC, READ_AU */
 601         u_char *aud_buf;       /* Pointer to audio data buffer,
 602                                   space allocated during sbpcd_init() */
 603         u_int sbp_audsiz;      /* size of aud_buf (# of raw frames) */
 604         u_char drv_type;
 605         u_char drv_options;
 606         int status_bits;
 607         u_char diskstate_flags;
 608         u_char sense_byte;
 609         
 610         u_char CD_changed;
 611         char open_count;
 612         u_char error_byte;
 613         
 614         u_char f_multisession;
 615         u_int lba_multi;
 616         int first_session;
 617         int last_session;
 618         int track_of_last_session;
 619         
 620         u_char audio_state;
 621         u_int pos_audio_start;
 622         u_int pos_audio_end;
 623         char vol_chan0;
 624         u_char vol_ctrl0;
 625         char vol_chan1;
 626         u_char vol_ctrl1;
 627 #if 000 /* no supported drive has it */
 628         char vol_chan2;
 629         u_char vol_ctrl2;
 630         char vol_chan3;
 631         u_char vol_ctrl3;
 632 #endif 000
 633         u_char volume_control; /* TEAC on/off bits */
 634         
 635         u_char SubQ_ctl_adr;
 636         u_char SubQ_trk;
 637         u_char SubQ_pnt_idx;
 638         u_int SubQ_run_tot;
 639         u_int SubQ_run_trk;
 640         u_char SubQ_whatisthis;
 641         
 642         u_char UPC_ctl_adr;
 643         u_char UPC_buf[7];
 644         
 645         int frame_size;
 646         int CDsize_frm;
 647         
 648         u_char xa_byte; /* 0x20: XA capabilities */
 649         u_char n_first_track; /* binary */
 650         u_char n_last_track; /* binary (not bcd), 0x01...0x63 */
 651         u_int size_msf; /* time of whole CD, position of LeadOut track */
 652         u_int size_blk;
 653         
 654         u_char TocEnt_nixbyte; /* em */
 655         u_char TocEnt_ctl_adr;
 656         u_char TocEnt_number;
 657         u_char TocEnt_format; /* em */
 658         u_int TocEnt_address;
 659         u_char ored_ctl_adr; /* to detect if CDROM contains data tracks */
 660         
 661         struct {
 662                 u_char nixbyte; /* em */
 663                 u_char ctl_adr; /* 0x4x: data, 0x0x: audio */
 664                 u_char number;
 665                 u_char format; /* em */ /* 0x00: lba, 0x01: msf */
 666                 u_int address;
 667         } TocBuffer[MAX_TRACKS+1]; /* last entry faked */ 
 668         
 669         int in_SpinUp; /* CR-52x test flag */
 670         int n_bytes; /* TEAC awaited response count */
 671         u_char error_state, b3, b4; /* TEAC command error state */
 672         u_char f_drv_error; /* TEAC command error flag */
 673         u_char speed_byte;
 674         int frmsiz;
 675         u_char f_XA; /* 1: XA */
 676         u_char type_byte; /* 0, 1, 3 */
 677         u_char mode_xb_6;
 678         u_char mode_yb_7;
 679         u_char mode_xb_8;
 680         u_char delay;
 681         
 682 } D_S[NR_SBPCD];
 683 
 684 /*
 685  * drive space ends here (needed separate for each unit)
 686  */
 687 /*==========================================================================*/
 688 #if 0
 689 unsigned long cli_sti; /* for saving the processor flags */
 690 #endif
 691 /*==========================================================================*/
 692 static struct timer_list delay_timer = { NULL, NULL, 0, 0, mark_timeout_delay};
 693 static struct timer_list data_timer = { NULL, NULL, 0, 0, mark_timeout_data};
 694 #if 0
 695 static struct timer_list audio_timer = { NULL, NULL, 0, 0, mark_timeout_audio};
 696 #endif
 697 /*==========================================================================*/
 698 /*
 699  * DDI interface
 700  */
 701 static void msg(int level, const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 702 {
 703         char buf[256];
 704         va_list args;
 705         
 706         if (!(sbpcd_debug&(1<<level))) return;
 707         
 708         msgnum++;
 709         if (msgnum>99) msgnum=0;
 710         sprintf(buf, "%s-%d [%02d]:  ", major_name, d, msgnum);
 711         va_start(args, fmt);
 712         vsprintf(&buf[15], fmt, args);
 713         va_end(args);
 714         printk(buf);
 715         sbp_sleep(55); /* else messages get lost */
 716         return;
 717 }
 718 /*==========================================================================*/
 719 /*
 720  * DDI interface: runtime trace bit pattern maintenance
 721  */
 722 static int sbpcd_dbg_ioctl(unsigned long arg, int level)
     /* [previous][next][first][last][top][bottom][index][help] */
 723 {
 724         switch(arg)
 725         {
 726         case 0: /* OFF */
 727                 sbpcd_debug = DBG_INF;
 728                 break;
 729                 
 730         default:
 731                 if (arg>=128) sbpcd_debug &= ~(1<<(arg-128));
 732                 else sbpcd_debug |= (1<<arg);
 733         }
 734         return (arg);
 735 }
 736 /*==========================================================================*/
 737 static void mark_timeout_delay(u_long i)
     /* [previous][next][first][last][top][bottom][index][help] */
 738 {
 739         timed_out_delay=1;
 740 #if 0
 741         msg(DBG_TIM,"delay timer expired.\n");
 742 #endif
 743 }
 744 /*==========================================================================*/
 745 static void mark_timeout_data(u_long i)
     /* [previous][next][first][last][top][bottom][index][help] */
 746 {
 747         timed_out_data=1;
 748 #if 0
 749         msg(DBG_TIM,"data timer expired.\n");
 750 #endif
 751 }
 752 /*==========================================================================*/
 753 #if 0
 754 static void mark_timeout_audio(u_long i)
     /* [previous][next][first][last][top][bottom][index][help] */
 755 {
 756         timed_out_audio=1;
 757 #if 0
 758         msg(DBG_TIM,"audio timer expired.\n");
 759 #endif
 760 }
 761 #endif
 762 /*==========================================================================*/
 763 /*
 764  * Wait a little while (used for polling the drive).
 765  */
 766 static void sbp_sleep(u_int time)
     /* [previous][next][first][last][top][bottom][index][help] */
 767 {
 768         sti();
 769         current->state = TASK_INTERRUPTIBLE;
 770         current->timeout = jiffies + time;
 771         schedule();
 772         sti();
 773 }
 774 /*==========================================================================*/
 775 /*
 776  *  convert logical_block_address to m-s-f_number (3 bytes only)
 777  */
 778 static INLINE void lba2msf(int lba, u_char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780         lba += CD_MSF_OFFSET;
 781         msf[0] = lba / (CD_SECS*CD_FRAMES);
 782         lba %= CD_SECS*CD_FRAMES;
 783         msf[1] = lba / CD_FRAMES;
 784         msf[2] = lba % CD_FRAMES;
 785 }
 786 /*==========================================================================*/
 787 /*==========================================================================*/
 788 /*
 789  *  convert msf-bin to msf-bcd
 790  */
 791 static INLINE void bin2bcdx(u_char *p)  /* must work only up to 75 or 99 */
     /* [previous][next][first][last][top][bottom][index][help] */
 792 {
 793         *p=((*p/10)<<4)|(*p%10);
 794 }
 795 /*==========================================================================*/
 796 static INLINE u_int blk2msf(u_int blk)
     /* [previous][next][first][last][top][bottom][index][help] */
 797 {
 798         MSF msf;
 799         u_int mm;
 800         
 801         msf.c[3] = 0;
 802         msf.c[2] = (blk + CD_MSF_OFFSET) / (CD_SECS * CD_FRAMES);
 803         mm = (blk + CD_MSF_OFFSET) % (CD_SECS * CD_FRAMES);
 804         msf.c[1] = mm / CD_FRAMES;
 805         msf.c[0] = mm % CD_FRAMES;
 806         return (msf.n);
 807 }
 808 /*==========================================================================*/
 809 static INLINE u_int make16(u_char rh, u_char rl)
     /* [previous][next][first][last][top][bottom][index][help] */
 810 {
 811         return ((rh<<8)|rl);
 812 }
 813 /*==========================================================================*/
 814 static INLINE u_int make32(u_int rh, u_int rl)
     /* [previous][next][first][last][top][bottom][index][help] */
 815 {
 816         return ((rh<<16)|rl);
 817 }
 818 /*==========================================================================*/
 819 static INLINE u_char swap_nibbles(u_char i)
     /* [previous][next][first][last][top][bottom][index][help] */
 820 {
 821         return ((i<<4)|(i>>4));
 822 }
 823 /*==========================================================================*/
 824 static INLINE u_char byt2bcd(u_char i)
     /* [previous][next][first][last][top][bottom][index][help] */
 825 {
 826         return (((i/10)<<4)+i%10);
 827 }
 828 /*==========================================================================*/
 829 static INLINE u_char bcd2bin(u_char bcd)
     /* [previous][next][first][last][top][bottom][index][help] */
 830 {
 831         return ((bcd>>4)*10+(bcd&0x0F));
 832 }
 833 /*==========================================================================*/
 834 static INLINE int msf2blk(int msfx)
     /* [previous][next][first][last][top][bottom][index][help] */
 835 {
 836         MSF msf;
 837         int i;
 838         
 839         msf.n=msfx;
 840         i=(msf.c[2] * CD_SECS + msf.c[1]) * CD_FRAMES + msf.c[0] - CD_MSF_OFFSET;
 841         if (i<0) return (0);
 842         return (i);
 843 }
 844 /*==========================================================================*/
 845 /*
 846  *  convert m-s-f_number (3 bytes only) to logical_block_address 
 847  */
 848 static INLINE int msf2lba(u_char *msf)
     /* [previous][next][first][last][top][bottom][index][help] */
 849 {
 850         int i;
 851         
 852         i=(msf[0] * CD_SECS + msf[1]) * CD_FRAMES + msf[2] - CD_MSF_OFFSET;
 853         if (i<0) return (0);
 854         return (i);
 855 }
 856 /*==========================================================================*/
 857 /* evaluate cc_ReadError code */ 
 858 static int sta2err(int sta)
     /* [previous][next][first][last][top][bottom][index][help] */
 859 {
 860         if (famT_drive)
 861         {
 862                 if (sta==0x00) return (0);
 863                 if (sta==0x01) return (-604); /* CRC error */
 864                 if (sta==0x02) return (-602); /* drive not ready */
 865                 if (sta==0x03) return (-607); /* unknown media */
 866                 if (sta==0x04) return (-612); /* general failure */
 867                 if (sta==0x05) return (0);
 868                 if (sta==0x06) return (-615); /* invalid disk change */
 869                 if (sta==0x0b) return (-612); /* general failure */
 870                 if (sta==0xff) return (-612); /* general failure */
 871                 return (0);
 872         }
 873         else
 874         {
 875                 if (sta<=2) return (sta);
 876                 if (sta==0x05) return (-604); /* CRC error */
 877                 if (sta==0x06) return (-606); /* seek error */
 878                 if (sta==0x0d) return (-606); /* seek error */
 879                 if (sta==0x0e) return (-603); /* unknown command */
 880                 if (sta==0x14) return (-603); /* unknown command */
 881                 if (sta==0x0c) return (-611); /* read fault */
 882                 if (sta==0x0f) return (-611); /* read fault */
 883                 if (sta==0x10) return (-611); /* read fault */
 884                 if (sta>=0x16) return (-612); /* general failure */
 885                 D_S[d].CD_changed=0xFF;
 886                 if (sta==0x11) return (-615); /* invalid disk change (LCS: removed) */
 887                 if (famL_drive)
 888                         if (sta==0x12) return (-615); /* invalid disk change (inserted) */
 889                 return (-602); /* drive not ready */
 890         }
 891 }
 892 /*==========================================================================*/
 893 static INLINE void clr_cmdbuf(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 894 {
 895         int i;
 896         
 897         for (i=0;i<10;i++) drvcmd[i]=0;
 898         cmd_type=0;
 899 }
 900 /*==========================================================================*/
 901 static void flush_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 902 {
 903         int i;
 904         
 905         sbp_sleep(15*HZ/10);
 906         for (i=maxtim_data;i!=0;i--) inb(CDi_status);
 907 }
 908 /*==========================================================================*/
 909 static int CDi_stat_loop(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 910 {
 911         int i,j;
 912         
 913         for(timeout = jiffies + 10*HZ, i=maxtim_data; timeout > jiffies; )
 914         {
 915                 for ( ;i!=0;i--)
 916                 {
 917                         j=inb(CDi_status);
 918                         if (!(j&s_not_data_ready)) return (j);
 919                         if (!(j&s_not_result_ready)) return (j);
 920                         if (fam0L_drive) if (j&s_attention) return (j);
 921                 }
 922                 sbp_sleep(1);
 923                 i = 1;
 924         }
 925         msg(DBG_LCS,"CDi_stat_loop failed\n");
 926         return (-1);
 927 }
 928 /*==========================================================================*/
 929 #if 00000
 930 /*==========================================================================*/
 931 static int tst_DataReady(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 932 {
 933         int i;
 934         
 935         i=inb(CDi_status);
 936         if (i&s_not_data_ready) return (0);
 937         return (1);
 938 }
 939 /*==========================================================================*/
 940 static int tst_ResultReady(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 941 {
 942         int i;
 943         
 944         i=inb(CDi_status);
 945         if (i&s_not_result_ready) return (0);
 946         return (1);
 947 }
 948 /*==========================================================================*/
 949 static int tst_Attention(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 950 {
 951         int i;
 952         
 953         i=inb(CDi_status);
 954         if (i&s_attention) return (1);
 955         return (0);
 956 }
 957 /*==========================================================================*/
 958 #endif 00000
 959 /*==========================================================================*/
 960 static int ResponseInfo(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 961 {
 962         int i,j,st=0;
 963         u_long timeout;
 964         
 965         for (i=0,timeout=jiffies+HZ;i<response_count;i++) 
 966         {
 967                 for (j=maxtim_data; ; )
 968                 {
 969                         for ( ;j!=0;j-- )
 970                         {
 971                                 st=inb(CDi_status);
 972                                 if (!(st&s_not_result_ready)) break;
 973                         }
 974                         if ((j!=0)||(timeout<=jiffies)) break;
 975                         sbp_sleep(1);
 976                         j = 1;
 977                 }
 978                 if (timeout<=jiffies) break;
 979                 infobuf[i]=inb(CDi_info);
 980         }
 981 #if 000
 982         while (!(inb(CDi_status)&s_not_result_ready))
 983         {
 984                 infobuf[i++]=inb(CDi_info);
 985         }
 986         j=i-response_count;
 987         if (j>0) msg(DBG_INF,"ResponseInfo: got %d trailing bytes.\n",j);
 988 #endif 000
 989         for (j=0;j<i;j++)
 990                 sprintf(&msgbuf[j*3]," %02X",infobuf[j]);
 991         msgbuf[j*3]=0;
 992         msg(DBG_CMD,"ResponseInfo:%s (%d,%d)\n",msgbuf,response_count,i);
 993         j=response_count-i;
 994         if (j>0) return (-j);
 995         else return (i);
 996 }
 997 /*==========================================================================*/
 998 static void EvaluateStatus(int st)
     /* [previous][next][first][last][top][bottom][index][help] */
 999 {
1000         D_S[d].status_bits=0;
1001         if (fam1_drive) D_S[d].status_bits=st|p_success;
1002         else if (fam0_drive)
1003         {
1004                 if (st&p_caddin_old) D_S[d].status_bits |= p_door_closed|p_caddy_in;
1005                 if (st&p_spinning) D_S[d].status_bits |= p_spinning;
1006                 if (st&p_check) D_S[d].status_bits |= p_check;
1007                 if (st&p_success_old) D_S[d].status_bits |= p_success;
1008                 if (st&p_busy_old) D_S[d].status_bits |= p_busy_new;
1009                 if (st&p_disk_ok) D_S[d].status_bits |= p_disk_ok;
1010         }
1011         else if (famL_drive)
1012         {
1013                 D_S[d].status_bits |= p_success;
1014                 if (st&p_caddin_old) D_S[d].status_bits |= p_disk_ok|p_caddy_in;
1015                 if (st&p_spinning) D_S[d].status_bits |= p_spinning;
1016                 if (st&p_check) D_S[d].status_bits |= p_check;
1017                 if (st&p_busy_old) D_S[d].status_bits |= p_busy_new;
1018                 if (st&p_lcs_door_closed) D_S[d].status_bits |= p_door_closed;
1019                 if (st&p_lcs_door_locked) D_S[d].status_bits |= p_door_locked;
1020         }
1021         else if (fam2_drive)
1022         {
1023                 D_S[d].status_bits |= p_success;
1024                 if (st&p2_check) D_S[d].status_bits |= p1_check;
1025                 if (st&p2_door_closed) D_S[d].status_bits |= p1_door_closed;
1026                 if (st&p2_disk_in) D_S[d].status_bits |= p1_disk_in;
1027                 if (st&p2_busy1) D_S[d].status_bits |= p1_busy;
1028                 if (st&p2_busy2) D_S[d].status_bits |= p1_busy;
1029                 if (st&p2_spinning) D_S[d].status_bits |= p1_spinning;
1030                 if (st&p2_door_locked) D_S[d].status_bits |= p1_door_locked;
1031                 if (st&p2_disk_ok) D_S[d].status_bits |= p1_disk_ok;
1032         }
1033         else if (famT_drive)
1034         {
1035                 return; /* still needs to get coded */
1036                 D_S[d].status_bits |= p_success;
1037                 if (st&p2_check) D_S[d].status_bits |= p1_check;
1038                 if (st&p2_door_closed) D_S[d].status_bits |= p1_door_closed;
1039                 if (st&p2_disk_in) D_S[d].status_bits |= p1_disk_in;
1040                 if (st&p2_busy1) D_S[d].status_bits |= p1_busy;
1041                 if (st&p2_busy2) D_S[d].status_bits |= p1_busy;
1042                 if (st&p2_spinning) D_S[d].status_bits |= p1_spinning;
1043                 if (st&p2_door_locked) D_S[d].status_bits |= p1_door_locked;
1044                 if (st&p2_disk_ok) D_S[d].status_bits |= p1_disk_ok;
1045         }
1046         return;
1047 }
1048 /*==========================================================================*/
1049 static int get_state_T(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1050 {
1051         int i;
1052         
1053         static int cmd_out_T(void);
1054 
1055         msg(DBG_TE2,"doing get_state_T...\n");
1056         clr_cmdbuf();
1057         D_S[d].n_bytes=1;
1058         drvcmd[0]=CMDT_STATUS;
1059         i=cmd_out_T();
1060         if (i>=0) i=infobuf[0];
1061         else
1062         {
1063                 msg(DBG_TEA,"get_state_T error %d\n", i);
1064                 return (i);
1065         }
1066         if (i>=0)
1067                 /* 2: closed, disk in */
1068                 D_S[d].status_bits=p1_door_closed|p1_disk_in|p1_spinning|p1_disk_ok;
1069         else if (D_S[d].error_state==6)
1070                 /* 3: closed, disk in, changed ("06 xx xx") */
1071                 D_S[d].status_bits=p1_door_closed|p1_disk_in;
1072         else if ((D_S[d].error_state!=2)||(D_S[d].b3!=0x3A)||(D_S[d].b4==0x00))
1073         {
1074                 /* 1: closed, no disk ("xx yy zz"or "02 3A 00") */
1075                 D_S[d].status_bits=p1_door_closed;
1076                 D_S[d].open_count=0;
1077         }
1078         else if (D_S[d].b4==0x01)
1079         {
1080                 /* 0: open ("02 3A 01") */
1081                 D_S[d].status_bits=0;
1082                 D_S[d].open_count=0;
1083         }
1084         else
1085         {
1086                 /* 1: closed, no disk ("02 3A xx") */
1087                 D_S[d].status_bits=p1_door_closed;
1088                 D_S[d].open_count=0;
1089         }
1090         msg(DBG_TE2,"get_state_T done (%02X)...\n", D_S[d].status_bits);
1091         return (D_S[d].status_bits);
1092 }
1093 /*==========================================================================*/
1094 static int ResponseStatus(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1095 {
1096         int i,j;
1097         u_long timeout;
1098         
1099         msg(DBG_STA,"doing ResponseStatus...\n");
1100         if (famT_drive) return (get_state_T());
1101         if (flags_cmd_out & f_respo3) timeout = jiffies;
1102         else if (flags_cmd_out & f_respo2) timeout = jiffies + 16*HZ;
1103         else timeout = jiffies + 4*HZ;
1104         j=maxtim_8;
1105         do
1106         {
1107                 for ( ;j!=0;j--)
1108                 { 
1109                         i=inb(CDi_status);
1110                         if (!(i&s_not_result_ready)) break;
1111                 }
1112                 if ((j!=0)||(timeout<jiffies)) break;
1113                 sbp_sleep(1);
1114                 j = 1;
1115         }
1116         while (1);
1117         if (j==0) 
1118         {
1119                 if ((flags_cmd_out & f_respo3) == 0)
1120                         msg(DBG_STA,"ResponseStatus: timeout.\n");
1121                 D_S[d].status_bits=0;
1122                 return (-401);
1123         }
1124         i=inb(CDi_info);
1125         msg(DBG_STA,"ResponseStatus: response %02X.\n", i);
1126         EvaluateStatus(i);
1127         msg(DBG_STA,"status_bits=%02X, i=%02X\n",D_S[d].status_bits,i);
1128         return (D_S[d].status_bits);
1129 }
1130 /*==========================================================================*/
1131 static void cc_ReadStatus(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1132 {
1133         int i;
1134         
1135         msg(DBG_STA,"giving cc_ReadStatus command\n");
1136         if (famT_drive) return;
1137         SBPCD_CLI;
1138         if (fam0L_drive) OUT(CDo_command,CMD0_STATUS);
1139         else if (fam1_drive) OUT(CDo_command,CMD1_STATUS);
1140         else if (fam2_drive) OUT(CDo_command,CMD2_STATUS);
1141         if (!fam0L_drive) for (i=0;i<6;i++) OUT(CDo_command,0);
1142         SBPCD_STI;
1143 }
1144 /*==========================================================================*/
1145 static int cc_ReadError(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1146 {
1147         int i;
1148 
1149         clr_cmdbuf();
1150         msg(DBG_ERR,"giving cc_ReadError command.\n");
1151         if (fam1_drive)
1152         {
1153                 drvcmd[0]=CMD1_READ_ERR;
1154                 response_count=8;
1155                 flags_cmd_out=f_putcmd|f_ResponseStatus;
1156         }
1157         else if (fam0L_drive)
1158         {
1159                 drvcmd[0]=CMD0_READ_ERR;
1160                 response_count=6;
1161                 if (famL_drive)
1162                         flags_cmd_out=f_putcmd;
1163                 else
1164                         flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus;
1165         }
1166         else if (fam2_drive)
1167         {
1168                 drvcmd[0]=CMD2_READ_ERR;
1169                 response_count=6;
1170                 flags_cmd_out=f_putcmd;
1171         }
1172         else if (famT_drive)
1173         {
1174                 response_count=5;
1175                 drvcmd[0]=CMDT_READ_ERR;
1176         }
1177         i=cmd_out();
1178         D_S[d].error_byte=0;
1179         msg(DBG_ERR,"cc_ReadError: cmd_out(CMDx_READ_ERR) returns %d (%02X)\n",i,i);
1180         if (i<0) return (i);
1181         if (fam0_drive) i=1;
1182         else i=2;
1183         D_S[d].error_byte=infobuf[i];
1184         msg(DBG_ERR,"cc_ReadError: infobuf[%d] is %d (%02X)\n",i,D_S[d].error_byte,D_S[d].error_byte);
1185         i=sta2err(infobuf[i]);
1186         return (i);
1187 }
1188 /*==========================================================================*/
1189 static int cmd_out_T(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1190 {
1191 #undef CMDT_TRIES
1192 #define CMDT_TRIES 1000
1193         
1194         static int cc_DriveReset(void);
1195         int i, j, l, ntries;
1196         
1197         D_S[d].error_state=0;
1198         D_S[d].b3=0;
1199         D_S[d].b4=0;
1200         D_S[d].f_drv_error=0;
1201         for (i=0;i<10;i++) sprintf(&msgbuf[i*3]," %02X",drvcmd[i]);
1202         msgbuf[i*3]=0;
1203         msg(DBG_CMD,"cmd_out_T:%s\n",msgbuf);
1204 
1205         OUT(CDo_sel_i_d,0);
1206         OUT(CDo_enable,D_S[d].drv_sel);
1207         i=inb(CDi_status);
1208         if (!(i&s_not_result_ready))
1209         do
1210         {
1211                 j=inb(CDi_info);
1212                 i=inb(CDi_status);
1213                 sbp_sleep(0);
1214                 msg(DBG_TEA,"cmd_out_T: spurious !s_not_result_ready. (%02X)\n", j);
1215         }
1216         while (!(i&s_not_result_ready));
1217         cli();
1218         for (i=0;i<10;i++) OUT(CDo_command,drvcmd[i]);
1219         sti();
1220         for (ntries=CMDT_TRIES;ntries>0;ntries--)
1221         {
1222                 if (drvcmd[0]==CMDT_READ_VER) sbp_sleep(HZ);
1223 #if 1
1224                 OUT(CDo_sel_i_d,0);
1225 #endif
1226                 i=inb(CDi_status);
1227                 if (!(i&s_not_data_ready)) /* f.e. CMDT_DISKINFO */
1228                 {
1229                         OUT(CDo_sel_i_d,1);
1230                         if (drvcmd[0]==CMDT_READ) return (0); /* handled elsewhere */
1231                         if (drvcmd[0]==CMDT_DISKINFO)
1232                         {
1233                                 l=0;
1234                                 do
1235                                 {
1236                                         infobuf[l++]=inb(CDi_data);
1237                                         i=inb(CDi_status);
1238                                 }
1239                                 while (!(i&s_not_data_ready));
1240                                 for (j=0;j<l;j++) sprintf(&msgbuf[j*3]," %02X",infobuf[j]);
1241                                 msgbuf[j*3]=0;
1242                                 msg(DBG_CMD,"cmd_out_T data response:%s\n", msgbuf);
1243                         }
1244                         else
1245                         {
1246                                 msg(DBG_TEA,"cmd_out_T: data response with cmd_%02X !!!!!!!!!!!!!!!!!!!!\n", drvcmd[0]);
1247                                 j=0;
1248                                 do
1249                                 {
1250                                         i=inb(CDi_data);
1251                                         j++;
1252                                         i=inb(CDi_status);
1253                                 }
1254                                 while (!(i&s_not_data_ready));
1255                                 msg(DBG_TEA,"cmd_out_T: data response: discarded %d bytes.\n", j);
1256                                 fatal_err++;
1257                         }
1258                 }
1259                 i=inb(CDi_status);
1260                 if (!(i&s_not_result_ready))
1261                 {
1262                         OUT(CDo_sel_i_d,0);
1263                         l=0;
1264                         do
1265                         {
1266                                 infobuf[l++]=inb(CDi_info);
1267                                 i=inb(CDi_status);
1268                         }
1269                         while (!(i&s_not_result_ready));
1270                         for (j=0;j<l;j++) sprintf(&msgbuf[j*3]," %02X",infobuf[j]);
1271                         msgbuf[j*3]=0;
1272                         msg(DBG_CMD,"cmd_out_T info response:%s\n", msgbuf);
1273                         if (infobuf[0]!=0x02) return (l); /* info length */
1274                         do
1275                         {
1276                                 ++recursion;
1277                                 if (recursion>1) msg(DBG_TEA,"cmd_out_T READ_ERR recursion (%02X): %d !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", drvcmd[0], recursion);
1278                                 clr_cmdbuf();
1279                                 drvcmd[0]=CMDT_READ_ERR;
1280                                 j=cmd_out_T(); /* !!! recursive here !!! */
1281                                 --recursion;
1282                                 sbp_sleep(1);
1283                         }
1284                         while (j<0);
1285                         D_S[d].error_state=infobuf[2];
1286                         D_S[d].b3=infobuf[3];
1287                         D_S[d].b4=infobuf[4];
1288                         if (D_S[d].f_drv_error)
1289                         {
1290                                 D_S[d].f_drv_error=0;
1291                                 cc_DriveReset();
1292                                 D_S[d].error_state=2;
1293                         }
1294                         return (-D_S[d].error_state-400);
1295                 }
1296                 if (drvcmd[0]==CMDT_READ) return (0); /* handled elsewhere */
1297                 sbp_sleep(HZ/10);
1298                 if (ntries>(CMDT_TRIES-50)) continue;
1299                 msg(DBG_TEA,"cmd_out_T: next CMDT_TRIES (%02X): %d.\n", drvcmd[0], ntries-1);
1300         }
1301         D_S[d].f_drv_error=1;
1302         cc_DriveReset();
1303         D_S[d].error_state=2;
1304         return (-99);
1305 }
1306 /*==========================================================================*/
1307 static int cmd_out(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1308 {
1309         int i=0;
1310         
1311         if (famT_drive) return(cmd_out_T());
1312         
1313         if (flags_cmd_out&f_putcmd)
1314         { 
1315                 for (i=0;i<7;i++)
1316                         sprintf(&msgbuf[i*3], " %02X", drvcmd[i]);
1317                 msgbuf[i*3]=0;
1318                 msg(DBG_CMD,"cmd_out:%s\n", msgbuf);
1319                 cli();
1320                 for (i=0;i<7;i++) OUT(CDo_command,drvcmd[i]);
1321                 sti();
1322         }
1323         if (response_count!=0)
1324         {
1325                 if (cmd_type!=0)
1326                 {
1327                         if (sbpro_type==1) OUT(CDo_sel_i_d,1);
1328                         msg(DBG_INF,"misleaded to try ResponseData.\n");
1329                         if (sbpro_type==1) OUT(CDo_sel_i_d,0);
1330                         return (-22);
1331                 }
1332                 else i=ResponseInfo();
1333                 if (i<0) return (i);
1334         }
1335         if (D_S[d].in_SpinUp) msg(DBG_SPI,"in_SpinUp: to CDi_stat_loop.\n");
1336         if (flags_cmd_out&f_lopsta)
1337         {
1338                 i=CDi_stat_loop();
1339                 if ((i<0)||!(i&s_attention)) return (-8);
1340         }
1341         if (!(flags_cmd_out&f_getsta)) goto LOC_229;
1342         
1343  LOC_228:
1344         if (D_S[d].in_SpinUp) msg(DBG_SPI,"in_SpinUp: to cc_ReadStatus.\n");
1345         cc_ReadStatus();
1346         
1347  LOC_229:
1348         if (flags_cmd_out&f_ResponseStatus) 
1349         {
1350                 if (D_S[d].in_SpinUp) msg(DBG_SPI,"in_SpinUp: to ResponseStatus.\n");
1351                 i=ResponseStatus();
1352                 /* builds status_bits, returns orig. status or p_busy_new */
1353                 if (i<0) return (i);
1354                 if (flags_cmd_out&(f_bit1|f_wait_if_busy))
1355                 {
1356                         if (!st_check)
1357                         {
1358                                 if ((flags_cmd_out&f_bit1)&&(i&p_success)) goto LOC_232;
1359                                 if ((!(flags_cmd_out&f_wait_if_busy))||(!st_busy)) goto LOC_228;
1360                         }
1361                 }
1362         }
1363  LOC_232:
1364         if (!(flags_cmd_out&f_obey_p_check)) return (0);
1365         if (!st_check) return (0);
1366         if (D_S[d].in_SpinUp) msg(DBG_SPI,"in_SpinUp: to cc_ReadError.\n");
1367         i=cc_ReadError();
1368         if (D_S[d].in_SpinUp) msg(DBG_SPI,"in_SpinUp: to cmd_out OK.\n");
1369         msg(DBG_000,"cmd_out: cc_ReadError=%d\n", i);
1370         return (i);
1371 }
1372 /*==========================================================================*/
1373 static int cc_Seek(u_int pos, char f_blk_msf)
     /* [previous][next][first][last][top][bottom][index][help] */
1374 {
1375         int i;
1376         
1377   clr_cmdbuf();
1378         if (f_blk_msf>1) return (-3);
1379         if (fam0_drive)
1380         {
1381                 drvcmd[0]=CMD0_SEEK;
1382                 if (f_blk_msf==1) pos=msf2blk(pos);
1383                 drvcmd[2]=(pos>>16)&0x00FF;
1384                 drvcmd[3]=(pos>>8)&0x00FF;
1385                 drvcmd[4]=pos&0x00FF;
1386                 flags_cmd_out = f_putcmd | f_respo2 | f_lopsta | f_getsta |
1387                         f_ResponseStatus | f_obey_p_check | f_bit1;
1388         }
1389         else if (fam1L_drive)
1390         {
1391                 drvcmd[0]=CMD1_SEEK; /* same as CMD1_ and CMDL_ */
1392                 if (f_blk_msf==0) pos=blk2msf(pos);
1393                 drvcmd[1]=(pos>>16)&0x00FF;
1394                 drvcmd[2]=(pos>>8)&0x00FF;
1395                 drvcmd[3]=pos&0x00FF;
1396                 if (famL_drive)
1397                         flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check|f_bit1;
1398                 else
1399                         flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1400         }
1401         else if (fam2_drive)
1402         {
1403                 drvcmd[0]=CMD2_SEEK;
1404                 if (f_blk_msf==0) pos=blk2msf(pos);
1405                 drvcmd[2]=(pos>>24)&0x00FF;
1406                 drvcmd[3]=(pos>>16)&0x00FF;
1407                 drvcmd[4]=(pos>>8)&0x00FF;
1408                 drvcmd[5]=pos&0x00FF;
1409                 flags_cmd_out=f_putcmd|f_ResponseStatus;
1410         }
1411         else if (famT_drive)
1412         {
1413                 drvcmd[0]=CMDT_SEEK;
1414                 if (f_blk_msf==1) pos=msf2blk(pos);
1415                 drvcmd[2]=(pos>>24)&0x00FF;
1416                 drvcmd[3]=(pos>>16)&0x00FF;
1417                 drvcmd[4]=(pos>>8)&0x00FF;
1418                 drvcmd[5]=pos&0x00FF;
1419                 D_S[d].n_bytes=1;
1420         }
1421         response_count=0;
1422         i=cmd_out();
1423         return (i);
1424 }
1425 /*==========================================================================*/
1426 static int cc_SpinUp(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1427 {
1428         int i;
1429         
1430         msg(DBG_SPI,"SpinUp.\n");
1431         D_S[d].in_SpinUp = 1;
1432         clr_cmdbuf();
1433         if (fam0L_drive)
1434         {
1435                 drvcmd[0]=CMD0_SPINUP;
1436                 flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|
1437                         f_ResponseStatus|f_obey_p_check|f_bit1;
1438         }
1439         else if (fam1_drive)
1440         {
1441                 drvcmd[0]=CMD1_SPINUP;
1442                 flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1443         }
1444         else if (fam2_drive)
1445         {
1446                 drvcmd[0]=CMD2_TRAY_CTL;
1447                 drvcmd[4]=0x01; /* "spinup" */
1448                 flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1449         }
1450         else if (famT_drive)
1451         {
1452                 drvcmd[0]=CMDT_TRAY_CTL;
1453                 drvcmd[4]=0x03; /* "insert", it hopefully spins the drive up */
1454         }
1455         response_count=0;
1456         i=cmd_out();
1457         D_S[d].in_SpinUp = 0;
1458         return (i);
1459 }
1460 /*==========================================================================*/
1461 static int cc_SpinDown(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1462 {
1463         int i;
1464         
1465         if (fam0_drive) return (0);
1466         clr_cmdbuf();
1467         response_count=0;
1468         if (fam1_drive)
1469         {
1470                 drvcmd[0]=CMD1_SPINDOWN;
1471                 flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1472         }
1473         else if (fam2_drive)
1474         {
1475                 drvcmd[0]=CMD2_TRAY_CTL;
1476                 drvcmd[4]=0x02; /* "eject" */
1477                 flags_cmd_out=f_putcmd|f_ResponseStatus;
1478         }
1479         else if (famL_drive)
1480         {
1481                 drvcmd[0]=CMDL_SPINDOWN;
1482                 drvcmd[1]=1;
1483                 flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check|f_bit1;
1484         }
1485         else if (famT_drive)
1486         {
1487                 drvcmd[0]=CMDT_TRAY_CTL;
1488                 drvcmd[4]=0x02; /* "eject" */
1489         }
1490         i=cmd_out();
1491         return (i);
1492 }
1493 /*==========================================================================*/
1494 static int cc_get_mode_T(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1495 {
1496         int i;
1497         
1498         clr_cmdbuf();
1499         response_count=10;
1500         drvcmd[0]=CMDT_GETMODE;
1501         drvcmd[4]=response_count;
1502         i=cmd_out_T();
1503         return (i);
1504 }
1505 /*==========================================================================*/
1506 static int cc_set_mode_T(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1507 {
1508         int i;
1509         
1510         clr_cmdbuf();
1511         response_count=1;
1512         drvcmd[0]=CMDT_SETMODE;
1513         drvcmd[1]=D_S[d].speed_byte;
1514         drvcmd[2]=D_S[d].frmsiz>>8;
1515         drvcmd[3]=D_S[d].frmsiz&0x0FF;
1516         drvcmd[4]=D_S[d].f_XA; /* 1: XA */
1517         drvcmd[5]=D_S[d].type_byte; /* 0, 1, 3 */
1518         drvcmd[6]=D_S[d].mode_xb_6;
1519         drvcmd[7]=D_S[d].mode_yb_7|D_S[d].volume_control;
1520         drvcmd[8]=D_S[d].mode_xb_8;
1521         drvcmd[9]=D_S[d].delay;
1522         i=cmd_out_T();
1523         return (i);
1524 }
1525 /*==========================================================================*/
1526 static int cc_prep_mode_T(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1527 {
1528         int i, j;
1529         
1530         i=cc_get_mode_T();
1531         if (i<0) return (i);
1532         for (i=0;i<10;i++)
1533                 sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
1534         msgbuf[i*3]=0;
1535         msg(DBG_TEA,"CMDT_GETMODE:%s\n", msgbuf);
1536         D_S[d].speed_byte=0x02; /* 0x02: auto quad, 0x82: quad, 0x81: double, 0x80: single */
1537         D_S[d].frmsiz=make16(infobuf[2],infobuf[3]);
1538         D_S[d].f_XA=infobuf[4];
1539         if (D_S[d].f_XA==0) D_S[d].type_byte=0;
1540         else D_S[d].type_byte=1;
1541         D_S[d].mode_xb_6=infobuf[6];
1542         D_S[d].mode_yb_7=1;
1543         D_S[d].mode_xb_8=infobuf[8];
1544         D_S[d].delay=0; /* 0, 1, 2, 3 */
1545         j=cc_set_mode_T();
1546         i=cc_get_mode_T();
1547         for (i=0;i<10;i++)
1548                 sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
1549         msgbuf[i*3]=0;
1550         msg(DBG_TEA,"CMDT_GETMODE:%s\n", msgbuf);
1551         return (j);
1552 }
1553 /*==========================================================================*/
1554 static int cc_SetSpeed(u_char speed, u_char x1, u_char x2)
     /* [previous][next][first][last][top][bottom][index][help] */
1555 {
1556         int i;
1557         
1558         if (fam0L_drive) return (-3);
1559         clr_cmdbuf();
1560         response_count=0;
1561         if (fam1_drive)
1562         {
1563                 drvcmd[0]=CMD1_SETMODE;
1564                 drvcmd[1]=0x03;
1565                 drvcmd[2]=speed;
1566                 drvcmd[3]=x1;
1567                 drvcmd[4]=x2;
1568                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1569         }
1570         else if (fam2_drive)
1571         {
1572                 drvcmd[0]=CMD2_SETSPEED;
1573                 if (speed&speed_auto)
1574                 {
1575                         drvcmd[2]=0xFF;
1576                         drvcmd[3]=0xFF;
1577                 }
1578                 else
1579                 {
1580                         drvcmd[2]=0;
1581                         drvcmd[3]=150;
1582                 }
1583                 flags_cmd_out=f_putcmd|f_ResponseStatus;
1584         }
1585         else if (famT_drive)
1586         {
1587                 return (0);
1588         }
1589         i=cmd_out();
1590         return (i);
1591 }
1592 /*==========================================================================*/
1593 static int cc_SetVolume(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1594 {
1595         int i;
1596         u_char channel0,channel1,volume0,volume1;
1597         u_char control0,value0,control1,value1;
1598         
1599         D_S[d].diskstate_flags &= ~volume_bit;
1600         clr_cmdbuf();
1601         channel0=D_S[d].vol_chan0;
1602         volume0=D_S[d].vol_ctrl0;
1603         channel1=control1=D_S[d].vol_chan1;
1604         volume1=value1=D_S[d].vol_ctrl1;
1605         control0=value0=0;
1606         
1607         if (((D_S[d].drv_options&audio_mono)!=0)&&(D_S[d].drv_type>=drv_211))
1608         {
1609                 if ((volume0!=0)&&(volume1==0))
1610                 {
1611                         volume1=volume0;
1612                         channel1=channel0;
1613                 }
1614                 else if ((volume0==0)&&(volume1!=0))
1615                 {
1616                         volume0=volume1;
1617                         channel0=channel1;
1618                 }
1619         }
1620         if (channel0>1)
1621         {
1622                 channel0=0;
1623                 volume0=0;
1624         }
1625         if (channel1>1)
1626         {
1627                 channel1=1;
1628                 volume1=0;
1629         }
1630         
1631         if (fam1_drive)
1632         {
1633                 control0=channel0+1;
1634                 control1=channel1+1;
1635                 value0=(volume0>volume1)?volume0:volume1;
1636                 value1=value0;
1637                 if (volume0==0) control0=0;
1638                 if (volume1==0) control1=0;
1639                 drvcmd[0]=CMD1_SETMODE;
1640                 drvcmd[1]=0x05;
1641                 drvcmd[3]=control0;
1642                 drvcmd[4]=value0;
1643                 drvcmd[5]=control1;
1644                 drvcmd[6]=value1;
1645                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1646         }
1647         else if (fam2_drive)
1648         {
1649                 control0=channel0+1;
1650                 control1=channel1+1;
1651                 value0=(volume0>volume1)?volume0:volume1;
1652                 value1=value0;
1653                 if (volume0==0) control0=0;
1654                 if (volume1==0) control1=0;
1655                 drvcmd[0]=CMD2_SETMODE;
1656                 drvcmd[1]=0x0E;
1657                 drvcmd[3]=control0;
1658                 drvcmd[4]=value0;
1659                 drvcmd[5]=control1;
1660                 drvcmd[6]=value1;
1661                 flags_cmd_out=f_putcmd|f_ResponseStatus;
1662         }
1663         else if (famL_drive)
1664         {
1665                 if ((volume0==0)||(channel0!=0)) control0 |= 0x80;
1666                 if ((volume1==0)||(channel1!=1)) control0 |= 0x40;
1667                 if (volume0|volume1) value0=0x80;
1668                 drvcmd[0]=CMDL_SETMODE;
1669                 drvcmd[1]=0x03;
1670                 drvcmd[4]=control0;
1671                 drvcmd[5]=value0;
1672                 flags_cmd_out=f_putcmd|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check|f_bit1;
1673         }
1674         else if (fam0_drive) /* different firmware levels */
1675         {
1676                 if (D_S[d].drv_type>=drv_300)
1677                 {
1678                         control0=volume0&0xFC;
1679                         value0=volume1&0xFC;
1680                         if ((volume0!=0)&&(volume0<4)) control0 |= 0x04;
1681                         if ((volume1!=0)&&(volume1<4)) value0 |= 0x04;
1682                         if (channel0!=0) control0 |= 0x01;
1683                         if (channel1==1) value0 |= 0x01;
1684                 }
1685                 else
1686                 {
1687                         value0=(volume0>volume1)?volume0:volume1;
1688                         if (D_S[d].drv_type<drv_211)
1689                         {
1690                                 if (channel0!=0)
1691                                 {
1692                                         i=channel1;
1693                                         channel1=channel0;
1694                                         channel0=i;
1695                                         i=volume1;
1696                                         volume1=volume0;
1697                                         volume0=i;
1698                                 }
1699                                 if (channel0==channel1)
1700                                 {
1701                                         if (channel0==0)
1702                                         {
1703                                                 channel1=1;
1704                                                 volume1=0;
1705                                                 volume0=value0;
1706                                         }
1707                                         else
1708                                         {
1709                                                 channel0=0;
1710                                                 volume0=0;
1711                                                 volume1=value0;
1712                                         }
1713                                 }
1714                         }
1715                         
1716                         if ((volume0!=0)&&(volume1!=0))
1717                         {
1718                                 if (volume0==0xFF) volume1=0xFF;
1719                                 else if (volume1==0xFF) volume0=0xFF;
1720                         }
1721                         else if (D_S[d].drv_type<drv_201) volume0=volume1=value0;
1722                         
1723                         if (D_S[d].drv_type>=drv_201)
1724                         {
1725                                 if (volume0==0) control0 |= 0x80;
1726                                 if (volume1==0) control0 |= 0x40;
1727                         }
1728                         if (D_S[d].drv_type>=drv_211)
1729                         {
1730                                 if (channel0!=0) control0 |= 0x20;
1731                                 if (channel1!=1) control0 |= 0x10;
1732                         }
1733                 }
1734                 drvcmd[0]=CMD0_SETMODE;
1735                 drvcmd[1]=0x83;
1736                 drvcmd[4]=control0;
1737                 drvcmd[5]=value0;
1738                 flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
1739         }
1740         else if (famT_drive)
1741         {
1742                 D_S[d].volume_control=0;
1743                 if (!volume0) D_S[d].volume_control|=0x10;
1744                 if (!volume1) D_S[d].volume_control|=0x20;
1745                 i=cc_prep_mode_T();
1746                 if (i<0) return (i);
1747         }
1748         if (!famT_drive)
1749         {
1750                 response_count=0;
1751                 i=cmd_out();
1752                 if (i<0) return (i);
1753         }
1754         D_S[d].diskstate_flags |= volume_bit;
1755         return (0);
1756 }
1757 /*==========================================================================*/
1758 static int GetStatus(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1759 {
1760         int i;
1761         
1762         if (famT_drive) return (0);
1763         flags_cmd_out=f_getsta|f_ResponseStatus|f_obey_p_check;
1764         response_count=0;
1765         cmd_type=0;
1766         i=cmd_out();
1767         return (i);
1768 }
1769 /*==========================================================================*/
1770 static int cc_DriveReset(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1771 {
1772         int i;
1773         
1774         msg(DBG_RES,"cc_DriveReset called.\n");
1775         clr_cmdbuf();
1776         response_count=0;
1777         if (fam0L_drive) OUT(CDo_reset,0x00);
1778         else if (fam1_drive)
1779         {
1780                 drvcmd[0]=CMD1_RESET;
1781                 flags_cmd_out=f_putcmd;
1782                 i=cmd_out();
1783         }
1784         else if (fam2_drive)
1785         {
1786                 drvcmd[0]=CMD2_RESET;
1787                 flags_cmd_out=f_putcmd;
1788                 i=cmd_out();
1789                 OUT(CDo_reset,0x00);
1790         }
1791         else if (famT_drive)
1792         {
1793                 OUT(CDo_sel_i_d,0);
1794                 OUT(CDo_enable,D_S[d].drv_sel);
1795                 OUT(CDo_command,CMDT_RESET);
1796                 for (i=1;i<10;i++) OUT(CDo_command,0);
1797         }
1798         if (fam0L_drive) sbp_sleep(5*HZ); /* wait 5 seconds */
1799         else sbp_sleep(1*HZ); /* wait a second */
1800 #if 1
1801         if (famT_drive)
1802         {
1803                 msg(DBG_TEA, "================CMDT_RESET given=================.\n");
1804                 sbp_sleep(3*HZ);
1805         }
1806 #endif 1
1807         flush_status();
1808         i=GetStatus();
1809         if (i<0) return i;
1810         if (!famT_drive)
1811                 if (D_S[d].error_byte!=aud_12) return -501;
1812         return (0);
1813 }
1814 /*==========================================================================*/
1815 static int SetSpeed(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1816 {
1817         int i, speed;
1818         
1819         if (!(D_S[d].drv_options&(speed_auto|speed_300|speed_150))) return (0);
1820         speed=speed_auto;
1821         if (!(D_S[d].drv_options&speed_auto))
1822         {
1823                 speed |= speed_300;
1824                 if (!(D_S[d].drv_options&speed_300)) speed=0;
1825         }
1826         i=cc_SetSpeed(speed,0,0);
1827         return (i);
1828 }
1829 /*==========================================================================*/
1830 static int DriveReset(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1831 {
1832         int i;
1833         
1834         i=cc_DriveReset();
1835         if (i<0) return (-22);
1836         do
1837         {
1838                 i=GetStatus();
1839                 if ((i<0)&&(i!=-615)) return (-2); /* i!=-615 is from sta2err */
1840                 if (!st_caddy_in) break;
1841                 sbp_sleep(1);
1842         }
1843         while (!st_diskok);
1844 #if 000
1845         D_S[d].CD_changed=1;
1846 #endif
1847         if ((st_door_closed) && (st_caddy_in))
1848         {
1849                 i=DiskInfo();
1850                 if (i<0) return (-23);
1851         }
1852         return (0);
1853 }
1854 /*==========================================================================*/
1855 static int cc_PlayAudio(int pos_audio_start,int pos_audio_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1856 {
1857         int i, j, n;
1858         
1859         if (D_S[d].audio_state==audio_playing) return (-EINVAL);
1860         clr_cmdbuf();
1861         response_count=0;
1862         if (famL_drive)
1863         {
1864                 drvcmd[0]=CMDL_PLAY;
1865                 i=msf2blk(pos_audio_start);
1866                 n=msf2blk(pos_audio_end)+1-i;
1867                 drvcmd[1]=(i>>16)&0x00FF;
1868                 drvcmd[2]=(i>>8)&0x00FF;
1869                 drvcmd[3]=i&0x00FF;
1870                 drvcmd[4]=(n>>16)&0x00FF;
1871                 drvcmd[5]=(n>>8)&0x00FF;
1872                 drvcmd[6]=n&0x00FF;
1873                 flags_cmd_out = f_putcmd | f_respo2 | f_lopsta | f_getsta |
1874                         f_ResponseStatus | f_obey_p_check | f_wait_if_busy;
1875         }
1876         else
1877         {
1878                 j=1;
1879                 if (fam1_drive)
1880                 {
1881                         drvcmd[0]=CMD1_PLAY_MSF;
1882                         flags_cmd_out = f_putcmd | f_respo2 | f_ResponseStatus |
1883                                 f_obey_p_check | f_wait_if_busy;
1884                 }
1885                 else if (fam2_drive)
1886                 {
1887                         drvcmd[0]=CMD2_PLAY_MSF;
1888                         flags_cmd_out = f_putcmd | f_ResponseStatus;
1889                 }
1890                 else if (famT_drive)
1891                 {
1892                         drvcmd[0]=CMDT_PLAY_MSF;
1893                         j=3;
1894                         response_count=1;
1895                 }
1896                 else if (fam0_drive)
1897                 {
1898                         drvcmd[0]=CMD0_PLAY_MSF;
1899                         flags_cmd_out = f_putcmd | f_respo2 | f_lopsta | f_getsta |
1900                                 f_ResponseStatus | f_obey_p_check | f_wait_if_busy;
1901                 }
1902                 drvcmd[j]=(pos_audio_start>>16)&0x00FF;
1903                 drvcmd[j+1]=(pos_audio_start>>8)&0x00FF;
1904                 drvcmd[j+2]=pos_audio_start&0x00FF;
1905                 drvcmd[j+3]=(pos_audio_end>>16)&0x00FF;
1906                 drvcmd[j+4]=(pos_audio_end>>8)&0x00FF;
1907                 drvcmd[j+5]=pos_audio_end&0x00FF;
1908         }
1909         i=cmd_out();
1910         return (i);
1911 }
1912 /*==========================================================================*/
1913 static int cc_Pause_Resume(int pau_res)
     /* [previous][next][first][last][top][bottom][index][help] */
1914 {
1915         int i;
1916         
1917         clr_cmdbuf();
1918         response_count=0;
1919         if (fam1_drive)
1920         {
1921                 drvcmd[0]=CMD1_PAU_RES;
1922                 if (pau_res!=1) drvcmd[1]=0x80;
1923                 flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
1924         }
1925         else if (fam2_drive)
1926         {
1927                 drvcmd[0]=CMD2_PAU_RES;
1928                 if (pau_res!=1) drvcmd[2]=0x01;
1929                 flags_cmd_out=f_putcmd|f_ResponseStatus;
1930         }
1931         else if (fam0L_drive)
1932         {
1933                 drvcmd[0]=CMD0_PAU_RES;
1934                 if (pau_res!=1) drvcmd[1]=0x80;
1935                 if (famL_drive)
1936                         flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|f_ResponseStatus|
1937                                 f_obey_p_check|f_bit1;
1938                 else
1939                         flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|f_ResponseStatus|
1940                                 f_obey_p_check;
1941         }
1942         else if (famT_drive)
1943         {
1944                 if (pau_res==3) return (cc_PlayAudio(D_S[d].pos_audio_start,D_S[d].pos_audio_end));
1945                 else if (pau_res==1) drvcmd[0]=CMDT_PAUSE;
1946                 else return (-56);
1947         }
1948         i=cmd_out();
1949         return (i);
1950 }
1951 /*==========================================================================*/
1952 static int cc_LockDoor(char lock)
     /* [previous][next][first][last][top][bottom][index][help] */
1953 {
1954         int i;
1955         
1956         if (fam0_drive) return (0);
1957         msg(DBG_LCK,"cc_LockDoor: %d (drive %d)\n", lock, d);
1958         msg(DBG_LCS,"p_door_locked bit %d before\n", st_door_locked);
1959         clr_cmdbuf();
1960         response_count=0;
1961         if (fam1_drive)
1962         {
1963                 drvcmd[0]=CMD1_LOCK_CTL;
1964                 if (lock==1) drvcmd[1]=0x01;
1965                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
1966         }
1967         else if (fam2_drive)
1968         {
1969                 drvcmd[0]=CMD2_LOCK_CTL;
1970                 if (lock==1) drvcmd[4]=0x01;
1971                 flags_cmd_out=f_putcmd|f_ResponseStatus;
1972         }
1973         else if (famL_drive)
1974         {
1975                 drvcmd[0]=CMDL_LOCK_CTL;
1976                 if (lock==1) drvcmd[1]=0x01;
1977                 flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check|f_bit1;
1978         }
1979         else if (famT_drive)
1980         {
1981                 drvcmd[0]=CMDT_LOCK_CTL;
1982                 if (lock==1) drvcmd[4]=0x01;
1983         }
1984         i=cmd_out();
1985         msg(DBG_LCS,"p_door_locked bit %d after\n", st_door_locked);
1986         return (i);
1987 }
1988 /*==========================================================================*/
1989 /*==========================================================================*/
1990 static int UnLockDoor(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1991 {
1992         int i,j;
1993         
1994         j=20;
1995         do
1996         {
1997                 i=cc_LockDoor(0);
1998                 --j;
1999                 sbp_sleep(1);
2000         }
2001         while ((i<0)&&(j));
2002         if (i<0)
2003         {
2004                 cc_DriveReset();
2005                 return -84;
2006         }
2007         return (0);
2008 }
2009 /*==========================================================================*/
2010 static int LockDoor(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2011 {
2012         int i,j;
2013         
2014         j=20;
2015         do
2016         {
2017                 i=cc_LockDoor(1);
2018                 --j;
2019                 sbp_sleep(1);
2020         }
2021         while ((i<0)&&(j));
2022         if (j==0)
2023         {               
2024                 cc_DriveReset();
2025                 j=20;
2026                 do
2027                 {
2028                         i=cc_LockDoor(1);
2029                         --j;
2030                         sbp_sleep(1);
2031                 }
2032                 while ((i<0)&&(j));
2033         }
2034         return (i);
2035 }
2036 /*==========================================================================*/
2037 static int cc_CloseTray(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2038 {
2039         int i;
2040         
2041         if (fam0_drive) return (0);
2042         msg(DBG_LCK,"cc_CloseTray (drive %d)\n", d);
2043         msg(DBG_LCS,"p_door_closed bit %d before\n", st_door_closed);
2044         
2045         clr_cmdbuf();
2046         response_count=0;
2047         if (fam1_drive)
2048         {
2049                 drvcmd[0]=CMD1_TRAY_CTL;
2050                 flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
2051         }
2052         else if (fam2_drive)
2053         {
2054                 drvcmd[0]=CMD2_TRAY_CTL;
2055                 drvcmd[1]=0x01;
2056                 drvcmd[4]=0x03; /* "insert" */
2057                 flags_cmd_out=f_putcmd|f_ResponseStatus;
2058         }
2059         else if (famL_drive)
2060         {
2061                 drvcmd[0]=CMDL_TRAY_CTL;
2062                 flags_cmd_out=f_putcmd|f_respo2|f_lopsta|f_getsta|
2063                         f_ResponseStatus|f_obey_p_check|f_bit1;
2064         }
2065         else if (famT_drive)
2066         {
2067                 drvcmd[0]=CMDT_TRAY_CTL;
2068                 drvcmd[4]=0x03; /* "insert" */
2069         }
2070         i=cmd_out();
2071         msg(DBG_LCS,"p_door_closed bit %d after\n", st_door_closed);
2072         return (i);
2073 }
2074 /*==========================================================================*/
2075 static int cc_ReadSubQ(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2076 {
2077         int i,j;
2078 
2079         D_S[d].diskstate_flags &= ~subq_bit;
2080         for (j=255;j>0;j--)
2081         {
2082                 clr_cmdbuf();
2083                 if (fam1_drive)
2084                 {
2085                         drvcmd[0]=CMD1_READSUBQ;
2086                         flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2087                         response_count=11;
2088                 }
2089                 else if (fam2_drive)
2090                 {
2091                         drvcmd[0]=CMD2_READSUBQ;
2092                         drvcmd[1]=0x02;
2093                         drvcmd[3]=0x01;
2094                         flags_cmd_out=f_putcmd;
2095                         response_count=10;
2096                 }
2097                 else if (fam0L_drive)
2098                 {
2099                         drvcmd[0]=CMD0_READSUBQ;
2100                         drvcmd[1]=0x02;
2101                         if (famL_drive)
2102                                 flags_cmd_out=f_putcmd;
2103                         else
2104                                 flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2105                         response_count=13;
2106                 }
2107                 else if (famT_drive)
2108                 {
2109                         response_count=12;
2110                         drvcmd[0]=CMDT_READSUBQ;
2111                         drvcmd[1]=0x02;
2112                         drvcmd[2]=0x40;
2113                         drvcmd[3]=0x01;
2114                         drvcmd[8]=response_count;
2115                 }
2116                 i=cmd_out();
2117                 if (i<0) return (i);
2118                 for (i=0;i<response_count;i++)
2119                 {
2120                         sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
2121                         msgbuf[i*3]=0;
2122                         msg(DBG_SQ1,"cc_ReadSubQ:%s\n", msgbuf);
2123                 }
2124                 if (famT_drive) break;
2125                 if (infobuf[0]!=0) break;
2126                 if ((!st_spinning) || (j==1))
2127                 {
2128                         D_S[d].SubQ_ctl_adr=D_S[d].SubQ_trk=D_S[d].SubQ_pnt_idx=D_S[d].SubQ_whatisthis=0;
2129                         D_S[d].SubQ_run_tot=D_S[d].SubQ_run_trk=0;
2130                         return (0);
2131                 }
2132         }
2133         if (famT_drive) D_S[d].SubQ_ctl_adr=infobuf[1];
2134         else D_S[d].SubQ_ctl_adr=swap_nibbles(infobuf[1]);
2135         D_S[d].SubQ_trk=byt2bcd(infobuf[2]);
2136         D_S[d].SubQ_pnt_idx=byt2bcd(infobuf[3]);
2137         if (fam0L_drive) i=5;
2138         else if (fam12_drive) i=4;
2139         else if (famT_drive) i=8;
2140         D_S[d].SubQ_run_tot=make32(make16(0,infobuf[i]),make16(infobuf[i+1],infobuf[i+2])); /* msf-bin */
2141         i=7;
2142         if (fam0L_drive) i=9;
2143         else if (fam12_drive) i=7;
2144         else if (famT_drive) i=4;
2145         D_S[d].SubQ_run_trk=make32(make16(0,infobuf[i]),make16(infobuf[i+1],infobuf[i+2])); /* msf-bin */
2146         D_S[d].SubQ_whatisthis=infobuf[i+3];
2147         D_S[d].diskstate_flags |= subq_bit;
2148         return (0);
2149 }
2150 /*==========================================================================*/
2151 static int cc_ModeSense(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2152 {
2153         int i;
2154         
2155         if (fam2_drive) return (0);
2156         D_S[d].diskstate_flags &= ~frame_size_bit;
2157         clr_cmdbuf();
2158         if (fam1_drive)
2159         {
2160                 response_count=5;
2161                 drvcmd[0]=CMD1_GETMODE;
2162                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2163         }
2164         else if (fam0L_drive)
2165         {
2166                 response_count=2;
2167                 drvcmd[0]=CMD0_GETMODE;
2168                 if (famL_drive) flags_cmd_out=f_putcmd;
2169                 else flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2170         }
2171         else if (famT_drive)
2172         {
2173                 response_count=10;
2174                 drvcmd[0]=CMDT_GETMODE;
2175                 drvcmd[4]=response_count;
2176         }
2177         i=cmd_out();
2178         if (i<0) return (i);
2179         i=0;
2180         D_S[d].sense_byte=0;
2181         if (fam1_drive) D_S[d].sense_byte=infobuf[i++];
2182         else if (famT_drive)
2183         {
2184                 if (infobuf[4]==0x01) D_S[d].xa_byte=0x20;
2185                 else D_S[d].xa_byte=0;
2186                 i=2;
2187         }
2188         D_S[d].frame_size=make16(infobuf[i],infobuf[i+1]);
2189         for (i=0;i<response_count;i++)
2190                 sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
2191         msgbuf[i*3]=0;
2192         msg(DBG_XA1,"cc_ModeSense:%s\n", msgbuf);
2193         
2194         D_S[d].diskstate_flags |= frame_size_bit;
2195         return (0);
2196 }
2197 /*==========================================================================*/
2198 /*==========================================================================*/
2199 static int cc_ModeSelect(int framesize)
     /* [previous][next][first][last][top][bottom][index][help] */
2200 {
2201         int i;
2202         
2203         if (fam2_drive) return (0);
2204         D_S[d].diskstate_flags &= ~frame_size_bit;
2205         clr_cmdbuf();
2206         D_S[d].frame_size=framesize;
2207         if (framesize==CD_FRAMESIZE_RAW) D_S[d].sense_byte=0x82;
2208         else D_S[d].sense_byte=0x00;
2209         
2210         msg(DBG_XA1,"cc_ModeSelect: %02X %04X\n",
2211             D_S[d].sense_byte, D_S[d].frame_size);
2212         
2213         if (fam1_drive)
2214         {
2215                 drvcmd[0]=CMD1_SETMODE;
2216                 drvcmd[1]=0x00;
2217                 drvcmd[2]=D_S[d].sense_byte;
2218                 drvcmd[3]=(D_S[d].frame_size>>8)&0xFF;
2219                 drvcmd[4]=D_S[d].frame_size&0xFF;
2220                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2221         }
2222         else if (fam0L_drive)
2223         {
2224                 drvcmd[0]=CMD0_SETMODE;
2225                 drvcmd[1]=0x00;
2226                 drvcmd[2]=(D_S[d].frame_size>>8)&0xFF;
2227                 drvcmd[3]=D_S[d].frame_size&0xFF;
2228                 drvcmd[4]=0x00;
2229                 if(famL_drive)
2230                         flags_cmd_out=f_putcmd|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check;
2231                 else
2232                         flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2233         }
2234         else if (famT_drive)
2235         {
2236                 return (-1);
2237         }
2238         response_count=0;
2239         i=cmd_out();
2240         if (i<0) return (i);
2241         D_S[d].diskstate_flags |= frame_size_bit;
2242         return (0);
2243 }
2244 /*==========================================================================*/
2245 static int cc_GetVolume(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2246 {
2247         int i;
2248         u_char switches;
2249         u_char chan0=0;
2250         u_char vol0=0;
2251         u_char chan1=1;
2252         u_char vol1=0;
2253         
2254         D_S[d].diskstate_flags &= ~volume_bit;
2255         clr_cmdbuf();
2256         if (fam1_drive)
2257         {
2258                 drvcmd[0]=CMD1_GETMODE;
2259                 drvcmd[1]=0x05;
2260                 response_count=5;
2261                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2262         }
2263         else if (fam2_drive)
2264         {
2265                 drvcmd[0]=CMD2_GETMODE;
2266                 drvcmd[1]=0x0E;
2267                 response_count=5;
2268                 flags_cmd_out=f_putcmd;
2269         }
2270         else if (fam0L_drive)
2271         {
2272                 drvcmd[0]=CMD0_GETMODE;
2273                 drvcmd[1]=0x03;
2274                 response_count=2;
2275                 if(famL_drive)
2276                         flags_cmd_out=f_putcmd;
2277                 else
2278                         flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2279         }
2280         else if (famT_drive)
2281         {
2282                 i=cc_get_mode_T();
2283                 if (i<0) return (i);
2284         }
2285         if (!famT_drive)
2286         {
2287                 i=cmd_out();
2288                 if (i<0) return (i);
2289         }
2290         if (fam1_drive)
2291         {
2292                 chan0=infobuf[1]&0x0F;
2293                 vol0=infobuf[2];
2294                 chan1=infobuf[3]&0x0F;
2295                 vol1=infobuf[4];
2296                 if (chan0==0)
2297                 {
2298                         chan0=1;
2299                         vol0=0;
2300                 }
2301                 if (chan1==0)
2302                 {
2303                         chan1=2;
2304                         vol1=0;
2305                 }
2306                 chan0 >>= 1;
2307                 chan1 >>= 1;
2308         }
2309         else if (fam2_drive)
2310         {
2311                 chan0=infobuf[1];
2312                 vol0=infobuf[2];
2313                 chan1=infobuf[3];
2314                 vol1=infobuf[4];
2315         }
2316         else if (famL_drive)
2317         {
2318                 chan0=0;
2319                 chan1=1;
2320                 vol0=vol1=infobuf[1];
2321                 switches=infobuf[0];
2322                 if ((switches&0x80)!=0) chan0=1;
2323                 if ((switches&0x40)!=0) chan1=0;
2324         }
2325         else if (fam0_drive) /* different firmware levels */
2326         {
2327                 chan0=0;
2328                 chan1=1;
2329                 vol0=vol1=infobuf[1];
2330                 if (D_S[d].drv_type>=drv_201)
2331                 {
2332                         if (D_S[d].drv_type<drv_300)
2333                         {
2334                                 switches=infobuf[0];
2335                                 if ((switches&0x80)!=0) vol0=0;
2336                                 if ((switches&0x40)!=0) vol1=0;
2337                                 if (D_S[d].drv_type>=drv_211)
2338                                 {
2339                                         if ((switches&0x20)!=0) chan0=1;
2340                                         if ((switches&0x10)!=0) chan1=0;
2341                                 }
2342                         }
2343                         else
2344                         {
2345                                 vol0=infobuf[0];
2346                                 if ((vol0&0x01)!=0) chan0=1;
2347                                 if ((vol1&0x01)==0) chan1=0;
2348                                 vol0 &= 0xFC;
2349                                 vol1 &= 0xFC;
2350                                 if (vol0!=0) vol0 += 3;
2351                                 if (vol1!=0) vol1 += 3;
2352                         }
2353                 }
2354         }
2355         else if (famT_drive)
2356         {
2357                 D_S[d].volume_control=infobuf[7];
2358                 chan0=0;
2359                 chan1=1;
2360                 if (D_S[d].volume_control&0x10) vol0=0;
2361                 else vol0=0xff;
2362                 if (D_S[d].volume_control&0x20) vol1=0;
2363                 else vol1=0xff;
2364         }
2365         D_S[d].vol_chan0=chan0;
2366         D_S[d].vol_ctrl0=vol0;
2367         D_S[d].vol_chan1=chan1;
2368         D_S[d].vol_ctrl1=vol1;
2369 #if 000
2370         D_S[d].vol_chan2=2;
2371         D_S[d].vol_ctrl2=0xFF;
2372         D_S[d].vol_chan3=3;
2373         D_S[d].vol_ctrl3=0xFF;
2374 #endif 000
2375         D_S[d].diskstate_flags |= volume_bit;
2376         return (0);
2377 }
2378 /*==========================================================================*/
2379 static int cc_ReadCapacity(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2380 {
2381         int i, j;
2382         
2383         if (fam2_drive) return (0); /* some firmware lacks this command */
2384         if (famL_drive) return (0); /* some firmware lacks this command */
2385         if (famT_drive) return (0); /* done with cc_ReadTocDescr() */
2386         D_S[d].diskstate_flags &= ~cd_size_bit;
2387         for (j=3;j>0;j--)
2388         {
2389                 clr_cmdbuf();
2390                 if (fam1_drive)
2391                 {
2392                         drvcmd[0]=CMD1_CAPACITY;
2393                         response_count=5;
2394                         flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2395                 }
2396 #if 00
2397                 else if (fam2_drive)
2398                 {
2399                         drvcmd[0]=CMD2_CAPACITY;
2400                         response_count=8;
2401                         flags_cmd_out=f_putcmd;
2402                 }
2403 #endif
2404                 else if (fam0_drive)
2405                 {
2406                         drvcmd[0]=CMD0_CAPACITY;
2407                         response_count=5;
2408                         flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2409                 }
2410                 i=cmd_out();
2411                 if (i>=0) break;
2412                 msg(DBG_000,"cc_ReadCapacity: cmd_out: err %d\n", i);
2413                 cc_ReadError();
2414         }
2415         if (j==0) return (i);
2416         if (fam1_drive) D_S[d].CDsize_frm=msf2blk(make32(make16(0,infobuf[0]),make16(infobuf[1],infobuf[2])))+CD_MSF_OFFSET;
2417         else if (fam0_drive) D_S[d].CDsize_frm=make32(make16(0,infobuf[0]),make16(infobuf[1],infobuf[2]));
2418 #if 00
2419         else if (fam2_drive) D_S[d].CDsize_frm=make32(make16(infobuf[0],infobuf[1]),make16(infobuf[2],infobuf[3]));
2420 #endif
2421         D_S[d].diskstate_flags |= cd_size_bit;
2422         msg(DBG_000,"cc_ReadCapacity: %d frames.\n", D_S[d].CDsize_frm);
2423         return (0);
2424 }
2425 /*==========================================================================*/
2426 static int cc_ReadTocDescr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2427 {
2428         int i;
2429         
2430         D_S[d].diskstate_flags &= ~toc_bit;
2431         clr_cmdbuf();
2432         if (fam1_drive)
2433         {
2434                 drvcmd[0]=CMD1_DISKINFO;
2435                 response_count=6;
2436                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2437         }
2438         else if (fam0L_drive)
2439         {
2440                 drvcmd[0]=CMD0_DISKINFO;
2441                 response_count=6;
2442                 if(famL_drive)
2443                         flags_cmd_out=f_putcmd;
2444                 else
2445                         flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2446         }
2447         else if (fam2_drive)
2448         {
2449                 /* possibly longer timeout periods necessary */
2450                 D_S[d].f_multisession=0;
2451                 drvcmd[0]=CMD2_DISKINFO;
2452                 drvcmd[1]=0x02;
2453                 drvcmd[2]=0xAB;
2454                 drvcmd[3]=0xFF; /* session */
2455                 response_count=8;
2456                 flags_cmd_out=f_putcmd;
2457         }
2458         else if (famT_drive)
2459         {
2460                 D_S[d].f_multisession=0;
2461                 response_count=12;
2462                 drvcmd[0]=CMDT_DISKINFO;
2463                 drvcmd[1]=0x02;
2464                 drvcmd[6]=CDROM_LEADOUT;
2465                 drvcmd[8]=response_count;
2466                 drvcmd[9]=0x00;
2467         }
2468         i=cmd_out();
2469         if (i<0) return (i);
2470         if ((fam1_drive)||(fam2_drive)||(famL_drive)||(fam0_drive))
2471                 D_S[d].xa_byte=infobuf[0];
2472         if (fam2_drive)
2473         {
2474                 D_S[d].first_session=infobuf[1];
2475                 D_S[d].last_session=infobuf[2];
2476                 D_S[d].n_first_track=infobuf[3];
2477                 D_S[d].n_last_track=infobuf[4];
2478                 if (D_S[d].first_session!=D_S[d].last_session)
2479                 {
2480                         D_S[d].f_multisession=1;
2481                         D_S[d].lba_multi=msf2blk(make32(make16(0,infobuf[5]),make16(infobuf[6],infobuf[7])));
2482                 }
2483 #if 0
2484                 if (D_S[d].first_session!=D_S[d].last_session)
2485                 {
2486                         if (D_S[d].last_session<=20)
2487                                 zwanzig=D_S[d].last_session+1;
2488                         else zwanzig=20;
2489                         for (count=D_S[d].first_session;count<zwanzig;count++)
2490                         {
2491                                 drvcmd[0]=CMD2_DISKINFO;
2492                                 drvcmd[1]=0x02;
2493                                 drvcmd[2]=0xAB;
2494                                 drvcmd[3]=count;
2495                                 response_count=8;
2496                                 flags_cmd_out=f_putcmd;
2497                                 i=cmd_out();
2498                                 if (i<0) return (i);
2499                                 D_S[d].msf_multi_n[count]=make32(make16(0,infobuf[5]),make16(infobuf[6],infobuf[7]));
2500                         }
2501                         D_S[d].diskstate_flags |= multisession_bit;
2502                 }
2503 #endif
2504                 drvcmd[0]=CMD2_DISKINFO;
2505                 drvcmd[1]=0x02;
2506                 drvcmd[2]=0xAA;
2507                 drvcmd[3]=0xFF;
2508                 response_count=5;
2509                 flags_cmd_out=f_putcmd;
2510                 i=cmd_out();
2511                 if (i<0) return (i);
2512                 D_S[d].size_msf=make32(make16(0,infobuf[2]),make16(infobuf[3],infobuf[4]));
2513                 D_S[d].size_blk=msf2blk(D_S[d].size_msf);
2514                 D_S[d].CDsize_frm=D_S[d].size_blk+1;
2515         }
2516         else if (famT_drive)
2517         {
2518                 D_S[d].size_msf=make32(make16(infobuf[8],infobuf[9]),make16(infobuf[10],infobuf[11]));
2519                 D_S[d].size_blk=msf2blk(D_S[d].size_msf);
2520                 D_S[d].CDsize_frm=D_S[d].size_blk+1;
2521                 D_S[d].n_first_track=infobuf[2];
2522                 D_S[d].n_last_track=infobuf[3];
2523         }
2524         else
2525         {
2526                 D_S[d].n_first_track=infobuf[1];
2527                 D_S[d].n_last_track=infobuf[2];
2528                 D_S[d].size_msf=make32(make16(0,infobuf[3]),make16(infobuf[4],infobuf[5]));
2529                 D_S[d].size_blk=msf2blk(D_S[d].size_msf);
2530                 if (famL_drive) D_S[d].CDsize_frm=D_S[d].size_blk+1;
2531         }
2532         D_S[d].diskstate_flags |= toc_bit;
2533         msg(DBG_TOC,"TocDesc: %02X %02X %02X %08X\n",
2534             D_S[d].xa_byte,
2535             D_S[d].n_first_track,
2536             D_S[d].n_last_track,
2537             D_S[d].size_msf);
2538         return (0);
2539 }
2540 /*==========================================================================*/
2541 static int cc_ReadTocEntry(int num)
     /* [previous][next][first][last][top][bottom][index][help] */
2542 {
2543         int i;
2544         
2545         clr_cmdbuf();
2546         if (fam1_drive)
2547         {
2548                 drvcmd[0]=CMD1_READTOC;
2549                 drvcmd[2]=num;
2550                 response_count=8;
2551                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2552         }
2553         else if (fam2_drive)
2554         {
2555                 /* possibly longer timeout periods necessary */
2556                 drvcmd[0]=CMD2_DISKINFO;
2557                 drvcmd[1]=0x02;
2558                 drvcmd[2]=num;
2559                 response_count=5;
2560                 flags_cmd_out=f_putcmd;
2561         }
2562         else if (fam0L_drive)
2563         {
2564                 drvcmd[0]=CMD0_READTOC;
2565                 drvcmd[1]=0x02;
2566                 drvcmd[2]=num;
2567                 response_count=8;
2568                 if(famL_drive)
2569                         flags_cmd_out=f_putcmd;
2570                 else
2571         flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2572         }
2573         else if (famT_drive)
2574         {
2575                 response_count=12;
2576                 drvcmd[0]=CMDT_DISKINFO;
2577                 drvcmd[1]=0x02;
2578                 drvcmd[6]=num;
2579                 drvcmd[8]=response_count;
2580                 drvcmd[9]=0x00;
2581         }
2582         i=cmd_out();
2583         if (i<0) return (i);
2584         if ((fam1_drive)||(famL_drive)||(fam0_drive))
2585         {
2586                 D_S[d].TocEnt_nixbyte=infobuf[0];
2587                 i=1;
2588         }
2589         else if (fam2_drive) i=0;
2590         else if (famT_drive)
2591         {
2592                 i=5;
2593         }
2594         D_S[d].TocEnt_ctl_adr=swap_nibbles(infobuf[i++]);
2595         if ((fam1_drive)||(famL_drive)||(fam0_drive))
2596         {
2597                 D_S[d].TocEnt_number=infobuf[i++];
2598                 D_S[d].TocEnt_format=infobuf[i];
2599         }
2600         else D_S[d].TocEnt_number=num;
2601         if (fam1_drive) i=4;
2602         else if (fam0L_drive) i=5;
2603         else if (fam2_drive) i=2;
2604         else if (famT_drive) i=9;
2605         D_S[d].TocEnt_address=make32(make16(0,infobuf[i]),
2606                                      make16(infobuf[i+1],infobuf[i+2]));
2607         msg(DBG_TOC,"TocEntry: %02X %02X %02X %02X %08X\n",
2608             D_S[d].TocEnt_nixbyte, D_S[d].TocEnt_ctl_adr,
2609             D_S[d].TocEnt_number, D_S[d].TocEnt_format,
2610             D_S[d].TocEnt_address);
2611         return (0);
2612 }
2613 /*==========================================================================*/
2614 static int cc_ReadPacket(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2615 {
2616         int i;
2617         
2618         clr_cmdbuf();
2619         drvcmd[0]=CMD0_PACKET;
2620         drvcmd[1]=response_count;
2621         if(famL_drive) flags_cmd_out=f_putcmd;
2622         else if (fam01_drive)
2623                 flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus|f_obey_p_check;
2624         else if (fam2_drive) return (-1); /* not implemented yet */
2625         else if (famT_drive)
2626         {
2627                 return (-1);
2628         }
2629         i=cmd_out();
2630         return (i);
2631 }
2632 /*==========================================================================*/
2633 static int convert_UPC(u_char *p)
     /* [previous][next][first][last][top][bottom][index][help] */
2634 {
2635         int i;
2636         
2637         p++;
2638         if (fam0L_drive) p[13]=0;
2639         for (i=0;i<7;i++)
2640         {
2641                 if (fam1_drive) D_S[d].UPC_buf[i]=swap_nibbles(*p++);
2642                 else if (fam0L_drive)
2643                 {
2644                         D_S[d].UPC_buf[i]=((*p++)<<4)&0xFF;
2645                         D_S[d].UPC_buf[i] |= *p++;
2646                 }
2647                 else if (famT_drive)
2648                 {
2649                         return (-1);
2650                 }
2651                 else /* CD200 */
2652                 {
2653                         return (-1);
2654                 }
2655         }
2656         D_S[d].UPC_buf[6] &= 0xF0;
2657         return (0);
2658 }
2659 /*==========================================================================*/
2660 static int cc_ReadUPC(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2661 {
2662         int i;
2663 #if TEST_UPC
2664         int block, checksum;
2665 #endif TEST_UPC
2666         
2667         if (fam2_drive) return (0); /* not implemented yet */
2668         if (famT_drive) return (0); /* not implemented yet */
2669 #if 1
2670         if (fam0_drive) return (0); /* but it should work */
2671 #endif 1
2672         
2673         D_S[d].diskstate_flags &= ~upc_bit;
2674 #if TEST_UPC
2675         for (block=CD_MSF_OFFSET+1;block<CD_MSF_OFFSET+200;block++)
2676         {
2677 #endif TEST_UPC
2678                 clr_cmdbuf();
2679                 if (fam1_drive)
2680                 {
2681                         drvcmd[0]=CMD1_READ_UPC;
2682 #if TEST_UPC
2683                         drvcmd[1]=(block>>16)&0xFF;
2684                         drvcmd[2]=(block>>8)&0xFF;
2685                         drvcmd[3]=block&0xFF;
2686 #endif TEST_UPC
2687                         response_count=8;
2688                         flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2689                 }
2690                 else if (fam0L_drive)
2691                 {
2692                         drvcmd[0]=CMD0_READ_UPC;
2693 #if TEST_UPC
2694                         drvcmd[2]=(block>>16)&0xFF;
2695                         drvcmd[3]=(block>>8)&0xFF;
2696                         drvcmd[4]=block&0xFF;
2697 #endif TEST_UPC
2698                         response_count=0;
2699                         flags_cmd_out=f_putcmd|f_lopsta|f_getsta|f_ResponseStatus|f_obey_p_check|f_bit1;
2700                 }
2701                 else if (fam2_drive)
2702                 {
2703                         return (-1);
2704                 }
2705                 else if (famT_drive)
2706                 {
2707                         return (-1);
2708                 }
2709                 i=cmd_out();
2710                 if (i<0)
2711                 {
2712                         msg(DBG_000,"cc_ReadUPC cmd_out: err %d\n", i);
2713                         return (i);
2714                 }
2715                 if (fam0L_drive)
2716                 {
2717                         response_count=16;
2718                         if (famL_drive) flags_cmd_out=f_putcmd;
2719                         i=cc_ReadPacket();
2720                         if (i<0)
2721                         {
2722                                 msg(DBG_000,"cc_ReadUPC ReadPacket: err %d\n", i);
2723                                 return (i);
2724                         }
2725                 }
2726 #if TEST_UPC
2727                 checksum=0;
2728 #endif TEST_UPC
2729                 for (i=0;i<(fam1_drive?8:16);i++)
2730                 {
2731 #if TEST_UPC
2732                         checksum |= infobuf[i];
2733 #endif TEST_UPC
2734                         sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
2735                 }
2736                 msgbuf[i*3]=0;
2737                 msg(DBG_UPC,"UPC info:%s\n", msgbuf);
2738 #if TEST_UPC
2739                 if ((checksum&0x7F)!=0) break;
2740         }
2741 #endif TEST_UPC
2742         D_S[d].UPC_ctl_adr=0;
2743         if (fam1_drive) i=0;
2744         else i=2;
2745         if ((infobuf[i]&0x80)!=0)
2746         {
2747                 convert_UPC(&infobuf[i]);
2748                 D_S[d].UPC_ctl_adr = (D_S[d].TocEnt_ctl_adr & 0xF0) | 0x02;
2749         }
2750         for (i=0;i<7;i++)
2751                 sprintf(&msgbuf[i*3], " %02X", D_S[d].UPC_buf[i]);
2752         sprintf(&msgbuf[i*3], " (%02X)", D_S[d].UPC_ctl_adr);
2753         msgbuf[i*3+5]=0;
2754         msg(DBG_UPC,"UPC code:%s\n", msgbuf);
2755         D_S[d].diskstate_flags |= upc_bit;
2756         return (0);
2757 }
2758 /*==========================================================================*/
2759 static int cc_CheckMultiSession(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2760 {
2761         int i;
2762         
2763         if (fam2_drive) return (0);
2764         D_S[d].f_multisession=0;
2765         D_S[d].lba_multi=0;
2766         if (fam0_drive) return (0);
2767         clr_cmdbuf();
2768         if (fam1_drive)
2769         {
2770                 drvcmd[0]=CMD1_MULTISESS;
2771                 response_count=6;
2772                 flags_cmd_out=f_putcmd|f_ResponseStatus|f_obey_p_check;
2773                 i=cmd_out();
2774                 if (i<0) return (i);
2775                 if ((infobuf[0]&0x80)!=0)
2776                 {
2777                         D_S[d].f_multisession=1;
2778                         D_S[d].lba_multi=msf2blk(make32(make16(0,infobuf[1]),
2779                                                         make16(infobuf[2],infobuf[3])));
2780                 }
2781         }
2782         else if (famL_drive)
2783         {
2784                 drvcmd[0]=CMDL_MULTISESS;
2785                 drvcmd[1]=3;
2786                 drvcmd[2]=1;
2787                 response_count=8;
2788                 flags_cmd_out=f_putcmd;
2789                 i=cmd_out();
2790                 if (i<0) return (i);
2791                 D_S[d].lba_multi=msf2blk(make32(make16(0,infobuf[5]),
2792                                                 make16(infobuf[6],infobuf[7])));
2793         }
2794         else if (famT_drive)
2795         {
2796                 response_count=12;
2797                 drvcmd[0]=CMDT_DISKINFO;
2798                 drvcmd[1]=0x02;
2799                 drvcmd[6]=0;
2800                 drvcmd[8]=response_count;
2801                 drvcmd[9]=0x40;
2802                 i=cmd_out();
2803                 if (i<0) return (i);
2804                 D_S[d].first_session=infobuf[2];
2805                 D_S[d].last_session=infobuf[3];
2806                 D_S[d].track_of_last_session=infobuf[6];
2807                 if (D_S[d].first_session!=D_S[d].last_session)
2808                 {
2809                         D_S[d].f_multisession=1;
2810                         D_S[d].lba_multi=msf2blk(make32(make16(0,infobuf[9]),make16(infobuf[10],infobuf[11])));
2811                 }
2812         }
2813         for (i=0;i<response_count;i++)
2814                 sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
2815         msgbuf[i*3]=0;
2816         msg(DBG_MUL,"MultiSession Info:%s (%d)\n", msgbuf, D_S[d].lba_multi);
2817         if (D_S[d].lba_multi>200)
2818         {
2819                 D_S[d].f_multisession=1;
2820                 msg(DBG_MUL,"MultiSession base: %06X\n", D_S[d].lba_multi);
2821         }
2822         return (0);
2823 }
2824 /*==========================================================================*/
2825 #if FUTURE
2826 static int cc_SubChanInfo(int frame, int count, u_char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
2827         /* "frame" is a RED BOOK (msf-bin) address */
2828 {
2829         int i;
2830         
2831         if (fam0L_drive) return (-ENOSYS); /* drive firmware lacks it */
2832         if (famT_drive)
2833         {
2834                 return (-1);
2835         }
2836 #if 0
2837         if (D_S[d].audio_state!=audio_playing) return (-ENODATA);
2838 #endif
2839         clr_cmdbuf();
2840         drvcmd[0]=CMD1_SUBCHANINF;
2841         drvcmd[1]=(frame>>16)&0xFF;
2842         drvcmd[2]=(frame>>8)&0xFF;
2843         drvcmd[3]=frame&0xFF;
2844         drvcmd[5]=(count>>8)&0xFF;
2845         drvcmd[6]=count&0xFF;
2846         flags_cmd_out=f_putcmd|f_respo2|f_ResponseStatus|f_obey_p_check;
2847         cmd_type=READ_SC;
2848         D_S[d].frame_size=CD_FRAMESIZE_SUB;
2849         i=cmd_out(); /* which buffer to use? */
2850         return (i);
2851 }
2852 #endif FUTURE
2853 /*==========================================================================*/
2854 static void check_datarate(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2855 {
2856         int i=0;
2857         
2858         msg(DBG_IOX,"check_datarate entered.\n");
2859         datarate=0;
2860 #if TEST_STI
2861         for (i=0;i<=1000;i++) printk(".");
2862 #endif
2863         /* set a timer to make (timed_out_delay!=0) after 1.1 seconds */
2864 #if 1
2865         del_timer(&delay_timer);
2866 #endif
2867         delay_timer.expires=jiffies+11*HZ/10;
2868         timed_out_delay=0;
2869         add_timer(&delay_timer);
2870 #if 0
2871         msg(DBG_TIM,"delay timer started (11*HZ/10).\n");
2872 #endif
2873         do
2874         {
2875                 i=inb(CDi_status);
2876                 datarate++;
2877 #if 1
2878                 if (datarate>0x6FFFFFFF) break;
2879 #endif 00000
2880         }
2881         while (!timed_out_delay);
2882         del_timer(&delay_timer);
2883 #if 0
2884         msg(DBG_TIM,"datarate: %04X\n", datarate);
2885 #endif
2886         if (datarate<65536) datarate=65536;
2887         maxtim16=datarate*16;
2888         maxtim04=datarate*4;
2889         maxtim02=datarate*2;
2890         maxtim_8=datarate/32;
2891 #if LONG_TIMING
2892         maxtim_data=datarate/100;
2893 #else
2894         maxtim_data=datarate/300;
2895 #endif LONG_TIMING
2896 #if 0
2897         msg(DBG_TIM,"maxtim_8 %d, maxtim_data %d.\n", maxtim_8, maxtim_data);
2898 #endif
2899 }
2900 /*==========================================================================*/
2901 #if 0
2902 static int c2_ReadError(int fam)
     /* [previous][next][first][last][top][bottom][index][help] */
2903 {
2904         int i;
2905         
2906         clr_cmdbuf();
2907         response_count=9;
2908         clr_respo_buf(9);
2909         if (fam==1)
2910         {
2911                 drvcmd[0]=CMD0_READ_ERR; /* same as CMD1_ and CMDL_ */
2912                 i=do_cmd(f_putcmd|f_lopsta|f_getsta|f_ResponseStatus);
2913         }
2914         else if (fam==2)
2915         {
2916                 drvcmd[0]=CMD2_READ_ERR;
2917                 i=do_cmd(f_putcmd);
2918         }
2919         else return (-1);
2920         return (i);
2921 }
2922 #endif
2923 /*==========================================================================*/
2924 static void ask_mail(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2925 {
2926         int i;
2927         
2928         msg(DBG_INF, "please mail the following lines to emoenke@gwdg.de:\n");
2929         msg(DBG_INF, "%s\n", VERSION);
2930         msg(DBG_INF, "address %03X, type %s, drive %s (ID %d)\n",
2931             CDo_command, type, D_S[d].drive_model, D_S[d].drv_id);
2932         for (i=0;i<12;i++)
2933                 sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
2934         msgbuf[i*3]=0;
2935         msg(DBG_INF,"infobuf =%s\n", msgbuf);
2936         for (i=0;i<12;i++)
2937                 sprintf(&msgbuf[i*3], " %c ", infobuf[i]);
2938         msgbuf[i*3]=0;
2939         msg(DBG_INF,"infobuf =%s\n", msgbuf);
2940 }
2941 /*==========================================================================*/
2942 static int check_version(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2943 {
2944         int i, j, l;
2945         int teac_possible=0;
2946         
2947         msg(DBG_INI,"check_version entered.\n");
2948         msg(DBG_TE2,"check_version: id=%d, d=%d.\n", D_S[d].drv_id, d);
2949         D_S[d].drv_type=0;
2950 
2951         /* check for CR-52x, CR-56x and LCS-7260 */
2952         /* clear any pending error state */
2953         clr_cmdbuf();
2954         drvcmd[0]=CMD0_READ_ERR; /* same as CMD1_ and CMDL_ */
2955         response_count=9;
2956         flags_cmd_out=f_putcmd;
2957         i=cmd_out();
2958         if (i<0) msg(DBG_INI,"CMD0_READ_ERR returns %d (ok anyway).\n",i);
2959         /* read drive version */
2960         clr_cmdbuf();
2961         for (i=0;i<12;i++) infobuf[i]=0;
2962         drvcmd[0]=CMD0_READ_VER; /* same as CMD1_ and CMDL_ */
2963         response_count=12; /* fam1: only 11 */
2964         flags_cmd_out=f_putcmd;
2965         i=cmd_out();
2966         if (i<-1) msg(DBG_INI,"CMD0_READ_VER returns %d\n",i);
2967         if (i==-11) teac_possible++;
2968         j=0;
2969         for (i=0;i<12;i++) j+=infobuf[i];
2970         if (j)
2971         {
2972                 for (i=0;i<12;i++)
2973                         sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
2974                 msgbuf[i*3]=0;
2975                 msg(DBG_IDX,"infobuf =%s\n", msgbuf);
2976                 msg(DBG_000,"infobuf =%s\n", msgbuf);
2977                 for (i=0;i<12;i++)
2978                         sprintf(&msgbuf[i*3], " %c ", infobuf[i]);
2979                 msgbuf[i*3]=0;
2980                 msg(DBG_IDX,"infobuf =%s\n", msgbuf);
2981                 msg(DBG_000,"infobuf =%s\n", msgbuf);
2982         }
2983         for (i=0;i<4;i++) if (infobuf[i]!=family1[i]) break;
2984         if (i==4)
2985         {
2986                 D_S[d].drive_model[0]='C';
2987                 D_S[d].drive_model[1]='R';
2988                 D_S[d].drive_model[2]='-';
2989                 D_S[d].drive_model[3]='5';
2990                 D_S[d].drive_model[4]=infobuf[i++];
2991                 D_S[d].drive_model[5]=infobuf[i++];
2992                 D_S[d].drive_model[6]=0;
2993                 D_S[d].drv_type=drv_fam1;
2994         }
2995         if (!D_S[d].drv_type)
2996         {
2997                 for (i=0;i<8;i++) if (infobuf[i]!=family0[i]) break;
2998                 if (i==8)
2999                 {
3000                         D_S[d].drive_model[0]='C';
3001                         D_S[d].drive_model[1]='R';
3002                         D_S[d].drive_model[2]='-';
3003                         D_S[d].drive_model[3]='5';
3004                         D_S[d].drive_model[4]='2';
3005                         D_S[d].drive_model[5]='x';
3006                         D_S[d].drive_model[6]=0;
3007                         D_S[d].drv_type=drv_fam0;
3008                 }
3009         }
3010         if (!D_S[d].drv_type)
3011         {
3012                 for (i=0;i<8;i++) if (infobuf[i]!=familyL[i]) break;
3013                 if (i==8)
3014                 {
3015                         for (j=0;j<8;j++)
3016                                 D_S[d].drive_model[j]=infobuf[j];
3017                         D_S[d].drive_model[8]=0;
3018                         D_S[d].drv_type=drv_famL;
3019                 }
3020         }
3021         if (!D_S[d].drv_type)
3022         {
3023                 /* check for CD200 */
3024                 clr_cmdbuf();
3025                 drvcmd[0]=CMD2_READ_ERR;
3026                 response_count=9;
3027                 flags_cmd_out=f_putcmd;
3028                 i=cmd_out();
3029                 if (i<0) msg(DBG_INI,"CMD2_READERR returns %d (ok anyway).\n",i);
3030                 if (i<0) msg(DBG_000,"CMD2_READERR returns %d (ok anyway).\n",i);
3031                 /* read drive version */
3032                 clr_cmdbuf();
3033                 for (i=0;i<12;i++) infobuf[i]=0;
3034                 if (sbpro_type==1) OUT(CDo_sel_i_d,0);
3035 #if 0
3036                 OUT(CDo_reset,0);
3037                 sbp_sleep(6*HZ);
3038                 OUT(CDo_enable,D_S[d].drv_sel);
3039 #endif 0
3040                 drvcmd[0]=CMD2_READ_VER;
3041                 response_count=12;
3042                 flags_cmd_out=f_putcmd;
3043                 i=cmd_out();
3044                 if (i<0) msg(DBG_INI,"CMD2_READ_VER returns %d\n",i);
3045                 if (i==-7) teac_possible++;
3046                 j=0;
3047                 for (i=0;i<12;i++) j+=infobuf[i];
3048                 if (j)
3049                 {
3050                         for (i=0;i<12;i++)
3051                                 sprintf(&msgbuf[i*3], " %02X", infobuf[i]);
3052                         msgbuf[i*3]=0;
3053                         msg(DBG_IDX,"infobuf =%s\n", msgbuf);
3054                         for (i=0;i<12;i++)
3055                                 sprintf(&msgbuf[i*3], " %c ", infobuf[i]);
3056                         msgbuf[i*3]=0;
3057                         msg(DBG_IDX,"infobuf =%s\n", msgbuf);
3058                 }
3059                 if (i>=0)
3060                 {
3061                         for (i=0;i<5;i++) if (infobuf[i]!=family2[i]) break;
3062                         if (i==5)
3063                         {
3064                                 D_S[d].drive_model[0]='C';
3065                                 D_S[d].drive_model[1]='D';
3066                                 D_S[d].drive_model[2]='2';
3067                                 D_S[d].drive_model[3]='0';
3068                                 D_S[d].drive_model[4]='0';
3069                                 D_S[d].drive_model[5]=infobuf[i++];
3070                                 D_S[d].drive_model[6]=infobuf[i++];
3071                                 D_S[d].drive_model[7]=0;
3072                                 D_S[d].drv_type=drv_fam2;
3073                         }
3074                 }
3075         }
3076         if (!D_S[d].drv_type)
3077         {
3078                 /* check for TEAC CD-55A */
3079                 msg(DBG_TEA,"teac_possible: %d\n",teac_possible);
3080                 for (j=1;j<=((D_S[d].drv_id==0)?3:1);j++)
3081                 {
3082                         for (l=1;l<=((D_S[d].drv_id==0)?10:1);l++)
3083                         {
3084                                 msg(DBG_TEA,"TEAC reset #%d-%d.\n", j, l);
3085                                 if (sbpro_type==1) OUT(CDo_reset,0);
3086                                 else
3087                                 {
3088                                         OUT(CDo_enable,D_S[d].drv_sel);
3089                                         OUT(CDo_sel_i_d,0);
3090                                         OUT(CDo_command,CMDT_RESET);
3091                                         for (i=0;i<9;i++) OUT(CDo_command,0);
3092                                 }
3093                                 sbp_sleep(5*HZ/10);
3094                                 OUT(CDo_enable,D_S[d].drv_sel);
3095                                 OUT(CDo_sel_i_d,0);
3096                                 i=inb(CDi_status);
3097                                 msg(DBG_TEA,"TEAC CDi_status: %02X.\n",i);
3098 #if 0
3099                                 if (i&s_not_result_ready) continue; /* drive not present or ready */
3100 #endif
3101                                 i=inb(CDi_info);
3102                                 msg(DBG_TEA,"TEAC CDi_info: %02X.\n",i);
3103                                 if (i==0x55) break; /* drive found */
3104                         }
3105                         if (i==0x55) break; /* drive found */
3106                 }
3107                 if (i==0x55) /* drive found */
3108                 {
3109                         msg(DBG_TEA,"TEAC drive found.\n");
3110                         clr_cmdbuf();
3111                         flags_cmd_out=f_putcmd;
3112                         response_count=12;
3113                         drvcmd[0]=CMDT_READ_VER;
3114                         drvcmd[4]=response_count;
3115                         for (i=0;i<12;i++) infobuf[i]=0;
3116                         i=cmd_out_T();
3117                         if (i!=0) msg(DBG_TEA,"cmd_out_T(CMDT_READ_VER) returns %d.\n",i);
3118                         for (i=1;i<6;i++) if (infobuf[i]!=familyT[i-1]) break;
3119                         if (i==6)
3120                         {
3121                                 D_S[d].drive_model[0]='C';
3122                                 D_S[d].drive_model[1]='D';
3123                                 D_S[d].drive_model[2]='-';
3124                                 D_S[d].drive_model[3]='5';
3125                                 D_S[d].drive_model[4]='5';
3126                                 D_S[d].drive_model[5]=0;
3127                                 D_S[d].drv_type=drv_famT;
3128                         }
3129                 }
3130         }
3131         if (!D_S[d].drv_type)
3132         {
3133                 msg(DBG_TEA,"no drive found at address %03X under ID %d.\n",CDo_command,D_S[d].drv_id);
3134                 return (-522);
3135         }
3136         for (j=0;j<4;j++) D_S[d].firmware_version[j]=infobuf[i+j];
3137         if (famL_drive)
3138         {
3139                 u_char lcs_firm_e1[]="A E1";
3140                 u_char lcs_firm_f4[]="A4F4";
3141                 
3142                 for (j=0;j<4;j++)
3143                         if (D_S[d].firmware_version[j]!=lcs_firm_e1[j]) break;
3144                 if (j==4) D_S[d].drv_type=drv_e1;
3145 
3146                 for (j=0;j<4;j++)
3147                         if (D_S[d].firmware_version[j]!=lcs_firm_f4[j]) break;
3148                 if (j==4) D_S[d].drv_type=drv_f4;
3149 
3150                 if (D_S[d].drv_type==drv_famL) ask_mail();
3151         }
3152         else if (famT_drive)
3153         {
3154                 j=infobuf[4]; /* one-byte version??? - here: 0x15 */
3155                 if (j=='5')
3156                 {
3157                         D_S[d].firmware_version[0]=infobuf[7];
3158                         D_S[d].firmware_version[1]=infobuf[8];
3159                         D_S[d].firmware_version[2]=infobuf[10];
3160                         D_S[d].firmware_version[3]=infobuf[11];
3161                 }
3162                 else
3163                 {
3164                         if (j!=0x15) ask_mail();
3165                         D_S[d].firmware_version[0]='0';
3166                         D_S[d].firmware_version[1]='.';
3167                         D_S[d].firmware_version[2]='0'+(j>>4);
3168                         D_S[d].firmware_version[3]='0'+(j&0x0f);
3169                 }
3170         }
3171         else /* CR-52x, CR-56x, CD200 */
3172         {
3173                 j = (D_S[d].firmware_version[0] & 0x0F) * 100 +
3174                         (D_S[d].firmware_version[2] & 0x0F) *10 +
3175                                 (D_S[d].firmware_version[3] & 0x0F);
3176                 if (fam0_drive)
3177                 {
3178                         if (j<200) D_S[d].drv_type=drv_199;
3179                         else if (j<201) D_S[d].drv_type=drv_200;
3180                         else if (j<210) D_S[d].drv_type=drv_201;
3181                         else if (j<211) D_S[d].drv_type=drv_210;
3182                         else if (j<300) D_S[d].drv_type=drv_211;
3183                         else if (j>=300) D_S[d].drv_type=drv_300;
3184                 }
3185                 else if (fam1_drive)
3186                 {
3187                         if (j<100) D_S[d].drv_type=drv_099;
3188                         else
3189                         {
3190                                 D_S[d].drv_type=drv_100;
3191                                 if ((j!=500)&&(j!=102)) ask_mail();
3192                         }
3193                 }
3194                 else if (fam2_drive)
3195                 {
3196                         if (D_S[d].drive_model[5]=='F')
3197                         {
3198                                 if ((j!=1)&&(j!=35)&&(j!=200)&&(j!=210)) ask_mail(); /* unknown version at time */
3199                         }
3200                         else
3201                         {
3202                                 msg(DBG_INF,"this CD200 drive is not fully supported yet - only audio will work.\n");
3203                                 if ((j!=101)&&(j!=35)) ask_mail(); /* unknown version at time */
3204                         }
3205                 }
3206         }
3207         msg(DBG_LCS,"drive type %02X\n",D_S[d].drv_type);
3208         msg(DBG_INI,"check_version done.\n");
3209         return (0);
3210 }
3211 /*==========================================================================*/
3212 static void switch_drive(int i)
     /* [previous][next][first][last][top][bottom][index][help] */
3213 {
3214         d=i;
3215         OUT(CDo_enable,D_S[d].drv_sel);
3216         msg(DBG_DID,"drive %d (ID=%d) activated.\n", i, D_S[d].drv_id);
3217         return;
3218 }
3219 /*==========================================================================*/
3220 #ifdef PATH_CHECK
3221 /*
3222  * probe for the presence of an interface card
3223  */
3224 static int check_card(int port)
     /* [previous][next][first][last][top][bottom][index][help] */
3225 {
3226 #undef N_RESPO
3227 #define N_RESPO 20
3228         int i, j, k;
3229         u_char response[N_RESPO];
3230         u_char save_port0;
3231         u_char save_port3;
3232         
3233         msg(DBG_INI,"check_card entered.\n");
3234         save_port0=inb(port+0);
3235         save_port3=inb(port+3);
3236         
3237         for (j=0;j<NR_SBPCD;j++)
3238         {
3239                 OUT(port+3,j) ; /* enable drive #j */
3240                 OUT(port+0,CMD0_PATH_CHECK);
3241                 for (i=10;i>0;i--) OUT(port+0,0);
3242                 for (k=0;k<N_RESPO;k++) response[k]=0;
3243                 for (k=0;k<N_RESPO;k++)
3244                 {
3245                         for (i=10000;i>0;i--)
3246                         {
3247                                 if (inb(port+1)&s_not_result_ready) continue;
3248                                 response[k]=inb(port+0);
3249                                 break;
3250                         }
3251                 }
3252                 for (i=0;i<N_RESPO;i++)
3253                         sprintf(&msgbuf[i*3], " %02X", response[i]);
3254                 msgbuf[i*3]=0;
3255                 msg(DBG_TEA,"path check 00 (%d): %s\n", j, msgbuf);
3256                 OUT(port+0,CMD0_PATH_CHECK);
3257                 for (i=10;i>0;i--) OUT(port+0,0);
3258                 for (k=0;k<N_RESPO;k++) response[k]=0xFF;
3259                 for (k=0;k<N_RESPO;k++)
3260                 {
3261                         for (i=10000;i>0;i--)
3262                         {
3263                                 if (inb(port+1)&s_not_result_ready) continue;
3264                                 response[k]=inb(port+0);
3265                                 break;
3266                         }
3267                 }
3268                 for (i=0;i<N_RESPO;i++)
3269                         sprintf(&msgbuf[i*3], " %02X", response[i]);
3270                 msgbuf[i*3]=0;
3271                 msg(DBG_TEA,"path check 00 (%d): %s\n", j, msgbuf);
3272 
3273                 if (response[0]==0xAA)
3274                         if (response[1]==0x55)
3275                                 return (0);
3276         }
3277         for (j=0;j<NR_SBPCD;j++)
3278         {
3279                 OUT(port+3,j) ; /* enable drive #j */
3280                 OUT(port+0,CMD2_READ_VER);
3281                 for (i=10;i>0;i--) OUT(port+0,0);
3282                 for (k=0;k<N_RESPO;k++) response[k]=0;
3283                 for (k=0;k<N_RESPO;k++)
3284                 {
3285                         for (i=1000000;i>0;i--)
3286                         {
3287                                 if (inb(port+1)&s_not_result_ready) continue;
3288                                 response[k]=inb(port+0);
3289                                 break;
3290                         }
3291                 }
3292                 for (i=0;i<N_RESPO;i++)
3293                         sprintf(&msgbuf[i*3], " %02X", response[i]);
3294                 msgbuf[i*3]=0;
3295                 msg(DBG_TEA,"path check 12 (%d): %s\n", j, msgbuf);
3296 
3297                 OUT(port+0,CMD2_READ_VER);
3298                 for (i=10;i>0;i--) OUT(port+0,0);
3299                 for (k=0;k<N_RESPO;k++) response[k]=0xFF;
3300                 for (k=0;k<N_RESPO;k++)
3301                 {
3302                         for (i=1000000;i>0;i--)
3303                         {
3304                                 if (inb(port+1)&s_not_result_ready) continue;
3305                                 response[k]=inb(port+0);
3306                                 break;
3307                         }
3308                 }
3309                 for (i=0;i<N_RESPO;i++)
3310                         sprintf(&msgbuf[i*3], " %02X", response[i]);
3311                 msgbuf[i*3]=0;
3312                 msg(DBG_TEA,"path check 12 (%d): %s\n", j, msgbuf);
3313 
3314                 if (response[0]==0xAA)
3315                         if (response[1]==0x55)
3316                                 return (0);
3317         }
3318         OUT(port+0,save_port0);
3319         OUT(port+3,save_port3);
3320         return (0); /* in any case - no real "function" at time */
3321 }
3322 #endif PATH_CHECK
3323 /*==========================================================================*/
3324 /*==========================================================================*/
3325 /*
3326  * probe for the presence of drives on the selected controller
3327  */
3328 static int check_drives(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3329 {
3330         int i, j;
3331         
3332         msg(DBG_INI,"check_drives entered.\n");
3333         ndrives=0;
3334         for (j=0;j<MAX_DRIVES;j++)
3335         {
3336                 D_S[ndrives].drv_id=j;
3337                 if (sbpro_type==1) D_S[ndrives].drv_sel=(j&0x01)<<1|(j&0x02)>>1;
3338                 else D_S[ndrives].drv_sel=j;
3339                 switch_drive(ndrives);
3340                 msg(DBG_INI,"check_drives: drive %d (ID=%d) activated.\n",ndrives,j);
3341                 msg(DBG_000,"check_drives: drive %d (ID=%d) activated.\n",ndrives,j);
3342                 i=check_version();
3343                 if (i<0) msg(DBG_INI,"check_version returns %d.\n",i);
3344                 else
3345                 {
3346                         D_S[d].drv_options=drv_pattern[j];
3347                         if (fam0L_drive) D_S[d].drv_options&=~(speed_auto|speed_300|speed_150);
3348                         msg(DBG_INF, "Drive %d (ID=%d): %.9s (%.4s) at 0x%03X (type %d)\n",
3349                             d,
3350                             D_S[d].drv_id,
3351                             D_S[d].drive_model,
3352                             D_S[d].firmware_version,
3353                             CDo_command,
3354                             sbpro_type);
3355                         ndrives++;
3356                 }
3357         }
3358         for (j=ndrives;j<NR_SBPCD;j++) D_S[j].drv_id=-1;
3359         if (ndrives==0) return (-1);
3360         return (0);
3361 }
3362 /*==========================================================================*/
3363 #if FUTURE
3364 /*
3365  *  obtain if requested service disturbs current audio state
3366  */            
3367 static int obey_audio_state(u_char audio_state, u_char func,u_char subfunc)
     /* [previous][next][first][last][top][bottom][index][help] */
3368 {
3369         switch (audio_state)                   /* audio status from controller  */
3370         {
3371         case aud_11: /* "audio play in progress" */
3372         case audx11:
3373                 switch (func)                      /* DOS command code */
3374                 {
3375                 case cmd_07: /* input flush  */
3376                 case cmd_0d: /* open device  */
3377                 case cmd_0e: /* close device */
3378                 case cmd_0c: /* ioctl output */
3379                         return (1);
3380                 case cmd_03: /* ioctl input  */
3381                         switch (subfunc)
3382                                 /* DOS ioctl input subfunction */
3383                         {
3384                         case cxi_00:
3385                         case cxi_06:
3386                         case cxi_09:
3387                                 return (1);
3388                         default:
3389                                 return (ERROR15);
3390                         }
3391                         return (1);
3392                 default:
3393                         return (ERROR15);
3394                 }
3395                 return (1);
3396         case aud_12:                  /* "audio play paused"      */
3397         case audx12:
3398                 return (1);
3399         default:
3400                 return (2);
3401         }
3402 }
3403 /*==========================================================================*/
3404 /* allowed is only
3405  * ioctl_o, flush_input, open_device, close_device, 
3406  * tell_address, tell_volume, tell_capabiliti,
3407  * tell_framesize, tell_CD_changed, tell_audio_posi
3408  */
3409 static int check_allowed1(u_char func1, u_char func2)
     /* [previous][next][first][last][top][bottom][index][help] */
3410 {
3411 #if 000
3412         if (func1==ioctl_o) return (0);
3413         if (func1==read_long) return (-1);
3414         if (func1==read_long_prefetch) return (-1);
3415         if (func1==seek) return (-1);
3416         if (func1==audio_play) return (-1);
3417         if (func1==audio_pause) return (-1);
3418         if (func1==audio_resume) return (-1);
3419         if (func1!=ioctl_i) return (0);
3420         if (func2==tell_SubQ_run_tot) return (-1);
3421         if (func2==tell_cdsize) return (-1);
3422         if (func2==tell_TocDescrip) return (-1);
3423         if (func2==tell_TocEntry) return (-1);
3424         if (func2==tell_subQ_info) return (-1);
3425         if (fam1_drive) if (func2==tell_SubChanInfo) return (-1);
3426         if (func2==tell_UPC) return (-1);
3427 #else
3428         return (0);
3429 #endif 000
3430 }
3431 /*==========================================================================*/
3432 static int check_allowed2(u_char func1, u_char func2)
     /* [previous][next][first][last][top][bottom][index][help] */
3433 {
3434 #if 000
3435         if (func1==read_long) return (-1);
3436         if (func1==read_long_prefetch) return (-1);
3437         if (func1==seek) return (-1);
3438         if (func1==audio_play) return (-1);
3439   if (func1!=ioctl_o) return (0);
3440         if (fam1_drive)
3441         {
3442                 if (func2==EjectDisk) return (-1);
3443                 if (func2==CloseTray) return (-1);
3444         }
3445 #else
3446         return (0);
3447 #endif 000
3448 }
3449 /*==========================================================================*/
3450 static int check_allowed3(u_char func1, u_char func2)
     /* [previous][next][first][last][top][bottom][index][help] */
3451 {
3452 #if 000
3453         if (func1==ioctl_i)
3454         {
3455                 if (func2==tell_address) return (0);
3456                 if (func2==tell_capabiliti) return (0);
3457                 if (func2==tell_CD_changed) return (0);
3458                 if (fam0L_drive) if (func2==tell_SubChanInfo) return (0);
3459                 return (-1);
3460         }
3461         if (func1==ioctl_o)
3462         {
3463                 if (func2==DriveReset) return (0);
3464                 if (fam0L_drive)
3465                 {
3466                         if (func2==EjectDisk) return (0);
3467                         if (func2==LockDoor) return (0);
3468           if (func2==CloseTray) return (0);
3469                 }
3470                 return (-1);
3471     }
3472         if (func1==flush_input) return (-1);
3473         if (func1==read_long) return (-1);
3474         if (func1==read_long_prefetch) return (-1);
3475         if (func1==seek) return (-1);
3476         if (func1==audio_play) return (-1);
3477         if (func1==audio_pause) return (-1);
3478         if (func1==audio_resume) return (-1);
3479 #else
3480         return (0);
3481 #endif 000
3482 }
3483 /*==========================================================================*/
3484 static int seek_pos_audio_end(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3485 {
3486         int i;
3487 
3488         i=msf2blk(D_S[d].pos_audio_end)-1;
3489         if (i<0) return (-1);
3490         i=cc_Seek(i,0);
3491         return (i);
3492 }
3493 #endif FUTURE
3494 /*==========================================================================*/
3495 static int ReadToC(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3496 {
3497         int i, j;
3498         D_S[d].diskstate_flags &= ~toc_bit;
3499         D_S[d].ored_ctl_adr=0;
3500         for (j=D_S[d].n_first_track;j<=D_S[d].n_last_track;j++)
3501         {
3502                 i=cc_ReadTocEntry(j);
3503                 if (i<0)
3504                 {
3505                         msg(DBG_INF,"cc_ReadTocEntry(%d) returns %d.\n",j,i);
3506                         return (i);
3507                 }
3508                 D_S[d].TocBuffer[j].nixbyte=D_S[d].TocEnt_nixbyte;
3509                 D_S[d].TocBuffer[j].ctl_adr=D_S[d].TocEnt_ctl_adr;
3510                 D_S[d].TocBuffer[j].number=D_S[d].TocEnt_number;
3511                 D_S[d].TocBuffer[j].format=D_S[d].TocEnt_format;
3512                 D_S[d].TocBuffer[j].address=D_S[d].TocEnt_address;
3513                 D_S[d].ored_ctl_adr |= D_S[d].TocEnt_ctl_adr;
3514         }
3515         /* fake entry for LeadOut Track */
3516         D_S[d].TocBuffer[j].nixbyte=0;
3517         D_S[d].TocBuffer[j].ctl_adr=0;
3518         D_S[d].TocBuffer[j].number=CDROM_LEADOUT;
3519         D_S[d].TocBuffer[j].format=0;
3520         D_S[d].TocBuffer[j].address=D_S[d].size_msf;
3521         
3522         D_S[d].diskstate_flags |= toc_bit;
3523         return (0);
3524 }
3525 /*==========================================================================*/
3526 static int DiskInfo(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3527 {
3528         int i, j;
3529         
3530         D_S[d].mode=READ_M1;
3531         
3532 #undef LOOP_COUNT
3533 #define LOOP_COUNT 10 /* needed for some "old" drives */
3534         
3535         msg(DBG_000,"DiskInfo entered.\n");
3536         for (j=1;j<LOOP_COUNT;j++)
3537         {
3538                 i=SetSpeed();
3539                 if (i<0)
3540                 {
3541                         msg(DBG_INF,"DiskInfo: SetSpeed returns %d\n", i);
3542                         continue;
3543                 }
3544                 i=cc_ModeSense();
3545                 if (i<0)
3546                 {
3547                         msg(DBG_INF,"DiskInfo: cc_ModeSense returns %d\n", i);
3548                         continue;
3549                 }
3550                 i=cc_ReadCapacity();
3551                 if (i>=0) break;
3552                 msg(DBG_INF,"DiskInfo: ReadCapacity #%d returns %d\n", j, i);
3553                 i=cc_DriveReset();
3554         }
3555         if (j==LOOP_COUNT) return (-33); /* give up */
3556         
3557         i=cc_ReadTocDescr();
3558         if (i<0)
3559         {
3560                 msg(DBG_INF,"DiskInfo: ReadTocDescr returns %d\n", i);
3561                 return (i);
3562         }
3563         i=ReadToC();
3564         if (i<0)
3565         {
3566                 msg(DBG_INF,"DiskInfo: ReadToC returns %d\n", i);
3567                 return (i);
3568         }
3569         i=cc_CheckMultiSession();
3570         if (i<0)
3571         {
3572                 msg(DBG_INF,"DiskInfo: cc_CheckMultiSession returns %d\n", i);
3573                 return (i);
3574         }
3575         if (D_S[d].f_multisession) D_S[d].sbp_bufsiz=1;  /* possibly a weird PhotoCD */
3576         else D_S[d].sbp_bufsiz=SBP_BUFFER_FRAMES;
3577         i=cc_ReadTocEntry(D_S[d].n_first_track);
3578         if (i<0)
3579         {
3580                 msg(DBG_INF,"DiskInfo: cc_ReadTocEntry(1) returns %d\n", i);
3581                 return (i);
3582         }
3583         i=cc_ReadUPC();
3584         if (i<0) msg(DBG_INF,"DiskInfo: cc_ReadUPC returns %d\n", i);
3585         if ((fam0L_drive) && (D_S[d].xa_byte==0x20))
3586         {
3587                 /* XA disk with old drive */
3588                 cc_ModeSelect(CD_FRAMESIZE_RAW1);
3589                 cc_ModeSense();
3590         }
3591         if (famT_drive) cc_prep_mode_T();
3592         msg(DBG_000,"DiskInfo done.\n");
3593         return (0);
3594 }
3595 /*==========================================================================*/
3596 #if FUTURE
3597 /*
3598  *  called always if driver gets entered
3599  *  returns 0 or ERROR2 or ERROR15
3600  */
3601 static int prepare(u_char func, u_char subfunc)
     /* [previous][next][first][last][top][bottom][index][help] */
3602 {
3603         int i;
3604         
3605         if (fam0L_drive)
3606         {
3607                 i=inb(CDi_status);
3608                 if (i&s_attention) GetStatus();
3609         }
3610         else if (fam1_drive) GetStatus();
3611         else if (fam2_drive) GetStatus();
3612         else if (famT_drive) GetStatus();
3613         if (D_S[d].CD_changed==0xFF)
3614         {
3615                 D_S[d].diskstate_flags=0;
3616                 D_S[d].audio_state=0;
3617                 if (!st_diskok)
3618                 {
3619                         i=check_allowed1(func,subfunc);
3620                         if (i<0) return (-2);
3621                 }
3622                 else 
3623                 {
3624                         i=check_allowed3(func,subfunc);
3625                         if (i<0)
3626                         {
3627                                 D_S[d].CD_changed=1;
3628                                 return (-15);
3629                         }
3630                 }
3631         }
3632         else
3633         {
3634                 if (!st_diskok)
3635                 {
3636                         D_S[d].diskstate_flags=0;
3637                         D_S[d].audio_state=0;
3638                         i=check_allowed1(func,subfunc);
3639                         if (i<0) return (-2);
3640                 }
3641                 else
3642                 { 
3643                         if (st_busy)
3644                         {
3645                                 if (D_S[d].audio_state!=audio_pausing)
3646                                 {
3647                                         i=check_allowed2(func,subfunc);
3648                                         if (i<0) return (-2);
3649                                 }
3650                         }
3651                         else
3652                         {
3653                                 if (D_S[d].audio_state==audio_playing) seek_pos_audio_end();
3654                                 D_S[d].audio_state=0;
3655                         }
3656                         if (!frame_size_valid)
3657                         {
3658                                 i=DiskInfo();
3659                                 if (i<0)
3660                                 {
3661                                         D_S[d].diskstate_flags=0;
3662                                         D_S[d].audio_state=0;
3663                                         i=check_allowed1(func,subfunc);
3664                                         if (i<0) return (-2);
3665                                 }
3666                         }
3667                 }
3668     }
3669         return (0);
3670 }
3671 #endif FUTURE
3672 /*==========================================================================*/
3673 /*==========================================================================*/
3674 /*
3675  * Check the results of the "get status" command.
3676  */
3677 static int sbp_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3678 {
3679         int st;
3680         
3681         st=ResponseStatus();
3682         if (st<0)
3683         {
3684                 msg(DBG_INF,"sbp_status: timeout.\n");
3685                 return (0);
3686         }
3687         
3688         if (!st_spinning) msg(DBG_SPI,"motor got off - ignoring.\n");
3689         
3690         if (st_check) 
3691         {
3692                 msg(DBG_INF,"st_check detected - retrying.\n");
3693                 return (0);
3694         }
3695         if (!st_door_closed)
3696         {
3697                 msg(DBG_INF,"door is open - retrying.\n");
3698                 return (0);
3699         }
3700         if (!st_caddy_in)
3701         {
3702                 msg(DBG_INF,"disk removed - retrying.\n");
3703                 return (0);
3704         }
3705         if (!st_diskok) 
3706         {
3707                 msg(DBG_INF,"!st_diskok detected - retrying.\n");
3708                 return (0);
3709         }
3710         if (st_busy) 
3711         {
3712                 msg(DBG_INF,"st_busy detected - retrying.\n");
3713                 return (0);
3714         }
3715         return (1);
3716 }
3717 /*==========================================================================*/
3718 
3719 /*==========================================================================*/
3720 /*==========================================================================*/
3721 /*
3722  * ioctl support
3723  */
3724 static int sbpcd_ioctl(struct inode *inode, struct file *file, u_int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
3725                        u_long arg)
3726 {
3727         int i, st;
3728         
3729         msg(DBG_IO2,"ioctl(%d, 0x%08lX, 0x%08lX)\n",
3730             MINOR(inode->i_rdev), cmd, arg);
3731         if (!inode) return (-EINVAL);
3732         i=MINOR(inode->i_rdev);
3733         if ((i<0) || (i>=NR_SBPCD) || (D_S[i].drv_id==-1))
3734         {
3735                 msg(DBG_INF, "ioctl: bad device: %04X\n", inode->i_rdev);
3736                 return (-ENXIO);             /* no such drive */
3737         }
3738         if (d!=i) switch_drive(i);
3739         
3740 #if 0
3741         st=GetStatus();
3742         if (st<0) return (-EIO);
3743         
3744         if (!toc_valid)
3745         {
3746                 i=DiskInfo();
3747                 if (i<0) return (-EIO); /* error reading TOC */
3748         }
3749 #endif
3750         
3751         msg(DBG_IO2,"ioctl: device %d, request %04X\n",i,cmd);
3752         switch (cmd)            /* Sun-compatible */
3753         {
3754         case DDIOCSDBG:         /* DDI Debug */
3755                 if (!suser()) return (-EPERM);
3756                 i=sbpcd_dbg_ioctl(arg,1);
3757                 return (i);
3758                 
3759         case CDROMPAUSE:     /* Pause the drive */
3760                 msg(DBG_IOC,"ioctl: CDROMPAUSE entered.\n");
3761                 /* pause the drive unit when it is currently in PLAY mode,         */
3762                 /* or reset the starting and ending locations when in PAUSED mode. */
3763                 /* If applicable, at the next stopping point it reaches            */
3764                 /* the drive will discontinue playing.                             */
3765                 switch (D_S[d].audio_state)
3766                 {
3767                 case audio_playing:
3768                         if (famL_drive) i=cc_ReadSubQ();
3769                         else i=cc_Pause_Resume(1);
3770                         if (i<0) return (-EIO);
3771                         if (famL_drive) i=cc_Pause_Resume(1);
3772                         else i=cc_ReadSubQ();
3773                         if (i<0) return (-EIO);
3774                         D_S[d].pos_audio_start=D_S[d].SubQ_run_tot;
3775                         D_S[d].audio_state=audio_pausing;
3776                         return (0);
3777                 case audio_pausing:
3778                         i=cc_Seek(D_S[d].pos_audio_start,1);
3779                         if (i<0) return (-EIO);
3780                         return (0);
3781                 default:
3782                         return (-EINVAL);
3783                 }
3784                 
3785         case CDROMRESUME: /* resume paused audio play */
3786                 msg(DBG_IOC,"ioctl: CDROMRESUME entered.\n");
3787                 /* resume playing audio tracks when a previous PLAY AUDIO call has  */
3788                 /* been paused with a PAUSE command.                                */
3789                 /* It will resume playing from the location saved in SubQ_run_tot.  */
3790                 if (D_S[d].audio_state!=audio_pausing) return -EINVAL;
3791                 if (famL_drive)
3792                         i=cc_PlayAudio(D_S[d].pos_audio_start,
3793                                        D_S[d].pos_audio_end);
3794                 else i=cc_Pause_Resume(3);
3795                 if (i<0) return (-EIO);
3796                 D_S[d].audio_state=audio_playing;
3797                 return (0);
3798                 
3799         case CDROMPLAYMSF:
3800                 msg(DBG_IOC,"ioctl: CDROMPLAYMSF entered.\n");
3801                 if (D_S[d].audio_state==audio_playing)
3802                 {
3803                         i=cc_Pause_Resume(1);
3804                         if (i<0) return (-EIO);
3805                         i=cc_ReadSubQ();
3806                         if (i<0) return (-EIO);
3807                         D_S[d].pos_audio_start=D_S[d].SubQ_run_tot;
3808                         i=cc_Seek(D_S[d].pos_audio_start,1);
3809                 }
3810                 st=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_msf));
3811                 if (st) return (st);
3812                 memcpy_fromfs(&msf, (void *) arg, sizeof(struct cdrom_msf));
3813                 /* values come as msf-bin */
3814                 D_S[d].pos_audio_start = (msf.cdmsf_min0<<16) |
3815                         (msf.cdmsf_sec0<<8) |
3816                                 msf.cdmsf_frame0;
3817                 D_S[d].pos_audio_end = (msf.cdmsf_min1<<16) |
3818                         (msf.cdmsf_sec1<<8) |
3819                                 msf.cdmsf_frame1;
3820                 msg(DBG_IOX,"ioctl: CDROMPLAYMSF %08X %08X\n",
3821                     D_S[d].pos_audio_start,D_S[d].pos_audio_end);
3822                 i=cc_PlayAudio(D_S[d].pos_audio_start,D_S[d].pos_audio_end);
3823                 msg(DBG_IOC,"ioctl: cc_PlayAudio returns %d\n",i);
3824 #if 0
3825                 if (i<0) return (-EIO);
3826 #endif 0
3827                 D_S[d].audio_state=audio_playing;
3828                 return (0);
3829                 
3830         case CDROMPLAYTRKIND: /* Play a track.  This currently ignores index. */
3831                 msg(DBG_IOC,"ioctl: CDROMPLAYTRKIND entered.\n");
3832                 if (D_S[d].audio_state==audio_playing)
3833                 {
3834                         msg(DBG_IOX,"CDROMPLAYTRKIND: already audio_playing.\n");
3835                         return (0);
3836                         return (-EINVAL);
3837                 }
3838                 st=verify_area(VERIFY_READ,(void *) arg,sizeof(struct cdrom_ti));
3839                 if (st<0)
3840                 {
3841                         msg(DBG_IOX,"CDROMPLAYTRKIND: verify_area error.\n");
3842                         return (st);
3843                 }
3844                 memcpy_fromfs(&ti,(void *) arg,sizeof(struct cdrom_ti));
3845                 msg(DBG_IOX,"ioctl: trk0: %d, ind0: %d, trk1:%d, ind1:%d\n",
3846                     ti.cdti_trk0,ti.cdti_ind0,ti.cdti_trk1,ti.cdti_ind1);
3847                 if (ti.cdti_trk0<D_S[d].n_first_track) return (-EINVAL);
3848                 if (ti.cdti_trk0>D_S[d].n_last_track) return (-EINVAL);
3849                 if (ti.cdti_trk1<ti.cdti_trk0) ti.cdti_trk1=ti.cdti_trk0;
3850                 if (ti.cdti_trk1>D_S[d].n_last_track) ti.cdti_trk1=D_S[d].n_last_track;
3851                 D_S[d].pos_audio_start=D_S[d].TocBuffer[ti.cdti_trk0].address;
3852                 D_S[d].pos_audio_end=D_S[d].TocBuffer[ti.cdti_trk1+1].address;
3853                 i=cc_PlayAudio(D_S[d].pos_audio_start,D_S[d].pos_audio_end);
3854 #if 0
3855                 if (i<0) return (-EIO);
3856 #endif 0
3857                 D_S[d].audio_state=audio_playing;
3858                 return (0);
3859                 
3860         case CDROMREADTOCHDR:        /* Read the table of contents header */
3861                 msg(DBG_IOC,"ioctl: CDROMREADTOCHDR entered.\n");
3862                 tochdr.cdth_trk0=D_S[d].n_first_track;
3863                 tochdr.cdth_trk1=D_S[d].n_last_track;
3864                 st=verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct cdrom_tochdr));
3865                 if (st) return (st);
3866                 memcpy_tofs((void *) arg, &tochdr, sizeof(struct cdrom_tochdr));
3867                 return (0);
3868                 
3869         case CDROMREADTOCENTRY:      /* Read an entry in the table of contents */
3870                 msg(DBG_IOC,"ioctl: CDROMREADTOCENTRY entered.\n");
3871                 st=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_tocentry));
3872                 if (st) return (st);
3873                 memcpy_fromfs(&tocentry, (void *) arg, sizeof(struct cdrom_tocentry));
3874                 i=tocentry.cdte_track;
3875                 if (i==CDROM_LEADOUT) i=D_S[d].n_last_track+1;
3876                 else if (i<D_S[d].n_first_track||i>D_S[d].n_last_track) return (-EINVAL);
3877                 tocentry.cdte_adr=D_S[d].TocBuffer[i].ctl_adr&0x0F;
3878                 tocentry.cdte_ctrl=(D_S[d].TocBuffer[i].ctl_adr>>4)&0x0F;
3879                 tocentry.cdte_datamode=D_S[d].TocBuffer[i].format;
3880                 if (tocentry.cdte_format==CDROM_MSF) /* MSF-bin required */
3881                 {
3882                         tocentry.cdte_addr.msf.minute=(D_S[d].TocBuffer[i].address>>16)&0x00FF;
3883                         tocentry.cdte_addr.msf.second=(D_S[d].TocBuffer[i].address>>8)&0x00FF;
3884                         tocentry.cdte_addr.msf.frame=D_S[d].TocBuffer[i].address&0x00FF;
3885                 }
3886                 else if (tocentry.cdte_format==CDROM_LBA) /* blk required */
3887                         tocentry.cdte_addr.lba=msf2blk(D_S[d].TocBuffer[i].address);
3888                 else return (-EINVAL);
3889                 st=verify_area(VERIFY_WRITE,(void *) arg, sizeof(struct cdrom_tocentry));
3890                 if (st) return (st);
3891                 memcpy_tofs((void *) arg, &tocentry, sizeof(struct cdrom_tocentry));
3892                 return (0);
3893                 
3894         case CDROMRESET:      /* hard reset the drive */
3895                 msg(DBG_IOC,"ioctl: CDROMRESET entered.\n");
3896                 i=DriveReset();
3897                 D_S[d].audio_state=0;
3898                 return (i);
3899                 
3900         case CDROMSTOP:      /* Spin down the drive */
3901                 msg(DBG_IOC,"ioctl: CDROMSTOP entered.\n");
3902                 i=cc_Pause_Resume(1);
3903                 D_S[d].audio_state=0;
3904                 return (i);
3905                 
3906         case CDROMSTART:  /* Spin up the drive */
3907                 msg(DBG_IOC,"ioctl: CDROMSTART entered.\n");
3908                 cc_SpinUp();
3909                 D_S[d].audio_state=0;
3910                 return (0);
3911                 
3912         case CDROMEJECT:
3913                 msg(DBG_IOC,"ioctl: CDROMEJECT entered.\n");
3914                 if (fam0_drive) return (0);
3915                 if (D_S[d].open_count>1) return (-EBUSY);
3916                 i=UnLockDoor();
3917                 D_S[d].open_count=-9; /* to get it locked next time again */
3918                 i=cc_SpinDown();
3919                 msg(DBG_IOX,"ioctl: cc_SpinDown returned %d.\n", i);
3920                 msg(DBG_TEA,"ioctl: cc_SpinDown returned %d.\n", i);
3921                 if (i<0) return (-EIO);
3922                 D_S[d].CD_changed=0xFF;
3923                 D_S[d].diskstate_flags=0;
3924                 D_S[d].audio_state=0;
3925                 return (0);
3926                 
3927         case CDROMEJECT_SW:
3928                 msg(DBG_IOC,"ioctl: CDROMEJECT_SW entered.\n");
3929                 if (fam0_drive) return (0);
3930                 D_S[d].f_eject=arg;
3931                 return (0);
3932                 
3933         case CDROMVOLCTRL:   /* Volume control */
3934                 msg(DBG_IOC,"ioctl: CDROMVOLCTRL entered.\n");
3935                 st=verify_area(VERIFY_READ,(void *) arg,sizeof(volctrl));
3936                 if (st) return (st);
3937                 memcpy_fromfs(&volctrl,(char *) arg,sizeof(volctrl));
3938                 D_S[d].vol_chan0=0;
3939                 D_S[d].vol_ctrl0=volctrl.channel0;
3940                 D_S[d].vol_chan1=1;
3941                 D_S[d].vol_ctrl1=volctrl.channel1;
3942                 i=cc_SetVolume();
3943                 return (0);
3944                 
3945         case CDROMVOLREAD:   /* read Volume settings from drive */
3946                 msg(DBG_IOC,"ioctl: CDROMVOLREAD entered.\n");
3947                 st=verify_area(VERIFY_WRITE,(void *)arg,sizeof(volctrl));
3948                 if (st) return (st);
3949                 st=cc_GetVolume();
3950                 if (st<0) return (st);
3951                 volctrl.channel0=D_S[d].vol_ctrl0;
3952                 volctrl.channel1=D_S[d].vol_ctrl1;
3953                 volctrl.channel2=0;
3954                 volctrl.channel2=0;
3955                 memcpy_tofs((void *)arg,&volctrl,sizeof(volctrl));
3956                 return (0);
3957 
3958         case CDROMSUBCHNL:   /* Get subchannel info */
3959                 msg(DBG_IOS,"ioctl: CDROMSUBCHNL entered.\n");
3960                 if ((st_spinning)||(!subq_valid)) { i=cc_ReadSubQ();
3961                                                     if (i<0) return (-EIO);
3962                                             }
3963                 st=verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct cdrom_subchnl));
3964                 if (st) return (st);
3965                 st=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_subchnl));
3966                 if (st) return (st);
3967                 memcpy_fromfs(&SC, (void *) arg, sizeof(struct cdrom_subchnl));
3968                 switch (D_S[d].audio_state)
3969                 {
3970                 case audio_playing:
3971                         SC.cdsc_audiostatus=CDROM_AUDIO_PLAY;
3972                         break;
3973                 case audio_pausing:
3974                         SC.cdsc_audiostatus=CDROM_AUDIO_PAUSED;
3975                         break;
3976                 default:
3977                         SC.cdsc_audiostatus=CDROM_AUDIO_NO_STATUS;
3978                         break;
3979                 }
3980                 SC.cdsc_adr=D_S[d].SubQ_ctl_adr;
3981                 SC.cdsc_ctrl=D_S[d].SubQ_ctl_adr>>4;
3982                 SC.cdsc_trk=bcd2bin(D_S[d].SubQ_trk);
3983                 SC.cdsc_ind=bcd2bin(D_S[d].SubQ_pnt_idx);
3984                 if (SC.cdsc_format==CDROM_LBA)
3985                 {
3986                         SC.cdsc_absaddr.lba=msf2blk(D_S[d].SubQ_run_tot);
3987                         SC.cdsc_reladdr.lba=msf2blk(D_S[d].SubQ_run_trk);
3988                 }
3989                 else /* not only if (SC.cdsc_format==CDROM_MSF) */
3990                 {
3991                         SC.cdsc_absaddr.msf.minute=(D_S[d].SubQ_run_tot>>16)&0x00FF;
3992                         SC.cdsc_absaddr.msf.second=(D_S[d].SubQ_run_tot>>8)&0x00FF;
3993                         SC.cdsc_absaddr.msf.frame=D_S[d].SubQ_run_tot&0x00FF;
3994                         SC.cdsc_reladdr.msf.minute=(D_S[d].SubQ_run_trk>>16)&0x00FF;
3995                         SC.cdsc_reladdr.msf.second=(D_S[d].SubQ_run_trk>>8)&0x00FF;
3996                         SC.cdsc_reladdr.msf.frame=D_S[d].SubQ_run_trk&0x00FF;
3997                 }
3998                 memcpy_tofs((void *) arg, &SC, sizeof(struct cdrom_subchnl));
3999                 msg(DBG_IOS,"CDROMSUBCHNL: %1X %02X %08X %08X %02X %02X %06X %06X\n",
4000                     SC.cdsc_format,SC.cdsc_audiostatus,
4001                     SC.cdsc_adr,SC.cdsc_ctrl,
4002                     SC.cdsc_trk,SC.cdsc_ind,
4003                     SC.cdsc_absaddr,SC.cdsc_reladdr);
4004                 return (0);
4005                 
4006         case CDROMREADMODE1:
4007                 msg(DBG_IOC,"ioctl: CDROMREADMODE1 requested.\n");
4008                 cc_ModeSelect(CD_FRAMESIZE);
4009                 cc_ModeSense();
4010                 D_S[d].mode=READ_M1;
4011                 return (0);
4012                 
4013         case CDROMREADMODE2: /* not usable at the moment */
4014                 msg(DBG_IOC,"ioctl: CDROMREADMODE2 requested.\n");
4015                 cc_ModeSelect(CD_FRAMESIZE_RAW1);
4016                 cc_ModeSense();
4017                 D_S[d].mode=READ_M2;
4018                 return (0);
4019                 
4020         case CDROMAUDIOBUFSIZ: /* configure the audio buffer size */
4021                 msg(DBG_IOC,"ioctl: CDROMAUDIOBUFSIZ entered.\n");
4022                 if (D_S[d].sbp_audsiz>0) vfree(D_S[d].aud_buf);
4023                 D_S[d].aud_buf=NULL;
4024                 D_S[d].sbp_audsiz=arg;
4025                 if (D_S[d].sbp_audsiz>0)
4026                 {
4027                         D_S[d].aud_buf=(u_char *) vmalloc(D_S[d].sbp_audsiz*CD_FRAMESIZE_RAW);
4028                         if (D_S[d].aud_buf==NULL)
4029                         {
4030                                 msg(DBG_INF,"audio buffer (%d frames) not available.\n",D_S[d].sbp_audsiz);
4031                                 D_S[d].sbp_audsiz=0;
4032                         }
4033                         else msg(DBG_INF,"audio buffer size: %d frames.\n",D_S[d].sbp_audsiz);
4034                 }
4035                 return (D_S[d].sbp_audsiz);
4036 
4037         case CDROMREADAUDIO:
4038         { /* start of CDROMREADAUDIO */
4039                 int i=0, j=0, frame, block;
4040                 u_int try=0;
4041                 u_long timeout;
4042                 u_char *p;
4043                 u_int data_tries = 0;
4044                 u_int data_waits = 0;
4045                 u_int data_retrying = 0;
4046                 int status_tries;
4047                 int error_flag;
4048                 
4049                 msg(DBG_IOC,"ioctl: CDROMREADAUDIO entered.\n");
4050                 if (fam0_drive) return (-EINVAL);
4051                 if (famL_drive) return (-EINVAL);
4052                 if (famT_drive) return (-EINVAL);
4053                 if (D_S[d].aud_buf==NULL) return (-EINVAL);
4054                 i=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_read_audio));
4055                 if (i) return (i);
4056                 memcpy_fromfs(&read_audio, (void *) arg, sizeof(struct cdrom_read_audio));
4057                 if (read_audio.nframes>D_S[d].sbp_audsiz) return (-EINVAL);
4058                 i=verify_area(VERIFY_WRITE, read_audio.buf,
4059                               read_audio.nframes*CD_FRAMESIZE_RAW);
4060                 if (i) return (i);
4061                 
4062                 if (read_audio.addr_format==CDROM_MSF) /* MSF-bin specification of where to start */
4063                         block=msf2lba(&read_audio.addr.msf.minute);
4064                 else if (read_audio.addr_format==CDROM_LBA) /* lba specification of where to start */
4065                         block=read_audio.addr.lba;
4066                 else return (-EINVAL);
4067 #if 000
4068                 i=cc_SetSpeed(speed_150,0,0);
4069                 if (i) msg(DBG_AUD,"read_audio: SetSpeed error %d\n", i);
4070 #endif
4071                 msg(DBG_AUD,"read_audio: lba: %d, msf: %06X\n",
4072                     block, blk2msf(block));
4073                 msg(DBG_AUD,"read_audio: before cc_ReadStatus.\n");
4074                 while (busy_data) sbp_sleep(HZ/10); /* wait a bit */
4075                 busy_audio=1;
4076                 error_flag=0;
4077                 for (data_tries=5; data_tries>0; data_tries--)
4078                 {
4079                         msg(DBG_AUD,"data_tries=%d ...\n", data_tries);
4080                         D_S[d].mode=READ_AU;
4081                         cc_ModeSelect(CD_FRAMESIZE_RAW);
4082                         cc_ModeSense();
4083                         for (status_tries=3; status_tries > 0; status_tries--)
4084                         {
4085                                 flags_cmd_out |= f_respo3;
4086                                 cc_ReadStatus();
4087                                 if (sbp_status() != 0) break;
4088                                 if (st_check) cc_ReadError();
4089                                 sbp_sleep(1);    /* wait a bit, try again */
4090                         }
4091                         if (status_tries == 0)
4092                         {
4093                                 msg(DBG_AUD,"read_audio: sbp_status: failed after 3 tries.\n");
4094                                 continue;
4095                         }
4096                         msg(DBG_AUD,"read_audio: sbp_status: ok.\n");
4097                         
4098                         flags_cmd_out = f_putcmd | f_respo2 | f_ResponseStatus | f_obey_p_check;
4099                         if (fam0L_drive)
4100                         {
4101                                 flags_cmd_out |= f_lopsta | f_getsta | f_bit1;
4102                                 cmd_type=READ_M2;
4103                                 drvcmd[0]=CMD0_READ_XA; /* "read XA frames", old drives */
4104                                 drvcmd[1]=(block>>16)&0x000000ff;
4105                                 drvcmd[2]=(block>>8)&0x000000ff;
4106                                 drvcmd[3]=block&0x000000ff;
4107                                 drvcmd[4]=0;
4108                                 drvcmd[5]=read_audio.nframes; /* # of frames */
4109                                 drvcmd[6]=0;
4110                         }
4111                         else if (fam1_drive)
4112                         {
4113                                 drvcmd[0]=CMD1_READ; /* "read frames", new drives */
4114                                 lba2msf(block,&drvcmd[1]); /* msf-bin format required */
4115                                 drvcmd[4]=0;
4116                                 drvcmd[5]=0;
4117                                 drvcmd[6]=read_audio.nframes; /* # of frames */
4118                         }
4119                         else if (fam2_drive)
4120                         {
4121                                 drvcmd[0]=CMD2_READ_XA2;
4122                                 lba2msf(block,&drvcmd[1]); /* msf-bin format required */
4123                                 drvcmd[4]=0;
4124                                 drvcmd[5]=read_audio.nframes; /* # of frames */
4125                                 drvcmd[6]=0x11; /* raw mode */
4126                         }
4127                         else if (famT_drive) /* CD-55A: not tested yet */
4128                         {
4129                         }
4130                         msg(DBG_AUD,"read_audio: before giving \"read\" command.\n");
4131                         for (i=0;i<7;i++) OUT(CDo_command,drvcmd[i]);
4132                         sbp_sleep(0);
4133                         msg(DBG_AUD,"read_audio: after giving \"read\" command.\n");
4134                         for (frame=1;frame<2 && !error_flag; frame++)
4135                         {
4136                                 try=maxtim_data;
4137                                 for (timeout=jiffies+9*HZ; ; )
4138                                 {
4139                                         for ( ; try!=0;try--)
4140                                         {
4141                                                 j=inb(CDi_status);
4142                                                 if (!(j&s_not_data_ready)) break;
4143                                                 if (!(j&s_not_result_ready)) break;
4144                                                 if (fam0L_drive) if (j&s_attention) break;
4145                                         }
4146                                         if (try != 0 || timeout <= jiffies) break;
4147                                         if (data_retrying == 0) data_waits++;
4148                                         data_retrying = 1;
4149                                         sbp_sleep(1);
4150                                         try = 1;
4151                                 }
4152                                 if (try==0)
4153                                 {
4154                                         msg(DBG_INF,"read_audio: sbp_data: CDi_status timeout.\n");
4155                                         error_flag++;
4156                                         break;
4157                                 }
4158                                 msg(DBG_AUD,"read_audio: sbp_data: CDi_status ok.\n");
4159                                 if (j&s_not_data_ready)
4160                                 {
4161                                         msg(DBG_INF, "read_audio: sbp_data: DATA_READY timeout.\n");
4162                                         error_flag++;
4163                                         break;
4164                                 }
4165                                 msg(DBG_AUD,"read_audio: before reading data.\n");
4166                                 error_flag=0;
4167                                 p = D_S[d].aud_buf;
4168                                 if (sbpro_type==1) OUT(CDo_sel_i_d,1);
4169                                 insb(CDi_data, p, read_audio.nframes*CD_FRAMESIZE_RAW);
4170                                 if (sbpro_type==1) OUT(CDo_sel_i_d,0);
4171                                 data_retrying = 0;
4172                         }
4173                         msg(DBG_AUD,"read_audio: after reading data.\n");
4174                         if (error_flag)    /* must have been spurious D_RDY or (ATTN&&!D_RDY) */
4175                         {
4176                                 msg(DBG_AUD,"read_audio: read aborted by drive\n");
4177 #if 0000
4178                                 i=cc_DriveReset();                /* ugly fix to prevent a hang */
4179 #else
4180                                 i=cc_ReadError();
4181 #endif 0000
4182                                 continue;
4183                         }
4184                         if (fam0L_drive)
4185                         {
4186                                 i=maxtim_data;
4187                                 for (timeout=jiffies+9*HZ; timeout > jiffies; timeout--)
4188                                 {
4189                                         for ( ;i!=0;i--)
4190                                         {
4191                                                 j=inb(CDi_status);
4192                                                 if (!(j&s_not_data_ready)) break;
4193                                                 if (!(j&s_not_result_ready)) break;
4194                                                 if (j&s_attention) break;
4195                                         }
4196                                         if (i != 0 || timeout <= jiffies) break;
4197                                         sbp_sleep(0);
4198                                         i = 1;
4199                                 }
4200                                 if (i==0) msg(DBG_AUD,"read_audio: STATUS TIMEOUT AFTER READ");
4201                                 if (!(j&s_attention))
4202                                 {
4203                                         msg(DBG_AUD,"read_audio: sbp_data: timeout waiting DRV_ATTN - retrying\n");
4204                                         i=cc_DriveReset();  /* ugly fix to prevent a hang */
4205                                         continue;
4206                                 }
4207                         }
4208                         do
4209                         {
4210                                 if (fam0L_drive) cc_ReadStatus();
4211                                 i=ResponseStatus();  /* builds status_bits, returns orig. status (old) or faked p_success (new) */
4212                                 if (i<0) { msg(DBG_AUD,
4213                                                "read_audio: cc_ReadStatus error after read: %02X\n",
4214                                                D_S[d].status_bits);
4215                                            continue; /* FIXME */
4216                                    }
4217                         }
4218                         while ((fam0L_drive)&&(!st_check)&&(!(i&p_success)));
4219                         if (st_check)
4220                         {
4221                                 i=cc_ReadError();
4222                                 msg(DBG_AUD,"read_audio: cc_ReadError was necessary after read: %02X\n",i);
4223                                 continue;
4224                         }
4225                         memcpy_tofs((u_char *) read_audio.buf,
4226                                     (u_char *) D_S[d].aud_buf,
4227                                     read_audio.nframes*CD_FRAMESIZE_RAW);
4228                         msg(DBG_AUD,"read_audio: memcpy_tofs done.\n");
4229                         break;
4230                 }
4231                 cc_ModeSelect(CD_FRAMESIZE);
4232                 cc_ModeSense();
4233                 D_S[d].mode=READ_M1;
4234                 busy_audio=0;
4235                 if (data_tries == 0)
4236                 {
4237                         msg(DBG_AUD,"read_audio: failed after 5 tries.\n");
4238                         return (-8);
4239                 }
4240                 msg(DBG_AUD,"read_audio: successful return.\n");
4241                 return (0);
4242         } /* end of CDROMREADAUDIO */
4243                 
4244         case CDROMMULTISESSION: /* tell start-of-last-session */
4245                 msg(DBG_IOC,"ioctl: CDROMMULTISESSION entered.\n");
4246                 st=verify_area(VERIFY_READ, (void *) arg, sizeof(struct cdrom_multisession));
4247                 if (st) return (st);
4248                 memcpy_fromfs(&ms_info, (void *) arg, sizeof(struct cdrom_multisession));
4249                 if (ms_info.addr_format==CDROM_MSF) /* MSF-bin requested */
4250                         lba2msf(D_S[d].lba_multi,&ms_info.addr.msf.minute);
4251                 else if (ms_info.addr_format==CDROM_LBA) /* lba requested */
4252                         ms_info.addr.lba=D_S[d].lba_multi;
4253                 else return (-EINVAL);
4254                 if (D_S[d].f_multisession) ms_info.xa_flag=1; /* valid redirection address */
4255                 else ms_info.xa_flag=0; /* invalid redirection address */
4256                 st=verify_area(VERIFY_WRITE,(void *) arg, sizeof(struct cdrom_multisession));
4257                 if (st) return (st);
4258                 memcpy_tofs((void *) arg, &ms_info, sizeof(struct cdrom_multisession));
4259                 msg(DBG_MUL,"ioctl: CDROMMULTISESSION done (%d, %08X).\n",
4260                     ms_info.xa_flag, ms_info.addr.lba);
4261                 return (0);
4262                 
4263         case BLKRASET:
4264                 if(!suser())  return -EACCES;
4265                 if(!(inode->i_rdev)) return -EINVAL;
4266                 if(arg > 0xff) return -EINVAL;
4267                 read_ahead[MAJOR(inode->i_rdev)] = arg;
4268                 return (0);
4269                 
4270         default:
4271                 msg(DBG_IOC,"ioctl: unknown function request %04X\n", cmd);
4272                 return (-EINVAL);
4273         } /* end switch(cmd) */
4274 }
4275 /*==========================================================================*/
4276 /*
4277  *  Take care of the different block sizes between cdrom and Linux.
4278  */
4279 static void sbp_transfer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
4280 {
4281         long offs;
4282         
4283         while ( (CURRENT->nr_sectors > 0) &&
4284                (CURRENT->sector/4 >= D_S[d].sbp_first_frame) &&
4285                (CURRENT->sector/4 <= D_S[d].sbp_last_frame) )
4286         {
4287                 offs = (CURRENT->sector - D_S[d].sbp_first_frame * 4) * 512;
4288                 memcpy(CURRENT->buffer, D_S[d].sbp_buf + offs, 512);
4289                 CURRENT->nr_sectors--;
4290                 CURRENT->sector++;
4291                 CURRENT->buffer += 512;
4292         }
4293 }
4294 /*==========================================================================*/
4295 /*
4296  *  I/O request routine, called from Linux kernel.
4297  */
4298 static void DO_SBPCD_REQUEST(void)
     /* [previous][next][first][last][top][bottom][index][help] */
4299 {
4300         u_int block;
4301         u_int nsect;
4302         int i, status_tries, data_tries;
4303         
4304  request_loop:
4305         INIT_REQUEST;
4306         sti();
4307         
4308         if ((CURRENT == NULL) || CURRENT->rq_status == RQ_INACTIVE)
4309                 goto err_done;
4310         if (CURRENT -> sector == -1)
4311                 goto err_done;
4312         if (CURRENT->cmd != READ)
4313         {
4314                 msg(DBG_INF, "bad cmd %d\n", CURRENT->cmd);
4315                 goto err_done;
4316         }
4317         i = MINOR(CURRENT->rq_dev);
4318         if ( (i<0) || (i>=NR_SBPCD) || (D_S[i].drv_id==-1))
4319         {
4320                 msg(DBG_INF, "do_request: bad device: %s\n",
4321                         kdevname(CURRENT->rq_dev));
4322                 goto err_done;
4323         }
4324         while (busy_audio) sbp_sleep(HZ); /* wait a bit */
4325         busy_data=1;
4326         
4327         if (D_S[i].audio_state==audio_playing) goto err_done;
4328         if (d!=i) switch_drive(i);
4329         
4330         block = CURRENT->sector; /* always numbered as 512-byte-pieces */
4331         nsect = CURRENT->nr_sectors; /* always counted as 512-byte-pieces */
4332         
4333         msg(DBG_BSZ,"read sector %d (%d sectors)\n", block, nsect);
4334 #if 0
4335         msg(DBG_MUL,"read LBA %d\n", block/4);
4336 #endif
4337         
4338         sbp_transfer();
4339         /* if we satisfied the request from the buffer, we're done. */
4340         if (CURRENT->nr_sectors == 0)
4341         {
4342                 end_request(1);
4343                 goto request_loop;
4344         }
4345 
4346 #if FUTURE
4347         i=prepare(0,0); /* at moment not really a hassle check, but ... */
4348         if (i!=0)
4349                 msg(DBG_INF,"\"prepare\" tells error %d -- ignored\n", i);
4350 #endif FUTURE
4351         
4352         if (!st_spinning) cc_SpinUp();
4353         
4354         for (data_tries=n_retries; data_tries > 0; data_tries--)
4355         {
4356                 for (status_tries=3; status_tries > 0; status_tries--)
4357                 {
4358                         flags_cmd_out |= f_respo3;
4359                         cc_ReadStatus();
4360                         if (sbp_status() != 0) break;
4361                         if (st_check) cc_ReadError();
4362                         sbp_sleep(1);    /* wait a bit, try again */
4363                 }
4364                 if (status_tries == 0)
4365                 {
4366                         msg(DBG_INF,"sbp_status: failed after 3 tries\n");
4367                         break;
4368                 }
4369                 
4370                 sbp_read_cmd();
4371                 sbp_sleep(0);
4372                 if (sbp_data() != 0)
4373                 {
4374                         end_request(1);
4375                         goto request_loop;
4376                 }
4377         }
4378         
4379  err_done:
4380         busy_data=0;
4381         end_request(0);
4382         sbp_sleep(0);    /* wait a bit, try again */
4383         goto request_loop;
4384 }
4385 /*==========================================================================*/
4386 /*
4387  *  build and send the READ command.
4388  */
4389 static void sbp_read_cmd(void)
     /* [previous][next][first][last][top][bottom][index][help] */
4390 {
4391 #undef OLD
4392 
4393         int i;
4394         int block;
4395         
4396         D_S[d].sbp_first_frame=D_S[d].sbp_last_frame=-1;      /* purge buffer */
4397         D_S[d].sbp_current = 0;
4398         block=CURRENT->sector/4;
4399         if (block+D_S[d].sbp_bufsiz <= D_S[d].CDsize_frm)
4400                 D_S[d].sbp_read_frames = D_S[d].sbp_bufsiz;
4401         else
4402         {
4403                 D_S[d].sbp_read_frames=D_S[d].CDsize_frm-block;
4404                 /* avoid reading past end of data */
4405                 if (D_S[d].sbp_read_frames < 1)
4406                 {
4407                         msg(DBG_INF,"requested frame %d, CD size %d ???\n",
4408                             block, D_S[d].CDsize_frm);
4409                         D_S[d].sbp_read_frames=1;
4410                 }
4411         }
4412         
4413         flags_cmd_out = f_putcmd | f_respo2 | f_ResponseStatus | f_obey_p_check;
4414         clr_cmdbuf();
4415         if (fam0L_drive)
4416         {
4417                 flags_cmd_out |= f_lopsta | f_getsta | f_bit1;
4418                 if (D_S[d].xa_byte==0x20)
4419                 {
4420                         cmd_type=READ_M2;
4421                         drvcmd[0]=CMD0_READ_XA; /* "read XA frames", old drives */
4422                         drvcmd[1]=(block>>16)&0x000000ff;
4423                         drvcmd[2]=(block>>8)&0x000000ff;
4424                         drvcmd[3]=block&0x000000ff;
4425                         drvcmd[5]=D_S[d].sbp_read_frames;
4426                 }
4427                 else
4428                 {
4429                         drvcmd[0]=CMD0_READ; /* "read frames", old drives */
4430                         if (D_S[d].drv_type>=drv_201)
4431                         {
4432                                 lba2msf(block,&drvcmd[1]); /* msf-bcd format required */
4433                                 bin2bcdx(&drvcmd[1]);
4434                                 bin2bcdx(&drvcmd[2]);
4435                                 bin2bcdx(&drvcmd[3]);
4436                         }
4437                         else
4438                         {
4439                                 drvcmd[1]=(block>>16)&0x000000ff;
4440                                 drvcmd[2]=(block>>8)&0x000000ff;
4441                                 drvcmd[3]=block&0x000000ff;
4442                         }
4443                         drvcmd[5]=D_S[d].sbp_read_frames;
4444                         drvcmd[6]=(D_S[d].drv_type<drv_201)?0:2; /* flag "lba or msf-bcd format" */
4445                 }
4446         }
4447         else if (fam1_drive)
4448         {
4449                 drvcmd[0]=CMD1_READ;
4450                 lba2msf(block,&drvcmd[1]); /* msf-bin format required */
4451                 drvcmd[6]=D_S[d].sbp_read_frames;
4452         }
4453         else if (fam2_drive)
4454         {
4455                 drvcmd[0]=CMD2_READ;
4456                 lba2msf(block,&drvcmd[1]); /* msf-bin format required */
4457                 drvcmd[5]=D_S[d].sbp_read_frames;
4458                 drvcmd[6]=0x02;
4459         }
4460         else if (famT_drive)
4461         {
4462                 drvcmd[0]=CMDT_READ;
4463                 drvcmd[2]=(block>>24)&0x0ff;
4464                 drvcmd[3]=(block>>16)&0x0ff;
4465                 drvcmd[4]=(block>>8)&0x0ff;
4466                 drvcmd[5]=block&0x0ff;
4467                 drvcmd[7]=(D_S[d].sbp_read_frames>>8)&0x0ff;
4468                 drvcmd[8]=D_S[d].sbp_read_frames&0x0ff;
4469         }
4470 #ifdef OLD
4471         SBPCD_CLI;
4472         for (i=0;i<7;i++) OUT(CDo_command,drvcmd[i]);
4473         if (famT_drive) for (i=7;i<10;i++) OUT(CDo_command,drvcmd[i]);
4474         SBPCD_STI;
4475 #else
4476         flags_cmd_out=f_putcmd;
4477         response_count=0;
4478         i=cmd_out(); /* immediate return here - read data "ourselves" */
4479         if (i<0) msg(DBG_INF,"error giving READ command: %0d\n", i);
4480 #endif OLD
4481         return;
4482 }
4483 /*==========================================================================*/
4484 /*
4485  *  Check the completion of the read-data command.  On success, read
4486  *  the D_S[d].sbp_bufsiz * 2048 bytes of data from the disk into buffer.
4487  */
4488 static int sbp_data(void)
     /* [previous][next][first][last][top][bottom][index][help] */
4489 {
4490         int i=0, j=0, l, frame;
4491         u_int try=0;
4492         u_long timeout;
4493         u_char *p;
4494         u_int data_tries = 0;
4495         u_int data_waits = 0;
4496         u_int data_retrying = 0;
4497         int error_flag;
4498         int xa_count;
4499         int max_latency;
4500         int success;
4501         int wait;
4502         int duration;
4503         
4504         error_flag=0;
4505         success=0;
4506 #if LONG_TIMING
4507         max_latency=9*HZ;
4508 #else
4509         if (D_S[d].f_multisession) max_latency=9*HZ;
4510         else max_latency=3*HZ;
4511 #endif
4512         msg(DBG_TE2,"beginning to READ\n");
4513         duration=jiffies;
4514         for (frame=0;frame<D_S[d].sbp_read_frames&&!error_flag; frame++)
4515         {
4516                 SBPCD_CLI;
4517                 
4518                 del_timer(&data_timer);
4519                 data_timer.expires=jiffies+max_latency;
4520                 timed_out_data=0;
4521                 add_timer(&data_timer);
4522                 while (!timed_out_data) 
4523                 {
4524                         if (D_S[d].f_multisession) try=maxtim_data*4;
4525                         else try=maxtim_data;
4526                         msg(DBG_000,"sbp_data: CDi_status loop: try=%d.\n",try);
4527                         for ( ; try!=0;try--)
4528                         {
4529                                 j=inb(CDi_status);
4530                                 if (!(j&s_not_data_ready)) break;;
4531                                 if (!(j&s_not_result_ready)) break;
4532                                 if (fam0L_drive) if (j&s_attention) break;
4533                         }
4534                         if (!(j&s_not_data_ready)) goto data_ready;
4535                         if (try==0)
4536                         {
4537                                 if (data_retrying == 0) data_waits++;
4538                                 data_retrying = 1;
4539                                 msg(DBG_000,"sbp_data: CDi_status loop: sleeping.\n");
4540                                 sbp_sleep(1);
4541                                 try = 1;
4542                         }
4543                 }
4544                 msg(DBG_INF,"sbp_data: CDi_status loop expired.\n");
4545         data_ready:
4546                 del_timer(&data_timer);
4547 
4548                 if (timed_out_data)
4549                 {
4550                         msg(DBG_INF,"sbp_data: CDi_status timeout (timed_out_data) (%02X).\n", j);
4551                         error_flag++;
4552                         break;
4553                 }
4554                 if (try==0)
4555                 {
4556                         msg(DBG_INF,"sbp_data: CDi_status timeout (try=0) (%02X).\n", j);
4557                         error_flag++;
4558                         break;
4559                 }
4560                 if (!(j&s_not_result_ready))
4561                 {
4562                         msg(DBG_INF, "sbp_data: RESULT_READY where DATA_READY awaited (%02X).\n", j);
4563                         response_count=20;
4564                         j=ResponseInfo();
4565                         j=inb(CDi_status);
4566                 }
4567                 if (j&s_not_data_ready)
4568                 {
4569                         if ((D_S[d].ored_ctl_adr&0x40)==0)
4570                                 msg(DBG_INF, "CD contains no data tracks.\n");
4571                         else msg(DBG_INF, "sbp_data: DATA_READY timeout (%02X).\n", j);
4572                         error_flag++;
4573                         break;
4574                 }
4575                 SBPCD_STI;
4576                 error_flag=0;
4577                 msg(DBG_000, "sbp_data: beginning to read.\n");
4578                 p = D_S[d].sbp_buf + frame *  CD_FRAMESIZE;
4579                 if (sbpro_type==1) OUT(CDo_sel_i_d,1);
4580                 if (cmd_type==READ_M2) insb(CDi_data, xa_head_buf, CD_XA_HEAD);
4581                 insb(CDi_data, p, CD_FRAMESIZE);
4582                 if (cmd_type==READ_M2) insb(CDi_data, xa_tail_buf, CD_XA_TAIL);
4583                 if (famT_drive) msg(DBG_TE2, "================frame read=================.\n");
4584                 D_S[d].sbp_current++;
4585                 if (sbpro_type==1) OUT(CDo_sel_i_d,0);
4586                 if (cmd_type==READ_M2)
4587                 {
4588                         for (xa_count=0;xa_count<CD_XA_HEAD;xa_count++)
4589                                 sprintf(&msgbuf[xa_count*3], " %02X", xa_head_buf[xa_count]);
4590                         msgbuf[xa_count*3]=0;
4591                         msg(DBG_XA1,"xa head:%s\n", msgbuf);
4592                 }
4593                 data_retrying = 0;
4594                 data_tries++;
4595                 if (data_tries >= 1000)
4596                 {
4597                         msg(DBG_INF,"sbp_data() statistics: %d waits in %d frames.\n", data_waits, data_tries);
4598                         data_waits = data_tries = 0;
4599                 }
4600         }
4601         duration=jiffies-duration;
4602         msg(DBG_TE2,"time to read %d frames: %d jiffies .\n",frame,duration);
4603         if (famT_drive)
4604         {
4605                 wait=8;
4606                 do
4607                 {
4608                         sbp_sleep(1);
4609                         OUT(CDo_sel_i_d,0);
4610                         i=inb(CDi_status);
4611                         if (!(i&s_not_data_ready))
4612                         {
4613                                 OUT(CDo_sel_i_d,1);
4614                                 j=0;
4615                                 do
4616                                 {
4617                                         i=inb(CDi_data);
4618                                         j++;
4619                                         i=inb(CDi_status);
4620                                 }
4621                                 while (!(i&s_not_data_ready));
4622                                 msg(DBG_TEA, "=============too much data (%d bytes)=================.\n", j);
4623                         }
4624                         if (!(i&s_not_result_ready))
4625                         {
4626                                 OUT(CDo_sel_i_d,0);
4627                                 l=0;
4628                                 do
4629                                 {
4630                                         infobuf[l++]=inb(CDi_info);
4631                                         i=inb(CDi_status);
4632                                 }
4633                                 while (!(i&s_not_result_ready));
4634                                 if (infobuf[0]==0x00) success=1;
4635 #if 1
4636                                 for (j=0;j<l;j++) sprintf(&msgbuf[j*3], " %02X", infobuf[j]);
4637                                 msgbuf[j*3]=0;
4638                                 msg(DBG_TE2,"sbp_data info response:%s\n", msgbuf);
4639 #endif
4640                                 if (infobuf[0]==0x02)
4641                                 {
4642                                         error_flag++;
4643                                         do
4644                                         {
4645                                                 ++recursion;
4646                                                 if (recursion>1) msg(DBG_TEA,"cmd_out_T READ_ERR recursion (sbp_data): %d !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",recursion);
4647                                                 else msg(DBG_TEA,"sbp_data: CMDT_READ_ERR necessary.\n");
4648                                                 clr_cmdbuf();
4649                                                 drvcmd[0]=CMDT_READ_ERR;
4650                                                 j=cmd_out_T(); /* !!! recursive here !!! */
4651                                                 --recursion;
4652                                                 sbp_sleep(1);
4653                                         }
4654                                         while (j<0);
4655                                         D_S[d].error_state=infobuf[2];
4656                                         D_S[d].b3=infobuf[3];
4657                                         D_S[d].b4=infobuf[4];
4658                                 }
4659                                 break;
4660                         }
4661                         else
4662                         {
4663 #if 0
4664                                 msg(DBG_TEA, "============= waiting for result=================.\n");
4665                                 sbp_sleep(1);
4666 #endif
4667                         }
4668                 }
4669                 while (wait--);
4670         }
4671 
4672         if (error_flag) /* must have been spurious D_RDY or (ATTN&&!D_RDY) */
4673         {
4674                 msg(DBG_TEA, "================error flag: %d=================.\n", error_flag);
4675                 msg(DBG_INF,"sbp_data: read aborted by drive.\n");
4676 #if 1
4677                 i=cc_DriveReset(); /* ugly fix to prevent a hang */
4678 #else
4679                 i=cc_ReadError();
4680 #endif
4681                 return (0);
4682         }
4683         
4684         if (fam0L_drive)
4685         {
4686                 SBPCD_CLI;
4687                 i=maxtim_data;
4688                 for (timeout=jiffies+HZ; timeout > jiffies; timeout--)
4689                 {
4690                         for ( ;i!=0;i--)
4691                         {
4692                                 j=inb(CDi_status);
4693                                 if (!(j&s_not_data_ready)) break;
4694                                 if (!(j&s_not_result_ready)) break;
4695                                 if (j&s_attention) break;
4696                         }
4697                         if (i != 0 || timeout <= jiffies) break;
4698                         sbp_sleep(0);
4699                         i = 1;
4700                 }
4701                 if (i==0) msg(DBG_INF,"status timeout after READ.\n");
4702                 if (!(j&s_attention))
4703                 {
4704                         msg(DBG_INF,"sbp_data: timeout waiting DRV_ATTN - retrying.\n");
4705                         i=cc_DriveReset();  /* ugly fix to prevent a hang */
4706                         SBPCD_STI;
4707                         return (0);
4708                 }
4709                 SBPCD_STI;
4710         }
4711         
4712 #if 0
4713         if (!success)
4714 #endif 0
4715                 do
4716                 {
4717                         if (fam0L_drive) cc_ReadStatus();
4718 #if 1
4719                         if (famT_drive) msg(DBG_TE2, "================before ResponseStatus=================.\n", i);
4720 #endif 1
4721                         i=ResponseStatus();  /* builds status_bits, returns orig. status (old) or faked p_success (new) */
4722 #if 1
4723                         if (famT_drive) msg(DBG_TE2, "================ResponseStatus: %d=================.\n", i);
4724 #endif 1
4725                         if (i<0)
4726                         {
4727                                 msg(DBG_INF,"bad cc_ReadStatus after read: %02X\n", D_S[d].status_bits);
4728                                 return (0);
4729                         }
4730                 }
4731                 while ((fam0L_drive)&&(!st_check)&&(!(i&p_success)));
4732         if (st_check)
4733         {
4734                 i=cc_ReadError();
4735                 msg(DBG_INF,"cc_ReadError was necessary after read: %d\n",i);
4736                 return (0);
4737         }
4738         if (fatal_err)
4739         {
4740                 fatal_err=0;
4741                 D_S[d].sbp_first_frame=D_S[d].sbp_last_frame=-1;      /* purge buffer */
4742                 D_S[d].sbp_current = 0;
4743                 msg(DBG_INF,"sbp_data: fatal_err - retrying.\n");
4744                 return (0);
4745         }
4746         
4747         D_S[d].sbp_first_frame = CURRENT -> sector / 4;
4748         D_S[d].sbp_last_frame = D_S[d].sbp_first_frame + D_S[d].sbp_read_frames - 1;
4749         sbp_transfer();
4750 #if 1
4751         if (famT_drive) msg(DBG_TE2, "================sbp_transfer() done=================.\n");
4752 #endif 1
4753         return (1);
4754 }
4755 /*==========================================================================*/
4756 /*==========================================================================*/
4757 /*
4758  *  Open the device special file.  Check that a disk is in. Read TOC.
4759  */
4760 static int sbpcd_open(struct inode *ip, struct file *fp)
     /* [previous][next][first][last][top][bottom][index][help] */
4761 {
4762         int i;
4763         
4764         i = MINOR(ip->i_rdev);
4765         if ((i<0) || (i>=NR_SBPCD) || (D_S[i].drv_id==-1))
4766         {
4767                 msg(DBG_INF, "open: bad device: %04X\n", ip->i_rdev);
4768                 return (-ENXIO);             /* no such drive */
4769         }
4770         if (fp->f_mode & 2)
4771                 return -EROFS;
4772         
4773         switch_drive(i);
4774         
4775         i=cc_ReadError();
4776         flags_cmd_out |= f_respo2;
4777         cc_ReadStatus();                         /* command: give 1-byte status */
4778         i=ResponseStatus();
4779         if (famT_drive&&(i<0))
4780         {
4781                 cc_DriveReset();
4782                 i=ResponseStatus();
4783                 i=ResponseStatus();
4784         }
4785         if (i<0)
4786         {
4787                 msg(DBG_INF,"sbpcd_open: ResponseStatus timed out (%d).\n",i);
4788                 return (-EIO);                  /* drive doesn't respond */
4789         }
4790         if (famT_drive) msg(DBG_TE2,"sbpcd_open: ResponseStatus=%02X\n", i);
4791         if (!st_door_closed)
4792         {
4793                 if (famT_drive) msg(DBG_TE2,"sbpcd_open: !st_door_closed.\n");
4794                 cc_CloseTray();
4795                 flags_cmd_out |= f_respo2;
4796                 cc_ReadStatus();
4797                 i=ResponseStatus();
4798         }
4799         if (!(famT_drive))
4800                 if (!st_spinning)
4801                 {
4802                         if (famT_drive) msg(DBG_TE2,"sbpcd_open: !st_spinning.\n");
4803                         cc_SpinUp();
4804                         flags_cmd_out |= f_respo2;
4805                         cc_ReadStatus();
4806                         i=ResponseStatus();
4807                 }
4808         if (famT_drive) msg(DBG_TE2,"sbpcd_open: status %02X\n", D_S[d].status_bits);
4809         if (!st_door_closed||!st_caddy_in)
4810         {
4811                 msg(DBG_INF, "sbpcd_open: no disk in drive.\n");
4812                 D_S[d].open_count=0;
4813 #if JUKEBOX
4814                 if (!fam0_drive)
4815                 {
4816                         i=UnLockDoor();
4817                         cc_SpinDown(); /* eject tray */
4818                 }
4819 #endif
4820                 return (-ENXIO);
4821         }
4822         /*
4823          * try to keep an "open" counter here and lock the door if 0->1.
4824          */
4825         MOD_INC_USE_COUNT;
4826         msg(DBG_LCK,"open_count: %d -> %d\n",
4827             D_S[d].open_count,D_S[d].open_count+1);
4828         if (++D_S[d].open_count<=1)
4829         {
4830                 i=LockDoor();
4831                 D_S[d].open_count=1;
4832                 if (famT_drive) msg(DBG_TE2,"sbpcd_open: before i=DiskInfo();.\n");
4833                 i=DiskInfo();
4834                 if (famT_drive) msg(DBG_TE2,"sbpcd_open: after i=DiskInfo();.\n");
4835                 if ((D_S[d].ored_ctl_adr&0x40)==0)
4836                         msg(DBG_INF,"CD contains no data tracks.\n");
4837         }
4838         if (!st_spinning) cc_SpinUp();
4839         return (0);
4840 }
4841 /*==========================================================================*/
4842 /*
4843  *  On close, we flush all sbp blocks from the buffer cache.
4844  */
4845 static void sbpcd_release(struct inode * ip, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
4846 {
4847         int i;
4848         
4849         i = MINOR(ip->i_rdev);
4850         if ((i<0) || (i>=NR_SBPCD) || (D_S[i].drv_id==-1))
4851         {
4852                 msg(DBG_INF, "release: bad device: %04X\n", ip->i_rdev);
4853                 return;
4854         }
4855         switch_drive(i);
4856         
4857         D_S[d].sbp_first_frame=D_S[d].sbp_last_frame=-1;
4858         sync_dev(ip->i_rdev);                   /* nonsense if read only device? */
4859         invalidate_buffers(ip->i_rdev);
4860         
4861         /*
4862          * try to keep an "open" counter here and unlock the door if 1->0.
4863          */
4864         MOD_DEC_USE_COUNT;
4865         msg(DBG_LCK,"open_count: %d -> %d\n",
4866             D_S[d].open_count,D_S[d].open_count-1);
4867         if (D_S[d].open_count>-2) /* CDROMEJECT may have been done */
4868         {
4869                 if (--D_S[d].open_count<=0) 
4870                 {
4871                         i=UnLockDoor();
4872                         if (D_S[d].audio_state!=audio_playing)
4873                                 if (D_S[d].f_eject) cc_SpinDown();
4874                         D_S[d].diskstate_flags &= ~cd_size_bit;
4875                         D_S[d].open_count=0; 
4876                 }
4877         }
4878 }
4879 /*==========================================================================*/
4880 /*
4881  *
4882  */
4883 static struct file_operations sbpcd_fops =
4884 {
4885         NULL,                   /* lseek - default */
4886         block_read,             /* read - general block-dev read */
4887         block_write,            /* write - general block-dev write */
4888         NULL,                   /* readdir - bad */
4889         NULL,                   /* select */
4890         sbpcd_ioctl,            /* ioctl */
4891         NULL,                   /* mmap */
4892         sbpcd_open,             /* open */
4893         sbpcd_release,          /* release */
4894         NULL,                   /* fsync */
4895         NULL,                   /* fasync */
4896         sbpcd_chk_disk_change,  /* media_change */
4897         NULL                    /* revalidate */
4898 };
4899 /*==========================================================================*/
4900 /*
4901  * accept "kernel command line" parameters 
4902  * (suggested by Peter MacDonald with SLS 1.03)
4903  *
4904  * This is only implemented for the first controller. Should be enough to
4905  * allow installing with a "strange" distribution kernel.
4906  *
4907  * use: tell LILO:
4908  *                 sbpcd=0x230,SoundBlaster
4909  *             or
4910  *                 sbpcd=0x300,LaserMate
4911  *             or
4912  *                 sbpcd=0x330,SoundScape
4913  *
4914  * (upper/lower case sensitive here - but all-lowercase is ok!!!).
4915  *
4916  * the address value has to be the CDROM PORT ADDRESS -
4917  * not the soundcard base address.
4918  * For the SPEA/SoundScape setup, DO NOT specify the "configuration port"
4919  * address, but the address which is really used for the CDROM (usually 8
4920  * bytes above).
4921  *
4922  */
4923 #if (SBPCD_ISSUE-1)
4924 static
4925 #endif
4926 void sbpcd_setup(const char *s, int *p)
     /* [previous][next][first][last][top][bottom][index][help] */
4927 {
4928         setup_done++;
4929         msg(DBG_INI,"sbpcd_setup called with %04X,%s\n",p[1], s);
4930         sbpro_type=0; /* default: "LaserMate" */
4931         if (p[0]>1) sbpro_type=p[2];
4932         if (!strcmp(s,str_sb)) sbpro_type=1;
4933         else if (!strcmp(s,str_sb_l)) sbpro_type=1;
4934         else if (!strcmp(s,str_sp)) sbpro_type=2;
4935         else if (!strcmp(s,str_sp_l)) sbpro_type=2;
4936         else if (!strcmp(s,str_ss)) sbpro_type=2;
4937         else if (!strcmp(s,str_ss_l)) sbpro_type=2;
4938         if (p[0]>0) sbpcd_ioaddr=p[1];
4939         
4940         CDo_command=sbpcd_ioaddr;
4941         CDi_info=sbpcd_ioaddr;
4942         CDi_status=sbpcd_ioaddr+1;
4943         CDo_sel_i_d=sbpcd_ioaddr+1;
4944         CDo_reset=sbpcd_ioaddr+2;
4945         CDo_enable=sbpcd_ioaddr+3; 
4946         if (sbpro_type==1)
4947         {
4948                 MIXER_addr=sbpcd_ioaddr-0x10+0x04;
4949                 MIXER_data=sbpcd_ioaddr-0x10+0x05;
4950                 CDi_data=sbpcd_ioaddr;
4951         }
4952         else CDi_data=sbpcd_ioaddr+2;
4953 }
4954 /*==========================================================================*/
4955 /*
4956  * Sequoia S-1000 CD-ROM Interface Configuration
4957  * as used within SPEA Media FX, Ensonic SoundScape and some Reveal cards
4958  * The soundcard has to get jumpered for the interface type "Panasonic"
4959  * (not Sony or Mitsumi) and to get soft-configured for
4960  *     -> configuration port address
4961  *     -> CDROM port offset (num_ports): has to be 8 here. Possibly this
4962  *        offset value determines the interface type (none, Panasonic,
4963  *        Mitsumi, Sony).
4964  *        The interface uses a configuration port (0x320, 0x330, 0x340, 0x350)
4965  *        some bytes below the real CDROM address.
4966  *         
4967  *        For the Panasonic style (LaserMate) interface and the configuration
4968  *        port 0x330, we have to use an offset of 8; so, the real CDROM port
4969  *        address is 0x338.
4970  */
4971 static int config_spea(void)
     /* [previous][next][first][last][top][bottom][index][help] */
4972 {
4973         int n_ports=0x10; /* 2:0x00, 8:0x10, 16:0x20, 32:0x30 */
4974         /* base address offset between configuration port and CDROM port */
4975         int irq_number=0; /* off:0x00, 2:0x01, 7:0x03, 12:0x05, 15:0x07 */
4976         int dma_channel=0; /* off: 0x00, 0:0x08, 1:0x18, 3:0x38, 5:0x58, 6:0x68 */
4977         int dack_polarity=0; /* L:0x00, H:0x80 */
4978         int drq_polarity=0x40; /* L:0x00, H:0x40 */
4979         int i;
4980 
4981 #define SPEA_REG_1 sbpcd_ioaddr-0x08+4
4982 #define SPEA_REG_2 sbpcd_ioaddr-0x08+5
4983         
4984         OUT(SPEA_REG_1,0xFF);
4985         i=inb(SPEA_REG_1);
4986         if (i!=0x0F)
4987         {
4988                 msg(DBG_SEQ,"no SPEA interface at %04X present.\n", sbpcd_ioaddr);
4989                 return (-1); /* no interface found */
4990         }
4991         OUT(SPEA_REG_1,0x04);
4992         OUT(SPEA_REG_2,0xC0);
4993         
4994         OUT(SPEA_REG_1,0x05);
4995         OUT(SPEA_REG_2,0x10|drq_polarity|dack_polarity);
4996         
4997 #if 1
4998 #define SPEA_PATTERN 0x80
4999 #else
5000 #define SPEA_PATTERN 0x00
5001 #endif
5002         OUT(SPEA_REG_1,0x06);
5003         OUT(SPEA_REG_2,dma_channel|irq_number|SPEA_PATTERN);
5004         OUT(SPEA_REG_2,dma_channel|irq_number|SPEA_PATTERN);
5005         
5006         OUT(SPEA_REG_1,0x09);
5007         i=(inb(SPEA_REG_2)&0xCF)|n_ports;
5008         OUT(SPEA_REG_2,i);
5009         
5010         sbpro_type = 0; /* acts like a LaserMate interface now */
5011         msg(DBG_SEQ,"found SoundScape interface at %04X.\n", sbpcd_ioaddr);
5012         return (0);
5013 }
5014 /*==========================================================================*/
5015 /*
5016  *  Test for presence of drive and initialize it.
5017  *  Called once at boot or load time.
5018  */
5019 #ifdef MODULE
5020 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
5021 #else
5022 int SBPCD_INIT(void)
5023 #endif MODULE
5024 {
5025         int i=0, j=0;
5026         int addr[2]={1, CDROM_PORT};
5027         int port_index;
5028         
5029         sti();
5030         
5031         msg(DBG_INF,"sbpcd.c %s\n", VERSION);
5032 #ifndef MODULE
5033 #if DISTRIBUTION
5034         if (!setup_done)
5035         {
5036                 msg(DBG_INF,"Looking for Matsushita/Panasonic, CreativeLabs, Longshine, TEAC CD-ROM drives\n");
5037                 msg(DBG_INF,"= = = = = = = = = = W A R N I N G = = = = = = = = = =\n");
5038                 msg(DBG_INF,"Auto-Probing can cause a hang (f.e. touching an NE2000 card).\n");
5039                 msg(DBG_INF,"If that happens, you have to reboot and use the\n");
5040                 msg(DBG_INF,"LILO (kernel) command line feature like:\n");
5041                 msg(DBG_INF,"   LILO boot: ... sbpcd=0x230,SoundBlaster\n");
5042                 msg(DBG_INF,"or like:\n");
5043                 msg(DBG_INF,"   LILO boot: ... sbpcd=0x300,LaserMate\n");
5044                 msg(DBG_INF,"or like:\n");
5045                 msg(DBG_INF,"   LILO boot: ... sbpcd=0x338,SoundScape\n");
5046                 msg(DBG_INF,"with your REAL address.\n");
5047                 msg(DBG_INF,"= = = = = = = = = = END of WARNING = = = = = == = = =\n");
5048         }
5049 #endif DISTRIBUTION
5050         sbpcd[0]=sbpcd_ioaddr; /* possibly changed by kernel command line */
5051         sbpcd[1]=sbpro_type; /* possibly changed by kernel command line */
5052 #endif MODULE
5053         
5054         for (port_index=0;port_index<NUM_PROBE;port_index+=2)
5055         {
5056                 addr[1]=sbpcd[port_index];
5057                 if (addr[1]==0) break;
5058                 if (check_region(addr[1],4))
5059                 {
5060                         msg(DBG_INF,"check_region: %03X is not free.\n",addr[1]);
5061                         continue;
5062                 }
5063                 if (sbpcd[port_index+1]==2) type=str_sp;
5064                 else if (sbpcd[port_index+1]==1) type=str_sb;
5065                 else type=str_lm;
5066                 sbpcd_setup(type, addr);
5067 #if DISTRIBUTION
5068                 msg(DBG_INF,"Scanning 0x%X (%s)...\n", CDo_command, type);
5069 #endif DISTRIBUTION
5070                 if (sbpcd[port_index+1]==2)
5071                 {
5072                         i=config_spea();
5073                         if (i<0) continue;
5074                 }
5075 #ifdef PATH_CHECK
5076                 if (check_card(addr[1])) continue;
5077 #endif PATH_CHECK
5078                 i=check_drives();
5079                 msg(DBG_INI,"check_drives done.\n");
5080                 if (i>=0) break; /* drive found */
5081         } /* end of cycling through the set of possible I/O port addresses */
5082         
5083         if (ndrives==0)
5084         {
5085                 msg(DBG_INF, "No drive found.\n");
5086 #ifdef MODULE
5087                 return -EIO;
5088 #else
5089                 goto init_done;
5090 #endif MODULE
5091         }
5092         
5093         if (port_index>0)
5094                 msg(DBG_INF, "You should configure sbpcd.h for your hardware.\n");
5095         check_datarate();
5096         msg(DBG_INI,"check_datarate done.\n");
5097         
5098 #if 0
5099         if (!famL_drive)
5100         {
5101                 OUT(CDo_reset,0);
5102                 sbp_sleep(HZ);
5103         }
5104 #endif 0
5105 
5106         for (j=0;j<NR_SBPCD;j++)
5107         {
5108                 if (D_S[j].drv_id==-1) continue;
5109                 switch_drive(j);
5110 #if 1
5111                 if (!famL_drive) cc_DriveReset();
5112 #endif 0
5113                 if (!st_spinning) cc_SpinUp();
5114                 D_S[d].sbp_first_frame = -1;  /* First frame in buffer */
5115                 D_S[d].sbp_last_frame = -1;   /* Last frame in buffer  */
5116                 D_S[d].sbp_read_frames = 0;   /* Number of frames being read to buffer */
5117                 D_S[d].sbp_current = 0;       /* Frame being currently read */
5118                 D_S[d].CD_changed=1;
5119                 D_S[d].frame_size=CD_FRAMESIZE;
5120                 D_S[d].f_eject=0;
5121 #if EJECT
5122                 if (!fam0_drive) D_S[d].f_eject=1;
5123 #endif EJECT
5124                 cc_ReadStatus();
5125                 i=ResponseStatus();  /* returns orig. status or p_busy_new */
5126                 if (famT_drive) i=ResponseStatus();  /* returns orig. status or p_busy_new */
5127                 if (i<0)
5128                         if (i!=-402)
5129                                 msg(DBG_INF,"init: ResponseStatus returns %d.\n",i);
5130                 else
5131                 {
5132                         if (st_check)
5133                         {
5134                                 i=cc_ReadError();
5135                                 msg(DBG_INI,"init: cc_ReadError returns %d\n",i);
5136                         }
5137                 }
5138                 msg(DBG_INI,"init: first GetStatus: %d\n",i);
5139                 msg(DBG_LCS,"init: first GetStatus: error_byte=%d\n",
5140                     D_S[d].error_byte);
5141                 if (D_S[d].error_byte==aud_12)
5142                 {
5143                         timeout=jiffies+2*HZ;
5144                         do
5145                         {
5146                                 i=GetStatus();
5147                                 msg(DBG_INI,"init: second GetStatus: %02X\n",i);
5148                                 msg(DBG_LCS,
5149                                     "init: second GetStatus: error_byte=%d\n",
5150                                     D_S[d].error_byte);
5151                                 if (i<0) break;
5152                                 if (!st_caddy_in) break;
5153                                 }
5154                         while ((!st_diskok)||(timeout<jiffies));
5155                 }
5156                 i=SetSpeed();
5157                 if (i>=0) D_S[d].CD_changed=1;
5158         }
5159         
5160         /*
5161          * Turn on the CD audio channels.
5162          * For "compatible" soundcards (with "SBPRO 0" or "SBPRO 2"), the addresses
5163          * are obtained from SOUND_BASE (see sbpcd.h).
5164          */
5165         if ((sbpro_type==1) || (SOUND_BASE))
5166         {
5167                 if (sbpro_type!=1)
5168                 {
5169                         MIXER_addr=SOUND_BASE+0x04; /* sound card's address register */
5170                         MIXER_data=SOUND_BASE+0x05; /* sound card's data register */
5171                 }
5172                 OUT(MIXER_addr,MIXER_CD_Volume); /* select SB Pro mixer register */
5173                 OUT(MIXER_data,0xCC); /* one nibble per channel, max. value: 0xFF */
5174         }
5175         
5176         if (register_blkdev(MAJOR_NR, major_name, &sbpcd_fops) != 0)
5177         {
5178                 msg(DBG_INF, "Can't get MAJOR %d for Matsushita CDROM\n", MAJOR_NR);
5179 #ifdef MODULE
5180                 return -EIO;
5181 #else
5182                 goto init_done;
5183 #endif MODULE
5184         }
5185         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
5186         read_ahead[MAJOR_NR] = SBP_BUFFER_FRAMES * (CD_FRAMESIZE / 512);
5187         
5188         request_region(CDo_command,4,major_name);
5189         
5190         for (j=0;j<NR_SBPCD;j++)
5191         {
5192                 if (D_S[j].drv_id==-1) continue;
5193                 switch_drive(j);
5194                 /*
5195                  * allocate memory for the frame buffers
5196                  */
5197                 D_S[j].aud_buf=NULL;
5198                 D_S[j].sbp_audsiz=0;
5199                 D_S[j].sbp_bufsiz=SBP_BUFFER_FRAMES;
5200                 if (D_S[j].drv_type&drv_fam1)
5201                         if (READ_AUDIO>0) D_S[j].sbp_audsiz=READ_AUDIO;
5202                 D_S[j].sbp_buf=(u_char *) vmalloc(D_S[j].sbp_bufsiz*CD_FRAMESIZE);
5203                 if (D_S[j].sbp_buf==NULL)
5204                 {
5205                         msg(DBG_INF,"data buffer (%d frames) not available.\n",D_S[j].sbp_bufsiz);
5206                         return -EIO;
5207                 }
5208 #ifdef MODULE
5209                 msg(DBG_INF,"data buffer size: %d frames.\n",SBP_BUFFER_FRAMES);
5210 #endif MODULE
5211                 if (D_S[j].sbp_audsiz>0)
5212                 {
5213                         D_S[j].aud_buf=(u_char *) vmalloc(D_S[j].sbp_audsiz*CD_FRAMESIZE_RAW);
5214                         if (D_S[j].aud_buf==NULL) msg(DBG_INF,"audio buffer (%d frames) not available.\n",D_S[j].sbp_audsiz);
5215                         else msg(DBG_INF,"audio buffer size: %d frames.\n",D_S[j].sbp_audsiz);
5216                 }
5217                 /*
5218                  * set the block size
5219                  */
5220                 sbpcd_blocksizes[j]=CD_FRAMESIZE;
5221         }
5222         blksize_size[MAJOR_NR]=sbpcd_blocksizes;
5223         
5224  init_done:
5225 #ifndef MODULE
5226 #if !(SBPCD_ISSUE-1)
5227 #ifdef CONFIG_SBPCD2
5228         sbpcd2_init();
5229 #endif CONFIG_SBPCD2
5230 #ifdef CONFIG_SBPCD3
5231         sbpcd3_init();
5232 #endif CONFIG_SBPCD3
5233 #ifdef CONFIG_SBPCD4
5234         sbpcd4_init();
5235 #endif CONFIG_SBPCD4
5236 #endif !(SBPCD_ISSUE-1)
5237 #endif MODULE
5238         return 0;
5239 }
5240 /*==========================================================================*/
5241 #ifdef MODULE
5242 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
5243 {
5244         int j;
5245         
5246         if (MOD_IN_USE)
5247         {
5248                 msg(DBG_INF, "%s module in use - can't remove it.\n", major_name);
5249                 return;
5250         }
5251         if ((unregister_blkdev(MAJOR_NR, major_name) == -EINVAL))
5252         {
5253                 msg(DBG_INF, "What's that: can't unregister %s.\n", major_name);
5254                 return;
5255         }
5256         release_region(CDo_command,4);
5257         
5258         for (j=0;j<NR_SBPCD;j++)
5259         {
5260                 if (D_S[j].drv_id==-1) continue;
5261                 vfree(D_S[j].sbp_buf);
5262                 if (D_S[j].sbp_audsiz>0) vfree(D_S[j].aud_buf);
5263         }
5264         msg(DBG_INF, "%s module released.\n", major_name);
5265 }
5266 #endif MODULE
5267 /*==========================================================================*/
5268 /*
5269  * Check if the media has changed in the CD-ROM drive.
5270  * used externally (isofs/inode.c, fs/buffer.c)
5271  * Currently disabled (has to get "synchronized").
5272  */
5273 static int sbpcd_chk_disk_change(kdev_t full_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
5274 {
5275         int i, st;
5276         
5277         msg(DBG_CHK,"media_check (%d) called\n", MINOR(full_dev));
5278         return (0); /* "busy" test necessary before we really can check */
5279         
5280         i=MINOR(full_dev);
5281         if ( (i<0) || (i>=NR_SBPCD) || (D_S[i].drv_id==-1) )
5282         {
5283                 msg(DBG_INF, "media_check: invalid device %04X.\n", full_dev);
5284                 return (-1);
5285         }
5286         
5287         switch_drive(i);
5288         
5289         cc_ReadStatus();                         /* command: give 1-byte status */
5290         st=ResponseStatus();
5291         msg(DBG_CHK,"media_check: %02X\n",D_S[d].status_bits);
5292         if (st<0)
5293         {
5294                 msg(DBG_INF,"media_check: ResponseStatus error.\n");
5295                 return (1); /* status not obtainable */
5296         }
5297         if (D_S[d].CD_changed==0xFF) msg(DBG_CHK,"media_check: \"changed\" assumed.\n");
5298         if (!st_spinning) msg(DBG_CHK,"media_check: motor off.\n");
5299         if (!st_door_closed)
5300         {
5301                 msg(DBG_CHK,"media_check: door open.\n");
5302                 D_S[d].CD_changed=0xFF;
5303         }
5304         if (!st_caddy_in)
5305         {
5306                 msg(DBG_CHK,"media_check: no disk in drive.\n");
5307                 D_S[d].open_count=0;
5308                 D_S[d].CD_changed=0xFF;
5309         }
5310         if (!st_diskok) msg(DBG_CHK,"media_check: !st_diskok.\n");
5311         
5312 #if 0000
5313         if (D_S[d].CD_changed==0xFF)
5314         {
5315                 D_S[d].CD_changed=1;
5316                 return (1); /* driver had a change detected before */
5317         }
5318 #endif 0000 /* seems to give additional errors at the moment */
5319         
5320         if (!st_diskok) return (1); /* disk not o.k. */
5321         if (!st_caddy_in) return (1); /* disk removed */
5322         if (!st_door_closed) return (1); /* door open */
5323         return (0);
5324 }
5325 /*==========================================================================*/
5326 /*
5327  * Overrides for Emacs so that we follow Linus's tabbing style.
5328  * Emacs will notice this stuff at the end of the file and automatically
5329  * adjust the settings for this buffer only.  This must remain at the end
5330  * of the file. 
5331  * ---------------------------------------------------------------------------
5332  * Local variables:
5333  * c-indent-level: 8
5334  * c-brace-imaginary-offset: 0
5335  * c-brace-offset: -8
5336  * c-argdecl-indent: 8
5337  * c-label-offset: -8
5338  * c-continued-statement-offset: 8
5339  * c-continued-brace-offset: 0
5340  * End:
5341  */

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