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