Home | History | Annotate | Line # | Download | only in ic
cd18xx.c revision 1.19
      1 /*	$NetBSD: cd18xx.c,v 1.19 2007/03/04 06:01:51 christos Exp $	*/
      2 
      3 /* XXXad does this even compile? */
      4 
      5 /*
      6  * Copyright (c) 1998, 2001 Matthew R. Green
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*-
     34  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     35  * All rights reserved.
     36  *
     37  * This code is derived from software contributed to The NetBSD Foundation
     38  * by Charles M. Hannum.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *        This product includes software developed by the NetBSD
     51  *        Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1991 The Regents of the University of California.
     71  * All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     98  */
     99 
    100 /*
    101  * cirrus logic CL-CD180/CD1864/CD1865 driver, based in (large) parts on
    102  * the com and z8530 drivers.  thanks charles.
    103  */
    104 
    105 #include <sys/cdefs.h>
    106 __KERNEL_RCSID(0, "$NetBSD: cd18xx.c,v 1.19 2007/03/04 06:01:51 christos Exp $");
    107 
    108 #include <sys/param.h>
    109 #include <sys/conf.h>
    110 #include <sys/device.h>
    111 #include <sys/systm.h>
    112 #include <sys/malloc.h>
    113 #include <sys/proc.h>
    114 #include <sys/kernel.h>
    115 #include <sys/tty.h>
    116 #include <sys/fcntl.h>
    117 #include <sys/kauth.h>
    118 
    119 #include <machine/bus.h>
    120 
    121 #include <dev/ic/cd18xxvar.h>
    122 #include <dev/ic/cd18xxreg.h>
    123 
    124 #include "ioconf.h"
    125 
    126 /*
    127  * some helpers
    128  */
    129 
    130 /* macros to clear/set/test flags. */
    131 #define SET(t, f)	(t) |= (f)
    132 #define CLR(t, f)	(t) &= ~(f)
    133 #define ISSET(t, f)	((t) & (f))
    134 
    135 static void	cdtty_attach(struct cd18xx_softc *, int);
    136 
    137 static inline void cd18xx_rint(struct cd18xx_softc *, int *);
    138 static inline void cd18xx_tint(struct cd18xx_softc *, int *);
    139 static inline void cd18xx_mint(struct cd18xx_softc *, int *);
    140 
    141 void cdtty_rxsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
    142 void cdtty_txsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
    143 void cdtty_stsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
    144 void cd18xx_softintr(void *);
    145 
    146 dev_type_open(cdttyopen);
    147 dev_type_close(cdttyclose);
    148 dev_type_read(cdttyread);
    149 dev_type_write(cdttywrite);
    150 dev_type_ioctl(cdttyioctl);
    151 dev_type_stop(cdttystop);
    152 dev_type_tty(cdttytty);
    153 dev_type_poll(cdttypoll);
    154 
    155 const struct cdevsw cdtty_cdevsw = {
    156 	cdttyopen, cdttyclose, cdttyread, cdttywrite, cdttyioctl,
    157 	cdttystop, cdttytty, cdttypoll, nommap, ttykqfilter, D_TTY
    158 };
    159 
    160 static void	cdtty_shutdown(struct cd18xx_softc *, struct cdtty_port *);
    161 static void	cdttystart(struct tty *);
    162 static int	cdttyparam(struct tty *, struct termios *);
    163 static void	cdtty_break(struct cd18xx_softc *, struct cdtty_port *, int);
    164 static void	cdtty_modem(struct cd18xx_softc *, struct cdtty_port *, int);
    165 static int	cdttyhwiflow(struct tty *, int);
    166 static void	cdtty_hwiflow(struct cd18xx_softc *, struct cdtty_port *);
    167 
    168 static void	cdtty_loadchannelregs(struct cd18xx_softc *,
    169 					   struct cdtty_port *);
    170 
    171 /* default read buffer size */
    172 u_int cdtty_rbuf_size = CDTTY_RING_SIZE;
    173 
    174 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    175 u_int cdtty_rbuf_hiwat = (CDTTY_RING_SIZE * 1) / 4;
    176 u_int cdtty_rbuf_lowat = (CDTTY_RING_SIZE * 3) / 4;
    177 
    178 #define CD18XXDEBUG
    179 #ifdef CD18XXDEBUG
    180 #define CDD_INFO	0x0001
    181 #define CDD_INTR	0x0002
    182 int cd18xx_debug = CDD_INTR|CDD_INFO;
    183 # define DPRINTF(l, x)	if (cd18xx_debug & l) printf x
    184 #else
    185 # define DPRINTF(l, x)	/* nothing */
    186 #endif
    187 
    188 /* Known supported revisions. */
    189 struct cd18xx_revs {
    190 	u_char	revision;
    191 	u_char	onehundred_pin;
    192 	char	*name;
    193 } cd18xx_revs[] = {
    194 	{ CD180_GFRCR_REV_B,		0, "CL-CD180 rev. B" },
    195 	{ CD180_GFRCR_REV_C,		0, "CL-CD180 rev. C" },
    196 	{ CD1864_GFRCR_REVISION_A,	1, "CL-CD1864 rev. A" },
    197 	{ CD1865_GFRCR_REVISION_A,	1, "CL-CD1865 rev. A" },
    198 	{ CD1865_GFRCR_REVISION_B,	1, "CL-CD1865 rev. B" },
    199 	{ CD1865_GFRCR_REVISION_C,	1, "CL-CD1865 rev. C" },
    200 	{ 0, 0, 0 }
    201 };
    202 
    203 /* wait for the CCR to go to zero */
    204 static inline int cd18xx_wait_ccr(struct cd18xx_softc *);
    205 static inline int
    206 cd18xx_wait_ccr(sc)
    207 	struct cd18xx_softc *sc;
    208 {
    209 	int i = 100000;
    210 
    211 	while (--i &&
    212 	    bus_space_read_1(sc->sc_tag, sc->sc_handle, CD18xx_CCR) != 0)
    213 		;
    214 	return (i == 0);
    215 }
    216 
    217 /*
    218  * device attach routine, high-end portion
    219  */
    220 void
    221 cd18xx_attach(sc)
    222 	struct cd18xx_softc *sc;
    223 {
    224 	static int chip_id_next = 1;
    225 	int onehundred_pin, revision, i, port;
    226 
    227 	/* read and print the revision */
    228 	revision = cd18xx_read(sc, CD18xx_GFRCR);
    229 	onehundred_pin = ISSET(cd18xx_read(sc, CD18xx_SRCR),CD18xx_SRCR_PKGTYP);
    230 	for (i = 0; cd18xx_revs[i].name; i++)
    231 		if (revision == cd18xx_revs[i].revision ||
    232 		    onehundred_pin == cd18xx_revs[i].onehundred_pin) {
    233 			printf(": %s", cd18xx_revs[i].name);
    234 			break;
    235 		}
    236 
    237 	if (cd18xx_revs[i].name == NULL) {
    238 		printf("%s: unknown revision, bailing.\n", sc->sc_dev.dv_xname);
    239 		return;
    240 	}
    241 
    242 	/* prepare for reset */
    243 	cd18xx_set_car(sc, 0);
    244 	cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_CLEAR);
    245 
    246 	/* wait for CCR to go to zero */
    247 	if (cd18xx_wait_ccr(sc)) {
    248 		printf("cd18xx_attach: reset change command timed out\n");
    249 		return;
    250 	}
    251 
    252 	/* full reset of all channels */
    253 	cd18xx_write(sc, CD18xx_CCR,
    254 	    CD18xx_CCR_RESET|CD18xx_CCR_RESET_HARD);
    255 
    256 	/* loop until the GSVR is ready */
    257 	i = 100000;
    258 	while (--i && cd18xx_read(sc, CD18xx_GSVR) == CD18xx_GSVR_READY)
    259 		;
    260 	if (i == 0) {
    261 		printf("\n%s: did not reset!\n", sc->sc_dev.dv_xname);
    262 		return;
    263 	}
    264 
    265 	/* write the chip_id */
    266 	sc->sc_chip_id = chip_id_next++;
    267 #ifdef DIAGNOSTIC
    268 	if (sc->sc_chip_id > 31)
    269 		panic("more than 31 cd18xx's?  help.");
    270 #endif
    271 	cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_SETID(sc));
    272 
    273 	/* rx/tx/modem service match vectors, initalised by higher level */
    274 	cd18xx_write(sc, CD18xx_MSMR, sc->sc_msmr | 0x80);
    275 	cd18xx_write(sc, CD18xx_TSMR, sc->sc_tsmr | 0x80);
    276 	cd18xx_write(sc, CD18xx_RSMR, sc->sc_rsmr | 0x80);
    277 
    278 	printf(", gsvr %x msmr %x tsmr %x rsmr %x",
    279 	    cd18xx_read(sc, CD18xx_GSVR),
    280 	    cd18xx_read(sc, CD18xx_MSMR),
    281 	    cd18xx_read(sc, CD18xx_TSMR),
    282 	    cd18xx_read(sc, CD18xx_RSMR));
    283 
    284 	/* prescale registers */
    285 	sc->sc_pprh = 0xf0;
    286 	sc->sc_pprl = 0;
    287 	cd18xx_write(sc, CD18xx_PPRH, sc->sc_pprh);
    288 	cd18xx_write(sc, CD18xx_PPRL, sc->sc_pprl);
    289 
    290 	/* establish our soft interrupt. */
    291 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, cd18xx_softintr, sc);
    292 
    293 	printf(", 8 ports ready (chip id %d)\n", sc->sc_chip_id);
    294 
    295 	/*
    296 	 * finally, we loop over all 8 channels initialising them
    297 	 */
    298 	for (port = 0; port < 8; port++)
    299 		cdtty_attach(sc, port);
    300 }
    301 
    302 /* tty portion of the code */
    303 
    304 /*
    305  * tty portion attach routine
    306  */
    307 void
    308 cdtty_attach(sc, port)
    309 	struct	cd18xx_softc *sc;
    310 	int port;
    311 {
    312 	struct cdtty_port *p = &sc->sc_ports[port];
    313 	int i;
    314 
    315 	/* load CAR with channel number */
    316 	cd18xx_set_car(sc, port);
    317 
    318 	/* wait for CCR to go to zero */
    319 	if (cd18xx_wait_ccr(sc)) {
    320 		printf("cd18xx_attach: change command timed out setting "
    321 		       "CAR for port %d\n", i);
    322 		return;
    323 	}
    324 
    325 	/* set the RPTR to (arbitrary) 8 */
    326 	cd18xx_write(sc, CD18xx_RTPR, 8);
    327 
    328 	/* reset the modem signal value register */
    329 	sc->sc_ports[port].p_msvr = CD18xx_MSVR_RESET;
    330 
    331 	/* zero the service request enable register */
    332 	cd18xx_write(sc, CD18xx_SRER, 0);
    333 
    334 	/* enable the transmitter & receiver */
    335 	SET(p->p_chanctl, CD18xx_CCR_CHANCTL |
    336 		          CD18xx_CCR_CHANCTL_TxEN |
    337 		          CD18xx_CCR_CHANCTL_RxEN);
    338 
    339 	/* XXX no console or kgdb support yet! */
    340 
    341 	/* get a tty structure */
    342 	p->p_tty = ttymalloc();
    343 	p->p_tty->t_oproc = cdttystart;
    344 	p->p_tty->t_param = cdttyparam;
    345 	p->p_tty->t_hwiflow = cdttyhwiflow;
    346 
    347 	p->p_rbuf = malloc(cdtty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
    348 	p->p_rbput = p->p_rbget = p->p_rbuf;
    349 	p->p_rbavail = cdtty_rbuf_size;
    350 	if (p->p_rbuf == NULL) {
    351 		printf("%s: unable to allocate ring buffer for tty %d\n",
    352 		    sc->sc_dev.dv_xname, port);
    353 		return;
    354 	}
    355 	p->p_ebuf = p->p_rbuf + (cdtty_rbuf_size << 1);
    356 
    357 	tty_attach(p->p_tty);
    358 }
    359 
    360 /*
    361  * cdtty_shutdown: called when the device is last closed.
    362  */
    363 void
    364 cdtty_shutdown(sc, p)
    365 	struct cd18xx_softc *sc;
    366 	struct cdtty_port *p;
    367 {
    368 	struct tty *tp = p->p_tty;
    369 	int s;
    370 
    371 	s = splserial();
    372 
    373 	/* If we were asserting flow control, then deassert it. */
    374 	SET(p->p_rx_flags, RX_IBUF_BLOCKED);
    375 	cdtty_hwiflow(sc, p);
    376 
    377 	/* Clear any break condition set with TIOCSBRK. */
    378 	cdtty_break(sc, p, 0);
    379 
    380 	/*
    381 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    382 	 * notice even if we immediately open the port again.
    383 	 * Avoid tsleeping above splhigh().
    384 	 */
    385 	if (ISSET(tp->t_cflag, HUPCL)) {
    386 		cdtty_modem(sc, p, 0);
    387 		splx(s);
    388 		/* XXX tsleep will only timeout */
    389 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    390 		s = splserial();
    391 	}
    392 
    393 	/* Turn off interrupts. */
    394 	p->p_srer = 0;
    395 	cd18xx_write(sc, CD18xx_SRER, p->p_srer);
    396 
    397 	splx(s);
    398 }
    399 
    400 /*
    401  * cdttyopen:  open syscall for cdtty terminals..
    402  */
    403 int
    404 cdttyopen(dev, flag, mode, p)
    405 	dev_t dev;
    406 	int flag;
    407 	int mode;
    408 	struct proc *p;
    409 {
    410 	struct tty *tp;
    411 	struct cd18xx_softc *sc;
    412 	struct cdtty_port *port;
    413 	int channel, instance, s, error;
    414 
    415 	channel = CD18XX_CHANNEL(dev);
    416 	instance = CD18XX_INSTANCE(dev);
    417 
    418 	/* ensure instance is valid */
    419 	if (instance >= clcd_cd.cd_ndevs)
    420 		return (ENXIO);
    421 
    422 	/* get softc and port */
    423 	sc = clcd_cd.cd_devs[instance];
    424 	if (sc == NULL)
    425 		return (ENXIO);
    426 	port = &sc->sc_ports[channel];
    427 	if (port == NULL || port->p_rbuf == NULL)
    428 		return (ENXIO);
    429 
    430 	/* kgdb support?  maybe later... */
    431 
    432 	tp = port->p_tty;
    433 
    434 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    435 		return (EBUSY);
    436 
    437 	s = spltty();
    438 
    439 	/*
    440 	 * Do the following iff this is a first open.
    441 	 */
    442 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    443 		struct termios t;
    444 
    445 		/* set up things in tp as necessary */
    446 		tp->t_dev = dev;
    447 
    448 		/*
    449 		 * Initialize the termios status to the defaults.  Add in the
    450 		 * sticky bits from TIOCSFLAGS.
    451 		 */
    452 		t.c_ispeed = 0;
    453 		t.c_ospeed = TTYDEF_SPEED;
    454 		t.c_cflag = TTYDEF_CFLAG;
    455 
    456 		if (ISSET(port->p_swflags, TIOCFLAG_CLOCAL))
    457 			SET(t.c_cflag, CLOCAL);
    458 		if (ISSET(port->p_swflags, TIOCFLAG_CRTSCTS))
    459 			SET(t.c_cflag, CRTSCTS);
    460 		if (ISSET(port->p_swflags, TIOCFLAG_CDTRCTS))
    461 			SET(t.c_cflag, CDTRCTS);
    462 		if (ISSET(port->p_swflags, TIOCFLAG_MDMBUF))
    463 			SET(t.c_cflag, MDMBUF);
    464 
    465 		/* Make sure param will see changes. */
    466 		tp->t_ospeed = 0;	/* XXX set above ignored? */
    467 		(void)cdttyparam(tp, &t);
    468 
    469 		tp->t_iflag = TTYDEF_IFLAG;
    470 		tp->t_oflag = TTYDEF_OFLAG;
    471 		tp->t_lflag = TTYDEF_LFLAG;
    472 		ttychars(tp);
    473 		ttsetwater(tp);
    474 
    475 		(void)splserial();
    476 
    477 		/* turn on rx and modem interrupts */
    478 		cd18xx_set_car(sc, CD18XX_CHANNEL(dev));
    479 		SET(port->p_srer, CD18xx_SRER_Rx |
    480 				  CD18xx_SRER_RxSC |
    481 				  CD18xx_SRER_CD);
    482 		cd18xx_write(sc, CD18xx_SRER, port->p_srer);
    483 
    484 		/* always turn on DTR when open */
    485 		cdtty_modem(sc, port, 1);
    486 
    487 		/* initialise ring buffer */
    488 		port->p_rbget = port->p_rbput = port->p_rbuf;
    489 		port->p_rbavail = cdtty_rbuf_size;
    490 		CLR(port->p_rx_flags, RX_ANY_BLOCK);
    491 		cdtty_hwiflow(sc, port);
    492 	}
    493 
    494 	/* drop spl back before going into the line open */
    495 	splx(s);
    496 
    497 	error = ttyopen(tp, CD18XX_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
    498 	if (error == 0)
    499 		error = (*tp->t_linesw->l_open)(dev, tp);
    500 
    501 	return (error);
    502 }
    503 
    504 /*
    505  * cdttyclose:  close syscall for cdtty terminals..
    506  */
    507 int
    508 cdttyclose(dev, flag, mode, p)
    509 	dev_t dev;
    510 	int flag;
    511 	int mode;
    512 	struct proc *p;
    513 {
    514 	struct cd18xx_softc *sc;
    515 	struct cdtty_port *port;
    516 	struct tty *tp;
    517 	int channel, instance;
    518 
    519 	channel = CD18XX_CHANNEL(dev);
    520 	instance = CD18XX_INSTANCE(dev);
    521 
    522 	/* ensure instance is valid */
    523 	if (instance >= clcd_cd.cd_ndevs)
    524 		return (ENXIO);
    525 
    526 	/* get softc and port */
    527 	sc = clcd_cd.cd_devs[instance];
    528 	if (sc == NULL)
    529 		return (ENXIO);
    530 	port = &sc->sc_ports[channel];
    531 
    532 	tp = port->p_tty;
    533 
    534 	(*tp->t_linesw->l_close)(tp, flag);
    535 	ttyclose(tp);
    536 
    537 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    538 		/*
    539 		 * Although we got a last close, the device may still be in
    540 		 * use; e.g. if this was the dialout node, and there are still
    541 		 * processes waiting for carrier on the non-dialout node.
    542 		 */
    543 		cdtty_shutdown(sc, port);
    544 	}
    545 
    546 	return (0);
    547 }
    548 
    549 /*
    550  * cdttyread:  read syscall for cdtty terminals..
    551  */
    552 int
    553 cdttyread(dev, uio, flag)
    554 	dev_t dev;
    555 	struct uio *uio;
    556 	int flag;
    557 {
    558 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
    559 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
    560 	struct tty *tp = port->p_tty;
    561 
    562 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    563 }
    564 
    565 /*
    566  * cdttywrite:  write syscall for cdtty terminals..
    567  */
    568 int
    569 cdttywrite(dev, uio, flag)
    570 	dev_t dev;
    571 	struct uio *uio;
    572 	int flag;
    573 {
    574 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
    575 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
    576 	struct tty *tp = port->p_tty;
    577 
    578 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    579 }
    580 
    581 int
    582 cdttypoll(dev, events, p)
    583 	dev_t dev;
    584 	int events;
    585 	struct proc *p;
    586 {
    587 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
    588 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
    589 	struct tty *tp = port->p_tty;
    590 
    591 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    592 }
    593 
    594 /*
    595  * cdttytty:  return a pointer to our (cdtty) tp.
    596  */
    597 struct tty *
    598 cdttytty(dev)
    599 	dev_t dev;
    600 {
    601 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
    602 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
    603 
    604 	return (port->p_tty);
    605 }
    606 
    607 /*
    608  * cdttyioctl:  ioctl syscall for cdtty terminals..
    609  */
    610 int
    611 cdttyioctl(dev, cmd, data, flag, p)
    612 	dev_t dev;
    613 	u_long cmd;
    614 	void *data;
    615 	int flag;
    616 	struct proc *p;
    617 {
    618 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
    619 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
    620 	struct tty *tp = port->p_tty;
    621 	int error, s;
    622 
    623 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    624 	if (error != EPASSTHROUGH)
    625 		return (error);
    626 
    627 	error = ttioctl(tp, cmd, data, flag, p);
    628 	if (error != EPASSTHROUGH)
    629 		return (error);
    630 
    631 	s = splserial();
    632 
    633 	switch (cmd) {
    634 	case TIOCSBRK:
    635 		cdtty_break(sc, port, 1);
    636 		break;
    637 
    638 	case TIOCCBRK:
    639 		cdtty_break(sc, port, 0);
    640 		break;
    641 
    642 	case TIOCSDTR:
    643 		cdtty_modem(sc, port, 1);
    644 		break;
    645 
    646 	case TIOCCDTR:
    647 		cdtty_modem(sc, port, 0);
    648 		break;
    649 
    650 	case TIOCGFLAGS:
    651 		*(int *)data = port->p_swflags;
    652 		break;
    653 
    654 	case TIOCSFLAGS:
    655 		error = kauth_authorize_device_tty(l->l_cred,
    656 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    657 		if (error)
    658 			return (error);
    659 		port->p_swflags = *(int *)data;
    660 		break;
    661 
    662 	case TIOCMSET:
    663 	case TIOCMBIS:
    664 	case TIOCMBIC:
    665 	case TIOCMGET:
    666 	default:
    667 		return (EPASSTHROUGH);
    668 	}
    669 
    670 	splx(s);
    671 	return (0);
    672 }
    673 
    674 /*
    675  * Start or restart transmission.
    676  */
    677 static void
    678 cdttystart(tp)
    679 	struct tty *tp;
    680 {
    681 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
    682 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
    683 	int s;
    684 
    685 	s = spltty();
    686 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    687 		goto out;
    688 	if (p->p_tx_stopped)
    689 		goto out;
    690 
    691 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    692 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    693 			CLR(tp->t_state, TS_ASLEEP);
    694 			wakeup((void *)&tp->t_outq);
    695 		}
    696 		selwakeup(&tp->t_wsel);
    697 		if (tp->t_outq.c_cc == 0)
    698 			goto out;
    699 	}
    700 
    701 	/* Grab the first contiguous region of buffer space. */
    702 	{
    703 		u_char *tba;
    704 		int tbc;
    705 
    706 		tba = tp->t_outq.c_cf;
    707 		tbc = ndqb(&tp->t_outq, 0);
    708 
    709 		(void)splserial();
    710 
    711 		p->p_tba = tba;
    712 		p->p_tbc = tbc;
    713 	}
    714 
    715 	SET(tp->t_state, TS_BUSY);
    716 	p->p_tx_busy = 1;
    717 
    718 	/* turn on tx interrupts */
    719 	if ((p->p_srer & CD18xx_SRER_Tx) == 0) {
    720 		cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
    721 		SET(p->p_srer, CD18xx_SRER_Tx);
    722 		cd18xx_write(sc, CD18xx_SRER, p->p_srer);
    723 	}
    724 
    725 	/*
    726 	 * Now bail; we can't actually transmit bytes until we're in a
    727 	 * transmit interrupt service routine.
    728 	 */
    729 out:
    730 	splx(s);
    731 	return;
    732 }
    733 
    734 /*
    735  * cdttystop:  handing ^S or other stop signals, for a cdtty
    736  */
    737 void
    738 cdttystop(tp, flag)
    739 	struct tty *tp;
    740 	int flag;
    741 {
    742 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
    743 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
    744 	int s;
    745 
    746 	s = splserial();
    747 	if (ISSET(tp->t_state, TS_BUSY)) {
    748 		/* Stop transmitting at the next chunk. */
    749 		p->p_tbc = 0;
    750 		p->p_heldtbc = 0;
    751 		if (!ISSET(tp->t_state, TS_TTSTOP))
    752 			SET(tp->t_state, TS_FLUSH);
    753 	}
    754 	splx(s);
    755 }
    756 
    757 /*
    758  * load a channel's registers.
    759  */
    760 void
    761 cdtty_loadchannelregs(sc, p)
    762 	struct cd18xx_softc *sc;
    763 	struct cdtty_port *p;
    764 {
    765 
    766 	cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
    767 	cd18xx_write(sc, CD18xx_SRER, p->p_srer);
    768 	cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active = p->p_msvr);
    769 	cd18xx_write(sc, CD18xx_COR1, p->p_cor1);
    770 	cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
    771 	cd18xx_write(sc, CD18xx_COR3, p->p_cor3);
    772 	/*
    773 	 * COR2 and COR3 change commands are not required here for
    774 	 * the CL-CD1865 but we do them anyway for simplicity.
    775 	 */
    776 	cd18xx_write(sc, CD18xx_CCR, CD18xx_CCR_CORCHG |
    777 				     CD18xx_CCR_CORCHG_COR1 |
    778 				     CD18xx_CCR_CORCHG_COR2 |
    779 				     CD18xx_CCR_CORCHG_COR3);
    780 	cd18xx_write(sc, CD18xx_RBPRH, p->p_rbprh);
    781 	cd18xx_write(sc, CD18xx_RBPRL, p->p_rbprl);
    782 	cd18xx_write(sc, CD18xx_TBPRH, p->p_tbprh);
    783 	cd18xx_write(sc, CD18xx_TBPRL, p->p_tbprl);
    784 	if (cd18xx_wait_ccr(sc)) {
    785 		DPRINTF(CDD_INFO,
    786 		    ("%s: cdtty_loadchannelregs ccr wait timed out\n",
    787 		    sc->sc_dev.dv_xname));
    788 	}
    789 	cd18xx_write(sc, CD18xx_CCR, p->p_chanctl);
    790 }
    791 
    792 /*
    793  * Set tty parameters from termios.
    794  * XXX - Should just copy the whole termios after
    795  * making sure all the changes could be done.
    796  */
    797 static int
    798 cdttyparam(tp, t)
    799 	struct tty *tp;
    800 	struct termios *t;
    801 {
    802 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
    803 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
    804 	int s;
    805 
    806 	/* Check requested parameters. */
    807 	if (t->c_ospeed < 0)
    808 		return (EINVAL);
    809 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    810 		return (EINVAL);
    811 
    812 	/*
    813 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    814 	 * is always active.
    815 	 */
    816 	if (ISSET(p->p_swflags, TIOCFLAG_SOFTCAR)) {
    817 		SET(t->c_cflag, CLOCAL);
    818 		CLR(t->c_cflag, HUPCL);
    819 	}
    820 
    821 	/*
    822 	 * If there were no changes, don't do anything.  This avoids dropping
    823 	 * input and improves performance when all we did was frob things like
    824 	 * VMIN and VTIME.
    825 	 */
    826 	if (tp->t_ospeed == t->c_ospeed &&
    827 	    tp->t_cflag == t->c_cflag)
    828 		return (0);
    829 
    830 	/*
    831 	 * Block interrupts so that state will not
    832 	 * be altered until we are done setting it up.
    833 	 */
    834 	s = splserial();
    835 
    836 	/*
    837 	 * Copy across the size, parity and stop bit info.
    838 	 */
    839 	switch (t->c_cflag & CSIZE) {
    840 	case CS5:
    841 		p->p_cor1 = CD18xx_COR1_CS5;
    842 		break;
    843 	case CS6:
    844 		p->p_cor1 = CD18xx_COR1_CS6;
    845 		break;
    846 	case CS7:
    847 		p->p_cor1 = CD18xx_COR1_CS7;
    848 		break;
    849 	default:
    850 		p->p_cor1 = CD18xx_COR1_CS8;
    851 		break;
    852 	}
    853 	if (ISSET(t->c_cflag, PARENB)) {
    854 		SET(p->p_cor1, CD18xx_COR1_PARITY_NORMAL);
    855 		if (ISSET(t->c_cflag, PARODD))
    856 			SET(p->p_cor1, CD18xx_COR1_PARITY_ODD);
    857 	}
    858 	if (!ISSET(t->c_iflag, INPCK))
    859 		SET(p->p_cor1, CD18xx_COR1_IGNORE);
    860 	if (ISSET(t->c_cflag, CSTOPB))
    861 		SET(p->p_cor1, CD18xx_COR1_STOPBIT_2);
    862 
    863 	/*
    864 	 * If we're not in a mode that assumes a connection is present, then
    865 	 * ignore carrier changes.
    866 	 */
    867 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
    868 		p->p_msvr_dcd = 0;
    869 	else
    870 		p->p_msvr_dcd = CD18xx_MSVR_CD;
    871 
    872 	/*
    873 	 * Set the flow control pins depending on the current flow control
    874 	 * mode.
    875 	 */
    876 	if (ISSET(t->c_cflag, CRTSCTS)) {
    877 		p->p_mcor1_dtr = CD18xx_MCOR1_DTR;
    878 		p->p_msvr_rts = CD18xx_MSVR_RTS;
    879 		p->p_msvr_cts = CD18xx_MSVR_CTS;
    880 		p->p_cor2 = CD18xx_COR2_RTSAOE|CD18xx_COR2_CTSAE;
    881 	} else if (ISSET(t->c_cflag, MDMBUF)) {
    882 		/*
    883 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
    884 		 * carrier detection.
    885 		 */
    886 		p->p_mcor1_dtr = 0;
    887 		p->p_msvr_rts = CD18xx_MSVR_DTR;
    888 		p->p_msvr_cts = CD18xx_MSVR_CD;
    889 		p->p_cor2 = 0;
    890 	} else {
    891 		/*
    892 		 * If no flow control, then always set RTS.  This will make
    893 		 * the other side happy if it mistakenly thinks we're doing
    894 		 * RTS/CTS flow control.
    895 		 */
    896 		p->p_mcor1_dtr = CD18xx_MSVR_DTR;
    897 		p->p_msvr_rts = 0;
    898 		p->p_msvr_cts = 0;
    899 		p->p_cor2 = 0;
    900 	}
    901 	p->p_msvr_mask = p->p_msvr_cts | p->p_msvr_dcd;
    902 
    903 	/*
    904 	 * Set the FIFO threshold based on the receive speed.
    905 	 *
    906 	 *  * If it's a low speed, it's probably a mouse or some other
    907 	 *    interactive device, so set the threshold low.
    908 	 *  * If it's a high speed, trim the trigger level down to prevent
    909 	 *    overflows.
    910 	 *  * Otherwise set it a bit higher.
    911 	 */
    912 	p->p_cor3 = (t->c_ospeed <= 1200 ? 1 : t->c_ospeed <= 38400 ? 8 : 4);
    913 
    914 #define PORT_RATE(o, s)	\
    915 	(((((o) + (s)/2) / (s)) + CD18xx_xBRPR_TPC/2) / CD18xx_xBRPR_TPC)
    916 	/* Compute BPS for the requested speeds */
    917 	if (t->c_ospeed) {
    918 		u_int32_t tbpr = PORT_RATE(sc->sc_osc, t->c_ospeed);
    919 
    920 		if (tbpr == 0 || tbpr > 0xffff)
    921 			return (EINVAL);
    922 
    923 		p->p_tbprh = tbpr >> 8;
    924 		p->p_tbprl = tbpr & 0xff;
    925 	}
    926 
    927 	if (t->c_ispeed) {
    928 		u_int32_t rbpr = PORT_RATE(sc->sc_osc, t->c_ispeed);
    929 
    930 		if (rbpr == 0 || rbpr > 0xffff)
    931 			return (EINVAL);
    932 
    933 		p->p_rbprh = rbpr >> 8;
    934 		p->p_rbprl = rbpr & 0xff;
    935 	}
    936 
    937 	/* And copy to tty. */
    938 	tp->t_ispeed = 0;
    939 	tp->t_ospeed = t->c_ospeed;
    940 	tp->t_cflag = t->c_cflag;
    941 
    942 	if (!p->p_heldchange) {
    943 		if (p->p_tx_busy) {
    944 			p->p_heldtbc = p->p_tbc;
    945 			p->p_tbc = 0;
    946 			p->p_heldchange = 1;
    947 		} else
    948 			cdtty_loadchannelregs(sc, p);
    949 	}
    950 
    951 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    952 		/* Disable the high water mark. */
    953 		p->p_r_hiwat = 0;
    954 		p->p_r_lowat = 0;
    955 		if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
    956 			CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
    957 			softintr_schedule(sc->sc_si);
    958 		}
    959 		if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    960 			CLR(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    961 			cdtty_hwiflow(sc, p);
    962 		}
    963 	} else {
    964 		p->p_r_hiwat = cdtty_rbuf_hiwat;
    965 		p->p_r_lowat = cdtty_rbuf_lowat;
    966 	}
    967 
    968 	splx(s);
    969 
    970 	/*
    971 	 * Update the tty layer's idea of the carrier bit, in case we changed
    972 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    973 	 * explicit request.
    974 	 */
    975 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(p->p_msvr, CD18xx_MSVR_CD));
    976 
    977 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    978 		if (p->p_tx_stopped) {
    979 			p->p_tx_stopped = 0;
    980 			cdttystart(tp);
    981 		}
    982 	}
    983 
    984 	return (0);
    985 }
    986 
    987 static void
    988 cdtty_break(sc, p, onoff)
    989 	struct cd18xx_softc *sc;
    990 	struct cdtty_port *p;
    991 	int onoff;
    992 {
    993 
    994 	/* tell tx intr handler we need a break */
    995 	p->p_needbreak = !!onoff;
    996 
    997 	/* turn on tx interrupts if break has changed */
    998 	if (p->p_needbreak != p->p_break)
    999 		SET(p->p_srer, CD18xx_SRER_Tx);
   1000 
   1001 	if (!p->p_heldchange) {
   1002 		if (p->p_tx_busy) {
   1003 			p->p_heldtbc = p->p_tbc;
   1004 			p->p_tbc = 0;
   1005 			p->p_heldchange = 1;
   1006 		} else
   1007 			cdtty_loadchannelregs(sc, p);
   1008 	}
   1009 }
   1010 
   1011 /*
   1012  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1013  * in transmission, the change is deferred.
   1014  */
   1015 static void
   1016 cdtty_modem(sc, p, onoff)
   1017 	struct cd18xx_softc *sc;
   1018 	struct cdtty_port *p;
   1019 	int onoff;
   1020 {
   1021 
   1022 	if (p->p_mcor1_dtr == 0)
   1023 		return;
   1024 
   1025 	if (onoff)
   1026 		CLR(p->p_mcor1, p->p_mcor1_dtr);
   1027 	else
   1028 		SET(p->p_mcor1, p->p_mcor1_dtr);
   1029 
   1030 	if (!p->p_heldchange) {
   1031 		if (p->p_tx_busy) {
   1032 			p->p_heldtbc = p->p_tbc;
   1033 			p->p_tbc = 0;
   1034 			p->p_heldchange = 1;
   1035 		} else
   1036 			cdtty_loadchannelregs(sc, p);
   1037 	}
   1038 }
   1039 
   1040 /*
   1041  * Try to block or unblock input using hardware flow-control.
   1042  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
   1043  * if this function returns non-zero, the TS_TBLOCK flag will
   1044  * be set or cleared according to the "block" arg passed.
   1045  */
   1046 int
   1047 cdttyhwiflow(tp, block)
   1048 	struct tty *tp;
   1049 	int block;
   1050 {
   1051 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
   1052 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
   1053 	int s;
   1054 
   1055 	if (p->p_msvr_rts == 0)
   1056 		return (0);
   1057 
   1058 	s = splserial();
   1059 	if (block) {
   1060 		if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
   1061 			SET(p->p_rx_flags, RX_TTY_BLOCKED);
   1062 			cdtty_hwiflow(sc, p);
   1063 		}
   1064 	} else {
   1065 		if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
   1066 			CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
   1067 			softintr_schedule(sc->sc_si);
   1068 		}
   1069 		if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
   1070 			CLR(p->p_rx_flags, RX_TTY_BLOCKED);
   1071 			cdtty_hwiflow(sc, p);
   1072 		}
   1073 	}
   1074 	splx(s);
   1075 	return (1);
   1076 }
   1077 
   1078 /*
   1079  * Internal version of cdttyhwiflow, called at cdtty's priority.
   1080  */
   1081 static void
   1082 cdtty_hwiflow(sc, p)
   1083 	struct cd18xx_softc *sc;
   1084 	struct cdtty_port *p;
   1085 {
   1086 
   1087 	if (p->p_msvr_rts == 0)
   1088 		return;
   1089 
   1090 	if (ISSET(p->p_rx_flags, RX_ANY_BLOCK)) {
   1091 		CLR(p->p_msvr, p->p_msvr_rts);
   1092 		CLR(p->p_msvr_active, p->p_msvr_rts);
   1093 	} else {
   1094 		SET(p->p_msvr, p->p_msvr_rts);
   1095 		SET(p->p_msvr_active, p->p_msvr_rts);
   1096 	}
   1097 	cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
   1098 	cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active);
   1099 }
   1100 
   1101 /*
   1102  * indiviual interrupt routines.
   1103  */
   1104 
   1105 /*
   1106  * this is the number of interrupts allowed, total.  set it to 0
   1107  * to allow unlimited interrpts
   1108  */
   1109 #define INTR_MAX_ALLOWED	0
   1110 
   1111 #if INTR_MAX_ALLOWED == 0
   1112 #define GOTINTR(sc, p)	/* nothing */
   1113 #else
   1114 int intrcount;
   1115 #define GOTINTR(sc, p)	\
   1116 do { \
   1117 	if (intrcount++ == INTR_MAX_ALLOWED) { \
   1118 		CLR(p->p_srer, CD18xx_SRER_Tx); \
   1119 		cd18xx_write(sc, CD18xx_SRER, p->p_srer); \
   1120 	} \
   1121 	DPRINTF(CDD_INTR, (", intrcount %d srer %x", intrcount, p->p_srer)); \
   1122 } while (0)
   1123 #endif
   1124 
   1125 /* receiver interrupt */
   1126 static inline void
   1127 cd18xx_rint(sc, ns)
   1128 	struct cd18xx_softc *sc;
   1129 	int *ns;
   1130 {
   1131 	struct cdtty_port *p;
   1132 	u_int channel, count;
   1133 	u_char *put, *end;
   1134 	u_int cc;
   1135 
   1136 	/* work out the channel and softc */
   1137 	channel = cd18xx_get_gscr1_channel(sc);
   1138 	p = &sc->sc_ports[channel];
   1139 	DPRINTF(CDD_INTR, ("%s: rint: channel %d", sc->sc_dev.dv_xname, channel));
   1140 	GOTINTR(sc, p);
   1141 
   1142 	end = p->p_ebuf;
   1143 	put = p->p_rbput;
   1144 	cc = p->p_rbavail;
   1145 
   1146 	/* read as many bytes as necessary */
   1147 	count = cd18xx_read(sc, CD18xx_RDCR);
   1148 	DPRINTF(CDD_INTR, (", %d bytes available: ", count));
   1149 
   1150 	while (cc > 0 && count > 0) {
   1151 		u_char rcsr = cd18xx_read(sc, CD18xx_RCSR);
   1152 
   1153 		put[0] = cd18xx_read(sc, CD18xx_RDR);
   1154 		put[1] = rcsr;
   1155 
   1156 		if (rcsr)
   1157 			*ns = 1;
   1158 
   1159 		put += 2;
   1160 		if (put >= end)
   1161 			put = p->p_rbuf;
   1162 
   1163 		DPRINTF(CDD_INTR, ("."));
   1164 		cc--;
   1165 		count--;
   1166 	}
   1167 
   1168 	DPRINTF(CDD_INTR, (" finished reading"));
   1169 
   1170 	/*
   1171 	 * Current string of incoming characters ended because
   1172 	 * no more data was available or we ran out of space.
   1173 	 * If we're out of space, turn off receive interrupts.
   1174 	 */
   1175 	p->p_rbput = put;
   1176 	p->p_rbavail = cc;
   1177 	if (!ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
   1178 		p->p_rx_ready = 1;
   1179 	}
   1180 
   1181 	/*
   1182 	 * If we're out of space, disable receive interrupts
   1183 	 * until the queue has drained a bit.
   1184 	 */
   1185 	if (!cc) {
   1186 		SET(p->p_rx_flags, RX_IBUF_OVERFLOWED);
   1187 		CLR(p->p_srer, CD18xx_SRER_Rx |
   1188 			       CD18xx_SRER_RxSC |
   1189 			       CD18xx_SRER_CD);
   1190 		cd18xx_write(sc, CD18xx_SRER, p->p_srer);
   1191 	}
   1192 
   1193 	/* finish the interrupt transaction with the IC */
   1194 	cd18xx_write(sc, CD18xx_EOSRR, 0);
   1195 	DPRINTF(CDD_INTR, (", done\n"));
   1196 }
   1197 
   1198 /*
   1199  * transmitter interrupt
   1200  *
   1201  * note this relys on the fact that we allow the transmitter FIFO to
   1202  * drain completely
   1203  */
   1204 static inline void
   1205 cd18xx_tint(sc, ns)
   1206 	struct cd18xx_softc *sc;
   1207 	int *ns;
   1208 {
   1209 	struct cdtty_port *p;
   1210 	u_int channel;
   1211 
   1212 	/* work out the channel and softc */
   1213 	channel = cd18xx_get_gscr1_channel(sc);
   1214 	p = &sc->sc_ports[channel];
   1215 	DPRINTF(CDD_INTR, ("%s: tint: channel %d", sc->sc_dev.dv_xname,
   1216 	    channel));
   1217 	GOTINTR(sc, p);
   1218 
   1219 	/* if the current break condition is wrong, fix it */
   1220 	if (p->p_break != p->p_needbreak) {
   1221 		u_char buf[2];
   1222 
   1223 		DPRINTF(CDD_INTR, (", changing break to %d", p->p_needbreak));
   1224 
   1225 		/* turn on ETC processing */
   1226 		cd18xx_write(sc, CD18xx_COR2, p->p_cor2 | CD18xx_COR2_ETC);
   1227 
   1228 		buf[0] = CD18xx_TDR_ETC_BYTE;
   1229 		buf[1] = p->p_needbreak ? CD18xx_TDR_BREAK_BYTE :
   1230 					    CD18xx_TDR_NOBREAK_BYTE;
   1231 		cd18xx_write_multi(sc, CD18xx_TDR, buf, 2);
   1232 
   1233 		p->p_break = p->p_needbreak;
   1234 
   1235 		/* turn off ETC processing */
   1236 		cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
   1237 	}
   1238 
   1239 	/*
   1240 	 * If we've delayed a parameter change, do it now, and restart
   1241 	 * output.
   1242 	 */
   1243 	if (p->p_heldchange) {
   1244 		cdtty_loadchannelregs(sc, p);
   1245 		p->p_heldchange = 0;
   1246 		p->p_tbc = p->p_heldtbc;
   1247 		p->p_heldtbc = 0;
   1248 	}
   1249 
   1250 	/* Output the next chunk of the contiguous buffer, if any. */
   1251 	if (p->p_tbc > 0) {
   1252 		int n;
   1253 
   1254 		n = p->p_tbc;
   1255 		if (n > 8) /* write up to 8 entries */
   1256 			n = 8;
   1257 		DPRINTF(CDD_INTR, (", writing %d bytes to TDR", n));
   1258 		cd18xx_write_multi(sc, CD18xx_TDR, p->p_tba, n);
   1259 		p->p_tbc -= n;
   1260 		p->p_tba += n;
   1261 	}
   1262 
   1263 	/* Disable transmit completion interrupts if we ran out of bytes. */
   1264 	if (p->p_tbc == 0) {
   1265 		/* Note that Tx interrupts should already be enabled */
   1266 		if (ISSET(p->p_srer, CD18xx_SRER_Tx)) {
   1267 			DPRINTF(CDD_INTR, (", disabling tx interrupts"));
   1268 			CLR(p->p_srer, CD18xx_SRER_Tx);
   1269 			cd18xx_write(sc, CD18xx_SRER, p->p_srer);
   1270 		}
   1271 		if (p->p_tx_busy) {
   1272 			p->p_tx_busy = 0;
   1273 			p->p_tx_done = 1;
   1274 		}
   1275 	}
   1276 	*ns = 1;
   1277 
   1278 	/* finish the interrupt transaction with the IC */
   1279 	cd18xx_write(sc, CD18xx_EOSRR, 0);
   1280 	DPRINTF(CDD_INTR, (", done\n"));
   1281 }
   1282 
   1283 /* modem signal change interrupt */
   1284 static inline void
   1285 cd18xx_mint(sc, ns)
   1286 	struct cd18xx_softc *sc;
   1287 	int *ns;
   1288 {
   1289 	struct cdtty_port *p;
   1290 	u_int channel;
   1291 	u_char msvr, delta;
   1292 
   1293 	/* work out the channel and softc */
   1294 	channel = cd18xx_get_gscr1_channel(sc);
   1295 	p = &sc->sc_ports[channel];
   1296 	DPRINTF(CDD_INTR, ("%s: mint: channel %d", sc->sc_dev.dv_xname, channel));
   1297 	GOTINTR(sc, p);
   1298 
   1299 	/*
   1300 	 * We ignore the MCR register, and handle detecting deltas
   1301 	 * via software, like many other serial drivers.
   1302 	 */
   1303 	msvr = cd18xx_read(sc, CD18xx_MSVR);
   1304 	delta = msvr ^ p->p_msvr;
   1305 	DPRINTF(CDD_INTR, (", msvr %d", msvr));
   1306 
   1307 	/*
   1308 	 * Process normal status changes
   1309 	 */
   1310 	if (ISSET(delta, p->p_msvr_mask)) {
   1311 		SET(p->p_msvr_delta, delta);
   1312 
   1313 		DPRINTF(CDD_INTR, (", status changed delta %d", delta));
   1314 		/*
   1315 		 * Stop output immediately if we lose the output
   1316 		 * flow control signal or carrier detect.
   1317 		 */
   1318 		if (ISSET(~msvr, p->p_msvr_mask)) {
   1319 			p->p_tbc = 0;
   1320 			p->p_heldtbc = 0;
   1321 			/* Stop modem interrupt processing */
   1322 		}
   1323 		p->p_st_check = 1;
   1324 		*ns = 1;
   1325 	}
   1326 
   1327 	/* reset the modem signal register */
   1328 	cd18xx_write(sc, CD18xx_MCR, 0);
   1329 
   1330 	/* finish the interrupt transaction with the IC */
   1331 	cd18xx_write(sc, CD18xx_EOSRR, 0);
   1332 	DPRINTF(CDD_INTR, (", done\n"));
   1333 }
   1334 
   1335 /*
   1336  * hardware interrupt routine.  call the relevant interrupt routines until
   1337  * no interrupts are pending.
   1338  *
   1339  * note:  we do receive interrupts before all others (as we'd rather lose
   1340  * a chance to transmit, than lose a character).  and we do transmit
   1341  * interrupts before modem interrupts.
   1342  *
   1343  * we have to traverse all of the cd18xx's attached, unfortunately.
   1344  */
   1345 int
   1346 cd18xx_hardintr(v)
   1347 	void *v;
   1348 {
   1349 	int i, rv = 0;
   1350 	u_char ack;
   1351 
   1352 	DPRINTF(CDD_INTR, ("cd18xx_hardintr (ndevs %d):\n", clcd_cd.cd_ndevs));
   1353 	for (i = 0; i < clcd_cd.cd_ndevs; i++)
   1354 	{
   1355 		struct cd18xx_softc *sc = clcd_cd.cd_devs[i];
   1356 		int status, ns = 0;
   1357 		int count = 1;	/* process only 1 interrupts at a time for now */
   1358 
   1359 		if (sc == NULL)
   1360 			continue;
   1361 
   1362 		DPRINTF(CDD_INTR, ("%s:", sc->sc_dev.dv_xname));
   1363 		while (count-- &&
   1364 		    (status = (cd18xx_read(sc, CD18xx_SRSR) &
   1365 		     CD18xx_SRSR_PENDING))) {
   1366 			rv = 1;
   1367 
   1368 			DPRINTF(CDD_INTR, (" status %x:", status));
   1369 			if (ISSET(status, CD18xx_SRSR_RxPEND)) {
   1370 				ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
   1371 				    CD18xx_INTRACK_RxINT);
   1372 				DPRINTF(CDD_INTR, (" rx: ack1 %x\n", ack));
   1373 				cd18xx_rint(sc, &ns);
   1374 			}
   1375 			if (ISSET(status, CD18xx_SRSR_TxPEND)) {
   1376 				ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
   1377 				    CD18xx_INTRACK_TxINT);
   1378 				DPRINTF(CDD_INTR, (" tx: ack1 %x\n", ack));
   1379 				cd18xx_tint(sc, &ns);
   1380 
   1381 			}
   1382 			if (ISSET(status, CD18xx_SRSR_MxPEND)) {
   1383 				ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
   1384 				    CD18xx_INTRACK_MxINT);
   1385 				DPRINTF(CDD_INTR, (" mx: ack1 %x\n", ack));
   1386 				cd18xx_mint(sc, &ns);
   1387 			}
   1388 		}
   1389 		if (ns)
   1390 			softintr_schedule(sc->sc_si);
   1391 	}
   1392 
   1393 	return (rv);
   1394 }
   1395 
   1396 /*
   1397  * software interrupt
   1398  */
   1399 
   1400 void
   1401 cdtty_rxsoft(sc, p, tp)
   1402 	struct cd18xx_softc *sc;
   1403 	struct cdtty_port *p;
   1404 	struct tty *tp;
   1405 {
   1406 	u_char *get, *end;
   1407 	u_int cc, scc;
   1408 	u_char rcsr;
   1409 	int code;
   1410 	int s;
   1411 
   1412 	end = p->p_ebuf;
   1413 	get = p->p_rbget;
   1414 	scc = cc = cdtty_rbuf_size - p->p_rbavail;
   1415 
   1416 	if (cc == cdtty_rbuf_size) {
   1417 		p->p_floods++;
   1418 #if 0
   1419 		if (p->p_errors++ == 0)
   1420 			callout_reset(&p->p_diag_callout, 60 * hz,
   1421 			    cdttydiag, p);
   1422 #endif
   1423 	}
   1424 
   1425 	while (cc) {
   1426 		code = get[0];
   1427 		rcsr = get[1];
   1428 		if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR | CD18xx_RCSR_BREAK |
   1429 				CD18xx_RCSR_FRAMERR | CD18xx_RCSR_PARITYERR)) {
   1430 			if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR)) {
   1431 				p->p_overflows++;
   1432 #if 0
   1433 				if (p->p_errors++ == 0)
   1434 					callout_reset(&p->p_diag_callout,
   1435 					    60 * hz, cdttydiag, p);
   1436 #endif
   1437 			}
   1438 			if (ISSET(rcsr, CD18xx_RCSR_BREAK|CD18xx_RCSR_FRAMERR))
   1439 				SET(code, TTY_FE);
   1440 			if (ISSET(rcsr, CD18xx_RCSR_PARITYERR))
   1441 				SET(code, TTY_PE);
   1442 		}
   1443 		if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
   1444 			/*
   1445 			 * The line discipline's buffer is out of space.
   1446 			 */
   1447 			if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
   1448 				/*
   1449 				 * We're either not using flow control, or the
   1450 				 * line discipline didn't tell us to block for
   1451 				 * some reason.  Either way, we have no way to
   1452 				 * know when there's more space available, so
   1453 				 * just drop the rest of the data.
   1454 				 */
   1455 				get += cc << 1;
   1456 				if (get >= end)
   1457 					get -= cdtty_rbuf_size << 1;
   1458 				cc = 0;
   1459 			} else {
   1460 				/*
   1461 				 * Don't schedule any more receive processing
   1462 				 * until the line discipline tells us there's
   1463 				 * space available (through cdttyhwiflow()).
   1464 				 * Leave the rest of the data in the input
   1465 				 * buffer.
   1466 				 */
   1467 				SET(p->p_rx_flags, RX_TTY_OVERFLOWED);
   1468 			}
   1469 			break;
   1470 		}
   1471 		get += 2;
   1472 		if (get >= end)
   1473 			get = p->p_rbuf;
   1474 		cc--;
   1475 	}
   1476 
   1477 	if (cc != scc) {
   1478 		p->p_rbget = get;
   1479 		s = splserial();
   1480 
   1481 		cc = p->p_rbavail += scc - cc;
   1482 		/* Buffers should be ok again, release possible block. */
   1483 		if (cc >= p->p_r_lowat) {
   1484 			if (ISSET(p->p_rx_flags, RX_IBUF_OVERFLOWED)) {
   1485 				CLR(p->p_rx_flags, RX_IBUF_OVERFLOWED);
   1486 				cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
   1487 				SET(p->p_srer, CD18xx_SRER_Rx |
   1488 					       CD18xx_SRER_RxSC |
   1489 					       CD18xx_SRER_CD);
   1490 				cd18xx_write(sc, CD18xx_SRER, p->p_srer);
   1491 			}
   1492 			if (ISSET(p->p_rx_flags, RX_IBUF_BLOCKED)) {
   1493 				CLR(p->p_rx_flags, RX_IBUF_BLOCKED);
   1494 				cdtty_hwiflow(sc, p);
   1495 			}
   1496 		}
   1497 		splx(s);
   1498 	}
   1499 }
   1500 
   1501 void
   1502 cdtty_txsoft(sc, p, tp)
   1503 	struct cd18xx_softc *sc;
   1504 	struct cdtty_port *p;
   1505 	struct tty *tp;
   1506 {
   1507 
   1508 	CLR(tp->t_state, TS_BUSY);
   1509 	if (ISSET(tp->t_state, TS_FLUSH))
   1510 		CLR(tp->t_state, TS_FLUSH);
   1511 	else
   1512 		ndflush(&tp->t_outq, (int)(p->p_tba - tp->t_outq.c_cf));
   1513 	(*tp->t_linesw->l_start)(tp);
   1514 }
   1515 
   1516 void
   1517 cdtty_stsoft(sc, p, tp)
   1518 	struct cd18xx_softc *sc;
   1519 	struct cdtty_port *p;
   1520 	struct tty *tp;
   1521 {
   1522 	u_char msvr, delta;
   1523 	int s;
   1524 
   1525 	s = splserial();
   1526 	msvr = p->p_msvr;
   1527 	delta = p->p_msvr_delta;
   1528 	p->p_msvr_delta = 0;
   1529 	splx(s);
   1530 
   1531 	if (ISSET(delta, p->p_msvr_dcd)) {
   1532 		/*
   1533 		 * Inform the tty layer that carrier detect changed.
   1534 		 */
   1535 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msvr, CD18xx_MSVR_CD));
   1536 	}
   1537 
   1538 	if (ISSET(delta, p->p_msvr_cts)) {
   1539 		/* Block or unblock output according to flow control. */
   1540 		if (ISSET(msvr, p->p_msvr_cts)) {
   1541 			p->p_tx_stopped = 0;
   1542 			(*tp->t_linesw->l_start)(tp);
   1543 		} else {
   1544 			p->p_tx_stopped = 1;
   1545 		}
   1546 	}
   1547 }
   1548 
   1549 void
   1550 cd18xx_softintr(v)
   1551 	void *v;
   1552 {
   1553 	struct cd18xx_softc *sc = v;
   1554 	struct cdtty_port *p;
   1555 	struct tty *tp;
   1556 	int i;
   1557 
   1558 	for (i = 0; i < 8; i++) {
   1559 		p = &sc->sc_ports[i];
   1560 
   1561 		tp = p->p_tty;
   1562 		if (tp == NULL)
   1563 			continue;
   1564 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1565 			continue;
   1566 
   1567 		if (p->p_rx_ready) {
   1568 			p->p_rx_ready = 0;
   1569 			cdtty_rxsoft(sc, p, tp);
   1570 		}
   1571 
   1572 		if (p->p_st_check) {
   1573 			p->p_st_check = 0;
   1574 			cdtty_stsoft(sc, p, tp);
   1575 		}
   1576 
   1577 		if (p->p_tx_done) {
   1578 			p->p_tx_done = 0;
   1579 			cdtty_txsoft(sc, p, tp);
   1580 		}
   1581 	}
   1582 }
   1583