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_clone.h>
46 #include <net/if_types.h>
47 #include <net/ifq_var.h>
49 #include <net/route.h>
50 #include <sys/sysctl.h>
51 #include <sys/condvar.h>
53 #include <bus/u4b/usb.h>
54 #include <bus/u4b/usbdi.h>
55 #include <bus/u4b/usb_busdma.h>
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>
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);
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
);
80 static const char * usbusname
= "usbus";
81 struct if_clone usbpf_cloner
= IF_CLONE_INITIALIZER("usbus",
89 if_clone_attach(&usbpf_cloner
);
91 kprintf("usbpf: Initialized\n");
96 usbpf_uninit(void *arg
)
101 struct usb_bus
*ubus
;
105 if_clone_detach(&usbpf_cloner
);
107 dc
= devclass_find(usbusname
);
110 error
= devclass_get_devices(dc
, &devlp
, &devlcnt
);
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
);
118 kfree(devlp
, M_TEMP
);
122 usbpf_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
124 /* No configuration allowed. */
128 static struct usb_bus
*
129 usbpf_ifname2ubus(int unit
)
134 dc
= devclass_find(usbusname
);
137 dev
= devclass_get_device(dc
, unit
);
141 return (device_get_softc(dev
));
145 usbpf_clone_create(struct if_clone
*ifc
, int unit
, caddr_t params
)
148 struct usb_bus
*ubus
;
150 ubus
= usbpf_ifname2ubus(unit
);
153 if (ubus
->ifp
!= NULL
)
155 ifp
= ubus
->ifp
= if_alloc(IFT_USB
);
157 device_printf(ubus
->parent
, "usbpf: Could not allocate "
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
;
171 * XXX According to the specification of DLT_USB, it indicates
172 * packets beginning with USB setup header. But not sure all
175 bpfattach(ifp
, DLT_USB
, USBPF_HDR_LEN
);
181 usbpf_clone_destroy(struct ifnet
*ifp
)
183 struct usb_bus
*ubus
;
186 ubus
= ifp
->if_softc
;
187 unit
= ifp
->if_dunit
;
198 usbpf_attach(struct usb_bus
*ubus
)
201 device_printf(ubus
->parent
, "usbpf: Attached\n");
205 usbpf_detach(struct usb_bus
*ubus
)
207 if (ubus
->ifp
!= NULL
)
208 usbpf_clone_destroy(ubus
->ifp
);
210 device_printf(ubus
->parent
, "usbpf: Detached\n");
214 usbpf_aggregate_xferflags(struct usb_xfer_flags
*flags
)
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
;
240 usbpf_aggregate_status(struct usb_xfer_flags_int
*flags
)
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
;
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
;
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
;
291 usbpf_xfer_frame_is_read(struct usb_xfer
*xfer
, uint32_t frame
)
295 if ((frame
== 0) && (xfer
->flags_int
.control_xfr
!= 0) &&
296 (xfer
->flags_int
.control_hdr
!= 0)) {
298 if (xfer
->flags_int
.usb_mode
== USB_MODE_DEVICE
) {
299 /* The device controller writes to memory */
302 /* The host controller reads from memory */
306 isread
= USB_GET_DATA_ISREAD(xfer
);
312 usbpf_xfer_precompute_size(struct usb_xfer
*xfer
, int type
)
318 if (type
== USBPF_XFERTAP_SUBMIT
)
319 nframes
= xfer
->nframes
;
321 nframes
= xfer
->aframes
;
323 totlen
= USBPF_HDR_LEN
+ (USBPF_FRAME_HDR_LEN
* nframes
);
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(
333 if (type
== USBPF_XFERTAP_SUBMIT
) {
334 totlen
+= USBPF_FRAME_ALIGN(
343 usbpf_xfertap(struct usb_xfer
*xfer
, int type
)
346 struct usbpf_pkthdr
*up
;
347 struct usbpf_framehdr
*uf
;
348 usb_frlength_t offset
;
357 bus
= xfer
->xroot
->bus
;
360 if (bus
->ifp
== NULL
)
362 #if 0 /* XXX this is not needed on dragonfly */
363 if (!bpf_peers_present(bus
->ifp
->if_bpf
))
366 totlen
= usbpf_xfer_precompute_size(xfer
, type
);
368 if (type
== USBPF_XFERTAP_SUBMIT
)
369 nframes
= xfer
->nframes
;
371 nframes
= xfer
->aframes
;
376 * When BPF supports it we could pass a fragmented array of
377 * buffers avoiding the data copy operation here.
379 buf
= ptr
= kmalloc(totlen
, M_TEMP
, M_NOWAIT
);
381 device_printf(bus
->parent
, "usbpf: Out of memory\n");
385 up
= (struct usbpf_pkthdr
*)ptr
;
386 ptr
+= USBPF_HDR_LEN
;
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
;
396 up
->up_mode
= USBPF_MODE_HOST
;
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
);
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
;
416 /* clear reserved area */
417 memset(up
->up_reserved
, 0, sizeof(up
->up_reserved
));
419 /* init offset and frame */
423 /* iterate all the USB frames and copy data, if any */
424 for (x
= 0; x
!= nframes
; x
++) {
429 length
= xfer
->frlengths
[x
];
431 /* get frame header pointer */
432 uf
= (struct usbpf_framehdr
*)ptr
;
433 ptr
+= USBPF_FRAME_HDR_LEN
;
435 /* fill out packet header */
436 uf
->length
= htole32(length
);
439 /* get information about data read/write */
440 isread
= usbpf_xfer_frame_is_read(xfer
, x
);
442 /* check if we need to copy any data */
444 if (type
== USBPF_XFERTAP_SUBMIT
)
447 uf
->flags
|= htole32(
448 USBPF_FRAMEFLAG_DATA_FOLLOWS
);
451 if (type
!= USBPF_XFERTAP_SUBMIT
)
454 uf
->flags
|= htole32(
455 USBPF_FRAMEFLAG_DATA_FOLLOWS
);
459 /* check if data is read direction */
461 uf
->flags
|= htole32(USBPF_FRAMEFLAG_READ
);
463 /* copy USB data, if any */
466 usbd_copy_out(&xfer
->frbuffers
[frame
],
467 offset
, ptr
, length
);
470 temp
= USBPF_FRAME_ALIGN(length
);
474 memset(ptr
+ length
, 0, temp
- length
);
479 if (xfer
->flags_int
.isochronous_xfr
) {
480 offset
+= usbd_xfer_old_frame_length(xfer
, x
);
486 bpf_tap(bus
->ifp
->if_bpf
, buf
, totlen
);