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