]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/usb/usb_pf.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / usb / usb_pf.c
1 /* $FreeBSD$ */
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  * 4. 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 #ifdef USB_GLOBAL_INCLUDE_FILE
37 #include USB_GLOBAL_INCLUDE_FILE
38 #else
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <net/if.h>
48 #include <net/if_types.h>
49 #include <net/if_clone.h>
50 #include <net/bpf.h>
51 #include <sys/sysctl.h>
52 #include <net/route.h>
53
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_controller.h>
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_bus.h>
62 #include <dev/usb/usb_pf.h>
63 #include <dev/usb/usb_transfer.h>
64 #endif                  /* USB_GLOBAL_INCLUDE_FILE */
65
66 static void usbpf_init(void *);
67 static void usbpf_uninit(void *);
68 static int usbpf_ioctl(struct ifnet *, u_long, caddr_t);
69 static int usbpf_clone_match(struct if_clone *, const char *);
70 static int usbpf_clone_create(struct if_clone *, char *, size_t, caddr_t);
71 static int usbpf_clone_destroy(struct if_clone *, struct ifnet *);
72 static struct usb_bus *usbpf_ifname2ubus(const char *);
73 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *);
74 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *);
75 static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t);
76 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int);
77
78 static struct if_clone *usbpf_cloner;
79 static const char usbusname[] = "usbus";
80
81 SYSINIT(usbpf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_init, NULL);
82 SYSUNINIT(usbpf_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_uninit, NULL);
83
84 static void
85 usbpf_init(void *arg)
86 {
87
88         usbpf_cloner = if_clone_advanced(usbusname, 0, usbpf_clone_match,
89             usbpf_clone_create, usbpf_clone_destroy);
90 }
91
92 static void
93 usbpf_uninit(void *arg)
94 {
95         int devlcnt;
96         device_t *devlp;
97         devclass_t dc;
98         struct usb_bus *ubus;
99         int error;
100         int i;
101         
102         if_clone_detach(usbpf_cloner);
103
104         dc = devclass_find(usbusname);
105         if (dc == NULL)
106                 return;
107         error = devclass_get_devices(dc, &devlp, &devlcnt);
108         if (error)
109                 return;
110         for (i = 0; i < devlcnt; i++) {
111                 ubus = device_get_softc(devlp[i]);
112                 if (ubus != NULL && ubus->ifp != NULL)
113                         usbpf_clone_destroy(usbpf_cloner, ubus->ifp);
114         }
115         free(devlp, M_TEMP);
116 }
117
118 static int
119 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
120 {
121         
122         /* No configuration allowed. */
123         return (EINVAL);
124 }
125
126 static struct usb_bus *
127 usbpf_ifname2ubus(const char *ifname)
128 {
129         device_t dev;
130         devclass_t dc;
131         int unit;
132         int error;
133
134         if (strncmp(ifname, usbusname, sizeof(usbusname) - 1) != 0)
135                 return (NULL);
136         error = ifc_name2unit(ifname, &unit);
137         if (error || unit < 0)
138                 return (NULL);
139         dc = devclass_find(usbusname);
140         if (dc == NULL)
141                 return (NULL);
142         dev = devclass_get_device(dc, unit);
143         if (dev == NULL)
144                 return (NULL);
145
146         return (device_get_softc(dev));
147 }
148
149 static int
150 usbpf_clone_match(struct if_clone *ifc, const char *name)
151 {
152         struct usb_bus *ubus;
153
154         ubus = usbpf_ifname2ubus(name);
155         if (ubus == NULL)
156                 return (0);
157         if (ubus->ifp != NULL)
158                 return (0);
159
160         return (1);
161 }
162
163 static int
164 usbpf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
165 {
166         int error;
167         int unit;
168         struct ifnet *ifp;
169         struct usb_bus *ubus;
170
171         error = ifc_name2unit(name, &unit);
172         if (error)
173                 return (error);
174         if (unit < 0)
175                 return (EINVAL);
176
177         ubus = usbpf_ifname2ubus(name);
178         if (ubus == NULL)
179                 return (1);
180         if (ubus->ifp != NULL)
181                 return (1);
182
183         error = ifc_alloc_unit(ifc, &unit);
184         if (error) {
185                 device_printf(ubus->parent, "usbpf: Could not allocate "
186                     "instance\n");
187                 return (error);
188         }
189         ifp = ubus->ifp = if_alloc(IFT_USB);
190         if (ifp == NULL) {
191                 ifc_free_unit(ifc, unit);
192                 device_printf(ubus->parent, "usbpf: Could not allocate "
193                     "instance\n");
194                 return (ENOSPC);
195         }
196         strlcpy(ifp->if_xname, name, sizeof(ifp->if_xname));
197         ifp->if_softc = ubus;
198         ifp->if_dname = usbusname;
199         ifp->if_dunit = unit;
200         ifp->if_ioctl = usbpf_ioctl;
201         if_attach(ifp);
202         ifp->if_flags |= IFF_UP;
203         rt_ifmsg(ifp);
204         /*
205          * XXX According to the specification of DLT_USB, it indicates
206          * packets beginning with USB setup header. But not sure all
207          * packets would be.
208          */
209         bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
210
211         return (0);
212 }
213
214 static int
215 usbpf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
216 {
217         struct usb_bus *ubus;
218         int unit;
219
220         ubus = ifp->if_softc;
221         unit = ifp->if_dunit;
222
223         ubus->ifp = NULL;
224         bpfdetach(ifp);
225         if_detach(ifp);
226         if_free(ifp);
227         ifc_free_unit(ifc, unit);
228         
229         return (0);
230 }
231
232 void
233 usbpf_attach(struct usb_bus *ubus)
234 {
235
236         if (bootverbose)
237                 device_printf(ubus->parent, "usbpf: Attached\n");
238 }
239
240 void
241 usbpf_detach(struct usb_bus *ubus)
242 {
243
244         if (ubus->ifp != NULL)
245                 usbpf_clone_destroy(usbpf_cloner, ubus->ifp);
246         if (bootverbose)
247                 device_printf(ubus->parent, "usbpf: Detached\n");
248 }
249
250 static uint32_t
251 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
252 {
253         uint32_t val = 0;
254
255         if (flags->force_short_xfer == 1)
256                 val |= USBPF_FLAG_FORCE_SHORT_XFER;
257         if (flags->short_xfer_ok == 1)
258                 val |= USBPF_FLAG_SHORT_XFER_OK;
259         if (flags->short_frames_ok == 1)
260                 val |= USBPF_FLAG_SHORT_FRAMES_OK;
261         if (flags->pipe_bof == 1)
262                 val |= USBPF_FLAG_PIPE_BOF;
263         if (flags->proxy_buffer == 1)
264                 val |= USBPF_FLAG_PROXY_BUFFER;
265         if (flags->ext_buffer == 1)
266                 val |= USBPF_FLAG_EXT_BUFFER;
267         if (flags->manual_status == 1)
268                 val |= USBPF_FLAG_MANUAL_STATUS;
269         if (flags->no_pipe_ok == 1)
270                 val |= USBPF_FLAG_NO_PIPE_OK;
271         if (flags->stall_pipe == 1)
272                 val |= USBPF_FLAG_STALL_PIPE;
273         return (val);
274 }
275
276 static uint32_t
277 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
278 {
279         uint32_t val = 0;
280
281         if (flags->open == 1)
282                 val |= USBPF_STATUS_OPEN;
283         if (flags->transferring == 1)
284                 val |= USBPF_STATUS_TRANSFERRING;
285         if (flags->did_dma_delay == 1)
286                 val |= USBPF_STATUS_DID_DMA_DELAY;
287         if (flags->did_close == 1)
288                 val |= USBPF_STATUS_DID_CLOSE;
289         if (flags->draining == 1)
290                 val |= USBPF_STATUS_DRAINING;
291         if (flags->started == 1)
292                 val |= USBPF_STATUS_STARTED;
293         if (flags->bandwidth_reclaimed == 1)
294                 val |= USBPF_STATUS_BW_RECLAIMED;
295         if (flags->control_xfr == 1)
296                 val |= USBPF_STATUS_CONTROL_XFR;
297         if (flags->control_hdr == 1)
298                 val |= USBPF_STATUS_CONTROL_HDR;
299         if (flags->control_act == 1)
300                 val |= USBPF_STATUS_CONTROL_ACT;
301         if (flags->control_stall == 1)
302                 val |= USBPF_STATUS_CONTROL_STALL;
303         if (flags->short_frames_ok == 1)
304                 val |= USBPF_STATUS_SHORT_FRAMES_OK;
305         if (flags->short_xfer_ok == 1)
306                 val |= USBPF_STATUS_SHORT_XFER_OK;
307 #if USB_HAVE_BUSDMA
308         if (flags->bdma_enable == 1)
309                 val |= USBPF_STATUS_BDMA_ENABLE;
310         if (flags->bdma_no_post_sync == 1)
311                 val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
312         if (flags->bdma_setup == 1)
313                 val |= USBPF_STATUS_BDMA_SETUP;
314 #endif
315         if (flags->isochronous_xfr == 1)
316                 val |= USBPF_STATUS_ISOCHRONOUS_XFR;
317         if (flags->curr_dma_set == 1)
318                 val |= USBPF_STATUS_CURR_DMA_SET;
319         if (flags->can_cancel_immed == 1)
320                 val |= USBPF_STATUS_CAN_CANCEL_IMMED;
321         if (flags->doing_callback == 1)
322                 val |= USBPF_STATUS_DOING_CALLBACK;
323
324         return (val);
325 }
326
327 static int
328 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
329 {
330         int isread;
331
332         if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
333             (xfer->flags_int.control_hdr != 0)) {
334                 /* special case */
335                 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
336                         /* The device controller writes to memory */
337                         isread = 1;
338                 } else {
339                         /* The host controller reads from memory */
340                         isread = 0;
341                 }
342         } else {
343                 isread = USB_GET_DATA_ISREAD(xfer);
344         }
345         return (isread);
346 }
347
348 static uint32_t
349 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
350 {
351         uint32_t totlen;
352         uint32_t x;
353         uint32_t nframes;
354
355         if (type == USBPF_XFERTAP_SUBMIT)
356                 nframes = xfer->nframes;
357         else
358                 nframes = xfer->aframes;
359
360         totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
361
362         /* precompute all trace lengths */
363         for (x = 0; x != nframes; x++) {
364                 if (usbpf_xfer_frame_is_read(xfer, x)) {
365                         if (type != USBPF_XFERTAP_SUBMIT) {
366                                 totlen += USBPF_FRAME_ALIGN(
367                                     xfer->frlengths[x]);
368                         }
369                 } else {
370                         if (type == USBPF_XFERTAP_SUBMIT) {
371                                 totlen += USBPF_FRAME_ALIGN(
372                                     xfer->frlengths[x]);
373                         }
374                 }
375         }
376         return (totlen);
377 }
378
379 void
380 usbpf_xfertap(struct usb_xfer *xfer, int type)
381 {
382         struct usb_bus *bus;
383         struct usbpf_pkthdr *up;
384         struct usbpf_framehdr *uf;
385         usb_frlength_t offset;
386         uint32_t totlen;
387         uint32_t frame;
388         uint32_t temp;
389         uint32_t nframes;
390         uint32_t x;
391         uint8_t *buf;
392         uint8_t *ptr;
393
394         bus = xfer->xroot->bus;
395
396         /* sanity checks */
397         if (bus->ifp == NULL)
398                 return;
399         if (!bpf_peers_present(bus->ifp->if_bpf))
400                 return;
401
402         totlen = usbpf_xfer_precompute_size(xfer, type);
403
404         if (type == USBPF_XFERTAP_SUBMIT)
405                 nframes = xfer->nframes;
406         else
407                 nframes = xfer->aframes;
408
409         /*
410          * XXX TODO XXX
411          *
412          * When BPF supports it we could pass a fragmented array of
413          * buffers avoiding the data copy operation here.
414          */
415         buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT);
416         if (buf == NULL) {
417                 device_printf(bus->parent, "usbpf: Out of memory\n");
418                 return;
419         }
420
421         up = (struct usbpf_pkthdr *)ptr;
422         ptr += USBPF_HDR_LEN;
423
424         /* fill out header */
425         temp = device_get_unit(bus->bdev);
426         up->up_totlen = htole32(totlen);
427         up->up_busunit = htole32(temp);
428         up->up_address = xfer->xroot->udev->device_index;
429         if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
430                 up->up_mode = USBPF_MODE_DEVICE;
431         else
432                 up->up_mode = USBPF_MODE_HOST;
433         up->up_type = type;
434         up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
435         temp = usbpf_aggregate_xferflags(&xfer->flags);
436         up->up_flags = htole32(temp);
437         temp = usbpf_aggregate_status(&xfer->flags_int);
438         up->up_status = htole32(temp);
439         temp = xfer->error;
440         up->up_error = htole32(temp);
441         temp = xfer->interval;
442         up->up_interval = htole32(temp);
443         up->up_frames = htole32(nframes);
444         temp = xfer->max_packet_size;
445         up->up_packet_size = htole32(temp);
446         temp = xfer->max_packet_count;
447         up->up_packet_count = htole32(temp);
448         temp = xfer->endpointno;
449         up->up_endpoint = htole32(temp);
450         up->up_speed = xfer->xroot->udev->speed;
451
452         /* clear reserved area */
453         memset(up->up_reserved, 0, sizeof(up->up_reserved));
454
455         /* init offset and frame */
456         offset = 0;
457         frame = 0;
458
459         /* iterate all the USB frames and copy data, if any */
460         for (x = 0; x != nframes; x++) {
461                 uint32_t length;
462                 int isread;
463
464                 /* get length */
465                 length = xfer->frlengths[x];
466
467                 /* get frame header pointer */
468                 uf = (struct usbpf_framehdr *)ptr;
469                 ptr += USBPF_FRAME_HDR_LEN;
470
471                 /* fill out packet header */
472                 uf->length = htole32(length);
473                 uf->flags = 0;
474
475                 /* get information about data read/write */
476                 isread = usbpf_xfer_frame_is_read(xfer, x);
477
478                 /* check if we need to copy any data */
479                 if (isread) {
480                         if (type == USBPF_XFERTAP_SUBMIT)
481                                 length = 0;
482                         else {
483                                 uf->flags |= htole32(
484                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
485                         }
486                 } else {
487                         if (type != USBPF_XFERTAP_SUBMIT)
488                                 length = 0;
489                         else {
490                                 uf->flags |= htole32(
491                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
492                         }
493                 }
494
495                 /* check if data is read direction */
496                 if (isread)
497                         uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
498
499                 /* copy USB data, if any */
500                 if (length != 0) {
501                         /* copy data */
502                         usbd_copy_out(&xfer->frbuffers[frame],
503                             offset, ptr, length);
504
505                         /* align length */
506                         temp = USBPF_FRAME_ALIGN(length);
507
508                         /* zero pad */
509                         if (temp != length)
510                                 memset(ptr + length, 0, temp - length);
511
512                         ptr += temp;
513                 }
514
515                 if (xfer->flags_int.isochronous_xfr) {
516                         offset += usbd_xfer_old_frame_length(xfer, x);
517                 } else {
518                         frame ++;
519                 }
520         }
521
522         bpf_tap(bus->ifp->if_bpf, buf, totlen);
523
524         free(buf, M_TEMP);
525 }