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