Home | History | Annotate | Line # | Download | only in dev
dcm.c revision 1.23
      1  1.23  thorpej /*	$NetBSD: dcm.c,v 1.23 1996/02/14 02:44:11 thorpej Exp $	*/
      2  1.15      cgd 
      3   1.1      cgd /*
      4  1.20  thorpej  * Copyright (c) 1995 Jason R. Thorpe.  All rights reserved.
      5   1.1      cgd  * Copyright (c) 1988 University of Utah.
      6  1.14  mycroft  * Copyright (c) 1982, 1986, 1990, 1993
      7  1.14  mycroft  *	The Regents of the University of California.  All rights reserved.
      8   1.1      cgd  *
      9   1.1      cgd  * This code is derived from software contributed to Berkeley by
     10   1.1      cgd  * the Systems Programming Group of the University of Utah Computer
     11   1.1      cgd  * Science Department.
     12   1.1      cgd  *
     13   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     14   1.1      cgd  * modification, are permitted provided that the following conditions
     15   1.1      cgd  * are met:
     16   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     17   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     18   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     19   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     20   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     21   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     22   1.1      cgd  *    must display the following acknowledgement:
     23   1.1      cgd  *	This product includes software developed by the University of
     24   1.1      cgd  *	California, Berkeley and its contributors.
     25   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     26   1.1      cgd  *    may be used to endorse or promote products derived from this software
     27   1.1      cgd  *    without specific prior written permission.
     28   1.1      cgd  *
     29   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39   1.1      cgd  * SUCH DAMAGE.
     40   1.1      cgd  *
     41  1.14  mycroft  * from Utah: $Hdr: dcm.c 1.29 92/01/21$
     42  1.14  mycroft  *
     43  1.15      cgd  *	@(#)dcm.c	8.4 (Berkeley) 1/12/94
     44   1.1      cgd  */
     45   1.1      cgd 
     46   1.1      cgd /*
     47   1.1      cgd  * TODO:
     48   1.1      cgd  *	Timeouts
     49   1.1      cgd  *	Test console support.
     50   1.1      cgd  */
     51   1.1      cgd 
     52   1.1      cgd #include "dcm.h"
     53   1.1      cgd #if NDCM > 0
     54   1.1      cgd /*
     55   1.1      cgd  *  98642/MUX
     56   1.1      cgd  */
     57  1.13  mycroft #include <sys/param.h>
     58  1.13  mycroft #include <sys/systm.h>
     59  1.13  mycroft #include <sys/ioctl.h>
     60  1.13  mycroft #include <sys/proc.h>
     61  1.13  mycroft #include <sys/tty.h>
     62  1.13  mycroft #include <sys/conf.h>
     63  1.13  mycroft #include <sys/file.h>
     64  1.13  mycroft #include <sys/uio.h>
     65  1.13  mycroft #include <sys/kernel.h>
     66  1.13  mycroft #include <sys/syslog.h>
     67  1.13  mycroft #include <sys/time.h>
     68  1.13  mycroft 
     69  1.14  mycroft #include <machine/cpu.h>
     70  1.14  mycroft 
     71  1.22  thorpej #include <dev/cons.h>
     72  1.22  thorpej 
     73  1.13  mycroft #include <hp300/dev/device.h>
     74  1.13  mycroft #include <hp300/dev/dcmreg.h>
     75  1.13  mycroft #include <hp300/hp300/isr.h>
     76   1.1      cgd 
     77   1.1      cgd #ifndef DEFAULT_BAUD_RATE
     78   1.1      cgd #define DEFAULT_BAUD_RATE 9600
     79   1.1      cgd #endif
     80   1.1      cgd 
     81  1.23  thorpej int	dcmmatch(), dcmparam();
     82  1.20  thorpej void	dcmattach(), dcmstart();
     83   1.1      cgd struct	driver dcmdriver = {
     84  1.20  thorpej 	dcmmatch, dcmattach, "dcm",
     85   1.1      cgd };
     86   1.1      cgd 
     87   1.1      cgd struct speedtab dcmspeedtab[] = {
     88   1.1      cgd 	0,	BR_0,
     89   1.1      cgd 	50,	BR_50,
     90   1.1      cgd 	75,	BR_75,
     91   1.1      cgd 	110,	BR_110,
     92   1.1      cgd 	134,	BR_134,
     93   1.1      cgd 	150,	BR_150,
     94   1.1      cgd 	300,	BR_300,
     95   1.1      cgd 	600,	BR_600,
     96   1.1      cgd 	1200,	BR_1200,
     97   1.1      cgd 	1800,	BR_1800,
     98   1.1      cgd 	2400,	BR_2400,
     99   1.1      cgd 	4800,	BR_4800,
    100   1.1      cgd 	9600,	BR_9600,
    101   1.1      cgd 	19200,	BR_19200,
    102   1.1      cgd 	38400,	BR_38400,
    103   1.1      cgd 	-1,	-1
    104   1.1      cgd };
    105   1.1      cgd 
    106   1.1      cgd /* u-sec per character based on baudrate (assumes 1 start/8 data/1 stop bit) */
    107   1.1      cgd #define	DCM_USPERCH(s)	(10000000 / (s))
    108   1.1      cgd 
    109   1.1      cgd /*
    110   1.1      cgd  * Per board interrupt scheme.  16.7ms is the polling interrupt rate
    111   1.1      cgd  * (16.7ms is about 550 baud, 38.4k is 72 chars in 16.7ms).
    112   1.1      cgd  */
    113   1.1      cgd #define DIS_TIMER	0
    114   1.1      cgd #define DIS_PERCHAR	1
    115   1.1      cgd #define DIS_RESET	2
    116   1.1      cgd 
    117   1.1      cgd int	dcmistype = -1;		/* -1 == dynamic, 0 == timer, 1 == perchar */
    118   1.1      cgd int     dcminterval = 5;	/* interval (secs) between checks */
    119   1.1      cgd struct	dcmischeme {
    120   1.1      cgd 	int	dis_perchar;	/* non-zero if interrupting per char */
    121   1.1      cgd 	long	dis_time;	/* last time examined */
    122   1.1      cgd 	int	dis_intr;	/* recv interrupts during last interval */
    123   1.1      cgd 	int	dis_char;	/* characters read during last interval */
    124  1.20  thorpej };
    125   1.1      cgd 
    126   1.1      cgd /*
    127  1.22  thorpej  * Stuff for DCM console support.  This could probably be done a little
    128  1.22  thorpej  * better.
    129   1.1      cgd  */
    130  1.22  thorpej static	struct dcmdevice *dcm_cn = NULL;	/* pointer to hardware */
    131  1.22  thorpej static	int dcm_lastcnpri = CN_DEAD;		/* XXX last priority */
    132  1.22  thorpej static	int dcmconsinit;			/* has been initialized */
    133   1.1      cgd #ifdef DCMCONSOLE
    134  1.22  thorpej static	int dcmconsole = DCMCONSOLE;
    135   1.1      cgd #else
    136  1.22  thorpej static	int dcmconsole = -1;
    137   1.1      cgd #endif
    138  1.22  thorpej 
    139   1.1      cgd int	dcmdefaultrate = DEFAULT_BAUD_RATE;
    140   1.1      cgd int	dcmconbrdbusy = 0;
    141   1.1      cgd int	dcmmajor;
    142   1.1      cgd 
    143   1.1      cgd #ifdef KGDB
    144   1.1      cgd /*
    145   1.1      cgd  * Kernel GDB support
    146   1.1      cgd  */
    147  1.14  mycroft #include <machine/remote-sl.h>
    148   1.1      cgd 
    149   1.1      cgd extern dev_t kgdb_dev;
    150   1.1      cgd extern int kgdb_rate;
    151   1.1      cgd extern int kgdb_debug_init;
    152   1.1      cgd #endif
    153   1.1      cgd 
    154  1.14  mycroft /* #define DCMSTATS */
    155   1.1      cgd 
    156   1.1      cgd #ifdef DEBUG
    157   1.1      cgd int	dcmdebug = 0x0;
    158   1.1      cgd #define DDB_SIOERR	0x01
    159   1.1      cgd #define DDB_PARAM	0x02
    160   1.1      cgd #define DDB_INPUT	0x04
    161   1.1      cgd #define DDB_OUTPUT	0x08
    162   1.1      cgd #define DDB_INTR	0x10
    163   1.1      cgd #define DDB_IOCTL	0x20
    164   1.1      cgd #define DDB_INTSCHM	0x40
    165   1.1      cgd #define DDB_MODEM	0x80
    166   1.1      cgd #define DDB_OPENCLOSE	0x100
    167   1.1      cgd #endif
    168   1.1      cgd 
    169  1.14  mycroft #ifdef DCMSTATS
    170   1.1      cgd #define	DCMRBSIZE	94
    171   1.1      cgd #define DCMXBSIZE	24
    172   1.1      cgd 
    173   1.1      cgd struct	dcmstats {
    174   1.1      cgd 	long	xints;		    /* # of xmit ints */
    175   1.1      cgd 	long	xchars;		    /* # of xmit chars */
    176   1.1      cgd 	long	xempty;		    /* times outq is empty in dcmstart */
    177   1.1      cgd 	long	xrestarts;	    /* times completed while xmitting */
    178   1.1      cgd 	long	rints;		    /* # of recv ints */
    179   1.1      cgd 	long	rchars;		    /* # of recv chars */
    180   1.1      cgd 	long	xsilo[DCMXBSIZE+2]; /* times this many chars xmit on one int */
    181   1.1      cgd 	long	rsilo[DCMRBSIZE+2]; /* times this many chars read on one int */
    182  1.20  thorpej };
    183   1.1      cgd #endif
    184   1.1      cgd 
    185  1.20  thorpej #define DCMUNIT(x)		minor(x)
    186  1.20  thorpej #define	DCMBOARD(x)		(((x) >> 2) & 0x3f)
    187  1.20  thorpej #define DCMPORT(x)		((x) & 3)
    188   1.1      cgd 
    189   1.1      cgd /*
    190   1.1      cgd  * Conversion from "HP DCE" to almost-normal DCE: on the 638 8-port mux,
    191   1.1      cgd  * the distribution panel uses "HP DCE" conventions.  If requested via
    192   1.1      cgd  * the device flags, we swap the inputs to something closer to normal DCE,
    193   1.1      cgd  * allowing a straight-through cable to a DTE or a reversed cable
    194   1.1      cgd  * to a DCE (reversing 2-3, 4-5, 8-20 and leaving 6 unconnected;
    195   1.1      cgd  * this gets "DCD" on pin 20 and "CTS" on 4, but doesn't connect
    196   1.1      cgd  * DSR or make RTS work, though).  The following gives the full
    197   1.1      cgd  * details of a cable from this mux panel to a modem:
    198   1.1      cgd  *
    199   1.1      cgd  *		     HP		    modem
    200   1.1      cgd  *		name	pin	pin	name
    201   1.1      cgd  * HP inputs:
    202   1.1      cgd  *		"Rx"	 2	 3	Tx
    203   1.1      cgd  *		CTS	 4	 5	CTS	(only needed for CCTS_OFLOW)
    204   1.1      cgd  *		DCD	20	 8	DCD
    205   1.1      cgd  *		"DSR"	 9	 6	DSR	(unneeded)
    206   1.1      cgd  *		RI	22	22	RI	(unneeded)
    207   1.1      cgd  *
    208   1.1      cgd  * HP outputs:
    209   1.1      cgd  *		"Tx"	 3	 2	Rx
    210   1.1      cgd  *		"DTR"	 6	not connected
    211   1.1      cgd  *		"RTS"	 8	20	DTR
    212   1.1      cgd  *		"SR"	23	 4	RTS	(often not needed)
    213   1.1      cgd  */
    214   1.1      cgd #define hp2dce_in(ibits)	(iconv[(ibits) & 0xf])
    215   1.1      cgd static char iconv[16] = {
    216   1.1      cgd 	0,		MI_DM,		MI_CTS,		MI_CTS|MI_DM,
    217   1.1      cgd 	MI_CD,		MI_CD|MI_DM,	MI_CD|MI_CTS,	MI_CD|MI_CTS|MI_DM,
    218   1.1      cgd 	MI_RI,		MI_RI|MI_DM,	MI_RI|MI_CTS,	MI_RI|MI_CTS|MI_DM,
    219   1.1      cgd 	MI_RI|MI_CD,	MI_RI|MI_CD|MI_DM, MI_RI|MI_CD|MI_CTS,
    220   1.1      cgd 	MI_RI|MI_CD|MI_CTS|MI_DM
    221   1.1      cgd };
    222   1.1      cgd 
    223  1.21  thorpej /*
    224  1.21  thorpej  * Note that 8-port boards appear as 2 4-port boards at consecutive
    225  1.21  thorpej  * select codes.
    226  1.21  thorpej  */
    227  1.21  thorpej #define	NDCMPORT	4
    228  1.20  thorpej 
    229  1.20  thorpej struct	dcm_softc {
    230  1.20  thorpej 	struct	hp_device *sc_hd;	/* device info */
    231  1.20  thorpej 	struct	dcmdevice *sc_dcm;	/* pointer to hardware */
    232  1.20  thorpej 	struct	tty *sc_tty[NDCMPORT];	/* our tty instances */
    233  1.20  thorpej 	struct	modemreg *sc_modem[NDCMPORT]; /* modem control */
    234  1.20  thorpej 	char	sc_mcndlast[NDCMPORT];	/* XXX last modem status for port */
    235  1.20  thorpej 	short	sc_softCAR;		/* mask of ports with soft-carrier */
    236  1.20  thorpej 	struct	dcmischeme sc_scheme;	/* interrupt scheme for board */
    237  1.20  thorpej 
    238  1.20  thorpej 	/*
    239  1.20  thorpej 	 * Mask of soft-carrier bits in config flags.
    240  1.20  thorpej 	 */
    241  1.20  thorpej #define	DCM_SOFTCAR	0x0000000f
    242  1.20  thorpej 
    243  1.20  thorpej 	int	sc_flags;		/* misc. configuration info */
    244  1.20  thorpej 
    245  1.20  thorpej 	/*
    246  1.20  thorpej 	 * Bits for sc_flags
    247  1.20  thorpej 	 */
    248  1.20  thorpej #define	DCM_ACTIVE	0x00000001	/* indicates board is alive */
    249  1.20  thorpej #define	DCM_STDDCE	0x00000010	/* re-map DCE to standard */
    250  1.20  thorpej #define	DCM_FLAGMASK	(DCM_STDDCE)	/* mask of valid bits in config flags */
    251  1.20  thorpej 
    252  1.20  thorpej #ifdef DCMSTATS
    253  1.20  thorpej 	struct	dcmstats sc_stats;	/* metrics gathering */
    254  1.20  thorpej #endif
    255  1.20  thorpej } dcm_softc[NDCM];
    256  1.20  thorpej 
    257  1.22  thorpej void	dcminit __P((struct dcmdevice *, int, int));
    258  1.23  thorpej int	dcmintr __P((void *));
    259  1.22  thorpej 
    260  1.20  thorpej int
    261  1.20  thorpej dcmmatch(hd)
    262   1.1      cgd 	register struct hp_device *hd;
    263   1.1      cgd {
    264  1.20  thorpej 	struct dcm_softc *sc = &dcm_softc[hd->hp_unit];
    265  1.20  thorpej 	struct dcmdevice *dcm;
    266  1.20  thorpej 	int i, timo = 0;
    267   1.1      cgd 	int s, brd, isconsole, mbits;
    268   1.1      cgd 
    269   1.1      cgd 	dcm = (struct dcmdevice *)hd->hp_addr;
    270   1.1      cgd 	if ((dcm->dcm_rsid & 0x1f) != DCMID)
    271   1.1      cgd 		return (0);
    272  1.20  thorpej 
    273   1.1      cgd 	brd = hd->hp_unit;
    274  1.20  thorpej 	isconsole = (brd == DCMBOARD(dcmconsole));
    275  1.20  thorpej 
    276   1.1      cgd 	/*
    277   1.1      cgd 	 * XXX selected console device (CONSUNIT) as determined by
    278   1.1      cgd 	 * dcmcnprobe does not agree with logical numbering imposed
    279   1.1      cgd 	 * by the config file (i.e. lowest address DCM is not unit
    280   1.1      cgd 	 * CONSUNIT).  Don't recognize this card.
    281   1.1      cgd 	 */
    282  1.20  thorpej 	if (isconsole && (dcm != sc->sc_dcm))
    283   1.1      cgd 		return (0);
    284   1.1      cgd 
    285  1.20  thorpej 	sc->sc_hd = hd;
    286  1.20  thorpej 	hd->hp_ipl = DCMIPL(dcm->dcm_ic);
    287  1.20  thorpej 
    288   1.1      cgd 	/*
    289   1.1      cgd 	 * Empirically derived self-test magic
    290   1.1      cgd 	 */
    291   1.1      cgd 	s = spltty();
    292   1.1      cgd 	dcm->dcm_rsid = DCMRS;
    293   1.1      cgd 	DELAY(50000);	/* 5000 is not long enough */
    294   1.1      cgd 	dcm->dcm_rsid = 0;
    295   1.1      cgd 	dcm->dcm_ic = IC_IE;
    296   1.1      cgd 	dcm->dcm_cr = CR_SELFT;
    297   1.1      cgd 	while ((dcm->dcm_ic & IC_IR) == 0)
    298   1.1      cgd 		if (++timo == 20000)
    299   1.1      cgd 			return (0);
    300   1.1      cgd 	DELAY(50000)	/* XXX why is this needed ???? */
    301   1.1      cgd 	while ((dcm->dcm_iir & IIR_SELFT) == 0)
    302   1.1      cgd 		if (++timo == 400000)
    303   1.1      cgd 			return (0);
    304   1.1      cgd 	DELAY(50000)	/* XXX why is this needed ???? */
    305   1.1      cgd 	if (dcm->dcm_stcon != ST_OK) {
    306   1.1      cgd 		if (!isconsole)
    307   1.1      cgd 			printf("dcm%d: self test failed: %x\n",
    308   1.1      cgd 			       brd, dcm->dcm_stcon);
    309   1.1      cgd 		return (0);
    310   1.1      cgd 	}
    311   1.1      cgd 	dcm->dcm_ic = IC_ID;
    312   1.1      cgd 	splx(s);
    313   1.1      cgd 
    314  1.20  thorpej 	return (1);
    315  1.20  thorpej }
    316  1.20  thorpej 
    317  1.20  thorpej void
    318  1.20  thorpej dcmattach(hd)
    319  1.20  thorpej 	register struct hp_device *hd;
    320  1.20  thorpej {
    321  1.20  thorpej 	struct dcm_softc *sc = &dcm_softc[hd->hp_unit];
    322  1.20  thorpej 	struct dcmdevice *dcm;
    323  1.20  thorpej 	int i, timo = 0;
    324  1.20  thorpej 	int s, brd, isconsole, mbits;
    325  1.20  thorpej 
    326  1.20  thorpej 	dcm = sc->sc_dcm = (struct dcmdevice *)hd->hp_addr;
    327  1.20  thorpej 
    328  1.20  thorpej 	brd = hd->hp_unit;
    329  1.20  thorpej 	isconsole = (brd == DCMBOARD(dcmconsole));
    330  1.20  thorpej 
    331  1.20  thorpej 	/* Extract configuration info from flags. */
    332  1.20  thorpej 	sc->sc_softCAR = (hd->hp_flags & DCM_SOFTCAR);
    333  1.20  thorpej 	sc->sc_flags = (hd->hp_flags & DCM_FLAGMASK);
    334  1.20  thorpej 
    335  1.20  thorpej 	/* Mark our unit as configured. */
    336  1.20  thorpej 	sc->sc_flags |= DCM_ACTIVE;
    337  1.20  thorpej 
    338  1.20  thorpej 	/* Establish the interrupt handler. */
    339  1.23  thorpej 	isrlink(dcmintr, sc, hd->hp_ipl, ISRPRI_TTY);
    340  1.20  thorpej 
    341   1.1      cgd 	if (dcmistype == DIS_TIMER)
    342   1.1      cgd 		dcmsetischeme(brd, DIS_RESET|DIS_TIMER);
    343   1.1      cgd 	else
    344   1.1      cgd 		dcmsetischeme(brd, DIS_RESET|DIS_PERCHAR);
    345   1.1      cgd 
    346   1.1      cgd 	/* load pointers to modem control */
    347  1.20  thorpej 	sc->sc_modem[0] = &dcm->dcm_modem0;
    348  1.20  thorpej 	sc->sc_modem[1] = &dcm->dcm_modem1;
    349  1.20  thorpej 	sc->sc_modem[2] = &dcm->dcm_modem2;
    350  1.20  thorpej 	sc->sc_modem[3] = &dcm->dcm_modem3;
    351  1.20  thorpej 
    352   1.1      cgd 	/* set DCD (modem) and CTS (flow control) on all ports */
    353  1.20  thorpej 	if (sc->sc_flags & DCM_STDDCE)
    354   1.1      cgd 		mbits = hp2dce_in(MI_CD|MI_CTS);
    355   1.1      cgd 	else
    356   1.1      cgd 		mbits = MI_CD|MI_CTS;
    357  1.20  thorpej 
    358  1.20  thorpej 	for (i = 0; i < NDCMPORT; i++)
    359  1.20  thorpej 		sc->sc_modem[i]->mdmmsk = mbits;
    360   1.1      cgd 
    361   1.1      cgd 	dcm->dcm_ic = IC_IE;		/* turn all interrupts on */
    362  1.20  thorpej 
    363   1.1      cgd 	/*
    364   1.1      cgd 	 * Need to reset baud rate, etc. of next print so reset dcmconsole.
    365   1.1      cgd 	 * Also make sure console is always "hardwired"
    366   1.1      cgd 	 */
    367   1.1      cgd 	if (isconsole) {
    368   1.1      cgd 		dcmconsinit = 0;
    369  1.20  thorpej 		sc->sc_softCAR |= (1 << DCMPORT(dcmconsole));
    370  1.20  thorpej 		printf(": console on port %d\n", DCMPORT(dcmconsole));
    371  1.20  thorpej 	} else
    372  1.20  thorpej 		printf("\n");
    373  1.20  thorpej 
    374  1.20  thorpej #ifdef KGDB
    375  1.20  thorpej 	if (major(kgdb_dev) == dcmmajor &&
    376  1.20  thorpej 	    DCMBOARD(DCMUNIT(kgdb_dev)) == brd) {
    377  1.20  thorpej 		if (dcmconsole == DCMUNIT(kgdb_dev))
    378  1.20  thorpej 			kgdb_dev = NODEV; /* can't debug over console port */
    379  1.20  thorpej #ifndef KGDB_CHEAT
    380  1.20  thorpej 		/*
    381  1.20  thorpej 		 * The following could potentially be replaced
    382  1.20  thorpej 		 * by the corresponding code in dcmcnprobe.
    383  1.20  thorpej 		 */
    384  1.20  thorpej 		else {
    385  1.22  thorpej 			dcminit(dcm, DCMPORT(DCMUNIT(kgdb_dev)),
    386  1.22  thorpej 			    kgdb_rate);
    387  1.20  thorpej 			if (kgdb_debug_init) {
    388  1.20  thorpej 				printf("%s port %d: ", sc->sc_hd->hp_xname,
    389  1.20  thorpej 				    DCMPORT(DCMUNIT(kgdb_dev)));
    390  1.20  thorpej 				kgdb_connect(1);
    391  1.20  thorpej 			} else
    392  1.20  thorpej 				printf("%s port %d: kgdb enabled\n",
    393  1.20  thorpej 				    sc->sc_hd->hp_xname,
    394  1.20  thorpej 				    DCMPORT(DCMUNIT(kgdb_dev)));
    395  1.20  thorpej 		}
    396  1.20  thorpej 		/* end could be replaced */
    397  1.20  thorpej #endif
    398   1.1      cgd 	}
    399  1.20  thorpej #endif
    400   1.1      cgd }
    401   1.1      cgd 
    402   1.1      cgd /* ARGSUSED */
    403  1.17  mycroft int
    404   1.1      cgd dcmopen(dev, flag, mode, p)
    405   1.1      cgd 	dev_t dev;
    406   1.1      cgd 	int flag, mode;
    407   1.1      cgd 	struct proc *p;
    408   1.1      cgd {
    409  1.20  thorpej 	struct dcm_softc *sc;
    410  1.20  thorpej 	struct tty *tp;
    411  1.20  thorpej 	int unit, brd, port;
    412  1.18  thorpej 	int error = 0, mbits, s;
    413   1.1      cgd 
    414  1.20  thorpej 	unit = DCMUNIT(dev);
    415  1.20  thorpej 	brd = DCMBOARD(unit);
    416  1.20  thorpej 	port = DCMPORT(unit);
    417  1.20  thorpej 
    418  1.20  thorpej 	if ((brd >= NDCM) || (port >= NDCMPORT))
    419   1.1      cgd 		return (ENXIO);
    420  1.20  thorpej 
    421  1.20  thorpej 	sc = &dcm_softc[brd];
    422  1.20  thorpej 	if ((sc->sc_flags & DCM_ACTIVE) == 0)
    423  1.20  thorpej 		return (ENXIO);
    424  1.20  thorpej 
    425  1.20  thorpej 	if (sc->sc_tty[port] == NULL)
    426  1.20  thorpej 		tp = sc->sc_tty[port] = ttymalloc();
    427   1.7  mycroft 	else
    428  1.20  thorpej 		tp = sc->sc_tty[port];
    429  1.20  thorpej 
    430   1.1      cgd 	tp->t_oproc = dcmstart;
    431   1.1      cgd 	tp->t_param = dcmparam;
    432   1.1      cgd 	tp->t_dev = dev;
    433  1.18  thorpej 
    434   1.1      cgd 	if ((tp->t_state & TS_ISOPEN) == 0) {
    435  1.18  thorpej 		/*
    436  1.18  thorpej 		 * Sanity clause: reset the card on first open.
    437  1.18  thorpej 		 * The card might be left in an inconsistent state
    438  1.18  thorpej 		 * if the card memory is read inadvertently.
    439  1.18  thorpej 		 */
    440  1.22  thorpej 		dcminit(sc->sc_dcm, port, dcmdefaultrate);
    441  1.18  thorpej 
    442   1.1      cgd 		tp->t_state |= TS_WOPEN;
    443   1.1      cgd 		ttychars(tp);
    444  1.18  thorpej 		tp->t_iflag = TTYDEF_IFLAG;
    445  1.18  thorpej 		tp->t_oflag = TTYDEF_OFLAG;
    446  1.18  thorpej 		tp->t_cflag = TTYDEF_CFLAG;
    447  1.18  thorpej 		tp->t_lflag = TTYDEF_LFLAG;
    448  1.18  thorpej 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    449  1.18  thorpej 
    450  1.18  thorpej 		s = spltty();
    451  1.18  thorpej 
    452   1.1      cgd 		(void) dcmparam(tp, &tp->t_termios);
    453   1.1      cgd 		ttsetwater(tp);
    454  1.18  thorpej 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
    455   1.1      cgd 		return (EBUSY);
    456  1.18  thorpej 	else
    457  1.18  thorpej 		s = spltty();
    458  1.18  thorpej 
    459  1.18  thorpej 	/* Set modem control state. */
    460   1.1      cgd 	mbits = MO_ON;
    461  1.20  thorpej 	if (sc->sc_flags & DCM_STDDCE)
    462   1.1      cgd 		mbits |= MO_SR;		/* pin 23, could be used as RTS */
    463  1.20  thorpej 
    464   1.1      cgd 	(void) dcmmctl(dev, mbits, DMSET);	/* enable port */
    465  1.18  thorpej 
    466  1.18  thorpej 	/* Set soft-carrier if so configured. */
    467  1.20  thorpej 	if ((sc->sc_softCAR & (1 << port)) ||
    468   1.1      cgd 	    (dcmmctl(dev, MO_OFF, DMGET) & MI_CD))
    469   1.1      cgd 		tp->t_state |= TS_CARR_ON;
    470  1.18  thorpej 
    471   1.1      cgd #ifdef DEBUG
    472   1.1      cgd 	if (dcmdebug & DDB_MODEM)
    473  1.20  thorpej 		printf("%s: dcmopen port %d softcarr %c\n",
    474  1.20  thorpej 		       sc->sc_hd->hp_xname, port,
    475  1.20  thorpej 		       (tp->t_state & TS_CARR_ON) ? '1' : '0');
    476   1.1      cgd #endif
    477  1.18  thorpej 
    478  1.18  thorpej 	/* Wait for carrier if necessary. */
    479  1.18  thorpej 	if ((flag & O_NONBLOCK) == 0)
    480  1.18  thorpej 		while ((tp->t_cflag & CLOCAL) == 0 &&
    481  1.18  thorpej 		    (tp->t_state & TS_CARR_ON) == 0) {
    482  1.18  thorpej 			tp->t_state |= TS_WOPEN;
    483  1.18  thorpej 			error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    484  1.18  thorpej 			    TTIPRI | PCATCH, ttopen, 0);
    485  1.18  thorpej 			if (error) {
    486  1.18  thorpej 				splx(s);
    487  1.18  thorpej 				return (error);
    488  1.18  thorpej 			}
    489  1.18  thorpej 		}
    490  1.18  thorpej 
    491  1.18  thorpej 	splx(s);
    492   1.1      cgd 
    493   1.1      cgd #ifdef DEBUG
    494   1.1      cgd 	if (dcmdebug & DDB_OPENCLOSE)
    495  1.20  thorpej 		printf("%s port %d: dcmopen: st %x fl %x\n",
    496  1.20  thorpej 			sc->sc_hd->hp_xname, port, tp->t_state, tp->t_flags);
    497   1.1      cgd #endif
    498   1.1      cgd 	if (error == 0)
    499   1.1      cgd 		error = (*linesw[tp->t_line].l_open)(dev, tp);
    500  1.18  thorpej 
    501   1.1      cgd 	return (error);
    502   1.1      cgd }
    503   1.1      cgd 
    504   1.1      cgd /*ARGSUSED*/
    505  1.17  mycroft int
    506   1.1      cgd dcmclose(dev, flag, mode, p)
    507   1.1      cgd 	dev_t dev;
    508   1.1      cgd 	int flag, mode;
    509   1.1      cgd 	struct proc *p;
    510   1.1      cgd {
    511  1.20  thorpej 	int s, unit, board, port;
    512  1.20  thorpej 	struct dcm_softc *sc;
    513  1.20  thorpej 	struct tty *tp;
    514   1.1      cgd 
    515  1.20  thorpej 	unit = DCMUNIT(dev);
    516  1.20  thorpej 	board = DCMBOARD(unit);
    517  1.20  thorpej 	port = DCMPORT(unit);
    518  1.20  thorpej 
    519  1.20  thorpej 	sc = &dcm_softc[board];
    520  1.20  thorpej 	tp = sc->sc_tty[port];
    521  1.20  thorpej 
    522   1.1      cgd 	(*linesw[tp->t_line].l_close)(tp, flag);
    523  1.18  thorpej 
    524  1.18  thorpej 	s = spltty();
    525  1.18  thorpej 
    526  1.18  thorpej 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
    527  1.18  thorpej 	    (tp->t_state & TS_ISOPEN) == 0)
    528   1.1      cgd 		(void) dcmmctl(dev, MO_OFF, DMSET);
    529   1.1      cgd #ifdef DEBUG
    530   1.1      cgd 	if (dcmdebug & DDB_OPENCLOSE)
    531  1.20  thorpej 		printf("%s port %d: dcmclose: st %x fl %x\n",
    532  1.20  thorpej 			sc->sc_hd->hp_xname, port, tp->t_state, tp->t_flags);
    533   1.1      cgd #endif
    534  1.18  thorpej 	splx(s);
    535   1.1      cgd 	ttyclose(tp);
    536  1.12  mycroft #if 0
    537   1.7  mycroft 	ttyfree(tp);
    538  1.20  thorpej 	sc->sc_tty[port] == NULL;
    539  1.12  mycroft #endif
    540   1.1      cgd 	return (0);
    541   1.1      cgd }
    542   1.1      cgd 
    543  1.17  mycroft int
    544   1.1      cgd dcmread(dev, uio, flag)
    545   1.1      cgd 	dev_t dev;
    546   1.1      cgd 	struct uio *uio;
    547  1.14  mycroft 	int flag;
    548   1.1      cgd {
    549  1.20  thorpej 	int unit, board, port;
    550  1.20  thorpej 	struct dcm_softc *sc;
    551  1.20  thorpej 	register struct tty *tp;
    552  1.20  thorpej 
    553  1.20  thorpej 	unit = DCMUNIT(dev);
    554  1.20  thorpej 	board = DCMBOARD(unit);
    555  1.20  thorpej 	port = DCMPORT(unit);
    556  1.20  thorpej 
    557  1.20  thorpej 	sc = &dcm_softc[board];
    558  1.20  thorpej 	tp = sc->sc_tty[port];
    559  1.14  mycroft 
    560   1.1      cgd 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    561   1.1      cgd }
    562   1.1      cgd 
    563  1.17  mycroft int
    564   1.1      cgd dcmwrite(dev, uio, flag)
    565   1.1      cgd 	dev_t dev;
    566   1.1      cgd 	struct uio *uio;
    567  1.14  mycroft 	int flag;
    568   1.1      cgd {
    569  1.20  thorpej 	int unit, board, port;
    570  1.20  thorpej 	struct dcm_softc *sc;
    571  1.20  thorpej 	register struct tty *tp;
    572  1.20  thorpej 
    573  1.20  thorpej 	unit = DCMUNIT(dev);
    574  1.20  thorpej 	board = DCMBOARD(unit);
    575  1.20  thorpej 	port = DCMPORT(unit);
    576  1.20  thorpej 
    577  1.20  thorpej 	sc = &dcm_softc[board];
    578  1.20  thorpej 	tp = sc->sc_tty[port];
    579  1.14  mycroft 
    580   1.1      cgd 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    581   1.1      cgd }
    582  1.17  mycroft 
    583  1.17  mycroft struct tty *
    584  1.17  mycroft dcmtty(dev)
    585  1.17  mycroft 	dev_t dev;
    586  1.17  mycroft {
    587  1.20  thorpej 	int unit, board, port;
    588  1.20  thorpej 	struct dcm_softc *sc;
    589  1.17  mycroft 
    590  1.20  thorpej 	unit = DCMUNIT(dev);
    591  1.20  thorpej 	board = DCMBOARD(unit);
    592  1.20  thorpej 	port = DCMPORT(unit);
    593  1.20  thorpej 
    594  1.20  thorpej 	sc = &dcm_softc[board];
    595  1.20  thorpej 
    596  1.20  thorpej 	return (sc->sc_tty[port]);
    597  1.17  mycroft }
    598   1.1      cgd 
    599  1.17  mycroft int
    600  1.23  thorpej dcmintr(arg)
    601  1.23  thorpej 	void *arg;
    602   1.1      cgd {
    603  1.23  thorpej 	struct dcm_softc *sc = arg;
    604  1.20  thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
    605  1.20  thorpej 	struct dcmischeme *dis = &sc->sc_scheme;
    606  1.23  thorpej 	int brd = sc->sc_hd->hp_unit;
    607  1.20  thorpej 	int code, i;
    608   1.1      cgd 	int pcnd[4], mcode, mcnd[4];
    609   1.1      cgd 
    610   1.1      cgd 	/*
    611   1.1      cgd 	 * Do all guarded register accesses right off to minimize
    612   1.1      cgd 	 * block out of hardware.
    613   1.1      cgd 	 */
    614   1.1      cgd 	SEM_LOCK(dcm);
    615   1.1      cgd 	if ((dcm->dcm_ic & IC_IR) == 0) {
    616   1.1      cgd 		SEM_UNLOCK(dcm);
    617   1.1      cgd 		return (0);
    618   1.1      cgd 	}
    619   1.1      cgd 	for (i = 0; i < 4; i++) {
    620   1.1      cgd 		pcnd[i] = dcm->dcm_icrtab[i].dcm_data;
    621   1.1      cgd 		dcm->dcm_icrtab[i].dcm_data = 0;
    622  1.20  thorpej 		code = sc->sc_modem[i]->mdmin;
    623  1.20  thorpej 		if (sc->sc_flags & DCM_STDDCE)
    624   1.1      cgd 			code = hp2dce_in(code);
    625   1.1      cgd 		mcnd[i] = code;
    626   1.1      cgd 	}
    627   1.1      cgd 	code = dcm->dcm_iir & IIR_MASK;
    628   1.1      cgd 	dcm->dcm_iir = 0;	/* XXX doc claims read clears interrupt?! */
    629   1.1      cgd 	mcode = dcm->dcm_modemintr;
    630   1.1      cgd 	dcm->dcm_modemintr = 0;
    631   1.1      cgd 	SEM_UNLOCK(dcm);
    632   1.1      cgd 
    633   1.1      cgd #ifdef DEBUG
    634   1.1      cgd 	if (dcmdebug & DDB_INTR) {
    635  1.20  thorpej 		printf("%s: dcmintr: iir %x pc %x/%x/%x/%x ",
    636  1.20  thorpej 		       sc->sc_hd->hp_xname, code, pcnd[0], pcnd[1],
    637  1.20  thorpej 		       pcnd[2], pcnd[3]);
    638   1.1      cgd 		printf("miir %x mc %x/%x/%x/%x\n",
    639   1.1      cgd 		       mcode, mcnd[0], mcnd[1], mcnd[2], mcnd[3]);
    640   1.1      cgd 	}
    641   1.1      cgd #endif
    642   1.1      cgd 	if (code & IIR_TIMEO)
    643  1.20  thorpej 		dcmrint(sc);
    644   1.1      cgd 	if (code & IIR_PORT0)
    645  1.20  thorpej 		dcmpint(sc, 0, pcnd[0]);
    646   1.1      cgd 	if (code & IIR_PORT1)
    647  1.20  thorpej 		dcmpint(sc, 1, pcnd[1]);
    648   1.1      cgd 	if (code & IIR_PORT2)
    649  1.20  thorpej 		dcmpint(sc, 2, pcnd[2]);
    650   1.1      cgd 	if (code & IIR_PORT3)
    651  1.20  thorpej 		dcmpint(sc, 3, pcnd[3]);
    652   1.1      cgd 	if (code & IIR_MODM) {
    653   1.1      cgd 		if (mcode == 0 || mcode & 0x1)	/* mcode==0 -> 98642 board */
    654  1.20  thorpej 			dcmmint(sc, 0, mcnd[0]);
    655   1.1      cgd 		if (mcode & 0x2)
    656  1.20  thorpej 			dcmmint(sc, 1, mcnd[1]);
    657   1.1      cgd 		if (mcode & 0x4)
    658  1.20  thorpej 			dcmmint(sc, 2, mcnd[2]);
    659   1.1      cgd 		if (mcode & 0x8)
    660  1.20  thorpej 			dcmmint(sc, 3, mcnd[3]);
    661   1.1      cgd 	}
    662   1.1      cgd 
    663   1.1      cgd 	/*
    664   1.1      cgd 	 * Chalk up a receiver interrupt if the timer running or one of
    665   1.1      cgd 	 * the ports reports a special character interrupt.
    666   1.1      cgd 	 */
    667   1.1      cgd 	if ((code & IIR_TIMEO) ||
    668   1.1      cgd 	    ((pcnd[0]|pcnd[1]|pcnd[2]|pcnd[3]) & IT_SPEC))
    669   1.1      cgd 		dis->dis_intr++;
    670   1.1      cgd 	/*
    671   1.1      cgd 	 * See if it is time to check/change the interrupt rate.
    672   1.1      cgd 	 */
    673   1.1      cgd 	if (dcmistype < 0 &&
    674   1.1      cgd 	    (i = time.tv_sec - dis->dis_time) >= dcminterval) {
    675   1.1      cgd 		/*
    676   1.1      cgd 		 * If currently per-character and averaged over 70 interrupts
    677   1.1      cgd 		 * per-second (66 is threshold of 600 baud) in last interval,
    678   1.1      cgd 		 * switch to timer mode.
    679   1.1      cgd 		 *
    680   1.1      cgd 		 * XXX decay counts ala load average to avoid spikes?
    681   1.1      cgd 		 */
    682   1.1      cgd 		if (dis->dis_perchar && dis->dis_intr > 70 * i)
    683   1.1      cgd 			dcmsetischeme(brd, DIS_TIMER);
    684   1.1      cgd 		/*
    685   1.1      cgd 		 * If currently using timer and had more interrupts than
    686   1.1      cgd 		 * received characters in the last interval, switch back
    687   1.1      cgd 		 * to per-character.  Note that after changing to per-char
    688   1.1      cgd 		 * we must process any characters already in the queue
    689   1.1      cgd 		 * since they may have arrived before the bitmap was setup.
    690   1.1      cgd 		 *
    691   1.1      cgd 		 * XXX decay counts?
    692   1.1      cgd 		 */
    693   1.1      cgd 		else if (!dis->dis_perchar && dis->dis_intr > dis->dis_char) {
    694   1.1      cgd 			dcmsetischeme(brd, DIS_PERCHAR);
    695  1.20  thorpej 			dcmrint(sc);
    696   1.1      cgd 		}
    697   1.1      cgd 		dis->dis_intr = dis->dis_char = 0;
    698   1.1      cgd 		dis->dis_time = time.tv_sec;
    699   1.1      cgd 	}
    700   1.1      cgd 	return (1);
    701   1.1      cgd }
    702   1.1      cgd 
    703   1.1      cgd /*
    704   1.1      cgd  *  Port interrupt.  Can be two things:
    705   1.1      cgd  *	First, it might be a special character (exception interrupt);
    706   1.1      cgd  *	Second, it may be a buffer empty (transmit interrupt);
    707   1.1      cgd  */
    708  1.20  thorpej dcmpint(sc, port, code)
    709  1.20  thorpej 	struct dcm_softc *sc;
    710  1.20  thorpej 	int port, code;
    711   1.1      cgd {
    712   1.1      cgd 
    713   1.1      cgd 	if (code & IT_SPEC)
    714  1.20  thorpej 		dcmreadbuf(sc, port);
    715   1.1      cgd 	if (code & IT_TX)
    716  1.20  thorpej 		dcmxint(sc, port);
    717   1.1      cgd }
    718   1.1      cgd 
    719  1.20  thorpej dcmrint(sc)
    720  1.20  thorpej 	struct dcm_softc *sc;
    721   1.1      cgd {
    722  1.20  thorpej 	int port;
    723   1.1      cgd 
    724  1.20  thorpej 	for (port = 0; port < NDCMPORT; port++)
    725  1.20  thorpej 		dcmreadbuf(sc, port);
    726   1.1      cgd }
    727   1.1      cgd 
    728  1.20  thorpej dcmreadbuf(sc, port)
    729  1.20  thorpej 	struct dcm_softc *sc;
    730  1.20  thorpej 	int port;
    731   1.1      cgd {
    732  1.20  thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
    733  1.20  thorpej 	struct tty *tp = sc->sc_tty[port];
    734  1.20  thorpej 	struct dcmpreg *pp = dcm_preg(dcm, port);
    735  1.20  thorpej 	struct dcmrfifo *fifo;
    736  1.20  thorpej 	int c, stat;
    737  1.20  thorpej 	u_int head;
    738   1.1      cgd 	int nch = 0;
    739  1.14  mycroft #ifdef DCMSTATS
    740  1.20  thorpej 	struct dcmstats *dsp = &sc->sc_stats;
    741   1.1      cgd 
    742   1.1      cgd 	dsp->rints++;
    743   1.1      cgd #endif
    744   1.1      cgd 	if ((tp->t_state & TS_ISOPEN) == 0) {
    745   1.1      cgd #ifdef KGDB
    746  1.20  thorpej 		if ((makedev(dcmmajor, minor(tp->t_dev)) == kgdb_dev) &&
    747   1.1      cgd 		    (head = pp->r_head & RX_MASK) != (pp->r_tail & RX_MASK) &&
    748  1.14  mycroft 		    dcm->dcm_rfifos[3-port][head>>1].data_char == FRAME_START) {
    749   1.1      cgd 			pp->r_head = (head + 2) & RX_MASK;
    750   1.1      cgd 			kgdb_connect(0);	/* trap into kgdb */
    751   1.1      cgd 			return;
    752   1.1      cgd 		}
    753   1.1      cgd #endif /* KGDB */
    754   1.1      cgd 		pp->r_head = pp->r_tail & RX_MASK;
    755   1.1      cgd 		return;
    756   1.1      cgd 	}
    757   1.1      cgd 
    758   1.1      cgd 	head = pp->r_head & RX_MASK;
    759   1.1      cgd 	fifo = &dcm->dcm_rfifos[3-port][head>>1];
    760   1.1      cgd 	/*
    761   1.1      cgd 	 * XXX upper bound on how many chars we will take in one swallow?
    762   1.1      cgd 	 */
    763   1.1      cgd 	while (head != (pp->r_tail & RX_MASK)) {
    764   1.1      cgd 		/*
    765   1.1      cgd 		 * Get character/status and update head pointer as fast
    766   1.1      cgd 		 * as possible to make room for more characters.
    767   1.1      cgd 		 */
    768   1.1      cgd 		c = fifo->data_char;
    769   1.1      cgd 		stat = fifo->data_stat;
    770   1.1      cgd 		head = (head + 2) & RX_MASK;
    771   1.1      cgd 		pp->r_head = head;
    772   1.1      cgd 		fifo = head ? fifo+1 : &dcm->dcm_rfifos[3-port][0];
    773   1.1      cgd 		nch++;
    774   1.1      cgd 
    775   1.1      cgd #ifdef DEBUG
    776   1.1      cgd 		if (dcmdebug & DDB_INPUT)
    777  1.20  thorpej 			printf("%s port %d: dcmreadbuf: c%x('%c') s%x f%x h%x t%x\n",
    778  1.20  thorpej 			       sc->sc_hd->hp_xname, port,
    779  1.20  thorpej 			       c&0xFF, c, stat&0xFF,
    780   1.1      cgd 			       tp->t_flags, head, pp->r_tail);
    781   1.1      cgd #endif
    782   1.1      cgd 		/*
    783   1.1      cgd 		 * Check for and handle errors
    784   1.1      cgd 		 */
    785   1.1      cgd 		if (stat & RD_MASK) {
    786   1.1      cgd #ifdef DEBUG
    787   1.1      cgd 			if (dcmdebug & (DDB_INPUT|DDB_SIOERR))
    788  1.20  thorpej 				printf("%s port %d: dcmreadbuf: err: c%x('%c') s%x\n",
    789  1.20  thorpej 				       sc->sc_hd->hp_xname, port,
    790  1.20  thorpej 				       stat, c&0xFF, c);
    791   1.1      cgd #endif
    792   1.1      cgd 			if (stat & (RD_BD | RD_FE))
    793   1.1      cgd 				c |= TTY_FE;
    794   1.1      cgd 			else if (stat & RD_PE)
    795   1.1      cgd 				c |= TTY_PE;
    796   1.1      cgd 			else if (stat & RD_OVF)
    797   1.1      cgd 				log(LOG_WARNING,
    798  1.20  thorpej 				    "%s port %d: silo overflow\n",
    799  1.20  thorpej 				    sc->sc_hd->hp_xname, port);
    800   1.1      cgd 			else if (stat & RD_OE)
    801   1.1      cgd 				log(LOG_WARNING,
    802  1.20  thorpej 				    "%s port %d: uart overflow\n",
    803  1.20  thorpej 				    sc->sc_hd->hp_xname, port);
    804   1.1      cgd 		}
    805   1.1      cgd 		(*linesw[tp->t_line].l_rint)(c, tp);
    806   1.1      cgd 	}
    807  1.20  thorpej 	sc->sc_scheme.dis_char += nch;
    808  1.20  thorpej 
    809  1.14  mycroft #ifdef DCMSTATS
    810   1.1      cgd 	dsp->rchars += nch;
    811   1.1      cgd 	if (nch <= DCMRBSIZE)
    812   1.1      cgd 		dsp->rsilo[nch]++;
    813   1.1      cgd 	else
    814   1.1      cgd 		dsp->rsilo[DCMRBSIZE+1]++;
    815   1.1      cgd #endif
    816   1.1      cgd }
    817   1.1      cgd 
    818  1.20  thorpej dcmxint(sc, port)
    819  1.20  thorpej 	struct dcm_softc *sc;
    820  1.20  thorpej 	int port;
    821   1.1      cgd {
    822  1.20  thorpej 	struct tty *tp = sc->sc_tty[port];
    823  1.20  thorpej 
    824   1.1      cgd 	tp->t_state &= ~TS_BUSY;
    825   1.1      cgd 	if (tp->t_state & TS_FLUSH)
    826   1.1      cgd 		tp->t_state &= ~TS_FLUSH;
    827   1.1      cgd 	(*linesw[tp->t_line].l_start)(tp);
    828   1.1      cgd }
    829   1.1      cgd 
    830  1.20  thorpej dcmmint(sc, port, mcnd)
    831  1.20  thorpej 	struct dcm_softc *sc;
    832  1.20  thorpej 	int port, mcnd;
    833   1.1      cgd {
    834   1.1      cgd 	int delta;
    835  1.20  thorpej 	struct tty *tp;
    836  1.20  thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
    837  1.20  thorpej 
    838  1.20  thorpej 	tp = sc->sc_tty[port];
    839   1.1      cgd 
    840   1.1      cgd #ifdef DEBUG
    841   1.1      cgd 	if (dcmdebug & DDB_MODEM)
    842  1.20  thorpej 		printf("%s port %d: dcmmint: mcnd %x mcndlast %x\n",
    843  1.20  thorpej 		       sc->sc_hd->hp_xname, port, mcnd, sc->sc_mcndlast[port]);
    844   1.1      cgd #endif
    845  1.20  thorpej 	delta = mcnd ^ sc->sc_mcndlast[port];
    846  1.20  thorpej 	sc->sc_mcndlast[port] = mcnd;
    847   1.1      cgd 	if ((delta & MI_CTS) && (tp->t_state & TS_ISOPEN) &&
    848   1.1      cgd 	    (tp->t_flags & CCTS_OFLOW)) {
    849   1.1      cgd 		if (mcnd & MI_CTS) {
    850   1.1      cgd 			tp->t_state &= ~TS_TTSTOP;
    851   1.1      cgd 			ttstart(tp);
    852   1.1      cgd 		} else
    853   1.1      cgd 			tp->t_state |= TS_TTSTOP;	/* inline dcmstop */
    854   1.1      cgd 	}
    855   1.1      cgd 	if (delta & MI_CD) {
    856   1.1      cgd 		if (mcnd & MI_CD)
    857   1.1      cgd 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    858  1.20  thorpej 		else if ((sc->sc_softCAR & (1 << port)) == 0 &&
    859   1.1      cgd 		    (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
    860  1.20  thorpej 			sc->sc_modem[port]->mdmout = MO_OFF;
    861   1.1      cgd 			SEM_LOCK(dcm);
    862  1.20  thorpej 			dcm->dcm_modemchng |= (1 << port);
    863   1.1      cgd 			dcm->dcm_cr |= CR_MODM;
    864   1.1      cgd 			SEM_UNLOCK(dcm);
    865   1.1      cgd 			DELAY(10); /* time to change lines */
    866   1.1      cgd 		}
    867   1.1      cgd 	}
    868   1.1      cgd }
    869   1.1      cgd 
    870  1.17  mycroft int
    871  1.13  mycroft dcmioctl(dev, cmd, data, flag, p)
    872   1.1      cgd 	dev_t dev;
    873  1.13  mycroft 	int cmd;
    874   1.1      cgd 	caddr_t data;
    875  1.13  mycroft 	int flag;
    876  1.13  mycroft 	struct proc *p;
    877   1.1      cgd {
    878  1.20  thorpej 	struct dcm_softc *sc;
    879  1.20  thorpej 	struct tty *tp;
    880  1.20  thorpej 	struct dcmdevice *dcm;
    881  1.20  thorpej 	int board, port, unit = DCMUNIT(dev);
    882   1.1      cgd 	int error, s;
    883  1.20  thorpej 
    884  1.20  thorpej 	port = DCMPORT(unit);
    885  1.20  thorpej 	board = DCMBOARD(unit);
    886  1.20  thorpej 
    887  1.20  thorpej 	sc = &dcm_softc[board];
    888  1.20  thorpej 	dcm = sc->sc_dcm;
    889  1.20  thorpej 	tp = sc->sc_tty[port];
    890   1.1      cgd 
    891   1.1      cgd #ifdef DEBUG
    892   1.1      cgd 	if (dcmdebug & DDB_IOCTL)
    893  1.20  thorpej 		printf("%s port %d: dcmioctl: cmd %x data %x flag %x\n",
    894  1.20  thorpej 		       sc->sc_hd->hp_xname, port, cmd, *data, flag);
    895   1.1      cgd #endif
    896  1.13  mycroft 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    897   1.1      cgd 	if (error >= 0)
    898   1.1      cgd 		return (error);
    899  1.13  mycroft 	error = ttioctl(tp, cmd, data, flag, p);
    900   1.1      cgd 	if (error >= 0)
    901   1.1      cgd 		return (error);
    902   1.1      cgd 
    903   1.1      cgd 	switch (cmd) {
    904   1.1      cgd 	case TIOCSBRK:
    905   1.1      cgd 		/*
    906   1.1      cgd 		 * Wait for transmitter buffer to empty
    907   1.1      cgd 		 */
    908   1.1      cgd 		s = spltty();
    909   1.1      cgd 		while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
    910   1.1      cgd 			DELAY(DCM_USPERCH(tp->t_ospeed));
    911   1.1      cgd 		SEM_LOCK(dcm);
    912   1.1      cgd 		dcm->dcm_cmdtab[port].dcm_data |= CT_BRK;
    913   1.1      cgd 		dcm->dcm_cr |= (1 << port);	/* start break */
    914   1.1      cgd 		SEM_UNLOCK(dcm);
    915   1.1      cgd 		splx(s);
    916   1.1      cgd 		break;
    917   1.1      cgd 
    918   1.1      cgd 	case TIOCCBRK:
    919   1.1      cgd 		SEM_LOCK(dcm);
    920   1.1      cgd 		dcm->dcm_cmdtab[port].dcm_data |= CT_BRK;
    921   1.1      cgd 		dcm->dcm_cr |= (1 << port);	/* end break */
    922   1.1      cgd 		SEM_UNLOCK(dcm);
    923   1.1      cgd 		break;
    924   1.1      cgd 
    925   1.1      cgd 	case TIOCSDTR:
    926   1.1      cgd 		(void) dcmmctl(dev, MO_ON, DMBIS);
    927   1.1      cgd 		break;
    928   1.1      cgd 
    929   1.1      cgd 	case TIOCCDTR:
    930   1.1      cgd 		(void) dcmmctl(dev, MO_ON, DMBIC);
    931   1.1      cgd 		break;
    932   1.1      cgd 
    933   1.1      cgd 	case TIOCMSET:
    934   1.1      cgd 		(void) dcmmctl(dev, *(int *)data, DMSET);
    935   1.1      cgd 		break;
    936   1.1      cgd 
    937   1.1      cgd 	case TIOCMBIS:
    938   1.1      cgd 		(void) dcmmctl(dev, *(int *)data, DMBIS);
    939   1.1      cgd 		break;
    940   1.1      cgd 
    941   1.1      cgd 	case TIOCMBIC:
    942   1.1      cgd 		(void) dcmmctl(dev, *(int *)data, DMBIC);
    943   1.1      cgd 		break;
    944   1.1      cgd 
    945   1.1      cgd 	case TIOCMGET:
    946   1.1      cgd 		*(int *)data = dcmmctl(dev, 0, DMGET);
    947   1.1      cgd 		break;
    948  1.18  thorpej 
    949  1.18  thorpej 	case TIOCGFLAGS: {
    950  1.18  thorpej 		int bits = 0;
    951  1.18  thorpej 
    952  1.20  thorpej 		if ((sc->sc_softCAR & (1 << port)))
    953  1.18  thorpej 			bits |= TIOCFLAG_SOFTCAR;
    954  1.18  thorpej 
    955  1.18  thorpej 		if (tp->t_cflag & CLOCAL)
    956  1.18  thorpej 			bits |= TIOCFLAG_CLOCAL;
    957  1.18  thorpej 
    958  1.18  thorpej 		*(int *)data = bits;
    959  1.18  thorpej 		break;
    960  1.18  thorpej 	}
    961  1.18  thorpej 
    962  1.18  thorpej 	case TIOCSFLAGS: {
    963  1.18  thorpej 		int userbits;
    964  1.18  thorpej 
    965  1.18  thorpej 		error = suser(p->p_ucred, &p->p_acflag);
    966  1.18  thorpej 		if (error)
    967  1.18  thorpej 			return (EPERM);
    968  1.18  thorpej 
    969  1.18  thorpej 		userbits = *(int *)data;
    970  1.18  thorpej 
    971  1.18  thorpej 		if ((userbits & TIOCFLAG_SOFTCAR) ||
    972  1.20  thorpej 		    ((board == DCMBOARD(dcmconsole)) &&
    973  1.20  thorpej 		    (port == DCMPORT(dcmconsole))))
    974  1.20  thorpej 			sc->sc_softCAR |= (1 << port);
    975  1.18  thorpej 
    976  1.18  thorpej 		if (userbits & TIOCFLAG_CLOCAL)
    977  1.18  thorpej 			tp->t_cflag |= CLOCAL;
    978  1.18  thorpej 
    979  1.18  thorpej 		break;
    980  1.18  thorpej 	}
    981   1.1      cgd 
    982   1.1      cgd 	default:
    983   1.1      cgd 		return (ENOTTY);
    984   1.1      cgd 	}
    985   1.1      cgd 	return (0);
    986   1.1      cgd }
    987   1.1      cgd 
    988  1.17  mycroft int
    989   1.1      cgd dcmparam(tp, t)
    990   1.1      cgd 	register struct tty *tp;
    991   1.1      cgd 	register struct termios *t;
    992   1.1      cgd {
    993  1.20  thorpej 	struct dcm_softc *sc;
    994  1.20  thorpej 	struct dcmdevice *dcm;
    995  1.20  thorpej 	int unit, board, port, mode, cflag = t->c_cflag;
    996   1.1      cgd 	int ospeed = ttspeedtab(t->c_ospeed, dcmspeedtab);
    997   1.1      cgd 
    998  1.20  thorpej 	unit = DCMUNIT(tp->t_dev);
    999  1.20  thorpej 	board = DCMBOARD(unit);
   1000  1.20  thorpej 	port = DCMPORT(unit);
   1001  1.20  thorpej 
   1002  1.20  thorpej 	sc = &dcm_softc[board];
   1003  1.20  thorpej 	dcm = sc->sc_dcm;
   1004  1.20  thorpej 
   1005   1.1      cgd 	/* check requested parameters */
   1006   1.1      cgd         if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
   1007   1.1      cgd                 return (EINVAL);
   1008   1.1      cgd         /* and copy to tty */
   1009   1.1      cgd         tp->t_ispeed = t->c_ispeed;
   1010   1.1      cgd         tp->t_ospeed = t->c_ospeed;
   1011   1.1      cgd         tp->t_cflag = cflag;
   1012   1.1      cgd 	if (ospeed == 0) {
   1013  1.20  thorpej 		(void) dcmmctl(DCMUNIT(tp->t_dev), MO_OFF, DMSET);
   1014   1.1      cgd 		return (0);
   1015   1.1      cgd 	}
   1016   1.1      cgd 
   1017   1.1      cgd 	mode = 0;
   1018   1.1      cgd 	switch (cflag&CSIZE) {
   1019   1.1      cgd 	case CS5:
   1020   1.1      cgd 		mode = LC_5BITS; break;
   1021   1.1      cgd 	case CS6:
   1022   1.1      cgd 		mode = LC_6BITS; break;
   1023   1.1      cgd 	case CS7:
   1024   1.1      cgd 		mode = LC_7BITS; break;
   1025   1.1      cgd 	case CS8:
   1026   1.1      cgd 		mode = LC_8BITS; break;
   1027   1.1      cgd 	}
   1028   1.1      cgd 	if (cflag&PARENB) {
   1029   1.1      cgd 		if (cflag&PARODD)
   1030   1.1      cgd 			mode |= LC_PODD;
   1031   1.1      cgd 		else
   1032   1.1      cgd 			mode |= LC_PEVEN;
   1033   1.1      cgd 	}
   1034   1.1      cgd 	if (cflag&CSTOPB)
   1035   1.1      cgd 		mode |= LC_2STOP;
   1036   1.1      cgd 	else
   1037   1.1      cgd 		mode |= LC_1STOP;
   1038   1.1      cgd #ifdef DEBUG
   1039   1.1      cgd 	if (dcmdebug & DDB_PARAM)
   1040  1.20  thorpej 		printf("%s port %d: dcmparam: cflag %x mode %x speed %d uperch %d\n",
   1041  1.20  thorpej 		       sc->sc_hd->hp_xname, port, cflag, mode, tp->t_ospeed,
   1042   1.1      cgd 		       DCM_USPERCH(tp->t_ospeed));
   1043   1.1      cgd #endif
   1044   1.1      cgd 
   1045   1.1      cgd 	/*
   1046   1.1      cgd 	 * Wait for transmitter buffer to empty.
   1047   1.1      cgd 	 */
   1048   1.1      cgd 	while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
   1049   1.1      cgd 		DELAY(DCM_USPERCH(tp->t_ospeed));
   1050   1.1      cgd 	/*
   1051   1.1      cgd 	 * Make changes known to hardware.
   1052   1.1      cgd 	 */
   1053   1.1      cgd 	dcm->dcm_data[port].dcm_baud = ospeed;
   1054   1.1      cgd 	dcm->dcm_data[port].dcm_conf = mode;
   1055   1.1      cgd 	SEM_LOCK(dcm);
   1056   1.1      cgd 	dcm->dcm_cmdtab[port].dcm_data |= CT_CON;
   1057   1.1      cgd 	dcm->dcm_cr |= (1 << port);
   1058   1.1      cgd 	SEM_UNLOCK(dcm);
   1059   1.1      cgd 	/*
   1060   1.1      cgd 	 * Delay for config change to take place. Weighted by baud.
   1061   1.1      cgd 	 * XXX why do we do this?
   1062   1.1      cgd 	 */
   1063   1.1      cgd 	DELAY(16 * DCM_USPERCH(tp->t_ospeed));
   1064   1.1      cgd 	return (0);
   1065   1.1      cgd }
   1066   1.1      cgd 
   1067   1.9  deraadt void
   1068   1.1      cgd dcmstart(tp)
   1069   1.1      cgd 	register struct tty *tp;
   1070   1.1      cgd {
   1071  1.20  thorpej 	struct dcm_softc *sc;
   1072  1.20  thorpej 	struct dcmdevice *dcm;
   1073  1.20  thorpej 	struct dcmpreg *pp;
   1074  1.20  thorpej 	struct dcmtfifo *fifo;
   1075  1.20  thorpej 	char *bp;
   1076  1.20  thorpej 	u_int head, tail, next;
   1077  1.20  thorpej 	int unit, board, port, nch;
   1078   1.1      cgd 	char buf[16];
   1079   1.1      cgd 	int s;
   1080  1.14  mycroft #ifdef DCMSTATS
   1081  1.20  thorpej 	struct dcmstats *dsp = &sc->sc_stats;
   1082   1.1      cgd 	int tch = 0;
   1083   1.1      cgd #endif
   1084   1.1      cgd 
   1085  1.20  thorpej 	unit = DCMUNIT(tp->t_dev);
   1086  1.20  thorpej 	board = DCMBOARD(unit);
   1087  1.20  thorpej 	port = DCMPORT(unit);
   1088  1.20  thorpej 
   1089  1.20  thorpej 	sc = &dcm_softc[board];
   1090  1.20  thorpej 	dcm = sc->sc_dcm;
   1091  1.20  thorpej 
   1092   1.1      cgd 	s = spltty();
   1093  1.14  mycroft #ifdef DCMSTATS
   1094   1.1      cgd 	dsp->xints++;
   1095   1.1      cgd #endif
   1096   1.1      cgd #ifdef DEBUG
   1097   1.1      cgd 	if (dcmdebug & DDB_OUTPUT)
   1098  1.20  thorpej 		printf("%s port %d: dcmstart: state %x flags %x outcc %d\n",
   1099  1.20  thorpej 		       sc->sc_hd->hp_xname, port, tp->t_state, tp->t_flags,
   1100   1.7  mycroft 		       tp->t_outq.c_cc);
   1101   1.1      cgd #endif
   1102   1.1      cgd 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
   1103   1.1      cgd 		goto out;
   1104   1.7  mycroft 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1105   1.1      cgd 		if (tp->t_state&TS_ASLEEP) {
   1106   1.1      cgd 			tp->t_state &= ~TS_ASLEEP;
   1107   1.7  mycroft 			wakeup((caddr_t)&tp->t_outq);
   1108   1.1      cgd 		}
   1109   1.2      cgd 		selwakeup(&tp->t_wsel);
   1110   1.1      cgd 	}
   1111   1.7  mycroft 	if (tp->t_outq.c_cc == 0) {
   1112  1.14  mycroft #ifdef DCMSTATS
   1113   1.1      cgd 		dsp->xempty++;
   1114   1.1      cgd #endif
   1115   1.1      cgd 		goto out;
   1116   1.1      cgd 	}
   1117   1.1      cgd 
   1118   1.1      cgd 	pp = dcm_preg(dcm, port);
   1119   1.1      cgd 	tail = pp->t_tail & TX_MASK;
   1120   1.1      cgd 	next = (tail + 1) & TX_MASK;
   1121   1.1      cgd 	head = pp->t_head & TX_MASK;
   1122   1.1      cgd 	if (head == next)
   1123   1.1      cgd 		goto out;
   1124   1.1      cgd 	fifo = &dcm->dcm_tfifos[3-port][tail];
   1125   1.1      cgd again:
   1126   1.1      cgd 	nch = q_to_b(&tp->t_outq, buf, (head - next) & TX_MASK);
   1127  1.14  mycroft #ifdef DCMSTATS
   1128   1.1      cgd 	tch += nch;
   1129   1.1      cgd #endif
   1130   1.1      cgd #ifdef DEBUG
   1131   1.1      cgd 	if (dcmdebug & DDB_OUTPUT)
   1132   1.1      cgd 		printf("\thead %x tail %x nch %d\n", head, tail, nch);
   1133   1.1      cgd #endif
   1134   1.1      cgd 	/*
   1135   1.1      cgd 	 * Loop transmitting all the characters we can.
   1136   1.1      cgd 	 */
   1137   1.1      cgd 	for (bp = buf; --nch >= 0; bp++) {
   1138   1.1      cgd 		fifo->data_char = *bp;
   1139   1.1      cgd 		pp->t_tail = next;
   1140   1.1      cgd 		/*
   1141   1.1      cgd 		 * If this is the first character,
   1142   1.1      cgd 		 * get the hardware moving right now.
   1143   1.1      cgd 		 */
   1144   1.1      cgd 		if (bp == buf) {
   1145   1.1      cgd 			tp->t_state |= TS_BUSY;
   1146   1.1      cgd 			SEM_LOCK(dcm);
   1147   1.1      cgd 			dcm->dcm_cmdtab[port].dcm_data |= CT_TX;
   1148   1.1      cgd 			dcm->dcm_cr |= (1 << port);
   1149   1.1      cgd 			SEM_UNLOCK(dcm);
   1150   1.1      cgd 		}
   1151   1.1      cgd 		tail = next;
   1152   1.1      cgd 		fifo = tail ? fifo+1 : &dcm->dcm_tfifos[3-port][0];
   1153   1.1      cgd 		next = (next + 1) & TX_MASK;
   1154   1.1      cgd 	}
   1155   1.1      cgd 	/*
   1156   1.1      cgd 	 * Head changed while we were loading the buffer,
   1157   1.1      cgd 	 * go back and load some more if we can.
   1158   1.1      cgd 	 */
   1159   1.7  mycroft 	if (tp->t_outq.c_cc && head != (pp->t_head & TX_MASK)) {
   1160  1.14  mycroft #ifdef DCMSTATS
   1161   1.1      cgd 		dsp->xrestarts++;
   1162   1.1      cgd #endif
   1163   1.1      cgd 		head = pp->t_head & TX_MASK;
   1164   1.1      cgd 		goto again;
   1165   1.1      cgd 	}
   1166   1.1      cgd 
   1167   1.1      cgd 	/*
   1168   1.1      cgd 	 * Kick it one last time in case it finished while we were
   1169   1.1      cgd 	 * loading the last bunch.
   1170   1.1      cgd 	 */
   1171   1.1      cgd 	if (bp > &buf[1]) {
   1172   1.1      cgd 		tp->t_state |= TS_BUSY;
   1173   1.1      cgd 		SEM_LOCK(dcm);
   1174   1.1      cgd 		dcm->dcm_cmdtab[port].dcm_data |= CT_TX;
   1175   1.1      cgd 		dcm->dcm_cr |= (1 << port);
   1176   1.1      cgd 		SEM_UNLOCK(dcm);
   1177   1.1      cgd 	}
   1178   1.1      cgd #ifdef DEBUG
   1179   1.1      cgd 	if (dcmdebug & DDB_INTR)
   1180  1.20  thorpej 		printf("%s port %d: dcmstart(%d): head %x tail %x outqcc %d\n",
   1181  1.20  thorpej 		    sc->sc_hd->hp_xname, port, head, tail, tp->t_outq.c_cc);
   1182   1.1      cgd #endif
   1183   1.1      cgd out:
   1184  1.14  mycroft #ifdef DCMSTATS
   1185   1.1      cgd 	dsp->xchars += tch;
   1186   1.1      cgd 	if (tch <= DCMXBSIZE)
   1187   1.1      cgd 		dsp->xsilo[tch]++;
   1188   1.1      cgd 	else
   1189   1.1      cgd 		dsp->xsilo[DCMXBSIZE+1]++;
   1190   1.1      cgd #endif
   1191   1.1      cgd 	splx(s);
   1192   1.1      cgd }
   1193   1.1      cgd 
   1194   1.1      cgd /*
   1195   1.1      cgd  * Stop output on a line.
   1196   1.1      cgd  */
   1197  1.17  mycroft int
   1198   1.1      cgd dcmstop(tp, flag)
   1199   1.1      cgd 	register struct tty *tp;
   1200  1.14  mycroft 	int flag;
   1201   1.1      cgd {
   1202   1.1      cgd 	int s;
   1203   1.1      cgd 
   1204   1.1      cgd 	s = spltty();
   1205   1.1      cgd 	if (tp->t_state & TS_BUSY) {
   1206   1.1      cgd 		/* XXX is there some way to safely stop transmission? */
   1207   1.1      cgd 		if ((tp->t_state&TS_TTSTOP) == 0)
   1208   1.1      cgd 			tp->t_state |= TS_FLUSH;
   1209   1.1      cgd 	}
   1210   1.1      cgd 	splx(s);
   1211   1.1      cgd }
   1212   1.1      cgd 
   1213   1.1      cgd /*
   1214   1.1      cgd  * Modem control
   1215   1.1      cgd  */
   1216   1.1      cgd dcmmctl(dev, bits, how)
   1217   1.1      cgd 	dev_t dev;
   1218   1.1      cgd 	int bits, how;
   1219   1.1      cgd {
   1220  1.20  thorpej 	struct dcm_softc *sc;
   1221  1.20  thorpej 	struct dcmdevice *dcm;
   1222  1.20  thorpej 	int s, unit, brd, port, hit = 0;
   1223  1.20  thorpej 
   1224  1.20  thorpej 	unit = DCMUNIT(dev);
   1225  1.20  thorpej 	brd = DCMBOARD(unit);
   1226  1.20  thorpej 	port = DCMPORT(unit);
   1227  1.20  thorpej 	sc = &dcm_softc[brd];
   1228  1.20  thorpej 	dcm = sc->sc_dcm;
   1229   1.1      cgd 
   1230   1.1      cgd #ifdef DEBUG
   1231   1.1      cgd 	if (dcmdebug & DDB_MODEM)
   1232  1.20  thorpej 		printf("%s port %d: dcmmctl: bits 0x%x how %x\n",
   1233  1.20  thorpej 		       sc->sc_hd->hp_xname, port, bits, how);
   1234   1.1      cgd #endif
   1235   1.1      cgd 
   1236   1.1      cgd 	s = spltty();
   1237  1.20  thorpej 
   1238   1.1      cgd 	switch (how) {
   1239   1.1      cgd 	case DMSET:
   1240  1.20  thorpej 		sc->sc_modem[port]->mdmout = bits;
   1241   1.1      cgd 		hit++;
   1242   1.1      cgd 		break;
   1243   1.1      cgd 
   1244   1.1      cgd 	case DMBIS:
   1245  1.20  thorpej 		sc->sc_modem[port]->mdmout |= bits;
   1246   1.1      cgd 		hit++;
   1247   1.1      cgd 		break;
   1248   1.1      cgd 
   1249   1.1      cgd 	case DMBIC:
   1250  1.20  thorpej 		sc->sc_modem[port]->mdmout &= ~bits;
   1251   1.1      cgd 		hit++;
   1252   1.1      cgd 		break;
   1253   1.1      cgd 
   1254   1.1      cgd 	case DMGET:
   1255  1.20  thorpej 		bits = sc->sc_modem[port]->mdmin;
   1256  1.20  thorpej 		if (sc->sc_flags & DCM_STDDCE)
   1257   1.1      cgd 			bits = hp2dce_in(bits);
   1258   1.1      cgd 		break;
   1259   1.1      cgd 	}
   1260   1.1      cgd 	if (hit) {
   1261   1.1      cgd 		SEM_LOCK(dcm);
   1262   1.1      cgd 		dcm->dcm_modemchng |= 1<<(unit & 3);
   1263   1.1      cgd 		dcm->dcm_cr |= CR_MODM;
   1264   1.1      cgd 		SEM_UNLOCK(dcm);
   1265   1.1      cgd 		DELAY(10); /* delay until done */
   1266   1.1      cgd 		(void) splx(s);
   1267   1.1      cgd 	}
   1268   1.1      cgd 	return (bits);
   1269   1.1      cgd }
   1270   1.1      cgd 
   1271   1.1      cgd /*
   1272   1.1      cgd  * Set board to either interrupt per-character or at a fixed interval.
   1273   1.1      cgd  */
   1274   1.1      cgd dcmsetischeme(brd, flags)
   1275   1.1      cgd 	int brd, flags;
   1276   1.1      cgd {
   1277  1.20  thorpej 	struct dcm_softc *sc = &dcm_softc[brd];
   1278  1.20  thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
   1279  1.20  thorpej 	struct dcmischeme *dis = &sc->sc_scheme;
   1280  1.20  thorpej 	int i;
   1281   1.1      cgd 	u_char mask;
   1282   1.1      cgd 	int perchar = flags & DIS_PERCHAR;
   1283   1.1      cgd 
   1284   1.1      cgd #ifdef DEBUG
   1285   1.1      cgd 	if (dcmdebug & DDB_INTSCHM)
   1286  1.20  thorpej 		printf("%s: dcmsetischeme(%d): cur %d, ints %d, chars %d\n",
   1287  1.20  thorpej 		       sc->sc_hd->hp_xname, perchar, dis->dis_perchar,
   1288   1.1      cgd 		       dis->dis_intr, dis->dis_char);
   1289   1.1      cgd 	if ((flags & DIS_RESET) == 0 && perchar == dis->dis_perchar) {
   1290  1.20  thorpej 		printf("%s: dcmsetischeme: redundent request %d\n",
   1291  1.20  thorpej 		       sc->sc_hd->hp_xname, perchar);
   1292   1.1      cgd 		return;
   1293   1.1      cgd 	}
   1294   1.1      cgd #endif
   1295   1.1      cgd 	/*
   1296   1.1      cgd 	 * If perchar is non-zero, we enable interrupts on all characters
   1297   1.1      cgd 	 * otherwise we disable perchar interrupts and use periodic
   1298   1.1      cgd 	 * polling interrupts.
   1299   1.1      cgd 	 */
   1300   1.1      cgd 	dis->dis_perchar = perchar;
   1301   1.1      cgd 	mask = perchar ? 0xf : 0x0;
   1302   1.1      cgd 	for (i = 0; i < 256; i++)
   1303   1.1      cgd 		dcm->dcm_bmap[i].data_data = mask;
   1304   1.1      cgd 	/*
   1305   1.1      cgd 	 * Don't slow down tandem mode, interrupt on flow control
   1306   1.1      cgd 	 * chars for any port on the board.
   1307   1.1      cgd 	 */
   1308   1.1      cgd 	if (!perchar) {
   1309  1.20  thorpej 		register struct tty *tp;
   1310   1.1      cgd 		int c;
   1311   1.1      cgd 
   1312  1.20  thorpej 		for (i = 0; i < NDCMPORT; i++) {
   1313  1.20  thorpej 			tp = sc->sc_tty[i];
   1314  1.20  thorpej 
   1315   1.1      cgd 			if ((c = tp->t_cc[VSTART]) != _POSIX_VDISABLE)
   1316   1.1      cgd 				dcm->dcm_bmap[c].data_data |= (1 << i);
   1317   1.1      cgd 			if ((c = tp->t_cc[VSTOP]) != _POSIX_VDISABLE)
   1318   1.1      cgd 				dcm->dcm_bmap[c].data_data |= (1 << i);
   1319   1.1      cgd 		}
   1320   1.1      cgd 	}
   1321   1.1      cgd 	/*
   1322   1.1      cgd 	 * Board starts with timer disabled so if first call is to
   1323   1.1      cgd 	 * set perchar mode then we don't want to toggle the timer.
   1324   1.1      cgd 	 */
   1325   1.1      cgd 	if (flags == (DIS_RESET|DIS_PERCHAR))
   1326   1.1      cgd 		return;
   1327   1.1      cgd 	/*
   1328   1.1      cgd 	 * Toggle card 16.7ms interrupts (we first make sure that card
   1329   1.1      cgd 	 * has cleared the bit so it will see the toggle).
   1330   1.1      cgd 	 */
   1331   1.1      cgd 	while (dcm->dcm_cr & CR_TIMER)
   1332   1.1      cgd 		;
   1333   1.1      cgd 	SEM_LOCK(dcm);
   1334   1.1      cgd 	dcm->dcm_cr |= CR_TIMER;
   1335   1.1      cgd 	SEM_UNLOCK(dcm);
   1336   1.1      cgd }
   1337   1.1      cgd 
   1338  1.22  thorpej void
   1339  1.22  thorpej dcminit(dcm, port, rate)
   1340  1.22  thorpej 	struct dcmdevice *dcm;
   1341  1.22  thorpej 	int port, rate;
   1342  1.22  thorpej {
   1343  1.22  thorpej 	int s, mode;
   1344  1.22  thorpej 
   1345  1.22  thorpej 	mode = LC_8BITS | LC_1STOP;
   1346  1.22  thorpej 
   1347  1.22  thorpej 	s = splhigh();
   1348  1.22  thorpej 
   1349  1.22  thorpej 	/*
   1350  1.22  thorpej 	 * Wait for transmitter buffer to empty.
   1351  1.22  thorpej 	 */
   1352  1.22  thorpej 	while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
   1353  1.22  thorpej 		DELAY(DCM_USPERCH(rate));
   1354  1.22  thorpej 
   1355  1.22  thorpej 	/*
   1356  1.22  thorpej 	 * Make changes known to hardware.
   1357  1.22  thorpej 	 */
   1358  1.22  thorpej 	dcm->dcm_data[port].dcm_baud = ttspeedtab(rate, dcmspeedtab);
   1359  1.22  thorpej 	dcm->dcm_data[port].dcm_conf = mode;
   1360  1.22  thorpej 	SEM_LOCK(dcm);
   1361  1.22  thorpej 	dcm->dcm_cmdtab[port].dcm_data |= CT_CON;
   1362  1.22  thorpej 	dcm->dcm_cr |= (1 << port);
   1363  1.22  thorpej 	SEM_UNLOCK(dcm);
   1364  1.22  thorpej 
   1365  1.22  thorpej 	/*
   1366  1.22  thorpej 	 * Delay for config change to take place. Weighted by baud.
   1367  1.22  thorpej 	 * XXX why do we do this?
   1368  1.22  thorpej 	 */
   1369  1.22  thorpej 	DELAY(16 * DCM_USPERCH(rate));
   1370  1.22  thorpej 	splx(s);
   1371  1.22  thorpej }
   1372  1.22  thorpej 
   1373   1.1      cgd /*
   1374   1.1      cgd  * Following are all routines needed for DCM to act as console
   1375   1.1      cgd  */
   1376   1.1      cgd 
   1377  1.16  mycroft void
   1378   1.1      cgd dcmcnprobe(cp)
   1379   1.1      cgd 	struct consdev *cp;
   1380   1.1      cgd {
   1381  1.22  thorpej 	struct dcm_softc *sc;	/* XXX thorpej */
   1382  1.20  thorpej 	struct dcmdevice *dcm;
   1383  1.20  thorpej 	struct hp_hw *hw;
   1384   1.1      cgd 	int unit;
   1385   1.1      cgd 
   1386   1.1      cgd 	/* locate the major number */
   1387   1.1      cgd 	for (dcmmajor = 0; dcmmajor < nchrdev; dcmmajor++)
   1388   1.1      cgd 		if (cdevsw[dcmmajor].d_open == dcmopen)
   1389   1.1      cgd 			break;
   1390   1.1      cgd 
   1391   1.1      cgd 	/*
   1392  1.22  thorpej 	 * XXX FIX ME!
   1393   1.1      cgd 	 * Implicitly assigns the lowest select code DCM card found to be
   1394   1.1      cgd 	 * logical unit 0 (actually CONUNIT).  If your config file does
   1395   1.1      cgd 	 * anything different, you're screwed.
   1396   1.1      cgd 	 */
   1397   1.1      cgd 	for (hw = sc_table; hw->hw_type; hw++)
   1398   1.1      cgd 		if (HW_ISDEV(hw, D_COMMDCM) && !badaddr((short *)hw->hw_kva))
   1399   1.1      cgd 			break;
   1400   1.1      cgd 	if (!HW_ISDEV(hw, D_COMMDCM)) {
   1401   1.1      cgd 		cp->cn_pri = CN_DEAD;
   1402   1.1      cgd 		return;
   1403   1.1      cgd 	}
   1404  1.20  thorpej 
   1405   1.1      cgd 	unit = CONUNIT;
   1406  1.22  thorpej 	dcm = (struct dcmdevice *)hw->hw_kva;
   1407   1.1      cgd 
   1408   1.1      cgd 	/* initialize required fields */
   1409   1.1      cgd 	cp->cn_dev = makedev(dcmmajor, unit);
   1410  1.20  thorpej 	switch (dcm->dcm_rsid) {
   1411   1.1      cgd 	case DCMID:
   1412   1.1      cgd 		cp->cn_pri = CN_NORMAL;
   1413   1.1      cgd 		break;
   1414  1.20  thorpej 
   1415   1.1      cgd 	case DCMID|DCMCON:
   1416   1.1      cgd 		cp->cn_pri = CN_REMOTE;
   1417   1.1      cgd 		break;
   1418  1.20  thorpej 
   1419   1.1      cgd 	default:
   1420   1.1      cgd 		cp->cn_pri = CN_DEAD;
   1421   1.1      cgd 		return;
   1422   1.1      cgd 	}
   1423  1.20  thorpej 
   1424   1.1      cgd 	/*
   1425   1.1      cgd 	 * If dcmconsole is initialized, raise our priority.
   1426   1.1      cgd 	 */
   1427  1.20  thorpej 	if (dcmconsole == unit)
   1428   1.1      cgd 		cp->cn_pri = CN_REMOTE;
   1429  1.22  thorpej 
   1430  1.22  thorpej 	/*
   1431  1.22  thorpej 	 * If our priority is higher than the currently-remembered
   1432  1.22  thorpej 	 * DCM, stash our priority and address, for the benefit of
   1433  1.22  thorpej 	 * dcmcninit().
   1434  1.22  thorpej 	 */
   1435  1.22  thorpej 	if (cp->cn_pri > dcm_lastcnpri) {
   1436  1.22  thorpej 		dcm_lastcnpri = cp->cn_pri;
   1437  1.22  thorpej 		dcm_cn = dcm;
   1438  1.22  thorpej 	}
   1439  1.22  thorpej 
   1440   1.1      cgd #ifdef KGDB_CHEAT
   1441   1.1      cgd 	/*
   1442   1.1      cgd 	 * This doesn't currently work, at least not with ite consoles;
   1443   1.1      cgd 	 * the console hasn't been initialized yet.
   1444   1.1      cgd 	 */
   1445  1.20  thorpej 	if (major(kgdb_dev) == dcmmajor &&
   1446  1.20  thorpej 	    DCMBOARD(DCMUNIT(kgdb_dev)) == DCMBOARD(unit)) {
   1447  1.22  thorpej 		dcminit(dcm_cn, DCMPORT(DCMUNIT(kgdb_dev)), kgdb_rate);
   1448   1.1      cgd 		if (kgdb_debug_init) {
   1449   1.1      cgd 			/*
   1450   1.1      cgd 			 * We assume that console is ready for us...
   1451   1.1      cgd 			 * this assumes that a dca or ite console
   1452   1.1      cgd 			 * has been selected already and will init
   1453   1.1      cgd 			 * on the first putc.
   1454   1.1      cgd 			 */
   1455  1.20  thorpej 			printf("dcm%d: ", DCMUNIT(kgdb_dev));
   1456   1.1      cgd 			kgdb_connect(1);
   1457   1.1      cgd 		}
   1458   1.1      cgd 	}
   1459   1.1      cgd #endif
   1460   1.1      cgd }
   1461   1.1      cgd 
   1462  1.16  mycroft void
   1463   1.1      cgd dcmcninit(cp)
   1464   1.1      cgd 	struct consdev *cp;
   1465   1.1      cgd {
   1466  1.22  thorpej 	int unit = DCMUNIT(cp->cn_dev);
   1467  1.22  thorpej 	int port = DCMPORT(unit);
   1468  1.20  thorpej 
   1469  1.22  thorpej 	dcminit(dcm_cn, port, dcmdefaultrate);
   1470   1.1      cgd 	dcmconsinit = 1;
   1471  1.20  thorpej 	dcmconsole = DCMUNIT(cp->cn_dev);
   1472   1.1      cgd }
   1473   1.1      cgd 
   1474  1.17  mycroft int
   1475   1.1      cgd dcmcngetc(dev)
   1476   1.1      cgd 	dev_t dev;
   1477   1.1      cgd {
   1478  1.20  thorpej 	struct dcmrfifo *fifo;
   1479  1.20  thorpej 	struct dcmpreg *pp;
   1480  1.20  thorpej 	u_int head;
   1481  1.22  thorpej 	int s, c, stat, unit, port;
   1482  1.20  thorpej 
   1483  1.20  thorpej 	unit = DCMUNIT(dev);
   1484  1.20  thorpej 	port = DCMPORT(unit);
   1485   1.1      cgd 
   1486  1.22  thorpej 	/*
   1487  1.22  thorpej 	 * NOTE: This assumes that unit == dcmconsole.  If it doesn't,
   1488  1.22  thorpej 	 * well, you lose.  (It's also extremely unlikely that will ever
   1489  1.22  thorpej 	 * not be the case.)
   1490  1.22  thorpej 	 */
   1491  1.22  thorpej 
   1492  1.22  thorpej 	pp = dcm_preg(dcm_cn, port);
   1493  1.20  thorpej 
   1494   1.1      cgd 	s = splhigh();
   1495   1.1      cgd 	head = pp->r_head & RX_MASK;
   1496  1.22  thorpej 	fifo = &dcm_cn->dcm_rfifos[3-port][head>>1];
   1497   1.1      cgd 	while (head == (pp->r_tail & RX_MASK))
   1498   1.1      cgd 		;
   1499   1.1      cgd 	/*
   1500   1.1      cgd 	 * If board interrupts are enabled, just let our received char
   1501   1.1      cgd 	 * interrupt through in case some other port on the board was
   1502   1.1      cgd 	 * busy.  Otherwise we must clear the interrupt.
   1503   1.1      cgd 	 */
   1504  1.22  thorpej 	SEM_LOCK(dcm_cn);
   1505  1.22  thorpej 	if ((dcm_cn->dcm_ic & IC_IE) == 0)
   1506  1.22  thorpej 		stat = dcm_cn->dcm_iir;
   1507  1.22  thorpej 	SEM_UNLOCK(dcm_cn);
   1508   1.1      cgd 	c = fifo->data_char;
   1509   1.1      cgd 	stat = fifo->data_stat;
   1510   1.1      cgd 	pp->r_head = (head + 2) & RX_MASK;
   1511   1.1      cgd 	splx(s);
   1512   1.1      cgd 	return (c);
   1513   1.1      cgd }
   1514   1.1      cgd 
   1515   1.1      cgd /*
   1516   1.1      cgd  * Console kernel output character routine.
   1517   1.1      cgd  */
   1518  1.16  mycroft void
   1519   1.1      cgd dcmcnputc(dev, c)
   1520   1.1      cgd 	dev_t dev;
   1521   1.1      cgd 	int c;
   1522   1.1      cgd {
   1523  1.20  thorpej 	struct dcmpreg *pp;
   1524   1.1      cgd 	unsigned tail;
   1525  1.22  thorpej 	int s, unit, port, stat;
   1526   1.1      cgd 
   1527  1.20  thorpej 	unit = DCMUNIT(dev);
   1528  1.20  thorpej 	port = DCMPORT(unit);
   1529  1.20  thorpej 
   1530  1.22  thorpej 	/*
   1531  1.22  thorpej 	 * NOTE: This assumes that unit == dcmconsole.  If it doesn't,
   1532  1.22  thorpej 	 * well, you lose.  (It's also extremely unlikely that will ever
   1533  1.22  thorpej 	 * not be the case.)
   1534  1.22  thorpej 	 */
   1535  1.22  thorpej 
   1536  1.22  thorpej 	pp = dcm_preg(dcm_cn, port);
   1537  1.20  thorpej 
   1538   1.1      cgd 	s = splhigh();
   1539   1.1      cgd #ifdef KGDB
   1540   1.1      cgd 	if (dev != kgdb_dev)
   1541   1.1      cgd #endif
   1542   1.1      cgd 	if (dcmconsinit == 0) {
   1543  1.22  thorpej 		dcminit(dcm_cn, port, dcmdefaultrate);
   1544   1.1      cgd 		dcmconsinit = 1;
   1545   1.1      cgd 	}
   1546   1.1      cgd 	tail = pp->t_tail & TX_MASK;
   1547   1.1      cgd 	while (tail != (pp->t_head & TX_MASK))
   1548   1.1      cgd 		;
   1549  1.22  thorpej 	dcm_cn->dcm_tfifos[3-port][tail].data_char = c;
   1550   1.1      cgd 	pp->t_tail = tail = (tail + 1) & TX_MASK;
   1551  1.22  thorpej 	SEM_LOCK(dcm_cn);
   1552  1.22  thorpej 	dcm_cn->dcm_cmdtab[port].dcm_data |= CT_TX;
   1553  1.22  thorpej 	dcm_cn->dcm_cr |= (1 << port);
   1554  1.22  thorpej 	SEM_UNLOCK(dcm_cn);
   1555   1.1      cgd 	while (tail != (pp->t_head & TX_MASK))
   1556   1.1      cgd 		;
   1557   1.1      cgd 	/*
   1558   1.1      cgd 	 * If board interrupts are enabled, just let our completion
   1559   1.1      cgd 	 * interrupt through in case some other port on the board
   1560   1.1      cgd 	 * was busy.  Otherwise we must clear the interrupt.
   1561   1.1      cgd 	 */
   1562  1.22  thorpej 	if ((dcm_cn->dcm_ic & IC_IE) == 0) {
   1563  1.22  thorpej 		SEM_LOCK(dcm_cn);
   1564  1.22  thorpej 		stat = dcm_cn->dcm_iir;
   1565  1.22  thorpej 		SEM_UNLOCK(dcm_cn);
   1566   1.1      cgd 	}
   1567   1.1      cgd 	splx(s);
   1568   1.1      cgd }
   1569  1.22  thorpej #endif /* NDCM > 0 */
   1570