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