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