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/caps.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 char name[NG_NODESIZ]; 153 ng_h4_info_p sc = NULL; 154 int error; 155 156 /* Super-user only */ 157 error = caps_priv_check_self(SYSCAP_NONET_NETGRAPH); 158 if (error != 0) 159 return (error); 160 161 /* Initialize private struct */ 162 sc = kmalloc(sizeof(*sc), M_NETGRAPH_H4, M_WAITOK | M_NULLOK | M_ZERO); 163 if (sc == NULL) 164 return (ENOMEM); 165 166 lwkt_gettoken(&tp->t_token); 167 sc->tp = tp; 168 sc->debug = NG_H4_WARN_LEVEL; 169 170 sc->state = NG_H4_W4_PKT_IND; 171 sc->want = 1; 172 sc->got = 0; 173 174 sc->outq.ifq_maxlen = NG_H4_DEFAULTQLEN; 175 ng_callout_init(&sc->timo); 176 177 NG_H4_LOCK(sc); 178 179 /* Setup netgraph node */ 180 error = ng_make_node_common(&typestruct, &sc->node); 181 if (error != 0) { 182 NG_H4_UNLOCK(sc); 183 184 kprintf("%s: Unable to create new node!\n", __func__); 185 186 bzero(sc, sizeof(*sc)); 187 kfree(sc, M_NETGRAPH_H4); 188 189 lwkt_reltoken(&tp->t_token); 190 return (error); 191 } 192 193 /* Assign node its name */ 194 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ng_h4_node ++); 195 196 error = ng_name_node(sc->node, name); 197 if (error != 0) { 198 NG_H4_UNLOCK(sc); 199 200 kprintf("%s: %s - node name exists?\n", __func__, name); 201 202 NG_NODE_UNREF(sc->node); 203 bzero(sc, sizeof(*sc)); 204 kfree(sc, M_NETGRAPH_H4); 205 206 lwkt_reltoken(&tp->t_token); 207 return (error); 208 } 209 210 /* Set back pointers */ 211 NG_NODE_SET_PRIVATE(sc->node, sc); 212 tp->t_sc = (caddr_t) sc; 213 214 /* The node has to be a WRITER because data can change node status */ 215 NG_NODE_FORCE_WRITER(sc->node); 216 217 /* 218 * Pre-allocate cblocks to the an appropriate amount. 219 * I'm not sure what is appropriate. 220 */ 221 222 ttyflush(tp, FREAD | FWRITE); 223 clist_alloc_cblocks(&tp->t_canq, 0); 224 clist_alloc_cblocks(&tp->t_rawq, 0); 225 clist_alloc_cblocks(&tp->t_outq, MLEN + NG_H4_HIWATER); 226 227 NG_H4_UNLOCK(sc); 228 229 lwkt_reltoken(&tp->t_token); 230 return (error); 231 } /* ng_h4_open */ 232 233 /* 234 * Line specific close routine, called from device close routine 235 * and from ttioctl. This causes the node to be destroyed as well. 236 */ 237 238 static int 239 ng_h4_close(struct tty *tp, int flag) 240 { 241 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 242 243 lwkt_gettoken(&tp->t_token); 244 ttyflush(tp, FREAD | FWRITE); 245 clist_free_cblocks(&tp->t_outq); 246 247 if (sc != NULL) { 248 NG_H4_LOCK(sc); 249 250 if (callout_pending(&sc->timo)) 251 ng_uncallout(&sc->timo, sc->node); 252 253 tp->t_sc = NULL; 254 sc->dying = 1; 255 256 NG_H4_UNLOCK(sc); 257 258 ng_rmnode_self(sc->node); 259 } 260 261 lwkt_reltoken(&tp->t_token); 262 return (0); 263 } /* ng_h4_close */ 264 265 /* 266 * Once the device has been turned into a node, we don't allow reading. 267 */ 268 269 static int 270 ng_h4_read(struct tty *tp, struct uio *uio, int flag) 271 { 272 return (EIO); 273 } /* ng_h4_read */ 274 275 /* 276 * Once the device has been turned into a node, we don't allow writing. 277 */ 278 279 static int 280 ng_h4_write(struct tty *tp, struct uio *uio, int flag) 281 { 282 return (EIO); 283 } /* ng_h4_write */ 284 285 /* 286 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 287 */ 288 289 static int 290 ng_h4_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, 291 struct ucred *cred) 292 { 293 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 294 int error = 0; 295 296 if (sc == NULL) 297 return (ENXIO); 298 299 lwkt_gettoken(&tp->t_token); 300 NG_H4_LOCK(sc); 301 302 switch (cmd) { 303 case NGIOCGINFO: 304 #undef NI 305 #define NI(x) ((struct nodeinfo *)(x)) 306 307 bzero(data, sizeof(*NI(data))); 308 309 if (NG_NODE_HAS_NAME(sc->node)) 310 strncpy(NI(data)->name, NG_NODE_NAME(sc->node), 311 sizeof(NI(data)->name) - 1); 312 313 strncpy(NI(data)->type, sc->node->nd_type->name, 314 sizeof(NI(data)->type) - 1); 315 316 NI(data)->id = (u_int32_t) ng_node2ID(sc->node); 317 NI(data)->hooks = NG_NODE_NUMHOOKS(sc->node); 318 break; 319 320 default: 321 error = ENOIOCTL; 322 break; 323 } 324 325 NG_H4_UNLOCK(sc); 326 327 lwkt_reltoken(&tp->t_token); 328 return (error); 329 } /* ng_h4_ioctl */ 330 331 /* 332 * Receive data coming from the device. We get one character at a time, which 333 * is kindof silly. 334 */ 335 336 static int 337 ng_h4_input(int c, struct tty *tp) 338 { 339 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 340 341 lwkt_gettoken(&tp->t_token); 342 if (sc == NULL || tp != sc->tp || 343 sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) { 344 lwkt_reltoken(&tp->t_token); 345 return (0); 346 } 347 348 NG_H4_LOCK(sc); 349 350 /* Check for error conditions */ 351 if ((tp->t_state & TS_CONNECTED) == 0) { 352 NG_H4_INFO("%s: %s - no carrier\n", __func__, 353 NG_NODE_NAME(sc->node)); 354 355 sc->state = NG_H4_W4_PKT_IND; 356 sc->want = 1; 357 sc->got = 0; 358 359 NG_H4_UNLOCK(sc); 360 361 lwkt_reltoken(&tp->t_token); 362 return (0); /* XXX Loss of synchronization here! */ 363 } 364 365 /* Check for framing error or overrun on this char */ 366 if (c & TTY_ERRORMASK) { 367 NG_H4_ERR("%s: %s - line error %#x, c=%#x\n", __func__, 368 NG_NODE_NAME(sc->node), c & TTY_ERRORMASK, 369 c & TTY_CHARMASK); 370 371 NG_H4_STAT_IERROR(sc->stat); 372 373 sc->state = NG_H4_W4_PKT_IND; 374 sc->want = 1; 375 sc->got = 0; 376 377 NG_H4_UNLOCK(sc); 378 379 lwkt_reltoken(&tp->t_token); 380 return (0); /* XXX Loss of synchronization here! */ 381 } 382 383 NG_H4_STAT_BYTES_RECV(sc->stat, 1); 384 385 /* Append char to mbuf */ 386 if (sc->got >= sizeof(sc->ibuf)) { 387 NG_H4_ALERT("%s: %s - input buffer overflow, c=%#x, got=%d\n", 388 __func__, NG_NODE_NAME(sc->node), c & TTY_CHARMASK, 389 sc->got); 390 391 NG_H4_STAT_IERROR(sc->stat); 392 393 sc->state = NG_H4_W4_PKT_IND; 394 sc->want = 1; 395 sc->got = 0; 396 397 NG_H4_UNLOCK(sc); 398 399 lwkt_reltoken(&tp->t_token); 400 return (0); /* XXX Loss of synchronization here! */ 401 } 402 403 sc->ibuf[sc->got ++] = (c & TTY_CHARMASK); 404 405 NG_H4_INFO("%s: %s - got char %#x, want=%d, got=%d\n", __func__, 406 NG_NODE_NAME(sc->node), c, sc->want, sc->got); 407 408 if (sc->got < sc->want) { 409 NG_H4_UNLOCK(sc); 410 411 lwkt_reltoken(&tp->t_token); 412 return (0); /* Wait for more */ 413 } 414 415 switch (sc->state) { 416 /* Got packet indicator */ 417 case NG_H4_W4_PKT_IND: 418 NG_H4_INFO("%s: %s - got packet indicator %#x\n", __func__, 419 NG_NODE_NAME(sc->node), sc->ibuf[0]); 420 421 sc->state = NG_H4_W4_PKT_HDR; 422 423 /* 424 * Since packet indicator included in the packet header 425 * just set sc->want to sizeof(packet header). 426 */ 427 428 switch (sc->ibuf[0]) { 429 case NG_HCI_ACL_DATA_PKT: 430 sc->want = sizeof(ng_hci_acldata_pkt_t); 431 break; 432 433 case NG_HCI_SCO_DATA_PKT: 434 sc->want = sizeof(ng_hci_scodata_pkt_t); 435 break; 436 437 case NG_HCI_EVENT_PKT: 438 sc->want = sizeof(ng_hci_event_pkt_t); 439 break; 440 441 default: 442 NG_H4_WARN("%s: %s - ignoring unknown packet " \ 443 "type=%#x\n", __func__, NG_NODE_NAME(sc->node), 444 sc->ibuf[0]); 445 446 NG_H4_STAT_IERROR(sc->stat); 447 448 sc->state = NG_H4_W4_PKT_IND; 449 sc->want = 1; 450 sc->got = 0; 451 break; 452 } 453 break; 454 455 /* Got packet header */ 456 case NG_H4_W4_PKT_HDR: 457 sc->state = NG_H4_W4_PKT_DATA; 458 459 switch (sc->ibuf[0]) { 460 case NG_HCI_ACL_DATA_PKT: 461 c = le16toh(((ng_hci_acldata_pkt_t *) 462 (sc->ibuf))->length); 463 break; 464 465 case NG_HCI_SCO_DATA_PKT: 466 c = ((ng_hci_scodata_pkt_t *)(sc->ibuf))->length; 467 break; 468 469 case NG_HCI_EVENT_PKT: 470 c = ((ng_hci_event_pkt_t *)(sc->ibuf))->length; 471 break; 472 473 default: 474 KASSERT((0), ("Invalid packet type=%#x", 475 sc->ibuf[0])); 476 break; 477 } 478 479 NG_H4_INFO("%s: %s - got packet header, packet type=%#x, " \ 480 "packet size=%d, payload size=%d\n", __func__, 481 NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got, c); 482 483 if (c > 0) { 484 sc->want += c; 485 486 /* 487 * Try to prevent possible buffer overrun 488 * 489 * XXX I'm *really* confused here. It turns out 490 * that Xircom card sends us packets with length 491 * greater then 512 bytes! This is greater then 492 * our old receive buffer (ibuf) size. In the same 493 * time the card demands from us *not* to send 494 * packets greater then 192 bytes. Weird! How the 495 * hell i should know how big *receive* buffer 496 * should be? For now increase receiving buffer 497 * size to 1K and add the following check. 498 */ 499 500 if (sc->want >= sizeof(sc->ibuf)) { 501 int b; 502 503 NG_H4_ALERT("%s: %s - packet too big for " \ 504 "buffer, type=%#x, got=%d, want=%d, " \ 505 "length=%d\n", __func__, 506 NG_NODE_NAME(sc->node), sc->ibuf[0], 507 sc->got, sc->want, c); 508 509 NG_H4_ALERT("Packet header:\n"); 510 for (b = 0; b < sc->got; b++) 511 NG_H4_ALERT("%#x ", sc->ibuf[b]); 512 NG_H4_ALERT("\n"); 513 514 /* Reset state */ 515 NG_H4_STAT_IERROR(sc->stat); 516 517 sc->state = NG_H4_W4_PKT_IND; 518 sc->want = 1; 519 sc->got = 0; 520 } 521 522 break; 523 } 524 525 /* else FALLTHROUGH and deliver frame */ 526 /* XXX Is this true? Should we deliver empty frame? */ 527 528 /* Got packet data */ 529 case NG_H4_W4_PKT_DATA: 530 NG_H4_INFO("%s: %s - got full packet, packet type=%#x, " \ 531 "packet size=%d\n", __func__, 532 NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got); 533 534 if (sc->hook != NULL && NG_HOOK_IS_VALID(sc->hook)) { 535 struct mbuf *m = NULL; 536 537 MGETHDR(m, M_NOWAIT, MT_DATA); 538 if (m != NULL) { 539 m->m_pkthdr.len = 0; 540 541 /* XXX m_copyback() is stupid */ 542 m->m_len = min(MHLEN, sc->got); 543 544 m_copyback(m, 0, sc->got, sc->ibuf); 545 NG_SEND_DATA_ONLY(c, sc->hook, m); 546 } else { 547 NG_H4_ERR("%s: %s - could not get mbuf\n", 548 __func__, NG_NODE_NAME(sc->node)); 549 550 NG_H4_STAT_IERROR(sc->stat); 551 } 552 } 553 554 sc->state = NG_H4_W4_PKT_IND; 555 sc->want = 1; 556 sc->got = 0; 557 558 NG_H4_STAT_PCKTS_RECV(sc->stat); 559 break; 560 561 default: 562 KASSERT((0), ("Invalid H4 node state=%d", sc->state)); 563 break; 564 } 565 566 NG_H4_UNLOCK(sc); 567 568 lwkt_reltoken(&tp->t_token); 569 return (0); 570 } /* ng_h4_input */ 571 572 /* 573 * This is called when the device driver is ready for more output. Called from 574 * tty system. 575 */ 576 577 static int 578 ng_h4_start(struct tty *tp) 579 { 580 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc; 581 struct mbuf *m = NULL; 582 int size; 583 584 lwkt_gettoken(&tp->t_token); 585 if (sc == NULL || tp != sc->tp || 586 sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) { 587 lwkt_reltoken(&tp->t_token); 588 return (0); 589 } 590 591 #if 0 592 while (tp->t_outq.c_cc < NG_H4_HIWATER) { /* XXX 2.2 specific ? */ 593 #else 594 while (1) { 595 #endif 596 /* Remove first mbuf from queue */ 597 IF_DEQUEUE(&sc->outq, m); 598 if (m == NULL) 599 break; 600 601 /* Send as much of it as possible */ 602 while (m != NULL) { 603 size = m->m_len - clist_btoq(mtod(m, u_char *), 604 m->m_len, &tp->t_outq); 605 606 NG_H4_LOCK(sc); 607 NG_H4_STAT_BYTES_SENT(sc->stat, size); 608 NG_H4_UNLOCK(sc); 609 610 m->m_data += size; 611 m->m_len -= size; 612 if (m->m_len > 0) 613 break; /* device can't take no more */ 614 615 m = m_free(m); 616 } 617 618 /* Put remainder of mbuf chain (if any) back on queue */ 619 if (m != NULL) { 620 IF_PREPEND(&sc->outq, m); 621 break; 622 } 623 624 /* Full packet has been sent */ 625 NG_H4_LOCK(sc); 626 NG_H4_STAT_PCKTS_SENT(sc->stat); 627 NG_H4_UNLOCK(sc); 628 } 629 630 /* 631 * Call output process whether or not there is any output. We are 632 * being called in lieu of ttstart and must do what it would. 633 */ 634 635 if (tp->t_oproc != NULL) 636 (*tp->t_oproc) (tp); 637 638 /* 639 * This timeout is needed for operation on a pseudo-tty, because the 640 * pty code doesn't call pppstart after it has drained the t_outq. 641 */ 642 643 NG_H4_LOCK(sc); 644 645 if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->timo)) 646 ng_callout(&sc->timo, sc->node, NULL, 1, 647 ng_h4_process_timeout, NULL, 0); 648 649 NG_H4_UNLOCK(sc); 650 651 lwkt_reltoken(&tp->t_token); 652 return (0); 653 } /* ng_h4_start */ 654 655 /***************************************************************************** 656 ***************************************************************************** 657 ** Netgraph node methods 658 ***************************************************************************** 659 *****************************************************************************/ 660 661 /* 662 * Initialize a new node of this type. We only allow nodes to be created as 663 * a result of setting the line discipline on a tty, so always return an error 664 * if not. 665 */ 666 667 static int 668 ng_h4_constructor(node_p node) 669 { 670 return (EOPNOTSUPP); 671 } /* ng_h4_constructor */ 672 673 /* 674 * Add a new hook. There can only be one. 675 */ 676 677 static int 678 ng_h4_newhook(node_p node, hook_p hook, const char *name) 679 { 680 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 681 682 if (strcmp(name, NG_H4_HOOK) != 0) 683 return (EINVAL); 684 685 NG_H4_LOCK(sc); 686 687 if (sc->hook != NULL) { 688 NG_H4_UNLOCK(sc); 689 return (EISCONN); 690 } 691 sc->hook = hook; 692 693 NG_H4_UNLOCK(sc); 694 695 return (0); 696 } /* ng_h4_newhook */ 697 698 /* 699 * Connect hook. Just say yes. 700 */ 701 702 static int 703 ng_h4_connect(hook_p hook) 704 { 705 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 706 707 if (hook != sc->hook) 708 panic("%s: hook != sc->hook", __func__); 709 710 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 711 NG_HOOK_FORCE_QUEUE(hook); 712 713 return (0); 714 } /* ng_h4_connect */ 715 716 /* 717 * Disconnect the hook 718 */ 719 720 static int 721 ng_h4_disconnect(hook_p hook) 722 { 723 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 724 725 /* 726 * We need to check for sc != NULL because we can be called from 727 * ng_h4_close() via ng_rmnode_self() 728 */ 729 730 if (sc != NULL) { 731 if (hook != sc->hook) 732 panic("%s: hook != sc->hook", __func__); 733 734 NG_H4_LOCK(sc); 735 736 /* XXX do we have to untimeout and drain out queue? */ 737 if (callout_pending(&sc->timo)) 738 ng_uncallout(&sc->timo, sc->node); 739 740 IF_DRAIN(&sc->outq); 741 742 sc->state = NG_H4_W4_PKT_IND; 743 sc->want = 1; 744 sc->got = 0; 745 746 sc->hook = NULL; 747 748 NG_H4_UNLOCK(sc); 749 } 750 751 return (0); 752 } /* ng_h4_disconnect */ 753 754 /* 755 * Remove this node. The does the netgraph portion of the shutdown. 756 * This should only be called indirectly from ng_h4_close(). 757 */ 758 759 static int 760 ng_h4_shutdown(node_p node) 761 { 762 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 763 764 NG_H4_LOCK(sc); 765 766 if (!sc->dying) { 767 NG_H4_UNLOCK(sc); 768 769 NG_NODE_REVIVE(node); /* we will persist */ 770 771 return (EOPNOTSUPP); 772 } 773 774 NG_H4_UNLOCK(sc); 775 776 NG_NODE_SET_PRIVATE(node, NULL); 777 778 IF_DRAIN(&sc->outq); 779 780 NG_NODE_UNREF(node); 781 bzero(sc, sizeof(*sc)); 782 kfree(sc, M_NETGRAPH_H4); 783 784 return (0); 785 } /* ng_h4_shutdown */ 786 787 /* 788 * Receive incoming data from Netgraph system. Put it on our 789 * output queue and start output if necessary. 790 */ 791 792 static int 793 ng_h4_rcvdata(hook_p hook, item_p item) 794 { 795 ng_h4_info_p sc = (ng_h4_info_p)NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 796 struct mbuf *m = NULL; 797 int qlen; 798 799 if (sc == NULL) 800 return (EHOSTDOWN); 801 802 if (hook != sc->hook) 803 panic("%s: hook != sc->hook", __func__); 804 805 NGI_GET_M(item, m); 806 NG_FREE_ITEM(item); 807 808 NG_H4_LOCK(sc); 809 810 if (IF_QFULL(&sc->outq)) { 811 NG_H4_ERR("%s: %s - dropping mbuf, len=%d\n", __func__, 812 NG_NODE_NAME(sc->node), m->m_pkthdr.len); 813 814 NG_H4_STAT_OERROR(sc->stat); 815 IF_DROP(&sc->outq); 816 817 NG_H4_UNLOCK(sc); 818 819 NG_FREE_M(m); 820 821 return (ENOBUFS); 822 } 823 824 NG_H4_INFO("%s: %s - queue mbuf, len=%d\n", __func__, 825 NG_NODE_NAME(sc->node), m->m_pkthdr.len); 826 827 IF_ENQUEUE(&sc->outq, m); 828 qlen = IF_QLEN(&sc->outq); 829 830 NG_H4_UNLOCK(sc); 831 832 /* 833 * If qlen > 1, then we should already have a scheduled callout 834 */ 835 836 if (qlen == 1) 837 ng_h4_start(sc->tp); 838 839 return (0); 840 } /* ng_h4_rcvdata */ 841 842 /* 843 * Receive control message 844 */ 845 846 static int 847 ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook) 848 { 849 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 850 struct ng_mesg *msg = NULL, *resp = NULL; 851 int error = 0; 852 853 if (sc == NULL) 854 return (EHOSTDOWN); 855 856 NGI_GET_MSG(item, msg); 857 NG_H4_LOCK(sc); 858 859 switch (msg->header.typecookie) { 860 case NGM_GENERIC_COOKIE: 861 switch (msg->header.cmd) { 862 case NGM_TEXT_STATUS: 863 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK); 864 if (resp == NULL) 865 error = ENOMEM; 866 else 867 ksnprintf(resp->data, NG_TEXTRESPONSE, 868 "Hook: %s\n" \ 869 "Debug: %d\n" \ 870 "State: %d\n" \ 871 "Queue: [have:%d,max:%d]\n" \ 872 "Input: [got:%d,want:%d]", 873 (sc->hook != NULL)? NG_H4_HOOK : "", 874 sc->debug, 875 sc->state, 876 IF_QLEN(&sc->outq), 877 sc->outq.ifq_maxlen, 878 sc->got, 879 sc->want); 880 break; 881 882 default: 883 error = EINVAL; 884 break; 885 } 886 break; 887 888 case NGM_H4_COOKIE: 889 switch (msg->header.cmd) { 890 case NGM_H4_NODE_RESET: 891 IF_DRAIN(&sc->outq); 892 sc->state = NG_H4_W4_PKT_IND; 893 sc->want = 1; 894 sc->got = 0; 895 break; 896 897 case NGM_H4_NODE_GET_STATE: 898 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_state_ep), 899 M_WAITOK | M_NULLOK); 900 if (resp == NULL) 901 error = ENOMEM; 902 else 903 *((ng_h4_node_state_ep *)(resp->data)) = 904 sc->state; 905 break; 906 907 case NGM_H4_NODE_GET_DEBUG: 908 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_debug_ep), 909 M_WAITOK | M_NULLOK); 910 if (resp == NULL) 911 error = ENOMEM; 912 else 913 *((ng_h4_node_debug_ep *)(resp->data)) = 914 sc->debug; 915 break; 916 917 case NGM_H4_NODE_SET_DEBUG: 918 if (msg->header.arglen != sizeof(ng_h4_node_debug_ep)) 919 error = EMSGSIZE; 920 else 921 sc->debug = 922 *((ng_h4_node_debug_ep *)(msg->data)); 923 break; 924 925 case NGM_H4_NODE_GET_QLEN: 926 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_qlen_ep), 927 M_WAITOK | M_NULLOK); 928 if (resp == NULL) 929 error = ENOMEM; 930 else 931 *((ng_h4_node_qlen_ep *)(resp->data)) = 932 sc->outq.ifq_maxlen; 933 break; 934 935 case NGM_H4_NODE_SET_QLEN: 936 if (msg->header.arglen != sizeof(ng_h4_node_qlen_ep)) 937 error = EMSGSIZE; 938 else if (*((ng_h4_node_qlen_ep *)(msg->data)) <= 0) 939 error = EINVAL; 940 else 941 sc->outq.ifq_maxlen = 942 *((ng_h4_node_qlen_ep *)(msg->data)); 943 break; 944 945 case NGM_H4_NODE_GET_STAT: 946 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_stat_ep), 947 M_WAITOK | M_NULLOK); 948 if (resp == NULL) 949 error = ENOMEM; 950 else 951 bcopy(&sc->stat, resp->data, 952 sizeof(ng_h4_node_stat_ep)); 953 break; 954 955 case NGM_H4_NODE_RESET_STAT: 956 NG_H4_STAT_RESET(sc->stat); 957 break; 958 959 default: 960 error = EINVAL; 961 break; 962 } 963 break; 964 965 default: 966 error = EINVAL; 967 break; 968 } 969 970 NG_H4_UNLOCK(sc); 971 972 NG_RESPOND_MSG(error, node, item, resp); 973 NG_FREE_MSG(msg); 974 975 return (error); 976 } /* ng_h4_rcvmsg */ 977 978 /* 979 * Timeout processing function. 980 * We still have data to output to the device, so try sending more. 981 */ 982 983 static void 984 ng_h4_process_timeout(node_p node, hook_p hook, void *arg1, int arg2) 985 { 986 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 987 988 ng_h4_start(sc->tp); 989 } /* ng_h4_process_timeout */ 990 991 /* 992 * Handle loading and unloading for this node type 993 */ 994 995 static int 996 ng_h4_mod_event(module_t mod, int event, void *data) 997 { 998 static int ng_h4_ldisc; 999 int error = 0; 1000 1001 switch (event) { 1002 case MOD_LOAD: 1003 /* Register line discipline */ 1004 crit_enter(); 1005 ng_h4_ldisc = ldisc_register(BTUARTDISC, &ng_h4_disc); 1006 crit_exit(); 1007 1008 if (ng_h4_ldisc < 0) { 1009 kprintf("%s: can't register H4 line discipline\n", 1010 __func__); 1011 error = EIO; 1012 } 1013 break; 1014 1015 case MOD_UNLOAD: 1016 /* Unregister line discipline */ 1017 crit_enter(); 1018 ldisc_deregister(ng_h4_ldisc); 1019 crit_exit(); 1020 break; 1021 1022 default: 1023 error = EOPNOTSUPP; 1024 break; 1025 } 1026 1027 return (error); 1028 } /* ng_h4_mod_event */ 1029 1030