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