xref: /openbsd-src/sys/dev/ic/z8530tty.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: z8530tty.c,v 1.25 2014/07/13 23:10:23 deraadt 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 	/*
290 	 * Check whether we serve as a console device.
291 	 * XXX - split console input/output channels aren't
292 	 *	 supported yet on /dev/console
293 	 */
294 	i = o = NULL;
295 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
296 		i = " input";
297 		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
298 			args->consdev->cn_dev = dev;
299 			cn_tab->cn_pollc = args->consdev->cn_pollc;
300 			cn_tab->cn_getc = args->consdev->cn_getc;
301 		}
302 		cn_tab->cn_dev = dev;
303 	}
304 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
305 		o = " output";
306 		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
307 			cn_tab->cn_putc = args->consdev->cn_putc;
308 		}
309 		cn_tab->cn_dev = dev;
310 	}
311 	if (i != NULL || o != NULL) {
312 		printf(": console%s", i ? (o ? "" : i) : o);
313 	}
314 
315 #ifdef KGDB
316 	if (zs_check_kgdb(cs, dev)) {
317 		/*
318 		 * Allow kgdb to "take over" this port.  Returns true
319 		 * if this serial port is in-use by kgdb.
320 		 */
321 		printf(": kgdb\n");
322 		/*
323 		 * This is the kgdb port (exclusive use)
324 		 * so skip the normal attach code.
325 		 */
326 		return;
327 	}
328 #endif
329 
330 #if defined(__sparc__) || defined(__sparc64__)
331 	if (strcmp(args->type, "keyboard") == 0 ||
332 	    strcmp(args->type, "mouse") == 0)
333 		printf(": %s", args->type);
334 #endif
335 
336 	printf("\n");
337 
338 	tp = ttymalloc(0);
339 	tp->t_dev = dev;
340 	tp->t_oproc = zsstart;
341 	tp->t_param = zsparam;
342 	tp->t_hwiflow = zshwiflow;
343 
344 	zst->zst_tty = tp;
345 	zst->zst_rbuf = mallocarray(zstty_rbuf_size, 2, M_DEVBUF, M_WAITOK);
346 	zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size * 2);
347 	/* Disable the high water mark. */
348 	zst->zst_r_hiwat = 0;
349 	zst->zst_r_lowat = 0;
350 	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
351 	zst->zst_rbavail = zstty_rbuf_size;
352 
353 	/* if there are no enable/disable functions, assume the device
354 	   is always enabled */
355 	if (!cs->enable)
356 		cs->enabled = 1;
357 
358 	/*
359 	 * Hardware init
360 	 */
361 	dtr_on = 0;
362 	resetbit = 0;
363 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
364 		/* Call zsparam similar to open. */
365 		struct termios t;
366 
367 		/* Wait a while for previous console output to complete */
368 		DELAY(10000);
369 
370 		/* Setup the "new" parameters in t. */
371 		t.c_ispeed = 0;
372 		t.c_ospeed = cs->cs_defspeed;
373 		t.c_cflag = cs->cs_defcflag;
374 
375 		s = splzs();
376 
377 		/*
378 		 * Turn on receiver and status interrupts.
379 		 * We defer the actual write of the register to zsparam(),
380 		 * but we must make sure status interrupts are turned on by
381 		 * the time zsparam() reads the initial rr0 state.
382 		 */
383 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
384 
385 		splx(s);
386 
387 		/* Make sure zsparam will see changes. */
388 		tp->t_ospeed = 0;
389 		(void)zsparam(tp, &t);
390 
391 		/* Make sure DTR is on now. */
392 		dtr_on = 1;
393 	} else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
394 		/* Not the console; may need reset. */
395 		resetbit = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
396 	}
397 
398 	s = splzs();
399 	if (resetbit)
400 		zs_write_reg(cs, 9, resetbit);
401 	zs_modem(zst, dtr_on);
402 	splx(s);
403 }
404 
405 
406 /*
407  * Return pointer to our tty.
408  */
409 struct tty *
410 zstty(dev_t dev)
411 {
412 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
413 
414 	return (zst->zst_tty);
415 }
416 
417 
418 void
419 zs_shutdown(struct zstty_softc *zst)
420 {
421 	struct zs_chanstate *cs = zst->zst_cs;
422 	struct tty *tp = zst->zst_tty;
423 	int s;
424 
425 	s = splzs();
426 
427 	/* If we were asserting flow control, then deassert it. */
428 	SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
429 	zs_hwiflow(zst);
430 
431 	/* Clear any break condition set with TIOCSBRK. */
432 	zs_break(cs, 0);
433 
434 	/* Turn off PPS capture on last close. */
435 	zst->zst_ppsmask = 0;
436 
437 	/*
438 	 * Hang up if necessary.  Wait a bit, so the other side has time to
439 	 * notice even if we immediately open the port again.
440 	 */
441 	if (ISSET(tp->t_cflag, HUPCL) || ISSET(tp->t_state, TS_WOPEN)) {
442 		zs_modem(zst, 0);
443 		/* hold low for 1 second */
444 		(void)tsleep(cs, TTIPRI, ttclos, hz);
445 	}
446 
447 	/* Turn off interrupts if not the console. */
448 	if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
449 		CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
450 		cs->cs_creg[1] = cs->cs_preg[1];
451 		zs_write_reg(cs, 1, cs->cs_creg[1]);
452 	}
453 
454 	/* Call the power management hook. */
455 	if (cs->disable) {
456 #ifdef DIAGNOSTIC
457 		if (!cs->enabled)
458 			panic("%s: not enabled?", __func__);
459 #endif
460 		(*cs->disable)(zst->zst_cs);
461 	}
462 
463 	splx(s);
464 }
465 
466 /*
467  * Open a zs serial (tty) port.
468  */
469 int
470 zsopen(dev_t dev, int flags, int mode, struct proc *p)
471 {
472 	struct zstty_softc *zst;
473 	struct zs_chanstate *cs;
474 	struct tty *tp;
475 	int s;
476 #if IPL_ZS != IPL_TTY
477 	int s2;
478 #endif
479 	int error;
480 
481 	zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
482 	if (zst == NULL)
483 		return (ENXIO);
484 
485 	tp = zst->zst_tty;
486 	cs = zst->zst_cs;
487 
488 	/* If KGDB took the line, then tp==NULL */
489 	if (tp == NULL)
490 		return (EBUSY);
491 
492 	if (ISSET(tp->t_state, TS_ISOPEN) &&
493 	    ISSET(tp->t_state, TS_XCLUDE) &&
494 	    suser(p, 0) != 0)
495 		return (EBUSY);
496 
497 	s = spltty();
498 
499 	/*
500 	 * Do the following iff this is a first open.
501 	 */
502 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
503 		struct termios t;
504 
505 		tp->t_dev = dev;
506 
507 		/* Call the power management hook. */
508 		if (cs->enable) {
509 			if ((*cs->enable)(cs)) {
510 				splx(s);
511 				printf("%s: device enable failed\n",
512 				    zst->zst_dev.dv_xname);
513 				return (EIO);
514 			}
515 		}
516 
517 		/*
518 		 * Initialize the termios status to the defaults.  Add in the
519 		 * sticky bits from TIOCSFLAGS.
520 		 */
521 		t.c_ispeed = 0;
522 		t.c_ospeed = cs->cs_defspeed;
523 		t.c_cflag = cs->cs_defcflag;
524 		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
525 			SET(t.c_cflag, CLOCAL);
526 		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
527 			SET(t.c_cflag, CRTSCTS);
528 		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
529 			SET(t.c_cflag, MDMBUF);
530 
531 #if IPL_ZS != IPL_TTY
532 		s2 = splzs();
533 #endif
534 
535 		/*
536 		 * Turn on receiver and status interrupts.
537 		 * We defer the actual write of the register to zsparam(),
538 		 * but we must make sure status interrupts are turned on by
539 		 * the time zsparam() reads the initial rr0 state.
540 		 */
541 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
542 
543 		/* Clear PPS capture state on first open. */
544 		zst->zst_ppsmask = 0;
545 
546 #if IPL_ZS != IPL_TTY
547 		splx(s2);
548 #endif
549 
550 		/* Make sure zsparam will see changes. */
551 		tp->t_ospeed = 0;
552 		(void)zsparam(tp, &t);
553 
554 		/*
555 		 * Note: zsparam has done: cflag, ispeed, ospeed
556 		 * so we just need to do: iflag, oflag, lflag, cc
557 		 * For "raw" mode, just leave all zeros.
558 		 */
559 		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
560 			tp->t_iflag = TTYDEF_IFLAG;
561 			tp->t_oflag = TTYDEF_OFLAG;
562 			tp->t_lflag = TTYDEF_LFLAG;
563 		} else {
564 			tp->t_iflag = 0;
565 			tp->t_oflag = 0;
566 			tp->t_lflag = 0;
567 		}
568 		ttychars(tp);
569 		ttsetwater(tp);
570 
571 		if (ZSDIALOUT(dev))
572 			SET(tp->t_state, TS_CARR_ON);
573 		else
574 			CLR(tp->t_state, TS_CARR_ON);
575 
576 #if IPL_ZS != IPL_TTY
577 		s2 = splzs();
578 #endif
579 
580 		/* Clear the input ring, and unblock. */
581 		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
582 		zst->zst_rbavail = zstty_rbuf_size;
583 		zs_iflush(cs);
584 		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
585 		zs_hwiflow(zst);
586 
587 #if IPL_ZS != IPL_TTY
588 		splx(s2);
589 #endif
590 	}
591 
592 	if (ZSDIALOUT(dev)) {
593 		if (ISSET(tp->t_state, TS_ISOPEN)) {
594 			/* someone already is dialed in... */
595 			splx(s);
596 			return EBUSY;
597 		}
598 		cs->cs_cua = 1;
599 	}
600 
601 	error = 0;
602 	/* wait for carrier if necessary */
603 	if (ISSET(flags, O_NONBLOCK)) {
604 		if (!ZSDIALOUT(dev) && cs->cs_cua) {
605 			/* Opening TTY non-blocking... but the CUA is busy */
606 			error = EBUSY;
607 		}
608 	} else
609 	  while (cs->cs_cua ||
610 	    (!ISSET(tp->t_cflag, CLOCAL) && !ISSET(tp->t_state, TS_CARR_ON))) {
611 		int rr0;
612 
613 		error = 0;
614 		SET(tp->t_state, TS_WOPEN);
615 
616 		if (!ZSDIALOUT(dev) && !cs->cs_cua) {
617 			/*
618 			 * Turn on DTR.  We must always do this on non-CUA
619 			 * devices, even if carrier is not present, because
620 			 * otherwise we'd have to use TIOCSDTR immediately
621 			 * after setting CLOCAL, which applications do not
622 			 * expect.  We always assert DTR while the device is
623 			 * open unless explicitly requested to deassert it.
624 			 */
625 #if IPL_ZS != IPL_TTY
626 			s2 = splzs();
627 #endif
628 			zs_modem(zst, 1);
629 			rr0 = zs_read_csr(cs);
630 #if IPL_ZS != IPL_TTY
631 			splx(s2);
632 #endif
633 
634 			/* loop, turning on the device, until carrier present */
635 			if (ISSET(rr0, ZSRR0_DCD) ||
636 			    ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR))
637 				SET(tp->t_state, TS_CARR_ON);
638 		}
639 
640 		if ((ISSET(tp->t_cflag, CLOCAL) ||
641 		    ISSET(tp->t_state, TS_CARR_ON)) && !cs->cs_cua)
642 			break;
643 
644 		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
645 		    ttopen, 0);
646 
647 		if (!ZSDIALOUT(dev) && cs->cs_cua && error == EINTR) {
648 			error = 0;
649 			continue;
650 		}
651 
652 		if (error) {
653 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
654 #if IPL_ZS != IPL_TTY
655 				s2 = splzs();
656 #endif
657 				zs_modem(zst, 0);
658 #if IPL_ZS != IPL_TTY
659 				splx(s2);
660 #endif
661 				CLR(tp->t_state, TS_WOPEN);
662 				ttwakeup(tp);
663 			}
664 			if (ZSDIALOUT(dev))
665 				cs->cs_cua = 0;
666 			CLR(tp->t_state, TS_WOPEN);
667 			break;
668 		}
669 		if (!ZSDIALOUT(dev) && cs->cs_cua)
670 			continue;
671 	}
672 
673 	splx(s);
674 
675 	if (error == 0)
676 		error = ((*linesw[tp->t_line].l_open)(dev, tp, p));
677 	if (error)
678 		goto bad;
679 
680 	return (0);
681 
682 bad:
683 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
684 		/*
685 		 * We failed to open the device, and nobody else had it opened.
686 		 * Clean up the state as appropriate.
687 		 */
688 		zs_shutdown(zst);
689 	}
690 
691 	return (error);
692 }
693 
694 /*
695  * Close a zs serial port.
696  */
697 int
698 zsclose(dev_t dev, int flags, int mode, struct proc *p)
699 {
700 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
701 	struct zs_chanstate *cs = zst->zst_cs;
702 	struct tty *tp = zst->zst_tty;
703 	int s;
704 
705 	/* XXX This is for cons.c. */
706 	if (!ISSET(tp->t_state, TS_ISOPEN))
707 		return 0;
708 
709 	(*linesw[tp->t_line].l_close)(tp, flags, p);
710 
711 	s = spltty();
712 	cs->cs_cua = 0;
713 	ttyclose(tp);
714 	splx(s);
715 
716 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
717 		/*
718 		 * Although we got a last close, the device may still be in
719 		 * use; e.g. if this was the dialout node, and there are still
720 		 * processes waiting for carrier on the non-dialout node.
721 		 */
722 		zs_shutdown(zst);
723 	}
724 
725 	return (0);
726 }
727 
728 /*
729  * Read/write zs serial port.
730  */
731 int
732 zsread(dev_t dev, struct uio *uio, int flags)
733 {
734 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
735 	struct tty *tp = zst->zst_tty;
736 
737 	return (*linesw[tp->t_line].l_read)(tp, uio, flags);
738 }
739 
740 int
741 zswrite(dev_t dev, struct uio *uio, int flags)
742 {
743 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
744 	struct tty *tp = zst->zst_tty;
745 
746 	return (*linesw[tp->t_line].l_write)(tp, uio, flags);
747 }
748 
749 int
750 zsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
751 {
752 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(dev));
753 	struct zs_chanstate *cs = zst->zst_cs;
754 	struct tty *tp = zst->zst_tty;
755 	int error;
756 	int s;
757 
758 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
759 	if (error >= 0)
760 		return (error);
761 
762 	error = ttioctl(tp, cmd, data, flag, p);
763 	if (error >= 0)
764 		return (error);
765 
766 #ifdef	ZS_MD_IOCTL
767 	error = ZS_MD_IOCTL;
768 	if (error >= 0)
769 		return (error);
770 #endif	/* ZS_MD_IOCTL */
771 
772 	error = 0;
773 
774 	s = splzs();
775 
776 	switch (cmd) {
777 	case TIOCSBRK:
778 		zs_break(cs, 1);
779 		break;
780 
781 	case TIOCCBRK:
782 		zs_break(cs, 0);
783 		break;
784 
785 	case TIOCGFLAGS:
786 		*(int *)data = zst->zst_swflags;
787 		break;
788 
789 	case TIOCSFLAGS:
790 		error = suser(p, 0);
791 		if (error)
792 			break;
793 		zst->zst_swflags = *(int *)data;
794 		break;
795 
796 	case TIOCSDTR:
797 		zs_modem(zst, 1);
798 		break;
799 
800 	case TIOCCDTR:
801 		zs_modem(zst, 0);
802 		break;
803 
804 	case TIOCMSET:
805 	case TIOCMBIS:
806 	case TIOCMBIC:
807 		tiocm_to_zs(zst, cmd, *(int *)data);
808 		break;
809 
810 	case TIOCMGET:
811 		*(int *)data = zs_to_tiocm(zst);
812 		break;
813 
814 	default:
815 		error = ENOTTY;
816 		break;
817 	}
818 
819 	splx(s);
820 
821 	return (error);
822 }
823 
824 /*
825  * Start or restart transmission.
826  */
827 void
828 zsstart(struct tty *tp)
829 {
830 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
831 	struct zs_chanstate *cs = zst->zst_cs;
832 	u_char *tba;
833 	int tbc, rr0;
834 	int s;
835 
836 	s = spltty();
837 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
838 		goto out;
839 	if (zst->zst_tx_stopped)
840 		goto out;
841 
842 	ttwakeupwr(tp);
843 	if (tp->t_outq.c_cc == 0)
844 		goto out;
845 
846 	/* Grab the first contiguous region of buffer space. */
847 	tba = tp->t_outq.c_cf;
848 	tbc = ndqb(&tp->t_outq, 0);
849 
850 #if IPL_ZS != IPL_TTY
851 	(void)splzs();
852 #endif
853 
854 	zst->zst_tba = tba;
855 	zst->zst_tbc = tbc;
856 	SET(tp->t_state, TS_BUSY);
857 	zst->zst_tx_busy = 1;
858 
859 	do {
860 		rr0 = zs_read_csr(cs);
861 		if ((rr0 & ZSRR0_TX_READY) == 0)
862 			break;
863 
864 		zs_write_data(cs, *zst->zst_tba);
865 		zst->zst_tbc--;
866 		zst->zst_tba++;
867 	} while (zst->zst_tbc > 0);
868 
869 out:
870 	splx(s);
871 }
872 
873 /*
874  * Stop output, e.g., for ^S or output flush.
875  */
876 int
877 zsstop(struct tty *tp, int flag)
878 {
879 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
880 	int s;
881 
882 	s = splzs();
883 	if (ISSET(tp->t_state, TS_BUSY)) {
884 		/* Stop transmitting at the next chunk. */
885 		zst->zst_tbc = 0;
886 		zst->zst_heldtbc = 0;
887 		if (!ISSET(tp->t_state, TS_TTSTOP))
888 			SET(tp->t_state, TS_FLUSH);
889 	}
890 	splx(s);
891 	return 0;
892 }
893 
894 /*
895  * Set ZS tty parameters from termios.
896  * XXX - Should just copy the whole termios after
897  * making sure all the changes could be done.
898  */
899 int
900 zsparam(struct tty *tp, struct termios *t)
901 {
902 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
903 	struct zs_chanstate *cs = zst->zst_cs;
904 	int ospeed;
905 	tcflag_t cflag;
906 	uint8_t tmp3, tmp4, tmp5;
907 	int s, error;
908 
909 	ospeed = t->c_ospeed;
910 	cflag = t->c_cflag;
911 
912 	/* Check requested parameters. */
913 	if (ospeed < 0)
914 		return (EINVAL);
915 	if (t->c_ispeed && t->c_ispeed != ospeed)
916 		return (EINVAL);
917 
918 	/*
919 	 * For the console, always force CLOCAL and !HUPCL, so that the port
920 	 * is always active.
921 	 */
922 	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
923 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
924 		SET(cflag, CLOCAL);
925 		CLR(cflag, HUPCL);
926 	}
927 
928 	/*
929 	 * Only whack the UART when params change.
930 	 * Some callers need to clear tp->t_ospeed
931 	 * to make sure initialization gets done.
932 	 */
933 	if (tp->t_ospeed == ospeed &&
934 	    tp->t_cflag == cflag)
935 		return (0);
936 
937 	/*
938 	 * Call MD functions to deal with changed
939 	 * clock modes or H/W flow control modes.
940 	 * The BRG divisor is set now. (reg 12,13)
941 	 */
942 	error = zs_set_speed(cs, ospeed);
943 	if (error)
944 		return (error);
945 	error = zs_set_modes(cs, cflag);
946 	if (error)
947 		return (error);
948 
949 	/*
950 	 * Block interrupts so that state will not
951 	 * be altered until we are done setting it up.
952 	 *
953 	 * Initial values in cs_preg are set before
954 	 * our attach routine is called.  The master
955 	 * interrupt enable is handled by zsc.c
956 	 *
957 	 */
958 	s = splzs();
959 
960 	/*
961 	 * Recalculate which status ints to enable.
962 	 */
963 	zs_maskintr(zst);
964 
965 	/* Recompute character size bits. */
966 	tmp3 = cs->cs_preg[3];
967 	tmp5 = cs->cs_preg[5];
968 	CLR(tmp3, ZSWR3_RXSIZE);
969 	CLR(tmp5, ZSWR5_TXSIZE);
970 	switch (ISSET(cflag, CSIZE)) {
971 	case CS5:
972 		SET(tmp3, ZSWR3_RX_5);
973 		SET(tmp5, ZSWR5_TX_5);
974 		break;
975 	case CS6:
976 		SET(tmp3, ZSWR3_RX_6);
977 		SET(tmp5, ZSWR5_TX_6);
978 		break;
979 	case CS7:
980 		SET(tmp3, ZSWR3_RX_7);
981 		SET(tmp5, ZSWR5_TX_7);
982 		break;
983 	case CS8:
984 		SET(tmp3, ZSWR3_RX_8);
985 		SET(tmp5, ZSWR5_TX_8);
986 		break;
987 	}
988 	cs->cs_preg[3] = tmp3;
989 	cs->cs_preg[5] = tmp5;
990 
991 	/*
992 	 * Recompute the stop bits and parity bits.  Note that
993 	 * zs_set_speed() may have set clock selection bits etc.
994 	 * in wr4, so those must preserved.
995 	 */
996 	tmp4 = cs->cs_preg[4];
997 	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
998 	if (ISSET(cflag, CSTOPB))
999 		SET(tmp4, ZSWR4_TWOSB);
1000 	else
1001 		SET(tmp4, ZSWR4_ONESB);
1002 	if (!ISSET(cflag, PARODD))
1003 		SET(tmp4, ZSWR4_EVENP);
1004 	if (ISSET(cflag, PARENB))
1005 		SET(tmp4, ZSWR4_PARENB);
1006 	cs->cs_preg[4] = tmp4;
1007 
1008 	/* And copy to tty. */
1009 	tp->t_ispeed = 0;
1010 	tp->t_ospeed = ospeed;
1011 	tp->t_cflag = cflag;
1012 
1013 	/*
1014 	 * If nothing is being transmitted, set up new current values,
1015 	 * else mark them as pending.
1016 	 */
1017 	if (!cs->cs_heldchange) {
1018 		if (zst->zst_tx_busy) {
1019 			zst->zst_heldtbc = zst->zst_tbc;
1020 			zst->zst_tbc = 0;
1021 			cs->cs_heldchange = 1;
1022 		} else
1023 			zs_loadchannelregs(cs);
1024 	}
1025 
1026 	/*
1027 	 * If hardware flow control is disabled, turn off the buffer water
1028 	 * marks and unblock any soft flow control state.  Otherwise, enable
1029 	 * the water marks.
1030 	 */
1031 	if (!ISSET(cflag, CHWFLOW)) {
1032 		zst->zst_r_hiwat = 0;
1033 		zst->zst_r_lowat = 0;
1034 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1035 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1036 			zst->zst_rx_ready = 1;
1037 			cs->cs_softreq = 1;
1038 		}
1039 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1040 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1041 			zs_hwiflow(zst);
1042 		}
1043 	} else {
1044 		zst->zst_r_hiwat = zstty_rbuf_hiwat;
1045 		zst->zst_r_lowat = zstty_rbuf_lowat;
1046 	}
1047 
1048 	/*
1049 	 * Force a recheck of the hardware carrier and flow control status,
1050 	 * since we may have changed which bits we're looking at.
1051 	 */
1052 	zstty_stint(cs, 1);
1053 
1054 	splx(s);
1055 
1056 	/*
1057 	 * If hardware flow control is disabled, unblock any hard flow control
1058 	 * state.
1059 	 */
1060 	if (!ISSET(cflag, CHWFLOW)) {
1061 		if (zst->zst_tx_stopped) {
1062 			zst->zst_tx_stopped = 0;
1063 			zsstart(tp);
1064 		}
1065 	}
1066 
1067 	zstty_softint(cs);
1068 
1069 	return (0);
1070 }
1071 
1072 /*
1073  * Compute interrupt enable bits and set in the pending bits. Called both
1074  * in zsparam() and when PPS (pulse per second timing) state changes.
1075  * Must be called at splzs().
1076  */
1077 void
1078 zs_maskintr(struct zstty_softc *zst)
1079 {
1080 	struct zs_chanstate *cs = zst->zst_cs;
1081 	uint8_t tmp15;
1082 
1083 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
1084 	if (zst->zst_ppsmask != 0)
1085 		cs->cs_rr0_mask |= cs->cs_rr0_pps;
1086 	tmp15 = cs->cs_preg[15];
1087 	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
1088 		SET(tmp15, ZSWR15_DCD_IE);
1089 	else
1090 		CLR(tmp15, ZSWR15_DCD_IE);
1091 	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
1092 		SET(tmp15, ZSWR15_CTS_IE);
1093 	else
1094 		CLR(tmp15, ZSWR15_CTS_IE);
1095 	cs->cs_preg[15] = tmp15;
1096 }
1097 
1098 
1099 /*
1100  * Raise or lower modem control (DTR/RTS) signals.  If a character is
1101  * in transmission, the change is deferred.
1102  * Called at splzs().
1103  */
1104 void
1105 zs_modem(struct zstty_softc *zst, int onoff)
1106 {
1107 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1108 
1109 	if (cs->cs_wr5_dtr == 0)
1110 		return;
1111 
1112 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1113 
1114 	if (onoff)
1115 		SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
1116 	else
1117 		CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
1118 
1119 	if (!cs->cs_heldchange) {
1120 		if (zst->zst_tx_busy) {
1121 			zst->zst_heldtbc = zst->zst_tbc;
1122 			zst->zst_tbc = 0;
1123 			cs->cs_heldchange = 1;
1124 		} else
1125 			zs_loadchannelregs(cs);
1126 	}
1127 }
1128 
1129 /*
1130  * Set modem bits.
1131  * Called at splzs().
1132  */
1133 void
1134 tiocm_to_zs(struct zstty_softc *zst, u_long how, int ttybits)
1135 {
1136 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1137 	uint8_t zsbits;
1138 
1139 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1140 
1141 	zsbits = 0;
1142 	if (ISSET(ttybits, TIOCM_DTR))
1143 		SET(zsbits, ZSWR5_DTR);
1144 	if (ISSET(ttybits, TIOCM_RTS))
1145 		SET(zsbits, ZSWR5_RTS);
1146 
1147 	switch (how) {
1148 	case TIOCMBIC:
1149 		CLR(ccs->cs_preg[5], zsbits);
1150 		break;
1151 
1152 	case TIOCMBIS:
1153 		SET(ccs->cs_preg[5], zsbits);
1154 		break;
1155 
1156 	case TIOCMSET:
1157 		CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
1158 		SET(ccs->cs_preg[5], zsbits);
1159 		break;
1160 	}
1161 
1162 	if (!cs->cs_heldchange) {
1163 		if (zst->zst_tx_busy) {
1164 			zst->zst_heldtbc = zst->zst_tbc;
1165 			zst->zst_tbc = 0;
1166 			cs->cs_heldchange = 1;
1167 		} else
1168 			zs_loadchannelregs(cs);
1169 	}
1170 }
1171 
1172 /*
1173  * Get modem bits.
1174  * Called at splzs().
1175  */
1176 int
1177 zs_to_tiocm(struct zstty_softc *zst)
1178 {
1179 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1180 	uint8_t zsbits;
1181 	int ttybits = 0;
1182 
1183 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1184 
1185 	zsbits = ccs->cs_preg[5];
1186 	if (ISSET(zsbits, ZSWR5_DTR))
1187 		SET(ttybits, TIOCM_DTR);
1188 	if (ISSET(zsbits, ZSWR5_RTS))
1189 		SET(ttybits, TIOCM_RTS);
1190 
1191 	zsbits = cs->cs_rr0;
1192 	if (ISSET(zsbits, ZSRR0_DCD))
1193 		SET(ttybits, TIOCM_CD);
1194 	if (ISSET(zsbits, ZSRR0_CTS))
1195 		SET(ttybits, TIOCM_CTS);
1196 
1197 	return (ttybits);
1198 }
1199 
1200 /*
1201  * Try to block or unblock input using hardware flow-control.
1202  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
1203  * if this function returns non-zero, the TS_TBLOCK flag will
1204  * be set or cleared according to the "block" arg passed.
1205  */
1206 int
1207 zshwiflow(struct tty *tp, int block)
1208 {
1209 	struct zstty_softc *zst = zs_device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
1210 	struct zs_chanstate *cs = zst->zst_cs;
1211 	int s;
1212 
1213 	if (cs->cs_wr5_rts == 0)
1214 		return (0);
1215 
1216 	s = splzs();
1217 	if (block) {
1218 		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1219 			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
1220 			zs_hwiflow(zst);
1221 		}
1222 	} else {
1223 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1224 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1225 			zst->zst_rx_ready = 1;
1226 			cs->cs_softreq = 1;
1227 		}
1228 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1229 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
1230 			zs_hwiflow(zst);
1231 		}
1232 	}
1233 	splx(s);
1234 	return (1);
1235 }
1236 
1237 /*
1238  * Internal version of zshwiflow
1239  * Called at splzs()
1240  */
1241 void
1242 zs_hwiflow(struct zstty_softc *zst)
1243 {
1244 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
1245 
1246 	if (cs->cs_wr5_rts == 0)
1247 		return;
1248 
1249 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1250 
1251 	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
1252 		CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
1253 		CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
1254 	} else {
1255 		SET(ccs->cs_preg[5], cs->cs_wr5_rts);
1256 		SET(ccs->cs_creg[5], cs->cs_wr5_rts);
1257 	}
1258 	zs_write_reg(ccs, 5, ccs->cs_creg[5]);
1259 }
1260 
1261 
1262 /****************************************************************
1263  * Interface to the lower layer (zscc)
1264  ****************************************************************/
1265 
1266 void zstty_rxsoft(struct zstty_softc *, struct tty *);
1267 void zstty_txsoft(struct zstty_softc *, struct tty *);
1268 void zstty_stsoft(struct zstty_softc *, struct tty *);
1269 void zstty_diag(void *);
1270 
1271 /*
1272  * Receiver Ready interrupt.
1273  * Called at splzs().
1274  */
1275 void
1276 zstty_rxint(struct zs_chanstate *cs)
1277 {
1278 	struct zstty_softc *zst = cs->cs_private;
1279 	uint8_t *put, *end;
1280 	u_int cc;
1281 	uint8_t rr0, rr1, c;
1282 
1283 	end = zst->zst_ebuf;
1284 	put = zst->zst_rbput;
1285 	cc = zst->zst_rbavail;
1286 
1287 	while (cc > 0) {
1288 		/*
1289 		 * First read the status, because reading the received char
1290 		 * destroys the status of this char.
1291 		 */
1292 		rr1 = zs_read_reg(cs, 1);
1293 		c = zs_read_data(cs);
1294 
1295 		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1296 			/* Clear the receive error. */
1297 			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1298 		}
1299 
1300 		put[0] = c;
1301 		put[1] = rr1;
1302 		put += 2;
1303 		if (put >= end)
1304 			put = zst->zst_rbuf;
1305 		cc--;
1306 
1307 		rr0 = zs_read_csr(cs);
1308 		if (!ISSET(rr0, ZSRR0_RX_READY))
1309 			break;
1310 	}
1311 
1312 	/*
1313 	 * Current string of incoming characters ended because
1314 	 * no more data was available or we ran out of space.
1315 	 * Schedule a receive event if any data was received.
1316 	 * If we're out of space, turn off receive interrupts.
1317 	 */
1318 	zst->zst_rbput = put;
1319 	zst->zst_rbavail = cc;
1320 	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1321 		zst->zst_rx_ready = 1;
1322 		cs->cs_softreq = 1;
1323 	}
1324 
1325 	/*
1326 	 * See if we are in danger of overflowing a buffer. If
1327 	 * so, use hardware flow control to ease the pressure.
1328 	 */
1329 	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
1330 	    cc < zst->zst_r_hiwat) {
1331 		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1332 		zs_hwiflow(zst);
1333 	}
1334 
1335 	/*
1336 	 * If we're out of space, disable receive interrupts
1337 	 * until the queue has drained a bit.
1338 	 */
1339 	if (!cc) {
1340 		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1341 		CLR(cs->cs_preg[1], ZSWR1_RIE);
1342 		cs->cs_creg[1] = cs->cs_preg[1];
1343 		zs_write_reg(cs, 1, cs->cs_creg[1]);
1344 	}
1345 }
1346 
1347 /*
1348  * Transmitter Ready interrupt.
1349  * Called at splzs().
1350  */
1351 void
1352 zstty_txint(struct zs_chanstate *cs)
1353 {
1354 	struct zstty_softc *zst = cs->cs_private;
1355 	int rr0;
1356 
1357 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
1358 
1359 	/*
1360 	 * If we've delayed a parameter change, do it now, and restart
1361 	 * output.
1362 	 */
1363 	if (cs->cs_heldchange) {
1364 		zs_loadchannelregs(cs);
1365 		cs->cs_heldchange = 0;
1366 		zst->zst_tbc = zst->zst_heldtbc;
1367 		zst->zst_heldtbc = 0;
1368 	}
1369 
1370 	while (zst->zst_tbc > 0) {
1371 		rr0 = zs_read_csr(cs);
1372 		if ((rr0 & ZSRR0_TX_READY) == 0)
1373 			break;
1374 
1375 		zs_write_data(cs, *zst->zst_tba);
1376 		zst->zst_tbc--;
1377 		zst->zst_tba++;
1378 	}
1379 
1380 	if (zst->zst_tbc == 0) {
1381 		if (zst->zst_tx_busy) {
1382 			zst->zst_tx_busy = 0;
1383 			zst->zst_tx_done = 1;
1384 			cs->cs_softreq = 1;
1385 		}
1386 	}
1387 }
1388 
1389 #ifdef DDB
1390 #include <ddb/db_var.h>
1391 #define	DB_CONSOLE	db_console
1392 #else
1393 #define	DB_CONSOLE	0
1394 #endif
1395 
1396 /*
1397  * Status Change interrupt.
1398  * Called at splzs().
1399  */
1400 void
1401 zstty_stint(struct zs_chanstate *cs, int force)
1402 {
1403 	struct zstty_softc *zst = cs->cs_private;
1404 	struct tty *tp = zst->zst_tty;
1405 	uint8_t rr0, delta;
1406 
1407 	rr0 = zs_read_csr(cs);
1408 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
1409 
1410 	/*
1411 	 * Check here for console break, so that we can abort
1412 	 * even when interrupts are locking up the machine.
1413 	 */
1414 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) &&
1415 	    ISSET(rr0, ZSRR0_BREAK) && DB_CONSOLE)
1416 		zs_abort(cs);
1417 
1418 	if (!force)
1419 		delta = rr0 ^ cs->cs_rr0;
1420 	else
1421 		delta = cs->cs_rr0_mask;
1422 
1423 	ttytstamp(tp, cs->cs_rr0 & ZSRR0_CTS, rr0 & ZSRR0_CTS,
1424 	    cs->cs_rr0 & ZSRR0_DCD, rr0 & ZSRR0_DCD);
1425 
1426 	cs->cs_rr0 = rr0;
1427 
1428 	if (ISSET(delta, cs->cs_rr0_mask)) {
1429 		SET(cs->cs_rr0_delta, delta);
1430 
1431 		/*
1432 		 * Stop output immediately if we lose the output
1433 		 * flow control signal or carrier detect.
1434 		 */
1435 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
1436 			zst->zst_tbc = 0;
1437 			zst->zst_heldtbc = 0;
1438 		}
1439 
1440 		zst->zst_st_check = 1;
1441 		cs->cs_softreq = 1;
1442 	}
1443 }
1444 
1445 void
1446 zstty_diag(void *arg)
1447 {
1448 	struct zstty_softc *zst = arg;
1449 	int overflows, floods;
1450 	int s;
1451 
1452 	s = splzs();
1453 	overflows = zst->zst_overflows;
1454 	zst->zst_overflows = 0;
1455 	floods = zst->zst_floods;
1456 	zst->zst_floods = 0;
1457 	zst->zst_errors = 0;
1458 	splx(s);
1459 
1460 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1461 	    zst->zst_dev.dv_xname,
1462 	    overflows, overflows == 1 ? "" : "s",
1463 	    floods, floods == 1 ? "" : "s");
1464 }
1465 
1466 void
1467 zstty_rxsoft(struct zstty_softc *zst, struct tty *tp)
1468 {
1469 	struct zs_chanstate *cs = zst->zst_cs;
1470 	int (*rint)(int, struct tty *) = linesw[tp->t_line].l_rint;
1471 	uint8_t *get, *end;
1472 	u_int cc, scc;
1473 	uint8_t rr1;
1474 	int code;
1475 	int s;
1476 
1477 	end = zst->zst_ebuf;
1478 	get = zst->zst_rbget;
1479 	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
1480 
1481 	if (cc == zstty_rbuf_size) {
1482 		zst->zst_floods++;
1483 		if (zst->zst_errors++ == 0)
1484 			timeout_add_sec(&zst->zst_diag_ch, 60);
1485 	}
1486 
1487 	/* If not yet open, drop the entire buffer content here */
1488 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
1489 		get += cc << 1;
1490 		if (get >= end)
1491 			get -= zstty_rbuf_size << 1;
1492 		cc = 0;
1493 	}
1494 	while (cc) {
1495 		code = get[0];
1496 		rr1 = get[1];
1497 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
1498 			if (ISSET(rr1, ZSRR1_DO)) {
1499 				zst->zst_overflows++;
1500 				if (zst->zst_errors++ == 0)
1501 					timeout_add_sec(&zst->zst_diag_ch, 60);
1502 			}
1503 			if (ISSET(rr1, ZSRR1_FE))
1504 				SET(code, TTY_FE);
1505 			if (ISSET(rr1, ZSRR1_PE))
1506 				SET(code, TTY_PE);
1507 		}
1508 		if ((*rint)(code, tp) == -1) {
1509 			/*
1510 			 * The line discipline's buffer is out of space.
1511 			 */
1512 			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1513 				/*
1514 				 * We're either not using flow control, or the
1515 				 * line discipline didn't tell us to block for
1516 				 * some reason.  Either way, we have no way to
1517 				 * know when there's more space available, so
1518 				 * just drop the rest of the data.
1519 				 */
1520 				get += cc << 1;
1521 				if (get >= end)
1522 					get -= zstty_rbuf_size << 1;
1523 				cc = 0;
1524 			} else {
1525 				/*
1526 				 * Don't schedule any more receive processing
1527 				 * until the line discipline tells us there's
1528 				 * space available (through comhwiflow()).
1529 				 * Leave the rest of the data in the input
1530 				 * buffer.
1531 				 */
1532 				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1533 			}
1534 			break;
1535 		}
1536 		get += 2;
1537 		if (get >= end)
1538 			get = zst->zst_rbuf;
1539 		cc--;
1540 	}
1541 
1542 	if (cc != scc) {
1543 		zst->zst_rbget = get;
1544 		s = splzs();
1545 		cc = zst->zst_rbavail += scc - cc;
1546 		/* Buffers should be ok again, release possible block. */
1547 		if (cc >= zst->zst_r_lowat) {
1548 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
1549 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1550 				SET(cs->cs_preg[1], ZSWR1_RIE);
1551 				cs->cs_creg[1] = cs->cs_preg[1];
1552 				zs_write_reg(cs, 1, cs->cs_creg[1]);
1553 			}
1554 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
1555 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1556 				zs_hwiflow(zst);
1557 			}
1558 		}
1559 		splx(s);
1560 	}
1561 }
1562 
1563 void
1564 zstty_txsoft(struct zstty_softc *zst, struct tty *tp)
1565 {
1566 	int s;
1567 
1568 	CLR(tp->t_state, TS_BUSY);
1569 	if (ISSET(tp->t_state, TS_FLUSH))
1570 		CLR(tp->t_state, TS_FLUSH);
1571 	else {
1572 		s = splzs();
1573 		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
1574 		splx(s);
1575 	}
1576 	(*linesw[tp->t_line].l_start)(tp);
1577 }
1578 
1579 void
1580 zstty_stsoft(struct zstty_softc *zst, struct tty *tp)
1581 {
1582 	struct zs_chanstate *cs = zst->zst_cs;
1583 	uint8_t rr0, delta;
1584 	int s;
1585 
1586 	s = splzs();
1587 	rr0 = cs->cs_rr0;
1588 	delta = cs->cs_rr0_delta;
1589 	cs->cs_rr0_delta = 0;
1590 	splx(s);
1591 
1592 	if (ISSET(delta, cs->cs_rr0_dcd)) {
1593 		/*
1594 		 * Inform the tty layer that carrier detect changed.
1595 		 */
1596 		(void)(*linesw[tp->t_line].l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
1597 	}
1598 
1599 	if (ISSET(delta, cs->cs_rr0_cts)) {
1600 		/* Block or unblock output according to flow control. */
1601 		if (ISSET(rr0, cs->cs_rr0_cts)) {
1602 			zst->zst_tx_stopped = 0;
1603 			(*linesw[tp->t_line].l_start)(tp);
1604 		} else {
1605 			zst->zst_tx_stopped = 1;
1606 		}
1607 	}
1608 }
1609 
1610 /*
1611  * Software interrupt.  Called at zssoft
1612  *
1613  * The main job to be done here is to empty the input ring
1614  * by passing its contents up to the tty layer.  The ring is
1615  * always emptied during this operation, therefore the ring
1616  * must not be larger than the space after "high water" in
1617  * the tty layer, or the tty layer might drop our input.
1618  *
1619  * Note: an "input blockage" condition is assumed to exist if
1620  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
1621  */
1622 void
1623 zstty_softint(struct zs_chanstate *cs)
1624 {
1625 	struct zstty_softc *zst = cs->cs_private;
1626 	struct tty *tp = zst->zst_tty;
1627 	int s;
1628 
1629 	s = spltty();
1630 
1631 	if (zst->zst_rx_ready) {
1632 		zst->zst_rx_ready = 0;
1633 		zstty_rxsoft(zst, tp);
1634 	}
1635 
1636 	if (zst->zst_st_check) {
1637 		zst->zst_st_check = 0;
1638 		zstty_stsoft(zst, tp);
1639 	}
1640 
1641 	if (zst->zst_tx_done) {
1642 		zst->zst_tx_done = 0;
1643 		zstty_txsoft(zst, tp);
1644 	}
1645 
1646 	splx(s);
1647 }
1648 
1649 struct zsops zsops_tty = {
1650 	zstty_rxint,	/* receive char available */
1651 	zstty_stint,	/* external/status */
1652 	zstty_txint,	/* xmit buffer empty */
1653 	zstty_softint,	/* process software interrupt */
1654 };
1655