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