1 /* 2 * ng_ubt.c 3 */ 4 5 /*- 6 * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: ng_ubt.c,v 1.16 2003/10/10 19:15:06 max Exp $ 31 * $FreeBSD$ 32 */ 33 34 /* 35 * NOTE: ng_ubt2 driver has a split personality. On one side it is 36 * a USB device driver and on the other it is a Netgraph node. This 37 * driver will *NOT* create traditional /dev/ enties, only Netgraph 38 * node. 39 * 40 * NOTE ON LOCKS USED: ng_ubt2 drives uses 2 locks (mutexes) 41 * 42 * 1) sc_if_mtx - lock for device's interface #0 and #1. This lock is used 43 * by USB for any USB request going over device's interface #0 and #1, 44 * i.e. interrupt, control, bulk and isoc. transfers. 45 * 46 * 2) sc_ng_mtx - this lock is used to protect shared (between USB, Netgraph 47 * and Taskqueue) data, such as outgoing mbuf queues, task flags and hook 48 * pointer. This lock *SHOULD NOT* be grabbed for a long time. In fact, 49 * think of it as a spin lock. 50 * 51 * NOTE ON LOCKING STRATEGY: ng_ubt2 driver operates in 3 different contexts. 52 * 53 * 1) USB context. This is where all the USB related stuff happens. All 54 * callbacks run in this context. All callbacks are called (by USB) with 55 * appropriate interface lock held. It is (generally) allowed to grab 56 * any additional locks. 57 * 58 * 2) Netgraph context. This is where all the Netgraph related stuff happens. 59 * Since we mark node as WRITER, the Netgraph node will be "locked" (from 60 * Netgraph point of view). Any variable that is only modified from the 61 * Netgraph context does not require any additonal locking. It is generally 62 * *NOT* allowed to grab *ANY* additional locks. Whatever you do, *DO NOT* 63 * grab any lock in the Netgraph context that could cause de-scheduling of 64 * the Netgraph thread for significant amount of time. In fact, the only 65 * lock that is allowed in the Netgraph context is the sc_ng_mtx lock. 66 * Also make sure that any code that is called from the Netgraph context 67 * follows the rule above. 68 * 69 * 3) Taskqueue context. This is where ubt_task runs. Since we are generally 70 * NOT allowed to grab any lock that could cause de-scheduling in the 71 * Netgraph context, and, USB requires us to grab interface lock before 72 * doing things with transfers, it is safer to transition from the Netgraph 73 * context to the Taskqueue context before we can call into USB subsystem. 74 * 75 * So, to put everything together, the rules are as follows. 76 * It is OK to call from the USB context or the Taskqueue context into 77 * the Netgraph context (i.e. call NG_SEND_xxx functions). In other words 78 * it is allowed to call into the Netgraph context with locks held. 79 * Is it *NOT* OK to call from the Netgraph context into the USB context, 80 * because USB requires us to grab interface locks, and, it is safer to 81 * avoid it. So, to make things safer we set task flags to indicate which 82 * actions we want to perform and schedule ubt_task which would run in the 83 * Taskqueue context. 84 * Is is OK to call from the Taskqueue context into the USB context, 85 * and, ubt_task does just that (i.e. grabs appropriate interface locks 86 * before calling into USB). 87 * Access to the outgoing queues, task flags and hook pointer is 88 * controlled by the sc_ng_mtx lock. It is an unavoidable evil. Again, 89 * sc_ng_mtx should really be a spin lock (and it is very likely to an 90 * equivalent of spin lock due to adaptive nature of FreeBSD mutexes). 91 * All USB callbacks accept softc pointer as a private data. USB ensures 92 * that this pointer is valid. 93 */ 94 95 #include <sys/stdint.h> 96 #include <sys/stddef.h> 97 #include <sys/param.h> 98 #include <sys/queue.h> 99 #include <sys/types.h> 100 #include <sys/systm.h> 101 #include <sys/kernel.h> 102 #include <sys/bus.h> 103 #include <sys/module.h> 104 #include <sys/lock.h> 105 #include <sys/mutex.h> 106 #include <sys/condvar.h> 107 #include <sys/sysctl.h> 108 #include <sys/sx.h> 109 #include <sys/unistd.h> 110 #include <sys/callout.h> 111 #include <sys/malloc.h> 112 #include <sys/priv.h> 113 114 #include "usbdevs.h" 115 #include <dev/usb/usb.h> 116 #include <dev/usb/usbdi.h> 117 #include <dev/usb/usbdi_util.h> 118 119 #define USB_DEBUG_VAR usb_debug 120 #include <dev/usb/usb_debug.h> 121 #include <dev/usb/usb_busdma.h> 122 123 #include <sys/mbuf.h> 124 #include <sys/taskqueue.h> 125 126 #include <netgraph/ng_message.h> 127 #include <netgraph/netgraph.h> 128 #include <netgraph/ng_parse.h> 129 #include <netgraph/bluetooth/include/ng_bluetooth.h> 130 #include <netgraph/bluetooth/include/ng_hci.h> 131 #include <netgraph/bluetooth/include/ng_ubt.h> 132 #include <netgraph/bluetooth/drivers/ubt/ng_ubt_var.h> 133 134 static int ubt_modevent(module_t, int, void *); 135 static device_probe_t ubt_probe; 136 static device_attach_t ubt_attach; 137 static device_detach_t ubt_detach; 138 139 static void ubt_task_schedule(ubt_softc_p, int); 140 static task_fn_t ubt_task; 141 142 #define ubt_xfer_start(sc, i) usbd_transfer_start((sc)->sc_xfer[(i)]) 143 144 /* Netgraph methods */ 145 static ng_constructor_t ng_ubt_constructor; 146 static ng_shutdown_t ng_ubt_shutdown; 147 static ng_newhook_t ng_ubt_newhook; 148 static ng_connect_t ng_ubt_connect; 149 static ng_disconnect_t ng_ubt_disconnect; 150 static ng_rcvmsg_t ng_ubt_rcvmsg; 151 static ng_rcvdata_t ng_ubt_rcvdata; 152 153 /* Queue length */ 154 static const struct ng_parse_struct_field ng_ubt_node_qlen_type_fields[] = 155 { 156 { "queue", &ng_parse_int32_type, }, 157 { "qlen", &ng_parse_int32_type, }, 158 { NULL, } 159 }; 160 static const struct ng_parse_type ng_ubt_node_qlen_type = 161 { 162 &ng_parse_struct_type, 163 &ng_ubt_node_qlen_type_fields 164 }; 165 166 /* Stat info */ 167 static const struct ng_parse_struct_field ng_ubt_node_stat_type_fields[] = 168 { 169 { "pckts_recv", &ng_parse_uint32_type, }, 170 { "bytes_recv", &ng_parse_uint32_type, }, 171 { "pckts_sent", &ng_parse_uint32_type, }, 172 { "bytes_sent", &ng_parse_uint32_type, }, 173 { "oerrors", &ng_parse_uint32_type, }, 174 { "ierrors", &ng_parse_uint32_type, }, 175 { NULL, } 176 }; 177 static const struct ng_parse_type ng_ubt_node_stat_type = 178 { 179 &ng_parse_struct_type, 180 &ng_ubt_node_stat_type_fields 181 }; 182 183 /* Netgraph node command list */ 184 static const struct ng_cmdlist ng_ubt_cmdlist[] = 185 { 186 { 187 NGM_UBT_COOKIE, 188 NGM_UBT_NODE_SET_DEBUG, 189 "set_debug", 190 &ng_parse_uint16_type, 191 NULL 192 }, 193 { 194 NGM_UBT_COOKIE, 195 NGM_UBT_NODE_GET_DEBUG, 196 "get_debug", 197 NULL, 198 &ng_parse_uint16_type 199 }, 200 { 201 NGM_UBT_COOKIE, 202 NGM_UBT_NODE_SET_QLEN, 203 "set_qlen", 204 &ng_ubt_node_qlen_type, 205 NULL 206 }, 207 { 208 NGM_UBT_COOKIE, 209 NGM_UBT_NODE_GET_QLEN, 210 "get_qlen", 211 &ng_ubt_node_qlen_type, 212 &ng_ubt_node_qlen_type 213 }, 214 { 215 NGM_UBT_COOKIE, 216 NGM_UBT_NODE_GET_STAT, 217 "get_stat", 218 NULL, 219 &ng_ubt_node_stat_type 220 }, 221 { 222 NGM_UBT_COOKIE, 223 NGM_UBT_NODE_RESET_STAT, 224 "reset_stat", 225 NULL, 226 NULL 227 }, 228 { 0, } 229 }; 230 231 /* Netgraph node type */ 232 static struct ng_type typestruct = 233 { 234 .version = NG_ABI_VERSION, 235 .name = NG_UBT_NODE_TYPE, 236 .constructor = ng_ubt_constructor, 237 .rcvmsg = ng_ubt_rcvmsg, 238 .shutdown = ng_ubt_shutdown, 239 .newhook = ng_ubt_newhook, 240 .connect = ng_ubt_connect, 241 .rcvdata = ng_ubt_rcvdata, 242 .disconnect = ng_ubt_disconnect, 243 .cmdlist = ng_ubt_cmdlist 244 }; 245 246 /**************************************************************************** 247 **************************************************************************** 248 ** USB specific 249 **************************************************************************** 250 ****************************************************************************/ 251 252 /* USB methods */ 253 static usb_callback_t ubt_ctrl_write_callback; 254 static usb_callback_t ubt_intr_read_callback; 255 static usb_callback_t ubt_bulk_read_callback; 256 static usb_callback_t ubt_bulk_write_callback; 257 static usb_callback_t ubt_isoc_read_callback; 258 static usb_callback_t ubt_isoc_write_callback; 259 260 static int ubt_fwd_mbuf_up(ubt_softc_p, struct mbuf **); 261 static int ubt_isoc_read_one_frame(struct usb_xfer *, int); 262 263 /* 264 * USB config 265 * 266 * The following desribes usb transfers that could be submitted on USB device. 267 * 268 * Interface 0 on the USB device must present the following endpoints 269 * 1) Interrupt endpoint to receive HCI events 270 * 2) Bulk IN endpoint to receive ACL data 271 * 3) Bulk OUT endpoint to send ACL data 272 * 273 * Interface 1 on the USB device must present the following endpoints 274 * 1) Isochronous IN endpoint to receive SCO data 275 * 2) Isochronous OUT endpoint to send SCO data 276 */ 277 278 static const struct usb_config ubt_config[UBT_N_TRANSFER] = 279 { 280 /* 281 * Interface #0 282 */ 283 284 /* Outgoing bulk transfer - ACL packets */ 285 [UBT_IF_0_BULK_DT_WR] = { 286 .type = UE_BULK, 287 .endpoint = UE_ADDR_ANY, 288 .direction = UE_DIR_OUT, 289 .if_index = 0, 290 .bufsize = UBT_BULK_WRITE_BUFFER_SIZE, 291 .flags = { .pipe_bof = 1, .force_short_xfer = 1, }, 292 .callback = &ubt_bulk_write_callback, 293 }, 294 /* Incoming bulk transfer - ACL packets */ 295 [UBT_IF_0_BULK_DT_RD] = { 296 .type = UE_BULK, 297 .endpoint = UE_ADDR_ANY, 298 .direction = UE_DIR_IN, 299 .if_index = 0, 300 .bufsize = UBT_BULK_READ_BUFFER_SIZE, 301 .flags = { .pipe_bof = 1, .short_xfer_ok = 1, }, 302 .callback = &ubt_bulk_read_callback, 303 }, 304 /* Incoming interrupt transfer - HCI events */ 305 [UBT_IF_0_INTR_DT_RD] = { 306 .type = UE_INTERRUPT, 307 .endpoint = UE_ADDR_ANY, 308 .direction = UE_DIR_IN, 309 .if_index = 0, 310 .flags = { .pipe_bof = 1, .short_xfer_ok = 1, }, 311 .bufsize = UBT_INTR_BUFFER_SIZE, 312 .callback = &ubt_intr_read_callback, 313 }, 314 /* Outgoing control transfer - HCI commands */ 315 [UBT_IF_0_CTRL_DT_WR] = { 316 .type = UE_CONTROL, 317 .endpoint = 0x00, /* control pipe */ 318 .direction = UE_DIR_ANY, 319 .if_index = 0, 320 .bufsize = UBT_CTRL_BUFFER_SIZE, 321 .callback = &ubt_ctrl_write_callback, 322 .timeout = 5000, /* 5 seconds */ 323 }, 324 325 /* 326 * Interface #1 327 */ 328 329 /* Incoming isochronous transfer #1 - SCO packets */ 330 [UBT_IF_1_ISOC_DT_RD1] = { 331 .type = UE_ISOCHRONOUS, 332 .endpoint = UE_ADDR_ANY, 333 .direction = UE_DIR_IN, 334 .if_index = 1, 335 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 336 .frames = UBT_ISOC_NFRAMES, 337 .flags = { .short_xfer_ok = 1, }, 338 .callback = &ubt_isoc_read_callback, 339 }, 340 /* Incoming isochronous transfer #2 - SCO packets */ 341 [UBT_IF_1_ISOC_DT_RD2] = { 342 .type = UE_ISOCHRONOUS, 343 .endpoint = UE_ADDR_ANY, 344 .direction = UE_DIR_IN, 345 .if_index = 1, 346 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 347 .frames = UBT_ISOC_NFRAMES, 348 .flags = { .short_xfer_ok = 1, }, 349 .callback = &ubt_isoc_read_callback, 350 }, 351 /* Outgoing isochronous transfer #1 - SCO packets */ 352 [UBT_IF_1_ISOC_DT_WR1] = { 353 .type = UE_ISOCHRONOUS, 354 .endpoint = UE_ADDR_ANY, 355 .direction = UE_DIR_OUT, 356 .if_index = 1, 357 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 358 .frames = UBT_ISOC_NFRAMES, 359 .flags = { .short_xfer_ok = 1, }, 360 .callback = &ubt_isoc_write_callback, 361 }, 362 /* Outgoing isochronous transfer #2 - SCO packets */ 363 [UBT_IF_1_ISOC_DT_WR2] = { 364 .type = UE_ISOCHRONOUS, 365 .endpoint = UE_ADDR_ANY, 366 .direction = UE_DIR_OUT, 367 .if_index = 1, 368 .bufsize = 0, /* use "wMaxPacketSize * frames" */ 369 .frames = UBT_ISOC_NFRAMES, 370 .flags = { .short_xfer_ok = 1, }, 371 .callback = &ubt_isoc_write_callback, 372 }, 373 }; 374 375 /* 376 * If for some reason device should not be attached then put 377 * VendorID/ProductID pair into the list below. The format is 378 * as follows: 379 * 380 * { USB_VPI(VENDOR_ID, PRODUCT_ID, 0) }, 381 * 382 * where VENDOR_ID and PRODUCT_ID are hex numbers. 383 */ 384 385 static const STRUCT_USB_HOST_ID ubt_ignore_devs[] = 386 { 387 /* AVM USB Bluetooth-Adapter BlueFritz! v1.0 */ 388 { USB_VPI(USB_VENDOR_AVM, 0x2200, 0) }, 389 390 /* Atheros 3011 with sflash firmware */ 391 { USB_VPI(0x0cf3, 0x3002, 0) }, 392 { USB_VPI(0x0cf3, 0xe019, 0) }, 393 { USB_VPI(0x13d3, 0x3304, 0) }, 394 { USB_VPI(0x0930, 0x0215, 0) }, 395 { USB_VPI(0x0489, 0xe03d, 0) }, 396 { USB_VPI(0x0489, 0xe027, 0) }, 397 398 /* Atheros AR9285 Malbec with sflash firmware */ 399 { USB_VPI(0x03f0, 0x311d, 0) }, 400 401 /* Atheros 3012 with sflash firmware */ 402 { USB_VPI(0x0cf3, 0x3004, 0), USB_DEV_BCD_LTEQ(1) }, 403 { USB_VPI(0x0cf3, 0x311d, 0), USB_DEV_BCD_LTEQ(1) }, 404 { USB_VPI(0x13d3, 0x3375, 0), USB_DEV_BCD_LTEQ(1) }, 405 { USB_VPI(0x04ca, 0x3005, 0), USB_DEV_BCD_LTEQ(1) }, 406 { USB_VPI(0x04ca, 0x3006, 0), USB_DEV_BCD_LTEQ(1) }, 407 { USB_VPI(0x04ca, 0x3008, 0), USB_DEV_BCD_LTEQ(1) }, 408 { USB_VPI(0x13d3, 0x3362, 0), USB_DEV_BCD_LTEQ(1) }, 409 { USB_VPI(0x0cf3, 0xe004, 0), USB_DEV_BCD_LTEQ(1) }, 410 { USB_VPI(0x0930, 0x0219, 0), USB_DEV_BCD_LTEQ(1) }, 411 { USB_VPI(0x0489, 0xe057, 0), USB_DEV_BCD_LTEQ(1) }, 412 { USB_VPI(0x13d3, 0x3393, 0), USB_DEV_BCD_LTEQ(1) }, 413 { USB_VPI(0x0489, 0xe04e, 0), USB_DEV_BCD_LTEQ(1) }, 414 { USB_VPI(0x0489, 0xe056, 0), USB_DEV_BCD_LTEQ(1) }, 415 416 /* Atheros AR5BBU12 with sflash firmware */ 417 { USB_VPI(0x0489, 0xe02c, 0), USB_DEV_BCD_LTEQ(1) }, 418 419 /* Atheros AR5BBU12 with sflash firmware */ 420 { USB_VPI(0x0489, 0xe03c, 0), USB_DEV_BCD_LTEQ(1) }, 421 { USB_VPI(0x0489, 0xe036, 0), USB_DEV_BCD_LTEQ(1) }, 422 }; 423 424 /* List of supported bluetooth devices */ 425 static const STRUCT_USB_HOST_ID ubt_devs[] = 426 { 427 /* Generic Bluetooth class devices */ 428 { USB_IFACE_CLASS(UDCLASS_WIRELESS), 429 USB_IFACE_SUBCLASS(UDSUBCLASS_RF), 430 USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) }, 431 432 /* AVM USB Bluetooth-Adapter BlueFritz! v2.0 */ 433 { USB_VPI(USB_VENDOR_AVM, 0x3800, 0) }, 434 435 /* Broadcom USB dongles, mostly BCM20702 and BCM20702A0 */ 436 { USB_VENDOR(USB_VENDOR_BROADCOM), 437 USB_IFACE_CLASS(UICLASS_VENDOR), 438 USB_IFACE_SUBCLASS(UDSUBCLASS_RF), 439 USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) }, 440 441 /* Apple-specific (Broadcom) devices */ 442 { USB_VENDOR(USB_VENDOR_APPLE), 443 USB_IFACE_CLASS(UICLASS_VENDOR), 444 USB_IFACE_SUBCLASS(UDSUBCLASS_RF), 445 USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) }, 446 447 /* Foxconn - Hon Hai */ 448 { USB_VENDOR(USB_VENDOR_FOXCONN), 449 USB_IFACE_CLASS(UICLASS_VENDOR), 450 USB_IFACE_SUBCLASS(UDSUBCLASS_RF), 451 USB_IFACE_PROTOCOL(UDPROTO_BLUETOOTH) }, 452 453 /* MediaTek MT76x0E */ 454 { USB_VPI(USB_VENDOR_MEDIATEK, 0x763f, 0) }, 455 456 /* Broadcom SoftSailing reporting vendor specific */ 457 { USB_VPI(USB_VENDOR_BROADCOM, 0x21e1, 0) }, 458 459 /* Apple MacBookPro 7,1 */ 460 { USB_VPI(USB_VENDOR_APPLE, 0x8213, 0) }, 461 462 /* Apple iMac11,1 */ 463 { USB_VPI(USB_VENDOR_APPLE, 0x8215, 0) }, 464 465 /* Apple MacBookPro6,2 */ 466 { USB_VPI(USB_VENDOR_APPLE, 0x8218, 0) }, 467 468 /* Apple MacBookAir3,1, MacBookAir3,2 */ 469 { USB_VPI(USB_VENDOR_APPLE, 0x821b, 0) }, 470 471 /* Apple MacBookAir4,1 */ 472 { USB_VPI(USB_VENDOR_APPLE, 0x821f, 0) }, 473 474 /* MacBookAir6,1 */ 475 { USB_VPI(USB_VENDOR_APPLE, 0x828f, 0) }, 476 477 /* Apple MacBookPro8,2 */ 478 { USB_VPI(USB_VENDOR_APPLE, 0x821a, 0) }, 479 480 /* Apple MacMini5,1 */ 481 { USB_VPI(USB_VENDOR_APPLE, 0x8281, 0) }, 482 483 /* Bluetooth Ultraport Module from IBM */ 484 { USB_VPI(USB_VENDOR_TDK, 0x030a, 0) }, 485 486 /* ALPS Modules with non-standard ID */ 487 { USB_VPI(USB_VENDOR_ALPS, 0x3001, 0) }, 488 { USB_VPI(USB_VENDOR_ALPS, 0x3002, 0) }, 489 490 { USB_VPI(USB_VENDOR_ERICSSON2, 0x1002, 0) }, 491 492 /* Canyon CN-BTU1 with HID interfaces */ 493 { USB_VPI(USB_VENDOR_CANYON, 0x0000, 0) }, 494 495 /* Broadcom BCM20702A0 */ 496 { USB_VPI(USB_VENDOR_ASUS, 0x17b5, 0) }, 497 { USB_VPI(USB_VENDOR_ASUS, 0x17cb, 0) }, 498 { USB_VPI(USB_VENDOR_LITEON, 0x2003, 0) }, 499 { USB_VPI(USB_VENDOR_FOXCONN, 0xe042, 0) }, 500 { USB_VPI(USB_VENDOR_DELL, 0x8197, 0) }, 501 }; 502 503 /* 504 * Probe for a USB Bluetooth device. 505 * USB context. 506 */ 507 508 static int 509 ubt_probe(device_t dev) 510 { 511 struct usb_attach_arg *uaa = device_get_ivars(dev); 512 int error; 513 514 if (uaa->usb_mode != USB_MODE_HOST) 515 return (ENXIO); 516 517 if (uaa->info.bIfaceIndex != 0) 518 return (ENXIO); 519 520 if (usbd_lookup_id_by_uaa(ubt_ignore_devs, 521 sizeof(ubt_ignore_devs), uaa) == 0) 522 return (ENXIO); 523 524 error = usbd_lookup_id_by_uaa(ubt_devs, sizeof(ubt_devs), uaa); 525 if (error == 0) 526 return (BUS_PROBE_GENERIC); 527 return (error); 528 } /* ubt_probe */ 529 530 /* 531 * Attach the device. 532 * USB context. 533 */ 534 535 static int 536 ubt_attach(device_t dev) 537 { 538 struct usb_attach_arg *uaa = device_get_ivars(dev); 539 struct ubt_softc *sc = device_get_softc(dev); 540 struct usb_endpoint_descriptor *ed; 541 struct usb_interface_descriptor *id; 542 struct usb_interface *iface; 543 uint16_t wMaxPacketSize; 544 uint8_t alt_index, i, j; 545 uint8_t iface_index[2] = { 0, 1 }; 546 547 device_set_usb_desc(dev); 548 549 sc->sc_dev = dev; 550 sc->sc_debug = NG_UBT_WARN_LEVEL; 551 552 /* 553 * Create Netgraph node 554 */ 555 556 if (ng_make_node_common(&typestruct, &sc->sc_node) != 0) { 557 UBT_ALERT(sc, "could not create Netgraph node\n"); 558 return (ENXIO); 559 } 560 561 /* Name Netgraph node */ 562 if (ng_name_node(sc->sc_node, device_get_nameunit(dev)) != 0) { 563 UBT_ALERT(sc, "could not name Netgraph node\n"); 564 NG_NODE_UNREF(sc->sc_node); 565 return (ENXIO); 566 } 567 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 568 NG_NODE_FORCE_WRITER(sc->sc_node); 569 570 /* 571 * Initialize device softc structure 572 */ 573 574 /* initialize locks */ 575 mtx_init(&sc->sc_ng_mtx, "ubt ng", NULL, MTX_DEF); 576 mtx_init(&sc->sc_if_mtx, "ubt if", NULL, MTX_DEF | MTX_RECURSE); 577 578 /* initialize packet queues */ 579 NG_BT_MBUFQ_INIT(&sc->sc_cmdq, UBT_DEFAULT_QLEN); 580 NG_BT_MBUFQ_INIT(&sc->sc_aclq, UBT_DEFAULT_QLEN); 581 NG_BT_MBUFQ_INIT(&sc->sc_scoq, UBT_DEFAULT_QLEN); 582 583 /* initialize glue task */ 584 TASK_INIT(&sc->sc_task, 0, ubt_task, sc); 585 586 /* 587 * Configure Bluetooth USB device. Discover all required USB 588 * interfaces and endpoints. 589 * 590 * USB device must present two interfaces: 591 * 1) Interface 0 that has 3 endpoints 592 * 1) Interrupt endpoint to receive HCI events 593 * 2) Bulk IN endpoint to receive ACL data 594 * 3) Bulk OUT endpoint to send ACL data 595 * 596 * 2) Interface 1 then has 2 endpoints 597 * 1) Isochronous IN endpoint to receive SCO data 598 * 2) Isochronous OUT endpoint to send SCO data 599 * 600 * Interface 1 (with isochronous endpoints) has several alternate 601 * configurations with different packet size. 602 */ 603 604 /* 605 * For interface #1 search alternate settings, and find 606 * the descriptor with the largest wMaxPacketSize 607 */ 608 609 wMaxPacketSize = 0; 610 alt_index = 0; 611 i = 0; 612 j = 0; 613 ed = NULL; 614 615 /* 616 * Search through all the descriptors looking for the largest 617 * packet size: 618 */ 619 while ((ed = (struct usb_endpoint_descriptor *)usb_desc_foreach( 620 usbd_get_config_descriptor(uaa->device), 621 (struct usb_descriptor *)ed))) { 622 623 if ((ed->bDescriptorType == UDESC_INTERFACE) && 624 (ed->bLength >= sizeof(*id))) { 625 id = (struct usb_interface_descriptor *)ed; 626 i = id->bInterfaceNumber; 627 j = id->bAlternateSetting; 628 } 629 630 if ((ed->bDescriptorType == UDESC_ENDPOINT) && 631 (ed->bLength >= sizeof(*ed)) && 632 (i == 1)) { 633 uint16_t temp; 634 635 temp = UGETW(ed->wMaxPacketSize); 636 if (temp > wMaxPacketSize) { 637 wMaxPacketSize = temp; 638 alt_index = j; 639 } 640 } 641 } 642 643 /* Set alt configuration on interface #1 only if we found it */ 644 if (wMaxPacketSize > 0 && 645 usbd_set_alt_interface_index(uaa->device, 1, alt_index)) { 646 UBT_ALERT(sc, "could not set alternate setting %d " \ 647 "for interface 1!\n", alt_index); 648 goto detach; 649 } 650 651 /* Setup transfers for both interfaces */ 652 if (usbd_transfer_setup(uaa->device, iface_index, sc->sc_xfer, 653 ubt_config, UBT_N_TRANSFER, sc, &sc->sc_if_mtx)) { 654 UBT_ALERT(sc, "could not allocate transfers\n"); 655 goto detach; 656 } 657 658 /* Claim all interfaces belonging to the Bluetooth part */ 659 for (i = 1;; i++) { 660 iface = usbd_get_iface(uaa->device, i); 661 if (iface == NULL) 662 break; 663 id = usbd_get_interface_descriptor(iface); 664 665 if ((id != NULL) && 666 (id->bInterfaceClass == UICLASS_WIRELESS) && 667 (id->bInterfaceSubClass == UISUBCLASS_RF) && 668 (id->bInterfaceProtocol == UIPROTO_BLUETOOTH)) { 669 usbd_set_parent_iface(uaa->device, i, 670 uaa->info.bIfaceIndex); 671 } 672 } 673 return (0); /* success */ 674 675 detach: 676 ubt_detach(dev); 677 678 return (ENXIO); 679 } /* ubt_attach */ 680 681 /* 682 * Detach the device. 683 * USB context. 684 */ 685 686 int 687 ubt_detach(device_t dev) 688 { 689 struct ubt_softc *sc = device_get_softc(dev); 690 node_p node = sc->sc_node; 691 692 /* Destroy Netgraph node */ 693 if (node != NULL) { 694 sc->sc_node = NULL; 695 NG_NODE_REALLY_DIE(node); 696 ng_rmnode_self(node); 697 } 698 699 /* Make sure ubt_task in gone */ 700 taskqueue_drain(taskqueue_swi, &sc->sc_task); 701 702 /* Free USB transfers, if any */ 703 usbd_transfer_unsetup(sc->sc_xfer, UBT_N_TRANSFER); 704 705 /* Destroy queues */ 706 UBT_NG_LOCK(sc); 707 NG_BT_MBUFQ_DESTROY(&sc->sc_cmdq); 708 NG_BT_MBUFQ_DESTROY(&sc->sc_aclq); 709 NG_BT_MBUFQ_DESTROY(&sc->sc_scoq); 710 UBT_NG_UNLOCK(sc); 711 712 mtx_destroy(&sc->sc_if_mtx); 713 mtx_destroy(&sc->sc_ng_mtx); 714 715 return (0); 716 } /* ubt_detach */ 717 718 /* 719 * Called when outgoing control request (HCI command) has completed, i.e. 720 * HCI command was sent to the device. 721 * USB context. 722 */ 723 724 static void 725 ubt_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error) 726 { 727 struct ubt_softc *sc = usbd_xfer_softc(xfer); 728 struct usb_device_request req; 729 struct mbuf *m; 730 struct usb_page_cache *pc; 731 int actlen; 732 733 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 734 735 switch (USB_GET_STATE(xfer)) { 736 case USB_ST_TRANSFERRED: 737 UBT_INFO(sc, "sent %d bytes to control pipe\n", actlen); 738 UBT_STAT_BYTES_SENT(sc, actlen); 739 UBT_STAT_PCKTS_SENT(sc); 740 /* FALLTHROUGH */ 741 742 case USB_ST_SETUP: 743 send_next: 744 /* Get next command mbuf, if any */ 745 UBT_NG_LOCK(sc); 746 NG_BT_MBUFQ_DEQUEUE(&sc->sc_cmdq, m); 747 UBT_NG_UNLOCK(sc); 748 749 if (m == NULL) { 750 UBT_INFO(sc, "HCI command queue is empty\n"); 751 break; /* transfer complete */ 752 } 753 754 /* Initialize a USB control request and then schedule it */ 755 bzero(&req, sizeof(req)); 756 req.bmRequestType = UBT_HCI_REQUEST; 757 USETW(req.wLength, m->m_pkthdr.len); 758 759 UBT_INFO(sc, "Sending control request, " \ 760 "bmRequestType=0x%02x, wLength=%d\n", 761 req.bmRequestType, UGETW(req.wLength)); 762 763 pc = usbd_xfer_get_frame(xfer, 0); 764 usbd_copy_in(pc, 0, &req, sizeof(req)); 765 pc = usbd_xfer_get_frame(xfer, 1); 766 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 767 768 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 769 usbd_xfer_set_frame_len(xfer, 1, m->m_pkthdr.len); 770 usbd_xfer_set_frames(xfer, 2); 771 772 NG_FREE_M(m); 773 774 usbd_transfer_submit(xfer); 775 break; 776 777 default: /* Error */ 778 if (error != USB_ERR_CANCELLED) { 779 UBT_WARN(sc, "control transfer failed: %s\n", 780 usbd_errstr(error)); 781 782 UBT_STAT_OERROR(sc); 783 goto send_next; 784 } 785 786 /* transfer cancelled */ 787 break; 788 } 789 } /* ubt_ctrl_write_callback */ 790 791 /* 792 * Called when incoming interrupt transfer (HCI event) has completed, i.e. 793 * HCI event was received from the device. 794 * USB context. 795 */ 796 797 static void 798 ubt_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 799 { 800 struct ubt_softc *sc = usbd_xfer_softc(xfer); 801 struct mbuf *m; 802 ng_hci_event_pkt_t *hdr; 803 struct usb_page_cache *pc; 804 int actlen; 805 806 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 807 808 m = NULL; 809 810 switch (USB_GET_STATE(xfer)) { 811 case USB_ST_TRANSFERRED: 812 /* Allocate a new mbuf */ 813 MGETHDR(m, M_NOWAIT, MT_DATA); 814 if (m == NULL) { 815 UBT_STAT_IERROR(sc); 816 goto submit_next; 817 } 818 819 if (!(MCLGET(m, M_NOWAIT))) { 820 UBT_STAT_IERROR(sc); 821 goto submit_next; 822 } 823 824 /* Add HCI packet type */ 825 *mtod(m, uint8_t *)= NG_HCI_EVENT_PKT; 826 m->m_pkthdr.len = m->m_len = 1; 827 828 if (actlen > MCLBYTES - 1) 829 actlen = MCLBYTES - 1; 830 831 pc = usbd_xfer_get_frame(xfer, 0); 832 usbd_copy_out(pc, 0, mtod(m, uint8_t *) + 1, actlen); 833 m->m_pkthdr.len += actlen; 834 m->m_len += actlen; 835 836 UBT_INFO(sc, "got %d bytes from interrupt pipe\n", 837 actlen); 838 839 /* Validate packet and send it up the stack */ 840 if (m->m_pkthdr.len < (int)sizeof(*hdr)) { 841 UBT_INFO(sc, "HCI event packet is too short\n"); 842 843 UBT_STAT_IERROR(sc); 844 goto submit_next; 845 } 846 847 hdr = mtod(m, ng_hci_event_pkt_t *); 848 if (hdr->length != (m->m_pkthdr.len - sizeof(*hdr))) { 849 UBT_ERR(sc, "Invalid HCI event packet size, " \ 850 "length=%d, pktlen=%d\n", 851 hdr->length, m->m_pkthdr.len); 852 853 UBT_STAT_IERROR(sc); 854 goto submit_next; 855 } 856 857 UBT_INFO(sc, "got complete HCI event frame, pktlen=%d, " \ 858 "length=%d\n", m->m_pkthdr.len, hdr->length); 859 860 UBT_STAT_PCKTS_RECV(sc); 861 UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len); 862 863 ubt_fwd_mbuf_up(sc, &m); 864 /* m == NULL at this point */ 865 /* FALLTHROUGH */ 866 867 case USB_ST_SETUP: 868 submit_next: 869 NG_FREE_M(m); /* checks for m != NULL */ 870 871 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 872 usbd_transfer_submit(xfer); 873 break; 874 875 default: /* Error */ 876 if (error != USB_ERR_CANCELLED) { 877 UBT_WARN(sc, "interrupt transfer failed: %s\n", 878 usbd_errstr(error)); 879 880 /* Try to clear stall first */ 881 usbd_xfer_set_stall(xfer); 882 goto submit_next; 883 } 884 /* transfer cancelled */ 885 break; 886 } 887 } /* ubt_intr_read_callback */ 888 889 /* 890 * Called when incoming bulk transfer (ACL packet) has completed, i.e. 891 * ACL packet was received from the device. 892 * USB context. 893 */ 894 895 static void 896 ubt_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 897 { 898 struct ubt_softc *sc = usbd_xfer_softc(xfer); 899 struct mbuf *m; 900 ng_hci_acldata_pkt_t *hdr; 901 struct usb_page_cache *pc; 902 int len; 903 int actlen; 904 905 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 906 907 m = NULL; 908 909 switch (USB_GET_STATE(xfer)) { 910 case USB_ST_TRANSFERRED: 911 /* Allocate new mbuf */ 912 MGETHDR(m, M_NOWAIT, MT_DATA); 913 if (m == NULL) { 914 UBT_STAT_IERROR(sc); 915 goto submit_next; 916 } 917 918 if (!(MCLGET(m, M_NOWAIT))) { 919 UBT_STAT_IERROR(sc); 920 goto submit_next; 921 } 922 923 /* Add HCI packet type */ 924 *mtod(m, uint8_t *)= NG_HCI_ACL_DATA_PKT; 925 m->m_pkthdr.len = m->m_len = 1; 926 927 if (actlen > MCLBYTES - 1) 928 actlen = MCLBYTES - 1; 929 930 pc = usbd_xfer_get_frame(xfer, 0); 931 usbd_copy_out(pc, 0, mtod(m, uint8_t *) + 1, actlen); 932 m->m_pkthdr.len += actlen; 933 m->m_len += actlen; 934 935 UBT_INFO(sc, "got %d bytes from bulk-in pipe\n", 936 actlen); 937 938 /* Validate packet and send it up the stack */ 939 if (m->m_pkthdr.len < (int)sizeof(*hdr)) { 940 UBT_INFO(sc, "HCI ACL packet is too short\n"); 941 942 UBT_STAT_IERROR(sc); 943 goto submit_next; 944 } 945 946 hdr = mtod(m, ng_hci_acldata_pkt_t *); 947 len = le16toh(hdr->length); 948 if (len != (int)(m->m_pkthdr.len - sizeof(*hdr))) { 949 UBT_ERR(sc, "Invalid ACL packet size, length=%d, " \ 950 "pktlen=%d\n", len, m->m_pkthdr.len); 951 952 UBT_STAT_IERROR(sc); 953 goto submit_next; 954 } 955 956 UBT_INFO(sc, "got complete ACL data packet, pktlen=%d, " \ 957 "length=%d\n", m->m_pkthdr.len, len); 958 959 UBT_STAT_PCKTS_RECV(sc); 960 UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len); 961 962 ubt_fwd_mbuf_up(sc, &m); 963 /* m == NULL at this point */ 964 /* FALLTHOUGH */ 965 966 case USB_ST_SETUP: 967 submit_next: 968 NG_FREE_M(m); /* checks for m != NULL */ 969 970 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 971 usbd_transfer_submit(xfer); 972 break; 973 974 default: /* Error */ 975 if (error != USB_ERR_CANCELLED) { 976 UBT_WARN(sc, "bulk-in transfer failed: %s\n", 977 usbd_errstr(error)); 978 979 /* Try to clear stall first */ 980 usbd_xfer_set_stall(xfer); 981 goto submit_next; 982 } 983 /* transfer cancelled */ 984 break; 985 } 986 } /* ubt_bulk_read_callback */ 987 988 /* 989 * Called when outgoing bulk transfer (ACL packet) has completed, i.e. 990 * ACL packet was sent to the device. 991 * USB context. 992 */ 993 994 static void 995 ubt_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 996 { 997 struct ubt_softc *sc = usbd_xfer_softc(xfer); 998 struct mbuf *m; 999 struct usb_page_cache *pc; 1000 int actlen; 1001 1002 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1003 1004 switch (USB_GET_STATE(xfer)) { 1005 case USB_ST_TRANSFERRED: 1006 UBT_INFO(sc, "sent %d bytes to bulk-out pipe\n", actlen); 1007 UBT_STAT_BYTES_SENT(sc, actlen); 1008 UBT_STAT_PCKTS_SENT(sc); 1009 /* FALLTHROUGH */ 1010 1011 case USB_ST_SETUP: 1012 send_next: 1013 /* Get next mbuf, if any */ 1014 UBT_NG_LOCK(sc); 1015 NG_BT_MBUFQ_DEQUEUE(&sc->sc_aclq, m); 1016 UBT_NG_UNLOCK(sc); 1017 1018 if (m == NULL) { 1019 UBT_INFO(sc, "ACL data queue is empty\n"); 1020 break; /* transfer completed */ 1021 } 1022 1023 /* 1024 * Copy ACL data frame back to a linear USB transfer buffer 1025 * and schedule transfer 1026 */ 1027 1028 pc = usbd_xfer_get_frame(xfer, 0); 1029 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 1030 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 1031 1032 UBT_INFO(sc, "bulk-out transfer has been started, len=%d\n", 1033 m->m_pkthdr.len); 1034 1035 NG_FREE_M(m); 1036 1037 usbd_transfer_submit(xfer); 1038 break; 1039 1040 default: /* Error */ 1041 if (error != USB_ERR_CANCELLED) { 1042 UBT_WARN(sc, "bulk-out transfer failed: %s\n", 1043 usbd_errstr(error)); 1044 1045 UBT_STAT_OERROR(sc); 1046 1047 /* try to clear stall first */ 1048 usbd_xfer_set_stall(xfer); 1049 goto send_next; 1050 } 1051 /* transfer cancelled */ 1052 break; 1053 } 1054 } /* ubt_bulk_write_callback */ 1055 1056 /* 1057 * Called when incoming isoc transfer (SCO packet) has completed, i.e. 1058 * SCO packet was received from the device. 1059 * USB context. 1060 */ 1061 1062 static void 1063 ubt_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error) 1064 { 1065 struct ubt_softc *sc = usbd_xfer_softc(xfer); 1066 int n; 1067 int actlen, nframes; 1068 1069 usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes); 1070 1071 switch (USB_GET_STATE(xfer)) { 1072 case USB_ST_TRANSFERRED: 1073 for (n = 0; n < nframes; n ++) 1074 if (ubt_isoc_read_one_frame(xfer, n) < 0) 1075 break; 1076 /* FALLTHROUGH */ 1077 1078 case USB_ST_SETUP: 1079 read_next: 1080 for (n = 0; n < nframes; n ++) 1081 usbd_xfer_set_frame_len(xfer, n, 1082 usbd_xfer_max_framelen(xfer)); 1083 1084 usbd_transfer_submit(xfer); 1085 break; 1086 1087 default: /* Error */ 1088 if (error != USB_ERR_CANCELLED) { 1089 UBT_STAT_IERROR(sc); 1090 goto read_next; 1091 } 1092 1093 /* transfer cancelled */ 1094 break; 1095 } 1096 } /* ubt_isoc_read_callback */ 1097 1098 /* 1099 * Helper function. Called from ubt_isoc_read_callback() to read 1100 * SCO data from one frame. 1101 * USB context. 1102 */ 1103 1104 static int 1105 ubt_isoc_read_one_frame(struct usb_xfer *xfer, int frame_no) 1106 { 1107 struct ubt_softc *sc = usbd_xfer_softc(xfer); 1108 struct usb_page_cache *pc; 1109 struct mbuf *m; 1110 int len, want, got, total; 1111 1112 /* Get existing SCO reassembly buffer */ 1113 pc = usbd_xfer_get_frame(xfer, 0); 1114 m = sc->sc_isoc_in_buffer; 1115 total = usbd_xfer_frame_len(xfer, frame_no); 1116 1117 /* While we have data in the frame */ 1118 while (total > 0) { 1119 if (m == NULL) { 1120 /* Start new reassembly buffer */ 1121 MGETHDR(m, M_NOWAIT, MT_DATA); 1122 if (m == NULL) { 1123 UBT_STAT_IERROR(sc); 1124 return (-1); /* XXX out of sync! */ 1125 } 1126 1127 if (!(MCLGET(m, M_NOWAIT))) { 1128 UBT_STAT_IERROR(sc); 1129 NG_FREE_M(m); 1130 return (-1); /* XXX out of sync! */ 1131 } 1132 1133 /* Expect SCO header */ 1134 *mtod(m, uint8_t *) = NG_HCI_SCO_DATA_PKT; 1135 m->m_pkthdr.len = m->m_len = got = 1; 1136 want = sizeof(ng_hci_scodata_pkt_t); 1137 } else { 1138 /* 1139 * Check if we have SCO header and if so 1140 * adjust amount of data we want 1141 */ 1142 got = m->m_pkthdr.len; 1143 want = sizeof(ng_hci_scodata_pkt_t); 1144 1145 if (got >= want) 1146 want += mtod(m, ng_hci_scodata_pkt_t *)->length; 1147 } 1148 1149 /* Append frame data to the SCO reassembly buffer */ 1150 len = total; 1151 if (got + len > want) 1152 len = want - got; 1153 1154 usbd_copy_out(pc, frame_no * usbd_xfer_max_framelen(xfer), 1155 mtod(m, uint8_t *) + m->m_pkthdr.len, len); 1156 1157 m->m_pkthdr.len += len; 1158 m->m_len += len; 1159 total -= len; 1160 1161 /* Check if we got everything we wanted, if not - continue */ 1162 if (got != want) 1163 continue; 1164 1165 /* If we got here then we got complete SCO frame */ 1166 UBT_INFO(sc, "got complete SCO data frame, pktlen=%d, " \ 1167 "length=%d\n", m->m_pkthdr.len, 1168 mtod(m, ng_hci_scodata_pkt_t *)->length); 1169 1170 UBT_STAT_PCKTS_RECV(sc); 1171 UBT_STAT_BYTES_RECV(sc, m->m_pkthdr.len); 1172 1173 ubt_fwd_mbuf_up(sc, &m); 1174 /* m == NULL at this point */ 1175 } 1176 1177 /* Put SCO reassembly buffer back */ 1178 sc->sc_isoc_in_buffer = m; 1179 1180 return (0); 1181 } /* ubt_isoc_read_one_frame */ 1182 1183 /* 1184 * Called when outgoing isoc transfer (SCO packet) has completed, i.e. 1185 * SCO packet was sent to the device. 1186 * USB context. 1187 */ 1188 1189 static void 1190 ubt_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error) 1191 { 1192 struct ubt_softc *sc = usbd_xfer_softc(xfer); 1193 struct usb_page_cache *pc; 1194 struct mbuf *m; 1195 int n, space, offset; 1196 int actlen, nframes; 1197 1198 usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes); 1199 pc = usbd_xfer_get_frame(xfer, 0); 1200 1201 switch (USB_GET_STATE(xfer)) { 1202 case USB_ST_TRANSFERRED: 1203 UBT_INFO(sc, "sent %d bytes to isoc-out pipe\n", actlen); 1204 UBT_STAT_BYTES_SENT(sc, actlen); 1205 UBT_STAT_PCKTS_SENT(sc); 1206 /* FALLTHROUGH */ 1207 1208 case USB_ST_SETUP: 1209 send_next: 1210 offset = 0; 1211 space = usbd_xfer_max_framelen(xfer) * nframes; 1212 m = NULL; 1213 1214 while (space > 0) { 1215 if (m == NULL) { 1216 UBT_NG_LOCK(sc); 1217 NG_BT_MBUFQ_DEQUEUE(&sc->sc_scoq, m); 1218 UBT_NG_UNLOCK(sc); 1219 1220 if (m == NULL) 1221 break; 1222 } 1223 1224 n = min(space, m->m_pkthdr.len); 1225 if (n > 0) { 1226 usbd_m_copy_in(pc, offset, m,0, n); 1227 m_adj(m, n); 1228 1229 offset += n; 1230 space -= n; 1231 } 1232 1233 if (m->m_pkthdr.len == 0) 1234 NG_FREE_M(m); /* sets m = NULL */ 1235 } 1236 1237 /* Put whatever is left from mbuf back on queue */ 1238 if (m != NULL) { 1239 UBT_NG_LOCK(sc); 1240 NG_BT_MBUFQ_PREPEND(&sc->sc_scoq, m); 1241 UBT_NG_UNLOCK(sc); 1242 } 1243 1244 /* 1245 * Calculate sizes for isoc frames. 1246 * Note that offset could be 0 at this point (i.e. we have 1247 * nothing to send). That is fine, as we have isoc. transfers 1248 * going in both directions all the time. In this case it 1249 * would be just empty isoc. transfer. 1250 */ 1251 1252 for (n = 0; n < nframes; n ++) { 1253 usbd_xfer_set_frame_len(xfer, n, 1254 min(offset, usbd_xfer_max_framelen(xfer))); 1255 offset -= usbd_xfer_frame_len(xfer, n); 1256 } 1257 1258 usbd_transfer_submit(xfer); 1259 break; 1260 1261 default: /* Error */ 1262 if (error != USB_ERR_CANCELLED) { 1263 UBT_STAT_OERROR(sc); 1264 goto send_next; 1265 } 1266 1267 /* transfer cancelled */ 1268 break; 1269 } 1270 } 1271 1272 /* 1273 * Utility function to forward provided mbuf upstream (i.e. up the stack). 1274 * Modifies value of the mbuf pointer (sets it to NULL). 1275 * Save to call from any context. 1276 */ 1277 1278 static int 1279 ubt_fwd_mbuf_up(ubt_softc_p sc, struct mbuf **m) 1280 { 1281 hook_p hook; 1282 int error; 1283 1284 /* 1285 * Close the race with Netgraph hook newhook/disconnect methods. 1286 * Save the hook pointer atomically. Two cases are possible: 1287 * 1288 * 1) The hook pointer is NULL. It means disconnect method got 1289 * there first. In this case we are done. 1290 * 1291 * 2) The hook pointer is not NULL. It means that hook pointer 1292 * could be either in valid or invalid (i.e. in the process 1293 * of disconnect) state. In any case grab an extra reference 1294 * to protect the hook pointer. 1295 * 1296 * It is ok to pass hook in invalid state to NG_SEND_DATA_ONLY() as 1297 * it checks for it. Drop extra reference after NG_SEND_DATA_ONLY(). 1298 */ 1299 1300 UBT_NG_LOCK(sc); 1301 if ((hook = sc->sc_hook) != NULL) 1302 NG_HOOK_REF(hook); 1303 UBT_NG_UNLOCK(sc); 1304 1305 if (hook == NULL) { 1306 NG_FREE_M(*m); 1307 return (ENETDOWN); 1308 } 1309 1310 NG_SEND_DATA_ONLY(error, hook, *m); 1311 NG_HOOK_UNREF(hook); 1312 1313 if (error != 0) 1314 UBT_STAT_IERROR(sc); 1315 1316 return (error); 1317 } /* ubt_fwd_mbuf_up */ 1318 1319 /**************************************************************************** 1320 **************************************************************************** 1321 ** Glue 1322 **************************************************************************** 1323 ****************************************************************************/ 1324 1325 /* 1326 * Schedule glue task. Should be called with sc_ng_mtx held. 1327 * Netgraph context. 1328 */ 1329 1330 static void 1331 ubt_task_schedule(ubt_softc_p sc, int action) 1332 { 1333 mtx_assert(&sc->sc_ng_mtx, MA_OWNED); 1334 1335 /* 1336 * Try to handle corner case when "start all" and "stop all" 1337 * actions can both be set before task is executed. 1338 * 1339 * The rules are 1340 * 1341 * sc_task_flags action new sc_task_flags 1342 * ------------------------------------------------------ 1343 * 0 start start 1344 * 0 stop stop 1345 * start start start 1346 * start stop stop 1347 * stop start stop|start 1348 * stop stop stop 1349 * stop|start start stop|start 1350 * stop|start stop stop 1351 */ 1352 1353 if (action != 0) { 1354 if ((action & UBT_FLAG_T_STOP_ALL) != 0) 1355 sc->sc_task_flags &= ~UBT_FLAG_T_START_ALL; 1356 1357 sc->sc_task_flags |= action; 1358 } 1359 1360 if (sc->sc_task_flags & UBT_FLAG_T_PENDING) 1361 return; 1362 1363 if (taskqueue_enqueue(taskqueue_swi, &sc->sc_task) == 0) { 1364 sc->sc_task_flags |= UBT_FLAG_T_PENDING; 1365 return; 1366 } 1367 1368 /* XXX: i think this should never happen */ 1369 } /* ubt_task_schedule */ 1370 1371 /* 1372 * Glue task. Examines sc_task_flags and does things depending on it. 1373 * Taskqueue context. 1374 */ 1375 1376 static void 1377 ubt_task(void *context, int pending) 1378 { 1379 ubt_softc_p sc = context; 1380 int task_flags, i; 1381 1382 UBT_NG_LOCK(sc); 1383 task_flags = sc->sc_task_flags; 1384 sc->sc_task_flags = 0; 1385 UBT_NG_UNLOCK(sc); 1386 1387 /* 1388 * Stop all USB transfers synchronously. 1389 * Stop interface #0 and #1 transfers at the same time and in the 1390 * same loop. usbd_transfer_drain() will do appropriate locking. 1391 */ 1392 1393 if (task_flags & UBT_FLAG_T_STOP_ALL) 1394 for (i = 0; i < UBT_N_TRANSFER; i ++) 1395 usbd_transfer_drain(sc->sc_xfer[i]); 1396 1397 /* Start incoming interrupt and bulk, and all isoc. USB transfers */ 1398 if (task_flags & UBT_FLAG_T_START_ALL) { 1399 /* 1400 * Interface #0 1401 */ 1402 1403 mtx_lock(&sc->sc_if_mtx); 1404 1405 ubt_xfer_start(sc, UBT_IF_0_INTR_DT_RD); 1406 ubt_xfer_start(sc, UBT_IF_0_BULK_DT_RD); 1407 1408 /* 1409 * Interface #1 1410 * Start both read and write isoc. transfers by default. 1411 * Get them going all the time even if we have nothing 1412 * to send to avoid any delays. 1413 */ 1414 1415 ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_RD1); 1416 ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_RD2); 1417 ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_WR1); 1418 ubt_xfer_start(sc, UBT_IF_1_ISOC_DT_WR2); 1419 1420 mtx_unlock(&sc->sc_if_mtx); 1421 } 1422 1423 /* Start outgoing control transfer */ 1424 if (task_flags & UBT_FLAG_T_START_CTRL) { 1425 mtx_lock(&sc->sc_if_mtx); 1426 ubt_xfer_start(sc, UBT_IF_0_CTRL_DT_WR); 1427 mtx_unlock(&sc->sc_if_mtx); 1428 } 1429 1430 /* Start outgoing bulk transfer */ 1431 if (task_flags & UBT_FLAG_T_START_BULK) { 1432 mtx_lock(&sc->sc_if_mtx); 1433 ubt_xfer_start(sc, UBT_IF_0_BULK_DT_WR); 1434 mtx_unlock(&sc->sc_if_mtx); 1435 } 1436 } /* ubt_task */ 1437 1438 /**************************************************************************** 1439 **************************************************************************** 1440 ** Netgraph specific 1441 **************************************************************************** 1442 ****************************************************************************/ 1443 1444 /* 1445 * Netgraph node constructor. Do not allow to create node of this type. 1446 * Netgraph context. 1447 */ 1448 1449 static int 1450 ng_ubt_constructor(node_p node) 1451 { 1452 return (EINVAL); 1453 } /* ng_ubt_constructor */ 1454 1455 /* 1456 * Netgraph node destructor. Destroy node only when device has been detached. 1457 * Netgraph context. 1458 */ 1459 1460 static int 1461 ng_ubt_shutdown(node_p node) 1462 { 1463 if (node->nd_flags & NGF_REALLY_DIE) { 1464 /* 1465 * We came here because the USB device is being 1466 * detached, so stop being persistant. 1467 */ 1468 NG_NODE_SET_PRIVATE(node, NULL); 1469 NG_NODE_UNREF(node); 1470 } else 1471 NG_NODE_REVIVE(node); /* tell ng_rmnode we are persisant */ 1472 1473 return (0); 1474 } /* ng_ubt_shutdown */ 1475 1476 /* 1477 * Create new hook. There can only be one. 1478 * Netgraph context. 1479 */ 1480 1481 static int 1482 ng_ubt_newhook(node_p node, hook_p hook, char const *name) 1483 { 1484 struct ubt_softc *sc = NG_NODE_PRIVATE(node); 1485 1486 if (strcmp(name, NG_UBT_HOOK) != 0) 1487 return (EINVAL); 1488 1489 UBT_NG_LOCK(sc); 1490 if (sc->sc_hook != NULL) { 1491 UBT_NG_UNLOCK(sc); 1492 1493 return (EISCONN); 1494 } 1495 1496 sc->sc_hook = hook; 1497 UBT_NG_UNLOCK(sc); 1498 1499 return (0); 1500 } /* ng_ubt_newhook */ 1501 1502 /* 1503 * Connect hook. Start incoming USB transfers. 1504 * Netgraph context. 1505 */ 1506 1507 static int 1508 ng_ubt_connect(hook_p hook) 1509 { 1510 struct ubt_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 1511 1512 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 1513 1514 UBT_NG_LOCK(sc); 1515 ubt_task_schedule(sc, UBT_FLAG_T_START_ALL); 1516 UBT_NG_UNLOCK(sc); 1517 1518 return (0); 1519 } /* ng_ubt_connect */ 1520 1521 /* 1522 * Disconnect hook. 1523 * Netgraph context. 1524 */ 1525 1526 static int 1527 ng_ubt_disconnect(hook_p hook) 1528 { 1529 struct ubt_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 1530 1531 UBT_NG_LOCK(sc); 1532 1533 if (hook != sc->sc_hook) { 1534 UBT_NG_UNLOCK(sc); 1535 1536 return (EINVAL); 1537 } 1538 1539 sc->sc_hook = NULL; 1540 1541 /* Kick off task to stop all USB xfers */ 1542 ubt_task_schedule(sc, UBT_FLAG_T_STOP_ALL); 1543 1544 /* Drain queues */ 1545 NG_BT_MBUFQ_DRAIN(&sc->sc_cmdq); 1546 NG_BT_MBUFQ_DRAIN(&sc->sc_aclq); 1547 NG_BT_MBUFQ_DRAIN(&sc->sc_scoq); 1548 1549 UBT_NG_UNLOCK(sc); 1550 1551 return (0); 1552 } /* ng_ubt_disconnect */ 1553 1554 /* 1555 * Process control message. 1556 * Netgraph context. 1557 */ 1558 1559 static int 1560 ng_ubt_rcvmsg(node_p node, item_p item, hook_p lasthook) 1561 { 1562 struct ubt_softc *sc = NG_NODE_PRIVATE(node); 1563 struct ng_mesg *msg, *rsp = NULL; 1564 struct ng_bt_mbufq *q; 1565 int error = 0, queue, qlen; 1566 1567 NGI_GET_MSG(item, msg); 1568 1569 switch (msg->header.typecookie) { 1570 case NGM_GENERIC_COOKIE: 1571 switch (msg->header.cmd) { 1572 case NGM_TEXT_STATUS: 1573 NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT); 1574 if (rsp == NULL) { 1575 error = ENOMEM; 1576 break; 1577 } 1578 1579 snprintf(rsp->data, NG_TEXTRESPONSE, 1580 "Hook: %s\n" \ 1581 "Task flags: %#x\n" \ 1582 "Debug: %d\n" \ 1583 "CMD queue: [have:%d,max:%d]\n" \ 1584 "ACL queue: [have:%d,max:%d]\n" \ 1585 "SCO queue: [have:%d,max:%d]", 1586 (sc->sc_hook != NULL) ? NG_UBT_HOOK : "", 1587 sc->sc_task_flags, 1588 sc->sc_debug, 1589 sc->sc_cmdq.len, 1590 sc->sc_cmdq.maxlen, 1591 sc->sc_aclq.len, 1592 sc->sc_aclq.maxlen, 1593 sc->sc_scoq.len, 1594 sc->sc_scoq.maxlen); 1595 break; 1596 1597 default: 1598 error = EINVAL; 1599 break; 1600 } 1601 break; 1602 1603 case NGM_UBT_COOKIE: 1604 switch (msg->header.cmd) { 1605 case NGM_UBT_NODE_SET_DEBUG: 1606 if (msg->header.arglen != sizeof(ng_ubt_node_debug_ep)){ 1607 error = EMSGSIZE; 1608 break; 1609 } 1610 1611 sc->sc_debug = *((ng_ubt_node_debug_ep *) (msg->data)); 1612 break; 1613 1614 case NGM_UBT_NODE_GET_DEBUG: 1615 NG_MKRESPONSE(rsp, msg, sizeof(ng_ubt_node_debug_ep), 1616 M_NOWAIT); 1617 if (rsp == NULL) { 1618 error = ENOMEM; 1619 break; 1620 } 1621 1622 *((ng_ubt_node_debug_ep *) (rsp->data)) = sc->sc_debug; 1623 break; 1624 1625 case NGM_UBT_NODE_SET_QLEN: 1626 if (msg->header.arglen != sizeof(ng_ubt_node_qlen_ep)) { 1627 error = EMSGSIZE; 1628 break; 1629 } 1630 1631 queue = ((ng_ubt_node_qlen_ep *) (msg->data))->queue; 1632 qlen = ((ng_ubt_node_qlen_ep *) (msg->data))->qlen; 1633 1634 switch (queue) { 1635 case NGM_UBT_NODE_QUEUE_CMD: 1636 q = &sc->sc_cmdq; 1637 break; 1638 1639 case NGM_UBT_NODE_QUEUE_ACL: 1640 q = &sc->sc_aclq; 1641 break; 1642 1643 case NGM_UBT_NODE_QUEUE_SCO: 1644 q = &sc->sc_scoq; 1645 break; 1646 1647 default: 1648 error = EINVAL; 1649 goto done; 1650 /* NOT REACHED */ 1651 } 1652 1653 q->maxlen = qlen; 1654 break; 1655 1656 case NGM_UBT_NODE_GET_QLEN: 1657 if (msg->header.arglen != sizeof(ng_ubt_node_qlen_ep)) { 1658 error = EMSGSIZE; 1659 break; 1660 } 1661 1662 queue = ((ng_ubt_node_qlen_ep *) (msg->data))->queue; 1663 1664 switch (queue) { 1665 case NGM_UBT_NODE_QUEUE_CMD: 1666 q = &sc->sc_cmdq; 1667 break; 1668 1669 case NGM_UBT_NODE_QUEUE_ACL: 1670 q = &sc->sc_aclq; 1671 break; 1672 1673 case NGM_UBT_NODE_QUEUE_SCO: 1674 q = &sc->sc_scoq; 1675 break; 1676 1677 default: 1678 error = EINVAL; 1679 goto done; 1680 /* NOT REACHED */ 1681 } 1682 1683 NG_MKRESPONSE(rsp, msg, sizeof(ng_ubt_node_qlen_ep), 1684 M_NOWAIT); 1685 if (rsp == NULL) { 1686 error = ENOMEM; 1687 break; 1688 } 1689 1690 ((ng_ubt_node_qlen_ep *) (rsp->data))->queue = queue; 1691 ((ng_ubt_node_qlen_ep *) (rsp->data))->qlen = q->maxlen; 1692 break; 1693 1694 case NGM_UBT_NODE_GET_STAT: 1695 NG_MKRESPONSE(rsp, msg, sizeof(ng_ubt_node_stat_ep), 1696 M_NOWAIT); 1697 if (rsp == NULL) { 1698 error = ENOMEM; 1699 break; 1700 } 1701 1702 bcopy(&sc->sc_stat, rsp->data, 1703 sizeof(ng_ubt_node_stat_ep)); 1704 break; 1705 1706 case NGM_UBT_NODE_RESET_STAT: 1707 UBT_STAT_RESET(sc); 1708 break; 1709 1710 default: 1711 error = EINVAL; 1712 break; 1713 } 1714 break; 1715 1716 default: 1717 error = EINVAL; 1718 break; 1719 } 1720 done: 1721 NG_RESPOND_MSG(error, node, item, rsp); 1722 NG_FREE_MSG(msg); 1723 1724 return (error); 1725 } /* ng_ubt_rcvmsg */ 1726 1727 /* 1728 * Process data. 1729 * Netgraph context. 1730 */ 1731 1732 static int 1733 ng_ubt_rcvdata(hook_p hook, item_p item) 1734 { 1735 struct ubt_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 1736 struct mbuf *m; 1737 struct ng_bt_mbufq *q; 1738 int action, error = 0; 1739 1740 if (hook != sc->sc_hook) { 1741 error = EINVAL; 1742 goto done; 1743 } 1744 1745 /* Deatch mbuf and get HCI frame type */ 1746 NGI_GET_M(item, m); 1747 1748 /* 1749 * Minimal size of the HCI frame is 4 bytes: 1 byte frame type, 1750 * 2 bytes connection handle and at least 1 byte of length. 1751 * Panic on data frame that has size smaller than 4 bytes (it 1752 * should not happen) 1753 */ 1754 1755 if (m->m_pkthdr.len < 4) 1756 panic("HCI frame size is too small! pktlen=%d\n", 1757 m->m_pkthdr.len); 1758 1759 /* Process HCI frame */ 1760 switch (*mtod(m, uint8_t *)) { /* XXX call m_pullup ? */ 1761 case NG_HCI_CMD_PKT: 1762 if (m->m_pkthdr.len - 1 > (int)UBT_CTRL_BUFFER_SIZE) 1763 panic("HCI command frame size is too big! " \ 1764 "buffer size=%zd, packet len=%d\n", 1765 UBT_CTRL_BUFFER_SIZE, m->m_pkthdr.len); 1766 1767 q = &sc->sc_cmdq; 1768 action = UBT_FLAG_T_START_CTRL; 1769 break; 1770 1771 case NG_HCI_ACL_DATA_PKT: 1772 if (m->m_pkthdr.len - 1 > UBT_BULK_WRITE_BUFFER_SIZE) 1773 panic("ACL data frame size is too big! " \ 1774 "buffer size=%d, packet len=%d\n", 1775 UBT_BULK_WRITE_BUFFER_SIZE, m->m_pkthdr.len); 1776 1777 q = &sc->sc_aclq; 1778 action = UBT_FLAG_T_START_BULK; 1779 break; 1780 1781 case NG_HCI_SCO_DATA_PKT: 1782 q = &sc->sc_scoq; 1783 action = 0; 1784 break; 1785 1786 default: 1787 UBT_ERR(sc, "Dropping unsupported HCI frame, type=0x%02x, " \ 1788 "pktlen=%d\n", *mtod(m, uint8_t *), m->m_pkthdr.len); 1789 1790 NG_FREE_M(m); 1791 error = EINVAL; 1792 goto done; 1793 /* NOT REACHED */ 1794 } 1795 1796 UBT_NG_LOCK(sc); 1797 if (NG_BT_MBUFQ_FULL(q)) { 1798 NG_BT_MBUFQ_DROP(q); 1799 UBT_NG_UNLOCK(sc); 1800 1801 UBT_ERR(sc, "Dropping HCI frame 0x%02x, len=%d. Queue full\n", 1802 *mtod(m, uint8_t *), m->m_pkthdr.len); 1803 1804 NG_FREE_M(m); 1805 } else { 1806 /* Loose HCI packet type, enqueue mbuf and kick off task */ 1807 m_adj(m, sizeof(uint8_t)); 1808 NG_BT_MBUFQ_ENQUEUE(q, m); 1809 ubt_task_schedule(sc, action); 1810 UBT_NG_UNLOCK(sc); 1811 } 1812 done: 1813 NG_FREE_ITEM(item); 1814 1815 return (error); 1816 } /* ng_ubt_rcvdata */ 1817 1818 /**************************************************************************** 1819 **************************************************************************** 1820 ** Module 1821 **************************************************************************** 1822 ****************************************************************************/ 1823 1824 /* 1825 * Load/Unload the driver module 1826 */ 1827 1828 static int 1829 ubt_modevent(module_t mod, int event, void *data) 1830 { 1831 int error; 1832 1833 switch (event) { 1834 case MOD_LOAD: 1835 error = ng_newtype(&typestruct); 1836 if (error != 0) 1837 printf("%s: Could not register Netgraph node type, " \ 1838 "error=%d\n", NG_UBT_NODE_TYPE, error); 1839 break; 1840 1841 case MOD_UNLOAD: 1842 error = ng_rmtype(&typestruct); 1843 break; 1844 1845 default: 1846 error = EOPNOTSUPP; 1847 break; 1848 } 1849 1850 return (error); 1851 } /* ubt_modevent */ 1852 1853 static devclass_t ubt_devclass; 1854 1855 static device_method_t ubt_methods[] = 1856 { 1857 DEVMETHOD(device_probe, ubt_probe), 1858 DEVMETHOD(device_attach, ubt_attach), 1859 DEVMETHOD(device_detach, ubt_detach), 1860 DEVMETHOD_END 1861 }; 1862 1863 static driver_t ubt_driver = 1864 { 1865 .name = "ubt", 1866 .methods = ubt_methods, 1867 .size = sizeof(struct ubt_softc), 1868 }; 1869 1870 DRIVER_MODULE(ng_ubt, uhub, ubt_driver, ubt_devclass, ubt_modevent, 0); 1871 MODULE_VERSION(ng_ubt, NG_BLUETOOTH_VERSION); 1872 MODULE_DEPEND(ng_ubt, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); 1873 MODULE_DEPEND(ng_ubt, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION); 1874 MODULE_DEPEND(ng_ubt, usb, 1, 1, 1); 1875 USB_PNP_HOST_INFO(ubt_devs); 1876