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