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