Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.170
      1 /*	$NetBSD: com.c,v 1.170 2000/03/23 07:01:29 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      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) 1991 The Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. 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  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     72  */
     73 
     74 /*
     75  * COM driver, uses National Semiconductor NS16450/NS16550AF UART
     76  * Supports automatic hardware flow control on StarTech ST16C650A UART
     77  */
     78 
     79 #include "opt_ddb.h"
     80 #include "opt_com.h"
     81 
     82 #include "rnd.h"
     83 #if NRND > 0 && defined(RND_COM)
     84 #include <sys/rnd.h>
     85 #endif
     86 
     87 #include <sys/param.h>
     88 #include <sys/systm.h>
     89 #include <sys/ioctl.h>
     90 #include <sys/select.h>
     91 #include <sys/tty.h>
     92 #include <sys/proc.h>
     93 #include <sys/user.h>
     94 #include <sys/conf.h>
     95 #include <sys/file.h>
     96 #include <sys/uio.h>
     97 #include <sys/kernel.h>
     98 #include <sys/syslog.h>
     99 #include <sys/types.h>
    100 #include <sys/device.h>
    101 #include <sys/malloc.h>
    102 #include <sys/timepps.h>
    103 #include <sys/vnode.h>
    104 
    105 #include <machine/intr.h>
    106 #include <machine/bus.h>
    107 
    108 #include <dev/ic/comreg.h>
    109 #include <dev/ic/comvar.h>
    110 #include <dev/ic/ns16550reg.h>
    111 #include <dev/ic/st16650reg.h>
    112 #ifdef COM_HAYESP
    113 #include <dev/ic/hayespreg.h>
    114 #endif
    115 #define	com_lcr	com_cfcr
    116 #include <dev/cons.h>
    117 
    118 #include "com.h"
    119 
    120 #ifdef COM_HAYESP
    121 int comprobeHAYESP __P((bus_space_handle_t hayespioh, struct com_softc *sc));
    122 #endif
    123 
    124 #if defined(DDB) || defined(KGDB)
    125 static void com_enable_debugport __P((struct com_softc *));
    126 #endif
    127 void	com_attach_subr	__P((struct com_softc *sc));
    128 void	com_config	__P((struct com_softc *));
    129 void	com_shutdown	__P((struct com_softc *));
    130 int	comspeed	__P((long, long));
    131 static	u_char	cflag2lcr __P((tcflag_t));
    132 int	comparam	__P((struct tty *, struct termios *));
    133 void	comstart	__P((struct tty *));
    134 void	comstop		__P((struct tty *, int));
    135 int	comhwiflow	__P((struct tty *, int));
    136 
    137 void	com_loadchannelregs __P((struct com_softc *));
    138 void	com_hwiflow	__P((struct com_softc *));
    139 void	com_break	__P((struct com_softc *, int));
    140 void	com_modem	__P((struct com_softc *, int));
    141 void	tiocm_to_com	__P((struct com_softc *, int, int));
    142 int	com_to_tiocm	__P((struct com_softc *));
    143 void	com_iflush	__P((struct com_softc *));
    144 
    145 int	com_common_getc	__P((bus_space_tag_t, bus_space_handle_t));
    146 void	com_common_putc	__P((bus_space_tag_t, bus_space_handle_t, int));
    147 
    148 /* XXX: These belong elsewhere */
    149 cdev_decl(com);
    150 bdev_decl(com);
    151 
    152 int	comcngetc	__P((dev_t));
    153 void	comcnputc	__P((dev_t, int));
    154 void	comcnpollc	__P((dev_t, int));
    155 
    156 #define	integrate	static inline
    157 #ifdef __GENERIC_SOFT_INTERRUPTS
    158 void 	comsoft		__P((void *));
    159 #else
    160 #ifndef __NO_SOFT_SERIAL_INTERRUPT
    161 void 	comsoft		__P((void));
    162 #else
    163 void 	comsoft		__P((void *));
    164 struct callout comsoft_callout = CALLOUT_INITIALIZER;
    165 #endif
    166 #endif
    167 integrate void com_rxsoft	__P((struct com_softc *, struct tty *));
    168 integrate void com_txsoft	__P((struct com_softc *, struct tty *));
    169 integrate void com_stsoft	__P((struct com_softc *, struct tty *));
    170 integrate void com_schedrx	__P((struct com_softc *));
    171 void	comdiag		__P((void *));
    172 
    173 extern struct cfdriver com_cd;
    174 
    175 /*
    176  * Make this an option variable one can patch.
    177  * But be warned:  this must be a power of 2!
    178  */
    179 u_int com_rbuf_size = COM_RING_SIZE;
    180 
    181 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    182 u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
    183 u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
    184 
    185 static int	comconsaddr;
    186 static bus_space_tag_t comconstag;
    187 static bus_space_handle_t comconsioh;
    188 static int	comconsattached;
    189 static int comconsrate;
    190 static tcflag_t comconscflag;
    191 
    192 static int ppscap =
    193 	PPS_TSFMT_TSPEC |
    194 	PPS_CAPTUREASSERT |
    195 	PPS_CAPTURECLEAR |
    196 #ifdef  PPS_SYNC
    197 	PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR |
    198 #endif	/* PPS_SYNC */
    199 	PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
    200 
    201 #ifndef __GENERIC_SOFT_INTERRUPTS
    202 #ifdef __NO_SOFT_SERIAL_INTERRUPT
    203 volatile int	com_softintr_scheduled;
    204 #endif
    205 #endif
    206 
    207 #ifdef KGDB
    208 #include <sys/kgdb.h>
    209 
    210 static int com_kgdb_addr;
    211 static bus_space_tag_t com_kgdb_iot;
    212 static bus_space_handle_t com_kgdb_ioh;
    213 static int com_kgdb_attached;
    214 
    215 int	com_kgdb_getc __P((void *));
    216 void	com_kgdb_putc __P((void *, int));
    217 #endif /* KGDB */
    218 
    219 #define	COMUNIT_MASK	0x7ffff
    220 #define	COMDIALOUT_MASK	0x80000
    221 
    222 #define	COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    223 #define	COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    224 
    225 #define	COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    226 			 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
    227 
    228 #define	BR	BUS_SPACE_BARRIER_READ
    229 #define	BW	BUS_SPACE_BARRIER_WRITE
    230 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    231 
    232 int
    233 comspeed(speed, frequency)
    234 	long speed, frequency;
    235 {
    236 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    237 
    238 	int x, err;
    239 
    240 #if 0
    241 	if (speed == 0)
    242 		return (0);
    243 #endif
    244 	if (speed <= 0)
    245 		return (-1);
    246 	x = divrnd(frequency / 16, speed);
    247 	if (x <= 0)
    248 		return (-1);
    249 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    250 	if (err < 0)
    251 		err = -err;
    252 	if (err > COM_TOLERANCE)
    253 		return (-1);
    254 	return (x);
    255 
    256 #undef	divrnd(n, q)
    257 }
    258 
    259 #ifdef COM_DEBUG
    260 int	com_debug = 0;
    261 
    262 void comstatus __P((struct com_softc *, char *));
    263 void
    264 comstatus(sc, str)
    265 	struct com_softc *sc;
    266 	char *str;
    267 {
    268 	struct tty *tp = sc->sc_tty;
    269 
    270 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    271 	    sc->sc_dev.dv_xname, str,
    272 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    273 	    ISSET(sc->sc_msr, MSR_DCD) ? "+" : "-",
    274 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    275 	    ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
    276 	    sc->sc_tx_stopped ? "+" : "-");
    277 
    278 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    279 	    sc->sc_dev.dv_xname, str,
    280 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    281 	    ISSET(sc->sc_msr, MSR_CTS) ? "+" : "-",
    282 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    283 	    ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
    284 	    sc->sc_rx_flags);
    285 }
    286 #endif
    287 
    288 int
    289 comprobe1(iot, ioh)
    290 	bus_space_tag_t iot;
    291 	bus_space_handle_t ioh;
    292 {
    293 
    294 	/* force access to id reg */
    295 	bus_space_write_1(iot, ioh, com_lcr, LCR_8BITS);
    296 	bus_space_write_1(iot, ioh, com_iir, 0);
    297 	if ((bus_space_read_1(iot, ioh, com_lcr) != LCR_8BITS) ||
    298 	    (bus_space_read_1(iot, ioh, com_iir) & 0x38))
    299 		return (0);
    300 
    301 	return (1);
    302 }
    303 
    304 #ifdef COM_HAYESP
    305 int
    306 comprobeHAYESP(hayespioh, sc)
    307 	bus_space_handle_t hayespioh;
    308 	struct com_softc *sc;
    309 {
    310 	char	val, dips;
    311 	int	combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
    312 	bus_space_tag_t iot = sc->sc_iot;
    313 
    314 	/*
    315 	 * Hayes ESP cards have two iobases.  One is for compatibility with
    316 	 * 16550 serial chips, and at the same ISA PC base addresses.  The
    317 	 * other is for ESP-specific enhanced features, and lies at a
    318 	 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
    319 	 */
    320 
    321 	/* Test for ESP signature */
    322 	if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
    323 		return (0);
    324 
    325 	/*
    326 	 * ESP is present at ESP enhanced base address; unknown com port
    327 	 */
    328 
    329 	/* Get the dip-switch configurations */
    330 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
    331 	dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
    332 
    333 	/* Determine which com port this ESP card services: bits 0,1 of  */
    334 	/*  dips is the port # (0-3); combaselist[val] is the com_iobase */
    335 	if (sc->sc_iobase != combaselist[dips & 0x03])
    336 		return (0);
    337 
    338 	printf(": ESP");
    339 
    340  	/* Check ESP Self Test bits. */
    341 	/* Check for ESP version 2.0: bits 4,5,6 == 010 */
    342 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
    343 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
    344 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
    345 	if ((val & 0x70) < 0x20) {
    346 		printf("-old (%o)", val & 0x70);
    347 		/* we do not support the necessary features */
    348 		return (0);
    349 	}
    350 
    351 	/* Check for ability to emulate 16550: bit 8 == 1 */
    352 	if ((dips & 0x80) == 0) {
    353 		printf(" slave");
    354 		/* XXX Does slave really mean no 16550 support?? */
    355 		return (0);
    356 	}
    357 
    358 	/*
    359 	 * If we made it this far, we are a full-featured ESP v2.0 (or
    360 	 * better), at the correct com port address.
    361 	 */
    362 
    363 	SET(sc->sc_hwflags, COM_HW_HAYESP);
    364 	printf(", 1024 byte fifo\n");
    365 	return (1);
    366 }
    367 #endif
    368 
    369 #if defined(DDB) || defined(KGDB)
    370 static void
    371 com_enable_debugport(sc)
    372 	struct com_softc *sc;
    373 {
    374 	int s;
    375 
    376 	/* Turn on line break interrupt, set carrier. */
    377 	s = splserial();
    378 	sc->sc_ier = IER_ERXRDY;
    379 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    380 	SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
    381 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
    382 	splx(s);
    383 }
    384 #endif
    385 
    386 void
    387 com_attach_subr(sc)
    388 	struct com_softc *sc;
    389 {
    390 	int iobase = sc->sc_iobase;
    391 	bus_space_tag_t iot = sc->sc_iot;
    392 	bus_space_handle_t ioh = sc->sc_ioh;
    393 	struct tty *tp;
    394 #ifdef COM16650
    395 	u_int8_t lcr;
    396 #endif
    397 #ifdef COM_HAYESP
    398 	int	hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
    399 	int	*hayespp;
    400 #endif
    401 
    402 	callout_init(&sc->sc_diag_callout);
    403 
    404 	/* Disable interrupts before configuring the device. */
    405 	sc->sc_ier = 0;
    406 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
    407 
    408 	if (iot == comconstag && iobase == comconsaddr) {
    409 		comconsattached = 1;
    410 
    411 		/* Make sure the console is always "hardwired". */
    412 		delay(1000);			/* wait for output to finish */
    413 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    414 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    415 	}
    416 
    417 #ifdef COM_HAYESP
    418 	/* Look for a Hayes ESP board. */
    419 	for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
    420 		bus_space_handle_t hayespioh;
    421 
    422 #define	HAYESP_NPORTS	8			/* XXX XXX XXX ??? ??? ??? */
    423 		if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
    424 			continue;
    425 		if (comprobeHAYESP(hayespioh, sc)) {
    426 			sc->sc_hayespioh = hayespioh;
    427 			sc->sc_fifolen = 1024;
    428 
    429 			break;
    430 		}
    431 		bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
    432 	}
    433 	/* No ESP; look for other things. */
    434 	if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
    435 #endif
    436 	sc->sc_fifolen = 1;
    437 	/* look for a NS 16550AF UART with FIFOs */
    438 	bus_space_write_1(iot, ioh, com_fifo,
    439 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    440 	delay(100);
    441 	if (ISSET(bus_space_read_1(iot, ioh, com_iir), IIR_FIFO_MASK)
    442 	    == IIR_FIFO_MASK)
    443 		if (ISSET(bus_space_read_1(iot, ioh, com_fifo), FIFO_TRIGGER_14)
    444 		    == FIFO_TRIGGER_14) {
    445 			SET(sc->sc_hwflags, COM_HW_FIFO);
    446 
    447 #ifdef COM16650
    448 			/*
    449 			 * IIR changes into the EFR if LCR is set to LCR_EERS
    450 			 * on 16650s. We also know IIR != 0 at this point.
    451 			 * Write 0 into the EFR, and read it. If the result
    452 			 * is 0, we have a 16650.
    453 			 *
    454 			 * Older 16650s were broken; the test to detect them
    455 			 * is taken from the Linux driver. Apparently
    456 			 * setting DLAB enable gives access to the EFR on
    457 			 * these chips.
    458 			 */
    459 			lcr = bus_space_read_1(iot, ioh, com_lcr);
    460 			bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
    461 			bus_space_write_1(iot, ioh, com_efr, 0);
    462 			if (bus_space_read_1(iot, ioh, com_efr) == 0) {
    463 				bus_space_write_1(iot, ioh, com_lcr,
    464 				    lcr | LCR_DLAB);
    465 				if (bus_space_read_1(iot, ioh, com_efr) == 0) {
    466 					CLR(sc->sc_hwflags, COM_HW_FIFO);
    467 					sc->sc_fifolen = 0;
    468 				} else {
    469 					SET(sc->sc_hwflags, COM_HW_FLOW);
    470 					sc->sc_fifolen = 32;
    471 				}
    472 			} else
    473 #endif
    474 				sc->sc_fifolen = 16;
    475 
    476 #ifdef COM16650
    477 			bus_space_write_1(iot, ioh, com_lcr, lcr);
    478 			if (sc->sc_fifolen == 0)
    479 				printf(": st16650, broken fifo\n");
    480 			else if (sc->sc_fifolen == 32)
    481 				printf(": st16650a, working fifo\n");
    482 			else
    483 #endif
    484 				printf(": ns16550a, working fifo\n");
    485 		} else
    486 			printf(": ns16550, broken fifo\n");
    487 	else
    488 		printf(": ns8250 or ns16450, no fifo\n");
    489 	bus_space_write_1(iot, ioh, com_fifo, 0);
    490 	if (ISSET(sc->sc_hwflags, COM_HW_TXFIFO_DISABLE)) {
    491 		sc->sc_fifolen = 1;
    492 		printf("%s: txfifo disabled\n", sc->sc_dev.dv_xname);
    493 	}
    494 #ifdef COM_HAYESP
    495 	}
    496 #endif
    497 
    498 	tp = ttymalloc();
    499 	tp->t_oproc = comstart;
    500 	tp->t_param = comparam;
    501 	tp->t_hwiflow = comhwiflow;
    502 
    503 	sc->sc_tty = tp;
    504 	sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    505 	if (sc->sc_rbuf == NULL) {
    506 		printf("%s: unable to allocate ring buffer\n",
    507 		    sc->sc_dev.dv_xname);
    508 		return;
    509 	}
    510 	sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
    511 
    512 	tty_attach(tp);
    513 
    514 	if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
    515 		SET(sc->sc_mcr, MCR_IENABLE);
    516 
    517 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    518 		int maj;
    519 
    520 		/* locate the major number */
    521 		for (maj = 0; maj < nchrdev; maj++)
    522 			if (cdevsw[maj].d_open == comopen)
    523 				break;
    524 
    525 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    526 
    527 		printf("%s: console\n", sc->sc_dev.dv_xname);
    528 	}
    529 
    530 #ifdef KGDB
    531 	/*
    532 	 * Allow kgdb to "take over" this port.  If this is
    533 	 * the kgdb device, it has exclusive use.
    534 	 */
    535 	if (iot == com_kgdb_iot && iobase == com_kgdb_addr) {
    536 		com_kgdb_attached = 1;
    537 
    538 		SET(sc->sc_hwflags, COM_HW_KGDB);
    539 		printf("%s: kgdb\n", sc->sc_dev.dv_xname);
    540 	}
    541 #endif
    542 
    543 #ifdef __GENERIC_SOFT_INTERRUPTS
    544 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, comsoft, sc);
    545 #endif
    546 
    547 #if NRND > 0 && defined(RND_COM)
    548 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    549 			  RND_TYPE_TTY, 0);
    550 #endif
    551 
    552 	/* if there are no enable/disable functions, assume the device
    553 	   is always enabled */
    554 	if (!sc->enable)
    555 		sc->enabled = 1;
    556 
    557 	com_config(sc);
    558 
    559 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    560 }
    561 
    562 void
    563 com_config(sc)
    564 	struct com_softc *sc;
    565 {
    566 	bus_space_tag_t iot = sc->sc_iot;
    567 	bus_space_handle_t ioh = sc->sc_ioh;
    568 
    569 	/* Disable interrupts before configuring the device. */
    570 	sc->sc_ier = 0;
    571 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
    572 
    573 #ifdef COM_HAYESP
    574 	/* Look for a Hayes ESP board. */
    575 	if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
    576 		sc->sc_fifolen = 1024;
    577 
    578 		/* Set 16550 compatibility mode */
    579 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
    580 				  HAYESP_SETMODE);
    581 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    582 				  HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
    583 				  HAYESP_MODE_SCALE);
    584 
    585 		/* Set RTS/CTS flow control */
    586 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
    587 				  HAYESP_SETFLOWTYPE);
    588 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    589 				  HAYESP_FLOW_RTS);
    590 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    591 				  HAYESP_FLOW_CTS);
    592 
    593 		/* Set flow control levels */
    594 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
    595 				  HAYESP_SETRXFLOW);
    596 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    597 				  HAYESP_HIBYTE(HAYESP_RXHIWMARK));
    598 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    599 				  HAYESP_LOBYTE(HAYESP_RXHIWMARK));
    600 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    601 				  HAYESP_HIBYTE(HAYESP_RXLOWMARK));
    602 		bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
    603 				  HAYESP_LOBYTE(HAYESP_RXLOWMARK));
    604 	}
    605 #endif
    606 
    607 #ifdef DDB
    608 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    609 		com_enable_debugport(sc);
    610 #endif
    611 
    612 #ifdef KGDB
    613 	/*
    614 	 * Allow kgdb to "take over" this port.  If this is
    615 	 * the kgdb device, it has exclusive use.
    616 	 */
    617 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    618 		com_enable_debugport(sc);
    619 #endif
    620 }
    621 
    622 int
    623 com_detach(self, flags)
    624 	struct device *self;
    625 	int flags;
    626 {
    627 	struct com_softc *sc = (struct com_softc *)self;
    628 	int maj, mn;
    629 
    630 	/* locate the major number */
    631 	for (maj = 0; maj < nchrdev; maj++)
    632 		if (cdevsw[maj].d_open == comopen)
    633 			break;
    634 
    635 	/* Nuke the vnodes for any open instances. */
    636 	mn = self->dv_unit;
    637 	vdevgone(maj, mn, mn, VCHR);
    638 
    639 	mn |= COMDIALOUT_MASK;
    640 	vdevgone(maj, mn, mn, VCHR);
    641 
    642 	/* Free the receive buffer. */
    643 	free(sc->sc_rbuf, M_DEVBUF);
    644 
    645 	/* Detach and free the tty. */
    646 	tty_detach(sc->sc_tty);
    647 	ttyfree(sc->sc_tty);
    648 
    649 #ifdef __GENERIC_SOFT_INTERRUPTS
    650 	/* Unhook the soft interrupt handler. */
    651 	softintr_disestablish(sc->sc_si);
    652 #endif
    653 
    654 #if NRND > 0 && defined(RND_COM)
    655 	/* Unhook the entropy source. */
    656 	rnd_detach_source(&sc->rnd_source);
    657 #endif
    658 
    659 	return (0);
    660 }
    661 
    662 int
    663 com_activate(self, act)
    664 	struct device *self;
    665 	enum devact act;
    666 {
    667 	struct com_softc *sc = (struct com_softc *)self;
    668 	int s, rv = 0;
    669 
    670 	s = splserial();
    671 	switch (act) {
    672 	case DVACT_ACTIVATE:
    673 		rv = EOPNOTSUPP;
    674 		break;
    675 
    676 	case DVACT_DEACTIVATE:
    677 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
    678 			rv = EBUSY;
    679 			break;
    680 		}
    681 
    682 		if (sc->disable != NULL && sc->enabled != 0) {
    683 			(*sc->disable)(sc);
    684 			sc->enabled = 0;
    685 		}
    686 		break;
    687 	}
    688 	splx(s);
    689 	return (rv);
    690 }
    691 
    692 void
    693 com_shutdown(sc)
    694 	struct com_softc *sc;
    695 {
    696 	struct tty *tp = sc->sc_tty;
    697 	int s;
    698 
    699 	s = splserial();
    700 
    701 	/* If we were asserting flow control, then deassert it. */
    702 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    703 	com_hwiflow(sc);
    704 
    705 	/* Clear any break condition set with TIOCSBRK. */
    706 	com_break(sc, 0);
    707 
    708 	/* Turn off PPS capture on last close. */
    709 	sc->sc_ppsmask = 0;
    710 	sc->ppsparam.mode = 0;
    711 
    712 	/*
    713 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    714 	 * notice even if we immediately open the port again.
    715 	 */
    716 	if (ISSET(tp->t_cflag, HUPCL)) {
    717 		com_modem(sc, 0);
    718 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    719 	}
    720 
    721 	/* Turn off interrupts. */
    722 #ifdef DDB
    723 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    724 		sc->sc_ier = IER_ERXRDY; /* interrupt on break */
    725 	else
    726 #endif
    727 		sc->sc_ier = 0;
    728 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    729 
    730 	if (sc->disable) {
    731 #ifdef DIAGNOSTIC
    732 		if (!sc->enabled)
    733 			panic("com_shutdown: not enabled?");
    734 #endif
    735 		(*sc->disable)(sc);
    736 		sc->enabled = 0;
    737 	}
    738 
    739 	splx(s);
    740 }
    741 
    742 int
    743 comopen(dev, flag, mode, p)
    744 	dev_t dev;
    745 	int flag, mode;
    746 	struct proc *p;
    747 {
    748 	int unit = COMUNIT(dev);
    749 	struct com_softc *sc;
    750 	struct tty *tp;
    751 	int s, s2;
    752 	int error;
    753 
    754 	if (unit >= com_cd.cd_ndevs)
    755 		return (ENXIO);
    756 	sc = com_cd.cd_devs[unit];
    757 	if (sc == 0 || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    758 	    sc->sc_rbuf == NULL)
    759 		return (ENXIO);
    760 
    761 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    762 		return (ENXIO);
    763 
    764 #ifdef KGDB
    765 	/*
    766 	 * If this is the kgdb port, no other use is permitted.
    767 	 */
    768 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    769 		return (EBUSY);
    770 #endif
    771 
    772 	tp = sc->sc_tty;
    773 
    774 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    775 	    ISSET(tp->t_state, TS_XCLUDE) &&
    776 	    p->p_ucred->cr_uid != 0)
    777 		return (EBUSY);
    778 
    779 	s = spltty();
    780 
    781 	/*
    782 	 * Do the following iff this is a first open.
    783 	 */
    784 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    785 		struct termios t;
    786 
    787 		tp->t_dev = dev;
    788 
    789 		s2 = splserial();
    790 
    791 		if (sc->enable) {
    792 			if ((*sc->enable)(sc)) {
    793 				splx(s2);
    794 				splx(s);
    795 				printf("%s: device enable failed\n",
    796 				       sc->sc_dev.dv_xname);
    797 				return (EIO);
    798 			}
    799 			sc->enabled = 1;
    800 			com_config(sc);
    801 		}
    802 
    803 		/* Turn on interrupts. */
    804 		sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
    805 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
    806 
    807 		/* Fetch the current modem control status, needed later. */
    808 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
    809 
    810 		/* Clear PPS capture state on first open. */
    811 		sc->sc_ppsmask = 0;
    812 		sc->ppsparam.mode = 0;
    813 
    814 		splx(s2);
    815 
    816 		/*
    817 		 * Initialize the termios status to the defaults.  Add in the
    818 		 * sticky bits from TIOCSFLAGS.
    819 		 */
    820 		t.c_ispeed = 0;
    821 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    822 			t.c_ospeed = comconsrate;
    823 			t.c_cflag = comconscflag;
    824 		} else {
    825 			t.c_ospeed = TTYDEF_SPEED;
    826 			t.c_cflag = TTYDEF_CFLAG;
    827 		}
    828 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    829 			SET(t.c_cflag, CLOCAL);
    830 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    831 			SET(t.c_cflag, CRTSCTS);
    832 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    833 			SET(t.c_cflag, MDMBUF);
    834 		/* Make sure comparam() will do something. */
    835 		tp->t_ospeed = 0;
    836 		(void) comparam(tp, &t);
    837 		tp->t_iflag = TTYDEF_IFLAG;
    838 		tp->t_oflag = TTYDEF_OFLAG;
    839 		tp->t_lflag = TTYDEF_LFLAG;
    840 		ttychars(tp);
    841 		ttsetwater(tp);
    842 
    843 		s2 = splserial();
    844 
    845 		/*
    846 		 * Turn on DTR.  We must always do this, even if carrier is not
    847 		 * present, because otherwise we'd have to use TIOCSDTR
    848 		 * immediately after setting CLOCAL, which applications do not
    849 		 * expect.  We always assert DTR while the device is open
    850 		 * unless explicitly requested to deassert it.
    851 		 */
    852 		com_modem(sc, 1);
    853 
    854 		/* Clear the input ring, and unblock. */
    855 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    856 		sc->sc_rbavail = com_rbuf_size;
    857 		com_iflush(sc);
    858 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    859 		com_hwiflow(sc);
    860 
    861 #ifdef COM_DEBUG
    862 		if (com_debug)
    863 			comstatus(sc, "comopen  ");
    864 #endif
    865 
    866 		splx(s2);
    867 	}
    868 
    869 	splx(s);
    870 
    871 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    872 	if (error)
    873 		goto bad;
    874 
    875 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    876 	if (error)
    877 		goto bad;
    878 
    879 	return (0);
    880 
    881 bad:
    882 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    883 		/*
    884 		 * We failed to open the device, and nobody else had it opened.
    885 		 * Clean up the state as appropriate.
    886 		 */
    887 		com_shutdown(sc);
    888 	}
    889 
    890 	return (error);
    891 }
    892 
    893 int
    894 comclose(dev, flag, mode, p)
    895 	dev_t dev;
    896 	int flag, mode;
    897 	struct proc *p;
    898 {
    899 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    900 	struct tty *tp = sc->sc_tty;
    901 
    902 	/* XXX This is for cons.c. */
    903 	if (!ISSET(tp->t_state, TS_ISOPEN))
    904 		return (0);
    905 
    906 	(*linesw[tp->t_line].l_close)(tp, flag);
    907 	ttyclose(tp);
    908 
    909 	if (COM_ISALIVE(sc) == 0)
    910 		return (0);
    911 
    912 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    913 		/*
    914 		 * Although we got a last close, the device may still be in
    915 		 * use; e.g. if this was the dialout node, and there are still
    916 		 * processes waiting for carrier on the non-dialout node.
    917 		 */
    918 		com_shutdown(sc);
    919 	}
    920 
    921 	return (0);
    922 }
    923 
    924 int
    925 comread(dev, uio, flag)
    926 	dev_t dev;
    927 	struct uio *uio;
    928 	int flag;
    929 {
    930 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    931 	struct tty *tp = sc->sc_tty;
    932 
    933 	if (COM_ISALIVE(sc) == 0)
    934 		return (EIO);
    935 
    936 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    937 }
    938 
    939 int
    940 comwrite(dev, uio, flag)
    941 	dev_t dev;
    942 	struct uio *uio;
    943 	int flag;
    944 {
    945 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    946 	struct tty *tp = sc->sc_tty;
    947 
    948 	if (COM_ISALIVE(sc) == 0)
    949 		return (EIO);
    950 
    951 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    952 }
    953 
    954 struct tty *
    955 comtty(dev)
    956 	dev_t dev;
    957 {
    958 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    959 	struct tty *tp = sc->sc_tty;
    960 
    961 	return (tp);
    962 }
    963 
    964 int
    965 comioctl(dev, cmd, data, flag, p)
    966 	dev_t dev;
    967 	u_long cmd;
    968 	caddr_t data;
    969 	int flag;
    970 	struct proc *p;
    971 {
    972 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    973 	struct tty *tp = sc->sc_tty;
    974 	int error;
    975 	int s;
    976 
    977 	if (COM_ISALIVE(sc) == 0)
    978 		return (EIO);
    979 
    980 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    981 	if (error >= 0)
    982 		return (error);
    983 
    984 	error = ttioctl(tp, cmd, data, flag, p);
    985 	if (error >= 0)
    986 		return (error);
    987 
    988 	error = 0;
    989 
    990 	s = splserial();
    991 
    992 	switch (cmd) {
    993 	case TIOCSBRK:
    994 		com_break(sc, 1);
    995 		break;
    996 
    997 	case TIOCCBRK:
    998 		com_break(sc, 0);
    999 		break;
   1000 
   1001 	case TIOCSDTR:
   1002 		com_modem(sc, 1);
   1003 		break;
   1004 
   1005 	case TIOCCDTR:
   1006 		com_modem(sc, 0);
   1007 		break;
   1008 
   1009 	case TIOCGFLAGS:
   1010 		*(int *)data = sc->sc_swflags;
   1011 		break;
   1012 
   1013 	case TIOCSFLAGS:
   1014 		error = suser(p->p_ucred, &p->p_acflag);
   1015 		if (error)
   1016 			break;
   1017 		sc->sc_swflags = *(int *)data;
   1018 		break;
   1019 
   1020 	case TIOCMSET:
   1021 	case TIOCMBIS:
   1022 	case TIOCMBIC:
   1023 		tiocm_to_com(sc, cmd, *(int *)data);
   1024 		break;
   1025 
   1026 	case TIOCMGET:
   1027 		*(int *)data = com_to_tiocm(sc);
   1028 		break;
   1029 
   1030 	case PPS_IOC_CREATE:
   1031 		break;
   1032 
   1033 	case PPS_IOC_DESTROY:
   1034 		break;
   1035 
   1036 	case PPS_IOC_GETPARAMS: {
   1037 		pps_params_t *pp;
   1038 		pp = (pps_params_t *)data;
   1039 		*pp = sc->ppsparam;
   1040 		break;
   1041 	}
   1042 
   1043 	case PPS_IOC_SETPARAMS: {
   1044 	  	pps_params_t *pp;
   1045 		int mode;
   1046 		pp = (pps_params_t *)data;
   1047 		if (pp->mode & ~ppscap) {
   1048 			error = EINVAL;
   1049 			break;
   1050 		}
   1051 		sc->ppsparam = *pp;
   1052 	 	/*
   1053 		 * Compute msr masks from user-specified timestamp state.
   1054 		 */
   1055 		mode = sc->ppsparam.mode;
   1056 #ifdef	PPS_SYNC
   1057 		if (mode & PPS_HARDPPSONASSERT) {
   1058 			mode |= PPS_CAPTUREASSERT;
   1059 			/* XXX revoke any previous HARDPPS source */
   1060 		}
   1061 		if (mode & PPS_HARDPPSONCLEAR) {
   1062 			mode |= PPS_CAPTURECLEAR;
   1063 			/* XXX revoke any previous HARDPPS source */
   1064 		}
   1065 #endif	/* PPS_SYNC */
   1066 		switch (mode & PPS_CAPTUREBOTH) {
   1067 		case 0:
   1068 			sc->sc_ppsmask = 0;
   1069 			break;
   1070 
   1071 		case PPS_CAPTUREASSERT:
   1072 			sc->sc_ppsmask = MSR_DCD;
   1073 			sc->sc_ppsassert = MSR_DCD;
   1074 			sc->sc_ppsclear = -1;
   1075 			break;
   1076 
   1077 		case PPS_CAPTURECLEAR:
   1078 			sc->sc_ppsmask = MSR_DCD;
   1079 			sc->sc_ppsassert = -1;
   1080 			sc->sc_ppsclear = 0;
   1081 			break;
   1082 
   1083 		case PPS_CAPTUREBOTH:
   1084 			sc->sc_ppsmask = MSR_DCD;
   1085 			sc->sc_ppsassert = MSR_DCD;
   1086 			sc->sc_ppsclear = 0;
   1087 			break;
   1088 
   1089 		default:
   1090 			error = EINVAL;
   1091 			break;
   1092 		}
   1093 		break;
   1094 	}
   1095 
   1096 	case PPS_IOC_GETCAP:
   1097 		*(int*)data = ppscap;
   1098 		break;
   1099 
   1100 	case PPS_IOC_FETCH: {
   1101 		pps_info_t *pi;
   1102 		pi = (pps_info_t *)data;
   1103 		*pi = sc->ppsinfo;
   1104 		break;
   1105 	}
   1106 
   1107 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
   1108 		/*
   1109 		 * Some GPS clocks models use the falling rather than
   1110 		 * rising edge as the on-the-second signal.
   1111 		 * The old API has no way to specify PPS polarity.
   1112 		 */
   1113 		sc->sc_ppsmask = MSR_DCD;
   1114 #ifndef PPS_TRAILING_EDGE
   1115 		sc->sc_ppsassert = MSR_DCD;
   1116 		sc->sc_ppsclear = -1;
   1117 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1118 		    &sc->ppsinfo.assert_timestamp);
   1119 #else
   1120 		sc->sc_ppsassert = -1
   1121 		sc->sc_ppsclear = 0;
   1122 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1123 		    &sc->ppsinfo.clear_timestamp);
   1124 #endif
   1125 		break;
   1126 
   1127 	default:
   1128 		error = ENOTTY;
   1129 		break;
   1130 	}
   1131 
   1132 	splx(s);
   1133 
   1134 #ifdef COM_DEBUG
   1135 	if (com_debug)
   1136 		comstatus(sc, "comioctl ");
   1137 #endif
   1138 
   1139 	return (error);
   1140 }
   1141 
   1142 integrate void
   1143 com_schedrx(sc)
   1144 	struct com_softc *sc;
   1145 {
   1146 
   1147 	sc->sc_rx_ready = 1;
   1148 
   1149 	/* Wake up the poller. */
   1150 #ifdef __GENERIC_SOFT_INTERRUPTS
   1151 	softintr_schedule(sc->sc_si);
   1152 #else
   1153 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1154 	setsoftserial();
   1155 #else
   1156 	if (!com_softintr_scheduled) {
   1157 		com_softintr_scheduled = 1;
   1158 		callout_reset(&comsoft_callout, 1, comsoft, NULL);
   1159 	}
   1160 #endif
   1161 #endif
   1162 }
   1163 
   1164 void
   1165 com_break(sc, onoff)
   1166 	struct com_softc *sc;
   1167 	int onoff;
   1168 {
   1169 
   1170 	if (onoff)
   1171 		SET(sc->sc_lcr, LCR_SBREAK);
   1172 	else
   1173 		CLR(sc->sc_lcr, LCR_SBREAK);
   1174 
   1175 	if (!sc->sc_heldchange) {
   1176 		if (sc->sc_tx_busy) {
   1177 			sc->sc_heldtbc = sc->sc_tbc;
   1178 			sc->sc_tbc = 0;
   1179 			sc->sc_heldchange = 1;
   1180 		} else
   1181 			com_loadchannelregs(sc);
   1182 	}
   1183 }
   1184 
   1185 void
   1186 com_modem(sc, onoff)
   1187 	struct com_softc *sc;
   1188 	int onoff;
   1189 {
   1190 
   1191 	if (sc->sc_mcr_dtr == 0)
   1192 		return;
   1193 
   1194 	if (onoff)
   1195 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1196 	else
   1197 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1198 
   1199 	if (!sc->sc_heldchange) {
   1200 		if (sc->sc_tx_busy) {
   1201 			sc->sc_heldtbc = sc->sc_tbc;
   1202 			sc->sc_tbc = 0;
   1203 			sc->sc_heldchange = 1;
   1204 		} else
   1205 			com_loadchannelregs(sc);
   1206 	}
   1207 }
   1208 
   1209 void
   1210 tiocm_to_com(sc, how, ttybits)
   1211 	struct com_softc *sc;
   1212 	int how, ttybits;
   1213 {
   1214 	u_char combits;
   1215 
   1216 	combits = 0;
   1217 	if (ISSET(ttybits, TIOCM_DTR))
   1218 		SET(combits, MCR_DTR);
   1219 	if (ISSET(ttybits, TIOCM_RTS))
   1220 		SET(combits, MCR_RTS);
   1221 
   1222 	switch (how) {
   1223 	case TIOCMBIC:
   1224 		CLR(sc->sc_mcr, combits);
   1225 		break;
   1226 
   1227 	case TIOCMBIS:
   1228 		SET(sc->sc_mcr, combits);
   1229 		break;
   1230 
   1231 	case TIOCMSET:
   1232 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
   1233 		SET(sc->sc_mcr, combits);
   1234 		break;
   1235 	}
   1236 
   1237 	if (!sc->sc_heldchange) {
   1238 		if (sc->sc_tx_busy) {
   1239 			sc->sc_heldtbc = sc->sc_tbc;
   1240 			sc->sc_tbc = 0;
   1241 			sc->sc_heldchange = 1;
   1242 		} else
   1243 			com_loadchannelregs(sc);
   1244 	}
   1245 }
   1246 
   1247 int
   1248 com_to_tiocm(sc)
   1249 	struct com_softc *sc;
   1250 {
   1251 	u_char combits;
   1252 	int ttybits = 0;
   1253 
   1254 	combits = sc->sc_mcr;
   1255 	if (ISSET(combits, MCR_DTR))
   1256 		SET(ttybits, TIOCM_DTR);
   1257 	if (ISSET(combits, MCR_RTS))
   1258 		SET(ttybits, TIOCM_RTS);
   1259 
   1260 	combits = sc->sc_msr;
   1261 	if (ISSET(combits, MSR_DCD))
   1262 		SET(ttybits, TIOCM_CD);
   1263 	if (ISSET(combits, MSR_CTS))
   1264 		SET(ttybits, TIOCM_CTS);
   1265 	if (ISSET(combits, MSR_DSR))
   1266 		SET(ttybits, TIOCM_DSR);
   1267 	if (ISSET(combits, MSR_RI | MSR_TERI))
   1268 		SET(ttybits, TIOCM_RI);
   1269 
   1270 	if (sc->sc_ier != 0)
   1271 		SET(ttybits, TIOCM_LE);
   1272 
   1273 	return (ttybits);
   1274 }
   1275 
   1276 static u_char
   1277 cflag2lcr(cflag)
   1278 	tcflag_t cflag;
   1279 {
   1280 	u_char lcr = 0;
   1281 
   1282 	switch (ISSET(cflag, CSIZE)) {
   1283 	case CS5:
   1284 		SET(lcr, LCR_5BITS);
   1285 		break;
   1286 	case CS6:
   1287 		SET(lcr, LCR_6BITS);
   1288 		break;
   1289 	case CS7:
   1290 		SET(lcr, LCR_7BITS);
   1291 		break;
   1292 	case CS8:
   1293 		SET(lcr, LCR_8BITS);
   1294 		break;
   1295 	}
   1296 	if (ISSET(cflag, PARENB)) {
   1297 		SET(lcr, LCR_PENAB);
   1298 		if (!ISSET(cflag, PARODD))
   1299 			SET(lcr, LCR_PEVEN);
   1300 	}
   1301 	if (ISSET(cflag, CSTOPB))
   1302 		SET(lcr, LCR_STOPB);
   1303 
   1304 	return (lcr);
   1305 }
   1306 
   1307 int
   1308 comparam(tp, t)
   1309 	struct tty *tp;
   1310 	struct termios *t;
   1311 {
   1312 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1313 	int ospeed = comspeed(t->c_ospeed, sc->sc_frequency);
   1314 	u_char lcr;
   1315 	int s;
   1316 
   1317 	if (COM_ISALIVE(sc) == 0)
   1318 		return (EIO);
   1319 
   1320 	/* Check requested parameters. */
   1321 	if (ospeed < 0)
   1322 		return (EINVAL);
   1323 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1324 		return (EINVAL);
   1325 
   1326 	/*
   1327 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1328 	 * is always active.
   1329 	 */
   1330 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1331 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1332 		SET(t->c_cflag, CLOCAL);
   1333 		CLR(t->c_cflag, HUPCL);
   1334 	}
   1335 
   1336 	/*
   1337 	 * If there were no changes, don't do anything.  This avoids dropping
   1338 	 * input and improves performance when all we did was frob things like
   1339 	 * VMIN and VTIME.
   1340 	 */
   1341 	if (tp->t_ospeed == t->c_ospeed &&
   1342 	    tp->t_cflag == t->c_cflag)
   1343 		return (0);
   1344 
   1345 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
   1346 
   1347 	s = splserial();
   1348 
   1349 	sc->sc_lcr = lcr;
   1350 
   1351 	/*
   1352 	 * If we're not in a mode that assumes a connection is present, then
   1353 	 * ignore carrier changes.
   1354 	 */
   1355 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1356 		sc->sc_msr_dcd = 0;
   1357 	else
   1358 		sc->sc_msr_dcd = MSR_DCD;
   1359 	/*
   1360 	 * Set the flow control pins depending on the current flow control
   1361 	 * mode.
   1362 	 */
   1363 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1364 		sc->sc_mcr_dtr = MCR_DTR;
   1365 		sc->sc_mcr_rts = MCR_RTS;
   1366 		sc->sc_msr_cts = MSR_CTS;
   1367 		sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
   1368 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1369 		/*
   1370 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1371 		 * carrier detection.
   1372 		 */
   1373 		sc->sc_mcr_dtr = 0;
   1374 		sc->sc_mcr_rts = MCR_DTR;
   1375 		sc->sc_msr_cts = MSR_DCD;
   1376 		sc->sc_efr = 0;
   1377 	} else {
   1378 		/*
   1379 		 * If no flow control, then always set RTS.  This will make
   1380 		 * the other side happy if it mistakenly thinks we're doing
   1381 		 * RTS/CTS flow control.
   1382 		 */
   1383 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
   1384 		sc->sc_mcr_rts = 0;
   1385 		sc->sc_msr_cts = 0;
   1386 		sc->sc_efr = 0;
   1387 		if (ISSET(sc->sc_mcr, MCR_DTR))
   1388 			SET(sc->sc_mcr, MCR_RTS);
   1389 		else
   1390 			CLR(sc->sc_mcr, MCR_RTS);
   1391 	}
   1392 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1393 
   1394 #if 0
   1395 	if (ospeed == 0)
   1396 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1397 	else
   1398 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1399 #endif
   1400 
   1401 	sc->sc_dlbl = ospeed;
   1402 	sc->sc_dlbh = ospeed >> 8;
   1403 
   1404 	/*
   1405 	 * Set the FIFO threshold based on the receive speed.
   1406 	 *
   1407 	 *  * If it's a low speed, it's probably a mouse or some other
   1408 	 *    interactive device, so set the threshold low.
   1409 	 *  * If it's a high speed, trim the trigger level down to prevent
   1410 	 *    overflows.
   1411 	 *  * Otherwise set it a bit higher.
   1412 	 */
   1413 	if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
   1414 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
   1415 	else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
   1416 		sc->sc_fifo = FIFO_ENABLE |
   1417 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
   1418 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
   1419 	else
   1420 		sc->sc_fifo = 0;
   1421 
   1422 	/* And copy to tty. */
   1423 	tp->t_ispeed = 0;
   1424 	tp->t_ospeed = t->c_ospeed;
   1425 	tp->t_cflag = t->c_cflag;
   1426 
   1427 	if (!sc->sc_heldchange) {
   1428 		if (sc->sc_tx_busy) {
   1429 			sc->sc_heldtbc = sc->sc_tbc;
   1430 			sc->sc_tbc = 0;
   1431 			sc->sc_heldchange = 1;
   1432 		} else
   1433 			com_loadchannelregs(sc);
   1434 	}
   1435 
   1436 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1437 		/* Disable the high water mark. */
   1438 		sc->sc_r_hiwat = 0;
   1439 		sc->sc_r_lowat = 0;
   1440 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1441 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1442 			com_schedrx(sc);
   1443 		}
   1444 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1445 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1446 			com_hwiflow(sc);
   1447 		}
   1448 	} else {
   1449 		sc->sc_r_hiwat = com_rbuf_hiwat;
   1450 		sc->sc_r_lowat = com_rbuf_lowat;
   1451 	}
   1452 
   1453 	splx(s);
   1454 
   1455 	/*
   1456 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1457 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1458 	 * explicit request.
   1459 	 */
   1460 	(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
   1461 
   1462 #ifdef COM_DEBUG
   1463 	if (com_debug)
   1464 		comstatus(sc, "comparam ");
   1465 #endif
   1466 
   1467 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1468 		if (sc->sc_tx_stopped) {
   1469 			sc->sc_tx_stopped = 0;
   1470 			comstart(tp);
   1471 		}
   1472 	}
   1473 
   1474 	return (0);
   1475 }
   1476 
   1477 void
   1478 com_iflush(sc)
   1479 	struct com_softc *sc;
   1480 {
   1481 	bus_space_tag_t iot = sc->sc_iot;
   1482 	bus_space_handle_t ioh = sc->sc_ioh;
   1483 #ifdef DIAGNOSTIC
   1484 	int reg;
   1485 #endif
   1486 	int timo;
   1487 
   1488 #ifdef DIAGNOSTIC
   1489 	reg = 0xffff;
   1490 #endif
   1491 	timo = 50000;
   1492 	/* flush any pending I/O */
   1493 	while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
   1494 	    && --timo)
   1495 #ifdef DIAGNOSTIC
   1496 		reg =
   1497 #else
   1498 		    (void)
   1499 #endif
   1500 		    bus_space_read_1(iot, ioh, com_data);
   1501 #ifdef DIAGNOSTIC
   1502 	if (!timo)
   1503 		printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1504 		       reg);
   1505 #endif
   1506 }
   1507 
   1508 void
   1509 com_loadchannelregs(sc)
   1510 	struct com_softc *sc;
   1511 {
   1512 	bus_space_tag_t iot = sc->sc_iot;
   1513 	bus_space_handle_t ioh = sc->sc_ioh;
   1514 
   1515 	/* XXXXX necessary? */
   1516 	com_iflush(sc);
   1517 
   1518 	bus_space_write_1(iot, ioh, com_ier, 0);
   1519 
   1520 	if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
   1521 		bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1522 		bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
   1523 	}
   1524 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
   1525 	bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
   1526 	bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
   1527 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
   1528 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
   1529 	bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
   1530 
   1531 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1532 }
   1533 
   1534 int
   1535 comhwiflow(tp, block)
   1536 	struct tty *tp;
   1537 	int block;
   1538 {
   1539 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1540 	int s;
   1541 
   1542 	if (COM_ISALIVE(sc) == 0)
   1543 		return (0);
   1544 
   1545 	if (sc->sc_mcr_rts == 0)
   1546 		return (0);
   1547 
   1548 	s = splserial();
   1549 	if (block) {
   1550 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1551 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1552 			com_hwiflow(sc);
   1553 		}
   1554 	} else {
   1555 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1556 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1557 			com_schedrx(sc);
   1558 		}
   1559 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1560 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1561 			com_hwiflow(sc);
   1562 		}
   1563 	}
   1564 	splx(s);
   1565 	return (1);
   1566 }
   1567 
   1568 /*
   1569  * (un)block input via hw flowcontrol
   1570  */
   1571 void
   1572 com_hwiflow(sc)
   1573 	struct com_softc *sc;
   1574 {
   1575 	bus_space_tag_t iot = sc->sc_iot;
   1576 	bus_space_handle_t ioh = sc->sc_ioh;
   1577 
   1578 	if (sc->sc_mcr_rts == 0)
   1579 		return;
   1580 
   1581 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1582 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1583 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1584 	} else {
   1585 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1586 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1587 	}
   1588 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
   1589 }
   1590 
   1591 
   1592 void
   1593 comstart(tp)
   1594 	struct tty *tp;
   1595 {
   1596 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1597 	bus_space_tag_t iot = sc->sc_iot;
   1598 	bus_space_handle_t ioh = sc->sc_ioh;
   1599 	int s;
   1600 
   1601 	if (COM_ISALIVE(sc) == 0)
   1602 		return;
   1603 
   1604 	s = spltty();
   1605 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1606 		goto out;
   1607 	if (sc->sc_tx_stopped)
   1608 		goto out;
   1609 
   1610 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1611 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1612 			CLR(tp->t_state, TS_ASLEEP);
   1613 			wakeup(&tp->t_outq);
   1614 		}
   1615 		selwakeup(&tp->t_wsel);
   1616 		if (tp->t_outq.c_cc == 0)
   1617 			goto out;
   1618 	}
   1619 
   1620 	/* Grab the first contiguous region of buffer space. */
   1621 	{
   1622 		u_char *tba;
   1623 		int tbc;
   1624 
   1625 		tba = tp->t_outq.c_cf;
   1626 		tbc = ndqb(&tp->t_outq, 0);
   1627 
   1628 		(void)splserial();
   1629 
   1630 		sc->sc_tba = tba;
   1631 		sc->sc_tbc = tbc;
   1632 	}
   1633 
   1634 	SET(tp->t_state, TS_BUSY);
   1635 	sc->sc_tx_busy = 1;
   1636 
   1637 	/* Enable transmit completion interrupts if necessary. */
   1638 	if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
   1639 		SET(sc->sc_ier, IER_ETXRDY);
   1640 		bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1641 	}
   1642 
   1643 	/* Output the first chunk of the contiguous buffer. */
   1644 	{
   1645 		int n;
   1646 
   1647 		n = sc->sc_tbc;
   1648 		if (n > sc->sc_fifolen)
   1649 			n = sc->sc_fifolen;
   1650 		bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   1651 		sc->sc_tbc -= n;
   1652 		sc->sc_tba += n;
   1653 	}
   1654 out:
   1655 	splx(s);
   1656 	return;
   1657 }
   1658 
   1659 /*
   1660  * Stop output on a line.
   1661  */
   1662 void
   1663 comstop(tp, flag)
   1664 	struct tty *tp;
   1665 	int flag;
   1666 {
   1667 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1668 	int s;
   1669 
   1670 	s = splserial();
   1671 	if (ISSET(tp->t_state, TS_BUSY)) {
   1672 		/* Stop transmitting at the next chunk. */
   1673 		sc->sc_tbc = 0;
   1674 		sc->sc_heldtbc = 0;
   1675 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1676 			SET(tp->t_state, TS_FLUSH);
   1677 	}
   1678 	splx(s);
   1679 }
   1680 
   1681 void
   1682 comdiag(arg)
   1683 	void *arg;
   1684 {
   1685 	struct com_softc *sc = arg;
   1686 	int overflows, floods;
   1687 	int s;
   1688 
   1689 	s = splserial();
   1690 	overflows = sc->sc_overflows;
   1691 	sc->sc_overflows = 0;
   1692 	floods = sc->sc_floods;
   1693 	sc->sc_floods = 0;
   1694 	sc->sc_errors = 0;
   1695 	splx(s);
   1696 
   1697 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1698 	    sc->sc_dev.dv_xname,
   1699 	    overflows, overflows == 1 ? "" : "s",
   1700 	    floods, floods == 1 ? "" : "s");
   1701 }
   1702 
   1703 integrate void
   1704 com_rxsoft(sc, tp)
   1705 	struct com_softc *sc;
   1706 	struct tty *tp;
   1707 {
   1708 	int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
   1709 	u_char *get, *end;
   1710 	u_int cc, scc;
   1711 	u_char lsr;
   1712 	int code;
   1713 	int s;
   1714 
   1715 	end = sc->sc_ebuf;
   1716 	get = sc->sc_rbget;
   1717 	scc = cc = com_rbuf_size - sc->sc_rbavail;
   1718 
   1719 	if (cc == com_rbuf_size) {
   1720 		sc->sc_floods++;
   1721 		if (sc->sc_errors++ == 0)
   1722 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1723 			    comdiag, sc);
   1724 	}
   1725 
   1726 	while (cc) {
   1727 		code = get[0];
   1728 		lsr = get[1];
   1729 		if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
   1730 			if (ISSET(lsr, LSR_OE)) {
   1731 				sc->sc_overflows++;
   1732 				if (sc->sc_errors++ == 0)
   1733 					callout_reset(&sc->sc_diag_callout,
   1734 					    60 * hz, comdiag, sc);
   1735 			}
   1736 			if (ISSET(lsr, LSR_BI | LSR_FE))
   1737 				SET(code, TTY_FE);
   1738 			if (ISSET(lsr, LSR_PE))
   1739 				SET(code, TTY_PE);
   1740 		}
   1741 		if ((*rint)(code, tp) == -1) {
   1742 			/*
   1743 			 * The line discipline's buffer is out of space.
   1744 			 */
   1745 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1746 				/*
   1747 				 * We're either not using flow control, or the
   1748 				 * line discipline didn't tell us to block for
   1749 				 * some reason.  Either way, we have no way to
   1750 				 * know when there's more space available, so
   1751 				 * just drop the rest of the data.
   1752 				 */
   1753 				get += cc << 1;
   1754 				if (get >= end)
   1755 					get -= com_rbuf_size << 1;
   1756 				cc = 0;
   1757 			} else {
   1758 				/*
   1759 				 * Don't schedule any more receive processing
   1760 				 * until the line discipline tells us there's
   1761 				 * space available (through comhwiflow()).
   1762 				 * Leave the rest of the data in the input
   1763 				 * buffer.
   1764 				 */
   1765 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1766 			}
   1767 			break;
   1768 		}
   1769 		get += 2;
   1770 		if (get >= end)
   1771 			get = sc->sc_rbuf;
   1772 		cc--;
   1773 	}
   1774 
   1775 	if (cc != scc) {
   1776 		sc->sc_rbget = get;
   1777 		s = splserial();
   1778 		cc = sc->sc_rbavail += scc - cc;
   1779 		/* Buffers should be ok again, release possible block. */
   1780 		if (cc >= sc->sc_r_lowat) {
   1781 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1782 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1783 				SET(sc->sc_ier, IER_ERXRDY);
   1784 				bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
   1785 			}
   1786 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1787 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1788 				com_hwiflow(sc);
   1789 			}
   1790 		}
   1791 		splx(s);
   1792 	}
   1793 }
   1794 
   1795 integrate void
   1796 com_txsoft(sc, tp)
   1797 	struct com_softc *sc;
   1798 	struct tty *tp;
   1799 {
   1800 
   1801 	CLR(tp->t_state, TS_BUSY);
   1802 	if (ISSET(tp->t_state, TS_FLUSH))
   1803 		CLR(tp->t_state, TS_FLUSH);
   1804 	else
   1805 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1806 	(*linesw[tp->t_line].l_start)(tp);
   1807 }
   1808 
   1809 integrate void
   1810 com_stsoft(sc, tp)
   1811 	struct com_softc *sc;
   1812 	struct tty *tp;
   1813 {
   1814 	u_char msr, delta;
   1815 	int s;
   1816 
   1817 	s = splserial();
   1818 	msr = sc->sc_msr;
   1819 	delta = sc->sc_msr_delta;
   1820 	sc->sc_msr_delta = 0;
   1821 	splx(s);
   1822 
   1823 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1824 		/*
   1825 		 * Inform the tty layer that carrier detect changed.
   1826 		 */
   1827 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
   1828 	}
   1829 
   1830 	if (ISSET(delta, sc->sc_msr_cts)) {
   1831 		/* Block or unblock output according to flow control. */
   1832 		if (ISSET(msr, sc->sc_msr_cts)) {
   1833 			sc->sc_tx_stopped = 0;
   1834 			(*linesw[tp->t_line].l_start)(tp);
   1835 		} else {
   1836 			sc->sc_tx_stopped = 1;
   1837 		}
   1838 	}
   1839 
   1840 #ifdef COM_DEBUG
   1841 	if (com_debug)
   1842 		comstatus(sc, "com_stsoft");
   1843 #endif
   1844 }
   1845 
   1846 #ifdef __GENERIC_SOFT_INTERRUPTS
   1847 void
   1848 comsoft(arg)
   1849 	void *arg;
   1850 {
   1851 	struct com_softc *sc = arg;
   1852 	struct tty *tp;
   1853 
   1854 	if (COM_ISALIVE(sc) == 0)
   1855 		return;
   1856 
   1857 	{
   1858 #else
   1859 void
   1860 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1861 comsoft()
   1862 #else
   1863 comsoft(arg)
   1864 	void *arg;
   1865 #endif
   1866 {
   1867 	struct com_softc	*sc;
   1868 	struct tty	*tp;
   1869 	int	unit;
   1870 #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1871 	int s;
   1872 
   1873 	s = splsoftserial();
   1874 	com_softintr_scheduled = 0;
   1875 #endif
   1876 
   1877 	for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
   1878 		sc = com_cd.cd_devs[unit];
   1879 		if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
   1880 			continue;
   1881 
   1882 		if (COM_ISALIVE(sc) == 0)
   1883 			continue;
   1884 
   1885 		tp = sc->sc_tty;
   1886 		if (tp == NULL)
   1887 			continue;
   1888 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1889 			continue;
   1890 #endif
   1891 		tp = sc->sc_tty;
   1892 
   1893 		if (sc->sc_rx_ready) {
   1894 			sc->sc_rx_ready = 0;
   1895 			com_rxsoft(sc, tp);
   1896 		}
   1897 
   1898 		if (sc->sc_st_check) {
   1899 			sc->sc_st_check = 0;
   1900 			com_stsoft(sc, tp);
   1901 		}
   1902 
   1903 		if (sc->sc_tx_done) {
   1904 			sc->sc_tx_done = 0;
   1905 			com_txsoft(sc, tp);
   1906 		}
   1907 	}
   1908 
   1909 #ifndef __GENERIC_SOFT_INTERRUPTS
   1910 #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1911 	splx(s);
   1912 #endif
   1913 #endif
   1914 }
   1915 
   1916 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
   1917 	/* there has got to be a better way to do comsoft() */
   1918 }}
   1919 #endif
   1920 
   1921 int
   1922 comintr(arg)
   1923 	void *arg;
   1924 {
   1925 	struct com_softc *sc = arg;
   1926 	bus_space_tag_t iot = sc->sc_iot;
   1927 	bus_space_handle_t ioh = sc->sc_ioh;
   1928 	u_char *put, *end;
   1929 	u_int cc;
   1930 	u_char lsr, iir;
   1931 
   1932 	if (COM_ISALIVE(sc) == 0)
   1933 		return (0);
   1934 
   1935 	iir = bus_space_read_1(iot, ioh, com_iir);
   1936 	if (ISSET(iir, IIR_NOPEND))
   1937 		return (0);
   1938 
   1939 	end = sc->sc_ebuf;
   1940 	put = sc->sc_rbput;
   1941 	cc = sc->sc_rbavail;
   1942 
   1943 	do {
   1944 		u_char	msr, delta;
   1945 
   1946 		lsr = bus_space_read_1(iot, ioh, com_lsr);
   1947 #if defined(DDB) || defined(KGDB)
   1948 		if (ISSET(lsr, LSR_BI)) {
   1949 #ifdef DDB
   1950 			if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1951 				console_debugger();
   1952 				continue;
   1953 			}
   1954 #endif
   1955 #ifdef KGDB
   1956 			if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
   1957 				kgdb_connect(1);
   1958 				continue;
   1959 			}
   1960 #endif
   1961 		}
   1962 #endif /* DDB || KGDB */
   1963 
   1964 		if (ISSET(lsr, LSR_RCV_MASK) &&
   1965 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1966 			while (cc > 0) {
   1967 				put[0] = bus_space_read_1(iot, ioh, com_data);
   1968 				put[1] = lsr;
   1969 				put += 2;
   1970 				if (put >= end)
   1971 					put = sc->sc_rbuf;
   1972 				cc--;
   1973 
   1974 				lsr = bus_space_read_1(iot, ioh, com_lsr);
   1975 				if (!ISSET(lsr, LSR_RCV_MASK))
   1976 					break;
   1977 			}
   1978 
   1979 			/*
   1980 			 * Current string of incoming characters ended because
   1981 			 * no more data was available or we ran out of space.
   1982 			 * Schedule a receive event if any data was received.
   1983 			 * If we're out of space, turn off receive interrupts.
   1984 			 */
   1985 			sc->sc_rbput = put;
   1986 			sc->sc_rbavail = cc;
   1987 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1988 				sc->sc_rx_ready = 1;
   1989 
   1990 			/*
   1991 			 * See if we are in danger of overflowing a buffer. If
   1992 			 * so, use hardware flow control to ease the pressure.
   1993 			 */
   1994 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1995 			    cc < sc->sc_r_hiwat) {
   1996 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1997 				com_hwiflow(sc);
   1998 			}
   1999 
   2000 			/*
   2001 			 * If we're out of space, disable receive interrupts
   2002 			 * until the queue has drained a bit.
   2003 			 */
   2004 			if (!cc) {
   2005 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   2006 				CLR(sc->sc_ier, IER_ERXRDY);
   2007 				bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   2008 			}
   2009 		} else {
   2010 			if ((iir & IIR_IMASK) == IIR_RXRDY) {
   2011 				bus_space_write_1(iot, ioh, com_ier, 0);
   2012 				delay(10);
   2013 				bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
   2014 				iir = IIR_NOPEND;
   2015 				continue;
   2016 			}
   2017 		}
   2018 
   2019 		msr = bus_space_read_1(iot, ioh, com_msr);
   2020 		delta = msr ^ sc->sc_msr;
   2021 		sc->sc_msr = msr;
   2022 		/*
   2023 		 * Pulse-per-second (PSS) signals on edge of DCD?
   2024 		 * Process these even if line discipline is ignoring DCD.
   2025 		 */
   2026 		if (delta & sc->sc_ppsmask) {
   2027 			struct timeval tv;
   2028 		    	if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
   2029 				/* XXX nanotime() */
   2030 				microtime(&tv);
   2031 				TIMEVAL_TO_TIMESPEC(&tv,
   2032 				    &sc->ppsinfo.assert_timestamp);
   2033 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   2034 					timespecadd(&sc->ppsinfo.assert_timestamp,
   2035 					    &sc->ppsparam.assert_offset,
   2036 						    &sc->ppsinfo.assert_timestamp);
   2037 				}
   2038 
   2039 #ifdef PPS_SYNC
   2040 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   2041 					hardpps(&tv, tv.tv_usec);
   2042 #endif
   2043 				sc->ppsinfo.assert_sequence++;
   2044 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   2045 
   2046 			} else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
   2047 				/* XXX nanotime() */
   2048 				microtime(&tv);
   2049 				TIMEVAL_TO_TIMESPEC(&tv,
   2050 				    &sc->ppsinfo.clear_timestamp);
   2051 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   2052 					timespecadd(&sc->ppsinfo.clear_timestamp,
   2053 					    &sc->ppsparam.clear_offset,
   2054 					    &sc->ppsinfo.clear_timestamp);
   2055 				}
   2056 
   2057 #ifdef PPS_SYNC
   2058 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   2059 					hardpps(&tv, tv.tv_usec);
   2060 #endif
   2061 				sc->ppsinfo.clear_sequence++;
   2062 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   2063 			}
   2064 		}
   2065 
   2066 		/*
   2067 		 * Process normal status changes
   2068 		 */
   2069 		if (ISSET(delta, sc->sc_msr_mask)) {
   2070 			SET(sc->sc_msr_delta, delta);
   2071 
   2072 			/*
   2073 			 * Stop output immediately if we lose the output
   2074 			 * flow control signal or carrier detect.
   2075 			 */
   2076 			if (ISSET(~msr, sc->sc_msr_mask)) {
   2077 				sc->sc_tbc = 0;
   2078 				sc->sc_heldtbc = 0;
   2079 #ifdef COM_DEBUG
   2080 				if (com_debug)
   2081 					comstatus(sc, "comintr  ");
   2082 #endif
   2083 			}
   2084 
   2085 			sc->sc_st_check = 1;
   2086 		}
   2087 	} while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
   2088 
   2089 	/*
   2090 	 * Done handling any receive interrupts. See if data can be
   2091 	 * transmitted as well. Schedule tx done event if no data left
   2092 	 * and tty was marked busy.
   2093 	 */
   2094 	if (ISSET(lsr, LSR_TXRDY)) {
   2095 		/*
   2096 		 * If we've delayed a parameter change, do it now, and restart
   2097 		 * output.
   2098 		 */
   2099 		if (sc->sc_heldchange) {
   2100 			com_loadchannelregs(sc);
   2101 			sc->sc_heldchange = 0;
   2102 			sc->sc_tbc = sc->sc_heldtbc;
   2103 			sc->sc_heldtbc = 0;
   2104 		}
   2105 
   2106 		/* Output the next chunk of the contiguous buffer, if any. */
   2107 		if (sc->sc_tbc > 0) {
   2108 			int n;
   2109 
   2110 			n = sc->sc_tbc;
   2111 			if (n > sc->sc_fifolen)
   2112 				n = sc->sc_fifolen;
   2113 			bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   2114 			sc->sc_tbc -= n;
   2115 			sc->sc_tba += n;
   2116 		} else {
   2117 			/* Disable transmit completion interrupts if necessary. */
   2118 			if (ISSET(sc->sc_ier, IER_ETXRDY)) {
   2119 				CLR(sc->sc_ier, IER_ETXRDY);
   2120 				bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   2121 			}
   2122 			if (sc->sc_tx_busy) {
   2123 				sc->sc_tx_busy = 0;
   2124 				sc->sc_tx_done = 1;
   2125 			}
   2126 		}
   2127 	}
   2128 
   2129 	/* Wake up the poller. */
   2130 #ifdef __GENERIC_SOFT_INTERRUPTS
   2131 	softintr_schedule(sc->sc_si);
   2132 #else
   2133 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   2134 	setsoftserial();
   2135 #else
   2136 	if (!com_softintr_scheduled) {
   2137 		com_softintr_scheduled = 1;
   2138 		callout_reset(&comsoft_callout, 1, comsoft, NULL);
   2139 	}
   2140 #endif
   2141 #endif
   2142 
   2143 #if NRND > 0 && defined(RND_COM)
   2144 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   2145 #endif
   2146 
   2147 	return (1);
   2148 }
   2149 
   2150 /*
   2151  * The following functions are polled getc and putc routines, shared
   2152  * by the console and kgdb glue.
   2153  */
   2154 
   2155 int
   2156 com_common_getc(iot, ioh)
   2157 	bus_space_tag_t iot;
   2158 	bus_space_handle_t ioh;
   2159 {
   2160 	int s = splserial();
   2161 	u_char stat, c;
   2162 
   2163 	/* block until a character becomes available */
   2164 	while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
   2165 		;
   2166 
   2167 	c = bus_space_read_1(iot, ioh, com_data);
   2168 	stat = bus_space_read_1(iot, ioh, com_iir);
   2169 	splx(s);
   2170 	return (c);
   2171 }
   2172 
   2173 void
   2174 com_common_putc(iot, ioh, c)
   2175 	bus_space_tag_t iot;
   2176 	bus_space_handle_t ioh;
   2177 	int c;
   2178 {
   2179 	int s = splserial();
   2180 	int timo;
   2181 
   2182 	/* wait for any pending transmission to finish */
   2183 	timo = 150000;
   2184 	while (!ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
   2185 		continue;
   2186 
   2187 	bus_space_write_1(iot, ioh, com_data, c);
   2188 	COM_BARRIER(iot, ioh, BR | BW);
   2189 
   2190 	/* wait for this transmission to complete */
   2191 	timo = 1500000;
   2192 	while (!ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
   2193 		continue;
   2194 
   2195 	splx(s);
   2196 }
   2197 
   2198 /*
   2199  * Initialize UART for use as console or KGDB line.
   2200  */
   2201 int
   2202 cominit(iot, iobase, rate, frequency, cflag, iohp)
   2203 	bus_space_tag_t iot;
   2204 	int iobase;
   2205 	int rate, frequency;
   2206 	tcflag_t cflag;
   2207 	bus_space_handle_t *iohp;
   2208 {
   2209 	bus_space_handle_t ioh;
   2210 
   2211 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
   2212 		return (ENOMEM); /* ??? */
   2213 
   2214 	bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   2215 	bus_space_write_1(iot, ioh, com_efr, 0);
   2216 	bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
   2217 	rate = comspeed(rate, frequency);
   2218 	bus_space_write_1(iot, ioh, com_dlbl, rate);
   2219 	bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
   2220 	bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
   2221 	bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
   2222 	bus_space_write_1(iot, ioh, com_fifo,
   2223 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
   2224 	bus_space_write_1(iot, ioh, com_ier, 0);
   2225 
   2226 	*iohp = ioh;
   2227 	return (0);
   2228 }
   2229 
   2230 /*
   2231  * Following are all routines needed for COM to act as console
   2232  */
   2233 
   2234 int
   2235 comcnattach(iot, iobase, rate, frequency, cflag)
   2236 	bus_space_tag_t iot;
   2237 	int iobase;
   2238 	int rate, frequency;
   2239 	tcflag_t cflag;
   2240 {
   2241 	int res;
   2242 	static struct consdev comcons = {
   2243 		NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL,
   2244 		    NODEV, CN_NORMAL
   2245 	};
   2246 
   2247 	res = cominit(iot, iobase, rate, frequency, cflag, &comconsioh);
   2248 	if (res)
   2249 		return (res);
   2250 
   2251 	cn_tab = &comcons;
   2252 
   2253 	comconstag = iot;
   2254 	comconsaddr = iobase;
   2255 	comconsrate = rate;
   2256 	comconscflag = cflag;
   2257 
   2258 	return (0);
   2259 }
   2260 
   2261 int
   2262 comcngetc(dev)
   2263 	dev_t dev;
   2264 {
   2265 
   2266 	return (com_common_getc(comconstag, comconsioh));
   2267 }
   2268 
   2269 /*
   2270  * Console kernel output character routine.
   2271  */
   2272 void
   2273 comcnputc(dev, c)
   2274 	dev_t dev;
   2275 	int c;
   2276 {
   2277 
   2278 	com_common_putc(comconstag, comconsioh, c);
   2279 }
   2280 
   2281 void
   2282 comcnpollc(dev, on)
   2283 	dev_t dev;
   2284 	int on;
   2285 {
   2286 
   2287 }
   2288 
   2289 #ifdef KGDB
   2290 int
   2291 com_kgdb_attach(iot, iobase, rate, frequency, cflag)
   2292 	bus_space_tag_t iot;
   2293 	int iobase;
   2294 	int rate, frequency;
   2295 	tcflag_t cflag;
   2296 {
   2297 	int res;
   2298 
   2299 	if (iot == comconstag && iobase == comconsaddr)
   2300 		return (EBUSY); /* cannot share with console */
   2301 
   2302 	res = cominit(iot, iobase, rate, frequency, cflag, &com_kgdb_ioh);
   2303 	if (res)
   2304 		return (res);
   2305 
   2306 	kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
   2307 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2308 
   2309 	com_kgdb_iot = iot;
   2310 	com_kgdb_addr = iobase;
   2311 
   2312 	return (0);
   2313 }
   2314 
   2315 /* ARGSUSED */
   2316 int
   2317 com_kgdb_getc(arg)
   2318 	void *arg;
   2319 {
   2320 
   2321 	return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
   2322 }
   2323 
   2324 /* ARGSUSED */
   2325 void
   2326 com_kgdb_putc(arg, c)
   2327 	void *arg;
   2328 	int c;
   2329 {
   2330 
   2331 	return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
   2332 }
   2333 #endif /* KGDB */
   2334 
   2335 /* helper function to identify the com ports used by
   2336  console or KGDB (and not yet autoconf attached) */
   2337 int
   2338 com_is_console(iot, iobase, ioh)
   2339 	bus_space_tag_t iot;
   2340 	int iobase;
   2341 	bus_space_handle_t *ioh;
   2342 {
   2343 	bus_space_handle_t help;
   2344 
   2345 	if (!comconsattached &&
   2346 	    iot == comconstag && iobase == comconsaddr)
   2347 		help = comconsioh;
   2348 #ifdef KGDB
   2349 	else if (!com_kgdb_attached &&
   2350 	    iot == com_kgdb_iot && iobase == com_kgdb_addr)
   2351 		help = com_kgdb_ioh;
   2352 #endif
   2353 	else
   2354 		return (0);
   2355 
   2356 	if (ioh)
   2357 		*ioh = help;
   2358 	return (1);
   2359 }
   2360