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