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

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

1.1       nbrk        1: /*     $OpenBSD: z8530tty.c,v 1.6 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 <macppc/dev/z8530reg.h>
                    112: #include <machine/z8530var.h>
                    113:
                    114: #include <dev/cons.h>
                    115:
                    116: #ifdef KGDB
                    117: extern int zs_check_kgdb();
                    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(struct device *parent, void *match, void *aux)
                    241: {
                    242:        struct cfdata *cf = match;
                    243:        struct zsc_attach_args *args = aux;
                    244:
                    245:        /* Exact match is better than wildcard. */
                    246:        if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
                    247:                return 2;
                    248:
                    249:        /* This driver accepts wildcard. */
                    250:        if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
                    251:                return 1;
                    252:
                    253:        return 0;
                    254: }
                    255:
                    256: void
                    257: zstty_attach(struct device *parent, struct device *self, void   *aux)
                    258: {
                    259:        struct zsc_softc *zsc = (void *) parent;
                    260:        struct zstty_softc *zst = (void *) self;
                    261:        struct zsc_attach_args *args = aux;
                    262:        struct zs_chanstate *cs;
                    263:        struct cfdata *cf;
                    264:        struct tty *tp;
                    265:        int channel, s, tty_unit;
                    266:        dev_t dev;
                    267:        char *i, *o;
                    268:        int maj;
                    269:
                    270:        cf = zst->zst_dev.dv_cfdata;
                    271:
                    272:         timeout_set(&zst->zst_diag_ch, zstty_diag, zst);
                    273:
                    274:        tty_unit = zst->zst_dev.dv_unit;
                    275:        channel = args->channel;
                    276:        cs = zsc->zsc_cs[channel];
                    277:        cs->cs_private = zst;
                    278:        cs->cs_ops = &zsops_tty;
                    279:
                    280:        zst->zst_cs = cs;
                    281:        zst->zst_swflags = cf->cf_flags;        /* softcar, etc. */
                    282:        zst->zst_hwflags = args->hwflags;
                    283:        /* locate the major number */
                    284:        for (maj = 0; maj < nchrdev; maj++)
                    285:                if (cdevsw[maj].d_open == zsopen)
                    286:                        break;
                    287:
                    288:        dev = makedev(maj, tty_unit);
                    289:
                    290:        if (zst->zst_swflags)
                    291:                printf(" flags 0x%x", zst->zst_swflags);
                    292:
                    293:        /*
                    294:         * Check whether we serve as a console device.
                    295:         * XXX - split console input/output channels aren't
                    296:         *       supported yet on /dev/console
                    297:         */
                    298:        i = o = NULL;
                    299:        if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
                    300:                i = "input";
                    301:                if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
                    302:                        args->consdev->cn_dev = dev;
                    303:                        cn_tab->cn_pollc = args->consdev->cn_pollc;
                    304:                        cn_tab->cn_getc = args->consdev->cn_getc;
                    305:                }
                    306:                cn_tab->cn_dev = dev;
                    307:                /* Set console magic to BREAK */
                    308:        }
                    309:        if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
                    310:                o = "output";
                    311:                if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
                    312:                        cn_tab->cn_putc = args->consdev->cn_putc;
                    313:                }
                    314:                cn_tab->cn_dev = dev;
                    315:        }
                    316:        if (i != NULL || o != NULL)
                    317:                printf(" (console %s)", i ? (o ? "i/o" : i) : o);
                    318:
                    319: #ifdef KGDB
                    320:        /*
                    321:         * Allow kgdb to "take over" this port.  If this port is
                    322:         * NOT the kgdb port, zs_check_kgdb() will return zero.
                    323:         * If it IS the kgdb port, it will print "kgdb,...\n"
                    324:         * and then return non-zero.
                    325:         */
                    326:        if (zs_check_kgdb(cs, dev)) {
                    327:                printf(" (kgdb)\n");
                    328:                /*
                    329:                 * This is the kgdb port (exclusive use)
                    330:                 * so skip the normal attach code.
                    331:                 */
                    332:                return;
                    333:        }
                    334: #endif
                    335:
                    336:        if (strcmp(args->type, "keyboard") == 0 ||
                    337:            strcmp(args->type, "mouse") == 0)
                    338:                printf(": %s", args->type);
                    339:
                    340:        printf("\n");
                    341:
                    342:        tp = ttymalloc();
                    343:        tp->t_dev = dev;
                    344:        tp->t_oproc = zsstart;
                    345:        tp->t_param = zsparam;
                    346:        tp->t_hwiflow = zshwiflow;
                    347:
                    348:        zst->zst_tty = tp;
                    349:        zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
                    350:        zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
                    351:        /* Disable the high water mark. */
                    352:        zst->zst_r_hiwat = 0;
                    353:        zst->zst_r_lowat = 0;
                    354:        zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
                    355:        zst->zst_rbavail = zstty_rbuf_size;
                    356:
                    357:        /* if there are no enable/disable functions, assume the device
                    358:           is always enabled */
                    359:        if (!cs->enable)
                    360:                cs->enabled = 1;
                    361:
                    362:        /*
                    363:         * Hardware init
                    364:         */
                    365:        if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
                    366:                /* Call zsparam similar to open. */
                    367:                struct termios t;
                    368:
                    369:                /* Wait a while for previous console output to complete */
                    370:                DELAY(10000);
                    371:
                    372:                /* Setup the "new" parameters in t. */
                    373:                t.c_ispeed = 0;
                    374:                t.c_ospeed = cs->cs_defspeed;
                    375:                t.c_cflag = cs->cs_defcflag;
                    376:
                    377:                s = splzs();
                    378:
                    379:                /*
                    380:                 * Turn on receiver and status interrupts.
                    381:                 * We defer the actual write of the register to zsparam(),
                    382:                 * but we must make sure status interrupts are turned on by
                    383:                 * the time zsparam() reads the initial rr0 state.
                    384:                 */
                    385:                SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
                    386:
                    387:                splx(s);
                    388:
                    389:                /* Make sure zsparam will see changes. */
                    390:                tp->t_ospeed = 0;
                    391:                (void) zsparam(tp, &t);
                    392:
                    393:                s = splzs();
                    394:
                    395:                /* Make sure DTR is on now. */
                    396:                zs_modem(zst, 1);
                    397:
                    398:                splx(s);
                    399:        } else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
                    400:                /* Not the console; may need reset. */
                    401:                int reset;
                    402:
                    403:                reset = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
                    404:
                    405:                s = splzs();
                    406:
                    407:                zs_write_reg(cs, 9, reset);
                    408:
                    409:                /* Will raise DTR in open. */
                    410:                zs_modem(zst, 0);
                    411:
                    412:                splx(s);
                    413:        }
                    414: }
                    415:
                    416:
                    417: /*
                    418:  * Return pointer to our tty.
                    419:  */
                    420: struct tty *
                    421: zstty(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(struct zstty_softc *zst)
                    437: {
                    438:        struct zs_chanstate *cs = zst->zst_cs;
                    439:        struct tty *tp = zst->zst_tty;
                    440:        int s;
                    441:
                    442:        s = splzs();
                    443:
                    444:        /* If we were asserting flow control, then deassert it. */
                    445:        SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
                    446:        zs_hwiflow(zst);
                    447:
                    448:        /* Clear any break condition set with TIOCSBRK. */
                    449:        zs_break(cs, 0);
                    450:
                    451:        /* Turn off PPS capture on last close. */
                    452:        zst->zst_ppsmask = 0;
                    453:
                    454:        /*
                    455:         * Hang up if necessary.  Wait a bit, so the other side has time to
                    456:         * notice even if we immediately open the port again.
                    457:         */
                    458:        if (ISSET(tp->t_cflag, HUPCL)) {
                    459:                zs_modem(zst, 0);
                    460:                (void) tsleep(cs, TTIPRI, ttclos, hz);
                    461:        }
                    462:
                    463:        /* Turn off interrupts if not the console. */
                    464:        if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
                    465:                CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
                    466:                cs->cs_creg[1] = cs->cs_preg[1];
                    467:                zs_write_reg(cs, 1, cs->cs_creg[1]);
                    468:        }
                    469:
                    470: /* Call the power management hook. */
                    471:        if (cs->disable) {
                    472: #ifdef DIAGNOSTIC
                    473:                if (!cs->enabled)
                    474:                        panic("zs_shutdown: not enabled?");
                    475: #endif
                    476:                (*cs->disable)(zst->zst_cs);
                    477:        }
                    478:
                    479:        splx(s);
                    480: }
                    481:
                    482: /*
                    483:  * Open a zs serial (tty) port.
                    484:  */
                    485: int
                    486: zsopen(dev_t dev, int flags, int mode, struct proc *p)
                    487: {
                    488:        struct tty *tp;
                    489:        struct zs_chanstate *cs;
                    490:        struct zstty_softc *zst;
                    491:        int s, s2;
                    492:        int error, unit;
                    493:
                    494:        unit = minor(dev);
                    495:        if (unit >= zstty_cd.cd_ndevs)
                    496:                return (ENXIO);
                    497:        zst = zstty_cd.cd_devs[unit];
                    498:        if (zst == NULL)
                    499:                return (ENXIO);
                    500:        tp = zst->zst_tty;
                    501:        cs = zst->zst_cs;
                    502:
                    503:        /* If KGDB took the line, then tp==NULL */
                    504:        if (tp == NULL)
                    505:                return (EBUSY);
                    506:
                    507:        if (ISSET(tp->t_state, TS_ISOPEN) &&
                    508:            ISSET(tp->t_state, TS_XCLUDE) &&
                    509:            p->p_ucred->cr_uid != 0)
                    510:                return (EBUSY);
                    511:
                    512:        s = spltty();
                    513:
                    514:        /*
                    515:         * Do the following iff this is a first open.
                    516:         */
                    517:        if (!ISSET(tp->t_state, TS_ISOPEN)) {
                    518:                struct termios t;
                    519:
                    520:                tp->t_dev = dev;
                    521:
                    522:                /* Call the power management hook. */
                    523:                if (cs->enable) {
                    524:                        if ((*cs->enable)(cs)) {
                    525:                                splx(s);
                    526:                                printf("%s: device enable failed\n",
                    527:                                zst->zst_dev.dv_xname);
                    528:                                return (EIO);
                    529:                        }
                    530:                }
                    531:
                    532:                /*
                    533:                 * Initialize the termios status to the defaults.  Add in the
                    534:                 * sticky bits from TIOCSFLAGS.
                    535:                 */
                    536:                t.c_ispeed = 0;
                    537:                t.c_ospeed = cs->cs_defspeed;
                    538:                t.c_cflag = cs->cs_defcflag;
                    539:                if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
                    540:                        SET(t.c_cflag, CLOCAL);
                    541:                if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
                    542:                        SET(t.c_cflag, CRTSCTS);
                    543:                if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
                    544:                        SET(t.c_cflag, MDMBUF);
                    545:
                    546:                s2 = splzs();
                    547:
                    548:                /*
                    549:                 * Turn on receiver and status interrupts.
                    550:                 * We defer the actual write of the register to zsparam(),
                    551:                 * but we must make sure status interrupts are turned on by
                    552:                 * the time zsparam() reads the initial rr0 state.
                    553:                 */
                    554:                SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
                    555:
                    556:                /* Clear PPS capture state on first open. */
                    557:                zst->zst_ppsmask = 0;
                    558:
                    559:                splx(s2);
                    560:
                    561:                /* Make sure zsparam will see changes. */
                    562:                tp->t_ospeed = 0;
                    563:                (void) zsparam(tp, &t);
                    564:
                    565:                /*
                    566:                 * Note: zsparam has done: cflag, ispeed, ospeed
                    567:                 * so we just need to do: iflag, oflag, lflag, cc
                    568:                 * For "raw" mode, just leave all zeros.
                    569:                 */
                    570:                if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
                    571:                        tp->t_iflag = TTYDEF_IFLAG;
                    572:                        tp->t_oflag = TTYDEF_OFLAG;
                    573:                        tp->t_lflag = TTYDEF_LFLAG;
                    574:                } else {
                    575:                        tp->t_iflag = 0;
                    576:                        tp->t_oflag = 0;
                    577:                        tp->t_lflag = 0;
                    578:                }
                    579:                ttychars(tp);
                    580:                ttsetwater(tp);
                    581:
                    582:                s2 = splzs();
                    583:
                    584:                /*
                    585:                 * Turn on DTR.  We must always do this, even if carrier is not
                    586:                 * present, because otherwise we'd have to use TIOCSDTR
                    587:                 * immediately after setting CLOCAL, which applications do not
                    588:                 * expect.  We always assert DTR while the device is open
                    589:                 * unless explicitly requested to deassert it.
                    590:                 */
                    591:                zs_modem(zst, 1);
                    592:
                    593:                /* Clear the input ring, and unblock. */
                    594:                zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
                    595:                zst->zst_rbavail = zstty_rbuf_size;
                    596:                zs_iflush(cs);
                    597:                CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
                    598:                zs_hwiflow(zst);
                    599:
                    600:                splx(s2);
                    601:        }
                    602:
                    603:        splx(s);
                    604:
                    605:        error = ((*linesw[tp->t_line].l_open)(dev, tp));
                    606:        if (error)
                    607:                goto bad;
                    608:
                    609:        return (0);
                    610:
                    611: bad:
                    612:        if (!ISSET(tp->t_state, TS_ISOPEN)) {
                    613:                /*
                    614:                 * We failed to open the device, and nobody else had it opened.
                    615:                 * Clean up the state as appropriate.
                    616:                 */
                    617:                zs_shutdown(zst);
                    618:        }
                    619:
                    620:        return (error);
                    621: }
                    622:
                    623: /*
                    624:  * Close a zs serial port.
                    625:  */
                    626: int
                    627: zsclose(dev_t dev, int flags, int mode, struct proc *p)
                    628: {
                    629:        struct zstty_softc *zst;
                    630:        struct zs_chanstate *cs;
                    631:        struct tty *tp;
                    632:
                    633:        zst = zstty_cd.cd_devs[minor(dev)];
                    634:        cs = zst->zst_cs;
                    635:        tp = zst->zst_tty;
                    636:
                    637:        /* XXX This is for cons.c. */
                    638:        if (!ISSET(tp->t_state, TS_ISOPEN))
                    639:                return 0;
                    640:
                    641:        (*linesw[tp->t_line].l_close)(tp, flags);
                    642:        ttyclose(tp);
                    643:
                    644:        if (!ISSET(tp->t_state, TS_ISOPEN)) {
                    645:                /*
                    646:                 * Although we got a last close, the device may still be in
                    647:                 * use; e.g. if this was the dialout node, and there are still
                    648:                 * processes waiting for carrier on the non-dialout node.
                    649:                 */
                    650:                zs_shutdown(zst);
                    651:        }
                    652:
                    653:        return (0);
                    654: }
                    655:
                    656: /*
                    657:  * Read/write zs serial port.
                    658:  */
                    659: int
                    660: zsread(dev_t dev, struct uio *uio, int flags)
                    661: {
                    662:        struct zstty_softc *zst;
                    663:        struct tty *tp;
                    664:
                    665:        zst = zstty_cd.cd_devs[minor(dev)];
                    666:        tp = zst->zst_tty;
                    667:
                    668:        return (*linesw[tp->t_line].l_read)(tp, uio, flags);
                    669: }
                    670:
                    671: int
                    672: zswrite(dev_t dev, struct uio *uio, 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_write)(tp, uio, flags);
                    681: }
                    682:
                    683: #define TIOCFLAG_ALL (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | \
                    684:                       TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF )
                    685:
                    686: int
                    687: zsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
                    688: {
                    689:        struct zstty_softc *zst;
                    690:        struct zs_chanstate *cs;
                    691:        struct tty *tp;
                    692:        int error;
                    693:        int s;
                    694:
                    695:        zst = zstty_cd.cd_devs[minor(dev)];
                    696:        cs = zst->zst_cs;
                    697:        tp = zst->zst_tty;
                    698:
                    699:        error = ((*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p));
                    700:        if (error >= 0)
                    701:                return (error);
                    702:
                    703:        error = ttioctl(tp, cmd, data, flag, p);
                    704:        if (error >= 0)
                    705:                return (error);
                    706:
                    707: #ifdef ZS_MD_IOCTL
                    708:        error = ZS_MD_IOCTL;
                    709:        if (error >= 0)
                    710:                return (error);
                    711: #endif /* ZS_MD_IOCTL */
                    712:
                    713:        error = 0;
                    714:
                    715:        s = splzs();
                    716:
                    717:        switch (cmd) {
                    718:        case TIOCSBRK:
                    719:                zs_break(cs, 1);
                    720:                break;
                    721:
                    722:        case TIOCCBRK:
                    723:                zs_break(cs, 0);
                    724:                break;
                    725:
                    726:        case TIOCGFLAGS:
                    727:                *(int *)data = zst->zst_swflags;
                    728:                break;
                    729:
                    730:        case TIOCSFLAGS:
                    731:                error = suser(p, 0);
                    732:                if (error != 0)
                    733:                        break;
                    734:                zst->zst_swflags = *(int *)data;
                    735:                break;
                    736:
                    737:        case TIOCSDTR:
                    738:                zs_modem(zst, 1);
                    739:                break;
                    740:
                    741:        case TIOCCDTR:
                    742:                zs_modem(zst, 0);
                    743:                break;
                    744:
                    745:        case TIOCMSET:
                    746:        case TIOCMBIS:
                    747:        case TIOCMBIC:
                    748:                tiocm_to_zs(zst, cmd, *(int *)data);
                    749:                break;
                    750:
                    751:        case TIOCMGET:
                    752:                *(int *)data = zs_to_tiocm(zst);
                    753:                break;
                    754:
                    755:        default:
                    756:                error = ENOTTY;
                    757:                break;
                    758:        }
                    759:
                    760:        splx(s);
                    761:
                    762:        return (error);
                    763: }
                    764:
                    765: /*
                    766:  * Start or restart transmission.
                    767:  */
                    768: void
                    769: zsstart(struct tty *tp)
                    770: {
                    771:        struct zstty_softc *zst;
                    772:        struct zs_chanstate *cs;
                    773:        int s;
                    774:
                    775:        zst = zstty_cd.cd_devs[minor(tp->t_dev)];
                    776:        cs = zst->zst_cs;
                    777:
                    778:        s = spltty();
                    779:        if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
                    780:                goto out;
                    781:        if (zst->zst_tx_stopped)
                    782:                goto out;
                    783:
                    784:        if (tp->t_outq.c_cc <= tp->t_lowat) {
                    785:                if (ISSET(tp->t_state, TS_ASLEEP)) {
                    786:                        CLR(tp->t_state, TS_ASLEEP);
                    787:                        wakeup((caddr_t)&tp->t_outq);
                    788:                }
                    789:                selwakeup(&tp->t_wsel);
                    790:                if (tp->t_outq.c_cc == 0)
                    791:                        goto out;
                    792:        }
                    793:
                    794:        /* Grab the first contiguous region of buffer space. */
                    795:        {
                    796:                u_char *tba;
                    797:                int tbc;
                    798:
                    799:                tba = tp->t_outq.c_cf;
                    800:                tbc = ndqb(&tp->t_outq, 0);
                    801:
                    802:                (void) splzs();
                    803:
                    804:                zst->zst_tba = tba;
                    805:                zst->zst_tbc = tbc;
                    806:        }
                    807:
                    808:        SET(tp->t_state, TS_BUSY);
                    809:        zst->zst_tx_busy = 1;
                    810:
                    811:        /* Enable transmit completion interrupts if necessary. */
                    812:        if (!ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
                    813:                SET(cs->cs_preg[1], ZSWR1_TIE);
                    814:                cs->cs_creg[1] = cs->cs_preg[1];
                    815:                zs_write_reg(cs, 1, cs->cs_creg[1]);
                    816:        }
                    817:
                    818:        /* Output the first character of the contiguous buffer. */
                    819:        zs_write_data(cs, *zst->zst_tba);
                    820:        zst->zst_tbc--;
                    821:        zst->zst_tba++;
                    822: out:
                    823:        splx(s);
                    824: }
                    825:
                    826: /*
                    827:  * Stop output, e.g., for ^S or output flush.
                    828:  */
                    829: int
                    830: zsstop(struct tty *tp, int flag)
                    831: {
                    832:        struct zstty_softc *zst;
                    833:        struct zs_chanstate *cs;
                    834:        int s;
                    835:
                    836:        zst = zstty_cd.cd_devs[minor(tp->t_dev)];
                    837:        cs = zst->zst_cs;
                    838:
                    839:        s = splzs();
                    840:        if (ISSET(tp->t_state, TS_BUSY)) {
                    841:                /* Stop transmitting at the next chunk. */
                    842:                zst->zst_tbc = 0;
                    843:                zst->zst_heldtbc = 0;
                    844:                if (!ISSET(tp->t_state, TS_TTSTOP))
                    845:                        SET(tp->t_state, TS_FLUSH);
                    846:        }
                    847:        splx(s);
                    848:        return (0);
                    849: }
                    850:
                    851: /*
                    852:  * Set ZS tty parameters from termios.
                    853:  * XXX - Should just copy the whole termios after
                    854:  * making sure all the changes could be done.
                    855:  */
                    856: int
                    857: zsparam(struct tty *tp, struct termios *t)
                    858: {
                    859:        struct zstty_softc *zst;
                    860:        struct zs_chanstate *cs;
                    861:        int ospeed, cflag;
                    862:        u_char tmp3, tmp4, tmp5;
                    863:        int s, error;
                    864:
                    865:        zst = zstty_cd.cd_devs[minor(tp->t_dev)];
                    866:        cs = zst->zst_cs;
                    867:
                    868:        ospeed = t->c_ospeed;
                    869:        cflag = t->c_cflag;
                    870:
                    871:        /* Check requested parameters. */
                    872:        if (ospeed < 0)
                    873:                return (EINVAL);
                    874:        if (t->c_ispeed && t->c_ispeed != ospeed)
                    875:                return (EINVAL);
                    876:
                    877:        /*
                    878:         * For the console, always force CLOCAL and !HUPCL, so that the port
                    879:         * is always active.
                    880:         */
                    881:        if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
                    882:            ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
                    883:                SET(cflag, CLOCAL);
                    884:                CLR(cflag, HUPCL);
                    885:        }
                    886:
                    887:        /*
                    888:         * Only whack the UART when params change.
                    889:         * Some callers need to clear tp->t_ospeed
                    890:         * to make sure initialization gets done.
                    891:         */
                    892:        if (tp->t_ospeed == ospeed &&
                    893:            tp->t_cflag == cflag)
                    894:                return (0);
                    895:
                    896:        /*
                    897:         * Call MD functions to deal with changed
                    898:         * clock modes or H/W flow control modes.
                    899:         * The BRG divisor is set now. (reg 12,13)
                    900:         */
                    901:        error = zs_set_speed(cs, ospeed);
                    902:        if (error)
                    903:                return (error);
                    904:        error = zs_set_modes(cs, cflag);
                    905:        if (error)
                    906:                return (error);
                    907:
                    908:        /*
                    909:         * Block interrupts so that state will not
                    910:         * be altered until we are done setting it up.
                    911:         *
                    912:         * Initial values in cs_preg are set before
                    913:         * our attach routine is called.  The master
                    914:         * interrupt enable is handled by zsc.c
                    915:         */
                    916:        s = splzs();
                    917:
                    918:        /*
                    919:         * Recalculate which status ints to enable.
                    920:         */
                    921:        zs_maskintr(zst);
                    922:
                    923:        /* Recompute character size bits. */
                    924:        tmp3 = cs->cs_preg[3];
                    925:        tmp5 = cs->cs_preg[5];
                    926:        CLR(tmp3, ZSWR3_RXSIZE);
                    927:        CLR(tmp5, ZSWR5_TXSIZE);
                    928:        switch (ISSET(cflag, CSIZE)) {
                    929:        case CS5:
                    930:                SET(tmp3, ZSWR3_RX_5);
                    931:                SET(tmp5, ZSWR5_TX_5);
                    932:                break;
                    933:        case CS6:
                    934:                SET(tmp3, ZSWR3_RX_6);
                    935:                SET(tmp5, ZSWR5_TX_6);
                    936:                break;
                    937:        case CS7:
                    938:                SET(tmp3, ZSWR3_RX_7);
                    939:                SET(tmp5, ZSWR5_TX_7);
                    940:                break;
                    941:        case CS8:
                    942:                SET(tmp3, ZSWR3_RX_8);
                    943:                SET(tmp5, ZSWR5_TX_8);
                    944:                break;
                    945:        }
                    946:        cs->cs_preg[3] = tmp3;
                    947:        cs->cs_preg[5] = tmp5;
                    948:
                    949:        /*
                    950:         * Recompute the stop bits and parity bits.  Note that
                    951:         * zs_set_speed() may have set clock selection bits etc.
                    952:         * in wr4, so those must preserved.
                    953:         */
                    954:        tmp4 = cs->cs_preg[4];
                    955:        CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
                    956:        if (ISSET(cflag, CSTOPB))
                    957:                SET(tmp4, ZSWR4_TWOSB);
                    958:        else
                    959:                SET(tmp4, ZSWR4_ONESB);
                    960:        if (!ISSET(cflag, PARODD))
                    961:                SET(tmp4, ZSWR4_EVENP);
                    962:        if (ISSET(cflag, PARENB))
                    963:                SET(tmp4, ZSWR4_PARENB);
                    964:        cs->cs_preg[4] = tmp4;
                    965:
                    966:        /* And copy to tty. */
                    967:        tp->t_ispeed = 0;
                    968:        tp->t_ospeed = ospeed;
                    969:        tp->t_cflag = cflag;
                    970:
                    971:        /*
                    972:         * If nothing is being transmitted, set up new current values,
                    973:         * else mark them as pending.
                    974:         */
                    975:        if (!cs->cs_heldchange) {
                    976:                if (zst->zst_tx_busy) {
                    977:                        zst->zst_heldtbc = zst->zst_tbc;
                    978:                        zst->zst_tbc = 0;
                    979:                        cs->cs_heldchange = 1;
                    980:                } else
                    981:                        zs_loadchannelregs(cs);
                    982:        }
                    983:
                    984:        /*
                    985:         * If hardware flow control is disabled, turn off the buffer water
                    986:         * marks and unblock any soft flow control state.  Otherwise, enable
                    987:         * the water marks.
                    988:         */
                    989:        if (!ISSET(cflag, CHWFLOW)) {
                    990:                zst->zst_r_hiwat = 0;
                    991:                zst->zst_r_lowat = 0;
                    992:                if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
                    993:                        CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
                    994:                        zst->zst_rx_ready = 1;
                    995:                        cs->cs_softreq = 1;
                    996:                }
                    997:                if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
                    998:                        CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
                    999:                        zs_hwiflow(zst);
                   1000:                }
                   1001:        } else {
                   1002:                zst->zst_r_hiwat = zstty_rbuf_hiwat;
                   1003:                zst->zst_r_lowat = zstty_rbuf_lowat;
                   1004:        }
                   1005:
                   1006:        /*
                   1007:         * Force a recheck of the hardware carrier and flow control status,
                   1008:         * since we may have changed which bits we're looking at.
                   1009:         */
                   1010:        zstty_stint(cs, 1);
                   1011:
                   1012:        splx(s);
                   1013:
                   1014:        /*
                   1015:         * If hardware flow control is disabled, unblock any hard flow control
                   1016:         * state.
                   1017:         */
                   1018:        if (!ISSET(cflag, CHWFLOW)) {
                   1019:                if (zst->zst_tx_stopped) {
                   1020:                        zst->zst_tx_stopped = 0;
                   1021:                        zsstart(tp);
                   1022:                }
                   1023:        }
                   1024:
                   1025:        zstty_softint(cs);
                   1026:
                   1027:        return (0);
                   1028: }
                   1029:
                   1030: /*
                   1031:  * Compute interrupt enable bits and set in the pending bits. Called both
                   1032:  * in zsparam() and when PPS (pulse per second timing) state changes.
                   1033:  * Must be called at splzs().
                   1034:  */
                   1035: void
                   1036: zs_maskintr(struct zstty_softc *zst)
                   1037: {
                   1038:        struct zs_chanstate *cs = zst->zst_cs;
                   1039:        int tmp15;
                   1040:
                   1041:        cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
                   1042:        if (zst->zst_ppsmask != 0)
                   1043:                cs->cs_rr0_mask |= cs->cs_rr0_pps;
                   1044:        tmp15 = cs->cs_preg[15];
                   1045:        if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
                   1046:                SET(tmp15, ZSWR15_DCD_IE);
                   1047:        else
                   1048:                CLR(tmp15, ZSWR15_DCD_IE);
                   1049:        if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
                   1050:                SET(tmp15, ZSWR15_CTS_IE);
                   1051:        else
                   1052:                CLR(tmp15, ZSWR15_CTS_IE);
                   1053:        cs->cs_preg[15] = tmp15;
                   1054: }
                   1055:
                   1056: /*
                   1057:  * Raise or lower modem control (DTR/RTS) signals.  If a character is
                   1058:  * in transmission, the change is deferred.
                   1059:  */
                   1060: void
                   1061: zs_modem(struct zstty_softc *zst, int onoff)
                   1062: {
                   1063:        struct zs_chanstate *cs = zst->zst_cs;
                   1064:
                   1065:        if (cs->cs_wr5_dtr == 0)
                   1066:                return;
                   1067:
                   1068:        if (onoff)
                   1069:                SET(cs->cs_preg[5], cs->cs_wr5_dtr);
                   1070:        else
                   1071:                CLR(cs->cs_preg[5], cs->cs_wr5_dtr);
                   1072:
                   1073:        if (!cs->cs_heldchange) {
                   1074:                if (zst->zst_tx_busy) {
                   1075:                        zst->zst_heldtbc = zst->zst_tbc;
                   1076:                        zst->zst_tbc = 0;
                   1077:                        cs->cs_heldchange = 1;
                   1078:                } else
                   1079:                        zs_loadchannelregs(cs);
                   1080:        }
                   1081: }
                   1082:
                   1083: void
                   1084: tiocm_to_zs(struct zstty_softc *zst, u_long how, int ttybits)
                   1085: {
                   1086:        struct zs_chanstate *cs = zst->zst_cs;
                   1087:        u_char zsbits;
                   1088:
                   1089:        zsbits = 0;
                   1090:        if (ISSET(ttybits, TIOCM_DTR))
                   1091:                SET(zsbits, ZSWR5_DTR);
                   1092:        if (ISSET(ttybits, TIOCM_RTS))
                   1093:                SET(zsbits, ZSWR5_RTS);
                   1094:
                   1095:        switch (how) {
                   1096:        case TIOCMBIC:
                   1097:                CLR(cs->cs_preg[5], zsbits);
                   1098:                break;
                   1099:
                   1100:        case TIOCMBIS:
                   1101:                SET(cs->cs_preg[5], zsbits);
                   1102:                break;
                   1103:
                   1104:        case TIOCMSET:
                   1105:                CLR(cs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
                   1106:                SET(cs->cs_preg[5], zsbits);
                   1107:                break;
                   1108:        }
                   1109:
                   1110:        if (!cs->cs_heldchange) {
                   1111:                if (zst->zst_tx_busy) {
                   1112:                        zst->zst_heldtbc = zst->zst_tbc;
                   1113:                        zst->zst_tbc = 0;
                   1114:                        cs->cs_heldchange = 1;
                   1115:                } else
                   1116:                        zs_loadchannelregs(cs);
                   1117:        }
                   1118: }
                   1119:
                   1120: int
                   1121: zs_to_tiocm(struct zstty_softc *zst)
                   1122: {
                   1123:        struct zs_chanstate *cs = zst->zst_cs;
                   1124:        u_char zsbits;
                   1125:        int ttybits = 0;
                   1126:
                   1127:        zsbits = cs->cs_preg[5];
                   1128:        if (ISSET(zsbits, ZSWR5_DTR))
                   1129:                SET(ttybits, TIOCM_DTR);
                   1130:        if (ISSET(zsbits, ZSWR5_RTS))
                   1131:                SET(ttybits, TIOCM_RTS);
                   1132:
                   1133:        zsbits = cs->cs_rr0;
                   1134:        if (ISSET(zsbits, ZSRR0_DCD))
                   1135:                SET(ttybits, TIOCM_CD);
                   1136:        if (ISSET(zsbits, ZSRR0_CTS))
                   1137:                SET(ttybits, TIOCM_CTS);
                   1138:
                   1139:        return (ttybits);
                   1140: }
                   1141:
                   1142: /*
                   1143:  * Try to block or unblock input using hardware flow-control.
                   1144:  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
                   1145:  * if this function returns non-zero, the TS_TBLOCK flag will
                   1146:  * be set or cleared according to the "block" arg passed.
                   1147:  */
                   1148: int
                   1149: zshwiflow(struct tty *tp, int block)
                   1150: {
                   1151:        struct zstty_softc *zst;
                   1152:        struct zs_chanstate *cs;
                   1153:        int s;
                   1154:
                   1155:        zst = zstty_cd.cd_devs[minor(tp->t_dev)];
                   1156:        cs = zst->zst_cs;
                   1157:
                   1158:        if (cs->cs_wr5_rts == 0)
                   1159:                return (0);
                   1160:
                   1161:        s = splzs();
                   1162:        if (block) {
                   1163:                if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
                   1164:                        SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
                   1165:                        zs_hwiflow(zst);
                   1166:                }
                   1167:        } else {
                   1168:                if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
                   1169:                        CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
                   1170:                        zst->zst_rx_ready = 1;
                   1171:                        cs->cs_softreq = 1;
                   1172:                }
                   1173:                if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
                   1174:                        CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
                   1175:                        zs_hwiflow(zst);
                   1176:                }
                   1177:        }
                   1178:        splx(s);
                   1179:        return (1);
                   1180: }
                   1181:
                   1182: /*
                   1183:  * Internal version of zshwiflow
                   1184:  * called at splzs
                   1185:  */
                   1186: void
                   1187: zs_hwiflow(struct zstty_softc *zst)
                   1188: {
                   1189:        struct zs_chanstate *cs = zst->zst_cs;
                   1190:
                   1191:        if (cs->cs_wr5_rts == 0)
                   1192:                return;
                   1193:
                   1194:        if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
                   1195:                CLR(cs->cs_preg[5], cs->cs_wr5_rts);
                   1196:                CLR(cs->cs_creg[5], cs->cs_wr5_rts);
                   1197:        } else {
                   1198:                SET(cs->cs_preg[5], cs->cs_wr5_rts);
                   1199:                SET(cs->cs_creg[5], cs->cs_wr5_rts);
                   1200:        }
                   1201:        zs_write_reg(cs, 5, cs->cs_creg[5]);
                   1202: }
                   1203:
                   1204:
                   1205: /****************************************************************
                   1206:  * Interface to the lower layer (zscc)
                   1207:  ****************************************************************/
                   1208:
                   1209: void zstty_rxsoft(struct zstty_softc *, struct tty *);
                   1210: void zstty_txsoft(struct zstty_softc *, struct tty *);
                   1211: void zstty_stsoft(struct zstty_softc *, struct tty *);
                   1212:
                   1213: /*
                   1214:  * receiver ready interrupt.
                   1215:  * called at splzs
                   1216:  */
                   1217: void
                   1218: zstty_rxint(struct zs_chanstate *cs)
                   1219: {
                   1220:        struct zstty_softc *zst = cs->cs_private;
                   1221:        u_char *put, *end;
                   1222:        u_int cc;
                   1223:        u_char rr0, rr1, c;
                   1224:
                   1225:        end = zst->zst_ebuf;
                   1226:        put = zst->zst_rbput;
                   1227:        cc = zst->zst_rbavail;
                   1228:
                   1229:        while (cc > 0) {
                   1230:                /*
                   1231:                 * First read the status, because reading the received char
                   1232:                 * destroys the status of this char.
                   1233:                 */
                   1234:                rr1 = zs_read_reg(cs, 1);
                   1235:                c = zs_read_data(cs);
                   1236:
                   1237:                if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
                   1238:                        /* Clear the receive error. */
                   1239:                        zs_write_csr(cs, ZSWR0_RESET_ERRORS);
                   1240:                }
                   1241:
                   1242:                put[0] = c;
                   1243:                put[1] = rr1;
                   1244:                put += 2;
                   1245:                if (put >= end)
                   1246:                        put = zst->zst_rbuf;
                   1247:                cc--;
                   1248:
                   1249:                rr0 = zs_read_csr(cs);
                   1250:                if (!ISSET(rr0, ZSRR0_RX_READY))
                   1251:                        break;
                   1252:        }
                   1253:
                   1254:        /*
                   1255:         * Current string of incoming characters ended because
                   1256:         * no more data was available or we ran out of space.
                   1257:         * Schedule a receive event if any data was received.
                   1258:         * If we're out of space, turn off receive interrupts.
                   1259:         */
                   1260:        zst->zst_rbput = put;
                   1261:        zst->zst_rbavail = cc;
                   1262:        if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
                   1263:                zst->zst_rx_ready = 1;
                   1264:                cs->cs_softreq = 1;
                   1265:        }
                   1266:
                   1267:        /*
                   1268:         * See if we are in danger of overflowing a buffer. If
                   1269:         * so, use hardware flow control to ease the pressure.
                   1270:         */
                   1271:        if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
                   1272:            cc < zst->zst_r_hiwat) {
                   1273:                SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
                   1274:                zs_hwiflow(zst);
                   1275:        }
                   1276:
                   1277:        /*
                   1278:         * If we're out of space, disable receive interrupts
                   1279:         * until the queue has drained a bit.
                   1280:         */
                   1281:        if (!cc) {
                   1282:                SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
                   1283:                CLR(cs->cs_preg[1], ZSWR1_RIE);
                   1284:                cs->cs_creg[1] = cs->cs_preg[1];
                   1285:                zs_write_reg(cs, 1, cs->cs_creg[1]);
                   1286:        }
                   1287: }
                   1288:
                   1289: /*
                   1290:  * transmitter ready interrupt.  (splzs)
                   1291:  */
                   1292: void
                   1293: zstty_txint(struct zs_chanstate *cs)
                   1294: {
                   1295:        struct zstty_softc *zst = cs->cs_private;
                   1296:
                   1297:        /*
                   1298:         * If we've delayed a parameter change, do it now, and restart
                   1299:         * output.
                   1300:         */
                   1301:        if (cs->cs_heldchange) {
                   1302:                zs_loadchannelregs(cs);
                   1303:                cs->cs_heldchange = 0;
                   1304:                zst->zst_tbc = zst->zst_heldtbc;
                   1305:                zst->zst_heldtbc = 0;
                   1306:        }
                   1307:
                   1308:        /* Output the next character in the buffer, if any. */
                   1309:        if (zst->zst_tbc > 0) {
                   1310:                zs_write_data(cs, *zst->zst_tba);
                   1311:                zst->zst_tbc--;
                   1312:                zst->zst_tba++;
                   1313:        } else {
                   1314:                /* Disable transmit completion interrupts if necessary. */
                   1315:                if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
                   1316:                        CLR(cs->cs_preg[1], ZSWR1_TIE);
                   1317:                        cs->cs_creg[1] = cs->cs_preg[1];
                   1318:                        zs_write_reg(cs, 1, cs->cs_creg[1]);
                   1319:                }
                   1320:                if (zst->zst_tx_busy) {
                   1321:                        zst->zst_tx_busy = 0;
                   1322:                        zst->zst_tx_done = 1;
                   1323:                        cs->cs_softreq = 1;
                   1324:                }
                   1325:        }
                   1326: }
                   1327:
                   1328: #ifdef DDB
                   1329: #include <ddb/db_var.h>
                   1330: #define        DB_CONSOLE      db_console
                   1331: #else
                   1332: #define        DB_CONSOLE      1
                   1333: #endif
                   1334:
                   1335: /*
                   1336:  * status change interrupt.  (splzs)
                   1337:  */
                   1338: void
                   1339: zstty_stint(struct zs_chanstate *cs, int force)
                   1340: {
                   1341:        struct zstty_softc *zst = cs->cs_private;
                   1342:        u_char rr0, delta;
                   1343:
                   1344:        rr0 = zs_read_csr(cs);
                   1345:        zs_write_csr(cs, ZSWR0_RESET_STATUS);
                   1346:
                   1347:        /*
                   1348:         * Check here for console break, so that we can abort
                   1349:         * even when interrupts are locking up the machine.
                   1350:         */
                   1351:        if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) &&
                   1352:            ISSET(rr0, ZSRR0_BREAK) && DB_CONSOLE)
                   1353:                zs_abort(cs);
                   1354:
                   1355:        if (!force)
                   1356:                delta = rr0 ^ cs->cs_rr0;
                   1357:        else
                   1358:                delta = cs->cs_rr0_mask;
                   1359:        cs->cs_rr0 = rr0;
                   1360:
                   1361:        if (ISSET(delta, cs->cs_rr0_mask)) {
                   1362:                SET(cs->cs_rr0_delta, delta);
                   1363:
                   1364:                /*
                   1365:                 * Stop output immediately if we lose the output
                   1366:                 * flow control signal or carrier detect.
                   1367:                 */
                   1368:                if (ISSET(~rr0, cs->cs_rr0_mask)) {
                   1369:                        zst->zst_tbc = 0;
                   1370:                        zst->zst_heldtbc = 0;
                   1371:                }
                   1372:
                   1373:                zst->zst_st_check = 1;
                   1374:                cs->cs_softreq = 1;
                   1375:        }
                   1376: }
                   1377:
                   1378: void
                   1379: zstty_diag(void *arg)
                   1380: {
                   1381:        struct zstty_softc *zst = arg;
                   1382:        int overflows, floods;
                   1383:        int s;
                   1384:
                   1385:        s = splzs();
                   1386:        overflows = zst->zst_overflows;
                   1387:        zst->zst_overflows = 0;
                   1388:        floods = zst->zst_floods;
                   1389:        zst->zst_floods = 0;
                   1390:        zst->zst_errors = 0;
                   1391:        splx(s);
                   1392:
                   1393:        log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
                   1394:            zst->zst_dev.dv_xname,
                   1395:            overflows, overflows == 1 ? "" : "s",
                   1396:            floods, floods == 1 ? "" : "s");
                   1397: }
                   1398:
                   1399: void
                   1400: zstty_rxsoft(struct zstty_softc *zst, struct tty *tp)
                   1401: {
                   1402:        struct zs_chanstate *cs = zst->zst_cs;
                   1403:        int (*rint)(int c, struct tty *tp) = linesw[tp->t_line].l_rint;
                   1404:        u_char *get, *end;
                   1405:        u_int cc, scc;
                   1406:        u_char rr1;
                   1407:        int code;
                   1408:        int s;
                   1409:
                   1410:        end = zst->zst_ebuf;
                   1411:        get = zst->zst_rbget;
                   1412:        scc = cc = zstty_rbuf_size - zst->zst_rbavail;
                   1413:
                   1414:        if (cc == zstty_rbuf_size) {
                   1415:                zst->zst_floods++;
                   1416:                if (zst->zst_errors++ == 0)
                   1417:                        timeout_add(&zst->zst_diag_ch, 60 * hz);
                   1418:        }
                   1419:
                   1420:        /* If not yet open, drop the entire buffer content here */
                   1421:        if (!ISSET(tp->t_state, TS_ISOPEN)) {
                   1422:                get += cc << 1;
                   1423:                if (get >= end)
                   1424:                        get -= zstty_rbuf_size << 1;
                   1425:                cc = 0;
                   1426:        }
                   1427:        while (cc) {
                   1428:                code = get[0];
                   1429:                rr1 = get[1];
                   1430:                if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
                   1431:                        if (ISSET(rr1, ZSRR1_DO)) {
                   1432:                                zst->zst_overflows++;
                   1433:                                if (zst->zst_errors++ == 0)
                   1434:                                        timeout_add(&zst->zst_diag_ch, 60 * hz);
                   1435:                        }
                   1436:                        if (ISSET(rr1, ZSRR1_FE))
                   1437:                                SET(code, TTY_FE);
                   1438:                        if (ISSET(rr1, ZSRR1_PE))
                   1439:                                SET(code, TTY_PE);
                   1440:                }
                   1441:                if ((*rint)(code, tp) == -1) {
                   1442:                        /*
                   1443:                         * The line discipline's buffer is out of space.
                   1444:                         */
                   1445:                        if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
                   1446:                                /*
                   1447:                                 * We're either not using flow control, or the
                   1448:                                 * line discipline didn't tell us to block for
                   1449:                                 * some reason.  Either way, we have no way to
                   1450:                                 * know when there's more space available, so
                   1451:                                 * just drop the rest of the data.
                   1452:                                 */
                   1453:                                get += cc << 1;
                   1454:                                if (get >= end)
                   1455:                                        get -= zstty_rbuf_size << 1;
                   1456:                                cc = 0;
                   1457:                        } else {
                   1458:                                /*
                   1459:                                 * Don't schedule any more receive processing
                   1460:                                 * until the line discipline tells us there's
                   1461:                                 * space available (through comhwiflow()).
                   1462:                                 * Leave the rest of the data in the input
                   1463:                                 * buffer.
                   1464:                                 */
                   1465:                                SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
                   1466:                        }
                   1467:                        break;
                   1468:                }
                   1469:                get += 2;
                   1470:                if (get >= end)
                   1471:                        get = zst->zst_rbuf;
                   1472:                cc--;
                   1473:        }
                   1474:
                   1475:        if (cc != scc) {
                   1476:                zst->zst_rbget = get;
                   1477:                s = splzs();
                   1478:                cc = zst->zst_rbavail += scc - cc;
                   1479:                /* Buffers should be ok again, release possible block. */
                   1480:                if (cc >= zst->zst_r_lowat) {
                   1481:                        if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
                   1482:                                CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
                   1483:                                SET(cs->cs_preg[1], ZSWR1_RIE);
                   1484:                                cs->cs_creg[1] = cs->cs_preg[1];
                   1485:                                zs_write_reg(cs, 1, cs->cs_creg[1]);
                   1486:                        }
                   1487:                        if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
                   1488:                                CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
                   1489:                                zs_hwiflow(zst);
                   1490:                        }
                   1491:                }
                   1492:                splx(s);
                   1493:        }
                   1494: }
                   1495:
                   1496: void
                   1497: zstty_txsoft(struct zstty_softc *zst, struct tty *tp)
                   1498: {
                   1499:        CLR(tp->t_state, TS_BUSY);
                   1500:        if (ISSET(tp->t_state, TS_FLUSH))
                   1501:                CLR(tp->t_state, TS_FLUSH);
                   1502:        else
                   1503:                ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
                   1504:        (*linesw[tp->t_line].l_start)(tp);
                   1505: }
                   1506:
                   1507: void
                   1508: zstty_stsoft(struct zstty_softc *zst, struct tty *tp)
                   1509: {
                   1510:        struct zs_chanstate *cs = zst->zst_cs;
                   1511:        u_char rr0, delta;
                   1512:        int s;
                   1513:
                   1514:        s = splzs();
                   1515:        rr0 = cs->cs_rr0;
                   1516:        delta = cs->cs_rr0_delta;
                   1517:        cs->cs_rr0_delta = 0;
                   1518:        splx(s);
                   1519:
                   1520:        if (ISSET(delta, cs->cs_rr0_dcd)) {
                   1521:                /*
                   1522:                 * Inform the tty layer that carrier detect changed.
                   1523:                 */
                   1524:                (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
                   1525:        }
                   1526:
                   1527:        if (ISSET(delta, cs->cs_rr0_cts)) {
                   1528:                /* Block or unblock output according to flow control. */
                   1529:                if (ISSET(rr0, cs->cs_rr0_cts)) {
                   1530:                        zst->zst_tx_stopped = 0;
                   1531:                        (*linesw[tp->t_line].l_start)(tp);
                   1532:                } else {
                   1533:                        zst->zst_tx_stopped = 1;
                   1534:                }
                   1535:        }
                   1536: }
                   1537:
                   1538: /*
                   1539:  * Software interrupt.  Called at zssoft
                   1540:  *
                   1541:  * The main job to be done here is to empty the input ring
                   1542:  * by passing its contents up to the tty layer.  The ring is
                   1543:  * always emptied during this operation, therefore the ring
                   1544:  * must not be larger than the space after "high water" in
                   1545:  * the tty layer, or the tty layer might drop our input.
                   1546:  *
                   1547:  * Note: an "input blockage" condition is assumed to exist if
                   1548:  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
                   1549:  */
                   1550: void
                   1551: zstty_softint(struct zs_chanstate *cs)
                   1552: {
                   1553:        struct zstty_softc *zst = cs->cs_private;
                   1554:        struct tty *tp = zst->zst_tty;
                   1555:        int s;
                   1556:
                   1557:        s = spltty();
                   1558:
                   1559:        if (zst->zst_rx_ready) {
                   1560:                zst->zst_rx_ready = 0;
                   1561:                zstty_rxsoft(zst, tp);
                   1562:        }
                   1563:
                   1564:        if (zst->zst_st_check) {
                   1565:                zst->zst_st_check = 0;
                   1566:                zstty_stsoft(zst, tp);
                   1567:        }
                   1568:
                   1569:        if (zst->zst_tx_done) {
                   1570:                zst->zst_tx_done = 0;
                   1571:                zstty_txsoft(zst, tp);
                   1572:        }
                   1573:
                   1574:        splx(s);
                   1575: }
                   1576:
                   1577: struct zsops zsops_tty = {
                   1578:        zstty_rxint,    /* receive char available */
                   1579:        zstty_stint,    /* external/status */
                   1580:        zstty_txint,    /* xmit buffer empty */
                   1581:        zstty_softint,  /* process software interrupt */
                   1582: };

CVSweb