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