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

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