1 /* $FreeBSD: head/sys/dev/usb/usb_pf.c 265779 2014-05-09 14:28:11Z hselasky $ */
3 * Copyright (c) 1990, 1991, 1993
4 * The Regents of the University of California. All rights reserved.
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <net/if_var.h>
46 #include <net/if_clone.h>
47 #include <net/if_types.h>
48 #include <net/ifq_var.h>
50 #include <net/route.h>
51 #include <sys/sysctl.h>
52 #include <sys/condvar.h>
54 #include <bus/u4b/usb.h>
55 #include <bus/u4b/usbdi.h>
56 #include <bus/u4b/usb_busdma.h>
58 #include <bus/u4b/usb_controller.h>
59 #include <bus/u4b/usb_core.h>
60 #include <bus/u4b/usb_process.h>
61 #include <bus/u4b/usb_device.h>
62 #include <bus/u4b/usb_bus.h>
63 #include <bus/u4b/usb_pf.h>
64 #include <bus/u4b/usb_transfer.h>
66 static void usbpf_init(void *);
67 static void usbpf_uninit(void *);
68 static int usbpf_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
69 static int usbpf_clone_create(struct if_clone
*, int, caddr_t
);
70 static int usbpf_clone_destroy(struct ifnet
*);
71 static struct usb_bus
*usbpf_ifname2ubus(int unit
);
72 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags
*);
73 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int
*);
74 static int usbpf_xfer_frame_is_read(struct usb_xfer
*, uint32_t);
75 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer
*, int);
78 SYSINIT(usbpf_init
, SI_SUB_PSEUDO
, SI_ORDER_MIDDLE
, usbpf_init
, NULL
);
79 SYSUNINIT(usbpf_uninit
, SI_SUB_PSEUDO
, SI_ORDER_MIDDLE
, usbpf_uninit
, NULL
);
81 static const char * usbusname
= "usbus";
82 struct if_clone usbpf_cloner
= IF_CLONE_INITIALIZER("usbus",
90 if_clone_attach(&usbpf_cloner
);
92 kprintf("usbpf: Initialized\n");
97 usbpf_uninit(void *arg
)
102 struct usb_bus
*ubus
;
106 if_clone_detach(&usbpf_cloner
);
108 dc
= devclass_find(usbusname
);
111 error
= devclass_get_devices(dc
, &devlp
, &devlcnt
);
114 for (i
= 0; i
< devlcnt
; i
++) {
115 ubus
= device_get_softc(devlp
[i
]);
116 if (ubus
!= NULL
&& ubus
->ifp
!= NULL
)
117 usbpf_clone_destroy(ubus
->ifp
);
119 kfree(devlp
, M_TEMP
);
123 usbpf_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
125 /* No configuration allowed. */
129 static struct usb_bus
*
130 usbpf_ifname2ubus(int unit
)
135 dc
= devclass_find(usbusname
);
138 dev
= devclass_get_device(dc
, unit
);
142 return (device_get_softc(dev
));
146 usbpf_clone_create(struct if_clone
*ifc
, int unit
, caddr_t params
)
149 struct usb_bus
*ubus
;
151 ubus
= usbpf_ifname2ubus(unit
);
154 if (ubus
->ifp
!= NULL
)
156 ifp
= ubus
->ifp
= if_alloc(IFT_USB
);
158 device_printf(ubus
->parent
, "usbpf: Could not allocate "
162 ksnprintf(ifp
->if_xname
, sizeof(ifp
->if_xname
), "%s%d", usbusname
, unit
);
163 ifp
->if_softc
= ubus
;
164 ifp
->if_dname
= usbusname
;
165 ifp
->if_dunit
= unit
;
166 ifp
->if_ioctl
= usbpf_ioctl
;
167 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
168 if_attach(ifp
, NULL
);
169 ifp
->if_flags
|= IFF_UP
;
172 * XXX According to the specification of DLT_USB, it indicates
173 * packets beginning with USB setup header. But not sure all
176 bpfattach(ifp
, DLT_USB
, USBPF_HDR_LEN
);
182 usbpf_clone_destroy(struct ifnet
*ifp
)
184 struct usb_bus
*ubus
;
187 ubus
= ifp
->if_softc
;
188 unit
= ifp
->if_dunit
;
199 usbpf_attach(struct usb_bus
*ubus
)
202 device_printf(ubus
->parent
, "usbpf: Attached\n");
206 usbpf_detach(struct usb_bus
*ubus
)
208 if (ubus
->ifp
!= NULL
)
209 usbpf_clone_destroy(ubus
->ifp
);
211 device_printf(ubus
->parent
, "usbpf: Detached\n");
215 usbpf_aggregate_xferflags(struct usb_xfer_flags
*flags
)
219 if (flags
->force_short_xfer
== 1)
220 val
|= USBPF_FLAG_FORCE_SHORT_XFER
;
221 if (flags
->short_xfer_ok
== 1)
222 val
|= USBPF_FLAG_SHORT_XFER_OK
;
223 if (flags
->short_frames_ok
== 1)
224 val
|= USBPF_FLAG_SHORT_FRAMES_OK
;
225 if (flags
->pipe_bof
== 1)
226 val
|= USBPF_FLAG_PIPE_BOF
;
227 if (flags
->proxy_buffer
== 1)
228 val
|= USBPF_FLAG_PROXY_BUFFER
;
229 if (flags
->ext_buffer
== 1)
230 val
|= USBPF_FLAG_EXT_BUFFER
;
231 if (flags
->manual_status
== 1)
232 val
|= USBPF_FLAG_MANUAL_STATUS
;
233 if (flags
->no_pipe_ok
== 1)
234 val
|= USBPF_FLAG_NO_PIPE_OK
;
235 if (flags
->stall_pipe
== 1)
236 val
|= USBPF_FLAG_STALL_PIPE
;
241 usbpf_aggregate_status(struct usb_xfer_flags_int
*flags
)
245 if (flags
->open
== 1)
246 val
|= USBPF_STATUS_OPEN
;
247 if (flags
->transferring
== 1)
248 val
|= USBPF_STATUS_TRANSFERRING
;
249 if (flags
->did_dma_delay
== 1)
250 val
|= USBPF_STATUS_DID_DMA_DELAY
;
251 if (flags
->did_close
== 1)
252 val
|= USBPF_STATUS_DID_CLOSE
;
253 if (flags
->draining
== 1)
254 val
|= USBPF_STATUS_DRAINING
;
255 if (flags
->started
== 1)
256 val
|= USBPF_STATUS_STARTED
;
257 if (flags
->bandwidth_reclaimed
== 1)
258 val
|= USBPF_STATUS_BW_RECLAIMED
;
259 if (flags
->control_xfr
== 1)
260 val
|= USBPF_STATUS_CONTROL_XFR
;
261 if (flags
->control_hdr
== 1)
262 val
|= USBPF_STATUS_CONTROL_HDR
;
263 if (flags
->control_act
== 1)
264 val
|= USBPF_STATUS_CONTROL_ACT
;
265 if (flags
->control_stall
== 1)
266 val
|= USBPF_STATUS_CONTROL_STALL
;
267 if (flags
->short_frames_ok
== 1)
268 val
|= USBPF_STATUS_SHORT_FRAMES_OK
;
269 if (flags
->short_xfer_ok
== 1)
270 val
|= USBPF_STATUS_SHORT_XFER_OK
;
272 if (flags
->bdma_enable
== 1)
273 val
|= USBPF_STATUS_BDMA_ENABLE
;
274 if (flags
->bdma_no_post_sync
== 1)
275 val
|= USBPF_STATUS_BDMA_NO_POST_SYNC
;
276 if (flags
->bdma_setup
== 1)
277 val
|= USBPF_STATUS_BDMA_SETUP
;
279 if (flags
->isochronous_xfr
== 1)
280 val
|= USBPF_STATUS_ISOCHRONOUS_XFR
;
281 if (flags
->curr_dma_set
== 1)
282 val
|= USBPF_STATUS_CURR_DMA_SET
;
283 if (flags
->can_cancel_immed
== 1)
284 val
|= USBPF_STATUS_CAN_CANCEL_IMMED
;
285 if (flags
->doing_callback
== 1)
286 val
|= USBPF_STATUS_DOING_CALLBACK
;
292 usbpf_xfer_frame_is_read(struct usb_xfer
*xfer
, uint32_t frame
)
296 if ((frame
== 0) && (xfer
->flags_int
.control_xfr
!= 0) &&
297 (xfer
->flags_int
.control_hdr
!= 0)) {
299 if (xfer
->flags_int
.usb_mode
== USB_MODE_DEVICE
) {
300 /* The device controller writes to memory */
303 /* The host controller reads from memory */
307 isread
= USB_GET_DATA_ISREAD(xfer
);
313 usbpf_xfer_precompute_size(struct usb_xfer
*xfer
, int type
)
319 if (type
== USBPF_XFERTAP_SUBMIT
)
320 nframes
= xfer
->nframes
;
322 nframes
= xfer
->aframes
;
324 totlen
= USBPF_HDR_LEN
+ (USBPF_FRAME_HDR_LEN
* nframes
);
326 /* precompute all trace lengths */
327 for (x
= 0; x
!= nframes
; x
++) {
328 if (usbpf_xfer_frame_is_read(xfer
, x
)) {
329 if (type
!= USBPF_XFERTAP_SUBMIT
) {
330 totlen
+= USBPF_FRAME_ALIGN(
334 if (type
== USBPF_XFERTAP_SUBMIT
) {
335 totlen
+= USBPF_FRAME_ALIGN(
344 usbpf_xfertap(struct usb_xfer
*xfer
, int type
)
347 struct usbpf_pkthdr
*up
;
348 struct usbpf_framehdr
*uf
;
349 usb_frlength_t offset
;
358 bus
= xfer
->xroot
->bus
;
361 if (bus
->ifp
== NULL
)
363 #if 0 /* XXX this is not needed on dragonfly */
364 if (!bpf_peers_present(bus
->ifp
->if_bpf
))
367 totlen
= usbpf_xfer_precompute_size(xfer
, type
);
369 if (type
== USBPF_XFERTAP_SUBMIT
)
370 nframes
= xfer
->nframes
;
372 nframes
= xfer
->aframes
;
377 * When BPF supports it we could pass a fragmented array of
378 * buffers avoiding the data copy operation here.
380 buf
= ptr
= kmalloc(totlen
, M_TEMP
, M_NOWAIT
);
382 device_printf(bus
->parent
, "usbpf: Out of memory\n");
386 up
= (struct usbpf_pkthdr
*)ptr
;
387 ptr
+= USBPF_HDR_LEN
;
389 /* fill out header */
390 temp
= device_get_unit(bus
->bdev
);
391 up
->up_totlen
= htole32(totlen
);
392 up
->up_busunit
= htole32(temp
);
393 up
->up_address
= xfer
->xroot
->udev
->device_index
;
394 if (xfer
->flags_int
.usb_mode
== USB_MODE_DEVICE
)
395 up
->up_mode
= USBPF_MODE_DEVICE
;
397 up
->up_mode
= USBPF_MODE_HOST
;
399 up
->up_xfertype
= xfer
->endpoint
->edesc
->bmAttributes
& UE_XFERTYPE
;
400 temp
= usbpf_aggregate_xferflags(&xfer
->flags
);
401 up
->up_flags
= htole32(temp
);
402 temp
= usbpf_aggregate_status(&xfer
->flags_int
);
403 up
->up_status
= htole32(temp
);
405 up
->up_error
= htole32(temp
);
406 temp
= xfer
->interval
;
407 up
->up_interval
= htole32(temp
);
408 up
->up_frames
= htole32(nframes
);
409 temp
= xfer
->max_packet_size
;
410 up
->up_packet_size
= htole32(temp
);
411 temp
= xfer
->max_packet_count
;
412 up
->up_packet_count
= htole32(temp
);
413 temp
= xfer
->endpointno
;
414 up
->up_endpoint
= htole32(temp
);
415 up
->up_speed
= xfer
->xroot
->udev
->speed
;
417 /* clear reserved area */
418 memset(up
->up_reserved
, 0, sizeof(up
->up_reserved
));
420 /* init offset and frame */
424 /* iterate all the USB frames and copy data, if any */
425 for (x
= 0; x
!= nframes
; x
++) {
430 length
= xfer
->frlengths
[x
];
432 /* get frame header pointer */
433 uf
= (struct usbpf_framehdr
*)ptr
;
434 ptr
+= USBPF_FRAME_HDR_LEN
;
436 /* fill out packet header */
437 uf
->length
= htole32(length
);
440 /* get information about data read/write */
441 isread
= usbpf_xfer_frame_is_read(xfer
, x
);
443 /* check if we need to copy any data */
445 if (type
== USBPF_XFERTAP_SUBMIT
)
448 uf
->flags
|= htole32(
449 USBPF_FRAMEFLAG_DATA_FOLLOWS
);
452 if (type
!= USBPF_XFERTAP_SUBMIT
)
455 uf
->flags
|= htole32(
456 USBPF_FRAMEFLAG_DATA_FOLLOWS
);
460 /* check if data is read direction */
462 uf
->flags
|= htole32(USBPF_FRAMEFLAG_READ
);
464 /* copy USB data, if any */
467 usbd_copy_out(&xfer
->frbuffers
[frame
],
468 offset
, ptr
, length
);
471 temp
= USBPF_FRAME_ALIGN(length
);
475 memset(ptr
+ length
, 0, temp
- length
);
480 if (xfer
->flags_int
.isochronous_xfr
) {
481 offset
+= usbd_xfer_old_frame_length(xfer
, x
);
487 bpf_tap(bus
->ifp
->if_bpf
, buf
, totlen
);