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