Home | History | Annotate | Line # | Download | only in dev
plcom.c revision 1.35
      1  1.35     skrll /*	$NetBSD: plcom.c,v 1.35 2012/05/14 19:40:06 skrll Exp $	*/
      2   1.1  rearnsha 
      3   1.1  rearnsha /*-
      4   1.1  rearnsha  * Copyright (c) 2001 ARM Ltd
      5   1.1  rearnsha  * All rights reserved.
      6   1.1  rearnsha  *
      7   1.1  rearnsha  * Redistribution and use in source and binary forms, with or without
      8   1.1  rearnsha  * modification, are permitted provided that the following conditions
      9   1.1  rearnsha  * are met:
     10   1.1  rearnsha  * 1. Redistributions of source code must retain the above copyright
     11   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer.
     12   1.1  rearnsha  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  rearnsha  *    documentation and/or other materials provided with the distribution.
     15   1.1  rearnsha  * 3. The name of the company may not be used to endorse or promote
     16   1.1  rearnsha  *    products derived from this software without specific prior written
     17   1.1  rearnsha  *    permission.
     18   1.1  rearnsha  *
     19   1.1  rearnsha  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     20   1.1  rearnsha  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     21   1.1  rearnsha  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22   1.1  rearnsha  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     23   1.1  rearnsha  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24   1.1  rearnsha  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25   1.1  rearnsha  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1  rearnsha  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1  rearnsha  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1  rearnsha  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1  rearnsha  * SUCH DAMAGE.
     30   1.1  rearnsha  *
     31   1.1  rearnsha  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     32   1.1  rearnsha  * All rights reserved.
     33   1.1  rearnsha  *
     34   1.1  rearnsha  * This code is derived from software contributed to The NetBSD Foundation
     35   1.1  rearnsha  * by Charles M. Hannum.
     36   1.1  rearnsha  *
     37   1.1  rearnsha  * Redistribution and use in source and binary forms, with or without
     38   1.1  rearnsha  * modification, are permitted provided that the following conditions
     39   1.1  rearnsha  * are met:
     40   1.1  rearnsha  * 1. Redistributions of source code must retain the above copyright
     41   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer.
     42   1.1  rearnsha  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer in the
     44   1.1  rearnsha  *    documentation and/or other materials provided with the distribution.
     45   1.1  rearnsha  *
     46   1.1  rearnsha  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     47   1.1  rearnsha  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     48   1.1  rearnsha  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     49   1.1  rearnsha  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     50   1.1  rearnsha  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     51   1.1  rearnsha  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     52   1.1  rearnsha  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     53   1.1  rearnsha  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     54   1.1  rearnsha  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     55   1.1  rearnsha  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     56   1.1  rearnsha  * POSSIBILITY OF SUCH DAMAGE.
     57   1.1  rearnsha  */
     58   1.1  rearnsha 
     59   1.1  rearnsha /*
     60   1.1  rearnsha  * Copyright (c) 1991 The Regents of the University of California.
     61   1.1  rearnsha  * All rights reserved.
     62   1.1  rearnsha  *
     63   1.1  rearnsha  * Redistribution and use in source and binary forms, with or without
     64   1.1  rearnsha  * modification, are permitted provided that the following conditions
     65   1.1  rearnsha  * are met:
     66   1.1  rearnsha  * 1. Redistributions of source code must retain the above copyright
     67   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer.
     68   1.1  rearnsha  * 2. Redistributions in binary form must reproduce the above copyright
     69   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer in the
     70   1.1  rearnsha  *    documentation and/or other materials provided with the distribution.
     71   1.9       agc  * 3. Neither the name of the University nor the names of its contributors
     72   1.1  rearnsha  *    may be used to endorse or promote products derived from this software
     73   1.1  rearnsha  *    without specific prior written permission.
     74   1.1  rearnsha  *
     75   1.1  rearnsha  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     76   1.1  rearnsha  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     77   1.1  rearnsha  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     78   1.1  rearnsha  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     79   1.1  rearnsha  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     80   1.1  rearnsha  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     81   1.1  rearnsha  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     82   1.1  rearnsha  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     83   1.1  rearnsha  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     84   1.1  rearnsha  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     85   1.1  rearnsha  * SUCH DAMAGE.
     86   1.1  rearnsha  *
     87   1.1  rearnsha  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     88   1.1  rearnsha  */
     89   1.1  rearnsha 
     90   1.1  rearnsha /*
     91   1.1  rearnsha  * COM driver for the Prime Cell PL010 UART, which is similar to the 16C550,
     92   1.1  rearnsha  * but has a completely different programmer's model.
     93   1.1  rearnsha  * Derived from the NS16550AF com driver.
     94   1.1  rearnsha  */
     95   1.8     lukem 
     96   1.8     lukem #include <sys/cdefs.h>
     97  1.35     skrll __KERNEL_RCSID(0, "$NetBSD: plcom.c,v 1.35 2012/05/14 19:40:06 skrll Exp $");
     98   1.1  rearnsha 
     99   1.1  rearnsha #include "opt_plcom.h"
    100   1.1  rearnsha #include "opt_ddb.h"
    101   1.1  rearnsha #include "opt_kgdb.h"
    102   1.7    martin #include "opt_lockdebug.h"
    103   1.7    martin #include "opt_multiprocessor.h"
    104   1.1  rearnsha 
    105   1.1  rearnsha #include "rnd.h"
    106  1.33       tls #ifdef RND_COM
    107   1.1  rearnsha #include <sys/rnd.h>
    108   1.1  rearnsha #endif
    109   1.1  rearnsha 
    110   1.1  rearnsha /*
    111   1.1  rearnsha  * Override cnmagic(9) macro before including <sys/systm.h>.
    112   1.1  rearnsha  * We need to know if cn_check_magic triggered debugger, so set a flag.
    113   1.1  rearnsha  * Callers of cn_check_magic must declare int cn_trapped = 0;
    114   1.1  rearnsha  * XXX: this is *ugly*!
    115   1.1  rearnsha  */
    116   1.1  rearnsha #define cn_trap()				\
    117   1.1  rearnsha 	do {					\
    118   1.1  rearnsha 		console_debugger();		\
    119   1.1  rearnsha 		cn_trapped = 1;			\
    120   1.1  rearnsha 	} while (/* CONSTCOND */ 0)
    121   1.1  rearnsha 
    122   1.1  rearnsha #include <sys/param.h>
    123   1.1  rearnsha #include <sys/systm.h>
    124   1.1  rearnsha #include <sys/ioctl.h>
    125   1.1  rearnsha #include <sys/select.h>
    126   1.1  rearnsha #include <sys/tty.h>
    127   1.1  rearnsha #include <sys/proc.h>
    128   1.1  rearnsha #include <sys/conf.h>
    129   1.1  rearnsha #include <sys/file.h>
    130   1.1  rearnsha #include <sys/uio.h>
    131   1.1  rearnsha #include <sys/kernel.h>
    132   1.1  rearnsha #include <sys/syslog.h>
    133   1.1  rearnsha #include <sys/types.h>
    134   1.1  rearnsha #include <sys/device.h>
    135   1.1  rearnsha #include <sys/malloc.h>
    136   1.1  rearnsha #include <sys/timepps.h>
    137   1.1  rearnsha #include <sys/vnode.h>
    138  1.16      elad #include <sys/kauth.h>
    139  1.25        ad #include <sys/intr.h>
    140  1.25        ad #include <sys/bus.h>
    141   1.1  rearnsha 
    142   1.1  rearnsha #include <evbarm/dev/plcomreg.h>
    143   1.1  rearnsha #include <evbarm/dev/plcomvar.h>
    144   1.1  rearnsha 
    145   1.1  rearnsha #include <dev/cons.h>
    146   1.1  rearnsha 
    147   1.1  rearnsha static void plcom_enable_debugport (struct plcom_softc *);
    148   1.1  rearnsha 
    149   1.1  rearnsha void	plcom_config	(struct plcom_softc *);
    150   1.1  rearnsha void	plcom_shutdown	(struct plcom_softc *);
    151   1.1  rearnsha int	plcomspeed	(long, long);
    152   1.1  rearnsha static	u_char	cflag2lcr (tcflag_t);
    153   1.1  rearnsha int	plcomparam	(struct tty *, struct termios *);
    154   1.1  rearnsha void	plcomstart	(struct tty *);
    155   1.1  rearnsha int	plcomhwiflow	(struct tty *, int);
    156   1.1  rearnsha 
    157   1.1  rearnsha void	plcom_loadchannelregs (struct plcom_softc *);
    158   1.1  rearnsha void	plcom_hwiflow	(struct plcom_softc *);
    159   1.1  rearnsha void	plcom_break	(struct plcom_softc *, int);
    160   1.1  rearnsha void	plcom_modem	(struct plcom_softc *, int);
    161   1.1  rearnsha void	tiocm_to_plcom	(struct plcom_softc *, u_long, int);
    162   1.1  rearnsha int	plcom_to_tiocm	(struct plcom_softc *);
    163   1.1  rearnsha void	plcom_iflush	(struct plcom_softc *);
    164   1.1  rearnsha 
    165   1.1  rearnsha int	plcom_common_getc (dev_t, bus_space_tag_t, bus_space_handle_t);
    166   1.1  rearnsha void	plcom_common_putc (dev_t, bus_space_tag_t, bus_space_handle_t, int);
    167   1.1  rearnsha 
    168   1.1  rearnsha int	plcominit	(bus_space_tag_t, bus_addr_t, int, int, tcflag_t,
    169   1.1  rearnsha 			    bus_space_handle_t *);
    170   1.1  rearnsha 
    171   1.4   gehenna dev_type_open(plcomopen);
    172   1.4   gehenna dev_type_close(plcomclose);
    173   1.4   gehenna dev_type_read(plcomread);
    174   1.4   gehenna dev_type_write(plcomwrite);
    175   1.4   gehenna dev_type_ioctl(plcomioctl);
    176   1.4   gehenna dev_type_stop(plcomstop);
    177   1.4   gehenna dev_type_tty(plcomtty);
    178   1.4   gehenna dev_type_poll(plcompoll);
    179   1.1  rearnsha 
    180   1.1  rearnsha int	plcomcngetc	(dev_t);
    181   1.1  rearnsha void	plcomcnputc	(dev_t, int);
    182   1.1  rearnsha void	plcomcnpollc	(dev_t, int);
    183   1.1  rearnsha 
    184   1.1  rearnsha #define	integrate	static inline
    185   1.1  rearnsha void 	plcomsoft	(void *);
    186   1.1  rearnsha integrate void plcom_rxsoft	(struct plcom_softc *, struct tty *);
    187   1.1  rearnsha integrate void plcom_txsoft	(struct plcom_softc *, struct tty *);
    188   1.1  rearnsha integrate void plcom_stsoft	(struct plcom_softc *, struct tty *);
    189   1.1  rearnsha integrate void plcom_schedrx	(struct plcom_softc *);
    190   1.1  rearnsha void	plcomdiag		(void *);
    191   1.1  rearnsha 
    192   1.1  rearnsha extern struct cfdriver plcom_cd;
    193   1.1  rearnsha 
    194   1.4   gehenna const struct cdevsw plcom_cdevsw = {
    195   1.4   gehenna 	plcomopen, plcomclose, plcomread, plcomwrite, plcomioctl,
    196   1.5  jdolecek 	plcomstop, plcomtty, plcompoll, nommap, ttykqfilter, D_TTY
    197   1.4   gehenna };
    198   1.4   gehenna 
    199   1.1  rearnsha /*
    200   1.1  rearnsha  * Make this an option variable one can patch.
    201   1.1  rearnsha  * But be warned:  this must be a power of 2!
    202   1.1  rearnsha  */
    203   1.1  rearnsha u_int plcom_rbuf_size = PLCOM_RING_SIZE;
    204   1.1  rearnsha 
    205   1.1  rearnsha /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    206   1.1  rearnsha u_int plcom_rbuf_hiwat = (PLCOM_RING_SIZE * 1) / 4;
    207   1.1  rearnsha u_int plcom_rbuf_lowat = (PLCOM_RING_SIZE * 3) / 4;
    208   1.1  rearnsha 
    209   1.1  rearnsha static int	plcomconsunit = -1;
    210   1.1  rearnsha static bus_space_tag_t plcomconstag;
    211   1.1  rearnsha static bus_space_handle_t plcomconsioh;
    212   1.1  rearnsha static int	plcomconsattached;
    213   1.1  rearnsha static int plcomconsrate;
    214   1.1  rearnsha static tcflag_t plcomconscflag;
    215   1.1  rearnsha static struct cnm_state plcom_cnm_state;
    216   1.1  rearnsha 
    217   1.1  rearnsha static int ppscap =
    218   1.1  rearnsha 	PPS_TSFMT_TSPEC |
    219   1.1  rearnsha 	PPS_CAPTUREASSERT |
    220   1.1  rearnsha 	PPS_CAPTURECLEAR |
    221   1.1  rearnsha #ifdef  PPS_SYNC
    222   1.1  rearnsha 	PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR |
    223   1.1  rearnsha #endif	/* PPS_SYNC */
    224   1.1  rearnsha 	PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
    225   1.1  rearnsha 
    226   1.1  rearnsha #ifdef KGDB
    227   1.1  rearnsha #include <sys/kgdb.h>
    228   1.1  rearnsha 
    229   1.1  rearnsha static int plcom_kgdb_unit;
    230   1.1  rearnsha static bus_space_tag_t plcom_kgdb_iot;
    231   1.1  rearnsha static bus_space_handle_t plcom_kgdb_ioh;
    232   1.1  rearnsha static int plcom_kgdb_attached;
    233   1.1  rearnsha 
    234   1.1  rearnsha int	plcom_kgdb_getc (void *);
    235   1.1  rearnsha void	plcom_kgdb_putc (void *, int);
    236   1.1  rearnsha #endif /* KGDB */
    237   1.1  rearnsha 
    238   1.1  rearnsha #define	PLCOMUNIT_MASK	0x7ffff
    239   1.1  rearnsha #define	PLCOMDIALOUT_MASK	0x80000
    240   1.1  rearnsha 
    241   1.1  rearnsha #define	PLCOMUNIT(x)	(minor(x) & PLCOMUNIT_MASK)
    242   1.1  rearnsha #define	PLCOMDIALOUT(x)	(minor(x) & PLCOMDIALOUT_MASK)
    243   1.1  rearnsha 
    244   1.1  rearnsha #define	PLCOM_ISALIVE(sc)	((sc)->enabled != 0 && \
    245  1.14   thorpej 				 device_is_active(&(sc)->sc_dev))
    246   1.1  rearnsha 
    247   1.1  rearnsha #define	BR	BUS_SPACE_BARRIER_READ
    248   1.1  rearnsha #define	BW	BUS_SPACE_BARRIER_WRITE
    249   1.1  rearnsha #define PLCOM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, PLCOM_UART_SIZE, (f))
    250   1.1  rearnsha 
    251   1.1  rearnsha #define PLCOM_LOCK(sc) simple_lock(&(sc)->sc_lock)
    252   1.1  rearnsha #define PLCOM_UNLOCK(sc) simple_unlock(&(sc)->sc_lock)
    253   1.1  rearnsha 
    254   1.1  rearnsha int
    255   1.1  rearnsha plcomspeed(long speed, long frequency)
    256   1.1  rearnsha {
    257   1.1  rearnsha #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    258   1.1  rearnsha 
    259   1.1  rearnsha 	int x, err;
    260   1.1  rearnsha 
    261   1.1  rearnsha #if 0
    262   1.1  rearnsha 	if (speed == 0)
    263   1.1  rearnsha 		return 0;
    264   1.1  rearnsha #endif
    265   1.1  rearnsha 	if (speed <= 0)
    266   1.1  rearnsha 		return -1;
    267   1.1  rearnsha 	x = divrnd(frequency / 16, speed);
    268   1.1  rearnsha 	if (x <= 0)
    269   1.1  rearnsha 		return -1;
    270   1.1  rearnsha 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    271   1.1  rearnsha 	if (err < 0)
    272   1.1  rearnsha 		err = -err;
    273   1.1  rearnsha 	if (err > PLCOM_TOLERANCE)
    274   1.1  rearnsha 		return -1;
    275   1.1  rearnsha 	return x;
    276   1.1  rearnsha 
    277   1.1  rearnsha #undef	divrnd
    278   1.1  rearnsha }
    279   1.1  rearnsha 
    280   1.1  rearnsha #ifdef PLCOM_DEBUG
    281   1.1  rearnsha int	plcom_debug = 0;
    282   1.1  rearnsha 
    283  1.34       bsh void plcomstatus (struct plcom_softc *, const char *);
    284   1.1  rearnsha void
    285  1.34       bsh plcomstatus(struct plcom_softc *sc, const char *str)
    286   1.1  rearnsha {
    287   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    288   1.1  rearnsha 
    289   1.1  rearnsha 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    290   1.1  rearnsha 	    sc->sc_dev.dv_xname, str,
    291   1.1  rearnsha 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    292  1.35     skrll 	    ISSET(sc->sc_msr, PL01X_MSR_DCD) ? "+" : "-",
    293   1.1  rearnsha 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    294  1.35     skrll 	    ISSET(sc->sc_mcr, PL01X_MCR_DTR) ? "+" : "-",
    295   1.1  rearnsha 	    sc->sc_tx_stopped ? "+" : "-");
    296   1.1  rearnsha 
    297   1.1  rearnsha 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    298   1.1  rearnsha 	    sc->sc_dev.dv_xname, str,
    299   1.1  rearnsha 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    300  1.35     skrll 	    ISSET(sc->sc_msr, PL01X_MSR_CTS) ? "+" : "-",
    301   1.1  rearnsha 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    302  1.35     skrll 	    ISSET(sc->sc_mcr, PL01X_MCR_RTS) ? "+" : "-",
    303   1.1  rearnsha 	    sc->sc_rx_flags);
    304   1.1  rearnsha }
    305   1.1  rearnsha #endif
    306   1.1  rearnsha 
    307  1.34       bsh /* XXX this function is not used? */
    308   1.1  rearnsha int
    309   1.1  rearnsha plcomprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
    310   1.1  rearnsha {
    311   1.1  rearnsha 	int data;
    312   1.1  rearnsha 
    313   1.1  rearnsha 	/* Disable the UART.  */
    314   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_cr, 0);
    315   1.1  rearnsha 	/* Make sure the FIFO is off.  */
    316  1.35     skrll 	bus_space_write_1(iot, ioh, plcom_lcr, PL01X_LCR_8BITS);
    317   1.1  rearnsha 	/* Disable interrupts.  */
    318   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_iir, 0);
    319   1.1  rearnsha 
    320   1.1  rearnsha 	/* Make sure we swallow anything in the receiving register.  */
    321   1.1  rearnsha 	data = bus_space_read_1(iot, ioh, plcom_dr);
    322   1.1  rearnsha 
    323  1.35     skrll 	if (bus_space_read_1(iot, ioh, plcom_lcr) != PL01X_LCR_8BITS)
    324   1.1  rearnsha 		return 0;
    325   1.1  rearnsha 
    326  1.35     skrll 	data = bus_space_read_1(iot, ioh, plcom_fr) & (PL01X_FR_RXFF | PL01X_FR_RXFE);
    327   1.1  rearnsha 
    328  1.35     skrll 	if (data != PL01X_FR_RXFE)
    329   1.1  rearnsha 		return 0;
    330   1.1  rearnsha 
    331   1.1  rearnsha 	return 1;
    332   1.1  rearnsha }
    333   1.1  rearnsha 
    334   1.1  rearnsha static void
    335   1.1  rearnsha plcom_enable_debugport(struct plcom_softc *sc)
    336   1.1  rearnsha {
    337   1.1  rearnsha 	int s;
    338   1.1  rearnsha 
    339   1.1  rearnsha 	/* Turn on line break interrupt, set carrier. */
    340   1.1  rearnsha 	s = splserial();
    341   1.1  rearnsha 	PLCOM_LOCK(sc);
    342  1.35     skrll 	sc->sc_cr = PL010_CR_RIE | PL010_CR_RTIE | PL01X_CR_UARTEN;
    343   1.1  rearnsha 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
    344  1.35     skrll 	SET(sc->sc_mcr, PL01X_MCR_DTR | PL01X_MCR_RTS);
    345  1.15   thorpej 	/* XXX device_unit() abuse */
    346  1.15   thorpej 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
    347  1.15   thorpej 	    sc->sc_mcr);
    348   1.1  rearnsha 	PLCOM_UNLOCK(sc);
    349   1.1  rearnsha 	splx(s);
    350   1.1  rearnsha }
    351   1.1  rearnsha 
    352   1.1  rearnsha void
    353   1.1  rearnsha plcom_attach_subr(struct plcom_softc *sc)
    354   1.1  rearnsha {
    355   1.1  rearnsha 	int unit = sc->sc_iounit;
    356   1.1  rearnsha 	bus_space_tag_t iot = sc->sc_iot;
    357   1.1  rearnsha 	bus_space_handle_t ioh = sc->sc_ioh;
    358   1.1  rearnsha 	struct tty *tp;
    359   1.1  rearnsha 
    360  1.21        ad 	callout_init(&sc->sc_diag_callout, 0);
    361   1.1  rearnsha 	simple_lock_init(&sc->sc_lock);
    362   1.1  rearnsha 
    363   1.1  rearnsha 	/* Disable interrupts before configuring the device. */
    364   1.1  rearnsha 	sc->sc_cr = 0;
    365   1.1  rearnsha 
    366   1.1  rearnsha 	if (plcomconstag && unit == plcomconsunit) {
    367   1.1  rearnsha 		plcomconsattached = 1;
    368   1.1  rearnsha 
    369   1.1  rearnsha 		plcomconstag = iot;
    370   1.1  rearnsha 		plcomconsioh = ioh;
    371   1.1  rearnsha 
    372   1.1  rearnsha 		/* Make sure the console is always "hardwired". */
    373   1.1  rearnsha 		delay(1000);			/* wait for output to finish */
    374   1.1  rearnsha 		SET(sc->sc_hwflags, PLCOM_HW_CONSOLE);
    375   1.1  rearnsha 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    376   1.1  rearnsha 		/* Must re-enable the console immediately, or we will
    377   1.1  rearnsha 		   hang when trying to print.  */
    378  1.35     skrll 		sc->sc_cr = PL01X_CR_UARTEN;
    379   1.1  rearnsha 	}
    380   1.1  rearnsha 
    381   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
    382   1.1  rearnsha 
    383   1.1  rearnsha 	/* The PL010 has a 16-byte fifo, but the tx interrupt triggers when
    384   1.1  rearnsha 	   there is space for 8 more bytes.  */
    385   1.1  rearnsha 	sc->sc_fifolen = 8;
    386   1.1  rearnsha 	printf("\n");
    387   1.1  rearnsha 
    388   1.1  rearnsha 	if (ISSET(sc->sc_hwflags, PLCOM_HW_TXFIFO_DISABLE)) {
    389   1.1  rearnsha 		sc->sc_fifolen = 1;
    390   1.1  rearnsha 		printf("%s: txfifo disabled\n", sc->sc_dev.dv_xname);
    391   1.1  rearnsha 	}
    392   1.1  rearnsha 
    393   1.1  rearnsha 	if (sc->sc_fifolen > 1)
    394   1.1  rearnsha 		SET(sc->sc_hwflags, PLCOM_HW_FIFO);
    395   1.1  rearnsha 
    396  1.32     rmind 	tp = tty_alloc();
    397   1.1  rearnsha 	tp->t_oproc = plcomstart;
    398   1.1  rearnsha 	tp->t_param = plcomparam;
    399   1.1  rearnsha 	tp->t_hwiflow = plcomhwiflow;
    400   1.1  rearnsha 
    401   1.1  rearnsha 	sc->sc_tty = tp;
    402   1.1  rearnsha 	sc->sc_rbuf = malloc(plcom_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    403   1.1  rearnsha 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    404   1.1  rearnsha 	sc->sc_rbavail = plcom_rbuf_size;
    405   1.1  rearnsha 	if (sc->sc_rbuf == NULL) {
    406   1.1  rearnsha 		printf("%s: unable to allocate ring buffer\n",
    407   1.1  rearnsha 		    sc->sc_dev.dv_xname);
    408   1.1  rearnsha 		return;
    409   1.1  rearnsha 	}
    410   1.1  rearnsha 	sc->sc_ebuf = sc->sc_rbuf + (plcom_rbuf_size << 1);
    411   1.1  rearnsha 
    412   1.1  rearnsha 	tty_attach(tp);
    413   1.1  rearnsha 
    414   1.1  rearnsha 	if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
    415   1.1  rearnsha 		int maj;
    416   1.1  rearnsha 
    417   1.1  rearnsha 		/* locate the major number */
    418   1.4   gehenna 		maj = cdevsw_lookup_major(&plcom_cdevsw);
    419   1.1  rearnsha 
    420  1.15   thorpej 		cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
    421   1.1  rearnsha 
    422   1.1  rearnsha 		printf("%s: console\n", sc->sc_dev.dv_xname);
    423   1.1  rearnsha 	}
    424   1.1  rearnsha 
    425   1.1  rearnsha #ifdef KGDB
    426   1.1  rearnsha 	/*
    427   1.1  rearnsha 	 * Allow kgdb to "take over" this port.  If this is
    428   1.1  rearnsha 	 * the kgdb device, it has exclusive use.
    429   1.1  rearnsha 	 */
    430   1.1  rearnsha 	if (iot == plcom_kgdb_iot && unit == plcom_kgdb_unit) {
    431   1.1  rearnsha 		plcom_kgdb_attached = 1;
    432   1.1  rearnsha 
    433   1.1  rearnsha 		SET(sc->sc_hwflags, PLCOM_HW_KGDB);
    434   1.1  rearnsha 		printf("%s: kgdb\n", sc->sc_dev.dv_xname);
    435   1.1  rearnsha 	}
    436   1.1  rearnsha #endif
    437   1.1  rearnsha 
    438  1.25        ad 	sc->sc_si = softint_establish(SOFTINT_SERIAL, plcomsoft, sc);
    439   1.1  rearnsha 
    440  1.33       tls #ifdef RND_COM
    441   1.1  rearnsha 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    442   1.1  rearnsha 			  RND_TYPE_TTY, 0);
    443   1.1  rearnsha #endif
    444   1.1  rearnsha 
    445   1.1  rearnsha 	/* if there are no enable/disable functions, assume the device
    446   1.1  rearnsha 	   is always enabled */
    447   1.1  rearnsha 	if (!sc->enable)
    448   1.1  rearnsha 		sc->enabled = 1;
    449   1.1  rearnsha 
    450   1.1  rearnsha 	plcom_config(sc);
    451   1.1  rearnsha 
    452   1.1  rearnsha 	SET(sc->sc_hwflags, PLCOM_HW_DEV_OK);
    453   1.1  rearnsha }
    454   1.1  rearnsha 
    455   1.1  rearnsha void
    456   1.1  rearnsha plcom_config(struct plcom_softc *sc)
    457   1.1  rearnsha {
    458   1.1  rearnsha 	bus_space_tag_t iot = sc->sc_iot;
    459   1.1  rearnsha 	bus_space_handle_t ioh = sc->sc_ioh;
    460   1.1  rearnsha 
    461   1.1  rearnsha 	/* Disable interrupts before configuring the device. */
    462   1.1  rearnsha 	sc->sc_cr = 0;
    463   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
    464   1.1  rearnsha 
    465   1.1  rearnsha 	if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE|PLCOM_HW_KGDB))
    466   1.1  rearnsha 		plcom_enable_debugport(sc);
    467   1.1  rearnsha }
    468   1.1  rearnsha 
    469   1.1  rearnsha int
    470  1.29       dsl plcom_detach(struct device *self, int flags)
    471   1.1  rearnsha {
    472   1.1  rearnsha 	struct plcom_softc *sc = (struct plcom_softc *)self;
    473   1.1  rearnsha 	int maj, mn;
    474   1.1  rearnsha 
    475  1.31    dyoung 	if (sc->sc_hwflags & (PLCOM_HW_CONSOLE|PLCOM_HW_KGDB))
    476  1.31    dyoung 		return EBUSY;
    477  1.31    dyoung 
    478  1.31    dyoung 	if (sc->disable != NULL && sc->enabled != 0) {
    479  1.31    dyoung 		(*sc->disable)(sc);
    480  1.31    dyoung 		sc->enabled = 0;
    481  1.31    dyoung 	}
    482  1.31    dyoung 
    483   1.1  rearnsha 	/* locate the major number */
    484   1.4   gehenna 	maj = cdevsw_lookup_major(&plcom_cdevsw);
    485   1.1  rearnsha 
    486   1.1  rearnsha 	/* Nuke the vnodes for any open instances. */
    487  1.15   thorpej 	mn = device_unit(self);
    488   1.1  rearnsha 	vdevgone(maj, mn, mn, VCHR);
    489   1.1  rearnsha 
    490   1.1  rearnsha 	mn |= PLCOMDIALOUT_MASK;
    491   1.1  rearnsha 	vdevgone(maj, mn, mn, VCHR);
    492   1.1  rearnsha 
    493   1.1  rearnsha 	/* Free the receive buffer. */
    494   1.1  rearnsha 	free(sc->sc_rbuf, M_DEVBUF);
    495   1.1  rearnsha 
    496   1.1  rearnsha 	/* Detach and free the tty. */
    497   1.1  rearnsha 	tty_detach(sc->sc_tty);
    498  1.32     rmind 	tty_free(sc->sc_tty);
    499   1.1  rearnsha 
    500   1.1  rearnsha 	/* Unhook the soft interrupt handler. */
    501  1.25        ad 	softint_disestablish(sc->sc_si);
    502   1.1  rearnsha 
    503  1.33       tls #ifdef RND_COM
    504   1.1  rearnsha 	/* Unhook the entropy source. */
    505   1.1  rearnsha 	rnd_detach_source(&sc->rnd_source);
    506   1.1  rearnsha #endif
    507   1.1  rearnsha 
    508   1.1  rearnsha 	return 0;
    509   1.1  rearnsha }
    510   1.1  rearnsha 
    511   1.1  rearnsha int
    512  1.31    dyoung plcom_activate(device_t self, enum devact act)
    513   1.1  rearnsha {
    514  1.31    dyoung 	struct plcom_softc *sc = device_private(self);
    515   1.1  rearnsha 
    516   1.1  rearnsha 	switch (act) {
    517   1.1  rearnsha 	case DVACT_DEACTIVATE:
    518  1.31    dyoung 		sc->enabled = 0;
    519  1.31    dyoung 		return 0;
    520  1.31    dyoung 	default:
    521  1.31    dyoung 		return EOPNOTSUPP;
    522   1.1  rearnsha 	}
    523   1.1  rearnsha }
    524   1.1  rearnsha 
    525   1.1  rearnsha void
    526   1.1  rearnsha plcom_shutdown(struct plcom_softc *sc)
    527   1.1  rearnsha {
    528   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    529   1.1  rearnsha 	int s;
    530   1.1  rearnsha 
    531   1.1  rearnsha 	s = splserial();
    532   1.1  rearnsha 	PLCOM_LOCK(sc);
    533   1.1  rearnsha 
    534   1.1  rearnsha 	/* If we were asserting flow control, then deassert it. */
    535   1.1  rearnsha 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    536   1.1  rearnsha 	plcom_hwiflow(sc);
    537   1.1  rearnsha 
    538   1.1  rearnsha 	/* Clear any break condition set with TIOCSBRK. */
    539   1.1  rearnsha 	plcom_break(sc, 0);
    540   1.1  rearnsha 
    541   1.1  rearnsha 	/* Turn off PPS capture on last close. */
    542  1.26        ad 	mutex_spin_enter(&timecounter_lock);
    543   1.1  rearnsha 	sc->sc_ppsmask = 0;
    544   1.1  rearnsha 	sc->ppsparam.mode = 0;
    545  1.26        ad 	mutex_spin_exit(&timecounter_lock);
    546   1.1  rearnsha 
    547   1.1  rearnsha 	/*
    548   1.1  rearnsha 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    549   1.1  rearnsha 	 * notice even if we immediately open the port again.
    550   1.1  rearnsha 	 * Avoid tsleeping above splhigh().
    551   1.1  rearnsha 	 */
    552   1.1  rearnsha 	if (ISSET(tp->t_cflag, HUPCL)) {
    553   1.1  rearnsha 		plcom_modem(sc, 0);
    554   1.1  rearnsha 		PLCOM_UNLOCK(sc);
    555   1.1  rearnsha 		splx(s);
    556   1.1  rearnsha 		/* XXX tsleep will only timeout */
    557   1.1  rearnsha 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    558   1.1  rearnsha 		s = splserial();
    559   1.1  rearnsha 		PLCOM_LOCK(sc);
    560   1.1  rearnsha 	}
    561   1.1  rearnsha 
    562   1.1  rearnsha 	/* Turn off interrupts. */
    563   1.1  rearnsha 	if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE))
    564   1.1  rearnsha 		/* interrupt on break */
    565  1.35     skrll 		sc->sc_cr = PL010_CR_RIE | PL010_CR_RTIE | PL01X_CR_UARTEN;
    566   1.1  rearnsha 	else
    567   1.1  rearnsha 		sc->sc_cr = 0;
    568   1.1  rearnsha 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
    569   1.1  rearnsha 
    570   1.1  rearnsha 	if (sc->disable) {
    571   1.1  rearnsha #ifdef DIAGNOSTIC
    572   1.1  rearnsha 		if (!sc->enabled)
    573   1.1  rearnsha 			panic("plcom_shutdown: not enabled?");
    574   1.1  rearnsha #endif
    575   1.1  rearnsha 		(*sc->disable)(sc);
    576   1.1  rearnsha 		sc->enabled = 0;
    577   1.1  rearnsha 	}
    578   1.1  rearnsha 	PLCOM_UNLOCK(sc);
    579   1.1  rearnsha 	splx(s);
    580   1.1  rearnsha }
    581   1.1  rearnsha 
    582   1.1  rearnsha int
    583  1.12  christos plcomopen(dev_t dev, int flag, int mode, struct lwp *l)
    584   1.1  rearnsha {
    585   1.1  rearnsha 	struct plcom_softc *sc;
    586   1.1  rearnsha 	struct tty *tp;
    587   1.1  rearnsha 	int s, s2;
    588   1.1  rearnsha 	int error;
    589   1.1  rearnsha 
    590  1.28    cegger 	sc = device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    591   1.1  rearnsha 	if (sc == NULL || !ISSET(sc->sc_hwflags, PLCOM_HW_DEV_OK) ||
    592   1.1  rearnsha 		sc->sc_rbuf == NULL)
    593   1.1  rearnsha 		return ENXIO;
    594   1.1  rearnsha 
    595  1.14   thorpej 	if (!device_is_active(&sc->sc_dev))
    596   1.1  rearnsha 		return ENXIO;
    597   1.1  rearnsha 
    598   1.1  rearnsha #ifdef KGDB
    599   1.1  rearnsha 	/*
    600   1.1  rearnsha 	 * If this is the kgdb port, no other use is permitted.
    601   1.1  rearnsha 	 */
    602   1.1  rearnsha 	if (ISSET(sc->sc_hwflags, PLCOM_HW_KGDB))
    603   1.1  rearnsha 		return EBUSY;
    604   1.1  rearnsha #endif
    605   1.1  rearnsha 
    606   1.1  rearnsha 	tp = sc->sc_tty;
    607   1.1  rearnsha 
    608  1.18      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    609  1.18      elad 		return (EBUSY);
    610   1.1  rearnsha 
    611   1.1  rearnsha 	s = spltty();
    612   1.1  rearnsha 
    613   1.1  rearnsha 	/*
    614   1.1  rearnsha 	 * Do the following iff this is a first open.
    615   1.1  rearnsha 	 */
    616   1.1  rearnsha 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    617   1.1  rearnsha 		struct termios t;
    618   1.1  rearnsha 
    619   1.1  rearnsha 		tp->t_dev = dev;
    620   1.1  rearnsha 
    621   1.1  rearnsha 		s2 = splserial();
    622   1.1  rearnsha 		PLCOM_LOCK(sc);
    623   1.1  rearnsha 
    624   1.1  rearnsha 		if (sc->enable) {
    625   1.1  rearnsha 			if ((*sc->enable)(sc)) {
    626   1.1  rearnsha 				PLCOM_UNLOCK(sc);
    627   1.1  rearnsha 				splx(s2);
    628   1.1  rearnsha 				splx(s);
    629   1.1  rearnsha 				printf("%s: device enable failed\n",
    630   1.1  rearnsha 				       sc->sc_dev.dv_xname);
    631   1.1  rearnsha 				return EIO;
    632   1.1  rearnsha 			}
    633   1.1  rearnsha 			sc->enabled = 1;
    634   1.1  rearnsha 			plcom_config(sc);
    635   1.1  rearnsha 		}
    636   1.1  rearnsha 
    637   1.1  rearnsha 		/* Turn on interrupts. */
    638   1.1  rearnsha 		/* IER_ERXRDY | IER_ERLS | IER_EMSC;  */
    639  1.35     skrll 		sc->sc_cr = PL010_CR_RIE | PL010_CR_RTIE | PL010_CR_MSIE | PL01X_CR_UARTEN;
    640   1.1  rearnsha 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
    641   1.1  rearnsha 
    642   1.1  rearnsha 		/* Fetch the current modem control status, needed later. */
    643   1.1  rearnsha 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, plcom_fr);
    644   1.1  rearnsha 
    645   1.1  rearnsha 		/* Clear PPS capture state on first open. */
    646  1.26        ad 
    647  1.26        ad 		mutex_spin_enter(&timecounter_lock);
    648   1.1  rearnsha 		sc->sc_ppsmask = 0;
    649   1.1  rearnsha 		sc->ppsparam.mode = 0;
    650  1.26        ad 		mutex_spin_exit(&timecounter_lock);
    651   1.1  rearnsha 
    652   1.1  rearnsha 		PLCOM_UNLOCK(sc);
    653   1.1  rearnsha 		splx(s2);
    654   1.1  rearnsha 
    655   1.1  rearnsha 		/*
    656   1.1  rearnsha 		 * Initialize the termios status to the defaults.  Add in the
    657   1.1  rearnsha 		 * sticky bits from TIOCSFLAGS.
    658   1.1  rearnsha 		 */
    659   1.1  rearnsha 		t.c_ispeed = 0;
    660   1.1  rearnsha 		if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
    661   1.1  rearnsha 			t.c_ospeed = plcomconsrate;
    662   1.1  rearnsha 			t.c_cflag = plcomconscflag;
    663   1.1  rearnsha 		} else {
    664   1.1  rearnsha 			t.c_ospeed = TTYDEF_SPEED;
    665   1.1  rearnsha 			t.c_cflag = TTYDEF_CFLAG;
    666   1.1  rearnsha 		}
    667   1.1  rearnsha 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    668   1.1  rearnsha 			SET(t.c_cflag, CLOCAL);
    669   1.1  rearnsha 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    670   1.1  rearnsha 			SET(t.c_cflag, CRTSCTS);
    671   1.1  rearnsha 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    672   1.1  rearnsha 			SET(t.c_cflag, MDMBUF);
    673   1.1  rearnsha 		/* Make sure plcomparam() will do something. */
    674   1.1  rearnsha 		tp->t_ospeed = 0;
    675   1.1  rearnsha 		(void) plcomparam(tp, &t);
    676   1.1  rearnsha 		tp->t_iflag = TTYDEF_IFLAG;
    677   1.1  rearnsha 		tp->t_oflag = TTYDEF_OFLAG;
    678   1.1  rearnsha 		tp->t_lflag = TTYDEF_LFLAG;
    679   1.1  rearnsha 		ttychars(tp);
    680   1.1  rearnsha 		ttsetwater(tp);
    681   1.1  rearnsha 
    682   1.1  rearnsha 		s2 = splserial();
    683   1.1  rearnsha 		PLCOM_LOCK(sc);
    684   1.1  rearnsha 
    685   1.1  rearnsha 		/*
    686   1.1  rearnsha 		 * Turn on DTR.  We must always do this, even if carrier is not
    687   1.1  rearnsha 		 * present, because otherwise we'd have to use TIOCSDTR
    688   1.1  rearnsha 		 * immediately after setting CLOCAL, which applications do not
    689   1.1  rearnsha 		 * expect.  We always assert DTR while the device is open
    690   1.1  rearnsha 		 * unless explicitly requested to deassert it.
    691   1.1  rearnsha 		 */
    692   1.1  rearnsha 		plcom_modem(sc, 1);
    693   1.1  rearnsha 
    694   1.1  rearnsha 		/* Clear the input ring, and unblock. */
    695   1.1  rearnsha 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    696   1.1  rearnsha 		sc->sc_rbavail = plcom_rbuf_size;
    697   1.1  rearnsha 		plcom_iflush(sc);
    698   1.1  rearnsha 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    699   1.1  rearnsha 		plcom_hwiflow(sc);
    700   1.1  rearnsha 
    701   1.1  rearnsha #ifdef PLCOM_DEBUG
    702   1.1  rearnsha 		if (plcom_debug)
    703   1.1  rearnsha 			plcomstatus(sc, "plcomopen  ");
    704   1.1  rearnsha #endif
    705   1.1  rearnsha 
    706   1.1  rearnsha 		PLCOM_UNLOCK(sc);
    707   1.1  rearnsha 		splx(s2);
    708   1.1  rearnsha 	}
    709   1.1  rearnsha 
    710   1.1  rearnsha 	splx(s);
    711   1.1  rearnsha 
    712   1.1  rearnsha 	error = ttyopen(tp, PLCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    713   1.1  rearnsha 	if (error)
    714   1.1  rearnsha 		goto bad;
    715   1.1  rearnsha 
    716   1.1  rearnsha 	error = (*tp->t_linesw->l_open)(dev, tp);
    717   1.1  rearnsha 	if (error)
    718   1.1  rearnsha 		goto bad;
    719   1.1  rearnsha 
    720   1.1  rearnsha 	return 0;
    721   1.1  rearnsha 
    722   1.1  rearnsha bad:
    723   1.1  rearnsha 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    724   1.1  rearnsha 		/*
    725   1.1  rearnsha 		 * We failed to open the device, and nobody else had it opened.
    726   1.1  rearnsha 		 * Clean up the state as appropriate.
    727   1.1  rearnsha 		 */
    728   1.1  rearnsha 		plcom_shutdown(sc);
    729   1.1  rearnsha 	}
    730   1.1  rearnsha 
    731   1.1  rearnsha 	return error;
    732   1.1  rearnsha }
    733   1.1  rearnsha 
    734   1.1  rearnsha int
    735  1.12  christos plcomclose(dev_t dev, int flag, int mode, struct lwp *l)
    736   1.1  rearnsha {
    737  1.28    cegger 	struct plcom_softc *sc =
    738  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    739   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    740   1.1  rearnsha 
    741   1.1  rearnsha 	/* XXX This is for cons.c. */
    742   1.1  rearnsha 	if (!ISSET(tp->t_state, TS_ISOPEN))
    743   1.1  rearnsha 		return 0;
    744   1.1  rearnsha 
    745   1.1  rearnsha 	(*tp->t_linesw->l_close)(tp, flag);
    746   1.1  rearnsha 	ttyclose(tp);
    747   1.1  rearnsha 
    748   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
    749   1.1  rearnsha 		return 0;
    750   1.1  rearnsha 
    751   1.1  rearnsha 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    752   1.1  rearnsha 		/*
    753   1.1  rearnsha 		 * Although we got a last close, the device may still be in
    754   1.1  rearnsha 		 * use; e.g. if this was the dialout node, and there are still
    755   1.1  rearnsha 		 * processes waiting for carrier on the non-dialout node.
    756   1.1  rearnsha 		 */
    757   1.1  rearnsha 		plcom_shutdown(sc);
    758   1.1  rearnsha 	}
    759   1.1  rearnsha 
    760   1.1  rearnsha 	return 0;
    761   1.1  rearnsha }
    762   1.1  rearnsha 
    763   1.1  rearnsha int
    764   1.1  rearnsha plcomread(dev_t dev, struct uio *uio, int flag)
    765   1.1  rearnsha {
    766  1.28    cegger 	struct plcom_softc *sc =
    767  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    768   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    769   1.1  rearnsha 
    770   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
    771   1.1  rearnsha 		return EIO;
    772   1.1  rearnsha 
    773   1.1  rearnsha 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    774   1.1  rearnsha }
    775   1.1  rearnsha 
    776   1.1  rearnsha int
    777   1.1  rearnsha plcomwrite(dev_t dev, struct uio *uio, int flag)
    778   1.1  rearnsha {
    779  1.28    cegger 	struct plcom_softc *sc =
    780  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    781   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    782   1.1  rearnsha 
    783   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
    784   1.1  rearnsha 		return EIO;
    785   1.1  rearnsha 
    786   1.1  rearnsha 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    787   1.1  rearnsha }
    788   1.1  rearnsha 
    789   1.1  rearnsha int
    790  1.12  christos plcompoll(dev_t dev, int events, struct lwp *l)
    791   1.1  rearnsha {
    792  1.28    cegger 	struct plcom_softc *sc =
    793  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    794   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    795   1.1  rearnsha 
    796   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
    797   1.1  rearnsha 		return EIO;
    798   1.1  rearnsha 
    799  1.12  christos 	return (*tp->t_linesw->l_poll)(tp, events, l);
    800   1.1  rearnsha }
    801   1.1  rearnsha 
    802   1.1  rearnsha struct tty *
    803   1.1  rearnsha plcomtty(dev_t dev)
    804   1.1  rearnsha {
    805  1.28    cegger 	struct plcom_softc *sc =
    806  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    807   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    808   1.1  rearnsha 
    809   1.1  rearnsha 	return tp;
    810   1.1  rearnsha }
    811   1.1  rearnsha 
    812   1.1  rearnsha int
    813  1.20  christos plcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    814   1.1  rearnsha {
    815  1.28    cegger 	struct plcom_softc *sc =
    816  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
    817   1.1  rearnsha 	struct tty *tp = sc->sc_tty;
    818   1.1  rearnsha 	int error;
    819   1.1  rearnsha 	int s;
    820   1.1  rearnsha 
    821   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
    822   1.1  rearnsha 		return EIO;
    823   1.1  rearnsha 
    824  1.12  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    825   1.3    atatat 	if (error != EPASSTHROUGH)
    826   1.1  rearnsha 		return error;
    827   1.1  rearnsha 
    828  1.12  christos 	error = ttioctl(tp, cmd, data, flag, l);
    829   1.3    atatat 	if (error != EPASSTHROUGH)
    830   1.1  rearnsha 		return error;
    831   1.1  rearnsha 
    832   1.1  rearnsha 	error = 0;
    833   1.1  rearnsha 
    834   1.1  rearnsha 	s = splserial();
    835   1.1  rearnsha 	PLCOM_LOCK(sc);
    836   1.1  rearnsha 
    837   1.1  rearnsha 	switch (cmd) {
    838   1.1  rearnsha 	case TIOCSBRK:
    839   1.1  rearnsha 		plcom_break(sc, 1);
    840   1.1  rearnsha 		break;
    841   1.1  rearnsha 
    842   1.1  rearnsha 	case TIOCCBRK:
    843   1.1  rearnsha 		plcom_break(sc, 0);
    844   1.1  rearnsha 		break;
    845   1.1  rearnsha 
    846   1.1  rearnsha 	case TIOCSDTR:
    847   1.1  rearnsha 		plcom_modem(sc, 1);
    848   1.1  rearnsha 		break;
    849   1.1  rearnsha 
    850   1.1  rearnsha 	case TIOCCDTR:
    851   1.1  rearnsha 		plcom_modem(sc, 0);
    852   1.1  rearnsha 		break;
    853   1.1  rearnsha 
    854   1.1  rearnsha 	case TIOCGFLAGS:
    855   1.1  rearnsha 		*(int *)data = sc->sc_swflags;
    856   1.1  rearnsha 		break;
    857   1.1  rearnsha 
    858   1.1  rearnsha 	case TIOCSFLAGS:
    859  1.19      elad 		error = kauth_authorize_device_tty(l->l_cred,
    860  1.19      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    861   1.1  rearnsha 		if (error)
    862   1.1  rearnsha 			break;
    863   1.1  rearnsha 		sc->sc_swflags = *(int *)data;
    864   1.1  rearnsha 		break;
    865   1.1  rearnsha 
    866   1.1  rearnsha 	case TIOCMSET:
    867   1.1  rearnsha 	case TIOCMBIS:
    868   1.1  rearnsha 	case TIOCMBIC:
    869   1.1  rearnsha 		tiocm_to_plcom(sc, cmd, *(int *)data);
    870   1.1  rearnsha 		break;
    871   1.1  rearnsha 
    872   1.1  rearnsha 	case TIOCMGET:
    873   1.1  rearnsha 		*(int *)data = plcom_to_tiocm(sc);
    874   1.1  rearnsha 		break;
    875   1.1  rearnsha 
    876   1.1  rearnsha 	case PPS_IOC_CREATE:
    877   1.1  rearnsha 		break;
    878   1.1  rearnsha 
    879   1.1  rearnsha 	case PPS_IOC_DESTROY:
    880   1.1  rearnsha 		break;
    881   1.1  rearnsha 
    882   1.1  rearnsha 	case PPS_IOC_GETPARAMS: {
    883   1.1  rearnsha 		pps_params_t *pp;
    884   1.1  rearnsha 		pp = (pps_params_t *)data;
    885  1.26        ad 		mutex_spin_enter(&timecounter_lock);
    886   1.1  rearnsha 		*pp = sc->ppsparam;
    887  1.26        ad 		mutex_spin_exit(&timecounter_lock);
    888   1.1  rearnsha 		break;
    889   1.1  rearnsha 	}
    890   1.1  rearnsha 
    891   1.1  rearnsha 	case PPS_IOC_SETPARAMS: {
    892   1.1  rearnsha 	  	pps_params_t *pp;
    893   1.1  rearnsha 		int mode;
    894   1.1  rearnsha 		pp = (pps_params_t *)data;
    895  1.26        ad 		mutex_spin_enter(&timecounter_lock);
    896   1.1  rearnsha 		if (pp->mode & ~ppscap) {
    897   1.1  rearnsha 			error = EINVAL;
    898  1.26        ad 			mutex_spin_exit(&timecounter_lock);
    899   1.1  rearnsha 			break;
    900   1.1  rearnsha 		}
    901   1.1  rearnsha 		sc->ppsparam = *pp;
    902   1.1  rearnsha 	 	/*
    903   1.1  rearnsha 		 * Compute msr masks from user-specified timestamp state.
    904   1.1  rearnsha 		 */
    905   1.1  rearnsha 		mode = sc->ppsparam.mode;
    906   1.1  rearnsha #ifdef	PPS_SYNC
    907   1.1  rearnsha 		if (mode & PPS_HARDPPSONASSERT) {
    908   1.1  rearnsha 			mode |= PPS_CAPTUREASSERT;
    909   1.1  rearnsha 			/* XXX revoke any previous HARDPPS source */
    910   1.1  rearnsha 		}
    911   1.1  rearnsha 		if (mode & PPS_HARDPPSONCLEAR) {
    912   1.1  rearnsha 			mode |= PPS_CAPTURECLEAR;
    913   1.1  rearnsha 			/* XXX revoke any previous HARDPPS source */
    914   1.1  rearnsha 		}
    915   1.1  rearnsha #endif	/* PPS_SYNC */
    916   1.1  rearnsha 		switch (mode & PPS_CAPTUREBOTH) {
    917   1.1  rearnsha 		case 0:
    918   1.1  rearnsha 			sc->sc_ppsmask = 0;
    919   1.1  rearnsha 			break;
    920   1.1  rearnsha 
    921   1.1  rearnsha 		case PPS_CAPTUREASSERT:
    922  1.35     skrll 			sc->sc_ppsmask = PL01X_MSR_DCD;
    923  1.35     skrll 			sc->sc_ppsassert = PL01X_MSR_DCD;
    924   1.1  rearnsha 			sc->sc_ppsclear = -1;
    925   1.1  rearnsha 			break;
    926   1.1  rearnsha 
    927   1.1  rearnsha 		case PPS_CAPTURECLEAR:
    928  1.35     skrll 			sc->sc_ppsmask = PL01X_MSR_DCD;
    929   1.1  rearnsha 			sc->sc_ppsassert = -1;
    930   1.1  rearnsha 			sc->sc_ppsclear = 0;
    931   1.1  rearnsha 			break;
    932   1.1  rearnsha 
    933   1.1  rearnsha 		case PPS_CAPTUREBOTH:
    934  1.35     skrll 			sc->sc_ppsmask = PL01X_MSR_DCD;
    935  1.35     skrll 			sc->sc_ppsassert = PL01X_MSR_DCD;
    936   1.1  rearnsha 			sc->sc_ppsclear = 0;
    937   1.1  rearnsha 			break;
    938   1.1  rearnsha 
    939   1.1  rearnsha 		default:
    940   1.1  rearnsha 			error = EINVAL;
    941   1.1  rearnsha 			break;
    942   1.1  rearnsha 		}
    943  1.26        ad 		mutex_spin_exit(&timecounter_lock);
    944   1.1  rearnsha 		break;
    945   1.1  rearnsha 	}
    946   1.1  rearnsha 
    947   1.1  rearnsha 	case PPS_IOC_GETCAP:
    948   1.1  rearnsha 		*(int*)data = ppscap;
    949   1.1  rearnsha 		break;
    950   1.1  rearnsha 
    951   1.1  rearnsha 	case PPS_IOC_FETCH: {
    952   1.1  rearnsha 		pps_info_t *pi;
    953   1.1  rearnsha 		pi = (pps_info_t *)data;
    954  1.26        ad 		mutex_spin_enter(&timecounter_lock);
    955   1.1  rearnsha 		*pi = sc->ppsinfo;
    956  1.26        ad 		mutex_spin_exit(&timecounter_lock);
    957   1.1  rearnsha 		break;
    958   1.1  rearnsha 	}
    959   1.1  rearnsha 
    960   1.1  rearnsha 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
    961   1.1  rearnsha 		/*
    962   1.1  rearnsha 		 * Some GPS clocks models use the falling rather than
    963   1.1  rearnsha 		 * rising edge as the on-the-second signal.
    964   1.1  rearnsha 		 * The old API has no way to specify PPS polarity.
    965   1.1  rearnsha 		 */
    966  1.26        ad 		mutex_spin_enter(&timecounter_lock);
    967  1.35     skrll 		sc->sc_ppsmask = PL01X_MSR_DCD;
    968   1.1  rearnsha #ifndef PPS_TRAILING_EDGE
    969  1.35     skrll 		sc->sc_ppsassert = PL01X_MSR_DCD;
    970   1.1  rearnsha 		sc->sc_ppsclear = -1;
    971   1.1  rearnsha 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
    972   1.1  rearnsha 		    &sc->ppsinfo.assert_timestamp);
    973   1.1  rearnsha #else
    974   1.1  rearnsha 		sc->sc_ppsassert = -1
    975   1.1  rearnsha 		sc->sc_ppsclear = 0;
    976   1.1  rearnsha 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
    977   1.1  rearnsha 		    &sc->ppsinfo.clear_timestamp);
    978   1.1  rearnsha #endif
    979  1.26        ad 		mutex_spin_exit(&timecounter_lock);
    980   1.1  rearnsha 		break;
    981   1.1  rearnsha 
    982   1.1  rearnsha 	default:
    983   1.3    atatat 		error = EPASSTHROUGH;
    984   1.1  rearnsha 		break;
    985   1.1  rearnsha 	}
    986   1.1  rearnsha 
    987   1.1  rearnsha 	PLCOM_UNLOCK(sc);
    988   1.1  rearnsha 	splx(s);
    989   1.1  rearnsha 
    990   1.1  rearnsha #ifdef PLCOM_DEBUG
    991   1.1  rearnsha 	if (plcom_debug)
    992   1.1  rearnsha 		plcomstatus(sc, "plcomioctl ");
    993   1.1  rearnsha #endif
    994   1.1  rearnsha 
    995   1.1  rearnsha 	return error;
    996   1.1  rearnsha }
    997   1.1  rearnsha 
    998   1.1  rearnsha integrate void
    999   1.1  rearnsha plcom_schedrx(struct plcom_softc *sc)
   1000   1.1  rearnsha {
   1001   1.1  rearnsha 
   1002   1.1  rearnsha 	sc->sc_rx_ready = 1;
   1003   1.1  rearnsha 
   1004   1.1  rearnsha 	/* Wake up the poller. */
   1005  1.25        ad 	softint_schedule(sc->sc_si);
   1006   1.1  rearnsha }
   1007   1.1  rearnsha 
   1008   1.1  rearnsha void
   1009   1.1  rearnsha plcom_break(struct plcom_softc *sc, int onoff)
   1010   1.1  rearnsha {
   1011   1.1  rearnsha 
   1012   1.1  rearnsha 	if (onoff)
   1013  1.35     skrll 		SET(sc->sc_lcr, PL01X_LCR_BRK);
   1014   1.1  rearnsha 	else
   1015  1.35     skrll 		CLR(sc->sc_lcr, PL01X_LCR_BRK);
   1016   1.1  rearnsha 
   1017   1.1  rearnsha 	if (!sc->sc_heldchange) {
   1018   1.1  rearnsha 		if (sc->sc_tx_busy) {
   1019   1.1  rearnsha 			sc->sc_heldtbc = sc->sc_tbc;
   1020   1.1  rearnsha 			sc->sc_tbc = 0;
   1021   1.1  rearnsha 			sc->sc_heldchange = 1;
   1022   1.1  rearnsha 		} else
   1023   1.1  rearnsha 			plcom_loadchannelregs(sc);
   1024   1.1  rearnsha 	}
   1025   1.1  rearnsha }
   1026   1.1  rearnsha 
   1027   1.1  rearnsha void
   1028   1.1  rearnsha plcom_modem(struct plcom_softc *sc, int onoff)
   1029   1.1  rearnsha {
   1030   1.1  rearnsha 
   1031   1.1  rearnsha 	if (sc->sc_mcr_dtr == 0)
   1032   1.1  rearnsha 		return;
   1033   1.1  rearnsha 
   1034   1.1  rearnsha 	if (onoff)
   1035   1.1  rearnsha 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1036   1.1  rearnsha 	else
   1037   1.1  rearnsha 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1038   1.1  rearnsha 
   1039   1.1  rearnsha 	if (!sc->sc_heldchange) {
   1040   1.1  rearnsha 		if (sc->sc_tx_busy) {
   1041   1.1  rearnsha 			sc->sc_heldtbc = sc->sc_tbc;
   1042   1.1  rearnsha 			sc->sc_tbc = 0;
   1043   1.1  rearnsha 			sc->sc_heldchange = 1;
   1044   1.1  rearnsha 		} else
   1045   1.1  rearnsha 			plcom_loadchannelregs(sc);
   1046   1.1  rearnsha 	}
   1047   1.1  rearnsha }
   1048   1.1  rearnsha 
   1049   1.1  rearnsha void
   1050   1.1  rearnsha tiocm_to_plcom(struct plcom_softc *sc, u_long how, int ttybits)
   1051   1.1  rearnsha {
   1052   1.1  rearnsha 	u_char plcombits;
   1053   1.1  rearnsha 
   1054   1.1  rearnsha 	plcombits = 0;
   1055   1.1  rearnsha 	if (ISSET(ttybits, TIOCM_DTR))
   1056  1.35     skrll 		SET(plcombits, PL01X_MCR_DTR);
   1057   1.1  rearnsha 	if (ISSET(ttybits, TIOCM_RTS))
   1058  1.35     skrll 		SET(plcombits, PL01X_MCR_RTS);
   1059   1.1  rearnsha 
   1060   1.1  rearnsha 	switch (how) {
   1061   1.1  rearnsha 	case TIOCMBIC:
   1062   1.1  rearnsha 		CLR(sc->sc_mcr, plcombits);
   1063   1.1  rearnsha 		break;
   1064   1.1  rearnsha 
   1065   1.1  rearnsha 	case TIOCMBIS:
   1066   1.1  rearnsha 		SET(sc->sc_mcr, plcombits);
   1067   1.1  rearnsha 		break;
   1068   1.1  rearnsha 
   1069   1.1  rearnsha 	case TIOCMSET:
   1070  1.35     skrll 		CLR(sc->sc_mcr, PL01X_MCR_DTR | PL01X_MCR_RTS);
   1071   1.1  rearnsha 		SET(sc->sc_mcr, plcombits);
   1072   1.1  rearnsha 		break;
   1073   1.1  rearnsha 	}
   1074   1.1  rearnsha 
   1075   1.1  rearnsha 	if (!sc->sc_heldchange) {
   1076   1.1  rearnsha 		if (sc->sc_tx_busy) {
   1077   1.1  rearnsha 			sc->sc_heldtbc = sc->sc_tbc;
   1078   1.1  rearnsha 			sc->sc_tbc = 0;
   1079   1.1  rearnsha 			sc->sc_heldchange = 1;
   1080   1.1  rearnsha 		} else
   1081   1.1  rearnsha 			plcom_loadchannelregs(sc);
   1082   1.1  rearnsha 	}
   1083   1.1  rearnsha }
   1084   1.1  rearnsha 
   1085   1.1  rearnsha int
   1086   1.1  rearnsha plcom_to_tiocm(struct plcom_softc *sc)
   1087   1.1  rearnsha {
   1088   1.1  rearnsha 	u_char plcombits;
   1089   1.1  rearnsha 	int ttybits = 0;
   1090   1.1  rearnsha 
   1091   1.1  rearnsha 	plcombits = sc->sc_mcr;
   1092  1.35     skrll 	if (ISSET(plcombits, PL01X_MCR_DTR))
   1093   1.1  rearnsha 		SET(ttybits, TIOCM_DTR);
   1094  1.35     skrll 	if (ISSET(plcombits, PL01X_MCR_RTS))
   1095   1.1  rearnsha 		SET(ttybits, TIOCM_RTS);
   1096   1.1  rearnsha 
   1097   1.1  rearnsha 	plcombits = sc->sc_msr;
   1098  1.35     skrll 	if (ISSET(plcombits, PL01X_MSR_DCD))
   1099   1.1  rearnsha 		SET(ttybits, TIOCM_CD);
   1100  1.35     skrll 	if (ISSET(plcombits, PL01X_MSR_CTS))
   1101   1.1  rearnsha 		SET(ttybits, TIOCM_CTS);
   1102  1.35     skrll 	if (ISSET(plcombits, PL01X_MSR_DSR))
   1103   1.1  rearnsha 		SET(ttybits, TIOCM_DSR);
   1104   1.1  rearnsha 
   1105   1.1  rearnsha 	if (sc->sc_cr != 0)
   1106   1.1  rearnsha 		SET(ttybits, TIOCM_LE);
   1107   1.1  rearnsha 
   1108   1.1  rearnsha 	return ttybits;
   1109   1.1  rearnsha }
   1110   1.1  rearnsha 
   1111   1.1  rearnsha static u_char
   1112   1.1  rearnsha cflag2lcr(tcflag_t cflag)
   1113   1.1  rearnsha {
   1114   1.1  rearnsha 	u_char lcr = 0;
   1115   1.1  rearnsha 
   1116   1.1  rearnsha 	switch (ISSET(cflag, CSIZE)) {
   1117   1.1  rearnsha 	case CS5:
   1118  1.35     skrll 		SET(lcr, PL01X_LCR_5BITS);
   1119   1.1  rearnsha 		break;
   1120   1.1  rearnsha 	case CS6:
   1121  1.35     skrll 		SET(lcr, PL01X_LCR_6BITS);
   1122   1.1  rearnsha 		break;
   1123   1.1  rearnsha 	case CS7:
   1124  1.35     skrll 		SET(lcr, PL01X_LCR_7BITS);
   1125   1.1  rearnsha 		break;
   1126   1.1  rearnsha 	case CS8:
   1127  1.35     skrll 		SET(lcr, PL01X_LCR_8BITS);
   1128   1.1  rearnsha 		break;
   1129   1.1  rearnsha 	}
   1130   1.1  rearnsha 	if (ISSET(cflag, PARENB)) {
   1131  1.35     skrll 		SET(lcr, PL01X_LCR_PEN);
   1132   1.1  rearnsha 		if (!ISSET(cflag, PARODD))
   1133  1.35     skrll 			SET(lcr, PL01X_LCR_EPS);
   1134   1.1  rearnsha 	}
   1135   1.1  rearnsha 	if (ISSET(cflag, CSTOPB))
   1136  1.35     skrll 		SET(lcr, PL01X_LCR_STP2);
   1137   1.1  rearnsha 
   1138   1.1  rearnsha 	return lcr;
   1139   1.1  rearnsha }
   1140   1.1  rearnsha 
   1141   1.1  rearnsha int
   1142   1.1  rearnsha plcomparam(struct tty *tp, struct termios *t)
   1143   1.1  rearnsha {
   1144  1.28    cegger 	struct plcom_softc *sc =
   1145  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1146   1.1  rearnsha 	int ospeed;
   1147   1.1  rearnsha 	u_char lcr;
   1148   1.1  rearnsha 	int s;
   1149   1.1  rearnsha 
   1150   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
   1151   1.1  rearnsha 		return EIO;
   1152   1.1  rearnsha 
   1153   1.1  rearnsha 	ospeed = plcomspeed(t->c_ospeed, sc->sc_frequency);
   1154   1.1  rearnsha 
   1155   1.1  rearnsha 	/* Check requested parameters. */
   1156   1.1  rearnsha 	if (ospeed < 0)
   1157   1.1  rearnsha 		return EINVAL;
   1158   1.1  rearnsha 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1159   1.1  rearnsha 		return EINVAL;
   1160   1.1  rearnsha 
   1161   1.1  rearnsha 	/*
   1162   1.1  rearnsha 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1163   1.1  rearnsha 	 * is always active.
   1164   1.1  rearnsha 	 */
   1165   1.1  rearnsha 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1166   1.1  rearnsha 	    ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
   1167   1.1  rearnsha 		SET(t->c_cflag, CLOCAL);
   1168   1.1  rearnsha 		CLR(t->c_cflag, HUPCL);
   1169   1.1  rearnsha 	}
   1170   1.1  rearnsha 
   1171   1.1  rearnsha 	/*
   1172   1.1  rearnsha 	 * If there were no changes, don't do anything.  This avoids dropping
   1173   1.1  rearnsha 	 * input and improves performance when all we did was frob things like
   1174   1.1  rearnsha 	 * VMIN and VTIME.
   1175   1.1  rearnsha 	 */
   1176   1.1  rearnsha 	if (tp->t_ospeed == t->c_ospeed &&
   1177   1.1  rearnsha 	    tp->t_cflag == t->c_cflag)
   1178   1.1  rearnsha 		return 0;
   1179   1.1  rearnsha 
   1180  1.35     skrll 	lcr = ISSET(sc->sc_lcr, PL01X_LCR_BRK) | cflag2lcr(t->c_cflag);
   1181   1.1  rearnsha 
   1182   1.1  rearnsha 	s = splserial();
   1183   1.1  rearnsha 	PLCOM_LOCK(sc);
   1184   1.1  rearnsha 
   1185   1.1  rearnsha 	sc->sc_lcr = lcr;
   1186   1.1  rearnsha 
   1187   1.1  rearnsha 	/*
   1188   1.1  rearnsha 	 * PL010 has a fixed-length FIFO trigger point.
   1189   1.1  rearnsha 	 */
   1190   1.1  rearnsha 	if (ISSET(sc->sc_hwflags, PLCOM_HW_FIFO))
   1191   1.1  rearnsha 		sc->sc_fifo = 1;
   1192   1.1  rearnsha 	else
   1193   1.1  rearnsha 		sc->sc_fifo = 0;
   1194   1.1  rearnsha 
   1195   1.1  rearnsha 	if (sc->sc_fifo)
   1196  1.35     skrll 		SET(sc->sc_lcr, PL01X_LCR_FEN);
   1197   1.1  rearnsha 
   1198   1.1  rearnsha 	/*
   1199   1.1  rearnsha 	 * If we're not in a mode that assumes a connection is present, then
   1200   1.1  rearnsha 	 * ignore carrier changes.
   1201   1.1  rearnsha 	 */
   1202   1.1  rearnsha 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1203   1.1  rearnsha 		sc->sc_msr_dcd = 0;
   1204   1.1  rearnsha 	else
   1205  1.35     skrll 		sc->sc_msr_dcd = PL01X_MSR_DCD;
   1206   1.1  rearnsha 	/*
   1207   1.1  rearnsha 	 * Set the flow control pins depending on the current flow control
   1208   1.1  rearnsha 	 * mode.
   1209   1.1  rearnsha 	 */
   1210   1.1  rearnsha 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1211  1.35     skrll 		sc->sc_mcr_dtr = PL01X_MCR_DTR;
   1212  1.35     skrll 		sc->sc_mcr_rts = PL01X_MCR_RTS;
   1213  1.35     skrll 		sc->sc_msr_cts = PL01X_MSR_CTS;
   1214   1.1  rearnsha 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1215   1.1  rearnsha 		/*
   1216   1.1  rearnsha 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1217   1.1  rearnsha 		 * carrier detection.
   1218   1.1  rearnsha 		 */
   1219   1.1  rearnsha 		sc->sc_mcr_dtr = 0;
   1220  1.35     skrll 		sc->sc_mcr_rts = PL01X_MCR_DTR;
   1221  1.35     skrll 		sc->sc_msr_cts = PL01X_MSR_DCD;
   1222   1.1  rearnsha 	} else {
   1223   1.1  rearnsha 		/*
   1224   1.1  rearnsha 		 * If no flow control, then always set RTS.  This will make
   1225   1.1  rearnsha 		 * the other side happy if it mistakenly thinks we're doing
   1226   1.1  rearnsha 		 * RTS/CTS flow control.
   1227   1.1  rearnsha 		 */
   1228  1.35     skrll 		sc->sc_mcr_dtr = PL01X_MCR_DTR | PL01X_MCR_RTS;
   1229   1.1  rearnsha 		sc->sc_mcr_rts = 0;
   1230   1.1  rearnsha 		sc->sc_msr_cts = 0;
   1231  1.35     skrll 		if (ISSET(sc->sc_mcr, PL01X_MCR_DTR))
   1232  1.35     skrll 			SET(sc->sc_mcr, PL01X_MCR_RTS);
   1233   1.1  rearnsha 		else
   1234  1.35     skrll 			CLR(sc->sc_mcr, PL01X_MCR_RTS);
   1235   1.1  rearnsha 	}
   1236   1.1  rearnsha 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1237   1.1  rearnsha 
   1238   1.1  rearnsha #if 0
   1239   1.1  rearnsha 	if (ospeed == 0)
   1240   1.1  rearnsha 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1241   1.1  rearnsha 	else
   1242   1.1  rearnsha 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1243   1.1  rearnsha #endif
   1244   1.1  rearnsha 
   1245   1.1  rearnsha 	sc->sc_dlbl = ospeed;
   1246   1.1  rearnsha 	sc->sc_dlbh = ospeed >> 8;
   1247   1.1  rearnsha 
   1248   1.1  rearnsha 	/* And copy to tty. */
   1249   1.1  rearnsha 	tp->t_ispeed = 0;
   1250   1.1  rearnsha 	tp->t_ospeed = t->c_ospeed;
   1251   1.1  rearnsha 	tp->t_cflag = t->c_cflag;
   1252   1.1  rearnsha 
   1253   1.1  rearnsha 	if (!sc->sc_heldchange) {
   1254   1.1  rearnsha 		if (sc->sc_tx_busy) {
   1255   1.1  rearnsha 			sc->sc_heldtbc = sc->sc_tbc;
   1256   1.1  rearnsha 			sc->sc_tbc = 0;
   1257   1.1  rearnsha 			sc->sc_heldchange = 1;
   1258   1.1  rearnsha 		} else
   1259   1.1  rearnsha 			plcom_loadchannelregs(sc);
   1260   1.1  rearnsha 	}
   1261   1.1  rearnsha 
   1262   1.1  rearnsha 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1263   1.1  rearnsha 		/* Disable the high water mark. */
   1264   1.1  rearnsha 		sc->sc_r_hiwat = 0;
   1265   1.1  rearnsha 		sc->sc_r_lowat = 0;
   1266   1.1  rearnsha 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1267   1.1  rearnsha 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1268   1.1  rearnsha 			plcom_schedrx(sc);
   1269   1.1  rearnsha 		}
   1270   1.1  rearnsha 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1271   1.1  rearnsha 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1272   1.1  rearnsha 			plcom_hwiflow(sc);
   1273   1.1  rearnsha 		}
   1274   1.1  rearnsha 	} else {
   1275   1.1  rearnsha 		sc->sc_r_hiwat = plcom_rbuf_hiwat;
   1276   1.1  rearnsha 		sc->sc_r_lowat = plcom_rbuf_lowat;
   1277   1.1  rearnsha 	}
   1278   1.1  rearnsha 
   1279   1.1  rearnsha 	PLCOM_UNLOCK(sc);
   1280   1.1  rearnsha 	splx(s);
   1281   1.1  rearnsha 
   1282   1.1  rearnsha 	/*
   1283   1.1  rearnsha 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1284   1.1  rearnsha 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1285   1.1  rearnsha 	 * explicit request.
   1286   1.1  rearnsha 	 */
   1287  1.35     skrll 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, PL01X_MSR_DCD));
   1288   1.1  rearnsha 
   1289   1.1  rearnsha #ifdef PLCOM_DEBUG
   1290   1.1  rearnsha 	if (plcom_debug)
   1291   1.1  rearnsha 		plcomstatus(sc, "plcomparam ");
   1292   1.1  rearnsha #endif
   1293   1.1  rearnsha 
   1294   1.1  rearnsha 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1295   1.1  rearnsha 		if (sc->sc_tx_stopped) {
   1296   1.1  rearnsha 			sc->sc_tx_stopped = 0;
   1297   1.1  rearnsha 			plcomstart(tp);
   1298   1.1  rearnsha 		}
   1299   1.1  rearnsha 	}
   1300   1.1  rearnsha 
   1301   1.1  rearnsha 	return 0;
   1302   1.1  rearnsha }
   1303   1.1  rearnsha 
   1304   1.1  rearnsha void
   1305   1.1  rearnsha plcom_iflush(struct plcom_softc *sc)
   1306   1.1  rearnsha {
   1307   1.1  rearnsha 	bus_space_tag_t iot = sc->sc_iot;
   1308   1.1  rearnsha 	bus_space_handle_t ioh = sc->sc_ioh;
   1309   1.1  rearnsha #ifdef DIAGNOSTIC
   1310   1.1  rearnsha 	int reg;
   1311   1.1  rearnsha #endif
   1312   1.1  rearnsha 	int timo;
   1313   1.1  rearnsha 
   1314   1.1  rearnsha #ifdef DIAGNOSTIC
   1315   1.1  rearnsha 	reg = 0xffff;
   1316   1.1  rearnsha #endif
   1317   1.1  rearnsha 	timo = 50000;
   1318   1.1  rearnsha 	/* flush any pending I/O */
   1319  1.35     skrll 	while (! ISSET(bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_RXFE)
   1320   1.1  rearnsha 	    && --timo)
   1321   1.1  rearnsha #ifdef DIAGNOSTIC
   1322   1.1  rearnsha 		reg =
   1323   1.1  rearnsha #else
   1324   1.1  rearnsha 		    (void)
   1325   1.1  rearnsha #endif
   1326   1.1  rearnsha 		    bus_space_read_1(iot, ioh, plcom_dr);
   1327   1.1  rearnsha #ifdef DIAGNOSTIC
   1328   1.1  rearnsha 	if (!timo)
   1329   1.1  rearnsha 		printf("%s: plcom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1330   1.1  rearnsha 		       reg);
   1331   1.1  rearnsha #endif
   1332   1.1  rearnsha }
   1333   1.1  rearnsha 
   1334   1.1  rearnsha void
   1335   1.1  rearnsha plcom_loadchannelregs(struct plcom_softc *sc)
   1336   1.1  rearnsha {
   1337   1.1  rearnsha 	bus_space_tag_t iot = sc->sc_iot;
   1338   1.1  rearnsha 	bus_space_handle_t ioh = sc->sc_ioh;
   1339   1.1  rearnsha 
   1340   1.1  rearnsha 	/* XXXXX necessary? */
   1341   1.1  rearnsha 	plcom_iflush(sc);
   1342   1.1  rearnsha 
   1343   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_cr, 0);
   1344   1.1  rearnsha 
   1345   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_dlbl, sc->sc_dlbl);
   1346   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_dlbh, sc->sc_dlbh);
   1347   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_lcr, sc->sc_lcr);
   1348  1.15   thorpej 	/* XXX device_unit() abuse */
   1349  1.15   thorpej 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
   1350   1.1  rearnsha 	    sc->sc_mcr_active = sc->sc_mcr);
   1351   1.1  rearnsha 
   1352   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
   1353   1.1  rearnsha }
   1354   1.1  rearnsha 
   1355   1.1  rearnsha int
   1356   1.1  rearnsha plcomhwiflow(struct tty *tp, int block)
   1357   1.1  rearnsha {
   1358  1.28    cegger 	struct plcom_softc *sc =
   1359  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1360   1.1  rearnsha 	int s;
   1361   1.1  rearnsha 
   1362   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
   1363   1.1  rearnsha 		return 0;
   1364   1.1  rearnsha 
   1365   1.1  rearnsha 	if (sc->sc_mcr_rts == 0)
   1366   1.1  rearnsha 		return 0;
   1367   1.1  rearnsha 
   1368   1.1  rearnsha 	s = splserial();
   1369   1.1  rearnsha 	PLCOM_LOCK(sc);
   1370   1.1  rearnsha 
   1371   1.1  rearnsha 	if (block) {
   1372   1.1  rearnsha 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1373   1.1  rearnsha 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1374   1.1  rearnsha 			plcom_hwiflow(sc);
   1375   1.1  rearnsha 		}
   1376   1.1  rearnsha 	} else {
   1377   1.1  rearnsha 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1378   1.1  rearnsha 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1379   1.1  rearnsha 			plcom_schedrx(sc);
   1380   1.1  rearnsha 		}
   1381   1.1  rearnsha 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1382   1.1  rearnsha 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1383   1.1  rearnsha 			plcom_hwiflow(sc);
   1384   1.1  rearnsha 		}
   1385   1.1  rearnsha 	}
   1386   1.1  rearnsha 
   1387   1.1  rearnsha 	PLCOM_UNLOCK(sc);
   1388   1.1  rearnsha 	splx(s);
   1389   1.1  rearnsha 	return 1;
   1390   1.1  rearnsha }
   1391   1.1  rearnsha 
   1392   1.1  rearnsha /*
   1393   1.1  rearnsha  * (un)block input via hw flowcontrol
   1394   1.1  rearnsha  */
   1395   1.1  rearnsha void
   1396   1.1  rearnsha plcom_hwiflow(struct plcom_softc *sc)
   1397   1.1  rearnsha {
   1398   1.1  rearnsha 	if (sc->sc_mcr_rts == 0)
   1399   1.1  rearnsha 		return;
   1400   1.1  rearnsha 
   1401   1.1  rearnsha 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1402   1.1  rearnsha 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1403   1.1  rearnsha 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1404   1.1  rearnsha 	} else {
   1405   1.1  rearnsha 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1406   1.1  rearnsha 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1407   1.1  rearnsha 	}
   1408  1.15   thorpej 	/* XXX device_unit() abuse */
   1409  1.15   thorpej 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
   1410   1.1  rearnsha 	    sc->sc_mcr_active);
   1411   1.1  rearnsha }
   1412   1.1  rearnsha 
   1413   1.1  rearnsha 
   1414   1.1  rearnsha void
   1415   1.1  rearnsha plcomstart(struct tty *tp)
   1416   1.1  rearnsha {
   1417  1.28    cegger 	struct plcom_softc *sc =
   1418  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1419   1.1  rearnsha 	bus_space_tag_t iot = sc->sc_iot;
   1420   1.1  rearnsha 	bus_space_handle_t ioh = sc->sc_ioh;
   1421   1.1  rearnsha 	int s;
   1422   1.1  rearnsha 
   1423   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
   1424   1.1  rearnsha 		return;
   1425   1.1  rearnsha 
   1426   1.1  rearnsha 	s = spltty();
   1427   1.1  rearnsha 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1428   1.1  rearnsha 		goto out;
   1429   1.1  rearnsha 	if (sc->sc_tx_stopped)
   1430   1.1  rearnsha 		goto out;
   1431   1.1  rearnsha 
   1432  1.24        ad 	if (!ttypull(tp))
   1433  1.24        ad 		goto out;
   1434   1.1  rearnsha 
   1435   1.1  rearnsha 	/* Grab the first contiguous region of buffer space. */
   1436   1.1  rearnsha 	{
   1437   1.1  rearnsha 		u_char *tba;
   1438   1.1  rearnsha 		int tbc;
   1439   1.1  rearnsha 
   1440   1.1  rearnsha 		tba = tp->t_outq.c_cf;
   1441   1.1  rearnsha 		tbc = ndqb(&tp->t_outq, 0);
   1442   1.1  rearnsha 
   1443   1.1  rearnsha 		(void)splserial();
   1444   1.1  rearnsha 		PLCOM_LOCK(sc);
   1445   1.1  rearnsha 
   1446   1.1  rearnsha 		sc->sc_tba = tba;
   1447   1.1  rearnsha 		sc->sc_tbc = tbc;
   1448   1.1  rearnsha 	}
   1449   1.1  rearnsha 
   1450   1.1  rearnsha 	SET(tp->t_state, TS_BUSY);
   1451   1.1  rearnsha 	sc->sc_tx_busy = 1;
   1452   1.1  rearnsha 
   1453   1.1  rearnsha 	/* Enable transmit completion interrupts if necessary. */
   1454  1.35     skrll 	if (!ISSET(sc->sc_cr, PL010_CR_TIE)) {
   1455  1.35     skrll 		SET(sc->sc_cr, PL010_CR_TIE);
   1456   1.1  rearnsha 		bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
   1457   1.1  rearnsha 	}
   1458   1.1  rearnsha 
   1459   1.1  rearnsha 	/* Output the first chunk of the contiguous buffer. */
   1460   1.1  rearnsha 	{
   1461   1.1  rearnsha 		int n;
   1462   1.1  rearnsha 
   1463   1.1  rearnsha 		n = sc->sc_tbc;
   1464   1.1  rearnsha 		if (n > sc->sc_fifolen)
   1465   1.1  rearnsha 			n = sc->sc_fifolen;
   1466   1.1  rearnsha 		bus_space_write_multi_1(iot, ioh, plcom_dr, sc->sc_tba, n);
   1467   1.1  rearnsha 		sc->sc_tbc -= n;
   1468   1.1  rearnsha 		sc->sc_tba += n;
   1469   1.1  rearnsha 	}
   1470   1.1  rearnsha 	PLCOM_UNLOCK(sc);
   1471   1.1  rearnsha out:
   1472   1.1  rearnsha 	splx(s);
   1473   1.1  rearnsha 	return;
   1474   1.1  rearnsha }
   1475   1.1  rearnsha 
   1476   1.1  rearnsha /*
   1477   1.1  rearnsha  * Stop output on a line.
   1478   1.1  rearnsha  */
   1479   1.1  rearnsha void
   1480   1.1  rearnsha plcomstop(struct tty *tp, int flag)
   1481   1.1  rearnsha {
   1482  1.28    cegger 	struct plcom_softc *sc =
   1483  1.28    cegger 		device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
   1484   1.1  rearnsha 	int s;
   1485   1.1  rearnsha 
   1486   1.1  rearnsha 	s = splserial();
   1487   1.1  rearnsha 	PLCOM_LOCK(sc);
   1488   1.1  rearnsha 	if (ISSET(tp->t_state, TS_BUSY)) {
   1489   1.1  rearnsha 		/* Stop transmitting at the next chunk. */
   1490   1.1  rearnsha 		sc->sc_tbc = 0;
   1491   1.1  rearnsha 		sc->sc_heldtbc = 0;
   1492   1.1  rearnsha 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1493   1.1  rearnsha 			SET(tp->t_state, TS_FLUSH);
   1494   1.1  rearnsha 	}
   1495   1.1  rearnsha 	PLCOM_UNLOCK(sc);
   1496   1.1  rearnsha 	splx(s);
   1497   1.1  rearnsha }
   1498   1.1  rearnsha 
   1499   1.1  rearnsha void
   1500   1.1  rearnsha plcomdiag(void *arg)
   1501   1.1  rearnsha {
   1502   1.1  rearnsha 	struct plcom_softc *sc = arg;
   1503   1.1  rearnsha 	int overflows, floods;
   1504   1.1  rearnsha 	int s;
   1505   1.1  rearnsha 
   1506   1.1  rearnsha 	s = splserial();
   1507   1.1  rearnsha 	PLCOM_LOCK(sc);
   1508   1.1  rearnsha 	overflows = sc->sc_overflows;
   1509   1.1  rearnsha 	sc->sc_overflows = 0;
   1510   1.1  rearnsha 	floods = sc->sc_floods;
   1511   1.1  rearnsha 	sc->sc_floods = 0;
   1512   1.1  rearnsha 	sc->sc_errors = 0;
   1513   1.1  rearnsha 	PLCOM_UNLOCK(sc);
   1514   1.1  rearnsha 	splx(s);
   1515   1.1  rearnsha 
   1516   1.1  rearnsha 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1517   1.1  rearnsha 	    sc->sc_dev.dv_xname,
   1518   1.1  rearnsha 	    overflows, overflows == 1 ? "" : "s",
   1519   1.1  rearnsha 	    floods, floods == 1 ? "" : "s");
   1520   1.1  rearnsha }
   1521   1.1  rearnsha 
   1522   1.1  rearnsha integrate void
   1523   1.1  rearnsha plcom_rxsoft(struct plcom_softc *sc, struct tty *tp)
   1524   1.1  rearnsha {
   1525   1.1  rearnsha 	int (*rint) (int, struct tty *) = tp->t_linesw->l_rint;
   1526   1.1  rearnsha 	u_char *get, *end;
   1527   1.1  rearnsha 	u_int cc, scc;
   1528   1.1  rearnsha 	u_char rsr;
   1529   1.1  rearnsha 	int code;
   1530   1.1  rearnsha 	int s;
   1531   1.1  rearnsha 
   1532   1.1  rearnsha 	end = sc->sc_ebuf;
   1533   1.1  rearnsha 	get = sc->sc_rbget;
   1534   1.1  rearnsha 	scc = cc = plcom_rbuf_size - sc->sc_rbavail;
   1535   1.1  rearnsha 
   1536   1.1  rearnsha 	if (cc == plcom_rbuf_size) {
   1537   1.1  rearnsha 		sc->sc_floods++;
   1538   1.1  rearnsha 		if (sc->sc_errors++ == 0)
   1539   1.1  rearnsha 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1540   1.1  rearnsha 			    plcomdiag, sc);
   1541   1.1  rearnsha 	}
   1542   1.1  rearnsha 
   1543   1.1  rearnsha 	while (cc) {
   1544   1.1  rearnsha 		code = get[0];
   1545   1.1  rearnsha 		rsr = get[1];
   1546  1.35     skrll 		if (ISSET(rsr, PL01X_RSR_ERROR)) {
   1547  1.35     skrll 			if (ISSET(rsr, PL01X_RSR_OE)) {
   1548   1.1  rearnsha 				sc->sc_overflows++;
   1549   1.1  rearnsha 				if (sc->sc_errors++ == 0)
   1550   1.1  rearnsha 					callout_reset(&sc->sc_diag_callout,
   1551   1.1  rearnsha 					    60 * hz, plcomdiag, sc);
   1552   1.1  rearnsha 			}
   1553  1.35     skrll 			if (ISSET(rsr, PL01X_RSR_BE | PL01X_RSR_FE))
   1554   1.1  rearnsha 				SET(code, TTY_FE);
   1555  1.35     skrll 			if (ISSET(rsr, PL01X_RSR_PE))
   1556   1.1  rearnsha 				SET(code, TTY_PE);
   1557   1.1  rearnsha 		}
   1558   1.1  rearnsha 		if ((*rint)(code, tp) == -1) {
   1559   1.1  rearnsha 			/*
   1560   1.1  rearnsha 			 * The line discipline's buffer is out of space.
   1561   1.1  rearnsha 			 */
   1562   1.1  rearnsha 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1563   1.1  rearnsha 				/*
   1564   1.1  rearnsha 				 * We're either not using flow control, or the
   1565   1.1  rearnsha 				 * line discipline didn't tell us to block for
   1566   1.1  rearnsha 				 * some reason.  Either way, we have no way to
   1567   1.1  rearnsha 				 * know when there's more space available, so
   1568   1.1  rearnsha 				 * just drop the rest of the data.
   1569   1.1  rearnsha 				 */
   1570   1.1  rearnsha 				get += cc << 1;
   1571   1.1  rearnsha 				if (get >= end)
   1572   1.1  rearnsha 					get -= plcom_rbuf_size << 1;
   1573   1.1  rearnsha 				cc = 0;
   1574   1.1  rearnsha 			} else {
   1575   1.1  rearnsha 				/*
   1576   1.1  rearnsha 				 * Don't schedule any more receive processing
   1577   1.1  rearnsha 				 * until the line discipline tells us there's
   1578   1.1  rearnsha 				 * space available (through plcomhwiflow()).
   1579   1.1  rearnsha 				 * Leave the rest of the data in the input
   1580   1.1  rearnsha 				 * buffer.
   1581   1.1  rearnsha 				 */
   1582   1.1  rearnsha 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1583   1.1  rearnsha 			}
   1584   1.1  rearnsha 			break;
   1585   1.1  rearnsha 		}
   1586   1.1  rearnsha 		get += 2;
   1587   1.1  rearnsha 		if (get >= end)
   1588   1.1  rearnsha 			get = sc->sc_rbuf;
   1589   1.1  rearnsha 		cc--;
   1590   1.1  rearnsha 	}
   1591   1.1  rearnsha 
   1592   1.1  rearnsha 	if (cc != scc) {
   1593   1.1  rearnsha 		sc->sc_rbget = get;
   1594   1.1  rearnsha 		s = splserial();
   1595   1.1  rearnsha 		PLCOM_LOCK(sc);
   1596   1.1  rearnsha 
   1597   1.1  rearnsha 		cc = sc->sc_rbavail += scc - cc;
   1598   1.1  rearnsha 		/* Buffers should be ok again, release possible block. */
   1599   1.1  rearnsha 		if (cc >= sc->sc_r_lowat) {
   1600   1.1  rearnsha 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1601   1.1  rearnsha 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1602  1.35     skrll 				SET(sc->sc_cr, PL010_CR_RIE | PL010_CR_RTIE);
   1603   1.1  rearnsha 				bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
   1604   1.1  rearnsha 			}
   1605   1.1  rearnsha 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1606   1.1  rearnsha 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1607   1.1  rearnsha 				plcom_hwiflow(sc);
   1608   1.1  rearnsha 			}
   1609   1.1  rearnsha 		}
   1610   1.1  rearnsha 		PLCOM_UNLOCK(sc);
   1611   1.1  rearnsha 		splx(s);
   1612   1.1  rearnsha 	}
   1613   1.1  rearnsha }
   1614   1.1  rearnsha 
   1615   1.1  rearnsha integrate void
   1616   1.1  rearnsha plcom_txsoft(struct plcom_softc *sc, struct tty *tp)
   1617   1.1  rearnsha {
   1618   1.1  rearnsha 
   1619   1.1  rearnsha 	CLR(tp->t_state, TS_BUSY);
   1620   1.1  rearnsha 	if (ISSET(tp->t_state, TS_FLUSH))
   1621   1.1  rearnsha 		CLR(tp->t_state, TS_FLUSH);
   1622   1.1  rearnsha 	else
   1623   1.1  rearnsha 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1624   1.1  rearnsha 	(*tp->t_linesw->l_start)(tp);
   1625   1.1  rearnsha }
   1626   1.1  rearnsha 
   1627   1.1  rearnsha integrate void
   1628   1.1  rearnsha plcom_stsoft(struct plcom_softc *sc, struct tty *tp)
   1629   1.1  rearnsha {
   1630   1.1  rearnsha 	u_char msr, delta;
   1631   1.1  rearnsha 	int s;
   1632   1.1  rearnsha 
   1633   1.1  rearnsha 	s = splserial();
   1634   1.1  rearnsha 	PLCOM_LOCK(sc);
   1635   1.1  rearnsha 	msr = sc->sc_msr;
   1636   1.1  rearnsha 	delta = sc->sc_msr_delta;
   1637   1.1  rearnsha 	sc->sc_msr_delta = 0;
   1638   1.1  rearnsha 	PLCOM_UNLOCK(sc);
   1639   1.1  rearnsha 	splx(s);
   1640   1.1  rearnsha 
   1641   1.1  rearnsha 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1642   1.1  rearnsha 		/*
   1643   1.1  rearnsha 		 * Inform the tty layer that carrier detect changed.
   1644   1.1  rearnsha 		 */
   1645  1.35     skrll 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, PL01X_MSR_DCD));
   1646   1.1  rearnsha 	}
   1647   1.1  rearnsha 
   1648   1.1  rearnsha 	if (ISSET(delta, sc->sc_msr_cts)) {
   1649   1.1  rearnsha 		/* Block or unblock output according to flow control. */
   1650   1.1  rearnsha 		if (ISSET(msr, sc->sc_msr_cts)) {
   1651   1.1  rearnsha 			sc->sc_tx_stopped = 0;
   1652   1.1  rearnsha 			(*tp->t_linesw->l_start)(tp);
   1653   1.1  rearnsha 		} else {
   1654   1.1  rearnsha 			sc->sc_tx_stopped = 1;
   1655   1.1  rearnsha 		}
   1656   1.1  rearnsha 	}
   1657   1.1  rearnsha 
   1658   1.1  rearnsha #ifdef PLCOM_DEBUG
   1659   1.1  rearnsha 	if (plcom_debug)
   1660   1.1  rearnsha 		plcomstatus(sc, "plcom_stsoft");
   1661   1.1  rearnsha #endif
   1662   1.1  rearnsha }
   1663   1.1  rearnsha 
   1664   1.1  rearnsha void
   1665   1.1  rearnsha plcomsoft(void *arg)
   1666   1.1  rearnsha {
   1667   1.1  rearnsha 	struct plcom_softc *sc = arg;
   1668   1.1  rearnsha 	struct tty *tp;
   1669   1.1  rearnsha 
   1670   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
   1671   1.1  rearnsha 		return;
   1672   1.1  rearnsha 
   1673  1.21        ad 	tp = sc->sc_tty;
   1674   1.1  rearnsha 
   1675  1.21        ad 	if (sc->sc_rx_ready) {
   1676  1.21        ad 		sc->sc_rx_ready = 0;
   1677  1.21        ad 		plcom_rxsoft(sc, tp);
   1678  1.21        ad 	}
   1679   1.1  rearnsha 
   1680  1.21        ad 	if (sc->sc_st_check) {
   1681  1.21        ad 		sc->sc_st_check = 0;
   1682  1.21        ad 		plcom_stsoft(sc, tp);
   1683  1.21        ad 	}
   1684   1.1  rearnsha 
   1685  1.21        ad 	if (sc->sc_tx_done) {
   1686  1.21        ad 		sc->sc_tx_done = 0;
   1687  1.21        ad 		plcom_txsoft(sc, tp);
   1688   1.1  rearnsha 	}
   1689   1.1  rearnsha }
   1690   1.1  rearnsha 
   1691   1.1  rearnsha int
   1692   1.1  rearnsha plcomintr(void *arg)
   1693   1.1  rearnsha {
   1694   1.1  rearnsha 	struct plcom_softc *sc = arg;
   1695   1.1  rearnsha 	bus_space_tag_t iot = sc->sc_iot;
   1696   1.1  rearnsha 	bus_space_handle_t ioh = sc->sc_ioh;
   1697   1.1  rearnsha 	u_char *put, *end;
   1698   1.1  rearnsha 	u_int cc;
   1699   1.1  rearnsha 	u_char rsr, iir;
   1700   1.1  rearnsha 
   1701   1.1  rearnsha 	if (PLCOM_ISALIVE(sc) == 0)
   1702   1.1  rearnsha 		return 0;
   1703   1.1  rearnsha 
   1704   1.1  rearnsha 	PLCOM_LOCK(sc);
   1705   1.1  rearnsha 	iir = bus_space_read_1(iot, ioh, plcom_iir);
   1706  1.35     skrll 	if (! ISSET(iir, PL010_IIR_IMASK)) {
   1707   1.1  rearnsha 		PLCOM_UNLOCK(sc);
   1708   1.1  rearnsha 		return 0;
   1709   1.1  rearnsha 	}
   1710   1.1  rearnsha 
   1711   1.1  rearnsha 	end = sc->sc_ebuf;
   1712   1.1  rearnsha 	put = sc->sc_rbput;
   1713   1.1  rearnsha 	cc = sc->sc_rbavail;
   1714   1.1  rearnsha 
   1715   1.1  rearnsha 	do {
   1716   1.1  rearnsha 		u_char	msr, delta, fr;
   1717   1.1  rearnsha 
   1718   1.1  rearnsha 		fr = bus_space_read_1(iot, ioh, plcom_fr);
   1719   1.1  rearnsha 
   1720  1.35     skrll 		if (!ISSET(fr, PL01X_FR_RXFE) &&
   1721   1.1  rearnsha 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1722   1.1  rearnsha 			while (cc > 0) {
   1723   1.1  rearnsha 				int cn_trapped = 0;
   1724   1.1  rearnsha 				put[0] = bus_space_read_1(iot, ioh,
   1725   1.1  rearnsha 				    plcom_dr);
   1726   1.1  rearnsha 				rsr = bus_space_read_1(iot, ioh, plcom_rsr);
   1727   1.1  rearnsha 				/* Clear any error status.  */
   1728  1.35     skrll 				if (ISSET(rsr, PL01X_RSR_ERROR))
   1729   1.1  rearnsha 					bus_space_write_1(iot, ioh, plcom_ecr,
   1730   1.1  rearnsha 					    0);
   1731  1.35     skrll 				if (ISSET(rsr, PL01X_RSR_BE)) {
   1732  1.10  rearnsha 					cn_trapped = 0;
   1733   1.1  rearnsha 					cn_check_magic(sc->sc_tty->t_dev,
   1734   1.1  rearnsha 					    CNC_BREAK, plcom_cnm_state);
   1735   1.1  rearnsha 					if (cn_trapped)
   1736   1.1  rearnsha 						continue;
   1737   1.1  rearnsha #if defined(KGDB)
   1738   1.1  rearnsha 					if (ISSET(sc->sc_hwflags,
   1739   1.1  rearnsha 					    PLCOM_HW_KGDB)) {
   1740   1.1  rearnsha 						kgdb_connect(1);
   1741   1.1  rearnsha 						continue;
   1742   1.1  rearnsha 					}
   1743   1.1  rearnsha #endif
   1744   1.1  rearnsha 				}
   1745   1.1  rearnsha 
   1746   1.1  rearnsha 				put[1] = rsr;
   1747  1.10  rearnsha 				cn_trapped = 0;
   1748   1.1  rearnsha 				cn_check_magic(sc->sc_tty->t_dev,
   1749   1.1  rearnsha 					       put[0], plcom_cnm_state);
   1750   1.1  rearnsha 				if (cn_trapped) {
   1751   1.1  rearnsha 					fr = bus_space_read_1(iot, ioh,
   1752   1.1  rearnsha 					    plcom_fr);
   1753  1.35     skrll 					if (ISSET(fr, PL01X_FR_RXFE))
   1754   1.1  rearnsha 						break;
   1755   1.1  rearnsha 
   1756   1.1  rearnsha 					continue;
   1757   1.1  rearnsha 				}
   1758   1.1  rearnsha 				put += 2;
   1759   1.1  rearnsha 				if (put >= end)
   1760   1.1  rearnsha 					put = sc->sc_rbuf;
   1761   1.1  rearnsha 				cc--;
   1762   1.1  rearnsha 
   1763   1.1  rearnsha 				fr = bus_space_read_1(iot, ioh, plcom_fr);
   1764  1.35     skrll 				if (ISSET(fr, PL01X_FR_RXFE))
   1765   1.1  rearnsha 					break;
   1766   1.1  rearnsha 			}
   1767   1.1  rearnsha 
   1768   1.1  rearnsha 			/*
   1769   1.1  rearnsha 			 * Current string of incoming characters ended because
   1770   1.1  rearnsha 			 * no more data was available or we ran out of space.
   1771   1.1  rearnsha 			 * Schedule a receive event if any data was received.
   1772   1.1  rearnsha 			 * If we're out of space, turn off receive interrupts.
   1773   1.1  rearnsha 			 */
   1774   1.1  rearnsha 			sc->sc_rbput = put;
   1775   1.1  rearnsha 			sc->sc_rbavail = cc;
   1776   1.1  rearnsha 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1777   1.1  rearnsha 				sc->sc_rx_ready = 1;
   1778   1.1  rearnsha 
   1779   1.1  rearnsha 			/*
   1780   1.1  rearnsha 			 * See if we are in danger of overflowing a buffer. If
   1781   1.1  rearnsha 			 * so, use hardware flow control to ease the pressure.
   1782   1.1  rearnsha 			 */
   1783   1.1  rearnsha 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1784   1.1  rearnsha 			    cc < sc->sc_r_hiwat) {
   1785   1.1  rearnsha 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1786   1.1  rearnsha 				plcom_hwiflow(sc);
   1787   1.1  rearnsha 			}
   1788   1.1  rearnsha 
   1789   1.1  rearnsha 			/*
   1790   1.1  rearnsha 			 * If we're out of space, disable receive interrupts
   1791   1.1  rearnsha 			 * until the queue has drained a bit.
   1792   1.1  rearnsha 			 */
   1793   1.1  rearnsha 			if (!cc) {
   1794   1.1  rearnsha 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1795  1.35     skrll 				CLR(sc->sc_cr, PL010_CR_RIE | PL010_CR_RTIE);
   1796   1.1  rearnsha 				bus_space_write_1(iot, ioh, plcom_cr,
   1797   1.1  rearnsha 				    sc->sc_cr);
   1798   1.1  rearnsha 			}
   1799   1.1  rearnsha 		} else {
   1800  1.35     skrll 			if (ISSET(iir, PL010_IIR_RIS)) {
   1801   1.1  rearnsha 				bus_space_write_1(iot, ioh, plcom_cr, 0);
   1802   1.1  rearnsha 				delay(10);
   1803   1.1  rearnsha 				bus_space_write_1(iot, ioh, plcom_cr,
   1804   1.1  rearnsha 				    sc->sc_cr);
   1805   1.1  rearnsha 				continue;
   1806   1.1  rearnsha 			}
   1807   1.1  rearnsha 		}
   1808   1.1  rearnsha 
   1809   1.1  rearnsha 		msr = bus_space_read_1(iot, ioh, plcom_fr);
   1810   1.1  rearnsha 		delta = msr ^ sc->sc_msr;
   1811   1.1  rearnsha 		sc->sc_msr = msr;
   1812   1.1  rearnsha 		/* Clear any pending modem status interrupt.  */
   1813  1.35     skrll 		if (iir & PL010_IIR_MIS)
   1814   1.1  rearnsha 			bus_space_write_1(iot, ioh, plcom_icr, 0);
   1815   1.1  rearnsha 		/*
   1816   1.1  rearnsha 		 * Pulse-per-second (PSS) signals on edge of DCD?
   1817   1.1  rearnsha 		 * Process these even if line discipline is ignoring DCD.
   1818   1.1  rearnsha 		 */
   1819   1.1  rearnsha 		if (delta & sc->sc_ppsmask) {
   1820   1.1  rearnsha 			struct timeval tv;
   1821  1.26        ad 			mutex_spin_enter(&timecounter_lock);
   1822   1.1  rearnsha 		    	if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
   1823   1.1  rearnsha 				/* XXX nanotime() */
   1824   1.1  rearnsha 				microtime(&tv);
   1825   1.1  rearnsha 				TIMEVAL_TO_TIMESPEC(&tv,
   1826   1.1  rearnsha 				    &sc->ppsinfo.assert_timestamp);
   1827   1.1  rearnsha 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   1828   1.1  rearnsha 					timespecadd(&sc->ppsinfo.assert_timestamp,
   1829   1.1  rearnsha 					    &sc->ppsparam.assert_offset,
   1830   1.1  rearnsha 						    &sc->ppsinfo.assert_timestamp);
   1831   1.1  rearnsha 				}
   1832   1.1  rearnsha 
   1833   1.1  rearnsha #ifdef PPS_SYNC
   1834   1.1  rearnsha 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   1835   1.1  rearnsha 					hardpps(&tv, tv.tv_usec);
   1836   1.1  rearnsha #endif
   1837   1.1  rearnsha 				sc->ppsinfo.assert_sequence++;
   1838   1.1  rearnsha 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1839   1.1  rearnsha 
   1840   1.1  rearnsha 			} else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
   1841   1.1  rearnsha 				/* XXX nanotime() */
   1842   1.1  rearnsha 				microtime(&tv);
   1843   1.1  rearnsha 				TIMEVAL_TO_TIMESPEC(&tv,
   1844   1.1  rearnsha 				    &sc->ppsinfo.clear_timestamp);
   1845   1.1  rearnsha 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   1846   1.1  rearnsha 					timespecadd(&sc->ppsinfo.clear_timestamp,
   1847   1.1  rearnsha 					    &sc->ppsparam.clear_offset,
   1848   1.1  rearnsha 					    &sc->ppsinfo.clear_timestamp);
   1849   1.1  rearnsha 				}
   1850   1.1  rearnsha 
   1851   1.1  rearnsha #ifdef PPS_SYNC
   1852   1.1  rearnsha 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   1853   1.1  rearnsha 					hardpps(&tv, tv.tv_usec);
   1854   1.1  rearnsha #endif
   1855   1.1  rearnsha 				sc->ppsinfo.clear_sequence++;
   1856   1.1  rearnsha 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1857   1.1  rearnsha 			}
   1858  1.26        ad 			mutex_spin_exit(&timecounter_lock);
   1859   1.1  rearnsha 		}
   1860   1.1  rearnsha 
   1861   1.1  rearnsha 		/*
   1862   1.1  rearnsha 		 * Process normal status changes
   1863   1.1  rearnsha 		 */
   1864   1.1  rearnsha 		if (ISSET(delta, sc->sc_msr_mask)) {
   1865   1.1  rearnsha 			SET(sc->sc_msr_delta, delta);
   1866   1.1  rearnsha 
   1867   1.1  rearnsha 			/*
   1868   1.1  rearnsha 			 * Stop output immediately if we lose the output
   1869   1.1  rearnsha 			 * flow control signal or carrier detect.
   1870   1.1  rearnsha 			 */
   1871   1.1  rearnsha 			if (ISSET(~msr, sc->sc_msr_mask)) {
   1872   1.1  rearnsha 				sc->sc_tbc = 0;
   1873   1.1  rearnsha 				sc->sc_heldtbc = 0;
   1874   1.1  rearnsha #ifdef PLCOM_DEBUG
   1875   1.1  rearnsha 				if (plcom_debug)
   1876   1.1  rearnsha 					plcomstatus(sc, "plcomintr  ");
   1877   1.1  rearnsha #endif
   1878   1.1  rearnsha 			}
   1879   1.1  rearnsha 
   1880   1.1  rearnsha 			sc->sc_st_check = 1;
   1881   1.1  rearnsha 		}
   1882   1.1  rearnsha 
   1883   1.1  rearnsha 		/*
   1884   1.1  rearnsha 		 * Done handling any receive interrupts. See if data
   1885   1.1  rearnsha 		 * can be * transmitted as well. Schedule tx done
   1886   1.1  rearnsha 		 * event if no data left * and tty was marked busy.
   1887   1.1  rearnsha 		 */
   1888  1.35     skrll 		if (ISSET(iir, PL010_IIR_TIS)) {
   1889   1.1  rearnsha 			/*
   1890   1.1  rearnsha 			 * If we've delayed a parameter change, do it
   1891   1.1  rearnsha 			 * now, and restart * output.
   1892   1.1  rearnsha 			 */
   1893   1.1  rearnsha 			if (sc->sc_heldchange) {
   1894   1.1  rearnsha 				plcom_loadchannelregs(sc);
   1895   1.1  rearnsha 				sc->sc_heldchange = 0;
   1896   1.1  rearnsha 				sc->sc_tbc = sc->sc_heldtbc;
   1897   1.1  rearnsha 				sc->sc_heldtbc = 0;
   1898   1.1  rearnsha 			}
   1899   1.1  rearnsha 
   1900   1.1  rearnsha 			/*
   1901   1.1  rearnsha 			 * Output the next chunk of the contiguous
   1902   1.1  rearnsha 			 * buffer, if any.
   1903   1.1  rearnsha 			 */
   1904   1.1  rearnsha 			if (sc->sc_tbc > 0) {
   1905   1.1  rearnsha 				int n;
   1906   1.1  rearnsha 
   1907   1.1  rearnsha 				n = sc->sc_tbc;
   1908   1.1  rearnsha 				if (n > sc->sc_fifolen)
   1909   1.1  rearnsha 					n = sc->sc_fifolen;
   1910   1.1  rearnsha 				bus_space_write_multi_1(iot, ioh, plcom_dr,
   1911   1.1  rearnsha 				    sc->sc_tba, n);
   1912   1.1  rearnsha 				sc->sc_tbc -= n;
   1913   1.1  rearnsha 				sc->sc_tba += n;
   1914   1.1  rearnsha 			} else {
   1915   1.1  rearnsha 				/*
   1916   1.1  rearnsha 				 * Disable transmit plcompletion
   1917   1.1  rearnsha 				 * interrupts if necessary.
   1918   1.1  rearnsha 				 */
   1919  1.35     skrll 				if (ISSET(sc->sc_cr, PL010_CR_TIE)) {
   1920  1.35     skrll 					CLR(sc->sc_cr, PL010_CR_TIE);
   1921   1.1  rearnsha 					bus_space_write_1(iot, ioh, plcom_cr,
   1922   1.1  rearnsha 					    sc->sc_cr);
   1923   1.1  rearnsha 				}
   1924   1.1  rearnsha 				if (sc->sc_tx_busy) {
   1925   1.1  rearnsha 					sc->sc_tx_busy = 0;
   1926   1.1  rearnsha 					sc->sc_tx_done = 1;
   1927   1.1  rearnsha 				}
   1928   1.1  rearnsha 			}
   1929   1.1  rearnsha 		}
   1930   1.1  rearnsha 	} while (ISSET((iir = bus_space_read_1(iot, ioh, plcom_iir)),
   1931  1.35     skrll 	    PL010_IIR_IMASK));
   1932   1.1  rearnsha 
   1933   1.1  rearnsha 	PLCOM_UNLOCK(sc);
   1934   1.1  rearnsha 
   1935   1.1  rearnsha 	/* Wake up the poller. */
   1936  1.25        ad 	softint_schedule(sc->sc_si);
   1937   1.1  rearnsha 
   1938  1.33       tls #ifdef RND_COM
   1939   1.1  rearnsha 	rnd_add_uint32(&sc->rnd_source, iir | rsr);
   1940   1.1  rearnsha #endif
   1941   1.1  rearnsha 
   1942   1.1  rearnsha 	return 1;
   1943   1.1  rearnsha }
   1944   1.1  rearnsha 
   1945   1.1  rearnsha /*
   1946   1.1  rearnsha  * The following functions are polled getc and putc routines, shared
   1947   1.1  rearnsha  * by the console and kgdb glue.
   1948   1.1  rearnsha  *
   1949   1.1  rearnsha  * The read-ahead code is so that you can detect pending in-band
   1950   1.1  rearnsha  * cn_magic in polled mode while doing output rather than having to
   1951   1.1  rearnsha  * wait until the kernel decides it needs input.
   1952   1.1  rearnsha  */
   1953   1.1  rearnsha 
   1954   1.1  rearnsha #define MAX_READAHEAD	20
   1955   1.1  rearnsha static int plcom_readahead[MAX_READAHEAD];
   1956   1.1  rearnsha static int plcom_readaheadcount = 0;
   1957   1.1  rearnsha 
   1958   1.1  rearnsha int
   1959   1.1  rearnsha plcom_common_getc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh)
   1960   1.1  rearnsha {
   1961   1.1  rearnsha 	int s = splserial();
   1962   1.1  rearnsha 	u_char stat, c;
   1963   1.1  rearnsha 
   1964   1.1  rearnsha 	/* got a character from reading things earlier */
   1965   1.1  rearnsha 	if (plcom_readaheadcount > 0) {
   1966   1.1  rearnsha 		int i;
   1967   1.1  rearnsha 
   1968   1.1  rearnsha 		c = plcom_readahead[0];
   1969   1.1  rearnsha 		for (i = 1; i < plcom_readaheadcount; i++) {
   1970   1.1  rearnsha 			plcom_readahead[i-1] = plcom_readahead[i];
   1971   1.1  rearnsha 		}
   1972   1.1  rearnsha 		plcom_readaheadcount--;
   1973   1.1  rearnsha 		splx(s);
   1974   1.1  rearnsha 		return c;
   1975   1.1  rearnsha 	}
   1976   1.1  rearnsha 
   1977   1.1  rearnsha 	/* block until a character becomes available */
   1978  1.35     skrll 	while (ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_RXFE))
   1979   1.1  rearnsha 		;
   1980   1.1  rearnsha 
   1981   1.1  rearnsha 	c = bus_space_read_1(iot, ioh, plcom_dr);
   1982   1.1  rearnsha 	stat = bus_space_read_1(iot, ioh, plcom_iir);
   1983   1.1  rearnsha 	{
   1984   1.1  rearnsha 		int cn_trapped = 0; /* unused */
   1985   1.1  rearnsha #ifdef DDB
   1986   1.1  rearnsha 		extern int db_active;
   1987   1.1  rearnsha 		if (!db_active)
   1988   1.1  rearnsha #endif
   1989   1.1  rearnsha 			cn_check_magic(dev, c, plcom_cnm_state);
   1990   1.1  rearnsha 	}
   1991   1.1  rearnsha 	splx(s);
   1992   1.1  rearnsha 	return c;
   1993   1.1  rearnsha }
   1994   1.1  rearnsha 
   1995   1.1  rearnsha void
   1996   1.1  rearnsha plcom_common_putc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh,
   1997   1.1  rearnsha     int c)
   1998   1.1  rearnsha {
   1999   1.1  rearnsha 	int s = splserial();
   2000   1.1  rearnsha 	int timo;
   2001   1.1  rearnsha 
   2002   1.1  rearnsha 	int cin, stat;
   2003   1.1  rearnsha 	if (plcom_readaheadcount < MAX_READAHEAD
   2004  1.35     skrll 	     && !ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_RXFE)) {
   2005   1.1  rearnsha 		int cn_trapped = 0;
   2006   1.1  rearnsha 		cin = bus_space_read_1(iot, ioh, plcom_dr);
   2007   1.1  rearnsha 		stat = bus_space_read_1(iot, ioh, plcom_iir);
   2008   1.1  rearnsha 		cn_check_magic(dev, cin, plcom_cnm_state);
   2009   1.1  rearnsha 		plcom_readahead[plcom_readaheadcount++] = cin;
   2010   1.1  rearnsha 	}
   2011   1.1  rearnsha 
   2012   1.1  rearnsha 	/* wait for any pending transmission to finish */
   2013   1.1  rearnsha 	timo = 150000;
   2014  1.35     skrll 	while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_TXFE) && --timo)
   2015   1.1  rearnsha 		continue;
   2016   1.1  rearnsha 
   2017   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_dr, c);
   2018   1.1  rearnsha 	PLCOM_BARRIER(iot, ioh, BR | BW);
   2019   1.1  rearnsha 
   2020   1.1  rearnsha 	/* wait for this transmission to complete */
   2021   1.1  rearnsha 	timo = 1500000;
   2022  1.35     skrll 	while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_TXFE) && --timo)
   2023   1.1  rearnsha 		continue;
   2024   1.1  rearnsha 
   2025   1.1  rearnsha 	splx(s);
   2026   1.1  rearnsha }
   2027   1.1  rearnsha 
   2028   1.1  rearnsha /*
   2029   1.1  rearnsha  * Initialize UART for use as console or KGDB line.
   2030   1.1  rearnsha  */
   2031   1.1  rearnsha int
   2032   1.1  rearnsha plcominit(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
   2033   1.1  rearnsha     tcflag_t cflag, bus_space_handle_t *iohp)
   2034   1.1  rearnsha {
   2035   1.1  rearnsha 	bus_space_handle_t ioh;
   2036   1.1  rearnsha 
   2037   1.1  rearnsha 	if (bus_space_map(iot, iobase, PLCOM_UART_SIZE, 0, &ioh))
   2038   1.1  rearnsha 		return ENOMEM; /* ??? */
   2039   1.1  rearnsha 
   2040   1.1  rearnsha 	rate = plcomspeed(rate, frequency);
   2041   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_cr, 0);
   2042   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_dlbl, rate);
   2043   1.1  rearnsha 	bus_space_write_1(iot, ioh, plcom_dlbh, rate >> 8);
   2044  1.35     skrll 	bus_space_write_1(iot, ioh, plcom_lcr, cflag2lcr(cflag) | PL01X_LCR_FEN);
   2045  1.35     skrll 	bus_space_write_1(iot, ioh, plcom_cr, PL01X_CR_UARTEN);
   2046   1.1  rearnsha 
   2047   1.1  rearnsha #if 0
   2048   1.1  rearnsha 	/* Ought to do something like this, but we have no sc to
   2049   1.1  rearnsha 	   dereference. */
   2050  1.15   thorpej 	/* XXX device_unit() abuse */
   2051  1.15   thorpej 	sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
   2052  1.35     skrll 	    PL01X_MCR_DTR | PL01X_MCR_RTS);
   2053   1.1  rearnsha #endif
   2054   1.1  rearnsha 
   2055   1.1  rearnsha 	*iohp = ioh;
   2056   1.1  rearnsha 	return 0;
   2057   1.1  rearnsha }
   2058   1.1  rearnsha 
   2059   1.1  rearnsha /*
   2060   1.1  rearnsha  * Following are all routines needed for PLCOM to act as console
   2061   1.1  rearnsha  */
   2062   1.1  rearnsha struct consdev plcomcons = {
   2063   1.1  rearnsha 	NULL, NULL, plcomcngetc, plcomcnputc, plcomcnpollc, NULL,
   2064   1.6        he 	NULL, NULL, NODEV, CN_NORMAL
   2065   1.1  rearnsha };
   2066   1.1  rearnsha 
   2067   1.1  rearnsha 
   2068   1.1  rearnsha int
   2069   1.1  rearnsha plcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
   2070   1.1  rearnsha     tcflag_t cflag, int unit)
   2071   1.1  rearnsha {
   2072   1.1  rearnsha 	int res;
   2073   1.1  rearnsha 
   2074   1.1  rearnsha 	res = plcominit(iot, iobase, rate, frequency, cflag, &plcomconsioh);
   2075   1.1  rearnsha 	if (res)
   2076   1.1  rearnsha 		return res;
   2077   1.1  rearnsha 
   2078   1.1  rearnsha 	cn_tab = &plcomcons;
   2079   1.1  rearnsha 	cn_init_magic(&plcom_cnm_state);
   2080   1.1  rearnsha 	cn_set_magic("\047\001"); /* default magic is BREAK */
   2081   1.1  rearnsha 
   2082   1.1  rearnsha 	plcomconstag = iot;
   2083   1.1  rearnsha 	plcomconsunit = unit;
   2084   1.1  rearnsha 	plcomconsrate = rate;
   2085   1.1  rearnsha 	plcomconscflag = cflag;
   2086   1.1  rearnsha 
   2087   1.1  rearnsha 	return 0;
   2088   1.1  rearnsha }
   2089   1.1  rearnsha 
   2090   1.1  rearnsha void
   2091   1.1  rearnsha plcomcndetach(void)
   2092   1.1  rearnsha {
   2093   1.1  rearnsha 	bus_space_unmap(plcomconstag, plcomconsioh, PLCOM_UART_SIZE);
   2094   1.1  rearnsha 	plcomconstag = NULL;
   2095   1.1  rearnsha 
   2096   1.1  rearnsha 	cn_tab = NULL;
   2097   1.1  rearnsha }
   2098   1.1  rearnsha 
   2099   1.1  rearnsha int
   2100   1.1  rearnsha plcomcngetc(dev_t dev)
   2101   1.1  rearnsha {
   2102   1.1  rearnsha 	return plcom_common_getc(dev, plcomconstag, plcomconsioh);
   2103   1.1  rearnsha }
   2104   1.1  rearnsha 
   2105   1.1  rearnsha /*
   2106   1.1  rearnsha  * Console kernel output character routine.
   2107   1.1  rearnsha  */
   2108   1.1  rearnsha void
   2109   1.1  rearnsha plcomcnputc(dev_t dev, int c)
   2110   1.1  rearnsha {
   2111   1.1  rearnsha 	plcom_common_putc(dev, plcomconstag, plcomconsioh, c);
   2112   1.1  rearnsha }
   2113   1.1  rearnsha 
   2114   1.1  rearnsha void
   2115   1.1  rearnsha plcomcnpollc(dev_t dev, int on)
   2116   1.1  rearnsha {
   2117   1.1  rearnsha 
   2118   1.1  rearnsha }
   2119   1.1  rearnsha 
   2120   1.1  rearnsha #ifdef KGDB
   2121   1.1  rearnsha int
   2122   1.1  rearnsha plcom_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate,
   2123   1.1  rearnsha    int frequency, tcflag_t cflag, int unit)
   2124   1.1  rearnsha {
   2125   1.1  rearnsha 	int res;
   2126   1.1  rearnsha 
   2127   1.1  rearnsha 	if (iot == plcomconstag && iobase == plcomconsunit)
   2128   1.1  rearnsha 		return EBUSY; /* cannot share with console */
   2129   1.1  rearnsha 
   2130   1.1  rearnsha 	res = plcominit(iot, iobase, rate, frequency, cflag, &plcom_kgdb_ioh);
   2131   1.1  rearnsha 	if (res)
   2132   1.1  rearnsha 		return res;
   2133   1.1  rearnsha 
   2134   1.1  rearnsha 	kgdb_attach(plcom_kgdb_getc, plcom_kgdb_putc, NULL);
   2135   1.1  rearnsha 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2136   1.1  rearnsha 
   2137   1.1  rearnsha 	plcom_kgdb_iot = iot;
   2138   1.1  rearnsha 	plcom_kgdb_unit = unit;
   2139   1.1  rearnsha 
   2140   1.1  rearnsha 	return 0;
   2141   1.1  rearnsha }
   2142   1.1  rearnsha 
   2143   1.1  rearnsha /* ARGSUSED */
   2144   1.1  rearnsha int
   2145   1.1  rearnsha plcom_kgdb_getc(void *arg)
   2146   1.1  rearnsha {
   2147   1.1  rearnsha 	return plcom_common_getc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh);
   2148   1.1  rearnsha }
   2149   1.1  rearnsha 
   2150   1.1  rearnsha /* ARGSUSED */
   2151   1.1  rearnsha void
   2152   1.1  rearnsha plcom_kgdb_putc(void *arg, int c)
   2153   1.1  rearnsha {
   2154   1.1  rearnsha 	plcom_common_putc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh, c);
   2155   1.1  rearnsha }
   2156   1.1  rearnsha #endif /* KGDB */
   2157   1.1  rearnsha 
   2158   1.1  rearnsha /* helper function to identify the plcom ports used by
   2159   1.1  rearnsha  console or KGDB (and not yet autoconf attached) */
   2160   1.1  rearnsha int
   2161   1.1  rearnsha plcom_is_console(bus_space_tag_t iot, int unit,
   2162   1.1  rearnsha     bus_space_handle_t *ioh)
   2163   1.1  rearnsha {
   2164   1.1  rearnsha 	bus_space_handle_t help;
   2165   1.1  rearnsha 
   2166   1.1  rearnsha 	if (!plcomconsattached &&
   2167   1.1  rearnsha 	    iot == plcomconstag && unit == plcomconsunit)
   2168   1.1  rearnsha 		help = plcomconsioh;
   2169   1.1  rearnsha #ifdef KGDB
   2170   1.1  rearnsha 	else if (!plcom_kgdb_attached &&
   2171   1.1  rearnsha 	    iot == plcom_kgdb_iot && unit == plcom_kgdb_unit)
   2172   1.1  rearnsha 		help = plcom_kgdb_ioh;
   2173   1.1  rearnsha #endif
   2174   1.1  rearnsha 	else
   2175   1.1  rearnsha 		return 0;
   2176   1.1  rearnsha 
   2177   1.1  rearnsha 	if (ioh)
   2178   1.1  rearnsha 		*ioh = help;
   2179   1.1  rearnsha 	return 1;
   2180   1.1  rearnsha }
   2181