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