Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.22
      1 /*-
      2  * Copyright (c) 1993, 1994 Charles Hannum.
      3  * Copyright (c) 1991 The Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the University of
     17  *	California, Berkeley and its contributors.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	from: @(#)com.c	7.5 (Berkeley) 5/16/91
     35  *	$Id: com.c,v 1.22 1994/03/12 07:25:16 cgd Exp $
     36  */
     37 
     38 /*
     39  * COM driver, based on HP dca driver
     40  * uses National Semiconductor NS16450/NS16550AF UART
     41  */
     42 #include "com.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/select.h>
     48 #include <sys/tty.h>
     49 #include <sys/proc.h>
     50 #include <sys/user.h>
     51 #include <sys/conf.h>
     52 #include <sys/file.h>
     53 #include <sys/uio.h>
     54 #include <sys/kernel.h>
     55 #include <sys/syslog.h>
     56 #include <sys/types.h>
     57 #include <sys/device.h>
     58 
     59 #include <machine/cpu.h>
     60 #include <machine/pio.h>
     61 
     62 #include <i386/isa/isa_device.h>
     63 #include <i386/isa/comreg.h>
     64 #include <i386/isa/ic/ns16550.h>
     65 
     66 struct com_softc {
     67 	struct device sc_dev;
     68 
     69 	u_short sc_iobase;
     70 	u_char sc_hwflags;
     71 #define	COM_HW_MULTI	0x01
     72 #define	COM_HW_CONFIGBITS	COM_HW_MULTI
     73 #define	COM_HW_FIFO	0x02
     74 #define	COM_HW_CONSOLE	0x40
     75 	u_char sc_swflags;
     76 #define	COM_SW_SOFTCAR	0x01
     77 #define	COM_SW_CLOCAL	0x02
     78 #define	COM_SW_CRTSCTS	0x04
     79 	u_char sc_msr, sc_mcr;
     80 } com_softc[NCOM];
     81 /* XXXX should be in com_softc, but not ready for that yet */
     82 struct	tty *com_tty[NCOM];
     83 
     84 int comprobe __P((struct isa_device *));
     85 int comattach __P((struct isa_device *));
     86 int comopen __P((dev_t, int, int, struct proc *));
     87 int comclose __P((dev_t, int, int, struct proc *));
     88 int comintr __P((int));
     89 int comparam __P((struct tty *, struct termios *));
     90 void comstart __P((struct tty *));
     91 
     92 struct	isa_driver comdriver = {
     93 	comprobe, comattach, "com"
     94 };
     95 
     96 int	comdefaultrate = TTYDEF_SPEED;
     97 #ifdef COMCONSOLE
     98 int	comconsole = COMCONSOLE;
     99 #else
    100 int	comconsole = -1;
    101 #endif
    102 int	comconsinit;
    103 int	commajor;
    104 
    105 #ifdef KGDB
    106 #include <machine/remote-sl.h>
    107 extern int kgdb_dev;
    108 extern int kgdb_rate;
    109 extern int kgdb_debug_init;
    110 #endif
    111 
    112 #define	COMUNIT(x)	(minor(x))
    113 
    114 #define	bis(c, b)	do { const register u_short com_ad = (c); \
    115 			     outb(com_ad, inb(com_ad) | (b)); } while(0)
    116 #define	bic(c, b)	do { const register u_short com_ad = (c); \
    117 			     outb(com_ad, inb(com_ad) & ~(b)); } while(0)
    118 
    119 int
    120 comspeed(speed)
    121 	long speed;
    122 {
    123 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    124 
    125 	int x, err;
    126 
    127 	if (speed == 0)
    128 		return 0;
    129 	if (speed < 0)
    130 		return -1;
    131 	x = divrnd((COM_FREQ / 16), speed);
    132 	if (x <= 0)
    133 		return -1;
    134 	err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
    135 	if (err < 0)
    136 		err = -err;
    137 	if (err > COM_TOLERANCE)
    138 		return -1;
    139 	return x;
    140 
    141 #undef	divrnd(n, q)
    142 }
    143 
    144 int
    145 comprobe1(iobase)
    146 	u_short iobase;
    147 {
    148 
    149 	/* force access to id reg */
    150 	outb(iobase + com_cfcr, 0);
    151 	outb(iobase + com_iir, 0);
    152 	if (inb(iobase + com_iir) & 0x38)
    153 		return 0;
    154 
    155 	return 1;
    156 }
    157 
    158 int
    159 comprobe(isa_dev)
    160 	struct isa_device *isa_dev;
    161 {
    162 	struct com_softc *sc = &com_softc[isa_dev->id_unit];
    163 	u_short iobase = isa_dev->id_iobase;
    164 
    165 	/* XXX HACK */
    166 	sprintf(sc->sc_dev.dv_xname, "%s%d", comdriver.name, isa_dev->id_unit);
    167 	sc->sc_dev.dv_unit = isa_dev->id_unit;
    168 
    169 	if (!comprobe1(iobase))
    170 		return 0;
    171 
    172 	return COM_NPORTS;
    173 }
    174 
    175 int
    176 comattach(isa_dev)
    177 	struct isa_device *isa_dev;
    178 {
    179 	int unit = isa_dev->id_unit;
    180 	struct com_softc *sc = &com_softc[unit];
    181 	u_short iobase = isa_dev->id_iobase;
    182 	struct tty *tp;
    183 
    184 	if (unit == comconsole)
    185 		delay(1000);
    186 
    187 	sc->sc_iobase = iobase;
    188 	sc->sc_hwflags = 0;
    189 	sc->sc_swflags = 0;
    190 
    191 	printf("%s: ", sc->sc_dev.dv_xname);
    192 
    193 	/* look for a NS 16550AF UART with FIFOs */
    194 	outb(iobase + com_fifo,
    195 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    196 	delay(100);
    197 	if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK)
    198 		if ((inb(iobase + com_fifo) & FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
    199 			sc->sc_hwflags |= COM_HW_FIFO;
    200 			printf("ns16550a, working fifo\n");
    201 		} else
    202 			printf("ns82550 or ns16550, broken fifo\n");
    203 	else
    204 		printf("ns82450 or ns16450, no fifo\n");
    205 	outb(iobase + com_fifo, 0);
    206 
    207 	/* disable interrupts */
    208 	outb(iobase + com_ier, 0);
    209 	outb(iobase + com_mcr, 0);
    210 
    211 #ifdef KGDB
    212 	if (kgdb_dev == makedev(commajor, unit)) {
    213 		if (comconsole == unit)
    214 			kgdb_dev = -1;	/* can't debug over console port */
    215 		else {
    216 			(void) cominit(unit, kgdb_rate);
    217 			if (kgdb_debug_init) {
    218 				/*
    219 				 * Print prefix of device name,
    220 				 * let kgdb_connect print the rest.
    221 				 */
    222 				printf("%s: ", sc->sc_dev.dv_xname);
    223 				kgdb_connect(1);
    224 			} else
    225 				printf("%s: kgdb enabled\n",
    226 				    sc->sc_dev.dv_xname);
    227 		}
    228 	}
    229 #endif
    230 
    231 	/*
    232 	 * Need to reset baud rate, etc. of next print so reset comconsinit.
    233 	 * Also make sure console is always "hardwired".
    234 	 */
    235 	if (unit == comconsole) {
    236 		comconsinit = 0;
    237 		sc->sc_hwflags |= COM_HW_CONSOLE;
    238 		sc->sc_swflags |= COM_SW_SOFTCAR;
    239 	}
    240 }
    241 
    242 int
    243 comopen(dev, flag, mode, p)
    244 	dev_t dev;
    245 	int flag, mode;
    246 	struct proc *p;
    247 {
    248 	int unit = COMUNIT(dev);
    249 	struct com_softc *sc;
    250 	u_short iobase;
    251 	struct tty *tp;
    252 	int s;
    253 	int error = 0;
    254 
    255 	if (unit > NCOM)
    256 		return ENXIO;
    257 	sc = &com_softc[unit];
    258 	if (!sc->sc_iobase)
    259 		return ENXIO;
    260 
    261 	s = spltty();
    262 
    263 	if (!com_tty[unit])
    264 		tp = com_tty[unit] = ttymalloc();
    265 	else
    266 		tp = com_tty[unit];
    267 
    268 	tp->t_oproc = comstart;
    269 	tp->t_param = comparam;
    270 	tp->t_dev = dev;
    271 	if ((tp->t_state & TS_ISOPEN) == 0) {
    272 		tp->t_state |= TS_WOPEN;
    273 		ttychars(tp);
    274 		tp->t_iflag = TTYDEF_IFLAG;
    275 		tp->t_oflag = TTYDEF_OFLAG;
    276 		tp->t_cflag = TTYDEF_CFLAG;
    277 		if (sc->sc_swflags & COM_SW_CLOCAL)
    278 			tp->t_cflag |= CLOCAL;
    279 		if (sc->sc_swflags & COM_SW_CRTSCTS)
    280 			tp->t_cflag |= CRTSCTS;
    281 		tp->t_lflag = TTYDEF_LFLAG;
    282 		tp->t_ispeed = tp->t_ospeed = comdefaultrate;
    283 		comparam(tp, &tp->t_termios);
    284 		ttsetwater(tp);
    285 
    286 		iobase = sc->sc_iobase;
    287 		/* flush any pending I/O */
    288 		if (sc->sc_hwflags & COM_HW_FIFO)
    289 			outb(iobase + com_fifo,
    290 			    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    291 			    FIFO_TRIGGER_8);
    292 		(void) inb(iobase + com_lsr);
    293 		(void) inb(iobase + com_data);
    294 		/* you turn me on, baby */
    295 		outb(iobase + com_mcr,
    296 		    sc->sc_mcr = MCR_DTR | MCR_RTS | MCR_IENABLE);
    297 		outb(iobase + com_ier,
    298 		    IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
    299 
    300 		sc->sc_msr = inb(iobase + com_msr);
    301 		if (sc->sc_swflags & COM_SW_SOFTCAR || sc->sc_msr & MSR_DCD)
    302 			tp->t_state |= TS_CARR_ON;
    303 		else
    304 			tp->t_state &= ~TS_CARR_ON;
    305 	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    306 		splx(s);
    307 		return EBUSY;
    308 	}
    309 
    310 	/* wait for carrier if necessary */
    311 	if ((flag & O_NONBLOCK) == 0)
    312 		while ((tp->t_cflag & CLOCAL) == 0 &&
    313 		    (tp->t_state & TS_CARR_ON) == 0) {
    314 			tp->t_state |= TS_WOPEN;
    315 			error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    316 			    TTIPRI | PCATCH, ttopen, 0);
    317 			if (error) {
    318 				/* XXX should turn off chip if we're the
    319 				   only waiter */
    320 				splx(s);
    321 				return error;
    322 			}
    323 		}
    324 	splx(s);
    325 
    326 	return (*linesw[tp->t_line].l_open)(dev, tp);
    327 }
    328 
    329 int
    330 comclose(dev, flag, mode, p)
    331 	dev_t dev;
    332 	int flag, mode;
    333 	struct proc *p;
    334 {
    335 	int unit = COMUNIT(dev);
    336 	struct com_softc *sc = &com_softc[unit];
    337 	u_short iobase = sc->sc_iobase;
    338 	struct tty *tp = com_tty[unit];
    339 
    340 	(*linesw[tp->t_line].l_close)(tp, flag);
    341 #ifdef KGDB
    342 	/* do not disable interrupts if debugging */
    343 	if (kgdb_dev != makedev(commajor, unit))
    344 #endif
    345 	{
    346 		bic(iobase + com_cfcr, CFCR_SBREAK);
    347 		outb(iobase + com_ier, 0);
    348 		if (tp->t_cflag & HUPCL &&
    349 		    (sc->sc_swflags & COM_SW_SOFTCAR) == 0)
    350 			/* XXX perhaps only clear DTR */
    351 			outb(iobase + com_mcr, 0);
    352 	}
    353 	ttyclose(tp);
    354 #ifdef notyet /* XXXX */
    355 	if (unit != comconsole) {
    356 		ttyfree(tp);
    357 		com_tty[unit] = (struct tty *)NULL;
    358 	}
    359 #endif
    360 	return 0;
    361 }
    362 
    363 int
    364 comread(dev, uio, flag)
    365 	dev_t dev;
    366 	struct uio *uio;
    367 	int flag;
    368 {
    369 	struct tty *tp = com_tty[COMUNIT(dev)];
    370 
    371 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    372 }
    373 
    374 int
    375 comwrite(dev, uio, flag)
    376 	dev_t dev;
    377 	struct uio *uio;
    378 	int flag;
    379 {
    380 	struct tty *tp = com_tty[COMUNIT(dev)];
    381 
    382 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    383 }
    384 
    385 static u_char
    386 tiocm_xxx2mcr(data)
    387 	int data;
    388 {
    389 	u_char m = 0;
    390 
    391 	if (data & TIOCM_DTR)
    392 		m |= MCR_DTR;
    393 	if (data & TIOCM_RTS)
    394 		m |= MCR_RTS;
    395 	return m;
    396 }
    397 
    398 int
    399 comioctl(dev, cmd, data, flag, p)
    400 	dev_t dev;
    401 	int cmd;
    402 	caddr_t data;
    403 	int flag;
    404 	struct proc *p;
    405 {
    406 	int unit = COMUNIT(dev);
    407 	struct com_softc *sc = &com_softc[unit];
    408 	u_short iobase = sc->sc_iobase;
    409 	struct tty *tp = com_tty[unit];
    410 	int error;
    411 
    412 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    413 	if (error >= 0)
    414 		return error;
    415 	error = ttioctl(tp, cmd, data, flag, p);
    416 	if (error >= 0)
    417 		return error;
    418 
    419 	switch (cmd) {
    420 	case TIOCSBRK:
    421 		bis(iobase + com_cfcr, CFCR_SBREAK);
    422 		break;
    423 	case TIOCCBRK:
    424 		bic(iobase + com_cfcr, CFCR_SBREAK);
    425 		break;
    426 	case TIOCSDTR:
    427 		outb(iobase + com_mcr, sc->sc_mcr |= (MCR_DTR | MCR_RTS));
    428 		break;
    429 	case TIOCCDTR:
    430 		outb(iobase + com_mcr, sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
    431 		break;
    432 	case TIOCMSET:
    433 		sc->sc_mcr &= ~(MCR_DTR | MCR_RTS);
    434 	case TIOCMBIS:
    435 		outb(iobase + com_mcr,
    436 		    sc->sc_mcr |= tiocm_xxx2mcr(*(int *)data));
    437 		break;
    438 	case TIOCMBIC:
    439 		outb(iobase + com_mcr,
    440 		    sc->sc_mcr &= ~tiocm_xxx2mcr(*(int *)data));
    441 		break;
    442 	case TIOCMGET: {
    443 		u_char m;
    444 		int bits = 0;
    445 
    446 		m = sc->sc_mcr;
    447 		if (m & MCR_DTR)
    448 			bits |= TIOCM_DTR;
    449 		if (m & MCR_RTS)
    450 			bits |= TIOCM_RTS;
    451 		m = sc->sc_msr;
    452 		if (m & MSR_DCD)
    453 			bits |= TIOCM_CD;
    454 		if (m & MSR_CTS)
    455 			bits |= TIOCM_CTS;
    456 		if (m & MSR_DSR)
    457 			bits |= TIOCM_DSR;
    458 		if (m & (MSR_RI | MSR_TERI))
    459 			bits |= TIOCM_RI;
    460 		if (inb(iobase + com_ier))
    461 			bits |= TIOCM_LE;
    462 		*(int *)data = bits;
    463 		break;
    464 	}
    465 	case TIOCGFLAGS: {
    466 		int bits = 0;
    467 
    468 		if (sc->sc_swflags & COM_SW_SOFTCAR)
    469 			bits |= TIOCFLAG_SOFTCAR;
    470 		if (sc->sc_swflags & COM_SW_CLOCAL)
    471 			bits |= TIOCFLAG_CLOCAL;
    472 		if (sc->sc_swflags & COM_SW_CRTSCTS)
    473 			bits |= TIOCFLAG_CRTSCTS;
    474 
    475 		*(int *)data = bits;
    476 		break;
    477 	}
    478 	case TIOCSFLAGS: {
    479 		int userbits, driverbits = 0;
    480 
    481                 error = suser(p->p_ucred, &p->p_acflag);
    482                 if (error != 0)
    483                         return(EPERM);
    484 
    485 		userbits = *(int *)data;
    486 		if ((userbits & TIOCFLAG_CLOCAL) ||
    487 		    (sc->sc_hwflags & COM_HW_CONSOLE))
    488 			driverbits |= COM_SW_CLOCAL;
    489 		if (userbits & TIOCFLAG_CLOCAL)
    490 			driverbits |= COM_SW_CLOCAL;
    491 		if (userbits & TIOCFLAG_CRTSCTS)
    492 			driverbits |= COM_SW_CRTSCTS;
    493 
    494 		break;
    495 	}
    496 	default:
    497 		return ENOTTY;
    498 	}
    499 
    500 	return 0;
    501 }
    502 
    503 int
    504 comparam(tp, t)
    505 	struct tty *tp;
    506 	struct termios *t;
    507 {
    508 	struct com_softc *sc = &com_softc[COMUNIT(tp->t_dev)];
    509 	u_short iobase = sc->sc_iobase;
    510 	int ospeed = comspeed(t->c_ospeed);
    511 	u_char cfcr;
    512 	int s;
    513 
    514 	/* check requested parameters */
    515 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    516 		return EINVAL;
    517 
    518 	switch (t->c_cflag & CSIZE) {
    519 	case CS5:
    520 		cfcr = CFCR_5BITS;
    521 		break;
    522 	case CS6:
    523 		cfcr = CFCR_6BITS;
    524 		break;
    525 	case CS7:
    526 		cfcr = CFCR_7BITS;
    527 		break;
    528 	case CS8:
    529 		cfcr = CFCR_8BITS;
    530 		break;
    531 	}
    532 	if (t->c_cflag & PARENB) {
    533 		cfcr |= CFCR_PENAB;
    534 		if ((t->c_cflag & PARODD) == 0)
    535 			cfcr |= CFCR_PEVEN;
    536 	}
    537 	if (t->c_cflag & CSTOPB)
    538 		cfcr |= CFCR_STOPB;
    539 
    540 	s = spltty();
    541 
    542 	/* and copy to tty */
    543 	tp->t_ispeed = t->c_ispeed;
    544 	tp->t_ospeed = t->c_ospeed;
    545 	tp->t_cflag = t->c_cflag;
    546 
    547 	if (ospeed == 0)
    548 		outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_DTR);
    549 	else
    550 		outb(iobase + com_mcr, sc->sc_mcr |= MCR_DTR);
    551 
    552 	outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
    553 	outb(iobase + com_dlbl, ospeed);
    554 	outb(iobase + com_dlbh, ospeed>>8);
    555 	outb(iobase + com_cfcr, cfcr);
    556 
    557 	/* When not using CRTSCTS, RTS follows DTR. */
    558 	if ((t->c_cflag & CRTSCTS) == 0) {
    559 		if (sc->sc_mcr & MCR_DTR) {
    560 			if ((sc->sc_mcr & MCR_RTS) == 0)
    561 				outb(iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
    562 		} else {
    563 			if (sc->sc_mcr & MCR_RTS)
    564 				outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_RTS);
    565 		}
    566 	}
    567 
    568 	/* If CTS is off and CRTSCTS is changed, we must toggle TS_TTSTOP. */
    569 	if ((sc->sc_msr & MSR_CTS) == 0 &&
    570 	    (tp->t_cflag & CRTSCTS) != (t->c_cflag & CRTSCTS)) {
    571 		if ((t->c_cflag & CRTSCTS) == 0) {
    572 			tp->t_state &= ~TS_TTSTOP;
    573 			ttstart(tp);
    574 		} else
    575 			tp->t_state |= TS_TTSTOP;
    576 	}
    577 
    578 	splx(s);
    579 	return 0;
    580 }
    581 
    582 void
    583 comstart(tp)
    584 	struct tty *tp;
    585 {
    586 	struct com_softc *sc = &com_softc[COMUNIT(tp->t_dev)];
    587 	u_short iobase = sc->sc_iobase;
    588 	int s;
    589 
    590 	s = spltty();
    591 	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
    592 		goto out;
    593 #if 0 /* XXXX I think this is handled adequately by commint() and comparam(). */
    594 	if (tp->t_cflag & CRTSCTS && (sc->sc_mcr & MSR_CTS) == 0)
    595 		goto out;
    596 #endif
    597 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    598 		if (tp->t_state & TS_ASLEEP) {
    599 			tp->t_state &= ~TS_ASLEEP;
    600 			wakeup((caddr_t)&tp->t_outq);
    601 		}
    602 		selwakeup(&tp->t_wsel);
    603 	}
    604 	if (tp->t_outq.c_cc == 0)
    605 		goto out;
    606 	tp->t_state |= TS_BUSY;
    607 	if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
    608 		goto out;
    609 	if (sc->sc_hwflags & COM_HW_FIFO) {
    610 		u_char buffer[16], *cp = buffer;
    611 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    612 		do {
    613 			outb(iobase + com_data, *cp++);
    614 		} while (--n);
    615 	} else
    616 		outb(iobase + com_data, getc(&tp->t_outq));
    617 out:
    618 	splx(s);
    619 }
    620 
    621 /*
    622  * Stop output on a line.
    623  */
    624 void
    625 comstop(tp, flag)
    626 	struct tty *tp;
    627 {
    628 	int s;
    629 
    630 	s = spltty();
    631 	if (tp->t_state & TS_BUSY)
    632 		if ((tp->t_state & TS_TTSTOP) == 0)
    633 			tp->t_state |= TS_FLUSH;
    634 	splx(s);
    635 }
    636 
    637 static inline void
    638 comeint(sc, stat)
    639 	struct com_softc *sc;
    640 	int stat;
    641 {
    642 	u_short iobase = sc->sc_iobase;
    643 	int unit = sc->sc_dev.dv_unit;
    644 	struct tty *tp = com_tty[unit];
    645 	int c;
    646 
    647 	c = inb(iobase + com_data);
    648 	if ((tp->t_state & TS_ISOPEN) == 0) {
    649 #ifdef KGDB
    650 		/* we don't care about parity errors */
    651 		if (((stat & (LSR_BI | LSR_FE | LSR_PE)) == LSR_PE) &&
    652 		    kgdb_dev == makedev(commajor, unit) && c == FRAME_END)
    653 			kgdb_connect(0); /* trap into kgdb */
    654 #endif
    655 		return;
    656 	}
    657 	if (stat & (LSR_BI | LSR_FE))
    658 		c |= TTY_FE;
    659 	else if (stat & LSR_PE)
    660 		c |= TTY_PE;
    661 	if (stat & LSR_OE)
    662 		log(LOG_WARNING, "%s: silo overflow\n", sc->sc_dev.dv_xname);
    663 	/* XXXX put in FIFO and process later */
    664 	(*linesw[tp->t_line].l_rint)(c, tp);
    665 }
    666 
    667 static inline void
    668 commint(sc)
    669 	struct com_softc *sc;
    670 {
    671 	u_short iobase = sc->sc_iobase;
    672 	struct tty *tp = com_tty[sc->sc_dev.dv_unit];
    673 	u_char msr, delta;
    674 
    675 	msr = inb(iobase + com_msr);
    676 	delta = msr ^ sc->sc_msr;
    677 	sc->sc_msr = msr;
    678 
    679 	if (delta & MSR_DCD && (sc->sc_swflags & COM_SW_SOFTCAR) == 0) {
    680 		if (msr & MSR_DCD)
    681 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
    682 		else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    683 			outb(iobase + com_mcr,
    684 			    sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
    685 	}
    686 	if (delta & MSR_CTS && tp->t_cflag & CRTSCTS) {
    687 		/* the line is up and we want to do rts/cts flow control */
    688 		if (msr & MSR_CTS) {
    689 			tp->t_state &= ~TS_TTSTOP;
    690 			ttstart(tp);
    691 		} else
    692 			tp->t_state |= TS_TTSTOP;
    693 	}
    694 }
    695 
    696 int
    697 comintr(unit)
    698 	int unit;
    699 {
    700 	struct com_softc *sc = &com_softc[unit];
    701 	u_short iobase = sc->sc_iobase;
    702 	struct tty *tp;
    703 	u_char code;
    704 
    705 	code = inb(iobase + com_iir) & IIR_IMASK;
    706 	if (code & IIR_NOPEND)
    707 		return 0;
    708 
    709 	for (;;) {
    710 		if (code & IIR_RXRDY) {
    711 			tp = com_tty[sc->sc_dev.dv_unit];
    712 			/* XXXX put in FIFO and process later */
    713 			while (code = (inb(iobase + com_lsr) & LSR_RCV_MASK)) {
    714 				if (code == LSR_RXRDY) {
    715 					code = inb(iobase + com_data);
    716 					if (tp->t_state & TS_ISOPEN)
    717 						(*linesw[tp->t_line].l_rint)(code, tp);
    718 #ifdef KGDB
    719 					else {
    720 						if (kgdb_dev == makedev(commajor, unit) &&
    721 						    code == FRAME_END)
    722 							kgdb_connect(0);
    723 					}
    724 #endif
    725 				} else
    726 					comeint(sc, code);
    727 			}
    728 		} else if (code == IIR_TXRDY) {
    729 			tp = com_tty[sc->sc_dev.dv_unit];
    730 			tp->t_state &= ~TS_BUSY;
    731 			if (tp->t_state & TS_FLUSH)
    732 				tp->t_state &= ~TS_FLUSH;
    733 			else
    734 				if (tp->t_line)
    735 					(*linesw[tp->t_line].l_start)(tp);
    736 				else
    737 					comstart(tp);
    738 		} else if (code == IIR_MLSC) {
    739 			commint(sc);
    740 		} else {
    741 			log(LOG_WARNING, "%s: weird interrupt: iir=0x%02x\n",
    742 			    sc->sc_dev.dv_xname, code);
    743 		}
    744 		code = inb(iobase + com_iir) & IIR_IMASK;
    745 		if (code & IIR_NOPEND)
    746 			return 1;
    747 	}
    748 }
    749 
    750 /*
    751  * Following are all routines needed for COM to act as console
    752  */
    753 #include <dev/cons.h>
    754 
    755 comcnprobe(cp)
    756 	struct consdev *cp;
    757 {
    758 	int unit = CONUNIT;
    759 
    760 	if (!comprobe1(CONADDR)) {
    761 		cp->cn_pri = CN_DEAD;
    762 		return;
    763 	}
    764 
    765 	/* locate the major number */
    766 	for (commajor = 0; commajor < nchrdev; commajor++)
    767 		if (cdevsw[commajor].d_open == comopen)
    768 			break;
    769 
    770 	com_softc[unit].sc_iobase = CONADDR;
    771 
    772 	/* initialize required fields */
    773 	cp->cn_dev = makedev(commajor, unit);
    774 #ifdef	COMCONSOLE
    775 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
    776 #else
    777 	cp->cn_pri = CN_NORMAL;
    778 #endif
    779 }
    780 
    781 comcninit(cp)
    782 	struct consdev *cp;
    783 {
    784 	int unit = CONUNIT;
    785 
    786 	cominit(unit, comdefaultrate);
    787 	comconsole = unit;
    788 	comconsinit = 0;
    789 }
    790 
    791 cominit(unit, rate)
    792 	int unit, rate;
    793 {
    794 	int s = splhigh();
    795 	u_short iobase = com_softc[unit].sc_iobase;
    796 	u_char stat;
    797 
    798 	outb(iobase + com_cfcr, CFCR_DLAB);
    799 	rate = comspeed(comdefaultrate);
    800 	outb(iobase + com_dlbl, rate);
    801 	outb(iobase + com_dlbh, rate >> 8);
    802 	outb(iobase + com_cfcr, CFCR_8BITS);
    803 	outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
    804 	outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
    805 	stat = inb(iobase + com_iir);
    806 	splx(s);
    807 }
    808 
    809 comcngetc(dev)
    810 	dev_t dev;
    811 {
    812 	int s = splhigh();
    813 	u_short iobase = com_softc[COMUNIT(dev)].sc_iobase;
    814 	u_char stat, c;
    815 
    816 	while (((stat = inb(iobase + com_lsr)) & LSR_RXRDY) == 0)
    817 		;
    818 	c = inb(iobase + com_data);
    819 	stat = inb(iobase + com_iir);
    820 	splx(s);
    821 	return c;
    822 }
    823 
    824 /*
    825  * Console kernel output character routine.
    826  */
    827 comcnputc(dev, c)
    828 	dev_t dev;
    829 	int c;
    830 {
    831 	int s = splhigh();
    832 	u_short iobase = com_softc[COMUNIT(dev)].sc_iobase;
    833 	u_char stat;
    834 	register int timo;
    835 
    836 #ifdef KGDB
    837 	if (dev != kgdb_dev)
    838 #endif
    839 	if (comconsinit == 0) {
    840 		(void) cominit(COMUNIT(dev), comdefaultrate);
    841 		comconsinit = 1;
    842 	}
    843 	/* wait for any pending transmission to finish */
    844 	timo = 50000;
    845 	while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
    846 		;
    847 	outb(iobase + com_data, c);
    848 	/* wait for this transmission to complete */
    849 	timo = 1500000;
    850 	while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
    851 		;
    852 	/* clear any interrupts generated by this transmission */
    853 	stat = inb(iobase + com_iir);
    854 	splx(s);
    855 }
    856