1 /*- 2 * Copyright (c) 1996-2000 Whistle Communications, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of author nor the names of its 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY NICK HIBMA AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* Driver for arbitrary double bulk pipe devices. 35 * The driver assumes that there will be the same driver on the other side. 36 * 37 * XXX Some more information on what the framing of the IP packets looks like. 38 * 39 * To take full advantage of bulk transmission, packets should be chosen 40 * between 1k and 5k in size (1k to make sure the sending side starts 41 * streaming, and <5k to avoid overflowing the system with small TDs). 42 */ 43 44 45 /* probe/attach/detach: 46 * Connect the driver to the hardware and netgraph 47 * 48 * The reason we submit a bulk in transfer is that USB does not know about 49 * interrupts. The bulk transfer continuously polls the device for data. 50 * While the device has no data available, the device NAKs the TDs. As soon 51 * as there is data, the transfer happens and the data comes flowing in. 52 * 53 * In case you were wondering, interrupt transfers happen exactly that way. 54 * It therefore doesn't make sense to use the interrupt pipe to signal 55 * 'data ready' and then schedule a bulk transfer to fetch it. That would 56 * incur a 2ms delay at least, without reducing bandwidth requirements. 57 * 58 */ 59 60 #include <sys/stdint.h> 61 #include <sys/stddef.h> 62 #include <sys/param.h> 63 #include <sys/queue.h> 64 #include <sys/types.h> 65 #include <sys/systm.h> 66 #include <sys/kernel.h> 67 #include <sys/bus.h> 68 #include <sys/module.h> 69 #include <sys/lock.h> 70 #include <sys/mutex.h> 71 #include <sys/condvar.h> 72 #include <sys/sysctl.h> 73 #include <sys/sx.h> 74 #include <sys/unistd.h> 75 #include <sys/callout.h> 76 #include <sys/malloc.h> 77 #include <sys/priv.h> 78 79 #include <dev/usb/usb.h> 80 #include <dev/usb/usbdi.h> 81 #include <dev/usb/usbdi_util.h> 82 #include "usbdevs.h" 83 84 #define USB_DEBUG_VAR udbp_debug 85 #include <dev/usb/usb_debug.h> 86 87 #include <sys/mbuf.h> 88 89 #include <netgraph/ng_message.h> 90 #include <netgraph/netgraph.h> 91 #include <netgraph/ng_parse.h> 92 #include <netgraph/bluetooth/include/ng_bluetooth.h> 93 94 #include <dev/usb/misc/udbp.h> 95 96 #ifdef USB_DEBUG 97 static int udbp_debug = 0; 98 99 static SYSCTL_NODE(_hw_usb, OID_AUTO, udbp, CTLFLAG_RW, 0, "USB udbp"); 100 SYSCTL_INT(_hw_usb_udbp, OID_AUTO, debug, CTLFLAG_RW, 101 &udbp_debug, 0, "udbp debug level"); 102 #endif 103 104 #define UDBP_TIMEOUT 2000 /* timeout on outbound transfers, in 105 * msecs */ 106 #define UDBP_BUFFERSIZE MCLBYTES /* maximum number of bytes in one 107 * transfer */ 108 #define UDBP_T_WR 0 109 #define UDBP_T_RD 1 110 #define UDBP_T_WR_CS 2 111 #define UDBP_T_RD_CS 3 112 #define UDBP_T_MAX 4 113 #define UDBP_Q_MAXLEN 50 114 115 struct udbp_softc { 116 117 struct mtx sc_mtx; 118 struct ng_bt_mbufq sc_xmitq_hipri; /* hi-priority transmit queue */ 119 struct ng_bt_mbufq sc_xmitq; /* low-priority transmit queue */ 120 121 struct usb_xfer *sc_xfer[UDBP_T_MAX]; 122 node_p sc_node; /* back pointer to node */ 123 hook_p sc_hook; /* pointer to the hook */ 124 struct mbuf *sc_bulk_in_buffer; 125 126 uint32_t sc_packets_in; /* packets in from downstream */ 127 uint32_t sc_packets_out; /* packets out towards downstream */ 128 129 uint8_t sc_flags; 130 #define UDBP_FLAG_READ_STALL 0x01 /* read transfer stalled */ 131 #define UDBP_FLAG_WRITE_STALL 0x02 /* write transfer stalled */ 132 133 uint8_t sc_name[16]; 134 }; 135 136 /* prototypes */ 137 138 static int udbp_modload(module_t mod, int event, void *data); 139 140 static device_probe_t udbp_probe; 141 static device_attach_t udbp_attach; 142 static device_detach_t udbp_detach; 143 144 static usb_callback_t udbp_bulk_read_callback; 145 static usb_callback_t udbp_bulk_read_clear_stall_callback; 146 static usb_callback_t udbp_bulk_write_callback; 147 static usb_callback_t udbp_bulk_write_clear_stall_callback; 148 149 static void udbp_bulk_read_complete(node_p, hook_p, void *, int); 150 151 static ng_constructor_t ng_udbp_constructor; 152 static ng_rcvmsg_t ng_udbp_rcvmsg; 153 static ng_shutdown_t ng_udbp_rmnode; 154 static ng_newhook_t ng_udbp_newhook; 155 static ng_connect_t ng_udbp_connect; 156 static ng_rcvdata_t ng_udbp_rcvdata; 157 static ng_disconnect_t ng_udbp_disconnect; 158 159 /* Parse type for struct ngudbpstat */ 160 static const struct ng_parse_struct_field 161 ng_udbp_stat_type_fields[] = NG_UDBP_STATS_TYPE_INFO; 162 163 static const struct ng_parse_type ng_udbp_stat_type = { 164 &ng_parse_struct_type, 165 &ng_udbp_stat_type_fields 166 }; 167 168 /* List of commands and how to convert arguments to/from ASCII */ 169 static const struct ng_cmdlist ng_udbp_cmdlist[] = { 170 { 171 NGM_UDBP_COOKIE, 172 NGM_UDBP_GET_STATUS, 173 "getstatus", 174 NULL, 175 &ng_udbp_stat_type, 176 }, 177 { 178 NGM_UDBP_COOKIE, 179 NGM_UDBP_SET_FLAG, 180 "setflag", 181 &ng_parse_int32_type, 182 NULL 183 }, 184 {0} 185 }; 186 187 /* Netgraph node type descriptor */ 188 static struct ng_type ng_udbp_typestruct = { 189 .version = NG_ABI_VERSION, 190 .name = NG_UDBP_NODE_TYPE, 191 .constructor = ng_udbp_constructor, 192 .rcvmsg = ng_udbp_rcvmsg, 193 .shutdown = ng_udbp_rmnode, 194 .newhook = ng_udbp_newhook, 195 .connect = ng_udbp_connect, 196 .rcvdata = ng_udbp_rcvdata, 197 .disconnect = ng_udbp_disconnect, 198 .cmdlist = ng_udbp_cmdlist, 199 }; 200 201 /* USB config */ 202 static const struct usb_config udbp_config[UDBP_T_MAX] = { 203 204 [UDBP_T_WR] = { 205 .type = UE_BULK, 206 .endpoint = UE_ADDR_ANY, 207 .direction = UE_DIR_OUT, 208 .bufsize = UDBP_BUFFERSIZE, 209 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 210 .callback = &udbp_bulk_write_callback, 211 .timeout = UDBP_TIMEOUT, 212 }, 213 214 [UDBP_T_RD] = { 215 .type = UE_BULK, 216 .endpoint = UE_ADDR_ANY, 217 .direction = UE_DIR_IN, 218 .bufsize = UDBP_BUFFERSIZE, 219 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 220 .callback = &udbp_bulk_read_callback, 221 }, 222 223 [UDBP_T_WR_CS] = { 224 .type = UE_CONTROL, 225 .endpoint = 0x00, /* Control pipe */ 226 .direction = UE_DIR_ANY, 227 .bufsize = sizeof(struct usb_device_request), 228 .callback = &udbp_bulk_write_clear_stall_callback, 229 .timeout = 1000, /* 1 second */ 230 .interval = 50, /* 50ms */ 231 }, 232 233 [UDBP_T_RD_CS] = { 234 .type = UE_CONTROL, 235 .endpoint = 0x00, /* Control pipe */ 236 .direction = UE_DIR_ANY, 237 .bufsize = sizeof(struct usb_device_request), 238 .callback = &udbp_bulk_read_clear_stall_callback, 239 .timeout = 1000, /* 1 second */ 240 .interval = 50, /* 50ms */ 241 }, 242 }; 243 244 static devclass_t udbp_devclass; 245 246 static device_method_t udbp_methods[] = { 247 /* Device interface */ 248 DEVMETHOD(device_probe, udbp_probe), 249 DEVMETHOD(device_attach, udbp_attach), 250 DEVMETHOD(device_detach, udbp_detach), 251 {0, 0} 252 }; 253 254 static driver_t udbp_driver = { 255 .name = "udbp", 256 .methods = udbp_methods, 257 .size = sizeof(struct udbp_softc), 258 }; 259 260 DRIVER_MODULE(udbp, uhub, udbp_driver, udbp_devclass, udbp_modload, 0); 261 MODULE_DEPEND(udbp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); 262 MODULE_DEPEND(udbp, usb, 1, 1, 1); 263 MODULE_VERSION(udbp, 1); 264 265 static int 266 udbp_modload(module_t mod, int event, void *data) 267 { 268 int error; 269 270 switch (event) { 271 case MOD_LOAD: 272 error = ng_newtype(&ng_udbp_typestruct); 273 if (error != 0) { 274 printf("%s: Could not register " 275 "Netgraph node type, error=%d\n", 276 NG_UDBP_NODE_TYPE, error); 277 } 278 break; 279 280 case MOD_UNLOAD: 281 error = ng_rmtype(&ng_udbp_typestruct); 282 break; 283 284 default: 285 error = EOPNOTSUPP; 286 break; 287 } 288 return (error); 289 } 290 291 static const STRUCT_USB_HOST_ID udbp_devs[] = { 292 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_TURBOCONNECT, 0)}, 293 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301, 0)}, 294 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302, 0)}, 295 {USB_VPI(USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_EZLINK, 0)}, 296 {USB_VPI(USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL620USB, 0)}, 297 }; 298 299 static int 300 udbp_probe(device_t dev) 301 { 302 struct usb_attach_arg *uaa = device_get_ivars(dev); 303 304 if (uaa->usb_mode != USB_MODE_HOST) 305 return (ENXIO); 306 if (uaa->info.bConfigIndex != 0) 307 return (ENXIO); 308 if (uaa->info.bIfaceIndex != 0) 309 return (ENXIO); 310 311 return (usbd_lookup_id_by_uaa(udbp_devs, sizeof(udbp_devs), uaa)); 312 } 313 314 static int 315 udbp_attach(device_t dev) 316 { 317 struct usb_attach_arg *uaa = device_get_ivars(dev); 318 struct udbp_softc *sc = device_get_softc(dev); 319 int error; 320 321 device_set_usb_desc(dev); 322 323 snprintf(sc->sc_name, sizeof(sc->sc_name), 324 "%s", device_get_nameunit(dev)); 325 326 mtx_init(&sc->sc_mtx, "udbp lock", NULL, MTX_DEF | MTX_RECURSE); 327 328 error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex, 329 sc->sc_xfer, udbp_config, UDBP_T_MAX, sc, &sc->sc_mtx); 330 if (error) { 331 DPRINTF("error=%s\n", usbd_errstr(error)); 332 goto detach; 333 } 334 NG_BT_MBUFQ_INIT(&sc->sc_xmitq, UDBP_Q_MAXLEN); 335 336 NG_BT_MBUFQ_INIT(&sc->sc_xmitq_hipri, UDBP_Q_MAXLEN); 337 338 /* create Netgraph node */ 339 340 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 341 printf("%s: Could not create Netgraph node\n", 342 sc->sc_name); 343 sc->sc_node = NULL; 344 goto detach; 345 } 346 /* name node */ 347 348 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 349 printf("%s: Could not name node\n", 350 sc->sc_name); 351 NG_NODE_UNREF(sc->sc_node); 352 sc->sc_node = NULL; 353 goto detach; 354 } 355 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 356 357 /* the device is now operational */ 358 359 return (0); /* success */ 360 361 detach: 362 udbp_detach(dev); 363 return (ENOMEM); /* failure */ 364 } 365 366 static int 367 udbp_detach(device_t dev) 368 { 369 struct udbp_softc *sc = device_get_softc(dev); 370 371 /* destroy Netgraph node */ 372 373 if (sc->sc_node != NULL) { 374 NG_NODE_SET_PRIVATE(sc->sc_node, NULL); 375 ng_rmnode_self(sc->sc_node); 376 sc->sc_node = NULL; 377 } 378 /* free USB transfers, if any */ 379 380 usbd_transfer_unsetup(sc->sc_xfer, UDBP_T_MAX); 381 382 mtx_destroy(&sc->sc_mtx); 383 384 /* destroy queues */ 385 386 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq); 387 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq_hipri); 388 389 /* extra check */ 390 391 if (sc->sc_bulk_in_buffer) { 392 m_freem(sc->sc_bulk_in_buffer); 393 sc->sc_bulk_in_buffer = NULL; 394 } 395 return (0); /* success */ 396 } 397 398 static void 399 udbp_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 400 { 401 struct udbp_softc *sc = usbd_xfer_softc(xfer); 402 struct usb_page_cache *pc; 403 struct mbuf *m; 404 int actlen; 405 406 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 407 408 switch (USB_GET_STATE(xfer)) { 409 case USB_ST_TRANSFERRED: 410 411 /* allocate new mbuf */ 412 413 MGETHDR(m, M_NOWAIT, MT_DATA); 414 415 if (m == NULL) { 416 goto tr_setup; 417 } 418 MCLGET(m, M_NOWAIT); 419 420 if (!(m->m_flags & M_EXT)) { 421 m_freem(m); 422 goto tr_setup; 423 } 424 m->m_pkthdr.len = m->m_len = actlen; 425 426 pc = usbd_xfer_get_frame(xfer, 0); 427 usbd_copy_out(pc, 0, m->m_data, actlen); 428 429 sc->sc_bulk_in_buffer = m; 430 431 DPRINTF("received package %d bytes\n", actlen); 432 433 case USB_ST_SETUP: 434 tr_setup: 435 if (sc->sc_bulk_in_buffer) { 436 ng_send_fn(sc->sc_node, NULL, &udbp_bulk_read_complete, NULL, 0); 437 return; 438 } 439 if (sc->sc_flags & UDBP_FLAG_READ_STALL) { 440 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 441 return; 442 } 443 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 444 usbd_transfer_submit(xfer); 445 return; 446 447 default: /* Error */ 448 if (error != USB_ERR_CANCELLED) { 449 /* try to clear stall first */ 450 sc->sc_flags |= UDBP_FLAG_READ_STALL; 451 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 452 } 453 return; 454 455 } 456 } 457 458 static void 459 udbp_bulk_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 460 { 461 struct udbp_softc *sc = usbd_xfer_softc(xfer); 462 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_RD]; 463 464 if (usbd_clear_stall_callback(xfer, xfer_other)) { 465 DPRINTF("stall cleared\n"); 466 sc->sc_flags &= ~UDBP_FLAG_READ_STALL; 467 usbd_transfer_start(xfer_other); 468 } 469 } 470 471 static void 472 udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2) 473 { 474 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 475 struct mbuf *m; 476 int error; 477 478 if (sc == NULL) { 479 return; 480 } 481 mtx_lock(&sc->sc_mtx); 482 483 m = sc->sc_bulk_in_buffer; 484 485 if (m) { 486 487 sc->sc_bulk_in_buffer = NULL; 488 489 if ((sc->sc_hook == NULL) || 490 NG_HOOK_NOT_VALID(sc->sc_hook)) { 491 DPRINTF("No upstream hook\n"); 492 goto done; 493 } 494 sc->sc_packets_in++; 495 496 NG_SEND_DATA_ONLY(error, sc->sc_hook, m); 497 498 m = NULL; 499 } 500 done: 501 if (m) { 502 m_freem(m); 503 } 504 /* start USB bulk-in transfer, if not already started */ 505 506 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]); 507 508 mtx_unlock(&sc->sc_mtx); 509 } 510 511 static void 512 udbp_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 513 { 514 struct udbp_softc *sc = usbd_xfer_softc(xfer); 515 struct usb_page_cache *pc; 516 struct mbuf *m; 517 518 switch (USB_GET_STATE(xfer)) { 519 case USB_ST_TRANSFERRED: 520 521 sc->sc_packets_out++; 522 523 case USB_ST_SETUP: 524 if (sc->sc_flags & UDBP_FLAG_WRITE_STALL) { 525 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 526 return; 527 } 528 /* get next mbuf, if any */ 529 530 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq_hipri, m); 531 if (m == NULL) { 532 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq, m); 533 if (m == NULL) { 534 DPRINTF("Data queue is empty\n"); 535 return; 536 } 537 } 538 if (m->m_pkthdr.len > MCLBYTES) { 539 DPRINTF("truncating large packet " 540 "from %d to %d bytes\n", m->m_pkthdr.len, 541 MCLBYTES); 542 m->m_pkthdr.len = MCLBYTES; 543 } 544 pc = usbd_xfer_get_frame(xfer, 0); 545 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 546 547 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 548 549 DPRINTF("packet out: %d bytes\n", m->m_pkthdr.len); 550 551 m_freem(m); 552 553 usbd_transfer_submit(xfer); 554 return; 555 556 default: /* Error */ 557 if (error != USB_ERR_CANCELLED) { 558 /* try to clear stall first */ 559 sc->sc_flags |= UDBP_FLAG_WRITE_STALL; 560 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 561 } 562 return; 563 564 } 565 } 566 567 static void 568 udbp_bulk_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 569 { 570 struct udbp_softc *sc = usbd_xfer_softc(xfer); 571 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_WR]; 572 573 if (usbd_clear_stall_callback(xfer, xfer_other)) { 574 DPRINTF("stall cleared\n"); 575 sc->sc_flags &= ~UDBP_FLAG_WRITE_STALL; 576 usbd_transfer_start(xfer_other); 577 } 578 } 579 580 /*********************************************************************** 581 * Start of Netgraph methods 582 **********************************************************************/ 583 584 /* 585 * If this is a device node so this work is done in the attach() 586 * routine and the constructor will return EINVAL as you should not be able 587 * to create nodes that depend on hardware (unless you can add the hardware :) 588 */ 589 static int 590 ng_udbp_constructor(node_p node) 591 { 592 return (EINVAL); 593 } 594 595 /* 596 * Give our ok for a hook to be added... 597 * If we are not running this might kick a device into life. 598 * Possibly decode information out of the hook name. 599 * Add the hook's private info to the hook structure. 600 * (if we had some). In this example, we assume that there is a 601 * an array of structs, called 'channel' in the private info, 602 * one for each active channel. The private 603 * pointer of each hook points to the appropriate UDBP_hookinfo struct 604 * so that the source of an input packet is easily identified. 605 */ 606 static int 607 ng_udbp_newhook(node_p node, hook_p hook, const char *name) 608 { 609 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 610 int32_t error = 0; 611 612 if (strcmp(name, NG_UDBP_HOOK_NAME)) { 613 return (EINVAL); 614 } 615 mtx_lock(&sc->sc_mtx); 616 617 if (sc->sc_hook != NULL) { 618 error = EISCONN; 619 } else { 620 sc->sc_hook = hook; 621 NG_HOOK_SET_PRIVATE(hook, NULL); 622 } 623 624 mtx_unlock(&sc->sc_mtx); 625 626 return (error); 627 } 628 629 /* 630 * Get a netgraph control message. 631 * Check it is one we understand. If needed, send a response. 632 * We could save the address for an async action later, but don't here. 633 * Always free the message. 634 * The response should be in a malloc'd region that the caller can 'free'. 635 * A response is not required. 636 * Theoretically you could respond defferently to old message types if 637 * the cookie in the header didn't match what we consider to be current 638 * (so that old userland programs could continue to work). 639 */ 640 static int 641 ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook) 642 { 643 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 644 struct ng_mesg *resp = NULL; 645 int error = 0; 646 struct ng_mesg *msg; 647 648 NGI_GET_MSG(item, msg); 649 /* Deal with message according to cookie and command */ 650 switch (msg->header.typecookie) { 651 case NGM_UDBP_COOKIE: 652 switch (msg->header.cmd) { 653 case NGM_UDBP_GET_STATUS: 654 { 655 struct ngudbpstat *stats; 656 657 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT); 658 if (!resp) { 659 error = ENOMEM; 660 break; 661 } 662 stats = (struct ngudbpstat *)resp->data; 663 mtx_lock(&sc->sc_mtx); 664 stats->packets_in = sc->sc_packets_in; 665 stats->packets_out = sc->sc_packets_out; 666 mtx_unlock(&sc->sc_mtx); 667 break; 668 } 669 case NGM_UDBP_SET_FLAG: 670 if (msg->header.arglen != sizeof(uint32_t)) { 671 error = EINVAL; 672 break; 673 } 674 DPRINTF("flags = 0x%08x\n", 675 *((uint32_t *)msg->data)); 676 break; 677 default: 678 error = EINVAL; /* unknown command */ 679 break; 680 } 681 break; 682 default: 683 error = EINVAL; /* unknown cookie type */ 684 break; 685 } 686 687 /* Take care of synchronous response, if any */ 688 NG_RESPOND_MSG(error, node, item, resp); 689 NG_FREE_MSG(msg); 690 return (error); 691 } 692 693 /* 694 * Accept data from the hook and queue it for output. 695 */ 696 static int 697 ng_udbp_rcvdata(hook_p hook, item_p item) 698 { 699 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 700 struct ng_bt_mbufq *queue_ptr; 701 struct mbuf *m; 702 struct ng_tag_prio *ptag; 703 int error; 704 705 if (sc == NULL) { 706 NG_FREE_ITEM(item); 707 return (EHOSTDOWN); 708 } 709 NGI_GET_M(item, m); 710 NG_FREE_ITEM(item); 711 712 /* 713 * Now queue the data for when it can be sent 714 */ 715 ptag = (void *)m_tag_locate(m, NGM_GENERIC_COOKIE, 716 NG_TAG_PRIO, NULL); 717 718 if (ptag && (ptag->priority > NG_PRIO_CUTOFF)) 719 queue_ptr = &sc->sc_xmitq_hipri; 720 else 721 queue_ptr = &sc->sc_xmitq; 722 723 mtx_lock(&sc->sc_mtx); 724 725 if (NG_BT_MBUFQ_FULL(queue_ptr)) { 726 NG_BT_MBUFQ_DROP(queue_ptr); 727 NG_FREE_M(m); 728 error = ENOBUFS; 729 } else { 730 NG_BT_MBUFQ_ENQUEUE(queue_ptr, m); 731 /* 732 * start bulk-out transfer, if not already started: 733 */ 734 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]); 735 error = 0; 736 } 737 738 mtx_unlock(&sc->sc_mtx); 739 740 return (error); 741 } 742 743 /* 744 * Do local shutdown processing.. 745 * We are a persistant device, we refuse to go away, and 746 * only remove our links and reset ourself. 747 */ 748 static int 749 ng_udbp_rmnode(node_p node) 750 { 751 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 752 753 /* Let old node go */ 754 NG_NODE_SET_PRIVATE(node, NULL); 755 NG_NODE_UNREF(node); /* forget it ever existed */ 756 757 if (sc == NULL) { 758 goto done; 759 } 760 /* Create Netgraph node */ 761 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 762 printf("%s: Could not create Netgraph node\n", 763 sc->sc_name); 764 sc->sc_node = NULL; 765 goto done; 766 } 767 /* Name node */ 768 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 769 printf("%s: Could not name Netgraph node\n", 770 sc->sc_name); 771 NG_NODE_UNREF(sc->sc_node); 772 sc->sc_node = NULL; 773 goto done; 774 } 775 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 776 777 done: 778 if (sc) { 779 mtx_unlock(&sc->sc_mtx); 780 } 781 return (0); 782 } 783 784 /* 785 * This is called once we've already connected a new hook to the other node. 786 * It gives us a chance to balk at the last minute. 787 */ 788 static int 789 ng_udbp_connect(hook_p hook) 790 { 791 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 792 793 /* probably not at splnet, force outward queueing */ 794 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 795 796 mtx_lock(&sc->sc_mtx); 797 798 sc->sc_flags |= (UDBP_FLAG_READ_STALL | 799 UDBP_FLAG_WRITE_STALL); 800 801 /* start bulk-in transfer */ 802 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]); 803 804 /* start bulk-out transfer */ 805 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]); 806 807 mtx_unlock(&sc->sc_mtx); 808 809 return (0); 810 } 811 812 /* 813 * Dook disconnection 814 * 815 * For this type, removal of the last link destroys the node 816 */ 817 static int 818 ng_udbp_disconnect(hook_p hook) 819 { 820 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 821 int error = 0; 822 823 if (sc != NULL) { 824 825 mtx_lock(&sc->sc_mtx); 826 827 if (hook != sc->sc_hook) { 828 error = EINVAL; 829 } else { 830 831 /* stop bulk-in transfer */ 832 usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD_CS]); 833 usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD]); 834 835 /* stop bulk-out transfer */ 836 usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR_CS]); 837 usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR]); 838 839 /* cleanup queues */ 840 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq); 841 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq_hipri); 842 843 if (sc->sc_bulk_in_buffer) { 844 m_freem(sc->sc_bulk_in_buffer); 845 sc->sc_bulk_in_buffer = NULL; 846 } 847 sc->sc_hook = NULL; 848 } 849 850 mtx_unlock(&sc->sc_mtx); 851 } 852 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 853 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 854 ng_rmnode_self(NG_HOOK_NODE(hook)); 855 856 return (error); 857 } 858