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 * 33 * $FreeBSD: src/sys/dev/nmdm/nmdm.c,v 1.5.2.1 2001/08/11 00:54:14 mp Exp $ 34 * $DragonFly: src/sys/dev/misc/nmdm/nmdm.c,v 1.16 2008/01/05 14:02:37 swildner Exp $ 35 */ 36 37 /* 38 * Pseudo-nulmodem Driver 39 */ 40 #include "opt_compat.h" 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 44 #include <sys/ioctl_compat.h> 45 #endif 46 #include <sys/proc.h> 47 #include <sys/priv.h> 48 #include <sys/thread2.h> 49 #include <sys/tty.h> 50 #include <sys/conf.h> 51 #include <sys/fcntl.h> 52 #include <sys/poll.h> 53 #include <sys/kernel.h> 54 #include <sys/vnode.h> 55 #include <sys/signalvar.h> 56 #include <sys/malloc.h> 57 58 MALLOC_DEFINE(M_NLMDM, "nullmodem", "nullmodem data structures"); 59 60 static void nmdmstart (struct tty *tp); 61 static void nmdmstop (struct tty *tp, int rw); 62 static void wakeup_other (struct tty *tp, int flag); 63 static void nmdminit (int n); 64 65 static d_open_t nmdmopen; 66 static d_close_t nmdmclose; 67 static d_read_t nmdmread; 68 static d_write_t nmdmwrite; 69 static d_ioctl_t nmdmioctl; 70 71 #define CDEV_MAJOR 18 72 static struct dev_ops nmdm_ops = { 73 { "pts", CDEV_MAJOR, D_TTY }, 74 .d_open = nmdmopen, 75 .d_close = nmdmclose, 76 .d_read = nmdmread, 77 .d_write = nmdmwrite, 78 .d_ioctl = nmdmioctl, 79 .d_poll = ttypoll, 80 .d_revoke = ttyrevoke 81 }; 82 83 #define BUFSIZ 100 /* Chunk size iomoved to/from user */ 84 85 struct softpart { 86 struct tty nm_tty; 87 cdev_t dev; 88 int modemsignals; /* bits defined in sys/ttycom.h */ 89 int gotbreak; 90 }; 91 92 struct nm_softc { 93 int pt_flags; 94 struct softpart part1, part2; 95 struct prison *pt_prison; 96 }; 97 98 #define PF_STOPPED 0x10 /* user told stopped */ 99 100 static void 101 nmdm_crossover(struct nm_softc *pti, 102 struct softpart *ourpart, 103 struct softpart *otherpart); 104 105 #define GETPARTS(tp, ourpart, otherpart) \ 106 do { \ 107 struct nm_softc *pti = tp->t_dev->si_drv1; \ 108 if (tp == &pti->part1.nm_tty) { \ 109 ourpart = &pti->part1; \ 110 otherpart = &pti->part2; \ 111 } else { \ 112 ourpart = &pti->part2; \ 113 otherpart = &pti->part1; \ 114 } \ 115 } while (0) 116 117 /* 118 * This function creates and initializes a pair of ttys. 119 */ 120 static void 121 nmdminit(int n) 122 { 123 cdev_t dev1, dev2; 124 struct nm_softc *pt; 125 126 /* 127 * Simplified unit number, use low 8 bits of minor number 128 * (remember, the minor number mask is 0xffff00ff). 129 */ 130 if (n & ~0x7f) 131 return; 132 133 pt = kmalloc(sizeof(*pt), M_NLMDM, M_WAITOK | M_ZERO); 134 dev_ops_add(&nmdm_ops, ~1, n << 1); 135 pt->part1.dev = dev1 = make_dev(&nmdm_ops, n << 1, 136 0, 0, 0666, "nmdm%dA", n); 137 pt->part2.dev = dev2 = make_dev(&nmdm_ops, (n << 1) + 1, 138 0, 0, 0666, "nmdm%dB", n); 139 140 dev1->si_drv1 = dev2->si_drv1 = pt; 141 dev1->si_tty = &pt->part1.nm_tty; 142 dev2->si_tty = &pt->part2.nm_tty; 143 ttyregister(&pt->part1.nm_tty); 144 ttyregister(&pt->part2.nm_tty); 145 pt->part1.nm_tty.t_oproc = nmdmstart; 146 pt->part2.nm_tty.t_oproc = nmdmstart; 147 pt->part1.nm_tty.t_stop = nmdmstop; 148 pt->part2.nm_tty.t_dev = dev1; 149 pt->part1.nm_tty.t_dev = dev2; 150 pt->part2.nm_tty.t_stop = nmdmstop; 151 } 152 153 /*ARGSUSED*/ 154 static int 155 nmdmopen(struct dev_open_args *ap) 156 { 157 cdev_t dev = ap->a_head.a_dev; 158 struct tty *tp, *tp2; 159 int error; 160 int minr; 161 #if 0 162 cdev_t nextdev; 163 #endif 164 struct nm_softc *pti; 165 int is_b; 166 int pair; 167 struct softpart *ourpart, *otherpart; 168 169 minr = lminor(dev); 170 pair = minr >> 1; 171 is_b = minr & 1; 172 173 #if 0 174 /* 175 * XXX: Gross hack for DEVFS: 176 * If we openned this device, ensure we have the 177 * next one too, so people can open it. 178 */ 179 if (pair < 127) { 180 nextdev = makedev(major(dev), (pair+pair) + 1); 181 if (!nextdev->si_drv1) { 182 nmdminit(pair + 1); 183 } 184 } 185 #endif 186 if (!dev->si_drv1) 187 nmdminit(pair); 188 189 if (!dev->si_drv1) 190 return(ENXIO); 191 192 pti = dev->si_drv1; 193 if (is_b) 194 tp = &pti->part2.nm_tty; 195 else 196 tp = &pti->part1.nm_tty; 197 GETPARTS(tp, ourpart, otherpart); 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 && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) { 209 return (EBUSY); 210 } else if (pti->pt_prison != ap->a_cred->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), PCATCH, "nmdopn", 0); 237 if (error) 238 return (error); 239 } 240 #endif 241 242 /* 243 * Give the line disciplin a chance to set this end up. 244 */ 245 error = (*linesw[tp->t_line].l_open)(dev, tp); 246 247 /* 248 * Wake up the other side. 249 * Theoretically not needed. 250 */ 251 ourpart->modemsignals |= TIOCM_DTR; 252 nmdm_crossover(pti, ourpart, otherpart); 253 if (error == 0) 254 wakeup_other(tp, FREAD|FWRITE); /* XXX */ 255 return (error); 256 } 257 258 static int 259 nmdmclose(struct dev_close_args *ap) 260 { 261 cdev_t dev = ap->a_head.a_dev; 262 struct tty *tp, *tp2; 263 int err; 264 struct softpart *ourpart, *otherpart; 265 266 /* 267 * let the other end know that the game is up 268 */ 269 tp = dev->si_tty; 270 GETPARTS(tp, ourpart, otherpart); 271 tp2 = &otherpart->nm_tty; 272 (void)(*linesw[tp2->t_line].l_modem)(tp2, 0); 273 274 /* 275 * XXX MDMBUF makes no sense for nmdms but would inhibit the above 276 * l_modem(). CLOCAL makes sense but isn't supported. Special 277 * l_modem()s that ignore carrier drop make no sense for nmdms but 278 * may be in use because other parts of the line discipline make 279 * sense for nmdms. Recover by doing everything that a normal 280 * ttymodem() would have done except for sending a SIGHUP. 281 */ 282 if (tp2->t_state & TS_ISOPEN) { 283 tp2->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 284 tp2->t_state |= TS_ZOMBIE; 285 ttyflush(tp2, FREAD | FWRITE); 286 } 287 288 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag); 289 ourpart->modemsignals &= ~TIOCM_DTR; 290 nmdm_crossover(dev->si_drv1, ourpart, otherpart); 291 nmdmstop(tp, FREAD|FWRITE); 292 (void) ttyclose(tp); 293 return (err); 294 } 295 296 static int 297 nmdmread(struct dev_read_args *ap) 298 { 299 cdev_t dev = ap->a_head.a_dev; 300 int error = 0; 301 struct tty *tp, *tp2; 302 struct softpart *ourpart, *otherpart; 303 304 tp = dev->si_tty; 305 GETPARTS(tp, ourpart, otherpart); 306 tp2 = &otherpart->nm_tty; 307 308 #if 0 309 if (tp2->t_state & TS_ISOPEN) { 310 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, flag); 311 wakeup_other(tp, FWRITE); 312 } else { 313 if (flag & IO_NDELAY) { 314 return (EWOULDBLOCK); 315 } 316 error = tsleep(TSA_PTC_READ(tp), PCATCH, "nmdout", 0); 317 } 318 } 319 #else 320 if ((error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag)) == 0) 321 wakeup_other(tp, FWRITE); 322 #endif 323 return (error); 324 } 325 326 /* 327 * Write to pseudo-tty. 328 * Wakeups of controlling tty will happen 329 * indirectly, when tty driver calls nmdmstart. 330 */ 331 static int 332 nmdmwrite(struct dev_write_args *ap) 333 { 334 cdev_t dev = ap->a_head.a_dev; 335 struct uio *uio = ap->a_uio; 336 u_char *cp = 0; 337 int cc = 0; 338 u_char locbuf[BUFSIZ]; 339 int cnt = 0; 340 int error = 0; 341 struct tty *tp1, *tp; 342 struct softpart *ourpart, *otherpart; 343 344 tp1 = dev->si_tty; 345 /* 346 * Get the other tty struct. 347 * basically we are writing into the INPUT side of the other device. 348 */ 349 GETPARTS(tp1, ourpart, otherpart); 350 tp = &otherpart->nm_tty; 351 352 again: 353 if ((tp->t_state & TS_ISOPEN) == 0) 354 return (EIO); 355 while (uio->uio_resid > 0 || cc > 0) { 356 /* 357 * Fill up the buffer if it's empty 358 */ 359 if (cc == 0) { 360 cc = min(uio->uio_resid, BUFSIZ); 361 cp = locbuf; 362 error = uiomove((caddr_t)cp, cc, uio); 363 if (error) 364 return (error); 365 /* check again for safety */ 366 if ((tp->t_state & TS_ISOPEN) == 0) { 367 /* adjust for data copied in but not written */ 368 uio->uio_resid += cc; 369 return (EIO); 370 } 371 } 372 while (cc > 0) { 373 if (((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= (TTYHOG-2)) 374 && ((tp->t_canq.c_cc > 0) || !(tp->t_iflag&ICANON))) { 375 /* 376 * Come here to wait for space in outq, 377 * or space in rawq, or an empty canq. 378 */ 379 wakeup(TSA_HUP_OR_INPUT(tp)); 380 if ((tp->t_state & TS_CONNECTED) == 0) { 381 /* 382 * Data piled up because not connected. 383 * Adjust for data copied in but 384 * not written. 385 */ 386 uio->uio_resid += cc; 387 return (EIO); 388 } 389 if (ap->a_ioflag & IO_NDELAY) { 390 /* 391 * Don't wait if asked not to. 392 * Adjust for data copied in but 393 * not written. 394 */ 395 uio->uio_resid += cc; 396 if (cnt == 0) 397 return (EWOULDBLOCK); 398 return (0); 399 } 400 error = tsleep(TSA_PTC_WRITE(tp), 401 PCATCH, "nmdout", 0); 402 if (error) { 403 /* 404 * Tsleep returned (signal?). 405 * Go find out what the user wants. 406 * adjust for data copied in but 407 * not written 408 */ 409 uio->uio_resid += cc; 410 return (error); 411 } 412 goto again; 413 } 414 (*linesw[tp->t_line].l_rint)(*cp++, tp); 415 cnt++; 416 cc--; 417 } 418 cc = 0; 419 } 420 return (0); 421 } 422 423 /* 424 * Start output on pseudo-tty. 425 * Wake up process selecting or sleeping for input from controlling tty. 426 */ 427 static void 428 nmdmstart(struct tty *tp) 429 { 430 struct nm_softc *pti = tp->t_dev->si_drv1; 431 432 if (tp->t_state & TS_TTSTOP) 433 return; 434 pti->pt_flags &= ~PF_STOPPED; 435 wakeup_other(tp, FREAD); 436 } 437 438 /* Wakes up the OTHER tty;*/ 439 static void 440 wakeup_other(struct tty *tp, int flag) 441 { 442 struct softpart *ourpart, *otherpart; 443 444 GETPARTS(tp, ourpart, otherpart); 445 if (flag & FREAD) { 446 selwakeup(&otherpart->nm_tty.t_rsel); 447 wakeup(TSA_PTC_READ((&otherpart->nm_tty))); 448 } 449 if (flag & FWRITE) { 450 selwakeup(&otherpart->nm_tty.t_wsel); 451 wakeup(TSA_PTC_WRITE((&otherpart->nm_tty))); 452 } 453 } 454 455 static void 456 nmdmstop(struct tty *tp, int flush) 457 { 458 struct nm_softc *pti = tp->t_dev->si_drv1; 459 int flag; 460 461 /* note: FLUSHREAD and FLUSHWRITE already ok */ 462 if (flush == 0) { 463 flush = TIOCPKT_STOP; 464 pti->pt_flags |= PF_STOPPED; 465 } else 466 pti->pt_flags &= ~PF_STOPPED; 467 /* change of perspective */ 468 flag = 0; 469 if (flush & FREAD) 470 flag |= FWRITE; 471 if (flush & FWRITE) 472 flag |= FREAD; 473 wakeup_other(tp, flag); 474 } 475 476 /*ARGSUSED*/ 477 static int 478 nmdmioctl(struct dev_ioctl_args *ap) 479 { 480 cdev_t dev = ap->a_head.a_dev; 481 struct tty *tp = dev->si_tty; 482 struct nm_softc *pti = dev->si_drv1; 483 int error; 484 struct tty *tp2; 485 struct softpart *ourpart, *otherpart; 486 487 crit_enter(); 488 GETPARTS(tp, ourpart, otherpart); 489 tp2 = &otherpart->nm_tty; 490 491 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, 492 ap->a_fflag, ap->a_cred); 493 if (error == ENOIOCTL) 494 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag); 495 if (error == ENOIOCTL) { 496 switch (ap->a_cmd) { 497 case TIOCSBRK: 498 otherpart->gotbreak = 1; 499 break; 500 case TIOCCBRK: 501 break; 502 case TIOCSDTR: 503 ourpart->modemsignals |= TIOCM_DTR; 504 break; 505 case TIOCCDTR: 506 ourpart->modemsignals &= TIOCM_DTR; 507 break; 508 case TIOCMSET: 509 ourpart->modemsignals = *(int *)ap->a_data; 510 otherpart->modemsignals = *(int *)ap->a_data; 511 break; 512 case TIOCMBIS: 513 ourpart->modemsignals |= *(int *)ap->a_data; 514 break; 515 case TIOCMBIC: 516 ourpart->modemsignals &= ~(*(int *)ap->a_data); 517 otherpart->modemsignals &= ~(*(int *)ap->a_data); 518 break; 519 case TIOCMGET: 520 *(int *)ap->a_data = ourpart->modemsignals; 521 break; 522 case TIOCMSDTRWAIT: 523 break; 524 case TIOCMGDTRWAIT: 525 *(int *)ap->a_data = 0; 526 break; 527 case TIOCTIMESTAMP: 528 case TIOCDCDTIMESTAMP: 529 default: 530 crit_exit(); 531 error = ENOTTY; 532 return (error); 533 } 534 error = 0; 535 nmdm_crossover(pti, ourpart, otherpart); 536 } 537 crit_exit(); 538 return (error); 539 } 540 541 static void 542 nmdm_crossover(struct nm_softc *pti, 543 struct softpart *ourpart, 544 struct softpart *otherpart) 545 { 546 otherpart->modemsignals &= ~(TIOCM_CTS|TIOCM_CAR); 547 if (ourpart->modemsignals & TIOCM_RTS) 548 otherpart->modemsignals |= TIOCM_CTS; 549 if (ourpart->modemsignals & TIOCM_DTR) 550 otherpart->modemsignals |= TIOCM_CAR; 551 } 552 553 554 555 static void nmdm_drvinit (void *unused); 556 557 static void 558 nmdm_drvinit(void *unused) 559 { 560 /* XXX: Gross hack for DEVFS */ 561 nmdminit(0); 562 } 563 564 SYSINIT(nmdmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,nmdm_drvinit,NULL) 565