xref: /openbsd-src/sys/dev/ic/z8530tty.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: z8530tty.c,v 1.27 2016/09/04 18:20:34 tedu Exp $	*/
2 /*	$NetBSD: z8530tty.c,v 1.77 2001/05/30 15:24:24 lukem Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998, 1999
6  *	Charles M. Hannum.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Charles M. Hannum.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1994 Gordon W. Ross
36  * Copyright (c) 1992, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This software was developed by the Computer Systems Engineering group
40  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
41  * contributed to Berkeley.
42  *
43  * All advertising materials mentioning features or use of this software
44  * must display the following acknowledgement:
45  *	This product includes software developed by the University of
46  *	California, Lawrence Berkeley Laboratory.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
73  */
74 
75 /*
76  * Zilog Z8530 Dual UART driver (tty interface)
77  *
78  * This is the "slave" driver that will be attached to
79  * the "zsc" driver for plain "tty" async. serial lines.
80  *
81  * Credits, history:
82  *
83  * The original version of this code was the sparc/dev/zs.c driver
84  * as distributed with the Berkeley 4.4 Lite release.  Since then,
85  * Gordon Ross reorganized the code into the current parent/child
86  * driver scheme, separating the Sun keyboard and mouse support
87  * into independent child drivers.
88  *
89  * RTS/CTS flow-control support was a collaboration of:
90  *	Gordon Ross <gwr@NetBSD.org>,
91  *	Bill Studenmund <wrstuden@loki.stanford.edu>
92  *	Ian Dall <Ian.Dall@dsto.defence.gov.au>
93  *
94  * The driver was massively overhauled in November 1997 by Charles Hannum,
95  * fixing *many* bugs, and substantially improving performance.
96  */
97 
98 #include <sys/param.h>
99 #include <sys/systm.h>
100 #include <sys/proc.h>
101 #include <sys/device.h>
102 #include <sys/conf.h>
103 #include <sys/file.h>
104 #include <sys/ioctl.h>
105 #include <sys/malloc.h>
106 #include <sys/tty.h>
107 #include <sys/time.h>
108 #include <sys/kernel.h>
109 #include <sys/syslog.h>
110 
111 #include <dev/ic/z8530reg.h>
112 #include <machine/z8530var.h>
113 
114 #include <dev/cons.h>
115 
116 /*
117  * Allow the MD var.h to override the default CFLAG so that
118  * console messages during boot come out with correct parity.
119  */
120 #ifndef	ZSTTY_DEF_CFLAG
121 #define	ZSTTY_DEF_CFLAG	TTYDEF_CFLAG
122 #endif
123 
124 /*
125  * How many input characters we can buffer.
126  * The port-specific var.h may override this.
127  * Note: must be a power of two!
128  */
129 #ifndef	ZSTTY_RING_SIZE
130 #define	ZSTTY_RING_SIZE	2048
131 #endif
132 
133 struct cfdriver zstty_cd = {
134 	NULL, "zstty", DV_TTY
135 };
136 
137 /*
138  * Make this an option variable one can patch.
139  * But be warned:  this must be a power of 2!
140  */
141 u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
142 
143 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
144 u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
145 u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
146 
147 struct zstty_softc {
148 	struct	device zst_dev;		/* required first: base device */
149 	struct  tty *zst_tty;
150 	struct	zs_chanstate *zst_cs;
151 
152 	struct timeout zst_diag_ch;
153 
154 	u_int zst_overflows,
155 	      zst_floods,
156 	      zst_errors;
157 
158 	int zst_hwflags,	/* see z8530var.h */
159 	    zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
160 
161 	u_int zst_r_hiwat,
162 	      zst_r_lowat;
163 	uint8_t *volatile zst_rbget,
164 		*volatile zst_rbput;
165 	volatile u_int zst_rbavail;
166 	uint8_t *zst_rbuf,
167 		*zst_ebuf;
168 
169 	/*
170 	 * The transmit byte count and address are used for pseudo-DMA
171 	 * output in the hardware interrupt code.  PDMA can be suspended
172 	 * to get pending changes done; heldtbc is used for this.  It can
173 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
174 	 */
175 	uint8_t *zst_tba;		/* transmit buffer address */
176 	u_int zst_tbc,			/* transmit byte count */
177 	      zst_heldtbc;		/* held tbc while xmission stopped */
178 
179 	/* Flags to communicate with zstty_softint() */
180 	volatile uint8_t zst_rx_flags,	/* receiver blocked */
181 #define	RX_TTY_BLOCKED		0x01
182 #define	RX_TTY_OVERFLOWED	0x02
183 #define	RX_IBUF_BLOCKED		0x04
184 #define	RX_IBUF_OVERFLOWED	0x08
185 #define	RX_ANY_BLOCK		0x0f
186 			zst_tx_busy,	/* working on an output chunk */
187 			zst_tx_done,	/* done with one output chunk */
188 			zst_tx_stopped,	/* H/W level stop (lost CTS) */
189 			zst_st_check,	/* got a status interrupt */
190 			zst_rx_ready;
191 
192 	/* PPS signal on DCD, with or without inkernel clock disciplining */
193 	uint8_t  zst_ppsmask;			/* pps signal mask */
194 	uint8_t  zst_ppsassert;			/* pps leading edge */
195 	uint8_t  zst_ppsclear;			/* pps trailing edge */
196 };
197 
198 /* Definition of the driver for autoconfig. */
199 int	zstty_match(struct device *, void *, void *);
200 void	zstty_attach(struct device *, struct device *, void *);
201 
202 const struct cfattach zstty_ca = {
203 	sizeof(struct zstty_softc), zstty_match, zstty_attach
204 };
205 
206 cdev_decl(zs);
207 
208 struct zsops zsops_tty;
209 
210 void zs_shutdown(struct zstty_softc *);
211 void	zsstart(struct tty *);
212 int	zsparam(struct tty *, struct termios *);
213 void zs_modem(struct zstty_softc *, int);
214 void tiocm_to_zs(struct zstty_softc *, u_long, int);
215 int  zs_to_tiocm(struct zstty_softc *);
216 int    zshwiflow(struct tty *, int);
217 void  zs_hwiflow(struct zstty_softc *);
218 void zs_maskintr(struct zstty_softc *);
219 
220 struct zstty_softc *zs_device_lookup(struct cfdriver *, int);
221 
222 /* Low-level routines. */
223 void zstty_rxint(struct zs_chanstate *);
224 void zstty_stint(struct zs_chanstate *, int);
225 void zstty_txint(struct zs_chanstate *);
226 void zstty_softint(struct zs_chanstate *);
227 void zstty_diag(void *);
228 
229 #define	ZSUNIT(x)	(minor(x) & 0x7f)
230 #define	ZSDIALOUT(x)	(minor(x) & 0x80)
231 
232 struct zstty_softc *
233 zs_device_lookup(struct cfdriver *cf, int unit)
234 {
235 	return (struct zstty_softc *)device_lookup(cf, unit);
236 }
237 
238 /*
239  * zstty_match: how is this zs channel configured?
240  */
241 int
242 zstty_match(struct device *parent, void *vcf, void *aux)
243 {
244 	struct cfdata *cf = vcf;
245 	struct zsc_attach_args *args = aux;
246 
247 	/* Exact match is better than wildcard. */
248 	if (cf->cf_loc[0] == args->channel)
249 		return 2;
250 
251 	/* This driver accepts wildcard. */
252 	if (cf->cf_loc[0] == -1)
253 		return 1;
254 
255 	return 0;
256 }
257 
258 void
259 zstty_attach(struct device *parent, struct device *self, void *aux)
260 {
261 	struct zsc_softc *zsc = (struct zsc_softc *)parent;
262 	struct zstty_softc *zst = (struct zstty_softc *)self;
263 	struct cfdata *cf = self->dv_cfdata;
264 	struct zsc_attach_args *args = aux;
265 	struct zs_chanstate *cs;
266 	struct tty *tp;
267 	int channel, s, tty_unit;
268 	dev_t dev;
269 	const char *i, *o;
270 	int dtr_on;
271 	int resetbit;
272 
273 	timeout_set(&zst->zst_diag_ch, zstty_diag, zst);
274 
275 	tty_unit = zst->zst_dev.dv_unit;
276 	channel = args->channel;
277 	cs = zsc->zsc_cs[channel];
278 	cs->cs_private = zst;
279 	cs->cs_ops = &zsops_tty;
280 
281 	zst->zst_cs = cs;
282 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
283 	zst->zst_hwflags = args->hwflags;
284 	dev = makedev(zs_major, tty_unit);
285 
286 	if (zst->zst_swflags)
287 		printf(" flags 0x%x", zst->zst_swflags);
288 
289 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_NO_DCD))
290 		SET(zst->zst_swflags, TIOCFLAG_SOFTCAR);
291 
292 	/*
293 	 * Check whether we serve as a console device.
294 	 * XXX - split console input/output channels aren't
295 	 *	 supported yet on /dev/console
296 	 */
297 	i = o = NULL;
298 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
299 		i = " input";
300 		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
301 			args->consdev->cn_dev = dev;
302 			cn_tab->cn_pollc = args->consdev->cn_pollc;
303 			cn_tab->cn_getc = args->consdev->cn_getc;
304 		}
305 		cn_tab->cn_dev = dev;
306 	}
307 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
308 		o = " output";
309 		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
310 			cn_tab->cn_putc = args->consdev->cn_putc;
311 		}
312 		cn_tab->cn_dev = dev;
313 	}
314 	if (i != NULL || o != NULL) {
315 		printf(": console%s", i ? (o ? "" : i) : o);
316 	}
317 
318 #ifdef KGDB
319 	if (zs_check_kgdb(cs, dev)) {
320 		/*
321 		 * Allow kgdb to "take over" this port.  Returns true
322 		 * if this serial port is in-use by kgdb.
323 		 */
324 		printf(": kgdb\n");
325 		/*
326 		 * This is the kgdb port (exclusive use)
327 		 * so skip the normal attach code.
328 		 */
329 		return;
330 	}
331 #endif
332 
333 #if defined(__sparc64__)
334 	if (strcmp(args->type, "keyboard") == 0 ||
335 	    strcmp(args->type, "mouse") == 0)
336 		printf(": %s", args->type);
337 #endif
338 
339 	printf("\n");
340 
341 	tp = ttymalloc(0);
342 	tp->t_dev = dev;
343 	tp->t_oproc = zsstart;
344 	tp->t_param = zsparam;
345 	tp->t_hwiflow = zshwiflow;
346 
347 	zst->zst_tty = tp;
348 	zst->zst_rbuf = mallocarray(zstty_rbuf_size, 2, M_DEVBUF, M_WAITOK);
349 	zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size * 2);
350 	/* Disable the high water mark. */
351 	zst->zst_r_hiwat = 0;
352 	zst->zst_r_lowat = 0;
353 	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
354 	zst->zst_rbavail = zstty_rbuf_size;
355 
356 	/* if there are no enable/disable functions, assume the device
357 	   is always enabled */
358 	if (!cs->enable)
359 		cs->enabled = 1;
360 
361 	/*
362 	 * Hardware init
363 	 */
364 	dtr_on = 0;
365 	resetbit = 0;
366 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
367 		/* Call zsparam similar to open. */
368 		struct termios t;
369 
370 		/* Wait a while for previous console output to complete */
371 		DELAY(10000);
372 
373 		/* Setup the "new" parameters in t. */
374 		t.c_ispeed = 0;
375 		t.c_ospeed = cs->cs_defspeed;
376 		t.c_cflag = cs->cs_defcflag;
377 
378 		s = splzs();
379 
380 		/*
381 		 * Turn on receiver and status interrupts.
382 		 * We defer the actual write of the register to zsparam(),
383 		 * but we must make sure status interrupts are turned on by
384 		 * the time zsparam() reads the initial rr0 state.
385 		 */
386 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
387 
388 		splx(s);
389 
390 		/* Make sure zsparam will see changes. */
391 		tp->t_ospeed = 0;
392 		(void)zsparam(tp, &t);
393 
394 		/* Make sure DTR is on now. */
395 		dtr_on = 1;
396 	} else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
397 		/* Not the console; may need reset. */
398 		resetbit = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
399 	}
400 
401 	s = splzs();
402 	if (resetbit)
403 		zs_write_reg(cs, 9, resetbit);
404 	zs_modem(zst, dtr_on);
405 	splx(s);
406 }
407 
408 
409 /*
410  * Return pointer to our tty.
411  */
412 struct tty *
413 zstty(dev_t dev)
414 {
415 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
416 
417 	return (zst->zst_tty);
418 }
419 
420 
421 void
422 zs_shutdown(struct zstty_softc *zst)
423 {
424 	struct zs_chanstate *cs = zst->zst_cs;
425 	struct tty *tp = zst->zst_tty;
426 	int s;
427 
428 	s = splzs();
429 
430 	/* If we were asserting flow control, then deassert it. */
431 	SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
432 	zs_hwiflow(zst);
433 
434 	/* Clear any break condition set with TIOCSBRK. */
435 	zs_break(cs, 0);
436 
437 	/* Turn off PPS capture on last close. */
438 	zst->zst_ppsmask = 0;
439 
440 	/*
441 	 * Hang up if necessary.  Wait a bit, so the other side has time to
442 	 * notice even if we immediately open the port again.
443 	 */
444 	if (ISSET(tp->t_cflag, HUPCL) || ISSET(tp->t_state, TS_WOPEN)) {
445 		zs_modem(zst, 0);
446 		/* hold low for 1 second */
447 		(void)tsleep(cs, TTIPRI, ttclos, hz);
448 	}
449 
450 	/* Turn off interrupts if not the console. */
451 	if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
452 		CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
453 		cs->cs_creg[1] = cs->cs_preg[1];
454 		zs_write_reg(cs, 1, cs->cs_creg[1]);
455 	}
456 
457 	/* Call the power management hook. */
458 	if (cs->disable) {
459 #ifdef DIAGNOSTIC
460 		if (!cs->enabled)
461 			panic("%s: not enabled?", __func__);
462 #endif
463 		(*cs->disable)(zst->zst_cs);
464 	}
465 
466 	splx(s);
467 }
468 
469 /*
470  * Open a zs serial (tty) port.
471  */
472 int
473 zsopen(dev_t dev, int flags, int mode, struct proc *p)
474 {
475 	struct zstty_softc *zst;
476 	struct zs_chanstate *cs;
477 	struct tty *tp;
478 	int s;
479 #if IPL_ZS != IPL_TTY
480 	int s2;
481 #endif
482 	int error;
483 
484 	zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
485 	if (zst == NULL)
486 		return (ENXIO);
487 
488 	tp = zst->zst_tty;
489 	cs = zst->zst_cs;
490 
491 	/* If KGDB took the line, then tp==NULL */
492 	if (tp == NULL)
493 		return (EBUSY);
494 
495 	if (ISSET(tp->t_state, TS_ISOPEN) &&
496 	    ISSET(tp->t_state, TS_XCLUDE) &&
497 	    suser(p, 0) != 0)
498 		return (EBUSY);
499 
500 	s = spltty();
501 
502 	/*
503 	 * Do the following iff this is a first open.
504 	 */
505 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
506 		struct termios t;
507 
508 		tp->t_dev = dev;
509 
510 		/* Call the power management hook. */
511 		if (cs->enable) {
512 			if ((*cs->enable)(cs)) {
513 				splx(s);
514 				printf("%s: device enable failed\n",
515 				    zst->zst_dev.dv_xname);
516 				return (EIO);
517 			}
518 		}
519 
520 		/*
521 		 * Initialize the termios status to the defaults.  Add in the
522 		 * sticky bits from TIOCSFLAGS.
523 		 */
524 		t.c_ispeed = 0;
525 		t.c_ospeed = cs->cs_defspeed;
526 		t.c_cflag = cs->cs_defcflag;
527 		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
528 			SET(t.c_cflag, CLOCAL);
529 		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
530 			SET(t.c_cflag, CRTSCTS);
531 		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
532 			SET(t.c_cflag, MDMBUF);
533 
534 #if IPL_ZS != IPL_TTY
535 		s2 = splzs();
536 #endif
537 
538 		/*
539 		 * Turn on receiver and status interrupts.
540 		 * We defer the actual write of the register to zsparam(),
541 		 * but we must make sure status interrupts are turned on by
542 		 * the time zsparam() reads the initial rr0 state.
543 		 */
544 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
545 
546 		/* Clear PPS capture state on first open. */
547 		zst->zst_ppsmask = 0;
548 
549 #if IPL_ZS != IPL_TTY
550 		splx(s2);
551 #endif
552 
553 		/* Make sure zsparam will see changes. */
554 		tp->t_ospeed = 0;
555 		(void)zsparam(tp, &t);
556 
557 		/*
558 		 * Note: zsparam has done: cflag, ispeed, ospeed
559 		 * so we just need to do: iflag, oflag, lflag, cc
560 		 * For "raw" mode, just leave all zeros.
561 		 */
562 		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
563 			tp->t_iflag = TTYDEF_IFLAG;
564 			tp->t_oflag = TTYDEF_OFLAG;
565 			tp->t_lflag = TTYDEF_LFLAG;
566 		} else {
567 			tp->t_iflag = 0;
568 			tp->t_oflag = 0;
569 			tp->t_lflag = 0;
570 		}
571 		ttychars(tp);
572 		ttsetwater(tp);
573 
574 		if (ZSDIALOUT(dev))
575 			SET(tp->t_state, TS_CARR_ON);
576 		else
577 			CLR(tp->t_state, TS_CARR_ON);
578 
579 #if IPL_ZS != IPL_TTY
580 		s2 = splzs();
581 #endif
582 
583 		/* Clear the input ring, and unblock. */
584 		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
585 		zst->zst_rbavail = zstty_rbuf_size;
586 		zs_iflush(cs);
587 		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
588 		zs_hwiflow(zst);
589 
590 #if IPL_ZS != IPL_TTY
591 		splx(s2);
592 #endif
593 	}
594 
595 	if (ZSDIALOUT(dev)) {
596 		if (ISSET(tp->t_state, TS_ISOPEN)) {
597 			/* someone already is dialed in... */
598 			splx(s);
599 			return EBUSY;
600 		}
601 		cs->cs_cua = 1;
602 	}
603 
604 	error = 0;
605 	/* wait for carrier if necessary */
606 	if (ISSET(flags, O_NONBLOCK)) {
607 		if (!ZSDIALOUT(dev) && cs->cs_cua) {
608 			/* Opening TTY non-blocking... but the CUA is busy */
609 			error = EBUSY;
610 		}
611 	} else
612 	  while (cs->cs_cua ||
613 	    (!ISSET(tp->t_cflag, CLOCAL) && !ISSET(tp->t_state, TS_CARR_ON))) {
614 		int rr0;
615 
616 		error = 0;
617 		SET(tp->t_state, TS_WOPEN);
618 
619 		if (!ZSDIALOUT(dev) && !cs->cs_cua) {
620 			/*
621 			 * Turn on DTR.  We must always do this on non-CUA
622 			 * devices, even if carrier is not present, because
623 			 * otherwise we'd have to use TIOCSDTR immediately
624 			 * after setting CLOCAL, which applications do not
625 			 * expect.  We always assert DTR while the device is
626 			 * open unless explicitly requested to deassert it.
627 			 */
628 #if IPL_ZS != IPL_TTY
629 			s2 = splzs();
630 #endif
631 			zs_modem(zst, 1);
632 			rr0 = zs_read_csr(cs);
633 #if IPL_ZS != IPL_TTY
634 			splx(s2);
635 #endif
636 
637 			/* loop, turning on the device, until carrier present */
638 			if (ISSET(rr0, ZSRR0_DCD) ||
639 			    ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR))
640 				SET(tp->t_state, TS_CARR_ON);
641 		}
642 
643 		if ((ISSET(tp->t_cflag, CLOCAL) ||
644 		    ISSET(tp->t_state, TS_CARR_ON)) && !cs->cs_cua)
645 			break;
646 
647 		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
648 		    ttopen, 0);
649 
650 		if (!ZSDIALOUT(dev) && cs->cs_cua && error == EINTR) {
651 			error = 0;
652 			continue;
653 		}
654 
655 		if (error) {
656 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
657 #if IPL_ZS != IPL_TTY
658 				s2 = splzs();
659 #endif
660 				zs_modem(zst, 0);
661 #if IPL_ZS != IPL_TTY
662 				splx(s2);
663 #endif
664 				CLR(tp->t_state, TS_WOPEN);
665 				ttwakeup(tp);
666 			}
667 			if (ZSDIALOUT(dev))
668 				cs->cs_cua = 0;
669 			CLR(tp->t_state, TS_WOPEN);
670 			break;
671 		}
672 		if (!ZSDIALOUT(dev) && cs->cs_cua)
673 			continue;
674 	}
675 
676 	splx(s);
677 
678 	if (error == 0)
679 		error = ((*linesw[tp->t_line].l_open)(dev, tp, p));
680 	if (error)
681 		goto bad;
682 
683 	return (0);
684 
685 bad:
686 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
687 		/*
688 		 * We failed to open the device, and nobody else had it opened.
689 		 * Clean up the state as appropriate.
690 		 */
691 		zs_shutdown(zst);
692 	}
693 
694 	return (error);
695 }
696 
697 /*
698  * Close a zs serial port.
699  */
700 int
701 zsclose(dev_t dev, int flags, int mode, struct proc *p)
702 {
703 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
704 	struct zs_chanstate *cs = zst->zst_cs;
705 	struct tty *tp = zst->zst_tty;
706 	int s;
707 
708 	/* XXX This is for cons.c. */
709 	if (!ISSET(tp->t_state, TS_ISOPEN))
710 		return 0;
711 
712 	(*linesw[tp->t_line].l_close)(tp, flags, p);
713 
714 	s = spltty();
715 	cs->cs_cua = 0;
716 	ttyclose(tp);
717 	splx(s);
718 
719 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
720 		/*
721 		 * Although we got a last close, the device may still be in
722 		 * use; e.g. if this was the dialout node, and there are still
723 		 * processes waiting for carrier on the non-dialout node.
724 		 */
725 		zs_shutdown(zst);
726 	}
727 
728 	return (0);
729 }
730 
731 /*
732  * Read/write zs serial port.
733  */
734 int
735 zsread(dev_t dev, struct uio *uio, int flags)
736 {
737 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
738 	struct tty *tp = zst->zst_tty;
739 
740 	return (*linesw[tp->t_line].l_read)(tp, uio, flags);
741 }
742 
743 int
744 zswrite(dev_t dev, struct uio *uio, int flags)
745 {
746 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
747 	struct tty *tp = zst->zst_tty;
748 
749 	return (*linesw[tp->t_line].l_write)(tp, uio, flags);
750 }
751 
752 int
753 zsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
754 {
755 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
756 	struct zs_chanstate *cs = zst->zst_cs;
757 	struct tty *tp = zst->zst_tty;
758 	int error;
759 	int s;
760 
761 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
762 	if (error >= 0)
763 		return (error);
764 
765 	error = ttioctl(tp, cmd, data, flag, p);
766 	if (error >= 0)
767 		return (error);
768 
769 #ifdef	ZS_MD_IOCTL
770 	error = ZS_MD_IOCTL;
771 	if (error >= 0)
772 		return (error);
773 #endif	/* ZS_MD_IOCTL */
774 
775 	error = 0;
776 
777 	s = splzs();
778 
779 	switch (cmd) {
780 	case TIOCSBRK:
781 		zs_break(cs, 1);
782 		break;
783 
784 	case TIOCCBRK:
785 		zs_break(cs, 0);
786 		break;
787 
788 	case TIOCGFLAGS:
789 		*(int *)data = zst->zst_swflags;
790 		break;
791 
792 	case TIOCSFLAGS:
793 		error = suser(p, 0);
794 		if (error)
795 			break;
796 		zst->zst_swflags = *(int *)data;
797 		if (ISSET(zst->zst_hwflags, ZS_HWFLAG_NO_DCD))
798 			SET(zst->zst_swflags, TIOCFLAG_SOFTCAR);
799 		break;
800 
801 	case TIOCSDTR:
802 		zs_modem(zst, 1);
803 		break;
804 
805 	case TIOCCDTR:
806 		zs_modem(zst, 0);
807 		break;
808 
809 	case TIOCMSET:
810 	case TIOCMBIS:
811 	case TIOCMBIC:
812 		tiocm_to_zs(zst, cmd, *(int *)data);
813 		break;
814 
815 	case TIOCMGET:
816 		*(int *)data = zs_to_tiocm(zst);
817 		break;
818 
819 	default:
820 		error = ENOTTY;
821 		break;
822 	}
823 
824 	splx(s);
825 
826 	return (error);
827 }
828 
829 /*
830  * Start or restart transmission.
831  */
832 void
833 zsstart(struct tty *tp)
834 {
835 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
836 	struct zs_chanstate *cs = zst->zst_cs;
837 	u_char *tba;
838 	int tbc, rr0;
839 	int s;
840 
841 	s = spltty();
842 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
843 		goto out;
844 	if (zst->zst_tx_stopped)
845 		goto out;
846 
847 	ttwakeupwr(tp);
848 	if (tp->t_outq.c_cc == 0)
849 		goto out;
850 
851 	/* Grab the first contiguous region of buffer space. */
852 	tba = tp->t_outq.c_cf;
853 	tbc = ndqb(&tp->t_outq, 0);
854 
855 #if IPL_ZS != IPL_TTY
856 	(void)splzs();
857 #endif
858 
859 	zst->zst_tba = tba;
860 	zst->zst_tbc = tbc;
861 	SET(tp->t_state, TS_BUSY);
862 	zst->zst_tx_busy = 1;
863 
864 	do {
865 		rr0 = zs_read_csr(cs);
866 		if ((rr0 & ZSRR0_TX_READY) == 0)
867 			break;
868 
869 		zs_write_data(cs, *zst->zst_tba);
870 		zst->zst_tbc--;
871 		zst->zst_tba++;
872 	} while (zst->zst_tbc > 0);
873 
874 out:
875 	splx(s);
876 }
877 
878 /*
879  * Stop output, e.g., for ^S or output flush.
880  */
881 int
882 zsstop(struct tty *tp, int flag)
883 {
884 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
885 	int s;
886 
887 	s = splzs();
888 	if (ISSET(tp->t_state, TS_BUSY)) {
889 		/* Stop transmitting at the next chunk. */
890 		zst->zst_tbc = 0;
891 		zst->zst_heldtbc = 0;
892 		if (!ISSET(tp->t_state, TS_TTSTOP))
893 			SET(tp->t_state, TS_FLUSH);
894 	}
895 	splx(s);
896 	return 0;
897 }
898 
899 /*
900  * Set ZS tty parameters from termios.
901  * XXX - Should just copy the whole termios after
902  * making sure all the changes could be done.
903  */
904 int
905 zsparam(struct tty *tp, struct termios *t)
906 {
907 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
908 	struct zs_chanstate *cs = zst->zst_cs;
909 	int ospeed;
910 	tcflag_t cflag;
911 	uint8_t tmp3, tmp4, tmp5;
912 	int s, error;
913 
914 	ospeed = t->c_ospeed;
915 	cflag = t->c_cflag;
916 
917 	/* Check requested parameters. */
918 	if (ospeed < 0)
919 		return (EINVAL);
920 	if (t->c_ispeed && t->c_ispeed != ospeed)
921 		return (EINVAL);
922 
923 	/*
924 	 * For the console, always force CLOCAL and !HUPCL, so that the port
925 	 * is always active.
926 	 */
927 	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
928 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
929 		SET(cflag, CLOCAL);
930 		CLR(cflag, HUPCL);
931 	}
932 
933 	/*
934 	 * Only whack the UART when params change.
935 	 * Some callers need to clear tp->t_ospeed
936 	 * to make sure initialization gets done.
937 	 */
938 	if (tp->t_ospeed == ospeed &&
939 	    tp->t_cflag == cflag)
940 		return (0);
941 
942 	/*
943 	 * Call MD functions to deal with changed
944 	 * clock modes or H/W flow control modes.
945 	 * The BRG divisor is set now. (reg 12,13)
946 	 */
947 	error = zs_set_speed(cs, ospeed);
948 	if (error)
949 		return (error);
950 	error = zs_set_modes(cs, cflag);
951 	if (error)
952 		return (error);
953 
954 	/*
955 	 * Block interrupts so that state will not
956 	 * be altered until we are done setting it up.
957 	 *
958 	 * Initial values in cs_preg are set before
959 	 * our attach routine is called.  The master
960 	 * interrupt enable is handled by zsc.c
961 	 *
962 	 */
963 	s = splzs();
964 
965 	/*
966 	 * Recalculate which status ints to enable.
967 	 */
968 	zs_maskintr(zst);
969 
970 	/* Recompute character size bits. */
971 	tmp3 = cs->cs_preg[3];
972 	tmp5 = cs->cs_preg[5];
973 	CLR(tmp3, ZSWR3_RXSIZE);
974 	CLR(tmp5, ZSWR5_TXSIZE);
975 	switch (ISSET(cflag, CSIZE)) {
976 	case CS5:
977 		SET(tmp3, ZSWR3_RX_5);
978 		SET(tmp5, ZSWR5_TX_5);
979 		break;
980 	case CS6:
981 		SET(tmp3, ZSWR3_RX_6);
982 		SET(tmp5, ZSWR5_TX_6);
983 		break;
984 	case CS7:
985 		SET(tmp3, ZSWR3_RX_7);
986 		SET(tmp5, ZSWR5_TX_7);
987 		break;
988 	case CS8:
989 		SET(tmp3, ZSWR3_RX_8);
990 		SET(tmp5, ZSWR5_TX_8);
991 		break;
992 	}
993 	cs->cs_preg[3] = tmp3;
994 	cs->cs_preg[5] = tmp5;
995 
996 	/*
997 	 * Recompute the stop bits and parity bits.  Note that
998 	 * zs_set_speed() may have set clock selection bits etc.
999 	 * in wr4, so those must preserved.
1000 	 */
1001 	tmp4 = cs->cs_preg[4];
1002 	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
1003 	if (ISSET(cflag, CSTOPB))
1004 		SET(tmp4, ZSWR4_TWOSB);
1005 	else
1006 		SET(tmp4, ZSWR4_ONESB);
1007 	if (!ISSET(cflag, PARODD))
1008 		SET(tmp4, ZSWR4_EVENP);
1009 	if (ISSET(cflag, PARENB))
1010 		SET(tmp4, ZSWR4_PARENB);
1011 	cs->cs_preg[4] = tmp4;
1012 
1013 	/* And copy to tty. */
1014 	tp->t_ispeed = 0;
1015 	tp->t_ospeed = ospeed;
1016 	tp->t_cflag = cflag;
1017 
1018 	/*
1019 	 * If nothing is being transmitted, set up new current values,
1020 	 * else mark them as pending.
1021 	 */
1022 	if (!cs->cs_heldchange) {
1023 		if (zst->zst_tx_busy) {
1024 			zst->zst_heldtbc = zst->zst_tbc;
1025 			zst->zst_tbc = 0;
1026 			cs->cs_heldchange = 1;
1027 		} else
1028 			zs_loadchannelregs(cs);
1029 	}
1030 
1031 	/*
1032 	 * If hardware flow control is disabled, turn off the buffer water
1033 	 * marks and unblock any soft flow control state.  Otherwise, enable
1034 	 * the water marks.
1035 	 */
1036 	if (!ISSET(cflag, CHWFLOW)) {
1037 		zst->zst_r_hiwat = 0;
1038 		zst->zst_r_lowat = 0;
1039 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1040 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1041 			zst->zst_rx_ready = 1;
1042 			cs->cs_softreq = 1;
1043 		}
1044 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1045 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1046 			zs_hwiflow(zst);
1047 		}
1048 	} else {
1049 		zst->zst_r_hiwat = zstty_rbuf_hiwat;
1050 		zst->zst_r_lowat = zstty_rbuf_lowat;
1051 	}
1052 
1053 	/*
1054 	 * Force a recheck of the hardware carrier and flow control status,
1055 	 * since we may have changed which bits we're looking at.
1056 	 */
1057 	zstty_stint(cs, 1);
1058 
1059 	splx(s);
1060 
1061 	/*
1062 	 * If hardware flow control is disabled, unblock any hard flow control
1063 	 * state.
1064 	 */
1065 	if (!ISSET(cflag, CHWFLOW)) {
1066 		if (zst->zst_tx_stopped) {
1067 			zst->zst_tx_stopped = 0;
1068 			zsstart(tp);
1069 		}
1070 	}
1071 
1072 	zstty_softint(cs);
1073 
1074 	return (0);
1075 }
1076 
1077 /*
1078  * Compute interrupt enable bits and set in the pending bits. Called both
1079  * in zsparam() and when PPS (pulse per second timing) state changes.
1080  * Must be called at splzs().
1081  */
1082 void
1083 zs_maskintr(struct zstty_softc *zst)
1084 {
1085 	struct zs_chanstate *cs = zst->zst_cs;
1086 	uint8_t tmp15;
1087 
1088 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
1089 	if (zst->zst_ppsmask != 0)
1090 		cs->cs_rr0_mask |= cs->cs_rr0_pps;
1091 	tmp15 = cs->cs_preg[15];
1092 	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
1093 		SET(tmp15, ZSWR15_DCD_IE);
1094 	else
1095 		CLR(tmp15, ZSWR15_DCD_IE);
1096 	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
1097 		SET(tmp15, ZSWR15_CTS_IE);
1098 	else
1099 		CLR(tmp15, ZSWR15_CTS_IE);
1100 	cs->cs_preg[15] = tmp15;
1101 }
1102 
1103 
1104 /*
1105  * Raise or lower modem control (DTR/RTS) signals.  If a character is
1106  * in transmission, the change is deferred.
1107  * Called at splzs().
1108  */
1109 void
1110 zs_modem(struct zstty_softc *zst, int onoff)
1111 {
1112 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1113 
1114 	if (cs->cs_wr5_dtr == 0)
1115 		return;
1116 
1117 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1118 
1119 	if (onoff)
1120 		SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
1121 	else
1122 		CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
1123 
1124 	if (!cs->cs_heldchange) {
1125 		if (zst->zst_tx_busy) {
1126 			zst->zst_heldtbc = zst->zst_tbc;
1127 			zst->zst_tbc = 0;
1128 			cs->cs_heldchange = 1;
1129 		} else
1130 			zs_loadchannelregs(cs);
1131 	}
1132 }
1133 
1134 /*
1135  * Set modem bits.
1136  * Called at splzs().
1137  */
1138 void
1139 tiocm_to_zs(struct zstty_softc *zst, u_long how, int ttybits)
1140 {
1141 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1142 	uint8_t zsbits;
1143 
1144 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1145 
1146 	zsbits = 0;
1147 	if (ISSET(ttybits, TIOCM_DTR))
1148 		SET(zsbits, ZSWR5_DTR);
1149 	if (ISSET(ttybits, TIOCM_RTS))
1150 		SET(zsbits, ZSWR5_RTS);
1151 
1152 	switch (how) {
1153 	case TIOCMBIC:
1154 		CLR(ccs->cs_preg[5], zsbits);
1155 		break;
1156 
1157 	case TIOCMBIS:
1158 		SET(ccs->cs_preg[5], zsbits);
1159 		break;
1160 
1161 	case TIOCMSET:
1162 		CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
1163 		SET(ccs->cs_preg[5], zsbits);
1164 		break;
1165 	}
1166 
1167 	if (!cs->cs_heldchange) {
1168 		if (zst->zst_tx_busy) {
1169 			zst->zst_heldtbc = zst->zst_tbc;
1170 			zst->zst_tbc = 0;
1171 			cs->cs_heldchange = 1;
1172 		} else
1173 			zs_loadchannelregs(cs);
1174 	}
1175 }
1176 
1177 /*
1178  * Get modem bits.
1179  * Called at splzs().
1180  */
1181 int
1182 zs_to_tiocm(struct zstty_softc *zst)
1183 {
1184 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1185 	uint8_t zsbits;
1186 	int ttybits = 0;
1187 
1188 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1189 
1190 	zsbits = ccs->cs_preg[5];
1191 	if (ISSET(zsbits, ZSWR5_DTR))
1192 		SET(ttybits, TIOCM_DTR);
1193 	if (ISSET(zsbits, ZSWR5_RTS))
1194 		SET(ttybits, TIOCM_RTS);
1195 
1196 	zsbits = cs->cs_rr0;
1197 	if (ISSET(zsbits, ZSRR0_DCD))
1198 		SET(ttybits, TIOCM_CD);
1199 	if (ISSET(zsbits, ZSRR0_CTS))
1200 		SET(ttybits, TIOCM_CTS);
1201 
1202 	return (ttybits);
1203 }
1204 
1205 /*
1206  * Try to block or unblock input using hardware flow-control.
1207  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
1208  * if this function returns non-zero, the TS_TBLOCK flag will
1209  * be set or cleared according to the "block" arg passed.
1210  */
1211 int
1212 zshwiflow(struct tty *tp, int block)
1213 {
1214 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
1215 	struct zs_chanstate *cs = zst->zst_cs;
1216 	int s;
1217 
1218 	if (cs->cs_wr5_rts == 0)
1219 		return (0);
1220 
1221 	s = splzs();
1222 	if (block) {
1223 		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1224 			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
1225 			zs_hwiflow(zst);
1226 		}
1227 	} else {
1228 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1229 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1230 			zst->zst_rx_ready = 1;
1231 			cs->cs_softreq = 1;
1232 		}
1233 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1234 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
1235 			zs_hwiflow(zst);
1236 		}
1237 	}
1238 	splx(s);
1239 	return (1);
1240 }
1241 
1242 /*
1243  * Internal version of zshwiflow
1244  * Called at splzs()
1245  */
1246 void
1247 zs_hwiflow(struct zstty_softc *zst)
1248 {
1249 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1250 
1251 	if (cs->cs_wr5_rts == 0)
1252 		return;
1253 
1254 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1255 
1256 	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
1257 		CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
1258 		CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
1259 	} else {
1260 		SET(ccs->cs_preg[5], cs->cs_wr5_rts);
1261 		SET(ccs->cs_creg[5], cs->cs_wr5_rts);
1262 	}
1263 	zs_write_reg(ccs, 5, ccs->cs_creg[5]);
1264 }
1265 
1266 
1267 /****************************************************************
1268  * Interface to the lower layer (zscc)
1269  ****************************************************************/
1270 
1271 void zstty_rxsoft(struct zstty_softc *, struct tty *);
1272 void zstty_txsoft(struct zstty_softc *, struct tty *);
1273 void zstty_stsoft(struct zstty_softc *, struct tty *);
1274 void zstty_diag(void *);
1275 
1276 /*
1277  * Receiver Ready interrupt.
1278  * Called at splzs().
1279  */
1280 void
1281 zstty_rxint(struct zs_chanstate *cs)
1282 {
1283 	struct zstty_softc *zst = cs->cs_private;
1284 	uint8_t *put, *end;
1285 	u_int cc;
1286 	uint8_t rr0, rr1, c;
1287 
1288 	end = zst->zst_ebuf;
1289 	put = zst->zst_rbput;
1290 	cc = zst->zst_rbavail;
1291 
1292 	while (cc > 0) {
1293 		/*
1294 		 * First read the status, because reading the received char
1295 		 * destroys the status of this char.
1296 		 */
1297 		rr1 = zs_read_reg(cs, 1);
1298 		c = zs_read_data(cs);
1299 
1300 		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1301 			/* Clear the receive error. */
1302 			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1303 		}
1304 
1305 		put[0] = c;
1306 		put[1] = rr1;
1307 		put += 2;
1308 		if (put >= end)
1309 			put = zst->zst_rbuf;
1310 		cc--;
1311 
1312 		rr0 = zs_read_csr(cs);
1313 		if (!ISSET(rr0, ZSRR0_RX_READY))
1314 			break;
1315 	}
1316 
1317 	/*
1318 	 * Current string of incoming characters ended because
1319 	 * no more data was available or we ran out of space.
1320 	 * Schedule a receive event if any data was received.
1321 	 * If we're out of space, turn off receive interrupts.
1322 	 */
1323 	zst->zst_rbput = put;
1324 	zst->zst_rbavail = cc;
1325 	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1326 		zst->zst_rx_ready = 1;
1327 		cs->cs_softreq = 1;
1328 	}
1329 
1330 	/*
1331 	 * See if we are in danger of overflowing a buffer. If
1332 	 * so, use hardware flow control to ease the pressure.
1333 	 */
1334 	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
1335 	    cc < zst->zst_r_hiwat) {
1336 		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1337 		zs_hwiflow(zst);
1338 	}
1339 
1340 	/*
1341 	 * If we're out of space, disable receive interrupts
1342 	 * until the queue has drained a bit.
1343 	 */
1344 	if (!cc) {
1345 		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1346 		CLR(cs->cs_preg[1], ZSWR1_RIE);
1347 		cs->cs_creg[1] = cs->cs_preg[1];
1348 		zs_write_reg(cs, 1, cs->cs_creg[1]);
1349 	}
1350 }
1351 
1352 /*
1353  * Transmitter Ready interrupt.
1354  * Called at splzs().
1355  */
1356 void
1357 zstty_txint(struct zs_chanstate *cs)
1358 {
1359 	struct zstty_softc *zst = cs->cs_private;
1360 	int rr0;
1361 
1362 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
1363 
1364 	/*
1365 	 * If we've delayed a parameter change, do it now, and restart
1366 	 * output.
1367 	 */
1368 	if (cs->cs_heldchange) {
1369 		zs_loadchannelregs(cs);
1370 		cs->cs_heldchange = 0;
1371 		zst->zst_tbc = zst->zst_heldtbc;
1372 		zst->zst_heldtbc = 0;
1373 	}
1374 
1375 	while (zst->zst_tbc > 0) {
1376 		rr0 = zs_read_csr(cs);
1377 		if ((rr0 & ZSRR0_TX_READY) == 0)
1378 			break;
1379 
1380 		zs_write_data(cs, *zst->zst_tba);
1381 		zst->zst_tbc--;
1382 		zst->zst_tba++;
1383 	}
1384 
1385 	if (zst->zst_tbc == 0) {
1386 		if (zst->zst_tx_busy) {
1387 			zst->zst_tx_busy = 0;
1388 			zst->zst_tx_done = 1;
1389 			cs->cs_softreq = 1;
1390 		}
1391 	}
1392 }
1393 
1394 #ifdef DDB
1395 #include <ddb/db_var.h>
1396 #define	DB_CONSOLE	db_console
1397 #else
1398 #define	DB_CONSOLE	0
1399 #endif
1400 
1401 /*
1402  * Status Change interrupt.
1403  * Called at splzs().
1404  */
1405 void
1406 zstty_stint(struct zs_chanstate *cs, int force)
1407 {
1408 	struct zstty_softc *zst = cs->cs_private;
1409 	struct tty *tp = zst->zst_tty;
1410 	uint8_t rr0, delta;
1411 
1412 	rr0 = zs_read_csr(cs);
1413 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
1414 
1415 	/*
1416 	 * Check here for console break, so that we can abort
1417 	 * even when interrupts are locking up the machine.
1418 	 */
1419 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) &&
1420 	    ISSET(rr0, ZSRR0_BREAK) && DB_CONSOLE)
1421 		zs_abort(cs);
1422 
1423 	if (!force)
1424 		delta = rr0 ^ cs->cs_rr0;
1425 	else
1426 		delta = cs->cs_rr0_mask;
1427 
1428 	ttytstamp(tp, cs->cs_rr0 & ZSRR0_CTS, rr0 & ZSRR0_CTS,
1429 	    cs->cs_rr0 & ZSRR0_DCD, rr0 & ZSRR0_DCD);
1430 
1431 	cs->cs_rr0 = rr0;
1432 
1433 	if (ISSET(delta, cs->cs_rr0_mask)) {
1434 		SET(cs->cs_rr0_delta, delta);
1435 
1436 		/*
1437 		 * Stop output immediately if we lose the output
1438 		 * flow control signal or carrier detect.
1439 		 */
1440 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
1441 			zst->zst_tbc = 0;
1442 			zst->zst_heldtbc = 0;
1443 		}
1444 
1445 		zst->zst_st_check = 1;
1446 		cs->cs_softreq = 1;
1447 	}
1448 }
1449 
1450 void
1451 zstty_diag(void *arg)
1452 {
1453 	struct zstty_softc *zst = arg;
1454 	int overflows, floods;
1455 	int s;
1456 
1457 	s = splzs();
1458 	overflows = zst->zst_overflows;
1459 	zst->zst_overflows = 0;
1460 	floods = zst->zst_floods;
1461 	zst->zst_floods = 0;
1462 	zst->zst_errors = 0;
1463 	splx(s);
1464 
1465 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1466 	    zst->zst_dev.dv_xname,
1467 	    overflows, overflows == 1 ? "" : "s",
1468 	    floods, floods == 1 ? "" : "s");
1469 }
1470 
1471 void
1472 zstty_rxsoft(struct zstty_softc *zst, struct tty *tp)
1473 {
1474 	struct zs_chanstate *cs = zst->zst_cs;
1475 	int (*rint)(int, struct tty *) = linesw[tp->t_line].l_rint;
1476 	uint8_t *get, *end;
1477 	u_int cc, scc;
1478 	uint8_t rr1;
1479 	int code;
1480 	int s;
1481 
1482 	end = zst->zst_ebuf;
1483 	get = zst->zst_rbget;
1484 	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
1485 
1486 	if (cc == zstty_rbuf_size) {
1487 		zst->zst_floods++;
1488 		if (zst->zst_errors++ == 0)
1489 			timeout_add_sec(&zst->zst_diag_ch, 60);
1490 	}
1491 
1492 	/* If not yet open, drop the entire buffer content here */
1493 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
1494 		get += cc << 1;
1495 		if (get >= end)
1496 			get -= zstty_rbuf_size << 1;
1497 		cc = 0;
1498 	}
1499 	while (cc) {
1500 		code = get[0];
1501 		rr1 = get[1];
1502 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
1503 			if (ISSET(rr1, ZSRR1_DO)) {
1504 				zst->zst_overflows++;
1505 				if (zst->zst_errors++ == 0)
1506 					timeout_add_sec(&zst->zst_diag_ch, 60);
1507 			}
1508 			if (ISSET(rr1, ZSRR1_FE))
1509 				SET(code, TTY_FE);
1510 			if (ISSET(rr1, ZSRR1_PE))
1511 				SET(code, TTY_PE);
1512 		}
1513 		if ((*rint)(code, tp) == -1) {
1514 			/*
1515 			 * The line discipline's buffer is out of space.
1516 			 */
1517 			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1518 				/*
1519 				 * We're either not using flow control, or the
1520 				 * line discipline didn't tell us to block for
1521 				 * some reason.  Either way, we have no way to
1522 				 * know when there's more space available, so
1523 				 * just drop the rest of the data.
1524 				 */
1525 				get += cc << 1;
1526 				if (get >= end)
1527 					get -= zstty_rbuf_size << 1;
1528 				cc = 0;
1529 			} else {
1530 				/*
1531 				 * Don't schedule any more receive processing
1532 				 * until the line discipline tells us there's
1533 				 * space available (through comhwiflow()).
1534 				 * Leave the rest of the data in the input
1535 				 * buffer.
1536 				 */
1537 				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1538 			}
1539 			break;
1540 		}
1541 		get += 2;
1542 		if (get >= end)
1543 			get = zst->zst_rbuf;
1544 		cc--;
1545 	}
1546 
1547 	if (cc != scc) {
1548 		zst->zst_rbget = get;
1549 		s = splzs();
1550 		cc = zst->zst_rbavail += scc - cc;
1551 		/* Buffers should be ok again, release possible block. */
1552 		if (cc >= zst->zst_r_lowat) {
1553 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
1554 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1555 				SET(cs->cs_preg[1], ZSWR1_RIE);
1556 				cs->cs_creg[1] = cs->cs_preg[1];
1557 				zs_write_reg(cs, 1, cs->cs_creg[1]);
1558 			}
1559 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
1560 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1561 				zs_hwiflow(zst);
1562 			}
1563 		}
1564 		splx(s);
1565 	}
1566 }
1567 
1568 void
1569 zstty_txsoft(struct zstty_softc *zst, struct tty *tp)
1570 {
1571 	int s;
1572 
1573 	CLR(tp->t_state, TS_BUSY);
1574 	if (ISSET(tp->t_state, TS_FLUSH))
1575 		CLR(tp->t_state, TS_FLUSH);
1576 	else {
1577 		s = splzs();
1578 		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
1579 		splx(s);
1580 	}
1581 	(*linesw[tp->t_line].l_start)(tp);
1582 }
1583 
1584 void
1585 zstty_stsoft(struct zstty_softc *zst, struct tty *tp)
1586 {
1587 	struct zs_chanstate *cs = zst->zst_cs;
1588 	uint8_t rr0, delta;
1589 	int s;
1590 
1591 	s = splzs();
1592 	rr0 = cs->cs_rr0;
1593 	delta = cs->cs_rr0_delta;
1594 	cs->cs_rr0_delta = 0;
1595 	splx(s);
1596 
1597 	if (ISSET(delta, cs->cs_rr0_dcd)) {
1598 		/*
1599 		 * Inform the tty layer that carrier detect changed.
1600 		 */
1601 		(void)(*linesw[tp->t_line].l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
1602 	}
1603 
1604 	if (ISSET(delta, cs->cs_rr0_cts)) {
1605 		/* Block or unblock output according to flow control. */
1606 		if (ISSET(rr0, cs->cs_rr0_cts)) {
1607 			zst->zst_tx_stopped = 0;
1608 			(*linesw[tp->t_line].l_start)(tp);
1609 		} else {
1610 			zst->zst_tx_stopped = 1;
1611 		}
1612 	}
1613 }
1614 
1615 /*
1616  * Software interrupt.  Called at zssoft
1617  *
1618  * The main job to be done here is to empty the input ring
1619  * by passing its contents up to the tty layer.  The ring is
1620  * always emptied during this operation, therefore the ring
1621  * must not be larger than the space after "high water" in
1622  * the tty layer, or the tty layer might drop our input.
1623  *
1624  * Note: an "input blockage" condition is assumed to exist if
1625  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
1626  */
1627 void
1628 zstty_softint(struct zs_chanstate *cs)
1629 {
1630 	struct zstty_softc *zst = cs->cs_private;
1631 	struct tty *tp = zst->zst_tty;
1632 	int s;
1633 
1634 	s = spltty();
1635 
1636 	if (zst->zst_rx_ready) {
1637 		zst->zst_rx_ready = 0;
1638 		zstty_rxsoft(zst, tp);
1639 	}
1640 
1641 	if (zst->zst_st_check) {
1642 		zst->zst_st_check = 0;
1643 		zstty_stsoft(zst, tp);
1644 	}
1645 
1646 	if (zst->zst_tx_done) {
1647 		zst->zst_tx_done = 0;
1648 		zstty_txsoft(zst, tp);
1649 	}
1650 
1651 	splx(s);
1652 }
1653 
1654 struct zsops zsops_tty = {
1655 	zstty_rxint,	/* receive char available */
1656 	zstty_stint,	/* external/status */
1657 	zstty_txint,	/* xmit buffer empty */
1658 	zstty_softint,	/* process software interrupt */
1659 };
1660