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