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