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