LINT build test. Aggregated source code adjustments to bring most of the
[dragonfly.git] / sys / dev / serial / stl / stallion.c
1 /*****************************************************************************/
2
3 /*
4  * stallion.c  -- stallion multiport serial driver.
5  *
6  * Copyright (c) 1995-1996 Greg Ungerer (gerg@stallion.oz.au).
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Greg Ungerer.
20  * 4. Neither the name of the author nor the names of any co-contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/sys/i386/isa/stallion.c,v 1.39.2.2 2001/08/30 12:29:57 murray Exp $
37  * $DragonFly: src/sys/dev/serial/stl/stallion.c,v 1.6 2003/07/21 07:57:45 dillon Exp $
38  */
39
40 /*****************************************************************************/
41
42 #define TTYDEFCHARS     1
43
44 #include "opt_compat.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/tty.h>
51 #include <sys/proc.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <i386/isa/isa_device.h>
55 #include <i386/isa/ic/scd1400.h>
56 #include <i386/isa/ic/sc26198.h>
57 #include <machine/comstats.h>
58
59 #include "pci.h"
60 #if NPCI > 0
61 #include <pci/pcivar.h>
62 #include <pci/pcireg.h>
63 #endif
64
65 #undef STLDEBUG
66
67 /*****************************************************************************/
68
69 /*
70  *      Define the version level of the kernel - so we can compile in the
71  *      appropriate bits of code. By default this will compile for a 2.1
72  *      level kernel.
73  */
74 #define VFREEBSD        220
75
76 #if VFREEBSD >= 220
77 #define STATIC          static
78 #else
79 #define STATIC
80 #endif
81
82 /*****************************************************************************/
83
84 /*
85  *      Define different board types. At the moment I have only declared
86  *      those boards that this driver supports. But I will use the standard
87  *      "assigned" board numbers. In the future this driver will support
88  *      some of the other Stallion boards. Currently supported boards are
89  *      abbreviated as EIO = EasyIO and ECH = EasyConnection 8/32.
90  */
91 #define BRD_EASYIO      20
92 #define BRD_ECH         21
93 #define BRD_ECHMC       22
94 #define BRD_ECHPCI      26
95 #define BRD_ECH64PCI    27
96 #define BRD_EASYIOPCI   28
97
98 /*
99  *      When using the BSD "config" stuff there is no easy way to specifiy
100  *      a secondary IO address region. So it is hard wired here. Also the
101  *      shared interrupt information is hard wired here...
102  */
103 static unsigned int     stl_ioshared = 0x280;
104 static unsigned int     stl_irqshared = 0;
105
106 /*****************************************************************************/
107
108 /*
109  *      Define important driver limitations.
110  */
111 #define STL_MAXBRDS             8
112 #define STL_MAXPANELS           4
113 #define STL_MAXBANKS            8
114 #define STL_PORTSPERPANEL       16
115 #define STL_PORTSPERBRD         64
116
117 /*
118  *      Define the important minor number break down bits. These have been
119  *      chosen to be "compatable" with the standard sio driver minor numbers.
120  *      Extra high bits are used to distinguish between boards.
121  */
122 #define STL_CALLOUTDEV          0x80
123 #define STL_CTRLLOCK            0x40
124 #define STL_CTRLINIT            0x20
125 #define STL_CTRLDEV             (STL_CTRLLOCK | STL_CTRLINIT)
126
127 #define STL_MEMDEV      0x07000000
128
129 #define STL_DEFSPEED    TTYDEF_SPEED
130 #define STL_DEFCFLAG    (CS8 | CREAD | HUPCL)
131
132 /*
133  *      I haven't really decided (or measured) what buffer sizes give
134  *      a good balance between performance and memory usage. These seem
135  *      to work pretty well...
136  */
137 #define STL_RXBUFSIZE           2048
138 #define STL_TXBUFSIZE           2048
139
140 #define STL_TXBUFLOW            (STL_TXBUFSIZE / 4)
141 #define STL_RXBUFHIGH           (3 * STL_RXBUFSIZE / 4)
142
143 /*****************************************************************************/
144
145 /*
146  *      Define our local driver identity first. Set up stuff to deal with
147  *      all the local structures required by a serial tty driver.
148  */
149 static const char       stl_drvname[] = "stl";
150 static const char       stl_longdrvname[] = "Stallion Multiport Serial Driver";
151 static const char       stl_drvversion[] = "2.0.0";
152 static int              stl_brdprobed[STL_MAXBRDS];
153
154 static int              stl_nrbrds = 0;
155 static int              stl_doingtimeout = 0;
156
157 static const char       __file__[] = /*__FILE__*/ "stallion.c";
158
159 /*
160  *      Define global stats structures. Not used often, and can be
161  *      re-used for each stats call.
162  */
163 static combrd_t         stl_brdstats;
164 static comstats_t       stl_comstats;
165
166 /*****************************************************************************/
167
168 /*
169  *      Define a set of structures to hold all the board/panel/port info
170  *      for our ports. These will be dynamically allocated as required.
171  */
172
173 /*
174  *      Define a ring queue structure for each port. This will hold the
175  *      TX data waiting to be output. Characters are fed into this buffer
176  *      from the line discipline (or even direct from user space!) and
177  *      then fed into the UARTs during interrupts. Will use a clasic ring
178  *      queue here for this. The good thing about this type of ring queue
179  *      is that the head and tail pointers can be updated without interrupt
180  *      protection - since "write" code only needs to change the head, and
181  *      interrupt code only needs to change the tail.
182  */
183 typedef struct {
184         char    *buf;
185         char    *endbuf;
186         char    *head;
187         char    *tail;
188 } stlrq_t;
189
190 /*
191  *      Port, panel and board structures to hold status info about each.
192  *      The board structure contains pointers to structures for each panel
193  *      connected to it, and in turn each panel structure contains pointers
194  *      for each port structure for each port on that panel. Note that
195  *      the port structure also contains the board and panel number that it
196  *      is associated with, this makes it (fairly) easy to get back to the
197  *      board/panel info for a port. Also note that the tty struct is at
198  *      the top of the structure, this is important, since the code uses
199  *      this fact to get the port struct pointer from the tty struct
200  *      pointer!
201  */
202 typedef struct stlport {
203         struct tty      tty;
204         int             portnr;
205         int             panelnr;
206         int             brdnr;
207         int             ioaddr;
208         int             uartaddr;
209         int             pagenr;
210         int             callout;
211         int             brklen;
212         int             dtrwait;
213         int             dotimestamp;
214         int             waitopens;
215         int             hotchar;
216         void            *uartp;
217         unsigned int    state;
218         unsigned int    hwid;
219         unsigned int    sigs;
220         unsigned int    rxignoremsk;
221         unsigned int    rxmarkmsk;
222         unsigned int    crenable;
223         unsigned int    imr;
224         unsigned long   clk;
225         struct termios  initintios;
226         struct termios  initouttios;
227         struct termios  lockintios;
228         struct termios  lockouttios;
229         struct timeval  timestamp;
230         comstats_t      stats;
231         stlrq_t         tx;
232         stlrq_t         rx;
233         stlrq_t         rxstatus;
234 } stlport_t;
235
236 typedef struct stlpanel {
237         int             panelnr;
238         int             brdnr;
239         int             pagenr;
240         int             nrports;
241         int             iobase;
242         unsigned int    hwid;
243         unsigned int    ackmask;
244         void            (*isr)(struct stlpanel *panelp, unsigned int iobase);
245         void            *uartp;
246         stlport_t       *ports[STL_PORTSPERPANEL];
247 } stlpanel_t;
248
249 typedef struct stlbrd {
250         int             brdnr;
251         int             brdtype;
252         int             unitid;
253         int             state;
254         int             nrpanels;
255         int             nrports;
256         int             nrbnks;
257         int             irq;
258         int             irqtype;
259         unsigned int    ioaddr1;
260         unsigned int    ioaddr2;
261         unsigned int    iostatus;
262         unsigned int    ioctrl;
263         unsigned int    ioctrlval;
264         unsigned int    hwid;
265         unsigned long   clk;
266         void            (*isr)(struct stlbrd *brdp);
267         unsigned int    bnkpageaddr[STL_MAXBANKS];
268         unsigned int    bnkstataddr[STL_MAXBANKS];
269         stlpanel_t      *bnk2panel[STL_MAXBANKS];
270         stlpanel_t      *panels[STL_MAXPANELS];
271         stlport_t       *ports[STL_PORTSPERBRD];
272 } stlbrd_t;
273
274 static stlbrd_t         *stl_brds[STL_MAXBRDS];
275
276 /*
277  *      Per board state flags. Used with the state field of the board struct.
278  *      Not really much here yet!
279  */
280 #define BRD_FOUND       0x1
281
282 /*
283  *      Define the port structure state flags. These set of flags are
284  *      modified at interrupt time - so setting and reseting them needs
285  *      to be atomic.
286  */
287 #define ASY_TXLOW       0x1
288 #define ASY_RXDATA      0x2
289 #define ASY_DCDCHANGE   0x4
290 #define ASY_DTRWAIT     0x8
291 #define ASY_RTSFLOW     0x10
292 #define ASY_RTSFLOWMODE 0x20
293 #define ASY_CTSFLOWMODE 0x40
294 #define ASY_TXFLOWED    0x80
295 #define ASY_TXBUSY      0x100
296 #define ASY_TXEMPTY     0x200
297
298 #define ASY_ACTIVE      (ASY_TXLOW | ASY_RXDATA | ASY_DCDCHANGE)
299
300 /*
301  *      Define an array of board names as printable strings. Handy for
302  *      referencing boards when printing trace and stuff.
303  */
304 static char     *stl_brdnames[] = {
305         (char *) NULL,
306         (char *) NULL,
307         (char *) NULL,
308         (char *) NULL,
309         (char *) NULL,
310         (char *) NULL,
311         (char *) NULL,
312         (char *) NULL,
313         (char *) NULL,
314         (char *) NULL,
315         (char *) NULL,
316         (char *) NULL,
317         (char *) NULL,
318         (char *) NULL,
319         (char *) NULL,
320         (char *) NULL,
321         (char *) NULL,
322         (char *) NULL,
323         (char *) NULL,
324         (char *) NULL,
325         "EasyIO",
326         "EC8/32-AT",
327         "EC8/32-MC",
328         (char *) NULL,
329         (char *) NULL,
330         (char *) NULL,
331         "EC8/32-PCI",
332         "EC8/64-PCI",
333         "EasyIO-PCI",
334 };
335
336 /*****************************************************************************/
337
338 /*
339  *      Hardware ID bits for the EasyIO and ECH boards. These defines apply
340  *      to the directly accessable io ports of these boards (not the cd1400
341  *      uarts - they are in scd1400.h).
342  */
343 #define EIO_8PORTRS     0x04
344 #define EIO_4PORTRS     0x05
345 #define EIO_8PORTDI     0x00
346 #define EIO_8PORTM      0x06
347 #define EIO_MK3         0x03
348 #define EIO_IDBITMASK   0x07
349
350 #define EIO_BRDMASK     0xf0
351 #define ID_BRD4         0x10
352 #define ID_BRD8         0x20
353 #define ID_BRD16        0x30
354
355 #define EIO_INTRPEND    0x08
356 #define EIO_INTEDGE     0x00
357 #define EIO_INTLEVEL    0x08
358
359 #define ECH_ID          0xa0
360 #define ECH_IDBITMASK   0xe0
361 #define ECH_BRDENABLE   0x08
362 #define ECH_BRDDISABLE  0x00
363 #define ECH_INTENABLE   0x01
364 #define ECH_INTDISABLE  0x00
365 #define ECH_INTLEVEL    0x02
366 #define ECH_INTEDGE     0x00
367 #define ECH_INTRPEND    0x01
368 #define ECH_BRDRESET    0x01
369
370 #define ECHMC_INTENABLE 0x01
371 #define ECHMC_BRDRESET  0x02
372
373 #define ECH_PNLSTATUS   2
374 #define ECH_PNL16PORT   0x20
375 #define ECH_PNLIDMASK   0x07
376 #define ECH_PNLXPID     0x40
377 #define ECH_PNLINTRPEND 0x80
378 #define ECH_ADDR2MASK   0x1e0
379
380 #define EIO_CLK         25000000
381 #define EIO_CLK8M       20000000
382 #define ECH_CLK         EIO_CLK
383
384 /*
385  *      Define the PCI vendor and device ID for Stallion PCI boards.
386  */
387 #define STL_PCINSVENDID 0x100b
388 #define STL_PCINSDEVID  0xd001
389
390 #define STL_PCIVENDID   0x124d
391 #define STL_PCI32DEVID  0x0000
392 #define STL_PCI64DEVID  0x0002
393 #define STL_PCIEIODEVID 0x0003
394
395 #define STL_PCIBADCLASS 0x0101
396
397 typedef struct stlpcibrd {
398         unsigned short          vendid;
399         unsigned short          devid;
400         int                     brdtype;
401 } stlpcibrd_t;
402
403 static  stlpcibrd_t     stl_pcibrds[] = {
404         { STL_PCIVENDID, STL_PCI64DEVID, BRD_ECH64PCI },
405         { STL_PCIVENDID, STL_PCIEIODEVID, BRD_EASYIOPCI },
406         { STL_PCIVENDID, STL_PCI32DEVID, BRD_ECHPCI },
407         { STL_PCINSVENDID, STL_PCINSDEVID, BRD_ECHPCI },
408 };
409
410 static int      stl_nrpcibrds = sizeof(stl_pcibrds) / sizeof(stlpcibrd_t);
411
412 /*****************************************************************************/
413
414 /*
415  *      Define the vector mapping bits for the programmable interrupt board
416  *      hardware. These bits encode the interrupt for the board to use - it
417  *      is software selectable (except the EIO-8M).
418  */
419 static unsigned char    stl_vecmap[] = {
420         0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
421         0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
422 };
423
424 /*
425  *      Set up enable and disable macros for the ECH boards. They require
426  *      the secondary io address space to be activated and deactivated.
427  *      This way all ECH boards can share their secondary io region.
428  *      If this is an ECH-PCI board then also need to set the page pointer
429  *      to point to the correct page.
430  */
431 #define BRDENABLE(brdnr,pagenr)                                         \
432         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
433                 outb(stl_brds[(brdnr)]->ioctrl,                         \
434                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE));\
435         else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)              \
436                 outb(stl_brds[(brdnr)]->ioctrl, (pagenr));
437
438 #define BRDDISABLE(brdnr)                                               \
439         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
440                 outb(stl_brds[(brdnr)]->ioctrl,                         \
441                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE));
442
443 /*
444  *      Define some spare buffer space for un-wanted received characters.
445  */
446 static char     stl_unwanted[SC26198_RXFIFOSIZE];
447
448 /*****************************************************************************/
449
450 /*
451  *      Define macros to extract a brd and port number from a minor number.
452  *      This uses the extended minor number range in the upper 2 bytes of
453  *      the device number. This gives us plenty of minor numbers to play
454  *      with...
455  */
456 #define MKDEV2BRD(m)    ((minor(m) & 0x00700000) >> 20)
457 #define MKDEV2PORT(m)   ((minor(m) & 0x1f) | ((minor(m) & 0x00010000) >> 11))
458
459 /*
460  *      Define some handy local macros...
461  */
462 #ifndef MIN
463 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
464 #endif
465
466 /*****************************************************************************/
467
468 /*
469  *      Declare all those functions in this driver!  First up is the set of
470  *      externally visible functions.
471  */
472
473 static int      stlprobe(struct isa_device *idp);
474 static int      stlattach(struct isa_device *idp);
475
476 STATIC  d_open_t        stlopen;
477 STATIC  d_close_t       stlclose;
478 STATIC  d_ioctl_t       stlioctl;
479
480 /*
481  *      Internal function prototypes.
482  */
483 static stlport_t *stl_dev2port(dev_t dev);
484 static int      stl_findfreeunit(void);
485 static int      stl_rawopen(stlport_t *portp);
486 static int      stl_rawclose(stlport_t *portp);
487 static void     stl_flush(stlport_t *portp, int flag);
488 static int      stl_param(struct tty *tp, struct termios *tiosp);
489 static void     stl_start(struct tty *tp);
490 static void     stl_stop(struct tty *tp, int);
491 static void     stl_ttyoptim(stlport_t *portp, struct termios *tiosp);
492 static void     stl_dotimeout(void);
493 static void     stl_poll(void *arg);
494 static void     stl_rxprocess(stlport_t *portp);
495 static void     stl_flowcontrol(stlport_t *portp, int hw, int sw);
496 static void     stl_dtrwakeup(void *arg);
497 static int      stl_brdinit(stlbrd_t *brdp);
498 static int      stl_initeio(stlbrd_t *brdp);
499 static int      stl_initech(stlbrd_t *brdp);
500 static int      stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
501 static void     stl_eiointr(stlbrd_t *brdp);
502 static void     stl_echatintr(stlbrd_t *brdp);
503 static void     stl_echmcaintr(stlbrd_t *brdp);
504 static void     stl_echpciintr(stlbrd_t *brdp);
505 static void     stl_echpci64intr(stlbrd_t *brdp);
506 static int      stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data,
507                         int flag, struct thread *td);
508 static int      stl_getbrdstats(caddr_t data);
509 static int      stl_getportstats(stlport_t *portp, caddr_t data);
510 static int      stl_clrportstats(stlport_t *portp, caddr_t data);
511 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
512 static ointhand2_t      stlintr;
513
514 #if NPCI > 0
515 static const char *stlpciprobe(pcici_t tag, pcidi_t type);
516 static void     stlpciattach(pcici_t tag, int unit);
517 static void     stlpciintr(void * arg);
518 #endif
519
520 /*
521  *      CD1400 uart specific handling functions.
522  */
523 static void     stl_cd1400setreg(stlport_t *portp, int regnr, int value);
524 static int      stl_cd1400getreg(stlport_t *portp, int regnr);
525 static int      stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
526 static int      stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
527 static void     stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
528 static int      stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
529 static int      stl_cd1400getsignals(stlport_t *portp);
530 static void     stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
531 static void     stl_cd1400ccrwait(stlport_t *portp);
532 static void     stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
533 static void     stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
534 static void     stl_cd1400disableintrs(stlport_t *portp);
535 static void     stl_cd1400sendbreak(stlport_t *portp, long len);
536 static void     stl_cd1400sendflow(stlport_t *portp, int hw, int sw);
537 static int      stl_cd1400datastate(stlport_t *portp);
538 static void     stl_cd1400flush(stlport_t *portp, int flag);
539 static __inline void    stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
540 static void     stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
541 static void     stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
542 static void     stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
543 static void     stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
544
545 /*
546  *      SC26198 uart specific handling functions.
547  */
548 static void     stl_sc26198setreg(stlport_t *portp, int regnr, int value);
549 static int      stl_sc26198getreg(stlport_t *portp, int regnr);
550 static int      stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
551 static int      stl_sc26198getglobreg(stlport_t *portp, int regnr);
552 static int      stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
553 static void     stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
554 static int      stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
555 static int      stl_sc26198getsignals(stlport_t *portp);
556 static void     stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
557 static void     stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
558 static void     stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
559 static void     stl_sc26198disableintrs(stlport_t *portp);
560 static void     stl_sc26198sendbreak(stlport_t *portp, long len);
561 static void     stl_sc26198sendflow(stlport_t *portp, int hw, int sw);
562 static int      stl_sc26198datastate(stlport_t *portp);
563 static void     stl_sc26198flush(stlport_t *portp, int flag);
564 static void     stl_sc26198txunflow(stlport_t *portp);
565 static void     stl_sc26198wait(stlport_t *portp);
566 static void     stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
567 static void     stl_sc26198txisr(stlport_t *port);
568 static void     stl_sc26198rxisr(stlport_t *port, unsigned int iack);
569 static void     stl_sc26198rxgoodchars(stlport_t *portp);
570 static void     stl_sc26198rxbadchars(stlport_t *portp);
571 static void     stl_sc26198otherisr(stlport_t *port, unsigned int iack);
572
573 /*****************************************************************************/
574
575 /*
576  *      Generic UART support structure.
577  */
578 typedef struct uart {
579         int     (*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
580         void    (*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
581         int     (*setport)(stlport_t *portp, struct termios *tiosp);
582         int     (*getsignals)(stlport_t *portp);
583         void    (*setsignals)(stlport_t *portp, int dtr, int rts);
584         void    (*enablerxtx)(stlport_t *portp, int rx, int tx);
585         void    (*startrxtx)(stlport_t *portp, int rx, int tx);
586         void    (*disableintrs)(stlport_t *portp);
587         void    (*sendbreak)(stlport_t *portp, long len);
588         void    (*sendflow)(stlport_t *portp, int hw, int sw);
589         void    (*flush)(stlport_t *portp, int flag);
590         int     (*datastate)(stlport_t *portp);
591         void    (*intr)(stlpanel_t *panelp, unsigned int iobase);
592 } uart_t;
593
594 /*
595  *      Define some macros to make calling these functions nice and clean.
596  */
597 #define stl_panelinit           (* ((uart_t *) panelp->uartp)->panelinit)
598 #define stl_portinit            (* ((uart_t *) portp->uartp)->portinit)
599 #define stl_setport             (* ((uart_t *) portp->uartp)->setport)
600 #define stl_getsignals          (* ((uart_t *) portp->uartp)->getsignals)
601 #define stl_setsignals          (* ((uart_t *) portp->uartp)->setsignals)
602 #define stl_enablerxtx          (* ((uart_t *) portp->uartp)->enablerxtx)
603 #define stl_startrxtx           (* ((uart_t *) portp->uartp)->startrxtx)
604 #define stl_disableintrs        (* ((uart_t *) portp->uartp)->disableintrs)
605 #define stl_sendbreak           (* ((uart_t *) portp->uartp)->sendbreak)
606 #define stl_sendflow            (* ((uart_t *) portp->uartp)->sendflow)
607 #define stl_uartflush           (* ((uart_t *) portp->uartp)->flush)
608 #define stl_datastate           (* ((uart_t *) portp->uartp)->datastate)
609
610 /*****************************************************************************/
611
612 /*
613  *      CD1400 UART specific data initialization.
614  */
615 static uart_t stl_cd1400uart = {
616         stl_cd1400panelinit,
617         stl_cd1400portinit,
618         stl_cd1400setport,
619         stl_cd1400getsignals,
620         stl_cd1400setsignals,
621         stl_cd1400enablerxtx,
622         stl_cd1400startrxtx,
623         stl_cd1400disableintrs,
624         stl_cd1400sendbreak,
625         stl_cd1400sendflow,
626         stl_cd1400flush,
627         stl_cd1400datastate,
628         stl_cd1400eiointr
629 };
630
631 /*
632  *      Define the offsets within the register bank of a cd1400 based panel.
633  *      These io address offsets are common to the EasyIO board as well.
634  */
635 #define EREG_ADDR       0
636 #define EREG_DATA       4
637 #define EREG_RXACK      5
638 #define EREG_TXACK      6
639 #define EREG_MDACK      7
640
641 #define EREG_BANKSIZE   8
642
643 #define CD1400_CLK      25000000
644 #define CD1400_CLK8M    20000000
645
646 /*
647  *      Define the cd1400 baud rate clocks. These are used when calculating
648  *      what clock and divisor to use for the required baud rate. Also
649  *      define the maximum baud rate allowed, and the default base baud.
650  */
651 static int      stl_cd1400clkdivs[] = {
652         CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
653 };
654
655 /*
656  *      Define the maximum baud rate of the cd1400 devices.
657  */
658 #define CD1400_MAXBAUD  230400
659
660 /*****************************************************************************/
661
662 /*
663  *      SC26198 UART specific data initization.
664  */
665 static uart_t stl_sc26198uart = {
666         stl_sc26198panelinit,
667         stl_sc26198portinit,
668         stl_sc26198setport,
669         stl_sc26198getsignals,
670         stl_sc26198setsignals,
671         stl_sc26198enablerxtx,
672         stl_sc26198startrxtx,
673         stl_sc26198disableintrs,
674         stl_sc26198sendbreak,
675         stl_sc26198sendflow,
676         stl_sc26198flush,
677         stl_sc26198datastate,
678         stl_sc26198intr
679 };
680
681 /*
682  *      Define the offsets within the register bank of a sc26198 based panel.
683  */
684 #define XP_DATA         0
685 #define XP_ADDR         1
686 #define XP_MODID        2
687 #define XP_STATUS       2
688 #define XP_IACK         3
689
690 #define XP_BANKSIZE     4
691
692 /*
693  *      Define the sc26198 baud rate table. Offsets within the table
694  *      represent the actual baud rate selector of sc26198 registers.
695  */
696 static unsigned int     sc26198_baudtable[] = {
697         50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
698         4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
699         230400, 460800
700 };
701
702 #define SC26198_NRBAUDS (sizeof(sc26198_baudtable) / sizeof(unsigned int))
703
704 /*
705  *      Define the maximum baud rate of the sc26198 devices.
706  */
707 #define SC26198_MAXBAUD 460800
708
709 /*****************************************************************************/
710
711 /*
712  *      Declare the driver isa structure.
713  */
714 struct isa_driver       stldriver = {
715         stlprobe, stlattach, "stl"
716 };
717
718 /*****************************************************************************/
719
720 #if NPCI > 0
721
722 /*
723  *      Declare the driver pci structure.
724  */
725 static unsigned long    stl_count;
726
727 static struct pci_device        stlpcidriver = {
728         "stl",
729         stlpciprobe,
730         stlpciattach,
731         &stl_count,
732         NULL,
733 };
734
735 COMPAT_PCI_DRIVER (stlpci, stlpcidriver);
736
737 #endif
738
739 /*****************************************************************************/
740
741 #if VFREEBSD >= 220
742
743 /*
744  *      FreeBSD-2.2+ kernel linkage.
745  */
746
747 #define CDEV_MAJOR      72
748 static struct cdevsw stl_cdevsw = {
749         /* name */      "stl",
750         /* maj */       CDEV_MAJOR,
751         /* flags */     D_TTY | D_KQFILTER,
752         /* port */      NULL,
753         /* autoq */     0,
754
755         /* open */      stlopen,
756         /* close */     stlclose,
757         /* read */      ttyread,
758         /* write */     ttywrite,
759         /* ioctl */     stlioctl,
760         /* poll */      ttypoll,
761         /* mmap */      nommap,
762         /* strategy */  nostrategy,
763         /* dump */      nodump,
764         /* psize */     nopsize,
765         /* kqfilter */  ttykqfilter
766 };
767
768 static void stl_drvinit(void *unused)
769 {
770
771         cdevsw_add(&stl_cdevsw);
772 }
773
774 SYSINIT(sidev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,stl_drvinit,NULL)
775
776 #endif
777
778 /*****************************************************************************/
779
780 /*
781  *      Probe for some type of EasyIO or EasyConnection 8/32 board at
782  *      the supplied address. All we do is check if we can find the
783  *      board ID for the board... (Note, PCI boards not checked here,
784  *      they are done in the stlpciprobe() routine).
785  */
786
787 static int stlprobe(struct isa_device *idp)
788 {
789         unsigned int    status;
790
791 #if STLDEBUG
792         printf("stlprobe(idp=%x): unit=%d iobase=%x\n", (int) idp,
793                 idp->id_unit, idp->id_iobase);
794 #endif
795
796         if (idp->id_unit > STL_MAXBRDS)
797                 return(0);
798
799         status = inb(idp->id_iobase + 1);
800         if ((status & ECH_IDBITMASK) == ECH_ID) {
801                 stl_brdprobed[idp->id_unit] = BRD_ECH;
802                 return(1);
803         }
804
805         status = inb(idp->id_iobase + 2);
806         switch (status & EIO_IDBITMASK) {
807         case EIO_8PORTRS:
808         case EIO_8PORTM:
809         case EIO_8PORTDI:
810         case EIO_4PORTRS:
811         case EIO_MK3:
812                 stl_brdprobed[idp->id_unit] = BRD_EASYIO;
813                 return(1);
814         default:
815                 break;
816         }
817         
818         return(0);
819 }
820
821 /*****************************************************************************/
822
823 /*
824  *      Find an available internal board number (unit number). The problem
825  *      is that the same unit numbers can be assigned to different boards
826  *      detected during the ISA and PCI initialization phases.
827  */
828
829 static int stl_findfreeunit()
830 {
831         int     i;
832
833         for (i = 0; (i < STL_MAXBRDS); i++)
834                 if (stl_brds[i] == (stlbrd_t *) NULL)
835                         break;
836         return((i >= STL_MAXBRDS) ? -1 : i);
837 }
838
839 /*****************************************************************************/
840
841 /*
842  *      Allocate resources for and initialize the specified board.
843  */
844
845 static int stlattach(struct isa_device *idp)
846 {
847         stlbrd_t        *brdp;
848         int             boardnr, portnr, minor_dev;
849
850 #if STLDEBUG
851         printf("stlattach(idp=%p): unit=%d iobase=%x\n", (void *) idp,
852                 idp->id_unit, idp->id_iobase);
853 #endif
854
855 /*      idp->id_ointr = stlintr; */
856
857         brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT);
858         if (brdp == (stlbrd_t *) NULL) {
859                 printf("STALLION: failed to allocate memory (size=%d)\n",
860                         sizeof(stlbrd_t));
861                 return(0);
862         }
863         bzero(brdp, sizeof(stlbrd_t));
864
865         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
866                 printf("STALLION: too many boards found, max=%d\n",
867                         STL_MAXBRDS);
868                 return(0);
869         }
870         if (brdp->brdnr >= stl_nrbrds)
871                 stl_nrbrds = brdp->brdnr + 1;
872
873         brdp->unitid = idp->id_unit;
874         brdp->brdtype = stl_brdprobed[idp->id_unit];
875         brdp->ioaddr1 = idp->id_iobase;
876         brdp->ioaddr2 = stl_ioshared;
877         brdp->irq = ffs(idp->id_irq) - 1;
878         brdp->irqtype = stl_irqshared;
879         stl_brdinit(brdp);
880
881         /* register devices for DEVFS */
882         boardnr = brdp->brdnr;
883         make_dev(&stl_cdevsw, boardnr + 0x1000000, UID_ROOT, GID_WHEEL,
884                  0600, "staliomem%d", boardnr);
885
886         for (portnr = 0, minor_dev = boardnr * 0x100000;
887               portnr < 32; portnr++, minor_dev++) {
888                 /* hw ports */
889                 make_dev(&stl_cdevsw, minor_dev,
890                          UID_ROOT, GID_WHEEL, 0600,
891                          "ttyE%d", portnr + (boardnr * 64));
892                 make_dev(&stl_cdevsw, minor_dev + 32,
893                          UID_ROOT, GID_WHEEL, 0600,
894                          "ttyiE%d", portnr + (boardnr * 64));
895                 make_dev(&stl_cdevsw, minor_dev + 64,
896                          UID_ROOT, GID_WHEEL, 0600,
897                          "ttylE%d", portnr + (boardnr * 64));
898                 make_dev(&stl_cdevsw, minor_dev + 128,
899                          UID_ROOT, GID_WHEEL, 0600,
900                          "cue%d", portnr + (boardnr * 64));
901                 make_dev(&stl_cdevsw, minor_dev + 160,
902                          UID_ROOT, GID_WHEEL, 0600,
903                          "cuie%d", portnr + (boardnr * 64));
904                 make_dev(&stl_cdevsw, minor_dev + 192,
905                          UID_ROOT, GID_WHEEL, 0600,
906                          "cule%d", portnr + (boardnr * 64));
907
908                 /* sw ports */
909                 make_dev(&stl_cdevsw, minor_dev + 0x10000,
910                          UID_ROOT, GID_WHEEL, 0600,
911                          "ttyE%d", portnr + (boardnr * 64) + 32);
912                 make_dev(&stl_cdevsw, minor_dev + 32 + 0x10000,
913                          UID_ROOT, GID_WHEEL, 0600,
914                          "ttyiE%d", portnr + (boardnr * 64) + 32);
915                 make_dev(&stl_cdevsw, minor_dev + 64 + 0x10000,
916                          UID_ROOT, GID_WHEEL, 0600,
917                          "ttylE%d", portnr + (boardnr * 64) + 32);
918                 make_dev(&stl_cdevsw, minor_dev + 128 + 0x10000,
919                          UID_ROOT, GID_WHEEL, 0600,
920                          "cue%d", portnr + (boardnr * 64) + 32);
921                 make_dev(&stl_cdevsw, minor_dev + 160 + 0x10000,
922                          UID_ROOT, GID_WHEEL, 0600,
923                          "cuie%d", portnr + (boardnr * 64) + 32);
924                 make_dev(&stl_cdevsw, minor_dev + 192 + 0x10000,
925                          UID_ROOT, GID_WHEEL, 0600,
926                          "cule%d", portnr + (boardnr * 64) + 32);
927         }
928         boardnr = brdp->brdnr;
929         make_dev(&stl_cdevsw, boardnr + 0x1000000, UID_ROOT, GID_WHEEL,
930                  0600, "staliomem%d", boardnr);
931
932         for (portnr = 0, minor_dev = boardnr * 0x100000;
933               portnr < 32; portnr++, minor_dev++) {
934                 /* hw ports */
935                 make_dev(&stl_cdevsw, minor_dev,
936                          UID_ROOT, GID_WHEEL, 0600,
937                          "ttyE%d", portnr + (boardnr * 64));
938                 make_dev(&stl_cdevsw, minor_dev + 32,
939                          UID_ROOT, GID_WHEEL, 0600,
940                          "ttyiE%d", portnr + (boardnr * 64));
941                 make_dev(&stl_cdevsw, minor_dev + 64,
942                          UID_ROOT, GID_WHEEL, 0600,
943                          "ttylE%d", portnr + (boardnr * 64));
944                 make_dev(&stl_cdevsw, minor_dev + 128,
945                          UID_ROOT, GID_WHEEL, 0600,
946                          "cue%d", portnr + (boardnr * 64));
947                 make_dev(&stl_cdevsw, minor_dev + 160,
948                          UID_ROOT, GID_WHEEL, 0600,
949                          "cuie%d", portnr + (boardnr * 64));
950                 make_dev(&stl_cdevsw, minor_dev + 192,
951                          UID_ROOT, GID_WHEEL, 0600,
952                          "cule%d", portnr + (boardnr * 64));
953
954                 /* sw ports */
955                 make_dev(&stl_cdevsw, minor_dev + 0x10000,
956                          UID_ROOT, GID_WHEEL, 0600,
957                          "ttyE%d", portnr + (boardnr * 64) + 32);
958                 make_dev(&stl_cdevsw, minor_dev + 32 + 0x10000,
959                          UID_ROOT, GID_WHEEL, 0600,
960                          "ttyiE%d", portnr + (boardnr * 64) + 32);
961                 make_dev(&stl_cdevsw, minor_dev + 64 + 0x10000,
962                          UID_ROOT, GID_WHEEL, 0600,
963                          "ttylE%d", portnr + (boardnr * 64) + 32);
964                 make_dev(&stl_cdevsw, minor_dev + 128 + 0x10000,
965                          UID_ROOT, GID_WHEEL, 0600,
966                          "cue%d", portnr + (boardnr * 64) + 32);
967                 make_dev(&stl_cdevsw, minor_dev + 160 + 0x10000,
968                          UID_ROOT, GID_WHEEL, 0600,
969                          "cuie%d", portnr + (boardnr * 64) + 32);
970                 make_dev(&stl_cdevsw, minor_dev + 192 + 0x10000,
971                          UID_ROOT, GID_WHEEL, 0600,
972                          "cule%d", portnr + (boardnr * 64) + 32);
973         }
974
975         return(1);
976 }
977
978 /*****************************************************************************/
979
980 #if NPCI > 0
981
982 /*
983  *      Probe specifically for the PCI boards. We need to be a little
984  *      carefull here, since it looks sort like a Nat Semi IDE chip...
985  */
986
987 static const char *stlpciprobe(pcici_t tag, pcidi_t type)
988 {
989         unsigned long   class;
990         int             i, brdtype;
991
992 #if STLDEBUG
993         printf("stlpciprobe(tag=%x,type=%x)\n", (int) &tag, (int) type);
994 #endif
995
996         brdtype = 0;
997         for (i = 0; (i < stl_nrpcibrds); i++) {
998                 if (((type & 0xffff) == stl_pcibrds[i].vendid) &&
999                     (((type >> 16) & 0xffff) == stl_pcibrds[i].devid)) {
1000                         brdtype = stl_pcibrds[i].brdtype;
1001                         break;
1002                 }
1003         }
1004
1005         if (brdtype == 0)
1006                 return((char *) NULL);
1007
1008         class = pci_conf_read(tag, PCI_CLASS_REG);
1009         if ((class & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE)
1010                 return((char *) NULL);
1011
1012         return(stl_brdnames[brdtype]);
1013 }
1014
1015 /*****************************************************************************/
1016
1017 /*
1018  *      Allocate resources for and initialize the specified PCI board.
1019  */
1020
1021 void stlpciattach(pcici_t tag, int unit)
1022 {
1023         stlbrd_t        *brdp;
1024         unsigned int    bar[4];
1025         unsigned int    id;
1026         int             i;
1027         int             boardnr, portnr, minor_dev;
1028
1029 #if STLDEBUG
1030         printf("stlpciattach(tag=%x,unit=%x)\n", (int) &tag, unit);
1031 #endif
1032
1033         brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT);
1034         if (brdp == (stlbrd_t *) NULL) {
1035                 printf("STALLION: failed to allocate memory (size=%d)\n",
1036                         sizeof(stlbrd_t));
1037                 return;
1038         }
1039         bzero(brdp, sizeof(stlbrd_t));
1040
1041         if ((unit < 0) || (unit > STL_MAXBRDS)) {
1042                 printf("STALLION: bad PCI board unit number=%d\n", unit);
1043                 return;
1044         }
1045
1046 /*
1047  *      Allocate us a new driver unique unit number.
1048  */
1049         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
1050                 printf("STALLION: too many boards found, max=%d\n",
1051                         STL_MAXBRDS);
1052                 return;
1053         }
1054         if (brdp->brdnr >= stl_nrbrds)
1055                 stl_nrbrds = brdp->brdnr + 1;
1056
1057 /*
1058  *      Determine what type of PCI board this is...
1059  */
1060         id = (unsigned int) pci_conf_read(tag, 0x0);
1061         for (i = 0; (i < stl_nrpcibrds); i++) {
1062                 if (((id & 0xffff) == stl_pcibrds[i].vendid) &&
1063                     (((id >> 16) & 0xffff) == stl_pcibrds[i].devid)) {
1064                         brdp->brdtype = stl_pcibrds[i].brdtype;
1065                         break;
1066                 }
1067         }
1068
1069         if (i >= stl_nrpcibrds) {
1070                 printf("STALLION: probed PCI board unknown type=%x\n", id);
1071                 return;
1072         }
1073
1074         for (i = 0; (i < 4); i++)
1075                 bar[i] = (unsigned int) pci_conf_read(tag, 0x10 + (i * 4)) &
1076                         0xfffc;
1077
1078         switch (brdp->brdtype) {
1079         case BRD_ECH64PCI:
1080                 brdp->ioaddr1 = bar[1];
1081                 brdp->ioaddr2 = bar[2];
1082                 break;
1083         case BRD_EASYIOPCI:
1084                 brdp->ioaddr1 = bar[2];
1085                 brdp->ioaddr2 = bar[1];
1086                 break;
1087         case BRD_ECHPCI:
1088                 brdp->ioaddr1 = bar[1];
1089                 brdp->ioaddr2 = bar[0];
1090                 break;
1091         default:
1092                 printf("STALLION: unknown PCI board type=%d\n", brdp->brdtype);
1093                 return;
1094                 break;
1095         }
1096
1097         brdp->unitid = brdp->brdnr; /* PCI units auto-assigned */
1098         brdp->irq = ((int) pci_conf_read(tag, 0x3c)) & 0xff;
1099         brdp->irqtype = 0;
1100         if (pci_map_int(tag, stlpciintr, (void *) NULL, &tty_imask) == 0) {
1101                 printf("STALLION: failed to map interrupt irq=%d for unit=%d\n",
1102                         brdp->irq, brdp->brdnr);
1103                 return;
1104         }
1105
1106         stl_brdinit(brdp);
1107
1108         /* register devices for DEVFS */
1109         boardnr = brdp->brdnr;
1110         make_dev(&stl_cdevsw, boardnr + 0x1000000, UID_ROOT, GID_WHEEL,
1111                  0600, "staliomem%d", boardnr);
1112
1113         for (portnr = 0, minor_dev = boardnr * 0x100000;
1114               portnr < 32; portnr++, minor_dev++) {
1115                 /* hw ports */
1116                 make_dev(&stl_cdevsw, minor_dev,
1117                          UID_ROOT, GID_WHEEL, 0600,
1118                          "ttyE%d", portnr + (boardnr * 64));
1119                 make_dev(&stl_cdevsw, minor_dev + 32,
1120                          UID_ROOT, GID_WHEEL, 0600,
1121                          "ttyiE%d", portnr + (boardnr * 64));
1122                 make_dev(&stl_cdevsw, minor_dev + 64,
1123                          UID_ROOT, GID_WHEEL, 0600,
1124                          "ttylE%d", portnr + (boardnr * 64));
1125                 make_dev(&stl_cdevsw, minor_dev + 128,
1126                          UID_ROOT, GID_WHEEL, 0600,
1127                          "cue%d", portnr + (boardnr * 64));
1128                 make_dev(&stl_cdevsw, minor_dev + 160,
1129                          UID_ROOT, GID_WHEEL, 0600,
1130                          "cuie%d", portnr + (boardnr * 64));
1131                 make_dev(&stl_cdevsw, minor_dev + 192,
1132                          UID_ROOT, GID_WHEEL, 0600,
1133                          "cule%d", portnr + (boardnr * 64));
1134
1135                 /* sw ports */
1136                 make_dev(&stl_cdevsw, minor_dev + 0x10000,
1137                          UID_ROOT, GID_WHEEL, 0600,
1138                          "ttyE%d", portnr + (boardnr * 64) + 32);
1139                 make_dev(&stl_cdevsw, minor_dev + 32 + 0x10000,
1140                          UID_ROOT, GID_WHEEL, 0600,
1141                          "ttyiE%d", portnr + (boardnr * 64) + 32);
1142                 make_dev(&stl_cdevsw, minor_dev + 64 + 0x10000,
1143                          UID_ROOT, GID_WHEEL, 0600,
1144                          "ttylE%d", portnr + (boardnr * 64) + 32);
1145                 make_dev(&stl_cdevsw, minor_dev + 128 + 0x10000,
1146                          UID_ROOT, GID_WHEEL, 0600,
1147                          "cue%d", portnr + (boardnr * 64) + 32);
1148                 make_dev(&stl_cdevsw, minor_dev + 160 + 0x10000,
1149                          UID_ROOT, GID_WHEEL, 0600,
1150                          "cuie%d", portnr + (boardnr * 64) + 32);
1151                 make_dev(&stl_cdevsw, minor_dev + 192 + 0x10000,
1152                          UID_ROOT, GID_WHEEL, 0600,
1153                          "cule%d", portnr + (boardnr * 64) + 32);
1154         }
1155 }
1156
1157 #endif
1158
1159 /*****************************************************************************/
1160
1161 STATIC int stlopen(dev_t dev, int flag, int mode, struct thread *td)
1162 {
1163         struct tty      *tp;
1164         stlport_t       *portp;
1165         int             error, callout, x;
1166
1167 #if STLDEBUG
1168         printf("stlopen(dev=%x,flag=%x,mode=%x,p=%x)\n", (int) dev, flag,
1169                 mode, (int) p);
1170 #endif
1171
1172 /*
1173  *      Firstly check if the supplied device number is a valid device.
1174  */
1175         if (minor(dev) & STL_MEMDEV)
1176                 return(0);
1177
1178         portp = stl_dev2port(dev);
1179         if (portp == (stlport_t *) NULL)
1180                 return(ENXIO);
1181         if (minor(dev) & STL_CTRLDEV)
1182                 return(0);
1183         tp = &portp->tty;
1184         dev->si_tty = tp;
1185         callout = minor(dev) & STL_CALLOUTDEV;
1186         error = 0;
1187
1188         x = spltty();
1189
1190 stlopen_restart:
1191 /*
1192  *      Wait here for the DTR drop timeout period to expire.
1193  */
1194         while (portp->state & ASY_DTRWAIT) {
1195                 error = tsleep(&portp->dtrwait, PCATCH, "stldtr", 0);
1196                 if (error)
1197                         goto stlopen_end;
1198         }
1199         
1200 /*
1201  *      We have a valid device, so now we check if it is already open.
1202  *      If not then initialize the port hardware and set up the tty
1203  *      struct as required.
1204  */
1205         if ((tp->t_state & TS_ISOPEN) == 0) {
1206                 tp->t_oproc = stl_start;
1207                 tp->t_stop = stl_stop;
1208                 tp->t_param = stl_param;
1209                 tp->t_dev = dev;
1210                 tp->t_termios = callout ? portp->initouttios :
1211                         portp->initintios;
1212                 stl_rawopen(portp);
1213                 ttsetwater(tp);
1214                 if ((portp->sigs & TIOCM_CD) || callout)
1215                         (*linesw[tp->t_line].l_modem)(tp, 1);
1216         } else {
1217                 if (callout) {
1218                         if (portp->callout == 0) {
1219                                 error = EBUSY;
1220                                 goto stlopen_end;
1221                         }
1222                 } else {
1223                         if (portp->callout != 0) {
1224                                 if (flag & O_NONBLOCK) {
1225                                         error = EBUSY;
1226                                         goto stlopen_end;
1227                                 }
1228                                 error = tsleep(&portp->callout,
1229                                             PCATCH, "stlcall", 0);
1230                                 if (error)
1231                                         goto stlopen_end;
1232                                 goto stlopen_restart;
1233                         }
1234                 }
1235                 if ((tp->t_state & TS_XCLUDE) && suser(td)) {
1236                         error = EBUSY;
1237                         goto stlopen_end;
1238                 }
1239         }
1240
1241 /*
1242  *      If this port is not the callout device and we do not have carrier
1243  *      then we need to sleep, waiting for it to be asserted.
1244  */
1245         if (((tp->t_state & TS_CARR_ON) == 0) && !callout &&
1246                         ((tp->t_cflag & CLOCAL) == 0) &&
1247                         ((flag & O_NONBLOCK) == 0)) {
1248                 portp->waitopens++;
1249                 error = tsleep(TSA_CARR_ON(tp), PCATCH, "stldcd", 0);
1250                 portp->waitopens--;
1251                 if (error)
1252                         goto stlopen_end;
1253                 goto stlopen_restart;
1254         }
1255
1256 /*
1257  *      Open the line discipline.
1258  */
1259         error = (*linesw[tp->t_line].l_open)(dev, tp);
1260         stl_ttyoptim(portp, &tp->t_termios);
1261         if ((tp->t_state & TS_ISOPEN) && callout)
1262                 portp->callout = 1;
1263
1264 /*
1265  *      If for any reason we get to here and the port is not actually
1266  *      open then close of the physical hardware - no point leaving it
1267  *      active when the open failed...
1268  */
1269 stlopen_end:
1270         splx(x);
1271         if (((tp->t_state & TS_ISOPEN) == 0) && (portp->waitopens == 0))
1272                 stl_rawclose(portp);
1273
1274         return(error);
1275 }
1276
1277 /*****************************************************************************/
1278
1279 STATIC int stlclose(dev_t dev, int flag, int mode, struct thread *td)
1280 {
1281         struct tty      *tp;
1282         stlport_t       *portp;
1283         int             x;
1284
1285 #if STLDEBUG
1286         printf("stlclose(dev=%s,flag=%x,mode=%x,p=%p)\n", devtoname(dev),
1287                 flag, mode, (void *) p);
1288 #endif
1289
1290         if (minor(dev) & STL_MEMDEV)
1291                 return(0);
1292         if (minor(dev) & STL_CTRLDEV)
1293                 return(0);
1294
1295         portp = stl_dev2port(dev);
1296         if (portp == (stlport_t *) NULL)
1297                 return(ENXIO);
1298         tp = &portp->tty;
1299
1300         x = spltty();
1301         (*linesw[tp->t_line].l_close)(tp, flag);
1302         stl_ttyoptim(portp, &tp->t_termios);
1303         stl_rawclose(portp);
1304         ttyclose(tp);
1305         splx(x);
1306         return(0);
1307 }
1308
1309 /*****************************************************************************/
1310
1311 #if VFREEBSD >= 220
1312
1313 STATIC void stl_stop(struct tty *tp, int rw)
1314 {
1315 #if STLDEBUG
1316         printf("stl_stop(tp=%x,rw=%x)\n", (int) tp, rw);
1317 #endif
1318
1319         stl_flush((stlport_t *) tp, rw);
1320 }
1321
1322 #else
1323
1324 STATIC int stlstop(struct tty *tp, int rw)
1325 {
1326 #if STLDEBUG
1327         printf("stlstop(tp=%x,rw=%x)\n", (int) tp, rw);
1328 #endif
1329
1330         stl_flush((stlport_t *) tp, rw);
1331         return(0);
1332 }
1333
1334 #endif
1335
1336 /*****************************************************************************/
1337
1338 STATIC int stlioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
1339                     struct thread *td)
1340 {
1341         struct termios  *newtios, *localtios;
1342         struct tty      *tp;
1343         stlport_t       *portp;
1344         int             error, i, x;
1345
1346 #if STLDEBUG
1347         printf("stlioctl(dev=%s,cmd=%lx,data=%p,flag=%x,p=%p)\n",
1348                 devtoname(dev), cmd, (void *) data, flag, (void *) p);
1349 #endif
1350
1351         if (minor(dev) & STL_MEMDEV)
1352                 return(stl_memioctl(dev, cmd, data, flag, td));
1353
1354         portp = stl_dev2port(dev);
1355         if (portp == (stlport_t *) NULL)
1356                 return(ENODEV);
1357         tp = &portp->tty;
1358         error = 0;
1359         
1360 /*
1361  *      First up handle ioctls on the control devices.
1362  */
1363         if (minor(dev) & STL_CTRLDEV) {
1364                 if ((minor(dev) & STL_CTRLDEV) == STL_CTRLINIT)
1365                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
1366                                 &portp->initouttios : &portp->initintios;
1367                 else if ((minor(dev) & STL_CTRLDEV) == STL_CTRLLOCK)
1368                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
1369                                 &portp->lockouttios : &portp->lockintios;
1370                 else
1371                         return(ENODEV);
1372
1373                 switch (cmd) {
1374                 case TIOCSETA:
1375                         if ((error = suser(td)) == 0)
1376                                 *localtios = *((struct termios *) data);
1377                         break;
1378                 case TIOCGETA:
1379                         *((struct termios *) data) = *localtios;
1380                         break;
1381                 case TIOCGETD:
1382                         *((int *) data) = TTYDISC;
1383                         break;
1384                 case TIOCGWINSZ:
1385                         bzero(data, sizeof(struct winsize));
1386                         break;
1387                 default:
1388                         error = ENOTTY;
1389                         break;
1390                 }
1391                 return(error);
1392         }
1393
1394 /*
1395  *      Deal with 4.3 compatability issues if we have too...
1396  */
1397 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1398         if (1) {
1399                 struct termios  tios;
1400                 unsigned long   oldcmd;
1401
1402                 tios = tp->t_termios;
1403                 oldcmd = cmd;
1404                 if ((error = ttsetcompat(tp, &cmd, data, &tios)))
1405                         return(error);
1406                 if (cmd != oldcmd)
1407                         data = (caddr_t) &tios;
1408         }
1409 #endif
1410
1411 /*
1412  *      Carry out some pre-cmd processing work first...
1413  *      Hmmm, not so sure we want this, disable for now...
1414  */
1415         if ((cmd == TIOCSETA) || (cmd == TIOCSETAW) || (cmd == TIOCSETAF)) {
1416                 newtios = (struct termios *) data;
1417                 localtios = (minor(dev) & STL_CALLOUTDEV) ? 
1418                         &portp->lockouttios : &portp->lockintios;
1419
1420                 newtios->c_iflag = (tp->t_iflag & localtios->c_iflag) |
1421                         (newtios->c_iflag & ~localtios->c_iflag);
1422                 newtios->c_oflag = (tp->t_oflag & localtios->c_oflag) |
1423                         (newtios->c_oflag & ~localtios->c_oflag);
1424                 newtios->c_cflag = (tp->t_cflag & localtios->c_cflag) |
1425                         (newtios->c_cflag & ~localtios->c_cflag);
1426                 newtios->c_lflag = (tp->t_lflag & localtios->c_lflag) |
1427                         (newtios->c_lflag & ~localtios->c_lflag);
1428                 for (i = 0; (i < NCCS); i++) {
1429                         if (localtios->c_cc[i] != 0)
1430                                 newtios->c_cc[i] = tp->t_cc[i];
1431                 }
1432                 if (localtios->c_ispeed != 0)
1433                         newtios->c_ispeed = tp->t_ispeed;
1434                 if (localtios->c_ospeed != 0)
1435                         newtios->c_ospeed = tp->t_ospeed;
1436         }
1437
1438 /*
1439  *      Call the line discipline and the common command processing to
1440  *      process this command (if they can).
1441  */
1442         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
1443         if (error != ENOIOCTL)
1444                 return(error);
1445
1446         x = spltty();
1447         error = ttioctl(tp, cmd, data, flag);
1448         stl_ttyoptim(portp, &tp->t_termios);
1449         if (error != ENOIOCTL) {
1450                 splx(x);
1451                 return(error);
1452         }
1453
1454         error = 0;
1455
1456 /*
1457  *      Process local commands here. These are all commands that only we
1458  *      can take care of (they all rely on actually doing something special
1459  *      to the actual hardware).
1460  */
1461         switch (cmd) {
1462         case TIOCSBRK:
1463                 stl_sendbreak(portp, -1);
1464                 break;
1465         case TIOCCBRK:
1466                 stl_sendbreak(portp, -2);
1467                 break;
1468         case TIOCSDTR:
1469                 stl_setsignals(portp, 1, -1);
1470                 break;
1471         case TIOCCDTR:
1472                 stl_setsignals(portp, 0, -1);
1473                 break;
1474         case TIOCMSET:
1475                 i = *((int *) data);
1476                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : 0),
1477                         ((i & TIOCM_RTS) ? 1 : 0));
1478                 break;
1479         case TIOCMBIS:
1480                 i = *((int *) data);
1481                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : -1),
1482                         ((i & TIOCM_RTS) ? 1 : -1));
1483                 break;
1484         case TIOCMBIC:
1485                 i = *((int *) data);
1486                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 0 : -1),
1487                         ((i & TIOCM_RTS) ? 0 : -1));
1488                 break;
1489         case TIOCMGET:
1490                 *((int *) data) = (stl_getsignals(portp) | TIOCM_LE);
1491                 break;
1492         case TIOCMSDTRWAIT:
1493                 if ((error = suser(td)) == 0)
1494                         portp->dtrwait = *((int *) data) * hz / 100;
1495                 break;
1496         case TIOCMGDTRWAIT:
1497                 *((int *) data) = portp->dtrwait * 100 / hz;
1498                 break;
1499         case TIOCTIMESTAMP:
1500                 portp->dotimestamp = 1;
1501                 *((struct timeval *) data) = portp->timestamp;
1502                 break;
1503         default:
1504                 error = ENOTTY;
1505                 break;
1506         }
1507         splx(x);
1508
1509         return(error);
1510 }
1511 /*****************************************************************************/
1512
1513 /*
1514  *      Convert the specified minor device number into a port struct
1515  *      pointer. Return NULL if the device number is not a valid port.
1516  */
1517
1518 STATIC stlport_t *stl_dev2port(dev_t dev)
1519 {
1520         stlbrd_t        *brdp;
1521
1522         brdp = stl_brds[MKDEV2BRD(dev)];
1523         if (brdp == (stlbrd_t *) NULL)
1524                 return((stlport_t *) NULL);
1525         return(brdp->ports[MKDEV2PORT(dev)]);
1526 }
1527
1528 /*****************************************************************************/
1529
1530 /*
1531  *      Initialize the port hardware. This involves enabling the transmitter
1532  *      and receiver, setting the port configuration, and setting the initial
1533  *      signal state.
1534  */
1535
1536 static int stl_rawopen(stlport_t *portp)
1537 {
1538 #if STLDEBUG
1539         printf("stl_rawopen(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1540                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1541 #endif
1542
1543         stl_setport(portp, &portp->tty.t_termios);
1544         portp->sigs = stl_getsignals(portp);
1545         stl_setsignals(portp, 1, 1);
1546         stl_enablerxtx(portp, 1, 1);
1547         stl_startrxtx(portp, 1, 0);
1548         return(0);
1549 }
1550
1551 /*****************************************************************************/
1552
1553 /*
1554  *      Shutdown the hardware of a port. Disable its transmitter and
1555  *      receiver, and maybe drop signals if appropriate.
1556  */
1557
1558 static int stl_rawclose(stlport_t *portp)
1559 {
1560         struct tty      *tp;
1561
1562 #if STLDEBUG
1563         printf("stl_rawclose(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1564                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1565 #endif
1566
1567         tp = &portp->tty;
1568         stl_disableintrs(portp);
1569         stl_enablerxtx(portp, 0, 0);
1570         stl_flush(portp, (FWRITE | FREAD));
1571         if (tp->t_cflag & HUPCL) {
1572                 stl_setsignals(portp, 0, 0);
1573                 if (portp->dtrwait != 0) {
1574                         portp->state |= ASY_DTRWAIT;
1575                         timeout(stl_dtrwakeup, portp, portp->dtrwait);
1576                 }
1577         }
1578         portp->callout = 0;
1579         portp->brklen = 0;
1580         portp->state &= ~(ASY_ACTIVE | ASY_RTSFLOW);
1581         wakeup(&portp->callout);
1582         wakeup(TSA_CARR_ON(tp));
1583         return(0);
1584 }
1585
1586 /*****************************************************************************/
1587
1588 /*
1589  *      Clear the DTR waiting flag, and wake up any sleepers waiting for
1590  *      DTR wait period to finish.
1591  */
1592
1593 static void stl_dtrwakeup(void *arg)
1594 {
1595         stlport_t       *portp;
1596
1597         portp = (stlport_t *) arg;
1598         portp->state &= ~ASY_DTRWAIT;
1599         wakeup(&portp->dtrwait);
1600 }
1601
1602 /*****************************************************************************/
1603
1604 /*
1605  *      Start (or continue) the transfer of TX data on this port. If the
1606  *      port is not currently busy then load up the interrupt ring queue
1607  *      buffer and kick of the transmitter. If the port is running low on
1608  *      TX data then refill the ring queue. This routine is also used to
1609  *      activate input flow control!
1610  */
1611
1612 static void stl_start(struct tty *tp)
1613 {
1614         stlport_t       *portp;
1615         unsigned int    len, stlen;
1616         char            *head, *tail;
1617         int             count, x;
1618
1619         portp = (stlport_t *) tp;
1620
1621 #if STLDEBUG
1622         printf("stl_start(tp=%x): brdnr=%d portnr=%d\n", (int) tp, 
1623                 portp->brdnr, portp->portnr);
1624 #endif
1625
1626         x = spltty();
1627
1628 /*
1629  *      Check if the ports input has been blocked, and take appropriate action.
1630  *      Not very often do we really need to do anything, so make it quick.
1631  */
1632         if (tp->t_state & TS_TBLOCK) {
1633                 if ((portp->state & ASY_RTSFLOWMODE) &&
1634                     ((portp->state & ASY_RTSFLOW) == 0))
1635                         stl_flowcontrol(portp, 0, -1);
1636         } else {
1637                 if (portp->state & ASY_RTSFLOW)
1638                         stl_flowcontrol(portp, 1, -1);
1639         }
1640
1641 #if VFREEBSD == 205
1642 /*
1643  *      Check if the output cooked clist buffers are near empty, wake up
1644  *      the line discipline to fill it up.
1645  */
1646         if (tp->t_outq.c_cc <= tp->t_lowat) {
1647                 if (tp->t_state & TS_ASLEEP) {
1648                         tp->t_state &= ~TS_ASLEEP;
1649                         wakeup(&tp->t_outq);
1650                 }
1651                 selwakeup(&tp->t_wsel);
1652         }
1653 #endif
1654
1655         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
1656                 splx(x);
1657                 return;
1658         }
1659
1660 /*
1661  *      Copy data from the clists into the interrupt ring queue. This will
1662  *      require at most 2 copys... What we do is calculate how many chars
1663  *      can fit into the ring queue, and how many can fit in 1 copy. If after
1664  *      the first copy there is still more room then do the second copy. 
1665  *      The beauty of this type of ring queue is that we do not need to
1666  *      spl protect our-selves, since we only ever update the head pointer,
1667  *      and the interrupt routine only ever updates the tail pointer.
1668  */
1669         if (tp->t_outq.c_cc != 0) {
1670                 head = portp->tx.head;
1671                 tail = portp->tx.tail;
1672                 if (head >= tail) {
1673                         len = STL_TXBUFSIZE - (head - tail) - 1;
1674                         stlen = portp->tx.endbuf - head;
1675                 } else {
1676                         len = tail - head - 1;
1677                         stlen = len;
1678                 }
1679
1680                 if (len > 0) {
1681                         stlen = MIN(len, stlen);
1682                         count = q_to_b(&tp->t_outq, head, stlen);
1683                         len -= count;
1684                         head += count;
1685                         if (head >= portp->tx.endbuf) {
1686                                 head = portp->tx.buf;
1687                                 if (len > 0) {
1688                                         stlen = q_to_b(&tp->t_outq, head, len);
1689                                         head += stlen;
1690                                         count += stlen;
1691                                 }
1692                         }
1693                         portp->tx.head = head;
1694                         if (count > 0)
1695                                 stl_startrxtx(portp, -1, 1);
1696                 }
1697
1698 /*
1699  *              If we sent something, make sure we are called again.
1700  */
1701                 tp->t_state |= TS_BUSY;
1702         }
1703
1704 #if VFREEBSD != 205
1705 /*
1706  *      Do any writer wakeups.
1707  */
1708         ttwwakeup(tp);
1709 #endif
1710
1711         splx(x);
1712 }
1713
1714 /*****************************************************************************/
1715
1716 static void stl_flush(stlport_t *portp, int flag)
1717 {
1718         char    *head, *tail;
1719         int     len, x;
1720
1721 #if STLDEBUG
1722         printf("stl_flush(portp=%x,flag=%x)\n", (int) portp, flag);
1723 #endif
1724
1725         if (portp == (stlport_t *) NULL)
1726                 return;
1727
1728         x = spltty();
1729
1730         if (flag & FWRITE) {
1731                 stl_uartflush(portp, FWRITE);
1732                 portp->tx.tail = portp->tx.head;
1733         }
1734
1735 /*
1736  *      The only thing to watch out for when flushing the read side is
1737  *      the RX status buffer. The interrupt code relys on the status
1738  *      bytes as being zeroed all the time (it does not bother setting
1739  *      a good char status to 0, it expects that it already will be).
1740  *      We also need to un-flow the RX channel if flow control was
1741  *      active.
1742  */
1743         if (flag & FREAD) {
1744                 head = portp->rx.head;
1745                 tail = portp->rx.tail;
1746                 if (head != tail) {
1747                         if (head >= tail) {
1748                                 len = head - tail;
1749                         } else {
1750                                 len = portp->rx.endbuf - tail;
1751                                 bzero(portp->rxstatus.buf,
1752                                         (head - portp->rx.buf));
1753                         }
1754                         bzero((tail + STL_RXBUFSIZE), len);
1755                         portp->rx.tail = head;
1756                 }
1757
1758                 if ((portp->state & ASY_RTSFLOW) &&
1759                                 ((portp->tty.t_state & TS_TBLOCK) == 0))
1760                         stl_flowcontrol(portp, 1, -1);
1761         }
1762
1763         splx(x);
1764 }
1765
1766 /*****************************************************************************/
1767
1768 /*
1769  *      Interrupt handler for host based boards. Interrupts for all boards
1770  *      are vectored through here.
1771  */
1772
1773 void stlintr(int unit)
1774 {
1775         stlbrd_t        *brdp;
1776         int             i;
1777
1778 #if STLDEBUG
1779         printf("stlintr(unit=%d)\n", unit);
1780 #endif
1781
1782         for (i = 0; (i < stl_nrbrds); i++) {
1783                 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
1784                         continue;
1785                 if (brdp->state == 0)
1786                         continue;
1787                 (* brdp->isr)(brdp);
1788         }
1789 }
1790
1791 /*****************************************************************************/
1792
1793 #if NPCI > 0
1794
1795 static void stlpciintr(void *arg)
1796 {
1797         stlintr(0);
1798 }
1799
1800 #endif
1801
1802 /*****************************************************************************/
1803
1804 /*
1805  *      Interrupt service routine for EasyIO boards.
1806  */
1807
1808 static void stl_eiointr(stlbrd_t *brdp)
1809 {
1810         stlpanel_t      *panelp;
1811         int             iobase;
1812
1813 #if STLDEBUG
1814         printf("stl_eiointr(brdp=%p)\n", brdp);
1815 #endif
1816
1817         panelp = (stlpanel_t *) brdp->panels[0];
1818         iobase = panelp->iobase;
1819         while (inb(brdp->iostatus) & EIO_INTRPEND)
1820                 (* panelp->isr)(panelp, iobase);
1821 }
1822
1823 /*
1824  *      Interrupt service routine for ECH-AT board types.
1825  */
1826
1827 static void stl_echatintr(stlbrd_t *brdp)
1828 {
1829         stlpanel_t      *panelp;
1830         unsigned int    ioaddr;
1831         int             bnknr;
1832
1833         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
1834
1835         while (inb(brdp->iostatus) & ECH_INTRPEND) {
1836                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1837                         ioaddr = brdp->bnkstataddr[bnknr];
1838                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1839                                 panelp = brdp->bnk2panel[bnknr];
1840                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1841                         }
1842                 }
1843         }
1844
1845         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
1846 }
1847
1848 /*****************************************************************************/
1849
1850 /*
1851  *      Interrupt service routine for ECH-MCA board types.
1852  */
1853
1854 static void stl_echmcaintr(stlbrd_t *brdp)
1855 {
1856         stlpanel_t      *panelp;
1857         unsigned int    ioaddr;
1858         int             bnknr;
1859
1860         while (inb(brdp->iostatus) & ECH_INTRPEND) {
1861                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1862                         ioaddr = brdp->bnkstataddr[bnknr];
1863                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1864                                 panelp = brdp->bnk2panel[bnknr];
1865                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1866                         }
1867                 }
1868         }
1869 }
1870
1871 /*****************************************************************************/
1872
1873 /*
1874  *      Interrupt service routine for ECH-PCI board types.
1875  */
1876
1877 static void stl_echpciintr(stlbrd_t *brdp)
1878 {
1879         stlpanel_t      *panelp;
1880         unsigned int    ioaddr;
1881         int             bnknr, recheck;
1882
1883 #if STLDEBUG
1884         printf("stl_echpciintr(brdp=%x)\n", (int) brdp);
1885 #endif
1886
1887         for (;;) {
1888                 recheck = 0;
1889                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1890                         outb(brdp->ioctrl, brdp->bnkpageaddr[bnknr]);
1891                         ioaddr = brdp->bnkstataddr[bnknr];
1892                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1893                                 panelp = brdp->bnk2panel[bnknr];
1894                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1895                                 recheck++;
1896                         }
1897                 }
1898                 if (! recheck)
1899                         break;
1900         }
1901 }
1902
1903 /*****************************************************************************/
1904
1905 /*
1906  *      Interrupt service routine for EC8/64-PCI board types.
1907  */
1908
1909 static void stl_echpci64intr(stlbrd_t *brdp)
1910 {
1911         stlpanel_t      *panelp;
1912         unsigned int    ioaddr;
1913         int             bnknr;
1914
1915 #if STLDEBUG
1916         printf("stl_echpci64intr(brdp=%p)\n", brdp);
1917 #endif
1918
1919         while (inb(brdp->ioctrl) & 0x1) {
1920                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1921                         ioaddr = brdp->bnkstataddr[bnknr];
1922 #if STLDEBUG
1923         printf("    --> ioaddr=%x status=%x(%x)\n", ioaddr, inb(ioaddr) & ECH_PNLINTRPEND, inb(ioaddr));
1924 #endif
1925                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1926                                 panelp = brdp->bnk2panel[bnknr];
1927                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1928                         }
1929                 }
1930         }
1931 }
1932
1933 /*****************************************************************************/
1934
1935 /*
1936  *      If we haven't scheduled a timeout then do it, some port needs high
1937  *      level processing.
1938  */
1939
1940 static void stl_dotimeout()
1941 {
1942 #if STLDEBUG
1943         printf("stl_dotimeout()\n");
1944 #endif
1945
1946         if (stl_doingtimeout == 0) {
1947                 timeout(stl_poll, 0, 1);
1948                 stl_doingtimeout++;
1949         }
1950 }
1951
1952 /*****************************************************************************/
1953
1954 /*
1955  *      Service "software" level processing. Too slow or painfull to be done
1956  *      at real hardware interrupt time. This way we might also be able to
1957  *      do some service on other waiting ports as well...
1958  */
1959
1960 static void stl_poll(void *arg)
1961 {
1962         stlbrd_t        *brdp;
1963         stlport_t       *portp;
1964         struct tty      *tp;
1965         int             brdnr, portnr, rearm, x;
1966
1967 #if STLDEBUG
1968         printf("stl_poll()\n");
1969 #endif
1970
1971         stl_doingtimeout = 0;
1972         rearm = 0;
1973
1974         x = spltty();
1975         for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
1976                 if ((brdp = stl_brds[brdnr]) == (stlbrd_t *) NULL)
1977                         continue;
1978                 for (portnr = 0; (portnr < brdp->nrports); portnr++) {
1979                         if ((portp = brdp->ports[portnr]) == (stlport_t *) NULL)
1980                                 continue;
1981                         if ((portp->state & ASY_ACTIVE) == 0)
1982                                 continue;
1983                         tp = &portp->tty;
1984
1985                         if (portp->state & ASY_RXDATA)
1986                                 stl_rxprocess(portp);
1987                         if (portp->state & ASY_DCDCHANGE) {
1988                                 portp->state &= ~ASY_DCDCHANGE;
1989                                 portp->sigs = stl_getsignals(portp);
1990                                 (*linesw[tp->t_line].l_modem)(tp,
1991                                         (portp->sigs & TIOCM_CD));
1992                         }
1993                         if (portp->state & ASY_TXEMPTY) {
1994                                 if (stl_datastate(portp) == 0) {
1995                                         portp->state &= ~ASY_TXEMPTY;
1996                                         tp->t_state &= ~TS_BUSY;
1997                                         (*linesw[tp->t_line].l_start)(tp);
1998                                 }
1999                         }
2000                         if (portp->state & ASY_TXLOW) {
2001                                 portp->state &= ~ASY_TXLOW;
2002                                 (*linesw[tp->t_line].l_start)(tp);
2003                         }
2004
2005                         if (portp->state & ASY_ACTIVE)
2006                                 rearm++;
2007                 }
2008         }
2009         splx(x);
2010
2011         if (rearm)
2012                 stl_dotimeout();
2013 }
2014
2015 /*****************************************************************************/
2016
2017 /*
2018  *      Process the RX data that has been buffered up in the RX ring queue.
2019  */
2020
2021 static void stl_rxprocess(stlport_t *portp)
2022 {
2023         struct tty      *tp;
2024         unsigned int    len, stlen, lostlen;
2025         char            *head, *tail;
2026         char            status;
2027         int             ch;
2028
2029 #if STLDEBUG
2030         printf("stl_rxprocess(portp=%x): brdnr=%d portnr=%d\n", (int) portp, 
2031                 portp->brdnr, portp->portnr);
2032 #endif
2033
2034         tp = &portp->tty;
2035         portp->state &= ~ASY_RXDATA;
2036
2037         if ((tp->t_state & TS_ISOPEN) == 0) {
2038                 stl_flush(portp, FREAD);
2039                 return;
2040         }
2041
2042 /*
2043  *      Calculate the amount of data in the RX ring queue. Also calculate
2044  *      the largest single copy size...
2045  */
2046         head = portp->rx.head;
2047         tail = portp->rx.tail;
2048         if (head >= tail) {
2049                 len = head - tail;
2050                 stlen = len;
2051         } else {
2052                 len = STL_RXBUFSIZE - (tail - head);
2053                 stlen = portp->rx.endbuf - tail;
2054         }
2055
2056         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
2057                 if (len > 0) {
2058                         if (((tp->t_rawq.c_cc + len) >= TTYHOG) &&
2059                                         ((portp->state & ASY_RTSFLOWMODE) ||
2060                                         (tp->t_iflag & IXOFF)) &&
2061                                         ((tp->t_state & TS_TBLOCK) == 0)) {
2062                                 ch = TTYHOG - tp->t_rawq.c_cc - 1;
2063                                 len = (ch > 0) ? ch : 0;
2064                                 stlen = MIN(stlen, len);
2065                                 ttyblock(tp);
2066                         }
2067                         lostlen = b_to_q(tail, stlen, &tp->t_rawq);
2068                         tail += stlen;
2069                         len -= stlen;
2070                         if (tail >= portp->rx.endbuf) {
2071                                 tail = portp->rx.buf;
2072                                 lostlen += b_to_q(tail, len, &tp->t_rawq);
2073                                 tail += len;
2074                         }
2075                         portp->stats.rxlost += lostlen;
2076                         ttwakeup(tp);
2077                         portp->rx.tail = tail;
2078                 }
2079         } else {
2080                 while (portp->rx.tail != head) {
2081                         ch = (unsigned char) *(portp->rx.tail);
2082                         status = *(portp->rx.tail + STL_RXBUFSIZE);
2083                         if (status) {
2084                                 *(portp->rx.tail + STL_RXBUFSIZE) = 0;
2085                                 if (status & ST_BREAK)
2086                                         ch |= TTY_BI;
2087                                 if (status & ST_FRAMING)
2088                                         ch |= TTY_FE;
2089                                 if (status & ST_PARITY)
2090                                         ch |= TTY_PE;
2091                                 if (status & ST_OVERRUN)
2092                                         ch |= TTY_OE;
2093                         }
2094                         (*linesw[tp->t_line].l_rint)(ch, tp);
2095                         if (portp->rx.tail == head)
2096                                 break;
2097
2098                         if (++(portp->rx.tail) >= portp->rx.endbuf)
2099                                 portp->rx.tail = portp->rx.buf;
2100                 }
2101         }
2102
2103         if (head != portp->rx.tail)
2104                 portp->state |= ASY_RXDATA;
2105
2106 /*
2107  *      If we were flow controled then maybe the buffer is low enough that
2108  *      we can re-activate it.
2109  */
2110         if ((portp->state & ASY_RTSFLOW) && ((tp->t_state & TS_TBLOCK) == 0))
2111                 stl_flowcontrol(portp, 1, -1);
2112 }
2113
2114 /*****************************************************************************/
2115
2116 static int stl_param(struct tty *tp, struct termios *tiosp)
2117 {
2118         stlport_t       *portp;
2119
2120         portp = (stlport_t *) tp;
2121         if (portp == (stlport_t *) NULL)
2122                 return(ENODEV);
2123
2124         return(stl_setport(portp, tiosp));
2125 }
2126
2127 /*****************************************************************************/
2128
2129 /*
2130  *      Action the flow control as required. The hw and sw args inform the
2131  *      routine what flow control methods it should try.
2132  */
2133
2134 static void stl_flowcontrol(stlport_t *portp, int hw, int sw)
2135 {
2136         unsigned char   *head, *tail;
2137         int             len, hwflow;
2138
2139 #if STLDEBUG
2140         printf("stl_flowcontrol(portp=%x,hw=%d,sw=%d)\n", (int) portp, hw, sw);
2141 #endif
2142
2143         hwflow = -1;
2144
2145         if (portp->state & ASY_RTSFLOWMODE) {
2146                 if (hw == 0) {
2147                         if ((portp->state & ASY_RTSFLOW) == 0)
2148                                 hwflow = 0;
2149                 } else if (hw > 0) {
2150                         if (portp->state & ASY_RTSFLOW) {
2151                                 head = portp->rx.head;
2152                                 tail = portp->rx.tail;
2153                                 len = (head >= tail) ? (head - tail) :
2154                                         (STL_RXBUFSIZE - (tail - head));
2155                                 if (len < STL_RXBUFHIGH)
2156                                         hwflow = 1;
2157                         }
2158                 }
2159         }
2160
2161 /*
2162  *      We have worked out what to do, if anything. So now apply it to the
2163  *      UART port.
2164  */
2165         stl_sendflow(portp, hwflow, sw);
2166 }
2167
2168 /*****************************************************************************/
2169
2170 /*
2171  *      Enable l_rint processing bypass mode if tty modes allow it.
2172  */
2173
2174 static void stl_ttyoptim(stlport_t *portp, struct termios *tiosp)
2175 {
2176         struct tty      *tp;
2177
2178         tp = &portp->tty;
2179         if (((tiosp->c_iflag &
2180               (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP)) == 0) &&
2181             (((tiosp->c_iflag & BRKINT) == 0) || (tiosp->c_iflag & IGNBRK)) &&
2182             (((tiosp->c_iflag & PARMRK) == 0) ||
2183                 ((tiosp->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))) &&
2184             ((tiosp->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) ==0) &&
2185             (linesw[tp->t_line].l_rint == ttyinput))
2186                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2187         else
2188                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2189         portp->hotchar = linesw[tp->t_line].l_hotchar;
2190 }
2191
2192 /*****************************************************************************/
2193
2194 /*
2195  *      Try and find and initialize all the ports on a panel. We don't care
2196  *      what sort of board these ports are on - since the port io registers
2197  *      are almost identical when dealing with ports.
2198  */
2199
2200 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2201 {
2202         stlport_t       *portp;
2203         unsigned int    chipmask;
2204         int             i, j;
2205
2206 #if STLDEBUG
2207         printf("stl_initports(panelp=%x)\n", (int) panelp);
2208 #endif
2209
2210         chipmask = stl_panelinit(brdp, panelp);
2211
2212 /*
2213  *      All UART's are initialized if found. Now go through and setup
2214  *      each ports data structures. Also initialize each individual
2215  *      UART port.
2216  */
2217         for (i = 0; (i < panelp->nrports); i++) {
2218                 portp = (stlport_t *) malloc(sizeof(stlport_t), M_TTYS,
2219                         M_NOWAIT);
2220                 if (portp == (stlport_t *) NULL) {
2221                         printf("STALLION: failed to allocate port memory "
2222                                 "(size=%d)\n", sizeof(stlport_t));
2223                         break;
2224                 }
2225                 bzero(portp, sizeof(stlport_t));
2226
2227                 portp->portnr = i;
2228                 portp->brdnr = panelp->brdnr;
2229                 portp->panelnr = panelp->panelnr;
2230                 portp->uartp = panelp->uartp;
2231                 portp->clk = brdp->clk;
2232                 panelp->ports[i] = portp;
2233
2234                 j = STL_TXBUFSIZE + (2 * STL_RXBUFSIZE);
2235                 portp->tx.buf = (char *) malloc(j, M_TTYS, M_NOWAIT);
2236                 if (portp->tx.buf == (char *) NULL) {
2237                         printf("STALLION: failed to allocate buffer memory "
2238                                 "(size=%d)\n", j);
2239                         break;
2240                 }
2241                 portp->tx.endbuf = portp->tx.buf + STL_TXBUFSIZE;
2242                 portp->tx.head = portp->tx.buf;
2243                 portp->tx.tail = portp->tx.buf;
2244                 portp->rx.buf = portp->tx.buf + STL_TXBUFSIZE;
2245                 portp->rx.endbuf = portp->rx.buf + STL_RXBUFSIZE;
2246                 portp->rx.head = portp->rx.buf;
2247                 portp->rx.tail = portp->rx.buf;
2248                 portp->rxstatus.buf = portp->rx.buf + STL_RXBUFSIZE;
2249                 portp->rxstatus.endbuf = portp->rxstatus.buf + STL_RXBUFSIZE;
2250                 portp->rxstatus.head = portp->rxstatus.buf;
2251                 portp->rxstatus.tail = portp->rxstatus.buf;
2252                 bzero(portp->rxstatus.head, STL_RXBUFSIZE);
2253
2254                 portp->initintios.c_ispeed = STL_DEFSPEED;
2255                 portp->initintios.c_ospeed = STL_DEFSPEED;
2256                 portp->initintios.c_cflag = STL_DEFCFLAG;
2257                 portp->initintios.c_iflag = 0;
2258                 portp->initintios.c_oflag = 0;
2259                 portp->initintios.c_lflag = 0;
2260                 bcopy(&ttydefchars[0], &portp->initintios.c_cc[0],
2261                         sizeof(portp->initintios.c_cc));
2262                 portp->initouttios = portp->initintios;
2263                 portp->dtrwait = 3 * hz;
2264
2265                 stl_portinit(brdp, panelp, portp);
2266         }
2267
2268         return(0);
2269 }
2270
2271 /*****************************************************************************/
2272
2273 /*
2274  *      Try to find and initialize an EasyIO board.
2275  */
2276
2277 static int stl_initeio(stlbrd_t *brdp)
2278 {
2279         stlpanel_t      *panelp;
2280         unsigned int    status;
2281
2282 #if STLDEBUG
2283         printf("stl_initeio(brdp=%x)\n", (int) brdp);
2284 #endif
2285
2286         brdp->ioctrl = brdp->ioaddr1 + 1;
2287         brdp->iostatus = brdp->ioaddr1 + 2;
2288         brdp->clk = EIO_CLK;
2289         brdp->isr = stl_eiointr;
2290
2291         status = inb(brdp->iostatus);
2292         switch (status & EIO_IDBITMASK) {
2293         case EIO_8PORTM:
2294                 brdp->clk = EIO_CLK8M;
2295                 /* fall thru */
2296         case EIO_8PORTRS:
2297         case EIO_8PORTDI:
2298                 brdp->nrports = 8;
2299                 break;
2300         case EIO_4PORTRS:
2301                 brdp->nrports = 4;
2302                 break;
2303         case EIO_MK3:
2304                 switch (status & EIO_BRDMASK) {
2305                 case ID_BRD4:
2306                         brdp->nrports = 4;
2307                         break;
2308                 case ID_BRD8:
2309                         brdp->nrports = 8;
2310                         break;
2311                 case ID_BRD16:
2312                         brdp->nrports = 16;
2313                         break;
2314                 default:
2315                         return(ENODEV);
2316                 }
2317                 brdp->ioctrl++;
2318                 break;
2319         default:
2320                 return(ENODEV);
2321         }
2322
2323         if (brdp->brdtype == BRD_EASYIOPCI) {
2324                 outb((brdp->ioaddr2 + 0x4c), 0x41);
2325         } else {
2326 /*
2327  *      Check that the supplied IRQ is good and then use it to setup the
2328  *      programmable interrupt bits on EIO board. Also set the edge/level
2329  *      triggered interrupt bit.
2330  */
2331                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2332                         (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2333                         printf("STALLION: invalid irq=%d for brd=%d\n",
2334                                brdp->irq, brdp->brdnr);
2335                         return(EINVAL);
2336                 }
2337                 outb(brdp->ioctrl, (stl_vecmap[brdp->irq] |
2338                         ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)));
2339         }
2340
2341         panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS, M_NOWAIT);
2342         if (panelp == (stlpanel_t *) NULL) {
2343                 printf("STALLION: failed to allocate memory (size=%d)\n",
2344                         sizeof(stlpanel_t));
2345                 return(ENOMEM);
2346         }
2347         bzero(panelp, sizeof(stlpanel_t));
2348
2349         panelp->brdnr = brdp->brdnr;
2350         panelp->panelnr = 0;
2351         panelp->nrports = brdp->nrports;
2352         panelp->iobase = brdp->ioaddr1;
2353         panelp->hwid = status;
2354         if ((status & EIO_IDBITMASK) == EIO_MK3) {
2355                 panelp->uartp = (void *) &stl_sc26198uart;
2356                 panelp->isr = stl_sc26198intr;
2357         } else {
2358                 panelp->uartp = (void *) &stl_cd1400uart;
2359                 panelp->isr = stl_cd1400eiointr;
2360         }
2361         brdp->panels[0] = panelp;
2362         brdp->nrpanels = 1;
2363         brdp->hwid = status;
2364         brdp->state |= BRD_FOUND;
2365         return(0);
2366 }
2367
2368 /*****************************************************************************/
2369
2370 /*
2371  *      Try to find an ECH board and initialize it. This code is capable of
2372  *      dealing with all types of ECH board.
2373  */
2374
2375 static int stl_initech(stlbrd_t *brdp)
2376 {
2377         stlpanel_t      *panelp;
2378         unsigned int    status, nxtid;
2379         int             panelnr, ioaddr, banknr, i;
2380
2381 #if STLDEBUG
2382         printf("stl_initech(brdp=%x)\n", (int) brdp);
2383 #endif
2384
2385 /*
2386  *      Set up the initial board register contents for boards. This varys a
2387  *      bit between the different board types. So we need to handle each
2388  *      separately. Also do a check that the supplied IRQ is good.
2389  */
2390         switch (brdp->brdtype) {
2391
2392         case BRD_ECH:
2393                 brdp->isr = stl_echatintr;
2394                 brdp->ioctrl = brdp->ioaddr1 + 1;
2395                 brdp->iostatus = brdp->ioaddr1 + 1;
2396                 status = inb(brdp->iostatus);
2397                 if ((status & ECH_IDBITMASK) != ECH_ID)
2398                         return(ENODEV);
2399                 brdp->hwid = status;
2400
2401                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2402                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2403                         printf("STALLION: invalid irq=%d for brd=%d\n",
2404                                 brdp->irq, brdp->brdnr);
2405                         return(EINVAL);
2406                 }
2407                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2408                 status |= (stl_vecmap[brdp->irq] << 1);
2409                 outb(brdp->ioaddr1, (status | ECH_BRDRESET));
2410                 brdp->ioctrlval = ECH_INTENABLE |
2411                         ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2412                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
2413                 outb(brdp->ioaddr1, status);
2414                 break;
2415
2416         case BRD_ECHMC:
2417                 brdp->isr = stl_echmcaintr;
2418                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2419                 brdp->iostatus = brdp->ioctrl;
2420                 status = inb(brdp->iostatus);
2421                 if ((status & ECH_IDBITMASK) != ECH_ID)
2422                         return(ENODEV);
2423                 brdp->hwid = status;
2424
2425                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2426                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2427                         printf("STALLION: invalid irq=%d for brd=%d\n",
2428                                 brdp->irq, brdp->brdnr);
2429                         return(EINVAL);
2430                 }
2431                 outb(brdp->ioctrl, ECHMC_BRDRESET);
2432                 outb(brdp->ioctrl, ECHMC_INTENABLE);
2433                 break;
2434
2435         case BRD_ECHPCI:
2436                 brdp->isr = stl_echpciintr;
2437                 brdp->ioctrl = brdp->ioaddr1 + 2;
2438                 break;
2439
2440         case BRD_ECH64PCI:
2441                 brdp->isr = stl_echpci64intr;
2442                 brdp->ioctrl = brdp->ioaddr2 + 0x40;
2443                 outb((brdp->ioaddr1 + 0x4c), 0x43);
2444                 break;
2445
2446         default:
2447                 printf("STALLION: unknown board type=%d\n", brdp->brdtype);
2448                 break;
2449         }
2450
2451         brdp->clk = ECH_CLK;
2452
2453 /*
2454  *      Scan through the secondary io address space looking for panels.
2455  *      As we find'em allocate and initialize panel structures for each.
2456  */
2457         ioaddr = brdp->ioaddr2;
2458         panelnr = 0;
2459         nxtid = 0;
2460         banknr = 0;
2461
2462         for (i = 0; (i < STL_MAXPANELS); i++) {
2463                 if (brdp->brdtype == BRD_ECHPCI) {
2464                         outb(brdp->ioctrl, nxtid);
2465                         ioaddr = brdp->ioaddr2;
2466                 }
2467                 status = inb(ioaddr + ECH_PNLSTATUS);
2468                 if ((status & ECH_PNLIDMASK) != nxtid)
2469                         break;
2470                 panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS,
2471                         M_NOWAIT);
2472                 if (panelp == (stlpanel_t *) NULL) {
2473                         printf("STALLION: failed to allocate memory"
2474                                 "(size=%d)\n", sizeof(stlpanel_t));
2475                         break;
2476                 }
2477                 bzero(panelp, sizeof(stlpanel_t));
2478                 panelp->brdnr = brdp->brdnr;
2479                 panelp->panelnr = panelnr;
2480                 panelp->iobase = ioaddr;
2481                 panelp->pagenr = nxtid;
2482                 panelp->hwid = status;
2483                 brdp->bnk2panel[banknr] = panelp;
2484                 brdp->bnkpageaddr[banknr] = nxtid;
2485                 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2486
2487                 if (status & ECH_PNLXPID) {
2488                         panelp->uartp = (void *) &stl_sc26198uart;
2489                         panelp->isr = stl_sc26198intr;
2490                         if (status & ECH_PNL16PORT) {
2491                                 panelp->nrports = 16;
2492                                 brdp->bnk2panel[banknr] = panelp;
2493                                 brdp->bnkpageaddr[banknr] = nxtid;
2494                                 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2495                                         ECH_PNLSTATUS;
2496                         } else {
2497                                 panelp->nrports = 8;
2498                         }
2499                 } else {
2500                         panelp->uartp = (void *) &stl_cd1400uart;
2501                         panelp->isr = stl_cd1400echintr;
2502                         if (status & ECH_PNL16PORT) {
2503                                 panelp->nrports = 16;
2504                                 panelp->ackmask = 0x80;
2505                                 if (brdp->brdtype != BRD_ECHPCI)
2506                                         ioaddr += EREG_BANKSIZE;
2507                                 brdp->bnk2panel[banknr] = panelp;
2508                                 brdp->bnkpageaddr[banknr] = ++nxtid;
2509                                 brdp->bnkstataddr[banknr++] = ioaddr +
2510                                         ECH_PNLSTATUS;
2511                         } else {
2512                                 panelp->nrports = 8;
2513                                 panelp->ackmask = 0xc0;
2514                         }
2515                 }
2516
2517                 nxtid++;
2518                 ioaddr += EREG_BANKSIZE;
2519                 brdp->nrports += panelp->nrports;
2520                 brdp->panels[panelnr++] = panelp;
2521                 if ((brdp->brdtype == BRD_ECH) || (brdp->brdtype == BRD_ECHMC)){
2522                         if (ioaddr >= (brdp->ioaddr2 + 0x20)) {
2523                                 printf("STALLION: too many ports attached "
2524                                         "to board %d, remove last module\n",
2525                                         brdp->brdnr);
2526                                 break;
2527                         }
2528                 }
2529         }
2530
2531         brdp->nrpanels = panelnr;
2532         brdp->nrbnks = banknr;
2533         if (brdp->brdtype == BRD_ECH)
2534                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
2535
2536         brdp->state |= BRD_FOUND;
2537         return(0);
2538 }
2539
2540 /*****************************************************************************/
2541
2542 /*
2543  *      Initialize and configure the specified board. This firstly probes
2544  *      for the board, if it is found then the board is initialized and
2545  *      then all its ports are initialized as well.
2546  */
2547
2548 static int stl_brdinit(stlbrd_t *brdp)
2549 {
2550         stlpanel_t      *panelp;
2551         int             i, j, k;
2552
2553 #if STLDEBUG
2554         printf("stl_brdinit(brdp=%x): unit=%d type=%d io1=%x io2=%x irq=%d\n",
2555                 (int) brdp, brdp->brdnr, brdp->brdtype, brdp->ioaddr1,
2556                 brdp->ioaddr2, brdp->irq);
2557 #endif
2558
2559         switch (brdp->brdtype) {
2560         case BRD_EASYIO:
2561         case BRD_EASYIOPCI:
2562                 stl_initeio(brdp);
2563                 break;
2564         case BRD_ECH:
2565         case BRD_ECHMC:
2566         case BRD_ECHPCI:
2567         case BRD_ECH64PCI:
2568                 stl_initech(brdp);
2569                 break;
2570         default:
2571                 printf("STALLION: unit=%d is unknown board type=%d\n",
2572                         brdp->brdnr, brdp->brdtype);
2573                 return(ENODEV);
2574         }
2575
2576         stl_brds[brdp->brdnr] = brdp;
2577         if ((brdp->state & BRD_FOUND) == 0) {
2578 #if 0
2579                 printf("STALLION: %s board not found, unit=%d io=%x irq=%d\n",
2580                         stl_brdnames[brdp->brdtype], brdp->brdnr,
2581                         brdp->ioaddr1, brdp->irq);
2582 #endif
2583                 return(ENODEV);
2584         }
2585
2586         for (i = 0, k = 0; (i < STL_MAXPANELS); i++) {
2587                 panelp = brdp->panels[i];
2588                 if (panelp != (stlpanel_t *) NULL) {
2589                         stl_initports(brdp, panelp);
2590                         for (j = 0; (j < panelp->nrports); j++)
2591                                 brdp->ports[k++] = panelp->ports[j];
2592                 }
2593         }
2594
2595         printf("stl%d: %s (driver version %s) unit=%d nrpanels=%d nrports=%d\n",
2596                 brdp->unitid, stl_brdnames[brdp->brdtype], stl_drvversion,
2597                 brdp->brdnr, brdp->nrpanels, brdp->nrports);
2598         return(0);
2599 }
2600
2601 /*****************************************************************************/
2602
2603 /*
2604  *      Return the board stats structure to user app.
2605  */
2606
2607 static int stl_getbrdstats(caddr_t data)
2608 {
2609         stlbrd_t        *brdp;
2610         stlpanel_t      *panelp;
2611         int             i;
2612
2613         stl_brdstats = *((combrd_t *) data);
2614         if (stl_brdstats.brd >= STL_MAXBRDS)
2615                 return(-ENODEV);
2616         brdp = stl_brds[stl_brdstats.brd];
2617         if (brdp == (stlbrd_t *) NULL)
2618                 return(-ENODEV);
2619
2620         bzero(&stl_brdstats, sizeof(combrd_t));
2621         stl_brdstats.brd = brdp->brdnr;
2622         stl_brdstats.type = brdp->brdtype;
2623         stl_brdstats.hwid = brdp->hwid;
2624         stl_brdstats.state = brdp->state;
2625         stl_brdstats.ioaddr = brdp->ioaddr1;
2626         stl_brdstats.ioaddr2 = brdp->ioaddr2;
2627         stl_brdstats.irq = brdp->irq;
2628         stl_brdstats.nrpanels = brdp->nrpanels;
2629         stl_brdstats.nrports = brdp->nrports;
2630         for (i = 0; (i < brdp->nrpanels); i++) {
2631                 panelp = brdp->panels[i];
2632                 stl_brdstats.panels[i].panel = i;
2633                 stl_brdstats.panels[i].hwid = panelp->hwid;
2634                 stl_brdstats.panels[i].nrports = panelp->nrports;
2635         }
2636
2637         *((combrd_t *) data) = stl_brdstats;;
2638         return(0);
2639 }
2640
2641 /*****************************************************************************/
2642
2643 /*
2644  *      Resolve the referenced port number into a port struct pointer.
2645  */
2646
2647 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2648 {
2649         stlbrd_t        *brdp;
2650         stlpanel_t      *panelp;
2651
2652         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2653                 return((stlport_t *) NULL);
2654         brdp = stl_brds[brdnr];
2655         if (brdp == (stlbrd_t *) NULL)
2656                 return((stlport_t *) NULL);
2657         if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2658                 return((stlport_t *) NULL);
2659         panelp = brdp->panels[panelnr];
2660         if (panelp == (stlpanel_t *) NULL)
2661                 return((stlport_t *) NULL);
2662         if ((portnr < 0) || (portnr >= panelp->nrports))
2663                 return((stlport_t *) NULL);
2664         return(panelp->ports[portnr]);
2665 }
2666
2667 /*****************************************************************************/
2668
2669 /*
2670  *      Return the port stats structure to user app. A NULL port struct
2671  *      pointer passed in means that we need to find out from the app
2672  *      what port to get stats for (used through board control device).
2673  */
2674
2675 static int stl_getportstats(stlport_t *portp, caddr_t data)
2676 {
2677         unsigned char   *head, *tail;
2678
2679         if (portp == (stlport_t *) NULL) {
2680                 stl_comstats = *((comstats_t *) data);
2681                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2682                         stl_comstats.port);
2683                 if (portp == (stlport_t *) NULL)
2684                         return(-ENODEV);
2685         }
2686
2687         portp->stats.state = portp->state;
2688         /*portp->stats.flags = portp->flags;*/
2689         portp->stats.hwid = portp->hwid;
2690         portp->stats.ttystate = portp->tty.t_state;
2691         portp->stats.cflags = portp->tty.t_cflag;
2692         portp->stats.iflags = portp->tty.t_iflag;
2693         portp->stats.oflags = portp->tty.t_oflag;
2694         portp->stats.lflags = portp->tty.t_lflag;
2695
2696         head = portp->tx.head;
2697         tail = portp->tx.tail;
2698         portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
2699                 (STL_TXBUFSIZE - (tail - head)));
2700
2701         head = portp->rx.head;
2702         tail = portp->rx.tail;
2703         portp->stats.rxbuffered = (head >= tail) ? (head - tail) :
2704                 (STL_RXBUFSIZE - (tail - head));
2705
2706         portp->stats.signals = (unsigned long) stl_getsignals(portp);
2707
2708         *((comstats_t *) data) = portp->stats;
2709         return(0);
2710 }
2711
2712 /*****************************************************************************/
2713
2714 /*
2715  *      Clear the port stats structure. We also return it zeroed out...
2716  */
2717
2718 static int stl_clrportstats(stlport_t *portp, caddr_t data)
2719 {
2720         if (portp == (stlport_t *) NULL) {
2721                 stl_comstats = *((comstats_t *) data);
2722                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2723                         stl_comstats.port);
2724                 if (portp == (stlport_t *) NULL)
2725                         return(ENODEV);
2726         }
2727
2728         bzero(&portp->stats, sizeof(comstats_t));
2729         portp->stats.brd = portp->brdnr;
2730         portp->stats.panel = portp->panelnr;
2731         portp->stats.port = portp->portnr;
2732         *((comstats_t *) data) = stl_comstats;
2733         return(0);
2734 }
2735
2736 /*****************************************************************************/
2737
2738 /*
2739  *      The "staliomem" device is used for stats collection in this driver.
2740  */
2741
2742 static int stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
2743                         struct thread *td)
2744 {
2745         int             rc;
2746
2747 #if STLDEBUG
2748         printf("stl_memioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
2749                 devtoname(dev), cmd, (void *) data, flag);
2750 #endif
2751
2752         rc = 0;
2753
2754         switch (cmd) {
2755         case COM_GETPORTSTATS:
2756                 rc = stl_getportstats((stlport_t *) NULL, data);
2757                 break;
2758         case COM_CLRPORTSTATS:
2759                 rc = stl_clrportstats((stlport_t *) NULL, data);
2760                 break;
2761         case COM_GETBRDSTATS:
2762                 rc = stl_getbrdstats(data);
2763                 break;
2764         default:
2765                 rc = ENOTTY;
2766                 break;
2767         }
2768
2769         return(rc);
2770 }
2771
2772 /*****************************************************************************/
2773
2774 /*****************************************************************************/
2775 /*                         CD1400 UART CODE                                  */
2776 /*****************************************************************************/
2777
2778 /*
2779  *      These functions get/set/update the registers of the cd1400 UARTs.
2780  *      Access to the cd1400 registers is via an address/data io port pair.
2781  */
2782
2783 static int stl_cd1400getreg(stlport_t *portp, int regnr)
2784 {
2785         outb(portp->ioaddr, (regnr + portp->uartaddr));
2786         return(inb(portp->ioaddr + EREG_DATA));
2787 }
2788
2789 /*****************************************************************************/
2790
2791 static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
2792 {
2793         outb(portp->ioaddr, (regnr + portp->uartaddr));
2794         outb((portp->ioaddr + EREG_DATA), value);
2795 }
2796
2797 /*****************************************************************************/
2798
2799 static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
2800 {
2801         outb(portp->ioaddr, (regnr + portp->uartaddr));
2802         if (inb(portp->ioaddr + EREG_DATA) != value) {
2803                 outb((portp->ioaddr + EREG_DATA), value);
2804                 return(1);
2805         }
2806         return(0);
2807 }
2808
2809 /*****************************************************************************/
2810
2811 static void stl_cd1400flush(stlport_t *portp, int flag)
2812 {
2813         int     x;
2814
2815 #if STLDEBUG
2816         printf("stl_cd1400flush(portp=%x,flag=%x)\n", (int) portp, flag);
2817 #endif
2818
2819         if (portp == (stlport_t *) NULL)
2820                 return;
2821
2822         x = spltty();
2823
2824         if (flag & FWRITE) {
2825                 BRDENABLE(portp->brdnr, portp->pagenr);
2826                 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2827                 stl_cd1400ccrwait(portp);
2828                 stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
2829                 stl_cd1400ccrwait(portp);
2830                 BRDDISABLE(portp->brdnr);
2831         }
2832
2833         if (flag & FREAD) {
2834                 /* Hmmm */
2835         }
2836
2837         splx(x);
2838 }
2839
2840 /*****************************************************************************/
2841
2842 static void stl_cd1400ccrwait(stlport_t *portp)
2843 {
2844         int     i;
2845
2846         for (i = 0; (i < CCR_MAXWAIT); i++) {
2847                 if (stl_cd1400getreg(portp, CCR) == 0)
2848                         return;
2849         }
2850
2851         printf("stl%d: cd1400 device not responding, panel=%d port=%d\n",
2852             portp->brdnr, portp->panelnr, portp->portnr);
2853 }
2854
2855 /*****************************************************************************/
2856
2857 /*
2858  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
2859  *      chars is pretty simple, stuff as many as possible from the TX buffer
2860  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
2861  *      are embedded as commands in the data stream. Oh no, had to use a goto!
2862  */
2863
2864 static __inline void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
2865 {
2866         struct tty      *tp;
2867         stlport_t       *portp;
2868         unsigned char   ioack, srer;
2869         char            *head, *tail;
2870         int             len, stlen;
2871
2872 #if STLDEBUG
2873         printf("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
2874 #endif
2875
2876         ioack = inb(ioaddr + EREG_TXACK);
2877         if (((ioack & panelp->ackmask) != 0) ||
2878             ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
2879                 printf("STALLION: bad TX interrupt ack value=%x\n",
2880                         ioack);
2881                 return;
2882         }
2883         portp = panelp->ports[(ioack >> 3)];
2884         tp = &portp->tty;
2885
2886 /*
2887  *      Unfortunately we need to handle breaks in the data stream, since
2888  *      this is the only way to generate them on the cd1400. Do it now if
2889  *      a break is to be sent. Some special cases here: brklen is -1 then
2890  *      start sending an un-timed break, if brklen is -2 then stop sending
2891  *      an un-timed break, if brklen is -3 then we have just sent an
2892  *      un-timed break and do not want any data to go out, if brklen is -4
2893  *      then a break has just completed so clean up the port settings.
2894  */
2895         if (portp->brklen != 0) {
2896                 if (portp->brklen >= -1) {
2897                         outb(ioaddr, (TDR + portp->uartaddr));
2898                         outb((ioaddr + EREG_DATA), ETC_CMD);
2899                         outb((ioaddr + EREG_DATA), ETC_STARTBREAK);
2900                         if (portp->brklen > 0) {
2901                                 outb((ioaddr + EREG_DATA), ETC_CMD);
2902                                 outb((ioaddr + EREG_DATA), ETC_DELAY);
2903                                 outb((ioaddr + EREG_DATA), portp->brklen);
2904                                 outb((ioaddr + EREG_DATA), ETC_CMD);
2905                                 outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
2906                                 portp->brklen = -4;
2907                         } else {
2908                                 portp->brklen = -3;
2909                         }
2910                 } else if (portp->brklen == -2) {
2911                         outb(ioaddr, (TDR + portp->uartaddr));
2912                         outb((ioaddr + EREG_DATA), ETC_CMD);
2913                         outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
2914                         portp->brklen = -4;
2915                 } else if (portp->brklen == -3) {
2916                         outb(ioaddr, (SRER + portp->uartaddr));
2917                         srer = inb(ioaddr + EREG_DATA);
2918                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
2919                         outb((ioaddr + EREG_DATA), srer);
2920                 } else {
2921                         outb(ioaddr, (COR2 + portp->uartaddr));
2922                         outb((ioaddr + EREG_DATA),
2923                                 (inb(ioaddr + EREG_DATA) & ~COR2_ETC));
2924                         portp->brklen = 0;
2925                 }
2926                 goto stl_txalldone;
2927         }
2928
2929         head = portp->tx.head;
2930         tail = portp->tx.tail;
2931         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
2932         if ((len == 0) || ((len < STL_TXBUFLOW) &&
2933             ((portp->state & ASY_TXLOW) == 0))) {
2934                 portp->state |= ASY_TXLOW;
2935                 stl_dotimeout();
2936         }
2937
2938         if (len == 0) {
2939                 outb(ioaddr, (SRER + portp->uartaddr));
2940                 srer = inb(ioaddr + EREG_DATA);
2941                 if (srer & SRER_TXDATA) {
2942                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
2943                 } else {
2944                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
2945                         portp->state |= ASY_TXEMPTY;
2946                         portp->state &= ~ASY_TXBUSY;
2947                 }
2948                 outb((ioaddr + EREG_DATA), srer);
2949         } else {
2950                 len = MIN(len, CD1400_TXFIFOSIZE);
2951                 portp->stats.txtotal += len;
2952                 stlen = MIN(len, (portp->tx.endbuf - tail));
2953                 outb(ioaddr, (TDR + portp->uartaddr));
2954                 outsb((ioaddr + EREG_DATA), tail, stlen);
2955                 len -= stlen;
2956                 tail += stlen;
2957                 if (tail >= portp->tx.endbuf)
2958                         tail = portp->tx.buf;
2959                 if (len > 0) {
2960                         outsb((ioaddr + EREG_DATA), tail, len);
2961                         tail += len;
2962                 }
2963                 portp->tx.tail = tail;
2964         }
2965
2966 stl_txalldone:
2967         outb(ioaddr, (EOSRR + portp->uartaddr));
2968         outb((ioaddr + EREG_DATA), 0);
2969 }
2970
2971 /*****************************************************************************/
2972
2973 /*
2974  *      Receive character interrupt handler. Determine if we have good chars
2975  *      or bad chars and then process appropriately.
2976  */
2977
2978 static __inline void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
2979 {
2980         stlport_t       *portp;
2981         struct tty      *tp;
2982         unsigned int    ioack, len, buflen, stlen;
2983         unsigned char   status;
2984         char            ch;
2985         char            *head, *tail;
2986
2987 #if STLDEBUG
2988         printf("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
2989 #endif
2990
2991         ioack = inb(ioaddr + EREG_RXACK);
2992         if ((ioack & panelp->ackmask) != 0) {
2993                 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
2994                 return;
2995         }
2996         portp = panelp->ports[(ioack >> 3)];
2997         tp = &portp->tty;
2998
2999 /*
3000  *      First up, calculate how much room there is in the RX ring queue.
3001  *      We also want to keep track of the longest possible copy length,
3002  *      this has to allow for the wrapping of the ring queue.
3003  */
3004         head = portp->rx.head;
3005         tail = portp->rx.tail;
3006         if (head >= tail) {
3007                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
3008                 stlen = portp->rx.endbuf - head;
3009         } else {
3010                 buflen = tail - head - 1;
3011                 stlen = buflen;
3012         }
3013
3014 /*
3015  *      Check if the input buffer is near full. If so then we should take
3016  *      some flow control action... It is very easy to do hardware and
3017  *      software flow control from here since we have the port selected on
3018  *      the UART.
3019  */
3020         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
3021                 if (((portp->state & ASY_RTSFLOW) == 0) &&
3022                     (portp->state & ASY_RTSFLOWMODE)) {
3023                         portp->state |= ASY_RTSFLOW;
3024                         stl_cd1400setreg(portp, MCOR1,
3025                                 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3026                         stl_cd1400setreg(portp, MSVR2, 0);
3027                         portp->stats.rxrtsoff++;
3028                 }
3029         }
3030
3031 /*
3032  *      OK we are set, process good data... If the RX ring queue is full
3033  *      just chuck the chars - don't leave them in the UART.
3034  */
3035         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
3036                 outb(ioaddr, (RDCR + portp->uartaddr));
3037                 len = inb(ioaddr + EREG_DATA);
3038                 if (buflen == 0) {
3039                         outb(ioaddr, (RDSR + portp->uartaddr));
3040                         insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
3041                         portp->stats.rxlost += len;
3042                         portp->stats.rxtotal += len;
3043                 } else {
3044                         len = MIN(len, buflen);
3045                         portp->stats.rxtotal += len;
3046                         stlen = MIN(len, stlen);
3047                         if (len > 0) {
3048                                 outb(ioaddr, (RDSR + portp->uartaddr));
3049                                 insb((ioaddr + EREG_DATA), head, stlen);
3050                                 head += stlen;
3051                                 if (head >= portp->rx.endbuf) {
3052                                         head = portp->rx.buf;
3053                                         len -= stlen;
3054                                         insb((ioaddr + EREG_DATA), head, len);
3055                                         head += len;
3056                                 }
3057                         }
3058                 }
3059         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
3060                 outb(ioaddr, (RDSR + portp->uartaddr));
3061                 status = inb(ioaddr + EREG_DATA);
3062                 ch = inb(ioaddr + EREG_DATA);
3063                 if (status & ST_BREAK)
3064                         portp->stats.rxbreaks++;
3065                 if (status & ST_FRAMING)
3066                         portp->stats.rxframing++;
3067                 if (status & ST_PARITY)
3068                         portp->stats.rxparity++;
3069                 if (status & ST_OVERRUN)
3070                         portp->stats.rxoverrun++;
3071                 if (status & ST_SCHARMASK) {
3072                         if ((status & ST_SCHARMASK) == ST_SCHAR1)
3073                                 portp->stats.txxon++;
3074                         if ((status & ST_SCHARMASK) == ST_SCHAR2)
3075                                 portp->stats.txxoff++;
3076                         goto stl_rxalldone;
3077                 }
3078                 if ((portp->rxignoremsk & status) == 0) {
3079                         if ((tp->t_state & TS_CAN_BYPASS_L_RINT) &&
3080                             ((status & ST_FRAMING) ||
3081                             ((status & ST_PARITY) && (tp->t_iflag & INPCK))))
3082                                 ch = 0;
3083                         if ((portp->rxmarkmsk & status) == 0)
3084                                 status = 0;
3085                         *(head + STL_RXBUFSIZE) = status;
3086                         *head++ = ch;
3087                         if (head >= portp->rx.endbuf)
3088                                 head = portp->rx.buf;
3089                 }
3090         } else {
3091                 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
3092                 return;
3093         }
3094
3095         portp->rx.head = head;
3096         portp->state |= ASY_RXDATA;
3097         stl_dotimeout();
3098
3099 stl_rxalldone:
3100         outb(ioaddr, (EOSRR + portp->uartaddr));
3101         outb((ioaddr + EREG_DATA), 0);
3102 }
3103
3104 /*****************************************************************************/
3105
3106 /*
3107  *      Modem interrupt handler. The is called when the modem signal line
3108  *      (DCD) has changed state.
3109  */
3110
3111 static __inline void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
3112 {
3113         stlport_t       *portp;
3114         unsigned int    ioack;
3115         unsigned char   misr;
3116
3117 #if STLDEBUG
3118         printf("stl_cd1400mdmisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
3119 #endif
3120
3121         ioack = inb(ioaddr + EREG_MDACK);
3122         if (((ioack & panelp->ackmask) != 0) ||
3123             ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
3124                 printf("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
3125                 return;
3126         }
3127         portp = panelp->ports[(ioack >> 3)];
3128
3129         outb(ioaddr, (MISR + portp->uartaddr));
3130         misr = inb(ioaddr + EREG_DATA);
3131         if (misr & MISR_DCD) {
3132                 portp->state |= ASY_DCDCHANGE;
3133                 portp->stats.modem++;
3134                 stl_dotimeout();
3135         }
3136
3137         outb(ioaddr, (EOSRR + portp->uartaddr));
3138         outb((ioaddr + EREG_DATA), 0);
3139 }
3140
3141 /*****************************************************************************/
3142
3143 /*
3144  *      Interrupt service routine for cd1400 EasyIO boards.
3145  */
3146
3147 static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
3148 {
3149         unsigned char   svrtype;
3150
3151 #if STLDEBUG
3152         printf("stl_cd1400eiointr(panelp=%x,iobase=%x)\n", (int) panelp,
3153                 iobase);
3154 #endif
3155
3156         outb(iobase, SVRR);
3157         svrtype = inb(iobase + EREG_DATA);
3158         if (panelp->nrports > 4) {
3159                 outb(iobase, (SVRR + 0x80));
3160                 svrtype |= inb(iobase + EREG_DATA);
3161         }
3162 #if STLDEBUG
3163 printf("stl_cd1400eiointr(panelp=%x,iobase=%x): svrr=%x\n", (int) panelp, iobase, svrtype);
3164 #endif
3165
3166         if (svrtype & SVRR_RX)
3167                 stl_cd1400rxisr(panelp, iobase);
3168         else if (svrtype & SVRR_TX)
3169                 stl_cd1400txisr(panelp, iobase);
3170         else if (svrtype & SVRR_MDM)
3171                 stl_cd1400mdmisr(panelp, iobase);
3172 }
3173
3174 /*****************************************************************************/
3175
3176 /*
3177  *      Interrupt service routine for cd1400 panels.
3178  */
3179
3180 static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
3181 {
3182         unsigned char   svrtype;
3183
3184 #if STLDEBUG
3185         printf("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
3186                 iobase);
3187 #endif
3188
3189         outb(iobase, SVRR);
3190         svrtype = inb(iobase + EREG_DATA);
3191         outb(iobase, (SVRR + 0x80));
3192         svrtype |= inb(iobase + EREG_DATA);
3193         if (svrtype & SVRR_RX)
3194                 stl_cd1400rxisr(panelp, iobase);
3195         else if (svrtype & SVRR_TX)
3196                 stl_cd1400txisr(panelp, iobase);
3197         else if (svrtype & SVRR_MDM)
3198                 stl_cd1400mdmisr(panelp, iobase);
3199 }
3200
3201 /*****************************************************************************/
3202
3203 /*
3204  *      Set up the cd1400 registers for a port based on the termios port
3205  *      settings.
3206  */
3207
3208 static int stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
3209 {
3210         unsigned int    clkdiv;
3211         unsigned char   cor1, cor2, cor3;
3212         unsigned char   cor4, cor5, ccr;
3213         unsigned char   srer, sreron, sreroff;
3214         unsigned char   mcor1, mcor2, rtpr;
3215         unsigned char   clk, div;
3216         int             x;
3217
3218 #if STLDEBUG
3219         printf("stl_cd1400setport(portp=%x,tiosp=%x): brdnr=%d portnr=%d\n",
3220                 (int) portp, (int) tiosp, portp->brdnr, portp->portnr);
3221 #endif
3222
3223         cor1 = 0;
3224         cor2 = 0;
3225         cor3 = 0;
3226         cor4 = 0;
3227         cor5 = 0;
3228         ccr = 0;
3229         rtpr = 0;
3230         clk = 0;
3231         div = 0;
3232         mcor1 = 0;
3233         mcor2 = 0;
3234         sreron = 0;
3235         sreroff = 0;
3236
3237 /*
3238  *      Set up the RX char ignore mask with those RX error types we
3239  *      can ignore. We could have used some special modes of the cd1400
3240  *      UART to help, but it is better this way because we can keep stats
3241  *      on the number of each type of RX exception event.
3242  */
3243         portp->rxignoremsk = 0;
3244         if (tiosp->c_iflag & IGNPAR)
3245                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
3246         if (tiosp->c_iflag & IGNBRK)
3247                 portp->rxignoremsk |= ST_BREAK;
3248
3249         portp->rxmarkmsk = ST_OVERRUN;
3250         if (tiosp->c_iflag & (INPCK | PARMRK))
3251                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
3252         if (tiosp->c_iflag & BRKINT)
3253                 portp->rxmarkmsk |= ST_BREAK;
3254
3255 /*
3256  *      Go through the char size, parity and stop bits and set all the
3257  *      option registers appropriately.
3258  */
3259         switch (tiosp->c_cflag & CSIZE) {
3260         case CS5:
3261                 cor1 |= COR1_CHL5;
3262                 break;
3263         case CS6:
3264                 cor1 |= COR1_CHL6;
3265                 break;
3266         case CS7:
3267                 cor1 |= COR1_CHL7;
3268                 break;
3269         default:
3270                 cor1 |= COR1_CHL8;
3271                 break;
3272         }
3273
3274         if (tiosp->c_cflag & CSTOPB)
3275                 cor1 |= COR1_STOP2;
3276         else
3277                 cor1 |= COR1_STOP1;
3278
3279         if (tiosp->c_cflag & PARENB) {
3280                 if (tiosp->c_cflag & PARODD)
3281                         cor1 |= (COR1_PARENB | COR1_PARODD);
3282                 else
3283                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
3284         } else {
3285                 cor1 |= COR1_PARNONE;
3286         }
3287
3288 /*
3289  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
3290  *      space for hardware flow control and the like. This should be set to
3291  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
3292  *      really be based on VTIME...
3293  */
3294         cor3 |= FIFO_RXTHRESHOLD;
3295         rtpr = 2;
3296
3297 /*
3298  *      Calculate the baud rate timers. For now we will just assume that
3299  *      the input and output baud are the same. Could have used a baud
3300  *      table here, but this way we can generate virtually any baud rate
3301  *      we like!
3302  */
3303         if (tiosp->c_ispeed == 0)
3304                 tiosp->c_ispeed = tiosp->c_ospeed;
3305         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > CD1400_MAXBAUD))
3306                 return(EINVAL);
3307
3308         if (tiosp->c_ospeed > 0) {
3309                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
3310                         clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) /
3311                                 tiosp->c_ospeed);
3312                         if (clkdiv < 0x100)
3313                                 break;
3314                 }
3315                 div = (unsigned char) clkdiv;
3316         }
3317
3318 /*
3319  *      Check what form of modem signaling is required and set it up.
3320  */
3321         if ((tiosp->c_cflag & CLOCAL) == 0) {
3322                 mcor1 |= MCOR1_DCD;
3323                 mcor2 |= MCOR2_DCD;
3324                 sreron |= SRER_MODEM;
3325         }
3326
3327 /*
3328  *      Setup cd1400 enhanced modes if we can. In particular we want to
3329  *      handle as much of the flow control as possbile automatically. As
3330  *      well as saving a few CPU cycles it will also greatly improve flow
3331  *      control reliablilty.
3332  */
3333         if (tiosp->c_iflag & IXON) {
3334                 cor2 |= COR2_TXIBE;
3335                 cor3 |= COR3_SCD12;
3336                 if (tiosp->c_iflag & IXANY)
3337                         cor2 |= COR2_IXM;
3338         }
3339
3340         if (tiosp->c_cflag & CCTS_OFLOW)
3341                 cor2 |= COR2_CTSAE;
3342         if (tiosp->c_cflag & CRTS_IFLOW)
3343                 mcor1 |= FIFO_RTSTHRESHOLD;
3344
3345 /*
3346  *      All cd1400 register values calculated so go through and set them
3347  *      all up.
3348  */
3349 #if STLDEBUG
3350         printf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
3351                 portp->panelnr, portp->brdnr);
3352         printf("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n", cor1, cor2,
3353                 cor3, cor4, cor5);
3354         printf("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
3355                 mcor1, mcor2, rtpr, sreron, sreroff);
3356         printf("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
3357         printf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
3358                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP], tiosp->c_cc[VSTART],
3359                 tiosp->c_cc[VSTOP]);
3360 #endif
3361
3362         x = spltty();
3363         BRDENABLE(portp->brdnr, portp->pagenr);
3364         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3365         srer = stl_cd1400getreg(portp, SRER);
3366         stl_cd1400setreg(portp, SRER, 0);
3367         ccr += stl_cd1400updatereg(portp, COR1, cor1);
3368         ccr += stl_cd1400updatereg(portp, COR2, cor2);
3369         ccr += stl_cd1400updatereg(portp, COR3, cor3);
3370         if (ccr) {
3371                 stl_cd1400ccrwait(portp);
3372                 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3373         }
3374         stl_cd1400setreg(portp, COR4, cor4);
3375         stl_cd1400setreg(portp, COR5, cor5);
3376         stl_cd1400setreg(portp, MCOR1, mcor1);
3377         stl_cd1400setreg(portp, MCOR2, mcor2);
3378         if (tiosp->c_ospeed == 0) {
3379                 stl_cd1400setreg(portp, MSVR1, 0);
3380         } else {
3381                 stl_cd1400setreg(portp, MSVR1, MSVR1_DTR);
3382                 stl_cd1400setreg(portp, TCOR, clk);
3383                 stl_cd1400setreg(portp, TBPR, div);
3384                 stl_cd1400setreg(portp, RCOR, clk);
3385                 stl_cd1400setreg(portp, RBPR, div);
3386         }
3387         stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3388         stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3389         stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3390         stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3391         stl_cd1400setreg(portp, RTPR, rtpr);
3392         mcor1 = stl_cd1400getreg(portp, MSVR1);
3393         if (mcor1 & MSVR1_DCD)
3394                 portp->sigs |= TIOCM_CD;
3395         else
3396                 portp->sigs &= ~TIOCM_CD;
3397         stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3398         BRDDISABLE(portp->brdnr);
3399         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
3400         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
3401         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
3402         stl_ttyoptim(portp, tiosp);
3403         splx(x);
3404
3405         return(0);
3406 }
3407
3408 /*****************************************************************************/
3409
3410 /*
3411  *      Action the flow control as required. The hw and sw args inform the
3412  *      routine what flow control methods it should try.
3413  */
3414
3415 static void stl_cd1400sendflow(stlport_t *portp, int hw, int sw)
3416 {
3417         int     x;
3418
3419 #if STLDEBUG
3420         printf("stl_cd1400sendflow(portp=%x,hw=%d,sw=%d)\n",
3421                 (int) portp, hw, sw);
3422 #endif
3423
3424         x = spltty();
3425         BRDENABLE(portp->brdnr, portp->pagenr);
3426         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3427
3428         if (sw >= 0) {
3429                 stl_cd1400ccrwait(portp);
3430                 if (sw) {
3431                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3432                         portp->stats.rxxoff++;
3433                 } else {
3434                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3435                         portp->stats.rxxon++;
3436                 }
3437                 stl_cd1400ccrwait(portp);
3438         }
3439
3440         if (hw == 0) {
3441                 portp->state |= ASY_RTSFLOW;
3442                 stl_cd1400setreg(portp, MCOR1,
3443                         (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3444                 stl_cd1400setreg(portp, MSVR2, 0);
3445                 portp->stats.rxrtsoff++;
3446         } else if (hw > 0) {
3447                 portp->state &= ~ASY_RTSFLOW;
3448                 stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3449                 stl_cd1400setreg(portp, MCOR1,
3450                         (stl_cd1400getreg(portp, MCOR1) | FIFO_RTSTHRESHOLD));
3451                 portp->stats.rxrtson++;
3452         }
3453
3454         BRDDISABLE(portp->brdnr);
3455         splx(x);
3456 }
3457
3458 /*****************************************************************************/
3459
3460 /*
3461  *      Return the current state of data flow on this port. This is only
3462  *      really interresting when determining if data has fully completed
3463  *      transmission or not... This is easy for the cd1400, it accurately
3464  *      maintains the busy port flag.
3465  */
3466
3467 static int stl_cd1400datastate(stlport_t *portp)
3468 {
3469 #if STLDEBUG
3470         printf("stl_cd1400datastate(portp=%x)\n", (int) portp);
3471 #endif
3472
3473         if (portp == (stlport_t *) NULL)
3474                 return(0);
3475
3476         return((portp->state & ASY_TXBUSY) ? 1 : 0);
3477 }
3478
3479 /*****************************************************************************/
3480
3481 /*
3482  *      Set the state of the DTR and RTS signals. Got to do some extra
3483  *      work here to deal hardware flow control.
3484  */
3485
3486 static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
3487 {
3488         unsigned char   msvr1, msvr2;
3489         int             x;
3490
3491 #if STLDEBUG
3492         printf("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n", (int) portp,
3493                 dtr, rts);
3494 #endif
3495
3496         msvr1 = 0;
3497         msvr2 = 0;
3498         if (dtr > 0)
3499                 msvr1 = MSVR1_DTR;
3500         if (rts > 0)
3501                 msvr2 = MSVR2_RTS;
3502
3503         x = spltty();
3504         BRDENABLE(portp->brdnr, portp->pagenr);
3505         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3506         if (rts >= 0) {
3507                 if (portp->tty.t_cflag & CRTS_IFLOW) {
3508                         if (rts == 0) {
3509                                 stl_cd1400setreg(portp, MCOR1,
3510                                       (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3511                                 portp->stats.rxrtsoff++;
3512                         } else {
3513                                 stl_cd1400setreg(portp, MCOR1,
3514                                         (stl_cd1400getreg(portp, MCOR1) |
3515                                         FIFO_RTSTHRESHOLD));
3516                                 portp->stats.rxrtson++;
3517                         }
3518                 }
3519                 stl_cd1400setreg(portp, MSVR2, msvr2);
3520         }
3521         if (dtr >= 0)
3522                 stl_cd1400setreg(portp, MSVR1, msvr1);
3523         BRDDISABLE(portp->brdnr);
3524         splx(x);
3525 }
3526
3527 /*****************************************************************************/
3528
3529 /*
3530  *      Get the state of the signals.
3531  */
3532
3533 static int stl_cd1400getsignals(stlport_t *portp)
3534 {
3535         unsigned char   msvr1, msvr2;
3536         int             sigs, x;
3537
3538 #if STLDEBUG
3539         printf("stl_cd1400getsignals(portp=%x)\n", (int) portp);
3540 #endif
3541
3542         x = spltty();
3543         BRDENABLE(portp->brdnr, portp->pagenr);
3544         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3545         msvr1 = stl_cd1400getreg(portp, MSVR1);
3546         msvr2 = stl_cd1400getreg(portp, MSVR2);
3547         BRDDISABLE(portp->brdnr);
3548         splx(x);
3549
3550         sigs = 0;
3551         sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3552         sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3553         sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3554         sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3555 #if 0
3556         sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3557         sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3558 #else
3559         sigs |= TIOCM_DSR;
3560 #endif
3561         return(sigs);
3562 }
3563
3564 /*****************************************************************************/
3565
3566 /*
3567  *      Enable or disable the Transmitter and/or Receiver.
3568  */
3569
3570 static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
3571 {
3572         unsigned char   ccr;
3573         int             x;
3574
3575 #if STLDEBUG
3576         printf("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3577                 (int) portp, rx, tx);
3578 #endif
3579
3580         ccr = 0;
3581         if (tx == 0)
3582                 ccr |= CCR_TXDISABLE;
3583         else if (tx > 0)
3584                 ccr |= CCR_TXENABLE;
3585         if (rx == 0)
3586                 ccr |= CCR_RXDISABLE;
3587         else if (rx > 0)
3588                 ccr |= CCR_RXENABLE;
3589
3590         x = spltty();
3591         BRDENABLE(portp->brdnr, portp->pagenr);
3592         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3593         stl_cd1400ccrwait(portp);
3594         stl_cd1400setreg(portp, CCR, ccr);
3595         stl_cd1400ccrwait(portp);
3596         BRDDISABLE(portp->brdnr);
3597         splx(x);
3598 }
3599
3600 /*****************************************************************************/
3601
3602 /*
3603  *      Start or stop the Transmitter and/or Receiver.
3604  */
3605
3606 static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
3607 {
3608         unsigned char   sreron, sreroff;
3609         int             x;
3610
3611 #if STLDEBUG
3612         printf("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
3613                 (int) portp, rx, tx);
3614 #endif
3615
3616         sreron = 0;
3617         sreroff = 0;
3618         if (tx == 0)
3619                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3620         else if (tx == 1)
3621                 sreron |= SRER_TXDATA;
3622         else if (tx >= 2)
3623                 sreron |= SRER_TXEMPTY;
3624         if (rx == 0)
3625                 sreroff |= SRER_RXDATA;
3626         else if (rx > 0)
3627                 sreron |= SRER_RXDATA;
3628
3629         x = spltty();
3630         BRDENABLE(portp->brdnr, portp->pagenr);
3631         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3632         stl_cd1400setreg(portp, SRER,
3633                 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3634         BRDDISABLE(portp->brdnr);
3635         if (tx > 0) {
3636                 portp->state |= ASY_TXBUSY;
3637                 portp->tty.t_state |= TS_BUSY;
3638         }
3639         splx(x);
3640 }
3641
3642 /*****************************************************************************/
3643
3644 /*
3645  *      Disable all interrupts from this port.
3646  */
3647
3648 static void stl_cd1400disableintrs(stlport_t *portp)
3649 {
3650         int     x;
3651
3652 #if STLDEBUG
3653         printf("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
3654 #endif
3655
3656         x = spltty();
3657         BRDENABLE(portp->brdnr, portp->pagenr);
3658         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3659         stl_cd1400setreg(portp, SRER, 0);
3660         BRDDISABLE(portp->brdnr);
3661         splx(x);
3662 }
3663
3664 /*****************************************************************************/
3665
3666 static void stl_cd1400sendbreak(stlport_t *portp, long len)
3667 {
3668         int     x;
3669
3670 #if STLDEBUG
3671         printf("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp,
3672                 (int) len);
3673 #endif
3674
3675         x = spltty();
3676         BRDENABLE(portp->brdnr, portp->pagenr);
3677         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3678         stl_cd1400setreg(portp, COR2,
3679                 (stl_cd1400getreg(portp, COR2) | COR2_ETC));
3680         stl_cd1400setreg(portp, SRER,
3681                 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3682                 SRER_TXEMPTY));
3683         BRDDISABLE(portp->brdnr);
3684         if (len > 0) {
3685                 len = len / 5;
3686                 portp->brklen = (len > 255) ? 255 : len;
3687         } else {
3688                 portp->brklen = len;
3689         }
3690         splx(x);
3691         portp->stats.txbreaks++;
3692 }
3693
3694 /*****************************************************************************/
3695
3696 /*
3697  *      Try and find and initialize all the ports on a panel. We don't care
3698  *      what sort of board these ports are on - since the port io registers
3699  *      are almost identical when dealing with ports.
3700  */
3701
3702 static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3703 {
3704 #if STLDEBUG
3705         printf("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
3706                 (int) brdp, (int) panelp, (int) portp);
3707 #endif
3708
3709         if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
3710             (portp == (stlport_t *) NULL))
3711                 return;
3712
3713         portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
3714                 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
3715         portp->uartaddr = (portp->portnr & 0x04) << 5;
3716         portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
3717
3718         BRDENABLE(portp->brdnr, portp->pagenr);
3719         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3720         stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
3721         portp->hwid = stl_cd1400getreg(portp, GFRCR);
3722         BRDDISABLE(portp->brdnr);
3723 }
3724
3725 /*****************************************************************************/
3726
3727 /*
3728  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3729  *      these ports are on - since the port io registers are almost
3730  *      identical when dealing with ports.
3731  */
3732
3733 static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3734 {
3735         unsigned int    gfrcr;
3736         int             chipmask, i, j;
3737         int             nrchips, uartaddr, ioaddr;
3738
3739 #if STLDEBUG
3740         printf("stl_cd1400panelinit(brdp=%x,panelp=%x)\n", (int) brdp,
3741                 (int) panelp);
3742 #endif
3743
3744         BRDENABLE(panelp->brdnr, panelp->pagenr);
3745
3746 /*
3747  *      Check that each chip is present and started up OK.
3748  */
3749         chipmask = 0;
3750         nrchips = panelp->nrports / CD1400_PORTS;
3751         for (i = 0; (i < nrchips); i++) {
3752                 if (brdp->brdtype == BRD_ECHPCI) {
3753                         outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
3754                         ioaddr = panelp->iobase;
3755                 } else {
3756                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
3757                 }
3758                 uartaddr = (i & 0x01) ? 0x080 : 0;
3759                 outb(ioaddr, (GFRCR + uartaddr));
3760                 outb((ioaddr + EREG_DATA), 0);
3761                 outb(ioaddr, (CCR + uartaddr));
3762                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
3763                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
3764                 outb(ioaddr, (GFRCR + uartaddr));
3765                 for (j = 0; (j < CCR_MAXWAIT); j++) {
3766                         if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
3767                                 break;
3768                 }
3769                 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
3770                         printf("STALLION: cd1400 not responding, "
3771                                 "board=%d panel=%d chip=%d\n", panelp->brdnr,
3772                                 panelp->panelnr, i);
3773                         continue;
3774                 }
3775                 chipmask |= (0x1 << i);
3776                 outb(ioaddr, (PPR + uartaddr));
3777                 outb((ioaddr + EREG_DATA), PPR_SCALAR);
3778         }
3779
3780
3781         BRDDISABLE(panelp->brdnr);
3782         return(chipmask);
3783 }
3784
3785 /*****************************************************************************/
3786 /*                      SC26198 HARDWARE FUNCTIONS                           */
3787 /*****************************************************************************/
3788
3789 /*
3790  *      These functions get/set/update the registers of the sc26198 UARTs.
3791  *      Access to the sc26198 registers is via an address/data io port pair.
3792  *      (Maybe should make this inline...)
3793  */
3794
3795 static int stl_sc26198getreg(stlport_t *portp, int regnr)
3796 {
3797         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3798         return(inb(portp->ioaddr + XP_DATA));
3799 }
3800
3801 static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
3802 {
3803         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3804         outb((portp->ioaddr + XP_DATA), value);
3805 }
3806
3807 static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
3808 {
3809         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3810         if (inb(portp->ioaddr + XP_DATA) != value) {
3811                 outb((portp->ioaddr + XP_DATA), value);
3812                 return(1);
3813         }
3814         return(0);
3815 }
3816
3817 /*****************************************************************************/
3818
3819 /*
3820  *      Functions to get and set the sc26198 global registers.
3821  */
3822
3823 static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
3824 {
3825         outb((portp->ioaddr + XP_ADDR), regnr);
3826         return(inb(portp->ioaddr + XP_DATA));
3827 }
3828
3829 #if 0
3830 static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
3831 {
3832         outb((portp->ioaddr + XP_ADDR), regnr);
3833         outb((portp->ioaddr + XP_DATA), value);
3834 }
3835 #endif
3836
3837 /*****************************************************************************/
3838
3839 /*
3840  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3841  *      these ports are on - since the port io registers are almost
3842  *      identical when dealing with ports.
3843  */
3844
3845 static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3846 {
3847         int     chipmask, i;
3848         int     nrchips, ioaddr;
3849
3850 #if STLDEBUG
3851         printf("stl_sc26198panelinit(brdp=%x,panelp=%x)\n", (int) brdp,
3852                 (int) panelp);
3853 #endif
3854
3855         BRDENABLE(panelp->brdnr, panelp->pagenr);
3856
3857 /*
3858  *      Check that each chip is present and started up OK.
3859  */
3860         chipmask = 0;
3861         nrchips = (panelp->nrports + 4) / SC26198_PORTS;
3862         if (brdp->brdtype == BRD_ECHPCI)
3863                 outb(brdp->ioctrl, panelp->pagenr);
3864
3865         for (i = 0; (i < nrchips); i++) {
3866                 ioaddr = panelp->iobase + (i * 4); 
3867                 outb((ioaddr + XP_ADDR), SCCR);
3868                 outb((ioaddr + XP_DATA), CR_RESETALL);
3869                 outb((ioaddr + XP_ADDR), TSTR);
3870                 if (inb(ioaddr + XP_DATA) != 0) {
3871                         printf("STALLION: sc26198 not responding, "
3872                                 "board=%d panel=%d chip=%d\n", panelp->brdnr,
3873                                 panelp->panelnr, i);
3874                         continue;
3875                 }
3876                 chipmask |= (0x1 << i);
3877                 outb((ioaddr + XP_ADDR), GCCR);
3878                 outb((ioaddr + XP_DATA), GCCR_IVRTYPCHANACK);
3879                 outb((ioaddr + XP_ADDR), WDTRCR);
3880                 outb((ioaddr + XP_DATA), 0xff);
3881         }
3882
3883         BRDDISABLE(panelp->brdnr);
3884         return(chipmask);
3885 }
3886
3887 /*****************************************************************************/
3888
3889 /*
3890  *      Initialize hardware specific port registers.
3891  */
3892
3893 static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3894 {
3895 #if STLDEBUG
3896         printf("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
3897                 (int) brdp, (int) panelp, (int) portp);
3898 #endif
3899
3900         if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
3901             (portp == (stlport_t *) NULL))
3902                 return;
3903
3904         portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
3905         portp->uartaddr = (portp->portnr & 0x07) << 4;
3906         portp->pagenr = panelp->pagenr;
3907         portp->hwid = 0x1;
3908
3909         BRDENABLE(portp->brdnr, portp->pagenr);
3910         stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
3911         BRDDISABLE(portp->brdnr);
3912 }
3913
3914 /*****************************************************************************/
3915
3916 /*
3917  *      Set up the sc26198 registers for a port based on the termios port
3918  *      settings.
3919  */
3920
3921 static int stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
3922 {
3923         unsigned char   mr0, mr1, mr2, clk;
3924         unsigned char   imron, imroff, iopr, ipr;
3925         int             x;
3926
3927 #if STLDEBUG
3928         printf("stl_sc26198setport(portp=%x,tiosp=%x): brdnr=%d portnr=%d\n",
3929                 (int) portp, (int) tiosp, portp->brdnr, portp->portnr);
3930 #endif
3931
3932         mr0 = 0;
3933         mr1 = 0;
3934         mr2 = 0;
3935         clk = 0;
3936         iopr = 0;
3937         imron = 0;
3938         imroff = 0;
3939
3940 /*
3941  *      Set up the RX char ignore mask with those RX error types we
3942  *      can ignore.
3943  */
3944         portp->rxignoremsk = 0;
3945         if (tiosp->c_iflag & IGNPAR)
3946                 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
3947                         SR_RXOVERRUN);
3948         if (tiosp->c_iflag & IGNBRK)
3949                 portp->rxignoremsk |= SR_RXBREAK;
3950
3951         portp->rxmarkmsk = SR_RXOVERRUN;
3952         if (tiosp->c_iflag & (INPCK | PARMRK))
3953                 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
3954         if (tiosp->c_iflag & BRKINT)
3955                 portp->rxmarkmsk |= SR_RXBREAK;
3956
3957 /*
3958  *      Go through the char size, parity and stop bits and set all the
3959  *      option registers appropriately.
3960  */
3961         switch (tiosp->c_cflag & CSIZE) {
3962         case CS5:
3963                 mr1 |= MR1_CS5;
3964                 break;
3965         case CS6:
3966                 mr1 |= MR1_CS6;
3967                 break;
3968         case CS7:
3969                 mr1 |= MR1_CS7;
3970                 break;
3971         default:
3972                 mr1 |= MR1_CS8;
3973                 break;
3974         }
3975
3976         if (tiosp->c_cflag & CSTOPB)
3977                 mr2 |= MR2_STOP2;
3978         else
3979                 mr2 |= MR2_STOP1;
3980
3981         if (tiosp->c_cflag & PARENB) {
3982                 if (tiosp->c_cflag & PARODD)
3983                         mr1 |= (MR1_PARENB | MR1_PARODD);
3984                 else
3985                         mr1 |= (MR1_PARENB | MR1_PAREVEN);
3986         } else {
3987                 mr1 |= MR1_PARNONE;
3988         }
3989
3990         mr1 |= MR1_ERRBLOCK;
3991
3992 /*
3993  *      Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
3994  *      space for hardware flow control and the like. This should be set to
3995  *      VMIN.
3996  */
3997         mr2 |= MR2_RXFIFOHALF;
3998
3999 /*
4000  *      Calculate the baud rate timers. For now we will just assume that
4001  *      the input and output baud are the same. The sc26198 has a fixed
4002  *      baud rate table, so only discrete baud rates possible.
4003  */
4004         if (tiosp->c_ispeed == 0)
4005                 tiosp->c_ispeed = tiosp->c_ospeed;
4006         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > SC26198_MAXBAUD))
4007                 return(EINVAL);
4008
4009         if (tiosp->c_ospeed > 0) {
4010                 for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
4011                         if (tiosp->c_ospeed <= sc26198_baudtable[clk])
4012                                 break;
4013                 }
4014         }
4015
4016 /*
4017  *      Check what form of modem signaling is required and set it up.
4018  */
4019         if ((tiosp->c_cflag & CLOCAL) == 0) {
4020                 iopr |= IOPR_DCDCOS;
4021                 imron |= IR_IOPORT;
4022         }
4023
4024 /*
4025  *      Setup sc26198 enhanced modes if we can. In particular we want to
4026  *      handle as much of the flow control as possible automatically. As
4027  *      well as saving a few CPU cycles it will also greatly improve flow
4028  *      control reliability.
4029  */
4030         if (tiosp->c_iflag & IXON) {
4031                 mr0 |= MR0_SWFTX | MR0_SWFT;
4032                 imron |= IR_XONXOFF;
4033         } else {
4034                 imroff |= IR_XONXOFF;
4035         }
4036 #if 0
4037         if (tiosp->c_iflag & IXOFF)
4038                 mr0 |= MR0_SWFRX;
4039 #endif
4040
4041         if (tiosp->c_cflag & CCTS_OFLOW)
4042                 mr2 |= MR2_AUTOCTS;
4043         if (tiosp->c_cflag & CRTS_IFLOW)
4044                 mr1 |= MR1_AUTORTS;
4045
4046 /*
4047  *      All sc26198 register values calculated so go through and set
4048  *      them all up.
4049  */
4050
4051 #if STLDEBUG
4052         printf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
4053                 portp->panelnr, portp->brdnr);
4054         printf("    mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
4055         printf("    iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
4056         printf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
4057                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
4058                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
4059 #endif
4060
4061         x = spltty();
4062         BRDENABLE(portp->brdnr, portp->pagenr);
4063         stl_sc26198setreg(portp, IMR, 0);
4064         stl_sc26198updatereg(portp, MR0, mr0);
4065         stl_sc26198updatereg(portp, MR1, mr1);
4066         stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
4067         stl_sc26198updatereg(portp, MR2, mr2);
4068         iopr = (stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr;
4069         if (tiosp->c_ospeed == 0) {
4070                 iopr &= ~IPR_DTR;
4071         } else {
4072                 iopr |= IPR_DTR;
4073                 stl_sc26198setreg(portp, TXCSR, clk);
4074                 stl_sc26198setreg(portp, RXCSR, clk);
4075         }
4076         stl_sc26198updatereg(portp, IOPIOR, iopr);
4077         stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
4078         stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
4079         ipr = stl_sc26198getreg(portp, IPR);
4080         if (ipr & IPR_DCD)
4081                 portp->sigs &= ~TIOCM_CD;
4082         else
4083                 portp->sigs |= TIOCM_CD;
4084         portp->imr = (portp->imr & ~imroff) | imron;
4085         stl_sc26198setreg(portp, IMR, portp->imr);
4086         BRDDISABLE(portp->brdnr);
4087         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
4088         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
4089         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
4090         stl_ttyoptim(portp, tiosp);
4091         splx(x);
4092
4093         return(0);
4094 }
4095
4096 /*****************************************************************************/
4097
4098 /*
4099  *      Set the state of the DTR and RTS signals.
4100  */
4101
4102 static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
4103 {
4104         unsigned char   iopioron, iopioroff;
4105         int             x;
4106
4107 #if STLDEBUG
4108         printf("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
4109                 (int) portp, dtr, rts);
4110 #endif
4111
4112         iopioron = 0;
4113         iopioroff = 0;
4114         if (dtr == 0)
4115                 iopioroff |= IPR_DTR;
4116         else if (dtr > 0)
4117                 iopioron |= IPR_DTR;
4118         if (rts == 0)
4119                 iopioroff |= IPR_RTS;
4120         else if (rts > 0)
4121                 iopioron |= IPR_RTS;
4122
4123         x = spltty();
4124         BRDENABLE(portp->brdnr, portp->pagenr);
4125         if ((rts >= 0) && (portp->tty.t_cflag & CRTS_IFLOW)) {
4126                 if (rts == 0) {
4127                         stl_sc26198setreg(portp, MR1,
4128                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4129                         portp->stats.rxrtsoff++;
4130                 } else {
4131                         stl_sc26198setreg(portp, MR1,
4132                                 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4133                         portp->stats.rxrtson++;
4134                 }
4135         }
4136         stl_sc26198setreg(portp, IOPIOR,
4137                 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
4138         BRDDISABLE(portp->brdnr);
4139         splx(x);
4140 }
4141
4142 /*****************************************************************************/
4143
4144 /*
4145  *      Return the state of the signals.
4146  */
4147
4148 static int stl_sc26198getsignals(stlport_t *portp)
4149 {
4150         unsigned char   ipr;
4151         int             x, sigs;
4152
4153 #if STLDEBUG
4154         printf("stl_sc26198getsignals(portp=%x)\n", (int) portp);
4155 #endif
4156
4157         x = spltty();
4158         BRDENABLE(portp->brdnr, portp->pagenr);
4159         ipr = stl_sc26198getreg(portp, IPR);
4160         BRDDISABLE(portp->brdnr);
4161         splx(x);
4162
4163         sigs = TIOCM_DSR;
4164         sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
4165         sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
4166         sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
4167         sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
4168         return(sigs);
4169 }
4170
4171 /*****************************************************************************/
4172
4173 /*
4174  *      Enable/Disable the Transmitter and/or Receiver.
4175  */
4176
4177 static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
4178 {
4179         unsigned char   ccr;
4180         int             x;
4181
4182 #if STLDEBUG
4183         printf("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
4184                 (int) portp, rx, tx);
4185 #endif
4186
4187         ccr = portp->crenable;
4188         if (tx == 0)
4189                 ccr &= ~CR_TXENABLE;
4190         else if (tx > 0)
4191                 ccr |= CR_TXENABLE;
4192         if (rx == 0)
4193                 ccr &= ~CR_RXENABLE;
4194         else if (rx > 0)
4195                 ccr |= CR_RXENABLE;
4196
4197         x = spltty();
4198         BRDENABLE(portp->brdnr, portp->pagenr);
4199         stl_sc26198setreg(portp, SCCR, ccr);
4200         BRDDISABLE(portp->brdnr);
4201         portp->crenable = ccr;
4202         splx(x);
4203 }
4204
4205 /*****************************************************************************/
4206
4207 /*
4208  *      Start/stop the Transmitter and/or Receiver.
4209  */
4210
4211 static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
4212 {
4213         unsigned char   imr;
4214         int             x;
4215
4216 #if STLDEBUG
4217         printf("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
4218                 (int) portp, rx, tx);
4219 #endif
4220
4221         imr = portp->imr;
4222         if (tx == 0)
4223                 imr &= ~IR_TXRDY;
4224         else if (tx == 1)
4225                 imr |= IR_TXRDY;
4226         if (rx == 0)
4227                 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
4228         else if (rx > 0)
4229                 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
4230
4231         x = spltty();
4232         BRDENABLE(portp->brdnr, portp->pagenr);
4233         stl_sc26198setreg(portp, IMR, imr);
4234         BRDDISABLE(portp->brdnr);
4235         portp->imr = imr;
4236         if (tx > 0) {
4237                 portp->state |= ASY_TXBUSY;
4238                 portp->tty.t_state |= TS_BUSY;
4239         }
4240         splx(x);
4241 }
4242
4243 /*****************************************************************************/
4244
4245 /*
4246  *      Disable all interrupts from this port.
4247  */
4248
4249 static void stl_sc26198disableintrs(stlport_t *portp)
4250 {
4251         int     x;
4252
4253 #if STLDEBUG
4254         printf("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
4255 #endif
4256
4257         x = spltty();
4258         BRDENABLE(portp->brdnr, portp->pagenr);
4259         portp->imr = 0;
4260         stl_sc26198setreg(portp, IMR, 0);
4261         BRDDISABLE(portp->brdnr);
4262         splx(x);
4263 }
4264
4265 /*****************************************************************************/
4266
4267 static void stl_sc26198sendbreak(stlport_t *portp, long len)
4268 {
4269         int     x;
4270
4271 #if STLDEBUG
4272         printf("stl_sc26198sendbreak(portp=%x,len=%d)\n",
4273                 (int) portp, (int) len);
4274 #endif
4275
4276         x = spltty();
4277         BRDENABLE(portp->brdnr, portp->pagenr);
4278         if (len == -1) {
4279                 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4280                 portp->stats.txbreaks++;
4281         } else {
4282                 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4283         }
4284         BRDDISABLE(portp->brdnr);
4285         splx(x);
4286 }
4287
4288 /*****************************************************************************/
4289
4290 /*
4291  *      Take flow control actions...
4292  */
4293
4294 static void stl_sc26198sendflow(stlport_t *portp, int hw, int sw)
4295 {
4296         unsigned char   mr0;
4297         int             x;
4298
4299 #if STLDEBUG
4300         printf("stl_sc26198sendflow(portp=%x,hw=%d,sw=%d)\n",
4301                 (int) portp, hw, sw);
4302 #endif
4303
4304         if (portp == (stlport_t *) NULL)
4305                 return;
4306
4307         x = spltty();
4308         BRDENABLE(portp->brdnr, portp->pagenr);
4309
4310         if (sw >= 0) {
4311                 mr0 = stl_sc26198getreg(portp, MR0);
4312                 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4313                 if (sw > 0) {
4314                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4315                         mr0 &= ~MR0_SWFRX;
4316                         portp->stats.rxxoff++;
4317                 } else {
4318                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4319                         mr0 |= MR0_SWFRX;
4320                         portp->stats.rxxon++;
4321                 }
4322                 stl_sc26198wait(portp);
4323                 stl_sc26198setreg(portp, MR0, mr0);
4324         }
4325
4326         if (hw == 0) {
4327                 portp->state |= ASY_RTSFLOW;
4328                 stl_sc26198setreg(portp, MR1,
4329                         (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4330                 stl_sc26198setreg(portp, IOPIOR,
4331                         (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4332                 portp->stats.rxrtsoff++;
4333         } else if (hw > 0) {
4334                 portp->state &= ~ASY_RTSFLOW;
4335                 stl_sc26198setreg(portp, MR1,
4336                         (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4337                 stl_sc26198setreg(portp, IOPIOR,
4338                         (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4339                 portp->stats.rxrtson++;
4340         }
4341
4342         BRDDISABLE(portp->brdnr);
4343         splx(x);
4344 }
4345
4346 /*****************************************************************************/
4347
4348 /*
4349  *      Return the current state of data flow on this port. This is only
4350  *      really interresting when determining if data has fully completed
4351  *      transmission or not... The sc26198 interrupt scheme cannot
4352  *      determine when all data has actually drained, so we need to
4353  *      check the port statusy register to be sure.
4354  */
4355
4356 static int stl_sc26198datastate(stlport_t *portp)
4357 {
4358         unsigned char   sr;
4359         int             x;
4360
4361 #if STLDEBUG
4362         printf("stl_sc26198datastate(portp=%x)\n", (int) portp);
4363 #endif
4364
4365         if (portp == (stlport_t *) NULL)
4366                 return(0);
4367         if (portp->state & ASY_TXBUSY) 
4368                 return(1);
4369
4370         x = spltty();
4371         BRDENABLE(portp->brdnr, portp->pagenr);
4372         sr = stl_sc26198getreg(portp, SR);
4373         BRDDISABLE(portp->brdnr);
4374         splx(x);
4375
4376         return((sr & SR_TXEMPTY) ? 0 : 1);
4377 }
4378
4379 /*****************************************************************************/
4380
4381 static void stl_sc26198flush(stlport_t *portp, int flag)
4382 {
4383         int     x;
4384
4385 #if STLDEBUG
4386         printf("stl_sc26198flush(portp=%x,flag=%x)\n", (int) portp, flag);
4387 #endif
4388
4389         if (portp == (stlport_t *) NULL)
4390                 return;
4391
4392         x = spltty();
4393         BRDENABLE(portp->brdnr, portp->pagenr);
4394         if (flag & FWRITE) {
4395                 stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4396                 stl_sc26198setreg(portp, SCCR, portp->crenable);
4397         }
4398         if (flag & FREAD) {
4399                 while (stl_sc26198getreg(portp, SR) & SR_RXRDY)
4400                         stl_sc26198getreg(portp, RXFIFO);
4401         }
4402         BRDDISABLE(portp->brdnr);
4403         splx(x);
4404 }
4405
4406 /*****************************************************************************/
4407
4408 /*
4409  *      If we are TX flow controlled and in IXANY mode then we may
4410  *      need to unflow control here. We gotta do this because of the
4411  *      automatic flow control modes of the sc26198 - which downs't
4412  *      support any concept of an IXANY mode.
4413  */
4414
4415 static void stl_sc26198txunflow(stlport_t *portp)
4416 {
4417         unsigned char   mr0;
4418
4419         mr0 = stl_sc26198getreg(portp, MR0);
4420         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4421         stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4422         stl_sc26198setreg(portp, MR0, mr0);
4423         portp->state &= ~ASY_TXFLOWED;
4424 }
4425
4426 /*****************************************************************************/
4427
4428 /*
4429  *      Delay for a small amount of time, to give the sc26198 a chance
4430  *      to process a command...
4431  */
4432
4433 static void stl_sc26198wait(stlport_t *portp)
4434 {
4435         int     i;
4436
4437 #if STLDEBUG
4438         printf("stl_sc26198wait(portp=%x)\n", (int) portp);
4439 #endif
4440
4441         if (portp == (stlport_t *) NULL)
4442                 return;
4443
4444         for (i = 0; (i < 20); i++)
4445                 stl_sc26198getglobreg(portp, TSTR);
4446 }
4447
4448 /*****************************************************************************/
4449
4450 /*
4451  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
4452  *      chars is pretty simple, stuff as many as possible from the TX buffer
4453  *      into the sc26198 FIFO.
4454  */
4455
4456 static __inline void stl_sc26198txisr(stlport_t *portp)
4457 {
4458         unsigned int    ioaddr;
4459         unsigned char   mr0;
4460         char            *head, *tail;
4461         int             len, stlen;
4462
4463 #if STLDEBUG
4464         printf("stl_sc26198txisr(portp=%x)\n", (int) portp);
4465 #endif
4466
4467         ioaddr = portp->ioaddr;
4468
4469         head = portp->tx.head;
4470         tail = portp->tx.tail;
4471         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4472         if ((len == 0) || ((len < STL_TXBUFLOW) &&
4473             ((portp->state & ASY_TXLOW) == 0))) {
4474                 portp->state |= ASY_TXLOW;
4475                 stl_dotimeout();
4476         }
4477
4478         if (len == 0) {
4479                 outb((ioaddr + XP_ADDR), (MR0 | portp->uartaddr));
4480                 mr0 = inb(ioaddr + XP_DATA);
4481                 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4482                         portp->imr &= ~IR_TXRDY;
4483                         outb((ioaddr + XP_ADDR), (IMR | portp->uartaddr));
4484                         outb((ioaddr + XP_DATA), portp->imr);
4485                         portp->state |= ASY_TXEMPTY;
4486                         portp->state &= ~ASY_TXBUSY;
4487                 } else {
4488                         mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4489                         outb((ioaddr + XP_DATA), mr0);
4490                 }
4491         } else {
4492                 len = MIN(len, SC26198_TXFIFOSIZE);
4493                 portp->stats.txtotal += len;
4494                 stlen = MIN(len, (portp->tx.endbuf - tail));
4495                 outb((ioaddr + XP_ADDR), GTXFIFO);
4496                 outsb((ioaddr + XP_DATA), tail, stlen);
4497                 len -= stlen;
4498                 tail += stlen;
4499                 if (tail >= portp->tx.endbuf)
4500                         tail = portp->tx.buf;
4501                 if (len > 0) {
4502                         outsb((ioaddr + XP_DATA), tail, len);
4503                         tail += len;
4504                 }
4505                 portp->tx.tail = tail;
4506         }
4507 }
4508
4509 /*****************************************************************************/
4510
4511 /*
4512  *      Receive character interrupt handler. Determine if we have good chars
4513  *      or bad chars and then process appropriately. Good chars are easy
4514  *      just shove the lot into the RX buffer and set all status byte to 0.
4515  *      If a bad RX char then process as required. This routine needs to be
4516  *      fast!
4517  */
4518
4519 static __inline void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
4520 {
4521 #if STLDEBUG
4522         printf("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
4523 #endif
4524
4525         if ((iack & IVR_TYPEMASK) == IVR_RXDATA)
4526                 stl_sc26198rxgoodchars(portp);
4527         else
4528                 stl_sc26198rxbadchars(portp);
4529
4530 /*
4531  *      If we are TX flow controlled and in IXANY mode then we may need
4532  *      to unflow control here. We gotta do this because of the automatic
4533  *      flow control modes of the sc26198.
4534  */
4535         if ((portp->state & ASY_TXFLOWED) && (portp->tty.t_iflag & IXANY))
4536                 stl_sc26198txunflow(portp);
4537 }
4538
4539 /*****************************************************************************/
4540
4541 /*
4542  *      Process the good received characters from RX FIFO.
4543  */
4544
4545 static void stl_sc26198rxgoodchars(stlport_t *portp)
4546 {
4547         unsigned int    ioaddr, len, buflen, stlen;
4548         char            *head, *tail;
4549
4550 #if STLDEBUG
4551         printf("stl_sc26198rxgoodchars(port=%x)\n", (int) portp);
4552 #endif
4553
4554         ioaddr = portp->ioaddr;
4555
4556 /*
4557  *      First up, calculate how much room there is in the RX ring queue.
4558  *      We also want to keep track of the longest possible copy length,
4559  *      this has to allow for the wrapping of the ring queue.
4560  */
4561         head = portp->rx.head;
4562         tail = portp->rx.tail;
4563         if (head >= tail) {
4564                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
4565                 stlen = portp->rx.endbuf - head;
4566         } else {
4567                 buflen = tail - head - 1;
4568                 stlen = buflen;
4569         }
4570
4571 /*
4572  *      Check if the input buffer is near full. If so then we should take
4573  *      some flow control action... It is very easy to do hardware and
4574  *      software flow control from here since we have the port selected on
4575  *      the UART.
4576  */
4577         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
4578                 if (((portp->state & ASY_RTSFLOW) == 0) &&
4579                     (portp->state & ASY_RTSFLOWMODE)) {
4580                         portp->state |= ASY_RTSFLOW;
4581                         stl_sc26198setreg(portp, MR1,
4582                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4583                         stl_sc26198setreg(portp, IOPIOR,
4584                                 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4585                         portp->stats.rxrtsoff++;
4586                 }
4587         }
4588
4589 /*
4590  *      OK we are set, process good data... If the RX ring queue is full
4591  *      just chuck the chars - don't leave them in the UART.
4592  */
4593         outb((ioaddr + XP_ADDR), GIBCR);
4594         len = inb(ioaddr + XP_DATA) + 1;
4595         if (buflen == 0) {
4596                 outb((ioaddr + XP_ADDR), GRXFIFO);
4597                 insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
4598                 portp->stats.rxlost += len;
4599                 portp->stats.rxtotal += len;
4600         } else {
4601                 len = MIN(len, buflen);
4602                 portp->stats.rxtotal += len;
4603                 stlen = MIN(len, stlen);
4604                 if (len > 0) {
4605                         outb((ioaddr + XP_ADDR), GRXFIFO);
4606                         insb((ioaddr + XP_DATA), head, stlen);
4607                         head += stlen;
4608                         if (head >= portp->rx.endbuf) {
4609                                 head = portp->rx.buf;
4610                                 len -= stlen;
4611                                 insb((ioaddr + XP_DATA), head, len);
4612                                 head += len;
4613                         }
4614                 }
4615         }
4616
4617         portp->rx.head = head;
4618         portp->state |= ASY_RXDATA;
4619         stl_dotimeout();
4620 }
4621
4622 /*****************************************************************************/
4623
4624 /*
4625  *      Process all characters in the RX FIFO of the UART. Check all char
4626  *      status bytes as well, and process as required. We need to check
4627  *      all bytes in the FIFO, in case some more enter the FIFO while we
4628  *      are here. To get the exact character error type we need to switch
4629  *      into CHAR error mode (that is why we need to make sure we empty
4630  *      the FIFO).
4631  */
4632
4633 static void stl_sc26198rxbadchars(stlport_t *portp)
4634 {
4635         unsigned char   mr1;
4636         unsigned int    status;
4637         char            *head, *tail;
4638         char            ch;
4639         int             len;
4640
4641 /*
4642  *      First up, calculate how much room there is in the RX ring queue.
4643  *      We also want to keep track of the longest possible copy length,
4644  *      this has to allow for the wrapping of the ring queue.
4645  */
4646         head = portp->rx.head;
4647         tail = portp->rx.tail;
4648         len = (head >= tail) ? (STL_RXBUFSIZE - (head - tail) - 1) :
4649                 (tail - head - 1);
4650
4651 /*
4652  *      To get the precise error type for each character we must switch
4653  *      back into CHAR error mode.
4654  */
4655         mr1 = stl_sc26198getreg(portp, MR1);
4656         stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
4657
4658         while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
4659                 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
4660                 ch = stl_sc26198getreg(portp, RXFIFO);
4661
4662                 if (status & SR_RXBREAK)
4663                         portp->stats.rxbreaks++;
4664                 if (status & SR_RXFRAMING)
4665                         portp->stats.rxframing++;
4666                 if (status & SR_RXPARITY)
4667                         portp->stats.rxparity++;
4668                 if (status & SR_RXOVERRUN)
4669                         portp->stats.rxoverrun++;
4670                 if ((portp->rxignoremsk & status) == 0) {
4671                         if ((portp->tty.t_state & TS_CAN_BYPASS_L_RINT) &&
4672                             ((status & SR_RXFRAMING) ||
4673                             ((status & SR_RXPARITY) &&
4674                             (portp->tty.t_iflag & INPCK))))
4675                                 ch = 0;
4676                         if ((portp->rxmarkmsk & status) == 0)
4677                                 status = 0;
4678                         if (len > 0) {
4679                                 *(head + STL_RXBUFSIZE) = status;
4680                                 *head++ = ch;
4681                                 if (head >= portp->rx.endbuf)
4682                                         head = portp->rx.buf;
4683                                 len--;
4684                         }
4685                 }
4686         }
4687
4688 /*
4689  *      To get correct interrupt class we must switch back into BLOCK
4690  *      error mode.
4691  */
4692         stl_sc26198setreg(portp, MR1, mr1);
4693
4694         portp->rx.head = head;
4695         portp->state |= ASY_RXDATA;
4696         stl_dotimeout();
4697 }
4698
4699 /*****************************************************************************/
4700
4701 /*
4702  *      Other interrupt handler. This includes modem signals, flow
4703  *      control actions, etc.
4704  */
4705
4706 static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
4707 {
4708         unsigned char   cir, ipr, xisr;
4709
4710 #if STLDEBUG
4711         printf("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
4712 #endif
4713
4714         cir = stl_sc26198getglobreg(portp, CIR);
4715
4716         switch (cir & CIR_SUBTYPEMASK) {
4717         case CIR_SUBCOS:
4718                 ipr = stl_sc26198getreg(portp, IPR);
4719                 if (ipr & IPR_DCDCHANGE) {
4720                         portp->state |= ASY_DCDCHANGE;
4721                         portp->stats.modem++;
4722                         stl_dotimeout();
4723                 }
4724                 break;
4725         case CIR_SUBXONXOFF:
4726                 xisr = stl_sc26198getreg(portp, XISR);
4727                 if (xisr & XISR_RXXONGOT) {
4728                         portp->state |= ASY_TXFLOWED;
4729                         portp->stats.txxoff++;
4730                 }
4731                 if (xisr & XISR_RXXOFFGOT) {
4732                         portp->state &= ~ASY_TXFLOWED;
4733                         portp->stats.txxon++;
4734                 }
4735                 break;
4736         case CIR_SUBBREAK:
4737                 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
4738                 stl_sc26198rxbadchars(portp);
4739                 break;
4740         default:
4741                 break;
4742         }
4743 }
4744
4745 /*****************************************************************************/
4746
4747 /*
4748  *      Interrupt service routine for sc26198 panels.
4749  */
4750
4751 static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
4752 {
4753         stlport_t       *portp;
4754         unsigned int    iack;
4755
4756 /* 
4757  *      Work around bug in sc26198 chip... Cannot have A6 address
4758  *      line of UART high, else iack will be returned as 0.
4759  */
4760         outb((iobase + 1), 0);
4761
4762         iack = inb(iobase + XP_IACK);
4763 #if STLDEBUG
4764         printf("stl_sc26198intr(panelp=%p,iobase=%x): iack=%x\n", panelp, iobase, iack);
4765 #endif
4766         portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4767
4768         if (iack & IVR_RXDATA)
4769                 stl_sc26198rxisr(portp, iack);
4770         else if (iack & IVR_TXDATA)
4771                 stl_sc26198txisr(portp);
4772         else
4773                 stl_sc26198otherisr(portp, iack);
4774 }
4775
4776 /*****************************************************************************/