[PATCH] devfs: Remove the tty_driver devfs_name field as it's no longer needed
[safe/jmp/linux-2.6] / drivers / char / stallion.c
1 /*****************************************************************************/
2
3 /*
4  *      stallion.c  -- stallion multiport serial driver.
5  *
6  *      Copyright (C) 1996-1999  Stallion Technologies
7  *      Copyright (C) 1994-1996  Greg Ungerer.
8  *
9  *      This code is loosely based on the Linux serial driver, written by
10  *      Linus Torvalds, Theodore T'so and others.
11  *
12  *      This program is free software; you can redistribute it and/or modify
13  *      it under the terms of the GNU General Public License as published by
14  *      the Free Software Foundation; either version 2 of the License, or
15  *      (at your option) any later version.
16  *
17  *      This program is distributed in the hope that it will be useful,
18  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *      GNU General Public License for more details.
21  *
22  *      You should have received a copy of the GNU General Public License
23  *      along with this program; if not, write to the Free Software
24  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 /*****************************************************************************/
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/serial.h>
36 #include <linux/cd1400.h>
37 #include <linux/sc26198.h>
38 #include <linux/comstats.h>
39 #include <linux/stallion.h>
40 #include <linux/ioport.h>
41 #include <linux/init.h>
42 #include <linux/smp_lock.h>
43 #include <linux/device.h>
44 #include <linux/delay.h>
45
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
48
49 #ifdef CONFIG_PCI
50 #include <linux/pci.h>
51 #endif
52
53 /*****************************************************************************/
54
55 /*
56  *      Define different board types. Use the standard Stallion "assigned"
57  *      board numbers. Boards supported in this driver are abbreviated as
58  *      EIO = EasyIO and ECH = EasyConnection 8/32.
59  */
60 #define BRD_EASYIO      20
61 #define BRD_ECH         21
62 #define BRD_ECHMC       22
63 #define BRD_ECHPCI      26
64 #define BRD_ECH64PCI    27
65 #define BRD_EASYIOPCI   28
66
67 /*
68  *      Define a configuration structure to hold the board configuration.
69  *      Need to set this up in the code (for now) with the boards that are
70  *      to be configured into the system. This is what needs to be modified
71  *      when adding/removing/modifying boards. Each line entry in the
72  *      stl_brdconf[] array is a board. Each line contains io/irq/memory
73  *      ranges for that board (as well as what type of board it is).
74  *      Some examples:
75  *              { BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
76  *      This line would configure an EasyIO board (4 or 8, no difference),
77  *      at io address 2a0 and irq 10.
78  *      Another example:
79  *              { BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
80  *      This line will configure an EasyConnection 8/32 board at primary io
81  *      address 2a8, secondary io address 280 and irq 12.
82  *      Enter as many lines into this array as you want (only the first 4
83  *      will actually be used!). Any combination of EasyIO and EasyConnection
84  *      boards can be specified. EasyConnection 8/32 boards can share their
85  *      secondary io addresses between each other.
86  *
87  *      NOTE: there is no need to put any entries in this table for PCI
88  *      boards. They will be found automatically by the driver - provided
89  *      PCI BIOS32 support is compiled into the kernel.
90  */
91
92 typedef struct {
93         int             brdtype;
94         int             ioaddr1;
95         int             ioaddr2;
96         unsigned long   memaddr;
97         int             irq;
98         int             irqtype;
99 } stlconf_t;
100
101 static stlconf_t        stl_brdconf[] = {
102         /*{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },*/
103 };
104
105 static int      stl_nrbrds = ARRAY_SIZE(stl_brdconf);
106
107 /*****************************************************************************/
108
109 /*
110  *      Define some important driver characteristics. Device major numbers
111  *      allocated as per Linux Device Registry.
112  */
113 #ifndef STL_SIOMEMMAJOR
114 #define STL_SIOMEMMAJOR         28
115 #endif
116 #ifndef STL_SERIALMAJOR
117 #define STL_SERIALMAJOR         24
118 #endif
119 #ifndef STL_CALLOUTMAJOR
120 #define STL_CALLOUTMAJOR        25
121 #endif
122
123 /*
124  *      Set the TX buffer size. Bigger is better, but we don't want
125  *      to chew too much memory with buffers!
126  */
127 #define STL_TXBUFLOW            512
128 #define STL_TXBUFSIZE           4096
129
130 /*****************************************************************************/
131
132 /*
133  *      Define our local driver identity first. Set up stuff to deal with
134  *      all the local structures required by a serial tty driver.
135  */
136 static char     *stl_drvtitle = "Stallion Multiport Serial Driver";
137 static char     *stl_drvname = "stallion";
138 static char     *stl_drvversion = "5.6.0";
139
140 static struct tty_driver        *stl_serial;
141
142 /*
143  *      We will need to allocate a temporary write buffer for chars that
144  *      come direct from user space. The problem is that a copy from user
145  *      space might cause a page fault (typically on a system that is
146  *      swapping!). All ports will share one buffer - since if the system
147  *      is already swapping a shared buffer won't make things any worse.
148  */
149 static char                     *stl_tmpwritebuf;
150
151 /*
152  *      Define a local default termios struct. All ports will be created
153  *      with this termios initially. Basically all it defines is a raw port
154  *      at 9600, 8 data bits, 1 stop bit.
155  */
156 static struct termios           stl_deftermios = {
157         .c_cflag        = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
158         .c_cc           = INIT_C_CC,
159 };
160
161 /*
162  *      Define global stats structures. Not used often, and can be
163  *      re-used for each stats call.
164  */
165 static comstats_t       stl_comstats;
166 static combrd_t         stl_brdstats;
167 static stlbrd_t         stl_dummybrd;
168 static stlport_t        stl_dummyport;
169
170 /*
171  *      Define global place to put buffer overflow characters.
172  */
173 static char             stl_unwanted[SC26198_RXFIFOSIZE];
174
175 /*****************************************************************************/
176
177 static stlbrd_t         *stl_brds[STL_MAXBRDS];
178
179 /*
180  *      Per board state flags. Used with the state field of the board struct.
181  *      Not really much here!
182  */
183 #define BRD_FOUND       0x1
184
185 /*
186  *      Define the port structure istate flags. These set of flags are
187  *      modified at interrupt time - so setting and reseting them needs
188  *      to be atomic. Use the bit clear/setting routines for this.
189  */
190 #define ASYI_TXBUSY     1
191 #define ASYI_TXLOW      2
192 #define ASYI_DCDCHANGE  3
193 #define ASYI_TXFLOWED   4
194
195 /*
196  *      Define an array of board names as printable strings. Handy for
197  *      referencing boards when printing trace and stuff.
198  */
199 static char     *stl_brdnames[] = {
200         (char *) NULL,
201         (char *) NULL,
202         (char *) NULL,
203         (char *) NULL,
204         (char *) NULL,
205         (char *) NULL,
206         (char *) NULL,
207         (char *) NULL,
208         (char *) NULL,
209         (char *) NULL,
210         (char *) NULL,
211         (char *) NULL,
212         (char *) NULL,
213         (char *) NULL,
214         (char *) NULL,
215         (char *) NULL,
216         (char *) NULL,
217         (char *) NULL,
218         (char *) NULL,
219         (char *) NULL,
220         "EasyIO",
221         "EC8/32-AT",
222         "EC8/32-MC",
223         (char *) NULL,
224         (char *) NULL,
225         (char *) NULL,
226         "EC8/32-PCI",
227         "EC8/64-PCI",
228         "EasyIO-PCI",
229 };
230
231 /*****************************************************************************/
232
233 /*
234  *      Define some string labels for arguments passed from the module
235  *      load line. These allow for easy board definitions, and easy
236  *      modification of the io, memory and irq resoucres.
237  */
238 static int      stl_nargs = 0;
239 static char     *board0[4];
240 static char     *board1[4];
241 static char     *board2[4];
242 static char     *board3[4];
243
244 static char     **stl_brdsp[] = {
245         (char **) &board0,
246         (char **) &board1,
247         (char **) &board2,
248         (char **) &board3
249 };
250
251 /*
252  *      Define a set of common board names, and types. This is used to
253  *      parse any module arguments.
254  */
255
256 typedef struct stlbrdtype {
257         char    *name;
258         int     type;
259 } stlbrdtype_t;
260
261 static stlbrdtype_t     stl_brdstr[] = {
262         { "easyio", BRD_EASYIO },
263         { "eio", BRD_EASYIO },
264         { "20", BRD_EASYIO },
265         { "ec8/32", BRD_ECH },
266         { "ec8/32-at", BRD_ECH },
267         { "ec8/32-isa", BRD_ECH },
268         { "ech", BRD_ECH },
269         { "echat", BRD_ECH },
270         { "21", BRD_ECH },
271         { "ec8/32-mc", BRD_ECHMC },
272         { "ec8/32-mca", BRD_ECHMC },
273         { "echmc", BRD_ECHMC },
274         { "echmca", BRD_ECHMC },
275         { "22", BRD_ECHMC },
276         { "ec8/32-pc", BRD_ECHPCI },
277         { "ec8/32-pci", BRD_ECHPCI },
278         { "26", BRD_ECHPCI },
279         { "ec8/64-pc", BRD_ECH64PCI },
280         { "ec8/64-pci", BRD_ECH64PCI },
281         { "ech-pci", BRD_ECH64PCI },
282         { "echpci", BRD_ECH64PCI },
283         { "echpc", BRD_ECH64PCI },
284         { "27", BRD_ECH64PCI },
285         { "easyio-pc", BRD_EASYIOPCI },
286         { "easyio-pci", BRD_EASYIOPCI },
287         { "eio-pci", BRD_EASYIOPCI },
288         { "eiopci", BRD_EASYIOPCI },
289         { "28", BRD_EASYIOPCI },
290 };
291
292 /*
293  *      Define the module agruments.
294  */
295 MODULE_AUTHOR("Greg Ungerer");
296 MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
297 MODULE_LICENSE("GPL");
298
299 module_param_array(board0, charp, &stl_nargs, 0);
300 MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
301 module_param_array(board1, charp, &stl_nargs, 0);
302 MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
303 module_param_array(board2, charp, &stl_nargs, 0);
304 MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
305 module_param_array(board3, charp, &stl_nargs, 0);
306 MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
307
308 /*****************************************************************************/
309
310 /*
311  *      Hardware ID bits for the EasyIO and ECH boards. These defines apply
312  *      to the directly accessible io ports of these boards (not the uarts -
313  *      they are in cd1400.h and sc26198.h).
314  */
315 #define EIO_8PORTRS     0x04
316 #define EIO_4PORTRS     0x05
317 #define EIO_8PORTDI     0x00
318 #define EIO_8PORTM      0x06
319 #define EIO_MK3         0x03
320 #define EIO_IDBITMASK   0x07
321
322 #define EIO_BRDMASK     0xf0
323 #define ID_BRD4         0x10
324 #define ID_BRD8         0x20
325 #define ID_BRD16        0x30
326
327 #define EIO_INTRPEND    0x08
328 #define EIO_INTEDGE     0x00
329 #define EIO_INTLEVEL    0x08
330 #define EIO_0WS         0x10
331
332 #define ECH_ID          0xa0
333 #define ECH_IDBITMASK   0xe0
334 #define ECH_BRDENABLE   0x08
335 #define ECH_BRDDISABLE  0x00
336 #define ECH_INTENABLE   0x01
337 #define ECH_INTDISABLE  0x00
338 #define ECH_INTLEVEL    0x02
339 #define ECH_INTEDGE     0x00
340 #define ECH_INTRPEND    0x01
341 #define ECH_BRDRESET    0x01
342
343 #define ECHMC_INTENABLE 0x01
344 #define ECHMC_BRDRESET  0x02
345
346 #define ECH_PNLSTATUS   2
347 #define ECH_PNL16PORT   0x20
348 #define ECH_PNLIDMASK   0x07
349 #define ECH_PNLXPID     0x40
350 #define ECH_PNLINTRPEND 0x80
351
352 #define ECH_ADDR2MASK   0x1e0
353
354 /*
355  *      Define the vector mapping bits for the programmable interrupt board
356  *      hardware. These bits encode the interrupt for the board to use - it
357  *      is software selectable (except the EIO-8M).
358  */
359 static unsigned char    stl_vecmap[] = {
360         0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
361         0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
362 };
363
364 /*
365  *      Set up enable and disable macros for the ECH boards. They require
366  *      the secondary io address space to be activated and deactivated.
367  *      This way all ECH boards can share their secondary io region.
368  *      If this is an ECH-PCI board then also need to set the page pointer
369  *      to point to the correct page.
370  */
371 #define BRDENABLE(brdnr,pagenr)                                         \
372         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
373                 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE),    \
374                         stl_brds[(brdnr)]->ioctrl);                     \
375         else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)              \
376                 outb((pagenr), stl_brds[(brdnr)]->ioctrl);
377
378 #define BRDDISABLE(brdnr)                                               \
379         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
380                 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE),   \
381                         stl_brds[(brdnr)]->ioctrl);
382
383 #define STL_CD1400MAXBAUD       230400
384 #define STL_SC26198MAXBAUD      460800
385
386 #define STL_BAUDBASE            115200
387 #define STL_CLOSEDELAY          (5 * HZ / 10)
388
389 /*****************************************************************************/
390
391 #ifdef CONFIG_PCI
392
393 /*
394  *      Define the Stallion PCI vendor and device IDs.
395  */
396 #ifndef PCI_VENDOR_ID_STALLION
397 #define PCI_VENDOR_ID_STALLION          0x124d
398 #endif
399 #ifndef PCI_DEVICE_ID_ECHPCI832
400 #define PCI_DEVICE_ID_ECHPCI832         0x0000
401 #endif
402 #ifndef PCI_DEVICE_ID_ECHPCI864
403 #define PCI_DEVICE_ID_ECHPCI864         0x0002
404 #endif
405 #ifndef PCI_DEVICE_ID_EIOPCI
406 #define PCI_DEVICE_ID_EIOPCI            0x0003
407 #endif
408
409 /*
410  *      Define structure to hold all Stallion PCI boards.
411  */
412 typedef struct stlpcibrd {
413         unsigned short          vendid;
414         unsigned short          devid;
415         int                     brdtype;
416 } stlpcibrd_t;
417
418 static stlpcibrd_t      stl_pcibrds[] = {
419         { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864, BRD_ECH64PCI },
420         { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI, BRD_EASYIOPCI },
421         { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832, BRD_ECHPCI },
422         { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, BRD_ECHPCI },
423 };
424
425 static int      stl_nrpcibrds = ARRAY_SIZE(stl_pcibrds);
426
427 #endif
428
429 /*****************************************************************************/
430
431 /*
432  *      Define macros to extract a brd/port number from a minor number.
433  */
434 #define MINOR2BRD(min)          (((min) & 0xc0) >> 6)
435 #define MINOR2PORT(min)         ((min) & 0x3f)
436
437 /*
438  *      Define a baud rate table that converts termios baud rate selector
439  *      into the actual baud rate value. All baud rate calculations are
440  *      based on the actual baud rate required.
441  */
442 static unsigned int     stl_baudrates[] = {
443         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
444         9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
445 };
446
447 /*
448  *      Define some handy local macros...
449  */
450 #undef  MIN
451 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
452
453 #undef  TOLOWER
454 #define TOLOWER(x)      ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
455
456 /*****************************************************************************/
457
458 /*
459  *      Declare all those functions in this driver!
460  */
461
462 static void     stl_argbrds(void);
463 static int      stl_parsebrd(stlconf_t *confp, char **argp);
464
465 static unsigned long stl_atol(char *str);
466
467 static int      stl_init(void);
468 static int      stl_open(struct tty_struct *tty, struct file *filp);
469 static void     stl_close(struct tty_struct *tty, struct file *filp);
470 static int      stl_write(struct tty_struct *tty, const unsigned char *buf, int count);
471 static void     stl_putchar(struct tty_struct *tty, unsigned char ch);
472 static void     stl_flushchars(struct tty_struct *tty);
473 static int      stl_writeroom(struct tty_struct *tty);
474 static int      stl_charsinbuffer(struct tty_struct *tty);
475 static int      stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
476 static void     stl_settermios(struct tty_struct *tty, struct termios *old);
477 static void     stl_throttle(struct tty_struct *tty);
478 static void     stl_unthrottle(struct tty_struct *tty);
479 static void     stl_stop(struct tty_struct *tty);
480 static void     stl_start(struct tty_struct *tty);
481 static void     stl_flushbuffer(struct tty_struct *tty);
482 static void     stl_breakctl(struct tty_struct *tty, int state);
483 static void     stl_waituntilsent(struct tty_struct *tty, int timeout);
484 static void     stl_sendxchar(struct tty_struct *tty, char ch);
485 static void     stl_hangup(struct tty_struct *tty);
486 static int      stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
487 static int      stl_portinfo(stlport_t *portp, int portnr, char *pos);
488 static int      stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data);
489
490 static int      stl_brdinit(stlbrd_t *brdp);
491 static int      stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
492 static int      stl_getserial(stlport_t *portp, struct serial_struct __user *sp);
493 static int      stl_setserial(stlport_t *portp, struct serial_struct __user *sp);
494 static int      stl_getbrdstats(combrd_t __user *bp);
495 static int      stl_getportstats(stlport_t *portp, comstats_t __user *cp);
496 static int      stl_clrportstats(stlport_t *portp, comstats_t __user *cp);
497 static int      stl_getportstruct(stlport_t __user *arg);
498 static int      stl_getbrdstruct(stlbrd_t __user *arg);
499 static int      stl_waitcarrier(stlport_t *portp, struct file *filp);
500 static int      stl_eiointr(stlbrd_t *brdp);
501 static int      stl_echatintr(stlbrd_t *brdp);
502 static int      stl_echmcaintr(stlbrd_t *brdp);
503 static int      stl_echpciintr(stlbrd_t *brdp);
504 static int      stl_echpci64intr(stlbrd_t *brdp);
505 static void     stl_offintr(void *private);
506 static stlbrd_t *stl_allocbrd(void);
507 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
508
509 static inline int       stl_initbrds(void);
510 static inline int       stl_initeio(stlbrd_t *brdp);
511 static inline int       stl_initech(stlbrd_t *brdp);
512 static inline int       stl_getbrdnr(void);
513
514 #ifdef  CONFIG_PCI
515 static inline int       stl_findpcibrds(void);
516 static inline int       stl_initpcibrd(int brdtype, struct pci_dev *devp);
517 #endif
518
519 /*
520  *      CD1400 uart specific handling functions.
521  */
522 static void     stl_cd1400setreg(stlport_t *portp, int regnr, int value);
523 static int      stl_cd1400getreg(stlport_t *portp, int regnr);
524 static int      stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
525 static int      stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
526 static void     stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
527 static void     stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
528 static int      stl_cd1400getsignals(stlport_t *portp);
529 static void     stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
530 static void     stl_cd1400ccrwait(stlport_t *portp);
531 static void     stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
532 static void     stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
533 static void     stl_cd1400disableintrs(stlport_t *portp);
534 static void     stl_cd1400sendbreak(stlport_t *portp, int len);
535 static void     stl_cd1400flowctrl(stlport_t *portp, int state);
536 static void     stl_cd1400sendflow(stlport_t *portp, int state);
537 static void     stl_cd1400flush(stlport_t *portp);
538 static int      stl_cd1400datastate(stlport_t *portp);
539 static void     stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
540 static void     stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
541 static void     stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
542 static void     stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
543 static void     stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
544
545 static inline int       stl_cd1400breakisr(stlport_t *portp, int ioaddr);
546
547 /*
548  *      SC26198 uart specific handling functions.
549  */
550 static void     stl_sc26198setreg(stlport_t *portp, int regnr, int value);
551 static int      stl_sc26198getreg(stlport_t *portp, int regnr);
552 static int      stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
553 static int      stl_sc26198getglobreg(stlport_t *portp, int regnr);
554 static int      stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
555 static void     stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
556 static void     stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
557 static int      stl_sc26198getsignals(stlport_t *portp);
558 static void     stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
559 static void     stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
560 static void     stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
561 static void     stl_sc26198disableintrs(stlport_t *portp);
562 static void     stl_sc26198sendbreak(stlport_t *portp, int len);
563 static void     stl_sc26198flowctrl(stlport_t *portp, int state);
564 static void     stl_sc26198sendflow(stlport_t *portp, int state);
565 static void     stl_sc26198flush(stlport_t *portp);
566 static int      stl_sc26198datastate(stlport_t *portp);
567 static void     stl_sc26198wait(stlport_t *portp);
568 static void     stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty);
569 static void     stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
570 static void     stl_sc26198txisr(stlport_t *port);
571 static void     stl_sc26198rxisr(stlport_t *port, unsigned int iack);
572 static void     stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch);
573 static void     stl_sc26198rxbadchars(stlport_t *portp);
574 static void     stl_sc26198otherisr(stlport_t *port, unsigned int iack);
575
576 /*****************************************************************************/
577
578 /*
579  *      Generic UART support structure.
580  */
581 typedef struct uart {
582         int     (*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
583         void    (*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
584         void    (*setport)(stlport_t *portp, struct termios *tiosp);
585         int     (*getsignals)(stlport_t *portp);
586         void    (*setsignals)(stlport_t *portp, int dtr, int rts);
587         void    (*enablerxtx)(stlport_t *portp, int rx, int tx);
588         void    (*startrxtx)(stlport_t *portp, int rx, int tx);
589         void    (*disableintrs)(stlport_t *portp);
590         void    (*sendbreak)(stlport_t *portp, int len);
591         void    (*flowctrl)(stlport_t *portp, int state);
592         void    (*sendflow)(stlport_t *portp, int state);
593         void    (*flush)(stlport_t *portp);
594         int     (*datastate)(stlport_t *portp);
595         void    (*intr)(stlpanel_t *panelp, unsigned int iobase);
596 } uart_t;
597
598 /*
599  *      Define some macros to make calling these functions nice and clean.
600  */
601 #define stl_panelinit           (* ((uart_t *) panelp->uartp)->panelinit)
602 #define stl_portinit            (* ((uart_t *) portp->uartp)->portinit)
603 #define stl_setport             (* ((uart_t *) portp->uartp)->setport)
604 #define stl_getsignals          (* ((uart_t *) portp->uartp)->getsignals)
605 #define stl_setsignals          (* ((uart_t *) portp->uartp)->setsignals)
606 #define stl_enablerxtx          (* ((uart_t *) portp->uartp)->enablerxtx)
607 #define stl_startrxtx           (* ((uart_t *) portp->uartp)->startrxtx)
608 #define stl_disableintrs        (* ((uart_t *) portp->uartp)->disableintrs)
609 #define stl_sendbreak           (* ((uart_t *) portp->uartp)->sendbreak)
610 #define stl_flowctrl            (* ((uart_t *) portp->uartp)->flowctrl)
611 #define stl_sendflow            (* ((uart_t *) portp->uartp)->sendflow)
612 #define stl_flush               (* ((uart_t *) portp->uartp)->flush)
613 #define stl_datastate           (* ((uart_t *) portp->uartp)->datastate)
614
615 /*****************************************************************************/
616
617 /*
618  *      CD1400 UART specific data initialization.
619  */
620 static uart_t stl_cd1400uart = {
621         stl_cd1400panelinit,
622         stl_cd1400portinit,
623         stl_cd1400setport,
624         stl_cd1400getsignals,
625         stl_cd1400setsignals,
626         stl_cd1400enablerxtx,
627         stl_cd1400startrxtx,
628         stl_cd1400disableintrs,
629         stl_cd1400sendbreak,
630         stl_cd1400flowctrl,
631         stl_cd1400sendflow,
632         stl_cd1400flush,
633         stl_cd1400datastate,
634         stl_cd1400eiointr
635 };
636
637 /*
638  *      Define the offsets within the register bank of a cd1400 based panel.
639  *      These io address offsets are common to the EasyIO board as well.
640  */
641 #define EREG_ADDR       0
642 #define EREG_DATA       4
643 #define EREG_RXACK      5
644 #define EREG_TXACK      6
645 #define EREG_MDACK      7
646
647 #define EREG_BANKSIZE   8
648
649 #define CD1400_CLK      25000000
650 #define CD1400_CLK8M    20000000
651
652 /*
653  *      Define the cd1400 baud rate clocks. These are used when calculating
654  *      what clock and divisor to use for the required baud rate. Also
655  *      define the maximum baud rate allowed, and the default base baud.
656  */
657 static int      stl_cd1400clkdivs[] = {
658         CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
659 };
660
661 /*****************************************************************************/
662
663 /*
664  *      SC26198 UART specific data initization.
665  */
666 static uart_t stl_sc26198uart = {
667         stl_sc26198panelinit,
668         stl_sc26198portinit,
669         stl_sc26198setport,
670         stl_sc26198getsignals,
671         stl_sc26198setsignals,
672         stl_sc26198enablerxtx,
673         stl_sc26198startrxtx,
674         stl_sc26198disableintrs,
675         stl_sc26198sendbreak,
676         stl_sc26198flowctrl,
677         stl_sc26198sendflow,
678         stl_sc26198flush,
679         stl_sc26198datastate,
680         stl_sc26198intr
681 };
682
683 /*
684  *      Define the offsets within the register bank of a sc26198 based panel.
685  */
686 #define XP_DATA         0
687 #define XP_ADDR         1
688 #define XP_MODID        2
689 #define XP_STATUS       2
690 #define XP_IACK         3
691
692 #define XP_BANKSIZE     4
693
694 /*
695  *      Define the sc26198 baud rate table. Offsets within the table
696  *      represent the actual baud rate selector of sc26198 registers.
697  */
698 static unsigned int     sc26198_baudtable[] = {
699         50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
700         4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
701         230400, 460800, 921600
702 };
703
704 #define SC26198_NRBAUDS         ARRAY_SIZE(sc26198_baudtable)
705
706 /*****************************************************************************/
707
708 /*
709  *      Define the driver info for a user level control device. Used mainly
710  *      to get at port stats - only not using the port device itself.
711  */
712 static struct file_operations   stl_fsiomem = {
713         .owner          = THIS_MODULE,
714         .ioctl          = stl_memioctl,
715 };
716
717 /*****************************************************************************/
718
719 static struct class *stallion_class;
720
721 /*
722  *      Loadable module initialization stuff.
723  */
724
725 static int __init stallion_module_init(void)
726 {
727         unsigned long   flags;
728
729 #ifdef DEBUG
730         printk("init_module()\n");
731 #endif
732
733         save_flags(flags);
734         cli();
735         stl_init();
736         restore_flags(flags);
737
738         return 0;
739 }
740
741 /*****************************************************************************/
742
743 static void __exit stallion_module_exit(void)
744 {
745         stlbrd_t        *brdp;
746         stlpanel_t      *panelp;
747         stlport_t       *portp;
748         unsigned long   flags;
749         int             i, j, k;
750
751 #ifdef DEBUG
752         printk("cleanup_module()\n");
753 #endif
754
755         printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
756                 stl_drvversion);
757
758         save_flags(flags);
759         cli();
760
761 /*
762  *      Free up all allocated resources used by the ports. This includes
763  *      memory and interrupts. As part of this process we will also do
764  *      a hangup on every open port - to try to flush out any processes
765  *      hanging onto ports.
766  */
767         i = tty_unregister_driver(stl_serial);
768         put_tty_driver(stl_serial);
769         if (i) {
770                 printk("STALLION: failed to un-register tty driver, "
771                         "errno=%d\n", -i);
772                 restore_flags(flags);
773                 return;
774         }
775         for (i = 0; i < 4; i++)
776                 class_device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
777         if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
778                 printk("STALLION: failed to un-register serial memory device, "
779                         "errno=%d\n", -i);
780         class_destroy(stallion_class);
781
782         kfree(stl_tmpwritebuf);
783
784         for (i = 0; (i < stl_nrbrds); i++) {
785                 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
786                         continue;
787
788                 free_irq(brdp->irq, brdp);
789
790                 for (j = 0; (j < STL_MAXPANELS); j++) {
791                         panelp = brdp->panels[j];
792                         if (panelp == (stlpanel_t *) NULL)
793                                 continue;
794                         for (k = 0; (k < STL_PORTSPERPANEL); k++) {
795                                 portp = panelp->ports[k];
796                                 if (portp == (stlport_t *) NULL)
797                                         continue;
798                                 if (portp->tty != (struct tty_struct *) NULL)
799                                         stl_hangup(portp->tty);
800                                 kfree(portp->tx.buf);
801                                 kfree(portp);
802                         }
803                         kfree(panelp);
804                 }
805
806                 release_region(brdp->ioaddr1, brdp->iosize1);
807                 if (brdp->iosize2 > 0)
808                         release_region(brdp->ioaddr2, brdp->iosize2);
809
810                 kfree(brdp);
811                 stl_brds[i] = (stlbrd_t *) NULL;
812         }
813
814         restore_flags(flags);
815 }
816
817 module_init(stallion_module_init);
818 module_exit(stallion_module_exit);
819
820 /*****************************************************************************/
821
822 /*
823  *      Check for any arguments passed in on the module load command line.
824  */
825
826 static void stl_argbrds(void)
827 {
828         stlconf_t       conf;
829         stlbrd_t        *brdp;
830         int             i;
831
832 #ifdef DEBUG
833         printk("stl_argbrds()\n");
834 #endif
835
836         for (i = stl_nrbrds; (i < stl_nargs); i++) {
837                 memset(&conf, 0, sizeof(conf));
838                 if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
839                         continue;
840                 if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
841                         continue;
842                 stl_nrbrds = i + 1;
843                 brdp->brdnr = i;
844                 brdp->brdtype = conf.brdtype;
845                 brdp->ioaddr1 = conf.ioaddr1;
846                 brdp->ioaddr2 = conf.ioaddr2;
847                 brdp->irq = conf.irq;
848                 brdp->irqtype = conf.irqtype;
849                 stl_brdinit(brdp);
850         }
851 }
852
853 /*****************************************************************************/
854
855 /*
856  *      Convert an ascii string number into an unsigned long.
857  */
858
859 static unsigned long stl_atol(char *str)
860 {
861         unsigned long   val;
862         int             base, c;
863         char            *sp;
864
865         val = 0;
866         sp = str;
867         if ((*sp == '0') && (*(sp+1) == 'x')) {
868                 base = 16;
869                 sp += 2;
870         } else if (*sp == '0') {
871                 base = 8;
872                 sp++;
873         } else {
874                 base = 10;
875         }
876
877         for (; (*sp != 0); sp++) {
878                 c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
879                 if ((c < 0) || (c >= base)) {
880                         printk("STALLION: invalid argument %s\n", str);
881                         val = 0;
882                         break;
883                 }
884                 val = (val * base) + c;
885         }
886         return val;
887 }
888
889 /*****************************************************************************/
890
891 /*
892  *      Parse the supplied argument string, into the board conf struct.
893  */
894
895 static int stl_parsebrd(stlconf_t *confp, char **argp)
896 {
897         char    *sp;
898         int     i;
899
900 #ifdef DEBUG
901         printk("stl_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
902 #endif
903
904         if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
905                 return 0;
906
907         for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
908                 *sp = TOLOWER(*sp);
909
910         for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++) {
911                 if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
912                         break;
913         }
914         if (i == ARRAY_SIZE(stl_brdstr)) {
915                 printk("STALLION: unknown board name, %s?\n", argp[0]);
916                 return 0;
917         }
918
919         confp->brdtype = stl_brdstr[i].type;
920
921         i = 1;
922         if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
923                 confp->ioaddr1 = stl_atol(argp[i]);
924         i++;
925         if (confp->brdtype == BRD_ECH) {
926                 if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
927                         confp->ioaddr2 = stl_atol(argp[i]);
928                 i++;
929         }
930         if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
931                 confp->irq = stl_atol(argp[i]);
932         return 1;
933 }
934
935 /*****************************************************************************/
936
937 /*
938  *      Allocate a new board structure. Fill out the basic info in it.
939  */
940
941 static stlbrd_t *stl_allocbrd(void)
942 {
943         stlbrd_t        *brdp;
944
945         brdp = kzalloc(sizeof(stlbrd_t), GFP_KERNEL);
946         if (!brdp) {
947                 printk("STALLION: failed to allocate memory (size=%d)\n",
948                         sizeof(stlbrd_t));
949                 return NULL;
950         }
951
952         brdp->magic = STL_BOARDMAGIC;
953         return brdp;
954 }
955
956 /*****************************************************************************/
957
958 static int stl_open(struct tty_struct *tty, struct file *filp)
959 {
960         stlport_t       *portp;
961         stlbrd_t        *brdp;
962         unsigned int    minordev;
963         int             brdnr, panelnr, portnr, rc;
964
965 #ifdef DEBUG
966         printk("stl_open(tty=%x,filp=%x): device=%s\n", (int) tty,
967                 (int) filp, tty->name);
968 #endif
969
970         minordev = tty->index;
971         brdnr = MINOR2BRD(minordev);
972         if (brdnr >= stl_nrbrds)
973                 return -ENODEV;
974         brdp = stl_brds[brdnr];
975         if (brdp == (stlbrd_t *) NULL)
976                 return -ENODEV;
977         minordev = MINOR2PORT(minordev);
978         for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
979                 if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
980                         break;
981                 if (minordev < brdp->panels[panelnr]->nrports) {
982                         portnr = minordev;
983                         break;
984                 }
985                 minordev -= brdp->panels[panelnr]->nrports;
986         }
987         if (portnr < 0)
988                 return -ENODEV;
989
990         portp = brdp->panels[panelnr]->ports[portnr];
991         if (portp == (stlport_t *) NULL)
992                 return -ENODEV;
993
994 /*
995  *      On the first open of the device setup the port hardware, and
996  *      initialize the per port data structure.
997  */
998         portp->tty = tty;
999         tty->driver_data = portp;
1000         portp->refcount++;
1001
1002         if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1003                 if (!portp->tx.buf) {
1004                         portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
1005                         if (!portp->tx.buf)
1006                                 return -ENOMEM;
1007                         portp->tx.head = portp->tx.buf;
1008                         portp->tx.tail = portp->tx.buf;
1009                 }
1010                 stl_setport(portp, tty->termios);
1011                 portp->sigs = stl_getsignals(portp);
1012                 stl_setsignals(portp, 1, 1);
1013                 stl_enablerxtx(portp, 1, 1);
1014                 stl_startrxtx(portp, 1, 0);
1015                 clear_bit(TTY_IO_ERROR, &tty->flags);
1016                 portp->flags |= ASYNC_INITIALIZED;
1017         }
1018
1019 /*
1020  *      Check if this port is in the middle of closing. If so then wait
1021  *      until it is closed then return error status, based on flag settings.
1022  *      The sleep here does not need interrupt protection since the wakeup
1023  *      for it is done with the same context.
1024  */
1025         if (portp->flags & ASYNC_CLOSING) {
1026                 interruptible_sleep_on(&portp->close_wait);
1027                 if (portp->flags & ASYNC_HUP_NOTIFY)
1028                         return -EAGAIN;
1029                 return -ERESTARTSYS;
1030         }
1031
1032 /*
1033  *      Based on type of open being done check if it can overlap with any
1034  *      previous opens still in effect. If we are a normal serial device
1035  *      then also we might have to wait for carrier.
1036  */
1037         if (!(filp->f_flags & O_NONBLOCK)) {
1038                 if ((rc = stl_waitcarrier(portp, filp)) != 0)
1039                         return rc;
1040         }
1041         portp->flags |= ASYNC_NORMAL_ACTIVE;
1042
1043         return 0;
1044 }
1045
1046 /*****************************************************************************/
1047
1048 /*
1049  *      Possibly need to wait for carrier (DCD signal) to come high. Say
1050  *      maybe because if we are clocal then we don't need to wait...
1051  */
1052
1053 static int stl_waitcarrier(stlport_t *portp, struct file *filp)
1054 {
1055         unsigned long   flags;
1056         int             rc, doclocal;
1057
1058 #ifdef DEBUG
1059         printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
1060 #endif
1061
1062         rc = 0;
1063         doclocal = 0;
1064
1065         if (portp->tty->termios->c_cflag & CLOCAL)
1066                 doclocal++;
1067
1068         save_flags(flags);
1069         cli();
1070         portp->openwaitcnt++;
1071         if (! tty_hung_up_p(filp))
1072                 portp->refcount--;
1073
1074         for (;;) {
1075                 stl_setsignals(portp, 1, 1);
1076                 if (tty_hung_up_p(filp) ||
1077                     ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1078                         if (portp->flags & ASYNC_HUP_NOTIFY)
1079                                 rc = -EBUSY;
1080                         else
1081                                 rc = -ERESTARTSYS;
1082                         break;
1083                 }
1084                 if (((portp->flags & ASYNC_CLOSING) == 0) &&
1085                     (doclocal || (portp->sigs & TIOCM_CD))) {
1086                         break;
1087                 }
1088                 if (signal_pending(current)) {
1089                         rc = -ERESTARTSYS;
1090                         break;
1091                 }
1092                 interruptible_sleep_on(&portp->open_wait);
1093         }
1094
1095         if (! tty_hung_up_p(filp))
1096                 portp->refcount++;
1097         portp->openwaitcnt--;
1098         restore_flags(flags);
1099
1100         return rc;
1101 }
1102
1103 /*****************************************************************************/
1104
1105 static void stl_close(struct tty_struct *tty, struct file *filp)
1106 {
1107         stlport_t       *portp;
1108         unsigned long   flags;
1109
1110 #ifdef DEBUG
1111         printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1112 #endif
1113
1114         portp = tty->driver_data;
1115         if (portp == (stlport_t *) NULL)
1116                 return;
1117
1118         save_flags(flags);
1119         cli();
1120         if (tty_hung_up_p(filp)) {
1121                 restore_flags(flags);
1122                 return;
1123         }
1124         if ((tty->count == 1) && (portp->refcount != 1))
1125                 portp->refcount = 1;
1126         if (portp->refcount-- > 1) {
1127                 restore_flags(flags);
1128                 return;
1129         }
1130
1131         portp->refcount = 0;
1132         portp->flags |= ASYNC_CLOSING;
1133
1134 /*
1135  *      May want to wait for any data to drain before closing. The BUSY
1136  *      flag keeps track of whether we are still sending or not - it is
1137  *      very accurate for the cd1400, not quite so for the sc26198.
1138  *      (The sc26198 has no "end-of-data" interrupt only empty FIFO)
1139  */
1140         tty->closing = 1;
1141         if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1142                 tty_wait_until_sent(tty, portp->closing_wait);
1143         stl_waituntilsent(tty, (HZ / 2));
1144
1145         portp->flags &= ~ASYNC_INITIALIZED;
1146         stl_disableintrs(portp);
1147         if (tty->termios->c_cflag & HUPCL)
1148                 stl_setsignals(portp, 0, 0);
1149         stl_enablerxtx(portp, 0, 0);
1150         stl_flushbuffer(tty);
1151         portp->istate = 0;
1152         if (portp->tx.buf != (char *) NULL) {
1153                 kfree(portp->tx.buf);
1154                 portp->tx.buf = (char *) NULL;
1155                 portp->tx.head = (char *) NULL;
1156                 portp->tx.tail = (char *) NULL;
1157         }
1158         set_bit(TTY_IO_ERROR, &tty->flags);
1159         tty_ldisc_flush(tty);
1160
1161         tty->closing = 0;
1162         portp->tty = (struct tty_struct *) NULL;
1163
1164         if (portp->openwaitcnt) {
1165                 if (portp->close_delay)
1166                         msleep_interruptible(jiffies_to_msecs(portp->close_delay));
1167                 wake_up_interruptible(&portp->open_wait);
1168         }
1169
1170         portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1171         wake_up_interruptible(&portp->close_wait);
1172         restore_flags(flags);
1173 }
1174
1175 /*****************************************************************************/
1176
1177 /*
1178  *      Write routine. Take data and stuff it in to the TX ring queue.
1179  *      If transmit interrupts are not running then start them.
1180  */
1181
1182 static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
1183 {
1184         stlport_t       *portp;
1185         unsigned int    len, stlen;
1186         unsigned char   *chbuf;
1187         char            *head, *tail;
1188
1189 #ifdef DEBUG
1190         printk("stl_write(tty=%x,buf=%x,count=%d)\n",
1191                 (int) tty, (int) buf, count);
1192 #endif
1193
1194         if ((tty == (struct tty_struct *) NULL) ||
1195             (stl_tmpwritebuf == (char *) NULL))
1196                 return 0;
1197         portp = tty->driver_data;
1198         if (portp == (stlport_t *) NULL)
1199                 return 0;
1200         if (portp->tx.buf == (char *) NULL)
1201                 return 0;
1202
1203 /*
1204  *      If copying direct from user space we must cater for page faults,
1205  *      causing us to "sleep" here for a while. To handle this copy in all
1206  *      the data we need now, into a local buffer. Then when we got it all
1207  *      copy it into the TX buffer.
1208  */
1209         chbuf = (unsigned char *) buf;
1210
1211         head = portp->tx.head;
1212         tail = portp->tx.tail;
1213         if (head >= tail) {
1214                 len = STL_TXBUFSIZE - (head - tail) - 1;
1215                 stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
1216         } else {
1217                 len = tail - head - 1;
1218                 stlen = len;
1219         }
1220
1221         len = MIN(len, count);
1222         count = 0;
1223         while (len > 0) {
1224                 stlen = MIN(len, stlen);
1225                 memcpy(head, chbuf, stlen);
1226                 len -= stlen;
1227                 chbuf += stlen;
1228                 count += stlen;
1229                 head += stlen;
1230                 if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
1231                         head = portp->tx.buf;
1232                         stlen = tail - head;
1233                 }
1234         }
1235         portp->tx.head = head;
1236
1237         clear_bit(ASYI_TXLOW, &portp->istate);
1238         stl_startrxtx(portp, -1, 1);
1239
1240         return count;
1241 }
1242
1243 /*****************************************************************************/
1244
1245 static void stl_putchar(struct tty_struct *tty, unsigned char ch)
1246 {
1247         stlport_t       *portp;
1248         unsigned int    len;
1249         char            *head, *tail;
1250
1251 #ifdef DEBUG
1252         printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1253 #endif
1254
1255         if (tty == (struct tty_struct *) NULL)
1256                 return;
1257         portp = tty->driver_data;
1258         if (portp == (stlport_t *) NULL)
1259                 return;
1260         if (portp->tx.buf == (char *) NULL)
1261                 return;
1262
1263         head = portp->tx.head;
1264         tail = portp->tx.tail;
1265
1266         len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
1267         len--;
1268
1269         if (len > 0) {
1270                 *head++ = ch;
1271                 if (head >= (portp->tx.buf + STL_TXBUFSIZE))
1272                         head = portp->tx.buf;
1273         }       
1274         portp->tx.head = head;
1275 }
1276
1277 /*****************************************************************************/
1278
1279 /*
1280  *      If there are any characters in the buffer then make sure that TX
1281  *      interrupts are on and get'em out. Normally used after the putchar
1282  *      routine has been called.
1283  */
1284
1285 static void stl_flushchars(struct tty_struct *tty)
1286 {
1287         stlport_t       *portp;
1288
1289 #ifdef DEBUG
1290         printk("stl_flushchars(tty=%x)\n", (int) tty);
1291 #endif
1292
1293         if (tty == (struct tty_struct *) NULL)
1294                 return;
1295         portp = tty->driver_data;
1296         if (portp == (stlport_t *) NULL)
1297                 return;
1298         if (portp->tx.buf == (char *) NULL)
1299                 return;
1300
1301 #if 0
1302         if (tty->stopped || tty->hw_stopped ||
1303             (portp->tx.head == portp->tx.tail))
1304                 return;
1305 #endif
1306         stl_startrxtx(portp, -1, 1);
1307 }
1308
1309 /*****************************************************************************/
1310
1311 static int stl_writeroom(struct tty_struct *tty)
1312 {
1313         stlport_t       *portp;
1314         char            *head, *tail;
1315
1316 #ifdef DEBUG
1317         printk("stl_writeroom(tty=%x)\n", (int) tty);
1318 #endif
1319
1320         if (tty == (struct tty_struct *) NULL)
1321                 return 0;
1322         portp = tty->driver_data;
1323         if (portp == (stlport_t *) NULL)
1324                 return 0;
1325         if (portp->tx.buf == (char *) NULL)
1326                 return 0;
1327
1328         head = portp->tx.head;
1329         tail = portp->tx.tail;
1330         return ((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
1331 }
1332
1333 /*****************************************************************************/
1334
1335 /*
1336  *      Return number of chars in the TX buffer. Normally we would just
1337  *      calculate the number of chars in the buffer and return that, but if
1338  *      the buffer is empty and TX interrupts are still on then we return
1339  *      that the buffer still has 1 char in it. This way whoever called us
1340  *      will not think that ALL chars have drained - since the UART still
1341  *      must have some chars in it (we are busy after all).
1342  */
1343
1344 static int stl_charsinbuffer(struct tty_struct *tty)
1345 {
1346         stlport_t       *portp;
1347         unsigned int    size;
1348         char            *head, *tail;
1349
1350 #ifdef DEBUG
1351         printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
1352 #endif
1353
1354         if (tty == (struct tty_struct *) NULL)
1355                 return 0;
1356         portp = tty->driver_data;
1357         if (portp == (stlport_t *) NULL)
1358                 return 0;
1359         if (portp->tx.buf == (char *) NULL)
1360                 return 0;
1361
1362         head = portp->tx.head;
1363         tail = portp->tx.tail;
1364         size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1365         if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1366                 size = 1;
1367         return size;
1368 }
1369
1370 /*****************************************************************************/
1371
1372 /*
1373  *      Generate the serial struct info.
1374  */
1375
1376 static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp)
1377 {
1378         struct serial_struct    sio;
1379         stlbrd_t                *brdp;
1380
1381 #ifdef DEBUG
1382         printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1383 #endif
1384
1385         memset(&sio, 0, sizeof(struct serial_struct));
1386         sio.line = portp->portnr;
1387         sio.port = portp->ioaddr;
1388         sio.flags = portp->flags;
1389         sio.baud_base = portp->baud_base;
1390         sio.close_delay = portp->close_delay;
1391         sio.closing_wait = portp->closing_wait;
1392         sio.custom_divisor = portp->custom_divisor;
1393         sio.hub6 = 0;
1394         if (portp->uartp == &stl_cd1400uart) {
1395                 sio.type = PORT_CIRRUS;
1396                 sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1397         } else {
1398                 sio.type = PORT_UNKNOWN;
1399                 sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1400         }
1401
1402         brdp = stl_brds[portp->brdnr];
1403         if (brdp != (stlbrd_t *) NULL)
1404                 sio.irq = brdp->irq;
1405
1406         return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
1407 }
1408
1409 /*****************************************************************************/
1410
1411 /*
1412  *      Set port according to the serial struct info.
1413  *      At this point we do not do any auto-configure stuff, so we will
1414  *      just quietly ignore any requests to change irq, etc.
1415  */
1416
1417 static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp)
1418 {
1419         struct serial_struct    sio;
1420
1421 #ifdef DEBUG
1422         printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1423 #endif
1424
1425         if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1426                 return -EFAULT;
1427         if (!capable(CAP_SYS_ADMIN)) {
1428                 if ((sio.baud_base != portp->baud_base) ||
1429                     (sio.close_delay != portp->close_delay) ||
1430                     ((sio.flags & ~ASYNC_USR_MASK) !=
1431                     (portp->flags & ~ASYNC_USR_MASK)))
1432                         return -EPERM;
1433         } 
1434
1435         portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1436                 (sio.flags & ASYNC_USR_MASK);
1437         portp->baud_base = sio.baud_base;
1438         portp->close_delay = sio.close_delay;
1439         portp->closing_wait = sio.closing_wait;
1440         portp->custom_divisor = sio.custom_divisor;
1441         stl_setport(portp, portp->tty->termios);
1442         return 0;
1443 }
1444
1445 /*****************************************************************************/
1446
1447 static int stl_tiocmget(struct tty_struct *tty, struct file *file)
1448 {
1449         stlport_t       *portp;
1450
1451         if (tty == (struct tty_struct *) NULL)
1452                 return -ENODEV;
1453         portp = tty->driver_data;
1454         if (portp == (stlport_t *) NULL)
1455                 return -ENODEV;
1456         if (tty->flags & (1 << TTY_IO_ERROR))
1457                 return -EIO;
1458
1459         return stl_getsignals(portp);
1460 }
1461
1462 static int stl_tiocmset(struct tty_struct *tty, struct file *file,
1463                         unsigned int set, unsigned int clear)
1464 {
1465         stlport_t       *portp;
1466         int rts = -1, dtr = -1;
1467
1468         if (tty == (struct tty_struct *) NULL)
1469                 return -ENODEV;
1470         portp = tty->driver_data;
1471         if (portp == (stlport_t *) NULL)
1472                 return -ENODEV;
1473         if (tty->flags & (1 << TTY_IO_ERROR))
1474                 return -EIO;
1475
1476         if (set & TIOCM_RTS)
1477                 rts = 1;
1478         if (set & TIOCM_DTR)
1479                 dtr = 1;
1480         if (clear & TIOCM_RTS)
1481                 rts = 0;
1482         if (clear & TIOCM_DTR)
1483                 dtr = 0;
1484
1485         stl_setsignals(portp, dtr, rts);
1486         return 0;
1487 }
1488
1489 static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1490 {
1491         stlport_t       *portp;
1492         unsigned int    ival;
1493         int             rc;
1494         void __user *argp = (void __user *)arg;
1495
1496 #ifdef DEBUG
1497         printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
1498                 (int) tty, (int) file, cmd, (int) arg);
1499 #endif
1500
1501         if (tty == (struct tty_struct *) NULL)
1502                 return -ENODEV;
1503         portp = tty->driver_data;
1504         if (portp == (stlport_t *) NULL)
1505                 return -ENODEV;
1506
1507         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1508             (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1509                 if (tty->flags & (1 << TTY_IO_ERROR))
1510                         return -EIO;
1511         }
1512
1513         rc = 0;
1514
1515         switch (cmd) {
1516         case TIOCGSOFTCAR:
1517                 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1518                         (unsigned __user *) argp);
1519                 break;
1520         case TIOCSSOFTCAR:
1521                 if (get_user(ival, (unsigned int __user *) arg))
1522                         return -EFAULT;
1523                 tty->termios->c_cflag =
1524                                 (tty->termios->c_cflag & ~CLOCAL) |
1525                                 (ival ? CLOCAL : 0);
1526                 break;
1527         case TIOCGSERIAL:
1528                 rc = stl_getserial(portp, argp);
1529                 break;
1530         case TIOCSSERIAL:
1531                 rc = stl_setserial(portp, argp);
1532                 break;
1533         case COM_GETPORTSTATS:
1534                 rc = stl_getportstats(portp, argp);
1535                 break;
1536         case COM_CLRPORTSTATS:
1537                 rc = stl_clrportstats(portp, argp);
1538                 break;
1539         case TIOCSERCONFIG:
1540         case TIOCSERGWILD:
1541         case TIOCSERSWILD:
1542         case TIOCSERGETLSR:
1543         case TIOCSERGSTRUCT:
1544         case TIOCSERGETMULTI:
1545         case TIOCSERSETMULTI:
1546         default:
1547                 rc = -ENOIOCTLCMD;
1548                 break;
1549         }
1550
1551         return rc;
1552 }
1553
1554 /*****************************************************************************/
1555
1556 static void stl_settermios(struct tty_struct *tty, struct termios *old)
1557 {
1558         stlport_t       *portp;
1559         struct termios  *tiosp;
1560
1561 #ifdef DEBUG
1562         printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
1563 #endif
1564
1565         if (tty == (struct tty_struct *) NULL)
1566                 return;
1567         portp = tty->driver_data;
1568         if (portp == (stlport_t *) NULL)
1569                 return;
1570
1571         tiosp = tty->termios;
1572         if ((tiosp->c_cflag == old->c_cflag) &&
1573             (tiosp->c_iflag == old->c_iflag))
1574                 return;
1575
1576         stl_setport(portp, tiosp);
1577         stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1578                 -1);
1579         if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1580                 tty->hw_stopped = 0;
1581                 stl_start(tty);
1582         }
1583         if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1584                 wake_up_interruptible(&portp->open_wait);
1585 }
1586
1587 /*****************************************************************************/
1588
1589 /*
1590  *      Attempt to flow control who ever is sending us data. Based on termios
1591  *      settings use software or/and hardware flow control.
1592  */
1593
1594 static void stl_throttle(struct tty_struct *tty)
1595 {
1596         stlport_t       *portp;
1597
1598 #ifdef DEBUG
1599         printk("stl_throttle(tty=%x)\n", (int) tty);
1600 #endif
1601
1602         if (tty == (struct tty_struct *) NULL)
1603                 return;
1604         portp = tty->driver_data;
1605         if (portp == (stlport_t *) NULL)
1606                 return;
1607         stl_flowctrl(portp, 0);
1608 }
1609
1610 /*****************************************************************************/
1611
1612 /*
1613  *      Unflow control the device sending us data...
1614  */
1615
1616 static void stl_unthrottle(struct tty_struct *tty)
1617 {
1618         stlport_t       *portp;
1619
1620 #ifdef DEBUG
1621         printk("stl_unthrottle(tty=%x)\n", (int) tty);
1622 #endif
1623
1624         if (tty == (struct tty_struct *) NULL)
1625                 return;
1626         portp = tty->driver_data;
1627         if (portp == (stlport_t *) NULL)
1628                 return;
1629         stl_flowctrl(portp, 1);
1630 }
1631
1632 /*****************************************************************************/
1633
1634 /*
1635  *      Stop the transmitter. Basically to do this we will just turn TX
1636  *      interrupts off.
1637  */
1638
1639 static void stl_stop(struct tty_struct *tty)
1640 {
1641         stlport_t       *portp;
1642
1643 #ifdef DEBUG
1644         printk("stl_stop(tty=%x)\n", (int) tty);
1645 #endif
1646
1647         if (tty == (struct tty_struct *) NULL)
1648                 return;
1649         portp = tty->driver_data;
1650         if (portp == (stlport_t *) NULL)
1651                 return;
1652         stl_startrxtx(portp, -1, 0);
1653 }
1654
1655 /*****************************************************************************/
1656
1657 /*
1658  *      Start the transmitter again. Just turn TX interrupts back on.
1659  */
1660
1661 static void stl_start(struct tty_struct *tty)
1662 {
1663         stlport_t       *portp;
1664
1665 #ifdef DEBUG
1666         printk("stl_start(tty=%x)\n", (int) tty);
1667 #endif
1668
1669         if (tty == (struct tty_struct *) NULL)
1670                 return;
1671         portp = tty->driver_data;
1672         if (portp == (stlport_t *) NULL)
1673                 return;
1674         stl_startrxtx(portp, -1, 1);
1675 }
1676
1677 /*****************************************************************************/
1678
1679 /*
1680  *      Hangup this port. This is pretty much like closing the port, only
1681  *      a little more brutal. No waiting for data to drain. Shutdown the
1682  *      port and maybe drop signals.
1683  */
1684
1685 static void stl_hangup(struct tty_struct *tty)
1686 {
1687         stlport_t       *portp;
1688
1689 #ifdef DEBUG
1690         printk("stl_hangup(tty=%x)\n", (int) tty);
1691 #endif
1692
1693         if (tty == (struct tty_struct *) NULL)
1694                 return;
1695         portp = tty->driver_data;
1696         if (portp == (stlport_t *) NULL)
1697                 return;
1698
1699         portp->flags &= ~ASYNC_INITIALIZED;
1700         stl_disableintrs(portp);
1701         if (tty->termios->c_cflag & HUPCL)
1702                 stl_setsignals(portp, 0, 0);
1703         stl_enablerxtx(portp, 0, 0);
1704         stl_flushbuffer(tty);
1705         portp->istate = 0;
1706         set_bit(TTY_IO_ERROR, &tty->flags);
1707         if (portp->tx.buf != (char *) NULL) {
1708                 kfree(portp->tx.buf);
1709                 portp->tx.buf = (char *) NULL;
1710                 portp->tx.head = (char *) NULL;
1711                 portp->tx.tail = (char *) NULL;
1712         }
1713         portp->tty = (struct tty_struct *) NULL;
1714         portp->flags &= ~ASYNC_NORMAL_ACTIVE;
1715         portp->refcount = 0;
1716         wake_up_interruptible(&portp->open_wait);
1717 }
1718
1719 /*****************************************************************************/
1720
1721 static void stl_flushbuffer(struct tty_struct *tty)
1722 {
1723         stlport_t       *portp;
1724
1725 #ifdef DEBUG
1726         printk("stl_flushbuffer(tty=%x)\n", (int) tty);
1727 #endif
1728
1729         if (tty == (struct tty_struct *) NULL)
1730                 return;
1731         portp = tty->driver_data;
1732         if (portp == (stlport_t *) NULL)
1733                 return;
1734
1735         stl_flush(portp);
1736         tty_wakeup(tty);
1737 }
1738
1739 /*****************************************************************************/
1740
1741 static void stl_breakctl(struct tty_struct *tty, int state)
1742 {
1743         stlport_t       *portp;
1744
1745 #ifdef DEBUG
1746         printk("stl_breakctl(tty=%x,state=%d)\n", (int) tty, state);
1747 #endif
1748
1749         if (tty == (struct tty_struct *) NULL)
1750                 return;
1751         portp = tty->driver_data;
1752         if (portp == (stlport_t *) NULL)
1753                 return;
1754
1755         stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1756 }
1757
1758 /*****************************************************************************/
1759
1760 static void stl_waituntilsent(struct tty_struct *tty, int timeout)
1761 {
1762         stlport_t       *portp;
1763         unsigned long   tend;
1764
1765 #ifdef DEBUG
1766         printk("stl_waituntilsent(tty=%x,timeout=%d)\n", (int) tty, timeout);
1767 #endif
1768
1769         if (tty == (struct tty_struct *) NULL)
1770                 return;
1771         portp = tty->driver_data;
1772         if (portp == (stlport_t *) NULL)
1773                 return;
1774
1775         if (timeout == 0)
1776                 timeout = HZ;
1777         tend = jiffies + timeout;
1778
1779         while (stl_datastate(portp)) {
1780                 if (signal_pending(current))
1781                         break;
1782                 msleep_interruptible(20);
1783                 if (time_after_eq(jiffies, tend))
1784                         break;
1785         }
1786 }
1787
1788 /*****************************************************************************/
1789
1790 static void stl_sendxchar(struct tty_struct *tty, char ch)
1791 {
1792         stlport_t       *portp;
1793
1794 #ifdef DEBUG
1795         printk("stl_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
1796 #endif
1797
1798         if (tty == (struct tty_struct *) NULL)
1799                 return;
1800         portp = tty->driver_data;
1801         if (portp == (stlport_t *) NULL)
1802                 return;
1803
1804         if (ch == STOP_CHAR(tty))
1805                 stl_sendflow(portp, 0);
1806         else if (ch == START_CHAR(tty))
1807                 stl_sendflow(portp, 1);
1808         else
1809                 stl_putchar(tty, ch);
1810 }
1811
1812 /*****************************************************************************/
1813
1814 #define MAXLINE         80
1815
1816 /*
1817  *      Format info for a specified port. The line is deliberately limited
1818  *      to 80 characters. (If it is too long it will be truncated, if too
1819  *      short then padded with spaces).
1820  */
1821
1822 static int stl_portinfo(stlport_t *portp, int portnr, char *pos)
1823 {
1824         char    *sp;
1825         int     sigs, cnt;
1826
1827         sp = pos;
1828         sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
1829                 portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1830                 (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1831
1832         if (portp->stats.rxframing)
1833                 sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
1834         if (portp->stats.rxparity)
1835                 sp += sprintf(sp, " pe:%d", (int) portp->stats.rxparity);
1836         if (portp->stats.rxbreaks)
1837                 sp += sprintf(sp, " brk:%d", (int) portp->stats.rxbreaks);
1838         if (portp->stats.rxoverrun)
1839                 sp += sprintf(sp, " oe:%d", (int) portp->stats.rxoverrun);
1840
1841         sigs = stl_getsignals(portp);
1842         cnt = sprintf(sp, "%s%s%s%s%s ",
1843                 (sigs & TIOCM_RTS) ? "|RTS" : "",
1844                 (sigs & TIOCM_CTS) ? "|CTS" : "",
1845                 (sigs & TIOCM_DTR) ? "|DTR" : "",
1846                 (sigs & TIOCM_CD) ? "|DCD" : "",
1847                 (sigs & TIOCM_DSR) ? "|DSR" : "");
1848         *sp = ' ';
1849         sp += cnt;
1850
1851         for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
1852                 *sp++ = ' ';
1853         if (cnt >= MAXLINE)
1854                 pos[(MAXLINE - 2)] = '+';
1855         pos[(MAXLINE - 1)] = '\n';
1856
1857         return MAXLINE;
1858 }
1859
1860 /*****************************************************************************/
1861
1862 /*
1863  *      Port info, read from the /proc file system.
1864  */
1865
1866 static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
1867 {
1868         stlbrd_t        *brdp;
1869         stlpanel_t      *panelp;
1870         stlport_t       *portp;
1871         int             brdnr, panelnr, portnr, totalport;
1872         int             curoff, maxoff;
1873         char            *pos;
1874
1875 #ifdef DEBUG
1876         printk("stl_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
1877                 "data=%x\n", (int) page, (int) start, (int) off, count,
1878                 (int) eof, (int) data);
1879 #endif
1880
1881         pos = page;
1882         totalport = 0;
1883         curoff = 0;
1884
1885         if (off == 0) {
1886                 pos += sprintf(pos, "%s: version %s", stl_drvtitle,
1887                         stl_drvversion);
1888                 while (pos < (page + MAXLINE - 1))
1889                         *pos++ = ' ';
1890                 *pos++ = '\n';
1891         }
1892         curoff =  MAXLINE;
1893
1894 /*
1895  *      We scan through for each board, panel and port. The offset is
1896  *      calculated on the fly, and irrelevant ports are skipped.
1897  */
1898         for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
1899                 brdp = stl_brds[brdnr];
1900                 if (brdp == (stlbrd_t *) NULL)
1901                         continue;
1902                 if (brdp->state == 0)
1903                         continue;
1904
1905                 maxoff = curoff + (brdp->nrports * MAXLINE);
1906                 if (off >= maxoff) {
1907                         curoff = maxoff;
1908                         continue;
1909                 }
1910
1911                 totalport = brdnr * STL_MAXPORTS;
1912                 for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1913                         panelp = brdp->panels[panelnr];
1914                         if (panelp == (stlpanel_t *) NULL)
1915                                 continue;
1916
1917                         maxoff = curoff + (panelp->nrports * MAXLINE);
1918                         if (off >= maxoff) {
1919                                 curoff = maxoff;
1920                                 totalport += panelp->nrports;
1921                                 continue;
1922                         }
1923
1924                         for (portnr = 0; (portnr < panelp->nrports); portnr++,
1925                             totalport++) {
1926                                 portp = panelp->ports[portnr];
1927                                 if (portp == (stlport_t *) NULL)
1928                                         continue;
1929                                 if (off >= (curoff += MAXLINE))
1930                                         continue;
1931                                 if ((pos - page + MAXLINE) > count)
1932                                         goto stl_readdone;
1933                                 pos += stl_portinfo(portp, totalport, pos);
1934                         }
1935                 }
1936         }
1937
1938         *eof = 1;
1939
1940 stl_readdone:
1941         *start = page;
1942         return (pos - page);
1943 }
1944
1945 /*****************************************************************************/
1946
1947 /*
1948  *      All board interrupts are vectored through here first. This code then
1949  *      calls off to the approrpriate board interrupt handlers.
1950  */
1951
1952 static irqreturn_t stl_intr(int irq, void *dev_id, struct pt_regs *regs)
1953 {
1954         stlbrd_t        *brdp = (stlbrd_t *) dev_id;
1955
1956 #ifdef DEBUG
1957         printk("stl_intr(brdp=%x,irq=%d,regs=%x)\n", (int) brdp, irq,
1958             (int) regs);
1959 #endif
1960
1961         return IRQ_RETVAL((* brdp->isr)(brdp));
1962 }
1963
1964 /*****************************************************************************/
1965
1966 /*
1967  *      Interrupt service routine for EasyIO board types.
1968  */
1969
1970 static int stl_eiointr(stlbrd_t *brdp)
1971 {
1972         stlpanel_t      *panelp;
1973         unsigned int    iobase;
1974         int             handled = 0;
1975
1976         panelp = brdp->panels[0];
1977         iobase = panelp->iobase;
1978         while (inb(brdp->iostatus) & EIO_INTRPEND) {
1979                 handled = 1;
1980                 (* panelp->isr)(panelp, iobase);
1981         }
1982         return handled;
1983 }
1984
1985 /*****************************************************************************/
1986
1987 /*
1988  *      Interrupt service routine for ECH-AT board types.
1989  */
1990
1991 static int stl_echatintr(stlbrd_t *brdp)
1992 {
1993         stlpanel_t      *panelp;
1994         unsigned int    ioaddr;
1995         int             bnknr;
1996         int             handled = 0;
1997
1998         outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
1999
2000         while (inb(brdp->iostatus) & ECH_INTRPEND) {
2001                 handled = 1;
2002                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2003                         ioaddr = brdp->bnkstataddr[bnknr];
2004                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2005                                 panelp = brdp->bnk2panel[bnknr];
2006                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2007                         }
2008                 }
2009         }
2010
2011         outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2012
2013         return handled;
2014 }
2015
2016 /*****************************************************************************/
2017
2018 /*
2019  *      Interrupt service routine for ECH-MCA board types.
2020  */
2021
2022 static int stl_echmcaintr(stlbrd_t *brdp)
2023 {
2024         stlpanel_t      *panelp;
2025         unsigned int    ioaddr;
2026         int             bnknr;
2027         int             handled = 0;
2028
2029         while (inb(brdp->iostatus) & ECH_INTRPEND) {
2030                 handled = 1;
2031                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2032                         ioaddr = brdp->bnkstataddr[bnknr];
2033                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2034                                 panelp = brdp->bnk2panel[bnknr];
2035                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2036                         }
2037                 }
2038         }
2039         return handled;
2040 }
2041
2042 /*****************************************************************************/
2043
2044 /*
2045  *      Interrupt service routine for ECH-PCI board types.
2046  */
2047
2048 static int stl_echpciintr(stlbrd_t *brdp)
2049 {
2050         stlpanel_t      *panelp;
2051         unsigned int    ioaddr;
2052         int             bnknr, recheck;
2053         int             handled = 0;
2054
2055         while (1) {
2056                 recheck = 0;
2057                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2058                         outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
2059                         ioaddr = brdp->bnkstataddr[bnknr];
2060                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2061                                 panelp = brdp->bnk2panel[bnknr];
2062                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2063                                 recheck++;
2064                                 handled = 1;
2065                         }
2066                 }
2067                 if (! recheck)
2068                         break;
2069         }
2070         return handled;
2071 }
2072
2073 /*****************************************************************************/
2074
2075 /*
2076  *      Interrupt service routine for ECH-8/64-PCI board types.
2077  */
2078
2079 static int stl_echpci64intr(stlbrd_t *brdp)
2080 {
2081         stlpanel_t      *panelp;
2082         unsigned int    ioaddr;
2083         int             bnknr;
2084         int             handled = 0;
2085
2086         while (inb(brdp->ioctrl) & 0x1) {
2087                 handled = 1;
2088                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2089                         ioaddr = brdp->bnkstataddr[bnknr];
2090                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
2091                                 panelp = brdp->bnk2panel[bnknr];
2092                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2093                         }
2094                 }
2095         }
2096
2097         return handled;
2098 }
2099
2100 /*****************************************************************************/
2101
2102 /*
2103  *      Service an off-level request for some channel.
2104  */
2105 static void stl_offintr(void *private)
2106 {
2107         stlport_t               *portp;
2108         struct tty_struct       *tty;
2109         unsigned int            oldsigs;
2110
2111         portp = private;
2112
2113 #ifdef DEBUG
2114         printk("stl_offintr(portp=%x)\n", (int) portp);
2115 #endif
2116
2117         if (portp == (stlport_t *) NULL)
2118                 return;
2119
2120         tty = portp->tty;
2121         if (tty == (struct tty_struct *) NULL)
2122                 return;
2123
2124         lock_kernel();
2125         if (test_bit(ASYI_TXLOW, &portp->istate)) {
2126                 tty_wakeup(tty);
2127         }
2128         if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
2129                 clear_bit(ASYI_DCDCHANGE, &portp->istate);
2130                 oldsigs = portp->sigs;
2131                 portp->sigs = stl_getsignals(portp);
2132                 if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
2133                         wake_up_interruptible(&portp->open_wait);
2134                 if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
2135                         if (portp->flags & ASYNC_CHECK_CD)
2136                                 tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
2137                 }
2138         }
2139         unlock_kernel();
2140 }
2141
2142 /*****************************************************************************/
2143
2144 /*
2145  *      Initialize all the ports on a panel.
2146  */
2147
2148 static int __init stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2149 {
2150         stlport_t       *portp;
2151         int             chipmask, i;
2152
2153 #ifdef DEBUG
2154         printk("stl_initports(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
2155 #endif
2156
2157         chipmask = stl_panelinit(brdp, panelp);
2158
2159 /*
2160  *      All UART's are initialized (if found!). Now go through and setup
2161  *      each ports data structures.
2162  */
2163         for (i = 0; (i < panelp->nrports); i++) {
2164                 portp = kzalloc(sizeof(stlport_t), GFP_KERNEL);
2165                 if (!portp) {
2166                         printk("STALLION: failed to allocate memory "
2167                                 "(size=%d)\n", sizeof(stlport_t));
2168                         break;
2169                 }
2170
2171                 portp->magic = STL_PORTMAGIC;
2172                 portp->portnr = i;
2173                 portp->brdnr = panelp->brdnr;
2174                 portp->panelnr = panelp->panelnr;
2175                 portp->uartp = panelp->uartp;
2176                 portp->clk = brdp->clk;
2177                 portp->baud_base = STL_BAUDBASE;
2178                 portp->close_delay = STL_CLOSEDELAY;
2179                 portp->closing_wait = 30 * HZ;
2180                 INIT_WORK(&portp->tqueue, stl_offintr, portp);
2181                 init_waitqueue_head(&portp->open_wait);
2182                 init_waitqueue_head(&portp->close_wait);
2183                 portp->stats.brd = portp->brdnr;
2184                 portp->stats.panel = portp->panelnr;
2185                 portp->stats.port = portp->portnr;
2186                 panelp->ports[i] = portp;
2187                 stl_portinit(brdp, panelp, portp);
2188         }
2189
2190         return(0);
2191 }
2192
2193 /*****************************************************************************/
2194
2195 /*
2196  *      Try to find and initialize an EasyIO board.
2197  */
2198
2199 static inline int stl_initeio(stlbrd_t *brdp)
2200 {
2201         stlpanel_t      *panelp;
2202         unsigned int    status;
2203         char            *name;
2204         int             rc;
2205
2206 #ifdef DEBUG
2207         printk("stl_initeio(brdp=%x)\n", (int) brdp);
2208 #endif
2209
2210         brdp->ioctrl = brdp->ioaddr1 + 1;
2211         brdp->iostatus = brdp->ioaddr1 + 2;
2212
2213         status = inb(brdp->iostatus);
2214         if ((status & EIO_IDBITMASK) == EIO_MK3)
2215                 brdp->ioctrl++;
2216
2217 /*
2218  *      Handle board specific stuff now. The real difference is PCI
2219  *      or not PCI.
2220  */
2221         if (brdp->brdtype == BRD_EASYIOPCI) {
2222                 brdp->iosize1 = 0x80;
2223                 brdp->iosize2 = 0x80;
2224                 name = "serial(EIO-PCI)";
2225                 outb(0x41, (brdp->ioaddr2 + 0x4c));
2226         } else {
2227                 brdp->iosize1 = 8;
2228                 name = "serial(EIO)";
2229                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2230                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2231                         printk("STALLION: invalid irq=%d for brd=%d\n",
2232                                 brdp->irq, brdp->brdnr);
2233                         return(-EINVAL);
2234                 }
2235                 outb((stl_vecmap[brdp->irq] | EIO_0WS |
2236                         ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
2237                         brdp->ioctrl);
2238         }
2239
2240         if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
2241                 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
2242                         "%x conflicts with another device\n", brdp->brdnr, 
2243                         brdp->ioaddr1);
2244                 return(-EBUSY);
2245         }
2246         
2247         if (brdp->iosize2 > 0)
2248                 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
2249                         printk(KERN_WARNING "STALLION: Warning, board %d I/O "
2250                                 "address %x conflicts with another device\n",
2251                                 brdp->brdnr, brdp->ioaddr2);
2252                         printk(KERN_WARNING "STALLION: Warning, also "
2253                                 "releasing board %d I/O address %x \n", 
2254                                 brdp->brdnr, brdp->ioaddr1);
2255                         release_region(brdp->ioaddr1, brdp->iosize1);
2256                         return(-EBUSY);
2257                 }
2258
2259 /*
2260  *      Everything looks OK, so let's go ahead and probe for the hardware.
2261  */
2262         brdp->clk = CD1400_CLK;
2263         brdp->isr = stl_eiointr;
2264
2265         switch (status & EIO_IDBITMASK) {
2266         case EIO_8PORTM:
2267                 brdp->clk = CD1400_CLK8M;
2268                 /* fall thru */
2269         case EIO_8PORTRS:
2270         case EIO_8PORTDI:
2271                 brdp->nrports = 8;
2272                 break;
2273         case EIO_4PORTRS:
2274                 brdp->nrports = 4;
2275                 break;
2276         case EIO_MK3:
2277                 switch (status & EIO_BRDMASK) {
2278                 case ID_BRD4:
2279                         brdp->nrports = 4;
2280                         break;
2281                 case ID_BRD8:
2282                         brdp->nrports = 8;
2283                         break;
2284                 case ID_BRD16:
2285                         brdp->nrports = 16;
2286                         break;
2287                 default:
2288                         return(-ENODEV);
2289                 }
2290                 break;
2291         default:
2292                 return(-ENODEV);
2293         }
2294
2295 /*
2296  *      We have verified that the board is actually present, so now we
2297  *      can complete the setup.
2298  */
2299
2300         panelp = kzalloc(sizeof(stlpanel_t), GFP_KERNEL);
2301         if (!panelp) {
2302                 printk(KERN_WARNING "STALLION: failed to allocate memory "
2303                         "(size=%d)\n", sizeof(stlpanel_t));
2304                 return -ENOMEM;
2305         }
2306
2307         panelp->magic = STL_PANELMAGIC;
2308         panelp->brdnr = brdp->brdnr;
2309         panelp->panelnr = 0;
2310         panelp->nrports = brdp->nrports;
2311         panelp->iobase = brdp->ioaddr1;
2312         panelp->hwid = status;
2313         if ((status & EIO_IDBITMASK) == EIO_MK3) {
2314                 panelp->uartp = (void *) &stl_sc26198uart;
2315                 panelp->isr = stl_sc26198intr;
2316         } else {
2317                 panelp->uartp = (void *) &stl_cd1400uart;
2318                 panelp->isr = stl_cd1400eiointr;
2319         }
2320
2321         brdp->panels[0] = panelp;
2322         brdp->nrpanels = 1;
2323         brdp->state |= BRD_FOUND;
2324         brdp->hwid = status;
2325         if (request_irq(brdp->irq, stl_intr, SA_SHIRQ, name, brdp) != 0) {
2326                 printk("STALLION: failed to register interrupt "
2327                     "routine for %s irq=%d\n", name, brdp->irq);
2328                 rc = -ENODEV;
2329         } else {
2330                 rc = 0;
2331         }
2332         return rc;
2333 }
2334
2335 /*****************************************************************************/
2336
2337 /*
2338  *      Try to find an ECH board and initialize it. This code is capable of
2339  *      dealing with all types of ECH board.
2340  */
2341
2342 static inline int stl_initech(stlbrd_t *brdp)
2343 {
2344         stlpanel_t      *panelp;
2345         unsigned int    status, nxtid, ioaddr, conflict;
2346         int             panelnr, banknr, i;
2347         char            *name;
2348
2349 #ifdef DEBUG
2350         printk("stl_initech(brdp=%x)\n", (int) brdp);
2351 #endif
2352
2353         status = 0;
2354         conflict = 0;
2355
2356 /*
2357  *      Set up the initial board register contents for boards. This varies a
2358  *      bit between the different board types. So we need to handle each
2359  *      separately. Also do a check that the supplied IRQ is good.
2360  */
2361         switch (brdp->brdtype) {
2362
2363         case BRD_ECH:
2364                 brdp->isr = stl_echatintr;
2365                 brdp->ioctrl = brdp->ioaddr1 + 1;
2366                 brdp->iostatus = brdp->ioaddr1 + 1;
2367                 status = inb(brdp->iostatus);
2368                 if ((status & ECH_IDBITMASK) != ECH_ID)
2369                         return(-ENODEV);
2370                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2371                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2372                         printk("STALLION: invalid irq=%d for brd=%d\n",
2373                                 brdp->irq, brdp->brdnr);
2374                         return(-EINVAL);
2375                 }
2376                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2377                 status |= (stl_vecmap[brdp->irq] << 1);
2378                 outb((status | ECH_BRDRESET), brdp->ioaddr1);
2379                 brdp->ioctrlval = ECH_INTENABLE |
2380                         ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2381                 for (i = 0; (i < 10); i++)
2382                         outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2383                 brdp->iosize1 = 2;
2384                 brdp->iosize2 = 32;
2385                 name = "serial(EC8/32)";
2386                 outb(status, brdp->ioaddr1);
2387                 break;
2388
2389         case BRD_ECHMC:
2390                 brdp->isr = stl_echmcaintr;
2391                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2392                 brdp->iostatus = brdp->ioctrl;
2393                 status = inb(brdp->iostatus);
2394                 if ((status & ECH_IDBITMASK) != ECH_ID)
2395                         return(-ENODEV);
2396                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2397                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2398                         printk("STALLION: invalid irq=%d for brd=%d\n",
2399                                 brdp->irq, brdp->brdnr);
2400                         return(-EINVAL);
2401                 }
2402                 outb(ECHMC_BRDRESET, brdp->ioctrl);
2403                 outb(ECHMC_INTENABLE, brdp->ioctrl);
2404                 brdp->iosize1 = 64;
2405                 name = "serial(EC8/32-MC)";
2406                 break;
2407
2408         case BRD_ECHPCI:
2409                 brdp->isr = stl_echpciintr;
2410                 brdp->ioctrl = brdp->ioaddr1 + 2;
2411                 brdp->iosize1 = 4;
2412                 brdp->iosize2 = 8;
2413                 name = "serial(EC8/32-PCI)";
2414                 break;
2415
2416         case BRD_ECH64PCI:
2417                 brdp->isr = stl_echpci64intr;
2418                 brdp->ioctrl = brdp->ioaddr2 + 0x40;
2419                 outb(0x43, (brdp->ioaddr1 + 0x4c));
2420                 brdp->iosize1 = 0x80;
2421                 brdp->iosize2 = 0x80;
2422                 name = "serial(EC8/64-PCI)";
2423                 break;
2424
2425         default:
2426                 printk("STALLION: unknown board type=%d\n", brdp->brdtype);
2427                 return(-EINVAL);
2428                 break;
2429         }
2430
2431 /*
2432  *      Check boards for possible IO address conflicts and return fail status 
2433  *      if an IO conflict found.
2434  */
2435         if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
2436                 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
2437                         "%x conflicts with another device\n", brdp->brdnr, 
2438                         brdp->ioaddr1);
2439                 return(-EBUSY);
2440         }
2441         
2442         if (brdp->iosize2 > 0)
2443                 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
2444                         printk(KERN_WARNING "STALLION: Warning, board %d I/O "
2445                                 "address %x conflicts with another device\n",
2446                                 brdp->brdnr, brdp->ioaddr2);
2447                         printk(KERN_WARNING "STALLION: Warning, also "
2448                                 "releasing board %d I/O address %x \n", 
2449                                 brdp->brdnr, brdp->ioaddr1);
2450                         release_region(brdp->ioaddr1, brdp->iosize1);
2451                         return(-EBUSY);
2452                 }
2453
2454 /*
2455  *      Scan through the secondary io address space looking for panels.
2456  *      As we find'em allocate and initialize panel structures for each.
2457  */
2458         brdp->clk = CD1400_CLK;
2459         brdp->hwid = status;
2460
2461         ioaddr = brdp->ioaddr2;
2462         banknr = 0;
2463         panelnr = 0;
2464         nxtid = 0;
2465
2466         for (i = 0; (i < STL_MAXPANELS); i++) {
2467                 if (brdp->brdtype == BRD_ECHPCI) {
2468                         outb(nxtid, brdp->ioctrl);
2469                         ioaddr = brdp->ioaddr2;
2470                 }
2471                 status = inb(ioaddr + ECH_PNLSTATUS);
2472                 if ((status & ECH_PNLIDMASK) != nxtid)
2473                         break;
2474                 panelp = kzalloc(sizeof(stlpanel_t), GFP_KERNEL);
2475                 if (!panelp) {
2476                         printk("STALLION: failed to allocate memory "
2477                                 "(size=%d)\n", sizeof(stlpanel_t));
2478                         break;
2479                 }
2480                 panelp->magic = STL_PANELMAGIC;
2481                 panelp->brdnr = brdp->brdnr;
2482                 panelp->panelnr = panelnr;
2483                 panelp->iobase = ioaddr;
2484                 panelp->pagenr = nxtid;
2485                 panelp->hwid = status;
2486                 brdp->bnk2panel[banknr] = panelp;
2487                 brdp->bnkpageaddr[banknr] = nxtid;
2488                 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2489
2490                 if (status & ECH_PNLXPID) {
2491                         panelp->uartp = (void *) &stl_sc26198uart;
2492                         panelp->isr = stl_sc26198intr;
2493                         if (status & ECH_PNL16PORT) {
2494                                 panelp->nrports = 16;
2495                                 brdp->bnk2panel[banknr] = panelp;
2496                                 brdp->bnkpageaddr[banknr] = nxtid;
2497                                 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2498                                         ECH_PNLSTATUS;
2499                         } else {
2500                                 panelp->nrports = 8;
2501                         }
2502                 } else {
2503                         panelp->uartp = (void *) &stl_cd1400uart;
2504                         panelp->isr = stl_cd1400echintr;
2505                         if (status & ECH_PNL16PORT) {
2506                                 panelp->nrports = 16;
2507                                 panelp->ackmask = 0x80;
2508                                 if (brdp->brdtype != BRD_ECHPCI)
2509                                         ioaddr += EREG_BANKSIZE;
2510                                 brdp->bnk2panel[banknr] = panelp;
2511                                 brdp->bnkpageaddr[banknr] = ++nxtid;
2512                                 brdp->bnkstataddr[banknr++] = ioaddr +
2513                                         ECH_PNLSTATUS;
2514                         } else {
2515                                 panelp->nrports = 8;
2516                                 panelp->ackmask = 0xc0;
2517                         }
2518                 }
2519
2520                 nxtid++;
2521                 ioaddr += EREG_BANKSIZE;
2522                 brdp->nrports += panelp->nrports;
2523                 brdp->panels[panelnr++] = panelp;
2524                 if ((brdp->brdtype != BRD_ECHPCI) &&
2525                     (ioaddr >= (brdp->ioaddr2 + brdp->iosize2)))
2526                         break;
2527         }
2528
2529         brdp->nrpanels = panelnr;
2530         brdp->nrbnks = banknr;
2531         if (brdp->brdtype == BRD_ECH)
2532                 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2533
2534         brdp->state |= BRD_FOUND;
2535         if (request_irq(brdp->irq, stl_intr, SA_SHIRQ, name, brdp) != 0) {
2536                 printk("STALLION: failed to register interrupt "
2537                     "routine for %s irq=%d\n", name, brdp->irq);
2538                 i = -ENODEV;
2539         } else {
2540                 i = 0;
2541         }
2542
2543         return(i);
2544 }
2545
2546 /*****************************************************************************/
2547
2548 /*
2549  *      Initialize and configure the specified board.
2550  *      Scan through all the boards in the configuration and see what we
2551  *      can find. Handle EIO and the ECH boards a little differently here
2552  *      since the initial search and setup is very different.
2553  */
2554
2555 static int __init stl_brdinit(stlbrd_t *brdp)
2556 {
2557         int     i;
2558
2559 #ifdef DEBUG
2560         printk("stl_brdinit(brdp=%x)\n", (int) brdp);
2561 #endif
2562
2563         switch (brdp->brdtype) {
2564         case BRD_EASYIO:
2565         case BRD_EASYIOPCI:
2566                 stl_initeio(brdp);
2567                 break;
2568         case BRD_ECH:
2569         case BRD_ECHMC:
2570         case BRD_ECHPCI:
2571         case BRD_ECH64PCI:
2572                 stl_initech(brdp);
2573                 break;
2574         default:
2575                 printk("STALLION: board=%d is unknown board type=%d\n",
2576                         brdp->brdnr, brdp->brdtype);
2577                 return(ENODEV);
2578         }
2579
2580         stl_brds[brdp->brdnr] = brdp;
2581         if ((brdp->state & BRD_FOUND) == 0) {
2582                 printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
2583                         stl_brdnames[brdp->brdtype], brdp->brdnr,
2584                         brdp->ioaddr1, brdp->irq);
2585                 return(ENODEV);
2586         }
2587
2588         for (i = 0; (i < STL_MAXPANELS); i++)
2589                 if (brdp->panels[i] != (stlpanel_t *) NULL)
2590                         stl_initports(brdp, brdp->panels[i]);
2591
2592         printk("STALLION: %s found, board=%d io=%x irq=%d "
2593                 "nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
2594                 brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
2595                 brdp->nrports);
2596         return(0);
2597 }
2598
2599 /*****************************************************************************/
2600
2601 /*
2602  *      Find the next available board number that is free.
2603  */
2604
2605 static inline int stl_getbrdnr(void)
2606 {
2607         int     i;
2608
2609         for (i = 0; (i < STL_MAXBRDS); i++) {
2610                 if (stl_brds[i] == (stlbrd_t *) NULL) {
2611                         if (i >= stl_nrbrds)
2612                                 stl_nrbrds = i + 1;
2613                         return(i);
2614                 }
2615         }
2616         return(-1);
2617 }
2618
2619 /*****************************************************************************/
2620
2621 #ifdef  CONFIG_PCI
2622
2623 /*
2624  *      We have a Stallion board. Allocate a board structure and
2625  *      initialize it. Read its IO and IRQ resources from PCI
2626  *      configuration space.
2627  */
2628
2629 static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp)
2630 {
2631         stlbrd_t        *brdp;
2632
2633 #ifdef DEBUG
2634         printk("stl_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n", brdtype,
2635                 devp->bus->number, devp->devfn);
2636 #endif
2637
2638         if (pci_enable_device(devp))
2639                 return(-EIO);
2640         if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2641                 return(-ENOMEM);
2642         if ((brdp->brdnr = stl_getbrdnr()) < 0) {
2643                 printk("STALLION: too many boards found, "
2644                         "maximum supported %d\n", STL_MAXBRDS);
2645                 return(0);
2646         }
2647         brdp->brdtype = brdtype;
2648
2649 /*
2650  *      Different Stallion boards use the BAR registers in different ways,
2651  *      so set up io addresses based on board type.
2652  */
2653 #ifdef DEBUG
2654         printk("%s(%d): BAR[]=%x,%x,%x,%x IRQ=%x\n", __FILE__, __LINE__,
2655                 pci_resource_start(devp, 0), pci_resource_start(devp, 1),
2656                 pci_resource_start(devp, 2), pci_resource_start(devp, 3), devp->irq);
2657 #endif
2658
2659 /*
2660  *      We have all resources from the board, so let's setup the actual
2661  *      board structure now.
2662  */
2663         switch (brdtype) {
2664         case BRD_ECHPCI:
2665                 brdp->ioaddr2 = pci_resource_start(devp, 0);
2666                 brdp->ioaddr1 = pci_resource_start(devp, 1);
2667                 break;
2668         case BRD_ECH64PCI:
2669                 brdp->ioaddr2 = pci_resource_start(devp, 2);
2670                 brdp->ioaddr1 = pci_resource_start(devp, 1);
2671                 break;
2672         case BRD_EASYIOPCI:
2673                 brdp->ioaddr1 = pci_resource_start(devp, 2);
2674                 brdp->ioaddr2 = pci_resource_start(devp, 1);
2675                 break;
2676         default:
2677                 printk("STALLION: unknown PCI board type=%d\n", brdtype);
2678                 break;
2679         }
2680
2681         brdp->irq = devp->irq;
2682         stl_brdinit(brdp);
2683
2684         return(0);
2685 }
2686
2687 /*****************************************************************************/
2688
2689 /*
2690  *      Find all Stallion PCI boards that might be installed. Initialize each
2691  *      one as it is found.
2692  */
2693
2694
2695 static inline int stl_findpcibrds(void)
2696 {
2697         struct pci_dev  *dev = NULL;
2698         int             i, rc;
2699
2700 #ifdef DEBUG
2701         printk("stl_findpcibrds()\n");
2702 #endif
2703
2704         for (i = 0; (i < stl_nrpcibrds); i++)
2705                 while ((dev = pci_find_device(stl_pcibrds[i].vendid,
2706                     stl_pcibrds[i].devid, dev))) {
2707
2708 /*
2709  *                      Found a device on the PCI bus that has our vendor and
2710  *                      device ID. Need to check now that it is really us.
2711  */
2712                         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2713                                 continue;
2714
2715                         rc = stl_initpcibrd(stl_pcibrds[i].brdtype, dev);
2716                         if (rc)
2717                                 return(rc);
2718                 }
2719
2720         return(0);
2721 }
2722
2723 #endif
2724
2725 /*****************************************************************************/
2726
2727 /*
2728  *      Scan through all the boards in the configuration and see what we
2729  *      can find. Handle EIO and the ECH boards a little differently here
2730  *      since the initial search and setup is too different.
2731  */
2732
2733 static inline int stl_initbrds(void)
2734 {
2735         stlbrd_t        *brdp;
2736         stlconf_t       *confp;
2737         int             i;
2738
2739 #ifdef DEBUG
2740         printk("stl_initbrds()\n");
2741 #endif
2742
2743         if (stl_nrbrds > STL_MAXBRDS) {
2744                 printk("STALLION: too many boards in configuration table, "
2745                         "truncating to %d\n", STL_MAXBRDS);
2746                 stl_nrbrds = STL_MAXBRDS;
2747         }
2748
2749 /*
2750  *      Firstly scan the list of static boards configured. Allocate
2751  *      resources and initialize the boards as found.
2752  */
2753         for (i = 0; (i < stl_nrbrds); i++) {
2754                 confp = &stl_brdconf[i];
2755                 stl_parsebrd(confp, stl_brdsp[i]);
2756                 if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2757                         return(-ENOMEM);
2758                 brdp->brdnr = i;
2759                 brdp->brdtype = confp->brdtype;
2760                 brdp->ioaddr1 = confp->ioaddr1;
2761                 brdp->ioaddr2 = confp->ioaddr2;
2762                 brdp->irq = confp->irq;
2763                 brdp->irqtype = confp->irqtype;
2764                 stl_brdinit(brdp);
2765         }
2766
2767 /*
2768  *      Find any dynamically supported boards. That is via module load
2769  *      line options or auto-detected on the PCI bus.
2770  */
2771         stl_argbrds();
2772 #ifdef CONFIG_PCI
2773         stl_findpcibrds();
2774 #endif
2775
2776         return(0);
2777 }
2778
2779 /*****************************************************************************/
2780
2781 /*
2782  *      Return the board stats structure to user app.
2783  */
2784
2785 static int stl_getbrdstats(combrd_t __user *bp)
2786 {
2787         stlbrd_t        *brdp;
2788         stlpanel_t      *panelp;
2789         int             i;
2790
2791         if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
2792                 return -EFAULT;
2793         if (stl_brdstats.brd >= STL_MAXBRDS)
2794                 return(-ENODEV);
2795         brdp = stl_brds[stl_brdstats.brd];
2796         if (brdp == (stlbrd_t *) NULL)
2797                 return(-ENODEV);
2798
2799         memset(&stl_brdstats, 0, sizeof(combrd_t));
2800         stl_brdstats.brd = brdp->brdnr;
2801         stl_brdstats.type = brdp->brdtype;
2802         stl_brdstats.hwid = brdp->hwid;
2803         stl_brdstats.state = brdp->state;
2804         stl_brdstats.ioaddr = brdp->ioaddr1;
2805         stl_brdstats.ioaddr2 = brdp->ioaddr2;
2806         stl_brdstats.irq = brdp->irq;
2807         stl_brdstats.nrpanels = brdp->nrpanels;
2808         stl_brdstats.nrports = brdp->nrports;
2809         for (i = 0; (i < brdp->nrpanels); i++) {
2810                 panelp = brdp->panels[i];
2811                 stl_brdstats.panels[i].panel = i;
2812                 stl_brdstats.panels[i].hwid = panelp->hwid;
2813                 stl_brdstats.panels[i].nrports = panelp->nrports;
2814         }
2815
2816         return copy_to_user(bp, &stl_brdstats, sizeof(combrd_t)) ? -EFAULT : 0;
2817 }
2818
2819 /*****************************************************************************/
2820
2821 /*
2822  *      Resolve the referenced port number into a port struct pointer.
2823  */
2824
2825 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2826 {
2827         stlbrd_t        *brdp;
2828         stlpanel_t      *panelp;
2829
2830         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2831                 return((stlport_t *) NULL);
2832         brdp = stl_brds[brdnr];
2833         if (brdp == (stlbrd_t *) NULL)
2834                 return((stlport_t *) NULL);
2835         if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2836                 return((stlport_t *) NULL);
2837         panelp = brdp->panels[panelnr];
2838         if (panelp == (stlpanel_t *) NULL)
2839                 return((stlport_t *) NULL);
2840         if ((portnr < 0) || (portnr >= panelp->nrports))
2841                 return((stlport_t *) NULL);
2842         return(panelp->ports[portnr]);
2843 }
2844
2845 /*****************************************************************************/
2846
2847 /*
2848  *      Return the port stats structure to user app. A NULL port struct
2849  *      pointer passed in means that we need to find out from the app
2850  *      what port to get stats for (used through board control device).
2851  */
2852
2853 static int stl_getportstats(stlport_t *portp, comstats_t __user *cp)
2854 {
2855         unsigned char   *head, *tail;
2856         unsigned long   flags;
2857
2858         if (!portp) {
2859                 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2860                         return -EFAULT;
2861                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2862                         stl_comstats.port);
2863                 if (portp == (stlport_t *) NULL)
2864                         return(-ENODEV);
2865         }
2866
2867         portp->stats.state = portp->istate;
2868         portp->stats.flags = portp->flags;
2869         portp->stats.hwid = portp->hwid;
2870
2871         portp->stats.ttystate = 0;
2872         portp->stats.cflags = 0;
2873         portp->stats.iflags = 0;
2874         portp->stats.oflags = 0;
2875         portp->stats.lflags = 0;
2876         portp->stats.rxbuffered = 0;
2877
2878         save_flags(flags);
2879         cli();
2880         if (portp->tty != (struct tty_struct *) NULL) {
2881                 if (portp->tty->driver_data == portp) {
2882                         portp->stats.ttystate = portp->tty->flags;
2883                         /* No longer available as a statistic */
2884                         portp->stats.rxbuffered = 1; /*portp->tty->flip.count; */
2885                         if (portp->tty->termios != (struct termios *) NULL) {
2886                                 portp->stats.cflags = portp->tty->termios->c_cflag;
2887                                 portp->stats.iflags = portp->tty->termios->c_iflag;
2888                                 portp->stats.oflags = portp->tty->termios->c_oflag;
2889                                 portp->stats.lflags = portp->tty->termios->c_lflag;
2890                         }
2891                 }
2892         }
2893         restore_flags(flags);
2894
2895         head = portp->tx.head;
2896         tail = portp->tx.tail;
2897         portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
2898                 (STL_TXBUFSIZE - (tail - head)));
2899
2900         portp->stats.signals = (unsigned long) stl_getsignals(portp);
2901
2902         return copy_to_user(cp, &portp->stats,
2903                             sizeof(comstats_t)) ? -EFAULT : 0;
2904 }
2905
2906 /*****************************************************************************/
2907
2908 /*
2909  *      Clear the port stats structure. We also return it zeroed out...
2910  */
2911
2912 static int stl_clrportstats(stlport_t *portp, comstats_t __user *cp)
2913 {
2914         if (!portp) {
2915                 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2916                         return -EFAULT;
2917                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2918                         stl_comstats.port);
2919                 if (portp == (stlport_t *) NULL)
2920                         return(-ENODEV);
2921         }
2922
2923         memset(&portp->stats, 0, sizeof(comstats_t));
2924         portp->stats.brd = portp->brdnr;
2925         portp->stats.panel = portp->panelnr;
2926         portp->stats.port = portp->portnr;
2927         return copy_to_user(cp, &portp->stats,
2928                             sizeof(comstats_t)) ? -EFAULT : 0;
2929 }
2930
2931 /*****************************************************************************/
2932
2933 /*
2934  *      Return the entire driver ports structure to a user app.
2935  */
2936
2937 static int stl_getportstruct(stlport_t __user *arg)
2938 {
2939         stlport_t       *portp;
2940
2941         if (copy_from_user(&stl_dummyport, arg, sizeof(stlport_t)))
2942                 return -EFAULT;
2943         portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
2944                  stl_dummyport.portnr);
2945         if (!portp)
2946                 return -ENODEV;
2947         return copy_to_user(arg, portp, sizeof(stlport_t)) ? -EFAULT : 0;
2948 }
2949
2950 /*****************************************************************************/
2951
2952 /*
2953  *      Return the entire driver board structure to a user app.
2954  */
2955
2956 static int stl_getbrdstruct(stlbrd_t __user *arg)
2957 {
2958         stlbrd_t        *brdp;
2959
2960         if (copy_from_user(&stl_dummybrd, arg, sizeof(stlbrd_t)))
2961                 return -EFAULT;
2962         if ((stl_dummybrd.brdnr < 0) || (stl_dummybrd.brdnr >= STL_MAXBRDS))
2963                 return -ENODEV;
2964         brdp = stl_brds[stl_dummybrd.brdnr];
2965         if (!brdp)
2966                 return(-ENODEV);
2967         return copy_to_user(arg, brdp, sizeof(stlbrd_t)) ? -EFAULT : 0;
2968 }
2969
2970 /*****************************************************************************/
2971
2972 /*
2973  *      The "staliomem" device is also required to do some special operations
2974  *      on the board and/or ports. In this driver it is mostly used for stats
2975  *      collection.
2976  */
2977
2978 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
2979 {
2980         int     brdnr, rc;
2981         void __user *argp = (void __user *)arg;
2982
2983 #ifdef DEBUG
2984         printk("stl_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n", (int) ip,
2985                 (int) fp, cmd, (int) arg);
2986 #endif
2987
2988         brdnr = iminor(ip);
2989         if (brdnr >= STL_MAXBRDS)
2990                 return(-ENODEV);
2991         rc = 0;
2992
2993         switch (cmd) {
2994         case COM_GETPORTSTATS:
2995                 rc = stl_getportstats(NULL, argp);
2996                 break;
2997         case COM_CLRPORTSTATS:
2998                 rc = stl_clrportstats(NULL, argp);
2999                 break;
3000         case COM_GETBRDSTATS:
3001                 rc = stl_getbrdstats(argp);
3002                 break;
3003         case COM_READPORT:
3004                 rc = stl_getportstruct(argp);
3005                 break;
3006         case COM_READBOARD:
3007                 rc = stl_getbrdstruct(argp);
3008                 break;
3009         default:
3010                 rc = -ENOIOCTLCMD;
3011                 break;
3012         }
3013
3014         return(rc);
3015 }
3016
3017 static struct tty_operations stl_ops = {
3018         .open = stl_open,
3019         .close = stl_close,
3020         .write = stl_write,
3021         .put_char = stl_putchar,
3022         .flush_chars = stl_flushchars,
3023         .write_room = stl_writeroom,
3024         .chars_in_buffer = stl_charsinbuffer,
3025         .ioctl = stl_ioctl,
3026         .set_termios = stl_settermios,
3027         .throttle = stl_throttle,
3028         .unthrottle = stl_unthrottle,
3029         .stop = stl_stop,
3030         .start = stl_start,
3031         .hangup = stl_hangup,
3032         .flush_buffer = stl_flushbuffer,
3033         .break_ctl = stl_breakctl,
3034         .wait_until_sent = stl_waituntilsent,
3035         .send_xchar = stl_sendxchar,
3036         .read_proc = stl_readproc,
3037         .tiocmget = stl_tiocmget,
3038         .tiocmset = stl_tiocmset,
3039 };
3040
3041 /*****************************************************************************/
3042
3043 static int __init stl_init(void)
3044 {
3045         int i;
3046         printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
3047
3048         stl_initbrds();
3049
3050         stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
3051         if (!stl_serial)
3052                 return -1;
3053
3054 /*
3055  *      Allocate a temporary write buffer.
3056  */
3057         stl_tmpwritebuf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
3058         if (!stl_tmpwritebuf)
3059                 printk("STALLION: failed to allocate memory (size=%d)\n",
3060                         STL_TXBUFSIZE);
3061
3062 /*
3063  *      Set up a character driver for per board stuff. This is mainly used
3064  *      to do stats ioctls on the ports.
3065  */
3066         if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
3067                 printk("STALLION: failed to register serial board device\n");
3068
3069         stallion_class = class_create(THIS_MODULE, "staliomem");
3070         for (i = 0; i < 4; i++)
3071                 class_device_create(stallion_class, NULL,
3072                                     MKDEV(STL_SIOMEMMAJOR, i), NULL,
3073                                     "staliomem%d", i);
3074
3075         stl_serial->owner = THIS_MODULE;
3076         stl_serial->driver_name = stl_drvname;
3077         stl_serial->name = "ttyE";
3078         stl_serial->major = STL_SERIALMAJOR;
3079         stl_serial->minor_start = 0;
3080         stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
3081         stl_serial->subtype = SERIAL_TYPE_NORMAL;
3082         stl_serial->init_termios = stl_deftermios;
3083         stl_serial->flags = TTY_DRIVER_REAL_RAW;
3084         tty_set_operations(stl_serial, &stl_ops);
3085
3086         if (tty_register_driver(stl_serial)) {
3087                 put_tty_driver(stl_serial);
3088                 printk("STALLION: failed to register serial driver\n");
3089                 return -1;
3090         }
3091
3092         return 0;
3093 }
3094
3095 /*****************************************************************************/
3096 /*                       CD1400 HARDWARE FUNCTIONS                           */
3097 /*****************************************************************************/
3098
3099 /*
3100  *      These functions get/set/update the registers of the cd1400 UARTs.
3101  *      Access to the cd1400 registers is via an address/data io port pair.
3102  *      (Maybe should make this inline...)
3103  */
3104
3105 static int stl_cd1400getreg(stlport_t *portp, int regnr)
3106 {
3107         outb((regnr + portp->uartaddr), portp->ioaddr);
3108         return inb(portp->ioaddr + EREG_DATA);
3109 }
3110
3111 static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
3112 {
3113         outb((regnr + portp->uartaddr), portp->ioaddr);
3114         outb(value, portp->ioaddr + EREG_DATA);
3115 }
3116
3117 static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
3118 {
3119         outb((regnr + portp->uartaddr), portp->ioaddr);
3120         if (inb(portp->ioaddr + EREG_DATA) != value) {
3121                 outb(value, portp->ioaddr + EREG_DATA);
3122                 return 1;
3123         }
3124         return 0;
3125 }
3126
3127 /*****************************************************************************/
3128
3129 /*
3130  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3131  *      these ports are on - since the port io registers are almost
3132  *      identical when dealing with ports.
3133  */
3134
3135 static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3136 {
3137         unsigned int    gfrcr;
3138         int             chipmask, i, j;
3139         int             nrchips, uartaddr, ioaddr;
3140
3141 #ifdef DEBUG
3142         printk("stl_panelinit(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
3143 #endif
3144
3145         BRDENABLE(panelp->brdnr, panelp->pagenr);
3146
3147 /*
3148  *      Check that each chip is present and started up OK.
3149  */
3150         chipmask = 0;
3151         nrchips = panelp->nrports / CD1400_PORTS;
3152         for (i = 0; (i < nrchips); i++) {
3153                 if (brdp->brdtype == BRD_ECHPCI) {
3154                         outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
3155                         ioaddr = panelp->iobase;
3156                 } else {
3157                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
3158                 }
3159                 uartaddr = (i & 0x01) ? 0x080 : 0;
3160                 outb((GFRCR + uartaddr), ioaddr);
3161                 outb(0, (ioaddr + EREG_DATA));
3162                 outb((CCR + uartaddr), ioaddr);
3163                 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3164                 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3165                 outb((GFRCR + uartaddr), ioaddr);
3166                 for (j = 0; (j < CCR_MAXWAIT); j++) {
3167                         if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
3168                                 break;
3169                 }
3170                 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
3171                         printk("STALLION: cd1400 not responding, "
3172                                 "brd=%d panel=%d chip=%d\n",
3173                                 panelp->brdnr, panelp->panelnr, i);
3174                         continue;
3175                 }
3176                 chipmask |= (0x1 << i);
3177                 outb((PPR + uartaddr), ioaddr);
3178                 outb(PPR_SCALAR, (ioaddr + EREG_DATA));
3179         }
3180
3181         BRDDISABLE(panelp->brdnr);
3182         return chipmask;
3183 }
3184
3185 /*****************************************************************************/
3186
3187 /*
3188  *      Initialize hardware specific port registers.
3189  */
3190
3191 static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3192 {
3193 #ifdef DEBUG
3194         printk("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
3195                 (int) brdp, (int) panelp, (int) portp);
3196 #endif
3197
3198         if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
3199             (portp == (stlport_t *) NULL))
3200                 return;
3201
3202         portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
3203                 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
3204         portp->uartaddr = (portp->portnr & 0x04) << 5;
3205         portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
3206
3207         BRDENABLE(portp->brdnr, portp->pagenr);
3208         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3209         stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
3210         portp->hwid = stl_cd1400getreg(portp, GFRCR);
3211         BRDDISABLE(portp->brdnr);
3212 }
3213
3214 /*****************************************************************************/
3215
3216 /*
3217  *      Wait for the command register to be ready. We will poll this,
3218  *      since it won't usually take too long to be ready.
3219  */
3220
3221 static void stl_cd1400ccrwait(stlport_t *portp)
3222 {
3223         int     i;
3224
3225         for (i = 0; (i < CCR_MAXWAIT); i++) {
3226                 if (stl_cd1400getreg(portp, CCR) == 0) {
3227                         return;
3228                 }
3229         }
3230
3231         printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
3232                 portp->portnr, portp->panelnr, portp->brdnr);
3233 }
3234
3235 /*****************************************************************************/
3236
3237 /*
3238  *      Set up the cd1400 registers for a port based on the termios port
3239  *      settings.
3240  */
3241
3242 static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
3243 {
3244         stlbrd_t        *brdp;
3245         unsigned long   flags;
3246         unsigned int    clkdiv, baudrate;
3247         unsigned char   cor1, cor2, cor3;
3248         unsigned char   cor4, cor5, ccr;
3249         unsigned char   srer, sreron, sreroff;
3250         unsigned char   mcor1, mcor2, rtpr;
3251         unsigned char   clk, div;
3252
3253         cor1 = 0;
3254         cor2 = 0;
3255         cor3 = 0;
3256         cor4 = 0;
3257         cor5 = 0;
3258         ccr = 0;
3259         rtpr = 0;
3260         clk = 0;
3261         div = 0;
3262         mcor1 = 0;
3263         mcor2 = 0;
3264         sreron = 0;
3265         sreroff = 0;
3266
3267         brdp = stl_brds[portp->brdnr];
3268         if (brdp == (stlbrd_t *) NULL)
3269                 return;
3270
3271 /*
3272  *      Set up the RX char ignore mask with those RX error types we
3273  *      can ignore. We can get the cd1400 to help us out a little here,
3274  *      it will ignore parity errors and breaks for us.
3275  */
3276         portp->rxignoremsk = 0;
3277         if (tiosp->c_iflag & IGNPAR) {
3278                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
3279                 cor1 |= COR1_PARIGNORE;
3280         }
3281         if (tiosp->c_iflag & IGNBRK) {
3282                 portp->rxignoremsk |= ST_BREAK;
3283                 cor4 |= COR4_IGNBRK;
3284         }
3285
3286         portp->rxmarkmsk = ST_OVERRUN;
3287         if (tiosp->c_iflag & (INPCK | PARMRK))
3288                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
3289         if (tiosp->c_iflag & BRKINT)
3290                 portp->rxmarkmsk |= ST_BREAK;
3291
3292 /*
3293  *      Go through the char size, parity and stop bits and set all the
3294  *      option register appropriately.
3295  */
3296         switch (tiosp->c_cflag & CSIZE) {
3297         case CS5:
3298                 cor1 |= COR1_CHL5;
3299                 break;
3300         case CS6:
3301                 cor1 |= COR1_CHL6;
3302                 break;
3303         case CS7:
3304                 cor1 |= COR1_CHL7;
3305                 break;
3306         default:
3307                 cor1 |= COR1_CHL8;
3308                 break;
3309         }
3310
3311         if (tiosp->c_cflag & CSTOPB)
3312                 cor1 |= COR1_STOP2;
3313         else
3314                 cor1 |= COR1_STOP1;
3315
3316         if (tiosp->c_cflag & PARENB) {
3317                 if (tiosp->c_cflag & PARODD)
3318                         cor1 |= (COR1_PARENB | COR1_PARODD);
3319                 else
3320                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
3321         } else {
3322                 cor1 |= COR1_PARNONE;
3323         }
3324
3325 /*
3326  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
3327  *      space for hardware flow control and the like. This should be set to
3328  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
3329  *      really be based on VTIME.
3330  */
3331         cor3 |= FIFO_RXTHRESHOLD;
3332         rtpr = 2;
3333
3334 /*
3335  *      Calculate the baud rate timers. For now we will just assume that
3336  *      the input and output baud are the same. Could have used a baud
3337  *      table here, but this way we can generate virtually any baud rate
3338  *      we like!
3339  */
3340         baudrate = tiosp->c_cflag & CBAUD;
3341         if (baudrate & CBAUDEX) {
3342                 baudrate &= ~CBAUDEX;
3343                 if ((baudrate < 1) || (baudrate > 4))
3344                         tiosp->c_cflag &= ~CBAUDEX;
3345                 else
3346                         baudrate += 15;
3347         }
3348         baudrate = stl_baudrates[baudrate];
3349         if ((tiosp->c_cflag & CBAUD) == B38400) {
3350                 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3351                         baudrate = 57600;
3352                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3353                         baudrate = 115200;
3354                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3355                         baudrate = 230400;
3356                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3357                         baudrate = 460800;
3358                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3359                         baudrate = (portp->baud_base / portp->custom_divisor);
3360         }
3361         if (baudrate > STL_CD1400MAXBAUD)
3362                 baudrate = STL_CD1400MAXBAUD;
3363
3364         if (baudrate > 0) {
3365                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
3366                         clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) / baudrate);
3367                         if (clkdiv < 0x100)
3368                                 break;
3369                 }
3370                 div = (unsigned char) clkdiv;
3371         }
3372
3373 /*
3374  *      Check what form of modem signaling is required and set it up.
3375  */
3376         if ((tiosp->c_cflag & CLOCAL) == 0) {
3377                 mcor1 |= MCOR1_DCD;
3378                 mcor2 |= MCOR2_DCD;
3379                 sreron |= SRER_MODEM;
3380                 portp->flags |= ASYNC_CHECK_CD;
3381         } else {
3382                 portp->flags &= ~ASYNC_CHECK_CD;
3383         }
3384
3385 /*
3386  *      Setup cd1400 enhanced modes if we can. In particular we want to
3387  *      handle as much of the flow control as possible automatically. As
3388  *      well as saving a few CPU cycles it will also greatly improve flow
3389  *      control reliability.
3390  */
3391         if (tiosp->c_iflag & IXON) {
3392                 cor2 |= COR2_TXIBE;
3393                 cor3 |= COR3_SCD12;
3394                 if (tiosp->c_iflag & IXANY)
3395                         cor2 |= COR2_IXM;
3396         }
3397
3398         if (tiosp->c_cflag & CRTSCTS) {
3399                 cor2 |= COR2_CTSAE;
3400                 mcor1 |= FIFO_RTSTHRESHOLD;
3401         }
3402
3403 /*
3404  *      All cd1400 register values calculated so go through and set
3405  *      them all up.
3406  */
3407
3408 #ifdef DEBUG
3409         printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
3410                 portp->portnr, portp->panelnr, portp->brdnr);
3411         printk("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
3412                 cor1, cor2, cor3, cor4, cor5);
3413         printk("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
3414                 mcor1, mcor2, rtpr, sreron, sreroff);
3415         printk("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
3416         printk("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
3417                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3418                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3419 #endif
3420
3421         save_flags(flags);
3422         cli();
3423         BRDENABLE(portp->brdnr, portp->pagenr);
3424         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3425         srer = stl_cd1400getreg(portp, SRER);
3426         stl_cd1400setreg(portp, SRER, 0);
3427         if (stl_cd1400updatereg(portp, COR1, cor1))
3428                 ccr = 1;
3429         if (stl_cd1400updatereg(portp, COR2, cor2))
3430                 ccr = 1;
3431         if (stl_cd1400updatereg(portp, COR3, cor3))
3432                 ccr = 1;
3433         if (ccr) {
3434                 stl_cd1400ccrwait(portp);
3435                 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3436         }
3437         stl_cd1400setreg(portp, COR4, cor4);
3438         stl_cd1400setreg(portp, COR5, cor5);
3439         stl_cd1400setreg(portp, MCOR1, mcor1);
3440         stl_cd1400setreg(portp, MCOR2, mcor2);
3441         if (baudrate > 0) {
3442                 stl_cd1400setreg(portp, TCOR, clk);
3443                 stl_cd1400setreg(portp, TBPR, div);
3444                 stl_cd1400setreg(portp, RCOR, clk);
3445                 stl_cd1400setreg(portp, RBPR, div);
3446         }
3447         stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3448         stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3449         stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3450         stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3451         stl_cd1400setreg(portp, RTPR, rtpr);
3452         mcor1 = stl_cd1400getreg(portp, MSVR1);
3453         if (mcor1 & MSVR1_DCD)
3454                 portp->sigs |= TIOCM_CD;
3455         else
3456                 portp->sigs &= ~TIOCM_CD;
3457         stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3458         BRDDISABLE(portp->brdnr);
3459         restore_flags(flags);
3460 }
3461
3462 /*****************************************************************************/
3463
3464 /*
3465  *      Set the state of the DTR and RTS signals.
3466  */
3467
3468 static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
3469 {
3470         unsigned char   msvr1, msvr2;
3471         unsigned long   flags;
3472
3473 #ifdef DEBUG
3474         printk("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n",
3475                 (int) portp, dtr, rts);
3476 #endif
3477
3478         msvr1 = 0;
3479         msvr2 = 0;
3480         if (dtr > 0)
3481                 msvr1 = MSVR1_DTR;
3482         if (rts > 0)
3483                 msvr2 = MSVR2_RTS;
3484
3485         save_flags(flags);
3486         cli();
3487         BRDENABLE(portp->brdnr, portp->pagenr);
3488         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3489         if (rts >= 0)
3490                 stl_cd1400setreg(portp, MSVR2, msvr2);
3491         if (dtr >= 0)
3492                 stl_cd1400setreg(portp, MSVR1, msvr1);
3493         BRDDISABLE(portp->brdnr);
3494         restore_flags(flags);
3495 }
3496
3497 /*****************************************************************************/
3498
3499 /*
3500  *      Return the state of the signals.
3501  */
3502
3503 static int stl_cd1400getsignals(stlport_t *portp)
3504 {
3505         unsigned char   msvr1, msvr2;
3506         unsigned long   flags;
3507         int             sigs;
3508
3509 #ifdef DEBUG
3510         printk("stl_cd1400getsignals(portp=%x)\n", (int) portp);
3511 #endif
3512
3513         save_flags(flags);
3514         cli();
3515         BRDENABLE(portp->brdnr, portp->pagenr);
3516         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3517         msvr1 = stl_cd1400getreg(portp, MSVR1);
3518         msvr2 = stl_cd1400getreg(portp, MSVR2);
3519         BRDDISABLE(portp->brdnr);
3520         restore_flags(flags);
3521
3522         sigs = 0;
3523         sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3524         sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3525         sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3526         sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3527 #if 0
3528         sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3529         sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3530 #else
3531         sigs |= TIOCM_DSR;
3532 #endif
3533         return sigs;
3534 }
3535
3536 /*****************************************************************************/
3537
3538 /*
3539  *      Enable/Disable the Transmitter and/or Receiver.
3540  */
3541
3542 static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
3543 {
3544         unsigned char   ccr;
3545         unsigned long   flags;
3546
3547 #ifdef DEBUG
3548         printk("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3549                 (int) portp, rx, tx);
3550 #endif
3551         ccr = 0;
3552
3553         if (tx == 0)
3554                 ccr |= CCR_TXDISABLE;
3555         else if (tx > 0)
3556                 ccr |= CCR_TXENABLE;
3557         if (rx == 0)
3558                 ccr |= CCR_RXDISABLE;
3559         else if (rx > 0)
3560                 ccr |= CCR_RXENABLE;
3561
3562         save_flags(flags);
3563         cli();
3564         BRDENABLE(portp->brdnr, portp->pagenr);
3565         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3566         stl_cd1400ccrwait(portp);
3567         stl_cd1400setreg(portp, CCR, ccr);
3568         stl_cd1400ccrwait(portp);
3569         BRDDISABLE(portp->brdnr);
3570         restore_flags(flags);
3571 }
3572
3573 /*****************************************************************************/
3574
3575 /*
3576  *      Start/stop the Transmitter and/or Receiver.
3577  */
3578
3579 static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
3580 {
3581         unsigned char   sreron, sreroff;
3582         unsigned long   flags;
3583
3584 #ifdef DEBUG
3585         printk("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
3586                 (int) portp, rx, tx);
3587 #endif
3588
3589         sreron = 0;
3590         sreroff = 0;
3591         if (tx == 0)
3592                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3593         else if (tx == 1)
3594                 sreron |= SRER_TXDATA;
3595         else if (tx >= 2)
3596                 sreron |= SRER_TXEMPTY;
3597         if (rx == 0)
3598                 sreroff |= SRER_RXDATA;
3599         else if (rx > 0)
3600                 sreron |= SRER_RXDATA;
3601
3602         save_flags(flags);
3603         cli();
3604         BRDENABLE(portp->brdnr, portp->pagenr);
3605         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3606         stl_cd1400setreg(portp, SRER,
3607                 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3608         BRDDISABLE(portp->brdnr);
3609         if (tx > 0)
3610                 set_bit(ASYI_TXBUSY, &portp->istate);
3611         restore_flags(flags);
3612 }
3613
3614 /*****************************************************************************/
3615
3616 /*
3617  *      Disable all interrupts from this port.
3618  */
3619
3620 static void stl_cd1400disableintrs(stlport_t *portp)
3621 {
3622         unsigned long   flags;
3623
3624 #ifdef DEBUG
3625         printk("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
3626 #endif
3627         save_flags(flags);
3628         cli();
3629         BRDENABLE(portp->brdnr, portp->pagenr);
3630         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3631         stl_cd1400setreg(portp, SRER, 0);
3632         BRDDISABLE(portp->brdnr);
3633         restore_flags(flags);
3634 }
3635
3636 /*****************************************************************************/
3637
3638 static void stl_cd1400sendbreak(stlport_t *portp, int len)
3639 {
3640         unsigned long   flags;
3641
3642 #ifdef DEBUG
3643         printk("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp, len);
3644 #endif
3645
3646         save_flags(flags);
3647         cli();
3648         BRDENABLE(portp->brdnr, portp->pagenr);
3649         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3650         stl_cd1400setreg(portp, SRER,
3651                 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3652                 SRER_TXEMPTY));
3653         BRDDISABLE(portp->brdnr);
3654         portp->brklen = len;
3655         if (len == 1)
3656                 portp->stats.txbreaks++;
3657         restore_flags(flags);
3658 }
3659
3660 /*****************************************************************************/
3661
3662 /*
3663  *      Take flow control actions...
3664  */
3665
3666 static void stl_cd1400flowctrl(stlport_t *portp, int state)
3667 {
3668         struct tty_struct       *tty;
3669         unsigned long           flags;
3670
3671 #ifdef DEBUG
3672         printk("stl_cd1400flowctrl(portp=%x,state=%x)\n", (int) portp, state);
3673 #endif
3674
3675         if (portp == (stlport_t *) NULL)
3676                 return;
3677         tty = portp->tty;
3678         if (tty == (struct tty_struct *) NULL)
3679                 return;
3680
3681         save_flags(flags);
3682         cli();
3683         BRDENABLE(portp->brdnr, portp->pagenr);
3684         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3685
3686         if (state) {
3687                 if (tty->termios->c_iflag & IXOFF) {
3688                         stl_cd1400ccrwait(portp);
3689                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3690                         portp->stats.rxxon++;
3691                         stl_cd1400ccrwait(portp);
3692                 }
3693 /*
3694  *              Question: should we return RTS to what it was before? It may
3695  *              have been set by an ioctl... Suppose not, since if you have
3696  *              hardware flow control set then it is pretty silly to go and
3697  *              set the RTS line by hand.
3698  */
3699                 if (tty->termios->c_cflag & CRTSCTS) {
3700                         stl_cd1400setreg(portp, MCOR1,
3701                                 (stl_cd1400getreg(portp, MCOR1) |
3702                                 FIFO_RTSTHRESHOLD));
3703                         stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3704                         portp->stats.rxrtson++;
3705                 }
3706         } else {
3707                 if (tty->termios->c_iflag & IXOFF) {
3708                         stl_cd1400ccrwait(portp);
3709                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3710                         portp->stats.rxxoff++;
3711                         stl_cd1400ccrwait(portp);
3712                 }
3713                 if (tty->termios->c_cflag & CRTSCTS) {
3714                         stl_cd1400setreg(portp, MCOR1,
3715                                 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3716                         stl_cd1400setreg(portp, MSVR2, 0);
3717                         portp->stats.rxrtsoff++;
3718                 }
3719         }
3720
3721         BRDDISABLE(portp->brdnr);
3722         restore_flags(flags);
3723 }
3724
3725 /*****************************************************************************/
3726
3727 /*
3728  *      Send a flow control character...
3729  */
3730
3731 static void stl_cd1400sendflow(stlport_t *portp, int state)
3732 {
3733         struct tty_struct       *tty;
3734         unsigned long           flags;
3735
3736 #ifdef DEBUG
3737         printk("stl_cd1400sendflow(portp=%x,state=%x)\n", (int) portp, state);
3738 #endif
3739
3740         if (portp == (stlport_t *) NULL)
3741                 return;
3742         tty = portp->tty;
3743         if (tty == (struct tty_struct *) NULL)
3744                 return;
3745
3746         save_flags(flags);
3747         cli();
3748         BRDENABLE(portp->brdnr, portp->pagenr);
3749         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3750         if (state) {
3751                 stl_cd1400ccrwait(portp);
3752                 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3753                 portp->stats.rxxon++;
3754                 stl_cd1400ccrwait(portp);
3755         } else {
3756                 stl_cd1400ccrwait(portp);
3757                 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3758                 portp->stats.rxxoff++;
3759                 stl_cd1400ccrwait(portp);
3760         }
3761         BRDDISABLE(portp->brdnr);
3762         restore_flags(flags);
3763 }
3764
3765 /*****************************************************************************/
3766
3767 static void stl_cd1400flush(stlport_t *portp)
3768 {
3769         unsigned long   flags;
3770
3771 #ifdef DEBUG
3772         printk("stl_cd1400flush(portp=%x)\n", (int) portp);
3773 #endif
3774
3775         if (portp == (stlport_t *) NULL)
3776                 return;
3777
3778         save_flags(flags);
3779         cli();
3780         BRDENABLE(portp->brdnr, portp->pagenr);
3781         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3782         stl_cd1400ccrwait(portp);
3783         stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
3784         stl_cd1400ccrwait(portp);
3785         portp->tx.tail = portp->tx.head;
3786         BRDDISABLE(portp->brdnr);
3787         restore_flags(flags);
3788 }
3789
3790 /*****************************************************************************/
3791
3792 /*
3793  *      Return the current state of data flow on this port. This is only
3794  *      really interresting when determining if data has fully completed
3795  *      transmission or not... This is easy for the cd1400, it accurately
3796  *      maintains the busy port flag.
3797  */
3798
3799 static int stl_cd1400datastate(stlport_t *portp)
3800 {
3801 #ifdef DEBUG
3802         printk("stl_cd1400datastate(portp=%x)\n", (int) portp);
3803 #endif
3804
3805         if (portp == (stlport_t *) NULL)
3806                 return 0;
3807
3808         return test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0;
3809 }
3810
3811 /*****************************************************************************/
3812
3813 /*
3814  *      Interrupt service routine for cd1400 EasyIO boards.
3815  */
3816
3817 static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
3818 {
3819         unsigned char   svrtype;
3820
3821 #ifdef DEBUG
3822         printk("stl_cd1400eiointr(panelp=%x,iobase=%x)\n",
3823                 (int) panelp, iobase);
3824 #endif
3825
3826         outb(SVRR, iobase);
3827         svrtype = inb(iobase + EREG_DATA);
3828         if (panelp->nrports > 4) {
3829                 outb((SVRR + 0x80), iobase);
3830                 svrtype |= inb(iobase + EREG_DATA);
3831         }
3832
3833         if (svrtype & SVRR_RX)
3834                 stl_cd1400rxisr(panelp, iobase);
3835         else if (svrtype & SVRR_TX)
3836                 stl_cd1400txisr(panelp, iobase);
3837         else if (svrtype & SVRR_MDM)
3838                 stl_cd1400mdmisr(panelp, iobase);
3839 }
3840
3841 /*****************************************************************************/
3842
3843 /*
3844  *      Interrupt service routine for cd1400 panels.
3845  */
3846
3847 static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
3848 {
3849         unsigned char   svrtype;
3850
3851 #ifdef DEBUG
3852         printk("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
3853                 iobase);
3854 #endif
3855
3856         outb(SVRR, iobase);
3857         svrtype = inb(iobase + EREG_DATA);
3858         outb((SVRR + 0x80), iobase);
3859         svrtype |= inb(iobase + EREG_DATA);
3860         if (svrtype & SVRR_RX)
3861                 stl_cd1400rxisr(panelp, iobase);
3862         else if (svrtype & SVRR_TX)
3863                 stl_cd1400txisr(panelp, iobase);
3864         else if (svrtype & SVRR_MDM)
3865                 stl_cd1400mdmisr(panelp, iobase);
3866 }
3867
3868
3869 /*****************************************************************************/
3870
3871 /*
3872  *      Unfortunately we need to handle breaks in the TX data stream, since
3873  *      this is the only way to generate them on the cd1400.
3874  */
3875
3876 static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr)
3877 {
3878         if (portp->brklen == 1) {
3879                 outb((COR2 + portp->uartaddr), ioaddr);
3880                 outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
3881                         (ioaddr + EREG_DATA));
3882                 outb((TDR + portp->uartaddr), ioaddr);
3883                 outb(ETC_CMD, (ioaddr + EREG_DATA));
3884                 outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
3885                 outb((SRER + portp->uartaddr), ioaddr);
3886                 outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
3887                         (ioaddr + EREG_DATA));
3888                 return 1;
3889         } else if (portp->brklen > 1) {
3890                 outb((TDR + portp->uartaddr), ioaddr);
3891                 outb(ETC_CMD, (ioaddr + EREG_DATA));
3892                 outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
3893                 portp->brklen = -1;
3894                 return 1;
3895         } else {
3896                 outb((COR2 + portp->uartaddr), ioaddr);
3897                 outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
3898                         (ioaddr + EREG_DATA));
3899                 portp->brklen = 0;
3900         }
3901         return 0;
3902 }
3903
3904 /*****************************************************************************/
3905
3906 /*
3907  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
3908  *      chars is pretty simple, stuff as many as possible from the TX buffer
3909  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
3910  *      are embedded as commands in the data stream. Oh no, had to use a goto!
3911  *      This could be optimized more, will do when I get time...
3912  *      In practice it is possible that interrupts are enabled but that the
3913  *      port has been hung up. Need to handle not having any TX buffer here,
3914  *      this is done by using the side effect that head and tail will also
3915  *      be NULL if the buffer has been freed.
3916  */
3917
3918 static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
3919 {
3920         stlport_t       *portp;
3921         int             len, stlen;
3922         char            *head, *tail;
3923         unsigned char   ioack, srer;
3924
3925 #ifdef DEBUG
3926         printk("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
3927 #endif
3928
3929         ioack = inb(ioaddr + EREG_TXACK);
3930         if (((ioack & panelp->ackmask) != 0) ||
3931             ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
3932                 printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
3933                 return;
3934         }
3935         portp = panelp->ports[(ioack >> 3)];
3936
3937 /*
3938  *      Unfortunately we need to handle breaks in the data stream, since
3939  *      this is the only way to generate them on the cd1400. Do it now if
3940  *      a break is to be sent.
3941  */
3942         if (portp->brklen != 0)
3943                 if (stl_cd1400breakisr(portp, ioaddr))
3944                         goto stl_txalldone;
3945
3946         head = portp->tx.head;
3947         tail = portp->tx.tail;
3948         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
3949         if ((len == 0) || ((len < STL_TXBUFLOW) &&
3950             (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
3951                 set_bit(ASYI_TXLOW, &portp->istate);
3952                 schedule_work(&portp->tqueue);
3953         }
3954
3955         if (len == 0) {
3956                 outb((SRER + portp->uartaddr), ioaddr);
3957                 srer = inb(ioaddr + EREG_DATA);
3958                 if (srer & SRER_TXDATA) {
3959                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
3960                 } else {
3961                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
3962                         clear_bit(ASYI_TXBUSY, &portp->istate);
3963                 }
3964                 outb(srer, (ioaddr + EREG_DATA));
3965         } else {
3966                 len = MIN(len, CD1400_TXFIFOSIZE);
3967                 portp->stats.txtotal += len;
3968                 stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
3969                 outb((TDR + portp->uartaddr), ioaddr);
3970                 outsb((ioaddr + EREG_DATA), tail, stlen);
3971                 len -= stlen;
3972                 tail += stlen;
3973                 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
3974                         tail = portp->tx.buf;
3975                 if (len > 0) {
3976                         outsb((ioaddr + EREG_DATA), tail, len);
3977                         tail += len;
3978                 }
3979                 portp->tx.tail = tail;
3980         }
3981
3982 stl_txalldone:
3983         outb((EOSRR + portp->uartaddr), ioaddr);
3984         outb(0, (ioaddr + EREG_DATA));
3985 }
3986
3987 /*****************************************************************************/
3988
3989 /*
3990  *      Receive character interrupt handler. Determine if we have good chars
3991  *      or bad chars and then process appropriately. Good chars are easy
3992  *      just shove the lot into the RX buffer and set all status byte to 0.
3993  *      If a bad RX char then process as required. This routine needs to be
3994  *      fast!  In practice it is possible that we get an interrupt on a port
3995  *      that is closed. This can happen on hangups - since they completely
3996  *      shutdown a port not in user context. Need to handle this case.
3997  */
3998
3999 static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
4000 {
4001         stlport_t               *portp;
4002         struct tty_struct       *tty;
4003         unsigned int            ioack, len, buflen;
4004         unsigned char           status;
4005         char                    ch;
4006
4007 #ifdef DEBUG
4008         printk("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
4009 #endif
4010
4011         ioack = inb(ioaddr + EREG_RXACK);
4012         if ((ioack & panelp->ackmask) != 0) {
4013                 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4014                 return;
4015         }
4016         portp = panelp->ports[(ioack >> 3)];
4017         tty = portp->tty;
4018
4019         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
4020                 outb((RDCR + portp->uartaddr), ioaddr);
4021                 len = inb(ioaddr + EREG_DATA);
4022                 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
4023                         len = MIN(len, sizeof(stl_unwanted));
4024                         outb((RDSR + portp->uartaddr), ioaddr);
4025                         insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
4026                         portp->stats.rxlost += len;
4027                         portp->stats.rxtotal += len;
4028                 } else {
4029                         len = MIN(len, buflen);
4030                         if (len > 0) {
4031                                 unsigned char *ptr;
4032                                 outb((RDSR + portp->uartaddr), ioaddr);
4033                                 tty_prepare_flip_string(tty, &ptr, len);
4034                                 insb((ioaddr + EREG_DATA), ptr, len);
4035                                 tty_schedule_flip(tty);
4036                                 portp->stats.rxtotal += len;
4037                         }
4038                 }
4039         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
4040                 outb((RDSR + portp->uartaddr), ioaddr);
4041                 status = inb(ioaddr + EREG_DATA);
4042                 ch = inb(ioaddr + EREG_DATA);
4043                 if (status & ST_PARITY)
4044                         portp->stats.rxparity++;
4045                 if (status & ST_FRAMING)
4046                         portp->stats.rxframing++;
4047                 if (status & ST_OVERRUN)
4048                         portp->stats.rxoverrun++;
4049                 if (status & ST_BREAK)
4050                         portp->stats.rxbreaks++;
4051                 if (status & ST_SCHARMASK) {
4052                         if ((status & ST_SCHARMASK) == ST_SCHAR1)
4053                                 portp->stats.txxon++;
4054                         if ((status & ST_SCHARMASK) == ST_SCHAR2)
4055                                 portp->stats.txxoff++;
4056                         goto stl_rxalldone;
4057                 }
4058                 if (tty != NULL && (portp->rxignoremsk & status) == 0) {
4059                         if (portp->rxmarkmsk & status) {
4060                                 if (status & ST_BREAK) {
4061                                         status = TTY_BREAK;
4062                                         if (portp->flags & ASYNC_SAK) {
4063                                                 do_SAK(tty);
4064                                                 BRDENABLE(portp->brdnr, portp->pagenr);
4065                                         }
4066                                 } else if (status & ST_PARITY) {
4067                                         status = TTY_PARITY;
4068                                 } else if (status & ST_FRAMING) {
4069                                         status = TTY_FRAME;
4070                                 } else if(status & ST_OVERRUN) {
4071                                         status = TTY_OVERRUN;
4072                                 } else {
4073                                         status = 0;
4074                                 }
4075                         } else {
4076                                 status = 0;
4077                         }
4078                         tty_insert_flip_char(tty, ch, status);
4079                         tty_schedule_flip(tty);
4080                 }
4081         } else {
4082                 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4083                 return;
4084         }
4085
4086 stl_rxalldone:
4087         outb((EOSRR + portp->uartaddr), ioaddr);
4088         outb(0, (ioaddr + EREG_DATA));
4089 }
4090
4091 /*****************************************************************************/
4092
4093 /*
4094  *      Modem interrupt handler. The is called when the modem signal line
4095  *      (DCD) has changed state. Leave most of the work to the off-level
4096  *      processing routine.
4097  */
4098
4099 static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
4100 {
4101         stlport_t       *portp;
4102         unsigned int    ioack;
4103         unsigned char   misr;
4104
4105 #ifdef DEBUG
4106         printk("stl_cd1400mdmisr(panelp=%x)\n", (int) panelp);
4107 #endif
4108
4109         ioack = inb(ioaddr + EREG_MDACK);
4110         if (((ioack & panelp->ackmask) != 0) ||
4111             ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
4112                 printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
4113                 return;
4114         }
4115         portp = panelp->ports[(ioack >> 3)];
4116
4117         outb((MISR + portp->uartaddr), ioaddr);
4118         misr = inb(ioaddr + EREG_DATA);
4119         if (misr & MISR_DCD) {
4120                 set_bit(ASYI_DCDCHANGE, &portp->istate);
4121                 schedule_work(&portp->tqueue);
4122                 portp->stats.modem++;
4123         }
4124
4125         outb((EOSRR + portp->uartaddr), ioaddr);
4126         outb(0, (ioaddr + EREG_DATA));
4127 }
4128
4129 /*****************************************************************************/
4130 /*                      SC26198 HARDWARE FUNCTIONS                           */
4131 /*****************************************************************************/
4132
4133 /*
4134  *      These functions get/set/update the registers of the sc26198 UARTs.
4135  *      Access to the sc26198 registers is via an address/data io port pair.
4136  *      (Maybe should make this inline...)
4137  */
4138
4139 static int stl_sc26198getreg(stlport_t *portp, int regnr)
4140 {
4141         outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4142         return inb(portp->ioaddr + XP_DATA);
4143 }
4144
4145 static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
4146 {
4147         outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4148         outb(value, (portp->ioaddr + XP_DATA));
4149 }
4150
4151 static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
4152 {
4153         outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4154         if (inb(portp->ioaddr + XP_DATA) != value) {
4155                 outb(value, (portp->ioaddr + XP_DATA));
4156                 return 1;
4157         }
4158         return 0;
4159 }
4160
4161 /*****************************************************************************/
4162
4163 /*
4164  *      Functions to get and set the sc26198 global registers.
4165  */
4166
4167 static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
4168 {
4169         outb(regnr, (portp->ioaddr + XP_ADDR));
4170         return inb(portp->ioaddr + XP_DATA);
4171 }
4172
4173 #if 0
4174 static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
4175 {
4176         outb(regnr, (portp->ioaddr + XP_ADDR));
4177         outb(value, (portp->ioaddr + XP_DATA));
4178 }
4179 #endif
4180
4181 /*****************************************************************************/
4182
4183 /*
4184  *      Inbitialize the UARTs in a panel. We don't care what sort of board
4185  *      these ports are on - since the port io registers are almost
4186  *      identical when dealing with ports.
4187  */
4188
4189 static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
4190 {
4191         int     chipmask, i;
4192         int     nrchips, ioaddr;
4193
4194 #ifdef DEBUG
4195         printk("stl_sc26198panelinit(brdp=%x,panelp=%x)\n",
4196                 (int) brdp, (int) panelp);
4197 #endif
4198
4199         BRDENABLE(panelp->brdnr, panelp->pagenr);
4200
4201 /*
4202  *      Check that each chip is present and started up OK.
4203  */
4204         chipmask = 0;
4205         nrchips = (panelp->nrports + 4) / SC26198_PORTS;
4206         if (brdp->brdtype == BRD_ECHPCI)
4207                 outb(panelp->pagenr, brdp->ioctrl);
4208
4209         for (i = 0; (i < nrchips); i++) {
4210                 ioaddr = panelp->iobase + (i * 4); 
4211                 outb(SCCR, (ioaddr + XP_ADDR));
4212                 outb(CR_RESETALL, (ioaddr + XP_DATA));
4213                 outb(TSTR, (ioaddr + XP_ADDR));
4214                 if (inb(ioaddr + XP_DATA) != 0) {
4215                         printk("STALLION: sc26198 not responding, "
4216                                 "brd=%d panel=%d chip=%d\n",
4217                                 panelp->brdnr, panelp->panelnr, i);
4218                         continue;
4219                 }
4220                 chipmask |= (0x1 << i);
4221                 outb(GCCR, (ioaddr + XP_ADDR));
4222                 outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
4223                 outb(WDTRCR, (ioaddr + XP_ADDR));
4224                 outb(0xff, (ioaddr + XP_DATA));
4225         }
4226
4227         BRDDISABLE(panelp->brdnr);
4228         return chipmask;
4229 }
4230
4231 /*****************************************************************************/
4232
4233 /*
4234  *      Initialize hardware specific port registers.
4235  */
4236
4237 static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
4238 {
4239 #ifdef DEBUG
4240         printk("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
4241                 (int) brdp, (int) panelp, (int) portp);
4242 #endif
4243
4244         if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
4245             (portp == (stlport_t *) NULL))
4246                 return;
4247
4248         portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
4249         portp->uartaddr = (portp->portnr & 0x07) << 4;
4250         portp->pagenr = panelp->pagenr;
4251         portp->hwid = 0x1;
4252
4253         BRDENABLE(portp->brdnr, portp->pagenr);
4254         stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
4255         BRDDISABLE(portp->brdnr);
4256 }
4257
4258 /*****************************************************************************/
4259
4260 /*
4261  *      Set up the sc26198 registers for a port based on the termios port
4262  *      settings.
4263  */
4264
4265 static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
4266 {
4267         stlbrd_t        *brdp;
4268         unsigned long   flags;
4269         unsigned int    baudrate;
4270         unsigned char   mr0, mr1, mr2, clk;
4271         unsigned char   imron, imroff, iopr, ipr;
4272
4273         mr0 = 0;
4274         mr1 = 0;
4275         mr2 = 0;
4276         clk = 0;
4277         iopr = 0;
4278         imron = 0;
4279         imroff = 0;
4280
4281         brdp = stl_brds[portp->brdnr];
4282         if (brdp == (stlbrd_t *) NULL)
4283                 return;
4284
4285 /*
4286  *      Set up the RX char ignore mask with those RX error types we
4287  *      can ignore.
4288  */
4289         portp->rxignoremsk = 0;
4290         if (tiosp->c_iflag & IGNPAR)
4291                 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
4292                         SR_RXOVERRUN);
4293         if (tiosp->c_iflag & IGNBRK)
4294                 portp->rxignoremsk |= SR_RXBREAK;
4295
4296         portp->rxmarkmsk = SR_RXOVERRUN;
4297         if (tiosp->c_iflag & (INPCK | PARMRK))
4298                 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
4299         if (tiosp->c_iflag & BRKINT)
4300                 portp->rxmarkmsk |= SR_RXBREAK;
4301
4302 /*
4303  *      Go through the char size, parity and stop bits and set all the
4304  *      option register appropriately.
4305  */
4306         switch (tiosp->c_cflag & CSIZE) {
4307         case CS5:
4308                 mr1 |= MR1_CS5;
4309                 break;
4310         case CS6:
4311                 mr1 |= MR1_CS6;
4312                 break;
4313         case CS7:
4314                 mr1 |= MR1_CS7;
4315                 break;
4316         default:
4317                 mr1 |= MR1_CS8;
4318                 break;
4319         }
4320
4321         if (tiosp->c_cflag & CSTOPB)
4322                 mr2 |= MR2_STOP2;
4323         else
4324                 mr2 |= MR2_STOP1;
4325
4326         if (tiosp->c_cflag & PARENB) {
4327                 if (tiosp->c_cflag & PARODD)
4328                         mr1 |= (MR1_PARENB | MR1_PARODD);
4329                 else
4330                         mr1 |= (MR1_PARENB | MR1_PAREVEN);
4331         } else {
4332                 mr1 |= MR1_PARNONE;
4333         }
4334
4335         mr1 |= MR1_ERRBLOCK;
4336
4337 /*
4338  *      Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
4339  *      space for hardware flow control and the like. This should be set to
4340  *      VMIN.
4341  */
4342         mr2 |= MR2_RXFIFOHALF;
4343
4344 /*
4345  *      Calculate the baud rate timers. For now we will just assume that
4346  *      the input and output baud are the same. The sc26198 has a fixed
4347  *      baud rate table, so only discrete baud rates possible.
4348  */
4349         baudrate = tiosp->c_cflag & CBAUD;
4350         if (baudrate & CBAUDEX) {
4351                 baudrate &= ~CBAUDEX;
4352                 if ((baudrate < 1) || (baudrate > 4))
4353                         tiosp->c_cflag &= ~CBAUDEX;
4354                 else
4355                         baudrate += 15;
4356         }
4357         baudrate = stl_baudrates[baudrate];
4358         if ((tiosp->c_cflag & CBAUD) == B38400) {
4359                 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
4360                         baudrate = 57600;
4361                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
4362                         baudrate = 115200;
4363                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
4364                         baudrate = 230400;
4365                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
4366                         baudrate = 460800;
4367                 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
4368                         baudrate = (portp->baud_base / portp->custom_divisor);
4369         }
4370         if (baudrate > STL_SC26198MAXBAUD)
4371                 baudrate = STL_SC26198MAXBAUD;
4372
4373         if (baudrate > 0) {
4374                 for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
4375                         if (baudrate <= sc26198_baudtable[clk])
4376                                 break;
4377                 }
4378         }
4379
4380 /*
4381  *      Check what form of modem signaling is required and set it up.
4382  */
4383         if (tiosp->c_cflag & CLOCAL) {
4384                 portp->flags &= ~ASYNC_CHECK_CD;
4385         } else {
4386                 iopr |= IOPR_DCDCOS;
4387                 imron |= IR_IOPORT;
4388                 portp->flags |= ASYNC_CHECK_CD;
4389         }
4390
4391 /*
4392  *      Setup sc26198 enhanced modes if we can. In particular we want to
4393  *      handle as much of the flow control as possible automatically. As
4394  *      well as saving a few CPU cycles it will also greatly improve flow
4395  *      control reliability.
4396  */
4397         if (tiosp->c_iflag & IXON) {
4398                 mr0 |= MR0_SWFTX | MR0_SWFT;
4399                 imron |= IR_XONXOFF;
4400         } else {
4401                 imroff |= IR_XONXOFF;
4402         }
4403         if (tiosp->c_iflag & IXOFF)
4404                 mr0 |= MR0_SWFRX;
4405
4406         if (tiosp->c_cflag & CRTSCTS) {
4407                 mr2 |= MR2_AUTOCTS;
4408                 mr1 |= MR1_AUTORTS;
4409         }
4410
4411 /*
4412  *      All sc26198 register values calculated so go through and set
4413  *      them all up.
4414  */
4415
4416 #ifdef DEBUG
4417         printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
4418                 portp->portnr, portp->panelnr, portp->brdnr);
4419         printk("    mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
4420         printk("    iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
4421         printk("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
4422                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
4423                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
4424 #endif
4425
4426         save_flags(flags);
4427         cli();
4428         BRDENABLE(portp->brdnr, portp->pagenr);
4429         stl_sc26198setreg(portp, IMR, 0);
4430         stl_sc26198updatereg(portp, MR0, mr0);
4431         stl_sc26198updatereg(portp, MR1, mr1);
4432         stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
4433         stl_sc26198updatereg(portp, MR2, mr2);
4434         stl_sc26198updatereg(portp, IOPIOR,
4435                 ((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
4436
4437         if (baudrate > 0) {
4438                 stl_sc26198setreg(portp, TXCSR, clk);
4439                 stl_sc26198setreg(portp, RXCSR, clk);
4440         }
4441
4442         stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
4443         stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
4444
4445         ipr = stl_sc26198getreg(portp, IPR);
4446         if (ipr & IPR_DCD)
4447                 portp->sigs &= ~TIOCM_CD;
4448         else
4449                 portp->sigs |= TIOCM_CD;
4450
4451         portp->imr = (portp->imr & ~imroff) | imron;
4452         stl_sc26198setreg(portp, IMR, portp->imr);
4453         BRDDISABLE(portp->brdnr);
4454         restore_flags(flags);
4455 }
4456
4457 /*****************************************************************************/
4458
4459 /*
4460  *      Set the state of the DTR and RTS signals.
4461  */
4462
4463 static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
4464 {
4465         unsigned char   iopioron, iopioroff;
4466         unsigned long   flags;
4467
4468 #ifdef DEBUG
4469         printk("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
4470                 (int) portp, dtr, rts);
4471 #endif
4472
4473         iopioron = 0;
4474         iopioroff = 0;
4475         if (dtr == 0)
4476                 iopioroff |= IPR_DTR;
4477         else if (dtr > 0)
4478                 iopioron |= IPR_DTR;
4479         if (rts == 0)
4480                 iopioroff |= IPR_RTS;
4481         else if (rts > 0)
4482                 iopioron |= IPR_RTS;
4483
4484         save_flags(flags);
4485         cli();
4486         BRDENABLE(portp->brdnr, portp->pagenr);
4487         stl_sc26198setreg(portp, IOPIOR,
4488                 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
4489         BRDDISABLE(portp->brdnr);
4490         restore_flags(flags);
4491 }
4492
4493 /*****************************************************************************/
4494
4495 /*
4496  *      Return the state of the signals.
4497  */
4498
4499 static int stl_sc26198getsignals(stlport_t *portp)
4500 {
4501         unsigned char   ipr;
4502         unsigned long   flags;
4503         int             sigs;
4504
4505 #ifdef DEBUG
4506         printk("stl_sc26198getsignals(portp=%x)\n", (int) portp);
4507 #endif
4508
4509         save_flags(flags);
4510         cli();
4511         BRDENABLE(portp->brdnr, portp->pagenr);
4512         ipr = stl_sc26198getreg(portp, IPR);
4513         BRDDISABLE(portp->brdnr);
4514         restore_flags(flags);
4515
4516         sigs = 0;
4517         sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
4518         sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
4519         sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
4520         sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
4521         sigs |= TIOCM_DSR;
4522         return sigs;
4523 }
4524
4525 /*****************************************************************************/
4526
4527 /*
4528  *      Enable/Disable the Transmitter and/or Receiver.
4529  */
4530
4531 static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
4532 {
4533         unsigned char   ccr;
4534         unsigned long   flags;
4535
4536 #ifdef DEBUG
4537         printk("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
4538                 (int) portp, rx, tx);
4539 #endif
4540
4541         ccr = portp->crenable;
4542         if (tx == 0)
4543                 ccr &= ~CR_TXENABLE;
4544         else if (tx > 0)
4545                 ccr |= CR_TXENABLE;
4546         if (rx == 0)
4547                 ccr &= ~CR_RXENABLE;
4548         else if (rx > 0)
4549                 ccr |= CR_RXENABLE;
4550
4551         save_flags(flags);
4552         cli();
4553         BRDENABLE(portp->brdnr, portp->pagenr);
4554         stl_sc26198setreg(portp, SCCR, ccr);
4555         BRDDISABLE(portp->brdnr);
4556         portp->crenable = ccr;
4557         restore_flags(flags);
4558 }
4559
4560 /*****************************************************************************/
4561
4562 /*
4563  *      Start/stop the Transmitter and/or Receiver.
4564  */
4565
4566 static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
4567 {
4568         unsigned char   imr;
4569         unsigned long   flags;
4570
4571 #ifdef DEBUG
4572         printk("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
4573                 (int) portp, rx, tx);
4574 #endif
4575
4576         imr = portp->imr;
4577         if (tx == 0)
4578                 imr &= ~IR_TXRDY;
4579         else if (tx == 1)
4580                 imr |= IR_TXRDY;
4581         if (rx == 0)
4582                 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
4583         else if (rx > 0)
4584                 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
4585
4586         save_flags(flags);
4587         cli();
4588         BRDENABLE(portp->brdnr, portp->pagenr);
4589         stl_sc26198setreg(portp, IMR, imr);
4590         BRDDISABLE(portp->brdnr);
4591         portp->imr = imr;
4592         if (tx > 0)
4593                 set_bit(ASYI_TXBUSY, &portp->istate);
4594         restore_flags(flags);
4595 }
4596
4597 /*****************************************************************************/
4598
4599 /*
4600  *      Disable all interrupts from this port.
4601  */
4602
4603 static void stl_sc26198disableintrs(stlport_t *portp)
4604 {
4605         unsigned long   flags;
4606
4607 #ifdef DEBUG
4608         printk("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
4609 #endif
4610
4611         save_flags(flags);
4612         cli();
4613         BRDENABLE(portp->brdnr, portp->pagenr);
4614         portp->imr = 0;
4615         stl_sc26198setreg(portp, IMR, 0);
4616         BRDDISABLE(portp->brdnr);
4617         restore_flags(flags);
4618 }
4619
4620 /*****************************************************************************/
4621
4622 static void stl_sc26198sendbreak(stlport_t *portp, int len)
4623 {
4624         unsigned long   flags;
4625
4626 #ifdef DEBUG
4627         printk("stl_sc26198sendbreak(portp=%x,len=%d)\n", (int) portp, len);
4628 #endif
4629
4630         save_flags(flags);
4631         cli();
4632         BRDENABLE(portp->brdnr, portp->pagenr);
4633         if (len == 1) {
4634                 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4635                 portp->stats.txbreaks++;
4636         } else {
4637                 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4638         }
4639         BRDDISABLE(portp->brdnr);
4640         restore_flags(flags);
4641 }
4642
4643 /*****************************************************************************/
4644
4645 /*
4646  *      Take flow control actions...
4647  */
4648
4649 static void stl_sc26198flowctrl(stlport_t *portp, int state)
4650 {
4651         struct tty_struct       *tty;
4652         unsigned long           flags;
4653         unsigned char           mr0;
4654
4655 #ifdef DEBUG
4656         printk("stl_sc26198flowctrl(portp=%x,state=%x)\n", (int) portp, state);
4657 #endif
4658
4659         if (portp == (stlport_t *) NULL)
4660                 return;
4661         tty = portp->tty;
4662         if (tty == (struct tty_struct *) NULL)
4663                 return;
4664
4665         save_flags(flags);
4666         cli();
4667         BRDENABLE(portp->brdnr, portp->pagenr);
4668
4669         if (state) {
4670                 if (tty->termios->c_iflag & IXOFF) {
4671                         mr0 = stl_sc26198getreg(portp, MR0);
4672                         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4673                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4674                         mr0 |= MR0_SWFRX;
4675                         portp->stats.rxxon++;
4676                         stl_sc26198wait(portp);
4677                         stl_sc26198setreg(portp, MR0, mr0);
4678                 }
4679 /*
4680  *              Question: should we return RTS to what it was before? It may
4681  *              have been set by an ioctl... Suppose not, since if you have
4682  *              hardware flow control set then it is pretty silly to go and
4683  *              set the RTS line by hand.
4684  */
4685                 if (tty->termios->c_cflag & CRTSCTS) {
4686                         stl_sc26198setreg(portp, MR1,
4687                                 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4688                         stl_sc26198setreg(portp, IOPIOR,
4689                                 (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4690                         portp->stats.rxrtson++;
4691                 }
4692         } else {
4693                 if (tty->termios->c_iflag & IXOFF) {
4694                         mr0 = stl_sc26198getreg(portp, MR0);
4695                         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4696                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4697                         mr0 &= ~MR0_SWFRX;
4698                         portp->stats.rxxoff++;
4699                         stl_sc26198wait(portp);
4700                         stl_sc26198setreg(portp, MR0, mr0);
4701                 }
4702                 if (tty->termios->c_cflag & CRTSCTS) {
4703                         stl_sc26198setreg(portp, MR1,
4704                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4705                         stl_sc26198setreg(portp, IOPIOR,
4706                                 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4707                         portp->stats.rxrtsoff++;
4708                 }
4709         }
4710
4711         BRDDISABLE(portp->brdnr);
4712         restore_flags(flags);
4713 }
4714
4715 /*****************************************************************************/
4716
4717 /*
4718  *      Send a flow control character.
4719  */
4720
4721 static void stl_sc26198sendflow(stlport_t *portp, int state)
4722 {
4723         struct tty_struct       *tty;
4724         unsigned long           flags;
4725         unsigned char           mr0;
4726
4727 #ifdef DEBUG
4728         printk("stl_sc26198sendflow(portp=%x,state=%x)\n", (int) portp, state);
4729 #endif
4730
4731         if (portp == (stlport_t *) NULL)
4732                 return;
4733         tty = portp->tty;
4734         if (tty == (struct tty_struct *) NULL)
4735                 return;
4736
4737         save_flags(flags);
4738         cli();
4739         BRDENABLE(portp->brdnr, portp->pagenr);
4740         if (state) {
4741                 mr0 = stl_sc26198getreg(portp, MR0);
4742                 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4743                 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4744                 mr0 |= MR0_SWFRX;
4745                 portp->stats.rxxon++;
4746                 stl_sc26198wait(portp);
4747                 stl_sc26198setreg(portp, MR0, mr0);
4748         } else {
4749                 mr0 = stl_sc26198getreg(portp, MR0);
4750                 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4751                 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4752                 mr0 &= ~MR0_SWFRX;
4753                 portp->stats.rxxoff++;
4754                 stl_sc26198wait(portp);
4755                 stl_sc26198setreg(portp, MR0, mr0);
4756         }
4757         BRDDISABLE(portp->brdnr);
4758         restore_flags(flags);
4759 }
4760
4761 /*****************************************************************************/
4762
4763 static void stl_sc26198flush(stlport_t *portp)
4764 {
4765         unsigned long   flags;
4766
4767 #ifdef DEBUG
4768         printk("stl_sc26198flush(portp=%x)\n", (int) portp);
4769 #endif
4770
4771         if (portp == (stlport_t *) NULL)
4772                 return;
4773
4774         save_flags(flags);
4775         cli();
4776         BRDENABLE(portp->brdnr, portp->pagenr);
4777         stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4778         stl_sc26198setreg(portp, SCCR, portp->crenable);
4779         BRDDISABLE(portp->brdnr);
4780         portp->tx.tail = portp->tx.head;
4781         restore_flags(flags);
4782 }
4783
4784 /*****************************************************************************/
4785
4786 /*
4787  *      Return the current state of data flow on this port. This is only
4788  *      really interresting when determining if data has fully completed
4789  *      transmission or not... The sc26198 interrupt scheme cannot
4790  *      determine when all data has actually drained, so we need to
4791  *      check the port statusy register to be sure.
4792  */
4793
4794 static int stl_sc26198datastate(stlport_t *portp)
4795 {
4796         unsigned long   flags;
4797         unsigned char   sr;
4798
4799 #ifdef DEBUG
4800         printk("stl_sc26198datastate(portp=%x)\n", (int) portp);
4801 #endif
4802
4803         if (portp == (stlport_t *) NULL)
4804                 return 0;
4805         if (test_bit(ASYI_TXBUSY, &portp->istate))
4806                 return 1;
4807
4808         save_flags(flags);
4809         cli();
4810         BRDENABLE(portp->brdnr, portp->pagenr);
4811         sr = stl_sc26198getreg(portp, SR);
4812         BRDDISABLE(portp->brdnr);
4813         restore_flags(flags);
4814
4815         return (sr & SR_TXEMPTY) ? 0 : 1;
4816 }
4817
4818 /*****************************************************************************/
4819
4820 /*
4821  *      Delay for a small amount of time, to give the sc26198 a chance
4822  *      to process a command...
4823  */
4824
4825 static void stl_sc26198wait(stlport_t *portp)
4826 {
4827         int     i;
4828
4829 #ifdef DEBUG
4830         printk("stl_sc26198wait(portp=%x)\n", (int) portp);
4831 #endif
4832
4833         if (portp == (stlport_t *) NULL)
4834                 return;
4835
4836         for (i = 0; (i < 20); i++)
4837                 stl_sc26198getglobreg(portp, TSTR);
4838 }
4839
4840 /*****************************************************************************/
4841
4842 /*
4843  *      If we are TX flow controlled and in IXANY mode then we may
4844  *      need to unflow control here. We gotta do this because of the
4845  *      automatic flow control modes of the sc26198.
4846  */
4847
4848 static inline void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty)
4849 {
4850         unsigned char   mr0;
4851
4852         mr0 = stl_sc26198getreg(portp, MR0);
4853         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4854         stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4855         stl_sc26198wait(portp);
4856         stl_sc26198setreg(portp, MR0, mr0);
4857         clear_bit(ASYI_TXFLOWED, &portp->istate);
4858 }
4859
4860 /*****************************************************************************/
4861
4862 /*
4863  *      Interrupt service routine for sc26198 panels.
4864  */
4865
4866 static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
4867 {
4868         stlport_t       *portp;
4869         unsigned int    iack;
4870
4871 /* 
4872  *      Work around bug in sc26198 chip... Cannot have A6 address
4873  *      line of UART high, else iack will be returned as 0.
4874  */
4875         outb(0, (iobase + 1));
4876
4877         iack = inb(iobase + XP_IACK);
4878         portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4879
4880         if (iack & IVR_RXDATA)
4881                 stl_sc26198rxisr(portp, iack);
4882         else if (iack & IVR_TXDATA)
4883                 stl_sc26198txisr(portp);
4884         else
4885                 stl_sc26198otherisr(portp, iack);
4886 }
4887
4888 /*****************************************************************************/
4889
4890 /*
4891  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
4892  *      chars is pretty simple, stuff as many as possible from the TX buffer
4893  *      into the sc26198 FIFO.
4894  *      In practice it is possible that interrupts are enabled but that the
4895  *      port has been hung up. Need to handle not having any TX buffer here,
4896  *      this is done by using the side effect that head and tail will also
4897  *      be NULL if the buffer has been freed.
4898  */
4899
4900 static void stl_sc26198txisr(stlport_t *portp)
4901 {
4902         unsigned int    ioaddr;
4903         unsigned char   mr0;
4904         int             len, stlen;
4905         char            *head, *tail;
4906
4907 #ifdef DEBUG
4908         printk("stl_sc26198txisr(portp=%x)\n", (int) portp);
4909 #endif
4910
4911         ioaddr = portp->ioaddr;
4912         head = portp->tx.head;
4913         tail = portp->tx.tail;
4914         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4915         if ((len == 0) || ((len < STL_TXBUFLOW) &&
4916             (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
4917                 set_bit(ASYI_TXLOW, &portp->istate);
4918                 schedule_work(&portp->tqueue); 
4919         }
4920
4921         if (len == 0) {
4922                 outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
4923                 mr0 = inb(ioaddr + XP_DATA);
4924                 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4925                         portp->imr &= ~IR_TXRDY;
4926                         outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
4927                         outb(portp->imr, (ioaddr + XP_DATA));
4928                         clear_bit(ASYI_TXBUSY, &portp->istate);
4929                 } else {
4930                         mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4931                         outb(mr0, (ioaddr + XP_DATA));
4932                 }
4933         } else {
4934                 len = MIN(len, SC26198_TXFIFOSIZE);
4935                 portp->stats.txtotal += len;
4936                 stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
4937                 outb(GTXFIFO, (ioaddr + XP_ADDR));
4938                 outsb((ioaddr + XP_DATA), tail, stlen);
4939                 len -= stlen;
4940                 tail += stlen;
4941                 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4942                         tail = portp->tx.buf;
4943                 if (len > 0) {
4944                         outsb((ioaddr + XP_DATA), tail, len);
4945                         tail += len;
4946                 }
4947                 portp->tx.tail = tail;
4948         }
4949 }
4950
4951 /*****************************************************************************/
4952
4953 /*
4954  *      Receive character interrupt handler. Determine if we have good chars
4955  *      or bad chars and then process appropriately. Good chars are easy
4956  *      just shove the lot into the RX buffer and set all status byte to 0.
4957  *      If a bad RX char then process as required. This routine needs to be
4958  *      fast!  In practice it is possible that we get an interrupt on a port
4959  *      that is closed. This can happen on hangups - since they completely
4960  *      shutdown a port not in user context. Need to handle this case.
4961  */
4962
4963 static void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
4964 {
4965         struct tty_struct       *tty;
4966         unsigned int            len, buflen, ioaddr;
4967
4968 #ifdef DEBUG
4969         printk("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
4970 #endif
4971
4972         tty = portp->tty;
4973         ioaddr = portp->ioaddr;
4974         outb(GIBCR, (ioaddr + XP_ADDR));
4975         len = inb(ioaddr + XP_DATA) + 1;
4976
4977         if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
4978                 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
4979                         len = MIN(len, sizeof(stl_unwanted));
4980                         outb(GRXFIFO, (ioaddr + XP_ADDR));
4981                         insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
4982                         portp->stats.rxlost += len;
4983                         portp->stats.rxtotal += len;
4984                 } else {
4985                         len = MIN(len, buflen);
4986                         if (len > 0) {
4987                                 unsigned char *ptr;
4988                                 outb(GRXFIFO, (ioaddr + XP_ADDR));
4989                                 tty_prepare_flip_string(tty, &ptr, len);
4990                                 insb((ioaddr + XP_DATA), ptr, len);
4991                                 tty_schedule_flip(tty);
4992                                 portp->stats.rxtotal += len;
4993                         }
4994                 }
4995         } else {
4996                 stl_sc26198rxbadchars(portp);
4997         }
4998
4999 /*
5000  *      If we are TX flow controlled and in IXANY mode then we may need
5001  *      to unflow control here. We gotta do this because of the automatic
5002  *      flow control modes of the sc26198.
5003  */
5004         if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
5005                 if ((tty != (struct tty_struct *) NULL) &&
5006                     (tty->termios != (struct termios *) NULL) &&
5007                     (tty->termios->c_iflag & IXANY)) {
5008                         stl_sc26198txunflow(portp, tty);
5009                 }
5010         }
5011 }
5012
5013 /*****************************************************************************/
5014
5015 /*
5016  *      Process an RX bad character.
5017  */
5018
5019 static inline void stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch)
5020 {
5021         struct tty_struct       *tty;
5022         unsigned int            ioaddr;
5023
5024         tty = portp->tty;
5025         ioaddr = portp->ioaddr;
5026
5027         if (status & SR_RXPARITY)
5028                 portp->stats.rxparity++;
5029         if (status & SR_RXFRAMING)
5030                 portp->stats.rxframing++;
5031         if (status & SR_RXOVERRUN)
5032                 portp->stats.rxoverrun++;
5033         if (status & SR_RXBREAK)
5034                 portp->stats.rxbreaks++;
5035
5036         if ((tty != (struct tty_struct *) NULL) &&
5037             ((portp->rxignoremsk & status) == 0)) {
5038                 if (portp->rxmarkmsk & status) {
5039                         if (status & SR_RXBREAK) {
5040                                 status = TTY_BREAK;
5041                                 if (portp->flags & ASYNC_SAK) {
5042                                         do_SAK(tty);
5043                                         BRDENABLE(portp->brdnr, portp->pagenr);
5044                                 }
5045                         } else if (status & SR_RXPARITY) {
5046                                 status = TTY_PARITY;
5047                         } else if (status & SR_RXFRAMING) {
5048                                 status = TTY_FRAME;
5049                         } else if(status & SR_RXOVERRUN) {
5050                                 status = TTY_OVERRUN;
5051                         } else {
5052                                 status = 0;
5053                         }
5054                 } else {
5055                         status = 0;
5056                 }
5057
5058                 tty_insert_flip_char(tty, ch, status);
5059                 tty_schedule_flip(tty);
5060
5061                 if (status == 0)
5062                         portp->stats.rxtotal++;
5063         }
5064 }
5065
5066 /*****************************************************************************/
5067
5068 /*
5069  *      Process all characters in the RX FIFO of the UART. Check all char
5070  *      status bytes as well, and process as required. We need to check
5071  *      all bytes in the FIFO, in case some more enter the FIFO while we
5072  *      are here. To get the exact character error type we need to switch
5073  *      into CHAR error mode (that is why we need to make sure we empty
5074  *      the FIFO).
5075  */
5076
5077 static void stl_sc26198rxbadchars(stlport_t *portp)
5078 {
5079         unsigned char   status, mr1;
5080         char            ch;
5081
5082 /*
5083  *      To get the precise error type for each character we must switch
5084  *      back into CHAR error mode.
5085  */
5086         mr1 = stl_sc26198getreg(portp, MR1);
5087         stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
5088
5089         while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
5090                 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
5091                 ch = stl_sc26198getreg(portp, RXFIFO);
5092                 stl_sc26198rxbadch(portp, status, ch);
5093         }
5094
5095 /*
5096  *      To get correct interrupt class we must switch back into BLOCK
5097  *      error mode.
5098  */
5099         stl_sc26198setreg(portp, MR1, mr1);
5100 }
5101
5102 /*****************************************************************************/
5103
5104 /*
5105  *      Other interrupt handler. This includes modem signals, flow
5106  *      control actions, etc. Most stuff is left to off-level interrupt
5107  *      processing time.
5108  */
5109
5110 static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
5111 {
5112         unsigned char   cir, ipr, xisr;
5113
5114 #ifdef DEBUG
5115         printk("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
5116 #endif
5117
5118         cir = stl_sc26198getglobreg(portp, CIR);
5119
5120         switch (cir & CIR_SUBTYPEMASK) {
5121         case CIR_SUBCOS:
5122                 ipr = stl_sc26198getreg(portp, IPR);
5123                 if (ipr & IPR_DCDCHANGE) {
5124                         set_bit(ASYI_DCDCHANGE, &portp->istate);
5125                         schedule_work(&portp->tqueue); 
5126                         portp->stats.modem++;
5127                 }
5128                 break;
5129         case CIR_SUBXONXOFF:
5130                 xisr = stl_sc26198getreg(portp, XISR);
5131                 if (xisr & XISR_RXXONGOT) {
5132                         set_bit(ASYI_TXFLOWED, &portp->istate);
5133                         portp->stats.txxoff++;
5134                 }
5135                 if (xisr & XISR_RXXOFFGOT) {
5136                         clear_bit(ASYI_TXFLOWED, &portp->istate);
5137                         portp->stats.txxon++;
5138                 }
5139                 break;
5140         case CIR_SUBBREAK:
5141                 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
5142                 stl_sc26198rxbadchars(portp);
5143                 break;
5144         default:
5145                 break;
5146         }
5147 }
5148
5149 /*****************************************************************************/