Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.35
      1 /*-
      2  * Copyright (c) 1993, 1994 Charles Hannum.
      3  * Copyright (c) 1991 The Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the University of
     17  *	California, Berkeley and its contributors.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	from: @(#)com.c	7.5 (Berkeley) 5/16/91
     35  *	$Id: com.c,v 1.35 1994/08/24 07:25:18 mycroft Exp $
     36  */
     37 
     38 /*
     39  * COM driver, based on HP dca driver
     40  * uses National Semiconductor NS16450/NS16550AF UART
     41  */
     42 #include "com.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/select.h>
     48 #include <sys/tty.h>
     49 #include <sys/proc.h>
     50 #include <sys/user.h>
     51 #include <sys/conf.h>
     52 #include <sys/file.h>
     53 #include <sys/uio.h>
     54 #include <sys/kernel.h>
     55 #include <sys/syslog.h>
     56 #include <sys/types.h>
     57 #include <sys/device.h>
     58 
     59 #include <machine/cpu.h>
     60 #include <machine/pio.h>
     61 
     62 #include <i386/isa/isavar.h>
     63 #include <i386/isa/comreg.h>
     64 #include <i386/isa/ic/ns16550.h>
     65 
     66 struct com_softc {
     67 	struct device sc_dev;
     68 	struct intrhand sc_ih;
     69 
     70 	int sc_overflows;
     71 	u_short sc_iobase;
     72 	u_char sc_hwflags;
     73 #define	COM_HW_NOIEN	0x01
     74 #define	COM_HW_FIFO	0x02
     75 #define	COM_HW_CONSOLE	0x40
     76 	u_char sc_swflags;
     77 #define	COM_SW_SOFTCAR	0x01
     78 #define	COM_SW_CLOCAL	0x02
     79 #define	COM_SW_CRTSCTS	0x04
     80 #define	COM_SW_MDMBUF	0x08
     81 	u_char sc_msr, sc_mcr;
     82 };
     83 /* XXXX should be in com_softc, but not ready for that yet */
     84 struct	tty *com_tty[NCOM];
     85 
     86 int comprobe();
     87 void comattach();
     88 int comopen __P((dev_t, int, int, struct proc *));
     89 int comclose __P((dev_t, int, int, struct proc *));
     90 void comdiag __P((void *));
     91 int comintr __P((struct com_softc *));
     92 int comparam __P((struct tty *, struct termios *));
     93 void comstart __P((struct tty *));
     94 
     95 struct cfdriver comcd = {
     96 	NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
     97 };
     98 
     99 int	comdefaultrate = TTYDEF_SPEED;
    100 #ifdef COMCONSOLE
    101 int	comconsole = COMCONSOLE;
    102 #else
    103 int	comconsole = -1;
    104 #endif
    105 int	comconsinit;
    106 int	commajor;
    107 
    108 #ifdef KGDB
    109 #include <machine/remote-sl.h>
    110 extern int kgdb_dev;
    111 extern int kgdb_rate;
    112 extern int kgdb_debug_init;
    113 #endif
    114 
    115 #define	COMUNIT(x)	(minor(x))
    116 
    117 #define	bis(c, b)	do { const register u_short com_ad = (c); \
    118 			     outb(com_ad, inb(com_ad) | (b)); } while(0)
    119 #define	bic(c, b)	do { const register u_short com_ad = (c); \
    120 			     outb(com_ad, inb(com_ad) & ~(b)); } while(0)
    121 
    122 int
    123 comspeed(speed)
    124 	long speed;
    125 {
    126 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    127 
    128 	int x, err;
    129 
    130 	if (speed == 0)
    131 		return 0;
    132 	if (speed < 0)
    133 		return -1;
    134 	x = divrnd((COM_FREQ / 16), speed);
    135 	if (x <= 0)
    136 		return -1;
    137 	err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
    138 	if (err < 0)
    139 		err = -err;
    140 	if (err > COM_TOLERANCE)
    141 		return -1;
    142 	return x;
    143 
    144 #undef	divrnd(n, q)
    145 }
    146 
    147 int
    148 comprobe1(iobase)
    149 	u_short iobase;
    150 {
    151 
    152 	/* force access to id reg */
    153 	outb(iobase + com_cfcr, 0);
    154 	outb(iobase + com_iir, 0);
    155 	if (inb(iobase + com_iir) & 0x38)
    156 		return 0;
    157 
    158 	return 1;
    159 }
    160 
    161 int
    162 comprobe(parent, self, aux)
    163 	struct device *parent, *self;
    164 	void *aux;
    165 {
    166 	struct com_softc *sc = (void *)self;
    167 	struct isa_attach_args *ia = aux;
    168 	u_short iobase = ia->ia_iobase;
    169 
    170 	if (!comprobe1(iobase))
    171 		return 0;
    172 
    173 	ia->ia_iosize = COM_NPORTS;
    174 	ia->ia_msize = 0;
    175 	return 1;
    176 }
    177 
    178 void
    179 comattach(parent, self, aux)
    180 	struct device *parent, *self;
    181 	void *aux;
    182 {
    183 	struct com_softc *sc = (void *)self;
    184 	struct isa_attach_args *ia = aux;
    185 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    186 	u_short iobase = ia->ia_iobase;
    187 	struct tty *tp;
    188 
    189 	sc->sc_iobase = iobase;
    190 	sc->sc_hwflags = cf->cf_flags & COM_HW_NOIEN;
    191 	sc->sc_swflags = 0;
    192 
    193 	if (sc->sc_dev.dv_unit == comconsole)
    194 		delay(1000);
    195 
    196 	/* look for a NS 16550AF UART with FIFOs */
    197 	outb(iobase + com_fifo,
    198 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    199 	delay(100);
    200 	if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK)
    201 		if ((inb(iobase + com_fifo) & FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
    202 			sc->sc_hwflags |= COM_HW_FIFO;
    203 			printf(": ns16550a, working fifo\n");
    204 		} else
    205 			printf(": ns82550 or ns16550, broken fifo\n");
    206 	else
    207 		printf(": ns82450 or ns16450, no fifo\n");
    208 	outb(iobase + com_fifo, 0);
    209 
    210 	/* disable interrupts */
    211 	outb(iobase + com_ier, 0);
    212 	outb(iobase + com_mcr, 0);
    213 
    214 	if (!parent) {	/* XXX no master */
    215 		sc->sc_ih.ih_fun = comintr;
    216 		sc->sc_ih.ih_arg = sc;
    217 		sc->sc_ih.ih_level = IPL_TTY;
    218 		intr_establish(ia->ia_irq, &sc->sc_ih);
    219 	}
    220 
    221 #ifdef KGDB
    222 	if (kgdb_dev == makedev(commajor, unit)) {
    223 		if (comconsole == unit)
    224 			kgdb_dev = -1;	/* can't debug over console port */
    225 		else {
    226 			(void) cominit(unit, kgdb_rate);
    227 			if (kgdb_debug_init) {
    228 				/*
    229 				 * Print prefix of device name,
    230 				 * let kgdb_connect print the rest.
    231 				 */
    232 				printf("%s: ", sc->sc_dev.dv_xname);
    233 				kgdb_connect(1);
    234 			} else
    235 				printf("%s: kgdb enabled\n",
    236 				    sc->sc_dev.dv_xname);
    237 		}
    238 	}
    239 #endif
    240 
    241 	if (sc->sc_dev.dv_unit == comconsole) {
    242 		/*
    243 		 * Need to reset baud rate, etc. of next print so reset
    244 		 * comconsinit.  Also make sure console is always "hardwired".
    245 		 */
    246 		comconsinit = 0;
    247 		sc->sc_hwflags |= COM_HW_CONSOLE;
    248 		sc->sc_swflags |= COM_SW_SOFTCAR;
    249 	}
    250 }
    251 
    252 int
    253 comopen(dev, flag, mode, p)
    254 	dev_t dev;
    255 	int flag, mode;
    256 	struct proc *p;
    257 {
    258 	int unit = COMUNIT(dev);
    259 	struct com_softc *sc;
    260 	u_short iobase;
    261 	struct tty *tp;
    262 	int s;
    263 	int error = 0;
    264 
    265 	if (unit >= comcd.cd_ndevs)
    266 		return ENXIO;
    267 	sc = comcd.cd_devs[unit];
    268 	if (!sc)
    269 		return ENXIO;
    270 
    271 	s = spltty();
    272 
    273 	if (!com_tty[unit])
    274 		tp = com_tty[unit] = ttymalloc();
    275 	else
    276 		tp = com_tty[unit];
    277 
    278 	tp->t_oproc = comstart;
    279 	tp->t_param = comparam;
    280 	tp->t_dev = dev;
    281 	if ((tp->t_state & TS_ISOPEN) == 0) {
    282 		tp->t_state |= TS_WOPEN;
    283 		ttychars(tp);
    284 		tp->t_iflag = TTYDEF_IFLAG;
    285 		tp->t_oflag = TTYDEF_OFLAG;
    286 		tp->t_cflag = TTYDEF_CFLAG;
    287 		if (sc->sc_swflags & COM_SW_CLOCAL)
    288 			tp->t_cflag |= CLOCAL;
    289 		if (sc->sc_swflags & COM_SW_CRTSCTS)
    290 			tp->t_cflag |= CRTSCTS;
    291 		if (sc->sc_swflags & COM_SW_MDMBUF)
    292 			tp->t_cflag |= MDMBUF;
    293 		tp->t_lflag = TTYDEF_LFLAG;
    294 		tp->t_ispeed = tp->t_ospeed = comdefaultrate;
    295 		comparam(tp, &tp->t_termios);
    296 		ttsetwater(tp);
    297 
    298 		iobase = sc->sc_iobase;
    299 		/* flush any pending I/O */
    300 		(void) inb(iobase + com_lsr);
    301 		(void) inb(iobase + com_data);
    302 		/* you turn me on, baby */
    303 		sc->sc_mcr = MCR_DTR | MCR_RTS;
    304 		if (!(sc->sc_hwflags & COM_HW_NOIEN))
    305 			sc->sc_mcr |= MCR_IENABLE;
    306 		outb(iobase + com_mcr, sc->sc_mcr);
    307 		outb(iobase + com_ier,
    308 		    IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
    309 
    310 		sc->sc_msr = inb(iobase + com_msr);
    311 		if (sc->sc_swflags & COM_SW_SOFTCAR || sc->sc_msr & MSR_DCD ||
    312 		    tp->t_cflag & MDMBUF)
    313 			tp->t_state |= TS_CARR_ON;
    314 		else
    315 			tp->t_state &= ~TS_CARR_ON;
    316 	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    317 		splx(s);
    318 		return EBUSY;
    319 	}
    320 
    321 	/* wait for carrier if necessary */
    322 	if ((flag & O_NONBLOCK) == 0)
    323 		while ((tp->t_cflag & CLOCAL) == 0 &&
    324 		    (tp->t_state & TS_CARR_ON) == 0) {
    325 			tp->t_state |= TS_WOPEN;
    326 			error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    327 			    TTIPRI | PCATCH, ttopen, 0);
    328 			if (error) {
    329 				/* XXX should turn off chip if we're the
    330 				   only waiter */
    331 				splx(s);
    332 				return error;
    333 			}
    334 		}
    335 	splx(s);
    336 
    337 	return (*linesw[tp->t_line].l_open)(dev, tp);
    338 }
    339 
    340 int
    341 comclose(dev, flag, mode, p)
    342 	dev_t dev;
    343 	int flag, mode;
    344 	struct proc *p;
    345 {
    346 	int unit = COMUNIT(dev);
    347 	struct com_softc *sc = comcd.cd_devs[unit];
    348 	u_short iobase = sc->sc_iobase;
    349 	struct tty *tp = com_tty[unit];
    350 
    351 	(*linesw[tp->t_line].l_close)(tp, flag);
    352 #ifdef KGDB
    353 	/* do not disable interrupts if debugging */
    354 	if (kgdb_dev != makedev(commajor, unit))
    355 #endif
    356 	{
    357 		bic(iobase + com_cfcr, CFCR_SBREAK);
    358 		outb(iobase + com_ier, 0);
    359 		if (tp->t_cflag & HUPCL &&
    360 		    (sc->sc_swflags & COM_SW_SOFTCAR) == 0)
    361 			/* XXX perhaps only clear DTR */
    362 			outb(iobase + com_mcr, 0);
    363 	}
    364 	ttyclose(tp);
    365 #ifdef notyet /* XXXX */
    366 	if (unit != comconsole) {
    367 		ttyfree(tp);
    368 		com_tty[unit] = (struct tty *)NULL;
    369 	}
    370 #endif
    371 	return 0;
    372 }
    373 
    374 int
    375 comread(dev, uio, flag)
    376 	dev_t dev;
    377 	struct uio *uio;
    378 	int flag;
    379 {
    380 	struct tty *tp = com_tty[COMUNIT(dev)];
    381 
    382 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    383 }
    384 
    385 int
    386 comwrite(dev, uio, flag)
    387 	dev_t dev;
    388 	struct uio *uio;
    389 	int flag;
    390 {
    391 	struct tty *tp = com_tty[COMUNIT(dev)];
    392 
    393 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    394 }
    395 
    396 static u_char
    397 tiocm_xxx2mcr(data)
    398 	int data;
    399 {
    400 	u_char m = 0;
    401 
    402 	if (data & TIOCM_DTR)
    403 		m |= MCR_DTR;
    404 	if (data & TIOCM_RTS)
    405 		m |= MCR_RTS;
    406 	return m;
    407 }
    408 
    409 int
    410 comioctl(dev, cmd, data, flag, p)
    411 	dev_t dev;
    412 	int cmd;
    413 	caddr_t data;
    414 	int flag;
    415 	struct proc *p;
    416 {
    417 	int unit = COMUNIT(dev);
    418 	struct com_softc *sc = comcd.cd_devs[unit];
    419 	u_short iobase = sc->sc_iobase;
    420 	struct tty *tp = com_tty[unit];
    421 	int error;
    422 
    423 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    424 	if (error >= 0)
    425 		return error;
    426 	error = ttioctl(tp, cmd, data, flag, p);
    427 	if (error >= 0)
    428 		return error;
    429 
    430 	switch (cmd) {
    431 	case TIOCSBRK:
    432 		bis(iobase + com_cfcr, CFCR_SBREAK);
    433 		break;
    434 	case TIOCCBRK:
    435 		bic(iobase + com_cfcr, CFCR_SBREAK);
    436 		break;
    437 	case TIOCSDTR:
    438 		outb(iobase + com_mcr, sc->sc_mcr |= (MCR_DTR | MCR_RTS));
    439 		break;
    440 	case TIOCCDTR:
    441 		outb(iobase + com_mcr, sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
    442 		break;
    443 	case TIOCMSET:
    444 		sc->sc_mcr &= ~(MCR_DTR | MCR_RTS);
    445 	case TIOCMBIS:
    446 		outb(iobase + com_mcr,
    447 		    sc->sc_mcr |= tiocm_xxx2mcr(*(int *)data));
    448 		break;
    449 	case TIOCMBIC:
    450 		outb(iobase + com_mcr,
    451 		    sc->sc_mcr &= ~tiocm_xxx2mcr(*(int *)data));
    452 		break;
    453 	case TIOCMGET: {
    454 		u_char m;
    455 		int bits = 0;
    456 
    457 		m = sc->sc_mcr;
    458 		if (m & MCR_DTR)
    459 			bits |= TIOCM_DTR;
    460 		if (m & MCR_RTS)
    461 			bits |= TIOCM_RTS;
    462 		m = sc->sc_msr;
    463 		if (m & MSR_DCD)
    464 			bits |= TIOCM_CD;
    465 		if (m & MSR_CTS)
    466 			bits |= TIOCM_CTS;
    467 		if (m & MSR_DSR)
    468 			bits |= TIOCM_DSR;
    469 		if (m & (MSR_RI | MSR_TERI))
    470 			bits |= TIOCM_RI;
    471 		if (inb(iobase + com_ier))
    472 			bits |= TIOCM_LE;
    473 		*(int *)data = bits;
    474 		break;
    475 	}
    476 	case TIOCGFLAGS: {
    477 		int bits = 0;
    478 
    479 		if (sc->sc_swflags & COM_SW_SOFTCAR)
    480 			bits |= TIOCFLAG_SOFTCAR;
    481 		if (sc->sc_swflags & COM_SW_CLOCAL)
    482 			bits |= TIOCFLAG_CLOCAL;
    483 		if (sc->sc_swflags & COM_SW_CRTSCTS)
    484 			bits |= TIOCFLAG_CRTSCTS;
    485 		if (sc->sc_swflags & COM_SW_MDMBUF)
    486 			bits |= TIOCFLAG_MDMBUF;
    487 
    488 		*(int *)data = bits;
    489 		break;
    490 	}
    491 	case TIOCSFLAGS: {
    492 		int userbits, driverbits = 0;
    493 
    494 		error = suser(p->p_ucred, &p->p_acflag);
    495 		if (error != 0)
    496 			return(EPERM);
    497 
    498 		userbits = *(int *)data;
    499 		if ((userbits & TIOCFLAG_SOFTCAR) ||
    500 		    (sc->sc_hwflags & COM_HW_CONSOLE))
    501 			driverbits |= COM_SW_SOFTCAR;
    502 		if (userbits & TIOCFLAG_CLOCAL)
    503 			driverbits |= COM_SW_CLOCAL;
    504 		if (userbits & TIOCFLAG_CRTSCTS)
    505 			driverbits |= COM_SW_CRTSCTS;
    506 		if (userbits & TIOCFLAG_MDMBUF)
    507 			driverbits |= COM_SW_MDMBUF;
    508 
    509 		sc->sc_swflags = driverbits;
    510 		break;
    511 	}
    512 	default:
    513 		return ENOTTY;
    514 	}
    515 
    516 	return 0;
    517 }
    518 
    519 int
    520 comparam(tp, t)
    521 	struct tty *tp;
    522 	struct termios *t;
    523 {
    524 	struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
    525 	u_short iobase = sc->sc_iobase;
    526 	int ospeed = comspeed(t->c_ospeed);
    527 	u_char cfcr;
    528 	int s;
    529 
    530 	/* check requested parameters */
    531 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    532 		return EINVAL;
    533 
    534 	switch (t->c_cflag & CSIZE) {
    535 	case CS5:
    536 		cfcr = CFCR_5BITS;
    537 		break;
    538 	case CS6:
    539 		cfcr = CFCR_6BITS;
    540 		break;
    541 	case CS7:
    542 		cfcr = CFCR_7BITS;
    543 		break;
    544 	case CS8:
    545 		cfcr = CFCR_8BITS;
    546 		break;
    547 	}
    548 	if (t->c_cflag & PARENB) {
    549 		cfcr |= CFCR_PENAB;
    550 		if ((t->c_cflag & PARODD) == 0)
    551 			cfcr |= CFCR_PEVEN;
    552 	}
    553 	if (t->c_cflag & CSTOPB)
    554 		cfcr |= CFCR_STOPB;
    555 
    556 	s = spltty();
    557 
    558 	/* Set the FIFO threshold based on the receive speed. */
    559 	if (sc->sc_hwflags & COM_HW_FIFO)
    560 		outb(iobase + com_fifo,
    561 		    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    562 		    (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
    563 
    564 	if (ospeed == 0)
    565 		outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_DTR);
    566 	else
    567 		outb(iobase + com_mcr, sc->sc_mcr |= MCR_DTR);
    568 
    569 	outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
    570 	outb(iobase + com_dlbl, ospeed);
    571 	outb(iobase + com_dlbh, ospeed>>8);
    572 	outb(iobase + com_cfcr, cfcr);
    573 
    574 	/* When not using CRTSCTS, RTS follows DTR. */
    575 	if ((t->c_cflag & CRTSCTS) == 0) {
    576 		if (sc->sc_mcr & MCR_DTR) {
    577 			if ((sc->sc_mcr & MCR_RTS) == 0)
    578 				outb(iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
    579 		} else {
    580 			if (sc->sc_mcr & MCR_RTS)
    581 				outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_RTS);
    582 		}
    583 	}
    584 
    585 	/*
    586 	 * If CTS is off and CRTSCTS is changed, we must toggle TS_TTSTOP.
    587 	 * XXX should be done at tty layer.
    588 	 */
    589 	if ((sc->sc_msr & MSR_CTS) == 0 &&
    590 	    (tp->t_cflag & CRTSCTS) != (t->c_cflag & CRTSCTS)) {
    591 		if ((t->c_cflag & CRTSCTS) == 0) {
    592 			tp->t_state &= ~TS_TTSTOP;
    593 			(*linesw[tp->t_line].l_start)(tp);
    594 		} else
    595 			tp->t_state |= TS_TTSTOP;
    596 	}
    597 
    598 	/*
    599 	 * If DCD is off and MDMBUF is changed, we must toggle TS_TTSTOP.
    600 	 * XXX should be done at tty layer.
    601 	 */
    602 	if ((sc->sc_swflags & COM_SW_SOFTCAR) == 0 &&
    603 	    (sc->sc_msr & MSR_DCD) == 0 &&
    604 	    (tp->t_cflag & MDMBUF) != (t->c_cflag & MDMBUF)) {
    605 		if ((t->c_cflag & MDMBUF) == 0) {
    606 			tp->t_state &= ~TS_TTSTOP;
    607 			(*linesw[tp->t_line].l_start)(tp);
    608 		} else
    609 			tp->t_state |= TS_TTSTOP;
    610 	}
    611 
    612 	/* and copy to tty */
    613 	tp->t_ispeed = t->c_ispeed;
    614 	tp->t_ospeed = t->c_ospeed;
    615 	tp->t_cflag = t->c_cflag;
    616 
    617 	splx(s);
    618 	return 0;
    619 }
    620 
    621 void
    622 comstart(tp)
    623 	struct tty *tp;
    624 {
    625 	struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
    626 	u_short iobase = sc->sc_iobase;
    627 	int s;
    628 
    629 	s = spltty();
    630 	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
    631 		goto out;
    632 #if 0 /* XXXX I think this is handled adequately by commint() and comparam(). */
    633 	if (tp->t_cflag & CRTSCTS && (sc->sc_mcr & MSR_CTS) == 0)
    634 		goto out;
    635 #endif
    636 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    637 		if (tp->t_state & TS_ASLEEP) {
    638 			tp->t_state &= ~TS_ASLEEP;
    639 			wakeup((caddr_t)&tp->t_outq);
    640 		}
    641 		selwakeup(&tp->t_wsel);
    642 	}
    643 	if (tp->t_outq.c_cc == 0)
    644 		goto out;
    645 	tp->t_state |= TS_BUSY;
    646 	if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
    647 		goto out;
    648 	if (sc->sc_hwflags & COM_HW_FIFO) {
    649 		u_char buffer[16], *cp = buffer;
    650 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    651 		do {
    652 			outb(iobase + com_data, *cp++);
    653 		} while (--n);
    654 	} else
    655 		outb(iobase + com_data, getc(&tp->t_outq));
    656 out:
    657 	splx(s);
    658 }
    659 
    660 /*
    661  * Stop output on a line.
    662  */
    663 void
    664 comstop(tp, flag)
    665 	struct tty *tp;
    666 {
    667 	int s;
    668 
    669 	s = spltty();
    670 	if (tp->t_state & TS_BUSY)
    671 		if ((tp->t_state & TS_TTSTOP) == 0)
    672 			tp->t_state |= TS_FLUSH;
    673 	splx(s);
    674 }
    675 
    676 static inline void
    677 comeint(sc, stat)
    678 	struct com_softc *sc;
    679 	int stat;
    680 {
    681 	u_short iobase = sc->sc_iobase;
    682 	int unit = sc->sc_dev.dv_unit;
    683 	struct tty *tp = com_tty[unit];
    684 	int c;
    685 
    686 	c = inb(iobase + com_data);
    687 	if ((tp->t_state & TS_ISOPEN) == 0) {
    688 #ifdef KGDB
    689 		/* we don't care about parity errors */
    690 		if (((stat & (LSR_BI | LSR_FE | LSR_PE)) == LSR_PE) &&
    691 		    kgdb_dev == makedev(commajor, unit) && c == FRAME_END)
    692 			kgdb_connect(0); /* trap into kgdb */
    693 #endif
    694 		return;
    695 	}
    696 	if (stat & (LSR_BI | LSR_FE))
    697 		c |= TTY_FE;
    698 	else if (stat & LSR_PE)
    699 		c |= TTY_PE;
    700 	if (stat & LSR_OE) {
    701 		if (sc->sc_overflows++ == 0)
    702 			timeout(comdiag, sc, 60 * hz);
    703 	}
    704 	/* XXXX put in FIFO and process later */
    705 	(*linesw[tp->t_line].l_rint)(c, tp);
    706 }
    707 
    708 static inline void
    709 commint(sc)
    710 	struct com_softc *sc;
    711 {
    712 	u_short iobase = sc->sc_iobase;
    713 	struct tty *tp = com_tty[sc->sc_dev.dv_unit];
    714 	u_char msr, delta;
    715 
    716 	msr = inb(iobase + com_msr);
    717 	delta = msr ^ sc->sc_msr;
    718 	sc->sc_msr = msr;
    719 
    720 	if (delta & MSR_DCD && (sc->sc_swflags & COM_SW_SOFTCAR) == 0) {
    721 		if (msr & MSR_DCD)
    722 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    723 		else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    724 			outb(iobase + com_mcr,
    725 			    sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
    726 	}
    727 	if (delta & MSR_CTS && tp->t_cflag & CRTSCTS) {
    728 		/* the line is up and we want to do rts/cts flow control */
    729 		if (msr & MSR_CTS) {
    730 			tp->t_state &= ~TS_TTSTOP;
    731 			(*linesw[tp->t_line].l_start)(tp);
    732 		} else
    733 			tp->t_state |= TS_TTSTOP;
    734 	}
    735 }
    736 
    737 void
    738 comdiag(arg)
    739 	void *arg;
    740 {
    741 	struct com_softc *sc = arg;
    742 	int overflows;
    743 	int s;
    744 
    745 	s = spltty();
    746 	overflows = sc->sc_overflows;
    747 	sc->sc_overflows = 0;
    748 	splx(s);
    749 
    750 	if (overflows)
    751 		log(LOG_WARNING, "%s: %d silo overflow%s\n",
    752 		    sc->sc_dev.dv_xname, overflows, overflows == 1 ? "" : "s");
    753 }
    754 
    755 int
    756 comintr(sc)
    757 	struct com_softc *sc;
    758 {
    759 	u_short iobase = sc->sc_iobase;
    760 	struct tty *tp;
    761 	u_char code;
    762 
    763 	code = inb(iobase + com_iir) & IIR_IMASK;
    764 	if (code & IIR_NOPEND)
    765 		return 0;
    766 
    767 	for (;;) {
    768 		if (code & IIR_RXRDY) {
    769 			tp = com_tty[sc->sc_dev.dv_unit];
    770 			/* XXXX put in FIFO and process later */
    771 			while (code = (inb(iobase + com_lsr) & LSR_RCV_MASK)) {
    772 				if (code == LSR_RXRDY) {
    773 					code = inb(iobase + com_data);
    774 					if (tp->t_state & TS_ISOPEN)
    775 						(*linesw[tp->t_line].l_rint)(code, tp);
    776 #ifdef KGDB
    777 					else {
    778 						if (kgdb_dev == makedev(commajor, unit) &&
    779 						    code == FRAME_END)
    780 							kgdb_connect(0);
    781 					}
    782 #endif
    783 				} else
    784 					comeint(sc, code);
    785 			}
    786 		} else if (code == IIR_TXRDY) {
    787 			tp = com_tty[sc->sc_dev.dv_unit];
    788 			tp->t_state &= ~TS_BUSY;
    789 			if (tp->t_state & TS_FLUSH)
    790 				tp->t_state &= ~TS_FLUSH;
    791 			else
    792 				if (tp->t_line)
    793 					(*linesw[tp->t_line].l_start)(tp);
    794 				else
    795 					comstart(tp);
    796 		} else if (code == IIR_MLSC) {
    797 			commint(sc);
    798 		} else {
    799 			log(LOG_WARNING, "%s: weird interrupt: iir=0x%02x\n",
    800 			    sc->sc_dev.dv_xname, code);
    801 		}
    802 		code = inb(iobase + com_iir) & IIR_IMASK;
    803 		if (code & IIR_NOPEND)
    804 			return 1;
    805 	}
    806 }
    807 
    808 /*
    809  * Following are all routines needed for COM to act as console
    810  */
    811 #include <dev/cons.h>
    812 
    813 comcnprobe(cp)
    814 	struct consdev *cp;
    815 {
    816 
    817 	if (!comprobe1(CONADDR)) {
    818 		cp->cn_pri = CN_DEAD;
    819 		return;
    820 	}
    821 
    822 	/* locate the major number */
    823 	for (commajor = 0; commajor < nchrdev; commajor++)
    824 		if (cdevsw[commajor].d_open == comopen)
    825 			break;
    826 
    827 	/* initialize required fields */
    828 	cp->cn_dev = makedev(commajor, CONUNIT);
    829 #ifdef	COMCONSOLE
    830 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
    831 #else
    832 	cp->cn_pri = CN_NORMAL;
    833 #endif
    834 }
    835 
    836 comcninit(cp)
    837 	struct consdev *cp;
    838 {
    839 
    840 	cominit(CONUNIT, comdefaultrate);
    841 	comconsole = CONUNIT;
    842 	comconsinit = 0;
    843 }
    844 
    845 cominit(unit, rate)
    846 	int unit, rate;
    847 {
    848 	int s = splhigh();
    849 	u_short iobase = CONADDR;
    850 	u_char stat;
    851 
    852 	outb(iobase + com_cfcr, CFCR_DLAB);
    853 	rate = comspeed(comdefaultrate);
    854 	outb(iobase + com_dlbl, rate);
    855 	outb(iobase + com_dlbh, rate >> 8);
    856 	outb(iobase + com_cfcr, CFCR_8BITS);
    857 	outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
    858 	outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
    859 	stat = inb(iobase + com_iir);
    860 	splx(s);
    861 }
    862 
    863 comcngetc(dev)
    864 	dev_t dev;
    865 {
    866 	int s = splhigh();
    867 	u_short iobase = CONADDR;
    868 	u_char stat, c;
    869 
    870 	while (((stat = inb(iobase + com_lsr)) & LSR_RXRDY) == 0)
    871 		;
    872 	c = inb(iobase + com_data);
    873 	stat = inb(iobase + com_iir);
    874 	splx(s);
    875 	return c;
    876 }
    877 
    878 /*
    879  * Console kernel output character routine.
    880  */
    881 comcnputc(dev, c)
    882 	dev_t dev;
    883 	int c;
    884 {
    885 	int s = splhigh();
    886 	u_short iobase = CONADDR;
    887 	u_char stat;
    888 	register int timo;
    889 
    890 #ifdef KGDB
    891 	if (dev != kgdb_dev)
    892 #endif
    893 	if (comconsinit == 0) {
    894 		(void) cominit(COMUNIT(dev), comdefaultrate);
    895 		comconsinit = 1;
    896 	}
    897 	/* wait for any pending transmission to finish */
    898 	timo = 50000;
    899 	while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
    900 		;
    901 	outb(iobase + com_data, c);
    902 	/* wait for this transmission to complete */
    903 	timo = 1500000;
    904 	while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
    905 		;
    906 	/* clear any interrupts generated by this transmission */
    907 	stat = inb(iobase + com_iir);
    908 	splx(s);
    909 }
    910