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