Home | History | Annotate | Line # | Download | only in dev
scif.c revision 1.23
      1  1.23   atatat /*	$NetBSD: scif.c,v 1.23 2002/03/17 19:40:50 atatat Exp $ */
      2   1.1   itojun 
      3   1.1   itojun /*-
      4   1.1   itojun  * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu.  All rights reserved.
      5   1.1   itojun  *
      6   1.1   itojun  * Redistribution and use in source and binary forms, with or without
      7   1.1   itojun  * modification, are permitted provided that the following conditions
      8   1.1   itojun  * are met:
      9   1.1   itojun  * 1. Redistributions of source code must retain the above copyright
     10   1.1   itojun  *    notice, this list of conditions and the following disclaimer.
     11   1.1   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1   itojun  *    notice, this list of conditions and the following disclaimer in the
     13   1.1   itojun  *    documentation and/or other materials provided with the distribution.
     14   1.1   itojun  * 3. The name of the author may not be used to endorse or promote products
     15   1.1   itojun  *    derived from this software without specific prior written permission.
     16   1.1   itojun  *
     17   1.1   itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1   itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1   itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1   itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1   itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1   itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1   itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1   itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1   itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26   1.1   itojun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1   itojun  */
     28   1.1   itojun 
     29   1.2  msaitoh /*-
     30   1.2  msaitoh  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     31   1.2  msaitoh  * All rights reserved.
     32   1.2  msaitoh  *
     33   1.2  msaitoh  * This code is derived from software contributed to The NetBSD Foundation
     34   1.2  msaitoh  * by Charles M. Hannum.
     35   1.2  msaitoh  *
     36   1.2  msaitoh  * Redistribution and use in source and binary forms, with or without
     37   1.2  msaitoh  * modification, are permitted provided that the following conditions
     38   1.2  msaitoh  * are met:
     39   1.2  msaitoh  * 1. Redistributions of source code must retain the above copyright
     40   1.2  msaitoh  *    notice, this list of conditions and the following disclaimer.
     41   1.2  msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.2  msaitoh  *    notice, this list of conditions and the following disclaimer in the
     43   1.2  msaitoh  *    documentation and/or other materials provided with the distribution.
     44   1.2  msaitoh  * 3. All advertising materials mentioning features or use of this software
     45   1.2  msaitoh  *    must display the following acknowledgement:
     46   1.2  msaitoh  *        This product includes software developed by the NetBSD
     47   1.2  msaitoh  *        Foundation, Inc. and its contributors.
     48   1.2  msaitoh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     49   1.2  msaitoh  *    contributors may be used to endorse or promote products derived
     50   1.2  msaitoh  *    from this software without specific prior written permission.
     51   1.2  msaitoh  *
     52   1.2  msaitoh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     53   1.2  msaitoh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     54   1.2  msaitoh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     55   1.2  msaitoh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     56   1.2  msaitoh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     57   1.2  msaitoh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     58   1.2  msaitoh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     59   1.2  msaitoh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     60   1.2  msaitoh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     61   1.2  msaitoh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     62   1.2  msaitoh  * POSSIBILITY OF SUCH DAMAGE.
     63   1.2  msaitoh  */
     64   1.2  msaitoh 
     65   1.2  msaitoh /*
     66   1.2  msaitoh  * Copyright (c) 1991 The Regents of the University of California.
     67   1.2  msaitoh  * All rights reserved.
     68   1.2  msaitoh  *
     69   1.2  msaitoh  * Redistribution and use in source and binary forms, with or without
     70   1.2  msaitoh  * modification, are permitted provided that the following conditions
     71   1.2  msaitoh  * are met:
     72   1.2  msaitoh  * 1. Redistributions of source code must retain the above copyright
     73   1.2  msaitoh  *    notice, this list of conditions and the following disclaimer.
     74   1.2  msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     75   1.2  msaitoh  *    notice, this list of conditions and the following disclaimer in the
     76   1.2  msaitoh  *    documentation and/or other materials provided with the distribution.
     77   1.2  msaitoh  * 3. All advertising materials mentioning features or use of this software
     78   1.2  msaitoh  *    must display the following acknowledgement:
     79   1.2  msaitoh  *	This product includes software developed by the University of
     80   1.2  msaitoh  *	California, Berkeley and its contributors.
     81   1.2  msaitoh  * 4. Neither the name of the University nor the names of its contributors
     82   1.2  msaitoh  *    may be used to endorse or promote products derived from this software
     83   1.2  msaitoh  *    without specific prior written permission.
     84   1.2  msaitoh  *
     85   1.2  msaitoh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86   1.2  msaitoh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87   1.2  msaitoh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88   1.2  msaitoh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89   1.2  msaitoh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90   1.2  msaitoh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91   1.2  msaitoh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92   1.2  msaitoh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93   1.2  msaitoh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94   1.2  msaitoh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95   1.2  msaitoh  * SUCH DAMAGE.
     96   1.2  msaitoh  *
     97   1.2  msaitoh  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     98   1.2  msaitoh  */
     99   1.2  msaitoh 
    100   1.2  msaitoh /*
    101   1.2  msaitoh  * SH internal serial driver
    102   1.2  msaitoh  *
    103   1.2  msaitoh  * This code is derived from both z8530tty.c and com.c
    104   1.2  msaitoh  */
    105   1.2  msaitoh 
    106  1.17    lukem #include "opt_kgdb.h"
    107   1.1   itojun #include "opt_scif.h"
    108   1.1   itojun 
    109   1.1   itojun #include <sys/param.h>
    110   1.1   itojun #include <sys/systm.h>
    111   1.1   itojun #include <sys/tty.h>
    112   1.1   itojun #include <sys/proc.h>
    113   1.1   itojun #include <sys/conf.h>
    114   1.1   itojun #include <sys/file.h>
    115   1.1   itojun #include <sys/syslog.h>
    116   1.1   itojun #include <sys/kernel.h>
    117   1.1   itojun #include <sys/device.h>
    118   1.1   itojun #include <sys/malloc.h>
    119  1.22      uch #include <sys/kgdb.h>
    120   1.1   itojun 
    121   1.1   itojun #include <dev/cons.h>
    122   1.1   itojun 
    123  1.21      uch #include <sh3/clock.h>
    124   1.1   itojun #include <sh3/scifreg.h>
    125  1.22      uch #include <sh3/dev/scifvar.h>
    126   1.1   itojun 
    127   1.1   itojun #include <machine/shbvar.h>
    128   1.1   itojun 
    129  1.20      uch static void	scifstart(struct tty *);
    130  1.20      uch static int	scifparam(struct tty *, struct termios *);
    131  1.22      uch static int kgdb_attached;
    132   1.1   itojun 
    133  1.20      uch void scifcnprobe(struct consdev *);
    134  1.20      uch void scifcninit(struct consdev *);
    135  1.20      uch void scifcnputc(dev_t, int);
    136  1.20      uch int scifcngetc(dev_t);
    137  1.20      uch void scifcnpoolc(dev_t, int);
    138  1.20      uch void scif_intr_init(void);
    139  1.20      uch int scifintr(void *);
    140   1.1   itojun 
    141   1.1   itojun struct scif_softc {
    142   1.1   itojun 	struct device sc_dev;		/* boilerplate */
    143   1.1   itojun 	struct tty *sc_tty;
    144   1.1   itojun 	void *sc_ih;
    145   1.1   itojun 
    146   1.8  thorpej 	struct callout sc_diag_ch;
    147   1.8  thorpej 
    148   1.1   itojun #if 0
    149   1.1   itojun 	bus_space_tag_t sc_iot;		/* ISA i/o space identifier */
    150   1.1   itojun 	bus_space_handle_t   sc_ioh;	/* ISA io handle */
    151   1.1   itojun 
    152   1.1   itojun 	int sc_drq;
    153   1.1   itojun 
    154   1.1   itojun 	int sc_frequency;
    155   1.1   itojun #endif
    156   1.1   itojun 
    157   1.1   itojun 	u_int sc_overflows,
    158   1.1   itojun 	      sc_floods,
    159   1.1   itojun 	      sc_errors;		/* number of retries so far */
    160   1.1   itojun 	u_char sc_status[7];		/* copy of registers */
    161   1.1   itojun 
    162   1.1   itojun 	int sc_hwflags;
    163   1.1   itojun 	int sc_swflags;
    164   1.1   itojun 	u_int sc_fifolen;
    165   1.1   itojun 
    166   1.1   itojun 	u_int sc_r_hiwat,
    167   1.1   itojun 	      sc_r_lowat;
    168   1.1   itojun 	u_char *volatile sc_rbget,
    169   1.1   itojun 	       *volatile sc_rbput;
    170   1.1   itojun  	volatile u_int sc_rbavail;
    171   1.1   itojun 	u_char *sc_rbuf,
    172   1.1   itojun 	       *sc_ebuf;
    173   1.1   itojun 
    174   1.1   itojun  	u_char *sc_tba;			/* transmit buffer address */
    175   1.1   itojun  	u_int sc_tbc,			/* transmit byte count */
    176   1.1   itojun 	      sc_heldtbc;
    177   1.1   itojun 
    178   1.1   itojun 	volatile u_char sc_rx_flags,
    179   1.1   itojun #define	RX_TTY_BLOCKED		0x01
    180   1.1   itojun #define	RX_TTY_OVERFLOWED	0x02
    181   1.1   itojun #define	RX_IBUF_BLOCKED		0x04
    182   1.1   itojun #define	RX_IBUF_OVERFLOWED	0x08
    183   1.1   itojun #define	RX_ANY_BLOCK		0x0f
    184   1.3  msaitoh 			sc_tx_busy,	/* working on an output chunk */
    185   1.3  msaitoh 			sc_tx_done,	/* done with one output chunk */
    186   1.2  msaitoh 			sc_tx_stopped,	/* H/W level stop (lost CTS) */
    187   1.2  msaitoh 			sc_st_check,	/* got a status interrupt */
    188   1.1   itojun 			sc_rx_ready;
    189   1.1   itojun 
    190   1.1   itojun 	volatile u_char sc_heldchange;
    191   1.1   itojun };
    192   1.1   itojun 
    193   1.1   itojun /* controller driver configuration */
    194  1.20      uch static int scif_match(struct device *, struct cfdata *, void *);
    195  1.20      uch static void scif_attach(struct device *, struct device *, void *);
    196   1.1   itojun 
    197  1.20      uch void	scif_break(struct scif_softc *, int);
    198  1.20      uch void	scif_iflush(struct scif_softc *);
    199   1.1   itojun 
    200   1.1   itojun #define	integrate	static inline
    201  1.15  thorpej #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    202  1.20      uch void 	scifsoft(void *);
    203   1.1   itojun #else
    204   1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
    205  1.20      uch void 	scifsoft(void);
    206   1.1   itojun #else
    207  1.20      uch void 	scifsoft(void *);
    208   1.1   itojun #endif
    209   1.1   itojun #endif
    210  1.20      uch integrate void scif_rxsoft(struct scif_softc *, struct tty *);
    211  1.20      uch integrate void scif_txsoft(struct scif_softc *, struct tty *);
    212  1.20      uch integrate void scif_stsoft(struct scif_softc *, struct tty *);
    213  1.20      uch integrate void scif_schedrx(struct scif_softc *);
    214  1.20      uch void	scifdiag(void *);
    215   1.1   itojun 
    216   1.1   itojun 
    217   1.1   itojun #define	SCIFUNIT_MASK		0x7ffff
    218   1.1   itojun #define	SCIFDIALOUT_MASK	0x80000
    219   1.1   itojun 
    220   1.1   itojun #define	SCIFUNIT(x)	(minor(x) & SCIFUNIT_MASK)
    221   1.1   itojun #define	SCIFDIALOUT(x)	(minor(x) & SCIFDIALOUT_MASK)
    222   1.1   itojun 
    223   1.1   itojun /* Macros to clear/set/test flags. */
    224   1.1   itojun #define SET(t, f)	(t) |= (f)
    225   1.1   itojun #define CLR(t, f)	(t) &= ~(f)
    226   1.1   itojun #define ISSET(t, f)	((t) & (f))
    227   1.1   itojun 
    228   1.1   itojun /* Hardware flag masks */
    229   1.1   itojun #define	SCIF_HW_NOIEN	0x01
    230   1.1   itojun #define	SCIF_HW_FIFO	0x02
    231   1.1   itojun #define	SCIF_HW_FLOW	0x08
    232   1.1   itojun #define	SCIF_HW_DEV_OK	0x20
    233   1.1   itojun #define	SCIF_HW_CONSOLE	0x40
    234   1.1   itojun #define	SCIF_HW_KGDB	0x80
    235   1.1   itojun 
    236   1.1   itojun /* Buffer size for character buffer */
    237   1.1   itojun #define	SCIF_RING_SIZE	2048
    238   1.1   itojun 
    239   1.1   itojun /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    240   1.1   itojun u_int scif_rbuf_hiwat = (SCIF_RING_SIZE * 1) / 4;
    241   1.1   itojun u_int scif_rbuf_lowat = (SCIF_RING_SIZE * 3) / 4;
    242   1.1   itojun 
    243   1.1   itojun #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    244   1.1   itojun int scifconscflag = CONMODE;
    245   1.9  msaitoh int scifisconsole = 0;
    246   1.1   itojun 
    247   1.7  msaitoh #ifdef SCIFCN_SPEED
    248   1.7  msaitoh unsigned int scifcn_speed = SCIFCN_SPEED;
    249   1.7  msaitoh #else
    250   1.7  msaitoh unsigned int scifcn_speed = 9600;
    251   1.7  msaitoh #endif
    252   1.7  msaitoh 
    253   1.1   itojun #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    254   1.1   itojun 
    255  1.15  thorpej #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
    256   1.1   itojun #ifdef __NO_SOFT_SERIAL_INTERRUPT
    257   1.1   itojun volatile int	scif_softintr_scheduled;
    258   1.8  thorpej struct callout scif_soft_ch = CALLOUT_INITIALIZER;
    259   1.1   itojun #endif
    260   1.1   itojun #endif
    261   1.1   itojun 
    262   1.1   itojun u_int scif_rbuf_size = SCIF_RING_SIZE;
    263   1.1   itojun 
    264   1.1   itojun struct cfattach scif_ca = {
    265   1.1   itojun 	sizeof(struct scif_softc), scif_match, scif_attach
    266   1.1   itojun };
    267   1.1   itojun 
    268   1.1   itojun extern struct cfdriver scif_cd;
    269   1.1   itojun 
    270   1.1   itojun cdev_decl(scif);
    271   1.1   itojun 
    272  1.20      uch void InitializeScif (unsigned int);
    273   1.1   itojun 
    274   1.1   itojun /*
    275   1.1   itojun  * following functions are debugging prupose only
    276   1.1   itojun  */
    277   1.1   itojun #define CR      0x0D
    278   1.1   itojun #define USART_ON (unsigned int)~0x08
    279   1.1   itojun 
    280  1.20      uch void scif_putc(unsigned char);
    281  1.20      uch unsigned char scif_getc(void);
    282  1.20      uch int ScifErrCheck(void);
    283   1.1   itojun 
    284   1.1   itojun /*
    285   1.1   itojun  * InitializeScif
    286   1.1   itojun  * : unsigned int bps;
    287   1.1   itojun  * : SCIF(Serial Communication Interface)
    288   1.1   itojun  */
    289   1.1   itojun 
    290   1.1   itojun void
    291  1.20      uch InitializeScif(unsigned int bps)
    292   1.1   itojun {
    293   1.1   itojun 
    294   1.1   itojun 	/* Initialize SCR */
    295   1.3  msaitoh 	SHREG_SCSCR2 = 0x00;
    296   1.1   itojun 
    297   1.6  msaitoh #if 0
    298   1.6  msaitoh 	SHREG_SCFCR2 = SCFCR2_TFRST | SCFCR2_RFRST | SCFCR2_MCE;
    299   1.6  msaitoh #else
    300   1.1   itojun 	SHREG_SCFCR2 = SCFCR2_TFRST | SCFCR2_RFRST;
    301   1.6  msaitoh #endif
    302   1.7  msaitoh 	/* Serial Mode Register */
    303   1.3  msaitoh 	SHREG_SCSMR2 = 0x00;	/* 8bit,NonParity,Even,1Stop */
    304   1.1   itojun 
    305   1.7  msaitoh 	/* Bit Rate Register */
    306  1.21      uch 	SHREG_SCBRR2 = divrnd(sh_clock_get_pclock(), 32 * bps) - 1;
    307   1.1   itojun 
    308   1.7  msaitoh 	/*
    309   1.7  msaitoh 	 * wait 1mSec, because Send/Recv must begin 1 bit period after
    310   1.7  msaitoh 	 * BRR is set.
    311   1.7  msaitoh 	 */
    312  1.21      uch 	delay(1000);
    313   1.1   itojun 
    314   1.6  msaitoh #if 0
    315   1.6  msaitoh 	SHREG_SCFCR2 = FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1 | SCFCR2_MCE;
    316   1.6  msaitoh #else
    317   1.1   itojun 	SHREG_SCFCR2 = FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1;
    318   1.6  msaitoh #endif
    319   1.1   itojun 
    320  1.18      wiz 	/* Send permission, Receive permission ON */
    321   1.3  msaitoh 	SHREG_SCSCR2 = SCSCR2_TE | SCSCR2_RE;
    322   1.1   itojun 
    323   1.7  msaitoh 	/* Serial Status Register */
    324   1.3  msaitoh 	SHREG_SCSSR2 &= SCSSR2_TDFE;	/* Clear Status */
    325   1.1   itojun }
    326   1.1   itojun 
    327   1.1   itojun 
    328   1.1   itojun /*
    329  1.14  msaitoh  * scif_putc
    330   1.1   itojun  *  : unsigned char c;
    331   1.1   itojun  */
    332   1.1   itojun 
    333   1.1   itojun void
    334  1.20      uch scif_putc(unsigned char c)
    335   1.1   itojun {
    336   1.1   itojun 
    337  1.14  msaitoh 	if (c == '\n')
    338  1.14  msaitoh 		scif_putc('\r');
    339  1.14  msaitoh 
    340   1.1   itojun 	/* wait for ready */
    341   1.1   itojun 	while ((SHREG_SCFDR2 & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
    342   1.1   itojun 		;
    343   1.1   itojun 
    344   1.1   itojun 	/* write send data to send register */
    345   1.1   itojun 	SHREG_SCFTDR2 = c;
    346   1.1   itojun 
    347   1.1   itojun 	/* clear ready flag */
    348   1.1   itojun 	SHREG_SCSSR2 &= ~(SCSSR2_TDFE | SCSSR2_TEND);
    349   1.1   itojun }
    350   1.1   itojun 
    351   1.1   itojun /*
    352   1.1   itojun  * : ScifErrCheck
    353   1.1   itojun  *	0x80 = error
    354   1.1   itojun  *	0x08 = frame error
    355   1.1   itojun  *	0x04 = parity error
    356   1.1   itojun  */
    357   1.1   itojun int
    358   1.1   itojun ScifErrCheck(void)
    359   1.1   itojun {
    360   1.1   itojun 
    361   1.1   itojun 	return(SHREG_SCSSR2 & (SCSSR2_ER | SCSSR2_FER | SCSSR2_PER));
    362   1.1   itojun }
    363   1.1   itojun 
    364   1.1   itojun /*
    365  1.14  msaitoh  * scif_getc
    366   1.1   itojun  */
    367   1.1   itojun #if 0
    368   1.1   itojun /* Old code */
    369   1.1   itojun unsigned char
    370  1.14  msaitoh scif_getc(void)
    371   1.1   itojun {
    372   1.1   itojun 	unsigned char c, err_c;
    373   1.1   itojun 
    374   1.1   itojun 	while (((err_c = SHREG_SCSSR2)
    375   1.1   itojun 		& (SCSSR2_RDF | SCSSR2_ER | SCSSR2_FER | SCSSR2_PER | SCSSR2_DR)) == 0)
    376   1.1   itojun 		;
    377  1.10  msaitoh 	if ((err_c & (SCSSR2_ER | SCSSR2_FER | SCSSR2_PER)) != 0) {
    378  1.10  msaitoh 		SHREG_SCSSR2 &= ~SCSSR2_ER;
    379   1.1   itojun 		return(err_c |= 0x80);
    380  1.10  msaitoh 	}
    381   1.1   itojun 
    382   1.1   itojun 	c = SHREG_SCFRDR2;
    383   1.1   itojun 
    384  1.10  msaitoh 	SHREG_SCSSR2 &= ~(SCSSR2_ER | SCSSR2_RDF | SCSSR2_DR);
    385   1.1   itojun 
    386   1.1   itojun 	return(c);
    387   1.1   itojun }
    388   1.1   itojun #else
    389   1.1   itojun unsigned char
    390  1.14  msaitoh scif_getc(void)
    391   1.1   itojun {
    392   1.1   itojun 	unsigned char c, err_c;
    393   1.1   itojun 
    394  1.12  msaitoh 	while (1) {
    395  1.12  msaitoh 		/* wait for ready */
    396  1.12  msaitoh 		while ((SHREG_SCFDR2 & SCFDR2_RECVCNT) == 0)
    397  1.12  msaitoh 			;
    398   1.1   itojun 
    399  1.12  msaitoh 		c = SHREG_SCFRDR2;
    400  1.12  msaitoh 		err_c = SHREG_SCSSR2;
    401  1.12  msaitoh 		SHREG_SCSSR2 &= ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF
    402  1.12  msaitoh 		    | SCSSR2_DR);
    403  1.12  msaitoh 		if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER
    404  1.12  msaitoh 		    | SCSSR2_PER)) == 0) {
    405  1.12  msaitoh 			return(c);
    406  1.12  msaitoh 		}
    407  1.12  msaitoh 	}
    408   1.1   itojun 
    409   1.1   itojun }
    410   1.1   itojun #endif
    411   1.1   itojun 
    412   1.1   itojun #if 0
    413   1.1   itojun #define SCIF_MAX_UNITS 2
    414   1.1   itojun #else
    415   1.1   itojun #define SCIF_MAX_UNITS 1
    416   1.1   itojun #endif
    417   1.1   itojun 
    418   1.1   itojun 
    419   1.1   itojun static int
    420  1.20      uch scif_match(struct device *parent, struct cfdata *cfp, void *aux)
    421   1.1   itojun {
    422   1.1   itojun 	struct shb_attach_args *sa = aux;
    423   1.1   itojun 
    424   1.1   itojun 	if (strcmp(cfp->cf_driver->cd_name, "scif")
    425   1.1   itojun 	    || cfp->cf_unit >= SCIF_MAX_UNITS)
    426   1.1   itojun 		return 0;
    427   1.1   itojun 
    428   1.1   itojun 	sa->ia_iosize = 0x10;
    429   1.1   itojun 	return 1;
    430   1.1   itojun }
    431   1.1   itojun 
    432   1.1   itojun static void
    433  1.20      uch scif_attach(struct device *parent, struct device *self, void *aux)
    434   1.1   itojun {
    435   1.1   itojun 	struct scif_softc *sc = (struct scif_softc *)self;
    436   1.1   itojun 	struct tty *tp;
    437   1.1   itojun 	int irq;
    438   1.1   itojun 	struct shb_attach_args *ia = aux;
    439   1.1   itojun 
    440   1.1   itojun 	sc->sc_hwflags = 0;	/* XXX */
    441   1.1   itojun 	sc->sc_swflags = 0;	/* XXX */
    442   1.1   itojun 	sc->sc_fifolen = 16;
    443   1.1   itojun 
    444   1.1   itojun 	irq = ia->ia_irq;
    445   1.1   itojun 
    446  1.22      uch 	if (scifisconsole || kgdb_attached) {
    447   1.9  msaitoh 		/* InitializeScif(scifcn_speed); */
    448   1.9  msaitoh 		SET(sc->sc_hwflags, SCIF_HW_CONSOLE);
    449   1.9  msaitoh 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    450  1.22      uch 		if (kgdb_attached) {
    451  1.22      uch 			SET(sc->sc_hwflags, SCIF_HW_KGDB);
    452  1.22      uch 			printf("\n%s: kgdb\n", sc->sc_dev.dv_xname);
    453  1.22      uch 		} else {
    454  1.22      uch 			printf("\n%s: console\n", sc->sc_dev.dv_xname);
    455  1.22      uch 		}
    456   1.9  msaitoh 	} else {
    457   1.9  msaitoh 		InitializeScif(9600);
    458   1.9  msaitoh 		printf("\n");
    459   1.9  msaitoh 	}
    460   1.1   itojun 
    461   1.8  thorpej 	callout_init(&sc->sc_diag_ch);
    462   1.8  thorpej 
    463   1.1   itojun 	if (irq != IRQUNK) {
    464   1.1   itojun 		sc->sc_ih = shb_intr_establish(SCIF_IRQ,
    465   1.1   itojun 		    IST_EDGE, IPL_SERIAL, scifintr, sc);
    466   1.1   itojun 	}
    467   1.1   itojun 
    468   1.9  msaitoh 	SET(sc->sc_hwflags, SCIF_HW_DEV_OK);
    469   1.1   itojun 
    470   1.1   itojun 	tp = ttymalloc();
    471   1.1   itojun 	tp->t_oproc = scifstart;
    472   1.1   itojun 	tp->t_param = scifparam;
    473   1.1   itojun 	tp->t_hwiflow = NULL;
    474   1.1   itojun 
    475   1.1   itojun 	sc->sc_tty = tp;
    476   1.1   itojun 	sc->sc_rbuf = malloc(scif_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    477   1.1   itojun 	if (sc->sc_rbuf == NULL) {
    478   1.1   itojun 		printf("%s: unable to allocate ring buffer\n",
    479   1.1   itojun 		    sc->sc_dev.dv_xname);
    480   1.1   itojun 		return;
    481   1.1   itojun 	}
    482   1.1   itojun 	sc->sc_ebuf = sc->sc_rbuf + (scif_rbuf_size << 1);
    483   1.1   itojun 
    484   1.1   itojun 	tty_attach(tp);
    485   1.1   itojun }
    486   1.1   itojun 
    487   1.1   itojun /*
    488   1.1   itojun  * Start or restart transmission.
    489   1.1   itojun  */
    490   1.1   itojun static void
    491  1.20      uch scifstart(struct tty *tp)
    492   1.1   itojun {
    493   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(tp->t_dev)];
    494   1.1   itojun 	int s;
    495   1.1   itojun 
    496   1.1   itojun 	s = spltty();
    497   1.1   itojun 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    498   1.1   itojun 		goto out;
    499   1.1   itojun 	if (sc->sc_tx_stopped)
    500   1.1   itojun 		goto out;
    501   1.1   itojun 
    502   1.1   itojun 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    503   1.1   itojun 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    504   1.1   itojun 			CLR(tp->t_state, TS_ASLEEP);
    505   1.1   itojun 			wakeup(&tp->t_outq);
    506   1.1   itojun 		}
    507   1.1   itojun 		selwakeup(&tp->t_wsel);
    508   1.1   itojun 		if (tp->t_outq.c_cc == 0)
    509   1.1   itojun 			goto out;
    510   1.1   itojun 	}
    511   1.1   itojun 
    512   1.1   itojun 	/* Grab the first contiguous region of buffer space. */
    513   1.1   itojun 	{
    514   1.1   itojun 		u_char *tba;
    515   1.1   itojun 		int tbc;
    516   1.1   itojun 
    517   1.1   itojun 		tba = tp->t_outq.c_cf;
    518   1.1   itojun 		tbc = ndqb(&tp->t_outq, 0);
    519   1.1   itojun 
    520   1.1   itojun 		(void)splserial();
    521   1.1   itojun 
    522   1.1   itojun 		sc->sc_tba = tba;
    523   1.1   itojun 		sc->sc_tbc = tbc;
    524   1.1   itojun 	}
    525   1.1   itojun 
    526   1.1   itojun 	SET(tp->t_state, TS_BUSY);
    527   1.1   itojun 	sc->sc_tx_busy = 1;
    528   1.1   itojun 
    529   1.1   itojun 	/* Enable transmit completion interrupts if necessary. */
    530   1.1   itojun 	SHREG_SCSCR2 |= SCSCR2_TIE | SCSCR2_RIE;
    531   1.1   itojun 
    532   1.1   itojun 	/* Output the first chunk of the contiguous buffer. */
    533   1.1   itojun 	{
    534   1.1   itojun 		int n;
    535   1.1   itojun 		int max;
    536   1.1   itojun 		int i;
    537   1.1   itojun 
    538   1.1   itojun 		n = sc->sc_tbc;
    539   1.1   itojun 		max = sc->sc_fifolen - ((SHREG_SCFDR2 & SCFDR2_TXCNT) >> 8);
    540   1.1   itojun 		if (n > max)
    541   1.1   itojun 			n = max;
    542   1.1   itojun 
    543   1.1   itojun 		for (i = 0; i < n; i++) {
    544  1.14  msaitoh 			scif_putc(*(sc->sc_tba));
    545   1.1   itojun 			sc->sc_tba++;
    546   1.1   itojun 		}
    547   1.1   itojun 		sc->sc_tbc -= n;
    548   1.1   itojun 	}
    549   1.1   itojun out:
    550   1.1   itojun 	splx(s);
    551   1.1   itojun 	return;
    552   1.1   itojun }
    553   1.1   itojun 
    554   1.1   itojun /*
    555   1.1   itojun  * Set SCIF tty parameters from termios.
    556   1.1   itojun  * XXX - Should just copy the whole termios after
    557   1.1   itojun  * making sure all the changes could be done.
    558   1.1   itojun  */
    559   1.1   itojun static int
    560  1.20      uch scifparam(struct tty *tp, struct termios *t)
    561   1.1   itojun {
    562   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(tp->t_dev)];
    563   1.1   itojun 	int ospeed = t->c_ospeed;
    564   1.1   itojun 	int s;
    565   1.1   itojun 
    566   1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    567   1.1   itojun 		return (EIO);
    568   1.1   itojun 
    569   1.1   itojun 	/* Check requested parameters. */
    570   1.1   itojun 	if (ospeed < 0)
    571   1.1   itojun 		return (EINVAL);
    572   1.1   itojun 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    573   1.1   itojun 		return (EINVAL);
    574   1.1   itojun 
    575   1.1   itojun 	/*
    576   1.1   itojun 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    577   1.1   itojun 	 * is always active.
    578   1.1   itojun 	 */
    579   1.1   itojun 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    580   1.1   itojun 	    ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
    581   1.1   itojun 		SET(t->c_cflag, CLOCAL);
    582   1.1   itojun 		CLR(t->c_cflag, HUPCL);
    583   1.1   itojun 	}
    584   1.1   itojun 
    585   1.1   itojun 	/*
    586   1.1   itojun 	 * If there were no changes, don't do anything.  This avoids dropping
    587   1.1   itojun 	 * input and improves performance when all we did was frob things like
    588   1.1   itojun 	 * VMIN and VTIME.
    589   1.1   itojun 	 */
    590   1.1   itojun 	if (tp->t_ospeed == t->c_ospeed &&
    591   1.1   itojun 	    tp->t_cflag == t->c_cflag)
    592   1.1   itojun 		return (0);
    593   1.1   itojun 
    594   1.1   itojun #if 0
    595   1.1   itojun /* XXX (msaitoh) */
    596   1.1   itojun 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
    597   1.1   itojun #endif
    598   1.1   itojun 
    599   1.1   itojun 	s = splserial();
    600   1.1   itojun 
    601   1.1   itojun 	/*
    602   1.1   itojun 	 * Set the flow control pins depending on the current flow control
    603   1.1   itojun 	 * mode.
    604   1.1   itojun 	 */
    605   1.1   itojun 	if (ISSET(t->c_cflag, CRTSCTS)) {
    606   1.1   itojun 		SHREG_SCFCR2 |= SCFCR2_MCE;
    607   1.1   itojun 	} else {
    608   1.1   itojun 		SHREG_SCFCR2 &= ~SCFCR2_MCE;
    609   1.1   itojun 	}
    610   1.1   itojun 
    611  1.21      uch 	SHREG_SCBRR2 = divrnd(sh_clock_get_pclock(), 32 * ospeed) -1;
    612   1.1   itojun 
    613   1.1   itojun 	/*
    614   1.1   itojun 	 * Set the FIFO threshold based on the receive speed.
    615   1.1   itojun 	 *
    616   1.1   itojun 	 *  * If it's a low speed, it's probably a mouse or some other
    617   1.1   itojun 	 *    interactive device, so set the threshold low.
    618   1.1   itojun 	 *  * If it's a high speed, trim the trigger level down to prevent
    619   1.1   itojun 	 *    overflows.
    620   1.1   itojun 	 *  * Otherwise set it a bit higher.
    621   1.1   itojun 	 */
    622   1.1   itojun #if 0
    623   1.1   itojun /* XXX (msaitoh) */
    624   1.1   itojun 	if (ISSET(sc->sc_hwflags, SCIF_HW_HAYESP))
    625   1.1   itojun 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
    626   1.1   itojun 	else if (ISSET(sc->sc_hwflags, SCIF_HW_FIFO))
    627   1.1   itojun 		sc->sc_fifo = FIFO_ENABLE |
    628   1.1   itojun 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
    629   1.1   itojun 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
    630   1.1   itojun 	else
    631   1.1   itojun 		sc->sc_fifo = 0;
    632   1.1   itojun #endif
    633   1.1   itojun 
    634   1.1   itojun 	/* And copy to tty. */
    635   1.1   itojun 	tp->t_ispeed = 0;
    636   1.1   itojun 	tp->t_ospeed = t->c_ospeed;
    637   1.1   itojun 	tp->t_cflag = t->c_cflag;
    638   1.1   itojun 
    639   1.1   itojun 	if (!sc->sc_heldchange) {
    640   1.1   itojun 		if (sc->sc_tx_busy) {
    641   1.1   itojun 			sc->sc_heldtbc = sc->sc_tbc;
    642   1.1   itojun 			sc->sc_tbc = 0;
    643   1.1   itojun 			sc->sc_heldchange = 1;
    644   1.1   itojun 		}
    645   1.1   itojun #if 0
    646   1.1   itojun /* XXX (msaitoh) */
    647   1.1   itojun 		else
    648   1.1   itojun 			scif_loadchannelregs(sc);
    649   1.1   itojun #endif
    650   1.1   itojun 	}
    651   1.1   itojun 
    652   1.1   itojun 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    653   1.1   itojun 		/* Disable the high water mark. */
    654   1.1   itojun 		sc->sc_r_hiwat = 0;
    655   1.1   itojun 		sc->sc_r_lowat = 0;
    656   1.1   itojun 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
    657   1.1   itojun 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    658   1.1   itojun 			scif_schedrx(sc);
    659   1.1   itojun 		}
    660   1.1   itojun 	} else {
    661   1.1   itojun 		sc->sc_r_hiwat = scif_rbuf_hiwat;
    662   1.1   itojun 		sc->sc_r_lowat = scif_rbuf_lowat;
    663   1.1   itojun 	}
    664   1.1   itojun 
    665   1.1   itojun 	splx(s);
    666   1.1   itojun 
    667   1.1   itojun #ifdef SCIF_DEBUG
    668   1.1   itojun 	if (scif_debug)
    669   1.1   itojun 		scifstatus(sc, "scifparam ");
    670   1.1   itojun #endif
    671   1.1   itojun 
    672   1.1   itojun 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    673   1.1   itojun 		if (sc->sc_tx_stopped) {
    674   1.1   itojun 			sc->sc_tx_stopped = 0;
    675   1.1   itojun 			scifstart(tp);
    676   1.1   itojun 		}
    677   1.1   itojun 	}
    678   1.1   itojun 
    679   1.1   itojun 	return (0);
    680   1.1   itojun }
    681   1.1   itojun 
    682   1.1   itojun void
    683  1.20      uch scif_iflush(struct scif_softc *sc)
    684   1.1   itojun {
    685   1.1   itojun 	int i;
    686   1.1   itojun 	unsigned char c;
    687   1.1   itojun 
    688   1.1   itojun 	i = SHREG_SCFDR2 & SCFDR2_RECVCNT;
    689   1.1   itojun 
    690   1.1   itojun 	while (i > 0) {
    691   1.1   itojun 		c = SHREG_SCFRDR2;
    692   1.1   itojun 		SHREG_SCSSR2 &= ~(SCSSR2_RDF | SCSSR2_DR);
    693   1.1   itojun 		i--;
    694   1.1   itojun 	}
    695   1.1   itojun }
    696   1.1   itojun 
    697   1.1   itojun int
    698  1.20      uch scifopen(dev_t dev, int flag, int mode, struct proc *p)
    699   1.1   itojun {
    700   1.1   itojun 	int unit = SCIFUNIT(dev);
    701   1.1   itojun 	struct scif_softc *sc;
    702   1.1   itojun 	struct tty *tp;
    703   1.1   itojun 	int s, s2;
    704   1.1   itojun 	int error;
    705   1.1   itojun 
    706   1.1   itojun 	if (unit >= scif_cd.cd_ndevs)
    707   1.1   itojun 		return (ENXIO);
    708   1.1   itojun 	sc = scif_cd.cd_devs[unit];
    709   1.1   itojun 	if (sc == 0 || !ISSET(sc->sc_hwflags, SCIF_HW_DEV_OK) ||
    710   1.1   itojun 	    sc->sc_rbuf == NULL)
    711   1.1   itojun 		return (ENXIO);
    712   1.1   itojun 
    713   1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    714   1.1   itojun 		return (ENXIO);
    715   1.1   itojun 
    716   1.1   itojun #ifdef KGDB
    717   1.1   itojun 	/*
    718   1.1   itojun 	 * If this is the kgdb port, no other use is permitted.
    719   1.1   itojun 	 */
    720   1.1   itojun 	if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB))
    721   1.1   itojun 		return (EBUSY);
    722  1.22      uch #endif /* KGDB */
    723   1.1   itojun 
    724   1.1   itojun 	tp = sc->sc_tty;
    725   1.1   itojun 
    726   1.1   itojun 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    727   1.1   itojun 	    ISSET(tp->t_state, TS_XCLUDE) &&
    728   1.1   itojun 	    p->p_ucred->cr_uid != 0)
    729   1.1   itojun 		return (EBUSY);
    730   1.1   itojun 
    731   1.1   itojun 	s = spltty();
    732   1.1   itojun 
    733   1.1   itojun 	/*
    734   1.1   itojun 	 * Do the following iff this is a first open.
    735   1.1   itojun 	 */
    736   1.1   itojun 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    737   1.1   itojun 		struct termios t;
    738   1.1   itojun 
    739   1.1   itojun 		tp->t_dev = dev;
    740   1.1   itojun 
    741   1.1   itojun 		s2 = splserial();
    742   1.1   itojun 
    743   1.1   itojun 		/* Turn on interrupts. */
    744   1.1   itojun 		SHREG_SCSCR2 |= SCSCR2_TIE | SCSCR2_RIE;
    745   1.1   itojun 
    746   1.1   itojun 		splx(s2);
    747   1.1   itojun 
    748   1.1   itojun 		/*
    749   1.1   itojun 		 * Initialize the termios status to the defaults.  Add in the
    750   1.1   itojun 		 * sticky bits from TIOCSFLAGS.
    751   1.1   itojun 		 */
    752   1.1   itojun 		t.c_ispeed = 0;
    753   1.1   itojun 		if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
    754   1.7  msaitoh 			t.c_ospeed = scifcn_speed;	/* XXX (msaitoh) */
    755   1.1   itojun 			t.c_cflag = scifconscflag;
    756   1.1   itojun 		} else {
    757   1.1   itojun 			t.c_ospeed = TTYDEF_SPEED;
    758   1.1   itojun 			t.c_cflag = TTYDEF_CFLAG;
    759   1.1   itojun 		}
    760   1.1   itojun 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    761   1.1   itojun 			SET(t.c_cflag, CLOCAL);
    762   1.1   itojun 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    763   1.1   itojun 			SET(t.c_cflag, CRTSCTS);
    764   1.1   itojun 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    765   1.1   itojun 			SET(t.c_cflag, MDMBUF);
    766   1.1   itojun 		/* Make sure scifparam() will do something. */
    767   1.1   itojun 		tp->t_ospeed = 0;
    768   1.1   itojun 		(void) scifparam(tp, &t);
    769   1.1   itojun 		tp->t_iflag = TTYDEF_IFLAG;
    770   1.1   itojun 		tp->t_oflag = TTYDEF_OFLAG;
    771   1.1   itojun 		tp->t_lflag = TTYDEF_LFLAG;
    772   1.1   itojun 		ttychars(tp);
    773   1.1   itojun 		ttsetwater(tp);
    774   1.1   itojun 
    775   1.1   itojun 		s2 = splserial();
    776   1.1   itojun 
    777   1.1   itojun 		/* Clear the input ring, and unblock. */
    778   1.1   itojun 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    779   1.1   itojun 		sc->sc_rbavail = scif_rbuf_size;
    780   1.1   itojun 		scif_iflush(sc);
    781   1.1   itojun 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    782   1.1   itojun #if 0
    783   1.1   itojun /* XXX (msaitoh) */
    784   1.1   itojun 		scif_hwiflow(sc);
    785   1.1   itojun #endif
    786   1.1   itojun 
    787   1.1   itojun #ifdef SCIF_DEBUG
    788   1.1   itojun 		if (scif_debug)
    789   1.1   itojun 			scifstatus(sc, "scifopen  ");
    790   1.1   itojun #endif
    791   1.1   itojun 
    792   1.1   itojun 		splx(s2);
    793   1.1   itojun 	}
    794   1.1   itojun 
    795   1.1   itojun 	splx(s);
    796   1.1   itojun 
    797   1.1   itojun 	error = ttyopen(tp, SCIFDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    798   1.1   itojun 	if (error)
    799   1.1   itojun 		goto bad;
    800   1.1   itojun 
    801  1.13      eeh 	error = (*tp->t_linesw->l_open)(dev, tp);
    802   1.1   itojun 	if (error)
    803   1.1   itojun 		goto bad;
    804   1.1   itojun 
    805   1.1   itojun 	return (0);
    806   1.1   itojun 
    807   1.1   itojun bad:
    808   1.1   itojun 
    809   1.1   itojun 	return (error);
    810   1.1   itojun }
    811   1.1   itojun 
    812   1.1   itojun int
    813  1.20      uch scifclose(dev_t dev, int flag, int mode, struct proc *p)
    814   1.1   itojun {
    815   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    816   1.1   itojun 	struct tty *tp = sc->sc_tty;
    817   1.1   itojun 
    818   1.1   itojun 	/* XXX This is for cons.c. */
    819   1.1   itojun 	if (!ISSET(tp->t_state, TS_ISOPEN))
    820   1.1   itojun 		return (0);
    821   1.1   itojun 
    822  1.13      eeh 	(*tp->t_linesw->l_close)(tp, flag);
    823   1.1   itojun 	ttyclose(tp);
    824   1.1   itojun 
    825   1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    826   1.1   itojun 		return (0);
    827   1.1   itojun 
    828   1.1   itojun 	return (0);
    829   1.1   itojun }
    830   1.1   itojun 
    831   1.1   itojun int
    832  1.20      uch scifread(dev_t dev, struct uio *uio, int flag)
    833   1.1   itojun {
    834   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    835   1.1   itojun 	struct tty *tp = sc->sc_tty;
    836   1.1   itojun 
    837  1.13      eeh 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    838   1.1   itojun }
    839   1.1   itojun 
    840   1.1   itojun int
    841  1.20      uch scifwrite(dev_t dev, struct uio *uio, int flag)
    842   1.1   itojun {
    843   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    844   1.1   itojun 	struct tty *tp = sc->sc_tty;
    845   1.1   itojun 
    846  1.13      eeh 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    847  1.16      scw }
    848  1.16      scw 
    849  1.16      scw int
    850  1.20      uch scifpoll(dev_t dev, int events, struct proc *p)
    851  1.16      scw {
    852  1.16      scw 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    853  1.16      scw 	struct tty *tp = sc->sc_tty;
    854  1.16      scw 
    855  1.16      scw 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    856   1.1   itojun }
    857   1.1   itojun 
    858   1.1   itojun struct tty *
    859  1.20      uch sciftty(dev_t dev)
    860   1.1   itojun {
    861   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    862   1.1   itojun 	struct tty *tp = sc->sc_tty;
    863   1.1   itojun 
    864   1.1   itojun 	return (tp);
    865   1.1   itojun }
    866   1.1   itojun 
    867   1.1   itojun int
    868  1.20      uch scifioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    869   1.1   itojun {
    870   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(dev)];
    871   1.1   itojun 	struct tty *tp = sc->sc_tty;
    872   1.1   itojun 	int error;
    873   1.1   itojun 	int s;
    874   1.1   itojun 
    875   1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    876   1.1   itojun 		return (EIO);
    877   1.1   itojun 
    878  1.13      eeh 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    879  1.23   atatat 	if (error != EPASSTHROUGH)
    880   1.1   itojun 		return (error);
    881   1.1   itojun 
    882   1.1   itojun 	error = ttioctl(tp, cmd, data, flag, p);
    883  1.23   atatat 	if (error != EPASSTHROUGH)
    884   1.1   itojun 		return (error);
    885   1.1   itojun 
    886   1.1   itojun 	error = 0;
    887   1.1   itojun 
    888   1.1   itojun 	s = splserial();
    889   1.1   itojun 
    890   1.1   itojun 	switch (cmd) {
    891   1.1   itojun 	case TIOCSBRK:
    892   1.1   itojun 		scif_break(sc, 1);
    893   1.1   itojun 		break;
    894   1.1   itojun 
    895   1.1   itojun 	case TIOCCBRK:
    896   1.1   itojun 		scif_break(sc, 0);
    897   1.1   itojun 		break;
    898   1.6  msaitoh 
    899   1.1   itojun 	case TIOCGFLAGS:
    900   1.1   itojun 		*(int *)data = sc->sc_swflags;
    901   1.1   itojun 		break;
    902   1.1   itojun 
    903   1.1   itojun 	case TIOCSFLAGS:
    904   1.1   itojun 		error = suser(p->p_ucred, &p->p_acflag);
    905   1.1   itojun 		if (error)
    906   1.1   itojun 			break;
    907   1.1   itojun 		sc->sc_swflags = *(int *)data;
    908   1.1   itojun 		break;
    909   1.1   itojun 
    910   1.1   itojun 	default:
    911  1.23   atatat 		error = EPASSTHROUGH;
    912   1.1   itojun 		break;
    913   1.1   itojun 	}
    914   1.1   itojun 
    915   1.1   itojun 	splx(s);
    916   1.1   itojun 
    917   1.1   itojun 	return (error);
    918   1.1   itojun }
    919   1.1   itojun 
    920   1.1   itojun integrate void
    921  1.20      uch scif_schedrx(struct scif_softc *sc)
    922   1.1   itojun {
    923   1.1   itojun 
    924   1.1   itojun 	sc->sc_rx_ready = 1;
    925   1.1   itojun 
    926   1.1   itojun 	/* Wake up the poller. */
    927  1.15  thorpej #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    928   1.1   itojun 	softintr_schedule(sc->sc_si);
    929   1.1   itojun #else
    930   1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
    931   1.1   itojun 	setsoftserial();
    932   1.1   itojun #else
    933   1.1   itojun 	if (!scif_softintr_scheduled) {
    934   1.1   itojun 		scif_softintr_scheduled = 1;
    935   1.8  thorpej 		callout_reset(&scif_soft_ch, 1, scifsoft, NULL);
    936   1.1   itojun 	}
    937   1.1   itojun #endif
    938   1.1   itojun #endif
    939   1.1   itojun }
    940   1.1   itojun 
    941   1.6  msaitoh void
    942  1.20      uch scif_break(struct scif_softc *sc, int onoff)
    943   1.6  msaitoh {
    944   1.6  msaitoh 
    945   1.6  msaitoh 	if (onoff)
    946   1.6  msaitoh 		SHREG_SCSSR2 &= ~SCSSR2_TDFE;
    947   1.6  msaitoh 	else
    948   1.6  msaitoh 		SHREG_SCSSR2 |= SCSSR2_TDFE;
    949   1.6  msaitoh 
    950   1.6  msaitoh #if 0	/* XXX */
    951   1.6  msaitoh 	if (!sc->sc_heldchange) {
    952   1.6  msaitoh 		if (sc->sc_tx_busy) {
    953   1.6  msaitoh 			sc->sc_heldtbc = sc->sc_tbc;
    954   1.6  msaitoh 			sc->sc_tbc = 0;
    955   1.6  msaitoh 			sc->sc_heldchange = 1;
    956   1.6  msaitoh 		} else
    957   1.6  msaitoh 			scif_loadchannelregs(sc);
    958   1.6  msaitoh 	}
    959   1.6  msaitoh #endif
    960   1.6  msaitoh }
    961   1.6  msaitoh 
    962   1.1   itojun /*
    963   1.1   itojun  * Stop output, e.g., for ^S or output flush.
    964   1.1   itojun  */
    965   1.1   itojun void
    966  1.20      uch scifstop(struct tty *tp, int flag)
    967   1.1   itojun {
    968   1.1   itojun 	struct scif_softc *sc = scif_cd.cd_devs[SCIFUNIT(tp->t_dev)];
    969   1.1   itojun 	int s;
    970   1.1   itojun 
    971   1.1   itojun 	s = splserial();
    972   1.1   itojun 	if (ISSET(tp->t_state, TS_BUSY)) {
    973   1.1   itojun 		/* Stop transmitting at the next chunk. */
    974   1.1   itojun 		sc->sc_tbc = 0;
    975   1.1   itojun 		sc->sc_heldtbc = 0;
    976   1.1   itojun 		if (!ISSET(tp->t_state, TS_TTSTOP))
    977   1.1   itojun 			SET(tp->t_state, TS_FLUSH);
    978   1.1   itojun 	}
    979   1.1   itojun 	splx(s);
    980   1.1   itojun }
    981   1.1   itojun 
    982   1.1   itojun void
    983   1.1   itojun scif_intr_init()
    984   1.1   itojun {
    985   1.1   itojun 	/* XXX */
    986   1.1   itojun }
    987   1.1   itojun 
    988   1.1   itojun void
    989  1.20      uch scifdiag(void *arg)
    990   1.1   itojun {
    991   1.1   itojun 	struct scif_softc *sc = arg;
    992   1.1   itojun 	int overflows, floods;
    993   1.1   itojun 	int s;
    994   1.1   itojun 
    995   1.1   itojun 	s = splserial();
    996   1.1   itojun 	overflows = sc->sc_overflows;
    997   1.1   itojun 	sc->sc_overflows = 0;
    998   1.1   itojun 	floods = sc->sc_floods;
    999   1.1   itojun 	sc->sc_floods = 0;
   1000   1.1   itojun 	sc->sc_errors = 0;
   1001   1.1   itojun 	splx(s);
   1002   1.1   itojun 
   1003   1.1   itojun 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1004   1.1   itojun 	    sc->sc_dev.dv_xname,
   1005   1.1   itojun 	    overflows, overflows == 1 ? "" : "s",
   1006   1.1   itojun 	    floods, floods == 1 ? "" : "s");
   1007   1.1   itojun }
   1008   1.1   itojun 
   1009   1.1   itojun integrate void
   1010  1.20      uch scif_rxsoft(struct scif_softc *sc, struct tty *tp)
   1011   1.1   itojun {
   1012  1.20      uch 	int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
   1013   1.1   itojun 	u_char *get, *end;
   1014   1.1   itojun 	u_int cc, scc;
   1015   1.1   itojun 	u_char ssr2;
   1016   1.1   itojun 	int code;
   1017   1.1   itojun 	int s;
   1018   1.1   itojun 
   1019   1.1   itojun 	end = sc->sc_ebuf;
   1020   1.1   itojun 	get = sc->sc_rbget;
   1021   1.1   itojun 	scc = cc = scif_rbuf_size - sc->sc_rbavail;
   1022   1.1   itojun 
   1023   1.1   itojun 	if (cc == scif_rbuf_size) {
   1024   1.1   itojun 		sc->sc_floods++;
   1025   1.1   itojun 		if (sc->sc_errors++ == 0)
   1026  1.11  msaitoh 			callout_reset(&sc->sc_diag_ch, 60 * hz, scifdiag, sc);
   1027   1.1   itojun 	}
   1028   1.1   itojun 
   1029   1.1   itojun 	while (cc) {
   1030   1.1   itojun 		code = get[0];
   1031   1.1   itojun 		ssr2 = get[1];
   1032   1.6  msaitoh 		if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) {
   1033   1.6  msaitoh 			if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER))
   1034   1.1   itojun 				SET(code, TTY_FE);
   1035   1.1   itojun 			if (ISSET(ssr2, SCSSR2_PER))
   1036   1.1   itojun 				SET(code, TTY_PE);
   1037   1.1   itojun 		}
   1038   1.1   itojun 		if ((*rint)(code, tp) == -1) {
   1039   1.1   itojun 			/*
   1040   1.1   itojun 			 * The line discipline's buffer is out of space.
   1041   1.1   itojun 			 */
   1042   1.1   itojun 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1043   1.1   itojun 				/*
   1044   1.1   itojun 				 * We're either not using flow control, or the
   1045   1.1   itojun 				 * line discipline didn't tell us to block for
   1046   1.1   itojun 				 * some reason.  Either way, we have no way to
   1047   1.1   itojun 				 * know when there's more space available, so
   1048   1.1   itojun 				 * just drop the rest of the data.
   1049   1.1   itojun 				 */
   1050   1.1   itojun 				get += cc << 1;
   1051   1.1   itojun 				if (get >= end)
   1052   1.1   itojun 					get -= scif_rbuf_size << 1;
   1053   1.1   itojun 				cc = 0;
   1054   1.1   itojun 			} else {
   1055   1.1   itojun 				/*
   1056   1.1   itojun 				 * Don't schedule any more receive processing
   1057   1.1   itojun 				 * until the line discipline tells us there's
   1058   1.1   itojun 				 * space available (through scifhwiflow()).
   1059   1.1   itojun 				 * Leave the rest of the data in the input
   1060   1.1   itojun 				 * buffer.
   1061   1.1   itojun 				 */
   1062   1.1   itojun 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1063   1.1   itojun 			}
   1064   1.1   itojun 			break;
   1065   1.1   itojun 		}
   1066   1.1   itojun 		get += 2;
   1067   1.1   itojun 		if (get >= end)
   1068   1.1   itojun 			get = sc->sc_rbuf;
   1069   1.1   itojun 		cc--;
   1070   1.1   itojun 	}
   1071   1.1   itojun 
   1072   1.1   itojun 	if (cc != scc) {
   1073   1.1   itojun 		sc->sc_rbget = get;
   1074   1.1   itojun 		s = splserial();
   1075   1.1   itojun 		cc = sc->sc_rbavail += scc - cc;
   1076   1.1   itojun 		/* Buffers should be ok again, release possible block. */
   1077   1.1   itojun 		if (cc >= sc->sc_r_lowat) {
   1078   1.1   itojun 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1079   1.1   itojun 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1080   1.1   itojun 				SHREG_SCSCR2 |= SCSCR2_RIE;
   1081   1.1   itojun 			}
   1082   1.1   itojun #if 0
   1083   1.1   itojun 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1084   1.1   itojun 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1085   1.1   itojun 				scif_hwiflow(sc);
   1086   1.1   itojun 			}
   1087   1.1   itojun #endif
   1088   1.1   itojun 		}
   1089   1.1   itojun 		splx(s);
   1090   1.1   itojun 	}
   1091   1.1   itojun }
   1092   1.1   itojun 
   1093   1.1   itojun integrate void
   1094  1.20      uch scif_txsoft(struct scif_softc *sc, struct tty *tp)
   1095   1.1   itojun {
   1096   1.1   itojun 
   1097   1.1   itojun 	CLR(tp->t_state, TS_BUSY);
   1098   1.1   itojun 	if (ISSET(tp->t_state, TS_FLUSH))
   1099   1.1   itojun 		CLR(tp->t_state, TS_FLUSH);
   1100   1.1   itojun 	else
   1101   1.1   itojun 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1102  1.13      eeh 	(*tp->t_linesw->l_start)(tp);
   1103   1.1   itojun }
   1104   1.1   itojun 
   1105   1.1   itojun integrate void
   1106  1.20      uch scif_stsoft(struct scif_softc *sc, struct tty *tp)
   1107   1.1   itojun {
   1108   1.1   itojun #if 0
   1109   1.1   itojun /* XXX (msaitoh) */
   1110   1.1   itojun 	u_char msr, delta;
   1111   1.1   itojun 	int s;
   1112   1.1   itojun 
   1113   1.1   itojun 	s = splserial();
   1114   1.1   itojun 	msr = sc->sc_msr;
   1115   1.1   itojun 	delta = sc->sc_msr_delta;
   1116   1.1   itojun 	sc->sc_msr_delta = 0;
   1117   1.1   itojun 	splx(s);
   1118   1.1   itojun 
   1119   1.1   itojun 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1120   1.1   itojun 		/*
   1121   1.1   itojun 		 * Inform the tty layer that carrier detect changed.
   1122   1.1   itojun 		 */
   1123  1.13      eeh 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
   1124   1.1   itojun 	}
   1125   1.1   itojun 
   1126   1.1   itojun 	if (ISSET(delta, sc->sc_msr_cts)) {
   1127   1.1   itojun 		/* Block or unblock output according to flow control. */
   1128   1.1   itojun 		if (ISSET(msr, sc->sc_msr_cts)) {
   1129   1.1   itojun 			sc->sc_tx_stopped = 0;
   1130  1.13      eeh 			(*tp->t_linesw->l_start)(tp);
   1131   1.1   itojun 		} else {
   1132   1.1   itojun 			sc->sc_tx_stopped = 1;
   1133   1.1   itojun 		}
   1134   1.1   itojun 	}
   1135   1.1   itojun 
   1136   1.1   itojun #ifdef SCIF_DEBUG
   1137   1.1   itojun 	if (scif_debug)
   1138   1.1   itojun 		scifstatus(sc, "scif_stsoft");
   1139   1.1   itojun #endif
   1140   1.1   itojun #endif
   1141   1.1   itojun }
   1142   1.1   itojun 
   1143  1.15  thorpej #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1144   1.1   itojun void
   1145  1.20      uch scifsoft(void *arg)
   1146   1.1   itojun {
   1147   1.1   itojun 	struct scif_softc *sc = arg;
   1148   1.1   itojun 	struct tty *tp;
   1149   1.1   itojun 
   1150   1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
   1151   1.1   itojun 		return;
   1152   1.1   itojun 
   1153   1.1   itojun 	{
   1154   1.1   itojun #else
   1155   1.1   itojun void
   1156   1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1157   1.1   itojun scifsoft()
   1158   1.1   itojun #else
   1159  1.20      uch scifsoft(void *arg)
   1160   1.1   itojun #endif
   1161   1.1   itojun {
   1162   1.1   itojun 	struct scif_softc	*sc;
   1163   1.1   itojun 	struct tty	*tp;
   1164   1.1   itojun 	int	unit;
   1165   1.1   itojun #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1166   1.1   itojun 	int s;
   1167   1.1   itojun 
   1168   1.1   itojun 	s = splsoftserial();
   1169   1.1   itojun 	scif_softintr_scheduled = 0;
   1170   1.1   itojun #endif
   1171   1.1   itojun 
   1172   1.1   itojun 	for (unit = 0; unit < scif_cd.cd_ndevs; unit++) {
   1173   1.1   itojun 		sc = scif_cd.cd_devs[unit];
   1174   1.1   itojun 		if (sc == NULL || !ISSET(sc->sc_hwflags, SCIF_HW_DEV_OK))
   1175   1.1   itojun 			continue;
   1176   1.1   itojun 
   1177   1.1   itojun 		if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
   1178   1.1   itojun 			continue;
   1179   1.1   itojun 
   1180   1.1   itojun 		tp = sc->sc_tty;
   1181   1.1   itojun 		if (tp == NULL)
   1182   1.1   itojun 			continue;
   1183   1.1   itojun 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1184   1.1   itojun 			continue;
   1185   1.1   itojun #endif
   1186   1.1   itojun 		tp = sc->sc_tty;
   1187   1.1   itojun 
   1188   1.1   itojun 		if (sc->sc_rx_ready) {
   1189   1.1   itojun 			sc->sc_rx_ready = 0;
   1190   1.1   itojun 			scif_rxsoft(sc, tp);
   1191   1.1   itojun 		}
   1192   1.1   itojun 
   1193   1.1   itojun #if 0
   1194   1.1   itojun 		if (sc->sc_st_check) {
   1195   1.1   itojun 			sc->sc_st_check = 0;
   1196   1.1   itojun 			scif_stsoft(sc, tp);
   1197   1.1   itojun 		}
   1198   1.1   itojun #endif
   1199   1.1   itojun 
   1200   1.1   itojun 		if (sc->sc_tx_done) {
   1201   1.1   itojun 			sc->sc_tx_done = 0;
   1202   1.1   itojun 			scif_txsoft(sc, tp);
   1203   1.1   itojun 		}
   1204   1.1   itojun 	}
   1205   1.1   itojun 
   1206  1.15  thorpej #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1207   1.1   itojun #ifdef __NO_SOFT_SERIAL_INTERRUPT
   1208   1.1   itojun 	splx(s);
   1209   1.1   itojun #endif
   1210   1.1   itojun #endif
   1211   1.1   itojun }
   1212   1.1   itojun 
   1213   1.1   itojun int
   1214  1.20      uch scifintr(void *arg)
   1215   1.1   itojun {
   1216   1.1   itojun 	struct scif_softc *sc = arg;
   1217   1.1   itojun 	u_char *put, *end;
   1218   1.1   itojun 	u_int cc;
   1219   1.1   itojun 	u_short ssr2;
   1220   1.1   itojun 	int count;
   1221   1.1   itojun 
   1222   1.1   itojun 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
   1223   1.1   itojun 		return (0);
   1224   1.1   itojun 
   1225   1.1   itojun 	end = sc->sc_ebuf;
   1226   1.1   itojun 	put = sc->sc_rbput;
   1227   1.1   itojun 	cc = sc->sc_rbavail;
   1228   1.1   itojun 
   1229   1.7  msaitoh 	ssr2 = SHREG_SCSSR2;
   1230   1.7  msaitoh 	if (ISSET(ssr2, SCSSR2_BRK)) {
   1231   1.7  msaitoh 		SHREG_SCSSR2 &= ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_DR);
   1232   1.1   itojun #ifdef DDB
   1233   1.7  msaitoh 		if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
   1234   1.7  msaitoh 			console_debugger();
   1235   1.7  msaitoh 		}
   1236  1.22      uch #endif /* DDB */
   1237   1.1   itojun #ifdef KGDB
   1238   1.7  msaitoh 		if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB)) {
   1239   1.7  msaitoh 			kgdb_connect(1);
   1240   1.7  msaitoh 		}
   1241  1.22      uch #endif /* KGDB */
   1242   1.7  msaitoh 	}
   1243   1.7  msaitoh 	count = SHREG_SCFDR2 & SCFDR2_RECVCNT;
   1244   1.7  msaitoh 	if (count != 0) {
   1245   1.7  msaitoh 		while ((cc > 0) && (count > 0)) {
   1246   1.7  msaitoh 			put[0] = SHREG_SCFRDR2;
   1247   1.7  msaitoh 			put[1] = (u_char)(SHREG_SCSSR2 & 0x00ff);
   1248   1.7  msaitoh 
   1249  1.10  msaitoh 			SHREG_SCSSR2 &= ~(SCSSR2_ER | SCSSR2_RDF | SCSSR2_DR);
   1250   1.7  msaitoh 
   1251   1.7  msaitoh 			put += 2;
   1252   1.7  msaitoh 			if (put >= end)
   1253   1.7  msaitoh 				put = sc->sc_rbuf;
   1254   1.7  msaitoh 			cc--;
   1255   1.7  msaitoh 			count--;
   1256   1.1   itojun 		}
   1257   1.1   itojun 
   1258   1.7  msaitoh 		/*
   1259   1.7  msaitoh 		 * Current string of incoming characters ended because
   1260   1.7  msaitoh 		 * no more data was available or we ran out of space.
   1261   1.7  msaitoh 		 * Schedule a receive event if any data was received.
   1262   1.7  msaitoh 		 * If we're out of space, turn off receive interrupts.
   1263   1.7  msaitoh 		 */
   1264   1.7  msaitoh 		sc->sc_rbput = put;
   1265   1.7  msaitoh 		sc->sc_rbavail = cc;
   1266   1.7  msaitoh 		if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1267   1.7  msaitoh 			sc->sc_rx_ready = 1;
   1268   1.1   itojun 
   1269   1.7  msaitoh 		/*
   1270   1.7  msaitoh 		 * See if we are in danger of overflowing a buffer. If
   1271   1.7  msaitoh 		 * so, use hardware flow control to ease the pressure.
   1272   1.7  msaitoh 		 */
   1273   1.7  msaitoh 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1274   1.7  msaitoh 		    cc < sc->sc_r_hiwat) {
   1275   1.7  msaitoh 			SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1276   1.1   itojun #if 0
   1277   1.7  msaitoh 			scif_hwiflow(sc);
   1278   1.1   itojun #endif
   1279   1.7  msaitoh 		}
   1280   1.1   itojun 
   1281   1.7  msaitoh 		/*
   1282   1.7  msaitoh 		 * If we're out of space, disable receive interrupts
   1283   1.7  msaitoh 		 * until the queue has drained a bit.
   1284   1.7  msaitoh 		 */
   1285   1.7  msaitoh 		if (!cc) {
   1286   1.7  msaitoh 			SHREG_SCSCR2 &= ~SCSCR2_RIE;
   1287   1.7  msaitoh 		}
   1288   1.7  msaitoh 	} else {
   1289   1.7  msaitoh 		if (SHREG_SCSSR2 & (SCSSR2_RDF | SCSSR2_DR)) {
   1290   1.7  msaitoh 			SHREG_SCSCR2 &= ~(SCSCR2_TIE | SCSCR2_RIE);
   1291   1.1   itojun 		}
   1292   1.7  msaitoh 	}
   1293   1.1   itojun 
   1294   1.1   itojun #if 0
   1295   1.7  msaitoh 	msr = bus_space_read_1(iot, ioh, scif_msr);
   1296   1.7  msaitoh 	delta = msr ^ sc->sc_msr;
   1297   1.7  msaitoh 	sc->sc_msr = msr;
   1298   1.7  msaitoh 	if (ISSET(delta, sc->sc_msr_mask)) {
   1299   1.7  msaitoh 		SET(sc->sc_msr_delta, delta);
   1300   1.1   itojun 
   1301   1.7  msaitoh 		/*
   1302   1.7  msaitoh 		 * Pulse-per-second clock signal on edge of DCD?
   1303   1.7  msaitoh 		 */
   1304   1.7  msaitoh 		if (ISSET(delta, sc->sc_ppsmask)) {
   1305   1.7  msaitoh 			struct timeval tv;
   1306   1.7  msaitoh 			if (ISSET(msr, sc->sc_ppsmask) ==
   1307   1.7  msaitoh 			    sc->sc_ppsassert) {
   1308   1.7  msaitoh 				/* XXX nanotime() */
   1309   1.7  msaitoh 				microtime(&tv);
   1310   1.7  msaitoh 				TIMEVAL_TO_TIMESPEC(&tv,
   1311   1.7  msaitoh 						    &sc->ppsinfo.assert_timestamp);
   1312   1.7  msaitoh 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   1313   1.7  msaitoh 					timespecadd(&sc->ppsinfo.assert_timestamp,
   1314   1.1   itojun 						    &sc->ppsparam.assert_offset,
   1315   1.1   itojun 						    &sc->ppsinfo.assert_timestamp);
   1316   1.7  msaitoh 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
   1317   1.7  msaitoh 				}
   1318   1.1   itojun 
   1319   1.1   itojun #ifdef PPS_SYNC
   1320   1.7  msaitoh 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   1321   1.7  msaitoh 					hardpps(&tv, tv.tv_usec);
   1322   1.1   itojun #endif
   1323   1.7  msaitoh 				sc->ppsinfo.assert_sequence++;
   1324   1.7  msaitoh 				sc->ppsinfo.current_mode =
   1325   1.7  msaitoh 					sc->ppsparam.mode;
   1326   1.7  msaitoh 
   1327   1.7  msaitoh 			} else if (ISSET(msr, sc->sc_ppsmask) ==
   1328   1.7  msaitoh 				   sc->sc_ppsclear) {
   1329   1.7  msaitoh 				/* XXX nanotime() */
   1330   1.7  msaitoh 				microtime(&tv);
   1331   1.7  msaitoh 				TIMEVAL_TO_TIMESPEC(&tv,
   1332   1.7  msaitoh 						    &sc->ppsinfo.clear_timestamp);
   1333   1.7  msaitoh 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   1334   1.7  msaitoh 					timespecadd(&sc->ppsinfo.clear_timestamp,
   1335   1.1   itojun 						    &sc->ppsparam.clear_offset,
   1336   1.1   itojun 						    &sc->ppsinfo.clear_timestamp);
   1337   1.7  msaitoh 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
   1338   1.7  msaitoh 				}
   1339   1.1   itojun 
   1340   1.1   itojun #ifdef PPS_SYNC
   1341   1.7  msaitoh 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   1342   1.7  msaitoh 					hardpps(&tv, tv.tv_usec);
   1343   1.1   itojun #endif
   1344   1.7  msaitoh 				sc->ppsinfo.clear_sequence++;
   1345   1.7  msaitoh 				sc->ppsinfo.current_mode =
   1346   1.7  msaitoh 					sc->ppsparam.mode;
   1347   1.1   itojun 			}
   1348   1.7  msaitoh 		}
   1349   1.1   itojun 
   1350   1.7  msaitoh 		/*
   1351   1.7  msaitoh 		 * Stop output immediately if we lose the output
   1352   1.7  msaitoh 		 * flow control signal or carrier detect.
   1353   1.7  msaitoh 		 */
   1354   1.7  msaitoh 		if (ISSET(~msr, sc->sc_msr_mask)) {
   1355   1.7  msaitoh 			sc->sc_tbc = 0;
   1356   1.7  msaitoh 			sc->sc_heldtbc = 0;
   1357   1.1   itojun #ifdef SCIF_DEBUG
   1358   1.7  msaitoh 			if (scif_debug)
   1359   1.7  msaitoh 				scifstatus(sc, "scifintr  ");
   1360   1.1   itojun #endif
   1361   1.7  msaitoh 		}
   1362   1.1   itojun 
   1363   1.7  msaitoh 		sc->sc_st_check = 1;
   1364   1.7  msaitoh 	}
   1365   1.1   itojun #endif
   1366   1.1   itojun 
   1367   1.1   itojun 	/*
   1368   1.1   itojun 	 * Done handling any receive interrupts. See if data can be
   1369   1.1   itojun 	 * transmitted as well. Schedule tx done event if no data left
   1370   1.1   itojun 	 * and tty was marked busy.
   1371   1.1   itojun 	 */
   1372   1.7  msaitoh 	if (((SHREG_SCFDR2 & SCFDR2_TXCNT) >> 8) != 16) { /* XXX (msaitoh) */
   1373   1.1   itojun 		/*
   1374   1.1   itojun 		 * If we've delayed a parameter change, do it now, and restart
   1375   1.1   itojun 		 * output.
   1376   1.1   itojun 		 */
   1377   1.1   itojun 		if (sc->sc_heldchange) {
   1378   1.1   itojun 			sc->sc_heldchange = 0;
   1379   1.1   itojun 			sc->sc_tbc = sc->sc_heldtbc;
   1380   1.1   itojun 			sc->sc_heldtbc = 0;
   1381   1.1   itojun 		}
   1382   1.1   itojun 
   1383   1.1   itojun 		/* Output the next chunk of the contiguous buffer, if any. */
   1384   1.1   itojun 		if (sc->sc_tbc > 0) {
   1385   1.1   itojun 			int n;
   1386   1.1   itojun 			int max;
   1387   1.1   itojun 			int i;
   1388   1.1   itojun 
   1389   1.1   itojun 			n = sc->sc_tbc;
   1390   1.1   itojun 			max = sc->sc_fifolen -
   1391   1.1   itojun 				((SHREG_SCFDR2 & SCFDR2_TXCNT) >> 8);
   1392   1.1   itojun 			if (n > max)
   1393   1.1   itojun 				n = max;
   1394   1.1   itojun 
   1395   1.1   itojun 			for (i = 0; i < n; i++) {
   1396  1.14  msaitoh 				scif_putc(*(sc->sc_tba));
   1397   1.1   itojun 				sc->sc_tba++;
   1398   1.1   itojun 			}
   1399   1.1   itojun 			sc->sc_tbc -= n;
   1400   1.1   itojun 		} else {
   1401   1.1   itojun 			/* Disable transmit completion interrupts if necessary. */
   1402   1.1   itojun #if 0
   1403   1.1   itojun 			if (ISSET(sc->sc_ier, IER_ETXRDY))
   1404   1.1   itojun #endif
   1405   1.1   itojun 				SHREG_SCSCR2 &= ~SCSCR2_TIE;
   1406   1.1   itojun 
   1407   1.1   itojun 			if (sc->sc_tx_busy) {
   1408   1.1   itojun 				sc->sc_tx_busy = 0;
   1409   1.1   itojun 				sc->sc_tx_done = 1;
   1410   1.1   itojun 			}
   1411   1.1   itojun 		}
   1412   1.1   itojun 	}
   1413   1.1   itojun 
   1414   1.1   itojun 	/* Wake up the poller. */
   1415  1.15  thorpej #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1416   1.1   itojun 	softintr_schedule(sc->sc_si);
   1417   1.1   itojun #else
   1418   1.1   itojun #ifndef __NO_SOFT_SERIAL_INTERRUPT
   1419   1.1   itojun 	setsoftserial();
   1420   1.1   itojun #else
   1421   1.1   itojun 	if (!scif_softintr_scheduled) {
   1422   1.1   itojun 		scif_softintr_scheduled = 1;
   1423   1.8  thorpej 		callout_reset(&scif_soft_ch, 1, scifsoft, NULL);
   1424   1.1   itojun 	}
   1425   1.1   itojun #endif
   1426   1.1   itojun #endif
   1427   1.1   itojun 
   1428   1.1   itojun #if NRND > 0 && defined(RND_SCIF)
   1429  1.20      uch rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1430   1.1   itojun #endif
   1431   1.1   itojun 
   1432   1.1   itojun 	return (1);
   1433   1.1   itojun }
   1434   1.1   itojun 
   1435   1.1   itojun void
   1436  1.20      uch scifcnprobe(struct consdev *cp)
   1437   1.1   itojun {
   1438   1.1   itojun 	int maj;
   1439   1.1   itojun 
   1440   1.1   itojun 	/* locate the major number */
   1441   1.1   itojun 	for (maj = 0; maj < nchrdev; maj++)
   1442   1.1   itojun 		if (cdevsw[maj].d_open == scifopen)
   1443   1.1   itojun 			break;
   1444   1.1   itojun 
   1445   1.1   itojun 	/* Initialize required fields. */
   1446   1.1   itojun 	cp->cn_dev = makedev(maj, 0);
   1447   1.4  msaitoh #ifdef SCIFCONSOLE
   1448   1.4  msaitoh 	cp->cn_pri = CN_REMOTE;
   1449   1.4  msaitoh #else
   1450   1.1   itojun 	cp->cn_pri = CN_NORMAL;
   1451   1.1   itojun #endif
   1452   1.1   itojun }
   1453   1.1   itojun 
   1454   1.1   itojun void
   1455  1.20      uch scifcninit(struct consdev *cp)
   1456   1.1   itojun {
   1457   1.1   itojun 
   1458   1.7  msaitoh 	InitializeScif(scifcn_speed);
   1459   1.9  msaitoh 	scifisconsole = 1;
   1460   1.1   itojun }
   1461   1.1   itojun 
   1462   1.1   itojun int
   1463  1.20      uch scifcngetc(dev_t dev)
   1464   1.1   itojun {
   1465   1.1   itojun 	int c;
   1466   1.1   itojun 	int s;
   1467   1.1   itojun 
   1468   1.1   itojun 	s = splserial();
   1469   1.1   itojun 	c = scif_getc();
   1470   1.1   itojun 	splx(s);
   1471   1.1   itojun 
   1472   1.1   itojun 	return (c);
   1473   1.1   itojun }
   1474   1.1   itojun 
   1475   1.1   itojun void
   1476  1.20      uch scifcnputc(dev_t dev, int c)
   1477   1.1   itojun {
   1478   1.1   itojun 	int s;
   1479   1.1   itojun 
   1480   1.1   itojun 	s = splserial();
   1481  1.14  msaitoh 	scif_putc((u_char)c);
   1482   1.1   itojun 	splx(s);
   1483   1.1   itojun }
   1484  1.22      uch 
   1485  1.22      uch #ifdef KGDB
   1486  1.22      uch int
   1487  1.22      uch scif_kgdb_init()
   1488  1.22      uch {
   1489  1.22      uch 
   1490  1.22      uch 	if (strcmp(kgdb_devname, "scif") != 0)
   1491  1.22      uch 		return (1);
   1492  1.22      uch 
   1493  1.22      uch 	if (scifisconsole)
   1494  1.22      uch 		return (1);	/* can't share with console */
   1495  1.22      uch 
   1496  1.22      uch 	InitializeScif(kgdb_rate);
   1497  1.22      uch 
   1498  1.22      uch 	kgdb_attach((int (*)(void *))scifcngetc,
   1499  1.22      uch 	    (void (*)(void *, int))scifcnputc, NULL);
   1500  1.22      uch 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   1501  1.22      uch 	kgdb_attached = 1;
   1502  1.22      uch 
   1503  1.22      uch 	return (0);
   1504  1.22      uch }
   1505  1.22      uch #endif /* KGDB */
   1506