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$"); 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 99 static int udbp_debug = 0; 100 101 static SYSCTL_NODE(_hw_usb, OID_AUTO, udbp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 102 "USB udbp"); 103 SYSCTL_INT(_hw_usb_udbp, OID_AUTO, debug, CTLFLAG_RWTUN, 104 &udbp_debug, 0, "udbp debug level"); 105 #endif 106 107 #define UDBP_TIMEOUT 2000 /* timeout on outbound transfers, in 108 * msecs */ 109 #define UDBP_BUFFERSIZE MCLBYTES /* maximum number of bytes in one 110 * transfer */ 111 #define UDBP_T_WR 0 112 #define UDBP_T_RD 1 113 #define UDBP_T_WR_CS 2 114 #define UDBP_T_RD_CS 3 115 #define UDBP_T_MAX 4 116 #define UDBP_Q_MAXLEN 50 117 118 struct udbp_softc { 119 120 struct mtx sc_mtx; 121 struct ng_bt_mbufq sc_xmitq_hipri; /* hi-priority transmit queue */ 122 struct ng_bt_mbufq sc_xmitq; /* low-priority transmit queue */ 123 124 struct usb_xfer *sc_xfer[UDBP_T_MAX]; 125 node_p sc_node; /* back pointer to node */ 126 hook_p sc_hook; /* pointer to the hook */ 127 struct mbuf *sc_bulk_in_buffer; 128 129 uint32_t sc_packets_in; /* packets in from downstream */ 130 uint32_t sc_packets_out; /* packets out towards downstream */ 131 132 uint8_t sc_flags; 133 #define UDBP_FLAG_READ_STALL 0x01 /* read transfer stalled */ 134 #define UDBP_FLAG_WRITE_STALL 0x02 /* write transfer stalled */ 135 136 uint8_t sc_name[16]; 137 }; 138 139 /* prototypes */ 140 141 static int udbp_modload(module_t mod, int event, void *data); 142 143 static device_probe_t udbp_probe; 144 static device_attach_t udbp_attach; 145 static device_detach_t udbp_detach; 146 147 static usb_callback_t udbp_bulk_read_callback; 148 static usb_callback_t udbp_bulk_read_clear_stall_callback; 149 static usb_callback_t udbp_bulk_write_callback; 150 static usb_callback_t udbp_bulk_write_clear_stall_callback; 151 152 static void udbp_bulk_read_complete(node_p, hook_p, void *, int); 153 154 static ng_constructor_t ng_udbp_constructor; 155 static ng_rcvmsg_t ng_udbp_rcvmsg; 156 static ng_shutdown_t ng_udbp_rmnode; 157 static ng_newhook_t ng_udbp_newhook; 158 static ng_connect_t ng_udbp_connect; 159 static ng_rcvdata_t ng_udbp_rcvdata; 160 static ng_disconnect_t ng_udbp_disconnect; 161 162 /* Parse type for struct ngudbpstat */ 163 static const struct ng_parse_struct_field 164 ng_udbp_stat_type_fields[] = NG_UDBP_STATS_TYPE_INFO; 165 166 static const struct ng_parse_type ng_udbp_stat_type = { 167 &ng_parse_struct_type, 168 &ng_udbp_stat_type_fields 169 }; 170 171 /* List of commands and how to convert arguments to/from ASCII */ 172 static const struct ng_cmdlist ng_udbp_cmdlist[] = { 173 { 174 NGM_UDBP_COOKIE, 175 NGM_UDBP_GET_STATUS, 176 "getstatus", 177 NULL, 178 &ng_udbp_stat_type, 179 }, 180 { 181 NGM_UDBP_COOKIE, 182 NGM_UDBP_SET_FLAG, 183 "setflag", 184 &ng_parse_int32_type, 185 NULL 186 }, 187 {0} 188 }; 189 190 /* Netgraph node type descriptor */ 191 static struct ng_type ng_udbp_typestruct = { 192 .version = NG_ABI_VERSION, 193 .name = NG_UDBP_NODE_TYPE, 194 .constructor = ng_udbp_constructor, 195 .rcvmsg = ng_udbp_rcvmsg, 196 .shutdown = ng_udbp_rmnode, 197 .newhook = ng_udbp_newhook, 198 .connect = ng_udbp_connect, 199 .rcvdata = ng_udbp_rcvdata, 200 .disconnect = ng_udbp_disconnect, 201 .cmdlist = ng_udbp_cmdlist, 202 }; 203 204 /* USB config */ 205 static const struct usb_config udbp_config[UDBP_T_MAX] = { 206 207 [UDBP_T_WR] = { 208 .type = UE_BULK, 209 .endpoint = UE_ADDR_ANY, 210 .direction = UE_DIR_OUT, 211 .bufsize = UDBP_BUFFERSIZE, 212 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 213 .callback = &udbp_bulk_write_callback, 214 .timeout = UDBP_TIMEOUT, 215 }, 216 217 [UDBP_T_RD] = { 218 .type = UE_BULK, 219 .endpoint = UE_ADDR_ANY, 220 .direction = UE_DIR_IN, 221 .bufsize = UDBP_BUFFERSIZE, 222 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 223 .callback = &udbp_bulk_read_callback, 224 }, 225 226 [UDBP_T_WR_CS] = { 227 .type = UE_CONTROL, 228 .endpoint = 0x00, /* Control pipe */ 229 .direction = UE_DIR_ANY, 230 .bufsize = sizeof(struct usb_device_request), 231 .callback = &udbp_bulk_write_clear_stall_callback, 232 .timeout = 1000, /* 1 second */ 233 .interval = 50, /* 50ms */ 234 }, 235 236 [UDBP_T_RD_CS] = { 237 .type = UE_CONTROL, 238 .endpoint = 0x00, /* Control pipe */ 239 .direction = UE_DIR_ANY, 240 .bufsize = sizeof(struct usb_device_request), 241 .callback = &udbp_bulk_read_clear_stall_callback, 242 .timeout = 1000, /* 1 second */ 243 .interval = 50, /* 50ms */ 244 }, 245 }; 246 247 static devclass_t udbp_devclass; 248 249 static device_method_t udbp_methods[] = { 250 /* Device interface */ 251 DEVMETHOD(device_probe, udbp_probe), 252 DEVMETHOD(device_attach, udbp_attach), 253 DEVMETHOD(device_detach, udbp_detach), 254 255 DEVMETHOD_END 256 }; 257 258 static driver_t udbp_driver = { 259 .name = "udbp", 260 .methods = udbp_methods, 261 .size = sizeof(struct udbp_softc), 262 }; 263 264 static const STRUCT_USB_HOST_ID udbp_devs[] = { 265 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U258, 0)}, 266 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_TURBOCONNECT, 0)}, 267 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_GADGETZERO, 0)}, 268 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301, 0)}, 269 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302, 0)}, 270 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL27A1, 0)}, 271 {USB_VPI(USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_EZLINK, 0)}, 272 {USB_VPI(USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL620USB, 0)}, 273 }; 274 275 DRIVER_MODULE(udbp, uhub, udbp_driver, udbp_devclass, udbp_modload, 0); 276 MODULE_DEPEND(udbp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); 277 MODULE_DEPEND(udbp, usb, 1, 1, 1); 278 MODULE_VERSION(udbp, 1); 279 USB_PNP_HOST_INFO(udbp_devs); 280 281 static int 282 udbp_modload(module_t mod, int event, void *data) 283 { 284 int error; 285 286 switch (event) { 287 case MOD_LOAD: 288 error = ng_newtype(&ng_udbp_typestruct); 289 if (error != 0) { 290 printf("%s: Could not register " 291 "Netgraph node type, error=%d\n", 292 NG_UDBP_NODE_TYPE, error); 293 } 294 break; 295 296 case MOD_UNLOAD: 297 error = ng_rmtype(&ng_udbp_typestruct); 298 break; 299 300 default: 301 error = EOPNOTSUPP; 302 break; 303 } 304 return (error); 305 } 306 307 static int 308 udbp_probe(device_t dev) 309 { 310 struct usb_attach_arg *uaa = device_get_ivars(dev); 311 312 if (uaa->usb_mode != USB_MODE_HOST) 313 return (ENXIO); 314 if (uaa->info.bConfigIndex != 0) 315 return (ENXIO); 316 if (uaa->info.bIfaceIndex != 0) 317 return (ENXIO); 318 319 return (usbd_lookup_id_by_uaa(udbp_devs, sizeof(udbp_devs), uaa)); 320 } 321 322 static int 323 udbp_attach(device_t dev) 324 { 325 struct usb_attach_arg *uaa = device_get_ivars(dev); 326 struct udbp_softc *sc = device_get_softc(dev); 327 int error; 328 329 device_set_usb_desc(dev); 330 331 snprintf(sc->sc_name, sizeof(sc->sc_name), 332 "%s", device_get_nameunit(dev)); 333 334 mtx_init(&sc->sc_mtx, "udbp lock", NULL, MTX_DEF | MTX_RECURSE); 335 336 error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex, 337 sc->sc_xfer, udbp_config, UDBP_T_MAX, sc, &sc->sc_mtx); 338 if (error) { 339 DPRINTF("error=%s\n", usbd_errstr(error)); 340 goto detach; 341 } 342 NG_BT_MBUFQ_INIT(&sc->sc_xmitq, UDBP_Q_MAXLEN); 343 344 NG_BT_MBUFQ_INIT(&sc->sc_xmitq_hipri, UDBP_Q_MAXLEN); 345 346 /* create Netgraph node */ 347 348 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 349 printf("%s: Could not create Netgraph node\n", 350 sc->sc_name); 351 sc->sc_node = NULL; 352 goto detach; 353 } 354 /* name node */ 355 356 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 357 printf("%s: Could not name node\n", 358 sc->sc_name); 359 NG_NODE_UNREF(sc->sc_node); 360 sc->sc_node = NULL; 361 goto detach; 362 } 363 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 364 365 /* the device is now operational */ 366 367 return (0); /* success */ 368 369 detach: 370 udbp_detach(dev); 371 return (ENOMEM); /* failure */ 372 } 373 374 static int 375 udbp_detach(device_t dev) 376 { 377 struct udbp_softc *sc = device_get_softc(dev); 378 379 /* destroy Netgraph node */ 380 381 if (sc->sc_node != NULL) { 382 NG_NODE_SET_PRIVATE(sc->sc_node, NULL); 383 ng_rmnode_self(sc->sc_node); 384 sc->sc_node = NULL; 385 } 386 /* free USB transfers, if any */ 387 388 usbd_transfer_unsetup(sc->sc_xfer, UDBP_T_MAX); 389 390 mtx_destroy(&sc->sc_mtx); 391 392 /* destroy queues */ 393 394 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq); 395 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq_hipri); 396 397 /* extra check */ 398 399 if (sc->sc_bulk_in_buffer) { 400 m_freem(sc->sc_bulk_in_buffer); 401 sc->sc_bulk_in_buffer = NULL; 402 } 403 return (0); /* success */ 404 } 405 406 static void 407 udbp_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 408 { 409 struct udbp_softc *sc = usbd_xfer_softc(xfer); 410 struct usb_page_cache *pc; 411 struct mbuf *m; 412 int actlen; 413 414 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 415 416 switch (USB_GET_STATE(xfer)) { 417 case USB_ST_TRANSFERRED: 418 419 /* allocate new mbuf */ 420 421 MGETHDR(m, M_NOWAIT, MT_DATA); 422 423 if (m == NULL) { 424 goto tr_setup; 425 } 426 427 if (!(MCLGET(m, M_NOWAIT))) { 428 m_freem(m); 429 goto tr_setup; 430 } 431 m->m_pkthdr.len = m->m_len = actlen; 432 433 pc = usbd_xfer_get_frame(xfer, 0); 434 usbd_copy_out(pc, 0, m->m_data, actlen); 435 436 sc->sc_bulk_in_buffer = m; 437 438 DPRINTF("received package %d bytes\n", actlen); 439 440 case USB_ST_SETUP: 441 tr_setup: 442 if (sc->sc_bulk_in_buffer) { 443 ng_send_fn(sc->sc_node, NULL, &udbp_bulk_read_complete, NULL, 0); 444 return; 445 } 446 if (sc->sc_flags & UDBP_FLAG_READ_STALL) { 447 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 448 return; 449 } 450 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 451 usbd_transfer_submit(xfer); 452 return; 453 454 default: /* Error */ 455 if (error != USB_ERR_CANCELLED) { 456 /* try to clear stall first */ 457 sc->sc_flags |= UDBP_FLAG_READ_STALL; 458 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 459 } 460 return; 461 462 } 463 } 464 465 static void 466 udbp_bulk_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 467 { 468 struct udbp_softc *sc = usbd_xfer_softc(xfer); 469 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_RD]; 470 471 if (usbd_clear_stall_callback(xfer, xfer_other)) { 472 DPRINTF("stall cleared\n"); 473 sc->sc_flags &= ~UDBP_FLAG_READ_STALL; 474 usbd_transfer_start(xfer_other); 475 } 476 } 477 478 static void 479 udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2) 480 { 481 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 482 struct mbuf *m; 483 int error; 484 485 if (sc == NULL) { 486 return; 487 } 488 mtx_lock(&sc->sc_mtx); 489 490 m = sc->sc_bulk_in_buffer; 491 492 if (m) { 493 494 sc->sc_bulk_in_buffer = NULL; 495 496 if ((sc->sc_hook == NULL) || 497 NG_HOOK_NOT_VALID(sc->sc_hook)) { 498 DPRINTF("No upstream hook\n"); 499 goto done; 500 } 501 sc->sc_packets_in++; 502 503 NG_SEND_DATA_ONLY(error, sc->sc_hook, m); 504 505 m = NULL; 506 } 507 done: 508 if (m) { 509 m_freem(m); 510 } 511 /* start USB bulk-in transfer, if not already started */ 512 513 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]); 514 515 mtx_unlock(&sc->sc_mtx); 516 } 517 518 static void 519 udbp_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 520 { 521 struct udbp_softc *sc = usbd_xfer_softc(xfer); 522 struct usb_page_cache *pc; 523 struct mbuf *m; 524 525 switch (USB_GET_STATE(xfer)) { 526 case USB_ST_TRANSFERRED: 527 528 sc->sc_packets_out++; 529 530 case USB_ST_SETUP: 531 if (sc->sc_flags & UDBP_FLAG_WRITE_STALL) { 532 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 533 return; 534 } 535 /* get next mbuf, if any */ 536 537 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq_hipri, m); 538 if (m == NULL) { 539 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq, m); 540 if (m == NULL) { 541 DPRINTF("Data queue is empty\n"); 542 return; 543 } 544 } 545 if (m->m_pkthdr.len > MCLBYTES) { 546 DPRINTF("truncating large packet " 547 "from %d to %d bytes\n", m->m_pkthdr.len, 548 MCLBYTES); 549 m->m_pkthdr.len = MCLBYTES; 550 } 551 pc = usbd_xfer_get_frame(xfer, 0); 552 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 553 554 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 555 556 DPRINTF("packet out: %d bytes\n", m->m_pkthdr.len); 557 558 m_freem(m); 559 560 usbd_transfer_submit(xfer); 561 return; 562 563 default: /* Error */ 564 if (error != USB_ERR_CANCELLED) { 565 /* try to clear stall first */ 566 sc->sc_flags |= UDBP_FLAG_WRITE_STALL; 567 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 568 } 569 return; 570 571 } 572 } 573 574 static void 575 udbp_bulk_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 576 { 577 struct udbp_softc *sc = usbd_xfer_softc(xfer); 578 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_WR]; 579 580 if (usbd_clear_stall_callback(xfer, xfer_other)) { 581 DPRINTF("stall cleared\n"); 582 sc->sc_flags &= ~UDBP_FLAG_WRITE_STALL; 583 usbd_transfer_start(xfer_other); 584 } 585 } 586 587 /*********************************************************************** 588 * Start of Netgraph methods 589 **********************************************************************/ 590 591 /* 592 * If this is a device node so this work is done in the attach() 593 * routine and the constructor will return EINVAL as you should not be able 594 * to create nodes that depend on hardware (unless you can add the hardware :) 595 */ 596 static int 597 ng_udbp_constructor(node_p node) 598 { 599 return (EINVAL); 600 } 601 602 /* 603 * Give our ok for a hook to be added... 604 * If we are not running this might kick a device into life. 605 * Possibly decode information out of the hook name. 606 * Add the hook's private info to the hook structure. 607 * (if we had some). In this example, we assume that there is a 608 * an array of structs, called 'channel' in the private info, 609 * one for each active channel. The private 610 * pointer of each hook points to the appropriate UDBP_hookinfo struct 611 * so that the source of an input packet is easily identified. 612 */ 613 static int 614 ng_udbp_newhook(node_p node, hook_p hook, const char *name) 615 { 616 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 617 int32_t error = 0; 618 619 if (strcmp(name, NG_UDBP_HOOK_NAME)) { 620 return (EINVAL); 621 } 622 mtx_lock(&sc->sc_mtx); 623 624 if (sc->sc_hook != NULL) { 625 error = EISCONN; 626 } else { 627 sc->sc_hook = hook; 628 NG_HOOK_SET_PRIVATE(hook, NULL); 629 } 630 631 mtx_unlock(&sc->sc_mtx); 632 633 return (error); 634 } 635 636 /* 637 * Get a netgraph control message. 638 * Check it is one we understand. If needed, send a response. 639 * We could save the address for an async action later, but don't here. 640 * Always free the message. 641 * The response should be in a malloc'd region that the caller can 'free'. 642 * A response is not required. 643 * Theoretically you could respond defferently to old message types if 644 * the cookie in the header didn't match what we consider to be current 645 * (so that old userland programs could continue to work). 646 */ 647 static int 648 ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook) 649 { 650 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 651 struct ng_mesg *resp = NULL; 652 int error = 0; 653 struct ng_mesg *msg; 654 655 NGI_GET_MSG(item, msg); 656 /* Deal with message according to cookie and command */ 657 switch (msg->header.typecookie) { 658 case NGM_UDBP_COOKIE: 659 switch (msg->header.cmd) { 660 case NGM_UDBP_GET_STATUS: 661 { 662 struct ngudbpstat *stats; 663 664 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT); 665 if (!resp) { 666 error = ENOMEM; 667 break; 668 } 669 stats = (struct ngudbpstat *)resp->data; 670 mtx_lock(&sc->sc_mtx); 671 stats->packets_in = sc->sc_packets_in; 672 stats->packets_out = sc->sc_packets_out; 673 mtx_unlock(&sc->sc_mtx); 674 break; 675 } 676 case NGM_UDBP_SET_FLAG: 677 if (msg->header.arglen != sizeof(uint32_t)) { 678 error = EINVAL; 679 break; 680 } 681 DPRINTF("flags = 0x%08x\n", 682 *((uint32_t *)msg->data)); 683 break; 684 default: 685 error = EINVAL; /* unknown command */ 686 break; 687 } 688 break; 689 default: 690 error = EINVAL; /* unknown cookie type */ 691 break; 692 } 693 694 /* Take care of synchronous response, if any */ 695 NG_RESPOND_MSG(error, node, item, resp); 696 NG_FREE_MSG(msg); 697 return (error); 698 } 699 700 /* 701 * Accept data from the hook and queue it for output. 702 */ 703 static int 704 ng_udbp_rcvdata(hook_p hook, item_p item) 705 { 706 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 707 struct ng_bt_mbufq *queue_ptr; 708 struct mbuf *m; 709 struct ng_tag_prio *ptag; 710 int error; 711 712 if (sc == NULL) { 713 NG_FREE_ITEM(item); 714 return (EHOSTDOWN); 715 } 716 NGI_GET_M(item, m); 717 NG_FREE_ITEM(item); 718 719 /* 720 * Now queue the data for when it can be sent 721 */ 722 ptag = (void *)m_tag_locate(m, NGM_GENERIC_COOKIE, 723 NG_TAG_PRIO, NULL); 724 725 if (ptag && (ptag->priority > NG_PRIO_CUTOFF)) 726 queue_ptr = &sc->sc_xmitq_hipri; 727 else 728 queue_ptr = &sc->sc_xmitq; 729 730 mtx_lock(&sc->sc_mtx); 731 732 if (NG_BT_MBUFQ_FULL(queue_ptr)) { 733 NG_BT_MBUFQ_DROP(queue_ptr); 734 NG_FREE_M(m); 735 error = ENOBUFS; 736 } else { 737 NG_BT_MBUFQ_ENQUEUE(queue_ptr, m); 738 /* 739 * start bulk-out transfer, if not already started: 740 */ 741 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]); 742 error = 0; 743 } 744 745 mtx_unlock(&sc->sc_mtx); 746 747 return (error); 748 } 749 750 /* 751 * Do local shutdown processing.. 752 * We are a persistent device, we refuse to go away, and 753 * only remove our links and reset ourself. 754 */ 755 static int 756 ng_udbp_rmnode(node_p node) 757 { 758 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 759 760 /* Let old node go */ 761 NG_NODE_SET_PRIVATE(node, NULL); 762 NG_NODE_UNREF(node); /* forget it ever existed */ 763 764 if (sc == NULL) { 765 goto done; 766 } 767 /* Create Netgraph node */ 768 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 769 printf("%s: Could not create Netgraph node\n", 770 sc->sc_name); 771 sc->sc_node = NULL; 772 goto done; 773 } 774 /* Name node */ 775 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 776 printf("%s: Could not name Netgraph node\n", 777 sc->sc_name); 778 NG_NODE_UNREF(sc->sc_node); 779 sc->sc_node = NULL; 780 goto done; 781 } 782 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 783 784 done: 785 if (sc) { 786 mtx_unlock(&sc->sc_mtx); 787 } 788 return (0); 789 } 790 791 /* 792 * This is called once we've already connected a new hook to the other node. 793 * It gives us a chance to balk at the last minute. 794 */ 795 static int 796 ng_udbp_connect(hook_p hook) 797 { 798 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 799 800 /* probably not at splnet, force outward queueing */ 801 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 802 803 mtx_lock(&sc->sc_mtx); 804 805 sc->sc_flags |= (UDBP_FLAG_READ_STALL | 806 UDBP_FLAG_WRITE_STALL); 807 808 /* start bulk-in transfer */ 809 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]); 810 811 /* start bulk-out transfer */ 812 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]); 813 814 mtx_unlock(&sc->sc_mtx); 815 816 return (0); 817 } 818 819 /* 820 * Dook disconnection 821 * 822 * For this type, removal of the last link destroys the node 823 */ 824 static int 825 ng_udbp_disconnect(hook_p hook) 826 { 827 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 828 int error = 0; 829 830 if (sc != NULL) { 831 832 mtx_lock(&sc->sc_mtx); 833 834 if (hook != sc->sc_hook) { 835 error = EINVAL; 836 } else { 837 838 /* stop bulk-in transfer */ 839 usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD_CS]); 840 usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD]); 841 842 /* stop bulk-out transfer */ 843 usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR_CS]); 844 usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR]); 845 846 /* cleanup queues */ 847 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq); 848 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq_hipri); 849 850 if (sc->sc_bulk_in_buffer) { 851 m_freem(sc->sc_bulk_in_buffer); 852 sc->sc_bulk_in_buffer = NULL; 853 } 854 sc->sc_hook = NULL; 855 } 856 857 mtx_unlock(&sc->sc_mtx); 858 } 859 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 860 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 861 ng_rmnode_self(NG_HOOK_NODE(hook)); 862 863 return (error); 864 } 865