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