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