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