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