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