cm206.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /* cm206.c. A linux-driver for the cm206 cdrom player with cm260 adapter card.
  2. Copyright (c) 1995--1997 David A. van Leeuwen.
  3. $Id: cm206.c,v 1.5 1997/12/26 11:02:51 david Exp $
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. History:
  16. Started 25 jan 1994. Waiting for documentation...
  17. 22 feb 1995: 0.1a first reasonably safe polling driver.
  18. Two major bugs, one in read_sector and one in
  19. do_cm206_request, happened to cancel!
  20. 25 feb 1995: 0.2a first reasonable interrupt driven version of above.
  21. uart writes are still done in polling mode.
  22. 25 feb 1995: 0.21a writes also in interrupt mode, still some
  23. small bugs to be found... Larger buffer.
  24. 2 mrt 1995: 0.22 Bug found (cd-> nowhere, interrupt was called in
  25. initialization), read_ahead of 16. Timeouts implemented.
  26. unclear if they do something...
  27. 7 mrt 1995: 0.23 Start of background read-ahead.
  28. 18 mrt 1995: 0.24 Working background read-ahead. (still problems)
  29. 26 mrt 1995: 0.25 Multi-session ioctl added (kernel v1.2).
  30. Statistics implemented, though separate stats206.h.
  31. Accessible through ioctl 0x1000 (just a number).
  32. Hard to choose between v1.2 development and 1.1.75.
  33. Bottom-half doesn't work with 1.2...
  34. 0.25a: fixed... typo. Still problems...
  35. 1 apr 1995: 0.26 Module support added. Most bugs found. Use kernel 1.2.n.
  36. 5 apr 1995: 0.27 Auto-probe for the adapter card base address.
  37. Auto-probe for the adaptor card irq line.
  38. 7 apr 1995: 0.28 Added lilo setup support for base address and irq.
  39. Use major number 32 (not in this source), officially
  40. assigned to this driver.
  41. 9 apr 1995: 0.29 Added very limited audio support. Toc_header, stop, pause,
  42. resume, eject. Play_track ignores track info, because we can't
  43. read a table-of-contents entry. Toc_entry is implemented
  44. as a `placebo' function: always returns start of disc.
  45. 3 may 1995: 0.30 Audio support completed. The get_toc_entry function
  46. is implemented as a binary search.
  47. 15 may 1995: 0.31 More work on audio stuff. Workman is not easy to
  48. satisfy; changed binary search into linear search.
  49. Auto-probe for base address somewhat relaxed.
  50. 1 jun 1995: 0.32 Removed probe_irq_on/off for module version.
  51. 10 jun 1995: 0.33 Workman still behaves funny, but you should be
  52. able to eject and substitute another disc.
  53. An adaptation of 0.33 is included in linux-1.3.7 by Eberhard Moenkeberg
  54. 18 jul 1995: 0.34 Patch by Heiko Eissfeldt included, mainly considering
  55. verify_area's in the ioctls. Some bugs introduced by
  56. EM considering the base port and irq fixed.
  57. 18 dec 1995: 0.35 Add some code for error checking... no luck...
  58. We jump to reach our goal: version 1.0 in the next stable linux kernel.
  59. 19 mar 1996: 0.95 Different implementation of CDROM_GET_UPC, on
  60. request of Thomas Quinot.
  61. 25 mar 1996: 0.96 Interpretation of opening with O_WRONLY or O_RDWR:
  62. open only for ioctl operation, e.g., for operation of
  63. tray etc.
  64. 4 apr 1996: 0.97 First implementation of layer between VFS and cdrom
  65. driver, a generic interface. Much of the functionality
  66. of cm206_open() and cm206_ioctl() is transferred to a
  67. new file cdrom.c and its header ucdrom.h.
  68. Upgrade to Linux kernel 1.3.78.
  69. 11 apr 1996 0.98 Upgrade to Linux kernel 1.3.85
  70. More code moved to cdrom.c
  71. 0.99 Some more small changes to decrease number
  72. of oopses at module load;
  73. 27 jul 1996 0.100 Many hours of debugging, kernel change from 1.2.13
  74. to 2.0.7 seems to have introduced some weird behavior
  75. in (interruptible_)sleep_on(&cd->data): the process
  76. seems to be woken without any explicit wake_up in my own
  77. code. Patch to try 100x in case such untriggered wake_up's
  78. occur.
  79. 28 jul 1996 0.101 Rewriting of the code that receives the command echo,
  80. using a fifo to store echoed bytes.
  81. Branch from 0.99:
  82. 0.99.1.0 Update to kernel release 2.0.10 dev_t -> kdev_t
  83. (emoenke) various typos found by others. extra
  84. module-load oops protection.
  85. 0.99.1.1 Initialization constant cdrom_dops.speed
  86. changed from float (2.0) to int (2); Cli()-sti() pair
  87. around cm260_reset() in module initialization code.
  88. 0.99.1.2 Changes literally as proposed by Scott Snyder
  89. <snyder@d0sgif.fnal.gov> for the 2.1 kernel line, which
  90. have to do mainly with the poor minor support i had. The
  91. major new concept is to change a cdrom driver's
  92. operations struct from the capabilities struct. This
  93. reflects the fact that there is one major for a driver,
  94. whilst there can be many minors whith completely
  95. different capabilities.
  96. 0.99.1.3 More changes for operations/info separation.
  97. 0.99.1.4 Added speed selection (someone had to do this
  98. first).
  99. 23 jan 1997 0.99.1.5 MODULE_PARMS call added.
  100. 23 jan 1997 0.100.1.2--0.100.1.5 following similar lines as
  101. 0.99.1.1--0.99.1.5. I get too many complaints about the
  102. drive making read errors. What't wrong with the 2.0+
  103. kernel line? Why get i (and othe cm206 owners) weird
  104. results? Why were things good in the good old 1.1--1.2
  105. era? Why don't i throw away the drive?
  106. 2 feb 1997 0.102 Added `volatile' to values in cm206_struct. Seems to
  107. reduce many of the problems. Rewrote polling routines
  108. to use fixed delays between polls.
  109. 0.103 Changed printk behavior.
  110. 0.104 Added a 0.100 -> 0.100.1.1 change
  111. 11 feb 1997 0.105 Allow auto_probe during module load, disable
  112. with module option "auto_probe=0". Moved some debugging
  113. statements to lower priority. Implemented select_speed()
  114. function.
  115. 13 feb 1997 1.0 Final version for 2.0 kernel line.
  116. All following changes will be for the 2.1 kernel line.
  117. 15 feb 1997 1.1 Keep up with kernel 2.1.26, merge in changes from
  118. cdrom.c 0.100.1.1--1.0. Add some more MODULE_PARMS.
  119. 14 sep 1997 1.2 Upgrade to Linux 2.1.55. Added blksize_size[], patch
  120. sent by James Bottomley <James.Bottomley@columbiasc.ncr.com>.
  121. 21 dec 1997 1.4 Upgrade to Linux 2.1.72.
  122. 24 jan 1998 Removed the cm206_disc_status() function, as it was now dead
  123. code. The Uniform CDROM driver now provides this functionality.
  124. 9 Nov. 1999 Make kernel-parameter implementation work with 2.3.x
  125. Removed init_module & cleanup_module in favor of
  126. module_init & module_exit.
  127. Torben Mathiasen <tmm@image.dk>
  128. *
  129. * Parts of the code are based upon lmscd.c written by Kai Petzke,
  130. * sbpcd.c written by Eberhard Moenkeberg, and mcd.c by Martin
  131. * Harriss, but any off-the-shelf dynamic programming algorithm won't
  132. * be able to find them.
  133. *
  134. * The cm206 drive interface and the cm260 adapter card seem to be
  135. * sufficiently different from their cm205/cm250 counterparts
  136. * in order to write a complete new driver.
  137. *
  138. * I call all routines connected to the Linux kernel something
  139. * with `cm206' in it, as this stuff is too series-dependent.
  140. *
  141. * Currently, my limited knowledge is based on:
  142. * - The Linux Kernel Hacker's guide, v. 0.5, by Michael K. Johnson
  143. * - Linux Kernel Programmierung, by Michael Beck and others
  144. * - Philips/LMS cm206 and cm226 product specification
  145. * - Philips/LMS cm260 product specification
  146. *
  147. * David van Leeuwen, david@tm.tno.nl. */
  148. #define REVISION "$Revision: 1.5 $"
  149. #include <linux/module.h>
  150. #include <linux/errno.h> /* These include what we really need */
  151. #include <linux/delay.h>
  152. #include <linux/string.h>
  153. #include <linux/sched.h>
  154. #include <linux/interrupt.h>
  155. #include <linux/timer.h>
  156. #include <linux/cdrom.h>
  157. #include <linux/devfs_fs_kernel.h>
  158. #include <linux/ioport.h>
  159. #include <linux/mm.h>
  160. #include <linux/slab.h>
  161. #include <linux/init.h>
  162. /* #include <linux/ucdrom.h> */
  163. #include <asm/io.h>
  164. #define MAJOR_NR CM206_CDROM_MAJOR
  165. #include <linux/blkdev.h>
  166. #undef DEBUG
  167. #define STATISTICS /* record times and frequencies of events */
  168. #define AUTO_PROBE_MODULE
  169. #define USE_INSW
  170. #include "cm206.h"
  171. /* This variable defines whether or not to probe for adapter base port
  172. address and interrupt request. It can be overridden by the boot
  173. parameter `auto'.
  174. */
  175. static int auto_probe = 1; /* Yes, why not? */
  176. static int cm206_base = CM206_BASE;
  177. static int cm206_irq = CM206_IRQ;
  178. #ifdef MODULE
  179. static int cm206[2] = { 0, 0 }; /* for compatible `insmod' parameter passing */
  180. module_param_array(cm206, int, NULL, 0); /* base,irq or irq,base */
  181. #endif
  182. module_param(cm206_base, int, 0); /* base */
  183. module_param(cm206_irq, int, 0); /* irq */
  184. module_param(auto_probe, bool, 0); /* auto probe base and irq */
  185. MODULE_LICENSE("GPL");
  186. #define POLLOOP 100 /* milliseconds */
  187. #define READ_AHEAD 1 /* defines private buffer, waste! */
  188. #define BACK_AHEAD 1 /* defines adapter-read ahead */
  189. #define DATA_TIMEOUT (3*HZ) /* measured in jiffies (10 ms) */
  190. #define UART_TIMEOUT (5*HZ/100)
  191. #define DSB_TIMEOUT (7*HZ) /* time for the slowest command to finish */
  192. #define UR_SIZE 4 /* uart receive buffer fifo size */
  193. #define LINUX_BLOCK_SIZE 512 /* WHERE is this defined? */
  194. #define RAW_SECTOR_SIZE 2352 /* ok, is also defined in cdrom.h */
  195. #define ISO_SECTOR_SIZE 2048
  196. #define BLOCKS_ISO (ISO_SECTOR_SIZE/LINUX_BLOCK_SIZE) /* 4 */
  197. #define CD_SYNC_HEAD 16 /* CD_SYNC + CD_HEAD */
  198. #ifdef STATISTICS /* keep track of errors in counters */
  199. #define stats(i) { ++cd->stats[st_ ## i]; \
  200. cd->last_stat[st_ ## i] = cd->stat_counter++; \
  201. }
  202. #else
  203. #define stats(i) (void) 0;
  204. #endif
  205. #define Debug(a) {printk (KERN_DEBUG); printk a;}
  206. #ifdef DEBUG
  207. #define debug(a) Debug(a)
  208. #else
  209. #define debug(a) (void) 0;
  210. #endif
  211. typedef unsigned char uch; /* 8-bits */
  212. typedef unsigned short ush; /* 16-bits */
  213. struct toc_struct { /* private copy of Table of Contents */
  214. uch track, fsm[3], q0;
  215. };
  216. struct cm206_struct {
  217. volatile ush intr_ds; /* data status read on last interrupt */
  218. volatile ush intr_ls; /* uart line status read on last interrupt */
  219. volatile uch ur[UR_SIZE]; /* uart receive buffer fifo */
  220. volatile uch ur_w, ur_r; /* write/read buffer index */
  221. volatile uch dsb, cc; /* drive status byte and condition (error) code */
  222. int command; /* command to be written to the uart */
  223. int openfiles;
  224. ush sector[READ_AHEAD * RAW_SECTOR_SIZE / 2]; /* buffered cd-sector */
  225. int sector_first, sector_last; /* range of these sectors */
  226. wait_queue_head_t uart; /* wait queues for interrupt */
  227. wait_queue_head_t data;
  228. struct timer_list timer; /* time-out */
  229. char timed_out;
  230. signed char max_sectors; /* number of sectors that fit in adapter mem */
  231. char wait_back; /* we're waiting for a background-read */
  232. char background; /* is a read going on in the background? */
  233. int adapter_first; /* if so, that's the starting sector */
  234. int adapter_last;
  235. char fifo_overflowed;
  236. uch disc_status[7]; /* result of get_disc_status command */
  237. #ifdef STATISTICS
  238. int stats[NR_STATS];
  239. int last_stat[NR_STATS]; /* `time' at which stat was stat */
  240. int stat_counter;
  241. #endif
  242. struct toc_struct toc[101]; /* The whole table of contents + lead-out */
  243. uch q[10]; /* Last read q-channel info */
  244. uch audio_status[5]; /* last read position on pause */
  245. uch media_changed; /* record if media changed */
  246. };
  247. #define DISC_STATUS cd->disc_status[0]
  248. #define FIRST_TRACK cd->disc_status[1]
  249. #define LAST_TRACK cd->disc_status[2]
  250. #define PAUSED cd->audio_status[0] /* misuse this memory byte! */
  251. #define PLAY_TO cd->toc[0] /* toc[0] records end-time in play */
  252. static struct cm206_struct *cd; /* the main memory structure */
  253. static struct request_queue *cm206_queue;
  254. static DEFINE_SPINLOCK(cm206_lock);
  255. /* First, we define some polling functions. These are actually
  256. only being used in the initialization. */
  257. static void send_command_polled(int command)
  258. {
  259. int loop = POLLOOP;
  260. while (!(inw(r_line_status) & ls_transmitter_buffer_empty)
  261. && loop > 0) {
  262. mdelay(1); /* one millisec delay */
  263. --loop;
  264. }
  265. outw(command, r_uart_transmit);
  266. }
  267. static uch receive_echo_polled(void)
  268. {
  269. int loop = POLLOOP;
  270. while (!(inw(r_line_status) & ls_receive_buffer_full) && loop > 0) {
  271. mdelay(1);
  272. --loop;
  273. }
  274. return ((uch) inw(r_uart_receive));
  275. }
  276. static uch send_receive_polled(int command)
  277. {
  278. send_command_polled(command);
  279. return receive_echo_polled();
  280. }
  281. static inline void clear_ur(void)
  282. {
  283. if (cd->ur_r != cd->ur_w) {
  284. debug(("Deleting bytes from fifo:"));
  285. for (; cd->ur_r != cd->ur_w;
  286. cd->ur_r++, cd->ur_r %= UR_SIZE)
  287. debug((" 0x%x", cd->ur[cd->ur_r]));
  288. debug(("\n"));
  289. }
  290. }
  291. static struct tasklet_struct cm206_tasklet;
  292. /* The interrupt handler. When the cm260 generates an interrupt, very
  293. much care has to be taken in reading out the registers in the right
  294. order; in case of a receive_buffer_full interrupt, first the
  295. uart_receive must be read, and then the line status again to
  296. de-assert the interrupt line. It took me a couple of hours to find
  297. this out:-(
  298. The function reset_cm206 appears to cause an interrupt, because
  299. pulling up the INIT line clears both the uart-write-buffer /and/
  300. the uart-write-buffer-empty mask. We call this a `lost interrupt,'
  301. as there seems so reason for this to happen.
  302. */
  303. static irqreturn_t cm206_interrupt(int sig, void *dev_id, struct pt_regs *regs)
  304. {
  305. volatile ush fool;
  306. cd->intr_ds = inw(r_data_status); /* resets data_ready, data_error,
  307. crc_error, sync_error, toc_ready
  308. interrupts */
  309. cd->intr_ls = inw(r_line_status); /* resets overrun bit */
  310. debug(("Intr, 0x%x 0x%x, %d\n", cd->intr_ds, cd->intr_ls,
  311. cd->background));
  312. if (cd->intr_ls & ls_attention)
  313. stats(attention);
  314. /* receive buffer full? */
  315. if (cd->intr_ls & ls_receive_buffer_full) {
  316. cd->ur[cd->ur_w] = inb(r_uart_receive); /* get order right! */
  317. cd->intr_ls = inw(r_line_status); /* resets rbf interrupt */
  318. debug(("receiving #%d: 0x%x\n", cd->ur_w,
  319. cd->ur[cd->ur_w]));
  320. cd->ur_w++;
  321. cd->ur_w %= UR_SIZE;
  322. if (cd->ur_w == cd->ur_r)
  323. debug(("cd->ur overflow!\n"));
  324. if (waitqueue_active(&cd->uart) && cd->background < 2) {
  325. del_timer(&cd->timer);
  326. wake_up_interruptible(&cd->uart);
  327. }
  328. }
  329. /* data ready in fifo? */
  330. else if (cd->intr_ds & ds_data_ready) {
  331. if (cd->background)
  332. ++cd->adapter_last;
  333. if (waitqueue_active(&cd->data)
  334. && (cd->wait_back || !cd->background)) {
  335. del_timer(&cd->timer);
  336. wake_up_interruptible(&cd->data);
  337. }
  338. stats(data_ready);
  339. }
  340. /* ready to issue a write command? */
  341. else if (cd->command && cd->intr_ls & ls_transmitter_buffer_empty) {
  342. outw(dc_normal | (inw(r_data_status) & 0x7f),
  343. r_data_control);
  344. outw(cd->command, r_uart_transmit);
  345. cd->command = 0;
  346. if (!cd->background)
  347. wake_up_interruptible(&cd->uart);
  348. }
  349. /* now treat errors (at least, identify them for debugging) */
  350. else if (cd->intr_ds & ds_fifo_overflow) {
  351. debug(("Fifo overflow at sectors 0x%x\n",
  352. cd->sector_first));
  353. fool = inw(r_fifo_output_buffer); /* de-assert the interrupt */
  354. cd->fifo_overflowed = 1; /* signal one word less should be read */
  355. stats(fifo_overflow);
  356. } else if (cd->intr_ds & ds_data_error) {
  357. debug(("Data error at sector 0x%x\n", cd->sector_first));
  358. stats(data_error);
  359. } else if (cd->intr_ds & ds_crc_error) {
  360. debug(("CRC error at sector 0x%x\n", cd->sector_first));
  361. stats(crc_error);
  362. } else if (cd->intr_ds & ds_sync_error) {
  363. debug(("Sync at sector 0x%x\n", cd->sector_first));
  364. stats(sync_error);
  365. } else if (cd->intr_ds & ds_toc_ready) {
  366. /* do something appropriate */
  367. }
  368. /* couldn't see why this interrupt, maybe due to init */
  369. else {
  370. outw(dc_normal | READ_AHEAD, r_data_control);
  371. stats(lost_intr);
  372. }
  373. if (cd->background
  374. && (cd->adapter_last - cd->adapter_first == cd->max_sectors
  375. || cd->fifo_overflowed))
  376. tasklet_schedule(&cm206_tasklet); /* issue a stop read command */
  377. stats(interrupt);
  378. return IRQ_HANDLED;
  379. }
  380. /* we have put the address of the wait queue in who */
  381. static void cm206_timeout(unsigned long who)
  382. {
  383. cd->timed_out = 1;
  384. debug(("Timing out\n"));
  385. wake_up_interruptible((wait_queue_head_t *) who);
  386. }
  387. /* This function returns 1 if a timeout occurred, 0 if an interrupt
  388. happened */
  389. static int sleep_or_timeout(wait_queue_head_t * wait, int timeout)
  390. {
  391. cd->timed_out = 0;
  392. init_timer(&cd->timer);
  393. cd->timer.data = (unsigned long) wait;
  394. cd->timer.expires = jiffies + timeout;
  395. add_timer(&cd->timer);
  396. debug(("going to sleep\n"));
  397. interruptible_sleep_on(wait);
  398. del_timer(&cd->timer);
  399. if (cd->timed_out) {
  400. cd->timed_out = 0;
  401. return 1;
  402. } else
  403. return 0;
  404. }
  405. static void send_command(int command)
  406. {
  407. debug(("Sending 0x%x\n", command));
  408. if (!(inw(r_line_status) & ls_transmitter_buffer_empty)) {
  409. cd->command = command;
  410. cli(); /* don't interrupt before sleep */
  411. outw(dc_mask_sync_error | dc_no_stop_on_error |
  412. (inw(r_data_status) & 0x7f), r_data_control);
  413. /* interrupt routine sends command */
  414. if (sleep_or_timeout(&cd->uart, UART_TIMEOUT)) {
  415. debug(("Time out on write-buffer\n"));
  416. stats(write_timeout);
  417. outw(command, r_uart_transmit);
  418. }
  419. debug(("Write commmand delayed\n"));
  420. } else
  421. outw(command, r_uart_transmit);
  422. }
  423. static uch receive_byte(int timeout)
  424. {
  425. uch ret;
  426. cli();
  427. debug(("cli\n"));
  428. ret = cd->ur[cd->ur_r];
  429. if (cd->ur_r != cd->ur_w) {
  430. sti();
  431. debug(("returning #%d: 0x%x\n", cd->ur_r,
  432. cd->ur[cd->ur_r]));
  433. cd->ur_r++;
  434. cd->ur_r %= UR_SIZE;
  435. return ret;
  436. } else if (sleep_or_timeout(&cd->uart, timeout)) { /* does sti() */
  437. debug(("Time out on receive-buffer\n"));
  438. #ifdef STATISTICS
  439. if (timeout == UART_TIMEOUT)
  440. stats(receive_timeout) /* no `;'! */
  441. else
  442. stats(dsb_timeout);
  443. #endif
  444. return 0xda;
  445. }
  446. ret = cd->ur[cd->ur_r];
  447. debug(("slept; returning #%d: 0x%x\n", cd->ur_r,
  448. cd->ur[cd->ur_r]));
  449. cd->ur_r++;
  450. cd->ur_r %= UR_SIZE;
  451. return ret;
  452. }
  453. static inline uch receive_echo(void)
  454. {
  455. return receive_byte(UART_TIMEOUT);
  456. }
  457. static inline uch send_receive(int command)
  458. {
  459. send_command(command);
  460. return receive_echo();
  461. }
  462. static inline uch wait_dsb(void)
  463. {
  464. return receive_byte(DSB_TIMEOUT);
  465. }
  466. static int type_0_command(int command, int expect_dsb)
  467. {
  468. int e;
  469. clear_ur();
  470. if (command != (e = send_receive(command))) {
  471. debug(("command 0x%x echoed as 0x%x\n", command, e));
  472. stats(echo);
  473. return -1;
  474. }
  475. if (expect_dsb) {
  476. cd->dsb = wait_dsb(); /* wait for command to finish */
  477. }
  478. return 0;
  479. }
  480. static int type_1_command(int command, int bytes, uch * status)
  481. { /* returns info */
  482. int i;
  483. if (type_0_command(command, 0))
  484. return -1;
  485. for (i = 0; i < bytes; i++)
  486. status[i] = send_receive(c_gimme);
  487. return 0;
  488. }
  489. /* This function resets the adapter card. We'd better not do this too
  490. * often, because it tends to generate `lost interrupts.' */
  491. static void reset_cm260(void)
  492. {
  493. outw(dc_normal | dc_initialize | READ_AHEAD, r_data_control);
  494. udelay(10); /* 3.3 mu sec minimum */
  495. outw(dc_normal | READ_AHEAD, r_data_control);
  496. }
  497. /* fsm: frame-sec-min from linear address; one of many */
  498. static void fsm(int lba, uch * fsm)
  499. {
  500. fsm[0] = lba % 75;
  501. lba /= 75;
  502. lba += 2;
  503. fsm[1] = lba % 60;
  504. fsm[2] = lba / 60;
  505. }
  506. static inline int fsm2lba(uch * fsm)
  507. {
  508. return fsm[0] + 75 * (fsm[1] - 2 + 60 * fsm[2]);
  509. }
  510. static inline int f_s_m2lba(uch f, uch s, uch m)
  511. {
  512. return f + 75 * (s - 2 + 60 * m);
  513. }
  514. static int start_read(int start)
  515. {
  516. uch read_sector[4] = { c_read_data, };
  517. int i, e;
  518. fsm(start, &read_sector[1]);
  519. clear_ur();
  520. for (i = 0; i < 4; i++)
  521. if (read_sector[i] != (e = send_receive(read_sector[i]))) {
  522. debug(("read_sector: %x echoes %x\n",
  523. read_sector[i], e));
  524. stats(echo);
  525. if (e == 0xff) { /* this seems to happen often */
  526. e = receive_echo();
  527. debug(("Second try %x\n", e));
  528. if (e != read_sector[i])
  529. return -1;
  530. }
  531. }
  532. return 0;
  533. }
  534. static int stop_read(void)
  535. {
  536. int e;
  537. type_0_command(c_stop, 0);
  538. if ((e = receive_echo()) != 0xff) {
  539. debug(("c_stop didn't send 0xff, but 0x%x\n", e));
  540. stats(stop_0xff);
  541. return -1;
  542. }
  543. return 0;
  544. }
  545. /* This function starts to read sectors in adapter memory, the
  546. interrupt routine should stop the read. In fact, the bottom_half
  547. routine takes care of this. Set a flag `background' in the cd
  548. struct to indicate the process. */
  549. static int read_background(int start, int reading)
  550. {
  551. if (cd->background)
  552. return -1; /* can't do twice */
  553. outw(dc_normal | BACK_AHEAD, r_data_control);
  554. if (!reading && start_read(start))
  555. return -2;
  556. cd->adapter_first = cd->adapter_last = start;
  557. cd->background = 1; /* flag a read is going on */
  558. return 0;
  559. }
  560. #ifdef USE_INSW
  561. #define transport_data insw
  562. #else
  563. /* this routine implements insw(,,). There was a time i had the
  564. impression that there would be any difference in error-behaviour. */
  565. void transport_data(int port, ush * dest, int count)
  566. {
  567. int i;
  568. ush *d;
  569. for (i = 0, d = dest; i < count; i++, d++)
  570. *d = inw(port);
  571. }
  572. #endif
  573. #define MAX_TRIES 100
  574. static int read_sector(int start)
  575. {
  576. int tries = 0;
  577. if (cd->background) {
  578. cd->background = 0;
  579. cd->adapter_last = -1; /* invalidate adapter memory */
  580. stop_read();
  581. }
  582. cd->fifo_overflowed = 0;
  583. reset_cm260(); /* empty fifo etc. */
  584. if (start_read(start))
  585. return -1;
  586. do {
  587. if (sleep_or_timeout(&cd->data, DATA_TIMEOUT)) {
  588. debug(("Read timed out sector 0x%x\n", start));
  589. stats(read_timeout);
  590. stop_read();
  591. return -3;
  592. }
  593. tries++;
  594. } while (cd->intr_ds & ds_fifo_empty && tries < MAX_TRIES);
  595. if (tries > 1)
  596. debug(("Took me some tries\n"))
  597. else
  598. if (tries == MAX_TRIES)
  599. debug(("MAX_TRIES tries for read sector\n"));
  600. transport_data(r_fifo_output_buffer, cd->sector,
  601. READ_AHEAD * RAW_SECTOR_SIZE / 2);
  602. if (read_background(start + READ_AHEAD, 1))
  603. stats(read_background);
  604. cd->sector_first = start;
  605. cd->sector_last = start + READ_AHEAD;
  606. stats(read_restarted);
  607. return 0;
  608. }
  609. /* The function of bottom-half is to send a stop command to the drive
  610. This isn't easy because the routine is not `owned' by any process;
  611. we can't go to sleep! The variable cd->background gives the status:
  612. 0 no read pending
  613. 1 a read is pending
  614. 2 c_stop waits for write_buffer_empty
  615. 3 c_stop waits for receive_buffer_full: echo
  616. 4 c_stop waits for receive_buffer_full: 0xff
  617. */
  618. static void cm206_tasklet_func(unsigned long ignore)
  619. {
  620. debug(("bh: %d\n", cd->background));
  621. switch (cd->background) {
  622. case 1:
  623. stats(bh);
  624. if (!(cd->intr_ls & ls_transmitter_buffer_empty)) {
  625. cd->command = c_stop;
  626. outw(dc_mask_sync_error | dc_no_stop_on_error |
  627. (inw(r_data_status) & 0x7f), r_data_control);
  628. cd->background = 2;
  629. break; /* we'd better not time-out here! */
  630. } else
  631. outw(c_stop, r_uart_transmit);
  632. /* fall into case 2: */
  633. case 2:
  634. /* the write has been satisfied by interrupt routine */
  635. cd->background = 3;
  636. break;
  637. case 3:
  638. if (cd->ur_r != cd->ur_w) {
  639. if (cd->ur[cd->ur_r] != c_stop) {
  640. debug(("cm206_bh: c_stop echoed 0x%x\n",
  641. cd->ur[cd->ur_r]));
  642. stats(echo);
  643. }
  644. cd->ur_r++;
  645. cd->ur_r %= UR_SIZE;
  646. }
  647. cd->background++;
  648. break;
  649. case 4:
  650. if (cd->ur_r != cd->ur_w) {
  651. if (cd->ur[cd->ur_r] != 0xff) {
  652. debug(("cm206_bh: c_stop reacted with 0x%x\n", cd->ur[cd->ur_r]));
  653. stats(stop_0xff);
  654. }
  655. cd->ur_r++;
  656. cd->ur_r %= UR_SIZE;
  657. }
  658. cd->background = 0;
  659. }
  660. }
  661. static DECLARE_TASKLET(cm206_tasklet, cm206_tasklet_func, 0);
  662. /* This command clears the dsb_possible_media_change flag, so we must
  663. * retain it.
  664. */
  665. static void get_drive_status(void)
  666. {
  667. uch status[2];
  668. type_1_command(c_drive_status, 2, status); /* this might be done faster */
  669. cd->dsb = status[0];
  670. cd->cc = status[1];
  671. cd->media_changed |=
  672. !!(cd->dsb & (dsb_possible_media_change |
  673. dsb_drive_not_ready | dsb_tray_not_closed));
  674. }
  675. static void get_disc_status(void)
  676. {
  677. if (type_1_command(c_disc_status, 7, cd->disc_status)) {
  678. debug(("get_disc_status: error\n"));
  679. }
  680. }
  681. /* The new open. The real opening strategy is defined in cdrom.c. */
  682. static int cm206_open(struct cdrom_device_info *cdi, int purpose)
  683. {
  684. if (!cd->openfiles) { /* reset only first time */
  685. cd->background = 0;
  686. reset_cm260();
  687. cd->adapter_last = -1; /* invalidate adapter memory */
  688. cd->sector_last = -1;
  689. }
  690. ++cd->openfiles;
  691. stats(open);
  692. return 0;
  693. }
  694. static void cm206_release(struct cdrom_device_info *cdi)
  695. {
  696. if (cd->openfiles == 1) {
  697. if (cd->background) {
  698. cd->background = 0;
  699. stop_read();
  700. }
  701. cd->sector_last = -1; /* Make our internal buffer invalid */
  702. FIRST_TRACK = 0; /* No valid disc status */
  703. }
  704. --cd->openfiles;
  705. }
  706. /* Empty buffer empties $sectors$ sectors of the adapter card buffer,
  707. * and then reads a sector in kernel memory. */
  708. static void empty_buffer(int sectors)
  709. {
  710. while (sectors >= 0) {
  711. transport_data(r_fifo_output_buffer,
  712. cd->sector + cd->fifo_overflowed,
  713. RAW_SECTOR_SIZE / 2 - cd->fifo_overflowed);
  714. --sectors;
  715. ++cd->adapter_first; /* update the current adapter sector */
  716. cd->fifo_overflowed = 0; /* reset overflow bit */
  717. stats(sector_transferred);
  718. }
  719. cd->sector_first = cd->adapter_first - 1;
  720. cd->sector_last = cd->adapter_first; /* update the buffer sector */
  721. }
  722. /* try_adapter. This function determines if the requested sector is
  723. in adapter memory, or will appear there soon. Returns 0 upon
  724. success */
  725. static int try_adapter(int sector)
  726. {
  727. if (cd->adapter_first <= sector && sector < cd->adapter_last) {
  728. /* sector is in adapter memory */
  729. empty_buffer(sector - cd->adapter_first);
  730. return 0;
  731. } else if (cd->background == 1 && cd->adapter_first <= sector
  732. && sector < cd->adapter_first + cd->max_sectors) {
  733. /* a read is going on, we can wait for it */
  734. cd->wait_back = 1;
  735. while (sector >= cd->adapter_last) {
  736. if (sleep_or_timeout(&cd->data, DATA_TIMEOUT)) {
  737. debug(("Timed out during background wait: %d %d %d %d\n", sector, cd->adapter_last, cd->adapter_first, cd->background));
  738. stats(back_read_timeout);
  739. cd->wait_back = 0;
  740. return -1;
  741. }
  742. }
  743. cd->wait_back = 0;
  744. empty_buffer(sector - cd->adapter_first);
  745. return 0;
  746. } else
  747. return -2;
  748. }
  749. /* This is not a very smart implementation. We could optimize for
  750. consecutive block numbers. I'm not convinced this would really
  751. bring down the processor load. */
  752. static void do_cm206_request(request_queue_t * q)
  753. {
  754. long int i, cd_sec_no;
  755. int quarter, error;
  756. uch *source, *dest;
  757. struct request *req;
  758. while (1) { /* repeat until all requests have been satisfied */
  759. req = elv_next_request(q);
  760. if (!req)
  761. return;
  762. if (req->cmd != READ) {
  763. debug(("Non-read command %d on cdrom\n", req->cmd));
  764. end_request(req, 0);
  765. continue;
  766. }
  767. spin_unlock_irq(q->queue_lock);
  768. error = 0;
  769. for (i = 0; i < req->nr_sectors; i++) {
  770. int e1, e2;
  771. cd_sec_no = (req->sector + i) / BLOCKS_ISO; /* 4 times 512 bytes */
  772. quarter = (req->sector + i) % BLOCKS_ISO;
  773. dest = req->buffer + i * LINUX_BLOCK_SIZE;
  774. /* is already in buffer memory? */
  775. if (cd->sector_first <= cd_sec_no
  776. && cd_sec_no < cd->sector_last) {
  777. source =
  778. ((uch *) cd->sector) + 16 +
  779. quarter * LINUX_BLOCK_SIZE +
  780. (cd_sec_no -
  781. cd->sector_first) * RAW_SECTOR_SIZE;
  782. memcpy(dest, source, LINUX_BLOCK_SIZE);
  783. } else if (!(e1 = try_adapter(cd_sec_no)) ||
  784. !(e2 = read_sector(cd_sec_no))) {
  785. source =
  786. ((uch *) cd->sector) + 16 +
  787. quarter * LINUX_BLOCK_SIZE;
  788. memcpy(dest, source, LINUX_BLOCK_SIZE);
  789. } else {
  790. error = 1;
  791. debug(("cm206_request: %d %d\n", e1, e2));
  792. }
  793. }
  794. spin_lock_irq(q->queue_lock);
  795. end_request(req, !error);
  796. }
  797. }
  798. /* Audio support. I've tried very hard, but the cm206 drive doesn't
  799. seem to have a get_toc (table-of-contents) function, while i'm
  800. pretty sure it must read the toc upon disc insertion. Therefore
  801. this function has been implemented through a binary search
  802. strategy. All track starts that happen to be found are stored in
  803. cd->toc[], for future use.
  804. I've spent a whole day on a bug that only shows under Workman---
  805. I don't get it. Tried everything, nothing works. If workman asks
  806. for track# 0xaa, it'll get the wrong time back. Any other program
  807. receives the correct value. I'm stymied.
  808. */
  809. /* seek seeks to address lba. It does wait to arrive there. */
  810. static void seek(int lba)
  811. {
  812. int i;
  813. uch seek_command[4] = { c_seek, };
  814. fsm(lba, &seek_command[1]);
  815. for (i = 0; i < 4; i++)
  816. type_0_command(seek_command[i], 0);
  817. cd->dsb = wait_dsb();
  818. }
  819. uch bcdbin(unsigned char bcd)
  820. { /* stolen from mcd.c! */
  821. return (bcd >> 4) * 10 + (bcd & 0xf);
  822. }
  823. static inline uch normalize_track(uch track)
  824. {
  825. if (track < 1)
  826. return 1;
  827. if (track > LAST_TRACK)
  828. return LAST_TRACK + 1;
  829. return track;
  830. }
  831. /* This function does a binary search for track start. It records all
  832. * tracks seen in the process. Input $track$ must be between 1 and
  833. * #-of-tracks+1. Note that the start of the disc must be in toc[1].fsm.
  834. */
  835. static int get_toc_lba(uch track)
  836. {
  837. int max = 74 * 60 * 75 - 150, min = fsm2lba(cd->toc[1].fsm);
  838. int i, lba, l, old_lba = 0;
  839. uch *q = cd->q;
  840. uch ct; /* current track */
  841. int binary = 0;
  842. const int skip = 3 * 60 * 75; /* 3 minutes */
  843. for (i = track; i > 0; i--)
  844. if (cd->toc[i].track) {
  845. min = fsm2lba(cd->toc[i].fsm);
  846. break;
  847. }
  848. lba = min + skip;
  849. do {
  850. seek(lba);
  851. type_1_command(c_read_current_q, 10, q);
  852. ct = normalize_track(q[1]);
  853. if (!cd->toc[ct].track) {
  854. l = q[9] - bcdbin(q[5]) + 75 * (q[8] -
  855. bcdbin(q[4]) - 2 +
  856. 60 * (q[7] -
  857. bcdbin(q
  858. [3])));
  859. cd->toc[ct].track = q[1]; /* lead out still 0xaa */
  860. fsm(l, cd->toc[ct].fsm);
  861. cd->toc[ct].q0 = q[0]; /* contains adr and ctrl info */
  862. if (ct == track)
  863. return l;
  864. }
  865. old_lba = lba;
  866. if (binary) {
  867. if (ct < track)
  868. min = lba;
  869. else
  870. max = lba;
  871. lba = (min + max) / 2;
  872. } else {
  873. if (ct < track)
  874. lba += skip;
  875. else {
  876. binary = 1;
  877. max = lba;
  878. min = lba - skip;
  879. lba = (min + max) / 2;
  880. }
  881. }
  882. } while (lba != old_lba);
  883. return lba;
  884. }
  885. static void update_toc_entry(uch track)
  886. {
  887. track = normalize_track(track);
  888. if (!cd->toc[track].track)
  889. get_toc_lba(track);
  890. }
  891. /* return 0 upon success */
  892. static int read_toc_header(struct cdrom_tochdr *hp)
  893. {
  894. if (!FIRST_TRACK)
  895. get_disc_status();
  896. if (hp) {
  897. int i;
  898. hp->cdth_trk0 = FIRST_TRACK;
  899. hp->cdth_trk1 = LAST_TRACK;
  900. /* fill in first track position */
  901. for (i = 0; i < 3; i++)
  902. cd->toc[1].fsm[i] = cd->disc_status[3 + i];
  903. update_toc_entry(LAST_TRACK + 1); /* find most entries */
  904. return 0;
  905. }
  906. return -1;
  907. }
  908. static void play_from_to_msf(struct cdrom_msf *msfp)
  909. {
  910. uch play_command[] = { c_play,
  911. msfp->cdmsf_frame0, msfp->cdmsf_sec0, msfp->cdmsf_min0,
  912. msfp->cdmsf_frame1, msfp->cdmsf_sec1, msfp->cdmsf_min1, 2,
  913. 2
  914. };
  915. int i;
  916. for (i = 0; i < 9; i++)
  917. type_0_command(play_command[i], 0);
  918. for (i = 0; i < 3; i++)
  919. PLAY_TO.fsm[i] = play_command[i + 4];
  920. PLAY_TO.track = 0; /* say no track end */
  921. cd->dsb = wait_dsb();
  922. }
  923. static void play_from_to_track(int from, int to)
  924. {
  925. uch play_command[8] = { c_play, };
  926. int i;
  927. if (from == 0) { /* continue paused play */
  928. for (i = 0; i < 3; i++) {
  929. play_command[i + 1] = cd->audio_status[i + 2];
  930. play_command[i + 4] = PLAY_TO.fsm[i];
  931. }
  932. } else {
  933. update_toc_entry(from);
  934. update_toc_entry(to + 1);
  935. for (i = 0; i < 3; i++) {
  936. play_command[i + 1] = cd->toc[from].fsm[i];
  937. PLAY_TO.fsm[i] = play_command[i + 4] =
  938. cd->toc[to + 1].fsm[i];
  939. }
  940. PLAY_TO.track = to;
  941. }
  942. for (i = 0; i < 7; i++)
  943. type_0_command(play_command[i], 0);
  944. for (i = 0; i < 2; i++)
  945. type_0_command(0x2, 0); /* volume */
  946. cd->dsb = wait_dsb();
  947. }
  948. static int get_current_q(struct cdrom_subchnl *qp)
  949. {
  950. int i;
  951. uch *q = cd->q;
  952. if (type_1_command(c_read_current_q, 10, q))
  953. return 0;
  954. /* q[0] = bcdbin(q[0]); Don't think so! */
  955. for (i = 2; i < 6; i++)
  956. q[i] = bcdbin(q[i]);
  957. qp->cdsc_adr = q[0] & 0xf;
  958. qp->cdsc_ctrl = q[0] >> 4; /* from mcd.c */
  959. qp->cdsc_trk = q[1];
  960. qp->cdsc_ind = q[2];
  961. if (qp->cdsc_format == CDROM_MSF) {
  962. qp->cdsc_reladdr.msf.minute = q[3];
  963. qp->cdsc_reladdr.msf.second = q[4];
  964. qp->cdsc_reladdr.msf.frame = q[5];
  965. qp->cdsc_absaddr.msf.minute = q[7];
  966. qp->cdsc_absaddr.msf.second = q[8];
  967. qp->cdsc_absaddr.msf.frame = q[9];
  968. } else {
  969. qp->cdsc_reladdr.lba = f_s_m2lba(q[5], q[4], q[3]);
  970. qp->cdsc_absaddr.lba = f_s_m2lba(q[9], q[8], q[7]);
  971. }
  972. get_drive_status();
  973. if (cd->dsb & dsb_play_in_progress)
  974. qp->cdsc_audiostatus = CDROM_AUDIO_PLAY;
  975. else if (PAUSED)
  976. qp->cdsc_audiostatus = CDROM_AUDIO_PAUSED;
  977. else
  978. qp->cdsc_audiostatus = CDROM_AUDIO_NO_STATUS;
  979. return 0;
  980. }
  981. static void invalidate_toc(void)
  982. {
  983. memset(cd->toc, 0, sizeof(cd->toc));
  984. memset(cd->disc_status, 0, sizeof(cd->disc_status));
  985. }
  986. /* cdrom.c guarantees that cdte_format == CDROM_MSF */
  987. static void get_toc_entry(struct cdrom_tocentry *ep)
  988. {
  989. uch track = normalize_track(ep->cdte_track);
  990. update_toc_entry(track);
  991. ep->cdte_addr.msf.frame = cd->toc[track].fsm[0];
  992. ep->cdte_addr.msf.second = cd->toc[track].fsm[1];
  993. ep->cdte_addr.msf.minute = cd->toc[track].fsm[2];
  994. ep->cdte_adr = cd->toc[track].q0 & 0xf;
  995. ep->cdte_ctrl = cd->toc[track].q0 >> 4;
  996. ep->cdte_datamode = 0;
  997. }
  998. /* Audio ioctl. Ioctl commands connected to audio are in such an
  999. * idiosyncratic i/o format, that we leave these untouched. Return 0
  1000. * upon success. Memory checking has been done by cdrom_ioctl(), the
  1001. * calling function, as well as LBA/MSF sanitization.
  1002. */
  1003. static int cm206_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
  1004. void *arg)
  1005. {
  1006. switch (cmd) {
  1007. case CDROMREADTOCHDR:
  1008. return read_toc_header((struct cdrom_tochdr *) arg);
  1009. case CDROMREADTOCENTRY:
  1010. get_toc_entry((struct cdrom_tocentry *) arg);
  1011. return 0;
  1012. case CDROMPLAYMSF:
  1013. play_from_to_msf((struct cdrom_msf *) arg);
  1014. return 0;
  1015. case CDROMPLAYTRKIND: /* admittedly, not particularly beautiful */
  1016. play_from_to_track(((struct cdrom_ti *) arg)->cdti_trk0,
  1017. ((struct cdrom_ti *) arg)->cdti_trk1);
  1018. return 0;
  1019. case CDROMSTOP:
  1020. PAUSED = 0;
  1021. if (cd->dsb & dsb_play_in_progress)
  1022. return type_0_command(c_stop, 1);
  1023. else
  1024. return 0;
  1025. case CDROMPAUSE:
  1026. get_drive_status();
  1027. if (cd->dsb & dsb_play_in_progress) {
  1028. type_0_command(c_stop, 1);
  1029. type_1_command(c_audio_status, 5,
  1030. cd->audio_status);
  1031. PAUSED = 1; /* say we're paused */
  1032. }
  1033. return 0;
  1034. case CDROMRESUME:
  1035. if (PAUSED)
  1036. play_from_to_track(0, 0);
  1037. PAUSED = 0;
  1038. return 0;
  1039. case CDROMSTART:
  1040. case CDROMVOLCTRL:
  1041. return 0;
  1042. case CDROMSUBCHNL:
  1043. return get_current_q((struct cdrom_subchnl *) arg);
  1044. default:
  1045. return -EINVAL;
  1046. }
  1047. }
  1048. static int cm206_media_changed(struct cdrom_device_info *cdi, int disc_nr)
  1049. {
  1050. if (cd != NULL) {
  1051. int r;
  1052. get_drive_status(); /* ensure cd->media_changed OK */
  1053. r = cd->media_changed;
  1054. cd->media_changed = 0; /* clear bit */
  1055. return r;
  1056. } else
  1057. return -EIO;
  1058. }
  1059. /* The new generic cdrom support. Routines should be concise, most of
  1060. the logic should be in cdrom.c */
  1061. /* controls tray movement */
  1062. static int cm206_tray_move(struct cdrom_device_info *cdi, int position)
  1063. {
  1064. if (position) { /* 1: eject */
  1065. type_0_command(c_open_tray, 1);
  1066. invalidate_toc();
  1067. } else
  1068. type_0_command(c_close_tray, 1); /* 0: close */
  1069. return 0;
  1070. }
  1071. /* gives current state of the drive */
  1072. static int cm206_drive_status(struct cdrom_device_info *cdi, int slot_nr)
  1073. {
  1074. get_drive_status();
  1075. if (cd->dsb & dsb_tray_not_closed)
  1076. return CDS_TRAY_OPEN;
  1077. if (!(cd->dsb & dsb_disc_present))
  1078. return CDS_NO_DISC;
  1079. if (cd->dsb & dsb_drive_not_ready)
  1080. return CDS_DRIVE_NOT_READY;
  1081. return CDS_DISC_OK;
  1082. }
  1083. /* locks or unlocks door lock==1: lock; return 0 upon success */
  1084. static int cm206_lock_door(struct cdrom_device_info *cdi, int lock)
  1085. {
  1086. uch command = (lock) ? c_lock_tray : c_unlock_tray;
  1087. type_0_command(command, 1); /* wait and get dsb */
  1088. /* the logic calculates the success, 0 means successful */
  1089. return lock ^ ((cd->dsb & dsb_tray_locked) != 0);
  1090. }
  1091. /* Although a session start should be in LBA format, we return it in
  1092. MSF format because it is slightly easier, and the new generic ioctl
  1093. will take care of the necessary conversion. */
  1094. static int cm206_get_last_session(struct cdrom_device_info *cdi,
  1095. struct cdrom_multisession *mssp)
  1096. {
  1097. if (!FIRST_TRACK)
  1098. get_disc_status();
  1099. if (mssp != NULL) {
  1100. if (DISC_STATUS & cds_multi_session) { /* multi-session */
  1101. mssp->addr.msf.frame = cd->disc_status[3];
  1102. mssp->addr.msf.second = cd->disc_status[4];
  1103. mssp->addr.msf.minute = cd->disc_status[5];
  1104. mssp->addr_format = CDROM_MSF;
  1105. mssp->xa_flag = 1;
  1106. } else {
  1107. mssp->xa_flag = 0;
  1108. }
  1109. return 1;
  1110. }
  1111. return 0;
  1112. }
  1113. static int cm206_get_upc(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
  1114. {
  1115. uch upc[10];
  1116. char *ret = mcn->medium_catalog_number;
  1117. int i;
  1118. if (type_1_command(c_read_upc, 10, upc))
  1119. return -EIO;
  1120. for (i = 0; i < 13; i++) {
  1121. int w = i / 2 + 1, r = i % 2;
  1122. if (r)
  1123. ret[i] = 0x30 | (upc[w] & 0x0f);
  1124. else
  1125. ret[i] = 0x30 | ((upc[w] >> 4) & 0x0f);
  1126. }
  1127. ret[13] = '\0';
  1128. return 0;
  1129. }
  1130. static int cm206_reset(struct cdrom_device_info *cdi)
  1131. {
  1132. stop_read();
  1133. reset_cm260();
  1134. outw(dc_normal | dc_break | READ_AHEAD, r_data_control);
  1135. mdelay(1); /* 750 musec minimum */
  1136. outw(dc_normal | READ_AHEAD, r_data_control);
  1137. cd->sector_last = -1; /* flag no data buffered */
  1138. cd->adapter_last = -1;
  1139. invalidate_toc();
  1140. return 0;
  1141. }
  1142. static int cm206_select_speed(struct cdrom_device_info *cdi, int speed)
  1143. {
  1144. int r;
  1145. switch (speed) {
  1146. case 0:
  1147. r = type_0_command(c_auto_mode, 1);
  1148. break;
  1149. case 1:
  1150. r = type_0_command(c_force_1x, 1);
  1151. break;
  1152. case 2:
  1153. r = type_0_command(c_force_2x, 1);
  1154. break;
  1155. default:
  1156. return -1;
  1157. }
  1158. if (r < 0)
  1159. return r;
  1160. else
  1161. return 1;
  1162. }
  1163. static struct cdrom_device_ops cm206_dops = {
  1164. .open = cm206_open,
  1165. .release = cm206_release,
  1166. .drive_status = cm206_drive_status,
  1167. .media_changed = cm206_media_changed,
  1168. .tray_move = cm206_tray_move,
  1169. .lock_door = cm206_lock_door,
  1170. .select_speed = cm206_select_speed,
  1171. .get_last_session = cm206_get_last_session,
  1172. .get_mcn = cm206_get_upc,
  1173. .reset = cm206_reset,
  1174. .audio_ioctl = cm206_audio_ioctl,
  1175. .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
  1176. CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
  1177. CDC_MCN | CDC_PLAY_AUDIO | CDC_SELECT_SPEED |
  1178. CDC_DRIVE_STATUS,
  1179. .n_minors = 1,
  1180. };
  1181. static struct cdrom_device_info cm206_info = {
  1182. .ops = &cm206_dops,
  1183. .speed = 2,
  1184. .capacity = 1,
  1185. .name = "cm206",
  1186. };
  1187. static int cm206_block_open(struct inode *inode, struct file *file)
  1188. {
  1189. return cdrom_open(&cm206_info, inode, file);
  1190. }
  1191. static int cm206_block_release(struct inode *inode, struct file *file)
  1192. {
  1193. return cdrom_release(&cm206_info, file);
  1194. }
  1195. static int cm206_block_ioctl(struct inode *inode, struct file *file,
  1196. unsigned cmd, unsigned long arg)
  1197. {
  1198. switch (cmd) {
  1199. #ifdef STATISTICS
  1200. case CM206CTL_GET_STAT:
  1201. if (arg >= NR_STATS)
  1202. return -EINVAL;
  1203. return cd->stats[arg];
  1204. case CM206CTL_GET_LAST_STAT:
  1205. if (arg >= NR_STATS)
  1206. return -EINVAL;
  1207. return cd->last_stat[arg];
  1208. #endif
  1209. default:
  1210. break;
  1211. }
  1212. return cdrom_ioctl(file, &cm206_info, inode, cmd, arg);
  1213. }
  1214. static int cm206_block_media_changed(struct gendisk *disk)
  1215. {
  1216. return cdrom_media_changed(&cm206_info);
  1217. }
  1218. static struct block_device_operations cm206_bdops =
  1219. {
  1220. .owner = THIS_MODULE,
  1221. .open = cm206_block_open,
  1222. .release = cm206_block_release,
  1223. .ioctl = cm206_block_ioctl,
  1224. .media_changed = cm206_block_media_changed,
  1225. };
  1226. static struct gendisk *cm206_gendisk;
  1227. /* This function probes for the adapter card. It returns the base
  1228. address if it has found the adapter card. One can specify a base
  1229. port to probe specifically, or 0 which means span all possible
  1230. bases.
  1231. Linus says it is too dangerous to use writes for probing, so we
  1232. stick with pure reads for a while. Hope that 8 possible ranges,
  1233. request_region, 15 bits of one port and 6 of another make things
  1234. likely enough to accept the region on the first hit...
  1235. */
  1236. static int __init probe_base_port(int base)
  1237. {
  1238. int b = 0x300, e = 0x370; /* this is the range of start addresses */
  1239. volatile int fool, i;
  1240. if (base)
  1241. b = e = base;
  1242. for (base = b; base <= e; base += 0x10) {
  1243. if (!request_region(base, 0x10,"cm206"))
  1244. continue;
  1245. for (i = 0; i < 3; i++)
  1246. fool = inw(base + 2); /* empty possibly uart_receive_buffer */
  1247. if ((inw(base + 6) & 0xffef) != 0x0001 || /* line_status */
  1248. (inw(base) & 0xad00) != 0) { /* data status */
  1249. release_region(base,0x10);
  1250. continue;
  1251. }
  1252. return (base);
  1253. }
  1254. return 0;
  1255. }
  1256. #if !defined(MODULE) || defined(AUTO_PROBE_MODULE)
  1257. /* Probe for irq# nr. If nr==0, probe for all possible irq's. */
  1258. static int __init probe_irq(int nr)
  1259. {
  1260. int irqs, irq;
  1261. outw(dc_normal | READ_AHEAD, r_data_control); /* disable irq-generation */
  1262. sti();
  1263. irqs = probe_irq_on();
  1264. reset_cm260(); /* causes interrupt */
  1265. udelay(100); /* wait for it */
  1266. irq = probe_irq_off(irqs);
  1267. outw(dc_normal | READ_AHEAD, r_data_control); /* services interrupt */
  1268. if (nr && irq != nr && irq > 0)
  1269. return 0; /* wrong interrupt happened */
  1270. else
  1271. return irq;
  1272. }
  1273. #endif
  1274. int __init cm206_init(void)
  1275. {
  1276. uch e = 0;
  1277. long int size = sizeof(struct cm206_struct);
  1278. struct gendisk *disk;
  1279. printk(KERN_INFO "cm206 cdrom driver " REVISION);
  1280. cm206_base = probe_base_port(auto_probe ? 0 : cm206_base);
  1281. if (!cm206_base) {
  1282. printk(" can't find adapter!\n");
  1283. return -EIO;
  1284. }
  1285. printk(" adapter at 0x%x", cm206_base);
  1286. cd = (struct cm206_struct *) kmalloc(size, GFP_KERNEL);
  1287. if (!cd)
  1288. goto out_base;
  1289. /* Now we have found the adaptor card, try to reset it. As we have
  1290. * found out earlier, this process generates an interrupt as well,
  1291. * so we might just exploit that fact for irq probing! */
  1292. #if !defined(MODULE) || defined(AUTO_PROBE_MODULE)
  1293. cm206_irq = probe_irq(auto_probe ? 0 : cm206_irq);
  1294. if (cm206_irq <= 0) {
  1295. printk("can't find IRQ!\n");
  1296. goto out_probe;
  1297. } else
  1298. printk(" IRQ %d found\n", cm206_irq);
  1299. #else
  1300. cli();
  1301. reset_cm260();
  1302. /* Now, the problem here is that reset_cm260 can generate an
  1303. interrupt. It seems that this can cause a kernel oops some time
  1304. later. So we wait a while and `service' this interrupt. */
  1305. mdelay(1);
  1306. outw(dc_normal | READ_AHEAD, r_data_control);
  1307. sti();
  1308. printk(" using IRQ %d\n", cm206_irq);
  1309. #endif
  1310. if (send_receive_polled(c_drive_configuration) !=
  1311. c_drive_configuration) {
  1312. printk(KERN_INFO " drive not there\n");
  1313. goto out_probe;
  1314. }
  1315. e = send_receive_polled(c_gimme);
  1316. printk(KERN_INFO "Firmware revision %d", e & dcf_revision_code);
  1317. if (e & dcf_transfer_rate)
  1318. printk(" double");
  1319. else
  1320. printk(" single");
  1321. printk(" speed drive");
  1322. if (e & dcf_motorized_tray)
  1323. printk(", motorized tray");
  1324. if (request_irq(cm206_irq, cm206_interrupt, 0, "cm206", NULL)) {
  1325. printk("\nUnable to reserve IRQ---aborted\n");
  1326. goto out_probe;
  1327. }
  1328. printk(".\n");
  1329. if (register_blkdev(MAJOR_NR, "cm206"))
  1330. goto out_blkdev;
  1331. disk = alloc_disk(1);
  1332. if (!disk)
  1333. goto out_disk;
  1334. disk->major = MAJOR_NR;
  1335. disk->first_minor = 0;
  1336. sprintf(disk->disk_name, "cm206cd");
  1337. disk->fops = &cm206_bdops;
  1338. disk->flags = GENHD_FL_CD;
  1339. cm206_gendisk = disk;
  1340. if (register_cdrom(&cm206_info) != 0) {
  1341. printk(KERN_INFO "Cannot register for cdrom %d!\n", MAJOR_NR);
  1342. goto out_cdrom;
  1343. }
  1344. cm206_queue = blk_init_queue(do_cm206_request, &cm206_lock);
  1345. if (!cm206_queue)
  1346. goto out_queue;
  1347. blk_queue_hardsect_size(cm206_queue, 2048);
  1348. disk->queue = cm206_queue;
  1349. add_disk(disk);
  1350. memset(cd, 0, sizeof(*cd)); /* give'm some reasonable value */
  1351. cd->sector_last = -1; /* flag no data buffered */
  1352. cd->adapter_last = -1;
  1353. init_timer(&cd->timer);
  1354. cd->timer.function = cm206_timeout;
  1355. cd->max_sectors = (inw(r_data_status) & ds_ram_size) ? 24 : 97;
  1356. printk(KERN_INFO "%d kB adapter memory available, "
  1357. " %ld bytes kernel memory used.\n", cd->max_sectors * 2,
  1358. size);
  1359. return 0;
  1360. out_queue:
  1361. unregister_cdrom(&cm206_info);
  1362. out_cdrom:
  1363. put_disk(disk);
  1364. out_disk:
  1365. unregister_blkdev(MAJOR_NR, "cm206");
  1366. out_blkdev:
  1367. free_irq(cm206_irq, NULL);
  1368. out_probe:
  1369. kfree(cd);
  1370. out_base:
  1371. release_region(cm206_base, 16);
  1372. return -EIO;
  1373. }
  1374. #ifdef MODULE
  1375. static void __init parse_options(void)
  1376. {
  1377. int i;
  1378. for (i = 0; i < 2; i++) {
  1379. if (0x300 <= cm206[i] && i <= 0x370
  1380. && cm206[i] % 0x10 == 0) {
  1381. cm206_base = cm206[i];
  1382. auto_probe = 0;
  1383. } else if (3 <= cm206[i] && cm206[i] <= 15) {
  1384. cm206_irq = cm206[i];
  1385. auto_probe = 0;
  1386. }
  1387. }
  1388. }
  1389. static int __cm206_init(void)
  1390. {
  1391. parse_options();
  1392. #if !defined(AUTO_PROBE_MODULE)
  1393. auto_probe = 0;
  1394. #endif
  1395. return cm206_init();
  1396. }
  1397. static void __exit cm206_exit(void)
  1398. {
  1399. del_gendisk(cm206_gendisk);
  1400. put_disk(cm206_gendisk);
  1401. if (unregister_cdrom(&cm206_info)) {
  1402. printk("Can't unregister cdrom cm206\n");
  1403. return;
  1404. }
  1405. if (unregister_blkdev(MAJOR_NR, "cm206")) {
  1406. printk("Can't unregister major cm206\n");
  1407. return;
  1408. }
  1409. blk_cleanup_queue(cm206_queue);
  1410. free_irq(cm206_irq, NULL);
  1411. kfree(cd);
  1412. release_region(cm206_base, 16);
  1413. printk(KERN_INFO "cm206 removed\n");
  1414. }
  1415. module_init(__cm206_init);
  1416. module_exit(cm206_exit);
  1417. #else /* !MODULE */
  1418. /* This setup function accepts either `auto' or numbers in the range
  1419. * 3--11 (for irq) or 0x300--0x370 (for base port) or both. */
  1420. static int __init cm206_setup(char *s)
  1421. {
  1422. int i, p[4];
  1423. (void) get_options(s, ARRAY_SIZE(p), p);
  1424. if (!strcmp(s, "auto"))
  1425. auto_probe = 1;
  1426. for (i = 1; i <= p[0]; i++) {
  1427. if (0x300 <= p[i] && i <= 0x370 && p[i] % 0x10 == 0) {
  1428. cm206_base = p[i];
  1429. auto_probe = 0;
  1430. } else if (3 <= p[i] && p[i] <= 15) {
  1431. cm206_irq = p[i];
  1432. auto_probe = 0;
  1433. }
  1434. }
  1435. return 1;
  1436. }
  1437. __setup("cm206=", cm206_setup);
  1438. #endif /* !MODULE */
  1439. MODULE_ALIAS_BLOCKDEV_MAJOR(CM206_CDROM_MAJOR);
  1440. /*
  1441. * Local variables:
  1442. * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -fno-strength-reduce -m486 -DMODULE -DMODVERSIONS -include /usr/src/linux/include/linux/modversions.h -c -o cm206.o cm206.c"
  1443. * End:
  1444. */