163}; 164 165static devclass_t rc_devclass; 166 167/* Flags */ 168#define RC_DTR_OFF 0x0001 /* DTR wait, for close/open */ 169#define RC_ACTOUT 0x0002 /* Dial-out port active */ 170#define RC_RTSFLOW 0x0004 /* RTS flow ctl enabled */ 171#define RC_CTSFLOW 0x0008 /* CTS flow ctl enabled */ 172#define RC_DORXFER 0x0010 /* RXFER event planned */ 173#define RC_DOXXFER 0x0020 /* XXFER event planned */ 174#define RC_MODCHG 0x0040 /* Modem status changed */ 175#define RC_OSUSP 0x0080 /* Output suspended */ 176#define RC_OSBUSY 0x0100 /* start() routine in progress */ 177#define RC_WAS_BUFOVFL 0x0200 /* low-level buffer ovferflow */ 178#define RC_WAS_SILOVFL 0x0400 /* silo buffer overflow */ 179#define RC_SEND_RDY 0x0800 /* ready to send */ 180 181/* Table for translation of RCSR status bits to internal form */ 182static int rc_rcsrt[16] = { 183 0, TTY_OE, TTY_FE, 184 TTY_FE|TTY_OE, TTY_PE, TTY_PE|TTY_OE, 185 TTY_PE|TTY_FE, TTY_PE|TTY_FE|TTY_OE, TTY_BI, 186 TTY_BI|TTY_OE, TTY_BI|TTY_FE, TTY_BI|TTY_FE|TTY_OE, 187 TTY_BI|TTY_PE, TTY_BI|TTY_PE|TTY_OE, TTY_BI|TTY_PE|TTY_FE, 188 TTY_BI|TTY_PE|TTY_FE|TTY_OE 189}; 190 191static int rc_ports[] = 192 { 0x220, 0x240, 0x250, 0x260, 0x2a0, 0x2b0, 0x300, 0x320 }; 193static int iobase_addrs[IOBASE_ADDRS] = 194 { 0, 0x400, 0x800, 0xc00, 0x1400, 0x1800, 0x1c00, 0x2000, 195 0x3000, 0x3400, 0x3800, 0x3c00, 0x4000, 0x8000 }; 196 197/**********************************************/ 198 199static int 200rc_probe(device_t dev) 201{ 202 u_int port; 203 int i, found; 204 205 /* 206 * We don't know of any PnP ID's for these cards. 207 */ 208 if (isa_get_logicalid(dev) != 0) 209 return (ENXIO); 210 211 /* 212 * We have to have an IO port hint that is valid. 213 */ 214 port = isa_get_port(dev); 215 if (port == -1) 216 return (ENXIO); 217 found = 0; 218 for (i = 0; i < sizeof(rc_ports) / sizeof(int); i++) 219 if (rc_ports[i] == port) { 220 found = 1; 221 break; 222 } 223 if (!found) 224 return (ENXIO); 225 226 /* 227 * We have to have an IRQ hint. 228 */ 229 if (isa_get_irq(dev) == -1) 230 return (ENXIO); 231 232 device_set_desc(dev, "SDL Riscom/8"); 233 return (0); 234} 235 236static int 237rc_attach(device_t dev) 238{ 239 struct rc_chans *rc; 240 struct tty *tp; 241 struct rc_softc *sc; 242 u_int port; 243 int base, chan, error, i, x; 244 dev_t cdev; 245 246 sc = device_get_softc(dev); 247 sc->sc_dev = dev; 248 249 /* 250 * We need to have IO ports. Lots of them. We need 251 * the following ranges relative to the base port: 252 * 0x0 - 0x10 253 * 0x400 - 0x410 254 * 0x800 - 0x810 255 * 0xc00 - 0xc10 256 * 0x1400 - 0x1410 257 * 0x1800 - 0x1810 258 * 0x1c00 - 0x1c10 259 * 0x2000 - 0x2010 260 * 0x3000 - 0x3010 261 * 0x3400 - 0x3410 262 * 0x3800 - 0x3810 263 * 0x3c00 - 0x3c10 264 * 0x4000 - 0x4010 265 * 0x8000 - 0x8010 266 */ 267 port = isa_get_port(dev); 268 for (i = 0; i < IOBASE_ADDRS; i++) 269 if (bus_set_resource(dev, SYS_RES_IOPORT, i, 270 port + iobase_addrs[i], 0x10) != 0) 271 return (ENXIO); 272 error = ENOMEM; 273 for (i = 0; i < IOBASE_ADDRS; i++) { 274 x = i; 275 sc->sc_port[i] = bus_alloc_resource(dev, SYS_RES_IOPORT, &x, 276 0ul, ~0ul, 0x10, RF_ACTIVE); 277 if (x != i) { 278 device_printf(dev, "ioport %d was rid %d\n", i, x); 279 goto fail; 280 } 281 if (sc->sc_port[i] == NULL) { 282 device_printf(dev, "failed to alloc ioports %x-%x\n", 283 port + iobase_addrs[i], 284 port + iobase_addrs[i] + 0x10); 285 goto fail; 286 } 287 } 288 sc->sc_bt = rman_get_bustag(sc->sc_port[0]); 289 sc->sc_bh = rman_get_bushandle(sc->sc_port[0]); 290 291 sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid, 292 0ul, ~0ul, 1, RF_ACTIVE); 293 if (sc->sc_irq == NULL) { 294 device_printf(dev, "failed to alloc IRQ\n"); 295 goto fail; 296 } 297 298 /* 299 * Now do some actual tests to make sure it works. 300 */ 301 error = ENXIO; 302 rcout(sc, CD180_PPRL, 0x22); /* Random values to Prescale reg. */ 303 rcout(sc, CD180_PPRH, 0x11); 304 if (rcin(sc, CD180_PPRL) != 0x22 || rcin(sc, CD180_PPRH) != 0x11) 305 goto fail; 306 if (rc_test(sc)) 307 goto fail; 308 309 /* 310 * Ok, start actually hooking things up. 311 */ 312 sc->sc_unit = device_get_unit(dev); 313 /*sc->sc_chipid = 0x10 + device_get_unit(dev);*/ 314 device_printf(dev, "%d chans, firmware rev. %c\n", 315 CD180_NCHAN, (rcin(sc, CD180_GFRCR) & 0xF) + 'A'); 316 rc = sc->sc_channels; 317 base = CD180_NCHAN * sc->sc_unit; 318 for (chan = 0; chan < CD180_NCHAN; chan++, rc++) { 319 rc->rc_rcb = sc; 320 rc->rc_chan = chan; 321 rc->rc_iptr = rc->rc_ibuf; 322 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE]; 323 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER]; 324 rc->rc_optr = rc->rc_obufend = rc->rc_obuf; 325 rc->rc_dtrwait = 3 * hz; 326 callout_init(&rc->rc_dtrcallout, 0); 327 tp = &rc->rc_tp; 328 ttychars(tp); 329 tp->t_lflag = tp->t_iflag = tp->t_oflag = 0; 330 tp->t_cflag = TTYDEF_CFLAG; 331 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 332 cdev = make_dev(&rc_cdevsw, chan + base, 333 UID_ROOT, GID_WHEEL, 0600, "ttym%d", chan + base); 334 cdev->si_drv1 = rc; 335 cdev->si_drv2 = 0; 336 cdev->si_tty = tp; 337 rc->rc_dev = cdev; 338 cdev = make_dev(&rc_cdevsw, chan + base + 128, 339 UID_UUCP, GID_DIALER, 0660, "cuam%d", chan + base); 340 cdev->si_drv1 = rc; 341 cdev->si_drv2 = (void *)1; 342 cdev->si_tty = tp; 343 rc->rc_cdev = cdev; 344 } 345 346 error = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_TTY, rc_intr, sc, 347 &sc->sc_hwicookie); 348 if (error) { 349 device_printf(dev, "failed to register interrupt handler\n"); 350 goto fail; 351 } 352 353 swi_add(&tty_ithd, "tty:rc", rc_pollcard, sc, SWI_TTY, 0, 354 &sc->sc_swicookie); 355 return (0); 356 357fail: 358 rc_release_resources(dev); 359 return (error); 360} 361 362static int 363rc_detach(device_t dev) 364{ 365 struct rc_softc *sc; 366 struct rc_chans *rc; 367 int error, i, s; 368 369 sc = device_get_softc(dev); 370 if (sc->sc_opencount > 0) 371 return (EBUSY); 372 sc->sc_opencount = -1; 373 374 rc = sc->sc_channels; 375 for (i = 0; i < CD180_NCHAN; i++, rc++) { 376 destroy_dev(rc->rc_dev); 377 destroy_dev(rc->rc_cdev); 378 } 379 380 rc = sc->sc_channels; 381 s = splsoftclock(); 382 for (i = 0; i < CD180_NCHAN; i++) { 383 if ((rc->rc_flags & RC_DTR_OFF) && 384 !callout_stop(&rc->rc_dtrcallout)) 385 tsleep(&rc->rc_dtrwait, TTIPRI, "rcdtrdet", 0); 386 } 387 388 error = bus_teardown_intr(dev, sc->sc_irq, sc->sc_hwicookie); 389 if (error) 390 device_printf(dev, "failed to deregister interrupt handler\n"); 391 ithread_remove_handler(sc->sc_swicookie); 392 rc_release_resources(dev); 393 394 return (0); 395} 396 397static void 398rc_release_resources(device_t dev) 399{ 400 struct rc_softc *sc; 401 int i; 402 403 sc = device_get_softc(dev); 404 if (sc->sc_irq != NULL) { 405 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, 406 sc->sc_irq); 407 sc->sc_irq = NULL; 408 } 409 for (i = 0; i < IOBASE_ADDRS; i++) { 410 if (sc->sc_port[i] == NULL) 411 break; 412 bus_release_resource(dev, SYS_RES_IOPORT, i, sc->sc_port[i]); 413 sc->sc_port[i] = NULL; 414 } 415} 416 417/* RC interrupt handling */ 418static void 419rc_intr(void *arg) 420{ 421 struct rc_softc *sc; 422 struct rc_chans *rc; 423 int resid, chan; 424 u_char val, iack, bsr, ucnt, *optr; 425 int good_data, t_state; 426 427 sc = (struct rc_softc *)arg; 428 bsr = ~(rcin(sc, RC_BSR)); 429 if (!(bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT))) { 430 device_printf(sc->sc_dev, "extra interrupt\n"); 431 rcout(sc, CD180_EOIR, 0); 432 return; 433 } 434 435 while (bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT)) { 436#ifdef RCDEBUG_DETAILED 437 device_printf(sc->sc_dev, "intr (%p) %s%s%s%s\n", arg, bsr, 438 (bsr & RC_BSR_TOUT)?"TOUT ":"", 439 (bsr & RC_BSR_RXINT)?"RXINT ":"", 440 (bsr & RC_BSR_TXINT)?"TXINT ":"", 441 (bsr & RC_BSR_MOINT)?"MOINT":""); 442#endif 443 if (bsr & RC_BSR_TOUT) { 444 device_printf(sc->sc_dev, 445 "hardware failure, reset board\n"); 446 rcout(sc, RC_CTOUT, 0); 447 rc_reinit(sc); 448 return; 449 } 450 if (bsr & RC_BSR_RXINT) { 451 iack = rcin(sc, RC_PILR_RX); 452 good_data = (iack == (GIVR_IT_RGDI | RC_FAKEID)); 453 if (!good_data && iack != (GIVR_IT_REI | RC_FAKEID)) { 454 device_printf(sc->sc_dev, 455 "fake rxint: %02x\n", iack); 456 goto more_intrs; 457 } 458 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 459 rc = &sc->sc_channels[chan]; 460 t_state = rc->rc_tp.t_state; 461 /* Do RTS flow control stuff */ 462 if ( (rc->rc_flags & RC_RTSFLOW) 463 || !(t_state & TS_ISOPEN) 464 ) { 465 if ( ( !(t_state & TS_ISOPEN) 466 || (t_state & TS_TBLOCK) 467 ) 468 && (rc->rc_msvr & MSVR_RTS) 469 ) 470 rcout(sc, CD180_MSVR, 471 rc->rc_msvr &= ~MSVR_RTS); 472 else if (!(rc->rc_msvr & MSVR_RTS)) 473 rcout(sc, CD180_MSVR, 474 rc->rc_msvr |= MSVR_RTS); 475 } 476 ucnt = rcin(sc, CD180_RDCR) & 0xF; 477 resid = 0; 478 479 if (t_state & TS_ISOPEN) { 480 /* check for input buffer overflow */ 481 if ((rc->rc_iptr + ucnt) >= rc->rc_bufend) { 482 resid = ucnt; 483 ucnt = rc->rc_bufend - rc->rc_iptr; 484 resid -= ucnt; 485 if (!(rc->rc_flags & RC_WAS_BUFOVFL)) { 486 rc->rc_flags |= RC_WAS_BUFOVFL; 487 sc->sc_scheduled_event++; 488 } 489 } 490 optr = rc->rc_iptr; 491 /* check foor good data */ 492 if (good_data) { 493 while (ucnt-- > 0) { 494 val = rcin(sc, CD180_RDR); 495 optr[0] = val; 496 optr[INPUT_FLAGS_SHIFT] = 0; 497 optr++; 498 sc->sc_scheduled_event++; 499 if (val != 0 && val == rc->rc_hotchar) 500 swi_sched(sc->sc_swicookie, 0); 501 } 502 } else { 503 /* Store also status data */ 504 while (ucnt-- > 0) { 505 iack = rcin(sc, CD180_RCSR); 506 if (iack & RCSR_Timeout) 507 break; 508 if ( (iack & RCSR_OE) 509 && !(rc->rc_flags & RC_WAS_SILOVFL)) { 510 rc->rc_flags |= RC_WAS_SILOVFL; 511 sc->sc_scheduled_event++; 512 } 513 val = rcin(sc, CD180_RDR); 514 /* 515 Don't store PE if IGNPAR and BREAK if IGNBRK, 516 this hack allows "raw" tty optimization 517 works even if IGN* is set. 518 */ 519 if ( !(iack & (RCSR_PE|RCSR_FE|RCSR_Break)) 520 || ((!(iack & (RCSR_PE|RCSR_FE)) 521 || !(rc->rc_tp.t_iflag & IGNPAR)) 522 && (!(iack & RCSR_Break) 523 || !(rc->rc_tp.t_iflag & IGNBRK)))) { 524 if ( (iack & (RCSR_PE|RCSR_FE)) 525 && (t_state & TS_CAN_BYPASS_L_RINT) 526 && ((iack & RCSR_FE) 527 || ((iack & RCSR_PE) 528 && (rc->rc_tp.t_iflag & INPCK)))) 529 val = 0; 530 else if (val != 0 && val == rc->rc_hotchar) 531 swi_sched(sc->sc_swicookie, 0); 532 optr[0] = val; 533 optr[INPUT_FLAGS_SHIFT] = iack; 534 optr++; 535 sc->sc_scheduled_event++; 536 } 537 } 538 } 539 rc->rc_iptr = optr; 540 rc->rc_flags |= RC_DORXFER; 541 } else 542 resid = ucnt; 543 /* Clear FIFO if necessary */ 544 while (resid-- > 0) { 545 if (!good_data) 546 iack = rcin(sc, CD180_RCSR); 547 else 548 iack = 0; 549 if (iack & RCSR_Timeout) 550 break; 551 (void) rcin(sc, CD180_RDR); 552 } 553 goto more_intrs; 554 } 555 if (bsr & RC_BSR_MOINT) { 556 iack = rcin(sc, RC_PILR_MODEM); 557 if (iack != (GIVR_IT_MSCI | RC_FAKEID)) { 558 device_printf(sc->sc_dev, "fake moint: %02x\n", 559 iack); 560 goto more_intrs; 561 } 562 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 563 rc = &sc->sc_channels[chan]; 564 iack = rcin(sc, CD180_MCR); 565 rc->rc_msvr = rcin(sc, CD180_MSVR); 566 rcout(sc, CD180_MCR, 0); 567#ifdef RCDEBUG 568 printrcflags(rc, "moint"); 569#endif 570 if (rc->rc_flags & RC_CTSFLOW) { 571 if (rc->rc_msvr & MSVR_CTS) 572 rc->rc_flags |= RC_SEND_RDY; 573 else 574 rc->rc_flags &= ~RC_SEND_RDY; 575 } else 576 rc->rc_flags |= RC_SEND_RDY; 577 if ((iack & MCR_CDchg) && !(rc->rc_flags & RC_MODCHG)) { 578 sc->sc_scheduled_event += LOTS_OF_EVENTS; 579 rc->rc_flags |= RC_MODCHG; 580 swi_sched(sc->sc_swicookie, 0); 581 } 582 goto more_intrs; 583 } 584 if (bsr & RC_BSR_TXINT) { 585 iack = rcin(sc, RC_PILR_TX); 586 if (iack != (GIVR_IT_TDI | RC_FAKEID)) { 587 device_printf(sc->sc_dev, "fake txint: %02x\n", 588 iack); 589 goto more_intrs; 590 } 591 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 592 rc = &sc->sc_channels[chan]; 593 if ( (rc->rc_flags & RC_OSUSP) 594 || !(rc->rc_flags & RC_SEND_RDY) 595 ) 596 goto more_intrs; 597 /* Handle breaks and other stuff */ 598 if (rc->rc_pendcmd) { 599 rcout(sc, CD180_COR2, rc->rc_cor2 |= COR2_ETC); 600 rcout(sc, CD180_TDR, CD180_C_ESC); 601 rcout(sc, CD180_TDR, rc->rc_pendcmd); 602 rcout(sc, CD180_COR2, rc->rc_cor2 &= ~COR2_ETC); 603 rc->rc_pendcmd = 0; 604 goto more_intrs; 605 } 606 optr = rc->rc_optr; 607 resid = rc->rc_obufend - optr; 608 if (resid > CD180_NFIFO) 609 resid = CD180_NFIFO; 610 while (resid-- > 0) 611 rcout(sc, CD180_TDR, *optr++); 612 rc->rc_optr = optr; 613 614 /* output completed? */ 615 if (optr >= rc->rc_obufend) { 616 rcout(sc, CD180_IER, rc->rc_ier &= ~IER_TxRdy); 617#ifdef RCDEBUG 618 device_printf(sc->sc_dev, 619 "channel %d: output completed\n", 620 rc->rc_chan); 621#endif 622 if (!(rc->rc_flags & RC_DOXXFER)) { 623 sc->sc_scheduled_event += LOTS_OF_EVENTS; 624 rc->rc_flags |= RC_DOXXFER; 625 swi_sched(sc->sc_swicookie, 0); 626 } 627 } 628 } 629 more_intrs: 630 rcout(sc, CD180_EOIR, 0); /* end of interrupt */ 631 rcout(sc, RC_CTOUT, 0); 632 bsr = ~(rcin(sc, RC_BSR)); 633 } 634} 635 636/* Feed characters to output buffer */ 637static void 638rc_start(struct tty *tp) 639{ 640 struct rc_softc *sc; 641 struct rc_chans *rc; 642 int s; 643 644 rc = TTY_TO_RC(tp); 645 if (rc->rc_flags & RC_OSBUSY) 646 return; 647 sc = rc->rc_rcb; 648 s = spltty(); 649 rc->rc_flags |= RC_OSBUSY; 650 critical_enter(); 651 if (tp->t_state & TS_TTSTOP) 652 rc->rc_flags |= RC_OSUSP; 653 else 654 rc->rc_flags &= ~RC_OSUSP; 655 /* Do RTS flow control stuff */ 656 if ( (rc->rc_flags & RC_RTSFLOW) 657 && (tp->t_state & TS_TBLOCK) 658 && (rc->rc_msvr & MSVR_RTS) 659 ) { 660 rcout(sc, CD180_CAR, rc->rc_chan); 661 rcout(sc, CD180_MSVR, rc->rc_msvr &= ~MSVR_RTS); 662 } else if (!(rc->rc_msvr & MSVR_RTS)) { 663 rcout(sc, CD180_CAR, rc->rc_chan); 664 rcout(sc, CD180_MSVR, rc->rc_msvr |= MSVR_RTS); 665 } 666 critical_exit(); 667 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP)) 668 goto out; 669#ifdef RCDEBUG 670 printrcflags(rc, "rcstart"); 671#endif 672 ttwwakeup(tp); 673#ifdef RCDEBUG 674 printf("rcstart: outq = %d obuf = %d\n", 675 tp->t_outq.c_cc, rc->rc_obufend - rc->rc_optr); 676#endif 677 if (tp->t_state & TS_BUSY) 678 goto out; /* output still in progress ... */ 679 680 if (tp->t_outq.c_cc > 0) { 681 u_int ocnt; 682 683 tp->t_state |= TS_BUSY; 684 ocnt = q_to_b(&tp->t_outq, rc->rc_obuf, sizeof rc->rc_obuf); 685 critical_enter(); 686 rc->rc_optr = rc->rc_obuf; 687 rc->rc_obufend = rc->rc_optr + ocnt; 688 critical_exit(); 689 if (!(rc->rc_ier & IER_TxRdy)) { 690#ifdef RCDEBUG 691 device_printf(sc->sc_dev, 692 "channel %d: rcstart enable txint\n", rc->rc_chan); 693#endif 694 rcout(sc, CD180_CAR, rc->rc_chan); 695 rcout(sc, CD180_IER, rc->rc_ier |= IER_TxRdy); 696 } 697 } 698out: 699 rc->rc_flags &= ~RC_OSBUSY; 700 (void) splx(s); 701} 702 703/* Handle delayed events. */ 704void 705rc_pollcard(void *arg) 706{ 707 struct rc_softc *sc; 708 struct rc_chans *rc; 709 struct tty *tp; 710 u_char *tptr, *eptr; 711 int chan, icnt; 712 713 sc = (struct rc_softc *)arg; 714 if (sc->sc_scheduled_event == 0) 715 return; 716 do { 717 rc = sc->sc_channels; 718 for (chan = 0; chan < CD180_NCHAN; rc++, chan++) { 719 tp = &rc->rc_tp; 720#ifdef RCDEBUG 721 if (rc->rc_flags & (RC_DORXFER|RC_DOXXFER|RC_MODCHG| 722 RC_WAS_BUFOVFL|RC_WAS_SILOVFL)) 723 printrcflags(rc, "rcevent"); 724#endif 725 if (rc->rc_flags & RC_WAS_BUFOVFL) { 726 critical_enter(); 727 rc->rc_flags &= ~RC_WAS_BUFOVFL; 728 sc->sc_scheduled_event--; 729 critical_exit(); 730 device_printf(sc->sc_dev, 731 "channel %d: interrupt-level buffer overflow\n", 732 chan); 733 } 734 if (rc->rc_flags & RC_WAS_SILOVFL) { 735 critical_enter(); 736 rc->rc_flags &= ~RC_WAS_SILOVFL; 737 sc->sc_scheduled_event--; 738 critical_exit(); 739 device_printf(sc->sc_dev, 740 "channel %d: silo overflow\n", chan); 741 } 742 if (rc->rc_flags & RC_MODCHG) { 743 critical_enter(); 744 rc->rc_flags &= ~RC_MODCHG; 745 sc->sc_scheduled_event -= LOTS_OF_EVENTS; 746 critical_exit(); 747 (*linesw[tp->t_line].l_modem)(tp, !!(rc->rc_msvr & MSVR_CD)); 748 } 749 if (rc->rc_flags & RC_DORXFER) { 750 critical_enter(); 751 rc->rc_flags &= ~RC_DORXFER; 752 eptr = rc->rc_iptr; 753 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) 754 tptr = &rc->rc_ibuf[RC_IBUFSIZE]; 755 else 756 tptr = rc->rc_ibuf; 757 icnt = eptr - tptr; 758 if (icnt > 0) { 759 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) { 760 rc->rc_iptr = rc->rc_ibuf; 761 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE]; 762 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER]; 763 } else { 764 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE]; 765 rc->rc_bufend = &rc->rc_ibuf[2 * RC_IBUFSIZE]; 766 rc->rc_hiwat = 767 &rc->rc_ibuf[RC_IBUFSIZE + RC_IHIGHWATER]; 768 } 769 if ( (rc->rc_flags & RC_RTSFLOW) 770 && (tp->t_state & TS_ISOPEN) 771 && !(tp->t_state & TS_TBLOCK) 772 && !(rc->rc_msvr & MSVR_RTS) 773 ) { 774 rcout(sc, CD180_CAR, chan); 775 rcout(sc, CD180_MSVR, 776 rc->rc_msvr |= MSVR_RTS); 777 } 778 sc->sc_scheduled_event -= icnt; 779 } 780 critical_exit(); 781 782 if (icnt <= 0 || !(tp->t_state & TS_ISOPEN)) 783 goto done1; 784 785 if ( (tp->t_state & TS_CAN_BYPASS_L_RINT) 786 && !(tp->t_state & TS_LOCAL)) { 787 if ((tp->t_rawq.c_cc + icnt) >= RB_I_HIGH_WATER 788 && ((rc->rc_flags & RC_RTSFLOW) || (tp->t_iflag & IXOFF)) 789 && !(tp->t_state & TS_TBLOCK)) 790 ttyblock(tp); 791 tk_nin += icnt; 792 tk_rawcc += icnt; 793 tp->t_rawcc += icnt; 794 if (b_to_q(tptr, icnt, &tp->t_rawq)) 795 device_printf(sc->sc_dev, 796 "channel %d: tty-level buffer overflow\n", 797 chan); 798 ttwakeup(tp); 799 if ((tp->t_state & TS_TTSTOP) && ((tp->t_iflag & IXANY) 800 || (tp->t_cc[VSTART] == tp->t_cc[VSTOP]))) { 801 tp->t_state &= ~TS_TTSTOP; 802 tp->t_lflag &= ~FLUSHO; 803 rc_start(tp); 804 } 805 } else { 806 for (; tptr < eptr; tptr++) 807 (*linesw[tp->t_line].l_rint) 808 (tptr[0] | 809 rc_rcsrt[tptr[INPUT_FLAGS_SHIFT] & 0xF], tp); 810 } 811done1: ; 812 } 813 if (rc->rc_flags & RC_DOXXFER) { 814 critical_enter(); 815 sc->sc_scheduled_event -= LOTS_OF_EVENTS; 816 rc->rc_flags &= ~RC_DOXXFER; 817 rc->rc_tp.t_state &= ~TS_BUSY; 818 critical_exit(); 819 (*linesw[tp->t_line].l_start)(tp); 820 } 821 if (sc->sc_scheduled_event == 0) 822 break; 823 } 824 } while (sc->sc_scheduled_event >= LOTS_OF_EVENTS); 825} 826 827static void 828rc_stop(struct tty *tp, int rw) 829{ 830 struct rc_softc *sc; 831 struct rc_chans *rc; 832 u_char *tptr, *eptr; 833 834 rc = TTY_TO_RC(tp); 835 sc = rc->rc_rcb; 836#ifdef RCDEBUG 837 device_printf(sc->sc_dev, "channel %d: rc_stop %s%s\n", 838 rc->rc_chan, (rw & FWRITE)?"FWRITE ":"", (rw & FREAD)?"FREAD":""); 839#endif 840 if (rw & FWRITE) 841 rc_discard_output(rc); 842 critical_enter(); 843 if (rw & FREAD) { 844 rc->rc_flags &= ~RC_DORXFER; 845 eptr = rc->rc_iptr; 846 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) { 847 tptr = &rc->rc_ibuf[RC_IBUFSIZE]; 848 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE]; 849 } else { 850 tptr = rc->rc_ibuf; 851 rc->rc_iptr = rc->rc_ibuf; 852 } 853 sc->sc_scheduled_event -= eptr - tptr; 854 } 855 if (tp->t_state & TS_TTSTOP) 856 rc->rc_flags |= RC_OSUSP; 857 else 858 rc->rc_flags &= ~RC_OSUSP; 859 critical_exit(); 860} 861 862static int 863rcopen(dev_t dev, int flag, int mode, d_thread_t *td) 864{ 865 struct rc_softc *sc; 866 struct rc_chans *rc; 867 struct tty *tp; 868 int s, error = 0; 869 870 rc = DEV_TO_RC(dev); 871 sc = rc->rc_rcb; 872 tp = &rc->rc_tp; 873 if (sc->sc_opencount < 0) 874 return (ENXIO); 875 sc->sc_opencount++; 876#ifdef RCDEBUG 877 device_printf(sc->sc_dev, "channel %d: rcopen: dev %p\n", 878 rc->rc_chan, dev); 879#endif 880 s = spltty(); 881 882again: 883 while (rc->rc_flags & RC_DTR_OFF) { 884 error = tsleep(&(rc->rc_dtrwait), TTIPRI | PCATCH, "rcdtr", 0); 885 if (error != 0) 886 goto out; 887 } 888 if (tp->t_state & TS_ISOPEN) { 889 if (CALLOUT(dev)) { 890 if (!(rc->rc_flags & RC_ACTOUT)) { 891 error = EBUSY; 892 goto out; 893 } 894 } else { 895 if (rc->rc_flags & RC_ACTOUT) { 896 if (flag & O_NONBLOCK) { 897 error = EBUSY; 898 goto out; 899 } 900 error = tsleep(&rc->rc_rcb, 901 TTIPRI|PCATCH, "rcbi", 0); 902 if (error) 903 goto out; 904 goto again; 905 } 906 } 907 if (tp->t_state & TS_XCLUDE && 908 suser(td)) { 909 error = EBUSY; 910 goto out; 911 } 912 } else { 913 tp->t_oproc = rc_start; 914 tp->t_param = rc_param; 915 tp->t_stop = rc_stop; 916 tp->t_dev = dev; 917 918 if (CALLOUT(dev)) 919 tp->t_cflag |= CLOCAL; 920 else 921 tp->t_cflag &= ~CLOCAL; 922 923 error = rc_param(tp, &tp->t_termios); 924 if (error) 925 goto out; 926 (void) rc_modctl(rc, TIOCM_RTS|TIOCM_DTR, DMSET); 927 928 if ((rc->rc_msvr & MSVR_CD) || CALLOUT(dev)) 929 (*linesw[tp->t_line].l_modem)(tp, 1); 930 } 931 if (!(tp->t_state & TS_CARR_ON) && !CALLOUT(dev) 932 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 933 rc->rc_dcdwaits++; 934 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "rcdcd", 0); 935 rc->rc_dcdwaits--; 936 if (error != 0) 937 goto out; 938 goto again; 939 } 940 error = (*linesw[tp->t_line].l_open)(dev, tp); 941 disc_optim(tp, &tp->t_termios, rc); 942 if ((tp->t_state & TS_ISOPEN) && CALLOUT(dev)) 943 rc->rc_flags |= RC_ACTOUT; 944out: 945 (void) splx(s); 946 947 if(rc->rc_dcdwaits == 0 && !(tp->t_state & TS_ISOPEN)) 948 rc_hardclose(rc); 949 950 return error; 951} 952 953static int 954rcclose(dev_t dev, int flag, int mode, d_thread_t *td) 955{ 956 struct rc_softc *sc; 957 struct rc_chans *rc; 958 struct tty *tp; 959 int s; 960 961 rc = DEV_TO_RC(dev); 962 sc = rc->rc_rcb; 963 tp = &rc->rc_tp; 964#ifdef RCDEBUG 965 device_printf(sc->sc_dev, "channel %d: rcclose dev %p\n", 966 rc->rc_chan, dev); 967#endif 968 s = spltty(); 969 (*linesw[tp->t_line].l_close)(tp, flag); 970 disc_optim(tp, &tp->t_termios, rc); 971 rc_stop(tp, FREAD | FWRITE); 972 rc_hardclose(rc); 973 ttyclose(tp); 974 splx(s); 975 KASSERT(sc->sc_opencount > 0, ("rcclose: non-positive open count")); 976 sc->sc_opencount--; 977 return 0; 978} 979 980static void 981rc_hardclose(struct rc_chans *rc) 982{ 983 struct rc_softc *sc; 984 struct tty *tp; 985 int s; 986 987 tp = &rc->rc_tp; 988 sc = rc->rc_rcb; 989 s = spltty(); 990 rcout(sc, CD180_CAR, rc->rc_chan); 991 992 /* Disable rx/tx intrs */ 993 rcout(sc, CD180_IER, rc->rc_ier = 0); 994 if ( (tp->t_cflag & HUPCL) 995 || (!(rc->rc_flags & RC_ACTOUT) 996 && !(rc->rc_msvr & MSVR_CD) 997 && !(tp->t_cflag & CLOCAL)) 998 || !(tp->t_state & TS_ISOPEN) 999 ) { 1000 CCRCMD(sc, rc->rc_chan, CCR_ResetChan); 1001 WAITFORCCR(sc, rc->rc_chan); 1002 (void) rc_modctl(rc, TIOCM_RTS, DMSET); 1003 if (rc->rc_dtrwait) { 1004 callout_reset(&rc->rc_dtrcallout, rc->rc_dtrwait, 1005 rc_dtrwakeup, rc); 1006 rc->rc_flags |= RC_DTR_OFF; 1007 } 1008 } 1009 rc->rc_flags &= ~RC_ACTOUT; 1010 wakeup( &rc->rc_rcb); /* wake bi */ 1011 wakeup(TSA_CARR_ON(tp)); 1012 (void) splx(s); 1013} 1014 1015/* Reset the bastard */ 1016static void 1017rc_hwreset(struct rc_softc *sc, uint chipid) 1018{ 1019 CCRCMD(sc, -1, CCR_HWRESET); /* Hardware reset */ 1020 DELAY(20000); 1021 WAITFORCCR(sc, -1); 1022 1023 rcout(sc, RC_CTOUT, 0); /* Clear timeout */ 1024 rcout(sc, CD180_GIVR, chipid); 1025 rcout(sc, CD180_GICR, 0); 1026 1027 /* Set Prescaler Registers (1 msec) */ 1028 rcout(sc, CD180_PPRL, ((RC_OSCFREQ + 999) / 1000) & 0xFF); 1029 rcout(sc, CD180_PPRH, ((RC_OSCFREQ + 999) / 1000) >> 8); 1030 1031 /* Initialize Priority Interrupt Level Registers */ 1032 rcout(sc, CD180_PILR1, RC_PILR_MODEM); 1033 rcout(sc, CD180_PILR2, RC_PILR_TX); 1034 rcout(sc, CD180_PILR3, RC_PILR_RX); 1035 1036 /* Reset DTR */ 1037 rcout(sc, RC_DTREG, ~0); 1038} 1039 1040/* Set channel parameters */ 1041static int 1042rc_param(struct tty *tp, struct termios *ts) 1043{ 1044 struct rc_softc *sc; 1045 struct rc_chans *rc; 1046 int idivs, odivs, s, val, cflag, iflag, lflag, inpflow; 1047 1048 if ( ts->c_ospeed < 0 || ts->c_ospeed > 76800 1049 || ts->c_ispeed < 0 || ts->c_ispeed > 76800 1050 ) 1051 return (EINVAL); 1052 if (ts->c_ispeed == 0) 1053 ts->c_ispeed = ts->c_ospeed; 1054 odivs = RC_BRD(ts->c_ospeed); 1055 idivs = RC_BRD(ts->c_ispeed); 1056 1057 rc = TTY_TO_RC(tp); 1058 sc = rc->rc_rcb; 1059 s = spltty(); 1060 1061 /* Select channel */ 1062 rcout(sc, CD180_CAR, rc->rc_chan); 1063 1064 /* If speed == 0, hangup line */ 1065 if (ts->c_ospeed == 0) { 1066 CCRCMD(sc, rc->rc_chan, CCR_ResetChan); 1067 WAITFORCCR(sc, rc->rc_chan); 1068 (void) rc_modctl(rc, TIOCM_DTR, DMBIC); 1069 } 1070 1071 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 1072 cflag = ts->c_cflag; 1073 iflag = ts->c_iflag; 1074 lflag = ts->c_lflag; 1075 1076 if (idivs > 0) { 1077 rcout(sc, CD180_RBPRL, idivs & 0xFF); 1078 rcout(sc, CD180_RBPRH, idivs >> 8); 1079 } 1080 if (odivs > 0) { 1081 rcout(sc, CD180_TBPRL, odivs & 0xFF); 1082 rcout(sc, CD180_TBPRH, odivs >> 8); 1083 } 1084 1085 /* set timeout value */ 1086 if (ts->c_ispeed > 0) { 1087 int itm = ts->c_ispeed > 2400 ? 5 : 10000 / ts->c_ispeed + 1; 1088 1089 if ( !(lflag & ICANON) 1090 && ts->c_cc[VMIN] != 0 && ts->c_cc[VTIME] != 0 1091 && ts->c_cc[VTIME] * 10 > itm) 1092 itm = ts->c_cc[VTIME] * 10; 1093 1094 rcout(sc, CD180_RTPR, itm <= 255 ? itm : 255); 1095 } 1096 1097 switch (cflag & CSIZE) { 1098 case CS5: val = COR1_5BITS; break; 1099 case CS6: val = COR1_6BITS; break; 1100 case CS7: val = COR1_7BITS; break; 1101 default: 1102 case CS8: val = COR1_8BITS; break; 1103 } 1104 if (cflag & PARENB) { 1105 val |= COR1_NORMPAR; 1106 if (cflag & PARODD) 1107 val |= COR1_ODDP; 1108 if (!(cflag & INPCK)) 1109 val |= COR1_Ignore; 1110 } else 1111 val |= COR1_Ignore; 1112 if (cflag & CSTOPB) 1113 val |= COR1_2SB; 1114 rcout(sc, CD180_COR1, val); 1115 1116 /* Set FIFO threshold */ 1117 val = ts->c_ospeed <= 4800 ? 1 : CD180_NFIFO / 2; 1118 inpflow = 0; 1119 if ( (iflag & IXOFF) 1120 && ( ts->c_cc[VSTOP] != _POSIX_VDISABLE 1121 && ( ts->c_cc[VSTART] != _POSIX_VDISABLE 1122 || (iflag & IXANY) 1123 ) 1124 ) 1125 ) { 1126 inpflow = 1; 1127 val |= COR3_SCDE|COR3_FCT; 1128 } 1129 rcout(sc, CD180_COR3, val); 1130 1131 /* Initialize on-chip automatic flow control */ 1132 val = 0; 1133 rc->rc_flags &= ~(RC_CTSFLOW|RC_SEND_RDY); 1134 if (cflag & CCTS_OFLOW) { 1135 rc->rc_flags |= RC_CTSFLOW; 1136 val |= COR2_CtsAE; 1137 } else 1138 rc->rc_flags |= RC_SEND_RDY; 1139 if (tp->t_state & TS_TTSTOP) 1140 rc->rc_flags |= RC_OSUSP; 1141 else 1142 rc->rc_flags &= ~RC_OSUSP; 1143 if (cflag & CRTS_IFLOW) 1144 rc->rc_flags |= RC_RTSFLOW; 1145 else 1146 rc->rc_flags &= ~RC_RTSFLOW; 1147 1148 if (inpflow) { 1149 if (ts->c_cc[VSTART] != _POSIX_VDISABLE) 1150 rcout(sc, CD180_SCHR1, ts->c_cc[VSTART]); 1151 rcout(sc, CD180_SCHR2, ts->c_cc[VSTOP]); 1152 val |= COR2_TxIBE; 1153 if (iflag & IXANY) 1154 val |= COR2_IXM; 1155 } 1156 1157 rcout(sc, CD180_COR2, rc->rc_cor2 = val); 1158 1159 CCRCMD(sc, rc->rc_chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3); 1160 1161 disc_optim(tp, ts, rc); 1162 1163 /* modem ctl */ 1164 val = cflag & CLOCAL ? 0 : MCOR1_CDzd; 1165 if (cflag & CCTS_OFLOW) 1166 val |= MCOR1_CTSzd; 1167 rcout(sc, CD180_MCOR1, val); 1168 1169 val = cflag & CLOCAL ? 0 : MCOR2_CDod; 1170 if (cflag & CCTS_OFLOW) 1171 val |= MCOR2_CTSod; 1172 rcout(sc, CD180_MCOR2, val); 1173 1174 /* enable i/o and interrupts */ 1175 CCRCMD(sc, rc->rc_chan, 1176 CCR_XMTREN | ((cflag & CREAD) ? CCR_RCVREN : CCR_RCVRDIS)); 1177 WAITFORCCR(sc, rc->rc_chan); 1178 1179 rc->rc_ier = cflag & CLOCAL ? 0 : IER_CD; 1180 if (cflag & CCTS_OFLOW) 1181 rc->rc_ier |= IER_CTS; 1182 if (cflag & CREAD) 1183 rc->rc_ier |= IER_RxData; 1184 if (tp->t_state & TS_BUSY) 1185 rc->rc_ier |= IER_TxRdy; 1186 if (ts->c_ospeed != 0) 1187 rc_modctl(rc, TIOCM_DTR, DMBIS); 1188 if ((cflag & CCTS_OFLOW) && (rc->rc_msvr & MSVR_CTS)) 1189 rc->rc_flags |= RC_SEND_RDY; 1190 rcout(sc, CD180_IER, rc->rc_ier); 1191 (void) splx(s); 1192 return 0; 1193} 1194 1195/* Re-initialize board after bogus interrupts */ 1196static void 1197rc_reinit(struct rc_softc *sc) 1198{ 1199 struct rc_chans *rc; 1200 int i; 1201 1202 rc_hwreset(sc, RC_FAKEID); 1203 rc = sc->sc_channels; 1204 for (i = 0; i < CD180_NCHAN; i++, rc++) 1205 (void) rc_param(&rc->rc_tp, &rc->rc_tp.t_termios); 1206} 1207 1208static int 1209rcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td) 1210{ 1211 struct rc_chans *rc; 1212 struct tty *tp; 1213 int s, error; 1214 1215 rc = DEV_TO_RC(dev); 1216 tp = &rc->rc_tp; 1217 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 1218 if (error != ENOIOCTL) 1219 return (error); 1220 error = ttioctl(tp, cmd, data, flag); 1221 disc_optim(tp, &tp->t_termios, rc); 1222 if (error != ENOIOCTL) 1223 return (error); 1224 s = spltty(); 1225 1226 switch (cmd) { 1227 case TIOCSBRK: 1228 rc->rc_pendcmd = CD180_C_SBRK; 1229 break; 1230 1231 case TIOCCBRK: 1232 rc->rc_pendcmd = CD180_C_EBRK; 1233 break; 1234 1235 case TIOCSDTR: 1236 (void) rc_modctl(rc, TIOCM_DTR, DMBIS); 1237 break; 1238 1239 case TIOCCDTR: 1240 (void) rc_modctl(rc, TIOCM_DTR, DMBIC); 1241 break; 1242 1243 case TIOCMGET: 1244 *(int *) data = rc_modctl(rc, 0, DMGET); 1245 break; 1246 1247 case TIOCMSET: 1248 (void) rc_modctl(rc, *(int *) data, DMSET); 1249 break; 1250 1251 case TIOCMBIC: 1252 (void) rc_modctl(rc, *(int *) data, DMBIC); 1253 break; 1254 1255 case TIOCMBIS: 1256 (void) rc_modctl(rc, *(int *) data, DMBIS); 1257 break; 1258 1259 case TIOCMSDTRWAIT: 1260 error = suser(td); 1261 if (error != 0) { 1262 splx(s); 1263 return (error); 1264 } 1265 rc->rc_dtrwait = *(int *)data * hz / 100; 1266 break; 1267 1268 case TIOCMGDTRWAIT: 1269 *(int *)data = rc->rc_dtrwait * 100 / hz; 1270 break; 1271 1272 default: 1273 (void) splx(s); 1274 return ENOTTY; 1275 } 1276 (void) splx(s); 1277 return 0; 1278} 1279 1280 1281/* Modem control routines */ 1282 1283static int 1284rc_modctl(struct rc_chans *rc, int bits, int cmd) 1285{ 1286 struct rc_softc *sc; 1287 u_char *dtr; 1288 u_char msvr; 1289 1290 sc = rc->rc_rcb; 1291 dtr = &sc->sc_dtr; 1292 rcout(sc, CD180_CAR, rc->rc_chan); 1293 1294 switch (cmd) { 1295 case DMSET: 1296 rcout(sc, RC_DTREG, (bits & TIOCM_DTR) ? 1297 ~(*dtr |= 1 << rc->rc_chan) : 1298 ~(*dtr &= ~(1 << rc->rc_chan))); 1299 msvr = rcin(sc, CD180_MSVR); 1300 if (bits & TIOCM_RTS) 1301 msvr |= MSVR_RTS; 1302 else 1303 msvr &= ~MSVR_RTS; 1304 if (bits & TIOCM_DTR) 1305 msvr |= MSVR_DTR; 1306 else 1307 msvr &= ~MSVR_DTR; 1308 rcout(sc, CD180_MSVR, msvr); 1309 break; 1310 1311 case DMBIS: 1312 if (bits & TIOCM_DTR) 1313 rcout(sc, RC_DTREG, ~(*dtr |= 1 << rc->rc_chan)); 1314 msvr = rcin(sc, CD180_MSVR); 1315 if (bits & TIOCM_RTS) 1316 msvr |= MSVR_RTS; 1317 if (bits & TIOCM_DTR) 1318 msvr |= MSVR_DTR; 1319 rcout(sc, CD180_MSVR, msvr); 1320 break; 1321 1322 case DMGET: 1323 bits = TIOCM_LE; 1324 msvr = rc->rc_msvr = rcin(sc, CD180_MSVR); 1325 1326 if (msvr & MSVR_RTS) 1327 bits |= TIOCM_RTS; 1328 if (msvr & MSVR_CTS) 1329 bits |= TIOCM_CTS; 1330 if (msvr & MSVR_DSR) 1331 bits |= TIOCM_DSR; 1332 if (msvr & MSVR_DTR) 1333 bits |= TIOCM_DTR; 1334 if (msvr & MSVR_CD) 1335 bits |= TIOCM_CD; 1336 if (~rcin(sc, RC_RIREG) & (1 << rc->rc_chan)) 1337 bits |= TIOCM_RI; 1338 return bits; 1339 1340 case DMBIC: 1341 if (bits & TIOCM_DTR) 1342 rcout(sc, RC_DTREG, ~(*dtr &= ~(1 << rc->rc_chan))); 1343 msvr = rcin(sc, CD180_MSVR); 1344 if (bits & TIOCM_RTS) 1345 msvr &= ~MSVR_RTS; 1346 if (bits & TIOCM_DTR) 1347 msvr &= ~MSVR_DTR; 1348 rcout(sc, CD180_MSVR, msvr); 1349 break; 1350 } 1351 rc->rc_msvr = rcin(sc, CD180_MSVR); 1352 return 0; 1353} 1354 1355#define ERR(s) do { \ 1356 device_printf(sc->sc_dev, "%s", ""); \ 1357 printf s ; \ 1358 printf("\n"); \ 1359 (void) splx(old_level); \ 1360 return 1; \ 1361} while (0) 1362 1363/* Test the board. */ 1364int 1365rc_test(struct rc_softc *sc) 1366{ 1367 int chan = 0; 1368 int i = 0, rcnt, old_level; 1369 unsigned int iack, chipid; 1370 unsigned short divs; 1371 static u_char ctest[] = "\377\125\252\045\244\0\377"; 1372#define CTLEN 8 1373 1374 struct rtest { 1375 u_char txbuf[CD180_NFIFO]; /* TX buffer */ 1376 u_char rxbuf[CD180_NFIFO]; /* RX buffer */ 1377 int rxptr; /* RX pointer */ 1378 int txptr; /* TX pointer */ 1379 } tchans[CD180_NCHAN]; 1380 1381 old_level = spltty(); 1382 1383 chipid = RC_FAKEID; 1384 1385 /* First, reset board to inital state */ 1386 rc_hwreset(sc, chipid); 1387 1388 divs = RC_BRD(19200); 1389 1390 /* Initialize channels */ 1391 for (chan = 0; chan < CD180_NCHAN; chan++) { 1392 1393 /* Select and reset channel */ 1394 rcout(sc, CD180_CAR, chan); 1395 CCRCMD(sc, chan, CCR_ResetChan); 1396 WAITFORCCR(sc, chan); 1397 1398 /* Set speed */ 1399 rcout(sc, CD180_RBPRL, divs & 0xFF); 1400 rcout(sc, CD180_RBPRH, divs >> 8); 1401 rcout(sc, CD180_TBPRL, divs & 0xFF); 1402 rcout(sc, CD180_TBPRH, divs >> 8); 1403 1404 /* set timeout value */ 1405 rcout(sc, CD180_RTPR, 0); 1406 1407 /* Establish local loopback */ 1408 rcout(sc, CD180_COR1, COR1_NOPAR | COR1_8BITS | COR1_1SB); 1409 rcout(sc, CD180_COR2, COR2_LLM); 1410 rcout(sc, CD180_COR3, CD180_NFIFO); 1411 CCRCMD(sc, chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3); 1412 CCRCMD(sc, chan, CCR_RCVREN | CCR_XMTREN); 1413 WAITFORCCR(sc, chan); 1414 rcout(sc, CD180_MSVR, MSVR_RTS); 1415 1416 /* Fill TXBUF with test data */ 1417 for (i = 0; i < CD180_NFIFO; i++) { 1418 tchans[chan].txbuf[i] = ctest[i]; 1419 tchans[chan].rxbuf[i] = 0; 1420 } 1421 tchans[chan].txptr = tchans[chan].rxptr = 0; 1422 1423 /* Now, start transmit */ 1424 rcout(sc, CD180_IER, IER_TxMpty|IER_RxData); 1425 } 1426 /* Pseudo-interrupt poll stuff */ 1427 for (rcnt = 10000; rcnt-- > 0; rcnt--) { 1428 i = ~(rcin(sc, RC_BSR)); 1429 if (i & RC_BSR_TOUT) 1430 ERR(("BSR timeout bit set\n")); 1431 else if (i & RC_BSR_TXINT) { 1432 iack = rcin(sc, RC_PILR_TX); 1433 if (iack != (GIVR_IT_TDI | chipid)) 1434 ERR(("Bad TX intr ack (%02x != %02x)\n", 1435 iack, GIVR_IT_TDI | chipid)); 1436 chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH; 1437 /* If no more data to transmit, disable TX intr */ 1438 if (tchans[chan].txptr >= CD180_NFIFO) { 1439 iack = rcin(sc, CD180_IER); 1440 rcout(sc, CD180_IER, iack & ~IER_TxMpty); 1441 } else { 1442 for (iack = tchans[chan].txptr; 1443 iack < CD180_NFIFO; iack++) 1444 rcout(sc, CD180_TDR, 1445 tchans[chan].txbuf[iack]); 1446 tchans[chan].txptr = iack; 1447 } 1448 rcout(sc, CD180_EOIR, 0); 1449 } else if (i & RC_BSR_RXINT) { 1450 u_char ucnt; 1451 1452 iack = rcin(sc, RC_PILR_RX); 1453 if (iack != (GIVR_IT_RGDI | chipid) && 1454 iack != (GIVR_IT_REI | chipid)) 1455 ERR(("Bad RX intr ack (%02x != %02x)\n", 1456 iack, GIVR_IT_RGDI | chipid)); 1457 chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH; 1458 ucnt = rcin(sc, CD180_RDCR) & 0xF; 1459 while (ucnt-- > 0) { 1460 iack = rcin(sc, CD180_RCSR); 1461 if (iack & RCSR_Timeout) 1462 break; 1463 if (iack & 0xF) 1464 ERR(("Bad char chan %d (RCSR = %02X)\n", 1465 chan, iack)); 1466 if (tchans[chan].rxptr > CD180_NFIFO) 1467 ERR(("Got extra chars chan %d\n", 1468 chan)); 1469 tchans[chan].rxbuf[tchans[chan].rxptr++] = 1470 rcin(sc, CD180_RDR); 1471 } 1472 rcout(sc, CD180_EOIR, 0); 1473 } 1474 rcout(sc, RC_CTOUT, 0); 1475 for (iack = chan = 0; chan < CD180_NCHAN; chan++) 1476 if (tchans[chan].rxptr >= CD180_NFIFO) 1477 iack++; 1478 if (iack == CD180_NCHAN) 1479 break; 1480 } 1481 for (chan = 0; chan < CD180_NCHAN; chan++) { 1482 /* Select and reset channel */ 1483 rcout(sc, CD180_CAR, chan); 1484 CCRCMD(sc, chan, CCR_ResetChan); 1485 } 1486 1487 if (!rcnt) 1488 ERR(("looses characters during local loopback\n")); 1489 /* Now, check data */ 1490 for (chan = 0; chan < CD180_NCHAN; chan++) 1491 for (i = 0; i < CD180_NFIFO; i++) 1492 if (ctest[i] != tchans[chan].rxbuf[i]) 1493 ERR(("data mismatch chan %d ptr %d (%d != %d)\n", 1494 chan, i, ctest[i], tchans[chan].rxbuf[i])); 1495 (void) splx(old_level); 1496 return 0; 1497} 1498 1499#ifdef RCDEBUG 1500static void 1501printrcflags(struct rc_chans *rc, char *comment) 1502{ 1503 struct rc_softc *sc; 1504 u_short f = rc->rc_flags; 1505 1506 sc = rc->rc_rcb; 1507 printf("rc%d/%d: %s flags: %s%s%s%s%s%s%s%s%s%s%s%s\n", 1508 rc->rc_rcb->rcb_unit, rc->rc_chan, comment, 1509 (f & RC_DTR_OFF)?"DTR_OFF " :"", 1510 (f & RC_ACTOUT) ?"ACTOUT " :"", 1511 (f & RC_RTSFLOW)?"RTSFLOW " :"", 1512 (f & RC_CTSFLOW)?"CTSFLOW " :"", 1513 (f & RC_DORXFER)?"DORXFER " :"", 1514 (f & RC_DOXXFER)?"DOXXFER " :"", 1515 (f & RC_MODCHG) ?"MODCHG " :"", 1516 (f & RC_OSUSP) ?"OSUSP " :"", 1517 (f & RC_OSBUSY) ?"OSBUSY " :"", 1518 (f & RC_WAS_BUFOVFL) ?"BUFOVFL " :"", 1519 (f & RC_WAS_SILOVFL) ?"SILOVFL " :"", 1520 (f & RC_SEND_RDY) ?"SEND_RDY":""); 1521 1522 rcout(sc, CD180_CAR, rc->rc_chan); 1523 1524 printf("rc%d/%d: msvr %02x ier %02x ccsr %02x\n", 1525 rc->rc_rcb->rcb_unit, rc->rc_chan, 1526 rcin(sc, CD180_MSVR), 1527 rcin(sc, CD180_IER), 1528 rcin(sc, CD180_CCSR)); 1529} 1530#endif /* RCDEBUG */ 1531 1532static void 1533rc_dtrwakeup(void *arg) 1534{ 1535 struct rc_chans *rc; 1536 1537 rc = (struct rc_chans *)arg; 1538 rc->rc_flags &= ~RC_DTR_OFF; 1539 wakeup(&rc->rc_dtrwait); 1540} 1541 1542static void 1543rc_discard_output(struct rc_chans *rc) 1544{ 1545 critical_enter(); 1546 if (rc->rc_flags & RC_DOXXFER) { 1547 rc->rc_rcb->sc_scheduled_event -= LOTS_OF_EVENTS; 1548 rc->rc_flags &= ~RC_DOXXFER; 1549 } 1550 rc->rc_optr = rc->rc_obufend; 1551 rc->rc_tp.t_state &= ~TS_BUSY; 1552 critical_exit(); 1553 ttwwakeup(&rc->rc_tp); 1554} 1555 1556static void 1557disc_optim(struct tty *tp, struct termios *t, struct rc_chans *rc) 1558{ 1559 1560 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 1561 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 1562 && (!(t->c_iflag & PARMRK) 1563 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 1564 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 1565 && linesw[tp->t_line].l_rint == ttyinput) 1566 tp->t_state |= TS_CAN_BYPASS_L_RINT; 1567 else 1568 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 1569 rc->rc_hotchar = linesw[tp->t_line].l_hotchar; 1570} 1571 1572static void 1573rc_wait0(struct rc_softc *sc, int chan, int line) 1574{ 1575 int rcnt; 1576 1577 for (rcnt = 50; rcnt && rcin(sc, CD180_CCR); rcnt--) 1578 DELAY(30); 1579 if (rcnt == 0) 1580 device_printf(sc->sc_dev, 1581 "channel %d command timeout, rc.c line: %d\n", chan, line); 1582} 1583 1584static device_method_t rc_methods[] = { 1585 /* Device interface */ 1586 DEVMETHOD(device_probe, rc_probe), 1587 DEVMETHOD(device_attach, rc_attach), 1588 DEVMETHOD(device_detach, rc_detach), 1589 { 0, 0 } 1590}; 1591 1592static driver_t rc_driver = { 1593 "rc", 1594 rc_methods, sizeof(struct rc_softc), 1595}; 1596 1597DRIVER_MODULE(rc, isa, rc_driver, rc_devclass, 0, 0);
| 159}; 160 161static devclass_t rc_devclass; 162 163/* Flags */ 164#define RC_DTR_OFF 0x0001 /* DTR wait, for close/open */ 165#define RC_ACTOUT 0x0002 /* Dial-out port active */ 166#define RC_RTSFLOW 0x0004 /* RTS flow ctl enabled */ 167#define RC_CTSFLOW 0x0008 /* CTS flow ctl enabled */ 168#define RC_DORXFER 0x0010 /* RXFER event planned */ 169#define RC_DOXXFER 0x0020 /* XXFER event planned */ 170#define RC_MODCHG 0x0040 /* Modem status changed */ 171#define RC_OSUSP 0x0080 /* Output suspended */ 172#define RC_OSBUSY 0x0100 /* start() routine in progress */ 173#define RC_WAS_BUFOVFL 0x0200 /* low-level buffer ovferflow */ 174#define RC_WAS_SILOVFL 0x0400 /* silo buffer overflow */ 175#define RC_SEND_RDY 0x0800 /* ready to send */ 176 177/* Table for translation of RCSR status bits to internal form */ 178static int rc_rcsrt[16] = { 179 0, TTY_OE, TTY_FE, 180 TTY_FE|TTY_OE, TTY_PE, TTY_PE|TTY_OE, 181 TTY_PE|TTY_FE, TTY_PE|TTY_FE|TTY_OE, TTY_BI, 182 TTY_BI|TTY_OE, TTY_BI|TTY_FE, TTY_BI|TTY_FE|TTY_OE, 183 TTY_BI|TTY_PE, TTY_BI|TTY_PE|TTY_OE, TTY_BI|TTY_PE|TTY_FE, 184 TTY_BI|TTY_PE|TTY_FE|TTY_OE 185}; 186 187static int rc_ports[] = 188 { 0x220, 0x240, 0x250, 0x260, 0x2a0, 0x2b0, 0x300, 0x320 }; 189static int iobase_addrs[IOBASE_ADDRS] = 190 { 0, 0x400, 0x800, 0xc00, 0x1400, 0x1800, 0x1c00, 0x2000, 191 0x3000, 0x3400, 0x3800, 0x3c00, 0x4000, 0x8000 }; 192 193/**********************************************/ 194 195static int 196rc_probe(device_t dev) 197{ 198 u_int port; 199 int i, found; 200 201 /* 202 * We don't know of any PnP ID's for these cards. 203 */ 204 if (isa_get_logicalid(dev) != 0) 205 return (ENXIO); 206 207 /* 208 * We have to have an IO port hint that is valid. 209 */ 210 port = isa_get_port(dev); 211 if (port == -1) 212 return (ENXIO); 213 found = 0; 214 for (i = 0; i < sizeof(rc_ports) / sizeof(int); i++) 215 if (rc_ports[i] == port) { 216 found = 1; 217 break; 218 } 219 if (!found) 220 return (ENXIO); 221 222 /* 223 * We have to have an IRQ hint. 224 */ 225 if (isa_get_irq(dev) == -1) 226 return (ENXIO); 227 228 device_set_desc(dev, "SDL Riscom/8"); 229 return (0); 230} 231 232static int 233rc_attach(device_t dev) 234{ 235 struct rc_chans *rc; 236 struct tty *tp; 237 struct rc_softc *sc; 238 u_int port; 239 int base, chan, error, i, x; 240 dev_t cdev; 241 242 sc = device_get_softc(dev); 243 sc->sc_dev = dev; 244 245 /* 246 * We need to have IO ports. Lots of them. We need 247 * the following ranges relative to the base port: 248 * 0x0 - 0x10 249 * 0x400 - 0x410 250 * 0x800 - 0x810 251 * 0xc00 - 0xc10 252 * 0x1400 - 0x1410 253 * 0x1800 - 0x1810 254 * 0x1c00 - 0x1c10 255 * 0x2000 - 0x2010 256 * 0x3000 - 0x3010 257 * 0x3400 - 0x3410 258 * 0x3800 - 0x3810 259 * 0x3c00 - 0x3c10 260 * 0x4000 - 0x4010 261 * 0x8000 - 0x8010 262 */ 263 port = isa_get_port(dev); 264 for (i = 0; i < IOBASE_ADDRS; i++) 265 if (bus_set_resource(dev, SYS_RES_IOPORT, i, 266 port + iobase_addrs[i], 0x10) != 0) 267 return (ENXIO); 268 error = ENOMEM; 269 for (i = 0; i < IOBASE_ADDRS; i++) { 270 x = i; 271 sc->sc_port[i] = bus_alloc_resource(dev, SYS_RES_IOPORT, &x, 272 0ul, ~0ul, 0x10, RF_ACTIVE); 273 if (x != i) { 274 device_printf(dev, "ioport %d was rid %d\n", i, x); 275 goto fail; 276 } 277 if (sc->sc_port[i] == NULL) { 278 device_printf(dev, "failed to alloc ioports %x-%x\n", 279 port + iobase_addrs[i], 280 port + iobase_addrs[i] + 0x10); 281 goto fail; 282 } 283 } 284 sc->sc_bt = rman_get_bustag(sc->sc_port[0]); 285 sc->sc_bh = rman_get_bushandle(sc->sc_port[0]); 286 287 sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid, 288 0ul, ~0ul, 1, RF_ACTIVE); 289 if (sc->sc_irq == NULL) { 290 device_printf(dev, "failed to alloc IRQ\n"); 291 goto fail; 292 } 293 294 /* 295 * Now do some actual tests to make sure it works. 296 */ 297 error = ENXIO; 298 rcout(sc, CD180_PPRL, 0x22); /* Random values to Prescale reg. */ 299 rcout(sc, CD180_PPRH, 0x11); 300 if (rcin(sc, CD180_PPRL) != 0x22 || rcin(sc, CD180_PPRH) != 0x11) 301 goto fail; 302 if (rc_test(sc)) 303 goto fail; 304 305 /* 306 * Ok, start actually hooking things up. 307 */ 308 sc->sc_unit = device_get_unit(dev); 309 /*sc->sc_chipid = 0x10 + device_get_unit(dev);*/ 310 device_printf(dev, "%d chans, firmware rev. %c\n", 311 CD180_NCHAN, (rcin(sc, CD180_GFRCR) & 0xF) + 'A'); 312 rc = sc->sc_channels; 313 base = CD180_NCHAN * sc->sc_unit; 314 for (chan = 0; chan < CD180_NCHAN; chan++, rc++) { 315 rc->rc_rcb = sc; 316 rc->rc_chan = chan; 317 rc->rc_iptr = rc->rc_ibuf; 318 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE]; 319 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER]; 320 rc->rc_optr = rc->rc_obufend = rc->rc_obuf; 321 rc->rc_dtrwait = 3 * hz; 322 callout_init(&rc->rc_dtrcallout, 0); 323 tp = &rc->rc_tp; 324 ttychars(tp); 325 tp->t_lflag = tp->t_iflag = tp->t_oflag = 0; 326 tp->t_cflag = TTYDEF_CFLAG; 327 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 328 cdev = make_dev(&rc_cdevsw, chan + base, 329 UID_ROOT, GID_WHEEL, 0600, "ttym%d", chan + base); 330 cdev->si_drv1 = rc; 331 cdev->si_drv2 = 0; 332 cdev->si_tty = tp; 333 rc->rc_dev = cdev; 334 cdev = make_dev(&rc_cdevsw, chan + base + 128, 335 UID_UUCP, GID_DIALER, 0660, "cuam%d", chan + base); 336 cdev->si_drv1 = rc; 337 cdev->si_drv2 = (void *)1; 338 cdev->si_tty = tp; 339 rc->rc_cdev = cdev; 340 } 341 342 error = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_TTY, rc_intr, sc, 343 &sc->sc_hwicookie); 344 if (error) { 345 device_printf(dev, "failed to register interrupt handler\n"); 346 goto fail; 347 } 348 349 swi_add(&tty_ithd, "tty:rc", rc_pollcard, sc, SWI_TTY, 0, 350 &sc->sc_swicookie); 351 return (0); 352 353fail: 354 rc_release_resources(dev); 355 return (error); 356} 357 358static int 359rc_detach(device_t dev) 360{ 361 struct rc_softc *sc; 362 struct rc_chans *rc; 363 int error, i, s; 364 365 sc = device_get_softc(dev); 366 if (sc->sc_opencount > 0) 367 return (EBUSY); 368 sc->sc_opencount = -1; 369 370 rc = sc->sc_channels; 371 for (i = 0; i < CD180_NCHAN; i++, rc++) { 372 destroy_dev(rc->rc_dev); 373 destroy_dev(rc->rc_cdev); 374 } 375 376 rc = sc->sc_channels; 377 s = splsoftclock(); 378 for (i = 0; i < CD180_NCHAN; i++) { 379 if ((rc->rc_flags & RC_DTR_OFF) && 380 !callout_stop(&rc->rc_dtrcallout)) 381 tsleep(&rc->rc_dtrwait, TTIPRI, "rcdtrdet", 0); 382 } 383 384 error = bus_teardown_intr(dev, sc->sc_irq, sc->sc_hwicookie); 385 if (error) 386 device_printf(dev, "failed to deregister interrupt handler\n"); 387 ithread_remove_handler(sc->sc_swicookie); 388 rc_release_resources(dev); 389 390 return (0); 391} 392 393static void 394rc_release_resources(device_t dev) 395{ 396 struct rc_softc *sc; 397 int i; 398 399 sc = device_get_softc(dev); 400 if (sc->sc_irq != NULL) { 401 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, 402 sc->sc_irq); 403 sc->sc_irq = NULL; 404 } 405 for (i = 0; i < IOBASE_ADDRS; i++) { 406 if (sc->sc_port[i] == NULL) 407 break; 408 bus_release_resource(dev, SYS_RES_IOPORT, i, sc->sc_port[i]); 409 sc->sc_port[i] = NULL; 410 } 411} 412 413/* RC interrupt handling */ 414static void 415rc_intr(void *arg) 416{ 417 struct rc_softc *sc; 418 struct rc_chans *rc; 419 int resid, chan; 420 u_char val, iack, bsr, ucnt, *optr; 421 int good_data, t_state; 422 423 sc = (struct rc_softc *)arg; 424 bsr = ~(rcin(sc, RC_BSR)); 425 if (!(bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT))) { 426 device_printf(sc->sc_dev, "extra interrupt\n"); 427 rcout(sc, CD180_EOIR, 0); 428 return; 429 } 430 431 while (bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT)) { 432#ifdef RCDEBUG_DETAILED 433 device_printf(sc->sc_dev, "intr (%p) %s%s%s%s\n", arg, bsr, 434 (bsr & RC_BSR_TOUT)?"TOUT ":"", 435 (bsr & RC_BSR_RXINT)?"RXINT ":"", 436 (bsr & RC_BSR_TXINT)?"TXINT ":"", 437 (bsr & RC_BSR_MOINT)?"MOINT":""); 438#endif 439 if (bsr & RC_BSR_TOUT) { 440 device_printf(sc->sc_dev, 441 "hardware failure, reset board\n"); 442 rcout(sc, RC_CTOUT, 0); 443 rc_reinit(sc); 444 return; 445 } 446 if (bsr & RC_BSR_RXINT) { 447 iack = rcin(sc, RC_PILR_RX); 448 good_data = (iack == (GIVR_IT_RGDI | RC_FAKEID)); 449 if (!good_data && iack != (GIVR_IT_REI | RC_FAKEID)) { 450 device_printf(sc->sc_dev, 451 "fake rxint: %02x\n", iack); 452 goto more_intrs; 453 } 454 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 455 rc = &sc->sc_channels[chan]; 456 t_state = rc->rc_tp.t_state; 457 /* Do RTS flow control stuff */ 458 if ( (rc->rc_flags & RC_RTSFLOW) 459 || !(t_state & TS_ISOPEN) 460 ) { 461 if ( ( !(t_state & TS_ISOPEN) 462 || (t_state & TS_TBLOCK) 463 ) 464 && (rc->rc_msvr & MSVR_RTS) 465 ) 466 rcout(sc, CD180_MSVR, 467 rc->rc_msvr &= ~MSVR_RTS); 468 else if (!(rc->rc_msvr & MSVR_RTS)) 469 rcout(sc, CD180_MSVR, 470 rc->rc_msvr |= MSVR_RTS); 471 } 472 ucnt = rcin(sc, CD180_RDCR) & 0xF; 473 resid = 0; 474 475 if (t_state & TS_ISOPEN) { 476 /* check for input buffer overflow */ 477 if ((rc->rc_iptr + ucnt) >= rc->rc_bufend) { 478 resid = ucnt; 479 ucnt = rc->rc_bufend - rc->rc_iptr; 480 resid -= ucnt; 481 if (!(rc->rc_flags & RC_WAS_BUFOVFL)) { 482 rc->rc_flags |= RC_WAS_BUFOVFL; 483 sc->sc_scheduled_event++; 484 } 485 } 486 optr = rc->rc_iptr; 487 /* check foor good data */ 488 if (good_data) { 489 while (ucnt-- > 0) { 490 val = rcin(sc, CD180_RDR); 491 optr[0] = val; 492 optr[INPUT_FLAGS_SHIFT] = 0; 493 optr++; 494 sc->sc_scheduled_event++; 495 if (val != 0 && val == rc->rc_hotchar) 496 swi_sched(sc->sc_swicookie, 0); 497 } 498 } else { 499 /* Store also status data */ 500 while (ucnt-- > 0) { 501 iack = rcin(sc, CD180_RCSR); 502 if (iack & RCSR_Timeout) 503 break; 504 if ( (iack & RCSR_OE) 505 && !(rc->rc_flags & RC_WAS_SILOVFL)) { 506 rc->rc_flags |= RC_WAS_SILOVFL; 507 sc->sc_scheduled_event++; 508 } 509 val = rcin(sc, CD180_RDR); 510 /* 511 Don't store PE if IGNPAR and BREAK if IGNBRK, 512 this hack allows "raw" tty optimization 513 works even if IGN* is set. 514 */ 515 if ( !(iack & (RCSR_PE|RCSR_FE|RCSR_Break)) 516 || ((!(iack & (RCSR_PE|RCSR_FE)) 517 || !(rc->rc_tp.t_iflag & IGNPAR)) 518 && (!(iack & RCSR_Break) 519 || !(rc->rc_tp.t_iflag & IGNBRK)))) { 520 if ( (iack & (RCSR_PE|RCSR_FE)) 521 && (t_state & TS_CAN_BYPASS_L_RINT) 522 && ((iack & RCSR_FE) 523 || ((iack & RCSR_PE) 524 && (rc->rc_tp.t_iflag & INPCK)))) 525 val = 0; 526 else if (val != 0 && val == rc->rc_hotchar) 527 swi_sched(sc->sc_swicookie, 0); 528 optr[0] = val; 529 optr[INPUT_FLAGS_SHIFT] = iack; 530 optr++; 531 sc->sc_scheduled_event++; 532 } 533 } 534 } 535 rc->rc_iptr = optr; 536 rc->rc_flags |= RC_DORXFER; 537 } else 538 resid = ucnt; 539 /* Clear FIFO if necessary */ 540 while (resid-- > 0) { 541 if (!good_data) 542 iack = rcin(sc, CD180_RCSR); 543 else 544 iack = 0; 545 if (iack & RCSR_Timeout) 546 break; 547 (void) rcin(sc, CD180_RDR); 548 } 549 goto more_intrs; 550 } 551 if (bsr & RC_BSR_MOINT) { 552 iack = rcin(sc, RC_PILR_MODEM); 553 if (iack != (GIVR_IT_MSCI | RC_FAKEID)) { 554 device_printf(sc->sc_dev, "fake moint: %02x\n", 555 iack); 556 goto more_intrs; 557 } 558 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 559 rc = &sc->sc_channels[chan]; 560 iack = rcin(sc, CD180_MCR); 561 rc->rc_msvr = rcin(sc, CD180_MSVR); 562 rcout(sc, CD180_MCR, 0); 563#ifdef RCDEBUG 564 printrcflags(rc, "moint"); 565#endif 566 if (rc->rc_flags & RC_CTSFLOW) { 567 if (rc->rc_msvr & MSVR_CTS) 568 rc->rc_flags |= RC_SEND_RDY; 569 else 570 rc->rc_flags &= ~RC_SEND_RDY; 571 } else 572 rc->rc_flags |= RC_SEND_RDY; 573 if ((iack & MCR_CDchg) && !(rc->rc_flags & RC_MODCHG)) { 574 sc->sc_scheduled_event += LOTS_OF_EVENTS; 575 rc->rc_flags |= RC_MODCHG; 576 swi_sched(sc->sc_swicookie, 0); 577 } 578 goto more_intrs; 579 } 580 if (bsr & RC_BSR_TXINT) { 581 iack = rcin(sc, RC_PILR_TX); 582 if (iack != (GIVR_IT_TDI | RC_FAKEID)) { 583 device_printf(sc->sc_dev, "fake txint: %02x\n", 584 iack); 585 goto more_intrs; 586 } 587 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 588 rc = &sc->sc_channels[chan]; 589 if ( (rc->rc_flags & RC_OSUSP) 590 || !(rc->rc_flags & RC_SEND_RDY) 591 ) 592 goto more_intrs; 593 /* Handle breaks and other stuff */ 594 if (rc->rc_pendcmd) { 595 rcout(sc, CD180_COR2, rc->rc_cor2 |= COR2_ETC); 596 rcout(sc, CD180_TDR, CD180_C_ESC); 597 rcout(sc, CD180_TDR, rc->rc_pendcmd); 598 rcout(sc, CD180_COR2, rc->rc_cor2 &= ~COR2_ETC); 599 rc->rc_pendcmd = 0; 600 goto more_intrs; 601 } 602 optr = rc->rc_optr; 603 resid = rc->rc_obufend - optr; 604 if (resid > CD180_NFIFO) 605 resid = CD180_NFIFO; 606 while (resid-- > 0) 607 rcout(sc, CD180_TDR, *optr++); 608 rc->rc_optr = optr; 609 610 /* output completed? */ 611 if (optr >= rc->rc_obufend) { 612 rcout(sc, CD180_IER, rc->rc_ier &= ~IER_TxRdy); 613#ifdef RCDEBUG 614 device_printf(sc->sc_dev, 615 "channel %d: output completed\n", 616 rc->rc_chan); 617#endif 618 if (!(rc->rc_flags & RC_DOXXFER)) { 619 sc->sc_scheduled_event += LOTS_OF_EVENTS; 620 rc->rc_flags |= RC_DOXXFER; 621 swi_sched(sc->sc_swicookie, 0); 622 } 623 } 624 } 625 more_intrs: 626 rcout(sc, CD180_EOIR, 0); /* end of interrupt */ 627 rcout(sc, RC_CTOUT, 0); 628 bsr = ~(rcin(sc, RC_BSR)); 629 } 630} 631 632/* Feed characters to output buffer */ 633static void 634rc_start(struct tty *tp) 635{ 636 struct rc_softc *sc; 637 struct rc_chans *rc; 638 int s; 639 640 rc = TTY_TO_RC(tp); 641 if (rc->rc_flags & RC_OSBUSY) 642 return; 643 sc = rc->rc_rcb; 644 s = spltty(); 645 rc->rc_flags |= RC_OSBUSY; 646 critical_enter(); 647 if (tp->t_state & TS_TTSTOP) 648 rc->rc_flags |= RC_OSUSP; 649 else 650 rc->rc_flags &= ~RC_OSUSP; 651 /* Do RTS flow control stuff */ 652 if ( (rc->rc_flags & RC_RTSFLOW) 653 && (tp->t_state & TS_TBLOCK) 654 && (rc->rc_msvr & MSVR_RTS) 655 ) { 656 rcout(sc, CD180_CAR, rc->rc_chan); 657 rcout(sc, CD180_MSVR, rc->rc_msvr &= ~MSVR_RTS); 658 } else if (!(rc->rc_msvr & MSVR_RTS)) { 659 rcout(sc, CD180_CAR, rc->rc_chan); 660 rcout(sc, CD180_MSVR, rc->rc_msvr |= MSVR_RTS); 661 } 662 critical_exit(); 663 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP)) 664 goto out; 665#ifdef RCDEBUG 666 printrcflags(rc, "rcstart"); 667#endif 668 ttwwakeup(tp); 669#ifdef RCDEBUG 670 printf("rcstart: outq = %d obuf = %d\n", 671 tp->t_outq.c_cc, rc->rc_obufend - rc->rc_optr); 672#endif 673 if (tp->t_state & TS_BUSY) 674 goto out; /* output still in progress ... */ 675 676 if (tp->t_outq.c_cc > 0) { 677 u_int ocnt; 678 679 tp->t_state |= TS_BUSY; 680 ocnt = q_to_b(&tp->t_outq, rc->rc_obuf, sizeof rc->rc_obuf); 681 critical_enter(); 682 rc->rc_optr = rc->rc_obuf; 683 rc->rc_obufend = rc->rc_optr + ocnt; 684 critical_exit(); 685 if (!(rc->rc_ier & IER_TxRdy)) { 686#ifdef RCDEBUG 687 device_printf(sc->sc_dev, 688 "channel %d: rcstart enable txint\n", rc->rc_chan); 689#endif 690 rcout(sc, CD180_CAR, rc->rc_chan); 691 rcout(sc, CD180_IER, rc->rc_ier |= IER_TxRdy); 692 } 693 } 694out: 695 rc->rc_flags &= ~RC_OSBUSY; 696 (void) splx(s); 697} 698 699/* Handle delayed events. */ 700void 701rc_pollcard(void *arg) 702{ 703 struct rc_softc *sc; 704 struct rc_chans *rc; 705 struct tty *tp; 706 u_char *tptr, *eptr; 707 int chan, icnt; 708 709 sc = (struct rc_softc *)arg; 710 if (sc->sc_scheduled_event == 0) 711 return; 712 do { 713 rc = sc->sc_channels; 714 for (chan = 0; chan < CD180_NCHAN; rc++, chan++) { 715 tp = &rc->rc_tp; 716#ifdef RCDEBUG 717 if (rc->rc_flags & (RC_DORXFER|RC_DOXXFER|RC_MODCHG| 718 RC_WAS_BUFOVFL|RC_WAS_SILOVFL)) 719 printrcflags(rc, "rcevent"); 720#endif 721 if (rc->rc_flags & RC_WAS_BUFOVFL) { 722 critical_enter(); 723 rc->rc_flags &= ~RC_WAS_BUFOVFL; 724 sc->sc_scheduled_event--; 725 critical_exit(); 726 device_printf(sc->sc_dev, 727 "channel %d: interrupt-level buffer overflow\n", 728 chan); 729 } 730 if (rc->rc_flags & RC_WAS_SILOVFL) { 731 critical_enter(); 732 rc->rc_flags &= ~RC_WAS_SILOVFL; 733 sc->sc_scheduled_event--; 734 critical_exit(); 735 device_printf(sc->sc_dev, 736 "channel %d: silo overflow\n", chan); 737 } 738 if (rc->rc_flags & RC_MODCHG) { 739 critical_enter(); 740 rc->rc_flags &= ~RC_MODCHG; 741 sc->sc_scheduled_event -= LOTS_OF_EVENTS; 742 critical_exit(); 743 (*linesw[tp->t_line].l_modem)(tp, !!(rc->rc_msvr & MSVR_CD)); 744 } 745 if (rc->rc_flags & RC_DORXFER) { 746 critical_enter(); 747 rc->rc_flags &= ~RC_DORXFER; 748 eptr = rc->rc_iptr; 749 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) 750 tptr = &rc->rc_ibuf[RC_IBUFSIZE]; 751 else 752 tptr = rc->rc_ibuf; 753 icnt = eptr - tptr; 754 if (icnt > 0) { 755 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) { 756 rc->rc_iptr = rc->rc_ibuf; 757 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE]; 758 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER]; 759 } else { 760 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE]; 761 rc->rc_bufend = &rc->rc_ibuf[2 * RC_IBUFSIZE]; 762 rc->rc_hiwat = 763 &rc->rc_ibuf[RC_IBUFSIZE + RC_IHIGHWATER]; 764 } 765 if ( (rc->rc_flags & RC_RTSFLOW) 766 && (tp->t_state & TS_ISOPEN) 767 && !(tp->t_state & TS_TBLOCK) 768 && !(rc->rc_msvr & MSVR_RTS) 769 ) { 770 rcout(sc, CD180_CAR, chan); 771 rcout(sc, CD180_MSVR, 772 rc->rc_msvr |= MSVR_RTS); 773 } 774 sc->sc_scheduled_event -= icnt; 775 } 776 critical_exit(); 777 778 if (icnt <= 0 || !(tp->t_state & TS_ISOPEN)) 779 goto done1; 780 781 if ( (tp->t_state & TS_CAN_BYPASS_L_RINT) 782 && !(tp->t_state & TS_LOCAL)) { 783 if ((tp->t_rawq.c_cc + icnt) >= RB_I_HIGH_WATER 784 && ((rc->rc_flags & RC_RTSFLOW) || (tp->t_iflag & IXOFF)) 785 && !(tp->t_state & TS_TBLOCK)) 786 ttyblock(tp); 787 tk_nin += icnt; 788 tk_rawcc += icnt; 789 tp->t_rawcc += icnt; 790 if (b_to_q(tptr, icnt, &tp->t_rawq)) 791 device_printf(sc->sc_dev, 792 "channel %d: tty-level buffer overflow\n", 793 chan); 794 ttwakeup(tp); 795 if ((tp->t_state & TS_TTSTOP) && ((tp->t_iflag & IXANY) 796 || (tp->t_cc[VSTART] == tp->t_cc[VSTOP]))) { 797 tp->t_state &= ~TS_TTSTOP; 798 tp->t_lflag &= ~FLUSHO; 799 rc_start(tp); 800 } 801 } else { 802 for (; tptr < eptr; tptr++) 803 (*linesw[tp->t_line].l_rint) 804 (tptr[0] | 805 rc_rcsrt[tptr[INPUT_FLAGS_SHIFT] & 0xF], tp); 806 } 807done1: ; 808 } 809 if (rc->rc_flags & RC_DOXXFER) { 810 critical_enter(); 811 sc->sc_scheduled_event -= LOTS_OF_EVENTS; 812 rc->rc_flags &= ~RC_DOXXFER; 813 rc->rc_tp.t_state &= ~TS_BUSY; 814 critical_exit(); 815 (*linesw[tp->t_line].l_start)(tp); 816 } 817 if (sc->sc_scheduled_event == 0) 818 break; 819 } 820 } while (sc->sc_scheduled_event >= LOTS_OF_EVENTS); 821} 822 823static void 824rc_stop(struct tty *tp, int rw) 825{ 826 struct rc_softc *sc; 827 struct rc_chans *rc; 828 u_char *tptr, *eptr; 829 830 rc = TTY_TO_RC(tp); 831 sc = rc->rc_rcb; 832#ifdef RCDEBUG 833 device_printf(sc->sc_dev, "channel %d: rc_stop %s%s\n", 834 rc->rc_chan, (rw & FWRITE)?"FWRITE ":"", (rw & FREAD)?"FREAD":""); 835#endif 836 if (rw & FWRITE) 837 rc_discard_output(rc); 838 critical_enter(); 839 if (rw & FREAD) { 840 rc->rc_flags &= ~RC_DORXFER; 841 eptr = rc->rc_iptr; 842 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) { 843 tptr = &rc->rc_ibuf[RC_IBUFSIZE]; 844 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE]; 845 } else { 846 tptr = rc->rc_ibuf; 847 rc->rc_iptr = rc->rc_ibuf; 848 } 849 sc->sc_scheduled_event -= eptr - tptr; 850 } 851 if (tp->t_state & TS_TTSTOP) 852 rc->rc_flags |= RC_OSUSP; 853 else 854 rc->rc_flags &= ~RC_OSUSP; 855 critical_exit(); 856} 857 858static int 859rcopen(dev_t dev, int flag, int mode, d_thread_t *td) 860{ 861 struct rc_softc *sc; 862 struct rc_chans *rc; 863 struct tty *tp; 864 int s, error = 0; 865 866 rc = DEV_TO_RC(dev); 867 sc = rc->rc_rcb; 868 tp = &rc->rc_tp; 869 if (sc->sc_opencount < 0) 870 return (ENXIO); 871 sc->sc_opencount++; 872#ifdef RCDEBUG 873 device_printf(sc->sc_dev, "channel %d: rcopen: dev %p\n", 874 rc->rc_chan, dev); 875#endif 876 s = spltty(); 877 878again: 879 while (rc->rc_flags & RC_DTR_OFF) { 880 error = tsleep(&(rc->rc_dtrwait), TTIPRI | PCATCH, "rcdtr", 0); 881 if (error != 0) 882 goto out; 883 } 884 if (tp->t_state & TS_ISOPEN) { 885 if (CALLOUT(dev)) { 886 if (!(rc->rc_flags & RC_ACTOUT)) { 887 error = EBUSY; 888 goto out; 889 } 890 } else { 891 if (rc->rc_flags & RC_ACTOUT) { 892 if (flag & O_NONBLOCK) { 893 error = EBUSY; 894 goto out; 895 } 896 error = tsleep(&rc->rc_rcb, 897 TTIPRI|PCATCH, "rcbi", 0); 898 if (error) 899 goto out; 900 goto again; 901 } 902 } 903 if (tp->t_state & TS_XCLUDE && 904 suser(td)) { 905 error = EBUSY; 906 goto out; 907 } 908 } else { 909 tp->t_oproc = rc_start; 910 tp->t_param = rc_param; 911 tp->t_stop = rc_stop; 912 tp->t_dev = dev; 913 914 if (CALLOUT(dev)) 915 tp->t_cflag |= CLOCAL; 916 else 917 tp->t_cflag &= ~CLOCAL; 918 919 error = rc_param(tp, &tp->t_termios); 920 if (error) 921 goto out; 922 (void) rc_modctl(rc, TIOCM_RTS|TIOCM_DTR, DMSET); 923 924 if ((rc->rc_msvr & MSVR_CD) || CALLOUT(dev)) 925 (*linesw[tp->t_line].l_modem)(tp, 1); 926 } 927 if (!(tp->t_state & TS_CARR_ON) && !CALLOUT(dev) 928 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 929 rc->rc_dcdwaits++; 930 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "rcdcd", 0); 931 rc->rc_dcdwaits--; 932 if (error != 0) 933 goto out; 934 goto again; 935 } 936 error = (*linesw[tp->t_line].l_open)(dev, tp); 937 disc_optim(tp, &tp->t_termios, rc); 938 if ((tp->t_state & TS_ISOPEN) && CALLOUT(dev)) 939 rc->rc_flags |= RC_ACTOUT; 940out: 941 (void) splx(s); 942 943 if(rc->rc_dcdwaits == 0 && !(tp->t_state & TS_ISOPEN)) 944 rc_hardclose(rc); 945 946 return error; 947} 948 949static int 950rcclose(dev_t dev, int flag, int mode, d_thread_t *td) 951{ 952 struct rc_softc *sc; 953 struct rc_chans *rc; 954 struct tty *tp; 955 int s; 956 957 rc = DEV_TO_RC(dev); 958 sc = rc->rc_rcb; 959 tp = &rc->rc_tp; 960#ifdef RCDEBUG 961 device_printf(sc->sc_dev, "channel %d: rcclose dev %p\n", 962 rc->rc_chan, dev); 963#endif 964 s = spltty(); 965 (*linesw[tp->t_line].l_close)(tp, flag); 966 disc_optim(tp, &tp->t_termios, rc); 967 rc_stop(tp, FREAD | FWRITE); 968 rc_hardclose(rc); 969 ttyclose(tp); 970 splx(s); 971 KASSERT(sc->sc_opencount > 0, ("rcclose: non-positive open count")); 972 sc->sc_opencount--; 973 return 0; 974} 975 976static void 977rc_hardclose(struct rc_chans *rc) 978{ 979 struct rc_softc *sc; 980 struct tty *tp; 981 int s; 982 983 tp = &rc->rc_tp; 984 sc = rc->rc_rcb; 985 s = spltty(); 986 rcout(sc, CD180_CAR, rc->rc_chan); 987 988 /* Disable rx/tx intrs */ 989 rcout(sc, CD180_IER, rc->rc_ier = 0); 990 if ( (tp->t_cflag & HUPCL) 991 || (!(rc->rc_flags & RC_ACTOUT) 992 && !(rc->rc_msvr & MSVR_CD) 993 && !(tp->t_cflag & CLOCAL)) 994 || !(tp->t_state & TS_ISOPEN) 995 ) { 996 CCRCMD(sc, rc->rc_chan, CCR_ResetChan); 997 WAITFORCCR(sc, rc->rc_chan); 998 (void) rc_modctl(rc, TIOCM_RTS, DMSET); 999 if (rc->rc_dtrwait) { 1000 callout_reset(&rc->rc_dtrcallout, rc->rc_dtrwait, 1001 rc_dtrwakeup, rc); 1002 rc->rc_flags |= RC_DTR_OFF; 1003 } 1004 } 1005 rc->rc_flags &= ~RC_ACTOUT; 1006 wakeup( &rc->rc_rcb); /* wake bi */ 1007 wakeup(TSA_CARR_ON(tp)); 1008 (void) splx(s); 1009} 1010 1011/* Reset the bastard */ 1012static void 1013rc_hwreset(struct rc_softc *sc, uint chipid) 1014{ 1015 CCRCMD(sc, -1, CCR_HWRESET); /* Hardware reset */ 1016 DELAY(20000); 1017 WAITFORCCR(sc, -1); 1018 1019 rcout(sc, RC_CTOUT, 0); /* Clear timeout */ 1020 rcout(sc, CD180_GIVR, chipid); 1021 rcout(sc, CD180_GICR, 0); 1022 1023 /* Set Prescaler Registers (1 msec) */ 1024 rcout(sc, CD180_PPRL, ((RC_OSCFREQ + 999) / 1000) & 0xFF); 1025 rcout(sc, CD180_PPRH, ((RC_OSCFREQ + 999) / 1000) >> 8); 1026 1027 /* Initialize Priority Interrupt Level Registers */ 1028 rcout(sc, CD180_PILR1, RC_PILR_MODEM); 1029 rcout(sc, CD180_PILR2, RC_PILR_TX); 1030 rcout(sc, CD180_PILR3, RC_PILR_RX); 1031 1032 /* Reset DTR */ 1033 rcout(sc, RC_DTREG, ~0); 1034} 1035 1036/* Set channel parameters */ 1037static int 1038rc_param(struct tty *tp, struct termios *ts) 1039{ 1040 struct rc_softc *sc; 1041 struct rc_chans *rc; 1042 int idivs, odivs, s, val, cflag, iflag, lflag, inpflow; 1043 1044 if ( ts->c_ospeed < 0 || ts->c_ospeed > 76800 1045 || ts->c_ispeed < 0 || ts->c_ispeed > 76800 1046 ) 1047 return (EINVAL); 1048 if (ts->c_ispeed == 0) 1049 ts->c_ispeed = ts->c_ospeed; 1050 odivs = RC_BRD(ts->c_ospeed); 1051 idivs = RC_BRD(ts->c_ispeed); 1052 1053 rc = TTY_TO_RC(tp); 1054 sc = rc->rc_rcb; 1055 s = spltty(); 1056 1057 /* Select channel */ 1058 rcout(sc, CD180_CAR, rc->rc_chan); 1059 1060 /* If speed == 0, hangup line */ 1061 if (ts->c_ospeed == 0) { 1062 CCRCMD(sc, rc->rc_chan, CCR_ResetChan); 1063 WAITFORCCR(sc, rc->rc_chan); 1064 (void) rc_modctl(rc, TIOCM_DTR, DMBIC); 1065 } 1066 1067 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 1068 cflag = ts->c_cflag; 1069 iflag = ts->c_iflag; 1070 lflag = ts->c_lflag; 1071 1072 if (idivs > 0) { 1073 rcout(sc, CD180_RBPRL, idivs & 0xFF); 1074 rcout(sc, CD180_RBPRH, idivs >> 8); 1075 } 1076 if (odivs > 0) { 1077 rcout(sc, CD180_TBPRL, odivs & 0xFF); 1078 rcout(sc, CD180_TBPRH, odivs >> 8); 1079 } 1080 1081 /* set timeout value */ 1082 if (ts->c_ispeed > 0) { 1083 int itm = ts->c_ispeed > 2400 ? 5 : 10000 / ts->c_ispeed + 1; 1084 1085 if ( !(lflag & ICANON) 1086 && ts->c_cc[VMIN] != 0 && ts->c_cc[VTIME] != 0 1087 && ts->c_cc[VTIME] * 10 > itm) 1088 itm = ts->c_cc[VTIME] * 10; 1089 1090 rcout(sc, CD180_RTPR, itm <= 255 ? itm : 255); 1091 } 1092 1093 switch (cflag & CSIZE) { 1094 case CS5: val = COR1_5BITS; break; 1095 case CS6: val = COR1_6BITS; break; 1096 case CS7: val = COR1_7BITS; break; 1097 default: 1098 case CS8: val = COR1_8BITS; break; 1099 } 1100 if (cflag & PARENB) { 1101 val |= COR1_NORMPAR; 1102 if (cflag & PARODD) 1103 val |= COR1_ODDP; 1104 if (!(cflag & INPCK)) 1105 val |= COR1_Ignore; 1106 } else 1107 val |= COR1_Ignore; 1108 if (cflag & CSTOPB) 1109 val |= COR1_2SB; 1110 rcout(sc, CD180_COR1, val); 1111 1112 /* Set FIFO threshold */ 1113 val = ts->c_ospeed <= 4800 ? 1 : CD180_NFIFO / 2; 1114 inpflow = 0; 1115 if ( (iflag & IXOFF) 1116 && ( ts->c_cc[VSTOP] != _POSIX_VDISABLE 1117 && ( ts->c_cc[VSTART] != _POSIX_VDISABLE 1118 || (iflag & IXANY) 1119 ) 1120 ) 1121 ) { 1122 inpflow = 1; 1123 val |= COR3_SCDE|COR3_FCT; 1124 } 1125 rcout(sc, CD180_COR3, val); 1126 1127 /* Initialize on-chip automatic flow control */ 1128 val = 0; 1129 rc->rc_flags &= ~(RC_CTSFLOW|RC_SEND_RDY); 1130 if (cflag & CCTS_OFLOW) { 1131 rc->rc_flags |= RC_CTSFLOW; 1132 val |= COR2_CtsAE; 1133 } else 1134 rc->rc_flags |= RC_SEND_RDY; 1135 if (tp->t_state & TS_TTSTOP) 1136 rc->rc_flags |= RC_OSUSP; 1137 else 1138 rc->rc_flags &= ~RC_OSUSP; 1139 if (cflag & CRTS_IFLOW) 1140 rc->rc_flags |= RC_RTSFLOW; 1141 else 1142 rc->rc_flags &= ~RC_RTSFLOW; 1143 1144 if (inpflow) { 1145 if (ts->c_cc[VSTART] != _POSIX_VDISABLE) 1146 rcout(sc, CD180_SCHR1, ts->c_cc[VSTART]); 1147 rcout(sc, CD180_SCHR2, ts->c_cc[VSTOP]); 1148 val |= COR2_TxIBE; 1149 if (iflag & IXANY) 1150 val |= COR2_IXM; 1151 } 1152 1153 rcout(sc, CD180_COR2, rc->rc_cor2 = val); 1154 1155 CCRCMD(sc, rc->rc_chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3); 1156 1157 disc_optim(tp, ts, rc); 1158 1159 /* modem ctl */ 1160 val = cflag & CLOCAL ? 0 : MCOR1_CDzd; 1161 if (cflag & CCTS_OFLOW) 1162 val |= MCOR1_CTSzd; 1163 rcout(sc, CD180_MCOR1, val); 1164 1165 val = cflag & CLOCAL ? 0 : MCOR2_CDod; 1166 if (cflag & CCTS_OFLOW) 1167 val |= MCOR2_CTSod; 1168 rcout(sc, CD180_MCOR2, val); 1169 1170 /* enable i/o and interrupts */ 1171 CCRCMD(sc, rc->rc_chan, 1172 CCR_XMTREN | ((cflag & CREAD) ? CCR_RCVREN : CCR_RCVRDIS)); 1173 WAITFORCCR(sc, rc->rc_chan); 1174 1175 rc->rc_ier = cflag & CLOCAL ? 0 : IER_CD; 1176 if (cflag & CCTS_OFLOW) 1177 rc->rc_ier |= IER_CTS; 1178 if (cflag & CREAD) 1179 rc->rc_ier |= IER_RxData; 1180 if (tp->t_state & TS_BUSY) 1181 rc->rc_ier |= IER_TxRdy; 1182 if (ts->c_ospeed != 0) 1183 rc_modctl(rc, TIOCM_DTR, DMBIS); 1184 if ((cflag & CCTS_OFLOW) && (rc->rc_msvr & MSVR_CTS)) 1185 rc->rc_flags |= RC_SEND_RDY; 1186 rcout(sc, CD180_IER, rc->rc_ier); 1187 (void) splx(s); 1188 return 0; 1189} 1190 1191/* Re-initialize board after bogus interrupts */ 1192static void 1193rc_reinit(struct rc_softc *sc) 1194{ 1195 struct rc_chans *rc; 1196 int i; 1197 1198 rc_hwreset(sc, RC_FAKEID); 1199 rc = sc->sc_channels; 1200 for (i = 0; i < CD180_NCHAN; i++, rc++) 1201 (void) rc_param(&rc->rc_tp, &rc->rc_tp.t_termios); 1202} 1203 1204static int 1205rcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td) 1206{ 1207 struct rc_chans *rc; 1208 struct tty *tp; 1209 int s, error; 1210 1211 rc = DEV_TO_RC(dev); 1212 tp = &rc->rc_tp; 1213 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 1214 if (error != ENOIOCTL) 1215 return (error); 1216 error = ttioctl(tp, cmd, data, flag); 1217 disc_optim(tp, &tp->t_termios, rc); 1218 if (error != ENOIOCTL) 1219 return (error); 1220 s = spltty(); 1221 1222 switch (cmd) { 1223 case TIOCSBRK: 1224 rc->rc_pendcmd = CD180_C_SBRK; 1225 break; 1226 1227 case TIOCCBRK: 1228 rc->rc_pendcmd = CD180_C_EBRK; 1229 break; 1230 1231 case TIOCSDTR: 1232 (void) rc_modctl(rc, TIOCM_DTR, DMBIS); 1233 break; 1234 1235 case TIOCCDTR: 1236 (void) rc_modctl(rc, TIOCM_DTR, DMBIC); 1237 break; 1238 1239 case TIOCMGET: 1240 *(int *) data = rc_modctl(rc, 0, DMGET); 1241 break; 1242 1243 case TIOCMSET: 1244 (void) rc_modctl(rc, *(int *) data, DMSET); 1245 break; 1246 1247 case TIOCMBIC: 1248 (void) rc_modctl(rc, *(int *) data, DMBIC); 1249 break; 1250 1251 case TIOCMBIS: 1252 (void) rc_modctl(rc, *(int *) data, DMBIS); 1253 break; 1254 1255 case TIOCMSDTRWAIT: 1256 error = suser(td); 1257 if (error != 0) { 1258 splx(s); 1259 return (error); 1260 } 1261 rc->rc_dtrwait = *(int *)data * hz / 100; 1262 break; 1263 1264 case TIOCMGDTRWAIT: 1265 *(int *)data = rc->rc_dtrwait * 100 / hz; 1266 break; 1267 1268 default: 1269 (void) splx(s); 1270 return ENOTTY; 1271 } 1272 (void) splx(s); 1273 return 0; 1274} 1275 1276 1277/* Modem control routines */ 1278 1279static int 1280rc_modctl(struct rc_chans *rc, int bits, int cmd) 1281{ 1282 struct rc_softc *sc; 1283 u_char *dtr; 1284 u_char msvr; 1285 1286 sc = rc->rc_rcb; 1287 dtr = &sc->sc_dtr; 1288 rcout(sc, CD180_CAR, rc->rc_chan); 1289 1290 switch (cmd) { 1291 case DMSET: 1292 rcout(sc, RC_DTREG, (bits & TIOCM_DTR) ? 1293 ~(*dtr |= 1 << rc->rc_chan) : 1294 ~(*dtr &= ~(1 << rc->rc_chan))); 1295 msvr = rcin(sc, CD180_MSVR); 1296 if (bits & TIOCM_RTS) 1297 msvr |= MSVR_RTS; 1298 else 1299 msvr &= ~MSVR_RTS; 1300 if (bits & TIOCM_DTR) 1301 msvr |= MSVR_DTR; 1302 else 1303 msvr &= ~MSVR_DTR; 1304 rcout(sc, CD180_MSVR, msvr); 1305 break; 1306 1307 case DMBIS: 1308 if (bits & TIOCM_DTR) 1309 rcout(sc, RC_DTREG, ~(*dtr |= 1 << rc->rc_chan)); 1310 msvr = rcin(sc, CD180_MSVR); 1311 if (bits & TIOCM_RTS) 1312 msvr |= MSVR_RTS; 1313 if (bits & TIOCM_DTR) 1314 msvr |= MSVR_DTR; 1315 rcout(sc, CD180_MSVR, msvr); 1316 break; 1317 1318 case DMGET: 1319 bits = TIOCM_LE; 1320 msvr = rc->rc_msvr = rcin(sc, CD180_MSVR); 1321 1322 if (msvr & MSVR_RTS) 1323 bits |= TIOCM_RTS; 1324 if (msvr & MSVR_CTS) 1325 bits |= TIOCM_CTS; 1326 if (msvr & MSVR_DSR) 1327 bits |= TIOCM_DSR; 1328 if (msvr & MSVR_DTR) 1329 bits |= TIOCM_DTR; 1330 if (msvr & MSVR_CD) 1331 bits |= TIOCM_CD; 1332 if (~rcin(sc, RC_RIREG) & (1 << rc->rc_chan)) 1333 bits |= TIOCM_RI; 1334 return bits; 1335 1336 case DMBIC: 1337 if (bits & TIOCM_DTR) 1338 rcout(sc, RC_DTREG, ~(*dtr &= ~(1 << rc->rc_chan))); 1339 msvr = rcin(sc, CD180_MSVR); 1340 if (bits & TIOCM_RTS) 1341 msvr &= ~MSVR_RTS; 1342 if (bits & TIOCM_DTR) 1343 msvr &= ~MSVR_DTR; 1344 rcout(sc, CD180_MSVR, msvr); 1345 break; 1346 } 1347 rc->rc_msvr = rcin(sc, CD180_MSVR); 1348 return 0; 1349} 1350 1351#define ERR(s) do { \ 1352 device_printf(sc->sc_dev, "%s", ""); \ 1353 printf s ; \ 1354 printf("\n"); \ 1355 (void) splx(old_level); \ 1356 return 1; \ 1357} while (0) 1358 1359/* Test the board. */ 1360int 1361rc_test(struct rc_softc *sc) 1362{ 1363 int chan = 0; 1364 int i = 0, rcnt, old_level; 1365 unsigned int iack, chipid; 1366 unsigned short divs; 1367 static u_char ctest[] = "\377\125\252\045\244\0\377"; 1368#define CTLEN 8 1369 1370 struct rtest { 1371 u_char txbuf[CD180_NFIFO]; /* TX buffer */ 1372 u_char rxbuf[CD180_NFIFO]; /* RX buffer */ 1373 int rxptr; /* RX pointer */ 1374 int txptr; /* TX pointer */ 1375 } tchans[CD180_NCHAN]; 1376 1377 old_level = spltty(); 1378 1379 chipid = RC_FAKEID; 1380 1381 /* First, reset board to inital state */ 1382 rc_hwreset(sc, chipid); 1383 1384 divs = RC_BRD(19200); 1385 1386 /* Initialize channels */ 1387 for (chan = 0; chan < CD180_NCHAN; chan++) { 1388 1389 /* Select and reset channel */ 1390 rcout(sc, CD180_CAR, chan); 1391 CCRCMD(sc, chan, CCR_ResetChan); 1392 WAITFORCCR(sc, chan); 1393 1394 /* Set speed */ 1395 rcout(sc, CD180_RBPRL, divs & 0xFF); 1396 rcout(sc, CD180_RBPRH, divs >> 8); 1397 rcout(sc, CD180_TBPRL, divs & 0xFF); 1398 rcout(sc, CD180_TBPRH, divs >> 8); 1399 1400 /* set timeout value */ 1401 rcout(sc, CD180_RTPR, 0); 1402 1403 /* Establish local loopback */ 1404 rcout(sc, CD180_COR1, COR1_NOPAR | COR1_8BITS | COR1_1SB); 1405 rcout(sc, CD180_COR2, COR2_LLM); 1406 rcout(sc, CD180_COR3, CD180_NFIFO); 1407 CCRCMD(sc, chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3); 1408 CCRCMD(sc, chan, CCR_RCVREN | CCR_XMTREN); 1409 WAITFORCCR(sc, chan); 1410 rcout(sc, CD180_MSVR, MSVR_RTS); 1411 1412 /* Fill TXBUF with test data */ 1413 for (i = 0; i < CD180_NFIFO; i++) { 1414 tchans[chan].txbuf[i] = ctest[i]; 1415 tchans[chan].rxbuf[i] = 0; 1416 } 1417 tchans[chan].txptr = tchans[chan].rxptr = 0; 1418 1419 /* Now, start transmit */ 1420 rcout(sc, CD180_IER, IER_TxMpty|IER_RxData); 1421 } 1422 /* Pseudo-interrupt poll stuff */ 1423 for (rcnt = 10000; rcnt-- > 0; rcnt--) { 1424 i = ~(rcin(sc, RC_BSR)); 1425 if (i & RC_BSR_TOUT) 1426 ERR(("BSR timeout bit set\n")); 1427 else if (i & RC_BSR_TXINT) { 1428 iack = rcin(sc, RC_PILR_TX); 1429 if (iack != (GIVR_IT_TDI | chipid)) 1430 ERR(("Bad TX intr ack (%02x != %02x)\n", 1431 iack, GIVR_IT_TDI | chipid)); 1432 chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH; 1433 /* If no more data to transmit, disable TX intr */ 1434 if (tchans[chan].txptr >= CD180_NFIFO) { 1435 iack = rcin(sc, CD180_IER); 1436 rcout(sc, CD180_IER, iack & ~IER_TxMpty); 1437 } else { 1438 for (iack = tchans[chan].txptr; 1439 iack < CD180_NFIFO; iack++) 1440 rcout(sc, CD180_TDR, 1441 tchans[chan].txbuf[iack]); 1442 tchans[chan].txptr = iack; 1443 } 1444 rcout(sc, CD180_EOIR, 0); 1445 } else if (i & RC_BSR_RXINT) { 1446 u_char ucnt; 1447 1448 iack = rcin(sc, RC_PILR_RX); 1449 if (iack != (GIVR_IT_RGDI | chipid) && 1450 iack != (GIVR_IT_REI | chipid)) 1451 ERR(("Bad RX intr ack (%02x != %02x)\n", 1452 iack, GIVR_IT_RGDI | chipid)); 1453 chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH; 1454 ucnt = rcin(sc, CD180_RDCR) & 0xF; 1455 while (ucnt-- > 0) { 1456 iack = rcin(sc, CD180_RCSR); 1457 if (iack & RCSR_Timeout) 1458 break; 1459 if (iack & 0xF) 1460 ERR(("Bad char chan %d (RCSR = %02X)\n", 1461 chan, iack)); 1462 if (tchans[chan].rxptr > CD180_NFIFO) 1463 ERR(("Got extra chars chan %d\n", 1464 chan)); 1465 tchans[chan].rxbuf[tchans[chan].rxptr++] = 1466 rcin(sc, CD180_RDR); 1467 } 1468 rcout(sc, CD180_EOIR, 0); 1469 } 1470 rcout(sc, RC_CTOUT, 0); 1471 for (iack = chan = 0; chan < CD180_NCHAN; chan++) 1472 if (tchans[chan].rxptr >= CD180_NFIFO) 1473 iack++; 1474 if (iack == CD180_NCHAN) 1475 break; 1476 } 1477 for (chan = 0; chan < CD180_NCHAN; chan++) { 1478 /* Select and reset channel */ 1479 rcout(sc, CD180_CAR, chan); 1480 CCRCMD(sc, chan, CCR_ResetChan); 1481 } 1482 1483 if (!rcnt) 1484 ERR(("looses characters during local loopback\n")); 1485 /* Now, check data */ 1486 for (chan = 0; chan < CD180_NCHAN; chan++) 1487 for (i = 0; i < CD180_NFIFO; i++) 1488 if (ctest[i] != tchans[chan].rxbuf[i]) 1489 ERR(("data mismatch chan %d ptr %d (%d != %d)\n", 1490 chan, i, ctest[i], tchans[chan].rxbuf[i])); 1491 (void) splx(old_level); 1492 return 0; 1493} 1494 1495#ifdef RCDEBUG 1496static void 1497printrcflags(struct rc_chans *rc, char *comment) 1498{ 1499 struct rc_softc *sc; 1500 u_short f = rc->rc_flags; 1501 1502 sc = rc->rc_rcb; 1503 printf("rc%d/%d: %s flags: %s%s%s%s%s%s%s%s%s%s%s%s\n", 1504 rc->rc_rcb->rcb_unit, rc->rc_chan, comment, 1505 (f & RC_DTR_OFF)?"DTR_OFF " :"", 1506 (f & RC_ACTOUT) ?"ACTOUT " :"", 1507 (f & RC_RTSFLOW)?"RTSFLOW " :"", 1508 (f & RC_CTSFLOW)?"CTSFLOW " :"", 1509 (f & RC_DORXFER)?"DORXFER " :"", 1510 (f & RC_DOXXFER)?"DOXXFER " :"", 1511 (f & RC_MODCHG) ?"MODCHG " :"", 1512 (f & RC_OSUSP) ?"OSUSP " :"", 1513 (f & RC_OSBUSY) ?"OSBUSY " :"", 1514 (f & RC_WAS_BUFOVFL) ?"BUFOVFL " :"", 1515 (f & RC_WAS_SILOVFL) ?"SILOVFL " :"", 1516 (f & RC_SEND_RDY) ?"SEND_RDY":""); 1517 1518 rcout(sc, CD180_CAR, rc->rc_chan); 1519 1520 printf("rc%d/%d: msvr %02x ier %02x ccsr %02x\n", 1521 rc->rc_rcb->rcb_unit, rc->rc_chan, 1522 rcin(sc, CD180_MSVR), 1523 rcin(sc, CD180_IER), 1524 rcin(sc, CD180_CCSR)); 1525} 1526#endif /* RCDEBUG */ 1527 1528static void 1529rc_dtrwakeup(void *arg) 1530{ 1531 struct rc_chans *rc; 1532 1533 rc = (struct rc_chans *)arg; 1534 rc->rc_flags &= ~RC_DTR_OFF; 1535 wakeup(&rc->rc_dtrwait); 1536} 1537 1538static void 1539rc_discard_output(struct rc_chans *rc) 1540{ 1541 critical_enter(); 1542 if (rc->rc_flags & RC_DOXXFER) { 1543 rc->rc_rcb->sc_scheduled_event -= LOTS_OF_EVENTS; 1544 rc->rc_flags &= ~RC_DOXXFER; 1545 } 1546 rc->rc_optr = rc->rc_obufend; 1547 rc->rc_tp.t_state &= ~TS_BUSY; 1548 critical_exit(); 1549 ttwwakeup(&rc->rc_tp); 1550} 1551 1552static void 1553disc_optim(struct tty *tp, struct termios *t, struct rc_chans *rc) 1554{ 1555 1556 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 1557 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 1558 && (!(t->c_iflag & PARMRK) 1559 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 1560 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 1561 && linesw[tp->t_line].l_rint == ttyinput) 1562 tp->t_state |= TS_CAN_BYPASS_L_RINT; 1563 else 1564 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 1565 rc->rc_hotchar = linesw[tp->t_line].l_hotchar; 1566} 1567 1568static void 1569rc_wait0(struct rc_softc *sc, int chan, int line) 1570{ 1571 int rcnt; 1572 1573 for (rcnt = 50; rcnt && rcin(sc, CD180_CCR); rcnt--) 1574 DELAY(30); 1575 if (rcnt == 0) 1576 device_printf(sc->sc_dev, 1577 "channel %d command timeout, rc.c line: %d\n", chan, line); 1578} 1579 1580static device_method_t rc_methods[] = { 1581 /* Device interface */ 1582 DEVMETHOD(device_probe, rc_probe), 1583 DEVMETHOD(device_attach, rc_attach), 1584 DEVMETHOD(device_detach, rc_detach), 1585 { 0, 0 } 1586}; 1587 1588static driver_t rc_driver = { 1589 "rc", 1590 rc_methods, sizeof(struct rc_softc), 1591}; 1592 1593DRIVER_MODULE(rc, isa, rc_driver, rc_devclass, 0, 0);
|