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