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