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