1 /*- 2 * (MPSAFE) 3 * 4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * ng_h4.c 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: ng_h4.c,v 1.10 2005/10/31 17:57:43 max Exp $ 31 * $FreeBSD: head/sys/netgraph/bluetooth/drivers/h4/ng_h4.c 243882 2012-12-05 08:04:20Z glebius $ 32 * 33 * Based on: 34 * --------- 35 * 36 * FreeBSD: src/sys/netgraph/ng_tty.c 37 * Author: Archie Cobbs <archie@freebsd.org> 38 * 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/conf.h> 45 #include <sys/endian.h> 46 #include <sys/errno.h> 47 #include <sys/fcntl.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/priv.h> 51 #include <sys/socket.h> 52 #include <sys/tty.h> 53 #include <sys/ttycom.h> 54 #include <net/if.h> 55 #include <net/if_var.h> 56 #include <netgraph7/ng_message.h> 57 #include <netgraph7/netgraph.h> 58 #include <netgraph7/ng_parse.h> 59 #include <netgraph7/bluetooth/include/ng_bluetooth.h> 60 #include <netgraph7/bluetooth/include/ng_hci.h> 61 #include <netgraph7/bluetooth/include/ng_h4.h> 62 #include <netgraph7/bluetooth/drivers/h4/ng_h4_var.h> 63 #include <netgraph7/bluetooth/drivers/h4/ng_h4_prse.h> 64 65 /***************************************************************************** 66 ***************************************************************************** 67 ** This node implements a Bluetooth HCI UART transport layer as per chapter 68 ** H4 of the Bluetooth Specification Book v1.1. It is a terminal line 69 ** discipline that is also a netgraph node. Installing this line discipline 70 ** on a terminal device instantiates a new netgraph node of this type, which 71 ** allows access to the device via the "hook" hook of the node. 72 ** 73 ** Once the line discipline is installed, you can find out the name of the 74 ** corresponding netgraph node via a NGIOCGINFO ioctl(). 75 ***************************************************************************** 76 *****************************************************************************/ 77 78 /* MALLOC define */ 79 #ifndef NG_SEPARATE_MALLOC 80 MALLOC_DEFINE(M_NETGRAPH_H4, "netgraph_h4", "Netgraph Bluetooth H4 node"); 81 #else 82 #define M_NETGRAPH_H4 M_NETGRAPH 83 #endif /* NG_SEPARATE_MALLOC */ 84 85 /* Line discipline methods */ 86 static int ng_h4_open (struct cdev *, struct tty *); 87 static int ng_h4_close (struct tty *, int); 88 static int ng_h4_read (struct tty *, struct uio *, int); 89 static int ng_h4_write (struct tty *, struct uio *, int); 90 static int ng_h4_input (int, struct tty *); 91 static int ng_h4_start (struct tty *); 92 static int ng_h4_ioctl (struct tty *, u_long, caddr_t, 93 int, struct ucred *); 94 95 /* Line discipline descriptor */ 96 static struct linesw ng_h4_disc = { 97 ng_h4_open, /* open */ 98 ng_h4_close, /* close */ 99 ng_h4_read, /* read */ 100 ng_h4_write, /* write */ 101 ng_h4_ioctl, /* ioctl */ 102 ng_h4_input, /* input */ 103 ng_h4_start, /* start */ 104 ttymodem /* modem */ 105 }; 106 107 /* Netgraph methods */ 108 static ng_constructor_t ng_h4_constructor; 109 static ng_rcvmsg_t ng_h4_rcvmsg; 110 static ng_shutdown_t ng_h4_shutdown; 111 static ng_newhook_t ng_h4_newhook; 112 static ng_connect_t ng_h4_connect; 113 static ng_rcvdata_t ng_h4_rcvdata; 114 static ng_disconnect_t ng_h4_disconnect; 115 116 /* Other stuff */ 117 static void ng_h4_process_timeout (node_p, hook_p, void *, int); 118 static int ng_h4_mod_event (module_t, int, void *); 119 120 /* Netgraph node type descriptor */ 121 static struct ng_type typestruct = { 122 .version = NG_ABI_VERSION, 123 .name = NG_H4_NODE_TYPE, 124 .mod_event = ng_h4_mod_event, 125 .constructor = ng_h4_constructor, 126 .rcvmsg = ng_h4_rcvmsg, 127 .shutdown = ng_h4_shutdown, 128 .newhook = ng_h4_newhook, 129 .connect = ng_h4_connect, 130 .rcvdata = ng_h4_rcvdata, 131 .disconnect = ng_h4_disconnect, 132 .cmdlist = ng_h4_cmdlist 133 }; 134 NETGRAPH_INIT(h4, &typestruct); 135 MODULE_VERSION(ng_h4, NG_BLUETOOTH_VERSION); 136 137 static int ng_h4_node = 0; 138 139 /***************************************************************************** 140 ***************************************************************************** 141 ** Line discipline methods 142 ***************************************************************************** 143 *****************************************************************************/ 144 145 /* 146 * Set our line discipline on the tty. 147 */ 148 149 static int 150 ng_h4_open(struct cdev *dev, struct tty *tp) 151 { 152 struct thread *td = curthread; 153 char name[NG_NODESIZ]; 154 ng_h4_info_p sc = NULL; 155 int error; 156 157 /* Super-user only */ 158 error = priv_check(td, PRIV_NETGRAPH_TTY); /* XXX */ 159 if (error != 0) 160 return (error); 161 162 /* Initialize private struct */ 163 sc = kmalloc(sizeof(*sc), M_NETGRAPH_H4, M_WAITOK | M_NULLOK | M_ZERO); 164 if (sc == NULL) 165 return (ENOMEM); 166 167 lwkt_gettoken(&tty_token); 168 sc->tp = tp; 169 sc->debug = NG_H4_WARN_LEVEL; 170 171 sc->state = NG_H4_W4_PKT_IND; 172 sc->want = 1; 173 sc->got = 0; 174 175 sc->outq.ifq_maxlen = NG_H4_DEFAULTQLEN; 176 ng_callout_init(&sc->timo); 177 178 NG_H4_LOCK(sc); 179 180 /* Setup netgraph node */ 181 error = ng_make_node_common(&typestruct, &sc->node); 182 if (error != 0) { 183 NG_H4_UNLOCK(sc); 184 185 kprintf("%s: Unable to create new node!\n", __func__); 186 187 bzero(sc, sizeof(*sc)); 188 kfree(sc, M_NETGRAPH_H4); 189 190 lwkt_reltoken(&tty_token); 191 return (error); 192 } 193 194 /* Assign node its name */ 195 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ng_h4_node ++); 196 197 error = ng_name_node(sc->node, name); 198 if (error != 0) { 199 NG_H4_UNLOCK(sc); 200 201 kprintf("%s: %s - node name exists?\n", __func__, name); 202 203 NG_NODE_UNREF(sc->node); 204 bzero(sc, sizeof(*sc)); 205 kfree(sc, M_NETGRAPH_H4); 206 207 lwkt_reltoken(&tty_token); 208 return (error); 209 } 210 211 /* Set back pointers */ 212 NG_NODE_SET_PRIVATE(sc->node, sc); 213 tp->t_sc = (caddr_t) sc; 214 215 /* The node has to be a WRITER because data can change node status */ 216 NG_NODE_FORCE_WRITER(sc->node); 217 218 /* 219 * Pre-allocate cblocks to the an appropriate amount. 220 * I'm not sure what is appropriate. 221 */ 222 223 ttyflush(tp, FREAD | FWRITE); 224 clist_alloc_cblocks(&tp->t_canq, 0, 0); 225 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 226 clist_alloc_cblocks(&tp->t_outq, 227 MLEN + NG_H4_HIWATER, MLEN + NG_H4_HIWATER); 228 229 NG_H4_UNLOCK(sc); 230 231 lwkt_reltoken(&tty_token); 232 return (error); 233 } /* ng_h4_open */ 234 235 /* 236 * Line specific close routine, called from device close routine 237 * and from ttioctl. This causes the node to be destroyed as well. 238 */ 239 240 static int 241 ng_h4_close(struct tty *tp, int flag) 242 { 243 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 244 245 lwkt_gettoken(&tty_token); 246 ttyflush(tp, FREAD | FWRITE); 247 clist_free_cblocks(&tp->t_outq); 248 249 if (sc != NULL) { 250 NG_H4_LOCK(sc); 251 252 if (callout_pending(&sc->timo)) 253 ng_uncallout(&sc->timo, sc->node); 254 255 tp->t_sc = NULL; 256 sc->dying = 1; 257 258 NG_H4_UNLOCK(sc); 259 260 ng_rmnode_self(sc->node); 261 } 262 263 lwkt_reltoken(&tty_token); 264 return (0); 265 } /* ng_h4_close */ 266 267 /* 268 * Once the device has been turned into a node, we don't allow reading. 269 */ 270 271 static int 272 ng_h4_read(struct tty *tp, struct uio *uio, int flag) 273 { 274 return (EIO); 275 } /* ng_h4_read */ 276 277 /* 278 * Once the device has been turned into a node, we don't allow writing. 279 */ 280 281 static int 282 ng_h4_write(struct tty *tp, struct uio *uio, int flag) 283 { 284 return (EIO); 285 } /* ng_h4_write */ 286 287 /* 288 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 289 */ 290 291 static int 292 ng_h4_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, 293 struct ucred *cred) 294 { 295 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 296 int error = 0; 297 298 if (sc == NULL) 299 return (ENXIO); 300 301 lwkt_gettoken(&tty_token); 302 NG_H4_LOCK(sc); 303 304 switch (cmd) { 305 case NGIOCGINFO: 306 #undef NI 307 #define NI(x) ((struct nodeinfo *)(x)) 308 309 bzero(data, sizeof(*NI(data))); 310 311 if (NG_NODE_HAS_NAME(sc->node)) 312 strncpy(NI(data)->name, NG_NODE_NAME(sc->node), 313 sizeof(NI(data)->name) - 1); 314 315 strncpy(NI(data)->type, sc->node->nd_type->name, 316 sizeof(NI(data)->type) - 1); 317 318 NI(data)->id = (u_int32_t) ng_node2ID(sc->node); 319 NI(data)->hooks = NG_NODE_NUMHOOKS(sc->node); 320 break; 321 322 default: 323 error = ENOIOCTL; 324 break; 325 } 326 327 NG_H4_UNLOCK(sc); 328 329 lwkt_reltoken(&tty_token); 330 return (error); 331 } /* ng_h4_ioctl */ 332 333 /* 334 * Receive data coming from the device. We get one character at a time, which 335 * is kindof silly. 336 */ 337 338 static int 339 ng_h4_input(int c, struct tty *tp) 340 { 341 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 342 343 lwkt_gettoken(&tty_token); 344 if (sc == NULL || tp != sc->tp || 345 sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) { 346 lwkt_reltoken(&tty_token); 347 return (0); 348 } 349 350 NG_H4_LOCK(sc); 351 352 /* Check for error conditions */ 353 if ((tp->t_state & TS_CONNECTED) == 0) { 354 NG_H4_INFO("%s: %s - no carrier\n", __func__, 355 NG_NODE_NAME(sc->node)); 356 357 sc->state = NG_H4_W4_PKT_IND; 358 sc->want = 1; 359 sc->got = 0; 360 361 NG_H4_UNLOCK(sc); 362 363 lwkt_reltoken(&tty_token); 364 return (0); /* XXX Loss of synchronization here! */ 365 } 366 367 /* Check for framing error or overrun on this char */ 368 if (c & TTY_ERRORMASK) { 369 NG_H4_ERR("%s: %s - line error %#x, c=%#x\n", __func__, 370 NG_NODE_NAME(sc->node), c & TTY_ERRORMASK, 371 c & TTY_CHARMASK); 372 373 NG_H4_STAT_IERROR(sc->stat); 374 375 sc->state = NG_H4_W4_PKT_IND; 376 sc->want = 1; 377 sc->got = 0; 378 379 NG_H4_UNLOCK(sc); 380 381 lwkt_reltoken(&tty_token); 382 return (0); /* XXX Loss of synchronization here! */ 383 } 384 385 NG_H4_STAT_BYTES_RECV(sc->stat, 1); 386 387 /* Append char to mbuf */ 388 if (sc->got >= sizeof(sc->ibuf)) { 389 NG_H4_ALERT("%s: %s - input buffer overflow, c=%#x, got=%d\n", 390 __func__, NG_NODE_NAME(sc->node), c & TTY_CHARMASK, 391 sc->got); 392 393 NG_H4_STAT_IERROR(sc->stat); 394 395 sc->state = NG_H4_W4_PKT_IND; 396 sc->want = 1; 397 sc->got = 0; 398 399 NG_H4_UNLOCK(sc); 400 401 lwkt_reltoken(&tty_token); 402 return (0); /* XXX Loss of synchronization here! */ 403 } 404 405 sc->ibuf[sc->got ++] = (c & TTY_CHARMASK); 406 407 NG_H4_INFO("%s: %s - got char %#x, want=%d, got=%d\n", __func__, 408 NG_NODE_NAME(sc->node), c, sc->want, sc->got); 409 410 if (sc->got < sc->want) { 411 NG_H4_UNLOCK(sc); 412 413 lwkt_reltoken(&tty_token); 414 return (0); /* Wait for more */ 415 } 416 417 switch (sc->state) { 418 /* Got packet indicator */ 419 case NG_H4_W4_PKT_IND: 420 NG_H4_INFO("%s: %s - got packet indicator %#x\n", __func__, 421 NG_NODE_NAME(sc->node), sc->ibuf[0]); 422 423 sc->state = NG_H4_W4_PKT_HDR; 424 425 /* 426 * Since packet indicator included in the packet header 427 * just set sc->want to sizeof(packet header). 428 */ 429 430 switch (sc->ibuf[0]) { 431 case NG_HCI_ACL_DATA_PKT: 432 sc->want = sizeof(ng_hci_acldata_pkt_t); 433 break; 434 435 case NG_HCI_SCO_DATA_PKT: 436 sc->want = sizeof(ng_hci_scodata_pkt_t); 437 break; 438 439 case NG_HCI_EVENT_PKT: 440 sc->want = sizeof(ng_hci_event_pkt_t); 441 break; 442 443 default: 444 NG_H4_WARN("%s: %s - ignoring unknown packet " \ 445 "type=%#x\n", __func__, NG_NODE_NAME(sc->node), 446 sc->ibuf[0]); 447 448 NG_H4_STAT_IERROR(sc->stat); 449 450 sc->state = NG_H4_W4_PKT_IND; 451 sc->want = 1; 452 sc->got = 0; 453 break; 454 } 455 break; 456 457 /* Got packet header */ 458 case NG_H4_W4_PKT_HDR: 459 sc->state = NG_H4_W4_PKT_DATA; 460 461 switch (sc->ibuf[0]) { 462 case NG_HCI_ACL_DATA_PKT: 463 c = le16toh(((ng_hci_acldata_pkt_t *) 464 (sc->ibuf))->length); 465 break; 466 467 case NG_HCI_SCO_DATA_PKT: 468 c = ((ng_hci_scodata_pkt_t *)(sc->ibuf))->length; 469 break; 470 471 case NG_HCI_EVENT_PKT: 472 c = ((ng_hci_event_pkt_t *)(sc->ibuf))->length; 473 break; 474 475 default: 476 KASSERT((0), ("Invalid packet type=%#x", 477 sc->ibuf[0])); 478 break; 479 } 480 481 NG_H4_INFO("%s: %s - got packet header, packet type=%#x, " \ 482 "packet size=%d, payload size=%d\n", __func__, 483 NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got, c); 484 485 if (c > 0) { 486 sc->want += c; 487 488 /* 489 * Try to prevent possible buffer overrun 490 * 491 * XXX I'm *really* confused here. It turns out 492 * that Xircom card sends us packets with length 493 * greater then 512 bytes! This is greater then 494 * our old receive buffer (ibuf) size. In the same 495 * time the card demands from us *not* to send 496 * packets greater then 192 bytes. Weird! How the 497 * hell i should know how big *receive* buffer 498 * should be? For now increase receiving buffer 499 * size to 1K and add the following check. 500 */ 501 502 if (sc->want >= sizeof(sc->ibuf)) { 503 int b; 504 505 NG_H4_ALERT("%s: %s - packet too big for " \ 506 "buffer, type=%#x, got=%d, want=%d, " \ 507 "length=%d\n", __func__, 508 NG_NODE_NAME(sc->node), sc->ibuf[0], 509 sc->got, sc->want, c); 510 511 NG_H4_ALERT("Packet header:\n"); 512 for (b = 0; b < sc->got; b++) 513 NG_H4_ALERT("%#x ", sc->ibuf[b]); 514 NG_H4_ALERT("\n"); 515 516 /* Reset state */ 517 NG_H4_STAT_IERROR(sc->stat); 518 519 sc->state = NG_H4_W4_PKT_IND; 520 sc->want = 1; 521 sc->got = 0; 522 } 523 524 break; 525 } 526 527 /* else FALLTHROUGH and deliver frame */ 528 /* XXX Is this true? Should we deliver empty frame? */ 529 530 /* Got packet data */ 531 case NG_H4_W4_PKT_DATA: 532 NG_H4_INFO("%s: %s - got full packet, packet type=%#x, " \ 533 "packet size=%d\n", __func__, 534 NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got); 535 536 if (sc->hook != NULL && NG_HOOK_IS_VALID(sc->hook)) { 537 struct mbuf *m = NULL; 538 539 MGETHDR(m, M_NOWAIT, MT_DATA); 540 if (m != NULL) { 541 m->m_pkthdr.len = 0; 542 543 /* XXX m_copyback() is stupid */ 544 m->m_len = min(MHLEN, sc->got); 545 546 m_copyback(m, 0, sc->got, sc->ibuf); 547 NG_SEND_DATA_ONLY(c, sc->hook, m); 548 } else { 549 NG_H4_ERR("%s: %s - could not get mbuf\n", 550 __func__, NG_NODE_NAME(sc->node)); 551 552 NG_H4_STAT_IERROR(sc->stat); 553 } 554 } 555 556 sc->state = NG_H4_W4_PKT_IND; 557 sc->want = 1; 558 sc->got = 0; 559 560 NG_H4_STAT_PCKTS_RECV(sc->stat); 561 break; 562 563 default: 564 KASSERT((0), ("Invalid H4 node state=%d", sc->state)); 565 break; 566 } 567 568 NG_H4_UNLOCK(sc); 569 570 lwkt_reltoken(&tty_token); 571 return (0); 572 } /* ng_h4_input */ 573 574 /* 575 * This is called when the device driver is ready for more output. Called from 576 * tty system. 577 */ 578 579 static int 580 ng_h4_start(struct tty *tp) 581 { 582 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 583 struct mbuf *m = NULL; 584 int size; 585 586 lwkt_gettoken(&tty_token); 587 if (sc == NULL || tp != sc->tp || 588 sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) { 589 lwkt_reltoken(&tty_token); 590 return (0); 591 } 592 593 #if 0 594 while (tp->t_outq.c_cc < NG_H4_HIWATER) { /* XXX 2.2 specific ? */ 595 #else 596 while (1) { 597 #endif 598 /* Remove first mbuf from queue */ 599 IF_DEQUEUE(&sc->outq, m); 600 if (m == NULL) 601 break; 602 603 /* Send as much of it as possible */ 604 while (m != NULL) { 605 size = m->m_len - b_to_q(mtod(m, u_char *), 606 m->m_len, &tp->t_outq); 607 608 NG_H4_LOCK(sc); 609 NG_H4_STAT_BYTES_SENT(sc->stat, size); 610 NG_H4_UNLOCK(sc); 611 612 m->m_data += size; 613 m->m_len -= size; 614 if (m->m_len > 0) 615 break; /* device can't take no more */ 616 617 m = m_free(m); 618 } 619 620 /* Put remainder of mbuf chain (if any) back on queue */ 621 if (m != NULL) { 622 IF_PREPEND(&sc->outq, m); 623 break; 624 } 625 626 /* Full packet has been sent */ 627 NG_H4_LOCK(sc); 628 NG_H4_STAT_PCKTS_SENT(sc->stat); 629 NG_H4_UNLOCK(sc); 630 } 631 632 /* 633 * Call output process whether or not there is any output. We are 634 * being called in lieu of ttstart and must do what it would. 635 */ 636 637 if (tp->t_oproc != NULL) 638 (*tp->t_oproc) (tp); 639 640 /* 641 * This timeout is needed for operation on a pseudo-tty, because the 642 * pty code doesn't call pppstart after it has drained the t_outq. 643 */ 644 645 NG_H4_LOCK(sc); 646 647 if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->timo)) 648 ng_callout(&sc->timo, sc->node, NULL, 1, 649 ng_h4_process_timeout, NULL, 0); 650 651 NG_H4_UNLOCK(sc); 652 653 lwkt_reltoken(&tty_token); 654 return (0); 655 } /* ng_h4_start */ 656 657 /***************************************************************************** 658 ***************************************************************************** 659 ** Netgraph node methods 660 ***************************************************************************** 661 *****************************************************************************/ 662 663 /* 664 * Initialize a new node of this type. We only allow nodes to be created as 665 * a result of setting the line discipline on a tty, so always return an error 666 * if not. 667 */ 668 669 static int 670 ng_h4_constructor(node_p node) 671 { 672 return (EOPNOTSUPP); 673 } /* ng_h4_constructor */ 674 675 /* 676 * Add a new hook. There can only be one. 677 */ 678 679 static int 680 ng_h4_newhook(node_p node, hook_p hook, const char *name) 681 { 682 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 683 684 if (strcmp(name, NG_H4_HOOK) != 0) 685 return (EINVAL); 686 687 NG_H4_LOCK(sc); 688 689 if (sc->hook != NULL) { 690 NG_H4_UNLOCK(sc); 691 return (EISCONN); 692 } 693 sc->hook = hook; 694 695 NG_H4_UNLOCK(sc); 696 697 return (0); 698 } /* ng_h4_newhook */ 699 700 /* 701 * Connect hook. Just say yes. 702 */ 703 704 static int 705 ng_h4_connect(hook_p hook) 706 { 707 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 708 709 if (hook != sc->hook) 710 panic("%s: hook != sc->hook", __func__); 711 712 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 713 NG_HOOK_FORCE_QUEUE(hook); 714 715 return (0); 716 } /* ng_h4_connect */ 717 718 /* 719 * Disconnect the hook 720 */ 721 722 static int 723 ng_h4_disconnect(hook_p hook) 724 { 725 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 726 727 /* 728 * We need to check for sc != NULL because we can be called from 729 * ng_h4_close() via ng_rmnode_self() 730 */ 731 732 if (sc != NULL) { 733 if (hook != sc->hook) 734 panic("%s: hook != sc->hook", __func__); 735 736 NG_H4_LOCK(sc); 737 738 /* XXX do we have to untimeout and drain out queue? */ 739 if (callout_pending(&sc->timo)) 740 ng_uncallout(&sc->timo, sc->node); 741 742 IF_DRAIN(&sc->outq); 743 744 sc->state = NG_H4_W4_PKT_IND; 745 sc->want = 1; 746 sc->got = 0; 747 748 sc->hook = NULL; 749 750 NG_H4_UNLOCK(sc); 751 } 752 753 return (0); 754 } /* ng_h4_disconnect */ 755 756 /* 757 * Remove this node. The does the netgraph portion of the shutdown. 758 * This should only be called indirectly from ng_h4_close(). 759 */ 760 761 static int 762 ng_h4_shutdown(node_p node) 763 { 764 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 765 766 NG_H4_LOCK(sc); 767 768 if (!sc->dying) { 769 NG_H4_UNLOCK(sc); 770 771 NG_NODE_REVIVE(node); /* we will persist */ 772 773 return (EOPNOTSUPP); 774 } 775 776 NG_H4_UNLOCK(sc); 777 778 NG_NODE_SET_PRIVATE(node, NULL); 779 780 IF_DRAIN(&sc->outq); 781 782 NG_NODE_UNREF(node); 783 bzero(sc, sizeof(*sc)); 784 kfree(sc, M_NETGRAPH_H4); 785 786 return (0); 787 } /* ng_h4_shutdown */ 788 789 /* 790 * Receive incoming data from Netgraph system. Put it on our 791 * output queue and start output if necessary. 792 */ 793 794 static int 795 ng_h4_rcvdata(hook_p hook, item_p item) 796 { 797 ng_h4_info_p sc = (ng_h4_info_p)NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 798 struct mbuf *m = NULL; 799 int qlen; 800 801 if (sc == NULL) 802 return (EHOSTDOWN); 803 804 if (hook != sc->hook) 805 panic("%s: hook != sc->hook", __func__); 806 807 NGI_GET_M(item, m); 808 NG_FREE_ITEM(item); 809 810 NG_H4_LOCK(sc); 811 812 if (IF_QFULL(&sc->outq)) { 813 NG_H4_ERR("%s: %s - dropping mbuf, len=%d\n", __func__, 814 NG_NODE_NAME(sc->node), m->m_pkthdr.len); 815 816 NG_H4_STAT_OERROR(sc->stat); 817 IF_DROP(&sc->outq); 818 819 NG_H4_UNLOCK(sc); 820 821 NG_FREE_M(m); 822 823 return (ENOBUFS); 824 } 825 826 NG_H4_INFO("%s: %s - queue mbuf, len=%d\n", __func__, 827 NG_NODE_NAME(sc->node), m->m_pkthdr.len); 828 829 IF_ENQUEUE(&sc->outq, m); 830 qlen = IF_QLEN(&sc->outq); 831 832 NG_H4_UNLOCK(sc); 833 834 /* 835 * If qlen > 1, then we should already have a scheduled callout 836 */ 837 838 if (qlen == 1) 839 ng_h4_start(sc->tp); 840 841 return (0); 842 } /* ng_h4_rcvdata */ 843 844 /* 845 * Receive control message 846 */ 847 848 static int 849 ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook) 850 { 851 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 852 struct ng_mesg *msg = NULL, *resp = NULL; 853 int error = 0; 854 855 if (sc == NULL) 856 return (EHOSTDOWN); 857 858 NGI_GET_MSG(item, msg); 859 NG_H4_LOCK(sc); 860 861 switch (msg->header.typecookie) { 862 case NGM_GENERIC_COOKIE: 863 switch (msg->header.cmd) { 864 case NGM_TEXT_STATUS: 865 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK); 866 if (resp == NULL) 867 error = ENOMEM; 868 else 869 ksnprintf(resp->data, NG_TEXTRESPONSE, 870 "Hook: %s\n" \ 871 "Debug: %d\n" \ 872 "State: %d\n" \ 873 "Queue: [have:%d,max:%d]\n" \ 874 "Input: [got:%d,want:%d]", 875 (sc->hook != NULL)? NG_H4_HOOK : "", 876 sc->debug, 877 sc->state, 878 IF_QLEN(&sc->outq), 879 sc->outq.ifq_maxlen, 880 sc->got, 881 sc->want); 882 break; 883 884 default: 885 error = EINVAL; 886 break; 887 } 888 break; 889 890 case NGM_H4_COOKIE: 891 switch (msg->header.cmd) { 892 case NGM_H4_NODE_RESET: 893 IF_DRAIN(&sc->outq); 894 sc->state = NG_H4_W4_PKT_IND; 895 sc->want = 1; 896 sc->got = 0; 897 break; 898 899 case NGM_H4_NODE_GET_STATE: 900 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_state_ep), 901 M_WAITOK | M_NULLOK); 902 if (resp == NULL) 903 error = ENOMEM; 904 else 905 *((ng_h4_node_state_ep *)(resp->data)) = 906 sc->state; 907 break; 908 909 case NGM_H4_NODE_GET_DEBUG: 910 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_debug_ep), 911 M_WAITOK | M_NULLOK); 912 if (resp == NULL) 913 error = ENOMEM; 914 else 915 *((ng_h4_node_debug_ep *)(resp->data)) = 916 sc->debug; 917 break; 918 919 case NGM_H4_NODE_SET_DEBUG: 920 if (msg->header.arglen != sizeof(ng_h4_node_debug_ep)) 921 error = EMSGSIZE; 922 else 923 sc->debug = 924 *((ng_h4_node_debug_ep *)(msg->data)); 925 break; 926 927 case NGM_H4_NODE_GET_QLEN: 928 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_qlen_ep), 929 M_WAITOK | M_NULLOK); 930 if (resp == NULL) 931 error = ENOMEM; 932 else 933 *((ng_h4_node_qlen_ep *)(resp->data)) = 934 sc->outq.ifq_maxlen; 935 break; 936 937 case NGM_H4_NODE_SET_QLEN: 938 if (msg->header.arglen != sizeof(ng_h4_node_qlen_ep)) 939 error = EMSGSIZE; 940 else if (*((ng_h4_node_qlen_ep *)(msg->data)) <= 0) 941 error = EINVAL; 942 else 943 sc->outq.ifq_maxlen = 944 *((ng_h4_node_qlen_ep *)(msg->data)); 945 break; 946 947 case NGM_H4_NODE_GET_STAT: 948 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_stat_ep), 949 M_WAITOK | M_NULLOK); 950 if (resp == NULL) 951 error = ENOMEM; 952 else 953 bcopy(&sc->stat, resp->data, 954 sizeof(ng_h4_node_stat_ep)); 955 break; 956 957 case NGM_H4_NODE_RESET_STAT: 958 NG_H4_STAT_RESET(sc->stat); 959 break; 960 961 default: 962 error = EINVAL; 963 break; 964 } 965 break; 966 967 default: 968 error = EINVAL; 969 break; 970 } 971 972 NG_H4_UNLOCK(sc); 973 974 NG_RESPOND_MSG(error, node, item, resp); 975 NG_FREE_MSG(msg); 976 977 return (error); 978 } /* ng_h4_rcvmsg */ 979 980 /* 981 * Timeout processing function. 982 * We still have data to output to the device, so try sending more. 983 */ 984 985 static void 986 ng_h4_process_timeout(node_p node, hook_p hook, void *arg1, int arg2) 987 { 988 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 989 990 ng_h4_start(sc->tp); 991 } /* ng_h4_process_timeout */ 992 993 /* 994 * Handle loading and unloading for this node type 995 */ 996 997 static int 998 ng_h4_mod_event(module_t mod, int event, void *data) 999 { 1000 static int ng_h4_ldisc; 1001 int error = 0; 1002 1003 switch (event) { 1004 case MOD_LOAD: 1005 /* Register line discipline */ 1006 crit_enter(); 1007 ng_h4_ldisc = ldisc_register(BTUARTDISC, &ng_h4_disc); 1008 crit_exit(); 1009 1010 if (ng_h4_ldisc < 0) { 1011 kprintf("%s: can't register H4 line discipline\n", 1012 __func__); 1013 error = EIO; 1014 } 1015 break; 1016 1017 case MOD_UNLOAD: 1018 /* Unregister line discipline */ 1019 crit_enter(); 1020 ldisc_deregister(ng_h4_ldisc); 1021 crit_exit(); 1022 break; 1023 1024 default: 1025 error = EOPNOTSUPP; 1026 break; 1027 } 1028 1029 return (error); 1030 } /* ng_h4_mod_event */ 1031 1032