udbp.c revision 184610
1139823Simp/*- 21541Srgrimes * Copyright (c) 1996-2000 Whistle Communications, Inc. 31541Srgrimes * All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. Neither the name of author nor the names of its 141541Srgrimes * contributors may be used to endorse or promote products derived 151541Srgrimes * from this software without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY NICK HIBMA AND CONTRIBUTORS 181541Srgrimes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 191541Srgrimes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 201541Srgrimes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 211541Srgrimes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 221541Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 231541Srgrimes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 241541Srgrimes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 251541Srgrimes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 261541Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 271541Srgrimes * POSSIBILITY OF SUCH DAMAGE. 281541Srgrimes * 291541Srgrimes */ 3050477Speter 311541Srgrimes#include <sys/cdefs.h> 321541Srgrimes__FBSDID("$FreeBSD: head/sys/dev/usb2/misc/udbp2.c 184610 2008-11-04 02:31:03Z alfred $"); 331541Srgrimes 34130514Srwatson/* Driver for arbitrary double bulk pipe devices. 3595759Stanimura * The driver assumes that there will be the same driver on the other side. 36126425Srwatson * 371541Srgrimes * XXX Some more information on what the framing of the IP packets looks like. 38130387Srwatson * 391541Srgrimes * To take full advantage of bulk transmission, packets should be chosen 4095759Stanimura * between 1k and 5k in size (1k to make sure the sending side starts 411541Srgrimes * streaming, and <5k to avoid overflowing the system with small TDs). 421541Srgrimes */ 4395759Stanimura 4495759Stanimura 451541Srgrimes/* probe/attach/detach: 461541Srgrimes * Connect the driver to the hardware and netgraph 471541Srgrimes * 48130514Srwatson * The reason we submit a bulk in transfer is that USB does not know about 49130514Srwatson * interrupts. The bulk transfer continuously polls the device for data. 501541Srgrimes * While the device has no data available, the device NAKs the TDs. As soon 511541Srgrimes * as there is data, the transfer happens and the data comes flowing in. 521541Srgrimes * 531541Srgrimes * In case you were wondering, interrupt transfers happen exactly that way. 54107113Sluigi * It therefore doesn't make sense to use the interrupt pipe to signal 551541Srgrimes * 'data ready' and then schedule a bulk transfer to fetch it. That would 56130514Srwatson * incur a 2ms delay at least, without reducing bandwidth requirements. 5724936Sphk * 581541Srgrimes */ 591541Srgrimes 601541Srgrimes#include <dev/usb2/include/usb2_devid.h> 611541Srgrimes#include <dev/usb2/include/usb2_standard.h> 621541Srgrimes#include <dev/usb2/include/usb2_mfunc.h> 631541Srgrimes#include <dev/usb2/include/usb2_error.h> 641541Srgrimes 651541Srgrimes#define USB_DEBUG_VAR udbp_debug 661541Srgrimes 671541Srgrimes#include <dev/usb2/core/usb2_core.h> 681541Srgrimes#include <dev/usb2/core/usb2_debug.h> 691541Srgrimes#include <dev/usb2/core/usb2_parse.h> 70107113Sluigi#include <dev/usb2/core/usb2_lookup.h> 71107113Sluigi#include <dev/usb2/core/usb2_util.h> 72107113Sluigi#include <dev/usb2/core/usb2_busdma.h> 73107113Sluigi 741541Srgrimes#include <sys/mbuf.h> 75107113Sluigi 76107113Sluigi#include <netgraph/ng_message.h> 771541Srgrimes#include <netgraph/netgraph.h> 781541Srgrimes#include <netgraph/ng_parse.h> 79107113Sluigi#include <netgraph/bluetooth/include/ng_bluetooth.h> 80130514Srwatson 8124936Sphk#include <dev/usb2/misc/udbp2.h> 821541Srgrimes 831541Srgrimes#if USB_DEBUG 841541Srgrimesstatic int udbp_debug = 0; 851541Srgrimes 861541SrgrimesSYSCTL_NODE(_hw_usb2, OID_AUTO, udbp, CTLFLAG_RW, 0, "USB udbp"); 871541SrgrimesSYSCTL_INT(_hw_usb2_udbp, OID_AUTO, debug, CTLFLAG_RW, 881541Srgrimes &udbp_debug, 0, "udbp debug level"); 891541Srgrimes#endif 901541Srgrimes 911541Srgrimes#define UDBP_TIMEOUT 2000 /* timeout on outbound transfers, in 921541Srgrimes * msecs */ 931541Srgrimes#define UDBP_BUFFERSIZE MCLBYTES /* maximum number of bytes in one 941541Srgrimes * transfer */ 95107113Sluigi#define UDBP_T_WR 0 96107113Sluigi#define UDBP_T_RD 1 971541Srgrimes#define UDBP_T_WR_CS 2 981541Srgrimes#define UDBP_T_RD_CS 3 991541Srgrimes#define UDBP_T_MAX 4 1001541Srgrimes#define UDBP_Q_MAXLEN 50 1011541Srgrimes 102107113Sluigistruct udbp_softc { 103107113Sluigi 1043443Sphk struct mtx sc_mtx; 1051541Srgrimes struct ng_bt_mbufq sc_xmitq_hipri; /* hi-priority transmit queue */ 106107113Sluigi struct ng_bt_mbufq sc_xmitq; /* low-priority transmit queue */ 1071541Srgrimes 1081541Srgrimes struct usb2_xfer *sc_xfer[UDBP_T_MAX]; 1091541Srgrimes node_p sc_node; /* back pointer to node */ 1101541Srgrimes hook_p sc_hook; /* pointer to the hook */ 1111541Srgrimes struct mbuf *sc_bulk_in_buffer; 1121541Srgrimes 1131541Srgrimes uint32_t sc_packets_in; /* packets in from downstream */ 1141541Srgrimes uint32_t sc_packets_out; /* packets out towards downstream */ 1151541Srgrimes 1161541Srgrimes uint8_t sc_flags; 117107113Sluigi#define UDBP_FLAG_READ_STALL 0x01 /* read transfer stalled */ 118107113Sluigi#define UDBP_FLAG_WRITE_STALL 0x02 /* write transfer stalled */ 1191541Srgrimes 1201541Srgrimes uint8_t sc_name[16]; 1211541Srgrimes}; 1221541Srgrimes 1231541Srgrimes/* prototypes */ 1241541Srgrimes 125130514Srwatsonstatic int udbp_modload(module_t mod, int event, void *data); 1261541Srgrimes 1271541Srgrimesstatic device_probe_t udbp_probe; 1281541Srgrimesstatic device_attach_t udbp_attach; 1291541Srgrimesstatic device_detach_t udbp_detach; 130107113Sluigi 131107113Sluigistatic usb2_callback_t udbp_bulk_read_callback; 132107113Sluigistatic usb2_callback_t udbp_bulk_read_clear_stall_callback; 133107113Sluigistatic usb2_callback_t udbp_bulk_write_callback; 1341541Srgrimesstatic usb2_callback_t udbp_bulk_write_clear_stall_callback; 1351541Srgrimes 136119995Srustatic void udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2); 1371541Srgrimes 1381541Srgrimesstatic ng_constructor_t ng_udbp_constructor; 1391541Srgrimesstatic ng_rcvmsg_t ng_udbp_rcvmsg; 1401541Srgrimesstatic ng_shutdown_t ng_udbp_rmnode; 141157366Srwatsonstatic ng_newhook_t ng_udbp_newhook; 14225201Swollmanstatic ng_connect_t ng_udbp_connect; 1431541Srgrimesstatic ng_rcvdata_t ng_udbp_rcvdata; 14425201Swollmanstatic ng_disconnect_t ng_udbp_disconnect; 1451541Srgrimes 146157366Srwatson/* Parse type for struct ngudbpstat */ 14725201Swollmanstatic const struct ng_parse_struct_field 148130387Srwatson ng_udbp_stat_type_fields[] = NG_UDBP_STATS_TYPE_INFO; 14925201Swollman 1501541Srgrimesstatic const struct ng_parse_type ng_udbp_stat_type = { 15125201Swollman &ng_parse_struct_type, 1521541Srgrimes &ng_udbp_stat_type_fields 15325201Swollman}; 15483366Sjulian 15525201Swollman/* List of commands and how to convert arguments to/from ASCII */ 15625201Swollmanstatic const struct ng_cmdlist ng_udbp_cmdlist[] = { 15725201Swollman { 1581541Srgrimes NGM_UDBP_COOKIE, 15925201Swollman NGM_UDBP_GET_STATUS, 16025201Swollman "getstatus", 16193593Sjhb NULL, 16225201Swollman &ng_udbp_stat_type, 16325201Swollman }, 16425201Swollman { 1651541Srgrimes NGM_UDBP_COOKIE, 16625201Swollman NGM_UDBP_SET_FLAG, 16783366Sjulian "setflag", 16825201Swollman &ng_parse_int32_type, 16925201Swollman NULL 17025201Swollman }, 1711541Srgrimes {0} 17225201Swollman}; 17383366Sjulian 17425201Swollman/* Netgraph node type descriptor */ 17525201Swollmanstatic struct ng_type ng_udbp_typestruct = { 17625201Swollman .version = NG_ABI_VERSION, 1771541Srgrimes .name = NG_UDBP_NODE_TYPE, 17825201Swollman .constructor = ng_udbp_constructor, 17925201Swollman .rcvmsg = ng_udbp_rcvmsg, 1801541Srgrimes .shutdown = ng_udbp_rmnode, 18125201Swollman .newhook = ng_udbp_newhook, 18225201Swollman .connect = ng_udbp_connect, 18325201Swollman .rcvdata = ng_udbp_rcvdata, 18425201Swollman .disconnect = ng_udbp_disconnect, 1851541Srgrimes .cmdlist = ng_udbp_cmdlist, 18625201Swollman}; 18725201Swollman 1881541Srgrimes/* USB config */ 18925201Swollmanstatic const struct usb2_config udbp_config[UDBP_T_MAX] = { 19025201Swollman 19125201Swollman [UDBP_T_WR] = { 1921541Srgrimes .type = UE_BULK, 19325201Swollman .endpoint = UE_ADDR_ANY, 19425201Swollman .direction = UE_DIR_OUT, 19525201Swollman .mh.bufsize = UDBP_BUFFERSIZE, 19625201Swollman .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 1971541Srgrimes .mh.callback = &udbp_bulk_write_callback, 19825201Swollman .mh.timeout = UDBP_TIMEOUT, 19925201Swollman }, 200107113Sluigi 20125201Swollman [UDBP_T_RD] = { 202107113Sluigi .type = UE_BULK, 20325201Swollman .endpoint = UE_ADDR_ANY, 20425201Swollman .direction = UE_DIR_IN, 20525201Swollman .mh.bufsize = UDBP_BUFFERSIZE, 20625201Swollman .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 2071541Srgrimes .mh.callback = &udbp_bulk_read_callback, 20825201Swollman }, 2091541Srgrimes 21025201Swollman [UDBP_T_WR_CS] = { 21128270Swollman .type = UE_CONTROL, 21225201Swollman .endpoint = 0x00, /* Control pipe */ 21325201Swollman .direction = UE_DIR_ANY, 2141541Srgrimes .mh.bufsize = sizeof(struct usb2_device_request), 21525201Swollman .mh.flags = {}, 21625201Swollman .mh.callback = &udbp_bulk_write_clear_stall_callback, 217107113Sluigi .mh.timeout = 1000, /* 1 second */ 21825201Swollman .mh.interval = 50, /* 50ms */ 219107113Sluigi }, 220126425Srwatson 22125201Swollman [UDBP_T_RD_CS] = { 22225201Swollman .type = UE_CONTROL, 2231541Srgrimes .endpoint = 0x00, /* Control pipe */ 22425201Swollman .direction = UE_DIR_ANY, 22525201Swollman .mh.bufsize = sizeof(struct usb2_device_request), 22625201Swollman .mh.flags = {}, 22725201Swollman .mh.callback = &udbp_bulk_read_clear_stall_callback, 22825201Swollman .mh.timeout = 1000, /* 1 second */ 22983366Sjulian .mh.interval = 50, /* 50ms */ 23025201Swollman }, 23125201Swollman}; 23225201Swollman 23325201Swollmanstatic devclass_t udbp_devclass; 23425201Swollman 23525201Swollmanstatic device_method_t udbp_methods[] = { 23625201Swollman /* Device interface */ 2371541Srgrimes DEVMETHOD(device_probe, udbp_probe), 23825201Swollman DEVMETHOD(device_attach, udbp_attach), 23925201Swollman DEVMETHOD(device_detach, udbp_detach), 24025201Swollman {0, 0} 24125201Swollman}; 24225201Swollman 24325201Swollmanstatic driver_t udbp_driver = { 24425201Swollman .name = "udbp", 24525201Swollman .methods = udbp_methods, 24625201Swollman .size = sizeof(struct udbp_softc), 24725201Swollman}; 24825201Swollman 24925201SwollmanDRIVER_MODULE(udbp, ushub, udbp_driver, udbp_devclass, udbp_modload, 0); 25025201SwollmanMODULE_DEPEND(udbp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); 25125201SwollmanMODULE_DEPEND(udbp, usb2_misc, 1, 1, 1); 25225201SwollmanMODULE_DEPEND(udbp, usb2_core, 1, 1, 1); 25328270Swollman 25425201Swollmanstatic int 25525201Swollmanudbp_modload(module_t mod, int event, void *data) 25625201Swollman{ 25725201Swollman int error; 25825201Swollman 25925201Swollman switch (event) { 26025201Swollman case MOD_LOAD: 26125201Swollman error = ng_newtype(&ng_udbp_typestruct); 2621541Srgrimes if (error != 0) { 2631541Srgrimes printf("%s: Could not register " 2641541Srgrimes "Netgraph node type, error=%d\n", 2651541Srgrimes NG_UDBP_NODE_TYPE, error); 2661541Srgrimes } 26725201Swollman break; 26825201Swollman 26925201Swollman case MOD_UNLOAD: 27025201Swollman error = ng_rmtype(&ng_udbp_typestruct); 27125201Swollman break; 27225201Swollman 27325201Swollman default: 27425201Swollman error = EOPNOTSUPP; 27525201Swollman break; 27625201Swollman } 27725201Swollman return (error); 27825201Swollman} 27925201Swollman 28025201Swollmanstatic int 28125201Swollmanudbp_probe(device_t dev) 28228270Swollman{ 28325201Swollman struct usb2_attach_arg *uaa = device_get_ivars(dev); 28425201Swollman 28525201Swollman if (uaa->usb2_mode != USB_MODE_HOST) { 28625201Swollman return (ENXIO); 28725201Swollman } 28825201Swollman /* 28925201Swollman * XXX Julian, add the id of the device if you have one to test 290126425Srwatson * things with. run 'usbdevs -v' and note the 3 ID's that appear. 29125201Swollman * The Vendor Id and Product Id are in hex and the Revision Id is in 29225201Swollman * bcd. But as usual if the revision is 0x101 then you should 29325201Swollman * compare the revision id in the device descriptor with 0x101 Or go 29425201Swollman * search the file usbdevs.h. Maybe the device is already in there. 295137386Sphk */ 296137386Sphk if (((uaa->info.idVendor == USB_VENDOR_NETCHIP) && 297137386Sphk (uaa->info.idProduct == USB_PRODUCT_NETCHIP_TURBOCONNECT))) 298137386Sphk return (0); 299137386Sphk 300137386Sphk if (((uaa->info.idVendor == USB_VENDOR_PROLIFIC) && 301137386Sphk ((uaa->info.idProduct == USB_PRODUCT_PROLIFIC_PL2301) || 302137386Sphk (uaa->info.idProduct == USB_PRODUCT_PROLIFIC_PL2302)))) 303137386Sphk return (0); 304137386Sphk 30525201Swollman if ((uaa->info.idVendor == USB_VENDOR_ANCHOR) && 306 (uaa->info.idProduct == USB_PRODUCT_ANCHOR_EZLINK)) 307 return (0); 308 309 if ((uaa->info.idVendor == USB_VENDOR_GENESYS) && 310 (uaa->info.idProduct == USB_PRODUCT_GENESYS_GL620USB)) 311 return (0); 312 313 return (ENXIO); 314} 315 316static int 317udbp_attach(device_t dev) 318{ 319 struct usb2_attach_arg *uaa = device_get_ivars(dev); 320 struct udbp_softc *sc = device_get_softc(dev); 321 int error; 322 323 if (sc == NULL) { 324 return (ENOMEM); 325 } 326 device_set_usb2_desc(dev); 327 328 snprintf(sc->sc_name, sizeof(sc->sc_name), 329 "%s", device_get_nameunit(dev)); 330 331 mtx_init(&sc->sc_mtx, "udbp lock", NULL, MTX_DEF | MTX_RECURSE); 332 333 error = usb2_transfer_setup(uaa->device, &uaa->info.bIfaceIndex, 334 sc->sc_xfer, udbp_config, UDBP_T_MAX, sc, &sc->sc_mtx); 335 if (error) { 336 DPRINTF("error=%s\n", usb2_errstr(error)); 337 goto detach; 338 } 339 NG_BT_MBUFQ_INIT(&sc->sc_xmitq, UDBP_Q_MAXLEN); 340 341 NG_BT_MBUFQ_INIT(&sc->sc_xmitq_hipri, UDBP_Q_MAXLEN); 342 343 /* create Netgraph node */ 344 345 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 346 printf("%s: Could not create Netgraph node\n", 347 sc->sc_name); 348 sc->sc_node = NULL; 349 goto detach; 350 } 351 /* name node */ 352 353 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 354 printf("%s: Could not name node\n", 355 sc->sc_name); 356 NG_NODE_UNREF(sc->sc_node); 357 sc->sc_node = NULL; 358 goto detach; 359 } 360 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 361 362 /* the device is now operational */ 363 364 return (0); /* success */ 365 366detach: 367 udbp_detach(dev); 368 return (ENOMEM); /* failure */ 369} 370 371static int 372udbp_detach(device_t dev) 373{ 374 struct udbp_softc *sc = device_get_softc(dev); 375 376 /* destroy Netgraph node */ 377 378 if (sc->sc_node != NULL) { 379 NG_NODE_SET_PRIVATE(sc->sc_node, NULL); 380 ng_rmnode_self(sc->sc_node); 381 sc->sc_node = NULL; 382 } 383 /* free USB transfers, if any */ 384 385 usb2_transfer_unsetup(sc->sc_xfer, UDBP_T_MAX); 386 387 mtx_destroy(&sc->sc_mtx); 388 389 /* destroy queues */ 390 391 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq); 392 NG_BT_MBUFQ_DESTROY(&sc->sc_xmitq_hipri); 393 394 /* extra check */ 395 396 if (sc->sc_bulk_in_buffer) { 397 m_freem(sc->sc_bulk_in_buffer); 398 sc->sc_bulk_in_buffer = NULL; 399 } 400 return (0); /* success */ 401} 402 403static void 404udbp_bulk_read_callback(struct usb2_xfer *xfer) 405{ 406 struct udbp_softc *sc = xfer->priv_sc; 407 struct mbuf *m; 408 409 switch (USB_GET_STATE(xfer)) { 410 case USB_ST_TRANSFERRED: 411 412 /* allocate new mbuf */ 413 414 MGETHDR(m, M_DONTWAIT, MT_DATA); 415 416 if (m == NULL) { 417 goto tr_setup; 418 } 419 MCLGET(m, M_DONTWAIT); 420 421 if (!(m->m_flags & M_EXT)) { 422 m_freem(m); 423 goto tr_setup; 424 } 425 m->m_pkthdr.len = m->m_len = xfer->actlen; 426 427 usb2_copy_out(xfer->frbuffers, 0, m->m_data, xfer->actlen); 428 429 sc->sc_bulk_in_buffer = m; 430 431 DPRINTF("received package %d " 432 "bytes\n", xfer->actlen); 433 434 case USB_ST_SETUP: 435tr_setup: 436 if (sc->sc_bulk_in_buffer) { 437 ng_send_fn(sc->sc_node, NULL, &udbp_bulk_read_complete, NULL, 0); 438 return; 439 } 440 if (sc->sc_flags & UDBP_FLAG_READ_STALL) { 441 usb2_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 442 return; 443 } 444 xfer->frlengths[0] = xfer->max_data_length; 445 usb2_start_hardware(xfer); 446 return; 447 448 default: /* Error */ 449 if (xfer->error != USB_ERR_CANCELLED) { 450 /* try to clear stall first */ 451 sc->sc_flags |= UDBP_FLAG_READ_STALL; 452 usb2_transfer_start(sc->sc_xfer[UDBP_T_RD_CS]); 453 } 454 return; 455 456 } 457} 458 459static void 460udbp_bulk_read_clear_stall_callback(struct usb2_xfer *xfer) 461{ 462 struct udbp_softc *sc = xfer->priv_sc; 463 struct usb2_xfer *xfer_other = sc->sc_xfer[UDBP_T_RD]; 464 465 if (usb2_clear_stall_callback(xfer, xfer_other)) { 466 DPRINTF("stall cleared\n"); 467 sc->sc_flags &= ~UDBP_FLAG_READ_STALL; 468 usb2_transfer_start(xfer_other); 469 } 470 return; 471} 472 473static void 474udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2) 475{ 476 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 477 struct mbuf *m; 478 int error; 479 480 if (sc == NULL) { 481 return; 482 } 483 mtx_lock(&sc->sc_mtx); 484 485 m = sc->sc_bulk_in_buffer; 486 487 if (m) { 488 489 sc->sc_bulk_in_buffer = NULL; 490 491 if ((sc->sc_hook == NULL) || 492 NG_HOOK_NOT_VALID(sc->sc_hook)) { 493 DPRINTF("No upstream hook\n"); 494 goto done; 495 } 496 sc->sc_packets_in++; 497 498 NG_SEND_DATA_ONLY(error, sc->sc_hook, m); 499 500 m = NULL; 501 } 502done: 503 if (m) { 504 m_freem(m); 505 } 506 /* start USB bulk-in transfer, if not already started */ 507 508 usb2_transfer_start(sc->sc_xfer[UDBP_T_RD]); 509 510 mtx_unlock(&sc->sc_mtx); 511 512 return; 513} 514 515static void 516udbp_bulk_write_callback(struct usb2_xfer *xfer) 517{ 518 struct udbp_softc *sc = xfer->priv_sc; 519 struct mbuf *m; 520 521 switch (USB_GET_STATE(xfer)) { 522 case USB_ST_TRANSFERRED: 523 524 sc->sc_packets_out++; 525 526 case USB_ST_SETUP: 527 if (sc->sc_flags & UDBP_FLAG_WRITE_STALL) { 528 usb2_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 529 return; 530 } 531 /* get next mbuf, if any */ 532 533 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq_hipri, m); 534 if (m == NULL) { 535 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq, m); 536 if (m == NULL) { 537 DPRINTF("Data queue is empty\n"); 538 return; 539 } 540 } 541 if (m->m_pkthdr.len > MCLBYTES) { 542 DPRINTF("truncating large packet " 543 "from %d to %d bytes\n", m->m_pkthdr.len, 544 MCLBYTES); 545 m->m_pkthdr.len = MCLBYTES; 546 } 547 usb2_m_copy_in(xfer->frbuffers, 0, m, 0, m->m_pkthdr.len); 548 549 xfer->frlengths[0] = m->m_pkthdr.len; 550 551 m_freem(m); 552 553 DPRINTF("packet out: %d bytes\n", 554 xfer->frlengths[0]); 555 556 usb2_start_hardware(xfer); 557 return; 558 559 default: /* Error */ 560 if (xfer->error != USB_ERR_CANCELLED) { 561 /* try to clear stall first */ 562 sc->sc_flags |= UDBP_FLAG_WRITE_STALL; 563 usb2_transfer_start(sc->sc_xfer[UDBP_T_WR_CS]); 564 } 565 return; 566 567 } 568} 569 570static void 571udbp_bulk_write_clear_stall_callback(struct usb2_xfer *xfer) 572{ 573 struct udbp_softc *sc = xfer->priv_sc; 574 struct usb2_xfer *xfer_other = sc->sc_xfer[UDBP_T_WR]; 575 576 if (usb2_clear_stall_callback(xfer, xfer_other)) { 577 DPRINTF("stall cleared\n"); 578 sc->sc_flags &= ~UDBP_FLAG_WRITE_STALL; 579 usb2_transfer_start(xfer_other); 580 } 581 return; 582} 583 584/*********************************************************************** 585 * Start of Netgraph methods 586 **********************************************************************/ 587 588/* 589 * If this is a device node so this work is done in the attach() 590 * routine and the constructor will return EINVAL as you should not be able 591 * to create nodes that depend on hardware (unless you can add the hardware :) 592 */ 593static int 594ng_udbp_constructor(node_p node) 595{ 596 return (EINVAL); 597} 598 599/* 600 * Give our ok for a hook to be added... 601 * If we are not running this might kick a device into life. 602 * Possibly decode information out of the hook name. 603 * Add the hook's private info to the hook structure. 604 * (if we had some). In this example, we assume that there is a 605 * an array of structs, called 'channel' in the private info, 606 * one for each active channel. The private 607 * pointer of each hook points to the appropriate UDBP_hookinfo struct 608 * so that the source of an input packet is easily identified. 609 */ 610static int 611ng_udbp_newhook(node_p node, hook_p hook, const char *name) 612{ 613 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 614 int32_t error = 0; 615 616 if (strcmp(name, NG_UDBP_HOOK_NAME)) { 617 return (EINVAL); 618 } 619 mtx_lock(&sc->sc_mtx); 620 621 if (sc->sc_hook != NULL) { 622 error = EISCONN; 623 } else { 624 sc->sc_hook = hook; 625 NG_HOOK_SET_PRIVATE(hook, NULL); 626 } 627 628 mtx_unlock(&sc->sc_mtx); 629 630 return (error); 631} 632 633/* 634 * Get a netgraph control message. 635 * Check it is one we understand. If needed, send a response. 636 * We could save the address for an async action later, but don't here. 637 * Always free the message. 638 * The response should be in a malloc'd region that the caller can 'free'. 639 * A response is not required. 640 * Theoretically you could respond defferently to old message types if 641 * the cookie in the header didn't match what we consider to be current 642 * (so that old userland programs could continue to work). 643 */ 644static int 645ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook) 646{ 647 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 648 struct ng_mesg *resp = NULL; 649 int error = 0; 650 struct ng_mesg *msg; 651 652 NGI_GET_MSG(item, msg); 653 /* Deal with message according to cookie and command */ 654 switch (msg->header.typecookie) { 655 case NGM_UDBP_COOKIE: 656 switch (msg->header.cmd) { 657 case NGM_UDBP_GET_STATUS: 658 { 659 struct ngudbpstat *stats; 660 661 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT); 662 if (!resp) { 663 error = ENOMEM; 664 break; 665 } 666 stats = (struct ngudbpstat *)resp->data; 667 mtx_lock(&sc->sc_mtx); 668 stats->packets_in = sc->sc_packets_in; 669 stats->packets_out = sc->sc_packets_out; 670 mtx_unlock(&sc->sc_mtx); 671 break; 672 } 673 case NGM_UDBP_SET_FLAG: 674 if (msg->header.arglen != sizeof(uint32_t)) { 675 error = EINVAL; 676 break; 677 } 678 DPRINTF("flags = 0x%08x\n", 679 *((uint32_t *)msg->data)); 680 break; 681 default: 682 error = EINVAL; /* unknown command */ 683 break; 684 } 685 break; 686 default: 687 error = EINVAL; /* unknown cookie type */ 688 break; 689 } 690 691 /* Take care of synchronous response, if any */ 692 NG_RESPOND_MSG(error, node, item, resp); 693 NG_FREE_MSG(msg); 694 return (error); 695} 696 697/* 698 * Accept data from the hook and queue it for output. 699 */ 700static int 701ng_udbp_rcvdata(hook_p hook, item_p item) 702{ 703 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 704 struct ng_bt_mbufq *queue_ptr; 705 struct mbuf *m; 706 struct ng_tag_prio *ptag; 707 int error; 708 709 if (sc == NULL) { 710 NG_FREE_ITEM(item); 711 return (EHOSTDOWN); 712 } 713 NGI_GET_M(item, m); 714 NG_FREE_ITEM(item); 715 716 /* 717 * Now queue the data for when it can be sent 718 */ 719 ptag = (void *)m_tag_locate(m, NGM_GENERIC_COOKIE, 720 NG_TAG_PRIO, NULL); 721 722 if (ptag && (ptag->priority > NG_PRIO_CUTOFF)) 723 queue_ptr = &sc->sc_xmitq_hipri; 724 else 725 queue_ptr = &sc->sc_xmitq; 726 727 mtx_lock(&sc->sc_mtx); 728 729 if (NG_BT_MBUFQ_FULL(queue_ptr)) { 730 NG_BT_MBUFQ_DROP(queue_ptr); 731 NG_FREE_M(m); 732 error = ENOBUFS; 733 } else { 734 NG_BT_MBUFQ_ENQUEUE(queue_ptr, m); 735 /* 736 * start bulk-out transfer, if not already started: 737 */ 738 usb2_transfer_start(sc->sc_xfer[UDBP_T_WR]); 739 error = 0; 740 } 741 742 mtx_unlock(&sc->sc_mtx); 743 744 return (error); 745} 746 747/* 748 * Do local shutdown processing.. 749 * We are a persistant device, we refuse to go away, and 750 * only remove our links and reset ourself. 751 */ 752static int 753ng_udbp_rmnode(node_p node) 754{ 755 struct udbp_softc *sc = NG_NODE_PRIVATE(node); 756 757 /* Let old node go */ 758 NG_NODE_SET_PRIVATE(node, NULL); 759 NG_NODE_UNREF(node); /* forget it ever existed */ 760 761 if (sc == NULL) { 762 goto done; 763 } 764 /* Create Netgraph node */ 765 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) { 766 printf("%s: Could not create Netgraph node\n", 767 sc->sc_name); 768 sc->sc_node = NULL; 769 goto done; 770 } 771 /* Name node */ 772 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) { 773 printf("%s: Could not name Netgraph node\n", 774 sc->sc_name); 775 NG_NODE_UNREF(sc->sc_node); 776 sc->sc_node = NULL; 777 goto done; 778 } 779 NG_NODE_SET_PRIVATE(sc->sc_node, sc); 780 781done: 782 if (sc) { 783 mtx_unlock(&sc->sc_mtx); 784 } 785 return (0); 786} 787 788/* 789 * This is called once we've already connected a new hook to the other node. 790 * It gives us a chance to balk at the last minute. 791 */ 792static int 793ng_udbp_connect(hook_p hook) 794{ 795 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 796 797 /* probably not at splnet, force outward queueing */ 798 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 799 800 mtx_lock(&sc->sc_mtx); 801 802 sc->sc_flags |= (UDBP_FLAG_READ_STALL | 803 UDBP_FLAG_WRITE_STALL); 804 805 /* start bulk-in transfer */ 806 usb2_transfer_start(sc->sc_xfer[UDBP_T_RD]); 807 808 /* start bulk-out transfer */ 809 usb2_transfer_start(sc->sc_xfer[UDBP_T_WR]); 810 811 mtx_unlock(&sc->sc_mtx); 812 813 return (0); 814} 815 816/* 817 * Dook disconnection 818 * 819 * For this type, removal of the last link destroys the node 820 */ 821static int 822ng_udbp_disconnect(hook_p hook) 823{ 824 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 825 int error = 0; 826 827 if (sc != NULL) { 828 829 mtx_lock(&sc->sc_mtx); 830 831 if (hook != sc->sc_hook) { 832 error = EINVAL; 833 } else { 834 835 /* stop bulk-in transfer */ 836 usb2_transfer_stop(sc->sc_xfer[UDBP_T_RD_CS]); 837 usb2_transfer_stop(sc->sc_xfer[UDBP_T_RD]); 838 839 /* stop bulk-out transfer */ 840 usb2_transfer_stop(sc->sc_xfer[UDBP_T_WR_CS]); 841 usb2_transfer_stop(sc->sc_xfer[UDBP_T_WR]); 842 843 /* cleanup queues */ 844 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq); 845 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq_hipri); 846 847 if (sc->sc_bulk_in_buffer) { 848 m_freem(sc->sc_bulk_in_buffer); 849 sc->sc_bulk_in_buffer = NULL; 850 } 851 sc->sc_hook = NULL; 852 } 853 854 mtx_unlock(&sc->sc_mtx); 855 } 856 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 857 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 858 ng_rmnode_self(NG_HOOK_NODE(hook)); 859 860 return (error); 861} 862