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