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