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