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