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