1 2 /* 3 * ng_tty.c 4 * 5 * Copyright (c) 1996-1999 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Author: Archie Cobbs <archie@freebsd.org> 38 * 39 * $FreeBSD$ 40 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $ 41 */ 42 43 /* 44 * This file implements a terminal line discipline that is also a 45 * netgraph node. Installing this line discipline on a terminal device 46 * instantiates a new netgraph node of this type, which allows access 47 * to the device via the "hook" hook of the node. 48 * 49 * Once the line discipline is installed, you can find out the name 50 * of the corresponding netgraph node via a NGIOCGINFO ioctl(). 51 * 52 * Incoming characters are delievered to the hook one at a time, each 53 * in its own mbuf. You may optionally define a ``hotchar,'' which causes 54 * incoming characters to be buffered up until either the hotchar is 55 * seen or the mbuf is full (MHLEN bytes). Then all buffered characters 56 * are immediately delivered. 57 * 58 * NOTE: This node operates at spltty(). 59 */ 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/kernel.h> 64 #include <sys/conf.h> 65 #include <sys/mbuf.h> 66 #include <sys/malloc.h> 67 #include <sys/fcntl.h> 68 #include <sys/tty.h> 69 #include <sys/ttycom.h> 70 #include <sys/syslog.h> 71 #include <sys/errno.h> 72 #include <sys/ioccom.h> 73 74 #include <netgraph/ng_message.h> 75 #include <netgraph/netgraph.h> 76 #include <netgraph/ng_tty.h> 77 78 #ifdef __i386__ /* fiddle with the spl locking */ 79 #include <sys/bus.h> 80 #include <machine/ipl.h> 81 #endif 82 83 /* Misc defs */ 84 #define MAX_MBUFQ 3 /* Max number of queued mbufs */ 85 #define NGT_HIWATER 400 /* High water mark on output */ 86 87 /* Per-node private info */ 88 struct ngt_sc { 89 struct tty *tp; /* Terminal device */ 90 node_p node; /* Netgraph node */ 91 hook_p hook; /* Netgraph hook */ 92 struct mbuf *m; /* Incoming data buffer */ 93 struct mbuf *qhead, **qtail; /* Queue of outgoing mbuf's */ 94 short qlen; /* Length of queue */ 95 short hotchar; /* Hotchar, or -1 if none */ 96 u_int flags; /* Flags */ 97 struct callout_handle chand; /* See man timeout(9) */ 98 }; 99 typedef struct ngt_sc *sc_p; 100 101 /* Flags */ 102 #define FLG_TIMEOUT 0x0001 /* A timeout is pending */ 103 #define FLG_DEBUG 0x0002 104 105 /* Debugging */ 106 #ifdef INVARIANTS 107 #define QUEUECHECK(sc) \ 108 do { \ 109 struct mbuf **mp; \ 110 int k; \ 111 \ 112 for (k = 0, mp = &sc->qhead; \ 113 k <= MAX_MBUFQ && *mp; \ 114 k++, mp = &(*mp)->m_nextpkt); \ 115 if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \ 116 panic(__FUNCTION__ ": queue"); \ 117 } while (0) 118 #else 119 #define QUEUECHECK(sc) do {} while (0) 120 #endif 121 122 /* Line discipline methods */ 123 static int ngt_open(dev_t dev, struct tty *tp); 124 static int ngt_close(struct tty *tp, int flag); 125 static int ngt_read(struct tty *tp, struct uio *uio, int flag); 126 static int ngt_write(struct tty *tp, struct uio *uio, int flag); 127 static int ngt_tioctl(struct tty *tp, 128 u_long cmd, caddr_t data, int flag, struct proc *); 129 static int ngt_input(int c, struct tty *tp); 130 static int ngt_start(struct tty *tp); 131 132 /* Netgraph methods */ 133 static ng_constructor_t ngt_constructor; 134 static ng_rcvmsg_t ngt_rcvmsg; 135 static ng_shutdown_t ngt_shutdown; 136 static ng_newhook_t ngt_newhook; 137 static ng_rcvdata_t ngt_rcvdata; 138 static ng_disconnect_t ngt_disconnect; 139 static int ngt_mod_event(module_t mod, int event, void *data); 140 141 /* Other stuff */ 142 static void ngt_timeout(void *arg); 143 144 #define ERROUT(x) do { error = (x); goto done; } while (0) 145 146 /* Line discipline descriptor */ 147 static struct linesw ngt_disc = { 148 ngt_open, 149 ngt_close, 150 ngt_read, 151 ngt_write, 152 ngt_tioctl, 153 ngt_input, 154 ngt_start, 155 ttymodem, 156 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */ 157 }; 158 159 /* Netgraph node type descriptor */ 160 static struct ng_type typestruct = { 161 NG_VERSION, 162 NG_TTY_NODE_TYPE, 163 ngt_mod_event, 164 ngt_constructor, 165 ngt_rcvmsg, 166 ngt_shutdown, 167 ngt_newhook, 168 NULL, 169 NULL, 170 ngt_rcvdata, 171 ngt_rcvdata, 172 ngt_disconnect, 173 NULL 174 }; 175 NETGRAPH_INIT(tty, &typestruct); 176 177 static int ngt_unit; 178 static int ngt_nodeop_ok; /* OK to create/remove node */ 179 static int ngt_ldisc; 180 181 /****************************************************************** 182 LINE DISCIPLINE METHODS 183 ******************************************************************/ 184 185 /* 186 * Set our line discipline on the tty. 187 * Called from device open routine or ttioctl() at >= splsofttty() 188 */ 189 static int 190 ngt_open(dev_t dev, struct tty *tp) 191 { 192 struct proc *const p = curproc; /* XXX */ 193 char name[sizeof(NG_TTY_NODE_TYPE) + 8]; 194 sc_p sc; 195 int s, error; 196 197 /* Super-user only */ 198 if ((error = suser(p))) 199 return (error); 200 s = splnet(); 201 (void) spltty(); /* XXX is this necessary? */ 202 203 /* Already installed? */ 204 if (tp->t_line == NETGRAPHDISC) { 205 sc = (sc_p) tp->t_sc; 206 if (sc != NULL && sc->tp == tp) 207 goto done; 208 } 209 210 /* Initialize private struct */ 211 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 212 if (sc == NULL) { 213 error = ENOMEM; 214 goto done; 215 } 216 sc->tp = tp; 217 sc->hotchar = NG_TTY_DFL_HOTCHAR; 218 sc->qtail = &sc->qhead; 219 QUEUECHECK(sc); 220 callout_handle_init(&sc->chand); 221 222 /* Setup netgraph node */ 223 ngt_nodeop_ok = 1; 224 error = ng_make_node_common(&typestruct, &sc->node); 225 ngt_nodeop_ok = 0; 226 if (error) { 227 FREE(sc, M_NETGRAPH); 228 goto done; 229 } 230 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++); 231 232 /* Set back pointers */ 233 sc->node->private = sc; 234 tp->t_sc = (caddr_t) sc; 235 236 /* Assign node its name */ 237 if ((error = ng_name_node(sc->node, name))) { 238 log(LOG_ERR, "%s: node name exists?\n", name); 239 ngt_nodeop_ok = 1; 240 ng_rmnode(sc->node); 241 ngt_nodeop_ok = 0; 242 goto done; 243 } 244 245 /* 246 * Pre-allocate cblocks to the an appropriate amount. 247 * I'm not sure what is appropriate. 248 */ 249 ttyflush(tp, FREAD | FWRITE); 250 clist_alloc_cblocks(&tp->t_canq, 0, 0); 251 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 252 clist_alloc_cblocks(&tp->t_outq, 253 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER); 254 255 done: 256 /* Done */ 257 splx(s); 258 return (error); 259 } 260 261 /* 262 * Line specific close routine, called from device close routine 263 * and from ttioctl at >= splsofttty(). This causes the node to 264 * be destroyed as well. 265 */ 266 static int 267 ngt_close(struct tty *tp, int flag) 268 { 269 const sc_p sc = (sc_p) tp->t_sc; 270 int s; 271 272 s = spltty(); 273 ttyflush(tp, FREAD | FWRITE); 274 clist_free_cblocks(&tp->t_outq); 275 tp->t_line = 0; 276 if (sc != NULL) { 277 if (sc->flags & FLG_TIMEOUT) { 278 untimeout(ngt_timeout, sc, sc->chand); 279 sc->flags &= ~FLG_TIMEOUT; 280 } 281 ngt_nodeop_ok = 1; 282 ng_rmnode(sc->node); 283 ngt_nodeop_ok = 0; 284 tp->t_sc = NULL; 285 } 286 splx(s); 287 return (0); 288 } 289 290 /* 291 * Once the device has been turned into a node, we don't allow reading. 292 */ 293 static int 294 ngt_read(struct tty *tp, struct uio *uio, int flag) 295 { 296 return (EIO); 297 } 298 299 /* 300 * Once the device has been turned into a node, we don't allow writing. 301 */ 302 static int 303 ngt_write(struct tty *tp, struct uio *uio, int flag) 304 { 305 return (EIO); 306 } 307 308 /* 309 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 310 */ 311 static int 312 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p) 313 { 314 const sc_p sc = (sc_p) tp->t_sc; 315 int s, error = 0; 316 317 s = spltty(); 318 switch (cmd) { 319 case NGIOCGINFO: 320 { 321 struct nodeinfo *const ni = (struct nodeinfo *) data; 322 const node_p node = sc->node; 323 324 bzero(ni, sizeof(*ni)); 325 if (node->name) 326 strncpy(ni->name, node->name, sizeof(ni->name) - 1); 327 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1); 328 ni->id = (u_int32_t) node; 329 ni->hooks = node->numhooks; 330 break; 331 } 332 default: 333 ERROUT(ENOIOCTL); 334 } 335 done: 336 splx(s); 337 return (error); 338 } 339 340 /* 341 * Receive data coming from the device. We get one character at 342 * a time, which is kindof silly. 343 * Only guaranteed to be at splsofttty() or spltty(). 344 */ 345 static int 346 ngt_input(int c, struct tty *tp) 347 { 348 const sc_p sc = (sc_p) tp->t_sc; 349 const node_p node = sc->node; 350 struct mbuf *m; 351 int s, error = 0; 352 353 if (!sc || tp != sc->tp) 354 return (0); 355 s = spltty(); 356 if (!sc->hook) 357 ERROUT(0); 358 359 /* Check for error conditions */ 360 if ((tp->t_state & TS_CONNECTED) == 0) { 361 if (sc->flags & FLG_DEBUG) 362 log(LOG_DEBUG, "%s: no carrier\n", node->name); 363 ERROUT(0); 364 } 365 if (c & TTY_ERRORMASK) { 366 /* framing error or overrun on this char */ 367 if (sc->flags & FLG_DEBUG) 368 log(LOG_DEBUG, "%s: line error %x\n", 369 node->name, c & TTY_ERRORMASK); 370 ERROUT(0); 371 } 372 c &= TTY_CHARMASK; 373 374 /* Get a new header mbuf if we need one */ 375 if (!(m = sc->m)) { 376 MGETHDR(m, M_DONTWAIT, MT_DATA); 377 if (!m) { 378 if (sc->flags & FLG_DEBUG) 379 log(LOG_ERR, 380 "%s: can't get mbuf\n", node->name); 381 ERROUT(ENOBUFS); 382 } 383 m->m_len = m->m_pkthdr.len = 0; 384 m->m_pkthdr.rcvif = NULL; 385 sc->m = m; 386 } 387 388 /* Add char to mbuf */ 389 *mtod(m, u_char *) = c; 390 m->m_data++; 391 m->m_len++; 392 m->m_pkthdr.len++; 393 394 /* Ship off mbuf if it's time */ 395 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) { 396 m->m_data = m->m_pktdat; 397 error = ng_queue_data(sc->hook, m, NULL); 398 sc->m = NULL; 399 } 400 done: 401 splx(s); 402 return (error); 403 } 404 405 /* 406 * This is called when the device driver is ready for more output. 407 * Called from tty system at splsofttty() or spltty(). 408 * Also call from ngt_rcv_data() when a new mbuf is available for output. 409 */ 410 static int 411 ngt_start(struct tty *tp) 412 { 413 const sc_p sc = (sc_p) tp->t_sc; 414 int s; 415 416 s = spltty(); 417 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */ 418 struct mbuf *m = sc->qhead; 419 420 /* Remove first mbuf from queue */ 421 if (!m) 422 break; 423 if ((sc->qhead = m->m_nextpkt) == NULL) 424 sc->qtail = &sc->qhead; 425 sc->qlen--; 426 QUEUECHECK(sc); 427 428 /* Send as much of it as possible */ 429 while (m) { 430 struct mbuf *m2; 431 int sent; 432 433 sent = m->m_len 434 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq); 435 m->m_data += sent; 436 m->m_len -= sent; 437 if (m->m_len > 0) 438 break; /* device can't take no more */ 439 MFREE(m, m2); 440 m = m2; 441 } 442 443 /* Put remainder of mbuf chain (if any) back on queue */ 444 if (m) { 445 m->m_nextpkt = sc->qhead; 446 sc->qhead = m; 447 if (sc->qtail == &sc->qhead) 448 sc->qtail = &m->m_nextpkt; 449 sc->qlen++; 450 QUEUECHECK(sc); 451 break; 452 } 453 } 454 455 /* Call output process whether or not there is any output. We are 456 * being called in lieu of ttstart and must do what it would. */ 457 if (tp->t_oproc != NULL) 458 (*tp->t_oproc) (tp); 459 460 /* This timeout is needed for operation on a pseudo-tty, because the 461 * pty code doesn't call pppstart after it has drained the t_outq. */ 462 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) { 463 sc->chand = timeout(ngt_timeout, sc, 1); 464 sc->flags |= FLG_TIMEOUT; 465 } 466 splx(s); 467 return (0); 468 } 469 470 /* 471 * We still have data to output to the device, so try sending more. 472 */ 473 static void 474 ngt_timeout(void *arg) 475 { 476 const sc_p sc = (sc_p) arg; 477 int s; 478 479 s = spltty(); 480 sc->flags &= ~FLG_TIMEOUT; 481 ngt_start(sc->tp); 482 splx(s); 483 } 484 485 /****************************************************************** 486 NETGRAPH NODE METHODS 487 ******************************************************************/ 488 489 /* 490 * Initialize a new node of this type. 491 * 492 * We only allow nodes to be created as a result of setting 493 * the line discipline on a tty, so always return an error if not. 494 */ 495 static int 496 ngt_constructor(node_p *nodep) 497 { 498 if (!ngt_nodeop_ok) 499 return (EOPNOTSUPP); 500 return (ng_make_node_common(&typestruct, nodep)); 501 } 502 503 /* 504 * Add a new hook. There can only be one. 505 */ 506 static int 507 ngt_newhook(node_p node, hook_p hook, const char *name) 508 { 509 const sc_p sc = node->private; 510 int s, error = 0; 511 512 if (strcmp(name, NG_TTY_HOOK)) 513 return (EINVAL); 514 s = spltty(); 515 if (sc->hook) 516 ERROUT(EISCONN); 517 sc->hook = hook; 518 done: 519 splx(s); 520 return (error); 521 } 522 523 /* 524 * Disconnect the hook 525 */ 526 static int 527 ngt_disconnect(hook_p hook) 528 { 529 const sc_p sc = hook->node->private; 530 int s; 531 532 s = spltty(); 533 if (hook != sc->hook) 534 panic(__FUNCTION__); 535 sc->hook = NULL; 536 m_freem(sc->m); 537 sc->m = NULL; 538 splx(s); 539 return (0); 540 } 541 542 /* 543 * Remove this node. The does the netgraph portion of the shutdown. 544 * This should only be called indirectly from ngt_close(). 545 */ 546 static int 547 ngt_shutdown(node_p node) 548 { 549 const sc_p sc = node->private; 550 551 if (!ngt_nodeop_ok) 552 return (EOPNOTSUPP); 553 ng_unname(node); 554 ng_cutlinks(node); 555 node->private = NULL; 556 ng_unref(sc->node); 557 m_freem(sc->qhead); 558 m_freem(sc->m); 559 bzero(sc, sizeof(*sc)); 560 FREE(sc, M_NETGRAPH); 561 return (0); 562 } 563 564 /* 565 * Receive incoming data from netgraph system. Put it on our 566 * output queue and start output if necessary. 567 */ 568 static int 569 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 570 struct mbuf **ret_m, meta_p *ret_meta) 571 { 572 const sc_p sc = hook->node->private; 573 int s, error = 0; 574 575 if (hook != sc->hook) 576 panic(__FUNCTION__); 577 NG_FREE_META(meta); 578 s = spltty(); 579 if (sc->qlen >= MAX_MBUFQ) 580 ERROUT(ENOBUFS); 581 m->m_nextpkt = NULL; 582 *sc->qtail = m; 583 sc->qtail = &m->m_nextpkt; 584 sc->qlen++; 585 QUEUECHECK(sc); 586 m = NULL; 587 if (sc->qlen == 1) 588 ngt_start(sc->tp); 589 done: 590 splx(s); 591 if (m) 592 m_freem(m); 593 return (error); 594 } 595 596 /* 597 * Receive control message 598 */ 599 static int 600 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 601 struct ng_mesg **rptr, hook_p lasthook) 602 { 603 const sc_p sc = (sc_p) node->private; 604 struct ng_mesg *resp = NULL; 605 int error = 0; 606 607 switch (msg->header.typecookie) { 608 case NGM_TTY_COOKIE: 609 switch (msg->header.cmd) { 610 case NGM_TTY_SET_HOTCHAR: 611 { 612 int hotchar; 613 614 if (msg->header.arglen != sizeof(int)) 615 ERROUT(EINVAL); 616 hotchar = *((int *) msg->data); 617 if (hotchar != (u_char) hotchar && hotchar != -1) 618 ERROUT(EINVAL); 619 sc->hotchar = hotchar; /* race condition is OK */ 620 break; 621 } 622 case NGM_TTY_GET_HOTCHAR: 623 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT); 624 if (!resp) 625 ERROUT(ENOMEM); 626 /* Race condition here is OK */ 627 *((int *) resp->data) = sc->hotchar; 628 break; 629 default: 630 ERROUT(EINVAL); 631 } 632 break; 633 default: 634 ERROUT(EINVAL); 635 } 636 if (rptr) 637 *rptr = resp; 638 else if (resp) 639 FREE(resp, M_NETGRAPH); 640 641 done: 642 FREE(msg, M_NETGRAPH); 643 return (error); 644 } 645 646 /****************************************************************** 647 INITIALIZATION 648 ******************************************************************/ 649 650 /* 651 * Handle loading and unloading for this node type 652 */ 653 static int 654 ngt_mod_event(module_t mod, int event, void *data) 655 { 656 /* struct ng_type *const type = data;*/ 657 int s, error = 0; 658 659 switch (event) { 660 case MOD_LOAD: 661 662 /* Register line discipline */ 663 s = spltty(); 664 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) { 665 splx(s); 666 log(LOG_ERR, "%s: can't register line discipline", 667 __FUNCTION__); 668 return (EIO); 669 } 670 splx(s); 671 break; 672 673 case MOD_UNLOAD: 674 675 /* Unregister line discipline */ 676 s = spltty(); 677 ldisc_deregister(ngt_ldisc); 678 splx(s); 679 break; 680 681 default: 682 error = EOPNOTSUPP; 683 break; 684 } 685 return (error); 686 } 687 688