Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.12.2.7
      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.7 1993/10/12 23:30:58 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((void *));
     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_4);
    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 	isa_establish(&sc->sc_id, &sc->sc_dev);
    232 
    233 	sc->sc_ih.ih_fun = comintr;
    234 	sc->sc_ih.ih_arg = sc;
    235 	intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    236 
    237 	outb(iobase + com_ier, 0);
    238 	outb(iobase + com_mcr, MCR_IENABLE);
    239 
    240 	/*
    241 	 * Need to reset baud rate, etc. of next print so reset comconsinit.
    242 	 * Also make sure console is always "hardwired"
    243 	 */
    244 	if (iobase == comconsole) {
    245 		constty = com_tty[unit] = ttymalloc();
    246 		comconsinit = 0;
    247 		sc->sc_flags |= COM_SOFTCAR;
    248 	}
    249 }
    250 
    251 int
    252 comopen(dev, flag, mode, p)
    253 	dev_t dev;
    254 	int flag, mode;
    255 	struct proc *p;
    256 {
    257 	int	unit = COMUNIT(dev);
    258 	struct	com_softc *sc;
    259 	u_short	iobase;
    260 	struct	tty *tp;
    261 	int	s;
    262 	int	error = 0;
    263 
    264 	if (unit >= comcd.cd_ndevs)
    265 		return ENXIO;
    266 	sc = comcd.cd_devs[unit];
    267 	if (!sc)
    268 		return ENXIO;
    269 
    270 	if (!com_tty[unit])
    271 		tp = com_tty[unit] = ttymalloc();
    272 	else
    273 		tp = com_tty[unit];
    274 
    275 	tp->t_oproc = comstart;
    276 	tp->t_param = comparam;
    277 	tp->t_dev = dev;
    278 	if ((tp->t_state & TS_ISOPEN) == 0) {
    279 		tp->t_state |= TS_WOPEN;
    280 		ttychars(tp);
    281 		/* preserve previous speed, if any */
    282 		if (tp->t_ispeed == 0) {
    283 			tp->t_iflag = TTYDEF_IFLAG;
    284 			tp->t_oflag = TTYDEF_OFLAG;
    285 			tp->t_cflag = TTYDEF_CFLAG;
    286 			tp->t_lflag = TTYDEF_LFLAG;
    287 			tp->t_ispeed = tp->t_ospeed = comdefaultrate;
    288 		}
    289 		comparam(tp, &tp->t_termios);
    290 		ttsetwater(tp);
    291 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
    292 		return EBUSY;
    293 
    294 	s = spltty();
    295 	iobase = sc->sc_iobase;
    296 	outb(iobase + com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
    297 	outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
    298 
    299 	if (sc->sc_flags & COM_SOFTCAR || inb(iobase + com_msr) & MSR_DCD)
    300 		tp->t_state |= TS_CARR_ON;
    301 	else
    302 		tp->t_state &=~ TS_CARR_ON;
    303 	while ((flag & O_NONBLOCK) == 0 && (tp->t_cflag & CLOCAL) == 0 &&
    304 	       (tp->t_state & TS_CARR_ON) == 0) {
    305 		/* wait for carrier; allow signals */
    306 		tp->t_state |= TS_WOPEN;
    307 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    308 		    ttopen, 0)) {
    309 			splx(s);
    310 			return error;
    311 		}
    312 	}
    313 	splx(s);
    314 
    315 	return (*linesw[tp->t_line].l_open)(dev, tp);
    316 }
    317 
    318 int
    319 comclose(dev, flag)
    320 	dev_t dev;
    321 	int flag;
    322 {
    323 	int	unit = COMUNIT(dev);
    324 	struct	com_softc *sc = comcd.cd_devs[unit];
    325 	u_short	iobase = sc->sc_iobase;
    326 	struct	tty *tp = com_tty[unit];
    327 
    328 	(*linesw[tp->t_line].l_close)(tp, flag);
    329 	bic(iobase + com_cfcr, CFCR_SBREAK);
    330 	outb(iobase + com_ier, 0);
    331 	if (tp->t_cflag & HUPCL)
    332 		bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    333 	ttyclose(tp);
    334 #ifdef notyet /* XXXX */
    335 	if (iobase != comconsole) {
    336 		ttyfree(tp);
    337 		com_tty[unit] = (struct tty *)NULL;
    338 	}
    339 #endif
    340 	return 0;
    341 }
    342 
    343 int
    344 comread(dev, uio, flag)
    345 	dev_t dev;
    346 	struct uio *uio;
    347 	int flag;
    348 {
    349 	struct	tty *tp = com_tty[COMUNIT(dev)];
    350 
    351 	return (*linesw[tp->t_line].l_read)(tp, uio, flag);
    352 }
    353 
    354 int
    355 comwrite(dev, uio, flag)
    356 	dev_t dev;
    357 	struct uio *uio;
    358 	int flag;
    359 {
    360 	struct	tty *tp = com_tty[COMUNIT(dev)];
    361 
    362 #if 0
    363 	/* XXXX what is this for? */
    364 	if (constty == tp)
    365 		constty = NULL;
    366 #endif
    367 	return (*linesw[tp->t_line].l_write)(tp, uio, flag);
    368 }
    369 
    370 static u_char
    371 tiocm_xxx2mcr(data)
    372 	int data;
    373 {
    374 	u_char m = 0;
    375 	if (data & TIOCM_DTR)
    376 		m |= MCR_DTR;
    377 	if (data & TIOCM_RTS)
    378 		m |= MCR_RTS;
    379 	return m;
    380 }
    381 
    382 int
    383 comioctl(dev, cmd, data, flag)
    384 	dev_t dev;
    385 	int cmd;
    386 	caddr_t data;
    387 	int flag;
    388 {
    389 	int	unit = COMUNIT(dev);
    390 	struct	com_softc *sc = comcd.cd_devs[unit];
    391 	u_short	iobase = sc->sc_iobase;
    392 	struct	tty *tp = com_tty[unit];
    393 	int	error;
    394 
    395 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
    396 	if (error >= 0)
    397 		return error;
    398 	error = ttioctl(tp, cmd, data, flag);
    399 	if (error >= 0)
    400 		return error;
    401 
    402 	switch (cmd) {
    403 	    case TIOCSBRK:
    404 		bis(iobase + com_cfcr, CFCR_SBREAK);
    405 		break;
    406 	    case TIOCCBRK:
    407 		bic(iobase + com_cfcr, CFCR_SBREAK);
    408 		break;
    409 	    case TIOCSDTR:
    410 		bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
    411 		break;
    412 	    case TIOCCDTR:
    413 		bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    414 		break;
    415 	    case TIOCMSET:
    416 		outb(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data) | MCR_IENABLE);
    417 		break;
    418 	    case TIOCMBIS:
    419 		bis(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
    420 		break;
    421 	    case TIOCMBIC:
    422 		bic(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
    423 		break;
    424 	    case TIOCMGET:
    425 		{
    426 			u_char	m = inb(iobase + com_mcr);
    427 			int	bits = 0;
    428 
    429 			m = inb(iobase + com_mcr);
    430 			if (m & MCR_DTR)
    431 				bits |= TIOCM_DTR;
    432 			if (m & MCR_RTS)
    433 				bits |= TIOCM_RTS;
    434 			m = inb(iobase + com_msr);
    435 			if (m & MSR_CTS)
    436 				bits |= TIOCM_CTS;
    437 			if (m & MSR_DSR)
    438 				bits |= TIOCM_DSR;
    439 			if (m & (MSR_RI | MSR_TERI))
    440 				bits |= TIOCM_RI;
    441 			if (inb(iobase + com_ier))
    442 				bits |= TIOCM_LE;
    443 			*(int *)data = bits;
    444 			break;
    445 		}
    446 		break;
    447 	    default:
    448 		return ENOTTY;
    449 	}
    450 
    451 	return 0;
    452 }
    453 
    454 int
    455 comparam(tp, t)
    456 	struct tty *tp;
    457 	struct termios *t;
    458 {
    459 	int	unit = COMUNIT(tp->t_dev);
    460 	struct	com_softc *sc = comcd.cd_devs[unit];
    461 	u_short	iobase = sc->sc_iobase;
    462 	int	ospeed = comspeed(t->c_ospeed);
    463 	u_char	cflag = t->c_cflag;
    464 	u_char	cfcr;
    465 
    466 	/* check requested parameters */
    467         if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    468                 return EINVAL;
    469 
    470         /* and copy to tty */
    471         tp->t_ispeed = t->c_ispeed;
    472         tp->t_ospeed = t->c_ospeed;
    473         tp->t_cflag = cflag;
    474 
    475 	if (ospeed == 0)
    476 		bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    477 	else
    478 		bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
    479 
    480 	outb(iobase + com_cfcr, CFCR_DLAB);
    481 	outb(iobase + com_dlbl, ospeed & 0xff);
    482 	outb(iobase + com_dlbh, ospeed >> 8);
    483 
    484 	switch (cflag & CSIZE) {
    485 	    case CS5:
    486 		cfcr = CFCR_5BITS;
    487 		break;
    488 	    case CS6:
    489 		cfcr = CFCR_6BITS;
    490 		break;
    491 	    case CS7:
    492 		cfcr = CFCR_7BITS;
    493 		break;
    494 	    case CS8:
    495 		cfcr = CFCR_8BITS;
    496 		break;
    497 	}
    498 	if (cflag & PARENB) {
    499 		cfcr |= CFCR_PENAB;
    500 		if ((cflag & PARODD) == 0)
    501 			cfcr |= CFCR_PEVEN;
    502 	}
    503 	if (cflag & CSTOPB)
    504 		cfcr |= CFCR_STOPB;
    505 	outb(iobase + com_cfcr, cfcr);
    506 
    507 	/* when not using CRTS_IFLOW, RTS follows DTR */
    508 	if ((cflag & CRTS_IFLOW) == 0) {
    509 		u_char	mcr = inb(iobase + com_mcr);
    510 
    511 		if (mcr & MCR_DTR)
    512 			if ((mcr & MCR_RTS) == 0)
    513 				outb(iobase + com_mcr, mcr | MCR_RTS);
    514 			else
    515 				;
    516 		else
    517 			if (mcr & MCR_RTS)
    518 				outb(iobase + com_mcr, mcr &~ MCR_RTS);
    519 			else
    520 				;
    521 	}
    522 
    523 	return 0;
    524 }
    525 
    526 void
    527 comstart(tp)
    528 	struct tty *tp;
    529 {
    530 	int	unit = COMUNIT(tp->t_dev);
    531 	struct	com_softc *sc = comcd.cd_devs[unit];
    532 	u_short	iobase = sc->sc_iobase;
    533 	int	s;
    534 
    535 	s = spltty();
    536 	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
    537 		goto out;
    538 	if (tp->t_cflag & CCTS_OFLOW && (inb(iobase + com_mcr) & MSR_CTS) == 0)
    539 		goto out;
    540 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    541 		if (tp->t_state & TS_ASLEEP) {
    542 			tp->t_state &=~ TS_ASLEEP;
    543 			wakeup((caddr_t)&tp->t_outq);
    544 		}
    545 		selwakeup(&tp->t_wsel);
    546 	}
    547 	if (tp->t_outq.c_cc == 0)
    548 		goto out;
    549 	tp->t_state |= TS_BUSY;
    550 	if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
    551 		goto out;
    552 	if (sc->sc_flags & COM_FIFO) {
    553 		u_char buffer[16], *cp = buffer;
    554 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    555 		do {
    556 			outb(iobase + com_data, *cp++);
    557 		} while (--n);
    558 	} else
    559 		outb(iobase + com_data, getc(&tp->t_outq));
    560     out:
    561 	splx(s);
    562 }
    563 
    564 /*
    565  * Stop output on a line.
    566  */
    567 void
    568 comstop(tp, flag)
    569 	struct tty *tp;
    570 {
    571 	int	s;
    572 
    573 	s = spltty();
    574 	if (tp->t_state & TS_BUSY) {
    575 		if ((tp->t_state & TS_TTSTOP) == 0)
    576 			tp->t_state |= TS_FLUSH;
    577 	}
    578 	splx(s);
    579 }
    580 
    581 static void
    582 comeint(sc, stat)
    583 	struct com_softc *sc;
    584 	int stat;
    585 {
    586 	u_short	iobase = sc->sc_iobase;
    587 	struct	tty *tp = com_tty[sc->sc_dev.dv_unit];
    588 	int	c;
    589 
    590 	c = inb(iobase + com_data);
    591 	if ((tp->t_state & TS_ISOPEN) == 0)
    592 		return;
    593 	if (stat & (LSR_BI | LSR_FE))
    594 		c |= TTY_FE;
    595 	else if (stat & LSR_PE)
    596 		c |= TTY_PE;
    597 	else if (stat & LSR_OE) {
    598 		c |= TTY_PE;		/* XXX ought to have its own define */
    599 		log(LOG_WARNING, "com%d: silo overflow\n", sc->sc_dev.dv_unit);
    600 	}
    601 	/* XXXX put in FIFO and process later */
    602 	(*linesw[tp->t_line].l_rint)(c, tp);
    603 }
    604 
    605 static void
    606 commint(sc)
    607 	struct com_softc *sc;
    608 {
    609 	u_short	iobase = sc->sc_iobase;
    610 	struct	tty *tp = com_tty[sc->sc_dev.dv_unit];
    611 	u_char	stat;
    612 
    613 	stat = inb(iobase + com_msr);
    614 	if (stat & MSR_DDCD && (sc->sc_flags & COM_SOFTCAR) == 0) {
    615 		if (stat & MSR_DCD)
    616 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    617 		else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    618 			bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
    619 	} else if (stat & MSR_DCTS && tp->t_state & TS_ISOPEN &&
    620 		   tp->t_cflag & CRTSCTS) {
    621 		/* the line is up and we want to do rts/cts flow control */
    622 		if (stat & MSR_CTS) {
    623 			tp->t_state &=~ TS_TTSTOP;
    624 			ttstart(tp);
    625 		} else
    626 			tp->t_state |= TS_TTSTOP;
    627 	}
    628 }
    629 
    630 static int
    631 comintr(sc)
    632 	struct com_softc *sc;
    633 {
    634 	u_short	iobase = sc->sc_iobase;
    635 	struct	tty *tp;
    636 	u_char	code;
    637 
    638 	code = inb(iobase + com_iir);
    639 	if (code & IIR_NOPEND)
    640 		return 0;
    641 
    642 	for (;;) {
    643 		switch (code & IIR_IMASK) {
    644 		    case IIR_RXRDY:
    645 		    case IIR_RXTOUT:
    646 			tp = com_tty[sc->sc_dev.dv_unit];
    647 			/* XXXX put in FIFO and process later */
    648 #define	RCVBYTE() \
    649 			code = inb(iobase + com_data); \
    650 			if (tp->t_state & TS_ISOPEN) \
    651 				(*linesw[tp->t_line].l_rint)(code, tp)
    652 			RCVBYTE();
    653 			if (sc->sc_flags & COM_FIFO)
    654 				while (code = inb(iobase + com_lsr) & LSR_RCV_MASK) {
    655 					if (code == LSR_RXRDY) {
    656 						RCVBYTE();
    657 					} else
    658 						comeint(sc, code);
    659 				}
    660 			break;
    661 		    case IIR_TXRDY:
    662 			tp = com_tty[sc->sc_dev.dv_unit];
    663 			tp->t_state &=~ (TS_BUSY | TS_FLUSH);
    664 			if (tp->t_line)
    665 				(*linesw[tp->t_line].l_start)(tp);
    666 			else
    667 				comstart(tp);
    668 			break;
    669 		    case IIR_RLS:
    670 			comeint(sc, inb(iobase + com_lsr));
    671 			break;
    672 		    default:
    673 			log(LOG_WARNING, "com%d: weird iir=0x%x\n",
    674 			    sc->sc_dev.dv_unit, code);
    675 			/* fall through */
    676 		    case IIR_MLSC:
    677 			commint(sc);
    678 			break;
    679 		}
    680 
    681 		code = inb(iobase + com_iir);
    682 		if (code & IIR_NOPEND)
    683 			return 1;
    684 	}
    685 }
    686 
    687 /* XXXXXXXXXXXXXXXX ---- gremlins below here ---- XXXXXXXXXXXXXXXX */
    688 
    689 #if 0
    690 /*
    691  * Following are all routines needed for COM to act as console
    692  */
    693 #include "i386/i386/cons.h"
    694 
    695 comcnprobe(cp)
    696 	struct consdev *cp;
    697 {
    698 	/* XXXX */
    699 	if (!_comprobe(CONADDR))
    700 		return CN_DEAD;
    701 
    702 	/* locate the major number */
    703 	for (commajor = 0; commajor < nchrdev; commajor++)
    704 		if (cdevsw[commajor].d_open == comopen)
    705 			break;
    706 
    707 	/* initialize required fields */
    708 	cp->cn_dev = makedev(commajor, unit);
    709 #ifdef	COMCONSOLE
    710 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
    711 #else
    712 	cp->cn_pri = CN_NORMAL;
    713 #endif
    714 }
    715 
    716 comcninit(cp)
    717 	struct consdev *cp;
    718 {
    719 	int unit = COMUNIT(cp->cn_dev);
    720 
    721 	comconsole = CONADDR;
    722 	comconsinit = 0;
    723 }
    724 
    725 cominit(unit, rate)
    726 	int unit, rate;
    727 {
    728 	int com;
    729 	int s;
    730 	short stat;
    731 
    732 	com = com_addr[unit];
    733 	s = splhigh();
    734 	outb(com+com_cfcr, CFCR_DLAB);
    735 	rate = COM_BAUDDIV(rate);
    736 	outb(com+com_data, rate & 0xFF);
    737 	outb(com+com_ier, rate >> 8);
    738 	outb(com+com_cfcr, CFCR_8BITS);
    739 	outb(com+com_ier, IER_ERXRDY | IER_ETXRDY);
    740 	outb(com+com_fifo, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4);
    741 	stat = inb(com+com_iir);
    742 	splx(s);
    743 }
    744 
    745 comcngetc(dev)
    746 {
    747 	com = com_addr[COMUNIT(dev)];
    748 	short stat;
    749 	int c, s;
    750 
    751 	s = splhigh();
    752 	while (((stat = inb(com+com_lsr)) & LSR_RXRDY) == 0)
    753 		;
    754 	c = inb(com+com_data);
    755 	stat = inb(com+com_iir);
    756 	splx(s);
    757 	return c;
    758 }
    759 
    760 /*
    761  * Console kernel output character routine.
    762  */
    763 comcnputc(dev, c)
    764 	dev_t dev;
    765 	int c;
    766 {
    767 	com = com_addr[COMUNIT(dev)];
    768 	int timo;
    769 	short stat;
    770 	int s = splhigh();
    771 
    772 	if (comconsinit == 0) {
    773 		(void) cominit(COMUNIT(dev), comdefaultrate);
    774 		comconsinit = 1;
    775 	}
    776 	/* wait for any pending transmission to finish */
    777 	timo = 50000;
    778 	while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
    779 		;
    780 	outb(com+com_data, c);
    781 	/* wait for this transmission to complete */
    782 	timo = 1500000;
    783 	while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
    784 		;
    785 	/* clear any interrupts generated by this transmission */
    786 	stat = inb(com+com_iir);
    787 	splx(s);
    788 }
    789 #endif
    790