/drivers/cdrom/sbpcd.c

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

Large files are truncated click here to view the full file

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