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