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