Home | History | Annotate | Line # | Download | only in ic
clmpcc.c revision 1.27.2.2
      1  1.27.2.2  elad /*	$NetBSD: clmpcc.c,v 1.27.2.2 2006/03/08 01:44:48 elad Exp $ */
      2  1.27.2.2  elad 
      3  1.27.2.2  elad /*-
      4  1.27.2.2  elad  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.27.2.2  elad  * All rights reserved.
      6  1.27.2.2  elad  *
      7  1.27.2.2  elad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.27.2.2  elad  * by Steve C. Woodford.
      9  1.27.2.2  elad  *
     10  1.27.2.2  elad  * Redistribution and use in source and binary forms, with or without
     11  1.27.2.2  elad  * modification, are permitted provided that the following conditions
     12  1.27.2.2  elad  * are met:
     13  1.27.2.2  elad  * 1. Redistributions of source code must retain the above copyright
     14  1.27.2.2  elad  *    notice, this list of conditions and the following disclaimer.
     15  1.27.2.2  elad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.27.2.2  elad  *    notice, this list of conditions and the following disclaimer in the
     17  1.27.2.2  elad  *    documentation and/or other materials provided with the distribution.
     18  1.27.2.2  elad  * 3. All advertising materials mentioning features or use of this software
     19  1.27.2.2  elad  *    must display the following acknowledgement:
     20  1.27.2.2  elad  *        This product includes software developed by the NetBSD
     21  1.27.2.2  elad  *        Foundation, Inc. and its contributors.
     22  1.27.2.2  elad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.27.2.2  elad  *    contributors may be used to endorse or promote products derived
     24  1.27.2.2  elad  *    from this software without specific prior written permission.
     25  1.27.2.2  elad  *
     26  1.27.2.2  elad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.27.2.2  elad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.27.2.2  elad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.27.2.2  elad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.27.2.2  elad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.27.2.2  elad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.27.2.2  elad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.27.2.2  elad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.27.2.2  elad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.27.2.2  elad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.27.2.2  elad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.27.2.2  elad  */
     38  1.27.2.2  elad 
     39  1.27.2.2  elad /*
     40  1.27.2.2  elad  * Cirrus Logic CD2400/CD2401 Four Channel Multi-Protocol Comms. Controller.
     41  1.27.2.2  elad  */
     42  1.27.2.2  elad 
     43  1.27.2.2  elad #include <sys/cdefs.h>
     44  1.27.2.2  elad __KERNEL_RCSID(0, "$NetBSD: clmpcc.c,v 1.27.2.2 2006/03/08 01:44:48 elad Exp $");
     45  1.27.2.2  elad 
     46  1.27.2.2  elad #include "opt_ddb.h"
     47  1.27.2.2  elad 
     48  1.27.2.2  elad #include <sys/param.h>
     49  1.27.2.2  elad #include <sys/systm.h>
     50  1.27.2.2  elad #include <sys/ioctl.h>
     51  1.27.2.2  elad #include <sys/select.h>
     52  1.27.2.2  elad #include <sys/tty.h>
     53  1.27.2.2  elad #include <sys/proc.h>
     54  1.27.2.2  elad #include <sys/user.h>
     55  1.27.2.2  elad #include <sys/conf.h>
     56  1.27.2.2  elad #include <sys/file.h>
     57  1.27.2.2  elad #include <sys/uio.h>
     58  1.27.2.2  elad #include <sys/kernel.h>
     59  1.27.2.2  elad #include <sys/syslog.h>
     60  1.27.2.2  elad #include <sys/device.h>
     61  1.27.2.2  elad #include <sys/malloc.h>
     62  1.27.2.2  elad #include <sys/kauth.h>
     63  1.27.2.2  elad 
     64  1.27.2.2  elad #include <machine/bus.h>
     65  1.27.2.2  elad #include <machine/intr.h>
     66  1.27.2.2  elad #include <machine/param.h>
     67  1.27.2.2  elad 
     68  1.27.2.2  elad #include <dev/ic/clmpccreg.h>
     69  1.27.2.2  elad #include <dev/ic/clmpccvar.h>
     70  1.27.2.2  elad #include <dev/cons.h>
     71  1.27.2.2  elad 
     72  1.27.2.2  elad 
     73  1.27.2.2  elad #if defined(CLMPCC_ONLY_BYTESWAP_LOW) && defined(CLMPCC_ONLY_BYTESWAP_HIGH)
     74  1.27.2.2  elad #error	"CLMPCC_ONLY_BYTESWAP_LOW and CLMPCC_ONLY_BYTESWAP_HIGH are mutually exclusive."
     75  1.27.2.2  elad #endif
     76  1.27.2.2  elad 
     77  1.27.2.2  elad 
     78  1.27.2.2  elad static int	clmpcc_init(struct clmpcc_softc *sc);
     79  1.27.2.2  elad static void	clmpcc_shutdown(struct clmpcc_chan *);
     80  1.27.2.2  elad static int	clmpcc_speed(struct clmpcc_softc *, speed_t, int *, int *);
     81  1.27.2.2  elad static int	clmpcc_param(struct tty *, struct termios *);
     82  1.27.2.2  elad static void	clmpcc_set_params(struct clmpcc_chan *);
     83  1.27.2.2  elad static void	clmpcc_start(struct tty *);
     84  1.27.2.2  elad static int 	clmpcc_modem_control(struct clmpcc_chan *, int, int);
     85  1.27.2.2  elad 
     86  1.27.2.2  elad #define	CLMPCCUNIT(x)		(minor(x) & 0x7fffc)
     87  1.27.2.2  elad #define CLMPCCCHAN(x)		(minor(x) & 0x00003)
     88  1.27.2.2  elad #define	CLMPCCDIALOUT(x)	(minor(x) & 0x80000)
     89  1.27.2.2  elad 
     90  1.27.2.2  elad /*
     91  1.27.2.2  elad  * These should be in a header file somewhere...
     92  1.27.2.2  elad  */
     93  1.27.2.2  elad #define	ISCLR(v, f)	(((v) & (f)) == 0)
     94  1.27.2.2  elad 
     95  1.27.2.2  elad extern struct cfdriver clmpcc_cd;
     96  1.27.2.2  elad 
     97  1.27.2.2  elad dev_type_open(clmpccopen);
     98  1.27.2.2  elad dev_type_close(clmpccclose);
     99  1.27.2.2  elad dev_type_read(clmpccread);
    100  1.27.2.2  elad dev_type_write(clmpccwrite);
    101  1.27.2.2  elad dev_type_ioctl(clmpccioctl);
    102  1.27.2.2  elad dev_type_stop(clmpccstop);
    103  1.27.2.2  elad dev_type_tty(clmpcctty);
    104  1.27.2.2  elad dev_type_poll(clmpccpoll);
    105  1.27.2.2  elad 
    106  1.27.2.2  elad const struct cdevsw clmpcc_cdevsw = {
    107  1.27.2.2  elad 	clmpccopen, clmpccclose, clmpccread, clmpccwrite, clmpccioctl,
    108  1.27.2.2  elad 	clmpccstop, clmpcctty, clmpccpoll, nommap, ttykqfilter, D_TTY
    109  1.27.2.2  elad };
    110  1.27.2.2  elad 
    111  1.27.2.2  elad /*
    112  1.27.2.2  elad  * Make this an option variable one can patch.
    113  1.27.2.2  elad  */
    114  1.27.2.2  elad u_int clmpcc_ibuf_size = CLMPCC_RING_SIZE;
    115  1.27.2.2  elad 
    116  1.27.2.2  elad 
    117  1.27.2.2  elad /*
    118  1.27.2.2  elad  * Things needed when the device is used as a console
    119  1.27.2.2  elad  */
    120  1.27.2.2  elad static struct clmpcc_softc *cons_sc = NULL;
    121  1.27.2.2  elad static int cons_chan;
    122  1.27.2.2  elad static int cons_rate;
    123  1.27.2.2  elad 
    124  1.27.2.2  elad static int	clmpcc_common_getc(struct clmpcc_softc *, int);
    125  1.27.2.2  elad static void	clmpcc_common_putc(struct clmpcc_softc *, int, int);
    126  1.27.2.2  elad int		clmpcccngetc(dev_t);
    127  1.27.2.2  elad void		clmpcccnputc(dev_t, int);
    128  1.27.2.2  elad 
    129  1.27.2.2  elad 
    130  1.27.2.2  elad /*
    131  1.27.2.2  elad  * Convenience functions, inlined for speed
    132  1.27.2.2  elad  */
    133  1.27.2.2  elad #define	integrate   static inline
    134  1.27.2.2  elad integrate u_int8_t  clmpcc_rdreg(struct clmpcc_softc *, u_int);
    135  1.27.2.2  elad integrate void      clmpcc_wrreg(struct clmpcc_softc *, u_int, u_int);
    136  1.27.2.2  elad integrate u_int8_t  clmpcc_rdreg_odd(struct clmpcc_softc *, u_int);
    137  1.27.2.2  elad integrate void      clmpcc_wrreg_odd(struct clmpcc_softc *, u_int, u_int);
    138  1.27.2.2  elad integrate void      clmpcc_wrtx_multi(struct clmpcc_softc *, u_int8_t *,
    139  1.27.2.2  elad 					u_int);
    140  1.27.2.2  elad integrate u_int8_t  clmpcc_select_channel(struct clmpcc_softc *, u_int);
    141  1.27.2.2  elad integrate void      clmpcc_channel_cmd(struct clmpcc_softc *,int,int);
    142  1.27.2.2  elad integrate void      clmpcc_enable_transmitter(struct clmpcc_chan *);
    143  1.27.2.2  elad 
    144  1.27.2.2  elad #define clmpcc_rd_msvr(s)	clmpcc_rdreg_odd(s,CLMPCC_REG_MSVR)
    145  1.27.2.2  elad #define clmpcc_wr_msvr(s,r,v)	clmpcc_wrreg_odd(s,r,v)
    146  1.27.2.2  elad #define clmpcc_wr_pilr(s,r,v)	clmpcc_wrreg_odd(s,r,v)
    147  1.27.2.2  elad #define clmpcc_rd_rxdata(s)	clmpcc_rdreg_odd(s,CLMPCC_REG_RDR)
    148  1.27.2.2  elad #define clmpcc_wr_txdata(s,v)	clmpcc_wrreg_odd(s,CLMPCC_REG_TDR,v)
    149  1.27.2.2  elad 
    150  1.27.2.2  elad 
    151  1.27.2.2  elad integrate u_int8_t
    152  1.27.2.2  elad clmpcc_rdreg(sc, offset)
    153  1.27.2.2  elad 	struct clmpcc_softc *sc;
    154  1.27.2.2  elad 	u_int offset;
    155  1.27.2.2  elad {
    156  1.27.2.2  elad #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    157  1.27.2.2  elad 	offset ^= sc->sc_byteswap;
    158  1.27.2.2  elad #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    159  1.27.2.2  elad 	offset ^= CLMPCC_BYTESWAP_HIGH;
    160  1.27.2.2  elad #endif
    161  1.27.2.2  elad 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
    162  1.27.2.2  elad }
    163  1.27.2.2  elad 
    164  1.27.2.2  elad integrate void
    165  1.27.2.2  elad clmpcc_wrreg(sc, offset, val)
    166  1.27.2.2  elad 	struct clmpcc_softc *sc;
    167  1.27.2.2  elad 	u_int offset;
    168  1.27.2.2  elad 	u_int val;
    169  1.27.2.2  elad {
    170  1.27.2.2  elad #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    171  1.27.2.2  elad 	offset ^= sc->sc_byteswap;
    172  1.27.2.2  elad #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    173  1.27.2.2  elad 	offset ^= CLMPCC_BYTESWAP_HIGH;
    174  1.27.2.2  elad #endif
    175  1.27.2.2  elad 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, val);
    176  1.27.2.2  elad }
    177  1.27.2.2  elad 
    178  1.27.2.2  elad integrate u_int8_t
    179  1.27.2.2  elad clmpcc_rdreg_odd(sc, offset)
    180  1.27.2.2  elad 	struct clmpcc_softc *sc;
    181  1.27.2.2  elad 	u_int offset;
    182  1.27.2.2  elad {
    183  1.27.2.2  elad #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    184  1.27.2.2  elad 	offset ^= (sc->sc_byteswap & 2);
    185  1.27.2.2  elad #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    186  1.27.2.2  elad 	offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
    187  1.27.2.2  elad #endif
    188  1.27.2.2  elad 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
    189  1.27.2.2  elad }
    190  1.27.2.2  elad 
    191  1.27.2.2  elad integrate void
    192  1.27.2.2  elad clmpcc_wrreg_odd(sc, offset, val)
    193  1.27.2.2  elad 	struct clmpcc_softc *sc;
    194  1.27.2.2  elad 	u_int offset;
    195  1.27.2.2  elad 	u_int val;
    196  1.27.2.2  elad {
    197  1.27.2.2  elad #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    198  1.27.2.2  elad 	offset ^= (sc->sc_byteswap & 2);
    199  1.27.2.2  elad #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    200  1.27.2.2  elad 	offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
    201  1.27.2.2  elad #endif
    202  1.27.2.2  elad 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, val);
    203  1.27.2.2  elad }
    204  1.27.2.2  elad 
    205  1.27.2.2  elad integrate void
    206  1.27.2.2  elad clmpcc_wrtx_multi(sc, buff, count)
    207  1.27.2.2  elad 	struct clmpcc_softc *sc;
    208  1.27.2.2  elad 	u_int8_t *buff;
    209  1.27.2.2  elad 	u_int count;
    210  1.27.2.2  elad {
    211  1.27.2.2  elad 	u_int offset = CLMPCC_REG_TDR;
    212  1.27.2.2  elad 
    213  1.27.2.2  elad #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    214  1.27.2.2  elad 	offset ^= (sc->sc_byteswap & 2);
    215  1.27.2.2  elad #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    216  1.27.2.2  elad 	offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
    217  1.27.2.2  elad #endif
    218  1.27.2.2  elad 	bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh, offset, buff, count);
    219  1.27.2.2  elad }
    220  1.27.2.2  elad 
    221  1.27.2.2  elad integrate u_int8_t
    222  1.27.2.2  elad clmpcc_select_channel(sc, new_chan)
    223  1.27.2.2  elad 	struct clmpcc_softc *sc;
    224  1.27.2.2  elad 	u_int new_chan;
    225  1.27.2.2  elad {
    226  1.27.2.2  elad 	u_int old_chan = clmpcc_rdreg_odd(sc, CLMPCC_REG_CAR);
    227  1.27.2.2  elad 
    228  1.27.2.2  elad 	clmpcc_wrreg_odd(sc, CLMPCC_REG_CAR, new_chan);
    229  1.27.2.2  elad 
    230  1.27.2.2  elad 	return old_chan;
    231  1.27.2.2  elad }
    232  1.27.2.2  elad 
    233  1.27.2.2  elad integrate void
    234  1.27.2.2  elad clmpcc_channel_cmd(sc, chan, cmd)
    235  1.27.2.2  elad 	struct clmpcc_softc *sc;
    236  1.27.2.2  elad 	int chan;
    237  1.27.2.2  elad 	int cmd;
    238  1.27.2.2  elad {
    239  1.27.2.2  elad 	int i;
    240  1.27.2.2  elad 
    241  1.27.2.2  elad 	for (i = 5000; i; i--) {
    242  1.27.2.2  elad 		if ( clmpcc_rdreg(sc, CLMPCC_REG_CCR) == 0 )
    243  1.27.2.2  elad 			break;
    244  1.27.2.2  elad 		delay(1);
    245  1.27.2.2  elad 	}
    246  1.27.2.2  elad 
    247  1.27.2.2  elad 	if ( i == 0 )
    248  1.27.2.2  elad 		printf("%s: channel %d command timeout (idle)\n",
    249  1.27.2.2  elad 			sc->sc_dev.dv_xname, chan);
    250  1.27.2.2  elad 
    251  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_CCR, cmd);
    252  1.27.2.2  elad }
    253  1.27.2.2  elad 
    254  1.27.2.2  elad integrate void
    255  1.27.2.2  elad clmpcc_enable_transmitter(ch)
    256  1.27.2.2  elad 	struct clmpcc_chan *ch;
    257  1.27.2.2  elad {
    258  1.27.2.2  elad 	u_int old;
    259  1.27.2.2  elad 	int s;
    260  1.27.2.2  elad 
    261  1.27.2.2  elad 	old = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
    262  1.27.2.2  elad 
    263  1.27.2.2  elad 	s = splserial();
    264  1.27.2.2  elad 	clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER,
    265  1.27.2.2  elad 		clmpcc_rdreg(ch->ch_sc, CLMPCC_REG_IER) | CLMPCC_IER_TX_EMPTY);
    266  1.27.2.2  elad 	SET(ch->ch_tty->t_state, TS_BUSY);
    267  1.27.2.2  elad 	splx(s);
    268  1.27.2.2  elad 
    269  1.27.2.2  elad 	clmpcc_select_channel(ch->ch_sc, old);
    270  1.27.2.2  elad }
    271  1.27.2.2  elad 
    272  1.27.2.2  elad static int
    273  1.27.2.2  elad clmpcc_speed(sc, speed, cor, bpr)
    274  1.27.2.2  elad 	struct clmpcc_softc *sc;
    275  1.27.2.2  elad 	speed_t speed;
    276  1.27.2.2  elad 	int *cor, *bpr;
    277  1.27.2.2  elad {
    278  1.27.2.2  elad 	int c, co, br;
    279  1.27.2.2  elad 
    280  1.27.2.2  elad 	for (co = 0, c = 8; c <= 2048; co++, c *= 4) {
    281  1.27.2.2  elad 		br = ((sc->sc_clk / c) / speed) - 1;
    282  1.27.2.2  elad 		if ( br < 0x100 ) {
    283  1.27.2.2  elad 			*cor = co;
    284  1.27.2.2  elad 			*bpr = br;
    285  1.27.2.2  elad 			return 0;
    286  1.27.2.2  elad 		}
    287  1.27.2.2  elad 	}
    288  1.27.2.2  elad 
    289  1.27.2.2  elad 	return -1;
    290  1.27.2.2  elad }
    291  1.27.2.2  elad 
    292  1.27.2.2  elad void
    293  1.27.2.2  elad clmpcc_attach(sc)
    294  1.27.2.2  elad 	struct clmpcc_softc *sc;
    295  1.27.2.2  elad {
    296  1.27.2.2  elad 	struct clmpcc_chan *ch;
    297  1.27.2.2  elad 	struct tty *tp;
    298  1.27.2.2  elad 	int chan;
    299  1.27.2.2  elad 
    300  1.27.2.2  elad 	if ( cons_sc != NULL &&
    301  1.27.2.2  elad 	     sc->sc_iot == cons_sc->sc_iot && sc->sc_ioh == cons_sc->sc_ioh )
    302  1.27.2.2  elad 		cons_sc = sc;
    303  1.27.2.2  elad 
    304  1.27.2.2  elad 	/* Initialise the chip */
    305  1.27.2.2  elad 	clmpcc_init(sc);
    306  1.27.2.2  elad 
    307  1.27.2.2  elad 	printf(": Cirrus Logic CD240%c Serial Controller\n",
    308  1.27.2.2  elad 		(clmpcc_rd_msvr(sc) & CLMPCC_MSVR_PORT_ID) ? '0' : '1');
    309  1.27.2.2  elad 
    310  1.27.2.2  elad #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
    311  1.27.2.2  elad 	sc->sc_soft_running = 0;
    312  1.27.2.2  elad #else
    313  1.27.2.2  elad 	sc->sc_softintr_cookie =
    314  1.27.2.2  elad 	    softintr_establish(IPL_SOFTSERIAL, clmpcc_softintr, sc);
    315  1.27.2.2  elad #ifdef DEBUG
    316  1.27.2.2  elad 	if (sc->sc_softintr_cookie == NULL)
    317  1.27.2.2  elad 		panic("clmpcc_attach: softintr_establish");
    318  1.27.2.2  elad #endif
    319  1.27.2.2  elad #endif
    320  1.27.2.2  elad 	memset(&(sc->sc_chans[0]), 0, sizeof(sc->sc_chans));
    321  1.27.2.2  elad 
    322  1.27.2.2  elad 	for (chan = 0; chan < CLMPCC_NUM_CHANS; chan++) {
    323  1.27.2.2  elad 		ch = &sc->sc_chans[chan];
    324  1.27.2.2  elad 
    325  1.27.2.2  elad 		ch->ch_sc = sc;
    326  1.27.2.2  elad 		ch->ch_car = chan;
    327  1.27.2.2  elad 
    328  1.27.2.2  elad 		tp = ttymalloc();
    329  1.27.2.2  elad 		tp->t_oproc = clmpcc_start;
    330  1.27.2.2  elad 		tp->t_param = clmpcc_param;
    331  1.27.2.2  elad 
    332  1.27.2.2  elad 		ch->ch_tty = tp;
    333  1.27.2.2  elad 
    334  1.27.2.2  elad 		ch->ch_ibuf = malloc(clmpcc_ibuf_size * 2, M_DEVBUF, M_NOWAIT);
    335  1.27.2.2  elad 		if ( ch->ch_ibuf == NULL ) {
    336  1.27.2.2  elad 			printf("%s(%d): unable to allocate ring buffer\n",
    337  1.27.2.2  elad 		    		sc->sc_dev.dv_xname, chan);
    338  1.27.2.2  elad 			return;
    339  1.27.2.2  elad 		}
    340  1.27.2.2  elad 
    341  1.27.2.2  elad 		ch->ch_ibuf_end = &(ch->ch_ibuf[clmpcc_ibuf_size * 2]);
    342  1.27.2.2  elad 		ch->ch_ibuf_rd = ch->ch_ibuf_wr = ch->ch_ibuf;
    343  1.27.2.2  elad 
    344  1.27.2.2  elad 		tty_attach(tp);
    345  1.27.2.2  elad 	}
    346  1.27.2.2  elad 
    347  1.27.2.2  elad 	printf("%s: %d channels available", sc->sc_dev.dv_xname,
    348  1.27.2.2  elad 					    CLMPCC_NUM_CHANS);
    349  1.27.2.2  elad 	if ( cons_sc == sc ) {
    350  1.27.2.2  elad 		printf(", console on channel %d.\n", cons_chan);
    351  1.27.2.2  elad 		SET(sc->sc_chans[cons_chan].ch_flags, CLMPCC_FLG_IS_CONSOLE);
    352  1.27.2.2  elad 		SET(sc->sc_chans[cons_chan].ch_openflags, TIOCFLAG_SOFTCAR);
    353  1.27.2.2  elad 	} else
    354  1.27.2.2  elad 		printf(".\n");
    355  1.27.2.2  elad }
    356  1.27.2.2  elad 
    357  1.27.2.2  elad static int
    358  1.27.2.2  elad clmpcc_init(sc)
    359  1.27.2.2  elad 	struct clmpcc_softc *sc;
    360  1.27.2.2  elad {
    361  1.27.2.2  elad 	u_int tcor, tbpr;
    362  1.27.2.2  elad 	u_int rcor, rbpr;
    363  1.27.2.2  elad 	u_int msvr_rts, msvr_dtr;
    364  1.27.2.2  elad 	u_int ccr;
    365  1.27.2.2  elad 	int is_console;
    366  1.27.2.2  elad 	int i;
    367  1.27.2.2  elad 
    368  1.27.2.2  elad 	/*
    369  1.27.2.2  elad 	 * All we're really concerned about here is putting the chip
    370  1.27.2.2  elad 	 * into a quiescent state so that it won't do anything until
    371  1.27.2.2  elad 	 * clmpccopen() is called. (Except the console channel.)
    372  1.27.2.2  elad 	 */
    373  1.27.2.2  elad 
    374  1.27.2.2  elad 	/*
    375  1.27.2.2  elad 	 * If the chip is acting as console, set all channels to the supplied
    376  1.27.2.2  elad 	 * console baud rate. Otherwise, plump for 9600.
    377  1.27.2.2  elad 	 */
    378  1.27.2.2  elad 	if ( cons_sc &&
    379  1.27.2.2  elad 	     sc->sc_ioh == cons_sc->sc_ioh && sc->sc_iot == cons_sc->sc_iot ) {
    380  1.27.2.2  elad 		clmpcc_speed(sc, cons_rate, &tcor, &tbpr);
    381  1.27.2.2  elad 		clmpcc_speed(sc, cons_rate, &rcor, &rbpr);
    382  1.27.2.2  elad 		is_console = 1;
    383  1.27.2.2  elad 	} else {
    384  1.27.2.2  elad 		clmpcc_speed(sc, 9600, &tcor, &tbpr);
    385  1.27.2.2  elad 		clmpcc_speed(sc, 9600, &rcor, &rbpr);
    386  1.27.2.2  elad 		is_console = 0;
    387  1.27.2.2  elad 	}
    388  1.27.2.2  elad 
    389  1.27.2.2  elad 	/* Allow any pending output to be sent */
    390  1.27.2.2  elad 	delay(10000);
    391  1.27.2.2  elad 
    392  1.27.2.2  elad 	/* Send the Reset All command  to channel 0 (resets all channels!) */
    393  1.27.2.2  elad 	clmpcc_channel_cmd(sc, 0, CLMPCC_CCR_T0_RESET_ALL);
    394  1.27.2.2  elad 
    395  1.27.2.2  elad 	delay(1000);
    396  1.27.2.2  elad 
    397  1.27.2.2  elad 	/*
    398  1.27.2.2  elad 	 * The chip will set it's firmware revision register to a non-zero
    399  1.27.2.2  elad 	 * value to indicate completion of reset.
    400  1.27.2.2  elad 	 */
    401  1.27.2.2  elad 	for (i = 10000; clmpcc_rdreg(sc, CLMPCC_REG_GFRCR) == 0 && i; i--)
    402  1.27.2.2  elad 		delay(1);
    403  1.27.2.2  elad 
    404  1.27.2.2  elad 	if ( i == 0 ) {
    405  1.27.2.2  elad 		/*
    406  1.27.2.2  elad 		 * Watch out... If this chip is console, the message
    407  1.27.2.2  elad 		 * probably won't be sent since we just reset it!
    408  1.27.2.2  elad 		 */
    409  1.27.2.2  elad 		printf("%s: Failed to reset chip\n", sc->sc_dev.dv_xname);
    410  1.27.2.2  elad 		return -1;
    411  1.27.2.2  elad 	}
    412  1.27.2.2  elad 
    413  1.27.2.2  elad 	for (i = 0; i < CLMPCC_NUM_CHANS; i++) {
    414  1.27.2.2  elad 		clmpcc_select_channel(sc, i);
    415  1.27.2.2  elad 
    416  1.27.2.2  elad 		/* All interrupts are disabled to begin with */
    417  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_IER, 0);
    418  1.27.2.2  elad 
    419  1.27.2.2  elad 		/* Make sure the channel interrupts on the correct vectors */
    420  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_LIVR, sc->sc_vector_base);
    421  1.27.2.2  elad 		clmpcc_wr_pilr(sc, CLMPCC_REG_RPILR, sc->sc_rpilr);
    422  1.27.2.2  elad 		clmpcc_wr_pilr(sc, CLMPCC_REG_TPILR, sc->sc_tpilr);
    423  1.27.2.2  elad 		clmpcc_wr_pilr(sc, CLMPCC_REG_MPILR, sc->sc_mpilr);
    424  1.27.2.2  elad 
    425  1.27.2.2  elad 		/* Receive timer prescaler set to 1ms */
    426  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_TPR,
    427  1.27.2.2  elad 				 CLMPCC_MSEC_TO_TPR(sc->sc_clk, 1));
    428  1.27.2.2  elad 
    429  1.27.2.2  elad 		/* We support Async mode only */
    430  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_CMR, CLMPCC_CMR_ASYNC);
    431  1.27.2.2  elad 
    432  1.27.2.2  elad 		/* Set the required baud rate */
    433  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_TCOR, CLMPCC_TCOR_CLK(tcor));
    434  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_TBPR, tbpr);
    435  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_RCOR, CLMPCC_RCOR_CLK(rcor));
    436  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_RBPR, rbpr);
    437  1.27.2.2  elad 
    438  1.27.2.2  elad 		/* Always default to 8N1 (XXX what about console?) */
    439  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR1, CLMPCC_COR1_CHAR_8BITS |
    440  1.27.2.2  elad 						  CLMPCC_COR1_NO_PARITY |
    441  1.27.2.2  elad 						  CLMPCC_COR1_IGNORE_PAR);
    442  1.27.2.2  elad 
    443  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR2, 0);
    444  1.27.2.2  elad 
    445  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR3, CLMPCC_COR3_STOP_1);
    446  1.27.2.2  elad 
    447  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR4, CLMPCC_COR4_DSRzd |
    448  1.27.2.2  elad 						  CLMPCC_COR4_CDzd |
    449  1.27.2.2  elad 						  CLMPCC_COR4_CTSzd);
    450  1.27.2.2  elad 
    451  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR5, CLMPCC_COR5_DSRod |
    452  1.27.2.2  elad 						  CLMPCC_COR5_CDod |
    453  1.27.2.2  elad 						  CLMPCC_COR5_CTSod |
    454  1.27.2.2  elad 						  CLMPCC_COR5_FLOW_NORM);
    455  1.27.2.2  elad 
    456  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR6, 0);
    457  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR7, 0);
    458  1.27.2.2  elad 
    459  1.27.2.2  elad 		/* Set the receive FIFO timeout */
    460  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_RTPRl, CLMPCC_RTPR_DEFAULT);
    461  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_RTPRh, 0);
    462  1.27.2.2  elad 
    463  1.27.2.2  elad 		/* At this point, we set up the console differently */
    464  1.27.2.2  elad 		if ( is_console && i == cons_chan ) {
    465  1.27.2.2  elad 			msvr_rts = CLMPCC_MSVR_RTS;
    466  1.27.2.2  elad 			msvr_dtr = CLMPCC_MSVR_DTR;
    467  1.27.2.2  elad 			ccr = CLMPCC_CCR_T0_RX_EN | CLMPCC_CCR_T0_TX_EN;
    468  1.27.2.2  elad 		} else {
    469  1.27.2.2  elad 			msvr_rts = 0;
    470  1.27.2.2  elad 			msvr_dtr = 0;
    471  1.27.2.2  elad 			ccr = CLMPCC_CCR_T0_RX_DIS | CLMPCC_CCR_T0_TX_DIS;
    472  1.27.2.2  elad 		}
    473  1.27.2.2  elad 
    474  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_MSVR_RTS, msvr_rts);
    475  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_MSVR_DTR, msvr_dtr);
    476  1.27.2.2  elad 		clmpcc_channel_cmd(sc, i, CLMPCC_CCR_T0_INIT | ccr);
    477  1.27.2.2  elad 		delay(100);
    478  1.27.2.2  elad 	}
    479  1.27.2.2  elad 
    480  1.27.2.2  elad 	return 0;
    481  1.27.2.2  elad }
    482  1.27.2.2  elad 
    483  1.27.2.2  elad static void
    484  1.27.2.2  elad clmpcc_shutdown(ch)
    485  1.27.2.2  elad 	struct clmpcc_chan *ch;
    486  1.27.2.2  elad {
    487  1.27.2.2  elad 	int oldch;
    488  1.27.2.2  elad 
    489  1.27.2.2  elad 	oldch = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
    490  1.27.2.2  elad 
    491  1.27.2.2  elad 	/* Turn off interrupts. */
    492  1.27.2.2  elad 	clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER, 0);
    493  1.27.2.2  elad 
    494  1.27.2.2  elad 	if ( ISCLR(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
    495  1.27.2.2  elad 		/* Disable the transmitter and receiver */
    496  1.27.2.2  elad 		clmpcc_channel_cmd(ch->ch_sc, ch->ch_car, CLMPCC_CCR_T0_RX_DIS |
    497  1.27.2.2  elad 							  CLMPCC_CCR_T0_TX_DIS);
    498  1.27.2.2  elad 
    499  1.27.2.2  elad 		/* Drop RTS and DTR */
    500  1.27.2.2  elad 		clmpcc_modem_control(ch, TIOCM_RTS | TIOCM_DTR, DMBIS);
    501  1.27.2.2  elad 	}
    502  1.27.2.2  elad 
    503  1.27.2.2  elad 	clmpcc_select_channel(ch->ch_sc, oldch);
    504  1.27.2.2  elad }
    505  1.27.2.2  elad 
    506  1.27.2.2  elad int
    507  1.27.2.2  elad clmpccopen(dev, flag, mode, l)
    508  1.27.2.2  elad 	dev_t dev;
    509  1.27.2.2  elad 	int flag, mode;
    510  1.27.2.2  elad 	struct lwp *l;
    511  1.27.2.2  elad {
    512  1.27.2.2  elad 	struct clmpcc_softc *sc;
    513  1.27.2.2  elad 	struct clmpcc_chan *ch;
    514  1.27.2.2  elad 	struct tty *tp;
    515  1.27.2.2  elad 	int oldch;
    516  1.27.2.2  elad 	int error;
    517  1.27.2.2  elad 
    518  1.27.2.2  elad 	sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
    519  1.27.2.2  elad 	if (sc == NULL)
    520  1.27.2.2  elad 		return (ENXIO);
    521  1.27.2.2  elad 
    522  1.27.2.2  elad 	ch = &sc->sc_chans[CLMPCCCHAN(dev)];
    523  1.27.2.2  elad 
    524  1.27.2.2  elad 	tp = ch->ch_tty;
    525  1.27.2.2  elad 
    526  1.27.2.2  elad 	if ( ISSET(tp->t_state, TS_ISOPEN) &&
    527  1.27.2.2  elad 	     ISSET(tp->t_state, TS_XCLUDE) &&
    528  1.27.2.2  elad 	     generic_authorize(l->l_proc->p_cred,
    529  1.27.2.2  elad 			       KAUTH_GENERIC_ISSUSER,
    530  1.27.2.2  elad 			       &l->l_proc->p_acflag) != 0 )
    531  1.27.2.2  elad 		return EBUSY;
    532  1.27.2.2  elad 
    533  1.27.2.2  elad 	/*
    534  1.27.2.2  elad 	 * Do the following iff this is a first open.
    535  1.27.2.2  elad 	 */
    536  1.27.2.2  elad 	if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
    537  1.27.2.2  elad 
    538  1.27.2.2  elad 		ttychars(tp);
    539  1.27.2.2  elad 
    540  1.27.2.2  elad 		tp->t_dev = dev;
    541  1.27.2.2  elad 		tp->t_iflag = TTYDEF_IFLAG;
    542  1.27.2.2  elad 		tp->t_oflag = TTYDEF_OFLAG;
    543  1.27.2.2  elad 		tp->t_lflag = TTYDEF_LFLAG;
    544  1.27.2.2  elad 		tp->t_cflag = TTYDEF_CFLAG;
    545  1.27.2.2  elad 		tp->t_ospeed = tp->t_ispeed = TTYDEF_SPEED;
    546  1.27.2.2  elad 
    547  1.27.2.2  elad 		if ( ISSET(ch->ch_openflags, TIOCFLAG_CLOCAL) )
    548  1.27.2.2  elad 			SET(tp->t_cflag, CLOCAL);
    549  1.27.2.2  elad 		if ( ISSET(ch->ch_openflags, TIOCFLAG_CRTSCTS) )
    550  1.27.2.2  elad 			SET(tp->t_cflag, CRTSCTS);
    551  1.27.2.2  elad 		if ( ISSET(ch->ch_openflags, TIOCFLAG_MDMBUF) )
    552  1.27.2.2  elad 			SET(tp->t_cflag, MDMBUF);
    553  1.27.2.2  elad 
    554  1.27.2.2  elad 		/*
    555  1.27.2.2  elad 		 * Override some settings if the channel is being
    556  1.27.2.2  elad 		 * used as the console.
    557  1.27.2.2  elad 		 */
    558  1.27.2.2  elad 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
    559  1.27.2.2  elad 			tp->t_ospeed = tp->t_ispeed = cons_rate;
    560  1.27.2.2  elad 			SET(tp->t_cflag, CLOCAL);
    561  1.27.2.2  elad 			CLR(tp->t_cflag, CRTSCTS);
    562  1.27.2.2  elad 			CLR(tp->t_cflag, HUPCL);
    563  1.27.2.2  elad 		}
    564  1.27.2.2  elad 
    565  1.27.2.2  elad 		ch->ch_control = 0;
    566  1.27.2.2  elad 
    567  1.27.2.2  elad 		clmpcc_param(tp, &tp->t_termios);
    568  1.27.2.2  elad 		ttsetwater(tp);
    569  1.27.2.2  elad 
    570  1.27.2.2  elad 		/* Clear the input ring */
    571  1.27.2.2  elad 		ch->ch_ibuf_rd = ch->ch_ibuf_wr = ch->ch_ibuf;
    572  1.27.2.2  elad 
    573  1.27.2.2  elad 		/* Select the channel */
    574  1.27.2.2  elad 		oldch = clmpcc_select_channel(sc, ch->ch_car);
    575  1.27.2.2  elad 
    576  1.27.2.2  elad 		/* Reset it */
    577  1.27.2.2  elad 		clmpcc_channel_cmd(sc, ch->ch_car, CLMPCC_CCR_T0_CLEAR |
    578  1.27.2.2  elad 						   CLMPCC_CCR_T0_RX_EN |
    579  1.27.2.2  elad 						   CLMPCC_CCR_T0_TX_EN);
    580  1.27.2.2  elad 
    581  1.27.2.2  elad 		/* Enable receiver and modem change interrupts. */
    582  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_IER, CLMPCC_IER_MODEM |
    583  1.27.2.2  elad 						 CLMPCC_IER_RET |
    584  1.27.2.2  elad 						 CLMPCC_IER_RX_FIFO);
    585  1.27.2.2  elad 
    586  1.27.2.2  elad 		/* Raise RTS and DTR */
    587  1.27.2.2  elad 		clmpcc_modem_control(ch, TIOCM_RTS | TIOCM_DTR, DMBIS);
    588  1.27.2.2  elad 
    589  1.27.2.2  elad 		clmpcc_select_channel(sc, oldch);
    590  1.27.2.2  elad 	}
    591  1.27.2.2  elad 
    592  1.27.2.2  elad 	error = ttyopen(tp, CLMPCCDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    593  1.27.2.2  elad 	if (error)
    594  1.27.2.2  elad 		goto bad;
    595  1.27.2.2  elad 
    596  1.27.2.2  elad 	error = (*tp->t_linesw->l_open)(dev, tp);
    597  1.27.2.2  elad 	if (error)
    598  1.27.2.2  elad 		goto bad;
    599  1.27.2.2  elad 
    600  1.27.2.2  elad 	return 0;
    601  1.27.2.2  elad 
    602  1.27.2.2  elad bad:
    603  1.27.2.2  elad 	if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
    604  1.27.2.2  elad 		/*
    605  1.27.2.2  elad 		 * We failed to open the device, and nobody else had it opened.
    606  1.27.2.2  elad 		 * Clean up the state as appropriate.
    607  1.27.2.2  elad 		 */
    608  1.27.2.2  elad 		clmpcc_shutdown(ch);
    609  1.27.2.2  elad 	}
    610  1.27.2.2  elad 
    611  1.27.2.2  elad 	return error;
    612  1.27.2.2  elad }
    613  1.27.2.2  elad 
    614  1.27.2.2  elad int
    615  1.27.2.2  elad clmpccclose(dev, flag, mode, l)
    616  1.27.2.2  elad 	dev_t dev;
    617  1.27.2.2  elad 	int flag, mode;
    618  1.27.2.2  elad 	struct lwp *l;
    619  1.27.2.2  elad {
    620  1.27.2.2  elad 	struct clmpcc_softc	*sc =
    621  1.27.2.2  elad 		device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
    622  1.27.2.2  elad 	struct clmpcc_chan	*ch = &sc->sc_chans[CLMPCCCHAN(dev)];
    623  1.27.2.2  elad 	struct tty		*tp = ch->ch_tty;
    624  1.27.2.2  elad 	int s;
    625  1.27.2.2  elad 
    626  1.27.2.2  elad 	if ( ISCLR(tp->t_state, TS_ISOPEN) )
    627  1.27.2.2  elad 		return 0;
    628  1.27.2.2  elad 
    629  1.27.2.2  elad 	(*tp->t_linesw->l_close)(tp, flag);
    630  1.27.2.2  elad 
    631  1.27.2.2  elad 	s = spltty();
    632  1.27.2.2  elad 
    633  1.27.2.2  elad 	if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
    634  1.27.2.2  elad 		/*
    635  1.27.2.2  elad 		 * Although we got a last close, the device may still be in
    636  1.27.2.2  elad 		 * use; e.g. if this was the dialout node, and there are still
    637  1.27.2.2  elad 		 * processes waiting for carrier on the non-dialout node.
    638  1.27.2.2  elad 		 */
    639  1.27.2.2  elad 		clmpcc_shutdown(ch);
    640  1.27.2.2  elad 	}
    641  1.27.2.2  elad 
    642  1.27.2.2  elad 	ttyclose(tp);
    643  1.27.2.2  elad 
    644  1.27.2.2  elad 	splx(s);
    645  1.27.2.2  elad 
    646  1.27.2.2  elad 	return 0;
    647  1.27.2.2  elad }
    648  1.27.2.2  elad 
    649  1.27.2.2  elad int
    650  1.27.2.2  elad clmpccread(dev, uio, flag)
    651  1.27.2.2  elad 	dev_t dev;
    652  1.27.2.2  elad 	struct uio *uio;
    653  1.27.2.2  elad 	int flag;
    654  1.27.2.2  elad {
    655  1.27.2.2  elad 	struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
    656  1.27.2.2  elad 	struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
    657  1.27.2.2  elad 
    658  1.27.2.2  elad 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    659  1.27.2.2  elad }
    660  1.27.2.2  elad 
    661  1.27.2.2  elad int
    662  1.27.2.2  elad clmpccwrite(dev, uio, flag)
    663  1.27.2.2  elad 	dev_t dev;
    664  1.27.2.2  elad 	struct uio *uio;
    665  1.27.2.2  elad 	int flag;
    666  1.27.2.2  elad {
    667  1.27.2.2  elad 	struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
    668  1.27.2.2  elad 	struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
    669  1.27.2.2  elad 
    670  1.27.2.2  elad 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    671  1.27.2.2  elad }
    672  1.27.2.2  elad 
    673  1.27.2.2  elad int
    674  1.27.2.2  elad clmpccpoll(dev, events, l)
    675  1.27.2.2  elad 	dev_t dev;
    676  1.27.2.2  elad 	int events;
    677  1.27.2.2  elad 	struct lwp *l;
    678  1.27.2.2  elad {
    679  1.27.2.2  elad 	struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
    680  1.27.2.2  elad 	struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
    681  1.27.2.2  elad 
    682  1.27.2.2  elad 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    683  1.27.2.2  elad }
    684  1.27.2.2  elad 
    685  1.27.2.2  elad struct tty *
    686  1.27.2.2  elad clmpcctty(dev)
    687  1.27.2.2  elad 	dev_t dev;
    688  1.27.2.2  elad {
    689  1.27.2.2  elad 	struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
    690  1.27.2.2  elad 
    691  1.27.2.2  elad 	return (sc->sc_chans[CLMPCCCHAN(dev)].ch_tty);
    692  1.27.2.2  elad }
    693  1.27.2.2  elad 
    694  1.27.2.2  elad int
    695  1.27.2.2  elad clmpccioctl(dev, cmd, data, flag, l)
    696  1.27.2.2  elad 	dev_t dev;
    697  1.27.2.2  elad 	u_long cmd;
    698  1.27.2.2  elad 	caddr_t data;
    699  1.27.2.2  elad 	int flag;
    700  1.27.2.2  elad 	struct lwp *l;
    701  1.27.2.2  elad {
    702  1.27.2.2  elad 	struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
    703  1.27.2.2  elad 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(dev)];
    704  1.27.2.2  elad 	struct tty *tp = ch->ch_tty;
    705  1.27.2.2  elad 	int error;
    706  1.27.2.2  elad 
    707  1.27.2.2  elad 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    708  1.27.2.2  elad 	if (error != EPASSTHROUGH)
    709  1.27.2.2  elad 		return error;
    710  1.27.2.2  elad 
    711  1.27.2.2  elad 	error = ttioctl(tp, cmd, data, flag, l);
    712  1.27.2.2  elad 	if (error != EPASSTHROUGH)
    713  1.27.2.2  elad 		return error;
    714  1.27.2.2  elad 
    715  1.27.2.2  elad 	error = 0;
    716  1.27.2.2  elad 
    717  1.27.2.2  elad 	switch (cmd) {
    718  1.27.2.2  elad 	case TIOCSBRK:
    719  1.27.2.2  elad 		SET(ch->ch_flags, CLMPCC_FLG_START_BREAK);
    720  1.27.2.2  elad 		clmpcc_enable_transmitter(ch);
    721  1.27.2.2  elad 		break;
    722  1.27.2.2  elad 
    723  1.27.2.2  elad 	case TIOCCBRK:
    724  1.27.2.2  elad 		SET(ch->ch_flags, CLMPCC_FLG_END_BREAK);
    725  1.27.2.2  elad 		clmpcc_enable_transmitter(ch);
    726  1.27.2.2  elad 		break;
    727  1.27.2.2  elad 
    728  1.27.2.2  elad 	case TIOCSDTR:
    729  1.27.2.2  elad 		clmpcc_modem_control(ch, TIOCM_DTR, DMBIS);
    730  1.27.2.2  elad 		break;
    731  1.27.2.2  elad 
    732  1.27.2.2  elad 	case TIOCCDTR:
    733  1.27.2.2  elad 		clmpcc_modem_control(ch, TIOCM_DTR, DMBIC);
    734  1.27.2.2  elad 		break;
    735  1.27.2.2  elad 
    736  1.27.2.2  elad 	case TIOCMSET:
    737  1.27.2.2  elad 		clmpcc_modem_control(ch, *((int *)data), DMSET);
    738  1.27.2.2  elad 		break;
    739  1.27.2.2  elad 
    740  1.27.2.2  elad 	case TIOCMBIS:
    741  1.27.2.2  elad 		clmpcc_modem_control(ch, *((int *)data), DMBIS);
    742  1.27.2.2  elad 		break;
    743  1.27.2.2  elad 
    744  1.27.2.2  elad 	case TIOCMBIC:
    745  1.27.2.2  elad 		clmpcc_modem_control(ch, *((int *)data), DMBIC);
    746  1.27.2.2  elad 		break;
    747  1.27.2.2  elad 
    748  1.27.2.2  elad 	case TIOCMGET:
    749  1.27.2.2  elad 		*((int *)data) = clmpcc_modem_control(ch, 0, DMGET);
    750  1.27.2.2  elad 		break;
    751  1.27.2.2  elad 
    752  1.27.2.2  elad 	case TIOCGFLAGS:
    753  1.27.2.2  elad 		*((int *)data) = ch->ch_openflags;
    754  1.27.2.2  elad 		break;
    755  1.27.2.2  elad 
    756  1.27.2.2  elad 	case TIOCSFLAGS:
    757  1.27.2.2  elad 		error = generic_authorize(l->l_proc->p_cred,
    758  1.27.2.2  elad 					  KAUTH_GENERIC_ISSUSER,
    759  1.27.2.2  elad 					  &l->l_proc->p_acflag);
    760  1.27.2.2  elad 		if ( error )
    761  1.27.2.2  elad 			break;
    762  1.27.2.2  elad 		ch->ch_openflags = *((int *)data) &
    763  1.27.2.2  elad 			(TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    764  1.27.2.2  elad 			 TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    765  1.27.2.2  elad 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) )
    766  1.27.2.2  elad 			SET(ch->ch_openflags, TIOCFLAG_SOFTCAR);
    767  1.27.2.2  elad 		break;
    768  1.27.2.2  elad 
    769  1.27.2.2  elad 	default:
    770  1.27.2.2  elad 		error = EPASSTHROUGH;
    771  1.27.2.2  elad 		break;
    772  1.27.2.2  elad 	}
    773  1.27.2.2  elad 
    774  1.27.2.2  elad 	return error;
    775  1.27.2.2  elad }
    776  1.27.2.2  elad 
    777  1.27.2.2  elad int
    778  1.27.2.2  elad clmpcc_modem_control(ch, bits, howto)
    779  1.27.2.2  elad 	struct clmpcc_chan *ch;
    780  1.27.2.2  elad 	int bits;
    781  1.27.2.2  elad 	int howto;
    782  1.27.2.2  elad {
    783  1.27.2.2  elad 	struct clmpcc_softc *sc = ch->ch_sc;
    784  1.27.2.2  elad 	struct tty *tp = ch->ch_tty;
    785  1.27.2.2  elad 	int oldch;
    786  1.27.2.2  elad 	int msvr;
    787  1.27.2.2  elad 	int rbits = 0;
    788  1.27.2.2  elad 
    789  1.27.2.2  elad 	oldch = clmpcc_select_channel(sc, ch->ch_car);
    790  1.27.2.2  elad 
    791  1.27.2.2  elad 	switch ( howto ) {
    792  1.27.2.2  elad 	case DMGET:
    793  1.27.2.2  elad 		msvr = clmpcc_rd_msvr(sc);
    794  1.27.2.2  elad 
    795  1.27.2.2  elad 		if ( sc->sc_swaprtsdtr ) {
    796  1.27.2.2  elad 			rbits |= (msvr & CLMPCC_MSVR_RTS) ? TIOCM_DTR : 0;
    797  1.27.2.2  elad 			rbits |= (msvr & CLMPCC_MSVR_DTR) ? TIOCM_RTS : 0;
    798  1.27.2.2  elad 		} else {
    799  1.27.2.2  elad 			rbits |= (msvr & CLMPCC_MSVR_RTS) ? TIOCM_RTS : 0;
    800  1.27.2.2  elad 			rbits |= (msvr & CLMPCC_MSVR_DTR) ? TIOCM_DTR : 0;
    801  1.27.2.2  elad 		}
    802  1.27.2.2  elad 
    803  1.27.2.2  elad 		rbits |= (msvr & CLMPCC_MSVR_CTS) ? TIOCM_CTS : 0;
    804  1.27.2.2  elad 		rbits |= (msvr & CLMPCC_MSVR_CD)  ? TIOCM_CD  : 0;
    805  1.27.2.2  elad 		rbits |= (msvr & CLMPCC_MSVR_DSR) ? TIOCM_DSR : 0;
    806  1.27.2.2  elad 		break;
    807  1.27.2.2  elad 
    808  1.27.2.2  elad 	case DMSET:
    809  1.27.2.2  elad 		if ( sc->sc_swaprtsdtr ) {
    810  1.27.2.2  elad 		    if ( ISCLR(tp->t_cflag, CRTSCTS) )
    811  1.27.2.2  elad 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR,
    812  1.27.2.2  elad 					bits & TIOCM_RTS ? CLMPCC_MSVR_DTR : 0);
    813  1.27.2.2  elad 		    clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS,
    814  1.27.2.2  elad 				bits & TIOCM_DTR ? CLMPCC_MSVR_RTS : 0);
    815  1.27.2.2  elad 		} else {
    816  1.27.2.2  elad 		    if ( ISCLR(tp->t_cflag, CRTSCTS) )
    817  1.27.2.2  elad 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS,
    818  1.27.2.2  elad 					bits & TIOCM_RTS ? CLMPCC_MSVR_RTS : 0);
    819  1.27.2.2  elad 		    clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR,
    820  1.27.2.2  elad 				bits & TIOCM_DTR ? CLMPCC_MSVR_DTR : 0);
    821  1.27.2.2  elad 		}
    822  1.27.2.2  elad 		break;
    823  1.27.2.2  elad 
    824  1.27.2.2  elad 	case DMBIS:
    825  1.27.2.2  elad 		if ( sc->sc_swaprtsdtr ) {
    826  1.27.2.2  elad 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISSET(bits, TIOCM_RTS) )
    827  1.27.2.2  elad 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_DTR, CLMPCC_MSVR_DTR);
    828  1.27.2.2  elad 		    if ( ISSET(bits, TIOCM_DTR) )
    829  1.27.2.2  elad 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_RTS, CLMPCC_MSVR_RTS);
    830  1.27.2.2  elad 		} else {
    831  1.27.2.2  elad 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISSET(bits, TIOCM_RTS) )
    832  1.27.2.2  elad 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_RTS, CLMPCC_MSVR_RTS);
    833  1.27.2.2  elad 		    if ( ISSET(bits, TIOCM_DTR) )
    834  1.27.2.2  elad 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_DTR, CLMPCC_MSVR_DTR);
    835  1.27.2.2  elad 		}
    836  1.27.2.2  elad 		break;
    837  1.27.2.2  elad 
    838  1.27.2.2  elad 	case DMBIC:
    839  1.27.2.2  elad 		if ( sc->sc_swaprtsdtr ) {
    840  1.27.2.2  elad 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISCLR(bits, TIOCM_RTS) )
    841  1.27.2.2  elad 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR, 0);
    842  1.27.2.2  elad 		    if ( ISCLR(bits, TIOCM_DTR) )
    843  1.27.2.2  elad 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS, 0);
    844  1.27.2.2  elad 		} else {
    845  1.27.2.2  elad 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISCLR(bits, TIOCM_RTS) )
    846  1.27.2.2  elad 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS, 0);
    847  1.27.2.2  elad 		    if ( ISCLR(bits, TIOCM_DTR) )
    848  1.27.2.2  elad 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR, 0);
    849  1.27.2.2  elad 		}
    850  1.27.2.2  elad 		break;
    851  1.27.2.2  elad 	}
    852  1.27.2.2  elad 
    853  1.27.2.2  elad 	clmpcc_select_channel(sc, oldch);
    854  1.27.2.2  elad 
    855  1.27.2.2  elad 	return rbits;
    856  1.27.2.2  elad }
    857  1.27.2.2  elad 
    858  1.27.2.2  elad static int
    859  1.27.2.2  elad clmpcc_param(tp, t)
    860  1.27.2.2  elad 	struct tty *tp;
    861  1.27.2.2  elad 	struct termios *t;
    862  1.27.2.2  elad {
    863  1.27.2.2  elad 	struct clmpcc_softc *sc =
    864  1.27.2.2  elad 	    device_lookup(&clmpcc_cd, CLMPCCUNIT(tp->t_dev));
    865  1.27.2.2  elad 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
    866  1.27.2.2  elad 	u_char cor;
    867  1.27.2.2  elad 	u_char oldch;
    868  1.27.2.2  elad 	int oclk, obpr;
    869  1.27.2.2  elad 	int iclk, ibpr;
    870  1.27.2.2  elad 	int s;
    871  1.27.2.2  elad 
    872  1.27.2.2  elad 	/* Check requested parameters. */
    873  1.27.2.2  elad 	if ( t->c_ospeed && clmpcc_speed(sc, t->c_ospeed, &oclk, &obpr) < 0 )
    874  1.27.2.2  elad 		return EINVAL;
    875  1.27.2.2  elad 
    876  1.27.2.2  elad 	if ( t->c_ispeed && clmpcc_speed(sc, t->c_ispeed, &iclk, &ibpr) < 0 )
    877  1.27.2.2  elad 		return EINVAL;
    878  1.27.2.2  elad 
    879  1.27.2.2  elad 	/*
    880  1.27.2.2  elad 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    881  1.27.2.2  elad 	 * is always active.
    882  1.27.2.2  elad 	 */
    883  1.27.2.2  elad 	if ( ISSET(ch->ch_openflags, TIOCFLAG_SOFTCAR) ||
    884  1.27.2.2  elad 	     ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
    885  1.27.2.2  elad 		SET(t->c_cflag, CLOCAL);
    886  1.27.2.2  elad 		CLR(t->c_cflag, HUPCL);
    887  1.27.2.2  elad 	}
    888  1.27.2.2  elad 
    889  1.27.2.2  elad 	CLR(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS);
    890  1.27.2.2  elad 
    891  1.27.2.2  elad 	/* If ospeed it zero, hangup the line */
    892  1.27.2.2  elad 	clmpcc_modem_control(ch, TIOCM_DTR, t->c_ospeed == 0 ? DMBIC : DMBIS);
    893  1.27.2.2  elad 
    894  1.27.2.2  elad 	if ( t->c_ospeed ) {
    895  1.27.2.2  elad 		ch->ch_tcor = CLMPCC_TCOR_CLK(oclk);
    896  1.27.2.2  elad 		ch->ch_tbpr = obpr;
    897  1.27.2.2  elad 	} else {
    898  1.27.2.2  elad 		ch->ch_tcor = 0;
    899  1.27.2.2  elad 		ch->ch_tbpr = 0;
    900  1.27.2.2  elad 	}
    901  1.27.2.2  elad 
    902  1.27.2.2  elad 	if ( t->c_ispeed ) {
    903  1.27.2.2  elad 		ch->ch_rcor = CLMPCC_RCOR_CLK(iclk);
    904  1.27.2.2  elad 		ch->ch_rbpr = ibpr;
    905  1.27.2.2  elad 	} else {
    906  1.27.2.2  elad 		ch->ch_rcor = 0;
    907  1.27.2.2  elad 		ch->ch_rbpr = 0;
    908  1.27.2.2  elad 	}
    909  1.27.2.2  elad 
    910  1.27.2.2  elad 	/* Work out value to use for COR1 */
    911  1.27.2.2  elad 	cor = 0;
    912  1.27.2.2  elad 	if ( ISSET(t->c_cflag, PARENB) ) {
    913  1.27.2.2  elad 		cor |= CLMPCC_COR1_NORM_PARITY;
    914  1.27.2.2  elad 		if ( ISSET(t->c_cflag, PARODD) )
    915  1.27.2.2  elad 			cor |= CLMPCC_COR1_ODD_PARITY;
    916  1.27.2.2  elad 	}
    917  1.27.2.2  elad 
    918  1.27.2.2  elad 	if ( ISCLR(t->c_cflag, INPCK) )
    919  1.27.2.2  elad 		cor |= CLMPCC_COR1_IGNORE_PAR;
    920  1.27.2.2  elad 
    921  1.27.2.2  elad 	switch ( t->c_cflag & CSIZE ) {
    922  1.27.2.2  elad 	  case CS5:
    923  1.27.2.2  elad 		cor |= CLMPCC_COR1_CHAR_5BITS;
    924  1.27.2.2  elad 		break;
    925  1.27.2.2  elad 
    926  1.27.2.2  elad 	  case CS6:
    927  1.27.2.2  elad 		cor |= CLMPCC_COR1_CHAR_6BITS;
    928  1.27.2.2  elad 		break;
    929  1.27.2.2  elad 
    930  1.27.2.2  elad 	  case CS7:
    931  1.27.2.2  elad 		cor |= CLMPCC_COR1_CHAR_7BITS;
    932  1.27.2.2  elad 		break;
    933  1.27.2.2  elad 
    934  1.27.2.2  elad 	  case CS8:
    935  1.27.2.2  elad 		cor |= CLMPCC_COR1_CHAR_8BITS;
    936  1.27.2.2  elad 		break;
    937  1.27.2.2  elad 	}
    938  1.27.2.2  elad 
    939  1.27.2.2  elad 	ch->ch_cor1 = cor;
    940  1.27.2.2  elad 
    941  1.27.2.2  elad 	/*
    942  1.27.2.2  elad 	 * The only interesting bit in COR2 is 'CTS Automatic Enable'
    943  1.27.2.2  elad 	 * when hardware flow control is in effect.
    944  1.27.2.2  elad 	 */
    945  1.27.2.2  elad 	ch->ch_cor2 = ISSET(t->c_cflag, CRTSCTS) ? CLMPCC_COR2_CtsAE : 0;
    946  1.27.2.2  elad 
    947  1.27.2.2  elad 	/* COR3 needs to be set to the number of stop bits... */
    948  1.27.2.2  elad 	ch->ch_cor3 = ISSET(t->c_cflag, CSTOPB) ? CLMPCC_COR3_STOP_2 :
    949  1.27.2.2  elad 						  CLMPCC_COR3_STOP_1;
    950  1.27.2.2  elad 
    951  1.27.2.2  elad 	/*
    952  1.27.2.2  elad 	 * COR4 contains the FIFO threshold setting.
    953  1.27.2.2  elad 	 * We adjust the threshold depending on the input speed...
    954  1.27.2.2  elad 	 */
    955  1.27.2.2  elad 	if ( t->c_ispeed <= 1200 )
    956  1.27.2.2  elad 		ch->ch_cor4 = CLMPCC_COR4_FIFO_LOW;
    957  1.27.2.2  elad 	else if ( t->c_ispeed <= 19200 )
    958  1.27.2.2  elad 		ch->ch_cor4 = CLMPCC_COR4_FIFO_MED;
    959  1.27.2.2  elad 	else
    960  1.27.2.2  elad 		ch->ch_cor4 = CLMPCC_COR4_FIFO_HIGH;
    961  1.27.2.2  elad 
    962  1.27.2.2  elad 	/*
    963  1.27.2.2  elad 	 * If chip is used with CTS and DTR swapped, we can enable
    964  1.27.2.2  elad 	 * automatic hardware flow control.
    965  1.27.2.2  elad 	 */
    966  1.27.2.2  elad 	if ( sc->sc_swaprtsdtr && ISSET(t->c_cflag, CRTSCTS) )
    967  1.27.2.2  elad 		ch->ch_cor5 = CLMPCC_COR5_FLOW_NORM;
    968  1.27.2.2  elad 	else
    969  1.27.2.2  elad 		ch->ch_cor5 = 0;
    970  1.27.2.2  elad 
    971  1.27.2.2  elad 	s = splserial();
    972  1.27.2.2  elad 	oldch = clmpcc_select_channel(sc, ch->ch_car);
    973  1.27.2.2  elad 
    974  1.27.2.2  elad 	/*
    975  1.27.2.2  elad 	 * COR2 needs to be set immediately otherwise we might never get
    976  1.27.2.2  elad 	 * a Tx EMPTY interrupt to change the other parameters.
    977  1.27.2.2  elad 	 */
    978  1.27.2.2  elad 	if ( clmpcc_rdreg(sc, CLMPCC_REG_COR2) != ch->ch_cor2 )
    979  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR2, ch->ch_cor2);
    980  1.27.2.2  elad 
    981  1.27.2.2  elad 	if ( ISCLR(ch->ch_tty->t_state, TS_BUSY) )
    982  1.27.2.2  elad 		clmpcc_set_params(ch);
    983  1.27.2.2  elad 	else
    984  1.27.2.2  elad 		SET(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS);
    985  1.27.2.2  elad 
    986  1.27.2.2  elad 	clmpcc_select_channel(sc, oldch);
    987  1.27.2.2  elad 
    988  1.27.2.2  elad 	splx(s);
    989  1.27.2.2  elad 
    990  1.27.2.2  elad 	return 0;
    991  1.27.2.2  elad }
    992  1.27.2.2  elad 
    993  1.27.2.2  elad static void
    994  1.27.2.2  elad clmpcc_set_params(ch)
    995  1.27.2.2  elad 	struct clmpcc_chan *ch;
    996  1.27.2.2  elad {
    997  1.27.2.2  elad 	struct clmpcc_softc *sc = ch->ch_sc;
    998  1.27.2.2  elad 	u_char r1;
    999  1.27.2.2  elad 	u_char r2;
   1000  1.27.2.2  elad 
   1001  1.27.2.2  elad 	if ( ch->ch_tcor || ch->ch_tbpr ) {
   1002  1.27.2.2  elad 		r1 = clmpcc_rdreg(sc, CLMPCC_REG_TCOR);
   1003  1.27.2.2  elad 		r2 = clmpcc_rdreg(sc, CLMPCC_REG_TBPR);
   1004  1.27.2.2  elad 		/* Only write Tx rate if it really has changed */
   1005  1.27.2.2  elad 		if ( ch->ch_tcor != r1 || ch->ch_tbpr != r2 ) {
   1006  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_TCOR, ch->ch_tcor);
   1007  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_TBPR, ch->ch_tbpr);
   1008  1.27.2.2  elad 		}
   1009  1.27.2.2  elad 	}
   1010  1.27.2.2  elad 
   1011  1.27.2.2  elad 	if ( ch->ch_rcor || ch->ch_rbpr ) {
   1012  1.27.2.2  elad 		r1 = clmpcc_rdreg(sc, CLMPCC_REG_RCOR);
   1013  1.27.2.2  elad 		r2 = clmpcc_rdreg(sc, CLMPCC_REG_RBPR);
   1014  1.27.2.2  elad 		/* Only write Rx rate if it really has changed */
   1015  1.27.2.2  elad 		if ( ch->ch_rcor != r1 || ch->ch_rbpr != r2 ) {
   1016  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_RCOR, ch->ch_rcor);
   1017  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_RBPR, ch->ch_rbpr);
   1018  1.27.2.2  elad 		}
   1019  1.27.2.2  elad 	}
   1020  1.27.2.2  elad 
   1021  1.27.2.2  elad 	if ( clmpcc_rdreg(sc, CLMPCC_REG_COR1) != ch->ch_cor1 ) {
   1022  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR1, ch->ch_cor1);
   1023  1.27.2.2  elad 		/* Any change to COR1 requires an INIT command */
   1024  1.27.2.2  elad 		SET(ch->ch_flags, CLMPCC_FLG_NEED_INIT);
   1025  1.27.2.2  elad 	}
   1026  1.27.2.2  elad 
   1027  1.27.2.2  elad 	if ( clmpcc_rdreg(sc, CLMPCC_REG_COR3) != ch->ch_cor3 )
   1028  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR3, ch->ch_cor3);
   1029  1.27.2.2  elad 
   1030  1.27.2.2  elad 	r1 = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
   1031  1.27.2.2  elad 	if ( ch->ch_cor4 != (r1 & CLMPCC_COR4_FIFO_MASK) ) {
   1032  1.27.2.2  elad 		/*
   1033  1.27.2.2  elad 		 * Note: If the FIFO has changed, we always set it to
   1034  1.27.2.2  elad 		 * zero here and disable the Receive Timeout interrupt.
   1035  1.27.2.2  elad 		 * It's up to the Rx Interrupt handler to pick the
   1036  1.27.2.2  elad 		 * appropriate moment to write the new FIFO length.
   1037  1.27.2.2  elad 		 */
   1038  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR4, r1 & ~CLMPCC_COR4_FIFO_MASK);
   1039  1.27.2.2  elad 		r1 = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1040  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_IER, r1 & ~CLMPCC_IER_RET);
   1041  1.27.2.2  elad 		SET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
   1042  1.27.2.2  elad 	}
   1043  1.27.2.2  elad 
   1044  1.27.2.2  elad 	r1 = clmpcc_rdreg(sc, CLMPCC_REG_COR5);
   1045  1.27.2.2  elad 	if ( ch->ch_cor5 != (r1 & CLMPCC_COR5_FLOW_MASK) ) {
   1046  1.27.2.2  elad 		r1 &= ~CLMPCC_COR5_FLOW_MASK;
   1047  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR5, r1 | ch->ch_cor5);
   1048  1.27.2.2  elad 	}
   1049  1.27.2.2  elad }
   1050  1.27.2.2  elad 
   1051  1.27.2.2  elad static void
   1052  1.27.2.2  elad clmpcc_start(tp)
   1053  1.27.2.2  elad 	struct tty *tp;
   1054  1.27.2.2  elad {
   1055  1.27.2.2  elad 	struct clmpcc_softc *sc =
   1056  1.27.2.2  elad 	    device_lookup(&clmpcc_cd, CLMPCCUNIT(tp->t_dev));
   1057  1.27.2.2  elad 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
   1058  1.27.2.2  elad 	u_int oldch;
   1059  1.27.2.2  elad 	int s;
   1060  1.27.2.2  elad 
   1061  1.27.2.2  elad 	s = spltty();
   1062  1.27.2.2  elad 
   1063  1.27.2.2  elad 	if ( ISCLR(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY) ) {
   1064  1.27.2.2  elad 		if ( tp->t_outq.c_cc <= tp->t_lowat ) {
   1065  1.27.2.2  elad 			if ( ISSET(tp->t_state, TS_ASLEEP) ) {
   1066  1.27.2.2  elad 				CLR(tp->t_state, TS_ASLEEP);
   1067  1.27.2.2  elad 				wakeup(&tp->t_outq);
   1068  1.27.2.2  elad 			}
   1069  1.27.2.2  elad 			selwakeup(&tp->t_wsel);
   1070  1.27.2.2  elad 		}
   1071  1.27.2.2  elad 
   1072  1.27.2.2  elad 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_START_BREAK |
   1073  1.27.2.2  elad 					 CLMPCC_FLG_END_BREAK) ||
   1074  1.27.2.2  elad 		     tp->t_outq.c_cc > 0 ) {
   1075  1.27.2.2  elad 
   1076  1.27.2.2  elad 			if ( ISCLR(ch->ch_flags, CLMPCC_FLG_START_BREAK |
   1077  1.27.2.2  elad 						 CLMPCC_FLG_END_BREAK) ) {
   1078  1.27.2.2  elad 				ch->ch_obuf_addr = tp->t_outq.c_cf;
   1079  1.27.2.2  elad 				ch->ch_obuf_size = ndqb(&tp->t_outq, 0);
   1080  1.27.2.2  elad 			}
   1081  1.27.2.2  elad 
   1082  1.27.2.2  elad 			/* Enable TX empty interrupts */
   1083  1.27.2.2  elad 			oldch = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
   1084  1.27.2.2  elad 			clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER,
   1085  1.27.2.2  elad 				clmpcc_rdreg(ch->ch_sc, CLMPCC_REG_IER) |
   1086  1.27.2.2  elad 					     CLMPCC_IER_TX_EMPTY);
   1087  1.27.2.2  elad 			clmpcc_select_channel(ch->ch_sc, oldch);
   1088  1.27.2.2  elad 			SET(tp->t_state, TS_BUSY);
   1089  1.27.2.2  elad 		}
   1090  1.27.2.2  elad 	}
   1091  1.27.2.2  elad 
   1092  1.27.2.2  elad 	splx(s);
   1093  1.27.2.2  elad }
   1094  1.27.2.2  elad 
   1095  1.27.2.2  elad /*
   1096  1.27.2.2  elad  * Stop output on a line.
   1097  1.27.2.2  elad  */
   1098  1.27.2.2  elad void
   1099  1.27.2.2  elad clmpccstop(tp, flag)
   1100  1.27.2.2  elad 	struct tty *tp;
   1101  1.27.2.2  elad 	int flag;
   1102  1.27.2.2  elad {
   1103  1.27.2.2  elad 	struct clmpcc_softc *sc =
   1104  1.27.2.2  elad 	    device_lookup(&clmpcc_cd, CLMPCCUNIT(tp->t_dev));
   1105  1.27.2.2  elad 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
   1106  1.27.2.2  elad 	int s;
   1107  1.27.2.2  elad 
   1108  1.27.2.2  elad 	s = splserial();
   1109  1.27.2.2  elad 
   1110  1.27.2.2  elad 	if ( ISSET(tp->t_state, TS_BUSY) ) {
   1111  1.27.2.2  elad 		if ( ISCLR(tp->t_state, TS_TTSTOP) )
   1112  1.27.2.2  elad 			SET(tp->t_state, TS_FLUSH);
   1113  1.27.2.2  elad 		ch->ch_obuf_size = 0;
   1114  1.27.2.2  elad 	}
   1115  1.27.2.2  elad 	splx(s);
   1116  1.27.2.2  elad }
   1117  1.27.2.2  elad 
   1118  1.27.2.2  elad /*
   1119  1.27.2.2  elad  * RX interrupt routine
   1120  1.27.2.2  elad  */
   1121  1.27.2.2  elad int
   1122  1.27.2.2  elad clmpcc_rxintr(arg)
   1123  1.27.2.2  elad 	void *arg;
   1124  1.27.2.2  elad {
   1125  1.27.2.2  elad 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1126  1.27.2.2  elad 	struct clmpcc_chan *ch;
   1127  1.27.2.2  elad 	u_int8_t *put, *end, rxd;
   1128  1.27.2.2  elad 	u_char errstat;
   1129  1.27.2.2  elad 	u_char fc, tc;
   1130  1.27.2.2  elad 	u_char risr;
   1131  1.27.2.2  elad 	u_char rir;
   1132  1.27.2.2  elad #ifdef DDB
   1133  1.27.2.2  elad 	int saw_break = 0;
   1134  1.27.2.2  elad #endif
   1135  1.27.2.2  elad 
   1136  1.27.2.2  elad 	/* Receive interrupt active? */
   1137  1.27.2.2  elad 	rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
   1138  1.27.2.2  elad 
   1139  1.27.2.2  elad 	/*
   1140  1.27.2.2  elad 	 * If we're using auto-vectored interrupts, we have to
   1141  1.27.2.2  elad 	 * verify if the chip is generating the interrupt.
   1142  1.27.2.2  elad 	 */
   1143  1.27.2.2  elad 	if ( sc->sc_vector_base == 0 && (rir & CLMPCC_RIR_RACT) == 0 )
   1144  1.27.2.2  elad 		return 0;
   1145  1.27.2.2  elad 
   1146  1.27.2.2  elad 	/* Get pointer to interrupting channel's data structure */
   1147  1.27.2.2  elad 	ch = &sc->sc_chans[rir & CLMPCC_RIR_RCN_MASK];
   1148  1.27.2.2  elad 
   1149  1.27.2.2  elad 	/* Get the interrupt status register */
   1150  1.27.2.2  elad 	risr = clmpcc_rdreg(sc, CLMPCC_REG_RISRl);
   1151  1.27.2.2  elad 	if ( risr & CLMPCC_RISR_TIMEOUT ) {
   1152  1.27.2.2  elad 		u_char reg;
   1153  1.27.2.2  elad 		/*
   1154  1.27.2.2  elad 		 * Set the FIFO threshold to zero, and disable
   1155  1.27.2.2  elad 		 * further receive timeout interrupts.
   1156  1.27.2.2  elad 		 */
   1157  1.27.2.2  elad 		reg = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
   1158  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_COR4, reg & ~CLMPCC_COR4_FIFO_MASK);
   1159  1.27.2.2  elad 		reg = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1160  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_IER, reg & ~CLMPCC_IER_RET);
   1161  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_REOIR, CLMPCC_REOIR_NO_TRANS);
   1162  1.27.2.2  elad 		SET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
   1163  1.27.2.2  elad 		return 1;
   1164  1.27.2.2  elad 	}
   1165  1.27.2.2  elad 
   1166  1.27.2.2  elad 	/* How many bytes are waiting in the FIFO?  */
   1167  1.27.2.2  elad 	fc = tc = clmpcc_rdreg(sc, CLMPCC_REG_RFOC) & CLMPCC_RFOC_MASK;
   1168  1.27.2.2  elad 
   1169  1.27.2.2  elad #ifdef DDB
   1170  1.27.2.2  elad 	/*
   1171  1.27.2.2  elad 	 * Allow BREAK on the console to drop to the debugger.
   1172  1.27.2.2  elad 	 */
   1173  1.27.2.2  elad 	if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) &&
   1174  1.27.2.2  elad 	     risr & CLMPCC_RISR_BREAK ) {
   1175  1.27.2.2  elad 		saw_break = 1;
   1176  1.27.2.2  elad 	}
   1177  1.27.2.2  elad #endif
   1178  1.27.2.2  elad 
   1179  1.27.2.2  elad 	if ( ISCLR(ch->ch_tty->t_state, TS_ISOPEN) && fc ) {
   1180  1.27.2.2  elad 		/* Just get rid of the data */
   1181  1.27.2.2  elad 		while ( fc-- )
   1182  1.27.2.2  elad 			(void) clmpcc_rd_rxdata(sc);
   1183  1.27.2.2  elad 		goto rx_done;
   1184  1.27.2.2  elad 	}
   1185  1.27.2.2  elad 
   1186  1.27.2.2  elad 	put = ch->ch_ibuf_wr;
   1187  1.27.2.2  elad 	end = ch->ch_ibuf_end;
   1188  1.27.2.2  elad 
   1189  1.27.2.2  elad 	/*
   1190  1.27.2.2  elad 	 * Note: The chip is completely hosed WRT these error
   1191  1.27.2.2  elad 	 *       conditions; there seems to be no way to associate
   1192  1.27.2.2  elad 	 *       the error with the correct character in the FIFO.
   1193  1.27.2.2  elad 	 *       We compromise by tagging the first character we read
   1194  1.27.2.2  elad 	 *       with the error. Not perfect, but there's no other way.
   1195  1.27.2.2  elad 	 */
   1196  1.27.2.2  elad 	errstat = 0;
   1197  1.27.2.2  elad 	if ( risr & CLMPCC_RISR_PARITY )
   1198  1.27.2.2  elad 		errstat |= TTY_PE;
   1199  1.27.2.2  elad 	if ( risr & (CLMPCC_RISR_FRAMING | CLMPCC_RISR_BREAK) )
   1200  1.27.2.2  elad 		errstat |= TTY_FE;
   1201  1.27.2.2  elad 
   1202  1.27.2.2  elad 	/*
   1203  1.27.2.2  elad 	 * As long as there are characters in the FIFO, and we
   1204  1.27.2.2  elad 	 * have space for them...
   1205  1.27.2.2  elad 	 */
   1206  1.27.2.2  elad 	while ( fc > 0 ) {
   1207  1.27.2.2  elad 
   1208  1.27.2.2  elad 		*put++ = rxd = clmpcc_rd_rxdata(sc);
   1209  1.27.2.2  elad 		*put++ = errstat;
   1210  1.27.2.2  elad 
   1211  1.27.2.2  elad 		if ( put >= end )
   1212  1.27.2.2  elad 			put = ch->ch_ibuf;
   1213  1.27.2.2  elad 
   1214  1.27.2.2  elad 		if ( put == ch->ch_ibuf_rd ) {
   1215  1.27.2.2  elad 			put -= 2;
   1216  1.27.2.2  elad 			if ( put < ch->ch_ibuf )
   1217  1.27.2.2  elad 				put = end - 2;
   1218  1.27.2.2  elad 		}
   1219  1.27.2.2  elad 
   1220  1.27.2.2  elad 		errstat = 0;
   1221  1.27.2.2  elad 		fc--;
   1222  1.27.2.2  elad 	}
   1223  1.27.2.2  elad 
   1224  1.27.2.2  elad 	ch->ch_ibuf_wr = put;
   1225  1.27.2.2  elad 
   1226  1.27.2.2  elad #if 0
   1227  1.27.2.2  elad 	if ( sc->sc_swaprtsdtr == 0 &&
   1228  1.27.2.2  elad 	     ISSET(cy->cy_tty->t_cflag, CRTSCTS) && cc < ch->ch_r_hiwat) {
   1229  1.27.2.2  elad 		/*
   1230  1.27.2.2  elad 		 * If RTS/DTR are not physically swapped, we have to
   1231  1.27.2.2  elad 		 * do hardware flow control manually
   1232  1.27.2.2  elad 		 */
   1233  1.27.2.2  elad 		clmpcc_wr_msvr(sc, CLMPCC_MSVR_RTS, 0);
   1234  1.27.2.2  elad 	}
   1235  1.27.2.2  elad #endif
   1236  1.27.2.2  elad 
   1237  1.27.2.2  elad rx_done:
   1238  1.27.2.2  elad 	if ( fc != tc ) {
   1239  1.27.2.2  elad 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR) ) {
   1240  1.27.2.2  elad 			u_char reg;
   1241  1.27.2.2  elad 			/*
   1242  1.27.2.2  elad 			 * Set the FIFO threshold to the preset value,
   1243  1.27.2.2  elad 			 * and enable receive timeout interrupts.
   1244  1.27.2.2  elad 			 */
   1245  1.27.2.2  elad 			reg = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
   1246  1.27.2.2  elad 			reg = (reg & ~CLMPCC_COR4_FIFO_MASK) | ch->ch_cor4;
   1247  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_COR4, reg);
   1248  1.27.2.2  elad 			reg = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1249  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_IER, reg | CLMPCC_IER_RET);
   1250  1.27.2.2  elad 			CLR(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
   1251  1.27.2.2  elad 		}
   1252  1.27.2.2  elad 
   1253  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_REOIR, 0);
   1254  1.27.2.2  elad #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1255  1.27.2.2  elad 		if ( sc->sc_soft_running == 0 ) {
   1256  1.27.2.2  elad 			sc->sc_soft_running = 1;
   1257  1.27.2.2  elad 			(sc->sc_softhook)(sc);
   1258  1.27.2.2  elad 		}
   1259  1.27.2.2  elad #else
   1260  1.27.2.2  elad 		softintr_schedule(sc->sc_softintr_cookie);
   1261  1.27.2.2  elad #endif
   1262  1.27.2.2  elad 	} else
   1263  1.27.2.2  elad 		clmpcc_wrreg(sc, CLMPCC_REG_REOIR, CLMPCC_REOIR_NO_TRANS);
   1264  1.27.2.2  elad 
   1265  1.27.2.2  elad #ifdef DDB
   1266  1.27.2.2  elad 	/*
   1267  1.27.2.2  elad 	 * Only =after= we write REOIR is it safe to drop to the debugger.
   1268  1.27.2.2  elad 	 */
   1269  1.27.2.2  elad 	if ( saw_break )
   1270  1.27.2.2  elad 		Debugger();
   1271  1.27.2.2  elad #endif
   1272  1.27.2.2  elad 
   1273  1.27.2.2  elad 	return 1;
   1274  1.27.2.2  elad }
   1275  1.27.2.2  elad 
   1276  1.27.2.2  elad /*
   1277  1.27.2.2  elad  * Tx interrupt routine
   1278  1.27.2.2  elad  */
   1279  1.27.2.2  elad int
   1280  1.27.2.2  elad clmpcc_txintr(arg)
   1281  1.27.2.2  elad 	void *arg;
   1282  1.27.2.2  elad {
   1283  1.27.2.2  elad 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1284  1.27.2.2  elad 	struct clmpcc_chan *ch;
   1285  1.27.2.2  elad 	struct tty *tp;
   1286  1.27.2.2  elad 	u_char ftc, oftc;
   1287  1.27.2.2  elad 	u_char tir, teoir;
   1288  1.27.2.2  elad 	int etcmode = 0;
   1289  1.27.2.2  elad 
   1290  1.27.2.2  elad 	/* Tx interrupt active? */
   1291  1.27.2.2  elad 	tir = clmpcc_rdreg(sc, CLMPCC_REG_TIR);
   1292  1.27.2.2  elad 
   1293  1.27.2.2  elad 	/*
   1294  1.27.2.2  elad 	 * If we're using auto-vectored interrupts, we have to
   1295  1.27.2.2  elad 	 * verify if the chip is generating the interrupt.
   1296  1.27.2.2  elad 	 */
   1297  1.27.2.2  elad 	if ( sc->sc_vector_base == 0 && (tir & CLMPCC_TIR_TACT) == 0 )
   1298  1.27.2.2  elad 		return 0;
   1299  1.27.2.2  elad 
   1300  1.27.2.2  elad 	/* Get pointer to interrupting channel's data structure */
   1301  1.27.2.2  elad 	ch = &sc->sc_chans[tir & CLMPCC_TIR_TCN_MASK];
   1302  1.27.2.2  elad 	tp = ch->ch_tty;
   1303  1.27.2.2  elad 
   1304  1.27.2.2  elad 	/* Dummy read of the interrupt status register */
   1305  1.27.2.2  elad 	(void) clmpcc_rdreg(sc, CLMPCC_REG_TISR);
   1306  1.27.2.2  elad 
   1307  1.27.2.2  elad 	/* Make sure embedded transmit commands are disabled */
   1308  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_COR2, ch->ch_cor2);
   1309  1.27.2.2  elad 
   1310  1.27.2.2  elad 	ftc = oftc = clmpcc_rdreg(sc, CLMPCC_REG_TFTC);
   1311  1.27.2.2  elad 
   1312  1.27.2.2  elad 	/* Handle a delayed parameter change */
   1313  1.27.2.2  elad 	if ( ISSET(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS) ) {
   1314  1.27.2.2  elad 		CLR(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS);
   1315  1.27.2.2  elad 		clmpcc_set_params(ch);
   1316  1.27.2.2  elad 	}
   1317  1.27.2.2  elad 
   1318  1.27.2.2  elad 	if ( ch->ch_obuf_size > 0 ) {
   1319  1.27.2.2  elad 		u_int n = min(ch->ch_obuf_size, ftc);
   1320  1.27.2.2  elad 
   1321  1.27.2.2  elad 		clmpcc_wrtx_multi(sc, ch->ch_obuf_addr, n);
   1322  1.27.2.2  elad 
   1323  1.27.2.2  elad 		ftc -= n;
   1324  1.27.2.2  elad 		ch->ch_obuf_size -= n;
   1325  1.27.2.2  elad 		ch->ch_obuf_addr += n;
   1326  1.27.2.2  elad 
   1327  1.27.2.2  elad 	} else {
   1328  1.27.2.2  elad 		/*
   1329  1.27.2.2  elad 		 * Check if we should start/stop a break
   1330  1.27.2.2  elad 		 */
   1331  1.27.2.2  elad 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_START_BREAK) ) {
   1332  1.27.2.2  elad 			CLR(ch->ch_flags, CLMPCC_FLG_START_BREAK);
   1333  1.27.2.2  elad 			/* Enable embedded transmit commands */
   1334  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_COR2,
   1335  1.27.2.2  elad 					ch->ch_cor2 | CLMPCC_COR2_ETC);
   1336  1.27.2.2  elad 			clmpcc_wr_txdata(sc, CLMPCC_ETC_MAGIC);
   1337  1.27.2.2  elad 			clmpcc_wr_txdata(sc, CLMPCC_ETC_SEND_BREAK);
   1338  1.27.2.2  elad 			ftc -= 2;
   1339  1.27.2.2  elad 			etcmode = 1;
   1340  1.27.2.2  elad 		}
   1341  1.27.2.2  elad 
   1342  1.27.2.2  elad 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_END_BREAK) ) {
   1343  1.27.2.2  elad 			CLR(ch->ch_flags, CLMPCC_FLG_END_BREAK);
   1344  1.27.2.2  elad 			/* Enable embedded transmit commands */
   1345  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_COR2,
   1346  1.27.2.2  elad 					ch->ch_cor2 | CLMPCC_COR2_ETC);
   1347  1.27.2.2  elad 			clmpcc_wr_txdata(sc, CLMPCC_ETC_MAGIC);
   1348  1.27.2.2  elad 			clmpcc_wr_txdata(sc, CLMPCC_ETC_STOP_BREAK);
   1349  1.27.2.2  elad 			ftc -= 2;
   1350  1.27.2.2  elad 			etcmode = 1;
   1351  1.27.2.2  elad 		}
   1352  1.27.2.2  elad 	}
   1353  1.27.2.2  elad 
   1354  1.27.2.2  elad 	tir = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1355  1.27.2.2  elad 
   1356  1.27.2.2  elad 	if ( ftc != oftc ) {
   1357  1.27.2.2  elad 		/*
   1358  1.27.2.2  elad 		 * Enable/disable the Tx FIFO threshold interrupt
   1359  1.27.2.2  elad 		 * according to how much data is in the FIFO.
   1360  1.27.2.2  elad 		 * However, always disable the FIFO threshold if
   1361  1.27.2.2  elad 		 * we've left the channel in 'Embedded Transmit
   1362  1.27.2.2  elad 		 * Command' mode.
   1363  1.27.2.2  elad 		 */
   1364  1.27.2.2  elad 		if ( etcmode || ftc >= ch->ch_cor4 )
   1365  1.27.2.2  elad 			tir &= ~CLMPCC_IER_TX_FIFO;
   1366  1.27.2.2  elad 		else
   1367  1.27.2.2  elad 			tir |= CLMPCC_IER_TX_FIFO;
   1368  1.27.2.2  elad 		teoir = 0;
   1369  1.27.2.2  elad 	} else {
   1370  1.27.2.2  elad 		/*
   1371  1.27.2.2  elad 		 * No data was sent.
   1372  1.27.2.2  elad 		 * Disable transmit interrupt.
   1373  1.27.2.2  elad 		 */
   1374  1.27.2.2  elad 		tir &= ~(CLMPCC_IER_TX_EMPTY|CLMPCC_IER_TX_FIFO);
   1375  1.27.2.2  elad 		teoir = CLMPCC_TEOIR_NO_TRANS;
   1376  1.27.2.2  elad 
   1377  1.27.2.2  elad 		/*
   1378  1.27.2.2  elad 		 * Request Tx processing in the soft interrupt handler
   1379  1.27.2.2  elad 		 */
   1380  1.27.2.2  elad 		ch->ch_tx_done = 1;
   1381  1.27.2.2  elad #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1382  1.27.2.2  elad 		if ( sc->sc_soft_running == 0 ) {
   1383  1.27.2.2  elad 			sc->sc_soft_running = 1;
   1384  1.27.2.2  elad 			(sc->sc_softhook)(sc);
   1385  1.27.2.2  elad 		}
   1386  1.27.2.2  elad #else
   1387  1.27.2.2  elad 		softintr_schedule(sc->sc_softintr_cookie);
   1388  1.27.2.2  elad #endif
   1389  1.27.2.2  elad 	}
   1390  1.27.2.2  elad 
   1391  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_IER, tir);
   1392  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_TEOIR, teoir);
   1393  1.27.2.2  elad 
   1394  1.27.2.2  elad 	return 1;
   1395  1.27.2.2  elad }
   1396  1.27.2.2  elad 
   1397  1.27.2.2  elad /*
   1398  1.27.2.2  elad  * Modem change interrupt routine
   1399  1.27.2.2  elad  */
   1400  1.27.2.2  elad int
   1401  1.27.2.2  elad clmpcc_mdintr(arg)
   1402  1.27.2.2  elad 	void *arg;
   1403  1.27.2.2  elad {
   1404  1.27.2.2  elad 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1405  1.27.2.2  elad 	u_char mir;
   1406  1.27.2.2  elad 
   1407  1.27.2.2  elad 	/* Modem status interrupt active? */
   1408  1.27.2.2  elad 	mir = clmpcc_rdreg(sc, CLMPCC_REG_MIR);
   1409  1.27.2.2  elad 
   1410  1.27.2.2  elad 	/*
   1411  1.27.2.2  elad 	 * If we're using auto-vectored interrupts, we have to
   1412  1.27.2.2  elad 	 * verify if the chip is generating the interrupt.
   1413  1.27.2.2  elad 	 */
   1414  1.27.2.2  elad 	if ( sc->sc_vector_base == 0 && (mir & CLMPCC_MIR_MACT) == 0 )
   1415  1.27.2.2  elad 		return 0;
   1416  1.27.2.2  elad 
   1417  1.27.2.2  elad 	/* Dummy read of the interrupt status register */
   1418  1.27.2.2  elad 	(void) clmpcc_rdreg(sc, CLMPCC_REG_MISR);
   1419  1.27.2.2  elad 
   1420  1.27.2.2  elad 	/* Retrieve current status of modem lines. */
   1421  1.27.2.2  elad 	sc->sc_chans[mir & CLMPCC_MIR_MCN_MASK].ch_control |=
   1422  1.27.2.2  elad 		clmpcc_rd_msvr(sc) & CLMPCC_MSVR_CD;
   1423  1.27.2.2  elad 
   1424  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_MEOIR, 0);
   1425  1.27.2.2  elad 
   1426  1.27.2.2  elad #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1427  1.27.2.2  elad 	if ( sc->sc_soft_running == 0 ) {
   1428  1.27.2.2  elad 		sc->sc_soft_running = 1;
   1429  1.27.2.2  elad 		(sc->sc_softhook)(sc);
   1430  1.27.2.2  elad 	}
   1431  1.27.2.2  elad #else
   1432  1.27.2.2  elad 	softintr_schedule(sc->sc_softintr_cookie);
   1433  1.27.2.2  elad #endif
   1434  1.27.2.2  elad 
   1435  1.27.2.2  elad 	return 1;
   1436  1.27.2.2  elad }
   1437  1.27.2.2  elad 
   1438  1.27.2.2  elad void
   1439  1.27.2.2  elad clmpcc_softintr(arg)
   1440  1.27.2.2  elad 	void *arg;
   1441  1.27.2.2  elad {
   1442  1.27.2.2  elad 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1443  1.27.2.2  elad 	struct clmpcc_chan *ch;
   1444  1.27.2.2  elad 	struct tty *tp;
   1445  1.27.2.2  elad 	int (*rint)(int, struct tty *);
   1446  1.27.2.2  elad 	u_char *get;
   1447  1.27.2.2  elad 	u_char reg;
   1448  1.27.2.2  elad 	u_int c;
   1449  1.27.2.2  elad 	int chan;
   1450  1.27.2.2  elad 
   1451  1.27.2.2  elad #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1452  1.27.2.2  elad 	sc->sc_soft_running = 0;
   1453  1.27.2.2  elad #endif
   1454  1.27.2.2  elad 
   1455  1.27.2.2  elad 	/* Handle Modem state changes too... */
   1456  1.27.2.2  elad 
   1457  1.27.2.2  elad 	for (chan = 0; chan < CLMPCC_NUM_CHANS; chan++) {
   1458  1.27.2.2  elad 		ch = &sc->sc_chans[chan];
   1459  1.27.2.2  elad 		tp = ch->ch_tty;
   1460  1.27.2.2  elad 
   1461  1.27.2.2  elad 		get = ch->ch_ibuf_rd;
   1462  1.27.2.2  elad 		rint = tp->t_linesw->l_rint;
   1463  1.27.2.2  elad 
   1464  1.27.2.2  elad 		/* Squirt buffered incoming data into the tty layer */
   1465  1.27.2.2  elad 		while ( get != ch->ch_ibuf_wr ) {
   1466  1.27.2.2  elad 			c = get[0];
   1467  1.27.2.2  elad 			c |= ((u_int)get[1]) << 8;
   1468  1.27.2.2  elad 			if ( (rint)(c, tp) == -1 ) {
   1469  1.27.2.2  elad 				ch->ch_ibuf_rd = ch->ch_ibuf_wr;
   1470  1.27.2.2  elad 				break;
   1471  1.27.2.2  elad 			}
   1472  1.27.2.2  elad 
   1473  1.27.2.2  elad 			get += 2;
   1474  1.27.2.2  elad 			if ( get == ch->ch_ibuf_end )
   1475  1.27.2.2  elad 				get = ch->ch_ibuf;
   1476  1.27.2.2  elad 
   1477  1.27.2.2  elad 			ch->ch_ibuf_rd = get;
   1478  1.27.2.2  elad 		}
   1479  1.27.2.2  elad 
   1480  1.27.2.2  elad 		/*
   1481  1.27.2.2  elad 		 * Is the transmitter idle and in need of attention?
   1482  1.27.2.2  elad 		 */
   1483  1.27.2.2  elad 		if ( ch->ch_tx_done ) {
   1484  1.27.2.2  elad 			ch->ch_tx_done = 0;
   1485  1.27.2.2  elad 
   1486  1.27.2.2  elad 			if ( ISSET(ch->ch_flags, CLMPCC_FLG_NEED_INIT) ) {
   1487  1.27.2.2  elad 				clmpcc_channel_cmd(sc, ch->ch_car,
   1488  1.27.2.2  elad 						       CLMPCC_CCR_T0_INIT  |
   1489  1.27.2.2  elad 						       CLMPCC_CCR_T0_RX_EN |
   1490  1.27.2.2  elad 					   	       CLMPCC_CCR_T0_TX_EN);
   1491  1.27.2.2  elad 				CLR(ch->ch_flags, CLMPCC_FLG_NEED_INIT);
   1492  1.27.2.2  elad 
   1493  1.27.2.2  elad 				/*
   1494  1.27.2.2  elad 				 * Allow time for the channel to initialise.
   1495  1.27.2.2  elad 				 * (Empirically derived duration; there must
   1496  1.27.2.2  elad 				 * be another way to determine the command
   1497  1.27.2.2  elad 				 * has completed without busy-waiting...)
   1498  1.27.2.2  elad 				 */
   1499  1.27.2.2  elad 				delay(800);
   1500  1.27.2.2  elad 
   1501  1.27.2.2  elad 				/*
   1502  1.27.2.2  elad 				 * Update the tty layer's idea of the carrier
   1503  1.27.2.2  elad 				 * bit, in case we changed CLOCAL or MDMBUF.
   1504  1.27.2.2  elad 				 * We don't hang up here; we only do that by
   1505  1.27.2.2  elad 				 * explicit request.
   1506  1.27.2.2  elad 				 */
   1507  1.27.2.2  elad 				reg = clmpcc_rd_msvr(sc) & CLMPCC_MSVR_CD;
   1508  1.27.2.2  elad 				(*tp->t_linesw->l_modem)(tp, reg != 0);
   1509  1.27.2.2  elad 			}
   1510  1.27.2.2  elad 
   1511  1.27.2.2  elad 			CLR(tp->t_state, TS_BUSY);
   1512  1.27.2.2  elad 			if ( ISSET(tp->t_state, TS_FLUSH) )
   1513  1.27.2.2  elad 				CLR(tp->t_state, TS_FLUSH);
   1514  1.27.2.2  elad 			else
   1515  1.27.2.2  elad 				ndflush(&tp->t_outq,
   1516  1.27.2.2  elad 				     (int)(ch->ch_obuf_addr - tp->t_outq.c_cf));
   1517  1.27.2.2  elad 
   1518  1.27.2.2  elad 			(*tp->t_linesw->l_start)(tp);
   1519  1.27.2.2  elad 		}
   1520  1.27.2.2  elad 	}
   1521  1.27.2.2  elad }
   1522  1.27.2.2  elad 
   1523  1.27.2.2  elad 
   1524  1.27.2.2  elad /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
   1525  1.27.2.2  elad /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
   1526  1.27.2.2  elad /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
   1527  1.27.2.2  elad /*
   1528  1.27.2.2  elad  * Following are all routines needed for a cd240x channel to act as console
   1529  1.27.2.2  elad  */
   1530  1.27.2.2  elad int
   1531  1.27.2.2  elad clmpcc_cnattach(sc, chan, rate)
   1532  1.27.2.2  elad 	struct clmpcc_softc *sc;
   1533  1.27.2.2  elad 	int chan;
   1534  1.27.2.2  elad 	int rate;
   1535  1.27.2.2  elad {
   1536  1.27.2.2  elad 	cons_sc = sc;
   1537  1.27.2.2  elad 	cons_chan = chan;
   1538  1.27.2.2  elad 	cons_rate = rate;
   1539  1.27.2.2  elad 
   1540  1.27.2.2  elad 	return (clmpcc_init(sc));
   1541  1.27.2.2  elad }
   1542  1.27.2.2  elad 
   1543  1.27.2.2  elad /*
   1544  1.27.2.2  elad  * The following functions are polled getc and putc routines, for console use.
   1545  1.27.2.2  elad  */
   1546  1.27.2.2  elad static int
   1547  1.27.2.2  elad clmpcc_common_getc(sc, chan)
   1548  1.27.2.2  elad 	struct clmpcc_softc *sc;
   1549  1.27.2.2  elad 	int chan;
   1550  1.27.2.2  elad {
   1551  1.27.2.2  elad 	u_char old_chan;
   1552  1.27.2.2  elad 	u_char old_ier;
   1553  1.27.2.2  elad 	u_char ch, rir, risr;
   1554  1.27.2.2  elad 	int s;
   1555  1.27.2.2  elad 
   1556  1.27.2.2  elad 	s = splhigh();
   1557  1.27.2.2  elad 
   1558  1.27.2.2  elad 	/* Save the currently active channel */
   1559  1.27.2.2  elad 	old_chan = clmpcc_select_channel(sc, chan);
   1560  1.27.2.2  elad 
   1561  1.27.2.2  elad 	/*
   1562  1.27.2.2  elad 	 * We have to put the channel into RX interrupt mode before
   1563  1.27.2.2  elad 	 * trying to read the Rx data register. So save the previous
   1564  1.27.2.2  elad 	 * interrupt mode.
   1565  1.27.2.2  elad 	 */
   1566  1.27.2.2  elad 	old_ier = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1567  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_IER, CLMPCC_IER_RX_FIFO);
   1568  1.27.2.2  elad 
   1569  1.27.2.2  elad 	/* Loop until we get a character */
   1570  1.27.2.2  elad 	for (;;) {
   1571  1.27.2.2  elad 		/*
   1572  1.27.2.2  elad 		 * The REN bit will be set in the Receive Interrupt Register
   1573  1.27.2.2  elad 		 * when the CD240x has a character to process. Remember,
   1574  1.27.2.2  elad 		 * the RACT bit won't be set until we generate an interrupt
   1575  1.27.2.2  elad 		 * acknowledge cycle via the MD front-end.
   1576  1.27.2.2  elad 		 */
   1577  1.27.2.2  elad 		rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
   1578  1.27.2.2  elad 		if ( (rir & CLMPCC_RIR_REN) == 0 )
   1579  1.27.2.2  elad 			continue;
   1580  1.27.2.2  elad 
   1581  1.27.2.2  elad 		/* Acknowledge the request */
   1582  1.27.2.2  elad 		if ( sc->sc_iackhook )
   1583  1.27.2.2  elad 			(sc->sc_iackhook)(sc, CLMPCC_IACK_RX);
   1584  1.27.2.2  elad 
   1585  1.27.2.2  elad 		/*
   1586  1.27.2.2  elad 		 * Determine if the interrupt is for the required channel
   1587  1.27.2.2  elad 		 * and if valid data is available.
   1588  1.27.2.2  elad 		 */
   1589  1.27.2.2  elad 		rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
   1590  1.27.2.2  elad 		risr = clmpcc_rdreg(sc, CLMPCC_REG_RISR);
   1591  1.27.2.2  elad 		if ( (rir & CLMPCC_RIR_RCN_MASK) != chan ||
   1592  1.27.2.2  elad 		     risr != 0 ) {
   1593  1.27.2.2  elad 			/* Rx error, or BREAK */
   1594  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_REOIR,
   1595  1.27.2.2  elad 					 CLMPCC_REOIR_NO_TRANS);
   1596  1.27.2.2  elad 		} else {
   1597  1.27.2.2  elad 			/* Dummy read of the FIFO count register */
   1598  1.27.2.2  elad 			(void) clmpcc_rdreg(sc, CLMPCC_REG_RFOC);
   1599  1.27.2.2  elad 
   1600  1.27.2.2  elad 			/* Fetch the received character */
   1601  1.27.2.2  elad 			ch = clmpcc_rd_rxdata(sc);
   1602  1.27.2.2  elad 
   1603  1.27.2.2  elad 			clmpcc_wrreg(sc, CLMPCC_REG_REOIR, 0);
   1604  1.27.2.2  elad 			break;
   1605  1.27.2.2  elad 		}
   1606  1.27.2.2  elad 	}
   1607  1.27.2.2  elad 
   1608  1.27.2.2  elad 	/* Restore the original IER and CAR register contents */
   1609  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_IER, old_ier);
   1610  1.27.2.2  elad 	clmpcc_select_channel(sc, old_chan);
   1611  1.27.2.2  elad 
   1612  1.27.2.2  elad 	splx(s);
   1613  1.27.2.2  elad 	return ch;
   1614  1.27.2.2  elad }
   1615  1.27.2.2  elad 
   1616  1.27.2.2  elad 
   1617  1.27.2.2  elad static void
   1618  1.27.2.2  elad clmpcc_common_putc(sc, chan, c)
   1619  1.27.2.2  elad 	struct clmpcc_softc *sc;
   1620  1.27.2.2  elad 	int chan;
   1621  1.27.2.2  elad 	int c;
   1622  1.27.2.2  elad {
   1623  1.27.2.2  elad 	u_char old_chan;
   1624  1.27.2.2  elad 	int s = splhigh();
   1625  1.27.2.2  elad 
   1626  1.27.2.2  elad 	/* Save the currently active channel */
   1627  1.27.2.2  elad 	old_chan = clmpcc_select_channel(sc, chan);
   1628  1.27.2.2  elad 
   1629  1.27.2.2  elad 	/*
   1630  1.27.2.2  elad 	 * Since we can only access the Tx Data register from within
   1631  1.27.2.2  elad 	 * the interrupt handler, the easiest way to get console data
   1632  1.27.2.2  elad 	 * onto the wire is using one of the Special Transmit Character
   1633  1.27.2.2  elad 	 * registers.
   1634  1.27.2.2  elad 	 */
   1635  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_SCHR4, c);
   1636  1.27.2.2  elad 	clmpcc_wrreg(sc, CLMPCC_REG_STCR, CLMPCC_STCR_SSPC(4) |
   1637  1.27.2.2  elad 					  CLMPCC_STCR_SND_SPC);
   1638  1.27.2.2  elad 
   1639  1.27.2.2  elad 	/* Wait until the "Send Special Character" command is accepted */
   1640  1.27.2.2  elad 	while ( clmpcc_rdreg(sc, CLMPCC_REG_STCR) != 0 )
   1641  1.27.2.2  elad 		;
   1642  1.27.2.2  elad 
   1643  1.27.2.2  elad 	/* Restore the previous channel selected */
   1644  1.27.2.2  elad 	clmpcc_select_channel(sc, old_chan);
   1645  1.27.2.2  elad 
   1646  1.27.2.2  elad 	splx(s);
   1647  1.27.2.2  elad }
   1648  1.27.2.2  elad 
   1649  1.27.2.2  elad int
   1650  1.27.2.2  elad clmpcccngetc(dev)
   1651  1.27.2.2  elad 	dev_t dev;
   1652  1.27.2.2  elad {
   1653  1.27.2.2  elad 	return clmpcc_common_getc(cons_sc, cons_chan);
   1654  1.27.2.2  elad }
   1655  1.27.2.2  elad 
   1656  1.27.2.2  elad /*
   1657  1.27.2.2  elad  * Console kernel output character routine.
   1658  1.27.2.2  elad  */
   1659  1.27.2.2  elad void
   1660  1.27.2.2  elad clmpcccnputc(dev, c)
   1661  1.27.2.2  elad 	dev_t dev;
   1662  1.27.2.2  elad 	int c;
   1663  1.27.2.2  elad {
   1664  1.27.2.2  elad 	if ( c == '\n' )
   1665  1.27.2.2  elad 		clmpcc_common_putc(cons_sc, cons_chan, '\r');
   1666  1.27.2.2  elad 
   1667  1.27.2.2  elad 	clmpcc_common_putc(cons_sc, cons_chan, c);
   1668  1.27.2.2  elad }
   1669