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