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

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