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