ede5ef76e7d889e47c21cac1decef80843d6e833
[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         unsigned int    chipmask;
1924         int             i, j;
1925
1926 #if STLDEBUG
1927         kprintf("stl_initports(panelp=%x)\n", (int) panelp);
1928 #endif
1929
1930         chipmask = stl_panelinit(brdp, panelp);
1931
1932 /*
1933  *      All UART's are initialized if found. Now go through and setup
1934  *      each ports data structures. Also initialize each individual
1935  *      UART port.
1936  */
1937         for (i = 0; (i < panelp->nrports); i++) {
1938                 portp = kmalloc(sizeof(stlport_t), M_TTYS, M_WAITOK | M_ZERO);
1939
1940                 portp->portnr = i;
1941                 portp->brdnr = panelp->brdnr;
1942                 portp->panelnr = panelp->panelnr;
1943                 portp->uartp = panelp->uartp;
1944                 portp->clk = brdp->clk;
1945                 panelp->ports[i] = portp;
1946
1947                 j = STL_TXBUFSIZE + (2 * STL_RXBUFSIZE);
1948                 portp->tx.buf = kmalloc(j, M_TTYS, M_WAITOK);
1949                 portp->tx.endbuf = portp->tx.buf + STL_TXBUFSIZE;
1950                 portp->tx.head = portp->tx.buf;
1951                 portp->tx.tail = portp->tx.buf;
1952                 portp->rx.buf = portp->tx.buf + STL_TXBUFSIZE;
1953                 portp->rx.endbuf = portp->rx.buf + STL_RXBUFSIZE;
1954                 portp->rx.head = portp->rx.buf;
1955                 portp->rx.tail = portp->rx.buf;
1956                 portp->rxstatus.buf = portp->rx.buf + STL_RXBUFSIZE;
1957                 portp->rxstatus.endbuf = portp->rxstatus.buf + STL_RXBUFSIZE;
1958                 portp->rxstatus.head = portp->rxstatus.buf;
1959                 portp->rxstatus.tail = portp->rxstatus.buf;
1960                 bzero(portp->rxstatus.head, STL_RXBUFSIZE);
1961
1962                 portp->initintios.c_ispeed = STL_DEFSPEED;
1963                 portp->initintios.c_ospeed = STL_DEFSPEED;
1964                 portp->initintios.c_cflag = STL_DEFCFLAG;
1965                 portp->initintios.c_iflag = 0;
1966                 portp->initintios.c_oflag = 0;
1967                 portp->initintios.c_lflag = 0;
1968                 bcopy(&ttydefchars[0], &portp->initintios.c_cc[0],
1969                         sizeof(portp->initintios.c_cc));
1970                 portp->initouttios = portp->initintios;
1971                 portp->dtrwait = 3 * hz;
1972                 callout_init(&portp->dtr_ch);
1973
1974                 stl_portinit(brdp, panelp, portp);
1975         }
1976
1977         return(0);
1978 }
1979
1980 /*****************************************************************************/
1981
1982 /*
1983  *      Try to find and initialize an EasyIO board.
1984  */
1985
1986 static int stl_initeio(stlbrd_t *brdp)
1987 {
1988         stlpanel_t      *panelp;
1989         unsigned int    status;
1990
1991 #if STLDEBUG
1992         kprintf("stl_initeio(brdp=%x)\n", (int) brdp);
1993 #endif
1994
1995         brdp->ioctrl = brdp->ioaddr1 + 1;
1996         brdp->iostatus = brdp->ioaddr1 + 2;
1997         brdp->clk = EIO_CLK;
1998         brdp->isr = stl_eiointr;
1999
2000         status = inb(brdp->iostatus);
2001         switch (status & EIO_IDBITMASK) {
2002         case EIO_8PORTM:
2003                 brdp->clk = EIO_CLK8M;
2004                 /* fall thru */
2005         case EIO_8PORTRS:
2006         case EIO_8PORTDI:
2007                 brdp->nrports = 8;
2008                 break;
2009         case EIO_4PORTRS:
2010                 brdp->nrports = 4;
2011                 break;
2012         case EIO_MK3:
2013                 switch (status & EIO_BRDMASK) {
2014                 case ID_BRD4:
2015                         brdp->nrports = 4;
2016                         break;
2017                 case ID_BRD8:
2018                         brdp->nrports = 8;
2019                         break;
2020                 case ID_BRD16:
2021                         brdp->nrports = 16;
2022                         break;
2023                 default:
2024                         return(ENODEV);
2025                 }
2026                 brdp->ioctrl++;
2027                 break;
2028         default:
2029                 return(ENODEV);
2030         }
2031
2032         if (brdp->brdtype == BRD_EASYIOPCI) {
2033                 outb((brdp->ioaddr2 + 0x4c), 0x41);
2034         } else {
2035 /*
2036  *      Check that the supplied IRQ is good and then use it to setup the
2037  *      programmable interrupt bits on EIO board. Also set the edge/level
2038  *      triggered interrupt bit.
2039  */
2040                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2041                         (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2042                         kprintf("STALLION: invalid irq=%d for brd=%d\n",
2043                                brdp->irq, brdp->brdnr);
2044                         return(EINVAL);
2045                 }
2046                 outb(brdp->ioctrl, (stl_vecmap[brdp->irq] |
2047                         ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)));
2048         }
2049
2050         panelp = kmalloc(sizeof(stlpanel_t), M_TTYS, M_WAITOK | M_ZERO);
2051         panelp->brdnr = brdp->brdnr;
2052         panelp->panelnr = 0;
2053         panelp->nrports = brdp->nrports;
2054         panelp->iobase = brdp->ioaddr1;
2055         panelp->hwid = status;
2056         if ((status & EIO_IDBITMASK) == EIO_MK3) {
2057                 panelp->uartp = (void *) &stl_sc26198uart;
2058                 panelp->isr = stl_sc26198intr;
2059         } else {
2060                 panelp->uartp = (void *) &stl_cd1400uart;
2061                 panelp->isr = stl_cd1400eiointr;
2062         }
2063         brdp->panels[0] = panelp;
2064         brdp->nrpanels = 1;
2065         brdp->hwid = status;
2066         brdp->state |= BRD_FOUND;
2067         return(0);
2068 }
2069
2070 /*****************************************************************************/
2071
2072 /*
2073  *      Try to find an ECH board and initialize it. This code is capable of
2074  *      dealing with all types of ECH board.
2075  */
2076
2077 static int stl_initech(stlbrd_t *brdp)
2078 {
2079         stlpanel_t      *panelp;
2080         unsigned int    status, nxtid;
2081         int             panelnr, ioaddr, banknr, i;
2082
2083 #if STLDEBUG
2084         kprintf("stl_initech(brdp=%x)\n", (int) brdp);
2085 #endif
2086
2087 /*
2088  *      Set up the initial board register contents for boards. This varys a
2089  *      bit between the different board types. So we need to handle each
2090  *      separately. Also do a check that the supplied IRQ is good.
2091  */
2092         switch (brdp->brdtype) {
2093
2094         case BRD_ECH:
2095                 brdp->isr = stl_echatintr;
2096                 brdp->ioctrl = brdp->ioaddr1 + 1;
2097                 brdp->iostatus = brdp->ioaddr1 + 1;
2098                 status = inb(brdp->iostatus);
2099                 if ((status & ECH_IDBITMASK) != ECH_ID)
2100                         return(ENODEV);
2101                 brdp->hwid = status;
2102
2103                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2104                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2105                         kprintf("STALLION: invalid irq=%d for brd=%d\n",
2106                                 brdp->irq, brdp->brdnr);
2107                         return(EINVAL);
2108                 }
2109                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2110                 status |= (stl_vecmap[brdp->irq] << 1);
2111                 outb(brdp->ioaddr1, (status | ECH_BRDRESET));
2112                 brdp->ioctrlval = ECH_INTENABLE |
2113                         ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2114                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
2115                 outb(brdp->ioaddr1, status);
2116                 break;
2117
2118         case BRD_ECHMC:
2119                 brdp->isr = stl_echmcaintr;
2120                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2121                 brdp->iostatus = brdp->ioctrl;
2122                 status = inb(brdp->iostatus);
2123                 if ((status & ECH_IDBITMASK) != ECH_ID)
2124                         return(ENODEV);
2125                 brdp->hwid = status;
2126
2127                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2128                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2129                         kprintf("STALLION: invalid irq=%d for brd=%d\n",
2130                                 brdp->irq, brdp->brdnr);
2131                         return(EINVAL);
2132                 }
2133                 outb(brdp->ioctrl, ECHMC_BRDRESET);
2134                 outb(brdp->ioctrl, ECHMC_INTENABLE);
2135                 break;
2136
2137         case BRD_ECHPCI:
2138                 brdp->isr = stl_echpciintr;
2139                 brdp->ioctrl = brdp->ioaddr1 + 2;
2140                 break;
2141
2142         case BRD_ECH64PCI:
2143                 brdp->isr = stl_echpci64intr;
2144                 brdp->ioctrl = brdp->ioaddr2 + 0x40;
2145                 outb((brdp->ioaddr1 + 0x4c), 0x43);
2146                 break;
2147
2148         default:
2149                 kprintf("STALLION: unknown board type=%d\n", brdp->brdtype);
2150                 break;
2151         }
2152
2153         brdp->clk = ECH_CLK;
2154
2155 /*
2156  *      Scan through the secondary io address space looking for panels.
2157  *      As we find'em allocate and initialize panel structures for each.
2158  */
2159         ioaddr = brdp->ioaddr2;
2160         panelnr = 0;
2161         nxtid = 0;
2162         banknr = 0;
2163
2164         for (i = 0; (i < STL_MAXPANELS); i++) {
2165                 if (brdp->brdtype == BRD_ECHPCI) {
2166                         outb(brdp->ioctrl, nxtid);
2167                         ioaddr = brdp->ioaddr2;
2168                 }
2169                 status = inb(ioaddr + ECH_PNLSTATUS);
2170                 if ((status & ECH_PNLIDMASK) != nxtid)
2171                         break;
2172                 panelp = kmalloc(sizeof(stlpanel_t), M_TTYS, M_WAITOK | M_ZERO);
2173                 panelp->brdnr = brdp->brdnr;
2174                 panelp->panelnr = panelnr;
2175                 panelp->iobase = ioaddr;
2176                 panelp->pagenr = nxtid;
2177                 panelp->hwid = status;
2178                 brdp->bnk2panel[banknr] = panelp;
2179                 brdp->bnkpageaddr[banknr] = nxtid;
2180                 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2181
2182                 if (status & ECH_PNLXPID) {
2183                         panelp->uartp = (void *) &stl_sc26198uart;
2184                         panelp->isr = stl_sc26198intr;
2185                         if (status & ECH_PNL16PORT) {
2186                                 panelp->nrports = 16;
2187                                 brdp->bnk2panel[banknr] = panelp;
2188                                 brdp->bnkpageaddr[banknr] = nxtid;
2189                                 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2190                                         ECH_PNLSTATUS;
2191                         } else {
2192                                 panelp->nrports = 8;
2193                         }
2194                 } else {
2195                         panelp->uartp = (void *) &stl_cd1400uart;
2196                         panelp->isr = stl_cd1400echintr;
2197                         if (status & ECH_PNL16PORT) {
2198                                 panelp->nrports = 16;
2199                                 panelp->ackmask = 0x80;
2200                                 if (brdp->brdtype != BRD_ECHPCI)
2201                                         ioaddr += EREG_BANKSIZE;
2202                                 brdp->bnk2panel[banknr] = panelp;
2203                                 brdp->bnkpageaddr[banknr] = ++nxtid;
2204                                 brdp->bnkstataddr[banknr++] = ioaddr +
2205                                         ECH_PNLSTATUS;
2206                         } else {
2207                                 panelp->nrports = 8;
2208                                 panelp->ackmask = 0xc0;
2209                         }
2210                 }
2211
2212                 nxtid++;
2213                 ioaddr += EREG_BANKSIZE;
2214                 brdp->nrports += panelp->nrports;
2215                 brdp->panels[panelnr++] = panelp;
2216                 if ((brdp->brdtype == BRD_ECH) || (brdp->brdtype == BRD_ECHMC)){
2217                         if (ioaddr >= (brdp->ioaddr2 + 0x20)) {
2218                                 kprintf("STALLION: too many ports attached "
2219                                         "to board %d, remove last module\n",
2220                                         brdp->brdnr);
2221                                 break;
2222                         }
2223                 }
2224         }
2225
2226         brdp->nrpanels = panelnr;
2227         brdp->nrbnks = banknr;
2228         if (brdp->brdtype == BRD_ECH)
2229                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
2230
2231         brdp->state |= BRD_FOUND;
2232         return(0);
2233 }
2234
2235 /*****************************************************************************/
2236
2237 /*
2238  *      Initialize and configure the specified board. This firstly probes
2239  *      for the board, if it is found then the board is initialized and
2240  *      then all its ports are initialized as well.
2241  */
2242
2243 static int stl_brdinit(stlbrd_t *brdp)
2244 {
2245         stlpanel_t      *panelp;
2246         int             i, j, k;
2247
2248 #if STLDEBUG
2249         kprintf("stl_brdinit(brdp=%x): unit=%d type=%d io1=%x io2=%x irq=%d\n",
2250                 (int) brdp, brdp->brdnr, brdp->brdtype, brdp->ioaddr1,
2251                 brdp->ioaddr2, brdp->irq);
2252 #endif
2253
2254         switch (brdp->brdtype) {
2255         case BRD_EASYIO:
2256         case BRD_EASYIOPCI:
2257                 stl_initeio(brdp);
2258                 break;
2259         case BRD_ECH:
2260         case BRD_ECHMC:
2261         case BRD_ECHPCI:
2262         case BRD_ECH64PCI:
2263                 stl_initech(brdp);
2264                 break;
2265         default:
2266                 kprintf("STALLION: unit=%d is unknown board type=%d\n",
2267                         brdp->brdnr, brdp->brdtype);
2268                 return(ENODEV);
2269         }
2270
2271         stl_brds[brdp->brdnr] = brdp;
2272         if ((brdp->state & BRD_FOUND) == 0) {
2273 #if 0
2274                 kprintf("STALLION: %s board not found, unit=%d io=%x irq=%d\n",
2275                         stl_brdnames[brdp->brdtype], brdp->brdnr,
2276                         brdp->ioaddr1, brdp->irq);
2277 #endif
2278                 return(ENODEV);
2279         }
2280
2281         for (i = 0, k = 0; (i < STL_MAXPANELS); i++) {
2282                 panelp = brdp->panels[i];
2283                 if (panelp != NULL) {
2284                         stl_initports(brdp, panelp);
2285                         for (j = 0; (j < panelp->nrports); j++)
2286                                 brdp->ports[k++] = panelp->ports[j];
2287                 }
2288         }
2289
2290         kprintf("stl%d: %s (driver version %s) unit=%d nrpanels=%d nrports=%d\n",
2291                 brdp->unitid, stl_brdnames[brdp->brdtype], stl_drvversion,
2292                 brdp->brdnr, brdp->nrpanels, brdp->nrports);
2293         return(0);
2294 }
2295
2296 /*****************************************************************************/
2297
2298 /*
2299  *      Return the board stats structure to user app.
2300  */
2301
2302 static int stl_getbrdstats(caddr_t data)
2303 {
2304         stlbrd_t        *brdp;
2305         stlpanel_t      *panelp;
2306         int             i;
2307
2308         stl_brdstats = *((combrd_t *) data);
2309         if (stl_brdstats.brd >= STL_MAXBRDS)
2310                 return(-ENODEV);
2311         brdp = stl_brds[stl_brdstats.brd];
2312         if (brdp == NULL)
2313                 return(-ENODEV);
2314
2315         bzero(&stl_brdstats, sizeof(combrd_t));
2316         stl_brdstats.brd = brdp->brdnr;
2317         stl_brdstats.type = brdp->brdtype;
2318         stl_brdstats.hwid = brdp->hwid;
2319         stl_brdstats.state = brdp->state;
2320         stl_brdstats.ioaddr = brdp->ioaddr1;
2321         stl_brdstats.ioaddr2 = brdp->ioaddr2;
2322         stl_brdstats.irq = brdp->irq;
2323         stl_brdstats.nrpanels = brdp->nrpanels;
2324         stl_brdstats.nrports = brdp->nrports;
2325         for (i = 0; (i < brdp->nrpanels); i++) {
2326                 panelp = brdp->panels[i];
2327                 stl_brdstats.panels[i].panel = i;
2328                 stl_brdstats.panels[i].hwid = panelp->hwid;
2329                 stl_brdstats.panels[i].nrports = panelp->nrports;
2330         }
2331
2332         *((combrd_t *) data) = stl_brdstats;
2333         return(0);
2334 }
2335
2336 /*****************************************************************************/
2337
2338 /*
2339  *      Resolve the referenced port number into a port struct pointer.
2340  */
2341
2342 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2343 {
2344         stlbrd_t        *brdp;
2345         stlpanel_t      *panelp;
2346
2347         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2348                 return(NULL);
2349         brdp = stl_brds[brdnr];
2350         if (brdp == NULL)
2351                 return(NULL);
2352         if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2353                 return(NULL);
2354         panelp = brdp->panels[panelnr];
2355         if (panelp == NULL)
2356                 return(NULL);
2357         if ((portnr < 0) || (portnr >= panelp->nrports))
2358                 return(NULL);
2359         return(panelp->ports[portnr]);
2360 }
2361
2362 /*****************************************************************************/
2363
2364 /*
2365  *      Return the port stats structure to user app. A NULL port struct
2366  *      pointer passed in means that we need to find out from the app
2367  *      what port to get stats for (used through board control device).
2368  */
2369
2370 static int stl_getportstats(stlport_t *portp, caddr_t data)
2371 {
2372         unsigned char   *head, *tail;
2373
2374         if (portp == NULL) {
2375                 stl_comstats = *((comstats_t *) data);
2376                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2377                         stl_comstats.port);
2378                 if (portp == NULL)
2379                         return(-ENODEV);
2380         }
2381
2382         portp->stats.state = portp->state;
2383         /*portp->stats.flags = portp->flags;*/
2384         portp->stats.hwid = portp->hwid;
2385         portp->stats.ttystate = portp->tty.t_state;
2386         portp->stats.cflags = portp->tty.t_cflag;
2387         portp->stats.iflags = portp->tty.t_iflag;
2388         portp->stats.oflags = portp->tty.t_oflag;
2389         portp->stats.lflags = portp->tty.t_lflag;
2390
2391         head = portp->tx.head;
2392         tail = portp->tx.tail;
2393         portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
2394                 (STL_TXBUFSIZE - (tail - head)));
2395
2396         head = portp->rx.head;
2397         tail = portp->rx.tail;
2398         portp->stats.rxbuffered = (head >= tail) ? (head - tail) :
2399                 (STL_RXBUFSIZE - (tail - head));
2400
2401         portp->stats.signals = (unsigned long) stl_getsignals(portp);
2402
2403         *((comstats_t *) data) = portp->stats;
2404         return(0);
2405 }
2406
2407 /*****************************************************************************/
2408
2409 /*
2410  *      Clear the port stats structure. We also return it zeroed out...
2411  */
2412
2413 static int stl_clrportstats(stlport_t *portp, caddr_t data)
2414 {
2415         if (portp == NULL) {
2416                 stl_comstats = *((comstats_t *) data);
2417                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2418                         stl_comstats.port);
2419                 if (portp == NULL)
2420                         return(ENODEV);
2421         }
2422
2423         bzero(&portp->stats, sizeof(comstats_t));
2424         portp->stats.brd = portp->brdnr;
2425         portp->stats.panel = portp->panelnr;
2426         portp->stats.port = portp->portnr;
2427         *((comstats_t *) data) = stl_comstats;
2428         return(0);
2429 }
2430
2431 /*****************************************************************************/
2432
2433 /*
2434  *      The "staliomem" device is used for stats collection in this driver.
2435  */
2436
2437 static int stl_memioctl(cdev_t dev, unsigned long cmd, caddr_t data, int flag)
2438 {
2439         int             rc;
2440
2441 #if STLDEBUG
2442         kprintf("stl_memioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
2443                 devtoname(dev), cmd, (void *) data, flag);
2444 #endif
2445
2446         rc = 0;
2447
2448         switch (cmd) {
2449         case COM_GETPORTSTATS:
2450                 rc = stl_getportstats(NULL, data);
2451                 break;
2452         case COM_CLRPORTSTATS:
2453                 rc = stl_clrportstats(NULL, data);
2454                 break;
2455         case COM_GETBRDSTATS:
2456                 rc = stl_getbrdstats(data);
2457                 break;
2458         default:
2459                 rc = ENOTTY;
2460                 break;
2461         }
2462
2463         return(rc);
2464 }
2465
2466 /*****************************************************************************/
2467
2468 /*****************************************************************************/
2469 /*                         CD1400 UART CODE                                  */
2470 /*****************************************************************************/
2471
2472 /*
2473  *      These functions get/set/update the registers of the cd1400 UARTs.
2474  *      Access to the cd1400 registers is via an address/data io port pair.
2475  */
2476
2477 static int stl_cd1400getreg(stlport_t *portp, int regnr)
2478 {
2479         outb(portp->ioaddr, (regnr + portp->uartaddr));
2480         return(inb(portp->ioaddr + EREG_DATA));
2481 }
2482
2483 /*****************************************************************************/
2484
2485 static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
2486 {
2487         outb(portp->ioaddr, (regnr + portp->uartaddr));
2488         outb((portp->ioaddr + EREG_DATA), value);
2489 }
2490
2491 /*****************************************************************************/
2492
2493 static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
2494 {
2495         outb(portp->ioaddr, (regnr + portp->uartaddr));
2496         if (inb(portp->ioaddr + EREG_DATA) != value) {
2497                 outb((portp->ioaddr + EREG_DATA), value);
2498                 return(1);
2499         }
2500         return(0);
2501 }
2502
2503 /*****************************************************************************/
2504
2505 static void stl_cd1400flush(stlport_t *portp, int flag)
2506 {
2507
2508 #if STLDEBUG
2509         kprintf("stl_cd1400flush(portp=%x,flag=%x)\n", (int) portp, flag);
2510 #endif
2511
2512         if (portp == NULL)
2513                 return;
2514
2515         crit_enter();
2516
2517         if (flag & FWRITE) {
2518                 BRDENABLE(portp->brdnr, portp->pagenr);
2519                 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2520                 stl_cd1400ccrwait(portp);
2521                 stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
2522                 stl_cd1400ccrwait(portp);
2523                 BRDDISABLE(portp->brdnr);
2524         }
2525
2526         if (flag & FREAD) {
2527                 /* Hmmm */
2528         }
2529
2530         crit_exit();
2531 }
2532
2533 /*****************************************************************************/
2534
2535 static void stl_cd1400ccrwait(stlport_t *portp)
2536 {
2537         int     i;
2538
2539         for (i = 0; (i < CCR_MAXWAIT); i++) {
2540                 if (stl_cd1400getreg(portp, CCR) == 0)
2541                         return;
2542         }
2543
2544         kprintf("stl%d: cd1400 device not responding, panel=%d port=%d\n",
2545             portp->brdnr, portp->panelnr, portp->portnr);
2546 }
2547
2548 /*****************************************************************************/
2549
2550 /*
2551  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
2552  *      chars is pretty simple, stuff as many as possible from the TX buffer
2553  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
2554  *      are embedded as commands in the data stream. Oh no, had to use a goto!
2555  */
2556
2557 static __inline void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
2558 {
2559         struct tty      *tp;
2560         stlport_t       *portp;
2561         unsigned char   ioack, srer;
2562         char            *head, *tail;
2563         int             len, stlen;
2564
2565 #if STLDEBUG
2566         kprintf("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
2567 #endif
2568
2569         ioack = inb(ioaddr + EREG_TXACK);
2570         if (((ioack & panelp->ackmask) != 0) ||
2571             ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
2572                 kprintf("STALLION: bad TX interrupt ack value=%x\n",
2573                         ioack);
2574                 return;
2575         }
2576         portp = panelp->ports[(ioack >> 3)];
2577         tp = &portp->tty;
2578
2579 /*
2580  *      Unfortunately we need to handle breaks in the data stream, since
2581  *      this is the only way to generate them on the cd1400. Do it now if
2582  *      a break is to be sent. Some special cases here: brklen is -1 then
2583  *      start sending an un-timed break, if brklen is -2 then stop sending
2584  *      an un-timed break, if brklen is -3 then we have just sent an
2585  *      un-timed break and do not want any data to go out, if brklen is -4
2586  *      then a break has just completed so clean up the port settings.
2587  */
2588         if (portp->brklen != 0) {
2589                 if (portp->brklen >= -1) {
2590                         outb(ioaddr, (TDR + portp->uartaddr));
2591                         outb((ioaddr + EREG_DATA), ETC_CMD);
2592                         outb((ioaddr + EREG_DATA), ETC_STARTBREAK);
2593                         if (portp->brklen > 0) {
2594                                 outb((ioaddr + EREG_DATA), ETC_CMD);
2595                                 outb((ioaddr + EREG_DATA), ETC_DELAY);
2596                                 outb((ioaddr + EREG_DATA), portp->brklen);
2597                                 outb((ioaddr + EREG_DATA), ETC_CMD);
2598                                 outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
2599                                 portp->brklen = -4;
2600                         } else {
2601                                 portp->brklen = -3;
2602                         }
2603                 } else if (portp->brklen == -2) {
2604                         outb(ioaddr, (TDR + portp->uartaddr));
2605                         outb((ioaddr + EREG_DATA), ETC_CMD);
2606                         outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
2607                         portp->brklen = -4;
2608                 } else if (portp->brklen == -3) {
2609                         outb(ioaddr, (SRER + portp->uartaddr));
2610                         srer = inb(ioaddr + EREG_DATA);
2611                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
2612                         outb((ioaddr + EREG_DATA), srer);
2613                 } else {
2614                         outb(ioaddr, (COR2 + portp->uartaddr));
2615                         outb((ioaddr + EREG_DATA),
2616                                 (inb(ioaddr + EREG_DATA) & ~COR2_ETC));
2617                         portp->brklen = 0;
2618                 }
2619                 goto stl_txalldone;
2620         }
2621
2622         head = portp->tx.head;
2623         tail = portp->tx.tail;
2624         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
2625         if ((len == 0) || ((len < STL_TXBUFLOW) &&
2626             ((portp->state & ASY_TXLOW) == 0))) {
2627                 portp->state |= ASY_TXLOW;
2628                 stl_dotimeout();
2629         }
2630
2631         if (len == 0) {
2632                 outb(ioaddr, (SRER + portp->uartaddr));
2633                 srer = inb(ioaddr + EREG_DATA);
2634                 if (srer & SRER_TXDATA) {
2635                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
2636                 } else {
2637                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
2638                         portp->state |= ASY_TXEMPTY;
2639                         portp->state &= ~ASY_TXBUSY;
2640                 }
2641                 outb((ioaddr + EREG_DATA), srer);
2642         } else {
2643                 len = MIN(len, CD1400_TXFIFOSIZE);
2644                 portp->stats.txtotal += len;
2645                 stlen = MIN(len, (portp->tx.endbuf - tail));
2646                 outb(ioaddr, (TDR + portp->uartaddr));
2647                 outsb((ioaddr + EREG_DATA), tail, stlen);
2648                 len -= stlen;
2649                 tail += stlen;
2650                 if (tail >= portp->tx.endbuf)
2651                         tail = portp->tx.buf;
2652                 if (len > 0) {
2653                         outsb((ioaddr + EREG_DATA), tail, len);
2654                         tail += len;
2655                 }
2656                 portp->tx.tail = tail;
2657         }
2658
2659 stl_txalldone:
2660         outb(ioaddr, (EOSRR + portp->uartaddr));
2661         outb((ioaddr + EREG_DATA), 0);
2662 }
2663
2664 /*****************************************************************************/
2665
2666 /*
2667  *      Receive character interrupt handler. Determine if we have good chars
2668  *      or bad chars and then process appropriately.
2669  */
2670
2671 static __inline void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
2672 {
2673         stlport_t       *portp;
2674         struct tty      *tp;
2675         unsigned int    ioack, len, buflen, stlen;
2676         unsigned char   status;
2677         char            ch;
2678         char            *head, *tail;
2679
2680 #if STLDEBUG
2681         kprintf("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
2682 #endif
2683
2684         ioack = inb(ioaddr + EREG_RXACK);
2685         if ((ioack & panelp->ackmask) != 0) {
2686                 kprintf("STALLION: bad RX interrupt ack value=%x\n", ioack);
2687                 return;
2688         }
2689         portp = panelp->ports[(ioack >> 3)];
2690         tp = &portp->tty;
2691
2692 /*
2693  *      First up, calculate how much room there is in the RX ring queue.
2694  *      We also want to keep track of the longest possible copy length,
2695  *      this has to allow for the wrapping of the ring queue.
2696  */
2697         head = portp->rx.head;
2698         tail = portp->rx.tail;
2699         if (head >= tail) {
2700                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
2701                 stlen = portp->rx.endbuf - head;
2702         } else {
2703                 buflen = tail - head - 1;
2704                 stlen = buflen;
2705         }
2706
2707 /*
2708  *      Check if the input buffer is near full. If so then we should take
2709  *      some flow control action... It is very easy to do hardware and
2710  *      software flow control from here since we have the port selected on
2711  *      the UART.
2712  */
2713         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
2714                 if (((portp->state & ASY_RTSFLOW) == 0) &&
2715                     (portp->state & ASY_RTSFLOWMODE)) {
2716                         portp->state |= ASY_RTSFLOW;
2717                         stl_cd1400setreg(portp, MCOR1,
2718                                 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
2719                         stl_cd1400setreg(portp, MSVR2, 0);
2720                         portp->stats.rxrtsoff++;
2721                 }
2722         }
2723
2724 /*
2725  *      OK we are set, process good data... If the RX ring queue is full
2726  *      just chuck the chars - don't leave them in the UART.
2727  */
2728         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
2729                 outb(ioaddr, (RDCR + portp->uartaddr));
2730                 len = inb(ioaddr + EREG_DATA);
2731                 if (buflen == 0) {
2732                         outb(ioaddr, (RDSR + portp->uartaddr));
2733                         insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
2734                         portp->stats.rxlost += len;
2735                         portp->stats.rxtotal += len;
2736                 } else {
2737                         len = MIN(len, buflen);
2738                         portp->stats.rxtotal += len;
2739                         stlen = MIN(len, stlen);
2740                         if (len > 0) {
2741                                 outb(ioaddr, (RDSR + portp->uartaddr));
2742                                 insb((ioaddr + EREG_DATA), head, stlen);
2743                                 head += stlen;
2744                                 if (head >= portp->rx.endbuf) {
2745                                         head = portp->rx.buf;
2746                                         len -= stlen;
2747                                         insb((ioaddr + EREG_DATA), head, len);
2748                                         head += len;
2749                                 }
2750                         }
2751                 }
2752         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
2753                 outb(ioaddr, (RDSR + portp->uartaddr));
2754                 status = inb(ioaddr + EREG_DATA);
2755                 ch = inb(ioaddr + EREG_DATA);
2756                 if (status & ST_BREAK)
2757                         portp->stats.rxbreaks++;
2758                 if (status & ST_FRAMING)
2759                         portp->stats.rxframing++;
2760                 if (status & ST_PARITY)
2761                         portp->stats.rxparity++;
2762                 if (status & ST_OVERRUN)
2763                         portp->stats.rxoverrun++;
2764                 if (status & ST_SCHARMASK) {
2765                         if ((status & ST_SCHARMASK) == ST_SCHAR1)
2766                                 portp->stats.txxon++;
2767                         if ((status & ST_SCHARMASK) == ST_SCHAR2)
2768                                 portp->stats.txxoff++;
2769                         goto stl_rxalldone;
2770                 }
2771                 if ((portp->rxignoremsk & status) == 0) {
2772                         if ((tp->t_state & TS_CAN_BYPASS_L_RINT) &&
2773                             ((status & ST_FRAMING) ||
2774                             ((status & ST_PARITY) && (tp->t_iflag & INPCK))))
2775                                 ch = 0;
2776                         if ((portp->rxmarkmsk & status) == 0)
2777                                 status = 0;
2778                         *(head + STL_RXBUFSIZE) = status;
2779                         *head++ = ch;
2780                         if (head >= portp->rx.endbuf)
2781                                 head = portp->rx.buf;
2782                 }
2783         } else {
2784                 kprintf("STALLION: bad RX interrupt ack value=%x\n", ioack);
2785                 return;
2786         }
2787
2788         portp->rx.head = head;
2789         portp->state |= ASY_RXDATA;
2790         stl_dotimeout();
2791
2792 stl_rxalldone:
2793         outb(ioaddr, (EOSRR + portp->uartaddr));
2794         outb((ioaddr + EREG_DATA), 0);
2795 }
2796
2797 /*****************************************************************************/
2798
2799 /*
2800  *      Modem interrupt handler. The is called when the modem signal line
2801  *      (DCD) has changed state.
2802  */
2803
2804 static __inline void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
2805 {
2806         stlport_t       *portp;
2807         unsigned int    ioack;
2808         unsigned char   misr;
2809
2810 #if STLDEBUG
2811         kprintf("stl_cd1400mdmisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
2812 #endif
2813
2814         ioack = inb(ioaddr + EREG_MDACK);
2815         if (((ioack & panelp->ackmask) != 0) ||
2816             ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
2817                 kprintf("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
2818                 return;
2819         }
2820         portp = panelp->ports[(ioack >> 3)];
2821
2822         outb(ioaddr, (MISR + portp->uartaddr));
2823         misr = inb(ioaddr + EREG_DATA);
2824         if (misr & MISR_DCD) {
2825                 portp->state |= ASY_DCDCHANGE;
2826                 portp->stats.modem++;
2827                 stl_dotimeout();
2828         }
2829
2830         outb(ioaddr, (EOSRR + portp->uartaddr));
2831         outb((ioaddr + EREG_DATA), 0);
2832 }
2833
2834 /*****************************************************************************/
2835
2836 /*
2837  *      Interrupt service routine for cd1400 EasyIO boards.
2838  */
2839
2840 static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
2841 {
2842         unsigned char   svrtype;
2843
2844 #if STLDEBUG
2845         kprintf("stl_cd1400eiointr(panelp=%x,iobase=%x)\n", (int) panelp,
2846                 iobase);
2847 #endif
2848
2849         outb(iobase, SVRR);
2850         svrtype = inb(iobase + EREG_DATA);
2851         if (panelp->nrports > 4) {
2852                 outb(iobase, (SVRR + 0x80));
2853                 svrtype |= inb(iobase + EREG_DATA);
2854         }
2855 #if STLDEBUG
2856 kprintf("stl_cd1400eiointr(panelp=%x,iobase=%x): svrr=%x\n", (int) panelp, iobase, svrtype);
2857 #endif
2858
2859         if (svrtype & SVRR_RX)
2860                 stl_cd1400rxisr(panelp, iobase);
2861         else if (svrtype & SVRR_TX)
2862                 stl_cd1400txisr(panelp, iobase);
2863         else if (svrtype & SVRR_MDM)
2864                 stl_cd1400mdmisr(panelp, iobase);
2865 }
2866
2867 /*****************************************************************************/
2868
2869 /*
2870  *      Interrupt service routine for cd1400 panels.
2871  */
2872
2873 static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
2874 {
2875         unsigned char   svrtype;
2876
2877 #if STLDEBUG
2878         kprintf("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
2879                 iobase);
2880 #endif
2881
2882         outb(iobase, SVRR);
2883         svrtype = inb(iobase + EREG_DATA);
2884         outb(iobase, (SVRR + 0x80));
2885         svrtype |= inb(iobase + EREG_DATA);
2886         if (svrtype & SVRR_RX)
2887                 stl_cd1400rxisr(panelp, iobase);
2888         else if (svrtype & SVRR_TX)
2889                 stl_cd1400txisr(panelp, iobase);
2890         else if (svrtype & SVRR_MDM)
2891                 stl_cd1400mdmisr(panelp, iobase);
2892 }
2893
2894 /*****************************************************************************/
2895
2896 /*
2897  *      Set up the cd1400 registers for a port based on the termios port
2898  *      settings.
2899  */
2900
2901 static int stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
2902 {
2903         unsigned int    clkdiv;
2904         unsigned char   cor1, cor2, cor3;
2905         unsigned char   cor4, cor5, ccr;
2906         unsigned char   srer, sreron, sreroff;
2907         unsigned char   mcor1, mcor2, rtpr;
2908         unsigned char   clk, div;
2909
2910 #if STLDEBUG
2911         kprintf("stl_cd1400setport(portp=%x,tiosp=%x): brdnr=%d portnr=%d\n",
2912                 (int) portp, (int) tiosp, portp->brdnr, portp->portnr);
2913 #endif
2914
2915         cor1 = 0;
2916         cor2 = 0;
2917         cor3 = 0;
2918         cor4 = 0;
2919         cor5 = 0;
2920         ccr = 0;
2921         rtpr = 0;
2922         clk = 0;
2923         div = 0;
2924         mcor1 = 0;
2925         mcor2 = 0;
2926         sreron = 0;
2927         sreroff = 0;
2928
2929 /*
2930  *      Set up the RX char ignore mask with those RX error types we
2931  *      can ignore. We could have used some special modes of the cd1400
2932  *      UART to help, but it is better this way because we can keep stats
2933  *      on the number of each type of RX exception event.
2934  */
2935         portp->rxignoremsk = 0;
2936         if (tiosp->c_iflag & IGNPAR)
2937                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
2938         if (tiosp->c_iflag & IGNBRK)
2939                 portp->rxignoremsk |= ST_BREAK;
2940
2941         portp->rxmarkmsk = ST_OVERRUN;
2942         if (tiosp->c_iflag & (INPCK | PARMRK))
2943                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2944         if (tiosp->c_iflag & BRKINT)
2945                 portp->rxmarkmsk |= ST_BREAK;
2946
2947 /*
2948  *      Go through the char size, parity and stop bits and set all the
2949  *      option registers appropriately.
2950  */
2951         switch (tiosp->c_cflag & CSIZE) {
2952         case CS5:
2953                 cor1 |= COR1_CHL5;
2954                 break;
2955         case CS6:
2956                 cor1 |= COR1_CHL6;
2957                 break;
2958         case CS7:
2959                 cor1 |= COR1_CHL7;
2960                 break;
2961         default:
2962                 cor1 |= COR1_CHL8;
2963                 break;
2964         }
2965
2966         if (tiosp->c_cflag & CSTOPB)
2967                 cor1 |= COR1_STOP2;
2968         else
2969                 cor1 |= COR1_STOP1;
2970
2971         if (tiosp->c_cflag & PARENB) {
2972                 if (tiosp->c_cflag & PARODD)
2973                         cor1 |= (COR1_PARENB | COR1_PARODD);
2974                 else
2975                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
2976         } else {
2977                 cor1 |= COR1_PARNONE;
2978         }
2979
2980 /*
2981  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2982  *      space for hardware flow control and the like. This should be set to
2983  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
2984  *      really be based on VTIME...
2985  */
2986         cor3 |= FIFO_RXTHRESHOLD;
2987         rtpr = 2;
2988
2989 /*
2990  *      Calculate the baud rate timers. For now we will just assume that
2991  *      the input and output baud are the same. Could have used a baud
2992  *      table here, but this way we can generate virtually any baud rate
2993  *      we like!
2994  */
2995         if (tiosp->c_ispeed == 0)
2996                 tiosp->c_ispeed = tiosp->c_ospeed;
2997         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > CD1400_MAXBAUD))
2998                 return(EINVAL);
2999
3000         if (tiosp->c_ospeed > 0) {
3001                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
3002                         clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) /
3003                                 tiosp->c_ospeed);
3004                         if (clkdiv < 0x100)
3005                                 break;
3006                 }
3007                 div = (unsigned char) clkdiv;
3008         }
3009
3010 /*
3011  *      Check what form of modem signaling is required and set it up.
3012  */
3013         if ((tiosp->c_cflag & CLOCAL) == 0) {
3014                 mcor1 |= MCOR1_DCD;
3015                 mcor2 |= MCOR2_DCD;
3016                 sreron |= SRER_MODEM;
3017         }
3018
3019 /*
3020  *      Setup cd1400 enhanced modes if we can. In particular we want to
3021  *      handle as much of the flow control as possbile automatically. As
3022  *      well as saving a few CPU cycles it will also greatly improve flow
3023  *      control reliablilty.
3024  */
3025         if (tiosp->c_iflag & IXON) {
3026                 cor2 |= COR2_TXIBE;
3027                 cor3 |= COR3_SCD12;
3028                 if (tiosp->c_iflag & IXANY)
3029                         cor2 |= COR2_IXM;
3030         }
3031
3032         if (tiosp->c_cflag & CCTS_OFLOW)
3033                 cor2 |= COR2_CTSAE;
3034         if (tiosp->c_cflag & CRTS_IFLOW)
3035                 mcor1 |= FIFO_RTSTHRESHOLD;
3036
3037 /*
3038  *      All cd1400 register values calculated so go through and set them
3039  *      all up.
3040  */
3041 #if STLDEBUG
3042         kprintf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
3043                 portp->panelnr, portp->brdnr);
3044         kprintf("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n", cor1, cor2,
3045                 cor3, cor4, cor5);
3046         kprintf("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
3047                 mcor1, mcor2, rtpr, sreron, sreroff);
3048         kprintf("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
3049         kprintf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
3050                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP], tiosp->c_cc[VSTART],
3051                 tiosp->c_cc[VSTOP]);
3052 #endif
3053
3054         crit_enter();
3055         BRDENABLE(portp->brdnr, portp->pagenr);
3056         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3057         srer = stl_cd1400getreg(portp, SRER);
3058         stl_cd1400setreg(portp, SRER, 0);
3059         ccr += stl_cd1400updatereg(portp, COR1, cor1);
3060         ccr += stl_cd1400updatereg(portp, COR2, cor2);
3061         ccr += stl_cd1400updatereg(portp, COR3, cor3);
3062         if (ccr) {
3063                 stl_cd1400ccrwait(portp);
3064                 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3065         }
3066         stl_cd1400setreg(portp, COR4, cor4);
3067         stl_cd1400setreg(portp, COR5, cor5);
3068         stl_cd1400setreg(portp, MCOR1, mcor1);
3069         stl_cd1400setreg(portp, MCOR2, mcor2);
3070         if (tiosp->c_ospeed == 0) {
3071                 stl_cd1400setreg(portp, MSVR1, 0);
3072         } else {
3073                 stl_cd1400setreg(portp, MSVR1, MSVR1_DTR);
3074                 stl_cd1400setreg(portp, TCOR, clk);
3075                 stl_cd1400setreg(portp, TBPR, div);
3076                 stl_cd1400setreg(portp, RCOR, clk);
3077                 stl_cd1400setreg(portp, RBPR, div);
3078         }
3079         stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3080         stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3081         stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3082         stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3083         stl_cd1400setreg(portp, RTPR, rtpr);
3084         mcor1 = stl_cd1400getreg(portp, MSVR1);
3085         if (mcor1 & MSVR1_DCD)
3086                 portp->sigs |= TIOCM_CD;
3087         else
3088                 portp->sigs &= ~TIOCM_CD;
3089         stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3090         BRDDISABLE(portp->brdnr);
3091         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
3092         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
3093         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
3094         stl_ttyoptim(portp, tiosp);
3095         crit_exit();
3096
3097         return(0);
3098 }
3099
3100 /*****************************************************************************/
3101
3102 /*
3103  *      Action the flow control as required. The hw and sw args inform the
3104  *      routine what flow control methods it should try.
3105  */
3106
3107 static void stl_cd1400sendflow(stlport_t *portp, int hw, int sw)
3108 {
3109
3110 #if STLDEBUG
3111         kprintf("stl_cd1400sendflow(portp=%x,hw=%d,sw=%d)\n",
3112                 (int) portp, hw, sw);
3113 #endif
3114
3115         crit_enter();
3116         BRDENABLE(portp->brdnr, portp->pagenr);
3117         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3118
3119         if (sw >= 0) {
3120                 stl_cd1400ccrwait(portp);
3121                 if (sw) {
3122                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3123                         portp->stats.rxxoff++;
3124                 } else {
3125                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3126                         portp->stats.rxxon++;
3127                 }
3128                 stl_cd1400ccrwait(portp);
3129         }
3130
3131         if (hw == 0) {
3132                 portp->state |= ASY_RTSFLOW;
3133                 stl_cd1400setreg(portp, MCOR1,
3134                         (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3135                 stl_cd1400setreg(portp, MSVR2, 0);
3136                 portp->stats.rxrtsoff++;
3137         } else if (hw > 0) {
3138                 portp->state &= ~ASY_RTSFLOW;
3139                 stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3140                 stl_cd1400setreg(portp, MCOR1,
3141                         (stl_cd1400getreg(portp, MCOR1) | FIFO_RTSTHRESHOLD));
3142                 portp->stats.rxrtson++;
3143         }
3144
3145         BRDDISABLE(portp->brdnr);
3146         crit_exit();
3147 }
3148
3149 /*****************************************************************************/
3150
3151 /*
3152  *      Return the current state of data flow on this port. This is only
3153  *      really interresting when determining if data has fully completed
3154  *      transmission or not... This is easy for the cd1400, it accurately
3155  *      maintains the busy port flag.
3156  */
3157
3158 static int stl_cd1400datastate(stlport_t *portp)
3159 {
3160 #if STLDEBUG
3161         kprintf("stl_cd1400datastate(portp=%x)\n", (int) portp);
3162 #endif
3163
3164         if (portp == NULL)
3165                 return(0);
3166
3167         return((portp->state & ASY_TXBUSY) ? 1 : 0);
3168 }
3169
3170 /*****************************************************************************/
3171
3172 /*
3173  *      Set the state of the DTR and RTS signals. Got to do some extra
3174  *      work here to deal hardware flow control.
3175  */
3176
3177 static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
3178 {
3179         unsigned char   msvr1, msvr2;
3180
3181 #if STLDEBUG
3182         kprintf("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n", (int) portp,
3183                 dtr, rts);
3184 #endif
3185
3186         msvr1 = 0;
3187         msvr2 = 0;
3188         if (dtr > 0)
3189                 msvr1 = MSVR1_DTR;
3190         if (rts > 0)
3191                 msvr2 = MSVR2_RTS;
3192
3193         crit_enter();
3194         BRDENABLE(portp->brdnr, portp->pagenr);
3195         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3196         if (rts >= 0) {
3197                 if (portp->tty.t_cflag & CRTS_IFLOW) {
3198                         if (rts == 0) {
3199                                 stl_cd1400setreg(portp, MCOR1,
3200                                       (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3201                                 portp->stats.rxrtsoff++;
3202                         } else {
3203                                 stl_cd1400setreg(portp, MCOR1,
3204                                         (stl_cd1400getreg(portp, MCOR1) |
3205                                         FIFO_RTSTHRESHOLD));
3206                                 portp->stats.rxrtson++;
3207                         }
3208                 }
3209                 stl_cd1400setreg(portp, MSVR2, msvr2);
3210         }
3211         if (dtr >= 0)
3212                 stl_cd1400setreg(portp, MSVR1, msvr1);
3213         BRDDISABLE(portp->brdnr);
3214         crit_exit();
3215 }
3216
3217 /*****************************************************************************/
3218
3219 /*
3220  *      Get the state of the signals.
3221  */
3222
3223 static int stl_cd1400getsignals(stlport_t *portp)
3224 {
3225         unsigned char   msvr1, msvr2;
3226         int             sigs;
3227
3228 #if STLDEBUG
3229         kprintf("stl_cd1400getsignals(portp=%x)\n", (int) portp);
3230 #endif
3231
3232         crit_enter();
3233         BRDENABLE(portp->brdnr, portp->pagenr);
3234         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3235         msvr1 = stl_cd1400getreg(portp, MSVR1);
3236         msvr2 = stl_cd1400getreg(portp, MSVR2);
3237         BRDDISABLE(portp->brdnr);
3238         crit_exit();
3239
3240         sigs = 0;
3241         sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3242         sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3243         sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3244         sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3245 #if 0
3246         sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3247         sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3248 #else
3249         sigs |= TIOCM_DSR;
3250 #endif
3251         return(sigs);
3252 }
3253
3254 /*****************************************************************************/
3255
3256 /*
3257  *      Enable or disable the Transmitter and/or Receiver.
3258  */
3259
3260 static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
3261 {
3262         unsigned char   ccr;
3263
3264 #if STLDEBUG
3265         kprintf("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3266                 (int) portp, rx, tx);
3267 #endif
3268
3269         ccr = 0;
3270         if (tx == 0)
3271                 ccr |= CCR_TXDISABLE;
3272         else if (tx > 0)
3273                 ccr |= CCR_TXENABLE;
3274         if (rx == 0)
3275                 ccr |= CCR_RXDISABLE;
3276         else if (rx > 0)
3277                 ccr |= CCR_RXENABLE;
3278
3279         crit_enter();
3280         BRDENABLE(portp->brdnr, portp->pagenr);
3281         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3282         stl_cd1400ccrwait(portp);
3283         stl_cd1400setreg(portp, CCR, ccr);
3284         stl_cd1400ccrwait(portp);
3285         BRDDISABLE(portp->brdnr);
3286         crit_exit();
3287 }
3288
3289 /*****************************************************************************/
3290
3291 /*
3292  *      Start or stop the Transmitter and/or Receiver.
3293  */
3294
3295 static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
3296 {
3297         unsigned char   sreron, sreroff;
3298
3299 #if STLDEBUG
3300         kprintf("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
3301                 (int) portp, rx, tx);
3302 #endif
3303
3304         sreron = 0;
3305         sreroff = 0;
3306         if (tx == 0)
3307                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3308         else if (tx == 1)
3309                 sreron |= SRER_TXDATA;
3310         else if (tx >= 2)
3311                 sreron |= SRER_TXEMPTY;
3312         if (rx == 0)
3313                 sreroff |= SRER_RXDATA;
3314         else if (rx > 0)
3315                 sreron |= SRER_RXDATA;
3316
3317         crit_enter();
3318         BRDENABLE(portp->brdnr, portp->pagenr);
3319         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3320         stl_cd1400setreg(portp, SRER,
3321                 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3322         BRDDISABLE(portp->brdnr);
3323         if (tx > 0) {
3324                 portp->state |= ASY_TXBUSY;
3325                 portp->tty.t_state |= TS_BUSY;
3326         }
3327         crit_exit();
3328 }
3329
3330 /*****************************************************************************/
3331
3332 /*
3333  *      Disable all interrupts from this port.
3334  */
3335
3336 static void stl_cd1400disableintrs(stlport_t *portp)
3337 {
3338
3339 #if STLDEBUG
3340         kprintf("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
3341 #endif
3342
3343         crit_enter();
3344         BRDENABLE(portp->brdnr, portp->pagenr);
3345         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3346         stl_cd1400setreg(portp, SRER, 0);
3347         BRDDISABLE(portp->brdnr);
3348         crit_exit();
3349 }
3350
3351 /*****************************************************************************/
3352
3353 static void stl_cd1400sendbreak(stlport_t *portp, long len)
3354 {
3355
3356 #if STLDEBUG
3357         kprintf("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp,
3358                 (int) len);
3359 #endif
3360
3361         crit_enter();
3362         BRDENABLE(portp->brdnr, portp->pagenr);
3363         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3364         stl_cd1400setreg(portp, COR2,
3365                 (stl_cd1400getreg(portp, COR2) | COR2_ETC));
3366         stl_cd1400setreg(portp, SRER,
3367                 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3368                 SRER_TXEMPTY));
3369         BRDDISABLE(portp->brdnr);
3370         if (len > 0) {
3371                 len = len / 5;
3372                 portp->brklen = (len > 255) ? 255 : len;
3373         } else {
3374                 portp->brklen = len;
3375         }
3376         crit_exit();
3377         portp->stats.txbreaks++;
3378 }
3379
3380 /*****************************************************************************/
3381
3382 /*
3383  *      Try and find and initialize all the ports on a panel. We don't care
3384  *      what sort of board these ports are on - since the port io registers
3385  *      are almost identical when dealing with ports.
3386  */
3387
3388 static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3389 {
3390 #if STLDEBUG
3391         kprintf("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
3392                 (int) brdp, (int) panelp, (int) portp);
3393 #endif
3394
3395         if ((brdp == NULL) || (panelp == NULL) ||
3396             (portp == NULL))
3397                 return;
3398
3399         portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
3400                 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
3401         portp->uartaddr = (portp->portnr & 0x04) << 5;
3402         portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
3403
3404         BRDENABLE(portp->brdnr, portp->pagenr);
3405         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3406         stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
3407         portp->hwid = stl_cd1400getreg(portp, GFRCR);
3408         BRDDISABLE(portp->brdnr);
3409 }
3410
3411 /*****************************************************************************/
3412
3413 /*
3414  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3415  *      these ports are on - since the port io registers are almost
3416  *      identical when dealing with ports.
3417  */
3418
3419 static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3420 {
3421         unsigned int    gfrcr;
3422         int             chipmask, i, j;
3423         int             nrchips, uartaddr, ioaddr;
3424
3425 #if STLDEBUG
3426         kprintf("stl_cd1400panelinit(brdp=%x,panelp=%x)\n", (int) brdp,
3427                 (int) panelp);
3428 #endif
3429
3430         BRDENABLE(panelp->brdnr, panelp->pagenr);
3431
3432 /*
3433  *      Check that each chip is present and started up OK.
3434  */
3435         chipmask = 0;
3436         nrchips = panelp->nrports / CD1400_PORTS;
3437         for (i = 0; (i < nrchips); i++) {
3438                 if (brdp->brdtype == BRD_ECHPCI) {
3439                         outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
3440                         ioaddr = panelp->iobase;
3441                 } else {
3442                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
3443                 }
3444                 uartaddr = (i & 0x01) ? 0x080 : 0;
3445                 outb(ioaddr, (GFRCR + uartaddr));
3446                 outb((ioaddr + EREG_DATA), 0);
3447                 outb(ioaddr, (CCR + uartaddr));
3448                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
3449                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
3450                 outb(ioaddr, (GFRCR + uartaddr));
3451                 for (j = 0; (j < CCR_MAXWAIT); j++) {
3452                         if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
3453                                 break;
3454                 }
3455                 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
3456                         kprintf("STALLION: cd1400 not responding, "
3457                                 "board=%d panel=%d chip=%d\n", panelp->brdnr,
3458                                 panelp->panelnr, i);
3459                         continue;
3460                 }
3461                 chipmask |= (0x1 << i);
3462                 outb(ioaddr, (PPR + uartaddr));
3463                 outb((ioaddr + EREG_DATA), PPR_SCALAR);
3464         }
3465
3466
3467         BRDDISABLE(panelp->brdnr);
3468         return(chipmask);
3469 }
3470
3471 /*****************************************************************************/
3472 /*                      SC26198 HARDWARE FUNCTIONS                           */
3473 /*****************************************************************************/
3474
3475 /*
3476  *      These functions get/set/update the registers of the sc26198 UARTs.
3477  *      Access to the sc26198 registers is via an address/data io port pair.
3478  *      (Maybe should make this inline...)
3479  */
3480
3481 static int stl_sc26198getreg(stlport_t *portp, int regnr)
3482 {
3483         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3484         return(inb(portp->ioaddr + XP_DATA));
3485 }
3486
3487 static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
3488 {
3489         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3490         outb((portp->ioaddr + XP_DATA), value);
3491 }
3492
3493 static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
3494 {
3495         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3496         if (inb(portp->ioaddr + XP_DATA) != value) {
3497                 outb((portp->ioaddr + XP_DATA), value);
3498                 return(1);
3499         }
3500         return(0);
3501 }
3502
3503 /*****************************************************************************/
3504
3505 /*
3506  *      Functions to get and set the sc26198 global registers.
3507  */
3508
3509 static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
3510 {
3511         outb((portp->ioaddr + XP_ADDR), regnr);
3512         return(inb(portp->ioaddr + XP_DATA));
3513 }
3514
3515 #if 0
3516 static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
3517 {
3518         outb((portp->ioaddr + XP_ADDR), regnr);
3519         outb((portp->ioaddr + XP_DATA), value);
3520 }
3521 #endif
3522
3523 /*****************************************************************************/
3524
3525 /*
3526  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3527  *      these ports are on - since the port io registers are almost
3528  *      identical when dealing with ports.
3529  */
3530
3531 static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3532 {
3533         int     chipmask, i;
3534         int     nrchips, ioaddr;
3535
3536 #if STLDEBUG
3537         kprintf("stl_sc26198panelinit(brdp=%x,panelp=%x)\n", (int) brdp,
3538                 (int) panelp);
3539 #endif
3540
3541         BRDENABLE(panelp->brdnr, panelp->pagenr);
3542
3543 /*
3544  *      Check that each chip is present and started up OK.
3545  */
3546         chipmask = 0;
3547         nrchips = (panelp->nrports + 4) / SC26198_PORTS;
3548         if (brdp->brdtype == BRD_ECHPCI)
3549                 outb(brdp->ioctrl, panelp->pagenr);
3550
3551         for (i = 0; (i < nrchips); i++) {
3552                 ioaddr = panelp->iobase + (i * 4); 
3553                 outb((ioaddr + XP_ADDR), SCCR);
3554                 outb((ioaddr + XP_DATA), CR_RESETALL);
3555                 outb((ioaddr + XP_ADDR), TSTR);
3556                 if (inb(ioaddr + XP_DATA) != 0) {
3557                         kprintf("STALLION: sc26198 not responding, "
3558                                 "board=%d panel=%d chip=%d\n", panelp->brdnr,
3559                                 panelp->panelnr, i);
3560                         continue;
3561                 }
3562                 chipmask |= (0x1 << i);
3563                 outb((ioaddr + XP_ADDR), GCCR);
3564                 outb((ioaddr + XP_DATA), GCCR_IVRTYPCHANACK);
3565                 outb((ioaddr + XP_ADDR), WDTRCR);
3566                 outb((ioaddr + XP_DATA), 0xff);
3567         }
3568
3569         BRDDISABLE(panelp->brdnr);
3570         return(chipmask);
3571 }
3572
3573 /*****************************************************************************/
3574
3575 /*
3576  *      Initialize hardware specific port registers.
3577  */
3578
3579 static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3580 {
3581 #if STLDEBUG
3582         kprintf("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
3583                 (int) brdp, (int) panelp, (int) portp);
3584 #endif
3585
3586         if ((brdp == NULL) || (panelp == NULL) ||
3587             (portp == NULL))
3588                 return;
3589
3590         portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
3591         portp->uartaddr = (portp->portnr & 0x07) << 4;
3592         portp->pagenr = panelp->pagenr;
3593         portp->hwid = 0x1;
3594
3595         BRDENABLE(portp->brdnr, portp->pagenr);
3596         stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
3597         BRDDISABLE(portp->brdnr);
3598 }
3599
3600 /*****************************************************************************/
3601
3602 /*
3603  *      Set up the sc26198 registers for a port based on the termios port
3604  *      settings.
3605  */
3606
3607 static int stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
3608 {
3609         unsigned char   mr0, mr1, mr2, clk;
3610         unsigned char   imron, imroff, iopr, ipr;
3611
3612 #if STLDEBUG
3613         kprintf("stl_sc26198setport(portp=%x,tiosp=%x): brdnr=%d portnr=%d\n",
3614                 (int) portp, (int) tiosp, portp->brdnr, portp->portnr);
3615 #endif
3616
3617         mr0 = 0;
3618         mr1 = 0;
3619         mr2 = 0;
3620         clk = 0;
3621         iopr = 0;
3622         imron = 0;
3623         imroff = 0;
3624
3625 /*
3626  *      Set up the RX char ignore mask with those RX error types we
3627  *      can ignore.
3628  */
3629         portp->rxignoremsk = 0;
3630         if (tiosp->c_iflag & IGNPAR)
3631                 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
3632                         SR_RXOVERRUN);
3633         if (tiosp->c_iflag & IGNBRK)
3634                 portp->rxignoremsk |= SR_RXBREAK;
3635
3636         portp->rxmarkmsk = SR_RXOVERRUN;
3637         if (tiosp->c_iflag & (INPCK | PARMRK))
3638                 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
3639         if (tiosp->c_iflag & BRKINT)
3640                 portp->rxmarkmsk |= SR_RXBREAK;
3641
3642 /*
3643  *      Go through the char size, parity and stop bits and set all the
3644  *      option registers appropriately.
3645  */
3646         switch (tiosp->c_cflag & CSIZE) {
3647         case CS5:
3648                 mr1 |= MR1_CS5;
3649                 break;
3650         case CS6:
3651                 mr1 |= MR1_CS6;
3652                 break;
3653         case CS7:
3654                 mr1 |= MR1_CS7;
3655                 break;
3656         default:
3657                 mr1 |= MR1_CS8;
3658                 break;
3659         }
3660
3661         if (tiosp->c_cflag & CSTOPB)
3662                 mr2 |= MR2_STOP2;
3663         else
3664                 mr2 |= MR2_STOP1;
3665
3666         if (tiosp->c_cflag & PARENB) {
3667                 if (tiosp->c_cflag & PARODD)
3668                         mr1 |= (MR1_PARENB | MR1_PARODD);
3669                 else
3670                         mr1 |= (MR1_PARENB | MR1_PAREVEN);
3671         } else {
3672                 mr1 |= MR1_PARNONE;
3673         }
3674
3675         mr1 |= MR1_ERRBLOCK;
3676
3677 /*
3678  *      Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
3679  *      space for hardware flow control and the like. This should be set to
3680  *      VMIN.
3681  */
3682         mr2 |= MR2_RXFIFOHALF;
3683
3684 /*
3685  *      Calculate the baud rate timers. For now we will just assume that
3686  *      the input and output baud are the same. The sc26198 has a fixed
3687  *      baud rate table, so only discrete baud rates possible.
3688  */
3689         if (tiosp->c_ispeed == 0)
3690                 tiosp->c_ispeed = tiosp->c_ospeed;
3691         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > SC26198_MAXBAUD))
3692                 return(EINVAL);
3693
3694         if (tiosp->c_ospeed > 0) {
3695                 for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
3696                         if (tiosp->c_ospeed <= sc26198_baudtable[clk])
3697                                 break;
3698                 }
3699         }
3700
3701 /*
3702  *      Check what form of modem signaling is required and set it up.
3703  */
3704         if ((tiosp->c_cflag & CLOCAL) == 0) {
3705                 iopr |= IOPR_DCDCOS;
3706                 imron |= IR_IOPORT;
3707         }
3708
3709 /*
3710  *      Setup sc26198 enhanced modes if we can. In particular we want to
3711  *      handle as much of the flow control as possible automatically. As
3712  *      well as saving a few CPU cycles it will also greatly improve flow
3713  *      control reliability.
3714  */
3715         if (tiosp->c_iflag & IXON) {
3716                 mr0 |= MR0_SWFTX | MR0_SWFT;
3717                 imron |= IR_XONXOFF;
3718         } else {
3719                 imroff |= IR_XONXOFF;
3720         }
3721 #if 0
3722         if (tiosp->c_iflag & IXOFF)
3723                 mr0 |= MR0_SWFRX;
3724 #endif
3725
3726         if (tiosp->c_cflag & CCTS_OFLOW)
3727                 mr2 |= MR2_AUTOCTS;
3728         if (tiosp->c_cflag & CRTS_IFLOW)
3729                 mr1 |= MR1_AUTORTS;
3730
3731 /*
3732  *      All sc26198 register values calculated so go through and set
3733  *      them all up.
3734  */
3735
3736 #if STLDEBUG
3737         kprintf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
3738                 portp->panelnr, portp->brdnr);
3739         kprintf("    mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
3740         kprintf("    iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
3741         kprintf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
3742                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3743                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3744 #endif
3745
3746         crit_enter();
3747         BRDENABLE(portp->brdnr, portp->pagenr);
3748         stl_sc26198setreg(portp, IMR, 0);
3749         stl_sc26198updatereg(portp, MR0, mr0);
3750         stl_sc26198updatereg(portp, MR1, mr1);
3751         stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
3752         stl_sc26198updatereg(portp, MR2, mr2);
3753         iopr = (stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr;
3754         if (tiosp->c_ospeed == 0) {
3755                 iopr &= ~IPR_DTR;
3756         } else {
3757                 iopr |= IPR_DTR;
3758                 stl_sc26198setreg(portp, TXCSR, clk);
3759                 stl_sc26198setreg(portp, RXCSR, clk);
3760         }
3761         stl_sc26198updatereg(portp, IOPIOR, iopr);
3762         stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
3763         stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
3764         ipr = stl_sc26198getreg(portp, IPR);
3765         if (ipr & IPR_DCD)
3766                 portp->sigs &= ~TIOCM_CD;
3767         else
3768                 portp->sigs |= TIOCM_CD;
3769         portp->imr = (portp->imr & ~imroff) | imron;
3770         stl_sc26198setreg(portp, IMR, portp->imr);
3771         BRDDISABLE(portp->brdnr);
3772         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
3773         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
3774         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
3775         stl_ttyoptim(portp, tiosp);
3776         crit_exit();
3777
3778         return(0);
3779 }
3780
3781 /*****************************************************************************/
3782
3783 /*
3784  *      Set the state of the DTR and RTS signals.
3785  */
3786
3787 static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
3788 {
3789         unsigned char   iopioron, iopioroff;
3790
3791 #if STLDEBUG
3792         kprintf("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
3793                 (int) portp, dtr, rts);
3794 #endif
3795
3796         iopioron = 0;
3797         iopioroff = 0;
3798         if (dtr == 0)
3799                 iopioroff |= IPR_DTR;
3800         else if (dtr > 0)
3801                 iopioron |= IPR_DTR;
3802         if (rts == 0)
3803                 iopioroff |= IPR_RTS;
3804         else if (rts > 0)
3805                 iopioron |= IPR_RTS;
3806
3807         crit_enter();
3808         BRDENABLE(portp->brdnr, portp->pagenr);
3809         if ((rts >= 0) && (portp->tty.t_cflag & CRTS_IFLOW)) {
3810                 if (rts == 0) {
3811                         stl_sc26198setreg(portp, MR1,
3812                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
3813                         portp->stats.rxrtsoff++;
3814                 } else {
3815                         stl_sc26198setreg(portp, MR1,
3816                                 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
3817                         portp->stats.rxrtson++;
3818                 }
3819         }
3820         stl_sc26198setreg(portp, IOPIOR,
3821                 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
3822         BRDDISABLE(portp->brdnr);
3823         crit_exit();
3824 }
3825
3826 /*****************************************************************************/
3827
3828 /*
3829  *      Return the state of the signals.
3830  */
3831
3832 static int stl_sc26198getsignals(stlport_t *portp)
3833 {
3834         unsigned char   ipr;
3835         int             sigs;
3836
3837 #if STLDEBUG
3838         kprintf("stl_sc26198getsignals(portp=%x)\n", (int) portp);
3839 #endif
3840
3841         crit_enter();
3842         BRDENABLE(portp->brdnr, portp->pagenr);
3843         ipr = stl_sc26198getreg(portp, IPR);
3844         BRDDISABLE(portp->brdnr);
3845         crit_exit();
3846
3847         sigs = TIOCM_DSR;
3848         sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
3849         sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
3850         sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
3851         sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
3852         return(sigs);
3853 }
3854
3855 /*****************************************************************************/
3856
3857 /*
3858  *      Enable/Disable the Transmitter and/or Receiver.
3859  */
3860
3861 static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
3862 {
3863         unsigned char   ccr;
3864
3865 #if STLDEBUG
3866         kprintf("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3867                 (int) portp, rx, tx);
3868 #endif
3869
3870         ccr = portp->crenable;
3871         if (tx == 0)
3872                 ccr &= ~CR_TXENABLE;
3873         else if (tx > 0)
3874                 ccr |= CR_TXENABLE;
3875         if (rx == 0)
3876                 ccr &= ~CR_RXENABLE;
3877         else if (rx > 0)
3878                 ccr |= CR_RXENABLE;
3879
3880         crit_enter();
3881         BRDENABLE(portp->brdnr, portp->pagenr);
3882         stl_sc26198setreg(portp, SCCR, ccr);
3883         BRDDISABLE(portp->brdnr);
3884         portp->crenable = ccr;
3885         crit_exit();
3886 }
3887
3888 /*****************************************************************************/
3889
3890 /*
3891  *      Start/stop the Transmitter and/or Receiver.
3892  */
3893
3894 static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
3895 {
3896         unsigned char   imr;
3897
3898 #if STLDEBUG
3899         kprintf("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
3900                 (int) portp, rx, tx);
3901 #endif
3902
3903         imr = portp->imr;
3904         if (tx == 0)
3905                 imr &= ~IR_TXRDY;
3906         else if (tx == 1)
3907                 imr |= IR_TXRDY;
3908         if (rx == 0)
3909                 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
3910         else if (rx > 0)
3911                 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
3912
3913         crit_enter();
3914         BRDENABLE(portp->brdnr, portp->pagenr);
3915         stl_sc26198setreg(portp, IMR, imr);
3916         BRDDISABLE(portp->brdnr);
3917         portp->imr = imr;
3918         if (tx > 0) {
3919                 portp->state |= ASY_TXBUSY;
3920                 portp->tty.t_state |= TS_BUSY;
3921         }
3922         crit_exit();
3923 }
3924
3925 /*****************************************************************************/
3926
3927 /*
3928  *      Disable all interrupts from this port.
3929  */
3930
3931 static void stl_sc26198disableintrs(stlport_t *portp)
3932 {
3933
3934 #if STLDEBUG
3935         kprintf("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
3936 #endif
3937
3938         crit_enter();
3939         BRDENABLE(portp->brdnr, portp->pagenr);
3940         portp->imr = 0;
3941         stl_sc26198setreg(portp, IMR, 0);
3942         BRDDISABLE(portp->brdnr);
3943         crit_exit();
3944 }
3945
3946 /*****************************************************************************/
3947
3948 static void stl_sc26198sendbreak(stlport_t *portp, long len)
3949 {
3950
3951 #if STLDEBUG
3952         kprintf("stl_sc26198sendbreak(portp=%x,len=%d)\n",
3953                 (int) portp, (int) len);
3954 #endif
3955
3956         crit_enter();
3957         BRDENABLE(portp->brdnr, portp->pagenr);
3958         if (len == -1) {
3959                 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
3960                 portp->stats.txbreaks++;
3961         } else {
3962                 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
3963         }
3964         BRDDISABLE(portp->brdnr);
3965         crit_exit();
3966 }
3967
3968 /*****************************************************************************/
3969
3970 /*
3971  *      Take flow control actions...
3972  */
3973
3974 static void stl_sc26198sendflow(stlport_t *portp, int hw, int sw)
3975 {
3976         unsigned char   mr0;
3977
3978 #if STLDEBUG
3979         kprintf("stl_sc26198sendflow(portp=%x,hw=%d,sw=%d)\n",
3980                 (int) portp, hw, sw);
3981 #endif
3982
3983         if (portp == NULL)
3984                 return;
3985
3986         crit_enter();
3987         BRDENABLE(portp->brdnr, portp->pagenr);
3988
3989         if (sw >= 0) {
3990                 mr0 = stl_sc26198getreg(portp, MR0);
3991                 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
3992                 if (sw > 0) {
3993                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
3994                         mr0 &= ~MR0_SWFRX;
3995                         portp->stats.rxxoff++;
3996                 } else {
3997                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
3998                         mr0 |= MR0_SWFRX;
3999                         portp->stats.rxxon++;
4000                 }
4001                 stl_sc26198wait(portp);
4002                 stl_sc26198setreg(portp, MR0, mr0);
4003         }
4004
4005         if (hw == 0) {
4006                 portp->state |= ASY_RTSFLOW;
4007                 stl_sc26198setreg(portp, MR1,
4008                         (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4009                 stl_sc26198setreg(portp, IOPIOR,
4010                         (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4011                 portp->stats.rxrtsoff++;
4012         } else if (hw > 0) {
4013                 portp->state &= ~ASY_RTSFLOW;
4014                 stl_sc26198setreg(portp, MR1,
4015                         (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4016                 stl_sc26198setreg(portp, IOPIOR,
4017                         (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4018                 portp->stats.rxrtson++;
4019         }
4020
4021         BRDDISABLE(portp->brdnr);
4022         crit_exit();
4023 }
4024
4025 /*****************************************************************************/
4026
4027 /*
4028  *      Return the current state of data flow on this port. This is only
4029  *      really interresting when determining if data has fully completed
4030  *      transmission or not... The sc26198 interrupt scheme cannot
4031  *      determine when all data has actually drained, so we need to
4032  *      check the port statusy register to be sure.
4033  */
4034
4035 static int stl_sc26198datastate(stlport_t *portp)
4036 {
4037         unsigned char   sr;
4038
4039 #if STLDEBUG
4040         kprintf("stl_sc26198datastate(portp=%x)\n", (int) portp);
4041 #endif
4042
4043         if (portp == NULL)
4044                 return(0);
4045         if (portp->state & ASY_TXBUSY) 
4046                 return(1);
4047
4048         crit_enter();
4049         BRDENABLE(portp->brdnr, portp->pagenr);
4050         sr = stl_sc26198getreg(portp, SR);
4051         BRDDISABLE(portp->brdnr);
4052         crit_exit();
4053
4054         return((sr & SR_TXEMPTY) ? 0 : 1);
4055 }
4056
4057 /*****************************************************************************/
4058
4059 static void stl_sc26198flush(stlport_t *portp, int flag)
4060 {
4061
4062 #if STLDEBUG
4063         kprintf("stl_sc26198flush(portp=%x,flag=%x)\n", (int) portp, flag);
4064 #endif
4065
4066         if (portp == NULL)
4067                 return;
4068
4069         crit_enter();
4070         BRDENABLE(portp->brdnr, portp->pagenr);
4071         if (flag & FWRITE) {
4072                 stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4073                 stl_sc26198setreg(portp, SCCR, portp->crenable);
4074         }
4075         if (flag & FREAD) {
4076                 while (stl_sc26198getreg(portp, SR) & SR_RXRDY)
4077                         stl_sc26198getreg(portp, RXFIFO);
4078         }
4079         BRDDISABLE(portp->brdnr);
4080         crit_exit();
4081 }
4082
4083 /*****************************************************************************/
4084
4085 /*
4086  *      If we are TX flow controlled and in IXANY mode then we may
4087  *      need to unflow control here. We gotta do this because of the
4088  *      automatic flow control modes of the sc26198 - which downs't
4089  *      support any concept of an IXANY mode.
4090  */
4091
4092 static void stl_sc26198txunflow(stlport_t *portp)
4093 {
4094         unsigned char   mr0;
4095
4096         mr0 = stl_sc26198getreg(portp, MR0);
4097         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4098         stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4099         stl_sc26198setreg(portp, MR0, mr0);
4100         portp->state &= ~ASY_TXFLOWED;
4101 }
4102
4103 /*****************************************************************************/
4104
4105 /*
4106  *      Delay for a small amount of time, to give the sc26198 a chance
4107  *      to process a command...
4108  */
4109
4110 static void stl_sc26198wait(stlport_t *portp)
4111 {
4112         int     i;
4113
4114 #if STLDEBUG
4115         kprintf("stl_sc26198wait(portp=%x)\n", (int) portp);
4116 #endif
4117
4118         if (portp == NULL)
4119                 return;
4120
4121         for (i = 0; (i < 20); i++)
4122                 stl_sc26198getglobreg(portp, TSTR);
4123 }
4124
4125 /*****************************************************************************/
4126
4127 /*
4128  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
4129  *      chars is pretty simple, stuff as many as possible from the TX buffer
4130  *      into the sc26198 FIFO.
4131  */
4132
4133 static __inline void stl_sc26198txisr(stlport_t *portp)
4134 {
4135         unsigned int    ioaddr;
4136         unsigned char   mr0;
4137         char            *head, *tail;
4138         int             len, stlen;
4139
4140 #if STLDEBUG
4141         kprintf("stl_sc26198txisr(portp=%x)\n", (int) portp);
4142 #endif
4143
4144         ioaddr = portp->ioaddr;
4145
4146         head = portp->tx.head;
4147         tail = portp->tx.tail;
4148         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4149         if ((len == 0) || ((len < STL_TXBUFLOW) &&
4150             ((portp->state & ASY_TXLOW) == 0))) {
4151                 portp->state |= ASY_TXLOW;
4152                 stl_dotimeout();
4153         }
4154
4155         if (len == 0) {
4156                 outb((ioaddr + XP_ADDR), (MR0 | portp->uartaddr));
4157                 mr0 = inb(ioaddr + XP_DATA);
4158                 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4159                         portp->imr &= ~IR_TXRDY;
4160                         outb((ioaddr + XP_ADDR), (IMR | portp->uartaddr));
4161                         outb((ioaddr + XP_DATA), portp->imr);
4162                         portp->state |= ASY_TXEMPTY;
4163                         portp->state &= ~ASY_TXBUSY;
4164                 } else {
4165                         mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4166                         outb((ioaddr + XP_DATA), mr0);
4167                 }
4168         } else {
4169                 len = MIN(len, SC26198_TXFIFOSIZE);
4170                 portp->stats.txtotal += len;
4171                 stlen = MIN(len, (portp->tx.endbuf - tail));
4172                 outb((ioaddr + XP_ADDR), GTXFIFO);
4173                 outsb((ioaddr + XP_DATA), tail, stlen);
4174                 len -= stlen;
4175                 tail += stlen;
4176                 if (tail >= portp->tx.endbuf)
4177                         tail = portp->tx.buf;
4178                 if (len > 0) {
4179                         outsb((ioaddr + XP_DATA), tail, len);
4180                         tail += len;
4181                 }
4182                 portp->tx.tail = tail;
4183         }
4184 }
4185
4186 /*****************************************************************************/
4187
4188 /*
4189  *      Receive character interrupt handler. Determine if we have good chars
4190  *      or bad chars and then process appropriately. Good chars are easy
4191  *      just shove the lot into the RX buffer and set all status byte to 0.
4192  *      If a bad RX char then process as required. This routine needs to be
4193  *      fast!
4194  */
4195
4196 static __inline void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
4197 {
4198 #if STLDEBUG
4199         kprintf("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
4200 #endif
4201
4202         if ((iack & IVR_TYPEMASK) == IVR_RXDATA)
4203                 stl_sc26198rxgoodchars(portp);
4204         else
4205                 stl_sc26198rxbadchars(portp);
4206
4207 /*
4208  *      If we are TX flow controlled and in IXANY mode then we may need
4209  *      to unflow control here. We gotta do this because of the automatic
4210  *      flow control modes of the sc26198.
4211  */
4212         if ((portp->state & ASY_TXFLOWED) && (portp->tty.t_iflag & IXANY))
4213                 stl_sc26198txunflow(portp);
4214 }
4215
4216 /*****************************************************************************/
4217
4218 /*
4219  *      Process the good received characters from RX FIFO.
4220  */
4221
4222 static void stl_sc26198rxgoodchars(stlport_t *portp)
4223 {
4224         unsigned int    ioaddr, len, buflen, stlen;
4225         char            *head, *tail;
4226
4227 #if STLDEBUG
4228         kprintf("stl_sc26198rxgoodchars(port=%x)\n", (int) portp);
4229 #endif
4230
4231         ioaddr = portp->ioaddr;
4232
4233 /*
4234  *      First up, calculate how much room there is in the RX ring queue.
4235  *      We also want to keep track of the longest possible copy length,
4236  *      this has to allow for the wrapping of the ring queue.
4237  */
4238         head = portp->rx.head;
4239         tail = portp->rx.tail;
4240         if (head >= tail) {
4241                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
4242                 stlen = portp->rx.endbuf - head;
4243         } else {
4244                 buflen = tail - head - 1;
4245                 stlen = buflen;
4246         }
4247
4248 /*
4249  *      Check if the input buffer is near full. If so then we should take
4250  *      some flow control action... It is very easy to do hardware and
4251  *      software flow control from here since we have the port selected on
4252  *      the UART.
4253  */
4254         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
4255                 if (((portp->state & ASY_RTSFLOW) == 0) &&
4256                     (portp->state & ASY_RTSFLOWMODE)) {
4257                         portp->state |= ASY_RTSFLOW;
4258                         stl_sc26198setreg(portp, MR1,
4259                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4260                         stl_sc26198setreg(portp, IOPIOR,
4261                                 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4262                         portp->stats.rxrtsoff++;
4263                 }
4264         }
4265
4266 /*
4267  *      OK we are set, process good data... If the RX ring queue is full
4268  *      just chuck the chars - don't leave them in the UART.
4269  */
4270         outb((ioaddr + XP_ADDR), GIBCR);
4271         len = inb(ioaddr + XP_DATA) + 1;
4272         if (buflen == 0) {
4273                 outb((ioaddr + XP_ADDR), GRXFIFO);
4274                 insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
4275                 portp->stats.rxlost += len;
4276                 portp->stats.rxtotal += len;
4277         } else {
4278                 len = MIN(len, buflen);
4279                 portp->stats.rxtotal += len;
4280                 stlen = MIN(len, stlen);
4281                 if (len > 0) {
4282                         outb((ioaddr + XP_ADDR), GRXFIFO);
4283                         insb((ioaddr + XP_DATA), head, stlen);
4284                         head += stlen;
4285                         if (head >= portp->rx.endbuf) {
4286                                 head = portp->rx.buf;
4287                                 len -= stlen;
4288                                 insb((ioaddr + XP_DATA), head, len);
4289                                 head += len;
4290                         }
4291                 }
4292         }
4293
4294         portp->rx.head = head;
4295         portp->state |= ASY_RXDATA;
4296         stl_dotimeout();
4297 }
4298
4299 /*****************************************************************************/
4300
4301 /*
4302  *      Process all characters in the RX FIFO of the UART. Check all char
4303  *      status bytes as well, and process as required. We need to check
4304  *      all bytes in the FIFO, in case some more enter the FIFO while we
4305  *      are here. To get the exact character error type we need to switch
4306  *      into CHAR error mode (that is why we need to make sure we empty
4307  *      the FIFO).
4308  */
4309
4310 static void stl_sc26198rxbadchars(stlport_t *portp)
4311 {
4312         unsigned char   mr1;
4313         unsigned int    status;
4314         char            *head, *tail;
4315         char            ch;
4316         int             len;
4317
4318 /*
4319  *      First up, calculate how much room there is in the RX ring queue.
4320  *      We also want to keep track of the longest possible copy length,
4321  *      this has to allow for the wrapping of the ring queue.
4322  */
4323         head = portp->rx.head;
4324         tail = portp->rx.tail;
4325         len = (head >= tail) ? (STL_RXBUFSIZE - (head - tail) - 1) :
4326                 (tail - head - 1);
4327
4328 /*
4329  *      To get the precise error type for each character we must switch
4330  *      back into CHAR error mode.
4331  */
4332         mr1 = stl_sc26198getreg(portp, MR1);
4333         stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
4334
4335         while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
4336                 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
4337                 ch = stl_sc26198getreg(portp, RXFIFO);
4338
4339                 if (status & SR_RXBREAK)
4340                         portp->stats.rxbreaks++;
4341                 if (status & SR_RXFRAMING)
4342                         portp->stats.rxframing++;
4343                 if (status & SR_RXPARITY)
4344                         portp->stats.rxparity++;
4345                 if (status & SR_RXOVERRUN)
4346                         portp->stats.rxoverrun++;
4347                 if ((portp->rxignoremsk & status) == 0) {
4348                         if ((portp->tty.t_state & TS_CAN_BYPASS_L_RINT) &&
4349                             ((status & SR_RXFRAMING) ||
4350                             ((status & SR_RXPARITY) &&
4351                             (portp->tty.t_iflag & INPCK))))
4352                                 ch = 0;
4353                         if ((portp->rxmarkmsk & status) == 0)
4354                                 status = 0;
4355                         if (len > 0) {
4356                                 *(head + STL_RXBUFSIZE) = status;
4357                                 *head++ = ch;
4358                                 if (head >= portp->rx.endbuf)
4359                                         head = portp->rx.buf;
4360                                 len--;
4361                         }
4362                 }
4363         }
4364
4365 /*
4366  *      To get correct interrupt class we must switch back into BLOCK
4367  *      error mode.
4368  */
4369         stl_sc26198setreg(portp, MR1, mr1);
4370
4371         portp->rx.head = head;
4372         portp->state |= ASY_RXDATA;
4373         stl_dotimeout();
4374 }
4375
4376 /*****************************************************************************/
4377
4378 /*
4379  *      Other interrupt handler. This includes modem signals, flow
4380  *      control actions, etc.
4381  */
4382
4383 static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
4384 {
4385         unsigned char   cir, ipr, xisr;
4386
4387 #if STLDEBUG
4388         kprintf("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
4389 #endif
4390
4391         cir = stl_sc26198getglobreg(portp, CIR);
4392
4393         switch (cir & CIR_SUBTYPEMASK) {
4394         case CIR_SUBCOS:
4395                 ipr = stl_sc26198getreg(portp, IPR);
4396                 if (ipr & IPR_DCDCHANGE) {
4397                         portp->state |= ASY_DCDCHANGE;
4398                         portp->stats.modem++;
4399                         stl_dotimeout();
4400                 }
4401                 break;
4402         case CIR_SUBXONXOFF:
4403                 xisr = stl_sc26198getreg(portp, XISR);
4404                 if (xisr & XISR_RXXONGOT) {
4405                         portp->state |= ASY_TXFLOWED;
4406                         portp->stats.txxoff++;
4407                 }
4408                 if (xisr & XISR_RXXOFFGOT) {
4409                         portp->state &= ~ASY_TXFLOWED;
4410                         portp->stats.txxon++;
4411                 }
4412                 break;
4413         case CIR_SUBBREAK:
4414                 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
4415                 stl_sc26198rxbadchars(portp);
4416                 break;
4417         default:
4418                 break;
4419         }
4420 }
4421
4422 /*****************************************************************************/
4423
4424 /*
4425  *      Interrupt service routine for sc26198 panels.
4426  */
4427
4428 static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
4429 {
4430         stlport_t       *portp;
4431         unsigned int    iack;
4432
4433 /* 
4434  *      Work around bug in sc26198 chip... Cannot have A6 address
4435  *      line of UART high, else iack will be returned as 0.
4436  */
4437         outb((iobase + 1), 0);
4438
4439         iack = inb(iobase + XP_IACK);
4440 #if STLDEBUG
4441         kprintf("stl_sc26198intr(panelp=%p,iobase=%x): iack=%x\n", panelp, iobase, iack);
4442 #endif
4443         portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4444
4445         if (iack & IVR_RXDATA)
4446                 stl_sc26198rxisr(portp, iack);
4447         else if (iack & IVR_TXDATA)
4448                 stl_sc26198txisr(portp);
4449         else
4450                 stl_sc26198otherisr(portp, iack);
4451 }
4452
4453 /*****************************************************************************/