Home | History | Annotate | Line # | Download | only in ixp12x0
ixp12x0_com.c revision 1.45
      1  1.45       tls /*	$NetBSD: ixp12x0_com.c,v 1.45 2014/08/10 16:44:33 tls Exp $ */
      2   1.1    ichiro /*
      3   1.1    ichiro  * Copyright (c) 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
      4   1.1    ichiro  * All rights reserved.
      5   1.1    ichiro  *
      6   1.1    ichiro  * This code is derived from software contributed to The NetBSD Foundation
      7   1.1    ichiro  * by Ichiro FUKUHARA and Naoto Shimazaki.
      8   1.1    ichiro  *
      9   1.1    ichiro  * This code is derived from software contributed to The NetBSD Foundation
     10   1.1    ichiro  * by IWAMOTO Toshihiro.
     11   1.1    ichiro  *
     12   1.1    ichiro  * This code is derived from software contributed to The NetBSD Foundation
     13   1.1    ichiro  * by Charles M. Hannum.
     14   1.1    ichiro  *
     15   1.1    ichiro  * Redistribution and use in source and binary forms, with or without
     16   1.1    ichiro  * modification, are permitted provided that the following conditions
     17   1.1    ichiro  * are met:
     18   1.1    ichiro  * 1. Redistributions of source code must retain the above copyright
     19   1.1    ichiro  *    notice, this list of conditions and the following disclaimer.
     20   1.1    ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     21   1.1    ichiro  *    notice, this list of conditions and the following disclaimer in the
     22   1.1    ichiro  *    documentation and/or other materials provided with the distribution.
     23   1.1    ichiro  *
     24   1.1    ichiro  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25   1.1    ichiro  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26   1.1    ichiro  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27   1.1    ichiro  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28   1.1    ichiro  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29   1.1    ichiro  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30   1.1    ichiro  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31   1.1    ichiro  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32   1.1    ichiro  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33   1.1    ichiro  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34   1.1    ichiro  * POSSIBILITY OF SUCH DAMAGE.
     35   1.1    ichiro  */
     36   1.1    ichiro 
     37   1.1    ichiro /*
     38   1.1    ichiro  * Copyright (c) 1991 The Regents of the University of California.
     39   1.1    ichiro  * All rights reserved.
     40   1.1    ichiro  *
     41   1.1    ichiro  * Redistribution and use in source and binary forms, with or without
     42   1.1    ichiro  * modification, are permitted provided that the following conditions
     43   1.1    ichiro  * are met:
     44   1.1    ichiro  * 1. Redistributions of source code must retain the above copyright
     45   1.1    ichiro  *    notice, this list of conditions and the following disclaimer.
     46   1.1    ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.1    ichiro  *    notice, this list of conditions and the following disclaimer in the
     48   1.1    ichiro  *    documentation and/or other materials provided with the distribution.
     49  1.19       agc  * 3. Neither the name of the University nor the names of its contributors
     50   1.1    ichiro  *    may be used to endorse or promote products derived from this software
     51   1.1    ichiro  *    without specific prior written permission.
     52   1.1    ichiro  *
     53   1.1    ichiro  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54   1.1    ichiro  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55   1.1    ichiro  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56   1.1    ichiro  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57   1.1    ichiro  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58   1.1    ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59   1.1    ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60   1.1    ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61   1.1    ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62   1.1    ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63   1.1    ichiro  * SUCH DAMAGE.
     64   1.1    ichiro  *
     65   1.1    ichiro  *      @(#)com.c       7.5 (Berkeley) 5/16/91
     66   1.1    ichiro  */
     67   1.1    ichiro 
     68  1.16       igy #include <sys/cdefs.h>
     69  1.45       tls __KERNEL_RCSID(0, "$NetBSD: ixp12x0_com.c,v 1.45 2014/08/10 16:44:33 tls Exp $");
     70   1.1    ichiro 
     71   1.1    ichiro #include "opt_ddb.h"
     72   1.1    ichiro #include "opt_kgdb.h"
     73   1.1    ichiro 
     74   1.1    ichiro #include "rnd.h"
     75  1.40       tls #ifdef RND_COM
     76   1.1    ichiro #include <sys/rnd.h>
     77   1.1    ichiro #endif
     78   1.1    ichiro 
     79   1.1    ichiro #include <sys/param.h>
     80   1.1    ichiro #include <sys/systm.h>
     81   1.1    ichiro #include <sys/types.h>
     82   1.1    ichiro #include <sys/conf.h>
     83   1.1    ichiro #include <sys/file.h>
     84   1.1    ichiro #include <sys/device.h>
     85   1.1    ichiro #include <sys/kernel.h>
     86   1.1    ichiro #include <sys/malloc.h>
     87   1.1    ichiro #include <sys/tty.h>
     88   1.1    ichiro #include <sys/uio.h>
     89   1.1    ichiro #include <sys/vnode.h>
     90  1.26      elad #include <sys/kauth.h>
     91  1.37  uebayasi #include <sys/lwp.h>
     92   1.1    ichiro 
     93   1.2    ichiro #include <machine/intr.h>
     94  1.39    dyoung #include <sys/bus.h>
     95   1.1    ichiro 
     96   1.1    ichiro #include <arm/ixp12x0/ixp12x0_comreg.h>
     97   1.1    ichiro #include <arm/ixp12x0/ixp12x0_comvar.h>
     98   1.1    ichiro #include <arm/ixp12x0/ixp12x0reg.h>
     99   1.1    ichiro #include <arm/ixp12x0/ixp12x0var.h>
    100   1.1    ichiro 
    101   1.1    ichiro #include <arm/ixp12x0/ixpsipvar.h>
    102   1.1    ichiro 
    103   1.2    ichiro #include <dev/cons.h>
    104   1.1    ichiro #include "ixpcom.h"
    105   1.1    ichiro 
    106   1.1    ichiro static int	ixpcomparam(struct tty *, struct termios *);
    107   1.1    ichiro static void	ixpcomstart(struct tty *);
    108   1.1    ichiro static int	ixpcomhwiflow(struct tty *, int);
    109   1.1    ichiro 
    110   1.1    ichiro static u_int	cflag2cr(tcflag_t);
    111   1.2    ichiro static void	ixpcom_iflush(struct ixpcom_softc *);
    112   1.2    ichiro static void	ixpcom_set_cr(struct ixpcom_softc *);
    113   1.1    ichiro 
    114   1.1    ichiro int             ixpcomcngetc(dev_t);
    115   1.1    ichiro void            ixpcomcnputc(dev_t, int);
    116   1.1    ichiro void            ixpcomcnpollc(dev_t, int);
    117   1.1    ichiro 
    118   1.1    ichiro static void	ixpcomsoft(void* arg);
    119   1.1    ichiro inline static void	ixpcom_txsoft(struct ixpcom_softc *, struct tty *);
    120   1.1    ichiro inline static void	ixpcom_rxsoft(struct ixpcom_softc *, struct tty *);
    121   1.1    ichiro 
    122   1.1    ichiro void            ixpcomcnprobe(struct consdev *);
    123   1.1    ichiro void            ixpcomcninit(struct consdev *);
    124   1.1    ichiro 
    125  1.42     skrll uint32_t	ixpcom_cr = 0;		/* tell cr to *_intr.c */
    126  1.42     skrll uint32_t	ixpcom_imask = 0;	/* intrrupt mask from *_intr.c */
    127   1.1    ichiro 
    128  1.10    ichiro 
    129  1.10    ichiro static struct ixpcom_cons_softc {
    130  1.10    ichiro 	bus_space_tag_t		sc_iot;
    131  1.10    ichiro 	bus_space_handle_t	sc_ioh;
    132  1.12       igy 	bus_addr_t		sc_baseaddr;
    133  1.10    ichiro 	int			sc_ospeed;
    134  1.10    ichiro 	tcflag_t		sc_cflag;
    135  1.12       igy 	int			sc_attached;
    136  1.10    ichiro } ixpcomcn_sc;
    137  1.10    ichiro 
    138   1.2    ichiro static struct cnm_state ixpcom_cnm_state;
    139   1.1    ichiro 
    140   1.8    ichiro struct ixpcom_softc* ixpcom_sc = NULL;
    141   1.8    ichiro 
    142   1.1    ichiro extern struct cfdriver ixpcom_cd;
    143   1.1    ichiro 
    144   1.4   gehenna dev_type_open(ixpcomopen);
    145   1.4   gehenna dev_type_close(ixpcomclose);
    146   1.4   gehenna dev_type_read(ixpcomread);
    147   1.4   gehenna dev_type_write(ixpcomwrite);
    148  1.11       igy dev_type_ioctl(ixpcomioctl);
    149   1.4   gehenna dev_type_stop(ixpcomstop);
    150   1.4   gehenna dev_type_tty(ixpcomtty);
    151   1.4   gehenna dev_type_poll(ixpcompoll);
    152   1.4   gehenna 
    153   1.4   gehenna const struct cdevsw ixpcom_cdevsw = {
    154  1.43  dholland 	.d_open = ixpcomopen,
    155  1.43  dholland 	.d_close = ixpcomclose,
    156  1.43  dholland 	.d_read = ixpcomread,
    157  1.43  dholland 	.d_write = ixpcomwrite,
    158  1.43  dholland 	.d_ioctl = ixpcomioctl,
    159  1.43  dholland 	.d_stop = ixpcomstop,
    160  1.43  dholland 	.d_tty = ixpcomtty,
    161  1.43  dholland 	.d_poll = ixpcompoll,
    162  1.43  dholland 	.d_mmap = nommap,
    163  1.43  dholland 	.d_kqfilter = ttykqfilter,
    164  1.44  dholland 	.d_discard = nodiscard,
    165  1.43  dholland 	.d_flag = D_TTY
    166   1.4   gehenna };
    167   1.4   gehenna 
    168   1.1    ichiro struct consdev ixpcomcons = {
    169   1.1    ichiro 	NULL, NULL, ixpcomcngetc, ixpcomcnputc, ixpcomcnpollc, NULL,
    170  1.15       igy 	NULL, NULL, NODEV, CN_NORMAL
    171   1.1    ichiro };
    172   1.1    ichiro 
    173   1.1    ichiro #ifndef DEFAULT_COMSPEED
    174   1.1    ichiro #define DEFAULT_COMSPEED 38400
    175   1.1    ichiro #endif
    176   1.1    ichiro 
    177   1.1    ichiro #define COMUNIT_MASK    0x7ffff
    178   1.1    ichiro #define COMDIALOUT_MASK 0x80000
    179   1.1    ichiro 
    180   1.1    ichiro #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    181   1.1    ichiro #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    182   1.1    ichiro 
    183   1.1    ichiro #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    184  1.41       chs 			 device_is_active((sc)->sc_dev))
    185   1.1    ichiro 
    186   1.1    ichiro #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    187   1.1    ichiro 
    188   1.1    ichiro #define	COM_LOCK(sc);
    189   1.1    ichiro #define	COM_UNLOCK(sc);
    190   1.1    ichiro 
    191   1.2    ichiro #define CFLAGS2CR_MASK	(CR_PE | CR_OES | CR_SBS | CR_DSS | CR_BRD)
    192   1.1    ichiro 
    193   1.1    ichiro void
    194  1.34    cegger ixpcom_attach_subr(struct ixpcom_softc *sc)
    195   1.1    ichiro {
    196   1.1    ichiro 	struct tty *tp;
    197   1.8    ichiro 
    198   1.8    ichiro 	ixpcom_sc = sc;
    199   1.1    ichiro 
    200  1.10    ichiro 	/* force to use ixpcom0 for console */
    201  1.12       igy 	if (sc->sc_iot == ixpcomcn_sc.sc_iot
    202  1.12       igy 	    && sc->sc_baseaddr == ixpcomcn_sc.sc_baseaddr) {
    203  1.12       igy 		ixpcomcn_sc.sc_attached = 1;
    204  1.10    ichiro 		sc->sc_speed = IXPCOMSPEED2BRD(ixpcomcn_sc.sc_ospeed);
    205   1.1    ichiro 
    206   1.1    ichiro 		/* Make sure the console is always "hardwired". */
    207  1.10    ichiro 		/* XXX IXPCOM_SR should be checked  */
    208   1.1    ichiro 		delay(10000);	/* wait for output to finish */
    209   1.1    ichiro 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    210   1.1    ichiro 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    211   1.1    ichiro 	}
    212   1.1    ichiro 
    213  1.38     rmind 	tp = tty_alloc();
    214   1.1    ichiro 	tp->t_oproc = ixpcomstart;
    215   1.1    ichiro 	tp->t_param = ixpcomparam;
    216   1.1    ichiro 	tp->t_hwiflow = ixpcomhwiflow;
    217   1.1    ichiro 
    218   1.1    ichiro 	sc->sc_tty = tp;
    219   1.1    ichiro 	sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    220   1.1    ichiro 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    221   1.1    ichiro 	sc->sc_rbavail = IXPCOM_RING_SIZE;
    222   1.1    ichiro 	if (sc->sc_rbuf == NULL) {
    223   1.1    ichiro 		printf("%s: unable to allocate ring buffer\n",
    224  1.41       chs 		    device_xname(sc->sc_dev));
    225   1.1    ichiro 		return;
    226   1.1    ichiro 	}
    227   1.1    ichiro 	sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1);
    228   1.1    ichiro 	sc->sc_tbc = 0;
    229   1.1    ichiro 
    230   1.1    ichiro 	sc->sc_rie = sc->sc_xie = 0;
    231   1.1    ichiro 	ixpcom_cr = IXPCOMSPEED2BRD(DEFAULT_COMSPEED)
    232   1.1    ichiro 		| CR_UE | sc->sc_rie | sc->sc_xie | DSS_8BIT;
    233   1.1    ichiro 
    234   1.1    ichiro 	tty_attach(tp);
    235   1.1    ichiro 
    236   1.1    ichiro 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    237   1.1    ichiro 		int maj;
    238   1.1    ichiro 
    239   1.1    ichiro 		/* locate the major number */
    240   1.4   gehenna 		maj = cdevsw_lookup_major(&ixpcom_cdevsw);
    241   1.1    ichiro 
    242  1.41       chs 		cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
    243   1.1    ichiro 
    244  1.41       chs 		aprint_normal("%s: console\n", device_xname(sc->sc_dev));
    245   1.1    ichiro 	}
    246   1.1    ichiro 
    247  1.32      matt 	sc->sc_si = softint_establish(SOFTINT_SERIAL, ixpcomsoft, sc);
    248   1.1    ichiro 
    249  1.40       tls #ifdef RND_COM
    250  1.41       chs 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    251  1.45       tls 			  RND_TYPE_TTY, RND_FLAG_COLLECT_TIME|
    252  1.45       tls 					RND_FLAG_ESTIMATE_TIME);
    253   1.1    ichiro #endif
    254   1.1    ichiro 
    255   1.1    ichiro 	/* if there are no enable/disable functions, assume the device
    256   1.1    ichiro 	   is always enabled */
    257   1.1    ichiro 	if (!sc->enable)
    258   1.1    ichiro 		sc->enabled = 1;
    259   1.1    ichiro 
    260   1.1    ichiro 	/* XXX configure register */
    261   1.1    ichiro 	/* xxx_config(sc) */
    262   1.1    ichiro 
    263   1.1    ichiro 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    264   1.1    ichiro }
    265   1.1    ichiro 
    266   1.2    ichiro static int
    267  1.34    cegger ixpcomparam(struct tty *tp, struct termios *t)
    268   1.2    ichiro {
    269   1.2    ichiro 	struct ixpcom_softc *sc
    270  1.34    cegger 		= device_lookup_private(&ixpcom_cd, COMUNIT(tp->t_dev));
    271  1.42     skrll 	uint32_t cr;
    272   1.2    ichiro 	int s;
    273   1.2    ichiro 
    274   1.2    ichiro 	if (COM_ISALIVE(sc) == 0)
    275   1.2    ichiro 		return (EIO);
    276   1.2    ichiro 
    277   1.2    ichiro 	cr = IXPCOMSPEED2BRD(t->c_ospeed);
    278   1.1    ichiro 
    279   1.2    ichiro 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    280   1.2    ichiro 		return (EINVAL);
    281   1.1    ichiro 
    282   1.2    ichiro 	/*
    283   1.2    ichiro 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    284   1.2    ichiro 	 * is always active.
    285   1.2    ichiro 	 */
    286   1.2    ichiro 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    287   1.2    ichiro 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    288   1.2    ichiro 		SET(t->c_cflag, CLOCAL);
    289   1.2    ichiro 		CLR(t->c_cflag, HUPCL);
    290   1.2    ichiro 	}
    291   1.1    ichiro 
    292   1.2    ichiro 	/*
    293   1.2    ichiro 	 * If there were no changes, don't do anything.  This avoids dropping
    294   1.2    ichiro 	 * input and improves performance when all we did was frob things like
    295   1.2    ichiro 	 * VMIN and VTIME.
    296   1.2    ichiro 	 */
    297   1.2    ichiro 	if (tp->t_ospeed == t->c_ospeed &&
    298   1.2    ichiro 	    tp->t_cflag == t->c_cflag)
    299   1.2    ichiro 		return (0);
    300   1.1    ichiro 
    301   1.2    ichiro 	cr |= cflag2cr(t->c_cflag);
    302   1.1    ichiro 
    303   1.1    ichiro 	s = splserial();
    304   1.2    ichiro 	COM_LOCK(sc);
    305   1.2    ichiro 
    306   1.2    ichiro 	ixpcom_cr = (ixpcom_cr & ~CFLAGS2CR_MASK) | cr;
    307   1.2    ichiro 
    308   1.2    ichiro 	/*
    309   1.2    ichiro 	 * ixpcom don't have any hardware flow control.
    310   1.2    ichiro 	 * we skip it.
    311   1.2    ichiro 	 */
    312   1.2    ichiro 
    313   1.2    ichiro 	/* And copy to tty. */
    314   1.2    ichiro 	tp->t_ispeed = 0;
    315   1.2    ichiro 	tp->t_ospeed = t->c_ospeed;
    316   1.2    ichiro 	tp->t_cflag = t->c_cflag;
    317   1.2    ichiro 
    318   1.2    ichiro 	if (!sc->sc_heldchange) {
    319   1.2    ichiro 		if (sc->sc_tx_busy) {
    320   1.2    ichiro 			sc->sc_heldtbc = sc->sc_tbc;
    321   1.2    ichiro 			sc->sc_tbc = 0;
    322   1.2    ichiro 			sc->sc_heldchange = 1;
    323   1.2    ichiro 		} else
    324   1.2    ichiro 			ixpcom_set_cr(sc);
    325   1.1    ichiro 	}
    326   1.1    ichiro 
    327   1.2    ichiro 	COM_UNLOCK(sc);
    328   1.1    ichiro 	splx(s);
    329   1.1    ichiro 
    330   1.2    ichiro 	/*
    331   1.2    ichiro 	 * Update the tty layer's idea of the carrier bit.
    332   1.2    ichiro 	 * We tell tty the carrier is always on.
    333   1.2    ichiro 	 */
    334   1.2    ichiro 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    335   1.2    ichiro 
    336   1.2    ichiro #ifdef COM_DEBUG
    337   1.2    ichiro 	if (com_debug)
    338   1.2    ichiro 		comstatus(sc, "comparam ");
    339   1.2    ichiro #endif
    340   1.2    ichiro 
    341   1.2    ichiro 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    342   1.2    ichiro 		if (sc->sc_tx_stopped) {
    343   1.2    ichiro 			sc->sc_tx_stopped = 0;
    344   1.2    ichiro 			ixpcomstart(tp);
    345   1.2    ichiro 		}
    346   1.2    ichiro 	}
    347   1.2    ichiro 
    348   1.1    ichiro 	return (0);
    349   1.1    ichiro }
    350   1.1    ichiro 
    351   1.2    ichiro static int
    352  1.36       dsl ixpcomhwiflow(struct tty *tp, int block)
    353   1.1    ichiro {
    354   1.1    ichiro 	return (0);
    355   1.1    ichiro }
    356   1.1    ichiro 
    357   1.2    ichiro static void
    358   1.6   thorpej ixpcom_filltx(struct ixpcom_softc *sc)
    359   1.2    ichiro {
    360   1.2    ichiro 	bus_space_tag_t iot = sc->sc_iot;
    361   1.2    ichiro 	bus_space_handle_t ioh = sc->sc_ioh;
    362   1.2    ichiro 	int n;
    363   1.2    ichiro 
    364   1.2    ichiro 	n = 0;
    365   1.2    ichiro         while (bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR) {
    366   1.2    ichiro 		if (n >= sc->sc_tbc)
    367   1.2    ichiro 			break;
    368   1.2    ichiro 		bus_space_write_4(iot, ioh, IXPCOM_DR,
    369   1.2    ichiro 				  0xff & *(sc->sc_tba + n));
    370   1.2    ichiro 		n++;
    371   1.2    ichiro         }
    372   1.2    ichiro         sc->sc_tbc -= n;
    373   1.2    ichiro         sc->sc_tba += n;
    374   1.2    ichiro }
    375   1.2    ichiro 
    376   1.2    ichiro static void
    377  1.34    cegger ixpcomstart(struct tty *tp)
    378   1.1    ichiro {
    379   1.2    ichiro 	struct ixpcom_softc *sc
    380  1.34    cegger 		= device_lookup_private(&ixpcom_cd, COMUNIT(tp->t_dev));
    381   1.2    ichiro 	int s;
    382   1.2    ichiro 
    383   1.2    ichiro 	if (COM_ISALIVE(sc) == 0)
    384   1.2    ichiro 		return;
    385   1.2    ichiro 
    386   1.2    ichiro 	s = spltty();
    387   1.2    ichiro 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    388   1.2    ichiro 		goto out;
    389   1.2    ichiro 	if (sc->sc_tx_stopped)
    390   1.2    ichiro 		goto out;
    391   1.2    ichiro 
    392  1.31        ad 	if (!ttypull(tp))
    393  1.31        ad 		goto out;
    394   1.2    ichiro 
    395   1.2    ichiro 	/* Grab the first contiguous region of buffer space. */
    396   1.2    ichiro 	{
    397   1.2    ichiro 		u_char *tba;
    398   1.2    ichiro 		int tbc;
    399   1.2    ichiro 
    400   1.2    ichiro 		tba = tp->t_outq.c_cf;
    401   1.2    ichiro 		tbc = ndqb(&tp->t_outq, 0);
    402   1.2    ichiro 
    403   1.2    ichiro 		(void)splserial();
    404   1.2    ichiro 		COM_LOCK(sc);
    405   1.2    ichiro 
    406   1.2    ichiro 		sc->sc_tba = tba;
    407   1.2    ichiro 		sc->sc_tbc = tbc;
    408   1.2    ichiro 	}
    409   1.2    ichiro 
    410   1.2    ichiro 	SET(tp->t_state, TS_BUSY);
    411   1.2    ichiro 	sc->sc_tx_busy = 1;
    412   1.2    ichiro 
    413   1.2    ichiro 	/* Enable transmit completion interrupts if necessary. */
    414   1.2    ichiro 	if (!ISSET(sc->sc_xie, CR_XIE)) {
    415   1.2    ichiro 		SET(sc->sc_xie, CR_XIE);
    416   1.2    ichiro 		ixpcom_set_cr(sc);
    417   1.2    ichiro 	}
    418   1.2    ichiro 
    419   1.2    ichiro 	/* Output the first chunk of the contiguous buffer. */
    420   1.2    ichiro 	ixpcom_filltx(sc);
    421   1.2    ichiro 
    422   1.2    ichiro 	COM_UNLOCK(sc);
    423   1.2    ichiro out:
    424   1.2    ichiro 	splx(s);
    425   1.1    ichiro 	return;
    426   1.1    ichiro }
    427   1.1    ichiro 
    428   1.2    ichiro static void
    429   1.6   thorpej ixpcom_break(struct ixpcom_softc *sc, int onoff)
    430   1.2    ichiro {
    431   1.2    ichiro 	if (onoff)
    432   1.2    ichiro 		SET(ixpcom_cr, CR_BRK);
    433   1.2    ichiro 	else
    434   1.2    ichiro 		CLR(ixpcom_cr, CR_BRK);
    435   1.2    ichiro 	if (!sc->sc_heldchange) {
    436   1.2    ichiro 		if (sc->sc_tx_busy) {
    437   1.2    ichiro 			sc->sc_heldtbc = sc->sc_tbc;
    438   1.2    ichiro 			sc->sc_tbc = 0;
    439   1.2    ichiro 			sc->sc_heldchange = 1;
    440   1.2    ichiro 		} else
    441   1.2    ichiro 			ixpcom_set_cr(sc);
    442   1.2    ichiro 	}
    443   1.2    ichiro }
    444   1.2    ichiro 
    445   1.2    ichiro static void
    446   1.6   thorpej ixpcom_shutdown(struct ixpcom_softc *sc)
    447   1.2    ichiro {
    448   1.2    ichiro 	int s;
    449   1.2    ichiro 
    450   1.2    ichiro 	s = splserial();
    451   1.2    ichiro 	COM_LOCK(sc);
    452   1.2    ichiro 
    453   1.2    ichiro 	/* Clear any break condition set with TIOCSBRK. */
    454   1.2    ichiro 	ixpcom_break(sc, 0);
    455   1.2    ichiro 
    456   1.2    ichiro 	/* Turn off interrupts. */
    457   1.2    ichiro 	sc->sc_rie = sc->sc_xie = 0;
    458   1.2    ichiro 	ixpcom_set_cr(sc);
    459   1.2    ichiro 
    460   1.2    ichiro 	if (sc->disable) {
    461   1.2    ichiro #ifdef DIAGNOSTIC
    462   1.2    ichiro 		if (!sc->enabled)
    463   1.2    ichiro 			panic("ixpcom_shutdown: not enabled?");
    464   1.2    ichiro #endif
    465   1.2    ichiro 		(*sc->disable)(sc);
    466   1.2    ichiro 		sc->enabled = 0;
    467   1.2    ichiro 	}
    468   1.2    ichiro 	COM_UNLOCK(sc);
    469   1.2    ichiro 	splx(s);
    470   1.2    ichiro }
    471   1.2    ichiro 
    472   1.1    ichiro int
    473  1.34    cegger ixpcomopen(dev_t dev, int flag, int mode, struct lwp *l)
    474   1.1    ichiro {
    475   1.2    ichiro 	struct ixpcom_softc *sc;
    476   1.2    ichiro 	struct tty *tp;
    477   1.2    ichiro 	int s, s2;
    478   1.2    ichiro 	int error;
    479   1.2    ichiro 
    480  1.34    cegger 	sc = device_lookup_private(&ixpcom_cd, COMUNIT(dev));
    481   1.2    ichiro 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    482   1.2    ichiro 		sc->sc_rbuf == NULL)
    483   1.2    ichiro 		return (ENXIO);
    484   1.2    ichiro 
    485  1.41       chs 	if (!device_is_active(sc->sc_dev))
    486   1.2    ichiro 		return (ENXIO);
    487   1.2    ichiro 
    488   1.2    ichiro #ifdef KGDB
    489   1.2    ichiro 	/*
    490   1.2    ichiro 	 * If this is the kgdb port, no other use is permitted.
    491   1.2    ichiro 	 */
    492   1.2    ichiro 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    493   1.2    ichiro 		return (EBUSY);
    494   1.2    ichiro #endif
    495   1.2    ichiro 
    496   1.2    ichiro 	tp = sc->sc_tty;
    497   1.2    ichiro 
    498  1.28      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    499   1.2    ichiro 		return (EBUSY);
    500   1.2    ichiro 
    501   1.2    ichiro 	s = spltty();
    502   1.2    ichiro 
    503   1.2    ichiro 	/*
    504   1.2    ichiro 	 * Do the following iff this is a first open.
    505   1.2    ichiro 	 */
    506   1.2    ichiro 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    507   1.2    ichiro 		struct termios t;
    508   1.2    ichiro 
    509   1.2    ichiro 		tp->t_dev = dev;
    510   1.2    ichiro 
    511   1.2    ichiro 		s2 = splserial();
    512   1.2    ichiro 		COM_LOCK(sc);
    513   1.2    ichiro 
    514   1.2    ichiro 		if (sc->enable) {
    515   1.2    ichiro 			if ((*sc->enable)(sc)) {
    516   1.2    ichiro 				COM_UNLOCK(sc);
    517   1.2    ichiro 				splx(s2);
    518   1.2    ichiro 				splx(s);
    519   1.2    ichiro 				printf("%s: device enable failed\n",
    520  1.41       chs 				       device_xname(sc->sc_dev));
    521   1.2    ichiro 				return (EIO);
    522   1.2    ichiro 			}
    523   1.2    ichiro 			sc->enabled = 1;
    524   1.2    ichiro #if 0
    525   1.2    ichiro /* XXXXXXXXXXXXXXX */
    526   1.2    ichiro 			com_config(sc);
    527   1.2    ichiro #endif
    528   1.2    ichiro 		}
    529   1.2    ichiro 
    530   1.2    ichiro 		/* Turn on interrupts. */
    531   1.2    ichiro 		SET(sc->sc_rie, CR_RIE);
    532   1.2    ichiro 		ixpcom_set_cr(sc);
    533   1.2    ichiro 
    534   1.2    ichiro #if 0
    535   1.2    ichiro 		/* Fetch the current modem control status, needed later. */
    536   1.2    ichiro 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
    537   1.2    ichiro 
    538   1.2    ichiro 		/* Clear PPS capture state on first open. */
    539   1.2    ichiro 		sc->sc_ppsmask = 0;
    540   1.2    ichiro 		sc->ppsparam.mode = 0;
    541   1.2    ichiro #endif
    542   1.2    ichiro 
    543   1.2    ichiro 		COM_UNLOCK(sc);
    544   1.2    ichiro 		splx(s2);
    545   1.2    ichiro 
    546   1.2    ichiro 		/*
    547   1.2    ichiro 		 * Initialize the termios status to the defaults.  Add in the
    548   1.2    ichiro 		 * sticky bits from TIOCSFLAGS.
    549   1.2    ichiro 		 */
    550   1.2    ichiro 		t.c_ispeed = 0;
    551   1.2    ichiro 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    552  1.10    ichiro 			t.c_ospeed = ixpcomcn_sc.sc_ospeed;
    553  1.10    ichiro 			t.c_cflag = ixpcomcn_sc.sc_cflag;
    554   1.2    ichiro 		} else {
    555   1.2    ichiro 			t.c_ospeed = TTYDEF_SPEED;
    556   1.2    ichiro 			t.c_cflag = TTYDEF_CFLAG;
    557   1.2    ichiro 		}
    558   1.2    ichiro 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    559   1.2    ichiro 			SET(t.c_cflag, CLOCAL);
    560   1.2    ichiro 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    561   1.2    ichiro 			SET(t.c_cflag, CRTSCTS);
    562   1.2    ichiro 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    563   1.2    ichiro 			SET(t.c_cflag, MDMBUF);
    564   1.2    ichiro 		/* Make sure ixpcomparam() will do something. */
    565   1.2    ichiro 		tp->t_ospeed = 0;
    566   1.2    ichiro 		(void) ixpcomparam(tp, &t);
    567   1.2    ichiro 		tp->t_iflag = TTYDEF_IFLAG;
    568   1.2    ichiro 		tp->t_oflag = TTYDEF_OFLAG;
    569   1.2    ichiro 		tp->t_lflag = TTYDEF_LFLAG;
    570   1.2    ichiro 		ttychars(tp);
    571   1.2    ichiro 		ttsetwater(tp);
    572   1.2    ichiro 
    573   1.2    ichiro 		s2 = splserial();
    574   1.2    ichiro 		COM_LOCK(sc);
    575   1.2    ichiro 
    576   1.2    ichiro 		/* Clear the input ring, and unblock. */
    577   1.2    ichiro 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    578   1.2    ichiro 		sc->sc_rbavail = IXPCOM_RING_SIZE;
    579   1.2    ichiro 		ixpcom_iflush(sc);
    580   1.2    ichiro 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    581   1.2    ichiro 
    582   1.2    ichiro #ifdef COM_DEBUG
    583   1.2    ichiro 		if (ixpcom_debug)
    584   1.2    ichiro 			comstatus(sc, "ixpcomopen  ");
    585   1.2    ichiro #endif
    586   1.2    ichiro 
    587   1.2    ichiro 		COM_UNLOCK(sc);
    588   1.2    ichiro 		splx(s2);
    589   1.2    ichiro 	}
    590   1.2    ichiro 
    591   1.2    ichiro 	splx(s);
    592   1.2    ichiro 
    593   1.2    ichiro 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    594   1.2    ichiro 	if (error)
    595   1.2    ichiro 		goto bad;
    596   1.2    ichiro 
    597   1.2    ichiro 	error = (*tp->t_linesw->l_open)(dev, tp);
    598   1.2    ichiro 	if (error)
    599   1.2    ichiro 		goto bad;
    600   1.2    ichiro 
    601   1.2    ichiro 	return (0);
    602   1.2    ichiro 
    603   1.2    ichiro bad:
    604   1.2    ichiro 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    605   1.2    ichiro 		/*
    606   1.2    ichiro 		 * We failed to open the device, and nobody else had it opened.
    607   1.2    ichiro 		 * Clean up the state as appropriate.
    608   1.2    ichiro 		 */
    609   1.2    ichiro 		ixpcom_shutdown(sc);
    610   1.2    ichiro 	}
    611   1.2    ichiro 
    612   1.2    ichiro 	return (error);
    613   1.2    ichiro }
    614   1.2    ichiro 
    615   1.2    ichiro int
    616  1.34    cegger ixpcomclose(dev_t dev, int flag, int mode, struct lwp *l)
    617   1.2    ichiro {
    618  1.34    cegger 	struct ixpcom_softc *sc = device_lookup_private(&ixpcom_cd, COMUNIT(dev));
    619   1.2    ichiro 	struct tty *tp = sc->sc_tty;
    620   1.2    ichiro 
    621   1.2    ichiro 	/* XXX This is for cons.c. */
    622   1.2    ichiro 	if (!ISSET(tp->t_state, TS_ISOPEN))
    623   1.2    ichiro 		return (0);
    624   1.2    ichiro 
    625   1.2    ichiro 	(*tp->t_linesw->l_close)(tp, flag);
    626   1.2    ichiro 	ttyclose(tp);
    627   1.2    ichiro 
    628   1.2    ichiro 	if (COM_ISALIVE(sc) == 0)
    629   1.2    ichiro 		return (0);
    630   1.2    ichiro 
    631   1.2    ichiro 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    632   1.2    ichiro 		/*
    633   1.2    ichiro 		 * Although we got a last close, the device may still be in
    634   1.2    ichiro 		 * use; e.g. if this was the dialout node, and there are still
    635   1.2    ichiro 		 * processes waiting for carrier on the non-dialout node.
    636   1.2    ichiro 		 */
    637   1.2    ichiro 		ixpcom_shutdown(sc);
    638   1.2    ichiro 	}
    639   1.2    ichiro 
    640   1.1    ichiro 	return (0);
    641   1.1    ichiro }
    642   1.2    ichiro 
    643   1.2    ichiro int
    644  1.34    cegger ixpcomread(dev_t dev, struct uio *uio, int flag)
    645   1.2    ichiro {
    646  1.34    cegger 	struct ixpcom_softc *sc = device_lookup_private(&ixpcom_cd, COMUNIT(dev));
    647   1.2    ichiro 	struct tty *tp = sc->sc_tty;
    648   1.2    ichiro 
    649   1.2    ichiro 	if (COM_ISALIVE(sc) == 0)
    650   1.2    ichiro 		return (EIO);
    651   1.2    ichiro 
    652   1.2    ichiro 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    653   1.2    ichiro }
    654   1.2    ichiro 
    655   1.2    ichiro int
    656  1.34    cegger ixpcomwrite(dev_t dev, struct uio *uio, int flag)
    657   1.2    ichiro {
    658  1.34    cegger 	struct ixpcom_softc *sc = device_lookup_private(&ixpcom_cd, COMUNIT(dev));
    659   1.2    ichiro 	struct tty *tp = sc->sc_tty;
    660   1.2    ichiro 
    661   1.2    ichiro 	if (COM_ISALIVE(sc) == 0)
    662   1.2    ichiro 		return (EIO);
    663   1.2    ichiro 
    664   1.2    ichiro 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    665   1.2    ichiro }
    666   1.2    ichiro 
    667   1.2    ichiro int
    668  1.34    cegger ixpcompoll(dev_t dev, int events, struct lwp *l)
    669   1.2    ichiro {
    670  1.34    cegger 	struct ixpcom_softc *sc = device_lookup_private(&ixpcom_cd, COMUNIT(dev));
    671   1.2    ichiro 	struct tty *tp = sc->sc_tty;
    672   1.2    ichiro 
    673   1.2    ichiro 	if (COM_ISALIVE(sc) == 0)
    674   1.2    ichiro 		return (EIO);
    675   1.2    ichiro 
    676  1.22  christos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    677   1.2    ichiro }
    678   1.2    ichiro 
    679   1.2    ichiro struct tty *
    680  1.34    cegger ixpcomtty(dev_t dev)
    681   1.2    ichiro {
    682  1.34    cegger 	struct ixpcom_softc *sc = device_lookup_private(&ixpcom_cd, COMUNIT(dev));
    683   1.2    ichiro 	struct tty *tp = sc->sc_tty;
    684   1.2    ichiro 
    685   1.2    ichiro 	return (tp);
    686  1.11       igy }
    687  1.11       igy 
    688  1.11       igy int
    689  1.30  christos ixpcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    690  1.11       igy {
    691  1.34    cegger 	struct ixpcom_softc *sc = device_lookup_private(&ixpcom_cd, COMUNIT(dev));
    692  1.11       igy 	struct tty *tp = sc->sc_tty;
    693  1.11       igy 	int error;
    694  1.11       igy 	int s;
    695  1.11       igy 
    696  1.11       igy 	if (COM_ISALIVE(sc) == 0)
    697  1.11       igy 		return (EIO);
    698  1.11       igy 
    699  1.22  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    700  1.11       igy 	if (error != EPASSTHROUGH)
    701  1.11       igy 		return (error);
    702  1.11       igy 
    703  1.22  christos 	error = ttioctl(tp, cmd, data, flag, l);
    704  1.11       igy 	if (error != EPASSTHROUGH)
    705  1.11       igy 		return (error);
    706  1.11       igy 
    707  1.11       igy 	error = 0;
    708  1.11       igy 
    709  1.11       igy 	s = splserial();
    710  1.11       igy 	COM_LOCK(sc);
    711  1.11       igy 
    712  1.11       igy 	switch (cmd) {
    713  1.11       igy 	case TIOCSBRK:
    714  1.11       igy 		ixpcom_break(sc, 1);
    715  1.11       igy 		break;
    716  1.11       igy 
    717  1.11       igy 	case TIOCCBRK:
    718  1.11       igy 		ixpcom_break(sc, 0);
    719  1.11       igy 		break;
    720  1.11       igy 
    721  1.11       igy 	case TIOCGFLAGS:
    722  1.11       igy 		*(int *)data = sc->sc_swflags;
    723  1.11       igy 		break;
    724  1.11       igy 
    725  1.11       igy 	case TIOCSFLAGS:
    726  1.29      elad 		error = kauth_authorize_device_tty(l->l_cred,
    727  1.29      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    728  1.11       igy 		if (error)
    729  1.11       igy 			break;
    730  1.11       igy 		sc->sc_swflags = *(int *)data;
    731  1.11       igy 		break;
    732  1.11       igy 
    733  1.11       igy 	default:
    734  1.11       igy 		error = EPASSTHROUGH;
    735  1.11       igy 		break;
    736  1.11       igy 	}
    737  1.11       igy 
    738  1.11       igy 	COM_UNLOCK(sc);
    739  1.11       igy 	splx(s);
    740  1.11       igy 
    741  1.11       igy 	return (error);
    742   1.2    ichiro }
    743   1.2    ichiro 
    744   1.2    ichiro /*
    745   1.2    ichiro  * Stop output on a line.
    746   1.2    ichiro  */
    747   1.2    ichiro void
    748  1.34    cegger ixpcomstop(struct tty *tp, int flag)
    749   1.2    ichiro {
    750   1.2    ichiro 	struct ixpcom_softc *sc
    751  1.34    cegger 		= device_lookup_private(&ixpcom_cd, COMUNIT(tp->t_dev));
    752   1.2    ichiro 	int s;
    753   1.2    ichiro 
    754   1.2    ichiro 	s = splserial();
    755   1.2    ichiro 	COM_LOCK(sc);
    756   1.2    ichiro 	if (ISSET(tp->t_state, TS_BUSY)) {
    757   1.2    ichiro 		/* Stop transmitting at the next chunk. */
    758   1.2    ichiro 		sc->sc_tbc = 0;
    759   1.2    ichiro 		sc->sc_heldtbc = 0;
    760   1.2    ichiro 		if (!ISSET(tp->t_state, TS_TTSTOP))
    761   1.2    ichiro 			SET(tp->t_state, TS_FLUSH);
    762   1.2    ichiro 	}
    763   1.2    ichiro 	COM_UNLOCK(sc);
    764   1.2    ichiro 	splx(s);
    765   1.2    ichiro }
    766   1.2    ichiro 
    767   1.1    ichiro static u_int
    768  1.36       dsl cflag2cr(tcflag_t cflag)
    769   1.1    ichiro {
    770   1.1    ichiro 	u_int cr;
    771   1.1    ichiro 
    772   1.1    ichiro 	cr  = (cflag & PARENB) ? CR_PE : 0;
    773   1.1    ichiro 	cr |= (cflag & PARODD) ? CR_OES : 0;
    774   1.1    ichiro 	cr |= (cflag & CSTOPB) ? CR_SBS : 0;
    775   1.1    ichiro 	cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0;
    776   1.1    ichiro 
    777   1.1    ichiro 	return (cr);
    778   1.1    ichiro }
    779   1.1    ichiro 
    780   1.1    ichiro static void
    781  1.36       dsl ixpcom_iflush(struct ixpcom_softc *sc)
    782   1.2    ichiro {
    783   1.2    ichiro 	bus_space_tag_t iot = sc->sc_iot;
    784   1.2    ichiro 	bus_space_handle_t ioh = sc->sc_ioh;
    785   1.2    ichiro #ifdef DIAGNOSTIC
    786   1.2    ichiro 	int reg;
    787   1.2    ichiro #endif
    788   1.2    ichiro 	int timo;
    789   1.2    ichiro 
    790   1.2    ichiro #ifdef DIAGNOSTIC
    791   1.2    ichiro 	reg = 0xffff;
    792   1.2    ichiro #endif
    793   1.2    ichiro 	timo = 50000;
    794   1.2    ichiro 	/* flush any pending I/O */
    795   1.2    ichiro 	while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR)
    796   1.2    ichiro 	       && --timo)
    797   1.2    ichiro #ifdef DIAGNOSTIC
    798   1.2    ichiro 		reg =
    799   1.2    ichiro #else
    800   1.2    ichiro 			(void)
    801   1.2    ichiro #endif
    802   1.2    ichiro 			bus_space_read_4(iot, ioh, IXPCOM_DR);
    803   1.2    ichiro #ifdef DIAGNOSTIC
    804   1.2    ichiro 	if (!timo)
    805  1.41       chs 		printf("%s: com_iflush timeout %02x\n", device_xname(sc->sc_dev),
    806   1.2    ichiro 		       reg);
    807   1.2    ichiro #endif
    808   1.2    ichiro }
    809   1.2    ichiro 
    810   1.2    ichiro static void
    811   1.6   thorpej ixpcom_set_cr(struct ixpcom_softc *sc)
    812   1.1    ichiro {
    813   1.1    ichiro 	/* XXX */
    814   1.1    ichiro 	ixpcom_cr &= ~(CR_RIE | CR_XIE);
    815   1.1    ichiro 	ixpcom_cr |= sc->sc_rie | sc->sc_xie;
    816   1.2    ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR,
    817   1.2    ichiro 			  ixpcom_cr & ~ixpcom_imask);
    818   1.1    ichiro }
    819   1.1    ichiro 
    820   1.1    ichiro int
    821  1.36       dsl ixpcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh, int ospeed, tcflag_t cflag)
    822   1.1    ichiro {
    823  1.10    ichiro 	int cr;
    824   1.1    ichiro 
    825   1.1    ichiro 	cn_tab = &ixpcomcons;
    826   1.2    ichiro 	cn_init_magic(&ixpcom_cnm_state);
    827   1.2    ichiro 	/*
    828   1.2    ichiro 	 * XXX
    829   1.2    ichiro 	 *
    830   1.2    ichiro 	 * ixpcom cannot detect a break.  It can only detect framing
    831   1.2    ichiro 	 * errors.  And, a sequence of "framing error, framing error"
    832   1.2    ichiro 	 * meens a break.  So I made this hack:
    833   1.2    ichiro 	 *
    834   1.2    ichiro 	 * 1. Set default magic is a sequence of "BREAK, BREAK".
    835   1.2    ichiro 	 * 2. Tell cn_check_magic() a "framing error" as BREAK.
    836   1.2    ichiro 	 *
    837   1.2    ichiro 	 * see ixpcom_intr() too.
    838   1.2    ichiro 	 *
    839   1.2    ichiro 	 */
    840   1.2    ichiro 	/* default magic is a couple of BREAK. */
    841   1.2    ichiro 	cn_set_magic("\047\001\047\001");
    842   1.1    ichiro 
    843  1.10    ichiro 	ixpcomcn_sc.sc_iot = iot;
    844  1.13       igy 	ixpcomcn_sc.sc_ioh = ioh;
    845  1.13       igy 	ixpcomcn_sc.sc_baseaddr = iobase;
    846  1.10    ichiro 	ixpcomcn_sc.sc_ospeed = ospeed;
    847  1.10    ichiro 	ixpcomcn_sc.sc_cflag = cflag;
    848   1.1    ichiro 
    849  1.10    ichiro 	cr = cflag2cr(cflag);
    850  1.10    ichiro 	cr |= IXPCOMSPEED2BRD(ospeed);
    851  1.10    ichiro 	cr |= CR_UE;
    852  1.10    ichiro 	ixpcom_cr = cr;
    853   1.1    ichiro 
    854  1.10    ichiro 	/* enable the UART */
    855  1.10    ichiro 	bus_space_write_4(iot, iobase, IXPCOM_CR, cr);
    856   1.1    ichiro 
    857  1.10    ichiro 	return (0);
    858   1.1    ichiro }
    859   1.1    ichiro 
    860   1.1    ichiro void
    861  1.36       dsl ixpcomcnprobe(struct consdev *cp)
    862   1.1    ichiro {
    863   1.1    ichiro 	cp->cn_pri = CN_REMOTE;
    864   1.1    ichiro }
    865   1.1    ichiro 
    866   1.1    ichiro void
    867  1.36       dsl ixpcomcnpollc(dev_t dev, int on)
    868   1.1    ichiro {
    869   1.1    ichiro }
    870   1.1    ichiro 
    871   1.1    ichiro void
    872  1.36       dsl ixpcomcnputc(dev_t dev, int c)
    873   1.1    ichiro {
    874  1.10    ichiro 	int			s;
    875  1.10    ichiro 	bus_space_tag_t		iot = ixpcomcn_sc.sc_iot;
    876  1.10    ichiro 	bus_space_handle_t	ioh = ixpcomcn_sc.sc_ioh;
    877   1.1    ichiro 
    878   1.9    ichiro 	s = splserial();
    879   1.1    ichiro 
    880  1.10    ichiro 	while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR))
    881   1.1    ichiro 		;
    882   1.1    ichiro 
    883  1.10    ichiro 	bus_space_write_4(iot, ioh, IXPCOM_DR, c);
    884   1.2    ichiro 
    885   1.2    ichiro #ifdef DEBUG
    886   1.2    ichiro 	if (c == '\r') {
    887  1.10    ichiro 		while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXE))
    888   1.2    ichiro 			;
    889   1.2    ichiro 	}
    890   1.2    ichiro #endif
    891   1.2    ichiro 
    892   1.1    ichiro 	splx(s);
    893   1.1    ichiro }
    894   1.1    ichiro 
    895   1.1    ichiro int
    896  1.36       dsl ixpcomcngetc(dev_t dev)
    897   1.1    ichiro {
    898  1.10    ichiro 	int			c;
    899  1.10    ichiro 	int			s;
    900  1.10    ichiro 	bus_space_tag_t		iot = ixpcomcn_sc.sc_iot;
    901  1.10    ichiro 	bus_space_handle_t	ioh = ixpcomcn_sc.sc_ioh;
    902   1.1    ichiro 
    903   1.9    ichiro         s = splserial();
    904   1.1    ichiro 
    905  1.10    ichiro 	while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_RXR))
    906   1.1    ichiro 		;
    907   1.1    ichiro 
    908  1.10    ichiro 	c = bus_space_read_4(iot, ioh, IXPCOM_DR);
    909   1.1    ichiro 	c &= 0xff;
    910   1.1    ichiro 	splx(s);
    911   1.1    ichiro 
    912   1.1    ichiro 	return (c);
    913   1.1    ichiro }
    914   1.1    ichiro 
    915   1.1    ichiro inline static void
    916  1.36       dsl ixpcom_txsoft(struct ixpcom_softc *sc, struct tty *tp)
    917   1.1    ichiro {
    918   1.2    ichiro 	CLR(tp->t_state, TS_BUSY);
    919   1.2    ichiro 	if (ISSET(tp->t_state, TS_FLUSH))
    920   1.2    ichiro 		CLR(tp->t_state, TS_FLUSH);
    921   1.2    ichiro         else
    922   1.2    ichiro 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
    923   1.2    ichiro 	(*tp->t_linesw->l_start)(tp);
    924   1.1    ichiro }
    925   1.1    ichiro 
    926   1.1    ichiro inline static void
    927  1.36       dsl ixpcom_rxsoft(struct ixpcom_softc *sc, struct tty *tp)
    928   1.1    ichiro {
    929  1.35       dsl 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
    930   1.1    ichiro 	u_char *get, *end;
    931   1.1    ichiro 	u_int cc, scc;
    932   1.1    ichiro 	u_char lsr;
    933   1.1    ichiro 	int code;
    934   1.1    ichiro 	int s;
    935   1.1    ichiro 
    936   1.1    ichiro 	end = sc->sc_ebuf;
    937   1.1    ichiro 	get = sc->sc_rbget;
    938   1.1    ichiro 	scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail;
    939   1.1    ichiro #if 0
    940   1.1    ichiro 	if (cc == IXPCOM_RING_SIZE) {
    941   1.1    ichiro 		sc->sc_floods++;
    942   1.1    ichiro 		if (sc->sc_errors++ == 0)
    943   1.1    ichiro 			callout_reset(&sc->sc_diag_callout, 60 * hz,
    944   1.1    ichiro 			    comdiag, sc);
    945   1.1    ichiro 	}
    946   1.1    ichiro #endif
    947   1.1    ichiro 	while (cc) {
    948   1.1    ichiro 		code = get[0];
    949   1.1    ichiro 		lsr = get[1];
    950   1.1    ichiro 		if (ISSET(lsr, DR_ROR | DR_FRE | DR_PRE)) {
    951   1.1    ichiro #if 0
    952   1.1    ichiro 			if (ISSET(lsr, DR_ROR)) {
    953   1.1    ichiro 				sc->sc_overflows++;
    954   1.1    ichiro 				if (sc->sc_errors++ == 0)
    955   1.1    ichiro 					callout_reset(&sc->sc_diag_callout,
    956   1.1    ichiro 					    60 * hz, comdiag, sc);
    957   1.1    ichiro 			}
    958   1.1    ichiro #endif
    959   1.1    ichiro 			if (ISSET(lsr, DR_FRE))
    960   1.1    ichiro 				SET(code, TTY_FE);
    961   1.1    ichiro 			if (ISSET(lsr, DR_PRE))
    962   1.1    ichiro 				SET(code, TTY_PE);
    963   1.1    ichiro 		}
    964   1.1    ichiro 		if ((*rint)(code, tp) == -1) {
    965   1.1    ichiro 			/*
    966   1.1    ichiro 			 * The line discipline's buffer is out of space.
    967   1.1    ichiro 			 */
    968   1.1    ichiro 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
    969   1.1    ichiro 				/*
    970   1.1    ichiro 				 * We're either not using flow control, or the
    971   1.1    ichiro 				 * line discipline didn't tell us to block for
    972   1.1    ichiro 				 * some reason.  Either way, we have no way to
    973   1.1    ichiro 				 * know when there's more space available, so
    974   1.1    ichiro 				 * just drop the rest of the data.
    975   1.1    ichiro 				 */
    976   1.1    ichiro 				get += cc << 1;
    977   1.1    ichiro 				if (get >= end)
    978   1.1    ichiro 					get -= IXPCOM_RING_SIZE << 1;
    979   1.1    ichiro 				cc = 0;
    980   1.1    ichiro 			} else {
    981   1.1    ichiro 				/*
    982   1.1    ichiro 				 * Don't schedule any more receive processing
    983   1.1    ichiro 				 * until the line discipline tells us there's
    984   1.1    ichiro 				 * space available (through comhwiflow()).
    985   1.1    ichiro 				 * Leave the rest of the data in the input
    986   1.1    ichiro 				 * buffer.
    987   1.1    ichiro 				 */
    988   1.1    ichiro 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    989   1.1    ichiro 			}
    990   1.1    ichiro 			break;
    991   1.1    ichiro 		}
    992   1.1    ichiro 		get += 2;
    993   1.1    ichiro 		if (get >= end)
    994   1.1    ichiro 			get = sc->sc_rbuf;
    995   1.1    ichiro 		cc--;
    996   1.1    ichiro 	}
    997   1.1    ichiro 
    998   1.1    ichiro 	if (cc != scc) {
    999   1.1    ichiro 		sc->sc_rbget = get;
   1000   1.1    ichiro 		s = splserial();
   1001   1.1    ichiro 		COM_LOCK(sc);
   1002   1.1    ichiro 
   1003   1.1    ichiro 		cc = sc->sc_rbavail += scc - cc;
   1004   1.1    ichiro 		/* Buffers should be ok again, release possible block. */
   1005   1.1    ichiro 		if (cc >= 1) {
   1006   1.1    ichiro 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1007   1.1    ichiro 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1008   1.1    ichiro 				SET(sc->sc_rie, CR_RIE);
   1009   1.2    ichiro 				ixpcom_set_cr(sc);
   1010   1.1    ichiro 			}
   1011   1.1    ichiro 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1012   1.1    ichiro 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1013   1.1    ichiro #if 0
   1014   1.1    ichiro 				com_hwiflow(sc);
   1015   1.1    ichiro #endif
   1016   1.1    ichiro 			}
   1017   1.1    ichiro 		}
   1018   1.1    ichiro 		COM_UNLOCK(sc);
   1019   1.1    ichiro 		splx(s);
   1020   1.1    ichiro 	}
   1021   1.1    ichiro }
   1022   1.1    ichiro 
   1023   1.1    ichiro static void
   1024   1.1    ichiro ixpcomsoft(void* arg)
   1025   1.1    ichiro {
   1026   1.1    ichiro 	struct ixpcom_softc *sc = arg;
   1027   1.1    ichiro 
   1028   1.1    ichiro 	if (COM_ISALIVE(sc) == 0)
   1029   1.1    ichiro 		return;
   1030   1.1    ichiro 
   1031   1.1    ichiro 	if (sc->sc_rx_ready) {
   1032   1.1    ichiro 		sc->sc_rx_ready = 0;
   1033   1.1    ichiro 		ixpcom_rxsoft(sc, sc->sc_tty);
   1034   1.1    ichiro 	}
   1035   1.1    ichiro 	if (sc->sc_tx_done) {
   1036   1.1    ichiro 		sc->sc_tx_done = 0;
   1037   1.1    ichiro 		ixpcom_txsoft(sc, sc->sc_tty);
   1038   1.1    ichiro 	}
   1039   1.1    ichiro }
   1040   1.1    ichiro 
   1041   1.2    ichiro int
   1042   1.1    ichiro ixpcomintr(void* arg)
   1043   1.1    ichiro {
   1044   1.1    ichiro 	struct ixpcom_softc *sc = arg;
   1045   1.1    ichiro 	bus_space_tag_t iot = sc->sc_iot;
   1046   1.1    ichiro 	bus_space_handle_t ioh = sc->sc_ioh;
   1047   1.1    ichiro 	u_char *put, *end;
   1048   1.1    ichiro 	u_int cc;
   1049   1.1    ichiro 	u_int cr;
   1050   1.1    ichiro 	u_int sr;
   1051  1.42     skrll 	uint32_t c;
   1052   1.1    ichiro 
   1053   1.1    ichiro 	if (COM_ISALIVE(sc) == 0)
   1054   1.1    ichiro 		return (0);
   1055   1.1    ichiro 
   1056   1.1    ichiro 	COM_LOCK(sc);
   1057   1.1    ichiro 	cr = bus_space_read_4(iot, ioh, IXPCOM_CR) & CR_UE;
   1058   1.1    ichiro 
   1059   1.1    ichiro 	if (!cr) {
   1060   1.1    ichiro 		COM_UNLOCK(sc);
   1061   1.1    ichiro 		return (0);
   1062   1.1    ichiro 	}
   1063   1.1    ichiro 
   1064   1.1    ichiro 	sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
   1065   1.1    ichiro 	if (!ISSET(sr, SR_TXR | SR_RXR))
   1066   1.1    ichiro 		return (0);
   1067   1.1    ichiro 
   1068   1.1    ichiro 	/*
   1069   1.2    ichiro 	 * XXX
   1070   1.2    ichiro 	 *
   1071   1.2    ichiro 	 * ixpcom cannot detect a break.  It can only detect framing
   1072   1.2    ichiro 	 * errors.  And, a sequence of "framing error, framing error"
   1073   1.2    ichiro 	 * meens a break.  So I made this hack:
   1074   1.2    ichiro 	 *
   1075   1.2    ichiro 	 * 1. Set default magic is a sequence of "BREAK, BREAK".
   1076   1.2    ichiro 	 * 2. Tell cn_check_magic() a "framing error" as BREAK.
   1077   1.2    ichiro 	 *
   1078   1.2    ichiro 	 * see ixpcomcnattach() too.
   1079   1.2    ichiro 	 *
   1080   1.1    ichiro 	 */
   1081   1.2    ichiro 	if (ISSET(sr, SR_FRE)) {
   1082   1.2    ichiro 		cn_check_magic(sc->sc_tty->t_dev,
   1083   1.2    ichiro 			       CNC_BREAK, ixpcom_cnm_state);
   1084   1.1    ichiro 	}
   1085   1.1    ichiro 
   1086   1.1    ichiro 	end = sc->sc_ebuf;
   1087   1.1    ichiro 	put = sc->sc_rbput;
   1088   1.1    ichiro 	cc = sc->sc_rbavail;
   1089   1.1    ichiro 
   1090   1.1    ichiro 	if (ISSET(sr, SR_RXR)) {
   1091   1.1    ichiro 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1092   1.1    ichiro 			while (cc > 0) {
   1093   1.1    ichiro 				if (!ISSET(sr, SR_RXR))
   1094   1.1    ichiro 					break;
   1095   1.1    ichiro 				c = bus_space_read_4(iot, ioh, IXPCOM_DR);
   1096   1.2    ichiro 				if (ISSET(c, DR_FRE)) {
   1097   1.2    ichiro 					cn_check_magic(sc->sc_tty->t_dev,
   1098   1.2    ichiro 						       CNC_BREAK,
   1099   1.2    ichiro 						       ixpcom_cnm_state);
   1100   1.2    ichiro 				}
   1101   1.1    ichiro 				put[0] = c & 0xff;
   1102   1.1    ichiro 				put[1] = (c >> 8) & 0xff;
   1103   1.2    ichiro 				cn_check_magic(sc->sc_tty->t_dev,
   1104   1.2    ichiro 					       put[0], ixpcom_cnm_state);
   1105   1.1    ichiro 				put += 2;
   1106   1.1    ichiro 				if (put >= end)
   1107   1.1    ichiro 					put = sc->sc_rbuf;
   1108   1.1    ichiro 				cc--;
   1109   1.1    ichiro 
   1110   1.1    ichiro 				sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
   1111   1.1    ichiro 			}
   1112   1.1    ichiro 
   1113   1.1    ichiro 			/*
   1114   1.1    ichiro 			 * Current string of incoming characters ended because
   1115   1.1    ichiro 			 * no more data was available or we ran out of space.
   1116   1.1    ichiro 			 * Schedule a receive event if any data was received.
   1117   1.1    ichiro 			 * If we're out of space, turn off receive interrupts.
   1118   1.1    ichiro 			 */
   1119   1.1    ichiro 			sc->sc_rbput = put;
   1120   1.1    ichiro 			sc->sc_rbavail = cc;
   1121   1.1    ichiro 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1122   1.1    ichiro 				sc->sc_rx_ready = 1;
   1123   1.1    ichiro 
   1124   1.2    ichiro 			/*
   1125   1.2    ichiro 			 * See if we are in danger of overflowing a buffer. If
   1126   1.2    ichiro 			 * so, use hardware flow control to ease the pressure.
   1127   1.2    ichiro 			 */
   1128   1.2    ichiro 
   1129   1.2    ichiro 			/* but ixpcom cannot. X-( */
   1130   1.1    ichiro 
   1131   1.1    ichiro 			/*
   1132   1.1    ichiro 			 * If we're out of space, disable receive interrupts
   1133   1.1    ichiro 			 * until the queue has drained a bit.
   1134   1.1    ichiro 			 */
   1135   1.1    ichiro 			if (!cc) {
   1136   1.1    ichiro 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1137   1.1    ichiro 				CLR(sc->sc_rie, CR_RIE);
   1138   1.2    ichiro 				ixpcom_set_cr(sc);
   1139   1.1    ichiro 			}
   1140   1.1    ichiro 		} else {
   1141   1.1    ichiro #ifdef DIAGNOSTIC
   1142   1.5    provos 			panic("ixpcomintr: we shouldn't reach here");
   1143   1.1    ichiro #endif
   1144   1.1    ichiro 			CLR(sc->sc_rie, CR_RIE);
   1145   1.2    ichiro 			ixpcom_set_cr(sc);
   1146   1.1    ichiro 		}
   1147   1.1    ichiro 	}
   1148   1.1    ichiro 
   1149   1.1    ichiro 	/*
   1150   1.1    ichiro 	 * Done handling any receive interrupts. See if data can be
   1151   1.1    ichiro 	 * transmitted as well. Schedule tx done event if no data left
   1152   1.1    ichiro 	 * and tty was marked busy.
   1153   1.1    ichiro 	 */
   1154   1.1    ichiro 	sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
   1155   1.1    ichiro 	if (ISSET(sr, SR_TXR)) {
   1156   1.1    ichiro 		/*
   1157   1.1    ichiro 		 * If we've delayed a parameter change, do it now, and restart
   1158   1.1    ichiro 		 * output.
   1159   1.1    ichiro 		 */
   1160   1.1    ichiro 		if (sc->sc_heldchange) {
   1161   1.2    ichiro 			ixpcom_set_cr(sc);
   1162   1.1    ichiro 			sc->sc_heldchange = 0;
   1163   1.1    ichiro 			sc->sc_tbc = sc->sc_heldtbc;
   1164   1.1    ichiro 			sc->sc_heldtbc = 0;
   1165   1.1    ichiro 		}
   1166   1.1    ichiro 
   1167   1.1    ichiro 		/* Output the next chunk of the contiguous buffer, if any. */
   1168   1.1    ichiro 		if (sc->sc_tbc > 0) {
   1169   1.2    ichiro 			ixpcom_filltx(sc);
   1170   1.1    ichiro 		} else {
   1171   1.1    ichiro 			/* Disable transmit completion interrupts if necessary. */
   1172   1.2    ichiro 			if (ISSET(sc->sc_xie, CR_XIE)) {
   1173   1.2    ichiro 				CLR(sc->sc_xie, CR_XIE);
   1174   1.2    ichiro 				ixpcom_set_cr(sc);
   1175   1.1    ichiro 			}
   1176   1.1    ichiro 			if (sc->sc_tx_busy) {
   1177   1.1    ichiro 				sc->sc_tx_busy = 0;
   1178   1.1    ichiro 				sc->sc_tx_done = 1;
   1179   1.1    ichiro 			}
   1180   1.1    ichiro 		}
   1181   1.1    ichiro 	}
   1182   1.1    ichiro 	COM_UNLOCK(sc);
   1183   1.1    ichiro 
   1184   1.1    ichiro 	/* Wake up the poller. */
   1185  1.32      matt 	softint_schedule(sc->sc_si);
   1186   1.1    ichiro 
   1187  1.40       tls #ifdef RND_COM
   1188   1.1    ichiro 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1189   1.1    ichiro #endif
   1190   1.1    ichiro 	return (1);
   1191   1.1    ichiro }
   1192