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