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