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