dhcpcd: update README.DRAGONFLY
[dragonfly.git] / sys / netgraph7 / bluetooth / drivers / h4 / ng_h4.c
blobb770f28bd633caf339e48ea2791c8733d2428d59
1 /*-
2 * (MPSAFE)
4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
7 * ng_h4.c
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.
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.
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 $
33 * Based on:
34 * ---------
36 * FreeBSD: src/sys/netgraph/ng_tty.c
37 * Author: Archie Cobbs <archie@freebsd.org>
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>
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.
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 *****************************************************************************/
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 */
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 *);
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 */
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;
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 *);
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
134 NETGRAPH_INIT(h4, &typestruct);
135 MODULE_VERSION(ng_h4, NG_BLUETOOTH_VERSION);
137 static int ng_h4_node = 0;
139 /*****************************************************************************
140 *****************************************************************************
141 ** Line discipline methods
142 *****************************************************************************
143 *****************************************************************************/
146 * Set our line discipline on the tty.
149 static int
150 ng_h4_open(struct cdev *dev, struct tty *tp)
152 char name[NG_NODESIZ];
153 ng_h4_info_p sc = NULL;
154 int error;
156 /* Super-user only */
157 error = caps_priv_check_self(SYSCAP_NONET_NETGRAPH);
158 if (error != 0)
159 return (error);
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);
166 lwkt_gettoken(&tp->t_token);
167 sc->tp = tp;
168 sc->debug = NG_H4_WARN_LEVEL;
170 sc->state = NG_H4_W4_PKT_IND;
171 sc->want = 1;
172 sc->got = 0;
174 sc->outq.ifq_maxlen = NG_H4_DEFAULTQLEN;
175 ng_callout_init(&sc->timo);
177 NG_H4_LOCK(sc);
179 /* Setup netgraph node */
180 error = ng_make_node_common(&typestruct, &sc->node);
181 if (error != 0) {
182 NG_H4_UNLOCK(sc);
184 kprintf("%s: Unable to create new node!\n", __func__);
186 bzero(sc, sizeof(*sc));
187 kfree(sc, M_NETGRAPH_H4);
189 lwkt_reltoken(&tp->t_token);
190 return (error);
193 /* Assign node its name */
194 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ng_h4_node ++);
196 error = ng_name_node(sc->node, name);
197 if (error != 0) {
198 NG_H4_UNLOCK(sc);
200 kprintf("%s: %s - node name exists?\n", __func__, name);
202 NG_NODE_UNREF(sc->node);
203 bzero(sc, sizeof(*sc));
204 kfree(sc, M_NETGRAPH_H4);
206 lwkt_reltoken(&tp->t_token);
207 return (error);
210 /* Set back pointers */
211 NG_NODE_SET_PRIVATE(sc->node, sc);
212 tp->t_sc = (caddr_t) sc;
214 /* The node has to be a WRITER because data can change node status */
215 NG_NODE_FORCE_WRITER(sc->node);
218 * Pre-allocate cblocks to the an appropriate amount.
219 * I'm not sure what is appropriate.
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);
227 NG_H4_UNLOCK(sc);
229 lwkt_reltoken(&tp->t_token);
230 return (error);
231 } /* ng_h4_open */
234 * Line specific close routine, called from device close routine
235 * and from ttioctl. This causes the node to be destroyed as well.
238 static int
239 ng_h4_close(struct tty *tp, int flag)
241 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
243 lwkt_gettoken(&tp->t_token);
244 ttyflush(tp, FREAD | FWRITE);
245 clist_free_cblocks(&tp->t_outq);
247 if (sc != NULL) {
248 NG_H4_LOCK(sc);
250 if (callout_pending(&sc->timo))
251 ng_uncallout(&sc->timo, sc->node);
253 tp->t_sc = NULL;
254 sc->dying = 1;
256 NG_H4_UNLOCK(sc);
258 ng_rmnode_self(sc->node);
261 lwkt_reltoken(&tp->t_token);
262 return (0);
263 } /* ng_h4_close */
266 * Once the device has been turned into a node, we don't allow reading.
269 static int
270 ng_h4_read(struct tty *tp, struct uio *uio, int flag)
272 return (EIO);
273 } /* ng_h4_read */
276 * Once the device has been turned into a node, we don't allow writing.
279 static int
280 ng_h4_write(struct tty *tp, struct uio *uio, int flag)
282 return (EIO);
283 } /* ng_h4_write */
286 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
289 static int
290 ng_h4_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
291 struct ucred *cred)
293 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
294 int error = 0;
296 if (sc == NULL)
297 return (ENXIO);
299 lwkt_gettoken(&tp->t_token);
300 NG_H4_LOCK(sc);
302 switch (cmd) {
303 case NGIOCGINFO:
304 #undef NI
305 #define NI(x) ((struct nodeinfo *)(x))
307 bzero(data, sizeof(*NI(data)));
309 if (NG_NODE_HAS_NAME(sc->node))
310 strncpy(NI(data)->name, NG_NODE_NAME(sc->node),
311 sizeof(NI(data)->name) - 1);
313 strncpy(NI(data)->type, sc->node->nd_type->name,
314 sizeof(NI(data)->type) - 1);
316 NI(data)->id = (u_int32_t) ng_node2ID(sc->node);
317 NI(data)->hooks = NG_NODE_NUMHOOKS(sc->node);
318 break;
320 default:
321 error = ENOIOCTL;
322 break;
325 NG_H4_UNLOCK(sc);
327 lwkt_reltoken(&tp->t_token);
328 return (error);
329 } /* ng_h4_ioctl */
332 * Receive data coming from the device. We get one character at a time, which
333 * is kindof silly.
336 static int
337 ng_h4_input(int c, struct tty *tp)
339 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
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);
348 NG_H4_LOCK(sc);
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));
355 sc->state = NG_H4_W4_PKT_IND;
356 sc->want = 1;
357 sc->got = 0;
359 NG_H4_UNLOCK(sc);
361 lwkt_reltoken(&tp->t_token);
362 return (0); /* XXX Loss of synchronization here! */
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);
371 NG_H4_STAT_IERROR(sc->stat);
373 sc->state = NG_H4_W4_PKT_IND;
374 sc->want = 1;
375 sc->got = 0;
377 NG_H4_UNLOCK(sc);
379 lwkt_reltoken(&tp->t_token);
380 return (0); /* XXX Loss of synchronization here! */
383 NG_H4_STAT_BYTES_RECV(sc->stat, 1);
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);
391 NG_H4_STAT_IERROR(sc->stat);
393 sc->state = NG_H4_W4_PKT_IND;
394 sc->want = 1;
395 sc->got = 0;
397 NG_H4_UNLOCK(sc);
399 lwkt_reltoken(&tp->t_token);
400 return (0); /* XXX Loss of synchronization here! */
403 sc->ibuf[sc->got ++] = (c & TTY_CHARMASK);
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);
408 if (sc->got < sc->want) {
409 NG_H4_UNLOCK(sc);
411 lwkt_reltoken(&tp->t_token);
412 return (0); /* Wait for more */
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]);
421 sc->state = NG_H4_W4_PKT_HDR;
424 * Since packet indicator included in the packet header
425 * just set sc->want to sizeof(packet header).
428 switch (sc->ibuf[0]) {
429 case NG_HCI_ACL_DATA_PKT:
430 sc->want = sizeof(ng_hci_acldata_pkt_t);
431 break;
433 case NG_HCI_SCO_DATA_PKT:
434 sc->want = sizeof(ng_hci_scodata_pkt_t);
435 break;
437 case NG_HCI_EVENT_PKT:
438 sc->want = sizeof(ng_hci_event_pkt_t);
439 break;
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]);
446 NG_H4_STAT_IERROR(sc->stat);
448 sc->state = NG_H4_W4_PKT_IND;
449 sc->want = 1;
450 sc->got = 0;
451 break;
453 break;
455 /* Got packet header */
456 case NG_H4_W4_PKT_HDR:
457 sc->state = NG_H4_W4_PKT_DATA;
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;
465 case NG_HCI_SCO_DATA_PKT:
466 c = ((ng_hci_scodata_pkt_t *)(sc->ibuf))->length;
467 break;
469 case NG_HCI_EVENT_PKT:
470 c = ((ng_hci_event_pkt_t *)(sc->ibuf))->length;
471 break;
473 default:
474 KASSERT((0), ("Invalid packet type=%#x",
475 sc->ibuf[0]));
476 break;
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);
483 if (c > 0) {
484 sc->want += c;
487 * Try to prevent possible buffer overrun
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.
500 if (sc->want >= sizeof(sc->ibuf)) {
501 int b;
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);
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");
514 /* Reset state */
515 NG_H4_STAT_IERROR(sc->stat);
517 sc->state = NG_H4_W4_PKT_IND;
518 sc->want = 1;
519 sc->got = 0;
522 break;
525 /* else FALLTHROUGH and deliver frame */
526 /* XXX Is this true? Should we deliver empty frame? */
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);
534 if (sc->hook != NULL && NG_HOOK_IS_VALID(sc->hook)) {
535 struct mbuf *m = NULL;
537 MGETHDR(m, M_NOWAIT, MT_DATA);
538 if (m != NULL && m_copyback2(m, 0, sc->got, sc->ibuf,
539 M_NOWAIT) == 0) {
540 NG_SEND_DATA_ONLY(c, sc->hook, m);
541 } else {
542 NG_H4_ERR("%s: %s - could not get mbuf\n",
543 __func__, NG_NODE_NAME(sc->node));
544 NG_H4_STAT_IERROR(sc->stat);
545 m_freem(m);
549 sc->state = NG_H4_W4_PKT_IND;
550 sc->want = 1;
551 sc->got = 0;
553 NG_H4_STAT_PCKTS_RECV(sc->stat);
554 break;
556 default:
557 KASSERT((0), ("Invalid H4 node state=%d", sc->state));
558 break;
561 NG_H4_UNLOCK(sc);
563 lwkt_reltoken(&tp->t_token);
564 return (0);
565 } /* ng_h4_input */
568 * This is called when the device driver is ready for more output. Called from
569 * tty system.
572 static int
573 ng_h4_start(struct tty *tp)
575 ng_h4_info_p sc = (ng_h4_info_p) tp->t_sc;
576 struct mbuf *m = NULL;
577 int size;
579 lwkt_gettoken(&tp->t_token);
580 if (sc == NULL || tp != sc->tp ||
581 sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) {
582 lwkt_reltoken(&tp->t_token);
583 return (0);
586 #if 0
587 while (tp->t_outq.c_cc < NG_H4_HIWATER) { /* XXX 2.2 specific ? */
588 #else
589 while (1) {
590 #endif
591 /* Remove first mbuf from queue */
592 IF_DEQUEUE(&sc->outq, m);
593 if (m == NULL)
594 break;
596 /* Send as much of it as possible */
597 while (m != NULL) {
598 size = m->m_len - clist_btoq(mtod(m, u_char *),
599 m->m_len, &tp->t_outq);
601 NG_H4_LOCK(sc);
602 NG_H4_STAT_BYTES_SENT(sc->stat, size);
603 NG_H4_UNLOCK(sc);
605 m->m_data += size;
606 m->m_len -= size;
607 if (m->m_len > 0)
608 break; /* device can't take no more */
610 m = m_free(m);
613 /* Put remainder of mbuf chain (if any) back on queue */
614 if (m != NULL) {
615 IF_PREPEND(&sc->outq, m);
616 break;
619 /* Full packet has been sent */
620 NG_H4_LOCK(sc);
621 NG_H4_STAT_PCKTS_SENT(sc->stat);
622 NG_H4_UNLOCK(sc);
626 * Call output process whether or not there is any output. We are
627 * being called in lieu of ttstart and must do what it would.
630 if (tp->t_oproc != NULL)
631 (*tp->t_oproc) (tp);
634 * This timeout is needed for operation on a pseudo-tty, because the
635 * pty code doesn't call pppstart after it has drained the t_outq.
638 NG_H4_LOCK(sc);
640 if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->timo))
641 ng_callout(&sc->timo, sc->node, NULL, 1,
642 ng_h4_process_timeout, NULL, 0);
644 NG_H4_UNLOCK(sc);
646 lwkt_reltoken(&tp->t_token);
647 return (0);
648 } /* ng_h4_start */
650 /*****************************************************************************
651 *****************************************************************************
652 ** Netgraph node methods
653 *****************************************************************************
654 *****************************************************************************/
657 * Initialize a new node of this type. We only allow nodes to be created as
658 * a result of setting the line discipline on a tty, so always return an error
659 * if not.
662 static int
663 ng_h4_constructor(node_p node)
665 return (EOPNOTSUPP);
666 } /* ng_h4_constructor */
669 * Add a new hook. There can only be one.
672 static int
673 ng_h4_newhook(node_p node, hook_p hook, const char *name)
675 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
677 if (strcmp(name, NG_H4_HOOK) != 0)
678 return (EINVAL);
680 NG_H4_LOCK(sc);
682 if (sc->hook != NULL) {
683 NG_H4_UNLOCK(sc);
684 return (EISCONN);
686 sc->hook = hook;
688 NG_H4_UNLOCK(sc);
690 return (0);
691 } /* ng_h4_newhook */
694 * Connect hook. Just say yes.
697 static int
698 ng_h4_connect(hook_p hook)
700 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
702 if (hook != sc->hook)
703 panic("%s: hook != sc->hook", __func__);
705 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
706 NG_HOOK_FORCE_QUEUE(hook);
708 return (0);
709 } /* ng_h4_connect */
712 * Disconnect the hook
715 static int
716 ng_h4_disconnect(hook_p hook)
718 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
721 * We need to check for sc != NULL because we can be called from
722 * ng_h4_close() via ng_rmnode_self()
725 if (sc != NULL) {
726 if (hook != sc->hook)
727 panic("%s: hook != sc->hook", __func__);
729 NG_H4_LOCK(sc);
731 /* XXX do we have to untimeout and drain out queue? */
732 if (callout_pending(&sc->timo))
733 ng_uncallout(&sc->timo, sc->node);
735 IF_DRAIN(&sc->outq);
737 sc->state = NG_H4_W4_PKT_IND;
738 sc->want = 1;
739 sc->got = 0;
741 sc->hook = NULL;
743 NG_H4_UNLOCK(sc);
746 return (0);
747 } /* ng_h4_disconnect */
750 * Remove this node. The does the netgraph portion of the shutdown.
751 * This should only be called indirectly from ng_h4_close().
754 static int
755 ng_h4_shutdown(node_p node)
757 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
759 NG_H4_LOCK(sc);
761 if (!sc->dying) {
762 NG_H4_UNLOCK(sc);
764 NG_NODE_REVIVE(node); /* we will persist */
766 return (EOPNOTSUPP);
769 NG_H4_UNLOCK(sc);
771 NG_NODE_SET_PRIVATE(node, NULL);
773 IF_DRAIN(&sc->outq);
775 NG_NODE_UNREF(node);
776 bzero(sc, sizeof(*sc));
777 kfree(sc, M_NETGRAPH_H4);
779 return (0);
780 } /* ng_h4_shutdown */
783 * Receive incoming data from Netgraph system. Put it on our
784 * output queue and start output if necessary.
787 static int
788 ng_h4_rcvdata(hook_p hook, item_p item)
790 ng_h4_info_p sc = (ng_h4_info_p)NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
791 struct mbuf *m = NULL;
792 int qlen;
794 if (sc == NULL)
795 return (EHOSTDOWN);
797 if (hook != sc->hook)
798 panic("%s: hook != sc->hook", __func__);
800 NGI_GET_M(item, m);
801 NG_FREE_ITEM(item);
803 NG_H4_LOCK(sc);
805 if (IF_QFULL(&sc->outq)) {
806 NG_H4_ERR("%s: %s - dropping mbuf, len=%d\n", __func__,
807 NG_NODE_NAME(sc->node), m->m_pkthdr.len);
809 NG_H4_STAT_OERROR(sc->stat);
810 IF_DROP(&sc->outq);
812 NG_H4_UNLOCK(sc);
814 NG_FREE_M(m);
816 return (ENOBUFS);
819 NG_H4_INFO("%s: %s - queue mbuf, len=%d\n", __func__,
820 NG_NODE_NAME(sc->node), m->m_pkthdr.len);
822 IF_ENQUEUE(&sc->outq, m);
823 qlen = IF_QLEN(&sc->outq);
825 NG_H4_UNLOCK(sc);
828 * If qlen > 1, then we should already have a scheduled callout
831 if (qlen == 1)
832 ng_h4_start(sc->tp);
834 return (0);
835 } /* ng_h4_rcvdata */
838 * Receive control message
841 static int
842 ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook)
844 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
845 struct ng_mesg *msg = NULL, *resp = NULL;
846 int error = 0;
848 if (sc == NULL)
849 return (EHOSTDOWN);
851 NGI_GET_MSG(item, msg);
852 NG_H4_LOCK(sc);
854 switch (msg->header.typecookie) {
855 case NGM_GENERIC_COOKIE:
856 switch (msg->header.cmd) {
857 case NGM_TEXT_STATUS:
858 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK);
859 if (resp == NULL)
860 error = ENOMEM;
861 else
862 ksnprintf(resp->data, NG_TEXTRESPONSE,
863 "Hook: %s\n" \
864 "Debug: %d\n" \
865 "State: %d\n" \
866 "Queue: [have:%d,max:%d]\n" \
867 "Input: [got:%d,want:%d]",
868 (sc->hook != NULL)? NG_H4_HOOK : "",
869 sc->debug,
870 sc->state,
871 IF_QLEN(&sc->outq),
872 sc->outq.ifq_maxlen,
873 sc->got,
874 sc->want);
875 break;
877 default:
878 error = EINVAL;
879 break;
881 break;
883 case NGM_H4_COOKIE:
884 switch (msg->header.cmd) {
885 case NGM_H4_NODE_RESET:
886 IF_DRAIN(&sc->outq);
887 sc->state = NG_H4_W4_PKT_IND;
888 sc->want = 1;
889 sc->got = 0;
890 break;
892 case NGM_H4_NODE_GET_STATE:
893 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_state_ep),
894 M_WAITOK | M_NULLOK);
895 if (resp == NULL)
896 error = ENOMEM;
897 else
898 *((ng_h4_node_state_ep *)(resp->data)) =
899 sc->state;
900 break;
902 case NGM_H4_NODE_GET_DEBUG:
903 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_debug_ep),
904 M_WAITOK | M_NULLOK);
905 if (resp == NULL)
906 error = ENOMEM;
907 else
908 *((ng_h4_node_debug_ep *)(resp->data)) =
909 sc->debug;
910 break;
912 case NGM_H4_NODE_SET_DEBUG:
913 if (msg->header.arglen != sizeof(ng_h4_node_debug_ep))
914 error = EMSGSIZE;
915 else
916 sc->debug =
917 *((ng_h4_node_debug_ep *)(msg->data));
918 break;
920 case NGM_H4_NODE_GET_QLEN:
921 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_qlen_ep),
922 M_WAITOK | M_NULLOK);
923 if (resp == NULL)
924 error = ENOMEM;
925 else
926 *((ng_h4_node_qlen_ep *)(resp->data)) =
927 sc->outq.ifq_maxlen;
928 break;
930 case NGM_H4_NODE_SET_QLEN:
931 if (msg->header.arglen != sizeof(ng_h4_node_qlen_ep))
932 error = EMSGSIZE;
933 else if (*((ng_h4_node_qlen_ep *)(msg->data)) <= 0)
934 error = EINVAL;
935 else
936 sc->outq.ifq_maxlen =
937 *((ng_h4_node_qlen_ep *)(msg->data));
938 break;
940 case NGM_H4_NODE_GET_STAT:
941 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_stat_ep),
942 M_WAITOK | M_NULLOK);
943 if (resp == NULL)
944 error = ENOMEM;
945 else
946 bcopy(&sc->stat, resp->data,
947 sizeof(ng_h4_node_stat_ep));
948 break;
950 case NGM_H4_NODE_RESET_STAT:
951 NG_H4_STAT_RESET(sc->stat);
952 break;
954 default:
955 error = EINVAL;
956 break;
958 break;
960 default:
961 error = EINVAL;
962 break;
965 NG_H4_UNLOCK(sc);
967 NG_RESPOND_MSG(error, node, item, resp);
968 NG_FREE_MSG(msg);
970 return (error);
971 } /* ng_h4_rcvmsg */
974 * Timeout processing function.
975 * We still have data to output to the device, so try sending more.
978 static void
979 ng_h4_process_timeout(node_p node, hook_p hook, void *arg1, int arg2)
981 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
983 ng_h4_start(sc->tp);
984 } /* ng_h4_process_timeout */
987 * Handle loading and unloading for this node type
990 static int
991 ng_h4_mod_event(module_t mod, int event, void *data)
993 static int ng_h4_ldisc;
994 int error = 0;
996 switch (event) {
997 case MOD_LOAD:
998 /* Register line discipline */
999 crit_enter();
1000 ng_h4_ldisc = ldisc_register(BTUARTDISC, &ng_h4_disc);
1001 crit_exit();
1003 if (ng_h4_ldisc < 0) {
1004 kprintf("%s: can't register H4 line discipline\n",
1005 __func__);
1006 error = EIO;
1008 break;
1010 case MOD_UNLOAD:
1011 /* Unregister line discipline */
1012 crit_enter();
1013 ldisc_deregister(ng_h4_ldisc);
1014 crit_exit();
1015 break;
1017 default:
1018 error = EOPNOTSUPP;
1019 break;
1022 return (error);
1023 } /* ng_h4_mod_event */