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 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 int 292 udbp_probe(device_t dev) 293 { 294 struct usb_attach_arg *uaa = device_get_ivars(dev); 295 296 if (uaa->usb_mode != USB_MODE_HOST) { 297 return (ENXIO); 298 } 299 /* 300 * XXX Julian, add the id of the device if you have one to test 301 * things with. run 'usbdevs -v' and note the 3 ID's that appear. 302 * The Vendor Id and Product Id are in hex and the Revision Id is in 303 * bcd. But as usual if the revision is 0x101 then you should 304 * compare the revision id in the device descriptor with 0x101 Or go 305 * search the file usbdevs.h. Maybe the device is already in there. 306 */ 307 if (((uaa->info.idVendor == USB_VENDOR_NETCHIP) && 308 (uaa->info.idProduct == USB_PRODUCT_NETCHIP_TURBOCONNECT))) 309 return (0); 310 311 if (((uaa->info.idVendor == USB_VENDOR_PROLIFIC) && 312 ((uaa->info.idProduct == USB_PRODUCT_PROLIFIC_PL2301) || 313 (uaa->info.idProduct == USB_PRODUCT_PROLIFIC_PL2302)))) 314 return (0); 315 316 if ((uaa->info.idVendor == USB_VENDOR_ANCHOR) && 317 (uaa->info.idProduct == USB_PRODUCT_ANCHOR_EZLINK)) 318 return (0); 319 320 if ((uaa->info.idVendor == USB_VENDOR_GENESYS) && 321 (uaa->info.idProduct == USB_PRODUCT_GENESYS_GL620USB)) 322 return (0); 323 324 return (ENXIO); 325 } 326 327 static int 328 udbp_attach(device_t dev) 329 { 330 struct usb_attach_arg *uaa = device_get_ivars(dev); 331 struct udbp_softc *sc = device_get_softc(dev); 332 int error; 333 334 device_set_usb_desc(dev); 335 336 snprintf(sc->sc_name, sizeof(sc->sc_name), 337 "%s", device_get_nameunit(dev)); 338 339 mtx_init(&sc->sc_mtx, "udbp lock", NULL, MTX_DEF | MTX_RECURSE); 340 341 error = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex, 342 sc->sc_xfer, udbp_config, UDBP_T_MAX, sc, &sc->sc_mtx); 343 if (error) { 344 DPRINTF("error=%s\n", usbd_errstr(error)); 345 goto detach; 346 } 347 NG_BT_MBUFQ_INIT(&sc->sc_xmitq, UDBP_Q_MAXLEN); 348 349 NG_BT_MBUFQ_INIT(&sc->sc_xmitq_hipri, UDBP_Q_MAXLEN); 350 351 /* create Netgraph node */ 352 353 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 354 printf("%s: Could not create Netgraph node\n", 355 sc->sc_name); 356 sc->sc_node = NULL; 357 goto detach; 358 } 359 /* name node */ 360 361 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 362 printf("%s: Could not name node\n", 363 sc->sc_name); 364 NG_NODE_UNREF(sc->sc_node); 365 sc->sc_node = NULL; 366 goto detach; 367 } 368 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 369 370 /* the device is now operational */ 371 372 return (0); /* success */ 373 374 detach: 375 udbp_detach(dev); 376 return (ENOMEM); /* failure */ 377 } 378 379 static int 380 udbp_detach(device_t dev) 381 { 382 struct udbp_softc *sc = device_get_softc(dev); 383 384 /* destroy Netgraph node */ 385 386 if (sc->sc_node != NULL) { 387 NG_NODE_SET_PRIVATE(sc->sc_node, NULL); 388 ng_rmnode_self(sc->sc_node); 389 sc->sc_node = NULL; 390 } 391 /* free USB transfers, if any */ 392 393 usbd_transfer_unsetup(sc->sc_xfer, UDBP_T_MAX); 394 395 mtx_destroy(&sc->sc_mtx); 396 397 /* destroy queues */ 398 399 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq); 400 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq_hipri); 401 402 /* extra check */ 403 404 if (sc->sc_bulk_in_buffer) { 405 m_freem(sc->sc_bulk_in_buffer); 406 sc->sc_bulk_in_buffer = NULL; 407 } 408 return (0); /* success */ 409 } 410 411 static void 412 udbp_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 413 { 414 struct udbp_softc *sc = usbd_xfer_softc(xfer); 415 struct usb_page_cache *pc; 416 struct mbuf *m; 417 int actlen; 418 419 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 420 421 switch (USB_GET_STATE(xfer)) { 422 case USB_ST_TRANSFERRED: 423 424 /* allocate new mbuf */ 425 426 MGETHDR(m, M_DONTWAIT, MT_DATA); 427 428 if (m == NULL) { 429 goto tr_setup; 430 } 431 MCLGET(m, M_DONTWAIT); 432 433 if (!(m->m_flags & M_EXT)) { 434 m_freem(m); 435 goto tr_setup; 436 } 437 m->m_pkthdr.len = m->m_len = actlen; 438 439 pc = usbd_xfer_get_frame(xfer, 0); 440 usbd_copy_out(pc, 0, m->m_data, actlen); 441 442 sc->sc_bulk_in_buffer = m; 443 444 DPRINTF("received package %d bytes\n", actlen); 445 446 case USB_ST_SETUP: 447 tr_setup: 448 if (sc->sc_bulk_in_buffer) { 449 ng_send_fn(sc->sc_node, NULL, &udbp_bulk_read_complete, NULL, 0); 450 return; 451 } 452 if (sc->sc_flags & UDBP_FLAG_READ_STALL) { 453 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 454 return; 455 } 456 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 457 usbd_transfer_submit(xfer); 458 return; 459 460 default: /* Error */ 461 if (error != USB_ERR_CANCELLED) { 462 /* try to clear stall first */ 463 sc->sc_flags |= UDBP_FLAG_READ_STALL; 464 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 465 } 466 return; 467 468 } 469 } 470 471 static void 472 udbp_bulk_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 473 { 474 struct udbp_softc *sc = usbd_xfer_softc(xfer); 475 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_RD]; 476 477 if (usbd_clear_stall_callback(xfer, xfer_other)) { 478 DPRINTF("stall cleared\n"); 479 sc->sc_flags &= ~UDBP_FLAG_READ_STALL; 480 usbd_transfer_start(xfer_other); 481 } 482 } 483 484 static void 485 udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2) 486 { 487 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 488 struct mbuf *m; 489 int error; 490 491 if (sc == NULL) { 492 return; 493 } 494 mtx_lock(&sc->sc_mtx); 495 496 m = sc->sc_bulk_in_buffer; 497 498 if (m) { 499 500 sc->sc_bulk_in_buffer = NULL; 501 502 if ((sc->sc_hook == NULL) || 503 NG_HOOK_NOT_VALID(sc->sc_hook)) { 504 DPRINTF("No upstream hook\n"); 505 goto done; 506 } 507 sc->sc_packets_in++; 508 509 NG_SEND_DATA_ONLY(error, sc->sc_hook, m); 510 511 m = NULL; 512 } 513 done: 514 if (m) { 515 m_freem(m); 516 } 517 /* start USB bulk-in transfer, if not already started */ 518 519 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]); 520 521 mtx_unlock(&sc->sc_mtx); 522 } 523 524 static void 525 udbp_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 526 { 527 struct udbp_softc *sc = usbd_xfer_softc(xfer); 528 struct usb_page_cache *pc; 529 struct mbuf *m; 530 531 switch (USB_GET_STATE(xfer)) { 532 case USB_ST_TRANSFERRED: 533 534 sc->sc_packets_out++; 535 536 case USB_ST_SETUP: 537 if (sc->sc_flags & UDBP_FLAG_WRITE_STALL) { 538 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 539 return; 540 } 541 /* get next mbuf, if any */ 542 543 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq_hipri, m); 544 if (m == NULL) { 545 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq, m); 546 if (m == NULL) { 547 DPRINTF("Data queue is empty\n"); 548 return; 549 } 550 } 551 if (m->m_pkthdr.len > MCLBYTES) { 552 DPRINTF("truncating large packet " 553 "from %d to %d bytes\n", m->m_pkthdr.len, 554 MCLBYTES); 555 m->m_pkthdr.len = MCLBYTES; 556 } 557 pc = usbd_xfer_get_frame(xfer, 0); 558 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 559 560 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); 561 562 DPRINTF("packet out: %d bytes\n", m->m_pkthdr.len); 563 564 m_freem(m); 565 566 usbd_transfer_submit(xfer); 567 return; 568 569 default: /* Error */ 570 if (error != USB_ERR_CANCELLED) { 571 /* try to clear stall first */ 572 sc->sc_flags |= UDBP_FLAG_WRITE_STALL; 573 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 574 } 575 return; 576 577 } 578 } 579 580 static void 581 udbp_bulk_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 582 { 583 struct udbp_softc *sc = usbd_xfer_softc(xfer); 584 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_WR]; 585 586 if (usbd_clear_stall_callback(xfer, xfer_other)) { 587 DPRINTF("stall cleared\n"); 588 sc->sc_flags &= ~UDBP_FLAG_WRITE_STALL; 589 usbd_transfer_start(xfer_other); 590 } 591 } 592 593 /*********************************************************************** 594 * Start of Netgraph methods 595 **********************************************************************/ 596 597 /* 598 * If this is a device node so this work is done in the attach() 599 * routine and the constructor will return EINVAL as you should not be able 600 * to create nodes that depend on hardware (unless you can add the hardware :) 601 */ 602 static int 603 ng_udbp_constructor(node_p node) 604 { 605 return (EINVAL); 606 } 607 608 /* 609 * Give our ok for a hook to be added... 610 * If we are not running this might kick a device into life. 611 * Possibly decode information out of the hook name. 612 * Add the hook's private info to the hook structure. 613 * (if we had some). In this example, we assume that there is a 614 * an array of structs, called 'channel' in the private info, 615 * one for each active channel. The private 616 * pointer of each hook points to the appropriate UDBP_hookinfo struct 617 * so that the source of an input packet is easily identified. 618 */ 619 static int 620 ng_udbp_newhook(node_p node, hook_p hook, const char *name) 621 { 622 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 623 int32_t error = 0; 624 625 if (strcmp(name, NG_UDBP_HOOK_NAME)) { 626 return (EINVAL); 627 } 628 mtx_lock(&sc->sc_mtx); 629 630 if (sc->sc_hook != NULL) { 631 error = EISCONN; 632 } else { 633 sc->sc_hook = hook; 634 NG_HOOK_SET_PRIVATE(hook, NULL); 635 } 636 637 mtx_unlock(&sc->sc_mtx); 638 639 return (error); 640 } 641 642 /* 643 * Get a netgraph control message. 644 * Check it is one we understand. If needed, send a response. 645 * We could save the address for an async action later, but don't here. 646 * Always free the message. 647 * The response should be in a malloc'd region that the caller can 'free'. 648 * A response is not required. 649 * Theoretically you could respond defferently to old message types if 650 * the cookie in the header didn't match what we consider to be current 651 * (so that old userland programs could continue to work). 652 */ 653 static int 654 ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook) 655 { 656 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 657 struct ng_mesg *resp = NULL; 658 int error = 0; 659 struct ng_mesg *msg; 660 661 NGI_GET_MSG(item, msg); 662 /* Deal with message according to cookie and command */ 663 switch (msg->header.typecookie) { 664 case NGM_UDBP_COOKIE: 665 switch (msg->header.cmd) { 666 case NGM_UDBP_GET_STATUS: 667 { 668 struct ngudbpstat *stats; 669 670 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT); 671 if (!resp) { 672 error = ENOMEM; 673 break; 674 } 675 stats = (struct ngudbpstat *)resp->data; 676 mtx_lock(&sc->sc_mtx); 677 stats->packets_in = sc->sc_packets_in; 678 stats->packets_out = sc->sc_packets_out; 679 mtx_unlock(&sc->sc_mtx); 680 break; 681 } 682 case NGM_UDBP_SET_FLAG: 683 if (msg->header.arglen != sizeof(uint32_t)) { 684 error = EINVAL; 685 break; 686 } 687 DPRINTF("flags = 0x%08x\n", 688 *((uint32_t *)msg->data)); 689 break; 690 default: 691 error = EINVAL; /* unknown command */ 692 break; 693 } 694 break; 695 default: 696 error = EINVAL; /* unknown cookie type */ 697 break; 698 } 699 700 /* Take care of synchronous response, if any */ 701 NG_RESPOND_MSG(error, node, item, resp); 702 NG_FREE_MSG(msg); 703 return (error); 704 } 705 706 /* 707 * Accept data from the hook and queue it for output. 708 */ 709 static int 710 ng_udbp_rcvdata(hook_p hook, item_p item) 711 { 712 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 713 struct ng_bt_mbufq *queue_ptr; 714 struct mbuf *m; 715 struct ng_tag_prio *ptag; 716 int error; 717 718 if (sc == NULL) { 719 NG_FREE_ITEM(item); 720 return (EHOSTDOWN); 721 } 722 NGI_GET_M(item, m); 723 NG_FREE_ITEM(item); 724 725 /* 726 * Now queue the data for when it can be sent 727 */ 728 ptag = (void *)m_tag_locate(m, NGM_GENERIC_COOKIE, 729 NG_TAG_PRIO, NULL); 730 731 if (ptag && (ptag->priority > NG_PRIO_CUTOFF)) 732 queue_ptr = &sc->sc_xmitq_hipri; 733 else 734 queue_ptr = &sc->sc_xmitq; 735 736 mtx_lock(&sc->sc_mtx); 737 738 if (NG_BT_MBUFQ_FULL(queue_ptr)) { 739 NG_BT_MBUFQ_DROP(queue_ptr); 740 NG_FREE_M(m); 741 error = ENOBUFS; 742 } else { 743 NG_BT_MBUFQ_ENQUEUE(queue_ptr, m); 744 /* 745 * start bulk-out transfer, if not already started: 746 */ 747 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]); 748 error = 0; 749 } 750 751 mtx_unlock(&sc->sc_mtx); 752 753 return (error); 754 } 755 756 /* 757 * Do local shutdown processing.. 758 * We are a persistant device, we refuse to go away, and 759 * only remove our links and reset ourself. 760 */ 761 static int 762 ng_udbp_rmnode(node_p node) 763 { 764 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 765 766 /* Let old node go */ 767 NG_NODE_SET_PRIVATE(node, NULL); 768 NG_NODE_UNREF(node); /* forget it ever existed */ 769 770 if (sc == NULL) { 771 goto done; 772 } 773 /* Create Netgraph node */ 774 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 775 printf("%s: Could not create Netgraph node\n", 776 sc->sc_name); 777 sc->sc_node = NULL; 778 goto done; 779 } 780 /* Name node */ 781 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 782 printf("%s: Could not name Netgraph node\n", 783 sc->sc_name); 784 NG_NODE_UNREF(sc->sc_node); 785 sc->sc_node = NULL; 786 goto done; 787 } 788 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 789 790 done: 791 if (sc) { 792 mtx_unlock(&sc->sc_mtx); 793 } 794 return (0); 795 } 796 797 /* 798 * This is called once we've already connected a new hook to the other node. 799 * It gives us a chance to balk at the last minute. 800 */ 801 static int 802 ng_udbp_connect(hook_p hook) 803 { 804 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 805 806 /* probably not at splnet, force outward queueing */ 807 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 808 809 mtx_lock(&sc->sc_mtx); 810 811 sc->sc_flags |= (UDBP_FLAG_READ_STALL | 812 UDBP_FLAG_WRITE_STALL); 813 814 /* start bulk-in transfer */ 815 usbd_transfer_start(sc->sc_xfer[UDBP_T_RD]); 816 817 /* start bulk-out transfer */ 818 usbd_transfer_start(sc->sc_xfer[UDBP_T_WR]); 819 820 mtx_unlock(&sc->sc_mtx); 821 822 return (0); 823 } 824 825 /* 826 * Dook disconnection 827 * 828 * For this type, removal of the last link destroys the node 829 */ 830 static int 831 ng_udbp_disconnect(hook_p hook) 832 { 833 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 834 int error = 0; 835 836 if (sc != NULL) { 837 838 mtx_lock(&sc->sc_mtx); 839 840 if (hook != sc->sc_hook) { 841 error = EINVAL; 842 } else { 843 844 /* stop bulk-in transfer */ 845 usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD_CS]); 846 usbd_transfer_stop(sc->sc_xfer[UDBP_T_RD]); 847 848 /* stop bulk-out transfer */ 849 usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR_CS]); 850 usbd_transfer_stop(sc->sc_xfer[UDBP_T_WR]); 851 852 /* cleanup queues */ 853 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq); 854 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq_hipri); 855 856 if (sc->sc_bulk_in_buffer) { 857 m_freem(sc->sc_bulk_in_buffer); 858 sc->sc_bulk_in_buffer = NULL; 859 } 860 sc->sc_hook = NULL; 861 } 862 863 mtx_unlock(&sc->sc_mtx); 864 } 865 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 866 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 867 ng_rmnode_self(NG_HOOK_NODE(hook)); 868 869 return (error); 870 } 871