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