Home | History | Annotate | Line # | Download | only in ic
com.c revision 1.273
      1  1.273    dyoung /*	$NetBSD: com.c,v 1.273 2008/02/29 07:02:05 dyoung Exp $	*/
      2   1.38       cgd 
      3    1.1       cgd /*-
      4  1.269        ad  * Copyright (c) 1998, 1999, 2004, 2008 The NetBSD Foundation, Inc.
      5  1.146   mycroft  * All rights reserved.
      6   1.99   mycroft  *
      7  1.146   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8  1.146   mycroft  * by Charles M. Hannum.
      9   1.99   mycroft  *
     10   1.99   mycroft  * Redistribution and use in source and binary forms, with or without
     11   1.99   mycroft  * modification, are permitted provided that the following conditions
     12   1.99   mycroft  * are met:
     13   1.99   mycroft  * 1. Redistributions of source code must retain the above copyright
     14   1.99   mycroft  *    notice, this list of conditions and the following disclaimer.
     15   1.99   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.99   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17   1.99   mycroft  *    documentation and/or other materials provided with the distribution.
     18   1.99   mycroft  * 3. All advertising materials mentioning features or use of this software
     19   1.99   mycroft  *    must display the following acknowledgement:
     20  1.146   mycroft  *        This product includes software developed by the NetBSD
     21  1.146   mycroft  *        Foundation, Inc. and its contributors.
     22  1.146   mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.146   mycroft  *    contributors may be used to endorse or promote products derived
     24  1.146   mycroft  *    from this software without specific prior written permission.
     25   1.99   mycroft  *
     26  1.146   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.146   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.146   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.146   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.146   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.146   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.146   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.146   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.146   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.146   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.146   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37   1.99   mycroft  */
     38   1.99   mycroft 
     39   1.99   mycroft /*
     40    1.1       cgd  * Copyright (c) 1991 The Regents of the University of California.
     41    1.1       cgd  * All rights reserved.
     42    1.1       cgd  *
     43    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44    1.1       cgd  * modification, are permitted provided that the following conditions
     45    1.1       cgd  * are met:
     46    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51  1.217       agc  * 3. Neither the name of the University nor the names of its contributors
     52    1.1       cgd  *    may be used to endorse or promote products derived from this software
     53    1.1       cgd  *    without specific prior written permission.
     54    1.1       cgd  *
     55    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65    1.1       cgd  * SUCH DAMAGE.
     66    1.1       cgd  *
     67   1.38       cgd  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     68    1.1       cgd  */
     69    1.1       cgd 
     70    1.1       cgd /*
     71   1.99   mycroft  * COM driver, uses National Semiconductor NS16450/NS16550AF UART
     72  1.116      fvdl  * Supports automatic hardware flow control on StarTech ST16C650A UART
     73    1.1       cgd  */
     74  1.191     lukem 
     75  1.191     lukem #include <sys/cdefs.h>
     76  1.273    dyoung __KERNEL_RCSID(0, "$NetBSD: com.c,v 1.273 2008/02/29 07:02:05 dyoung Exp $");
     77  1.145  jonathan 
     78  1.185     lukem #include "opt_com.h"
     79  1.145  jonathan #include "opt_ddb.h"
     80  1.185     lukem #include "opt_kgdb.h"
     81  1.213    martin #include "opt_lockdebug.h"
     82  1.213    martin #include "opt_multiprocessor.h"
     83  1.224    simonb #include "opt_ntp.h"
     84  1.115  explorer 
     85  1.115  explorer #include "rnd.h"
     86  1.115  explorer #if NRND > 0 && defined(RND_COM)
     87  1.115  explorer #include <sys/rnd.h>
     88  1.115  explorer #endif
     89  1.115  explorer 
     90  1.227   thorpej /* The COM16650 option was renamed to COM_16650. */
     91  1.227   thorpej #ifdef COM16650
     92  1.227   thorpej #error Obsolete COM16650 option; use COM_16650 instead.
     93  1.227   thorpej #endif
     94  1.227   thorpej 
     95  1.186       uwe /*
     96  1.186       uwe  * Override cnmagic(9) macro before including <sys/systm.h>.
     97  1.186       uwe  * We need to know if cn_check_magic triggered debugger, so set a flag.
     98  1.186       uwe  * Callers of cn_check_magic must declare int cn_trapped = 0;
     99  1.186       uwe  * XXX: this is *ugly*!
    100  1.186       uwe  */
    101  1.186       uwe #define cn_trap()				\
    102  1.186       uwe 	do {					\
    103  1.186       uwe 		console_debugger();		\
    104  1.186       uwe 		cn_trapped = 1;			\
    105  1.186       uwe 	} while (/* CONSTCOND */ 0)
    106  1.186       uwe 
    107   1.14   mycroft #include <sys/param.h>
    108   1.14   mycroft #include <sys/systm.h>
    109   1.14   mycroft #include <sys/ioctl.h>
    110   1.14   mycroft #include <sys/select.h>
    111  1.234        ws #include <sys/poll.h>
    112   1.14   mycroft #include <sys/tty.h>
    113   1.14   mycroft #include <sys/proc.h>
    114   1.14   mycroft #include <sys/user.h>
    115   1.14   mycroft #include <sys/conf.h>
    116   1.14   mycroft #include <sys/file.h>
    117   1.14   mycroft #include <sys/uio.h>
    118   1.14   mycroft #include <sys/kernel.h>
    119   1.14   mycroft #include <sys/syslog.h>
    120   1.21   mycroft #include <sys/device.h>
    121  1.127   mycroft #include <sys/malloc.h>
    122  1.144  jonathan #include <sys/timepps.h>
    123  1.149   thorpej #include <sys/vnode.h>
    124  1.243      elad #include <sys/kauth.h>
    125  1.263        ad #include <sys/intr.h>
    126   1.14   mycroft 
    127  1.265        ad #include <sys/bus.h>
    128   1.14   mycroft 
    129  1.113   thorpej #include <dev/ic/comreg.h>
    130  1.113   thorpej #include <dev/ic/comvar.h>
    131   1.60       cgd #include <dev/ic/ns16550reg.h>
    132  1.116      fvdl #include <dev/ic/st16650reg.h>
    133   1.65  christos #ifdef COM_HAYESP
    134   1.65  christos #include <dev/ic/hayespreg.h>
    135   1.65  christos #endif
    136   1.62   mycroft #define	com_lcr	com_cfcr
    137  1.106  drochner #include <dev/cons.h>
    138   1.14   mycroft 
    139  1.247   gdamore #ifdef	COM_REGMAP
    140  1.247   gdamore #define	CSR_WRITE_1(r, o, v)	\
    141  1.247   gdamore 	bus_space_write_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], v)
    142  1.247   gdamore #define	CSR_READ_1(r, o)	\
    143  1.247   gdamore 	bus_space_read_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o])
    144  1.247   gdamore #define	CSR_WRITE_2(r, o, v)	\
    145  1.247   gdamore 	bus_space_write_2((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], v)
    146  1.247   gdamore #define	CSR_READ_2(r, o)	\
    147  1.247   gdamore 	bus_space_read_2((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o])
    148  1.247   gdamore #define	CSR_WRITE_MULTI(r, o, p, n)	\
    149  1.247   gdamore 	bus_space_write_multi_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], p, n)
    150  1.247   gdamore #else
    151  1.247   gdamore #define	CSR_WRITE_1(r, o, v)	\
    152  1.247   gdamore 	bus_space_write_1((r)->cr_iot, (r)->cr_ioh, o, v)
    153  1.247   gdamore #define	CSR_READ_1(r, o)	\
    154  1.247   gdamore 	bus_space_read_1((r)->cr_iot, (r)->cr_ioh, o)
    155  1.247   gdamore #define	CSR_WRITE_2(r, o, v)	\
    156  1.247   gdamore 	bus_space_write_2((r)->cr_iot, (r)->cr_ioh, o, v)
    157  1.247   gdamore #define	CSR_READ_2(r, o)	\
    158  1.247   gdamore 	bus_space_read_2((r)->cr_iot, (r)->cr_ioh, o)
    159  1.247   gdamore #define	CSR_WRITE_MULTI(r, o, p, n)	\
    160  1.247   gdamore 	bus_space_write_multi_1((r)->cr_iot, (r)->cr_ioh, o, p, n)
    161   1.65  christos #endif
    162  1.102   thorpej 
    163  1.247   gdamore 
    164  1.197    simonb static void com_enable_debugport(struct com_softc *);
    165  1.186       uwe 
    166  1.197    simonb void	com_config(struct com_softc *);
    167  1.197    simonb void	com_shutdown(struct com_softc *);
    168  1.210   thorpej int	comspeed(long, long, int);
    169  1.197    simonb static	u_char	cflag2lcr(tcflag_t);
    170  1.197    simonb int	comparam(struct tty *, struct termios *);
    171  1.197    simonb void	comstart(struct tty *);
    172  1.197    simonb int	comhwiflow(struct tty *, int);
    173  1.197    simonb 
    174  1.197    simonb void	com_loadchannelregs(struct com_softc *);
    175  1.197    simonb void	com_hwiflow(struct com_softc *);
    176  1.197    simonb void	com_break(struct com_softc *, int);
    177  1.197    simonb void	com_modem(struct com_softc *, int);
    178  1.197    simonb void	tiocm_to_com(struct com_softc *, u_long, int);
    179  1.197    simonb int	com_to_tiocm(struct com_softc *);
    180  1.197    simonb void	com_iflush(struct com_softc *);
    181   1.80  christos 
    182  1.247   gdamore int	com_common_getc(dev_t, struct com_regs *);
    183  1.247   gdamore void	com_common_putc(dev_t, struct com_regs *, int);
    184  1.102   thorpej 
    185  1.247   gdamore int	cominit(struct com_regs *, int, int, int, tcflag_t);
    186  1.187    simonb 
    187  1.197    simonb int	comcngetc(dev_t);
    188  1.197    simonb void	comcnputc(dev_t, int);
    189  1.197    simonb void	comcnpollc(dev_t, int);
    190   1.80  christos 
    191   1.99   mycroft #define	integrate	static inline
    192  1.197    simonb void 	comsoft(void *);
    193  1.197    simonb integrate void com_rxsoft(struct com_softc *, struct tty *);
    194  1.197    simonb integrate void com_txsoft(struct com_softc *, struct tty *);
    195  1.197    simonb integrate void com_stsoft(struct com_softc *, struct tty *);
    196  1.197    simonb integrate void com_schedrx(struct com_softc *);
    197  1.197    simonb void	comdiag(void *);
    198  1.127   mycroft 
    199  1.130   thorpej extern struct cfdriver com_cd;
    200   1.76   thorpej 
    201  1.199   gehenna dev_type_open(comopen);
    202  1.199   gehenna dev_type_close(comclose);
    203  1.199   gehenna dev_type_read(comread);
    204  1.199   gehenna dev_type_write(comwrite);
    205  1.199   gehenna dev_type_ioctl(comioctl);
    206  1.199   gehenna dev_type_stop(comstop);
    207  1.199   gehenna dev_type_tty(comtty);
    208  1.199   gehenna dev_type_poll(compoll);
    209  1.199   gehenna 
    210  1.199   gehenna const struct cdevsw com_cdevsw = {
    211  1.199   gehenna 	comopen, comclose, comread, comwrite, comioctl,
    212  1.200  jdolecek 	comstop, comtty, compoll, nommap, ttykqfilter, D_TTY
    213  1.199   gehenna };
    214  1.199   gehenna 
    215  1.127   mycroft /*
    216  1.127   mycroft  * Make this an option variable one can patch.
    217  1.127   mycroft  * But be warned:  this must be a power of 2!
    218  1.127   mycroft  */
    219  1.127   mycroft u_int com_rbuf_size = COM_RING_SIZE;
    220  1.127   mycroft 
    221  1.127   mycroft /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    222  1.127   mycroft u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
    223  1.127   mycroft u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
    224  1.127   mycroft 
    225  1.247   gdamore static struct com_regs comconsregs;
    226  1.247   gdamore static int comconsattached;
    227  1.106  drochner static int comconsrate;
    228  1.106  drochner static tcflag_t comconscflag;
    229  1.186       uwe static struct cnm_state com_cnm_state;
    230   1.99   mycroft 
    231    1.1       cgd #ifdef KGDB
    232  1.102   thorpej #include <sys/kgdb.h>
    233  1.106  drochner 
    234  1.247   gdamore static struct com_regs comkgdbregs;
    235  1.106  drochner static int com_kgdb_attached;
    236  1.102   thorpej 
    237  1.197    simonb int	com_kgdb_getc(void *);
    238  1.197    simonb void	com_kgdb_putc(void *, int);
    239  1.102   thorpej #endif /* KGDB */
    240    1.1       cgd 
    241  1.247   gdamore #ifdef COM_REGMAP
    242  1.247   gdamore /* initializer for typical 16550-ish hardware */
    243  1.247   gdamore #define	COM_REG_16550	{ \
    244  1.247   gdamore 	com_data, com_data, com_dlbl, com_dlbh, com_ier, com_iir, com_fifo, \
    245  1.247   gdamore 	com_efr, com_lcr, com_mcr, com_lsr, com_msr }
    246  1.247   gdamore 
    247  1.247   gdamore const bus_size_t com_std_map[16] = COM_REG_16550;
    248  1.247   gdamore #endif /* COM_REGMAP */
    249  1.247   gdamore 
    250  1.149   thorpej #define	COMUNIT_MASK	0x7ffff
    251  1.149   thorpej #define	COMDIALOUT_MASK	0x80000
    252  1.149   thorpej 
    253  1.149   thorpej #define	COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    254  1.149   thorpej #define	COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    255  1.149   thorpej 
    256  1.149   thorpej #define	COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    257  1.241   thorpej 			 device_is_active(&(sc)->sc_dev))
    258    1.1       cgd 
    259  1.160   thorpej #define	BR	BUS_SPACE_BARRIER_READ
    260  1.160   thorpej #define	BW	BUS_SPACE_BARRIER_WRITE
    261  1.247   gdamore #define COM_BARRIER(r, f) \
    262  1.247   gdamore 	bus_space_barrier((r)->cr_iot, (r)->cr_ioh, 0, (r)->cr_nports, (f))
    263  1.160   thorpej 
    264  1.210   thorpej /*ARGSUSED*/
    265   1.21   mycroft int
    266  1.256  christos comspeed(long speed, long frequency, int type)
    267    1.1       cgd {
    268   1.21   mycroft #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    269   1.21   mycroft 
    270   1.21   mycroft 	int x, err;
    271   1.21   mycroft 
    272   1.99   mycroft #if 0
    273   1.21   mycroft 	if (speed == 0)
    274   1.99   mycroft 		return (0);
    275   1.99   mycroft #endif
    276   1.99   mycroft 	if (speed <= 0)
    277   1.99   mycroft 		return (-1);
    278  1.109        is 	x = divrnd(frequency / 16, speed);
    279   1.21   mycroft 	if (x <= 0)
    280   1.99   mycroft 		return (-1);
    281  1.114        is 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    282   1.21   mycroft 	if (err < 0)
    283   1.21   mycroft 		err = -err;
    284   1.21   mycroft 	if (err > COM_TOLERANCE)
    285   1.99   mycroft 		return (-1);
    286   1.99   mycroft 	return (x);
    287   1.21   mycroft 
    288  1.172   thorpej #undef	divrnd
    289   1.21   mycroft }
    290   1.21   mycroft 
    291   1.99   mycroft #ifdef COM_DEBUG
    292  1.101   mycroft int	com_debug = 0;
    293  1.101   mycroft 
    294  1.235    kleink void comstatus(struct com_softc *, const char *);
    295   1.99   mycroft void
    296  1.235    kleink comstatus(struct com_softc *sc, const char *str)
    297   1.99   mycroft {
    298   1.99   mycroft 	struct tty *tp = sc->sc_tty;
    299   1.99   mycroft 
    300  1.218  christos 	printf("%s: %s %cclocal  %cdcd %cts_carr_on %cdtr %ctx_stopped\n",
    301   1.99   mycroft 	    sc->sc_dev.dv_xname, str,
    302  1.218  christos 	    ISSET(tp->t_cflag, CLOCAL) ? '+' : '-',
    303  1.218  christos 	    ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-',
    304  1.218  christos 	    ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-',
    305  1.218  christos 	    ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-',
    306  1.218  christos 	    sc->sc_tx_stopped ? '+' : '-');
    307   1.99   mycroft 
    308  1.218  christos 	printf("%s: %s %ccrtscts %ccts %cts_ttstop  %crts rx_flags=0x%x\n",
    309   1.99   mycroft 	    sc->sc_dev.dv_xname, str,
    310  1.218  christos 	    ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-',
    311  1.218  christos 	    ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-',
    312  1.218  christos 	    ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-',
    313  1.218  christos 	    ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-',
    314  1.101   mycroft 	    sc->sc_rx_flags);
    315   1.99   mycroft }
    316   1.99   mycroft #endif
    317   1.99   mycroft 
    318   1.21   mycroft int
    319  1.247   gdamore com_probe_subr(struct com_regs *regs)
    320   1.21   mycroft {
    321   1.21   mycroft 
    322    1.1       cgd 	/* force access to id reg */
    323  1.247   gdamore 	CSR_WRITE_1(regs, COM_REG_LCR, LCR_8BITS);
    324  1.247   gdamore 	CSR_WRITE_1(regs, COM_REG_IIR, 0);
    325  1.247   gdamore 	if ((CSR_READ_1(regs, COM_REG_LCR) != LCR_8BITS) ||
    326  1.247   gdamore 	    (CSR_READ_1(regs, COM_REG_IIR) & 0x38))
    327   1.99   mycroft 		return (0);
    328   1.21   mycroft 
    329   1.99   mycroft 	return (1);
    330    1.1       cgd }
    331    1.1       cgd 
    332   1.65  christos int
    333  1.247   gdamore comprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
    334   1.64  christos {
    335  1.247   gdamore 	struct com_regs	regs;
    336   1.64  christos 
    337  1.247   gdamore 	regs.cr_iot = iot;
    338  1.247   gdamore 	regs.cr_ioh = ioh;
    339  1.247   gdamore #ifdef	COM_REGMAP
    340  1.247   gdamore 	memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));;
    341  1.247   gdamore #endif
    342   1.64  christos 
    343  1.247   gdamore 	return com_probe_subr(&regs);
    344   1.64  christos }
    345   1.64  christos 
    346  1.264        ad /*
    347  1.264        ad  * No locking in this routine; it is only called during attach,
    348  1.264        ad  * or with the port already locked.
    349  1.264        ad  */
    350  1.104  drochner static void
    351  1.197    simonb com_enable_debugport(struct com_softc *sc)
    352  1.104  drochner {
    353  1.263        ad 
    354  1.104  drochner 	/* Turn on line break interrupt, set carrier. */
    355  1.104  drochner 	sc->sc_ier = IER_ERXRDY;
    356  1.209   thorpej 	if (sc->sc_type == COM_TYPE_PXA2x0)
    357  1.208       scw 		sc->sc_ier |= IER_EUART | IER_ERXTOUT;
    358  1.247   gdamore 	CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
    359  1.104  drochner 	SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
    360  1.247   gdamore 	CSR_WRITE_1(&sc->sc_regs, COM_REG_MCR, sc->sc_mcr);
    361  1.104  drochner }
    362    1.1       cgd 
    363   1.29   mycroft void
    364  1.197    simonb com_attach_subr(struct com_softc *sc)
    365   1.29   mycroft {
    366  1.247   gdamore 	struct com_regs *regsp = &sc->sc_regs;
    367  1.127   mycroft 	struct tty *tp;
    368  1.227   thorpej #ifdef COM_16650
    369  1.116      fvdl 	u_int8_t lcr;
    370  1.118      fvdl #endif
    371  1.208       scw 	const char *fifo_msg = NULL;
    372  1.117   mycroft 
    373  1.257       uwe 	aprint_naive("\n");
    374  1.257       uwe 
    375  1.260        ad 	callout_init(&sc->sc_diag_callout, 0);
    376  1.267        ad 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
    377  1.170   thorpej 
    378  1.117   mycroft 	/* Disable interrupts before configuring the device. */
    379  1.209   thorpej 	if (sc->sc_type == COM_TYPE_PXA2x0)
    380  1.208       scw 		sc->sc_ier = IER_EUART;
    381  1.208       scw 	else
    382  1.208       scw 		sc->sc_ier = 0;
    383    1.1       cgd 
    384  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
    385  1.247   gdamore 
    386  1.247   gdamore 	if (regsp->cr_iot == comconsregs.cr_iot &&
    387  1.247   gdamore 	    regsp->cr_iobase == comconsregs.cr_iobase) {
    388  1.105  drochner 		comconsattached = 1;
    389  1.105  drochner 
    390   1.96   mycroft 		/* Make sure the console is always "hardwired". */
    391  1.226   thorpej 		delay(10000);			/* wait for output to finish */
    392   1.75       cgd 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    393   1.99   mycroft 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    394   1.75       cgd 	}
    395   1.26       cgd 
    396  1.247   gdamore 	/* Probe for FIFO */
    397  1.247   gdamore 	switch (sc->sc_type) {
    398  1.247   gdamore 	case COM_TYPE_HAYESP:
    399  1.247   gdamore 		goto fifodone;
    400  1.247   gdamore 
    401  1.247   gdamore 	case COM_TYPE_AU1x00:
    402  1.247   gdamore 		sc->sc_fifolen = 16;
    403  1.247   gdamore 		fifo_msg = "Au1X00 UART, working fifo";
    404  1.247   gdamore 		SET(sc->sc_hwflags, COM_HW_FIFO);
    405  1.247   gdamore 		goto fifodelay;
    406  1.247   gdamore 	}
    407   1.99   mycroft 
    408   1.99   mycroft 	sc->sc_fifolen = 1;
    409    1.1       cgd 	/* look for a NS 16550AF UART with FIFOs */
    410  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_FIFO,
    411   1.21   mycroft 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
    412   1.20   mycroft 	delay(100);
    413  1.247   gdamore 	if (ISSET(CSR_READ_1(regsp, COM_REG_IIR), IIR_FIFO_MASK)
    414   1.99   mycroft 	    == IIR_FIFO_MASK)
    415  1.247   gdamore 		if (ISSET(CSR_READ_1(regsp, COM_REG_FIFO), FIFO_TRIGGER_14)
    416   1.99   mycroft 		    == FIFO_TRIGGER_14) {
    417   1.62   mycroft 			SET(sc->sc_hwflags, COM_HW_FIFO);
    418  1.116      fvdl 
    419  1.227   thorpej #ifdef COM_16650
    420  1.116      fvdl 			/*
    421  1.116      fvdl 			 * IIR changes into the EFR if LCR is set to LCR_EERS
    422  1.116      fvdl 			 * on 16650s. We also know IIR != 0 at this point.
    423  1.116      fvdl 			 * Write 0 into the EFR, and read it. If the result
    424  1.116      fvdl 			 * is 0, we have a 16650.
    425  1.116      fvdl 			 *
    426  1.116      fvdl 			 * Older 16650s were broken; the test to detect them
    427  1.116      fvdl 			 * is taken from the Linux driver. Apparently
    428  1.116      fvdl 			 * setting DLAB enable gives access to the EFR on
    429  1.116      fvdl 			 * these chips.
    430  1.116      fvdl 			 */
    431  1.247   gdamore 			lcr = CSR_READ_1(regsp, COM_REG_LCR);
    432  1.247   gdamore 			CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
    433  1.247   gdamore 			CSR_WRITE_1(regsp, COM_REG_EFR, 0);
    434  1.247   gdamore 			if (CSR_READ_1(regsp, COM_REG_EFR) == 0) {
    435  1.247   gdamore 				CSR_WRITE_1(regsp, COM_REG_LCR,
    436  1.116      fvdl 				    lcr | LCR_DLAB);
    437  1.247   gdamore 				if (CSR_READ_1(regsp, COM_REG_EFR) == 0) {
    438  1.116      fvdl 					CLR(sc->sc_hwflags, COM_HW_FIFO);
    439  1.116      fvdl 					sc->sc_fifolen = 0;
    440  1.116      fvdl 				} else {
    441  1.116      fvdl 					SET(sc->sc_hwflags, COM_HW_FLOW);
    442  1.116      fvdl 					sc->sc_fifolen = 32;
    443  1.116      fvdl 				}
    444  1.118      fvdl 			} else
    445  1.118      fvdl #endif
    446  1.116      fvdl 				sc->sc_fifolen = 16;
    447  1.116      fvdl 
    448  1.227   thorpej #ifdef COM_16650
    449  1.247   gdamore 			CSR_WRITE_1(regsp, COM_REG_LCR, lcr);
    450  1.119  drochner 			if (sc->sc_fifolen == 0)
    451  1.208       scw 				fifo_msg = "st16650, broken fifo";
    452  1.119  drochner 			else if (sc->sc_fifolen == 32)
    453  1.208       scw 				fifo_msg = "st16650a, working fifo";
    454  1.119  drochner 			else
    455  1.118      fvdl #endif
    456  1.208       scw 				fifo_msg = "ns16550a, working fifo";
    457   1.21   mycroft 		} else
    458  1.208       scw 			fifo_msg = "ns16550, broken fifo";
    459   1.21   mycroft 	else
    460  1.208       scw 		fifo_msg = "ns8250 or ns16450, no fifo";
    461  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_FIFO, 0);
    462  1.247   gdamore fifodelay:
    463  1.208       scw 	/*
    464  1.208       scw 	 * Some chips will clear down both Tx and Rx FIFOs when zero is
    465  1.208       scw 	 * written to com_fifo. If this chip is the console, writing zero
    466  1.208       scw 	 * results in some of the chip/FIFO description being lost, so delay
    467  1.208       scw 	 * printing it until now.
    468  1.208       scw 	 */
    469  1.208       scw 	delay(10);
    470  1.208       scw 	aprint_normal(": %s\n", fifo_msg);
    471  1.166      soda 	if (ISSET(sc->sc_hwflags, COM_HW_TXFIFO_DISABLE)) {
    472  1.166      soda 		sc->sc_fifolen = 1;
    473  1.202   thorpej 		aprint_normal("%s: txfifo disabled\n", sc->sc_dev.dv_xname);
    474  1.166      soda 	}
    475  1.247   gdamore 
    476  1.247   gdamore fifodone:
    477   1.21   mycroft 
    478  1.127   mycroft 	tp = ttymalloc();
    479  1.127   mycroft 	tp->t_oproc = comstart;
    480  1.127   mycroft 	tp->t_param = comparam;
    481  1.127   mycroft 	tp->t_hwiflow = comhwiflow;
    482  1.127   mycroft 
    483  1.127   mycroft 	sc->sc_tty = tp;
    484  1.147   thorpej 	sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    485  1.182  sommerfe 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    486  1.182  sommerfe 	sc->sc_rbavail = com_rbuf_size;
    487  1.147   thorpej 	if (sc->sc_rbuf == NULL) {
    488  1.202   thorpej 		aprint_error("%s: unable to allocate ring buffer\n",
    489  1.147   thorpej 		    sc->sc_dev.dv_xname);
    490  1.147   thorpej 		return;
    491  1.147   thorpej 	}
    492  1.127   mycroft 	sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
    493  1.147   thorpej 
    494  1.147   thorpej 	tty_attach(tp);
    495  1.147   thorpej 
    496   1.99   mycroft 	if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
    497   1.99   mycroft 		SET(sc->sc_mcr, MCR_IENABLE);
    498   1.30   mycroft 
    499   1.96   mycroft 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    500  1.106  drochner 		int maj;
    501  1.106  drochner 
    502  1.106  drochner 		/* locate the major number */
    503  1.199   gehenna 		maj = cdevsw_lookup_major(&com_cdevsw);
    504  1.106  drochner 
    505  1.242   thorpej 		tp->t_dev = cn_tab->cn_dev = makedev(maj,
    506  1.242   thorpej 						     device_unit(&sc->sc_dev));
    507  1.131      marc 
    508  1.273    dyoung 		aprint_normal_dev(&sc->sc_dev, "console\n");
    509   1.96   mycroft 	}
    510   1.96   mycroft 
    511    1.1       cgd #ifdef KGDB
    512  1.102   thorpej 	/*
    513  1.102   thorpej 	 * Allow kgdb to "take over" this port.  If this is
    514  1.206    briggs 	 * not the console and is the kgdb device, it has
    515  1.206    briggs 	 * exclusive use.  If it's the console _and_ the
    516  1.206    briggs 	 * kgdb device, it doesn't.
    517  1.102   thorpej 	 */
    518  1.247   gdamore 	if (regsp->cr_iot == comkgdbregs.cr_iot &&
    519  1.247   gdamore 	    regsp->cr_iobase == comkgdbregs.cr_iobase) {
    520  1.206    briggs 		if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    521  1.206    briggs 			com_kgdb_attached = 1;
    522  1.106  drochner 
    523  1.206    briggs 			SET(sc->sc_hwflags, COM_HW_KGDB);
    524  1.206    briggs 		}
    525  1.202   thorpej 		aprint_normal("%s: kgdb\n", sc->sc_dev.dv_xname);
    526  1.103  drochner 	}
    527   1.99   mycroft #endif
    528   1.99   mycroft 
    529  1.263        ad 	sc->sc_si = softint_establish(SOFTINT_SERIAL, comsoft, sc);
    530  1.115  explorer 
    531  1.115  explorer #if NRND > 0 && defined(RND_COM)
    532  1.115  explorer 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    533  1.155  explorer 			  RND_TYPE_TTY, 0);
    534  1.115  explorer #endif
    535  1.131      marc 
    536  1.131      marc 	/* if there are no enable/disable functions, assume the device
    537  1.131      marc 	   is always enabled */
    538  1.131      marc 	if (!sc->enable)
    539  1.131      marc 		sc->enabled = 1;
    540  1.131      marc 
    541  1.131      marc 	com_config(sc);
    542  1.132       cgd 
    543  1.132       cgd 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    544  1.131      marc }
    545  1.131      marc 
    546  1.131      marc void
    547  1.197    simonb com_config(struct com_softc *sc)
    548  1.131      marc {
    549  1.247   gdamore 	struct com_regs *regsp = &sc->sc_regs;
    550  1.131      marc 
    551  1.131      marc 	/* Disable interrupts before configuring the device. */
    552  1.209   thorpej 	if (sc->sc_type == COM_TYPE_PXA2x0)
    553  1.208       scw 		sc->sc_ier = IER_EUART;
    554  1.208       scw 	else
    555  1.208       scw 		sc->sc_ier = 0;
    556  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
    557  1.247   gdamore 	(void) CSR_READ_1(regsp, COM_REG_IIR);
    558  1.131      marc 
    559  1.131      marc #ifdef COM_HAYESP
    560  1.131      marc 	/* Look for a Hayes ESP board. */
    561  1.209   thorpej 	if (sc->sc_type == COM_TYPE_HAYESP) {
    562  1.131      marc 
    563  1.131      marc 		/* Set 16550 compatibility mode */
    564  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
    565  1.131      marc 				  HAYESP_SETMODE);
    566  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
    567  1.131      marc 				  HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
    568  1.131      marc 				  HAYESP_MODE_SCALE);
    569  1.131      marc 
    570  1.131      marc 		/* Set RTS/CTS flow control */
    571  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
    572  1.131      marc 				  HAYESP_SETFLOWTYPE);
    573  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
    574  1.131      marc 				  HAYESP_FLOW_RTS);
    575  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
    576  1.131      marc 				  HAYESP_FLOW_CTS);
    577  1.131      marc 
    578  1.131      marc 		/* Set flow control levels */
    579  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
    580  1.131      marc 				  HAYESP_SETRXFLOW);
    581  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
    582  1.131      marc 				  HAYESP_HIBYTE(HAYESP_RXHIWMARK));
    583  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
    584  1.131      marc 				  HAYESP_LOBYTE(HAYESP_RXHIWMARK));
    585  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
    586  1.131      marc 				  HAYESP_HIBYTE(HAYESP_RXLOWMARK));
    587  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
    588  1.131      marc 				  HAYESP_LOBYTE(HAYESP_RXLOWMARK));
    589  1.131      marc 	}
    590  1.131      marc #endif
    591  1.131      marc 
    592  1.186       uwe 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE|COM_HW_KGDB))
    593  1.131      marc 		com_enable_debugport(sc);
    594    1.1       cgd }
    595    1.1       cgd 
    596  1.149   thorpej int
    597  1.256  christos com_detach(struct device *self, int flags)
    598  1.149   thorpej {
    599  1.149   thorpej 	struct com_softc *sc = (struct com_softc *)self;
    600  1.149   thorpej 	int maj, mn;
    601  1.149   thorpej 
    602  1.272    dyoung         if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    603  1.272    dyoung 		return EBUSY;
    604  1.272    dyoung 
    605  1.149   thorpej 	/* locate the major number */
    606  1.199   gehenna 	maj = cdevsw_lookup_major(&com_cdevsw);
    607  1.149   thorpej 
    608  1.149   thorpej 	/* Nuke the vnodes for any open instances. */
    609  1.242   thorpej 	mn = device_unit(self);
    610  1.149   thorpej 	vdevgone(maj, mn, mn, VCHR);
    611  1.149   thorpej 
    612  1.149   thorpej 	mn |= COMDIALOUT_MASK;
    613  1.149   thorpej 	vdevgone(maj, mn, mn, VCHR);
    614  1.149   thorpej 
    615  1.196  christos 	if (sc->sc_rbuf == NULL) {
    616  1.196  christos 		/*
    617  1.196  christos 		 * Ring buffer allocation failed in the com_attach_subr,
    618  1.196  christos 		 * only the tty is allocated, and nothing else.
    619  1.196  christos 		 */
    620  1.196  christos 		ttyfree(sc->sc_tty);
    621  1.196  christos 		return 0;
    622  1.196  christos 	}
    623  1.232     perry 
    624  1.149   thorpej 	/* Free the receive buffer. */
    625  1.149   thorpej 	free(sc->sc_rbuf, M_DEVBUF);
    626  1.149   thorpej 
    627  1.149   thorpej 	/* Detach and free the tty. */
    628  1.149   thorpej 	tty_detach(sc->sc_tty);
    629  1.149   thorpej 	ttyfree(sc->sc_tty);
    630  1.149   thorpej 
    631  1.149   thorpej 	/* Unhook the soft interrupt handler. */
    632  1.263        ad 	softint_disestablish(sc->sc_si);
    633  1.149   thorpej 
    634  1.149   thorpej #if NRND > 0 && defined(RND_COM)
    635  1.149   thorpej 	/* Unhook the entropy source. */
    636  1.149   thorpej 	rnd_detach_source(&sc->rnd_source);
    637  1.149   thorpej #endif
    638  1.273    dyoung 	mutex_destroy(&sc->sc_lock);
    639  1.273    dyoung 	callout_destroy(&sc->sc_diag_callout);
    640  1.149   thorpej 
    641  1.271        ad 	/* Destroy the lock. */
    642  1.271        ad 	mutex_destroy(&sc->sc_lock);
    643  1.271        ad 
    644  1.149   thorpej 	return (0);
    645  1.149   thorpej }
    646  1.149   thorpej 
    647  1.149   thorpej int
    648  1.197    simonb com_activate(struct device *self, enum devact act)
    649  1.149   thorpej {
    650  1.149   thorpej 	struct com_softc *sc = (struct com_softc *)self;
    651  1.263        ad 	int rv = 0;
    652  1.149   thorpej 
    653  1.149   thorpej 	switch (act) {
    654  1.149   thorpej 	case DVACT_ACTIVATE:
    655  1.149   thorpej 		rv = EOPNOTSUPP;
    656  1.149   thorpej 		break;
    657  1.149   thorpej 
    658  1.149   thorpej 	case DVACT_DEACTIVATE:
    659  1.149   thorpej 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
    660  1.149   thorpej 			rv = EBUSY;
    661  1.149   thorpej 			break;
    662  1.149   thorpej 		}
    663  1.149   thorpej 
    664  1.149   thorpej 		if (sc->disable != NULL && sc->enabled != 0) {
    665  1.149   thorpej 			(*sc->disable)(sc);
    666  1.149   thorpej 			sc->enabled = 0;
    667  1.149   thorpej 		}
    668  1.149   thorpej 		break;
    669  1.149   thorpej 	}
    670  1.179  sommerfe 
    671  1.149   thorpej 	return (rv);
    672  1.149   thorpej }
    673  1.149   thorpej 
    674  1.141   mycroft void
    675  1.197    simonb com_shutdown(struct com_softc *sc)
    676  1.141   mycroft {
    677  1.141   mycroft 	struct tty *tp = sc->sc_tty;
    678  1.141   mycroft 
    679  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
    680  1.141   mycroft 
    681  1.141   mycroft 	/* If we were asserting flow control, then deassert it. */
    682  1.141   mycroft 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    683  1.141   mycroft 	com_hwiflow(sc);
    684  1.141   mycroft 
    685  1.141   mycroft 	/* Clear any break condition set with TIOCSBRK. */
    686  1.141   mycroft 	com_break(sc, 0);
    687  1.141   mycroft 
    688  1.141   mycroft 	/*
    689  1.141   mycroft 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    690  1.141   mycroft 	 * notice even if we immediately open the port again.
    691  1.175  sommerfe 	 * Avoid tsleeping above splhigh().
    692  1.141   mycroft 	 */
    693  1.141   mycroft 	if (ISSET(tp->t_cflag, HUPCL)) {
    694  1.141   mycroft 		com_modem(sc, 0);
    695  1.263        ad 		mutex_spin_exit(&sc->sc_lock);
    696  1.263        ad 		/* XXX will only timeout */
    697  1.263        ad 		(void) kpause(ttclos, false, hz, NULL);
    698  1.263        ad 		mutex_spin_enter(&sc->sc_lock);
    699  1.141   mycroft 	}
    700  1.141   mycroft 
    701  1.141   mycroft 	/* Turn off interrupts. */
    702  1.208       scw 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    703  1.141   mycroft 		sc->sc_ier = IER_ERXRDY; /* interrupt on break */
    704  1.209   thorpej 		if (sc->sc_type == COM_TYPE_PXA2x0)
    705  1.208       scw 			sc->sc_ier |= IER_ERXTOUT;
    706  1.208       scw 	} else
    707  1.141   mycroft 		sc->sc_ier = 0;
    708  1.208       scw 
    709  1.209   thorpej 	if (sc->sc_type == COM_TYPE_PXA2x0)
    710  1.208       scw 		sc->sc_ier |= IER_EUART;
    711  1.208       scw 
    712  1.247   gdamore 	CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
    713  1.141   mycroft 
    714  1.269        ad 	mutex_spin_exit(&sc->sc_lock);
    715  1.269        ad 
    716  1.141   mycroft 	if (sc->disable) {
    717  1.141   mycroft #ifdef DIAGNOSTIC
    718  1.141   mycroft 		if (!sc->enabled)
    719  1.141   mycroft 			panic("com_shutdown: not enabled?");
    720  1.141   mycroft #endif
    721  1.141   mycroft 		(*sc->disable)(sc);
    722  1.141   mycroft 		sc->enabled = 0;
    723  1.141   mycroft 	}
    724  1.141   mycroft }
    725  1.141   mycroft 
    726   1.21   mycroft int
    727  1.256  christos comopen(dev_t dev, int flag, int mode, struct lwp *l)
    728    1.1       cgd {
    729   1.21   mycroft 	struct com_softc *sc;
    730   1.21   mycroft 	struct tty *tp;
    731  1.263        ad 	int s;
    732  1.142   mycroft 	int error;
    733  1.173   thorpej 
    734  1.173   thorpej 	sc = device_lookup(&com_cd, COMUNIT(dev));
    735  1.173   thorpej 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    736  1.177       eeh 		sc->sc_rbuf == NULL)
    737   1.99   mycroft 		return (ENXIO);
    738   1.21   mycroft 
    739  1.241   thorpej 	if (!device_is_active(&sc->sc_dev))
    740  1.149   thorpej 		return (ENXIO);
    741  1.149   thorpej 
    742  1.102   thorpej #ifdef KGDB
    743  1.102   thorpej 	/*
    744  1.102   thorpej 	 * If this is the kgdb port, no other use is permitted.
    745  1.102   thorpej 	 */
    746  1.102   thorpej 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    747  1.102   thorpej 		return (EBUSY);
    748  1.102   thorpej #endif
    749  1.102   thorpej 
    750  1.120   mycroft 	tp = sc->sc_tty;
    751   1.21   mycroft 
    752  1.253      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    753   1.99   mycroft 		return (EBUSY);
    754   1.99   mycroft 
    755   1.99   mycroft 	s = spltty();
    756   1.99   mycroft 
    757   1.99   mycroft 	/*
    758   1.99   mycroft 	 * Do the following iff this is a first open.
    759   1.99   mycroft 	 */
    760  1.141   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    761   1.99   mycroft 		struct termios t;
    762   1.99   mycroft 
    763  1.127   mycroft 		tp->t_dev = dev;
    764  1.127   mycroft 
    765  1.120   mycroft 
    766  1.131      marc 		if (sc->enable) {
    767  1.131      marc 			if ((*sc->enable)(sc)) {
    768  1.134     enami 				splx(s);
    769  1.131      marc 				printf("%s: device enable failed\n",
    770  1.131      marc 				       sc->sc_dev.dv_xname);
    771  1.131      marc 				return (EIO);
    772  1.131      marc 			}
    773  1.269        ad 			mutex_spin_enter(&sc->sc_lock);
    774  1.131      marc 			sc->enabled = 1;
    775  1.131      marc 			com_config(sc);
    776  1.269        ad 		} else {
    777  1.269        ad 			mutex_spin_enter(&sc->sc_lock);
    778  1.131      marc 		}
    779  1.131      marc 
    780   1.99   mycroft 		/* Turn on interrupts. */
    781   1.99   mycroft 		sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
    782  1.209   thorpej 		if (sc->sc_type == COM_TYPE_PXA2x0)
    783  1.208       scw 			sc->sc_ier |= IER_EUART | IER_ERXTOUT;
    784  1.247   gdamore 		CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
    785   1.99   mycroft 
    786   1.99   mycroft 		/* Fetch the current modem control status, needed later. */
    787  1.247   gdamore 		sc->sc_msr = CSR_READ_1(&sc->sc_regs, COM_REG_MSR);
    788   1.99   mycroft 
    789  1.144  jonathan 		/* Clear PPS capture state on first open. */
    790  1.244    kardel 		memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
    791  1.244    kardel 		sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
    792  1.244    kardel 		pps_init(&sc->sc_pps_state);
    793  1.144  jonathan 
    794  1.263        ad 		mutex_spin_exit(&sc->sc_lock);
    795   1.99   mycroft 
    796   1.99   mycroft 		/*
    797   1.99   mycroft 		 * Initialize the termios status to the defaults.  Add in the
    798   1.99   mycroft 		 * sticky bits from TIOCSFLAGS.
    799   1.99   mycroft 		 */
    800   1.98   mycroft 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    801   1.99   mycroft 			t.c_ospeed = comconsrate;
    802   1.99   mycroft 			t.c_cflag = comconscflag;
    803   1.98   mycroft 		} else {
    804   1.99   mycroft 			t.c_ospeed = TTYDEF_SPEED;
    805   1.99   mycroft 			t.c_cflag = TTYDEF_CFLAG;
    806   1.98   mycroft 		}
    807  1.237       dsl 		t.c_ispeed = t.c_ospeed;
    808   1.99   mycroft 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    809   1.99   mycroft 			SET(t.c_cflag, CLOCAL);
    810   1.99   mycroft 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    811   1.99   mycroft 			SET(t.c_cflag, CRTSCTS);
    812   1.99   mycroft 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    813   1.99   mycroft 			SET(t.c_cflag, MDMBUF);
    814  1.129   mycroft 		/* Make sure comparam() will do something. */
    815  1.129   mycroft 		tp->t_ospeed = 0;
    816  1.120   mycroft 		(void) comparam(tp, &t);
    817   1.99   mycroft 		tp->t_iflag = TTYDEF_IFLAG;
    818   1.99   mycroft 		tp->t_oflag = TTYDEF_OFLAG;
    819   1.16        ws 		tp->t_lflag = TTYDEF_LFLAG;
    820   1.99   mycroft 		ttychars(tp);
    821    1.1       cgd 		ttsetwater(tp);
    822   1.21   mycroft 
    823  1.263        ad 		mutex_spin_enter(&sc->sc_lock);
    824  1.136   mycroft 
    825   1.99   mycroft 		/*
    826   1.99   mycroft 		 * Turn on DTR.  We must always do this, even if carrier is not
    827   1.99   mycroft 		 * present, because otherwise we'd have to use TIOCSDTR
    828  1.121   mycroft 		 * immediately after setting CLOCAL, which applications do not
    829  1.121   mycroft 		 * expect.  We always assert DTR while the device is open
    830  1.121   mycroft 		 * unless explicitly requested to deassert it.
    831   1.99   mycroft 		 */
    832   1.99   mycroft 		com_modem(sc, 1);
    833   1.65  christos 
    834   1.99   mycroft 		/* Clear the input ring, and unblock. */
    835  1.127   mycroft 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    836  1.127   mycroft 		sc->sc_rbavail = com_rbuf_size;
    837   1.99   mycroft 		com_iflush(sc);
    838  1.101   mycroft 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    839  1.101   mycroft 		com_hwiflow(sc);
    840   1.65  christos 
    841   1.99   mycroft #ifdef COM_DEBUG
    842  1.101   mycroft 		if (com_debug)
    843  1.101   mycroft 			comstatus(sc, "comopen  ");
    844   1.65  christos #endif
    845   1.21   mycroft 
    846  1.263        ad 		mutex_spin_exit(&sc->sc_lock);
    847   1.99   mycroft 	}
    848  1.232     perry 
    849  1.143   mycroft 	splx(s);
    850   1.21   mycroft 
    851  1.143   mycroft 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    852  1.143   mycroft 	if (error)
    853  1.143   mycroft 		goto bad;
    854  1.141   mycroft 
    855  1.181       eeh 	error = (*tp->t_linesw->l_open)(dev, tp);
    856  1.139     enami 	if (error)
    857  1.141   mycroft 		goto bad;
    858  1.139     enami 
    859  1.141   mycroft 	return (0);
    860  1.139     enami 
    861  1.141   mycroft bad:
    862  1.141   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    863  1.141   mycroft 		/*
    864  1.141   mycroft 		 * We failed to open the device, and nobody else had it opened.
    865  1.141   mycroft 		 * Clean up the state as appropriate.
    866  1.141   mycroft 		 */
    867  1.141   mycroft 		com_shutdown(sc);
    868  1.141   mycroft 	}
    869  1.139     enami 
    870   1.99   mycroft 	return (error);
    871    1.1       cgd }
    872  1.232     perry 
    873   1.21   mycroft int
    874  1.256  christos comclose(dev_t dev, int flag, int mode, struct lwp *l)
    875    1.1       cgd {
    876  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
    877   1.50   mycroft 	struct tty *tp = sc->sc_tty;
    878   1.57   mycroft 
    879   1.57   mycroft 	/* XXX This is for cons.c. */
    880   1.62   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN))
    881   1.99   mycroft 		return (0);
    882   1.21   mycroft 
    883  1.181       eeh 	(*tp->t_linesw->l_close)(tp, flag);
    884    1.1       cgd 	ttyclose(tp);
    885   1.99   mycroft 
    886  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
    887  1.149   thorpej 		return (0);
    888  1.149   thorpej 
    889  1.143   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    890  1.143   mycroft 		/*
    891  1.143   mycroft 		 * Although we got a last close, the device may still be in
    892  1.143   mycroft 		 * use; e.g. if this was the dialout node, and there are still
    893  1.143   mycroft 		 * processes waiting for carrier on the non-dialout node.
    894  1.143   mycroft 		 */
    895  1.143   mycroft 		com_shutdown(sc);
    896  1.143   mycroft 	}
    897  1.120   mycroft 
    898   1.99   mycroft 	return (0);
    899    1.1       cgd }
    900  1.232     perry 
    901   1.21   mycroft int
    902  1.197    simonb comread(dev_t dev, struct uio *uio, int flag)
    903    1.1       cgd {
    904  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
    905   1.52   mycroft 	struct tty *tp = sc->sc_tty;
    906  1.149   thorpej 
    907  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
    908  1.149   thorpej 		return (EIO);
    909  1.232     perry 
    910  1.181       eeh 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    911    1.1       cgd }
    912  1.232     perry 
    913   1.21   mycroft int
    914  1.197    simonb comwrite(dev_t dev, struct uio *uio, int flag)
    915    1.1       cgd {
    916  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
    917   1.52   mycroft 	struct tty *tp = sc->sc_tty;
    918  1.149   thorpej 
    919  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
    920  1.149   thorpej 		return (EIO);
    921  1.232     perry 
    922  1.181       eeh 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    923  1.184       scw }
    924  1.184       scw 
    925  1.184       scw int
    926  1.238  christos compoll(dev_t dev, int events, struct lwp *l)
    927  1.184       scw {
    928  1.184       scw 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
    929  1.184       scw 	struct tty *tp = sc->sc_tty;
    930  1.184       scw 
    931  1.184       scw 	if (COM_ISALIVE(sc) == 0)
    932  1.234        ws 		return (POLLHUP);
    933  1.232     perry 
    934  1.238  christos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    935    1.1       cgd }
    936   1.50   mycroft 
    937   1.50   mycroft struct tty *
    938  1.197    simonb comtty(dev_t dev)
    939   1.50   mycroft {
    940  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
    941   1.52   mycroft 	struct tty *tp = sc->sc_tty;
    942   1.50   mycroft 
    943   1.52   mycroft 	return (tp);
    944   1.50   mycroft }
    945  1.111  christos 
    946   1.21   mycroft int
    947  1.259  christos comioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    948    1.1       cgd {
    949  1.273    dyoung 	struct com_softc *sc;
    950  1.273    dyoung 	struct tty *tp;
    951   1.21   mycroft 	int error;
    952   1.21   mycroft 
    953  1.273    dyoung 	sc = device_lookup(&com_cd, COMUNIT(dev));
    954  1.273    dyoung 	if (sc == NULL)
    955  1.273    dyoung 		return ENXIO;
    956  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
    957  1.149   thorpej 		return (EIO);
    958  1.149   thorpej 
    959  1.273    dyoung 	tp = sc->sc_tty;
    960  1.273    dyoung 
    961  1.238  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    962  1.194    atatat 	if (error != EPASSTHROUGH)
    963   1.99   mycroft 		return (error);
    964   1.99   mycroft 
    965  1.238  christos 	error = ttioctl(tp, cmd, data, flag, l);
    966  1.194    atatat 	if (error != EPASSTHROUGH)
    967   1.99   mycroft 		return (error);
    968  1.138   mycroft 
    969  1.138   mycroft 	error = 0;
    970  1.249      elad 	switch (cmd) {
    971  1.249      elad 	case TIOCSFLAGS:
    972  1.254      elad 		error = kauth_authorize_device_tty(l->l_cred,
    973  1.254      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    974  1.249      elad 		break;
    975  1.249      elad 	default:
    976  1.249      elad 		/* nothing */
    977  1.249      elad 		break;
    978  1.249      elad 	}
    979  1.249      elad 	if (error) {
    980  1.249      elad 		return error;
    981  1.249      elad 	}
    982    1.1       cgd 
    983  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
    984  1.136   mycroft 
    985    1.1       cgd 	switch (cmd) {
    986    1.1       cgd 	case TIOCSBRK:
    987   1.99   mycroft 		com_break(sc, 1);
    988    1.1       cgd 		break;
    989   1.99   mycroft 
    990    1.1       cgd 	case TIOCCBRK:
    991   1.99   mycroft 		com_break(sc, 0);
    992    1.1       cgd 		break;
    993   1.99   mycroft 
    994    1.1       cgd 	case TIOCSDTR:
    995   1.99   mycroft 		com_modem(sc, 1);
    996    1.1       cgd 		break;
    997   1.99   mycroft 
    998    1.1       cgd 	case TIOCCDTR:
    999   1.99   mycroft 		com_modem(sc, 0);
   1000    1.1       cgd 		break;
   1001   1.99   mycroft 
   1002   1.99   mycroft 	case TIOCGFLAGS:
   1003   1.99   mycroft 		*(int *)data = sc->sc_swflags;
   1004   1.99   mycroft 		break;
   1005   1.99   mycroft 
   1006   1.99   mycroft 	case TIOCSFLAGS:
   1007   1.99   mycroft 		sc->sc_swflags = *(int *)data;
   1008   1.99   mycroft 		break;
   1009   1.99   mycroft 
   1010    1.1       cgd 	case TIOCMSET:
   1011    1.1       cgd 	case TIOCMBIS:
   1012    1.1       cgd 	case TIOCMBIC:
   1013  1.153   mycroft 		tiocm_to_com(sc, cmd, *(int *)data);
   1014  1.111  christos 		break;
   1015  1.111  christos 
   1016  1.153   mycroft 	case TIOCMGET:
   1017  1.153   mycroft 		*(int *)data = com_to_tiocm(sc);
   1018  1.111  christos 		break;
   1019  1.144  jonathan 
   1020  1.244    kardel 	case PPS_IOC_CREATE:
   1021  1.244    kardel 	case PPS_IOC_DESTROY:
   1022  1.244    kardel 	case PPS_IOC_GETPARAMS:
   1023  1.244    kardel 	case PPS_IOC_SETPARAMS:
   1024  1.244    kardel 	case PPS_IOC_GETCAP:
   1025  1.244    kardel 	case PPS_IOC_FETCH:
   1026  1.244    kardel #ifdef PPS_SYNC
   1027  1.244    kardel 	case PPS_IOC_KCBIND:
   1028  1.244    kardel #endif
   1029  1.244    kardel 		error = pps_ioctl(cmd, data, &sc->sc_pps_state);
   1030  1.244    kardel 		break;
   1031  1.224    simonb 
   1032  1.144  jonathan 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
   1033  1.244    kardel #ifndef PPS_TRAILING_EDGE
   1034  1.244    kardel 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1035  1.244    kardel 		    &sc->sc_pps_state.ppsinfo.assert_timestamp);
   1036  1.244    kardel #else
   1037  1.244    kardel 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1038  1.244    kardel 		    &sc->sc_pps_state.ppsinfo.clear_timestamp);
   1039  1.244    kardel #endif
   1040  1.144  jonathan 		break;
   1041  1.144  jonathan 
   1042   1.99   mycroft 	default:
   1043  1.194    atatat 		error = EPASSTHROUGH;
   1044  1.136   mycroft 		break;
   1045   1.21   mycroft 	}
   1046   1.22       cgd 
   1047  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1048  1.136   mycroft 
   1049   1.99   mycroft #ifdef COM_DEBUG
   1050  1.101   mycroft 	if (com_debug)
   1051   1.99   mycroft 		comstatus(sc, "comioctl ");
   1052   1.99   mycroft #endif
   1053   1.99   mycroft 
   1054  1.136   mycroft 	return (error);
   1055   1.99   mycroft }
   1056   1.99   mycroft 
   1057  1.101   mycroft integrate void
   1058  1.197    simonb com_schedrx(struct com_softc *sc)
   1059  1.101   mycroft {
   1060  1.101   mycroft 
   1061  1.101   mycroft 	sc->sc_rx_ready = 1;
   1062  1.101   mycroft 
   1063  1.101   mycroft 	/* Wake up the poller. */
   1064  1.263        ad 	softint_schedule(sc->sc_si);
   1065  1.101   mycroft }
   1066  1.101   mycroft 
   1067   1.99   mycroft void
   1068  1.197    simonb com_break(struct com_softc *sc, int onoff)
   1069   1.99   mycroft {
   1070   1.99   mycroft 
   1071   1.99   mycroft 	if (onoff)
   1072   1.99   mycroft 		SET(sc->sc_lcr, LCR_SBREAK);
   1073   1.99   mycroft 	else
   1074   1.99   mycroft 		CLR(sc->sc_lcr, LCR_SBREAK);
   1075   1.22       cgd 
   1076   1.99   mycroft 	if (!sc->sc_heldchange) {
   1077   1.99   mycroft 		if (sc->sc_tx_busy) {
   1078   1.99   mycroft 			sc->sc_heldtbc = sc->sc_tbc;
   1079   1.99   mycroft 			sc->sc_tbc = 0;
   1080   1.99   mycroft 			sc->sc_heldchange = 1;
   1081   1.99   mycroft 		} else
   1082   1.99   mycroft 			com_loadchannelregs(sc);
   1083   1.22       cgd 	}
   1084   1.99   mycroft }
   1085   1.22       cgd 
   1086   1.99   mycroft void
   1087  1.197    simonb com_modem(struct com_softc *sc, int onoff)
   1088   1.99   mycroft {
   1089   1.22       cgd 
   1090  1.153   mycroft 	if (sc->sc_mcr_dtr == 0)
   1091  1.153   mycroft 		return;
   1092  1.153   mycroft 
   1093   1.99   mycroft 	if (onoff)
   1094   1.99   mycroft 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1095   1.99   mycroft 	else
   1096   1.99   mycroft 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1097   1.22       cgd 
   1098   1.99   mycroft 	if (!sc->sc_heldchange) {
   1099   1.99   mycroft 		if (sc->sc_tx_busy) {
   1100   1.99   mycroft 			sc->sc_heldtbc = sc->sc_tbc;
   1101   1.99   mycroft 			sc->sc_tbc = 0;
   1102   1.99   mycroft 			sc->sc_heldchange = 1;
   1103   1.99   mycroft 		} else
   1104   1.99   mycroft 			com_loadchannelregs(sc);
   1105   1.22       cgd 	}
   1106  1.153   mycroft }
   1107  1.153   mycroft 
   1108  1.153   mycroft void
   1109  1.197    simonb tiocm_to_com(struct com_softc *sc, u_long how, int ttybits)
   1110  1.153   mycroft {
   1111  1.153   mycroft 	u_char combits;
   1112  1.153   mycroft 
   1113  1.153   mycroft 	combits = 0;
   1114  1.153   mycroft 	if (ISSET(ttybits, TIOCM_DTR))
   1115  1.153   mycroft 		SET(combits, MCR_DTR);
   1116  1.153   mycroft 	if (ISSET(ttybits, TIOCM_RTS))
   1117  1.153   mycroft 		SET(combits, MCR_RTS);
   1118  1.232     perry 
   1119  1.153   mycroft 	switch (how) {
   1120  1.153   mycroft 	case TIOCMBIC:
   1121  1.153   mycroft 		CLR(sc->sc_mcr, combits);
   1122  1.153   mycroft 		break;
   1123  1.153   mycroft 
   1124  1.153   mycroft 	case TIOCMBIS:
   1125  1.153   mycroft 		SET(sc->sc_mcr, combits);
   1126  1.153   mycroft 		break;
   1127  1.153   mycroft 
   1128  1.153   mycroft 	case TIOCMSET:
   1129  1.153   mycroft 		CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
   1130  1.153   mycroft 		SET(sc->sc_mcr, combits);
   1131  1.153   mycroft 		break;
   1132  1.153   mycroft 	}
   1133  1.153   mycroft 
   1134  1.153   mycroft 	if (!sc->sc_heldchange) {
   1135  1.153   mycroft 		if (sc->sc_tx_busy) {
   1136  1.153   mycroft 			sc->sc_heldtbc = sc->sc_tbc;
   1137  1.153   mycroft 			sc->sc_tbc = 0;
   1138  1.153   mycroft 			sc->sc_heldchange = 1;
   1139  1.153   mycroft 		} else
   1140  1.153   mycroft 			com_loadchannelregs(sc);
   1141  1.153   mycroft 	}
   1142  1.153   mycroft }
   1143  1.153   mycroft 
   1144  1.153   mycroft int
   1145  1.197    simonb com_to_tiocm(struct com_softc *sc)
   1146  1.153   mycroft {
   1147  1.153   mycroft 	u_char combits;
   1148  1.153   mycroft 	int ttybits = 0;
   1149  1.153   mycroft 
   1150  1.153   mycroft 	combits = sc->sc_mcr;
   1151  1.153   mycroft 	if (ISSET(combits, MCR_DTR))
   1152  1.153   mycroft 		SET(ttybits, TIOCM_DTR);
   1153  1.153   mycroft 	if (ISSET(combits, MCR_RTS))
   1154  1.153   mycroft 		SET(ttybits, TIOCM_RTS);
   1155  1.153   mycroft 
   1156  1.153   mycroft 	combits = sc->sc_msr;
   1157  1.153   mycroft 	if (ISSET(combits, MSR_DCD))
   1158  1.153   mycroft 		SET(ttybits, TIOCM_CD);
   1159  1.153   mycroft 	if (ISSET(combits, MSR_CTS))
   1160  1.153   mycroft 		SET(ttybits, TIOCM_CTS);
   1161  1.153   mycroft 	if (ISSET(combits, MSR_DSR))
   1162  1.153   mycroft 		SET(ttybits, TIOCM_DSR);
   1163  1.153   mycroft 	if (ISSET(combits, MSR_RI | MSR_TERI))
   1164  1.153   mycroft 		SET(ttybits, TIOCM_RI);
   1165  1.153   mycroft 
   1166  1.228   mycroft 	if (ISSET(sc->sc_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC))
   1167  1.153   mycroft 		SET(ttybits, TIOCM_LE);
   1168  1.153   mycroft 
   1169  1.153   mycroft 	return (ttybits);
   1170    1.1       cgd }
   1171    1.1       cgd 
   1172  1.106  drochner static u_char
   1173  1.197    simonb cflag2lcr(tcflag_t cflag)
   1174  1.106  drochner {
   1175  1.106  drochner 	u_char lcr = 0;
   1176  1.106  drochner 
   1177  1.106  drochner 	switch (ISSET(cflag, CSIZE)) {
   1178  1.127   mycroft 	case CS5:
   1179  1.106  drochner 		SET(lcr, LCR_5BITS);
   1180  1.106  drochner 		break;
   1181  1.127   mycroft 	case CS6:
   1182  1.106  drochner 		SET(lcr, LCR_6BITS);
   1183  1.106  drochner 		break;
   1184  1.127   mycroft 	case CS7:
   1185  1.106  drochner 		SET(lcr, LCR_7BITS);
   1186  1.106  drochner 		break;
   1187  1.127   mycroft 	case CS8:
   1188  1.106  drochner 		SET(lcr, LCR_8BITS);
   1189  1.106  drochner 		break;
   1190  1.106  drochner 	}
   1191  1.106  drochner 	if (ISSET(cflag, PARENB)) {
   1192  1.106  drochner 		SET(lcr, LCR_PENAB);
   1193  1.106  drochner 		if (!ISSET(cflag, PARODD))
   1194  1.106  drochner 			SET(lcr, LCR_PEVEN);
   1195  1.106  drochner 	}
   1196  1.106  drochner 	if (ISSET(cflag, CSTOPB))
   1197  1.106  drochner 		SET(lcr, LCR_STOPB);
   1198  1.106  drochner 
   1199  1.110     enami 	return (lcr);
   1200  1.106  drochner }
   1201  1.106  drochner 
   1202   1.21   mycroft int
   1203  1.197    simonb comparam(struct tty *tp, struct termios *t)
   1204    1.1       cgd {
   1205  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1206  1.188     enami 	int ospeed;
   1207   1.62   mycroft 	u_char lcr;
   1208   1.21   mycroft 
   1209  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
   1210  1.149   thorpej 		return (EIO);
   1211  1.149   thorpej 
   1212  1.188     enami #ifdef COM_HAYESP
   1213  1.209   thorpej 	if (sc->sc_type == COM_TYPE_HAYESP) {
   1214  1.188     enami 		int prescaler, speed;
   1215  1.188     enami 
   1216  1.188     enami 		/*
   1217  1.188     enami 		 * Calculate UART clock prescaler.  It should be in
   1218  1.188     enami 		 * range of 0 .. 3.
   1219  1.188     enami 		 */
   1220  1.188     enami 		for (prescaler = 0, speed = t->c_ospeed; prescaler < 4;
   1221  1.188     enami 		    prescaler++, speed /= 2)
   1222  1.210   thorpej 			if ((ospeed = comspeed(speed, sc->sc_frequency,
   1223  1.210   thorpej 					       sc->sc_type)) > 0)
   1224  1.188     enami 				break;
   1225  1.188     enami 
   1226  1.188     enami 		if (prescaler == 4)
   1227  1.188     enami 			return (EINVAL);
   1228  1.188     enami 		sc->sc_prescaler = prescaler;
   1229  1.188     enami 	} else
   1230  1.188     enami #endif
   1231  1.210   thorpej 	ospeed = comspeed(t->c_ospeed, sc->sc_frequency, sc->sc_type);
   1232  1.188     enami 
   1233  1.127   mycroft 	/* Check requested parameters. */
   1234   1.99   mycroft 	if (ospeed < 0)
   1235   1.99   mycroft 		return (EINVAL);
   1236   1.99   mycroft 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1237   1.99   mycroft 		return (EINVAL);
   1238   1.21   mycroft 
   1239   1.99   mycroft 	/*
   1240   1.99   mycroft 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1241   1.99   mycroft 	 * is always active.
   1242   1.99   mycroft 	 */
   1243   1.99   mycroft 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1244   1.99   mycroft 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1245   1.99   mycroft 		SET(t->c_cflag, CLOCAL);
   1246   1.99   mycroft 		CLR(t->c_cflag, HUPCL);
   1247   1.62   mycroft 	}
   1248  1.129   mycroft 
   1249  1.129   mycroft 	/*
   1250  1.129   mycroft 	 * If there were no changes, don't do anything.  This avoids dropping
   1251  1.129   mycroft 	 * input and improves performance when all we did was frob things like
   1252  1.129   mycroft 	 * VMIN and VTIME.
   1253  1.129   mycroft 	 */
   1254  1.129   mycroft 	if (tp->t_ospeed == t->c_ospeed &&
   1255  1.129   mycroft 	    tp->t_cflag == t->c_cflag)
   1256  1.129   mycroft 		return (0);
   1257  1.126   mycroft 
   1258  1.126   mycroft 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
   1259  1.126   mycroft 
   1260  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
   1261  1.126   mycroft 
   1262  1.126   mycroft 	sc->sc_lcr = lcr;
   1263   1.36   mycroft 
   1264   1.36   mycroft 	/*
   1265   1.99   mycroft 	 * If we're not in a mode that assumes a connection is present, then
   1266   1.99   mycroft 	 * ignore carrier changes.
   1267   1.36   mycroft 	 */
   1268   1.99   mycroft 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1269   1.99   mycroft 		sc->sc_msr_dcd = 0;
   1270   1.99   mycroft 	else
   1271   1.99   mycroft 		sc->sc_msr_dcd = MSR_DCD;
   1272   1.99   mycroft 	/*
   1273   1.99   mycroft 	 * Set the flow control pins depending on the current flow control
   1274   1.99   mycroft 	 * mode.
   1275   1.99   mycroft 	 */
   1276   1.99   mycroft 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1277   1.99   mycroft 		sc->sc_mcr_dtr = MCR_DTR;
   1278   1.99   mycroft 		sc->sc_mcr_rts = MCR_RTS;
   1279   1.99   mycroft 		sc->sc_msr_cts = MSR_CTS;
   1280  1.116      fvdl 		sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
   1281   1.99   mycroft 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1282   1.99   mycroft 		/*
   1283   1.99   mycroft 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1284   1.99   mycroft 		 * carrier detection.
   1285   1.99   mycroft 		 */
   1286   1.99   mycroft 		sc->sc_mcr_dtr = 0;
   1287   1.99   mycroft 		sc->sc_mcr_rts = MCR_DTR;
   1288   1.99   mycroft 		sc->sc_msr_cts = MSR_DCD;
   1289  1.116      fvdl 		sc->sc_efr = 0;
   1290   1.99   mycroft 	} else {
   1291   1.99   mycroft 		/*
   1292   1.99   mycroft 		 * If no flow control, then always set RTS.  This will make
   1293   1.99   mycroft 		 * the other side happy if it mistakenly thinks we're doing
   1294   1.99   mycroft 		 * RTS/CTS flow control.
   1295   1.99   mycroft 		 */
   1296   1.99   mycroft 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
   1297   1.99   mycroft 		sc->sc_mcr_rts = 0;
   1298   1.99   mycroft 		sc->sc_msr_cts = 0;
   1299  1.116      fvdl 		sc->sc_efr = 0;
   1300   1.99   mycroft 		if (ISSET(sc->sc_mcr, MCR_DTR))
   1301   1.99   mycroft 			SET(sc->sc_mcr, MCR_RTS);
   1302   1.99   mycroft 		else
   1303   1.99   mycroft 			CLR(sc->sc_mcr, MCR_RTS);
   1304   1.99   mycroft 	}
   1305   1.99   mycroft 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1306   1.99   mycroft 
   1307   1.95   mycroft #if 0
   1308   1.99   mycroft 	if (ospeed == 0)
   1309   1.99   mycroft 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1310   1.99   mycroft 	else
   1311   1.99   mycroft 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1312   1.66   mycroft #endif
   1313   1.66   mycroft 
   1314   1.99   mycroft 	sc->sc_dlbl = ospeed;
   1315   1.99   mycroft 	sc->sc_dlbh = ospeed >> 8;
   1316   1.66   mycroft 
   1317   1.99   mycroft 	/*
   1318   1.99   mycroft 	 * Set the FIFO threshold based on the receive speed.
   1319   1.99   mycroft 	 *
   1320   1.99   mycroft 	 *  * If it's a low speed, it's probably a mouse or some other
   1321   1.99   mycroft 	 *    interactive device, so set the threshold low.
   1322   1.99   mycroft 	 *  * If it's a high speed, trim the trigger level down to prevent
   1323   1.99   mycroft 	 *    overflows.
   1324   1.99   mycroft 	 *  * Otherwise set it a bit higher.
   1325   1.99   mycroft 	 */
   1326  1.209   thorpej 	if (sc->sc_type == COM_TYPE_HAYESP)
   1327   1.99   mycroft 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
   1328   1.99   mycroft 	else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
   1329   1.99   mycroft 		sc->sc_fifo = FIFO_ENABLE |
   1330  1.230   mycroft 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8);
   1331   1.99   mycroft 	else
   1332   1.99   mycroft 		sc->sc_fifo = 0;
   1333   1.21   mycroft 
   1334  1.127   mycroft 	/* And copy to tty. */
   1335  1.240       dsl 	tp->t_ispeed = t->c_ospeed;
   1336   1.57   mycroft 	tp->t_ospeed = t->c_ospeed;
   1337   1.57   mycroft 	tp->t_cflag = t->c_cflag;
   1338   1.25       cgd 
   1339   1.99   mycroft 	if (!sc->sc_heldchange) {
   1340   1.99   mycroft 		if (sc->sc_tx_busy) {
   1341   1.99   mycroft 			sc->sc_heldtbc = sc->sc_tbc;
   1342   1.99   mycroft 			sc->sc_tbc = 0;
   1343   1.99   mycroft 			sc->sc_heldchange = 1;
   1344   1.99   mycroft 		} else
   1345   1.99   mycroft 			com_loadchannelregs(sc);
   1346   1.99   mycroft 	}
   1347   1.99   mycroft 
   1348  1.124   mycroft 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1349  1.125   mycroft 		/* Disable the high water mark. */
   1350  1.125   mycroft 		sc->sc_r_hiwat = 0;
   1351  1.125   mycroft 		sc->sc_r_lowat = 0;
   1352  1.124   mycroft 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1353  1.124   mycroft 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1354  1.124   mycroft 			com_schedrx(sc);
   1355  1.124   mycroft 		}
   1356  1.124   mycroft 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1357  1.124   mycroft 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1358  1.124   mycroft 			com_hwiflow(sc);
   1359  1.124   mycroft 		}
   1360  1.125   mycroft 	} else {
   1361  1.127   mycroft 		sc->sc_r_hiwat = com_rbuf_hiwat;
   1362  1.127   mycroft 		sc->sc_r_lowat = com_rbuf_lowat;
   1363  1.124   mycroft 	}
   1364  1.124   mycroft 
   1365  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1366   1.99   mycroft 
   1367   1.25       cgd 	/*
   1368   1.99   mycroft 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1369  1.124   mycroft 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1370  1.124   mycroft 	 * explicit request.
   1371   1.25       cgd 	 */
   1372  1.181       eeh 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
   1373   1.99   mycroft 
   1374   1.99   mycroft #ifdef COM_DEBUG
   1375  1.101   mycroft 	if (com_debug)
   1376  1.101   mycroft 		comstatus(sc, "comparam ");
   1377   1.99   mycroft #endif
   1378   1.99   mycroft 
   1379   1.99   mycroft 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1380   1.99   mycroft 		if (sc->sc_tx_stopped) {
   1381   1.99   mycroft 			sc->sc_tx_stopped = 0;
   1382   1.99   mycroft 			comstart(tp);
   1383   1.99   mycroft 		}
   1384   1.21   mycroft 	}
   1385    1.1       cgd 
   1386   1.99   mycroft 	return (0);
   1387   1.99   mycroft }
   1388   1.99   mycroft 
   1389   1.99   mycroft void
   1390  1.197    simonb com_iflush(struct com_softc *sc)
   1391   1.99   mycroft {
   1392  1.247   gdamore 	struct com_regs	*regsp = &sc->sc_regs;
   1393  1.131      marc #ifdef DIAGNOSTIC
   1394  1.131      marc 	int reg;
   1395  1.131      marc #endif
   1396  1.131      marc 	int timo;
   1397   1.99   mycroft 
   1398  1.131      marc #ifdef DIAGNOSTIC
   1399  1.131      marc 	reg = 0xffff;
   1400  1.131      marc #endif
   1401  1.131      marc 	timo = 50000;
   1402   1.99   mycroft 	/* flush any pending I/O */
   1403  1.247   gdamore 	while (ISSET(CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY)
   1404  1.131      marc 	    && --timo)
   1405  1.131      marc #ifdef DIAGNOSTIC
   1406  1.131      marc 		reg =
   1407  1.131      marc #else
   1408  1.131      marc 		    (void)
   1409  1.131      marc #endif
   1410  1.247   gdamore 		    CSR_READ_1(regsp, COM_REG_RXDATA);
   1411  1.131      marc #ifdef DIAGNOSTIC
   1412  1.131      marc 	if (!timo)
   1413  1.131      marc 		printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1414  1.131      marc 		       reg);
   1415  1.131      marc #endif
   1416   1.99   mycroft }
   1417   1.99   mycroft 
   1418   1.99   mycroft void
   1419  1.197    simonb com_loadchannelregs(struct com_softc *sc)
   1420   1.99   mycroft {
   1421  1.247   gdamore 	struct com_regs *regsp = &sc->sc_regs;
   1422   1.99   mycroft 
   1423   1.99   mycroft 	/* XXXXX necessary? */
   1424   1.99   mycroft 	com_iflush(sc);
   1425   1.99   mycroft 
   1426  1.209   thorpej 	if (sc->sc_type == COM_TYPE_PXA2x0)
   1427  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_IER, IER_EUART);
   1428  1.208       scw 	else
   1429  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_IER, 0);
   1430   1.99   mycroft 
   1431  1.116      fvdl 	if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
   1432  1.247   gdamore 		if (sc->sc_type != COM_TYPE_AU1x00) {	/* no EFR on alchemy */
   1433  1.247   gdamore 			CSR_WRITE_1(regsp, COM_REG_EFR, sc->sc_efr);
   1434  1.247   gdamore 			CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
   1435  1.247   gdamore 		}
   1436  1.116      fvdl 	}
   1437  1.247   gdamore 	if (sc->sc_type == COM_TYPE_AU1x00) {
   1438  1.247   gdamore 		/* alchemy has single separate 16-bit clock divisor register */
   1439  1.247   gdamore 		CSR_WRITE_2(regsp, COM_REG_DLBL, sc->sc_dlbl +
   1440  1.247   gdamore 		    (sc->sc_dlbh << 8));
   1441  1.247   gdamore 	} else {
   1442  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr | LCR_DLAB);
   1443  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_DLBL, sc->sc_dlbl);
   1444  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_DLBH, sc->sc_dlbh);
   1445  1.247   gdamore 	}
   1446  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr);
   1447  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr_active = sc->sc_mcr);
   1448  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_FIFO, sc->sc_fifo);
   1449  1.188     enami #ifdef COM_HAYESP
   1450  1.209   thorpej 	if (sc->sc_type == COM_TYPE_HAYESP) {
   1451  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
   1452  1.188     enami 		    HAYESP_SETPRESCALER);
   1453  1.258      cube 		bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
   1454  1.188     enami 		    sc->sc_prescaler);
   1455  1.188     enami 	}
   1456  1.188     enami #endif
   1457   1.99   mycroft 
   1458  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
   1459   1.99   mycroft }
   1460   1.99   mycroft 
   1461   1.99   mycroft int
   1462  1.197    simonb comhwiflow(struct tty *tp, int block)
   1463   1.99   mycroft {
   1464  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1465   1.99   mycroft 
   1466  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
   1467  1.149   thorpej 		return (0);
   1468  1.149   thorpej 
   1469   1.99   mycroft 	if (sc->sc_mcr_rts == 0)
   1470   1.99   mycroft 		return (0);
   1471   1.99   mycroft 
   1472  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
   1473  1.232     perry 
   1474   1.99   mycroft 	if (block) {
   1475  1.101   mycroft 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1476  1.101   mycroft 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1477  1.101   mycroft 			com_hwiflow(sc);
   1478  1.101   mycroft 		}
   1479   1.99   mycroft 	} else {
   1480  1.101   mycroft 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1481  1.101   mycroft 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1482  1.101   mycroft 			com_schedrx(sc);
   1483  1.101   mycroft 		}
   1484  1.101   mycroft 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1485  1.101   mycroft 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1486  1.101   mycroft 			com_hwiflow(sc);
   1487  1.101   mycroft 		}
   1488   1.99   mycroft 	}
   1489  1.179  sommerfe 
   1490  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1491   1.99   mycroft 	return (1);
   1492   1.99   mycroft }
   1493  1.232     perry 
   1494   1.99   mycroft /*
   1495   1.99   mycroft  * (un)block input via hw flowcontrol
   1496   1.99   mycroft  */
   1497   1.99   mycroft void
   1498  1.197    simonb com_hwiflow(struct com_softc *sc)
   1499   1.99   mycroft {
   1500  1.247   gdamore 	struct com_regs *regsp= &sc->sc_regs;
   1501   1.99   mycroft 
   1502   1.99   mycroft 	if (sc->sc_mcr_rts == 0)
   1503   1.99   mycroft 		return;
   1504   1.99   mycroft 
   1505  1.101   mycroft 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1506   1.99   mycroft 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1507   1.99   mycroft 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1508   1.99   mycroft 	} else {
   1509   1.99   mycroft 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1510   1.99   mycroft 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1511   1.99   mycroft 	}
   1512  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr_active);
   1513    1.1       cgd }
   1514   1.21   mycroft 
   1515   1.99   mycroft 
   1516   1.12   deraadt void
   1517  1.197    simonb comstart(struct tty *tp)
   1518    1.1       cgd {
   1519  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1520  1.247   gdamore 	struct com_regs *regsp = &sc->sc_regs;
   1521   1.21   mycroft 	int s;
   1522   1.21   mycroft 
   1523  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
   1524  1.149   thorpej 		return;
   1525  1.149   thorpej 
   1526    1.1       cgd 	s = spltty();
   1527  1.178       eeh 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1528   1.70   mycroft 		goto out;
   1529  1.178       eeh 	if (sc->sc_tx_stopped)
   1530  1.127   mycroft 		goto out;
   1531  1.266        ad 	if (!ttypull(tp))
   1532  1.266        ad 		goto out;
   1533   1.99   mycroft 
   1534   1.99   mycroft 	/* Grab the first contiguous region of buffer space. */
   1535   1.99   mycroft 	{
   1536   1.99   mycroft 		u_char *tba;
   1537   1.99   mycroft 		int tbc;
   1538   1.99   mycroft 
   1539   1.99   mycroft 		tba = tp->t_outq.c_cf;
   1540   1.99   mycroft 		tbc = ndqb(&tp->t_outq, 0);
   1541   1.99   mycroft 
   1542  1.263        ad 		mutex_spin_enter(&sc->sc_lock);
   1543   1.99   mycroft 
   1544   1.99   mycroft 		sc->sc_tba = tba;
   1545   1.99   mycroft 		sc->sc_tbc = tbc;
   1546   1.99   mycroft 	}
   1547   1.99   mycroft 
   1548   1.62   mycroft 	SET(tp->t_state, TS_BUSY);
   1549   1.99   mycroft 	sc->sc_tx_busy = 1;
   1550   1.64  christos 
   1551   1.99   mycroft 	/* Enable transmit completion interrupts if necessary. */
   1552   1.70   mycroft 	if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
   1553   1.70   mycroft 		SET(sc->sc_ier, IER_ETXRDY);
   1554  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
   1555   1.70   mycroft 	}
   1556   1.99   mycroft 
   1557   1.99   mycroft 	/* Output the first chunk of the contiguous buffer. */
   1558  1.195   thorpej 	if (!ISSET(sc->sc_hwflags, COM_HW_NO_TXPRELOAD)) {
   1559  1.201   thorpej 		u_int n;
   1560   1.99   mycroft 
   1561  1.127   mycroft 		n = sc->sc_tbc;
   1562  1.127   mycroft 		if (n > sc->sc_fifolen)
   1563  1.127   mycroft 			n = sc->sc_fifolen;
   1564  1.247   gdamore 		CSR_WRITE_MULTI(regsp, COM_REG_TXDATA, sc->sc_tba, n);
   1565   1.99   mycroft 		sc->sc_tbc -= n;
   1566   1.99   mycroft 		sc->sc_tba += n;
   1567   1.64  christos 	}
   1568  1.233       tls 
   1569  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1570   1.99   mycroft out:
   1571    1.1       cgd 	splx(s);
   1572   1.99   mycroft 	return;
   1573    1.1       cgd }
   1574   1.21   mycroft 
   1575    1.1       cgd /*
   1576    1.1       cgd  * Stop output on a line.
   1577    1.1       cgd  */
   1578   1.85   mycroft void
   1579  1.256  christos comstop(struct tty *tp, int flag)
   1580    1.1       cgd {
   1581  1.173   thorpej 	struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
   1582    1.1       cgd 
   1583  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
   1584   1.99   mycroft 	if (ISSET(tp->t_state, TS_BUSY)) {
   1585   1.99   mycroft 		/* Stop transmitting at the next chunk. */
   1586   1.99   mycroft 		sc->sc_tbc = 0;
   1587   1.99   mycroft 		sc->sc_heldtbc = 0;
   1588   1.62   mycroft 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1589   1.62   mycroft 			SET(tp->t_state, TS_FLUSH);
   1590   1.99   mycroft 	}
   1591  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1592    1.1       cgd }
   1593    1.1       cgd 
   1594   1.33   mycroft void
   1595  1.197    simonb comdiag(void *arg)
   1596   1.33   mycroft {
   1597   1.33   mycroft 	struct com_softc *sc = arg;
   1598   1.99   mycroft 	int overflows, floods;
   1599   1.33   mycroft 
   1600  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
   1601   1.33   mycroft 	overflows = sc->sc_overflows;
   1602   1.33   mycroft 	sc->sc_overflows = 0;
   1603   1.57   mycroft 	floods = sc->sc_floods;
   1604   1.57   mycroft 	sc->sc_floods = 0;
   1605   1.99   mycroft 	sc->sc_errors = 0;
   1606  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1607   1.57   mycroft 
   1608  1.127   mycroft 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1609   1.57   mycroft 	    sc->sc_dev.dv_xname,
   1610   1.57   mycroft 	    overflows, overflows == 1 ? "" : "s",
   1611   1.99   mycroft 	    floods, floods == 1 ? "" : "s");
   1612   1.57   mycroft }
   1613   1.57   mycroft 
   1614   1.99   mycroft integrate void
   1615  1.197    simonb com_rxsoft(struct com_softc *sc, struct tty *tp)
   1616   1.99   mycroft {
   1617  1.198    simonb 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1618  1.127   mycroft 	u_char *get, *end;
   1619  1.127   mycroft 	u_int cc, scc;
   1620  1.127   mycroft 	u_char lsr;
   1621  1.127   mycroft 	int code;
   1622   1.57   mycroft 
   1623  1.127   mycroft 	end = sc->sc_ebuf;
   1624   1.99   mycroft 	get = sc->sc_rbget;
   1625  1.127   mycroft 	scc = cc = com_rbuf_size - sc->sc_rbavail;
   1626   1.99   mycroft 
   1627  1.127   mycroft 	if (cc == com_rbuf_size) {
   1628   1.99   mycroft 		sc->sc_floods++;
   1629   1.99   mycroft 		if (sc->sc_errors++ == 0)
   1630  1.170   thorpej 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1631  1.170   thorpej 			    comdiag, sc);
   1632   1.99   mycroft 	}
   1633   1.99   mycroft 
   1634  1.205      gson 	/* If not yet open, drop the entire buffer content here */
   1635  1.205      gson 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1636  1.205      gson 		get += cc << 1;
   1637  1.205      gson 		if (get >= end)
   1638  1.205      gson 			get -= com_rbuf_size << 1;
   1639  1.205      gson 		cc = 0;
   1640  1.205      gson 	}
   1641  1.101   mycroft 	while (cc) {
   1642  1.128   mycroft 		code = get[0];
   1643  1.127   mycroft 		lsr = get[1];
   1644  1.128   mycroft 		if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
   1645  1.128   mycroft 			if (ISSET(lsr, LSR_OE)) {
   1646  1.128   mycroft 				sc->sc_overflows++;
   1647  1.128   mycroft 				if (sc->sc_errors++ == 0)
   1648  1.170   thorpej 					callout_reset(&sc->sc_diag_callout,
   1649  1.170   thorpej 					    60 * hz, comdiag, sc);
   1650  1.128   mycroft 			}
   1651  1.127   mycroft 			if (ISSET(lsr, LSR_BI | LSR_FE))
   1652  1.127   mycroft 				SET(code, TTY_FE);
   1653  1.127   mycroft 			if (ISSET(lsr, LSR_PE))
   1654  1.127   mycroft 				SET(code, TTY_PE);
   1655  1.127   mycroft 		}
   1656  1.127   mycroft 		if ((*rint)(code, tp) == -1) {
   1657  1.101   mycroft 			/*
   1658  1.101   mycroft 			 * The line discipline's buffer is out of space.
   1659  1.101   mycroft 			 */
   1660  1.101   mycroft 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1661  1.101   mycroft 				/*
   1662  1.101   mycroft 				 * We're either not using flow control, or the
   1663  1.101   mycroft 				 * line discipline didn't tell us to block for
   1664  1.101   mycroft 				 * some reason.  Either way, we have no way to
   1665  1.101   mycroft 				 * know when there's more space available, so
   1666  1.101   mycroft 				 * just drop the rest of the data.
   1667  1.101   mycroft 				 */
   1668  1.127   mycroft 				get += cc << 1;
   1669  1.127   mycroft 				if (get >= end)
   1670  1.127   mycroft 					get -= com_rbuf_size << 1;
   1671  1.101   mycroft 				cc = 0;
   1672  1.101   mycroft 			} else {
   1673  1.101   mycroft 				/*
   1674  1.101   mycroft 				 * Don't schedule any more receive processing
   1675  1.101   mycroft 				 * until the line discipline tells us there's
   1676  1.101   mycroft 				 * space available (through comhwiflow()).
   1677  1.101   mycroft 				 * Leave the rest of the data in the input
   1678  1.101   mycroft 				 * buffer.
   1679  1.101   mycroft 				 */
   1680  1.101   mycroft 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1681  1.101   mycroft 			}
   1682  1.101   mycroft 			break;
   1683  1.101   mycroft 		}
   1684  1.127   mycroft 		get += 2;
   1685  1.127   mycroft 		if (get >= end)
   1686  1.127   mycroft 			get = sc->sc_rbuf;
   1687  1.101   mycroft 		cc--;
   1688   1.99   mycroft 	}
   1689   1.99   mycroft 
   1690  1.101   mycroft 	if (cc != scc) {
   1691  1.101   mycroft 		sc->sc_rbget = get;
   1692  1.263        ad 		mutex_spin_enter(&sc->sc_lock);
   1693  1.232     perry 
   1694  1.101   mycroft 		cc = sc->sc_rbavail += scc - cc;
   1695  1.101   mycroft 		/* Buffers should be ok again, release possible block. */
   1696  1.101   mycroft 		if (cc >= sc->sc_r_lowat) {
   1697  1.101   mycroft 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1698  1.101   mycroft 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1699  1.101   mycroft 				SET(sc->sc_ier, IER_ERXRDY);
   1700  1.208       scw #ifdef COM_PXA2X0
   1701  1.209   thorpej 				if (sc->sc_type == COM_TYPE_PXA2x0)
   1702  1.208       scw 					SET(sc->sc_ier, IER_ERXTOUT);
   1703  1.208       scw #endif
   1704  1.247   gdamore 				CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
   1705  1.101   mycroft 			}
   1706  1.101   mycroft 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1707  1.101   mycroft 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1708  1.101   mycroft 				com_hwiflow(sc);
   1709  1.101   mycroft 			}
   1710  1.101   mycroft 		}
   1711  1.263        ad 		mutex_spin_exit(&sc->sc_lock);
   1712   1.57   mycroft 	}
   1713   1.99   mycroft }
   1714   1.99   mycroft 
   1715   1.99   mycroft integrate void
   1716  1.197    simonb com_txsoft(struct com_softc *sc, struct tty *tp)
   1717   1.99   mycroft {
   1718   1.33   mycroft 
   1719   1.99   mycroft 	CLR(tp->t_state, TS_BUSY);
   1720   1.99   mycroft 	if (ISSET(tp->t_state, TS_FLUSH))
   1721   1.99   mycroft 		CLR(tp->t_state, TS_FLUSH);
   1722   1.99   mycroft 	else
   1723   1.99   mycroft 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1724  1.181       eeh 	(*tp->t_linesw->l_start)(tp);
   1725   1.99   mycroft }
   1726   1.57   mycroft 
   1727   1.99   mycroft integrate void
   1728  1.197    simonb com_stsoft(struct com_softc *sc, struct tty *tp)
   1729   1.99   mycroft {
   1730   1.99   mycroft 	u_char msr, delta;
   1731   1.57   mycroft 
   1732  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
   1733   1.99   mycroft 	msr = sc->sc_msr;
   1734   1.99   mycroft 	delta = sc->sc_msr_delta;
   1735   1.99   mycroft 	sc->sc_msr_delta = 0;
   1736  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1737   1.57   mycroft 
   1738   1.99   mycroft 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1739   1.99   mycroft 		/*
   1740   1.99   mycroft 		 * Inform the tty layer that carrier detect changed.
   1741   1.99   mycroft 		 */
   1742  1.181       eeh 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
   1743   1.99   mycroft 	}
   1744   1.61   mycroft 
   1745   1.99   mycroft 	if (ISSET(delta, sc->sc_msr_cts)) {
   1746   1.99   mycroft 		/* Block or unblock output according to flow control. */
   1747   1.99   mycroft 		if (ISSET(msr, sc->sc_msr_cts)) {
   1748   1.99   mycroft 			sc->sc_tx_stopped = 0;
   1749  1.181       eeh 			(*tp->t_linesw->l_start)(tp);
   1750   1.99   mycroft 		} else {
   1751   1.99   mycroft 			sc->sc_tx_stopped = 1;
   1752   1.61   mycroft 		}
   1753   1.99   mycroft 	}
   1754   1.99   mycroft 
   1755   1.99   mycroft #ifdef COM_DEBUG
   1756  1.101   mycroft 	if (com_debug)
   1757  1.127   mycroft 		comstatus(sc, "com_stsoft");
   1758   1.99   mycroft #endif
   1759   1.99   mycroft }
   1760   1.99   mycroft 
   1761   1.99   mycroft void
   1762  1.197    simonb comsoft(void *arg)
   1763   1.99   mycroft {
   1764   1.99   mycroft 	struct com_softc *sc = arg;
   1765   1.99   mycroft 	struct tty *tp;
   1766   1.99   mycroft 
   1767  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
   1768  1.131      marc 		return;
   1769  1.131      marc 
   1770  1.261        ad 	tp = sc->sc_tty;
   1771   1.99   mycroft 
   1772  1.261        ad 	if (sc->sc_rx_ready) {
   1773  1.261        ad 		sc->sc_rx_ready = 0;
   1774  1.261        ad 		com_rxsoft(sc, tp);
   1775  1.261        ad 	}
   1776   1.57   mycroft 
   1777  1.261        ad 	if (sc->sc_st_check) {
   1778  1.261        ad 		sc->sc_st_check = 0;
   1779  1.261        ad 		com_stsoft(sc, tp);
   1780  1.261        ad 	}
   1781   1.57   mycroft 
   1782  1.261        ad 	if (sc->sc_tx_done) {
   1783  1.261        ad 		sc->sc_tx_done = 0;
   1784  1.261        ad 		com_txsoft(sc, tp);
   1785   1.57   mycroft 	}
   1786   1.21   mycroft }
   1787  1.140      ross 
   1788   1.21   mycroft int
   1789  1.197    simonb comintr(void *arg)
   1790   1.21   mycroft {
   1791   1.49       cgd 	struct com_softc *sc = arg;
   1792  1.247   gdamore 	struct com_regs *regsp = &sc->sc_regs;
   1793  1.247   gdamore 
   1794  1.127   mycroft 	u_char *put, *end;
   1795  1.127   mycroft 	u_int cc;
   1796  1.127   mycroft 	u_char lsr, iir;
   1797   1.55   mycroft 
   1798  1.149   thorpej 	if (COM_ISALIVE(sc) == 0)
   1799  1.131      marc 		return (0);
   1800  1.131      marc 
   1801  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
   1802  1.247   gdamore 	iir = CSR_READ_1(regsp, COM_REG_IIR);
   1803  1.179  sommerfe 	if (ISSET(iir, IIR_NOPEND)) {
   1804  1.263        ad 		mutex_spin_exit(&sc->sc_lock);
   1805   1.55   mycroft 		return (0);
   1806  1.179  sommerfe 	}
   1807   1.21   mycroft 
   1808  1.127   mycroft 	end = sc->sc_ebuf;
   1809   1.99   mycroft 	put = sc->sc_rbput;
   1810   1.99   mycroft 	cc = sc->sc_rbavail;
   1811   1.99   mycroft 
   1812  1.189    briggs again:	do {
   1813  1.168  jonathan 		u_char	msr, delta;
   1814   1.21   mycroft 
   1815  1.247   gdamore 		lsr = CSR_READ_1(regsp, COM_REG_LSR);
   1816  1.103  drochner 		if (ISSET(lsr, LSR_BI)) {
   1817  1.186       uwe 			int cn_trapped = 0;
   1818  1.207      fvdl 
   1819  1.186       uwe 			cn_check_magic(sc->sc_tty->t_dev,
   1820  1.186       uwe 				       CNC_BREAK, com_cnm_state);
   1821  1.186       uwe 			if (cn_trapped)
   1822  1.103  drochner 				continue;
   1823  1.206    briggs #if defined(KGDB) && !defined(DDB)
   1824  1.103  drochner 			if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
   1825  1.103  drochner 				kgdb_connect(1);
   1826  1.103  drochner 				continue;
   1827  1.103  drochner 			}
   1828  1.103  drochner #endif
   1829  1.102   thorpej 		}
   1830  1.102   thorpej 
   1831  1.101   mycroft 		if (ISSET(lsr, LSR_RCV_MASK) &&
   1832  1.101   mycroft 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1833  1.127   mycroft 			while (cc > 0) {
   1834  1.186       uwe 				int cn_trapped = 0;
   1835  1.247   gdamore 				put[0] = CSR_READ_1(regsp, COM_REG_RXDATA);
   1836  1.127   mycroft 				put[1] = lsr;
   1837  1.186       uwe 				cn_check_magic(sc->sc_tty->t_dev,
   1838  1.186       uwe 					       put[0], com_cnm_state);
   1839  1.229   mycroft 				if (cn_trapped)
   1840  1.229   mycroft 					goto next;
   1841  1.127   mycroft 				put += 2;
   1842  1.127   mycroft 				if (put >= end)
   1843  1.127   mycroft 					put = sc->sc_rbuf;
   1844  1.127   mycroft 				cc--;
   1845  1.229   mycroft 			next:
   1846  1.247   gdamore 				lsr = CSR_READ_1(regsp, COM_REG_LSR);
   1847  1.127   mycroft 				if (!ISSET(lsr, LSR_RCV_MASK))
   1848  1.127   mycroft 					break;
   1849   1.99   mycroft 			}
   1850  1.127   mycroft 
   1851   1.99   mycroft 			/*
   1852   1.99   mycroft 			 * Current string of incoming characters ended because
   1853  1.127   mycroft 			 * no more data was available or we ran out of space.
   1854  1.127   mycroft 			 * Schedule a receive event if any data was received.
   1855  1.127   mycroft 			 * If we're out of space, turn off receive interrupts.
   1856   1.99   mycroft 			 */
   1857   1.99   mycroft 			sc->sc_rbput = put;
   1858   1.99   mycroft 			sc->sc_rbavail = cc;
   1859  1.101   mycroft 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1860  1.101   mycroft 				sc->sc_rx_ready = 1;
   1861  1.127   mycroft 
   1862   1.99   mycroft 			/*
   1863   1.99   mycroft 			 * See if we are in danger of overflowing a buffer. If
   1864   1.99   mycroft 			 * so, use hardware flow control to ease the pressure.
   1865   1.99   mycroft 			 */
   1866  1.101   mycroft 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1867   1.99   mycroft 			    cc < sc->sc_r_hiwat) {
   1868  1.101   mycroft 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1869  1.101   mycroft 				com_hwiflow(sc);
   1870   1.99   mycroft 			}
   1871  1.127   mycroft 
   1872   1.99   mycroft 			/*
   1873  1.101   mycroft 			 * If we're out of space, disable receive interrupts
   1874  1.101   mycroft 			 * until the queue has drained a bit.
   1875   1.99   mycroft 			 */
   1876   1.99   mycroft 			if (!cc) {
   1877  1.101   mycroft 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1878  1.208       scw #ifdef COM_PXA2X0
   1879  1.209   thorpej 				if (sc->sc_type == COM_TYPE_PXA2x0)
   1880  1.229   mycroft 					CLR(sc->sc_ier, IER_ERXRDY|IER_ERXTOUT);
   1881  1.229   mycroft 				else
   1882  1.208       scw #endif
   1883  1.229   mycroft 					CLR(sc->sc_ier, IER_ERXRDY);
   1884  1.247   gdamore 				CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
   1885   1.99   mycroft 			}
   1886   1.88   mycroft 		} else {
   1887  1.228   mycroft 			if ((iir & (IIR_RXRDY|IIR_TXRDY)) == IIR_RXRDY) {
   1888  1.247   gdamore 				(void) CSR_READ_1(regsp, COM_REG_RXDATA);
   1889   1.88   mycroft 				continue;
   1890   1.88   mycroft 			}
   1891   1.88   mycroft 		}
   1892   1.55   mycroft 
   1893  1.247   gdamore 		msr = CSR_READ_1(regsp, COM_REG_MSR);
   1894   1.99   mycroft 		delta = msr ^ sc->sc_msr;
   1895   1.99   mycroft 		sc->sc_msr = msr;
   1896  1.244    kardel 		if ((sc->sc_pps_state.ppsparam.mode & PPS_CAPTUREBOTH) &&
   1897  1.244    kardel 		    (delta & MSR_DCD)) {
   1898  1.244    kardel 			pps_capture(&sc->sc_pps_state);
   1899  1.244    kardel 			pps_event(&sc->sc_pps_state,
   1900  1.244    kardel 			    (msr & MSR_DCD) ?
   1901  1.244    kardel 			    PPS_CAPTUREASSERT :
   1902  1.244    kardel 			    PPS_CAPTURECLEAR);
   1903  1.244    kardel 		}
   1904  1.168  jonathan 
   1905  1.167  jonathan 		/*
   1906  1.167  jonathan 		 * Process normal status changes
   1907  1.167  jonathan 		 */
   1908  1.167  jonathan 		if (ISSET(delta, sc->sc_msr_mask)) {
   1909  1.167  jonathan 			SET(sc->sc_msr_delta, delta);
   1910   1.99   mycroft 
   1911   1.99   mycroft 			/*
   1912   1.99   mycroft 			 * Stop output immediately if we lose the output
   1913   1.99   mycroft 			 * flow control signal or carrier detect.
   1914   1.99   mycroft 			 */
   1915   1.99   mycroft 			if (ISSET(~msr, sc->sc_msr_mask)) {
   1916   1.99   mycroft 				sc->sc_tbc = 0;
   1917   1.99   mycroft 				sc->sc_heldtbc = 0;
   1918   1.69   mycroft #ifdef COM_DEBUG
   1919  1.101   mycroft 				if (com_debug)
   1920  1.101   mycroft 					comstatus(sc, "comintr  ");
   1921   1.69   mycroft #endif
   1922   1.99   mycroft 			}
   1923   1.55   mycroft 
   1924   1.99   mycroft 			sc->sc_st_check = 1;
   1925   1.55   mycroft 		}
   1926  1.225     enami 	} while (!ISSET((iir =
   1927  1.247   gdamore 	    CSR_READ_1(regsp, COM_REG_IIR)), IIR_NOPEND) &&
   1928  1.225     enami 	    /*
   1929  1.225     enami 	     * Since some device (e.g., ST16C1550) doesn't clear IIR_TXRDY
   1930  1.225     enami 	     * by IIR read, so we can't do this way: `process all interrupts,
   1931  1.225     enami 	     * then do TX if possble'.
   1932  1.225     enami 	     */
   1933  1.225     enami 	    (iir & IIR_IMASK) != IIR_TXRDY);
   1934   1.55   mycroft 
   1935   1.99   mycroft 	/*
   1936  1.225     enami 	 * Read LSR again, since there may be an interrupt between
   1937  1.225     enami 	 * the last LSR read and IIR read above.
   1938  1.225     enami 	 */
   1939  1.247   gdamore 	lsr = CSR_READ_1(regsp, COM_REG_LSR);
   1940  1.225     enami 
   1941  1.225     enami 	/*
   1942  1.225     enami 	 * See if data can be transmitted as well.
   1943  1.225     enami 	 * Schedule tx done event if no data left
   1944   1.99   mycroft 	 * and tty was marked busy.
   1945   1.99   mycroft 	 */
   1946   1.99   mycroft 	if (ISSET(lsr, LSR_TXRDY)) {
   1947   1.99   mycroft 		/*
   1948   1.99   mycroft 		 * If we've delayed a parameter change, do it now, and restart
   1949   1.99   mycroft 		 * output.
   1950   1.99   mycroft 		 */
   1951   1.99   mycroft 		if (sc->sc_heldchange) {
   1952   1.99   mycroft 			com_loadchannelregs(sc);
   1953   1.99   mycroft 			sc->sc_heldchange = 0;
   1954   1.99   mycroft 			sc->sc_tbc = sc->sc_heldtbc;
   1955   1.99   mycroft 			sc->sc_heldtbc = 0;
   1956   1.99   mycroft 		}
   1957  1.127   mycroft 
   1958   1.99   mycroft 		/* Output the next chunk of the contiguous buffer, if any. */
   1959   1.99   mycroft 		if (sc->sc_tbc > 0) {
   1960  1.201   thorpej 			u_int n;
   1961   1.99   mycroft 
   1962  1.127   mycroft 			n = sc->sc_tbc;
   1963  1.127   mycroft 			if (n > sc->sc_fifolen)
   1964  1.127   mycroft 				n = sc->sc_fifolen;
   1965  1.247   gdamore 			CSR_WRITE_MULTI(regsp, COM_REG_TXDATA, sc->sc_tba, n);
   1966   1.99   mycroft 			sc->sc_tbc -= n;
   1967   1.99   mycroft 			sc->sc_tba += n;
   1968  1.127   mycroft 		} else {
   1969  1.127   mycroft 			/* Disable transmit completion interrupts if necessary. */
   1970  1.127   mycroft 			if (ISSET(sc->sc_ier, IER_ETXRDY)) {
   1971  1.127   mycroft 				CLR(sc->sc_ier, IER_ETXRDY);
   1972  1.247   gdamore 				CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
   1973  1.127   mycroft 			}
   1974  1.127   mycroft 			if (sc->sc_tx_busy) {
   1975  1.127   mycroft 				sc->sc_tx_busy = 0;
   1976  1.127   mycroft 				sc->sc_tx_done = 1;
   1977  1.127   mycroft 			}
   1978   1.62   mycroft 		}
   1979   1.99   mycroft 	}
   1980  1.189    briggs 
   1981  1.247   gdamore 	if (!ISSET((iir = CSR_READ_1(regsp, COM_REG_IIR)), IIR_NOPEND))
   1982  1.189    briggs 		goto again;
   1983  1.189    briggs 
   1984  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   1985   1.62   mycroft 
   1986   1.99   mycroft 	/* Wake up the poller. */
   1987  1.263        ad 	softint_schedule(sc->sc_si);
   1988  1.115  explorer 
   1989  1.115  explorer #if NRND > 0 && defined(RND_COM)
   1990  1.115  explorer 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1991  1.115  explorer #endif
   1992  1.115  explorer 
   1993   1.88   mycroft 	return (1);
   1994    1.1       cgd }
   1995    1.1       cgd 
   1996    1.1       cgd /*
   1997  1.102   thorpej  * The following functions are polled getc and putc routines, shared
   1998  1.102   thorpej  * by the console and kgdb glue.
   1999  1.232     perry  *
   2000  1.186       uwe  * The read-ahead code is so that you can detect pending in-band
   2001  1.186       uwe  * cn_magic in polled mode while doing output rather than having to
   2002  1.186       uwe  * wait until the kernel decides it needs input.
   2003  1.102   thorpej  */
   2004  1.102   thorpej 
   2005  1.186       uwe #define MAX_READAHEAD	20
   2006  1.186       uwe static int com_readahead[MAX_READAHEAD];
   2007  1.186       uwe static int com_readaheadcount = 0;
   2008  1.174     jeffs 
   2009  1.102   thorpej int
   2010  1.247   gdamore com_common_getc(dev_t dev, struct com_regs *regsp)
   2011  1.102   thorpej {
   2012  1.102   thorpej 	int s = splserial();
   2013  1.102   thorpej 	u_char stat, c;
   2014  1.102   thorpej 
   2015  1.174     jeffs 	/* got a character from reading things earlier */
   2016  1.186       uwe 	if (com_readaheadcount > 0) {
   2017  1.174     jeffs 		int i;
   2018  1.174     jeffs 
   2019  1.186       uwe 		c = com_readahead[0];
   2020  1.186       uwe 		for (i = 1; i < com_readaheadcount; i++) {
   2021  1.186       uwe 			com_readahead[i-1] = com_readahead[i];
   2022  1.174     jeffs 		}
   2023  1.186       uwe 		com_readaheadcount--;
   2024  1.174     jeffs 		splx(s);
   2025  1.174     jeffs 		return (c);
   2026  1.174     jeffs 	}
   2027  1.174     jeffs 
   2028  1.135   thorpej 	/* block until a character becomes available */
   2029  1.247   gdamore 	while (!ISSET(stat = CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY))
   2030  1.102   thorpej 		;
   2031  1.135   thorpej 
   2032  1.247   gdamore 	c = CSR_READ_1(regsp, COM_REG_RXDATA);
   2033  1.247   gdamore 	stat = CSR_READ_1(regsp, COM_REG_IIR);
   2034  1.186       uwe 	{
   2035  1.186       uwe 		int cn_trapped = 0; /* unused */
   2036  1.186       uwe #ifdef DDB
   2037  1.174     jeffs 		extern int db_active;
   2038  1.186       uwe 		if (!db_active)
   2039  1.186       uwe #endif
   2040  1.186       uwe 			cn_check_magic(dev, c, com_cnm_state);
   2041  1.174     jeffs 	}
   2042  1.102   thorpej 	splx(s);
   2043  1.102   thorpej 	return (c);
   2044  1.102   thorpej }
   2045  1.102   thorpej 
   2046  1.102   thorpej void
   2047  1.247   gdamore com_common_putc(dev_t dev, struct com_regs *regsp, int c)
   2048  1.102   thorpej {
   2049  1.102   thorpej 	int s = splserial();
   2050  1.204    simonb 	int cin, stat, timo;
   2051  1.174     jeffs 
   2052  1.232     perry 	if (com_readaheadcount < MAX_READAHEAD
   2053  1.247   gdamore 	     && ISSET(stat = CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY)) {
   2054  1.186       uwe 		int cn_trapped = 0;
   2055  1.247   gdamore 		cin = CSR_READ_1(regsp, COM_REG_RXDATA);
   2056  1.247   gdamore 		stat = CSR_READ_1(regsp, COM_REG_IIR);
   2057  1.186       uwe 		cn_check_magic(dev, cin, com_cnm_state);
   2058  1.186       uwe 		com_readahead[com_readaheadcount++] = cin;
   2059  1.174     jeffs 	}
   2060  1.102   thorpej 
   2061  1.102   thorpej 	/* wait for any pending transmission to finish */
   2062  1.161      ross 	timo = 150000;
   2063  1.247   gdamore 	while (!ISSET(CSR_READ_1(regsp, COM_REG_LSR), LSR_TXRDY) && --timo)
   2064  1.161      ross 		continue;
   2065  1.135   thorpej 
   2066  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_TXDATA, c);
   2067  1.247   gdamore 	COM_BARRIER(regsp, BR | BW);
   2068  1.160   thorpej 
   2069  1.157   mycroft 	splx(s);
   2070  1.102   thorpej }
   2071  1.102   thorpej 
   2072  1.102   thorpej /*
   2073  1.165  drochner  * Initialize UART for use as console or KGDB line.
   2074   1.99   mycroft  */
   2075  1.106  drochner int
   2076  1.247   gdamore cominit(struct com_regs *regsp, int rate, int frequency, int type,
   2077  1.247   gdamore     tcflag_t cflag)
   2078    1.1       cgd {
   2079  1.106  drochner 
   2080  1.247   gdamore 	if (bus_space_map(regsp->cr_iot, regsp->cr_iobase, regsp->cr_nports, 0,
   2081  1.247   gdamore 		&regsp->cr_ioh))
   2082  1.110     enami 		return (ENOMEM); /* ??? */
   2083    1.1       cgd 
   2084  1.210   thorpej 	rate = comspeed(rate, frequency, type);
   2085  1.247   gdamore 	if (type != COM_TYPE_AU1x00) {
   2086  1.247   gdamore 		/* no EFR on alchemy */
   2087  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
   2088  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_EFR, 0);
   2089  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_LCR, LCR_DLAB);
   2090  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_DLBL, rate & 0xff);
   2091  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_DLBH, rate >> 8);
   2092  1.247   gdamore 	} else {
   2093  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_DLBL, rate);
   2094  1.247   gdamore 	}
   2095  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_LCR, cflag2lcr(cflag));
   2096  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_MCR, MCR_DTR | MCR_RTS);
   2097  1.247   gdamore 	CSR_WRITE_1(regsp, COM_REG_FIFO,
   2098   1.87   mycroft 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
   2099  1.212       bsh #ifdef COM_PXA2X0
   2100  1.223    simonb 	if (type == COM_TYPE_PXA2x0)
   2101  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_IER, IER_EUART);
   2102  1.221    simonb 	else
   2103  1.212       bsh #endif
   2104  1.247   gdamore 		CSR_WRITE_1(regsp, COM_REG_IER, 0);
   2105  1.106  drochner 
   2106  1.110     enami 	return (0);
   2107   1.99   mycroft }
   2108   1.99   mycroft 
   2109   1.99   mycroft /*
   2110  1.106  drochner  * Following are all routines needed for COM to act as console
   2111   1.99   mycroft  */
   2112  1.177       eeh struct consdev comcons = {
   2113  1.203      matt 	NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL, NULL, NULL,
   2114  1.177       eeh 	NODEV, CN_NORMAL
   2115  1.177       eeh };
   2116  1.177       eeh 
   2117  1.106  drochner 
   2118  1.106  drochner int
   2119  1.247   gdamore comcnattach1(struct com_regs *regsp, int rate, int frequency, int type,
   2120  1.247   gdamore     tcflag_t cflag)
   2121   1.99   mycroft {
   2122  1.106  drochner 	int res;
   2123  1.106  drochner 
   2124  1.247   gdamore 	comconsregs = *regsp;
   2125  1.247   gdamore 
   2126  1.247   gdamore 	res = cominit(&comconsregs, rate, frequency, type, cflag);
   2127  1.110     enami 	if (res)
   2128  1.110     enami 		return (res);
   2129  1.106  drochner 
   2130  1.106  drochner 	cn_tab = &comcons;
   2131  1.186       uwe 	cn_init_magic(&com_cnm_state);
   2132  1.186       uwe 	cn_set_magic("\047\001"); /* default magic is BREAK */
   2133  1.106  drochner 
   2134  1.106  drochner 	comconsrate = rate;
   2135  1.106  drochner 	comconscflag = cflag;
   2136   1.99   mycroft 
   2137  1.110     enami 	return (0);
   2138    1.1       cgd }
   2139    1.1       cgd 
   2140   1.80  christos int
   2141  1.247   gdamore comcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
   2142  1.247   gdamore     int type, tcflag_t cflag)
   2143  1.247   gdamore {
   2144  1.247   gdamore 	struct com_regs	regs;
   2145  1.247   gdamore 
   2146  1.251       mrg 	memset(&regs, 0, sizeof regs);
   2147  1.247   gdamore 	regs.cr_iot = iot;
   2148  1.247   gdamore 	regs.cr_iobase = iobase;
   2149  1.247   gdamore 	regs.cr_nports = COM_NPORTS;
   2150  1.247   gdamore #ifdef	COM_REGMAP
   2151  1.247   gdamore 	memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
   2152  1.247   gdamore #endif
   2153  1.247   gdamore 
   2154  1.247   gdamore 	return comcnattach1(&regs, rate, frequency, type, cflag);
   2155  1.247   gdamore }
   2156  1.247   gdamore 
   2157  1.247   gdamore int
   2158  1.197    simonb comcngetc(dev_t dev)
   2159    1.1       cgd {
   2160  1.197    simonb 
   2161  1.247   gdamore 	return (com_common_getc(dev, &comconsregs));
   2162    1.1       cgd }
   2163    1.1       cgd 
   2164    1.1       cgd /*
   2165    1.1       cgd  * Console kernel output character routine.
   2166    1.1       cgd  */
   2167   1.48   mycroft void
   2168  1.197    simonb comcnputc(dev_t dev, int c)
   2169    1.1       cgd {
   2170  1.197    simonb 
   2171  1.247   gdamore 	com_common_putc(dev, &comconsregs, c);
   2172   1.37   mycroft }
   2173   1.37   mycroft 
   2174   1.37   mycroft void
   2175  1.256  christos comcnpollc(dev_t dev, int on)
   2176   1.37   mycroft {
   2177   1.37   mycroft 
   2178  1.106  drochner }
   2179  1.106  drochner 
   2180  1.106  drochner #ifdef KGDB
   2181  1.106  drochner int
   2182  1.247   gdamore com_kgdb_attach1(struct com_regs *regsp, int rate, int frequency, int type,
   2183  1.247   gdamore     tcflag_t cflag)
   2184  1.106  drochner {
   2185  1.106  drochner 	int res;
   2186  1.107  drochner 
   2187  1.247   gdamore 	if (regsp->cr_iot == comconsregs.cr_iot &&
   2188  1.247   gdamore 	    regsp->cr_iobase == comconsregs.cr_iobase) {
   2189  1.206    briggs #if !defined(DDB)
   2190  1.110     enami 		return (EBUSY); /* cannot share with console */
   2191  1.206    briggs #else
   2192  1.247   gdamore 		comkgdbregs = *regsp;
   2193  1.247   gdamore 		comkgdbregs.cr_ioh = comconsregs.cr_ioh;
   2194  1.206    briggs #endif
   2195  1.206    briggs 	} else {
   2196  1.247   gdamore 		comkgdbregs = *regsp;
   2197  1.247   gdamore 		res = cominit(&comkgdbregs, rate, frequency, type, cflag);
   2198  1.206    briggs 		if (res)
   2199  1.206    briggs 			return (res);
   2200  1.190      fvdl 
   2201  1.206    briggs 		/*
   2202  1.206    briggs 		 * XXXfvdl this shouldn't be needed, but the cn_magic goo
   2203  1.206    briggs 		 * expects this to be initialized
   2204  1.206    briggs 		 */
   2205  1.206    briggs 		cn_init_magic(&com_cnm_state);
   2206  1.206    briggs 		cn_set_magic("\047\001");
   2207  1.206    briggs 	}
   2208  1.106  drochner 
   2209  1.106  drochner 	kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
   2210  1.106  drochner 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2211  1.106  drochner 
   2212  1.247   gdamore 	return (0);
   2213  1.247   gdamore }
   2214  1.247   gdamore 
   2215  1.247   gdamore int
   2216  1.247   gdamore com_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate,
   2217  1.247   gdamore     int frequency, int type, tcflag_t cflag)
   2218  1.247   gdamore {
   2219  1.247   gdamore 	struct com_regs regs;
   2220  1.247   gdamore 
   2221  1.247   gdamore 	regs.cr_iot = iot;
   2222  1.247   gdamore 	regs.cr_nports = COM_NPORTS;
   2223  1.247   gdamore 	regs.cr_iobase = iobase;
   2224  1.247   gdamore #ifdef COM_REGMAP
   2225  1.247   gdamore 	memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
   2226  1.247   gdamore #endif
   2227  1.106  drochner 
   2228  1.247   gdamore 	return com_kgdb_attach1(&regs, rate, frequency, type, cflag);
   2229  1.106  drochner }
   2230  1.106  drochner 
   2231  1.106  drochner /* ARGSUSED */
   2232  1.106  drochner int
   2233  1.256  christos com_kgdb_getc(void *arg)
   2234  1.106  drochner {
   2235  1.197    simonb 
   2236  1.247   gdamore 	return (com_common_getc(NODEV, &comkgdbregs));
   2237  1.106  drochner }
   2238  1.106  drochner 
   2239  1.106  drochner /* ARGSUSED */
   2240  1.106  drochner void
   2241  1.256  christos com_kgdb_putc(void *arg, int c)
   2242  1.106  drochner {
   2243  1.197    simonb 
   2244  1.247   gdamore 	com_common_putc(NODEV, &comkgdbregs, c);
   2245  1.106  drochner }
   2246  1.106  drochner #endif /* KGDB */
   2247  1.106  drochner 
   2248  1.106  drochner /* helper function to identify the com ports used by
   2249  1.106  drochner  console or KGDB (and not yet autoconf attached) */
   2250  1.106  drochner int
   2251  1.197    simonb com_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh)
   2252  1.106  drochner {
   2253  1.106  drochner 	bus_space_handle_t help;
   2254  1.106  drochner 
   2255  1.110     enami 	if (!comconsattached &&
   2256  1.247   gdamore 	    iot == comconsregs.cr_iot && iobase == comconsregs.cr_iobase)
   2257  1.247   gdamore 		help = comconsregs.cr_ioh;
   2258  1.106  drochner #ifdef KGDB
   2259  1.110     enami 	else if (!com_kgdb_attached &&
   2260  1.247   gdamore 	    iot == comkgdbregs.cr_iot && iobase == comkgdbregs.cr_iobase)
   2261  1.247   gdamore 		help = comkgdbregs.cr_ioh;
   2262  1.106  drochner #endif
   2263  1.106  drochner 	else
   2264  1.110     enami 		return (0);
   2265  1.106  drochner 
   2266  1.110     enami 	if (ioh)
   2267  1.110     enami 		*ioh = help;
   2268  1.110     enami 	return (1);
   2269    1.2       cgd }
   2270  1.245     perry 
   2271  1.247   gdamore /*
   2272  1.247   gdamore  * this routine exists to serve as a shutdown hook for systems that
   2273  1.247   gdamore  * have firmware which doesn't interact properly with a com device in
   2274  1.247   gdamore  * FIFO mode.
   2275  1.247   gdamore  */
   2276  1.273    dyoung bool
   2277  1.273    dyoung com_cleanup(device_t self, int how)
   2278  1.247   gdamore {
   2279  1.273    dyoung 	struct com_softc *sc = device_private(self);
   2280  1.247   gdamore 
   2281  1.247   gdamore 	if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
   2282  1.247   gdamore 		CSR_WRITE_1(&sc->sc_regs, COM_REG_FIFO, 0);
   2283  1.273    dyoung 
   2284  1.273    dyoung 	return true;
   2285  1.273    dyoung }
   2286  1.273    dyoung 
   2287  1.273    dyoung bool
   2288  1.273    dyoung com_suspend(device_t self PMF_FN_ARGS)
   2289  1.273    dyoung {
   2290  1.273    dyoung 	struct com_softc *sc = device_private(self);
   2291  1.273    dyoung 
   2292  1.273    dyoung 	CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, 0);
   2293  1.273    dyoung 	(void)CSR_READ_1(&sc->sc_regs, COM_REG_IIR);
   2294  1.273    dyoung 
   2295  1.273    dyoung 	return true;
   2296  1.247   gdamore }
   2297  1.247   gdamore 
   2298  1.268    dyoung bool
   2299  1.273    dyoung com_resume(device_t self PMF_FN_ARGS)
   2300  1.245     perry {
   2301  1.273    dyoung 	struct com_softc *sc = device_private(self);
   2302  1.245     perry 
   2303  1.263        ad 	mutex_spin_enter(&sc->sc_lock);
   2304  1.268    dyoung 	com_loadchannelregs(sc);
   2305  1.263        ad 	mutex_spin_exit(&sc->sc_lock);
   2306  1.268    dyoung 
   2307  1.268    dyoung 	return true;
   2308  1.245     perry }
   2309