root/drivers/block/sbpcd.c

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

DEFINITIONS

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

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

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