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