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