Author: thompsa
Date: Wed May 27 16:33:08 2009
New Revision: 192906
URL: http://svn.freebsd.org/changeset/base/192906

Log:
  move ng_ubt.c back to its original place

Added:
  head/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c   (props changed)
     - copied unchanged from r192905, head/sys/dev/usb/bluetooth/ng_ubt.c
Deleted:
  head/sys/dev/usb/bluetooth/ng_ubt.c

Copied: head/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c (from r192905, 
head/sys/dev/usb/bluetooth/ng_ubt.c)
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/netgraph/bluetooth/drivers/ubt/ng_ubt.c    Wed May 27 16:33:08 
2009        (r192906, copy of r192905, head/sys/dev/usb/bluetooth/ng_ubt.c)
@@ -0,0 +1,1722 @@
+/*
+ * ng_ubt.c
+ */
+
+/*-
+ * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmen...@yahoo.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: ng_ubt.c,v 1.16 2003/10/10 19:15:06 max Exp $
+ * $FreeBSD$
+ */
+
+/*
+ * NOTE: ng_ubt2 driver has a split personality. On one side it is
+ * a USB device driver and on the other it is a Netgraph node. This
+ * driver will *NOT* create traditional /dev/ enties, only Netgraph 
+ * node.
+ *
+ * NOTE ON LOCKS USED: ng_ubt2 drives uses 2 locks (mutexes)
+ *
+ * 1) sc_if_mtx - lock for device's interface #0 and #1. This lock is used
+ *    by USB for any USB request going over device's interface #0 and #1,
+ *    i.e. interrupt, control, bulk and isoc. transfers.
+ * 
+ * 2) sc_ng_mtx - this lock is used to protect shared (between USB, Netgraph
+ *    and Taskqueue) data, such as outgoing mbuf queues, task flags and hook
+ *    pointer. This lock *SHOULD NOT* be grabbed for a long time. In fact,
+ *    think of it as a spin lock.
+ *
+ * NOTE ON LOCKING STRATEGY: ng_ubt2 driver operates in 3 different contexts.
+ *
+ * 1) USB context. This is where all the USB related stuff happens. All
+ *    callbacks run in this context. All callbacks are called (by USB) with
+ *    appropriate interface lock held. It is (generally) allowed to grab
+ *    any additional locks.
+ *
+ * 2) Netgraph context. This is where all the Netgraph related stuff happens.
+ *    Since we mark node as WRITER, the Netgraph node will be "locked" (from
+ *    Netgraph point of view). Any variable that is only modified from the
+ *    Netgraph context does not require any additonal locking. It is generally
+ *    *NOT* allowed to grab *ANY* additional locks. Whatever you do, *DO NOT*
+ *    grab any lock in the Netgraph context that could cause de-scheduling of
+ *    the Netgraph thread for significant amount of time. In fact, the only
+ *    lock that is allowed in the Netgraph context is the sc_ng_mtx lock.
+ *    Also make sure that any code that is called from the Netgraph context
+ *    follows the rule above.
+ *
+ * 3) Taskqueue context. This is where ubt_task runs. Since we are generally
+ *    NOT allowed to grab any lock that could cause de-scheduling in the
+ *    Netgraph context, and, USB requires us to grab interface lock before
+ *    doing things with transfers, it is safer to transition from the Netgraph
+ *    context to the Taskqueue context before we can call into USB subsystem.
+ *
+ * So, to put everything together, the rules are as follows.
+ *     It is OK to call from the USB context or the Taskqueue context into
+ * the Netgraph context (i.e. call NG_SEND_xxx functions). In other words
+ * it is allowed to call into the Netgraph context with locks held.
+ *     Is it *NOT* OK to call from the Netgraph context into the USB context,
+ * because USB requires us to grab interface locks, and, it is safer to
+ * avoid it. So, to make things safer we set task flags to indicate which
+ * actions we want to perform and schedule ubt_task which would run in the
+ * Taskqueue context.
+ *     Is is OK to call from the Taskqueue context into the USB context,
+ * and, ubt_task does just that (i.e. grabs appropriate interface locks
+ * before calling into USB).
+ *     Access to the outgoing queues, task flags and hook pointer is
+ * controlled by the sc_ng_mtx lock. It is an unavoidable evil. Again,
+ * sc_ng_mtx should really be a spin lock (and it is very likely to an
+ * equivalent of spin lock due to adaptive nature of FreeBSD mutexes).
+ *     All USB callbacks accept softc pointer as a private data. USB ensures
+ * that this pointer is valid.
+ */
+
+#include "usbdevs.h"
+#include <dev/usb/usb.h>
+#include <dev/usb/usb_mfunc.h>
+#include <dev/usb/usb_error.h>
+
+#define        USB_DEBUG_VAR usb2_debug
+
+#include <dev/usb/usb_core.h>
+#include <dev/usb/usb_debug.h>
+#include <dev/usb/usb_parse.h>
+#include <dev/usb/usb_lookup.h>
+#include <dev/usb/usb_util.h>
+#include <dev/usb/usb_busdma.h>
+#include <dev/usb/usb_process.h>
+#include <dev/usb/usb_transfer.h>
+
+#include <sys/mbuf.h>
+#include <sys/taskqueue.h>
+
+#include <netgraph/ng_message.h>
+#include <netgraph/netgraph.h>
+#include <netgraph/ng_parse.h>
+#include <netgraph/bluetooth/include/ng_bluetooth.h>
+#include <netgraph/bluetooth/include/ng_hci.h>
+#include <netgraph/bluetooth/include/ng_ubt.h>
+
+#include <dev/usb/bluetooth/ng_ubt_var.h>
+
+static int             ubt_modevent(module_t, int, void *);
+static device_probe_t  ubt_probe;
+static device_attach_t ubt_attach;
+static device_detach_t ubt_detach;
+
+static void            ubt_task_schedule(ubt_softc_p, int);
+static task_fn_t       ubt_task;
+
+#define        ubt_xfer_start(sc, i)   usb2_transfer_start((sc)->sc_xfer[(i)])
+
+/* Netgraph methods */
+static ng_constructor_t        ng_ubt_constructor;
+static ng_shutdown_t   ng_ubt_shutdown;
+static ng_newhook_t    ng_ubt_newhook;
+static ng_connect_t    ng_ubt_connect;
+static ng_disconnect_t ng_ubt_disconnect;
+static ng_rcvmsg_t     ng_ubt_rcvmsg;
+static ng_rcvdata_t    ng_ubt_rcvdata;
+
+/* Queue length */
+static const struct ng_parse_struct_field      ng_ubt_node_qlen_type_fields[] =
+{
+       { "queue", &ng_parse_int32_type, },
+       { "qlen",  &ng_parse_int32_type, },
+       { NULL, }
+};
+static const struct ng_parse_type              ng_ubt_node_qlen_type =
+{
+       &ng_parse_struct_type,
+       &ng_ubt_node_qlen_type_fields
+};
+
+/* Stat info */
+static const struct ng_parse_struct_field      ng_ubt_node_stat_type_fields[] =
+{
+       { "pckts_recv", &ng_parse_uint32_type, },
+       { "bytes_recv", &ng_parse_uint32_type, },
+       { "pckts_sent", &ng_parse_uint32_type, },
+       { "bytes_sent", &ng_parse_uint32_type, },
+       { "oerrors",    &ng_parse_uint32_type, },
+       { "ierrors",    &ng_parse_uint32_type, },
+       { NULL, }
+};
+static const struct ng_parse_type              ng_ubt_node_stat_type =
+{
+       &ng_parse_struct_type,
+       &ng_ubt_node_stat_type_fields
+};
+
+/* Netgraph node command list */
+static const struct ng_cmdlist                 ng_ubt_cmdlist[] =
+{
+       {
+               NGM_UBT_COOKIE,
+               NGM_UBT_NODE_SET_DEBUG,
+               "set_debug",
+               &ng_parse_uint16_type,
+               NULL
+       },
+       {
+               NGM_UBT_COOKIE,
+               NGM_UBT_NODE_GET_DEBUG,
+               "get_debug",
+               NULL,
+               &ng_parse_uint16_type
+       },
+       {
+               NGM_UBT_COOKIE,
+               NGM_UBT_NODE_SET_QLEN,
+               "set_qlen",
+               &ng_ubt_node_qlen_type,
+               NULL
+       },
+       {
+               NGM_UBT_COOKIE,
+               NGM_UBT_NODE_GET_QLEN,
+               "get_qlen",
+               &ng_ubt_node_qlen_type,
+               &ng_ubt_node_qlen_type
+       },
+       {
+               NGM_UBT_COOKIE,
+               NGM_UBT_NODE_GET_STAT,
+               "get_stat",
+               NULL,
+               &ng_ubt_node_stat_type
+       },
+       {
+               NGM_UBT_COOKIE,
+               NGM_UBT_NODE_RESET_STAT,
+               "reset_stat",
+               NULL,
+               NULL
+       },
+       { 0, }
+};
+
+/* Netgraph node type */
+static struct ng_type  typestruct =
+{
+       .version =      NG_ABI_VERSION,
+       .name =         NG_UBT_NODE_TYPE,
+       .constructor =  ng_ubt_constructor,
+       .rcvmsg =       ng_ubt_rcvmsg,
+       .shutdown =     ng_ubt_shutdown,
+       .newhook =      ng_ubt_newhook,
+       .connect =      ng_ubt_connect,
+       .rcvdata =      ng_ubt_rcvdata,
+       .disconnect =   ng_ubt_disconnect,
+       .cmdlist =      ng_ubt_cmdlist
+};
+
+/****************************************************************************
+ ****************************************************************************
+ **                              USB specific
+ ****************************************************************************
+ ****************************************************************************/
+
+/* USB methods */
+static usb2_callback_t ubt_ctrl_write_callback;
+static usb2_callback_t ubt_intr_read_callback;
+static usb2_callback_t ubt_bulk_read_callback;
+static usb2_callback_t ubt_bulk_write_callback;
+static usb2_callback_t ubt_isoc_read_callback;
+static usb2_callback_t ubt_isoc_write_callback;
+
+static int             ubt_fwd_mbuf_up(ubt_softc_p, struct mbuf **);
+static int             ubt_isoc_read_one_frame(struct usb2_xfer *, int);
+
+/*
+ * USB config
+ * 
+ * The following desribes usb transfers that could be submitted on USB device.
+ *
+ * Interface 0 on the USB device must present the following endpoints
+ *     1) Interrupt endpoint to receive HCI events
+ *     2) Bulk IN endpoint to receive ACL data
+ *     3) Bulk OUT endpoint to send ACL data
+ *
+ * Interface 1 on the USB device must present the following endpoints
+ *     1) Isochronous IN endpoint to receive SCO data
+ *     2) Isochronous OUT endpoint to send SCO data
+ */
+
+static const struct usb2_config                ubt_config[UBT_N_TRANSFER] =
+{
+       /*
+        * Interface #0
+        */
+
+       /* Outgoing bulk transfer - ACL packets */
+       [UBT_IF_0_BULK_DT_WR] = {
+               .type =         UE_BULK,
+               .endpoint =     UE_ADDR_ANY,
+               .direction =    UE_DIR_OUT,
+               .if_index =     0,
+               .bufsize =      UBT_BULK_WRITE_BUFFER_SIZE,
+               .flags =        { .pipe_bof = 1, .force_short_xfer = 1, },
+               .callback =     &ubt_bulk_write_callback,
+       },
+       /* Incoming bulk transfer - ACL packets */
+       [UBT_IF_0_BULK_DT_RD] = {
+               .type =         UE_BULK,
+               .endpoint =     UE_ADDR_ANY,
+               .direction =    UE_DIR_IN,
+               .if_index =     0,
+               .bufsize =      UBT_BULK_READ_BUFFER_SIZE,
+               .flags =        { .pipe_bof = 1, .short_xfer_ok = 1, },
+               .callback =     &ubt_bulk_read_callback,
+       },
+       /* Incoming interrupt transfer - HCI events */
+       [UBT_IF_0_INTR_DT_RD] = {
+               .type =         UE_INTERRUPT,
+               .endpoint =     UE_ADDR_ANY,
+               .direction =    UE_DIR_IN,
+               .if_index =     0,
+               .flags =        { .pipe_bof = 1, .short_xfer_ok = 1, },
+               .bufsize =      UBT_INTR_BUFFER_SIZE,
+               .callback =     &ubt_intr_read_callback,
+       },
+       /* Outgoing control transfer - HCI commands */
+       [UBT_IF_0_CTRL_DT_WR] = {
+               .type =         UE_CONTROL,
+               .endpoint =     0x00,   /* control pipe */
+               .direction =    UE_DIR_ANY,
+               .if_index =     0,
+               .bufsize =      UBT_CTRL_BUFFER_SIZE,
+               .callback =     &ubt_ctrl_write_callback,
+               .timeout =      5000,   /* 5 seconds */
+       },
+
+       /*
+        * Interface #1
+        */
+
+       /* Incoming isochronous transfer #1 - SCO packets */
+       [UBT_IF_1_ISOC_DT_RD1] = {
+               .type =         UE_ISOCHRONOUS,
+               .endpoint =     UE_ADDR_ANY,
+               .direction =    UE_DIR_IN,
+               .if_index =     1,
+               .bufsize =      0,      /* use "wMaxPacketSize * frames" */
+               .frames =       UBT_ISOC_NFRAMES,
+               .flags =        { .short_xfer_ok = 1, },
+               .callback =     &ubt_isoc_read_callback,
+       },
+       /* Incoming isochronous transfer #2 - SCO packets */
+       [UBT_IF_1_ISOC_DT_RD2] = {
+               .type =         UE_ISOCHRONOUS,
+               .endpoint =     UE_ADDR_ANY,
+               .direction =    UE_DIR_IN,
+               .if_index =     1,
+               .bufsize =      0,      /* use "wMaxPacketSize * frames" */
+               .frames =       UBT_ISOC_NFRAMES,
+               .flags =        { .short_xfer_ok = 1, },
+               .callback =     &ubt_isoc_read_callback,
+       },
+       /* Outgoing isochronous transfer #1 - SCO packets */
+       [UBT_IF_1_ISOC_DT_WR1] = {
+               .type =         UE_ISOCHRONOUS,
+               .endpoint =     UE_ADDR_ANY,
+               .direction =    UE_DIR_OUT,
+               .if_index =     1,
+               .bufsize =      0,      /* use "wMaxPacketSize * frames" */
+               .frames =       UBT_ISOC_NFRAMES,
+               .flags =        { .short_xfer_ok = 1, },
+               .callback =     &ubt_isoc_write_callback,
+       },
+       /* Outgoing isochronous transfer #2 - SCO packets */
+       [UBT_IF_1_ISOC_DT_WR2] = {
+               .type =         UE_ISOCHRONOUS,
+               .endpoint =     UE_ADDR_ANY,
+               .direction =    UE_DIR_OUT,
+               .if_index =     1,
+               .bufsize =      0,      /* use "wMaxPacketSize * frames" */
+               .frames =       UBT_ISOC_NFRAMES,
+               .flags =        { .short_xfer_ok = 1, },
+               .callback =     &ubt_isoc_write_callback,
+       },
+};
+
+/*
+ * If for some reason device should not be attached then put
+ * VendorID/ProductID pair into the list below. The format is
+ * as follows:
+ *
+ *     { USB_VPI(VENDOR_ID, PRODUCT_ID, 0) },
+ *
+ * where VENDOR_ID and PRODUCT_ID are hex numbers.
+ */
+
+static const struct usb2_device_id ubt_ignore_devs[] = 
+{
+       /* AVM USB Bluetooth-Adapter BlueFritz! v1.0 */
+       { USB_VPI(USB_VENDOR_AVM, 0x2200, 0) },
+};
+
+/* List of supported bluetooth devices */
+static const struct usb2_device_id ubt_devs[] =
+{
+       /* Generic Bluetooth class devices */
+       { USB_IFACE_CLASS(UDCLASS_WIRELESS),
+         USB_IFACE_SUBCLASS(UDSUBCLASS_RF),
+         USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) },
+
+       /* AVM USB Bluetooth-Adapter BlueFritz! v2.0 */
+       { USB_VPI(USB_VENDOR_AVM, 0x3800, 0) },
+};
+
+/*
+ * Probe for a USB Bluetooth device.
+ * USB context.
+ */
+
+static int
+ubt_probe(device_t dev)
+{
+       struct usb2_attach_arg  *uaa = device_get_ivars(dev);
+
+       if (uaa->usb_mode != USB_MODE_HOST)
+               return (ENXIO);
+
+       if (uaa->info.bIfaceIndex != 0)
+               return (ENXIO);
+
+       if (uaa->use_generic == 0)
+               return (ENXIO);
+
+       if (usb2_lookup_id_by_uaa(ubt_ignore_devs,
+                       sizeof(ubt_ignore_devs), uaa) == 0)
+               return (ENXIO);
+
+       return (usb2_lookup_id_by_uaa(ubt_devs, sizeof(ubt_devs), uaa));
+} /* ubt_probe */
+
+/*
+ * Attach the device.
+ * USB context.
+ */
+
+static int
+ubt_attach(device_t dev)
+{
+       struct usb2_attach_arg          *uaa = device_get_ivars(dev);
+       struct ubt_softc                *sc = device_get_softc(dev);
+       struct usb2_endpoint_descriptor *ed;
+       struct usb2_interface_descriptor *id;
+       uint16_t                        wMaxPacketSize;
+       uint8_t                         alt_index, i, j;
+       uint8_t                         iface_index[2] = { 0, 1 };
+
+       device_set_usb2_desc(dev);
+
+       sc->sc_dev = dev;
+       sc->sc_debug = NG_UBT_WARN_LEVEL;
+
+       /* 
+        * Create Netgraph node
+        */
+
+       if (ng_make_node_common(&typestruct, &sc->sc_node) != 0) {
+               UBT_ALERT(sc, "could not create Netgraph node\n");
+               return (ENXIO);
+       }
+
+       /* Name Netgraph node */
+       if (ng_name_node(sc->sc_node, device_get_nameunit(dev)) != 0) {
+               UBT_ALERT(sc, "could not name Netgraph node\n");
+               NG_NODE_UNREF(sc->sc_node);
+               return (ENXIO);
+       }
+       NG_NODE_SET_PRIVATE(sc->sc_node, sc);
+       NG_NODE_FORCE_WRITER(sc->sc_node);
+
+       /*
+        * Initialize device softc structure
+        */
+
+       /* initialize locks */
+       mtx_init(&sc->sc_ng_mtx, "ubt ng", NULL, MTX_DEF);
+       mtx_init(&sc->sc_if_mtx, "ubt if", NULL, MTX_DEF | MTX_RECURSE);
+
+       /* initialize packet queues */
+       NG_BT_MBUFQ_INIT(&sc->sc_cmdq, UBT_DEFAULT_QLEN);
+       NG_BT_MBUFQ_INIT(&sc->sc_aclq, UBT_DEFAULT_QLEN);
+       NG_BT_MBUFQ_INIT(&sc->sc_scoq, UBT_DEFAULT_QLEN);
+
+       /* initialize glue task */
+       TASK_INIT(&sc->sc_task, 0, ubt_task, sc);
+
+       /*
+        * Configure Bluetooth USB device. Discover all required USB
+        * interfaces and endpoints.
+        *
+        * USB device must present two interfaces:
+        * 1) Interface 0 that has 3 endpoints
+        *      1) Interrupt endpoint to receive HCI events
+        *      2) Bulk IN endpoint to receive ACL data
+        *      3) Bulk OUT endpoint to send ACL data
+        *
+        * 2) Interface 1 then has 2 endpoints
+        *      1) Isochronous IN endpoint to receive SCO data
+        *      2) Isochronous OUT endpoint to send SCO data
+        *
+        * Interface 1 (with isochronous endpoints) has several alternate
+        * configurations with different packet size.
+        */
+
+       /*
+        * For interface #1 search alternate settings, and find
+        * the descriptor with the largest wMaxPacketSize
+        */
+
+       wMaxPacketSize = 0;
+       alt_index = 0;
+       i = 0;
+       j = 0;
+       ed = NULL;
+
+       /* 
+        * Search through all the descriptors looking for the largest
+        * packet size:
+        */
+       while ((ed = (struct usb2_endpoint_descriptor *)usb2_desc_foreach(
+           usb2_get_config_descriptor(uaa->device), 
+           (struct usb2_descriptor *)ed))) {
+
+               if ((ed->bDescriptorType == UDESC_INTERFACE) &&
+                   (ed->bLength >= sizeof(*id))) {
+                       id = (struct usb2_interface_descriptor *)ed;
+                       i = id->bInterfaceNumber;
+                       j = id->bAlternateSetting;
+               }
+
+               if ((ed->bDescriptorType == UDESC_ENDPOINT) &&
+                   (ed->bLength >= sizeof(*ed)) &&
+                   (i == 1)) {
+                       uint16_t temp;
+
+                       temp = UGETW(ed->wMaxPacketSize);
+                       if (temp > wMaxPacketSize) {
+                               wMaxPacketSize = temp;
+                               alt_index = j;
+                       }
+               }
+       }
+
+       /* Set alt configuration on interface #1 only if we found it */
+       if (wMaxPacketSize > 0 &&
+           usb2_set_alt_interface_index(uaa->device, 1, alt_index)) {
+               UBT_ALERT(sc, "could not set alternate setting %d " \
+                       "for interface 1!\n", alt_index);
+               goto detach;
+       }
+
+       /* Setup transfers for both interfaces */
+       if (usb2_transfer_setup(uaa->device, iface_index, sc->sc_xfer,
+                       ubt_config, UBT_N_TRANSFER, sc, &sc->sc_if_mtx)) {
+               UBT_ALERT(sc, "could not allocate transfers\n");
+               goto detach;
+       }
+
+       /* Claim all interfaces on the device */
+       for (i = 1; usb2_get_iface(uaa->device, i) != NULL; i ++)
+               usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
+
+       return (0); /* success */
+
+detach:
+       ubt_detach(dev);
+
+       return (ENXIO);
+} /* ubt_attach */
+
+/*
+ * Detach the device.
+ * USB context.
+ */
+
+int
+ubt_detach(device_t dev)
+{
+       struct ubt_softc        *sc = device_get_softc(dev);
+       node_p                  node = sc->sc_node;
+
+       /* Destroy Netgraph node */
+       if (node != NULL) {
+               sc->sc_node = NULL;
+               NG_NODE_REALLY_DIE(node);
+               ng_rmnode_self(node);
+       }
+
+       /* Make sure ubt_task in gone */
+       taskqueue_drain(taskqueue_swi, &sc->sc_task);
+
+       /* Free USB transfers, if any */
+       usb2_transfer_unsetup(sc->sc_xfer, UBT_N_TRANSFER);
+
+       /* Destroy queues */
+       UBT_NG_LOCK(sc);
+       NG_BT_MBUFQ_DESTROY(&sc->sc_cmdq);
+       NG_BT_MBUFQ_DESTROY(&sc->sc_aclq);
+       NG_BT_MBUFQ_DESTROY(&sc->sc_scoq);
+       UBT_NG_UNLOCK(sc);
+
+       mtx_destroy(&sc->sc_if_mtx);
+       mtx_destroy(&sc->sc_ng_mtx);
+
+       return (0);
+} /* ubt_detach */
+
+/* 
+ * Called when outgoing control request (HCI command) has completed, i.e.
+ * HCI command was sent to the device.
+ * USB context.
+ */
+
+static void
+ubt_ctrl_write_callback(struct usb2_xfer *xfer)
+{
+       struct ubt_softc                *sc = xfer->priv_sc;
+       struct usb2_device_request      req;
+       struct mbuf                     *m;
+
+       switch (USB_GET_STATE(xfer)) {
+       case USB_ST_TRANSFERRED:
+               UBT_INFO(sc, "sent %d bytes to control pipe\n", xfer->actlen);
+               UBT_STAT_BYTES_SENT(sc, xfer->actlen);
+               UBT_STAT_PCKTS_SENT(sc);
+               /* FALLTHROUGH */
+
+       case USB_ST_SETUP:
+send_next:
+               /* Get next command mbuf, if any */
+               UBT_NG_LOCK(sc);
+               NG_BT_MBUFQ_DEQUEUE(&sc->sc_cmdq, m);
+               UBT_NG_UNLOCK(sc);
+
+               if (m == NULL) {
+                       UBT_INFO(sc, "HCI command queue is empty\n");
+                       break;  /* transfer complete */
+               }
+
+               /* Initialize a USB control request and then schedule it */
+               bzero(&req, sizeof(req));
+               req.bmRequestType = UBT_HCI_REQUEST;
+               USETW(req.wLength, m->m_pkthdr.len);
+
+               UBT_INFO(sc, "Sending control request, " \
+                       "bmRequestType=0x%02x, wLength=%d\n",
+                       req.bmRequestType, UGETW(req.wLength));
+
+               usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
+               usb2_m_copy_in(xfer->frbuffers + 1, 0, m, 0, m->m_pkthdr.len);
+
+               xfer->frlengths[0] = sizeof(req);
+               xfer->frlengths[1] = m->m_pkthdr.len;
+               xfer->nframes = 2;
+
+               NG_FREE_M(m);
+
+               usb2_start_hardware(xfer);
+               break;
+
+       default: /* Error */
+               if (xfer->error != USB_ERR_CANCELLED) {
+                       UBT_WARN(sc, "control transfer failed: %s\n",
+                               usb2_errstr(xfer->error));
+
+                       UBT_STAT_OERROR(sc);
+                       goto send_next;
+               }
+
+               /* transfer cancelled */
+               break;
+       }
+} /* ubt_ctrl_write_callback */
+
+/* 
+ * Called when incoming interrupt transfer (HCI event) has completed, i.e.
+ * HCI event was received from the device.
+ * USB context.
+ */
+
+static void
+ubt_intr_read_callback(struct usb2_xfer *xfer)
+{
+       struct ubt_softc        *sc = xfer->priv_sc;
+       struct mbuf             *m;
+       ng_hci_event_pkt_t      *hdr;
+
+       m = NULL;
+
+       switch (USB_GET_STATE(xfer)) {
+       case USB_ST_TRANSFERRED:
+               /* Allocate a new mbuf */
+               MGETHDR(m, M_DONTWAIT, MT_DATA);
+               if (m == NULL) {
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               MCLGET(m, M_DONTWAIT);
+               if (!(m->m_flags & M_EXT)) {
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               /* Add HCI packet type */
+               *mtod(m, uint8_t *)= NG_HCI_EVENT_PKT;
+               m->m_pkthdr.len = m->m_len = 1;
+
+               if (xfer->actlen > MCLBYTES - 1)
+                       xfer->actlen = MCLBYTES - 1;
+
+               usb2_copy_out(xfer->frbuffers, 0, mtod(m, uint8_t *) + 1,
+                       xfer->actlen);
+               m->m_pkthdr.len += xfer->actlen;
+               m->m_len += xfer->actlen;
+
+               UBT_INFO(sc, "got %d bytes from interrupt pipe\n",
+                       xfer->actlen);
+
+               /* Validate packet and send it up the stack */
+               if (m->m_pkthdr.len < sizeof(*hdr)) {
+                       UBT_INFO(sc, "HCI event packet is too short\n");
+
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               hdr = mtod(m, ng_hci_event_pkt_t *);
+               if (hdr->length != (m->m_pkthdr.len - sizeof(*hdr))) {
+                       UBT_ERR(sc, "Invalid HCI event packet size, " \
+                               "length=%d, pktlen=%d\n",
+                               hdr->length, m->m_pkthdr.len);
+
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               UBT_INFO(sc, "got complete HCI event frame, pktlen=%d, " \
+                       "length=%d\n", m->m_pkthdr.len, hdr->length);
+
+               UBT_STAT_PCKTS_RECV(sc);
+               UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len);
+
+               ubt_fwd_mbuf_up(sc, &m);
+               /* m == NULL at this point */
+               /* FALLTHROUGH */
+
+       case USB_ST_SETUP:
+submit_next:
+               NG_FREE_M(m); /* checks for m != NULL */
+
+               xfer->frlengths[0] = xfer->max_data_length;
+               usb2_start_hardware(xfer);
+               break;
+
+       default: /* Error */
+               if (xfer->error != USB_ERR_CANCELLED) {
+                       UBT_WARN(sc, "interrupt transfer failed: %s\n",
+                               usb2_errstr(xfer->error));
+
+                       /* Try to clear stall first */
+                       xfer->flags.stall_pipe = 1;
+                       goto submit_next;
+               }
+                       /* transfer cancelled */
+               break;
+       }
+} /* ubt_intr_read_callback */
+
+/*
+ * Called when incoming bulk transfer (ACL packet) has completed, i.e.
+ * ACL packet was received from the device.
+ * USB context.
+ */
+
+static void
+ubt_bulk_read_callback(struct usb2_xfer *xfer)
+{
+       struct ubt_softc        *sc = xfer->priv_sc;
+       struct mbuf             *m;
+       ng_hci_acldata_pkt_t    *hdr;
+       uint16_t                len;
+
+       m = NULL;
+
+       switch (USB_GET_STATE(xfer)) {
+       case USB_ST_TRANSFERRED:
+               /* Allocate new mbuf */
+               MGETHDR(m, M_DONTWAIT, MT_DATA);
+               if (m == NULL) {
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               MCLGET(m, M_DONTWAIT);
+               if (!(m->m_flags & M_EXT)) {
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               /* Add HCI packet type */
+               *mtod(m, uint8_t *)= NG_HCI_ACL_DATA_PKT;
+               m->m_pkthdr.len = m->m_len = 1;
+
+               if (xfer->actlen > MCLBYTES - 1)
+                       xfer->actlen = MCLBYTES - 1;
+
+               usb2_copy_out(xfer->frbuffers, 0, mtod(m, uint8_t *) + 1,
+                       xfer->actlen);
+               m->m_pkthdr.len += xfer->actlen;
+               m->m_len += xfer->actlen;
+
+               UBT_INFO(sc, "got %d bytes from bulk-in pipe\n",
+                       xfer->actlen);
+
+               /* Validate packet and send it up the stack */
+               if (m->m_pkthdr.len < sizeof(*hdr)) {
+                       UBT_INFO(sc, "HCI ACL packet is too short\n");
+
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               hdr = mtod(m, ng_hci_acldata_pkt_t *);
+               len = le16toh(hdr->length);
+               if (len != (m->m_pkthdr.len - sizeof(*hdr))) {
+                       UBT_ERR(sc, "Invalid ACL packet size, length=%d, " \
+                               "pktlen=%d\n", len, m->m_pkthdr.len);
+
+                       UBT_STAT_IERROR(sc);
+                       goto submit_next;
+               }
+
+               UBT_INFO(sc, "got complete ACL data packet, pktlen=%d, " \
+                       "length=%d\n", m->m_pkthdr.len, len);
+
+               UBT_STAT_PCKTS_RECV(sc);
+               UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len);
+
+               ubt_fwd_mbuf_up(sc, &m);
+               /* m == NULL at this point */
+               /* FALLTHOUGH */
+
+       case USB_ST_SETUP:
+submit_next:
+               NG_FREE_M(m); /* checks for m != NULL */
+
+               xfer->frlengths[0] = xfer->max_data_length;
+               usb2_start_hardware(xfer);
+               break;
+
+       default: /* Error */
+               if (xfer->error != USB_ERR_CANCELLED) {
+                       UBT_WARN(sc, "bulk-in transfer failed: %s\n",
+                               usb2_errstr(xfer->error));
+
+                       /* Try to clear stall first */
+                       xfer->flags.stall_pipe = 1;
+                       goto submit_next;
+               }
+                       /* transfer cancelled */
+               break;
+       }
+} /* ubt_bulk_read_callback */
+
+/*
+ * Called when outgoing bulk transfer (ACL packet) has completed, i.e.
+ * ACL packet was sent to the device.
+ * USB context.
+ */
+
+static void
+ubt_bulk_write_callback(struct usb2_xfer *xfer)
+{
+       struct ubt_softc        *sc = xfer->priv_sc;
+       struct mbuf             *m;
+
+       switch (USB_GET_STATE(xfer)) {
+       case USB_ST_TRANSFERRED:
+               UBT_INFO(sc, "sent %d bytes to bulk-out pipe\n", xfer->actlen);
+               UBT_STAT_BYTES_SENT(sc, xfer->actlen);
+               UBT_STAT_PCKTS_SENT(sc);
+               /* FALLTHROUGH */
+
+       case USB_ST_SETUP:
+send_next:
+               /* Get next mbuf, if any */
+               UBT_NG_LOCK(sc);
+               NG_BT_MBUFQ_DEQUEUE(&sc->sc_aclq, m);
+               UBT_NG_UNLOCK(sc);
+
+               if (m == NULL) {
+                       UBT_INFO(sc, "ACL data queue is empty\n");
+                       break; /* transfer completed */
+               }
+
+               /*
+                * Copy ACL data frame back to a linear USB transfer buffer
+                * and schedule transfer
+                */
+
+               usb2_m_copy_in(xfer->frbuffers, 0, m, 0, m->m_pkthdr.len);
+               xfer->frlengths[0] = m->m_pkthdr.len;
+
+               UBT_INFO(sc, "bulk-out transfer has been started, len=%d\n",
+                       m->m_pkthdr.len);
+
+               NG_FREE_M(m);
+
+               usb2_start_hardware(xfer);
+               break;
+
+       default: /* Error */
+               if (xfer->error != USB_ERR_CANCELLED) {
+                       UBT_WARN(sc, "bulk-out transfer failed: %s\n",
+                               usb2_errstr(xfer->error));
+
+                       UBT_STAT_OERROR(sc);
+
+                       /* try to clear stall first */
+                       xfer->flags.stall_pipe = 1;
+                       goto send_next;
+               }
+                       /* transfer cancelled */
+               break;
+       }
+} /* ubt_bulk_write_callback */
+
+/*
+ * Called when incoming isoc transfer (SCO packet) has completed, i.e.
+ * SCO packet was received from the device.
+ * USB context.
+ */
+
+static void
+ubt_isoc_read_callback(struct usb2_xfer *xfer)
+{
+       struct ubt_softc        *sc = xfer->priv_sc;
+       int                     n;
+
+       switch (USB_GET_STATE(xfer)) {
+       case USB_ST_TRANSFERRED:
+               for (n = 0; n < xfer->nframes; n ++)
+                       if (ubt_isoc_read_one_frame(xfer, n) < 0)
+                               break;
+               /* FALLTHROUGH */
+
+       case USB_ST_SETUP:
+read_next:
+               for (n = 0; n < xfer->nframes; n ++)
+                       xfer->frlengths[n] = xfer->max_frame_size;
+
+               usb2_start_hardware(xfer);
+               break;
+
+       default: /* Error */
+                if (xfer->error != USB_ERR_CANCELLED) {
+                        UBT_STAT_IERROR(sc);
+                        goto read_next;
+                }
+
+               /* transfer cancelled */
+               break;
+       }
+} /* ubt_isoc_read_callback */
+
+/*
+ * Helper function. Called from ubt_isoc_read_callback() to read
+ * SCO data from one frame.
+ * USB context.
+ */
+
+static int
+ubt_isoc_read_one_frame(struct usb2_xfer *xfer, int frame_no)
+{
+       struct ubt_softc        *sc = xfer->priv_sc;
+       struct mbuf             *m;
+       int                     len, want, got;
+
+       /* Get existing SCO reassembly buffer */
+       m = sc->sc_isoc_in_buffer;
+       sc->sc_isoc_in_buffer = NULL;
+
+       /* While we have data in the frame */
+       while ((len = xfer->frlengths[frame_no]) > 0) {
+               if (m == NULL) {
+                       /* Start new reassembly buffer */
+                       MGETHDR(m, M_DONTWAIT, MT_DATA);
+                       if (m == NULL) {
+                               UBT_STAT_IERROR(sc);
+                               return (-1);    /* XXX out of sync! */
+                       }
+
+                       MCLGET(m, M_DONTWAIT);
+                       if (!(m->m_flags & M_EXT)) {
+                               UBT_STAT_IERROR(sc);
+                               NG_FREE_M(m);
+                               return (-1);    /* XXX out of sync! */
+                       }
+
+                       /* Expect SCO header */
+                       *mtod(m, uint8_t *) = NG_HCI_SCO_DATA_PKT;
+                       m->m_pkthdr.len = m->m_len = got = 1;
+                       want = sizeof(ng_hci_scodata_pkt_t);
+               } else {
+                       /*
+                        * Check if we have SCO header and if so 
+                        * adjust amount of data we want
+                        */
+                       got = m->m_pkthdr.len;
+                       want = sizeof(ng_hci_scodata_pkt_t);

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to