Import 2.1.36
[davej-history.git] / drivers / char / cyclades.c
blob0a1825ee8355409978c92be1f94753420ab4c668
1 static char rcsid[] =
2 "$Revision: 1.36.3.9 $$Date: 1996/10/07 19:47:13 $";
3 /*
4 * linux/drivers/char/cyclades.c
6 * This file contains the driver for the Cyclades Cyclom-Y multiport
7 * serial boards.
9 * Maintained by Marcio Saito (marcio@cyclades.com) and
10 * Randolph Bentson (bentson@grieg.seaslug.org)
12 * For Technical support and installation problems, please send e-mail
13 * to support@cyclades.com.
15 * Much of the design and some of the code came from serial.c
16 * which was copyright (C) 1991, 1992 Linus Torvalds. It was
17 * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
18 * and then fixed as suggested by Michael K. Johnson 12/12/92.
20 * This version does not support shared irq's.
22 * This module exports the following rs232 io functions:
23 * int cy_init(void);
24 * int cy_open(struct tty_struct *tty, struct file *filp);
26 * $Log: cyclades.c,v $
27 * Revision 1.36.3.9 1996/10/07 19:47:13 bentson
28 * add MOD_DEC_USE_COUNT in one return from cy_close (as
29 * noted by Jon Lewis <jlewis@INORGANIC5.FDT.NET>)
31 * Revision 1.36.3.8 1996/06/07 16:29:00 bentson
32 * starting minor number at zero; added missing verify_area
33 * as noted by Heiko Eissfeldt <heiko@colossus.escape.de>
35 * Revision 1.36.3.7 1996/04/19 21:06:18 bentson
36 * remove unneeded boot message & fix CLOCAL hardware flow
37 * control (Miquel van Smoorenburg <miquels@Q.cistron.nl>);
38 * remove unused diagnostic statements; minor 0 is first;
40 * Revision 1.36.3.6 1996/03/13 13:21:17 marcio
41 * The kernel function ioremap (available only in later 1.3.xx kernels)
42 * allows the access to memory addresses above the RAM. This revision
43 * of the driver supports PCI boards below 1Mb (device id 0x100) and
44 * above 1Mb (device id 0x101).
46 * Revision 1.36.3.5 1996/03/07 15:20:17 bentson
47 * Some global changes to interrupt handling spilled into
48 * this driver--mostly unused arguments in system function
49 * calls. Also added change by Marcio Saito which should
50 * reduce lost interrupts at startup by fast processors.
52 * Revision 1.36.3.4 1995/11/13 20:45:10 bentson
53 * Changes by Corey Minyard <minyard@wf-rch.cirr.com> distributed
54 * in 1.3.41 kernel to remove a possible race condition, extend
55 * some error messages, and let the driver run as a loadable module
56 * Change by Alan Wendt <alan@ez0.ezlink.com> to remove a
57 * possible race condition.
58 * Change by Marcio Saito <marcio@cyclades.com> to fix PCI addressing.
60 * Revision 1.36.3.3 1995/11/13 19:44:48 bentson
61 * Changes by Linus Torvalds in 1.3.33 kernel distribution
62 * required due to reordering of driver initialization.
63 * Drivers are now initialized *after* memory management.
65 * Revision 1.36.3.2 1995/09/08 22:07:14 bentson
66 * remove printk from ISR; fix typo
68 * Revision 1.36.3.1 1995/09/01 12:00:42 marcio
69 * Minor fixes in the PCI board support. PCI function calls in
70 * conditional compilation (CONFIG_PCI). Thanks to Jim Duncan
71 * <duncan@okay.com>. "bad serial count" message removed.
73 * Revision 1.36.3 1995/08/22 09:19:42 marcio
74 * Cyclom-Y/PCI support added. Changes in the cy_init routine and
75 * board initialization. Changes in the boot messages. The driver
76 * supports up to 4 boards and 64 ports by default.
78 * Revision 1.36.1.4 1995/03/29 06:14:14 bentson
79 * disambiguate between Cyclom-16Y and Cyclom-32Ye;
81 * Revision 1.36.1.3 1995/03/23 22:15:35 bentson
82 * add missing break in modem control block in ioctl switch statement
83 * (discovered by Michael Edward Chastain <mec@jobe.shell.portal.com>);
85 * Revision 1.36.1.2 1995/03/22 19:16:22 bentson
86 * make sure CTS flow control is set as soon as possible (thanks
87 * to note from David Lambert <lambert@chesapeake.rps.slb.com>);
89 * Revision 1.36.1.1 1995/03/13 15:44:43 bentson
90 * initialize defaults for receive threshold and stale data timeout;
91 * cosmetic changes;
93 * Revision 1.36 1995/03/10 23:33:53 bentson
94 * added support of chips 4-7 in 32 port Cyclom-Ye;
95 * fix cy_interrupt pointer dereference problem
96 * (Joe Portman <baron@aa.net>);
97 * give better error response if open is attempted on non-existent port
98 * (Zachariah Vaum <jchryslr@netcom.com>);
99 * correct command timeout (Kenneth Lerman <lerman@@seltd.newnet.com>);
100 * conditional compilation for -16Y on systems with fast, noisy bus;
101 * comment out diagnostic print function;
102 * cleaned up table of base addresses;
103 * set receiver time-out period register to correct value,
104 * set receive threshold to better default values,
105 * set chip timer to more accurate 200 Hz ticking,
106 * add code to monitor and modify receive parameters
107 * (Rik Faith <faith@cs.unc.edu> Nick Simicich
108 * <njs@scifi.emi.net>);
110 * Revision 1.35 1994/12/16 13:54:18 steffen
111 * additional patch by Marcio Saito for board detection
112 * Accidently left out in 1.34
114 * Revision 1.34 1994/12/10 12:37:12 steffen
115 * This is the corrected version as suggested by Marcio Saito
117 * Revision 1.33 1994/12/01 22:41:18 bentson
118 * add hooks to support more high speeds directly; add tytso
119 * patch regarding CLOCAL wakeups
121 * Revision 1.32 1994/11/23 19:50:04 bentson
122 * allow direct kernel control of higher signalling rates;
123 * look for cards at additional locations
125 * Revision 1.31 1994/11/16 04:33:28 bentson
126 * ANOTHER fix from Corey Minyard, minyard@wf-rch.cirr.com--
127 * a problem in chars_in_buffer has been resolved by some
128 * small changes; this should yield smoother output
130 * Revision 1.30 1994/11/16 04:28:05 bentson
131 * Fix from Corey Minyard, Internet: minyard@metronet.com,
132 * UUCP: minyard@wf-rch.cirr.com, WORK: minyardbnr.ca, to
133 * cy_hangup that appears to clear up much (all?) of the
134 * DTR glitches; also he's added/cleaned-up diagnostic messages
136 * Revision 1.29 1994/11/16 04:16:07 bentson
137 * add change proposed by Ralph Sims, ralphs@halcyon.com, to
138 * operate higher speeds in same way as other serial ports;
139 * add more serial ports (for up to two 16-port muxes).
141 * Revision 1.28 1994/11/04 00:13:16 root
142 * turn off diagnostic messages
144 * Revision 1.27 1994/11/03 23:46:37 root
145 * bunch of changes to bring driver into greater conformance
146 * with the serial.c driver (looking for missed fixes)
148 * Revision 1.26 1994/11/03 22:40:36 root
149 * automatic interrupt probing fixed.
151 * Revision 1.25 1994/11/03 20:17:02 root
152 * start to implement auto-irq
154 * Revision 1.24 1994/11/03 18:01:55 root
155 * still working on modem signals--trying not to drop DTR
156 * during the getty/login processes
158 * Revision 1.23 1994/11/03 17:51:36 root
159 * extend baud rate support; set receive threshold as function
160 * of baud rate; fix some problems with RTS/CTS;
162 * Revision 1.22 1994/11/02 18:05:35 root
163 * changed arguments to udelay to type long to get
164 * delays to be of correct duration
166 * Revision 1.21 1994/11/02 17:37:30 root
167 * employ udelay (after calibrating loops_per_second earlier
168 * in init/main.c) instead of using home-grown delay routines
170 * Revision 1.20 1994/11/02 03:11:38 root
171 * cy_chars_in_buffer forces a return value of 0 to let
172 * login work (don't know why it does); some functions
173 * that were returning EFAULT, now executes the code;
174 * more work on deciding when to disable xmit interrupts;
176 * Revision 1.19 1994/11/01 20:10:14 root
177 * define routine to start transmission interrupts (by enabling
178 * transmit interrupts); directly enable/disable modem interrupts;
180 * Revision 1.18 1994/11/01 18:40:45 bentson
181 * Don't always enable transmit interrupts in startup; interrupt on
182 * TxMpty instead of TxRdy to help characters get out before shutdown;
183 * restructure xmit interrupt to check for chars first and quit if
184 * none are ready to go; modem status (MXVRx) is upright, _not_ inverted
185 * (to my view);
187 * Revision 1.17 1994/10/30 04:39:45 bentson
188 * rename serial_driver and callout_driver to cy_serial_driver and
189 * cy_callout_driver to avoid linkage interference; initialize
190 * info->type to PORT_CIRRUS; ruggedize paranoia test; elide ->port
191 * from cyclades_port structure; add paranoia check to cy_close;
193 * Revision 1.16 1994/10/30 01:14:33 bentson
194 * change major numbers; add some _early_ return statements;
196 * Revision 1.15 1994/10/29 06:43:15 bentson
197 * final tidying up for clean compile; enable some error reporting
199 * Revision 1.14 1994/10/28 20:30:22 Bentson
200 * lots of changes to drag the driver towards the new tty_io
201 * structures and operation. not expected to work, but may
202 * compile cleanly.
204 * Revision 1.13 1994/07/21 23:08:57 Bentson
205 * add some diagnostic cruft; support 24 lines (for testing
206 * both -8Y and -16Y cards; be more thorough in servicing all
207 * chips during interrupt; add "volatile" a few places to
208 * circumvent compiler optimizations; fix base & offset
209 * computations in block_til_ready (was causing chip 0 to
210 * stop operation)
212 * Revision 1.12 1994/07/19 16:42:11 Bentson
213 * add some hackery for kernel version 1.1.8; expand
214 * error messages; refine timing for delay loops and
215 * declare loop params volatile
217 * Revision 1.11 1994/06/11 21:53:10 bentson
218 * get use of save_car right in transmit interrupt service
220 * Revision 1.10.1.1 1994/06/11 21:31:18 bentson
221 * add some diagnostic printing; try to fix save_car stuff
223 * Revision 1.10 1994/06/11 20:36:08 bentson
224 * clean up compiler warnings
226 * Revision 1.9 1994/06/11 19:42:46 bentson
227 * added a bunch of code to support modem signalling
229 * Revision 1.8 1994/06/11 17:57:07 bentson
230 * recognize break & parity error
232 * Revision 1.7 1994/06/05 05:51:34 bentson
233 * Reorder baud table to be monotonic; add cli to CP; discard
234 * incoming characters and status if the line isn't open; start to
235 * fold code into cy_throttle; start to port get_serial_info,
236 * set_serial_info, get_modem_info, set_modem_info, and send_break
237 * from serial.c; expand cy_ioctl; relocate and expand config_setup;
238 * get flow control characters from tty struct; invalidate ports w/o
239 * hardware;
241 * Revision 1.6 1994/05/31 18:42:21 bentson
242 * add a loop-breaker in the interrupt service routine;
243 * note when port is initialized so that it can be shut
244 * down under the right conditions; receive works without
245 * any obvious errors
247 * Revision 1.5 1994/05/30 00:55:02 bentson
248 * transmit works without obvious errors
250 * Revision 1.4 1994/05/27 18:46:27 bentson
251 * incorporated more code from lib_y.c; can now print short
252 * strings under interrupt control to port zero; seems to
253 * select ports/channels/lines correctly
255 * Revision 1.3 1994/05/25 22:12:44 bentson
256 * shifting from multi-port on a card to proper multiplexor
257 * data structures; added skeletons of most routines
259 * Revision 1.2 1994/05/19 13:21:43 bentson
260 * start to crib from other sources
264 #include <linux/module.h>
266 #include <linux/errno.h>
267 #include <linux/signal.h>
268 #include <linux/sched.h>
269 #include <linux/timer.h>
270 #include <linux/tty.h>
271 #include <linux/serial.h>
272 #include <linux/interrupt.h>
273 #include <linux/string.h>
274 #include <linux/fcntl.h>
275 #include <linux/ptrace.h>
276 #include <linux/cyclades.h>
277 #include <linux/delay.h>
278 #include <linux/major.h>
279 #include <linux/mm.h>
281 #include <asm/system.h>
282 #include <asm/io.h>
283 #include <asm/uaccess.h>
284 #include <asm/bitops.h>
286 #include <linux/config.h>
287 #include <linux/types.h>
288 #include <linux/kernel.h>
289 #include <linux/bios32.h>
290 #include <linux/pci.h>
291 #include <linux/init.h>
293 #define small_delay(x) for(j=0;j<x;j++)k++;
296 #define SERIAL_PARANOIA_CHECK
297 #undef SERIAL_DEBUG_OPEN
298 #undef SERIAL_DEBUG_THROTTLE
299 #undef SERIAL_DEBUG_OTHER
300 #undef SERIAL_DEBUG_IO
301 #undef SERIAL_DEBUG_COUNT
302 #undef SERIAL_DEBUG_DTR
303 #undef CYCLOM_16Y_HACK
304 #undef CYCLOM_ENABLE_MONITORING
306 #ifndef MIN
307 #define MIN(a,b) ((a) < (b) ? (a) : (b))
308 #endif
310 #define WAKEUP_CHARS 256
312 #define STD_COM_FLAGS (0)
314 #define SERIAL_TYPE_NORMAL 1
315 #define SERIAL_TYPE_CALLOUT 2
318 DECLARE_TASK_QUEUE(tq_cyclades);
320 struct tty_driver cy_serial_driver, cy_callout_driver;
322 static volatile int cy_irq_triggered;
323 static volatile int cy_triggered;
324 static int cy_wild_int_mask;
325 static unsigned char *intr_base_addr;
328 /* This is the address lockup table. The driver will probe for Cyclom-Y/ISA
329 boards at all addresses in here. If you want the driver to probe addresses
330 in a different address, add it to this table.
331 If the driver is probing some other board and causing problems, remove the
332 address from this table. */
334 static unsigned char *cy_isa_addresses[] = {
335 (unsigned char *) 0xD0000,
336 (unsigned char *) 0xD2000,
337 (unsigned char *) 0xD4000,
338 (unsigned char *) 0xD6000,
339 (unsigned char *) 0xD8000,
340 (unsigned char *) 0xDA000,
341 (unsigned char *) 0xDC000,
342 (unsigned char *) 0xDE000,
344 #define NR_ISA_ADDRESSES (sizeof(cy_isa_addresses)/sizeof(unsigned char *))
346 /* This is the per-card data structure containing address, irq, number of
347 channels, etc. This driver supports a maximum of NR_CARDS cards. If
348 you need to install more boards, change this constant in the definition
349 below. No other change is necessary to support more boards. */
351 #define NR_CARDS 4
353 static struct cyclades_card cy_card[NR_CARDS];
355 /* This is the per-channel data structure containing pointers, flags
356 and variables for the port. This driver supports a maximum of NR_PORTS.
357 If the total number of ports is larger than NR_PORTS, change this
358 constant in the definition below. No other change is necessary to
359 support more boards/ports. */
361 #define NR_PORTS 64
363 static struct cyclades_port cy_port[NR_PORTS];
365 /* The Cyclom-Ye has placed the sequential chips in non-sequential
366 * address order. This look-up table overcomes that problem.
368 static int cy_chip_offset [] =
369 { 0x0000,
370 0x0400,
371 0x0800,
372 0x0C00,
373 0x0200,
374 0x0600,
375 0x0A00,
376 0x0E00
379 /* PCI related definitions */
381 static unsigned short cy_pci_nboard = 0;
382 static unsigned short cy_isa_nboard = 0;
383 static unsigned short cy_nboard = 0;
384 static unsigned short cy_pci_dev_id[] = {
385 PCI_DEVICE_ID_CYCLOM_Y_Lo,/* PCI below 1Mb */
386 PCI_DEVICE_ID_CYCLOM_Y_Hi,/* PCI above 1Mb */
387 0 /* end of table */
390 int cy_detect_isa(void);
391 int cy_detect_pci(void);
393 static int cy_next_channel = 0; /* next minor available */
395 static int serial_refcount;
397 static struct tty_struct *serial_table[NR_PORTS];
398 static struct termios *serial_termios[NR_PORTS];
399 static struct termios *serial_termios_locked[NR_PORTS];
401 /* This is the per-irq data structure,
402 it maps an irq to the corresponding card */
404 struct cyclades_card *IRQ_cards[16];
408 * tmp_buf is used as a temporary buffer by serial_write. We need to
409 * lock it in case the copy_from_user blocks while swapping in a page,
410 * and some other program tries to do a serial write at the same time.
411 * Since the lock will only come under contention when the system is
412 * swapping and available memory is low, it makes sense to share one
413 * buffer across all the serial ports, since it significantly saves
414 * memory if large numbers of serial ports are open.
416 static unsigned char *tmp_buf = 0;
417 static struct semaphore tmp_buf_sem = MUTEX;
420 * This is used to look up the divisor speeds and the timeouts
421 * We're normally limited to 15 distinct baud rates. The extra
422 * are accessed via settings in info->flags.
423 * 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
424 * 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
425 * HI VHI
427 static int baud_table[] = {
428 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
429 1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800,115200,150000,
432 static char baud_co[] = { /* 25 MHz clock option table */
433 /* value => 00 01 02 03 04 */
434 /* divide by 8 32 128 512 2048 */
435 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
436 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
438 static char baud_bpr[] = { /* 25 MHz baud rate period table */
439 0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
440 0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15};
442 static char baud_cor3[] = { /* receive threshold */
443 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
444 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07};
448 static void shutdown(struct cyclades_port *);
449 static int startup (struct cyclades_port *);
450 static void cy_throttle(struct tty_struct *);
451 static void cy_unthrottle(struct tty_struct *);
452 static void config_setup(struct cyclades_port *);
453 #ifdef CYCLOM_SHOW_STATUS
454 static void show_status(int);
455 #endif
458 static inline int
459 serial_paranoia_check(struct cyclades_port *info,
460 kdev_t device, const char *routine)
462 #ifdef SERIAL_PARANOIA_CHECK
463 static const char *badmagic =
464 "Warning: bad magic number for serial struct (%s) in %s\n";
465 static const char *badinfo =
466 "Warning: null cyclades_port for (%s) in %s\n";
467 static const char *badrange =
468 "Warning: cyclades_port out of range for (%s) in %s\n";
470 if (!info) {
471 printk(badinfo, kdevname(device), routine);
472 return 1;
475 if( (long)info < (long)(&cy_port[0])
476 || (long)(&cy_port[NR_PORTS]) < (long)info ){
477 printk(badrange, kdevname(device), routine);
478 return 1;
481 if (info->magic != CYCLADES_MAGIC) {
482 printk(badmagic, kdevname(device), routine);
483 return 1;
485 #endif
486 return 0;
487 } /* serial_paranoia_check */
489 /* The following diagnostic routines allow the driver to spew
490 information on the screen, even (especially!) during interrupts.
492 void
493 SP(char *data){
494 unsigned long flags;
495 save_flags(flags); cli();
496 console_print(data);
497 restore_flags(flags);
499 void
500 CP(char data){
501 unsigned long flags;
502 char scrn[2];
503 save_flags(flags); cli();
504 scrn[0] = data;
505 scrn[1] = '\0';
506 console_print(scrn);
507 restore_flags(flags);
508 }/* CP */
510 void CP1(int data) { (data<10)? CP(data+'0'): CP(data+'A'-10); }/* CP1 */
511 void CP2(int data) { CP1((data>>4) & 0x0f); CP1( data & 0x0f); }/* CP2 */
512 void CP4(int data) { CP2((data>>8) & 0xff); CP2(data & 0xff); }/* CP4 */
513 void CP8(long data) { CP4((data>>16) & 0xffff); CP4(data & 0xffff); }/* CP8 */
515 /* This routine waits up to 1000 micro-seconds for the previous
516 command to the Cirrus chip to complete and then issues the
517 new command. An error is returned if the previous command
518 didn't finish within the time limit.
520 u_short
521 write_cy_cmd(u_char *base_addr, u_char cmd, int index)
523 unsigned long flags;
524 volatile int i;
526 save_flags(flags); cli();
527 /* Check to see that the previous command has completed */
528 for(i = 0 ; i < 100 ; i++){
529 if (base_addr[CyCCR<<index] == 0){
530 break;
532 udelay(10L);
534 /* if the CCR never cleared, the previous command
535 didn't finish within the "reasonable time" */
536 if ( i == 100 ) {
537 restore_flags(flags);
538 return (-1);
541 /* Issue the new command */
542 base_addr[CyCCR<<index] = cmd;
543 restore_flags(flags);
544 return(0);
545 } /* write_cy_cmd */
548 /* cy_start and cy_stop provide software output flow control as a
549 function of XON/XOFF, software CTS, and other such stuff. */
551 static void
552 cy_stop(struct tty_struct *tty)
554 struct cyclades_card *cinfo;
555 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
556 unsigned char *base_addr;
557 int chip,channel,index;
558 unsigned long flags;
560 #ifdef SERIAL_DEBUG_OTHER
561 printk("cy_stop ttyC%d\n", info->line); /* */
562 #endif
564 if (serial_paranoia_check(info, tty->device, "cy_stop"))
565 return;
567 cinfo = &cy_card[info->card];
568 index = cinfo->bus_index;
569 channel = info->line - cinfo->first_line;
570 chip = channel>>2;
571 channel &= 0x03;
572 base_addr = (unsigned char*)
573 (cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index));
575 save_flags(flags); cli();
576 base_addr[CyCAR<<index] = (u_char)(channel & 0x0003); /* index channel */
577 base_addr[CySRER<<index] &= ~CyTxMpty;
578 restore_flags(flags);
580 return;
581 } /* cy_stop */
583 static void
584 cy_start(struct tty_struct *tty)
586 struct cyclades_card *cinfo;
587 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
588 unsigned char *base_addr;
589 int chip,channel,index;
590 unsigned long flags;
592 #ifdef SERIAL_DEBUG_OTHER
593 printk("cy_start ttyC%d\n", info->line); /* */
594 #endif
596 if (serial_paranoia_check(info, tty->device, "cy_start"))
597 return;
599 cinfo = &cy_card[info->card];
600 index = cinfo->bus_index;
601 channel = info->line - cinfo->first_line;
602 chip = channel>>2;
603 channel &= 0x03;
604 base_addr = (unsigned char*)
605 (cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index));
607 save_flags(flags); cli();
608 base_addr[CyCAR<<index] = (u_char)(channel & 0x0003);
609 base_addr[CySRER<<index] |= CyTxMpty;
610 restore_flags(flags);
612 return;
613 } /* cy_start */
617 * This routine is used by the interrupt handler to schedule
618 * processing in the software interrupt portion of the driver
619 * (also known as the "bottom half"). This can be called any
620 * number of times for any channel without harm.
622 static inline void
623 cy_sched_event(struct cyclades_port *info, int event)
625 info->event |= 1 << event; /* remember what kind of event and who */
626 queue_task_irq_off(&info->tqueue, &tq_cyclades); /* it belongs to */
627 mark_bh(CYCLADES_BH); /* then trigger event */
628 } /* cy_sched_event */
631 static int probe_ready;
634 * This interrupt routine is used
635 * while we are probing for submarines.
637 static void
638 cy_probe(int irq, void *dev_id, struct pt_regs *regs)
640 int save_xir, save_car;
641 int index = 0; /* probing interrupts is only for ISA */
643 if (!probe_ready) {
644 *(intr_base_addr + (Cy_ClrIntr<<index)) = 0;
645 return;
648 cy_irq_triggered = irq;
649 cy_triggered |= 1 << irq;
651 if(intr_base_addr[CySVRR<<index] != 0) {
652 save_xir = (u_char) intr_base_addr[CyTIR<<index];
653 save_car = intr_base_addr[CyCAR<<index];
654 if ((save_xir & 0x3) != 0){
655 SP("channel ");
656 CP2(save_xir);
657 SP(" requesting unexpected interrupt\n");
659 intr_base_addr[CyCAR<<index] = (save_xir & 0x3);
660 intr_base_addr[CySRER<<index] &= ~CyTxMpty;
661 intr_base_addr[CyTIR<<index] = (save_xir & 0x3f);
662 intr_base_addr[CyCAR<<index] = (save_car);
664 *(intr_base_addr + (Cy_ClrIntr<<index)) = 0; /* Cy_ClrIntr is 0x1800 */
665 return;
666 } /* cy_probe */
668 /* The real interrupt service routine is called
669 whenever the card wants its hand held--chars
670 received, out buffer empty, modem change, etc.
672 static void
673 cy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
675 struct tty_struct *tty;
676 int status;
677 struct cyclades_card *cinfo;
678 struct cyclades_port *info;
679 volatile unsigned char *base_addr, *card_base_addr;
680 int chip;
681 int save_xir, channel, save_car;
682 char data;
683 int char_count;
684 int outch;
685 int i,j,index;
686 int too_many;
687 int had_work;
688 int mdm_change;
689 int mdm_status;
691 if((cinfo = IRQ_cards[irq]) == 0){
692 return; /* spurious interrupt */
695 card_base_addr = (unsigned char *)cinfo->base_addr;
696 index = cinfo->bus_index;
698 /* This loop checks all chips in the card. Make a note whenever
699 _any_ chip had some work to do, as this is considered an
700 indication that there will be more to do. Only when no chip
701 has any work does this outermost loop exit.
704 had_work = 0;
705 for ( chip = 0 ; chip < cinfo->num_chips ; chip ++) {
706 base_addr = (unsigned char *)
707 (cinfo->base_addr + (cy_chip_offset[chip]<<index));
708 too_many = 0;
709 while ( (status = base_addr[CySVRR<<index]) != 0x00) {
710 had_work++;
711 /* The purpose of the following test is to ensure that
712 no chip can monopolize the driver. This forces the
713 chips to be checked in a round-robin fashion (after
714 draining each of a bunch (1000) of characters).
716 if(1000<too_many++){
717 break;
719 if (status & CySRReceive) { /* reception interrupt */
720 /* determine the channel and change to that context */
721 save_xir = (u_char) base_addr[CyRIR<<index];
722 channel = (u_short ) (save_xir & CyIRChannel);
723 i = channel + chip * 4 + cinfo->first_line;
724 info = &cy_port[i];
725 info->last_active = jiffies;
726 save_car = base_addr[CyCAR<<index];
727 base_addr[CyCAR<<index] = save_xir;
729 /* if there is nowhere to put the data, discard it */
730 if(info->tty == 0){
731 j = (base_addr[CyRIVR<<index] & CyIVRMask);
732 if ( j == CyIVRRxEx ) { /* exception */
733 data = base_addr[CyRDSR<<index];
734 } else { /* normal character reception */
735 char_count = base_addr[CyRDCR<<index];
736 while(char_count--){
737 data = base_addr[CyRDSR<<index];
740 }else{ /* there is an open port for this data */
741 tty = info->tty;
742 j = (base_addr[CyRIVR<<index] & CyIVRMask);
743 if ( j == CyIVRRxEx ) { /* exception */
744 data = base_addr[CyRDSR<<index];
745 if(data & info->ignore_status_mask){
746 continue;
748 if (tty->flip.count < TTY_FLIPBUF_SIZE){
749 tty->flip.count++;
750 if (data & info->read_status_mask){
751 if(data & CyBREAK){
752 *tty->flip.flag_buf_ptr++ =
753 TTY_BREAK;
754 *tty->flip.char_buf_ptr++ =
755 base_addr[CyRDSR<<index];
756 if (info->flags & ASYNC_SAK){
757 do_SAK(tty);
759 }else if(data & CyFRAME){
760 *tty->flip.flag_buf_ptr++ =
761 TTY_FRAME;
762 *tty->flip.char_buf_ptr++ =
763 base_addr[CyRDSR<<index];
764 }else if(data & CyPARITY){
765 *tty->flip.flag_buf_ptr++ =
766 TTY_PARITY;
767 *tty->flip.char_buf_ptr++ =
768 base_addr[CyRDSR<<index];
769 }else if(data & CyOVERRUN){
770 *tty->flip.flag_buf_ptr++ =
771 TTY_OVERRUN;
772 *tty->flip.char_buf_ptr++ = 0;
773 /* If the flip buffer itself is
774 overflowing, we still loose
775 the next incoming character.
777 if(tty->flip.count < TTY_FLIPBUF_SIZE){
778 tty->flip.count++;
779 *tty->flip.flag_buf_ptr++ =
780 TTY_NORMAL;
781 *tty->flip.char_buf_ptr++ =
782 base_addr[CyRDSR<<index];
784 /* These two conditions may imply */
785 /* a normal read should be done. */
786 /* }else if(data & CyTIMEOUT){ */
787 /* }else if(data & CySPECHAR){ */
788 }else{
789 *tty->flip.flag_buf_ptr++ = 0;
790 *tty->flip.char_buf_ptr++ = 0;
792 }else{
793 *tty->flip.flag_buf_ptr++ = 0;
794 *tty->flip.char_buf_ptr++ = 0;
796 }else{
797 /* there was a software buffer overrun
798 and nothing could be done about it!!! */
800 } else { /* normal character reception */
801 /* load # characters available from the chip */
802 char_count = base_addr[CyRDCR<<index];
804 #ifdef CYCLOM_ENABLE_MONITORING
805 ++info->mon.int_count;
806 info->mon.char_count += char_count;
807 if (char_count > info->mon.char_max)
808 info->mon.char_max = char_count;
809 info->mon.char_last = char_count;
810 #endif
811 while(char_count--){
812 if (tty->flip.count >= TTY_FLIPBUF_SIZE){
813 break;
815 tty->flip.count++;
816 data = base_addr[CyRDSR<<index];
817 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
818 *tty->flip.char_buf_ptr++ = data;
819 #ifdef CYCLOM_16Y_HACK
820 udelay(10L);
821 #endif
824 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
826 /* end of service */
827 base_addr[CyRIR<<index] = (save_xir & 0x3f);
828 base_addr[CyCAR<<index] = (save_car);
832 if (status & CySRTransmit) { /* transmission interrupt */
833 /* Since we only get here when the transmit buffer is empty,
834 we know we can always stuff a dozen characters. */
836 /* determine the channel and change to that context */
837 save_xir = (u_char) base_addr[CyTIR<<index];
838 channel = (u_short ) (save_xir & CyIRChannel);
839 i = channel + chip * 4 + cinfo->first_line;
840 save_car = base_addr[CyCAR<<index];
841 base_addr[CyCAR<<index] = save_xir;
843 /* validate the port number (as configured and open) */
844 if( (i < 0) || (NR_PORTS <= i) ){
845 base_addr[CySRER<<index] &= ~CyTxMpty;
846 goto txend;
848 info = &cy_port[i];
849 info->last_active = jiffies;
850 if(info->tty == 0){
851 base_addr[CySRER<<index] &= ~CyTxMpty;
852 goto txdone;
855 /* load the on-chip space available for outbound data */
856 char_count = info->xmit_fifo_size;
859 if(info->x_char) { /* send special char */
860 outch = info->x_char;
861 base_addr[CyTDR<<index] = outch;
862 char_count--;
863 info->x_char = 0;
866 if (info->x_break){
867 /* The Cirrus chip requires the "Embedded Transmit
868 Commands" of start break, delay, and end break
869 sequences to be sent. The duration of the
870 break is given in TICs, which runs at HZ
871 (typically 100) and the PPR runs at 200 Hz,
872 so the delay is duration * 200/HZ, and thus a
873 break can run from 1/100 sec to about 5/4 sec.
875 base_addr[CyTDR<<index] = 0; /* start break */
876 base_addr[CyTDR<<index] = 0x81;
877 base_addr[CyTDR<<index] = 0; /* delay a bit */
878 base_addr[CyTDR<<index] = 0x82;
879 base_addr[CyTDR<<index] = info->x_break*200/HZ;
880 base_addr[CyTDR<<index] = 0; /* terminate break */
881 base_addr[CyTDR<<index] = 0x83;
882 char_count -= 7;
883 info->x_break = 0;
886 while (char_count-- > 0){
887 if (!info->xmit_cnt){
888 base_addr[CySRER<<index] &= ~CyTxMpty;
889 goto txdone;
891 if (info->xmit_buf == 0){
892 base_addr[CySRER<<index] &= ~CyTxMpty;
893 goto txdone;
895 if (info->tty->stopped || info->tty->hw_stopped){
896 base_addr[CySRER<<index] &= ~CyTxMpty;
897 goto txdone;
899 /* Because the Embedded Transmit Commands have been
900 enabled, we must check to see if the escape
901 character, NULL, is being sent. If it is, we
902 must ensure that there is room for it to be
903 doubled in the output stream. Therefore we
904 no longer advance the pointer when the character
905 is fetched, but rather wait until after the check
906 for a NULL output character. (This is necessary
907 because there may not be room for the two chars
908 needed to send a NULL.
910 outch = info->xmit_buf[info->xmit_tail];
911 if( outch ){
912 info->xmit_cnt--;
913 info->xmit_tail = (info->xmit_tail + 1)
914 & (PAGE_SIZE - 1);
915 base_addr[CyTDR<<index] = outch;
916 }else{
917 if(char_count > 1){
918 info->xmit_cnt--;
919 info->xmit_tail = (info->xmit_tail + 1)
920 & (PAGE_SIZE - 1);
921 base_addr[CyTDR<<index] = outch;
922 base_addr[CyTDR<<index] = 0;
923 char_count--;
924 }else{
929 txdone:
930 if (info->xmit_cnt < WAKEUP_CHARS) {
931 cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
934 txend:
935 /* end of service */
936 base_addr[CyTIR<<index] = (save_xir & 0x3f);
937 base_addr[CyCAR<<index] = (save_car);
940 if (status & CySRModem) { /* modem interrupt */
942 /* determine the channel and change to that context */
943 save_xir = (u_char) base_addr[CyMIR<<index];
944 channel = (u_short ) (save_xir & CyIRChannel);
945 info = &cy_port[channel + chip * 4 + cinfo->first_line];
946 info->last_active = jiffies;
947 save_car = base_addr[CyCAR<<index];
948 base_addr[CyCAR<<index] = save_xir;
950 mdm_change = base_addr[CyMISR<<index];
951 mdm_status = base_addr[CyMSVR1<<index];
953 if(info->tty == 0){ /* nowhere to put the data, ignore it */
955 }else{
956 if((mdm_change & CyDCD)
957 && (info->flags & ASYNC_CHECK_CD)){
958 if(mdm_status & CyDCD){
959 cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
960 }else if(!((info->flags & ASYNC_CALLOUT_ACTIVE)
961 &&(info->flags & ASYNC_CALLOUT_NOHUP))){
962 cy_sched_event(info, Cy_EVENT_HANGUP);
965 if((mdm_change & CyCTS)
966 && (info->flags & ASYNC_CTS_FLOW)){
967 if(info->tty->hw_stopped){
968 if(mdm_status & CyCTS){
969 /* !!! cy_start isn't used because... */
970 info->tty->hw_stopped = 0;
971 base_addr[CySRER<<index] |= CyTxMpty;
972 cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
974 }else{
975 if(!(mdm_status & CyCTS)){
976 /* !!! cy_stop isn't used because... */
977 info->tty->hw_stopped = 1;
978 base_addr[CySRER<<index] &= ~CyTxMpty;
982 if(mdm_status & CyDSR){
984 if(mdm_status & CyRI){
987 /* end of service */
988 base_addr[CyMIR<<index] = (save_xir & 0x3f);
989 base_addr[CyCAR<<index] = save_car;
991 } /* end while status != 0 */
992 } /* end loop for chips... */
993 } while(had_work);
995 /* clear interrupts */
996 *(card_base_addr + (Cy_ClrIntr<<index)) = 0; /* Cy_ClrIntr is 0x1800 */
998 } /* cy_interrupt */
1001 * This routine is used to handle the "bottom half" processing for the
1002 * serial driver, known also the "software interrupt" processing.
1003 * This processing is done at the kernel interrupt level, after the
1004 * cy_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
1005 * is where time-consuming activities which can not be done in the
1006 * interrupt driver proper are done; the interrupt driver schedules
1007 * them using cy_sched_event(), and they get done here.
1009 * This is done through one level of indirection--the task queue.
1010 * When a hardware interrupt service routine wants service by the
1011 * driver's bottom half, it enqueues the appropriate tq_struct (one
1012 * per port) to the tq_cyclades work queue and sets a request flag
1013 * via mark_bh for processing that queue. When the time is right,
1014 * do_cyclades_bh is called (because of the mark_bh) and it requests
1015 * that the work queue be processed.
1017 * Although this may seem unwieldy, it gives the system a way to
1018 * pass an argument (in this case the pointer to the cyclades_port
1019 * structure) to the bottom half of the driver. Previous kernels
1020 * had to poll every port to see if that port needed servicing.
1022 static void
1023 do_cyclades_bh(void)
1025 run_task_queue(&tq_cyclades);
1026 } /* do_cyclades_bh */
1028 static void
1029 do_softint(void *private_)
1031 struct cyclades_port *info = (struct cyclades_port *) private_;
1032 struct tty_struct *tty;
1034 tty = info->tty;
1035 if (!tty)
1036 return;
1038 if (clear_bit(Cy_EVENT_HANGUP, &info->event)) {
1039 tty_hangup(info->tty);
1040 wake_up_interruptible(&info->open_wait);
1041 info->flags &= ~(ASYNC_NORMAL_ACTIVE|
1042 ASYNC_CALLOUT_ACTIVE);
1044 if (clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
1045 wake_up_interruptible(&info->open_wait);
1047 if (clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
1048 if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP))
1049 && tty->ldisc.write_wakeup){
1050 (tty->ldisc.write_wakeup)(tty);
1052 wake_up_interruptible(&tty->write_wait);
1054 } /* do_softint */
1058 * Grab all interrupts in preparation for doing an automatic irq
1059 * detection. dontgrab is a mask of irq's _not_ to grab. Returns a
1060 * mask of irq's which were grabbed and should therefore be freed
1061 * using free_all_interrupts().
1063 static int
1064 grab_all_interrupts(int dontgrab)
1066 int irq_lines = 0;
1067 int i, mask;
1069 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1070 if (!(mask & dontgrab)
1071 && !request_irq(i, cy_probe, SA_INTERRUPT, "serial probe", NULL)) {
1072 irq_lines |= mask;
1075 return irq_lines;
1076 } /* grab_all_interrupts */
1079 * Release all interrupts grabbed by grab_all_interrupts
1081 static void
1082 free_all_interrupts(int irq_lines)
1084 int i;
1086 for (i = 0; i < 16; i++) {
1087 if (irq_lines & (1 << i))
1088 free_irq(i,NULL);
1090 } /* free_all_interrupts */
1093 * This routine returns a bitfield of "wild interrupts". Basically,
1094 * any unclaimed interrupts which is flapping around.
1096 static int
1097 check_wild_interrupts(void)
1099 int i, mask;
1100 int wild_interrupts = 0;
1101 int irq_lines;
1102 unsigned long timeout;
1103 unsigned long flags;
1105 /*Turn on interrupts (they may be off) */
1106 save_flags(flags); sti();
1108 irq_lines = grab_all_interrupts(0);
1111 * Delay for 0.1 seconds -- we use a busy loop since this may
1112 * occur during the bootup sequence
1114 timeout = jiffies+10;
1115 while (timeout >= jiffies)
1118 cy_triggered = 0; /* Reset after letting things settle */
1120 timeout = jiffies+10;
1121 while (timeout >= jiffies)
1124 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1125 if ((cy_triggered & (1 << i)) &&
1126 (irq_lines & (1 << i))) {
1127 wild_interrupts |= mask;
1130 free_all_interrupts(irq_lines);
1131 restore_flags(flags);
1132 return wild_interrupts;
1133 } /* check_wild_interrupts */
1136 * This routine is called by do_auto_irq(); it attempts to determine
1137 * which interrupt a serial port is configured to use. It is not
1138 * fool-proof, but it works a large part of the time.
1140 static int
1141 get_auto_irq(unsigned char *address)
1143 unsigned long timeout;
1144 unsigned char *base_addr;
1145 int index;
1147 index = 0; /* IRQ probing is only for ISA */
1148 base_addr = address;
1149 intr_base_addr = address;
1152 * Enable interrupts and see who answers
1154 cy_irq_triggered = 0;
1155 cli();
1156 base_addr[CyCAR<<index] = 0;
1157 write_cy_cmd(base_addr,CyCHAN_CTL|CyENB_XMTR,index);
1158 base_addr[CySRER<<index] |= CyTxMpty;
1159 probe_ready = 1;
1160 sti();
1162 timeout = jiffies+2;
1163 while (timeout >= jiffies) {
1164 if (cy_irq_triggered)
1165 break;
1167 probe_ready = 0;
1168 return(cy_irq_triggered);
1169 } /* get_auto_irq */
1172 * Calls get_auto_irq() multiple times, to make sure we don't get
1173 * faked out by random interrupts
1175 static int
1176 do_auto_irq(unsigned char *address)
1178 int irq_lines = 0;
1179 int irq_try_1 = 0, irq_try_2 = 0;
1180 int retries;
1181 unsigned long flags;
1183 /* Turn on interrupts (they may be off) */
1184 save_flags(flags); sti();
1186 probe_ready = 0;
1188 cy_wild_int_mask = check_wild_interrupts();
1190 irq_lines = grab_all_interrupts(cy_wild_int_mask);
1192 for (retries = 0; retries < 5; retries++) {
1193 if (!irq_try_1)
1194 irq_try_1 = get_auto_irq(address);
1195 if (!irq_try_2)
1196 irq_try_2 = get_auto_irq(address);
1197 if (irq_try_1 && irq_try_2) {
1198 if (irq_try_1 == irq_try_2)
1199 break;
1200 irq_try_1 = irq_try_2 = 0;
1203 restore_flags(flags);
1204 free_all_interrupts(irq_lines);
1205 return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
1206 } /* do_auto_irq */
1209 /* This is called whenever a port becomes active;
1210 interrupts are enabled and DTR & RTS are turned on.
1212 static int
1213 startup(struct cyclades_port * info)
1215 unsigned long flags;
1216 unsigned char *base_addr;
1217 int card,chip,channel,index;
1219 if (info->flags & ASYNC_INITIALIZED){
1220 return 0;
1223 if (!info->type){
1224 if (info->tty){
1225 set_bit(TTY_IO_ERROR, &info->tty->flags);
1227 return 0;
1229 if (!info->xmit_buf){
1230 info->xmit_buf = (unsigned char *) get_free_page (GFP_KERNEL);
1231 if (!info->xmit_buf){
1232 return -ENOMEM;
1236 config_setup(info);
1238 card = info->card;
1239 channel = (info->line) - (cy_card[card].first_line);
1240 chip = channel>>2;
1241 channel &= 0x03;
1242 index = cy_card[card].bus_index;
1243 base_addr = (unsigned char*)
1244 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1246 #ifdef SERIAL_DEBUG_OPEN
1247 printk("startup card %d, chip %d, channel %d, base_addr %lx",
1248 card, chip, channel, (long)base_addr);/**/
1249 #endif
1251 save_flags(flags); cli();
1252 base_addr[CyCAR<<index] = (u_char)channel;
1254 base_addr[CyRTPR<<index] = (info->default_timeout
1255 ? info->default_timeout
1256 : 0x02); /* 10ms rx timeout */
1258 write_cy_cmd(base_addr,CyCHAN_CTL|CyENB_RCVR|CyENB_XMTR,index);
1260 base_addr[CyCAR<<index] = (u_char)channel; /* !!! Is this needed? */
1261 base_addr[CyMSVR1<<index] = CyRTS;
1262 base_addr[CyMSVR2<<index] = CyDTR;
1264 #ifdef SERIAL_DEBUG_DTR
1265 printk("cyc: %d: raising DTR\n", __LINE__);
1266 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1267 #endif
1269 base_addr[CySRER<<index] |= CyRxData;
1270 info->flags |= ASYNC_INITIALIZED;
1272 if (info->tty){
1273 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1275 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1277 restore_flags(flags);
1279 #ifdef SERIAL_DEBUG_OPEN
1280 printk(" done\n");
1281 #endif
1282 return 0;
1283 } /* startup */
1285 void
1286 start_xmit( struct cyclades_port *info )
1288 unsigned long flags;
1289 unsigned char *base_addr;
1290 int card,chip,channel,index;
1292 card = info->card;
1293 channel = (info->line) - (cy_card[card].first_line);
1294 chip = channel>>2;
1295 channel &= 0x03;
1296 index = cy_card[card].bus_index;
1297 base_addr = (unsigned char*)
1298 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1300 save_flags(flags); cli();
1301 base_addr[CyCAR<<index] = channel;
1302 base_addr[CySRER<<index] |= CyTxMpty;
1303 restore_flags(flags);
1304 } /* start_xmit */
1307 * This routine shuts down a serial port; interrupts are disabled,
1308 * and DTR is dropped if the hangup on close termio flag is on.
1310 static void
1311 shutdown(struct cyclades_port * info)
1313 unsigned long flags;
1314 unsigned char *base_addr;
1315 int card,chip,channel,index;
1317 if (!(info->flags & ASYNC_INITIALIZED)){
1318 return;
1321 card = info->card;
1322 channel = info->line - cy_card[card].first_line;
1323 chip = channel>>2;
1324 channel &= 0x03;
1325 index = cy_card[card].bus_index;
1326 base_addr = (unsigned char*)
1327 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1329 #ifdef SERIAL_DEBUG_OPEN
1330 printk("shutdown card %d, chip %d, channel %d, base_addr %lx\n",
1331 card, chip, channel, (long)base_addr);
1332 #endif
1334 /* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
1335 SENT BEFORE DROPPING THE LINE !!! (Perhaps
1336 set some flag that is read when XMTY happens.)
1337 Other choices are to delay some fixed interval
1338 or schedule some later processing.
1340 save_flags(flags); cli();
1341 if (info->xmit_buf){
1342 unsigned char * temp;
1343 temp = info->xmit_buf;
1344 info->xmit_buf = 0;
1345 free_page((unsigned long) temp);
1348 base_addr[CyCAR<<index] = (u_char)channel;
1349 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1350 base_addr[CyMSVR1<<index] = ~CyRTS;
1351 base_addr[CyMSVR2<<index] = ~CyDTR;
1352 #ifdef SERIAL_DEBUG_DTR
1353 printk("cyc: %d: dropping DTR\n", __LINE__);
1354 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1355 #endif
1357 write_cy_cmd(base_addr,CyCHAN_CTL|CyDIS_RCVR,index);
1358 /* it may be appropriate to clear _XMIT at
1359 some later date (after testing)!!! */
1361 if (info->tty){
1362 set_bit(TTY_IO_ERROR, &info->tty->flags);
1364 info->flags &= ~ASYNC_INITIALIZED;
1365 restore_flags(flags);
1367 #ifdef SERIAL_DEBUG_OPEN
1368 printk(" done\n");
1369 #endif
1370 return;
1371 } /* shutdown */
1374 * This routine finds or computes the various line characteristics.
1376 static void
1377 config_setup(struct cyclades_port * info)
1379 unsigned long flags;
1380 unsigned char *base_addr;
1381 int card,chip,channel,index;
1382 unsigned cflag;
1383 int i;
1385 if (!info->tty || !info->tty->termios){
1386 return;
1388 if (info->line == -1){
1389 return;
1391 cflag = info->tty->termios->c_cflag;
1393 /* baud rate */
1394 i = cflag & CBAUD;
1395 #ifdef CBAUDEX
1396 /* Starting with kernel 1.1.65, there is direct support for
1397 higher baud rates. The following code supports those
1398 changes. The conditional aspect allows this driver to be
1399 used for earlier as well as later kernel versions. (The
1400 mapping is slightly different from serial.c because there
1401 is still the possibility of supporting 75 kbit/sec with
1402 the Cyclades board.)
1404 if (i & CBAUDEX) {
1405 if (i == B57600)
1406 i = 16;
1407 else if(i == B115200)
1408 i = 18;
1409 #ifdef B78600
1410 else if(i == B78600)
1411 i = 17;
1412 #endif
1413 else
1414 info->tty->termios->c_cflag &= ~CBAUDEX;
1416 #endif
1417 if (i == 15) {
1418 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1419 i += 1;
1420 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1421 i += 3;
1423 info->tbpr = baud_bpr[i]; /* Tx BPR */
1424 info->tco = baud_co[i]; /* Tx CO */
1425 info->rbpr = baud_bpr[i]; /* Rx BPR */
1426 info->rco = baud_co[i]; /* Rx CO */
1427 if (baud_table[i] == 134) {
1428 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
1429 /* get it right for 134.5 baud */
1430 } else if (baud_table[i]) {
1431 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
1432 /* this needs to be propagated into the card info */
1433 } else {
1434 info->timeout = 0;
1436 /* By tradition (is it a standard?) a baud rate of zero
1437 implies the line should be/has been closed. A bit
1438 later in this routine such a test is performed. */
1440 /* byte size and parity */
1441 info->cor5 = 0;
1442 info->cor4 = 0;
1443 info->cor3 = (info->default_threshold
1444 ? info->default_threshold
1445 : baud_cor3[i]); /* receive threshold */
1446 info->cor2 = CyETC;
1447 switch(cflag & CSIZE){
1448 case CS5:
1449 info->cor1 = Cy_5_BITS;
1450 break;
1451 case CS6:
1452 info->cor1 = Cy_6_BITS;
1453 break;
1454 case CS7:
1455 info->cor1 = Cy_7_BITS;
1456 break;
1457 case CS8:
1458 info->cor1 = Cy_8_BITS;
1459 break;
1461 if(cflag & CSTOPB){
1462 info->cor1 |= Cy_2_STOP;
1464 if (cflag & PARENB){
1465 if (cflag & PARODD){
1466 info->cor1 |= CyPARITY_O;
1467 }else{
1468 info->cor1 |= CyPARITY_E;
1470 }else{
1471 info->cor1 |= CyPARITY_NONE;
1474 /* CTS flow control flag */
1475 if (cflag & CRTSCTS){
1476 info->flags |= ASYNC_CTS_FLOW;
1477 info->cor2 |= CyCtsAE;
1478 }else{
1479 info->flags &= ~ASYNC_CTS_FLOW;
1480 info->cor2 &= ~CyCtsAE;
1482 if (cflag & CLOCAL)
1483 info->flags &= ~ASYNC_CHECK_CD;
1484 else
1485 info->flags |= ASYNC_CHECK_CD;
1487 /***********************************************
1488 The hardware option, CyRtsAO, presents RTS when
1489 the chip has characters to send. Since most modems
1490 use RTS as reverse (inbound) flow control, this
1491 option is not used. If inbound flow control is
1492 necessary, DTR can be programmed to provide the
1493 appropriate signals for use with a non-standard
1494 cable. Contact Marcio Saito for details.
1495 ***********************************************/
1497 card = info->card;
1498 channel = (info->line) - (cy_card[card].first_line);
1499 chip = channel>>2;
1500 channel &= 0x03;
1501 index = cy_card[card].bus_index;
1502 base_addr = (unsigned char*)
1503 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1505 save_flags(flags); cli();
1506 base_addr[CyCAR<<index] = (u_char)channel;
1508 /* tx and rx baud rate */
1510 base_addr[CyTCOR<<index] = info->tco;
1511 base_addr[CyTBPR<<index] = info->tbpr;
1512 base_addr[CyRCOR<<index] = info->rco;
1513 base_addr[CyRBPR<<index] = info->rbpr;
1515 /* set line characteristics according configuration */
1517 base_addr[CySCHR1<<index] = START_CHAR(info->tty);
1518 base_addr[CySCHR2<<index] = STOP_CHAR(info->tty);
1519 base_addr[CyCOR1<<index] = info->cor1;
1520 base_addr[CyCOR2<<index] = info->cor2;
1521 base_addr[CyCOR3<<index] = info->cor3;
1522 base_addr[CyCOR4<<index] = info->cor4;
1523 base_addr[CyCOR5<<index] = info->cor5;
1525 write_cy_cmd(base_addr,CyCOR_CHANGE|CyCOR1ch|CyCOR2ch|CyCOR3ch,index);
1527 base_addr[CyCAR<<index] = (u_char)channel; /* !!! Is this needed? */
1529 base_addr[CyRTPR<<index] = (info->default_timeout
1530 ? info->default_timeout
1531 : 0x02); /* 10ms rx timeout */
1533 if (C_CLOCAL(info->tty)) {
1534 base_addr[CySRER<<index] |= CyMdmCh; /* without modem intr */
1535 /* act on 1->0 modem transitions */
1536 base_addr[CyMCOR1<<index] = CyCTS;
1537 /* act on 0->1 modem transitions */
1538 base_addr[CyMCOR2<<index] = CyCTS;
1539 } else {
1540 base_addr[CySRER<<index] |= CyMdmCh; /* with modem intr */
1541 /* act on 1->0 modem transitions */
1542 base_addr[CyMCOR1<<index] = CyDSR|CyCTS|CyRI|CyDCD;
1543 /* act on 0->1 modem transitions */
1544 base_addr[CyMCOR2<<index] = CyDSR|CyCTS|CyRI|CyDCD;
1547 if(i == 0){ /* baud rate is zero, turn off line */
1548 base_addr[CyMSVR2<<index] = ~CyDTR;
1549 #ifdef SERIAL_DEBUG_DTR
1550 printk("cyc: %d: dropping DTR\n", __LINE__);
1551 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1552 #endif
1553 }else{
1554 base_addr[CyMSVR2<<index] = CyDTR;
1555 #ifdef SERIAL_DEBUG_DTR
1556 printk("cyc: %d: raising DTR\n", __LINE__);
1557 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1558 #endif
1561 if (info->tty){
1562 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1565 restore_flags(flags);
1567 } /* config_setup */
1570 static void
1571 cy_put_char(struct tty_struct *tty, unsigned char ch)
1573 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1574 unsigned long flags;
1576 #ifdef SERIAL_DEBUG_IO
1577 printk("cy_put_char ttyC%d\n", info->line);
1578 #endif
1580 if (serial_paranoia_check(info, tty->device, "cy_put_char"))
1581 return;
1583 if (!tty || !info->xmit_buf)
1584 return;
1586 save_flags(flags); cli();
1587 if (info->xmit_cnt >= PAGE_SIZE - 1) {
1588 restore_flags(flags);
1589 return;
1592 info->xmit_buf[info->xmit_head++] = ch;
1593 info->xmit_head &= PAGE_SIZE - 1;
1594 info->xmit_cnt++;
1595 restore_flags(flags);
1596 } /* cy_put_char */
1599 static void
1600 cy_flush_chars(struct tty_struct *tty)
1602 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1603 unsigned long flags;
1604 unsigned char *base_addr;
1605 int card,chip,channel,index;
1607 #ifdef SERIAL_DEBUG_IO
1608 printk("cy_flush_chars ttyC%d\n", info->line); /* */
1609 #endif
1611 if (serial_paranoia_check(info, tty->device, "cy_flush_chars"))
1612 return;
1614 if (info->xmit_cnt <= 0 || tty->stopped
1615 || tty->hw_stopped || !info->xmit_buf)
1616 return;
1618 card = info->card;
1619 channel = info->line - cy_card[card].first_line;
1620 chip = channel>>2;
1621 channel &= 0x03;
1622 index = cy_card[card].bus_index;
1623 base_addr = (unsigned char*)
1624 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1626 save_flags(flags); cli();
1627 base_addr[CyCAR<<index] = channel;
1628 base_addr[CySRER<<index] |= CyTxMpty;
1629 restore_flags(flags);
1630 } /* cy_flush_chars */
1633 /* This routine gets called when tty_write has put something into
1634 the write_queue. If the port is not already transmitting stuff,
1635 start it off by enabling interrupts. The interrupt service
1636 routine will then ensure that the characters are sent. If the
1637 port is already active, there is no need to kick it.
1639 static int
1640 cy_write(struct tty_struct * tty, int from_user,
1641 const unsigned char *buf, int count)
1643 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1644 unsigned long flags;
1645 int c, total = 0;
1647 #ifdef SERIAL_DEBUG_IO
1648 printk("cy_write ttyC%d\n", info->line); /* */
1649 #endif
1651 if (serial_paranoia_check(info, tty->device, "cy_write")){
1652 return 0;
1655 if (!tty || !info->xmit_buf || !tmp_buf){
1656 return 0;
1659 while (1) {
1660 save_flags(flags); cli();
1661 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1662 SERIAL_XMIT_SIZE - info->xmit_head));
1663 if (c <= 0){
1664 restore_flags(flags);
1665 break;
1668 if (from_user) {
1669 down(&tmp_buf_sem);
1670 copy_from_user(tmp_buf, buf, c);
1671 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1672 SERIAL_XMIT_SIZE - info->xmit_head));
1673 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1674 up(&tmp_buf_sem);
1675 } else
1676 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1677 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1678 info->xmit_cnt += c;
1679 restore_flags(flags);
1680 buf += c;
1681 count -= c;
1682 total += c;
1686 if (info->xmit_cnt
1687 && !tty->stopped
1688 && !tty->hw_stopped ) {
1689 start_xmit(info);
1691 return total;
1692 } /* cy_write */
1695 static int
1696 cy_write_room(struct tty_struct *tty)
1698 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1699 int ret;
1701 #ifdef SERIAL_DEBUG_IO
1702 printk("cy_write_room ttyC%d\n", info->line); /* */
1703 #endif
1705 if (serial_paranoia_check(info, tty->device, "cy_write_room"))
1706 return 0;
1707 ret = PAGE_SIZE - info->xmit_cnt - 1;
1708 if (ret < 0)
1709 ret = 0;
1710 return ret;
1711 } /* cy_write_room */
1714 static int
1715 cy_chars_in_buffer(struct tty_struct *tty)
1717 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1719 #ifdef SERIAL_DEBUG_IO
1720 printk("cy_chars_in_buffer ttyC%d %d\n", info->line, info->xmit_cnt); /* */
1721 #endif
1723 if (serial_paranoia_check(info, tty->device, "cy_chars_in_buffer"))
1724 return 0;
1726 return info->xmit_cnt;
1727 } /* cy_chars_in_buffer */
1730 static void
1731 cy_flush_buffer(struct tty_struct *tty)
1733 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1734 unsigned long flags;
1736 #ifdef SERIAL_DEBUG_IO
1737 printk("cy_flush_buffer ttyC%d\n", info->line); /* */
1738 #endif
1740 if (serial_paranoia_check(info, tty->device, "cy_flush_buffer"))
1741 return;
1742 save_flags(flags); cli();
1743 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1744 restore_flags(flags);
1745 wake_up_interruptible(&tty->write_wait);
1746 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
1747 && tty->ldisc.write_wakeup)
1748 (tty->ldisc.write_wakeup)(tty);
1749 } /* cy_flush_buffer */
1752 /* This routine is called by the upper-layer tty layer to signal
1753 that incoming characters should be throttled or that the
1754 throttle should be released.
1756 static void
1757 cy_throttle(struct tty_struct * tty)
1759 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1760 unsigned long flags;
1761 unsigned char *base_addr;
1762 int card,chip,channel,index;
1764 #ifdef SERIAL_DEBUG_THROTTLE
1765 char buf[64];
1767 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1768 tty->ldisc.chars_in_buffer(tty));
1769 printk("cy_throttle ttyC%d\n", info->line);
1770 #endif
1772 if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
1773 return;
1776 if (I_IXOFF(tty)) {
1777 info->x_char = STOP_CHAR(tty);
1778 /* Should use the "Send Special Character" feature!!! */
1781 card = info->card;
1782 channel = info->line - cy_card[card].first_line;
1783 chip = channel>>2;
1784 channel &= 0x03;
1785 index = cy_card[card].bus_index;
1786 base_addr = (unsigned char*)
1787 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1789 save_flags(flags); cli();
1790 base_addr[CyCAR<<index] = (u_char)channel;
1791 base_addr[CyMSVR1<<index] = ~CyRTS;
1792 restore_flags(flags);
1794 return;
1795 } /* cy_throttle */
1798 static void
1799 cy_unthrottle(struct tty_struct * tty)
1801 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1802 unsigned long flags;
1803 unsigned char *base_addr;
1804 int card,chip,channel,index;
1806 #ifdef SERIAL_DEBUG_THROTTLE
1807 char buf[64];
1809 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1810 tty->ldisc.chars_in_buffer(tty));
1811 printk("cy_unthrottle ttyC%d\n", info->line);
1812 #endif
1814 if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
1815 return;
1818 if (I_IXOFF(tty)) {
1819 info->x_char = START_CHAR(tty);
1820 /* Should use the "Send Special Character" feature!!! */
1823 card = info->card;
1824 channel = info->line - cy_card[card].first_line;
1825 chip = channel>>2;
1826 channel &= 0x03;
1827 index = cy_card[card].bus_index;
1828 base_addr = (unsigned char*)
1829 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1831 save_flags(flags); cli();
1832 base_addr[CyCAR<<index] = (u_char)channel;
1833 base_addr[CyMSVR1<<index] = CyRTS;
1834 restore_flags(flags);
1836 return;
1837 } /* cy_unthrottle */
1839 static int
1840 get_serial_info(struct cyclades_port * info,
1841 struct serial_struct * retinfo)
1843 struct serial_struct tmp;
1844 struct cyclades_card *cinfo = &cy_card[info->card];
1846 if (!retinfo)
1847 return -EFAULT;
1848 memset(&tmp, 0, sizeof(tmp));
1849 tmp.type = info->type;
1850 tmp.line = info->line;
1851 tmp.port = info->card * 0x100 + info->line - cinfo->first_line;
1852 tmp.irq = cinfo->irq;
1853 tmp.flags = info->flags;
1854 tmp.baud_base = 0; /*!!!*/
1855 tmp.close_delay = info->close_delay;
1856 tmp.custom_divisor = 0; /*!!!*/
1857 tmp.hub6 = 0; /*!!!*/
1858 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1859 return 0;
1860 } /* get_serial_info */
1862 static int
1863 set_serial_info(struct cyclades_port * info,
1864 struct serial_struct * new_info)
1866 struct serial_struct new_serial;
1867 struct cyclades_port old_info;
1869 if (!new_info)
1870 return -EFAULT;
1871 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1872 old_info = *info;
1874 if (!suser()) {
1875 if ((new_serial.close_delay != info->close_delay) ||
1876 ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
1877 (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
1878 return -EPERM;
1879 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1880 (new_serial.flags & ASYNC_USR_MASK));
1881 goto check_and_exit;
1886 * OK, past this point, all the error checking has been done.
1887 * At this point, we start making changes.....
1890 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1891 (new_serial.flags & ASYNC_FLAGS));
1892 info->close_delay = new_serial.close_delay;
1895 check_and_exit:
1896 if (info->flags & ASYNC_INITIALIZED){
1897 config_setup(info);
1898 return 0;
1899 }else{
1900 return startup(info);
1902 } /* set_serial_info */
1904 static int
1905 get_modem_info(struct cyclades_port * info, unsigned int *value)
1907 int card,chip,channel,index;
1908 unsigned char *base_addr;
1909 unsigned long flags;
1910 unsigned char status;
1911 unsigned int result;
1913 card = info->card;
1914 channel = (info->line) - (cy_card[card].first_line);
1915 chip = channel>>2;
1916 channel &= 0x03;
1917 index = cy_card[card].bus_index;
1918 base_addr = (unsigned char*)
1919 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1921 save_flags(flags); cli();
1922 base_addr[CyCAR<<index] = (u_char)channel;
1923 status = base_addr[CyMSVR1<<index];
1924 status |= base_addr[CyMSVR2<<index];
1925 restore_flags(flags);
1927 result = ((status & CyRTS) ? TIOCM_RTS : 0)
1928 | ((status & CyDTR) ? TIOCM_DTR : 0)
1929 | ((status & CyDCD) ? TIOCM_CAR : 0)
1930 | ((status & CyRI) ? TIOCM_RNG : 0)
1931 | ((status & CyDSR) ? TIOCM_DSR : 0)
1932 | ((status & CyCTS) ? TIOCM_CTS : 0);
1933 put_user(result,(unsigned int *) value);
1934 return 0;
1935 } /* get_modem_info */
1937 static int
1938 set_modem_info(struct cyclades_port * info, unsigned int cmd,
1939 unsigned int *value)
1941 int card,chip,channel,index;
1942 unsigned char *base_addr;
1943 unsigned long flags;
1944 unsigned int arg;
1945 int error;
1947 error = get_user(arg, value);
1948 if (error)
1949 return error;
1951 card = info->card;
1952 channel = (info->line) - (cy_card[card].first_line);
1953 chip = channel>>2;
1954 channel &= 0x03;
1955 index = cy_card[card].bus_index;
1956 base_addr = (unsigned char*)
1957 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
1959 switch (cmd) {
1960 case TIOCMBIS:
1961 if (arg & TIOCM_RTS){
1962 save_flags(flags); cli();
1963 base_addr[CyCAR<<index] = (u_char)channel;
1964 base_addr[CyMSVR1<<index] = CyRTS;
1965 restore_flags(flags);
1967 if (arg & TIOCM_DTR){
1968 save_flags(flags); cli();
1969 base_addr[CyCAR<<index] = (u_char)channel;
1970 base_addr[CyMSVR2<<index] = CyDTR;
1971 #ifdef SERIAL_DEBUG_DTR
1972 printk("cyc: %d: raising DTR\n", __LINE__);
1973 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1974 #endif
1975 restore_flags(flags);
1977 break;
1978 case TIOCMBIC:
1979 if (arg & TIOCM_RTS){
1980 save_flags(flags); cli();
1981 base_addr[CyCAR<<index] = (u_char)channel;
1982 base_addr[CyMSVR1<<index] = ~CyRTS;
1983 restore_flags(flags);
1985 if (arg & TIOCM_DTR){
1986 save_flags(flags); cli();
1987 base_addr[CyCAR<<index] = (u_char)channel;
1988 base_addr[CyMSVR2<<index] = ~CyDTR;
1989 #ifdef SERIAL_DEBUG_DTR
1990 printk("cyc: %d: dropping DTR\n", __LINE__);
1991 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
1992 #endif
1993 restore_flags(flags);
1995 break;
1996 case TIOCMSET:
1997 if (arg & TIOCM_RTS){
1998 save_flags(flags); cli();
1999 base_addr[CyCAR<<index] = (u_char)channel;
2000 base_addr[CyMSVR1<<index] = CyRTS;
2001 restore_flags(flags);
2002 }else{
2003 save_flags(flags); cli();
2004 base_addr[CyCAR<<index] = (u_char)channel;
2005 base_addr[CyMSVR1<<index] = ~CyRTS;
2006 restore_flags(flags);
2008 if (arg & TIOCM_DTR){
2009 save_flags(flags); cli();
2010 base_addr[CyCAR<<index] = (u_char)channel;
2011 base_addr[CyMSVR2<<index] = CyDTR;
2012 #ifdef SERIAL_DEBUG_DTR
2013 printk("cyc: %d: raising DTR\n", __LINE__);
2014 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
2015 #endif
2016 restore_flags(flags);
2017 }else{
2018 save_flags(flags); cli();
2019 base_addr[CyCAR<<index] = (u_char)channel;
2020 base_addr[CyMSVR2<<index] = ~CyDTR;
2021 #ifdef SERIAL_DEBUG_DTR
2022 printk("cyc: %d: dropping DTR\n", __LINE__);
2023 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
2024 #endif
2025 restore_flags(flags);
2027 break;
2028 default:
2029 return -EINVAL;
2031 return 0;
2032 } /* set_modem_info */
2034 static void
2035 send_break( struct cyclades_port * info, int duration)
2036 { /* Let the transmit ISR take care of this (since it
2037 requires stuffing characters into the output stream).
2039 info->x_break = duration;
2040 if (!info->xmit_cnt ) {
2041 start_xmit(info);
2043 } /* send_break */
2045 static int
2046 get_mon_info(struct cyclades_port * info, struct cyclades_monitor * mon)
2049 copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor));
2050 info->mon.int_count = 0;
2051 info->mon.char_count = 0;
2052 info->mon.char_max = 0;
2053 info->mon.char_last = 0;
2054 return 0;
2057 static int
2058 set_threshold(struct cyclades_port * info, unsigned long value)
2060 unsigned char *base_addr;
2061 int card,channel,chip,index;
2063 card = info->card;
2064 channel = info->line - cy_card[card].first_line;
2065 chip = channel>>2;
2066 channel &= 0x03;
2067 index = cy_card[card].bus_index;
2068 base_addr = (unsigned char*)
2069 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2071 info->cor3 &= ~CyREC_FIFO;
2072 info->cor3 |= value & CyREC_FIFO;
2073 base_addr[CyCOR3<<index] = info->cor3;
2074 write_cy_cmd(base_addr,CyCOR_CHANGE|CyCOR3ch,index);
2075 return 0;
2078 static int
2079 get_threshold(struct cyclades_port * info, unsigned long *value)
2081 unsigned char *base_addr;
2082 int card,channel,chip,index;
2083 unsigned long tmp;
2085 card = info->card;
2086 channel = info->line - cy_card[card].first_line;
2087 chip = channel>>2;
2088 channel &= 0x03;
2089 index = cy_card[card].bus_index;
2090 base_addr = (unsigned char*)
2091 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2093 tmp = base_addr[CyCOR3<<index] & CyREC_FIFO;
2094 put_user(tmp,value);
2095 return 0;
2098 static int
2099 set_default_threshold(struct cyclades_port * info, unsigned long value)
2101 info->default_threshold = value & 0x0f;
2102 return 0;
2105 static int
2106 get_default_threshold(struct cyclades_port * info, unsigned long *value)
2108 put_user(info->default_threshold,value);
2109 return 0;
2112 static int
2113 set_timeout(struct cyclades_port * info, unsigned long value)
2115 unsigned char *base_addr;
2116 int card,channel,chip,index;
2118 card = info->card;
2119 channel = info->line - cy_card[card].first_line;
2120 chip = channel>>2;
2121 channel &= 0x03;
2122 index = cy_card[card].bus_index;
2123 base_addr = (unsigned char*)
2124 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2126 base_addr[CyRTPR<<index] = value & 0xff;
2127 return 0;
2130 static int
2131 get_timeout(struct cyclades_port * info, unsigned long *value)
2133 unsigned char *base_addr;
2134 int card,channel,chip,index;
2135 unsigned long tmp;
2137 card = info->card;
2138 channel = info->line - cy_card[card].first_line;
2139 chip = channel>>2;
2140 channel &= 0x03;
2141 index = cy_card[card].bus_index;
2142 base_addr = (unsigned char*)
2143 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
2145 tmp = base_addr[CyRTPR<<index];
2146 put_user(tmp,value);
2147 return 0;
2150 static int
2151 set_default_timeout(struct cyclades_port * info, unsigned long value)
2153 info->default_timeout = value & 0xff;
2154 return 0;
2157 static int
2158 get_default_timeout(struct cyclades_port * info, unsigned long *value)
2160 put_user(info->default_timeout,value);
2161 return 0;
2164 static int
2165 cy_ioctl(struct tty_struct *tty, struct file * file,
2166 unsigned int cmd, unsigned long arg)
2168 int error;
2169 struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2170 int ret_val = 0;
2172 #ifdef SERIAL_DEBUG_OTHER
2173 printk("cy_ioctl ttyC%d, cmd = %x arg = %lx\n", info->line, cmd, arg); /* */
2174 #endif
2176 switch (cmd) {
2177 case CYGETMON:
2178 error = verify_area(VERIFY_WRITE, (void *) arg
2179 ,sizeof(struct cyclades_monitor));
2180 if (error){
2181 ret_val = error;
2182 break;
2184 ret_val = get_mon_info(info, (struct cyclades_monitor *)arg);
2185 break;
2186 case CYGETTHRESH:
2187 error = verify_area(VERIFY_WRITE, (void *) arg
2188 ,sizeof(unsigned long));
2189 if (error){
2190 ret_val = error;
2191 break;
2193 ret_val = get_threshold(info, (unsigned long *)arg);
2194 break;
2195 case CYSETTHRESH:
2196 ret_val = set_threshold(info, (unsigned long)arg);
2197 break;
2198 case CYGETDEFTHRESH:
2199 error = verify_area(VERIFY_WRITE, (void *) arg
2200 ,sizeof(unsigned long));
2201 if (error){
2202 ret_val = error;
2203 break;
2205 ret_val = get_default_threshold(info, (unsigned long *)arg);
2206 break;
2207 case CYSETDEFTHRESH:
2208 ret_val = set_default_threshold(info, (unsigned long)arg);
2209 break;
2210 case CYGETTIMEOUT:
2211 error = verify_area(VERIFY_WRITE, (void *) arg
2212 ,sizeof(unsigned long));
2213 if (error){
2214 ret_val = error;
2215 break;
2217 ret_val = get_timeout(info, (unsigned long *)arg);
2218 break;
2219 case CYSETTIMEOUT:
2220 ret_val = set_timeout(info, (unsigned long)arg);
2221 break;
2222 case CYGETDEFTIMEOUT:
2223 error = verify_area(VERIFY_WRITE, (void *) arg
2224 ,sizeof(unsigned long));
2225 if (error){
2226 ret_val = error;
2227 break;
2229 ret_val = get_default_timeout(info, (unsigned long *)arg);
2230 break;
2231 case CYSETDEFTIMEOUT:
2232 ret_val = set_default_timeout(info, (unsigned long)arg);
2233 break;
2234 case TCSBRK: /* SVID version: non-zero arg --> no break */
2235 ret_val = tty_check_change(tty);
2236 if (ret_val)
2237 return ret_val;
2238 tty_wait_until_sent(tty,0);
2239 if (!arg)
2240 send_break(info, HZ/4); /* 1/4 second */
2241 break;
2242 case TCSBRKP: /* support for POSIX tcsendbreak() */
2243 ret_val = tty_check_change(tty);
2244 if (ret_val)
2245 return ret_val;
2246 tty_wait_until_sent(tty,0);
2247 send_break(info, arg ? arg*(HZ/10) : HZ/4);
2248 break;
2249 case TIOCMBIS:
2250 case TIOCMBIC:
2251 case TIOCMSET:
2252 ret_val = set_modem_info(info, cmd, (unsigned int *) arg);
2253 break;
2255 /* The following commands are incompletely implemented!!! */
2256 case TIOCGSOFTCAR:
2257 ret_val = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned int *) arg);
2258 break;
2259 case TIOCSSOFTCAR:
2260 ret_val = get_user(arg,(unsigned int *) arg);
2261 if (ret_val)
2262 break;
2263 tty->termios->c_cflag =
2264 ((tty->termios->c_cflag & ~CLOCAL) |
2265 (arg ? CLOCAL : 0));
2266 break;
2267 case TIOCMGET:
2268 error = verify_area(VERIFY_WRITE, (void *) arg
2269 ,sizeof(unsigned int));
2270 if (error){
2271 ret_val = error;
2272 break;
2274 ret_val = get_modem_info(info, (unsigned int *) arg);
2275 break;
2276 case TIOCGSERIAL:
2277 error = verify_area(VERIFY_WRITE, (void *) arg
2278 ,sizeof(struct serial_struct));
2279 if (error){
2280 ret_val = error;
2281 break;
2283 ret_val = get_serial_info(info,
2284 (struct serial_struct *) arg);
2285 break;
2286 case TIOCSSERIAL:
2287 error = verify_area(VERIFY_READ, (void *) arg
2288 ,sizeof(struct serial_struct));
2289 if (error){
2290 ret_val = error;
2291 break;
2293 ret_val = set_serial_info(info,
2294 (struct serial_struct *) arg);
2295 break;
2296 default:
2297 ret_val = -ENOIOCTLCMD;
2300 #ifdef SERIAL_DEBUG_OTHER
2301 printk("cy_ioctl done\n");
2302 #endif
2304 return ret_val;
2305 } /* cy_ioctl */
2310 static void
2311 cy_set_termios(struct tty_struct *tty, struct termios * old_termios)
2313 struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2315 #ifdef SERIAL_DEBUG_OTHER
2316 printk("cy_set_termios ttyC%d\n", info->line);
2317 #endif
2319 if (tty->termios->c_cflag == old_termios->c_cflag)
2320 return;
2321 config_setup(info);
2323 if ((old_termios->c_cflag & CRTSCTS) &&
2324 !(tty->termios->c_cflag & CRTSCTS)) {
2325 tty->stopped = 0;
2326 cy_start(tty);
2328 #ifdef tytso_patch_94Nov25_1726
2329 if (!(old_termios->c_cflag & CLOCAL) &&
2330 (tty->termios->c_cflag & CLOCAL))
2331 wake_up_interruptible(&info->open_wait);
2332 #endif
2334 return;
2335 } /* cy_set_termios */
2338 static void
2339 cy_close(struct tty_struct * tty, struct file * filp)
2341 struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2342 unsigned long flags;
2344 #ifdef SERIAL_DEBUG_OTHER
2345 printk("cy_close ttyC%d\n", info->line);
2346 #endif
2348 if (!info
2349 || serial_paranoia_check(info, tty->device, "cy_close")){
2350 return;
2352 #ifdef SERIAL_DEBUG_OPEN
2353 printk("cy_close ttyC%d, count = %d\n", info->line, info->count);
2354 #endif
2356 save_flags(flags); cli();
2358 /* If the TTY is being hung up, nothing to do */
2359 if (tty_hung_up_p(filp)) {
2360 MOD_DEC_USE_COUNT;
2361 restore_flags(flags);
2362 return;
2365 if ((tty->count == 1) && (info->count != 1)) {
2367 * Uh, oh. tty->count is 1, which means that the tty
2368 * structure will be freed. Info->count should always
2369 * be one in these conditions. If it's greater than
2370 * one, we've got real problems, since it means the
2371 * serial port won't be shutdown.
2373 printk("cy_close: bad serial port count; tty->count is 1, "
2374 "info->count is %d\n", info->count);
2375 info->count = 1;
2377 #ifdef SERIAL_DEBUG_COUNT
2378 printk("cyc: %d(%d): decrementing count to %d\n", __LINE__, current->pid, info->count - 1);
2379 #endif
2380 if (--info->count < 0) {
2381 #ifdef SERIAL_DEBUG_COUNT
2382 printk("cyc: %d: setting count to 0\n", __LINE__);
2383 #endif
2384 info->count = 0;
2386 if (info->count)
2388 MOD_DEC_USE_COUNT;
2389 restore_flags(flags);
2390 return;
2392 info->flags |= ASYNC_CLOSING;
2394 * Save the termios structure, since this port may have
2395 * separate termios for callout and dialin.
2397 if (info->flags & ASYNC_NORMAL_ACTIVE)
2398 info->normal_termios = *tty->termios;
2399 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2400 info->callout_termios = *tty->termios;
2401 if (info->flags & ASYNC_INITIALIZED)
2402 tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
2403 shutdown(info);
2404 if (tty->driver.flush_buffer)
2405 tty->driver.flush_buffer(tty);
2406 if (tty->ldisc.flush_buffer)
2407 tty->ldisc.flush_buffer(tty);
2408 info->event = 0;
2409 info->tty = 0;
2410 if (info->blocked_open) {
2411 if (info->close_delay) {
2412 current->state = TASK_INTERRUPTIBLE;
2413 current->timeout = jiffies + info->close_delay;
2414 schedule();
2416 wake_up_interruptible(&info->open_wait);
2418 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2419 ASYNC_CLOSING);
2420 wake_up_interruptible(&info->close_wait);
2422 #ifdef SERIAL_DEBUG_OTHER
2423 printk("cy_close done\n");
2424 #endif
2426 MOD_DEC_USE_COUNT;
2427 restore_flags(flags);
2428 return;
2429 } /* cy_close */
2432 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
2434 void
2435 cy_hangup(struct tty_struct *tty)
2437 struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2439 #ifdef SERIAL_DEBUG_OTHER
2440 printk("cy_hangup ttyC%d\n", info->line); /* */
2441 #endif
2443 if (serial_paranoia_check(info, tty->device, "cy_hangup"))
2444 return;
2446 shutdown(info);
2447 info->event = 0;
2448 info->count = 0;
2449 #ifdef SERIAL_DEBUG_COUNT
2450 printk("cyc: %d(%d): setting count to 0\n", __LINE__, current->pid);
2451 #endif
2452 info->tty = 0;
2453 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2454 wake_up_interruptible(&info->open_wait);
2455 } /* cy_hangup */
2460 * ------------------------------------------------------------
2461 * cy_open() and friends
2462 * ------------------------------------------------------------
2465 static int
2466 block_til_ready(struct tty_struct *tty, struct file * filp,
2467 struct cyclades_port *info)
2469 struct wait_queue wait = { current, NULL };
2470 struct cyclades_card *cinfo;
2471 unsigned long flags;
2472 int chip, channel,index;
2473 int retval;
2474 char *base_addr;
2477 * If the device is in the middle of being closed, then block
2478 * until it's done, and then try again.
2480 if (info->flags & ASYNC_CLOSING) {
2481 interruptible_sleep_on(&info->close_wait);
2482 if (info->flags & ASYNC_HUP_NOTIFY){
2483 return -EAGAIN;
2484 }else{
2485 return -ERESTARTSYS;
2490 * If this is a callout device, then just make sure the normal
2491 * device isn't being used.
2493 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2494 if (info->flags & ASYNC_NORMAL_ACTIVE){
2495 return -EBUSY;
2497 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2498 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2499 (info->session != current->session)){
2500 return -EBUSY;
2502 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2503 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2504 (info->pgrp != current->pgrp)){
2505 return -EBUSY;
2507 info->flags |= ASYNC_CALLOUT_ACTIVE;
2508 return 0;
2512 * If non-blocking mode is set, then make the check up front
2513 * and then exit.
2515 if (filp->f_flags & O_NONBLOCK) {
2516 if (info->flags & ASYNC_CALLOUT_ACTIVE){
2517 return -EBUSY;
2519 info->flags |= ASYNC_NORMAL_ACTIVE;
2520 return 0;
2524 * Block waiting for the carrier detect and the line to become
2525 * free (i.e., not in use by the callout). While we are in
2526 * this loop, info->count is dropped by one, so that
2527 * cy_close() knows when to free things. We restore it upon
2528 * exit, either normal or abnormal.
2530 retval = 0;
2531 add_wait_queue(&info->open_wait, &wait);
2532 #ifdef SERIAL_DEBUG_OPEN
2533 printk("block_til_ready before block: ttyC%d, count = %d\n",
2534 info->line, info->count);/**/
2535 #endif
2536 info->count--;
2537 #ifdef SERIAL_DEBUG_COUNT
2538 printk("cyc: %d(%d): decrementing count to %d\n", __LINE__, current->pid, info->count);
2539 #endif
2540 info->blocked_open++;
2542 cinfo = &cy_card[info->card];
2543 channel = info->line - cinfo->first_line;
2544 chip = channel>>2;
2545 channel &= 0x03;
2546 index = cinfo->bus_index;
2547 base_addr = (char *) (cinfo->base_addr + (cy_chip_offset[chip]<<index));
2549 while (1) {
2550 save_flags(flags); cli();
2551 if (!(info->flags & ASYNC_CALLOUT_ACTIVE)){
2552 base_addr[CyCAR<<index] = (u_char)channel;
2553 base_addr[CyMSVR1<<index] = CyRTS;
2554 base_addr[CyMSVR2<<index] = CyDTR;
2555 #ifdef SERIAL_DEBUG_DTR
2556 printk("cyc: %d: raising DTR\n", __LINE__);
2557 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1<<index], base_addr[CyMSVR2<<index]);
2558 #endif
2560 restore_flags(flags);
2561 current->state = TASK_INTERRUPTIBLE;
2562 if (tty_hung_up_p(filp)
2563 || !(info->flags & ASYNC_INITIALIZED) ){
2564 if (info->flags & ASYNC_HUP_NOTIFY) {
2565 retval = -EAGAIN;
2566 }else{
2567 retval = -ERESTARTSYS;
2569 break;
2571 save_flags(flags); cli();
2572 base_addr[CyCAR<<index] = (u_char)channel;
2573 if (!(info->flags & ASYNC_CALLOUT_ACTIVE)
2574 && !(info->flags & ASYNC_CLOSING)
2575 && (C_CLOCAL(tty)
2576 || (base_addr[CyMSVR1<<index] & CyDCD))) {
2577 restore_flags(flags);
2578 break;
2580 restore_flags(flags);
2581 if (current->signal & ~current->blocked) {
2582 retval = -ERESTARTSYS;
2583 break;
2585 #ifdef SERIAL_DEBUG_OPEN
2586 printk("block_til_ready blocking: ttyC%d, count = %d\n",
2587 info->line, info->count);/**/
2588 #endif
2589 schedule();
2591 current->state = TASK_RUNNING;
2592 remove_wait_queue(&info->open_wait, &wait);
2593 if (!tty_hung_up_p(filp)){
2594 info->count++;
2595 #ifdef SERIAL_DEBUG_COUNT
2596 printk("cyc: %d(%d): incrementing count to %d\n", __LINE__, current->pid, info->count);
2597 #endif
2599 info->blocked_open--;
2600 #ifdef SERIAL_DEBUG_OPEN
2601 printk("block_til_ready after blocking: ttyC%d, count = %d\n",
2602 info->line, info->count);/**/
2603 #endif
2604 if (retval)
2605 return retval;
2606 info->flags |= ASYNC_NORMAL_ACTIVE;
2607 return 0;
2608 } /* block_til_ready */
2611 * This routine is called whenever a serial port is opened. It
2612 * performs the serial-specific initialization for the tty structure.
2615 cy_open(struct tty_struct *tty, struct file * filp)
2617 struct cyclades_port *info;
2618 int retval, line;
2620 line = MINOR(tty->device) - tty->driver.minor_start;
2621 if ((line < 0) || (NR_PORTS <= line)){
2622 return -ENODEV;
2624 info = &cy_port[line];
2625 if (info->line < 0){
2626 return -ENODEV;
2628 #ifdef SERIAL_DEBUG_OTHER
2629 printk("cy_open ttyC%d\n", info->line); /* */
2630 #endif
2631 if (serial_paranoia_check(info, tty->device, "cy_open")){
2632 return -ENODEV;
2634 #ifdef SERIAL_DEBUG_OPEN
2635 printk("cy_open ttyC%d, count = %d\n", info->line, info->count);/**/
2636 #endif
2637 info->count++;
2638 #ifdef SERIAL_DEBUG_COUNT
2639 printk("cyc: %d(%d): incrementing count to %d\n", __LINE__, current->pid, info->count);
2640 #endif
2641 tty->driver_data = info;
2642 info->tty = tty;
2644 if (!tmp_buf) {
2645 tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2646 if (!tmp_buf){
2647 return -ENOMEM;
2651 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2652 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2653 *tty->termios = info->normal_termios;
2654 else
2655 *tty->termios = info->callout_termios;
2658 * Start up serial port
2660 retval = startup(info);
2661 if (retval){
2662 return retval;
2665 MOD_INC_USE_COUNT;
2667 retval = block_til_ready(tty, filp, info);
2668 if (retval) {
2669 #ifdef SERIAL_DEBUG_OPEN
2670 printk("cy_open returning after block_til_ready with %d\n",
2671 retval);
2672 #endif
2673 return retval;
2676 info->session = current->session;
2677 info->pgrp = current->pgrp;
2679 #ifdef SERIAL_DEBUG_OPEN
2680 printk("cy_open done\n");/**/
2681 #endif
2682 return 0;
2683 } /* cy_open */
2688 * ---------------------------------------------------------------------
2689 * cy_init() and friends
2691 * cy_init() is called at boot-time to initialize the serial driver.
2692 * ---------------------------------------------------------------------
2696 * This routine prints out the appropriate serial driver version
2697 * number, and identifies which options were configured into this
2698 * driver.
2700 static inline void
2701 show_version(void)
2703 printk("Cyclom driver %s\n",rcsid);
2704 } /* show_version */
2706 /* initialize chips on card -- return number of valid
2707 chips (which is number of ports/4) */
2708 __initfunc(static int
2709 cy_init_card(unsigned char *true_base_addr,int index))
2711 unsigned int chip_number;
2712 unsigned char* base_addr;
2714 true_base_addr[Cy_HwReset<<index] = 0; /* Cy_HwReset is 0x1400 */
2715 true_base_addr[Cy_ClrIntr<<index] = 0; /* Cy_ClrIntr is 0x1800 */
2716 udelay(500L);
2718 for(chip_number=0; chip_number<CyMaxChipsPerCard; chip_number++){
2719 base_addr = true_base_addr + (cy_chip_offset[chip_number]<<index);
2720 udelay(1000L);
2721 if(base_addr[CyCCR<<index] != 0x00){
2722 /*************
2723 printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
2724 chip_number, (unsigned long)base_addr);
2725 *************/
2726 return chip_number;
2729 base_addr[CyGFRCR<<index] = 0;
2730 udelay(10L);
2732 /* The Cyclom-16Y does not decode address bit 9 and therefore
2733 cannot distinguish between references to chip 0 and a non-
2734 existent chip 4. If the preceding clearing of the supposed
2735 chip 4 GFRCR register appears at chip 0, there is no chip 4
2736 and this must be a Cyclom-16Y, not a Cyclom-32Ye.
2738 if (chip_number == 4
2739 && *(true_base_addr + (cy_chip_offset[0]<<index) + (CyGFRCR<<index)) == 0){
2740 return chip_number;
2743 base_addr[CyCCR<<index] = CyCHIP_RESET;
2744 udelay(1000L);
2746 if(base_addr[CyGFRCR<<index] == 0x00){
2748 printk(" chip #%d at %#6lx is not responding (GFRCR stayed 0)\n",
2749 chip_number, (unsigned long)base_addr);
2751 return chip_number;
2753 if((0xf0 & base_addr[CyGFRCR<<index]) != 0x40){
2755 printk(" chip #%d at %#6lx is not valid (GFRCR == %#2x)\n",
2756 chip_number, (unsigned long)base_addr, base_addr[CyGFRCR<<index]);
2758 return chip_number;
2760 base_addr[CyGCR<<index] = CyCH0_SERIAL;
2761 base_addr[CyPPR<<index] = 244; /* better value than CyCLOCK_25_1MS * 5
2762 to run clock at 200 Hz */
2765 printk(" chip #%d at %#6lx is rev 0x%2x\n",
2766 chip_number, (unsigned long)base_addr, base_addr[CyGFRCR<<index]);
2770 return chip_number;
2771 } /* cy_init_card */
2773 /* The serial driver boot-time initialization code!
2774 Hardware I/O ports are mapped to character special devices on a
2775 first found, first allocated manner. That is, this code searches
2776 for Cyclom cards in the system. As each is found, it is probed
2777 to discover how many chips (and thus how many ports) are present.
2778 These ports are mapped to the tty ports 32 and upward in monotonic
2779 fashion. If an 8-port card is replaced with a 16-port card, the
2780 port mapping on a following card will shift.
2782 This approach is different from what is used in the other serial
2783 device driver because the Cyclom is more properly a multiplexer,
2784 not just an aggregation of serial ports on one card.
2786 If there are more cards with more ports than have been statically
2787 allocated above, a warning is printed and the extra ports are ignored.
2789 __initfunc(int
2790 cy_init(void))
2792 struct cyclades_port *info;
2793 struct cyclades_card *cinfo;
2794 int board,port,i;
2796 show_version();
2798 /* Initialize the tty_driver structure */
2800 memset(&cy_serial_driver, 0, sizeof(struct tty_driver));
2801 cy_serial_driver.magic = TTY_DRIVER_MAGIC;
2802 cy_serial_driver.name = "ttyC";
2803 cy_serial_driver.major = CYCLADES_MAJOR;
2804 cy_serial_driver.minor_start = 0;
2805 cy_serial_driver.num = NR_PORTS;
2806 cy_serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2807 cy_serial_driver.subtype = SERIAL_TYPE_NORMAL;
2808 cy_serial_driver.init_termios = tty_std_termios;
2809 cy_serial_driver.init_termios.c_cflag =
2810 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2811 cy_serial_driver.flags = TTY_DRIVER_REAL_RAW;
2812 cy_serial_driver.refcount = &serial_refcount;
2813 cy_serial_driver.table = serial_table;
2814 cy_serial_driver.termios = serial_termios;
2815 cy_serial_driver.termios_locked = serial_termios_locked;
2816 cy_serial_driver.open = cy_open;
2817 cy_serial_driver.close = cy_close;
2818 cy_serial_driver.write = cy_write;
2819 cy_serial_driver.put_char = cy_put_char;
2820 cy_serial_driver.flush_chars = cy_flush_chars;
2821 cy_serial_driver.write_room = cy_write_room;
2822 cy_serial_driver.chars_in_buffer = cy_chars_in_buffer;
2823 cy_serial_driver.flush_buffer = cy_flush_buffer;
2824 cy_serial_driver.ioctl = cy_ioctl;
2825 cy_serial_driver.throttle = cy_throttle;
2826 cy_serial_driver.unthrottle = cy_unthrottle;
2827 cy_serial_driver.set_termios = cy_set_termios;
2828 cy_serial_driver.stop = cy_stop;
2829 cy_serial_driver.start = cy_start;
2830 cy_serial_driver.hangup = cy_hangup;
2833 * The callout device is just like normal device except for
2834 * major number and the subtype code.
2836 cy_callout_driver = cy_serial_driver;
2837 cy_callout_driver.name = "cub";
2838 cy_callout_driver.major = CYCLADESAUX_MAJOR;
2839 cy_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2841 if (tty_register_driver(&cy_serial_driver))
2842 panic("Couldn't register Cyclom serial driver\n");
2843 if (tty_register_driver(&cy_callout_driver))
2844 panic("Couldn't register Cyclom callout driver\n");
2846 init_bh(CYCLADES_BH, do_cyclades_bh);
2848 for (i = 0; i < 16; i++) {
2849 IRQ_cards[i] = 0;
2852 for (i = 0; i < NR_CARDS; i++) {
2853 /* base_addr=0 indicates board not found */
2854 cy_card[i].base_addr = 0;
2857 /* the code below is responsible to find the boards. Each different
2858 type of board has its own detection routine. If a board is found,
2859 the next cy_card structure available is set by the detection
2860 routine. These functions are responsible for checking the availability
2861 of cy_card and cy_port data structures and updating the
2862 cy_next_channel. */
2864 /* look for isa boards */
2865 cy_isa_nboard = cy_detect_isa();
2867 /* look for pci boards */
2868 cy_pci_nboard = cy_detect_pci();
2870 cy_nboard = cy_isa_nboard + cy_pci_nboard;
2872 /* invalidate remaining cy_card structures */
2873 for (i = 0 ; i < NR_CARDS ; i++) {
2874 if (cy_card[i].base_addr == 0) {
2875 cy_card[i].first_line = -1;
2878 /* invalidate remaining cy_port structures */
2879 for (i = cy_next_channel ; i < NR_PORTS ; i++) {
2880 cy_port[i].line = -1;
2881 cy_port[i].magic = -1;
2884 /* initialize per-port data structures for each valid board found */
2885 for (board = 0 ; board < cy_nboard ; board++) {
2886 cinfo = &cy_card[board];
2887 for (port = cinfo->first_line ;
2888 port < cinfo->first_line + 4*cinfo->num_chips ;
2889 port++)
2891 info = &cy_port[port];
2892 info->magic = CYCLADES_MAGIC;
2893 info->type = PORT_CIRRUS;
2894 info->card = board;
2895 info->line = port;
2896 info->flags = STD_COM_FLAGS;
2897 info->tty = 0;
2898 info->xmit_fifo_size = 12;
2899 info->cor1 = CyPARITY_NONE|Cy_1_STOP|Cy_8_BITS;
2900 info->cor2 = CyETC;
2901 info->cor3 = 0x08; /* _very_ small receive threshold */
2902 info->cor4 = 0;
2903 info->cor5 = 0;
2904 info->tbpr = baud_bpr[13]; /* Tx BPR */
2905 info->tco = baud_co[13]; /* Tx CO */
2906 info->rbpr = baud_bpr[13]; /* Rx BPR */
2907 info->rco = baud_co[13]; /* Rx CO */
2908 info->close_delay = 0;
2909 info->x_char = 0;
2910 info->event = 0;
2911 info->count = 0;
2912 #ifdef SERIAL_DEBUG_COUNT
2913 printk("cyc: %d: setting count to 0\n", __LINE__);
2914 #endif
2915 info->blocked_open = 0;
2916 info->default_threshold = 0;
2917 info->default_timeout = 0;
2918 info->tqueue.routine = do_softint;
2919 info->tqueue.data = info;
2920 info->callout_termios =cy_callout_driver.init_termios;
2921 info->normal_termios = cy_serial_driver.init_termios;
2922 info->open_wait = 0;
2923 info->close_wait = 0;
2924 /* info->session */
2925 /* info->pgrp */
2926 info->read_status_mask = CyTIMEOUT| CySPECHAR| CyBREAK
2927 | CyPARITY| CyFRAME| CyOVERRUN;
2928 /* info->timeout */
2931 return 0;
2933 } /* cy_init */
2935 #ifdef MODULE
2937 init_module(void)
2939 return(cy_init());
2942 void
2943 cleanup_module(void)
2945 unsigned long flags;
2946 int i;
2948 save_flags(flags);
2949 cli();
2950 remove_bh(CYCLADES_BH);
2951 if (tty_unregister_driver(&cy_callout_driver))
2952 printk("Couldn't unregister Cyclom callout driver\n");
2953 if (tty_unregister_driver(&cy_serial_driver))
2954 printk("Couldn't unregister Cyclom serial driver\n");
2955 restore_flags(flags);
2957 for (i = 0; i < NR_CARDS; i++) {
2958 if (cy_card[i].base_addr != 0)
2960 free_irq(cy_card[i].irq,NULL);
2964 #endif
2967 * ---------------------------------------------------------------------
2968 * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
2969 * sets global variables and return the number of ISA boards found.
2970 * ---------------------------------------------------------------------
2972 __initfunc(int
2973 cy_detect_isa())
2975 unsigned int cy_isa_irq,nboard;
2976 unsigned char *cy_isa_address;
2977 unsigned short i,j,cy_isa_nchan;
2979 nboard = 0;
2981 /* scan the address table probing for Cyclom-Y/ISA boards */
2982 for (i = 0 ; i < NR_ISA_ADDRESSES ; i++) {
2983 cy_isa_address = cy_isa_addresses[i];
2984 if (cy_isa_address == 0x0000) {
2985 return(nboard);
2988 /* probe for CD1400... */
2989 cy_isa_nchan = 4 * cy_init_card(cy_isa_address,0);
2990 if (cy_isa_nchan == 0) {
2991 continue;
2994 /* find out the board's irq by probing */
2995 cy_isa_irq = do_auto_irq(cy_isa_address);
2996 if (cy_isa_irq == 0) {
2997 printk("Cyclom-Y/ISA found at 0x%x but the IRQ could not be detected.\n",
2998 (unsigned int) cy_isa_address);
2999 continue;
3002 if((cy_next_channel+cy_isa_nchan) > NR_PORTS) {
3003 printk("Cyclom-Y/ISA found at 0x%x but no more channel structures are available.\n",
3004 (unsigned int) cy_isa_address);
3005 return(nboard);
3007 /* fill the next cy_card structure available */
3008 for (j = 0 ; j < NR_CARDS ; j++) {
3009 if (cy_card[j].base_addr == 0) break;
3011 if (j == NR_CARDS) { /* no more cy_cards available */
3012 printk("Cyclom-Y/ISA found at 0x%x but no more card structures are available.\n",
3013 (unsigned int) cy_isa_address);
3014 return(nboard);
3017 /* allocate IRQ */
3018 if(request_irq(cy_isa_irq,cy_interrupt,SA_INTERRUPT,"cyclades",NULL))
3020 printk("Cyclom-Y/ISA found at 0x%x but could not allocate interrupt IRQ#%d.\n",
3021 (unsigned int) cy_isa_address,cy_isa_irq);
3022 return(nboard);
3025 /* set cy_card */
3026 cy_card[j].base_addr = (int) cy_isa_address;
3027 cy_card[j].irq = (int) cy_isa_irq;
3028 cy_card[j].bus_index = 0;
3029 cy_card[j].first_line = cy_next_channel;
3030 cy_card[j].num_chips = cy_isa_nchan/4;
3031 IRQ_cards[cy_isa_irq] = &cy_card[j];
3032 nboard++;
3034 /* print message */
3035 printk("Cyclom-Y/ISA #%d: 0x%x-0x%x, IRQ%d, %d channels starting from port %d.\n",
3036 j+1,(unsigned int) cy_isa_address,
3037 (unsigned int)(cy_isa_address + 0x1fff),
3038 cy_isa_irq,cy_isa_nchan,cy_next_channel);
3039 cy_next_channel += cy_isa_nchan;
3041 return(nboard);
3046 * ---------------------------------------------------------------------
3047 * cy_detect_pci() - Test PCI bus presence and Cyclom-Ye/PCI.
3048 * sets global variables and return the number of PCI boards found.
3049 * ---------------------------------------------------------------------
3051 __initfunc(int
3052 cy_detect_pci())
3054 #ifdef CONFIG_PCI
3055 unsigned char cyy_bus, cyy_dev_fn, cyy_rev_id;
3056 unsigned long pci_intr_ctrl;
3057 unsigned char cy_pci_irq;
3058 unsigned int cy_pci_address, cy_pci_io;
3059 unsigned short i,j,cy_pci_nchan;
3060 unsigned short device_id,dev_index = 0,board_index = 0;
3062 if(pcibios_present() == 0) { /* PCI bus not present */
3063 return(0);
3065 for (i = 0; i < NR_CARDS; i++) {
3066 /* look for a Cyclom-Y card by vendor and device id */
3067 while((device_id = cy_pci_dev_id[dev_index]) != 0) {
3068 if(pcibios_find_device(PCI_VENDOR_ID_CYCLADES,
3069 device_id,board_index,
3070 &cyy_bus, &cyy_dev_fn) != 0)
3072 dev_index++; /* try next device id */
3073 board_index = 0;
3074 } else {
3075 board_index++;
3076 break; /* found a board */
3079 if (device_id == 0) break;
3081 /* read PCI configuration area */
3082 pcibios_read_config_byte(cyy_bus, cyy_dev_fn,
3083 PCI_INTERRUPT_LINE, &cy_pci_irq);
3084 pcibios_read_config_dword(cyy_bus, cyy_dev_fn,
3085 PCI_BASE_ADDRESS_1, &cy_pci_io);
3086 pcibios_read_config_dword(cyy_bus, cyy_dev_fn,
3087 PCI_BASE_ADDRESS_2, &cy_pci_address);
3088 pcibios_read_config_byte(cyy_bus, cyy_dev_fn,
3089 PCI_REVISION_ID, &cyy_rev_id);
3090 cy_pci_address &= 0xfffffff0;
3091 if ((ulong)cy_pci_address >= 0x100000) { /* above 1M? */
3092 cy_pci_address =
3093 (unsigned int) ioremap(cy_pci_address,0x4000);
3095 cy_pci_io &= 0xfffffffc;
3096 cy_pci_nchan = 4 * cy_init_card((unsigned char *)
3097 cy_pci_address,1);
3098 if(cy_pci_nchan == 0) {
3099 printk("Cyclom-Y PCI host card with no Serial-Modules at 0x%x.\n",
3100 (unsigned int) cy_pci_address);
3101 continue;
3103 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
3104 printk("Cyclom-Y/PCI found at 0x%x but no more channel structures are available.\n",
3105 (unsigned int) cy_pci_address);
3106 return(i);
3108 #ifdef CY_PCI_DEBUG
3109 printk("Cyclom-Ye/PCI #%d (bus=0x0%x, pci_id=0x%x, rev_id=%d).\n",
3110 i+1,cyy_bus,cyy_dev_fn,cyy_rev_id);
3111 printk("Cyclom-Ye/PCI: found at 0x%x, IRQ%d, ioaddr = 0x%lx.\n",
3112 cy_pci_address,(int)cy_pci_irq,cy_pci_io);
3113 #endif
3114 /* fill the next cy_card structure available */
3115 for (j = 0 ; j < NR_CARDS ; j++) {
3116 if (cy_card[j].base_addr == 0) break;
3118 if (j == NR_CARDS) { /* no more cy_cards available */
3119 printk("Cyclom-Y/PCI found at 0x%x but no more card structures are available.\n",
3120 (unsigned int) cy_pci_address);
3121 return(i);
3124 /* allocate IRQ */
3125 if(request_irq(cy_pci_irq,cy_interrupt,SA_INTERRUPT,"cyclades",NULL))
3127 printk("Cyclom-Y/PCI found at 0x%x but could not allocate interrupt IRQ%d.\n",
3128 (unsigned int) cy_pci_address,cy_pci_irq);
3129 return(i);
3132 /* set cy_card */
3133 cy_card[j].base_addr = (int) cy_pci_address;
3134 cy_card[j].irq = (int) cy_pci_irq;
3135 cy_card[j].bus_index = 1;
3136 cy_card[j].first_line = cy_next_channel;
3137 cy_card[j].num_chips = cy_pci_nchan/4;
3138 IRQ_cards[cy_pci_irq] = &cy_card[j];
3140 /* enable interrupts in the PCI interface */
3141 outw(inw(cy_pci_io+0x68)|0x0900,cy_pci_io+0x68);
3142 pci_intr_ctrl = (unsigned long)(inw(cy_pci_io+0x68) | inw(cy_pci_io+0x6a)<<16);
3144 /* print message */
3145 printk("Cyclom-Y/PCI #%d: 0x%x-0x%x, IRQ%d, %d channels starting from port %d.\n",
3146 j+1,(unsigned int) cy_pci_address,
3147 (unsigned int)(cy_pci_address + 0x3fff),
3148 (int)cy_pci_irq,cy_pci_nchan,cy_next_channel);
3150 cy_next_channel += cy_pci_nchan;
3152 return(i);
3153 #else
3154 return(0);
3155 #endif /* ifdef CONFIG_PCI */
3159 #ifdef CYCLOM_SHOW_STATUS
3160 static void
3161 show_status(int line_num)
3163 unsigned char *base_addr;
3164 int card,chip,channel,index;
3165 struct cyclades_port * info;
3166 unsigned long flags;
3168 info = &cy_port[line_num];
3169 card = info->card;
3170 index = cy_card[card].bus_index;
3171 channel = (info->line) - (cy_card[card].first_line);
3172 chip = channel>>2;
3173 channel &= 0x03;
3174 printk(" card %d, chip %d, channel %d\n", card, chip, channel);/**/
3176 printk(" cy_card\n");
3177 printk(" irq base_addr num_chips first_line = %d %lx %d %d\n",
3178 cy_card[card].irq, (long)cy_card[card].base_addr,
3179 cy_card[card].num_chips, cy_card[card].first_line);
3181 printk(" cy_port\n");
3182 printk(" card line flags = %d %d %x\n",
3183 info->card, info->line, info->flags);
3184 printk(" *tty read_status_mask timeout xmit_fifo_size = %lx %x %x %x\n",
3185 (long)info->tty, info->read_status_mask,
3186 info->timeout, info->xmit_fifo_size);
3187 printk(" cor1,cor2,cor3,cor4,cor5 = %x %x %x %x %x\n",
3188 info->cor1, info->cor2, info->cor3, info->cor4, info->cor5);
3189 printk(" tbpr,tco,rbpr,rco = %d %d %d %d\n",
3190 info->tbpr, info->tco, info->rbpr, info->rco);
3191 printk(" close_delay event count = %d %d %d\n",
3192 info->close_delay, info->event, info->count);
3193 printk(" x_char blocked_open = %x %x\n",
3194 info->x_char, info->blocked_open);
3195 printk(" session pgrp open_wait = %lx %lx %lx\n",
3196 info->session, info->pgrp, (long)info->open_wait);
3199 save_flags(flags); cli();
3201 base_addr = (unsigned char*)
3202 (cy_card[card].base_addr + (cy_chip_offset[chip]<<index));
3204 /* Global Registers */
3206 printk(" CyGFRCR %x\n", base_addr[CyGFRCR<<index]);
3207 printk(" CyCAR %x\n", base_addr[CyCAR<<index]);
3208 printk(" CyGCR %x\n", base_addr[CyGCR<<index]);
3209 printk(" CySVRR %x\n", base_addr[CySVRR<<index]);
3210 printk(" CyRICR %x\n", base_addr[CyRICR<<index]);
3211 printk(" CyTICR %x\n", base_addr[CyTICR<<index]);
3212 printk(" CyMICR %x\n", base_addr[CyMICR<<index]);
3213 printk(" CyRIR %x\n", base_addr[CyRIR<<index]);
3214 printk(" CyTIR %x\n", base_addr[CyTIR<<index]);
3215 printk(" CyMIR %x\n", base_addr[CyMIR<<index]);
3216 printk(" CyPPR %x\n", base_addr[CyPPR<<index]);
3218 base_addr[CyCAR<<index] = (u_char)channel;
3220 /* Virtual Registers */
3222 printk(" CyRIVR %x\n", base_addr[CyRIVR<<index]);
3223 printk(" CyTIVR %x\n", base_addr[CyTIVR<<index]);
3224 printk(" CyMIVR %x\n", base_addr[CyMIVR<<index]);
3225 printk(" CyMISR %x\n", base_addr[CyMISR<<index]);
3227 /* Channel Registers */
3229 printk(" CyCCR %x\n", base_addr[CyCCR<<index]);
3230 printk(" CySRER %x\n", base_addr[CySRER<<index]);
3231 printk(" CyCOR1 %x\n", base_addr[CyCOR1<<index]);
3232 printk(" CyCOR2 %x\n", base_addr[CyCOR2<<index]);
3233 printk(" CyCOR3 %x\n", base_addr[CyCOR3<<index]);
3234 printk(" CyCOR4 %x\n", base_addr[CyCOR4<<index]);
3235 printk(" CyCOR5 %x\n", base_addr[CyCOR5<<index]);
3236 printk(" CyCCSR %x\n", base_addr[CyCCSR<<index]);
3237 printk(" CyRDCR %x\n", base_addr[CyRDCR<<index]);
3238 printk(" CySCHR1 %x\n", base_addr[CySCHR1<<index]);
3239 printk(" CySCHR2 %x\n", base_addr[CySCHR2<<index]);
3240 printk(" CySCHR3 %x\n", base_addr[CySCHR3<<index]);
3241 printk(" CySCHR4 %x\n", base_addr[CySCHR4<<index]);
3242 printk(" CySCRL %x\n", base_addr[CySCRL<<index]);
3243 printk(" CySCRH %x\n", base_addr[CySCRH<<index]);
3244 printk(" CyLNC %x\n", base_addr[CyLNC<<index]);
3245 printk(" CyMCOR1 %x\n", base_addr[CyMCOR1<<index]);
3246 printk(" CyMCOR2 %x\n", base_addr[CyMCOR2<<index]);
3247 printk(" CyRTPR %x\n", base_addr[CyRTPR<<index]);
3248 printk(" CyMSVR1 %x\n", base_addr[CyMSVR1<<index]);
3249 printk(" CyMSVR2 %x\n", base_addr[CyMSVR2<<index]);
3250 printk(" CyRBPR %x\n", base_addr[CyRBPR<<index]);
3251 printk(" CyRCOR %x\n", base_addr[CyRCOR<<index]);
3252 printk(" CyTBPR %x\n", base_addr[CyTBPR<<index]);
3253 printk(" CyTCOR %x\n", base_addr[CyTCOR<<index]);
3255 restore_flags(flags);
3256 } /* show_status */
3257 #endif