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