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