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