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