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

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