root/drivers/cdrom/sbpcd.c

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

DEFINITIONS

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

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

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