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