Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.66
      1 /*	$NetBSD: com.c,v 1.66 1996/02/17 04:04:28 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.
    749 			 */
    750 			while (ISSET(tp->t_state, TS_BUSY)) {
    751 				int error;
    752 
    753 				++sc->sc_halt;
    754 				error = ttysleep(tp, &tp->t_outq,
    755 				    TTOPRI | PCATCH, "comprm", 0);
    756 				--sc->sc_halt;
    757 				if (error) {
    758 					splx(s);
    759 					comstart(tp);
    760 					return (error);
    761 				}
    762 			}
    763 
    764 			outb(iobase + com_lcr, lcr | LCR_DLAB);
    765 			outb(iobase + com_dlbl, ospeed);
    766 			outb(iobase + com_dlbh, ospeed >> 8);
    767 			outb(iobase + com_lcr, lcr);
    768 			SET(sc->sc_mcr, MCR_DTR);
    769 			outb(iobase + com_mcr, sc->sc_mcr);
    770 		} else
    771 			outb(iobase + com_lcr, lcr);
    772 
    773 		if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP) &&
    774 		    ISSET(sc->sc_hwflags, COM_HW_FIFO))
    775 			outb(iobase + com_fifo,
    776 			    FIFO_ENABLE |
    777 			    (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
    778 	} else
    779 		outb(iobase + com_lcr, lcr);
    780 
    781 	/* When not using CRTSCTS, RTS follows DTR. */
    782 	if (!ISSET(t->c_cflag, CRTSCTS)) {
    783 		if (ISSET(sc->sc_mcr, MCR_DTR)) {
    784 			if (!ISSET(sc->sc_mcr, MCR_RTS)) {
    785 				SET(sc->sc_mcr, MCR_RTS);
    786 				outb(iobase + com_mcr, sc->sc_mcr);
    787 			}
    788 		} else {
    789 			if (ISSET(sc->sc_mcr, MCR_RTS)) {
    790 				CLR(sc->sc_mcr, MCR_RTS);
    791 				outb(iobase + com_mcr, sc->sc_mcr);
    792 			}
    793 		}
    794 		sc->sc_dtr = MCR_DTR | MCR_RTS;
    795 	} else
    796 		sc->sc_dtr = MCR_DTR;
    797 
    798 	/* and copy to tty */
    799 	tp->t_ispeed = t->c_ispeed;
    800 	tp->t_ospeed = t->c_ospeed;
    801 	oldcflag = tp->t_cflag;
    802 	tp->t_cflag = t->c_cflag;
    803 
    804 	/*
    805 	 * If DCD is off and MDMBUF is changed, ask the tty layer if we should
    806 	 * stop the device.
    807 	 */
    808 	if (!ISSET(sc->sc_msr, MSR_DCD) &&
    809 	    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
    810 	    ISSET(oldcflag, MDMBUF) != ISSET(tp->t_cflag, MDMBUF) &&
    811 	    (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
    812 		CLR(sc->sc_mcr, sc->sc_dtr);
    813 		outb(iobase + com_mcr, sc->sc_mcr);
    814 	}
    815 
    816 	/* Just to be sure... */
    817 	splx(s);
    818 	comstart(tp);
    819 	return 0;
    820 }
    821 
    822 void
    823 comstart(tp)
    824 	struct tty *tp;
    825 {
    826 	struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
    827 	int iobase = sc->sc_iobase;
    828 	int s;
    829 
    830 	s = spltty();
    831 	if (ISSET(tp->t_state, TS_TTSTOP | TS_BUSY))
    832 		goto out;
    833 	if (sc->sc_halt > 0)
    834 		goto out;
    835 	if (ISSET(tp->t_cflag, CRTSCTS) && !ISSET(sc->sc_msr, MSR_CTS))
    836 		goto out;
    837 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    838 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    839 			CLR(tp->t_state, TS_ASLEEP);
    840 			wakeup(&tp->t_outq);
    841 		}
    842 		if (tp->t_outq.c_cc == 0)
    843 			goto out;
    844 		selwakeup(&tp->t_wsel);
    845 	}
    846 	SET(tp->t_state, TS_BUSY);
    847 
    848 #ifdef COM_HAYESP
    849 	if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
    850 		u_char buffer[1024], *cp = buffer;
    851 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    852 		do
    853 			outb(iobase + com_data, *cp++);
    854 		while (--n);
    855 	}
    856 	else
    857 #endif
    858 	if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
    859 		u_char buffer[16], *cp = buffer;
    860 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    861 		do {
    862 			outb(iobase + com_data, *cp++);
    863 		} while (--n);
    864 	} else
    865 		outb(iobase + com_data, getc(&tp->t_outq));
    866 out:
    867 	splx(s);
    868 }
    869 
    870 /*
    871  * Stop output on a line.
    872  */
    873 void
    874 comstop(tp, flag)
    875 	struct tty *tp;
    876 {
    877 	int s;
    878 
    879 	s = spltty();
    880 	if (ISSET(tp->t_state, TS_BUSY))
    881 		if (!ISSET(tp->t_state, TS_TTSTOP))
    882 			SET(tp->t_state, TS_FLUSH);
    883 	splx(s);
    884 }
    885 
    886 void
    887 comdiag(arg)
    888 	void *arg;
    889 {
    890 	struct com_softc *sc = arg;
    891 	int overflows, floods;
    892 	int s;
    893 
    894 	s = spltty();
    895 	sc->sc_errors = 0;
    896 	overflows = sc->sc_overflows;
    897 	sc->sc_overflows = 0;
    898 	floods = sc->sc_floods;
    899 	sc->sc_floods = 0;
    900 	splx(s);
    901 
    902 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s\n",
    903 	    sc->sc_dev.dv_xname,
    904 	    overflows, overflows == 1 ? "" : "s",
    905 	    floods, floods == 1 ? "" : "s");
    906 }
    907 
    908 void
    909 compoll(arg)
    910 	void *arg;
    911 {
    912 	int unit;
    913 	struct com_softc *sc;
    914 	struct tty *tp;
    915 	register u_char *ibufp;
    916 	u_char *ibufend;
    917 	register int c;
    918 	int s;
    919 	static int lsrmap[8] = {
    920 		0,      TTY_PE,
    921 		TTY_FE, TTY_PE|TTY_FE,
    922 		TTY_FE, TTY_PE|TTY_FE,
    923 		TTY_FE, TTY_PE|TTY_FE
    924 	};
    925 
    926 	s = spltty();
    927 	if (comevents == 0) {
    928 		splx(s);
    929 		goto out;
    930 	}
    931 	comevents = 0;
    932 	splx(s);
    933 
    934 	for (unit = 0; unit < comcd.cd_ndevs; unit++) {
    935 		sc = comcd.cd_devs[unit];
    936 		if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
    937 			continue;
    938 
    939 		tp = sc->sc_tty;
    940 
    941 		s = spltty();
    942 
    943 		ibufp = sc->sc_ibuf;
    944 		ibufend = sc->sc_ibufp;
    945 
    946 		if (ibufp == ibufend) {
    947 			splx(s);
    948 			continue;
    949 		}
    950 
    951 		sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
    952 					     sc->sc_ibufs[1] : sc->sc_ibufs[0];
    953 		sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
    954 		sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
    955 
    956 		if (tp == 0 || !ISSET(tp->t_state, TS_ISOPEN)) {
    957 			splx(s);
    958 			continue;
    959 		}
    960 
    961 		if (ISSET(tp->t_cflag, CRTSCTS) &&
    962 		    !ISSET(sc->sc_mcr, MCR_RTS)) {
    963 			/* XXX */
    964 			SET(sc->sc_mcr, MCR_RTS);
    965 			outb(sc->sc_iobase + com_mcr, sc->sc_mcr);
    966 		}
    967 
    968 		splx(s);
    969 
    970 		while (ibufp < ibufend) {
    971 			c = *ibufp++;
    972 			if (*ibufp & LSR_OE) {
    973 				sc->sc_overflows++;
    974 				if (sc->sc_errors++ == 0)
    975 					timeout(comdiag, sc, 60 * hz);
    976 			}
    977 			/* This is ugly, but fast. */
    978 			c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
    979 			(*linesw[tp->t_line].l_rint)(c, tp);
    980 		}
    981 	}
    982 
    983 out:
    984 	timeout(compoll, NULL, 1);
    985 }
    986 
    987 int
    988 comintr(arg)
    989 	void *arg;
    990 {
    991 	struct com_softc *sc = arg;
    992 	int iobase = sc->sc_iobase;
    993 	struct tty *tp;
    994 	u_char lsr, data, msr, delta;
    995 
    996 	if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
    997 		return (0);
    998 
    999 	tp = sc->sc_tty;
   1000 
   1001 	for (;;) {
   1002 		lsr = inb(iobase + com_lsr);
   1003 
   1004 		if (ISSET(lsr, LSR_RCV_MASK)) {
   1005 			register u_char *p = sc->sc_ibufp;
   1006 
   1007 			comevents = 1;
   1008 			do {
   1009 				data = ISSET(lsr, LSR_RXRDY) ?
   1010 				    inb(iobase + com_data) : 0;
   1011 				if (ISSET(lsr, LSR_BI)) {
   1012 #ifdef DDB
   1013 					if (sc->sc_dev.dv_unit == comconsole) {
   1014 						Debugger();
   1015 						goto next;
   1016 					}
   1017 #endif
   1018 					data = '\0';
   1019 				}
   1020 				if (p >= sc->sc_ibufend) {
   1021 					sc->sc_floods++;
   1022 					if (sc->sc_errors++ == 0)
   1023 						timeout(comdiag, sc, 60 * hz);
   1024 				} else {
   1025 					*p++ = data;
   1026 					*p++ = lsr;
   1027 					if (p == sc->sc_ibufhigh &&
   1028 					    ISSET(tp->t_cflag, CRTSCTS)) {
   1029 						/* XXX */
   1030 						CLR(sc->sc_mcr, MCR_RTS);
   1031 						outb(iobase + com_mcr,
   1032 						     sc->sc_mcr);
   1033 					}
   1034 				}
   1035 			next:
   1036 				lsr = inb(iobase + com_lsr);
   1037 			} while (ISSET(lsr, LSR_RCV_MASK));
   1038 
   1039 			sc->sc_ibufp = p;
   1040 		}
   1041 #if 0
   1042 		else if (ISSET(lsr, LSR_BI|LSR_FE|LSR_PE|LSR_OE))
   1043 			printf("weird lsr %02x\n", lsr);
   1044 #endif
   1045 
   1046 		msr = inb(iobase + com_msr);
   1047 
   1048 		if (msr != sc->sc_msr) {
   1049 			delta = msr ^ sc->sc_msr;
   1050 			sc->sc_msr = msr;
   1051 			if (ISSET(delta, MSR_DCD) &&
   1052 			    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
   1053 			    (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD)) == 0) {
   1054 				CLR(sc->sc_mcr, sc->sc_dtr);
   1055 				outb(iobase + com_mcr, sc->sc_mcr);
   1056 			}
   1057 			if (ISSET(delta & msr, MSR_CTS) &&
   1058 			    ISSET(tp->t_cflag, CRTSCTS)) {
   1059 				/* the line is up and we want to do rts/cts flow control */
   1060 				(*linesw[tp->t_line].l_start)(tp);
   1061 			}
   1062 		}
   1063 
   1064 		if (ISSET(lsr, LSR_TXRDY) && ISSET(tp->t_state, TS_BUSY)) {
   1065 			CLR(tp->t_state, TS_BUSY);
   1066 			if (sc->sc_halt > 0)
   1067 				wakeup(&tp->t_outq);
   1068 			else if (ISSET(tp->t_state, TS_FLUSH))
   1069 				CLR(tp->t_state, TS_FLUSH);
   1070 			else
   1071 				(*linesw[tp->t_line].l_start)(tp);
   1072 		}
   1073 
   1074 		if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
   1075 			return (1);
   1076 	}
   1077 }
   1078 
   1079 /*
   1080  * Following are all routines needed for COM to act as console
   1081  */
   1082 #include <dev/cons.h>
   1083 
   1084 void
   1085 comcnprobe(cp)
   1086 	struct consdev *cp;
   1087 {
   1088 
   1089 	if (!comprobe1(CONADDR)) {
   1090 		cp->cn_pri = CN_DEAD;
   1091 		return;
   1092 	}
   1093 
   1094 	/* locate the major number */
   1095 	for (commajor = 0; commajor < nchrdev; commajor++)
   1096 		if (cdevsw[commajor].d_open == comopen)
   1097 			break;
   1098 
   1099 	/* initialize required fields */
   1100 	cp->cn_dev = makedev(commajor, CONUNIT);
   1101 #ifdef	COMCONSOLE
   1102 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
   1103 #else
   1104 	cp->cn_pri = CN_NORMAL;
   1105 #endif
   1106 }
   1107 
   1108 void
   1109 comcninit(cp)
   1110 	struct consdev *cp;
   1111 {
   1112 
   1113 	cominit(CONUNIT, comdefaultrate);
   1114 	comconsole = CONUNIT;
   1115 	comconsinit = 0;
   1116 }
   1117 
   1118 cominit(unit, rate)
   1119 	int unit, rate;
   1120 {
   1121 	int s = splhigh();
   1122 	int iobase = CONADDR;
   1123 	u_char stat;
   1124 
   1125 	outb(iobase + com_lcr, LCR_DLAB);
   1126 	rate = comspeed(comdefaultrate);
   1127 	outb(iobase + com_dlbl, rate);
   1128 	outb(iobase + com_dlbh, rate >> 8);
   1129 	outb(iobase + com_lcr, LCR_8BITS);
   1130 	outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
   1131 	outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
   1132 	stat = inb(iobase + com_iir);
   1133 	splx(s);
   1134 }
   1135 
   1136 comcngetc(dev)
   1137 	dev_t dev;
   1138 {
   1139 	int s = splhigh();
   1140 	int iobase = CONADDR;
   1141 	u_char stat, c;
   1142 
   1143 	while (!ISSET(stat = inb(iobase + com_lsr), LSR_RXRDY))
   1144 		;
   1145 	c = inb(iobase + com_data);
   1146 	stat = inb(iobase + com_iir);
   1147 	splx(s);
   1148 	return c;
   1149 }
   1150 
   1151 /*
   1152  * Console kernel output character routine.
   1153  */
   1154 void
   1155 comcnputc(dev, c)
   1156 	dev_t dev;
   1157 	int c;
   1158 {
   1159 	int s = splhigh();
   1160 	int iobase = CONADDR;
   1161 	u_char stat;
   1162 	register int timo;
   1163 
   1164 #ifdef KGDB
   1165 	if (dev != kgdb_dev)
   1166 #endif
   1167 	if (comconsinit == 0) {
   1168 		(void) cominit(COMUNIT(dev), comdefaultrate);
   1169 		comconsinit = 1;
   1170 	}
   1171 	/* wait for any pending transmission to finish */
   1172 	timo = 50000;
   1173 	while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
   1174 		;
   1175 	outb(iobase + com_data, c);
   1176 	/* wait for this transmission to complete */
   1177 	timo = 1500000;
   1178 	while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
   1179 		;
   1180 	/* clear any interrupts generated by this transmission */
   1181 	stat = inb(iobase + com_iir);
   1182 	splx(s);
   1183 }
   1184 
   1185 void
   1186 comcnpollc(dev, on)
   1187 	dev_t dev;
   1188 	int on;
   1189 {
   1190 
   1191 }
   1192