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