root/drivers/cdrom/sbpcd.c

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

DEFINITIONS

This source file includes following definitions.
  1. msg
  2. sbpcd_dbg_ioctl
  3. mark_timeout_delay
  4. mark_timeout_data
  5. mark_timeout_audio
  6. sbp_sleep
  7. lba2msf
  8. bin2bcdx
  9. blk2msf
  10. make16
  11. make32
  12. swap_nibbles
  13. byt2bcd
  14. bcd2bin
  15. msf2blk
  16. msf2lba
  17. sta2err
  18. clr_cmdbuf
  19. flush_status
  20. CDi_stat_loop
  21. tst_DataReady
  22. tst_ResultReady
  23. tst_Attention
  24. ResponseInfo
  25. EvaluateStatus
  26. get_state_T
  27. ResponseStatus
  28. cc_ReadStatus
  29. cc_ReadError
  30. cmd_out_T
  31. cmd_out
  32. cc_Seek
  33. cc_SpinUp
  34. cc_SpinDown
  35. cc_get_mode_T
  36. cc_set_mode_T
  37. cc_prep_mode_T
  38. cc_SetSpeed
  39. cc_SetVolume
  40. GetStatus
  41. cc_DriveReset
  42. SetSpeed
  43. DriveReset
  44. cc_PlayAudio
  45. cc_Pause_Resume
  46. cc_LockDoor
  47. UnLockDoor
  48. LockDoor
  49. cc_CloseTray
  50. cc_ReadSubQ
  51. cc_ModeSense
  52. cc_ModeSelect
  53. cc_GetVolume
  54. cc_ReadCapacity
  55. cc_ReadTocDescr
  56. cc_ReadTocEntry
  57. cc_ReadPacket
  58. convert_UPC
  59. cc_ReadUPC
  60. cc_CheckMultiSession
  61. cc_SubChanInfo
  62. check_datarate
  63. c2_ReadError
  64. ask_mail
  65. check_version
  66. switch_drive
  67. check_card
  68. check_drives
  69. obey_audio_state
  70. check_allowed1
  71. check_allowed2
  72. check_allowed3
  73. seek_pos_audio_end
  74. ReadToC
  75. DiskInfo
  76. prepare
  77. sbp_status
  78. sbpcd_ioctl
  79. sbp_transfer
  80. DO_SBPCD_REQUEST
  81. sbp_read_cmd
  82. sbp_data
  83. sbpcd_open
  84. sbpcd_release
  85. sbpcd_setup
  86. config_spea
  87. init_module
  88. cleanup_module
  89. sbpcd_chk_disk_change

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