PageRenderTime 73ms CodeModel.GetById 12ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/cdrom/sbpcd.c

https://bitbucket.org/evzijst/gittest
C | 5978 lines | 4918 code | 272 blank | 788 comment | 1194 complexity | def27a3f5ff2b3207e0ee3902794f44c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 *  sbpcd.c   CD-ROM device driver for the whole family of traditional,
   3 *            non-ATAPI IDE-style Matsushita/Panasonic CR-5xx drives.
   4 *            Works with SoundBlaster compatible cards and with "no-sound"
   5 *            interface cards like Lasermate, Panasonic CI-101P, Teac, ...
   6 *            Also for the Longshine LCS-7260 drive.
   7 *            Also for the IBM "External ISA CD-Rom" drive.
   8 *            Also for the CreativeLabs CD200 drive.
   9 *            Also for the TEAC CD-55A drive.
  10 *            Also for the ECS-AT "Vertos 100" drive.
  11 *            Not for Sanyo drives (but for the H94A, sjcd is there...).
  12 *            Not for any other Funai drives than the CD200 types (sometimes
  13 *             labelled E2550UA or MK4015 or 2800F).
  14 */
  15
  16#define VERSION "v4.63 Andrew J. Kroll <ag784@freenet.buffalo.edu> Wed Jul 26 04:24:10 EDT 2000"
  17
  18/*   Copyright (C) 1993, 1994, 1995  Eberhard Moenkeberg <emoenke@gwdg.de>
  19 *
  20 *   This program is free software; you can redistribute it and/or modify
  21 *   it under the terms of the GNU General Public License as published by
  22 *   the Free Software Foundation; either version 2, or (at your option)
  23 *   any later version.
  24 *
  25 *   You should have received a copy of the GNU General Public License
  26 *   (for example /usr/src/linux/COPYING); if not, write to the Free
  27 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28 *
  29 *   If you change this software, you should mail a .diff file with some
  30 *   description lines to emoenke@gwdg.de. I want to know about it.
  31 *
  32 *   If you are the editor of a Linux CD, you should enable sbpcd.c within
  33 *   your boot floppy kernel and send me one of your CDs for free.
  34 *
  35 *   If you would like to port the driver to an other operating system (f.e.
  36 *   FreeBSD or NetBSD) or use it as an information source, you shall not be
  37 *   restricted by the GPL under the following conditions:
  38 *     a) the source code of your work is freely available
  39 *     b) my part of the work gets mentioned at all places where your 
  40 *        authorship gets mentioned
  41 *     c) I receive a copy of your code together with a full installation
  42 *        package of your operating system for free.
  43 *
  44 *
  45 *  VERSION HISTORY
  46 *
  47 *  0.1  initial release, April/May 93, after mcd.c (Martin Harriss)
  48 *
  49 *  0.2  thek "repeat:"-loop in do_sbpcd_request did not check for
  50 *       end-of-request_queue (resulting in kernel panic).
  51 *       Flow control seems stable, but throughput is not better.  
  52 *
  53 *  0.3  interrupt locking totally eliminated (maybe "inb" and "outb"
  54 *       are still locking) - 0.2 made keyboard-type-ahead losses.
  55 *       check_sbpcd_media_change added (to use by isofs/inode.c)
  56 *       - but it detects almost nothing.
  57 *
  58 *  0.4  use MAJOR 25 definitely.
  59 *       Almost total re-design to support double-speed drives and
  60 *       "naked" (no sound) interface cards ("LaserMate" interface type).
  61 *       Flow control should be exact now.
  62 *       Don't occupy the SbPro IRQ line (not needed either); will
  63 *       live together with Hannu Savolainen's sndkit now.
  64 *       Speeded up data transfer to 150 kB/sec, with help from Kai
  65 *       Makisara, the "provider" of the "mt" tape utility.
  66 *       Give "SpinUp" command if necessary.
  67 *       First steps to support up to 4 drives (but currently only one).
  68 *       Implemented audio capabilities - workman should work, xcdplayer
  69 *       gives some problems.
  70 *       This version is still consuming too much CPU time, and
  71 *       sleeping still has to be worked on.
  72 *       During "long" implied seeks, it seems possible that a 
  73 *       ReadStatus command gets ignored. That gives the message
  74 *       "ResponseStatus timed out" (happens about 6 times here during
  75 *       a "ls -alR" of the YGGDRASIL LGX-Beta CD). Such a case is
  76 *       handled without data error, but it should get done better.
  77 *
  78 *  0.5  Free CPU during waits (again with help from Kai Makisara).
  79 *       Made it work together with the LILO/kernel setup standard.
  80 *       Included auto-probing code, as suggested by YGGDRASIL.
  81 *       Formal redesign to add DDI debugging.
  82 *       There are still flaws in IOCTL (workman with double speed drive).
  83 *
  84 *  1.0  Added support for all drive IDs (0...3, no longer only 0)
  85 *       and up to 4 drives on one controller.
  86 *       Added "#define MANY_SESSION" for "old" multi session CDs.
  87 *
  88 *  1.1  Do SpinUp for new drives, too.
  89 *       Revised for clean compile under "old" kernels (0.99pl9).
  90 *
  91 *  1.2  Found the "workman with double-speed drive" bug: use the driver's
  92 *       audio_state, not what the drive is reporting with ReadSubQ.
  93 *
  94 *  1.3  Minor cleanups.
  95 *       Refinements regarding Workman.
  96 *
  97 *  1.4  Read XA disks (PhotoCDs) with "old" drives, too (but only the first
  98 *       session - no chance to fully access a "multi-session" CD).
  99 *       This currently still is too slow (50 kB/sec) - but possibly
 100 *       the old drives won't do it faster.
 101 *       Implemented "door (un)lock" for new drives (still does not work
 102 *       as wanted - no lock possible after an unlock).
 103 *       Added some debugging printout for the UPC/EAN code - but my drives 
 104 *       return only zeroes. Is there no UPC/EAN code written?
 105 *
 106 *  1.5  Laborate with UPC/EAN code (not better yet).
 107 *       Adapt to kernel 1.1.8 change (have to explicitly include
 108 *       <linux/string.h> now).
 109 *
 110 *  1.6  Trying to read audio frames as data. Impossible with the current
 111 *       drive firmware levels, as it seems. Awaiting any hint. ;-)
 112 *       Changed "door unlock": repeat it until success.
 113 *       Changed CDROMSTOP routine (stop somewhat "softer" so that Workman
 114 *       won't get confused).
 115 *       Added a third interface type: Sequoia S-1000, as used with the SPEA
 116 *       Media FX sound card. This interface (usable for Sony and Mitsumi 
 117 *       drives, too) needs a special configuration setup and behaves like a 
 118 *       LaserMate type after that. Still experimental - I do not have such
 119 *       an interface.
 120 *       Use the "variable BLOCK_SIZE" feature (2048). But it does only work
 121 *       if you give the mount option "block=2048".
 122 *       The media_check routine is currently disabled; now that it gets
 123 *       called as it should I fear it must get synchronized for not to
 124 *       disturb the normal driver's activity.
 125 *
 126 *  2.0  Version number bumped - two reasons:
 127 *       - reading audio tracks as data works now with CR-562 and CR-563. We
 128 *       currently do it by an IOCTL (yet has to get standardized), one frame
 129 *       at a time; that is pretty slow. But it works.
 130 *       - we are maintaining now up to 4 interfaces (each up to 4 drives):
 131 *       did it the easy way - a different MAJOR (25, 26, ...) and a different
 132 *       copy of the driver (sbpcd.c, sbpcd2.c, sbpcd3.c, sbpcd4.c - only
 133 *       distinguished by the value of SBPCD_ISSUE and the driver's name),
 134 *       and a common sbpcd.h file.
 135 *       Bettered the "ReadCapacity error" problem with old CR-52x drives (the
 136 *       drives sometimes need a manual "eject/insert" before work): just
 137 *       reset the drive and do again. Needs lots of resets here and sometimes
 138 *       that does not cure, so this can't be the solution.
 139 *
 140 *  2.1  Found bug with multisession CDs (accessing frame 16).
 141 *       "read audio" works now with address type CDROM_MSF, too.
 142 *       Bigger audio frame buffer: allows reading max. 4 frames at time; this
 143 *       gives a significant speedup, but reading more than one frame at once
 144 *       gives missing chunks at each single frame boundary.
 145 *
 146 *  2.2  Kernel interface cleanups: timers, init, setup, media check.
 147 *
 148 *  2.3  Let "door lock" and "eject" live together.
 149 *       Implemented "close tray" (done automatically during open).
 150 *
 151 *  2.4  Use different names for device registering.
 152 *
 153 *  2.5  Added "#if EJECT" code (default: enabled) to automatically eject
 154 *       the tray during last call to "sbpcd_release".
 155 *       Added "#if JUKEBOX" code (default: disabled) to automatically eject
 156 *       the tray during call to "sbpcd_open" if no disk is in.
 157 *       Turn on the CD volume of "compatible" sound cards, too; just define
 158 *       SOUND_BASE (in sbpcd.h) accordingly (default: disabled).
 159 *
 160 *  2.6  Nothing new.  
 161 *
 162 *  2.7  Added CDROMEJECT_SW ioctl to set the "EJECT" behavior on the fly:
 163 *       0 disables, 1 enables auto-ejecting. Useful to keep the tray in
 164 *       during shutdown.
 165 *
 166 *  2.8  Added first support (still BETA, I need feedback or a drive) for
 167 *       the Longshine LCS-7260 drives. They appear as double-speed drives
 168 *       using the "old" command scheme, extended by tray control and door
 169 *       lock functions.
 170 *       Found (and fixed preliminary) a flaw with some multisession CDs: we
 171 *       have to re-direct not only the accesses to frame 16 (the isofs
 172 *       routines drive it up to max. 100), but also those to the continuation
 173 *       (repetition) frames (as far as they exist - currently set fix as
 174 *       16..20).
 175 *       Changed default of the "JUKEBOX" define. If you use this default,
 176 *       your tray will eject if you try to mount without a disk in. Next
 177 *       mount command will insert the tray - so, just fill in a disk. ;-)
 178 *
 179 *  2.9  Fulfilled the Longshine LCS-7260 support; with great help and
 180 *       experiments by Serge Robyns.
 181 *       First attempts to support the TEAC CD-55A drives; but still not
 182 *       usable yet.
 183 *       Implemented the CDROMMULTISESSION ioctl; this is an attempt to handle
 184 *       multi session CDs more "transparent" (redirection handling has to be
 185 *       done within the isofs routines, and only for the special purpose of
 186 *       obtaining the "right" volume descriptor; accesses to the raw device
 187 *       should not get redirected).
 188 *
 189 *  3.0  Just a "normal" increment, with some provisions to do it better. ;-)
 190 *       Introduced "#define READ_AUDIO" to specify the maximum number of 
 191 *       audio frames to grab with one request. This defines a buffer size
 192 *       within kernel space; a value of 0 will reserve no such space and
 193 *       disable the CDROMREADAUDIO ioctl. A value of 75 enables the reading
 194 *       of a whole second with one command, but will use a buffer of more
 195 *       than 172 kB.
 196 *       Started CD200 support. Drive detection should work, but nothing
 197 *       more.
 198 *
 199 *  3.1  Working to support the CD200 and the Teac CD-55A drives.
 200 *       AT-BUS style device numbering no longer used: use SCSI style now.
 201 *       So, the first "found" device has MINOR 0, regardless of the
 202 *       jumpered drive ID. This implies modifications to the /dev/sbpcd*
 203 *       entries for some people, but will help the DAU (german TLA, english:
 204 *       "newbie", maybe ;-) to install his "first" system from a CD.
 205 *
 206 *  3.2  Still testing with CD200 and CD-55A drives.
 207 *
 208 *  3.3  Working with CD200 support.
 209 *
 210 *  3.4  Auto-probing stops if an address of 0 is seen (to be entered with
 211 *       the kernel command line).
 212 *       Made the driver "loadable". If used as a module, "audio copy" is
 213 *       disabled, and the internal read ahead data buffer has a reduced size
 214 *       of 4 kB; so, throughput may be reduced a little bit with slow CPUs.
 215 *
 216 *  3.5  Provisions to handle weird photoCDs which have an interrupted
 217 *       "formatting" immediately after the last frames of some files: simply
 218 *       never "read ahead" with MultiSession CDs. By this, CPU usage may be
 219 *       increased with those CDs, and there may be a loss in speed.
 220 *       Re-structured the messaging system.
 221 *       The "loadable" version no longer has a limited READ_AUDIO buffer
 222 *       size.
 223 *       Removed "MANY_SESSION" handling for "old" multi session CDs.
 224 *       Added "private" IOCTLs CDROMRESET and CDROMVOLREAD.
 225 *       Started again to support the TEAC CD-55A drives, now that I found
 226 *       the money for "my own" drive. ;-)
 227 *       The TEAC CD-55A support is fairly working now.
 228 *       I have measured that the drive "delivers" at 600 kB/sec (even with
 229 *       bigger requests than the drive's 64 kB buffer can satisfy), but
 230 *       the "real" rate does not exceed 520 kB/sec at the moment. 
 231 *       Caused by the various changes to build in TEAC support, the timed
 232 *       loops are de-optimized at the moment (less throughput with CR-52x
 233 *       drives, and the TEAC will give speed only with SBP_BUFFER_FRAMES 64).
 234 *
 235 *  3.6  Fixed TEAC data read problems with SbPro interfaces.
 236 *       Initial size of the READ_AUDIO buffer is 0. Can get set to any size
 237 *       during runtime.
 238 *
 239 *  3.7  Introduced MAX_DRIVES for some poor interface cards (seen with TEAC
 240 *       drives) which allow only one drive (ID 0); this avoids repetitive
 241 *       detection under IDs 1..3. 
 242 *       Elongated cmd_out_T response waiting; necessary for photo CDs with
 243 *       a lot of sessions.
 244 *       Bettered the sbpcd_open() behavior with TEAC drives.
 245 *
 246 *  3.8  Elongated max_latency for CR-56x drives.
 247 *
 248 *  3.9  Finally fixed the long-known SoundScape/SPEA/Sequoia S-1000 interface
 249 *       configuration bug.
 250 *       Now Corey, Heiko, Ken, Leo, Vadim/Eric & Werner are invited to copy
 251 *       the config_spea() routine into their drivers. ;-)
 252 *
 253 *  4.0  No "big step" - normal version increment.
 254 *       Adapted the benefits from 1.3.33.
 255 *       Fiddled with CDROMREADAUDIO flaws.
 256 *       Avoid ReadCapacity command with CD200 drives (the MKE 1.01 version
 257 *       seems not to support it).
 258 *       Fulfilled "read audio" for CD200 drives, with help of Pete Heist
 259 *       (heistp@rpi.edu).
 260 *
 261 *  4.1  Use loglevel KERN_INFO with printk().
 262 *       Added support for "Vertos 100" drive ("ECS-AT") - it is very similar
 263 *       to the Longshine LCS-7260. Give feedback if you can - I never saw
 264 *       such a drive, and I have no specs.
 265 *
 266 *  4.2  Support for Teac 16-bit interface cards. Can't get auto-detected,
 267 *       so you have to jumper your card to 0x2C0. Still not 100% - come
 268 *       in contact if you can give qualified feedback.
 269 *       Use loglevel KERN_NOTICE with printk(). If you get annoyed by a
 270 *       flood of unwanted messages and the accompanied delay, try to read
 271 *       my documentation. Especially the Linux CDROM drivers have to do an
 272 *       important job for the newcomers, so the "distributed" version has
 273 *       to fit some special needs. Since generations, the flood of messages
 274 *       is user-configurable (even at runtime), but to get aware of this, one
 275 *       needs a special mental quality: the ability to read.
 276 *       
 277 *  4.3  CD200F does not like to receive a command while the drive is
 278 *       reading the ToC; still trying to solve it.
 279 *       Removed some redundant verify_area calls (yes, Heiko Eissfeldt
 280 *       is visiting all the Linux CDROM drivers ;-).
 281 *       
 282 *  4.4  Adapted one idea from tiensivu@pilot.msu.edu's "stripping-down"
 283 *       experiments: "KLOGD_PAUSE".
 284 *       Inhibited "play audio" attempts with data CDs. Provisions for a
 285 *       "data-safe" handling of "mixed" (data plus audio) Cds.
 286 *
 287 *  4.5  Meanwhile Gonzalo Tornaria <tornaria@cmat.edu.uy> (GTL) built a
 288 *       special end_request routine: we seem to have to take care for not
 289 *       to have two processes working at the request list. My understanding
 290 *       was and is that ll_rw_blk should not call do_sbpcd_request as long
 291 *       as there is still one call active (the first call will care for all
 292 *       outstanding I/Os, and if a second call happens, that is a bug in
 293 *       ll_rw_blk.c).
 294 *       "Check media change" without touching any drive.
 295 *
 296 *  4.6  Use a semaphore to synchronize multi-activity; elaborated by Rob
 297 *       Riggs <rriggs@tesser.com>. At the moment, we simply block "read"
 298 *       against "ioctl" and vice versa. This could be refined further, but
 299 *       I guess with almost no performance increase.
 300 *       Experiments to speed up the CD-55A; again with help of Rob Riggs
 301 *       (to be true, he gave both, idea & code. ;-)
 302 *
 303 *  4.61 Ported to Uniform CD-ROM driver by 
 304 *       Heiko Eissfeldt <heiko@colossus.escape.de> with additional
 305 *       changes by Erik Andersen <andersee@debian.org>
 306 *
 307 *  4.62 Fix a bug where playing audio left the drive in an unusable state.
 308 *         Heiko Eissfeldt <heiko@colossus.escape.de>
 309 *
 310 *  November 1999 -- Make kernel-parameter implementation work with 2.3.x 
 311 *	             Removed init_module & cleanup_module in favor of 
 312 *	             module_init & module_exit.
 313 *	             Torben Mathiasen <tmm@image.dk>
 314 *
 315 *  4.63 Bug fixes for audio annoyances, new legacy CDROM maintainer.
 316 *		Annoying things fixed:
 317 *		TOC reread on automated disk changes
 318 *		TOC reread on manual cd changes
 319 *		Play IOCTL tries to play CD before it's actually ready... sometimes.
 320 *		CD_AUDIO_COMPLETED state so workman (and other playes) can repeat play.
 321 *		Andrew J. Kroll <ag784@freenet.buffalo.edu> Wed Jul 26 04:24:10 EDT 2000
 322 *
 323 *  4.64 Fix module parameters - were being completely ignored.
 324 *	 Can also specify max_drives=N as a setup int to get rid of
 325 *	 "ghost" drives on crap hardware (aren't they all?)   Paul Gortmaker
 326 *
 327 *  TODO
 328 *     implement "read all subchannel data" (96 bytes per frame)
 329 *     remove alot of the virtual status bits and deal with hardware status
 330 *     move the change of cd for audio to a better place
 331 *     add debug levels to insmod parameters (trivial)
 332 *
 333 *     special thanks to Kai Makisara (kai.makisara@vtt.fi) for his fine
 334 *     elaborated speed-up experiments (and the fabulous results!), for
 335 *     the "push" towards load-free wait loops, and for the extensive mail
 336 *     thread which brought additional hints and bug fixes.
 337 *
 338 */
 339
 340/*
 341 * Trying to merge requests breaks this driver horribly (as in it goes
 342 * boom and apparently has done so since 2.3.41).  As it is a legacy
 343 * driver for a horribly slow double speed CD on a hideous interface
 344 * designed for polled operation, I won't lose any sleep in simply
 345 * disallowing merging.				Paul G.  02/2001
 346 *
 347 * Thu May 30 14:14:47 CEST 2002:
 348 *
 349 * I have presumably found the reson for the above - there was a bogous
 350 * end_request substitute, which was manipulating the request queues
 351 * incorrectly. If someone has access to the actual hardware, and it's
 352 * still operations - well  please free to test it.
 353 *
 354 * Marcin Dalecki
 355 */
 356
 357/*
 358 * Add bio/kdev_t changes for 2.5.x required to make it work again. 
 359 * Still room for improvement in the request handling here if anyone
 360 * actually cares.  Bring your own chainsaw.    Paul G.  02/2002
 361 */
 362
 363
 364#include <linux/module.h>
 365
 366#include <linux/errno.h>
 367#include <linux/sched.h>
 368#include <linux/mm.h>
 369#include <linux/timer.h>
 370#include <linux/fs.h>
 371#include <linux/kernel.h>
 372#include <linux/cdrom.h>
 373#include <linux/ioport.h>
 374#include <linux/devfs_fs_kernel.h>
 375#include <linux/major.h>
 376#include <linux/string.h>
 377#include <linux/vmalloc.h>
 378#include <linux/init.h>
 379#include <linux/interrupt.h>
 380
 381#include <asm/system.h>
 382#include <asm/io.h>
 383#include <asm/uaccess.h>
 384#include <stdarg.h>
 385#include <linux/config.h>
 386#include "sbpcd.h"
 387
 388#define MAJOR_NR MATSUSHITA_CDROM_MAJOR
 389#include <linux/blkdev.h>
 390
 391/*==========================================================================*/
 392#if SBPCD_DIS_IRQ
 393# define SBPCD_CLI cli()
 394# define SBPCD_STI sti()
 395#else
 396# define SBPCD_CLI
 397# define SBPCD_STI
 398#endif
 399
 400/*==========================================================================*/
 401/*
 402 * auto-probing address list
 403 * inspired by Adam J. Richter from Yggdrasil
 404 *
 405 * still not good enough - can cause a hang.
 406 *   example: a NE 2000 ethernet card at 300 will cause a hang probing 310.
 407 * if that happens, reboot and use the LILO (kernel) command line.
 408 * The possibly conflicting ethernet card addresses get NOT probed 
 409 * by default - to minimize the hang possibilities. 
 410 *
 411 * The SB Pro addresses get "mirrored" at 0x6xx and some more locations - to
 412 * avoid a type error, the 0x2xx-addresses must get checked before 0x6xx.
 413 *
 414 * send mail to emoenke@gwdg.de if your interface card is not FULLY
 415 * represented here.
 416 */
 417static int sbpcd[] =
 418{
 419	CDROM_PORT, SBPRO, /* probe with user's setup first */
 420#if DISTRIBUTION
 421	0x230, 1, /* Soundblaster Pro and 16 (default) */
 422#if 0
 423	0x300, 0, /* CI-101P (default), WDH-7001C (default),
 424		     Galaxy (default), Reveal (one default) */
 425	0x250, 1, /* OmniCD default, Soundblaster Pro and 16 */
 426	0x2C0, 3, /* Teac 16-bit cards */
 427	0x260, 1, /* OmniCD */
 428	0x320, 0, /* Lasermate, CI-101P, WDH-7001C, Galaxy, Reveal (other default),
 429		     Longshine LCS-6853 (default) */
 430	0x338, 0, /* Reveal Sound Wave 32 card model #SC600 */
 431	0x340, 0, /* Mozart sound card (default), Lasermate, CI-101P */
 432	0x360, 0, /* Lasermate, CI-101P */
 433	0x270, 1, /* Soundblaster 16 */
 434	0x670, 0, /* "sound card #9" */
 435	0x690, 0, /* "sound card #9" */
 436	0x338, 2, /* SPEA Media FX, Ensonic SoundScape (default) */
 437	0x328, 2, /* SPEA Media FX */
 438	0x348, 2, /* SPEA Media FX */
 439	0x634, 0, /* some newer sound cards */
 440	0x638, 0, /* some newer sound cards */
 441	0x230, 1, /* some newer sound cards */
 442	/* due to incomplete address decoding of the SbPro card, these must be last */
 443	0x630, 0, /* "sound card #9" (default) */
 444	0x650, 0, /* "sound card #9" */
 445#ifdef MODULE
 446	/*
 447	 * some "hazardous" locations (no harm with the loadable version)
 448	 * (will stop the bus if a NE2000 ethernet card resides at offset -0x10)
 449	 */
 450	0x330, 0, /* Lasermate, CI-101P, WDH-7001C */
 451	0x350, 0, /* Lasermate, CI-101P */
 452	0x358, 2, /* SPEA Media FX */
 453	0x370, 0, /* Lasermate, CI-101P */
 454	0x290, 1, /* Soundblaster 16 */
 455	0x310, 0, /* Lasermate, CI-101P, WDH-7001C */
 456#endif /* MODULE */
 457#endif
 458#endif /* DISTRIBUTION */
 459};
 460
 461/*
 462 * Protects access to global structures etc.
 463 */
 464static  __cacheline_aligned DEFINE_SPINLOCK(sbpcd_lock);
 465static struct request_queue *sbpcd_queue;
 466
 467MODULE_PARM(sbpcd, "2i");
 468MODULE_PARM(max_drives, "i");
 469
 470#define NUM_PROBE  (sizeof(sbpcd) / sizeof(int))
 471
 472/*==========================================================================*/
 473
 474#define INLINE inline
 475
 476/*==========================================================================*/
 477/*
 478 * the forward references:
 479 */
 480static void sbp_sleep(u_int);
 481static void mark_timeout_delay(u_long);
 482static void mark_timeout_data(u_long);
 483#if 0
 484static void mark_timeout_audio(u_long);
 485#endif
 486static void sbp_read_cmd(struct request *req);
 487static int sbp_data(struct request *req);
 488static int cmd_out(void);
 489static int DiskInfo(void);
 490
 491/*==========================================================================*/
 492
 493/*
 494 * pattern for printk selection:
 495 *
 496 * (1<<DBG_INF)  necessary information
 497 * (1<<DBG_BSZ)  BLOCK_SIZE trace
 498 * (1<<DBG_REA)  "read" status trace
 499 * (1<<DBG_CHK)  "media check" trace
 500 * (1<<DBG_TIM)  datarate timer test
 501 * (1<<DBG_INI)  initialization trace
 502 * (1<<DBG_TOC)  tell TocEntry values
 503 * (1<<DBG_IOC)  ioctl trace
 504 * (1<<DBG_STA)  "ResponseStatus" trace
 505 * (1<<DBG_ERR)  "cc_ReadError" trace
 506 * (1<<DBG_CMD)  "cmd_out" trace
 507 * (1<<DBG_WRN)  give explanation before auto-probing
 508 * (1<<DBG_MUL)  multi session code test
 509 * (1<<DBG_IDX)  "drive_id != 0" test code
 510 * (1<<DBG_IOX)  some special information
 511 * (1<<DBG_DID)  drive ID test
 512 * (1<<DBG_RES)  drive reset info
 513 * (1<<DBG_SPI)  SpinUp test info
 514 * (1<<DBG_IOS)  ioctl trace: "subchannel"
 515 * (1<<DBG_IO2)  ioctl trace: general
 516 * (1<<DBG_UPC)  show UPC info
 517 * (1<<DBG_XA1)  XA mode debugging
 518 * (1<<DBG_LCK)  door (un)lock info
 519 * (1<<DBG_SQ1)   dump SubQ frame
 520 * (1<<DBG_AUD)  "read audio" debugging
 521 * (1<<DBG_SEQ)  Sequoia interface configuration trace
 522 * (1<<DBG_LCS)  Longshine LCS-7260 debugging trace
 523 * (1<<DBG_CD2)  MKE/Funai CD200 debugging trace
 524 * (1<<DBG_TEA)  TEAC CD-55A debugging trace
 525 * (1<<DBG_ECS)  ECS-AT (Vertos-100) debugging trace
 526 * (1<<DBG_000)  unnecessary information
 527 */
 528#if DISTRIBUTION
 529static int sbpcd_debug = (1<<DBG_INF);
 530#else
 531static int sbpcd_debug = 0 & ((1<<DBG_INF) |
 532			  (1<<DBG_TOC) |
 533			  (1<<DBG_MUL) |
 534			  (1<<DBG_UPC));
 535#endif /* DISTRIBUTION */
 536
 537static int sbpcd_ioaddr = CDROM_PORT;	/* default I/O base address */
 538static int sbpro_type = SBPRO;
 539static unsigned char f_16bit;
 540static unsigned char do_16bit;
 541static int CDo_command, CDo_reset;
 542static int CDo_sel_i_d, CDo_enable;
 543static int CDi_info, CDi_status, CDi_data;
 544static struct cdrom_msf msf;
 545static struct cdrom_ti ti;
 546static struct cdrom_tochdr tochdr;
 547static struct cdrom_tocentry tocentry;
 548static struct cdrom_subchnl SC;
 549static struct cdrom_volctrl volctrl;
 550static struct cdrom_read_audio read_audio;
 551
 552static unsigned char msgnum;
 553static char msgbuf[80];
 554
 555static int max_drives = MAX_DRIVES;
 556#ifndef MODULE
 557static unsigned char setup_done;
 558static const char *str_sb_l = "soundblaster";
 559static const char *str_sp_l = "spea";
 560static const char *str_ss_l = "soundscape";
 561static const char *str_t16_l = "teac16bit";
 562static const char *str_ss = "SoundScape";
 563#endif
 564static const char *str_sb = "SoundBlaster";
 565static const char *str_lm = "LaserMate";
 566static const char *str_sp = "SPEA";
 567static const char *str_t16 = "Teac16bit";
 568static const char *type;
 569static const char *major_name="sbpcd";
 570
 571/*==========================================================================*/
 572
 573#ifdef FUTURE
 574static DECLARE_WAIT_QUEUE_HEAD(sbp_waitq);
 575#endif /* FUTURE */
 576
 577static int teac=SBP_TEAC_SPEED;
 578static int buffers=SBP_BUFFER_FRAMES;
 579
 580static u_char family0[]="MATSHITA"; /* MKE CR-521, CR-522, CR-523 */
 581static u_char family1[]="CR-56";    /* MKE CR-562, CR-563 */
 582static u_char family2[]="CD200";    /* MKE CD200, Funai CD200F */
 583static u_char familyL[]="LCS-7260"; /* Longshine LCS-7260 */
 584static u_char familyT[]="CD-55";    /* TEAC CD-55A */
 585static u_char familyV[]="ECS-AT";   /* ECS Vertos 100 */
 586
 587static u_int recursion; /* internal testing only */
 588static u_int fatal_err; /* internal testing only */
 589static u_int response_count;
 590static u_int flags_cmd_out;
 591static u_char cmd_type;
 592static u_char drvcmd[10];
 593static u_char infobuf[20];
 594static u_char xa_head_buf[CD_XA_HEAD];
 595static u_char xa_tail_buf[CD_XA_TAIL];
 596
 597#if OLD_BUSY
 598static volatile u_char busy_data;
 599static volatile u_char busy_audio; /* true semaphores would be safer */
 600#endif /* OLD_BUSY */ 
 601static DECLARE_MUTEX(ioctl_read_sem);
 602static u_long timeout;
 603static volatile u_char timed_out_delay;
 604static volatile u_char timed_out_data;
 605#if 0
 606static volatile u_char timed_out_audio;
 607#endif
 608static u_int datarate= 1000000;
 609static u_int maxtim16=16000000;
 610static u_int maxtim04= 4000000;
 611static u_int maxtim02= 2000000;
 612static u_int maxtim_8=   30000;
 613#if LONG_TIMING
 614static u_int maxtim_data= 9000;
 615#else
 616static u_int maxtim_data= 3000;
 617#endif /* LONG_TIMING */ 
 618#if DISTRIBUTION
 619static int n_retries=6;
 620#else
 621static int n_retries=6;
 622#endif
 623/*==========================================================================*/
 624
 625static int ndrives;
 626static u_char drv_pattern[NR_SBPCD]={speed_auto,speed_auto,speed_auto,speed_auto};
 627
 628/*==========================================================================*/
 629/*
 630 * drive space begins here (needed separate for each unit) 
 631 */
 632static struct sbpcd_drive {
 633	char drv_id;           /* "jumpered" drive ID or -1 */
 634	char drv_sel;          /* drive select lines bits */
 635	
 636	char drive_model[9];
 637	u_char firmware_version[4];
 638	char f_eject;          /* auto-eject flag: 0 or 1 */
 639	u_char *sbp_buf;       /* Pointer to internal data buffer,
 640				  space allocated during sbpcd_init() */
 641	u_int sbp_bufsiz;      /* size of sbp_buf (# of frames) */
 642	int sbp_first_frame;   /* First frame in buffer */
 643	int sbp_last_frame;    /* Last frame in buffer  */
 644	int sbp_read_frames;   /* Number of frames being read to buffer */
 645	int sbp_current;       /* Frame being currently read */
 646	
 647	u_char mode;           /* read_mode: READ_M1, READ_M2, READ_SC, READ_AU */
 648	u_char *aud_buf;       /* Pointer to audio data buffer,
 649				  space allocated during sbpcd_init() */
 650	u_int sbp_audsiz;      /* size of aud_buf (# of raw frames) */
 651	u_int drv_type;
 652	u_char drv_options;
 653	int status_bits;
 654	u_char diskstate_flags;
 655	u_char sense_byte;
 656	
 657	u_char CD_changed;
 658	char open_count;
 659	u_char error_byte;
 660	
 661	u_char f_multisession;
 662	u_int lba_multi;
 663	int first_session;
 664	int last_session;
 665	int track_of_last_session;
 666	
 667	u_char audio_state;
 668	u_int pos_audio_start;
 669	u_int pos_audio_end;
 670	char vol_chan0;
 671	u_char vol_ctrl0;
 672	char vol_chan1;
 673	u_char vol_ctrl1;
 674#if 000 /* no supported drive has it */
 675	char vol_chan2;
 676	u_char vol_ctrl2;
 677	char vol_chan3;
 678	u_char vol_ctrl3;
 679#endif /*000 */
 680	u_char volume_control; /* TEAC on/off bits */
 681	
 682	u_char SubQ_ctl_adr;
 683	u_char SubQ_trk;
 684	u_char SubQ_pnt_idx;
 685	u_int SubQ_run_tot;
 686	u_int SubQ_run_trk;
 687	u_char SubQ_whatisthis;
 688	
 689	u_char UPC_ctl_adr;
 690	u_char UPC_buf[7];
 691	
 692	int frame_size;
 693	int CDsize_frm;
 694	
 695	u_char xa_byte; /* 0x20: XA capabilities */
 696	u_char n_first_track; /* binary */
 697	u_char n_last_track; /* binary (not bcd), 0x01...0x63 */
 698	u_int size_msf; /* time of whole CD, position of LeadOut track */
 699	u_int size_blk;
 700	
 701	u_char TocEnt_nixbyte; /* em */
 702	u_char TocEnt_ctl_adr;
 703	u_char TocEnt_number;
 704	u_char TocEnt_format; /* em */
 705	u_int TocEnt_address;
 706#ifdef SAFE_MIXED
 707	char has_data;
 708#endif /* SAFE_MIXED */ 
 709	u_char ored_ctl_adr; /* to detect if CDROM contains data tracks */
 710	
 711	struct {
 712		u_char nixbyte; /* em */
 713		u_char ctl_adr; /* 0x4x: data, 0x0x: audio */
 714		u_char number;
 715		u_char format; /* em */ /* 0x00: lba, 0x01: msf */
 716		u_int address;
 717	} TocBuffer[MAX_TRACKS+1]; /* last entry faked */ 
 718	
 719	int in_SpinUp; /* CR-52x test flag */
 720	int n_bytes; /* TEAC awaited response count */
 721	u_char error_state, b3, b4; /* TEAC command error state */
 722	u_char f_drv_error; /* TEAC command error flag */
 723	u_char speed_byte;
 724	int frmsiz;
 725	u_char f_XA; /* 1: XA */
 726	u_char type_byte; /* 0, 1, 3 */
 727	u_char mode_xb_6;
 728	u_char mode_yb_7;
 729	u_char mode_xb_8;
 730	u_char delay;
 731	struct cdrom_device_info *sbpcd_infop;
 732	struct gendisk *disk;
 733} D_S[NR_SBPCD];
 734
 735static struct sbpcd_drive *current_drive = D_S;
 736
 737/*
 738 * drive space ends here (needed separate for each unit)
 739 */
 740/*==========================================================================*/
 741#if 0
 742unsigned long cli_sti; /* for saving the processor flags */
 743#endif
 744/*==========================================================================*/
 745static struct timer_list delay_timer =
 746		TIMER_INITIALIZER(mark_timeout_delay, 0, 0);
 747static struct timer_list data_timer =
 748		TIMER_INITIALIZER(mark_timeout_data, 0, 0);
 749#if 0
 750static struct timer_list audio_timer =
 751		TIMER_INITIALIZER(mark_timeout_audio, 0, 0);
 752#endif
 753/*==========================================================================*/
 754/*
 755 * DDI interface
 756 */
 757static void msg(int level, const char *fmt, ...)
 758{
 759#if DISTRIBUTION
 760#define MSG_LEVEL KERN_NOTICE
 761#else
 762#define MSG_LEVEL KERN_INFO
 763#endif /* DISTRIBUTION */
 764
 765	char buf[256];
 766	va_list args;
 767	
 768	if (!(sbpcd_debug&(1<<level))) return;
 769	
 770	msgnum++;
 771	if (msgnum>99) msgnum=0;
 772	sprintf(buf, MSG_LEVEL "%s-%d [%02d]:  ", major_name, current_drive - D_S, msgnum);
 773	va_start(args, fmt);
 774	vsprintf(&buf[18], fmt, args);
 775	va_end(args);
 776	printk(buf);
 777#if KLOGD_PAUSE
 778	sbp_sleep(KLOGD_PAUSE); /* else messages get lost */
 779#endif /* KLOGD_PAUSE */ 
 780	return;
 781}
 782/*==========================================================================*/
 783/*
 784 * DDI interface: runtime trace bit pattern maintenance
 785 */
 786static int sbpcd_dbg_ioctl(unsigned long arg, int level)
 787{
 788	switch(arg)
 789	{
 790	case 0:	/* OFF */
 791		sbpcd_debug = DBG_INF;
 792		break;
 793		
 794	default:
 795		if (arg>=128) sbpcd_debug &= ~(1<<(arg-128));
 796		else sbpcd_debug |= (1<<arg);
 797	}
 798	return (arg);
 799}
 800/*==========================================================================*/
 801static void mark_timeout_delay(u_long i)
 802{
 803	timed_out_delay=1;
 804#if 0
 805	msg(DBG_TIM,"delay timer expired.\n");
 806#endif
 807}
 808/*==========================================================================*/
 809static void mark_timeout_data(u_long i)
 810{
 811	timed_out_data=1;
 812#if 0
 813	msg(DBG_TIM,"data timer expired.\n");
 814#endif
 815}
 816/*==========================================================================*/
 817#if 0
 818static void mark_timeout_audio(u_long i)
 819{
 820	timed_out_audio=1;
 821#if 0
 822	msg(DBG_TIM,"audio timer expired.\n");
 823#endif
 824}
 825#endif
 826/*==========================================================================*/
 827/*
 828 * Wait a little while (used for polling the drive).
 829 */
 830static void sbp_sleep(u_int time)
 831{
 832	sti();
 833	current->state = TASK_INTERRUPTIBLE;
 834	schedule_timeout(time);
 835	sti();
 836}
 837/*==========================================================================*/
 838#define RETURN_UP(rc) {up(&ioctl_read_sem); return(rc);}
 839/*==========================================================================*/
 840/*
 841 *  convert logical_block_address to m-s-f_number (3 bytes only)
 842 */
 843static INLINE void lba2msf(int lba, u_char *msf)
 844{
 845	lba += CD_MSF_OFFSET;
 846	msf[0] = lba / (CD_SECS*CD_FRAMES);
 847	lba %= CD_SECS*CD_FRAMES;
 848	msf[1] = lba / CD_FRAMES;
 849	msf[2] = lba % CD_FRAMES;
 850}
 851/*==========================================================================*/
 852/*==========================================================================*/
 853/*
 854 *  convert msf-bin to msf-bcd
 855 */
 856static INLINE void bin2bcdx(u_char *p)  /* must work only up to 75 or 99 */
 857{
 858	*p=((*p/10)<<4)|(*p%10);
 859}
 860/*==========================================================================*/
 861static INLINE u_int blk2msf(u_int blk)
 862{
 863	MSF msf;
 864	u_int mm;
 865	
 866	msf.c[3] = 0;
 867	msf.c[2] = (blk + CD_MSF_OFFSET) / (CD_SECS * CD_FRAMES);
 868	mm = (blk + CD_MSF_OFFSET) % (CD_SECS * CD_FRAMES);
 869	msf.c[1] = mm / CD_FRAMES;
 870	msf.c[0] = mm % CD_FRAMES;
 871	return (msf.n);
 872}
 873/*==========================================================================*/
 874static INLINE u_int make16(u_char rh, u_char rl)
 875{
 876	return ((rh<<8)|rl);
 877}
 878/*==========================================================================*/
 879static INLINE u_int make32(u_int rh, u_int rl)
 880{
 881	return ((rh<<16)|rl);
 882}
 883/*==========================================================================*/
 884static INLINE u_char swap_nibbles(u_char i)
 885{
 886	return ((i<<4)|(i>>4));
 887}
 888/*==========================================================================*/
 889static INLINE u_char byt2bcd(u_char i)
 890{
 891	return (((i/10)<<4)+i%10);
 892}
 893/*==========================================================================*/
 894static INLINE u_char bcd2bin(u_char bcd)
 895{
 896	return ((bcd>>4)*10+(bcd&0x0F));
 897}
 898/*==========================================================================*/
 899static INLINE int msf2blk(int msfx)
 900{
 901	MSF msf;
 902	int i;
 903	
 904	msf.n=msfx;
 905	i=(msf.c[2] * CD_SECS + msf.c[1]) * CD_FRAMES + msf.c[0] - CD_MSF_OFFSET;
 906	if (i<0) return (0);
 907	return (i);
 908}
 909/*==========================================================================*/
 910/*
 911 *  convert m-s-f_number (3 bytes only) to logical_block_address 
 912 */
 913static INLINE int msf2lba(u_char *msf)
 914{
 915	int i;
 916	
 917	i=(msf[0] * CD_SECS + msf[1]) * CD_FRAMES + msf[2] - CD_MSF_OFFSET;
 918	if (i<0) return (0);
 919	return (i);
 920}
 921/*==========================================================================*/
 922/* evaluate cc_ReadError code */ 
 923static int sta2err(int sta)
 924{
 925	if (famT_drive)
 926	{
 927		if (sta==0x00) return (0);
 928		if (sta==0x01) return (-604); /* CRC error */
 929		if (sta==0x02) return (-602); /* drive not ready */
 930		if (sta==0x03) return (-607); /* unknown media */
 931		if (sta==0x04) return (-612); /* general failure */
 932		if (sta==0x05) return (0);
 933		if (sta==0x06) return (-ERR_DISKCHANGE); /* disk change */
 934		if (sta==0x0b) return (-612); /* general failure */
 935		if (sta==0xff) return (-612); /* general failure */
 936		return (0);
 937	}
 938	else
 939	{
 940		if (sta<=2) return (sta);
 941		if (sta==0x05) return (-604); /* CRC error */
 942		if (sta==0x06) return (-606); /* seek error */
 943		if (sta==0x0d) return (-606); /* seek error */
 944		if (sta==0x0e) return (-603); /* unknown command */
 945		if (sta==0x14) return (-603); /* unknown command */
 946		if (sta==0x0c) return (-611); /* read fault */
 947		if (sta==0x0f) return (-611); /* read fault */
 948		if (sta==0x10) return (-611); /* read fault */
 949		if (sta>=0x16) return (-612); /* general failure */
 950		if (sta==0x11) return (-ERR_DISKCHANGE); /* disk change (LCS: removed) */
 951		if (famL_drive)
 952			if (sta==0x12) return (-ERR_DISKCHANGE); /* disk change (inserted) */
 953		return (-602); /* drive not ready */
 954	}
 955}
 956/*==========================================================================*/
 957static INLINE void clr_cmdbuf(void)
 958{
 959	int i;
 960	
 961	for (i=0;i<10;i++) drvcmd[i]=0;
 962	cmd_type=0;
 963}
 964/*==========================================================================*/
 965static void flush_status(void)
 966{
 967	int i;
 968	
 969	sbp_sleep(15*HZ/10);
 970	for (i=maxtim_data;i!=0;i--) inb(CDi_status);
 971}
 972/*====================================================================*/
 973/*
 974 * CDi status loop for Teac CD-55A (Rob Riggs)
 975 *
 976 * This is needed because for some strange reason
 977 * the CD-55A can take a real long time to give a
 978 * status response. This seems to happen after we
 979 * issue a READ command where a long seek is involved.
 980 *
 981 * I tried to ensure that we get max throughput with
 982 * minimal busy waiting. We busy wait at first, then
 983 * "switch gears" and start sleeping. We sleep for
 984 * longer periods of time the longer we wait.
 985 *
 986 */
 987static int CDi_stat_loop_T(void)
 988{
 989	int	i, gear=1;
 990	u_long  timeout_1, timeout_2, timeout_3, timeout_4;
 991
 992	timeout_1 = jiffies + HZ / 50;  /* sbp_sleep(0) for a short period */
 993	timeout_2 = jiffies + HZ / 5;	/* nap for no more than 200ms */
 994	timeout_3 = jiffies + 5 * HZ;	/* sleep for up to 5s */
 995	timeout_4 = jiffies + 45 * HZ;	/* long sleep for up to 45s. */
 996	do
 997          {
 998            i = inb(CDi_status);
 999            if (!(i&s_not_data_ready)) return (i);
1000            if (!(i&s_not_result_ready)) return (i);
1001            switch(gear)
1002              {
1003              case 4:
1004                sbp_sleep(HZ);
1005                if (time_after(jiffies, timeout_4)) gear++;
1006                msg(DBG_TEA, "CDi_stat_loop_T: long sleep active.\n");
1007                break;
1008              case 3:
1009                sbp_sleep(HZ/10);
1010                if (time_after(jiffies, timeout_3)) gear++;
1011                break;
1012              case 2:
1013                sbp_sleep(HZ/100);
1014                if (time_after(jiffies, timeout_2)) gear++;
1015                break;
1016              case 1:
1017                sbp_sleep(0);
1018                if (time_after(jiffies, timeout_1)) gear++;
1019              }
1020          } while (gear < 5);
1021	return -1;
1022}
1023/*==========================================================================*/
1024static int CDi_stat_loop(void)
1025{
1026	int i,j;
1027	
1028	for(timeout = jiffies + 10*HZ, i=maxtim_data; time_before(jiffies, timeout); )
1029	{
1030		for ( ;i!=0;i--)
1031		{
1032			j=inb(CDi_status);
1033			if (!(j&s_not_data_ready)) return (j);
1034			if (!(j&s_not_result_ready)) return (j);
1035			if (fam0L_drive) if (j&s_attention) return (j);
1036		}
1037		sbp_sleep(1);
1038		i = 1;
1039	}
1040	msg(DBG_LCS,"CDi_stat_loop failed in line %d\n", __LINE__);
1041	return (-1);
1042}
1043/*==========================================================================*/
1044#if 00000
1045/*==========================================================================*/
1046static int tst_DataReady(void)
1047{
1048	int i;
1049	
1050	i=inb(CDi_status);
1051	if (i&s_not_data_ready) return (0);
1052	return (1);
1053}
1054/*==========================================================================*/
1055static int tst_ResultReady(void)
1056{
1057	int i;
1058	
1059	i=inb(CDi_status);
1060	if (i&s_not_result_ready) return (0);
1061	return (1);
1062}
1063/*==========================================================================*/
1064static int tst_Attention(void)
1065{
1066	int i;
1067	
1068	i=inb(CDi_status);
1069	if (i&s_attention) return (1);
1070	return (0);
1071}
1072/*==========================================================================*/
1073#endif
1074/*==========================================================================*/
1075static int ResponseInfo(void)
1076{
1077	int i,j,st=0;
1078	u_long timeout;
1079	
1080	for (i=0,timeout=jiffies+HZ;i<response_count;i++) 
1081	{
1082		for (j=maxtim_data; ; )
1083		{
1084			for ( ;j!=0;j-- )
1085			{
1086				st=inb(CDi_status);
1087				if (!(st&s_not_result_ready)) break;
1088			}
1089			if ((j!=0)||time_after_eq(jiffies, timeout)) break;
1090			sbp_sleep(1);
1091			j = 1;
1092		}
1093		if (time_after_eq(jiffies, timeout)) break;
1094		infobuf[i]=inb(CDi_info);
1095	}
1096#if 000
1097	while (!(inb(CDi_status)&s_not_result_ready))
1098	{
1099		infobuf[i++]=inb(CDi_info);
1100	}
1101	j=i-response_count;
1102	if (j>0) msg(DBG_INF,"ResponseInfo: got %d trailing bytes.\n",j);
1103#endif /* 000 */
1104	for (j=0;j<i;j++)
1105		sprintf(&msgbuf[j*3]," %02X",infobuf[j]);
1106	msgbuf[j*3]=0;
1107	msg(DBG_CMD,"ResponseInfo:%s (%d,%d)\n",msgbuf,response_count,i);
1108	j=response_count-i;
1109	if (j>0) return (-j);
1110	else return (i);
1111}
1112/*==========================================================================*/
1113static void EvaluateStatus(int st)
1114{
1115	current_drive->status_bits=0;
1116	if (fam1_drive) current_drive->status_bits=st|p_success;
1117	else if (fam0_drive)
1118	{
1119		if (st&p_caddin_old) current_drive->status_bits |= p_door_closed|p_caddy_in;
1120		if (st&p_spinning) current_drive->status_bits |= p_spinning;
1121		if (st&p_check) current_drive->status_bits |= p_check;
1122 		if (st&p_success_old) current_drive->status_bits |= p_success;
1123 		if (st&p_busy_old) current_drive->status_bits |= p_busy_new;
1124		if (st&p_disk_ok) current_drive->status_bits |= p_disk_ok;
1125	}
1126	else if (famLV_drive)
1127	{
1128 		current_drive->status_bits |= p_success;
1129		if (st&p_caddin_old) current_drive->status_bits |= p_disk_ok|p_caddy_in;
1130		if (st&p_spinning) current_drive->status_bits |= p_spinning;
1131		if (st&p_check) current_drive->status_bits |= p_check;
1132		if (st&p_busy_old) current_drive->status_bits |= p_busy_new;
1133		if (st&p_lcs_door_closed) current_drive->status_bits |= p_door_closed;
1134		if (st&p_lcs_door_locked) current_drive->status_bits |= p_door_locked;
1135	}
1136	else if (fam2_drive)
1137	{
1138 		current_drive->status_bits |= p_success;
1139		if (st&p2_check) current_drive->status_bits |= p1_check;
1140		if (st&p2_door_closed) current_drive->status_bits |= p1_door_closed;
1141		if (st&p2_disk_in) current_drive->status_bits |= p1_disk_in;
1142		if (st&p2_busy1) current_drive->status_bits |= p1_busy;
1143		if (st&p2_busy2) current_drive->status_bits |= p1_busy;
1144		if (st&p2_spinning) current_drive->status_bits |= p1_spinning;
1145		if (st&p2_door_locked) current_drive->status_bits |= p1_door_locked;
1146		if (st&p2_disk_ok) current_drive->status_bits |= p1_disk_ok;
1147	}
1148	else if (famT_drive)
1149	{
1150		return; /* still needs to get coded */
1151 		current_drive->status_bits |= p_success;
1152		if (st&p2_check) current_drive->status_bits |= p1_check;
1153		if (st&p2_door_closed) current_drive->status_bits |= p1_door_closed;
1154		if (st&p2_disk_in) current_drive->status_bits |= p1_disk_in;
1155		if (st&p2_busy1) current_drive->status_bits |= p1_busy;
1156		if (st&p2_busy2) current_drive->status_bits |= p1_busy;
1157		if (st&p2_spinning) current_drive->status_bits |= p1_spinning;
1158		if (st&p2_door_locked) current_drive->status_bits |= p1_door_locked;
1159		if (st&p2_disk_ok) current_drive->status_bits |= p1_disk_ok;
1160	}
1161	return;
1162}
1163/*==========================================================================*/
1164static int cmd_out_T(void);
1165
1166static int get_state_T(void)
1167{
1168	int i;
1169
1170	clr_cmdbuf();
1171	current_drive->n_bytes=1;
1172	drvcmd[0]=CMDT_STATUS;
1173	i=cmd_out_T();
1174	if (i>=0) i=infobuf[0];
1175	else
1176	{
1177		msg(DBG_TEA,"get_state_T error %d\n", i);
1178		return (i);
1179	}
1180	if (i>=0)
1181		/* 2: closed, disk in */
1182		current_drive->status_bits=p1_door_closed|p1_disk_in|p1_spinning|p1_disk_ok;
1183	else if (current_drive->error_state==6)
1184	{
1185		/* 3: closed, disk in, changed ("06 xx xx") */
1186		current_drive->status_bits=p1_door_closed|p1_disk_in;
1187		current_drive->CD_changed=0xFF;
1188		current_drive->diskstate_flags &= ~toc_bit;
1189	}
1190	else if ((current_drive->error_state!=2)||(current_drive->b3!=0x3A)||(current_drive->b4==0x00))
1191	{
1192		/* 1: closed, no disk ("xx yy zz"or "02 3A 00") */
1193		current_drive->status_bits=p1_door_closed;
1194		current_drive->open_count=0;
1195	}
1196	else if (current_drive->b4==0x01)
1197	{
1198		/* 0: open ("02 3A 01") */
1199		current_drive->status_bits=0;
1200		current_drive->open_count=0;
1201	}
1202	else
1203	{
1204		/* 1: closed, no disk ("02 3A xx") */
1205		current_drive->status_bits=p1_door_closed;
1206		current_drive->open_count=0;
1207	}
1208	return (current_drive->status_bits);
1209}
1210/*==========================================================================*/
1211static int ResponseStatus(void)
1212{
1213	int i,j;
1214	u_long timeout;
1215	
1216	msg(DBG_STA,"doing ResponseStatus...\n");
1217	if (famT_drive) return (get_state_T());
1218	if (flags_cmd_out & f_respo3) timeout = jiffies;
1219	else if (flags_cmd_out & f_respo2) timeout = jiffies + 16*HZ;
1220	else timeout = jiffies + 4*HZ;
1221	j=maxtim_8;
1222	do
1223	{
1224		for ( ;j!=0;j--)
1225		{ 
1226			i=inb(CDi_status);
1227			if (!(i&s_not_result_ready)) break;
1228		}
1229		if ((j!=0)||time_after(jiffies, timeout)) break;
1230		sbp_sleep(1);
1231		j = 1;
1232	}
1233	while (1);
1234	if (j==0) 
1235	{
1236		if ((flags_cmd_out & f_respo3) == 0)
1237			msg(DBG_STA,"ResponseStatus: timeout.\n");
1238		current_drive->status_bits=0;
1239		return (-401);
1240	}
1241	i=inb(CDi_info);
1242	msg(DBG_STA,"ResponseStatus: response %02X.\n", i);
1243	EvaluateStatus(i);
1244	msg(DBG_STA,"status_bits=%02X, i=%02X\n",current_drive->status_bits,i);
1245	return (current_drive->status_bits);
1246}
1247/*==========================================================================*/
1248static void cc_ReadStatus(void)
1249{
1250	int i;
1251	
1252	msg(DBG_STA,"giving cc_ReadStatus command\n");
1253	if (famT_drive) return;
1254	SBPCD_CLI;
1255	if (fam0LV_drive) OUT(CDo_command,CMD0_STATUS);
1256	else if (fam1_drive) OUT(CDo_command,CMD1_STATUS);
1257	else if (fam2_drive) OUT(CDo_command,CMD2_STATUS);
1258	if (!fam0LV_drive) for (i=0;i<6;i++) OUT(CDo_command,0);
1259	SBPCD_STI;
1260}
1261/*==========================================================================*/
1262static int cc_ReadError(void)
1263{
1264	int i;
1265
1266	clr_cmdbuf();
1267	msg(DBG_ERR,"giving cc_ReadError command.\n");
1268	if (fam1_drive)
1269	{
1270		drvcmd[0]=CMD1_READ_ERR;
1271		response_count=8;
1272		flags_cmd_out=f_putcmd|f_ResponseStatus;
1273	}
1274	else if (fam0LV_drive)
1275	{
1276		drvcmd[0]=CMD0_READ_ERR;
1277		response_count=6;
1278		if (famLV_drive)
1279			flags_cmd_out=f_putcmd;
1280		else
1281			flags_cmd_out=f_putcmd|f_getsta|f_ResponseStatus;
1282	}
1283	else if (fam2_drive)
1284	{
1285		drvcmd[0]=CMD2_READ_ERR;
1286		response_count=6;
1287		flags_cmd_out=f_putcmd;
1288	}
1289	else if (famT_drive)
1290	{
1291		response_count=5;
1292		drvcmd[0]=CMDT_READ_ERR;
1293	}
1294	i=cmd_out();
1295	current_drive->error_byte=0;
1296	msg(DBG_ERR,"cc_ReadError: cmd_out(CMDx_READ_ERR) returns %d (%02X)\n",i,i);
1297	if (i<0) return (i);
1298	if (fam0V_drive) i=1;
1299	else i=2;
1300	current_drive->error_byte=infobuf[i];
1301	msg(DBG_ERR,"cc_ReadError: infobuf[%d] is %d (%02X)\n",i,current_drive->error_byte,current_drive->error_byte);
1302	i=sta2err(infobuf[i]);
1303        if (i==-ERR_DISKCHANGE)
1304        {
1305                current_drive->CD_changed=0xFF;
1306                current_drive->diskstate_flags &= ~toc_bit;
1307        }
1308	return (i);
1309}
1310/*==========================================================================*/
1311static int cc_DriveReset(void);
1312
1313static int cmd_out_T(void)
1314{
1315#undef CMDT_TRIES
1316#define CMDT_TRIES 1000
1317#define TEST_FALSE_FF 1
1318
1319	int i, j, l=0, m, ntries;
1320	unsigned long flags;
1321
1322	current_drive->error_state=0;
1323	current_drive->b3=0;
1324	current_drive->b4=0;
1325	current_drive->f_drv_error=0;
1326	for (i=0;i<10;i++) sprintf(&msgbuf[i*3]," %02X",drvcmd[i]);
1327	msgbuf[i*3]=0;
1328	msg(DBG_CMD,"cmd_out_T:%s\n",msgbuf);
1329
1330	OUT(CDo_sel_i_d,0);
1331	OUT(CDo_enable,current_drive->drv_sel);
1332	i=inb(CDi_status);
1333	do_16bit=0;
1334	if ((f_16bit)&&(!(i&0x80)))
1335	{
1336		do_16bit=1;
1337		msg(DBG_TEA,"cmd_out_T: do_16bit set.\n");
1338	}
1339	if (!(i&s_not_result_ready))
1340	do
1341	{
1342		j=inb(CDi_info);
1343		i=inb(CDi_status);
1344		sbp_sleep(0);
1345		msg(DBG_TEA,"cmd_out_T: spurious !s_not_result_ready. (%02X)\n", j);
1346	}
1347	while (!(i&s_not_result_ready));
1348	save_flags(flags); cli();
1349	for (i=0;i<10;i++) OUT(CDo_command,drvcmd[i]);
1350	restore_flags(flags);
1351	for (ntries=CMDT_TRIES;ntries>0;ntries--)
1352	{
1353		if (drvcmd[0]==CMDT_READ_VER) sbp_sleep(HZ); /* fixme */
1354#if 01
1355		OUT(CDo_sel_i_d,1);
1356#endif /* 01 */
1357		if (teac==2)
1358                  {
1359                    if ((i=CDi_stat_loop_T()) == -1) break;
1360                  }
1361		else
1362                  {
1363#if 0
1364                    OUT(CDo_sel_i_d,1);
1365#endif /* 0 */ 
1366                    i=inb(CDi_status);
1367                  }
1368		if (!(i&s_not_data_ready)) /* f.e. CMDT_DISKINFO */
1369		{
1370			OUT(CDo_sel_i_d,1);
1371			if (drvcmd[0]==CMDT_READ) return (0); /* handled elsewhere */
1372			if (drvcmd[0]==CMDT_DISKINFO)
1373			{
1374				l=0;
1375				do
1376                                {
1377                                        if (do_16bit)
1378                                        {
1379                                                i=inw(CDi_data);
1380                                                infobuf[l++]=i&0x0ff;
1381                                                infobuf[l++]=i>>8;
1382#if TEST_FALSE_FF
1383                                                if ((l==2)&&(infobuf[0]==0x0ff))
1384                                                {
1385                                                        infobuf[0]=infobuf[1];
1386                                                        l=1;
1387                                                        msg(DBG_TEA,"cmd_out_T: do_16bit: false first byte!\n");
1388                                                }
1389#endif /* TEST_FALSE_FF */ 
1390                                        }
1391                                        else infobuf[l++]=inb(CDi_data);
1392                                        i=inb(CDi_status);
1393                                }
1394				while (!(i&s_not_data_ready));
1395				for (j=0;j<l;j++) sprintf(&msgbuf[j*3]," %02X",infobuf[j]);
1396				msgbuf[j*3]=0;
1397				msg(DBG_CMD,"cmd_out_T data response:%s\n", msgbuf);
1398			}
1399			else
1400			{
1401				msg(DBG_TEA,"cmd_out_T: data response with cmd_%02X!\n",
1402                                    drvcmd[0]);
1403				j=0;
1404				do
1405				{
1406                                        if (do_16bit) i=inw(CDi_data);
1407                                        else i=inb(CDi_data);
1408                                        j++;
1409                                        i=inb(CDi_status);
1410				}
1411				while (!(i&s_not_data_ready));
1412				msg(DBG_TEA,"cmd_out_T: data response: discarded %d bytes/words.\n", j);
1413				fatal_err++;
1414			}
1415		}
1416		i=inb(CDi_status);
1417		if (!(i&s_not_result_ready))
1418		{
1419			OUT(CDo_sel_i_d,0);
1420			if (drvcmd[0]==CMDT_DISKINFO) m=l;
1421			else m=0;
1422			do
1423			{
1424				

Large files files are truncated, but you can click here to view the full file