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