Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.76
      1  1.76   thorpej /*	$NetBSD: com.c,v 1.76 1996/03/17 00:53:10 thorpej Exp $	*/
      2  1.38       cgd 
      3   1.1       cgd /*-
      4  1.71   mycroft  * Copyright (c) 1993, 1994, 1995, 1996
      5  1.71   mycroft  *	Charles M. Hannum.  All rights reserved.
      6   1.1       cgd  * Copyright (c) 1991 The Regents of the University of California.
      7   1.1       cgd  * All rights reserved.
      8   1.1       cgd  *
      9   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     10   1.1       cgd  * modification, are permitted provided that the following conditions
     11   1.1       cgd  * are met:
     12   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     14   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     16   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     17   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     18   1.1       cgd  *    must display the following acknowledgement:
     19   1.1       cgd  *	This product includes software developed by the University of
     20   1.1       cgd  *	California, Berkeley and its contributors.
     21   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     22   1.1       cgd  *    may be used to endorse or promote products derived from this software
     23   1.1       cgd  *    without specific prior written permission.
     24   1.1       cgd  *
     25   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1       cgd  * SUCH DAMAGE.
     36   1.1       cgd  *
     37  1.38       cgd  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     38   1.1       cgd  */
     39   1.1       cgd 
     40   1.1       cgd /*
     41   1.1       cgd  * COM driver, based on HP dca driver
     42   1.1       cgd  * uses National Semiconductor NS16450/NS16550AF UART
     43   1.1       cgd  */
     44  1.14   mycroft #include <sys/param.h>
     45  1.14   mycroft #include <sys/systm.h>
     46  1.14   mycroft #include <sys/ioctl.h>
     47  1.14   mycroft #include <sys/select.h>
     48  1.14   mycroft #include <sys/tty.h>
     49  1.14   mycroft #include <sys/proc.h>
     50  1.14   mycroft #include <sys/user.h>
     51  1.14   mycroft #include <sys/conf.h>
     52  1.14   mycroft #include <sys/file.h>
     53  1.14   mycroft #include <sys/uio.h>
     54  1.14   mycroft #include <sys/kernel.h>
     55  1.14   mycroft #include <sys/syslog.h>
     56  1.14   mycroft #include <sys/types.h>
     57  1.21   mycroft #include <sys/device.h>
     58  1.14   mycroft 
     59  1.21   mycroft #include <machine/cpu.h>
     60  1.75       cgd #include <machine/bus.h>
     61  1.14   mycroft 
     62  1.49       cgd #include <dev/isa/isavar.h>
     63  1.47       cgd #include <dev/isa/comreg.h>
     64  1.74       cgd #include <dev/isa/comvar.h>
     65  1.60       cgd #include <dev/ic/ns16550reg.h>
     66  1.65  christos #ifdef COM_HAYESP
     67  1.65  christos #include <dev/ic/hayespreg.h>
     68  1.65  christos #endif
     69  1.62   mycroft #define	com_lcr	com_cfcr
     70  1.14   mycroft 
     71  1.64  christos #define	COM_IBUFSIZE	(2 * 512)
     72  1.57   mycroft #define	COM_IHIGHWATER	((3 * COM_IBUFSIZE) / 4)
     73  1.57   mycroft 
     74  1.21   mycroft struct com_softc {
     75  1.21   mycroft 	struct device sc_dev;
     76  1.49       cgd 	void *sc_ih;
     77  1.50   mycroft 	struct tty *sc_tty;
     78   1.1       cgd 
     79  1.33   mycroft 	int sc_overflows;
     80  1.57   mycroft 	int sc_floods;
     81  1.57   mycroft 	int sc_errors;
     82  1.57   mycroft 
     83  1.66   mycroft 	int sc_halt;
     84  1.66   mycroft 
     85  1.41   mycroft 	int sc_iobase;
     86  1.65  christos #ifdef COM_HAYESP
     87  1.65  christos 	int sc_hayespbase;
     88  1.65  christos #endif
     89  1.75       cgd 
     90  1.75       cgd 	bus_chipset_tag_t sc_bc;
     91  1.75       cgd 	bus_io_handle_t sc_ioh;
     92  1.75       cgd 	bus_io_handle_t sc_hayespioh;
     93  1.75       cgd 
     94  1.22       cgd 	u_char sc_hwflags;
     95  1.29   mycroft #define	COM_HW_NOIEN	0x01
     96  1.22       cgd #define	COM_HW_FIFO	0x02
     97  1.65  christos #define	COM_HW_HAYESP	0x04
     98  1.22       cgd #define	COM_HW_CONSOLE	0x40
     99  1.22       cgd 	u_char sc_swflags;
    100  1.22       cgd #define	COM_SW_SOFTCAR	0x01
    101  1.22       cgd #define	COM_SW_CLOCAL	0x02
    102  1.22       cgd #define	COM_SW_CRTSCTS	0x04
    103  1.25       cgd #define	COM_SW_MDMBUF	0x08
    104  1.69   mycroft 	u_char sc_msr, sc_mcr, sc_lcr, sc_ier;
    105  1.57   mycroft 	u_char sc_dtr;
    106  1.57   mycroft 
    107  1.57   mycroft 	u_char *sc_ibuf, *sc_ibufp, *sc_ibufhigh, *sc_ibufend;
    108  1.57   mycroft 	u_char sc_ibufs[2][COM_IBUFSIZE];
    109  1.29   mycroft };
    110  1.21   mycroft 
    111  1.40   mycroft int comprobe __P((struct device *, void *, void *));
    112  1.75       cgd #ifdef COM_HAYESP
    113  1.75       cgd int comprobeHAYESP __P((bus_io_handle_t hayespioh, struct com_softc *sc));
    114  1.75       cgd #endif
    115  1.40   mycroft void comattach __P((struct device *, struct device *, void *));
    116  1.21   mycroft int comopen __P((dev_t, int, int, struct proc *));
    117  1.21   mycroft int comclose __P((dev_t, int, int, struct proc *));
    118  1.33   mycroft void comdiag __P((void *));
    119  1.49       cgd int comintr __P((void *));
    120  1.57   mycroft void compoll __P((void *));
    121  1.21   mycroft int comparam __P((struct tty *, struct termios *));
    122  1.21   mycroft void comstart __P((struct tty *));
    123   1.1       cgd 
    124  1.76   thorpej /*
    125  1.76   thorpej  * XXX the following two cfattach structs should be different, and possibly
    126  1.76   thorpej  * XXX elsewhere.
    127  1.76   thorpej  */
    128  1.76   thorpej struct cfattach com_isa_ca = {
    129  1.76   thorpej 	sizeof(struct com_softc), comprobe, comattach
    130  1.76   thorpej };
    131  1.76   thorpej 
    132  1.76   thorpej struct cfattach com_multi_ca = {
    133  1.76   thorpej 	sizeof(struct com_softc), comprobe, comattach
    134  1.76   thorpej };
    135  1.75       cgd 
    136  1.76   thorpej struct cfdriver com_cd = {
    137  1.76   thorpej 	NULL, "com", DV_TTY
    138   1.1       cgd };
    139   1.1       cgd 
    140  1.76   thorpej int cominit __P((bus_chipset_tag_t, bus_io_handle_t, int));
    141  1.76   thorpej 
    142   1.1       cgd #ifdef COMCONSOLE
    143  1.75       cgd int	comdefaultrate = CONSPEED;		/* XXX why set default? */
    144   1.1       cgd #else
    145  1.66   mycroft int	comdefaultrate = TTYDEF_SPEED;
    146   1.1       cgd #endif
    147  1.75       cgd int	comconsaddr;
    148   1.1       cgd int	comconsinit;
    149  1.75       cgd int	comconsattached;
    150  1.75       cgd bus_chipset_tag_t comconsbc;
    151  1.75       cgd bus_io_handle_t comconsioh;
    152  1.75       cgd 
    153   1.1       cgd int	commajor;
    154  1.57   mycroft int	comsopen = 0;
    155  1.57   mycroft int	comevents = 0;
    156   1.1       cgd 
    157   1.1       cgd #ifdef KGDB
    158  1.14   mycroft #include <machine/remote-sl.h>
    159   1.1       cgd extern int kgdb_dev;
    160   1.1       cgd extern int kgdb_rate;
    161   1.1       cgd extern int kgdb_debug_init;
    162   1.1       cgd #endif
    163   1.1       cgd 
    164  1.21   mycroft #define	COMUNIT(x)	(minor(x))
    165   1.1       cgd 
    166  1.62   mycroft /* Macros to clear/set/test flags. */
    167  1.62   mycroft #define	SET(t, f)	(t) |= (f)
    168  1.62   mycroft #define	CLR(t, f)	(t) &= ~(f)
    169  1.62   mycroft #define	ISSET(t, f)	((t) & (f))
    170  1.21   mycroft 
    171  1.21   mycroft int
    172  1.21   mycroft comspeed(speed)
    173  1.21   mycroft 	long speed;
    174   1.1       cgd {
    175  1.21   mycroft #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    176  1.21   mycroft 
    177  1.21   mycroft 	int x, err;
    178  1.21   mycroft 
    179  1.21   mycroft 	if (speed == 0)
    180  1.21   mycroft 		return 0;
    181  1.21   mycroft 	if (speed < 0)
    182  1.21   mycroft 		return -1;
    183  1.21   mycroft 	x = divrnd((COM_FREQ / 16), speed);
    184  1.21   mycroft 	if (x <= 0)
    185  1.21   mycroft 		return -1;
    186  1.21   mycroft 	err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
    187  1.21   mycroft 	if (err < 0)
    188  1.21   mycroft 		err = -err;
    189  1.21   mycroft 	if (err > COM_TOLERANCE)
    190  1.21   mycroft 		return -1;
    191  1.21   mycroft 	return x;
    192  1.21   mycroft 
    193  1.21   mycroft #undef	divrnd(n, q)
    194  1.21   mycroft }
    195  1.21   mycroft 
    196  1.21   mycroft int
    197  1.75       cgd comprobe1(bc, ioh, iobase)
    198  1.75       cgd 	bus_chipset_tag_t bc;
    199  1.75       cgd 	bus_io_handle_t ioh;
    200  1.41   mycroft 	int iobase;
    201  1.21   mycroft {
    202  1.21   mycroft 
    203   1.1       cgd 	/* force access to id reg */
    204  1.75       cgd 	bus_io_write_1(bc, ioh, com_lcr, 0);
    205  1.75       cgd 	bus_io_write_1(bc, ioh, com_iir, 0);
    206  1.75       cgd 	if (bus_io_read_1(bc, ioh, com_iir) & 0x38)
    207  1.21   mycroft 		return 0;
    208  1.21   mycroft 
    209  1.21   mycroft 	return 1;
    210   1.1       cgd }
    211   1.1       cgd 
    212  1.65  christos #ifdef COM_HAYESP
    213  1.65  christos int
    214  1.75       cgd comprobeHAYESP(hayespioh, sc)
    215  1.75       cgd 	bus_io_handle_t hayespioh;
    216  1.64  christos 	struct com_softc *sc;
    217  1.64  christos {
    218  1.65  christos 	char	val, dips;
    219  1.64  christos 	int	combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
    220  1.75       cgd 	bus_chipset_tag_t bc = sc->sc_bc;
    221  1.64  christos 
    222  1.64  christos 	/*
    223  1.64  christos 	 * Hayes ESP cards have two iobases.  One is for compatibility with
    224  1.64  christos 	 * 16550 serial chips, and at the same ISA PC base addresses.  The
    225  1.64  christos 	 * other is for ESP-specific enhanced features, and lies at a
    226  1.65  christos 	 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
    227  1.64  christos 	 */
    228  1.64  christos 
    229  1.64  christos 	/* Test for ESP signature */
    230  1.75       cgd 	if ((bus_io_read_1(bc, hayespioh, 0) & 0xf3) == 0)
    231  1.65  christos 		return 0;
    232  1.64  christos 
    233  1.64  christos 	/*
    234  1.64  christos 	 * ESP is present at ESP enhanced base address; unknown com port
    235  1.64  christos 	 */
    236  1.64  christos 
    237  1.64  christos 	/* Get the dip-switch configurations */
    238  1.75       cgd 	bus_io_write_1(bc, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
    239  1.75       cgd 	dips = bus_io_read_1(bc, hayespioh, HAYESP_STATUS1);
    240  1.64  christos 
    241  1.64  christos 	/* Determine which com port this ESP card services: bits 0,1 of  */
    242  1.64  christos 	/*  dips is the port # (0-3); combaselist[val] is the com_iobase */
    243  1.65  christos 	if (sc->sc_iobase != combaselist[dips & 0x03])
    244  1.65  christos 		return 0;
    245  1.65  christos 
    246  1.65  christos 	printf(": ESP");
    247  1.64  christos 
    248  1.65  christos  	/* Check ESP Self Test bits. */
    249  1.64  christos 	/* Check for ESP version 2.0: bits 4,5,6 == 010 */
    250  1.75       cgd 	bus_io_write_1(bc, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
    251  1.75       cgd 	val = bus_io_read_1(bc, hayespioh, HAYESP_STATUS1); /* Clear reg 1 */
    252  1.75       cgd 	val = bus_io_read_1(bc, hayespioh, HAYESP_STATUS2);
    253  1.64  christos 	if ((val & 0x70) < 0x20) {
    254  1.64  christos 		printf("-old (%o)", val & 0x70);
    255  1.64  christos 		/* we do not support the necessary features */
    256  1.65  christos 		return 0;
    257  1.64  christos 	}
    258  1.64  christos 
    259  1.64  christos 	/* Check for ability to emulate 16550: bit 8 == 1 */
    260  1.64  christos 	if ((dips & 0x80) == 0) {
    261  1.64  christos 		printf(" slave");
    262  1.64  christos 		/* XXX Does slave really mean no 16550 support?? */
    263  1.65  christos 		return 0;
    264  1.64  christos 	}
    265  1.64  christos 
    266  1.64  christos 	/*
    267  1.64  christos 	 * If we made it this far, we are a full-featured ESP v2.0 (or
    268  1.64  christos 	 * better), at the correct com port address.
    269  1.64  christos 	 */
    270  1.64  christos 
    271  1.65  christos 	SET(sc->sc_hwflags, COM_HW_HAYESP);
    272  1.73    scottr 	printf(", 1024 byte fifo\n");
    273  1.65  christos 	return 1;
    274  1.64  christos }
    275  1.65  christos #endif
    276  1.64  christos 
    277  1.21   mycroft int
    278  1.40   mycroft comprobe(parent, match, aux)
    279  1.40   mycroft 	struct device *parent;
    280  1.40   mycroft 	void *match, *aux;
    281  1.29   mycroft {
    282  1.75       cgd 	struct cfdata *cf = match;
    283  1.75       cgd 	bus_chipset_tag_t bc;
    284  1.75       cgd 	bus_io_handle_t ioh;
    285  1.75       cgd 	int iobase, needioh;
    286  1.75       cgd 	int rv = 1;
    287  1.75       cgd 
    288  1.76   thorpej 	/*
    289  1.76   thorpej 	 * XXX should be broken out into functions for isa probe and
    290  1.76   thorpej 	 * XXX for commulti probe, with a helper function that contains
    291  1.76   thorpej 	 * XXX most of the interesting stuff.
    292  1.76   thorpej 	 */
    293  1.75       cgd 	if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isa")) {
    294  1.75       cgd 		struct isa_attach_args *ia = aux;
    295  1.75       cgd 
    296  1.75       cgd 		bc = ia->ia_bc;
    297  1.75       cgd 		iobase = ia->ia_iobase;
    298  1.75       cgd 		needioh = 1;
    299  1.75       cgd 	} else {
    300  1.75       cgd 		struct commulti_attach_args *ca = aux;
    301  1.75       cgd 
    302  1.75       cgd 		if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != ca->ca_slave)
    303  1.75       cgd 			return (0);
    304  1.75       cgd 
    305  1.75       cgd 		bc = ca->ca_bc;
    306  1.75       cgd 		iobase = ca->ca_iobase;
    307  1.75       cgd 		ioh = ca->ca_ioh;
    308  1.75       cgd 		needioh = 0;
    309  1.75       cgd 	}
    310  1.75       cgd 
    311  1.75       cgd 	/* if it's in use as console, it's there. */
    312  1.75       cgd 	if (iobase == comconsaddr && !comconsattached)
    313  1.75       cgd 		goto out;
    314  1.75       cgd 
    315  1.75       cgd 	if (needioh && bus_io_map(bc, iobase, COM_NPORTS, &ioh)) {
    316  1.75       cgd 		rv = 0;
    317  1.75       cgd 		goto out;
    318  1.75       cgd 	}
    319  1.75       cgd 	rv = comprobe1(bc, ioh, iobase);
    320  1.75       cgd 	if (needioh)
    321  1.75       cgd 		bus_io_unmap(bc, ioh, COM_NPORTS);
    322  1.21   mycroft 
    323  1.75       cgd out:
    324  1.75       cgd 	if (rv && !strcmp(parent->dv_cfdata->cf_driver->cd_name, "isa")) {
    325  1.75       cgd 		struct isa_attach_args *ia = aux;
    326  1.21   mycroft 
    327  1.75       cgd 		ia->ia_iosize = COM_NPORTS;
    328  1.75       cgd 		ia->ia_msize = 0;
    329  1.75       cgd 	}
    330  1.75       cgd 	return (rv);
    331  1.21   mycroft }
    332   1.1       cgd 
    333  1.29   mycroft void
    334  1.29   mycroft comattach(parent, self, aux)
    335  1.29   mycroft 	struct device *parent, *self;
    336  1.29   mycroft 	void *aux;
    337  1.29   mycroft {
    338  1.29   mycroft 	struct com_softc *sc = (void *)self;
    339  1.29   mycroft 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    340  1.75       cgd 	int iobase, irq;
    341  1.75       cgd 	bus_chipset_tag_t bc;
    342  1.75       cgd 	bus_io_handle_t ioh;
    343  1.21   mycroft 	struct tty *tp;
    344  1.65  christos #ifdef COM_HAYESP
    345  1.65  christos 	int	hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
    346  1.65  christos 	int	*hayespp;
    347  1.65  christos #endif
    348   1.1       cgd 
    349  1.76   thorpej 	/*
    350  1.76   thorpej 	 * XXX should be broken out into functions for isa attach and
    351  1.76   thorpej 	 * XXX for commulti attach, with a helper function that contains
    352  1.76   thorpej 	 * XXX most of the interesting stuff.
    353  1.76   thorpej 	 */
    354  1.75       cgd 	sc->sc_hwflags = 0;
    355  1.75       cgd 	sc->sc_swflags = 0;
    356  1.75       cgd 	if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isa")) {
    357  1.75       cgd 		struct isa_attach_args *ia = aux;
    358  1.75       cgd 
    359  1.75       cgd 		/*
    360  1.75       cgd 		 * We're living on an isa.
    361  1.75       cgd 		 */
    362  1.75       cgd 		iobase = ia->ia_iobase;
    363  1.75       cgd 		bc = ia->ia_bc;
    364  1.75       cgd 	        if (iobase != comconsaddr) {
    365  1.75       cgd 	                if (bus_io_map(bc, iobase, COM_NPORTS, &ioh))
    366  1.75       cgd 				panic("comattach: io mapping failed");
    367  1.75       cgd 		} else
    368  1.75       cgd 	                ioh = comconsioh;
    369  1.75       cgd 		irq = ia->ia_irq;
    370  1.75       cgd 	} else {
    371  1.75       cgd 		struct commulti_attach_args *ca = aux;
    372  1.75       cgd 
    373  1.75       cgd 		/*
    374  1.75       cgd 		 * We're living on a commulti.
    375  1.75       cgd 		 */
    376  1.75       cgd 		iobase = ca->ca_iobase;
    377  1.75       cgd 		bc = ca->ca_bc;
    378  1.75       cgd 		ioh = ca->ca_ioh;
    379  1.75       cgd 		irq = IRQUNK;
    380  1.75       cgd 
    381  1.75       cgd 		if (ca->ca_noien)
    382  1.75       cgd 			sc->sc_hwflags |= COM_HW_NOIEN;
    383  1.75       cgd 	}
    384  1.75       cgd 
    385  1.75       cgd 	sc->sc_bc = bc;
    386  1.75       cgd 	sc->sc_ioh = ioh;
    387  1.21   mycroft 	sc->sc_iobase = iobase;
    388  1.21   mycroft 
    389  1.75       cgd 	if (iobase == comconsaddr) {
    390  1.75       cgd 		comconsattached = 1;
    391  1.75       cgd 
    392  1.75       cgd 		/*
    393  1.75       cgd 		 * Need to reset baud rate, etc. of next print so reset
    394  1.75       cgd 		 * comconsinit.  Also make sure console is always "hardwired".
    395  1.75       cgd 		 */
    396  1.75       cgd 		delay(1000);			/* wait for output to finish */
    397  1.75       cgd 		comconsinit = 0;
    398  1.75       cgd 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    399  1.75       cgd 		SET(sc->sc_swflags, COM_SW_SOFTCAR);
    400  1.75       cgd 	}
    401  1.26       cgd 
    402  1.65  christos #ifdef COM_HAYESP
    403  1.65  christos 	/* Look for a Hayes ESP board. */
    404  1.75       cgd 	for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
    405  1.75       cgd 		bus_io_handle_t hayespioh;
    406  1.75       cgd 
    407  1.75       cgd #define	HAYESP_NPORTS	8			/* XXX XXX XXX ??? ??? ??? */
    408  1.75       cgd 		if (bus_io_map(bc, *hayespp, HAYESP_NPORTS, &hayespioh))
    409  1.75       cgd 			continue;
    410  1.75       cgd 		if (comprobeHAYESP(hayespioh, sc)) {
    411  1.65  christos 			sc->sc_hayespbase = *hayespp;
    412  1.75       cgd 			sc->sc_hayespioh = hayespioh;
    413  1.65  christos 			break;
    414  1.65  christos 		}
    415  1.75       cgd 		bus_io_unmap(bc, hayespioh, HAYESP_NPORTS);
    416  1.75       cgd 	}
    417  1.65  christos 	/* No ESP; look for other things. */
    418  1.65  christos 	if (*hayespp == 0) {
    419  1.65  christos #endif
    420  1.64  christos 
    421   1.1       cgd 	/* look for a NS 16550AF UART with FIFOs */
    422  1.75       cgd 	bus_io_write_1(bc, ioh, com_fifo,
    423  1.21   mycroft 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    424  1.20   mycroft 	delay(100);
    425  1.75       cgd 	if (ISSET(bus_io_read_1(bc, ioh, com_iir), IIR_FIFO_MASK) == IIR_FIFO_MASK)
    426  1.75       cgd 		if (ISSET(bus_io_read_1(bc, ioh, com_fifo), FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
    427  1.62   mycroft 			SET(sc->sc_hwflags, COM_HW_FIFO);
    428  1.29   mycroft 			printf(": ns16550a, working fifo\n");
    429  1.21   mycroft 		} else
    430  1.56       jtc 			printf(": ns16550, broken fifo\n");
    431  1.21   mycroft 	else
    432  1.56       jtc 		printf(": ns8250 or ns16450, no fifo\n");
    433  1.75       cgd 	bus_io_write_1(bc, ioh, com_fifo, 0);
    434  1.65  christos #ifdef COM_HAYESP
    435  1.65  christos 	}
    436  1.65  christos #endif
    437  1.21   mycroft 
    438  1.21   mycroft 	/* disable interrupts */
    439  1.75       cgd 	bus_io_write_1(bc, ioh, com_ier, 0);
    440  1.75       cgd 	bus_io_write_1(bc, ioh, com_mcr, 0);
    441   1.1       cgd 
    442  1.75       cgd 	if (irq != IRQUNK)
    443  1.75       cgd 		sc->sc_ih = isa_intr_establish(irq, IST_EDGE, IPL_TTY,
    444  1.63   mycroft 		    comintr, sc);
    445  1.30   mycroft 
    446   1.1       cgd #ifdef KGDB
    447   1.2       cgd 	if (kgdb_dev == makedev(commajor, unit)) {
    448  1.75       cgd 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    449   1.1       cgd 			kgdb_dev = -1;	/* can't debug over console port */
    450   1.1       cgd 		else {
    451  1.75       cgd 			(void) cominit(bc, ioh, kgdb_rate);
    452   1.1       cgd 			if (kgdb_debug_init) {
    453   1.1       cgd 				/*
    454   1.1       cgd 				 * Print prefix of device name,
    455   1.1       cgd 				 * let kgdb_connect print the rest.
    456   1.1       cgd 				 */
    457  1.21   mycroft 				printf("%s: ", sc->sc_dev.dv_xname);
    458   1.1       cgd 				kgdb_connect(1);
    459   1.1       cgd 			} else
    460  1.21   mycroft 				printf("%s: kgdb enabled\n",
    461  1.21   mycroft 				    sc->sc_dev.dv_xname);
    462   1.1       cgd 		}
    463   1.1       cgd 	}
    464   1.1       cgd #endif
    465  1.21   mycroft 
    466  1.75       cgd 	/* XXX maybe move up some? */
    467  1.75       cgd 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    468  1.75       cgd 		printf("%s: console\n", sc->sc_dev.dv_xname);
    469   1.1       cgd }
    470   1.1       cgd 
    471  1.21   mycroft int
    472  1.21   mycroft comopen(dev, flag, mode, p)
    473  1.21   mycroft 	dev_t dev;
    474  1.21   mycroft 	int flag, mode;
    475  1.21   mycroft 	struct proc *p;
    476   1.1       cgd {
    477  1.21   mycroft 	int unit = COMUNIT(dev);
    478  1.21   mycroft 	struct com_softc *sc;
    479  1.75       cgd 	bus_chipset_tag_t bc;
    480  1.75       cgd 	bus_io_handle_t ioh;
    481  1.21   mycroft 	struct tty *tp;
    482  1.21   mycroft 	int s;
    483   1.1       cgd 	int error = 0;
    484   1.1       cgd 
    485  1.76   thorpej 	if (unit >= com_cd.cd_ndevs)
    486  1.21   mycroft 		return ENXIO;
    487  1.76   thorpej 	sc = com_cd.cd_devs[unit];
    488  1.29   mycroft 	if (!sc)
    489  1.21   mycroft 		return ENXIO;
    490  1.21   mycroft 
    491  1.50   mycroft 	if (!sc->sc_tty)
    492  1.50   mycroft 		tp = sc->sc_tty = ttymalloc();
    493  1.21   mycroft 	else
    494  1.50   mycroft 		tp = sc->sc_tty;
    495  1.21   mycroft 
    496   1.1       cgd 	tp->t_oproc = comstart;
    497   1.1       cgd 	tp->t_param = comparam;
    498   1.1       cgd 	tp->t_dev = dev;
    499  1.62   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    500  1.62   mycroft 		SET(tp->t_state, TS_WOPEN);
    501   1.1       cgd 		ttychars(tp);
    502  1.16        ws 		tp->t_iflag = TTYDEF_IFLAG;
    503  1.16        ws 		tp->t_oflag = TTYDEF_OFLAG;
    504  1.16        ws 		tp->t_cflag = TTYDEF_CFLAG;
    505  1.62   mycroft 		if (ISSET(sc->sc_swflags, COM_SW_CLOCAL))
    506  1.62   mycroft 			SET(tp->t_cflag, CLOCAL);
    507  1.62   mycroft 		if (ISSET(sc->sc_swflags, COM_SW_CRTSCTS))
    508  1.62   mycroft 			SET(tp->t_cflag, CRTSCTS);
    509  1.62   mycroft 		if (ISSET(sc->sc_swflags, COM_SW_MDMBUF))
    510  1.62   mycroft 			SET(tp->t_cflag, MDMBUF);
    511  1.16        ws 		tp->t_lflag = TTYDEF_LFLAG;
    512  1.16        ws 		tp->t_ispeed = tp->t_ospeed = comdefaultrate;
    513  1.57   mycroft 
    514  1.57   mycroft 		s = spltty();
    515  1.57   mycroft 
    516   1.1       cgd 		comparam(tp, &tp->t_termios);
    517   1.1       cgd 		ttsetwater(tp);
    518  1.21   mycroft 
    519  1.57   mycroft 		if (comsopen++ == 0)
    520  1.57   mycroft 			timeout(compoll, NULL, 1);
    521  1.57   mycroft 
    522  1.57   mycroft 		sc->sc_ibufp = sc->sc_ibuf = sc->sc_ibufs[0];
    523  1.57   mycroft 		sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
    524  1.57   mycroft 		sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
    525  1.57   mycroft 
    526  1.75       cgd 		bc = sc->sc_bc;
    527  1.75       cgd 		ioh = sc->sc_ioh;
    528  1.65  christos #ifdef COM_HAYESP
    529  1.64  christos 		/* Setup the ESP board */
    530  1.65  christos 		if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
    531  1.75       cgd 			bus_io_handle_t hayespioh = sc->sc_hayespioh;
    532  1.65  christos 
    533  1.75       cgd 			bus_io_write_1(bc, ioh, com_fifo,
    534  1.64  christos 			     FIFO_DMA_MODE|FIFO_ENABLE|
    535  1.64  christos 			     FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_8);
    536  1.65  christos 
    537  1.64  christos 			/* Set 16550 compatibility mode */
    538  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD1, HAYESP_SETMODE);
    539  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD2,
    540  1.65  christos 			     HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
    541  1.65  christos 			     HAYESP_MODE_SCALE);
    542  1.65  christos 
    543  1.64  christos 			/* Set RTS/CTS flow control */
    544  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD1, HAYESP_SETFLOWTYPE);
    545  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD2, HAYESP_FLOW_RTS);
    546  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD2, HAYESP_FLOW_CTS);
    547  1.65  christos 
    548  1.64  christos 			/* Set flow control levels */
    549  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD1, HAYESP_SETRXFLOW);
    550  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD2,
    551  1.65  christos 			     HAYESP_HIBYTE(HAYESP_RXHIWMARK));
    552  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD2,
    553  1.65  christos 			     HAYESP_LOBYTE(HAYESP_RXHIWMARK));
    554  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD2,
    555  1.65  christos 			     HAYESP_HIBYTE(HAYESP_RXLOWMARK));
    556  1.75       cgd 			bus_io_write_1(bc, hayespioh, HAYESP_CMD2,
    557  1.65  christos 			     HAYESP_LOBYTE(HAYESP_RXLOWMARK));
    558  1.65  christos 		} else
    559  1.65  christos #endif
    560  1.65  christos 		if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
    561  1.64  christos 			/* Set the FIFO threshold based on the receive speed. */
    562  1.75       cgd 			bus_io_write_1(bc, ioh, com_fifo,
    563  1.36   mycroft 			    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
    564  1.36   mycroft 			    (tp->t_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
    565  1.21   mycroft 		/* flush any pending I/O */
    566  1.75       cgd 		while (ISSET(bus_io_read_1(bc, ioh, com_lsr), LSR_RXRDY))
    567  1.75       cgd 			(void) bus_io_read_1(bc, ioh, com_data);
    568  1.21   mycroft 		/* you turn me on, baby */
    569  1.26       cgd 		sc->sc_mcr = MCR_DTR | MCR_RTS;
    570  1.62   mycroft 		if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
    571  1.62   mycroft 			SET(sc->sc_mcr, MCR_IENABLE);
    572  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    573  1.69   mycroft 		sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
    574  1.75       cgd 		bus_io_write_1(bc, ioh, com_ier, sc->sc_ier);
    575  1.21   mycroft 
    576  1.75       cgd 		sc->sc_msr = bus_io_read_1(bc, ioh, com_msr);
    577  1.62   mycroft 		if (ISSET(sc->sc_swflags, COM_SW_SOFTCAR) ||
    578  1.62   mycroft 		    ISSET(sc->sc_msr, MSR_DCD) || ISSET(tp->t_cflag, MDMBUF))
    579  1.62   mycroft 			SET(tp->t_state, TS_CARR_ON);
    580  1.21   mycroft 		else
    581  1.62   mycroft 			CLR(tp->t_state, TS_CARR_ON);
    582  1.62   mycroft 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    583  1.21   mycroft 		return EBUSY;
    584  1.62   mycroft 	else
    585  1.57   mycroft 		s = spltty();
    586  1.21   mycroft 
    587  1.21   mycroft 	/* wait for carrier if necessary */
    588  1.62   mycroft 	if (!ISSET(flag, O_NONBLOCK))
    589  1.62   mycroft 		while (!ISSET(tp->t_cflag, CLOCAL) &&
    590  1.62   mycroft 		    !ISSET(tp->t_state, TS_CARR_ON)) {
    591  1.62   mycroft 			SET(tp->t_state, TS_WOPEN);
    592  1.62   mycroft 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    593  1.62   mycroft 			    ttopen, 0);
    594  1.21   mycroft 			if (error) {
    595  1.21   mycroft 				/* XXX should turn off chip if we're the
    596  1.21   mycroft 				   only waiter */
    597  1.21   mycroft 				splx(s);
    598  1.21   mycroft 				return error;
    599  1.21   mycroft 			}
    600  1.21   mycroft 		}
    601  1.21   mycroft 	splx(s);
    602  1.21   mycroft 
    603  1.21   mycroft 	return (*linesw[tp->t_line].l_open)(dev, tp);
    604   1.1       cgd }
    605   1.1       cgd 
    606  1.21   mycroft int
    607   1.1       cgd comclose(dev, flag, mode, p)
    608   1.1       cgd 	dev_t dev;
    609   1.1       cgd 	int flag, mode;
    610   1.1       cgd 	struct proc *p;
    611   1.1       cgd {
    612  1.21   mycroft 	int unit = COMUNIT(dev);
    613  1.76   thorpej 	struct com_softc *sc = com_cd.cd_devs[unit];
    614  1.50   mycroft 	struct tty *tp = sc->sc_tty;
    615  1.75       cgd 	bus_chipset_tag_t bc = sc->sc_bc;
    616  1.75       cgd 	bus_io_handle_t ioh = sc->sc_ioh;
    617  1.57   mycroft 	int s;
    618  1.57   mycroft 
    619  1.57   mycroft 	/* XXX This is for cons.c. */
    620  1.62   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN))
    621  1.57   mycroft 		return 0;
    622  1.21   mycroft 
    623   1.1       cgd 	(*linesw[tp->t_line].l_close)(tp, flag);
    624  1.57   mycroft 	s = spltty();
    625  1.62   mycroft 	CLR(sc->sc_lcr, LCR_SBREAK);
    626  1.75       cgd 	bus_io_write_1(bc, ioh, com_lcr, sc->sc_lcr);
    627  1.75       cgd 	bus_io_write_1(bc, ioh, com_ier, 0);
    628  1.62   mycroft 	if (ISSET(tp->t_cflag, HUPCL) &&
    629  1.62   mycroft 	    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR)) {
    630  1.57   mycroft 		/* XXX perhaps only clear DTR */
    631  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, 0);
    632  1.57   mycroft 	}
    633  1.62   mycroft 	CLR(tp->t_state, TS_BUSY | TS_FLUSH);
    634  1.57   mycroft 	if (--comsopen == 0)
    635  1.57   mycroft 		untimeout(compoll, NULL);
    636  1.57   mycroft 	splx(s);
    637   1.1       cgd 	ttyclose(tp);
    638  1.21   mycroft #ifdef notyet /* XXXX */
    639  1.75       cgd 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    640  1.21   mycroft 		ttyfree(tp);
    641  1.50   mycroft 		sc->sc_tty = 0;
    642  1.21   mycroft 	}
    643  1.13       cgd #endif
    644  1.21   mycroft 	return 0;
    645   1.1       cgd }
    646   1.1       cgd 
    647  1.21   mycroft int
    648   1.1       cgd comread(dev, uio, flag)
    649   1.1       cgd 	dev_t dev;
    650   1.1       cgd 	struct uio *uio;
    651  1.21   mycroft 	int flag;
    652   1.1       cgd {
    653  1.76   thorpej 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    654  1.52   mycroft 	struct tty *tp = sc->sc_tty;
    655   1.1       cgd 
    656   1.1       cgd 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    657   1.1       cgd }
    658   1.1       cgd 
    659  1.21   mycroft int
    660   1.1       cgd comwrite(dev, uio, flag)
    661   1.1       cgd 	dev_t dev;
    662   1.1       cgd 	struct uio *uio;
    663  1.21   mycroft 	int flag;
    664   1.1       cgd {
    665  1.76   thorpej 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    666  1.52   mycroft 	struct tty *tp = sc->sc_tty;
    667   1.1       cgd 
    668   1.1       cgd 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    669   1.1       cgd }
    670  1.50   mycroft 
    671  1.50   mycroft struct tty *
    672  1.50   mycroft comtty(dev)
    673  1.50   mycroft 	dev_t dev;
    674  1.50   mycroft {
    675  1.76   thorpej 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
    676  1.52   mycroft 	struct tty *tp = sc->sc_tty;
    677  1.50   mycroft 
    678  1.52   mycroft 	return (tp);
    679  1.50   mycroft }
    680   1.1       cgd 
    681  1.21   mycroft static u_char
    682  1.21   mycroft tiocm_xxx2mcr(data)
    683  1.21   mycroft 	int data;
    684  1.21   mycroft {
    685  1.21   mycroft 	u_char m = 0;
    686  1.21   mycroft 
    687  1.62   mycroft 	if (ISSET(data, TIOCM_DTR))
    688  1.62   mycroft 		SET(m, MCR_DTR);
    689  1.62   mycroft 	if (ISSET(data, TIOCM_RTS))
    690  1.62   mycroft 		SET(m, MCR_RTS);
    691  1.21   mycroft 	return m;
    692   1.1       cgd }
    693   1.1       cgd 
    694  1.21   mycroft int
    695  1.19   mycroft comioctl(dev, cmd, data, flag, p)
    696   1.1       cgd 	dev_t dev;
    697  1.39       cgd 	u_long cmd;
    698   1.1       cgd 	caddr_t data;
    699  1.19   mycroft 	int flag;
    700  1.19   mycroft 	struct proc *p;
    701   1.1       cgd {
    702  1.21   mycroft 	int unit = COMUNIT(dev);
    703  1.76   thorpej 	struct com_softc *sc = com_cd.cd_devs[unit];
    704  1.50   mycroft 	struct tty *tp = sc->sc_tty;
    705  1.75       cgd 	bus_chipset_tag_t bc = sc->sc_bc;
    706  1.75       cgd 	bus_io_handle_t ioh = sc->sc_ioh;
    707  1.21   mycroft 	int error;
    708  1.21   mycroft 
    709  1.19   mycroft 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    710   1.1       cgd 	if (error >= 0)
    711  1.21   mycroft 		return error;
    712  1.19   mycroft 	error = ttioctl(tp, cmd, data, flag, p);
    713   1.1       cgd 	if (error >= 0)
    714  1.21   mycroft 		return error;
    715   1.1       cgd 
    716   1.1       cgd 	switch (cmd) {
    717   1.1       cgd 	case TIOCSBRK:
    718  1.62   mycroft 		SET(sc->sc_lcr, LCR_SBREAK);
    719  1.75       cgd 		bus_io_write_1(bc, ioh, com_lcr, sc->sc_lcr);
    720   1.1       cgd 		break;
    721   1.1       cgd 	case TIOCCBRK:
    722  1.62   mycroft 		CLR(sc->sc_lcr, LCR_SBREAK);
    723  1.75       cgd 		bus_io_write_1(bc, ioh, com_lcr, sc->sc_lcr);
    724   1.1       cgd 		break;
    725   1.1       cgd 	case TIOCSDTR:
    726  1.62   mycroft 		SET(sc->sc_mcr, sc->sc_dtr);
    727  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    728   1.1       cgd 		break;
    729   1.1       cgd 	case TIOCCDTR:
    730  1.62   mycroft 		CLR(sc->sc_mcr, sc->sc_dtr);
    731  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    732   1.1       cgd 		break;
    733   1.1       cgd 	case TIOCMSET:
    734  1.62   mycroft 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
    735   1.1       cgd 	case TIOCMBIS:
    736  1.62   mycroft 		SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
    737  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    738   1.1       cgd 		break;
    739   1.1       cgd 	case TIOCMBIC:
    740  1.62   mycroft 		CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
    741  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    742   1.1       cgd 		break;
    743  1.21   mycroft 	case TIOCMGET: {
    744  1.21   mycroft 		u_char m;
    745  1.21   mycroft 		int bits = 0;
    746  1.21   mycroft 
    747  1.21   mycroft 		m = sc->sc_mcr;
    748  1.62   mycroft 		if (ISSET(m, MCR_DTR))
    749  1.62   mycroft 			SET(bits, TIOCM_DTR);
    750  1.62   mycroft 		if (ISSET(m, MCR_RTS))
    751  1.62   mycroft 			SET(bits, TIOCM_RTS);
    752  1.21   mycroft 		m = sc->sc_msr;
    753  1.62   mycroft 		if (ISSET(m, MSR_DCD))
    754  1.62   mycroft 			SET(bits, TIOCM_CD);
    755  1.62   mycroft 		if (ISSET(m, MSR_CTS))
    756  1.62   mycroft 			SET(bits, TIOCM_CTS);
    757  1.62   mycroft 		if (ISSET(m, MSR_DSR))
    758  1.62   mycroft 			SET(bits, TIOCM_DSR);
    759  1.62   mycroft 		if (ISSET(m, MSR_RI | MSR_TERI))
    760  1.62   mycroft 			SET(bits, TIOCM_RI);
    761  1.75       cgd 		if (bus_io_read_1(bc, ioh, com_ier))
    762  1.62   mycroft 			SET(bits, TIOCM_LE);
    763  1.21   mycroft 		*(int *)data = bits;
    764   1.1       cgd 		break;
    765  1.21   mycroft 	}
    766  1.22       cgd 	case TIOCGFLAGS: {
    767  1.62   mycroft 		int driverbits, userbits = 0;
    768  1.22       cgd 
    769  1.62   mycroft 		driverbits = sc->sc_swflags;
    770  1.62   mycroft 		if (ISSET(driverbits, COM_SW_SOFTCAR))
    771  1.62   mycroft 			SET(userbits, TIOCFLAG_SOFTCAR);
    772  1.62   mycroft 		if (ISSET(driverbits, COM_SW_CLOCAL))
    773  1.62   mycroft 			SET(userbits, TIOCFLAG_CLOCAL);
    774  1.62   mycroft 		if (ISSET(driverbits, COM_SW_CRTSCTS))
    775  1.62   mycroft 			SET(userbits, TIOCFLAG_CRTSCTS);
    776  1.62   mycroft 		if (ISSET(driverbits, COM_SW_MDMBUF))
    777  1.62   mycroft 			SET(userbits, TIOCFLAG_MDMBUF);
    778  1.22       cgd 
    779  1.62   mycroft 		*(int *)data = userbits;
    780  1.22       cgd 		break;
    781  1.22       cgd 	}
    782  1.22       cgd 	case TIOCSFLAGS: {
    783  1.22       cgd 		int userbits, driverbits = 0;
    784  1.22       cgd 
    785  1.23       cgd 		error = suser(p->p_ucred, &p->p_acflag);
    786  1.23       cgd 		if (error != 0)
    787  1.23       cgd 			return(EPERM);
    788  1.22       cgd 
    789  1.22       cgd 		userbits = *(int *)data;
    790  1.62   mycroft 		if (ISSET(userbits, TIOCFLAG_SOFTCAR) ||
    791  1.62   mycroft 		    ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    792  1.62   mycroft 			SET(driverbits, COM_SW_SOFTCAR);
    793  1.62   mycroft 		if (ISSET(userbits, TIOCFLAG_CLOCAL))
    794  1.62   mycroft 			SET(driverbits, COM_SW_CLOCAL);
    795  1.62   mycroft 		if (ISSET(userbits, TIOCFLAG_CRTSCTS))
    796  1.62   mycroft 			SET(driverbits, COM_SW_CRTSCTS);
    797  1.62   mycroft 		if (ISSET(userbits, TIOCFLAG_MDMBUF))
    798  1.62   mycroft 			SET(driverbits, COM_SW_MDMBUF);
    799  1.22       cgd 
    800  1.23       cgd 		sc->sc_swflags = driverbits;
    801  1.22       cgd 		break;
    802  1.22       cgd 	}
    803   1.1       cgd 	default:
    804  1.21   mycroft 		return ENOTTY;
    805   1.1       cgd 	}
    806  1.21   mycroft 
    807  1.21   mycroft 	return 0;
    808   1.1       cgd }
    809   1.1       cgd 
    810  1.21   mycroft int
    811   1.1       cgd comparam(tp, t)
    812  1.21   mycroft 	struct tty *tp;
    813  1.21   mycroft 	struct termios *t;
    814   1.1       cgd {
    815  1.76   thorpej 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
    816  1.75       cgd 	bus_chipset_tag_t bc = sc->sc_bc;
    817  1.75       cgd 	bus_io_handle_t ioh = sc->sc_ioh;
    818  1.21   mycroft 	int ospeed = comspeed(t->c_ospeed);
    819  1.62   mycroft 	u_char lcr;
    820  1.57   mycroft 	tcflag_t oldcflag;
    821  1.21   mycroft 	int s;
    822  1.21   mycroft 
    823   1.1       cgd 	/* check requested parameters */
    824  1.21   mycroft 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    825  1.21   mycroft 		return EINVAL;
    826  1.21   mycroft 
    827  1.66   mycroft 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK);
    828  1.62   mycroft 
    829  1.62   mycroft 	switch (ISSET(t->c_cflag, CSIZE)) {
    830   1.1       cgd 	case CS5:
    831  1.62   mycroft 		SET(lcr, LCR_5BITS);
    832  1.21   mycroft 		break;
    833   1.1       cgd 	case CS6:
    834  1.62   mycroft 		SET(lcr, LCR_6BITS);
    835  1.21   mycroft 		break;
    836   1.1       cgd 	case CS7:
    837  1.62   mycroft 		SET(lcr, LCR_7BITS);
    838  1.21   mycroft 		break;
    839   1.1       cgd 	case CS8:
    840  1.62   mycroft 		SET(lcr, LCR_8BITS);
    841  1.21   mycroft 		break;
    842   1.1       cgd 	}
    843  1.62   mycroft 	if (ISSET(t->c_cflag, PARENB)) {
    844  1.62   mycroft 		SET(lcr, LCR_PENAB);
    845  1.62   mycroft 		if (!ISSET(t->c_cflag, PARODD))
    846  1.62   mycroft 			SET(lcr, LCR_PEVEN);
    847   1.1       cgd 	}
    848  1.62   mycroft 	if (ISSET(t->c_cflag, CSTOPB))
    849  1.62   mycroft 		SET(lcr, LCR_STOPB);
    850  1.62   mycroft 
    851  1.62   mycroft 	sc->sc_lcr = lcr;
    852   1.1       cgd 
    853  1.21   mycroft 	s = spltty();
    854  1.35   mycroft 
    855  1.62   mycroft 	if (ospeed == 0) {
    856  1.62   mycroft 		CLR(sc->sc_mcr, MCR_DTR);
    857  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    858  1.62   mycroft 	}
    859  1.36   mycroft 
    860  1.36   mycroft 	/*
    861  1.36   mycroft 	 * Set the FIFO threshold based on the receive speed, if we are
    862  1.36   mycroft 	 * changing it.
    863  1.36   mycroft 	 */
    864  1.66   mycroft #if 1
    865  1.65  christos 	if (tp->t_ispeed != t->c_ispeed) {
    866  1.66   mycroft #else
    867  1.66   mycroft 	if (1) {
    868  1.66   mycroft #endif
    869  1.66   mycroft 		if (ospeed != 0) {
    870  1.66   mycroft 			/*
    871  1.66   mycroft 			 * Make sure the transmit FIFO is empty before
    872  1.67   mycroft 			 * proceeding.  If we don't do this, some revisions
    873  1.67   mycroft 			 * of the UART will hang.  Interestingly enough,
    874  1.67   mycroft 			 * even if we do this will the last character is
    875  1.67   mycroft 			 * still being pushed out, they don't hang.  This
    876  1.67   mycroft 			 * seems good enough.
    877  1.66   mycroft 			 */
    878  1.66   mycroft 			while (ISSET(tp->t_state, TS_BUSY)) {
    879  1.66   mycroft 				int error;
    880  1.66   mycroft 
    881  1.66   mycroft 				++sc->sc_halt;
    882  1.66   mycroft 				error = ttysleep(tp, &tp->t_outq,
    883  1.66   mycroft 				    TTOPRI | PCATCH, "comprm", 0);
    884  1.66   mycroft 				--sc->sc_halt;
    885  1.66   mycroft 				if (error) {
    886  1.66   mycroft 					splx(s);
    887  1.66   mycroft 					comstart(tp);
    888  1.66   mycroft 					return (error);
    889  1.66   mycroft 				}
    890  1.66   mycroft 			}
    891  1.66   mycroft 
    892  1.75       cgd 			bus_io_write_1(bc, ioh, com_lcr, lcr | LCR_DLAB);
    893  1.75       cgd 			bus_io_write_1(bc, ioh, com_dlbl, ospeed);
    894  1.75       cgd 			bus_io_write_1(bc, ioh, com_dlbh, ospeed >> 8);
    895  1.75       cgd 			bus_io_write_1(bc, ioh, com_lcr, lcr);
    896  1.66   mycroft 			SET(sc->sc_mcr, MCR_DTR);
    897  1.75       cgd 			bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    898  1.66   mycroft 		} else
    899  1.75       cgd 			bus_io_write_1(bc, ioh, com_lcr, lcr);
    900  1.66   mycroft 
    901  1.66   mycroft 		if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP) &&
    902  1.66   mycroft 		    ISSET(sc->sc_hwflags, COM_HW_FIFO))
    903  1.75       cgd 			bus_io_write_1(bc, ioh, com_fifo,
    904  1.57   mycroft 			    FIFO_ENABLE |
    905  1.36   mycroft 			    (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
    906  1.57   mycroft 	} else
    907  1.75       cgd 		bus_io_write_1(bc, ioh, com_lcr, lcr);
    908  1.21   mycroft 
    909  1.22       cgd 	/* When not using CRTSCTS, RTS follows DTR. */
    910  1.62   mycroft 	if (!ISSET(t->c_cflag, CRTSCTS)) {
    911  1.62   mycroft 		if (ISSET(sc->sc_mcr, MCR_DTR)) {
    912  1.62   mycroft 			if (!ISSET(sc->sc_mcr, MCR_RTS)) {
    913  1.62   mycroft 				SET(sc->sc_mcr, MCR_RTS);
    914  1.75       cgd 				bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    915  1.62   mycroft 			}
    916  1.21   mycroft 		} else {
    917  1.62   mycroft 			if (ISSET(sc->sc_mcr, MCR_RTS)) {
    918  1.62   mycroft 				CLR(sc->sc_mcr, MCR_RTS);
    919  1.75       cgd 				bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    920  1.62   mycroft 			}
    921  1.21   mycroft 		}
    922  1.57   mycroft 		sc->sc_dtr = MCR_DTR | MCR_RTS;
    923  1.57   mycroft 	} else
    924  1.57   mycroft 		sc->sc_dtr = MCR_DTR;
    925  1.21   mycroft 
    926  1.57   mycroft 	/* and copy to tty */
    927  1.57   mycroft 	tp->t_ispeed = t->c_ispeed;
    928  1.57   mycroft 	tp->t_ospeed = t->c_ospeed;
    929  1.57   mycroft 	oldcflag = tp->t_cflag;
    930  1.57   mycroft 	tp->t_cflag = t->c_cflag;
    931  1.25       cgd 
    932  1.25       cgd 	/*
    933  1.57   mycroft 	 * If DCD is off and MDMBUF is changed, ask the tty layer if we should
    934  1.57   mycroft 	 * stop the device.
    935  1.25       cgd 	 */
    936  1.62   mycroft 	if (!ISSET(sc->sc_msr, MSR_DCD) &&
    937  1.62   mycroft 	    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
    938  1.62   mycroft 	    ISSET(oldcflag, MDMBUF) != ISSET(tp->t_cflag, MDMBUF) &&
    939  1.57   mycroft 	    (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
    940  1.62   mycroft 		CLR(sc->sc_mcr, sc->sc_dtr);
    941  1.75       cgd 		bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
    942  1.21   mycroft 	}
    943   1.1       cgd 
    944  1.66   mycroft 	/* Just to be sure... */
    945  1.21   mycroft 	splx(s);
    946  1.66   mycroft 	comstart(tp);
    947  1.21   mycroft 	return 0;
    948   1.1       cgd }
    949  1.21   mycroft 
    950  1.12   deraadt void
    951   1.1       cgd comstart(tp)
    952  1.21   mycroft 	struct tty *tp;
    953   1.1       cgd {
    954  1.76   thorpej 	struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
    955  1.75       cgd 	bus_chipset_tag_t bc = sc->sc_bc;
    956  1.75       cgd 	bus_io_handle_t ioh = sc->sc_ioh;
    957  1.21   mycroft 	int s;
    958  1.21   mycroft 
    959   1.1       cgd 	s = spltty();
    960  1.69   mycroft 	if (ISSET(tp->t_state, TS_BUSY))
    961  1.70   mycroft 		goto out;
    962  1.69   mycroft 	if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP) ||
    963  1.69   mycroft 	    sc->sc_halt > 0)
    964  1.69   mycroft 		goto stopped;
    965  1.62   mycroft 	if (ISSET(tp->t_cflag, CRTSCTS) && !ISSET(sc->sc_msr, MSR_CTS))
    966  1.69   mycroft 		goto stopped;
    967  1.61   mycroft 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    968  1.62   mycroft 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    969  1.62   mycroft 			CLR(tp->t_state, TS_ASLEEP);
    970  1.62   mycroft 			wakeup(&tp->t_outq);
    971  1.61   mycroft 		}
    972  1.61   mycroft 		if (tp->t_outq.c_cc == 0)
    973  1.69   mycroft 			goto stopped;
    974  1.61   mycroft 		selwakeup(&tp->t_wsel);
    975  1.61   mycroft 	}
    976  1.62   mycroft 	SET(tp->t_state, TS_BUSY);
    977  1.64  christos 
    978  1.70   mycroft 	if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
    979  1.70   mycroft 		SET(sc->sc_ier, IER_ETXRDY);
    980  1.75       cgd 		bus_io_write_1(bc, ioh, com_ier, sc->sc_ier);
    981  1.70   mycroft 	}
    982  1.65  christos #ifdef COM_HAYESP
    983  1.65  christos 	if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
    984  1.64  christos 		u_char buffer[1024], *cp = buffer;
    985  1.64  christos 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    986  1.64  christos 		do
    987  1.75       cgd 			bus_io_write_1(bc, ioh, com_data, *cp++);
    988  1.64  christos 		while (--n);
    989  1.64  christos 	}
    990  1.65  christos 	else
    991  1.65  christos #endif
    992  1.65  christos 	if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
    993  1.21   mycroft 		u_char buffer[16], *cp = buffer;
    994  1.21   mycroft 		int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
    995  1.21   mycroft 		do {
    996  1.75       cgd 			bus_io_write_1(bc, ioh, com_data, *cp++);
    997  1.21   mycroft 		} while (--n);
    998  1.21   mycroft 	} else
    999  1.75       cgd 		bus_io_write_1(bc, ioh, com_data, getc(&tp->t_outq));
   1000  1.70   mycroft out:
   1001  1.69   mycroft 	splx(s);
   1002  1.69   mycroft 	return;
   1003  1.69   mycroft stopped:
   1004  1.69   mycroft 	if (ISSET(sc->sc_ier, IER_ETXRDY)) {
   1005  1.69   mycroft 		CLR(sc->sc_ier, IER_ETXRDY);
   1006  1.75       cgd 		bus_io_write_1(bc, ioh, com_ier, sc->sc_ier);
   1007  1.69   mycroft 	}
   1008   1.1       cgd 	splx(s);
   1009   1.1       cgd }
   1010  1.21   mycroft 
   1011   1.1       cgd /*
   1012   1.1       cgd  * Stop output on a line.
   1013   1.1       cgd  */
   1014  1.21   mycroft void
   1015   1.1       cgd comstop(tp, flag)
   1016  1.21   mycroft 	struct tty *tp;
   1017   1.1       cgd {
   1018  1.21   mycroft 	int s;
   1019   1.1       cgd 
   1020   1.1       cgd 	s = spltty();
   1021  1.62   mycroft 	if (ISSET(tp->t_state, TS_BUSY))
   1022  1.62   mycroft 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1023  1.62   mycroft 			SET(tp->t_state, TS_FLUSH);
   1024   1.1       cgd 	splx(s);
   1025   1.1       cgd }
   1026   1.1       cgd 
   1027  1.33   mycroft void
   1028  1.33   mycroft comdiag(arg)
   1029  1.33   mycroft 	void *arg;
   1030  1.33   mycroft {
   1031  1.33   mycroft 	struct com_softc *sc = arg;
   1032  1.57   mycroft 	int overflows, floods;
   1033  1.33   mycroft 	int s;
   1034  1.33   mycroft 
   1035  1.33   mycroft 	s = spltty();
   1036  1.57   mycroft 	sc->sc_errors = 0;
   1037  1.33   mycroft 	overflows = sc->sc_overflows;
   1038  1.33   mycroft 	sc->sc_overflows = 0;
   1039  1.57   mycroft 	floods = sc->sc_floods;
   1040  1.57   mycroft 	sc->sc_floods = 0;
   1041  1.57   mycroft 	splx(s);
   1042  1.57   mycroft 
   1043  1.57   mycroft 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s\n",
   1044  1.57   mycroft 	    sc->sc_dev.dv_xname,
   1045  1.57   mycroft 	    overflows, overflows == 1 ? "" : "s",
   1046  1.57   mycroft 	    floods, floods == 1 ? "" : "s");
   1047  1.57   mycroft }
   1048  1.57   mycroft 
   1049  1.57   mycroft void
   1050  1.57   mycroft compoll(arg)
   1051  1.57   mycroft 	void *arg;
   1052  1.57   mycroft {
   1053  1.57   mycroft 	int unit;
   1054  1.57   mycroft 	struct com_softc *sc;
   1055  1.57   mycroft 	struct tty *tp;
   1056  1.57   mycroft 	register u_char *ibufp;
   1057  1.57   mycroft 	u_char *ibufend;
   1058  1.57   mycroft 	register int c;
   1059  1.57   mycroft 	int s;
   1060  1.57   mycroft 	static int lsrmap[8] = {
   1061  1.57   mycroft 		0,      TTY_PE,
   1062  1.57   mycroft 		TTY_FE, TTY_PE|TTY_FE,
   1063  1.57   mycroft 		TTY_FE, TTY_PE|TTY_FE,
   1064  1.57   mycroft 		TTY_FE, TTY_PE|TTY_FE
   1065  1.57   mycroft 	};
   1066  1.57   mycroft 
   1067  1.57   mycroft 	s = spltty();
   1068  1.57   mycroft 	if (comevents == 0) {
   1069  1.57   mycroft 		splx(s);
   1070  1.57   mycroft 		goto out;
   1071  1.57   mycroft 	}
   1072  1.57   mycroft 	comevents = 0;
   1073  1.33   mycroft 	splx(s);
   1074  1.33   mycroft 
   1075  1.76   thorpej 	for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
   1076  1.76   thorpej 		sc = com_cd.cd_devs[unit];
   1077  1.57   mycroft 		if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
   1078  1.57   mycroft 			continue;
   1079  1.57   mycroft 
   1080  1.57   mycroft 		tp = sc->sc_tty;
   1081  1.57   mycroft 
   1082  1.57   mycroft 		s = spltty();
   1083  1.57   mycroft 
   1084  1.57   mycroft 		ibufp = sc->sc_ibuf;
   1085  1.57   mycroft 		ibufend = sc->sc_ibufp;
   1086  1.61   mycroft 
   1087  1.61   mycroft 		if (ibufp == ibufend) {
   1088  1.61   mycroft 			splx(s);
   1089  1.61   mycroft 			continue;
   1090  1.61   mycroft 		}
   1091  1.57   mycroft 
   1092  1.57   mycroft 		sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
   1093  1.57   mycroft 					     sc->sc_ibufs[1] : sc->sc_ibufs[0];
   1094  1.57   mycroft 		sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
   1095  1.57   mycroft 		sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
   1096  1.57   mycroft 
   1097  1.62   mycroft 		if (tp == 0 || !ISSET(tp->t_state, TS_ISOPEN)) {
   1098  1.57   mycroft 			splx(s);
   1099  1.57   mycroft 			continue;
   1100  1.57   mycroft 		}
   1101  1.57   mycroft 
   1102  1.62   mycroft 		if (ISSET(tp->t_cflag, CRTSCTS) &&
   1103  1.62   mycroft 		    !ISSET(sc->sc_mcr, MCR_RTS)) {
   1104  1.62   mycroft 			/* XXX */
   1105  1.62   mycroft 			SET(sc->sc_mcr, MCR_RTS);
   1106  1.75       cgd 			bus_io_write_1(sc->sc_bc, sc->sc_ioh, com_mcr,
   1107  1.75       cgd 			    sc->sc_mcr);
   1108  1.62   mycroft 		}
   1109  1.57   mycroft 
   1110  1.57   mycroft 		splx(s);
   1111  1.57   mycroft 
   1112  1.57   mycroft 		while (ibufp < ibufend) {
   1113  1.57   mycroft 			c = *ibufp++;
   1114  1.57   mycroft 			if (*ibufp & LSR_OE) {
   1115  1.57   mycroft 				sc->sc_overflows++;
   1116  1.57   mycroft 				if (sc->sc_errors++ == 0)
   1117  1.57   mycroft 					timeout(comdiag, sc, 60 * hz);
   1118  1.57   mycroft 			}
   1119  1.57   mycroft 			/* This is ugly, but fast. */
   1120  1.57   mycroft 			c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
   1121  1.57   mycroft 			(*linesw[tp->t_line].l_rint)(c, tp);
   1122  1.57   mycroft 		}
   1123  1.57   mycroft 	}
   1124  1.57   mycroft 
   1125  1.57   mycroft out:
   1126  1.57   mycroft 	timeout(compoll, NULL, 1);
   1127  1.21   mycroft }
   1128   1.1       cgd 
   1129  1.21   mycroft int
   1130  1.49       cgd comintr(arg)
   1131  1.49       cgd 	void *arg;
   1132  1.21   mycroft {
   1133  1.49       cgd 	struct com_softc *sc = arg;
   1134  1.75       cgd 	bus_chipset_tag_t bc = sc->sc_bc;
   1135  1.75       cgd 	bus_io_handle_t ioh = sc->sc_ioh;
   1136  1.21   mycroft 	struct tty *tp;
   1137  1.55   mycroft 	u_char lsr, data, msr, delta;
   1138  1.69   mycroft #ifdef COM_DEBUG
   1139  1.72   mycroft 	int n;
   1140  1.69   mycroft 	struct {
   1141  1.72   mycroft 		u_char iir, lsr, msr;
   1142  1.69   mycroft 	} iter[32];
   1143  1.69   mycroft #endif
   1144  1.55   mycroft 
   1145  1.72   mycroft #ifdef COM_DEBUG
   1146  1.72   mycroft 	n = 0;
   1147  1.75       cgd 	if (ISSET(iter[n].iir = bus_io_read_1(bc, ioh, com_iir), IIR_NOPEND))
   1148  1.72   mycroft 		return (0);
   1149  1.72   mycroft #else
   1150  1.75       cgd 	if (ISSET(bus_io_read_1(bc, ioh, com_iir), IIR_NOPEND))
   1151  1.55   mycroft 		return (0);
   1152  1.72   mycroft #endif
   1153  1.21   mycroft 
   1154  1.55   mycroft 	tp = sc->sc_tty;
   1155  1.21   mycroft 
   1156  1.21   mycroft 	for (;;) {
   1157  1.69   mycroft #ifdef COM_DEBUG
   1158  1.69   mycroft 		iter[n].lsr =
   1159  1.69   mycroft #endif
   1160  1.75       cgd 		lsr = bus_io_read_1(bc, ioh, com_lsr);
   1161  1.55   mycroft 
   1162  1.69   mycroft 		if (ISSET(lsr, LSR_RXRDY)) {
   1163  1.57   mycroft 			register u_char *p = sc->sc_ibufp;
   1164  1.57   mycroft 
   1165  1.57   mycroft 			comevents = 1;
   1166  1.55   mycroft 			do {
   1167  1.75       cgd 				data = bus_io_read_1(bc, ioh, com_data);
   1168  1.62   mycroft 				if (ISSET(lsr, LSR_BI)) {
   1169  1.72   mycroft #ifdef notdef
   1170  1.69   mycroft 					printf("break %02x %02x %02x %02x\n",
   1171  1.69   mycroft 					    sc->sc_msr, sc->sc_mcr, sc->sc_lcr,
   1172  1.69   mycroft 					    sc->sc_dtr);
   1173  1.69   mycroft #endif
   1174  1.57   mycroft #ifdef DDB
   1175  1.75       cgd 					if (ISSET(sc->sc_hwflags,
   1176  1.75       cgd 					    COM_HW_CONSOLE)) {
   1177  1.58   mycroft 						Debugger();
   1178  1.58   mycroft 						goto next;
   1179  1.59   mycroft 					}
   1180  1.21   mycroft #endif
   1181  1.62   mycroft 				}
   1182  1.57   mycroft 				if (p >= sc->sc_ibufend) {
   1183  1.57   mycroft 					sc->sc_floods++;
   1184  1.57   mycroft 					if (sc->sc_errors++ == 0)
   1185  1.57   mycroft 						timeout(comdiag, sc, 60 * hz);
   1186  1.57   mycroft 				} else {
   1187  1.57   mycroft 					*p++ = data;
   1188  1.57   mycroft 					*p++ = lsr;
   1189  1.57   mycroft 					if (p == sc->sc_ibufhigh &&
   1190  1.62   mycroft 					    ISSET(tp->t_cflag, CRTSCTS)) {
   1191  1.62   mycroft 						/* XXX */
   1192  1.62   mycroft 						CLR(sc->sc_mcr, MCR_RTS);
   1193  1.75       cgd 						bus_io_write_1(bc, ioh, com_mcr,
   1194  1.75       cgd 						    sc->sc_mcr);
   1195  1.62   mycroft 					}
   1196  1.55   mycroft 				}
   1197  1.57   mycroft 			next:
   1198  1.69   mycroft #ifdef COM_DEBUG
   1199  1.69   mycroft 				if (++n >= 32)
   1200  1.69   mycroft 					goto ohfudge;
   1201  1.69   mycroft 				iter[n].lsr =
   1202  1.69   mycroft #endif
   1203  1.75       cgd 				lsr = bus_io_read_1(bc, ioh, com_lsr);
   1204  1.69   mycroft 			} while (ISSET(lsr, LSR_RXRDY));
   1205  1.57   mycroft 
   1206  1.57   mycroft 			sc->sc_ibufp = p;
   1207  1.55   mycroft 		}
   1208  1.69   mycroft #ifdef COM_DEBUG
   1209  1.62   mycroft 		else if (ISSET(lsr, LSR_BI|LSR_FE|LSR_PE|LSR_OE))
   1210  1.62   mycroft 			printf("weird lsr %02x\n", lsr);
   1211  1.62   mycroft #endif
   1212  1.55   mycroft 
   1213  1.69   mycroft #ifdef COM_DEBUG
   1214  1.69   mycroft 		iter[n].msr =
   1215  1.69   mycroft #endif
   1216  1.75       cgd 		msr = bus_io_read_1(bc, ioh, com_msr);
   1217  1.55   mycroft 
   1218  1.55   mycroft 		if (msr != sc->sc_msr) {
   1219  1.55   mycroft 			delta = msr ^ sc->sc_msr;
   1220  1.55   mycroft 			sc->sc_msr = msr;
   1221  1.62   mycroft 			if (ISSET(delta, MSR_DCD) &&
   1222  1.62   mycroft 			    !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
   1223  1.62   mycroft 			    (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD)) == 0) {
   1224  1.62   mycroft 				CLR(sc->sc_mcr, sc->sc_dtr);
   1225  1.75       cgd 				bus_io_write_1(bc, ioh, com_mcr, sc->sc_mcr);
   1226  1.55   mycroft 			}
   1227  1.62   mycroft 			if (ISSET(delta & msr, MSR_CTS) &&
   1228  1.62   mycroft 			    ISSET(tp->t_cflag, CRTSCTS)) {
   1229  1.55   mycroft 				/* the line is up and we want to do rts/cts flow control */
   1230  1.57   mycroft 				(*linesw[tp->t_line].l_start)(tp);
   1231  1.55   mycroft 			}
   1232  1.55   mycroft 		}
   1233  1.55   mycroft 
   1234  1.62   mycroft 		if (ISSET(lsr, LSR_TXRDY) && ISSET(tp->t_state, TS_BUSY)) {
   1235  1.68   mycroft 			CLR(tp->t_state, TS_BUSY | TS_FLUSH);
   1236  1.66   mycroft 			if (sc->sc_halt > 0)
   1237  1.66   mycroft 				wakeup(&tp->t_outq);
   1238  1.69   mycroft 			(*linesw[tp->t_line].l_start)(tp);
   1239  1.62   mycroft 		}
   1240  1.62   mycroft 
   1241  1.69   mycroft #ifdef COM_DEBUG
   1242  1.69   mycroft 		if (++n >= 32)
   1243  1.69   mycroft 			goto ohfudge;
   1244  1.75       cgd 		if (ISSET(iter[n].iir = bus_io_read_1(bc, ioh, com_iir), IIR_NOPEND))
   1245  1.72   mycroft 			return (1);
   1246  1.72   mycroft #else
   1247  1.75       cgd 		if (ISSET(bus_io_read_1(bc, ioh, com_iir), IIR_NOPEND))
   1248  1.72   mycroft 			return (1);
   1249  1.69   mycroft #endif
   1250   1.1       cgd 	}
   1251  1.69   mycroft #ifdef COM_DEBUG
   1252  1.69   mycroft ohfudge:
   1253  1.69   mycroft 	printf("comintr: too many iterations");
   1254  1.69   mycroft 	for (n = 0; n < 32; n++) {
   1255  1.69   mycroft 		if ((n % 4) == 0)
   1256  1.72   mycroft 			printf("\ncomintr: iter[%02d]", n);
   1257  1.72   mycroft 		printf("  %02x %02x %02x", iter[n].iir, iter[n].lsr, iter[n].msr);
   1258  1.69   mycroft 	}
   1259  1.69   mycroft 	printf("\n");
   1260  1.72   mycroft 	printf("comintr: msr %02x mcr %02x lcr %02x ier %02x\n",
   1261  1.72   mycroft 	    sc->sc_msr, sc->sc_mcr, sc->sc_lcr, sc->sc_ier);
   1262  1.72   mycroft 	printf("comintr: state %08x cc %d\n", sc->sc_tty->t_state,
   1263  1.72   mycroft 	    sc->sc_tty->t_outq.c_cc);
   1264  1.69   mycroft #endif
   1265   1.1       cgd }
   1266   1.1       cgd 
   1267   1.1       cgd /*
   1268   1.1       cgd  * Following are all routines needed for COM to act as console
   1269   1.1       cgd  */
   1270  1.17       cgd #include <dev/cons.h>
   1271   1.1       cgd 
   1272  1.48   mycroft void
   1273   1.1       cgd comcnprobe(cp)
   1274   1.1       cgd 	struct consdev *cp;
   1275   1.1       cgd {
   1276  1.75       cgd 	bus_chipset_tag_t bc;
   1277  1.75       cgd 	bus_io_handle_t ioh;
   1278  1.75       cgd 	int found;
   1279  1.75       cgd 
   1280  1.75       cgd #if 0
   1281  1.75       cgd 	XXX NEEDS TO BE FIXED XXX
   1282  1.75       cgd 	bc = ???;
   1283  1.75       cgd #endif
   1284  1.75       cgd 	if (bus_io_map(bc, CONADDR, COM_NPORTS, &ioh)) {
   1285  1.75       cgd 		cp->cn_pri = CN_DEAD;
   1286  1.75       cgd 		return;
   1287  1.75       cgd 	}
   1288  1.75       cgd 	found = comprobe1(bc, ioh, CONADDR);
   1289  1.75       cgd 	bus_io_unmap(bc, ioh, COM_NPORTS);
   1290  1.75       cgd 	if (!found) {
   1291  1.21   mycroft 		cp->cn_pri = CN_DEAD;
   1292  1.21   mycroft 		return;
   1293  1.21   mycroft 	}
   1294   1.1       cgd 
   1295   1.1       cgd 	/* locate the major number */
   1296   1.1       cgd 	for (commajor = 0; commajor < nchrdev; commajor++)
   1297   1.1       cgd 		if (cdevsw[commajor].d_open == comopen)
   1298   1.1       cgd 			break;
   1299   1.1       cgd 
   1300   1.1       cgd 	/* initialize required fields */
   1301  1.29   mycroft 	cp->cn_dev = makedev(commajor, CONUNIT);
   1302   1.1       cgd #ifdef	COMCONSOLE
   1303   1.1       cgd 	cp->cn_pri = CN_REMOTE;		/* Force a serial port console */
   1304   1.1       cgd #else
   1305   1.1       cgd 	cp->cn_pri = CN_NORMAL;
   1306   1.1       cgd #endif
   1307   1.1       cgd }
   1308   1.1       cgd 
   1309  1.48   mycroft void
   1310   1.1       cgd comcninit(cp)
   1311   1.1       cgd 	struct consdev *cp;
   1312   1.1       cgd {
   1313   1.1       cgd 
   1314  1.75       cgd #if 0
   1315  1.75       cgd 	XXX NEEDS TO BE FIXED XXX
   1316  1.75       cgd 	comconsbc = ???;
   1317  1.75       cgd #endif
   1318  1.75       cgd 	if (bus_io_map(comconsbc, CONADDR, COM_NPORTS, &comconsioh))
   1319  1.75       cgd 		panic("comcninit: mapping failed");
   1320  1.75       cgd 
   1321  1.75       cgd 	cominit(comconsbc, comconsioh, comdefaultrate);
   1322  1.75       cgd 	comconsaddr = CONADDR;
   1323  1.21   mycroft 	comconsinit = 0;
   1324   1.1       cgd }
   1325   1.1       cgd 
   1326  1.75       cgd cominit(bc, ioh, rate)
   1327  1.75       cgd 	bus_chipset_tag_t bc;
   1328  1.75       cgd 	bus_io_handle_t ioh;
   1329  1.75       cgd 	int rate;
   1330   1.1       cgd {
   1331  1.21   mycroft 	int s = splhigh();
   1332  1.21   mycroft 	u_char stat;
   1333   1.1       cgd 
   1334  1.75       cgd 	bus_io_write_1(bc, ioh, com_lcr, LCR_DLAB);
   1335  1.21   mycroft 	rate = comspeed(comdefaultrate);
   1336  1.75       cgd 	bus_io_write_1(bc, ioh, com_dlbl, rate);
   1337  1.75       cgd 	bus_io_write_1(bc, ioh, com_dlbh, rate >> 8);
   1338  1.75       cgd 	bus_io_write_1(bc, ioh, com_lcr, LCR_8BITS);
   1339  1.75       cgd 	bus_io_write_1(bc, ioh, com_ier, IER_ERXRDY | IER_ETXRDY);
   1340  1.75       cgd 	bus_io_write_1(bc, ioh, com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
   1341  1.75       cgd 	stat = bus_io_read_1(bc, ioh, com_iir);
   1342   1.1       cgd 	splx(s);
   1343   1.1       cgd }
   1344   1.1       cgd 
   1345   1.1       cgd comcngetc(dev)
   1346  1.21   mycroft 	dev_t dev;
   1347   1.1       cgd {
   1348  1.21   mycroft 	int s = splhigh();
   1349  1.75       cgd 	bus_chipset_tag_t bc = comconsbc;
   1350  1.75       cgd 	bus_io_handle_t ioh = comconsioh;
   1351  1.21   mycroft 	u_char stat, c;
   1352   1.1       cgd 
   1353  1.75       cgd 	while (!ISSET(stat = bus_io_read_1(bc, ioh, com_lsr), LSR_RXRDY))
   1354   1.1       cgd 		;
   1355  1.75       cgd 	c = bus_io_read_1(bc, ioh, com_data);
   1356  1.75       cgd 	stat = bus_io_read_1(bc, ioh, com_iir);
   1357   1.1       cgd 	splx(s);
   1358  1.21   mycroft 	return c;
   1359   1.1       cgd }
   1360   1.1       cgd 
   1361   1.1       cgd /*
   1362   1.1       cgd  * Console kernel output character routine.
   1363   1.1       cgd  */
   1364  1.48   mycroft void
   1365   1.1       cgd comcnputc(dev, c)
   1366   1.1       cgd 	dev_t dev;
   1367  1.21   mycroft 	int c;
   1368   1.1       cgd {
   1369  1.21   mycroft 	int s = splhigh();
   1370  1.75       cgd 	bus_chipset_tag_t bc = comconsbc;
   1371  1.75       cgd 	bus_io_handle_t ioh = comconsioh;
   1372  1.21   mycroft 	u_char stat;
   1373   1.1       cgd 	register int timo;
   1374   1.1       cgd 
   1375   1.1       cgd #ifdef KGDB
   1376   1.1       cgd 	if (dev != kgdb_dev)
   1377   1.1       cgd #endif
   1378   1.1       cgd 	if (comconsinit == 0) {
   1379  1.75       cgd 		(void) cominit(bc, ioh, comdefaultrate);
   1380   1.1       cgd 		comconsinit = 1;
   1381   1.1       cgd 	}
   1382   1.1       cgd 	/* wait for any pending transmission to finish */
   1383   1.1       cgd 	timo = 50000;
   1384  1.75       cgd 	while (!ISSET(stat = bus_io_read_1(bc, ioh, com_lsr), LSR_TXRDY) && --timo)
   1385   1.1       cgd 		;
   1386  1.75       cgd 	bus_io_write_1(bc, ioh, com_data, c);
   1387   1.1       cgd 	/* wait for this transmission to complete */
   1388   1.1       cgd 	timo = 1500000;
   1389  1.75       cgd 	while (!ISSET(stat = bus_io_read_1(bc, ioh, com_lsr), LSR_TXRDY) && --timo)
   1390   1.1       cgd 		;
   1391   1.1       cgd 	/* clear any interrupts generated by this transmission */
   1392  1.75       cgd 	stat = bus_io_read_1(bc, ioh, com_iir);
   1393   1.1       cgd 	splx(s);
   1394  1.37   mycroft }
   1395  1.37   mycroft 
   1396  1.37   mycroft void
   1397  1.37   mycroft comcnpollc(dev, on)
   1398  1.37   mycroft 	dev_t dev;
   1399  1.37   mycroft 	int on;
   1400  1.37   mycroft {
   1401  1.37   mycroft 
   1402   1.2       cgd }
   1403