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