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