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

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