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