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