Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.67
      1 /*	$NetBSD: com.c,v 1.67 1996/02/17 04:51:41 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
      5  * Copyright (c) 1991 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     37  */
     38 
     39 /*
     40  * COM driver, based on HP dca driver
     41  * uses National Semiconductor NS16450/NS16550AF UART
     42  */
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/select.h>
     47 #include <sys/tty.h>
     48 #include <sys/proc.h>
     49 #include <sys/user.h>
     50 #include <sys/conf.h>
     51 #include <sys/file.h>
     52 #include <sys/uio.h>
     53 #include <sys/kernel.h>
     54 #include <sys/syslog.h>
     55 #include <sys/types.h>
     56 #include <sys/device.h>
     57 
     58 #include <machine/cpu.h>
     59 #include <machine/pio.h>
     60 
     61 #include <dev/isa/isavar.h>
     62 #include <dev/isa/comreg.h>
     63 #include <dev/ic/ns16550reg.h>
     64 #ifdef COM_HAYESP
     65 #include <dev/ic/hayespreg.h>
     66 #endif
     67 #define	com_lcr	com_cfcr
     68 
     69 #define	COM_IBUFSIZE	(2 * 512)
     70 #define	COM_IHIGHWATER	((3 * COM_IBUFSIZE) / 4)
     71 
     72 struct com_softc {
     73 	struct device sc_dev;
     74 	void *sc_ih;
     75 	struct tty *sc_tty;
     76 
     77 	int sc_overflows;
     78 	int sc_floods;
     79 	int sc_errors;
     80 
     81 	int sc_halt;
     82 
     83 	int sc_iobase;
     84 #ifdef COM_HAYESP
     85 	int sc_hayespbase;
     86 #endif
     87 	u_char sc_hwflags;
     88 #define	COM_HW_NOIEN	0x01
     89 #define	COM_HW_FIFO	0x02
     90 #define	COM_HW_HAYESP	0x04
     91 #define	COM_HW_CONSOLE	0x40
     92 	u_char sc_swflags;
     93 #define	COM_SW_SOFTCAR	0x01
     94 #define	COM_SW_CLOCAL	0x02
     95 #define	COM_SW_CRTSCTS	0x04
     96 #define	COM_SW_MDMBUF	0x08
     97 	u_char sc_msr, sc_mcr, sc_lcr;
     98 	u_char sc_dtr;
     99 
    100 	u_char *sc_ibuf, *sc_ibufp, *sc_ibufhigh, *sc_ibufend;
    101 	u_char sc_ibufs[2][COM_IBUFSIZE];
    102 };
    103 
    104 int comprobe __P((struct device *, void *, void *));
    105 void comattach __P((struct device *, struct device *, void *));
    106 int comopen __P((dev_t, int, int, struct proc *));
    107 int comclose __P((dev_t, int, int, struct proc *));
    108 void comdiag __P((void *));
    109 int comintr __P((void *));
    110 void compoll __P((void *));
    111 int comparam __P((struct tty *, struct termios *));
    112 void comstart __P((struct tty *));
    113 
    114 struct cfdriver comcd = {
    115 	NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
    116 };
    117 
    118 #ifdef COMCONSOLE
    119 int	comdefaultrate = CONSPEED;
    120 int	comconsole = COMCONSOLE;
    121 #else
    122 int	comdefaultrate = TTYDEF_SPEED;
    123 int	comconsole = -1;
    124 #endif
    125 int	comconsinit;
    126 int	commajor;
    127 int	comsopen = 0;
    128 int	comevents = 0;
    129 
    130 #ifdef KGDB
    131 #include <machine/remote-sl.h>
    132 extern int kgdb_dev;
    133 extern int kgdb_rate;
    134 extern int kgdb_debug_init;
    135 #endif
    136 
    137 #define	COMUNIT(x)	(minor(x))
    138 
    139 /* Macros to clear/set/test flags. */
    140 #define	SET(t, f)	(t) |= (f)
    141 #define	CLR(t, f)	(t) &= ~(f)
    142 #define	ISSET(t, f)	((t) & (f))
    143 
    144 int
    145 comspeed(speed)
    146 	long speed;
    147 {
    148 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    149 
    150 	int x, err;
    151 
    152 	if (speed == 0)
    153 		return 0;
    154 	if (speed < 0)
    155 		return -1;
    156 	x = divrnd((COM_FREQ / 16), speed);
    157 	if (x <= 0)
    158 		return -1;
    159 	err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
    160 	if (err < 0)
    161 		err = -err;
    162 	if (err > COM_TOLERANCE)
    163 		return -1;
    164 	return x;
    165 
    166 #undef	divrnd(n, q)
    167 }
    168 
    169 int
    170 comprobe1(iobase)
    171 	int iobase;
    172 {
    173 
    174 	/* force access to id reg */
    175 	outb(iobase + com_lcr, 0);
    176 	outb(iobase + com_iir, 0);
    177 	if (inb(iobase + com_iir) & 0x38)
    178 		return 0;
    179 
    180 	return 1;
    181 }
    182 
    183 #ifdef COM_HAYESP
    184 int
    185 comprobeHAYESP(iobase, sc)
    186 	int iobase;
    187 	struct com_softc *sc;
    188 {
    189 	char	val, dips;
    190 	int	combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
    191 
    192 	/*
    193 	 * Hayes ESP cards have two iobases.  One is for compatibility with
    194 	 * 16550 serial chips, and at the same ISA PC base addresses.  The
    195 	 * other is for ESP-specific enhanced features, and lies at a
    196 	 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
    197 	 */
    198 
    199 	/* Test for ESP signature */
    200 	if ((inb(iobase) & 0xf3) == 0)
    201 		return 0;
    202 
    203 	/*
    204 	 * ESP is present at ESP enhanced base address; unknown com port
    205 	 */
    206 
    207 	/* Get the dip-switch configurations */
    208 	outb(iobase + HAYESP_CMD1, HAYESP_GETDIPS);
    209 	dips = inb(iobase + HAYESP_STATUS1);
    210 
    211 	/* Determine which com port this ESP card services: bits 0,1 of  */
    212 	/*  dips is the port # (0-3); combaselist[val] is the com_iobase */
    213 	if (sc->sc_iobase != combaselist[dips & 0x03])
    214 		return 0;
    215 
    216 	printf(": ESP");
    217 
    218  	/* Check ESP Self Test bits. */
    219 	/* Check for ESP version 2.0: bits 4,5,6 == 010 */
    220 	outb(iobase + HAYESP_CMD1, HAYESP_GETTEST);
    221 	val = inb(iobase + HAYESP_STATUS1);	/* Clear reg 1 */
    222 	val = inb(iobase + HAYESP_STATUS2);
    223 	if ((val & 0x70) < 0x20) {
    224 		printf("-old (%o)", val & 0x70);
    225 		/* we do not support the necessary features */
    226 		return 0;
    227 	}
    228 
    229 	/* Check for ability to emulate 16550: bit 8 == 1 */
    230 	if ((dips & 0x80) == 0) {
    231 		printf(" slave");
    232 		/* XXX Does slave really mean no 16550 support?? */
    233 		return 0;
    234 	}
    235 
    236 	/*
    237 	 * If we made it this far, we are a full-featured ESP v2.0 (or
    238 	 * better), at the correct com port address.
    239 	 */
    240 
    241 	SET(sc->sc_hwflags, COM_HW_HAYESP);
    242 	printf(", 1024k fifo\n");
    243 	return 1;
    244 }
    245 #endif
    246 
    247 int
    248 comprobe(parent, match, aux)
    249 	struct device *parent;
    250 	void *match, *aux;
    251 {
    252 	struct isa_attach_args *ia = aux;
    253 	int iobase = ia->ia_iobase;
    254 
    255 	if (!comprobe1(iobase))
    256 		return 0;
    257 
    258 	ia->ia_iosize = COM_NPORTS;
    259 	ia->ia_msize = 0;
    260 	return 1;
    261 }
    262 
    263 void
    264 comattach(parent, self, aux)
    265 	struct device *parent, *self;
    266 	void *aux;
    267 {
    268 	struct com_softc *sc = (void *)self;
    269 	struct isa_attach_args *ia = aux;
    270 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    271 	int iobase = ia->ia_iobase;
    272 	struct tty *tp;
    273 #ifdef COM_HAYESP
    274 	int	hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
    275 	int	*hayespp;
    276 #endif
    277 
    278 	sc->sc_iobase = iobase;
    279 	sc->sc_hwflags = ISSET(cf->cf_flags, COM_HW_NOIEN);
    280 	sc->sc_swflags = 0;
    281 
    282 	if (sc->sc_dev.dv_unit == comconsole)
    283 		delay(1000);
    284 
    285 #ifdef COM_HAYESP
    286 	/* Look for a Hayes ESP board. */
    287 	for (hayespp = hayesp_ports; *hayespp != 0; hayespp++)
    288 		if (comprobeHAYESP(*hayespp, sc)) {
    289 			sc->sc_hayespbase = *hayespp;
    290 			break;
    291 		}
    292 	/* No ESP; look for other things. */
    293 	if (*hayespp == 0) {
    294 #endif
    295 
    296 	/* look for a NS 16550AF UART with FIFOs */
    297 	outb(iobase + com_fifo,
    298 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    299 	delay(100);
    300 	if (ISSET(inb(iobase + com_iir), IIR_FIFO_MASK) == IIR_FIFO_MASK)
    301 		if (ISSET(inb(iobase + com_fifo), FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
    302 			SET(sc->sc_hwflags, COM_HW_FIFO);
    303 			printf(": ns16550a, working fifo\n");
    304 		} else
    305 			printf(": ns16550, broken fifo\n");
    306 	else
    307 		printf(": ns8250 or ns16450, no fifo\n");
    308 	outb(iobase + com_fifo, 0);
    309 #ifdef COM_HAYESP
    310 	}
    311 #endif
    312 
    313 	/* disable interrupts */
    314 	outb(iobase + com_ier, 0);
    315 	outb(iobase + com_mcr, 0);
    316 
    317 	if (ia->ia_irq != IRQUNK)
    318 		sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_TTY,
    319 		    comintr, sc);
    320 
    321 #ifdef KGDB
    322 	if (kgdb_dev == makedev(commajor, unit)) {
    323 		if (comconsole == unit)
    324 			kgdb_dev = -1;	/* can't debug over console port */
    325 		else {
    326 			(void) cominit(unit, kgdb_rate);
    327 			if (kgdb_debug_init) {
    328 				/*
    329 				 * Print prefix of device name,
    330 				 * let kgdb_connect print the rest.
    331 				 */
    332 				printf("%s: ", sc->sc_dev.dv_xname);
    333 				kgdb_connect(1);
    334 			} else
    335 				printf("%s: kgdb enabled\n",
    336 				    sc->sc_dev.dv_xname);
    337 		}
    338 	}
    339 #endif
    340 
    341 	if (sc->sc_dev.dv_unit == comconsole) {
    342 		/*
    343 		 * Need to reset baud rate, etc. of next print so reset
    344 		 * comconsinit.  Also make sure console is always "hardwired".
    345 		 */
    346 		comconsinit = 0;
    347 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    348 		SET(sc->sc_swflags, COM_SW_SOFTCAR);
    349 	}
    350 }
    351 
    352 int
    353 comopen(dev, flag, mode, p)
    354 	dev_t dev;
    355 	int flag, mode;
    356 	struct proc *p;
    357 {
    358 	int unit = COMUNIT(dev);
    359 	struct com_softc *sc;
    360 	int iobase;
    361 	struct tty *tp;
    362 	int s;
    363 	int error = 0;
    364 
    365 	if (unit >= comcd.cd_ndevs)
    366 		return ENXIO;
    367 	sc = comcd.cd_devs[unit];
    368 	if (!sc)
    369 		return ENXIO;
    370 
    371 	if (!sc->sc_tty)
    372 		tp = sc->sc_tty = ttymalloc();
    373 	else
    374 		tp = sc->sc_tty;
    375 
    376 	tp->t_oproc = comstart;
    377 	tp->t_param = comparam;
    378 	tp->t_dev = dev;
    379 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    380 		SET(tp->t_state, TS_WOPEN);
    381 		ttychars(tp);
    382 		tp->t_iflag = TTYDEF_IFLAG;
    383 		tp->t_oflag = TTYDEF_OFLAG;
    384 		tp->t_cflag = TTYDEF_CFLAG;
    385 		if (ISSET(sc->sc_swflags, COM_SW_CLOCAL))
    386 			SET(tp->t_cflag, CLOCAL);
    387 		if (ISSET(sc->sc_swflags, COM_SW_CRTSCTS))
    388 			SET(tp->t_cflag, CRTSCTS);
    389 		if (ISSET(sc->sc_swflags, COM_SW_MDMBUF))
    390 			SET(tp->t_cflag, MDMBUF);
    391 		tp->t_lflag = TTYDEF_LFLAG;
    392 		tp->t_ispeed = tp->t_ospeed = comdefaultrate;
    393 
    394 		s = spltty();
    395 
    396 		comparam(tp, &tp->t_termios);
    397 		ttsetwater(tp);
    398 
    399 		if (comsopen++ == 0)
    400 			timeout(compoll, NULL, 1);
    401 
    402 		sc->sc_ibufp = sc->sc_ibuf = sc->sc_ibufs[0];
    403 		sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
    404 		sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
    405 
    406 		iobase = sc->sc_iobase;
    407 #ifdef COM_HAYESP
    408 		/* Setup the ESP board */
    409 		if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
    410 			int hayespbase = sc->sc_hayespbase;
    411 
    412 			outb(iobase + com_fifo,
    413 			     FIFO_DMA_MODE|FIFO_ENABLE|
    414 			     FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_8);
    415 
    416 			/* Set 16550 compatibility mode */
    417 			outb(hayespbase + HAYESP_CMD1, HAYESP_SETMODE);
    418 			outb(hayespbase + HAYESP_CMD2,
    419 			     HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
    420 			     HAYESP_MODE_SCALE);
    421 
    422 			/* Set RTS/CTS flow control */
    423 			outb(hayespbase + HAYESP_CMD1, HAYESP_SETFLOWTYPE);
    424 			outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_RTS);
    425 			outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_CTS);
    426 
    427 			/* Set flow control levels */
    428 			outb(hayespbase + HAYESP_CMD1, HAYESP_SETRXFLOW);
    429 			outb(hayespbase + HAYESP_CMD2,
    430 			     HAYESP_HIBYTE(HAYESP_RXHIWMARK));
    431 			outb(hayespbase + HAYESP_CMD2,
    432 			     HAYESP_LOBYTE(HAYESP_RXHIWMARK));
    433 			outb(hayespbase + HAYESP_CMD2,
    434 			     HAYESP_HIBYTE(HAYESP_RXLOWMARK));
    435 			outb(hayespbase + HAYESP_CMD2,
    436 			     HAYESP_LOBYTE(HAYESP_RXLOWMARK));
    437 		} else
    438 #endif
    439 		if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
    440 			/* Set the FIFO threshold based on the receive speed. */
    441 			outb(iobase + com_fifo,
    442 			    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    443 			    (tp->t_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
    444 		/* flush any pending I/O */
    445 		while (ISSET(inb(iobase + com_lsr), LSR_RXRDY))
    446 			(void) inb(iobase + com_data);
    447 		/* you turn me on, baby */
    448 		sc->sc_mcr = MCR_DTR | MCR_RTS;
    449 		if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
    450 			SET(sc->sc_mcr, MCR_IENABLE);
    451 		outb(iobase + com_mcr, sc->sc_mcr);
    452 		outb(iobase + com_ier,
    453 		    IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
    454 
    455 		sc->sc_msr = inb(iobase + com_msr);
    456 		if (ISSET(sc->sc_swflags, COM_SW_SOFTCAR) ||
    457 		    ISSET(sc->sc_msr, MSR_DCD) || ISSET(tp->t_cflag, MDMBUF))
    458 			SET(tp->t_state, TS_CARR_ON);
    459 		else
    460 			CLR(tp->t_state, TS_CARR_ON);
    461 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    462 		return EBUSY;
    463 	else
    464 		s = spltty();
    465 
    466 	/* wait for carrier if necessary */
    467 	if (!ISSET(flag, O_NONBLOCK))
    468 		while (!ISSET(tp->t_cflag, CLOCAL) &&
    469 		    !ISSET(tp->t_state, TS_CARR_ON)) {
    470 			SET(tp->t_state, TS_WOPEN);
    471 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    472 			    ttopen, 0);
    473 			if (error) {
    474 				/* XXX should turn off chip if we're the
    475 				   only waiter */
    476 				splx(s);
    477 				return error;
    478 			}
    479 		}
    480 	splx(s);
    481 
    482 	return (*linesw[tp->t_line].l_open)(dev, tp);
    483 }
    484 
    485 int
    486 comclose(dev, flag, mode, p)
    487 	dev_t dev;
    488 	int flag, mode;
    489 	struct proc *p;
    490 {
    491 	int unit = COMUNIT(dev);
    492 	struct com_softc *sc = comcd.cd_devs[unit];
    493 	struct tty *tp = sc->sc_tty;
    494 	int iobase = sc->sc_iobase;
    495 	int s;
    496 
    497 	/* XXX This is for cons.c. */
    498 	if (!ISSET(tp->t_state, TS_ISOPEN))
    499 		return 0;
    500 
    501 	(*linesw[tp->t_line].l_close)(tp, flag);
    502 	s = spltty();
    503 	CLR(sc->sc_lcr, LCR_SBREAK);
    504 	outb(iobase + com_lcr, sc->sc_lcr);
    505 	outb(iobase + com_ier, 0);
    506 	if (ISSET(tp->t_cflag, HUPCL) &&
    507 	    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR)) {
    508 		/* XXX perhaps only clear DTR */
    509 		outb(iobase + com_mcr, 0);
    510 	}
    511 	CLR(tp->t_state, TS_BUSY | TS_FLUSH);
    512 	if (--comsopen == 0)
    513 		untimeout(compoll, NULL);
    514 	splx(s);
    515 	ttyclose(tp);
    516 #ifdef notyet /* XXXX */
    517 	if (unit != comconsole) {
    518 		ttyfree(tp);
    519 		sc->sc_tty = 0;
    520 	}
    521 #endif
    522 	return 0;
    523 }
    524 
    525 int
    526 comread(dev, uio, flag)
    527 	dev_t dev;
    528 	struct uio *uio;
    529 	int flag;
    530 {
    531 	struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
    532 	struct tty *tp = sc->sc_tty;
    533 
    534 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    535 }
    536 
    537 int
    538 comwrite(dev, uio, flag)
    539 	dev_t dev;
    540 	struct uio *uio;
    541 	int flag;
    542 {
    543 	struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
    544 	struct tty *tp = sc->sc_tty;
    545 
    546 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    547 }
    548 
    549 struct tty *
    550 comtty(dev)
    551 	dev_t dev;
    552 {
    553 	struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
    554 	struct tty *tp = sc->sc_tty;
    555 
    556 	return (tp);
    557 }
    558 
    559 static u_char
    560 tiocm_xxx2mcr(data)
    561 	int data;
    562 {
    563 	u_char m = 0;
    564 
    565 	if (ISSET(data, TIOCM_DTR))
    566 		SET(m, MCR_DTR);
    567 	if (ISSET(data, TIOCM_RTS))
    568 		SET(m, MCR_RTS);
    569 	return m;
    570 }
    571 
    572 int
    573 comioctl(dev, cmd, data, flag, p)
    574 	dev_t dev;
    575 	u_long cmd;
    576 	caddr_t data;
    577 	int flag;
    578 	struct proc *p;
    579 {
    580 	int unit = COMUNIT(dev);
    581 	struct com_softc *sc = comcd.cd_devs[unit];
    582 	struct tty *tp = sc->sc_tty;
    583 	int iobase = sc->sc_iobase;
    584 	int error;
    585 
    586 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    587 	if (error >= 0)
    588 		return error;
    589 	error = ttioctl(tp, cmd, data, flag, p);
    590 	if (error >= 0)
    591 		return error;
    592 
    593 	switch (cmd) {
    594 	case TIOCSBRK:
    595 		SET(sc->sc_lcr, LCR_SBREAK);
    596 		outb(iobase + com_lcr, sc->sc_lcr);
    597 		break;
    598 	case TIOCCBRK:
    599 		CLR(sc->sc_lcr, LCR_SBREAK);
    600 		outb(iobase + com_lcr, sc->sc_lcr);
    601 		break;
    602 	case TIOCSDTR:
    603 		SET(sc->sc_mcr, sc->sc_dtr);
    604 		outb(iobase + com_mcr, sc->sc_mcr);
    605 		break;
    606 	case TIOCCDTR:
    607 		CLR(sc->sc_mcr, sc->sc_dtr);
    608 		outb(iobase + com_mcr, sc->sc_mcr);
    609 		break;
    610 	case TIOCMSET:
    611 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
    612 	case TIOCMBIS:
    613 		SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
    614 		outb(iobase + com_mcr, sc->sc_mcr);
    615 		break;
    616 	case TIOCMBIC:
    617 		CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
    618 		outb(iobase + com_mcr, sc->sc_mcr);
    619 		break;
    620 	case TIOCMGET: {
    621 		u_char m;
    622 		int bits = 0;
    623 
    624 		m = sc->sc_mcr;
    625 		if (ISSET(m, MCR_DTR))
    626 			SET(bits, TIOCM_DTR);
    627 		if (ISSET(m, MCR_RTS))
    628 			SET(bits, TIOCM_RTS);
    629 		m = sc->sc_msr;
    630 		if (ISSET(m, MSR_DCD))
    631 			SET(bits, TIOCM_CD);
    632 		if (ISSET(m, MSR_CTS))
    633 			SET(bits, TIOCM_CTS);
    634 		if (ISSET(m, MSR_DSR))
    635 			SET(bits, TIOCM_DSR);
    636 		if (ISSET(m, MSR_RI | MSR_TERI))
    637 			SET(bits, TIOCM_RI);
    638 		if (inb(iobase + com_ier))
    639 			SET(bits, TIOCM_LE);
    640 		*(int *)data = bits;
    641 		break;
    642 	}
    643 	case TIOCGFLAGS: {
    644 		int driverbits, userbits = 0;
    645 
    646 		driverbits = sc->sc_swflags;
    647 		if (ISSET(driverbits, COM_SW_SOFTCAR))
    648 			SET(userbits, TIOCFLAG_SOFTCAR);
    649 		if (ISSET(driverbits, COM_SW_CLOCAL))
    650 			SET(userbits, TIOCFLAG_CLOCAL);
    651 		if (ISSET(driverbits, COM_SW_CRTSCTS))
    652 			SET(userbits, TIOCFLAG_CRTSCTS);
    653 		if (ISSET(driverbits, COM_SW_MDMBUF))
    654 			SET(userbits, TIOCFLAG_MDMBUF);
    655 
    656 		*(int *)data = userbits;
    657 		break;
    658 	}
    659 	case TIOCSFLAGS: {
    660 		int userbits, driverbits = 0;
    661 
    662 		error = suser(p->p_ucred, &p->p_acflag);
    663 		if (error != 0)
    664 			return(EPERM);
    665 
    666 		userbits = *(int *)data;
    667 		if (ISSET(userbits, TIOCFLAG_SOFTCAR) ||
    668 		    ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    669 			SET(driverbits, COM_SW_SOFTCAR);
    670 		if (ISSET(userbits, TIOCFLAG_CLOCAL))
    671 			SET(driverbits, COM_SW_CLOCAL);
    672 		if (ISSET(userbits, TIOCFLAG_CRTSCTS))
    673 			SET(driverbits, COM_SW_CRTSCTS);
    674 		if (ISSET(userbits, TIOCFLAG_MDMBUF))
    675 			SET(driverbits, COM_SW_MDMBUF);
    676 
    677 		sc->sc_swflags = driverbits;
    678 		break;
    679 	}
    680 	default:
    681 		return ENOTTY;
    682 	}
    683 
    684 	return 0;
    685 }
    686 
    687 int
    688 comparam(tp, t)
    689 	struct tty *tp;
    690 	struct termios *t;
    691 {
    692 	struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
    693 	int iobase = sc->sc_iobase;
    694 	int ospeed = comspeed(t->c_ospeed);
    695 	u_char lcr;
    696 	tcflag_t oldcflag;
    697 	int s;
    698 
    699 	/* check requested parameters */
    700 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    701 		return EINVAL;
    702 
    703 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK);
    704 
    705 	switch (ISSET(t->c_cflag, CSIZE)) {
    706 	case CS5:
    707 		SET(lcr, LCR_5BITS);
    708 		break;
    709 	case CS6:
    710 		SET(lcr, LCR_6BITS);
    711 		break;
    712 	case CS7:
    713 		SET(lcr, LCR_7BITS);
    714 		break;
    715 	case CS8:
    716 		SET(lcr, LCR_8BITS);
    717 		break;
    718 	}
    719 	if (ISSET(t->c_cflag, PARENB)) {
    720 		SET(lcr, LCR_PENAB);
    721 		if (!ISSET(t->c_cflag, PARODD))
    722 			SET(lcr, LCR_PEVEN);
    723 	}
    724 	if (ISSET(t->c_cflag, CSTOPB))
    725 		SET(lcr, LCR_STOPB);
    726 
    727 	sc->sc_lcr = lcr;
    728 
    729 	s = spltty();
    730 
    731 	if (ospeed == 0) {
    732 		CLR(sc->sc_mcr, MCR_DTR);
    733 		outb(iobase + com_mcr, sc->sc_mcr);
    734 	}
    735 
    736 	/*
    737 	 * Set the FIFO threshold based on the receive speed, if we are
    738 	 * changing it.
    739 	 */
    740 #if 1
    741 	if (tp->t_ispeed != t->c_ispeed) {
    742 #else
    743 	if (1) {
    744 #endif
    745 		if (ospeed != 0) {
    746 			/*
    747 			 * Make sure the transmit FIFO is empty before
    748 			 * proceeding.  If we don't do this, some revisions
    749 			 * of the UART will hang.  Interestingly enough,
    750 			 * even if we do this will the last character is
    751 			 * still being pushed out, they don't hang.  This
    752 			 * seems good enough.
    753 			 */
    754 			while (ISSET(tp->t_state, TS_BUSY)) {
    755 				int error;
    756 
    757 				++sc->sc_halt;
    758 				error = ttysleep(tp, &tp->t_outq,
    759 				    TTOPRI | PCATCH, "comprm", 0);
    760 				--sc->sc_halt;
    761 				if (error) {
    762 					splx(s);
    763 					comstart(tp);
    764 					return (error);
    765 				}
    766 			}
    767 
    768 			outb(iobase + com_lcr, lcr | LCR_DLAB);
    769 			outb(iobase + com_dlbl, ospeed);
    770 			outb(iobase + com_dlbh, ospeed >> 8);
    771 			outb(iobase + com_lcr, lcr);
    772 			SET(sc->sc_mcr, MCR_DTR);
    773 			outb(iobase + com_mcr, sc->sc_mcr);
    774 		} else
    775 			outb(iobase + com_lcr, lcr);
    776 
    777 		if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP) &&
    778 		    ISSET(sc->sc_hwflags, COM_HW_FIFO))
    779 			outb(iobase + com_fifo,
    780 			    FIFO_ENABLE |
    781 			    (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
    782 	} else
    783 		outb(iobase + com_lcr, lcr);
    784 
    785 	/* When not using CRTSCTS, RTS follows DTR. */
    786 	if (!ISSET(t->c_cflag, CRTSCTS)) {
    787 		if (ISSET(sc->sc_mcr, MCR_DTR)) {
    788 			if (!ISSET(sc->sc_mcr, MCR_RTS)) {
    789 				SET(sc->sc_mcr, MCR_RTS);
    790 				outb(iobase + com_mcr, sc->sc_mcr);
    791 			}
    792 		} else {
    793 			if (ISSET(sc->sc_mcr, MCR_RTS)) {
    794 				CLR(sc->sc_mcr, MCR_RTS);
    795 				outb(iobase + com_mcr, sc->sc_mcr);
    796 			}
    797 		}
    798 		sc->sc_dtr = MCR_DTR | MCR_RTS;
    799 	} else
    800 		sc->sc_dtr = MCR_DTR;
    801 
    802 	/* and copy to tty */
    803 	tp->t_ispeed = t->c_ispeed;
    804 	tp->t_ospeed = t->c_ospeed;
    805 	oldcflag = tp->t_cflag;
    806 	tp->t_cflag = t->c_cflag;
    807 
    808 	/*
    809 	 * If DCD is off and MDMBUF is changed, ask the tty layer if we should
    810 	 * stop the device.
    811 	 */
    812 	if (!ISSET(sc->sc_msr, MSR_DCD) &&
    813 	    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
    814 	    ISSET(oldcflag, MDMBUF) != ISSET(tp->t_cflag, MDMBUF) &&
    815 	    (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
    816 		CLR(sc->sc_mcr, sc->sc_dtr);
    817 		outb(iobase + com_mcr, sc->sc_mcr);
    818 	}
    819 
    820 	/* Just to be sure... */
    821 	splx(s);
    822 	comstart(tp);
    823 	return 0;
    824 }
    825 
    826 void
    827 comstart(tp)
    828 	struct tty *tp;
    829 {
    830 	struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
    831 	int iobase = sc->sc_iobase;
    832 	int s;
    833 
    834 	s = spltty();
    835 	if (ISSET(tp->t_state, TS_TTSTOP | TS_BUSY))
    836 		goto out;
    837 	if (sc->sc_halt > 0)
    838 		goto out;
    839 	if (ISSET(tp->t_cflag, CRTSCTS) && !ISSET(sc->sc_msr, MSR_CTS))
    840 		goto out;
    841 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    842 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    843 			CLR(tp->t_state, TS_ASLEEP);
    844 			wakeup(&tp->t_outq);
    845 		}
    846 		if (tp->t_outq.c_cc == 0)
    847 			goto out;
    848 		selwakeup(&tp->t_wsel);
    849 	}
    850 	SET(tp->t_state, TS_BUSY);
    851 
    852 #ifdef COM_HAYESP
    853 	if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
    854 		u_char buffer[1024], *cp = buffer;
    855 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    856 		do
    857 			outb(iobase + com_data, *cp++);
    858 		while (--n);
    859 	}
    860 	else
    861 #endif
    862 	if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
    863 		u_char buffer[16], *cp = buffer;
    864 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    865 		do {
    866 			outb(iobase + com_data, *cp++);
    867 		} while (--n);
    868 	} else
    869 		outb(iobase + com_data, getc(&tp->t_outq));
    870 out:
    871 	splx(s);
    872 }
    873 
    874 /*
    875  * Stop output on a line.
    876  */
    877 void
    878 comstop(tp, flag)
    879 	struct tty *tp;
    880 {
    881 	int s;
    882 
    883 	s = spltty();
    884 	if (ISSET(tp->t_state, TS_BUSY))
    885 		if (!ISSET(tp->t_state, TS_TTSTOP))
    886 			SET(tp->t_state, TS_FLUSH);
    887 	splx(s);
    888 }
    889 
    890 void
    891 comdiag(arg)
    892 	void *arg;
    893 {
    894 	struct com_softc *sc = arg;
    895 	int overflows, floods;
    896 	int s;
    897 
    898 	s = spltty();
    899 	sc->sc_errors = 0;
    900 	overflows = sc->sc_overflows;
    901 	sc->sc_overflows = 0;
    902 	floods = sc->sc_floods;
    903 	sc->sc_floods = 0;
    904 	splx(s);
    905 
    906 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s\n",
    907 	    sc->sc_dev.dv_xname,
    908 	    overflows, overflows == 1 ? "" : "s",
    909 	    floods, floods == 1 ? "" : "s");
    910 }
    911 
    912 void
    913 compoll(arg)
    914 	void *arg;
    915 {
    916 	int unit;
    917 	struct com_softc *sc;
    918 	struct tty *tp;
    919 	register u_char *ibufp;
    920 	u_char *ibufend;
    921 	register int c;
    922 	int s;
    923 	static int lsrmap[8] = {
    924 		0,      TTY_PE,
    925 		TTY_FE, TTY_PE|TTY_FE,
    926 		TTY_FE, TTY_PE|TTY_FE,
    927 		TTY_FE, TTY_PE|TTY_FE
    928 	};
    929 
    930 	s = spltty();
    931 	if (comevents == 0) {
    932 		splx(s);
    933 		goto out;
    934 	}
    935 	comevents = 0;
    936 	splx(s);
    937 
    938 	for (unit = 0; unit < comcd.cd_ndevs; unit++) {
    939 		sc = comcd.cd_devs[unit];
    940 		if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
    941 			continue;
    942 
    943 		tp = sc->sc_tty;
    944 
    945 		s = spltty();
    946 
    947 		ibufp = sc->sc_ibuf;
    948 		ibufend = sc->sc_ibufp;
    949 
    950 		if (ibufp == ibufend) {
    951 			splx(s);
    952 			continue;
    953 		}
    954 
    955 		sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
    956 					     sc->sc_ibufs[1] : sc->sc_ibufs[0];
    957 		sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
    958 		sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
    959 
    960 		if (tp == 0 || !ISSET(tp->t_state, TS_ISOPEN)) {
    961 			splx(s);
    962 			continue;
    963 		}
    964 
    965 		if (ISSET(tp->t_cflag, CRTSCTS) &&
    966 		    !ISSET(sc->sc_mcr, MCR_RTS)) {
    967 			/* XXX */
    968 			SET(sc->sc_mcr, MCR_RTS);
    969 			outb(sc->sc_iobase + com_mcr, sc->sc_mcr);
    970 		}
    971 
    972 		splx(s);
    973 
    974 		while (ibufp < ibufend) {
    975 			c = *ibufp++;
    976 			if (*ibufp & LSR_OE) {
    977 				sc->sc_overflows++;
    978 				if (sc->sc_errors++ == 0)
    979 					timeout(comdiag, sc, 60 * hz);
    980 			}
    981 			/* This is ugly, but fast. */
    982 			c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
    983 			(*linesw[tp->t_line].l_rint)(c, tp);
    984 		}
    985 	}
    986 
    987 out:
    988 	timeout(compoll, NULL, 1);
    989 }
    990 
    991 int
    992 comintr(arg)
    993 	void *arg;
    994 {
    995 	struct com_softc *sc = arg;
    996 	int iobase = sc->sc_iobase;
    997 	struct tty *tp;
    998 	u_char lsr, data, msr, delta;
    999 
   1000 	if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
   1001 		return (0);
   1002 
   1003 	tp = sc->sc_tty;
   1004 
   1005 	for (;;) {
   1006 		lsr = inb(iobase + com_lsr);
   1007 
   1008 		if (ISSET(lsr, LSR_RCV_MASK)) {
   1009 			register u_char *p = sc->sc_ibufp;
   1010 
   1011 			comevents = 1;
   1012 			do {
   1013 				data = ISSET(lsr, LSR_RXRDY) ?
   1014 				    inb(iobase + com_data) : 0;
   1015 				if (ISSET(lsr, LSR_BI)) {
   1016 #ifdef DDB
   1017 					if (sc->sc_dev.dv_unit == comconsole) {
   1018 						Debugger();
   1019 						goto next;
   1020 					}
   1021 #endif
   1022 					data = '\0';
   1023 				}
   1024 				if (p >= sc->sc_ibufend) {
   1025 					sc->sc_floods++;
   1026 					if (sc->sc_errors++ == 0)
   1027 						timeout(comdiag, sc, 60 * hz);
   1028 				} else {
   1029 					*p++ = data;
   1030 					*p++ = lsr;
   1031 					if (p == sc->sc_ibufhigh &&
   1032 					    ISSET(tp->t_cflag, CRTSCTS)) {
   1033 						/* XXX */
   1034 						CLR(sc->sc_mcr, MCR_RTS);
   1035 						outb(iobase + com_mcr,
   1036 						     sc->sc_mcr);
   1037 					}
   1038 				}
   1039 			next:
   1040 				lsr = inb(iobase + com_lsr);
   1041 			} while (ISSET(lsr, LSR_RCV_MASK));
   1042 
   1043 			sc->sc_ibufp = p;
   1044 		}
   1045 #if 0
   1046 		else if (ISSET(lsr, LSR_BI|LSR_FE|LSR_PE|LSR_OE))
   1047 			printf("weird lsr %02x\n", lsr);
   1048 #endif
   1049 
   1050 		msr = inb(iobase + com_msr);
   1051 
   1052 		if (msr != sc->sc_msr) {
   1053 			delta = msr ^ sc->sc_msr;
   1054 			sc->sc_msr = msr;
   1055 			if (ISSET(delta, MSR_DCD) &&
   1056 			    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
   1057 			    (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD)) == 0) {
   1058 				CLR(sc->sc_mcr, sc->sc_dtr);
   1059 				outb(iobase + com_mcr, sc->sc_mcr);
   1060 			}
   1061 			if (ISSET(delta & msr, MSR_CTS) &&
   1062 			    ISSET(tp->t_cflag, CRTSCTS)) {
   1063 				/* the line is up and we want to do rts/cts flow control */
   1064 				(*linesw[tp->t_line].l_start)(tp);
   1065 			}
   1066 		}
   1067 
   1068 		if (ISSET(lsr, LSR_TXRDY) && ISSET(tp->t_state, TS_BUSY)) {
   1069 			CLR(tp->t_state, TS_BUSY);
   1070 			if (sc->sc_halt > 0)
   1071 				wakeup(&tp->t_outq);
   1072 			else if (ISSET(tp->t_state, TS_FLUSH))
   1073 				CLR(tp->t_state, TS_FLUSH);
   1074 			else
   1075 				(*linesw[tp->t_line].l_start)(tp);
   1076 		}
   1077 
   1078 		if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
   1079 			return (1);
   1080 	}
   1081 }
   1082 
   1083 /*
   1084  * Following are all routines needed for COM to act as console
   1085  */
   1086 #include <dev/cons.h>
   1087 
   1088 void
   1089 comcnprobe(cp)
   1090 	struct consdev *cp;
   1091 {
   1092 
   1093 	if (!comprobe1(CONADDR)) {
   1094 		cp->cn_pri = CN_DEAD;
   1095 		return;
   1096 	}
   1097 
   1098 	/* locate the major number */
   1099 	for (commajor = 0; commajor < nchrdev; commajor++)
   1100 		if (cdevsw[commajor].d_open == comopen)
   1101 			break;
   1102 
   1103 	/* initialize required fields */
   1104 	cp->cn_dev = makedev(commajor, CONUNIT);
   1105 #ifdef	COMCONSOLE
   1106 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
   1107 #else
   1108 	cp->cn_pri = CN_NORMAL;
   1109 #endif
   1110 }
   1111 
   1112 void
   1113 comcninit(cp)
   1114 	struct consdev *cp;
   1115 {
   1116 
   1117 	cominit(CONUNIT, comdefaultrate);
   1118 	comconsole = CONUNIT;
   1119 	comconsinit = 0;
   1120 }
   1121 
   1122 cominit(unit, rate)
   1123 	int unit, rate;
   1124 {
   1125 	int s = splhigh();
   1126 	int iobase = CONADDR;
   1127 	u_char stat;
   1128 
   1129 	outb(iobase + com_lcr, LCR_DLAB);
   1130 	rate = comspeed(comdefaultrate);
   1131 	outb(iobase + com_dlbl, rate);
   1132 	outb(iobase + com_dlbh, rate >> 8);
   1133 	outb(iobase + com_lcr, LCR_8BITS);
   1134 	outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
   1135 	outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
   1136 	stat = inb(iobase + com_iir);
   1137 	splx(s);
   1138 }
   1139 
   1140 comcngetc(dev)
   1141 	dev_t dev;
   1142 {
   1143 	int s = splhigh();
   1144 	int iobase = CONADDR;
   1145 	u_char stat, c;
   1146 
   1147 	while (!ISSET(stat = inb(iobase + com_lsr), LSR_RXRDY))
   1148 		;
   1149 	c = inb(iobase + com_data);
   1150 	stat = inb(iobase + com_iir);
   1151 	splx(s);
   1152 	return c;
   1153 }
   1154 
   1155 /*
   1156  * Console kernel output character routine.
   1157  */
   1158 void
   1159 comcnputc(dev, c)
   1160 	dev_t dev;
   1161 	int c;
   1162 {
   1163 	int s = splhigh();
   1164 	int iobase = CONADDR;
   1165 	u_char stat;
   1166 	register int timo;
   1167 
   1168 #ifdef KGDB
   1169 	if (dev != kgdb_dev)
   1170 #endif
   1171 	if (comconsinit == 0) {
   1172 		(void) cominit(COMUNIT(dev), comdefaultrate);
   1173 		comconsinit = 1;
   1174 	}
   1175 	/* wait for any pending transmission to finish */
   1176 	timo = 50000;
   1177 	while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
   1178 		;
   1179 	outb(iobase + com_data, c);
   1180 	/* wait for this transmission to complete */
   1181 	timo = 1500000;
   1182 	while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
   1183 		;
   1184 	/* clear any interrupts generated by this transmission */
   1185 	stat = inb(iobase + com_iir);
   1186 	splx(s);
   1187 }
   1188 
   1189 void
   1190 comcnpollc(dev, on)
   1191 	dev_t dev;
   1192 	int on;
   1193 {
   1194 
   1195 }
   1196