0fd4658bf03ed50cfe75bf7882efd68f40b52bce
[dragonfly.git] / sys / bus / u4b / usb_pf.c
1 /* $FreeBSD: head/sys/dev/usb/usb_pf.c 265779 2014-05-09 14:28:11Z hselasky $ */
2 /*-
3  * Copyright (c) 1990, 1991, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from the Stanford/CMU enet packet filter,
7  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
8  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
9  * Berkeley Laboratory.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <net/if.h>
45 #include <net/if_clone.h>
46 #include <net/if_types.h>
47 #include <net/ifq_var.h>
48 #include <net/bpf.h>
49 #include <net/route.h>
50 #include <sys/sysctl.h>
51 #include <sys/condvar.h>
52
53 #include <bus/u4b/usb.h>
54 #include <bus/u4b/usbdi.h>
55 #include <bus/u4b/usb_busdma.h>
56
57 #include <bus/u4b/usb_controller.h>
58 #include <bus/u4b/usb_core.h>
59 #include <bus/u4b/usb_process.h>
60 #include <bus/u4b/usb_device.h>
61 #include <bus/u4b/usb_bus.h>
62 #include <bus/u4b/usb_pf.h>
63 #include <bus/u4b/usb_transfer.h>
64
65 static void usbpf_init(void *);
66 static void usbpf_uninit(void *);
67 static int usbpf_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
68 static int usbpf_clone_create(struct if_clone *, int, caddr_t);
69 static int usbpf_clone_destroy(struct ifnet *);
70 static struct usb_bus *usbpf_ifname2ubus(int unit);
71 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *);
72 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *);
73 static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t);
74 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int);
75
76
77 SYSINIT(usbpf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_init, NULL);
78 SYSUNINIT(usbpf_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_uninit, NULL);
79
80 static const char * usbusname = "usbus";
81 struct if_clone usbpf_cloner = IF_CLONE_INITIALIZER("usbus",
82                                  usbpf_clone_create,
83                                  usbpf_clone_destroy,
84                                  0, IF_MAXUNIT);
85
86 static void
87 usbpf_init(void *arg)
88 {
89         if_clone_attach(&usbpf_cloner);
90         if (bootverbose)
91                 kprintf("usbpf: Initialized\n");
92         return;
93 }
94
95 static void
96 usbpf_uninit(void *arg)
97 {
98         int devlcnt;
99         device_t *devlp;
100         devclass_t dc;
101         struct usb_bus *ubus;
102         int error;
103         int i;
104
105         if_clone_detach(&usbpf_cloner);
106
107         dc = devclass_find(usbusname);
108         if (dc == NULL)
109                 return;
110         error = devclass_get_devices(dc, &devlp, &devlcnt);
111         if (error)
112                 return;
113         for (i = 0; i < devlcnt; i++) {
114                 ubus = device_get_softc(devlp[i]);
115                 if (ubus != NULL && ubus->ifp != NULL)
116                         usbpf_clone_destroy(ubus->ifp);
117         }
118         kfree(devlp, M_TEMP);
119 }
120
121 static int
122 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
123 {
124         /* No configuration allowed. */
125         return (EINVAL);
126 }
127
128 static struct usb_bus *
129 usbpf_ifname2ubus(int unit)
130 {
131         device_t dev;
132         devclass_t dc;
133
134         dc = devclass_find(usbusname);
135         if (dc == NULL)
136                 return (NULL);
137         dev = devclass_get_device(dc, unit);
138         if (dev == NULL)
139                 return (NULL);
140
141         return (device_get_softc(dev));
142 }
143
144 static int
145 usbpf_clone_create(struct if_clone *ifc, int unit, caddr_t params)
146 {
147         struct ifnet *ifp;
148         struct usb_bus *ubus;
149
150         ubus = usbpf_ifname2ubus(unit);
151         if (ubus == NULL)
152                 return (EINVAL);
153         if (ubus->ifp != NULL)
154                 return (EINVAL);
155         ifp = ubus->ifp = if_alloc(IFT_USB);
156         if (ifp == NULL) {
157                 device_printf(ubus->parent, "usbpf: Could not allocate "
158                     "instance\n");
159                 return (ENOSPC);
160         }
161         ksnprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d", usbusname, unit);
162         ifp->if_softc = ubus;
163         ifp->if_dname = usbusname;
164         ifp->if_dunit = unit;
165         ifp->if_ioctl = usbpf_ioctl;
166         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
167         if_attach(ifp, NULL);
168         ifp->if_flags |= IFF_UP;
169         rt_ifmsg(ifp);
170         /*
171          * XXX According to the specification of DLT_USB, it indicates
172          * packets beginning with USB setup header. But not sure all
173          * packets would be.
174          */
175         bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
176
177         return (0);
178 }
179
180 static int
181 usbpf_clone_destroy(struct ifnet *ifp) 
182 {
183         struct usb_bus *ubus;
184         int unit;
185
186         ubus = ifp->if_softc;
187         unit = ifp->if_dunit;
188
189         ubus->ifp = NULL;
190         bpfdetach(ifp);
191         if_detach(ifp);
192         if_free(ifp);
193
194         return (0);
195 }
196
197 void
198 usbpf_attach(struct usb_bus *ubus)
199 {
200         if (bootverbose)
201                 device_printf(ubus->parent, "usbpf: Attached\n");
202 }
203
204 void
205 usbpf_detach(struct usb_bus *ubus)
206 {
207         if (ubus->ifp != NULL)
208                 usbpf_clone_destroy(ubus->ifp);
209         if (bootverbose)
210                 device_printf(ubus->parent, "usbpf: Detached\n");
211 }
212
213 static uint32_t
214 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
215 {
216         uint32_t val = 0;
217
218         if (flags->force_short_xfer == 1)
219                 val |= USBPF_FLAG_FORCE_SHORT_XFER;
220         if (flags->short_xfer_ok == 1)
221                 val |= USBPF_FLAG_SHORT_XFER_OK;
222         if (flags->short_frames_ok == 1)
223                 val |= USBPF_FLAG_SHORT_FRAMES_OK;
224         if (flags->pipe_bof == 1)
225                 val |= USBPF_FLAG_PIPE_BOF;
226         if (flags->proxy_buffer == 1)
227                 val |= USBPF_FLAG_PROXY_BUFFER;
228         if (flags->ext_buffer == 1)
229                 val |= USBPF_FLAG_EXT_BUFFER;
230         if (flags->manual_status == 1)
231                 val |= USBPF_FLAG_MANUAL_STATUS;
232         if (flags->no_pipe_ok == 1)
233                 val |= USBPF_FLAG_NO_PIPE_OK;
234         if (flags->stall_pipe == 1)
235                 val |= USBPF_FLAG_STALL_PIPE;
236         return (val);
237 }
238
239 static uint32_t
240 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
241 {
242         uint32_t val = 0;
243
244         if (flags->open == 1)
245                 val |= USBPF_STATUS_OPEN;
246         if (flags->transferring == 1)
247                 val |= USBPF_STATUS_TRANSFERRING;
248         if (flags->did_dma_delay == 1)
249                 val |= USBPF_STATUS_DID_DMA_DELAY;
250         if (flags->did_close == 1)
251                 val |= USBPF_STATUS_DID_CLOSE;
252         if (flags->draining == 1)
253                 val |= USBPF_STATUS_DRAINING;
254         if (flags->started == 1)
255                 val |= USBPF_STATUS_STARTED;
256         if (flags->bandwidth_reclaimed == 1)
257                 val |= USBPF_STATUS_BW_RECLAIMED;
258         if (flags->control_xfr == 1)
259                 val |= USBPF_STATUS_CONTROL_XFR;
260         if (flags->control_hdr == 1)
261                 val |= USBPF_STATUS_CONTROL_HDR;
262         if (flags->control_act == 1)
263                 val |= USBPF_STATUS_CONTROL_ACT;
264         if (flags->control_stall == 1)
265                 val |= USBPF_STATUS_CONTROL_STALL;
266         if (flags->short_frames_ok == 1)
267                 val |= USBPF_STATUS_SHORT_FRAMES_OK;
268         if (flags->short_xfer_ok == 1)
269                 val |= USBPF_STATUS_SHORT_XFER_OK;
270 #if USB_HAVE_BUSDMA
271         if (flags->bdma_enable == 1)
272                 val |= USBPF_STATUS_BDMA_ENABLE;
273         if (flags->bdma_no_post_sync == 1)
274                 val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
275         if (flags->bdma_setup == 1)
276                 val |= USBPF_STATUS_BDMA_SETUP;
277 #endif
278         if (flags->isochronous_xfr == 1)
279                 val |= USBPF_STATUS_ISOCHRONOUS_XFR;
280         if (flags->curr_dma_set == 1)
281                 val |= USBPF_STATUS_CURR_DMA_SET;
282         if (flags->can_cancel_immed == 1)
283                 val |= USBPF_STATUS_CAN_CANCEL_IMMED;
284         if (flags->doing_callback == 1)
285                 val |= USBPF_STATUS_DOING_CALLBACK;
286
287         return (val);
288 }
289
290 static int
291 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
292 {
293         int isread;
294
295         if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
296             (xfer->flags_int.control_hdr != 0)) {
297                 /* special case */
298                 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
299                         /* The device controller writes to memory */
300                         isread = 1;
301                 } else {
302                         /* The host controller reads from memory */
303                         isread = 0;
304                 }
305         } else {
306                 isread = USB_GET_DATA_ISREAD(xfer);
307         }
308         return (isread);
309 }
310
311 static uint32_t
312 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
313 {
314         uint32_t totlen;
315         uint32_t x;
316         uint32_t nframes;
317
318         if (type == USBPF_XFERTAP_SUBMIT)
319                 nframes = xfer->nframes;
320         else
321                 nframes = xfer->aframes;
322
323         totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
324
325         /* precompute all trace lengths */
326         for (x = 0; x != nframes; x++) {
327                 if (usbpf_xfer_frame_is_read(xfer, x)) {
328                         if (type != USBPF_XFERTAP_SUBMIT) {
329                                 totlen += USBPF_FRAME_ALIGN(
330                                     xfer->frlengths[x]);
331                         }
332                 } else {
333                         if (type == USBPF_XFERTAP_SUBMIT) {
334                                 totlen += USBPF_FRAME_ALIGN(
335                                     xfer->frlengths[x]);
336                         }
337                 }
338         }
339         return (totlen);
340 }
341
342 void
343 usbpf_xfertap(struct usb_xfer *xfer, int type)
344 {
345         struct usb_bus *bus;
346         struct usbpf_pkthdr *up;
347         struct usbpf_framehdr *uf;
348         usb_frlength_t offset;
349         uint32_t totlen;
350         uint32_t frame;
351         uint32_t temp;
352         uint32_t nframes;
353         uint32_t x;
354         uint8_t *buf;
355         uint8_t *ptr;
356
357         bus = xfer->xroot->bus;
358
359         /* sanity checks */
360         if (bus->ifp == NULL)
361                 return;
362 #if 0 /* XXX this is not needed on dragonfly */
363         if (!bpf_peers_present(bus->ifp->if_bpf))
364                 return;
365 #endif
366         totlen = usbpf_xfer_precompute_size(xfer, type);
367
368         if (type == USBPF_XFERTAP_SUBMIT)
369                 nframes = xfer->nframes;
370         else
371                 nframes = xfer->aframes;
372
373         /*
374          * XXX TODO XXX
375          *
376          * When BPF supports it we could pass a fragmented array of
377          * buffers avoiding the data copy operation here.
378          */
379         buf = ptr = kmalloc(totlen, M_TEMP, M_NOWAIT);
380         if (buf == NULL) {
381                 device_printf(bus->parent, "usbpf: Out of memory\n");
382                 return;
383         }
384
385         up = (struct usbpf_pkthdr *)ptr;
386         ptr += USBPF_HDR_LEN;
387
388         /* fill out header */
389         temp = device_get_unit(bus->bdev);
390         up->up_totlen = htole32(totlen);
391         up->up_busunit = htole32(temp);
392         up->up_address = xfer->xroot->udev->device_index;
393         if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
394                 up->up_mode = USBPF_MODE_DEVICE;
395         else
396                 up->up_mode = USBPF_MODE_HOST;
397         up->up_type = type;
398         up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
399         temp = usbpf_aggregate_xferflags(&xfer->flags);
400         up->up_flags = htole32(temp);
401         temp = usbpf_aggregate_status(&xfer->flags_int);
402         up->up_status = htole32(temp);
403         temp = xfer->error;
404         up->up_error = htole32(temp);
405         temp = xfer->interval;
406         up->up_interval = htole32(temp);
407         up->up_frames = htole32(nframes);
408         temp = xfer->max_packet_size;
409         up->up_packet_size = htole32(temp);
410         temp = xfer->max_packet_count;
411         up->up_packet_count = htole32(temp);
412         temp = xfer->endpointno;
413         up->up_endpoint = htole32(temp);
414         up->up_speed = xfer->xroot->udev->speed;
415
416         /* clear reserved area */
417         memset(up->up_reserved, 0, sizeof(up->up_reserved));
418
419         /* init offset and frame */
420         offset = 0;
421         frame = 0;
422
423         /* iterate all the USB frames and copy data, if any */
424         for (x = 0; x != nframes; x++) {
425                 uint32_t length;
426                 int isread;
427
428                 /* get length */
429                 length = xfer->frlengths[x];
430
431                 /* get frame header pointer */
432                 uf = (struct usbpf_framehdr *)ptr;
433                 ptr += USBPF_FRAME_HDR_LEN;
434
435                 /* fill out packet header */
436                 uf->length = htole32(length);
437                 uf->flags = 0;
438
439                 /* get information about data read/write */
440                 isread = usbpf_xfer_frame_is_read(xfer, x);
441
442                 /* check if we need to copy any data */
443                 if (isread) {
444                         if (type == USBPF_XFERTAP_SUBMIT)
445                                 length = 0;
446                         else {
447                                 uf->flags |= htole32(
448                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
449                         }
450                 } else {
451                         if (type != USBPF_XFERTAP_SUBMIT)
452                                 length = 0;
453                         else {
454                                 uf->flags |= htole32(
455                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
456                         }
457                 }
458
459                 /* check if data is read direction */
460                 if (isread)
461                         uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
462
463                 /* copy USB data, if any */
464                 if (length != 0) {
465                         /* copy data */
466                         usbd_copy_out(&xfer->frbuffers[frame],
467                             offset, ptr, length);
468
469                         /* align length */
470                         temp = USBPF_FRAME_ALIGN(length);
471
472                         /* zero pad */
473                         if (temp != length)
474                                 memset(ptr + length, 0, temp - length);
475
476                         ptr += temp;
477                 }
478
479                 if (xfer->flags_int.isochronous_xfr) {
480                         offset += usbd_xfer_old_frame_length(xfer, x);
481                 } else {
482                         frame ++;
483                 }
484         }
485
486         bpf_tap(bus->ifp->if_bpf, buf, totlen);
487
488         kfree(buf, M_TEMP);
489 }