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