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