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