Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.159.2.4
      1 /*	$NetBSD: com.c,v 1.159.2.4 2000/02/08 22:14:27 he 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_IOC_CREATE:
   1023 		break;
   1024 
   1025 	case PPS_IOC_DESTROY:
   1026 		break;
   1027 
   1028 	case PPS_IOC_GETPARAMS: {
   1029 		pps_params_t *pp;
   1030 		pp = (pps_params_t *)data;
   1031 		*pp = sc->ppsparam;
   1032 		break;
   1033 	}
   1034 
   1035 	case PPS_IOC_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_IOC_GETCAP:
   1089 		*(int*)data = ppscap;
   1090 		break;
   1091 
   1092 	case PPS_IOC_FETCH: {
   1093 		pps_info_t *pi;
   1094 		pi = (pps_info_t *)data;
   1095 		*pi = sc->ppsinfo;
   1096 		break;
   1097 	}
   1098 
   1099 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
   1100 		/*
   1101 		 * Some GPS clocks models use the falling rather than
   1102 		 * rising edge as the on-the-second signal.
   1103 		 * The old API has no way to specify PPS polarity.
   1104 		 */
   1105 		sc->sc_ppsmask = MSR_DCD;
   1106 #ifndef PPS_TRAILING_EDGE
   1107 		sc->sc_ppsassert = MSR_DCD;
   1108 		sc->sc_ppsclear = -1;
   1109 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1110 		    &sc->ppsinfo.assert_timestamp);
   1111 #else
   1112 		sc->sc_ppsassert = -1
   1113 		sc->sc_ppsclear = 0;
   1114 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1115 		    &sc->ppsinfo.clear_timestamp);
   1116 #endif
   1117 		break;
   1118 
   1119 	default:
   1120 		error = ENOTTY;
   1121 		break;
   1122 	}
   1123 
   1124 	splx(s);
   1125 
   1126 #ifdef COM_DEBUG
   1127 	if (com_debug)
   1128 		comstatus(sc, "comioctl ");
   1129 #endif
   1130 
   1131 	return (error);
   1132 }
   1133 
   1134 integrate void
   1135 com_schedrx(sc)
   1136 	struct com_softc *sc;
   1137 {
   1138 
   1139 	sc->sc_rx_ready = 1;
   1140 
   1141 	/* Wake up the poller. */
   1142 #ifdef __GENERIC_SOFT_INTERRUPTS
   1143 	softintr_schedule(sc->sc_si);
   1144 #else
   1145 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1146 	setsoftserial();
   1147 #else
   1148 	if (!com_softintr_scheduled) {
   1149 		com_softintr_scheduled = 1;
   1150 		timeout(comsoft, NULL, 1);
   1151 	}
   1152 #endif
   1153 #endif
   1154 }
   1155 
   1156 void
   1157 com_break(sc, onoff)
   1158 	struct com_softc *sc;
   1159 	int onoff;
   1160 {
   1161 
   1162 	if (onoff)
   1163 		SET(sc->sc_lcr, LCR_SBREAK);
   1164 	else
   1165 		CLR(sc->sc_lcr, LCR_SBREAK);
   1166 
   1167 	if (!sc->sc_heldchange) {
   1168 		if (sc->sc_tx_busy) {
   1169 			sc->sc_heldtbc = sc->sc_tbc;
   1170 			sc->sc_tbc = 0;
   1171 			sc->sc_heldchange = 1;
   1172 		} else
   1173 			com_loadchannelregs(sc);
   1174 	}
   1175 }
   1176 
   1177 void
   1178 com_modem(sc, onoff)
   1179 	struct com_softc *sc;
   1180 	int onoff;
   1181 {
   1182 
   1183 	if (sc->sc_mcr_dtr == 0)
   1184 		return;
   1185 
   1186 	if (onoff)
   1187 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1188 	else
   1189 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1190 
   1191 	if (!sc->sc_heldchange) {
   1192 		if (sc->sc_tx_busy) {
   1193 			sc->sc_heldtbc = sc->sc_tbc;
   1194 			sc->sc_tbc = 0;
   1195 			sc->sc_heldchange = 1;
   1196 		} else
   1197 			com_loadchannelregs(sc);
   1198 	}
   1199 }
   1200 
   1201 void
   1202 tiocm_to_com(sc, how, ttybits)
   1203 	struct com_softc *sc;
   1204 	int how, ttybits;
   1205 {
   1206 	u_char combits;
   1207 
   1208 	combits = 0;
   1209 	if (ISSET(ttybits, TIOCM_DTR))
   1210 		SET(combits, MCR_DTR);
   1211 	if (ISSET(ttybits, TIOCM_RTS))
   1212 		SET(combits, MCR_RTS);
   1213 
   1214 	switch (how) {
   1215 	case TIOCMBIC:
   1216 		CLR(sc->sc_mcr, combits);
   1217 		break;
   1218 
   1219 	case TIOCMBIS:
   1220 		SET(sc->sc_mcr, combits);
   1221 		break;
   1222 
   1223 	case TIOCMSET:
   1224 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
   1225 		SET(sc->sc_mcr, combits);
   1226 		break;
   1227 	}
   1228 
   1229 	if (!sc->sc_heldchange) {
   1230 		if (sc->sc_tx_busy) {
   1231 			sc->sc_heldtbc = sc->sc_tbc;
   1232 			sc->sc_tbc = 0;
   1233 			sc->sc_heldchange = 1;
   1234 		} else
   1235 			com_loadchannelregs(sc);
   1236 	}
   1237 }
   1238 
   1239 int
   1240 com_to_tiocm(sc)
   1241 	struct com_softc *sc;
   1242 {
   1243 	u_char combits;
   1244 	int ttybits = 0;
   1245 
   1246 	combits = sc->sc_mcr;
   1247 	if (ISSET(combits, MCR_DTR))
   1248 		SET(ttybits, TIOCM_DTR);
   1249 	if (ISSET(combits, MCR_RTS))
   1250 		SET(ttybits, TIOCM_RTS);
   1251 
   1252 	combits = sc->sc_msr;
   1253 	if (ISSET(combits, MSR_DCD))
   1254 		SET(ttybits, TIOCM_CD);
   1255 	if (ISSET(combits, MSR_CTS))
   1256 		SET(ttybits, TIOCM_CTS);
   1257 	if (ISSET(combits, MSR_DSR))
   1258 		SET(ttybits, TIOCM_DSR);
   1259 	if (ISSET(combits, MSR_RI | MSR_TERI))
   1260 		SET(ttybits, TIOCM_RI);
   1261 
   1262 	if (sc->sc_ier != 0)
   1263 		SET(ttybits, TIOCM_LE);
   1264 
   1265 	return (ttybits);
   1266 }
   1267 
   1268 static u_char
   1269 cflag2lcr(cflag)
   1270 	tcflag_t cflag;
   1271 {
   1272 	u_char lcr = 0;
   1273 
   1274 	switch (ISSET(cflag, CSIZE)) {
   1275 	case CS5:
   1276 		SET(lcr, LCR_5BITS);
   1277 		break;
   1278 	case CS6:
   1279 		SET(lcr, LCR_6BITS);
   1280 		break;
   1281 	case CS7:
   1282 		SET(lcr, LCR_7BITS);
   1283 		break;
   1284 	case CS8:
   1285 		SET(lcr, LCR_8BITS);
   1286 		break;
   1287 	}
   1288 	if (ISSET(cflag, PARENB)) {
   1289 		SET(lcr, LCR_PENAB);
   1290 		if (!ISSET(cflag, PARODD))
   1291 			SET(lcr, LCR_PEVEN);
   1292 	}
   1293 	if (ISSET(cflag, CSTOPB))
   1294 		SET(lcr, LCR_STOPB);
   1295 
   1296 	return (lcr);
   1297 }
   1298 
   1299 int
   1300 comparam(tp, t)
   1301 	struct tty *tp;
   1302 	struct termios *t;
   1303 {
   1304 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1305 	int ospeed = comspeed(t->c_ospeed, sc->sc_frequency);
   1306 	u_char lcr;
   1307 	int s;
   1308 
   1309 	if (COM_ISALIVE(sc) == 0)
   1310 		return (EIO);
   1311 
   1312 	/* Check requested parameters. */
   1313 	if (ospeed < 0)
   1314 		return (EINVAL);
   1315 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1316 		return (EINVAL);
   1317 
   1318 	/*
   1319 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1320 	 * is always active.
   1321 	 */
   1322 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1323 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1324 		SET(t->c_cflag, CLOCAL);
   1325 		CLR(t->c_cflag, HUPCL);
   1326 	}
   1327 
   1328 	/*
   1329 	 * If there were no changes, don't do anything.  This avoids dropping
   1330 	 * input and improves performance when all we did was frob things like
   1331 	 * VMIN and VTIME.
   1332 	 */
   1333 	if (tp->t_ospeed == t->c_ospeed &&
   1334 	    tp->t_cflag == t->c_cflag)
   1335 		return (0);
   1336 
   1337 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
   1338 
   1339 	s = splserial();
   1340 
   1341 	sc->sc_lcr = lcr;
   1342 
   1343 	/*
   1344 	 * If we're not in a mode that assumes a connection is present, then
   1345 	 * ignore carrier changes.
   1346 	 */
   1347 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1348 		sc->sc_msr_dcd = 0;
   1349 	else
   1350 		sc->sc_msr_dcd = MSR_DCD;
   1351 	/*
   1352 	 * Set the flow control pins depending on the current flow control
   1353 	 * mode.
   1354 	 */
   1355 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1356 		sc->sc_mcr_dtr = MCR_DTR;
   1357 		sc->sc_mcr_rts = MCR_RTS;
   1358 		sc->sc_msr_cts = MSR_CTS;
   1359 		sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
   1360 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1361 		/*
   1362 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1363 		 * carrier detection.
   1364 		 */
   1365 		sc->sc_mcr_dtr = 0;
   1366 		sc->sc_mcr_rts = MCR_DTR;
   1367 		sc->sc_msr_cts = MSR_DCD;
   1368 		sc->sc_efr = 0;
   1369 	} else {
   1370 		/*
   1371 		 * If no flow control, then always set RTS.  This will make
   1372 		 * the other side happy if it mistakenly thinks we're doing
   1373 		 * RTS/CTS flow control.
   1374 		 */
   1375 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
   1376 		sc->sc_mcr_rts = 0;
   1377 		sc->sc_msr_cts = 0;
   1378 		sc->sc_efr = 0;
   1379 		if (ISSET(sc->sc_mcr, MCR_DTR))
   1380 			SET(sc->sc_mcr, MCR_RTS);
   1381 		else
   1382 			CLR(sc->sc_mcr, MCR_RTS);
   1383 	}
   1384 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1385 
   1386 #if 0
   1387 	if (ospeed == 0)
   1388 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1389 	else
   1390 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1391 #endif
   1392 
   1393 	sc->sc_dlbl = ospeed;
   1394 	sc->sc_dlbh = ospeed >> 8;
   1395 
   1396 	/*
   1397 	 * Set the FIFO threshold based on the receive speed.
   1398 	 *
   1399 	 *  * If it's a low speed, it's probably a mouse or some other
   1400 	 *    interactive device, so set the threshold low.
   1401 	 *  * If it's a high speed, trim the trigger level down to prevent
   1402 	 *    overflows.
   1403 	 *  * Otherwise set it a bit higher.
   1404 	 */
   1405 	if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
   1406 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
   1407 	else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
   1408 		sc->sc_fifo = FIFO_ENABLE |
   1409 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
   1410 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
   1411 	else
   1412 		sc->sc_fifo = 0;
   1413 
   1414 	/* And copy to tty. */
   1415 	tp->t_ispeed = 0;
   1416 	tp->t_ospeed = t->c_ospeed;
   1417 	tp->t_cflag = t->c_cflag;
   1418 
   1419 	if (!sc->sc_heldchange) {
   1420 		if (sc->sc_tx_busy) {
   1421 			sc->sc_heldtbc = sc->sc_tbc;
   1422 			sc->sc_tbc = 0;
   1423 			sc->sc_heldchange = 1;
   1424 		} else
   1425 			com_loadchannelregs(sc);
   1426 	}
   1427 
   1428 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1429 		/* Disable the high water mark. */
   1430 		sc->sc_r_hiwat = 0;
   1431 		sc->sc_r_lowat = 0;
   1432 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1433 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1434 			com_schedrx(sc);
   1435 		}
   1436 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1437 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1438 			com_hwiflow(sc);
   1439 		}
   1440 	} else {
   1441 		sc->sc_r_hiwat = com_rbuf_hiwat;
   1442 		sc->sc_r_lowat = com_rbuf_lowat;
   1443 	}
   1444 
   1445 	splx(s);
   1446 
   1447 	/*
   1448 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1449 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1450 	 * explicit request.
   1451 	 */
   1452 	(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
   1453 
   1454 #ifdef COM_DEBUG
   1455 	if (com_debug)
   1456 		comstatus(sc, "comparam ");
   1457 #endif
   1458 
   1459 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1460 		if (sc->sc_tx_stopped) {
   1461 			sc->sc_tx_stopped = 0;
   1462 			comstart(tp);
   1463 		}
   1464 	}
   1465 
   1466 	return (0);
   1467 }
   1468 
   1469 void
   1470 com_iflush(sc)
   1471 	struct com_softc *sc;
   1472 {
   1473 	bus_space_tag_t iot = sc->sc_iot;
   1474 	bus_space_handle_t ioh = sc->sc_ioh;
   1475 #ifdef DIAGNOSTIC
   1476 	int reg;
   1477 #endif
   1478 	int timo;
   1479 
   1480 #ifdef DIAGNOSTIC
   1481 	reg = 0xffff;
   1482 #endif
   1483 	timo = 50000;
   1484 	/* flush any pending I/O */
   1485 	while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
   1486 	    && --timo)
   1487 #ifdef DIAGNOSTIC
   1488 		reg =
   1489 #else
   1490 		    (void)
   1491 #endif
   1492 		    bus_space_read_1(iot, ioh, com_data);
   1493 #ifdef DIAGNOSTIC
   1494 	if (!timo)
   1495 		printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1496 		       reg);
   1497 #endif
   1498 }
   1499 
   1500 void
   1501 com_loadchannelregs(sc)
   1502 	struct com_softc *sc;
   1503 {
   1504 	bus_space_tag_t iot = sc->sc_iot;
   1505 	bus_space_handle_t ioh = sc->sc_ioh;
   1506 
   1507 	/* XXXXX necessary? */
   1508 	com_iflush(sc);
   1509 
   1510 	bus_space_write_1(iot, ioh, com_ier, 0);
   1511 
   1512 	if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
   1513 		bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1514 		bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
   1515 	}
   1516 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
   1517 	bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
   1518 	bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
   1519 	bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
   1520 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
   1521 	bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
   1522 
   1523 	bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1524 }
   1525 
   1526 int
   1527 comhwiflow(tp, block)
   1528 	struct tty *tp;
   1529 	int block;
   1530 {
   1531 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1532 	int s;
   1533 
   1534 	if (COM_ISALIVE(sc) == 0)
   1535 		return (0);
   1536 
   1537 	if (sc->sc_mcr_rts == 0)
   1538 		return (0);
   1539 
   1540 	s = splserial();
   1541 	if (block) {
   1542 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1543 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1544 			com_hwiflow(sc);
   1545 		}
   1546 	} else {
   1547 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1548 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1549 			com_schedrx(sc);
   1550 		}
   1551 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1552 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1553 			com_hwiflow(sc);
   1554 		}
   1555 	}
   1556 	splx(s);
   1557 	return (1);
   1558 }
   1559 
   1560 /*
   1561  * (un)block input via hw flowcontrol
   1562  */
   1563 void
   1564 com_hwiflow(sc)
   1565 	struct com_softc *sc;
   1566 {
   1567 	bus_space_tag_t iot = sc->sc_iot;
   1568 	bus_space_handle_t ioh = sc->sc_ioh;
   1569 
   1570 	if (sc->sc_mcr_rts == 0)
   1571 		return;
   1572 
   1573 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1574 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1575 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1576 	} else {
   1577 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1578 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1579 	}
   1580 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
   1581 }
   1582 
   1583 
   1584 void
   1585 comstart(tp)
   1586 	struct tty *tp;
   1587 {
   1588 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1589 	bus_space_tag_t iot = sc->sc_iot;
   1590 	bus_space_handle_t ioh = sc->sc_ioh;
   1591 	int s;
   1592 
   1593 	if (COM_ISALIVE(sc) == 0)
   1594 		return;
   1595 
   1596 	s = spltty();
   1597 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1598 		goto out;
   1599 	if (sc->sc_tx_stopped)
   1600 		goto out;
   1601 
   1602 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1603 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1604 			CLR(tp->t_state, TS_ASLEEP);
   1605 			wakeup(&tp->t_outq);
   1606 		}
   1607 		selwakeup(&tp->t_wsel);
   1608 		if (tp->t_outq.c_cc == 0)
   1609 			goto out;
   1610 	}
   1611 
   1612 	/* Grab the first contiguous region of buffer space. */
   1613 	{
   1614 		u_char *tba;
   1615 		int tbc;
   1616 
   1617 		tba = tp->t_outq.c_cf;
   1618 		tbc = ndqb(&tp->t_outq, 0);
   1619 
   1620 		(void)splserial();
   1621 
   1622 		sc->sc_tba = tba;
   1623 		sc->sc_tbc = tbc;
   1624 	}
   1625 
   1626 	SET(tp->t_state, TS_BUSY);
   1627 	sc->sc_tx_busy = 1;
   1628 
   1629 	/* Enable transmit completion interrupts if necessary. */
   1630 	if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
   1631 		SET(sc->sc_ier, IER_ETXRDY);
   1632 		bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1633 	}
   1634 
   1635 	/* Output the first chunk of the contiguous buffer. */
   1636 	{
   1637 		int n;
   1638 
   1639 		n = sc->sc_tbc;
   1640 		if (n > sc->sc_fifolen)
   1641 			n = sc->sc_fifolen;
   1642 		bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   1643 		sc->sc_tbc -= n;
   1644 		sc->sc_tba += n;
   1645 	}
   1646 out:
   1647 	splx(s);
   1648 	return;
   1649 }
   1650 
   1651 /*
   1652  * Stop output on a line.
   1653  */
   1654 void
   1655 comstop(tp, flag)
   1656 	struct tty *tp;
   1657 	int flag;
   1658 {
   1659 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
   1660 	int s;
   1661 
   1662 	s = splserial();
   1663 	if (ISSET(tp->t_state, TS_BUSY)) {
   1664 		/* Stop transmitting at the next chunk. */
   1665 		sc->sc_tbc = 0;
   1666 		sc->sc_heldtbc = 0;
   1667 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1668 			SET(tp->t_state, TS_FLUSH);
   1669 	}
   1670 	splx(s);
   1671 }
   1672 
   1673 void
   1674 comdiag(arg)
   1675 	void *arg;
   1676 {
   1677 	struct com_softc *sc = arg;
   1678 	int overflows, floods;
   1679 	int s;
   1680 
   1681 	s = splserial();
   1682 	overflows = sc->sc_overflows;
   1683 	sc->sc_overflows = 0;
   1684 	floods = sc->sc_floods;
   1685 	sc->sc_floods = 0;
   1686 	sc->sc_errors = 0;
   1687 	splx(s);
   1688 
   1689 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1690 	    sc->sc_dev.dv_xname,
   1691 	    overflows, overflows == 1 ? "" : "s",
   1692 	    floods, floods == 1 ? "" : "s");
   1693 }
   1694 
   1695 integrate void
   1696 com_rxsoft(sc, tp)
   1697 	struct com_softc *sc;
   1698 	struct tty *tp;
   1699 {
   1700 	int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
   1701 	u_char *get, *end;
   1702 	u_int cc, scc;
   1703 	u_char lsr;
   1704 	int code;
   1705 	int s;
   1706 
   1707 	end = sc->sc_ebuf;
   1708 	get = sc->sc_rbget;
   1709 	scc = cc = com_rbuf_size - sc->sc_rbavail;
   1710 
   1711 	if (cc == com_rbuf_size) {
   1712 		sc->sc_floods++;
   1713 		if (sc->sc_errors++ == 0)
   1714 			timeout(comdiag, sc, 60 * hz);
   1715 	}
   1716 
   1717 	while (cc) {
   1718 		code = get[0];
   1719 		lsr = get[1];
   1720 		if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
   1721 			if (ISSET(lsr, LSR_OE)) {
   1722 				sc->sc_overflows++;
   1723 				if (sc->sc_errors++ == 0)
   1724 					timeout(comdiag, sc, 60 * hz);
   1725 			}
   1726 			if (ISSET(lsr, LSR_BI | LSR_FE))
   1727 				SET(code, TTY_FE);
   1728 			if (ISSET(lsr, LSR_PE))
   1729 				SET(code, TTY_PE);
   1730 		}
   1731 		if ((*rint)(code, tp) == -1) {
   1732 			/*
   1733 			 * The line discipline's buffer is out of space.
   1734 			 */
   1735 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1736 				/*
   1737 				 * We're either not using flow control, or the
   1738 				 * line discipline didn't tell us to block for
   1739 				 * some reason.  Either way, we have no way to
   1740 				 * know when there's more space available, so
   1741 				 * just drop the rest of the data.
   1742 				 */
   1743 				get += cc << 1;
   1744 				if (get >= end)
   1745 					get -= com_rbuf_size << 1;
   1746 				cc = 0;
   1747 			} else {
   1748 				/*
   1749 				 * Don't schedule any more receive processing
   1750 				 * until the line discipline tells us there's
   1751 				 * space available (through comhwiflow()).
   1752 				 * Leave the rest of the data in the input
   1753 				 * buffer.
   1754 				 */
   1755 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1756 			}
   1757 			break;
   1758 		}
   1759 		get += 2;
   1760 		if (get >= end)
   1761 			get = sc->sc_rbuf;
   1762 		cc--;
   1763 	}
   1764 
   1765 	if (cc != scc) {
   1766 		sc->sc_rbget = get;
   1767 		s = splserial();
   1768 		cc = sc->sc_rbavail += scc - cc;
   1769 		/* Buffers should be ok again, release possible block. */
   1770 		if (cc >= sc->sc_r_lowat) {
   1771 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1772 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1773 				SET(sc->sc_ier, IER_ERXRDY);
   1774 				bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
   1775 			}
   1776 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1777 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1778 				com_hwiflow(sc);
   1779 			}
   1780 		}
   1781 		splx(s);
   1782 	}
   1783 }
   1784 
   1785 integrate void
   1786 com_txsoft(sc, tp)
   1787 	struct com_softc *sc;
   1788 	struct tty *tp;
   1789 {
   1790 
   1791 	CLR(tp->t_state, TS_BUSY);
   1792 	if (ISSET(tp->t_state, TS_FLUSH))
   1793 		CLR(tp->t_state, TS_FLUSH);
   1794 	else
   1795 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1796 	(*linesw[tp->t_line].l_start)(tp);
   1797 }
   1798 
   1799 integrate void
   1800 com_stsoft(sc, tp)
   1801 	struct com_softc *sc;
   1802 	struct tty *tp;
   1803 {
   1804 	u_char msr, delta;
   1805 	int s;
   1806 
   1807 	s = splserial();
   1808 	msr = sc->sc_msr;
   1809 	delta = sc->sc_msr_delta;
   1810 	sc->sc_msr_delta = 0;
   1811 	splx(s);
   1812 
   1813 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1814 		/*
   1815 		 * Inform the tty layer that carrier detect changed.
   1816 		 */
   1817 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
   1818 	}
   1819 
   1820 	if (ISSET(delta, sc->sc_msr_cts)) {
   1821 		/* Block or unblock output according to flow control. */
   1822 		if (ISSET(msr, sc->sc_msr_cts)) {
   1823 			sc->sc_tx_stopped = 0;
   1824 			(*linesw[tp->t_line].l_start)(tp);
   1825 		} else {
   1826 			sc->sc_tx_stopped = 1;
   1827 		}
   1828 	}
   1829 
   1830 #ifdef COM_DEBUG
   1831 	if (com_debug)
   1832 		comstatus(sc, "com_stsoft");
   1833 #endif
   1834 }
   1835 
   1836 #ifdef __GENERIC_SOFT_INTERRUPTS
   1837 void
   1838 comsoft(arg)
   1839 	void *arg;
   1840 {
   1841 	struct com_softc *sc = arg;
   1842 	struct tty *tp;
   1843 
   1844 	if (COM_ISALIVE(sc) == 0)
   1845 		return;
   1846 
   1847 	{
   1848 #else
   1849 void
   1850 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1851 comsoft()
   1852 #else
   1853 comsoft(arg)
   1854 	void *arg;
   1855 #endif
   1856 {
   1857 	struct com_softc	*sc;
   1858 	struct tty	*tp;
   1859 	int	unit;
   1860 #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1861 	int s;
   1862 
   1863 	s = splsoftserial();
   1864 	com_softintr_scheduled = 0;
   1865 #endif
   1866 
   1867 	for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
   1868 		sc = com_cd.cd_devs[unit];
   1869 		if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
   1870 			continue;
   1871 
   1872 		if (COM_ISALIVE(sc) == 0)
   1873 			continue;
   1874 
   1875 		tp = sc->sc_tty;
   1876 		if (tp == NULL)
   1877 			continue;
   1878 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1879 			continue;
   1880 #endif
   1881 		tp = sc->sc_tty;
   1882 
   1883 		if (sc->sc_rx_ready) {
   1884 			sc->sc_rx_ready = 0;
   1885 			com_rxsoft(sc, tp);
   1886 		}
   1887 
   1888 		if (sc->sc_st_check) {
   1889 			sc->sc_st_check = 0;
   1890 			com_stsoft(sc, tp);
   1891 		}
   1892 
   1893 		if (sc->sc_tx_done) {
   1894 			sc->sc_tx_done = 0;
   1895 			com_txsoft(sc, tp);
   1896 		}
   1897 	}
   1898 
   1899 #ifndef __GENERIC_SOFT_INTERRUPTS
   1900 #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1901 	splx(s);
   1902 #endif
   1903 #endif
   1904 }
   1905 
   1906 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
   1907 	/* there has got to be a better way to do comsoft() */
   1908 }}
   1909 #endif
   1910 
   1911 int
   1912 comintr(arg)
   1913 	void *arg;
   1914 {
   1915 	struct com_softc *sc = arg;
   1916 	bus_space_tag_t iot = sc->sc_iot;
   1917 	bus_space_handle_t ioh = sc->sc_ioh;
   1918 	u_char *put, *end;
   1919 	u_int cc;
   1920 	u_char lsr, iir;
   1921 
   1922 	if (COM_ISALIVE(sc) == 0)
   1923 		return (0);
   1924 
   1925 	iir = bus_space_read_1(iot, ioh, com_iir);
   1926 	if (ISSET(iir, IIR_NOPEND))
   1927 		return (0);
   1928 
   1929 	end = sc->sc_ebuf;
   1930 	put = sc->sc_rbput;
   1931 	cc = sc->sc_rbavail;
   1932 
   1933 	do {
   1934 		u_char	msr, delta;
   1935 
   1936 		lsr = bus_space_read_1(iot, ioh, com_lsr);
   1937 #if defined(DDB) || defined(KGDB)
   1938 		if (ISSET(lsr, LSR_BI)) {
   1939 #ifdef DDB
   1940 			if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1941 				console_debugger();
   1942 				continue;
   1943 			}
   1944 #endif
   1945 #ifdef KGDB
   1946 			if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
   1947 				kgdb_connect(1);
   1948 				continue;
   1949 			}
   1950 #endif
   1951 		}
   1952 #endif /* DDB || KGDB */
   1953 
   1954 		if (ISSET(lsr, LSR_RCV_MASK) &&
   1955 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1956 			while (cc > 0) {
   1957 				put[0] = bus_space_read_1(iot, ioh, com_data);
   1958 				put[1] = lsr;
   1959 				put += 2;
   1960 				if (put >= end)
   1961 					put = sc->sc_rbuf;
   1962 				cc--;
   1963 
   1964 				lsr = bus_space_read_1(iot, ioh, com_lsr);
   1965 				if (!ISSET(lsr, LSR_RCV_MASK))
   1966 					break;
   1967 			}
   1968 
   1969 			/*
   1970 			 * Current string of incoming characters ended because
   1971 			 * no more data was available or we ran out of space.
   1972 			 * Schedule a receive event if any data was received.
   1973 			 * If we're out of space, turn off receive interrupts.
   1974 			 */
   1975 			sc->sc_rbput = put;
   1976 			sc->sc_rbavail = cc;
   1977 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1978 				sc->sc_rx_ready = 1;
   1979 
   1980 			/*
   1981 			 * See if we are in danger of overflowing a buffer. If
   1982 			 * so, use hardware flow control to ease the pressure.
   1983 			 */
   1984 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1985 			    cc < sc->sc_r_hiwat) {
   1986 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1987 				com_hwiflow(sc);
   1988 			}
   1989 
   1990 			/*
   1991 			 * If we're out of space, disable receive interrupts
   1992 			 * until the queue has drained a bit.
   1993 			 */
   1994 			if (!cc) {
   1995 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1996 				CLR(sc->sc_ier, IER_ERXRDY);
   1997 				bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   1998 			}
   1999 		} else {
   2000 			if ((iir & IIR_IMASK) == IIR_RXRDY) {
   2001 				bus_space_write_1(iot, ioh, com_ier, 0);
   2002 				delay(10);
   2003 				bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
   2004 				iir = IIR_NOPEND;
   2005 				continue;
   2006 			}
   2007 		}
   2008 
   2009 		msr = bus_space_read_1(iot, ioh, com_msr);
   2010 		delta = msr ^ sc->sc_msr;
   2011 		sc->sc_msr = msr;
   2012 		/*
   2013 		 * Pulse-per-second (PSS) signals on edge of DCD?
   2014 		 * Process these even if line discipline is ignoring DCD.
   2015 		 */
   2016 		if (delta & sc->sc_ppsmask) {
   2017 			struct timeval tv;
   2018 		    	if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
   2019 				/* XXX nanotime() */
   2020 				microtime(&tv);
   2021 				TIMEVAL_TO_TIMESPEC(&tv,
   2022 				    &sc->ppsinfo.assert_timestamp);
   2023 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   2024 					timespecadd(&sc->ppsinfo.assert_timestamp,
   2025 					    &sc->ppsparam.assert_offset,
   2026 						    &sc->ppsinfo.assert_timestamp);
   2027 				}
   2028 
   2029 #ifdef PPS_SYNC
   2030 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   2031 					hardpps(&tv, tv.tv_usec);
   2032 #endif
   2033 				sc->ppsinfo.assert_sequence++;
   2034 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   2035 
   2036 			} else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
   2037 				/* XXX nanotime() */
   2038 				microtime(&tv);
   2039 				TIMEVAL_TO_TIMESPEC(&tv,
   2040 				    &sc->ppsinfo.clear_timestamp);
   2041 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   2042 					timespecadd(&sc->ppsinfo.clear_timestamp,
   2043 					    &sc->ppsparam.clear_offset,
   2044 					    &sc->ppsinfo.clear_timestamp);
   2045 				}
   2046 
   2047 #ifdef PPS_SYNC
   2048 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   2049 					hardpps(&tv, tv.tv_usec);
   2050 #endif
   2051 				sc->ppsinfo.clear_sequence++;
   2052 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   2053 			}
   2054 		}
   2055 
   2056 		/*
   2057 		 * Process normal status changes
   2058 		 */
   2059 		if (ISSET(delta, sc->sc_msr_mask)) {
   2060 			SET(sc->sc_msr_delta, delta);
   2061 
   2062 			/*
   2063 			 * Stop output immediately if we lose the output
   2064 			 * flow control signal or carrier detect.
   2065 			 */
   2066 			if (ISSET(~msr, sc->sc_msr_mask)) {
   2067 				sc->sc_tbc = 0;
   2068 				sc->sc_heldtbc = 0;
   2069 #ifdef COM_DEBUG
   2070 				if (com_debug)
   2071 					comstatus(sc, "comintr  ");
   2072 #endif
   2073 			}
   2074 
   2075 			sc->sc_st_check = 1;
   2076 		}
   2077 	} while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
   2078 
   2079 	/*
   2080 	 * Done handling any receive interrupts. See if data can be
   2081 	 * transmitted as well. Schedule tx done event if no data left
   2082 	 * and tty was marked busy.
   2083 	 */
   2084 	if (ISSET(lsr, LSR_TXRDY)) {
   2085 		/*
   2086 		 * If we've delayed a parameter change, do it now, and restart
   2087 		 * output.
   2088 		 */
   2089 		if (sc->sc_heldchange) {
   2090 			com_loadchannelregs(sc);
   2091 			sc->sc_heldchange = 0;
   2092 			sc->sc_tbc = sc->sc_heldtbc;
   2093 			sc->sc_heldtbc = 0;
   2094 		}
   2095 
   2096 		/* Output the next chunk of the contiguous buffer, if any. */
   2097 		if (sc->sc_tbc > 0) {
   2098 			int n;
   2099 
   2100 			n = sc->sc_tbc;
   2101 			if (n > sc->sc_fifolen)
   2102 				n = sc->sc_fifolen;
   2103 			bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
   2104 			sc->sc_tbc -= n;
   2105 			sc->sc_tba += n;
   2106 		} else {
   2107 			/* Disable transmit completion interrupts if necessary. */
   2108 			if (ISSET(sc->sc_ier, IER_ETXRDY)) {
   2109 				CLR(sc->sc_ier, IER_ETXRDY);
   2110 				bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
   2111 			}
   2112 			if (sc->sc_tx_busy) {
   2113 				sc->sc_tx_busy = 0;
   2114 				sc->sc_tx_done = 1;
   2115 			}
   2116 		}
   2117 	}
   2118 
   2119 	/* Wake up the poller. */
   2120 #ifdef __GENERIC_SOFT_INTERRUPTS
   2121 	softintr_schedule(sc->sc_si);
   2122 #else
   2123 #ifndef __NO_SOFT_SERIAL_INTERRUPT
   2124 	setsoftserial();
   2125 #else
   2126 	if (!com_softintr_scheduled) {
   2127 		com_softintr_scheduled = 1;
   2128 		timeout(comsoft, NULL, 1);
   2129 	}
   2130 #endif
   2131 #endif
   2132 
   2133 #if NRND > 0 && defined(RND_COM)
   2134 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   2135 #endif
   2136 
   2137 	return (1);
   2138 }
   2139 
   2140 /*
   2141  * The following functions are polled getc and putc routines, shared
   2142  * by the console and kgdb glue.
   2143  */
   2144 
   2145 int
   2146 com_common_getc(iot, ioh)
   2147 	bus_space_tag_t iot;
   2148 	bus_space_handle_t ioh;
   2149 {
   2150 	int s = splserial();
   2151 	u_char stat, c;
   2152 
   2153 	/* block until a character becomes available */
   2154 	while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
   2155 		;
   2156 
   2157 	c = bus_space_read_1(iot, ioh, com_data);
   2158 	stat = bus_space_read_1(iot, ioh, com_iir);
   2159 	splx(s);
   2160 	return (c);
   2161 }
   2162 
   2163 void
   2164 com_common_putc(iot, ioh, c)
   2165 	bus_space_tag_t iot;
   2166 	bus_space_handle_t ioh;
   2167 	int c;
   2168 {
   2169 	int s = splserial();
   2170 	int timo;
   2171 
   2172 	/* wait for any pending transmission to finish */
   2173 	timo = 150000;
   2174 	while (!ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
   2175 		continue;
   2176 
   2177 	bus_space_write_1(iot, ioh, com_data, c);
   2178 	COM_BARRIER(iot, ioh, BR | BW);
   2179 
   2180 	/* wait for this transmission to complete */
   2181 	timo = 1500000;
   2182 	while (!ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
   2183 		continue;
   2184 
   2185 	splx(s);
   2186 }
   2187 
   2188 /*
   2189  * Initialize UART for use as console or KGDB line.
   2190  */
   2191 int
   2192 cominit(iot, iobase, rate, frequency, cflag, iohp)
   2193 	bus_space_tag_t iot;
   2194 	int iobase;
   2195 	int rate, frequency;
   2196 	tcflag_t cflag;
   2197 	bus_space_handle_t *iohp;
   2198 {
   2199 	bus_space_handle_t ioh;
   2200 
   2201 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
   2202 		return (ENOMEM); /* ??? */
   2203 
   2204 	bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   2205 	bus_space_write_1(iot, ioh, com_efr, 0);
   2206 	bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
   2207 	rate = comspeed(rate, frequency);
   2208 	bus_space_write_1(iot, ioh, com_dlbl, rate);
   2209 	bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
   2210 	bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
   2211 	bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
   2212 	bus_space_write_1(iot, ioh, com_fifo,
   2213 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
   2214 	bus_space_write_1(iot, ioh, com_ier, 0);
   2215 
   2216 	*iohp = ioh;
   2217 	return (0);
   2218 }
   2219 
   2220 /*
   2221  * Following are all routines needed for COM to act as console
   2222  */
   2223 
   2224 int
   2225 comcnattach(iot, iobase, rate, frequency, cflag)
   2226 	bus_space_tag_t iot;
   2227 	int iobase;
   2228 	int rate, frequency;
   2229 	tcflag_t cflag;
   2230 {
   2231 	int res;
   2232 	static struct consdev comcons = {
   2233 		NULL, NULL, comcngetc, comcnputc, comcnpollc, NODEV, CN_NORMAL
   2234 	};
   2235 
   2236 	res = cominit(iot, iobase, rate, frequency, cflag, &comconsioh);
   2237 	if (res)
   2238 		return (res);
   2239 
   2240 	cn_tab = &comcons;
   2241 
   2242 	comconstag = iot;
   2243 	comconsaddr = iobase;
   2244 	comconsrate = rate;
   2245 	comconscflag = cflag;
   2246 
   2247 	return (0);
   2248 }
   2249 
   2250 int
   2251 comcngetc(dev)
   2252 	dev_t dev;
   2253 {
   2254 
   2255 	return (com_common_getc(comconstag, comconsioh));
   2256 }
   2257 
   2258 /*
   2259  * Console kernel output character routine.
   2260  */
   2261 void
   2262 comcnputc(dev, c)
   2263 	dev_t dev;
   2264 	int c;
   2265 {
   2266 
   2267 	com_common_putc(comconstag, comconsioh, c);
   2268 }
   2269 
   2270 void
   2271 comcnpollc(dev, on)
   2272 	dev_t dev;
   2273 	int on;
   2274 {
   2275 
   2276 }
   2277 
   2278 #ifdef KGDB
   2279 int
   2280 com_kgdb_attach(iot, iobase, rate, frequency, cflag)
   2281 	bus_space_tag_t iot;
   2282 	int iobase;
   2283 	int rate, frequency;
   2284 	tcflag_t cflag;
   2285 {
   2286 	int res;
   2287 
   2288 	if (iot == comconstag && iobase == comconsaddr)
   2289 		return (EBUSY); /* cannot share with console */
   2290 
   2291 	res = cominit(iot, iobase, rate, frequency, cflag, &com_kgdb_ioh);
   2292 	if (res)
   2293 		return (res);
   2294 
   2295 	kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
   2296 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2297 
   2298 	com_kgdb_iot = iot;
   2299 	com_kgdb_addr = iobase;
   2300 
   2301 	return (0);
   2302 }
   2303 
   2304 /* ARGSUSED */
   2305 int
   2306 com_kgdb_getc(arg)
   2307 	void *arg;
   2308 {
   2309 
   2310 	return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
   2311 }
   2312 
   2313 /* ARGSUSED */
   2314 void
   2315 com_kgdb_putc(arg, c)
   2316 	void *arg;
   2317 	int c;
   2318 {
   2319 
   2320 	return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
   2321 }
   2322 #endif /* KGDB */
   2323 
   2324 /* helper function to identify the com ports used by
   2325  console or KGDB (and not yet autoconf attached) */
   2326 int
   2327 com_is_console(iot, iobase, ioh)
   2328 	bus_space_tag_t iot;
   2329 	int iobase;
   2330 	bus_space_handle_t *ioh;
   2331 {
   2332 	bus_space_handle_t help;
   2333 
   2334 	if (!comconsattached &&
   2335 	    iot == comconstag && iobase == comconsaddr)
   2336 		help = comconsioh;
   2337 #ifdef KGDB
   2338 	else if (!com_kgdb_attached &&
   2339 	    iot == com_kgdb_iot && iobase == com_kgdb_addr)
   2340 		help = com_kgdb_ioh;
   2341 #endif
   2342 	else
   2343 		return (0);
   2344 
   2345 	if (ioh)
   2346 		*ioh = help;
   2347 	return (1);
   2348 }
   2349