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