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