Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.12.2.9
      1 /*-
      2  * Copyright (c) 1993 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.12.2.9 1993/10/15 09:51:56 mycroft Exp $
     36  */
     37 
     38 /*
     39  * COM driver, based originally on HP dca driver
     40  * uses National Semiconductor NS16450/NS16550AF UART
     41  */
     42 #include "param.h"
     43 #include "systm.h"
     44 #include "ioctl.h"
     45 #include "select.h"
     46 #include "tty.h"
     47 #include "proc.h"
     48 #include "user.h"
     49 #include "conf.h"
     50 #include "file.h"
     51 #include "uio.h"
     52 #include "kernel.h"
     53 #include "syslog.h"
     54 #include "types.h"
     55 #include "sys/device.h"
     56 
     57 #include "machine/cpu.h"
     58 #include "machine/pio.h"
     59 
     60 #include "i386/isa/isavar.h"
     61 #include "i386/isa/icu.h"
     62 #include "i386/isa/comreg.h"
     63 #include "i386/isa/ic/ns16550.h"
     64 
     65 struct com_softc {
     66 	struct	device sc_dev;
     67 	struct	isadev sc_id;
     68 	struct	intrhand sc_ih;
     69 
     70 	u_short	sc_iobase;
     71 	u_char	sc_flags;
     72 #define	COM_SOFTCAR	0x01
     73 #define	COM_FIFO	0x02
     74 };
     75 /* XXXX should be in com_softc, but not ready for that yet */
     76 #include "com.h"
     77 struct	tty *com_tty[NCOM];
     78 
     79 int	comdefaultrate = TTYDEF_SPEED;
     80 
     81 static int comprobe __P((struct device *, struct cfdata *, void *));
     82 static void comforceintr __P((void *));
     83 static void comattach __P((struct device *, struct device *, void *));
     84 static int comintr __P((struct com_softc *));
     85 
     86 struct cfdriver comcd =
     87 { NULL, "com", comprobe, comattach, sizeof (struct com_softc) };
     88 
     89 int comparam __P((struct tty *, struct termios *));
     90 void comstart __P((struct tty *));
     91 
     92 int	comconsole = -1;
     93 int	comconsinit;
     94 int	commajor;
     95 extern	struct tty *constty;
     96 
     97 #define	COMUNIT(x)		(minor(x))
     98 
     99 #define	bis(c, b)	do { const register u_short com_ad = (c); \
    100 			     outb(com_ad, inb(com_ad) | (b)); } while(0)
    101 #define	bic(c, b)	do { const register u_short com_ad = (c); \
    102 			     outb(com_ad, inb(com_ad) &~ (b)); } while(0)
    103 
    104 static int
    105 comspeed(speed)
    106 	int speed;
    107 {
    108 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    109 
    110 	int x, err;
    111 
    112 	if (speed == 0)
    113 		return 0;
    114 	if (speed < 0)
    115 		return -1;
    116 	x = divrnd((COM_FREQ / 16), speed);
    117 	if (x <= 0)
    118 		return -1;
    119 	err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
    120 	if (err < 0)
    121 		err = -err;
    122 	if (err > COM_TOLERANCE)
    123 		return -1;
    124 	return x;
    125 
    126 #undef	divrnd(n, q)
    127 }
    128 
    129 static int
    130 _comprobe(iobase)
    131 	u_short iobase;
    132 {
    133 
    134 	/* force access to id reg */
    135 	outb(iobase + com_cfcr, 0);
    136 	outb(iobase + com_iir, 0);
    137 	if (inb(iobase + com_iir) & 0x38)
    138 		return 0;
    139 	outb(iobase + com_ier, 0);
    140 	return 1;
    141 }
    142 
    143 static int
    144 comprobe(parent, cf, aux)
    145 	struct device *parent;
    146 	struct cfdata *cf;
    147 	void *aux;
    148 {
    149 	struct	isa_attach_args *ia = aux;
    150 	u_short	iobase = ia->ia_iobase;
    151 
    152 	if (iobase == IOBASEUNK)
    153 		return 0;
    154 
    155 	if (!_comprobe(iobase))
    156 		return 0;
    157 
    158 	if (ia->ia_irq == IRQUNK) {
    159 		ia->ia_irq = isa_discoverintr(comforceintr, aux);
    160 		if (ia->ia_irq == IRQNONE)
    161 			return 0;
    162 	}
    163 
    164 	/* disable interrupts */
    165 	outb(iobase + com_mcr, 0);
    166 	outb(iobase + com_ier, 0);
    167 
    168 	ia->ia_iosize = COM_NPORTS;
    169 	ia->ia_drq = DRQUNK;
    170 	ia->ia_msize = 0;
    171 	return 1;
    172 }
    173 
    174 static void
    175 comforceintr(aux)
    176 	void *aux;
    177 {
    178 	struct	isa_attach_args *ia = aux;
    179 	u_short	iobase = ia->ia_iobase;
    180 
    181 #if 0
    182 	/*
    183 	 * As usual, the PC compatible world isn't.  We'd like to use the
    184 	 * loopback feature to generate an interrupt, but alas, some lame
    185 	 * clones don't support it.
    186 	 */
    187 	outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK);
    188 	outb(iobase + com_ier, IER_EMSC);
    189 	outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK | MCR_DRS);
    190 	outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK);
    191 #else
    192 	/*
    193 	 * So instead we try to force the transmit buffer empty (though
    194 	 * it probably is already).
    195 	 */
    196 	outb(iobase + com_cfcr, CFCR_8BITS);	/* turn off DLAB */
    197 	outb(iobase + com_ier, 0);
    198 	outb(iobase + com_fifo, 0);
    199 	outb(iobase + com_lsr, LSR_TXRDY | LSR_TSRE);
    200 	outb(iobase + com_mcr, MCR_IENABLE);
    201 	outb(iobase + com_ier, IER_ETXRDY);
    202 #endif
    203 }
    204 
    205 static void
    206 comattach(parent, self, aux)
    207 	struct device *parent, *self;
    208 	void *aux;
    209 {
    210 	struct	com_softc *sc = (struct com_softc *)self;
    211 	struct	isa_attach_args *ia = aux;
    212 	u_short	iobase = ia->ia_iobase;
    213 	struct	tty *tp;
    214 	u_char	unit = sc->sc_dev.dv_unit;
    215 
    216 	if (iobase == comconsole)
    217 		delay(1000);
    218 
    219 	sc->sc_iobase = iobase;
    220 	sc->sc_flags = 0;
    221 
    222 	/* look for a NS 16550AF UART with FIFOs */
    223 	outb(iobase + com_fifo,
    224 	     FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_8);
    225 	delay(100);
    226 	if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK) {
    227 		sc->sc_flags |= COM_FIFO;
    228 		printf(": ns16550\n");
    229 	} else
    230 		printf(": ns8250 or ns16450\n");
    231 	outb(iobase + com_fifo, 0);
    232 	isa_establish(&sc->sc_id, &sc->sc_dev);
    233 
    234 	sc->sc_ih.ih_fun = comintr;
    235 	sc->sc_ih.ih_arg = sc;
    236 	intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    237 
    238 	outb(iobase + com_ier, 0);
    239 	outb(iobase + com_mcr, MCR_IENABLE);
    240 
    241 	/*
    242 	 * Need to reset baud rate, etc. of next print so reset comconsinit.
    243 	 * Also make sure console is always "hardwired"
    244 	 */
    245 	if (iobase == comconsole) {
    246 		constty = com_tty[unit] = ttymalloc();
    247 		comconsinit = 0;
    248 		sc->sc_flags |= COM_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 	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 		/* preserve previous speed, if any */
    283 		if (tp->t_ispeed == 0) {
    284 			tp->t_iflag = TTYDEF_IFLAG;
    285 			tp->t_oflag = TTYDEF_OFLAG;
    286 			tp->t_cflag = TTYDEF_CFLAG;
    287 			tp->t_lflag = TTYDEF_LFLAG;
    288 			tp->t_ispeed = tp->t_ospeed = comdefaultrate;
    289 		}
    290 		comparam(tp, &tp->t_termios);
    291 		ttsetwater(tp);
    292 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
    293 		return EBUSY;
    294 
    295 	s = spltty();
    296 	iobase = sc->sc_iobase;
    297 	/* flush any pending I/O */
    298 	if (sc->sc_flags & COM_FIFO)
    299 		outb(iobase + com_fifo,
    300 		     FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_8);
    301 	(void) inb(iobase + com_lsr);
    302 	(void) inb(iobase + com_data);
    303 	/* you turn me on, baby */
    304 	outb(iobase + com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
    305 	outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
    306 
    307 	if (sc->sc_flags & COM_SOFTCAR || inb(iobase + com_msr) & MSR_DCD)
    308 		tp->t_state |= TS_CARR_ON;
    309 	else
    310 		tp->t_state &=~ TS_CARR_ON;
    311 	while ((flag & O_NONBLOCK) == 0 && (tp->t_cflag & CLOCAL) == 0 &&
    312 	       (tp->t_state & TS_CARR_ON) == 0) {
    313 		/* wait for carrier; allow signals */
    314 		tp->t_state |= TS_WOPEN;
    315 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    316 		    ttopen, 0)) {
    317 			splx(s);
    318 			return error;
    319 		}
    320 	}
    321 	splx(s);
    322 
    323 	return (*linesw[tp->t_line].l_open)(dev, tp);
    324 }
    325 
    326 int
    327 comclose(dev, flag)
    328 	dev_t dev;
    329 	int flag;
    330 {
    331 	int	unit = COMUNIT(dev);
    332 	struct	com_softc *sc = comcd.cd_devs[unit];
    333 	u_short	iobase = sc->sc_iobase;
    334 	struct	tty *tp = com_tty[unit];
    335 
    336 	(*linesw[tp->t_line].l_close)(tp, flag);
    337 	bic(iobase + com_cfcr, CFCR_SBREAK);
    338 	outb(iobase + com_ier, 0);
    339 	if (tp->t_cflag & HUPCL)
    340 		bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    341 	ttyclose(tp);
    342 #ifdef notyet /* XXXX */
    343 	if (iobase != comconsole) {
    344 		ttyfree(tp);
    345 		com_tty[unit] = (struct tty *)NULL;
    346 	}
    347 #endif
    348 	return 0;
    349 }
    350 
    351 int
    352 comread(dev, uio, flag)
    353 	dev_t dev;
    354 	struct uio *uio;
    355 	int flag;
    356 {
    357 	struct	tty *tp = com_tty[COMUNIT(dev)];
    358 
    359 	return (*linesw[tp->t_line].l_read)(tp, uio, flag);
    360 }
    361 
    362 int
    363 comwrite(dev, uio, flag)
    364 	dev_t dev;
    365 	struct uio *uio;
    366 	int flag;
    367 {
    368 	struct	tty *tp = com_tty[COMUNIT(dev)];
    369 
    370 #if 0
    371 	/* XXXX what is this for? */
    372 	if (constty == tp)
    373 		constty = NULL;
    374 #endif
    375 	return (*linesw[tp->t_line].l_write)(tp, uio, flag);
    376 }
    377 
    378 static u_char
    379 tiocm_xxx2mcr(data)
    380 	int data;
    381 {
    382 	u_char m = 0;
    383 	if (data & TIOCM_DTR)
    384 		m |= MCR_DTR;
    385 	if (data & TIOCM_RTS)
    386 		m |= MCR_RTS;
    387 	return m;
    388 }
    389 
    390 int
    391 comioctl(dev, cmd, data, flag)
    392 	dev_t dev;
    393 	int cmd;
    394 	caddr_t data;
    395 	int flag;
    396 {
    397 	int	unit = COMUNIT(dev);
    398 	struct	com_softc *sc = comcd.cd_devs[unit];
    399 	u_short	iobase = sc->sc_iobase;
    400 	struct	tty *tp = com_tty[unit];
    401 	int	error;
    402 
    403 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
    404 	if (error >= 0)
    405 		return error;
    406 	error = ttioctl(tp, cmd, data, flag);
    407 	if (error >= 0)
    408 		return error;
    409 
    410 	switch (cmd) {
    411 	    case TIOCSBRK:
    412 		bis(iobase + com_cfcr, CFCR_SBREAK);
    413 		break;
    414 	    case TIOCCBRK:
    415 		bic(iobase + com_cfcr, CFCR_SBREAK);
    416 		break;
    417 	    case TIOCSDTR:
    418 		bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
    419 		break;
    420 	    case TIOCCDTR:
    421 		bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    422 		break;
    423 	    case TIOCMSET:
    424 		outb(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data) | MCR_IENABLE);
    425 		break;
    426 	    case TIOCMBIS:
    427 		bis(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
    428 		break;
    429 	    case TIOCMBIC:
    430 		bic(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
    431 		break;
    432 	    case TIOCMGET:
    433 		{
    434 			u_char	m = inb(iobase + com_mcr);
    435 			int	bits = 0;
    436 
    437 			m = inb(iobase + com_mcr);
    438 			if (m & MCR_DTR)
    439 				bits |= TIOCM_DTR;
    440 			if (m & MCR_RTS)
    441 				bits |= TIOCM_RTS;
    442 			m = inb(iobase + com_msr);
    443 			if (m & MSR_DCD)
    444 				bits |= TIOCM_CD;
    445 			if (m & MSR_CTS)
    446 				bits |= TIOCM_CTS;
    447 			if (m & MSR_DSR)
    448 				bits |= TIOCM_DSR;
    449 			if (m & (MSR_RI | MSR_TERI))
    450 				bits |= TIOCM_RI;
    451 			if (inb(iobase + com_ier))
    452 				bits |= TIOCM_LE;
    453 			*(int *)data = bits;
    454 			break;
    455 		}
    456 		break;
    457 	    default:
    458 		return ENOTTY;
    459 	}
    460 
    461 	return 0;
    462 }
    463 
    464 int
    465 comparam(tp, t)
    466 	struct tty *tp;
    467 	struct termios *t;
    468 {
    469 	int	unit = COMUNIT(tp->t_dev);
    470 	struct	com_softc *sc = comcd.cd_devs[unit];
    471 	u_short	iobase = sc->sc_iobase;
    472 	int	ospeed = comspeed(t->c_ospeed);
    473 	u_char	cflag = t->c_cflag;
    474 	u_char	cfcr;
    475 	int	s;
    476 
    477 	/* check requested parameters */
    478 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    479 	        return EINVAL;
    480 
    481 	switch (cflag & CSIZE) {
    482 	    case CS5:
    483 		cfcr = CFCR_5BITS;
    484 		break;
    485 	    case CS6:
    486 		cfcr = CFCR_6BITS;
    487 		break;
    488 	    case CS7:
    489 		cfcr = CFCR_7BITS;
    490 		break;
    491 	    case CS8:
    492 		cfcr = CFCR_8BITS;
    493 		break;
    494 	}
    495 	if (cflag & PARENB) {
    496 		cfcr |= CFCR_PENAB;
    497 		if ((cflag & PARODD) == 0)
    498 			cfcr |= CFCR_PEVEN;
    499 	}
    500 	if (cflag & CSTOPB)
    501 		cfcr |= CFCR_STOPB;
    502 
    503 	s = spltty();
    504 
    505 	/* and copy to tty */
    506 	tp->t_ispeed = t->c_ispeed;
    507 	tp->t_ospeed = t->c_ospeed;
    508 	tp->t_cflag = cflag;
    509 
    510 	if (ospeed == 0)
    511 		bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    512 	else
    513 		bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
    514 
    515 	outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
    516 	outb(iobase + com_dlbl, ospeed);
    517 	outb(iobase + com_dlbh, ospeed>>8);
    518 	outb(iobase + com_cfcr, cfcr);
    519 
    520 	/* when not using CRTS_IFLOW, RTS follows DTR */
    521 	if ((cflag & CRTS_IFLOW) == 0) {
    522 		u_char	mcr = inb(iobase + com_mcr);
    523 
    524 		if (mcr & MCR_DTR)
    525 			if ((mcr & MCR_RTS) == 0)
    526 				outb(iobase + com_mcr, mcr | MCR_RTS);
    527 			else
    528 				;
    529 		else
    530 			if (mcr & MCR_RTS)
    531 				outb(iobase + com_mcr, mcr &~ MCR_RTS);
    532 			else
    533 				;
    534 	}
    535 
    536 	splx(s);
    537 
    538 	return 0;
    539 }
    540 
    541 void
    542 comstart(tp)
    543 	struct tty *tp;
    544 {
    545 	int	unit = COMUNIT(tp->t_dev);
    546 	struct	com_softc *sc = comcd.cd_devs[unit];
    547 	u_short	iobase = sc->sc_iobase;
    548 	int	s;
    549 
    550 	s = spltty();
    551 	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
    552 		goto out;
    553 	if (tp->t_cflag & CCTS_OFLOW && (inb(iobase + com_mcr) & MSR_CTS) == 0)
    554 		goto out;
    555 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    556 		if (tp->t_state & TS_ASLEEP) {
    557 			tp->t_state &=~ TS_ASLEEP;
    558 			wakeup((caddr_t)&tp->t_outq);
    559 		}
    560 		selwakeup(&tp->t_wsel);
    561 	}
    562 	if (tp->t_outq.c_cc == 0)
    563 		goto out;
    564 	tp->t_state |= TS_BUSY;
    565 	if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
    566 		goto out;
    567 	if (sc->sc_flags & COM_FIFO) {
    568 		u_char buffer[16], *cp = buffer;
    569 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    570 		do {
    571 			outb(iobase + com_data, *cp++);
    572 		} while (--n);
    573 	} else
    574 		outb(iobase + com_data, getc(&tp->t_outq));
    575     out:
    576 	splx(s);
    577 }
    578 
    579 /*
    580  * Stop output on a line.
    581  */
    582 void
    583 comstop(tp, flag)
    584 	struct tty *tp;
    585 {
    586 	int	s;
    587 
    588 	s = spltty();
    589 	if (tp->t_state & TS_BUSY)
    590 		if ((tp->t_state & TS_TTSTOP) == 0)
    591 			tp->t_state |= TS_FLUSH;
    592 	splx(s);
    593 }
    594 
    595 static void
    596 comeint(sc, stat)
    597 	struct com_softc *sc;
    598 	int stat;
    599 {
    600 	u_short	iobase = sc->sc_iobase;
    601 	struct	tty *tp = com_tty[sc->sc_dev.dv_unit];
    602 	int	c;
    603 
    604 	c = inb(iobase + com_data);
    605 	if ((tp->t_state & TS_ISOPEN) == 0)
    606 		return;
    607 	if (stat & (LSR_BI | LSR_FE))
    608 		c |= TTY_FE;
    609 	else if (stat & LSR_PE)
    610 		c |= TTY_PE;
    611 	else if (stat & LSR_OE) {
    612 		c |= TTY_PE;		/* XXX ought to have its own define */
    613 		log(LOG_WARNING, "com%d: silo overflow\n", sc->sc_dev.dv_unit);
    614 	}
    615 	/* XXXX put in FIFO and process later */
    616 	(*linesw[tp->t_line].l_rint)(c, tp);
    617 }
    618 
    619 static void
    620 commint(sc)
    621 	struct com_softc *sc;
    622 {
    623 	u_short	iobase = sc->sc_iobase;
    624 	struct	tty *tp = com_tty[sc->sc_dev.dv_unit];
    625 	u_char	stat;
    626 
    627 	stat = inb(iobase + com_msr);
    628 	if (stat & MSR_DDCD && (sc->sc_flags & COM_SOFTCAR) == 0) {
    629 		if (stat & MSR_DCD)
    630 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    631 		else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    632 			bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    633 	} else if (stat & MSR_DCTS && tp->t_state & TS_ISOPEN &&
    634 		   tp->t_cflag & CRTSCTS) {
    635 		/* the line is up and we want to do rts/cts flow control */
    636 		if (stat & MSR_CTS) {
    637 			tp->t_state &=~ TS_TTSTOP;
    638 			ttstart(tp);
    639 		} else
    640 			tp->t_state |= TS_TTSTOP;
    641 	}
    642 }
    643 
    644 static int
    645 comintr(sc)
    646 	struct com_softc *sc;
    647 {
    648 	u_short	iobase = sc->sc_iobase;
    649 	struct	tty *tp;
    650 	u_char	code;
    651 
    652 	code = inb(iobase + com_iir);
    653 	if (code & IIR_NOPEND)
    654 		return 0;
    655 
    656 	for (;;) {
    657 		switch (code & IIR_IMASK) {
    658 		    case IIR_RXRDY:
    659 		    case IIR_RXTOUT:
    660 			tp = com_tty[sc->sc_dev.dv_unit];
    661 			/* XXXX put in FIFO and process later */
    662 #define	RCVBYTE() \
    663 			code = inb(iobase + com_data); \
    664 			if (tp->t_state & TS_ISOPEN) \
    665 				(*linesw[tp->t_line].l_rint)(code, tp)
    666 			RCVBYTE();
    667 			if (sc->sc_flags & COM_FIFO)
    668 				while (code = inb(iobase + com_lsr) & LSR_RCV_MASK) {
    669 					if (code == LSR_RXRDY) {
    670 						RCVBYTE();
    671 					} else
    672 						comeint(sc, code);
    673 				}
    674 			break;
    675 		    case IIR_TXRDY:
    676 			tp = com_tty[sc->sc_dev.dv_unit];
    677 			tp->t_state &=~ TS_BUSY;
    678 			if (tp->t_state & TS_FLUSH) {
    679 				tp->t_state &=~ TS_FLUSH;
    680 				wakeup((caddr_t)&tp->t_rawq);
    681 			} else {
    682 				if (tp->t_line)
    683 					(*linesw[tp->t_line].l_start)(tp);
    684 				else
    685 					comstart(tp);
    686 			}
    687 			break;
    688 		    case IIR_RLS:
    689 			comeint(sc, inb(iobase + com_lsr));
    690 			break;
    691 		    default:
    692 			log(LOG_WARNING, "com%d: weird iir=0x%x\n",
    693 			    sc->sc_dev.dv_unit, code);
    694 			/* fall through */
    695 		    case IIR_MLSC:
    696 			commint(sc);
    697 			break;
    698 		}
    699 
    700 		code = inb(iobase + com_iir);
    701 		if (code & IIR_NOPEND)
    702 			return 1;
    703 	}
    704 }
    705 
    706 /* XXXXXXXXXXXXXXXX ---- gremlins below here ---- XXXXXXXXXXXXXXXX */
    707 
    708 #if 0
    709 /*
    710  * Following are all routines needed for COM to act as console
    711  */
    712 #include "i386/i386/cons.h"
    713 
    714 comcnprobe(cp)
    715 	struct consdev *cp;
    716 {
    717 	/* XXXX */
    718 	if (!_comprobe(CONADDR))
    719 		return CN_DEAD;
    720 
    721 	/* locate the major number */
    722 	for (commajor = 0; commajor < nchrdev; commajor++)
    723 		if (cdevsw[commajor].d_open == comopen)
    724 			break;
    725 
    726 	/* initialize required fields */
    727 	cp->cn_dev = makedev(commajor, unit);
    728 #ifdef	COMCONSOLE
    729 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
    730 #else
    731 	cp->cn_pri = CN_NORMAL;
    732 #endif
    733 }
    734 
    735 comcninit(cp)
    736 	struct consdev *cp;
    737 {
    738 	int unit = COMUNIT(cp->cn_dev);
    739 
    740 	comconsole = CONADDR;
    741 	comconsinit = 0;
    742 }
    743 
    744 cominit(unit, rate)
    745 	int unit, rate;
    746 {
    747 	int com;
    748 	int s;
    749 	short stat;
    750 
    751 	com = com_addr[unit];
    752 	s = splhigh();
    753 	outb(com+com_cfcr, CFCR_DLAB);
    754 	rate = COM_BAUDDIV(rate);
    755 	outb(com+com_data, rate & 0xFF);
    756 	outb(com+com_ier, rate >> 8);
    757 	outb(com+com_cfcr, CFCR_8BITS);
    758 	outb(com+com_ier, IER_ERXRDY | IER_ETXRDY);
    759 	outb(com+com_fifo, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4);
    760 	stat = inb(com+com_iir);
    761 	splx(s);
    762 }
    763 
    764 comcngetc(dev)
    765 {
    766 	com = com_addr[COMUNIT(dev)];
    767 	short stat;
    768 	int c, s;
    769 
    770 	s = splhigh();
    771 	while (((stat = inb(com+com_lsr)) & LSR_RXRDY) == 0)
    772 		;
    773 	c = inb(com+com_data);
    774 	stat = inb(com+com_iir);
    775 	splx(s);
    776 	return c;
    777 }
    778 
    779 /*
    780  * Console kernel output character routine.
    781  */
    782 comcnputc(dev, c)
    783 	dev_t dev;
    784 	int c;
    785 {
    786 	com = com_addr[COMUNIT(dev)];
    787 	int timo;
    788 	short stat;
    789 	int s = splhigh();
    790 
    791 	if (comconsinit == 0) {
    792 		(void) cominit(COMUNIT(dev), comdefaultrate);
    793 		comconsinit = 1;
    794 	}
    795 	/* wait for any pending transmission to finish */
    796 	timo = 50000;
    797 	while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
    798 		;
    799 	outb(com+com_data, c);
    800 	/* wait for this transmission to complete */
    801 	timo = 1500000;
    802 	while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
    803 		;
    804 	/* clear any interrupts generated by this transmission */
    805 	stat = inb(com+com_iir);
    806 	splx(s);
    807 }
    808 #endif
    809