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