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