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