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