1 /* 2 * Copyright (C) 1995 by Pavel Antonov, Moscow, Russia. 3 * Copyright (C) 1995 by Andrey A. Chernov, Moscow, Russia. 4 * Copyright (C) 2002 by John Baldwin <[email protected]> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* 32 * SDL Communications Riscom/8 (based on Cirrus Logic CL-CD180) driver 33 * 34 */ 35 36 /*#define RCDEBUG*/ 37 38 #include "opt_tty.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/bus.h> 43 #include <sys/conf.h> 44 #include <sys/fcntl.h> 45 #include <sys/interrupt.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/module.h> 49 #include <sys/serial.h> 50 #include <sys/tty.h> 51 #include <machine/bus.h> 52 #include <machine/resource.h> 53 #include <sys/rman.h> 54 55 #include <dev/ic/cd180.h> 56 #include <dev/rc/rcreg.h> 57 #include <isa/isavar.h> 58 59 #define IOBASE_ADDRS 14 60 61 #define DEV_TO_RC(dev) (struct rc_chans *)((dev)->si_drv1) 62 #define TTY_TO_RC(tty) DEV_TO_RC((tty)->t_dev) 63 64 #define rcin(sc, port) RC_IN(sc, port) 65 #define rcout(sc, port, v) RC_OUT(sc, port, v) 66 67 #define WAITFORCCR(sc, chan) rc_wait0((sc), (chan), __LINE__) 68 69 #define CCRCMD(sc, chan, cmd) do { \ 70 WAITFORCCR((sc), (chan)); \ 71 rcout((sc), CD180_CCR, (cmd)); \ 72 } while (0) 73 74 #define RC_IBUFSIZE 256 75 #define RB_I_HIGH_WATER (TTYHOG - 2 * RC_IBUFSIZE) 76 #define RC_OBUFSIZE 512 77 #define RC_IHIGHWATER (3 * RC_IBUFSIZE / 4) 78 #define INPUT_FLAGS_SHIFT (2 * RC_IBUFSIZE) 79 #define LOTS_OF_EVENTS 64 80 81 #define RC_FAKEID 0x10 82 83 #define CALLOUT(dev) (((intptr_t)(dev)->si_drv2) != 0) 84 85 /* Per-channel structure */ 86 struct rc_chans { 87 struct rc_softc *rc_rcb; /* back ptr */ 88 struct cdev *rc_dev; /* non-callout device */ 89 struct cdev *rc_cdev; /* callout device */ 90 u_short rc_flags; /* Misc. flags */ 91 int rc_chan; /* Channel # */ 92 u_char rc_ier; /* intr. enable reg */ 93 u_char rc_msvr; /* modem sig. status */ 94 u_char rc_cor2; /* options reg */ 95 u_char rc_pendcmd; /* special cmd pending */ 96 u_int rc_dtrwait; /* dtr timeout */ 97 u_int rc_dcdwaits; /* how many waits DCD in open */ 98 struct tty rc_tp; /* tty struct */ 99 u_char *rc_iptr; /* Chars input buffer */ 100 u_char *rc_hiwat; /* hi-water mark */ 101 u_char *rc_bufend; /* end of buffer */ 102 u_char *rc_optr; /* ptr in output buf */ 103 u_char *rc_obufend; /* end of output buf */ 104 u_char rc_ibuf[4 * RC_IBUFSIZE]; /* input buffer */ 105 u_char rc_obuf[RC_OBUFSIZE]; /* output buffer */ 106 struct callout rc_dtrcallout; 107 }; 108 109 /* Per-board structure */ 110 struct rc_softc { 111 device_t sc_dev; 112 struct resource *sc_irq; 113 struct resource *sc_port[IOBASE_ADDRS]; 114 int sc_irqrid; 115 void *sc_hwicookie; 116 bus_space_tag_t sc_bt; 117 bus_space_handle_t sc_bh; 118 u_int sc_unit; /* unit # */ 119 u_char sc_dtr; /* DTR status */ 120 int sc_opencount; 121 int sc_scheduled_event; 122 void *sc_swicookie; 123 struct rc_chans sc_channels[CD180_NCHAN]; /* channels */ 124 }; 125 126 /* Static prototypes */ 127 static void rc_break(struct tty *, int); 128 static void rc_release_resources(device_t dev); 129 static void rc_intr(void *); 130 static void rc_hwreset(struct rc_softc *, unsigned int); 131 static int rc_test(struct rc_softc *); 132 static void rc_discard_output(struct rc_chans *); 133 static void rc_hardclose(struct rc_chans *); 134 static int rc_modem(struct tty *, int, int); 135 static void rc_start(struct tty *); 136 static void rc_stop(struct tty *, int rw); 137 static int rc_param(struct tty *, struct termios *); 138 static void rc_pollcard(void *); 139 static void rc_reinit(struct rc_softc *); 140 #ifdef RCDEBUG 141 static void printrcflags(); 142 #endif 143 static void rc_dtrwakeup(void *); 144 static void rc_wait0(struct rc_softc *sc, int chan, int line); 145 146 static d_open_t rcopen; 147 static d_close_t rcclose; 148 static d_ioctl_t rcioctl; 149 150 static struct cdevsw rc_cdevsw = { 151 .d_version = D_VERSION, 152 .d_open = rcopen, 153 .d_close = rcclose, 154 .d_ioctl = rcioctl, 155 .d_name = "rc", 156 .d_flags = D_TTY | D_NEEDGIANT, 157 }; 158 159 static devclass_t rc_devclass; 160 161 /* Flags */ 162 #define RC_DTR_OFF 0x0001 /* DTR wait, for close/open */ 163 #define RC_ACTOUT 0x0002 /* Dial-out port active */ 164 #define RC_RTSFLOW 0x0004 /* RTS flow ctl enabled */ 165 #define RC_CTSFLOW 0x0008 /* CTS flow ctl enabled */ 166 #define RC_DORXFER 0x0010 /* RXFER event planned */ 167 #define RC_DOXXFER 0x0020 /* XXFER event planned */ 168 #define RC_MODCHG 0x0040 /* Modem status changed */ 169 #define RC_OSUSP 0x0080 /* Output suspended */ 170 #define RC_OSBUSY 0x0100 /* start() routine in progress */ 171 #define RC_WAS_BUFOVFL 0x0200 /* low-level buffer ovferflow */ 172 #define RC_WAS_SILOVFL 0x0400 /* silo buffer overflow */ 173 #define RC_SEND_RDY 0x0800 /* ready to send */ 174 175 /* Table for translation of RCSR status bits to internal form */ 176 static int rc_rcsrt[16] = { 177 0, TTY_OE, TTY_FE, 178 TTY_FE|TTY_OE, TTY_PE, TTY_PE|TTY_OE, 179 TTY_PE|TTY_FE, TTY_PE|TTY_FE|TTY_OE, TTY_BI, 180 TTY_BI|TTY_OE, TTY_BI|TTY_FE, TTY_BI|TTY_FE|TTY_OE, 181 TTY_BI|TTY_PE, TTY_BI|TTY_PE|TTY_OE, TTY_BI|TTY_PE|TTY_FE, 182 TTY_BI|TTY_PE|TTY_FE|TTY_OE 183 }; 184 185 static int rc_ports[] = 186 { 0x220, 0x240, 0x250, 0x260, 0x2a0, 0x2b0, 0x300, 0x320 }; 187 static int iobase_addrs[IOBASE_ADDRS] = 188 { 0, 0x400, 0x800, 0xc00, 0x1400, 0x1800, 0x1c00, 0x2000, 189 0x3000, 0x3400, 0x3800, 0x3c00, 0x4000, 0x8000 }; 190 191 /**********************************************/ 192 193 static int 194 rc_probe(device_t dev) 195 { 196 u_int port; 197 int i, found; 198 199 /* 200 * We don't know of any PnP ID's for these cards. 201 */ 202 if (isa_get_logicalid(dev) != 0) 203 return (ENXIO); 204 205 /* 206 * We have to have an IO port hint that is valid. 207 */ 208 port = isa_get_port(dev); 209 if (port == -1) 210 return (ENXIO); 211 found = 0; 212 for (i = 0; i < sizeof(rc_ports) / sizeof(int); i++) 213 if (rc_ports[i] == port) { 214 found = 1; 215 break; 216 } 217 if (!found) 218 return (ENXIO); 219 220 /* 221 * We have to have an IRQ hint. 222 */ 223 if (isa_get_irq(dev) == -1) 224 return (ENXIO); 225 226 device_set_desc(dev, "SDL Riscom/8"); 227 return (0); 228 } 229 230 static int 231 rc_attach(device_t dev) 232 { 233 struct rc_chans *rc; 234 struct tty *tp; 235 struct rc_softc *sc; 236 u_int port; 237 int base, chan, error, i, x; 238 struct cdev *cdev; 239 240 sc = device_get_softc(dev); 241 sc->sc_dev = dev; 242 243 /* 244 * We need to have IO ports. Lots of them. We need 245 * the following ranges relative to the base port: 246 * 0x0 - 0x10 247 * 0x400 - 0x410 248 * 0x800 - 0x810 249 * 0xc00 - 0xc10 250 * 0x1400 - 0x1410 251 * 0x1800 - 0x1810 252 * 0x1c00 - 0x1c10 253 * 0x2000 - 0x2010 254 * 0x3000 - 0x3010 255 * 0x3400 - 0x3410 256 * 0x3800 - 0x3810 257 * 0x3c00 - 0x3c10 258 * 0x4000 - 0x4010 259 * 0x8000 - 0x8010 260 */ 261 port = isa_get_port(dev); 262 for (i = 0; i < IOBASE_ADDRS; i++) 263 if (bus_set_resource(dev, SYS_RES_IOPORT, i, 264 port + iobase_addrs[i], 0x10) != 0) 265 return (ENXIO); 266 error = ENOMEM; 267 for (i = 0; i < IOBASE_ADDRS; i++) { 268 x = i; 269 sc->sc_port[i] = bus_alloc_resource(dev, SYS_RES_IOPORT, &x, 270 0ul, ~0ul, 0x10, RF_ACTIVE); 271 if (x != i) { 272 device_printf(dev, "ioport %d was rid %d\n", i, x); 273 goto fail; 274 } 275 if (sc->sc_port[i] == NULL) { 276 device_printf(dev, "failed to alloc ioports %x-%x\n", 277 port + iobase_addrs[i], 278 port + iobase_addrs[i] + 0x10); 279 goto fail; 280 } 281 } 282 sc->sc_bt = rman_get_bustag(sc->sc_port[0]); 283 sc->sc_bh = rman_get_bushandle(sc->sc_port[0]); 284 285 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irqrid, 286 RF_ACTIVE); 287 if (sc->sc_irq == NULL) { 288 device_printf(dev, "failed to alloc IRQ\n"); 289 goto fail; 290 } 291 292 /* 293 * Now do some actual tests to make sure it works. 294 */ 295 error = ENXIO; 296 rcout(sc, CD180_PPRL, 0x22); /* Random values to Prescale reg. */ 297 rcout(sc, CD180_PPRH, 0x11); 298 if (rcin(sc, CD180_PPRL) != 0x22 || rcin(sc, CD180_PPRH) != 0x11) 299 goto fail; 300 if (rc_test(sc)) 301 goto fail; 302 303 /* 304 * Ok, start actually hooking things up. 305 */ 306 sc->sc_unit = device_get_unit(dev); 307 /*sc->sc_chipid = 0x10 + device_get_unit(dev);*/ 308 device_printf(dev, "%d chans, firmware rev. %c\n", 309 CD180_NCHAN, (rcin(sc, CD180_GFRCR) & 0xF) + 'A'); 310 rc = sc->sc_channels; 311 base = CD180_NCHAN * sc->sc_unit; 312 for (chan = 0; chan < CD180_NCHAN; chan++, rc++) { 313 rc->rc_rcb = sc; 314 rc->rc_chan = chan; 315 rc->rc_iptr = rc->rc_ibuf; 316 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE]; 317 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER]; 318 rc->rc_optr = rc->rc_obufend = rc->rc_obuf; 319 rc->rc_dtrwait = 3 * hz; 320 callout_init(&rc->rc_dtrcallout, 0); 321 tp = &rc->rc_tp; 322 ttychars(tp); 323 tp->t_lflag = tp->t_iflag = tp->t_oflag = 0; 324 tp->t_cflag = TTYDEF_CFLAG; 325 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 326 cdev = make_dev(&rc_cdevsw, chan + base, 327 UID_ROOT, GID_WHEEL, 0600, "ttym%d", chan + base); 328 cdev->si_drv1 = rc; 329 cdev->si_drv2 = 0; 330 cdev->si_tty = tp; 331 rc->rc_dev = cdev; 332 cdev = make_dev(&rc_cdevsw, chan + base + 128, 333 UID_UUCP, GID_DIALER, 0660, "cuam%d", chan + base); 334 cdev->si_drv1 = rc; 335 cdev->si_drv2 = (void *)1; 336 cdev->si_tty = tp; 337 rc->rc_cdev = cdev; 338 } 339 340 error = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_TTY, rc_intr, sc, 341 &sc->sc_hwicookie); 342 if (error) { 343 device_printf(dev, "failed to register interrupt handler\n"); 344 goto fail; 345 } 346 347 swi_add(&tty_ithd, "tty:rc", rc_pollcard, sc, SWI_TTY, 0, 348 &sc->sc_swicookie); 349 return (0); 350 351 fail: 352 rc_release_resources(dev); 353 return (error); 354 } 355 356 static int 357 rc_detach(device_t dev) 358 { 359 struct rc_softc *sc; 360 struct rc_chans *rc; 361 int error, i, s; 362 363 sc = device_get_softc(dev); 364 if (sc->sc_opencount > 0) 365 return (EBUSY); 366 sc->sc_opencount = -1; 367 368 rc = sc->sc_channels; 369 for (i = 0; i < CD180_NCHAN; i++, rc++) { 370 destroy_dev(rc->rc_dev); 371 destroy_dev(rc->rc_cdev); 372 } 373 374 rc = sc->sc_channels; 375 s = splsoftclock(); 376 for (i = 0; i < CD180_NCHAN; i++) { 377 if ((rc->rc_flags & RC_DTR_OFF) && 378 !callout_stop(&rc->rc_dtrcallout)) 379 tsleep(&rc->rc_dtrwait, TTIPRI, "rcdtrdet", 0); 380 } 381 382 error = bus_teardown_intr(dev, sc->sc_irq, sc->sc_hwicookie); 383 if (error) 384 device_printf(dev, "failed to deregister interrupt handler\n"); 385 ithread_remove_handler(sc->sc_swicookie); 386 rc_release_resources(dev); 387 388 return (0); 389 } 390 391 static void 392 rc_release_resources(device_t dev) 393 { 394 struct rc_softc *sc; 395 int i; 396 397 sc = device_get_softc(dev); 398 if (sc->sc_irq != NULL) { 399 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, 400 sc->sc_irq); 401 sc->sc_irq = NULL; 402 } 403 for (i = 0; i < IOBASE_ADDRS; i++) { 404 if (sc->sc_port[i] == NULL) 405 break; 406 bus_release_resource(dev, SYS_RES_IOPORT, i, sc->sc_port[i]); 407 sc->sc_port[i] = NULL; 408 } 409 } 410 411 /* RC interrupt handling */ 412 static void 413 rc_intr(void *arg) 414 { 415 struct rc_softc *sc; 416 struct rc_chans *rc; 417 int resid, chan; 418 u_char val, iack, bsr, ucnt, *optr; 419 int good_data, t_state; 420 421 sc = (struct rc_softc *)arg; 422 bsr = ~(rcin(sc, RC_BSR)); 423 if (!(bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT))) { 424 device_printf(sc->sc_dev, "extra interrupt\n"); 425 rcout(sc, CD180_EOIR, 0); 426 return; 427 } 428 429 while (bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT)) { 430 #ifdef RCDEBUG_DETAILED 431 device_printf(sc->sc_dev, "intr (%p) %s%s%s%s\n", arg, bsr, 432 (bsr & RC_BSR_TOUT)?"TOUT ":"", 433 (bsr & RC_BSR_RXINT)?"RXINT ":"", 434 (bsr & RC_BSR_TXINT)?"TXINT ":"", 435 (bsr & RC_BSR_MOINT)?"MOINT":""); 436 #endif 437 if (bsr & RC_BSR_TOUT) { 438 device_printf(sc->sc_dev, 439 "hardware failure, reset board\n"); 440 rcout(sc, RC_CTOUT, 0); 441 rc_reinit(sc); 442 return; 443 } 444 if (bsr & RC_BSR_RXINT) { 445 iack = rcin(sc, RC_PILR_RX); 446 good_data = (iack == (GIVR_IT_RGDI | RC_FAKEID)); 447 if (!good_data && iack != (GIVR_IT_REI | RC_FAKEID)) { 448 device_printf(sc->sc_dev, 449 "fake rxint: %02x\n", iack); 450 goto more_intrs; 451 } 452 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 453 rc = &sc->sc_channels[chan]; 454 t_state = rc->rc_tp.t_state; 455 /* Do RTS flow control stuff */ 456 if ( (rc->rc_flags & RC_RTSFLOW) 457 || !(t_state & TS_ISOPEN) 458 ) { 459 if ( ( !(t_state & TS_ISOPEN) 460 || (t_state & TS_TBLOCK) 461 ) 462 && (rc->rc_msvr & MSVR_RTS) 463 ) 464 rcout(sc, CD180_MSVR, 465 rc->rc_msvr &= ~MSVR_RTS); 466 else if (!(rc->rc_msvr & MSVR_RTS)) 467 rcout(sc, CD180_MSVR, 468 rc->rc_msvr |= MSVR_RTS); 469 } 470 ucnt = rcin(sc, CD180_RDCR) & 0xF; 471 resid = 0; 472 473 if (t_state & TS_ISOPEN) { 474 /* check for input buffer overflow */ 475 if ((rc->rc_iptr + ucnt) >= rc->rc_bufend) { 476 resid = ucnt; 477 ucnt = rc->rc_bufend - rc->rc_iptr; 478 resid -= ucnt; 479 if (!(rc->rc_flags & RC_WAS_BUFOVFL)) { 480 rc->rc_flags |= RC_WAS_BUFOVFL; 481 sc->sc_scheduled_event++; 482 } 483 } 484 optr = rc->rc_iptr; 485 /* check foor good data */ 486 if (good_data) { 487 while (ucnt-- > 0) { 488 val = rcin(sc, CD180_RDR); 489 optr[0] = val; 490 optr[INPUT_FLAGS_SHIFT] = 0; 491 optr++; 492 sc->sc_scheduled_event++; 493 if (val != 0 && val == rc->rc_tp.t_hotchar) 494 swi_sched(sc->sc_swicookie, 0); 495 } 496 } else { 497 /* Store also status data */ 498 while (ucnt-- > 0) { 499 iack = rcin(sc, CD180_RCSR); 500 if (iack & RCSR_Timeout) 501 break; 502 if ( (iack & RCSR_OE) 503 && !(rc->rc_flags & RC_WAS_SILOVFL)) { 504 rc->rc_flags |= RC_WAS_SILOVFL; 505 sc->sc_scheduled_event++; 506 } 507 val = rcin(sc, CD180_RDR); 508 /* 509 Don't store PE if IGNPAR and BREAK if IGNBRK, 510 this hack allows "raw" tty optimization 511 works even if IGN* is set. 512 */ 513 if ( !(iack & (RCSR_PE|RCSR_FE|RCSR_Break)) 514 || ((!(iack & (RCSR_PE|RCSR_FE)) 515 || !(rc->rc_tp.t_iflag & IGNPAR)) 516 && (!(iack & RCSR_Break) 517 || !(rc->rc_tp.t_iflag & IGNBRK)))) { 518 if ( (iack & (RCSR_PE|RCSR_FE)) 519 && (t_state & TS_CAN_BYPASS_L_RINT) 520 && ((iack & RCSR_FE) 521 || ((iack & RCSR_PE) 522 && (rc->rc_tp.t_iflag & INPCK)))) 523 val = 0; 524 else if (val != 0 && val == rc->rc_tp.t_hotchar) 525 swi_sched(sc->sc_swicookie, 0); 526 optr[0] = val; 527 optr[INPUT_FLAGS_SHIFT] = iack; 528 optr++; 529 sc->sc_scheduled_event++; 530 } 531 } 532 } 533 rc->rc_iptr = optr; 534 rc->rc_flags |= RC_DORXFER; 535 } else 536 resid = ucnt; 537 /* Clear FIFO if necessary */ 538 while (resid-- > 0) { 539 if (!good_data) 540 iack = rcin(sc, CD180_RCSR); 541 else 542 iack = 0; 543 if (iack & RCSR_Timeout) 544 break; 545 (void) rcin(sc, CD180_RDR); 546 } 547 goto more_intrs; 548 } 549 if (bsr & RC_BSR_MOINT) { 550 iack = rcin(sc, RC_PILR_MODEM); 551 if (iack != (GIVR_IT_MSCI | RC_FAKEID)) { 552 device_printf(sc->sc_dev, "fake moint: %02x\n", 553 iack); 554 goto more_intrs; 555 } 556 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 557 rc = &sc->sc_channels[chan]; 558 iack = rcin(sc, CD180_MCR); 559 rc->rc_msvr = rcin(sc, CD180_MSVR); 560 rcout(sc, CD180_MCR, 0); 561 #ifdef RCDEBUG 562 printrcflags(rc, "moint"); 563 #endif 564 if (rc->rc_flags & RC_CTSFLOW) { 565 if (rc->rc_msvr & MSVR_CTS) 566 rc->rc_flags |= RC_SEND_RDY; 567 else 568 rc->rc_flags &= ~RC_SEND_RDY; 569 } else 570 rc->rc_flags |= RC_SEND_RDY; 571 if ((iack & MCR_CDchg) && !(rc->rc_flags & RC_MODCHG)) { 572 sc->sc_scheduled_event += LOTS_OF_EVENTS; 573 rc->rc_flags |= RC_MODCHG; 574 swi_sched(sc->sc_swicookie, 0); 575 } 576 goto more_intrs; 577 } 578 if (bsr & RC_BSR_TXINT) { 579 iack = rcin(sc, RC_PILR_TX); 580 if (iack != (GIVR_IT_TDI | RC_FAKEID)) { 581 device_printf(sc->sc_dev, "fake txint: %02x\n", 582 iack); 583 goto more_intrs; 584 } 585 chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH); 586 rc = &sc->sc_channels[chan]; 587 if ( (rc->rc_flags & RC_OSUSP) 588 || !(rc->rc_flags & RC_SEND_RDY) 589 ) 590 goto more_intrs; 591 /* Handle breaks and other stuff */ 592 if (rc->rc_pendcmd) { 593 rcout(sc, CD180_COR2, rc->rc_cor2 |= COR2_ETC); 594 rcout(sc, CD180_TDR, CD180_C_ESC); 595 rcout(sc, CD180_TDR, rc->rc_pendcmd); 596 rcout(sc, CD180_COR2, rc->rc_cor2 &= ~COR2_ETC); 597 rc->rc_pendcmd = 0; 598 goto more_intrs; 599 } 600 optr = rc->rc_optr; 601 resid = rc->rc_obufend - optr; 602 if (resid > CD180_NFIFO) 603 resid = CD180_NFIFO; 604 while (resid-- > 0) 605 rcout(sc, CD180_TDR, *optr++); 606 rc->rc_optr = optr; 607 608 /* output completed? */ 609 if (optr >= rc->rc_obufend) { 610 rcout(sc, CD180_IER, rc->rc_ier &= ~IER_TxRdy); 611 #ifdef RCDEBUG 612 device_printf(sc->sc_dev, 613 "channel %d: output completed\n", 614 rc->rc_chan); 615 #endif 616 if (!(rc->rc_flags & RC_DOXXFER)) { 617 sc->sc_scheduled_event += LOTS_OF_EVENTS; 618 rc->rc_flags |= RC_DOXXFER; 619 swi_sched(sc->sc_swicookie, 0); 620 } 621 } 622 } 623 more_intrs: 624 rcout(sc, CD180_EOIR, 0); /* end of interrupt */ 625 rcout(sc, RC_CTOUT, 0); 626 bsr = ~(rcin(sc, RC_BSR)); 627 } 628 } 629 630 /* Feed characters to output buffer */ 631 static void 632 rc_start(struct tty *tp) 633 { 634 struct rc_softc *sc; 635 struct rc_chans *rc; 636 int s; 637 638 rc = TTY_TO_RC(tp); 639 if (rc->rc_flags & RC_OSBUSY) 640 return; 641 sc = rc->rc_rcb; 642 s = spltty(); 643 rc->rc_flags |= RC_OSBUSY; 644 critical_enter(); 645 if (tp->t_state & TS_TTSTOP) 646 rc->rc_flags |= RC_OSUSP; 647 else 648 rc->rc_flags &= ~RC_OSUSP; 649 /* Do RTS flow control stuff */ 650 if ( (rc->rc_flags & RC_RTSFLOW) 651 && (tp->t_state & TS_TBLOCK) 652 && (rc->rc_msvr & MSVR_RTS) 653 ) { 654 rcout(sc, CD180_CAR, rc->rc_chan); 655 rcout(sc, CD180_MSVR, rc->rc_msvr &= ~MSVR_RTS); 656 } else if (!(rc->rc_msvr & MSVR_RTS)) { 657 rcout(sc, CD180_CAR, rc->rc_chan); 658 rcout(sc, CD180_MSVR, rc->rc_msvr |= MSVR_RTS); 659 } 660 critical_exit(); 661 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP)) 662 goto out; 663 #ifdef RCDEBUG 664 printrcflags(rc, "rcstart"); 665 #endif 666 ttwwakeup(tp); 667 #ifdef RCDEBUG 668 printf("rcstart: outq = %d obuf = %d\n", 669 tp->t_outq.c_cc, rc->rc_obufend - rc->rc_optr); 670 #endif 671 if (tp->t_state & TS_BUSY) 672 goto out; /* output still in progress ... */ 673 674 if (tp->t_outq.c_cc > 0) { 675 u_int ocnt; 676 677 tp->t_state |= TS_BUSY; 678 ocnt = q_to_b(&tp->t_outq, rc->rc_obuf, sizeof rc->rc_obuf); 679 critical_enter(); 680 rc->rc_optr = rc->rc_obuf; 681 rc->rc_obufend = rc->rc_optr + ocnt; 682 critical_exit(); 683 if (!(rc->rc_ier & IER_TxRdy)) { 684 #ifdef RCDEBUG 685 device_printf(sc->sc_dev, 686 "channel %d: rcstart enable txint\n", rc->rc_chan); 687 #endif 688 rcout(sc, CD180_CAR, rc->rc_chan); 689 rcout(sc, CD180_IER, rc->rc_ier |= IER_TxRdy); 690 } 691 } 692 out: 693 rc->rc_flags &= ~RC_OSBUSY; 694 (void) splx(s); 695 } 696 697 /* Handle delayed events. */ 698 void 699 rc_pollcard(void *arg) 700 { 701 struct rc_softc *sc; 702 struct rc_chans *rc; 703 struct tty *tp; 704 u_char *tptr, *eptr; 705 int chan, icnt; 706 707 sc = (struct rc_softc *)arg; 708 if (sc->sc_scheduled_event == 0) 709 return; 710 do { 711 rc = sc->sc_channels; 712 for (chan = 0; chan < CD180_NCHAN; rc++, chan++) { 713 tp = &rc->rc_tp; 714 #ifdef RCDEBUG 715 if (rc->rc_flags & (RC_DORXFER|RC_DOXXFER|RC_MODCHG| 716 RC_WAS_BUFOVFL|RC_WAS_SILOVFL)) 717 printrcflags(rc, "rcevent"); 718 #endif 719 if (rc->rc_flags & RC_WAS_BUFOVFL) { 720 critical_enter(); 721 rc->rc_flags &= ~RC_WAS_BUFOVFL; 722 sc->sc_scheduled_event--; 723 critical_exit(); 724 device_printf(sc->sc_dev, 725 "channel %d: interrupt-level buffer overflow\n", 726 chan); 727 } 728 if (rc->rc_flags & RC_WAS_SILOVFL) { 729 critical_enter(); 730 rc->rc_flags &= ~RC_WAS_SILOVFL; 731 sc->sc_scheduled_event--; 732 critical_exit(); 733 device_printf(sc->sc_dev, 734 "channel %d: silo overflow\n", chan); 735 } 736 if (rc->rc_flags & RC_MODCHG) { 737 critical_enter(); 738 rc->rc_flags &= ~RC_MODCHG; 739 sc->sc_scheduled_event -= LOTS_OF_EVENTS; 740 critical_exit(); 741 ttyld_modem(tp, !!(rc->rc_msvr & MSVR_CD)); 742 } 743 if (rc->rc_flags & RC_DORXFER) { 744 critical_enter(); 745 rc->rc_flags &= ~RC_DORXFER; 746 eptr = rc->rc_iptr; 747 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) 748 tptr = &rc->rc_ibuf[RC_IBUFSIZE]; 749 else 750 tptr = rc->rc_ibuf; 751 icnt = eptr - tptr; 752 if (icnt > 0) { 753 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) { 754 rc->rc_iptr = rc->rc_ibuf; 755 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE]; 756 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER]; 757 } else { 758 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE]; 759 rc->rc_bufend = &rc->rc_ibuf[2 * RC_IBUFSIZE]; 760 rc->rc_hiwat = 761 &rc->rc_ibuf[RC_IBUFSIZE + RC_IHIGHWATER]; 762 } 763 if ( (rc->rc_flags & RC_RTSFLOW) 764 && (tp->t_state & TS_ISOPEN) 765 && !(tp->t_state & TS_TBLOCK) 766 && !(rc->rc_msvr & MSVR_RTS) 767 ) { 768 rcout(sc, CD180_CAR, chan); 769 rcout(sc, CD180_MSVR, 770 rc->rc_msvr |= MSVR_RTS); 771 } 772 sc->sc_scheduled_event -= icnt; 773 } 774 critical_exit(); 775 776 if (icnt <= 0 || !(tp->t_state & TS_ISOPEN)) 777 goto done1; 778 779 if ( (tp->t_state & TS_CAN_BYPASS_L_RINT) 780 && !(tp->t_state & TS_LOCAL)) { 781 if ((tp->t_rawq.c_cc + icnt) >= RB_I_HIGH_WATER 782 && ((rc->rc_flags & RC_RTSFLOW) || (tp->t_iflag & IXOFF)) 783 && !(tp->t_state & TS_TBLOCK)) 784 ttyblock(tp); 785 tk_nin += icnt; 786 tk_rawcc += icnt; 787 tp->t_rawcc += icnt; 788 if (b_to_q(tptr, icnt, &tp->t_rawq)) 789 device_printf(sc->sc_dev, 790 "channel %d: tty-level buffer overflow\n", 791 chan); 792 ttwakeup(tp); 793 if ((tp->t_state & TS_TTSTOP) && ((tp->t_iflag & IXANY) 794 || (tp->t_cc[VSTART] == tp->t_cc[VSTOP]))) { 795 tp->t_state &= ~TS_TTSTOP; 796 tp->t_lflag &= ~FLUSHO; 797 rc_start(tp); 798 } 799 } else { 800 for (; tptr < eptr; tptr++) 801 ttyld_rint(tp, 802 (tptr[0] | 803 rc_rcsrt[tptr[INPUT_FLAGS_SHIFT] & 0xF])); 804 } 805 done1: ; 806 } 807 if (rc->rc_flags & RC_DOXXFER) { 808 critical_enter(); 809 sc->sc_scheduled_event -= LOTS_OF_EVENTS; 810 rc->rc_flags &= ~RC_DOXXFER; 811 rc->rc_tp.t_state &= ~TS_BUSY; 812 critical_exit(); 813 ttyld_start(tp); 814 } 815 if (sc->sc_scheduled_event == 0) 816 break; 817 } 818 } while (sc->sc_scheduled_event >= LOTS_OF_EVENTS); 819 } 820 821 static void 822 rc_stop(struct tty *tp, int rw) 823 { 824 struct rc_softc *sc; 825 struct rc_chans *rc; 826 u_char *tptr, *eptr; 827 828 rc = TTY_TO_RC(tp); 829 sc = rc->rc_rcb; 830 #ifdef RCDEBUG 831 device_printf(sc->sc_dev, "channel %d: rc_stop %s%s\n", 832 rc->rc_chan, (rw & FWRITE)?"FWRITE ":"", (rw & FREAD)?"FREAD":""); 833 #endif 834 if (rw & FWRITE) 835 rc_discard_output(rc); 836 critical_enter(); 837 if (rw & FREAD) { 838 rc->rc_flags &= ~RC_DORXFER; 839 eptr = rc->rc_iptr; 840 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) { 841 tptr = &rc->rc_ibuf[RC_IBUFSIZE]; 842 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE]; 843 } else { 844 tptr = rc->rc_ibuf; 845 rc->rc_iptr = rc->rc_ibuf; 846 } 847 sc->sc_scheduled_event -= eptr - tptr; 848 } 849 if (tp->t_state & TS_TTSTOP) 850 rc->rc_flags |= RC_OSUSP; 851 else 852 rc->rc_flags &= ~RC_OSUSP; 853 critical_exit(); 854 } 855 856 static int 857 rcopen(struct cdev *dev, int flag, int mode, d_thread_t *td) 858 { 859 struct rc_softc *sc; 860 struct rc_chans *rc; 861 struct tty *tp; 862 int s, error = 0; 863 864 rc = DEV_TO_RC(dev); 865 sc = rc->rc_rcb; 866 tp = &rc->rc_tp; 867 if (sc->sc_opencount < 0) 868 return (ENXIO); 869 sc->sc_opencount++; 870 #ifdef RCDEBUG 871 device_printf(sc->sc_dev, "channel %d: rcopen: dev %p\n", 872 rc->rc_chan, dev); 873 #endif 874 s = spltty(); 875 876 again: 877 while (rc->rc_flags & RC_DTR_OFF) { 878 error = tsleep(&(rc->rc_dtrwait), TTIPRI | PCATCH, "rcdtr", 0); 879 if (error != 0) 880 goto out; 881 } 882 if (tp->t_state & TS_ISOPEN) { 883 if (CALLOUT(dev)) { 884 if (!(rc->rc_flags & RC_ACTOUT)) { 885 error = EBUSY; 886 goto out; 887 } 888 } else { 889 if (rc->rc_flags & RC_ACTOUT) { 890 if (flag & O_NONBLOCK) { 891 error = EBUSY; 892 goto out; 893 } 894 error = tsleep(&rc->rc_rcb, 895 TTIPRI|PCATCH, "rcbi", 0); 896 if (error) 897 goto out; 898 goto again; 899 } 900 } 901 if (tp->t_state & TS_XCLUDE && 902 suser(td)) { 903 error = EBUSY; 904 goto out; 905 } 906 } else { 907 tp->t_oproc = rc_start; 908 tp->t_param = rc_param; 909 tp->t_modem = rc_modem; 910 tp->t_break = rc_break; 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_modem(tp, SER_DTR | SER_RTS, 0); 923 924 if ((rc->rc_msvr & MSVR_CD) || CALLOUT(dev)) 925 ttyld_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 = ttyld_open(tp, dev); 937 ttyldoptim(tp); 938 if ((tp->t_state & TS_ISOPEN) && CALLOUT(dev)) 939 rc->rc_flags |= RC_ACTOUT; 940 out: 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 949 static int 950 rcclose(struct cdev *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 ttyld_close(tp, flag); 966 ttyldoptim(tp); 967 rc_hardclose(rc); 968 ttyclose(tp); 969 splx(s); 970 KASSERT(sc->sc_opencount > 0, ("rcclose: non-positive open count")); 971 sc->sc_opencount--; 972 return 0; 973 } 974 975 static void 976 rc_hardclose(struct rc_chans *rc) 977 { 978 struct rc_softc *sc; 979 struct tty *tp; 980 int s; 981 982 tp = &rc->rc_tp; 983 sc = rc->rc_rcb; 984 s = spltty(); 985 rcout(sc, CD180_CAR, rc->rc_chan); 986 987 /* Disable rx/tx intrs */ 988 rcout(sc, CD180_IER, rc->rc_ier = 0); 989 if ( (tp->t_cflag & HUPCL) 990 || (!(rc->rc_flags & RC_ACTOUT) 991 && !(rc->rc_msvr & MSVR_CD) 992 && !(tp->t_cflag & CLOCAL)) 993 || !(tp->t_state & TS_ISOPEN) 994 ) { 995 CCRCMD(sc, rc->rc_chan, CCR_ResetChan); 996 WAITFORCCR(sc, rc->rc_chan); 997 (void) rc_modem(tp, SER_RTS, 0); 998 if (rc->rc_dtrwait) { 999 callout_reset(&rc->rc_dtrcallout, rc->rc_dtrwait, 1000 rc_dtrwakeup, rc); 1001 rc->rc_flags |= RC_DTR_OFF; 1002 } 1003 } 1004 rc->rc_flags &= ~RC_ACTOUT; 1005 wakeup( &rc->rc_rcb); /* wake bi */ 1006 wakeup(TSA_CARR_ON(tp)); 1007 (void) splx(s); 1008 } 1009 1010 /* Reset the bastard */ 1011 static void 1012 rc_hwreset(struct rc_softc *sc, u_int chipid) 1013 { 1014 CCRCMD(sc, -1, CCR_HWRESET); /* Hardware reset */ 1015 DELAY(20000); 1016 WAITFORCCR(sc, -1); 1017 1018 rcout(sc, RC_CTOUT, 0); /* Clear timeout */ 1019 rcout(sc, CD180_GIVR, chipid); 1020 rcout(sc, CD180_GICR, 0); 1021 1022 /* Set Prescaler Registers (1 msec) */ 1023 rcout(sc, CD180_PPRL, ((RC_OSCFREQ + 999) / 1000) & 0xFF); 1024 rcout(sc, CD180_PPRH, ((RC_OSCFREQ + 999) / 1000) >> 8); 1025 1026 /* Initialize Priority Interrupt Level Registers */ 1027 rcout(sc, CD180_PILR1, RC_PILR_MODEM); 1028 rcout(sc, CD180_PILR2, RC_PILR_TX); 1029 rcout(sc, CD180_PILR3, RC_PILR_RX); 1030 1031 /* Reset DTR */ 1032 rcout(sc, RC_DTREG, ~0); 1033 } 1034 1035 /* Set channel parameters */ 1036 static int 1037 rc_param(struct tty *tp, struct termios *ts) 1038 { 1039 struct rc_softc *sc; 1040 struct rc_chans *rc; 1041 int idivs, odivs, s, val, cflag, iflag, lflag, inpflow; 1042 1043 if ( ts->c_ospeed < 0 || ts->c_ospeed > 76800 1044 || ts->c_ispeed < 0 || ts->c_ispeed > 76800 1045 ) 1046 return (EINVAL); 1047 if (ts->c_ispeed == 0) 1048 ts->c_ispeed = ts->c_ospeed; 1049 odivs = RC_BRD(ts->c_ospeed); 1050 idivs = RC_BRD(ts->c_ispeed); 1051 1052 rc = TTY_TO_RC(tp); 1053 sc = rc->rc_rcb; 1054 s = spltty(); 1055 1056 /* Select channel */ 1057 rcout(sc, CD180_CAR, rc->rc_chan); 1058 1059 /* If speed == 0, hangup line */ 1060 if (ts->c_ospeed == 0) { 1061 CCRCMD(sc, rc->rc_chan, CCR_ResetChan); 1062 WAITFORCCR(sc, rc->rc_chan); 1063 (void) rc_modem(tp, 0, SER_DTR); 1064 } 1065 1066 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 1067 cflag = ts->c_cflag; 1068 iflag = ts->c_iflag; 1069 lflag = ts->c_lflag; 1070 1071 if (idivs > 0) { 1072 rcout(sc, CD180_RBPRL, idivs & 0xFF); 1073 rcout(sc, CD180_RBPRH, idivs >> 8); 1074 } 1075 if (odivs > 0) { 1076 rcout(sc, CD180_TBPRL, odivs & 0xFF); 1077 rcout(sc, CD180_TBPRH, odivs >> 8); 1078 } 1079 1080 /* set timeout value */ 1081 if (ts->c_ispeed > 0) { 1082 int itm = ts->c_ispeed > 2400 ? 5 : 10000 / ts->c_ispeed + 1; 1083 1084 if ( !(lflag & ICANON) 1085 && ts->c_cc[VMIN] != 0 && ts->c_cc[VTIME] != 0 1086 && ts->c_cc[VTIME] * 10 > itm) 1087 itm = ts->c_cc[VTIME] * 10; 1088 1089 rcout(sc, CD180_RTPR, itm <= 255 ? itm : 255); 1090 } 1091 1092 switch (cflag & CSIZE) { 1093 case CS5: val = COR1_5BITS; break; 1094 case CS6: val = COR1_6BITS; break; 1095 case CS7: val = COR1_7BITS; break; 1096 default: 1097 case CS8: val = COR1_8BITS; break; 1098 } 1099 if (cflag & PARENB) { 1100 val |= COR1_NORMPAR; 1101 if (cflag & PARODD) 1102 val |= COR1_ODDP; 1103 if (!(cflag & INPCK)) 1104 val |= COR1_Ignore; 1105 } else 1106 val |= COR1_Ignore; 1107 if (cflag & CSTOPB) 1108 val |= COR1_2SB; 1109 rcout(sc, CD180_COR1, val); 1110 1111 /* Set FIFO threshold */ 1112 val = ts->c_ospeed <= 4800 ? 1 : CD180_NFIFO / 2; 1113 inpflow = 0; 1114 if ( (iflag & IXOFF) 1115 && ( ts->c_cc[VSTOP] != _POSIX_VDISABLE 1116 && ( ts->c_cc[VSTART] != _POSIX_VDISABLE 1117 || (iflag & IXANY) 1118 ) 1119 ) 1120 ) { 1121 inpflow = 1; 1122 val |= COR3_SCDE|COR3_FCT; 1123 } 1124 rcout(sc, CD180_COR3, val); 1125 1126 /* Initialize on-chip automatic flow control */ 1127 val = 0; 1128 rc->rc_flags &= ~(RC_CTSFLOW|RC_SEND_RDY); 1129 if (cflag & CCTS_OFLOW) { 1130 rc->rc_flags |= RC_CTSFLOW; 1131 val |= COR2_CtsAE; 1132 } else 1133 rc->rc_flags |= RC_SEND_RDY; 1134 if (tp->t_state & TS_TTSTOP) 1135 rc->rc_flags |= RC_OSUSP; 1136 else 1137 rc->rc_flags &= ~RC_OSUSP; 1138 if (cflag & CRTS_IFLOW) 1139 rc->rc_flags |= RC_RTSFLOW; 1140 else 1141 rc->rc_flags &= ~RC_RTSFLOW; 1142 1143 if (inpflow) { 1144 if (ts->c_cc[VSTART] != _POSIX_VDISABLE) 1145 rcout(sc, CD180_SCHR1, ts->c_cc[VSTART]); 1146 rcout(sc, CD180_SCHR2, ts->c_cc[VSTOP]); 1147 val |= COR2_TxIBE; 1148 if (iflag & IXANY) 1149 val |= COR2_IXM; 1150 } 1151 1152 rcout(sc, CD180_COR2, rc->rc_cor2 = val); 1153 1154 CCRCMD(sc, rc->rc_chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3); 1155 1156 ttyldoptim(tp); 1157 1158 /* modem ctl */ 1159 val = cflag & CLOCAL ? 0 : MCOR1_CDzd; 1160 if (cflag & CCTS_OFLOW) 1161 val |= MCOR1_CTSzd; 1162 rcout(sc, CD180_MCOR1, val); 1163 1164 val = cflag & CLOCAL ? 0 : MCOR2_CDod; 1165 if (cflag & CCTS_OFLOW) 1166 val |= MCOR2_CTSod; 1167 rcout(sc, CD180_MCOR2, val); 1168 1169 /* enable i/o and interrupts */ 1170 CCRCMD(sc, rc->rc_chan, 1171 CCR_XMTREN | ((cflag & CREAD) ? CCR_RCVREN : CCR_RCVRDIS)); 1172 WAITFORCCR(sc, rc->rc_chan); 1173 1174 rc->rc_ier = cflag & CLOCAL ? 0 : IER_CD; 1175 if (cflag & CCTS_OFLOW) 1176 rc->rc_ier |= IER_CTS; 1177 if (cflag & CREAD) 1178 rc->rc_ier |= IER_RxData; 1179 if (tp->t_state & TS_BUSY) 1180 rc->rc_ier |= IER_TxRdy; 1181 if (ts->c_ospeed != 0) 1182 rc_modem(tp, SER_DTR, 0); 1183 if ((cflag & CCTS_OFLOW) && (rc->rc_msvr & MSVR_CTS)) 1184 rc->rc_flags |= RC_SEND_RDY; 1185 rcout(sc, CD180_IER, rc->rc_ier); 1186 (void) splx(s); 1187 return 0; 1188 } 1189 1190 /* Re-initialize board after bogus interrupts */ 1191 static void 1192 rc_reinit(struct rc_softc *sc) 1193 { 1194 struct rc_chans *rc; 1195 int i; 1196 1197 rc_hwreset(sc, RC_FAKEID); 1198 rc = sc->sc_channels; 1199 for (i = 0; i < CD180_NCHAN; i++, rc++) 1200 (void) rc_param(&rc->rc_tp, &rc->rc_tp.t_termios); 1201 } 1202 1203 static int 1204 rcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, d_thread_t *td) 1205 { 1206 struct rc_chans *rc; 1207 struct tty *tp; 1208 int s, error; 1209 1210 rc = DEV_TO_RC(dev); 1211 tp = &rc->rc_tp; 1212 error = ttyioctl(dev, cmd, data, flag, td); 1213 ttyldoptim(tp); 1214 if (error != ENOTTY) 1215 return (error); 1216 s = spltty(); 1217 1218 switch (cmd) { 1219 case TIOCMSDTRWAIT: 1220 error = suser(td); 1221 if (error != 0) { 1222 splx(s); 1223 return (error); 1224 } 1225 rc->rc_dtrwait = *(int *)data * hz / 100; 1226 break; 1227 1228 case TIOCMGDTRWAIT: 1229 *(int *)data = rc->rc_dtrwait * 100 / hz; 1230 break; 1231 1232 default: 1233 (void) splx(s); 1234 return ENOTTY; 1235 } 1236 (void) splx(s); 1237 return 0; 1238 } 1239 1240 1241 /* Modem control routines */ 1242 1243 static int 1244 rc_modem(struct tty *tp, int biton, int bitoff) 1245 { 1246 struct rc_chans *rc; 1247 struct rc_softc *sc; 1248 u_char *dtr; 1249 u_char msvr; 1250 1251 rc = DEV_TO_RC(tp->t_dev); 1252 sc = rc->rc_rcb; 1253 dtr = &sc->sc_dtr; 1254 rcout(sc, CD180_CAR, rc->rc_chan); 1255 1256 if (biton == 0 && bitoff == 0) { 1257 msvr = rc->rc_msvr = rcin(sc, CD180_MSVR); 1258 1259 if (msvr & MSVR_RTS) 1260 biton |= SER_RTS; 1261 if (msvr & MSVR_CTS) 1262 biton |= SER_CTS; 1263 if (msvr & MSVR_DSR) 1264 biton |= SER_DSR; 1265 if (msvr & MSVR_DTR) 1266 biton |= SER_DTR; 1267 if (msvr & MSVR_CD) 1268 biton |= SER_DCD; 1269 if (~rcin(sc, RC_RIREG) & (1 << rc->rc_chan)) 1270 biton |= SER_RI; 1271 return biton; 1272 } 1273 if (biton & SER_DTR) 1274 rcout(sc, RC_DTREG, ~(*dtr |= 1 << rc->rc_chan)); 1275 if (bitoff & SER_DTR) 1276 rcout(sc, RC_DTREG, ~(*dtr &= ~(1 << rc->rc_chan))); 1277 msvr = rcin(sc, CD180_MSVR); 1278 if (biton & SER_DTR) 1279 msvr |= MSVR_DTR; 1280 if (bitoff & SER_DTR) 1281 msvr &= ~MSVR_DTR; 1282 if (biton & SER_RTS) 1283 msvr |= MSVR_RTS; 1284 if (bitoff & SER_RTS) 1285 msvr &= ~MSVR_RTS; 1286 rcout(sc, CD180_MSVR, msvr); 1287 return 0; 1288 } 1289 1290 static void 1291 rc_break(struct tty *tp, int brk) 1292 { 1293 struct rc_chans *rc; 1294 1295 rc = DEV_TO_RC(tp->t_dev); 1296 1297 if (brk) 1298 rc->rc_pendcmd = CD180_C_SBRK; 1299 else 1300 rc->rc_pendcmd = CD180_C_EBRK; 1301 } 1302 1303 #define ERR(s) do { \ 1304 device_printf(sc->sc_dev, "%s", ""); \ 1305 printf s ; \ 1306 printf("\n"); \ 1307 (void) splx(old_level); \ 1308 return 1; \ 1309 } while (0) 1310 1311 /* Test the board. */ 1312 int 1313 rc_test(struct rc_softc *sc) 1314 { 1315 int chan = 0; 1316 int i = 0, rcnt, old_level; 1317 unsigned int iack, chipid; 1318 unsigned short divs; 1319 static u_char ctest[] = "\377\125\252\045\244\0\377"; 1320 #define CTLEN 8 1321 1322 struct rtest { 1323 u_char txbuf[CD180_NFIFO]; /* TX buffer */ 1324 u_char rxbuf[CD180_NFIFO]; /* RX buffer */ 1325 int rxptr; /* RX pointer */ 1326 int txptr; /* TX pointer */ 1327 } tchans[CD180_NCHAN]; 1328 1329 old_level = spltty(); 1330 1331 chipid = RC_FAKEID; 1332 1333 /* First, reset board to inital state */ 1334 rc_hwreset(sc, chipid); 1335 1336 divs = RC_BRD(19200); 1337 1338 /* Initialize channels */ 1339 for (chan = 0; chan < CD180_NCHAN; chan++) { 1340 1341 /* Select and reset channel */ 1342 rcout(sc, CD180_CAR, chan); 1343 CCRCMD(sc, chan, CCR_ResetChan); 1344 WAITFORCCR(sc, chan); 1345 1346 /* Set speed */ 1347 rcout(sc, CD180_RBPRL, divs & 0xFF); 1348 rcout(sc, CD180_RBPRH, divs >> 8); 1349 rcout(sc, CD180_TBPRL, divs & 0xFF); 1350 rcout(sc, CD180_TBPRH, divs >> 8); 1351 1352 /* set timeout value */ 1353 rcout(sc, CD180_RTPR, 0); 1354 1355 /* Establish local loopback */ 1356 rcout(sc, CD180_COR1, COR1_NOPAR | COR1_8BITS | COR1_1SB); 1357 rcout(sc, CD180_COR2, COR2_LLM); 1358 rcout(sc, CD180_COR3, CD180_NFIFO); 1359 CCRCMD(sc, chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3); 1360 CCRCMD(sc, chan, CCR_RCVREN | CCR_XMTREN); 1361 WAITFORCCR(sc, chan); 1362 rcout(sc, CD180_MSVR, MSVR_RTS); 1363 1364 /* Fill TXBUF with test data */ 1365 for (i = 0; i < CD180_NFIFO; i++) { 1366 tchans[chan].txbuf[i] = ctest[i]; 1367 tchans[chan].rxbuf[i] = 0; 1368 } 1369 tchans[chan].txptr = tchans[chan].rxptr = 0; 1370 1371 /* Now, start transmit */ 1372 rcout(sc, CD180_IER, IER_TxMpty|IER_RxData); 1373 } 1374 /* Pseudo-interrupt poll stuff */ 1375 for (rcnt = 10000; rcnt-- > 0; rcnt--) { 1376 i = ~(rcin(sc, RC_BSR)); 1377 if (i & RC_BSR_TOUT) 1378 ERR(("BSR timeout bit set\n")); 1379 else if (i & RC_BSR_TXINT) { 1380 iack = rcin(sc, RC_PILR_TX); 1381 if (iack != (GIVR_IT_TDI | chipid)) 1382 ERR(("Bad TX intr ack (%02x != %02x)\n", 1383 iack, GIVR_IT_TDI | chipid)); 1384 chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH; 1385 /* If no more data to transmit, disable TX intr */ 1386 if (tchans[chan].txptr >= CD180_NFIFO) { 1387 iack = rcin(sc, CD180_IER); 1388 rcout(sc, CD180_IER, iack & ~IER_TxMpty); 1389 } else { 1390 for (iack = tchans[chan].txptr; 1391 iack < CD180_NFIFO; iack++) 1392 rcout(sc, CD180_TDR, 1393 tchans[chan].txbuf[iack]); 1394 tchans[chan].txptr = iack; 1395 } 1396 rcout(sc, CD180_EOIR, 0); 1397 } else if (i & RC_BSR_RXINT) { 1398 u_char ucnt; 1399 1400 iack = rcin(sc, RC_PILR_RX); 1401 if (iack != (GIVR_IT_RGDI | chipid) && 1402 iack != (GIVR_IT_REI | chipid)) 1403 ERR(("Bad RX intr ack (%02x != %02x)\n", 1404 iack, GIVR_IT_RGDI | chipid)); 1405 chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH; 1406 ucnt = rcin(sc, CD180_RDCR) & 0xF; 1407 while (ucnt-- > 0) { 1408 iack = rcin(sc, CD180_RCSR); 1409 if (iack & RCSR_Timeout) 1410 break; 1411 if (iack & 0xF) 1412 ERR(("Bad char chan %d (RCSR = %02X)\n", 1413 chan, iack)); 1414 if (tchans[chan].rxptr > CD180_NFIFO) 1415 ERR(("Got extra chars chan %d\n", 1416 chan)); 1417 tchans[chan].rxbuf[tchans[chan].rxptr++] = 1418 rcin(sc, CD180_RDR); 1419 } 1420 rcout(sc, CD180_EOIR, 0); 1421 } 1422 rcout(sc, RC_CTOUT, 0); 1423 for (iack = chan = 0; chan < CD180_NCHAN; chan++) 1424 if (tchans[chan].rxptr >= CD180_NFIFO) 1425 iack++; 1426 if (iack == CD180_NCHAN) 1427 break; 1428 } 1429 for (chan = 0; chan < CD180_NCHAN; chan++) { 1430 /* Select and reset channel */ 1431 rcout(sc, CD180_CAR, chan); 1432 CCRCMD(sc, chan, CCR_ResetChan); 1433 } 1434 1435 if (!rcnt) 1436 ERR(("looses characters during local loopback\n")); 1437 /* Now, check data */ 1438 for (chan = 0; chan < CD180_NCHAN; chan++) 1439 for (i = 0; i < CD180_NFIFO; i++) 1440 if (ctest[i] != tchans[chan].rxbuf[i]) 1441 ERR(("data mismatch chan %d ptr %d (%d != %d)\n", 1442 chan, i, ctest[i], tchans[chan].rxbuf[i])); 1443 (void) splx(old_level); 1444 return 0; 1445 } 1446 1447 #ifdef RCDEBUG 1448 static void 1449 printrcflags(struct rc_chans *rc, char *comment) 1450 { 1451 struct rc_softc *sc; 1452 u_short f = rc->rc_flags; 1453 1454 sc = rc->rc_rcb; 1455 printf("rc%d/%d: %s flags: %s%s%s%s%s%s%s%s%s%s%s%s\n", 1456 rc->rc_rcb->rcb_unit, rc->rc_chan, comment, 1457 (f & RC_DTR_OFF)?"DTR_OFF " :"", 1458 (f & RC_ACTOUT) ?"ACTOUT " :"", 1459 (f & RC_RTSFLOW)?"RTSFLOW " :"", 1460 (f & RC_CTSFLOW)?"CTSFLOW " :"", 1461 (f & RC_DORXFER)?"DORXFER " :"", 1462 (f & RC_DOXXFER)?"DOXXFER " :"", 1463 (f & RC_MODCHG) ?"MODCHG " :"", 1464 (f & RC_OSUSP) ?"OSUSP " :"", 1465 (f & RC_OSBUSY) ?"OSBUSY " :"", 1466 (f & RC_WAS_BUFOVFL) ?"BUFOVFL " :"", 1467 (f & RC_WAS_SILOVFL) ?"SILOVFL " :"", 1468 (f & RC_SEND_RDY) ?"SEND_RDY":""); 1469 1470 rcout(sc, CD180_CAR, rc->rc_chan); 1471 1472 printf("rc%d/%d: msvr %02x ier %02x ccsr %02x\n", 1473 rc->rc_rcb->rcb_unit, rc->rc_chan, 1474 rcin(sc, CD180_MSVR), 1475 rcin(sc, CD180_IER), 1476 rcin(sc, CD180_CCSR)); 1477 } 1478 #endif /* RCDEBUG */ 1479 1480 static void 1481 rc_dtrwakeup(void *arg) 1482 { 1483 struct rc_chans *rc; 1484 1485 rc = (struct rc_chans *)arg; 1486 rc->rc_flags &= ~RC_DTR_OFF; 1487 wakeup(&rc->rc_dtrwait); 1488 } 1489 1490 static void 1491 rc_discard_output(struct rc_chans *rc) 1492 { 1493 critical_enter(); 1494 if (rc->rc_flags & RC_DOXXFER) { 1495 rc->rc_rcb->sc_scheduled_event -= LOTS_OF_EVENTS; 1496 rc->rc_flags &= ~RC_DOXXFER; 1497 } 1498 rc->rc_optr = rc->rc_obufend; 1499 rc->rc_tp.t_state &= ~TS_BUSY; 1500 critical_exit(); 1501 ttwwakeup(&rc->rc_tp); 1502 } 1503 1504 static void 1505 rc_wait0(struct rc_softc *sc, int chan, int line) 1506 { 1507 int rcnt; 1508 1509 for (rcnt = 50; rcnt && rcin(sc, CD180_CCR); rcnt--) 1510 DELAY(30); 1511 if (rcnt == 0) 1512 device_printf(sc->sc_dev, 1513 "channel %d command timeout, rc.c line: %d\n", chan, line); 1514 } 1515 1516 static device_method_t rc_methods[] = { 1517 /* Device interface */ 1518 DEVMETHOD(device_probe, rc_probe), 1519 DEVMETHOD(device_attach, rc_attach), 1520 DEVMETHOD(device_detach, rc_detach), 1521 { 0, 0 } 1522 }; 1523 1524 static driver_t rc_driver = { 1525 "rc", 1526 rc_methods, sizeof(struct rc_softc), 1527 }; 1528 1529 DRIVER_MODULE(rc, isa, rc_driver, rc_devclass, 0, 0); 1530