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