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