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