Merge with rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[pandora-kernel.git] / drivers / char / cyclades.c
1 #undef  BLOCKMOVE
2 #define Z_WAKE
3 #undef  Z_EXT_CHARS_IN_BUFFER
4 static char rcsid[] =
5 "$Revision: 2.3.2.20 $$Date: 2004/02/25 18:14:16 $";
6
7 /*
8  *  linux/drivers/char/cyclades.c
9  *
10  * This file contains the driver for the Cyclades async multiport
11  * serial boards.
12  *
13  * Initially written by Randolph Bentson <bentson@grieg.seaslug.org>.
14  * Modified and maintained by Marcio Saito <marcio@cyclades.com>.
15  * Currently maintained by Cyclades team <async@cyclades.com>.
16  *
17  * For Technical support and installation problems, please send e-mail
18  * to support@cyclades.com.
19  *
20  * Much of the design and some of the code came from serial.c
21  * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
22  * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
23  * and then fixed as suggested by Michael K. Johnson 12/12/92.
24  *
25  * This version supports shared IRQ's (only for PCI boards).
26  *
27  * $Log: cyclades.c,v $
28  * Prevent users from opening non-existing Z ports.
29  *
30  * Revision 2.3.2.8   2000/07/06 18:14:16 ivan
31  * Fixed the PCI detection function to work properly on Alpha systems.
32  * Implemented support for TIOCSERGETLSR ioctl.
33  * Implemented full support for non-standard baud rates.
34  *
35  * Revision 2.3.2.7   2000/06/01 18:26:34 ivan
36  * Request PLX I/O region, although driver doesn't use it, to avoid
37  * problems with other drivers accessing it.
38  * Removed count for on-board buffer characters in cy_chars_in_buffer
39  * (Cyclades-Z only).
40  *
41  * Revision 2.3.2.6   2000/05/05 13:56:05 ivan
42  * Driver now reports physical instead of virtual memory addresses.
43  * Masks were added to some Cyclades-Z read accesses.
44  * Implemented workaround for PLX9050 bug that would cause a system lockup
45  * in certain systems, depending on the MMIO addresses allocated to the
46  * board.
47  * Changed the Tx interrupt programming in the CD1400 chips to boost up
48  * performance (Cyclom-Y only).
49  * Code is now compliant with the new module interface (module_[init|exit]).
50  * Make use of the PCI helper functions to access PCI resources.
51  * Did some code "housekeeping".
52  *
53  * Revision 2.3.2.5   2000/01/19 14:35:33 ivan
54  * Fixed bug in cy_set_termios on CRTSCTS flag turnoff.
55  *
56  * Revision 2.3.2.4   2000/01/17 09:19:40 ivan
57  * Fixed SMP locking in Cyclom-Y interrupt handler.
58  *
59  * Revision 2.3.2.3   1999/12/28 12:11:39 ivan
60  * Added a new cyclades_card field called nports to allow the driver to
61  * know the exact number of ports found by the Z firmware after its load;
62  * RX buffer contention prevention logic on interrupt op mode revisited
63  * (Cyclades-Z only);
64  * Revisited printk's for Z debug;
65  * Driver now makes sure that the constant SERIAL_XMIT_SIZE is defined;
66  *
67  * Revision 2.3.2.2   1999/10/01 11:27:43 ivan
68  * Fixed bug in cyz_poll that would make all ports but port 0 
69  * unable to transmit/receive data (Cyclades-Z only);
70  * Implemented logic to prevent the RX buffer from being stuck with data
71  * due to a driver / firmware race condition in interrupt op mode
72  * (Cyclades-Z only);
73  * Fixed bug in block_til_ready logic that would lead to a system crash;
74  * Revisited cy_close spinlock usage;
75  *
76  * Revision 2.3.2.1   1999/09/28 11:01:22 ivan
77  * Revisited CONFIG_PCI conditional compilation for PCI board support;
78  * Implemented TIOCGICOUNT and TIOCMIWAIT ioctl support;
79  * _Major_ cleanup on the Cyclades-Z interrupt support code / logic;
80  * Removed CTS handling from the driver -- this is now completely handled
81  * by the firmware (Cyclades-Z only);
82  * Flush RX on-board buffers on a port open (Cyclades-Z only);
83  * Fixed handling of ASYNC_SPD_* TTY flags;
84  * Module unload now unmaps all memory area allocated by ioremap;
85  *
86  * Revision 2.3.1.1   1999/07/15 16:45:53 ivan
87  * Removed CY_PROC conditional compilation;
88  * Implemented SMP-awareness for the driver;
89  * Implemented a new ISA IRQ autoprobe that uses the irq_probe_[on|off] 
90  * functions;
91  * The driver now accepts memory addresses (maddr=0xMMMMM) and IRQs
92  * (irq=NN) as parameters (only for ISA boards);
93  * Fixed bug in set_line_char that would prevent the Cyclades-Z 
94  * ports from being configured at speeds above 115.2Kbps;
95  * Fixed bug in cy_set_termios that would prevent XON/XOFF flow control
96  * switching from working properly;
97  * The driver now only prints IRQ info for the Cyclades-Z if it's 
98  * configured to work in interrupt mode;
99  *
100  * Revision 2.2.2.3   1999/06/28 11:13:29 ivan
101  * Added support for interrupt mode operation for the Z cards;
102  * Removed the driver inactivity control for the Z;
103  * Added a missing MOD_DEC_USE_COUNT in the cy_open function for when 
104  * the Z firmware is not loaded yet;
105  * Replaced the "manual" Z Tx flush buffer by a call to a FW command of 
106  * same functionality;
107  * Implemented workaround for IRQ setting loss on the PCI configuration 
108  * registers after a PCI bridge EEPROM reload (affects PLX9060 only);
109  *
110  * Revision 2.2.2.2  1999/05/14 17:18:15 ivan
111  * /proc entry location changed to /proc/tty/driver/cyclades;
112  * Added support to shared IRQ's (only for PCI boards);
113  * Added support for Cobalt Qube2 systems;
114  * IRQ [de]allocation scheme revisited;
115  * BREAK implementation changed in order to make use of the 'break_ctl'
116  * TTY facility;
117  * Fixed typo in TTY structure field 'driver_name';
118  * Included a PCI bridge reset and EEPROM reload in the board 
119  * initialization code (for both Y and Z series).
120  *
121  * Revision 2.2.2.1  1999/04/08 16:17:43 ivan
122  * Fixed a bug in cy_wait_until_sent that was preventing the port to be 
123  * closed properly after a SIGINT;
124  * Module usage counter scheme revisited;
125  * Added support to the upcoming Y PCI boards (i.e., support to additional
126  * PCI Device ID's).
127  * 
128  * Revision 2.2.1.10 1999/01/20 16:14:29 ivan
129  * Removed all unnecessary page-alignement operations in ioremap calls
130  * (ioremap is currently safe for these operations).
131  *
132  * Revision 2.2.1.9  1998/12/30 18:18:30 ivan
133  * Changed access to PLX PCI bridge registers from I/O to MMIO, in 
134  * order to make PLX9050-based boards work with certain motherboards.
135  *
136  * Revision 2.2.1.8  1998/11/13 12:46:20 ivan
137  * cy_close function now resets (correctly) the tty->closing flag;
138  * JIFFIES_DIFF macro fixed.
139  *
140  * Revision 2.2.1.7  1998/09/03 12:07:28 ivan
141  * Fixed bug in cy_close function, which was not informing HW of
142  * which port should have the reception disabled before doing so;
143  * fixed Cyclom-8YoP hardware detection bug.
144  *
145  * Revision 2.2.1.6  1998/08/20 17:15:39 ivan
146  * Fixed bug in cy_close function, which causes malfunction
147  * of one of the first 4 ports when a higher port is closed
148  * (Cyclom-Y only).
149  *
150  * Revision 2.2.1.5  1998/08/10 18:10:28 ivan
151  * Fixed Cyclom-4Yo hardware detection bug.
152  *
153  * Revision 2.2.1.4  1998/08/04 11:02:50 ivan
154  * /proc/cyclades implementation with great collaboration of 
155  * Marc Lewis <marc@blarg.net>;
156  * cyy_interrupt was changed to avoid occurrence of kernel oopses
157  * during PPP operation.
158  *
159  * Revision 2.2.1.3  1998/06/01 12:09:10 ivan
160  * General code review in order to comply with 2.1 kernel standards;
161  * data loss prevention for slow devices revisited (cy_wait_until_sent
162  * was created);
163  * removed conditional compilation for new/old PCI structure support 
164  * (now the driver only supports the new PCI structure).
165  *
166  * Revision 2.2.1.1  1998/03/19 16:43:12 ivan
167  * added conditional compilation for new/old PCI structure support;
168  * removed kernel series (2.0.x / 2.1.x) conditional compilation.
169  *
170  * Revision 2.1.1.3  1998/03/16 18:01:12 ivan
171  * cleaned up the data loss fix;
172  * fixed XON/XOFF handling once more (Cyclades-Z);
173  * general review of the driver routines;
174  * introduction of a mechanism to prevent data loss with slow 
175  * printers, by forcing a delay before closing the port.
176  *
177  * Revision 2.1.1.2  1998/02/17 16:50:00 ivan
178  * fixed detection/handling of new CD1400 in Ye boards;
179  * fixed XON/XOFF handling (Cyclades-Z);
180  * fixed data loss caused by a premature port close;
181  * introduction of a flag that holds the CD1400 version ID per port
182  * (used by the CYGETCD1400VER new ioctl).
183  *
184  * Revision 2.1.1.1  1997/12/03 17:31:19 ivan
185  * Code review for the module cleanup routine;
186  * fixed RTS and DTR status report for new CD1400's in get_modem_info;
187  * includes anonymous changes regarding signal_pending.
188  * 
189  * Revision 2.1  1997/11/01 17:42:41 ivan
190  * Changes in the driver to support Alpha systems (except 8Zo V_1);
191  * BREAK fix for the Cyclades-Z boards;
192  * driver inactivity control by FW implemented;
193  * introduction of flag that allows driver to take advantage of 
194  * a special CD1400 feature related to HW flow control;
195  * added support for the CD1400  rev. J (Cyclom-Y boards);
196  * introduction of ioctls to:
197  *  - control the rtsdtr_inv flag (Cyclom-Y);
198  *  - control the rflow flag (Cyclom-Y);
199  *  - adjust the polling interval (Cyclades-Z);
200  *
201  * Revision 1.36.4.33  1997/06/27 19:00:00  ivan
202  * Fixes related to kernel version conditional 
203  * compilation.
204  *  
205  * Revision 1.36.4.32  1997/06/14 19:30:00  ivan
206  * Compatibility issues between kernels 2.0.x and 
207  * 2.1.x (mainly related to clear_bit function).
208  *  
209  * Revision 1.36.4.31  1997/06/03 15:30:00  ivan
210  * Changes to define the memory window according to the 
211  * board type.
212  *  
213  * Revision 1.36.4.30  1997/05/16 15:30:00  daniel
214  * Changes to support new cycladesZ boards.
215  *
216  * Revision 1.36.4.29  1997/05/12 11:30:00  daniel
217  * Merge of Bentson's and Daniel's version 1.36.4.28.
218  * Corrects bug in cy_detect_pci: check if there are more
219  * ports than the number of static structs allocated.
220  * Warning message during initialization if this driver is
221  * used with the new generation of cycladesZ boards.  Those
222  * will be supported only in next release of the driver.
223  * Corrects bug in cy_detect_pci and cy_detect_isa that
224  * returned wrong number of VALID boards, when a cyclomY
225  * was found with no serial modules connected.
226  * Changes to use current (2.1.x) kernel subroutine names
227  * and created macros for compilation with 2.0.x kernel,
228  * instead of the other way around.
229  *
230  * Revision 1.36.4.28  1997/05/?? ??:00:00  bentson
231  * Change queue_task_irq_off to queue_task_irq.
232  * The inline function queue_task_irq_off (tqueue.h)
233  * was removed from latest releases of 2.1.x kernel.
234  * Use of macro __init to mark the initialization
235  * routines, so memory can be reused.
236  * Also incorporate implementation of critical region
237  * in function cleanup_module() created by anonymous
238  * linuxer.
239  *
240  * Revision 1.36.4.28  1997/04/25 16:00:00  daniel
241  * Change to support new firmware that solves DCD problem:
242  * application could fail to receive SIGHUP signal when DCD
243  * varying too fast.
244  *
245  * Revision 1.36.4.27  1997/03/26 10:30:00  daniel
246  * Changed for support linux versions 2.1.X.
247  * Backward compatible with linux versions 2.0.X.
248  * Corrected illegal use of filler field in
249  * CH_CTRL struct.
250  * Deleted some debug messages.
251  *
252  * Revision 1.36.4.26  1997/02/27 12:00:00  daniel
253  * Included check for NULL tty pointer in cyz_poll.
254  *
255  * Revision 1.36.4.25  1997/02/26 16:28:30  bentson
256  * Bill Foster at Blarg! Online services noticed that
257  * some of the switch elements of -Z modem control
258  * lacked a closing "break;"
259  *
260  * Revision 1.36.4.24  1997/02/24 11:00:00  daniel
261  * Changed low water threshold for buffer xmit_buf
262  *
263  * Revision 1.36.4.23  1996/12/02 21:50:16  bentson
264  * Marcio provided fix to modem status fetch for -Z
265  *
266  * Revision 1.36.4.22  1996/10/28 22:41:17  bentson
267  * improve mapping of -Z control page (thanks to Steve
268  * Price <stevep@fa.tdktca.com> for help on this)
269  *
270  * Revision 1.36.4.21  1996/09/10 17:00:10  bentson
271  * shift from CPU-bound to memcopy in cyz_polling operation
272  *
273  * Revision 1.36.4.20  1996/09/09 18:30:32  Bentson
274  * Added support to set and report higher speeds.
275  *
276  * Revision 1.36.4.19c  1996/08/09 10:00:00  Marcio Saito
277  * Some fixes in the HW flow control for the BETA release.
278  * Don't try to register the IRQ.
279  *
280  * Revision 1.36.4.19  1996/08/08 16:23:18  Bentson
281  * make sure "cyc" appears in all kernel messages; all soft interrupts
282  * handled by same routine; recognize out-of-band reception; comment
283  * out some diagnostic messages; leave RTS/CTS flow control to hardware;
284  * fix race condition in -Z buffer management; only -Y needs to explicitly
285  * flush chars; tidy up some startup messages;
286  *
287  * Revision 1.36.4.18  1996/07/25 18:57:31  bentson
288  * shift MOD_INC_USE_COUNT location to match
289  * serial.c; purge some diagnostic messages;
290  *
291  * Revision 1.36.4.17  1996/07/25 18:01:08  bentson
292  * enable modem status messages and fetch & process them; note
293  * time of last activity type for each port; set_line_char now
294  * supports more than line 0 and treats 0 baud correctly;
295  * get_modem_info senses rs_status;
296  *
297  * Revision 1.36.4.16  1996/07/20 08:43:15  bentson
298  * barely works--now's time to turn on
299  * more features 'til it breaks
300  *
301  * Revision 1.36.4.15  1996/07/19 22:30:06  bentson
302  * check more -Z board status; shorten boot message
303  *
304  * Revision 1.36.4.14  1996/07/19 22:20:37  bentson
305  * fix reference to ch_ctrl in startup; verify return
306  * values from cyz_issue_cmd and cyz_update_channel;
307  * more stuff to get modem control correct;
308  *
309  * Revision 1.36.4.13  1996/07/11 19:53:33  bentson
310  * more -Z stuff folded in; re-order changes to put -Z stuff
311  * after -Y stuff (to make changes clearer)
312  *
313  * Revision 1.36.4.12  1996/07/11 15:40:55  bentson
314  * Add code to poll Cyclades-Z.  Add code to get & set RS-232 control.
315  * Add code to send break.  Clear firmware ID word at startup (so
316  * that other code won't talk to inactive board).
317  *
318  * Revision 1.36.4.11  1996/07/09 05:28:29  bentson
319  * add code for -Z in set_line_char
320  *
321  * Revision 1.36.4.10  1996/07/08 19:28:37  bentson
322  * fold more -Z stuff (or in some cases, error messages)
323  * into driver; add text to "don't know what to do" messages.
324  *
325  * Revision 1.36.4.9  1996/07/08 18:38:38  bentson
326  * moved compile-time flags near top of file; cosmetic changes
327  * to narrow text (to allow 2-up printing); changed many declarations
328  * to "static" to limit external symbols; shuffled code order to
329  * coalesce -Y and -Z specific code, also to put internal functions
330  * in order of tty_driver structure; added code to recognize -Z
331  * ports (and for moment, do nothing or report error); add cy_startup
332  * to parse boot command line for extra base addresses for ISA probes;
333  *
334  * Revision 1.36.4.8  1996/06/25 17:40:19  bentson
335  * reorder some code, fix types of some vars (int vs. long),
336  * add cy_setup to support user declared ISA addresses
337  *
338  * Revision 1.36.4.7  1996/06/21 23:06:18  bentson
339  * dump ioctl based firmware load (it's now a user level
340  * program); ensure uninitialzed ports cannot be used
341  *
342  * Revision 1.36.4.6  1996/06/20 23:17:19  bentson
343  * rename vars and restructure some code
344  *
345  * Revision 1.36.4.5  1996/06/14 15:09:44  bentson
346  * get right status back after boot load
347  *
348  * Revision 1.36.4.4  1996/06/13 19:51:44  bentson
349  * successfully loads firmware
350  *
351  * Revision 1.36.4.3  1996/06/13 06:08:33  bentson
352  * add more of the code for the boot/load ioctls
353  *
354  * Revision 1.36.4.2  1996/06/11 21:00:51  bentson
355  * start to add Z functionality--starting with ioctl
356  * for loading firmware
357  *
358  * Revision 1.36.4.1  1996/06/10 18:03:02  bentson
359  * added code to recognize Z/PCI card at initialization; report
360  * presence, but card is not initialized (because firmware needs
361  * to be loaded)
362  *
363  * Revision 1.36.3.8  1996/06/07 16:29:00  bentson
364  * starting minor number at zero; added missing verify_area
365  * as noted by Heiko Eissfeldt <heiko@colossus.escape.de>
366  *
367  * Revision 1.36.3.7  1996/04/19 21:06:18  bentson
368  * remove unneeded boot message & fix CLOCAL hardware flow
369  * control (Miquel van Smoorenburg <miquels@Q.cistron.nl>);
370  * remove unused diagnostic statements; minor 0 is first;
371  *
372  * Revision 1.36.3.6  1996/03/13 13:21:17  marcio
373  * The kernel function vremap (available only in later 1.3.xx kernels)
374  * allows the access to memory addresses above the RAM. This revision
375  * of the driver supports PCI boards below 1Mb (device id 0x100) and
376  * above 1Mb (device id 0x101).
377  *
378  * Revision 1.36.3.5  1996/03/07 15:20:17  bentson
379  * Some global changes to interrupt handling spilled into
380  * this driver--mostly unused arguments in system function
381  * calls.  Also added change by Marcio Saito which should
382  * reduce lost interrupts at startup by fast processors.
383  *
384  * Revision 1.36.3.4  1995/11/13  20:45:10  bentson
385  * Changes by Corey Minyard <minyard@wf-rch.cirr.com> distributed
386  * in 1.3.41 kernel to remove a possible race condition, extend
387  * some error messages, and let the driver run as a loadable module
388  * Change by Alan Wendt <alan@ez0.ezlink.com> to remove a
389  * possible race condition.
390  * Change by Marcio Saito <marcio@cyclades.com> to fix PCI addressing.
391  *
392  * Revision 1.36.3.3  1995/11/13  19:44:48  bentson
393  * Changes by Linus Torvalds in 1.3.33 kernel distribution
394  * required due to reordering of driver initialization.
395  * Drivers are now initialized *after* memory management.
396  *
397  * Revision 1.36.3.2  1995/09/08  22:07:14  bentson
398  * remove printk from ISR; fix typo
399  *
400  * Revision 1.36.3.1  1995/09/01  12:00:42  marcio
401  * Minor fixes in the PCI board support. PCI function calls in
402  * conditional compilation (CONFIG_PCI). Thanks to Jim Duncan
403  * <duncan@okay.com>. "bad serial count" message removed.
404  *
405  * Revision 1.36.3  1995/08/22  09:19:42  marcio
406  * Cyclom-Y/PCI support added. Changes in the cy_init routine and
407  * board initialization. Changes in the boot messages. The driver
408  * supports up to 4 boards and 64 ports by default.
409  *
410  * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
411  * disambiguate between Cyclom-16Y and Cyclom-32Ye;
412  *
413  * Revision 1.36.1.3  1995/03/23  22:15:35  bentson
414  * add missing break in modem control block in ioctl switch statement
415  * (discovered by Michael Edward Chastain <mec@jobe.shell.portal.com>);
416  *
417  * Revision 1.36.1.2  1995/03/22  19:16:22  bentson
418  * make sure CTS flow control is set as soon as possible (thanks
419  * to note from David Lambert <lambert@chesapeake.rps.slb.com>);
420  *
421  * Revision 1.36.1.1  1995/03/13  15:44:43  bentson
422  * initialize defaults for receive threshold and stale data timeout;
423  * cosmetic changes;
424  *
425  * Revision 1.36  1995/03/10  23:33:53  bentson
426  * added support of chips 4-7 in 32 port Cyclom-Ye;
427  * fix cy_interrupt pointer dereference problem
428  * (Joe Portman <baron@aa.net>);
429  * give better error response if open is attempted on non-existent port
430  * (Zachariah Vaum <jchryslr@netcom.com>);
431  * correct command timeout (Kenneth Lerman <lerman@@seltd.newnet.com>);
432  * conditional compilation for -16Y on systems with fast, noisy bus;
433  * comment out diagnostic print function;
434  * cleaned up table of base addresses;
435  * set receiver time-out period register to correct value,
436  * set receive threshold to better default values,
437  * set chip timer to more accurate 200 Hz ticking,
438  * add code to monitor and modify receive parameters
439  * (Rik Faith <faith@cs.unc.edu> Nick Simicich
440  * <njs@scifi.emi.net>);
441  *
442  * Revision 1.35  1994/12/16  13:54:18  steffen
443  * additional patch by Marcio Saito for board detection
444  * Accidently left out in 1.34
445  *
446  * Revision 1.34  1994/12/10  12:37:12  steffen
447  * This is the corrected version as suggested by Marcio Saito
448  *
449  * Revision 1.33  1994/12/01  22:41:18  bentson
450  * add hooks to support more high speeds directly; add tytso
451  * patch regarding CLOCAL wakeups
452  *
453  * Revision 1.32  1994/11/23  19:50:04  bentson
454  * allow direct kernel control of higher signalling rates;
455  * look for cards at additional locations
456  *
457  * Revision 1.31  1994/11/16  04:33:28  bentson
458  * ANOTHER fix from Corey Minyard, minyard@wf-rch.cirr.com--
459  * a problem in chars_in_buffer has been resolved by some
460  * small changes;  this should yield smoother output
461  *
462  * Revision 1.30  1994/11/16  04:28:05  bentson
463  * Fix from Corey Minyard, Internet: minyard@metronet.com,
464  * UUCP: minyard@wf-rch.cirr.com, WORK: minyardbnr.ca, to
465  * cy_hangup that appears to clear up much (all?) of the
466  * DTR glitches; also he's added/cleaned-up diagnostic messages
467  *
468  * Revision 1.29  1994/11/16  04:16:07  bentson
469  * add change proposed by Ralph Sims, ralphs@halcyon.com, to
470  * operate higher speeds in same way as other serial ports;
471  * add more serial ports (for up to two 16-port muxes).
472  *
473  * Revision 1.28  1994/11/04  00:13:16  root
474  * turn off diagnostic messages
475  *
476  * Revision 1.27  1994/11/03  23:46:37  root
477  * bunch of changes to bring driver into greater conformance
478  * with the serial.c driver (looking for missed fixes)
479  *
480  * Revision 1.26  1994/11/03  22:40:36  root
481  * automatic interrupt probing fixed.
482  *
483  * Revision 1.25  1994/11/03  20:17:02  root
484  * start to implement auto-irq
485  *
486  * Revision 1.24  1994/11/03  18:01:55  root
487  * still working on modem signals--trying not to drop DTR
488  * during the getty/login processes
489  *
490  * Revision 1.23  1994/11/03  17:51:36  root
491  * extend baud rate support; set receive threshold as function
492  * of baud rate; fix some problems with RTS/CTS;
493  *
494  * Revision 1.22  1994/11/02  18:05:35  root
495  * changed arguments to udelay to type long to get
496  * delays to be of correct duration
497  *
498  * Revision 1.21  1994/11/02  17:37:30  root
499  * employ udelay (after calibrating loops_per_second earlier
500  * in init/main.c) instead of using home-grown delay routines
501  *
502  * Revision 1.20  1994/11/02  03:11:38  root
503  * cy_chars_in_buffer forces a return value of 0 to let
504  * login work (don't know why it does); some functions
505  * that were returning EFAULT, now executes the code;
506  * more work on deciding when to disable xmit interrupts;
507  *
508  * Revision 1.19  1994/11/01  20:10:14  root
509  * define routine to start transmission interrupts (by enabling
510  * transmit interrupts); directly enable/disable modem interrupts;
511  *
512  * Revision 1.18  1994/11/01  18:40:45  bentson
513  * Don't always enable transmit interrupts in startup; interrupt on
514  * TxMpty instead of TxRdy to help characters get out before shutdown;
515  * restructure xmit interrupt to check for chars first and quit if
516  * none are ready to go; modem status (MXVRx) is upright, _not_ inverted
517  * (to my view);
518  *
519  * Revision 1.17  1994/10/30  04:39:45  bentson
520  * rename serial_driver and callout_driver to cy_serial_driver and
521  * cy_callout_driver to avoid linkage interference; initialize
522  * info->type to PORT_CIRRUS; ruggedize paranoia test; elide ->port
523  * from cyclades_port structure; add paranoia check to cy_close;
524  *
525  * Revision 1.16  1994/10/30  01:14:33  bentson
526  * change major numbers; add some _early_ return statements;
527  *
528  * Revision 1.15  1994/10/29  06:43:15  bentson
529  * final tidying up for clean compile;  enable some error reporting
530  *
531  * Revision 1.14  1994/10/28  20:30:22  Bentson
532  * lots of changes to drag the driver towards the new tty_io
533  * structures and operation.  not expected to work, but may
534  * compile cleanly.
535  *
536  * Revision 1.13  1994/07/21  23:08:57  Bentson
537  * add some diagnostic cruft; support 24 lines (for testing
538  * both -8Y and -16Y cards; be more thorough in servicing all
539  * chips during interrupt; add "volatile" a few places to
540  * circumvent compiler optimizations; fix base & offset
541  * computations in block_til_ready (was causing chip 0 to
542  * stop operation)
543  *
544  * Revision 1.12  1994/07/19  16:42:11  Bentson
545  * add some hackery for kernel version 1.1.8; expand
546  * error messages; refine timing for delay loops and
547  * declare loop params volatile
548  *
549  * Revision 1.11  1994/06/11  21:53:10  bentson
550  * get use of save_car right in transmit interrupt service
551  *
552  * Revision 1.10.1.1  1994/06/11  21:31:18  bentson
553  * add some diagnostic printing; try to fix save_car stuff
554  *
555  * Revision 1.10  1994/06/11  20:36:08  bentson
556  * clean up compiler warnings
557  *
558  * Revision 1.9  1994/06/11  19:42:46  bentson
559  * added a bunch of code to support modem signalling
560  *
561  * Revision 1.8  1994/06/11  17:57:07  bentson
562  * recognize break & parity error
563  *
564  * Revision 1.7  1994/06/05  05:51:34  bentson
565  * Reorder baud table to be monotonic; add cli to CP; discard
566  * incoming characters and status if the line isn't open; start to
567  * fold code into cy_throttle; start to port get_serial_info,
568  * set_serial_info, get_modem_info, set_modem_info, and send_break
569  * from serial.c; expand cy_ioctl; relocate and expand config_setup;
570  * get flow control characters from tty struct; invalidate ports w/o
571  * hardware;
572  *
573  * Revision 1.6  1994/05/31  18:42:21  bentson
574  * add a loop-breaker in the interrupt service routine;
575  * note when port is initialized so that it can be shut
576  * down under the right conditions; receive works without
577  * any obvious errors
578  *
579  * Revision 1.5  1994/05/30  00:55:02  bentson
580  * transmit works without obvious errors
581  *
582  * Revision 1.4  1994/05/27  18:46:27  bentson
583  * incorporated more code from lib_y.c; can now print short
584  * strings under interrupt control to port zero; seems to
585  * select ports/channels/lines correctly
586  *
587  * Revision 1.3  1994/05/25  22:12:44  bentson
588  * shifting from multi-port on a card to proper multiplexor
589  * data structures;  added skeletons of most routines
590  *
591  * Revision 1.2  1994/05/19  13:21:43  bentson
592  * start to crib from other sources
593  *
594  */
595
596 /* If you need to install more boards than NR_CARDS, change the constant
597    in the definition below. No other change is necessary to support up to
598    eight boards. Beyond that you'll have to extend cy_isa_addresses. */
599
600 #define NR_CARDS        4
601
602 /*
603    If the total number of ports is larger than NR_PORTS, change this
604    constant in the definition below. No other change is necessary to
605    support more boards/ports. */
606
607 #define NR_PORTS        256
608
609 #define ZE_V1_NPORTS    64
610 #define ZO_V1   0
611 #define ZO_V2   1
612 #define ZE_V1   2
613
614 #define SERIAL_PARANOIA_CHECK
615 #undef  CY_DEBUG_OPEN
616 #undef  CY_DEBUG_THROTTLE
617 #undef  CY_DEBUG_OTHER
618 #undef  CY_DEBUG_IO
619 #undef  CY_DEBUG_COUNT
620 #undef  CY_DEBUG_DTR
621 #undef  CY_DEBUG_WAIT_UNTIL_SENT
622 #undef  CY_DEBUG_INTERRUPTS
623 #undef  CY_16Y_HACK
624 #undef  CY_ENABLE_MONITORING
625 #undef  CY_PCI_DEBUG
626
627 #if 0
628 #define PAUSE __asm__("nop");
629 #else
630 #define PAUSE ;
631 #endif
632
633 /*
634  * Include section 
635  */
636 #include <linux/config.h>
637 #include <linux/module.h>
638 #include <linux/errno.h>
639 #include <linux/signal.h>
640 #include <linux/sched.h>
641 #include <linux/timer.h>
642 #include <linux/interrupt.h>
643 #include <linux/tty.h>
644 #include <linux/tty_flip.h>
645 #include <linux/serial.h>
646 #include <linux/major.h>
647 #include <linux/string.h>
648 #include <linux/fcntl.h>
649 #include <linux/ptrace.h>
650 #include <linux/cyclades.h>
651 #include <linux/mm.h>
652 #include <linux/ioport.h>
653 #include <linux/init.h>
654 #include <linux/delay.h>
655 #include <linux/spinlock.h>
656 #include <linux/bitops.h>
657
658 #include <asm/system.h>
659 #include <asm/io.h>
660 #include <asm/irq.h>
661 #include <asm/uaccess.h>
662
663 #define CY_LOCK(info,flags)                                     \
664                 do {                                            \
665                 spin_lock_irqsave(&cy_card[info->card].card_lock, flags); \
666                 } while (0)
667                 
668 #define CY_UNLOCK(info,flags)                                   \
669                 do {                                            \
670                 spin_unlock_irqrestore(&cy_card[info->card].card_lock, flags); \
671                 } while (0)
672
673 #include <linux/types.h>
674 #include <linux/kernel.h>
675 #include <linux/pci.h>
676
677 #include <linux/stat.h>
678 #include <linux/proc_fs.h>
679
680 static void cy_throttle (struct tty_struct *tty);
681 static void cy_send_xchar (struct tty_struct *tty, char ch);
682
683 #define IS_CYC_Z(card) ((card).num_chips == -1)
684
685 #define Z_FPGA_CHECK(card) \
686     ((cy_readl(&((struct RUNTIME_9060 __iomem *) \
687                  ((card).ctl_addr))->init_ctrl) & (1<<17)) != 0)
688
689 #define ISZLOADED(card) (((ZO_V1==cy_readl(&((struct RUNTIME_9060 __iomem *) \
690                         ((card).ctl_addr))->mail_box_0)) || \
691                         Z_FPGA_CHECK(card)) && \
692                         (ZFIRM_ID==cy_readl(&((struct FIRM_ID __iomem *) \
693                         ((card).base_addr+ID_ADDRESS))->signature)))
694
695 #ifndef SERIAL_XMIT_SIZE
696 #define SERIAL_XMIT_SIZE        (min(PAGE_SIZE, 4096))
697 #endif
698 #define WAKEUP_CHARS            256
699
700 #define STD_COM_FLAGS (0)
701
702 #define JIFFIES_DIFF(n, j)      ((j) - (n))
703
704 static struct tty_driver *cy_serial_driver;
705
706 #ifdef CONFIG_ISA
707 /* This is the address lookup table. The driver will probe for
708    Cyclom-Y/ISA boards at all addresses in here. If you want the
709    driver to probe addresses at a different address, add it to
710    this table.  If the driver is probing some other board and
711    causing problems, remove the offending address from this table.
712    The cy_setup function extracts additional addresses from the
713    boot options line.  The form is "cyclades=address,address..."
714 */
715
716 static unsigned int cy_isa_addresses[] = {
717         0xD0000,
718         0xD2000,
719         0xD4000,
720         0xD6000,
721         0xD8000,
722         0xDA000,
723         0xDC000,
724         0xDE000,
725         0,0,0,0,0,0,0,0
726 };
727 #define NR_ISA_ADDRS ARRAY_SIZE(cy_isa_addresses)
728
729 #ifdef MODULE
730 static long maddr[NR_CARDS] = { 0, };
731 static int irq[NR_CARDS]  = { 0, };
732
733 module_param_array(maddr, long, NULL, 0);
734 module_param_array(irq, int, NULL, 0);
735 #endif
736
737 #endif /* CONFIG_ISA */
738
739 /* This is the per-card data structure containing address, irq, number of
740    channels, etc. This driver supports a maximum of NR_CARDS cards.
741 */
742 static struct cyclades_card cy_card[NR_CARDS];
743
744 /* This is the per-channel data structure containing pointers, flags
745  and variables for the port. This driver supports a maximum of NR_PORTS.
746 */
747 static struct cyclades_port cy_port[NR_PORTS];
748
749 static int cy_next_channel; /* next minor available */
750
751 /*
752  * tmp_buf is used as a temporary buffer by serial_write.  We need to
753  * lock it in case the copy_from_user blocks while swapping in a page,
754  * and some other program tries to do a serial write at the same time.
755  * Since the lock will only come under contention when the system is
756  * swapping and available memory is low, it makes sense to share one
757  * buffer across all the serial ports, since it significantly saves
758  * memory if large numbers of serial ports are open.  This buffer is
759  * allocated when the first cy_open occurs.
760  */
761 static unsigned char *tmp_buf;
762
763 /*
764  * This is used to look up the divisor speeds and the timeouts
765  * We're normally limited to 15 distinct baud rates.  The extra
766  * are accessed via settings in info->flags.
767  *      0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
768  *     10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
769  *                                               HI            VHI
770  *     20
771  */
772 static int baud_table[] = {
773        0,    50,    75,   110,   134,   150,   200,   300,   600,  1200,
774     1800,  2400,  4800,  9600, 19200, 38400, 57600, 76800,115200,150000,
775   230400,     0};
776
777 static char baud_co_25[] = {  /* 25 MHz clock option table */
778     /* value =>    00    01   02    03    04 */
779     /* divide by    8    32   128   512  2048 */
780     0x00,  0x04,  0x04,  0x04,  0x04,  0x04,  0x03,  0x03,  0x03,  0x02,
781     0x02,  0x02,  0x01,  0x01,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
782
783 static char baud_bpr_25[] = {  /* 25 MHz baud rate period table */
784     0x00,  0xf5,  0xa3,  0x6f,  0x5c,  0x51,  0xf5,  0xa3,  0x51,  0xa3,
785     0x6d,  0x51,  0xa3,  0x51,  0xa3,  0x51,  0x36,  0x29,  0x1b,  0x15};
786
787 static char baud_co_60[] = {  /* 60 MHz clock option table (CD1400 J) */
788     /* value =>    00    01   02    03    04 */
789     /* divide by    8    32   128   512  2048 */
790     0x00,  0x00,  0x00,  0x04,  0x04,  0x04,  0x04,  0x04,  0x03,  0x03,
791     0x03,  0x02,  0x02,  0x01,  0x01,  0x00,  0x00,  0x00,  0x00,  0x00,
792     0x00};
793
794 static char baud_bpr_60[] = {  /* 60 MHz baud rate period table (CD1400 J) */
795     0x00,  0x82,  0x21,  0xff,  0xdb,  0xc3,  0x92,  0x62,  0xc3,  0x62,
796     0x41,  0xc3,  0x62,  0xc3,  0x62,  0xc3,  0x82,  0x62,  0x41,  0x32,
797     0x21};
798
799 static char baud_cor3[] = {  /* receive threshold */
800     0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,
801     0x0a,  0x0a,  0x0a,  0x09,  0x09,  0x08,  0x08,  0x08,  0x08,  0x07,
802     0x07};
803
804 /*
805  * The Cyclades driver implements HW flow control as any serial driver.
806  * The cyclades_port structure member rflow and the vector rflow_thr 
807  * allows us to take advantage of a special feature in the CD1400 to avoid 
808  * data loss even when the system interrupt latency is too high. These flags 
809  * are to be used only with very special applications. Setting these flags 
810  * requires the use of a special cable (DTR and RTS reversed). In the new 
811  * CD1400-based boards (rev. 6.00 or later), there is no need for special 
812  * cables.
813  */
814
815 static char rflow_thr[] = {  /* rflow threshold */
816     0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
817     0x00,  0x00,  0x00,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,
818     0x0a};
819
820 /*  The Cyclom-Ye has placed the sequential chips in non-sequential
821  *  address order.  This look-up table overcomes that problem.
822  */
823 static int cy_chip_offset [] =
824     { 0x0000,
825       0x0400,
826       0x0800,
827       0x0C00,
828       0x0200,
829       0x0600,
830       0x0A00,
831       0x0E00
832     };
833
834 /* PCI related definitions */
835
836 static unsigned short   cy_pci_nboard;
837 static unsigned short   cy_isa_nboard;
838 static unsigned short   cy_nboard;
839 #ifdef CONFIG_PCI
840 static unsigned short   cy_pci_dev_id[] = {
841                             PCI_DEVICE_ID_CYCLOM_Y_Lo,  /* PCI < 1Mb */
842                             PCI_DEVICE_ID_CYCLOM_Y_Hi,  /* PCI > 1Mb */
843                             PCI_DEVICE_ID_CYCLOM_4Y_Lo, /* 4Y PCI < 1Mb */
844                             PCI_DEVICE_ID_CYCLOM_4Y_Hi, /* 4Y PCI > 1Mb */
845                             PCI_DEVICE_ID_CYCLOM_8Y_Lo, /* 8Y PCI < 1Mb */
846                             PCI_DEVICE_ID_CYCLOM_8Y_Hi, /* 8Y PCI > 1Mb */
847                             PCI_DEVICE_ID_CYCLOM_Z_Lo,  /* Z PCI < 1Mb */
848                             PCI_DEVICE_ID_CYCLOM_Z_Hi,  /* Z PCI > 1Mb */
849                             0                           /* end of table */
850                         };
851 #endif
852
853 static void cy_start(struct tty_struct *);
854 static void set_line_char(struct cyclades_port *);
855 static int cyz_issue_cmd(struct cyclades_card *, uclong, ucchar, uclong);
856 #ifdef CONFIG_ISA
857 static unsigned detect_isa_irq(void __iomem *);
858 #endif /* CONFIG_ISA */
859
860 static int cyclades_get_proc_info(char *, char **, off_t , int , int *, void *);
861
862 #ifndef CONFIG_CYZ_INTR
863 static void cyz_poll(unsigned long);
864
865 /* The Cyclades-Z polling cycle is defined by this variable */
866 static long cyz_polling_cycle = CZ_DEF_POLL;
867
868 static int cyz_timeron = 0;
869 static DEFINE_TIMER(cyz_timerlist, cyz_poll, 0, 0);
870
871 #else /* CONFIG_CYZ_INTR */
872 static void cyz_rx_restart(unsigned long);
873 static struct timer_list cyz_rx_full_timer[NR_PORTS];
874 #endif /* CONFIG_CYZ_INTR */
875
876 static inline int
877 serial_paranoia_check(struct cyclades_port *info,
878                         char *name, const char *routine)
879 {
880 #ifdef SERIAL_PARANOIA_CHECK
881     static const char *badmagic =
882         "cyc Warning: bad magic number for serial struct (%s) in %s\n";
883     static const char *badinfo =
884         "cyc Warning: null cyclades_port for (%s) in %s\n";
885     static const char *badrange =
886         "cyc Warning: cyclades_port out of range for (%s) in %s\n";
887
888     if (!info) {
889         printk(badinfo, name, routine);
890         return 1;
891     }
892
893     if( (long)info < (long)(&cy_port[0])
894     || (long)(&cy_port[NR_PORTS]) < (long)info ){
895         printk(badrange, name, routine);
896         return 1;
897     }
898
899     if (info->magic != CYCLADES_MAGIC) {
900         printk(badmagic, name, routine);
901         return 1;
902     }
903 #endif
904         return 0;
905 } /* serial_paranoia_check */
906
907 /*
908  * This routine is used by the interrupt handler to schedule
909  * processing in the software interrupt portion of the driver
910  * (also known as the "bottom half").  This can be called any
911  * number of times for any channel without harm.
912  */
913 static inline void
914 cy_sched_event(struct cyclades_port *info, int event)
915 {
916     info->event |= 1 << event; /* remember what kind of event and who */
917     schedule_work(&info->tqueue);
918 } /* cy_sched_event */
919
920
921 /*
922  * This routine is used to handle the "bottom half" processing for the
923  * serial driver, known also the "software interrupt" processing.
924  * This processing is done at the kernel interrupt level, after the
925  * cy#/_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
926  * is where time-consuming activities which can not be done in the
927  * interrupt driver proper are done; the interrupt driver schedules
928  * them using cy_sched_event(), and they get done here.
929  *
930  * This is done through one level of indirection--the task queue.
931  * When a hardware interrupt service routine wants service by the
932  * driver's bottom half, it enqueues the appropriate tq_struct (one
933  * per port) to the keventd work queue and sets a request flag
934  * that the work queue be processed.
935  *
936  * Although this may seem unwieldy, it gives the system a way to
937  * pass an argument (in this case the pointer to the cyclades_port
938  * structure) to the bottom half of the driver.  Previous kernels
939  * had to poll every port to see if that port needed servicing.
940  */
941 static void
942 do_softint(void *private_)
943 {
944   struct cyclades_port *info = (struct cyclades_port *) private_;
945   struct tty_struct    *tty;
946
947     tty = info->tty;
948     if (!tty)
949         return;
950
951     if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
952         tty_hangup(info->tty);
953         wake_up_interruptible(&info->open_wait);
954         info->flags &= ~ASYNC_NORMAL_ACTIVE;
955     }
956     if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
957         wake_up_interruptible(&info->open_wait);
958     }
959 #ifdef CONFIG_CYZ_INTR
960     if (test_and_clear_bit(Cy_EVENT_Z_RX_FULL, &info->event)) {
961         if (cyz_rx_full_timer[info->line].function == NULL) {
962             cyz_rx_full_timer[info->line].expires = jiffies + 1;
963             cyz_rx_full_timer[info->line].function = cyz_rx_restart;
964             cyz_rx_full_timer[info->line].data = (unsigned long)info;
965             add_timer(&cyz_rx_full_timer[info->line]);
966         }
967     }
968 #endif
969     if (test_and_clear_bit(Cy_EVENT_DELTA_WAKEUP, &info->event)) {
970         wake_up_interruptible(&info->delta_msr_wait);
971     }
972     if (test_and_clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
973         tty_wakeup(tty);
974         wake_up_interruptible(&tty->write_wait);
975     }
976 #ifdef Z_WAKE
977     if (test_and_clear_bit(Cy_EVENT_SHUTDOWN_WAKEUP, &info->event)) {
978         wake_up_interruptible(&info->shutdown_wait);
979     }
980 #endif
981 } /* do_softint */
982
983
984 /***********************************************************/
985 /********* Start of block of Cyclom-Y specific code ********/
986
987 /* This routine waits up to 1000 micro-seconds for the previous
988    command to the Cirrus chip to complete and then issues the
989    new command.  An error is returned if the previous command
990    didn't finish within the time limit.
991
992    This function is only called from inside spinlock-protected code.
993  */
994 static int
995 cyy_issue_cmd(void __iomem *base_addr, u_char cmd, int index)
996 {
997   volatile int  i;
998
999     /* Check to see that the previous command has completed */
1000     for(i = 0 ; i < 100 ; i++){
1001         if (cy_readb(base_addr+(CyCCR<<index)) == 0){
1002             break;
1003         }
1004         udelay(10L);
1005     }
1006     /* if the CCR never cleared, the previous command
1007        didn't finish within the "reasonable time" */
1008     if (i == 100)       return (-1);
1009
1010     /* Issue the new command */
1011     cy_writeb(base_addr+(CyCCR<<index), cmd);
1012
1013     return(0);
1014 } /* cyy_issue_cmd */
1015
1016 #ifdef CONFIG_ISA
1017 /* ISA interrupt detection code */
1018 static unsigned 
1019 detect_isa_irq(void __iomem *address)
1020 {
1021   int irq;
1022   unsigned long irqs, flags;
1023   int save_xir, save_car;
1024   int index = 0; /* IRQ probing is only for ISA */
1025
1026     /* forget possible initially masked and pending IRQ */
1027     irq = probe_irq_off(probe_irq_on());
1028
1029     /* Clear interrupts on the board first */
1030     cy_writeb(address + (Cy_ClrIntr<<index), 0);
1031                               /* Cy_ClrIntr is 0x1800 */
1032
1033     irqs = probe_irq_on();
1034     /* Wait ... */
1035     udelay(5000L);
1036
1037     /* Enable the Tx interrupts on the CD1400 */
1038     local_irq_save(flags);
1039         cy_writeb(address + (CyCAR<<index), 0);
1040         cyy_issue_cmd(address, CyCHAN_CTL|CyENB_XMTR, index);
1041
1042         cy_writeb(address + (CyCAR<<index), 0);
1043         cy_writeb(address + (CySRER<<index), 
1044                 cy_readb(address + (CySRER<<index)) | CyTxRdy);
1045     local_irq_restore(flags);
1046
1047     /* Wait ... */
1048     udelay(5000L);
1049
1050     /* Check which interrupt is in use */
1051     irq = probe_irq_off(irqs);
1052
1053     /* Clean up */
1054     save_xir = (u_char) cy_readb(address + (CyTIR<<index));
1055     save_car = cy_readb(address + (CyCAR<<index));
1056     cy_writeb(address + (CyCAR<<index), (save_xir & 0x3));
1057     cy_writeb(address + (CySRER<<index),
1058         cy_readb(address + (CySRER<<index)) & ~CyTxRdy);
1059     cy_writeb(address + (CyTIR<<index), (save_xir & 0x3f));
1060     cy_writeb(address + (CyCAR<<index), (save_car));
1061     cy_writeb(address + (Cy_ClrIntr<<index), 0);
1062                               /* Cy_ClrIntr is 0x1800 */
1063
1064     return (irq > 0)? irq : 0;
1065 }
1066 #endif /* CONFIG_ISA */
1067
1068 /* The real interrupt service routine is called
1069    whenever the card wants its hand held--chars
1070    received, out buffer empty, modem change, etc.
1071  */
1072 static irqreturn_t
1073 cyy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1074 {
1075   struct tty_struct *tty;
1076   int status;
1077   struct cyclades_card *cinfo;
1078   struct cyclades_port *info;
1079   void __iomem *base_addr, *card_base_addr;
1080   int chip;
1081   int save_xir, channel, save_car;
1082   char data;
1083   volatile int char_count;
1084   int outch;
1085   int i,j,index;
1086   int too_many;
1087   int had_work;
1088   int mdm_change;
1089   int mdm_status;
1090   int len;
1091     if((cinfo = (struct cyclades_card *)dev_id) == 0){
1092 #ifdef CY_DEBUG_INTERRUPTS
1093         printk("cyy_interrupt: spurious interrupt %d\n\r", irq);
1094 #endif
1095         return IRQ_NONE; /* spurious interrupt */
1096     }
1097
1098     card_base_addr = cinfo->base_addr;
1099     index = cinfo->bus_index;
1100
1101
1102     /* This loop checks all chips in the card.  Make a note whenever
1103        _any_ chip had some work to do, as this is considered an
1104        indication that there will be more to do.  Only when no chip
1105        has any work does this outermost loop exit.
1106      */
1107     do{
1108         had_work = 0;
1109         for ( chip = 0 ; chip < cinfo->num_chips ; chip ++) {
1110             base_addr = cinfo->base_addr + (cy_chip_offset[chip]<<index);
1111             too_many = 0;
1112             while ( (status = cy_readb(base_addr+(CySVRR<<index))) != 0x00) {
1113                 had_work++;
1114                 /* The purpose of the following test is to ensure that
1115                    no chip can monopolize the driver.  This forces the
1116                    chips to be checked in a round-robin fashion (after
1117                    draining each of a bunch (1000) of characters).
1118                  */
1119                 if(1000<too_many++){
1120                     break;
1121                 }
1122                 if (status & CySRReceive) { /* reception interrupt */
1123 #ifdef CY_DEBUG_INTERRUPTS
1124                     printk("cyy_interrupt: rcvd intr, chip %d\n\r", chip);
1125 #endif
1126                     /* determine the channel & change to that context */
1127                     spin_lock(&cinfo->card_lock);
1128                     save_xir = (u_char) cy_readb(base_addr+(CyRIR<<index));
1129                     channel = (u_short ) (save_xir & CyIRChannel);
1130                     i = channel + chip * 4 + cinfo->first_line;
1131                     info = &cy_port[i];
1132                     info->last_active = jiffies;
1133                     save_car = cy_readb(base_addr+(CyCAR<<index));
1134                     cy_writeb(base_addr+(CyCAR<<index), save_xir);
1135
1136                     /* if there is nowhere to put the data, discard it */
1137                     if(info->tty == 0){
1138                         j = (cy_readb(base_addr+(CyRIVR<<index)) & CyIVRMask);
1139                         if ( j == CyIVRRxEx ) { /* exception */
1140                             data = cy_readb(base_addr+(CyRDSR<<index));
1141                         } else { /* normal character reception */
1142                             char_count = cy_readb(base_addr+(CyRDCR<<index));
1143                             while(char_count--){
1144                                 data = cy_readb(base_addr+(CyRDSR<<index));
1145                             }
1146                         }
1147                     }else{ /* there is an open port for this data */
1148                         tty = info->tty;
1149                         j = (cy_readb(base_addr+(CyRIVR<<index)) & CyIVRMask);
1150                         if ( j == CyIVRRxEx ) { /* exception */
1151                             data = cy_readb(base_addr+(CyRDSR<<index));
1152
1153                             /* For statistics only */
1154                             if (data & CyBREAK)
1155                                 info->icount.brk++;
1156                             else if(data & CyFRAME)
1157                                 info->icount.frame++;
1158                             else if(data & CyPARITY)
1159                                 info->icount.parity++;
1160                             else if(data & CyOVERRUN)
1161                                 info->icount.overrun++;
1162
1163                             if(data & info->ignore_status_mask){
1164                                 info->icount.rx++;
1165                                 continue;
1166                             }
1167                             if (tty_buffer_request_room(tty, 1)) {
1168                                 if (data & info->read_status_mask){
1169                                     if(data & CyBREAK){
1170                                         tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_BREAK);
1171                                         info->icount.rx++;
1172                                         if (info->flags & ASYNC_SAK){
1173                                             do_SAK(tty);
1174                                         }
1175                                     }else if(data & CyFRAME){
1176                                         tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_FRAME);
1177                                         info->icount.rx++;
1178                                         info->idle_stats.frame_errs++;
1179                                     }else if(data & CyPARITY){
1180                                         /* Pieces of seven... */
1181                                         tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_PARITY);
1182                                         info->icount.rx++;
1183                                         info->idle_stats.parity_errs++;
1184                                     }else if(data & CyOVERRUN){
1185                                         tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1186                                         info->icount.rx++;
1187                                         /* If the flip buffer itself is
1188                                            overflowing, we still lose
1189                                            the next incoming character.
1190                                          */
1191                                         tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_FRAME);
1192                                         info->icount.rx++;
1193                                         info->idle_stats.overruns++;
1194                                     /* These two conditions may imply */
1195                                     /* a normal read should be done. */
1196                                     /* }else if(data & CyTIMEOUT){ */
1197                                     /* }else if(data & CySPECHAR){ */
1198                                     }else {
1199                                         tty_insert_flip_char(tty, 0, TTY_NORMAL);
1200                                         info->icount.rx++;
1201                                     }
1202                                 }else{
1203                                     tty_insert_flip_char(tty, 0, TTY_NORMAL);
1204                                     info->icount.rx++;
1205                                 }
1206                             }else{
1207                                 /* there was a software buffer
1208                                    overrun and nothing could be
1209                                    done about it!!! */
1210                                 info->icount.buf_overrun++;
1211                                 info->idle_stats.overruns++;
1212                             }
1213                         } else { /* normal character reception */
1214                             /* load # chars available from the chip */
1215                             char_count = cy_readb(base_addr+(CyRDCR<<index));
1216
1217 #ifdef CY_ENABLE_MONITORING
1218                             ++info->mon.int_count;
1219                             info->mon.char_count += char_count;
1220                             if (char_count > info->mon.char_max)
1221                                info->mon.char_max = char_count;
1222                             info->mon.char_last = char_count;
1223 #endif
1224                             len = tty_buffer_request_room(tty, char_count);
1225                             while(len--){
1226                                 data = cy_readb(base_addr+(CyRDSR<<index));
1227                                 tty_insert_flip_char(tty, data, TTY_NORMAL);
1228                                 info->idle_stats.recv_bytes++;
1229                                 info->icount.rx++;
1230 #ifdef CY_16Y_HACK
1231                                 udelay(10L);
1232 #endif
1233                             }
1234                              info->idle_stats.recv_idle = jiffies;
1235                         }
1236                         schedule_delayed_work(&tty->buf.work, 1);
1237                     }
1238                     /* end of service */
1239                     cy_writeb(base_addr+(CyRIR<<index), (save_xir & 0x3f));
1240                     cy_writeb(base_addr+(CyCAR<<index), (save_car));
1241                     spin_unlock(&cinfo->card_lock);
1242                 }
1243
1244
1245                 if (status & CySRTransmit) { /* transmission interrupt */
1246                     /* Since we only get here when the transmit buffer
1247                        is empty, we know we can always stuff a dozen
1248                        characters. */
1249 #ifdef CY_DEBUG_INTERRUPTS
1250                     printk("cyy_interrupt: xmit intr, chip %d\n\r", chip);
1251 #endif
1252
1253                     /* determine the channel & change to that context */
1254                     spin_lock(&cinfo->card_lock);
1255                     save_xir = (u_char) cy_readb(base_addr+(CyTIR<<index));
1256                     channel = (u_short ) (save_xir & CyIRChannel);
1257                     i = channel + chip * 4 + cinfo->first_line;
1258                     save_car = cy_readb(base_addr+(CyCAR<<index));
1259                     cy_writeb(base_addr+(CyCAR<<index), save_xir);
1260
1261                     /* validate the port# (as configured and open) */
1262                     if( (i < 0) || (NR_PORTS <= i) ){
1263                         cy_writeb(base_addr+(CySRER<<index),
1264                              cy_readb(base_addr+(CySRER<<index)) & ~CyTxRdy);
1265                         goto txend;
1266                     }
1267                     info = &cy_port[i];
1268                     info->last_active = jiffies;
1269                     if(info->tty == 0){
1270                         cy_writeb(base_addr+(CySRER<<index),
1271                              cy_readb(base_addr+(CySRER<<index)) & ~CyTxRdy);
1272                         goto txdone;
1273                     }
1274
1275                     /* load the on-chip space for outbound data */
1276                     char_count = info->xmit_fifo_size;
1277
1278                     if(info->x_char) { /* send special char */
1279                         outch = info->x_char;
1280                         cy_writeb(base_addr+(CyTDR<<index), outch);
1281                         char_count--;
1282                         info->icount.tx++;
1283                         info->x_char = 0;
1284                     }
1285
1286                     if (info->breakon || info->breakoff) {
1287                         if (info->breakon) {
1288                             cy_writeb(base_addr + (CyTDR<<index), 0); 
1289                             cy_writeb(base_addr + (CyTDR<<index), 0x81);
1290                             info->breakon = 0;
1291                             char_count -= 2;
1292                         }
1293                         if (info->breakoff) {
1294                             cy_writeb(base_addr + (CyTDR<<index), 0); 
1295                             cy_writeb(base_addr + (CyTDR<<index), 0x83);
1296                             info->breakoff = 0;
1297                             char_count -= 2;
1298                         }
1299                     }
1300
1301                     while (char_count-- > 0){
1302                         if (!info->xmit_cnt){
1303                             if (cy_readb(base_addr+(CySRER<<index))&CyTxMpty) {
1304                                 cy_writeb(base_addr+(CySRER<<index),
1305                                           cy_readb(base_addr+(CySRER<<index)) &
1306                                           ~CyTxMpty);
1307                             } else {
1308                                 cy_writeb(base_addr+(CySRER<<index),
1309                                           ((cy_readb(base_addr+(CySRER<<index))
1310                                             & ~CyTxRdy)
1311                                            | CyTxMpty));
1312                             }
1313                             goto txdone;
1314                         }
1315                         if (info->xmit_buf == 0){
1316                             cy_writeb(base_addr+(CySRER<<index),
1317                                 cy_readb(base_addr+(CySRER<<index)) & 
1318                                         ~CyTxRdy);
1319                             goto txdone;
1320                         }
1321                         if (info->tty->stopped || info->tty->hw_stopped){
1322                             cy_writeb(base_addr+(CySRER<<index),
1323                                 cy_readb(base_addr+(CySRER<<index)) & 
1324                                         ~CyTxRdy);
1325                             goto txdone;
1326                         }
1327                         /* Because the Embedded Transmit Commands have
1328                            been enabled, we must check to see if the
1329                            escape character, NULL, is being sent.  If it
1330                            is, we must ensure that there is room for it
1331                            to be doubled in the output stream.  Therefore
1332                            we no longer advance the pointer when the
1333                            character is fetched, but rather wait until
1334                            after the check for a NULL output character.
1335                            This is necessary because there may not be
1336                            room for the two chars needed to send a NULL.)
1337                          */
1338                         outch = info->xmit_buf[info->xmit_tail];
1339                         if( outch ){
1340                             info->xmit_cnt--;
1341                             info->xmit_tail = (info->xmit_tail + 1)
1342                                                       & (SERIAL_XMIT_SIZE - 1);
1343                             cy_writeb(base_addr+(CyTDR<<index), outch);
1344                             info->icount.tx++;
1345                         }else{
1346                             if(char_count > 1){
1347                                 info->xmit_cnt--;
1348                                 info->xmit_tail = (info->xmit_tail + 1)
1349                                                       & (SERIAL_XMIT_SIZE - 1);
1350                                 cy_writeb(base_addr+(CyTDR<<index), 
1351                                           outch);
1352                                 cy_writeb(base_addr+(CyTDR<<index), 0);
1353                                 info->icount.tx++;
1354                                 char_count--;
1355                             }else{
1356                             }
1357                         }
1358                     }
1359
1360         txdone:
1361                     if (info->xmit_cnt < WAKEUP_CHARS) {
1362                         cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
1363                     }
1364         txend:
1365                     /* end of service */
1366                     cy_writeb(base_addr+(CyTIR<<index), 
1367                               (save_xir & 0x3f));
1368                     cy_writeb(base_addr+(CyCAR<<index), (save_car));
1369                     spin_unlock(&cinfo->card_lock);
1370                 }
1371
1372                 if (status & CySRModem) {        /* modem interrupt */
1373
1374                     /* determine the channel & change to that context */
1375                     spin_lock(&cinfo->card_lock);
1376                     save_xir = (u_char) cy_readb(base_addr+(CyMIR<<index));
1377                     channel = (u_short ) (save_xir & CyIRChannel);
1378                     info = &cy_port[channel + chip * 4
1379                                            + cinfo->first_line];
1380                     info->last_active = jiffies;
1381                     save_car = cy_readb(base_addr+(CyCAR<<index));
1382                     cy_writeb(base_addr+(CyCAR<<index), save_xir);
1383
1384                     mdm_change = cy_readb(base_addr+(CyMISR<<index));
1385                     mdm_status = cy_readb(base_addr+(CyMSVR1<<index));
1386
1387                     if(info->tty == 0){/* no place for data, ignore it*/
1388                         ;
1389                     }else{
1390                         if (mdm_change & CyANY_DELTA) {
1391                             /* For statistics only */
1392                             if (mdm_change & CyDCD)     info->icount.dcd++;
1393                             if (mdm_change & CyCTS)     info->icount.cts++;
1394                             if (mdm_change & CyDSR)     info->icount.dsr++;
1395                             if (mdm_change & CyRI)      info->icount.rng++;
1396
1397                             cy_sched_event(info, Cy_EVENT_DELTA_WAKEUP);
1398                         }
1399
1400                         if((mdm_change & CyDCD)
1401                         && (info->flags & ASYNC_CHECK_CD)){
1402                             if(mdm_status & CyDCD){
1403                                 cy_sched_event(info,
1404                                     Cy_EVENT_OPEN_WAKEUP);
1405                             }else{
1406                                 cy_sched_event(info,
1407                                     Cy_EVENT_HANGUP);
1408                             }
1409                         }
1410                         if((mdm_change & CyCTS)
1411                         && (info->flags & ASYNC_CTS_FLOW)){
1412                             if(info->tty->hw_stopped){
1413                                 if(mdm_status & CyCTS){
1414                                     /* cy_start isn't used
1415                                          because... !!! */
1416                                     info->tty->hw_stopped = 0;
1417                                   cy_writeb(base_addr+(CySRER<<index),
1418                                        cy_readb(base_addr+(CySRER<<index)) | 
1419                                        CyTxRdy);
1420                                     cy_sched_event(info,
1421                                         Cy_EVENT_WRITE_WAKEUP);
1422                                 }
1423                             }else{
1424                                 if(!(mdm_status & CyCTS)){
1425                                     /* cy_stop isn't used
1426                                          because ... !!! */
1427                                     info->tty->hw_stopped = 1;
1428                                   cy_writeb(base_addr+(CySRER<<index),
1429                                        cy_readb(base_addr+(CySRER<<index)) & 
1430                                        ~CyTxRdy);
1431                                 }
1432                             }
1433                         }
1434                         if(mdm_change & CyDSR){
1435                         }
1436                         if(mdm_change & CyRI){
1437                         }
1438                     }
1439                     /* end of service */
1440                     cy_writeb(base_addr+(CyMIR<<index), 
1441                               (save_xir & 0x3f));
1442                     cy_writeb(base_addr+(CyCAR<<index), save_car);
1443                     spin_unlock(&cinfo->card_lock);
1444                 }
1445             }          /* end while status != 0 */
1446         }            /* end loop for chips... */
1447     } while(had_work);
1448
1449    /* clear interrupts */
1450    spin_lock(&cinfo->card_lock);
1451    cy_writeb(card_base_addr + (Cy_ClrIntr<<index), 0);
1452                                 /* Cy_ClrIntr is 0x1800 */
1453    spin_unlock(&cinfo->card_lock);
1454    return IRQ_HANDLED;
1455 } /* cyy_interrupt */
1456
1457 /***********************************************************/
1458 /********* End of block of Cyclom-Y specific code **********/
1459 /******** Start of block of Cyclades-Z specific code *********/
1460 /***********************************************************/
1461
1462 static int
1463 cyz_fetch_msg( struct cyclades_card *cinfo,
1464             uclong *channel, ucchar *cmd, uclong *param)
1465 {
1466   struct FIRM_ID __iomem *firm_id;
1467   struct ZFW_CTRL __iomem *zfw_ctrl;
1468   struct BOARD_CTRL __iomem *board_ctrl;
1469   unsigned long loc_doorbell;
1470
1471     firm_id = cinfo->base_addr + ID_ADDRESS;
1472     if (!ISZLOADED(*cinfo)){
1473         return (-1);
1474     }
1475     zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1476     board_ctrl = &zfw_ctrl->board_ctrl;
1477
1478     loc_doorbell = cy_readl(&((struct RUNTIME_9060 __iomem *)
1479                      (cinfo->ctl_addr))->loc_doorbell);
1480     if (loc_doorbell){
1481         *cmd = (char)(0xff & loc_doorbell);
1482         *channel = cy_readl(&board_ctrl->fwcmd_channel);
1483         *param = (uclong)cy_readl(&board_ctrl->fwcmd_param);
1484         cy_writel(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->loc_doorbell, 
1485                  0xffffffff);
1486         return 1;
1487     }
1488     return 0;
1489 } /* cyz_fetch_msg */
1490
1491 static int
1492 cyz_issue_cmd( struct cyclades_card *cinfo,
1493             uclong channel, ucchar cmd, uclong param)
1494 {
1495   struct FIRM_ID __iomem *firm_id;
1496   struct ZFW_CTRL __iomem *zfw_ctrl;
1497   struct BOARD_CTRL __iomem *board_ctrl;
1498   unsigned long __iomem *pci_doorbell;
1499   int index;
1500
1501     firm_id = cinfo->base_addr + ID_ADDRESS;
1502     if (!ISZLOADED(*cinfo)){
1503         return (-1);
1504     }
1505     zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1506     board_ctrl = &zfw_ctrl->board_ctrl;
1507
1508     index = 0;
1509     pci_doorbell = &((struct RUNTIME_9060 __iomem *) (cinfo->ctl_addr))->pci_doorbell;
1510     while( (cy_readl(pci_doorbell) & 0xff) != 0){
1511         if (index++ == 1000){
1512             return((int)(cy_readl(pci_doorbell) & 0xff));
1513         }
1514         udelay(50L);
1515     }
1516     cy_writel(&board_ctrl->hcmd_channel, channel);
1517     cy_writel(&board_ctrl->hcmd_param , param);
1518     cy_writel(pci_doorbell, (long)cmd);
1519
1520     return(0);
1521 } /* cyz_issue_cmd */
1522
1523 static void
1524 cyz_handle_rx(struct cyclades_port *info,
1525               volatile struct CH_CTRL __iomem *ch_ctrl,
1526               volatile struct BUF_CTRL __iomem *buf_ctrl)
1527 {
1528   struct cyclades_card *cinfo = &cy_card[info->card];
1529   struct tty_struct *tty = info->tty;
1530   volatile int char_count;
1531   int len;
1532 #ifdef BLOCKMOVE
1533   int small_count;
1534 #else
1535   char data;
1536 #endif
1537   volatile uclong rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
1538
1539     rx_get = new_rx_get = cy_readl(&buf_ctrl->rx_get);
1540     rx_put = cy_readl(&buf_ctrl->rx_put);
1541     rx_bufsize = cy_readl(&buf_ctrl->rx_bufsize);
1542     rx_bufaddr = cy_readl(&buf_ctrl->rx_bufaddr);
1543     if (rx_put >= rx_get)
1544         char_count = rx_put - rx_get;
1545     else
1546         char_count = rx_put - rx_get + rx_bufsize;
1547
1548     if ( char_count ) {
1549         info->last_active = jiffies;
1550         info->jiffies[1] = jiffies;
1551
1552 #ifdef CY_ENABLE_MONITORING
1553         info->mon.int_count++;
1554         info->mon.char_count += char_count;
1555         if (char_count > info->mon.char_max)
1556             info->mon.char_max = char_count;
1557         info->mon.char_last = char_count;
1558 #endif
1559         if(tty == 0){
1560             /* flush received characters */
1561             new_rx_get = (new_rx_get + char_count) & (rx_bufsize - 1);
1562             info->rflush_count++;
1563         }else{
1564 #ifdef BLOCKMOVE
1565             /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
1566                for performance, but because of buffer boundaries, there
1567                may be several steps to the operation */
1568             while(0 < (small_count = 
1569                        min_t(unsigned int, (rx_bufsize - new_rx_get),
1570                        min_t(unsigned int, (TTY_FLIPBUF_SIZE - tty->flip.count), char_count))
1571                  )) {
1572                 memcpy_fromio(tty->flip.char_buf_ptr,
1573                               (char *)(cinfo->base_addr
1574                                        + rx_bufaddr + new_rx_get),
1575                               small_count);
1576
1577                 tty->flip.char_buf_ptr += small_count;
1578                 memset(tty->flip.flag_buf_ptr, TTY_NORMAL, small_count);
1579                 tty->flip.flag_buf_ptr += small_count;
1580                 new_rx_get = (new_rx_get + small_count) & (rx_bufsize - 1);
1581                 char_count -= small_count;
1582                 info->icount.rx += small_count;
1583                 info->idle_stats.recv_bytes += small_count;
1584                 tty->flip.count += small_count;
1585             }
1586 #else
1587             len = tty_buffer_request_room(tty, char_count);
1588             while(len--){
1589                 data = cy_readb(cinfo->base_addr + rx_bufaddr + new_rx_get);
1590                 new_rx_get = (new_rx_get + 1) & (rx_bufsize - 1);
1591                 tty_insert_flip_char(tty, data, TTY_NORMAL);
1592                 info->idle_stats.recv_bytes++;
1593                 info->icount.rx++;
1594             }
1595 #endif
1596 #ifdef CONFIG_CYZ_INTR
1597             /* Recalculate the number of chars in the RX buffer and issue
1598                a cmd in case it's higher than the RX high water mark */
1599             rx_put = cy_readl(&buf_ctrl->rx_put);
1600             if (rx_put >= rx_get)
1601                 char_count = rx_put - rx_get;
1602             else
1603                 char_count = rx_put - rx_get + rx_bufsize;
1604             if(char_count >= cy_readl(&buf_ctrl->rx_threshold)) {
1605                 cy_sched_event(info, Cy_EVENT_Z_RX_FULL);
1606             }
1607 #endif
1608             info->idle_stats.recv_idle = jiffies;
1609             schedule_delayed_work(&tty->buf.work, 1);
1610         }
1611         /* Update rx_get */
1612         cy_writel(&buf_ctrl->rx_get, new_rx_get);
1613     }
1614 }
1615
1616 static void
1617 cyz_handle_tx(struct cyclades_port *info,
1618               volatile struct CH_CTRL __iomem *ch_ctrl,
1619               volatile struct BUF_CTRL __iomem *buf_ctrl)
1620 {
1621   struct cyclades_card *cinfo = &cy_card[info->card];
1622   struct tty_struct *tty = info->tty;
1623   char data;
1624   volatile int char_count;
1625 #ifdef BLOCKMOVE
1626   int small_count;
1627 #endif
1628   volatile uclong tx_put, tx_get, tx_bufsize, tx_bufaddr;
1629
1630     if (info->xmit_cnt <= 0)    /* Nothing to transmit */
1631         return;
1632
1633     tx_get = cy_readl(&buf_ctrl->tx_get);
1634     tx_put = cy_readl(&buf_ctrl->tx_put);
1635     tx_bufsize = cy_readl(&buf_ctrl->tx_bufsize);
1636     tx_bufaddr = cy_readl(&buf_ctrl->tx_bufaddr);
1637     if (tx_put >= tx_get)
1638         char_count = tx_get - tx_put - 1 + tx_bufsize;
1639     else
1640         char_count = tx_get - tx_put - 1;
1641
1642     if ( char_count ) {
1643
1644         if( tty == 0 ){
1645             goto ztxdone;
1646         }
1647
1648         if(info->x_char) { /* send special char */
1649             data = info->x_char;
1650
1651             cy_writeb((cinfo->base_addr + tx_bufaddr + tx_put), data);
1652             tx_put = (tx_put + 1) & (tx_bufsize - 1);
1653             info->x_char = 0;
1654             char_count--;
1655             info->icount.tx++;
1656             info->last_active = jiffies;
1657             info->jiffies[2] = jiffies;
1658         }
1659 #ifdef BLOCKMOVE
1660         while(0 < (small_count = 
1661                    min_t(unsigned int, (tx_bufsize - tx_put),
1662                        min_t(unsigned int, (SERIAL_XMIT_SIZE - info->xmit_tail),
1663                            min_t(unsigned int, info->xmit_cnt, char_count))))) {
1664
1665             memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr + tx_put),
1666                         &info->xmit_buf[info->xmit_tail],
1667                         small_count);
1668
1669             tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1670             char_count -= small_count;
1671             info->icount.tx += small_count;
1672             info->xmit_cnt -= small_count;
1673             info->xmit_tail = 
1674                 (info->xmit_tail + small_count) & (SERIAL_XMIT_SIZE - 1);
1675             info->last_active = jiffies;
1676             info->jiffies[2] = jiffies;
1677         }
1678 #else
1679         while (info->xmit_cnt && char_count){
1680             data = info->xmit_buf[info->xmit_tail];
1681             info->xmit_cnt--;
1682             info->xmit_tail = (info->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
1683
1684             cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1685             tx_put = (tx_put + 1) & (tx_bufsize - 1);
1686             char_count--;
1687             info->icount.tx++;
1688             info->last_active = jiffies;
1689             info->jiffies[2] = jiffies;
1690         }
1691 #endif
1692     ztxdone:
1693         if (info->xmit_cnt < WAKEUP_CHARS) {
1694             cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
1695         }
1696         /* Update tx_put */
1697         cy_writel(&buf_ctrl->tx_put, tx_put);
1698     }
1699 }
1700
1701 static void
1702 cyz_handle_cmd(struct cyclades_card *cinfo)
1703 {
1704   struct tty_struct *tty;
1705   struct cyclades_port *info;
1706   static volatile struct FIRM_ID __iomem *firm_id;
1707   static volatile struct ZFW_CTRL __iomem *zfw_ctrl;
1708   static volatile struct BOARD_CTRL __iomem *board_ctrl;
1709   static volatile struct CH_CTRL __iomem *ch_ctrl;
1710   static volatile struct BUF_CTRL __iomem *buf_ctrl;
1711   uclong channel;
1712   ucchar cmd;
1713   uclong param;
1714   uclong hw_ver, fw_ver;
1715   int special_count;
1716   int delta_count;
1717
1718     firm_id = cinfo->base_addr + ID_ADDRESS;
1719     zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1720     board_ctrl = &zfw_ctrl->board_ctrl;
1721     fw_ver = cy_readl(&board_ctrl->fw_version);
1722     hw_ver = cy_readl(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->mail_box_0);
1723
1724
1725     while(cyz_fetch_msg(cinfo, &channel, &cmd, &param) == 1) {
1726         special_count = 0;
1727         delta_count = 0;
1728         info = &cy_port[channel + cinfo->first_line];
1729         if((tty = info->tty) == 0) {
1730             continue;
1731         }
1732         ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
1733         buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
1734
1735         switch(cmd) {
1736             case C_CM_PR_ERROR:
1737                 tty_insert_flip_char(tty, 0, TTY_PARITY);
1738                 info->icount.rx++;
1739                 special_count++;
1740                 break;
1741             case C_CM_FR_ERROR:
1742                 tty_insert_flip_char(tty, 0, TTY_FRAME);
1743                 info->icount.rx++;
1744                 special_count++;
1745                 break;
1746             case C_CM_RXBRK:
1747                 tty_insert_flip_char(tty, 0, TTY_BREAK);
1748                 info->icount.rx++;
1749                 special_count++;
1750                 break;
1751             case C_CM_MDCD:
1752                 info->icount.dcd++;
1753                 delta_count++;
1754                 if (info->flags & ASYNC_CHECK_CD){
1755                     if ((fw_ver > 241 ? 
1756                           ((u_long)param) : 
1757                           cy_readl(&ch_ctrl->rs_status)) & C_RS_DCD) {
1758                         cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
1759                     }else{
1760                         cy_sched_event(info, Cy_EVENT_HANGUP);
1761                     }
1762                 }
1763                 break;
1764             case C_CM_MCTS:
1765                 info->icount.cts++;
1766                 delta_count++;
1767                 break;
1768             case C_CM_MRI:
1769                 info->icount.rng++;
1770                 delta_count++;
1771                 break;
1772             case C_CM_MDSR:
1773                 info->icount.dsr++;
1774                 delta_count++;
1775                 break;
1776 #ifdef Z_WAKE
1777             case C_CM_IOCTLW:
1778                 cy_sched_event(info, Cy_EVENT_SHUTDOWN_WAKEUP);
1779                 break;
1780 #endif
1781 #ifdef CONFIG_CYZ_INTR
1782             case C_CM_RXHIWM:
1783             case C_CM_RXNNDT:
1784             case C_CM_INTBACK2:
1785                 /* Reception Interrupt */
1786 #ifdef CY_DEBUG_INTERRUPTS
1787                 printk("cyz_interrupt: rcvd intr, card %d, port %ld\n\r", 
1788                         info->card, channel);
1789 #endif
1790                 cyz_handle_rx(info, ch_ctrl, buf_ctrl);
1791                 break;
1792             case C_CM_TXBEMPTY:
1793             case C_CM_TXLOWWM:
1794             case C_CM_INTBACK:
1795                 /* Transmission Interrupt */
1796 #ifdef CY_DEBUG_INTERRUPTS
1797                 printk("cyz_interrupt: xmit intr, card %d, port %ld\n\r", 
1798                         info->card, channel);
1799 #endif
1800                 cyz_handle_tx(info, ch_ctrl, buf_ctrl);
1801                 break;
1802 #endif /* CONFIG_CYZ_INTR */
1803             case C_CM_FATAL:
1804                 /* should do something with this !!! */
1805                 break;
1806             default:
1807                 break;
1808         }
1809         if(delta_count)
1810             cy_sched_event(info, Cy_EVENT_DELTA_WAKEUP);
1811         if(special_count)
1812             schedule_delayed_work(&tty->buf.work, 1);
1813     }
1814 }
1815
1816 #ifdef CONFIG_CYZ_INTR
1817 static irqreturn_t
1818 cyz_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1819 {
1820   struct cyclades_card *cinfo;
1821
1822     if((cinfo = (struct cyclades_card *)dev_id) == 0){
1823 #ifdef CY_DEBUG_INTERRUPTS
1824         printk("cyz_interrupt: spurious interrupt %d\n\r", irq);
1825 #endif
1826         return IRQ_NONE; /* spurious interrupt */
1827     }
1828
1829     if (!ISZLOADED(*cinfo)) {
1830 #ifdef CY_DEBUG_INTERRUPTS
1831         printk("cyz_interrupt: board not yet loaded (IRQ%d).\n\r", irq);
1832 #endif
1833         return IRQ_NONE;
1834     }
1835
1836     /* Handle the interrupts */
1837     cyz_handle_cmd(cinfo);
1838
1839     return IRQ_HANDLED;
1840 } /* cyz_interrupt */
1841
1842 static void
1843 cyz_rx_restart(unsigned long arg)
1844 {
1845     struct cyclades_port *info = (struct cyclades_port *)arg;
1846     int retval;
1847     int card = info->card;
1848     uclong channel = (info->line) - (cy_card[card].first_line);
1849     unsigned long flags;
1850
1851     CY_LOCK(info, flags);
1852     retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_INTBACK2, 0L);
1853     if (retval != 0){
1854         printk("cyc:cyz_rx_restart retval on ttyC%d was %x\n", 
1855                info->line, retval);
1856     }
1857     cyz_rx_full_timer[info->line].function = NULL;
1858     CY_UNLOCK(info, flags);
1859 }
1860
1861 #else /* CONFIG_CYZ_INTR */
1862
1863 static void
1864 cyz_poll(unsigned long arg)
1865 {
1866   struct cyclades_card *cinfo;
1867   struct cyclades_port *info;
1868   struct tty_struct *tty;
1869   static volatile struct FIRM_ID *firm_id;
1870   static volatile struct ZFW_CTRL *zfw_ctrl;
1871   static volatile struct BOARD_CTRL *board_ctrl;
1872   static volatile struct CH_CTRL *ch_ctrl;
1873   static volatile struct BUF_CTRL *buf_ctrl;
1874   int card, port;
1875
1876     cyz_timerlist.expires = jiffies + (HZ);
1877     for (card = 0 ; card < NR_CARDS ; card++){
1878         cinfo = &cy_card[card];
1879
1880         if (!IS_CYC_Z(*cinfo)) continue;
1881         if (!ISZLOADED(*cinfo)) continue;
1882
1883         firm_id = cinfo->base_addr + ID_ADDRESS;
1884         zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1885         board_ctrl = &(zfw_ctrl->board_ctrl);
1886
1887         /* Skip first polling cycle to avoid racing conditions with the FW */
1888         if (!cinfo->intr_enabled) {
1889             cinfo->nports = (int) cy_readl(&board_ctrl->n_channel);
1890             cinfo->intr_enabled = 1;
1891             continue;
1892         }
1893
1894         cyz_handle_cmd(cinfo);
1895
1896         for (port = 0 ; port < cinfo->nports ; port++) {
1897             info = &cy_port[ port + cinfo->first_line ];
1898             tty = info->tty;
1899             ch_ctrl = &(zfw_ctrl->ch_ctrl[port]);
1900             buf_ctrl = &(zfw_ctrl->buf_ctrl[port]);
1901
1902             if (!info->throttle)
1903                 cyz_handle_rx(info, ch_ctrl, buf_ctrl);
1904             cyz_handle_tx(info, ch_ctrl, buf_ctrl);
1905         }
1906         /* poll every 'cyz_polling_cycle' period */
1907         cyz_timerlist.expires = jiffies + cyz_polling_cycle;
1908     }
1909     add_timer(&cyz_timerlist);
1910
1911     return;
1912 } /* cyz_poll */
1913
1914 #endif /* CONFIG_CYZ_INTR */
1915
1916 /********** End of block of Cyclades-Z specific code *********/
1917 /***********************************************************/
1918
1919
1920 /* This is called whenever a port becomes active;
1921    interrupts are enabled and DTR & RTS are turned on.
1922  */
1923 static int
1924 startup(struct cyclades_port * info)
1925 {
1926   unsigned long flags;
1927   int retval = 0;
1928   void __iomem *base_addr;
1929   int card,chip,channel,index;
1930   unsigned long page;
1931
1932     card = info->card;
1933     channel = (info->line) - (cy_card[card].first_line);
1934
1935     page = get_zeroed_page(GFP_KERNEL);
1936     if (!page)
1937         return -ENOMEM;
1938
1939     CY_LOCK(info, flags);
1940
1941     if (info->flags & ASYNC_INITIALIZED){
1942         free_page(page);
1943         goto errout;
1944     }
1945
1946     if (!info->type){
1947         if (info->tty){
1948             set_bit(TTY_IO_ERROR, &info->tty->flags);
1949         }
1950         free_page(page);
1951         goto errout;
1952     }
1953
1954     if (info->xmit_buf)
1955         free_page(page);
1956     else
1957         info->xmit_buf = (unsigned char *) page;
1958
1959     CY_UNLOCK(info, flags);
1960
1961     set_line_char(info);
1962
1963     if (!IS_CYC_Z(cy_card[card])) {
1964         chip = channel>>2;
1965         channel &= 0x03;
1966         index = cy_card[card].bus_index;
1967         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
1968
1969 #ifdef CY_DEBUG_OPEN
1970         printk("cyc startup card %d, chip %d, channel %d, base_addr %lx\n",
1971              card, chip, channel, (long)base_addr);/**/
1972 #endif
1973
1974         CY_LOCK(info, flags);
1975
1976         cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
1977
1978         cy_writeb(base_addr+(CyRTPR<<index), (info->default_timeout
1979                  ? info->default_timeout : 0x02)); /* 10ms rx timeout */
1980
1981         cyy_issue_cmd(base_addr,CyCHAN_CTL|CyENB_RCVR|CyENB_XMTR,index);
1982
1983         cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
1984         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
1985         cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
1986
1987 #ifdef CY_DEBUG_DTR
1988         printk("cyc:startup raising DTR\n");
1989         printk("     status: 0x%x, 0x%x\n",
1990                 cy_readb(base_addr+(CyMSVR1<<index)), 
1991                 cy_readb(base_addr+(CyMSVR2<<index)));
1992 #endif
1993
1994         cy_writeb(base_addr+(CySRER<<index),
1995                 cy_readb(base_addr+(CySRER<<index)) | CyRxData);
1996         info->flags |= ASYNC_INITIALIZED;
1997
1998         if (info->tty){
1999             clear_bit(TTY_IO_ERROR, &info->tty->flags);
2000         }
2001         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2002         info->breakon = info->breakoff = 0;
2003         memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
2004         info->idle_stats.in_use    =
2005         info->idle_stats.recv_idle =
2006         info->idle_stats.xmit_idle = jiffies;
2007
2008         CY_UNLOCK(info, flags);
2009
2010     } else {
2011       struct FIRM_ID __iomem *firm_id;
2012       struct ZFW_CTRL __iomem *zfw_ctrl;
2013       struct BOARD_CTRL __iomem *board_ctrl;
2014       struct CH_CTRL __iomem *ch_ctrl;
2015       int retval;
2016
2017         base_addr = cy_card[card].base_addr;
2018
2019         firm_id = base_addr + ID_ADDRESS;
2020         if (!ISZLOADED(cy_card[card])){
2021             return -ENODEV;
2022         }
2023
2024         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2025         board_ctrl = &zfw_ctrl->board_ctrl;
2026         ch_ctrl = zfw_ctrl->ch_ctrl;
2027
2028 #ifdef CY_DEBUG_OPEN
2029         printk("cyc startup Z card %d, channel %d, base_addr %lx\n",
2030              card, channel, (long)base_addr);/**/
2031 #endif
2032
2033         CY_LOCK(info, flags);
2034
2035         cy_writel(&ch_ctrl[channel].op_mode, C_CH_ENABLE);
2036 #ifdef Z_WAKE
2037 #ifdef CONFIG_CYZ_INTR
2038         cy_writel(&ch_ctrl[channel].intr_enable, 
2039                   C_IN_TXBEMPTY|C_IN_TXLOWWM|C_IN_RXHIWM|C_IN_RXNNDT|
2040                   C_IN_IOCTLW|
2041                   C_IN_MDCD);
2042 #else
2043         cy_writel(&ch_ctrl[channel].intr_enable, 
2044                   C_IN_IOCTLW|
2045                   C_IN_MDCD);
2046 #endif /* CONFIG_CYZ_INTR */
2047 #else
2048 #ifdef CONFIG_CYZ_INTR
2049         cy_writel(&ch_ctrl[channel].intr_enable, 
2050                   C_IN_TXBEMPTY|C_IN_TXLOWWM|C_IN_RXHIWM|C_IN_RXNNDT|
2051                   C_IN_MDCD);
2052 #else
2053         cy_writel(&ch_ctrl[channel].intr_enable, 
2054                   C_IN_MDCD);
2055 #endif /* CONFIG_CYZ_INTR */
2056 #endif /* Z_WAKE */
2057
2058         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_IOCTL, 0L);
2059         if (retval != 0){
2060             printk("cyc:startup(1) retval on ttyC%d was %x\n",
2061                    info->line, retval);
2062         }
2063
2064         /* Flush RX buffers before raising DTR and RTS */
2065         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_FLUSH_RX, 0L);
2066         if (retval != 0){
2067             printk("cyc:startup(2) retval on ttyC%d was %x\n",
2068                    info->line, retval);
2069         }
2070
2071         /* set timeout !!! */
2072         /* set RTS and DTR !!! */
2073         cy_writel(&ch_ctrl[channel].rs_control,
2074              cy_readl(&ch_ctrl[channel].rs_control) | C_RS_RTS | C_RS_DTR) ;
2075         retval = cyz_issue_cmd(&cy_card[info->card],
2076             channel, C_CM_IOCTLM, 0L);
2077         if (retval != 0){
2078             printk("cyc:startup(3) retval on ttyC%d was %x\n",
2079                    info->line, retval);
2080         }
2081 #ifdef CY_DEBUG_DTR
2082             printk("cyc:startup raising Z DTR\n");
2083 #endif
2084
2085         /* enable send, recv, modem !!! */
2086
2087         info->flags |= ASYNC_INITIALIZED;
2088         if (info->tty){
2089             clear_bit(TTY_IO_ERROR, &info->tty->flags);
2090         }
2091         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2092         info->breakon = info->breakoff = 0;
2093         memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
2094         info->idle_stats.in_use    =
2095         info->idle_stats.recv_idle =
2096         info->idle_stats.xmit_idle = jiffies;
2097
2098         CY_UNLOCK(info, flags);
2099     }
2100
2101 #ifdef CY_DEBUG_OPEN
2102         printk(" cyc startup done\n");
2103 #endif
2104         return 0;
2105
2106 errout:
2107         CY_UNLOCK(info, flags);
2108         return retval;
2109 } /* startup */
2110
2111
2112 static void
2113 start_xmit( struct cyclades_port *info )
2114 {
2115   unsigned long flags;
2116   void __iomem *base_addr;
2117   int card,chip,channel,index;
2118
2119     card = info->card;
2120     channel = (info->line) - (cy_card[card].first_line);
2121     if (!IS_CYC_Z(cy_card[card])) {
2122         chip = channel>>2;
2123         channel &= 0x03;
2124         index = cy_card[card].bus_index;
2125         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
2126
2127         CY_LOCK(info, flags);
2128             cy_writeb(base_addr+(CyCAR<<index), channel);
2129             cy_writeb(base_addr+(CySRER<<index), 
2130                cy_readb(base_addr+(CySRER<<index)) | CyTxRdy);
2131         CY_UNLOCK(info, flags);
2132     } else {
2133 #ifdef CONFIG_CYZ_INTR
2134       int retval;
2135
2136         CY_LOCK(info, flags);
2137             retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_INTBACK, 0L);
2138             if (retval != 0){
2139                 printk("cyc:start_xmit retval on ttyC%d was %x\n",
2140                        info->line, retval);
2141             }
2142         CY_UNLOCK(info, flags);
2143 #else /* CONFIG_CYZ_INTR */
2144         /* Don't have to do anything at this time */
2145 #endif /* CONFIG_CYZ_INTR */
2146     }
2147 } /* start_xmit */
2148
2149 /*
2150  * This routine shuts down a serial port; interrupts are disabled,
2151  * and DTR is dropped if the hangup on close termio flag is on.
2152  */
2153 static void
2154 shutdown(struct cyclades_port * info)
2155 {
2156   unsigned long flags;
2157   void __iomem *base_addr;
2158   int card,chip,channel,index;
2159
2160     if (!(info->flags & ASYNC_INITIALIZED)){
2161         return;
2162     }
2163
2164     card = info->card;
2165     channel = info->line - cy_card[card].first_line;
2166     if (!IS_CYC_Z(cy_card[card])) {
2167         chip = channel>>2;
2168         channel &= 0x03;
2169         index = cy_card[card].bus_index;
2170         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
2171
2172 #ifdef CY_DEBUG_OPEN
2173     printk("cyc shutdown Y card %d, chip %d, channel %d, base_addr %lx\n",
2174                 card, chip, channel, (long)base_addr);
2175 #endif
2176
2177         CY_LOCK(info, flags);
2178
2179             /* Clear delta_msr_wait queue to avoid mem leaks. */
2180             wake_up_interruptible(&info->delta_msr_wait);
2181
2182             if (info->xmit_buf){
2183                 unsigned char * temp;
2184                 temp = info->xmit_buf;
2185                 info->xmit_buf = NULL;
2186                 free_page((unsigned long) temp);
2187             }
2188             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2189             if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
2190                 cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
2191                 cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
2192 #ifdef CY_DEBUG_DTR
2193                 printk("cyc shutdown dropping DTR\n");
2194                 printk("     status: 0x%x, 0x%x\n",
2195                     cy_readb(base_addr+(CyMSVR1<<index)), 
2196                     cy_readb(base_addr+(CyMSVR2<<index)));
2197 #endif
2198             }
2199             cyy_issue_cmd(base_addr,CyCHAN_CTL|CyDIS_RCVR,index);
2200              /* it may be appropriate to clear _XMIT at
2201                some later date (after testing)!!! */
2202
2203             if (info->tty){
2204                 set_bit(TTY_IO_ERROR, &info->tty->flags);
2205             }
2206             info->flags &= ~ASYNC_INITIALIZED;
2207         CY_UNLOCK(info, flags);
2208     } else {
2209       struct FIRM_ID __iomem *firm_id;
2210       struct ZFW_CTRL __iomem *zfw_ctrl;
2211       struct BOARD_CTRL __iomem *board_ctrl;
2212       struct CH_CTRL __iomem *ch_ctrl;
2213       int retval;
2214
2215         base_addr = cy_card[card].base_addr;
2216 #ifdef CY_DEBUG_OPEN
2217     printk("cyc shutdown Z card %d, channel %d, base_addr %lx\n",
2218                 card, channel, (long)base_addr);
2219 #endif
2220
2221         firm_id = base_addr + ID_ADDRESS;
2222         if (!ISZLOADED(cy_card[card])) {
2223             return;
2224         }
2225
2226         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2227         board_ctrl = &zfw_ctrl->board_ctrl;
2228         ch_ctrl = zfw_ctrl->ch_ctrl;
2229
2230         CY_LOCK(info, flags);
2231
2232             if (info->xmit_buf){
2233                 unsigned char * temp;
2234                 temp = info->xmit_buf;
2235                 info->xmit_buf = NULL;
2236                 free_page((unsigned long) temp);
2237             }
2238             
2239             if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
2240                 cy_writel(&ch_ctrl[channel].rs_control,
2241                    (uclong)(cy_readl(&ch_ctrl[channel].rs_control) & 
2242                    ~(C_RS_RTS | C_RS_DTR)));
2243                 retval = cyz_issue_cmd(&cy_card[info->card],
2244                         channel, C_CM_IOCTLM, 0L);
2245                 if (retval != 0){
2246                     printk("cyc:shutdown retval on ttyC%d was %x\n",
2247                            info->line, retval);
2248                 }
2249 #ifdef CY_DEBUG_DTR
2250                 printk("cyc:shutdown dropping Z DTR\n");
2251 #endif
2252             }
2253             
2254             if (info->tty){
2255                 set_bit(TTY_IO_ERROR, &info->tty->flags);
2256             }
2257             info->flags &= ~ASYNC_INITIALIZED;
2258
2259         CY_UNLOCK(info, flags);
2260     }
2261
2262 #ifdef CY_DEBUG_OPEN
2263     printk(" cyc shutdown done\n");
2264 #endif
2265     return;
2266 } /* shutdown */
2267
2268
2269 /*
2270  * ------------------------------------------------------------
2271  * cy_open() and friends
2272  * ------------------------------------------------------------
2273  */
2274
2275 static int
2276 block_til_ready(struct tty_struct *tty, struct file * filp,
2277                            struct cyclades_port *info)
2278 {
2279   DECLARE_WAITQUEUE(wait, current);
2280   struct cyclades_card *cinfo;
2281   unsigned long flags;
2282   int chip, channel,index;
2283   int retval;
2284   void __iomem *base_addr;
2285
2286     cinfo = &cy_card[info->card];
2287     channel = info->line - cinfo->first_line;
2288
2289     /*
2290      * If the device is in the middle of being closed, then block
2291      * until it's done, and then try again.
2292      */
2293     if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
2294         if (info->flags & ASYNC_CLOSING) {
2295             interruptible_sleep_on(&info->close_wait);
2296         }
2297         return ((info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
2298     }
2299
2300     /*
2301      * If non-blocking mode is set, then make the check up front
2302      * and then exit.
2303      */
2304     if ((filp->f_flags & O_NONBLOCK) ||
2305         (tty->flags & (1 << TTY_IO_ERROR))) {
2306         info->flags |= ASYNC_NORMAL_ACTIVE;
2307         return 0;
2308     }
2309
2310     /*
2311      * Block waiting for the carrier detect and the line to become
2312      * free (i.e., not in use by the callout).  While we are in
2313      * this loop, info->count is dropped by one, so that
2314      * cy_close() knows when to free things.  We restore it upon
2315      * exit, either normal or abnormal.
2316      */
2317     retval = 0;
2318     add_wait_queue(&info->open_wait, &wait);
2319 #ifdef CY_DEBUG_OPEN
2320     printk("cyc block_til_ready before block: ttyC%d, count = %d\n",
2321            info->line, info->count);/**/
2322 #endif
2323     CY_LOCK(info, flags);
2324     if (!tty_hung_up_p(filp))
2325         info->count--;
2326     CY_UNLOCK(info, flags);
2327 #ifdef CY_DEBUG_COUNT
2328     printk("cyc block_til_ready: (%d): decrementing count to %d\n",
2329         current->pid, info->count);
2330 #endif
2331     info->blocked_open++;
2332
2333     if (!IS_CYC_Z(*cinfo)) {
2334         chip = channel>>2;
2335         channel &= 0x03;
2336         index = cinfo->bus_index;
2337         base_addr = cinfo->base_addr + (cy_chip_offset[chip]<<index);
2338
2339         while (1) {
2340             CY_LOCK(info, flags);
2341                 if ((tty->termios->c_cflag & CBAUD)){
2342                     cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2343                     cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
2344                     cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
2345 #ifdef CY_DEBUG_DTR
2346                     printk("cyc:block_til_ready raising DTR\n");
2347                     printk("     status: 0x%x, 0x%x\n",
2348                         cy_readb(base_addr+(CyMSVR1<<index)), 
2349                         cy_readb(base_addr+(CyMSVR2<<index)));
2350 #endif
2351                 }
2352             CY_UNLOCK(info, flags);
2353
2354             set_current_state(TASK_INTERRUPTIBLE);
2355             if (tty_hung_up_p(filp)
2356             || !(info->flags & ASYNC_INITIALIZED) ){
2357                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ? 
2358                     -EAGAIN : -ERESTARTSYS);
2359                 break;
2360             }
2361
2362             CY_LOCK(info, flags);
2363                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2364                 if (!(info->flags & ASYNC_CLOSING)
2365                 && (C_CLOCAL(tty)
2366                     || (cy_readb(base_addr+(CyMSVR1<<index)) & CyDCD))) {
2367                         CY_UNLOCK(info, flags);
2368                         break;
2369                 }
2370             CY_UNLOCK(info, flags);
2371
2372             if (signal_pending(current)) {
2373                 retval = -ERESTARTSYS;
2374                 break;
2375             }
2376 #ifdef CY_DEBUG_OPEN
2377             printk("cyc block_til_ready blocking: ttyC%d, count = %d\n",
2378                    info->line, info->count);/**/
2379 #endif
2380             schedule();
2381         }
2382     } else {
2383       struct FIRM_ID __iomem *firm_id;
2384       struct ZFW_CTRL __iomem *zfw_ctrl;
2385       struct BOARD_CTRL __iomem *board_ctrl;
2386       struct CH_CTRL __iomem *ch_ctrl;
2387       int retval;
2388
2389         base_addr = cinfo->base_addr;
2390         firm_id = base_addr + ID_ADDRESS;
2391         if (!ISZLOADED(*cinfo)){
2392             current->state = TASK_RUNNING;
2393             remove_wait_queue(&info->open_wait, &wait);
2394             return -EINVAL;
2395         }
2396
2397         zfw_ctrl = base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2398         board_ctrl = &zfw_ctrl->board_ctrl;
2399         ch_ctrl = zfw_ctrl->ch_ctrl;
2400
2401         while (1) {
2402             if ((tty->termios->c_cflag & CBAUD)){
2403                 cy_writel(&ch_ctrl[channel].rs_control,
2404                         cy_readl(&ch_ctrl[channel].rs_control) |
2405                         (C_RS_RTS | C_RS_DTR));
2406                 retval = cyz_issue_cmd(&cy_card[info->card],
2407                                        channel, C_CM_IOCTLM, 0L);
2408                 if (retval != 0){
2409                     printk("cyc:block_til_ready retval on ttyC%d was %x\n",
2410                            info->line, retval);
2411                 }
2412 #ifdef CY_DEBUG_DTR
2413                 printk("cyc:block_til_ready raising Z DTR\n");
2414 #endif
2415             }
2416
2417             set_current_state(TASK_INTERRUPTIBLE);
2418             if (tty_hung_up_p(filp)
2419             || !(info->flags & ASYNC_INITIALIZED) ){
2420                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2421                     -EAGAIN : -ERESTARTSYS);
2422                 break;
2423             }
2424             if (!(info->flags & ASYNC_CLOSING)
2425             && (C_CLOCAL(tty)
2426               || (cy_readl(&ch_ctrl[channel].rs_status) & C_RS_DCD))) {
2427                 break;
2428             }
2429             if (signal_pending(current)) {
2430                 retval = -ERESTARTSYS;
2431                 break;
2432             }
2433 #ifdef CY_DEBUG_OPEN
2434             printk("cyc block_til_ready blocking: ttyC%d, count = %d\n",
2435                    info->line, info->count);/**/
2436 #endif
2437             schedule();
2438         }
2439     }
2440     current->state = TASK_RUNNING;
2441     remove_wait_queue(&info->open_wait, &wait);
2442     if (!tty_hung_up_p(filp)){
2443         info->count++;
2444 #ifdef CY_DEBUG_COUNT
2445         printk("cyc:block_til_ready (%d): incrementing count to %d\n",
2446             current->pid, info->count);
2447 #endif
2448     }
2449     info->blocked_open--;
2450 #ifdef CY_DEBUG_OPEN
2451     printk("cyc:block_til_ready after blocking: ttyC%d, count = %d\n",
2452            info->line, info->count);/**/
2453 #endif
2454     if (retval)
2455         return retval;
2456     info->flags |= ASYNC_NORMAL_ACTIVE;
2457     return 0;
2458 } /* block_til_ready */
2459
2460
2461 /*
2462  * This routine is called whenever a serial port is opened.  It
2463  * performs the serial-specific initialization for the tty structure.
2464  */
2465 static int
2466 cy_open(struct tty_struct *tty, struct file * filp)
2467 {
2468   struct cyclades_port  *info;
2469   int retval, line;
2470   unsigned long page;
2471
2472     line = tty->index;
2473     if ((line < 0) || (NR_PORTS <= line)){
2474         return -ENODEV;
2475     }
2476     info = &cy_port[line];
2477     if (info->line < 0){
2478         return -ENODEV;
2479     }
2480     
2481     /* If the card's firmware hasn't been loaded,
2482        treat it as absent from the system.  This
2483        will make the user pay attention.
2484     */
2485     if (IS_CYC_Z(cy_card[info->card])) {
2486         struct cyclades_card *cinfo = &cy_card[info->card];
2487         struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
2488
2489         if (!ISZLOADED(*cinfo)) {
2490             if (((ZE_V1 ==cy_readl(&((struct RUNTIME_9060 __iomem *)
2491                 (cinfo->ctl_addr))->mail_box_0)) &&
2492                 Z_FPGA_CHECK (*cinfo)) &&
2493                 (ZFIRM_HLT == cy_readl (&firm_id->signature)))
2494             {
2495                 printk ("cyc:Cyclades-Z Error: you need an external power supply for this number of ports.\n\rFirmware halted.\r\n");
2496             } else {
2497                 printk("cyc:Cyclades-Z firmware not yet loaded\n");
2498             }
2499             return -ENODEV;
2500         }
2501 #ifdef CONFIG_CYZ_INTR
2502         else {
2503             /* In case this Z board is operating in interrupt mode, its 
2504                interrupts should be enabled as soon as the first open happens 
2505                to one of its ports. */
2506             if (!cinfo->intr_enabled) {
2507                 struct ZFW_CTRL __iomem *zfw_ctrl;
2508                 struct BOARD_CTRL __iomem *board_ctrl;
2509
2510                 zfw_ctrl = cinfo->base_addr + (cy_readl (&firm_id->zfwctrl_addr) & 0xfffff);
2511
2512                 board_ctrl = &zfw_ctrl->board_ctrl;
2513
2514                 /* Enable interrupts on the PLX chip */
2515                 cy_writew(cinfo->ctl_addr+0x68,
2516                         cy_readw(cinfo->ctl_addr+0x68)|0x0900);
2517                 /* Enable interrupts on the FW */
2518                 retval = cyz_issue_cmd(cinfo,
2519                                         0, C_CM_IRQ_ENBL, 0L);
2520                 if (retval != 0){
2521                     printk("cyc:IRQ enable retval was %x\n", retval);
2522                 }
2523                 cinfo->nports = (int) cy_readl (&board_ctrl->n_channel);
2524                 cinfo->intr_enabled = 1;
2525             }
2526         }
2527 #endif /* CONFIG_CYZ_INTR */
2528         /* Make sure this Z port really exists in hardware */
2529         if (info->line > (cinfo->first_line + cinfo->nports - 1))
2530                 return -ENODEV;
2531     }
2532 #ifdef CY_DEBUG_OTHER
2533     printk("cyc:cy_open ttyC%d\n", info->line); /* */
2534 #endif
2535     tty->driver_data = info;
2536     info->tty = tty;
2537     if (serial_paranoia_check(info, tty->name, "cy_open")){
2538         return -ENODEV;
2539     }
2540 #ifdef CY_DEBUG_OPEN
2541     printk("cyc:cy_open ttyC%d, count = %d\n",
2542         info->line, info->count);/**/
2543 #endif
2544     info->count++;
2545 #ifdef CY_DEBUG_COUNT
2546     printk("cyc:cy_open (%d): incrementing count to %d\n",
2547         current->pid, info->count);
2548 #endif
2549     if (!tmp_buf) {
2550         page = get_zeroed_page(GFP_KERNEL);
2551         if (!page)
2552             return -ENOMEM;
2553         if (tmp_buf)
2554             free_page(page);
2555         else
2556             tmp_buf = (unsigned char *) page;
2557     }
2558
2559     /*
2560      * If the port is the middle of closing, bail out now
2561      */
2562     if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
2563         if (info->flags & ASYNC_CLOSING)
2564             interruptible_sleep_on(&info->close_wait);
2565         return ((info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
2566     }
2567
2568     /*
2569      * Start up serial port
2570      */
2571     retval = startup(info);
2572     if (retval){
2573         return retval;
2574     }
2575
2576     retval = block_til_ready(tty, filp, info);
2577     if (retval) {
2578 #ifdef CY_DEBUG_OPEN
2579         printk("cyc:cy_open returning after block_til_ready with %d\n",
2580                retval);
2581 #endif
2582         return retval;
2583     }
2584
2585     info->throttle = 0;
2586
2587 #ifdef CY_DEBUG_OPEN
2588     printk(" cyc:cy_open done\n");/**/
2589 #endif
2590
2591     return 0;
2592 } /* cy_open */
2593
2594
2595 /*
2596  * cy_wait_until_sent() --- wait until the transmitter is empty
2597  */
2598 static void 
2599 cy_wait_until_sent(struct tty_struct *tty, int timeout)
2600 {
2601   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2602   void __iomem *base_addr;
2603   int card,chip,channel,index;
2604   unsigned long orig_jiffies;
2605   int char_time;
2606         
2607     if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
2608         return;
2609
2610     if (info->xmit_fifo_size == 0)
2611         return; /* Just in case.... */
2612
2613
2614     orig_jiffies = jiffies;
2615     /*
2616      * Set the check interval to be 1/5 of the estimated time to
2617      * send a single character, and make it at least 1.  The check
2618      * interval should also be less than the timeout.
2619      * 
2620      * Note: we have to use pretty tight timings here to satisfy
2621      * the NIST-PCTS.
2622      */
2623     char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2624     char_time = char_time / 5;
2625     if (char_time <= 0)
2626         char_time = 1;
2627     if (timeout < 0)
2628         timeout = 0;
2629     if (timeout)
2630         char_time = min(char_time, timeout);
2631     /*
2632      * If the transmitter hasn't cleared in twice the approximate
2633      * amount of time to send the entire FIFO, it probably won't
2634      * ever clear.  This assumes the UART isn't doing flow
2635      * control, which is currently the case.  Hence, if it ever
2636      * takes longer than info->timeout, this is probably due to a
2637      * UART bug of some kind.  So, we clamp the timeout parameter at
2638      * 2*info->timeout.
2639      */
2640     if (!timeout || timeout > 2*info->timeout)
2641         timeout = 2*info->timeout;
2642 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2643     printk("In cy_wait_until_sent(%d) check=%lu...", timeout, char_time);
2644     printk("jiff=%lu...", jiffies);
2645 #endif
2646     card = info->card;
2647     channel = (info->line) - (cy_card[card].first_line);
2648     if (!IS_CYC_Z(cy_card[card])) {
2649         chip = channel>>2;
2650         channel &= 0x03;
2651         index = cy_card[card].bus_index;
2652         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
2653         while (cy_readb(base_addr+(CySRER<<index)) & CyTxRdy) {
2654 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2655             printk("Not clean (jiff=%lu)...", jiffies);
2656 #endif
2657             if (msleep_interruptible(jiffies_to_msecs(char_time)))
2658                 break;
2659             if (timeout && time_after(jiffies, orig_jiffies + timeout))
2660                 break;
2661         }
2662     } else {
2663         // Nothing to do!
2664     }
2665     /* Run one more char cycle */
2666     msleep_interruptible(jiffies_to_msecs(char_time * 5));
2667 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2668     printk("Clean (jiff=%lu)...done\n", jiffies);
2669 #endif
2670 }
2671
2672 /*
2673  * This routine is called when a particular tty device is closed.
2674  */
2675 static void
2676 cy_close(struct tty_struct *tty, struct file *filp)
2677 {
2678   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2679   unsigned long flags;
2680
2681 #ifdef CY_DEBUG_OTHER
2682     printk("cyc:cy_close ttyC%d\n", info->line);
2683 #endif
2684
2685     if (!info || serial_paranoia_check(info, tty->name, "cy_close")){
2686         return;
2687     }
2688
2689     CY_LOCK(info, flags);
2690     /* If the TTY is being hung up, nothing to do */
2691     if (tty_hung_up_p(filp)) {
2692         CY_UNLOCK(info, flags);
2693         return;
2694     }
2695         
2696 #ifdef CY_DEBUG_OPEN
2697     printk("cyc:cy_close ttyC%d, count = %d\n", info->line, info->count);
2698 #endif
2699     if ((tty->count == 1) && (info->count != 1)) {
2700         /*
2701          * Uh, oh.  tty->count is 1, which means that the tty
2702          * structure will be freed.  Info->count should always
2703          * be one in these conditions.  If it's greater than
2704          * one, we've got real problems, since it means the
2705          * serial port won't be shutdown.
2706          */
2707         printk("cyc:cy_close: bad serial port count; tty->count is 1, "
2708            "info->count is %d\n", info->count);
2709         info->count = 1;
2710     }
2711 #ifdef CY_DEBUG_COUNT
2712     printk("cyc:cy_close at (%d): decrementing count to %d\n",
2713         current->pid, info->count - 1);
2714 #endif
2715     if (--info->count < 0) {
2716 #ifdef CY_DEBUG_COUNT
2717     printk("cyc:cyc_close setting count to 0\n");
2718 #endif
2719         info->count = 0;
2720     }
2721     if (info->count) {
2722         CY_UNLOCK(info, flags);
2723         return;
2724     }
2725     info->flags |= ASYNC_CLOSING;
2726
2727     /*
2728     * Now we wait for the transmit buffer to clear; and we notify
2729     * the line discipline to only process XON/XOFF characters.
2730     */
2731     tty->closing = 1;
2732     CY_UNLOCK(info, flags);
2733     if (info->closing_wait != CY_CLOSING_WAIT_NONE) {
2734         tty_wait_until_sent(tty, info->closing_wait);
2735     }
2736     CY_LOCK(info, flags);
2737
2738     if (!IS_CYC_Z(cy_card[info->card])) {
2739         int channel = info->line - cy_card[info->card].first_line;
2740         int index = cy_card[info->card].bus_index;
2741         void __iomem *base_addr = cy_card[info->card].base_addr + (cy_chip_offset[channel>>2] << index);
2742         /* Stop accepting input */
2743         channel &= 0x03;
2744         cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2745         cy_writeb(base_addr+(CySRER<<index),
2746                         cy_readb(base_addr+(CySRER<<index)) & ~CyRxData);
2747         if (info->flags & ASYNC_INITIALIZED) {
2748             /* Waiting for on-board buffers to be empty before closing 
2749                the port */
2750             CY_UNLOCK(info, flags);
2751             cy_wait_until_sent(tty, info->timeout);
2752             CY_LOCK(info, flags);
2753         }
2754     } else {
2755 #ifdef Z_WAKE
2756         /* Waiting for on-board buffers to be empty before closing the port */
2757         void __iomem *base_addr = cy_card[info->card].base_addr;
2758         struct FIRM_ID __iomem *firm_id = base_addr + ID_ADDRESS;
2759         struct ZFW_CTRL __iomem *zfw_ctrl = base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2760         struct CH_CTRL __iomem *ch_ctrl = zfw_ctrl->ch_ctrl;
2761         int channel = info->line - cy_card[info->card].first_line;
2762         int retval;
2763
2764         if (cy_readl(&ch_ctrl[channel].flow_status) != C_FS_TXIDLE) {
2765             retval = cyz_issue_cmd(&cy_card[info->card], channel, 
2766                                    C_CM_IOCTLW, 0L);
2767             if (retval != 0){
2768                 printk("cyc:cy_close retval on ttyC%d was %x\n",
2769                        info->line, retval);
2770             }
2771             CY_UNLOCK(info, flags);
2772             interruptible_sleep_on(&info->shutdown_wait);
2773             CY_LOCK(info, flags);
2774         }
2775 #endif
2776     }
2777
2778     CY_UNLOCK(info, flags);
2779     shutdown(info);
2780     if (tty->driver->flush_buffer)
2781         tty->driver->flush_buffer(tty);
2782     tty_ldisc_flush(tty);        
2783     CY_LOCK(info, flags);
2784
2785     tty->closing = 0;
2786     info->event = 0;
2787     info->tty = NULL;
2788     if (info->blocked_open) {
2789         CY_UNLOCK(info, flags);
2790         if (info->close_delay) {
2791             msleep_interruptible(jiffies_to_msecs(info->close_delay));
2792         }
2793         wake_up_interruptible(&info->open_wait);
2794         CY_LOCK(info, flags);
2795     }
2796     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2797     wake_up_interruptible(&info->close_wait);
2798
2799 #ifdef CY_DEBUG_OTHER
2800     printk(" cyc:cy_close done\n");
2801 #endif
2802
2803     CY_UNLOCK(info, flags);
2804     return;
2805 } /* cy_close */
2806
2807
2808 /* This routine gets called when tty_write has put something into
2809  * the write_queue.  The characters may come from user space or
2810  * kernel space.
2811  *
2812  * This routine will return the number of characters actually
2813  * accepted for writing.
2814  *
2815  * If the port is not already transmitting stuff, start it off by
2816  * enabling interrupts.  The interrupt service routine will then
2817  * ensure that the characters are sent.
2818  * If the port is already active, there is no need to kick it.
2819  *
2820  */
2821 static int
2822 cy_write(struct tty_struct * tty, const unsigned char *buf, int count)
2823 {
2824   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2825   unsigned long flags;
2826   int c, ret = 0;
2827
2828 #ifdef CY_DEBUG_IO
2829     printk("cyc:cy_write ttyC%d\n", info->line); /* */
2830 #endif
2831
2832     if (serial_paranoia_check(info, tty->name, "cy_write")){
2833         return 0;
2834     }
2835         
2836     if (!tty || !info->xmit_buf || !tmp_buf){
2837         return 0;
2838     }
2839
2840     CY_LOCK(info, flags);
2841     while (1) {
2842         c = min(count, min((int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1),
2843                         (int)(SERIAL_XMIT_SIZE - info->xmit_head)));
2844                 
2845         if (c <= 0)
2846             break;
2847
2848         memcpy(info->xmit_buf + info->xmit_head, buf, c);
2849         info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
2850         info->xmit_cnt += c;
2851         buf += c;
2852         count -= c;
2853         ret += c;
2854     }
2855     CY_UNLOCK(info, flags);
2856
2857     info->idle_stats.xmit_bytes += ret;
2858     info->idle_stats.xmit_idle   = jiffies;
2859
2860     if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2861         start_xmit(info);
2862     }
2863     return ret;
2864 } /* cy_write */
2865
2866
2867 /*
2868  * This routine is called by the kernel to write a single
2869  * character to the tty device.  If the kernel uses this routine,
2870  * it must call the flush_chars() routine (if defined) when it is
2871  * done stuffing characters into the driver.  If there is no room
2872  * in the queue, the character is ignored.
2873  */
2874 static void
2875 cy_put_char(struct tty_struct *tty, unsigned char ch)
2876 {
2877   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2878   unsigned long flags;
2879
2880 #ifdef CY_DEBUG_IO
2881     printk("cyc:cy_put_char ttyC%d\n", info->line);
2882 #endif
2883
2884     if (serial_paranoia_check(info, tty->name, "cy_put_char"))
2885         return;
2886
2887     if (!tty || !info->xmit_buf)
2888         return;
2889
2890     CY_LOCK(info, flags);
2891         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
2892             CY_UNLOCK(info, flags);
2893             return;
2894         }
2895
2896         info->xmit_buf[info->xmit_head++] = ch;
2897         info->xmit_head &= SERIAL_XMIT_SIZE - 1;
2898         info->xmit_cnt++;
2899         info->idle_stats.xmit_bytes++;
2900         info->idle_stats.xmit_idle = jiffies;
2901     CY_UNLOCK(info, flags);
2902 } /* cy_put_char */
2903
2904
2905 /*
2906  * This routine is called by the kernel after it has written a
2907  * series of characters to the tty device using put_char().  
2908  */
2909 static void
2910 cy_flush_chars(struct tty_struct *tty)
2911 {
2912   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2913                                 
2914 #ifdef CY_DEBUG_IO
2915     printk("cyc:cy_flush_chars ttyC%d\n", info->line); /* */
2916 #endif
2917
2918     if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
2919         return;
2920
2921     if (info->xmit_cnt <= 0 || tty->stopped
2922     || tty->hw_stopped || !info->xmit_buf)
2923         return;
2924
2925     start_xmit(info);
2926 } /* cy_flush_chars */
2927
2928
2929 /*
2930  * This routine returns the numbers of characters the tty driver
2931  * will accept for queuing to be written.  This number is subject
2932  * to change as output buffers get emptied, or if the output flow
2933  * control is activated.
2934  */
2935 static int
2936 cy_write_room(struct tty_struct *tty)
2937 {
2938   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2939   int   ret;
2940                                 
2941 #ifdef CY_DEBUG_IO
2942     printk("cyc:cy_write_room ttyC%d\n", info->line); /* */
2943 #endif
2944
2945     if (serial_paranoia_check(info, tty->name, "cy_write_room"))
2946         return 0;
2947     ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2948     if (ret < 0)
2949         ret = 0;
2950     return ret;
2951 } /* cy_write_room */
2952
2953
2954 static int
2955 cy_chars_in_buffer(struct tty_struct *tty)
2956 {
2957   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2958   int card, channel;
2959                                 
2960     if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
2961         return 0;
2962
2963     card = info->card;
2964     channel = (info->line) - (cy_card[card].first_line);
2965
2966 #ifdef Z_EXT_CHARS_IN_BUFFER
2967     if (!IS_CYC_Z(cy_card[card])) {
2968 #endif /* Z_EXT_CHARS_IN_BUFFER */
2969 #ifdef CY_DEBUG_IO
2970         printk("cyc:cy_chars_in_buffer ttyC%d %d\n",
2971                 info->line, info->xmit_cnt); /* */
2972 #endif
2973         return info->xmit_cnt;
2974 #ifdef Z_EXT_CHARS_IN_BUFFER
2975     } else {
2976         static volatile struct FIRM_ID *firm_id;
2977         static volatile struct ZFW_CTRL *zfw_ctrl;
2978         static volatile struct CH_CTRL *ch_ctrl;
2979         static volatile struct BUF_CTRL *buf_ctrl;
2980         int char_count;
2981         volatile uclong tx_put, tx_get, tx_bufsize;
2982
2983         firm_id = cy_card[card].base_addr + ID_ADDRESS;
2984         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2985         ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
2986         buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
2987
2988         tx_get = cy_readl(&buf_ctrl->tx_get);
2989         tx_put = cy_readl(&buf_ctrl->tx_put);
2990         tx_bufsize = cy_readl(&buf_ctrl->tx_bufsize);
2991         if (tx_put >= tx_get)
2992             char_count = tx_put - tx_get;
2993         else
2994             char_count = tx_put - tx_get + tx_bufsize;
2995 #ifdef CY_DEBUG_IO
2996         printk("cyc:cy_chars_in_buffer ttyC%d %d\n",
2997                 info->line, info->xmit_cnt + char_count); /* */
2998 #endif
2999         return (info->xmit_cnt + char_count);
3000     }
3001 #endif /* Z_EXT_CHARS_IN_BUFFER */
3002 } /* cy_chars_in_buffer */
3003
3004
3005 /*
3006  * ------------------------------------------------------------
3007  * cy_ioctl() and friends
3008  * ------------------------------------------------------------
3009  */
3010
3011 static void
3012 cyy_baud_calc(struct cyclades_port *info, uclong baud)
3013 {
3014     int co, co_val, bpr;
3015     uclong cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 : 25000000);
3016
3017     if (baud == 0) {
3018         info->tbpr = info->tco = info->rbpr = info->rco = 0;
3019         return;
3020     }
3021
3022     /* determine which prescaler to use */
3023     for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
3024         if (cy_clock / co_val / baud > 63)
3025             break;
3026     }
3027
3028     bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
3029     if (bpr > 255)
3030         bpr = 255;
3031
3032     info->tbpr = info->rbpr = bpr;
3033     info->tco = info->rco = co;
3034 }
3035
3036 /*
3037  * This routine finds or computes the various line characteristics.
3038  * It used to be called config_setup
3039  */
3040 static void
3041 set_line_char(struct cyclades_port * info)
3042 {
3043   unsigned long flags;
3044   void __iomem *base_addr;
3045   int card,chip,channel,index;
3046   unsigned cflag, iflag;
3047   unsigned short chip_number;
3048   int baud, baud_rate = 0;
3049   int   i;
3050
3051
3052     if (!info->tty || !info->tty->termios){
3053         return;
3054     }
3055     if (info->line == -1){
3056         return;
3057     }
3058     cflag = info->tty->termios->c_cflag;
3059     iflag = info->tty->termios->c_iflag;
3060
3061     /*
3062      * Set up the tty->alt_speed kludge
3063      */
3064     if (info->tty) {
3065         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3066             info->tty->alt_speed = 57600;
3067         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3068             info->tty->alt_speed = 115200;
3069         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3070             info->tty->alt_speed = 230400;
3071         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3072             info->tty->alt_speed = 460800;
3073     }
3074
3075     card = info->card;
3076     channel = (info->line) - (cy_card[card].first_line);
3077     chip_number = channel / 4;
3078
3079     if (!IS_CYC_Z(cy_card[card])) {
3080
3081         index = cy_card[card].bus_index;
3082
3083         /* baud rate */
3084         baud = tty_get_baud_rate(info->tty);
3085         if ((baud == 38400) &&
3086             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3087             if (info->custom_divisor)
3088                 baud_rate = info->baud / info->custom_divisor;
3089             else
3090                 baud_rate = info->baud;
3091         } else if (baud > CD1400_MAX_SPEED) {
3092             baud = CD1400_MAX_SPEED;
3093         }
3094         /* find the baud index */
3095         for (i = 0; i < 20; i++) {
3096             if (baud == baud_table[i]) {
3097                 break;
3098             }
3099         }
3100         if (i == 20) {
3101             i = 19; /* CD1400_MAX_SPEED */
3102         } 
3103
3104         if ((baud == 38400) &&
3105             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3106             cyy_baud_calc(info, baud_rate);
3107         } else {
3108             if(info->chip_rev >= CD1400_REV_J) {
3109                 /* It is a CD1400 rev. J or later */
3110                 info->tbpr = baud_bpr_60[i]; /* Tx BPR */
3111                 info->tco = baud_co_60[i]; /* Tx CO */
3112                 info->rbpr = baud_bpr_60[i]; /* Rx BPR */
3113                 info->rco = baud_co_60[i]; /* Rx CO */
3114             } else {
3115                 info->tbpr = baud_bpr_25[i]; /* Tx BPR */
3116                 info->tco = baud_co_25[i]; /* Tx CO */
3117                 info->rbpr = baud_bpr_25[i]; /* Rx BPR */
3118                 info->rco = baud_co_25[i]; /* Rx CO */
3119             }
3120         }
3121         if (baud_table[i] == 134) {
3122             /* get it right for 134.5 baud */
3123             info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
3124         } else if ((baud == 38400) &&
3125                    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3126             info->timeout = (info->xmit_fifo_size*HZ*15/baud_rate) + 2;
3127         } else if (baud_table[i]) {
3128             info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
3129             /* this needs to be propagated into the card info */
3130         } else {
3131             info->timeout = 0;
3132         }
3133         /* By tradition (is it a standard?) a baud rate of zero
3134            implies the line should be/has been closed.  A bit
3135            later in this routine such a test is performed. */
3136
3137         /* byte size and parity */
3138         info->cor5 = 0;
3139         info->cor4 = 0;
3140         info->cor3 = (info->default_threshold
3141                       ? info->default_threshold
3142                       : baud_cor3[i]); /* receive threshold */
3143         info->cor2 = CyETC;
3144         switch(cflag & CSIZE){
3145         case CS5:
3146             info->cor1 = Cy_5_BITS;
3147             break;
3148         case CS6:
3149             info->cor1 = Cy_6_BITS;
3150             break;
3151         case CS7:
3152             info->cor1 = Cy_7_BITS;
3153             break;
3154         case CS8:
3155             info->cor1 = Cy_8_BITS;
3156             break;
3157         }
3158         if(cflag & CSTOPB){
3159             info->cor1 |= Cy_2_STOP;
3160         }
3161         if (cflag & PARENB){
3162             if (cflag & PARODD){
3163                 info->cor1 |= CyPARITY_O;
3164             }else{
3165                 info->cor1 |= CyPARITY_E;
3166             }
3167         }else{
3168             info->cor1 |= CyPARITY_NONE;
3169         }
3170             
3171         /* CTS flow control flag */
3172         if (cflag & CRTSCTS){
3173             info->flags |= ASYNC_CTS_FLOW;
3174             info->cor2 |= CyCtsAE;
3175         }else{
3176             info->flags &= ~ASYNC_CTS_FLOW;
3177             info->cor2 &= ~CyCtsAE;
3178         }
3179         if (cflag & CLOCAL)
3180             info->flags &= ~ASYNC_CHECK_CD;
3181         else
3182             info->flags |= ASYNC_CHECK_CD;
3183
3184          /***********************************************
3185             The hardware option, CyRtsAO, presents RTS when
3186             the chip has characters to send.  Since most modems
3187             use RTS as reverse (inbound) flow control, this
3188             option is not used.  If inbound flow control is
3189             necessary, DTR can be programmed to provide the
3190             appropriate signals for use with a non-standard
3191             cable.  Contact Marcio Saito for details.
3192          ***********************************************/
3193
3194         chip = channel>>2;
3195         channel &= 0x03;
3196         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3197
3198         CY_LOCK(info, flags);
3199             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3200
3201            /* tx and rx baud rate */
3202
3203             cy_writeb(base_addr+(CyTCOR<<index), info->tco);
3204             cy_writeb(base_addr+(CyTBPR<<index), info->tbpr);
3205             cy_writeb(base_addr+(CyRCOR<<index), info->rco);
3206             cy_writeb(base_addr+(CyRBPR<<index), info->rbpr);
3207
3208             /* set line characteristics  according configuration */
3209
3210             cy_writeb(base_addr+(CySCHR1<<index), 
3211                       START_CHAR(info->tty));
3212             cy_writeb(base_addr+(CySCHR2<<index), 
3213                       STOP_CHAR(info->tty));
3214             cy_writeb(base_addr+(CyCOR1<<index), info->cor1);
3215             cy_writeb(base_addr+(CyCOR2<<index), info->cor2);
3216             cy_writeb(base_addr+(CyCOR3<<index), info->cor3);
3217             cy_writeb(base_addr+(CyCOR4<<index), info->cor4);
3218             cy_writeb(base_addr+(CyCOR5<<index), info->cor5);
3219
3220             cyy_issue_cmd(base_addr,
3221                      CyCOR_CHANGE|CyCOR1ch|CyCOR2ch|CyCOR3ch,index);
3222
3223             cy_writeb(base_addr+(CyCAR<<index), 
3224                       (u_char)channel); /* !!! Is this needed? */
3225             cy_writeb(base_addr+(CyRTPR<<index), (info->default_timeout
3226                                                  ? info->default_timeout
3227                                                  : 0x02)); /* 10ms rx timeout */
3228
3229             if (C_CLOCAL(info->tty)) {
3230                 /* without modem intr */
3231                 cy_writeb(base_addr+(CySRER<<index),
3232                    cy_readb(base_addr+(CySRER<<index)) | CyMdmCh); 
3233                                         /* act on 1->0 modem transitions */
3234                 if ((cflag & CRTSCTS) && info->rflow) {
3235                         cy_writeb(base_addr+(CyMCOR1<<index), 
3236                                   (CyCTS|rflow_thr[i]));
3237                 } else {
3238                         cy_writeb(base_addr+(CyMCOR1<<index), CyCTS);
3239                 }
3240                                         /* act on 0->1 modem transitions */
3241                 cy_writeb(base_addr+(CyMCOR2<<index), CyCTS);
3242             } else {
3243                 /* without modem intr */
3244                 cy_writeb(base_addr+(CySRER<<index),
3245                    cy_readb(base_addr+(CySRER<<index)) | CyMdmCh); 
3246                                         /* act on 1->0 modem transitions */
3247                 if ((cflag & CRTSCTS) && info->rflow) {
3248                         cy_writeb(base_addr+(CyMCOR1<<index), 
3249                                   (CyDSR|CyCTS|CyRI|CyDCD|rflow_thr[i]));
3250                 } else {
3251                         cy_writeb(base_addr+(CyMCOR1<<index), 
3252                                   CyDSR|CyCTS|CyRI|CyDCD);
3253                 }
3254                                         /* act on 0->1 modem transitions */
3255                 cy_writeb(base_addr+(CyMCOR2<<index), 
3256                           CyDSR|CyCTS|CyRI|CyDCD);
3257             }
3258
3259             if(i == 0){ /* baud rate is zero, turn off line */
3260                 if (info->rtsdtr_inv) {
3261                         cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
3262                 } else {
3263                         cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
3264                 }
3265 #ifdef CY_DEBUG_DTR
3266                 printk("cyc:set_line_char dropping DTR\n");
3267                 printk("     status: 0x%x, 0x%x\n", 
3268                     cy_readb(base_addr+(CyMSVR1<<index)),
3269                     cy_readb(base_addr+(CyMSVR2<<index)));
3270 #endif
3271             }else{
3272                 if (info->rtsdtr_inv) {
3273                         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
3274                 } else {
3275                         cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
3276                 }
3277 #ifdef CY_DEBUG_DTR
3278                 printk("cyc:set_line_char raising DTR\n");
3279                 printk("     status: 0x%x, 0x%x\n",
3280                     cy_readb(base_addr+(CyMSVR1<<index)),
3281                     cy_readb(base_addr+(CyMSVR2<<index)));
3282 #endif
3283             }
3284
3285             if (info->tty){
3286                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
3287             }
3288         CY_UNLOCK(info, flags);
3289
3290     } else {
3291       struct FIRM_ID __iomem *firm_id;
3292       struct ZFW_CTRL __iomem *zfw_ctrl;
3293       struct BOARD_CTRL __iomem *board_ctrl;
3294       struct CH_CTRL __iomem *ch_ctrl;
3295       struct BUF_CTRL __iomem *buf_ctrl;
3296       uclong sw_flow;
3297       int retval;
3298
3299         firm_id = cy_card[card].base_addr + ID_ADDRESS;
3300         if (!ISZLOADED(cy_card[card])) {
3301             return;
3302         }
3303
3304         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
3305         board_ctrl = &zfw_ctrl->board_ctrl;
3306         ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
3307         buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
3308
3309         /* baud rate */
3310         baud = tty_get_baud_rate(info->tty);
3311         if ((baud == 38400) &&
3312             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3313             if (info->custom_divisor)
3314                 baud_rate = info->baud / info->custom_divisor;
3315             else
3316                 baud_rate = info->baud;
3317         } else if (baud > CYZ_MAX_SPEED) {
3318             baud = CYZ_MAX_SPEED;
3319         }
3320         cy_writel(&ch_ctrl->comm_baud , baud);
3321
3322         if (baud == 134) {
3323             /* get it right for 134.5 baud */
3324             info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
3325         } else if ((baud == 38400) &&
3326                    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3327             info->timeout = (info->xmit_fifo_size*HZ*15/baud_rate) + 2;
3328         } else if (baud) {
3329             info->timeout = (info->xmit_fifo_size*HZ*15/baud) + 2;
3330             /* this needs to be propagated into the card info */
3331         } else {
3332             info->timeout = 0;
3333         }
3334
3335         /* byte size and parity */
3336         switch(cflag & CSIZE){
3337         case CS5: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS5); break;
3338         case CS6: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS6); break;
3339         case CS7: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS7); break;
3340         case CS8: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS8); break;
3341         }
3342         if(cflag & CSTOPB){
3343             cy_writel(&ch_ctrl->comm_data_l,
3344                cy_readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
3345         }else{
3346             cy_writel(&ch_ctrl->comm_data_l,
3347                cy_readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
3348         }
3349         if (cflag & PARENB){
3350             if (cflag & PARODD){
3351                 cy_writel(&ch_ctrl->comm_parity , C_PR_ODD);
3352             }else{
3353                 cy_writel(&ch_ctrl->comm_parity , C_PR_EVEN);
3354             }
3355         }else{
3356             cy_writel(&ch_ctrl->comm_parity , C_PR_NONE);
3357         }
3358
3359         /* CTS flow control flag */
3360         if (cflag & CRTSCTS){
3361             cy_writel(&ch_ctrl->hw_flow,
3362                cy_readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
3363         }else{
3364             cy_writel(&ch_ctrl->hw_flow,
3365                cy_readl(&ch_ctrl->hw_flow) & ~(C_RS_CTS | C_RS_RTS));
3366         }
3367         /* As the HW flow control is done in firmware, the driver doesn't
3368            need to care about it */
3369         info->flags &= ~ASYNC_CTS_FLOW;
3370
3371         /* XON/XOFF/XANY flow control flags */
3372         sw_flow = 0;
3373         if (iflag & IXON){
3374             sw_flow |= C_FL_OXX;
3375             if (iflag & IXANY)
3376                 sw_flow |= C_FL_OIXANY;
3377         }
3378         cy_writel(&ch_ctrl->sw_flow, sw_flow);
3379
3380         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_IOCTL, 0L);
3381         if (retval != 0){
3382             printk("cyc:set_line_char retval on ttyC%d was %x\n",
3383                    info->line, retval);
3384         }
3385
3386         /* CD sensitivity */
3387         if (cflag & CLOCAL){
3388             info->flags &= ~ASYNC_CHECK_CD;
3389         }else{
3390             info->flags |= ASYNC_CHECK_CD;
3391         }
3392
3393         if(baud == 0){ /* baud rate is zero, turn off line */
3394             cy_writel(&ch_ctrl->rs_control,
3395                cy_readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
3396 #ifdef CY_DEBUG_DTR
3397             printk("cyc:set_line_char dropping Z DTR\n");
3398 #endif
3399         }else{
3400             cy_writel(&ch_ctrl->rs_control,
3401                cy_readl(&ch_ctrl->rs_control) | C_RS_DTR);
3402 #ifdef CY_DEBUG_DTR
3403             printk("cyc:set_line_char raising Z DTR\n");
3404 #endif
3405         }
3406
3407         retval = cyz_issue_cmd( &cy_card[card], channel, C_CM_IOCTLM, 0L);
3408         if (retval != 0){
3409             printk("cyc:set_line_char(2) retval on ttyC%d was %x\n",
3410                    info->line, retval);
3411         }
3412
3413         if (info->tty){
3414             clear_bit(TTY_IO_ERROR, &info->tty->flags);
3415         }
3416     }
3417 } /* set_line_char */
3418
3419
3420 static int
3421 get_serial_info(struct cyclades_port * info,
3422                            struct serial_struct __user * retinfo)
3423 {
3424   struct serial_struct tmp;
3425   struct cyclades_card *cinfo = &cy_card[info->card];
3426
3427     if (!retinfo)
3428             return -EFAULT;
3429     memset(&tmp, 0, sizeof(tmp));
3430     tmp.type = info->type;
3431     tmp.line = info->line;
3432     tmp.port = info->card * 0x100 + info->line - cinfo->first_line;
3433     tmp.irq = cinfo->irq;
3434     tmp.flags = info->flags;
3435     tmp.close_delay = info->close_delay;
3436     tmp.baud_base = info->baud;
3437     tmp.custom_divisor = info->custom_divisor;
3438     tmp.hub6 = 0;               /*!!!*/
3439     return copy_to_user(retinfo,&tmp,sizeof(*retinfo))?-EFAULT:0;
3440 } /* get_serial_info */
3441
3442
3443 static int
3444 set_serial_info(struct cyclades_port * info,
3445                            struct serial_struct __user * new_info)
3446 {
3447   struct serial_struct new_serial;
3448   struct cyclades_port old_info;
3449
3450     if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
3451         return -EFAULT;
3452     old_info = *info;
3453
3454     if (!capable(CAP_SYS_ADMIN)) {
3455             if ((new_serial.close_delay != info->close_delay) ||
3456                 (new_serial.baud_base != info->baud) ||
3457                 ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
3458                  (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
3459                     return -EPERM;
3460             info->flags = ((info->flags & ~ASYNC_USR_MASK) |
3461                            (new_serial.flags & ASYNC_USR_MASK));
3462             info->baud = new_serial.baud_base;
3463             info->custom_divisor = new_serial.custom_divisor;
3464             goto check_and_exit;
3465     }
3466
3467
3468     /*
3469      * OK, past this point, all the error checking has been done.
3470      * At this point, we start making changes.....
3471      */
3472
3473     info->baud = new_serial.baud_base;
3474     info->custom_divisor = new_serial.custom_divisor;
3475     info->flags = ((info->flags & ~ASYNC_FLAGS) |
3476                     (new_serial.flags & ASYNC_FLAGS));
3477     info->close_delay = new_serial.close_delay * HZ/100;
3478     info->closing_wait = new_serial.closing_wait * HZ/100;
3479
3480 check_and_exit:
3481     if (info->flags & ASYNC_INITIALIZED){
3482         set_line_char(info);
3483         return 0;
3484     }else{
3485         return startup(info);
3486     }
3487 } /* set_serial_info */
3488
3489 /*
3490  * get_lsr_info - get line status register info
3491  *
3492  * Purpose: Let user call ioctl() to get info when the UART physically
3493  *          is emptied.  On bus types like RS485, the transmitter must
3494  *          release the bus after transmitting. This must be done when
3495  *          the transmit shift register is empty, not be done when the
3496  *          transmit holding register is empty.  This functionality
3497  *          allows an RS485 driver to be written in user space.
3498  */
3499 static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
3500 {
3501     int card, chip, channel, index;
3502     unsigned char status;
3503     unsigned int result;
3504     unsigned long flags;
3505     void __iomem *base_addr;
3506
3507     card = info->card;
3508     channel = (info->line) - (cy_card[card].first_line);
3509     if (!IS_CYC_Z(cy_card[card])) {
3510         chip = channel>>2;
3511         channel &= 0x03;
3512         index = cy_card[card].bus_index;
3513         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3514
3515         CY_LOCK(info, flags);
3516         status = cy_readb(base_addr+(CySRER<<index)) & (CyTxRdy|CyTxMpty);
3517         CY_UNLOCK(info, flags);
3518         result = (status ? 0 : TIOCSER_TEMT);
3519     } else {
3520         /* Not supported yet */
3521         return -EINVAL;
3522     }
3523     return put_user(result, (unsigned long __user *) value);
3524 }
3525
3526 static int
3527 cy_tiocmget(struct tty_struct *tty, struct file *file)
3528 {
3529   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3530   int card,chip,channel,index;
3531   void __iomem *base_addr;
3532   unsigned long flags;
3533   unsigned char status;
3534   unsigned long lstatus;
3535   unsigned int result;
3536   struct FIRM_ID __iomem *firm_id;
3537   struct ZFW_CTRL __iomem *zfw_ctrl;
3538   struct BOARD_CTRL __iomem *board_ctrl;
3539   struct CH_CTRL __iomem *ch_ctrl;
3540
3541     if (serial_paranoia_check(info, tty->name, __FUNCTION__))
3542         return -ENODEV;
3543
3544     card = info->card;
3545     channel = (info->line) - (cy_card[card].first_line);
3546     if (!IS_CYC_Z(cy_card[card])) {
3547         chip = channel>>2;
3548         channel &= 0x03;
3549         index = cy_card[card].bus_index;
3550         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3551
3552         CY_LOCK(info, flags);
3553             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3554             status = cy_readb(base_addr+(CyMSVR1<<index));
3555             status |= cy_readb(base_addr+(CyMSVR2<<index));
3556         CY_UNLOCK(info, flags);
3557
3558         if (info->rtsdtr_inv) {
3559             result =  ((status  & CyRTS) ? TIOCM_DTR : 0)
3560                     | ((status  & CyDTR) ? TIOCM_RTS : 0);
3561         } else {
3562             result =  ((status  & CyRTS) ? TIOCM_RTS : 0)
3563                     | ((status  & CyDTR) ? TIOCM_DTR : 0);
3564         }
3565         result |=  ((status  & CyDCD) ? TIOCM_CAR : 0)
3566                  | ((status  & CyRI) ? TIOCM_RNG : 0)
3567                  | ((status  & CyDSR) ? TIOCM_DSR : 0)
3568                  | ((status  & CyCTS) ? TIOCM_CTS : 0);
3569     } else {
3570         base_addr = cy_card[card].base_addr;
3571
3572         if (cy_card[card].num_chips != -1){
3573             return -EINVAL;
3574         }
3575
3576         firm_id = cy_card[card].base_addr + ID_ADDRESS;
3577         if (ISZLOADED(cy_card[card])) {
3578             zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
3579             board_ctrl = &zfw_ctrl->board_ctrl;
3580             ch_ctrl = zfw_ctrl->ch_ctrl;
3581             lstatus = cy_readl(&ch_ctrl[channel].rs_status);
3582             result =  ((lstatus  & C_RS_RTS) ? TIOCM_RTS : 0)
3583                     | ((lstatus  & C_RS_DTR) ? TIOCM_DTR : 0)
3584                     | ((lstatus  & C_RS_DCD) ? TIOCM_CAR : 0)
3585                     | ((lstatus  & C_RS_RI) ? TIOCM_RNG : 0)
3586                     | ((lstatus  & C_RS_DSR) ? TIOCM_DSR : 0)
3587                     | ((lstatus  & C_RS_CTS) ? TIOCM_CTS : 0);
3588         }else{
3589             result = 0;
3590             return -ENODEV;
3591         }
3592
3593     }
3594     return result;
3595 } /* cy_tiomget */
3596
3597
3598 static int
3599 cy_tiocmset(struct tty_struct *tty, struct file *file,
3600             unsigned int set, unsigned int clear)
3601 {
3602   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3603   int card,chip,channel,index;
3604   void __iomem *base_addr;
3605   unsigned long flags;
3606   struct FIRM_ID __iomem *firm_id;
3607   struct ZFW_CTRL __iomem *zfw_ctrl;
3608   struct BOARD_CTRL __iomem *board_ctrl;
3609   struct CH_CTRL __iomem *ch_ctrl;
3610   int retval;
3611
3612     if (serial_paranoia_check(info, tty->name, __FUNCTION__))
3613         return -ENODEV;
3614
3615     card = info->card;
3616     channel = (info->line) - (cy_card[card].first_line);
3617     if (!IS_CYC_Z(cy_card[card])) {
3618         chip = channel>>2;
3619         channel &= 0x03;
3620         index = cy_card[card].bus_index;
3621         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3622
3623         if (set & TIOCM_RTS){
3624                 CY_LOCK(info, flags);
3625                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3626                 if (info->rtsdtr_inv) {
3627                         cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
3628                 } else {
3629                         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
3630                 }
3631                 CY_UNLOCK(info, flags);
3632         }
3633         if (clear & TIOCM_RTS) {
3634                 CY_LOCK(info, flags);
3635                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3636                 if (info->rtsdtr_inv) {
3637                         cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
3638                 } else {
3639                         cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
3640                 }
3641                 CY_UNLOCK(info, flags);
3642         }
3643         if (set & TIOCM_DTR){
3644                 CY_LOCK(info, flags);
3645                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3646                 if (info->rtsdtr_inv) {
3647                         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
3648                 } else {
3649                         cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
3650                 }
3651 #ifdef CY_DEBUG_DTR
3652                 printk("cyc:set_modem_info raising DTR\n");
3653                 printk("     status: 0x%x, 0x%x\n",
3654                     cy_readb(base_addr+(CyMSVR1<<index)), 
3655                     cy_readb(base_addr+(CyMSVR2<<index)));
3656 #endif
3657                 CY_UNLOCK(info, flags);
3658         }
3659         if (clear & TIOCM_DTR) {
3660                 CY_LOCK(info, flags);
3661                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3662                 if (info->rtsdtr_inv) {
3663                         cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
3664                 } else {
3665                         cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
3666                 }
3667
3668 #ifdef CY_DEBUG_DTR
3669                 printk("cyc:set_modem_info dropping DTR\n");
3670                 printk("     status: 0x%x, 0x%x\n",
3671                     cy_readb(base_addr+(CyMSVR1<<index)), 
3672                     cy_readb(base_addr+(CyMSVR2<<index)));
3673 #endif
3674                 CY_UNLOCK(info, flags);
3675         }
3676     } else {
3677         base_addr = cy_card[card].base_addr;
3678
3679         firm_id = cy_card[card].base_addr + ID_ADDRESS;
3680         if (ISZLOADED(cy_card[card])) {
3681             zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
3682             board_ctrl = &zfw_ctrl->board_ctrl;
3683             ch_ctrl = zfw_ctrl->ch_ctrl;
3684
3685             if (set & TIOCM_RTS){
3686                     CY_LOCK(info, flags);
3687                     cy_writel(&ch_ctrl[channel].rs_control,
3688                        cy_readl(&ch_ctrl[channel].rs_control) | C_RS_RTS);
3689                     CY_UNLOCK(info, flags);
3690             }
3691             if (clear & TIOCM_RTS) {
3692                     CY_LOCK(info, flags);
3693                     cy_writel(&ch_ctrl[channel].rs_control,
3694                        cy_readl(&ch_ctrl[channel].rs_control) & ~C_RS_RTS);
3695                     CY_UNLOCK(info, flags);
3696             }
3697             if (set & TIOCM_DTR){
3698                     CY_LOCK(info, flags);
3699                     cy_writel(&ch_ctrl[channel].rs_control,
3700                        cy_readl(&ch_ctrl[channel].rs_control) | C_RS_DTR);
3701 #ifdef CY_DEBUG_DTR
3702                     printk("cyc:set_modem_info raising Z DTR\n");
3703 #endif
3704                     CY_UNLOCK(info, flags);
3705             }
3706             if (clear & TIOCM_DTR) {
3707                     CY_LOCK(info, flags);
3708                     cy_writel(&ch_ctrl[channel].rs_control,
3709                        cy_readl(&ch_ctrl[channel].rs_control) & ~C_RS_DTR);
3710 #ifdef CY_DEBUG_DTR
3711                     printk("cyc:set_modem_info clearing Z DTR\n");
3712 #endif
3713                     CY_UNLOCK(info, flags);
3714             }
3715         }else{
3716             return -ENODEV;
3717         }
3718         CY_LOCK(info, flags);
3719         retval = cyz_issue_cmd(&cy_card[info->card],
3720                                     channel, C_CM_IOCTLM,0L);
3721         if (retval != 0){
3722             printk("cyc:set_modem_info retval on ttyC%d was %x\n",
3723                    info->line, retval);
3724         }
3725         CY_UNLOCK(info, flags);
3726     }
3727     return 0;
3728 } /* cy_tiocmset */
3729
3730 /*
3731  * cy_break() --- routine which turns the break handling on or off
3732  */
3733 static void
3734 cy_break(struct tty_struct *tty, int break_state)
3735 {
3736     struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3737     unsigned long flags;
3738
3739     if (serial_paranoia_check(info, tty->name, "cy_break"))
3740         return;
3741
3742     CY_LOCK(info, flags);
3743     if (!IS_CYC_Z(cy_card[info->card])) {
3744         /* Let the transmit ISR take care of this (since it
3745            requires stuffing characters into the output stream).
3746         */
3747         if (break_state == -1) {
3748             if (!info->breakon) {
3749                 info->breakon = 1;
3750                 if (!info->xmit_cnt) {
3751                     CY_UNLOCK(info, flags);
3752                     start_xmit(info);
3753                     CY_LOCK(info, flags);
3754                 }
3755             }
3756         } else {
3757             if (!info->breakoff) {
3758                 info->breakoff = 1;
3759                 if (!info->xmit_cnt) {
3760                     CY_UNLOCK(info, flags);
3761                     start_xmit(info);
3762                     CY_LOCK(info, flags);
3763                 }
3764             }
3765         }
3766     } else {
3767         int retval;
3768
3769         if (break_state == -1) {
3770             retval = cyz_issue_cmd(&cy_card[info->card],
3771                 (info->line) - (cy_card[info->card].first_line),
3772                 C_CM_SET_BREAK, 0L);
3773             if (retval != 0) {
3774                 printk("cyc:cy_break (set) retval on ttyC%d was %x\n",
3775                        info->line, retval);
3776             }
3777         } else {
3778             retval = cyz_issue_cmd(&cy_card[info->card],
3779                 (info->line) - (cy_card[info->card].first_line),
3780                 C_CM_CLR_BREAK, 0L);
3781             if (retval != 0) {
3782                 printk("cyc:cy_break (clr) retval on ttyC%d was %x\n",
3783                        info->line, retval);
3784             }
3785         }
3786     }
3787     CY_UNLOCK(info, flags);
3788 } /* cy_break */
3789
3790 static int
3791 get_mon_info(struct cyclades_port * info, struct cyclades_monitor __user * mon)
3792 {
3793
3794     if(copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
3795         return -EFAULT;
3796     info->mon.int_count  = 0;
3797     info->mon.char_count = 0;
3798     info->mon.char_max   = 0;
3799     info->mon.char_last  = 0;
3800     return 0;
3801 }/* get_mon_info */
3802
3803
3804 static int
3805 set_threshold(struct cyclades_port * info, unsigned long value)
3806 {
3807   void __iomem *base_addr;
3808   int card,channel,chip,index;
3809   unsigned long flags;
3810    
3811     card = info->card;
3812     channel = info->line - cy_card[card].first_line;
3813     if (!IS_CYC_Z(cy_card[card])) {
3814         chip = channel>>2;
3815         channel &= 0x03;
3816         index = cy_card[card].bus_index;
3817         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3818
3819         info->cor3 &= ~CyREC_FIFO;
3820         info->cor3 |= value & CyREC_FIFO;
3821
3822         CY_LOCK(info, flags);
3823             cy_writeb(base_addr+(CyCOR3<<index), info->cor3);
3824             cyy_issue_cmd(base_addr,CyCOR_CHANGE|CyCOR3ch,index);
3825         CY_UNLOCK(info, flags);
3826     } else {
3827         // Nothing to do!
3828     }
3829     return 0;
3830 }/* set_threshold */
3831
3832
3833 static int
3834 get_threshold(struct cyclades_port * info, unsigned long __user *value)
3835 {
3836   void __iomem *base_addr;
3837   int card,channel,chip,index;
3838   unsigned long tmp;
3839    
3840     card = info->card;
3841     channel = info->line - cy_card[card].first_line;
3842     if (!IS_CYC_Z(cy_card[card])) {
3843         chip = channel>>2;
3844         channel &= 0x03;
3845         index = cy_card[card].bus_index;
3846         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3847
3848         tmp = cy_readb(base_addr+(CyCOR3<<index)) & CyREC_FIFO;
3849         return put_user(tmp,value);
3850     } else {
3851         // Nothing to do!
3852         return 0;
3853     }
3854 }/* get_threshold */
3855
3856
3857 static int
3858 set_default_threshold(struct cyclades_port * info, unsigned long value)
3859 {
3860     info->default_threshold = value & 0x0f;
3861     return 0;
3862 }/* set_default_threshold */
3863
3864
3865 static int
3866 get_default_threshold(struct cyclades_port * info, unsigned long __user *value)
3867 {
3868     return put_user(info->default_threshold,value);
3869 }/* get_default_threshold */
3870
3871
3872 static int
3873 set_timeout(struct cyclades_port * info, unsigned long value)
3874 {
3875   void __iomem *base_addr;
3876   int card,channel,chip,index;
3877   unsigned long flags;
3878    
3879     card = info->card;
3880     channel = info->line - cy_card[card].first_line;
3881     if (!IS_CYC_Z(cy_card[card])) {
3882         chip = channel>>2;
3883         channel &= 0x03;
3884         index = cy_card[card].bus_index;
3885         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3886
3887         CY_LOCK(info, flags);
3888             cy_writeb(base_addr+(CyRTPR<<index), value & 0xff);
3889         CY_UNLOCK(info, flags);
3890     } else {
3891         // Nothing to do!
3892     }
3893     return 0;
3894 }/* set_timeout */
3895
3896
3897 static int
3898 get_timeout(struct cyclades_port * info, unsigned long __user *value)
3899 {
3900   void __iomem *base_addr;
3901   int card,channel,chip,index;
3902   unsigned long tmp;
3903    
3904     card = info->card;
3905     channel = info->line - cy_card[card].first_line;
3906     if (!IS_CYC_Z(cy_card[card])) {
3907         chip = channel>>2;
3908         channel &= 0x03;
3909         index = cy_card[card].bus_index;
3910         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3911
3912         tmp = cy_readb(base_addr+(CyRTPR<<index));
3913         return put_user(tmp,value);
3914     } else {
3915         // Nothing to do!
3916         return 0;
3917     }
3918 }/* get_timeout */
3919
3920
3921 static int
3922 set_default_timeout(struct cyclades_port * info, unsigned long value)
3923 {
3924     info->default_timeout = value & 0xff;
3925     return 0;
3926 }/* set_default_timeout */
3927
3928
3929 static int
3930 get_default_timeout(struct cyclades_port * info, unsigned long __user *value)
3931 {
3932     return put_user(info->default_timeout,value);
3933 }/* get_default_timeout */
3934
3935 /*
3936  * This routine allows the tty driver to implement device-
3937  * specific ioctl's.  If the ioctl number passed in cmd is
3938  * not recognized by the driver, it should return ENOIOCTLCMD.
3939  */
3940 static int
3941 cy_ioctl(struct tty_struct *tty, struct file * file,
3942             unsigned int cmd, unsigned long arg)
3943 {
3944   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3945   struct cyclades_icount cprev, cnow;           /* kernel counter temps */
3946   struct serial_icounter_struct __user *p_cuser;        /* user space */
3947   int ret_val = 0;
3948   unsigned long flags;
3949   void __user *argp = (void __user *)arg;
3950
3951     if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
3952         return -ENODEV;
3953
3954 #ifdef CY_DEBUG_OTHER
3955     printk("cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
3956         info->line, cmd, arg); /* */
3957 #endif
3958
3959     switch (cmd) {
3960         case CYGETMON:
3961             ret_val = get_mon_info(info, argp);
3962             break;
3963         case CYGETTHRESH:
3964             ret_val = get_threshold(info, argp);
3965             break;
3966         case CYSETTHRESH:
3967             ret_val = set_threshold(info, arg);
3968             break;
3969         case CYGETDEFTHRESH:
3970             ret_val = get_default_threshold(info, argp);
3971             break;
3972         case CYSETDEFTHRESH:
3973             ret_val = set_default_threshold(info, arg);
3974             break;
3975         case CYGETTIMEOUT:
3976             ret_val = get_timeout(info, argp);
3977             break;
3978         case CYSETTIMEOUT:
3979             ret_val = set_timeout(info, arg);
3980             break;
3981         case CYGETDEFTIMEOUT:
3982             ret_val = get_default_timeout(info, argp);
3983             break;
3984         case CYSETDEFTIMEOUT:
3985             ret_val = set_default_timeout(info, arg);
3986             break;
3987         case CYSETRFLOW:
3988             info->rflow = (int)arg;
3989             ret_val = 0;
3990             break;
3991         case CYGETRFLOW:
3992             ret_val = info->rflow;
3993             break;
3994         case CYSETRTSDTR_INV:
3995             info->rtsdtr_inv = (int)arg;
3996             ret_val = 0;
3997             break;
3998         case CYGETRTSDTR_INV:
3999             ret_val = info->rtsdtr_inv;
4000             break;
4001         case CYGETCARDINFO:
4002             if (copy_to_user(argp, &cy_card[info->card], 
4003                         sizeof (struct cyclades_card))) {
4004                 ret_val = -EFAULT;
4005                 break;
4006             }
4007             ret_val = 0;
4008             break;
4009         case CYGETCD1400VER:
4010             ret_val = info->chip_rev;
4011             break;
4012 #ifndef CONFIG_CYZ_INTR
4013         case CYZSETPOLLCYCLE:
4014             cyz_polling_cycle = (arg * HZ) / 1000;
4015             ret_val = 0;
4016             break;
4017         case CYZGETPOLLCYCLE:
4018             ret_val = (cyz_polling_cycle * 1000) / HZ;
4019             break;
4020 #endif /* CONFIG_CYZ_INTR */
4021         case CYSETWAIT:
4022             info->closing_wait = (unsigned short)arg * HZ/100;
4023             ret_val = 0;
4024             break;
4025         case CYGETWAIT:
4026             ret_val = info->closing_wait / (HZ/100);
4027             break;
4028         case TIOCGSERIAL:
4029             ret_val = get_serial_info(info, argp);
4030             break;
4031         case TIOCSSERIAL:
4032             ret_val = set_serial_info(info, argp);
4033             break;
4034         case TIOCSERGETLSR: /* Get line status register */
4035             ret_val = get_lsr_info(info, argp);
4036             break;
4037         /*
4038          * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 
4039          * - mask passed in arg for lines of interest
4040          *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
4041          * Caller should use TIOCGICOUNT to see which one it was
4042          */
4043         case TIOCMIWAIT:
4044             CY_LOCK(info, flags);
4045             /* note the counters on entry */
4046             cprev = info->icount;
4047             CY_UNLOCK(info, flags);
4048             while (1) {
4049                 interruptible_sleep_on(&info->delta_msr_wait);
4050                 /* see if a signal did it */
4051                 if (signal_pending(current)) {
4052                     return -ERESTARTSYS;
4053                 }
4054
4055                 CY_LOCK(info, flags);
4056                 cnow = info->icount; /* atomic copy */
4057                 CY_UNLOCK(info, flags);
4058
4059                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
4060                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
4061                     return -EIO; /* no change => error */
4062                 }
4063                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 
4064                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 
4065                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) || 
4066                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
4067                     return 0;
4068                 }
4069                 cprev = cnow;
4070             }
4071             /* NOTREACHED */
4072
4073         /*
4074          * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
4075          * Return: write counters to the user passed counter struct
4076          * NB: both 1->0 and 0->1 transitions are counted except for
4077          *     RI where only 0->1 is counted.
4078          */
4079         case TIOCGICOUNT:
4080             CY_LOCK(info, flags);
4081             cnow = info->icount;
4082             CY_UNLOCK(info, flags);
4083             p_cuser = argp;
4084             ret_val = put_user(cnow.cts, &p_cuser->cts);
4085             if (ret_val) return ret_val;
4086             ret_val = put_user(cnow.dsr, &p_cuser->dsr);
4087             if (ret_val) return ret_val;
4088             ret_val = put_user(cnow.rng, &p_cuser->rng);
4089             if (ret_val) return ret_val;
4090             ret_val = put_user(cnow.dcd, &p_cuser->dcd);
4091             if (ret_val) return ret_val;
4092             ret_val = put_user(cnow.rx, &p_cuser->rx);
4093             if (ret_val) return ret_val;
4094             ret_val = put_user(cnow.tx, &p_cuser->tx);
4095             if (ret_val) return ret_val;
4096             ret_val = put_user(cnow.frame, &p_cuser->frame);
4097             if (ret_val) return ret_val;
4098             ret_val = put_user(cnow.overrun, &p_cuser->overrun);
4099             if (ret_val) return ret_val;
4100             ret_val = put_user(cnow.parity, &p_cuser->parity);
4101             if (ret_val) return ret_val;
4102             ret_val = put_user(cnow.brk, &p_cuser->brk);
4103             if (ret_val) return ret_val;
4104             ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
4105             if (ret_val) return ret_val;
4106             ret_val = 0;
4107             break;
4108         default:
4109             ret_val = -ENOIOCTLCMD;
4110     }
4111
4112 #ifdef CY_DEBUG_OTHER
4113     printk(" cyc:cy_ioctl done\n");
4114 #endif
4115
4116     return ret_val;
4117 } /* cy_ioctl */
4118
4119
4120 /*
4121  * This routine allows the tty driver to be notified when
4122  * device's termios settings have changed.  Note that a
4123  * well-designed tty driver should be prepared to accept the case
4124  * where old == NULL, and try to do something rational.
4125  */
4126 static void
4127 cy_set_termios(struct tty_struct *tty, struct termios * old_termios)
4128 {
4129   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4130
4131 #ifdef CY_DEBUG_OTHER
4132     printk("cyc:cy_set_termios ttyC%d\n", info->line);
4133 #endif
4134
4135     if ((tty->termios->c_cflag == old_termios->c_cflag) &&
4136         ((tty->termios->c_iflag & (IXON|IXANY)) == 
4137          (old_termios->c_iflag & (IXON|IXANY))))
4138         return;
4139     set_line_char(info);
4140
4141     if ((old_termios->c_cflag & CRTSCTS) &&
4142         !(tty->termios->c_cflag & CRTSCTS)) {
4143             tty->hw_stopped = 0;
4144             cy_start(tty);
4145     }
4146 #if 0
4147     /*
4148      * No need to wake up processes in open wait, since they
4149      * sample the CLOCAL flag once, and don't recheck it.
4150      * XXX  It's not clear whether the current behavior is correct
4151      * or not.  Hence, this may change.....
4152      */
4153     if (!(old_termios->c_cflag & CLOCAL) &&
4154         (tty->termios->c_cflag & CLOCAL))
4155             wake_up_interruptible(&info->open_wait);
4156 #endif
4157
4158     return;
4159 } /* cy_set_termios */
4160
4161 /* This function is used to send a high-priority XON/XOFF character to
4162    the device.
4163 */
4164 static void
4165 cy_send_xchar (struct tty_struct *tty, char ch)
4166 {
4167         struct cyclades_port *info = (struct cyclades_port *) tty->driver_data;
4168         int card, channel;
4169
4170         if (serial_paranoia_check (info, tty->name, "cy_send_xchar"))
4171                 return;
4172
4173         info->x_char = ch;
4174
4175         if (ch)
4176                 cy_start (tty);
4177
4178         card = info->card;
4179         channel = info->line - cy_card[card].first_line;
4180
4181         if (IS_CYC_Z (cy_card[card])) {
4182                 if (ch == STOP_CHAR (tty))
4183                         cyz_issue_cmd (&cy_card[card], channel, C_CM_SENDXOFF, 0L);
4184                 else if (ch == START_CHAR (tty))
4185                         cyz_issue_cmd (&cy_card[card], channel, C_CM_SENDXON, 0L);
4186         }
4187 }
4188
4189 /* This routine is called by the upper-layer tty layer to signal
4190    that incoming characters should be throttled because the input
4191    buffers are close to full.
4192  */
4193 static void
4194 cy_throttle(struct tty_struct * tty)
4195 {
4196   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4197   unsigned long flags;
4198   void __iomem *base_addr;
4199   int card,chip,channel,index;
4200
4201 #ifdef CY_DEBUG_THROTTLE
4202   char buf[64];
4203
4204     printk("cyc:throttle %s: %d....ttyC%d\n", 
4205            tty_name(tty, buf),
4206            tty->ldisc.chars_in_buffer(tty), info->line);
4207 #endif
4208
4209     if (serial_paranoia_check(info, tty->name, "cy_throttle")){
4210             return;
4211     }
4212
4213     card = info->card;
4214
4215     if (I_IXOFF(tty)) {
4216         if (!IS_CYC_Z (cy_card[card]))
4217             cy_send_xchar (tty, STOP_CHAR (tty));
4218         else
4219             info->throttle = 1;
4220     }
4221
4222     if (tty->termios->c_cflag & CRTSCTS) {
4223         channel = info->line - cy_card[card].first_line;
4224         if (!IS_CYC_Z(cy_card[card])) {
4225             chip = channel>>2;
4226             channel &= 0x03;
4227             index = cy_card[card].bus_index;
4228             base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
4229
4230             CY_LOCK(info, flags);
4231             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
4232             if (info->rtsdtr_inv) {
4233                 cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
4234              } else {
4235                 cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
4236              }
4237             CY_UNLOCK(info, flags);
4238         } else {
4239             info->throttle = 1;
4240         }
4241     }
4242
4243     return;
4244 } /* cy_throttle */
4245
4246
4247 /*
4248  * This routine notifies the tty driver that it should signal
4249  * that characters can now be sent to the tty without fear of
4250  * overrunning the input buffers of the line disciplines.
4251  */
4252 static void
4253 cy_unthrottle(struct tty_struct * tty)
4254 {
4255   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4256   unsigned long flags;
4257   void __iomem *base_addr;
4258   int card,chip,channel,index;
4259
4260 #ifdef CY_DEBUG_THROTTLE
4261   char buf[64];
4262         
4263     printk("cyc:unthrottle %s: %d....ttyC%d\n", 
4264            tty_name(tty, buf),
4265            tty->ldisc.chars_in_buffer(tty), info->line);
4266 #endif
4267
4268     if (serial_paranoia_check(info, tty->name, "cy_unthrottle")){
4269             return;
4270     }
4271
4272     if (I_IXOFF(tty)) {
4273         if (info->x_char)
4274             info->x_char = 0;
4275         else
4276             cy_send_xchar (tty, START_CHAR (tty));
4277     }
4278
4279     if (tty->termios->c_cflag & CRTSCTS) {
4280         card = info->card;
4281         channel = info->line - cy_card[card].first_line;
4282         if (!IS_CYC_Z(cy_card[card])) {
4283             chip = channel>>2;
4284             channel &= 0x03;
4285             index = cy_card[card].bus_index;
4286             base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
4287
4288             CY_LOCK(info, flags);
4289             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
4290             if (info->rtsdtr_inv) {
4291                     cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
4292             } else {
4293                     cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
4294             }
4295             CY_UNLOCK(info, flags);
4296         } else {
4297             info->throttle = 0;
4298         }
4299     }
4300
4301     return;
4302 } /* cy_unthrottle */
4303
4304
4305 /* cy_start and cy_stop provide software output flow control as a
4306    function of XON/XOFF, software CTS, and other such stuff.
4307 */
4308 static void
4309 cy_stop(struct tty_struct *tty)
4310 {
4311   struct cyclades_card *cinfo;
4312   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4313   void __iomem *base_addr;
4314   int chip,channel,index;
4315   unsigned long flags;
4316
4317 #ifdef CY_DEBUG_OTHER
4318     printk("cyc:cy_stop ttyC%d\n", info->line); /* */
4319 #endif
4320
4321     if (serial_paranoia_check(info, tty->name, "cy_stop"))
4322         return;
4323         
4324     cinfo = &cy_card[info->card];
4325     channel = info->line - cinfo->first_line;
4326     if (!IS_CYC_Z(*cinfo)) {
4327         index = cinfo->bus_index;
4328         chip = channel>>2;
4329         channel &= 0x03;
4330         base_addr = cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index);
4331
4332         CY_LOCK(info, flags);
4333             cy_writeb(base_addr+(CyCAR<<index),
4334                (u_char)(channel & 0x0003)); /* index channel */
4335             cy_writeb(base_addr+(CySRER<<index), 
4336                cy_readb(base_addr+(CySRER<<index)) & ~CyTxRdy);
4337         CY_UNLOCK(info, flags);
4338     } else {
4339         // Nothing to do!
4340     }
4341
4342     return;
4343 } /* cy_stop */
4344
4345
4346 static void
4347 cy_start(struct tty_struct *tty)
4348 {
4349   struct cyclades_card *cinfo;
4350   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4351   void __iomem *base_addr;
4352   int chip,channel,index;
4353   unsigned long flags;
4354
4355 #ifdef CY_DEBUG_OTHER
4356     printk("cyc:cy_start ttyC%d\n", info->line); /* */
4357 #endif
4358
4359     if (serial_paranoia_check(info, tty->name, "cy_start"))
4360         return;
4361         
4362     cinfo = &cy_card[info->card];
4363     channel = info->line - cinfo->first_line;
4364     index = cinfo->bus_index;
4365     if (!IS_CYC_Z(*cinfo)) {
4366         chip = channel>>2;
4367         channel &= 0x03;
4368         base_addr = cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index);
4369
4370         CY_LOCK(info, flags);
4371             cy_writeb(base_addr+(CyCAR<<index),
4372                (u_char)(channel & 0x0003)); /* index channel */
4373             cy_writeb(base_addr+(CySRER<<index), 
4374                cy_readb(base_addr+(CySRER<<index)) | CyTxRdy);
4375         CY_UNLOCK(info, flags);
4376     } else {
4377         // Nothing to do!
4378     }
4379
4380     return;
4381 } /* cy_start */
4382
4383
4384 static void
4385 cy_flush_buffer(struct tty_struct *tty)
4386 {
4387   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4388   int card, channel, retval;
4389   unsigned long flags;
4390                                 
4391 #ifdef CY_DEBUG_IO
4392     printk("cyc:cy_flush_buffer ttyC%d\n", info->line); /* */
4393 #endif
4394
4395     if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
4396         return;
4397
4398     card = info->card;
4399     channel = (info->line) - (cy_card[card].first_line);
4400
4401     CY_LOCK(info, flags);
4402     info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
4403     CY_UNLOCK(info, flags);
4404
4405     if (IS_CYC_Z(cy_card[card])) { /* If it is a Z card, flush the on-board 
4406                                       buffers as well */
4407         CY_LOCK(info, flags);
4408         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_FLUSH_TX, 0L);
4409         if (retval != 0) {
4410             printk("cyc: flush_buffer retval on ttyC%d was %x\n",
4411                    info->line, retval);
4412         }
4413         CY_UNLOCK(info, flags);
4414     }
4415     tty_wakeup(tty);
4416     wake_up_interruptible(&tty->write_wait);
4417 } /* cy_flush_buffer */
4418
4419
4420 /*
4421  * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
4422  */
4423 static void
4424 cy_hangup(struct tty_struct *tty)
4425 {
4426   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
4427         
4428 #ifdef CY_DEBUG_OTHER
4429     printk("cyc:cy_hangup ttyC%d\n", info->line); /* */
4430 #endif
4431
4432     if (serial_paranoia_check(info, tty->name, "cy_hangup"))
4433         return;
4434
4435     cy_flush_buffer(tty);
4436     shutdown(info);
4437     info->event = 0;
4438     info->count = 0;
4439 #ifdef CY_DEBUG_COUNT
4440     printk("cyc:cy_hangup (%d): setting count to 0\n", current->pid);
4441 #endif
4442     info->tty = NULL;
4443     info->flags &= ~ASYNC_NORMAL_ACTIVE;
4444     wake_up_interruptible(&info->open_wait);
4445 } /* cy_hangup */
4446
4447
4448 /*
4449  * ---------------------------------------------------------------------
4450  * cy_init() and friends
4451  *
4452  * cy_init() is called at boot-time to initialize the serial driver.
4453  * ---------------------------------------------------------------------
4454  */
4455
4456 /* initialize chips on Cyclom-Y card -- return number of valid
4457    chips (which is number of ports/4) */
4458 static unsigned short __init
4459 cyy_init_card(void __iomem *true_base_addr,int index)
4460 {
4461   unsigned int chip_number;
4462   void __iomem *base_addr;
4463
4464     cy_writeb(true_base_addr+(Cy_HwReset<<index), 0); 
4465                                                 /* Cy_HwReset is 0x1400 */
4466     cy_writeb(true_base_addr+(Cy_ClrIntr<<index), 0); 
4467                                                 /* Cy_ClrIntr is 0x1800 */
4468     udelay(500L);
4469
4470     for(chip_number=0; chip_number<CyMAX_CHIPS_PER_CARD; chip_number++){
4471         base_addr = true_base_addr + (cy_chip_offset[chip_number]<<index);
4472         mdelay(1);
4473         if(cy_readb(base_addr+(CyCCR<<index)) != 0x00){
4474             /*************
4475             printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
4476                chip_number, (unsigned long)base_addr);
4477             *************/
4478             return chip_number;
4479         }
4480
4481         cy_writeb(base_addr+(CyGFRCR<<index), 0);
4482         udelay(10L);
4483
4484         /* The Cyclom-16Y does not decode address bit 9 and therefore
4485            cannot distinguish between references to chip 0 and a non-
4486            existent chip 4.  If the preceding clearing of the supposed
4487            chip 4 GFRCR register appears at chip 0, there is no chip 4
4488            and this must be a Cyclom-16Y, not a Cyclom-32Ye.
4489         */
4490         if (chip_number == 4
4491         && cy_readb(true_base_addr
4492             + (cy_chip_offset[0]<<index)
4493             + (CyGFRCR<<index)) == 0){
4494             return chip_number;
4495         }
4496
4497         cy_writeb(base_addr+(CyCCR<<index), CyCHIP_RESET);
4498         mdelay(1);
4499
4500         if(cy_readb(base_addr+(CyGFRCR<<index)) == 0x00){
4501             /*
4502             printk(" chip #%d at %#6lx is not responding ",
4503                chip_number, (unsigned long)base_addr);
4504             printk("(GFRCR stayed 0)\n",
4505             */
4506             return chip_number;
4507         }
4508         if((0xf0 & (cy_readb(base_addr+(CyGFRCR<<index)))) != 0x40){
4509             /*
4510             printk(" chip #%d at %#6lx is not valid (GFRCR == %#2x)\n",
4511                chip_number, (unsigned long)base_addr,
4512                base_addr[CyGFRCR<<index]);
4513             */
4514             return chip_number;
4515         }
4516         cy_writeb(base_addr+(CyGCR<<index), CyCH0_SERIAL);
4517         if (cy_readb(base_addr+(CyGFRCR<<index)) >= CD1400_REV_J){
4518             /* It is a CD1400 rev. J or later */
4519             /* Impossible to reach 5ms with this chip. 
4520                Changed to 2ms instead (f = 500 Hz). */
4521             cy_writeb(base_addr+(CyPPR<<index), CyCLOCK_60_2MS);
4522         } else {
4523             /* f = 200 Hz */
4524             cy_writeb(base_addr+(CyPPR<<index), CyCLOCK_25_5MS);
4525         }
4526
4527     /*
4528         printk(" chip #%d at %#6lx is rev 0x%2x\n",
4529                chip_number, (unsigned long)base_addr,
4530                cy_readb(base_addr+(CyGFRCR<<index)));
4531     */
4532     }
4533     return chip_number;
4534 } /* cyy_init_card */
4535
4536 /*
4537  * ---------------------------------------------------------------------
4538  * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
4539  * sets global variables and return the number of ISA boards found.
4540  * ---------------------------------------------------------------------
4541  */
4542 static int __init
4543 cy_detect_isa(void)
4544 {
4545 #ifdef CONFIG_ISA
4546   unsigned short        cy_isa_irq,nboard;
4547   void __iomem          *cy_isa_address;
4548   unsigned short        i,j,cy_isa_nchan;
4549 #ifdef MODULE
4550   int isparam = 0;
4551 #endif
4552
4553         nboard = 0;
4554
4555 #ifdef MODULE
4556         /* Check for module parameters */
4557         for(i = 0 ; i < NR_CARDS; i++) {
4558             if (maddr[i] || i) {
4559                 isparam = 1;
4560                 cy_isa_addresses[i] = maddr[i];
4561             }
4562             if (!maddr[i])
4563                 break;
4564         }
4565 #endif
4566
4567         /* scan the address table probing for Cyclom-Y/ISA boards */
4568         for (i = 0 ; i < NR_ISA_ADDRS ; i++) {
4569                 unsigned int isa_address = cy_isa_addresses[i];
4570                 if (isa_address  == 0x0000) {
4571                         return(nboard);
4572                 }
4573
4574                 /* probe for CD1400... */
4575                 cy_isa_address = ioremap(isa_address, CyISA_Ywin);
4576                 cy_isa_nchan = CyPORTS_PER_CHIP * 
4577                      cyy_init_card(cy_isa_address,0);
4578                 if (cy_isa_nchan == 0) {
4579                         continue;
4580                 }
4581
4582 #ifdef MODULE
4583                 if (isparam && irq[i])
4584                     cy_isa_irq = irq[i];
4585                 else
4586 #endif
4587                 /* find out the board's irq by probing */
4588                 cy_isa_irq = detect_isa_irq(cy_isa_address);
4589                 if (cy_isa_irq == 0) {
4590                         printk("Cyclom-Y/ISA found at 0x%lx ",
4591                                 (unsigned long) cy_isa_address);
4592                         printk("but the IRQ could not be detected.\n");
4593                         continue;
4594                 }
4595
4596                 if((cy_next_channel+cy_isa_nchan) > NR_PORTS) {
4597                         printk("Cyclom-Y/ISA found at 0x%lx ",
4598                                 (unsigned long) cy_isa_address);
4599                         printk("but no more channels are available.\n");
4600                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
4601                         return(nboard);
4602                 }
4603                 /* fill the next cy_card structure available */
4604                 for (j = 0 ; j < NR_CARDS ; j++) {
4605                         if (cy_card[j].base_addr == 0)  break;
4606                 }
4607                 if (j == NR_CARDS) {    /* no more cy_cards available */
4608                         printk("Cyclom-Y/ISA found at 0x%lx ",
4609                                 (unsigned long) cy_isa_address);
4610                         printk("but no more cards can be used .\n");
4611                         printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4612                         return(nboard);
4613                 }
4614
4615                 /* allocate IRQ */
4616                 if(request_irq(cy_isa_irq, cyy_interrupt,
4617                                    SA_INTERRUPT, "Cyclom-Y", &cy_card[j]))
4618                 {
4619                         printk("Cyclom-Y/ISA found at 0x%lx ",
4620                                 (unsigned long) cy_isa_address);
4621                         printk("but could not allocate IRQ#%d.\n",
4622                                 cy_isa_irq);
4623                         return(nboard);
4624                 }
4625
4626                 /* set cy_card */
4627                 cy_card[j].base_addr = cy_isa_address;
4628                 cy_card[j].ctl_addr = NULL;
4629                 cy_card[j].irq = (int) cy_isa_irq;
4630                 cy_card[j].bus_index = 0;
4631                 cy_card[j].first_line = cy_next_channel;
4632                 cy_card[j].num_chips = cy_isa_nchan/4;
4633                 nboard++;
4634                         
4635                 /* print message */
4636                 printk("Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d, ",
4637                     j+1, (unsigned long) cy_isa_address,
4638                     (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
4639                     cy_isa_irq);
4640                 printk("%d channels starting from port %d.\n",
4641                         cy_isa_nchan, cy_next_channel);
4642                 cy_next_channel += cy_isa_nchan;
4643         }
4644         return(nboard);
4645 #else
4646         return(0);
4647 #endif /* CONFIG_ISA */
4648 } /* cy_detect_isa */
4649
4650 static void 
4651 plx_init(void __iomem *addr, uclong initctl)
4652 {
4653     /* Reset PLX */
4654     cy_writel(addr + initctl, cy_readl(addr + initctl) | 0x40000000);
4655     udelay(100L);
4656     cy_writel(addr + initctl, cy_readl(addr + initctl) & ~0x40000000);
4657
4658     /* Reload Config. Registers from EEPROM */
4659     cy_writel(addr + initctl, cy_readl(addr + initctl) | 0x20000000);
4660     udelay(100L);
4661     cy_writel(addr + initctl, cy_readl(addr + initctl) & ~0x20000000);
4662 }
4663
4664 /*
4665  * ---------------------------------------------------------------------
4666  * cy_detect_pci() - Test PCI bus presence and Cyclom-Ye/PCI.
4667  * sets global variables and return the number of PCI boards found.
4668  * ---------------------------------------------------------------------
4669  */
4670 static int __init
4671 cy_detect_pci(void)
4672 {
4673 #ifdef CONFIG_PCI
4674
4675   struct pci_dev        *pdev = NULL;
4676   unsigned char         cyy_rev_id;
4677   unsigned char         cy_pci_irq = 0;
4678   uclong                cy_pci_phys0, cy_pci_phys2;
4679   void __iomem          *cy_pci_addr0, *cy_pci_addr2;
4680   unsigned short        i,j,cy_pci_nchan, plx_ver;
4681   unsigned short        device_id,dev_index = 0;
4682   uclong                mailbox;
4683   uclong                ZeIndex = 0;
4684   void __iomem          *Ze_addr0[NR_CARDS], *Ze_addr2[NR_CARDS];
4685   uclong                Ze_phys0[NR_CARDS], Ze_phys2[NR_CARDS];
4686   unsigned char         Ze_irq[NR_CARDS];
4687   struct pci_dev        *Ze_pdev[NR_CARDS];
4688
4689         for (i = 0; i < NR_CARDS; i++) {
4690                 /* look for a Cyclades card by vendor and device id */
4691                 while((device_id = cy_pci_dev_id[dev_index]) != 0) {
4692                         if((pdev = pci_get_device(PCI_VENDOR_ID_CYCLADES,
4693                                         device_id, pdev)) == NULL) {
4694                                 dev_index++;    /* try next device id */
4695                         } else {
4696                                 break;          /* found a board */
4697                         }
4698                 }
4699
4700                 if (device_id == 0)
4701                     break;
4702
4703                 if (pci_enable_device(pdev))
4704                     continue;
4705
4706                 /* read PCI configuration area */
4707                 cy_pci_irq = pdev->irq;
4708                 cy_pci_phys0 = pci_resource_start(pdev, 0);
4709                 cy_pci_phys2 = pci_resource_start(pdev, 2);
4710                 pci_read_config_byte(pdev, PCI_REVISION_ID, &cyy_rev_id);
4711
4712                 device_id &= ~PCI_DEVICE_ID_MASK;
4713
4714     if ((device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo)
4715            || (device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi)){
4716 #ifdef CY_PCI_DEBUG
4717             printk("Cyclom-Y/PCI (bus=0x0%x, pci_id=0x%x, ",
4718                 pdev->bus->number, pdev->devfn);
4719             printk("rev_id=%d) IRQ%d\n",
4720                 cyy_rev_id, (int)cy_pci_irq);
4721             printk("Cyclom-Y/PCI:found  winaddr=0x%lx ctladdr=0x%lx\n",
4722                 (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4723 #endif
4724
4725                 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
4726                     printk("  Warning: PCI I/O bit incorrectly set. "
4727                            "Ignoring it...\n");
4728                     pdev->resource[2].flags &= ~IORESOURCE_IO;
4729                 }
4730
4731                 /* Although we don't use this I/O region, we should
4732                    request it from the kernel anyway, to avoid problems
4733                    with other drivers accessing it. */
4734                 if (pci_request_regions(pdev, "Cyclom-Y") != 0) {
4735                         printk(KERN_ERR "cyclades: failed to reserve PCI resources\n");
4736                         continue;
4737                 }
4738
4739 #if defined(__alpha__)
4740                 if (device_id  == PCI_DEVICE_ID_CYCLOM_Y_Lo) { /* below 1M? */
4741                     printk("Cyclom-Y/PCI (bus=0x0%x, pci_id=0x%x, ",
4742                         pdev->bus->number, pdev->devfn);
4743                     printk("rev_id=%d) IRQ%d\n",
4744                         cyy_rev_id, (int)cy_pci_irq);
4745                     printk("Cyclom-Y/PCI:found  winaddr=0x%lx ctladdr=0x%lx\n",
4746                         (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4747                     printk("Cyclom-Y/PCI not supported for low addresses in "
4748                            "Alpha systems.\n");
4749                     i--;
4750                     continue;
4751                 }
4752 #endif
4753                 cy_pci_addr0 = ioremap(cy_pci_phys0, CyPCI_Yctl);
4754                 cy_pci_addr2 = ioremap(cy_pci_phys2, CyPCI_Ywin);
4755
4756 #ifdef CY_PCI_DEBUG
4757             printk("Cyclom-Y/PCI: relocate winaddr=0x%lx ctladdr=0x%lx\n",
4758                 (u_long)cy_pci_addr2, (u_long)cy_pci_addr0);
4759 #endif
4760                 cy_pci_nchan = (unsigned short)(CyPORTS_PER_CHIP * 
4761                        cyy_init_card(cy_pci_addr2, 1));
4762                 if(cy_pci_nchan == 0) {
4763                         printk("Cyclom-Y PCI host card with ");
4764                         printk("no Serial-Modules at 0x%lx.\n",
4765                             (ulong) cy_pci_phys2);
4766                         i--;
4767                         continue;
4768                 }
4769                 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
4770                         printk("Cyclom-Y/PCI found at 0x%lx ",
4771                             (ulong) cy_pci_phys2);
4772                         printk("but no channels are available.\n");
4773                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
4774                         return(i);
4775                 }
4776                 /* fill the next cy_card structure available */
4777                 for (j = 0 ; j < NR_CARDS ; j++) {
4778                         if (cy_card[j].base_addr == 0)  break;
4779                 }
4780                 if (j == NR_CARDS) {    /* no more cy_cards available */
4781                         printk("Cyclom-Y/PCI found at 0x%lx ",
4782                             (ulong) cy_pci_phys2);
4783                         printk("but no more cards can be used.\n");
4784                         printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4785                         return(i);
4786                 }
4787
4788                 /* allocate IRQ */
4789                 if(request_irq(cy_pci_irq, cyy_interrupt,
4790                         SA_SHIRQ, "Cyclom-Y", &cy_card[j]))
4791                 {
4792                         printk("Cyclom-Y/PCI found at 0x%lx ",
4793                             (ulong) cy_pci_phys2);
4794                         printk("but could not allocate IRQ%d.\n",
4795                             cy_pci_irq);
4796                         return(i);
4797                 }
4798
4799                 /* set cy_card */
4800                 cy_card[j].base_phys = (ulong)cy_pci_phys2;
4801                 cy_card[j].ctl_phys = (ulong)cy_pci_phys0;
4802                 cy_card[j].base_addr = cy_pci_addr2;
4803                 cy_card[j].ctl_addr = cy_pci_addr0;
4804                 cy_card[j].irq = (int) cy_pci_irq;
4805                 cy_card[j].bus_index = 1;
4806                 cy_card[j].first_line = cy_next_channel;
4807                 cy_card[j].num_chips = cy_pci_nchan/4;
4808                 cy_card[j].pdev = pdev;
4809         
4810                 /* enable interrupts in the PCI interface */
4811                 plx_ver = cy_readb(cy_pci_addr2 + CyPLX_VER) & 0x0f;
4812                 switch (plx_ver) {
4813                     case PLX_9050:
4814
4815                     cy_writeb(cy_pci_addr0+0x4c, 0x43);
4816                     break;
4817
4818                     case PLX_9060:
4819                     case PLX_9080:
4820                     default: /* Old boards, use PLX_9060 */
4821
4822                     plx_init(cy_pci_addr0, 0x6c);
4823                     /* For some yet unknown reason, once the PLX9060 reloads
4824                        the EEPROM, the IRQ is lost and, thus, we have to
4825                        re-write it to the PCI config. registers.
4826                        This will remain here until we find a permanent fix. */
4827                     pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, cy_pci_irq);
4828
4829                     cy_writew(cy_pci_addr0+0x68, 
4830                         cy_readw(cy_pci_addr0+0x68)|0x0900);
4831                     break;
4832                 }
4833
4834                 /* print message */
4835                 printk("Cyclom-Y/PCI #%d: 0x%lx-0x%lx, IRQ%d, ",
4836                        j+1, 
4837                        (ulong)cy_pci_phys2, 
4838                        (ulong)(cy_pci_phys2 + CyPCI_Ywin - 1),
4839                        (int)cy_pci_irq);
4840                 printk("%d channels starting from port %d.\n",
4841                     cy_pci_nchan, cy_next_channel);
4842
4843                 cy_next_channel += cy_pci_nchan;
4844     }else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo){
4845             /* print message */
4846                 printk("Cyclades-Z/PCI (bus=0x0%x, pci_id=0x%x, ",
4847                     pdev->bus->number, pdev->devfn);
4848                 printk("rev_id=%d) IRQ%d\n",
4849                     cyy_rev_id, (int)cy_pci_irq);
4850                 printk("Cyclades-Z/PCI: found winaddr=0x%lx ctladdr=0x%lx\n",
4851                     (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4852             printk("Cyclades-Z/PCI not supported for low addresses\n");
4853             break;
4854     }else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi){
4855 #ifdef CY_PCI_DEBUG
4856             printk("Cyclades-Z/PCI (bus=0x0%x, pci_id=0x%x, ",
4857                 pdev->bus->number, pdev->devfn);
4858             printk("rev_id=%d) IRQ%d\n",
4859                 cyy_rev_id, (int)cy_pci_irq);
4860             printk("Cyclades-Z/PCI: found winaddr=0x%lx ctladdr=0x%lx\n",
4861                 (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4862 #endif
4863                 cy_pci_addr0 = ioremap(cy_pci_phys0, CyPCI_Zctl);
4864
4865                 /* Disable interrupts on the PLX before resetting it */
4866                 cy_writew(cy_pci_addr0+0x68,
4867                         cy_readw(cy_pci_addr0+0x68) & ~0x0900);
4868
4869                 plx_init(cy_pci_addr0, 0x6c);
4870                 /* For some yet unknown reason, once the PLX9060 reloads
4871                    the EEPROM, the IRQ is lost and, thus, we have to
4872                    re-write it to the PCI config. registers.
4873                    This will remain here until we find a permanent fix. */
4874                 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, cy_pci_irq);
4875
4876                 mailbox = (uclong)cy_readl(&((struct RUNTIME_9060 __iomem *) 
4877                            cy_pci_addr0)->mail_box_0);
4878
4879                 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
4880                     printk("  Warning: PCI I/O bit incorrectly set. "
4881                            "Ignoring it...\n");
4882                     pdev->resource[2].flags &= ~IORESOURCE_IO;
4883                 }
4884
4885                 /* Although we don't use this I/O region, we should
4886                    request it from the kernel anyway, to avoid problems
4887                    with other drivers accessing it. */
4888                 if (pci_request_regions(pdev, "Cyclades-Z") != 0) {
4889                         printk(KERN_ERR "cyclades: failed to reserve PCI resources\n");
4890                         continue;
4891                 }
4892         
4893                 if (mailbox == ZE_V1) {
4894                     cy_pci_addr2 = ioremap(cy_pci_phys2, CyPCI_Ze_win);
4895                     if (ZeIndex == NR_CARDS) {
4896                         printk("Cyclades-Ze/PCI found at 0x%lx ",
4897                                 (ulong)cy_pci_phys2);
4898                         printk("but no more cards can be used.\n");
4899                         printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4900                     } else {
4901                         Ze_phys0[ZeIndex] = cy_pci_phys0;
4902                         Ze_phys2[ZeIndex] = cy_pci_phys2;
4903                         Ze_addr0[ZeIndex] = cy_pci_addr0;
4904                         Ze_addr2[ZeIndex] = cy_pci_addr2;
4905                         Ze_irq[ZeIndex] = cy_pci_irq;
4906                         Ze_pdev[ZeIndex] = pdev;
4907                         ZeIndex++;
4908                     }
4909                     i--;
4910                     continue;
4911                 } else {
4912                     cy_pci_addr2 = ioremap(cy_pci_phys2, CyPCI_Zwin);
4913                 }
4914
4915 #ifdef CY_PCI_DEBUG
4916             printk("Cyclades-Z/PCI: relocate winaddr=0x%lx ctladdr=0x%lx\n",
4917                 (ulong)cy_pci_addr2, (ulong)cy_pci_addr0);
4918             if (mailbox == ZO_V1) {
4919                 cy_writel(&((struct RUNTIME_9060 *)
4920                           (cy_pci_addr0))->loc_addr_base, WIN_CREG);
4921                 PAUSE
4922                 printk("Cyclades-8Zo/PCI: FPGA id %lx, ver %lx\n",
4923                        (ulong)(0xff & cy_readl(&((struct CUSTOM_REG *)
4924                         (cy_pci_addr2))->fpga_id)),
4925                        (ulong)(0xff & cy_readl(&((struct CUSTOM_REG *)
4926                         (cy_pci_addr2))->fpga_version)));
4927                 cy_writel(&((struct RUNTIME_9060 *)
4928                           (cy_pci_addr0))->loc_addr_base, WIN_RAM);
4929             } else {
4930                 printk("Cyclades-Z/PCI: New Cyclades-Z board.  FPGA not loaded\n");
4931             }
4932 #endif
4933             /* The following clears the firmware id word.  This ensures
4934                that the driver will not attempt to talk to the board
4935                until it has been properly initialized.
4936              */
4937                 PAUSE
4938                 if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
4939                     cy_writel(cy_pci_addr2 + ID_ADDRESS, 0L);
4940
4941                 /* This must be a Cyclades-8Zo/PCI.  The extendable
4942                    version will have a different device_id and will
4943                    be allocated its maximum number of ports. */
4944                 cy_pci_nchan = 8;
4945
4946                 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
4947                         printk("Cyclades-8Zo/PCI found at 0x%lx ",
4948                             (ulong)cy_pci_phys2);
4949                         printk("but no channels are available.\n");
4950                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
4951                         return(i);
4952                 }
4953
4954                 /* fill the next cy_card structure available */
4955                 for (j = 0 ; j < NR_CARDS ; j++) {
4956                         if (cy_card[j].base_addr == 0)  break;
4957                 }
4958                 if (j == NR_CARDS) {    /* no more cy_cards available */
4959                     printk("Cyclades-8Zo/PCI found at 0x%lx ",
4960                         (ulong)cy_pci_phys2);
4961                     printk("but no more cards can be used.\n");
4962                     printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4963                     return(i);
4964                 }
4965
4966 #ifdef CONFIG_CYZ_INTR
4967                 /* allocate IRQ only if board has an IRQ */
4968                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) ) {
4969                     if(request_irq(cy_pci_irq, cyz_interrupt,
4970                         SA_SHIRQ, "Cyclades-Z", &cy_card[j]))
4971                     {
4972                         printk("Cyclom-8Zo/PCI found at 0x%lx ",
4973                             (ulong) cy_pci_phys2);
4974                         printk("but could not allocate IRQ%d.\n",
4975                             cy_pci_irq);
4976                         return(i);
4977                     }
4978                 }
4979 #endif /* CONFIG_CYZ_INTR */
4980
4981
4982                 /* set cy_card */
4983                 cy_card[j].base_phys = cy_pci_phys2;
4984                 cy_card[j].ctl_phys = cy_pci_phys0;
4985                 cy_card[j].base_addr = cy_pci_addr2;
4986                 cy_card[j].ctl_addr = cy_pci_addr0;
4987                 cy_card[j].irq = (int) cy_pci_irq;
4988                 cy_card[j].bus_index = 1;
4989                 cy_card[j].first_line = cy_next_channel;
4990                 cy_card[j].num_chips = -1;
4991                 cy_card[j].pdev = pdev;
4992
4993                 /* print message */
4994 #ifdef CONFIG_CYZ_INTR
4995                 /* don't report IRQ if board is no IRQ */
4996                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) )
4997                     printk("Cyclades-8Zo/PCI #%d: 0x%lx-0x%lx, IRQ%d, ",
4998                         j+1,(ulong)cy_pci_phys2,
4999                         (ulong)(cy_pci_phys2 + CyPCI_Zwin - 1),
5000                         (int)cy_pci_irq);
5001                 else
5002 #endif /* CONFIG_CYZ_INTR */
5003                     printk("Cyclades-8Zo/PCI #%d: 0x%lx-0x%lx, ",
5004                         j+1,(ulong)cy_pci_phys2,
5005                         (ulong)(cy_pci_phys2 + CyPCI_Zwin - 1));
5006
5007                 printk("%d channels starting from port %d.\n",
5008                     cy_pci_nchan,cy_next_channel);
5009                 cy_next_channel += cy_pci_nchan;
5010             }
5011         }
5012
5013         for (; ZeIndex != 0 && i < NR_CARDS; i++) {
5014             cy_pci_phys0 = Ze_phys0[0];
5015             cy_pci_phys2 = Ze_phys2[0];
5016             cy_pci_addr0 = Ze_addr0[0];
5017             cy_pci_addr2 = Ze_addr2[0];
5018             cy_pci_irq = Ze_irq[0];
5019             pdev = Ze_pdev[0];
5020             for (j = 0 ; j < ZeIndex-1 ; j++) {
5021                 Ze_phys0[j] = Ze_phys0[j+1];
5022                 Ze_phys2[j] = Ze_phys2[j+1];
5023                 Ze_addr0[j] = Ze_addr0[j+1];
5024                 Ze_addr2[j] = Ze_addr2[j+1];
5025                 Ze_irq[j] = Ze_irq[j+1];
5026                 Ze_pdev[j] = Ze_pdev[j+1];
5027             }
5028             ZeIndex--;
5029                 mailbox = (uclong)cy_readl(&((struct RUNTIME_9060 __iomem *) 
5030                                            cy_pci_addr0)->mail_box_0);
5031 #ifdef CY_PCI_DEBUG
5032             printk("Cyclades-Z/PCI: relocate winaddr=0x%lx ctladdr=0x%lx\n",
5033                 (ulong)cy_pci_addr2, (ulong)cy_pci_addr0);
5034             printk("Cyclades-Z/PCI: New Cyclades-Z board.  FPGA not loaded\n");
5035 #endif
5036                 PAUSE
5037                 /* This must be the new Cyclades-Ze/PCI. */
5038                 cy_pci_nchan = ZE_V1_NPORTS;
5039
5040                 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
5041                         printk("Cyclades-Ze/PCI found at 0x%lx ",
5042                             (ulong)cy_pci_phys2);
5043                         printk("but no channels are available.\n");
5044                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
5045                         return(i);
5046                 }
5047
5048                 /* fill the next cy_card structure available */
5049                 for (j = 0 ; j < NR_CARDS ; j++) {
5050                         if (cy_card[j].base_addr == 0)  break;
5051                 }
5052                 if (j == NR_CARDS) {    /* no more cy_cards available */
5053                     printk("Cyclades-Ze/PCI found at 0x%lx ",
5054                         (ulong)cy_pci_phys2);
5055                     printk("but no more cards can be used.\n");
5056                     printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
5057                     return(i);
5058                 }
5059
5060 #ifdef CONFIG_CYZ_INTR
5061                 /* allocate IRQ only if board has an IRQ */
5062                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) ) {
5063                     if(request_irq(cy_pci_irq, cyz_interrupt,
5064                         SA_SHIRQ, "Cyclades-Z", &cy_card[j]))
5065                     {
5066                         printk("Cyclom-Ze/PCI found at 0x%lx ",
5067                             (ulong) cy_pci_phys2);
5068                         printk("but could not allocate IRQ%d.\n",
5069                             cy_pci_irq);
5070                         return(i);
5071                     }
5072                 }
5073 #endif /* CONFIG_CYZ_INTR */
5074
5075                 /* set cy_card */
5076                 cy_card[j].base_phys = cy_pci_phys2;
5077                 cy_card[j].ctl_phys = cy_pci_phys0;
5078                 cy_card[j].base_addr = cy_pci_addr2;
5079                 cy_card[j].ctl_addr = cy_pci_addr0;
5080                 cy_card[j].irq = (int) cy_pci_irq;
5081                 cy_card[j].bus_index = 1;
5082                 cy_card[j].first_line = cy_next_channel;
5083                 cy_card[j].num_chips = -1;
5084                 cy_card[j].pdev = pdev;
5085
5086                 /* print message */
5087 #ifdef CONFIG_CYZ_INTR
5088                 /* don't report IRQ if board is no IRQ */
5089                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) )
5090                     printk("Cyclades-Ze/PCI #%d: 0x%lx-0x%lx, IRQ%d, ",
5091                         j+1,(ulong)cy_pci_phys2,
5092                         (ulong)(cy_pci_phys2 + CyPCI_Ze_win - 1),
5093                         (int)cy_pci_irq);
5094                 else
5095 #endif /* CONFIG_CYZ_INTR */
5096                     printk("Cyclades-Ze/PCI #%d: 0x%lx-0x%lx, ",
5097                         j+1,(ulong)cy_pci_phys2,
5098                         (ulong)(cy_pci_phys2 + CyPCI_Ze_win - 1));
5099
5100                 printk("%d channels starting from port %d.\n",
5101                     cy_pci_nchan,cy_next_channel);
5102                 cy_next_channel += cy_pci_nchan;
5103         }
5104         if (ZeIndex != 0) {
5105             printk("Cyclades-Ze/PCI found at 0x%x ",
5106                 (unsigned int) Ze_phys2[0]);
5107             printk("but no more cards can be used.\n");
5108             printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
5109         }
5110         return(i);
5111 #else
5112         return(0);
5113 #endif /* ifdef CONFIG_PCI */
5114 } /* cy_detect_pci */
5115
5116
5117 /*
5118  * This routine prints out the appropriate serial driver version number
5119  * and identifies which options were configured into this driver.
5120  */
5121 static inline void
5122 show_version(void)
5123 {
5124   char *rcsvers, *rcsdate, *tmp;
5125     rcsvers = strchr(rcsid, ' '); rcsvers++;
5126     tmp = strchr(rcsvers, ' '); *tmp++ = '\0';
5127     rcsdate = strchr(tmp, ' '); rcsdate++;
5128     tmp = strrchr(rcsdate, ' '); *tmp = '\0';
5129     printk("Cyclades driver %s %s\n",
5130         rcsvers, rcsdate);
5131     printk("        built %s %s\n",
5132         __DATE__, __TIME__);
5133 } /* show_version */
5134
5135 static int 
5136 cyclades_get_proc_info(char *buf, char **start, off_t offset, int length,
5137                        int *eof, void *data)
5138 {
5139     struct cyclades_port  *info;
5140     int i;
5141     int len=0;
5142     off_t begin=0;
5143     off_t pos=0;
5144     int size;
5145     __u32 cur_jifs = jiffies;
5146
5147     size = sprintf(buf, "Dev TimeOpen   BytesOut  IdleOut    BytesIn   IdleIn  Overruns  Ldisc\n");
5148
5149     pos += size;
5150     len += size;
5151
5152     /* Output one line for each known port */
5153     for (i = 0; i < NR_PORTS && cy_port[i].line >= 0; i++) {
5154         info = &cy_port[i];
5155
5156         if (info->count)
5157             size = sprintf(buf+len,
5158                         "%3d %8lu %10lu %8lu %10lu %8lu %9lu %6ld\n",
5159                         info->line,
5160                         JIFFIES_DIFF(info->idle_stats.in_use, cur_jifs) / HZ,
5161                         info->idle_stats.xmit_bytes,
5162                         JIFFIES_DIFF(info->idle_stats.xmit_idle, cur_jifs) / HZ,
5163                         info->idle_stats.recv_bytes,
5164                         JIFFIES_DIFF(info->idle_stats.recv_idle, cur_jifs) / HZ,
5165                         info->idle_stats.overruns,
5166                         (long) info->tty->ldisc.num);
5167         else
5168             size = sprintf(buf+len,
5169                         "%3d %8lu %10lu %8lu %10lu %8lu %9lu %6ld\n",
5170                         info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
5171         len += size;
5172         pos = begin + len;
5173
5174         if (pos < offset) {
5175             len   = 0;
5176             begin = pos;
5177         }
5178         if (pos > offset + length)
5179             goto done;
5180     }
5181     *eof = 1;
5182 done:
5183     *start = buf + (offset - begin);    /* Start of wanted data */
5184     len -= (offset - begin);            /* Start slop */
5185     if (len > length)
5186         len = length;                   /* Ending slop */
5187     if (len < 0)
5188         len = 0;
5189     return len;
5190 }
5191
5192 /* The serial driver boot-time initialization code!
5193     Hardware I/O ports are mapped to character special devices on a
5194     first found, first allocated manner.  That is, this code searches
5195     for Cyclom cards in the system.  As each is found, it is probed
5196     to discover how many chips (and thus how many ports) are present.
5197     These ports are mapped to the tty ports 32 and upward in monotonic
5198     fashion.  If an 8-port card is replaced with a 16-port card, the
5199     port mapping on a following card will shift.
5200
5201     This approach is different from what is used in the other serial
5202     device driver because the Cyclom is more properly a multiplexer,
5203     not just an aggregation of serial ports on one card.
5204
5205     If there are more cards with more ports than have been
5206     statically allocated above, a warning is printed and the
5207     extra ports are ignored.
5208  */
5209
5210 static struct tty_operations cy_ops = {
5211     .open = cy_open,
5212     .close = cy_close,
5213     .write = cy_write,
5214     .put_char = cy_put_char,
5215     .flush_chars = cy_flush_chars,
5216     .write_room = cy_write_room,
5217     .chars_in_buffer = cy_chars_in_buffer,
5218     .flush_buffer = cy_flush_buffer,
5219     .ioctl = cy_ioctl,
5220     .throttle = cy_throttle,
5221     .unthrottle = cy_unthrottle,
5222     .set_termios = cy_set_termios,
5223     .stop = cy_stop,
5224     .start = cy_start,
5225     .hangup = cy_hangup,
5226     .break_ctl = cy_break,
5227     .wait_until_sent = cy_wait_until_sent,
5228     .read_proc = cyclades_get_proc_info,
5229     .tiocmget = cy_tiocmget,
5230     .tiocmset = cy_tiocmset,
5231 };
5232
5233 static int __init
5234 cy_init(void)
5235 {
5236   struct cyclades_port  *info;
5237   struct cyclades_card *cinfo;
5238   int number_z_boards = 0;
5239   int board,port,i,index;
5240   unsigned long mailbox;
5241   unsigned short chip_number;
5242   int nports;
5243
5244     cy_serial_driver = alloc_tty_driver(NR_PORTS);
5245     if (!cy_serial_driver)
5246         return -ENOMEM;
5247     show_version();
5248
5249     /* Initialize the tty_driver structure */
5250     
5251     cy_serial_driver->owner = THIS_MODULE;
5252     cy_serial_driver->driver_name = "cyclades";
5253     cy_serial_driver->name = "ttyC";
5254     cy_serial_driver->devfs_name = "tts/C";
5255     cy_serial_driver->major = CYCLADES_MAJOR;
5256     cy_serial_driver->minor_start = 0;
5257     cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
5258     cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
5259     cy_serial_driver->init_termios = tty_std_termios;
5260     cy_serial_driver->init_termios.c_cflag =
5261             B9600 | CS8 | CREAD | HUPCL | CLOCAL;
5262     cy_serial_driver->flags = TTY_DRIVER_REAL_RAW;
5263     tty_set_operations(cy_serial_driver, &cy_ops);
5264
5265     if (tty_register_driver(cy_serial_driver))
5266             panic("Couldn't register Cyclades serial driver\n");
5267
5268     for (i = 0; i < NR_CARDS; i++) {
5269             /* base_addr=0 indicates board not found */
5270             cy_card[i].base_addr = NULL;
5271     }
5272
5273     /* the code below is responsible to find the boards. Each different
5274        type of board has its own detection routine. If a board is found,
5275        the next cy_card structure available is set by the detection
5276        routine. These functions are responsible for checking the
5277        availability of cy_card and cy_port data structures and updating
5278        the cy_next_channel. */
5279
5280     /* look for isa boards */
5281     cy_isa_nboard = cy_detect_isa();
5282
5283     /* look for pci boards */
5284     cy_pci_nboard = cy_detect_pci();
5285
5286     cy_nboard = cy_isa_nboard + cy_pci_nboard;
5287
5288     /* invalidate remaining cy_card structures */
5289     for (i = 0 ; i < NR_CARDS ; i++) {
5290         if (cy_card[i].base_addr == 0) {
5291                 cy_card[i].first_line = -1;
5292                 cy_card[i].ctl_addr = NULL;
5293                 cy_card[i].irq = 0;
5294                 cy_card[i].bus_index = 0;
5295                 cy_card[i].first_line = 0;
5296                 cy_card[i].num_chips = 0;
5297         }
5298     }
5299     /* invalidate remaining cy_port structures */
5300     for (i = cy_next_channel ; i < NR_PORTS ; i++) {
5301         cy_port[i].line = -1;
5302         cy_port[i].magic = -1;
5303     }
5304
5305     /* initialize per-port data structures for each valid board found */
5306     for (board = 0 ; board < cy_nboard ; board++) {
5307             cinfo = &cy_card[board];
5308             if (cinfo->num_chips == -1) { /* Cyclades-Z */
5309                 number_z_boards++;
5310                 mailbox = cy_readl(&((struct RUNTIME_9060 __iomem *)
5311                              cy_card[board].ctl_addr)->mail_box_0);
5312                 nports = (mailbox == ZE_V1) ? ZE_V1_NPORTS : 8;
5313                 cinfo->intr_enabled = 0;
5314                 cinfo->nports = 0; /* Will be correctly set later, after 
5315                                       Z FW is loaded */
5316                 spin_lock_init(&cinfo->card_lock);
5317                 for (port = cinfo->first_line ;
5318                      port < cinfo->first_line + nports;
5319                      port++)
5320                 {
5321                     info = &cy_port[port];
5322                     info->magic = CYCLADES_MAGIC;
5323                     info->type = PORT_STARTECH;
5324                     info->card = board;
5325                     info->line = port;
5326                     info->chip_rev = 0;
5327                     info->flags = STD_COM_FLAGS;
5328                     info->tty = NULL;
5329                     if (mailbox == ZO_V1)
5330                         info->xmit_fifo_size = CYZ_FIFO_SIZE;
5331                     else
5332                         info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
5333                     info->cor1 = 0;
5334                     info->cor2 = 0;
5335                     info->cor3 = 0;
5336                     info->cor4 = 0;
5337                     info->cor5 = 0;
5338                     info->tbpr = 0;
5339                     info->tco = 0;
5340                     info->rbpr = 0;
5341                     info->rco = 0;
5342                     info->custom_divisor = 0;
5343                     info->close_delay = 5*HZ/10;
5344                     info->closing_wait = CLOSING_WAIT_DELAY;
5345                     info->icount.cts = info->icount.dsr = 
5346                         info->icount.rng = info->icount.dcd = 0;
5347                     info->icount.rx = info->icount.tx = 0;
5348                     info->icount.frame = info->icount.parity = 0;
5349                     info->icount.overrun = info->icount.brk = 0;
5350                     info->x_char = 0;
5351                     info->event = 0;
5352                     info->count = 0;
5353                     info->blocked_open = 0;
5354                     info->default_threshold = 0;
5355                     info->default_timeout = 0;
5356                     INIT_WORK(&info->tqueue, do_softint, info);
5357                     init_waitqueue_head(&info->open_wait);
5358                     init_waitqueue_head(&info->close_wait);
5359                     init_waitqueue_head(&info->shutdown_wait);
5360                     init_waitqueue_head(&info->delta_msr_wait);
5361                     /* info->session */
5362                     /* info->pgrp */
5363                     info->read_status_mask = 0;
5364                     /* info->timeout */
5365                     /* Bentson's vars */
5366                     info->jiffies[0] = 0;
5367                     info->jiffies[1] = 0;
5368                     info->jiffies[2] = 0;
5369                     info->rflush_count = 0;
5370 #ifdef CONFIG_CYZ_INTR
5371                     init_timer(&cyz_rx_full_timer[port]);
5372                     cyz_rx_full_timer[port].function = NULL;
5373 #endif
5374                 }
5375                 continue;
5376             }else{ /* Cyclom-Y of some kind*/
5377                 index = cinfo->bus_index;
5378                 spin_lock_init(&cinfo->card_lock);
5379                 cinfo->nports = CyPORTS_PER_CHIP * cinfo->num_chips;
5380                 for (port = cinfo->first_line ;
5381                      port < cinfo->first_line + cinfo->nports ;
5382                      port++)
5383                 {
5384                     info = &cy_port[port];
5385                     info->magic = CYCLADES_MAGIC;
5386                     info->type = PORT_CIRRUS;
5387                     info->card = board;
5388                     info->line = port;
5389                     info->flags = STD_COM_FLAGS;
5390                     info->tty = NULL;
5391                     info->xmit_fifo_size = CyMAX_CHAR_FIFO;
5392                     info->cor1 = CyPARITY_NONE|Cy_1_STOP|Cy_8_BITS;
5393                     info->cor2 = CyETC;
5394                     info->cor3 = 0x08; /* _very_ small rcv threshold */
5395                     info->cor4 = 0;
5396                     info->cor5 = 0;
5397                     info->custom_divisor = 0;
5398                     info->close_delay = 5*HZ/10;
5399                     info->closing_wait = CLOSING_WAIT_DELAY;
5400                     info->icount.cts = info->icount.dsr = 
5401                         info->icount.rng = info->icount.dcd = 0;
5402                     info->icount.rx = info->icount.tx = 0;
5403                     info->icount.frame = info->icount.parity = 0;
5404                     info->icount.overrun = info->icount.brk = 0;
5405                     chip_number = (port - cinfo->first_line) / 4;
5406                     if ((info->chip_rev =
5407                          cy_readb(cinfo->base_addr +
5408                                   (cy_chip_offset[chip_number]<<index) +
5409                                   (CyGFRCR<<index))) >= CD1400_REV_J) {
5410                         /* It is a CD1400 rev. J or later */
5411                         info->tbpr = baud_bpr_60[13]; /* Tx BPR */
5412                         info->tco = baud_co_60[13]; /* Tx CO */
5413                         info->rbpr = baud_bpr_60[13]; /* Rx BPR */
5414                         info->rco = baud_co_60[13]; /* Rx CO */
5415                         info->rflow = 0;
5416                         info->rtsdtr_inv = 1;
5417                     } else {
5418                         info->tbpr = baud_bpr_25[13]; /* Tx BPR */
5419                         info->tco = baud_co_25[13]; /* Tx CO */
5420                         info->rbpr = baud_bpr_25[13]; /* Rx BPR */
5421                         info->rco = baud_co_25[13]; /* Rx CO */
5422                         info->rflow = 0;
5423                         info->rtsdtr_inv = 0;
5424                     }
5425                     info->x_char = 0;
5426                     info->event = 0;
5427                     info->count = 0;
5428                     info->blocked_open = 0;
5429                     info->default_threshold = 0;
5430                     info->default_timeout = 0;
5431                     INIT_WORK(&info->tqueue, do_softint, info);
5432                     init_waitqueue_head(&info->open_wait);
5433                     init_waitqueue_head(&info->close_wait);
5434                     init_waitqueue_head(&info->shutdown_wait);
5435                     init_waitqueue_head(&info->delta_msr_wait);
5436                     /* info->session */
5437                     /* info->pgrp */
5438                     info->read_status_mask =
5439                                   CyTIMEOUT| CySPECHAR| CyBREAK
5440                                   | CyPARITY| CyFRAME| CyOVERRUN;
5441                     /* info->timeout */
5442                 }
5443             }
5444     }
5445
5446 #ifndef CONFIG_CYZ_INTR
5447     if (number_z_boards && !cyz_timeron){
5448         cyz_timeron++;
5449         cyz_timerlist.expires = jiffies + 1;
5450         add_timer(&cyz_timerlist);
5451 #ifdef CY_PCI_DEBUG
5452         printk("Cyclades-Z polling initialized\n");
5453 #endif
5454     }
5455 #endif /* CONFIG_CYZ_INTR */
5456
5457     return 0;
5458     
5459 } /* cy_init */
5460
5461 static void __exit
5462 cy_cleanup_module(void)
5463 {
5464     int i, e1;
5465
5466 #ifndef CONFIG_CYZ_INTR
5467     if (cyz_timeron){
5468         cyz_timeron = 0;
5469         del_timer(&cyz_timerlist);
5470     }
5471 #endif /* CONFIG_CYZ_INTR */
5472
5473     if ((e1 = tty_unregister_driver(cy_serial_driver)))
5474             printk("cyc: failed to unregister Cyclades serial driver(%d)\n",
5475                 e1);
5476
5477     put_tty_driver(cy_serial_driver);
5478
5479     for (i = 0; i < NR_CARDS; i++) {
5480         if (cy_card[i].base_addr) {
5481             iounmap(cy_card[i].base_addr);
5482             if (cy_card[i].ctl_addr)
5483                 iounmap(cy_card[i].ctl_addr);
5484             if (cy_card[i].irq
5485 #ifndef CONFIG_CYZ_INTR
5486                 && cy_card[i].num_chips != -1 /* not a Z card */
5487 #endif /* CONFIG_CYZ_INTR */
5488             )
5489                 free_irq(cy_card[i].irq, &cy_card[i]);
5490 #ifdef CONFIG_PCI
5491                 if (cy_card[i].pdev)
5492                         pci_release_regions(cy_card[i].pdev);
5493 #endif
5494         }
5495     }
5496     if (tmp_buf) {
5497         free_page((unsigned long) tmp_buf);
5498         tmp_buf = NULL;
5499     }
5500 } /* cy_cleanup_module */
5501
5502 module_init(cy_init);
5503 module_exit(cy_cleanup_module);
5504
5505 MODULE_LICENSE("GPL");