1/* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 *
|
82}; 83 84#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 85#define NMDM_MAX_NUM 128 /* Artificially limit # devices. */ 86#define PF_STOPPED 0x10 /* user told stopped */ 87 88struct softpart { 89 struct tty nm_tty; 90 dev_t dev; 91 int modemsignals; /* bits defined in sys/ttycom.h */ 92 int gotbreak; 93}; 94 95struct nm_softc { 96 int pt_flags; 97 struct softpart part1, part2; 98 struct prison *pt_prison; 99}; 100 101static void 102nmdm_crossover(struct nm_softc *pti, 103 struct softpart *ourpart, 104 struct softpart *otherpart); 105 106#define GETPARTS(tp, ourpart, otherpart) \ 107do { \ 108 struct nm_softc *pti = tp->t_dev->si_drv1; \ 109 if (tp == &pti->part1.nm_tty) { \ 110 ourpart = &pti->part1; \ 111 otherpart = &pti->part2; \ 112 } else { \ 113 ourpart = &pti->part2; \ 114 otherpart = &pti->part1; \ 115 } \ 116} while (0) 117 118/* 119 * This function creates and initializes a pair of ttys. 120 */ 121static void 122nmdminit(n) 123 int n; 124{ 125 dev_t dev1, dev2; 126 struct nm_softc *pt; 127 128 /* For now we only map the lower 8 bits of the minor */ 129 if (n & ~0xff) 130 return; 131 132 pt = malloc(sizeof(*pt), M_NLMDM, M_WAITOK); 133 bzero(pt, sizeof(*pt)); 134 pt->part1.dev = dev1 = make_dev(&nmdm_cdevsw, n+n, 135 0, 0, 0666, "nmdm%dA", n); 136 pt->part2.dev = dev2 = make_dev(&nmdm_cdevsw, n+n+1, 137 0, 0, 0666, "nmdm%dB", n); 138 139 dev1->si_drv1 = dev2->si_drv1 = pt; 140 dev1->si_tty = &pt->part1.nm_tty; 141 dev2->si_tty = &pt->part2.nm_tty; 142 ttyregister(&pt->part1.nm_tty); 143 ttyregister(&pt->part2.nm_tty); 144 pt->part1.nm_tty.t_oproc = nmdmstart; 145 pt->part2.nm_tty.t_oproc = nmdmstart; 146 pt->part1.nm_tty.t_stop = nmdmstop; 147 pt->part2.nm_tty.t_dev = dev1; 148 pt->part1.nm_tty.t_dev = dev2; 149 pt->part2.nm_tty.t_stop = nmdmstop; 150} 151 152/* 153 * Device opened from userland 154 */ 155static int 156nmdmopen(dev_t dev, int flag, int devtype, struct thread *td) 157{ 158 register struct tty *tp, *tp2; 159 int error; 160 int minr; 161 dev_t nextdev; 162 struct nm_softc *pti; 163 int is_b; 164 int pair; 165 struct softpart *ourpart, *otherpart; 166 167 /* 168 * XXX: Gross hack for DEVFS: 169 * If we openned this device, ensure we have the 170 * next one too, so people can open it. 171 */ 172 minr = dev2unit(dev); 173 pair = minr >> 1; 174 is_b = minr & 1; 175 176 if (pair < (NMDM_MAX_NUM - 1)) { 177 nextdev = makedev(major(dev), minr + 2); 178 if (!nextdev->si_drv1) { 179 nmdminit(pair + 1); 180 } 181 } else { /* Limit ourselves to 128 of them for now */ 182 if (pair > (NMDM_MAX_NUM - 1)) 183 return (ENXIO); 184 } 185 if (!dev->si_drv1) 186 nmdminit(pair); 187 188 if (!dev->si_drv1) 189 return(ENXIO); 190 191 pti = dev->si_drv1; 192 if (is_b) 193 tp = &pti->part2.nm_tty; 194 else 195 tp = &pti->part1.nm_tty; 196 GETPARTS(tp, ourpart, otherpart); 197 198 tp2 = &otherpart->nm_tty; 199 ourpart->modemsignals |= TIOCM_LE; 200 201 if ((tp->t_state & TS_ISOPEN) == 0) { 202 ttychars(tp); /* Set up default chars */ 203 tp->t_iflag = TTYDEF_IFLAG; 204 tp->t_oflag = TTYDEF_OFLAG; 205 tp->t_lflag = TTYDEF_LFLAG; 206 tp->t_cflag = TTYDEF_CFLAG; 207 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 208 } else if (tp->t_state & TS_XCLUDE && suser(td)) { 209 return (EBUSY); 210 } else if (pti->pt_prison != td->td_ucred->cr_prison) { 211 return (EBUSY); 212 } 213 214 /* 215 * If the other side is open we have carrier 216 */ 217 if (tp2->t_state & TS_ISOPEN) { 218 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 219 } 220 221 /* 222 * And the other side gets carrier as we are now open. 223 */ 224 (void)(*linesw[tp2->t_line].l_modem)(tp2, 1); 225 226 /* External processing makes no sense here */ 227 tp->t_lflag &= ~EXTPROC; 228 229 /* 230 * Wait here if we don't have carrier. 231 */ 232#if 0 233 while ((tp->t_state & TS_CARR_ON) == 0) { 234 if (flag & FNONBLOCK) 235 break; 236 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 237 "nmdopn", 0); 238 if (error) 239 return (error); 240 } 241#endif 242 243 /* 244 * Give the line disciplin a chance to set this end up. 245 */ 246 error = (*linesw[tp->t_line].l_open)(dev, tp); 247 248 /* 249 * Wake up the other side. 250 * Theoretically not needed. 251 */ 252 ourpart->modemsignals |= TIOCM_DTR; 253 nmdm_crossover(pti, ourpart, otherpart); 254 if (error == 0) 255 wakeup_other(tp, FREAD|FWRITE); /* XXX */ 256 return (error); 257} 258 259/* 260 * Device closed again 261 */ 262static int 263nmdmclose(dev_t dev, int flag, int mode, struct thread *td) 264{ 265 register struct tty *tp, *tp2; 266 int err; 267 struct softpart *ourpart, *otherpart; 268 269 /* 270 * let the other end know that the game is up 271 */ 272 tp = dev->si_tty; 273 GETPARTS(tp, ourpart, otherpart); 274 tp2 = &otherpart->nm_tty; 275 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0); 276 277 /* 278 * XXX MDMBUF makes no sense for nmdms but would inhibit the above 279 * l_modem(). CLOCAL makes sense but isn't supported. Special 280 * l_modem()s that ignore carrier drop make no sense for nmdms but 281 * may be in use because other parts of the line discipline make 282 * sense for nmdms. Recover by doing everything that a normal 283 * ttymodem() would have done except for sending a SIGHUP. 284 */ 285 if (tp2->t_state & TS_ISOPEN) { 286 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 287 tp2->t_state |= TS_ZOMBIE; 288 ttyflush(tp2, FREAD | FWRITE); 289 } 290 291 err = (*linesw[tp->t_line].l_close)(tp, flag); 292 ourpart->modemsignals &= ~TIOCM_DTR; 293 nmdm_crossover(dev->si_drv1, ourpart, otherpart); 294 nmdmstop(tp, FREAD|FWRITE); 295 (void) ttyclose(tp); 296 return (err); 297} 298 299/* 300 * handle read(2) request from userland 301 */ 302static int 303nmdmread(dev_t dev, struct uio *uio, int flag) 304{ 305 int error = 0; 306 struct tty *tp, *tp2; 307 struct softpart *ourpart, *otherpart; 308 309 tp = dev->si_tty; 310 GETPARTS(tp, ourpart, otherpart); 311 tp2 = &otherpart->nm_tty; 312 313#if 0 314 if (tp2->t_state & TS_ISOPEN) { 315 error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 316 wakeup_other(tp, FWRITE); 317 } else { 318 if (flag & IO_NDELAY) { 319 return (EWOULDBLOCK); 320 } 321 error = tsleep(TSA_PTC_READ(tp), 322 TTIPRI | PCATCH, "nmdout", 0); 323 } 324 } 325#else 326 if ((error = (*linesw[tp->t_line].l_read)(tp, uio, flag)) == 0) 327 wakeup_other(tp, FWRITE); 328#endif 329 return (error); 330} 331 332/* 333 * Write to pseudo-tty. 334 * Wakeups of controlling tty will happen 335 * indirectly, when tty driver calls nmdmstart. 336 */ 337static int 338nmdmwrite(dev_t dev, struct uio *uio, int flag) 339{ 340 register u_char *cp = 0; 341 register int cc = 0; 342 u_char locbuf[BUFSIZ]; 343 int cnt = 0; 344 int error = 0; 345 struct tty *tp1, *tp; 346 struct softpart *ourpart, *otherpart; 347 348 tp1 = dev->si_tty; 349 /* 350 * Get the other tty struct. 351 * basically we are writing into the INPUT side of the other device. 352 */ 353 GETPARTS(tp1, ourpart, otherpart); 354 tp = &otherpart->nm_tty; 355 356again: 357 if ((tp->t_state & TS_ISOPEN) == 0) 358 return (EIO); 359 while (uio->uio_resid > 0 || cc > 0) { 360 /* 361 * Fill up the buffer if it's empty 362 */ 363 if (cc == 0) { 364 cc = min(uio->uio_resid, BUFSIZ); 365 cp = locbuf; 366 error = uiomove((caddr_t)cp, cc, uio); 367 if (error) 368 return (error); 369 /* check again for safety */ 370 if ((tp->t_state & TS_ISOPEN) == 0) { 371 /* adjust for data copied in but not written */ 372 uio->uio_resid += cc; 373 return (EIO); 374 } 375 } 376 while (cc > 0) { 377 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2)) 378 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) { 379 /* 380 * Come here to wait for space in outq, 381 * or space in rawq, or an empty canq. 382 */ 383 wakeup(TSA_HUP_OR_INPUT(tp)); 384 if ((tp->t_state & TS_CONNECTED) == 0) { 385 /* 386 * Data piled up because not connected. 387 * Adjust for data copied in but 388 * not written. 389 */ 390 uio->uio_resid += cc; 391 return (EIO); 392 } 393 if (flag & IO_NDELAY) { 394 /* 395 * Don't wait if asked not to. 396 * Adjust for data copied in but 397 * not written. 398 */ 399 uio->uio_resid += cc; 400 if (cnt == 0) 401 return (EWOULDBLOCK); 402 return (0); 403 } 404 error = tsleep(TSA_PTC_WRITE(tp), 405 TTOPRI | PCATCH, "nmdout", 0); 406 if (error) { 407 /* 408 * Tsleep returned (signal?). 409 * Go find out what the user wants. 410 * adjust for data copied in but 411 * not written 412 */ 413 uio->uio_resid += cc; 414 return (error); 415 } 416 goto again; 417 } 418 (*linesw[tp->t_line].l_rint)(*cp++, tp); 419 cnt++; 420 cc--; 421 } 422 cc = 0; 423 } 424 return (0); 425} 426 427/* 428 * Start output on pseudo-tty. 429 * Wake up process selecting or sleeping for input from controlling tty. 430 */ 431static void 432nmdmstart(struct tty *tp) 433{ 434 register struct nm_softc *pti = tp->t_dev->si_drv1; 435 436 if (tp->t_state & TS_TTSTOP) 437 return; 438 pti->pt_flags &= ~PF_STOPPED; 439 wakeup_other(tp, FREAD); 440} 441 442/* Wakes up the OTHER tty;*/ 443static void 444wakeup_other(struct tty *tp, int flag) 445{ 446 struct softpart *ourpart, *otherpart; 447 448 GETPARTS(tp, ourpart, otherpart); 449 if (flag & FREAD) { 450 selwakeup(&otherpart->nm_tty.t_rsel); 451 wakeup(TSA_PTC_READ((&otherpart->nm_tty))); 452 } 453 if (flag & FWRITE) { 454 selwakeup(&otherpart->nm_tty.t_wsel); 455 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty))); 456 } 457} 458 459/* 460 * stopped output on tty, called when device is closed 461 */ 462static void 463nmdmstop(register struct tty *tp, int flush) 464{ 465 struct nm_softc *pti = tp->t_dev->si_drv1; 466 int flag; 467 468 /* note: FLUSHREAD and FLUSHWRITE already ok */ 469 if (flush == 0) { 470 flush = TIOCPKT_STOP; 471 pti->pt_flags |= PF_STOPPED; 472 } else 473 pti->pt_flags &= ~PF_STOPPED; 474 /* change of perspective */ 475 flag = 0; 476 if (flush & FREAD) 477 flag |= FWRITE; 478 if (flush & FWRITE) 479 flag |= FREAD; 480 wakeup_other(tp, flag); 481} 482 483/* 484 * handle ioctl(2) request from userland 485 */ 486static int 487nmdmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 488{ 489 register struct tty *tp = dev->si_tty; 490 struct nm_softc *pti = dev->si_drv1; 491 int error, s; 492 register struct tty *tp2; 493 struct softpart *ourpart, *otherpart; 494 495 s = spltty(); 496 GETPARTS(tp, ourpart, otherpart); 497 tp2 = &otherpart->nm_tty; 498 499 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 500 if (error == ENOIOCTL) 501 error = ttioctl(tp, cmd, data, flag); 502 if (error == ENOIOCTL) { 503 switch (cmd) { 504 case TIOCSBRK: 505 otherpart->gotbreak = 1; 506 break; 507 case TIOCCBRK: 508 break; 509 case TIOCSDTR: 510 ourpart->modemsignals |= TIOCM_DTR; 511 break; 512 case TIOCCDTR: 513 ourpart->modemsignals &= TIOCM_DTR; 514 break; 515 case TIOCMSET: 516 ourpart->modemsignals = *(int *)data; 517 otherpart->modemsignals = *(int *)data; 518 break; 519 case TIOCMBIS: 520 ourpart->modemsignals |= *(int *)data; 521 break; 522 case TIOCMBIC: 523 ourpart->modemsignals &= ~(*(int *)data); 524 otherpart->modemsignals &= ~(*(int *)data); 525 break; 526 case TIOCMGET: 527 *(int *)data = ourpart->modemsignals; 528 break; 529 case TIOCMSDTRWAIT: 530 break; 531 case TIOCMGDTRWAIT: 532 *(int *)data = 0; 533 break; 534 case TIOCTIMESTAMP: 535 /* FALLTHROUGH */ 536 case TIOCDCDTIMESTAMP: 537 default: 538 splx(s); 539 error = ENOTTY; 540 return (error); 541 } 542 error = 0; 543 nmdm_crossover(pti, ourpart, otherpart); 544 } 545 splx(s); 546 return (error); 547} 548 549static void 550nmdm_crossover(struct nm_softc *pti, struct softpart *ourpart, 551 struct softpart *otherpart) 552{ 553 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR); 554 if (ourpart->modemsignals & TIOCM_RTS) 555 otherpart->modemsignals |= TIOCM_CTS; 556 if (ourpart->modemsignals & TIOCM_DTR) 557 otherpart->modemsignals |= TIOCM_CAR; 558} 559 560/* 561 * Module handling 562 */ 563static int 564nmdm_modevent(module_t mod, int type, void *data) 565{ 566 int error = 0; 567 568 switch(type) { 569 case MOD_LOAD: /* start with 4 of them */ 570 nmdminit(0); 571 nmdminit(1); 572 nmdminit(2); 573 nmdminit(3); 574 break; 575 576 case MOD_SHUTDOWN: 577 /* FALLTHROUGH */ 578 case MOD_UNLOAD: 579 nmdmshutdown(); 580 break; 581 default: 582 error = EOPNOTSUPP; 583 } 584 return (error); 585} 586 587/* 588 * Handle teardown of device 589 */ 590static int 591nmdmshutdown(void) 592{ 593 int i; 594 dev_t nextdev1; 595 dev_t nextdev2; 596 void * ptr1; 597 598 for(i = 0;( i < NMDM_MAX_NUM) ;i++) { 599 nextdev1 = makedev(CDEV_MAJOR, (i+i) ); 600 nextdev2 = makedev(CDEV_MAJOR, (i+i) + 1); 601 ptr1 = nextdev1->si_drv1; 602 if (ptr1) { 603 revoke_and_destroy_dev(nextdev1); 604 revoke_and_destroy_dev(nextdev2); 605 free(ptr1, M_NLMDM); 606 } else { 607 freedev(nextdev1); 608 freedev(nextdev2); 609 } 610 } 611 return(0); 612} 613 614DEV_MODULE(nmdm, nmdm_modevent, NULL);
|