Home | History | Annotate | Line # | Download | only in qbus
dhu.c revision 1.44
      1 /*	$NetBSD: dhu.c,v 1.44 2006/05/15 20:44:04 yamt Exp $	*/
      2 /*
      3  * Copyright (c) 2003, Hugh Graham.
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Ralph Campbell and Rick Macklem.
      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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1996  Ken C. Wellsch.  All rights reserved.
     37  *
     38  * This code is derived from software contributed to Berkeley by
     39  * Ralph Campbell and Rick Macklem.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. All advertising materials mentioning features or use of this software
     50  *    must display the following acknowledgement:
     51  *	This product includes software developed by the University of
     52  *	California, Berkeley and its contributors.
     53  * 4. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: dhu.c,v 1.44 2006/05/15 20:44:04 yamt Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/tty.h>
     77 #include <sys/proc.h>
     78 #include <sys/buf.h>
     79 #include <sys/conf.h>
     80 #include <sys/file.h>
     81 #include <sys/uio.h>
     82 #include <sys/kernel.h>
     83 #include <sys/syslog.h>
     84 #include <sys/device.h>
     85 #include <sys/kauth.h>
     86 
     87 #include <machine/bus.h>
     88 #include <machine/scb.h>
     89 
     90 #include <dev/qbus/ubavar.h>
     91 
     92 #include <dev/qbus/dhureg.h>
     93 
     94 #include "ioconf.h"
     95 
     96 /* A DHU-11 has 16 ports while a DHV-11 has only 8. We use 16 by default */
     97 
     98 #define	NDHULINE	16
     99 
    100 #define DHU_M2U(c)	((c)>>4)	/* convert minor(dev) to unit # */
    101 #define DHU_LINE(u)	((u)&0xF)	/* extract line # from minor(dev) */
    102 
    103 struct	dhu_softc {
    104 	struct	device	sc_dev;		/* Device struct used by config */
    105 	struct	evcnt	sc_rintrcnt;	/* Interrupt statistics */
    106 	struct	evcnt	sc_tintrcnt;	/* Interrupt statistics */
    107 	int		sc_type;	/* controller type, DHU or DHV */
    108 	int		sc_lines;	/* number of lines */
    109 	bus_space_tag_t	sc_iot;
    110 	bus_space_handle_t sc_ioh;
    111 	bus_dma_tag_t	sc_dmat;
    112 	struct {
    113 		struct	tty *dhu_tty;	/* what we work on */
    114 		bus_dmamap_t dhu_dmah;
    115 		int	dhu_state;	/* to manage TX output status */
    116 		short	dhu_cc;		/* character count on TX */
    117 		short	dhu_modem;	/* modem bits state */
    118 	} sc_dhu[NDHULINE];
    119 };
    120 
    121 #define IS_DHU			16	/* Unibus DHU-11 board linecount */
    122 #define IS_DHV			 8	/* Q-bus DHV-11 or DHQ-11 */
    123 
    124 #define STATE_IDLE		000	/* no current output in progress */
    125 #define STATE_DMA_RUNNING	001	/* DMA TX in progress */
    126 #define STATE_DMA_STOPPED	002	/* DMA TX was aborted */
    127 #define STATE_TX_ONE_CHAR	004	/* did a single char directly */
    128 
    129 /* Flags used to monitor modem bits, make them understood outside driver */
    130 
    131 #define DML_DTR		TIOCM_DTR
    132 #define DML_RTS		TIOCM_RTS
    133 #define DML_CTS		TIOCM_CTS
    134 #define DML_DCD		TIOCM_CD
    135 #define DML_RI		TIOCM_RI
    136 #define DML_DSR		TIOCM_DSR
    137 #define DML_BRK		0100000		/* no equivalent, we will mask */
    138 
    139 #define DHU_READ_WORD(reg) \
    140 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg)
    141 #define DHU_WRITE_WORD(reg, val) \
    142 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val)
    143 #define DHU_READ_BYTE(reg) \
    144 	bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg)
    145 #define DHU_WRITE_BYTE(reg, val) \
    146 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val)
    147 
    148 
    149 /*  On a stock DHV, channel pairs (0/1, 2/3, etc.) must use */
    150 /* a baud rate from the same group.  So limiting to B is likely */
    151 /* best, although clone boards like the ABLE QHV allow all settings. */
    152 
    153 static const struct speedtab dhuspeedtab[] = {
    154   {       0,	0		},	/* Groups  */
    155   {      50,	DHU_LPR_B50	},	/* A	   */
    156   {      75,	DHU_LPR_B75	},	/* 	 B */
    157   {     110,	DHU_LPR_B110	},	/* A and B */
    158   {     134,	DHU_LPR_B134	},	/* A and B */
    159   {     150,	DHU_LPR_B150	},	/* 	 B */
    160   {     300,	DHU_LPR_B300	},	/* A and B */
    161   {     600,	DHU_LPR_B600	},	/* A and B */
    162   {    1200,	DHU_LPR_B1200	},	/* A and B */
    163   {    1800,	DHU_LPR_B1800	},	/* 	 B */
    164   {    2000,	DHU_LPR_B2000	},	/* 	 B */
    165   {    2400,	DHU_LPR_B2400	},	/* A and B */
    166   {    4800,	DHU_LPR_B4800	},	/* A and B */
    167   {    7200,	DHU_LPR_B7200	},	/* A	   */
    168   {    9600,	DHU_LPR_B9600	},	/* A and B */
    169   {   19200,	DHU_LPR_B19200	},	/* 	 B */
    170   {   38400,	DHU_LPR_B38400	},	/* A	   */
    171   {      -1,	-1		}
    172 };
    173 
    174 static int	dhu_match(struct device *, struct cfdata *, void *);
    175 static void	dhu_attach(struct device *, struct device *, void *);
    176 static	void	dhurint(void *);
    177 static	void	dhuxint(void *);
    178 static	void	dhustart(struct tty *);
    179 static	int	dhuparam(struct tty *, struct termios *);
    180 static	int	dhuiflow(struct tty *, int);
    181 static unsigned	dhumctl(struct dhu_softc *,int, int, int);
    182 
    183 CFATTACH_DECL(dhu, sizeof(struct dhu_softc),
    184     dhu_match, dhu_attach, NULL, NULL);
    185 
    186 dev_type_open(dhuopen);
    187 dev_type_close(dhuclose);
    188 dev_type_read(dhuread);
    189 dev_type_write(dhuwrite);
    190 dev_type_ioctl(dhuioctl);
    191 dev_type_stop(dhustop);
    192 dev_type_tty(dhutty);
    193 dev_type_poll(dhupoll);
    194 
    195 const struct cdevsw dhu_cdevsw = {
    196 	dhuopen, dhuclose, dhuread, dhuwrite, dhuioctl,
    197 	dhustop, dhutty, dhupoll, nommap, ttykqfilter, D_TTY
    198 };
    199 
    200 /* Autoconfig handles: setup the controller to interrupt, */
    201 /* then complete the housecleaning for full operation */
    202 
    203 static int
    204 dhu_match(parent, cf, aux)
    205 	struct device *parent;
    206 	struct cfdata *cf;
    207 	void *aux;
    208 {
    209 	struct uba_attach_args *ua = aux;
    210 	int n;
    211 
    212 	/* Reset controller to initialize, enable TX/RX interrupts */
    213 	/* to catch floating vector info elsewhere when completed */
    214 
    215 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, DHU_UBA_CSR,
    216 	    DHU_CSR_MASTER_RESET | DHU_CSR_RXIE | DHU_CSR_TXIE);
    217 
    218 	/* Now wait up to 3 seconds for self-test to complete. */
    219 
    220 	for (n = 0; n < 300; n++) {
    221 		DELAY(10000);
    222 		if ((bus_space_read_2(ua->ua_iot, ua->ua_ioh, DHU_UBA_CSR) &
    223 		    DHU_CSR_MASTER_RESET) == 0)
    224 			break;
    225 	}
    226 
    227 	/* If the RESET did not clear after 3 seconds, */
    228 	/* the controller must be broken. */
    229 
    230 	if (n >= 300)
    231 		return 0;
    232 
    233 	/* Check whether diagnostic run has signalled a failure. */
    234 
    235 	if ((bus_space_read_2(ua->ua_iot, ua->ua_ioh, DHU_UBA_CSR) &
    236 	    DHU_CSR_DIAG_FAIL) != 0)
    237 		return 0;
    238 
    239 	return 1;
    240 }
    241 
    242 static void
    243 dhu_attach(parent, self, aux)
    244 	struct device *parent, *self;
    245 	void *aux;
    246 {
    247 	struct dhu_softc *sc = device_private(self);
    248 	struct uba_attach_args *ua = aux;
    249 	unsigned c;
    250 	int n, i;
    251 
    252 	sc->sc_iot = ua->ua_iot;
    253 	sc->sc_ioh = ua->ua_ioh;
    254 	sc->sc_dmat = ua->ua_dmat;
    255 	/* Process the 8 bytes of diagnostic info put into */
    256 	/* the FIFO following the master reset operation. */
    257 
    258 	printf("\n%s:", self->dv_xname);
    259 	for (n = 0; n < 8; n++) {
    260 		c = DHU_READ_WORD(DHU_UBA_RBUF);
    261 
    262 		if ((c&DHU_DIAG_CODE) == DHU_DIAG_CODE) {
    263 			if ((c&0200) == 0000)
    264 				printf(" rom(%d) version %d",
    265 					((c>>1)&01), ((c>>2)&037));
    266 			else if (((c>>2)&07) != 0)
    267 				printf(" diag-error(proc%d)=%x",
    268 					((c>>1)&01), ((c>>2)&07));
    269 		}
    270 	}
    271 
    272 	c = DHU_READ_WORD(DHU_UBA_STAT);
    273 
    274 	sc->sc_type = (c & DHU_STAT_DHU)? IS_DHU: IS_DHV;
    275 
    276 	sc->sc_lines = 8;	/* default */
    277 	if (sc->sc_type == IS_DHU && (c & DHU_STAT_MDL))
    278 		sc->sc_lines = 16;
    279 
    280 	printf("\n%s: DH%s-11\n", self->dv_xname,
    281 	    sc->sc_type == IS_DHU ? "U" : "V");
    282 
    283 	for (i = 0; i < sc->sc_lines; i++) {
    284 		struct tty *tp;
    285 		tp = sc->sc_dhu[i].dhu_tty = ttymalloc();
    286 		sc->sc_dhu[i].dhu_state = STATE_IDLE;
    287 		bus_dmamap_create(sc->sc_dmat, tp->t_outq.c_cn, 1,
    288 		    tp->t_outq.c_cn, 0, BUS_DMA_ALLOCNOW|BUS_DMA_NOWAIT,
    289 		    &sc->sc_dhu[i].dhu_dmah);
    290 		bus_dmamap_load(sc->sc_dmat, sc->sc_dhu[i].dhu_dmah,
    291 		    tp->t_outq.c_cs, tp->t_outq.c_cn, 0, BUS_DMA_NOWAIT);
    292 
    293 	}
    294 
    295 	/* Now establish RX & TX interrupt handlers */
    296 
    297 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec,
    298 		dhurint, sc, &sc->sc_rintrcnt);
    299 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec + 4,
    300 		dhuxint, sc, &sc->sc_tintrcnt);
    301 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    302 		sc->sc_dev.dv_xname, "rintr");
    303 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    304 		sc->sc_dev.dv_xname, "tintr");
    305 }
    306 
    307 /* Receiver Interrupt */
    308 
    309 static void
    310 dhurint(arg)
    311 	void *arg;
    312 {
    313 	struct	dhu_softc *sc = arg;
    314 	struct tty *tp;
    315 	int cc, line;
    316 	unsigned c, delta;
    317 	int overrun = 0;
    318 
    319 	while ((c = DHU_READ_WORD(DHU_UBA_RBUF)) & DHU_RBUF_DATA_VALID) {
    320 
    321 		/* Ignore diagnostic FIFO entries. */
    322 
    323 		if ((c & DHU_DIAG_CODE) == DHU_DIAG_CODE)
    324 			continue;
    325 
    326 		cc = c & 0xFF;
    327 		line = DHU_LINE(c>>8);
    328 		tp = sc->sc_dhu[line].dhu_tty;
    329 
    330 		/* LINK.TYPE is set so we get modem control FIFO entries */
    331 
    332 		if ((c & DHU_DIAG_CODE) == DHU_MODEM_CODE) {
    333 			c = (c << 8);
    334 			/* Do MDMBUF flow control, wakeup sleeping opens */
    335 			if (c & DHU_STAT_DCD) {
    336 				if (!(tp->t_state & TS_CARR_ON))
    337 				    (void)(*tp->t_linesw->l_modem)(tp, 1);
    338 			}
    339 			else if ((tp->t_state & TS_CARR_ON) &&
    340 				(*tp->t_linesw->l_modem)(tp, 0) == 0)
    341 					(void) dhumctl(sc, line, 0, DMSET);
    342 
    343 			/* Do CRTSCTS flow control */
    344 			delta = c ^ sc->sc_dhu[line].dhu_modem;
    345 			sc->sc_dhu[line].dhu_modem = c;
    346 			if ((delta & DHU_STAT_CTS) &&
    347 			    (tp->t_state & TS_ISOPEN) &&
    348 			    (tp->t_cflag & CRTSCTS)) {
    349 				if (c & DHU_STAT_CTS) {
    350 					tp->t_state &= ~TS_TTSTOP;
    351 					ttstart(tp);
    352 				} else {
    353 					tp->t_state |= TS_TTSTOP;
    354 					dhustop(tp, 0);
    355 				}
    356 			}
    357 			continue;
    358 		}
    359 
    360 		if (!(tp->t_state & TS_ISOPEN)) {
    361 			wakeup((caddr_t)&tp->t_rawq);
    362 			continue;
    363 		}
    364 
    365 		if ((c & DHU_RBUF_OVERRUN_ERR) && overrun == 0) {
    366 			log(LOG_WARNING, "%s: silo overflow, line %d\n",
    367 				sc->sc_dev.dv_xname, line);
    368 			overrun = 1;
    369 		}
    370 		/* A BREAK key will appear as a NULL with a framing error */
    371 		if (c & DHU_RBUF_FRAMING_ERR)
    372 			cc |= TTY_FE;
    373 		if (c & DHU_RBUF_PARITY_ERR)
    374 			cc |= TTY_PE;
    375 
    376 		(*tp->t_linesw->l_rint)(cc, tp);
    377 	}
    378 }
    379 
    380 /* Transmitter Interrupt */
    381 
    382 static void
    383 dhuxint(arg)
    384 	void *arg;
    385 {
    386 	struct	dhu_softc *sc = arg;
    387 	struct tty *tp;
    388 	int line, i;
    389 
    390 	while ((i = DHU_READ_BYTE(DHU_UBA_CSR_HI)) & (DHU_CSR_TX_ACTION >> 8)) {
    391 
    392 		line = DHU_LINE(i);
    393 		tp = sc->sc_dhu[line].dhu_tty;
    394 
    395 		if (i & (DHU_CSR_TX_DMA_ERROR >> 8))
    396 			printf("%s: DMA ERROR on line: %d\n",
    397 			    sc->sc_dev.dv_xname, line);
    398 		if (i & (DHU_CSR_DIAG_FAIL >> 8))
    399 			printf("%s: DIAG FAIL on line: %d\n",
    400 			    sc->sc_dev.dv_xname, line);
    401 
    402 		tp->t_state &= ~TS_BUSY;
    403 		if (tp->t_state & TS_FLUSH)
    404 			tp->t_state &= ~TS_FLUSH;
    405 		else {
    406 			if (sc->sc_dhu[line].dhu_state == STATE_DMA_STOPPED)
    407 				sc->sc_dhu[line].dhu_cc -=
    408 				    DHU_READ_WORD(DHU_UBA_TBUFCNT);
    409 			ndflush(&tp->t_outq, sc->sc_dhu[line].dhu_cc);
    410 			sc->sc_dhu[line].dhu_cc = 0;
    411 		}
    412 
    413 		sc->sc_dhu[line].dhu_state = STATE_IDLE;
    414 
    415 		(*tp->t_linesw->l_start)(tp);
    416 	}
    417 }
    418 
    419 int
    420 dhuopen(dev, flag, mode, l)
    421 	dev_t dev;
    422 	int flag, mode;
    423 	struct lwp *l;
    424 {
    425 	struct tty *tp;
    426 	int unit, line;
    427 	struct dhu_softc *sc;
    428 	int s, error = 0;
    429 
    430 	unit = DHU_M2U(minor(dev));
    431 	line = DHU_LINE(minor(dev));
    432 
    433 	if (unit >= dhu_cd.cd_ndevs || dhu_cd.cd_devs[unit] == NULL)
    434 		return (ENXIO);
    435 
    436 	sc = dhu_cd.cd_devs[unit];
    437 
    438 	if (line >= sc->sc_lines)
    439 		return ENXIO;
    440 
    441 	if (sc->sc_type == IS_DHU) {
    442 		s = spltty();	/* CSR 3:0 must be 0 */
    443 		DHU_WRITE_BYTE(DHU_UBA_CSR, DHU_CSR_RXIE);
    444 		DHU_WRITE_BYTE(DHU_UBA_RXTIME, 10);
    445 		splx(s);	/* RX int delay 10ms */
    446 	}
    447 
    448 	s = spltty();
    449 	DHU_WRITE_BYTE(DHU_UBA_CSR, DHU_CSR_RXIE | line);
    450 	sc->sc_dhu[line].dhu_modem = DHU_READ_WORD(DHU_UBA_STAT);
    451 	(void) splx(s);
    452 
    453 	tp = sc->sc_dhu[line].dhu_tty;
    454 
    455 	tp->t_oproc   = dhustart;
    456 	tp->t_param   = dhuparam;
    457 	tp->t_hwiflow = dhuiflow;
    458 	tp->t_dev = dev;
    459 	if ((tp->t_state & TS_ISOPEN) == 0) {
    460 		ttychars(tp);
    461 		if (tp->t_ispeed == 0) {
    462 			tp->t_iflag = TTYDEF_IFLAG;
    463 			tp->t_oflag = TTYDEF_OFLAG;
    464 			tp->t_cflag = TTYDEF_CFLAG;
    465 			tp->t_lflag = TTYDEF_LFLAG;
    466 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    467 		}
    468 		(void) dhuparam(tp, &tp->t_termios);
    469 		ttsetwater(tp);
    470 	} else if ((tp->t_state & TS_XCLUDE) &&
    471 	    kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER,
    472 	    &l->l_proc->p_acflag) != 0)
    473 		return (EBUSY);
    474 	/* Use DMBIS and *not* DMSET or else we clobber incoming bits */
    475 	if (dhumctl(sc, line, DML_DTR|DML_RTS, DMBIS) & DML_DCD)
    476 		tp->t_state |= TS_CARR_ON;
    477 	s = spltty();
    478 	while (!(flag & O_NONBLOCK) && !(tp->t_cflag & CLOCAL) &&
    479 	    !(tp->t_state & TS_CARR_ON)) {
    480 		tp->t_wopen++;
    481 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    482 				TTIPRI | PCATCH, ttopen, 0);
    483 		tp->t_wopen--;
    484 		if (error)
    485 			break;
    486 	}
    487 	(void) splx(s);
    488 	if (error)
    489 		return (error);
    490 	return ((*tp->t_linesw->l_open)(dev, tp));
    491 }
    492 
    493 /*ARGSUSED*/
    494 int
    495 dhuclose(dev, flag, mode, l)
    496 	dev_t dev;
    497 	int flag, mode;
    498 	struct lwp *l;
    499 {
    500 	struct tty *tp;
    501 	int unit, line;
    502 	struct dhu_softc *sc;
    503 
    504 	unit = DHU_M2U(minor(dev));
    505 	line = DHU_LINE(minor(dev));
    506 
    507 	sc = dhu_cd.cd_devs[unit];
    508 
    509 	tp = sc->sc_dhu[line].dhu_tty;
    510 
    511 	(*tp->t_linesw->l_close)(tp, flag);
    512 
    513 	/* Make sure a BREAK state is not left enabled. */
    514 
    515 	(void) dhumctl(sc, line, DML_BRK, DMBIC);
    516 
    517 	/* Do a hangup if so required. */
    518 
    519 	if ((tp->t_cflag & HUPCL) || tp->t_wopen ||
    520 	    !(tp->t_state & TS_ISOPEN))
    521 		(void) dhumctl(sc, line, 0, DMSET);
    522 
    523 	return (ttyclose(tp));
    524 }
    525 
    526 int
    527 dhuread(dev, uio, flag)
    528 	dev_t dev;
    529 	struct uio *uio;
    530 	int flag;
    531 {
    532 	struct dhu_softc *sc;
    533 	struct tty *tp;
    534 
    535 	sc = dhu_cd.cd_devs[DHU_M2U(minor(dev))];
    536 
    537 	tp = sc->sc_dhu[DHU_LINE(minor(dev))].dhu_tty;
    538 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    539 }
    540 
    541 int
    542 dhuwrite(dev, uio, flag)
    543 	dev_t dev;
    544 	struct uio *uio;
    545 	int flag;
    546 {
    547 	struct dhu_softc *sc;
    548 	struct tty *tp;
    549 
    550 	sc = dhu_cd.cd_devs[DHU_M2U(minor(dev))];
    551 
    552 	tp = sc->sc_dhu[DHU_LINE(minor(dev))].dhu_tty;
    553 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    554 }
    555 
    556 int
    557 dhupoll(dev, events, l)
    558 	dev_t dev;
    559 	int events;
    560 	struct lwp *l;
    561 {
    562 	struct dhu_softc *sc;
    563 	struct tty *tp;
    564 
    565 	sc = dhu_cd.cd_devs[DHU_M2U(minor(dev))];
    566 
    567 	tp = sc->sc_dhu[DHU_LINE(minor(dev))].dhu_tty;
    568 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    569 }
    570 
    571 /*ARGSUSED*/
    572 int
    573 dhuioctl(dev, cmd, data, flag, l)
    574 	dev_t dev;
    575 	u_long cmd;
    576 	caddr_t data;
    577 	int flag;
    578 	struct lwp *l;
    579 {
    580 	struct dhu_softc *sc;
    581 	struct tty *tp;
    582 	int unit, line;
    583 	int error;
    584 
    585 	unit = DHU_M2U(minor(dev));
    586 	line = DHU_LINE(minor(dev));
    587 	sc = dhu_cd.cd_devs[unit];
    588 	tp = sc->sc_dhu[line].dhu_tty;
    589 
    590 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    591 	if (error != EPASSTHROUGH)
    592 		return (error);
    593 
    594 	error = ttioctl(tp, cmd, data, flag, l);
    595 	if (error != EPASSTHROUGH)
    596 		return (error);
    597 
    598 	switch (cmd) {
    599 
    600 	case TIOCSBRK:
    601 		(void) dhumctl(sc, line, DML_BRK, DMBIS);
    602 		break;
    603 
    604 	case TIOCCBRK:
    605 		(void) dhumctl(sc, line, DML_BRK, DMBIC);
    606 		break;
    607 
    608 	case TIOCSDTR:
    609 		(void) dhumctl(sc, line, DML_DTR|DML_RTS, DMBIS);
    610 		break;
    611 
    612 	case TIOCCDTR:
    613 		(void) dhumctl(sc, line, DML_DTR|DML_RTS, DMBIC);
    614 		break;
    615 
    616 	case TIOCMSET:
    617 		(void) dhumctl(sc, line, *(int *)data, DMSET);
    618 		break;
    619 
    620 	case TIOCMBIS:
    621 		(void) dhumctl(sc, line, *(int *)data, DMBIS);
    622 		break;
    623 
    624 	case TIOCMBIC:
    625 		(void) dhumctl(sc, line, *(int *)data, DMBIC);
    626 		break;
    627 
    628 	case TIOCMGET:
    629 		*(int *)data = (dhumctl(sc, line, 0, DMGET) & ~DML_BRK);
    630 		break;
    631 
    632 	default:
    633 		return (EPASSTHROUGH);
    634 	}
    635 	return (0);
    636 }
    637 
    638 struct tty *
    639 dhutty(dev)
    640 	dev_t dev;
    641 {
    642 	struct dhu_softc *sc = dhu_cd.cd_devs[DHU_M2U(minor(dev))];
    643 	struct tty *tp = sc->sc_dhu[DHU_LINE(minor(dev))].dhu_tty;
    644 	return (tp);
    645 }
    646 
    647 /*ARGSUSED*/
    648 void
    649 dhustop(tp, flag)
    650 	struct tty *tp;
    651 	int flag;
    652 {
    653 	struct dhu_softc *sc;
    654 	int line;
    655 	int s;
    656 
    657 	s = spltty();
    658 
    659 	if (tp->t_state & TS_BUSY) {
    660 
    661 		sc = dhu_cd.cd_devs[DHU_M2U(minor(tp->t_dev))];
    662 		line = DHU_LINE(minor(tp->t_dev));
    663 
    664 		if (sc->sc_dhu[line].dhu_state == STATE_DMA_RUNNING) {
    665 
    666 			sc->sc_dhu[line].dhu_state = STATE_DMA_STOPPED;
    667 
    668 			DHU_WRITE_BYTE(DHU_UBA_CSR, DHU_CSR_RXIE | line);
    669 			DHU_WRITE_WORD(DHU_UBA_LNCTRL,
    670 			    DHU_READ_WORD(DHU_UBA_LNCTRL) |
    671 			    DHU_LNCTRL_DMA_ABORT);
    672 		}
    673 
    674 		if (!(tp->t_state & TS_TTSTOP))
    675 			tp->t_state |= TS_FLUSH;
    676 	}
    677 	(void) splx(s);
    678 }
    679 
    680 static void
    681 dhustart(tp)
    682 	struct tty *tp;
    683 {
    684 	struct dhu_softc *sc;
    685 	int line, cc;
    686 	int addr;
    687 	int s;
    688 
    689 	s = spltty();
    690 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
    691 		goto out;
    692 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    693 		if (tp->t_state & TS_ASLEEP) {
    694 			tp->t_state &= ~TS_ASLEEP;
    695 			wakeup((caddr_t)&tp->t_outq);
    696 		}
    697 		selwakeup(&tp->t_wsel);
    698 	}
    699 	if (tp->t_outq.c_cc == 0)
    700 		goto out;
    701 	cc = ndqb(&tp->t_outq, 0);
    702 	if (cc == 0)
    703 		goto out;
    704 
    705 	tp->t_state |= TS_BUSY;
    706 
    707 	sc = dhu_cd.cd_devs[DHU_M2U(minor(tp->t_dev))];
    708 
    709 	line = DHU_LINE(minor(tp->t_dev));
    710 
    711 	DHU_WRITE_BYTE(DHU_UBA_CSR, DHU_CSR_RXIE | line);
    712 
    713 	sc->sc_dhu[line].dhu_cc = cc;
    714 
    715 	if (cc == 1 && sc->sc_type == IS_DHV) {
    716 
    717 		sc->sc_dhu[line].dhu_state = STATE_TX_ONE_CHAR;
    718 
    719 		DHU_WRITE_WORD(DHU_UBA_TXCHAR,
    720 		    DHU_TXCHAR_DATA_VALID | *tp->t_outq.c_cf);
    721 
    722 	} else {
    723 
    724 		sc->sc_dhu[line].dhu_state = STATE_DMA_RUNNING;
    725 
    726 		addr = sc->sc_dhu[line].dhu_dmah->dm_segs[0].ds_addr +
    727 			(tp->t_outq.c_cf - tp->t_outq.c_cs);
    728 
    729 		DHU_WRITE_WORD(DHU_UBA_TBUFCNT, cc);
    730 		DHU_WRITE_WORD(DHU_UBA_TBUFAD1, addr & 0xFFFF);
    731 		DHU_WRITE_WORD(DHU_UBA_TBUFAD2, ((addr>>16) & 0x3F) |
    732 		    DHU_TBUFAD2_TX_ENABLE);
    733 		DHU_WRITE_WORD(DHU_UBA_LNCTRL,
    734 		    DHU_READ_WORD(DHU_UBA_LNCTRL) & ~DHU_LNCTRL_DMA_ABORT);
    735 		DHU_WRITE_WORD(DHU_UBA_TBUFAD2,
    736 		    DHU_READ_WORD(DHU_UBA_TBUFAD2) | DHU_TBUFAD2_DMA_START);
    737 	}
    738 out:
    739 	(void) splx(s);
    740 	return;
    741 }
    742 
    743 static int
    744 dhuparam(tp, t)
    745 	struct tty *tp;
    746 	struct termios *t;
    747 {
    748 	struct dhu_softc *sc;
    749 	int cflag = t->c_cflag;
    750 	int ispeed = ttspeedtab(t->c_ispeed, dhuspeedtab);
    751 	int ospeed = ttspeedtab(t->c_ospeed, dhuspeedtab);
    752 	unsigned lpr, lnctrl;
    753 	int unit, line;
    754 	int s;
    755 
    756 	unit = DHU_M2U(minor(tp->t_dev));
    757 	line = DHU_LINE(minor(tp->t_dev));
    758 
    759 	sc = dhu_cd.cd_devs[unit];
    760 
    761 	/* check requested parameters */
    762 	if (ospeed < 0 || ispeed < 0)
    763 		return (EINVAL);
    764 
    765 	tp->t_ispeed = t->c_ispeed;
    766 	tp->t_ospeed = t->c_ospeed;
    767 	tp->t_cflag = cflag;
    768 
    769 	if (ospeed == 0) {
    770 		(void) dhumctl(sc, line, 0, DMSET);	/* hang up line */
    771 		return (0);
    772 	}
    773 
    774 	s = spltty();
    775 	DHU_WRITE_BYTE(DHU_UBA_CSR, DHU_CSR_RXIE | line);
    776 
    777 	lpr = ((ispeed&017)<<8) | ((ospeed&017)<<12) ;
    778 
    779 	switch (cflag & CSIZE) {
    780 
    781 	case CS5:
    782 		lpr |= DHU_LPR_5_BIT_CHAR;
    783 		break;
    784 
    785 	case CS6:
    786 		lpr |= DHU_LPR_6_BIT_CHAR;
    787 		break;
    788 
    789 	case CS7:
    790 		lpr |= DHU_LPR_7_BIT_CHAR;
    791 		break;
    792 
    793 	default:
    794 		lpr |= DHU_LPR_8_BIT_CHAR;
    795 		break;
    796 	}
    797 
    798 	if (cflag & PARENB)
    799 		lpr |= DHU_LPR_PARENB;
    800 	if (!(cflag & PARODD))
    801 		lpr |= DHU_LPR_EPAR;
    802 	if (cflag & CSTOPB)
    803 		lpr |= DHU_LPR_2_STOP;
    804 
    805 	DHU_WRITE_WORD(DHU_UBA_LPR, lpr);
    806 
    807 	DHU_WRITE_WORD(DHU_UBA_TBUFAD2,
    808 	    DHU_READ_WORD(DHU_UBA_TBUFAD2) | DHU_TBUFAD2_TX_ENABLE);
    809 
    810 	lnctrl = DHU_READ_WORD(DHU_UBA_LNCTRL);
    811 
    812 	/* Setting LINK.TYPE enables modem signal change interrupts. */
    813 
    814 	lnctrl |= (DHU_LNCTRL_RX_ENABLE | DHU_LNCTRL_LINK_TYPE);
    815 
    816 	/* Enable the auto XON/XOFF feature on the controller */
    817 
    818 	if (t->c_iflag & IXON)
    819 		lnctrl |= DHU_LNCTRL_OAUTO;
    820 	else
    821 		lnctrl &= ~DHU_LNCTRL_OAUTO;
    822 
    823 	if (t->c_iflag & IXOFF)
    824 		lnctrl |= DHU_LNCTRL_IAUTO;
    825 	else
    826 		lnctrl &= ~DHU_LNCTRL_IAUTO;
    827 
    828 	DHU_WRITE_WORD(DHU_UBA_LNCTRL, lnctrl);
    829 
    830 	(void) splx(s);
    831 	return (0);
    832 }
    833 
    834 static int
    835 dhuiflow(tp, flag)
    836 	struct tty *tp;
    837 	int flag;
    838 {
    839 	struct dhu_softc *sc;
    840 	int line = DHU_LINE(minor(tp->t_dev));
    841 
    842 	if (tp->t_cflag & CRTSCTS) {
    843 		sc = dhu_cd.cd_devs[DHU_M2U(minor(tp->t_dev))];
    844 		(void) dhumctl(sc, line, DML_RTS, ((flag)? DMBIC: DMBIS));
    845 		return (1);
    846 	}
    847 	return (0);
    848 }
    849 
    850 static unsigned
    851 dhumctl(sc, line, bits, how)
    852 	struct dhu_softc *sc;
    853 	int line, bits, how;
    854 {
    855 	unsigned status;
    856 	unsigned lnctrl;
    857 	unsigned mbits;
    858 	int s;
    859 
    860 	s = spltty();
    861 
    862 	DHU_WRITE_BYTE(DHU_UBA_CSR, DHU_CSR_RXIE | line);
    863 
    864 	mbits = 0;
    865 
    866 	/* external signals as seen from the port */
    867 
    868 	status = DHU_READ_WORD(DHU_UBA_STAT);
    869 
    870 	if (status & DHU_STAT_CTS)
    871 		mbits |= DML_CTS;
    872 
    873 	if (status & DHU_STAT_DCD)
    874 		mbits |= DML_DCD;
    875 
    876 	if (status & DHU_STAT_DSR)
    877 		mbits |= DML_DSR;
    878 
    879 	if (status & DHU_STAT_RI)
    880 		mbits |= DML_RI;
    881 
    882 	/* internal signals/state delivered to port */
    883 
    884 	lnctrl = DHU_READ_WORD(DHU_UBA_LNCTRL);
    885 
    886 	if (lnctrl & DHU_LNCTRL_RTS)
    887 		mbits |= DML_RTS;
    888 
    889 	if (lnctrl & DHU_LNCTRL_DTR)
    890 		mbits |= DML_DTR;
    891 
    892 	if (lnctrl & DHU_LNCTRL_BREAK)
    893 		mbits |= DML_BRK;
    894 
    895 	switch (how) {
    896 
    897 	case DMSET:
    898 		mbits = bits;
    899 		break;
    900 
    901 	case DMBIS:
    902 		mbits |= bits;
    903 		break;
    904 
    905 	case DMBIC:
    906 		mbits &= ~bits;
    907 		break;
    908 
    909 	case DMGET:
    910 		(void) splx(s);
    911 		return (mbits);
    912 	}
    913 
    914 	if (mbits & DML_RTS)
    915 		lnctrl |= DHU_LNCTRL_RTS;
    916 	else
    917 		lnctrl &= ~DHU_LNCTRL_RTS;
    918 
    919 	if (mbits & DML_DTR)
    920 		lnctrl |= DHU_LNCTRL_DTR;
    921 	else
    922 		lnctrl &= ~DHU_LNCTRL_DTR;
    923 
    924 	if (mbits & DML_BRK)
    925 		lnctrl |= DHU_LNCTRL_BREAK;
    926 	else
    927 		lnctrl &= ~DHU_LNCTRL_BREAK;
    928 
    929 	DHU_WRITE_WORD(DHU_UBA_LNCTRL, lnctrl);
    930 
    931 	(void) splx(s);
    932 	return (mbits);
    933 }
    934