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

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