Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.30
      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.30 1994/04/07 06:50:28 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 	u_short sc_iobase;
     71 	u_char sc_hwflags;
     72 #define	COM_HW_NOIEN	0x01
     73 #define	COM_HW_FIFO	0x02
     74 #define	COM_HW_CONSOLE	0x40
     75 	u_char sc_swflags;
     76 #define	COM_SW_SOFTCAR	0x01
     77 #define	COM_SW_CLOCAL	0x02
     78 #define	COM_SW_CRTSCTS	0x04
     79 #define	COM_SW_MDMBUF	0x08
     80 	u_char sc_msr, sc_mcr;
     81 };
     82 /* XXXX should be in com_softc, but not ready for that yet */
     83 struct	tty *com_tty[NCOM];
     84 
     85 int comprobe();
     86 void comattach();
     87 int comopen __P((dev_t, int, int, struct proc *));
     88 int comclose __P((dev_t, int, int, struct proc *));
     89 int comintr __P((struct com_softc *));
     90 int comparam __P((struct tty *, struct termios *));
     91 void comstart __P((struct tty *));
     92 
     93 struct cfdriver comcd = {
     94 	NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
     95 };
     96 
     97 int	comdefaultrate = TTYDEF_SPEED;
     98 #ifdef COMCONSOLE
     99 int	comconsole = COMCONSOLE;
    100 #else
    101 int	comconsole = -1;
    102 #endif
    103 int	comconsinit;
    104 int	commajor;
    105 
    106 #ifdef KGDB
    107 #include <machine/remote-sl.h>
    108 extern int kgdb_dev;
    109 extern int kgdb_rate;
    110 extern int kgdb_debug_init;
    111 #endif
    112 
    113 #define	COMUNIT(x)	(minor(x))
    114 
    115 #define	bis(c, b)	do { const register u_short com_ad = (c); \
    116 			     outb(com_ad, inb(com_ad) | (b)); } while(0)
    117 #define	bic(c, b)	do { const register u_short com_ad = (c); \
    118 			     outb(com_ad, inb(com_ad) & ~(b)); } while(0)
    119 
    120 int
    121 comspeed(speed)
    122 	long speed;
    123 {
    124 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    125 
    126 	int x, err;
    127 
    128 	if (speed == 0)
    129 		return 0;
    130 	if (speed < 0)
    131 		return -1;
    132 	x = divrnd((COM_FREQ / 16), speed);
    133 	if (x <= 0)
    134 		return -1;
    135 	err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
    136 	if (err < 0)
    137 		err = -err;
    138 	if (err > COM_TOLERANCE)
    139 		return -1;
    140 	return x;
    141 
    142 #undef	divrnd(n, q)
    143 }
    144 
    145 int
    146 comprobe1(iobase)
    147 	u_short iobase;
    148 {
    149 
    150 	/* force access to id reg */
    151 	outb(iobase + com_cfcr, 0);
    152 	outb(iobase + com_iir, 0);
    153 	if (inb(iobase + com_iir) & 0x38)
    154 		return 0;
    155 
    156 	return 1;
    157 }
    158 
    159 int
    160 comprobe(parent, self, aux)
    161 	struct device *parent, *self;
    162 	void *aux;
    163 {
    164 	struct com_softc *sc = (void *)self;
    165 	struct isa_attach_args *ia = aux;
    166 	u_short iobase = ia->ia_iobase;
    167 
    168 	if (!comprobe1(iobase))
    169 		return 0;
    170 
    171 	ia->ia_iosize = COM_NPORTS;
    172 	ia->ia_msize = 0;
    173 	return 1;
    174 }
    175 
    176 void
    177 comattach(parent, self, aux)
    178 	struct device *parent, *self;
    179 	void *aux;
    180 {
    181 	struct com_softc *sc = (void *)self;
    182 	struct isa_attach_args *ia = aux;
    183 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    184 	u_short iobase = ia->ia_iobase;
    185 	struct tty *tp;
    186 
    187 	sc->sc_iobase = iobase;
    188 	sc->sc_hwflags = cf->cf_flags & COM_HW_NOIEN;
    189 	sc->sc_swflags = 0;
    190 
    191 	if (sc->sc_dev.dv_unit == comconsole)
    192 		delay(1000);
    193 
    194 	/* look for a NS 16550AF UART with FIFOs */
    195 	outb(iobase + com_fifo,
    196 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    197 	delay(100);
    198 	if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK)
    199 		if ((inb(iobase + com_fifo) & FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
    200 			sc->sc_hwflags |= COM_HW_FIFO;
    201 			printf(": ns16550a, working fifo\n");
    202 		} else
    203 			printf(": ns82550 or ns16550, broken fifo\n");
    204 	else
    205 		printf(": ns82450 or ns16450, no fifo\n");
    206 	outb(iobase + com_fifo, 0);
    207 
    208 	/* disable interrupts */
    209 	outb(iobase + com_ier, 0);
    210 	outb(iobase + com_mcr, 0);
    211 
    212 	if (!parent) {	/* XXX no master */
    213 		sc->sc_ih.ih_fun = comintr;
    214 		sc->sc_ih.ih_arg = sc;
    215 		sc->sc_ih.ih_level = IPL_TTY;
    216 		intr_establish(ia->ia_irq, &sc->sc_ih);
    217 	}
    218 
    219 #ifdef KGDB
    220 	if (kgdb_dev == makedev(commajor, unit)) {
    221 		if (comconsole == unit)
    222 			kgdb_dev = -1;	/* can't debug over console port */
    223 		else {
    224 			(void) cominit(unit, kgdb_rate);
    225 			if (kgdb_debug_init) {
    226 				/*
    227 				 * Print prefix of device name,
    228 				 * let kgdb_connect print the rest.
    229 				 */
    230 				printf("%s: ", sc->sc_dev.dv_xname);
    231 				kgdb_connect(1);
    232 			} else
    233 				printf("%s: kgdb enabled\n",
    234 				    sc->sc_dev.dv_xname);
    235 		}
    236 	}
    237 #endif
    238 
    239 	if (sc->sc_dev.dv_unit == comconsole) {
    240 		/*
    241 		 * Need to reset baud rate, etc. of next print so reset
    242 		 * comconsinit.  Also make sure console is always "hardwired".
    243 		 */
    244 		comconsinit = 0;
    245 		sc->sc_hwflags |= COM_HW_CONSOLE;
    246 		sc->sc_swflags |= COM_SW_SOFTCAR;
    247 	}
    248 }
    249 
    250 int
    251 comopen(dev, flag, mode, p)
    252 	dev_t dev;
    253 	int flag, mode;
    254 	struct proc *p;
    255 {
    256 	int unit = COMUNIT(dev);
    257 	struct com_softc *sc;
    258 	u_short iobase;
    259 	struct tty *tp;
    260 	int s;
    261 	int error = 0;
    262 
    263 	if (unit >= comcd.cd_ndevs)
    264 		return ENXIO;
    265 	sc = comcd.cd_devs[unit];
    266 	if (!sc)
    267 		return ENXIO;
    268 
    269 	s = spltty();
    270 
    271 	if (!com_tty[unit])
    272 		tp = com_tty[unit] = ttymalloc();
    273 	else
    274 		tp = com_tty[unit];
    275 
    276 	tp->t_oproc = comstart;
    277 	tp->t_param = comparam;
    278 	tp->t_dev = dev;
    279 	if ((tp->t_state & TS_ISOPEN) == 0) {
    280 		tp->t_state |= TS_WOPEN;
    281 		ttychars(tp);
    282 		tp->t_iflag = TTYDEF_IFLAG;
    283 		tp->t_oflag = TTYDEF_OFLAG;
    284 		tp->t_cflag = TTYDEF_CFLAG;
    285 		if (sc->sc_swflags & COM_SW_CLOCAL)
    286 			tp->t_cflag |= CLOCAL;
    287 		if (sc->sc_swflags & COM_SW_CRTSCTS)
    288 			tp->t_cflag |= CRTSCTS;
    289 		if (sc->sc_swflags & COM_SW_MDMBUF)
    290 			tp->t_cflag |= MDMBUF;
    291 		tp->t_lflag = TTYDEF_LFLAG;
    292 		tp->t_ispeed = tp->t_ospeed = comdefaultrate;
    293 		comparam(tp, &tp->t_termios);
    294 		ttsetwater(tp);
    295 
    296 		iobase = sc->sc_iobase;
    297 		/* flush any pending I/O */
    298 		if (sc->sc_hwflags & COM_HW_FIFO)
    299 			outb(iobase + com_fifo,
    300 			    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    301 			    FIFO_TRIGGER_8);
    302 		(void) inb(iobase + com_lsr);
    303 		(void) inb(iobase + com_data);
    304 		/* you turn me on, baby */
    305 		sc->sc_mcr = MCR_DTR | MCR_RTS;
    306 		if (!(sc->sc_hwflags & COM_HW_NOIEN))
    307 			sc->sc_mcr |= MCR_IENABLE;
    308 		outb(iobase + com_mcr, sc->sc_mcr);
    309 		outb(iobase + com_ier,
    310 		    IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
    311 
    312 		sc->sc_msr = inb(iobase + com_msr);
    313 		if (sc->sc_swflags & COM_SW_SOFTCAR || sc->sc_msr & MSR_DCD ||
    314 		    tp->t_lflag&MDMBUF)
    315 			tp->t_state |= TS_CARR_ON;
    316 		else
    317 			tp->t_state &= ~TS_CARR_ON;
    318 	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    319 		splx(s);
    320 		return EBUSY;
    321 	}
    322 
    323 	/* wait for carrier if necessary */
    324 	if ((flag & O_NONBLOCK) == 0)
    325 		while ((tp->t_cflag & CLOCAL) == 0 &&
    326 		    (tp->t_state & TS_CARR_ON) == 0) {
    327 			tp->t_state |= TS_WOPEN;
    328 			error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    329 			    TTIPRI | PCATCH, ttopen, 0);
    330 			if (error) {
    331 				/* XXX should turn off chip if we're the
    332 				   only waiter */
    333 				splx(s);
    334 				return error;
    335 			}
    336 		}
    337 	splx(s);
    338 
    339 	return (*linesw[tp->t_line].l_open)(dev, tp);
    340 }
    341 
    342 int
    343 comclose(dev, flag, mode, p)
    344 	dev_t dev;
    345 	int flag, mode;
    346 	struct proc *p;
    347 {
    348 	int unit = COMUNIT(dev);
    349 	struct com_softc *sc = comcd.cd_devs[unit];
    350 	u_short iobase = sc->sc_iobase;
    351 	struct tty *tp = com_tty[unit];
    352 
    353 	(*linesw[tp->t_line].l_close)(tp, flag);
    354 #ifdef KGDB
    355 	/* do not disable interrupts if debugging */
    356 	if (kgdb_dev != makedev(commajor, unit))
    357 #endif
    358 	{
    359 		bic(iobase + com_cfcr, CFCR_SBREAK);
    360 		outb(iobase + com_ier, 0);
    361 		if (tp->t_cflag & HUPCL &&
    362 		    (sc->sc_swflags & COM_SW_SOFTCAR) == 0)
    363 			/* XXX perhaps only clear DTR */
    364 			outb(iobase + com_mcr, 0);
    365 	}
    366 	ttyclose(tp);
    367 #ifdef notyet /* XXXX */
    368 	if (unit != comconsole) {
    369 		ttyfree(tp);
    370 		com_tty[unit] = (struct tty *)NULL;
    371 	}
    372 #endif
    373 	return 0;
    374 }
    375 
    376 int
    377 comread(dev, uio, flag)
    378 	dev_t dev;
    379 	struct uio *uio;
    380 	int flag;
    381 {
    382 	struct tty *tp = com_tty[COMUNIT(dev)];
    383 
    384 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    385 }
    386 
    387 int
    388 comwrite(dev, uio, flag)
    389 	dev_t dev;
    390 	struct uio *uio;
    391 	int flag;
    392 {
    393 	struct tty *tp = com_tty[COMUNIT(dev)];
    394 
    395 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    396 }
    397 
    398 static u_char
    399 tiocm_xxx2mcr(data)
    400 	int data;
    401 {
    402 	u_char m = 0;
    403 
    404 	if (data & TIOCM_DTR)
    405 		m |= MCR_DTR;
    406 	if (data & TIOCM_RTS)
    407 		m |= MCR_RTS;
    408 	return m;
    409 }
    410 
    411 int
    412 comioctl(dev, cmd, data, flag, p)
    413 	dev_t dev;
    414 	int cmd;
    415 	caddr_t data;
    416 	int flag;
    417 	struct proc *p;
    418 {
    419 	int unit = COMUNIT(dev);
    420 	struct com_softc *sc = comcd.cd_devs[unit];
    421 	u_short iobase = sc->sc_iobase;
    422 	struct tty *tp = com_tty[unit];
    423 	int error;
    424 
    425 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    426 	if (error >= 0)
    427 		return error;
    428 	error = ttioctl(tp, cmd, data, flag, p);
    429 	if (error >= 0)
    430 		return error;
    431 
    432 	switch (cmd) {
    433 	case TIOCSBRK:
    434 		bis(iobase + com_cfcr, CFCR_SBREAK);
    435 		break;
    436 	case TIOCCBRK:
    437 		bic(iobase + com_cfcr, CFCR_SBREAK);
    438 		break;
    439 	case TIOCSDTR:
    440 		outb(iobase + com_mcr, sc->sc_mcr |= (MCR_DTR | MCR_RTS));
    441 		break;
    442 	case TIOCCDTR:
    443 		outb(iobase + com_mcr, sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
    444 		break;
    445 	case TIOCMSET:
    446 		sc->sc_mcr &= ~(MCR_DTR | MCR_RTS);
    447 	case TIOCMBIS:
    448 		outb(iobase + com_mcr,
    449 		    sc->sc_mcr |= tiocm_xxx2mcr(*(int *)data));
    450 		break;
    451 	case TIOCMBIC:
    452 		outb(iobase + com_mcr,
    453 		    sc->sc_mcr &= ~tiocm_xxx2mcr(*(int *)data));
    454 		break;
    455 	case TIOCMGET: {
    456 		u_char m;
    457 		int bits = 0;
    458 
    459 		m = sc->sc_mcr;
    460 		if (m & MCR_DTR)
    461 			bits |= TIOCM_DTR;
    462 		if (m & MCR_RTS)
    463 			bits |= TIOCM_RTS;
    464 		m = sc->sc_msr;
    465 		if (m & MSR_DCD)
    466 			bits |= TIOCM_CD;
    467 		if (m & MSR_CTS)
    468 			bits |= TIOCM_CTS;
    469 		if (m & MSR_DSR)
    470 			bits |= TIOCM_DSR;
    471 		if (m & (MSR_RI | MSR_TERI))
    472 			bits |= TIOCM_RI;
    473 		if (inb(iobase + com_ier))
    474 			bits |= TIOCM_LE;
    475 		*(int *)data = bits;
    476 		break;
    477 	}
    478 	case TIOCGFLAGS: {
    479 		int bits = 0;
    480 
    481 		if (sc->sc_swflags & COM_SW_SOFTCAR)
    482 			bits |= TIOCFLAG_SOFTCAR;
    483 		if (sc->sc_swflags & COM_SW_CLOCAL)
    484 			bits |= TIOCFLAG_CLOCAL;
    485 		if (sc->sc_swflags & COM_SW_CRTSCTS)
    486 			bits |= TIOCFLAG_CRTSCTS;
    487 		if (sc->sc_swflags & COM_SW_MDMBUF)
    488 			bits |= TIOCFLAG_MDMBUF;
    489 
    490 		*(int *)data = bits;
    491 		break;
    492 	}
    493 	case TIOCSFLAGS: {
    494 		int userbits, driverbits = 0;
    495 
    496 		error = suser(p->p_ucred, &p->p_acflag);
    497 		if (error != 0)
    498 			return(EPERM);
    499 
    500 		userbits = *(int *)data;
    501 		if ((userbits & TIOCFLAG_SOFTCAR) ||
    502 		    (sc->sc_hwflags & COM_HW_CONSOLE))
    503 			driverbits |= COM_SW_SOFTCAR;
    504 		if (userbits & TIOCFLAG_CLOCAL)
    505 			driverbits |= COM_SW_CLOCAL;
    506 		if (userbits & TIOCFLAG_CRTSCTS)
    507 			driverbits |= COM_SW_CRTSCTS;
    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 	/* and copy to tty */
    559 	tp->t_ispeed = t->c_ispeed;
    560 	tp->t_ospeed = t->c_ospeed;
    561 	tp->t_cflag = t->c_cflag;
    562 
    563 	if (ospeed == 0)
    564 		outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_DTR);
    565 	else
    566 		outb(iobase + com_mcr, sc->sc_mcr |= MCR_DTR);
    567 
    568 	outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
    569 	outb(iobase + com_dlbl, ospeed);
    570 	outb(iobase + com_dlbh, ospeed>>8);
    571 	outb(iobase + com_cfcr, cfcr);
    572 
    573 	/* When not using CRTSCTS, RTS follows DTR. */
    574 	if ((t->c_cflag & CRTSCTS) == 0) {
    575 		if (sc->sc_mcr & MCR_DTR) {
    576 			if ((sc->sc_mcr & MCR_RTS) == 0)
    577 				outb(iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
    578 		} else {
    579 			if (sc->sc_mcr & MCR_RTS)
    580 				outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_RTS);
    581 		}
    582 	}
    583 
    584 	/*
    585 	 * If CTS is off and CRTSCTS is changed, we must toggle TS_TTSTOP.
    586 	 * XXX should be done at tty layer.
    587 	 */
    588 	if ((sc->sc_msr & MSR_CTS) == 0 &&
    589 	    (tp->t_cflag & CRTSCTS) != (t->c_cflag & CRTSCTS)) {
    590 		if ((t->c_cflag & CRTSCTS) == 0) {
    591 			tp->t_state &= ~TS_TTSTOP;
    592 			ttstart(tp);
    593 		} else
    594 			tp->t_state |= TS_TTSTOP;
    595 	}
    596 
    597 	/*
    598 	 * If DCD is off and MDMBUF is changed, we must toggle TS_TTSTOP.
    599 	 * XXX should be done at tty layer.
    600 	 */
    601 	if ((sc->sc_swflags & COM_SW_SOFTCAR) == 0 &&
    602 	    (sc->sc_msr & MSR_DCD) == 0 &&
    603 	    (tp->t_cflag & MDMBUF) != (t->c_cflag & MDMBUF)) {
    604 		if ((t->c_cflag & MDMBUF) == 0) {
    605 			tp->t_state &= ~TS_TTSTOP;
    606 			ttstart(tp);
    607 		} else
    608 			tp->t_state |= TS_TTSTOP;
    609 	}
    610 
    611 	splx(s);
    612 	return 0;
    613 }
    614 
    615 void
    616 comstart(tp)
    617 	struct tty *tp;
    618 {
    619 	struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
    620 	u_short iobase = sc->sc_iobase;
    621 	int s;
    622 
    623 	s = spltty();
    624 	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
    625 		goto out;
    626 #if 0 /* XXXX I think this is handled adequately by commint() and comparam(). */
    627 	if (tp->t_cflag & CRTSCTS && (sc->sc_mcr & MSR_CTS) == 0)
    628 		goto out;
    629 #endif
    630 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    631 		if (tp->t_state & TS_ASLEEP) {
    632 			tp->t_state &= ~TS_ASLEEP;
    633 			wakeup((caddr_t)&tp->t_outq);
    634 		}
    635 		selwakeup(&tp->t_wsel);
    636 	}
    637 	if (tp->t_outq.c_cc == 0)
    638 		goto out;
    639 	tp->t_state |= TS_BUSY;
    640 	if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
    641 		goto out;
    642 	if (sc->sc_hwflags & COM_HW_FIFO) {
    643 		u_char buffer[16], *cp = buffer;
    644 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    645 		do {
    646 			outb(iobase + com_data, *cp++);
    647 		} while (--n);
    648 	} else
    649 		outb(iobase + com_data, getc(&tp->t_outq));
    650 out:
    651 	splx(s);
    652 }
    653 
    654 /*
    655  * Stop output on a line.
    656  */
    657 void
    658 comstop(tp, flag)
    659 	struct tty *tp;
    660 {
    661 	int s;
    662 
    663 	s = spltty();
    664 	if (tp->t_state & TS_BUSY)
    665 		if ((tp->t_state & TS_TTSTOP) == 0)
    666 			tp->t_state |= TS_FLUSH;
    667 	splx(s);
    668 }
    669 
    670 static inline void
    671 comeint(sc, stat)
    672 	struct com_softc *sc;
    673 	int stat;
    674 {
    675 	u_short iobase = sc->sc_iobase;
    676 	int unit = sc->sc_dev.dv_unit;
    677 	struct tty *tp = com_tty[unit];
    678 	int c;
    679 
    680 	c = inb(iobase + com_data);
    681 	if ((tp->t_state & TS_ISOPEN) == 0) {
    682 #ifdef KGDB
    683 		/* we don't care about parity errors */
    684 		if (((stat & (LSR_BI | LSR_FE | LSR_PE)) == LSR_PE) &&
    685 		    kgdb_dev == makedev(commajor, unit) && c == FRAME_END)
    686 			kgdb_connect(0); /* trap into kgdb */
    687 #endif
    688 		return;
    689 	}
    690 	if (stat & (LSR_BI | LSR_FE))
    691 		c |= TTY_FE;
    692 	else if (stat & LSR_PE)
    693 		c |= TTY_PE;
    694 	if (stat & LSR_OE)
    695 		log(LOG_WARNING, "%s: silo overflow\n", sc->sc_dev.dv_xname);
    696 	/* XXXX put in FIFO and process later */
    697 	(*linesw[tp->t_line].l_rint)(c, tp);
    698 }
    699 
    700 static inline void
    701 commint(sc)
    702 	struct com_softc *sc;
    703 {
    704 	u_short iobase = sc->sc_iobase;
    705 	struct tty *tp = com_tty[sc->sc_dev.dv_unit];
    706 	u_char msr, delta;
    707 
    708 	msr = inb(iobase + com_msr);
    709 	delta = msr ^ sc->sc_msr;
    710 	sc->sc_msr = msr;
    711 
    712 	if (delta & MSR_DCD && (sc->sc_swflags & COM_SW_SOFTCAR) == 0) {
    713 		if (msr & MSR_DCD)
    714 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    715 		else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    716 			outb(iobase + com_mcr,
    717 			    sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
    718 	}
    719 	if (delta & MSR_CTS && tp->t_cflag & CRTSCTS) {
    720 		/* the line is up and we want to do rts/cts flow control */
    721 		if (msr & MSR_CTS) {
    722 			tp->t_state &= ~TS_TTSTOP;
    723 			ttstart(tp);
    724 		} else
    725 			tp->t_state |= TS_TTSTOP;
    726 	}
    727 }
    728 
    729 int
    730 comintr(sc)
    731 	struct com_softc *sc;
    732 {
    733 	u_short iobase = sc->sc_iobase;
    734 	struct tty *tp;
    735 	u_char code;
    736 
    737 	code = inb(iobase + com_iir) & IIR_IMASK;
    738 	if (code & IIR_NOPEND)
    739 		return 0;
    740 
    741 	for (;;) {
    742 		if (code & IIR_RXRDY) {
    743 			tp = com_tty[sc->sc_dev.dv_unit];
    744 			/* XXXX put in FIFO and process later */
    745 			while (code = (inb(iobase + com_lsr) & LSR_RCV_MASK)) {
    746 				if (code == LSR_RXRDY) {
    747 					code = inb(iobase + com_data);
    748 					if (tp->t_state & TS_ISOPEN)
    749 						(*linesw[tp->t_line].l_rint)(code, tp);
    750 #ifdef KGDB
    751 					else {
    752 						if (kgdb_dev == makedev(commajor, unit) &&
    753 						    code == FRAME_END)
    754 							kgdb_connect(0);
    755 					}
    756 #endif
    757 				} else
    758 					comeint(sc, code);
    759 			}
    760 		} else if (code == IIR_TXRDY) {
    761 			tp = com_tty[sc->sc_dev.dv_unit];
    762 			tp->t_state &= ~TS_BUSY;
    763 			if (tp->t_state & TS_FLUSH)
    764 				tp->t_state &= ~TS_FLUSH;
    765 			else
    766 				if (tp->t_line)
    767 					(*linesw[tp->t_line].l_start)(tp);
    768 				else
    769 					comstart(tp);
    770 		} else if (code == IIR_MLSC) {
    771 			commint(sc);
    772 		} else {
    773 			log(LOG_WARNING, "%s: weird interrupt: iir=0x%02x\n",
    774 			    sc->sc_dev.dv_xname, code);
    775 		}
    776 		code = inb(iobase + com_iir) & IIR_IMASK;
    777 		if (code & IIR_NOPEND)
    778 			return 1;
    779 	}
    780 }
    781 
    782 /*
    783  * Following are all routines needed for COM to act as console
    784  */
    785 #include <dev/cons.h>
    786 
    787 comcnprobe(cp)
    788 	struct consdev *cp;
    789 {
    790 
    791 	if (!comprobe1(CONADDR)) {
    792 		cp->cn_pri = CN_DEAD;
    793 		return;
    794 	}
    795 
    796 	/* locate the major number */
    797 	for (commajor = 0; commajor < nchrdev; commajor++)
    798 		if (cdevsw[commajor].d_open == comopen)
    799 			break;
    800 
    801 	/* initialize required fields */
    802 	cp->cn_dev = makedev(commajor, CONUNIT);
    803 #ifdef	COMCONSOLE
    804 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
    805 #else
    806 	cp->cn_pri = CN_NORMAL;
    807 #endif
    808 }
    809 
    810 comcninit(cp)
    811 	struct consdev *cp;
    812 {
    813 
    814 	cominit(CONUNIT, comdefaultrate);
    815 	comconsole = CONUNIT;
    816 	comconsinit = 0;
    817 }
    818 
    819 cominit(unit, rate)
    820 	int unit, rate;
    821 {
    822 	int s = splhigh();
    823 	u_short iobase = CONADDR;
    824 	u_char stat;
    825 
    826 	outb(iobase + com_cfcr, CFCR_DLAB);
    827 	rate = comspeed(comdefaultrate);
    828 	outb(iobase + com_dlbl, rate);
    829 	outb(iobase + com_dlbh, rate >> 8);
    830 	outb(iobase + com_cfcr, CFCR_8BITS);
    831 	outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
    832 	outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
    833 	stat = inb(iobase + com_iir);
    834 	splx(s);
    835 }
    836 
    837 comcngetc(dev)
    838 	dev_t dev;
    839 {
    840 	int s = splhigh();
    841 	u_short iobase = CONADDR;
    842 	u_char stat, c;
    843 
    844 	while (((stat = inb(iobase + com_lsr)) & LSR_RXRDY) == 0)
    845 		;
    846 	c = inb(iobase + com_data);
    847 	stat = inb(iobase + com_iir);
    848 	splx(s);
    849 	return c;
    850 }
    851 
    852 /*
    853  * Console kernel output character routine.
    854  */
    855 comcnputc(dev, c)
    856 	dev_t dev;
    857 	int c;
    858 {
    859 	int s = splhigh();
    860 	u_short iobase = CONADDR;
    861 	u_char stat;
    862 	register int timo;
    863 
    864 #ifdef KGDB
    865 	if (dev != kgdb_dev)
    866 #endif
    867 	if (comconsinit == 0) {
    868 		(void) cominit(COMUNIT(dev), comdefaultrate);
    869 		comconsinit = 1;
    870 	}
    871 	/* wait for any pending transmission to finish */
    872 	timo = 50000;
    873 	while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
    874 		;
    875 	outb(iobase + com_data, c);
    876 	/* wait for this transmission to complete */
    877 	timo = 1500000;
    878 	while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
    879 		;
    880 	/* clear any interrupts generated by this transmission */
    881 	stat = inb(iobase + com_iir);
    882 	splx(s);
    883 }
    884