[BACK]Return to z8530tty.c CVS log [TXT][DIR] Up to [local] / sys / arch / sparc / dev

Annotation of sys/arch/sparc/dev/z8530tty.c, Revision 1.1.1.1

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

CVSweb