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