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