upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.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 explictly
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/serial.h>
645 #include <linux/major.h>
646 #include <linux/string.h>
647 #include <linux/fcntl.h>
648 #include <linux/ptrace.h>
649 #include <linux/cyclades.h>
650 #include <linux/mm.h>
651 #include <linux/ioport.h>
652 #include <linux/init.h>
653 #include <linux/delay.h>
654 #include <linux/spinlock.h>
655 #include <linux/bitops.h>
656
657 #include <asm/system.h>
658 #include <asm/io.h>
659 #include <asm/irq.h>
660 #include <asm/uaccess.h>
661
662 #define CY_LOCK(info,flags)                                     \
663                 do {                                            \
664                 spin_lock_irqsave(&cy_card[info->card].card_lock, flags); \
665                 } while (0)
666                 
667 #define CY_UNLOCK(info,flags)                                   \
668                 do {                                            \
669                 spin_unlock_irqrestore(&cy_card[info->card].card_lock, flags); \
670                 } while (0)
671
672 #include <linux/types.h>
673 #include <linux/kernel.h>
674 #include <linux/pci.h>
675
676 #include <linux/stat.h>
677 #include <linux/proc_fs.h>
678
679 static void cy_throttle (struct tty_struct *tty);
680 static void cy_send_xchar (struct tty_struct *tty, char ch);
681
682 #define IS_CYC_Z(card) ((card).num_chips == -1)
683
684 #define Z_FPGA_CHECK(card) \
685     ((cy_readl(&((struct RUNTIME_9060 __iomem *) \
686                  ((card).ctl_addr))->init_ctrl) & (1<<17)) != 0)
687
688 #define ISZLOADED(card) (((ZO_V1==cy_readl(&((struct RUNTIME_9060 __iomem *) \
689                         ((card).ctl_addr))->mail_box_0)) || \
690                         Z_FPGA_CHECK(card)) && \
691                         (ZFIRM_ID==cy_readl(&((struct FIRM_ID __iomem *) \
692                         ((card).base_addr+ID_ADDRESS))->signature)))
693
694 #ifndef SERIAL_XMIT_SIZE
695 #define SERIAL_XMIT_SIZE        (min(PAGE_SIZE, 4096))
696 #endif
697 #define WAKEUP_CHARS            256
698
699 #define STD_COM_FLAGS (0)
700
701 #define JIFFIES_DIFF(n, j)      ((j) - (n))
702
703 static struct tty_driver *cy_serial_driver;
704
705 #ifdef CONFIG_ISA
706 /* This is the address lookup table. The driver will probe for
707    Cyclom-Y/ISA boards at all addresses in here. If you want the
708    driver to probe addresses at a different address, add it to
709    this table.  If the driver is probing some other board and
710    causing problems, remove the offending address from this table.
711    The cy_setup function extracts additional addresses from the
712    boot options line.  The form is "cyclades=address,address..."
713 */
714
715 static unsigned int cy_isa_addresses[] = {
716         0xD0000,
717         0xD2000,
718         0xD4000,
719         0xD6000,
720         0xD8000,
721         0xDA000,
722         0xDC000,
723         0xDE000,
724         0,0,0,0,0,0,0,0
725 };
726 #define NR_ISA_ADDRS (sizeof(cy_isa_addresses)/sizeof(unsigned char*))
727
728 #ifdef MODULE
729 static long maddr[NR_CARDS] = { 0, };
730 static int irq[NR_CARDS]  = { 0, };
731
732 module_param_array(maddr, long, NULL, 0);
733 module_param_array(irq, int, NULL, 0);
734 #endif
735
736 #endif /* CONFIG_ISA */
737
738 /* This is the per-card data structure containing address, irq, number of
739    channels, etc. This driver supports a maximum of NR_CARDS cards.
740 */
741 static struct cyclades_card cy_card[NR_CARDS];
742
743 /* This is the per-channel data structure containing pointers, flags
744  and variables for the port. This driver supports a maximum of NR_PORTS.
745 */
746 static struct cyclades_port cy_port[NR_PORTS];
747
748 static int cy_next_channel; /* next minor available */
749
750 /*
751  * tmp_buf is used as a temporary buffer by serial_write.  We need to
752  * lock it in case the copy_from_user blocks while swapping in a page,
753  * and some other program tries to do a serial write at the same time.
754  * Since the lock will only come under contention when the system is
755  * swapping and available memory is low, it makes sense to share one
756  * buffer across all the serial ports, since it significantly saves
757  * memory if large numbers of serial ports are open.  This buffer is
758  * allocated when the first cy_open occurs.
759  */
760 static unsigned char *tmp_buf;
761 DECLARE_MUTEX(tmp_buf_sem);
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 struct timer_list cyz_timerlist = TIMER_INITIALIZER(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
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->flip.count < TTY_FLIPBUF_SIZE){
1168                                 tty->flip.count++;
1169                                 if (data & info->read_status_mask){
1170                                     if(data & CyBREAK){
1171                                         *tty->flip.flag_buf_ptr++ =
1172                                                             TTY_BREAK;
1173                                         *tty->flip.char_buf_ptr++ =
1174                                           cy_readb(base_addr+(CyRDSR<<index));
1175                                         info->icount.rx++;
1176                                         if (info->flags & ASYNC_SAK){
1177                                             do_SAK(tty);
1178                                         }
1179                                     }else if(data & CyFRAME){
1180                                         *tty->flip.flag_buf_ptr++ =
1181                                                             TTY_FRAME;
1182                                         *tty->flip.char_buf_ptr++ =
1183                                           cy_readb(base_addr+(CyRDSR<<index));
1184                                         info->icount.rx++;
1185                                         info->idle_stats.frame_errs++;
1186                                     }else if(data & CyPARITY){
1187                                         *tty->flip.flag_buf_ptr++ =
1188                                                             TTY_PARITY;
1189                                         *tty->flip.char_buf_ptr++ =
1190                                           cy_readb(base_addr+(CyRDSR<<index));
1191                                         info->icount.rx++;
1192                                         info->idle_stats.parity_errs++;
1193                                     }else if(data & CyOVERRUN){
1194                                         *tty->flip.flag_buf_ptr++ =
1195                                                             TTY_OVERRUN;
1196                                         *tty->flip.char_buf_ptr++ = 0;
1197                                         info->icount.rx++;
1198                                         /* If the flip buffer itself is
1199                                            overflowing, we still lose
1200                                            the next incoming character.
1201                                          */
1202                                         if(tty->flip.count
1203                                                    < TTY_FLIPBUF_SIZE){
1204                                             tty->flip.count++;
1205                                             *tty->flip.flag_buf_ptr++ =
1206                                                              TTY_NORMAL;
1207                                            *tty->flip.char_buf_ptr++ =
1208                                             cy_readb(base_addr+(CyRDSR<<index));
1209                                             info->icount.rx++;
1210                                         }
1211                                         info->idle_stats.overruns++;
1212                                     /* These two conditions may imply */
1213                                     /* a normal read should be done. */
1214                                     /* }else if(data & CyTIMEOUT){ */
1215                                     /* }else if(data & CySPECHAR){ */
1216                                     }else{
1217                                         *tty->flip.flag_buf_ptr++ = 0;
1218                                         *tty->flip.char_buf_ptr++ = 0;
1219                                         info->icount.rx++;
1220                                     }
1221                                 }else{
1222                                     *tty->flip.flag_buf_ptr++ = 0;
1223                                     *tty->flip.char_buf_ptr++ = 0;
1224                                     info->icount.rx++;
1225                                 }
1226                             }else{
1227                                 /* there was a software buffer
1228                                    overrun and nothing could be
1229                                    done about it!!! */
1230                                 info->icount.buf_overrun++;
1231                                 info->idle_stats.overruns++;
1232                             }
1233                         } else { /* normal character reception */
1234                             /* load # chars available from the chip */
1235                             char_count = cy_readb(base_addr+(CyRDCR<<index));
1236
1237 #ifdef CY_ENABLE_MONITORING
1238                             ++info->mon.int_count;
1239                             info->mon.char_count += char_count;
1240                             if (char_count > info->mon.char_max)
1241                                info->mon.char_max = char_count;
1242                             info->mon.char_last = char_count;
1243 #endif
1244                             while(char_count--){
1245                                 if (tty->flip.count >= TTY_FLIPBUF_SIZE){
1246                                         break;
1247                                 }
1248                                 tty->flip.count++;
1249                                 data = cy_readb(base_addr+(CyRDSR<<index));
1250                                 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
1251                                 *tty->flip.char_buf_ptr++ = data;
1252                                 info->idle_stats.recv_bytes++;
1253                                 info->icount.rx++;
1254 #ifdef CY_16Y_HACK
1255                                 udelay(10L);
1256 #endif
1257                             }
1258                              info->idle_stats.recv_idle = jiffies;
1259                         }
1260                         schedule_delayed_work(&tty->flip.work, 1);
1261                     }
1262                     /* end of service */
1263                     cy_writeb(base_addr+(CyRIR<<index), (save_xir & 0x3f));
1264                     cy_writeb(base_addr+(CyCAR<<index), (save_car));
1265                     spin_unlock(&cinfo->card_lock);
1266                 }
1267
1268
1269                 if (status & CySRTransmit) { /* transmission interrupt */
1270                     /* Since we only get here when the transmit buffer
1271                        is empty, we know we can always stuff a dozen
1272                        characters. */
1273 #ifdef CY_DEBUG_INTERRUPTS
1274                     printk("cyy_interrupt: xmit intr, chip %d\n\r", chip);
1275 #endif
1276
1277                     /* determine the channel & change to that context */
1278                     spin_lock(&cinfo->card_lock);
1279                     save_xir = (u_char) cy_readb(base_addr+(CyTIR<<index));
1280                     channel = (u_short ) (save_xir & CyIRChannel);
1281                     i = channel + chip * 4 + cinfo->first_line;
1282                     save_car = cy_readb(base_addr+(CyCAR<<index));
1283                     cy_writeb(base_addr+(CyCAR<<index), save_xir);
1284
1285                     /* validate the port# (as configured and open) */
1286                     if( (i < 0) || (NR_PORTS <= i) ){
1287                         cy_writeb(base_addr+(CySRER<<index),
1288                              cy_readb(base_addr+(CySRER<<index)) & ~CyTxRdy);
1289                         goto txend;
1290                     }
1291                     info = &cy_port[i];
1292                     info->last_active = jiffies;
1293                     if(info->tty == 0){
1294                         cy_writeb(base_addr+(CySRER<<index),
1295                              cy_readb(base_addr+(CySRER<<index)) & ~CyTxRdy);
1296                         goto txdone;
1297                     }
1298
1299                     /* load the on-chip space for outbound data */
1300                     char_count = info->xmit_fifo_size;
1301
1302                     if(info->x_char) { /* send special char */
1303                         outch = info->x_char;
1304                         cy_writeb(base_addr+(CyTDR<<index), outch);
1305                         char_count--;
1306                         info->icount.tx++;
1307                         info->x_char = 0;
1308                     }
1309
1310                     if (info->breakon || info->breakoff) {
1311                         if (info->breakon) {
1312                             cy_writeb(base_addr + (CyTDR<<index), 0); 
1313                             cy_writeb(base_addr + (CyTDR<<index), 0x81);
1314                             info->breakon = 0;
1315                             char_count -= 2;
1316                         }
1317                         if (info->breakoff) {
1318                             cy_writeb(base_addr + (CyTDR<<index), 0); 
1319                             cy_writeb(base_addr + (CyTDR<<index), 0x83);
1320                             info->breakoff = 0;
1321                             char_count -= 2;
1322                         }
1323                     }
1324
1325                     while (char_count-- > 0){
1326                         if (!info->xmit_cnt){
1327                             if (cy_readb(base_addr+(CySRER<<index))&CyTxMpty) {
1328                                 cy_writeb(base_addr+(CySRER<<index),
1329                                           cy_readb(base_addr+(CySRER<<index)) &
1330                                           ~CyTxMpty);
1331                             } else {
1332                                 cy_writeb(base_addr+(CySRER<<index),
1333                                           ((cy_readb(base_addr+(CySRER<<index))
1334                                             & ~CyTxRdy)
1335                                            | CyTxMpty));
1336                             }
1337                             goto txdone;
1338                         }
1339                         if (info->xmit_buf == 0){
1340                             cy_writeb(base_addr+(CySRER<<index),
1341                                 cy_readb(base_addr+(CySRER<<index)) & 
1342                                         ~CyTxRdy);
1343                             goto txdone;
1344                         }
1345                         if (info->tty->stopped || info->tty->hw_stopped){
1346                             cy_writeb(base_addr+(CySRER<<index),
1347                                 cy_readb(base_addr+(CySRER<<index)) & 
1348                                         ~CyTxRdy);
1349                             goto txdone;
1350                         }
1351                         /* Because the Embedded Transmit Commands have
1352                            been enabled, we must check to see if the
1353                            escape character, NULL, is being sent.  If it
1354                            is, we must ensure that there is room for it
1355                            to be doubled in the output stream.  Therefore
1356                            we no longer advance the pointer when the
1357                            character is fetched, but rather wait until
1358                            after the check for a NULL output character.
1359                            This is necessary because there may not be
1360                            room for the two chars needed to send a NULL.)
1361                          */
1362                         outch = info->xmit_buf[info->xmit_tail];
1363                         if( outch ){
1364                             info->xmit_cnt--;
1365                             info->xmit_tail = (info->xmit_tail + 1)
1366                                                       & (SERIAL_XMIT_SIZE - 1);
1367                             cy_writeb(base_addr+(CyTDR<<index), outch);
1368                             info->icount.tx++;
1369                         }else{
1370                             if(char_count > 1){
1371                                 info->xmit_cnt--;
1372                                 info->xmit_tail = (info->xmit_tail + 1)
1373                                                       & (SERIAL_XMIT_SIZE - 1);
1374                                 cy_writeb(base_addr+(CyTDR<<index), 
1375                                           outch);
1376                                 cy_writeb(base_addr+(CyTDR<<index), 0);
1377                                 info->icount.tx++;
1378                                 char_count--;
1379                             }else{
1380                             }
1381                         }
1382                     }
1383
1384         txdone:
1385                     if (info->xmit_cnt < WAKEUP_CHARS) {
1386                         cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
1387                     }
1388         txend:
1389                     /* end of service */
1390                     cy_writeb(base_addr+(CyTIR<<index), 
1391                               (save_xir & 0x3f));
1392                     cy_writeb(base_addr+(CyCAR<<index), (save_car));
1393                     spin_unlock(&cinfo->card_lock);
1394                 }
1395
1396                 if (status & CySRModem) {        /* modem interrupt */
1397
1398                     /* determine the channel & change to that context */
1399                     spin_lock(&cinfo->card_lock);
1400                     save_xir = (u_char) cy_readb(base_addr+(CyMIR<<index));
1401                     channel = (u_short ) (save_xir & CyIRChannel);
1402                     info = &cy_port[channel + chip * 4
1403                                            + cinfo->first_line];
1404                     info->last_active = jiffies;
1405                     save_car = cy_readb(base_addr+(CyCAR<<index));
1406                     cy_writeb(base_addr+(CyCAR<<index), save_xir);
1407
1408                     mdm_change = cy_readb(base_addr+(CyMISR<<index));
1409                     mdm_status = cy_readb(base_addr+(CyMSVR1<<index));
1410
1411                     if(info->tty == 0){/* no place for data, ignore it*/
1412                         ;
1413                     }else{
1414                         if (mdm_change & CyANY_DELTA) {
1415                             /* For statistics only */
1416                             if (mdm_change & CyDCD)     info->icount.dcd++;
1417                             if (mdm_change & CyCTS)     info->icount.cts++;
1418                             if (mdm_change & CyDSR)     info->icount.dsr++;
1419                             if (mdm_change & CyRI)      info->icount.rng++;
1420
1421                             cy_sched_event(info, Cy_EVENT_DELTA_WAKEUP);
1422                         }
1423
1424                         if((mdm_change & CyDCD)
1425                         && (info->flags & ASYNC_CHECK_CD)){
1426                             if(mdm_status & CyDCD){
1427                                 cy_sched_event(info,
1428                                     Cy_EVENT_OPEN_WAKEUP);
1429                             }else{
1430                                 cy_sched_event(info,
1431                                     Cy_EVENT_HANGUP);
1432                             }
1433                         }
1434                         if((mdm_change & CyCTS)
1435                         && (info->flags & ASYNC_CTS_FLOW)){
1436                             if(info->tty->hw_stopped){
1437                                 if(mdm_status & CyCTS){
1438                                     /* cy_start isn't used
1439                                          because... !!! */
1440                                     info->tty->hw_stopped = 0;
1441                                   cy_writeb(base_addr+(CySRER<<index),
1442                                        cy_readb(base_addr+(CySRER<<index)) | 
1443                                        CyTxRdy);
1444                                     cy_sched_event(info,
1445                                         Cy_EVENT_WRITE_WAKEUP);
1446                                 }
1447                             }else{
1448                                 if(!(mdm_status & CyCTS)){
1449                                     /* cy_stop isn't used
1450                                          because ... !!! */
1451                                     info->tty->hw_stopped = 1;
1452                                   cy_writeb(base_addr+(CySRER<<index),
1453                                        cy_readb(base_addr+(CySRER<<index)) & 
1454                                        ~CyTxRdy);
1455                                 }
1456                             }
1457                         }
1458                         if(mdm_change & CyDSR){
1459                         }
1460                         if(mdm_change & CyRI){
1461                         }
1462                     }
1463                     /* end of service */
1464                     cy_writeb(base_addr+(CyMIR<<index), 
1465                               (save_xir & 0x3f));
1466                     cy_writeb(base_addr+(CyCAR<<index), save_car);
1467                     spin_unlock(&cinfo->card_lock);
1468                 }
1469             }          /* end while status != 0 */
1470         }            /* end loop for chips... */
1471     } while(had_work);
1472
1473    /* clear interrupts */
1474    spin_lock(&cinfo->card_lock);
1475    cy_writeb(card_base_addr + (Cy_ClrIntr<<index), 0);
1476                                 /* Cy_ClrIntr is 0x1800 */
1477    spin_unlock(&cinfo->card_lock);
1478    return IRQ_HANDLED;
1479 } /* cyy_interrupt */
1480
1481 /***********************************************************/
1482 /********* End of block of Cyclom-Y specific code **********/
1483 /******** Start of block of Cyclades-Z specific code *********/
1484 /***********************************************************/
1485
1486 static int
1487 cyz_fetch_msg( struct cyclades_card *cinfo,
1488             uclong *channel, ucchar *cmd, uclong *param)
1489 {
1490   struct FIRM_ID __iomem *firm_id;
1491   struct ZFW_CTRL __iomem *zfw_ctrl;
1492   struct BOARD_CTRL __iomem *board_ctrl;
1493   unsigned long loc_doorbell;
1494
1495     firm_id = cinfo->base_addr + ID_ADDRESS;
1496     if (!ISZLOADED(*cinfo)){
1497         return (-1);
1498     }
1499     zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1500     board_ctrl = &zfw_ctrl->board_ctrl;
1501
1502     loc_doorbell = cy_readl(&((struct RUNTIME_9060 __iomem *)
1503                      (cinfo->ctl_addr))->loc_doorbell);
1504     if (loc_doorbell){
1505         *cmd = (char)(0xff & loc_doorbell);
1506         *channel = cy_readl(&board_ctrl->fwcmd_channel);
1507         *param = (uclong)cy_readl(&board_ctrl->fwcmd_param);
1508         cy_writel(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->loc_doorbell, 
1509                  0xffffffff);
1510         return 1;
1511     }
1512     return 0;
1513 } /* cyz_fetch_msg */
1514
1515 static int
1516 cyz_issue_cmd( struct cyclades_card *cinfo,
1517             uclong channel, ucchar cmd, uclong param)
1518 {
1519   struct FIRM_ID __iomem *firm_id;
1520   struct ZFW_CTRL __iomem *zfw_ctrl;
1521   struct BOARD_CTRL __iomem *board_ctrl;
1522   unsigned long __iomem *pci_doorbell;
1523   int index;
1524
1525     firm_id = cinfo->base_addr + ID_ADDRESS;
1526     if (!ISZLOADED(*cinfo)){
1527         return (-1);
1528     }
1529     zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1530     board_ctrl = &zfw_ctrl->board_ctrl;
1531
1532     index = 0;
1533     pci_doorbell = &((struct RUNTIME_9060 __iomem *) (cinfo->ctl_addr))->pci_doorbell;
1534     while( (cy_readl(pci_doorbell) & 0xff) != 0){
1535         if (index++ == 1000){
1536             return((int)(cy_readl(pci_doorbell) & 0xff));
1537         }
1538         udelay(50L);
1539     }
1540     cy_writel(&board_ctrl->hcmd_channel, channel);
1541     cy_writel(&board_ctrl->hcmd_param , param);
1542     cy_writel(pci_doorbell, (long)cmd);
1543
1544     return(0);
1545 } /* cyz_issue_cmd */
1546
1547 static void
1548 cyz_handle_rx(struct cyclades_port *info,
1549               volatile struct CH_CTRL __iomem *ch_ctrl,
1550               volatile struct BUF_CTRL __iomem *buf_ctrl)
1551 {
1552   struct cyclades_card *cinfo = &cy_card[info->card];
1553   struct tty_struct *tty = info->tty;
1554   volatile int char_count;
1555 #ifdef BLOCKMOVE
1556   int small_count;
1557 #else
1558   char data;
1559 #endif
1560   volatile uclong rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
1561
1562     rx_get = new_rx_get = cy_readl(&buf_ctrl->rx_get);
1563     rx_put = cy_readl(&buf_ctrl->rx_put);
1564     rx_bufsize = cy_readl(&buf_ctrl->rx_bufsize);
1565     rx_bufaddr = cy_readl(&buf_ctrl->rx_bufaddr);
1566     if (rx_put >= rx_get)
1567         char_count = rx_put - rx_get;
1568     else
1569         char_count = rx_put - rx_get + rx_bufsize;
1570
1571     if ( char_count ) {
1572         info->last_active = jiffies;
1573         info->jiffies[1] = jiffies;
1574
1575 #ifdef CY_ENABLE_MONITORING
1576         info->mon.int_count++;
1577         info->mon.char_count += char_count;
1578         if (char_count > info->mon.char_max)
1579             info->mon.char_max = char_count;
1580         info->mon.char_last = char_count;
1581 #endif
1582         if(tty == 0){
1583             /* flush received characters */
1584             new_rx_get = (new_rx_get + char_count) & (rx_bufsize - 1);
1585             info->rflush_count++;
1586         }else{
1587 #ifdef BLOCKMOVE
1588             /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
1589                for performance, but because of buffer boundaries, there
1590                may be several steps to the operation */
1591             while(0 < (small_count = 
1592                        min_t(unsigned int, (rx_bufsize - new_rx_get),
1593                        min_t(unsigned int, (TTY_FLIPBUF_SIZE - tty->flip.count), char_count))
1594                  )) {
1595                 memcpy_fromio(tty->flip.char_buf_ptr,
1596                               (char *)(cinfo->base_addr
1597                                        + rx_bufaddr + new_rx_get),
1598                               small_count);
1599
1600                 tty->flip.char_buf_ptr += small_count;
1601                 memset(tty->flip.flag_buf_ptr, TTY_NORMAL, small_count);
1602                 tty->flip.flag_buf_ptr += small_count;
1603                 new_rx_get = (new_rx_get + small_count) & (rx_bufsize - 1);
1604                 char_count -= small_count;
1605                 info->icount.rx += small_count;
1606                 info->idle_stats.recv_bytes += small_count;
1607                 tty->flip.count += small_count;
1608             }
1609 #else
1610             while(char_count--){
1611                 if (tty->flip.count >= N_TTY_BUF_SIZE - tty->read_cnt)
1612                     break;
1613
1614                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1615                     break;
1616
1617                 data = cy_readb(cinfo->base_addr + rx_bufaddr + new_rx_get);
1618                 new_rx_get = (new_rx_get + 1) & (rx_bufsize - 1);
1619                 tty->flip.count++;
1620                 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
1621                 *tty->flip.char_buf_ptr++ = data;
1622                 info->idle_stats.recv_bytes++;
1623                 info->icount.rx++;
1624             }
1625 #endif
1626 #ifdef CONFIG_CYZ_INTR
1627             /* Recalculate the number of chars in the RX buffer and issue
1628                a cmd in case it's higher than the RX high water mark */
1629             rx_put = cy_readl(&buf_ctrl->rx_put);
1630             if (rx_put >= rx_get)
1631                 char_count = rx_put - rx_get;
1632             else
1633                 char_count = rx_put - rx_get + rx_bufsize;
1634             if(char_count >= cy_readl(&buf_ctrl->rx_threshold)) {
1635                 cy_sched_event(info, Cy_EVENT_Z_RX_FULL);
1636             }
1637 #endif
1638             info->idle_stats.recv_idle = jiffies;
1639             schedule_delayed_work(&tty->flip.work, 1);
1640         }
1641         /* Update rx_get */
1642         cy_writel(&buf_ctrl->rx_get, new_rx_get);
1643     }
1644 }
1645
1646 static void
1647 cyz_handle_tx(struct cyclades_port *info,
1648               volatile struct CH_CTRL __iomem *ch_ctrl,
1649               volatile struct BUF_CTRL __iomem *buf_ctrl)
1650 {
1651   struct cyclades_card *cinfo = &cy_card[info->card];
1652   struct tty_struct *tty = info->tty;
1653   char data;
1654   volatile int char_count;
1655 #ifdef BLOCKMOVE
1656   int small_count;
1657 #endif
1658   volatile uclong tx_put, tx_get, tx_bufsize, tx_bufaddr;
1659
1660     if (info->xmit_cnt <= 0)    /* Nothing to transmit */
1661         return;
1662
1663     tx_get = cy_readl(&buf_ctrl->tx_get);
1664     tx_put = cy_readl(&buf_ctrl->tx_put);
1665     tx_bufsize = cy_readl(&buf_ctrl->tx_bufsize);
1666     tx_bufaddr = cy_readl(&buf_ctrl->tx_bufaddr);
1667     if (tx_put >= tx_get)
1668         char_count = tx_get - tx_put - 1 + tx_bufsize;
1669     else
1670         char_count = tx_get - tx_put - 1;
1671
1672     if ( char_count ) {
1673
1674         if( tty == 0 ){
1675             goto ztxdone;
1676         }
1677
1678         if(info->x_char) { /* send special char */
1679             data = info->x_char;
1680
1681             cy_writeb((cinfo->base_addr + tx_bufaddr + tx_put), data);
1682             tx_put = (tx_put + 1) & (tx_bufsize - 1);
1683             info->x_char = 0;
1684             char_count--;
1685             info->icount.tx++;
1686             info->last_active = jiffies;
1687             info->jiffies[2] = jiffies;
1688         }
1689 #ifdef BLOCKMOVE
1690         while(0 < (small_count = 
1691                    min_t(unsigned int, (tx_bufsize - tx_put),
1692                        min_t(unsigned int, (SERIAL_XMIT_SIZE - info->xmit_tail),
1693                            min_t(unsigned int, info->xmit_cnt, char_count))))) {
1694
1695             memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr + tx_put),
1696                         &info->xmit_buf[info->xmit_tail],
1697                         small_count);
1698
1699             tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1700             char_count -= small_count;
1701             info->icount.tx += small_count;
1702             info->xmit_cnt -= small_count;
1703             info->xmit_tail = 
1704                 (info->xmit_tail + small_count) & (SERIAL_XMIT_SIZE - 1);
1705             info->last_active = jiffies;
1706             info->jiffies[2] = jiffies;
1707         }
1708 #else
1709         while (info->xmit_cnt && char_count){
1710             data = info->xmit_buf[info->xmit_tail];
1711             info->xmit_cnt--;
1712             info->xmit_tail = (info->xmit_tail + 1) & (SERIAL_XMIT_SIZE - 1);
1713
1714             cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1715             tx_put = (tx_put + 1) & (tx_bufsize - 1);
1716             char_count--;
1717             info->icount.tx++;
1718             info->last_active = jiffies;
1719             info->jiffies[2] = jiffies;
1720         }
1721 #endif
1722     ztxdone:
1723         if (info->xmit_cnt < WAKEUP_CHARS) {
1724             cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
1725         }
1726         /* Update tx_put */
1727         cy_writel(&buf_ctrl->tx_put, tx_put);
1728     }
1729 }
1730
1731 static void
1732 cyz_handle_cmd(struct cyclades_card *cinfo)
1733 {
1734   struct tty_struct *tty;
1735   struct cyclades_port *info;
1736   static volatile struct FIRM_ID __iomem *firm_id;
1737   static volatile struct ZFW_CTRL __iomem *zfw_ctrl;
1738   static volatile struct BOARD_CTRL __iomem *board_ctrl;
1739   static volatile struct CH_CTRL __iomem *ch_ctrl;
1740   static volatile struct BUF_CTRL __iomem *buf_ctrl;
1741   uclong channel;
1742   ucchar cmd;
1743   uclong param;
1744   uclong hw_ver, fw_ver;
1745   int special_count;
1746   int delta_count;
1747
1748     firm_id = cinfo->base_addr + ID_ADDRESS;
1749     zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1750     board_ctrl = &zfw_ctrl->board_ctrl;
1751     fw_ver = cy_readl(&board_ctrl->fw_version);
1752     hw_ver = cy_readl(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->mail_box_0);
1753
1754
1755     while(cyz_fetch_msg(cinfo, &channel, &cmd, &param) == 1) {
1756         special_count = 0;
1757         delta_count = 0;
1758         info = &cy_port[channel + cinfo->first_line];
1759         if((tty = info->tty) == 0) {
1760             continue;
1761         }
1762         ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
1763         buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
1764
1765         switch(cmd) {
1766             case C_CM_PR_ERROR:
1767                 tty->flip.count++;
1768                 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1769                 *tty->flip.char_buf_ptr++ = 0;
1770                 info->icount.rx++;
1771                 special_count++;
1772                 break;
1773             case C_CM_FR_ERROR:
1774                 tty->flip.count++;
1775                 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1776                 *tty->flip.char_buf_ptr++ = 0;
1777                 info->icount.rx++;
1778                 special_count++;
1779                 break;
1780             case C_CM_RXBRK:
1781                 tty->flip.count++;
1782                 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1783                 *tty->flip.char_buf_ptr++ = 0;
1784                 info->icount.rx++;
1785                 special_count++;
1786                 break;
1787             case C_CM_MDCD:
1788                 info->icount.dcd++;
1789                 delta_count++;
1790                 if (info->flags & ASYNC_CHECK_CD){
1791                     if ((fw_ver > 241 ? 
1792                           ((u_long)param) : 
1793                           cy_readl(&ch_ctrl->rs_status)) & C_RS_DCD) {
1794                         cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
1795                     }else{
1796                         cy_sched_event(info, Cy_EVENT_HANGUP);
1797                     }
1798                 }
1799                 break;
1800             case C_CM_MCTS:
1801                 info->icount.cts++;
1802                 delta_count++;
1803                 break;
1804             case C_CM_MRI:
1805                 info->icount.rng++;
1806                 delta_count++;
1807                 break;
1808             case C_CM_MDSR:
1809                 info->icount.dsr++;
1810                 delta_count++;
1811                 break;
1812 #ifdef Z_WAKE
1813             case C_CM_IOCTLW:
1814                 cy_sched_event(info, Cy_EVENT_SHUTDOWN_WAKEUP);
1815                 break;
1816 #endif
1817 #ifdef CONFIG_CYZ_INTR
1818             case C_CM_RXHIWM:
1819             case C_CM_RXNNDT:
1820             case C_CM_INTBACK2:
1821                 /* Reception Interrupt */
1822 #ifdef CY_DEBUG_INTERRUPTS
1823                 printk("cyz_interrupt: rcvd intr, card %d, port %ld\n\r", 
1824                         info->card, channel);
1825 #endif
1826                 cyz_handle_rx(info, ch_ctrl, buf_ctrl);
1827                 break;
1828             case C_CM_TXBEMPTY:
1829             case C_CM_TXLOWWM:
1830             case C_CM_INTBACK:
1831                 /* Transmission Interrupt */
1832 #ifdef CY_DEBUG_INTERRUPTS
1833                 printk("cyz_interrupt: xmit intr, card %d, port %ld\n\r", 
1834                         info->card, channel);
1835 #endif
1836                 cyz_handle_tx(info, ch_ctrl, buf_ctrl);
1837                 break;
1838 #endif /* CONFIG_CYZ_INTR */
1839             case C_CM_FATAL:
1840                 /* should do something with this !!! */
1841                 break;
1842             default:
1843                 break;
1844         }
1845         if(delta_count)
1846             cy_sched_event(info, Cy_EVENT_DELTA_WAKEUP);
1847         if(special_count)
1848             schedule_delayed_work(&tty->flip.work, 1);
1849     }
1850 }
1851
1852 #ifdef CONFIG_CYZ_INTR
1853 static irqreturn_t
1854 cyz_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1855 {
1856   struct cyclades_card *cinfo;
1857
1858     if((cinfo = (struct cyclades_card *)dev_id) == 0){
1859 #ifdef CY_DEBUG_INTERRUPTS
1860         printk("cyz_interrupt: spurious interrupt %d\n\r", irq);
1861 #endif
1862         return IRQ_NONE; /* spurious interrupt */
1863     }
1864
1865     if (!ISZLOADED(*cinfo)) {
1866 #ifdef CY_DEBUG_INTERRUPTS
1867         printk("cyz_interrupt: board not yet loaded (IRQ%d).\n\r", irq);
1868 #endif
1869         return IRQ_NONE;
1870     }
1871
1872     /* Handle the interrupts */
1873     cyz_handle_cmd(cinfo);
1874
1875     return IRQ_HANDLED;
1876 } /* cyz_interrupt */
1877
1878 static void
1879 cyz_rx_restart(unsigned long arg)
1880 {
1881     struct cyclades_port *info = (struct cyclades_port *)arg;
1882     int retval;
1883     int card = info->card;
1884     uclong channel = (info->line) - (cy_card[card].first_line);
1885     unsigned long flags;
1886
1887     CY_LOCK(info, flags);
1888     retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_INTBACK2, 0L);
1889     if (retval != 0){
1890         printk("cyc:cyz_rx_restart retval on ttyC%d was %x\n", 
1891                info->line, retval);
1892     }
1893     cyz_rx_full_timer[info->line].function = NULL;
1894     CY_UNLOCK(info, flags);
1895 }
1896
1897 #else /* CONFIG_CYZ_INTR */
1898
1899 static void
1900 cyz_poll(unsigned long arg)
1901 {
1902   struct cyclades_card *cinfo;
1903   struct cyclades_port *info;
1904   struct tty_struct *tty;
1905   static volatile struct FIRM_ID *firm_id;
1906   static volatile struct ZFW_CTRL *zfw_ctrl;
1907   static volatile struct BOARD_CTRL *board_ctrl;
1908   static volatile struct CH_CTRL *ch_ctrl;
1909   static volatile struct BUF_CTRL *buf_ctrl;
1910   int card, port;
1911
1912     cyz_timerlist.expires = jiffies + (HZ);
1913     for (card = 0 ; card < NR_CARDS ; card++){
1914         cinfo = &cy_card[card];
1915
1916         if (!IS_CYC_Z(*cinfo)) continue;
1917         if (!ISZLOADED(*cinfo)) continue;
1918
1919         firm_id = cinfo->base_addr + ID_ADDRESS;
1920         zfw_ctrl = cinfo->base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
1921         board_ctrl = &(zfw_ctrl->board_ctrl);
1922
1923         /* Skip first polling cycle to avoid racing conditions with the FW */
1924         if (!cinfo->intr_enabled) {
1925             cinfo->nports = (int) cy_readl(&board_ctrl->n_channel);
1926             cinfo->intr_enabled = 1;
1927             continue;
1928         }
1929
1930         cyz_handle_cmd(cinfo);
1931
1932         for (port = 0 ; port < cinfo->nports ; port++) {
1933             info = &cy_port[ port + cinfo->first_line ];
1934             tty = info->tty;
1935             ch_ctrl = &(zfw_ctrl->ch_ctrl[port]);
1936             buf_ctrl = &(zfw_ctrl->buf_ctrl[port]);
1937
1938             if (!info->throttle)
1939                 cyz_handle_rx(info, ch_ctrl, buf_ctrl);
1940             cyz_handle_tx(info, ch_ctrl, buf_ctrl);
1941         }
1942         /* poll every 'cyz_polling_cycle' period */
1943         cyz_timerlist.expires = jiffies + cyz_polling_cycle;
1944     }
1945     add_timer(&cyz_timerlist);
1946
1947     return;
1948 } /* cyz_poll */
1949
1950 #endif /* CONFIG_CYZ_INTR */
1951
1952 /********** End of block of Cyclades-Z specific code *********/
1953 /***********************************************************/
1954
1955
1956 /* This is called whenever a port becomes active;
1957    interrupts are enabled and DTR & RTS are turned on.
1958  */
1959 static int
1960 startup(struct cyclades_port * info)
1961 {
1962   unsigned long flags;
1963   int retval = 0;
1964   void __iomem *base_addr;
1965   int card,chip,channel,index;
1966   unsigned long page;
1967
1968     card = info->card;
1969     channel = (info->line) - (cy_card[card].first_line);
1970
1971     page = get_zeroed_page(GFP_KERNEL);
1972     if (!page)
1973         return -ENOMEM;
1974
1975     CY_LOCK(info, flags);
1976
1977     if (info->flags & ASYNC_INITIALIZED){
1978         free_page(page);
1979         goto errout;
1980     }
1981
1982     if (!info->type){
1983         if (info->tty){
1984             set_bit(TTY_IO_ERROR, &info->tty->flags);
1985         }
1986         free_page(page);
1987         goto errout;
1988     }
1989
1990     if (info->xmit_buf)
1991         free_page(page);
1992     else
1993         info->xmit_buf = (unsigned char *) page;
1994
1995     CY_UNLOCK(info, flags);
1996
1997     set_line_char(info);
1998
1999     if (!IS_CYC_Z(cy_card[card])) {
2000         chip = channel>>2;
2001         channel &= 0x03;
2002         index = cy_card[card].bus_index;
2003         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
2004
2005 #ifdef CY_DEBUG_OPEN
2006         printk("cyc startup card %d, chip %d, channel %d, base_addr %lx\n",
2007              card, chip, channel, (long)base_addr);/**/
2008 #endif
2009
2010         CY_LOCK(info, flags);
2011
2012         cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2013
2014         cy_writeb(base_addr+(CyRTPR<<index), (info->default_timeout
2015                  ? info->default_timeout : 0x02)); /* 10ms rx timeout */
2016
2017         cyy_issue_cmd(base_addr,CyCHAN_CTL|CyENB_RCVR|CyENB_XMTR,index);
2018
2019         cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2020         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
2021         cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
2022
2023 #ifdef CY_DEBUG_DTR
2024         printk("cyc:startup raising DTR\n");
2025         printk("     status: 0x%x, 0x%x\n",
2026                 cy_readb(base_addr+(CyMSVR1<<index)), 
2027                 cy_readb(base_addr+(CyMSVR2<<index)));
2028 #endif
2029
2030         cy_writeb(base_addr+(CySRER<<index),
2031                 cy_readb(base_addr+(CySRER<<index)) | CyRxData);
2032         info->flags |= ASYNC_INITIALIZED;
2033
2034         if (info->tty){
2035             clear_bit(TTY_IO_ERROR, &info->tty->flags);
2036         }
2037         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2038         info->breakon = info->breakoff = 0;
2039         memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
2040         info->idle_stats.in_use    =
2041         info->idle_stats.recv_idle =
2042         info->idle_stats.xmit_idle = jiffies;
2043
2044         CY_UNLOCK(info, flags);
2045
2046     } else {
2047       struct FIRM_ID __iomem *firm_id;
2048       struct ZFW_CTRL __iomem *zfw_ctrl;
2049       struct BOARD_CTRL __iomem *board_ctrl;
2050       struct CH_CTRL __iomem *ch_ctrl;
2051       int retval;
2052
2053         base_addr = cy_card[card].base_addr;
2054
2055         firm_id = base_addr + ID_ADDRESS;
2056         if (!ISZLOADED(cy_card[card])){
2057             return -ENODEV;
2058         }
2059
2060         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2061         board_ctrl = &zfw_ctrl->board_ctrl;
2062         ch_ctrl = zfw_ctrl->ch_ctrl;
2063
2064 #ifdef CY_DEBUG_OPEN
2065         printk("cyc startup Z card %d, channel %d, base_addr %lx\n",
2066              card, channel, (long)base_addr);/**/
2067 #endif
2068
2069         CY_LOCK(info, flags);
2070
2071         cy_writel(&ch_ctrl[channel].op_mode, C_CH_ENABLE);
2072 #ifdef Z_WAKE
2073 #ifdef CONFIG_CYZ_INTR
2074         cy_writel(&ch_ctrl[channel].intr_enable, 
2075                   C_IN_TXBEMPTY|C_IN_TXLOWWM|C_IN_RXHIWM|C_IN_RXNNDT|
2076                   C_IN_IOCTLW|
2077                   C_IN_MDCD);
2078 #else
2079         cy_writel(&ch_ctrl[channel].intr_enable, 
2080                   C_IN_IOCTLW|
2081                   C_IN_MDCD);
2082 #endif /* CONFIG_CYZ_INTR */
2083 #else
2084 #ifdef CONFIG_CYZ_INTR
2085         cy_writel(&ch_ctrl[channel].intr_enable, 
2086                   C_IN_TXBEMPTY|C_IN_TXLOWWM|C_IN_RXHIWM|C_IN_RXNNDT|
2087                   C_IN_MDCD);
2088 #else
2089         cy_writel(&ch_ctrl[channel].intr_enable, 
2090                   C_IN_MDCD);
2091 #endif /* CONFIG_CYZ_INTR */
2092 #endif /* Z_WAKE */
2093
2094         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_IOCTL, 0L);
2095         if (retval != 0){
2096             printk("cyc:startup(1) retval on ttyC%d was %x\n",
2097                    info->line, retval);
2098         }
2099
2100         /* Flush RX buffers before raising DTR and RTS */
2101         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_FLUSH_RX, 0L);
2102         if (retval != 0){
2103             printk("cyc:startup(2) retval on ttyC%d was %x\n",
2104                    info->line, retval);
2105         }
2106
2107         /* set timeout !!! */
2108         /* set RTS and DTR !!! */
2109         cy_writel(&ch_ctrl[channel].rs_control,
2110              cy_readl(&ch_ctrl[channel].rs_control) | C_RS_RTS | C_RS_DTR) ;
2111         retval = cyz_issue_cmd(&cy_card[info->card],
2112             channel, C_CM_IOCTLM, 0L);
2113         if (retval != 0){
2114             printk("cyc:startup(3) retval on ttyC%d was %x\n",
2115                    info->line, retval);
2116         }
2117 #ifdef CY_DEBUG_DTR
2118             printk("cyc:startup raising Z DTR\n");
2119 #endif
2120
2121         /* enable send, recv, modem !!! */
2122
2123         info->flags |= ASYNC_INITIALIZED;
2124         if (info->tty){
2125             clear_bit(TTY_IO_ERROR, &info->tty->flags);
2126         }
2127         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2128         info->breakon = info->breakoff = 0;
2129         memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
2130         info->idle_stats.in_use    =
2131         info->idle_stats.recv_idle =
2132         info->idle_stats.xmit_idle = jiffies;
2133
2134         CY_UNLOCK(info, flags);
2135     }
2136
2137 #ifdef CY_DEBUG_OPEN
2138         printk(" cyc startup done\n");
2139 #endif
2140         return 0;
2141
2142 errout:
2143         CY_UNLOCK(info, flags);
2144         return retval;
2145 } /* startup */
2146
2147
2148 static void
2149 start_xmit( struct cyclades_port *info )
2150 {
2151   unsigned long flags;
2152   void __iomem *base_addr;
2153   int card,chip,channel,index;
2154
2155     card = info->card;
2156     channel = (info->line) - (cy_card[card].first_line);
2157     if (!IS_CYC_Z(cy_card[card])) {
2158         chip = channel>>2;
2159         channel &= 0x03;
2160         index = cy_card[card].bus_index;
2161         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
2162
2163         CY_LOCK(info, flags);
2164             cy_writeb(base_addr+(CyCAR<<index), channel);
2165             cy_writeb(base_addr+(CySRER<<index), 
2166                cy_readb(base_addr+(CySRER<<index)) | CyTxRdy);
2167         CY_UNLOCK(info, flags);
2168     } else {
2169 #ifdef CONFIG_CYZ_INTR
2170       int retval;
2171
2172         CY_LOCK(info, flags);
2173             retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_INTBACK, 0L);
2174             if (retval != 0){
2175                 printk("cyc:start_xmit retval on ttyC%d was %x\n",
2176                        info->line, retval);
2177             }
2178         CY_UNLOCK(info, flags);
2179 #else /* CONFIG_CYZ_INTR */
2180         /* Don't have to do anything at this time */
2181 #endif /* CONFIG_CYZ_INTR */
2182     }
2183 } /* start_xmit */
2184
2185 /*
2186  * This routine shuts down a serial port; interrupts are disabled,
2187  * and DTR is dropped if the hangup on close termio flag is on.
2188  */
2189 static void
2190 shutdown(struct cyclades_port * info)
2191 {
2192   unsigned long flags;
2193   void __iomem *base_addr;
2194   int card,chip,channel,index;
2195
2196     if (!(info->flags & ASYNC_INITIALIZED)){
2197         return;
2198     }
2199
2200     card = info->card;
2201     channel = info->line - cy_card[card].first_line;
2202     if (!IS_CYC_Z(cy_card[card])) {
2203         chip = channel>>2;
2204         channel &= 0x03;
2205         index = cy_card[card].bus_index;
2206         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
2207
2208 #ifdef CY_DEBUG_OPEN
2209     printk("cyc shutdown Y card %d, chip %d, channel %d, base_addr %lx\n",
2210                 card, chip, channel, (long)base_addr);
2211 #endif
2212
2213         CY_LOCK(info, flags);
2214
2215             /* Clear delta_msr_wait queue to avoid mem leaks. */
2216             wake_up_interruptible(&info->delta_msr_wait);
2217
2218             if (info->xmit_buf){
2219                 unsigned char * temp;
2220                 temp = info->xmit_buf;
2221                 info->xmit_buf = NULL;
2222                 free_page((unsigned long) temp);
2223             }
2224             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2225             if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
2226                 cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
2227                 cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
2228 #ifdef CY_DEBUG_DTR
2229                 printk("cyc shutdown dropping DTR\n");
2230                 printk("     status: 0x%x, 0x%x\n",
2231                     cy_readb(base_addr+(CyMSVR1<<index)), 
2232                     cy_readb(base_addr+(CyMSVR2<<index)));
2233 #endif
2234             }
2235             cyy_issue_cmd(base_addr,CyCHAN_CTL|CyDIS_RCVR,index);
2236              /* it may be appropriate to clear _XMIT at
2237                some later date (after testing)!!! */
2238
2239             if (info->tty){
2240                 set_bit(TTY_IO_ERROR, &info->tty->flags);
2241             }
2242             info->flags &= ~ASYNC_INITIALIZED;
2243         CY_UNLOCK(info, flags);
2244     } else {
2245       struct FIRM_ID __iomem *firm_id;
2246       struct ZFW_CTRL __iomem *zfw_ctrl;
2247       struct BOARD_CTRL __iomem *board_ctrl;
2248       struct CH_CTRL __iomem *ch_ctrl;
2249       int retval;
2250
2251         base_addr = cy_card[card].base_addr;
2252 #ifdef CY_DEBUG_OPEN
2253     printk("cyc shutdown Z card %d, channel %d, base_addr %lx\n",
2254                 card, channel, (long)base_addr);
2255 #endif
2256
2257         firm_id = base_addr + ID_ADDRESS;
2258         if (!ISZLOADED(cy_card[card])) {
2259             return;
2260         }
2261
2262         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2263         board_ctrl = &zfw_ctrl->board_ctrl;
2264         ch_ctrl = zfw_ctrl->ch_ctrl;
2265
2266         CY_LOCK(info, flags);
2267
2268             if (info->xmit_buf){
2269                 unsigned char * temp;
2270                 temp = info->xmit_buf;
2271                 info->xmit_buf = NULL;
2272                 free_page((unsigned long) temp);
2273             }
2274             
2275             if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
2276                 cy_writel(&ch_ctrl[channel].rs_control,
2277                    (uclong)(cy_readl(&ch_ctrl[channel].rs_control) & 
2278                    ~(C_RS_RTS | C_RS_DTR)));
2279                 retval = cyz_issue_cmd(&cy_card[info->card],
2280                         channel, C_CM_IOCTLM, 0L);
2281                 if (retval != 0){
2282                     printk("cyc:shutdown retval on ttyC%d was %x\n",
2283                            info->line, retval);
2284                 }
2285 #ifdef CY_DEBUG_DTR
2286                 printk("cyc:shutdown dropping Z DTR\n");
2287 #endif
2288             }
2289             
2290             if (info->tty){
2291                 set_bit(TTY_IO_ERROR, &info->tty->flags);
2292             }
2293             info->flags &= ~ASYNC_INITIALIZED;
2294
2295         CY_UNLOCK(info, flags);
2296     }
2297
2298 #ifdef CY_DEBUG_OPEN
2299     printk(" cyc shutdown done\n");
2300 #endif
2301     return;
2302 } /* shutdown */
2303
2304
2305 /*
2306  * ------------------------------------------------------------
2307  * cy_open() and friends
2308  * ------------------------------------------------------------
2309  */
2310
2311 static int
2312 block_til_ready(struct tty_struct *tty, struct file * filp,
2313                            struct cyclades_port *info)
2314 {
2315   DECLARE_WAITQUEUE(wait, current);
2316   struct cyclades_card *cinfo;
2317   unsigned long flags;
2318   int chip, channel,index;
2319   int retval;
2320   void __iomem *base_addr;
2321
2322     cinfo = &cy_card[info->card];
2323     channel = info->line - cinfo->first_line;
2324
2325     /*
2326      * If the device is in the middle of being closed, then block
2327      * until it's done, and then try again.
2328      */
2329     if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
2330         if (info->flags & ASYNC_CLOSING) {
2331             interruptible_sleep_on(&info->close_wait);
2332         }
2333         return ((info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
2334     }
2335
2336     /*
2337      * If non-blocking mode is set, then make the check up front
2338      * and then exit.
2339      */
2340     if ((filp->f_flags & O_NONBLOCK) ||
2341         (tty->flags & (1 << TTY_IO_ERROR))) {
2342         info->flags |= ASYNC_NORMAL_ACTIVE;
2343         return 0;
2344     }
2345
2346     /*
2347      * Block waiting for the carrier detect and the line to become
2348      * free (i.e., not in use by the callout).  While we are in
2349      * this loop, info->count is dropped by one, so that
2350      * cy_close() knows when to free things.  We restore it upon
2351      * exit, either normal or abnormal.
2352      */
2353     retval = 0;
2354     add_wait_queue(&info->open_wait, &wait);
2355 #ifdef CY_DEBUG_OPEN
2356     printk("cyc block_til_ready before block: ttyC%d, count = %d\n",
2357            info->line, info->count);/**/
2358 #endif
2359     CY_LOCK(info, flags);
2360     if (!tty_hung_up_p(filp))
2361         info->count--;
2362     CY_UNLOCK(info, flags);
2363 #ifdef CY_DEBUG_COUNT
2364     printk("cyc block_til_ready: (%d): decrementing count to %d\n",
2365         current->pid, info->count);
2366 #endif
2367     info->blocked_open++;
2368
2369     if (!IS_CYC_Z(*cinfo)) {
2370         chip = channel>>2;
2371         channel &= 0x03;
2372         index = cinfo->bus_index;
2373         base_addr = cinfo->base_addr + (cy_chip_offset[chip]<<index);
2374
2375         while (1) {
2376             CY_LOCK(info, flags);
2377                 if ((tty->termios->c_cflag & CBAUD)){
2378                     cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2379                     cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
2380                     cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
2381 #ifdef CY_DEBUG_DTR
2382                     printk("cyc:block_til_ready raising DTR\n");
2383                     printk("     status: 0x%x, 0x%x\n",
2384                         cy_readb(base_addr+(CyMSVR1<<index)), 
2385                         cy_readb(base_addr+(CyMSVR2<<index)));
2386 #endif
2387                 }
2388             CY_UNLOCK(info, flags);
2389
2390             set_current_state(TASK_INTERRUPTIBLE);
2391             if (tty_hung_up_p(filp)
2392             || !(info->flags & ASYNC_INITIALIZED) ){
2393                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ? 
2394                     -EAGAIN : -ERESTARTSYS);
2395                 break;
2396             }
2397
2398             CY_LOCK(info, flags);
2399                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2400                 if (!(info->flags & ASYNC_CLOSING)
2401                 && (C_CLOCAL(tty)
2402                     || (cy_readb(base_addr+(CyMSVR1<<index)) & CyDCD))) {
2403                         CY_UNLOCK(info, flags);
2404                         break;
2405                 }
2406             CY_UNLOCK(info, flags);
2407
2408             if (signal_pending(current)) {
2409                 retval = -ERESTARTSYS;
2410                 break;
2411             }
2412 #ifdef CY_DEBUG_OPEN
2413             printk("cyc block_til_ready blocking: ttyC%d, count = %d\n",
2414                    info->line, info->count);/**/
2415 #endif
2416             schedule();
2417         }
2418     } else {
2419       struct FIRM_ID __iomem *firm_id;
2420       struct ZFW_CTRL __iomem *zfw_ctrl;
2421       struct BOARD_CTRL __iomem *board_ctrl;
2422       struct CH_CTRL __iomem *ch_ctrl;
2423       int retval;
2424
2425         base_addr = cinfo->base_addr;
2426         firm_id = base_addr + ID_ADDRESS;
2427         if (!ISZLOADED(*cinfo)){
2428             current->state = TASK_RUNNING;
2429             remove_wait_queue(&info->open_wait, &wait);
2430             return -EINVAL;
2431         }
2432
2433         zfw_ctrl = base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2434         board_ctrl = &zfw_ctrl->board_ctrl;
2435         ch_ctrl = zfw_ctrl->ch_ctrl;
2436
2437         while (1) {
2438             if ((tty->termios->c_cflag & CBAUD)){
2439                 cy_writel(&ch_ctrl[channel].rs_control,
2440                         cy_readl(&ch_ctrl[channel].rs_control) |
2441                         (C_RS_RTS | C_RS_DTR));
2442                 retval = cyz_issue_cmd(&cy_card[info->card],
2443                                        channel, C_CM_IOCTLM, 0L);
2444                 if (retval != 0){
2445                     printk("cyc:block_til_ready retval on ttyC%d was %x\n",
2446                            info->line, retval);
2447                 }
2448 #ifdef CY_DEBUG_DTR
2449                 printk("cyc:block_til_ready raising Z DTR\n");
2450 #endif
2451             }
2452
2453             set_current_state(TASK_INTERRUPTIBLE);
2454             if (tty_hung_up_p(filp)
2455             || !(info->flags & ASYNC_INITIALIZED) ){
2456                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2457                     -EAGAIN : -ERESTARTSYS);
2458                 break;
2459             }
2460             if (!(info->flags & ASYNC_CLOSING)
2461             && (C_CLOCAL(tty)
2462               || (cy_readl(&ch_ctrl[channel].rs_status) & C_RS_DCD))) {
2463                 break;
2464             }
2465             if (signal_pending(current)) {
2466                 retval = -ERESTARTSYS;
2467                 break;
2468             }
2469 #ifdef CY_DEBUG_OPEN
2470             printk("cyc block_til_ready blocking: ttyC%d, count = %d\n",
2471                    info->line, info->count);/**/
2472 #endif
2473             schedule();
2474         }
2475     }
2476     current->state = TASK_RUNNING;
2477     remove_wait_queue(&info->open_wait, &wait);
2478     if (!tty_hung_up_p(filp)){
2479         info->count++;
2480 #ifdef CY_DEBUG_COUNT
2481         printk("cyc:block_til_ready (%d): incrementing count to %d\n",
2482             current->pid, info->count);
2483 #endif
2484     }
2485     info->blocked_open--;
2486 #ifdef CY_DEBUG_OPEN
2487     printk("cyc:block_til_ready after blocking: ttyC%d, count = %d\n",
2488            info->line, info->count);/**/
2489 #endif
2490     if (retval)
2491         return retval;
2492     info->flags |= ASYNC_NORMAL_ACTIVE;
2493     return 0;
2494 } /* block_til_ready */
2495
2496
2497 /*
2498  * This routine is called whenever a serial port is opened.  It
2499  * performs the serial-specific initialization for the tty structure.
2500  */
2501 static int
2502 cy_open(struct tty_struct *tty, struct file * filp)
2503 {
2504   struct cyclades_port  *info;
2505   int retval, line;
2506   unsigned long page;
2507
2508     line = tty->index;
2509     if ((line < 0) || (NR_PORTS <= line)){
2510         return -ENODEV;
2511     }
2512     info = &cy_port[line];
2513     if (info->line < 0){
2514         return -ENODEV;
2515     }
2516     
2517     /* If the card's firmware hasn't been loaded,
2518        treat it as absent from the system.  This
2519        will make the user pay attention.
2520     */
2521     if (IS_CYC_Z(cy_card[info->card])) {
2522         struct cyclades_card *cinfo = &cy_card[info->card];
2523         struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
2524
2525         if (!ISZLOADED(*cinfo)) {
2526             if (((ZE_V1 ==cy_readl(&((struct RUNTIME_9060 __iomem *)
2527                 (cinfo->ctl_addr))->mail_box_0)) &&
2528                 Z_FPGA_CHECK (*cinfo)) &&
2529                 (ZFIRM_HLT == cy_readl (&firm_id->signature)))
2530             {
2531                 printk ("cyc:Cyclades-Z Error: you need an external power supply for this number of ports.\n\rFirmware halted.\r\n");
2532             } else {
2533                 printk("cyc:Cyclades-Z firmware not yet loaded\n");
2534             }
2535             return -ENODEV;
2536         }
2537 #ifdef CONFIG_CYZ_INTR
2538         else {
2539             /* In case this Z board is operating in interrupt mode, its 
2540                interrupts should be enabled as soon as the first open happens 
2541                to one of its ports. */
2542             if (!cinfo->intr_enabled) {
2543                 struct ZFW_CTRL __iomem *zfw_ctrl;
2544                 struct BOARD_CTRL __iomem *board_ctrl;
2545
2546                 zfw_ctrl = cinfo->base_addr + (cy_readl (&firm_id->zfwctrl_addr) & 0xfffff);
2547
2548                 board_ctrl = &zfw_ctrl->board_ctrl;
2549
2550                 /* Enable interrupts on the PLX chip */
2551                 cy_writew(cinfo->ctl_addr+0x68,
2552                         cy_readw(cinfo->ctl_addr+0x68)|0x0900);
2553                 /* Enable interrupts on the FW */
2554                 retval = cyz_issue_cmd(cinfo,
2555                                         0, C_CM_IRQ_ENBL, 0L);
2556                 if (retval != 0){
2557                     printk("cyc:IRQ enable retval was %x\n", retval);
2558                 }
2559                 cinfo->nports = (int) cy_readl (&board_ctrl->n_channel);
2560                 cinfo->intr_enabled = 1;
2561             }
2562         }
2563 #endif /* CONFIG_CYZ_INTR */
2564         /* Make sure this Z port really exists in hardware */
2565         if (info->line > (cinfo->first_line + cinfo->nports - 1))
2566                 return -ENODEV;
2567     }
2568 #ifdef CY_DEBUG_OTHER
2569     printk("cyc:cy_open ttyC%d\n", info->line); /* */
2570 #endif
2571     tty->driver_data = info;
2572     info->tty = tty;
2573     if (serial_paranoia_check(info, tty->name, "cy_open")){
2574         return -ENODEV;
2575     }
2576 #ifdef CY_DEBUG_OPEN
2577     printk("cyc:cy_open ttyC%d, count = %d\n",
2578         info->line, info->count);/**/
2579 #endif
2580     info->count++;
2581 #ifdef CY_DEBUG_COUNT
2582     printk("cyc:cy_open (%d): incrementing count to %d\n",
2583         current->pid, info->count);
2584 #endif
2585     if (!tmp_buf) {
2586         page = get_zeroed_page(GFP_KERNEL);
2587         if (!page)
2588             return -ENOMEM;
2589         if (tmp_buf)
2590             free_page(page);
2591         else
2592             tmp_buf = (unsigned char *) page;
2593     }
2594
2595     /*
2596      * If the port is the middle of closing, bail out now
2597      */
2598     if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
2599         if (info->flags & ASYNC_CLOSING)
2600             interruptible_sleep_on(&info->close_wait);
2601         return ((info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
2602     }
2603
2604     /*
2605      * Start up serial port
2606      */
2607     retval = startup(info);
2608     if (retval){
2609         return retval;
2610     }
2611
2612     retval = block_til_ready(tty, filp, info);
2613     if (retval) {
2614 #ifdef CY_DEBUG_OPEN
2615         printk("cyc:cy_open returning after block_til_ready with %d\n",
2616                retval);
2617 #endif
2618         return retval;
2619     }
2620
2621     info->throttle = 0;
2622
2623 #ifdef CY_DEBUG_OPEN
2624     printk(" cyc:cy_open done\n");/**/
2625 #endif
2626
2627     return 0;
2628 } /* cy_open */
2629
2630
2631 /*
2632  * cy_wait_until_sent() --- wait until the transmitter is empty
2633  */
2634 static void 
2635 cy_wait_until_sent(struct tty_struct *tty, int timeout)
2636 {
2637   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
2638   void __iomem *base_addr;
2639   int card,chip,channel,index;
2640   unsigned long orig_jiffies;
2641   int char_time;
2642         
2643     if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
2644         return;
2645
2646     if (info->xmit_fifo_size == 0)
2647         return; /* Just in case.... */
2648
2649
2650     orig_jiffies = jiffies;
2651     /*
2652      * Set the check interval to be 1/5 of the estimated time to
2653      * send a single character, and make it at least 1.  The check
2654      * interval should also be less than the timeout.
2655      * 
2656      * Note: we have to use pretty tight timings here to satisfy
2657      * the NIST-PCTS.
2658      */
2659     char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2660     char_time = char_time / 5;
2661     if (char_time <= 0)
2662         char_time = 1;
2663     if (timeout < 0)
2664         timeout = 0;
2665     if (timeout)
2666         char_time = min(char_time, timeout);
2667     /*
2668      * If the transmitter hasn't cleared in twice the approximate
2669      * amount of time to send the entire FIFO, it probably won't
2670      * ever clear.  This assumes the UART isn't doing flow
2671      * control, which is currently the case.  Hence, if it ever
2672      * takes longer than info->timeout, this is probably due to a
2673      * UART bug of some kind.  So, we clamp the timeout parameter at
2674      * 2*info->timeout.
2675      */
2676     if (!timeout || timeout > 2*info->timeout)
2677         timeout = 2*info->timeout;
2678 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2679     printk("In cy_wait_until_sent(%d) check=%lu...", timeout, char_time);
2680     printk("jiff=%lu...", jiffies);
2681 #endif
2682     card = info->card;
2683     channel = (info->line) - (cy_card[card].first_line);
2684     if (!IS_CYC_Z(cy_card[card])) {
2685         chip = channel>>2;
2686         channel &= 0x03;
2687         index = cy_card[card].bus_index;
2688         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
2689         while (cy_readb(base_addr+(CySRER<<index)) & CyTxRdy) {
2690 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2691             printk("Not clean (jiff=%lu)...", jiffies);
2692 #endif
2693             if (msleep_interruptible(jiffies_to_msecs(char_time)))
2694                 break;
2695             if (timeout && time_after(jiffies, orig_jiffies + timeout))
2696                 break;
2697         }
2698     } else {
2699         // Nothing to do!
2700     }
2701     /* Run one more char cycle */
2702     msleep_interruptible(jiffies_to_msecs(char_time * 5));
2703 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2704     printk("Clean (jiff=%lu)...done\n", jiffies);
2705 #endif
2706 }
2707
2708 /*
2709  * This routine is called when a particular tty device is closed.
2710  */
2711 static void
2712 cy_close(struct tty_struct *tty, struct file *filp)
2713 {
2714   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2715   unsigned long flags;
2716
2717 #ifdef CY_DEBUG_OTHER
2718     printk("cyc:cy_close ttyC%d\n", info->line);
2719 #endif
2720
2721     if (!info || serial_paranoia_check(info, tty->name, "cy_close")){
2722         return;
2723     }
2724
2725     CY_LOCK(info, flags);
2726     /* If the TTY is being hung up, nothing to do */
2727     if (tty_hung_up_p(filp)) {
2728         CY_UNLOCK(info, flags);
2729         return;
2730     }
2731         
2732 #ifdef CY_DEBUG_OPEN
2733     printk("cyc:cy_close ttyC%d, count = %d\n", info->line, info->count);
2734 #endif
2735     if ((tty->count == 1) && (info->count != 1)) {
2736         /*
2737          * Uh, oh.  tty->count is 1, which means that the tty
2738          * structure will be freed.  Info->count should always
2739          * be one in these conditions.  If it's greater than
2740          * one, we've got real problems, since it means the
2741          * serial port won't be shutdown.
2742          */
2743         printk("cyc:cy_close: bad serial port count; tty->count is 1, "
2744            "info->count is %d\n", info->count);
2745         info->count = 1;
2746     }
2747 #ifdef CY_DEBUG_COUNT
2748     printk("cyc:cy_close at (%d): decrementing count to %d\n",
2749         current->pid, info->count - 1);
2750 #endif
2751     if (--info->count < 0) {
2752 #ifdef CY_DEBUG_COUNT
2753     printk("cyc:cyc_close setting count to 0\n");
2754 #endif
2755         info->count = 0;
2756     }
2757     if (info->count) {
2758         CY_UNLOCK(info, flags);
2759         return;
2760     }
2761     info->flags |= ASYNC_CLOSING;
2762
2763     /*
2764     * Now we wait for the transmit buffer to clear; and we notify
2765     * the line discipline to only process XON/XOFF characters.
2766     */
2767     tty->closing = 1;
2768     CY_UNLOCK(info, flags);
2769     if (info->closing_wait != CY_CLOSING_WAIT_NONE) {
2770         tty_wait_until_sent(tty, info->closing_wait);
2771     }
2772     CY_LOCK(info, flags);
2773
2774     if (!IS_CYC_Z(cy_card[info->card])) {
2775         int channel = info->line - cy_card[info->card].first_line;
2776         int index = cy_card[info->card].bus_index;
2777         void __iomem *base_addr = cy_card[info->card].base_addr + (cy_chip_offset[channel>>2] << index);
2778         /* Stop accepting input */
2779         channel &= 0x03;
2780         cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
2781         cy_writeb(base_addr+(CySRER<<index),
2782                         cy_readb(base_addr+(CySRER<<index)) & ~CyRxData);
2783         if (info->flags & ASYNC_INITIALIZED) {
2784             /* Waiting for on-board buffers to be empty before closing 
2785                the port */
2786             CY_UNLOCK(info, flags);
2787             cy_wait_until_sent(tty, info->timeout);
2788             CY_LOCK(info, flags);
2789         }
2790     } else {
2791 #ifdef Z_WAKE
2792         /* Waiting for on-board buffers to be empty before closing the port */
2793         void __iomem *base_addr = cy_card[info->card].base_addr;
2794         struct FIRM_ID __iomem *firm_id = base_addr + ID_ADDRESS;
2795         struct ZFW_CTRL __iomem *zfw_ctrl = base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
2796         struct CH_CTRL __iomem *ch_ctrl = zfw_ctrl->ch_ctrl;
2797         int channel = info->line - cy_card[info->card].first_line;
2798         int retval;
2799
2800         if (cy_readl(&ch_ctrl[channel].flow_status) != C_FS_TXIDLE) {
2801             retval = cyz_issue_cmd(&cy_card[info->card], channel, 
2802                                    C_CM_IOCTLW, 0L);
2803             if (retval != 0){
2804                 printk("cyc:cy_close retval on ttyC%d was %x\n",
2805                        info->line, retval);
2806             }
2807             CY_UNLOCK(info, flags);
2808             interruptible_sleep_on(&info->shutdown_wait);
2809             CY_LOCK(info, flags);
2810         }
2811 #endif
2812     }
2813
2814     CY_UNLOCK(info, flags);
2815     shutdown(info);
2816     if (tty->driver->flush_buffer)
2817         tty->driver->flush_buffer(tty);
2818     tty_ldisc_flush(tty);        
2819     CY_LOCK(info, flags);
2820
2821     tty->closing = 0;
2822     info->event = 0;
2823     info->tty = NULL;
2824     if (info->blocked_open) {
2825         CY_UNLOCK(info, flags);
2826         if (info->close_delay) {
2827             msleep_interruptible(jiffies_to_msecs(info->close_delay));
2828         }
2829         wake_up_interruptible(&info->open_wait);
2830         CY_LOCK(info, flags);
2831     }
2832     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2833     wake_up_interruptible(&info->close_wait);
2834
2835 #ifdef CY_DEBUG_OTHER
2836     printk(" cyc:cy_close done\n");
2837 #endif
2838
2839     CY_UNLOCK(info, flags);
2840     return;
2841 } /* cy_close */
2842
2843
2844 /* This routine gets called when tty_write has put something into
2845  * the write_queue.  The characters may come from user space or
2846  * kernel space.
2847  *
2848  * This routine will return the number of characters actually
2849  * accepted for writing.
2850  *
2851  * If the port is not already transmitting stuff, start it off by
2852  * enabling interrupts.  The interrupt service routine will then
2853  * ensure that the characters are sent.
2854  * If the port is already active, there is no need to kick it.
2855  *
2856  */
2857 static int
2858 cy_write(struct tty_struct * tty, const unsigned char *buf, int count)
2859 {
2860   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2861   unsigned long flags;
2862   int c, ret = 0;
2863
2864 #ifdef CY_DEBUG_IO
2865     printk("cyc:cy_write ttyC%d\n", info->line); /* */
2866 #endif
2867
2868     if (serial_paranoia_check(info, tty->name, "cy_write")){
2869         return 0;
2870     }
2871         
2872     if (!tty || !info->xmit_buf || !tmp_buf){
2873         return 0;
2874     }
2875
2876     CY_LOCK(info, flags);
2877     while (1) {
2878         c = min(count, min((int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1),
2879                         (int)(SERIAL_XMIT_SIZE - info->xmit_head)));
2880                 
2881         if (c <= 0)
2882             break;
2883
2884         memcpy(info->xmit_buf + info->xmit_head, buf, c);
2885         info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
2886         info->xmit_cnt += c;
2887         buf += c;
2888         count -= c;
2889         ret += c;
2890     }
2891     CY_UNLOCK(info, flags);
2892
2893     info->idle_stats.xmit_bytes += ret;
2894     info->idle_stats.xmit_idle   = jiffies;
2895
2896     if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2897         start_xmit(info);
2898     }
2899     return ret;
2900 } /* cy_write */
2901
2902
2903 /*
2904  * This routine is called by the kernel to write a single
2905  * character to the tty device.  If the kernel uses this routine,
2906  * it must call the flush_chars() routine (if defined) when it is
2907  * done stuffing characters into the driver.  If there is no room
2908  * in the queue, the character is ignored.
2909  */
2910 static void
2911 cy_put_char(struct tty_struct *tty, unsigned char ch)
2912 {
2913   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2914   unsigned long flags;
2915
2916 #ifdef CY_DEBUG_IO
2917     printk("cyc:cy_put_char ttyC%d\n", info->line);
2918 #endif
2919
2920     if (serial_paranoia_check(info, tty->name, "cy_put_char"))
2921         return;
2922
2923     if (!tty || !info->xmit_buf)
2924         return;
2925
2926     CY_LOCK(info, flags);
2927         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
2928             CY_UNLOCK(info, flags);
2929             return;
2930         }
2931
2932         info->xmit_buf[info->xmit_head++] = ch;
2933         info->xmit_head &= SERIAL_XMIT_SIZE - 1;
2934         info->xmit_cnt++;
2935         info->idle_stats.xmit_bytes++;
2936         info->idle_stats.xmit_idle = jiffies;
2937     CY_UNLOCK(info, flags);
2938 } /* cy_put_char */
2939
2940
2941 /*
2942  * This routine is called by the kernel after it has written a
2943  * series of characters to the tty device using put_char().  
2944  */
2945 static void
2946 cy_flush_chars(struct tty_struct *tty)
2947 {
2948   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2949                                 
2950 #ifdef CY_DEBUG_IO
2951     printk("cyc:cy_flush_chars ttyC%d\n", info->line); /* */
2952 #endif
2953
2954     if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
2955         return;
2956
2957     if (info->xmit_cnt <= 0 || tty->stopped
2958     || tty->hw_stopped || !info->xmit_buf)
2959         return;
2960
2961     start_xmit(info);
2962 } /* cy_flush_chars */
2963
2964
2965 /*
2966  * This routine returns the numbers of characters the tty driver
2967  * will accept for queuing to be written.  This number is subject
2968  * to change as output buffers get emptied, or if the output flow
2969  * control is activated.
2970  */
2971 static int
2972 cy_write_room(struct tty_struct *tty)
2973 {
2974   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2975   int   ret;
2976                                 
2977 #ifdef CY_DEBUG_IO
2978     printk("cyc:cy_write_room ttyC%d\n", info->line); /* */
2979 #endif
2980
2981     if (serial_paranoia_check(info, tty->name, "cy_write_room"))
2982         return 0;
2983     ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2984     if (ret < 0)
2985         ret = 0;
2986     return ret;
2987 } /* cy_write_room */
2988
2989
2990 static int
2991 cy_chars_in_buffer(struct tty_struct *tty)
2992 {
2993   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
2994   int card, channel;
2995                                 
2996     if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
2997         return 0;
2998
2999     card = info->card;
3000     channel = (info->line) - (cy_card[card].first_line);
3001
3002 #ifdef Z_EXT_CHARS_IN_BUFFER
3003     if (!IS_CYC_Z(cy_card[card])) {
3004 #endif /* Z_EXT_CHARS_IN_BUFFER */
3005 #ifdef CY_DEBUG_IO
3006         printk("cyc:cy_chars_in_buffer ttyC%d %d\n",
3007                 info->line, info->xmit_cnt); /* */
3008 #endif
3009         return info->xmit_cnt;
3010 #ifdef Z_EXT_CHARS_IN_BUFFER
3011     } else {
3012         static volatile struct FIRM_ID *firm_id;
3013         static volatile struct ZFW_CTRL *zfw_ctrl;
3014         static volatile struct CH_CTRL *ch_ctrl;
3015         static volatile struct BUF_CTRL *buf_ctrl;
3016         int char_count;
3017         volatile uclong tx_put, tx_get, tx_bufsize;
3018
3019         firm_id = cy_card[card].base_addr + ID_ADDRESS;
3020         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
3021         ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
3022         buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
3023
3024         tx_get = cy_readl(&buf_ctrl->tx_get);
3025         tx_put = cy_readl(&buf_ctrl->tx_put);
3026         tx_bufsize = cy_readl(&buf_ctrl->tx_bufsize);
3027         if (tx_put >= tx_get)
3028             char_count = tx_put - tx_get;
3029         else
3030             char_count = tx_put - tx_get + tx_bufsize;
3031 #ifdef CY_DEBUG_IO
3032         printk("cyc:cy_chars_in_buffer ttyC%d %d\n",
3033                 info->line, info->xmit_cnt + char_count); /* */
3034 #endif
3035         return (info->xmit_cnt + char_count);
3036     }
3037 #endif /* Z_EXT_CHARS_IN_BUFFER */
3038 } /* cy_chars_in_buffer */
3039
3040
3041 /*
3042  * ------------------------------------------------------------
3043  * cy_ioctl() and friends
3044  * ------------------------------------------------------------
3045  */
3046
3047 static void
3048 cyy_baud_calc(struct cyclades_port *info, uclong baud)
3049 {
3050     int co, co_val, bpr;
3051     uclong cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 : 25000000);
3052
3053     if (baud == 0) {
3054         info->tbpr = info->tco = info->rbpr = info->rco = 0;
3055         return;
3056     }
3057
3058     /* determine which prescaler to use */
3059     for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
3060         if (cy_clock / co_val / baud > 63)
3061             break;
3062     }
3063
3064     bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
3065     if (bpr > 255)
3066         bpr = 255;
3067
3068     info->tbpr = info->rbpr = bpr;
3069     info->tco = info->rco = co;
3070 }
3071
3072 /*
3073  * This routine finds or computes the various line characteristics.
3074  * It used to be called config_setup
3075  */
3076 static void
3077 set_line_char(struct cyclades_port * info)
3078 {
3079   unsigned long flags;
3080   void __iomem *base_addr;
3081   int card,chip,channel,index;
3082   unsigned cflag, iflag;
3083   unsigned short chip_number;
3084   int baud, baud_rate = 0;
3085   int   i;
3086
3087
3088     if (!info->tty || !info->tty->termios){
3089         return;
3090     }
3091     if (info->line == -1){
3092         return;
3093     }
3094     cflag = info->tty->termios->c_cflag;
3095     iflag = info->tty->termios->c_iflag;
3096
3097     /*
3098      * Set up the tty->alt_speed kludge
3099      */
3100     if (info->tty) {
3101         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3102             info->tty->alt_speed = 57600;
3103         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3104             info->tty->alt_speed = 115200;
3105         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3106             info->tty->alt_speed = 230400;
3107         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3108             info->tty->alt_speed = 460800;
3109     }
3110
3111     card = info->card;
3112     channel = (info->line) - (cy_card[card].first_line);
3113     chip_number = channel / 4;
3114
3115     if (!IS_CYC_Z(cy_card[card])) {
3116
3117         index = cy_card[card].bus_index;
3118
3119         /* baud rate */
3120         baud = tty_get_baud_rate(info->tty);
3121         if ((baud == 38400) &&
3122             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3123             if (info->custom_divisor)
3124                 baud_rate = info->baud / info->custom_divisor;
3125             else
3126                 baud_rate = info->baud;
3127         } else if (baud > CD1400_MAX_SPEED) {
3128             baud = CD1400_MAX_SPEED;
3129         }
3130         /* find the baud index */
3131         for (i = 0; i < 20; i++) {
3132             if (baud == baud_table[i]) {
3133                 break;
3134             }
3135         }
3136         if (i == 20) {
3137             i = 19; /* CD1400_MAX_SPEED */
3138         } 
3139
3140         if ((baud == 38400) &&
3141             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3142             cyy_baud_calc(info, baud_rate);
3143         } else {
3144             if(info->chip_rev >= CD1400_REV_J) {
3145                 /* It is a CD1400 rev. J or later */
3146                 info->tbpr = baud_bpr_60[i]; /* Tx BPR */
3147                 info->tco = baud_co_60[i]; /* Tx CO */
3148                 info->rbpr = baud_bpr_60[i]; /* Rx BPR */
3149                 info->rco = baud_co_60[i]; /* Rx CO */
3150             } else {
3151                 info->tbpr = baud_bpr_25[i]; /* Tx BPR */
3152                 info->tco = baud_co_25[i]; /* Tx CO */
3153                 info->rbpr = baud_bpr_25[i]; /* Rx BPR */
3154                 info->rco = baud_co_25[i]; /* Rx CO */
3155             }
3156         }
3157         if (baud_table[i] == 134) {
3158             /* get it right for 134.5 baud */
3159             info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
3160         } else if ((baud == 38400) &&
3161                    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3162             info->timeout = (info->xmit_fifo_size*HZ*15/baud_rate) + 2;
3163         } else if (baud_table[i]) {
3164             info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
3165             /* this needs to be propagated into the card info */
3166         } else {
3167             info->timeout = 0;
3168         }
3169         /* By tradition (is it a standard?) a baud rate of zero
3170            implies the line should be/has been closed.  A bit
3171            later in this routine such a test is performed. */
3172
3173         /* byte size and parity */
3174         info->cor5 = 0;
3175         info->cor4 = 0;
3176         info->cor3 = (info->default_threshold
3177                       ? info->default_threshold
3178                       : baud_cor3[i]); /* receive threshold */
3179         info->cor2 = CyETC;
3180         switch(cflag & CSIZE){
3181         case CS5:
3182             info->cor1 = Cy_5_BITS;
3183             break;
3184         case CS6:
3185             info->cor1 = Cy_6_BITS;
3186             break;
3187         case CS7:
3188             info->cor1 = Cy_7_BITS;
3189             break;
3190         case CS8:
3191             info->cor1 = Cy_8_BITS;
3192             break;
3193         }
3194         if(cflag & CSTOPB){
3195             info->cor1 |= Cy_2_STOP;
3196         }
3197         if (cflag & PARENB){
3198             if (cflag & PARODD){
3199                 info->cor1 |= CyPARITY_O;
3200             }else{
3201                 info->cor1 |= CyPARITY_E;
3202             }
3203         }else{
3204             info->cor1 |= CyPARITY_NONE;
3205         }
3206             
3207         /* CTS flow control flag */
3208         if (cflag & CRTSCTS){
3209             info->flags |= ASYNC_CTS_FLOW;
3210             info->cor2 |= CyCtsAE;
3211         }else{
3212             info->flags &= ~ASYNC_CTS_FLOW;
3213             info->cor2 &= ~CyCtsAE;
3214         }
3215         if (cflag & CLOCAL)
3216             info->flags &= ~ASYNC_CHECK_CD;
3217         else
3218             info->flags |= ASYNC_CHECK_CD;
3219
3220          /***********************************************
3221             The hardware option, CyRtsAO, presents RTS when
3222             the chip has characters to send.  Since most modems
3223             use RTS as reverse (inbound) flow control, this
3224             option is not used.  If inbound flow control is
3225             necessary, DTR can be programmed to provide the
3226             appropriate signals for use with a non-standard
3227             cable.  Contact Marcio Saito for details.
3228          ***********************************************/
3229
3230         chip = channel>>2;
3231         channel &= 0x03;
3232         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3233
3234         CY_LOCK(info, flags);
3235             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3236
3237            /* tx and rx baud rate */
3238
3239             cy_writeb(base_addr+(CyTCOR<<index), info->tco);
3240             cy_writeb(base_addr+(CyTBPR<<index), info->tbpr);
3241             cy_writeb(base_addr+(CyRCOR<<index), info->rco);
3242             cy_writeb(base_addr+(CyRBPR<<index), info->rbpr);
3243
3244             /* set line characteristics  according configuration */
3245
3246             cy_writeb(base_addr+(CySCHR1<<index), 
3247                       START_CHAR(info->tty));
3248             cy_writeb(base_addr+(CySCHR2<<index), 
3249                       STOP_CHAR(info->tty));
3250             cy_writeb(base_addr+(CyCOR1<<index), info->cor1);
3251             cy_writeb(base_addr+(CyCOR2<<index), info->cor2);
3252             cy_writeb(base_addr+(CyCOR3<<index), info->cor3);
3253             cy_writeb(base_addr+(CyCOR4<<index), info->cor4);
3254             cy_writeb(base_addr+(CyCOR5<<index), info->cor5);
3255
3256             cyy_issue_cmd(base_addr,
3257                      CyCOR_CHANGE|CyCOR1ch|CyCOR2ch|CyCOR3ch,index);
3258
3259             cy_writeb(base_addr+(CyCAR<<index), 
3260                       (u_char)channel); /* !!! Is this needed? */
3261             cy_writeb(base_addr+(CyRTPR<<index), (info->default_timeout
3262                                                  ? info->default_timeout
3263                                                  : 0x02)); /* 10ms rx timeout */
3264
3265             if (C_CLOCAL(info->tty)) {
3266                 /* without modem intr */
3267                 cy_writeb(base_addr+(CySRER<<index),
3268                    cy_readb(base_addr+(CySRER<<index)) | CyMdmCh); 
3269                                         /* act on 1->0 modem transitions */
3270                 if ((cflag & CRTSCTS) && info->rflow) {
3271                         cy_writeb(base_addr+(CyMCOR1<<index), 
3272                                   (CyCTS|rflow_thr[i]));
3273                 } else {
3274                         cy_writeb(base_addr+(CyMCOR1<<index), CyCTS);
3275                 }
3276                                         /* act on 0->1 modem transitions */
3277                 cy_writeb(base_addr+(CyMCOR2<<index), CyCTS);
3278             } else {
3279                 /* without modem intr */
3280                 cy_writeb(base_addr+(CySRER<<index),
3281                    cy_readb(base_addr+(CySRER<<index)) | CyMdmCh); 
3282                                         /* act on 1->0 modem transitions */
3283                 if ((cflag & CRTSCTS) && info->rflow) {
3284                         cy_writeb(base_addr+(CyMCOR1<<index), 
3285                                   (CyDSR|CyCTS|CyRI|CyDCD|rflow_thr[i]));
3286                 } else {
3287                         cy_writeb(base_addr+(CyMCOR1<<index), 
3288                                   CyDSR|CyCTS|CyRI|CyDCD);
3289                 }
3290                                         /* act on 0->1 modem transitions */
3291                 cy_writeb(base_addr+(CyMCOR2<<index), 
3292                           CyDSR|CyCTS|CyRI|CyDCD);
3293             }
3294
3295             if(i == 0){ /* baud rate is zero, turn off line */
3296                 if (info->rtsdtr_inv) {
3297                         cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
3298                 } else {
3299                         cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
3300                 }
3301 #ifdef CY_DEBUG_DTR
3302                 printk("cyc:set_line_char dropping DTR\n");
3303                 printk("     status: 0x%x, 0x%x\n", 
3304                     cy_readb(base_addr+(CyMSVR1<<index)),
3305                     cy_readb(base_addr+(CyMSVR2<<index)));
3306 #endif
3307             }else{
3308                 if (info->rtsdtr_inv) {
3309                         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
3310                 } else {
3311                         cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
3312                 }
3313 #ifdef CY_DEBUG_DTR
3314                 printk("cyc:set_line_char raising DTR\n");
3315                 printk("     status: 0x%x, 0x%x\n",
3316                     cy_readb(base_addr+(CyMSVR1<<index)),
3317                     cy_readb(base_addr+(CyMSVR2<<index)));
3318 #endif
3319             }
3320
3321             if (info->tty){
3322                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
3323             }
3324         CY_UNLOCK(info, flags);
3325
3326     } else {
3327       struct FIRM_ID __iomem *firm_id;
3328       struct ZFW_CTRL __iomem *zfw_ctrl;
3329       struct BOARD_CTRL __iomem *board_ctrl;
3330       struct CH_CTRL __iomem *ch_ctrl;
3331       struct BUF_CTRL __iomem *buf_ctrl;
3332       uclong sw_flow;
3333       int retval;
3334
3335         firm_id = cy_card[card].base_addr + ID_ADDRESS;
3336         if (!ISZLOADED(cy_card[card])) {
3337             return;
3338         }
3339
3340         zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
3341         board_ctrl = &zfw_ctrl->board_ctrl;
3342         ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
3343         buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
3344
3345         /* baud rate */
3346         baud = tty_get_baud_rate(info->tty);
3347         if ((baud == 38400) &&
3348             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3349             if (info->custom_divisor)
3350                 baud_rate = info->baud / info->custom_divisor;
3351             else
3352                 baud_rate = info->baud;
3353         } else if (baud > CYZ_MAX_SPEED) {
3354             baud = CYZ_MAX_SPEED;
3355         }
3356         cy_writel(&ch_ctrl->comm_baud , baud);
3357
3358         if (baud == 134) {
3359             /* get it right for 134.5 baud */
3360             info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
3361         } else if ((baud == 38400) &&
3362                    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
3363             info->timeout = (info->xmit_fifo_size*HZ*15/baud_rate) + 2;
3364         } else if (baud) {
3365             info->timeout = (info->xmit_fifo_size*HZ*15/baud) + 2;
3366             /* this needs to be propagated into the card info */
3367         } else {
3368             info->timeout = 0;
3369         }
3370
3371         /* byte size and parity */
3372         switch(cflag & CSIZE){
3373         case CS5: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS5); break;
3374         case CS6: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS6); break;
3375         case CS7: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS7); break;
3376         case CS8: cy_writel(&ch_ctrl->comm_data_l , C_DL_CS8); break;
3377         }
3378         if(cflag & CSTOPB){
3379             cy_writel(&ch_ctrl->comm_data_l,
3380                cy_readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
3381         }else{
3382             cy_writel(&ch_ctrl->comm_data_l,
3383                cy_readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
3384         }
3385         if (cflag & PARENB){
3386             if (cflag & PARODD){
3387                 cy_writel(&ch_ctrl->comm_parity , C_PR_ODD);
3388             }else{
3389                 cy_writel(&ch_ctrl->comm_parity , C_PR_EVEN);
3390             }
3391         }else{
3392             cy_writel(&ch_ctrl->comm_parity , C_PR_NONE);
3393         }
3394
3395         /* CTS flow control flag */
3396         if (cflag & CRTSCTS){
3397             cy_writel(&ch_ctrl->hw_flow,
3398                cy_readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
3399         }else{
3400             cy_writel(&ch_ctrl->hw_flow,
3401                cy_readl(&ch_ctrl->hw_flow) & ~(C_RS_CTS | C_RS_RTS));
3402         }
3403         /* As the HW flow control is done in firmware, the driver doesn't
3404            need to care about it */
3405         info->flags &= ~ASYNC_CTS_FLOW;
3406
3407         /* XON/XOFF/XANY flow control flags */
3408         sw_flow = 0;
3409         if (iflag & IXON){
3410             sw_flow |= C_FL_OXX;
3411             if (iflag & IXANY)
3412                 sw_flow |= C_FL_OIXANY;
3413         }
3414         cy_writel(&ch_ctrl->sw_flow, sw_flow);
3415
3416         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_IOCTL, 0L);
3417         if (retval != 0){
3418             printk("cyc:set_line_char retval on ttyC%d was %x\n",
3419                    info->line, retval);
3420         }
3421
3422         /* CD sensitivity */
3423         if (cflag & CLOCAL){
3424             info->flags &= ~ASYNC_CHECK_CD;
3425         }else{
3426             info->flags |= ASYNC_CHECK_CD;
3427         }
3428
3429         if(baud == 0){ /* baud rate is zero, turn off line */
3430             cy_writel(&ch_ctrl->rs_control,
3431                cy_readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
3432 #ifdef CY_DEBUG_DTR
3433             printk("cyc:set_line_char dropping Z DTR\n");
3434 #endif
3435         }else{
3436             cy_writel(&ch_ctrl->rs_control,
3437                cy_readl(&ch_ctrl->rs_control) | C_RS_DTR);
3438 #ifdef CY_DEBUG_DTR
3439             printk("cyc:set_line_char raising Z DTR\n");
3440 #endif
3441         }
3442
3443         retval = cyz_issue_cmd( &cy_card[card], channel, C_CM_IOCTLM, 0L);
3444         if (retval != 0){
3445             printk("cyc:set_line_char(2) retval on ttyC%d was %x\n",
3446                    info->line, retval);
3447         }
3448
3449         if (info->tty){
3450             clear_bit(TTY_IO_ERROR, &info->tty->flags);
3451         }
3452     }
3453 } /* set_line_char */
3454
3455
3456 static int
3457 get_serial_info(struct cyclades_port * info,
3458                            struct serial_struct __user * retinfo)
3459 {
3460   struct serial_struct tmp;
3461   struct cyclades_card *cinfo = &cy_card[info->card];
3462
3463     if (!retinfo)
3464             return -EFAULT;
3465     memset(&tmp, 0, sizeof(tmp));
3466     tmp.type = info->type;
3467     tmp.line = info->line;
3468     tmp.port = info->card * 0x100 + info->line - cinfo->first_line;
3469     tmp.irq = cinfo->irq;
3470     tmp.flags = info->flags;
3471     tmp.close_delay = info->close_delay;
3472     tmp.baud_base = info->baud;
3473     tmp.custom_divisor = info->custom_divisor;
3474     tmp.hub6 = 0;               /*!!!*/
3475     return copy_to_user(retinfo,&tmp,sizeof(*retinfo))?-EFAULT:0;
3476 } /* get_serial_info */
3477
3478
3479 static int
3480 set_serial_info(struct cyclades_port * info,
3481                            struct serial_struct __user * new_info)
3482 {
3483   struct serial_struct new_serial;
3484   struct cyclades_port old_info;
3485
3486     if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
3487         return -EFAULT;
3488     old_info = *info;
3489
3490     if (!capable(CAP_SYS_ADMIN)) {
3491             if ((new_serial.close_delay != info->close_delay) ||
3492                 (new_serial.baud_base != info->baud) ||
3493                 ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
3494                  (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
3495                     return -EPERM;
3496             info->flags = ((info->flags & ~ASYNC_USR_MASK) |
3497                            (new_serial.flags & ASYNC_USR_MASK));
3498             info->baud = new_serial.baud_base;
3499             info->custom_divisor = new_serial.custom_divisor;
3500             goto check_and_exit;
3501     }
3502
3503
3504     /*
3505      * OK, past this point, all the error checking has been done.
3506      * At this point, we start making changes.....
3507      */
3508
3509     info->baud = new_serial.baud_base;
3510     info->custom_divisor = new_serial.custom_divisor;
3511     info->flags = ((info->flags & ~ASYNC_FLAGS) |
3512                     (new_serial.flags & ASYNC_FLAGS));
3513     info->close_delay = new_serial.close_delay * HZ/100;
3514     info->closing_wait = new_serial.closing_wait * HZ/100;
3515
3516 check_and_exit:
3517     if (info->flags & ASYNC_INITIALIZED){
3518         set_line_char(info);
3519         return 0;
3520     }else{
3521         return startup(info);
3522     }
3523 } /* set_serial_info */
3524
3525 /*
3526  * get_lsr_info - get line status register info
3527  *
3528  * Purpose: Let user call ioctl() to get info when the UART physically
3529  *          is emptied.  On bus types like RS485, the transmitter must
3530  *          release the bus after transmitting. This must be done when
3531  *          the transmit shift register is empty, not be done when the
3532  *          transmit holding register is empty.  This functionality
3533  *          allows an RS485 driver to be written in user space.
3534  */
3535 static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
3536 {
3537     int card, chip, channel, index;
3538     unsigned char status;
3539     unsigned int result;
3540     unsigned long flags;
3541     void __iomem *base_addr;
3542
3543     card = info->card;
3544     channel = (info->line) - (cy_card[card].first_line);
3545     if (!IS_CYC_Z(cy_card[card])) {
3546         chip = channel>>2;
3547         channel &= 0x03;
3548         index = cy_card[card].bus_index;
3549         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3550
3551         CY_LOCK(info, flags);
3552         status = cy_readb(base_addr+(CySRER<<index)) & (CyTxRdy|CyTxMpty);
3553         CY_UNLOCK(info, flags);
3554         result = (status ? 0 : TIOCSER_TEMT);
3555     } else {
3556         /* Not supported yet */
3557         return -EINVAL;
3558     }
3559     return put_user(result, (unsigned long __user *) value);
3560 }
3561
3562 static int
3563 cy_tiocmget(struct tty_struct *tty, struct file *file)
3564 {
3565   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3566   int card,chip,channel,index;
3567   void __iomem *base_addr;
3568   unsigned long flags;
3569   unsigned char status;
3570   unsigned long lstatus;
3571   unsigned int result;
3572   struct FIRM_ID __iomem *firm_id;
3573   struct ZFW_CTRL __iomem *zfw_ctrl;
3574   struct BOARD_CTRL __iomem *board_ctrl;
3575   struct CH_CTRL __iomem *ch_ctrl;
3576
3577     if (serial_paranoia_check(info, tty->name, __FUNCTION__))
3578         return -ENODEV;
3579
3580     card = info->card;
3581     channel = (info->line) - (cy_card[card].first_line);
3582     if (!IS_CYC_Z(cy_card[card])) {
3583         chip = channel>>2;
3584         channel &= 0x03;
3585         index = cy_card[card].bus_index;
3586         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3587
3588         CY_LOCK(info, flags);
3589             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3590             status = cy_readb(base_addr+(CyMSVR1<<index));
3591             status |= cy_readb(base_addr+(CyMSVR2<<index));
3592         CY_UNLOCK(info, flags);
3593
3594         if (info->rtsdtr_inv) {
3595             result =  ((status  & CyRTS) ? TIOCM_DTR : 0)
3596                     | ((status  & CyDTR) ? TIOCM_RTS : 0);
3597         } else {
3598             result =  ((status  & CyRTS) ? TIOCM_RTS : 0)
3599                     | ((status  & CyDTR) ? TIOCM_DTR : 0);
3600         }
3601         result |=  ((status  & CyDCD) ? TIOCM_CAR : 0)
3602                  | ((status  & CyRI) ? TIOCM_RNG : 0)
3603                  | ((status  & CyDSR) ? TIOCM_DSR : 0)
3604                  | ((status  & CyCTS) ? TIOCM_CTS : 0);
3605     } else {
3606         base_addr = cy_card[card].base_addr;
3607
3608         if (cy_card[card].num_chips != -1){
3609             return -EINVAL;
3610         }
3611
3612         firm_id = cy_card[card].base_addr + ID_ADDRESS;
3613         if (ISZLOADED(cy_card[card])) {
3614             zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
3615             board_ctrl = &zfw_ctrl->board_ctrl;
3616             ch_ctrl = zfw_ctrl->ch_ctrl;
3617             lstatus = cy_readl(&ch_ctrl[channel].rs_status);
3618             result =  ((lstatus  & C_RS_RTS) ? TIOCM_RTS : 0)
3619                     | ((lstatus  & C_RS_DTR) ? TIOCM_DTR : 0)
3620                     | ((lstatus  & C_RS_DCD) ? TIOCM_CAR : 0)
3621                     | ((lstatus  & C_RS_RI) ? TIOCM_RNG : 0)
3622                     | ((lstatus  & C_RS_DSR) ? TIOCM_DSR : 0)
3623                     | ((lstatus  & C_RS_CTS) ? TIOCM_CTS : 0);
3624         }else{
3625             result = 0;
3626             return -ENODEV;
3627         }
3628
3629     }
3630     return result;
3631 } /* cy_tiomget */
3632
3633
3634 static int
3635 cy_tiocmset(struct tty_struct *tty, struct file *file,
3636             unsigned int set, unsigned int clear)
3637 {
3638   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3639   int card,chip,channel,index;
3640   void __iomem *base_addr;
3641   unsigned long flags;
3642   struct FIRM_ID __iomem *firm_id;
3643   struct ZFW_CTRL __iomem *zfw_ctrl;
3644   struct BOARD_CTRL __iomem *board_ctrl;
3645   struct CH_CTRL __iomem *ch_ctrl;
3646   int retval;
3647
3648     if (serial_paranoia_check(info, tty->name, __FUNCTION__))
3649         return -ENODEV;
3650
3651     card = info->card;
3652     channel = (info->line) - (cy_card[card].first_line);
3653     if (!IS_CYC_Z(cy_card[card])) {
3654         chip = channel>>2;
3655         channel &= 0x03;
3656         index = cy_card[card].bus_index;
3657         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3658
3659         if (set & TIOCM_RTS){
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+(CyMSVR2<<index), CyDTR);
3664                 } else {
3665                         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
3666                 }
3667                 CY_UNLOCK(info, flags);
3668         }
3669         if (clear & TIOCM_RTS) {
3670                 CY_LOCK(info, flags);
3671                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3672                 if (info->rtsdtr_inv) {
3673                         cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
3674                 } else {
3675                         cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
3676                 }
3677                 CY_UNLOCK(info, flags);
3678         }
3679         if (set & TIOCM_DTR){
3680                 CY_LOCK(info, flags);
3681                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3682                 if (info->rtsdtr_inv) {
3683                         cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
3684                 } else {
3685                         cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
3686                 }
3687 #ifdef CY_DEBUG_DTR
3688                 printk("cyc:set_modem_info raising DTR\n");
3689                 printk("     status: 0x%x, 0x%x\n",
3690                     cy_readb(base_addr+(CyMSVR1<<index)), 
3691                     cy_readb(base_addr+(CyMSVR2<<index)));
3692 #endif
3693                 CY_UNLOCK(info, flags);
3694         }
3695         if (clear & TIOCM_DTR) {
3696                 CY_LOCK(info, flags);
3697                 cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
3698                 if (info->rtsdtr_inv) {
3699                         cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
3700                 } else {
3701                         cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
3702                 }
3703
3704 #ifdef CY_DEBUG_DTR
3705                 printk("cyc:set_modem_info dropping DTR\n");
3706                 printk("     status: 0x%x, 0x%x\n",
3707                     cy_readb(base_addr+(CyMSVR1<<index)), 
3708                     cy_readb(base_addr+(CyMSVR2<<index)));
3709 #endif
3710                 CY_UNLOCK(info, flags);
3711         }
3712     } else {
3713         base_addr = cy_card[card].base_addr;
3714
3715         firm_id = cy_card[card].base_addr + ID_ADDRESS;
3716         if (ISZLOADED(cy_card[card])) {
3717             zfw_ctrl = cy_card[card].base_addr + (cy_readl(&firm_id->zfwctrl_addr) & 0xfffff);
3718             board_ctrl = &zfw_ctrl->board_ctrl;
3719             ch_ctrl = zfw_ctrl->ch_ctrl;
3720
3721             if (set & TIOCM_RTS){
3722                     CY_LOCK(info, flags);
3723                     cy_writel(&ch_ctrl[channel].rs_control,
3724                        cy_readl(&ch_ctrl[channel].rs_control) | C_RS_RTS);
3725                     CY_UNLOCK(info, flags);
3726             }
3727             if (clear & TIOCM_RTS) {
3728                     CY_LOCK(info, flags);
3729                     cy_writel(&ch_ctrl[channel].rs_control,
3730                        cy_readl(&ch_ctrl[channel].rs_control) & ~C_RS_RTS);
3731                     CY_UNLOCK(info, flags);
3732             }
3733             if (set & TIOCM_DTR){
3734                     CY_LOCK(info, flags);
3735                     cy_writel(&ch_ctrl[channel].rs_control,
3736                        cy_readl(&ch_ctrl[channel].rs_control) | C_RS_DTR);
3737 #ifdef CY_DEBUG_DTR
3738                     printk("cyc:set_modem_info raising Z DTR\n");
3739 #endif
3740                     CY_UNLOCK(info, flags);
3741             }
3742             if (clear & TIOCM_DTR) {
3743                     CY_LOCK(info, flags);
3744                     cy_writel(&ch_ctrl[channel].rs_control,
3745                        cy_readl(&ch_ctrl[channel].rs_control) & ~C_RS_DTR);
3746 #ifdef CY_DEBUG_DTR
3747                     printk("cyc:set_modem_info clearing Z DTR\n");
3748 #endif
3749                     CY_UNLOCK(info, flags);
3750             }
3751         }else{
3752             return -ENODEV;
3753         }
3754         CY_LOCK(info, flags);
3755         retval = cyz_issue_cmd(&cy_card[info->card],
3756                                     channel, C_CM_IOCTLM,0L);
3757         if (retval != 0){
3758             printk("cyc:set_modem_info retval on ttyC%d was %x\n",
3759                    info->line, retval);
3760         }
3761         CY_UNLOCK(info, flags);
3762     }
3763     return 0;
3764 } /* cy_tiocmset */
3765
3766 /*
3767  * cy_break() --- routine which turns the break handling on or off
3768  */
3769 static void
3770 cy_break(struct tty_struct *tty, int break_state)
3771 {
3772     struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3773     unsigned long flags;
3774
3775     if (serial_paranoia_check(info, tty->name, "cy_break"))
3776         return;
3777
3778     CY_LOCK(info, flags);
3779     if (!IS_CYC_Z(cy_card[info->card])) {
3780         /* Let the transmit ISR take care of this (since it
3781            requires stuffing characters into the output stream).
3782         */
3783         if (break_state == -1) {
3784             if (!info->breakon) {
3785                 info->breakon = 1;
3786                 if (!info->xmit_cnt) {
3787                     CY_UNLOCK(info, flags);
3788                     start_xmit(info);
3789                     CY_LOCK(info, flags);
3790                 }
3791             }
3792         } else {
3793             if (!info->breakoff) {
3794                 info->breakoff = 1;
3795                 if (!info->xmit_cnt) {
3796                     CY_UNLOCK(info, flags);
3797                     start_xmit(info);
3798                     CY_LOCK(info, flags);
3799                 }
3800             }
3801         }
3802     } else {
3803         int retval;
3804
3805         if (break_state == -1) {
3806             retval = cyz_issue_cmd(&cy_card[info->card],
3807                 (info->line) - (cy_card[info->card].first_line),
3808                 C_CM_SET_BREAK, 0L);
3809             if (retval != 0) {
3810                 printk("cyc:cy_break (set) retval on ttyC%d was %x\n",
3811                        info->line, retval);
3812             }
3813         } else {
3814             retval = cyz_issue_cmd(&cy_card[info->card],
3815                 (info->line) - (cy_card[info->card].first_line),
3816                 C_CM_CLR_BREAK, 0L);
3817             if (retval != 0) {
3818                 printk("cyc:cy_break (clr) retval on ttyC%d was %x\n",
3819                        info->line, retval);
3820             }
3821         }
3822     }
3823     CY_UNLOCK(info, flags);
3824 } /* cy_break */
3825
3826 static int
3827 get_mon_info(struct cyclades_port * info, struct cyclades_monitor __user * mon)
3828 {
3829
3830     if(copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
3831         return -EFAULT;
3832     info->mon.int_count  = 0;
3833     info->mon.char_count = 0;
3834     info->mon.char_max   = 0;
3835     info->mon.char_last  = 0;
3836     return 0;
3837 }/* get_mon_info */
3838
3839
3840 static int
3841 set_threshold(struct cyclades_port * info, unsigned long value)
3842 {
3843   void __iomem *base_addr;
3844   int card,channel,chip,index;
3845   unsigned long flags;
3846    
3847     card = info->card;
3848     channel = info->line - cy_card[card].first_line;
3849     if (!IS_CYC_Z(cy_card[card])) {
3850         chip = channel>>2;
3851         channel &= 0x03;
3852         index = cy_card[card].bus_index;
3853         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3854
3855         info->cor3 &= ~CyREC_FIFO;
3856         info->cor3 |= value & CyREC_FIFO;
3857
3858         CY_LOCK(info, flags);
3859             cy_writeb(base_addr+(CyCOR3<<index), info->cor3);
3860             cyy_issue_cmd(base_addr,CyCOR_CHANGE|CyCOR3ch,index);
3861         CY_UNLOCK(info, flags);
3862     } else {
3863         // Nothing to do!
3864     }
3865     return 0;
3866 }/* set_threshold */
3867
3868
3869 static int
3870 get_threshold(struct cyclades_port * info, unsigned long __user *value)
3871 {
3872   void __iomem *base_addr;
3873   int card,channel,chip,index;
3874   unsigned long tmp;
3875    
3876     card = info->card;
3877     channel = info->line - cy_card[card].first_line;
3878     if (!IS_CYC_Z(cy_card[card])) {
3879         chip = channel>>2;
3880         channel &= 0x03;
3881         index = cy_card[card].bus_index;
3882         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3883
3884         tmp = cy_readb(base_addr+(CyCOR3<<index)) & CyREC_FIFO;
3885         return put_user(tmp,value);
3886     } else {
3887         // Nothing to do!
3888         return 0;
3889     }
3890 }/* get_threshold */
3891
3892
3893 static int
3894 set_default_threshold(struct cyclades_port * info, unsigned long value)
3895 {
3896     info->default_threshold = value & 0x0f;
3897     return 0;
3898 }/* set_default_threshold */
3899
3900
3901 static int
3902 get_default_threshold(struct cyclades_port * info, unsigned long __user *value)
3903 {
3904     return put_user(info->default_threshold,value);
3905 }/* get_default_threshold */
3906
3907
3908 static int
3909 set_timeout(struct cyclades_port * info, unsigned long value)
3910 {
3911   void __iomem *base_addr;
3912   int card,channel,chip,index;
3913   unsigned long flags;
3914    
3915     card = info->card;
3916     channel = info->line - cy_card[card].first_line;
3917     if (!IS_CYC_Z(cy_card[card])) {
3918         chip = channel>>2;
3919         channel &= 0x03;
3920         index = cy_card[card].bus_index;
3921         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3922
3923         CY_LOCK(info, flags);
3924             cy_writeb(base_addr+(CyRTPR<<index), value & 0xff);
3925         CY_UNLOCK(info, flags);
3926     } else {
3927         // Nothing to do!
3928     }
3929     return 0;
3930 }/* set_timeout */
3931
3932
3933 static int
3934 get_timeout(struct cyclades_port * info, unsigned long __user *value)
3935 {
3936   void __iomem *base_addr;
3937   int card,channel,chip,index;
3938   unsigned long tmp;
3939    
3940     card = info->card;
3941     channel = info->line - cy_card[card].first_line;
3942     if (!IS_CYC_Z(cy_card[card])) {
3943         chip = channel>>2;
3944         channel &= 0x03;
3945         index = cy_card[card].bus_index;
3946         base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
3947
3948         tmp = cy_readb(base_addr+(CyRTPR<<index));
3949         return put_user(tmp,value);
3950     } else {
3951         // Nothing to do!
3952         return 0;
3953     }
3954 }/* get_timeout */
3955
3956
3957 static int
3958 set_default_timeout(struct cyclades_port * info, unsigned long value)
3959 {
3960     info->default_timeout = value & 0xff;
3961     return 0;
3962 }/* set_default_timeout */
3963
3964
3965 static int
3966 get_default_timeout(struct cyclades_port * info, unsigned long __user *value)
3967 {
3968     return put_user(info->default_timeout,value);
3969 }/* get_default_timeout */
3970
3971 /*
3972  * This routine allows the tty driver to implement device-
3973  * specific ioctl's.  If the ioctl number passed in cmd is
3974  * not recognized by the driver, it should return ENOIOCTLCMD.
3975  */
3976 static int
3977 cy_ioctl(struct tty_struct *tty, struct file * file,
3978             unsigned int cmd, unsigned long arg)
3979 {
3980   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
3981   struct cyclades_icount cprev, cnow;           /* kernel counter temps */
3982   struct serial_icounter_struct __user *p_cuser;        /* user space */
3983   int ret_val = 0;
3984   unsigned long flags;
3985   void __user *argp = (void __user *)arg;
3986
3987     if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
3988         return -ENODEV;
3989
3990 #ifdef CY_DEBUG_OTHER
3991     printk("cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
3992         info->line, cmd, arg); /* */
3993 #endif
3994
3995     switch (cmd) {
3996         case CYGETMON:
3997             ret_val = get_mon_info(info, argp);
3998             break;
3999         case CYGETTHRESH:
4000             ret_val = get_threshold(info, argp);
4001             break;
4002         case CYSETTHRESH:
4003             ret_val = set_threshold(info, arg);
4004             break;
4005         case CYGETDEFTHRESH:
4006             ret_val = get_default_threshold(info, argp);
4007             break;
4008         case CYSETDEFTHRESH:
4009             ret_val = set_default_threshold(info, arg);
4010             break;
4011         case CYGETTIMEOUT:
4012             ret_val = get_timeout(info, argp);
4013             break;
4014         case CYSETTIMEOUT:
4015             ret_val = set_timeout(info, arg);
4016             break;
4017         case CYGETDEFTIMEOUT:
4018             ret_val = get_default_timeout(info, argp);
4019             break;
4020         case CYSETDEFTIMEOUT:
4021             ret_val = set_default_timeout(info, arg);
4022             break;
4023         case CYSETRFLOW:
4024             info->rflow = (int)arg;
4025             ret_val = 0;
4026             break;
4027         case CYGETRFLOW:
4028             ret_val = info->rflow;
4029             break;
4030         case CYSETRTSDTR_INV:
4031             info->rtsdtr_inv = (int)arg;
4032             ret_val = 0;
4033             break;
4034         case CYGETRTSDTR_INV:
4035             ret_val = info->rtsdtr_inv;
4036             break;
4037         case CYGETCARDINFO:
4038             if (copy_to_user(argp, &cy_card[info->card], 
4039                         sizeof (struct cyclades_card))) {
4040                 ret_val = -EFAULT;
4041                 break;
4042             }
4043             ret_val = 0;
4044             break;
4045         case CYGETCD1400VER:
4046             ret_val = info->chip_rev;
4047             break;
4048 #ifndef CONFIG_CYZ_INTR
4049         case CYZSETPOLLCYCLE:
4050             cyz_polling_cycle = (arg * HZ) / 1000;
4051             ret_val = 0;
4052             break;
4053         case CYZGETPOLLCYCLE:
4054             ret_val = (cyz_polling_cycle * 1000) / HZ;
4055             break;
4056 #endif /* CONFIG_CYZ_INTR */
4057         case CYSETWAIT:
4058             info->closing_wait = (unsigned short)arg * HZ/100;
4059             ret_val = 0;
4060             break;
4061         case CYGETWAIT:
4062             ret_val = info->closing_wait / (HZ/100);
4063             break;
4064         case TIOCGSERIAL:
4065             ret_val = get_serial_info(info, argp);
4066             break;
4067         case TIOCSSERIAL:
4068             ret_val = set_serial_info(info, argp);
4069             break;
4070         case TIOCSERGETLSR: /* Get line status register */
4071             ret_val = get_lsr_info(info, argp);
4072             break;
4073         /*
4074          * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 
4075          * - mask passed in arg for lines of interest
4076          *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
4077          * Caller should use TIOCGICOUNT to see which one it was
4078          */
4079         case TIOCMIWAIT:
4080             CY_LOCK(info, flags);
4081             /* note the counters on entry */
4082             cprev = info->icount;
4083             CY_UNLOCK(info, flags);
4084             while (1) {
4085                 interruptible_sleep_on(&info->delta_msr_wait);
4086                 /* see if a signal did it */
4087                 if (signal_pending(current)) {
4088                     return -ERESTARTSYS;
4089                 }
4090
4091                 CY_LOCK(info, flags);
4092                 cnow = info->icount; /* atomic copy */
4093                 CY_UNLOCK(info, flags);
4094
4095                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
4096                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
4097                     return -EIO; /* no change => error */
4098                 }
4099                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 
4100                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 
4101                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) || 
4102                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
4103                     return 0;
4104                 }
4105                 cprev = cnow;
4106             }
4107             /* NOTREACHED */
4108
4109         /*
4110          * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
4111          * Return: write counters to the user passed counter struct
4112          * NB: both 1->0 and 0->1 transitions are counted except for
4113          *     RI where only 0->1 is counted.
4114          */
4115         case TIOCGICOUNT:
4116             CY_LOCK(info, flags);
4117             cnow = info->icount;
4118             CY_UNLOCK(info, flags);
4119             p_cuser = argp;
4120             ret_val = put_user(cnow.cts, &p_cuser->cts);
4121             if (ret_val) return ret_val;
4122             ret_val = put_user(cnow.dsr, &p_cuser->dsr);
4123             if (ret_val) return ret_val;
4124             ret_val = put_user(cnow.rng, &p_cuser->rng);
4125             if (ret_val) return ret_val;
4126             ret_val = put_user(cnow.dcd, &p_cuser->dcd);
4127             if (ret_val) return ret_val;
4128             ret_val = put_user(cnow.rx, &p_cuser->rx);
4129             if (ret_val) return ret_val;
4130             ret_val = put_user(cnow.tx, &p_cuser->tx);
4131             if (ret_val) return ret_val;
4132             ret_val = put_user(cnow.frame, &p_cuser->frame);
4133             if (ret_val) return ret_val;
4134             ret_val = put_user(cnow.overrun, &p_cuser->overrun);
4135             if (ret_val) return ret_val;
4136             ret_val = put_user(cnow.parity, &p_cuser->parity);
4137             if (ret_val) return ret_val;
4138             ret_val = put_user(cnow.brk, &p_cuser->brk);
4139             if (ret_val) return ret_val;
4140             ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
4141             if (ret_val) return ret_val;
4142             ret_val = 0;
4143             break;
4144         default:
4145             ret_val = -ENOIOCTLCMD;
4146     }
4147
4148 #ifdef CY_DEBUG_OTHER
4149     printk(" cyc:cy_ioctl done\n");
4150 #endif
4151
4152     return ret_val;
4153 } /* cy_ioctl */
4154
4155
4156 /*
4157  * This routine allows the tty driver to be notified when
4158  * device's termios settings have changed.  Note that a
4159  * well-designed tty driver should be prepared to accept the case
4160  * where old == NULL, and try to do something rational.
4161  */
4162 static void
4163 cy_set_termios(struct tty_struct *tty, struct termios * old_termios)
4164 {
4165   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4166
4167 #ifdef CY_DEBUG_OTHER
4168     printk("cyc:cy_set_termios ttyC%d\n", info->line);
4169 #endif
4170
4171     if ((tty->termios->c_cflag == old_termios->c_cflag) &&
4172         ((tty->termios->c_iflag & (IXON|IXANY)) == 
4173          (old_termios->c_iflag & (IXON|IXANY))))
4174         return;
4175     set_line_char(info);
4176
4177     if ((old_termios->c_cflag & CRTSCTS) &&
4178         !(tty->termios->c_cflag & CRTSCTS)) {
4179             tty->hw_stopped = 0;
4180             cy_start(tty);
4181     }
4182 #if 0
4183     /*
4184      * No need to wake up processes in open wait, since they
4185      * sample the CLOCAL flag once, and don't recheck it.
4186      * XXX  It's not clear whether the current behavior is correct
4187      * or not.  Hence, this may change.....
4188      */
4189     if (!(old_termios->c_cflag & CLOCAL) &&
4190         (tty->termios->c_cflag & CLOCAL))
4191             wake_up_interruptible(&info->open_wait);
4192 #endif
4193
4194     return;
4195 } /* cy_set_termios */
4196
4197 /* This function is used to send a high-priority XON/XOFF character to
4198    the device.
4199 */
4200 static void
4201 cy_send_xchar (struct tty_struct *tty, char ch)
4202 {
4203         struct cyclades_port *info = (struct cyclades_port *) tty->driver_data;
4204         int card, channel;
4205
4206         if (serial_paranoia_check (info, tty->name, "cy_send_xchar"))
4207                 return;
4208
4209         info->x_char = ch;
4210
4211         if (ch)
4212                 cy_start (tty);
4213
4214         card = info->card;
4215         channel = info->line - cy_card[card].first_line;
4216
4217         if (IS_CYC_Z (cy_card[card])) {
4218                 if (ch == STOP_CHAR (tty))
4219                         cyz_issue_cmd (&cy_card[card], channel, C_CM_SENDXOFF, 0L);
4220                 else if (ch == START_CHAR (tty))
4221                         cyz_issue_cmd (&cy_card[card], channel, C_CM_SENDXON, 0L);
4222         }
4223 }
4224
4225 /* This routine is called by the upper-layer tty layer to signal
4226    that incoming characters should be throttled because the input
4227    buffers are close to full.
4228  */
4229 static void
4230 cy_throttle(struct tty_struct * tty)
4231 {
4232   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4233   unsigned long flags;
4234   void __iomem *base_addr;
4235   int card,chip,channel,index;
4236
4237 #ifdef CY_DEBUG_THROTTLE
4238   char buf[64];
4239
4240     printk("cyc:throttle %s: %d....ttyC%d\n", 
4241            tty_name(tty, buf),
4242            tty->ldisc.chars_in_buffer(tty), info->line);
4243 #endif
4244
4245     if (serial_paranoia_check(info, tty->name, "cy_throttle")){
4246             return;
4247     }
4248
4249     card = info->card;
4250
4251     if (I_IXOFF(tty)) {
4252         if (!IS_CYC_Z (cy_card[card]))
4253             cy_send_xchar (tty, STOP_CHAR (tty));
4254         else
4255             info->throttle = 1;
4256     }
4257
4258     if (tty->termios->c_cflag & CRTSCTS) {
4259         channel = info->line - cy_card[card].first_line;
4260         if (!IS_CYC_Z(cy_card[card])) {
4261             chip = channel>>2;
4262             channel &= 0x03;
4263             index = cy_card[card].bus_index;
4264             base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
4265
4266             CY_LOCK(info, flags);
4267             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
4268             if (info->rtsdtr_inv) {
4269                 cy_writeb(base_addr+(CyMSVR2<<index), ~CyDTR);
4270              } else {
4271                 cy_writeb(base_addr+(CyMSVR1<<index), ~CyRTS);
4272              }
4273             CY_UNLOCK(info, flags);
4274         } else {
4275             info->throttle = 1;
4276         }
4277     }
4278
4279     return;
4280 } /* cy_throttle */
4281
4282
4283 /*
4284  * This routine notifies the tty driver that it should signal
4285  * that characters can now be sent to the tty without fear of
4286  * overrunning the input buffers of the line disciplines.
4287  */
4288 static void
4289 cy_unthrottle(struct tty_struct * tty)
4290 {
4291   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4292   unsigned long flags;
4293   void __iomem *base_addr;
4294   int card,chip,channel,index;
4295
4296 #ifdef CY_DEBUG_THROTTLE
4297   char buf[64];
4298         
4299     printk("cyc:unthrottle %s: %d....ttyC%d\n", 
4300            tty_name(tty, buf),
4301            tty->ldisc.chars_in_buffer(tty), info->line);
4302 #endif
4303
4304     if (serial_paranoia_check(info, tty->name, "cy_unthrottle")){
4305             return;
4306     }
4307
4308     if (I_IXOFF(tty)) {
4309         if (info->x_char)
4310             info->x_char = 0;
4311         else
4312             cy_send_xchar (tty, START_CHAR (tty));
4313     }
4314
4315     if (tty->termios->c_cflag & CRTSCTS) {
4316         card = info->card;
4317         channel = info->line - cy_card[card].first_line;
4318         if (!IS_CYC_Z(cy_card[card])) {
4319             chip = channel>>2;
4320             channel &= 0x03;
4321             index = cy_card[card].bus_index;
4322             base_addr = cy_card[card].base_addr + (cy_chip_offset[chip]<<index);
4323
4324             CY_LOCK(info, flags);
4325             cy_writeb(base_addr+(CyCAR<<index), (u_char)channel);
4326             if (info->rtsdtr_inv) {
4327                     cy_writeb(base_addr+(CyMSVR2<<index), CyDTR);
4328             } else {
4329                     cy_writeb(base_addr+(CyMSVR1<<index), CyRTS);
4330             }
4331             CY_UNLOCK(info, flags);
4332         } else {
4333             info->throttle = 0;
4334         }
4335     }
4336
4337     return;
4338 } /* cy_unthrottle */
4339
4340
4341 /* cy_start and cy_stop provide software output flow control as a
4342    function of XON/XOFF, software CTS, and other such stuff.
4343 */
4344 static void
4345 cy_stop(struct tty_struct *tty)
4346 {
4347   struct cyclades_card *cinfo;
4348   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4349   void __iomem *base_addr;
4350   int chip,channel,index;
4351   unsigned long flags;
4352
4353 #ifdef CY_DEBUG_OTHER
4354     printk("cyc:cy_stop ttyC%d\n", info->line); /* */
4355 #endif
4356
4357     if (serial_paranoia_check(info, tty->name, "cy_stop"))
4358         return;
4359         
4360     cinfo = &cy_card[info->card];
4361     channel = info->line - cinfo->first_line;
4362     if (!IS_CYC_Z(*cinfo)) {
4363         index = cinfo->bus_index;
4364         chip = channel>>2;
4365         channel &= 0x03;
4366         base_addr = cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index);
4367
4368         CY_LOCK(info, flags);
4369             cy_writeb(base_addr+(CyCAR<<index),
4370                (u_char)(channel & 0x0003)); /* index channel */
4371             cy_writeb(base_addr+(CySRER<<index), 
4372                cy_readb(base_addr+(CySRER<<index)) & ~CyTxRdy);
4373         CY_UNLOCK(info, flags);
4374     } else {
4375         // Nothing to do!
4376     }
4377
4378     return;
4379 } /* cy_stop */
4380
4381
4382 static void
4383 cy_start(struct tty_struct *tty)
4384 {
4385   struct cyclades_card *cinfo;
4386   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4387   void __iomem *base_addr;
4388   int chip,channel,index;
4389   unsigned long flags;
4390
4391 #ifdef CY_DEBUG_OTHER
4392     printk("cyc:cy_start ttyC%d\n", info->line); /* */
4393 #endif
4394
4395     if (serial_paranoia_check(info, tty->name, "cy_start"))
4396         return;
4397         
4398     cinfo = &cy_card[info->card];
4399     channel = info->line - cinfo->first_line;
4400     index = cinfo->bus_index;
4401     if (!IS_CYC_Z(*cinfo)) {
4402         chip = channel>>2;
4403         channel &= 0x03;
4404         base_addr = cy_card[info->card].base_addr + (cy_chip_offset[chip]<<index);
4405
4406         CY_LOCK(info, flags);
4407             cy_writeb(base_addr+(CyCAR<<index),
4408                (u_char)(channel & 0x0003)); /* index channel */
4409             cy_writeb(base_addr+(CySRER<<index), 
4410                cy_readb(base_addr+(CySRER<<index)) | CyTxRdy);
4411         CY_UNLOCK(info, flags);
4412     } else {
4413         // Nothing to do!
4414     }
4415
4416     return;
4417 } /* cy_start */
4418
4419
4420 static void
4421 cy_flush_buffer(struct tty_struct *tty)
4422 {
4423   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
4424   int card, channel, retval;
4425   unsigned long flags;
4426                                 
4427 #ifdef CY_DEBUG_IO
4428     printk("cyc:cy_flush_buffer ttyC%d\n", info->line); /* */
4429 #endif
4430
4431     if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
4432         return;
4433
4434     card = info->card;
4435     channel = (info->line) - (cy_card[card].first_line);
4436
4437     CY_LOCK(info, flags);
4438     info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
4439     CY_UNLOCK(info, flags);
4440
4441     if (IS_CYC_Z(cy_card[card])) { /* If it is a Z card, flush the on-board 
4442                                       buffers as well */
4443         CY_LOCK(info, flags);
4444         retval = cyz_issue_cmd(&cy_card[card], channel, C_CM_FLUSH_TX, 0L);
4445         if (retval != 0) {
4446             printk("cyc: flush_buffer retval on ttyC%d was %x\n",
4447                    info->line, retval);
4448         }
4449         CY_UNLOCK(info, flags);
4450     }
4451     tty_wakeup(tty);
4452     wake_up_interruptible(&tty->write_wait);
4453 } /* cy_flush_buffer */
4454
4455
4456 /*
4457  * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
4458  */
4459 static void
4460 cy_hangup(struct tty_struct *tty)
4461 {
4462   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
4463         
4464 #ifdef CY_DEBUG_OTHER
4465     printk("cyc:cy_hangup ttyC%d\n", info->line); /* */
4466 #endif
4467
4468     if (serial_paranoia_check(info, tty->name, "cy_hangup"))
4469         return;
4470
4471     cy_flush_buffer(tty);
4472     shutdown(info);
4473     info->event = 0;
4474     info->count = 0;
4475 #ifdef CY_DEBUG_COUNT
4476     printk("cyc:cy_hangup (%d): setting count to 0\n", current->pid);
4477 #endif
4478     info->tty = NULL;
4479     info->flags &= ~ASYNC_NORMAL_ACTIVE;
4480     wake_up_interruptible(&info->open_wait);
4481 } /* cy_hangup */
4482
4483
4484 /*
4485  * ---------------------------------------------------------------------
4486  * cy_init() and friends
4487  *
4488  * cy_init() is called at boot-time to initialize the serial driver.
4489  * ---------------------------------------------------------------------
4490  */
4491
4492 /* initialize chips on Cyclom-Y card -- return number of valid
4493    chips (which is number of ports/4) */
4494 static unsigned short __init
4495 cyy_init_card(void __iomem *true_base_addr,int index)
4496 {
4497   unsigned int chip_number;
4498   void __iomem *base_addr;
4499
4500     cy_writeb(true_base_addr+(Cy_HwReset<<index), 0); 
4501                                                 /* Cy_HwReset is 0x1400 */
4502     cy_writeb(true_base_addr+(Cy_ClrIntr<<index), 0); 
4503                                                 /* Cy_ClrIntr is 0x1800 */
4504     udelay(500L);
4505
4506     for(chip_number=0; chip_number<CyMAX_CHIPS_PER_CARD; chip_number++){
4507         base_addr = true_base_addr + (cy_chip_offset[chip_number]<<index);
4508         mdelay(1);
4509         if(cy_readb(base_addr+(CyCCR<<index)) != 0x00){
4510             /*************
4511             printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
4512                chip_number, (unsigned long)base_addr);
4513             *************/
4514             return chip_number;
4515         }
4516
4517         cy_writeb(base_addr+(CyGFRCR<<index), 0);
4518         udelay(10L);
4519
4520         /* The Cyclom-16Y does not decode address bit 9 and therefore
4521            cannot distinguish between references to chip 0 and a non-
4522            existent chip 4.  If the preceding clearing of the supposed
4523            chip 4 GFRCR register appears at chip 0, there is no chip 4
4524            and this must be a Cyclom-16Y, not a Cyclom-32Ye.
4525         */
4526         if (chip_number == 4
4527         && cy_readb(true_base_addr
4528             + (cy_chip_offset[0]<<index)
4529             + (CyGFRCR<<index)) == 0){
4530             return chip_number;
4531         }
4532
4533         cy_writeb(base_addr+(CyCCR<<index), CyCHIP_RESET);
4534         mdelay(1);
4535
4536         if(cy_readb(base_addr+(CyGFRCR<<index)) == 0x00){
4537             /*
4538             printk(" chip #%d at %#6lx is not responding ",
4539                chip_number, (unsigned long)base_addr);
4540             printk("(GFRCR stayed 0)\n",
4541             */
4542             return chip_number;
4543         }
4544         if((0xf0 & (cy_readb(base_addr+(CyGFRCR<<index)))) != 0x40){
4545             /*
4546             printk(" chip #%d at %#6lx is not valid (GFRCR == %#2x)\n",
4547                chip_number, (unsigned long)base_addr,
4548                base_addr[CyGFRCR<<index]);
4549             */
4550             return chip_number;
4551         }
4552         cy_writeb(base_addr+(CyGCR<<index), CyCH0_SERIAL);
4553         if (cy_readb(base_addr+(CyGFRCR<<index)) >= CD1400_REV_J){
4554             /* It is a CD1400 rev. J or later */
4555             /* Impossible to reach 5ms with this chip. 
4556                Changed to 2ms instead (f = 500 Hz). */
4557             cy_writeb(base_addr+(CyPPR<<index), CyCLOCK_60_2MS);
4558         } else {
4559             /* f = 200 Hz */
4560             cy_writeb(base_addr+(CyPPR<<index), CyCLOCK_25_5MS);
4561         }
4562
4563     /*
4564         printk(" chip #%d at %#6lx is rev 0x%2x\n",
4565                chip_number, (unsigned long)base_addr,
4566                cy_readb(base_addr+(CyGFRCR<<index)));
4567     */
4568     }
4569     return chip_number;
4570 } /* cyy_init_card */
4571
4572 /*
4573  * ---------------------------------------------------------------------
4574  * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
4575  * sets global variables and return the number of ISA boards found.
4576  * ---------------------------------------------------------------------
4577  */
4578 static int __init
4579 cy_detect_isa(void)
4580 {
4581 #ifdef CONFIG_ISA
4582   unsigned short        cy_isa_irq,nboard;
4583   void __iomem          *cy_isa_address;
4584   unsigned short        i,j,cy_isa_nchan;
4585 #ifdef MODULE
4586   int isparam = 0;
4587 #endif
4588
4589         nboard = 0;
4590
4591 #ifdef MODULE
4592         /* Check for module parameters */
4593         for(i = 0 ; i < NR_CARDS; i++) {
4594             if (maddr[i] || i) {
4595                 isparam = 1;
4596                 cy_isa_addresses[i] = maddr[i];
4597             }
4598             if (!maddr[i])
4599                 break;
4600         }
4601 #endif
4602
4603         /* scan the address table probing for Cyclom-Y/ISA boards */
4604         for (i = 0 ; i < NR_ISA_ADDRS ; i++) {
4605                 unsigned int isa_address = cy_isa_addresses[i];
4606                 if (isa_address  == 0x0000) {
4607                         return(nboard);
4608                 }
4609
4610                 /* probe for CD1400... */
4611                 cy_isa_address = ioremap(isa_address, CyISA_Ywin);
4612                 cy_isa_nchan = CyPORTS_PER_CHIP * 
4613                      cyy_init_card(cy_isa_address,0);
4614                 if (cy_isa_nchan == 0) {
4615                         continue;
4616                 }
4617
4618 #ifdef MODULE
4619                 if (isparam && irq[i])
4620                     cy_isa_irq = irq[i];
4621                 else
4622 #endif
4623                 /* find out the board's irq by probing */
4624                 cy_isa_irq = detect_isa_irq(cy_isa_address);
4625                 if (cy_isa_irq == 0) {
4626                         printk("Cyclom-Y/ISA found at 0x%lx ",
4627                                 (unsigned long) cy_isa_address);
4628                         printk("but the IRQ could not be detected.\n");
4629                         continue;
4630                 }
4631
4632                 if((cy_next_channel+cy_isa_nchan) > NR_PORTS) {
4633                         printk("Cyclom-Y/ISA found at 0x%lx ",
4634                                 (unsigned long) cy_isa_address);
4635                         printk("but no more channels are available.\n");
4636                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
4637                         return(nboard);
4638                 }
4639                 /* fill the next cy_card structure available */
4640                 for (j = 0 ; j < NR_CARDS ; j++) {
4641                         if (cy_card[j].base_addr == 0)  break;
4642                 }
4643                 if (j == NR_CARDS) {    /* no more cy_cards available */
4644                         printk("Cyclom-Y/ISA found at 0x%lx ",
4645                                 (unsigned long) cy_isa_address);
4646                         printk("but no more cards can be used .\n");
4647                         printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4648                         return(nboard);
4649                 }
4650
4651                 /* allocate IRQ */
4652                 if(request_irq(cy_isa_irq, cyy_interrupt,
4653                                    SA_INTERRUPT, "Cyclom-Y", &cy_card[j]))
4654                 {
4655                         printk("Cyclom-Y/ISA found at 0x%lx ",
4656                                 (unsigned long) cy_isa_address);
4657                         printk("but could not allocate IRQ#%d.\n",
4658                                 cy_isa_irq);
4659                         return(nboard);
4660                 }
4661
4662                 /* set cy_card */
4663                 cy_card[j].base_addr = cy_isa_address;
4664                 cy_card[j].ctl_addr = NULL;
4665                 cy_card[j].irq = (int) cy_isa_irq;
4666                 cy_card[j].bus_index = 0;
4667                 cy_card[j].first_line = cy_next_channel;
4668                 cy_card[j].num_chips = cy_isa_nchan/4;
4669                 nboard++;
4670                         
4671                 /* print message */
4672                 printk("Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d, ",
4673                     j+1, (unsigned long) cy_isa_address,
4674                     (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
4675                     cy_isa_irq);
4676                 printk("%d channels starting from port %d.\n",
4677                         cy_isa_nchan, cy_next_channel);
4678                 cy_next_channel += cy_isa_nchan;
4679         }
4680         return(nboard);
4681 #else
4682         return(0);
4683 #endif /* CONFIG_ISA */
4684 } /* cy_detect_isa */
4685
4686 static void 
4687 plx_init(void __iomem *addr, uclong initctl)
4688 {
4689     /* Reset PLX */
4690     cy_writel(addr + initctl, cy_readl(addr + initctl) | 0x40000000);
4691     udelay(100L);
4692     cy_writel(addr + initctl, cy_readl(addr + initctl) & ~0x40000000);
4693
4694     /* Reload Config. Registers from EEPROM */
4695     cy_writel(addr + initctl, cy_readl(addr + initctl) | 0x20000000);
4696     udelay(100L);
4697     cy_writel(addr + initctl, cy_readl(addr + initctl) & ~0x20000000);
4698 }
4699
4700 /*
4701  * ---------------------------------------------------------------------
4702  * cy_detect_pci() - Test PCI bus presence and Cyclom-Ye/PCI.
4703  * sets global variables and return the number of PCI boards found.
4704  * ---------------------------------------------------------------------
4705  */
4706 static int __init
4707 cy_detect_pci(void)
4708 {
4709 #ifdef CONFIG_PCI
4710
4711   struct pci_dev        *pdev = NULL;
4712   unsigned char         cyy_rev_id;
4713   unsigned char         cy_pci_irq = 0;
4714   uclong                cy_pci_phys0, cy_pci_phys2;
4715   void __iomem          *cy_pci_addr0, *cy_pci_addr2;
4716   unsigned short        i,j,cy_pci_nchan, plx_ver;
4717   unsigned short        device_id,dev_index = 0;
4718   uclong                mailbox;
4719   uclong                ZeIndex = 0;
4720   void __iomem          *Ze_addr0[NR_CARDS], *Ze_addr2[NR_CARDS];
4721   uclong                Ze_phys0[NR_CARDS], Ze_phys2[NR_CARDS];
4722   unsigned char         Ze_irq[NR_CARDS];
4723   struct pci_dev        *Ze_pdev[NR_CARDS];
4724
4725         for (i = 0; i < NR_CARDS; i++) {
4726                 /* look for a Cyclades card by vendor and device id */
4727                 while((device_id = cy_pci_dev_id[dev_index]) != 0) {
4728                         if((pdev = pci_get_device(PCI_VENDOR_ID_CYCLADES,
4729                                         device_id, pdev)) == NULL) {
4730                                 dev_index++;    /* try next device id */
4731                         } else {
4732                                 break;          /* found a board */
4733                         }
4734                 }
4735
4736                 if (device_id == 0)
4737                     break;
4738
4739                 if (pci_enable_device(pdev))
4740                     continue;
4741
4742                 /* read PCI configuration area */
4743                 cy_pci_irq = pdev->irq;
4744                 cy_pci_phys0 = pci_resource_start(pdev, 0);
4745                 cy_pci_phys2 = pci_resource_start(pdev, 2);
4746                 pci_read_config_byte(pdev, PCI_REVISION_ID, &cyy_rev_id);
4747
4748                 device_id &= ~PCI_DEVICE_ID_MASK;
4749
4750     if ((device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo)
4751            || (device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi)){
4752 #ifdef CY_PCI_DEBUG
4753             printk("Cyclom-Y/PCI (bus=0x0%x, pci_id=0x%x, ",
4754                 pdev->bus->number, pdev->devfn);
4755             printk("rev_id=%d) IRQ%d\n",
4756                 cyy_rev_id, (int)cy_pci_irq);
4757             printk("Cyclom-Y/PCI:found  winaddr=0x%lx ctladdr=0x%lx\n",
4758                 (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4759 #endif
4760
4761                 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
4762                     printk("  Warning: PCI I/O bit incorrectly set. "
4763                            "Ignoring it...\n");
4764                     pdev->resource[2].flags &= ~IORESOURCE_IO;
4765                 }
4766
4767                 /* Although we don't use this I/O region, we should
4768                    request it from the kernel anyway, to avoid problems
4769                    with other drivers accessing it. */
4770                 if (pci_request_regions(pdev, "Cyclom-Y") != 0) {
4771                         printk(KERN_ERR "cyclades: failed to reserve PCI resources\n");
4772                         continue;
4773                 }
4774
4775 #if defined(__alpha__)
4776                 if (device_id  == PCI_DEVICE_ID_CYCLOM_Y_Lo) { /* below 1M? */
4777                     printk("Cyclom-Y/PCI (bus=0x0%x, pci_id=0x%x, ",
4778                         pdev->bus->number, pdev->devfn);
4779                     printk("rev_id=%d) IRQ%d\n",
4780                         cyy_rev_id, (int)cy_pci_irq);
4781                     printk("Cyclom-Y/PCI:found  winaddr=0x%lx ctladdr=0x%lx\n",
4782                         (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4783                     printk("Cyclom-Y/PCI not supported for low addresses in "
4784                            "Alpha systems.\n");
4785                     i--;
4786                     continue;
4787                 }
4788 #endif
4789                 cy_pci_addr0 = ioremap(cy_pci_phys0, CyPCI_Yctl);
4790                 cy_pci_addr2 = ioremap(cy_pci_phys2, CyPCI_Ywin);
4791
4792 #ifdef CY_PCI_DEBUG
4793             printk("Cyclom-Y/PCI: relocate winaddr=0x%lx ctladdr=0x%lx\n",
4794                 (u_long)cy_pci_addr2, (u_long)cy_pci_addr0);
4795 #endif
4796                 cy_pci_nchan = (unsigned short)(CyPORTS_PER_CHIP * 
4797                        cyy_init_card(cy_pci_addr2, 1));
4798                 if(cy_pci_nchan == 0) {
4799                         printk("Cyclom-Y PCI host card with ");
4800                         printk("no Serial-Modules at 0x%lx.\n",
4801                             (ulong) cy_pci_phys2);
4802                         i--;
4803                         continue;
4804                 }
4805                 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
4806                         printk("Cyclom-Y/PCI found at 0x%lx ",
4807                             (ulong) cy_pci_phys2);
4808                         printk("but no channels are available.\n");
4809                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
4810                         return(i);
4811                 }
4812                 /* fill the next cy_card structure available */
4813                 for (j = 0 ; j < NR_CARDS ; j++) {
4814                         if (cy_card[j].base_addr == 0)  break;
4815                 }
4816                 if (j == NR_CARDS) {    /* no more cy_cards available */
4817                         printk("Cyclom-Y/PCI found at 0x%lx ",
4818                             (ulong) cy_pci_phys2);
4819                         printk("but no more cards can be used.\n");
4820                         printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4821                         return(i);
4822                 }
4823
4824                 /* allocate IRQ */
4825                 if(request_irq(cy_pci_irq, cyy_interrupt,
4826                         SA_SHIRQ, "Cyclom-Y", &cy_card[j]))
4827                 {
4828                         printk("Cyclom-Y/PCI found at 0x%lx ",
4829                             (ulong) cy_pci_phys2);
4830                         printk("but could not allocate IRQ%d.\n",
4831                             cy_pci_irq);
4832                         return(i);
4833                 }
4834
4835                 /* set cy_card */
4836                 cy_card[j].base_phys = (ulong)cy_pci_phys2;
4837                 cy_card[j].ctl_phys = (ulong)cy_pci_phys0;
4838                 cy_card[j].base_addr = cy_pci_addr2;
4839                 cy_card[j].ctl_addr = cy_pci_addr0;
4840                 cy_card[j].irq = (int) cy_pci_irq;
4841                 cy_card[j].bus_index = 1;
4842                 cy_card[j].first_line = cy_next_channel;
4843                 cy_card[j].num_chips = cy_pci_nchan/4;
4844                 cy_card[j].pdev = pdev;
4845         
4846                 /* enable interrupts in the PCI interface */
4847                 plx_ver = cy_readb(cy_pci_addr2 + CyPLX_VER) & 0x0f;
4848                 switch (plx_ver) {
4849                     case PLX_9050:
4850
4851                     cy_writeb(cy_pci_addr0+0x4c, 0x43);
4852                     break;
4853
4854                     case PLX_9060:
4855                     case PLX_9080:
4856                     default: /* Old boards, use PLX_9060 */
4857
4858                     plx_init(cy_pci_addr0, 0x6c);
4859                     /* For some yet unknown reason, once the PLX9060 reloads
4860                        the EEPROM, the IRQ is lost and, thus, we have to
4861                        re-write it to the PCI config. registers.
4862                        This will remain here until we find a permanent fix. */
4863                     pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, cy_pci_irq);
4864
4865                     cy_writew(cy_pci_addr0+0x68, 
4866                         cy_readw(cy_pci_addr0+0x68)|0x0900);
4867                     break;
4868                 }
4869
4870                 /* print message */
4871                 printk("Cyclom-Y/PCI #%d: 0x%lx-0x%lx, IRQ%d, ",
4872                        j+1, 
4873                        (ulong)cy_pci_phys2, 
4874                        (ulong)(cy_pci_phys2 + CyPCI_Ywin - 1),
4875                        (int)cy_pci_irq);
4876                 printk("%d channels starting from port %d.\n",
4877                     cy_pci_nchan, cy_next_channel);
4878
4879                 cy_next_channel += cy_pci_nchan;
4880     }else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo){
4881             /* print message */
4882                 printk("Cyclades-Z/PCI (bus=0x0%x, pci_id=0x%x, ",
4883                     pdev->bus->number, pdev->devfn);
4884                 printk("rev_id=%d) IRQ%d\n",
4885                     cyy_rev_id, (int)cy_pci_irq);
4886                 printk("Cyclades-Z/PCI: found winaddr=0x%lx ctladdr=0x%lx\n",
4887                     (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4888             printk("Cyclades-Z/PCI not supported for low addresses\n");
4889             break;
4890     }else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi){
4891 #ifdef CY_PCI_DEBUG
4892             printk("Cyclades-Z/PCI (bus=0x0%x, pci_id=0x%x, ",
4893                 pdev->bus->number, pdev->devfn);
4894             printk("rev_id=%d) IRQ%d\n",
4895                 cyy_rev_id, (int)cy_pci_irq);
4896             printk("Cyclades-Z/PCI: found winaddr=0x%lx ctladdr=0x%lx\n",
4897                 (ulong)cy_pci_phys2, (ulong)cy_pci_phys0);
4898 #endif
4899                 cy_pci_addr0 = ioremap(cy_pci_phys0, CyPCI_Zctl);
4900
4901                 /* Disable interrupts on the PLX before resetting it */
4902                 cy_writew(cy_pci_addr0+0x68,
4903                         cy_readw(cy_pci_addr0+0x68) & ~0x0900);
4904
4905                 plx_init(cy_pci_addr0, 0x6c);
4906                 /* For some yet unknown reason, once the PLX9060 reloads
4907                    the EEPROM, the IRQ is lost and, thus, we have to
4908                    re-write it to the PCI config. registers.
4909                    This will remain here until we find a permanent fix. */
4910                 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, cy_pci_irq);
4911
4912                 mailbox = (uclong)cy_readl(&((struct RUNTIME_9060 __iomem *) 
4913                            cy_pci_addr0)->mail_box_0);
4914
4915                 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
4916                     printk("  Warning: PCI I/O bit incorrectly set. "
4917                            "Ignoring it...\n");
4918                     pdev->resource[2].flags &= ~IORESOURCE_IO;
4919                 }
4920
4921                 /* Although we don't use this I/O region, we should
4922                    request it from the kernel anyway, to avoid problems
4923                    with other drivers accessing it. */
4924                 if (pci_request_regions(pdev, "Cyclades-Z") != 0) {
4925                         printk(KERN_ERR "cyclades: failed to reserve PCI resources\n");
4926                         continue;
4927                 }
4928         
4929                 if (mailbox == ZE_V1) {
4930                     cy_pci_addr2 = ioremap(cy_pci_phys2, CyPCI_Ze_win);
4931                     if (ZeIndex == NR_CARDS) {
4932                         printk("Cyclades-Ze/PCI found at 0x%lx ",
4933                                 (ulong)cy_pci_phys2);
4934                         printk("but no more cards can be used.\n");
4935                         printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4936                     } else {
4937                         Ze_phys0[ZeIndex] = cy_pci_phys0;
4938                         Ze_phys2[ZeIndex] = cy_pci_phys2;
4939                         Ze_addr0[ZeIndex] = cy_pci_addr0;
4940                         Ze_addr2[ZeIndex] = cy_pci_addr2;
4941                         Ze_irq[ZeIndex] = cy_pci_irq;
4942                         Ze_pdev[ZeIndex] = pdev;
4943                         ZeIndex++;
4944                     }
4945                     i--;
4946                     continue;
4947                 } else {
4948                     cy_pci_addr2 = ioremap(cy_pci_phys2, CyPCI_Zwin);
4949                 }
4950
4951 #ifdef CY_PCI_DEBUG
4952             printk("Cyclades-Z/PCI: relocate winaddr=0x%lx ctladdr=0x%lx\n",
4953                 (ulong)cy_pci_addr2, (ulong)cy_pci_addr0);
4954             if (mailbox == ZO_V1) {
4955                 cy_writel(&((struct RUNTIME_9060 *)
4956                           (cy_pci_addr0))->loc_addr_base, WIN_CREG);
4957                 PAUSE
4958                 printk("Cyclades-8Zo/PCI: FPGA id %lx, ver %lx\n",
4959                        (ulong)(0xff & cy_readl(&((struct CUSTOM_REG *)
4960                         (cy_pci_addr2))->fpga_id)),
4961                        (ulong)(0xff & cy_readl(&((struct CUSTOM_REG *)
4962                         (cy_pci_addr2))->fpga_version)));
4963                 cy_writel(&((struct RUNTIME_9060 *)
4964                           (cy_pci_addr0))->loc_addr_base, WIN_RAM);
4965             } else {
4966                 printk("Cyclades-Z/PCI: New Cyclades-Z board.  FPGA not loaded\n");
4967             }
4968 #endif
4969             /* The following clears the firmware id word.  This ensures
4970                that the driver will not attempt to talk to the board
4971                until it has been properly initialized.
4972              */
4973                 PAUSE
4974                 if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
4975                     cy_writel(cy_pci_addr2 + ID_ADDRESS, 0L);
4976
4977                 /* This must be a Cyclades-8Zo/PCI.  The extendable
4978                    version will have a different device_id and will
4979                    be allocated its maximum number of ports. */
4980                 cy_pci_nchan = 8;
4981
4982                 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
4983                         printk("Cyclades-8Zo/PCI found at 0x%lx ",
4984                             (ulong)cy_pci_phys2);
4985                         printk("but no channels are available.\n");
4986                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
4987                         return(i);
4988                 }
4989
4990                 /* fill the next cy_card structure available */
4991                 for (j = 0 ; j < NR_CARDS ; j++) {
4992                         if (cy_card[j].base_addr == 0)  break;
4993                 }
4994                 if (j == NR_CARDS) {    /* no more cy_cards available */
4995                     printk("Cyclades-8Zo/PCI found at 0x%lx ",
4996                         (ulong)cy_pci_phys2);
4997                     printk("but no more cards can be used.\n");
4998                     printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
4999                     return(i);
5000                 }
5001
5002 #ifdef CONFIG_CYZ_INTR
5003                 /* allocate IRQ only if board has an IRQ */
5004                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) ) {
5005                     if(request_irq(cy_pci_irq, cyz_interrupt,
5006                         SA_SHIRQ, "Cyclades-Z", &cy_card[j]))
5007                     {
5008                         printk("Cyclom-8Zo/PCI found at 0x%lx ",
5009                             (ulong) cy_pci_phys2);
5010                         printk("but could not allocate IRQ%d.\n",
5011                             cy_pci_irq);
5012                         return(i);
5013                     }
5014                 }
5015 #endif /* CONFIG_CYZ_INTR */
5016
5017
5018                 /* set cy_card */
5019                 cy_card[j].base_phys = cy_pci_phys2;
5020                 cy_card[j].ctl_phys = cy_pci_phys0;
5021                 cy_card[j].base_addr = cy_pci_addr2;
5022                 cy_card[j].ctl_addr = cy_pci_addr0;
5023                 cy_card[j].irq = (int) cy_pci_irq;
5024                 cy_card[j].bus_index = 1;
5025                 cy_card[j].first_line = cy_next_channel;
5026                 cy_card[j].num_chips = -1;
5027                 cy_card[j].pdev = pdev;
5028
5029                 /* print message */
5030 #ifdef CONFIG_CYZ_INTR
5031                 /* don't report IRQ if board is no IRQ */
5032                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) )
5033                     printk("Cyclades-8Zo/PCI #%d: 0x%lx-0x%lx, IRQ%d, ",
5034                         j+1,(ulong)cy_pci_phys2,
5035                         (ulong)(cy_pci_phys2 + CyPCI_Zwin - 1),
5036                         (int)cy_pci_irq);
5037                 else
5038 #endif /* CONFIG_CYZ_INTR */
5039                     printk("Cyclades-8Zo/PCI #%d: 0x%lx-0x%lx, ",
5040                         j+1,(ulong)cy_pci_phys2,
5041                         (ulong)(cy_pci_phys2 + CyPCI_Zwin - 1));
5042
5043                 printk("%d channels starting from port %d.\n",
5044                     cy_pci_nchan,cy_next_channel);
5045                 cy_next_channel += cy_pci_nchan;
5046             }
5047         }
5048
5049         for (; ZeIndex != 0 && i < NR_CARDS; i++) {
5050             cy_pci_phys0 = Ze_phys0[0];
5051             cy_pci_phys2 = Ze_phys2[0];
5052             cy_pci_addr0 = Ze_addr0[0];
5053             cy_pci_addr2 = Ze_addr2[0];
5054             cy_pci_irq = Ze_irq[0];
5055             pdev = Ze_pdev[0];
5056             for (j = 0 ; j < ZeIndex-1 ; j++) {
5057                 Ze_phys0[j] = Ze_phys0[j+1];
5058                 Ze_phys2[j] = Ze_phys2[j+1];
5059                 Ze_addr0[j] = Ze_addr0[j+1];
5060                 Ze_addr2[j] = Ze_addr2[j+1];
5061                 Ze_irq[j] = Ze_irq[j+1];
5062                 Ze_pdev[j] = Ze_pdev[j+1];
5063             }
5064             ZeIndex--;
5065                 mailbox = (uclong)cy_readl(&((struct RUNTIME_9060 __iomem *) 
5066                                            cy_pci_addr0)->mail_box_0);
5067 #ifdef CY_PCI_DEBUG
5068             printk("Cyclades-Z/PCI: relocate winaddr=0x%lx ctladdr=0x%lx\n",
5069                 (ulong)cy_pci_addr2, (ulong)cy_pci_addr0);
5070             printk("Cyclades-Z/PCI: New Cyclades-Z board.  FPGA not loaded\n");
5071 #endif
5072                 PAUSE
5073                 /* This must be the new Cyclades-Ze/PCI. */
5074                 cy_pci_nchan = ZE_V1_NPORTS;
5075
5076                 if((cy_next_channel+cy_pci_nchan) > NR_PORTS) {
5077                         printk("Cyclades-Ze/PCI found at 0x%lx ",
5078                             (ulong)cy_pci_phys2);
5079                         printk("but no channels are available.\n");
5080                         printk("Change NR_PORTS in cyclades.c and recompile kernel.\n");
5081                         return(i);
5082                 }
5083
5084                 /* fill the next cy_card structure available */
5085                 for (j = 0 ; j < NR_CARDS ; j++) {
5086                         if (cy_card[j].base_addr == 0)  break;
5087                 }
5088                 if (j == NR_CARDS) {    /* no more cy_cards available */
5089                     printk("Cyclades-Ze/PCI found at 0x%lx ",
5090                         (ulong)cy_pci_phys2);
5091                     printk("but no more cards can be used.\n");
5092                     printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
5093                     return(i);
5094                 }
5095
5096 #ifdef CONFIG_CYZ_INTR
5097                 /* allocate IRQ only if board has an IRQ */
5098                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) ) {
5099                     if(request_irq(cy_pci_irq, cyz_interrupt,
5100                         SA_SHIRQ, "Cyclades-Z", &cy_card[j]))
5101                     {
5102                         printk("Cyclom-Ze/PCI found at 0x%lx ",
5103                             (ulong) cy_pci_phys2);
5104                         printk("but could not allocate IRQ%d.\n",
5105                             cy_pci_irq);
5106                         return(i);
5107                     }
5108                 }
5109 #endif /* CONFIG_CYZ_INTR */
5110
5111                 /* set cy_card */
5112                 cy_card[j].base_phys = cy_pci_phys2;
5113                 cy_card[j].ctl_phys = cy_pci_phys0;
5114                 cy_card[j].base_addr = cy_pci_addr2;
5115                 cy_card[j].ctl_addr = cy_pci_addr0;
5116                 cy_card[j].irq = (int) cy_pci_irq;
5117                 cy_card[j].bus_index = 1;
5118                 cy_card[j].first_line = cy_next_channel;
5119                 cy_card[j].num_chips = -1;
5120                 cy_card[j].pdev = pdev;
5121
5122                 /* print message */
5123 #ifdef CONFIG_CYZ_INTR
5124                 /* don't report IRQ if board is no IRQ */
5125                 if( (cy_pci_irq != 0) && (cy_pci_irq != 255) )
5126                     printk("Cyclades-Ze/PCI #%d: 0x%lx-0x%lx, IRQ%d, ",
5127                         j+1,(ulong)cy_pci_phys2,
5128                         (ulong)(cy_pci_phys2 + CyPCI_Ze_win - 1),
5129                         (int)cy_pci_irq);
5130                 else
5131 #endif /* CONFIG_CYZ_INTR */
5132                     printk("Cyclades-Ze/PCI #%d: 0x%lx-0x%lx, ",
5133                         j+1,(ulong)cy_pci_phys2,
5134                         (ulong)(cy_pci_phys2 + CyPCI_Ze_win - 1));
5135
5136                 printk("%d channels starting from port %d.\n",
5137                     cy_pci_nchan,cy_next_channel);
5138                 cy_next_channel += cy_pci_nchan;
5139         }
5140         if (ZeIndex != 0) {
5141             printk("Cyclades-Ze/PCI found at 0x%x ",
5142                 (unsigned int) Ze_phys2[0]);
5143             printk("but no more cards can be used.\n");
5144             printk("Change NR_CARDS in cyclades.c and recompile kernel.\n");
5145         }
5146         return(i);
5147 #else
5148         return(0);
5149 #endif /* ifdef CONFIG_PCI */
5150 } /* cy_detect_pci */
5151
5152
5153 /*
5154  * This routine prints out the appropriate serial driver version number
5155  * and identifies which options were configured into this driver.
5156  */
5157 static inline void
5158 show_version(void)
5159 {
5160   char *rcsvers, *rcsdate, *tmp;
5161     rcsvers = strchr(rcsid, ' '); rcsvers++;
5162     tmp = strchr(rcsvers, ' '); *tmp++ = '\0';
5163     rcsdate = strchr(tmp, ' '); rcsdate++;
5164     tmp = strrchr(rcsdate, ' '); *tmp = '\0';
5165     printk("Cyclades driver %s %s\n",
5166         rcsvers, rcsdate);
5167     printk("        built %s %s\n",
5168         __DATE__, __TIME__);
5169 } /* show_version */
5170
5171 static int 
5172 cyclades_get_proc_info(char *buf, char **start, off_t offset, int length,
5173                        int *eof, void *data)
5174 {
5175     struct cyclades_port  *info;
5176     int i;
5177     int len=0;
5178     off_t begin=0;
5179     off_t pos=0;
5180     int size;
5181     __u32 cur_jifs = jiffies;
5182
5183     size = sprintf(buf, "Dev TimeOpen   BytesOut  IdleOut    BytesIn   IdleIn  Overruns  Ldisc\n");
5184
5185     pos += size;
5186     len += size;
5187
5188     /* Output one line for each known port */
5189     for (i = 0; i < NR_PORTS && cy_port[i].line >= 0; i++) {
5190         info = &cy_port[i];
5191
5192         if (info->count)
5193             size = sprintf(buf+len,
5194                         "%3d %8lu %10lu %8lu %10lu %8lu %9lu %6ld\n",
5195                         info->line,
5196                         JIFFIES_DIFF(info->idle_stats.in_use, cur_jifs) / HZ,
5197                         info->idle_stats.xmit_bytes,
5198                         JIFFIES_DIFF(info->idle_stats.xmit_idle, cur_jifs) / HZ,
5199                         info->idle_stats.recv_bytes,
5200                         JIFFIES_DIFF(info->idle_stats.recv_idle, cur_jifs) / HZ,
5201                         info->idle_stats.overruns,
5202                         (long) info->tty->ldisc.num);
5203         else
5204             size = sprintf(buf+len,
5205                         "%3d %8lu %10lu %8lu %10lu %8lu %9lu %6ld\n",
5206                         info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
5207         len += size;
5208         pos = begin + len;
5209
5210         if (pos < offset) {
5211             len   = 0;
5212             begin = pos;
5213         }
5214         if (pos > offset + length)
5215             goto done;
5216     }
5217     *eof = 1;
5218 done:
5219     *start = buf + (offset - begin);    /* Start of wanted data */
5220     len -= (offset - begin);            /* Start slop */
5221     if (len > length)
5222         len = length;                   /* Ending slop */
5223     if (len < 0)
5224         len = 0;
5225     return len;
5226 }
5227
5228 /* The serial driver boot-time initialization code!
5229     Hardware I/O ports are mapped to character special devices on a
5230     first found, first allocated manner.  That is, this code searches
5231     for Cyclom cards in the system.  As each is found, it is probed
5232     to discover how many chips (and thus how many ports) are present.
5233     These ports are mapped to the tty ports 32 and upward in monotonic
5234     fashion.  If an 8-port card is replaced with a 16-port card, the
5235     port mapping on a following card will shift.
5236
5237     This approach is different from what is used in the other serial
5238     device driver because the Cyclom is more properly a multiplexer,
5239     not just an aggregation of serial ports on one card.
5240
5241     If there are more cards with more ports than have been
5242     statically allocated above, a warning is printed and the
5243     extra ports are ignored.
5244  */
5245
5246 static struct tty_operations cy_ops = {
5247     .open = cy_open,
5248     .close = cy_close,
5249     .write = cy_write,
5250     .put_char = cy_put_char,
5251     .flush_chars = cy_flush_chars,
5252     .write_room = cy_write_room,
5253     .chars_in_buffer = cy_chars_in_buffer,
5254     .flush_buffer = cy_flush_buffer,
5255     .ioctl = cy_ioctl,
5256     .throttle = cy_throttle,
5257     .unthrottle = cy_unthrottle,
5258     .set_termios = cy_set_termios,
5259     .stop = cy_stop,
5260     .start = cy_start,
5261     .hangup = cy_hangup,
5262     .break_ctl = cy_break,
5263     .wait_until_sent = cy_wait_until_sent,
5264     .read_proc = cyclades_get_proc_info,
5265     .tiocmget = cy_tiocmget,
5266     .tiocmset = cy_tiocmset,
5267 };
5268
5269 static int __init
5270 cy_init(void)
5271 {
5272   struct cyclades_port  *info;
5273   struct cyclades_card *cinfo;
5274   int number_z_boards = 0;
5275   int board,port,i,index;
5276   unsigned long mailbox;
5277   unsigned short chip_number;
5278   int nports;
5279
5280     cy_serial_driver = alloc_tty_driver(NR_PORTS);
5281     if (!cy_serial_driver)
5282         return -ENOMEM;
5283     show_version();
5284
5285     /* Initialize the tty_driver structure */
5286     
5287     cy_serial_driver->owner = THIS_MODULE;
5288     cy_serial_driver->driver_name = "cyclades";
5289     cy_serial_driver->name = "ttyC";
5290     cy_serial_driver->devfs_name = "tts/C";
5291     cy_serial_driver->major = CYCLADES_MAJOR;
5292     cy_serial_driver->minor_start = 0;
5293     cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
5294     cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
5295     cy_serial_driver->init_termios = tty_std_termios;
5296     cy_serial_driver->init_termios.c_cflag =
5297             B9600 | CS8 | CREAD | HUPCL | CLOCAL;
5298     cy_serial_driver->flags = TTY_DRIVER_REAL_RAW;
5299     tty_set_operations(cy_serial_driver, &cy_ops);
5300
5301     if (tty_register_driver(cy_serial_driver))
5302             panic("Couldn't register Cyclades serial driver\n");
5303
5304     for (i = 0; i < NR_CARDS; i++) {
5305             /* base_addr=0 indicates board not found */
5306             cy_card[i].base_addr = NULL;
5307     }
5308
5309     /* the code below is responsible to find the boards. Each different
5310        type of board has its own detection routine. If a board is found,
5311        the next cy_card structure available is set by the detection
5312        routine. These functions are responsible for checking the
5313        availability of cy_card and cy_port data structures and updating
5314        the cy_next_channel. */
5315
5316     /* look for isa boards */
5317     cy_isa_nboard = cy_detect_isa();
5318
5319     /* look for pci boards */
5320     cy_pci_nboard = cy_detect_pci();
5321
5322     cy_nboard = cy_isa_nboard + cy_pci_nboard;
5323
5324     /* invalidate remaining cy_card structures */
5325     for (i = 0 ; i < NR_CARDS ; i++) {
5326         if (cy_card[i].base_addr == 0) {
5327                 cy_card[i].first_line = -1;
5328                 cy_card[i].ctl_addr = NULL;
5329                 cy_card[i].irq = 0;
5330                 cy_card[i].bus_index = 0;
5331                 cy_card[i].first_line = 0;
5332                 cy_card[i].num_chips = 0;
5333         }
5334     }
5335     /* invalidate remaining cy_port structures */
5336     for (i = cy_next_channel ; i < NR_PORTS ; i++) {
5337         cy_port[i].line = -1;
5338         cy_port[i].magic = -1;
5339     }
5340
5341     /* initialize per-port data structures for each valid board found */
5342     for (board = 0 ; board < cy_nboard ; board++) {
5343             cinfo = &cy_card[board];
5344             if (cinfo->num_chips == -1) { /* Cyclades-Z */
5345                 number_z_boards++;
5346                 mailbox = cy_readl(&((struct RUNTIME_9060 __iomem *)
5347                              cy_card[board].ctl_addr)->mail_box_0);
5348                 nports = (mailbox == ZE_V1) ? ZE_V1_NPORTS : 8;
5349                 cinfo->intr_enabled = 0;
5350                 cinfo->nports = 0; /* Will be correctly set later, after 
5351                                       Z FW is loaded */
5352                 spin_lock_init(&cinfo->card_lock);
5353                 for (port = cinfo->first_line ;
5354                      port < cinfo->first_line + nports;
5355                      port++)
5356                 {
5357                     info = &cy_port[port];
5358                     info->magic = CYCLADES_MAGIC;
5359                     info->type = PORT_STARTECH;
5360                     info->card = board;
5361                     info->line = port;
5362                     info->chip_rev = 0;
5363                     info->flags = STD_COM_FLAGS;
5364                     info->tty = NULL;
5365                     if (mailbox == ZO_V1)
5366                         info->xmit_fifo_size = CYZ_FIFO_SIZE;
5367                     else
5368                         info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
5369                     info->cor1 = 0;
5370                     info->cor2 = 0;
5371                     info->cor3 = 0;
5372                     info->cor4 = 0;
5373                     info->cor5 = 0;
5374                     info->tbpr = 0;
5375                     info->tco = 0;
5376                     info->rbpr = 0;
5377                     info->rco = 0;
5378                     info->custom_divisor = 0;
5379                     info->close_delay = 5*HZ/10;
5380                     info->closing_wait = CLOSING_WAIT_DELAY;
5381                     info->icount.cts = info->icount.dsr = 
5382                         info->icount.rng = info->icount.dcd = 0;
5383                     info->icount.rx = info->icount.tx = 0;
5384                     info->icount.frame = info->icount.parity = 0;
5385                     info->icount.overrun = info->icount.brk = 0;
5386                     info->x_char = 0;
5387                     info->event = 0;
5388                     info->count = 0;
5389                     info->blocked_open = 0;
5390                     info->default_threshold = 0;
5391                     info->default_timeout = 0;
5392                     INIT_WORK(&info->tqueue, do_softint, info);
5393                     init_waitqueue_head(&info->open_wait);
5394                     init_waitqueue_head(&info->close_wait);
5395                     init_waitqueue_head(&info->shutdown_wait);
5396                     init_waitqueue_head(&info->delta_msr_wait);
5397                     /* info->session */
5398                     /* info->pgrp */
5399                     info->read_status_mask = 0;
5400                     /* info->timeout */
5401                     /* Bentson's vars */
5402                     info->jiffies[0] = 0;
5403                     info->jiffies[1] = 0;
5404                     info->jiffies[2] = 0;
5405                     info->rflush_count = 0;
5406 #ifdef CONFIG_CYZ_INTR
5407                     init_timer(&cyz_rx_full_timer[port]);
5408                     cyz_rx_full_timer[port].function = NULL;
5409 #endif
5410                 }
5411                 continue;
5412             }else{ /* Cyclom-Y of some kind*/
5413                 index = cinfo->bus_index;
5414                 spin_lock_init(&cinfo->card_lock);
5415                 cinfo->nports = CyPORTS_PER_CHIP * cinfo->num_chips;
5416                 for (port = cinfo->first_line ;
5417                      port < cinfo->first_line + cinfo->nports ;
5418                      port++)
5419                 {
5420                     info = &cy_port[port];
5421                     info->magic = CYCLADES_MAGIC;
5422                     info->type = PORT_CIRRUS;
5423                     info->card = board;
5424                     info->line = port;
5425                     info->flags = STD_COM_FLAGS;
5426                     info->tty = NULL;
5427                     info->xmit_fifo_size = CyMAX_CHAR_FIFO;
5428                     info->cor1 = CyPARITY_NONE|Cy_1_STOP|Cy_8_BITS;
5429                     info->cor2 = CyETC;
5430                     info->cor3 = 0x08; /* _very_ small rcv threshold */
5431                     info->cor4 = 0;
5432                     info->cor5 = 0;
5433                     info->custom_divisor = 0;
5434                     info->close_delay = 5*HZ/10;
5435                     info->closing_wait = CLOSING_WAIT_DELAY;
5436                     info->icount.cts = info->icount.dsr = 
5437                         info->icount.rng = info->icount.dcd = 0;
5438                     info->icount.rx = info->icount.tx = 0;
5439                     info->icount.frame = info->icount.parity = 0;
5440                     info->icount.overrun = info->icount.brk = 0;
5441                     chip_number = (port - cinfo->first_line) / 4;
5442                     if ((info->chip_rev =
5443                          cy_readb(cinfo->base_addr +
5444                                   (cy_chip_offset[chip_number]<<index) +
5445                                   (CyGFRCR<<index))) >= CD1400_REV_J) {
5446                         /* It is a CD1400 rev. J or later */
5447                         info->tbpr = baud_bpr_60[13]; /* Tx BPR */
5448                         info->tco = baud_co_60[13]; /* Tx CO */
5449                         info->rbpr = baud_bpr_60[13]; /* Rx BPR */
5450                         info->rco = baud_co_60[13]; /* Rx CO */
5451                         info->rflow = 0;
5452                         info->rtsdtr_inv = 1;
5453                     } else {
5454                         info->tbpr = baud_bpr_25[13]; /* Tx BPR */
5455                         info->tco = baud_co_25[13]; /* Tx CO */
5456                         info->rbpr = baud_bpr_25[13]; /* Rx BPR */
5457                         info->rco = baud_co_25[13]; /* Rx CO */
5458                         info->rflow = 0;
5459                         info->rtsdtr_inv = 0;
5460                     }
5461                     info->x_char = 0;
5462                     info->event = 0;
5463                     info->count = 0;
5464                     info->blocked_open = 0;
5465                     info->default_threshold = 0;
5466                     info->default_timeout = 0;
5467                     INIT_WORK(&info->tqueue, do_softint, info);
5468                     init_waitqueue_head(&info->open_wait);
5469                     init_waitqueue_head(&info->close_wait);
5470                     init_waitqueue_head(&info->shutdown_wait);
5471                     init_waitqueue_head(&info->delta_msr_wait);
5472                     /* info->session */
5473                     /* info->pgrp */
5474                     info->read_status_mask =
5475                                   CyTIMEOUT| CySPECHAR| CyBREAK
5476                                   | CyPARITY| CyFRAME| CyOVERRUN;
5477                     /* info->timeout */
5478                 }
5479             }
5480     }
5481
5482 #ifndef CONFIG_CYZ_INTR
5483     if (number_z_boards && !cyz_timeron){
5484         cyz_timeron++;
5485         cyz_timerlist.expires = jiffies + 1;
5486         add_timer(&cyz_timerlist);
5487 #ifdef CY_PCI_DEBUG
5488         printk("Cyclades-Z polling initialized\n");
5489 #endif
5490     }
5491 #endif /* CONFIG_CYZ_INTR */
5492
5493     return 0;
5494     
5495 } /* cy_init */
5496
5497 static void __exit
5498 cy_cleanup_module(void)
5499 {
5500     int i, e1;
5501
5502 #ifndef CONFIG_CYZ_INTR
5503     if (cyz_timeron){
5504         cyz_timeron = 0;
5505         del_timer(&cyz_timerlist);
5506     }
5507 #endif /* CONFIG_CYZ_INTR */
5508
5509     if ((e1 = tty_unregister_driver(cy_serial_driver)))
5510             printk("cyc: failed to unregister Cyclades serial driver(%d)\n",
5511                 e1);
5512
5513     put_tty_driver(cy_serial_driver);
5514
5515     for (i = 0; i < NR_CARDS; i++) {
5516         if (cy_card[i].base_addr) {
5517             iounmap(cy_card[i].base_addr);
5518             if (cy_card[i].ctl_addr)
5519                 iounmap(cy_card[i].ctl_addr);
5520             if (cy_card[i].irq
5521 #ifndef CONFIG_CYZ_INTR
5522                 && cy_card[i].num_chips != -1 /* not a Z card */
5523 #endif /* CONFIG_CYZ_INTR */
5524             )
5525                 free_irq(cy_card[i].irq, &cy_card[i]);
5526 #ifdef CONFIG_PCI
5527                 if (cy_card[i].pdev)
5528                         pci_release_regions(cy_card[i].pdev);
5529 #endif
5530         }
5531     }
5532     if (tmp_buf) {
5533         free_page((unsigned long) tmp_buf);
5534         tmp_buf = NULL;
5535     }
5536 } /* cy_cleanup_module */
5537
5538 module_init(cy_init);
5539 module_exit(cy_cleanup_module);
5540
5541 #ifndef MODULE
5542 /* called by linux/init/main.c to parse command line options */
5543 void
5544 cy_setup(char *str, int *ints)
5545 {
5546 #ifdef CONFIG_ISA
5547   int i, j;
5548
5549     for (i = 0 ; i < NR_ISA_ADDRS ; i++) {
5550         if (cy_isa_addresses[i] == 0) break;
5551     }
5552     for (j = 1; j <= ints[0]; j++){
5553         if ( i < NR_ISA_ADDRS ){
5554             cy_isa_addresses[i++] = ints[j];
5555         }
5556     }
5557 #endif /* CONFIG_ISA */
5558 } /* cy_setup */
5559 #endif /* MODULE */
5560
5561 MODULE_LICENSE("GPL");