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