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