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