1 /* $NetBSD: ixp12x0_com.c,v 1.19 2003/08/07 16:26:53 agc Exp $ */ 2 /* 3 * Copyright (c) 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Ichiro FUKUHARA and Naoto Shimazaki. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by IWAMOTO Toshihiro. 11 * 12 * This code is derived from software contributed to The NetBSD Foundation 13 * by Charles M. Hannum. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. All advertising materials mentioning features or use of this software 24 * must display the following acknowledgement: 25 * This product includes software developed by the NetBSD 26 * Foundation, Inc. and its contributors. 27 * 4. Neither the name of The NetBSD Foundation nor the names of its 28 * contributors may be used to endorse or promote products derived 29 * from this software without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 * POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 /* 45 * Copyright (c) 1991 The Regents of the University of California. 46 * All rights reserved. 47 * 48 * Redistribution and use in source and binary forms, with or without 49 * modification, are permitted provided that the following conditions 50 * are met: 51 * 1. Redistributions of source code must retain the above copyright 52 * notice, this list of conditions and the following disclaimer. 53 * 2. Redistributions in binary form must reproduce the above copyright 54 * notice, this list of conditions and the following disclaimer in the 55 * documentation and/or other materials provided with the distribution. 56 * 3. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)com.c 7.5 (Berkeley) 5/16/91 73 */ 74 75 #include <sys/cdefs.h> 76 __KERNEL_RCSID(0, "$NetBSD: ixp12x0_com.c,v 1.19 2003/08/07 16:26:53 agc Exp $"); 77 78 #include "opt_ddb.h" 79 #include "opt_kgdb.h" 80 81 #include "rnd.h" 82 #if NRND > 0 && defined(RND_COM) 83 #include <sys/rnd.h> 84 #endif 85 86 #include <sys/param.h> 87 #include <sys/systm.h> 88 #include <sys/types.h> 89 #include <sys/conf.h> 90 #include <sys/file.h> 91 #include <sys/device.h> 92 #include <sys/kernel.h> 93 #include <sys/malloc.h> 94 #include <sys/tty.h> 95 #include <sys/uio.h> 96 #include <sys/vnode.h> 97 98 #include <machine/intr.h> 99 #include <machine/bus.h> 100 101 #include <arm/ixp12x0/ixp12x0_comreg.h> 102 #include <arm/ixp12x0/ixp12x0_comvar.h> 103 #include <arm/ixp12x0/ixp12x0reg.h> 104 #include <arm/ixp12x0/ixp12x0var.h> 105 106 #include <arm/ixp12x0/ixpsipvar.h> 107 108 #include <dev/cons.h> 109 #include "ixpcom.h" 110 111 static int ixpcomparam(struct tty *, struct termios *); 112 static void ixpcomstart(struct tty *); 113 static int ixpcomhwiflow(struct tty *, int); 114 115 static u_int cflag2cr(tcflag_t); 116 static void ixpcom_iflush(struct ixpcom_softc *); 117 static void ixpcom_set_cr(struct ixpcom_softc *); 118 119 int ixpcomcngetc(dev_t); 120 void ixpcomcnputc(dev_t, int); 121 void ixpcomcnpollc(dev_t, int); 122 123 static void ixpcomsoft(void* arg); 124 inline static void ixpcom_txsoft(struct ixpcom_softc *, struct tty *); 125 inline static void ixpcom_rxsoft(struct ixpcom_softc *, struct tty *); 126 127 void ixpcomcnprobe(struct consdev *); 128 void ixpcomcninit(struct consdev *); 129 130 u_int32_t ixpcom_cr = 0; /* tell cr to *_intr.c */ 131 u_int32_t ixpcom_imask = 0; /* intrrupt mask from *_intr.c */ 132 133 134 static struct ixpcom_cons_softc { 135 bus_space_tag_t sc_iot; 136 bus_space_handle_t sc_ioh; 137 bus_addr_t sc_baseaddr; 138 int sc_ospeed; 139 tcflag_t sc_cflag; 140 int sc_attached; 141 } ixpcomcn_sc; 142 143 static struct cnm_state ixpcom_cnm_state; 144 145 struct ixpcom_softc* ixpcom_sc = NULL; 146 147 extern struct cfdriver ixpcom_cd; 148 149 dev_type_open(ixpcomopen); 150 dev_type_close(ixpcomclose); 151 dev_type_read(ixpcomread); 152 dev_type_write(ixpcomwrite); 153 dev_type_ioctl(ixpcomioctl); 154 dev_type_stop(ixpcomstop); 155 dev_type_tty(ixpcomtty); 156 dev_type_poll(ixpcompoll); 157 158 const struct cdevsw ixpcom_cdevsw = { 159 ixpcomopen, ixpcomclose, ixpcomread, ixpcomwrite, ixpcomioctl, 160 ixpcomstop, ixpcomtty, ixpcompoll, nommap, ttykqfilter, D_TTY 161 }; 162 163 struct consdev ixpcomcons = { 164 NULL, NULL, ixpcomcngetc, ixpcomcnputc, ixpcomcnpollc, NULL, 165 NULL, NULL, NODEV, CN_NORMAL 166 }; 167 168 #ifndef DEFAULT_COMSPEED 169 #define DEFAULT_COMSPEED 38400 170 #endif 171 172 #define COMUNIT_MASK 0x7ffff 173 #define COMDIALOUT_MASK 0x80000 174 175 #define COMUNIT(x) (minor(x) & COMUNIT_MASK) 176 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK) 177 178 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \ 179 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE)) 180 181 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f)) 182 183 #define COM_LOCK(sc); 184 #define COM_UNLOCK(sc); 185 186 #define SET(t, f) (t) |= (f) 187 #define CLR(t, f) (t) &= ~(f) 188 #define ISSET(t, f) ((t) & (f)) 189 190 #define CFLAGS2CR_MASK (CR_PE | CR_OES | CR_SBS | CR_DSS | CR_BRD) 191 192 void 193 ixpcom_attach_subr(sc) 194 struct ixpcom_softc *sc; 195 { 196 struct tty *tp; 197 198 ixpcom_sc = sc; 199 200 /* force to use ixpcom0 for console */ 201 if (sc->sc_iot == ixpcomcn_sc.sc_iot 202 && sc->sc_baseaddr == ixpcomcn_sc.sc_baseaddr) { 203 ixpcomcn_sc.sc_attached = 1; 204 sc->sc_speed = IXPCOMSPEED2BRD(ixpcomcn_sc.sc_ospeed); 205 206 /* Make sure the console is always "hardwired". */ 207 /* XXX IXPCOM_SR should be checked */ 208 delay(10000); /* wait for output to finish */ 209 SET(sc->sc_hwflags, COM_HW_CONSOLE); 210 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 211 } 212 213 tp = ttymalloc(); 214 tp->t_oproc = ixpcomstart; 215 tp->t_param = ixpcomparam; 216 tp->t_hwiflow = ixpcomhwiflow; 217 218 sc->sc_tty = tp; 219 sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT); 220 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 221 sc->sc_rbavail = IXPCOM_RING_SIZE; 222 if (sc->sc_rbuf == NULL) { 223 printf("%s: unable to allocate ring buffer\n", 224 sc->sc_dev.dv_xname); 225 return; 226 } 227 sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1); 228 sc->sc_tbc = 0; 229 230 sc->sc_rie = sc->sc_xie = 0; 231 ixpcom_cr = IXPCOMSPEED2BRD(DEFAULT_COMSPEED) 232 | CR_UE | sc->sc_rie | sc->sc_xie | DSS_8BIT; 233 234 tty_attach(tp); 235 236 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 237 int maj; 238 239 /* locate the major number */ 240 maj = cdevsw_lookup_major(&ixpcom_cdevsw); 241 242 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit); 243 244 aprint_normal("%s: console\n", sc->sc_dev.dv_xname); 245 } 246 247 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixpcomsoft, sc); 248 249 #if NRND > 0 && defined(RND_COM) 250 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, 251 RND_TYPE_TTY, 0); 252 #endif 253 254 /* if there are no enable/disable functions, assume the device 255 is always enabled */ 256 if (!sc->enable) 257 sc->enabled = 1; 258 259 /* XXX configure register */ 260 /* xxx_config(sc) */ 261 262 SET(sc->sc_hwflags, COM_HW_DEV_OK); 263 } 264 265 static int 266 ixpcomparam(tp, t) 267 struct tty *tp; 268 struct termios *t; 269 { 270 struct ixpcom_softc *sc 271 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev)); 272 u_int32_t cr; 273 int s; 274 275 if (COM_ISALIVE(sc) == 0) 276 return (EIO); 277 278 cr = IXPCOMSPEED2BRD(t->c_ospeed); 279 280 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 281 return (EINVAL); 282 283 /* 284 * For the console, always force CLOCAL and !HUPCL, so that the port 285 * is always active. 286 */ 287 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 288 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 289 SET(t->c_cflag, CLOCAL); 290 CLR(t->c_cflag, HUPCL); 291 } 292 293 /* 294 * If there were no changes, don't do anything. This avoids dropping 295 * input and improves performance when all we did was frob things like 296 * VMIN and VTIME. 297 */ 298 if (tp->t_ospeed == t->c_ospeed && 299 tp->t_cflag == t->c_cflag) 300 return (0); 301 302 cr |= cflag2cr(t->c_cflag); 303 304 s = splserial(); 305 COM_LOCK(sc); 306 307 ixpcom_cr = (ixpcom_cr & ~CFLAGS2CR_MASK) | cr; 308 309 /* 310 * ixpcom don't have any hardware flow control. 311 * we skip it. 312 */ 313 314 /* And copy to tty. */ 315 tp->t_ispeed = 0; 316 tp->t_ospeed = t->c_ospeed; 317 tp->t_cflag = t->c_cflag; 318 319 if (!sc->sc_heldchange) { 320 if (sc->sc_tx_busy) { 321 sc->sc_heldtbc = sc->sc_tbc; 322 sc->sc_tbc = 0; 323 sc->sc_heldchange = 1; 324 } else 325 ixpcom_set_cr(sc); 326 } 327 328 COM_UNLOCK(sc); 329 splx(s); 330 331 /* 332 * Update the tty layer's idea of the carrier bit. 333 * We tell tty the carrier is always on. 334 */ 335 (void) (*tp->t_linesw->l_modem)(tp, 1); 336 337 #ifdef COM_DEBUG 338 if (com_debug) 339 comstatus(sc, "comparam "); 340 #endif 341 342 if (!ISSET(t->c_cflag, CHWFLOW)) { 343 if (sc->sc_tx_stopped) { 344 sc->sc_tx_stopped = 0; 345 ixpcomstart(tp); 346 } 347 } 348 349 return (0); 350 } 351 352 static int 353 ixpcomhwiflow(tp, block) 354 struct tty *tp; 355 int block; 356 { 357 return (0); 358 } 359 360 static void 361 ixpcom_filltx(struct ixpcom_softc *sc) 362 { 363 bus_space_tag_t iot = sc->sc_iot; 364 bus_space_handle_t ioh = sc->sc_ioh; 365 int n; 366 367 n = 0; 368 while (bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR) { 369 if (n >= sc->sc_tbc) 370 break; 371 bus_space_write_4(iot, ioh, IXPCOM_DR, 372 0xff & *(sc->sc_tba + n)); 373 n++; 374 } 375 sc->sc_tbc -= n; 376 sc->sc_tba += n; 377 } 378 379 static void 380 ixpcomstart(tp) 381 struct tty *tp; 382 { 383 struct ixpcom_softc *sc 384 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev)); 385 int s; 386 387 if (COM_ISALIVE(sc) == 0) 388 return; 389 390 s = spltty(); 391 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 392 goto out; 393 if (sc->sc_tx_stopped) 394 goto out; 395 396 if (tp->t_outq.c_cc <= tp->t_lowat) { 397 if (ISSET(tp->t_state, TS_ASLEEP)) { 398 CLR(tp->t_state, TS_ASLEEP); 399 wakeup(&tp->t_outq); 400 } 401 selwakeup(&tp->t_wsel); 402 if (tp->t_outq.c_cc == 0) 403 goto out; 404 } 405 406 /* Grab the first contiguous region of buffer space. */ 407 { 408 u_char *tba; 409 int tbc; 410 411 tba = tp->t_outq.c_cf; 412 tbc = ndqb(&tp->t_outq, 0); 413 414 (void)splserial(); 415 COM_LOCK(sc); 416 417 sc->sc_tba = tba; 418 sc->sc_tbc = tbc; 419 } 420 421 SET(tp->t_state, TS_BUSY); 422 sc->sc_tx_busy = 1; 423 424 /* Enable transmit completion interrupts if necessary. */ 425 if (!ISSET(sc->sc_xie, CR_XIE)) { 426 SET(sc->sc_xie, CR_XIE); 427 ixpcom_set_cr(sc); 428 } 429 430 /* Output the first chunk of the contiguous buffer. */ 431 ixpcom_filltx(sc); 432 433 COM_UNLOCK(sc); 434 out: 435 splx(s); 436 return; 437 } 438 439 static void 440 ixpcom_break(struct ixpcom_softc *sc, int onoff) 441 { 442 if (onoff) 443 SET(ixpcom_cr, CR_BRK); 444 else 445 CLR(ixpcom_cr, CR_BRK); 446 if (!sc->sc_heldchange) { 447 if (sc->sc_tx_busy) { 448 sc->sc_heldtbc = sc->sc_tbc; 449 sc->sc_tbc = 0; 450 sc->sc_heldchange = 1; 451 } else 452 ixpcom_set_cr(sc); 453 } 454 } 455 456 static void 457 ixpcom_shutdown(struct ixpcom_softc *sc) 458 { 459 int s; 460 461 s = splserial(); 462 COM_LOCK(sc); 463 464 /* Clear any break condition set with TIOCSBRK. */ 465 ixpcom_break(sc, 0); 466 467 /* Turn off interrupts. */ 468 sc->sc_rie = sc->sc_xie = 0; 469 ixpcom_set_cr(sc); 470 471 if (sc->disable) { 472 #ifdef DIAGNOSTIC 473 if (!sc->enabled) 474 panic("ixpcom_shutdown: not enabled?"); 475 #endif 476 (*sc->disable)(sc); 477 sc->enabled = 0; 478 } 479 COM_UNLOCK(sc); 480 splx(s); 481 } 482 483 int 484 ixpcomopen(dev, flag, mode, p) 485 dev_t dev; 486 int flag, mode; 487 struct proc *p; 488 { 489 struct ixpcom_softc *sc; 490 struct tty *tp; 491 int s, s2; 492 int error; 493 494 sc = device_lookup(&ixpcom_cd, COMUNIT(dev)); 495 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) || 496 sc->sc_rbuf == NULL) 497 return (ENXIO); 498 499 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0) 500 return (ENXIO); 501 502 #ifdef KGDB 503 /* 504 * If this is the kgdb port, no other use is permitted. 505 */ 506 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) 507 return (EBUSY); 508 #endif 509 510 tp = sc->sc_tty; 511 512 if (ISSET(tp->t_state, TS_ISOPEN) && 513 ISSET(tp->t_state, TS_XCLUDE) && 514 p->p_ucred->cr_uid != 0) 515 return (EBUSY); 516 517 s = spltty(); 518 519 /* 520 * Do the following iff this is a first open. 521 */ 522 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 523 struct termios t; 524 525 tp->t_dev = dev; 526 527 s2 = splserial(); 528 COM_LOCK(sc); 529 530 if (sc->enable) { 531 if ((*sc->enable)(sc)) { 532 COM_UNLOCK(sc); 533 splx(s2); 534 splx(s); 535 printf("%s: device enable failed\n", 536 sc->sc_dev.dv_xname); 537 return (EIO); 538 } 539 sc->enabled = 1; 540 #if 0 541 /* XXXXXXXXXXXXXXX */ 542 com_config(sc); 543 #endif 544 } 545 546 /* Turn on interrupts. */ 547 SET(sc->sc_rie, CR_RIE); 548 ixpcom_set_cr(sc); 549 550 #if 0 551 /* Fetch the current modem control status, needed later. */ 552 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr); 553 554 /* Clear PPS capture state on first open. */ 555 sc->sc_ppsmask = 0; 556 sc->ppsparam.mode = 0; 557 #endif 558 559 COM_UNLOCK(sc); 560 splx(s2); 561 562 /* 563 * Initialize the termios status to the defaults. Add in the 564 * sticky bits from TIOCSFLAGS. 565 */ 566 t.c_ispeed = 0; 567 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 568 t.c_ospeed = ixpcomcn_sc.sc_ospeed; 569 t.c_cflag = ixpcomcn_sc.sc_cflag; 570 } else { 571 t.c_ospeed = TTYDEF_SPEED; 572 t.c_cflag = TTYDEF_CFLAG; 573 } 574 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 575 SET(t.c_cflag, CLOCAL); 576 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 577 SET(t.c_cflag, CRTSCTS); 578 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 579 SET(t.c_cflag, MDMBUF); 580 /* Make sure ixpcomparam() will do something. */ 581 tp->t_ospeed = 0; 582 (void) ixpcomparam(tp, &t); 583 tp->t_iflag = TTYDEF_IFLAG; 584 tp->t_oflag = TTYDEF_OFLAG; 585 tp->t_lflag = TTYDEF_LFLAG; 586 ttychars(tp); 587 ttsetwater(tp); 588 589 s2 = splserial(); 590 COM_LOCK(sc); 591 592 /* Clear the input ring, and unblock. */ 593 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 594 sc->sc_rbavail = IXPCOM_RING_SIZE; 595 ixpcom_iflush(sc); 596 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 597 598 #ifdef COM_DEBUG 599 if (ixpcom_debug) 600 comstatus(sc, "ixpcomopen "); 601 #endif 602 603 COM_UNLOCK(sc); 604 splx(s2); 605 } 606 607 splx(s); 608 609 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 610 if (error) 611 goto bad; 612 613 error = (*tp->t_linesw->l_open)(dev, tp); 614 if (error) 615 goto bad; 616 617 return (0); 618 619 bad: 620 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 621 /* 622 * We failed to open the device, and nobody else had it opened. 623 * Clean up the state as appropriate. 624 */ 625 ixpcom_shutdown(sc); 626 } 627 628 return (error); 629 } 630 631 int 632 ixpcomclose(dev, flag, mode, p) 633 dev_t dev; 634 int flag, mode; 635 struct proc *p; 636 { 637 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev)); 638 struct tty *tp = sc->sc_tty; 639 640 /* XXX This is for cons.c. */ 641 if (!ISSET(tp->t_state, TS_ISOPEN)) 642 return (0); 643 644 (*tp->t_linesw->l_close)(tp, flag); 645 ttyclose(tp); 646 647 if (COM_ISALIVE(sc) == 0) 648 return (0); 649 650 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 651 /* 652 * Although we got a last close, the device may still be in 653 * use; e.g. if this was the dialout node, and there are still 654 * processes waiting for carrier on the non-dialout node. 655 */ 656 ixpcom_shutdown(sc); 657 } 658 659 return (0); 660 } 661 662 int 663 ixpcomread(dev, uio, flag) 664 dev_t dev; 665 struct uio *uio; 666 int flag; 667 { 668 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev)); 669 struct tty *tp = sc->sc_tty; 670 671 if (COM_ISALIVE(sc) == 0) 672 return (EIO); 673 674 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 675 } 676 677 int 678 ixpcomwrite(dev, uio, flag) 679 dev_t dev; 680 struct uio *uio; 681 int flag; 682 { 683 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev)); 684 struct tty *tp = sc->sc_tty; 685 686 if (COM_ISALIVE(sc) == 0) 687 return (EIO); 688 689 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 690 } 691 692 int 693 ixpcompoll(dev, events, p) 694 dev_t dev; 695 int events; 696 struct proc *p; 697 { 698 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev)); 699 struct tty *tp = sc->sc_tty; 700 701 if (COM_ISALIVE(sc) == 0) 702 return (EIO); 703 704 return ((*tp->t_linesw->l_poll)(tp, events, p)); 705 } 706 707 struct tty * 708 ixpcomtty(dev) 709 dev_t dev; 710 { 711 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev)); 712 struct tty *tp = sc->sc_tty; 713 714 return (tp); 715 } 716 717 int 718 ixpcomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 719 { 720 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev)); 721 struct tty *tp = sc->sc_tty; 722 int error; 723 int s; 724 725 if (COM_ISALIVE(sc) == 0) 726 return (EIO); 727 728 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 729 if (error != EPASSTHROUGH) 730 return (error); 731 732 error = ttioctl(tp, cmd, data, flag, p); 733 if (error != EPASSTHROUGH) 734 return (error); 735 736 error = 0; 737 738 s = splserial(); 739 COM_LOCK(sc); 740 741 switch (cmd) { 742 case TIOCSBRK: 743 ixpcom_break(sc, 1); 744 break; 745 746 case TIOCCBRK: 747 ixpcom_break(sc, 0); 748 break; 749 750 case TIOCGFLAGS: 751 *(int *)data = sc->sc_swflags; 752 break; 753 754 case TIOCSFLAGS: 755 error = suser(p->p_ucred, &p->p_acflag); 756 if (error) 757 break; 758 sc->sc_swflags = *(int *)data; 759 break; 760 761 default: 762 error = EPASSTHROUGH; 763 break; 764 } 765 766 COM_UNLOCK(sc); 767 splx(s); 768 769 return (error); 770 } 771 772 /* 773 * Stop output on a line. 774 */ 775 void 776 ixpcomstop(tp, flag) 777 struct tty *tp; 778 int flag; 779 { 780 struct ixpcom_softc *sc 781 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev)); 782 int s; 783 784 s = splserial(); 785 COM_LOCK(sc); 786 if (ISSET(tp->t_state, TS_BUSY)) { 787 /* Stop transmitting at the next chunk. */ 788 sc->sc_tbc = 0; 789 sc->sc_heldtbc = 0; 790 if (!ISSET(tp->t_state, TS_TTSTOP)) 791 SET(tp->t_state, TS_FLUSH); 792 } 793 COM_UNLOCK(sc); 794 splx(s); 795 } 796 797 static u_int 798 cflag2cr(cflag) 799 tcflag_t cflag; 800 { 801 u_int cr; 802 803 cr = (cflag & PARENB) ? CR_PE : 0; 804 cr |= (cflag & PARODD) ? CR_OES : 0; 805 cr |= (cflag & CSTOPB) ? CR_SBS : 0; 806 cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0; 807 808 return (cr); 809 } 810 811 static void 812 ixpcom_iflush(sc) 813 struct ixpcom_softc *sc; 814 { 815 bus_space_tag_t iot = sc->sc_iot; 816 bus_space_handle_t ioh = sc->sc_ioh; 817 #ifdef DIAGNOSTIC 818 int reg; 819 #endif 820 int timo; 821 822 #ifdef DIAGNOSTIC 823 reg = 0xffff; 824 #endif 825 timo = 50000; 826 /* flush any pending I/O */ 827 while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR) 828 && --timo) 829 #ifdef DIAGNOSTIC 830 reg = 831 #else 832 (void) 833 #endif 834 bus_space_read_4(iot, ioh, IXPCOM_DR); 835 #ifdef DIAGNOSTIC 836 if (!timo) 837 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname, 838 reg); 839 #endif 840 } 841 842 static void 843 ixpcom_set_cr(struct ixpcom_softc *sc) 844 { 845 /* XXX */ 846 ixpcom_cr &= ~(CR_RIE | CR_XIE); 847 ixpcom_cr |= sc->sc_rie | sc->sc_xie; 848 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR, 849 ixpcom_cr & ~ixpcom_imask); 850 } 851 852 int 853 ixpcomcnattach(iot, iobase, ioh, ospeed, cflag) 854 bus_space_tag_t iot; 855 bus_addr_t iobase; 856 bus_space_handle_t ioh; 857 int ospeed; 858 tcflag_t cflag; 859 { 860 int cr; 861 862 cn_tab = &ixpcomcons; 863 cn_init_magic(&ixpcom_cnm_state); 864 /* 865 * XXX 866 * 867 * ixpcom cannot detect a break. It can only detect framing 868 * errors. And, a sequence of "framing error, framing error" 869 * meens a break. So I made this hack: 870 * 871 * 1. Set default magic is a sequence of "BREAK, BREAK". 872 * 2. Tell cn_check_magic() a "framing error" as BREAK. 873 * 874 * see ixpcom_intr() too. 875 * 876 */ 877 /* default magic is a couple of BREAK. */ 878 cn_set_magic("\047\001\047\001"); 879 880 ixpcomcn_sc.sc_iot = iot; 881 ixpcomcn_sc.sc_ioh = ioh; 882 ixpcomcn_sc.sc_baseaddr = iobase; 883 ixpcomcn_sc.sc_ospeed = ospeed; 884 ixpcomcn_sc.sc_cflag = cflag; 885 886 cr = cflag2cr(cflag); 887 cr |= IXPCOMSPEED2BRD(ospeed); 888 cr |= CR_UE; 889 ixpcom_cr = cr; 890 891 /* enable the UART */ 892 bus_space_write_4(iot, iobase, IXPCOM_CR, cr); 893 894 return (0); 895 } 896 897 void 898 ixpcomcnprobe(cp) 899 struct consdev *cp; 900 { 901 cp->cn_pri = CN_REMOTE; 902 } 903 904 void 905 ixpcomcnpollc(dev, on) 906 dev_t dev; 907 int on; 908 { 909 } 910 911 void 912 ixpcomcnputc(dev, c) 913 dev_t dev; 914 int c; 915 { 916 int s; 917 bus_space_tag_t iot = ixpcomcn_sc.sc_iot; 918 bus_space_handle_t ioh = ixpcomcn_sc.sc_ioh; 919 920 s = splserial(); 921 922 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR)) 923 ; 924 925 bus_space_write_4(iot, ioh, IXPCOM_DR, c); 926 927 #ifdef DEBUG 928 if (c == '\r') { 929 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXE)) 930 ; 931 } 932 #endif 933 934 splx(s); 935 } 936 937 int 938 ixpcomcngetc(dev) 939 dev_t dev; 940 { 941 int c; 942 int s; 943 bus_space_tag_t iot = ixpcomcn_sc.sc_iot; 944 bus_space_handle_t ioh = ixpcomcn_sc.sc_ioh; 945 946 s = splserial(); 947 948 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_RXR)) 949 ; 950 951 c = bus_space_read_4(iot, ioh, IXPCOM_DR); 952 c &= 0xff; 953 splx(s); 954 955 return (c); 956 } 957 958 inline static void 959 ixpcom_txsoft(sc, tp) 960 struct ixpcom_softc *sc; 961 struct tty *tp; 962 { 963 CLR(tp->t_state, TS_BUSY); 964 if (ISSET(tp->t_state, TS_FLUSH)) 965 CLR(tp->t_state, TS_FLUSH); 966 else 967 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf)); 968 (*tp->t_linesw->l_start)(tp); 969 } 970 971 inline static void 972 ixpcom_rxsoft(sc, tp) 973 struct ixpcom_softc *sc; 974 struct tty *tp; 975 { 976 int (*rint) __P((int c, struct tty *tp)) = tp->t_linesw->l_rint; 977 u_char *get, *end; 978 u_int cc, scc; 979 u_char lsr; 980 int code; 981 int s; 982 983 end = sc->sc_ebuf; 984 get = sc->sc_rbget; 985 scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail; 986 #if 0 987 if (cc == IXPCOM_RING_SIZE) { 988 sc->sc_floods++; 989 if (sc->sc_errors++ == 0) 990 callout_reset(&sc->sc_diag_callout, 60 * hz, 991 comdiag, sc); 992 } 993 #endif 994 while (cc) { 995 code = get[0]; 996 lsr = get[1]; 997 if (ISSET(lsr, DR_ROR | DR_FRE | DR_PRE)) { 998 #if 0 999 if (ISSET(lsr, DR_ROR)) { 1000 sc->sc_overflows++; 1001 if (sc->sc_errors++ == 0) 1002 callout_reset(&sc->sc_diag_callout, 1003 60 * hz, comdiag, sc); 1004 } 1005 #endif 1006 if (ISSET(lsr, DR_FRE)) 1007 SET(code, TTY_FE); 1008 if (ISSET(lsr, DR_PRE)) 1009 SET(code, TTY_PE); 1010 } 1011 if ((*rint)(code, tp) == -1) { 1012 /* 1013 * The line discipline's buffer is out of space. 1014 */ 1015 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 1016 /* 1017 * We're either not using flow control, or the 1018 * line discipline didn't tell us to block for 1019 * some reason. Either way, we have no way to 1020 * know when there's more space available, so 1021 * just drop the rest of the data. 1022 */ 1023 get += cc << 1; 1024 if (get >= end) 1025 get -= IXPCOM_RING_SIZE << 1; 1026 cc = 0; 1027 } else { 1028 /* 1029 * Don't schedule any more receive processing 1030 * until the line discipline tells us there's 1031 * space available (through comhwiflow()). 1032 * Leave the rest of the data in the input 1033 * buffer. 1034 */ 1035 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 1036 } 1037 break; 1038 } 1039 get += 2; 1040 if (get >= end) 1041 get = sc->sc_rbuf; 1042 cc--; 1043 } 1044 1045 if (cc != scc) { 1046 sc->sc_rbget = get; 1047 s = splserial(); 1048 COM_LOCK(sc); 1049 1050 cc = sc->sc_rbavail += scc - cc; 1051 /* Buffers should be ok again, release possible block. */ 1052 if (cc >= 1) { 1053 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 1054 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1055 SET(sc->sc_rie, CR_RIE); 1056 ixpcom_set_cr(sc); 1057 } 1058 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) { 1059 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED); 1060 #if 0 1061 com_hwiflow(sc); 1062 #endif 1063 } 1064 } 1065 COM_UNLOCK(sc); 1066 splx(s); 1067 } 1068 } 1069 1070 static void 1071 ixpcomsoft(void* arg) 1072 { 1073 struct ixpcom_softc *sc = arg; 1074 1075 if (COM_ISALIVE(sc) == 0) 1076 return; 1077 1078 if (sc->sc_rx_ready) { 1079 sc->sc_rx_ready = 0; 1080 ixpcom_rxsoft(sc, sc->sc_tty); 1081 } 1082 if (sc->sc_tx_done) { 1083 sc->sc_tx_done = 0; 1084 ixpcom_txsoft(sc, sc->sc_tty); 1085 } 1086 } 1087 1088 int 1089 ixpcomintr(void* arg) 1090 { 1091 struct ixpcom_softc *sc = arg; 1092 bus_space_tag_t iot = sc->sc_iot; 1093 bus_space_handle_t ioh = sc->sc_ioh; 1094 u_char *put, *end; 1095 u_int cc; 1096 u_int cr; 1097 u_int sr; 1098 u_int32_t c; 1099 1100 if (COM_ISALIVE(sc) == 0) 1101 return (0); 1102 1103 COM_LOCK(sc); 1104 cr = bus_space_read_4(iot, ioh, IXPCOM_CR) & CR_UE; 1105 1106 if (!cr) { 1107 COM_UNLOCK(sc); 1108 return (0); 1109 } 1110 1111 sr = bus_space_read_4(iot, ioh, IXPCOM_SR); 1112 if (!ISSET(sr, SR_TXR | SR_RXR)) 1113 return (0); 1114 1115 /* 1116 * XXX 1117 * 1118 * ixpcom cannot detect a break. It can only detect framing 1119 * errors. And, a sequence of "framing error, framing error" 1120 * meens a break. So I made this hack: 1121 * 1122 * 1. Set default magic is a sequence of "BREAK, BREAK". 1123 * 2. Tell cn_check_magic() a "framing error" as BREAK. 1124 * 1125 * see ixpcomcnattach() too. 1126 * 1127 */ 1128 if (ISSET(sr, SR_FRE)) { 1129 cn_check_magic(sc->sc_tty->t_dev, 1130 CNC_BREAK, ixpcom_cnm_state); 1131 } 1132 1133 end = sc->sc_ebuf; 1134 put = sc->sc_rbput; 1135 cc = sc->sc_rbavail; 1136 1137 if (ISSET(sr, SR_RXR)) { 1138 if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 1139 while (cc > 0) { 1140 if (!ISSET(sr, SR_RXR)) 1141 break; 1142 c = bus_space_read_4(iot, ioh, IXPCOM_DR); 1143 if (ISSET(c, DR_FRE)) { 1144 cn_check_magic(sc->sc_tty->t_dev, 1145 CNC_BREAK, 1146 ixpcom_cnm_state); 1147 } 1148 put[0] = c & 0xff; 1149 put[1] = (c >> 8) & 0xff; 1150 cn_check_magic(sc->sc_tty->t_dev, 1151 put[0], ixpcom_cnm_state); 1152 put += 2; 1153 if (put >= end) 1154 put = sc->sc_rbuf; 1155 cc--; 1156 1157 sr = bus_space_read_4(iot, ioh, IXPCOM_SR); 1158 } 1159 1160 /* 1161 * Current string of incoming characters ended because 1162 * no more data was available or we ran out of space. 1163 * Schedule a receive event if any data was received. 1164 * If we're out of space, turn off receive interrupts. 1165 */ 1166 sc->sc_rbput = put; 1167 sc->sc_rbavail = cc; 1168 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) 1169 sc->sc_rx_ready = 1; 1170 1171 /* 1172 * See if we are in danger of overflowing a buffer. If 1173 * so, use hardware flow control to ease the pressure. 1174 */ 1175 1176 /* but ixpcom cannot. X-( */ 1177 1178 /* 1179 * If we're out of space, disable receive interrupts 1180 * until the queue has drained a bit. 1181 */ 1182 if (!cc) { 1183 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1184 CLR(sc->sc_rie, CR_RIE); 1185 ixpcom_set_cr(sc); 1186 } 1187 } else { 1188 #ifdef DIAGNOSTIC 1189 panic("ixpcomintr: we shouldn't reach here"); 1190 #endif 1191 CLR(sc->sc_rie, CR_RIE); 1192 ixpcom_set_cr(sc); 1193 } 1194 } 1195 1196 /* 1197 * Done handling any receive interrupts. See if data can be 1198 * transmitted as well. Schedule tx done event if no data left 1199 * and tty was marked busy. 1200 */ 1201 sr = bus_space_read_4(iot, ioh, IXPCOM_SR); 1202 if (ISSET(sr, SR_TXR)) { 1203 /* 1204 * If we've delayed a parameter change, do it now, and restart 1205 * output. 1206 */ 1207 if (sc->sc_heldchange) { 1208 ixpcom_set_cr(sc); 1209 sc->sc_heldchange = 0; 1210 sc->sc_tbc = sc->sc_heldtbc; 1211 sc->sc_heldtbc = 0; 1212 } 1213 1214 /* Output the next chunk of the contiguous buffer, if any. */ 1215 if (sc->sc_tbc > 0) { 1216 ixpcom_filltx(sc); 1217 } else { 1218 /* Disable transmit completion interrupts if necessary. */ 1219 if (ISSET(sc->sc_xie, CR_XIE)) { 1220 CLR(sc->sc_xie, CR_XIE); 1221 ixpcom_set_cr(sc); 1222 } 1223 if (sc->sc_tx_busy) { 1224 sc->sc_tx_busy = 0; 1225 sc->sc_tx_done = 1; 1226 } 1227 } 1228 } 1229 COM_UNLOCK(sc); 1230 1231 /* Wake up the poller. */ 1232 softintr_schedule(sc->sc_si); 1233 1234 #if NRND > 0 && defined(RND_COM) 1235 rnd_add_uint32(&sc->rnd_source, iir | lsr); 1236 #endif 1237 return (1); 1238 } 1239