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