]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/stallion.c
Use the MI ithread helper functions in the x86 interrupt code.
[FreeBSD/FreeBSD.git] / sys / i386 / isa / 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$
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/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/bus.h>
53 #include <i386/isa/isa_device.h>
54 #include <i386/isa/ic/scd1400.h>
55 #include <machine/comstats.h>
56
57 #include "pci.h"
58 #ifdef COMPILING_LINT
59 #warning "The stallion pci device is broken and not compiled with LINT"
60 #undef NPCI
61 #define NPCI 0
62 #endif
63 #if NPCI > 0
64 #ifndef COMPAT_OLDPCI
65 #error "The stallion pci driver requires the old pci compatibility shims"
66 #endif
67 #include <pci/pcivar.h>
68 #include <pci/pcireg.h>
69 #endif
70
71 /*****************************************************************************/
72
73 /*
74  *      Define the version level of the kernel - so we can compile in the
75  *      appropriate bits of code. By default this will compile for a 2.1
76  *      level kernel.
77  */
78 #define VFREEBSD        220
79
80 #if VFREEBSD >= 220
81 #define STATIC          static
82 #else
83 #define STATIC
84 #endif
85
86 /*****************************************************************************/
87
88 /*
89  *      Define different board types. At the moment I have only declared
90  *      those boards that this driver supports. But I will use the standard
91  *      "assigned" board numbers. In the future this driver will support
92  *      some of the other Stallion boards. Currently supported boards are
93  *      abbreviated as EIO = EasyIO and ECH = EasyConnection 8/32.
94  */
95 #define BRD_EASYIO      20
96 #define BRD_ECH         21
97 #define BRD_ECHMC       22
98 #define BRD_ECHPCI      26
99
100 /*
101  *      When using the BSD "config" stuff there is no easy way to specifiy
102  *      a secondary IO address region. So it is hard wired here. Also the
103  *      shared interrupt information is hard wired here...
104  */
105 static unsigned int     stl_ioshared = 0x280;
106 static unsigned int     stl_irqshared = 0;
107
108 /*****************************************************************************/
109
110 /*
111  *      Define important driver limitations.
112  */
113 #define STL_MAXBRDS             8
114 #define STL_MAXPANELS           4
115 #define STL_PORTSPERPANEL       16
116 #define STL_PORTSPERBRD         64
117
118 /*
119  *      Define the important minor number break down bits. These have been
120  *      chosen to be "compatible" with the standard sio driver minor numbers.
121  *      Extra high bits are used to distinguish between boards.
122  */
123 #define STL_CALLOUTDEV          0x80
124 #define STL_CTRLLOCK            0x40
125 #define STL_CTRLINIT            0x20
126 #define STL_CTRLDEV             (STL_CTRLLOCK | STL_CTRLINIT)
127
128 #define STL_MEMDEV      0x07000000
129
130 #define STL_DEFSPEED    TTYDEF_SPEED
131 #define STL_DEFCFLAG    (CS8 | CREAD | HUPCL)
132
133 /*
134  *      I haven't really decided (or measured) what buffer sizes give
135  *      a good balance between performance and memory usage. These seem
136  *      to work pretty well...
137  */
138 #define STL_RXBUFSIZE           2048
139 #define STL_TXBUFSIZE           2048
140
141 #define STL_TXBUFLOW            (STL_TXBUFSIZE / 4)
142 #define STL_RXBUFHIGH           (3 * STL_RXBUFSIZE / 4)
143
144 /*****************************************************************************/
145
146 /*
147  *      Define our local driver identity first. Set up stuff to deal with
148  *      all the local structures required by a serial tty driver.
149  */
150 static const char       stl_drvname[] = "stl";
151 static const char       stl_longdrvname[] = "Stallion Multiport Serial Driver";
152 static const char       stl_drvversion[] = "1.0.0";
153 static int              stl_brdprobed[STL_MAXBRDS];
154
155 static int              stl_nrbrds = 0;
156 static int              stl_doingtimeout = 0;
157
158 static const char       __file__[] = /*__FILE__*/ "stallion.c";
159
160 /*
161  *      Define global stats structures. Not used often, and can be
162  *      re-used for each stats call.
163  */
164 static combrd_t         stl_brdstats;
165 static comstats_t       stl_comstats;
166
167 /*****************************************************************************/
168
169 /*
170  *      Define a set of structures to hold all the board/panel/port info
171  *      for our ports. These will be dynamically allocated as required.
172  */
173
174 /*
175  *      Define a ring queue structure for each port. This will hold the
176  *      TX data waiting to be output. Characters are fed into this buffer
177  *      from the line discipline (or even direct from user space!) and
178  *      then fed into the UARTs during interrupts. Will use a clasic ring
179  *      queue here for this. The good thing about this type of ring queue
180  *      is that the head and tail pointers can be updated without interrupt
181  *      protection - since "write" code only needs to change the head, and
182  *      interrupt code only needs to change the tail.
183  */
184 typedef struct {
185         char    *buf;
186         char    *endbuf;
187         char    *head;
188         char    *tail;
189 } stlrq_t;
190
191 /*
192  *      Port, panel and board structures to hold status info about each.
193  *      The board structure contains pointers to structures for each panel
194  *      connected to it, and in turn each panel structure contains pointers
195  *      for each port structure for each port on that panel. Note that
196  *      the port structure also contains the board and panel number that it
197  *      is associated with, this makes it (fairly) easy to get back to the
198  *      board/panel info for a port. Also note that the tty struct is at
199  *      the top of the structure, this is important, since the code uses
200  *      this fact to get the port struct pointer from the tty struct
201  *      pointer!
202  */
203 typedef struct {
204         struct tty      tty;
205         int             portnr;
206         int             panelnr;
207         int             brdnr;
208         int             ioaddr;
209         int             uartaddr;
210         int             pagenr;
211         int             callout;
212         int             brklen;
213         int             dtrwait;
214         int             dotimestamp;
215         int             waitopens;
216         int             hotchar;
217         unsigned int    state;
218         unsigned int    hwid;
219         unsigned int    sigs;
220         unsigned int    rxignoremsk;
221         unsigned int    rxmarkmsk;
222         unsigned long   clk;
223         struct termios  initintios;
224         struct termios  initouttios;
225         struct termios  lockintios;
226         struct termios  lockouttios;
227         struct timeval  timestamp;
228         comstats_t      stats;
229         stlrq_t         tx;
230         stlrq_t         rx;
231         stlrq_t         rxstatus;
232 } stlport_t;
233
234 typedef struct {
235         int             panelnr;
236         int             brdnr;
237         int             pagenr;
238         int             nrports;
239         int             iobase;
240         unsigned int    hwid;
241         unsigned int    ackmask;
242         stlport_t       *ports[STL_PORTSPERPANEL];
243 } stlpanel_t;
244
245 typedef struct {
246         int             brdnr;
247         int             brdtype;
248         int             unitid;
249         int             state;
250         int             nrpanels;
251         int             nrports;
252         int             irq;
253         int             irqtype;
254         unsigned int    ioaddr1;
255         unsigned int    ioaddr2;
256         unsigned int    iostatus;
257         unsigned int    ioctrl;
258         unsigned int    ioctrlval;
259         unsigned int    hwid;
260         unsigned long   clk;
261         stlpanel_t      *panels[STL_MAXPANELS];
262         stlport_t       *ports[STL_PORTSPERBRD];
263 } stlbrd_t;
264
265 static stlbrd_t         *stl_brds[STL_MAXBRDS];
266
267 /*
268  *      Per board state flags. Used with the state field of the board struct.
269  *      Not really much here yet!
270  */
271 #define BRD_FOUND       0x1
272
273 /*
274  *      Define the port structure state flags. These set of flags are
275  *      modified at interrupt time - so setting and reseting them needs
276  *      to be atomic.
277  */
278 #define ASY_TXLOW       0x1
279 #define ASY_RXDATA      0x2
280 #define ASY_DCDCHANGE   0x4
281 #define ASY_DTRWAIT     0x8
282 #define ASY_RTSFLOW     0x10
283 #define ASY_RTSFLOWMODE 0x20
284 #define ASY_CTSFLOWMODE 0x40
285
286 #define ASY_ACTIVE      (ASY_TXLOW | ASY_RXDATA | ASY_DCDCHANGE)
287
288 /*
289  *      Define an array of board names as printable strings. Handy for
290  *      referencing boards when printing trace and stuff.
291  */
292 static char     *stl_brdnames[] = {
293         (char *) NULL,
294         (char *) NULL,
295         (char *) NULL,
296         (char *) NULL,
297         (char *) NULL,
298         (char *) NULL,
299         (char *) NULL,
300         (char *) NULL,
301         (char *) NULL,
302         (char *) NULL,
303         (char *) NULL,
304         (char *) NULL,
305         (char *) NULL,
306         (char *) NULL,
307         (char *) NULL,
308         (char *) NULL,
309         (char *) NULL,
310         (char *) NULL,
311         (char *) NULL,
312         (char *) NULL,
313         "EasyIO",
314         "EC8/32-AT",
315         "EC8/32-MC",
316         (char *) NULL,
317         (char *) NULL,
318         (char *) NULL,
319         "EC8/32-PCI",
320 };
321
322 /*****************************************************************************/
323
324 /*
325  *      Hardware ID bits for the EasyIO and ECH boards. These defines apply
326  *      to the directly accessible io ports of these boards (not the cd1400
327  *      uarts - they are in scd1400.h).
328  */
329 #define EIO_8PORTRS     0x04
330 #define EIO_4PORTRS     0x05
331 #define EIO_8PORTDI     0x00
332 #define EIO_8PORTM      0x06
333 #define EIO_IDBITMASK   0x07
334 #define EIO_INTRPEND    0x08
335 #define EIO_INTEDGE     0x00
336 #define EIO_INTLEVEL    0x08
337
338 #define ECH_ID          0xa0
339 #define ECH_IDBITMASK   0xe0
340 #define ECH_BRDENABLE   0x08
341 #define ECH_BRDDISABLE  0x00
342 #define ECH_INTENABLE   0x01
343 #define ECH_INTDISABLE  0x00
344 #define ECH_INTLEVEL    0x02
345 #define ECH_INTEDGE     0x00
346 #define ECH_INTRPEND    0x01
347 #define ECH_BRDRESET    0x01
348
349 #define ECHMC_INTENABLE 0x01
350 #define ECHMC_BRDRESET  0x02
351
352 #define ECH_PNLSTATUS   2
353 #define ECH_PNL16PORT   0x20
354 #define ECH_PNLIDMASK   0x07
355 #define ECH_PNLINTRPEND 0x80
356 #define ECH_ADDR2MASK   0x1e0
357
358 #define EIO_CLK         25000000
359 #define EIO_CLK8M       20000000
360 #define ECH_CLK         EIO_CLK
361
362 /*
363  *      Define the offsets within the register bank for all io registers.
364  *      These io address offsets are common to both the EIO and ECH.
365  */
366 #define EREG_ADDR       0
367 #define EREG_DATA       4
368 #define EREG_RXACK      5
369 #define EREG_TXACK      6
370 #define EREG_MDACK      7
371
372 #define EREG_BANKSIZE   8
373
374 /*
375  *      Define the PCI vendor and device id for ECH8/32-PCI.
376  */
377 #define STL_PCIDEVID    0xd001100b
378
379 /*
380  *      Define the vector mapping bits for the programmable interrupt board
381  *      hardware. These bits encode the interrupt for the board to use - it
382  *      is software selectable (except the EIO-8M).
383  */
384 static unsigned char    stl_vecmap[] = {
385         0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
386         0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
387 };
388
389 /*
390  *      Set up enable and disable macros for the ECH boards. They require
391  *      the secondary io address space to be activated and deactivated.
392  *      This way all ECH boards can share their secondary io region.
393  *      If this is an ECH-PCI board then also need to set the page pointer
394  *      to point to the correct page.
395  */
396 #define BRDENABLE(brdnr,pagenr)                                         \
397         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
398                 outb(stl_brds[(brdnr)]->ioctrl,                         \
399                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE));\
400         else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)              \
401                 outb(stl_brds[(brdnr)]->ioctrl, (pagenr));
402
403 #define BRDDISABLE(brdnr)                                               \
404         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
405                 outb(stl_brds[(brdnr)]->ioctrl,                         \
406                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE));
407
408 /*
409  *      Define the cd1400 baud rate clocks. These are used when calculating
410  *      what clock and divisor to use for the required baud rate. Also
411  *      define the maximum baud rate allowed, and the default base baud.
412  */
413 static int      stl_cd1400clkdivs[] = {
414         CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
415 };
416
417 #define STL_MAXBAUD     230400
418
419 /*****************************************************************************/
420
421 /*
422  *      Define macros to extract a brd and port number from a minor number.
423  *      This uses the extended minor number range in the upper 2 bytes of
424  *      the device number. This gives us plenty of minor numbers to play
425  *      with...
426  */
427 #define MKDEV2BRD(m)    ((minor(m) & 0x00700000) >> 20)
428 #define MKDEV2PORT(m)   ((minor(m) & 0x1f) | ((minor(m) & 0x00010000) >> 11))
429
430 /*
431  *      Define some handy local macros...
432  */
433 #ifndef MIN
434 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
435 #endif
436
437 /*****************************************************************************/
438
439 /*
440  *      Declare all those functions in this driver!  First up is the set of
441  *      externally visible functions.
442  */
443
444 static int      stlprobe(struct isa_device *idp);
445 static int      stlattach(struct isa_device *idp);
446
447 STATIC  d_open_t        stlopen;
448 STATIC  d_close_t       stlclose;
449 STATIC  d_ioctl_t       stlioctl;
450
451 /*
452  *      Internal function prototypes.
453  */
454 static stlport_t *stl_dev2port(dev_t dev);
455 static int      stl_findfreeunit(void);
456 static int      stl_rawopen(stlport_t *portp);
457 static int      stl_rawclose(stlport_t *portp);
458 static int      stl_param(struct tty *tp, struct termios *tiosp);
459 static void     stl_start(struct tty *tp);
460 static void     stl_stop(struct tty *tp, int);
461 static void     stl_ttyoptim(stlport_t *portp, struct termios *tiosp);
462 static void     stl_dotimeout(void);
463 static void     stl_poll(void *arg);
464 static void     stl_rxprocess(stlport_t *portp);
465 static void     stl_dtrwakeup(void *arg);
466 static int      stl_brdinit(stlbrd_t *brdp);
467 static int      stl_initeio(stlbrd_t *brdp);
468 static int      stl_initech(stlbrd_t *brdp);
469 static int      stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
470 static ointhand2_t      stlintr;
471 static __inline void    stl_txisr(stlpanel_t *panelp, int ioaddr);
472 static __inline void    stl_rxisr(stlpanel_t *panelp, int ioaddr);
473 static __inline void    stl_mdmisr(stlpanel_t *panelp, int ioaddr);
474 static void     stl_setreg(stlport_t *portp, int regnr, int value);
475 static int      stl_getreg(stlport_t *portp, int regnr);
476 static int      stl_updatereg(stlport_t *portp, int regnr, int value);
477 static int      stl_getsignals(stlport_t *portp);
478 static void     stl_setsignals(stlport_t *portp, int dtr, int rts);
479 static void     stl_flowcontrol(stlport_t *portp, int hw, int sw);
480 static void     stl_ccrwait(stlport_t *portp);
481 static void     stl_enablerxtx(stlport_t *portp, int rx, int tx);
482 static void     stl_startrxtx(stlport_t *portp, int rx, int tx);
483 static void     stl_disableintrs(stlport_t *portp);
484 static void     stl_sendbreak(stlport_t *portp, long len);
485 static void     stl_flush(stlport_t *portp, int flag);
486 static int      stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data,
487                         int flag, struct proc *p);
488 static int      stl_getbrdstats(caddr_t data);
489 static int      stl_getportstats(stlport_t *portp, caddr_t data);
490 static int      stl_clrportstats(stlport_t *portp, caddr_t data);
491 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
492
493 #if NPCI > 0
494 static const char *stlpciprobe(pcici_t tag, pcidi_t type);
495 static void     stlpciattach(pcici_t tag, int unit);
496 static void     stlpciintr(void * arg);
497 #endif
498
499 /*****************************************************************************/
500
501 /*
502  *      Declare the driver isa structure.
503  */
504 struct isa_driver       stldriver = {
505         INTR_TYPE_TTY,
506         stlprobe,
507         stlattach,
508         "stl"
509 };
510 COMPAT_ISA_DRIVER(stl, stldriver);
511
512 /*****************************************************************************/
513
514 #if NPCI > 0
515
516 /*
517  *      Declare the driver pci structure.
518  */
519 static unsigned long    stl_count;
520
521 static struct pci_device        stlpcidriver = {
522         "stl",
523         stlpciprobe,
524         stlpciattach,
525         &stl_count,
526         NULL,
527 };
528
529 COMPAT_PCI_DRIVER (stlpci, stlpcidriver);
530
531 #endif
532
533 /*****************************************************************************/
534
535 #if VFREEBSD >= 220
536
537 /*
538  *      FreeBSD-2.2+ kernel linkage.
539  */
540
541 #define CDEV_MAJOR      72
542 static struct cdevsw stl_cdevsw = {
543         /* open */      stlopen,
544         /* close */     stlclose,
545         /* read */      ttyread,
546         /* write */     ttywrite,
547         /* ioctl */     stlioctl,
548         /* poll */      ttypoll,
549         /* mmap */      nommap,
550         /* strategy */  nostrategy,
551         /* name */      "stl",
552         /* maj */       CDEV_MAJOR,
553         /* dump */      nodump,
554         /* psize */     nopsize,
555         /* flags */     D_TTY,
556         /* bmaj */      -1
557 };
558
559 static void stl_drvinit(void *unused)
560 {
561
562         cdevsw_add(&stl_cdevsw);
563 }
564
565 SYSINIT(sidev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,stl_drvinit,NULL)
566
567 #endif
568
569 /*****************************************************************************/
570
571 /*
572  *      Probe for some type of EasyIO or EasyConnection 8/32 board at
573  *      the supplied address. All we do is check if we can find the
574  *      board ID for the board... (Note, PCI boards not checked here,
575  *      they are done in the stlpciprobe() routine).
576  */
577
578 static int stlprobe(struct isa_device *idp)
579 {
580         unsigned int    status;
581
582 #if DEBUG
583         printf("stlprobe(idp=%x): unit=%d iobase=%x\n", (int) idp,
584                 idp->id_unit, idp->id_iobase);
585 #endif
586
587         if (idp->id_unit > STL_MAXBRDS)
588                 return(0);
589
590         status = inb(idp->id_iobase + 1);
591         if ((status & ECH_IDBITMASK) == ECH_ID) {
592                 stl_brdprobed[idp->id_unit] = BRD_ECH;
593                 return(1);
594         }
595
596         status = inb(idp->id_iobase + 2);
597         switch (status & EIO_IDBITMASK) {
598         case EIO_8PORTRS:
599         case EIO_8PORTM:
600         case EIO_8PORTDI:
601         case EIO_4PORTRS:
602                 stl_brdprobed[idp->id_unit] = BRD_EASYIO;
603                 return(1);
604         default:
605                 break;
606         }
607         
608         return(0);
609 }
610
611 /*****************************************************************************/
612
613 /*
614  *      Find an available internal board number (unit number). The problem
615  *      is that the same unit numbers can be assigned to different boards
616  *      detected during the ISA and PCI initialization phases.
617  */
618
619 static int stl_findfreeunit()
620 {
621         int     i;
622
623         for (i = 0; (i < STL_MAXBRDS); i++)
624                 if (stl_brds[i] == (stlbrd_t *) NULL)
625                         break;
626         return((i >= STL_MAXBRDS) ? -1 : i);
627 }
628
629 /*****************************************************************************/
630
631 /*
632  *      Allocate resources for and initialize the specified board.
633  */
634
635 static int stlattach(struct isa_device *idp)
636 {
637         stlbrd_t        *brdp;
638
639 #if DEBUG
640         printf("stlattach(idp=%p): unit=%d iobase=%x\n", (void *) idp,
641                 idp->id_unit, idp->id_iobase);
642 #endif
643
644         idp->id_ointr = stlintr;
645
646         brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT | M_ZERO);
647         if (brdp == (stlbrd_t *) NULL) {
648                 printf("STALLION: failed to allocate memory (size=%d)\n",
649                         sizeof(stlbrd_t));
650                 return(0);
651         }
652
653         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
654                 printf("STALLION: too many boards found, max=%d\n",
655                         STL_MAXBRDS);
656                 return(0);
657         }
658         if (brdp->brdnr >= stl_nrbrds)
659                 stl_nrbrds = brdp->brdnr + 1;
660
661         brdp->unitid = idp->id_unit;
662         brdp->brdtype = stl_brdprobed[idp->id_unit];
663         brdp->ioaddr1 = idp->id_iobase;
664         brdp->ioaddr2 = stl_ioshared;
665         brdp->irq = ffs(idp->id_irq) - 1;
666         brdp->irqtype = stl_irqshared;
667         stl_brdinit(brdp);
668
669         return(1);
670 }
671
672 /*****************************************************************************/
673
674 #if NPCI > 0
675
676 /*
677  *      Probe specifically for the PCI boards. We need to be a little
678  *      carefull here, since it looks sort like a Nat Semi IDE chip...
679  */
680
681 static const char *stlpciprobe(pcici_t tag, pcidi_t type)
682 {
683         unsigned long   class;
684
685 #if DEBUG
686         printf("stlpciprobe(tag=%x,type=%x)\n", (int) &tag, (int) type);
687 #endif
688
689         switch (type) {
690         case STL_PCIDEVID:
691                 break;
692         default:
693                 return((char *) NULL);
694         }
695
696         class = pci_conf_read(tag, PCI_CLASS_REG);
697         if ((class & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE)
698                 return((char *) NULL);
699
700         return("Stallion EasyConnection 8/32-PCI");
701 }
702
703 /*****************************************************************************/
704
705 /*
706  *      Allocate resources for and initialize the specified PCI board.
707  */
708
709 void stlpciattach(pcici_t tag, int unit)
710 {
711         stlbrd_t        *brdp;
712
713 #if DEBUG
714         printf("stlpciattach(tag=%x,unit=%x)\n", (int) &tag, unit);
715 #endif
716
717         brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT | M_ZERO);
718         if (brdp == (stlbrd_t *) NULL) {
719                 printf("STALLION: failed to allocate memory (size=%d)\n",
720                         sizeof(stlbrd_t));
721                 return;
722         }
723
724         if ((unit < 0) || (unit > STL_MAXBRDS)) {
725                 printf("STALLION: bad PCI board unit number=%d\n", unit);
726                 return;
727         }
728
729 /*
730  *      Allocate us a new driver unique unit number.
731  */
732         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
733                 printf("STALLION: too many boards found, max=%d\n",
734                         STL_MAXBRDS);
735                 return;
736         }
737         if (brdp->brdnr >= stl_nrbrds)
738                 stl_nrbrds = brdp->brdnr + 1;
739
740         brdp->unitid = 0;
741         brdp->brdtype = BRD_ECHPCI;
742         brdp->ioaddr1 = ((unsigned int) pci_conf_read(tag, 0x14)) & 0xfffc;
743         brdp->ioaddr2 = ((unsigned int) pci_conf_read(tag, 0x10)) & 0xfffc;
744         brdp->irq = ((int) pci_conf_read(tag, 0x3c)) & 0xff;
745         brdp->irqtype = 0;
746         if (pci_map_int(tag, stlpciintr, (void *) NULL, &tty_imask) == 0) {
747                 printf("STALLION: failed to map interrupt irq=%d for unit=%d\n",
748                         brdp->irq, brdp->brdnr);
749                 return;
750         }
751
752 #if 0
753         printf("%s(%d): ECH-PCI iobase=%x iopage=%x irq=%d\n", __file__,                         __LINE__, brdp->ioaddr2, brdp->ioaddr1, brdp->irq);
754 #endif
755         stl_brdinit(brdp);
756 }
757
758 #endif
759
760 /*****************************************************************************/
761
762 STATIC int stlopen(dev_t dev, int flag, int mode, struct proc *p)
763 {
764         struct tty      *tp;
765         stlport_t       *portp;
766         int             error, callout, x;
767
768 #if DEBUG
769         printf("stlopen(dev=%x,flag=%x,mode=%x,p=%x)\n", (int) dev, flag,
770                 mode, (int) p);
771 #endif
772
773 /*
774  *      Firstly check if the supplied device number is a valid device.
775  */
776         if (minor(dev) & STL_MEMDEV)
777                 return(0);
778
779         portp = stl_dev2port(dev);
780         if (portp == (stlport_t *) NULL)
781                 return(ENXIO);
782         tp = &portp->tty;
783         dev->si_tty = tp;
784         callout = minor(dev) & STL_CALLOUTDEV;
785         error = 0;
786
787         x = spltty();
788
789 stlopen_restart:
790 /*
791  *      Wait here for the DTR drop timeout period to expire.
792  */
793         while (portp->state & ASY_DTRWAIT) {
794                 error = tsleep(&portp->dtrwait, (TTIPRI | PCATCH),
795                         "stldtr", 0);
796                 if (error)
797                         goto stlopen_end;
798         }
799         
800 /*
801  *      We have a valid device, so now we check if it is already open.
802  *      If not then initialize the port hardware and set up the tty
803  *      struct as required.
804  */
805         if ((tp->t_state & TS_ISOPEN) == 0) {
806                 tp->t_oproc = stl_start;
807                 tp->t_stop = stl_stop;
808                 tp->t_param = stl_param;
809                 tp->t_dev = dev;
810                 tp->t_termios = callout ? portp->initouttios :
811                         portp->initintios;
812                 stl_rawopen(portp);
813                 if ((portp->sigs & TIOCM_CD) || callout)
814                         (*linesw[tp->t_line].l_modem)(tp, 1);
815         } else {
816                 if (callout) {
817                         if (portp->callout == 0) {
818                                 error = EBUSY;
819                                 goto stlopen_end;
820                         }
821                 } else {
822                         if (portp->callout != 0) {
823                                 if (flag & O_NONBLOCK) {
824                                         error = EBUSY;
825                                         goto stlopen_end;
826                                 }
827                                 error = tsleep(&portp->callout,
828                                         (TTIPRI | PCATCH), "stlcall", 0);
829                                 if (error)
830                                         goto stlopen_end;
831                                 goto stlopen_restart;
832                         }
833                 }
834                 if ((tp->t_state & TS_XCLUDE) &&
835                     suser(p)) {
836                         error = EBUSY;
837                         goto stlopen_end;
838                 }
839         }
840
841 /*
842  *      If this port is not the callout device and we do not have carrier
843  *      then we need to sleep, waiting for it to be asserted.
844  */
845         if (((tp->t_state & TS_CARR_ON) == 0) && !callout &&
846                         ((tp->t_cflag & CLOCAL) == 0) &&
847                         ((flag & O_NONBLOCK) == 0)) {
848                 portp->waitopens++;
849                 error = tsleep(TSA_CARR_ON(tp), (TTIPRI | PCATCH), "stldcd", 0);
850                 portp->waitopens--;
851                 if (error)
852                         goto stlopen_end;
853                 goto stlopen_restart;
854         }
855
856 /*
857  *      Open the line discipline.
858  */
859         error = (*linesw[tp->t_line].l_open)(dev, tp);
860         stl_ttyoptim(portp, &tp->t_termios);
861         if ((tp->t_state & TS_ISOPEN) && callout)
862                 portp->callout = 1;
863
864 /*
865  *      If for any reason we get to here and the port is not actually
866  *      open then close of the physical hardware - no point leaving it
867  *      active when the open failed...
868  */
869 stlopen_end:
870         splx(x);
871         if (((tp->t_state & TS_ISOPEN) == 0) && (portp->waitopens == 0))
872                 stl_rawclose(portp);
873
874         return(error);
875 }
876
877 /*****************************************************************************/
878
879 STATIC int stlclose(dev_t dev, int flag, int mode, struct proc *p)
880 {
881         struct tty      *tp;
882         stlport_t       *portp;
883         int             x;
884
885 #if DEBUG
886         printf("stlclose(dev=%s,flag=%x,mode=%x,p=%p)\n", devtoname(dev),
887                 flag, mode, (void *) p);
888 #endif
889
890         if (minor(dev) & STL_MEMDEV)
891                 return(0);
892
893         portp = stl_dev2port(dev);
894         if (portp == (stlport_t *) NULL)
895                 return(ENXIO);
896         tp = &portp->tty;
897
898         x = spltty();
899         (*linesw[tp->t_line].l_close)(tp, flag);
900         stl_ttyoptim(portp, &tp->t_termios);
901         stl_rawclose(portp);
902         ttyclose(tp);
903         splx(x);
904         return(0);
905 }
906
907 /*****************************************************************************/
908
909 #if VFREEBSD >= 220
910
911 STATIC void stl_stop(struct tty *tp, int rw)
912 {
913 #if DEBUG
914         printf("stl_stop(tp=%x,rw=%x)\n", (int) tp, rw);
915 #endif
916
917         stl_flush((stlport_t *) tp, rw);
918 }
919
920 #else
921
922 STATIC int stlstop(struct tty *tp, int rw)
923 {
924 #if DEBUG
925         printf("stlstop(tp=%x,rw=%x)\n", (int) tp, rw);
926 #endif
927
928         stl_flush((stlport_t *) tp, rw);
929         return(0);
930 }
931
932 #endif
933
934 /*****************************************************************************/
935
936 STATIC int stlioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
937                     struct proc *p)
938 {
939         struct termios  *newtios, *localtios;
940         struct tty      *tp;
941         stlport_t       *portp;
942         int             error, i, x;
943
944 #if DEBUG
945         printf("stlioctl(dev=%s,cmd=%lx,data=%p,flag=%x,p=%p)\n",
946                 devtoname(dev), cmd, (void *) data, flag, (void *) p);
947 #endif
948
949         if (minor(dev) & STL_MEMDEV)
950                 return(stl_memioctl(dev, cmd, data, flag, p));
951
952         portp = stl_dev2port(dev);
953         if (portp == (stlport_t *) NULL)
954                 return(ENODEV);
955         tp = &portp->tty;
956         error = 0;
957         
958 /*
959  *      First up handle ioctls on the control devices.
960  */
961         if (minor(dev) & STL_CTRLDEV) {
962                 if ((minor(dev) & STL_CTRLDEV) == STL_CTRLINIT)
963                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
964                                 &portp->initouttios : &portp->initintios;
965                 else if ((minor(dev) & STL_CTRLDEV) == STL_CTRLLOCK)
966                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
967                                 &portp->lockouttios : &portp->lockintios;
968                 else
969                         return(ENODEV);
970
971                 switch (cmd) {
972                 case TIOCSETA:
973                         if ((error = suser(p)) == 0)
974                                 *localtios = *((struct termios *) data);
975                         break;
976                 case TIOCGETA:
977                         *((struct termios *) data) = *localtios;
978                         break;
979                 case TIOCGETD:
980                         *((int *) data) = TTYDISC;
981                         break;
982                 case TIOCGWINSZ:
983                         bzero(data, sizeof(struct winsize));
984                         break;
985                 default:
986                         error = ENOTTY;
987                         break;
988                 }
989                 return(error);
990         }
991
992 /*
993  *      Deal with 4.3 compatibility issues if we have too...
994  */
995 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
996         if (1) {
997                 struct termios  tios;
998                 unsigned long   oldcmd;
999
1000                 tios = tp->t_termios;
1001                 oldcmd = cmd;
1002                 if ((error = ttsetcompat(tp, &cmd, data, &tios)))
1003                         return(error);
1004                 if (cmd != oldcmd)
1005                         data = (caddr_t) &tios;
1006         }
1007 #endif
1008
1009 /*
1010  *      Carry out some pre-cmd processing work first...
1011  *      Hmmm, not so sure we want this, disable for now...
1012  */
1013         if ((cmd == TIOCSETA) || (cmd == TIOCSETAW) || (cmd == TIOCSETAF)) {
1014                 newtios = (struct termios *) data;
1015                 localtios = (minor(dev) & STL_CALLOUTDEV) ? 
1016                         &portp->lockouttios : &portp->lockintios;
1017
1018                 newtios->c_iflag = (tp->t_iflag & localtios->c_iflag) |
1019                         (newtios->c_iflag & ~localtios->c_iflag);
1020                 newtios->c_oflag = (tp->t_oflag & localtios->c_oflag) |
1021                         (newtios->c_oflag & ~localtios->c_oflag);
1022                 newtios->c_cflag = (tp->t_cflag & localtios->c_cflag) |
1023                         (newtios->c_cflag & ~localtios->c_cflag);
1024                 newtios->c_lflag = (tp->t_lflag & localtios->c_lflag) |
1025                         (newtios->c_lflag & ~localtios->c_lflag);
1026                 for (i = 0; (i < NCCS); i++) {
1027                         if (localtios->c_cc[i] != 0)
1028                                 newtios->c_cc[i] = tp->t_cc[i];
1029                 }
1030                 if (localtios->c_ispeed != 0)
1031                         newtios->c_ispeed = tp->t_ispeed;
1032                 if (localtios->c_ospeed != 0)
1033                         newtios->c_ospeed = tp->t_ospeed;
1034         }
1035
1036 /*
1037  *      Call the line discipline and the common command processing to
1038  *      process this command (if they can).
1039  */
1040         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1041         if (error != ENOIOCTL)
1042                 return(error);
1043
1044         x = spltty();
1045         error = ttioctl(tp, cmd, data, flag);
1046         stl_ttyoptim(portp, &tp->t_termios);
1047         if (error != ENOIOCTL) {
1048                 splx(x);
1049                 return(error);
1050         }
1051
1052         error = 0;
1053
1054 /*
1055  *      Process local commands here. These are all commands that only we
1056  *      can take care of (they all rely on actually doing something special
1057  *      to the actual hardware).
1058  */
1059         switch (cmd) {
1060         case TIOCSBRK:
1061                 stl_sendbreak(portp, -1);
1062                 break;
1063         case TIOCCBRK:
1064                 stl_sendbreak(portp, -2);
1065                 break;
1066         case TIOCSDTR:
1067                 stl_setsignals(portp, 1, -1);
1068                 break;
1069         case TIOCCDTR:
1070                 stl_setsignals(portp, 0, -1);
1071                 break;
1072         case TIOCMSET:
1073                 i = *((int *) data);
1074                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : 0),
1075                         ((i & TIOCM_RTS) ? 1 : 0));
1076                 break;
1077         case TIOCMBIS:
1078                 i = *((int *) data);
1079                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : -1),
1080                         ((i & TIOCM_RTS) ? 1 : -1));
1081                 break;
1082         case TIOCMBIC:
1083                 i = *((int *) data);
1084                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 0 : -1),
1085                         ((i & TIOCM_RTS) ? 0 : -1));
1086                 break;
1087         case TIOCMGET:
1088                 *((int *) data) = (stl_getsignals(portp) | TIOCM_LE);
1089                 break;
1090         case TIOCMSDTRWAIT:
1091                 if ((error = suser(p)) == 0)
1092                         portp->dtrwait = *((int *) data) * hz / 100;
1093                 break;
1094         case TIOCMGDTRWAIT:
1095                 *((int *) data) = portp->dtrwait * 100 / hz;
1096                 break;
1097         case TIOCTIMESTAMP:
1098                 portp->dotimestamp = 1;
1099                 *((struct timeval *) data) = portp->timestamp;
1100                 break;
1101         default:
1102                 error = ENOTTY;
1103                 break;
1104         }
1105         splx(x);
1106
1107         return(error);
1108 }
1109
1110 /*****************************************************************************/
1111
1112 /*
1113  *      Convert the specified minor device number into a port struct
1114  *      pointer. Return NULL if the device number is not a valid port.
1115  */
1116
1117 STATIC stlport_t *stl_dev2port(dev_t dev)
1118 {
1119         stlbrd_t        *brdp;
1120
1121         brdp = stl_brds[MKDEV2BRD(dev)];
1122         if (brdp == (stlbrd_t *) NULL)
1123                 return((stlport_t *) NULL);
1124         return(brdp->ports[MKDEV2PORT(dev)]);
1125 }
1126
1127 /*****************************************************************************/
1128
1129 /*
1130  *      Initialize the port hardware. This involves enabling the transmitter
1131  *      and receiver, setting the port configuration, and setting the initial
1132  *      signal state.
1133  */
1134
1135 static int stl_rawopen(stlport_t *portp)
1136 {
1137 #if DEBUG
1138         printf("stl_rawopen(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1139                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1140 #endif
1141         stl_param(&portp->tty, &portp->tty.t_termios);
1142         portp->sigs = stl_getsignals(portp);
1143         stl_setsignals(portp, 1, 1);
1144         stl_enablerxtx(portp, 1, 1);
1145         stl_startrxtx(portp, 1, 0);
1146         return(0);
1147 }
1148
1149 /*****************************************************************************/
1150
1151 /*
1152  *      Shutdown the hardware of a port. Disable its transmitter and
1153  *      receiver, and maybe drop signals if appropriate.
1154  */
1155
1156 static int stl_rawclose(stlport_t *portp)
1157 {
1158         struct tty      *tp;
1159
1160 #if DEBUG
1161         printf("stl_rawclose(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1162                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1163 #endif
1164
1165         tp = &portp->tty;
1166         stl_disableintrs(portp);
1167         stl_enablerxtx(portp, 0, 0);
1168         stl_flush(portp, (FWRITE | FREAD));
1169         if (tp->t_cflag & HUPCL) {
1170                 stl_setsignals(portp, 0, 0);
1171                 if (portp->dtrwait != 0) {
1172                         portp->state |= ASY_DTRWAIT;
1173                         timeout(stl_dtrwakeup, portp, portp->dtrwait);
1174                 }
1175         }
1176         portp->callout = 0;
1177         portp->brklen = 0;
1178         portp->state &= ~(ASY_ACTIVE | ASY_RTSFLOW);
1179         wakeup(&portp->callout);
1180         wakeup(TSA_CARR_ON(tp));
1181         return(0);
1182 }
1183
1184 /*****************************************************************************/
1185
1186 /*
1187  *      Clear the DTR waiting flag, and wake up any sleepers waiting for
1188  *      DTR wait period to finish.
1189  */
1190
1191 static void stl_dtrwakeup(void *arg)
1192 {
1193         stlport_t       *portp;
1194
1195         portp = (stlport_t *) arg;
1196         portp->state &= ~ASY_DTRWAIT;
1197         wakeup(&portp->dtrwait);
1198 }
1199
1200 /*****************************************************************************/
1201
1202 /*
1203  *      Start (or continue) the transfer of TX data on this port. If the
1204  *      port is not currently busy then load up the interrupt ring queue
1205  *      buffer and kick of the transmitter. If the port is running low on
1206  *      TX data then refill the ring queue. This routine is also used to
1207  *      activate input flow control!
1208  */
1209
1210 static void stl_start(struct tty *tp)
1211 {
1212         stlport_t       *portp;
1213         unsigned int    len, stlen;
1214         char            *head, *tail;
1215         int             count, x;
1216
1217         portp = (stlport_t *) tp;
1218
1219 #if DEBUG
1220         printf("stl_start(tp=%x): brdnr=%d portnr=%d\n", (int) tp, 
1221                 portp->brdnr, portp->portnr);
1222 #endif
1223
1224         x = spltty();
1225
1226 /*
1227  *      Check if the ports input has been blocked, and take appropriate action.
1228  *      Not very often do we really need to do anything, so make it quick.
1229  */
1230         if (tp->t_state & TS_TBLOCK) {
1231                 if ((portp->state & ASY_RTSFLOW) == 0)
1232                         stl_flowcontrol(portp, 0, -1);
1233         } else {
1234                 if (portp->state & ASY_RTSFLOW)
1235                         stl_flowcontrol(portp, 1, -1);
1236         }
1237
1238 #if VFREEBSD == 205
1239 /*
1240  *      Check if the output cooked clist buffers are near empty, wake up
1241  *      the line discipline to fill it up.
1242  */
1243         if (tp->t_outq.c_cc <= tp->t_lowat) {
1244                 if (tp->t_state & TS_ASLEEP) {
1245                         tp->t_state &= ~TS_ASLEEP;
1246                         wakeup(&tp->t_outq);
1247                 }
1248                 selwakeup(&tp->t_wsel);
1249         }
1250 #endif
1251
1252         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
1253                 splx(x);
1254                 return;
1255         }
1256
1257 /*
1258  *      Copy data from the clists into the interrupt ring queue. This will
1259  *      require at most 2 copys... What we do is calculate how many chars
1260  *      can fit into the ring queue, and how many can fit in 1 copy. If after
1261  *      the first copy there is still more room then do the second copy. 
1262  *      The beauty of this type of ring queue is that we do not need to
1263  *      spl protect our-selves, since we only ever update the head pointer,
1264  *      and the interrupt routine only ever updates the tail pointer.
1265  */
1266         if (tp->t_outq.c_cc != 0) {
1267                 head = portp->tx.head;
1268                 tail = portp->tx.tail;
1269                 if (head >= tail) {
1270                         len = STL_TXBUFSIZE - (head - tail) - 1;
1271                         stlen = portp->tx.endbuf - head;
1272                 } else {
1273                         len = tail - head - 1;
1274                         stlen = len;
1275                 }
1276
1277                 if (len > 0) {
1278                         stlen = MIN(len, stlen);
1279                         count = q_to_b(&tp->t_outq, head, stlen);
1280                         len -= count;
1281                         head += count;
1282                         if (head >= portp->tx.endbuf) {
1283                                 head = portp->tx.buf;
1284                                 if (len > 0) {
1285                                         stlen = q_to_b(&tp->t_outq, head, len);
1286                                         head += stlen;
1287                                         count += stlen;
1288                                 }
1289                         }
1290                         portp->tx.head = head;
1291                         if (count > 0)
1292                                 stl_startrxtx(portp, -1, 1);
1293                 }
1294
1295 /*
1296  *              If we sent something, make sure we are called again.
1297  */
1298                 tp->t_state |= TS_BUSY;
1299         }
1300
1301 #if VFREEBSD != 205
1302 /*
1303  *      Do any writer wakeups.
1304  */
1305         ttwwakeup(tp);
1306 #endif
1307
1308         splx(x);
1309 }
1310
1311 /*****************************************************************************/
1312
1313 static void stl_flush(stlport_t *portp, int flag)
1314 {
1315         char    *head, *tail;
1316         int     len, x;
1317
1318 #if DEBUG
1319         printf("stl_flush(portp=%x,flag=%x)\n", (int) portp, flag);
1320 #endif
1321
1322         if (portp == (stlport_t *) NULL)
1323                 return;
1324
1325         x = spltty();
1326
1327         if (flag & FWRITE) {
1328                 BRDENABLE(portp->brdnr, portp->pagenr);
1329                 stl_setreg(portp, CAR, (portp->portnr & 0x03));
1330                 stl_ccrwait(portp);
1331                 stl_setreg(portp, CCR, CCR_TXFLUSHFIFO);
1332                 stl_ccrwait(portp);
1333                 portp->tx.tail = portp->tx.head;
1334                 BRDDISABLE(portp->brdnr);
1335         }
1336
1337 /*
1338  *      The only thing to watch out for when flushing the read side is
1339  *      the RX status buffer. The interrupt code relys on the status
1340  *      bytes as being zeroed all the time (it does not bother setting
1341  *      a good char status to 0, it expects that it already will be).
1342  *      We also need to un-flow the RX channel if flow control was
1343  *      active.
1344  */
1345         if (flag & FREAD) {
1346                 head = portp->rx.head;
1347                 tail = portp->rx.tail;
1348                 if (head != tail) {
1349                         if (head >= tail) {
1350                                 len = head - tail;
1351                         } else {
1352                                 len = portp->rx.endbuf - tail;
1353                                 bzero(portp->rxstatus.buf,
1354                                         (head - portp->rx.buf));
1355                         }
1356                         bzero((tail + STL_RXBUFSIZE), len);
1357                         portp->rx.tail = head;
1358                 }
1359
1360                 if ((portp->state & ASY_RTSFLOW) &&
1361                                 ((portp->tty.t_state & TS_TBLOCK) == 0))
1362                         stl_flowcontrol(portp, 1, -1);
1363         }
1364
1365         splx(x);
1366 }
1367
1368 /*****************************************************************************/
1369
1370 /*
1371  *      These functions get/set/update the registers of the cd1400 UARTs.
1372  *      Access to the cd1400 registers is via an address/data io port pair.
1373  *      (Maybe should make this inline...)
1374  */
1375
1376 static int stl_getreg(stlport_t *portp, int regnr)
1377 {
1378         outb(portp->ioaddr, (regnr + portp->uartaddr));
1379         return(inb(portp->ioaddr + EREG_DATA));
1380 }
1381
1382 /*****************************************************************************/
1383
1384 static void stl_setreg(stlport_t *portp, int regnr, int value)
1385 {
1386         outb(portp->ioaddr, (regnr + portp->uartaddr));
1387         outb((portp->ioaddr + EREG_DATA), value);
1388 }
1389
1390 /*****************************************************************************/
1391
1392 static int stl_updatereg(stlport_t *portp, int regnr, int value)
1393 {
1394         outb(portp->ioaddr, (regnr + portp->uartaddr));
1395         if (inb(portp->ioaddr + EREG_DATA) != value) {
1396                 outb((portp->ioaddr + EREG_DATA), value);
1397                 return(1);
1398         }
1399         return(0);
1400 }
1401
1402 /*****************************************************************************/
1403
1404 /*
1405  *      Wait for the command register to be ready. We will poll this, since
1406  *      it won't usually take too long to be ready, and it is only really
1407  *      used for non-critical actions.
1408  */
1409
1410 static void stl_ccrwait(stlport_t *portp)
1411 {
1412         int     i;
1413
1414         for (i = 0; (i < CCR_MAXWAIT); i++) {
1415                 if (stl_getreg(portp, CCR) == 0) {
1416                         return;
1417                 }
1418         }
1419
1420         printf("STALLION: cd1400 device not responding, brd=%d panel=%d"
1421                 "port=%d\n", portp->brdnr, portp->panelnr, portp->portnr);
1422 }
1423
1424 /*****************************************************************************/
1425
1426 /*
1427  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
1428  *      chars is pretty simple, stuff as many as possible from the TX buffer
1429  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
1430  *      are embedded as commands in the data stream. Oh no, had to use a goto!
1431  *      This could be optimized more, will do when I get time...
1432  *      In practice it is possible that interrupts are enabled but that the
1433  *      port has been hung up. Need to handle not having any TX buffer here,
1434  *      this is done by using the side effect that head and tail will also
1435  *      be NULL if the buffer has been freed.
1436  */
1437
1438 static __inline void stl_txisr(stlpanel_t *panelp, int ioaddr)
1439 {
1440         stlport_t       *portp;
1441         int             len, stlen;
1442         char            *head, *tail;
1443         unsigned char   ioack, srer;
1444
1445 #if DEBUG
1446         printf("stl_txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1447 #endif
1448
1449         ioack = inb(ioaddr + EREG_TXACK);
1450         if (((ioack & panelp->ackmask) != 0) ||
1451                         ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
1452                 printf("STALLION: bad TX interrupt ack value=%x\n", ioack);
1453                 return;
1454         }
1455         portp = panelp->ports[(ioack >> 3)];
1456
1457 /*
1458  *      Unfortunately we need to handle breaks in the data stream, since
1459  *      this is the only way to generate them on the cd1400. Do it now if
1460  *      a break is to be sent. Some special cases here: brklen is -1 then
1461  *      start sending an un-timed break, if brklen is -2 then stop sending
1462  *      an un-timed break, if brklen is -3 then we have just sent an
1463  *      un-timed break and do not want any data to go out, if brklen is -4
1464  *      then a break has just completed so clean up the port settings.
1465  */
1466         if (portp->brklen != 0) {
1467                 if (portp->brklen >= -1) {
1468                         outb(ioaddr, (TDR + portp->uartaddr));
1469                         outb((ioaddr + EREG_DATA), ETC_CMD);
1470                         outb((ioaddr + EREG_DATA), ETC_STARTBREAK);
1471                         if (portp->brklen > 0) {
1472                                 outb((ioaddr + EREG_DATA), ETC_CMD);
1473                                 outb((ioaddr + EREG_DATA), ETC_DELAY);
1474                                 outb((ioaddr + EREG_DATA), portp->brklen);
1475                                 outb((ioaddr + EREG_DATA), ETC_CMD);
1476                                 outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
1477                                 portp->brklen = -4;
1478                         } else {
1479                                 portp->brklen = -3;
1480                         }
1481                 } else if (portp->brklen == -2) {
1482                         outb(ioaddr, (TDR + portp->uartaddr));
1483                         outb((ioaddr + EREG_DATA), ETC_CMD);
1484                         outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
1485                         portp->brklen = -4;
1486                 } else if (portp->brklen == -3) {
1487                         outb(ioaddr, (SRER + portp->uartaddr));
1488                         srer = inb(ioaddr + EREG_DATA);
1489                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
1490                         outb((ioaddr + EREG_DATA), srer);
1491                 } else {
1492                         outb(ioaddr, (COR2 + portp->uartaddr));
1493                         outb((ioaddr + EREG_DATA),
1494                                 (inb(ioaddr + EREG_DATA) & ~COR2_ETC));
1495                         portp->brklen = 0;
1496                 }
1497                 goto stl_txalldone;
1498         }
1499
1500         head = portp->tx.head;
1501         tail = portp->tx.tail;
1502         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1503         if ((len == 0) || ((len < STL_TXBUFLOW) &&
1504                         ((portp->state & ASY_TXLOW) == 0))) {
1505                 portp->state |= ASY_TXLOW;
1506                 stl_dotimeout();
1507         }
1508
1509         if (len == 0) {
1510                 outb(ioaddr, (SRER + portp->uartaddr));
1511                 srer = inb(ioaddr + EREG_DATA);
1512                 if (srer & SRER_TXDATA) {
1513                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
1514                 } else {
1515                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
1516                         portp->tty.t_state &= ~TS_BUSY;
1517                 }
1518                 outb((ioaddr + EREG_DATA), srer);
1519         } else {
1520                 len = MIN(len, CD1400_TXFIFOSIZE);
1521                 portp->stats.txtotal += len;
1522                 stlen = MIN(len, (portp->tx.endbuf - tail));
1523                 outb(ioaddr, (TDR + portp->uartaddr));
1524                 outsb((ioaddr + EREG_DATA), tail, stlen);
1525                 len -= stlen;
1526                 tail += stlen;
1527                 if (tail >= portp->tx.endbuf)
1528                         tail = portp->tx.buf;
1529                 if (len > 0) {
1530                         outsb((ioaddr + EREG_DATA), tail, len);
1531                         tail += len;
1532                 }
1533                 portp->tx.tail = tail;
1534         }
1535
1536 stl_txalldone:
1537         outb(ioaddr, (EOSRR + portp->uartaddr));
1538         outb((ioaddr + EREG_DATA), 0);
1539 }
1540
1541 /*****************************************************************************/
1542
1543 /*
1544  *      Receive character interrupt handler. Determine if we have good chars
1545  *      or bad chars and then process appropriately. Good chars are easy
1546  *      just shove the lot into the RX buffer and set all status bytes to 0.
1547  *      If a bad RX char then process as required. This routine needs to be
1548  *      fast!
1549  */
1550
1551 static __inline void stl_rxisr(stlpanel_t *panelp, int ioaddr)
1552 {
1553         stlport_t       *portp;
1554         struct tty      *tp;
1555         unsigned int    ioack, len, buflen, stlen;
1556         unsigned char   status;
1557         char            ch;
1558         char            *head, *tail;
1559         static char     unwanted[CD1400_RXFIFOSIZE];
1560
1561 #if DEBUG
1562         printf("stl_rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1563 #endif
1564
1565         ioack = inb(ioaddr + EREG_RXACK);
1566         if ((ioack & panelp->ackmask) != 0) {
1567                 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
1568                 return;
1569         }
1570         portp = panelp->ports[(ioack >> 3)];
1571         tp = &portp->tty;
1572
1573 /*
1574  *      First up, caluclate how much room there is in the RX ring queue.
1575  *      We also want to keep track of the longest possible copy length,
1576  *      this has to allow for the wrapping of the ring queue.
1577  */
1578         head = portp->rx.head;
1579         tail = portp->rx.tail;
1580         if (head >= tail) {
1581                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
1582                 stlen = portp->rx.endbuf - head;
1583         } else {
1584                 buflen = tail - head - 1;
1585                 stlen = buflen;
1586         }
1587
1588 /*
1589  *      Check if the input buffer is near full. If so then we should take
1590  *      some flow control action... It is very easy to do hardware and
1591  *      software flow control from here since we have the port selected on
1592  *      the UART.
1593  */
1594         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
1595                 if (((portp->state & ASY_RTSFLOW) == 0) &&
1596                                 (portp->state & ASY_RTSFLOWMODE)) {
1597                         portp->state |= ASY_RTSFLOW;
1598                         stl_setreg(portp, MCOR1,
1599                                 (stl_getreg(portp, MCOR1) & 0xf0));
1600                         stl_setreg(portp, MSVR2, 0);
1601                         portp->stats.rxrtsoff++;
1602                 }
1603         }
1604
1605 /*
1606  *      OK we are set, process good data... If the RX ring queue is full
1607  *      just chuck the chars - don't leave them in the UART.
1608  */
1609         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
1610                 outb(ioaddr, (RDCR + portp->uartaddr));
1611                 len = inb(ioaddr + EREG_DATA);
1612                 if (buflen == 0) {
1613                         outb(ioaddr, (RDSR + portp->uartaddr));
1614                         insb((ioaddr + EREG_DATA), &unwanted[0], len);
1615                         portp->stats.rxlost += len;
1616                         portp->stats.rxtotal += len;
1617                 } else {
1618                         len = MIN(len, buflen);
1619                         portp->stats.rxtotal += len;
1620                         stlen = MIN(len, stlen);
1621                         if (len > 0) {
1622                                 outb(ioaddr, (RDSR + portp->uartaddr));
1623                                 insb((ioaddr + EREG_DATA), head, stlen);
1624                                 head += stlen;
1625                                 if (head >= portp->rx.endbuf) {
1626                                         head = portp->rx.buf;
1627                                         len -= stlen;
1628                                         insb((ioaddr + EREG_DATA), head, len);
1629                                         head += len;
1630                                 }
1631                         }
1632                 }
1633         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
1634                 outb(ioaddr, (RDSR + portp->uartaddr));
1635                 status = inb(ioaddr + EREG_DATA);
1636                 ch = inb(ioaddr + EREG_DATA);
1637                 if (status & ST_BREAK)
1638                         portp->stats.rxbreaks++;
1639                 if (status & ST_FRAMING)
1640                         portp->stats.rxframing++;
1641                 if (status & ST_PARITY)
1642                         portp->stats.rxparity++;
1643                 if (status & ST_OVERRUN)
1644                         portp->stats.rxoverrun++;
1645                 if (status & ST_SCHARMASK) {
1646                         if ((status & ST_SCHARMASK) == ST_SCHAR1)
1647                                 portp->stats.txxon++;
1648                         if ((status & ST_SCHARMASK) == ST_SCHAR2)
1649                                 portp->stats.txxoff++;
1650                         goto stl_rxalldone;
1651                 }
1652                 if ((portp->rxignoremsk & status) == 0) {
1653                         if ((tp->t_state & TS_CAN_BYPASS_L_RINT) &&
1654                             ((status & ST_FRAMING) ||
1655                             ((status & ST_PARITY) && (tp->t_iflag & INPCK))))
1656                                 ch = 0;
1657                         if ((portp->rxmarkmsk & status) == 0)
1658                                 status = 0;
1659                         *(head + STL_RXBUFSIZE) = status;
1660                         *head++ = ch;
1661                         if (head >= portp->rx.endbuf)
1662                                 head = portp->rx.buf;
1663                 }
1664         } else {
1665                 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
1666                 return;
1667         }
1668
1669         portp->rx.head = head;
1670         portp->state |= ASY_RXDATA;
1671         stl_dotimeout();
1672
1673 stl_rxalldone:
1674         outb(ioaddr, (EOSRR + portp->uartaddr));
1675         outb((ioaddr + EREG_DATA), 0);
1676 }
1677
1678 /*****************************************************************************/
1679
1680 /*
1681  *      Modem interrupt handler. The is called when the modem signal line
1682  *      (DCD) has changed state. Leave most of the work to the off-level
1683  *      processing routine.
1684  */
1685
1686 static __inline void stl_mdmisr(stlpanel_t *panelp, int ioaddr)
1687 {
1688         stlport_t       *portp;
1689         unsigned int    ioack;
1690         unsigned char   misr;
1691
1692 #if DEBUG
1693         printf("stl_mdmisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1694 #endif
1695
1696         ioack = inb(ioaddr + EREG_MDACK);
1697         if (((ioack & panelp->ackmask) != 0) ||
1698                         ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
1699                 printf("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
1700                 return;
1701         }
1702         portp = panelp->ports[(ioack >> 3)];
1703
1704         outb(ioaddr, (MISR + portp->uartaddr));
1705         misr = inb(ioaddr + EREG_DATA);
1706         if (misr & MISR_DCD) {
1707                 portp->state |= ASY_DCDCHANGE;
1708                 portp->stats.modem++;
1709                 stl_dotimeout();
1710         }
1711
1712         outb(ioaddr, (EOSRR + portp->uartaddr));
1713         outb((ioaddr + EREG_DATA), 0);
1714 }
1715
1716 /*****************************************************************************/
1717
1718 /*
1719  *      Interrupt handler for EIO and ECH boards. This code ain't all that
1720  *      pretty, but the idea is to make it as fast as possible. This code is
1721  *      well suited to be assemblerized :-)  We don't use the general purpose
1722  *      register access functions here, for speed we will go strait to the
1723  *      io register.
1724  */
1725
1726 static void stlintr(int unit)
1727 {
1728         stlbrd_t        *brdp;
1729         stlpanel_t      *panelp;
1730         unsigned char   svrtype;
1731         int             i, panelnr, iobase;
1732         int             cnt;
1733
1734 #if DEBUG
1735         printf("stlintr(unit=%d)\n", unit);
1736 #endif
1737
1738         cnt = 0;
1739         panelp = (stlpanel_t *) NULL;
1740         for (i = 0; (i < stl_nrbrds); ) {
1741                 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL) {
1742                         i++;
1743                         continue;
1744                 }
1745                 if (brdp->state == 0) {
1746                         i++;
1747                         continue;
1748                 }
1749 /*
1750  *              The following section of code handles the subtle differences
1751  *              between board types. It is sort of similar, but different
1752  *              enough to handle each separately.
1753  */
1754                 if (brdp->brdtype == BRD_EASYIO) {
1755                         if ((inb(brdp->iostatus) & EIO_INTRPEND) == 0) {
1756                                 i++;
1757                                 continue;
1758                         }
1759                         panelp = brdp->panels[0];
1760                         iobase = panelp->iobase;
1761                         outb(iobase, SVRR);
1762                         svrtype = inb(iobase + EREG_DATA);
1763                         if (brdp->nrports > 4) {
1764                                 outb(iobase, (SVRR + 0x80));
1765                                 svrtype |= inb(iobase + EREG_DATA);
1766                         }
1767                 } else if (brdp->brdtype == BRD_ECH) {
1768                         if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1769                                 i++;
1770                                 continue;
1771                         }
1772                         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
1773                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1774                                 panelp = brdp->panels[panelnr];
1775                                 iobase = panelp->iobase;
1776                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1777                                         break;
1778                                 if (panelp->nrports > 8) {
1779                                         iobase += 0x8;
1780                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1781                                                 break;
1782                                 }
1783                         }       
1784                         if (panelnr >= brdp->nrpanels) {
1785                                 i++;
1786                                 continue;
1787                         }
1788                         outb(iobase, SVRR);
1789                         svrtype = inb(iobase + EREG_DATA);
1790                         outb(iobase, (SVRR + 0x80));
1791                         svrtype |= inb(iobase + EREG_DATA);
1792                 } else if (brdp->brdtype == BRD_ECHPCI) {
1793                         iobase = brdp->ioaddr2;
1794                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1795                                 panelp = brdp->panels[panelnr];
1796                                 outb(brdp->ioctrl, panelp->pagenr);
1797                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1798                                         break;
1799                                 if (panelp->nrports > 8) {
1800                                         outb(brdp->ioctrl, (panelp->pagenr + 1));
1801                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1802                                                 break;
1803                                 }
1804                         }       
1805                         if (panelnr >= brdp->nrpanels) {
1806                                 i++;
1807                                 continue;
1808                         }
1809                         outb(iobase, SVRR);
1810                         svrtype = inb(iobase + EREG_DATA);
1811                         outb(iobase, (SVRR + 0x80));
1812                         svrtype |= inb(iobase + EREG_DATA);
1813                 } else if (brdp->brdtype == BRD_ECHMC) {
1814                         if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1815                                 i++;
1816                                 continue;
1817                         }
1818                         for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1819                                 panelp = brdp->panels[panelnr];
1820                                 iobase = panelp->iobase;
1821                                 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1822                                         break;
1823                                 if (panelp->nrports > 8) {
1824                                         iobase += 0x8;
1825                                         if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1826                                                 break;
1827                                 }
1828                         }       
1829                         if (panelnr >= brdp->nrpanels) {
1830                                 i++;
1831                                 continue;
1832                         }
1833                         outb(iobase, SVRR);
1834                         svrtype = inb(iobase + EREG_DATA);
1835                         outb(iobase, (SVRR + 0x80));
1836                         svrtype |= inb(iobase + EREG_DATA);
1837                 } else {
1838                         printf("STALLION: unknown board type=%x\n", brdp->brdtype);
1839                         i++;
1840                         continue;
1841                 }
1842
1843 /*
1844  *              We have determined what type of service is required for a
1845  *              port. From here on in the service of a port is the same no
1846  *              matter what the board type...
1847  */
1848                 if (svrtype & SVRR_RX)
1849                         stl_rxisr(panelp, iobase);
1850                 if (svrtype & SVRR_TX)
1851                         stl_txisr(panelp, iobase);
1852                 if (svrtype & SVRR_MDM)
1853                         stl_mdmisr(panelp, iobase);
1854
1855                 if (brdp->brdtype == BRD_ECH)
1856                         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
1857         }
1858 }
1859
1860 /*****************************************************************************/
1861
1862 #if NPCI > 0
1863
1864 static void stlpciintr(void *arg)
1865 {
1866         stlintr(0);
1867 }
1868
1869 #endif
1870
1871 /*****************************************************************************/
1872
1873 /*
1874  *      If we haven't scheduled a timeout then do it, some port needs high
1875  *      level processing.
1876  */
1877
1878 static void stl_dotimeout()
1879 {
1880 #if DEBUG
1881         printf("stl_dotimeout()\n");
1882 #endif
1883
1884         if (stl_doingtimeout == 0) {
1885                 timeout(stl_poll, 0, 1);
1886                 stl_doingtimeout++;
1887         }
1888 }
1889
1890 /*****************************************************************************/
1891
1892 /*
1893  *      Service "software" level processing. Too slow or painfull to be done
1894  *      at real hardware interrupt time. This way we might also be able to
1895  *      do some service on other waiting ports as well...
1896  */
1897
1898 static void stl_poll(void *arg)
1899 {
1900         stlbrd_t        *brdp;
1901         stlport_t       *portp;
1902         struct tty      *tp;
1903         int             brdnr, portnr, rearm, x;
1904
1905 #if DEBUG
1906         printf("stl_poll()\n");
1907 #endif
1908
1909         stl_doingtimeout = 0;
1910         rearm = 0;
1911
1912         x = spltty();
1913         for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
1914                 if ((brdp = stl_brds[brdnr]) == (stlbrd_t *) NULL)
1915                         continue;
1916                 for (portnr = 0; (portnr < brdp->nrports); portnr++) {
1917                         if ((portp = brdp->ports[portnr]) == (stlport_t *) NULL)
1918                                 continue;
1919                         if ((portp->state & ASY_ACTIVE) == 0)
1920                                 continue;
1921                         tp = &portp->tty;
1922
1923                         if (portp->state & ASY_RXDATA)
1924                                 stl_rxprocess(portp);
1925                         if (portp->state & ASY_DCDCHANGE) {
1926                                 portp->state &= ~ASY_DCDCHANGE;
1927                                 portp->sigs = stl_getsignals(portp);
1928                                 (*linesw[tp->t_line].l_modem)(tp,
1929                                         (portp->sigs & TIOCM_CD));
1930                         }
1931                         if (portp->state & ASY_TXLOW) {
1932                                 portp->state &= ~ASY_TXLOW;
1933                                 (*linesw[tp->t_line].l_start)(tp);
1934                         }
1935
1936                         if (portp->state & ASY_ACTIVE)
1937                                 rearm++;
1938                 }
1939         }
1940         splx(x);
1941
1942         if (rearm)
1943                 stl_dotimeout();
1944 }
1945
1946 /*****************************************************************************/
1947
1948 /*
1949  *      Process the RX data that has been buffered up in the RX ring queue.
1950  */
1951
1952 static void stl_rxprocess(stlport_t *portp)
1953 {
1954         struct tty      *tp;
1955         unsigned int    len, stlen, lostlen;
1956         char            *head, *tail;
1957         char            status;
1958         int             ch;
1959
1960 #if DEBUG
1961         printf("stl_rxprocess(portp=%x): brdnr=%d portnr=%d\n", (int) portp, 
1962                 portp->brdnr, portp->portnr);
1963 #endif
1964
1965         tp = &portp->tty;
1966         portp->state &= ~ASY_RXDATA;
1967
1968         if ((tp->t_state & TS_ISOPEN) == 0) {
1969                 stl_flush(portp, FREAD);
1970                 return;
1971         }
1972
1973 /*
1974  *      Calculate the amount of data in the RX ring queue. Also calculate
1975  *      the largest single copy size...
1976  */
1977         head = portp->rx.head;
1978         tail = portp->rx.tail;
1979         if (head >= tail) {
1980                 len = head - tail;
1981                 stlen = len;
1982         } else {
1983                 len = STL_RXBUFSIZE - (tail - head);
1984                 stlen = portp->rx.endbuf - tail;
1985         }
1986
1987         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1988                 if (len > 0) {
1989                         if (((tp->t_rawq.c_cc + len) >= TTYHOG) &&
1990                                         ((portp->state & ASY_RTSFLOWMODE) ||
1991                                         (tp->t_iflag & IXOFF)) &&
1992                                         ((tp->t_state & TS_TBLOCK) == 0)) {
1993                                 ch = TTYHOG - tp->t_rawq.c_cc - 1;
1994                                 len = (ch > 0) ? ch : 0;
1995                                 stlen = MIN(stlen, len);
1996                                 ttyblock(tp);
1997                         }
1998                         lostlen = b_to_q(tail, stlen, &tp->t_rawq);
1999                         tail += stlen;
2000                         len -= stlen;
2001                         if (tail >= portp->rx.endbuf) {
2002                                 tail = portp->rx.buf;
2003                                 lostlen += b_to_q(tail, len, &tp->t_rawq);
2004                                 tail += len;
2005                         }
2006                         portp->stats.rxlost += lostlen;
2007                         ttwakeup(tp);
2008                         portp->rx.tail = tail;
2009                 }
2010         } else {
2011                 while (portp->rx.tail != head) {
2012                         ch = (unsigned char) *(portp->rx.tail);
2013                         status = *(portp->rx.tail + STL_RXBUFSIZE);
2014                         if (status) {
2015                                 *(portp->rx.tail + STL_RXBUFSIZE) = 0;
2016                                 if (status & ST_BREAK)
2017                                         ch |= TTY_BI;
2018                                 if (status & ST_FRAMING)
2019                                         ch |= TTY_FE;
2020                                 if (status & ST_PARITY)
2021                                         ch |= TTY_PE;
2022                                 if (status & ST_OVERRUN)
2023                                         ch |= TTY_OE;
2024                         }
2025                         (*linesw[tp->t_line].l_rint)(ch, tp);
2026                         if (portp->rx.tail == head)
2027                                 break;
2028
2029                         if (++(portp->rx.tail) >= portp->rx.endbuf)
2030                                 portp->rx.tail = portp->rx.buf;
2031                 }
2032         }
2033
2034         if (head != portp->rx.tail)
2035                 portp->state |= ASY_RXDATA;
2036
2037 /*
2038  *      If we where flow controled then maybe the buffer is low enough that
2039  *      we can re-activate it.
2040  */
2041         if ((portp->state & ASY_RTSFLOW) && ((tp->t_state & TS_TBLOCK) == 0))
2042                 stl_flowcontrol(portp, 1, -1);
2043 }
2044
2045 /*****************************************************************************/
2046
2047 /*
2048  *      Set up the cd1400 registers for a port based on the termios port
2049  *      settings.
2050  */
2051
2052 static int stl_param(struct tty *tp, struct termios *tiosp)
2053 {
2054         stlport_t       *portp;
2055         unsigned int    clkdiv;
2056         unsigned char   cor1, cor2, cor3;
2057         unsigned char   cor4, cor5, ccr;
2058         unsigned char   srer, sreron, sreroff;
2059         unsigned char   mcor1, mcor2, rtpr;
2060         unsigned char   clk, div;
2061         int             x;
2062
2063         portp = (stlport_t *) tp;
2064
2065 #if DEBUG
2066         printf("stl_param(tp=%x,tiosp=%x): brdnr=%d portnr=%d\n", (int) tp, 
2067                 (int) tiosp, portp->brdnr, portp->portnr);
2068 #endif
2069
2070         cor1 = 0;
2071         cor2 = 0;
2072         cor3 = 0;
2073         cor4 = 0;
2074         cor5 = 0;
2075         ccr = 0;
2076         rtpr = 0;
2077         clk = 0;
2078         div = 0;
2079         mcor1 = 0;
2080         mcor2 = 0;
2081         sreron = 0;
2082         sreroff = 0;
2083
2084 /*
2085  *      Set up the RX char ignore mask with those RX error types we
2086  *      can ignore. We could have used some special modes of the cd1400
2087  *      UART to help, but it is better this way because we can keep stats
2088  *      on the number of each type of RX exception event.
2089  */
2090         portp->rxignoremsk = 0;
2091         if (tiosp->c_iflag & IGNPAR)
2092                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
2093         if (tiosp->c_iflag & IGNBRK)
2094                 portp->rxignoremsk |= ST_BREAK;
2095
2096         portp->rxmarkmsk = ST_OVERRUN;
2097         if (tiosp->c_iflag & (INPCK | PARMRK))
2098                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2099         if (tiosp->c_iflag & BRKINT)
2100                 portp->rxmarkmsk |= ST_BREAK;
2101
2102 /*
2103  *      Go through the char size, parity and stop bits and set all the
2104  *      option registers appropriately.
2105  */
2106         switch (tiosp->c_cflag & CSIZE) {
2107         case CS5:
2108                 cor1 |= COR1_CHL5;
2109                 break;
2110         case CS6:
2111                 cor1 |= COR1_CHL6;
2112                 break;
2113         case CS7:
2114                 cor1 |= COR1_CHL7;
2115                 break;
2116         default:
2117                 cor1 |= COR1_CHL8;
2118                 break;
2119         }
2120
2121         if (tiosp->c_cflag & CSTOPB)
2122                 cor1 |= COR1_STOP2;
2123         else
2124                 cor1 |= COR1_STOP1;
2125
2126         if (tiosp->c_cflag & PARENB) {
2127                 if (tiosp->c_cflag & PARODD)
2128                         cor1 |= (COR1_PARENB | COR1_PARODD);
2129                 else
2130                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
2131         } else {
2132                 cor1 |= COR1_PARNONE;
2133         }
2134
2135         if (tiosp->c_iflag & ISTRIP)
2136                 cor5 |= COR5_ISTRIP;
2137
2138 /*
2139  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2140  *      space for hardware flow control and the like. This should be set to
2141  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
2142  *      really be based on VTIME...
2143  */
2144         cor3 |= FIFO_RXTHRESHOLD;
2145         rtpr = 2;
2146
2147 /*
2148  *      Calculate the baud rate timers. For now we will just assume that
2149  *      the input and output baud are the same. Could have used a baud
2150  *      table here, but this way we can generate virtually any baud rate
2151  *      we like!
2152  */
2153         if (tiosp->c_ispeed == 0)
2154                 tiosp->c_ispeed = tiosp->c_ospeed;
2155         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > STL_MAXBAUD))
2156                 return(EINVAL);
2157
2158         if (tiosp->c_ospeed > 0) {
2159                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
2160                         clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) /
2161                                 tiosp->c_ospeed);
2162                         if (clkdiv < 0x100)
2163                                 break;
2164                 }
2165                 div = (unsigned char) clkdiv;
2166         }
2167
2168 /*
2169  *      Check what form of modem signaling is required and set it up.
2170  */
2171         if ((tiosp->c_cflag & CLOCAL) == 0) {
2172                 mcor1 |= MCOR1_DCD;
2173                 mcor2 |= MCOR2_DCD;
2174                 sreron |= SRER_MODEM;
2175         }
2176
2177 /*
2178  *      Setup cd1400 enhanced modes if we can. In particular we want to
2179  *      handle as much of the flow control as possbile automatically. As
2180  *      well as saving a few CPU cycles it will also greatly improve flow
2181  *      control reliablilty.
2182  */
2183         if (tiosp->c_iflag & IXON) {
2184                 cor2 |= COR2_TXIBE;
2185                 cor3 |= COR3_SCD12;
2186                 if (tiosp->c_iflag & IXANY)
2187                         cor2 |= COR2_IXM;
2188         }
2189
2190         if (tiosp->c_cflag & CCTS_OFLOW)
2191                 cor2 |= COR2_CTSAE;
2192         if (tiosp->c_cflag & CRTS_IFLOW)
2193                 mcor1 |= FIFO_RTSTHRESHOLD;
2194
2195 /*
2196  *      All cd1400 register values calculated so go through and set them
2197  *      all up.
2198  */
2199 #if DEBUG
2200         printf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
2201                 portp->panelnr, portp->brdnr);
2202         printf("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n", cor1, cor2,
2203                 cor3, cor4, cor5);
2204         printf("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
2205                 mcor1, mcor2, rtpr, sreron, sreroff);
2206         printf("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
2207         printf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
2208                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP], tiosp->c_cc[VSTART],
2209                 tiosp->c_cc[VSTOP]);
2210 #endif
2211
2212         x = spltty();
2213         BRDENABLE(portp->brdnr, portp->pagenr);
2214         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2215         srer = stl_getreg(portp, SRER);
2216         stl_setreg(portp, SRER, 0);
2217         ccr += stl_updatereg(portp, COR1, cor1);
2218         ccr += stl_updatereg(portp, COR2, cor2);
2219         ccr += stl_updatereg(portp, COR3, cor3);
2220         if (ccr) {
2221                 stl_ccrwait(portp);
2222                 stl_setreg(portp, CCR, CCR_CORCHANGE);
2223         }
2224         stl_setreg(portp, COR4, cor4);
2225         stl_setreg(portp, COR5, cor5);
2226         stl_setreg(portp, MCOR1, mcor1);
2227         stl_setreg(portp, MCOR2, mcor2);
2228         if (tiosp->c_ospeed == 0) {
2229                 stl_setreg(portp, MSVR1, 0);
2230         } else {
2231                 stl_setreg(portp, MSVR1, MSVR1_DTR);
2232                 stl_setreg(portp, TCOR, clk);
2233                 stl_setreg(portp, TBPR, div);
2234                 stl_setreg(portp, RCOR, clk);
2235                 stl_setreg(portp, RBPR, div);
2236         }
2237         stl_setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
2238         stl_setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
2239         stl_setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
2240         stl_setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
2241         stl_setreg(portp, RTPR, rtpr);
2242         mcor1 = stl_getreg(portp, MSVR1);
2243         if (mcor1 & MSVR1_DCD)
2244                 portp->sigs |= TIOCM_CD;
2245         else
2246                 portp->sigs &= ~TIOCM_CD;
2247         stl_setreg(portp, SRER, ((srer & ~sreroff) | sreron));
2248         BRDDISABLE(portp->brdnr);
2249         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
2250         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
2251         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
2252         stl_ttyoptim(portp, tiosp);
2253         splx(x);
2254
2255         return(0);
2256 }
2257
2258 /*****************************************************************************/
2259
2260 /*
2261  *      Action the flow control as required. The hw and sw args inform the
2262  *      routine what flow control methods it should try.
2263  */
2264
2265 static void stl_flowcontrol(stlport_t *portp, int hw, int sw)
2266 {
2267         unsigned char   *head, *tail;
2268         int             len, hwflow, x;
2269
2270 #if DEBUG
2271         printf("stl_flowcontrol(portp=%x,hw=%d,sw=%d)\n", (int) portp, hw, sw);
2272 #endif
2273
2274         hwflow = -1;
2275
2276         if (portp->state & ASY_RTSFLOWMODE) {
2277                 if (hw == 0) {
2278                         if ((portp->state & ASY_RTSFLOW) == 0)
2279                                 hwflow = 0;
2280                 } else if (hw > 0) {
2281                         if (portp->state & ASY_RTSFLOW) {
2282                                 head = portp->rx.head;
2283                                 tail = portp->rx.tail;
2284                                 len = (head >= tail) ? (head - tail) :
2285                                         (STL_RXBUFSIZE - (tail - head));
2286                                 if (len < STL_RXBUFHIGH)
2287                                         hwflow = 1;
2288                         }
2289                 }
2290         }
2291
2292 /*
2293  *      We have worked out what to do, if anything. So now apply it to the
2294  *      UART port.
2295  */
2296         if (hwflow >= 0) {
2297                 x = spltty();
2298                 BRDENABLE(portp->brdnr, portp->pagenr);
2299                 stl_setreg(portp, CAR, (portp->portnr & 0x03));
2300                 if (hwflow == 0) {
2301                         portp->state |= ASY_RTSFLOW;
2302                         stl_setreg(portp, MCOR1,
2303                                 (stl_getreg(portp, MCOR1) & 0xf0));
2304                         stl_setreg(portp, MSVR2, 0);
2305                         portp->stats.rxrtsoff++;
2306                 } else if (hwflow > 0) {
2307                         portp->state &= ~ASY_RTSFLOW;
2308                         stl_setreg(portp, MSVR2, MSVR2_RTS);
2309                         stl_setreg(portp, MCOR1,
2310                                 (stl_getreg(portp, MCOR1) | FIFO_RTSTHRESHOLD));
2311                         portp->stats.rxrtson++;
2312                 }
2313                 BRDDISABLE(portp->brdnr);
2314                 splx(x);
2315         }
2316 }
2317
2318
2319 /*****************************************************************************/
2320
2321 /*
2322  *      Set the state of the DTR and RTS signals.
2323  */
2324
2325 static void stl_setsignals(stlport_t *portp, int dtr, int rts)
2326 {
2327         unsigned char   msvr1, msvr2;
2328         int             x;
2329
2330 #if DEBUG
2331         printf("stl_setsignals(portp=%x,dtr=%d,rts=%d)\n", (int) portp,
2332                 dtr, rts);
2333 #endif
2334
2335         msvr1 = 0;
2336         msvr2 = 0;
2337         if (dtr > 0)
2338                 msvr1 = MSVR1_DTR;
2339         if (rts > 0)
2340                 msvr2 = MSVR2_RTS;
2341
2342         x = spltty();
2343         BRDENABLE(portp->brdnr, portp->pagenr);
2344         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2345         if (rts >= 0)
2346                 stl_setreg(portp, MSVR2, msvr2);
2347         if (dtr >= 0)
2348                 stl_setreg(portp, MSVR1, msvr1);
2349         BRDDISABLE(portp->brdnr);
2350         splx(x);
2351 }
2352
2353 /*****************************************************************************/
2354
2355 /*
2356  *      Get the state of the signals.
2357  */
2358
2359 static int stl_getsignals(stlport_t *portp)
2360 {
2361         unsigned char   msvr1, msvr2;
2362         int             sigs, x;
2363
2364 #if DEBUG
2365         printf("stl_getsignals(portp=%x)\n", (int) portp);
2366 #endif
2367
2368         x = spltty();
2369         BRDENABLE(portp->brdnr, portp->pagenr);
2370         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2371         msvr1 = stl_getreg(portp, MSVR1);
2372         msvr2 = stl_getreg(portp, MSVR2);
2373         BRDDISABLE(portp->brdnr);
2374         splx(x);
2375
2376         sigs = 0;
2377         sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
2378         sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
2379         sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
2380         sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
2381         sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
2382         sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
2383         return(sigs);
2384 }
2385
2386 /*****************************************************************************/
2387
2388 /*
2389  *      Enable or disable the Transmitter and/or Receiver.
2390  */
2391
2392 static void stl_enablerxtx(stlport_t *portp, int rx, int tx)
2393 {
2394         unsigned char   ccr;
2395         int             x;
2396
2397 #if DEBUG
2398         printf("stl_enablerxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2399 #endif
2400
2401         ccr = 0;
2402         if (tx == 0)
2403                 ccr |= CCR_TXDISABLE;
2404         else if (tx > 0)
2405                 ccr |= CCR_TXENABLE;
2406         if (rx == 0)
2407                 ccr |= CCR_RXDISABLE;
2408         else if (rx > 0)
2409                 ccr |= CCR_RXENABLE;
2410
2411         x = spltty();
2412         BRDENABLE(portp->brdnr, portp->pagenr);
2413         stl_setreg(portp, CAR, (portp->portnr & 0x03));
2414         stl_ccrwait(portp);
2415         stl_setreg(portp, CCR, ccr);
2416         stl_ccrwait(portp);
2417         BRDDISABLE(portp->brdnr);
2418         splx(x);
2419 }
2420
2421 /*****************************************************************************/
2422
2423 /*
2424  *      Start or stop the Transmitter and/or Receiver.
2425  */
2426
2427 static void stl_startrxtx(stlport_t *portp, int rx, int tx)
2428 {
2429         unsigned char   sreron, sreroff;
2430         int             x;
2431
2432 #if DEBUG
2433         printf("stl_startrxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2434 #endif
2435
2436         sreron = 0;
2437         sreroff = 0;
2438         if (tx == 0)
2439                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
2440         else if (tx == 1)
2441                 sreron |= SRER_TXDATA;
2442         else if (tx >= 2)
2443                 sreron |= SRER_TXEMPTY;
2444         if (rx == 0)
2445                 sreroff |= SRER_RXDATA;
2446         else if (rx > 0)
2447                 sreron |= SRER_RXDATA;
2448
2449         x = spltty();
2450         BRDENABLE(portp->brdnr, portp->pagenr);
2451         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2452         stl_setreg(portp, SRER,
2453                 ((stl_getreg(portp, SRER) & ~sreroff) | sreron));
2454         BRDDISABLE(portp->brdnr);
2455         if (tx > 0)
2456                 portp->tty.t_state |= TS_BUSY;
2457         splx(x);
2458 }
2459
2460 /*****************************************************************************/
2461
2462 /*
2463  *      Disable all interrupts from this port.
2464  */
2465
2466 static void stl_disableintrs(stlport_t *portp)
2467 {
2468         int     x;
2469
2470 #if DEBUG
2471         printf("stl_disableintrs(portp=%x)\n", (int) portp);
2472 #endif
2473
2474         x = spltty();
2475         BRDENABLE(portp->brdnr, portp->pagenr);
2476         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2477         stl_setreg(portp, SRER, 0);
2478         BRDDISABLE(portp->brdnr);
2479         splx(x);
2480 }
2481
2482 /*****************************************************************************/
2483
2484 static void stl_sendbreak(stlport_t *portp, long len)
2485 {
2486         int     x;
2487
2488 #if DEBUG
2489         printf("stl_sendbreak(portp=%x,len=%d)\n", (int) portp, (int) len);
2490 #endif
2491
2492         x = spltty();
2493         BRDENABLE(portp->brdnr, portp->pagenr);
2494         stl_setreg(portp, CAR, (portp->portnr & 0x3));
2495         stl_setreg(portp, COR2, (stl_getreg(portp, COR2) | COR2_ETC));
2496         stl_setreg(portp, SRER,
2497                 ((stl_getreg(portp, SRER) & ~SRER_TXDATA) | SRER_TXEMPTY));
2498         BRDDISABLE(portp->brdnr);
2499         if (len > 0) {
2500                 len = len / 5;
2501                 portp->brklen = (len > 255) ? 255 : len;
2502         } else {
2503                 portp->brklen = len;
2504         }
2505         splx(x);
2506         portp->stats.txbreaks++;
2507 }
2508
2509 /*****************************************************************************/
2510
2511 /*
2512  *      Enable l_rint processing bypass mode if tty modes allow it.
2513  */
2514
2515 static void stl_ttyoptim(stlport_t *portp, struct termios *tiosp)
2516 {
2517         struct tty      *tp;
2518
2519         tp = &portp->tty;
2520         if (((tiosp->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR)) == 0) &&
2521             (((tiosp->c_iflag & BRKINT) == 0) || (tiosp->c_iflag & IGNBRK)) &&
2522             (((tiosp->c_iflag & PARMRK) == 0) ||
2523                 ((tiosp->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))) &&
2524             ((tiosp->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) ==0) &&
2525             (linesw[tp->t_line].l_rint == ttyinput))
2526                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2527         else
2528                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2529         portp->hotchar = linesw[tp->t_line].l_hotchar;
2530 }
2531
2532 /*****************************************************************************/
2533
2534 /*
2535  *      Try and find and initialize all the ports on a panel. We don't care
2536  *      what sort of board these ports are on - since the port io registers
2537  *      are almost identical when dealing with ports.
2538  */
2539
2540 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2541 {
2542         stlport_t       *portp;
2543         unsigned int    chipmask;
2544         unsigned int    gfrcr;
2545         int             nrchips, uartaddr, ioaddr;
2546         int             i, j;
2547
2548 #if DEBUG
2549         printf("stl_initports(panelp=%x)\n", (int) panelp);
2550 #endif
2551
2552         BRDENABLE(panelp->brdnr, panelp->pagenr);
2553
2554 /*
2555  *      Check that each chip is present and started up OK.
2556  */
2557         chipmask = 0;
2558         nrchips = panelp->nrports / CD1400_PORTS;
2559         for (i = 0; (i < nrchips); i++) {
2560                 if (brdp->brdtype == BRD_ECHPCI) {
2561                         outb(brdp->ioctrl, (panelp->pagenr + (i >> 1)));
2562                         ioaddr = panelp->iobase;
2563                 } else {
2564                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
2565                 }
2566                 uartaddr = (i & 0x01) ? 0x080 : 0;
2567                 outb(ioaddr, (GFRCR + uartaddr));
2568                 outb((ioaddr + EREG_DATA), 0);
2569                 outb(ioaddr, (CCR + uartaddr));
2570                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
2571                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
2572                 outb(ioaddr, (GFRCR + uartaddr));
2573                 for (j = 0; (j < CCR_MAXWAIT); j++) {
2574                         gfrcr = inb(ioaddr + EREG_DATA);
2575                         if ((gfrcr > 0x40) && (gfrcr < 0x60))
2576                                 break;
2577                 }
2578                 if (j >= CCR_MAXWAIT) {
2579                         printf("STALLION: cd1400 not responding, brd=%d "
2580                                 "panel=%d chip=%d\n", panelp->brdnr,
2581                                 panelp->panelnr, i);
2582                         continue;
2583                 }
2584                 chipmask |= (0x1 << i);
2585                 outb(ioaddr, (PPR + uartaddr));
2586                 outb((ioaddr + EREG_DATA), PPR_SCALAR);
2587         }
2588
2589 /*
2590  *      All cd1400's are initialized (if found!). Now go through and setup
2591  *      each ports data structures. Also init the LIVR register of cd1400
2592  *      for each port.
2593  */
2594         ioaddr = panelp->iobase;
2595         for (i = 0; (i < panelp->nrports); i++) {
2596                 if (brdp->brdtype == BRD_ECHPCI) {
2597                         outb(brdp->ioctrl, (panelp->pagenr + (i >> 3)));
2598                         ioaddr = panelp->iobase;
2599                 } else {
2600                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 3));
2601                 }
2602                 if ((chipmask & (0x1 << (i / 4))) == 0)
2603                         continue;
2604                 portp = (stlport_t *) malloc(sizeof(stlport_t), M_TTYS,
2605                         M_NOWAIT | M_ZERO);
2606                 if (portp == (stlport_t *) NULL) {
2607                         printf("STALLION: failed to allocate port memory "
2608                                 "(size=%d)\n", sizeof(stlport_t));
2609                         break;
2610                 }
2611
2612                 portp->portnr = i;
2613                 portp->brdnr = panelp->brdnr;
2614                 portp->panelnr = panelp->panelnr;
2615                 portp->clk = brdp->clk;
2616                 portp->ioaddr = ioaddr;
2617                 portp->uartaddr = (i & 0x4) << 5;
2618                 portp->pagenr = panelp->pagenr + (i >> 3);
2619                 portp->hwid = stl_getreg(portp, GFRCR);
2620                 stl_setreg(portp, CAR, (i & 0x3));
2621                 stl_setreg(portp, LIVR, (i << 3));
2622                 panelp->ports[i] = portp;
2623
2624                 j = STL_TXBUFSIZE + (2 * STL_RXBUFSIZE);
2625                 portp->tx.buf = (char *) malloc(j, M_TTYS, M_NOWAIT);
2626                 if (portp->tx.buf == (char *) NULL) {
2627                         printf("STALLION: failed to allocate buffer memory "
2628                                 "(size=%d)\n", j);
2629                         break;
2630                 }
2631                 portp->tx.endbuf = portp->tx.buf + STL_TXBUFSIZE;
2632                 portp->tx.head = portp->tx.buf;
2633                 portp->tx.tail = portp->tx.buf;
2634                 portp->rx.buf = portp->tx.buf + STL_TXBUFSIZE;
2635                 portp->rx.endbuf = portp->rx.buf + STL_RXBUFSIZE;
2636                 portp->rx.head = portp->rx.buf;
2637                 portp->rx.tail = portp->rx.buf;
2638                 portp->rxstatus.buf = portp->rx.buf + STL_RXBUFSIZE;
2639                 portp->rxstatus.endbuf = portp->rxstatus.buf + STL_RXBUFSIZE;
2640                 portp->rxstatus.head = portp->rxstatus.buf;
2641                 portp->rxstatus.tail = portp->rxstatus.buf;
2642                 bzero(portp->rxstatus.head, STL_RXBUFSIZE);
2643
2644                 portp->initintios.c_ispeed = STL_DEFSPEED;
2645                 portp->initintios.c_ospeed = STL_DEFSPEED;
2646                 portp->initintios.c_cflag = STL_DEFCFLAG;
2647                 portp->initintios.c_iflag = 0;
2648                 portp->initintios.c_oflag = 0;
2649                 portp->initintios.c_lflag = 0;
2650                 bcopy(&ttydefchars[0], &portp->initintios.c_cc[0],
2651                         sizeof(portp->initintios.c_cc));
2652                 portp->initouttios = portp->initintios;
2653                 portp->dtrwait = 3 * hz;
2654         }
2655
2656         BRDDISABLE(panelp->brdnr);
2657         return(0);
2658 }
2659
2660 /*****************************************************************************/
2661
2662 /*
2663  *      Try to find and initialize an EasyIO board.
2664  */
2665
2666 static int stl_initeio(stlbrd_t *brdp)
2667 {
2668         stlpanel_t      *panelp;
2669         unsigned int    status;
2670
2671 #if DEBUG
2672         printf("stl_initeio(brdp=%x)\n", (int) brdp);
2673 #endif
2674
2675         brdp->ioctrl = brdp->ioaddr1 + 1;
2676         brdp->iostatus = brdp->ioaddr1 + 2;
2677         brdp->clk = EIO_CLK;
2678
2679         status = inb(brdp->iostatus);
2680         switch (status & EIO_IDBITMASK) {
2681         case EIO_8PORTM:
2682                 brdp->clk = EIO_CLK8M;
2683                 /* fall thru */
2684         case EIO_8PORTRS:
2685         case EIO_8PORTDI:
2686                 brdp->nrports = 8;
2687                 break;
2688         case EIO_4PORTRS:
2689                 brdp->nrports = 4;
2690                 break;
2691         default:
2692                 return(ENODEV);
2693         }
2694
2695 /*
2696  *      Check that the supplied IRQ is good and then use it to setup the
2697  *      programmable interrupt bits on EIO board. Also set the edge/level
2698  *      triggered interrupt bit.
2699  */
2700         if ((brdp->irq < 0) || (brdp->irq > 15) ||
2701                         (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2702                 printf("STALLION: invalid irq=%d for brd=%d\n", brdp->irq,
2703                         brdp->brdnr);
2704                 return(EINVAL);
2705         }
2706         outb(brdp->ioctrl, (stl_vecmap[brdp->irq] |
2707                 ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)));
2708
2709         panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS,
2710                 M_NOWAIT | M_ZERO);
2711         if (panelp == (stlpanel_t *) NULL) {
2712                 printf("STALLION: failed to allocate memory (size=%d)\n",
2713                         sizeof(stlpanel_t));
2714                 return(ENOMEM);
2715         }
2716
2717         panelp->brdnr = brdp->brdnr;
2718         panelp->panelnr = 0;
2719         panelp->nrports = brdp->nrports;
2720         panelp->iobase = brdp->ioaddr1;
2721         panelp->hwid = status;
2722         brdp->panels[0] = panelp;
2723         brdp->nrpanels = 1;
2724         brdp->hwid = status;
2725         brdp->state |= BRD_FOUND;
2726         return(0);
2727 }
2728
2729 /*****************************************************************************/
2730
2731 /*
2732  *      Try to find an ECH board and initialize it. This code is capable of
2733  *      dealing with all types of ECH board.
2734  */
2735
2736 static int stl_initech(stlbrd_t *brdp)
2737 {
2738         stlpanel_t      *panelp;
2739         unsigned int    status, nxtid;
2740         int             panelnr, ioaddr, i;
2741
2742 #if DEBUG
2743         printf("stl_initech(brdp=%x)\n", (int) brdp);
2744 #endif
2745
2746 /*
2747  *      Set up the initial board register contents for boards. This varys a
2748  *      bit between the different board types. So we need to handle each
2749  *      separately. Also do a check that the supplied IRQ is good.
2750  */
2751         if (brdp->brdtype == BRD_ECH) {
2752                 brdp->ioctrl = brdp->ioaddr1 + 1;
2753                 brdp->iostatus = brdp->ioaddr1 + 1;
2754                 status = inb(brdp->iostatus);
2755                 if ((status & ECH_IDBITMASK) != ECH_ID)
2756                         return(ENODEV);
2757                 brdp->hwid = status;
2758
2759                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2760                                 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2761                         printf("STALLION: invalid irq=%d for brd=%d\n",
2762                                 brdp->irq, brdp->brdnr);
2763                         return(EINVAL);
2764                 }
2765                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2766                 status |= (stl_vecmap[brdp->irq] << 1);
2767                 outb(brdp->ioaddr1, (status | ECH_BRDRESET));
2768                 brdp->ioctrlval = ECH_INTENABLE |
2769                         ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2770                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
2771                 outb(brdp->ioaddr1, status);
2772         } else if (brdp->brdtype == BRD_ECHMC) {
2773                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2774                 brdp->iostatus = brdp->ioctrl;
2775                 status = inb(brdp->iostatus);
2776                 if ((status & ECH_IDBITMASK) != ECH_ID)
2777                         return(ENODEV);
2778                 brdp->hwid = status;
2779
2780                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2781                                 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2782                         printf("STALLION: invalid irq=%d for brd=%d\n",
2783                                 brdp->irq, brdp->brdnr);
2784                         return(EINVAL);
2785                 }
2786                 outb(brdp->ioctrl, ECHMC_BRDRESET);
2787                 outb(brdp->ioctrl, ECHMC_INTENABLE);
2788         } else if (brdp->brdtype == BRD_ECHPCI) {
2789                 brdp->ioctrl = brdp->ioaddr1 + 2;
2790         }
2791
2792         brdp->clk = ECH_CLK;
2793
2794 /*
2795  *      Scan through the secondary io address space looking for panels.
2796  *      As we find'em allocate and initialize panel structures for each.
2797  */
2798         ioaddr = brdp->ioaddr2;
2799         panelnr = 0;
2800         nxtid = 0;
2801
2802         for (i = 0; (i < STL_MAXPANELS); i++) {
2803                 if (brdp->brdtype == BRD_ECHPCI) {
2804                         outb(brdp->ioctrl, nxtid);
2805                         ioaddr = brdp->ioaddr2;
2806                 }
2807                 status = inb(ioaddr + ECH_PNLSTATUS);
2808                 if ((status & ECH_PNLIDMASK) != nxtid)
2809                         break;
2810                 panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS,
2811                         M_NOWAIT | M_ZERO);
2812                 if (panelp == (stlpanel_t *) NULL) {
2813                         printf("STALLION: failed to allocate memory"
2814                                 "(size=%d)\n", sizeof(stlpanel_t));
2815                         break;
2816                 }
2817                 panelp->brdnr = brdp->brdnr;
2818                 panelp->panelnr = panelnr;
2819                 panelp->iobase = ioaddr;
2820                 panelp->pagenr = nxtid;
2821                 panelp->hwid = status;
2822                 if (status & ECH_PNL16PORT) {
2823                         if ((brdp->nrports + 16) > 32)
2824                                 break;
2825                         panelp->nrports = 16;
2826                         panelp->ackmask = 0x80;
2827                         brdp->nrports += 16;
2828                         ioaddr += (EREG_BANKSIZE * 2);
2829                         nxtid += 2;
2830                 } else {
2831                         panelp->nrports = 8;
2832                         panelp->ackmask = 0xc0;
2833                         brdp->nrports += 8;
2834                         ioaddr += EREG_BANKSIZE;
2835                         nxtid++;
2836                 }
2837                 brdp->panels[panelnr++] = panelp;
2838                 brdp->nrpanels++;
2839                 if (ioaddr >= (brdp->ioaddr2 + 0x20))
2840                         break;
2841         }
2842
2843         if (brdp->brdtype == BRD_ECH)
2844                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
2845
2846         brdp->state |= BRD_FOUND;
2847         return(0);
2848 }
2849
2850 /*****************************************************************************/
2851
2852 /*
2853  *      Initialize and configure the specified board. This firstly probes
2854  *      for the board, if it is found then the board is initialized and
2855  *      then all its ports are initialized as well.
2856  */
2857
2858 static int stl_brdinit(stlbrd_t *brdp)
2859 {
2860         stlpanel_t      *panelp;
2861         int             i, j, k;
2862
2863 #if DEBUG
2864         printf("stl_brdinit(brdp=%x): unit=%d type=%d io1=%x io2=%x irq=%d\n",
2865                 (int) brdp, brdp->brdnr, brdp->brdtype, brdp->ioaddr1,
2866                 brdp->ioaddr2, brdp->irq);
2867 #endif
2868
2869         switch (brdp->brdtype) {
2870         case BRD_EASYIO:
2871                 stl_initeio(brdp);
2872                 break;
2873         case BRD_ECH:
2874         case BRD_ECHMC:
2875         case BRD_ECHPCI:
2876                 stl_initech(brdp);
2877                 break;
2878         default:
2879                 printf("STALLION: unit=%d is unknown board type=%d\n",
2880                         brdp->brdnr, brdp->brdtype);
2881                 return(ENODEV);
2882         }
2883
2884         stl_brds[brdp->brdnr] = brdp;
2885         if ((brdp->state & BRD_FOUND) == 0) {
2886 #if 0
2887                 printf("STALLION: %s board not found, unit=%d io=%x irq=%d\n",
2888                         stl_brdnames[brdp->brdtype], brdp->brdnr,
2889                         brdp->ioaddr1, brdp->irq);
2890 #endif
2891                 return(ENODEV);
2892         }
2893
2894         for (i = 0, k = 0; (i < STL_MAXPANELS); i++) {
2895                 panelp = brdp->panels[i];
2896                 if (panelp != (stlpanel_t *) NULL) {
2897                         stl_initports(brdp, panelp);
2898                         for (j = 0; (j < panelp->nrports); j++)
2899                                 brdp->ports[k++] = panelp->ports[j];
2900                 }
2901         }
2902
2903         printf("stl%d: %s (driver version %s) unit=%d nrpanels=%d nrports=%d\n",
2904                 brdp->unitid, stl_brdnames[brdp->brdtype], stl_drvversion,
2905                 brdp->brdnr, brdp->nrpanels, brdp->nrports);
2906         return(0);
2907 }
2908
2909 /*****************************************************************************/
2910
2911 /*
2912  *      Return the board stats structure to user app.
2913  */
2914
2915 static int stl_getbrdstats(caddr_t data)
2916 {
2917         stlbrd_t        *brdp;
2918         stlpanel_t      *panelp;
2919         int             i;
2920
2921         stl_brdstats = *((combrd_t *) data);
2922         if (stl_brdstats.brd >= STL_MAXBRDS)
2923                 return(-ENODEV);
2924         brdp = stl_brds[stl_brdstats.brd];
2925         if (brdp == (stlbrd_t *) NULL)
2926                 return(-ENODEV);
2927
2928         bzero(&stl_brdstats, sizeof(combrd_t));
2929         stl_brdstats.brd = brdp->brdnr;
2930         stl_brdstats.type = brdp->brdtype;
2931         stl_brdstats.hwid = brdp->hwid;
2932         stl_brdstats.state = brdp->state;
2933         stl_brdstats.ioaddr = brdp->ioaddr1;
2934         stl_brdstats.ioaddr2 = brdp->ioaddr2;
2935         stl_brdstats.irq = brdp->irq;
2936         stl_brdstats.nrpanels = brdp->nrpanels;
2937         stl_brdstats.nrports = brdp->nrports;
2938         for (i = 0; (i < brdp->nrpanels); i++) {
2939                 panelp = brdp->panels[i];
2940                 stl_brdstats.panels[i].panel = i;
2941                 stl_brdstats.panels[i].hwid = panelp->hwid;
2942                 stl_brdstats.panels[i].nrports = panelp->nrports;
2943         }
2944
2945         *((combrd_t *) data) = stl_brdstats;;
2946         return(0);
2947 }
2948
2949 /*****************************************************************************/
2950
2951 /*
2952  *      Resolve the referenced port number into a port struct pointer.
2953  */
2954
2955 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2956 {
2957         stlbrd_t        *brdp;
2958         stlpanel_t      *panelp;
2959
2960         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2961                 return((stlport_t *) NULL);
2962         brdp = stl_brds[brdnr];
2963         if (brdp == (stlbrd_t *) NULL)
2964                 return((stlport_t *) NULL);
2965         if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2966                 return((stlport_t *) NULL);
2967         panelp = brdp->panels[panelnr];
2968         if (panelp == (stlpanel_t *) NULL)
2969                 return((stlport_t *) NULL);
2970         if ((portnr < 0) || (portnr >= panelp->nrports))
2971                 return((stlport_t *) NULL);
2972         return(panelp->ports[portnr]);
2973 }
2974
2975 /*****************************************************************************/
2976
2977 /*
2978  *      Return the port stats structure to user app. A NULL port struct
2979  *      pointer passed in means that we need to find out from the app
2980  *      what port to get stats for (used through board control device).
2981  */
2982
2983 static int stl_getportstats(stlport_t *portp, caddr_t data)
2984 {
2985         unsigned char   *head, *tail;
2986
2987         if (portp == (stlport_t *) NULL) {
2988                 stl_comstats = *((comstats_t *) data);
2989                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2990                         stl_comstats.port);
2991                 if (portp == (stlport_t *) NULL)
2992                         return(-ENODEV);
2993         }
2994
2995         portp->stats.state = portp->state;
2996         /*portp->stats.flags = portp->flags;*/
2997         portp->stats.hwid = portp->hwid;
2998         portp->stats.ttystate = portp->tty.t_state;
2999         portp->stats.cflags = portp->tty.t_cflag;
3000         portp->stats.iflags = portp->tty.t_iflag;
3001         portp->stats.oflags = portp->tty.t_oflag;
3002         portp->stats.lflags = portp->tty.t_lflag;
3003
3004         head = portp->tx.head;
3005         tail = portp->tx.tail;
3006         portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
3007                 (STL_TXBUFSIZE - (tail - head)));
3008
3009         head = portp->rx.head;
3010         tail = portp->rx.tail;
3011         portp->stats.rxbuffered = (head >= tail) ? (head - tail) :
3012                 (STL_RXBUFSIZE - (tail - head));
3013
3014         portp->stats.signals = (unsigned long) stl_getsignals(portp);
3015
3016         *((comstats_t *) data) = portp->stats;
3017         return(0);
3018 }
3019
3020 /*****************************************************************************/
3021
3022 /*
3023  *      Clear the port stats structure. We also return it zeroed out...
3024  */
3025
3026 static int stl_clrportstats(stlport_t *portp, caddr_t data)
3027 {
3028         if (portp == (stlport_t *) NULL) {
3029                 stl_comstats = *((comstats_t *) data);
3030                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
3031                         stl_comstats.port);
3032                 if (portp == (stlport_t *) NULL)
3033                         return(-ENODEV);
3034         }
3035
3036         bzero(&portp->stats, sizeof(comstats_t));
3037         portp->stats.brd = portp->brdnr;
3038         portp->stats.panel = portp->panelnr;
3039         portp->stats.port = portp->portnr;
3040         *((comstats_t *) data) = stl_comstats;
3041         return(0);
3042 }
3043
3044 /*****************************************************************************/
3045
3046 /*
3047  *      The "staliomem" device is used for stats collection in this driver.
3048  */
3049
3050 static int stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
3051                         struct proc *p)
3052 {
3053         stlbrd_t        *brdp;
3054         int             brdnr, rc;
3055
3056 #if DEBUG
3057         printf("stl_memioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
3058                 devtoname(dev), cmd, (void *) data, flag);
3059 #endif
3060
3061         brdnr = minor(dev) & 0x7;
3062         brdp = stl_brds[brdnr];
3063         if (brdp == (stlbrd_t *) NULL)
3064                 return(ENODEV);
3065         if (brdp->state == 0)
3066                 return(ENODEV);
3067
3068         rc = 0;
3069
3070         switch (cmd) {
3071         case COM_GETPORTSTATS:
3072                 rc = stl_getportstats((stlport_t *) NULL, data);
3073                 break;
3074         case COM_CLRPORTSTATS:
3075                 rc = stl_clrportstats((stlport_t *) NULL, data);
3076                 break;
3077         case COM_GETBRDSTATS:
3078                 rc = stl_getbrdstats(data);
3079                 break;
3080         default:
3081                 rc = ENOTTY;
3082                 break;
3083         }
3084
3085         return(rc);
3086 }
3087
3088 /*****************************************************************************/