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