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

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