z8530tty.c revision 1.100
1/* $NetBSD: z8530tty.c,v 1.100 2005/09/06 21:40:39 kleink 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.100 2005/09/06 21:40:39 kleink 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 175static 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 */ 180u_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. */ 183u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4; 184u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4; 185 186static int zsppscap = 187 PPS_TSFMT_TSPEC | 188 PPS_CAPTUREASSERT | 189 PPS_CAPTURECLEAR | 190 PPS_OFFSETASSERT | PPS_OFFSETCLEAR; 191 192struct 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. */ 251static int zstty_match(struct device *, struct cfdata *, void *); 252static void zstty_attach(struct device *, struct device *, void *); 253 254CFATTACH_DECL(zstty, sizeof(struct zstty_softc), 255 zstty_match, zstty_attach, NULL, NULL); 256 257extern struct cfdriver zstty_cd; 258 259dev_type_open(zsopen); 260dev_type_close(zsclose); 261dev_type_read(zsread); 262dev_type_write(zswrite); 263dev_type_ioctl(zsioctl); 264dev_type_stop(zsstop); 265dev_type_tty(zstty); 266dev_type_poll(zspoll); 267 268const struct cdevsw zstty_cdevsw = { 269 zsopen, zsclose, zsread, zswrite, zsioctl, 270 zsstop, zstty, zspoll, nommap, ttykqfilter, D_TTY 271}; 272 273struct zsops zsops_tty; 274 275static void zs_shutdown(struct zstty_softc *); 276static void zsstart(struct tty *); 277static int zsparam(struct tty *, struct termios *); 278static void zs_modem(struct zstty_softc *, int); 279static void tiocm_to_zs(struct zstty_softc *, u_long, int); 280static int zs_to_tiocm(struct zstty_softc *); 281static int zshwiflow(struct tty *, int); 282static void zs_hwiflow(struct zstty_softc *); 283static void zs_maskintr(struct zstty_softc *); 284 285/* Low-level routines. */ 286static void zstty_rxint (struct zs_chanstate *); 287static void zstty_stint (struct zs_chanstate *, int); 288static void zstty_txint (struct zs_chanstate *); 289static void zstty_softint(struct zs_chanstate *); 290 291#define ZSUNIT(x) (minor(x) & 0x7ffff) 292#define ZSDIALOUT(x) (minor(x) & 0x80000) 293 294struct 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 302struct tty * 303zstty_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 */ 313int 314zstty_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 332void 333zstty_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 */ 481struct tty * 482zstty(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 491void 492zs_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 */ 557int 558zsopen(dev, flags, mode, p) 559 dev_t dev; 560 int flags; 561 int mode; 562 struct proc *p; 563{ 564 struct zstty_softc *zst; 565 struct zs_chanstate *cs; 566 struct tty *tp; 567 int s, s2; 568 int error; 569 570 zst = device_lookup(&zstty_cd, ZSUNIT(dev)); 571 if (zst == NULL) 572 return (ENXIO); 573 574 tp = zst->zst_tty; 575 cs = zst->zst_cs; 576 577 /* If KGDB took the line, then tp==NULL */ 578 if (tp == NULL) 579 return (EBUSY); 580 581 if (ISSET(tp->t_state, TS_ISOPEN) && 582 ISSET(tp->t_state, TS_XCLUDE) && 583 suser(p->p_ucred, &p->p_acflag) != 0) 584 return (EBUSY); 585 586 s = spltty(); 587 588 /* 589 * Do the following iff this is a first open. 590 */ 591 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 592 struct termios t; 593 594 tp->t_dev = dev; 595 596 /* Call the power management hook. */ 597 if (cs->enable) { 598 if ((*cs->enable)(cs)) { 599 splx(s); 600 printf("%s: device enable failed\n", 601 zst->zst_dev.dv_xname); 602 return (EIO); 603 } 604 } 605 606 /* 607 * Initialize the termios status to the defaults. Add in the 608 * sticky bits from TIOCSFLAGS. 609 */ 610 t.c_ispeed = 0; 611 t.c_ospeed = cs->cs_defspeed; 612 t.c_cflag = cs->cs_defcflag; 613 if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL)) 614 SET(t.c_cflag, CLOCAL); 615 if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS)) 616 SET(t.c_cflag, CRTSCTS); 617 if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS)) 618 SET(t.c_cflag, CDTRCTS); 619 if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF)) 620 SET(t.c_cflag, MDMBUF); 621 622 s2 = splzs(); 623 simple_lock(&cs->cs_lock); 624 625 /* 626 * Turn on receiver and status interrupts. 627 * We defer the actual write of the register to zsparam(), 628 * but we must make sure status interrupts are turned on by 629 * the time zsparam() reads the initial rr0 state. 630 */ 631 SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE); 632 633 /* Clear PPS capture state on first open. */ 634 zst->zst_ppsmask = 0; 635 zst->ppsparam.mode = 0; 636 637 simple_unlock(&cs->cs_lock); 638 splx(s2); 639 640 /* Make sure zsparam will see changes. */ 641 tp->t_ospeed = 0; 642 (void) zsparam(tp, &t); 643 644 /* 645 * Note: zsparam has done: cflag, ispeed, ospeed 646 * so we just need to do: iflag, oflag, lflag, cc 647 * For "raw" mode, just leave all zeros. 648 */ 649 if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) { 650 tp->t_iflag = TTYDEF_IFLAG; 651 tp->t_oflag = TTYDEF_OFLAG; 652 tp->t_lflag = TTYDEF_LFLAG; 653 } else { 654 tp->t_iflag = 0; 655 tp->t_oflag = 0; 656 tp->t_lflag = 0; 657 } 658 ttychars(tp); 659 ttsetwater(tp); 660 661 s2 = splzs(); 662 simple_lock(&cs->cs_lock); 663 664 /* 665 * Turn on DTR. We must always do this, even if carrier is not 666 * present, because otherwise we'd have to use TIOCSDTR 667 * immediately after setting CLOCAL, which applications do not 668 * expect. We always assert DTR while the device is open 669 * unless explicitly requested to deassert it. 670 */ 671 zs_modem(zst, 1); 672 673 /* Clear the input ring, and unblock. */ 674 zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf; 675 zst->zst_rbavail = zstty_rbuf_size; 676 zs_iflush(cs); 677 CLR(zst->zst_rx_flags, RX_ANY_BLOCK); 678 zs_hwiflow(zst); 679 680 simple_unlock(&cs->cs_lock); 681 splx(s2); 682 } 683 684 splx(s); 685 686 error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK)); 687 if (error) 688 goto bad; 689 690 error = (*tp->t_linesw->l_open)(dev, tp); 691 if (error) 692 goto bad; 693 694 return (0); 695 696bad: 697 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 698 /* 699 * We failed to open the device, and nobody else had it opened. 700 * Clean up the state as appropriate. 701 */ 702 zs_shutdown(zst); 703 } 704 705 return (error); 706} 707 708/* 709 * Close a zs serial port. 710 */ 711int 712zsclose(dev, flags, mode, p) 713 dev_t dev; 714 int flags; 715 int mode; 716 struct proc *p; 717{ 718 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev)); 719 struct tty *tp = zst->zst_tty; 720 721 /* XXX This is for cons.c. */ 722 if (!ISSET(tp->t_state, TS_ISOPEN)) 723 return 0; 724 725 (*tp->t_linesw->l_close)(tp, flags); 726 ttyclose(tp); 727 728 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 729 /* 730 * Although we got a last close, the device may still be in 731 * use; e.g. if this was the dialout node, and there are still 732 * processes waiting for carrier on the non-dialout node. 733 */ 734 zs_shutdown(zst); 735 } 736 737 return (0); 738} 739 740/* 741 * Read/write zs serial port. 742 */ 743int 744zsread(dev, uio, flags) 745 dev_t dev; 746 struct uio *uio; 747 int flags; 748{ 749 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev)); 750 struct tty *tp = zst->zst_tty; 751 752 return ((*tp->t_linesw->l_read)(tp, uio, flags)); 753} 754 755int 756zswrite(dev, uio, flags) 757 dev_t dev; 758 struct uio *uio; 759 int flags; 760{ 761 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev)); 762 struct tty *tp = zst->zst_tty; 763 764 return ((*tp->t_linesw->l_write)(tp, uio, flags)); 765} 766 767int 768zspoll(dev, events, p) 769 dev_t dev; 770 int events; 771 struct proc *p; 772{ 773 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev)); 774 struct tty *tp = zst->zst_tty; 775 776 return ((*tp->t_linesw->l_poll)(tp, events, p)); 777} 778 779int 780zsioctl(dev, cmd, data, flag, p) 781 dev_t dev; 782 u_long cmd; 783 caddr_t data; 784 int flag; 785 struct proc *p; 786{ 787 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev)); 788 struct zs_chanstate *cs = zst->zst_cs; 789 struct tty *tp = zst->zst_tty; 790 int error; 791 int s; 792 793 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 794 if (error != EPASSTHROUGH) 795 return (error); 796 797 error = ttioctl(tp, cmd, data, flag, p); 798 if (error != EPASSTHROUGH) 799 return (error); 800 801#ifdef ZS_MD_IOCTL 802 error = ZS_MD_IOCTL(cs, cmd, data); 803 if (error != EPASSTHROUGH) 804 return (error); 805#endif /* ZS_MD_IOCTL */ 806 807 error = 0; 808 809 s = splzs(); 810 simple_lock(&cs->cs_lock); 811 812 switch (cmd) { 813 case TIOCSBRK: 814 zs_break(cs, 1); 815 break; 816 817 case TIOCCBRK: 818 zs_break(cs, 0); 819 break; 820 821 case TIOCGFLAGS: 822 *(int *)data = zst->zst_swflags; 823 break; 824 825 case TIOCSFLAGS: 826 error = suser(p->p_ucred, &p->p_acflag); 827 if (error) 828 break; 829 zst->zst_swflags = *(int *)data; 830 break; 831 832 case TIOCSDTR: 833 zs_modem(zst, 1); 834 break; 835 836 case TIOCCDTR: 837 zs_modem(zst, 0); 838 break; 839 840 case TIOCMSET: 841 case TIOCMBIS: 842 case TIOCMBIC: 843 tiocm_to_zs(zst, cmd, *(int *)data); 844 break; 845 846 case TIOCMGET: 847 *(int *)data = zs_to_tiocm(zst); 848 break; 849 850 case PPS_IOC_CREATE: 851 break; 852 853 case PPS_IOC_DESTROY: 854 break; 855 856 case PPS_IOC_GETPARAMS: { 857 pps_params_t *pp; 858 pp = (pps_params_t *)data; 859 *pp = zst->ppsparam; 860 break; 861 } 862 863 case PPS_IOC_SETPARAMS: { 864 pps_params_t *pp; 865 int mode; 866 if (cs->cs_rr0_pps == 0) { 867 error = EINVAL; 868 break; 869 } 870 pp = (pps_params_t *)data; 871 if (pp->mode & ~zsppscap) { 872 error = EINVAL; 873 break; 874 } 875 zst->ppsparam = *pp; 876 /* 877 * compute masks from user-specified timestamp state. 878 */ 879 mode = zst->ppsparam.mode; 880 switch (mode & PPS_CAPTUREBOTH) { 881 case 0: 882 zst->zst_ppsmask = 0; 883 break; 884 885 case PPS_CAPTUREASSERT: 886 zst->zst_ppsmask = ZSRR0_DCD; 887 zst->zst_ppsassert = ZSRR0_DCD; 888 zst->zst_ppsclear = -1; 889 break; 890 891 case PPS_CAPTURECLEAR: 892 zst->zst_ppsmask = ZSRR0_DCD; 893 zst->zst_ppsassert = -1; 894 zst->zst_ppsclear = 0; 895 break; 896 897 case PPS_CAPTUREBOTH: 898 zst->zst_ppsmask = ZSRR0_DCD; 899 zst->zst_ppsassert = ZSRR0_DCD; 900 zst->zst_ppsclear = 0; 901 break; 902 903 default: 904 error = EINVAL; 905 break; 906 } 907 908 /* 909 * Now update interrupts. 910 */ 911 zs_maskintr(zst); 912 /* 913 * If nothing is being transmitted, set up new current values, 914 * else mark them as pending. 915 */ 916 if (!cs->cs_heldchange) { 917 if (zst->zst_tx_busy) { 918 zst->zst_heldtbc = zst->zst_tbc; 919 zst->zst_tbc = 0; 920 cs->cs_heldchange = 1; 921 } else 922 zs_loadchannelregs(cs); 923 } 924 925 break; 926 } 927 928 case PPS_IOC_GETCAP: 929 *(int *)data = zsppscap; 930 break; 931 932 case PPS_IOC_FETCH: { 933 pps_info_t *pi; 934 pi = (pps_info_t *)data; 935 *pi = zst->ppsinfo; 936 break; 937 } 938 939#ifdef PPS_SYNC 940 case PPS_IOC_KCBIND: { 941 int edge = (*(int *)data) & PPS_CAPTUREBOTH; 942 943 if (edge == 0) { 944 /* 945 * remove binding for this source; ignore 946 * the request if this is not the current 947 * hardpps source 948 */ 949 if (pps_kc_hardpps_source == zst) { 950 pps_kc_hardpps_source = NULL; 951 pps_kc_hardpps_mode = 0; 952 } 953 } else { 954 /* 955 * bind hardpps to this source, replacing any 956 * previously specified source or edges 957 */ 958 pps_kc_hardpps_source = zst; 959 pps_kc_hardpps_mode = edge; 960 } 961 break; 962 } 963#endif /* PPS_SYNC */ 964 965 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */ 966 if (cs->cs_rr0_pps == 0) { 967 error = EINVAL; 968 break; 969 } 970 /* 971 * Some GPS clocks models use the falling rather than 972 * rising edge as the on-the-second signal. 973 * The old API has no way to specify PPS polarity. 974 */ 975 zst->zst_ppsmask = ZSRR0_DCD; 976#ifndef PPS_TRAILING_EDGE 977 zst->zst_ppsassert = ZSRR0_DCD; 978 zst->zst_ppsclear = -1; 979 TIMESPEC_TO_TIMEVAL((struct timeval *)data, 980 &zst->ppsinfo.assert_timestamp); 981#else 982 zst->zst_ppsassert = -1; 983 zst->zst_ppsclear = 01; 984 TIMESPEC_TO_TIMEVAL((struct timeval *)data, 985 &zst->ppsinfo.clear_timestamp); 986#endif 987 /* 988 * Now update interrupts. 989 */ 990 zs_maskintr(zst); 991 /* 992 * If nothing is being transmitted, set up new current values, 993 * else mark them as pending. 994 */ 995 if (!cs->cs_heldchange) { 996 if (zst->zst_tx_busy) { 997 zst->zst_heldtbc = zst->zst_tbc; 998 zst->zst_tbc = 0; 999 cs->cs_heldchange = 1; 1000 } else 1001 zs_loadchannelregs(cs); 1002 } 1003 1004 break; 1005 1006 default: 1007 error = EPASSTHROUGH; 1008 break; 1009 } 1010 1011 simple_unlock(&cs->cs_lock); 1012 splx(s); 1013 1014 return (error); 1015} 1016 1017/* 1018 * Start or restart transmission. 1019 */ 1020static void 1021zsstart(tp) 1022 struct tty *tp; 1023{ 1024 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev)); 1025 struct zs_chanstate *cs = zst->zst_cs; 1026 int s; 1027 1028 s = spltty(); 1029 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 1030 goto out; 1031 if (zst->zst_tx_stopped) 1032 goto out; 1033 1034 if (tp->t_outq.c_cc <= tp->t_lowat) { 1035 if (ISSET(tp->t_state, TS_ASLEEP)) { 1036 CLR(tp->t_state, TS_ASLEEP); 1037 wakeup((caddr_t)&tp->t_outq); 1038 } 1039 selwakeup(&tp->t_wsel); 1040 if (tp->t_outq.c_cc == 0) 1041 goto out; 1042 } 1043 1044 /* Grab the first contiguous region of buffer space. */ 1045 { 1046 u_char *tba; 1047 int tbc; 1048 1049 tba = tp->t_outq.c_cf; 1050 tbc = ndqb(&tp->t_outq, 0); 1051 1052 (void) splzs(); 1053 simple_lock(&cs->cs_lock); 1054 1055 zst->zst_tba = tba; 1056 zst->zst_tbc = tbc; 1057 } 1058 1059 SET(tp->t_state, TS_BUSY); 1060 zst->zst_tx_busy = 1; 1061 1062 /* Enable transmit completion interrupts if necessary. */ 1063 if (!ISSET(cs->cs_preg[1], ZSWR1_TIE)) { 1064 SET(cs->cs_preg[1], ZSWR1_TIE); 1065 cs->cs_creg[1] = cs->cs_preg[1]; 1066 zs_write_reg(cs, 1, cs->cs_creg[1]); 1067 } 1068 1069 /* Output the first character of the contiguous buffer. */ 1070 { 1071 zs_write_data(cs, *zst->zst_tba); 1072 zst->zst_tbc--; 1073 zst->zst_tba++; 1074 } 1075 simple_unlock(&cs->cs_lock); 1076out: 1077 splx(s); 1078 return; 1079} 1080 1081/* 1082 * Stop output, e.g., for ^S or output flush. 1083 */ 1084void 1085zsstop(tp, flag) 1086 struct tty *tp; 1087 int flag; 1088{ 1089 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev)); 1090 int s; 1091 1092 s = splzs(); 1093 if (ISSET(tp->t_state, TS_BUSY)) { 1094 /* Stop transmitting at the next chunk. */ 1095 zst->zst_tbc = 0; 1096 zst->zst_heldtbc = 0; 1097 if (!ISSET(tp->t_state, TS_TTSTOP)) 1098 SET(tp->t_state, TS_FLUSH); 1099 } 1100 splx(s); 1101} 1102 1103/* 1104 * Set ZS tty parameters from termios. 1105 * XXX - Should just copy the whole termios after 1106 * making sure all the changes could be done. 1107 */ 1108static int 1109zsparam(tp, t) 1110 struct tty *tp; 1111 struct termios *t; 1112{ 1113 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev)); 1114 struct zs_chanstate *cs = zst->zst_cs; 1115 int ospeed; 1116 tcflag_t cflag; 1117 u_char tmp3, tmp4, tmp5; 1118 int s, error; 1119 1120 ospeed = t->c_ospeed; 1121 cflag = t->c_cflag; 1122 1123 /* Check requested parameters. */ 1124 if (ospeed < 0) 1125 return (EINVAL); 1126 if (t->c_ispeed && t->c_ispeed != ospeed) 1127 return (EINVAL); 1128 1129 /* 1130 * For the console, always force CLOCAL and !HUPCL, so that the port 1131 * is always active. 1132 */ 1133 if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) || 1134 ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) { 1135 SET(cflag, CLOCAL); 1136 CLR(cflag, HUPCL); 1137 } 1138 1139 /* 1140 * Only whack the UART when params change. 1141 * Some callers need to clear tp->t_ospeed 1142 * to make sure initialization gets done. 1143 */ 1144 if (tp->t_ospeed == ospeed && 1145 tp->t_cflag == cflag) 1146 return (0); 1147 1148 /* 1149 * Call MD functions to deal with changed 1150 * clock modes or H/W flow control modes. 1151 * The BRG divisor is set now. (reg 12,13) 1152 */ 1153 error = zs_set_speed(cs, ospeed); 1154 if (error) 1155 return (error); 1156 error = zs_set_modes(cs, cflag); 1157 if (error) 1158 return (error); 1159 1160 /* 1161 * Block interrupts so that state will not 1162 * be altered until we are done setting it up. 1163 * 1164 * Initial values in cs_preg are set before 1165 * our attach routine is called. The master 1166 * interrupt enable is handled by zsc.c 1167 * 1168 */ 1169 s = splzs(); 1170 simple_lock(&cs->cs_lock); 1171 1172 /* 1173 * Recalculate which status ints to enable. 1174 */ 1175 zs_maskintr(zst); 1176 1177 /* Recompute character size bits. */ 1178 tmp3 = cs->cs_preg[3]; 1179 tmp5 = cs->cs_preg[5]; 1180 CLR(tmp3, ZSWR3_RXSIZE); 1181 CLR(tmp5, ZSWR5_TXSIZE); 1182 switch (ISSET(cflag, CSIZE)) { 1183 case CS5: 1184 SET(tmp3, ZSWR3_RX_5); 1185 SET(tmp5, ZSWR5_TX_5); 1186 break; 1187 case CS6: 1188 SET(tmp3, ZSWR3_RX_6); 1189 SET(tmp5, ZSWR5_TX_6); 1190 break; 1191 case CS7: 1192 SET(tmp3, ZSWR3_RX_7); 1193 SET(tmp5, ZSWR5_TX_7); 1194 break; 1195 case CS8: 1196 SET(tmp3, ZSWR3_RX_8); 1197 SET(tmp5, ZSWR5_TX_8); 1198 break; 1199 } 1200 cs->cs_preg[3] = tmp3; 1201 cs->cs_preg[5] = tmp5; 1202 1203 /* 1204 * Recompute the stop bits and parity bits. Note that 1205 * zs_set_speed() may have set clock selection bits etc. 1206 * in wr4, so those must preserved. 1207 */ 1208 tmp4 = cs->cs_preg[4]; 1209 CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK); 1210 if (ISSET(cflag, CSTOPB)) 1211 SET(tmp4, ZSWR4_TWOSB); 1212 else 1213 SET(tmp4, ZSWR4_ONESB); 1214 if (!ISSET(cflag, PARODD)) 1215 SET(tmp4, ZSWR4_EVENP); 1216 if (ISSET(cflag, PARENB)) 1217 SET(tmp4, ZSWR4_PARENB); 1218 cs->cs_preg[4] = tmp4; 1219 1220 /* And copy to tty. */ 1221 tp->t_ispeed = 0; 1222 tp->t_ospeed = ospeed; 1223 tp->t_cflag = cflag; 1224 1225 /* 1226 * If nothing is being transmitted, set up new current values, 1227 * else mark them as pending. 1228 */ 1229 if (!cs->cs_heldchange) { 1230 if (zst->zst_tx_busy) { 1231 zst->zst_heldtbc = zst->zst_tbc; 1232 zst->zst_tbc = 0; 1233 cs->cs_heldchange = 1; 1234 } else 1235 zs_loadchannelregs(cs); 1236 } 1237 1238 /* 1239 * If hardware flow control is disabled, turn off the buffer water 1240 * marks and unblock any soft flow control state. Otherwise, enable 1241 * the water marks. 1242 */ 1243 if (!ISSET(cflag, CHWFLOW)) { 1244 zst->zst_r_hiwat = 0; 1245 zst->zst_r_lowat = 0; 1246 if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) { 1247 CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED); 1248 zst->zst_rx_ready = 1; 1249 cs->cs_softreq = 1; 1250 } 1251 if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) { 1252 CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED); 1253 zs_hwiflow(zst); 1254 } 1255 } else { 1256 zst->zst_r_hiwat = zstty_rbuf_hiwat; 1257 zst->zst_r_lowat = zstty_rbuf_lowat; 1258 } 1259 1260 /* 1261 * Force a recheck of the hardware carrier and flow control status, 1262 * since we may have changed which bits we're looking at. 1263 */ 1264 zstty_stint(cs, 1); 1265 1266 simple_unlock(&cs->cs_lock); 1267 splx(s); 1268 1269 /* 1270 * If hardware flow control is disabled, unblock any hard flow control 1271 * state. 1272 */ 1273 if (!ISSET(cflag, CHWFLOW)) { 1274 if (zst->zst_tx_stopped) { 1275 zst->zst_tx_stopped = 0; 1276 zsstart(tp); 1277 } 1278 } 1279 1280 zstty_softint(cs); 1281 1282 return (0); 1283} 1284 1285/* 1286 * Compute interrupt enable bits and set in the pending bits. Called both 1287 * in zsparam() and when PPS (pulse per second timing) state changes. 1288 * Must be called at splzs(). 1289 */ 1290static void 1291zs_maskintr(zst) 1292 struct zstty_softc *zst; 1293{ 1294 struct zs_chanstate *cs = zst->zst_cs; 1295 int tmp15; 1296 1297 cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd; 1298 if (zst->zst_ppsmask != 0) 1299 cs->cs_rr0_mask |= cs->cs_rr0_pps; 1300 tmp15 = cs->cs_preg[15]; 1301 if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD)) 1302 SET(tmp15, ZSWR15_DCD_IE); 1303 else 1304 CLR(tmp15, ZSWR15_DCD_IE); 1305 if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS)) 1306 SET(tmp15, ZSWR15_CTS_IE); 1307 else 1308 CLR(tmp15, ZSWR15_CTS_IE); 1309 cs->cs_preg[15] = tmp15; 1310} 1311 1312 1313/* 1314 * Raise or lower modem control (DTR/RTS) signals. If a character is 1315 * in transmission, the change is deferred. 1316 * Called at splzs() and with the channel lock held. 1317 */ 1318static void 1319zs_modem(zst, onoff) 1320 struct zstty_softc *zst; 1321 int onoff; 1322{ 1323 struct zs_chanstate *cs = zst->zst_cs, *ccs; 1324 1325 if (cs->cs_wr5_dtr == 0) 1326 return; 1327 1328 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs); 1329 1330 if (onoff) 1331 SET(ccs->cs_preg[5], cs->cs_wr5_dtr); 1332 else 1333 CLR(ccs->cs_preg[5], cs->cs_wr5_dtr); 1334 1335 if (!cs->cs_heldchange) { 1336 if (zst->zst_tx_busy) { 1337 zst->zst_heldtbc = zst->zst_tbc; 1338 zst->zst_tbc = 0; 1339 cs->cs_heldchange = 1; 1340 } else 1341 zs_loadchannelregs(cs); 1342 } 1343} 1344 1345/* 1346 * Set modem bits. 1347 * Called at splzs() and with the channel lock held. 1348 */ 1349static void 1350tiocm_to_zs(zst, how, ttybits) 1351 struct zstty_softc *zst; 1352 u_long how; 1353 int ttybits; 1354{ 1355 struct zs_chanstate *cs = zst->zst_cs, *ccs; 1356 u_char zsbits; 1357 1358 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs); 1359 1360 zsbits = 0; 1361 if (ISSET(ttybits, TIOCM_DTR)) 1362 SET(zsbits, ZSWR5_DTR); 1363 if (ISSET(ttybits, TIOCM_RTS)) 1364 SET(zsbits, ZSWR5_RTS); 1365 1366 switch (how) { 1367 case TIOCMBIC: 1368 CLR(ccs->cs_preg[5], zsbits); 1369 break; 1370 1371 case TIOCMBIS: 1372 SET(ccs->cs_preg[5], zsbits); 1373 break; 1374 1375 case TIOCMSET: 1376 CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR); 1377 SET(ccs->cs_preg[5], zsbits); 1378 break; 1379 } 1380 1381 if (!cs->cs_heldchange) { 1382 if (zst->zst_tx_busy) { 1383 zst->zst_heldtbc = zst->zst_tbc; 1384 zst->zst_tbc = 0; 1385 cs->cs_heldchange = 1; 1386 } else 1387 zs_loadchannelregs(cs); 1388 } 1389} 1390 1391/* 1392 * Get modem bits. 1393 * Called at splzs() and with the channel lock held. 1394 */ 1395static int 1396zs_to_tiocm(zst) 1397 struct zstty_softc *zst; 1398{ 1399 struct zs_chanstate *cs = zst->zst_cs, *ccs; 1400 u_char zsbits; 1401 int ttybits = 0; 1402 1403 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs); 1404 1405 zsbits = ccs->cs_preg[5]; 1406 if (ISSET(zsbits, ZSWR5_DTR)) 1407 SET(ttybits, TIOCM_DTR); 1408 if (ISSET(zsbits, ZSWR5_RTS)) 1409 SET(ttybits, TIOCM_RTS); 1410 1411 zsbits = cs->cs_rr0; 1412 if (ISSET(zsbits, ZSRR0_DCD)) 1413 SET(ttybits, TIOCM_CD); 1414 if (ISSET(zsbits, ZSRR0_CTS)) 1415 SET(ttybits, TIOCM_CTS); 1416 1417 return (ttybits); 1418} 1419 1420/* 1421 * Try to block or unblock input using hardware flow-control. 1422 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and 1423 * if this function returns non-zero, the TS_TBLOCK flag will 1424 * be set or cleared according to the "block" arg passed. 1425 */ 1426int 1427zshwiflow(tp, block) 1428 struct tty *tp; 1429 int block; 1430{ 1431 struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev)); 1432 struct zs_chanstate *cs = zst->zst_cs; 1433 int s; 1434 1435 if (cs->cs_wr5_rts == 0) 1436 return (0); 1437 1438 s = splzs(); 1439 simple_lock(&cs->cs_lock); 1440 if (block) { 1441 if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) { 1442 SET(zst->zst_rx_flags, RX_TTY_BLOCKED); 1443 zs_hwiflow(zst); 1444 } 1445 } else { 1446 if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) { 1447 CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED); 1448 zst->zst_rx_ready = 1; 1449 cs->cs_softreq = 1; 1450 } 1451 if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) { 1452 CLR(zst->zst_rx_flags, RX_TTY_BLOCKED); 1453 zs_hwiflow(zst); 1454 } 1455 } 1456 simple_unlock(&cs->cs_lock); 1457 splx(s); 1458 return (1); 1459} 1460 1461/* 1462 * Internal version of zshwiflow 1463 * Called at splzs() and with the channel lock held. 1464 */ 1465static void 1466zs_hwiflow(zst) 1467 struct zstty_softc *zst; 1468{ 1469 struct zs_chanstate *cs = zst->zst_cs, *ccs; 1470 1471 if (cs->cs_wr5_rts == 0) 1472 return; 1473 1474 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs); 1475 1476 if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) { 1477 CLR(ccs->cs_preg[5], cs->cs_wr5_rts); 1478 CLR(ccs->cs_creg[5], cs->cs_wr5_rts); 1479 } else { 1480 SET(ccs->cs_preg[5], cs->cs_wr5_rts); 1481 SET(ccs->cs_creg[5], cs->cs_wr5_rts); 1482 } 1483 zs_write_reg(ccs, 5, ccs->cs_creg[5]); 1484} 1485 1486 1487/**************************************************************** 1488 * Interface to the lower layer (zscc) 1489 ****************************************************************/ 1490 1491#define integrate static inline 1492integrate void zstty_rxsoft(struct zstty_softc *, struct tty *); 1493integrate void zstty_txsoft(struct zstty_softc *, struct tty *); 1494integrate void zstty_stsoft(struct zstty_softc *, struct tty *); 1495static void zstty_diag(void *); 1496 1497/* 1498 * Receiver Ready interrupt. 1499 * Called at splzs() and with the channel lock held. 1500 */ 1501static void 1502zstty_rxint(cs) 1503 struct zs_chanstate *cs; 1504{ 1505 struct zstty_softc *zst = cs->cs_private; 1506 u_char *put, *end; 1507 u_int cc; 1508 u_char rr0, rr1, c; 1509 1510 end = zst->zst_ebuf; 1511 put = zst->zst_rbput; 1512 cc = zst->zst_rbavail; 1513 1514 while (cc > 0) { 1515 /* 1516 * First read the status, because reading the received char 1517 * destroys the status of this char. 1518 */ 1519 rr1 = zs_read_reg(cs, 1); 1520 c = zs_read_data(cs); 1521 1522 if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) { 1523 /* Clear the receive error. */ 1524 zs_write_csr(cs, ZSWR0_RESET_ERRORS); 1525 } 1526 1527 cn_check_magic(zst->zst_tty->t_dev, c, zstty_cnm_state); 1528 put[0] = c; 1529 put[1] = rr1; 1530 put += 2; 1531 if (put >= end) 1532 put = zst->zst_rbuf; 1533 cc--; 1534 1535 rr0 = zs_read_csr(cs); 1536 if (!ISSET(rr0, ZSRR0_RX_READY)) 1537 break; 1538 } 1539 1540 /* 1541 * Current string of incoming characters ended because 1542 * no more data was available or we ran out of space. 1543 * Schedule a receive event if any data was received. 1544 * If we're out of space, turn off receive interrupts. 1545 */ 1546 zst->zst_rbput = put; 1547 zst->zst_rbavail = cc; 1548 if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) { 1549 zst->zst_rx_ready = 1; 1550 cs->cs_softreq = 1; 1551 } 1552 1553 /* 1554 * See if we are in danger of overflowing a buffer. If 1555 * so, use hardware flow control to ease the pressure. 1556 */ 1557 if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) && 1558 cc < zst->zst_r_hiwat) { 1559 SET(zst->zst_rx_flags, RX_IBUF_BLOCKED); 1560 zs_hwiflow(zst); 1561 } 1562 1563 /* 1564 * If we're out of space, disable receive interrupts 1565 * until the queue has drained a bit. 1566 */ 1567 if (!cc) { 1568 SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED); 1569 CLR(cs->cs_preg[1], ZSWR1_RIE); 1570 cs->cs_creg[1] = cs->cs_preg[1]; 1571 zs_write_reg(cs, 1, cs->cs_creg[1]); 1572 } 1573 1574#if 0 1575 printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail); 1576#endif 1577} 1578 1579/* 1580 * Transmitter Ready interrupt. 1581 * Called at splzs() and with the channel lock held. 1582 */ 1583static void 1584zstty_txint(cs) 1585 struct zs_chanstate *cs; 1586{ 1587 struct zstty_softc *zst = cs->cs_private; 1588 1589 /* 1590 * If we've delayed a parameter change, do it now, and restart 1591 * output. 1592 */ 1593 if (cs->cs_heldchange) { 1594 zs_loadchannelregs(cs); 1595 cs->cs_heldchange = 0; 1596 zst->zst_tbc = zst->zst_heldtbc; 1597 zst->zst_heldtbc = 0; 1598 } 1599 1600 /* Output the next character in the buffer, if any. */ 1601 if (zst->zst_tbc > 0) { 1602 zs_write_data(cs, *zst->zst_tba); 1603 zst->zst_tbc--; 1604 zst->zst_tba++; 1605 } else { 1606 /* Disable transmit completion interrupts if necessary. */ 1607 if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) { 1608 CLR(cs->cs_preg[1], ZSWR1_TIE); 1609 cs->cs_creg[1] = cs->cs_preg[1]; 1610 zs_write_reg(cs, 1, cs->cs_creg[1]); 1611 } 1612 if (zst->zst_tx_busy) { 1613 zst->zst_tx_busy = 0; 1614 zst->zst_tx_done = 1; 1615 cs->cs_softreq = 1; 1616 } 1617 } 1618} 1619 1620/* 1621 * Status Change interrupt. 1622 * Called at splzs() and with the channel lock held. 1623 */ 1624static void 1625zstty_stint(cs, force) 1626 struct zs_chanstate *cs; 1627 int force; 1628{ 1629 struct zstty_softc *zst = cs->cs_private; 1630 u_char rr0, delta; 1631 1632 rr0 = zs_read_csr(cs); 1633 zs_write_csr(cs, ZSWR0_RESET_STATUS); 1634 1635 /* 1636 * Check here for console break, so that we can abort 1637 * even when interrupts are locking up the machine. 1638 */ 1639 if (ISSET(rr0, ZSRR0_BREAK)) 1640 cn_check_magic(zst->zst_tty->t_dev, CNC_BREAK, zstty_cnm_state); 1641 1642 if (!force) 1643 delta = rr0 ^ cs->cs_rr0; 1644 else 1645 delta = cs->cs_rr0_mask; 1646 cs->cs_rr0 = rr0; 1647 1648 if (ISSET(delta, cs->cs_rr0_mask)) { 1649 SET(cs->cs_rr0_delta, delta); 1650 1651 /* 1652 * Pulse-per-second clock signal on edge of DCD? 1653 */ 1654 if (ISSET(delta, zst->zst_ppsmask)) { 1655 struct timeval tv; 1656 if (ISSET(rr0, zst->zst_ppsmask) == zst->zst_ppsassert) { 1657 /* XXX nanotime() */ 1658 microtime(&tv); 1659 TIMEVAL_TO_TIMESPEC(&tv, 1660 &zst->ppsinfo.assert_timestamp); 1661 if (zst->ppsparam.mode & PPS_OFFSETASSERT) { 1662 timespecadd(&zst->ppsinfo.assert_timestamp, 1663 &zst->ppsparam.assert_offset, 1664 &zst->ppsinfo.assert_timestamp); 1665 } 1666 1667#ifdef PPS_SYNC 1668 if (pps_kc_hardpps_source == zst && 1669 pps_kc_hardpps_mode & PPS_CAPTUREASSERT) { 1670 hardpps(&tv, tv.tv_usec); 1671 } 1672#endif 1673 zst->ppsinfo.assert_sequence++; 1674 zst->ppsinfo.current_mode = zst->ppsparam.mode; 1675 } else if (ISSET(rr0, zst->zst_ppsmask) == 1676 zst->zst_ppsclear) { 1677 /* XXX nanotime() */ 1678 microtime(&tv); 1679 TIMEVAL_TO_TIMESPEC(&tv, 1680 &zst->ppsinfo.clear_timestamp); 1681 if (zst->ppsparam.mode & PPS_OFFSETCLEAR) { 1682 timespecadd(&zst->ppsinfo.clear_timestamp, 1683 &zst->ppsparam.clear_offset, 1684 &zst->ppsinfo.clear_timestamp); 1685 } 1686 1687#ifdef PPS_SYNC 1688 if (pps_kc_hardpps_source == zst && 1689 pps_kc_hardpps_mode & PPS_CAPTURECLEAR) { 1690 hardpps(&tv, tv.tv_usec); 1691 } 1692#endif 1693 zst->ppsinfo.clear_sequence++; 1694 zst->ppsinfo.current_mode = zst->ppsparam.mode; 1695 } 1696 } 1697 1698 /* 1699 * Stop output immediately if we lose the output 1700 * flow control signal or carrier detect. 1701 */ 1702 if (ISSET(~rr0, cs->cs_rr0_mask)) { 1703 zst->zst_tbc = 0; 1704 zst->zst_heldtbc = 0; 1705 } 1706 1707 zst->zst_st_check = 1; 1708 cs->cs_softreq = 1; 1709 } 1710} 1711 1712void 1713zstty_diag(arg) 1714 void *arg; 1715{ 1716 struct zstty_softc *zst = arg; 1717 int overflows, floods; 1718 int s; 1719 1720 s = splzs(); 1721 overflows = zst->zst_overflows; 1722 zst->zst_overflows = 0; 1723 floods = zst->zst_floods; 1724 zst->zst_floods = 0; 1725 zst->zst_errors = 0; 1726 splx(s); 1727 1728 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n", 1729 zst->zst_dev.dv_xname, 1730 overflows, overflows == 1 ? "" : "s", 1731 floods, floods == 1 ? "" : "s"); 1732} 1733 1734integrate void 1735zstty_rxsoft(zst, tp) 1736 struct zstty_softc *zst; 1737 struct tty *tp; 1738{ 1739 struct zs_chanstate *cs = zst->zst_cs; 1740 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 1741 u_char *get, *end; 1742 u_int cc, scc; 1743 u_char rr1; 1744 int code; 1745 int s; 1746 1747 end = zst->zst_ebuf; 1748 get = zst->zst_rbget; 1749 scc = cc = zstty_rbuf_size - zst->zst_rbavail; 1750 1751 if (cc == zstty_rbuf_size) { 1752 zst->zst_floods++; 1753 if (zst->zst_errors++ == 0) 1754 callout_reset(&zst->zst_diag_ch, 60 * hz, 1755 zstty_diag, zst); 1756 } 1757 1758 /* If not yet open, drop the entire buffer content here */ 1759 if (!ISSET(tp->t_state, TS_ISOPEN)) { 1760 get += cc << 1; 1761 if (get >= end) 1762 get -= zstty_rbuf_size << 1; 1763 cc = 0; 1764 } 1765 while (cc) { 1766 code = get[0]; 1767 rr1 = get[1]; 1768 if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) { 1769 if (ISSET(rr1, ZSRR1_DO)) { 1770 zst->zst_overflows++; 1771 if (zst->zst_errors++ == 0) 1772 callout_reset(&zst->zst_diag_ch, 1773 60 * hz, zstty_diag, zst); 1774 } 1775 if (ISSET(rr1, ZSRR1_FE)) 1776 SET(code, TTY_FE); 1777 if (ISSET(rr1, ZSRR1_PE)) 1778 SET(code, TTY_PE); 1779 } 1780 if ((*rint)(code, tp) == -1) { 1781 /* 1782 * The line discipline's buffer is out of space. 1783 */ 1784 if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) { 1785 /* 1786 * We're either not using flow control, or the 1787 * line discipline didn't tell us to block for 1788 * some reason. Either way, we have no way to 1789 * know when there's more space available, so 1790 * just drop the rest of the data. 1791 */ 1792 get += cc << 1; 1793 if (get >= end) 1794 get -= zstty_rbuf_size << 1; 1795 cc = 0; 1796 } else { 1797 /* 1798 * Don't schedule any more receive processing 1799 * until the line discipline tells us there's 1800 * space available (through comhwiflow()). 1801 * Leave the rest of the data in the input 1802 * buffer. 1803 */ 1804 SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED); 1805 } 1806 break; 1807 } 1808 get += 2; 1809 if (get >= end) 1810 get = zst->zst_rbuf; 1811 cc--; 1812 } 1813 1814 if (cc != scc) { 1815 zst->zst_rbget = get; 1816 s = splzs(); 1817 simple_lock(&cs->cs_lock); 1818 cc = zst->zst_rbavail += scc - cc; 1819 /* Buffers should be ok again, release possible block. */ 1820 if (cc >= zst->zst_r_lowat) { 1821 if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) { 1822 CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED); 1823 SET(cs->cs_preg[1], ZSWR1_RIE); 1824 cs->cs_creg[1] = cs->cs_preg[1]; 1825 zs_write_reg(cs, 1, cs->cs_creg[1]); 1826 } 1827 if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) { 1828 CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED); 1829 zs_hwiflow(zst); 1830 } 1831 } 1832 simple_unlock(&cs->cs_lock); 1833 splx(s); 1834 } 1835 1836#if 0 1837 printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail); 1838#endif 1839} 1840 1841integrate void 1842zstty_txsoft(zst, tp) 1843 struct zstty_softc *zst; 1844 struct tty *tp; 1845{ 1846 struct zs_chanstate *cs = zst->zst_cs; 1847 int s; 1848 1849 s = splzs(); 1850 simple_lock(&cs->cs_lock); 1851 CLR(tp->t_state, TS_BUSY); 1852 if (ISSET(tp->t_state, TS_FLUSH)) 1853 CLR(tp->t_state, TS_FLUSH); 1854 else 1855 ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf)); 1856 simple_unlock(&cs->cs_lock); 1857 splx(s); 1858 (*tp->t_linesw->l_start)(tp); 1859} 1860 1861integrate void 1862zstty_stsoft(zst, tp) 1863 struct zstty_softc *zst; 1864 struct tty *tp; 1865{ 1866 struct zs_chanstate *cs = zst->zst_cs; 1867 u_char rr0, delta; 1868 int s; 1869 1870 s = splzs(); 1871 simple_lock(&cs->cs_lock); 1872 rr0 = cs->cs_rr0; 1873 delta = cs->cs_rr0_delta; 1874 cs->cs_rr0_delta = 0; 1875 simple_unlock(&cs->cs_lock); 1876 splx(s); 1877 1878 if (ISSET(delta, cs->cs_rr0_dcd)) { 1879 /* 1880 * Inform the tty layer that carrier detect changed. 1881 */ 1882 (void) (*tp->t_linesw->l_modem)(tp, ISSET(rr0, ZSRR0_DCD)); 1883 } 1884 1885 if (ISSET(delta, cs->cs_rr0_cts)) { 1886 /* Block or unblock output according to flow control. */ 1887 if (ISSET(rr0, cs->cs_rr0_cts)) { 1888 zst->zst_tx_stopped = 0; 1889 (*tp->t_linesw->l_start)(tp); 1890 } else { 1891 zst->zst_tx_stopped = 1; 1892 } 1893 } 1894} 1895 1896/* 1897 * Software interrupt. Called at zssoft 1898 * 1899 * The main job to be done here is to empty the input ring 1900 * by passing its contents up to the tty layer. The ring is 1901 * always emptied during this operation, therefore the ring 1902 * must not be larger than the space after "high water" in 1903 * the tty layer, or the tty layer might drop our input. 1904 * 1905 * Note: an "input blockage" condition is assumed to exist if 1906 * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set. 1907 */ 1908static void 1909zstty_softint(cs) 1910 struct zs_chanstate *cs; 1911{ 1912 struct zstty_softc *zst = cs->cs_private; 1913 struct tty *tp = zst->zst_tty; 1914 int s; 1915 1916 s = spltty(); 1917 1918 if (zst->zst_rx_ready) { 1919 zst->zst_rx_ready = 0; 1920 zstty_rxsoft(zst, tp); 1921 } 1922 1923 if (zst->zst_st_check) { 1924 zst->zst_st_check = 0; 1925 zstty_stsoft(zst, tp); 1926 } 1927 1928 if (zst->zst_tx_done) { 1929 zst->zst_tx_done = 0; 1930 zstty_txsoft(zst, tp); 1931 } 1932 1933 splx(s); 1934} 1935 1936struct zsops zsops_tty = { 1937 zstty_rxint, /* receive char available */ 1938 zstty_stint, /* external/status */ 1939 zstty_txint, /* xmit buffer empty */ 1940 zstty_softint, /* process software interrupt */ 1941}; 1942