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