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