The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/stallion.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: a7ca692e570d1a2852c20c7f403acfbf


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.