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