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