FreeBSD kernel usb device Code
udbp.c
Go to the documentation of this file.
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/* probe/attach/detach:
47 * Connect the driver to the hardware and netgraph
48 *
49 * The reason we submit a bulk in transfer is that USB does not know about
50 * interrupts. The bulk transfer continuously polls the device for data.
51 * While the device has no data available, the device NAKs the TDs. As soon
52 * as there is data, the transfer happens and the data comes flowing in.
53 *
54 * In case you were wondering, interrupt transfers happen exactly that way.
55 * It therefore doesn't make sense to use the interrupt pipe to signal
56 * 'data ready' and then schedule a bulk transfer to fetch it. That would
57 * incur a 2ms delay at least, without reducing bandwidth requirements.
58 *
59 */
60
61#include <sys/stdint.h>
62#include <sys/stddef.h>
63#include <sys/param.h>
64#include <sys/queue.h>
65#include <sys/types.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/bus.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
100static SYSCTL_NODE(_hw_usb, OID_AUTO, udbp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
101 "USB udbp");
102SYSCTL_INT(_hw_usb_udbp, OID_AUTO, debug, CTLFLAG_RWTUN,
103 &udbp_debug, 0, "udbp debug level");
104#endif
105
106#define UDBP_TIMEOUT 2000 /* timeout on outbound transfers, in
107 * msecs */
108#define UDBP_BUFFERSIZE MCLBYTES /* maximum number of bytes in one
109 * transfer */
110#define UDBP_T_WR 0
111#define UDBP_T_RD 1
112#define UDBP_T_WR_CS 2
113#define UDBP_T_RD_CS 3
114#define UDBP_T_MAX 4
115#define UDBP_Q_MAXLEN 50
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 */
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;
127 uint32_t sc_packets_in; /* packets in from downstream */
128 uint32_t sc_packets_out; /* packets out towards downstream */
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);
141static device_probe_t udbp_probe;
142static device_attach_t udbp_attach;
143static device_detach_t udbp_detach;
149
150static void udbp_bulk_read_complete(node_p, hook_p, void *, int);
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
163
164static const struct ng_parse_type ng_udbp_stat_type = {
165 &ng_parse_struct_type,
167};
169/* List of commands and how to convert arguments to/from ASCII */
170static const struct ng_cmdlist ng_udbp_cmdlist[] = {
171 {
174 "getstatus",
175 NULL,
177 },
178 {
181 "setflag",
182 &ng_parse_int32_type,
183 NULL
184 },
185 {0}
186};
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};
202/* USB config */
203static const struct usb_config udbp_config[UDBP_T_MAX] = {
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),
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),
239 .timeout = 1000, /* 1 second */
240 .interval = 50, /* 50ms */
241 },
243
244static devclass_t udbp_devclass;
245
246static 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
252 DEVMETHOD_END
254
255static driver_t udbp_driver = {
256 .name = "udbp",
257 .methods = udbp_methods,
258 .size = sizeof(struct udbp_softc),
260
261static const STRUCT_USB_HOST_ID udbp_devs[] = {
262 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U258, 0)},
263 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_TURBOCONNECT, 0)},
264 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_GADGETZERO, 0)},
265 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301, 0)},
266 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302, 0)},
267 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL27A1, 0)},
268 {USB_VPI(USB_VENDOR_ANCHOR, USB_PRODUCT_ANCHOR_EZLINK, 0)},
269 {USB_VPI(USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL620USB, 0)},
273MODULE_DEPEND(udbp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
274MODULE_DEPEND(udbp, usb, 1, 1, 1);
275MODULE_VERSION(udbp, 1);
278static int
279udbp_modload(module_t mod, int event, void *data)
280{
281 int error;
282
283 switch (event) {
284 case MOD_LOAD:
285 error = ng_newtype(&ng_udbp_typestruct);
286 if (error != 0) {
287 printf("%s: Could not register "
288 "Netgraph node type, error=%d\n",
290 }
291 break;
292
293 case MOD_UNLOAD:
294 error = ng_rmtype(&ng_udbp_typestruct);
295 break;
296
297 default:
298 error = EOPNOTSUPP;
299 break;
300 }
301 return (error);
302}
304static int
305udbp_probe(device_t dev)
306{
307 struct usb_attach_arg *uaa = device_get_ivars(dev);
308
309 if (uaa->usb_mode != USB_MODE_HOST)
310 return (ENXIO);
311 if (uaa->info.bConfigIndex != 0)
312 return (ENXIO);
313 if (uaa->info.bIfaceIndex != 0)
314 return (ENXIO);
315
316 return (usbd_lookup_id_by_uaa(udbp_devs, sizeof(udbp_devs), uaa));
317}
319static int
320udbp_attach(device_t dev)
321{
322 struct usb_attach_arg *uaa = device_get_ivars(dev);
323 struct udbp_softc *sc = device_get_softc(dev);
324 int error;
325
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
334 sc->sc_xfer, udbp_config, UDBP_T_MAX, sc, &sc->sc_mtx);
335 if (error) {
336 DPRINTF("error=%s\n", usbd_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:
368 return (ENOMEM); /* failure */
369}
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
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}
403static void
405{
406 struct udbp_softc *sc = usbd_xfer_softc(xfer);
407 struct usb_page_cache *pc;
408 struct mbuf *m;
409 int actlen;
410
411 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
412
413 switch (USB_GET_STATE(xfer)) {
415
416 /* allocate new mbuf */
417
418 MGETHDR(m, M_NOWAIT, MT_DATA);
419
420 if (m == NULL) {
421 goto tr_setup;
422 }
423
424 if (!(MCLGET(m, M_NOWAIT))) {
425 m_freem(m);
426 goto tr_setup;
427 }
428 m->m_pkthdr.len = m->m_len = actlen;
429
430 pc = usbd_xfer_get_frame(xfer, 0);
431 usbd_copy_out(pc, 0, m->m_data, actlen);
432
433 sc->sc_bulk_in_buffer = m;
434
435 DPRINTF("received package %d bytes\n", actlen);
436
437 case USB_ST_SETUP:
438tr_setup:
439 if (sc->sc_bulk_in_buffer) {
440 ng_send_fn(sc->sc_node, NULL, &udbp_bulk_read_complete, NULL, 0);
441 return;
442 }
443 if (sc->sc_flags & UDBP_FLAG_READ_STALL) {
445 return;
446 }
449 return;
450
451 default: /* Error */
452 if (error != USB_ERR_CANCELLED) {
453 /* try to clear stall first */
456 }
457 return;
458 }
459}
461static void
463{
464 struct udbp_softc *sc = usbd_xfer_softc(xfer);
465 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_RD];
466
467 if (usbd_clear_stall_callback(xfer, xfer_other)) {
468 DPRINTF("stall cleared\n");
469 sc->sc_flags &= ~UDBP_FLAG_READ_STALL;
470 usbd_transfer_start(xfer_other);
471 }
472}
474static void
475udbp_bulk_read_complete(node_p node, hook_p hook, void *arg1, int arg2)
476{
477 struct udbp_softc *sc = NG_NODE_PRIVATE(node);
478 struct mbuf *m;
479 int error;
480
481 if (sc == NULL) {
482 return;
483 }
484 mtx_lock(&sc->sc_mtx);
485
486 m = sc->sc_bulk_in_buffer;
487
488 if (m) {
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
509
510 mtx_unlock(&sc->sc_mtx);
511}
513static void
515{
516 struct udbp_softc *sc = usbd_xfer_softc(xfer);
517 struct usb_page_cache *pc;
518 struct mbuf *m;
519
520 switch (USB_GET_STATE(xfer)) {
522
523 sc->sc_packets_out++;
524
525 case USB_ST_SETUP:
528 return;
529 }
530 /* get next mbuf, if any */
531
532 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq_hipri, m);
533 if (m == NULL) {
534 NG_BT_MBUFQ_DEQUEUE(&sc->sc_xmitq, m);
535 if (m == NULL) {
536 DPRINTF("Data queue is empty\n");
537 return;
538 }
539 }
540 if (m->m_pkthdr.len > MCLBYTES) {
541 DPRINTF("truncating large packet "
542 "from %d to %d bytes\n", m->m_pkthdr.len,
543 MCLBYTES);
544 m->m_pkthdr.len = MCLBYTES;
545 }
546 pc = usbd_xfer_get_frame(xfer, 0);
547 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
548
549 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
550
551 DPRINTF("packet out: %d bytes\n", m->m_pkthdr.len);
552
553 m_freem(m);
554
556 return;
557
558 default: /* Error */
559 if (error != USB_ERR_CANCELLED) {
560 /* try to clear stall first */
563 }
564 return;
565 }
566}
568static void
570{
571 struct udbp_softc *sc = usbd_xfer_softc(xfer);
572 struct usb_xfer *xfer_other = sc->sc_xfer[UDBP_T_WR];
573
574 if (usbd_clear_stall_callback(xfer, xfer_other)) {
575 DPRINTF("stall cleared\n");
576 sc->sc_flags &= ~UDBP_FLAG_WRITE_STALL;
577 usbd_transfer_start(xfer_other);
578 }
579}
580
581/***********************************************************************
582 * Start of Netgraph methods
583 **********************************************************************/
584
585/*
586 * If this is a device node so this work is done in the attach()
587 * routine and the constructor will return EINVAL as you should not be able
588 * to create nodes that depend on hardware (unless you can add the hardware :)
589 */
590static int
591ng_udbp_constructor(node_p node)
592{
593 return (EINVAL);
594}
595
596/*
597 * Give our ok for a hook to be added...
598 * If we are not running this might kick a device into life.
599 * Possibly decode information out of the hook name.
600 * Add the hook's private info to the hook structure.
601 * (if we had some). In this example, we assume that there is a
602 * an array of structs, called 'channel' in the private info,
603 * one for each active channel. The private
604 * pointer of each hook points to the appropriate UDBP_hookinfo struct
605 * so that the source of an input packet is easily identified.
606 */
607static int
608ng_udbp_newhook(node_p node, hook_p hook, const char *name)
609{
610 struct udbp_softc *sc = NG_NODE_PRIVATE(node);
611 int32_t error = 0;
612
613 if (strcmp(name, NG_UDBP_HOOK_NAME)) {
614 return (EINVAL);
615 }
616 mtx_lock(&sc->sc_mtx);
617
618 if (sc->sc_hook != NULL) {
619 error = EISCONN;
620 } else {
621 sc->sc_hook = hook;
622 NG_HOOK_SET_PRIVATE(hook, NULL);
623 }
624
625 mtx_unlock(&sc->sc_mtx);
626
627 return (error);
628}
629
630/*
631 * Get a netgraph control message.
632 * Check it is one we understand. If needed, send a response.
633 * We could save the address for an async action later, but don't here.
634 * Always free the message.
635 * The response should be in a malloc'd region that the caller can 'free'.
636 * A response is not required.
637 * Theoretically you could respond defferently to old message types if
638 * the cookie in the header didn't match what we consider to be current
639 * (so that old userland programs could continue to work).
640 */
641static int
642ng_udbp_rcvmsg(node_p node, item_p item, hook_p lasthook)
643{
644 struct udbp_softc *sc = NG_NODE_PRIVATE(node);
645 struct ng_mesg *resp = NULL;
646 int error = 0;
647 struct ng_mesg *msg;
648
649 NGI_GET_MSG(item, msg);
650 /* Deal with message according to cookie and command */
651 switch (msg->header.typecookie) {
652 case NGM_UDBP_COOKIE:
653 switch (msg->header.cmd) {
655 {
656 struct ngudbpstat *stats;
657
658 NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);
659 if (!resp) {
660 error = ENOMEM;
661 break;
662 }
663 stats = (struct ngudbpstat *)resp->data;
664 mtx_lock(&sc->sc_mtx);
665 stats->packets_in = sc->sc_packets_in;
666 stats->packets_out = sc->sc_packets_out;
667 mtx_unlock(&sc->sc_mtx);
668 break;
669 }
671 if (msg->header.arglen != sizeof(uint32_t)) {
672 error = EINVAL;
673 break;
674 }
675 DPRINTF("flags = 0x%08x\n",
676 *((uint32_t *)msg->data));
677 break;
678 default:
679 error = EINVAL; /* unknown command */
680 break;
681 }
682 break;
683 default:
684 error = EINVAL; /* unknown cookie type */
685 break;
686 }
687
688 /* Take care of synchronous response, if any */
689 NG_RESPOND_MSG(error, node, item, resp);
690 NG_FREE_MSG(msg);
691 return (error);
692}
693
694/*
695 * Accept data from the hook and queue it for output.
696 */
697static int
698ng_udbp_rcvdata(hook_p hook, item_p item)
699{
700 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
701 struct ng_bt_mbufq *queue_ptr;
702 struct mbuf *m;
703 struct ng_tag_prio *ptag;
704 int error;
705
706 if (sc == NULL) {
707 NG_FREE_ITEM(item);
708 return (EHOSTDOWN);
709 }
710 NGI_GET_M(item, m);
711 NG_FREE_ITEM(item);
712
713 /*
714 * Now queue the data for when it can be sent
715 */
716 ptag = (void *)m_tag_locate(m, NGM_GENERIC_COOKIE,
717 NG_TAG_PRIO, NULL);
718
719 if (ptag && (ptag->priority > NG_PRIO_CUTOFF))
720 queue_ptr = &sc->sc_xmitq_hipri;
721 else
722 queue_ptr = &sc->sc_xmitq;
723
724 mtx_lock(&sc->sc_mtx);
725
726 if (NG_BT_MBUFQ_FULL(queue_ptr)) {
727 NG_BT_MBUFQ_DROP(queue_ptr);
728 NG_FREE_M(m);
729 error = ENOBUFS;
730 } else {
731 NG_BT_MBUFQ_ENQUEUE(queue_ptr, m);
732 /*
733 * start bulk-out transfer, if not already started:
734 */
736 error = 0;
737 }
738
739 mtx_unlock(&sc->sc_mtx);
740
741 return (error);
742}
743
744/*
745 * Do local shutdown processing..
746 * We are a persistent device, we refuse to go away, and
747 * only remove our links and reset ourself.
748 */
749static int
750ng_udbp_rmnode(node_p node)
751{
752 struct udbp_softc *sc = NG_NODE_PRIVATE(node);
753
754 /* Let old node go */
755 NG_NODE_SET_PRIVATE(node, NULL);
756 NG_NODE_UNREF(node); /* forget it ever existed */
757
758 if (sc == NULL) {
759 goto done;
760 }
761 /* Create Netgraph node */
762 if (ng_make_node_common(&ng_udbp_typestruct, &sc->sc_node) != 0) {
763 printf("%s: Could not create Netgraph node\n",
764 sc->sc_name);
765 sc->sc_node = NULL;
766 goto done;
767 }
768 /* Name node */
769 if (ng_name_node(sc->sc_node, sc->sc_name) != 0) {
770 printf("%s: Could not name Netgraph node\n",
771 sc->sc_name);
772 NG_NODE_UNREF(sc->sc_node);
773 sc->sc_node = NULL;
774 goto done;
775 }
776 NG_NODE_SET_PRIVATE(sc->sc_node, sc);
777
778done:
779 if (sc) {
780 mtx_unlock(&sc->sc_mtx);
781 }
782 return (0);
783}
784
785/*
786 * This is called once we've already connected a new hook to the other node.
787 * It gives us a chance to balk at the last minute.
788 */
789static int
790ng_udbp_connect(hook_p hook)
791{
792 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
793
794 /* probably not at splnet, force outward queueing */
795 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
796
797 mtx_lock(&sc->sc_mtx);
798
801
802 /* start bulk-in transfer */
804
805 /* start bulk-out transfer */
807
808 mtx_unlock(&sc->sc_mtx);
809
810 return (0);
811}
812
813/*
814 * Dook disconnection
815 *
816 * For this type, removal of the last link destroys the node
817 */
818static int
819ng_udbp_disconnect(hook_p hook)
820{
821 struct udbp_softc *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
822 int error = 0;
823
824 if (sc != NULL) {
825 mtx_lock(&sc->sc_mtx);
826
827 if (hook != sc->sc_hook) {
828 error = EINVAL;
829 } else {
830 /* stop bulk-in transfer */
833
834 /* stop bulk-out transfer */
837
838 /* cleanup queues */
839 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq);
840 NG_BT_MBUFQ_DRAIN(&sc->sc_xmitq_hipri);
841
842 if (sc->sc_bulk_in_buffer) {
843 m_freem(sc->sc_bulk_in_buffer);
844 sc->sc_bulk_in_buffer = NULL;
845 }
846 sc->sc_hook = NULL;
847 }
848
849 mtx_unlock(&sc->sc_mtx);
850 }
851 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
852 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
853 ng_rmnode_self(NG_HOOK_NODE(hook));
854
855 return (error);
856}
static int debug
Definition: cfumass.c:73
static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "USB DWC OTG")
SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN, &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2/3 - ULPI/HSIC/INTERNAL/UTMI+")
struct @109 error
const char * name
device_t dev
uint32_t packets_out
Definition: udbp.h:63
uint32_t packets_in
Definition: udbp.h:62
struct mbuf * sc_bulk_in_buffer
Definition: udbp.c:123
struct ng_bt_mbufq sc_xmitq_hipri
Definition: udbp.c:117
hook_p sc_hook
Definition: udbp.c:122
uint8_t sc_flags
Definition: udbp.c:128
uint8_t sc_name[16]
Definition: udbp.c:132
struct usb_xfer * sc_xfer[UDBP_T_MAX]
Definition: udbp.c:120
uint32_t sc_packets_out
Definition: udbp.c:126
struct mtx sc_mtx
Definition: udbp.c:116
uint32_t sc_packets_in
Definition: udbp.c:125
node_p sc_node
Definition: udbp.c:121
struct ng_bt_mbufq sc_xmitq
Definition: udbp.c:118
enum usb_hc_mode usb_mode
Definition: usbdi.h:432
struct usbd_lookup_info info
Definition: usbdi.h:426
struct usb_device * device
Definition: usbdi.h:430
uint8_t type
Definition: usbdi.h:238
uint8_t bIfaceIndex
Definition: usbdi.h:417
uint8_t bConfigIndex
Definition: usbdi.h:419
static int udbp_modload(module_t mod, int event, void *data)
Definition: udbp.c:277
#define UDBP_TIMEOUT
Definition: udbp.c:106
static device_method_t udbp_methods[]
Definition: udbp.c:244
static device_probe_t udbp_probe
Definition: udbp.c:139
#define UDBP_T_MAX
Definition: udbp.c:112
static ng_disconnect_t ng_udbp_disconnect
Definition: udbp.c:156
#define UDBP_T_WR_CS
Definition: udbp.c:110
DRIVER_MODULE(udbp, uhub, udbp_driver, udbp_devclass, udbp_modload, 0)
MODULE_DEPEND(udbp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION)
MODULE_VERSION(udbp, 1)
static struct ng_type ng_udbp_typestruct
Definition: udbp.c:187
static device_detach_t udbp_detach
Definition: udbp.c:141
static driver_t udbp_driver
Definition: udbp.c:253
#define UDBP_T_WR
Definition: udbp.c:108
#define UDBP_Q_MAXLEN
Definition: udbp.c:113
static const STRUCT_USB_HOST_ID udbp_devs[]
Definition: udbp.c:259
static ng_rcvdata_t ng_udbp_rcvdata
Definition: udbp.c:155
static usb_callback_t udbp_bulk_read_callback
Definition: udbp.c:143
static device_attach_t udbp_attach
Definition: udbp.c:140
#define UDBP_FLAG_READ_STALL
Definition: udbp.c:129
#define UDBP_FLAG_WRITE_STALL
Definition: udbp.c:130
static const struct ng_parse_type ng_udbp_stat_type
Definition: udbp.c:162
static ng_constructor_t ng_udbp_constructor
Definition: udbp.c:150
static devclass_t udbp_devclass
Definition: udbp.c:242
USB_PNP_HOST_INFO(udbp_devs)
#define UDBP_T_RD
Definition: udbp.c:109
#define UDBP_T_RD_CS
Definition: udbp.c:111
__FBSDID("$FreeBSD$")
static ng_shutdown_t ng_udbp_rmnode
Definition: udbp.c:152
static const struct ng_cmdlist ng_udbp_cmdlist[]
Definition: udbp.c:168
static const struct usb_config udbp_config[UDBP_T_MAX]
Definition: udbp.c:201
static usb_callback_t udbp_bulk_read_clear_stall_callback
Definition: udbp.c:144
static ng_newhook_t ng_udbp_newhook
Definition: udbp.c:153
static void udbp_bulk_read_complete(node_p, hook_p, void *, int)
Definition: udbp.c:473
static ng_connect_t ng_udbp_connect
Definition: udbp.c:154
static usb_callback_t udbp_bulk_write_clear_stall_callback
Definition: udbp.c:146
static const struct ng_parse_struct_field ng_udbp_stat_type_fields[]
Definition: udbp.c:160
static usb_callback_t udbp_bulk_write_callback
Definition: udbp.c:145
static ng_rcvmsg_t ng_udbp_rcvmsg
Definition: udbp.c:151
#define UDBP_BUFFERSIZE
Definition: udbp.c:107
#define NGM_UDBP_COOKIE
Definition: udbp.h:50
#define NG_UDBP_STATS_TYPE_INFO
Definition: udbp.h:73
@ NGM_UDBP_SET_FLAG
Definition: udbp.h:56
@ NGM_UDBP_GET_STATUS
Definition: udbp.h:57
#define NG_UDBP_NODE_TYPE
Definition: udbp.h:44
#define NG_UDBP_HOOK_NAME
Definition: udbp.h:52
#define DPRINTF(...)
Definition: umass.c:179
#define UE_DIR_ANY
Definition: usb.h:535
#define UE_ADDR_ANY
Definition: usb.h:537
#define UE_BULK
Definition: usb.h:543
#define UE_DIR_IN
Definition: usb.h:531
#define UE_DIR_OUT
Definition: usb.h:532
@ USB_MODE_HOST
Definition: usb.h:778
#define UE_CONTROL
Definition: usb.h:541
void usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset, void *ptr, usb_frlength_t len)
Definition: usb_busdma.c:283
const char * usbd_errstr(usb_error_t err)
Definition: usb_error.c:93
INTERFACE usb
Definition: usb_if.m:35
int usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id, struct usb_attach_arg *uaa)
Definition: usb_lookup.c:143
void usbd_transfer_submit(struct usb_xfer *xfer)
void usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
void usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex, usb_frlength_t len)
uint8_t usbd_clear_stall_callback(struct usb_xfer *xfer1, struct usb_xfer *xfer2)
struct usb_page_cache * usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
usb_error_t usbd_transfer_setup(struct usb_device *udev, const uint8_t *ifaces, struct usb_xfer **ppxfer, const struct usb_config *setup_start, uint16_t n_setup, void *priv_sc, struct mtx *xfer_mtx)
Definition: usb_transfer.c:987
void usbd_transfer_start(struct usb_xfer *xfer)
void * usbd_xfer_softc(struct usb_xfer *xfer)
void usbd_transfer_stop(struct usb_xfer *xfer)
void usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes, int *nframes)
usb_frlength_t usbd_xfer_max_len(struct usb_xfer *xfer)
void device_set_usb_desc(device_t dev)
Definition: usb_util.c:73
#define USB_ST_SETUP
Definition: usbdi.h:502
usb_error_t
Definition: usbdi.h:45
@ USB_ERR_CANCELLED
Definition: usbdi.h:51
#define USB_ST_TRANSFERRED
Definition: usbdi.h:503
void usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset, struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len)
void() usb_callback_t(struct usb_xfer *, usb_error_t)
Definition: usbdi.h:94
#define USB_VPI(vend, prod, info)
Definition: usbdi.h:367
#define STRUCT_USB_HOST_ID
Definition: usbdi.h:258
#define USB_GET_STATE(xfer)
Definition: usbdi.h:515