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