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