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