kernel/netgraph7: Port the kernel part of the netgraph7 bluetooth stack.
authorSascha Wildner <saw@online.de>
Sat, 15 Nov 2014 22:31:48 +0000 (23:31 +0100)
committerSascha Wildner <saw@online.de>
Sat, 15 Nov 2014 22:31:48 +0000 (23:31 +0100)
It still needs more testing/debugging, along with the userland part.

In-discussion-with: nant

49 files changed:
sys/netgraph7/Makefile
sys/netgraph7/bluetooth/Makefile [new file with mode: 0644]
sys/netgraph7/bluetooth/bluetooth/Makefile [new file with mode: 0644]
sys/netgraph7/bluetooth/common/ng_bluetooth.c
sys/netgraph7/bluetooth/drivers/Makefile [new file with mode: 0644]
sys/netgraph7/bluetooth/drivers/h4/Makefile [new file with mode: 0644]
sys/netgraph7/bluetooth/drivers/h4/ng_h4.c
sys/netgraph7/bluetooth/drivers/h4/ng_h4_prse.h
sys/netgraph7/bluetooth/drivers/h4/ng_h4_var.h
sys/netgraph7/bluetooth/hci/Makefile [new file with mode: 0644]
sys/netgraph7/bluetooth/hci/ng_hci_cmds.c
sys/netgraph7/bluetooth/hci/ng_hci_cmds.h
sys/netgraph7/bluetooth/hci/ng_hci_evnt.c
sys/netgraph7/bluetooth/hci/ng_hci_evnt.h
sys/netgraph7/bluetooth/hci/ng_hci_main.c
sys/netgraph7/bluetooth/hci/ng_hci_misc.c
sys/netgraph7/bluetooth/hci/ng_hci_misc.h
sys/netgraph7/bluetooth/hci/ng_hci_prse.h
sys/netgraph7/bluetooth/hci/ng_hci_ulpi.c
sys/netgraph7/bluetooth/hci/ng_hci_ulpi.h
sys/netgraph7/bluetooth/include/ng_bluetooth.h
sys/netgraph7/bluetooth/include/ng_btsocket.h
sys/netgraph7/bluetooth/include/ng_btsocket_hci_raw.h
sys/netgraph7/bluetooth/include/ng_btsocket_l2cap.h
sys/netgraph7/bluetooth/include/ng_btsocket_rfcomm.h
sys/netgraph7/bluetooth/include/ng_h4.h
sys/netgraph7/bluetooth/include/ng_hci.h
sys/netgraph7/bluetooth/include/ng_l2cap.h
sys/netgraph7/bluetooth/include/ng_ubt.h
sys/netgraph7/bluetooth/l2cap/Makefile [new file with mode: 0644]
sys/netgraph7/bluetooth/l2cap/ng_l2cap_cmds.c
sys/netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h
sys/netgraph7/bluetooth/l2cap/ng_l2cap_evnt.c
sys/netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h
sys/netgraph7/bluetooth/l2cap/ng_l2cap_llpi.c
sys/netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h
sys/netgraph7/bluetooth/l2cap/ng_l2cap_main.c
sys/netgraph7/bluetooth/l2cap/ng_l2cap_misc.c
sys/netgraph7/bluetooth/l2cap/ng_l2cap_misc.h
sys/netgraph7/bluetooth/l2cap/ng_l2cap_prse.h
sys/netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.c
sys/netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h
sys/netgraph7/bluetooth/socket/Makefile [new file with mode: 0644]
sys/netgraph7/bluetooth/socket/ng_btsocket.c
sys/netgraph7/bluetooth/socket/ng_btsocket_hci_raw.c
sys/netgraph7/bluetooth/socket/ng_btsocket_l2cap.c
sys/netgraph7/bluetooth/socket/ng_btsocket_l2cap_raw.c
sys/netgraph7/bluetooth/socket/ng_btsocket_rfcomm.c
sys/netgraph7/netgraph.h

index ae26303..dcadecc 100644 (file)
@@ -4,6 +4,7 @@
 SUBDIR=        UI \
        async \
        bpf \
+       bluetooth \
        bridge \
        cisco \
        deflate \
diff --git a/sys/netgraph7/bluetooth/Makefile b/sys/netgraph7/bluetooth/Makefile
new file mode 100644 (file)
index 0000000..b348a81
--- /dev/null
@@ -0,0 +1,11 @@
+# $Id: Makefile,v 1.2 2003/04/27 00:22:11 max Exp $
+# $FreeBSD: head/sys/modules/netgraph/bluetooth/Makefile 181233 2008-08-03 10:32:17Z ed $
+
+SUBDIR=        \
+       bluetooth \
+       drivers \
+       hci     \
+       l2cap   \
+       socket
+
+.include <bsd.subdir.mk>
diff --git a/sys/netgraph7/bluetooth/bluetooth/Makefile b/sys/netgraph7/bluetooth/bluetooth/Makefile
new file mode 100644 (file)
index 0000000..bb1a382
--- /dev/null
@@ -0,0 +1,9 @@
+# $Id: Makefile,v 1.1 2002/11/24 20:39:57 max Exp $
+# $FreeBSD: head/sys/modules/netgraph/bluetooth/bluetooth/Makefile 155824 2006-02-18 23:49:28Z yar $
+
+.PATH: ${.CURDIR}/../common
+
+KMOD=          ng_bluetooth
+SRCS=          ng_bluetooth.c
+
+.include <bsd.kmod.mk>
index 309c2b8..1e10a8d 100644 (file)
@@ -29,8 +29,6 @@
  *
  * $Id: ng_bluetooth.c,v 1.3 2003/04/26 22:37:31 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/common/ng_bluetooth.c,v 1.7 2007/06/04 18:25:07 dwmalone Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/common/ng_bluetooth.c,v 1.2 2008/06/26 23:05:39 dillon Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/common/ng_bluetooth.c,v 1.2 2008/06/26 23:05:39 dillon Exp $
  */
 
 #include <sys/param.h>
@@ -40,7 +38,7 @@
 #include <sys/module.h>
 #include <sys/sysctl.h>
 
-#include "bluetooth/include/ng_bluetooth.h"
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
 
 /*
  * Bluetooth stack sysctl globals
diff --git a/sys/netgraph7/bluetooth/drivers/Makefile b/sys/netgraph7/bluetooth/drivers/Makefile
new file mode 100644 (file)
index 0000000..8fd40f3
--- /dev/null
@@ -0,0 +1,7 @@
+SUBDIR=        bt3c h4
+
+.if !defined(WANT_OLDUSB)
+SUBDIR+=ubt ubtbcmfw
+.endif
+
+.include <bsd.subdir.mk>
diff --git a/sys/netgraph7/bluetooth/drivers/h4/Makefile b/sys/netgraph7/bluetooth/drivers/h4/Makefile
new file mode 100644 (file)
index 0000000..68580e2
--- /dev/null
@@ -0,0 +1,7 @@
+# $Id: Makefile,v 1.1 2002/11/24 20:40:04 max Exp $
+# $FreeBSD: head/sys/modules/netgraph/bluetooth/h4/Makefile 155824 2006-02-18 23:49:28Z yar $
+
+KMOD=          ng_h4
+SRCS=          ng_h4.c
+
+.include <bsd.kmod.mk>
index 395fb87..c47dc0f 100644 (file)
@@ -28,7 +28,7 @@
  * SUCH DAMAGE.
  *
  * $Id: ng_h4.c,v 1.10 2005/10/31 17:57:43 max Exp $
- * $FreeBSD: src/sys/netgraph/bluetooth/drivers/h4/ng_h4.c,v 1.17 2007/08/13 17:19:28 emax Exp $
+ * $FreeBSD: head/sys/netgraph/bluetooth/drivers/h4/ng_h4.c 243882 2012-12-05 08:04:20Z glebius $
  * 
  * Based on:
  * ---------
 #include <sys/ttycom.h>
 #include <net/if.h>
 #include <net/if_var.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "ng_parse.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_h4.h"
-#include "bluetooth/drivers/h4/ng_h4_var.h"
-#include "bluetooth/drivers/h4/ng_h4_prse.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/ng_parse.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_h4.h>
+#include <netgraph7/bluetooth/drivers/h4/ng_h4_var.h>
+#include <netgraph7/bluetooth/drivers/h4/ng_h4_prse.h>
 
 /*****************************************************************************
  *****************************************************************************
@@ -90,7 +90,7 @@ static int    ng_h4_write     (struct tty *, struct uio *, int);
 static int     ng_h4_input     (int, struct tty *);
 static int     ng_h4_start     (struct tty *);
 static int     ng_h4_ioctl     (struct tty *, u_long, caddr_t, 
-                                       int, struct thread *);
+                                       int, struct ucred *);
 
 /* Line discipline descriptor */
 static struct linesw           ng_h4_disc = {
@@ -172,8 +172,7 @@ ng_h4_open(struct cdev *dev, struct tty *tp)
        sc->want = 1;
        sc->got = 0;
 
-       mtx_init(&sc->outq.ifq_mtx, "ng_h4 node+queue", NULL, MTX_DEF);
-       IFQ_SET_MAXLEN(&sc->outq, NG_H4_DEFAULTQLEN);
+       sc->outq.ifq_maxlen = NG_H4_DEFAULTQLEN;
        ng_callout_init(&sc->timo);
 
        NG_H4_LOCK(sc);
@@ -185,7 +184,6 @@ ng_h4_open(struct cdev *dev, struct tty *tp)
 
                kprintf("%s: Unable to create new node!\n", __func__);
 
-               mtx_destroy(&sc->outq.ifq_mtx);
                bzero(sc, sizeof(*sc));
                kfree(sc, M_NETGRAPH_H4);
 
@@ -203,7 +201,6 @@ ng_h4_open(struct cdev *dev, struct tty *tp)
                kprintf("%s: %s - node name exists?\n", __func__, name);
 
                NG_NODE_UNREF(sc->node);
-               mtx_destroy(&sc->outq.ifq_mtx);
                bzero(sc, sizeof(*sc));
                kfree(sc, M_NETGRAPH_H4);
 
@@ -213,7 +210,7 @@ ng_h4_open(struct cdev *dev, struct tty *tp)
 
        /* Set back pointers */
        NG_NODE_SET_PRIVATE(sc->node, sc);
-       tp->t_lsc = (caddr_t) sc;
+       tp->t_sc = (caddr_t) sc;
 
        /* The node has to be a WRITER because data can change node status */
        NG_NODE_FORCE_WRITER(sc->node);
@@ -243,7 +240,7 @@ ng_h4_open(struct cdev *dev, struct tty *tp)
 static int
 ng_h4_close(struct tty *tp, int flag)
 {
-       ng_h4_info_p    sc = (ng_h4_info_p) tp->t_lsc;
+       ng_h4_info_p    sc = (ng_h4_info_p) tp->t_sc;
 
        lwkt_gettoken(&tty_token);
        ttyflush(tp, FREAD | FWRITE);
@@ -255,7 +252,7 @@ ng_h4_close(struct tty *tp, int flag)
                if (callout_pending(&sc->timo))
                        ng_uncallout(&sc->timo, sc->node);
 
-               tp->t_lsc = NULL;
+               tp->t_sc = NULL;
                sc->dying = 1;
 
                NG_H4_UNLOCK(sc);
@@ -293,9 +290,9 @@ ng_h4_write(struct tty *tp, struct uio *uio, int flag)
 
 static int
 ng_h4_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
-               struct thread *td)
+    struct ucred *cred)
 {
-       ng_h4_info_p    sc = (ng_h4_info_p) tp->t_lsc;
+       ng_h4_info_p    sc = (ng_h4_info_p) tp->t_sc;
        int             error = 0;
 
        if (sc == NULL)
@@ -341,7 +338,7 @@ ng_h4_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
 static int
 ng_h4_input(int c, struct tty *tp)
 {
-       ng_h4_info_p    sc = (ng_h4_info_p) tp->t_lsc;
+       ng_h4_info_p    sc = (ng_h4_info_p) tp->t_sc;
 
        lwkt_gettoken(&tty_token);
        if (sc == NULL || tp != sc->tp ||
@@ -582,7 +579,7 @@ ng_h4_input(int c, struct tty *tp)
 static int
 ng_h4_start(struct tty *tp)
 {
-       ng_h4_info_p     sc = (ng_h4_info_p) tp->t_lsc;
+       ng_h4_info_p     sc = (ng_h4_info_p) tp->t_sc;
        struct mbuf     *m = NULL;
        int              size;
 
@@ -637,7 +634,8 @@ ng_h4_start(struct tty *tp)
         * being called in lieu of ttstart and must do what it would.
         */
 
-       tt_oproc(sc->tp);
+       if (tp->t_oproc != NULL)
+               (*tp->t_oproc) (tp);
 
        /*
         * This timeout is needed for operation on a pseudo-tty, because the
@@ -646,7 +644,7 @@ ng_h4_start(struct tty *tp)
 
        NG_H4_LOCK(sc);
 
-       if (!IFQ_IS_EMPTY(&sc->outq) && !callout_pending(&sc->timo))
+       if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->timo))
                ng_callout(&sc->timo, sc->node, NULL, 1,
                        ng_h4_process_timeout, NULL, 0);
 
@@ -741,7 +739,7 @@ ng_h4_disconnect(hook_p hook)
                if (callout_pending(&sc->timo))
                        ng_uncallout(&sc->timo, sc->node);
 
-               _IF_DRAIN(&sc->outq); 
+               IF_DRAIN(&sc->outq); 
 
                sc->state = NG_H4_W4_PKT_IND;
                sc->want = 1;
@@ -779,10 +777,9 @@ ng_h4_shutdown(node_p node)
 
        NG_NODE_SET_PRIVATE(node, NULL);
 
-       _IF_DRAIN(&sc->outq);
+       IF_DRAIN(&sc->outq);
 
        NG_NODE_UNREF(node);
-       mtx_destroy(&sc->outq.ifq_mtx);
        bzero(sc, sizeof(*sc));
        kfree(sc, M_NETGRAPH_H4);
 
@@ -812,12 +809,12 @@ ng_h4_rcvdata(hook_p hook, item_p item)
 
        NG_H4_LOCK(sc);
 
-       if (_IF_QFULL(&sc->outq)) {
+       if (IF_QFULL(&sc->outq)) {
                NG_H4_ERR("%s: %s - dropping mbuf, len=%d\n", __func__,
                        NG_NODE_NAME(sc->node), m->m_pkthdr.len);
 
                NG_H4_STAT_OERROR(sc->stat);
-               _IF_DROP(&sc->outq);
+               IF_DROP(&sc->outq);
 
                NG_H4_UNLOCK(sc);
 
@@ -829,8 +826,8 @@ ng_h4_rcvdata(hook_p hook, item_p item)
        NG_H4_INFO("%s: %s - queue mbuf, len=%d\n", __func__,
                NG_NODE_NAME(sc->node), m->m_pkthdr.len);
 
-       _IF_ENQUEUE(&sc->outq, m);
-       qlen = _IF_QLEN(&sc->outq);
+       IF_ENQUEUE(&sc->outq, m);
+       qlen = IF_QLEN(&sc->outq);
 
        NG_H4_UNLOCK(sc);
 
@@ -838,11 +835,8 @@ ng_h4_rcvdata(hook_p hook, item_p item)
         * If qlen > 1, then we should already have a scheduled callout
         */
 
-       if (qlen == 1) {
-               mtx_lock(&Giant);
+       if (qlen == 1)
                ng_h4_start(sc->tp);
-               mtx_unlock(&Giant);
-       }
 
        return (0);
 } /* ng_h4_rcvdata */
@@ -881,7 +875,7 @@ ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook)
                                        (sc->hook != NULL)? NG_H4_HOOK : "",
                                        sc->debug,
                                        sc->state,
-                                       _IF_QLEN(&sc->outq),
+                                       IF_QLEN(&sc->outq),
                                        sc->outq.ifq_maxlen,
                                        sc->got,
                                        sc->want);
@@ -896,7 +890,7 @@ ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook)
        case NGM_H4_COOKIE:
                switch (msg->header.cmd) {
                case NGM_H4_NODE_RESET:
-                       _IF_DRAIN(&sc->outq); 
+                       IF_DRAIN(&sc->outq); 
                        sc->state = NG_H4_W4_PKT_IND;
                        sc->want = 1;
                        sc->got = 0;
@@ -946,8 +940,8 @@ ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook)
                        else if (*((ng_h4_node_qlen_ep *)(msg->data)) <= 0)
                                error = EINVAL;
                        else
-                               IFQ_SET_MAXLEN(&sc->outq,
-                                       *((ng_h4_node_qlen_ep *)(msg->data)));
+                               sc->outq.ifq_maxlen =
+                                       *((ng_h4_node_qlen_ep *)(msg->data));
                        break;
 
                case NGM_H4_NODE_GET_STAT:
@@ -993,9 +987,7 @@ ng_h4_process_timeout(node_p node, hook_p hook, void *arg1, int arg2)
 {
        ng_h4_info_p    sc = (ng_h4_info_p) NG_NODE_PRIVATE(node);
 
-       mtx_lock(&Giant);
        ng_h4_start(sc->tp);
-       mtx_unlock(&Giant);
 } /* ng_h4_process_timeout */
 
 /*
@@ -1011,9 +1003,9 @@ ng_h4_mod_event(module_t mod, int event, void *data)
        switch (event) {
        case MOD_LOAD:
                /* Register line discipline */
-               mtx_lock(&Giant);
-               ng_h4_ldisc = ldisc_register(H4DISC, &ng_h4_disc);
-               mtx_unlock(&Giant);
+               crit_enter();
+               ng_h4_ldisc = ldisc_register(BTUARTDISC, &ng_h4_disc);
+               crit_exit();
 
                if (ng_h4_ldisc < 0) {
                        kprintf("%s: can't register H4 line discipline\n",
@@ -1024,9 +1016,9 @@ ng_h4_mod_event(module_t mod, int event, void *data)
 
        case MOD_UNLOAD:
                /* Unregister line discipline */
-               mtx_lock(&Giant);
+               crit_enter();
                ldisc_deregister(ng_h4_ldisc);
-               mtx_unlock(&Giant);
+               crit_exit();
                break;
 
        default:
index cc19414..4492e8c 100644 (file)
@@ -28,8 +28,7 @@
  * SUCH DAMAGE.
  *
  * $Id: ng_h4_prse.h,v 1.4 2005/10/31 17:57:43 max Exp $
- * $FreeBSD: src/sys/netgraph/bluetooth/drivers/h4/ng_h4_prse.h,v 1.5 2007/08/13 17:19:28 emax Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/drivers/h4/ng_h4_prse.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
+ * $FreeBSD: head/sys/netgraph/bluetooth/drivers/h4/ng_h4_prse.h 171818 2007-08-13 17:19:28Z emax $
  */
 
 /***************************************************************************
index 36d2556..fe85037 100644 (file)
@@ -28,7 +28,7 @@
  * SUCH DAMAGE.
  *
  * $Id: ng_h4_var.h,v 1.5 2005/10/31 17:57:43 max Exp $
- * $FreeBSD: src/sys/netgraph/bluetooth/drivers/h4/ng_h4_var.h,v 1.6 2007/08/13 17:19:28 emax Exp $
+ * $FreeBSD: head/sys/netgraph/bluetooth/drivers/h4/ng_h4_var.h 171818 2007-08-13 17:19:28Z emax $
  * 
  * Based on:
  * ---------
@@ -83,8 +83,8 @@ typedef struct ng_h4_info {
 
        struct ifqueue          outq;   /* Queue of outgoing mbuf's */
 #define NG_H4_DEFAULTQLEN       12     /* XXX max number of mbuf's in outq */
-#define        NG_H4_LOCK(sc)          IF_LOCK(&sc->outq)
-#define        NG_H4_UNLOCK(sc)        IF_UNLOCK(&sc->outq)
+#define        NG_H4_LOCK(sc)          crit_enter();
+#define        NG_H4_UNLOCK(sc)        crit_exit();
 
 #define NG_H4_IBUF_SIZE                1024    /* XXX must be big enough to hold full 
                                           frame */
diff --git a/sys/netgraph7/bluetooth/hci/Makefile b/sys/netgraph7/bluetooth/hci/Makefile
new file mode 100644 (file)
index 0000000..507cdde
--- /dev/null
@@ -0,0 +1,8 @@
+# $Id: Makefile,v 1.1 2002/11/24 20:40:05 max Exp $
+# $FreeBSD: head/sys/modules/netgraph/bluetooth/hci/Makefile 124472 2004-01-13 11:28:51Z ru $
+
+KMOD=          ng_hci
+SRCS=          ng_hci_main.c ng_hci_cmds.c ng_hci_evnt.c  \
+               ng_hci_ulpi.c ng_hci_misc.c
+
+.include <bsd.kmod.mk>
index c611886..bd9f7e5 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/hci/ng_hci_var.h"
-#include "bluetooth/hci/ng_hci_cmds.h"
-#include "bluetooth/hci/ng_hci_evnt.h"
-#include "bluetooth/hci/ng_hci_ulpi.h"
-#include "bluetooth/hci/ng_hci_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/hci/ng_hci_var.h>
+#include <netgraph7/bluetooth/hci/ng_hci_cmds.h>
+#include <netgraph7/bluetooth/hci/ng_hci_evnt.h>
+#include <netgraph7/bluetooth/hci/ng_hci_ulpi.h>
+#include <netgraph7/bluetooth/hci/ng_hci_misc.h>
 
 /******************************************************************************
  ******************************************************************************
index f77cde2..f802aac 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_hci_cmds.h,v 1.1 2002/11/24 19:46:58 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_cmds.h,v 1.4 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/hci/ng_hci_cmds.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_HCI_CMDS_H_
index 8642863..5f51fad 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_hci_evnt.c,v 1.6 2003/09/08 18:57:51 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_evnt.c,v 1.8 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/hci/ng_hci_evnt.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #include <sys/param.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/hci/ng_hci_var.h"
-#include "bluetooth/hci/ng_hci_cmds.h"
-#include "bluetooth/hci/ng_hci_evnt.h"
-#include "bluetooth/hci/ng_hci_ulpi.h"
-#include "bluetooth/hci/ng_hci_misc.h"
+#include <sys/refcount.h>
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/netgraph2.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/hci/ng_hci_var.h>
+#include <netgraph7/bluetooth/hci/ng_hci_cmds.h>
+#include <netgraph7/bluetooth/hci/ng_hci_evnt.h>
+#include <netgraph7/bluetooth/hci/ng_hci_ulpi.h>
+#include <netgraph7/bluetooth/hci/ng_hci_misc.h>
 
 /******************************************************************************
  ******************************************************************************
@@ -248,7 +249,7 @@ static int
 send_data_packets(ng_hci_unit_p unit, int link_type, int limit)
 {
        ng_hci_unit_con_p       con = NULL, winner = NULL;
-       item_p                  item = NULL;
+       item_p                  item = NULL, item2;
        int                     min_pending, total_sent, sent, error, v;
 
        for (total_sent = 0; limit > 0; ) {
@@ -285,6 +286,7 @@ send_data_packets(ng_hci_unit_p unit, int link_type, int limit)
                        NG_BT_ITEMQ_DEQUEUE(&winner->conq, item);
                        if (item == NULL)
                                break;
+                       item2 = item;
                
                        NG_HCI_INFO(
 "%s: %s - sending data packet, handle=%d, len=%d\n",
@@ -312,6 +314,7 @@ send_data_packets(ng_hci_unit_p unit, int link_type, int limit)
                                /* ... and forward item to the driver */
                                NG_FWD_ITEM_HOOK(error, item, unit->drv);
                        }
+                       ng_unref_item(item2, error);
 
                        if (error != 0) {
                                NG_HCI_ERR(
index 5854421..e6949f6 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_hci_evnt.h,v 1.1 2002/11/24 19:46:58 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_evnt.h,v 1.4 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/hci/ng_hci_evnt.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_HCI_EVNT_H_
index eb96a3e..3141177 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "ng_parse.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/hci/ng_hci_var.h"
-#include "bluetooth/hci/ng_hci_prse.h"
-#include "bluetooth/hci/ng_hci_cmds.h"
-#include "bluetooth/hci/ng_hci_evnt.h"
-#include "bluetooth/hci/ng_hci_ulpi.h"
-#include "bluetooth/hci/ng_hci_misc.h"
+#include <sys/refcount.h>
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/netgraph2.h>
+#include <netgraph7/ng_parse.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/hci/ng_hci_var.h>
+#include <netgraph7/bluetooth/hci/ng_hci_prse.h>
+#include <netgraph7/bluetooth/hci/ng_hci_cmds.h>
+#include <netgraph7/bluetooth/hci/ng_hci_evnt.h>
+#include <netgraph7/bluetooth/hci/ng_hci_ulpi.h>
+#include <netgraph7/bluetooth/hci/ng_hci_misc.h>
 
 /******************************************************************************
  ******************************************************************************
@@ -868,6 +870,7 @@ ng_hci_acl_rcvdata(hook_p hook, item_p item)
 
        /* Queue item and schedule data transfer */
        NGI_M(item) = m;
+       ng_ref_item(item);
        NG_BT_ITEMQ_ENQUEUE(&con->conq, item);
        item = NULL;
        m = NULL;
@@ -987,6 +990,7 @@ ng_hci_sco_rcvdata(hook_p hook, item_p item)
 
        /* Queue item and schedule data transfer */
        NGI_M(item) = m;
+       ng_ref_item(item);
        NG_BT_ITEMQ_ENQUEUE(&con->conq, item);
        item = NULL;
        m = NULL;
index b78b5c9..b5c792b 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/hci/ng_hci_var.h"
-#include "bluetooth/hci/ng_hci_cmds.h"
-#include "bluetooth/hci/ng_hci_evnt.h"
-#include "bluetooth/hci/ng_hci_ulpi.h"
-#include "bluetooth/hci/ng_hci_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/hci/ng_hci_var.h>
+#include <netgraph7/bluetooth/hci/ng_hci_cmds.h>
+#include <netgraph7/bluetooth/hci/ng_hci_evnt.h>
+#include <netgraph7/bluetooth/hci/ng_hci_ulpi.h>
+#include <netgraph7/bluetooth/hci/ng_hci_misc.h>
 
 /******************************************************************************
  ******************************************************************************
index 074fa3b..7861b91 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_hci_misc.h,v 1.3 2003/09/08 18:57:51 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_misc.h,v 1.5 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/hci/ng_hci_misc.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_HCI_MISC_H_
index 10fa348..5856f17 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_hci_prse.h,v 1.2 2003/03/18 00:09:36 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_prse.h,v 1.4 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/hci/ng_hci_prse.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 /***************************************************************************
index 07c0cd3..4601251 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/hci/ng_hci_var.h"
-#include "bluetooth/hci/ng_hci_cmds.h"
-#include "bluetooth/hci/ng_hci_evnt.h"
-#include "bluetooth/hci/ng_hci_ulpi.h"
-#include "bluetooth/hci/ng_hci_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/hci/ng_hci_var.h>
+#include <netgraph7/bluetooth/hci/ng_hci_cmds.h>
+#include <netgraph7/bluetooth/hci/ng_hci_evnt.h>
+#include <netgraph7/bluetooth/hci/ng_hci_ulpi.h>
+#include <netgraph7/bluetooth/hci/ng_hci_misc.h>
 
 /******************************************************************************
  ******************************************************************************
index 33be008..e3bfb44 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_hci_ulpi.h,v 1.2 2003/04/26 22:35:21 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_ulpi.h,v 1.3 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/hci/ng_hci_ulpi.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_HCI_ULPI_H_
index d7ffb61..c59812b 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_bluetooth.h,v 1.4 2003/04/26 22:32:34 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_bluetooth.h,v 1.5 2008/04/15 21:15:32 mav Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_bluetooth.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_BLUETOOTH_H_
index a14afe8..387968c 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_btsocket.h,v 1.8 2003/04/26 22:32:10 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_btsocket.h,v 1.8 2006/05/17 00:13:06 emax Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_btsocket.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_BTSOCKET_H_
index 2830772..dc9e358 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_btsocket_hci_raw.h,v 1.3 2003/03/25 23:53:32 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_btsocket_hci_raw.h,v 1.7 2006/07/21 17:11:13 rwatson Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_btsocket_hci_raw.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_BTSOCKET_HCI_RAW_H_
@@ -52,7 +51,7 @@ struct ng_btsocket_hci_raw_pcb {
        u_int32_t                                token;  /* message token */
        struct ng_mesg                          *msg;    /* message */
        LIST_ENTRY(ng_btsocket_hci_raw_pcb)      next;   /* link to next */
-       struct mtx                               pcb_mtx; /* pcb mutex */
+       struct lock                              pcb_lock; /* pcb lock */
 };
 typedef struct ng_btsocket_hci_raw_pcb         ng_btsocket_hci_raw_pcb_t;
 typedef struct ng_btsocket_hci_raw_pcb *       ng_btsocket_hci_raw_pcb_p;
@@ -67,23 +66,20 @@ typedef struct ng_btsocket_hci_raw_pcb *    ng_btsocket_hci_raw_pcb_p;
 #ifdef _KERNEL
 
 void ng_btsocket_hci_raw_init       (void);
-void ng_btsocket_hci_raw_abort      (struct socket *);
+void ng_btsocket_hci_raw_abort      (netmsg_t msg);
+#if 0 /* XXX */
 void ng_btsocket_hci_raw_close      (struct socket *);
-int  ng_btsocket_hci_raw_attach     (struct socket *, int, struct thread *);
-int  ng_btsocket_hci_raw_bind       (struct socket *, struct sockaddr *, 
-                                     struct thread *);
-int  ng_btsocket_hci_raw_connect    (struct socket *, struct sockaddr *, 
-                                     struct thread *);
-int  ng_btsocket_hci_raw_control    (struct socket *, u_long, caddr_t,
-                                     struct ifnet *, struct thread *);
-int  ng_btsocket_hci_raw_ctloutput  (struct socket *, struct sockopt *);
-void ng_btsocket_hci_raw_detach     (struct socket *);
-int  ng_btsocket_hci_raw_disconnect (struct socket *);
-int  ng_btsocket_hci_raw_peeraddr   (struct socket *, struct sockaddr **);
-int  ng_btsocket_hci_raw_send       (struct socket *, int, struct mbuf *,
-                                     struct sockaddr *, struct mbuf *,
-                                     struct thread *);
-int  ng_btsocket_hci_raw_sockaddr   (struct socket *, struct sockaddr **);
+#endif
+void ng_btsocket_hci_raw_attach     (netmsg_t msg);
+void ng_btsocket_hci_raw_bind       (netmsg_t msg);
+void ng_btsocket_hci_raw_connect    (netmsg_t msg);
+void ng_btsocket_hci_raw_control    (netmsg_t msg);
+void ng_btsocket_hci_raw_ctloutput  (netmsg_t msg);
+void ng_btsocket_hci_raw_detach     (netmsg_t msg);
+void ng_btsocket_hci_raw_disconnect (netmsg_t msg);
+void ng_btsocket_hci_raw_peeraddr   (netmsg_t msg);
+void ng_btsocket_hci_raw_send       (netmsg_t msg);
+void ng_btsocket_hci_raw_sockaddr   (netmsg_t msg);
 
 #endif /* _KERNEL */
  
index 0415b0c..7e4e195 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_btsocket_l2cap.h,v 1.4 2003/03/25 23:53:33 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_btsocket_l2cap.h,v 1.8 2006/07/21 17:11:13 rwatson Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_btsocket_l2cap.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_BTSOCKET_L2CAP_H_
@@ -76,7 +75,7 @@ struct ng_btsocket_l2cap_raw_pcb {
        u_int32_t                                token; /* message token */
        struct ng_mesg                          *msg;   /* message */
 
-       struct mtx                               pcb_mtx; /* pcb mutex */
+       struct lock                              pcb_lock; /* pcb lock */
 
        LIST_ENTRY(ng_btsocket_l2cap_raw_pcb)    next;  /* link to next PCB */
 };
@@ -93,22 +92,19 @@ typedef struct ng_btsocket_l2cap_raw_pcb *  ng_btsocket_l2cap_raw_pcb_p;
 #ifdef _KERNEL
 
 void ng_btsocket_l2cap_raw_init       (void);
-void ng_btsocket_l2cap_raw_abort      (struct socket *);
+void ng_btsocket_l2cap_raw_abort      (netmsg_t msg);
+#if 0 /* XXX */
 void ng_btsocket_l2cap_raw_close      (struct socket *);
-int  ng_btsocket_l2cap_raw_attach     (struct socket *, int, struct thread *);
-int  ng_btsocket_l2cap_raw_bind       (struct socket *, struct sockaddr *,
-                                       struct thread *);
-int  ng_btsocket_l2cap_raw_connect    (struct socket *, struct sockaddr *,
-                                       struct thread *);
-int  ng_btsocket_l2cap_raw_control    (struct socket *, u_long, caddr_t,
-                                       struct ifnet *, struct thread *);
-void ng_btsocket_l2cap_raw_detach     (struct socket *);
-int  ng_btsocket_l2cap_raw_disconnect (struct socket *);
-int  ng_btsocket_l2cap_raw_peeraddr   (struct socket *, struct sockaddr **);
-int  ng_btsocket_l2cap_raw_send       (struct socket *, int, struct mbuf *,
-                                       struct sockaddr *, struct mbuf *,
-                                       struct thread *);
-int  ng_btsocket_l2cap_raw_sockaddr   (struct socket *, struct sockaddr **);
+#endif
+void ng_btsocket_l2cap_raw_attach     (netmsg_t msg);
+void ng_btsocket_l2cap_raw_bind       (netmsg_t msg);
+void ng_btsocket_l2cap_raw_connect    (netmsg_t msg);
+void ng_btsocket_l2cap_raw_control    (netmsg_t msg);
+void ng_btsocket_l2cap_raw_detach     (netmsg_t msg);
+void ng_btsocket_l2cap_raw_disconnect (netmsg_t msg);
+void ng_btsocket_l2cap_raw_peeraddr   (netmsg_t msg);
+void ng_btsocket_l2cap_raw_send       (netmsg_t msg);
+void ng_btsocket_l2cap_raw_sockaddr   (netmsg_t msg);
 
 #endif /* _KERNEL */
 
@@ -163,12 +159,12 @@ struct ng_btsocket_l2cap_pcb {
        u_int16_t                        flush_timo; /* flush timeout */   
        u_int16_t                        link_timo;  /* link timeout */ 
 
-       struct callout_handle            timo;       /* timeout */
+       struct callout                   timo;       /* timeout */
 
        u_int32_t                        token;      /* message token */
        ng_btsocket_l2cap_rtentry_p      rt;         /* routing info */
 
-       struct mtx                       pcb_mtx;    /* pcb mutex */
+       struct lock                      pcb_lock;    /* pcb lock */
 
        LIST_ENTRY(ng_btsocket_l2cap_pcb) next;      /* link to next PCB */
 };
@@ -185,25 +181,22 @@ typedef struct ng_btsocket_l2cap_pcb *    ng_btsocket_l2cap_pcb_p;
 #ifdef _KERNEL
 
 void ng_btsocket_l2cap_init       (void);
-void ng_btsocket_l2cap_abort      (struct socket *);
+void ng_btsocket_l2cap_abort      (netmsg_t msg);
+#if 0 /* XXX */
 void ng_btsocket_l2cap_close      (struct socket *);
-int  ng_btsocket_l2cap_accept     (struct socket *, struct sockaddr **);
-int  ng_btsocket_l2cap_attach     (struct socket *, int, struct thread *);
-int  ng_btsocket_l2cap_bind       (struct socket *, struct sockaddr *,
-                                   struct thread *);
-int  ng_btsocket_l2cap_connect    (struct socket *, struct sockaddr *,
-                                   struct thread *);
-int  ng_btsocket_l2cap_control    (struct socket *, u_long, caddr_t,
-                                   struct ifnet *, struct thread *);
-int  ng_btsocket_l2cap_ctloutput  (struct socket *, struct sockopt *);
-void ng_btsocket_l2cap_detach     (struct socket *);
-int  ng_btsocket_l2cap_disconnect (struct socket *);
-int  ng_btsocket_l2cap_listen     (struct socket *, int, struct thread *);
-int  ng_btsocket_l2cap_peeraddr   (struct socket *, struct sockaddr **);
-int  ng_btsocket_l2cap_send       (struct socket *, int, struct mbuf *,
-                                   struct sockaddr *, struct mbuf *,
-                                   struct thread *);
-int  ng_btsocket_l2cap_sockaddr   (struct socket *, struct sockaddr **);
+#endif
+void ng_btsocket_l2cap_accept     (netmsg_t msg);
+void ng_btsocket_l2cap_attach     (netmsg_t msg);
+void ng_btsocket_l2cap_bind       (netmsg_t msg);
+void ng_btsocket_l2cap_connect    (netmsg_t msg);
+void ng_btsocket_l2cap_control    (netmsg_t msg);
+void ng_btsocket_l2cap_ctloutput  (netmsg_t msg);
+void ng_btsocket_l2cap_detach     (netmsg_t msg);
+void ng_btsocket_l2cap_disconnect (netmsg_t msg);
+void ng_btsocket_l2cap_listen     (netmsg_t msg);
+void ng_btsocket_l2cap_peeraddr   (netmsg_t msg);
+void ng_btsocket_l2cap_send       (netmsg_t msg);
+void ng_btsocket_l2cap_sockaddr   (netmsg_t msg);
 
 #endif /* _KERNEL */
 
index 51caff4..a57a1f5 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_btsocket_rfcomm.h,v 1.10 2003/03/29 22:27:42 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_btsocket_rfcomm.h,v 1.8 2006/07/21 17:11:13 rwatson Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_btsocket_rfcomm.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_BTSOCKET_RFCOMM_H_
@@ -252,7 +251,7 @@ struct ng_btsocket_rfcomm_session {
        u_int16_t                                mtu;    /* default MTU */
        struct ng_bt_mbufq                       outq;   /* outgoing queue */
 
-       struct mtx                               session_mtx; /* session lock */
+       struct lock                              session_lock; /* session lock */
        LIST_HEAD(, ng_btsocket_rfcomm_pcb)      dlcs;   /* active DLC */
 
        LIST_ENTRY(ng_btsocket_rfcomm_session)   next;   /* link to next */
@@ -296,8 +295,8 @@ struct ng_btsocket_rfcomm_pcb {
        int16_t                                  rx_cred; /* RX credits */
        int16_t                                  tx_cred; /* TX credits */
 
-       struct mtx                               pcb_mtx; /* PCB lock */
-       struct callout_handle                    timo;    /* timeout */
+       struct lock                              pcb_lock; /* PCB lock */
+       struct callout                           timo;    /* timeout */
 
        LIST_ENTRY(ng_btsocket_rfcomm_pcb)       session_next;/* link to next */
        LIST_ENTRY(ng_btsocket_rfcomm_pcb)       next;    /* link to next */
@@ -315,25 +314,22 @@ typedef struct ng_btsocket_rfcomm_pcb *   ng_btsocket_rfcomm_pcb_p;
 #ifdef _KERNEL
 
 void ng_btsocket_rfcomm_init       (void);
-void ng_btsocket_rfcomm_abort      (struct socket *);
+void ng_btsocket_rfcomm_abort      (netmsg_t msg);
+#if 0 /* XXX */
 void ng_btsocket_rfcomm_close      (struct socket *);
-int  ng_btsocket_rfcomm_accept     (struct socket *, struct sockaddr **);
-int  ng_btsocket_rfcomm_attach     (struct socket *, int, struct thread *);
-int  ng_btsocket_rfcomm_bind       (struct socket *, struct sockaddr *,
-                                    struct thread *);
-int  ng_btsocket_rfcomm_connect    (struct socket *, struct sockaddr *,
-                                    struct thread *);
-int  ng_btsocket_rfcomm_control    (struct socket *, u_long, caddr_t,
-                                    struct ifnet *, struct thread *);
-int  ng_btsocket_rfcomm_ctloutput  (struct socket *, struct sockopt *);
-void ng_btsocket_rfcomm_detach     (struct socket *);
-int  ng_btsocket_rfcomm_disconnect (struct socket *);
-int  ng_btsocket_rfcomm_listen     (struct socket *, int, struct thread *);
-int  ng_btsocket_rfcomm_peeraddr   (struct socket *, struct sockaddr **);
-int  ng_btsocket_rfcomm_send       (struct socket *, int, struct mbuf *,
-                                    struct sockaddr *, struct mbuf *,
-                                    struct thread *);
-int  ng_btsocket_rfcomm_sockaddr   (struct socket *, struct sockaddr **);
+#endif
+void ng_btsocket_rfcomm_accept     (netmsg_t msg);
+void ng_btsocket_rfcomm_attach     (netmsg_t msg);
+void ng_btsocket_rfcomm_bind       (netmsg_t msg);
+void ng_btsocket_rfcomm_connect    (netmsg_t msg);
+void ng_btsocket_rfcomm_control    (netmsg_t msg);
+void ng_btsocket_rfcomm_ctloutput  (netmsg_t msg);
+void ng_btsocket_rfcomm_detach     (netmsg_t msg);
+void ng_btsocket_rfcomm_disconnect (netmsg_t msg);
+void ng_btsocket_rfcomm_listen     (netmsg_t msg);
+void ng_btsocket_rfcomm_peeraddr   (netmsg_t msg);
+void ng_btsocket_rfcomm_send       (netmsg_t msg);
+void ng_btsocket_rfcomm_sockaddr   (netmsg_t msg);
 
 #endif /* _KERNEL */
 
index 091d0a4..7e9969c 100644 (file)
@@ -28,8 +28,7 @@
  * SUCH DAMAGE.
  *
  * $Id: ng_h4.h,v 1.1 2002/11/24 19:47:05 max Exp $
- * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_h4.h,v 1.5 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_h4.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
+ * $FreeBSD: head/sys/netgraph/bluetooth/include/ng_h4.h 139823 2005-01-07 01:45:51Z imp $
  * 
  * Based on:
  * ---------
index 7d7e1d4..da47555 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_hci.h,v 1.2 2003/03/18 00:09:37 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_hci.h,v 1.7 2006/05/17 00:13:06 emax Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_hci.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 /*
index f455743..3be95d9 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap.h,v 1.2 2003/04/27 00:52:26 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_l2cap.h,v 1.4 2005/08/31 18:13:23 emax Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_l2cap.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 /*
index d8f5ffd..0233f08 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_ubt.h,v 1.6 2003/04/13 21:34:42 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_ubt.h,v 1.3 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/include/ng_ubt.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NG_UBT_H_
diff --git a/sys/netgraph7/bluetooth/l2cap/Makefile b/sys/netgraph7/bluetooth/l2cap/Makefile
new file mode 100644 (file)
index 0000000..be4722c
--- /dev/null
@@ -0,0 +1,8 @@
+# $Id: Makefile,v 1.1 2002/11/24 20:40:11 max Exp $
+# $FreeBSD: head/sys/modules/netgraph/bluetooth/l2cap/Makefile 124472 2004-01-13 11:28:51Z ru $
+
+KMOD=          ng_l2cap
+SRCS=          ng_l2cap_main.c ng_l2cap_cmds.c ng_l2cap_evnt.c \
+               ng_l2cap_ulpi.c ng_l2cap_llpi.c ng_l2cap_misc.c
+
+.include <bsd.kmod.mk>
index 9baf983..475f47e 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/l2cap/ng_l2cap_var.h"
-#include "bluetooth/l2cap/ng_l2cap_cmds.h"
-#include "bluetooth/l2cap/ng_l2cap_evnt.h"
-#include "bluetooth/l2cap/ng_l2cap_llpi.h"
-#include "bluetooth/l2cap/ng_l2cap_ulpi.h"
-#include "bluetooth/l2cap/ng_l2cap_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_var.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_misc.h>
 
 /******************************************************************************
  ******************************************************************************
index 39e6f8e..c631eb6 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap_cmds.h,v 1.4 2003/04/01 18:15:26 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_cmds.h,v 1.5 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_L2CAP_CMDS_H_
index a226eff..43f4831 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/l2cap/ng_l2cap_var.h"
-#include "bluetooth/l2cap/ng_l2cap_cmds.h"
-#include "bluetooth/l2cap/ng_l2cap_evnt.h"
-#include "bluetooth/l2cap/ng_l2cap_llpi.h"
-#include "bluetooth/l2cap/ng_l2cap_ulpi.h"
-#include "bluetooth/l2cap/ng_l2cap_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_var.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_misc.h>
 
 /******************************************************************************
  ******************************************************************************
index 151d4f5..d4cd0c9 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap_evnt.h,v 1.1 2002/11/24 19:47:06 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_evnt.h,v 1.3 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_L2CAP_EVNT_H_
index 3203879..4570fe5 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/l2cap/ng_l2cap_var.h"
-#include "bluetooth/l2cap/ng_l2cap_cmds.h"
-#include "bluetooth/l2cap/ng_l2cap_evnt.h"
-#include "bluetooth/l2cap/ng_l2cap_llpi.h"
-#include "bluetooth/l2cap/ng_l2cap_ulpi.h"
-#include "bluetooth/l2cap/ng_l2cap_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_var.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_misc.h>
 
 /******************************************************************************
  ******************************************************************************
index 0c7847a..a630bd4 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap_llpi.h,v 1.2 2003/04/28 21:44:59 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_llpi.h,v 1.3 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_L2CAP_LLPI_H_
index 52ea4f8..07ad403 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "ng_parse.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/l2cap/ng_l2cap_var.h"
-#include "bluetooth/l2cap/ng_l2cap_cmds.h"
-#include "bluetooth/l2cap/ng_l2cap_evnt.h"
-#include "bluetooth/l2cap/ng_l2cap_llpi.h"
-#include "bluetooth/l2cap/ng_l2cap_ulpi.h"
-#include "bluetooth/l2cap/ng_l2cap_misc.h"
-#include "bluetooth/l2cap/ng_l2cap_prse.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/ng_parse.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_var.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_misc.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_prse.h>
 
 /******************************************************************************
  ******************************************************************************
index 4fecd82..8caf653 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap_misc.c,v 1.5 2003/09/08 19:11:45 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_misc.c,v 1.12 2005/08/31 18:13:23 emax Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_misc.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #include <sys/param.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/l2cap/ng_l2cap_var.h"
-#include "bluetooth/l2cap/ng_l2cap_cmds.h"
-#include "bluetooth/l2cap/ng_l2cap_evnt.h"
-#include "bluetooth/l2cap/ng_l2cap_llpi.h"
-#include "bluetooth/l2cap/ng_l2cap_ulpi.h"
-#include "bluetooth/l2cap/ng_l2cap_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_var.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_misc.h>
 
 static u_int16_t       ng_l2cap_get_cid        (ng_l2cap_p);
 
index d32f8dc..7892499 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap_misc.h,v 1.3 2003/09/08 19:11:45 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_misc.h,v 1.6 2005/08/21 19:15:14 pjd Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_misc.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_L2CAP_MISC_H_
index 2e17c74..bc2dba3 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap_prse.h,v 1.2 2003/04/28 21:44:59 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_prse.h,v 1.4 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_prse.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 /***************************************************************************
index f1bb321..94dfcad 100644 (file)
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/queue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/l2cap/ng_l2cap_var.h"
-#include "bluetooth/l2cap/ng_l2cap_cmds.h"
-#include "bluetooth/l2cap/ng_l2cap_evnt.h"
-#include "bluetooth/l2cap/ng_l2cap_llpi.h"
-#include "bluetooth/l2cap/ng_l2cap_ulpi.h"
-#include "bluetooth/l2cap/ng_l2cap_misc.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_var.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_evnt.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_llpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h>
+#include <netgraph7/bluetooth/l2cap/ng_l2cap_misc.h>
 
 /******************************************************************************
  ******************************************************************************
index f7cb0a8..4e9380c 100644 (file)
@@ -29,7 +29,6 @@
  *
  * $Id: ng_l2cap_ulpi.h,v 1.1 2002/11/24 19:47:06 max Exp $
  * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h,v 1.3 2005/01/07 01:45:43 imp Exp $
- * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_ulpi.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
  */
 
 #ifndef _NETGRAPH_L2CAP_ULPI_H_
diff --git a/sys/netgraph7/bluetooth/socket/Makefile b/sys/netgraph7/bluetooth/socket/Makefile
new file mode 100644 (file)
index 0000000..cbc7ad4
--- /dev/null
@@ -0,0 +1,11 @@
+# $Id: Makefile,v 1.2 2003/01/12 23:37:31 max Exp $
+# $FreeBSD: head/sys/modules/netgraph/bluetooth/socket/Makefile 181034 2008-07-30 22:42:17Z emax $
+
+KMOD=          ng_btsocket
+SRCS=          ng_btsocket.c \
+               ng_btsocket_hci_raw.c \
+               ng_btsocket_l2cap_raw.c \
+               ng_btsocket_l2cap.c \
+               ng_btsocket_rfcomm.c
+
+.include <bsd.kmod.mk>
index b38d0ed..8a8d5d0 100644 (file)
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/bitstring.h>
+#include <bitstring.h>         /* XXX */
 #include <sys/errno.h>
 #include <sys/domain.h>
 #include <sys/kernel.h>
 #include <sys/lock.h>
 #include <sys/mbuf.h>
-#include <sys/mutex.h>
 #include <sys/protosw.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 #include <sys/taskqueue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/include/ng_btsocket.h"
-#include "bluetooth/include/ng_btsocket_hci_raw.h"
-#include "bluetooth/include/ng_btsocket_l2cap.h"
-#include "bluetooth/include/ng_btsocket_rfcomm.h"
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/include/ng_btsocket.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_hci_raw.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_l2cap.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_rfcomm.h>
 
 static int                     ng_btsocket_modevent (module_t, int, void *);
 extern struct domain           ng_btsocket_domain;
@@ -74,7 +73,11 @@ static struct pr_usrreqs     ng_btsocket_hci_raw_usrreqs = {
        .pru_send =             ng_btsocket_hci_raw_send,
        .pru_shutdown =         NULL,
        .pru_sockaddr =         ng_btsocket_hci_raw_sockaddr,
+#if 0 /* XXX */
        .pru_close =            ng_btsocket_hci_raw_close,
+#endif
+       .pru_sosend =           sosend,
+       .pru_soreceive =        soreceive,
 };
 
 /*
@@ -93,7 +96,11 @@ static struct pr_usrreqs     ng_btsocket_l2cap_raw_usrreqs = {
        .pru_send =             ng_btsocket_l2cap_raw_send,
        .pru_shutdown =         NULL,
        .pru_sockaddr =         ng_btsocket_l2cap_raw_sockaddr,
+#if 0 /* XXX */
        .pru_close =            ng_btsocket_l2cap_raw_close,
+#endif
+       .pru_sosend =           sosend,
+       .pru_soreceive =        soreceive,
 };
 
 /*
@@ -114,7 +121,11 @@ static struct pr_usrreqs   ng_btsocket_l2cap_usrreqs = {
        .pru_send =             ng_btsocket_l2cap_send,
        .pru_shutdown =         NULL,
        .pru_sockaddr =         ng_btsocket_l2cap_sockaddr,
+#if 0 /* XXX */
        .pru_close =            ng_btsocket_l2cap_close,
+#endif
+       .pru_sosend =           sosend,
+       .pru_soreceive =        soreceive,
 };
 
 /*
@@ -135,7 +146,11 @@ static struct pr_usrreqs   ng_btsocket_rfcomm_usrreqs = {
        .pru_send =             ng_btsocket_rfcomm_send,
        .pru_shutdown =         NULL,
        .pru_sockaddr =         ng_btsocket_rfcomm_sockaddr,
+#if 0 /* XXX */
        .pru_close =            ng_btsocket_rfcomm_close,
+#endif
+       .pru_sosend =           sosend,
+       .pru_soreceive =        soreceive,
 };
 
 /* 
index f2b72d8..f6c8b60 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/bitstring.h>
+#include <bitstring.h>         /* XXX */
 #include <sys/domain.h>
 #include <sys/endian.h>
 #include <sys/errno.h>
@@ -42,7 +42,6 @@
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
-#include <sys/mutex.h>
 #include <sys/priv.h>
 #include <sys/protosw.h>
 #include <sys/queue.h>
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 #include <sys/taskqueue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/include/ng_btsocket.h"
-#include "bluetooth/include/ng_btsocket_hci_raw.h"
+#include <sys/msgport2.h>
+#include <sys/refcount.h>
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/netgraph2.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/include/ng_btsocket.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_hci_raw.h>
 
 /* MALLOC define */
 #ifdef NG_SEPARATE_MALLOC
@@ -111,12 +113,12 @@ static u_int32_t                          ng_btsocket_hci_raw_debug_level;
 static u_int32_t                               ng_btsocket_hci_raw_ioctl_timeout;
 static node_p                                  ng_btsocket_hci_raw_node;
 static struct ng_bt_itemq                      ng_btsocket_hci_raw_queue;
-static struct mtx                              ng_btsocket_hci_raw_queue_mtx;
+static struct lock                             ng_btsocket_hci_raw_queue_lock;
 static struct task                             ng_btsocket_hci_raw_task;
 static LIST_HEAD(, ng_btsocket_hci_raw_pcb)    ng_btsocket_hci_raw_sockets;
-static struct mtx                              ng_btsocket_hci_raw_sockets_mtx;
+static struct lock                             ng_btsocket_hci_raw_sockets_lock;
 static u_int32_t                               ng_btsocket_hci_raw_token;
-static struct mtx                              ng_btsocket_hci_raw_token_mtx;
+static struct lock                             ng_btsocket_hci_raw_token_lock;
 static struct ng_btsocket_hci_raw_sec_filter   *ng_btsocket_hci_raw_sec_filter;
  
 /* Sysctl tree */
@@ -265,7 +267,7 @@ ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
                        return (0);
                }
 
-               mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
+               lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_EXCLUSIVE);
                if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
                        NG_BTSOCKET_HCI_RAW_ERR(
 "%s: Input queue is full\n", __func__);
@@ -274,10 +276,11 @@ ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
                        NG_FREE_ITEM(item);
                        error = ENOBUFS;
                } else {
+                       ng_ref_item(item);
                        NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
                        error = ng_btsocket_hci_raw_wakeup_input_task();
                }
-               mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
+               lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_RELEASE);
        } else {
                NG_FREE_ITEM(item);
                error = EINVAL;
@@ -318,7 +321,7 @@ ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
                NGI_GET_M(item, nam->m_next);
                NGI_M(item) = nam;
 
-               mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
+               lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_EXCLUSIVE);
                if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
                        NG_BTSOCKET_HCI_RAW_ERR(
 "%s: Input queue is full\n", __func__);
@@ -327,10 +330,11 @@ ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
                        NG_FREE_ITEM(item);
                        error = ENOBUFS;
                } else {
+                       ng_ref_item(item);
                        NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
                        error = ng_btsocket_hci_raw_wakeup_input_task();
                }
-               mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
+               lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_RELEASE);
        } else {
                NG_BTSOCKET_HCI_RAW_ERR(
 "%s: Failed to allocate address mbuf\n", __func__);
@@ -357,14 +361,14 @@ ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
 static void
 ng_btsocket_hci_raw_get_token(u_int32_t *token)
 {
-       mtx_lock(&ng_btsocket_hci_raw_token_mtx);
+       lockmgr(&ng_btsocket_hci_raw_token_lock, LK_EXCLUSIVE);
   
        if (++ ng_btsocket_hci_raw_token == 0)
                ng_btsocket_hci_raw_token = 1;
  
        *token = ng_btsocket_hci_raw_token;
  
-       mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
+       lockmgr(&ng_btsocket_hci_raw_token_lock, LK_RELEASE);
 } /* ng_btsocket_hci_raw_get_token */
 
 /*
@@ -400,7 +404,7 @@ ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
        struct ng_mesg  *msg = NULL;
        int              error = 0;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
        if (msg == NULL)
@@ -416,7 +420,7 @@ ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
                return (error);
        }
 
-       error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl", 
+       error = lksleep(&pcb->msg, &pcb->pcb_lock, PCATCH, "hcictl", 
                        ng_btsocket_hci_raw_ioctl_timeout * hz);
        pcb->token = 0;
 
@@ -444,7 +448,7 @@ ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
        int             dir;
        struct timeval  tv;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
                dir = (m->m_flags & M_PROTO1)? 1 : 0;
@@ -484,11 +488,11 @@ ng_btsocket_hci_raw_data_input(struct mbuf *nam)
 
        sa = mtod(nam, struct sockaddr_hci *);
 
-       mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
 
        LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
 
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                /*
                 * If socket was bound then check address and
@@ -519,7 +523,7 @@ ng_btsocket_hci_raw_data_input(struct mbuf *nam)
 
                        ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
 
-                       if (sbappendaddr(&pcb->so->so_rcv, 
+                       if (sbappendaddr(&pcb->so->so_rcv.sb
                                        (struct sockaddr *) sa, m, ctl))
                                sorwakeup(pcb->so);
                        else {
@@ -531,10 +535,10 @@ ng_btsocket_hci_raw_data_input(struct mbuf *nam)
                        }
                }
 next:
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
        }
 
-       mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
 
        NG_FREE_M(nam);
        NG_FREE_M(m0);
@@ -549,25 +553,25 @@ ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
 {
        ng_btsocket_hci_raw_pcb_p       pcb = NULL;
 
-       mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
 
        LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                if (msg->header.token == pcb->token) {
                        pcb->msg = msg;
                        wakeup(&pcb->msg);
 
-                       mtx_unlock(&pcb->pcb_mtx);
-                       mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+                       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
 
                        return;
                }
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
        }
 
-       mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
 
        NG_FREE_MSG(msg); /* checks for != NULL */
 } /* ng_btsocket_hci_raw_msg_input */
@@ -582,9 +586,9 @@ ng_btsocket_hci_raw_input(void *context, int pending)
        item_p  item = NULL;
 
        for (;;) {
-               mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
+               lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_EXCLUSIVE);
                NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
-               mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
+               lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_RELEASE);
 
                if (item == NULL)
                        break;
@@ -611,6 +615,7 @@ ng_btsocket_hci_raw_input(void *context, int pending)
                }
 
                NG_FREE_ITEM(item);
+               ng_unref_item(item, 0);
        }
 } /* ng_btsocket_hci_raw_input */
 
@@ -666,7 +671,7 @@ ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
 {
        int     type, event, opcode;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        switch ((type = *mtod(m, u_int8_t *))) {
        case NG_HCI_CMD_PKT:
@@ -760,20 +765,20 @@ ng_btsocket_hci_raw_init(void)
 
        /* Create input queue */
        NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, ifqmaxlen);
-       mtx_init(&ng_btsocket_hci_raw_queue_mtx,
-               "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_hci_raw_queue_lock,
+               "btsocks_hci_raw_queue_lock", 0, 0);
        TASK_INIT(&ng_btsocket_hci_raw_task, 0,
                ng_btsocket_hci_raw_input, NULL);
 
        /* Create list of sockets */
        LIST_INIT(&ng_btsocket_hci_raw_sockets);
-       mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
-               "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_hci_raw_sockets_lock,
+               "btsocks_hci_raw_sockets_lock", 0, 0);
 
        /* Tokens */
        ng_btsocket_hci_raw_token = 0;
-       mtx_init(&ng_btsocket_hci_raw_token_mtx,
-               "btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_hci_raw_token_lock,
+               "btsocks_hci_raw_token_lock", 0, 0);
 
        /* 
         * Security filter
@@ -873,49 +878,64 @@ ng_btsocket_hci_raw_init(void)
  */
 
 void
-ng_btsocket_hci_raw_abort(struct socket *so)
+ng_btsocket_hci_raw_abort(netmsg_t msg)
 {
 } /* ng_btsocket_hci_raw_abort */
 
+#if 0 /* XXX */
 void
 ng_btsocket_hci_raw_close(struct socket *so)
 {
 } /* ng_btsocket_hci_raw_close */
+#endif
 
 /*
  * Create new raw HCI socket
  */
 
-int
-ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
+void
+ng_btsocket_hci_raw_attach(netmsg_t msg)
 {
-       ng_btsocket_hci_raw_pcb_p       pcb = so2hci_raw_pcb(so);
-       int                             error = 0;
+       struct socket                   *so = msg->attach.base.nm_so;
+       int                              proto = msg->attach.nm_proto;
+       ng_btsocket_hci_raw_pcb_p        pcb = so2hci_raw_pcb(so);
+       int                              error = 0;
 
-       if (pcb != NULL)
-               return (EISCONN);
+       if (pcb != NULL) {
+               error = EISCONN;
+               goto out;
+       }
 
-       if (ng_btsocket_hci_raw_node == NULL)
-               return (EPROTONOSUPPORT);
-       if (proto != BLUETOOTH_PROTO_HCI)
-               return (EPROTONOSUPPORT);
-       if (so->so_type != SOCK_RAW)
-               return (ESOCKTNOSUPPORT);
+       if (ng_btsocket_hci_raw_node == NULL) {
+               error = EPROTONOSUPPORT;
+               goto out;
+       }
+       if (proto != BLUETOOTH_PROTO_HCI) {
+               error = EPROTONOSUPPORT;
+               goto out;
+       }
+       if (so->so_type != SOCK_RAW) {
+               error = ESOCKTNOSUPPORT;
+               goto out;
+       }
 
        error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
-                               NG_BTSOCKET_HCI_RAW_RECVSPACE);
+                               NG_BTSOCKET_HCI_RAW_RECVSPACE, NULL);
        if (error != 0)
-               return (error);
+               goto out;
 
        pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_HCI_RAW,
                      M_WAITOK | M_NULLOK | M_ZERO);
-       if (pcb == NULL)
-               return (ENOMEM);
+       if (pcb == NULL) {
+               error = ENOMEM;
+               goto out;
+       }
 
        so->so_pcb = (caddr_t) pcb;
        pcb->so = so;
 
-       if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
+       if (curproc == NULL ||
+           priv_check(curthread, PRIV_NETBLUETOOTH_RAW) == 0)
                pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
 
        /*
@@ -926,116 +946,156 @@ ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
        bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
        bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
 
-       mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
+       lockinit(&pcb->pcb_lock, "btsocks_hci_raw_pcb_lock", 0, 0);
 
-       mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
        LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
-       mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->attach.base.lmsg, error);
 } /* ng_btsocket_hci_raw_attach */
 
 /*
  * Bind raw HCI socket
  */
 
-int
-ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
-               struct thread *td)
+void
+ng_btsocket_hci_raw_bind(netmsg_t msg)
 {
+       struct socket                   *so = msg->bind.base.nm_so;
+       struct sockaddr                 *nam = msg->bind.nm_nam;
        ng_btsocket_hci_raw_pcb_p        pcb = so2hci_raw_pcb(so);
        struct sockaddr_hci             *sa = (struct sockaddr_hci *) nam;
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_hci_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_hci_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->hci_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->hci_len != sizeof(*sa))
-               return (EINVAL);
-       if (sa->hci_node[0] == 0)
-               return (EINVAL);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->hci_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->hci_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->hci_node[0] == 0) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
        bcopy(sa, &pcb->addr, sizeof(pcb->addr));
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->bind.base.lmsg, error);
 } /* ng_btsocket_hci_raw_bind */
 
 /*
  * Connect raw HCI socket
  */
 
-int
-ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
-               struct thread *td)
+void
+ng_btsocket_hci_raw_connect(netmsg_t msg)
 {
+       struct socket                   *so = msg->connect.base.nm_so;
+       struct sockaddr                 *nam = msg->connect.nm_nam;
        ng_btsocket_hci_raw_pcb_p        pcb = so2hci_raw_pcb(so);
        struct sockaddr_hci             *sa = (struct sockaddr_hci *) nam;
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_hci_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_hci_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->hci_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->hci_len != sizeof(*sa))
-               return (EINVAL);
-       if (sa->hci_node[0] == 0)
-               return (EDESTADDRREQ);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->hci_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->hci_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->hci_node[0] == 0) {
+               error = EDESTADDRREQ;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
-               mtx_unlock(&pcb->pcb_mtx);
-               return (EADDRNOTAVAIL);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EADDRNOTAVAIL;
+               goto out;
        }
 
        soisconnected(so);
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->connect.base.lmsg, error);
 } /* ng_btsocket_hci_raw_connect */
 
 /*
  * Process ioctl on socket
  */
 
-int
-ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
-               struct ifnet *ifp, struct thread *td)
+void
+ng_btsocket_hci_raw_control(netmsg_t msg)
 {
+       struct socket                   *so = msg->control.base.nm_so;
+       u_long                           cmd = msg->control.nm_cmd;
+       caddr_t                          data = msg->control.nm_data;
        ng_btsocket_hci_raw_pcb_p        pcb = so2hci_raw_pcb(so);
        char                             path[NG_NODESIZ + 1];
-       struct ng_mesg                  *msg = NULL;
+       struct ng_mesg                  *ngmsg = NULL;
        int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_hci_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_hci_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* Check if we have device name */
        if (pcb->addr.hci_node[0] == 0) {
-               mtx_unlock(&pcb->pcb_mtx);
-               return (EHOSTUNREACH);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EHOSTUNREACH;
+               goto out;
        }
 
        /* Check if we have pending ioctl() */
        if (pcb->token != 0) {
-               mtx_unlock(&pcb->pcb_mtx);
-               return (EBUSY);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EBUSY;
+               goto out;
        }
 
        ksnprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
@@ -1145,24 +1205,24 @@ ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
                        break;
                }
 
-               NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
+               NG_MKMESSAGE(ngmsg, NGM_HCI_COOKIE,
                        NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_WAITOK | M_NULLOK);
-               if (msg == NULL) {
+               if (ngmsg == NULL) {
                        error = ENOMEM;
                        break;
                }
-               ng_btsocket_hci_raw_get_token(&msg->header.token);
-               pcb->token = msg->header.token;
+               ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
+               pcb->token = ngmsg->header.token;
                pcb->msg = NULL;
 
-               NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
+               NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, path, 0);
                if (error != 0) {
                        pcb->token = 0;
                        break;
                }
 
-               error = msleep(&pcb->msg, &pcb->pcb_mtx,
-                               PZERO|PCATCH, "hcictl", 
+               error = lksleep(&pcb->msg, &pcb->pcb_lock,
+                               PCATCH, "hcictl", 
                                ng_btsocket_hci_raw_ioctl_timeout * hz);
                pcb->token = 0;
 
@@ -1201,24 +1261,24 @@ ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
                        break;
                }
 
-               NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
+               NG_MKMESSAGE(ngmsg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
                        0, M_WAITOK | M_NULLOK);
-               if (msg == NULL) {
+               if (ngmsg == NULL) {
                        error = ENOMEM;
                        break;
                }
-               ng_btsocket_hci_raw_get_token(&msg->header.token);
-               pcb->token = msg->header.token;
+               ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
+               pcb->token = ngmsg->header.token;
                pcb->msg = NULL;
 
-               NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
+               NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, path, 0);
                if (error != 0) {
                        pcb->token = 0;
                        break;
                }
 
-               error = msleep(&pcb->msg, &pcb->pcb_mtx,
-                               PZERO|PCATCH, "hcictl", 
+               error = lksleep(&pcb->msg, &pcb->pcb_lock,
+                               PCATCH, "hcictl", 
                                ng_btsocket_hci_raw_ioctl_timeout * hz);
                pcb->token = 0;
 
@@ -1321,24 +1381,24 @@ ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
                        break;
                }
 
-               NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
+               NG_MKMESSAGE(ngmsg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
                        0, M_WAITOK | M_NULLOK);
-               if (msg == NULL) {
+               if (ngmsg == NULL) {
                        error = ENOMEM;
                        break;
                }
-               ng_btsocket_hci_raw_get_token(&msg->header.token);
-               pcb->token = msg->header.token;
+               ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
+               pcb->token = ngmsg->header.token;
                pcb->msg = NULL;
 
-               NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
+               NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, ".:", 0);
                if (error != 0) {
                        pcb->token = 0;
                        break;
                }
 
-               error = msleep(&pcb->msg, &pcb->pcb_mtx,
-                               PZERO|PCATCH, "hcictl",
+               error = lksleep(&pcb->msg, &pcb->pcb_lock,
+                               PCATCH, "hcictl",
                                ng_btsocket_hci_raw_ioctl_timeout * hz);
                pcb->token = 0;
 
@@ -1378,31 +1438,38 @@ ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->control.base.lmsg, error);
 } /* ng_btsocket_hci_raw_control */
 
 /*
  * Process getsockopt/setsockopt system calls
  */
 
-int
-ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
+void
+ng_btsocket_hci_raw_ctloutput(netmsg_t msg)
 {
-       ng_btsocket_hci_raw_pcb_p               pcb = so2hci_raw_pcb(so);
-       struct ng_btsocket_hci_raw_filter       filter;
-       int                                     error = 0, dir;
+       struct socket                           *so = msg->ctloutput.base.nm_so;
+       struct sockopt                          *sopt = msg->ctloutput.nm_sopt;
+       ng_btsocket_hci_raw_pcb_p                pcb = so2hci_raw_pcb(so);
+       struct ng_btsocket_hci_raw_filter        filter;
+       int                                      error = 0, dir;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_hci_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_hci_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        if (sopt->sopt_level != SOL_HCI_RAW)
-               return (0);
+               goto out;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        switch (sopt->sopt_dir) {
        case SOPT_GET:
@@ -1456,9 +1523,10 @@ ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       
-       return (error);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+
+out:
+       lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
 } /* ng_btsocket_hci_raw_ctloutput */
 
 /*
@@ -1466,70 +1534,85 @@ ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
  */
 
 void
-ng_btsocket_hci_raw_detach(struct socket *so)
+ng_btsocket_hci_raw_detach(netmsg_t msg)
 {
-       ng_btsocket_hci_raw_pcb_p       pcb = so2hci_raw_pcb(so);
+       struct socket                   *so = msg->detach.base.nm_so;
+       ng_btsocket_hci_raw_pcb_p        pcb = so2hci_raw_pcb(so);
+       int                              error = 0;
 
        KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
 
        if (ng_btsocket_hci_raw_node == NULL)
-               return;
+               goto out;
 
-       mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        LIST_REMOVE(pcb, next);
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
 
-       mtx_destroy(&pcb->pcb_mtx);
+       lockuninit(&pcb->pcb_lock);
 
        bzero(pcb, sizeof(*pcb));
        kfree(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
 
        so->so_pcb = NULL;
+
+out:
+       lwkt_replymsg(&msg->detach.base.lmsg, error);
 } /* ng_btsocket_hci_raw_detach */
 
 /*
  * Disconnect raw HCI socket
  */
 
-int
-ng_btsocket_hci_raw_disconnect(struct socket *so)
+void
+ng_btsocket_hci_raw_disconnect(netmsg_t msg)
 {
+       struct socket                   *so = msg->disconnect.base.nm_so;
        ng_btsocket_hci_raw_pcb_p        pcb = so2hci_raw_pcb(so);
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_hci_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_hci_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
        soisdisconnected(so);
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->disconnect.base.lmsg, error);
 } /* ng_btsocket_hci_raw_disconnect */
 
 /*
  * Get socket peer's address
  */
 
-int
-ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_hci_raw_peeraddr(netmsg_t msg)
 {
-       return (ng_btsocket_hci_raw_sockaddr(so, nam));
+       return (ng_btsocket_hci_raw_sockaddr(msg));
 } /* ng_btsocket_hci_raw_peeraddr */
 
 /*
  * Send data
  */
 
-int
-ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
-               struct sockaddr *sa, struct mbuf *control, struct thread *td)
+void
+ng_btsocket_hci_raw_send(netmsg_t msg)
 {
+       struct socket                   *so = msg->send.base.nm_so;
+       struct mbuf                     *control = msg->send.nm_control;
+       struct mbuf                     *m = msg->send.nm_m;
+       struct sockaddr                 *sa = msg->send.nm_addr;
        ng_btsocket_hci_raw_pcb_p        pcb = so2hci_raw_pcb(so);
        struct mbuf                     *nam = NULL;
        int                              error = 0;
@@ -1564,17 +1647,17 @@ ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
                goto drop;
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        error = ng_btsocket_hci_raw_filter(pcb, m, 0);
        if (error != 0) {
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                goto drop;
        }
 
        if (sa == NULL) {
                if (pcb->addr.hci_node[0] == 0) {
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
                        error = EDESTADDRREQ;
                        goto drop;
                }
@@ -1584,7 +1667,7 @@ ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
 
        MGET(nam, MB_DONTWAIT, MT_SONAME);
        if (nam == NULL) {
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                error = ENOBUFS;
                goto drop;
        }
@@ -1595,43 +1678,57 @@ ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
        nam->m_next = m;
        m = NULL;
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+
+       error = ng_send_fn(ng_btsocket_hci_raw_node, NULL,
+                               ng_btsocket_hci_raw_output, nam, 0);
+       goto out;
 
-       return (ng_send_fn(ng_btsocket_hci_raw_node, NULL, 
-                               ng_btsocket_hci_raw_output, nam, 0));
 drop:
        NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
        NG_FREE_M(nam);
        NG_FREE_M(m);
-       
-       return (error);
+
+out:   
+       lwkt_replymsg(&msg->send.base.lmsg, error);
 } /* ng_btsocket_hci_raw_send */
 
 /*
  * Get socket address
  */
 
-int
-ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_hci_raw_sockaddr(netmsg_t msg)
 {
-       ng_btsocket_hci_raw_pcb_p       pcb = so2hci_raw_pcb(so);
-       struct sockaddr_hci             sa;
+       struct socket                    *so = msg->sockaddr.base.nm_so;
+       struct sockaddr                 **nam = msg->sockaddr.nm_nam;
+       ng_btsocket_hci_raw_pcb_p         pcb = so2hci_raw_pcb(so);
+       struct sockaddr_hci               sa;
+       int                               error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_hci_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_hci_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        bzero(&sa, sizeof(sa));
        sa.hci_len = sizeof(sa);
        sa.hci_family = AF_BLUETOOTH;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
        strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+
+       *nam = dup_sockaddr((struct sockaddr *) &sa);
 
-       *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
+       if (*nam == NULL)
+               error = ENOMEM;
 
-       return ((*nam == NULL)? ENOMEM : 0);
+out:
+       lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
 } /* ng_btsocket_hci_raw_sockaddr */
 
index 5681596..10cd243 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/bitstring.h>
+#include <bitstring.h>         /* XXX */
 #include <sys/domain.h>
 #include <sys/endian.h>
 #include <sys/errno.h>
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
-#include <sys/mutex.h>
 #include <sys/protosw.h>
 #include <sys/queue.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 #include <sys/taskqueue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/include/ng_btsocket.h"
-#include "bluetooth/include/ng_btsocket_l2cap.h"
+#include <sys/msgport2.h>
+#include <sys/refcount.h>
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/netgraph2.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/include/ng_btsocket.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_l2cap.h>
 
 /* MALLOC define */
 #ifdef NG_SEPARATE_MALLOC
@@ -95,12 +97,12 @@ extern int                                  ifqmaxlen;
 static u_int32_t                               ng_btsocket_l2cap_debug_level;
 static node_p                                  ng_btsocket_l2cap_node;
 static struct ng_bt_itemq                      ng_btsocket_l2cap_queue;
-static struct mtx                              ng_btsocket_l2cap_queue_mtx;
+static struct lock                             ng_btsocket_l2cap_queue_lock;
 static struct task                             ng_btsocket_l2cap_queue_task;
 static LIST_HEAD(, ng_btsocket_l2cap_pcb)      ng_btsocket_l2cap_sockets;
-static struct mtx                              ng_btsocket_l2cap_sockets_mtx;
+static struct lock                             ng_btsocket_l2cap_sockets_lock;
 static LIST_HEAD(, ng_btsocket_l2cap_rtentry)  ng_btsocket_l2cap_rt;
-static struct mtx                              ng_btsocket_l2cap_rt_mtx;
+static struct lock                             ng_btsocket_l2cap_rt_lock;
 static struct task                             ng_btsocket_l2cap_rt_task;
 
 /* Sysctl tree */
@@ -203,10 +205,10 @@ static ng_btsocket_l2cap_pcb_p     ng_btsocket_l2cap_pcb_by_cid (bdaddr_p, int);
 static int                         ng_btsocket_l2cap_result2errno(int);
 
 #define ng_btsocket_l2cap_wakeup_input_task() \
-       taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_queue_task)
+       taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_queue_task)
 
 #define ng_btsocket_l2cap_wakeup_route_task() \
-       taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_rt_task)
+       taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_rt_task)
 
 /*****************************************************************************
  *****************************************************************************
@@ -323,7 +325,7 @@ ng_btsocket_l2cap_node_rcvmsg(node_p node, item_p item, hook_p hook)
        int              error = 0;
 
        if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
-               mtx_lock(&ng_btsocket_l2cap_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_queue_lock, LK_EXCLUSIVE);
                if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
                        NG_BTSOCKET_L2CAP_ERR(
 "%s: Input queue is full (msg)\n", __func__);
@@ -337,10 +339,11 @@ ng_btsocket_l2cap_node_rcvmsg(node_p node, item_p item, hook_p hook)
                                NGI_SET_HOOK(item, hook);
                        }
 
+                       ng_ref_item(item);
                        NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
                        error = ng_btsocket_l2cap_wakeup_input_task();
                }
-               mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_queue_lock, LK_RELEASE);
        } else {
                NG_FREE_ITEM(item);
                error = EINVAL;
@@ -358,7 +361,7 @@ ng_btsocket_l2cap_node_rcvdata(hook_p hook, item_p item)
 {
        int     error = 0;
 
-       mtx_lock(&ng_btsocket_l2cap_queue_mtx);
+       lockmgr(&ng_btsocket_l2cap_queue_lock, LK_EXCLUSIVE);
        if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
                NG_BTSOCKET_L2CAP_ERR(
 "%s: Input queue is full (data)\n", __func__);
@@ -370,10 +373,11 @@ ng_btsocket_l2cap_node_rcvdata(hook_p hook, item_p item)
                NG_HOOK_REF(hook);
                NGI_SET_HOOK(item, hook);
 
+               ng_ref_item(item);
                NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
                error = ng_btsocket_l2cap_wakeup_input_task();
        }
-       mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
+       lockmgr(&ng_btsocket_l2cap_queue_lock, LK_RELEASE);
 
        return (error);
 } /* ng_btsocket_l2cap_node_rcvdata */
@@ -396,16 +400,16 @@ ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
 
        op = (ng_l2cap_l2ca_con_op *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /* Look for the socket with the token */
        pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
        if (pcb == NULL) {
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (ENOENT);
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        NG_BTSOCKET_L2CAP_INFO(
 "%s: Got L2CA_Connect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
@@ -419,8 +423,8 @@ ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
                pcb->state);
 
        if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
                return (ENOENT);
        }
@@ -429,8 +433,8 @@ ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
 
        if (op->result == NG_L2CAP_PENDING) {
                ng_btsocket_l2cap_timeout(pcb);
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
                return (0);
        }
@@ -470,8 +474,8 @@ ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
                soisdisconnected(pcb->so); 
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (error);
 } /* ng_btsocket_l2cap_process_l2ca_con_req_rsp */
@@ -492,16 +496,16 @@ ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg,
 
        op = (ng_l2cap_l2ca_con_rsp_op *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /* Look for the socket with the token */
        pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
        if (pcb == NULL) {
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (ENOENT);
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        NG_BTSOCKET_L2CAP_INFO(
 "%s: Got L2CA_ConnectRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
@@ -514,8 +518,8 @@ ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg,
                pcb->psm, pcb->cid, op->result, pcb->state);
 
        if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
                return (ENOENT);
        }
@@ -535,8 +539,8 @@ ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg,
                ng_btsocket_l2cap_timeout(pcb);
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (0);
 } /* ng_btsocket_process_l2ca_con_rsp_rsp */
@@ -572,13 +576,13 @@ ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg *msg,
                ip->bdaddr.b[2], ip->bdaddr.b[1], ip->bdaddr.b[0],
                ip->psm, ip->lcid, ip->ident);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
        
        pcb = ng_btsocket_l2cap_pcb_by_addr(&rt->src, ip->psm);
        if (pcb != NULL) {
                struct socket   *so1 = NULL;
 
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                /*
                 * First check the pending connections queue and if we have
@@ -604,7 +608,7 @@ ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg *msg,
                KASSERT((pcb1 != NULL),
 ("%s: pcb1 == NULL\n", __func__));
 
-               mtx_lock(&pcb1->pcb_mtx);
+               lockmgr(&pcb1->pcb_lock, LK_EXCLUSIVE);
 
                if (bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src)) != 0)
                        bcopy(&pcb->src, &pcb1->src, sizeof(pcb1->src));
@@ -643,13 +647,13 @@ respond:
                        ng_btsocket_l2cap_timeout(pcb1);
                }
 
-               mtx_unlock(&pcb1->pcb_mtx);
+               lockmgr(&pcb1->pcb_lock, LK_RELEASE);
        }
 
        if (pcb != NULL)
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (error);
 } /* ng_btsocket_l2cap_process_l2ca_con_ind */
@@ -670,7 +674,7 @@ ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
 
        op = (ng_l2cap_l2ca_cfg_op *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /* 
         * Socket must have issued a Configure request, so we must have a 
@@ -686,11 +690,11 @@ ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
                 * Disconnect, because we do not know channel ID
                 */
 
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (ENOENT);
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
         NG_BTSOCKET_L2CAP_INFO(
 "%s: Got L2CA_Config response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
@@ -704,8 +708,8 @@ ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
                pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
 
        if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
                return (ENOENT);
        }
@@ -765,8 +769,8 @@ ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
                soisdisconnected(pcb->so);
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (0);
 } /* ng_btsocket_l2cap_process_l2ca_cfg_req_rsp */
@@ -788,16 +792,16 @@ ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg,
 
        op = (ng_l2cap_l2ca_cfg_rsp_op *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /* Look for the socket with the token */
        pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
        if (pcb == NULL) {
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (ENOENT);
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
         NG_BTSOCKET_L2CAP_INFO(
 "%s: Got L2CA_ConfigRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
@@ -811,8 +815,8 @@ ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg,
                pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
 
        if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
                return (ENOENT);
        }
@@ -845,8 +849,8 @@ ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg,
                }
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (error);
 
@@ -860,8 +864,8 @@ disconnect:
        pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
        soisdisconnected(pcb->so);
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (error);
 } /* ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp */
@@ -883,16 +887,16 @@ ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg,
 
        ip = (ng_l2cap_l2ca_cfg_ind_ip *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /* Check for the open socket that has given channel ID */
        pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
        if (pcb == NULL) {
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (ENOENT);
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
         NG_BTSOCKET_L2CAP_INFO(
 "%s: Got L2CA_Config indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
@@ -906,8 +910,8 @@ ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg,
 
        /* XXX FIXME re-configuration on open socket */
        if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
                return (ENOENT);
        }
@@ -944,8 +948,8 @@ ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg,
                        pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (error);
 } /* ng_btsocket_l2cap_process_l2cap_cfg_ind */
@@ -967,7 +971,7 @@ ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg *msg,
 
        op = (ng_l2cap_l2ca_discon_op *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /*
         * Socket layer must have issued L2CA_Disconnect request, so there 
@@ -977,11 +981,11 @@ ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg *msg,
 
        pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
        if (pcb == NULL) {
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (0);
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* XXX Close socket no matter what op->result says */
        if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
@@ -1001,8 +1005,8 @@ ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg *msg,
                soisdisconnected(pcb->so);
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (0);
 } /* ng_btsocket_l2cap_process_l2ca_discon_rsp */
@@ -1024,12 +1028,12 @@ ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg *msg,
 
        ip = (ng_l2cap_l2ca_discon_ind_ip *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /* Look for the socket with given channel ID */
        pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
        if (pcb == NULL) {
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (0);
        }
 
@@ -1039,7 +1043,7 @@ ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg *msg,
         * not do anything here anyway.
         */
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                NG_BTSOCKET_L2CAP_INFO(
 "%s: Got L2CA_Disconnect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
@@ -1057,8 +1061,8 @@ ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg *msg,
        pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
        soisdisconnected(pcb->so);
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (0);
 } /* ng_btsocket_l2cap_process_l2ca_discon_ind */
@@ -1080,16 +1084,16 @@ ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg,
 
        op = (ng_l2cap_l2ca_write_op *)(msg->data);
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /* Look for the socket with given token */
        pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
        if (pcb == NULL) {
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                return (ENOENT);
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                NG_BTSOCKET_L2CAP_INFO(
 "%s: Got L2CA_Write response, src bdaddr=%x:%x:%x:%x:%x:%x, " \
@@ -1103,8 +1107,8 @@ ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg,
                        pcb->state);
 
        if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
                return (ENOENT);
        }
@@ -1115,12 +1119,12 @@ ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg,
         * Check if we have more data to send
         */
 
-       sbdroprecord(&pcb->so->so_snd);
-       if (pcb->so->so_snd.sb_cc > 0) {
+       sbdroprecord(&pcb->so->so_snd.sb);
+       if (pcb->so->so_snd.sb.sb_cc > 0) {
                if (ng_btsocket_l2cap_send2(pcb) == 0)
                        ng_btsocket_l2cap_timeout(pcb);
                else
-                       sbdroprecord(&pcb->so->so_snd); /* XXX */
+                       sbdroprecord(&pcb->so->so_snd.sb); /* XXX */
        }
 
        /*
@@ -1131,8 +1135,8 @@ ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg,
        pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
        sowwakeup(pcb->so);
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
        return (0);
 } /* ng_btsocket_l2cap_process_l2ca_write_rsp */
@@ -1148,7 +1152,7 @@ ng_btsocket_l2cap_send_l2ca_con_req(ng_btsocket_l2cap_pcb_p pcb)
        ng_l2cap_l2ca_con_ip    *ip = NULL;
        int                      error = 0;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->rt == NULL || 
            pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
@@ -1216,7 +1220,7 @@ ng_btsocket_l2cap_send_l2ca_cfg_req(ng_btsocket_l2cap_pcb_p pcb)
        ng_l2cap_l2ca_cfg_ip    *ip = NULL;
        int                      error = 0;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->rt == NULL || 
            pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
@@ -1252,7 +1256,7 @@ ng_btsocket_l2cap_send_l2ca_cfg_rsp(ng_btsocket_l2cap_pcb_p pcb)
        ng_l2cap_l2ca_cfg_rsp_ip        *ip = NULL;
        int                              error = 0;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->rt == NULL || 
            pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
@@ -1287,7 +1291,7 @@ ng_btsocket_l2cap_send_l2ca_discon_req(u_int32_t token,
        ng_l2cap_l2ca_discon_ip *ip = NULL;
        int                      error = 0;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->rt == NULL || 
            pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
@@ -1384,16 +1388,16 @@ ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
 
        if (hdr->dcid >= NG_L2CAP_FIRST_CID) {
 
-               mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
                /* Normal packet: find connected socket */
                pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, hdr->dcid);
                if (pcb == NULL) {
-                       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+                       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                        goto drop;
                }
 
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
                        NG_BTSOCKET_L2CAP_ERR(
@@ -1403,8 +1407,8 @@ ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
                                rt->src.b[2], rt->src.b[1], rt->src.b[0],
                                hdr->dcid, pcb->state);
 
-                       mtx_unlock(&pcb->pcb_mtx);
-                       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+                       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                        goto drop;
                }
 
@@ -1418,13 +1422,13 @@ ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
                                rt->src.b[2], rt->src.b[1], rt->src.b[0],
                                hdr->dcid, hdr->length, pcb->imtu);
 
-                       mtx_unlock(&pcb->pcb_mtx);
-                       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+                       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                        goto drop;
                }
 
                /* Check if we have enough space in socket receive queue */
-               if (m->m_pkthdr.len > sbspace(&pcb->so->so_rcv)) {
+               if (m->m_pkthdr.len > ssb_space(&pcb->so->so_rcv)) {
 
                        /* 
                         * This is really bad. Receive queue on socket does
@@ -1440,21 +1444,21 @@ ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
                                rt->src.b[5], rt->src.b[4], rt->src.b[3],
                                rt->src.b[2], rt->src.b[1], rt->src.b[0],
                                hdr->dcid, m->m_pkthdr.len,
-                               sbspace(&pcb->so->so_rcv));
+                               ssb_space(&pcb->so->so_rcv));
 
-                       mtx_unlock(&pcb->pcb_mtx);
-                       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+                       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
                        goto drop;
                }
 
                /* Append packet to the socket receive queue and wakeup */
-               sbappendrecord(&pcb->so->so_rcv, m);
+               sbappendrecord(&pcb->so->so_rcv.sb, m);
                m = NULL;
 
                sorwakeup(pcb->so);
 
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
        } else if (hdr->dcid == NG_L2CAP_CLT_CID) {
                /* Broadcast packet: give packet to all sockets  */
 
@@ -1500,18 +1504,18 @@ ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
                        rt->src.b[2], rt->src.b[1], rt->src.b[0],
                        clt_hdr->psm, hdr->length);
 
-               mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
                LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next) {
                        struct mbuf     *copy = NULL;
 
-                       mtx_lock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                        if (bcmp(&rt->src, &pcb->src, sizeof(pcb->src)) != 0 ||
                            pcb->psm != clt_hdr->psm || 
                            pcb->state != NG_BTSOCKET_L2CAP_OPEN || 
                            (pcb->so->so_options & SO_BROADCAST) == 0 || 
-                           m->m_pkthdr.len > sbspace(&pcb->so->so_rcv))
+                           m->m_pkthdr.len > ssb_space(&pcb->so->so_rcv))
                                goto next;
 
                        /*
@@ -1522,14 +1526,14 @@ ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
 
                        copy = m_dup(m, MB_DONTWAIT);
                        if (copy != NULL) {
-                               sbappendrecord(&pcb->so->so_rcv, copy);
+                               sbappendrecord(&pcb->so->so_rcv.sb, copy);
                                sorwakeup(pcb->so);
                        }
 next:
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
                }
 
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
        }
 drop:
        NG_FREE_M(m); /* checks for m != NULL */
@@ -1552,14 +1556,14 @@ ng_btsocket_l2cap_default_msg_input(struct ng_mesg *msg, hook_p hook)
                if (bcmp(msg->data, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
                        break;
 
-               mtx_lock(&ng_btsocket_l2cap_rt_mtx);
+               lockmgr(&ng_btsocket_l2cap_rt_lock, LK_EXCLUSIVE);
 
                rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook);
                if (rt == NULL) {
                        rt = kmalloc(sizeof(*rt), M_NETGRAPH_BTSOCKET_L2CAP,
                                     M_WAITOK | M_NULLOK | M_ZERO);
                        if (rt == NULL) {
-                               mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
+                               lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
                                break;
                        }
 
@@ -1571,7 +1575,7 @@ ng_btsocket_l2cap_default_msg_input(struct ng_mesg *msg, hook_p hook)
                bcopy(msg->data, &rt->src, sizeof(rt->src));
                rt->hook = hook;
 
-               mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
+               lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
 
                NG_BTSOCKET_L2CAP_INFO(
 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
@@ -1670,9 +1674,9 @@ ng_btsocket_l2cap_input(void *context, int pending)
        hook_p  hook = NULL;
 
        for (;;) {
-               mtx_lock(&ng_btsocket_l2cap_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_queue_lock, LK_EXCLUSIVE);
                NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_queue, item);
-               mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_queue_lock, LK_RELEASE);
 
                if (item == NULL)
                        break;
@@ -1724,6 +1728,7 @@ drop:
                        NG_HOOK_UNREF(hook);
 
                NG_FREE_ITEM(item);
+               ng_unref_item(item, 0);
        }
 } /* ng_btsocket_l2cap_input */
 
@@ -1738,15 +1743,15 @@ ng_btsocket_l2cap_rtclean(void *context, int pending)
        ng_btsocket_l2cap_pcb_p         pcb = NULL, pcb_next = NULL;
        ng_btsocket_l2cap_rtentry_p     rt = NULL;
 
-       mtx_lock(&ng_btsocket_l2cap_rt_mtx);
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_rt_lock, LK_EXCLUSIVE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        /*
         * First disconnect all sockets that use "invalid" hook
         */
 
        for (pcb = LIST_FIRST(&ng_btsocket_l2cap_sockets); pcb != NULL; ) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
                pcb_next = LIST_NEXT(pcb, next);
 
                if (pcb->rt != NULL &&
@@ -1763,7 +1768,7 @@ ng_btsocket_l2cap_rtclean(void *context, int pending)
                        pcb->rt = NULL;
                }
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                pcb = pcb_next;
        }
 
@@ -1787,8 +1792,8 @@ ng_btsocket_l2cap_rtclean(void *context, int pending)
                rt = rt_next;
        }
 
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
 } /* ng_btsocket_l2cap_rtclean */
 
 /*
@@ -1837,20 +1842,20 @@ ng_btsocket_l2cap_init(void)
 
        /* Create input queue */
        NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_queue, ifqmaxlen);
-       mtx_init(&ng_btsocket_l2cap_queue_mtx,
-               "btsocks_l2cap_queue_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_l2cap_queue_lock,
+               "btsocks_l2cap_queue_lock", 0, 0);
        TASK_INIT(&ng_btsocket_l2cap_queue_task, 0,
                ng_btsocket_l2cap_input, NULL);
 
        /* Create list of sockets */
        LIST_INIT(&ng_btsocket_l2cap_sockets);
-       mtx_init(&ng_btsocket_l2cap_sockets_mtx,
-               "btsocks_l2cap_sockets_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_l2cap_sockets_lock,
+               "btsocks_l2cap_sockets_lock", 0, 0);
 
        /* Routing table */
        LIST_INIT(&ng_btsocket_l2cap_rt);
-       mtx_init(&ng_btsocket_l2cap_rt_mtx,
-               "btsocks_l2cap_rt_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_l2cap_rt_lock,
+               "btsocks_l2cap_rt_lock", 0, 0);
        TASK_INIT(&ng_btsocket_l2cap_rt_task, 0,
                ng_btsocket_l2cap_rtclean, NULL);
 } /* ng_btsocket_l2cap_init */
@@ -1860,73 +1865,97 @@ ng_btsocket_l2cap_init(void)
  */
 
 void
-ng_btsocket_l2cap_abort(struct socket *so)
+ng_btsocket_l2cap_abort(netmsg_t msg)
 {
+       struct socket           *so = msg->abort.base.nm_so;
+
        so->so_error = ECONNABORTED;
 
-       (void)ng_btsocket_l2cap_disconnect(so);
+       ng_btsocket_l2cap_disconnect(msg);
 } /* ng_btsocket_l2cap_abort */
 
+#if 0 /* XXX */
 void
 ng_btsocket_l2cap_close(struct socket *so)
 {
 
        (void)ng_btsocket_l2cap_disconnect(so);
 } /* ng_btsocket_l2cap_close */
+#endif
 
 /*
  * Accept connection on socket. Nothing to do here, socket must be connected
  * and ready, so just return peer address and be done with it.
  */
 
-int
-ng_btsocket_l2cap_accept(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_l2cap_accept(netmsg_t msg)
 {
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EINVAL);
+       int                     error = 0;
+
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+
+       ng_btsocket_l2cap_peeraddr(msg);
+       return;
 
-       return (ng_btsocket_l2cap_peeraddr(so, nam));
+out:
+       lwkt_replymsg(&msg->accept.base.lmsg, error);
 } /* ng_btsocket_l2cap_accept */
 
 /*
  * Create and attach new socket
  */
 
-int
-ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td)
+void
+ng_btsocket_l2cap_attach(netmsg_t msg)
 {
-       static u_int32_t        token = 0;
-       ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
-       int                     error;
+       struct socket           *so = msg->attach.base.nm_so;
+       int                      proto = msg->attach.nm_proto;
+       static u_int32_t         token = 0;
+       ng_btsocket_l2cap_pcb_p  pcb = so2l2cap_pcb(so);
+       int                      error = 0;
 
        /* Check socket and protocol */
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EPROTONOSUPPORT);
-       if (so->so_type != SOCK_SEQPACKET)
-               return (ESOCKTNOSUPPORT);
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EPROTONOSUPPORT;
+               goto out;
+       }
+       if (so->so_type != SOCK_SEQPACKET) {
+               error = ESOCKTNOSUPPORT;
+               goto out;
+       }
 
 #if 0 /* XXX sonewconn() calls "pru_attach" with proto == 0 */
        if (proto != 0) 
-               if (proto != BLUETOOTH_PROTO_L2CAP)
-                       return (EPROTONOSUPPORT);
+               if (proto != BLUETOOTH_PROTO_L2CAP) {
+                       error = EPROTONOSUPPORT;
+                       goto out;
+               }
 #endif /* XXX */
 
-       if (pcb != NULL)
-               return (EISCONN);
+       if (pcb != NULL) {
+               error = EISCONN;
+               goto out;
+       }
 
        /* Reserve send and receive space if it is not reserved yet */
-       if ((so->so_snd.sb_hiwat == 0) || (so->so_rcv.sb_hiwat == 0)) {
+       if ((so->so_snd.ssb_hiwat == 0) || (so->so_rcv.ssb_hiwat == 0)) {
                error = soreserve(so, NG_BTSOCKET_L2CAP_SENDSPACE,
-                                       NG_BTSOCKET_L2CAP_RECVSPACE);
+                                       NG_BTSOCKET_L2CAP_RECVSPACE, NULL);
                if (error != 0)
-                       return (error);
+                       goto out;
        }
 
        /* Allocate the PCB */
         pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_L2CAP,
                      M_WAITOK | M_NULLOK | M_ZERO);
-        if (pcb == NULL)
-                return (ENOMEM);
+        if (pcb == NULL) {
+               error = ENOMEM;
+               goto out;
+       }
 
        /* Link the PCB and the socket */
        so->so_pcb = (caddr_t) pcb;
@@ -1950,7 +1979,7 @@ ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td)
        pcb->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT;
        pcb->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT;
 
-       callout_handle_init(&pcb->timo);
+       callout_init_mp(&pcb->timo);
 
        /*
         * XXX Mark PCB mutex as DUPOK to prevent "duplicated lock of
@@ -1959,8 +1988,7 @@ ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td)
         * for "old" (accepting) PCB and "new" (created) PCB.
         */
                
-       mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_pcb_mtx", NULL,
-               MTX_DEF|MTX_DUPOK);
+       lockinit(&pcb->pcb_lock, "btsocks_l2cap_pcb_lock", 0, LK_CANRECURSE);
 
         /*
         * Add the PCB to the list
@@ -1981,10 +2009,10 @@ ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td)
         * to figure out if we were called from socket() or from sonewconn().
         */
 
-       if (td != NULL)
-               mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       if (proto != 0)
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
        else
-               mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
+               KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
        
        /* Set PCB token. Use ng_btsocket_l2cap_sockets_mtx for protection */
        if (++ token == 0)
@@ -1994,34 +2022,44 @@ ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td)
 
        LIST_INSERT_HEAD(&ng_btsocket_l2cap_sockets, pcb, next);
 
-       if (td != NULL)
-               mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       if (proto != 0)
+               lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
-        return (0);
+out:
+       lwkt_replymsg(&msg->attach.base.lmsg, error);
 } /* ng_btsocket_l2cap_attach */
 
 /*
  * Bind socket
  */
 
-int
-ng_btsocket_l2cap_bind(struct socket *so, struct sockaddr *nam, 
-               struct thread *td)
+void
+ng_btsocket_l2cap_bind(netmsg_t msg)
 {
+       struct socket           *so = msg->bind.base.nm_so;
+       struct sockaddr         *nam = msg->bind.nm_nam;
        ng_btsocket_l2cap_pcb_t *pcb = NULL;
        struct sockaddr_l2cap   *sa = (struct sockaddr_l2cap *) nam;
        int                      psm, error = 0;
 
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EINVAL);
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        /* Verify address */
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->l2cap_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->l2cap_len != sizeof(*sa))
-               return (EINVAL);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->l2cap_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->l2cap_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
 
        psm = le16toh(sa->l2cap_psm);
 
@@ -2033,7 +2071,7 @@ ng_btsocket_l2cap_bind(struct socket *so, struct sockaddr *nam,
         * channels with the same PSM between the same pair of BD_ADDR'es.
         */
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
 
        LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next)
                if (psm != 0 && psm == pcb->psm &&
@@ -2051,44 +2089,62 @@ ng_btsocket_l2cap_bind(struct socket *so, struct sockaddr *nam,
        } else
                error = EADDRINUSE;
 
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->bind.base.lmsg, error);
 } /* ng_btsocket_l2cap_bind */
 
 /*
  * Connect socket
  */
 
-int
-ng_btsocket_l2cap_connect(struct socket *so, struct sockaddr *nam, 
-               struct thread *td)
+void
+ng_btsocket_l2cap_connect(netmsg_t msg)
 {
+       struct socket                   *so = msg->connect.base.nm_so;
+       struct sockaddr                 *nam = msg->connect.nm_nam;
        ng_btsocket_l2cap_pcb_t         *pcb = so2l2cap_pcb(so);
        struct sockaddr_l2cap           *sa = (struct sockaddr_l2cap *) nam;
        ng_btsocket_l2cap_rtentry_t     *rt = NULL;
        int                              have_src, error = 0;
 
        /* Check socket */
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EINVAL);
-       if (pcb->state == NG_BTSOCKET_L2CAP_CONNECTING)
-               return (EINPROGRESS);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (pcb->state == NG_BTSOCKET_L2CAP_CONNECTING) {
+               error = EINPROGRESS;
+               goto out;
+       }
 
        /* Verify address */
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->l2cap_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->l2cap_len != sizeof(*sa))
-               return (EINVAL);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->l2cap_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->l2cap_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
        if (sa->l2cap_psm == 0 ||
-           bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
-               return (EDESTADDRREQ);
-       if (pcb->psm != 0 && pcb->psm != le16toh(sa->l2cap_psm))
-               return (EINVAL);
+           bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0) {
+               error = EDESTADDRREQ;
+               goto out;
+       }
+       if (pcb->psm != 0 && pcb->psm != le16toh(sa->l2cap_psm)) {
+               error = EINVAL;
+               goto out;
+       }
 
        /*
         * Routing. Socket should be bound to some source address. The source
@@ -2097,9 +2153,9 @@ ng_btsocket_l2cap_connect(struct socket *so, struct sockaddr *nam,
         * src != dst.
         */
 
-       mtx_lock(&ng_btsocket_l2cap_rt_mtx);
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&ng_btsocket_l2cap_rt_lock, LK_EXCLUSIVE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* Send destination address and PSM */
        bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
@@ -2145,44 +2201,50 @@ ng_btsocket_l2cap_connect(struct socket *so, struct sockaddr *nam,
                }
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->connect.base.lmsg, error);
 } /* ng_btsocket_l2cap_connect */
 
 /*
  * Process ioctl's calls on socket
  */
 
-int
-ng_btsocket_l2cap_control(struct socket *so, u_long cmd, caddr_t data,
-               struct ifnet *ifp, struct thread *td)
+void
+ng_btsocket_l2cap_control(netmsg_t msg)
 {
-       return (EINVAL);
+       lwkt_replymsg(&msg->control.base.lmsg, EINVAL);
 } /* ng_btsocket_l2cap_control */
 
 /*
  * Process getsockopt/setsockopt system calls
  */
 
-int
-ng_btsocket_l2cap_ctloutput(struct socket *so, struct sockopt *sopt)
+void
+ng_btsocket_l2cap_ctloutput(netmsg_t msg)
 {
-       ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
-       int                     error = 0;
-       ng_l2cap_cfg_opt_val_t  v;
+       struct socket           *so = msg->ctloutput.base.nm_so;
+       struct sockopt          *sopt = msg->ctloutput.nm_sopt;
+       ng_btsocket_l2cap_pcb_p  pcb = so2l2cap_pcb(so);
+       int                      error = 0;
+       ng_l2cap_cfg_opt_val_t   v;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        if (sopt->sopt_level != SOL_L2CAP)
-               return (0);
+               goto out;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        switch (sopt->sopt_dir) {
        case SOPT_GET:
@@ -2263,9 +2325,10 @@ ng_btsocket_l2cap_ctloutput(struct socket *so, struct sockopt *sopt)
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
 } /* ng_btsocket_l2cap_ctloutput */
 
 /*
@@ -2273,17 +2336,19 @@ ng_btsocket_l2cap_ctloutput(struct socket *so, struct sockopt *sopt)
  */
 
 void
-ng_btsocket_l2cap_detach(struct socket *so)
+ng_btsocket_l2cap_detach(netmsg_t msg)
 {
-       ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
+       struct socket           *so = msg->detach.base.nm_so;
+       ng_btsocket_l2cap_pcb_p  pcb = so2l2cap_pcb(so);
+       int                      error = 0;
 
        KASSERT(pcb != NULL, ("ng_btsocket_l2cap_detach: pcb == NULL"));
 
        if (ng_btsocket_l2cap_node == NULL) 
-               return;
+               goto out;
 
-       mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* XXX what to do with pending request? */
        if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
@@ -2298,37 +2363,46 @@ ng_btsocket_l2cap_detach(struct socket *so)
 
        LIST_REMOVE(pcb, next);
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
 
-       mtx_destroy(&pcb->pcb_mtx);
+       lockuninit(&pcb->pcb_lock);
        bzero(pcb, sizeof(*pcb));
        kfree(pcb, M_NETGRAPH_BTSOCKET_L2CAP);
 
        soisdisconnected(so);
        so->so_pcb = NULL;
+
+out:
+       lwkt_replymsg(&msg->detach.base.lmsg, error);
 } /* ng_btsocket_l2cap_detach */
 
 /*
  * Disconnect socket
  */
 
-int
-ng_btsocket_l2cap_disconnect(struct socket *so)
+void
+ng_btsocket_l2cap_disconnect(netmsg_t msg)
 {
-       ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
-       int                     error = 0;
+       struct socket           *so = msg->disconnect.base.nm_so;
+       ng_btsocket_l2cap_pcb_p  pcb = so2l2cap_pcb(so);
+       int                      error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        if (pcb->state == NG_BTSOCKET_L2CAP_DISCONNECTING) {
-               mtx_unlock(&pcb->pcb_mtx);
-               return (EINPROGRESS);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EINPROGRESS;
+               goto out;
        }
 
        if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
@@ -2347,25 +2421,30 @@ ng_btsocket_l2cap_disconnect(struct socket *so)
                /* XXX FIXME what to do if error != 0 */
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->disconnect.base.lmsg, error);
 } /* ng_btsocket_l2cap_disconnect */
 
 /*
  * Listen on socket
  */
 
-int
-ng_btsocket_l2cap_listen(struct socket *so, int backlog, struct thread *td)
+void
+ng_btsocket_l2cap_listen(netmsg_t msg)
 {
-       ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
-       int error;
+       struct socket           *so = msg->listen.base.nm_so;
+       struct thread           *td = msg->listen.nm_td;
+       int                      backlog = msg->listen.nm_flags; /* XXX */
+       ng_btsocket_l2cap_pcb_p  pcb = so2l2cap_pcb(so);
+       int                      error = 0;
 
-       SOCK_LOCK(so);
-       error = solisten_proto_check(so);
-       if (error != 0)
+       if (so->so_state &
+           (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) {
+               error = EINVAL;
                goto out;
+       }
        if (pcb == NULL) {
                error = EINVAL;
                goto out;
@@ -2378,45 +2457,58 @@ ng_btsocket_l2cap_listen(struct socket *so, int backlog, struct thread *td)
                error = EADDRNOTAVAIL;
                goto out;
        }
-       solisten_proto(so, backlog);
+       solisten(so, backlog, td);
+
 out:
-       SOCK_UNLOCK(so);
-       return (error);
+       lwkt_replymsg(&msg->listen.base.lmsg, error);
 } /* ng_btsocket_listen */
 
 /*
  * Get peer address
  */
 
-int
-ng_btsocket_l2cap_peeraddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_l2cap_peeraddr(netmsg_t msg)
 {
-       ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
-       struct sockaddr_l2cap   sa;
+       struct socket            *so = msg->peeraddr.base.nm_so;
+       struct sockaddr         **nam = msg->peeraddr.nm_nam;
+       ng_btsocket_l2cap_pcb_p   pcb = so2l2cap_pcb(so);
+       struct sockaddr_l2cap     sa;
+       int                       error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
        sa.l2cap_psm = htole16(pcb->psm);
        sa.l2cap_len = sizeof(sa);
        sa.l2cap_family = AF_BLUETOOTH;
 
-       *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
+       *nam = dup_sockaddr((struct sockaddr *) &sa);
+
+       if (*nam == NULL)
+               error = ENOMEM;
 
-       return ((*nam == NULL)? ENOMEM : 0);
+out:
+       lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
 } /* ng_btsocket_l2cap_peeraddr */
 
 /*
  * Send data to socket
  */
 
-int
-ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
-               struct sockaddr *nam, struct mbuf *control, struct thread *td)
+void
+ng_btsocket_l2cap_send(netmsg_t msg)
 {
+       struct socket           *so = msg->send.base.nm_so;
+       struct mbuf             *m = msg->send.nm_m;
+       struct mbuf             *control = msg->send.nm_control;
        ng_btsocket_l2cap_pcb_t *pcb = so2l2cap_pcb(so);
        int                      error = 0;
 
@@ -2431,11 +2523,11 @@ ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
                goto drop;
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* Make sure socket is connected */
        if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                error = ENOTCONN;
                goto drop;
        }
@@ -2443,7 +2535,7 @@ ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
        /* Check route */
        if (pcb->rt == NULL ||
            pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) {
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                error = ENETDOWN;
                goto drop;
        }
@@ -2453,7 +2545,7 @@ ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
                NG_BTSOCKET_L2CAP_ERR(
 "%s: Packet too big, len=%d, omtu=%d\n", __func__, m->m_pkthdr.len, pcb->omtu);
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                error = EMSGSIZE;
                goto drop;
        }
@@ -2465,7 +2557,7 @@ ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
         * L2CA_WRITE_RSP.
         */
 
-       sbappendrecord(&pcb->so->so_snd, m);
+       sbappendrecord(&pcb->so->so_snd.sb, m);
        m = NULL;
 
        if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
@@ -2473,15 +2565,15 @@ ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
                if (error == 0)
                        ng_btsocket_l2cap_timeout(pcb);
                else
-                       sbdroprecord(&pcb->so->so_snd); /* XXX */
+                       sbdroprecord(&pcb->so->so_snd.sb); /* XXX */
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 drop:
        NG_FREE_M(m); /* checks for != NULL */
        NG_FREE_M(control);
 
-       return (error);
+       lwkt_replymsg(&msg->send.base.lmsg, error);
 } /* ng_btsocket_l2cap_send */
 
 /*
@@ -2495,12 +2587,12 @@ ng_btsocket_l2cap_send2(ng_btsocket_l2cap_pcb_p pcb)
        ng_l2cap_l2ca_hdr_t     *hdr = NULL;
        int                      error = 0;
        
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
-       if (pcb->so->so_snd.sb_cc == 0)
+       if (pcb->so->so_snd.sb.sb_cc == 0)
                return (EINVAL); /* XXX */
 
-       m = m_dup(pcb->so->so_snd.sb_mb, MB_DONTWAIT);
+       m = m_dup(pcb->so->so_snd.sb.sb_mb, MB_DONTWAIT);
        if (m == NULL)
                return (ENOBUFS);
 
@@ -2541,25 +2633,36 @@ ng_btsocket_l2cap_send2(ng_btsocket_l2cap_pcb_p pcb)
  * Get socket address
  */
 
-int
-ng_btsocket_l2cap_sockaddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_l2cap_sockaddr(netmsg_t msg)
 {
-       ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
-       struct sockaddr_l2cap   sa;
+       struct socket            *so = msg->sockaddr.base.nm_so;
+       struct sockaddr         **nam = msg->sockaddr.nm_nam;
+       ng_btsocket_l2cap_pcb_p   pcb = so2l2cap_pcb(so);
+       struct sockaddr_l2cap     sa;
+       int                       error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
        sa.l2cap_psm = htole16(pcb->psm);
        sa.l2cap_len = sizeof(sa);
        sa.l2cap_family = AF_BLUETOOTH;
 
-       *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
+       *nam = dup_sockaddr((struct sockaddr *) &sa);
 
-       return ((*nam == NULL)? ENOMEM : 0);
+       if (*nam == NULL)
+               error = ENOMEM;
+
+out:
+       lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
 } /* ng_btsocket_l2cap_sockaddr */
 
 /*****************************************************************************
@@ -2578,7 +2681,7 @@ ng_btsocket_l2cap_pcb_by_addr(bdaddr_p bdaddr, int psm)
 {
        ng_btsocket_l2cap_pcb_p p = NULL, p1 = NULL;
 
-       mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
+       KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
 
        LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next) {
                if (p->so == NULL || !(p->so->so_options & SO_ACCEPTCONN) || 
@@ -2608,7 +2711,7 @@ ng_btsocket_l2cap_pcb_by_token(u_int32_t token)
        if (token == 0)
                return (NULL);
 
-       mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
+       KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
 
        LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
                if (p->token == token)
@@ -2627,7 +2730,7 @@ ng_btsocket_l2cap_pcb_by_cid(bdaddr_p src, int cid)
 {
        ng_btsocket_l2cap_pcb_p p = NULL;
 
-       mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
+       KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
 
        LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
                if (p->cid == cid && bcmp(src, &p->src, sizeof(p->src)) == 0)
@@ -2643,12 +2746,12 @@ ng_btsocket_l2cap_pcb_by_cid(bdaddr_p src, int cid)
 static void
 ng_btsocket_l2cap_timeout(ng_btsocket_l2cap_pcb_p pcb)
 {
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
                pcb->flags |= NG_BTSOCKET_L2CAP_TIMO;
-               pcb->timo = timeout(ng_btsocket_l2cap_process_timeout, pcb,
-                                       bluetooth_l2cap_ertx_timeout());
+               callout_reset(&pcb->timo, bluetooth_l2cap_ertx_timeout(),
+                   ng_btsocket_l2cap_process_timeout, pcb);
        } else
                KASSERT(0,
 ("%s: Duplicated socket timeout?!\n", __func__));
@@ -2661,10 +2764,10 @@ ng_btsocket_l2cap_timeout(ng_btsocket_l2cap_pcb_p pcb)
 static void
 ng_btsocket_l2cap_untimeout(ng_btsocket_l2cap_pcb_p pcb)
 {
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) {
-               untimeout(ng_btsocket_l2cap_process_timeout, pcb, pcb->timo);
+               callout_stop(&pcb->timo);
                pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
        } else
                KASSERT(0,
@@ -2680,7 +2783,7 @@ ng_btsocket_l2cap_process_timeout(void *xpcb)
 {
        ng_btsocket_l2cap_pcb_p pcb = (ng_btsocket_l2cap_pcb_p) xpcb;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
        pcb->so->so_error = ETIMEDOUT;
@@ -2699,7 +2802,7 @@ ng_btsocket_l2cap_process_timeout(void *xpcb)
 
        case NG_BTSOCKET_L2CAP_OPEN:
                /* Send timeout - drop packet and wakeup sender */
-               sbdroprecord(&pcb->so->so_snd);
+               sbdroprecord(&pcb->so->so_snd.sb);
                sowwakeup(pcb->so);
                break;
 
@@ -2715,7 +2818,7 @@ ng_btsocket_l2cap_process_timeout(void *xpcb)
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 } /* ng_btsocket_l2cap_process_timeout */
 
 /*
index f052009..37b4dfb 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/bitstring.h>
+#include <bitstring.h>         /* XXX */
 #include <sys/domain.h>
 #include <sys/errno.h>
 #include <sys/filedesc.h>
@@ -41,7 +41,6 @@
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
-#include <sys/mutex.h>
 #include <sys/priv.h>
 #include <sys/protosw.h>
 #include <sys/queue.h>
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 #include <sys/taskqueue.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/include/ng_btsocket.h"
-#include "bluetooth/include/ng_btsocket_l2cap.h"
+#include <sys/msgport2.h>
+#include <sys/refcount.h>
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/netgraph2.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/include/ng_btsocket.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_l2cap.h>
 
 /* MALLOC define */
 #ifdef NG_SEPARATE_MALLOC
@@ -108,14 +110,14 @@ static u_int32_t                          ng_btsocket_l2cap_raw_debug_level;
 static u_int32_t                               ng_btsocket_l2cap_raw_ioctl_timeout;
 static node_p                                  ng_btsocket_l2cap_raw_node;
 static struct ng_bt_itemq                      ng_btsocket_l2cap_raw_queue;
-static struct mtx                              ng_btsocket_l2cap_raw_queue_mtx;
+static struct lock                             ng_btsocket_l2cap_raw_queue_lock;
 static struct task                             ng_btsocket_l2cap_raw_queue_task;
 static LIST_HEAD(, ng_btsocket_l2cap_raw_pcb)  ng_btsocket_l2cap_raw_sockets;
-static struct mtx                              ng_btsocket_l2cap_raw_sockets_mtx;
+static struct lock                             ng_btsocket_l2cap_raw_sockets_lock;
 static u_int32_t                               ng_btsocket_l2cap_raw_token;
-static struct mtx                              ng_btsocket_l2cap_raw_token_mtx;
+static struct lock                             ng_btsocket_l2cap_raw_token_lock;
 static LIST_HEAD(, ng_btsocket_l2cap_rtentry)  ng_btsocket_l2cap_raw_rt;
-static struct mtx                              ng_btsocket_l2cap_raw_rt_mtx;
+static struct lock                             ng_btsocket_l2cap_raw_rt_lock;
 static struct task                             ng_btsocket_l2cap_raw_rt_task;
 
 /* Sysctl tree */
@@ -285,7 +287,7 @@ ng_btsocket_l2cap_raw_node_rcvmsg(node_p node, item_p item, hook_p hook)
                        }
                }
 
-               mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_raw_queue_lock, LK_EXCLUSIVE);
                if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_raw_queue)) {
                        NG_BTSOCKET_L2CAP_RAW_ERR(
 "%s: Input queue is full\n", __func__);
@@ -299,10 +301,11 @@ ng_btsocket_l2cap_raw_node_rcvmsg(node_p node, item_p item, hook_p hook)
                                NGI_SET_HOOK(item, hook);
                        }
 
+                       ng_ref_item(item);
                        NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_raw_queue, item);
                        error = ng_btsocket_l2cap_raw_wakeup_input_task();
                }
-               mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_raw_queue_lock, LK_RELEASE);
        } else {
                NG_FREE_ITEM(item);
                error = EINVAL;
@@ -341,9 +344,9 @@ ng_btsocket_l2cap_raw_input(void *context, int pending)
        struct ng_mesg  *msg = NULL;
 
        for (;;) {
-               mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_raw_queue_lock, LK_EXCLUSIVE);
                NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_raw_queue, item);
-               mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
+               lockmgr(&ng_btsocket_l2cap_raw_queue_lock, LK_RELEASE);
 
                if (item == NULL)
                        break;
@@ -354,6 +357,7 @@ ng_btsocket_l2cap_raw_input(void *context, int pending)
                NGI_GET_MSG(item, msg);
                NGI_GET_HOOK(item, hook);
                NG_FREE_ITEM(item);
+               ng_unref_item(item, 0);
 
                switch (msg->header.cmd) {
                case NGM_L2CAP_NODE_HOOK_INFO: {
@@ -378,12 +382,12 @@ ng_btsocket_l2cap_raw_input(void *context, int pending)
 
                                NG_HOOK_SET_PRIVATE(hook, rt);
 
-                               mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
+                               lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_EXCLUSIVE);
 
                                LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_rt, 
                                        rt, next);
                        } else
-                               mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
+                               lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_EXCLUSIVE);
                
                        bcopy(msg->data, &rt->src, sizeof(rt->src));
                        rt->hook = hook;
@@ -394,7 +398,7 @@ ng_btsocket_l2cap_raw_input(void *context, int pending)
                                rt->src.b[5], rt->src.b[4], rt->src.b[3],
                                rt->src.b[2], rt->src.b[1], rt->src.b[0]);
 
-                       mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
+                       lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_RELEASE);
                        } break;
 
                case NGM_L2CAP_NODE_GET_FLAGS:
@@ -406,23 +410,23 @@ ng_btsocket_l2cap_raw_input(void *context, int pending)
                case NGM_L2CAP_L2CA_GET_INFO: {
                        ng_btsocket_l2cap_raw_pcb_p     pcb = NULL;
 
-                       mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
+                       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_EXCLUSIVE);
 
                        LIST_FOREACH(pcb,&ng_btsocket_l2cap_raw_sockets,next) {
-                               mtx_lock(&pcb->pcb_mtx);
+                               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                                if (pcb->token == msg->header.token) {
                                        pcb->msg = msg;
                                        msg = NULL;
                                        wakeup(&pcb->msg);
-                                       mtx_unlock(&pcb->pcb_mtx);
+                                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
                                        break;
                                }
 
-                               mtx_unlock(&pcb->pcb_mtx);
+                               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                        }
 
-                       mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
+                       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_RELEASE);
                        } break;
 
                default:
@@ -453,10 +457,10 @@ ng_btsocket_l2cap_raw_rtclean(void *context, int pending)
         * First disconnect all sockets that use "invalid" hook
         */
 
-       mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_EXCLUSIVE);
 
        LIST_FOREACH(pcb, &ng_btsocket_l2cap_raw_sockets, next) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                if (pcb->rt != NULL &&
                    pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
@@ -467,16 +471,16 @@ ng_btsocket_l2cap_raw_rtclean(void *context, int pending)
                        pcb->rt = NULL;
                }
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
        }
 
-       mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_RELEASE);
 
        /*
         * Now cleanup routing table
         */
 
-       mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_EXCLUSIVE);
 
        for (rt = LIST_FIRST(&ng_btsocket_l2cap_raw_rt); rt != NULL; ) {
                ng_btsocket_l2cap_rtentry_p     rt_next = LIST_NEXT(rt, next);
@@ -494,7 +498,7 @@ ng_btsocket_l2cap_raw_rtclean(void *context, int pending)
                rt = rt_next;
        }
 
-       mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_RELEASE);
 } /* ng_btsocket_l2cap_raw_rtclean */
 
 /*
@@ -544,25 +548,25 @@ ng_btsocket_l2cap_raw_init(void)
 
        /* Create input queue */
        NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_raw_queue, ifqmaxlen);
-       mtx_init(&ng_btsocket_l2cap_raw_queue_mtx,
-               "btsocks_l2cap_raw_queue_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_l2cap_raw_queue_lock,
+               "btsocks_l2cap_raw_queue_lock", 0, 0);
        TASK_INIT(&ng_btsocket_l2cap_raw_queue_task, 0,
                ng_btsocket_l2cap_raw_input, NULL);
 
        /* Create list of sockets */
        LIST_INIT(&ng_btsocket_l2cap_raw_sockets);
-       mtx_init(&ng_btsocket_l2cap_raw_sockets_mtx,
-               "btsocks_l2cap_raw_sockets_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_l2cap_raw_sockets_lock,
+               "btsocks_l2cap_raw_sockets_lock", 0, 0);
 
        /* Tokens */
        ng_btsocket_l2cap_raw_token = 0;
-       mtx_init(&ng_btsocket_l2cap_raw_token_mtx,
-               "btsocks_l2cap_raw_token_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_l2cap_raw_token_lock,
+               "btsocks_l2cap_raw_token_lock", 0, 0);
 
        /* Routing table */
        LIST_INIT(&ng_btsocket_l2cap_raw_rt);
-       mtx_init(&ng_btsocket_l2cap_raw_rt_mtx,
-               "btsocks_l2cap_raw_rt_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_l2cap_raw_rt_lock,
+               "btsocks_l2cap_raw_rt_lock", 0, 0);
        TASK_INIT(&ng_btsocket_l2cap_raw_rt_task, 0,
                ng_btsocket_l2cap_raw_rtclean, NULL);
 } /* ng_btsocket_l2cap_raw_init */
@@ -572,93 +576,118 @@ ng_btsocket_l2cap_raw_init(void)
  */
 
 void
-ng_btsocket_l2cap_raw_abort(struct socket *so)
+ng_btsocket_l2cap_raw_abort(netmsg_t msg)
 {
 
-       (void)ng_btsocket_l2cap_raw_disconnect(so);
+       (void)ng_btsocket_l2cap_raw_disconnect(msg);
 } /* ng_btsocket_l2cap_raw_abort */
 
+#if 0 /* XXX */
 void
 ng_btsocket_l2cap_raw_close(struct socket *so)
 {
 
        (void)ng_btsocket_l2cap_raw_disconnect(so);
 } /* ng_btsocket_l2cap_raw_close */
+#endif
 
 /*
  * Create and attach new socket
  */
 
-int
-ng_btsocket_l2cap_raw_attach(struct socket *so, int proto, struct thread *td)
+void
+ng_btsocket_l2cap_raw_attach(netmsg_t msg)
 {
-       ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
-       int                             error;
+       struct socket                   *so = msg->attach.base.nm_so;
+       ng_btsocket_l2cap_raw_pcb_p      pcb = so2l2cap_raw_pcb(so);
+       int                              error = 0;
 
-       if (pcb != NULL)
-               return (EISCONN);
+       if (pcb != NULL) {
+               error = EISCONN;
+               goto out;
+       }
 
-       if (ng_btsocket_l2cap_raw_node == NULL) 
-               return (EPROTONOSUPPORT);
-       if (so->so_type != SOCK_RAW)
-               return (ESOCKTNOSUPPORT);
+       if (ng_btsocket_l2cap_raw_node == NULL) {
+               error = EPROTONOSUPPORT;
+               goto out;
+       }
+       if (so->so_type != SOCK_RAW) {
+               error = ESOCKTNOSUPPORT;
+               goto out;
+       }
 
        /* Reserve send and receive space if it is not reserved yet */
        error = soreserve(so, NG_BTSOCKET_L2CAP_RAW_SENDSPACE,
-                       NG_BTSOCKET_L2CAP_RAW_RECVSPACE);
+                       NG_BTSOCKET_L2CAP_RAW_RECVSPACE, NULL);
        if (error != 0)
-               return (error);
+               goto out;
 
        /* Allocate the PCB */
         pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_L2CAP_RAW,
                      M_WAITOK | M_NULLOK | M_ZERO);
-        if (pcb == NULL)
-                return (ENOMEM);
+        if (pcb == NULL) {
+               error = ENOMEM;
+               goto out;
+       }
 
        /* Link the PCB and the socket */
        so->so_pcb = (caddr_t) pcb;
        pcb->so = so;
 
-       if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
+       if (curproc == NULL ||
+           priv_check(curthread, PRIV_NETBLUETOOTH_RAW) == 0)
                pcb->flags |= NG_BTSOCKET_L2CAP_RAW_PRIVILEGED;
 
-       mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_raw_pcb_mtx", NULL, MTX_DEF);
+       lockinit(&pcb->pcb_lock, "btsocks_l2cap_raw_pcb_lock", 0, 0);
 
         /* Add the PCB to the list */
-       mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_EXCLUSIVE);
        LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_sockets, pcb, next);
-       mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_RELEASE);
 
-        return (0);
+out:
+       lwkt_replymsg(&msg->attach.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_attach */
 
 /*
  * Bind socket
  */
 
-int
-ng_btsocket_l2cap_raw_bind(struct socket *so, struct sockaddr *nam, 
-               struct thread *td)
+void
+ng_btsocket_l2cap_raw_bind(netmsg_t msg)
 {
+       struct socket                   *so = msg->bind.base.nm_so;
+       struct sockaddr                 *nam = msg->bind.nm_nam;
        ng_btsocket_l2cap_raw_pcb_t     *pcb = so2l2cap_raw_pcb(so);
        struct sockaddr_l2cap           *sa = (struct sockaddr_l2cap *) nam;
        ng_btsocket_l2cap_rtentry_t     *rt = NULL;
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_raw_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->l2cap_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->l2cap_len != sizeof(*sa))
-               return (EINVAL);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->l2cap_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->l2cap_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
 
        if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY,
                                sizeof(sa->l2cap_bdaddr)) != 0) {
-               mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
+               lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_EXCLUSIVE);
 
                LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
                        if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
@@ -669,56 +698,72 @@ ng_btsocket_l2cap_raw_bind(struct socket *so, struct sockaddr *nam,
                                break;
                }
 
-               mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
+               lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_RELEASE);
 
-               if (rt == NULL)
-                       return (ENETDOWN);
+               if (rt == NULL) {
+                       error = ENETDOWN;
+                       goto out;
+               }
        } else
                rt = NULL;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
        bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
        pcb->rt = rt;
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->bind.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_bind */
 
 /*
  * Connect socket
  */
 
-int
-ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam, 
-               struct thread *td)
+void
+ng_btsocket_l2cap_raw_connect(netmsg_t msg)
 {
+       struct socket                   *so = msg->connect.base.nm_so;
+       struct sockaddr                 *nam = msg->connect.nm_nam;
        ng_btsocket_l2cap_raw_pcb_t     *pcb = so2l2cap_raw_pcb(so);
        struct sockaddr_l2cap           *sa = (struct sockaddr_l2cap *) nam;
        ng_btsocket_l2cap_rtentry_t     *rt = NULL;
-       int                              error;
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_raw_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->l2cap_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->l2cap_len != sizeof(*sa))
-               return (EINVAL);
-       if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
-               return (EINVAL);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->l2cap_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->l2cap_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
+       if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
 
        if (bcmp(&pcb->src, &pcb->dst, sizeof(pcb->src)) == 0) {
-               mtx_unlock(&pcb->pcb_mtx);
-
-               return (EADDRNOTAVAIL);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EADDRNOTAVAIL;
+               goto out;
        }
 
        /*
@@ -727,16 +772,15 @@ ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam,
 
        if (pcb->rt != NULL) {
                soisconnected(so);
-               mtx_unlock(&pcb->pcb_mtx);
-
-               return (0);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               goto out;
        }
        
        /*
         * Find the first hook that does not match specified destination address
         */
 
-       mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_EXCLUSIVE);
 
        LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
                if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
@@ -751,46 +795,53 @@ ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam,
 
                pcb->rt = rt;
                bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
-
-               error = 0;
        } else
                error = ENETDOWN;
 
-       mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_rt_lock, LK_RELEASE);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return  (error);
+out:
+       lwkt_replymsg(&msg->connect.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_connect */
 
 /*
  * Process ioctl's calls on socket
  */
 
-int
-ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
-               struct ifnet *ifp, struct thread *td)
+void
+ng_btsocket_l2cap_raw_control(netmsg_t msg)
 {
+       struct socket                   *so = msg->control.base.nm_so;
+       u_long                           cmd = msg->control.nm_cmd;
+       caddr_t                          data = msg->control.nm_data;
        ng_btsocket_l2cap_raw_pcb_p      pcb = so2l2cap_raw_pcb(so);
-       struct ng_mesg                  *msg = NULL;
+       struct ng_mesg                  *ngmsg = NULL;
        int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* Check if we route info */
        if (pcb->rt == NULL) {
-               mtx_unlock(&pcb->pcb_mtx);
-               return (EHOSTUNREACH);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EHOSTUNREACH;
+               goto out;
        }
 
        /* Check if we have pending ioctl() */
        if (pcb->token != 0) {
-               mtx_unlock(&pcb->pcb_mtx);
-               return (EBUSY);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EBUSY;
+               goto out;
        }  
 
        switch (cmd) {
@@ -837,24 +888,24 @@ ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
                        break;
                }
 
-               NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_GET_CON_LIST,
+               NG_MKMESSAGE(ngmsg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_GET_CON_LIST,
                        0, M_WAITOK | M_NULLOK);
-               if (msg == NULL) {
+               if (ngmsg == NULL) {
                        error = ENOMEM;
                        break;
                }
-               ng_btsocket_l2cap_raw_get_token(&msg->header.token);
-               pcb->token = msg->header.token;
+               ng_btsocket_l2cap_raw_get_token(&ngmsg->header.token);
+               pcb->token = ngmsg->header.token;
                pcb->msg = NULL;
 
-               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
+               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, ngmsg,
                        pcb->rt->hook, 0);
                if (error != 0) {
                        pcb->token = 0;
                        break;
                }
 
-               error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
+               error = lksleep(&pcb->msg, &pcb->pcb_lock, PCATCH, "l2ctl",
                                ng_btsocket_l2cap_raw_ioctl_timeout * hz);
                pcb->token = 0;
 
@@ -892,24 +943,24 @@ ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
                        break;
                }
  
-               NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
+               NG_MKMESSAGE(ngmsg, NGM_L2CAP_COOKIE,
                        NGM_L2CAP_NODE_GET_CHAN_LIST, 0, M_WAITOK | M_NULLOK);
-               if (msg == NULL) {
+               if (ngmsg == NULL) {
                        error = ENOMEM;
                        break;
                }
-               ng_btsocket_l2cap_raw_get_token(&msg->header.token);
-               pcb->token = msg->header.token;
+               ng_btsocket_l2cap_raw_get_token(&ngmsg->header.token);
+               pcb->token = ngmsg->header.token;
                pcb->msg = NULL;
 
-               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
+               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, ngmsg,
                        pcb->rt->hook, 0);
                if (error != 0) {
                        pcb->token = 0;
                        break;
                }
 
-               error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
+               error = lksleep(&pcb->msg, &pcb->pcb_lock, PCATCH, "l2ctl",
                                ng_btsocket_l2cap_raw_ioctl_timeout * hz);
                pcb->token = 0;
 
@@ -951,38 +1002,38 @@ ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
                        break;
                }
 
-               NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
+               NG_MKMESSAGE(ngmsg, NGM_L2CAP_COOKIE,
                        NGM_L2CAP_L2CA_PING, sizeof(*ip) + p->echo_size,
                        M_WAITOK | M_NULLOK);
-               if (msg == NULL) {
+               if (ngmsg == NULL) {
                        error = ENOMEM;
                        break;
                }
-               ng_btsocket_l2cap_raw_get_token(&msg->header.token);
-               pcb->token = msg->header.token;
+               ng_btsocket_l2cap_raw_get_token(&ngmsg->header.token);
+               pcb->token = ngmsg->header.token;
                pcb->msg = NULL;
 
-               ip = (ng_l2cap_l2ca_ping_ip *)(msg->data);
+               ip = (ng_l2cap_l2ca_ping_ip *)(ngmsg->data);
                bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
                ip->echo_size = p->echo_size;
 
                if (ip->echo_size > 0) {
                        error = copyin(p->echo_data, ip + 1, p->echo_size);
                        if (error != 0) {
-                               NG_FREE_MSG(msg);
+                               NG_FREE_MSG(ngmsg);
                                pcb->token = 0;
                                break;
                        }
                }
 
-               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
+               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, ngmsg,
                        pcb->rt->hook, 0);
                if (error != 0) {
                        pcb->token = 0;
                        break;
                }
 
-               error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
+               error = lksleep(&pcb->msg, &pcb->pcb_lock, PCATCH, "l2ctl",
                                bluetooth_l2cap_rtx_timeout());
                pcb->token = 0;
 
@@ -1021,29 +1072,29 @@ ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
                        break;
                }
 
-               NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
+               NG_MKMESSAGE(ngmsg, NGM_L2CAP_COOKIE,
                        NGM_L2CAP_L2CA_GET_INFO, sizeof(*ip) + p->info_size,
                        M_WAITOK | M_NULLOK);
-               if (msg == NULL) {
+               if (ngmsg == NULL) {
                        error = ENOMEM;
                        break;
                }
-               ng_btsocket_l2cap_raw_get_token(&msg->header.token);
-               pcb->token = msg->header.token;
+               ng_btsocket_l2cap_raw_get_token(&ngmsg->header.token);
+               pcb->token = ngmsg->header.token;
                pcb->msg = NULL;
 
-               ip = (ng_l2cap_l2ca_get_info_ip *)(msg->data);
+               ip = (ng_l2cap_l2ca_get_info_ip *)(ngmsg->data);
                bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
                ip->info_type = p->info_type;
 
-               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
+               NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, ngmsg,
                        pcb->rt->hook, 0);
                if (error != 0) {
                        pcb->token = 0;
                        break;
                }
 
-               error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
+               error = lksleep(&pcb->msg, &pcb->pcb_lock, PCATCH, "l2ctl",
                                bluetooth_l2cap_rtx_timeout());
                pcb->token = 0;
 
@@ -1092,9 +1143,10 @@ ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->control.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_control */
 
 /*
@@ -1102,120 +1154,156 @@ ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
  */
 
 void
-ng_btsocket_l2cap_raw_detach(struct socket *so)
+ng_btsocket_l2cap_raw_detach(netmsg_t msg)
 {
-       ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
+       struct socket                   *so = msg->detach.base.nm_so;
+       ng_btsocket_l2cap_raw_pcb_p      pcb = so2l2cap_raw_pcb(so);
+       int                              error = 0;
 
        KASSERT(pcb != NULL, ("nt_btsocket_l2cap_raw_detach: pcb == NULL"));
        if (ng_btsocket_l2cap_raw_node == NULL) 
-               return;
+               goto out;
 
-       mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_EXCLUSIVE);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        LIST_REMOVE(pcb, next);
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_l2cap_raw_sockets_lock, LK_RELEASE);
 
-       mtx_destroy(&pcb->pcb_mtx);
+       lockuninit(&pcb->pcb_lock);
 
        bzero(pcb, sizeof(*pcb));
        kfree(pcb, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
 
        so->so_pcb = NULL;
+
+out:
+       lwkt_replymsg(&msg->detach.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_detach */
 
 /*
  * Disconnect socket
  */
 
-int
-ng_btsocket_l2cap_raw_disconnect(struct socket *so)
+void
+ng_btsocket_l2cap_raw_disconnect(netmsg_t msg)
 {
-       ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
+       struct socket                   *so = msg->disconnect.base.nm_so;
+       ng_btsocket_l2cap_raw_pcb_p      pcb = so2l2cap_raw_pcb(so);
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_raw_node == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
        pcb->rt = NULL;
        soisdisconnected(so);
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->disconnect.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_disconnect */
 
 /*
  * Get peer address
  */
 
-int
-ng_btsocket_l2cap_raw_peeraddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_l2cap_raw_peeraddr(netmsg_t msg)
 {
-       ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
-       struct sockaddr_l2cap           sa;
+       struct socket                    *so = msg->peeraddr.base.nm_so;
+       struct sockaddr                 **nam = msg->peeraddr.nm_nam;
+       ng_btsocket_l2cap_raw_pcb_p       pcb = so2l2cap_raw_pcb(so);
+       struct sockaddr_l2cap             sa;
+       int                               error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_raw_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
        bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
        sa.l2cap_psm = 0;
        sa.l2cap_len = sizeof(sa);
        sa.l2cap_family = AF_BLUETOOTH;
 
-       *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
+       *nam = dup_sockaddr((struct sockaddr *) &sa);
+
+       if (*nam == NULL)
+               error = ENOMEM;
 
-       return ((*nam == NULL)? ENOMEM : 0);
+out:
+       lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_peeraddr */
 
 /*
  * Send data to socket
  */
 
-int
-ng_btsocket_l2cap_raw_send(struct socket *so, int flags, struct mbuf *m,
-               struct sockaddr *nam, struct mbuf *control, struct thread *td)
+void
+ng_btsocket_l2cap_raw_send(netmsg_t msg)
 {
+       struct mbuf                     *control = msg->send.nm_control;
+       struct mbuf                     *m = msg->send.nm_m;
+
        NG_FREE_M(m); /* Checks for m != NULL */
        NG_FREE_M(control);
 
-       return (EOPNOTSUPP);
+       lwkt_replymsg(&msg->send.base.lmsg, EOPNOTSUPP);
 } /* ng_btsocket_l2cap_raw_send */
 
 /*
  * Get socket address
  */
 
-int
-ng_btsocket_l2cap_raw_sockaddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_l2cap_raw_sockaddr(netmsg_t msg)
 {
-       ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
-       struct sockaddr_l2cap           sa;
+       struct socket                    *so = msg->sockaddr.base.nm_so;
+       struct sockaddr                 **nam = msg->sockaddr.nm_nam;
+       ng_btsocket_l2cap_raw_pcb_p       pcb = so2l2cap_raw_pcb(so);
+       struct sockaddr_l2cap             sa;
+       int                               error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (ng_btsocket_l2cap_raw_node == NULL) 
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (ng_btsocket_l2cap_raw_node == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
        bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
        sa.l2cap_psm = 0;
        sa.l2cap_len = sizeof(sa);
        sa.l2cap_family = AF_BLUETOOTH;
 
-       *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
+       *nam = dup_sockaddr((struct sockaddr *) &sa);
+
+       if (*nam == NULL)
+               error = ENOMEM;
 
-       return ((*nam == NULL)? ENOMEM : 0);
+out:
+       lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
 } /* ng_btsocket_l2cap_raw_sockaddr */
 
 /*
@@ -1225,14 +1313,14 @@ ng_btsocket_l2cap_raw_sockaddr(struct socket *so, struct sockaddr **nam)
 static void
 ng_btsocket_l2cap_raw_get_token(u_int32_t *token)
 {
-       mtx_lock(&ng_btsocket_l2cap_raw_token_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_token_lock, LK_EXCLUSIVE);
   
        if (++ ng_btsocket_l2cap_raw_token == 0)
                ng_btsocket_l2cap_raw_token = 1;
  
        *token = ng_btsocket_l2cap_raw_token;
  
-       mtx_unlock(&ng_btsocket_l2cap_raw_token_mtx);
+       lockmgr(&ng_btsocket_l2cap_raw_token_lock, LK_RELEASE);
 } /* ng_btsocket_l2cap_raw_get_token */
 
 /*
@@ -1268,7 +1356,7 @@ ng_btsocket_l2cap_raw_send_sync_ngmsg(ng_btsocket_l2cap_raw_pcb_p pcb,
        struct ng_mesg  *msg = NULL;
        int              error = 0;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
        if (msg == NULL)
@@ -1285,7 +1373,7 @@ ng_btsocket_l2cap_raw_send_sync_ngmsg(ng_btsocket_l2cap_raw_pcb_p pcb,
                return (error);
        }
 
-       error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
+       error = lksleep(&pcb->msg, &pcb->pcb_lock, PCATCH, "l2ctl",
                        ng_btsocket_l2cap_raw_ioctl_timeout * hz);
        pcb->token = 0;
 
index 51cc86f..53658d6 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/bitstring.h>
+#include <bitstring.h>         /* XXX */
 #include <sys/domain.h>
 #include <sys/endian.h>
 #include <sys/errno.h>
@@ -42,7 +42,6 @@
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
-#include <sys/mutex.h>
 #include <sys/proc.h>
 #include <sys/protosw.h>
 #include <sys/queue.h>
 #include <sys/sysctl.h>
 #include <sys/taskqueue.h>
 #include <sys/uio.h>
-#include "ng_message.h"
-#include "netgraph.h"
-#include "bluetooth/include/ng_bluetooth.h"
-#include "bluetooth/include/ng_hci.h"
-#include "bluetooth/include/ng_l2cap.h"
-#include "bluetooth/include/ng_btsocket.h"
-#include "bluetooth/include/ng_btsocket_l2cap.h"
-#include "bluetooth/include/ng_btsocket_rfcomm.h"
+#include <sys/msgport2.h>
+#include <sys/fcntl.h>
+#include <sys/socketvar2.h>
+#include <net/netmsg2.h>
+#include <netgraph7/ng_message.h>
+#include <netgraph7/netgraph.h>
+#include <netgraph7/bluetooth/include/ng_bluetooth.h>
+#include <netgraph7/bluetooth/include/ng_hci.h>
+#include <netgraph7/bluetooth/include/ng_l2cap.h>
+#include <netgraph7/bluetooth/include/ng_btsocket.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_l2cap.h>
+#include <netgraph7/bluetooth/include/ng_btsocket_rfcomm.h>
 
 /* MALLOC define */
 #ifdef NG_SEPARATE_MALLOC
@@ -95,7 +98,7 @@ static void ng_btsocket_rfcomm_sessions_task
 static void ng_btsocket_rfcomm_session_task
        (ng_btsocket_rfcomm_session_p s);
 #define ng_btsocket_rfcomm_task_wakeup() \
-       taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_rfcomm_task)
+       taskqueue_enqueue(taskqueue_swi, &ng_btsocket_rfcomm_task)
 
 static ng_btsocket_rfcomm_pcb_p ng_btsocket_rfcomm_connect_ind
        (ng_btsocket_rfcomm_session_p s, int channel);
@@ -187,9 +190,9 @@ static u_int32_t                            ng_btsocket_rfcomm_debug_level;
 static u_int32_t                               ng_btsocket_rfcomm_timo;
 struct task                                    ng_btsocket_rfcomm_task;
 static LIST_HEAD(, ng_btsocket_rfcomm_session) ng_btsocket_rfcomm_sessions;
-static struct mtx                              ng_btsocket_rfcomm_sessions_mtx;
+static struct lock                             ng_btsocket_rfcomm_sessions_lock;
 static LIST_HEAD(, ng_btsocket_rfcomm_pcb)     ng_btsocket_rfcomm_sockets;
-static struct mtx                              ng_btsocket_rfcomm_sockets_mtx;
+static struct lock                             ng_btsocket_rfcomm_sockets_lock;
 
 /* Sysctl tree */
 SYSCTL_DECL(_net_bluetooth_rfcomm_sockets);
@@ -327,13 +330,13 @@ ng_btsocket_rfcomm_init(void)
 
        /* RFCOMM sessions list */
        LIST_INIT(&ng_btsocket_rfcomm_sessions);
-       mtx_init(&ng_btsocket_rfcomm_sessions_mtx,
-               "btsocks_rfcomm_sessions_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_rfcomm_sessions_lock,
+               "btsocks_rfcomm_sessions_lock", 0, 0);
 
        /* RFCOMM sockets list */
        LIST_INIT(&ng_btsocket_rfcomm_sockets);
-       mtx_init(&ng_btsocket_rfcomm_sockets_mtx,
-               "btsocks_rfcomm_sockets_mtx", NULL, MTX_DEF);
+       lockinit(&ng_btsocket_rfcomm_sockets_lock,
+               "btsocks_rfcomm_sockets_lock", 0, 0);
 } /* ng_btsocket_rfcomm_init */
 
 /*
@@ -341,67 +344,79 @@ ng_btsocket_rfcomm_init(void)
  */
 
 void
-ng_btsocket_rfcomm_abort(struct socket *so)
+ng_btsocket_rfcomm_abort(netmsg_t msg)
 {
+       struct socket                   *so = msg->abort.base.nm_so;
 
        so->so_error = ECONNABORTED;
-       (void)ng_btsocket_rfcomm_disconnect(so);
+       ng_btsocket_rfcomm_disconnect(msg);
 } /* ng_btsocket_rfcomm_abort */
 
+#if 0 /* XXX */
 void
 ng_btsocket_rfcomm_close(struct socket *so)
 {
 
        (void)ng_btsocket_rfcomm_disconnect(so);
 } /* ng_btsocket_rfcomm_close */
+#endif
 
 /*
  * Accept connection on socket. Nothing to do here, socket must be connected
  * and ready, so just return peer address and be done with it.
  */
 
-int
-ng_btsocket_rfcomm_accept(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_rfcomm_accept(netmsg_t msg)
 {
-       return (ng_btsocket_rfcomm_peeraddr(so, nam));
+       ng_btsocket_rfcomm_peeraddr(msg);
 } /* ng_btsocket_rfcomm_accept */
 
 /*
  * Create and attach new socket
  */
 
-int
-ng_btsocket_rfcomm_attach(struct socket *so, int proto, struct thread *td)
+void
+ng_btsocket_rfcomm_attach(netmsg_t msg)
 {
-       ng_btsocket_rfcomm_pcb_p        pcb = so2rfcomm_pcb(so);
-       int                             error;
+       struct socket                   *so = msg->attach.base.nm_so;
+       ng_btsocket_rfcomm_pcb_p         pcb = so2rfcomm_pcb(so);
+       int                              error = 0;
 
        /* Check socket and protocol */
-       if (so->so_type != SOCK_STREAM)
-               return (ESOCKTNOSUPPORT);
+       if (so->so_type != SOCK_STREAM) {
+               error = ESOCKTNOSUPPORT;
+               goto out;
+       }
 
 #if 0 /* XXX sonewconn() calls "pru_attach" with proto == 0 */
        if (proto != 0) 
-               if (proto != BLUETOOTH_PROTO_RFCOMM)
-                       return (EPROTONOSUPPORT);
+               if (proto != BLUETOOTH_PROTO_RFCOMM) {
+                       error = EPROTONOSUPPORT;
+                       goto out;
+               }
 #endif /* XXX */
 
-       if (pcb != NULL)
-               return (EISCONN);
+       if (pcb != NULL) {
+               error = EISCONN;
+               goto out;
+       }
 
        /* Reserve send and receive space if it is not reserved yet */
-       if ((so->so_snd.sb_hiwat == 0) || (so->so_rcv.sb_hiwat == 0)) {
+       if ((so->so_snd.ssb_hiwat == 0) || (so->so_rcv.ssb_hiwat == 0)) {
                error = soreserve(so, NG_BTSOCKET_RFCOMM_SENDSPACE,
-                                       NG_BTSOCKET_RFCOMM_RECVSPACE);
+                                       NG_BTSOCKET_RFCOMM_RECVSPACE, NULL);
                if (error != 0)
-                       return (error);
+                       goto out;
        }
 
        /* Allocate the PCB */
         pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_RFCOMM,
                      M_WAITOK | M_NULLOK | M_ZERO);
-        if (pcb == NULL)
-                return (ENOMEM);
+        if (pcb == NULL) {
+               error = ENOMEM;
+               goto out;
+       }
 
        /* Link the PCB and the socket */
        so->so_pcb = (caddr_t) pcb;
@@ -418,97 +433,126 @@ ng_btsocket_rfcomm_attach(struct socket *so, int proto, struct thread *td)
        pcb->tx_cred = 0;
        pcb->rx_cred = RFCOMM_DEFAULT_CREDITS;
 
-       mtx_init(&pcb->pcb_mtx, "btsocks_rfcomm_pcb_mtx", NULL, MTX_DEF);
-       callout_handle_init(&pcb->timo);
+       lockinit(&pcb->pcb_lock, "btsocks_rfcomm_pcb_lock", 0, 0);
+       callout_init_mp(&pcb->timo);
 
        /* Add the PCB to the list */
-       mtx_lock(&ng_btsocket_rfcomm_sockets_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_EXCLUSIVE);
        LIST_INSERT_HEAD(&ng_btsocket_rfcomm_sockets, pcb, next);
-       mtx_unlock(&ng_btsocket_rfcomm_sockets_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_RELEASE);
 
-        return (0);
+out:
+       lwkt_replymsg(&msg->attach.base.lmsg, error);
 } /* ng_btsocket_rfcomm_attach */
 
 /*
  * Bind socket
  */
 
-int
-ng_btsocket_rfcomm_bind(struct socket *so, struct sockaddr *nam, 
-               struct thread *td)
+void
+ng_btsocket_rfcomm_bind(netmsg_t msg)
 {
+       struct socket                   *so = msg->bind.base.nm_so;
+       struct sockaddr                 *nam = msg->bind.nm_nam;
        ng_btsocket_rfcomm_pcb_t        *pcb = so2rfcomm_pcb(so), *pcb1;
        struct sockaddr_rfcomm          *sa = (struct sockaddr_rfcomm *) nam;
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        /* Verify address */
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->rfcomm_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->rfcomm_len != sizeof(*sa))
-               return (EINVAL);
-       if (sa->rfcomm_channel > 30)
-               return (EINVAL);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->rfcomm_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->rfcomm_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->rfcomm_channel > 30) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        if (sa->rfcomm_channel != 0) {
-               mtx_lock(&ng_btsocket_rfcomm_sockets_mtx);
+               lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_EXCLUSIVE);
 
                LIST_FOREACH(pcb1, &ng_btsocket_rfcomm_sockets, next) {
                        if (pcb1->channel == sa->rfcomm_channel &&
                            bcmp(&pcb1->src, &sa->rfcomm_bdaddr,
                                        sizeof(pcb1->src)) == 0) {
-                               mtx_unlock(&ng_btsocket_rfcomm_sockets_mtx);
-                               mtx_unlock(&pcb->pcb_mtx);
+                               lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_RELEASE);
+                               lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-                               return (EADDRINUSE);
+                               error = EADDRINUSE;
+                               goto out;
                        }
                }
 
-               mtx_unlock(&ng_btsocket_rfcomm_sockets_mtx);
+               lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_RELEASE);
        }
 
        bcopy(&sa->rfcomm_bdaddr, &pcb->src, sizeof(pcb->src));
        pcb->channel = sa->rfcomm_channel;
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->bind.base.lmsg, error);
 } /* ng_btsocket_rfcomm_bind */
 
 /*
  * Connect socket
  */
 
-int
-ng_btsocket_rfcomm_connect(struct socket *so, struct sockaddr *nam, 
-               struct thread *td)
+void
+ng_btsocket_rfcomm_connect(netmsg_t msg)
 {
+       struct socket                   *so = msg->connect.base.nm_so;
+       struct sockaddr                 *nam = msg->connect.nm_nam;
+       struct thread                   *td = msg->connect.nm_td;
        ng_btsocket_rfcomm_pcb_t        *pcb = so2rfcomm_pcb(so);
        struct sockaddr_rfcomm          *sa = (struct sockaddr_rfcomm *) nam;
        ng_btsocket_rfcomm_session_t    *s = NULL;
        struct socket                   *l2so = NULL;
        int                              dlci, error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        /* Verify address */
-       if (sa == NULL)
-               return (EINVAL);
-       if (sa->rfcomm_family != AF_BLUETOOTH)
-               return (EAFNOSUPPORT);
-       if (sa->rfcomm_len != sizeof(*sa))
-               return (EINVAL);
-       if (sa->rfcomm_channel > 30)
-               return (EINVAL);
+       if (sa == NULL) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->rfcomm_family != AF_BLUETOOTH) {
+               error = EAFNOSUPPORT;
+               goto out;
+       }
+       if (sa->rfcomm_len != sizeof(*sa)) {
+               error = EINVAL;
+               goto out;
+       }
+       if (sa->rfcomm_channel > 30) {
+               error = EINVAL;
+               goto out;
+       }
        if (sa->rfcomm_channel == 0 ||
-           bcmp(&sa->rfcomm_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
-               return (EDESTADDRREQ);
+           bcmp(&sa->rfcomm_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0) {
+               error = EDESTADDRREQ;
+               goto out;
+       }
 
        /*
         * XXX FIXME - This is FUBAR. socreate() will call soalloc(1), i.e.
@@ -521,13 +565,13 @@ ng_btsocket_rfcomm_connect(struct socket *so, struct sockaddr *nam,
         */
 
        error = socreate(PF_BLUETOOTH, &l2so, SOCK_SEQPACKET,
-                       BLUETOOTH_PROTO_L2CAP, td->td_ucred, td);
+                       BLUETOOTH_PROTO_L2CAP, td);
 
        /* 
         * Look for session between "pcb->src" and "sa->rfcomm_bdaddr" (dst)
         */
 
-       mtx_lock(&ng_btsocket_rfcomm_sessions_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_EXCLUSIVE);
 
        s = ng_btsocket_rfcomm_session_by_addr(&pcb->src, &sa->rfcomm_bdaddr);
        if (s == NULL) {
@@ -538,36 +582,37 @@ ng_btsocket_rfcomm_connect(struct socket *so, struct sockaddr *nam,
                 */
 
                if (l2so == NULL) {
-                       mtx_unlock(&ng_btsocket_rfcomm_sessions_mtx);
-                       return (error);
+                       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_RELEASE);
+                       goto out;
                }
 
                error = ng_btsocket_rfcomm_session_create(&s, l2so,
                                &pcb->src, &sa->rfcomm_bdaddr, td);
                if (error != 0) {
-                       mtx_unlock(&ng_btsocket_rfcomm_sessions_mtx);
-                       soclose(l2so);
+                       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_RELEASE);
+                       soclose(l2so, FNONBLOCK);
 
-                       return (error);
+                       goto out;
                }
        } else if (l2so != NULL)
-               soclose(l2so); /* we don't need new L2CAP socket */
+               soclose(l2so, FNONBLOCK); /* we don't need new L2CAP socket */
 
        /*
         * Check if we already have the same DLCI the the same session
         */
 
-       mtx_lock(&s->session_mtx);
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&s->session_lock, LK_EXCLUSIVE);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        dlci = RFCOMM_MKDLCI(!INITIATOR(s), sa->rfcomm_channel);
 
        if (ng_btsocket_rfcomm_pcb_by_dlci(s, dlci) != NULL) {
-               mtx_unlock(&pcb->pcb_mtx);
-               mtx_unlock(&s->session_mtx);
-               mtx_unlock(&ng_btsocket_rfcomm_sessions_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               lockmgr(&s->session_lock, LK_RELEASE);
+               lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_RELEASE);
 
-               return (EBUSY);
+               error = EBUSY;
+               goto out;
        }
 
        /*
@@ -612,11 +657,12 @@ ng_btsocket_rfcomm_connect(struct socket *so, struct sockaddr *nam,
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
-       mtx_unlock(&s->session_mtx);
-       mtx_unlock(&ng_btsocket_rfcomm_sessions_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
+       lockmgr(&s->session_lock, LK_RELEASE);
+       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->connect.base.lmsg, error);
 } /* ng_btsocket_rfcomm_connect */
 
 /*
@@ -624,30 +670,33 @@ ng_btsocket_rfcomm_connect(struct socket *so, struct sockaddr *nam,
  * XXX FIXME this should provide interface to the RFCOMM multiplexor channel
  */
 
-int
-ng_btsocket_rfcomm_control(struct socket *so, u_long cmd, caddr_t data,
-               struct ifnet *ifp, struct thread *td)
+void
+ng_btsocket_rfcomm_control(netmsg_t msg)
 {
-       return (EINVAL);
+       lwkt_replymsg(&msg->control.base.lmsg, EINVAL);
 } /* ng_btsocket_rfcomm_control */
 
 /*
  * Process getsockopt/setsockopt system calls
  */
 
-int
-ng_btsocket_rfcomm_ctloutput(struct socket *so, struct sockopt *sopt)
+void
+ng_btsocket_rfcomm_ctloutput(netmsg_t msg)
 {
-       ng_btsocket_rfcomm_pcb_p                pcb = so2rfcomm_pcb(so);
-       struct ng_btsocket_rfcomm_fc_info       fcinfo;
-       int                                     error = 0;
+       struct socket                           *so = msg->ctloutput.base.nm_so;
+       struct sockopt                          *sopt = msg->ctloutput.nm_sopt;
+       ng_btsocket_rfcomm_pcb_p                 pcb = so2rfcomm_pcb(so);
+       struct ng_btsocket_rfcomm_fc_info        fcinfo;
+       int                                      error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
        if (sopt->sopt_level != SOL_RFCOMM)
-               return (0);
+               goto out;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        switch (sopt->sopt_dir) {
        case SOPT_GET:
@@ -687,9 +736,10 @@ ng_btsocket_rfcomm_ctloutput(struct socket *so, struct sockopt *sopt)
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (error);
+out:
+       lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
 } /* ng_btsocket_rfcomm_ctloutput */
 
 /*
@@ -697,13 +747,15 @@ ng_btsocket_rfcomm_ctloutput(struct socket *so, struct sockopt *sopt)
  */
 
 void
-ng_btsocket_rfcomm_detach(struct socket *so)
+ng_btsocket_rfcomm_detach(netmsg_t msg)
 {
-       ng_btsocket_rfcomm_pcb_p        pcb = so2rfcomm_pcb(so);
+       struct socket                   *so = msg->detach.base.nm_so;
+       ng_btsocket_rfcomm_pcb_p         pcb = so2rfcomm_pcb(so);
+       int                              error = 0;
 
        KASSERT(pcb != NULL, ("ng_btsocket_rfcomm_detach: pcb == NULL"));
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        so->so_pcb = NULL;
 
@@ -730,7 +782,7 @@ ng_btsocket_rfcomm_detach(struct socket *so)
        }
        
        while (pcb->state != NG_BTSOCKET_RFCOMM_DLC_CLOSED)
-               msleep(&pcb->state, &pcb->pcb_mtx, PZERO, "rf_det", 0);
+               lksleep(&pcb->state, &pcb->pcb_lock, 0, "rf_det", 0);
 
        if (pcb->session != NULL)
                panic("%s: pcb->session != NULL", __func__);
@@ -738,37 +790,44 @@ ng_btsocket_rfcomm_detach(struct socket *so)
                panic("%s: timeout on closed DLC, flags=%#x",
                        __func__, pcb->flags);
 
-       mtx_lock(&ng_btsocket_rfcomm_sockets_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_EXCLUSIVE);
        LIST_REMOVE(pcb, next);
-       mtx_unlock(&ng_btsocket_rfcomm_sockets_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_RELEASE);
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       mtx_destroy(&pcb->pcb_mtx);
+       lockuninit(&pcb->pcb_lock);
        bzero(pcb, sizeof(*pcb));
        kfree(pcb, M_NETGRAPH_BTSOCKET_RFCOMM);
 
        soisdisconnected(so);
        sofree(so);             /* for so_pcb = NULL */
+
+       lwkt_replymsg(&msg->detach.base.lmsg, error);
 } /* ng_btsocket_rfcomm_detach */
 
 /*
  * Disconnect socket
  */
 
-int
-ng_btsocket_rfcomm_disconnect(struct socket *so)
+void
+ng_btsocket_rfcomm_disconnect(netmsg_t msg)
 {
-       ng_btsocket_rfcomm_pcb_p        pcb = so2rfcomm_pcb(so);
+       struct socket                   *so = msg->disconnect.base.nm_so;
+       ng_btsocket_rfcomm_pcb_p         pcb = so2rfcomm_pcb(so);
+       int                              error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        if (pcb->state == NG_BTSOCKET_RFCOMM_DLC_DISCONNECTING) {
-               mtx_unlock(&pcb->pcb_mtx);
-               return (EINPROGRESS);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
+               error = EINPROGRESS;
+               goto out;
        }
 
        /* XXX What to do with pending request? */
@@ -801,34 +860,42 @@ ng_btsocket_rfcomm_disconnect(struct socket *so)
                break;
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-       return (0);
+out:
+       lwkt_replymsg(&msg->disconnect.base.lmsg, error);
 } /* ng_btsocket_rfcomm_disconnect */
 
 /*
  * Listen on socket. First call to listen() will create listening RFCOMM session
  */
 
-int
-ng_btsocket_rfcomm_listen(struct socket *so, int backlog, struct thread *td)
+void
+ng_btsocket_rfcomm_listen(netmsg_t msg)
 {
+       struct socket                   *so = msg->listen.base.nm_so;
+       struct thread                   *td = msg->listen.nm_td;
+       int                              backlog = msg->listen.nm_flags; /* XXX */
        ng_btsocket_rfcomm_pcb_p         pcb = so2rfcomm_pcb(so), pcb1;
        ng_btsocket_rfcomm_session_p     s = NULL;
        struct socket                   *l2so = NULL;
-       int                              error, socreate_error, usedchannels;
+       int                              error = 0, socreate_error, usedchannels;
 
-       if (pcb == NULL)
-               return (EINVAL);
-       if (pcb->channel > 30)
-               return (EADDRNOTAVAIL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out2;
+       }
+       if (pcb->channel > 30) {
+               error = EADDRNOTAVAIL;
+               goto out2;
+       }
 
        usedchannels = 0;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        if (pcb->channel == 0) {
-               mtx_lock(&ng_btsocket_rfcomm_sockets_mtx);
+               lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_EXCLUSIVE);
 
                LIST_FOREACH(pcb1, &ng_btsocket_rfcomm_sockets, next)
                        if (pcb1->channel != 0 &&
@@ -840,16 +907,17 @@ ng_btsocket_rfcomm_listen(struct socket *so, int backlog, struct thread *td)
                                break;
 
                if (pcb->channel == 0) {
-                       mtx_unlock(&ng_btsocket_rfcomm_sockets_mtx);
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_RELEASE);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
-                       return (EADDRNOTAVAIL);
+                       error = EADDRNOTAVAIL;
+                       goto out2;
                }
 
-               mtx_unlock(&ng_btsocket_rfcomm_sockets_mtx);
+               lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_RELEASE);
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
        /*
         * XXX FIXME - This is FUBAR. socreate() will call soalloc(1), i.e.
@@ -862,18 +930,18 @@ ng_btsocket_rfcomm_listen(struct socket *so, int backlog, struct thread *td)
         */
 
        socreate_error = socreate(PF_BLUETOOTH, &l2so, SOCK_SEQPACKET,
-                       BLUETOOTH_PROTO_L2CAP, td->td_ucred, td);
+                       BLUETOOTH_PROTO_L2CAP, td);
 
        /*
         * Transition the socket and session into the LISTENING state.  Check
         * for collisions first, as there can only be one.
         */
-       mtx_lock(&ng_btsocket_rfcomm_sessions_mtx);
-       SOCK_LOCK(so);
-       error = solisten_proto_check(so);
-       SOCK_UNLOCK(so);
-       if (error != 0)
+       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_EXCLUSIVE);
+       if (so->so_state &
+           (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) {
+               error = EINVAL;
                goto out;
+       }
 
        LIST_FOREACH(s, &ng_btsocket_rfcomm_sessions, next)
                if (s->state == NG_BTSOCKET_RFCOMM_SESSION_LISTENING)
@@ -903,51 +971,63 @@ ng_btsocket_rfcomm_listen(struct socket *so, int backlog, struct thread *td)
                        goto out;
                l2so = NULL;
        }
-       SOCK_LOCK(so);
-       solisten_proto(so, backlog);
-       SOCK_UNLOCK(so);
+       solisten(so, backlog, td);
+
 out:
-       mtx_unlock(&ng_btsocket_rfcomm_sessions_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_RELEASE);
        /*
         * If we still have an l2so reference here, it's unneeded, so release
         * it.
         */
        if (l2so != NULL)
-               soclose(l2so);
-       return (error);
+               soclose(l2so, FNONBLOCK);
+
+out2:
+       lwkt_replymsg(&msg->listen.base.lmsg, error);
 } /* ng_btsocket_listen */
 
 /*
  * Get peer address
  */
 
-int
-ng_btsocket_rfcomm_peeraddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_rfcomm_peeraddr(netmsg_t msg)
 {
-       ng_btsocket_rfcomm_pcb_p        pcb = so2rfcomm_pcb(so);
-       struct sockaddr_rfcomm          sa;
+       struct socket                    *so = msg->peeraddr.base.nm_so;
+       struct sockaddr                 **nam = msg->peeraddr.nm_nam;
+       ng_btsocket_rfcomm_pcb_p          pcb = so2rfcomm_pcb(so);
+       struct sockaddr_rfcomm            sa;
+       int                               error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        bcopy(&pcb->dst, &sa.rfcomm_bdaddr, sizeof(sa.rfcomm_bdaddr));
        sa.rfcomm_channel = pcb->channel;
        sa.rfcomm_len = sizeof(sa);
        sa.rfcomm_family = AF_BLUETOOTH;
 
-       *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
+       *nam = dup_sockaddr((struct sockaddr *) &sa);
 
-       return ((*nam == NULL)? ENOMEM : 0);
+       if (*nam == NULL)
+               error = ENOMEM;
+
+out:
+       lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
 } /* ng_btsocket_rfcomm_peeraddr */
 
 /*
  * Send data to socket
  */
 
-int
-ng_btsocket_rfcomm_send(struct socket *so, int flags, struct mbuf *m,
-               struct sockaddr *nam, struct mbuf *control, struct thread *td)
+void
+ng_btsocket_rfcomm_send(netmsg_t msg)
 {
+       struct socket                   *so = msg->send.base.nm_so;
+       struct mbuf                     *m = msg->send.nm_m;
+       struct mbuf                     *control = msg->send.nm_control;
        ng_btsocket_rfcomm_pcb_t        *pcb = so2rfcomm_pcb(so);
        int                              error = 0;
 
@@ -957,17 +1037,17 @@ ng_btsocket_rfcomm_send(struct socket *so, int flags, struct mbuf *m,
                goto drop;
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* Make sure DLC is connected */
        if (pcb->state != NG_BTSOCKET_RFCOMM_DLC_CONNECTED) {
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                error = ENOTCONN;
                goto drop;
        }
 
        /* Put the packet on the socket's send queue and wakeup RFCOMM task */
-       sbappend(&pcb->so->so_snd, m);
+       sbappend(&pcb->so->so_snd.sb, m);
        m = NULL;
        
        if (!(pcb->flags & NG_BTSOCKET_RFCOMM_DLC_SENDING)) {
@@ -975,35 +1055,44 @@ ng_btsocket_rfcomm_send(struct socket *so, int flags, struct mbuf *m,
                error = ng_btsocket_rfcomm_task_wakeup();
        }
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 drop:
        NG_FREE_M(m); /* checks for != NULL */
        NG_FREE_M(control);
 
-       return (error);
+       lwkt_replymsg(&msg->send.base.lmsg, error);
 } /* ng_btsocket_rfcomm_send */
 
 /*
  * Get socket address
  */
 
-int
-ng_btsocket_rfcomm_sockaddr(struct socket *so, struct sockaddr **nam)
+void
+ng_btsocket_rfcomm_sockaddr(netmsg_t msg)
 {
-       ng_btsocket_rfcomm_pcb_p        pcb = so2rfcomm_pcb(so);
-       struct sockaddr_rfcomm          sa;
+       struct socket                    *so = msg->sockaddr.base.nm_so;
+       struct sockaddr                 **nam = msg->sockaddr.nm_nam;
+       ng_btsocket_rfcomm_pcb_p          pcb = so2rfcomm_pcb(so);
+       struct sockaddr_rfcomm            sa;
+       int                               error = 0;
 
-       if (pcb == NULL)
-               return (EINVAL);
+       if (pcb == NULL) {
+               error = EINVAL;
+               goto out;
+       }
 
        bcopy(&pcb->src, &sa.rfcomm_bdaddr, sizeof(sa.rfcomm_bdaddr));
        sa.rfcomm_channel = pcb->channel;
        sa.rfcomm_len = sizeof(sa);
        sa.rfcomm_family = AF_BLUETOOTH;
 
-       *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
+       *nam = dup_sockaddr((struct sockaddr *) &sa);
 
-       return ((*nam == NULL)? ENOMEM : 0);
+       if (*nam == NULL)
+               error = ENOMEM;
+
+out:
+       lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
 } /* ng_btsocket_rfcomm_sockaddr */
 
 /*
@@ -1033,10 +1122,10 @@ ng_btsocket_rfcomm_sessions_task(void *ctx, int pending)
 {
        ng_btsocket_rfcomm_session_p    s = NULL, s_next = NULL;
 
-       mtx_lock(&ng_btsocket_rfcomm_sessions_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_EXCLUSIVE);
 
        for (s = LIST_FIRST(&ng_btsocket_rfcomm_sessions); s != NULL; ) {
-               mtx_lock(&s->session_mtx);
+               lockmgr(&s->session_lock, LK_EXCLUSIVE);
                s_next = LIST_NEXT(s, next);
 
                ng_btsocket_rfcomm_session_task(s);
@@ -1052,26 +1141,22 @@ ng_btsocket_rfcomm_sessions_task(void *ctx, int pending)
                        /* Close L2CAP socket */
                        s->l2so->so_upcallarg = NULL;
                        s->l2so->so_upcall = NULL;
-                       SOCKBUF_LOCK(&s->l2so->so_rcv);
-                       s->l2so->so_rcv.sb_flags &= ~SB_UPCALL;
-                       SOCKBUF_UNLOCK(&s->l2so->so_rcv);
-                       SOCKBUF_LOCK(&s->l2so->so_snd);
-                       s->l2so->so_snd.sb_flags &= ~SB_UPCALL;
-                       SOCKBUF_UNLOCK(&s->l2so->so_snd);
-                       soclose(s->l2so);
+                       atomic_clear_int(&s->l2so->so_rcv.ssb_flags, SSB_UPCALL);
+                       atomic_clear_int(&s->l2so->so_snd.ssb_flags, SSB_UPCALL);
+                       soclose(s->l2so, FNONBLOCK);
 
-                       mtx_unlock(&s->session_mtx);
+                       lockmgr(&s->session_lock, LK_RELEASE);
 
-                       mtx_destroy(&s->session_mtx);
+                       lockuninit(&s->session_lock);
                        bzero(s, sizeof(*s));
                        kfree(s, M_NETGRAPH_BTSOCKET_RFCOMM);
                } else
-                       mtx_unlock(&s->session_mtx);
+                       lockmgr(&s->session_lock, LK_RELEASE);
 
                s = s_next;
        }
 
-       mtx_unlock(&ng_btsocket_rfcomm_sessions_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sessions_lock, LK_RELEASE);
 } /* ng_btsocket_rfcomm_sessions_task */
 
 /*
@@ -1081,13 +1166,13 @@ ng_btsocket_rfcomm_sessions_task(void *ctx, int pending)
 static void
 ng_btsocket_rfcomm_session_task(ng_btsocket_rfcomm_session_p s)
 {
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
-       if (s->l2so->so_rcv.sb_state & SBS_CANTRCVMORE) {
+       if (s->l2so->so_state & SS_CANTRCVMORE) {
                NG_BTSOCKET_RFCOMM_INFO(
-"%s: L2CAP connection has been terminated, so=%p, so_state=%#x, so_count=%d, " \
+"%s: L2CAP connection has been terminated, so=%p, so_state=%#x, " \
 "state=%d, flags=%#x\n", __func__, s->l2so, s->l2so->so_state, 
-                       s->l2so->so_count, s->state, s->flags);
+                       s->state, s->flags);
 
                s->state = NG_BTSOCKET_RFCOMM_SESSION_CLOSED;
                ng_btsocket_rfcomm_session_clean(s);
@@ -1147,7 +1232,7 @@ ng_btsocket_rfcomm_connect_ind(ng_btsocket_rfcomm_session_p s, int channel)
        ng_btsocket_l2cap_pcb_p          l2pcb = NULL;
        struct socket                   *so1 = NULL;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /*
         * Try to find RFCOMM socket that listens on given source address 
@@ -1165,12 +1250,12 @@ ng_btsocket_rfcomm_connect_ind(ng_btsocket_rfcomm_session_p s, int channel)
         * and channel.
         */
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        if (pcb->so->so_qlen <= pcb->so->so_qlimit)
                so1 = sonewconn(pcb->so, 0);
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
        if (so1 == NULL)
                return (NULL);
@@ -1184,7 +1269,7 @@ ng_btsocket_rfcomm_connect_ind(ng_btsocket_rfcomm_session_p s, int channel)
        if (pcb1 == NULL)
                panic("%s: pcb1 == NULL", __func__);
 
-       mtx_lock(&pcb1->pcb_mtx);
+       lockmgr(&pcb1->pcb_lock, LK_EXCLUSIVE);
 
        bcopy(&l2pcb->src, &pcb1->src, sizeof(pcb1->src));
        bcopy(&l2pcb->dst, &pcb1->dst, sizeof(pcb1->dst));
@@ -1194,7 +1279,7 @@ ng_btsocket_rfcomm_connect_ind(ng_btsocket_rfcomm_session_p s, int channel)
        LIST_INSERT_HEAD(&s->dlcs, pcb1, session_next);
        pcb1->session = s;
                        
-       mtx_unlock(&pcb1->pcb_mtx);
+       lockmgr(&pcb1->pcb_lock, LK_RELEASE);
 
        return (pcb1);
 } /* ng_btsocket_rfcomm_connect_ind */
@@ -1209,7 +1294,7 @@ ng_btsocket_rfcomm_connect_cfm(ng_btsocket_rfcomm_session_p s)
        ng_btsocket_rfcomm_pcb_p        pcb = NULL, pcb_next = NULL;
        int                             error;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /*
         * Wake up all waiting sockets and send PN request for each of them. 
@@ -1220,7 +1305,7 @@ ng_btsocket_rfcomm_connect_cfm(ng_btsocket_rfcomm_session_p s)
         */
 
        for (pcb = LIST_FIRST(&s->dlcs); pcb != NULL; ) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
                pcb_next = LIST_NEXT(pcb, session_next);
 
                if (pcb->state == NG_BTSOCKET_RFCOMM_DLC_W4_CONNECT) {
@@ -1235,7 +1320,7 @@ ng_btsocket_rfcomm_connect_cfm(ng_btsocket_rfcomm_session_p s)
                                ng_btsocket_rfcomm_pcb_kill(pcb, error);
                }
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                pcb = pcb_next;
        }
 } /* ng_btsocket_rfcomm_connect_cfm */
@@ -1262,7 +1347,7 @@ ng_btsocket_rfcomm_session_create(ng_btsocket_rfcomm_session_p *sp,
        int                             error;
        u_int16_t                       mtu;
 
-       mtx_assert(&ng_btsocket_rfcomm_sessions_mtx, MA_OWNED);
+       KKASSERT(lockowned(&ng_btsocket_rfcomm_sessions_lock) != 0);
 
        /* Allocate the RFCOMM session */
         s = kmalloc(sizeof(*s), M_NETGRAPH_BTSOCKET_RFCOMM,
@@ -1283,24 +1368,19 @@ ng_btsocket_rfcomm_session_create(ng_btsocket_rfcomm_session_p *sp,
         * for "old" (accepting) session and "new" (created) session.
         */
 
-       mtx_init(&s->session_mtx, "btsocks_rfcomm_session_mtx", NULL,
-               MTX_DEF|MTX_DUPOK);
+       lockinit(&s->session_lock, "btsocks_rfcomm_session_lock", 0,
+           LK_CANRECURSE);
 
        LIST_INIT(&s->dlcs);
 
        /* Prepare L2CAP socket */
        l2so->so_upcallarg = NULL;
        l2so->so_upcall = ng_btsocket_rfcomm_upcall;
-       SOCKBUF_LOCK(&l2so->so_rcv);
-       l2so->so_rcv.sb_flags |= SB_UPCALL;
-       SOCKBUF_UNLOCK(&l2so->so_rcv);
-       SOCKBUF_LOCK(&l2so->so_snd);
-       l2so->so_snd.sb_flags |= SB_UPCALL;
-       SOCKBUF_UNLOCK(&l2so->so_snd);
-       sosetstate(l2so, SS_NBIO);
+       atomic_set_int(&l2so->so_rcv.ssb_flags, SSB_UPCALL);
+       atomic_set_int(&l2so->so_snd.ssb_flags, SSB_UPCALL);
        s->l2so = l2so;
 
-       mtx_lock(&s->session_mtx);
+       lockmgr(&s->session_lock, LK_EXCLUSIVE);
 
        /*
         * "src" == NULL and "dst" == NULL means just create session.
@@ -1365,25 +1445,20 @@ done:
        LIST_INSERT_HEAD(&ng_btsocket_rfcomm_sessions, s, next);
        *sp = s;
 
-       mtx_unlock(&s->session_mtx);
+       lockmgr(&s->session_lock, LK_RELEASE);
 
        return (0);
 
 bad:
-       mtx_unlock(&s->session_mtx);
+       lockmgr(&s->session_lock, LK_RELEASE);
 
        /* Return L2CAP socket back to its original state */
        l2so->so_upcallarg = NULL;
        l2so->so_upcall = NULL;
-       SOCKBUF_LOCK(&l2so->so_rcv);
-       l2so->so_rcv.sb_flags &= ~SB_UPCALL;
-       SOCKBUF_UNLOCK(&l2so->so_rcv);
-       SOCKBUF_LOCK(&l2so->so_snd);
-       l2so->so_snd.sb_flags &= ~SB_UPCALL;
-       SOCKBUF_UNLOCK(&l2so->so_snd);
-       soclrstate(l2so, SS_NBIO);
-
-       mtx_destroy(&s->session_mtx);
+       atomic_clear_int(&l2so->so_rcv.ssb_flags, SSB_UPCALL);
+       atomic_clear_int(&l2so->so_snd.ssb_flags, SSB_UPCALL);
+
+       lockuninit(&s->session_lock);
        bzero(s, sizeof(*s));
        kfree(s, M_NETGRAPH_BTSOCKET_RFCOMM);
 
@@ -1404,8 +1479,8 @@ ng_btsocket_rfcomm_session_accept(ng_btsocket_rfcomm_session_p s0)
        ng_btsocket_rfcomm_session_p     s = NULL;
        int                              error = 0;
 
-       mtx_assert(&ng_btsocket_rfcomm_sessions_mtx, MA_OWNED);
-       mtx_assert(&s0->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&ng_btsocket_rfcomm_sessions_lock) != 0);
+       KKASSERT(lockowned(&s0->session_lock) != 0);
 
        /* Check if there is a complete L2CAP connection in the queue */
        if ((error = s0->l2so->so_error) != 0) {
@@ -1416,10 +1491,8 @@ ng_btsocket_rfcomm_session_accept(ng_btsocket_rfcomm_session_p s0)
                return (error);
        }
 
-       ACCEPT_LOCK();
        if (TAILQ_EMPTY(&s0->l2so->so_comp)) {
-               ACCEPT_UNLOCK();
-               if (s0->l2so->so_rcv.sb_state & SBS_CANTRCVMORE)
+               if (s0->l2so->so_state & SS_CANTRCVMORE)
                        return (ECONNABORTED);
                return (EWOULDBLOCK);
        }
@@ -1431,19 +1504,14 @@ ng_btsocket_rfcomm_session_accept(ng_btsocket_rfcomm_session_p s0)
 
        TAILQ_REMOVE(&s0->l2so->so_comp, l2so, so_list);
        s0->l2so->so_qlen --;
-       l2so->so_qstate &= ~SQ_COMP;
        l2so->so_head = NULL;
-       SOCK_LOCK(l2so);
-       soref(l2so);
-       sosetstate(l2so, SS_NBIO);
-       SOCK_UNLOCK(l2so);
-       ACCEPT_UNLOCK();
+       soreference(l2so);
 
        error = soaccept(l2so, (struct sockaddr **) &l2sa);
        if (error != 0) {
                NG_BTSOCKET_RFCOMM_ERR(
 "%s: soaccept() on L2CAP socket failed, error=%d\n", __func__, error);
-               soclose(l2so);
+               soclose(l2so, FNONBLOCK);
 
                return (error);
        }
@@ -1462,7 +1530,7 @@ ng_btsocket_rfcomm_session_accept(ng_btsocket_rfcomm_session_p s0)
                error = ng_btsocket_rfcomm_session_create(&s, l2so, NULL, NULL,
                                curthread /* XXX */);
                if (error == 0) {
-                       mtx_lock(&s->session_mtx);
+                       lockmgr(&s->session_lock, LK_EXCLUSIVE);
 
                        s->flags = 0;
                        s->state = NG_BTSOCKET_RFCOMM_SESSION_CONNECTED;
@@ -1476,12 +1544,12 @@ ng_btsocket_rfcomm_session_accept(ng_btsocket_rfcomm_session_p s0)
                        s->mtu = min(l2pcb->imtu, l2pcb->omtu) -
                                        sizeof(struct rfcomm_frame_hdr) - 1 - 1;
 
-                       mtx_unlock(&s->session_mtx);
+                       lockmgr(&s->session_lock, LK_RELEASE);
                } else {
                        NG_BTSOCKET_RFCOMM_ALERT(
 "%s: Failed to create new RFCOMM session, error=%d\n", __func__, error);
 
-                       soclose(l2so);
+                       soclose(l2so, FNONBLOCK);
                }
        } else {
                NG_BTSOCKET_RFCOMM_WARN(
@@ -1494,7 +1562,7 @@ ng_btsocket_rfcomm_session_accept(ng_btsocket_rfcomm_session_p s0)
                        s->state, s->flags);
 
                error = EBUSY;
-               soclose(l2so);
+               soclose(l2so, FNONBLOCK);
        }
 
        return (error);
@@ -1511,7 +1579,7 @@ ng_btsocket_rfcomm_session_connect(ng_btsocket_rfcomm_session_p s)
        ng_btsocket_l2cap_pcb_p l2pcb = so2l2cap_pcb(s->l2so);
        int                     error;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /* First check if connection has failed */
        if ((error = s->l2so->so_error) != 0) {
@@ -1561,11 +1629,10 @@ ng_btsocket_rfcomm_session_connect(ng_btsocket_rfcomm_session_p s)
 static int
 ng_btsocket_rfcomm_session_receive(ng_btsocket_rfcomm_session_p s)
 {
-       struct mbuf     *m = NULL;
-       struct uio       uio;
-       int              more, flags, error;
+       struct sockbuf   sio;
+       int              more, error, rcvflg, savelen = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /* Can we read from the L2CAP socket? */
        if (!soreadable(s->l2so))
@@ -1590,15 +1657,12 @@ ng_btsocket_rfcomm_session_receive(ng_btsocket_rfcomm_session_p s)
         * May be s->mtu + sizeof(struct rfcomm_frame_hdr) + 1 + 1?
         */
 
+       sbinit(&sio, 0);
        for (more = 1; more; ) {
                /* Try to get next packet from socket */
-               bzero(&uio, sizeof(uio));
-/*             uio.uio_td = NULL; */
-               uio.uio_resid = 1000000000;
-               flags = MSG_DONTWAIT;
-
-               m = NULL;
-               error = soreceive(s->l2so, NULL, &uio, &m, NULL, &flags);
+               sbinit(&sio, savelen);
+               rcvflg = MSG_WAITALL;
+               error = soreceive(s->l2so, NULL, NULL, &sio, NULL, &rcvflg);
                if (error != 0) {
                        if (error == EWOULDBLOCK)
                                return (0); /* XXX can happen? */
@@ -1609,10 +1673,10 @@ ng_btsocket_rfcomm_session_receive(ng_btsocket_rfcomm_session_p s)
                        return (error);
                }
        
-               more = (m->m_nextpkt != NULL);
-               m->m_nextpkt = NULL;
+               more = (sio.sb_mb->m_nextpkt != NULL);
+               sio.sb_mb->m_nextpkt = NULL;
 
-               ng_btsocket_rfcomm_receive_frame(s, m);
+               ng_btsocket_rfcomm_receive_frame(s, sio.sb_mb);
        }
 
        return (0);
@@ -1628,8 +1692,9 @@ ng_btsocket_rfcomm_session_send(ng_btsocket_rfcomm_session_p s)
 {
        struct mbuf     *m = NULL;
        int              error;
+       struct netmsg_pru_send msg;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /* Send as much as we can from the session queue */
        while (sowriteable(s->l2so)) {
@@ -1649,8 +1714,14 @@ ng_btsocket_rfcomm_session_send(ng_btsocket_rfcomm_session_p s)
                        return (0); /* we are done */
 
                /* Call send function on the L2CAP socket */
-               error = (*s->l2so->so_proto->pr_usrreqs->pru_send)(s->l2so,
-                               0, m, NULL, NULL, curthread /* XXX */);
+               netmsg_init(&msg.base, s->l2so, &curthread->td_msgport,
+                   0, s->l2so->so_proto->pr_usrreqs->pru_send);
+               msg.nm_flags = 0;
+               msg.nm_m = m;
+               msg.nm_addr = NULL;
+               msg.nm_control = NULL;
+               msg.nm_td = curthread; /* XXX */
+               error = lwkt_domsg(s->l2so->so_port, &msg.base.lmsg, 0);
                if (error != 0) {
                        NG_BTSOCKET_RFCOMM_ERR(
 "%s: Could not send data to L2CAP socket, error=%d\n", __func__, error);
@@ -1673,7 +1744,7 @@ ng_btsocket_rfcomm_session_clean(ng_btsocket_rfcomm_session_p s)
        ng_btsocket_rfcomm_pcb_p        pcb = NULL, pcb_next = NULL;
        int                             error;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /*
         * Note: cannot use LIST_FOREACH because ng_btsocket_rfcomm_pcb_kill
@@ -1681,7 +1752,7 @@ ng_btsocket_rfcomm_session_clean(ng_btsocket_rfcomm_session_p s)
         */
 
        for (pcb = LIST_FIRST(&s->dlcs); pcb != NULL; ) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
                pcb_next = LIST_NEXT(pcb, session_next);
 
                NG_BTSOCKET_RFCOMM_INFO(
@@ -1695,7 +1766,7 @@ ng_btsocket_rfcomm_session_clean(ng_btsocket_rfcomm_session_p s)
 
                ng_btsocket_rfcomm_pcb_kill(pcb, error);
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                pcb = pcb_next;
        }
 } /* ng_btsocket_rfcomm_session_clean */
@@ -1710,7 +1781,7 @@ ng_btsocket_rfcomm_session_process_pcb(ng_btsocket_rfcomm_session_p s)
        ng_btsocket_rfcomm_pcb_p        pcb = NULL, pcb_next = NULL;
        int                             error;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /*
         * Note: cannot use LIST_FOREACH because ng_btsocket_rfcomm_pcb_kill
@@ -1718,7 +1789,7 @@ ng_btsocket_rfcomm_session_process_pcb(ng_btsocket_rfcomm_session_p s)
         */
 
        for (pcb = LIST_FIRST(&s->dlcs); pcb != NULL; ) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
                pcb_next = LIST_NEXT(pcb, session_next);
 
                switch (pcb->state) {
@@ -1791,7 +1862,7 @@ ng_btsocket_rfcomm_session_process_pcb(ng_btsocket_rfcomm_session_p s)
                        break;
                }
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
                pcb = pcb_next;
        }
 } /* ng_btsocket_rfcomm_session_process_pcb */
@@ -1808,7 +1879,7 @@ ng_btsocket_rfcomm_session_by_addr(bdaddr_p src, bdaddr_p dst)
        ng_btsocket_l2cap_pcb_p         l2pcb = NULL;
        int                             any_src;
 
-       mtx_assert(&ng_btsocket_rfcomm_sessions_mtx, MA_OWNED);
+       KKASSERT(lockowned(&ng_btsocket_rfcomm_sessions_lock) != 0);
 
        any_src = (bcmp(src, NG_HCI_BDADDR_ANY, sizeof(*src)) == 0);
 
@@ -1844,7 +1915,7 @@ ng_btsocket_rfcomm_receive_frame(ng_btsocket_rfcomm_session_p s,
        u_int8_t                 dlci, type;
        int                      error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /* Pullup as much as we can into first mbuf (for direct access) */
        length = min(m0->m_pkthdr.len, MHLEN);
@@ -1978,7 +2049,7 @@ ng_btsocket_rfcomm_receive_sabm(ng_btsocket_rfcomm_session_p s, int dlci)
        ng_btsocket_rfcomm_pcb_p        pcb = NULL;
        int                             error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got SABM, session state=%d, flags=%#x, mtu=%d, dlci=%d\n",
@@ -2027,13 +2098,13 @@ ng_btsocket_rfcomm_receive_sabm(ng_btsocket_rfcomm_session_p s, int dlci)
 
        pcb = ng_btsocket_rfcomm_pcb_by_dlci(s, dlci);
        if (pcb != NULL) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                if (pcb->state != NG_BTSOCKET_RFCOMM_DLC_CONNECTING) {
                        NG_BTSOCKET_RFCOMM_ERR(
 "%s: Got SABM for dlci=%d in invalid state=%d, flags=%#x\n",
                                __func__, dlci, pcb->state, pcb->flags);
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
                        return (ENOENT);
                }
@@ -2050,7 +2121,7 @@ ng_btsocket_rfcomm_receive_sabm(ng_btsocket_rfcomm_session_p s, int dlci)
                } else
                        ng_btsocket_rfcomm_pcb_kill(pcb, error);
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
 
                return (error);
        }
@@ -2062,7 +2133,7 @@ ng_btsocket_rfcomm_receive_sabm(ng_btsocket_rfcomm_session_p s, int dlci)
 
        pcb = ng_btsocket_rfcomm_connect_ind(s, RFCOMM_SRVCHANNEL(dlci));
        if (pcb != NULL) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                pcb->dlci = dlci;
 
@@ -2076,7 +2147,7 @@ ng_btsocket_rfcomm_receive_sabm(ng_btsocket_rfcomm_session_p s, int dlci)
                } else
                        ng_btsocket_rfcomm_pcb_kill(pcb, error);
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
        } else
                /* Nobody is listen()ing on the requested DLCI */
                error = ng_btsocket_rfcomm_send_command(s,RFCOMM_FRAME_DM,dlci);
@@ -2094,7 +2165,7 @@ ng_btsocket_rfcomm_receive_disc(ng_btsocket_rfcomm_session_p s, int dlci)
        ng_btsocket_rfcomm_pcb_p        pcb = NULL;
        int                             error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got DISC, session state=%d, flags=%#x, mtu=%d, dlci=%d\n",
@@ -2118,7 +2189,7 @@ ng_btsocket_rfcomm_receive_disc(ng_btsocket_rfcomm_session_p s, int dlci)
                if (pcb != NULL) {
                        int     err;
 
-                       mtx_lock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got DISC for dlci=%d, state=%d, flags=%#x\n",
@@ -2134,7 +2205,7 @@ ng_btsocket_rfcomm_receive_disc(ng_btsocket_rfcomm_session_p s, int dlci)
 
                        ng_btsocket_rfcomm_pcb_kill(pcb, err);
 
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
                } else {
                        NG_BTSOCKET_RFCOMM_WARN(
 "%s: Got DISC for non-existing dlci=%d\n", __func__, dlci);
@@ -2157,7 +2228,7 @@ ng_btsocket_rfcomm_receive_ua(ng_btsocket_rfcomm_session_p s, int dlci)
        ng_btsocket_rfcomm_pcb_p        pcb = NULL;
        int                             error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got UA, session state=%d, flags=%#x, mtu=%d, dlci=%d\n",
@@ -2191,7 +2262,7 @@ ng_btsocket_rfcomm_receive_ua(ng_btsocket_rfcomm_session_p s, int dlci)
        /* Check if we have this DLCI */
        pcb = ng_btsocket_rfcomm_pcb_by_dlci(s, dlci);
        if (pcb != NULL) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got UA for dlci=%d, state=%d, flags=%#x\n",
@@ -2220,7 +2291,7 @@ ng_btsocket_rfcomm_receive_ua(ng_btsocket_rfcomm_session_p s, int dlci)
                        break;
                }
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
        } else {
                NG_BTSOCKET_RFCOMM_WARN(
 "%s: Got UA for non-existing dlci=%d\n", __func__, dlci);
@@ -2241,7 +2312,7 @@ ng_btsocket_rfcomm_receive_dm(ng_btsocket_rfcomm_session_p s, int dlci)
        ng_btsocket_rfcomm_pcb_p        pcb = NULL;
        int                             error;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got DM, session state=%d, flags=%#x, mtu=%d, dlci=%d\n",
@@ -2255,7 +2326,7 @@ ng_btsocket_rfcomm_receive_dm(ng_btsocket_rfcomm_session_p s, int dlci)
        } else {
                pcb = ng_btsocket_rfcomm_pcb_by_dlci(s, dlci);
                if (pcb != NULL) {
-                       mtx_lock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got DM for dlci=%d, state=%d, flags=%#x\n",
@@ -2268,7 +2339,7 @@ ng_btsocket_rfcomm_receive_dm(ng_btsocket_rfcomm_session_p s, int dlci)
 
                        ng_btsocket_rfcomm_pcb_kill(pcb, error);
 
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
                } else
                        NG_BTSOCKET_RFCOMM_WARN(
 "%s: Got DM for non-existing dlci=%d\n", __func__, dlci);
@@ -2288,7 +2359,7 @@ ng_btsocket_rfcomm_receive_uih(ng_btsocket_rfcomm_session_p s, int dlci,
        ng_btsocket_rfcomm_pcb_p        pcb = NULL;
        int                             error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got UIH, session state=%d, flags=%#x, mtu=%d, dlci=%d, pf=%d, len=%d\n",
@@ -2312,7 +2383,7 @@ ng_btsocket_rfcomm_receive_uih(ng_btsocket_rfcomm_session_p s, int dlci,
                goto drop;
        }
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        /* Check dlci state */  
        if (pcb->state != NG_BTSOCKET_RFCOMM_DLC_CONNECTED) {
@@ -2371,7 +2442,7 @@ ng_btsocket_rfcomm_receive_uih(ng_btsocket_rfcomm_session_p s, int dlci,
                                pcb->mtu, m0->m_pkthdr.len);
 
                        error = EMSGSIZE;
-               } else if (m0->m_pkthdr.len > sbspace(&pcb->so->so_rcv)) {
+               } else if (m0->m_pkthdr.len > ssb_space(&pcb->so->so_rcv)) {
  
                        /*
                         * This is really bad. Receive queue on socket does
@@ -2383,19 +2454,19 @@ ng_btsocket_rfcomm_receive_uih(ng_btsocket_rfcomm_session_p s, int dlci,
 "%s: Not enough space in socket receive queue. Dropping UIH for dlci=%d, " \
 "state=%d, flags=%#x, len=%d, space=%ld\n",
                                __func__, dlci, pcb->state, pcb->flags,
-                               m0->m_pkthdr.len, sbspace(&pcb->so->so_rcv));
+                               m0->m_pkthdr.len, ssb_space(&pcb->so->so_rcv));
 
                        error = ENOBUFS;
                } else {
                        /* Append packet to the socket receive queue */
-                       sbappend(&pcb->so->so_rcv, m0);
+                       sbappend(&pcb->so->so_rcv.sb, m0);
                        m0 = NULL;
 
                        sorwakeup(pcb->so);
                }
        }
 drop1:
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 drop:
        NG_FREE_M(m0); /* checks for != NULL */
 
@@ -2433,7 +2504,7 @@ ng_btsocket_rfcomm_receive_mcc(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        struct rfcomm_mcc_hdr   *hdr = NULL;
        u_int8_t                 cr, type, length;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /*
         * We can access data directly in the first mbuf, because we have
@@ -2529,7 +2600,7 @@ ng_btsocket_rfcomm_receive_test(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        struct rfcomm_mcc_hdr   *hdr = mtod(m0, struct rfcomm_mcc_hdr *);
        int                      error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got MCC TEST, cr=%d, length=%d, session state=%d, flags=%#x, mtu=%d, " \
@@ -2557,7 +2628,7 @@ ng_btsocket_rfcomm_receive_fc(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        u_int8_t                 type = RFCOMM_MCC_TYPE(hdr->type);
        int                      error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /*
         * Turn ON/OFF aggregate flow on the entire session. When remote peer 
@@ -2598,7 +2669,7 @@ ng_btsocket_rfcomm_receive_msc(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        ng_btsocket_rfcomm_pcb_t        *pcb = NULL;
        int                              error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got MCC MSC, dlci=%d, cr=%d, length=%d, session state=%d, flags=%#x, " \
@@ -2618,7 +2689,7 @@ ng_btsocket_rfcomm_receive_msc(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
                        return (ENOENT);
                }
 
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                if (pcb->state != NG_BTSOCKET_RFCOMM_DLC_CONNECTING &&
                    pcb->state != NG_BTSOCKET_RFCOMM_DLC_CONNECTED) {
@@ -2627,7 +2698,7 @@ ng_btsocket_rfcomm_receive_msc(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
                                __func__, RFCOMM_DLCI(msc->address),
                                pcb->state);
 
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
                        NG_FREE_M(m0);
 
                        return (EINVAL);
@@ -2646,7 +2717,7 @@ ng_btsocket_rfcomm_receive_msc(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
                        ng_btsocket_rfcomm_pcb_send(pcb, ALOT);
 #endif /* YYY */
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
        } else
                NG_FREE_M(m0); /* XXX ignore response */
 
@@ -2668,7 +2739,7 @@ ng_btsocket_rfcomm_receive_rpn(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        u_int8_t                 bit_rate, data_bits, stop_bits, parity,
                                 flow_control, xon_char, xoff_char;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got MCC RPN, dlci=%d, cr=%d, length=%d, session state=%d, flags=%#x, " \
@@ -2772,7 +2843,7 @@ ng_btsocket_rfcomm_receive_rls(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        struct rfcomm_mcc_rls   *rls = (struct rfcomm_mcc_rls *)(hdr + 1);
        int                      error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        /*
         * XXX FIXME Do we have to do anything else here? Remote peer tries to 
@@ -2814,7 +2885,7 @@ ng_btsocket_rfcomm_receive_pn(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        ng_btsocket_rfcomm_pcb_t        *pcb = NULL;
        int                              error = 0;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Got MCC PN, dlci=%d, cr=%d, length=%d, flow_control=%#x, priority=%d, " \
@@ -2835,7 +2906,7 @@ ng_btsocket_rfcomm_receive_pn(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
        /* Check if we have this dlci */
        pcb = ng_btsocket_rfcomm_pcb_by_dlci(s, pn->dlci);
        if (pcb != NULL) {
-               mtx_lock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                if (RFCOMM_CR(hdr->type)) {
                        /* PN Request */
@@ -2871,13 +2942,13 @@ ng_btsocket_rfcomm_receive_pn(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
                        NG_FREE_M(m0);
                }
 
-               mtx_unlock(&pcb->pcb_mtx);
+               lockmgr(&pcb->pcb_lock, LK_RELEASE);
        } else if (RFCOMM_CR(hdr->type)) {
                /* PN request to non-existing dlci - incomming connection */
                pcb = ng_btsocket_rfcomm_connect_ind(s,
                                RFCOMM_SRVCHANNEL(pn->dlci));
                if (pcb != NULL) {
-                       mtx_lock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
                        pcb->dlci = pn->dlci;
 
@@ -2904,7 +2975,7 @@ ng_btsocket_rfcomm_receive_pn(ng_btsocket_rfcomm_session_p s, struct mbuf *m0)
                        } else
                                ng_btsocket_rfcomm_pcb_kill(pcb, error);
 
-                       mtx_unlock(&pcb->pcb_mtx);
+                       lockmgr(&pcb->pcb_lock, LK_RELEASE);
                } else {
                        /* Nobody is listen()ing on this channel */
                        error = ng_btsocket_rfcomm_send_command(s,
@@ -2943,7 +3014,7 @@ static void
 ng_btsocket_rfcomm_set_pn(ng_btsocket_rfcomm_pcb_p pcb, u_int8_t cr,
                u_int8_t flow_control, u_int8_t credits, u_int16_t mtu)
 {
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        pcb->mtu = le16toh(mtu);
 
@@ -2983,7 +3054,7 @@ ng_btsocket_rfcomm_send_command(ng_btsocket_rfcomm_session_p s,
        struct mbuf             *m = NULL;
        int                      cr;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Sending command type %#x, session state=%d, flags=%#x, mtu=%d, dlci=%d\n",
@@ -3035,7 +3106,7 @@ ng_btsocket_rfcomm_send_uih(ng_btsocket_rfcomm_session_p s, u_int8_t address,
        struct mbuf             *m = NULL, *mcrc = NULL;
        u_int16_t                length;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        MGETHDR(m, MB_DONTWAIT, MT_DATA);
        if (m == NULL) {
@@ -3108,8 +3179,8 @@ ng_btsocket_rfcomm_send_msc(ng_btsocket_rfcomm_pcb_p pcb)
        struct rfcomm_mcc_hdr   *hdr = NULL;
        struct rfcomm_mcc_msc   *msc = NULL;
 
-       mtx_assert(&pcb->session->session_mtx, MA_OWNED);
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->session->session_lock) != 0);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        MGETHDR(m, MB_DONTWAIT, MT_DATA);
        if (m == NULL)
@@ -3146,8 +3217,8 @@ ng_btsocket_rfcomm_send_pn(ng_btsocket_rfcomm_pcb_p pcb)
        struct rfcomm_mcc_hdr   *hdr = NULL;
        struct rfcomm_mcc_pn    *pn = NULL;
 
-       mtx_assert(&pcb->session->session_mtx, MA_OWNED);
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->session->session_lock) != 0);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        MGETHDR(m, MB_DONTWAIT, MT_DATA);
        if (m == NULL)
@@ -3200,16 +3271,16 @@ ng_btsocket_rfcomm_send_credits(ng_btsocket_rfcomm_pcb_p pcb)
        int             error = 0;
        u_int8_t        credits;
 
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
-       mtx_assert(&pcb->session->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
+       KKASSERT(lockowned(&pcb->session->session_lock) != 0);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Sending more credits, dlci=%d, state=%d, flags=%#x, mtu=%d, " \
 "space=%ld, tx_cred=%d, rx_cred=%d\n",
                __func__, pcb->dlci, pcb->state, pcb->flags, pcb->mtu,
-               sbspace(&pcb->so->so_rcv), pcb->tx_cred, pcb->rx_cred);
+               ssb_space(&pcb->so->so_rcv), pcb->tx_cred, pcb->rx_cred);
 
-       credits = sbspace(&pcb->so->so_rcv) / pcb->mtu;
+       credits = ssb_space(&pcb->so->so_rcv) / pcb->mtu;
        if (credits > 0) {
                if (pcb->rx_cred + credits > RFCOMM_MAX_CREDITS)
                        credits = RFCOMM_MAX_CREDITS - pcb->rx_cred;
@@ -3230,7 +3301,7 @@ ng_btsocket_rfcomm_send_credits(ng_btsocket_rfcomm_pcb_p pcb)
 "%s: Could not send credits, error=%d, dlci=%d, state=%d, flags=%#x, " \
 "mtu=%d, space=%ld, tx_cred=%d, rx_cred=%d\n",
                                __func__, error, pcb->dlci, pcb->state,
-                               pcb->flags, pcb->mtu, sbspace(&pcb->so->so_rcv),
+                               pcb->flags, pcb->mtu, ssb_space(&pcb->so->so_rcv),
                                pcb->tx_cred, pcb->rx_cred);
        }
 
@@ -3254,8 +3325,8 @@ ng_btsocket_rfcomm_pcb_send(ng_btsocket_rfcomm_pcb_p pcb, int limit)
        struct mbuf     *m = NULL;
        int              sent, length, error;
 
-       mtx_assert(&pcb->session->session_mtx, MA_OWNED);
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->session->session_lock) != 0);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->flags & NG_BTSOCKET_RFCOMM_DLC_CFC)
                limit = min(limit, pcb->tx_cred);
@@ -3275,18 +3346,19 @@ ng_btsocket_rfcomm_pcb_send(ng_btsocket_rfcomm_pcb_p pcb, int limit)
        }
 
        for (error = 0, sent = 0; sent < limit; sent ++) { 
-               length = min(pcb->mtu, pcb->so->so_snd.sb_cc);
+               length = min(pcb->mtu, pcb->so->so_snd.sb.sb_cc);
                if (length == 0)
                        break;
 
                /* Get the chunk from the socket's send buffer */
-               m = ng_btsocket_rfcomm_prepare_packet(&pcb->so->so_snd, length);
+               m = ng_btsocket_rfcomm_prepare_packet(&pcb->so->so_snd.sb,
+                   length);
                if (m == NULL) {
                        error = ENOBUFS;
                        break;
                }
 
-               sbdrop(&pcb->so->so_snd, length);
+               sbdrop(&pcb->so->so_snd.sb, length);
 
                error = ng_btsocket_rfcomm_send_uih(pcb->session,
                                RFCOMM_MKADDRESS(INITIATOR(pcb->session),
@@ -3325,8 +3397,8 @@ ng_btsocket_rfcomm_pcb_kill(ng_btsocket_rfcomm_pcb_p pcb, int error)
                panic("%s: DLC without session, pcb=%p, state=%d, flags=%#x",
                        __func__, pcb, pcb->state, pcb->flags);
 
-       mtx_assert(&pcb->session->session_mtx, MA_OWNED);
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->session->session_lock) != 0);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->flags & NG_BTSOCKET_RFCOMM_DLC_TIMO)
                ng_btsocket_rfcomm_untimeout(pcb);
@@ -3392,7 +3464,7 @@ ng_btsocket_rfcomm_pcb_by_dlci(ng_btsocket_rfcomm_session_p s, int dlci)
 {
        ng_btsocket_rfcomm_pcb_p        pcb = NULL;
 
-       mtx_assert(&s->session_mtx, MA_OWNED);
+       KKASSERT(lockowned(&s->session_lock) != 0);
 
        LIST_FOREACH(pcb, &s->dlcs, session_next)
                if (pcb->dlci == dlci)
@@ -3410,7 +3482,7 @@ ng_btsocket_rfcomm_pcb_listener(bdaddr_p src, int channel)
 {
        ng_btsocket_rfcomm_pcb_p        pcb = NULL, pcb1 = NULL;
 
-       mtx_lock(&ng_btsocket_rfcomm_sockets_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_EXCLUSIVE);
 
        LIST_FOREACH(pcb, &ng_btsocket_rfcomm_sockets, next) {
                if (pcb->channel != channel ||
@@ -3424,7 +3496,7 @@ ng_btsocket_rfcomm_pcb_listener(bdaddr_p src, int channel)
                        pcb1 = pcb;
        }
 
-       mtx_unlock(&ng_btsocket_rfcomm_sockets_mtx);
+       lockmgr(&ng_btsocket_rfcomm_sockets_lock, LK_RELEASE);
 
        return ((pcb != NULL)? pcb : pcb1);
 } /* ng_btsocket_rfcomm_pcb_listener */
@@ -3442,13 +3514,13 @@ ng_btsocket_rfcomm_pcb_listener(bdaddr_p src, int channel)
 static void
 ng_btsocket_rfcomm_timeout(ng_btsocket_rfcomm_pcb_p pcb)
 {
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (!(pcb->flags & NG_BTSOCKET_RFCOMM_DLC_TIMO)) {
                pcb->flags |= NG_BTSOCKET_RFCOMM_DLC_TIMO;
                pcb->flags &= ~NG_BTSOCKET_RFCOMM_DLC_TIMEDOUT;
-               pcb->timo = timeout(ng_btsocket_rfcomm_process_timeout, pcb,
-                                       ng_btsocket_rfcomm_timo * hz);
+               callout_reset(&pcb->timo, ng_btsocket_rfcomm_timo * hz,
+                   ng_btsocket_rfcomm_process_timeout, pcb);
        } else
                panic("%s: Duplicated socket timeout?!", __func__);
 } /* ng_btsocket_rfcomm_timeout */
@@ -3460,10 +3532,10 @@ ng_btsocket_rfcomm_timeout(ng_btsocket_rfcomm_pcb_p pcb)
 static void
 ng_btsocket_rfcomm_untimeout(ng_btsocket_rfcomm_pcb_p pcb)
 {
-       mtx_assert(&pcb->pcb_mtx, MA_OWNED);
+       KKASSERT(lockowned(&pcb->pcb_lock) != 0);
 
        if (pcb->flags & NG_BTSOCKET_RFCOMM_DLC_TIMO) {
-               untimeout(ng_btsocket_rfcomm_process_timeout, pcb, pcb->timo);
+               callout_stop(&pcb->timo);
                pcb->flags &= ~NG_BTSOCKET_RFCOMM_DLC_TIMO;
                pcb->flags &= ~NG_BTSOCKET_RFCOMM_DLC_TIMEDOUT;
        } else
@@ -3479,7 +3551,7 @@ ng_btsocket_rfcomm_process_timeout(void *xpcb)
 {
        ng_btsocket_rfcomm_pcb_p        pcb = (ng_btsocket_rfcomm_pcb_p) xpcb;
 
-       mtx_lock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
 
        NG_BTSOCKET_RFCOMM_INFO(
 "%s: Timeout, so=%p, dlci=%d, state=%d, flags=%#x\n",
@@ -3507,7 +3579,7 @@ ng_btsocket_rfcomm_process_timeout(void *xpcb)
 
        ng_btsocket_rfcomm_task_wakeup();
 
-       mtx_unlock(&pcb->pcb_mtx);
+       lockmgr(&pcb->pcb_lock, LK_RELEASE);
 } /* ng_btsocket_rfcomm_process_timeout */
 
 /*
index 0a7f65d..62810b1 100644 (file)
@@ -601,6 +601,7 @@ struct ng_apply_info {
 struct ng_item {
        struct lwkt_msg         el_lmsg;
        u_long  el_flags;
+       STAILQ_ENTRY(ng_item)   el_next;
        node_p  el_dest; /* The node it will be applied against (or NULL) */
        hook_p  el_hook; /* Entering hook. Optional in Control messages */
        union {