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); 212 if (sc == NULL) { 213 error = ENOMEM; 214 goto done; 215 } 216 bzero(sc, sizeof(*sc)); 217 sc->tp = tp; 218 sc->hotchar = NG_TTY_DFL_HOTCHAR; 219 sc->qtail = &sc->qhead; 220 QUEUECHECK(sc); 221 callout_handle_init(&sc->chand); 222 223 /* Setup netgraph node */ 224 ngt_nodeop_ok = 1; 225 error = ng_make_node_common(&typestruct, &sc->node); 226 ngt_nodeop_ok = 0; 227 if (error) { 228 FREE(sc, M_NETGRAPH); 229 goto done; 230 } 231 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++); 232 233 /* Set back pointers */ 234 sc->node->private = sc; 235 tp->t_sc = (caddr_t) sc; 236 237 /* Assign node its name */ 238 if ((error = ng_name_node(sc->node, name))) { 239 log(LOG_ERR, "%s: node name exists?\n", name); 240 ngt_nodeop_ok = 1; 241 ng_rmnode(sc->node); 242 ngt_nodeop_ok = 0; 243 goto done; 244 } 245 246 /* 247 * Pre-allocate cblocks to the an appropriate amount. 248 * I'm not sure what is appropriate. 249 */ 250 ttyflush(tp, FREAD | FWRITE); 251 clist_alloc_cblocks(&tp->t_canq, 0, 0); 252 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 253 clist_alloc_cblocks(&tp->t_outq, 254 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER); 255 256 done: 257 /* Done */ 258 splx(s); 259 return (error); 260 } 261 262 /* 263 * Line specific close routine, called from device close routine 264 * and from ttioctl at >= splsofttty(). This causes the node to 265 * be destroyed as well. 266 */ 267 static int 268 ngt_close(struct tty *tp, int flag) 269 { 270 const sc_p sc = (sc_p) tp->t_sc; 271 int s; 272 273 s = spltty(); 274 ttyflush(tp, FREAD | FWRITE); 275 clist_free_cblocks(&tp->t_outq); 276 tp->t_line = 0; 277 if (sc != NULL) { 278 if (sc->flags & FLG_TIMEOUT) { 279 untimeout(ngt_timeout, sc, sc->chand); 280 sc->flags &= ~FLG_TIMEOUT; 281 } 282 ngt_nodeop_ok = 1; 283 ng_rmnode(sc->node); 284 ngt_nodeop_ok = 0; 285 tp->t_sc = NULL; 286 } 287 splx(s); 288 return (0); 289 } 290 291 /* 292 * Once the device has been turned into a node, we don't allow reading. 293 */ 294 static int 295 ngt_read(struct tty *tp, struct uio *uio, int flag) 296 { 297 return (EIO); 298 } 299 300 /* 301 * Once the device has been turned into a node, we don't allow writing. 302 */ 303 static int 304 ngt_write(struct tty *tp, struct uio *uio, int flag) 305 { 306 return (EIO); 307 } 308 309 /* 310 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 311 */ 312 static int 313 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p) 314 { 315 const sc_p sc = (sc_p) tp->t_sc; 316 int s, error = 0; 317 318 s = spltty(); 319 switch (cmd) { 320 case NGIOCGINFO: 321 { 322 struct nodeinfo *const ni = (struct nodeinfo *) data; 323 const node_p node = sc->node; 324 325 bzero(ni, sizeof(*ni)); 326 if (node->name) 327 strncpy(ni->name, node->name, sizeof(ni->name) - 1); 328 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1); 329 ni->id = (u_int32_t) node; 330 ni->hooks = node->numhooks; 331 break; 332 } 333 default: 334 ERROUT(ENOIOCTL); 335 } 336 done: 337 splx(s); 338 return (error); 339 } 340 341 /* 342 * Receive data coming from the device. We get one character at 343 * a time, which is kindof silly. 344 * Only guaranteed to be at splsofttty() or spltty(). 345 */ 346 static int 347 ngt_input(int c, struct tty *tp) 348 { 349 const sc_p sc = (sc_p) tp->t_sc; 350 const node_p node = sc->node; 351 struct mbuf *m; 352 int s, error = 0; 353 354 if (!sc || tp != sc->tp) 355 return (0); 356 s = spltty(); 357 if (!sc->hook) 358 ERROUT(0); 359 360 /* Check for error conditions */ 361 if ((tp->t_state & TS_CONNECTED) == 0) { 362 if (sc->flags & FLG_DEBUG) 363 log(LOG_DEBUG, "%s: no carrier\n", node->name); 364 ERROUT(0); 365 } 366 if (c & TTY_ERRORMASK) { 367 /* framing error or overrun on this char */ 368 if (sc->flags & FLG_DEBUG) 369 log(LOG_DEBUG, "%s: line error %x\n", 370 node->name, c & TTY_ERRORMASK); 371 ERROUT(0); 372 } 373 c &= TTY_CHARMASK; 374 375 /* Get a new header mbuf if we need one */ 376 if (!(m = sc->m)) { 377 MGETHDR(m, M_DONTWAIT, MT_DATA); 378 if (!m) { 379 if (sc->flags & FLG_DEBUG) 380 log(LOG_ERR, 381 "%s: can't get mbuf\n", node->name); 382 ERROUT(ENOBUFS); 383 } 384 m->m_len = m->m_pkthdr.len = 0; 385 m->m_pkthdr.rcvif = NULL; 386 sc->m = m; 387 } 388 389 /* Add char to mbuf */ 390 *mtod(m, u_char *) = c; 391 m->m_data++; 392 m->m_len++; 393 m->m_pkthdr.len++; 394 395 /* Ship off mbuf if it's time */ 396 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) { 397 m->m_data = m->m_pktdat; 398 error = ng_queue_data(sc->hook, m, NULL); 399 sc->m = NULL; 400 } 401 done: 402 splx(s); 403 return (error); 404 } 405 406 /* 407 * This is called when the device driver is ready for more output. 408 * Called from tty system at splsofttty() or spltty(). 409 * Also call from ngt_rcv_data() when a new mbuf is available for output. 410 */ 411 static int 412 ngt_start(struct tty *tp) 413 { 414 const sc_p sc = (sc_p) tp->t_sc; 415 int s; 416 417 s = spltty(); 418 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */ 419 struct mbuf *m = sc->qhead; 420 421 /* Remove first mbuf from queue */ 422 if (!m) 423 break; 424 if ((sc->qhead = m->m_nextpkt) == NULL) 425 sc->qtail = &sc->qhead; 426 sc->qlen--; 427 QUEUECHECK(sc); 428 429 /* Send as much of it as possible */ 430 while (m) { 431 struct mbuf *m2; 432 int sent; 433 434 sent = m->m_len 435 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq); 436 m->m_data += sent; 437 m->m_len -= sent; 438 if (m->m_len > 0) 439 break; /* device can't take no more */ 440 MFREE(m, m2); 441 m = m2; 442 } 443 444 /* Put remainder of mbuf chain (if any) back on queue */ 445 if (m) { 446 m->m_nextpkt = sc->qhead; 447 sc->qhead = m; 448 if (sc->qtail == &sc->qhead) 449 sc->qtail = &m->m_nextpkt; 450 sc->qlen++; 451 QUEUECHECK(sc); 452 break; 453 } 454 } 455 456 /* Call output process whether or not there is any output. We are 457 * being called in lieu of ttstart and must do what it would. */ 458 if (tp->t_oproc != NULL) 459 (*tp->t_oproc) (tp); 460 461 /* This timeout is needed for operation on a pseudo-tty, because the 462 * pty code doesn't call pppstart after it has drained the t_outq. */ 463 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) { 464 sc->chand = timeout(ngt_timeout, sc, 1); 465 sc->flags |= FLG_TIMEOUT; 466 } 467 splx(s); 468 return (0); 469 } 470 471 /* 472 * We still have data to output to the device, so try sending more. 473 */ 474 static void 475 ngt_timeout(void *arg) 476 { 477 const sc_p sc = (sc_p) arg; 478 int s; 479 480 s = spltty(); 481 sc->flags &= ~FLG_TIMEOUT; 482 ngt_start(sc->tp); 483 splx(s); 484 } 485 486 /****************************************************************** 487 NETGRAPH NODE METHODS 488 ******************************************************************/ 489 490 /* 491 * Initialize a new node of this type. 492 * 493 * We only allow nodes to be created as a result of setting 494 * the line discipline on a tty, so always return an error if not. 495 */ 496 static int 497 ngt_constructor(node_p *nodep) 498 { 499 if (!ngt_nodeop_ok) 500 return (EOPNOTSUPP); 501 return (ng_make_node_common(&typestruct, nodep)); 502 } 503 504 /* 505 * Add a new hook. There can only be one. 506 */ 507 static int 508 ngt_newhook(node_p node, hook_p hook, const char *name) 509 { 510 const sc_p sc = node->private; 511 int s, error = 0; 512 513 if (strcmp(name, NG_TTY_HOOK)) 514 return (EINVAL); 515 s = spltty(); 516 if (sc->hook) 517 ERROUT(EISCONN); 518 sc->hook = hook; 519 done: 520 splx(s); 521 return (error); 522 } 523 524 /* 525 * Disconnect the hook 526 */ 527 static int 528 ngt_disconnect(hook_p hook) 529 { 530 const sc_p sc = hook->node->private; 531 int s; 532 533 s = spltty(); 534 if (hook != sc->hook) 535 panic(__FUNCTION__); 536 sc->hook = NULL; 537 m_freem(sc->m); 538 sc->m = NULL; 539 splx(s); 540 return (0); 541 } 542 543 /* 544 * Remove this node. The does the netgraph portion of the shutdown. 545 * This should only be called indirectly from ngt_close(). 546 */ 547 static int 548 ngt_shutdown(node_p node) 549 { 550 const sc_p sc = node->private; 551 552 if (!ngt_nodeop_ok) 553 return (EOPNOTSUPP); 554 ng_unname(node); 555 ng_cutlinks(node); 556 node->private = NULL; 557 ng_unref(sc->node); 558 m_freem(sc->qhead); 559 m_freem(sc->m); 560 bzero(sc, sizeof(*sc)); 561 FREE(sc, M_NETGRAPH); 562 return (0); 563 } 564 565 /* 566 * Receive incoming data from netgraph system. Put it on our 567 * output queue and start output if necessary. 568 */ 569 static int 570 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 571 struct mbuf **ret_m, meta_p *ret_meta) 572 { 573 const sc_p sc = hook->node->private; 574 int s, error = 0; 575 576 if (hook != sc->hook) 577 panic(__FUNCTION__); 578 NG_FREE_META(meta); 579 s = spltty(); 580 if (sc->qlen >= MAX_MBUFQ) 581 ERROUT(ENOBUFS); 582 m->m_nextpkt = NULL; 583 *sc->qtail = m; 584 sc->qtail = &m->m_nextpkt; 585 sc->qlen++; 586 QUEUECHECK(sc); 587 m = NULL; 588 if (sc->qlen == 1) 589 ngt_start(sc->tp); 590 done: 591 splx(s); 592 if (m) 593 m_freem(m); 594 return (error); 595 } 596 597 /* 598 * Receive control message 599 */ 600 static int 601 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 602 struct ng_mesg **rptr, hook_p lasthook) 603 { 604 const sc_p sc = (sc_p) node->private; 605 struct ng_mesg *resp = NULL; 606 int error = 0; 607 608 switch (msg->header.typecookie) { 609 case NGM_TTY_COOKIE: 610 switch (msg->header.cmd) { 611 case NGM_TTY_SET_HOTCHAR: 612 { 613 int hotchar; 614 615 if (msg->header.arglen != sizeof(int)) 616 ERROUT(EINVAL); 617 hotchar = *((int *) msg->data); 618 if (hotchar != (u_char) hotchar && hotchar != -1) 619 ERROUT(EINVAL); 620 sc->hotchar = hotchar; /* race condition is OK */ 621 break; 622 } 623 case NGM_TTY_GET_HOTCHAR: 624 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT); 625 if (!resp) 626 ERROUT(ENOMEM); 627 /* Race condition here is OK */ 628 *((int *) resp->data) = sc->hotchar; 629 break; 630 default: 631 ERROUT(EINVAL); 632 } 633 break; 634 default: 635 ERROUT(EINVAL); 636 } 637 if (rptr) 638 *rptr = resp; 639 else if (resp) 640 FREE(resp, M_NETGRAPH); 641 642 done: 643 FREE(msg, M_NETGRAPH); 644 return (error); 645 } 646 647 /****************************************************************** 648 INITIALIZATION 649 ******************************************************************/ 650 651 /* 652 * Handle loading and unloading for this node type 653 */ 654 static int 655 ngt_mod_event(module_t mod, int event, void *data) 656 { 657 /* struct ng_type *const type = data;*/ 658 int s, error = 0; 659 660 switch (event) { 661 case MOD_LOAD: 662 663 /* Register line discipline */ 664 s = spltty(); 665 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) { 666 splx(s); 667 log(LOG_ERR, "%s: can't register line discipline", 668 __FUNCTION__); 669 return (EIO); 670 } 671 splx(s); 672 break; 673 674 case MOD_UNLOAD: 675 676 /* Unregister line discipline */ 677 s = spltty(); 678 ldisc_deregister(ngt_ldisc); 679 splx(s); 680 break; 681 682 default: 683 error = EOPNOTSUPP; 684 break; 685 } 686 return (error); 687 } 688 689