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