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