1 /* $NetBSD: mfc.c,v 1.62 2024/09/08 09:36:48 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1990 The Regents of the University of California. 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 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 1994 Michael L. Hitch 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 44 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 46 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 47 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 52 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 53 */ 54 55 #include "opt_kgdb.h" 56 57 #include <sys/cdefs.h> 58 __KERNEL_RCSID(0, "$NetBSD: mfc.c,v 1.62 2024/09/08 09:36:48 rillig Exp $"); 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/kernel.h> 63 #include <sys/device.h> 64 #include <sys/tty.h> 65 #include <sys/proc.h> 66 #include <sys/file.h> 67 #include <sys/uio.h> 68 #include <sys/syslog.h> 69 #include <sys/queue.h> 70 #include <sys/conf.h> 71 #include <sys/kauth.h> 72 #include <machine/cpu.h> 73 #include <amiga/amiga/device.h> 74 #include <amiga/amiga/isr.h> 75 #include <amiga/amiga/custom.h> 76 #include <amiga/amiga/cia.h> 77 #include <amiga/amiga/cc.h> 78 #include <amiga/dev/zbusvar.h> 79 80 #include <dev/cons.h> 81 82 #include "mfcs.h" 83 84 #ifndef SEROBUF_SIZE 85 #define SEROBUF_SIZE 128 86 #endif 87 #ifndef SERIBUF_SIZE 88 #define SERIBUF_SIZE 1024 89 #endif 90 91 #define splser() spl6() 92 93 /* 94 * 68581 DUART registers 95 */ 96 struct mfc_regs { 97 volatile u_char du_mr1a; 98 #define du_mr2a du_mr1a 99 u_char pad0; 100 volatile u_char du_csra; 101 #define du_sra du_csra 102 u_char pad2; 103 volatile u_char du_cra; 104 u_char pad4; 105 volatile u_char du_tba; 106 #define du_rba du_tba 107 u_char pad6; 108 volatile u_char du_acr; 109 #define du_ipcr du_acr 110 u_char pad8; 111 volatile u_char du_imr; 112 #define du_isr du_imr 113 u_char pad10; 114 volatile u_char du_ctur; 115 #define du_cmsb du_ctur 116 u_char pad12; 117 volatile u_char du_ctlr; 118 #define du_clsb du_ctlr 119 u_char pad14; 120 volatile u_char du_mr1b; 121 #define du_mr2b du_mr1b 122 u_char pad16; 123 volatile u_char du_csrb; 124 #define du_srb du_csrb 125 u_char pad18; 126 volatile u_char du_crb; 127 u_char pad20; 128 volatile u_char du_tbb; 129 #define du_rbb du_tbb 130 u_char pad22; 131 volatile u_char du_ivr; 132 u_char pad24; 133 volatile u_char du_opcr; 134 #define du_ip du_opcr 135 u_char pad26; 136 volatile u_char du_btst; 137 #define du_strc du_btst 138 u_char pad28; 139 volatile u_char du_btrst; 140 #define du_stpc du_btrst 141 u_char pad30; 142 }; 143 144 /* 145 * 68681 DUART serial port registers 146 */ 147 struct duart_regs { 148 volatile u_char ch_mr1; 149 #define ch_mr2 ch_mr1 150 u_char pad0; 151 volatile u_char ch_csr; 152 #define ch_sr ch_csr 153 u_char pad1; 154 volatile u_char ch_cr; 155 u_char pad2; 156 volatile u_char ch_tb; 157 #define ch_rb ch_tb 158 u_char pad3; 159 }; 160 161 struct mfc_softc { 162 device_t sc_dev; 163 struct isr sc_isr; 164 struct mfc_regs *sc_regs; 165 u_long clk_frq; 166 u_short ct_val; 167 u_char ct_usecnt; 168 u_char imask; 169 u_char mfc_iii; 170 u_char last_ip; 171 }; 172 173 #if NMFCS > 0 174 struct mfcs_softc { 175 device_t sc_dev; 176 struct tty *sc_tty; 177 struct duart_regs *sc_duart; 178 struct mfc_regs *sc_regs; 179 struct mfc_softc *sc_mfc; 180 int swflags; 181 long flags; /* XXX */ 182 #define CT_USED 1 /* CT in use */ 183 u_short *rptr, *wptr, incnt, ovfl; 184 u_short inbuf[SERIBUF_SIZE]; 185 char *ptr, *end; 186 char outbuf[SEROBUF_SIZE]; 187 struct vbl_node vbl_node; 188 void *mfcs_si; 189 }; 190 #endif 191 192 #if NMFCP > 0 193 struct mfcp_softc { 194 }; 195 #endif 196 197 struct mfc_args { 198 struct zbus_args zargs; 199 const char *subdev; 200 char unit; 201 }; 202 203 int mfcprint(void *, const char *); 204 void mfcattach(device_t, device_t, void *); 205 int mfcmatch(device_t, cfdata_t, void *); 206 207 #if NMFCS > 0 208 int mfcsmatch(device_t, cfdata_t, void *); 209 void mfcsattach(device_t, device_t, void *); 210 int mfcsparam( struct tty *, struct termios *); 211 int mfcshwiflow(struct tty *, int); 212 void mfcsstart(struct tty *); 213 int mfcsmctl(dev_t, int, int); 214 void mfcsxintr(int); 215 void mfcseint(int, int); 216 void mfcsmint(register int); 217 void mfcs_intr_soft(void *); 218 #endif 219 220 #if NMFCP > 0 221 void mfcpattach(device_t, device_t, void *); 222 int mfcpmatch(device_t, cfdata_t, void *); 223 #endif 224 int mfcintr(void *); 225 226 CFATTACH_DECL_NEW(mfc, sizeof(struct mfc_softc), 227 mfcmatch, mfcattach, NULL, NULL); 228 229 #if NMFCS > 0 230 CFATTACH_DECL_NEW(mfcs, sizeof(struct mfcs_softc), 231 mfcsmatch, mfcsattach, NULL, NULL); 232 233 extern struct cfdriver mfcs_cd; 234 #endif 235 236 #if NMFCP > 0 237 CFATTACH_DECL_NEW(mfcp, sizeof(struct mfcp_softc), 238 mfcpmatch, mfcpattach, NULL, NULL); 239 #endif 240 241 dev_type_open(mfcsopen); 242 dev_type_close(mfcsclose); 243 dev_type_read(mfcsread); 244 dev_type_write(mfcswrite); 245 dev_type_ioctl(mfcsioctl); 246 dev_type_stop(mfcsstop); 247 dev_type_tty(mfcstty); 248 dev_type_poll(mfcspoll); 249 250 const struct cdevsw mfcs_cdevsw = { 251 .d_open = mfcsopen, 252 .d_close = mfcsclose, 253 .d_read = mfcsread, 254 .d_write = mfcswrite, 255 .d_ioctl = mfcsioctl, 256 .d_stop = mfcsstop, 257 .d_tty = mfcstty, 258 .d_poll = mfcspoll, 259 .d_mmap = nommap, 260 .d_kqfilter = ttykqfilter, 261 .d_discard = nodiscard, 262 .d_flag = D_TTY 263 }; 264 265 int mfcs_active; 266 int mfcsdefaultrate = 38400 /*TTYDEF_SPEED*/; 267 #define SWFLAGS(dev) (sc->swflags | (((dev) & 0x80) == 0 ? TIOCFLAG_SOFTCAR : 0)) 268 269 #ifdef notyet 270 /* 271 * MultiFaceCard III, II+ (not supported yet), and 272 * SerialMaster 500+ (not supported yet) 273 * baud rate tables for BRG set 1 [not used yet] 274 */ 275 276 const struct speedtab mfcs3speedtab1[] = { 277 { 0, 0 }, 278 { 100, 0x00 }, 279 { 220, 0x11 }, 280 { 600, 0x44 }, 281 { 1200, 0x55 }, 282 { 2400, 0x66 }, 283 { 4800, 0x88 }, 284 { 9600, 0x99 }, 285 { 19200, 0xbb }, 286 { 115200, 0xcc }, 287 { -1, -1 } 288 }; 289 290 /* 291 * MultiFaceCard II, I, and SerialMaster 500 292 * baud rate tables for BRG set 1 [not used yet] 293 */ 294 295 const struct speedtab mfcs2speedtab1[] = { 296 { 0, 0 }, 297 { 50, 0x00 }, 298 { 110, 0x11 }, 299 { 300, 0x44 }, 300 { 600, 0x55 }, 301 { 1200, 0x66 }, 302 { 2400, 0x88 }, 303 { 4800, 0x99 }, 304 { 9600, 0xbb }, 305 { 38400, 0xcc }, 306 { -1, -1 } 307 }; 308 #endif 309 310 /* 311 * MultiFaceCard III, II+ (not supported yet), and 312 * SerialMaster 500+ (not supported yet) 313 * baud rate tables for BRG set 2 314 */ 315 316 const struct speedtab mfcs3speedtab2[] = { 317 { 0, 0 }, 318 { 150, 0x00 }, 319 { 200, 0x11 }, 320 { 300, 0x33 }, 321 { 600, 0x44 }, 322 { 1200, 0x55 }, 323 { 2400, 0x66 }, 324 { 4800, 0x88 }, 325 { 9600, 0x99 }, 326 { 19200, 0xbb }, 327 { 38400, 0xcc }, 328 { -1, -1 } 329 }; 330 331 /* 332 * MultiFaceCard II, I, and SerialMaster 500 333 * baud rate tables for BRG set 2 334 */ 335 336 const struct speedtab mfcs2speedtab2[] = { 337 { 0, 0 }, 338 { 75, 0x00 }, 339 { 100, 0x11 }, 340 { 150, 0x33 }, 341 { 300, 0x44 }, 342 { 600, 0x55 }, 343 { 1200, 0x66 }, 344 { 2400, 0x88 }, 345 { 4800, 0x99 }, 346 { 9600, 0xbb }, 347 { 19200, 0xcc }, 348 { -1, -1 } 349 }; 350 351 /* 352 * if we are a bsc/Alf Data MultFaceCard (I, II, and III) 353 */ 354 int 355 mfcmatch(device_t parent, cfdata_t cf, void *aux) 356 { 357 struct zbus_args *zap; 358 359 zap = aux; 360 if (zap->manid == 2092 && 361 (zap->prodid == 16 || zap->prodid == 17 || zap->prodid == 18)) 362 363 return(1); 364 return(0); 365 } 366 367 void 368 mfcattach(device_t parent, device_t self, void *aux) 369 { 370 struct mfc_softc *scc; 371 struct zbus_args *zap; 372 struct mfc_args ma; 373 int unit; 374 struct mfc_regs *rp; 375 376 zap = aux; 377 378 printf ("\n"); 379 380 scc = device_private(self); 381 scc->sc_dev = self; 382 unit = device_unit(self); 383 scc->sc_regs = rp = zap->va; 384 if (zap->prodid == 18) 385 scc->mfc_iii = 3; 386 scc->clk_frq = scc->mfc_iii ? 230400 : 115200; 387 388 rp->du_opcr = 0x00; /* configure output port? */ 389 rp->du_btrst = 0x0f; /* clear modem lines */ 390 rp->du_ivr = 0; /* IVR */ 391 rp->du_imr = 0; /* IMR */ 392 rp->du_acr = 0xe0; /* baud rate generate set 2 */ 393 rp->du_ctur = 0; 394 rp->du_ctlr = 4; 395 rp->du_csra = 0xcc; /* clock select = 38400 */ 396 rp->du_cra = 0x10; /* reset mode register ptr */ 397 rp->du_cra = 0x20; 398 rp->du_cra = 0x30; 399 rp->du_cra = 0x40; 400 rp->du_mr1a = 0x93; /* MRA1 */ 401 rp->du_mr2a = 0x17; /* MRA2 */ 402 rp->du_csrb = 0xcc; /* clock select = 38400 */ 403 rp->du_crb = 0x10; /* reset mode register ptr */ 404 rp->du_crb = 0x20; 405 rp->du_crb = 0x30; 406 rp->du_crb = 0x40; 407 rp->du_mr1b = 0x93; /* MRB1 */ 408 rp->du_mr2b = 0x17; /* MRB2 */ 409 rp->du_cra = 0x05; /* enable A Rx & Tx */ 410 rp->du_crb = 0x05; /* enable B Rx & Tx */ 411 412 scc->sc_isr.isr_intr = mfcintr; 413 scc->sc_isr.isr_arg = scc; 414 scc->sc_isr.isr_ipl = 6; 415 add_isr(&scc->sc_isr); 416 417 /* configure ports */ 418 memcpy(&ma.zargs, zap, sizeof(struct zbus_args)); 419 420 ma.subdev = "mfcs"; 421 ma.unit = unit * 2; 422 config_found(self, &ma, mfcprint, CFARGS_NONE); 423 424 ma.unit = unit * 2 + 1; 425 config_found(self, &ma, mfcprint, CFARGS_NONE); 426 427 ma.subdev = "mfcp"; 428 ma.unit = unit; 429 config_found(self, &ma, mfcprint, CFARGS_NONE); 430 } 431 432 /* 433 * 434 */ 435 int 436 mfcsmatch(device_t parent, cfdata_t cf, void *aux) 437 { 438 struct mfc_args *ma; 439 440 ma = aux; 441 if (strcmp(ma->subdev, "mfcs") == 0) 442 return (1); 443 return (0); 444 } 445 446 void 447 mfcsattach(device_t parent, device_t self, void *aux) 448 { 449 int unit; 450 struct mfcs_softc *sc; 451 struct mfc_softc *scc; 452 struct mfc_args *ma; 453 struct mfc_regs *rp; 454 455 sc = device_private(self); 456 sc->sc_dev = self; 457 scc = device_private(parent); 458 ma = aux; 459 460 printf (": input fifo %d output fifo %d\n", SERIBUF_SIZE, 461 SEROBUF_SIZE); 462 463 unit = ma->unit; 464 mfcs_active |= 1 << unit; 465 sc->rptr = sc->wptr = sc->inbuf; 466 sc->sc_mfc = scc; 467 sc->sc_regs = rp = scc->sc_regs; 468 sc->sc_duart = (struct duart_regs *) ((unit & 1) ? 469 __UNVOLATILE(&rp->du_mr1b) : __UNVOLATILE(&rp->du_mr1a)); 470 sc->mfcs_si = softint_establish(SOFTINT_SERIAL, mfcs_intr_soft, sc); 471 /* 472 * should have only one vbl routine to handle all ports? 473 */ 474 sc->vbl_node.function = (void (*) (void *)) mfcsmint; 475 sc->vbl_node.data = (void *) unit; 476 add_vbl_function(&sc->vbl_node, 1, (void *) unit); 477 } 478 479 /* 480 * print diag if pnp is NULL else just extra 481 */ 482 int 483 mfcprint(void *aux, const char *pnp) 484 { 485 if (pnp == NULL) 486 return(UNCONF); 487 return(QUIET); 488 } 489 490 int 491 mfcsopen(dev_t dev, int flag, int mode, struct lwp *l) 492 { 493 struct tty *tp; 494 struct mfcs_softc *sc; 495 int unit, error; 496 497 error = 0; 498 unit = dev & 0x1f; 499 500 sc = device_lookup_private(&mfcs_cd, unit); 501 if (sc == NULL || (mfcs_active & (1 << unit)) == 0) 502 return (ENXIO); 503 504 if (sc->sc_tty) 505 tp = sc->sc_tty; 506 else { 507 tp = sc->sc_tty = tty_alloc(); 508 tty_attach(tp); 509 } 510 511 tp->t_oproc = (void (*) (struct tty *)) mfcsstart; 512 tp->t_param = mfcsparam; 513 tp->t_dev = dev; 514 tp->t_hwiflow = mfcshwiflow; 515 516 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 517 return (EBUSY); 518 519 ttylock(tp); 520 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) { 521 ttychars(tp); 522 if (tp->t_ispeed == 0) { 523 /* 524 * only when cleared do we reset to defaults. 525 */ 526 tp->t_iflag = TTYDEF_IFLAG; 527 tp->t_oflag = TTYDEF_OFLAG; 528 tp->t_cflag = TTYDEF_CFLAG; 529 tp->t_lflag = TTYDEF_LFLAG; 530 tp->t_ispeed = tp->t_ospeed = mfcsdefaultrate; 531 } 532 /* 533 * do these all the time 534 */ 535 if (sc->swflags & TIOCFLAG_CLOCAL) 536 tp->t_cflag |= CLOCAL; 537 if (sc->swflags & TIOCFLAG_CRTSCTS) 538 tp->t_cflag |= CRTSCTS; 539 if (sc->swflags & TIOCFLAG_MDMBUF) 540 tp->t_cflag |= MDMBUF; 541 mfcsparam(tp, &tp->t_termios); 542 ttsetwater(tp); 543 544 (void)mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET); 545 if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) || 546 (mfcsmctl(dev, 0, DMGET) & TIOCM_CD)) 547 tp->t_state |= TS_CARR_ON; 548 else 549 tp->t_state &= ~TS_CARR_ON; 550 } 551 552 /* 553 * if NONBLOCK requested, ignore carrier 554 */ 555 if (flag & O_NONBLOCK) 556 goto done; 557 558 /* 559 * block waiting for carrier 560 */ 561 while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) { 562 tp->t_wopen++; 563 error = ttysleep(tp, &tp->t_rawcv, true, 0); 564 tp->t_wopen--; 565 if (error) { 566 ttyunlock(tp); 567 return(error); 568 } 569 } 570 done: 571 /* This is a way to handle lost XON characters */ 572 if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) { 573 tp->t_state &= ~TS_TTSTOP; 574 ttstart (tp); 575 } 576 /* 577 * Reset the tty pointer, as there could have been a dialout 578 * use of the tty with a dialin open waiting. 579 */ 580 tp->t_dev = dev; 581 ttyunlock(tp); 582 return tp->t_linesw->l_open(dev, tp); 583 } 584 585 /*ARGSUSED*/ 586 int 587 mfcsclose(dev_t dev, int flag, int mode, struct lwp *l) 588 { 589 struct tty *tp; 590 int unit; 591 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, dev & 31); 592 struct mfc_softc *scc= sc->sc_mfc; 593 594 unit = dev & 31; 595 596 tp = sc->sc_tty; 597 tp->t_linesw->l_close(tp, flag); 598 sc->sc_duart->ch_cr = 0x70; /* stop break */ 599 600 scc->imask &= ~(0x7 << ((unit & 1) * 4)); 601 scc->sc_regs->du_imr = scc->imask; 602 if (sc->flags & CT_USED) { 603 --scc->ct_usecnt; 604 sc->flags &= ~CT_USED; 605 } 606 607 /* 608 * If the device is closed, it's close, no matter whether we deal with 609 * modem control signals nor not. 610 */ 611 #if 0 612 if (tp->t_cflag & HUPCL || tp->t_wopen != 0 || 613 (tp->t_state & TS_ISOPEN) == 0) 614 #endif 615 (void) mfcsmctl(dev, 0, DMSET); 616 ttyclose(tp); 617 #if not_yet 618 if (tp != &mfcs_cons) { 619 remove_vbl_function(&sc->vbl_node); 620 tty_free(tp); 621 sc->sc_tty = (struct tty *) NULL; 622 } 623 #endif 624 return (0); 625 } 626 627 int 628 mfcsread(dev_t dev, struct uio *uio, int flag) 629 { 630 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, dev & 31); 631 struct tty *tp = sc->sc_tty; 632 if (tp == NULL) 633 return(ENXIO); 634 return tp->t_linesw->l_read(tp, uio, flag); 635 } 636 637 int 638 mfcswrite(dev_t dev, struct uio *uio, int flag) 639 { 640 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, dev & 31); 641 struct tty *tp = sc->sc_tty; 642 643 if (tp == NULL) 644 return(ENXIO); 645 return tp->t_linesw->l_write(tp, uio, flag); 646 } 647 648 int 649 mfcspoll(dev_t dev, int events, struct lwp *l) 650 { 651 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, dev & 31); 652 struct tty *tp = sc->sc_tty; 653 654 if (tp == NULL) 655 return(ENXIO); 656 return ((*tp->t_linesw->l_poll)(tp, events, l)); 657 } 658 659 struct tty * 660 mfcstty(dev_t dev) 661 { 662 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, dev & 31); 663 664 return (sc->sc_tty); 665 } 666 667 int 668 mfcsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 669 { 670 register struct tty *tp; 671 register int error; 672 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, dev & 31); 673 674 tp = sc->sc_tty; 675 if (!tp) 676 return ENXIO; 677 678 error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l); 679 if (error != EPASSTHROUGH) 680 return(error); 681 682 error = ttioctl(tp, cmd, data, flag, l); 683 if (error != EPASSTHROUGH) 684 return(error); 685 686 switch (cmd) { 687 case TIOCSBRK: 688 sc->sc_duart->ch_cr = 0x60; /* start break */ 689 break; 690 691 case TIOCCBRK: 692 sc->sc_duart->ch_cr = 0x70; /* stop break */ 693 break; 694 695 case TIOCSDTR: 696 (void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS); 697 break; 698 699 case TIOCCDTR: 700 (void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC); 701 break; 702 703 case TIOCMSET: 704 (void) mfcsmctl(dev, *(int *) data, DMSET); 705 break; 706 707 case TIOCMBIS: 708 (void) mfcsmctl(dev, *(int *) data, DMBIS); 709 break; 710 711 case TIOCMBIC: 712 (void) mfcsmctl(dev, *(int *) data, DMBIC); 713 break; 714 715 case TIOCMGET: 716 *(int *)data = mfcsmctl(dev, 0, DMGET); 717 break; 718 case TIOCGFLAGS: 719 *(int *)data = SWFLAGS(dev); 720 break; 721 case TIOCSFLAGS: 722 error = kauth_authorize_device_tty(l->l_cred, 723 KAUTH_DEVICE_TTY_PRIVSET, tp); 724 if (error != 0) 725 return(EPERM); 726 727 sc->swflags = *(int *)data; 728 sc->swflags &= /* only allow valid flags */ 729 (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS); 730 /* XXXX need to change duart parameters? */ 731 break; 732 default: 733 return(EPASSTHROUGH); 734 } 735 736 return(0); 737 } 738 739 int 740 mfcsparam(struct tty *tp, struct termios *t) 741 { 742 int cflag, unit, ospeed; 743 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, tp->t_dev & 31); 744 struct mfc_softc *scc= sc->sc_mfc; 745 746 cflag = t->c_cflag; 747 unit = tp->t_dev & 31; 748 if (sc->flags & CT_USED) { 749 --scc->ct_usecnt; 750 sc->flags &= ~CT_USED; 751 } 752 ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 : 753 mfcs2speedtab2); 754 755 /* 756 * If Baud Rate Generator can't generate requested speed, 757 * try to use the counter/timer. 758 */ 759 if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) { 760 ospeed = scc->clk_frq / t->c_ospeed; /* divisor */ 761 if (scc->ct_usecnt > 0 && scc->ct_val != ospeed) 762 ospeed = -1; 763 else { 764 scc->sc_regs->du_ctur = ospeed >> 8; 765 scc->sc_regs->du_ctlr = ospeed; 766 scc->ct_val = ospeed; 767 ++scc->ct_usecnt; 768 sc->flags |= CT_USED; 769 ospeed = 0xdd; 770 } 771 } 772 /* XXXX 68681 duart could handle split speeds */ 773 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed)) 774 return(EINVAL); 775 776 /* XXXX handle parity, character size, stop bits, flow control */ 777 778 /* 779 * copy to tty 780 */ 781 tp->t_ispeed = t->c_ispeed; 782 tp->t_ospeed = t->c_ospeed; 783 tp->t_cflag = cflag; 784 785 /* 786 * enable interrupts 787 */ 788 scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80; 789 scc->sc_regs->du_imr = scc->imask; 790 #if defined(DEBUG) && 0 791 printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n", 792 t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag); 793 #endif 794 if (ospeed == 0) 795 (void)mfcsmctl(tp->t_dev, 0, DMSET); /* hang up line */ 796 else { 797 /* 798 * (re)enable DTR 799 * and set baud rate. (8 bit mode) 800 */ 801 (void)mfcsmctl(tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET); 802 sc->sc_duart->ch_csr = ospeed; 803 } 804 return(0); 805 } 806 807 int 808 mfcshwiflow(struct tty *tp, int flag) 809 { 810 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, tp->t_dev & 31); 811 int unit = tp->t_dev & 1; 812 813 if (flag) 814 sc->sc_regs->du_btrst = 1 << unit; 815 else 816 sc->sc_regs->du_btst = 1 << unit; 817 return 1; 818 } 819 820 void 821 mfcsstart(struct tty *tp) 822 { 823 int cc, s, unit; 824 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, tp->t_dev & 31); 825 struct mfc_softc *scc= sc->sc_mfc; 826 827 if ((tp->t_state & TS_ISOPEN) == 0) 828 return; 829 830 unit = tp->t_dev & 1; 831 832 s = splser(); 833 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) 834 goto out; 835 836 cc = tp->t_outq.c_cc; 837 if (!ttypull(tp) || (tp->t_state & TS_BUSY)) 838 goto out; 839 840 /* 841 * We only do bulk transfers if using CTSRTS flow control, not for 842 * (probably sloooow) ixon/ixoff devices. 843 */ 844 if ((tp->t_cflag & CRTSCTS) == 0) 845 cc = 1; 846 847 /* 848 * Limit the amount of output we do in one burst 849 * to prevent hogging the CPU. 850 */ 851 if (cc > SEROBUF_SIZE) 852 cc = SEROBUF_SIZE; 853 cc = q_to_b(&tp->t_outq, sc->outbuf, cc); 854 if (cc > 0) { 855 tp->t_state |= TS_BUSY; 856 857 sc->ptr = sc->outbuf; 858 sc->end = sc->outbuf + cc; 859 860 /* 861 * Get first character out, then have TBE-interrupts blow out 862 * further characters, until buffer is empty, and TS_BUSY gets 863 * cleared. 864 */ 865 sc->sc_duart->ch_tb = *sc->ptr++; 866 scc->imask |= 1 << (unit * 4); 867 sc->sc_regs->du_imr = scc->imask; 868 } 869 out: 870 splx(s); 871 } 872 873 /* 874 * Stop output on a line. 875 */ 876 /*ARGSUSED*/ 877 void 878 mfcsstop(struct tty *tp, int flag) 879 { 880 int s; 881 882 s = splser(); 883 if (tp->t_state & TS_BUSY) { 884 if ((tp->t_state & TS_TTSTOP) == 0) 885 tp->t_state |= TS_FLUSH; 886 } 887 splx(s); 888 } 889 890 int 891 mfcsmctl(dev_t dev, int bits, int how) 892 { 893 int unit, s; 894 u_char ub = 0; 895 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, dev & 31); 896 897 unit = dev & 1; 898 899 /* 900 * convert TIOCM* mask into CIA mask 901 * which is active low 902 */ 903 if (how != DMGET) { 904 /* 905 * need to save current state of DTR & RTS ? 906 */ 907 if (bits & TIOCM_DTR) 908 ub |= 0x04 << unit; 909 if (bits & TIOCM_RTS) 910 ub |= 0x01 << unit; 911 } 912 s = splser(); 913 switch (how) { 914 case DMSET: 915 sc->sc_regs->du_btst = ub; 916 sc->sc_regs->du_btrst = ub ^ (0x05 << unit); 917 break; 918 919 case DMBIC: 920 sc->sc_regs->du_btrst = ub; 921 ub = ~sc->sc_regs->du_ip; 922 break; 923 924 case DMBIS: 925 sc->sc_regs->du_btst = ub; 926 ub = ~sc->sc_regs->du_ip; 927 break; 928 929 case DMGET: 930 ub = ~sc->sc_regs->du_ip; 931 break; 932 } 933 (void)splx(s); 934 935 /* XXXX should keep DTR & RTS states in softc? */ 936 bits = TIOCM_DTR | TIOCM_RTS; 937 if (ub & (1 << unit)) 938 bits |= TIOCM_CTS; 939 if (ub & (4 << unit)) 940 bits |= TIOCM_DSR; 941 if (ub & (0x10 << unit)) 942 bits |= TIOCM_CD; 943 /* XXXX RI is not supported on all boards */ 944 if (sc->sc_regs->pad26 & (1 << unit)) 945 bits |= TIOCM_RI; 946 947 return(bits); 948 } 949 950 /* 951 * Level 6 interrupt processing for the MultiFaceCard 68681 DUART 952 */ 953 954 int 955 mfcintr(void *arg) 956 { 957 struct mfc_softc *scc = arg; 958 struct mfcs_softc *sc; 959 struct mfc_regs *regs; 960 struct tty *tp; 961 int istat, unit; 962 u_short c; 963 964 regs = scc->sc_regs; 965 istat = regs->du_isr & scc->imask; 966 if (istat == 0) 967 return (0); 968 unit = device_unit(scc->sc_dev) * 2; 969 if (istat & 0x02) { /* channel A receive interrupt */ 970 sc = device_lookup_private(&mfcs_cd, unit); 971 while (1) { 972 c = regs->du_sra << 8; 973 if ((c & 0x0100) == 0) 974 break; 975 c |= regs->du_rba; 976 if (sc->incnt == SERIBUF_SIZE) 977 ++sc->ovfl; 978 else { 979 *sc->wptr++ = c; 980 if (sc->wptr == sc->inbuf + SERIBUF_SIZE) 981 sc->wptr = sc->inbuf; 982 ++sc->incnt; 983 if (sc->incnt > SERIBUF_SIZE - 16) 984 regs->du_btrst = 1; 985 } 986 if (c & 0x1000) 987 regs->du_cra = 0x40; 988 } 989 } 990 if (istat & 0x20) { /* channel B receive interrupt */ 991 sc = device_lookup_private(&mfcs_cd, unit + 1); 992 while (1) { 993 c = regs->du_srb << 8; 994 if ((c & 0x0100) == 0) 995 break; 996 c |= regs->du_rbb; 997 if (sc->incnt == SERIBUF_SIZE) 998 ++sc->ovfl; 999 else { 1000 *sc->wptr++ = c; 1001 if (sc->wptr == sc->inbuf + SERIBUF_SIZE) 1002 sc->wptr = sc->inbuf; 1003 ++sc->incnt; 1004 if (sc->incnt > SERIBUF_SIZE - 16) 1005 regs->du_btrst = 2; 1006 } 1007 if (c & 0x1000) 1008 regs->du_crb = 0x40; 1009 } 1010 } 1011 if (istat & 0x01) { /* channel A transmit interrupt */ 1012 sc = device_lookup_private(&mfcs_cd, unit); 1013 tp = sc->sc_tty; 1014 if (sc->ptr == sc->end) { 1015 tp->t_state &= ~(TS_BUSY | TS_FLUSH); 1016 scc->imask &= ~0x01; 1017 regs->du_imr = scc->imask; 1018 softint_schedule(sc->mfcs_si); 1019 } 1020 else 1021 regs->du_tba = *sc->ptr++; 1022 } 1023 if (istat & 0x10) { /* channel B transmit interrupt */ 1024 sc = device_lookup_private(&mfcs_cd, unit + 1); 1025 tp = sc->sc_tty; 1026 if (sc->ptr == sc->end) { 1027 tp->t_state &= ~(TS_BUSY | TS_FLUSH); 1028 scc->imask &= ~0x10; 1029 regs->du_imr = scc->imask; 1030 softint_schedule(sc->mfcs_si); 1031 } 1032 else 1033 regs->du_tbb = *sc->ptr++; 1034 } 1035 if (istat & 0x80) { /* input port change interrupt */ 1036 c = regs->du_ipcr; 1037 printf ("%s: ipcr %02x", device_xname(scc->sc_dev), c); 1038 } 1039 return(1); 1040 } 1041 1042 void 1043 mfcsxintr(int unit) 1044 { 1045 int s1, s2, ovfl; 1046 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, unit); 1047 struct tty *tp = sc->sc_tty; 1048 1049 /* 1050 * Make sure we're not interrupted by another 1051 * vbl, but allow level6 ints 1052 */ 1053 s1 = spltty(); 1054 1055 /* 1056 * pass along any accumulated information 1057 * while input is not blocked 1058 */ 1059 while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) { 1060 /* 1061 * no collision with ser_fastint() 1062 */ 1063 mfcseint(unit, *sc->rptr++); 1064 1065 ovfl = 0; 1066 /* lock against mfcs_fastint() */ 1067 s2 = splser(); 1068 --sc->incnt; 1069 if (sc->rptr == sc->inbuf + SERIBUF_SIZE) 1070 sc->rptr = sc->inbuf; 1071 if (sc->ovfl != 0) { 1072 ovfl = sc->ovfl; 1073 sc->ovfl = 0; 1074 } 1075 splx(s2); 1076 if (ovfl != 0) 1077 log(LOG_WARNING, "%s: %d buffer overflow!\n", 1078 device_xname(sc->sc_dev), ovfl); 1079 } 1080 if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) { 1081 sc->sc_regs->du_btst = 1 << unit; /* XXXX */ 1082 } 1083 splx(s1); 1084 } 1085 1086 void 1087 mfcseint(int unit, int stat) 1088 { 1089 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, unit); 1090 struct tty *tp; 1091 u_char ch; 1092 int c; 1093 1094 tp = sc->sc_tty; 1095 ch = stat & 0xff; 1096 c = ch; 1097 1098 if ((tp->t_state & TS_ISOPEN) == 0) { 1099 #ifdef KGDB 1100 extern const struct cdevsw ser_cdevsw; 1101 int maj; 1102 1103 /* we don't care about parity errors */ 1104 maj = cdevsw_lookup_major(&ser_cdevsw); 1105 if (kgdb_dev == makedev(maj, unit) && c == FRAME_END) 1106 kgdb_connect(0); /* trap into kgdb */ 1107 #endif 1108 return; 1109 } 1110 1111 /* 1112 * Check for break and (if enabled) parity error. 1113 */ 1114 if (stat & 0xc000) 1115 c |= TTY_FE; 1116 else if (stat & 0x2000) 1117 c |= TTY_PE; 1118 1119 if (stat & 0x1000) 1120 log(LOG_WARNING, "%s: fifo overflow\n", 1121 device_xname(device_lookup_private(&mfcs_cd, unit))); 1122 1123 tp->t_linesw->l_rint(c, tp); 1124 } 1125 1126 /* 1127 * This interrupt is periodically invoked in the vertical blank 1128 * interrupt. It's used to keep track of the modem control lines 1129 * and (new with the fast_int code) to move accumulated data 1130 * up into the tty layer. 1131 */ 1132 void 1133 mfcsmint(int unit) 1134 { 1135 struct tty *tp; 1136 struct mfcs_softc *sc = device_lookup_private(&mfcs_cd, unit); 1137 u_char stat, last, istat; 1138 1139 tp = sc->sc_tty; 1140 if (!tp) 1141 return; 1142 1143 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) { 1144 sc->rptr = sc->wptr = sc->inbuf; 1145 sc->incnt = 0; 1146 return; 1147 } 1148 /* 1149 * empty buffer 1150 */ 1151 mfcsxintr(unit); 1152 1153 stat = ~sc->sc_regs->du_ip; 1154 last = sc->sc_mfc->last_ip; 1155 sc->sc_mfc->last_ip = stat; 1156 1157 /* 1158 * check whether any interesting signal changed state 1159 */ 1160 istat = stat ^ last; 1161 1162 if ((istat & (0x10 << (unit & 1))) && /* CD changed */ 1163 (SWFLAGS(tp->t_dev) & TIOCFLAG_SOFTCAR) == 0) { 1164 if (stat & (0x10 << (unit & 1))) 1165 tp->t_linesw->l_modem(tp, 1); 1166 else if (tp->t_linesw->l_modem(tp, 0) == 0) { 1167 sc->sc_regs->du_btrst = 0x0a << (unit & 1); 1168 } 1169 } 1170 } 1171 1172 void 1173 mfcs_intr_soft(void *arg) 1174 { 1175 struct mfcs_softc *sc = (struct mfcs_softc *)arg; 1176 struct tty *tp = sc->sc_tty; 1177 1178 if (tp->t_linesw) 1179 tp->t_linesw->l_start(tp); 1180 else 1181 mfcsstart(tp); 1182 } 1183