118ec6525SWeongyo Jeong /*-
27282444bSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
37282444bSPedro F. Giffuni *
418ec6525SWeongyo Jeong * Copyright (c) 1990, 1991, 1993
518ec6525SWeongyo Jeong * The Regents of the University of California. All rights reserved.
618ec6525SWeongyo Jeong *
718ec6525SWeongyo Jeong * This code is derived from the Stanford/CMU enet packet filter,
818ec6525SWeongyo Jeong * (net/enet.c) distributed as part of 4.3BSD, and code contributed
918ec6525SWeongyo Jeong * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
1018ec6525SWeongyo Jeong * Berkeley Laboratory.
1118ec6525SWeongyo Jeong *
1218ec6525SWeongyo Jeong * Redistribution and use in source and binary forms, with or without
1318ec6525SWeongyo Jeong * modification, are permitted provided that the following conditions
1418ec6525SWeongyo Jeong * are met:
1518ec6525SWeongyo Jeong * 1. Redistributions of source code must retain the above copyright
1618ec6525SWeongyo Jeong * notice, this list of conditions and the following disclaimer.
1718ec6525SWeongyo Jeong * 2. Redistributions in binary form must reproduce the above copyright
1818ec6525SWeongyo Jeong * notice, this list of conditions and the following disclaimer in the
1918ec6525SWeongyo Jeong * documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
2118ec6525SWeongyo Jeong * may be used to endorse or promote products derived from this software
2218ec6525SWeongyo Jeong * without specific prior written permission.
2318ec6525SWeongyo Jeong *
2418ec6525SWeongyo Jeong * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2518ec6525SWeongyo Jeong * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2618ec6525SWeongyo Jeong * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2718ec6525SWeongyo Jeong * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2818ec6525SWeongyo Jeong * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2918ec6525SWeongyo Jeong * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3018ec6525SWeongyo Jeong * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3118ec6525SWeongyo Jeong * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3218ec6525SWeongyo Jeong * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3318ec6525SWeongyo Jeong * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3418ec6525SWeongyo Jeong * SUCH DAMAGE.
3518ec6525SWeongyo Jeong */
3618ec6525SWeongyo Jeong
37d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
38d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
39d2b99310SHans Petter Selasky #else
4018ec6525SWeongyo Jeong #include <sys/param.h>
4118ec6525SWeongyo Jeong #include <sys/kernel.h>
4218ec6525SWeongyo Jeong #include <sys/bus.h>
4318ec6525SWeongyo Jeong #include <sys/fcntl.h>
4418ec6525SWeongyo Jeong #include <sys/malloc.h>
4518ec6525SWeongyo Jeong #include <sys/proc.h>
4618ec6525SWeongyo Jeong #include <sys/socket.h>
4718ec6525SWeongyo Jeong #include <sys/sockio.h>
4818ec6525SWeongyo Jeong #include <net/if.h>
4976039bc8SGleb Smirnoff #include <net/if_var.h>
50e376f9c3SWeongyo Jeong #include <net/if_types.h>
5173904744SHiroki Sato #include <net/if_clone.h>
52e376f9c3SWeongyo Jeong #include <net/bpf.h>
5331a11691SHans Petter Selasky #include <sys/sysctl.h>
5473904744SHiroki Sato #include <net/route.h>
5518ec6525SWeongyo Jeong
5618ec6525SWeongyo Jeong #include <dev/usb/usb.h>
5718ec6525SWeongyo Jeong #include <dev/usb/usbdi.h>
5818ec6525SWeongyo Jeong #include <dev/usb/usb_busdma.h>
5918ec6525SWeongyo Jeong #include <dev/usb/usb_controller.h>
6018ec6525SWeongyo Jeong #include <dev/usb/usb_core.h>
6118ec6525SWeongyo Jeong #include <dev/usb/usb_process.h>
6218ec6525SWeongyo Jeong #include <dev/usb/usb_device.h>
6318ec6525SWeongyo Jeong #include <dev/usb/usb_bus.h>
6418ec6525SWeongyo Jeong #include <dev/usb/usb_pf.h>
6518ec6525SWeongyo Jeong #include <dev/usb/usb_transfer.h>
66d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */
6718ec6525SWeongyo Jeong
682e39e9eeSHans Petter Selasky static void usbpf_init(void *);
692e39e9eeSHans Petter Selasky static void usbpf_uninit(void *);
70935b194dSJustin Hibbits static int usbpf_ioctl(if_t, u_long, caddr_t);
7173904744SHiroki Sato static int usbpf_clone_match(struct if_clone *, const char *);
7291ebcbe0SAlexander V. Chernikov static int usbpf_clone_create(struct if_clone *, char *, size_t,
73935b194dSJustin Hibbits struct ifc_data *, if_t *);
74935b194dSJustin Hibbits static int usbpf_clone_destroy(struct if_clone *, if_t, uint32_t);
7573904744SHiroki Sato static struct usb_bus *usbpf_ifname2ubus(const char *);
7673904744SHiroki Sato static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *);
7773904744SHiroki Sato static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *);
7873904744SHiroki Sato static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t);
7973904744SHiroki Sato static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int);
8031a11691SHans Petter Selasky
8142a58907SGleb Smirnoff static struct if_clone *usbpf_cloner;
8242a58907SGleb Smirnoff static const char usbusname[] = "usbus";
8331a11691SHans Petter Selasky
8473904744SHiroki Sato SYSINIT(usbpf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_init, NULL);
8573904744SHiroki Sato SYSUNINIT(usbpf_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_uninit, NULL);
8673904744SHiroki Sato
8773904744SHiroki Sato static void
usbpf_init(void * arg)882e39e9eeSHans Petter Selasky usbpf_init(void *arg)
8918ec6525SWeongyo Jeong {
9091ebcbe0SAlexander V. Chernikov struct if_clone_addreq req = {
9191ebcbe0SAlexander V. Chernikov .match_f = usbpf_clone_match,
9291ebcbe0SAlexander V. Chernikov .create_f = usbpf_clone_create,
9391ebcbe0SAlexander V. Chernikov .destroy_f = usbpf_clone_destroy,
9491ebcbe0SAlexander V. Chernikov };
9518ec6525SWeongyo Jeong
9691ebcbe0SAlexander V. Chernikov usbpf_cloner = ifc_attach_cloner(usbusname, &req);
9731a11691SHans Petter Selasky }
9831a11691SHans Petter Selasky
9973904744SHiroki Sato static void
usbpf_uninit(void * arg)1002e39e9eeSHans Petter Selasky usbpf_uninit(void *arg)
10173904744SHiroki Sato {
10273904744SHiroki Sato int devlcnt;
10373904744SHiroki Sato device_t *devlp;
10473904744SHiroki Sato devclass_t dc;
10573904744SHiroki Sato struct usb_bus *ubus;
10673904744SHiroki Sato int error;
10773904744SHiroki Sato int i;
10873904744SHiroki Sato
10942a58907SGleb Smirnoff if_clone_detach(usbpf_cloner);
11073904744SHiroki Sato
11142a58907SGleb Smirnoff dc = devclass_find(usbusname);
11273904744SHiroki Sato if (dc == NULL)
11373904744SHiroki Sato return;
11473904744SHiroki Sato error = devclass_get_devices(dc, &devlp, &devlcnt);
11573904744SHiroki Sato if (error)
11673904744SHiroki Sato return;
11773904744SHiroki Sato for (i = 0; i < devlcnt; i++) {
11873904744SHiroki Sato ubus = device_get_softc(devlp[i]);
11973904744SHiroki Sato if (ubus != NULL && ubus->ifp != NULL)
12091ebcbe0SAlexander V. Chernikov usbpf_clone_destroy(usbpf_cloner, ubus->ifp, 0);
12173904744SHiroki Sato }
12200081138SKevin Lo free(devlp, M_TEMP);
12373904744SHiroki Sato }
12473904744SHiroki Sato
12573904744SHiroki Sato static int
usbpf_ioctl(if_t ifp,u_long cmd,caddr_t data)126935b194dSJustin Hibbits usbpf_ioctl(if_t ifp, u_long cmd, caddr_t data)
12773904744SHiroki Sato {
12873904744SHiroki Sato
12973904744SHiroki Sato /* No configuration allowed. */
13073904744SHiroki Sato return (EINVAL);
13173904744SHiroki Sato }
13273904744SHiroki Sato
13373904744SHiroki Sato static struct usb_bus *
usbpf_ifname2ubus(const char * ifname)13473904744SHiroki Sato usbpf_ifname2ubus(const char *ifname)
13573904744SHiroki Sato {
13673904744SHiroki Sato device_t dev;
13773904744SHiroki Sato devclass_t dc;
13873904744SHiroki Sato int unit;
13973904744SHiroki Sato int error;
14073904744SHiroki Sato
14142a58907SGleb Smirnoff if (strncmp(ifname, usbusname, sizeof(usbusname) - 1) != 0)
14273904744SHiroki Sato return (NULL);
14373904744SHiroki Sato error = ifc_name2unit(ifname, &unit);
14473904744SHiroki Sato if (error || unit < 0)
14573904744SHiroki Sato return (NULL);
14642a58907SGleb Smirnoff dc = devclass_find(usbusname);
14773904744SHiroki Sato if (dc == NULL)
14873904744SHiroki Sato return (NULL);
14973904744SHiroki Sato dev = devclass_get_device(dc, unit);
15073904744SHiroki Sato if (dev == NULL)
15173904744SHiroki Sato return (NULL);
15273904744SHiroki Sato
15373904744SHiroki Sato return (device_get_softc(dev));
15473904744SHiroki Sato }
15573904744SHiroki Sato
15673904744SHiroki Sato static int
usbpf_clone_match(struct if_clone * ifc,const char * name)15773904744SHiroki Sato usbpf_clone_match(struct if_clone *ifc, const char *name)
15873904744SHiroki Sato {
15973904744SHiroki Sato struct usb_bus *ubus;
16073904744SHiroki Sato
16173904744SHiroki Sato ubus = usbpf_ifname2ubus(name);
16273904744SHiroki Sato if (ubus == NULL)
16373904744SHiroki Sato return (0);
16473904744SHiroki Sato if (ubus->ifp != NULL)
16573904744SHiroki Sato return (0);
16673904744SHiroki Sato
16773904744SHiroki Sato return (1);
16873904744SHiroki Sato }
16973904744SHiroki Sato
17073904744SHiroki Sato static int
usbpf_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,if_t * ifpp)17191ebcbe0SAlexander V. Chernikov usbpf_clone_create(struct if_clone *ifc, char *name, size_t len,
172935b194dSJustin Hibbits struct ifc_data *ifd, if_t *ifpp)
17373904744SHiroki Sato {
17473904744SHiroki Sato int error;
17573904744SHiroki Sato int unit;
176935b194dSJustin Hibbits if_t ifp;
17773904744SHiroki Sato struct usb_bus *ubus;
17873904744SHiroki Sato
17973904744SHiroki Sato error = ifc_name2unit(name, &unit);
18089b4ba99SHiroki Sato if (error)
18173904744SHiroki Sato return (error);
18289b4ba99SHiroki Sato if (unit < 0)
18389b4ba99SHiroki Sato return (EINVAL);
18473904744SHiroki Sato
18573904744SHiroki Sato ubus = usbpf_ifname2ubus(name);
18673904744SHiroki Sato if (ubus == NULL)
18773904744SHiroki Sato return (1);
18873904744SHiroki Sato if (ubus->ifp != NULL)
18973904744SHiroki Sato return (1);
19073904744SHiroki Sato
19173904744SHiroki Sato error = ifc_alloc_unit(ifc, &unit);
19273904744SHiroki Sato if (error) {
19331a11691SHans Petter Selasky device_printf(ubus->parent, "usbpf: Could not allocate "
19431a11691SHans Petter Selasky "instance\n");
19573904744SHiroki Sato return (error);
19631a11691SHans Petter Selasky }
19773904744SHiroki Sato ifp = ubus->ifp = if_alloc(IFT_USB);
198935b194dSJustin Hibbits if_setsoftc(ifp, ubus);
199935b194dSJustin Hibbits if_initname(ifp, usbusname, unit);
200935b194dSJustin Hibbits if_setname(ifp, name);
201935b194dSJustin Hibbits if_setioctlfn(ifp, usbpf_ioctl);
202e376f9c3SWeongyo Jeong if_attach(ifp);
203935b194dSJustin Hibbits if_setflagbits(ifp, IFF_UP, 0);
2041bcd230fSAlexander V. Chernikov rt_ifmsg(ifp, IFF_UP);
205e376f9c3SWeongyo Jeong /*
20631a11691SHans Petter Selasky * XXX According to the specification of DLT_USB, it indicates
20731a11691SHans Petter Selasky * packets beginning with USB setup header. But not sure all
20831a11691SHans Petter Selasky * packets would be.
209e376f9c3SWeongyo Jeong */
210e376f9c3SWeongyo Jeong bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
21191ebcbe0SAlexander V. Chernikov *ifpp = ifp;
21218ec6525SWeongyo Jeong
21373904744SHiroki Sato return (0);
21473904744SHiroki Sato }
21573904744SHiroki Sato
21673904744SHiroki Sato static int
usbpf_clone_destroy(struct if_clone * ifc,if_t ifp,uint32_t flags)217935b194dSJustin Hibbits usbpf_clone_destroy(struct if_clone *ifc, if_t ifp, uint32_t flags)
21873904744SHiroki Sato {
21973904744SHiroki Sato struct usb_bus *ubus;
22073904744SHiroki Sato int unit;
22173904744SHiroki Sato
222935b194dSJustin Hibbits ubus = if_getsoftc(ifp);
223935b194dSJustin Hibbits unit = if_getdunit(ifp);
22473904744SHiroki Sato
22522ef3de7SHans Petter Selasky /*
22622ef3de7SHans Petter Selasky * Lock USB before clearing the "ifp" pointer, to avoid
22722ef3de7SHans Petter Selasky * clearing the pointer in the middle of a TAP operation:
22822ef3de7SHans Petter Selasky */
22922ef3de7SHans Petter Selasky USB_BUS_LOCK(ubus);
23073904744SHiroki Sato ubus->ifp = NULL;
23122ef3de7SHans Petter Selasky USB_BUS_UNLOCK(ubus);
23273904744SHiroki Sato bpfdetach(ifp);
23373904744SHiroki Sato if_detach(ifp);
23473904744SHiroki Sato if_free(ifp);
23573904744SHiroki Sato ifc_free_unit(ifc, unit);
23673904744SHiroki Sato
23773904744SHiroki Sato return (0);
23873904744SHiroki Sato }
23973904744SHiroki Sato
24073904744SHiroki Sato void
usbpf_attach(struct usb_bus * ubus)24173904744SHiroki Sato usbpf_attach(struct usb_bus *ubus)
24273904744SHiroki Sato {
24373904744SHiroki Sato
24418ec6525SWeongyo Jeong if (bootverbose)
24531a11691SHans Petter Selasky device_printf(ubus->parent, "usbpf: Attached\n");
24618ec6525SWeongyo Jeong }
24718ec6525SWeongyo Jeong
24818ec6525SWeongyo Jeong void
usbpf_detach(struct usb_bus * ubus)24918ec6525SWeongyo Jeong usbpf_detach(struct usb_bus *ubus)
25018ec6525SWeongyo Jeong {
25118ec6525SWeongyo Jeong
25273904744SHiroki Sato if (ubus->ifp != NULL)
25391ebcbe0SAlexander V. Chernikov usbpf_clone_destroy(usbpf_cloner, ubus->ifp, 0);
25473904744SHiroki Sato if (bootverbose)
25573904744SHiroki Sato device_printf(ubus->parent, "usbpf: Detached\n");
25618ec6525SWeongyo Jeong }
25718ec6525SWeongyo Jeong
25818ec6525SWeongyo Jeong static uint32_t
usbpf_aggregate_xferflags(struct usb_xfer_flags * flags)25918ec6525SWeongyo Jeong usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
26018ec6525SWeongyo Jeong {
26118ec6525SWeongyo Jeong uint32_t val = 0;
26218ec6525SWeongyo Jeong
26318ec6525SWeongyo Jeong if (flags->force_short_xfer == 1)
26418ec6525SWeongyo Jeong val |= USBPF_FLAG_FORCE_SHORT_XFER;
26518ec6525SWeongyo Jeong if (flags->short_xfer_ok == 1)
26618ec6525SWeongyo Jeong val |= USBPF_FLAG_SHORT_XFER_OK;
26718ec6525SWeongyo Jeong if (flags->short_frames_ok == 1)
26818ec6525SWeongyo Jeong val |= USBPF_FLAG_SHORT_FRAMES_OK;
26918ec6525SWeongyo Jeong if (flags->pipe_bof == 1)
27018ec6525SWeongyo Jeong val |= USBPF_FLAG_PIPE_BOF;
27118ec6525SWeongyo Jeong if (flags->proxy_buffer == 1)
27218ec6525SWeongyo Jeong val |= USBPF_FLAG_PROXY_BUFFER;
27318ec6525SWeongyo Jeong if (flags->ext_buffer == 1)
27418ec6525SWeongyo Jeong val |= USBPF_FLAG_EXT_BUFFER;
27518ec6525SWeongyo Jeong if (flags->manual_status == 1)
27618ec6525SWeongyo Jeong val |= USBPF_FLAG_MANUAL_STATUS;
27718ec6525SWeongyo Jeong if (flags->no_pipe_ok == 1)
27818ec6525SWeongyo Jeong val |= USBPF_FLAG_NO_PIPE_OK;
27918ec6525SWeongyo Jeong if (flags->stall_pipe == 1)
28018ec6525SWeongyo Jeong val |= USBPF_FLAG_STALL_PIPE;
28118ec6525SWeongyo Jeong return (val);
28218ec6525SWeongyo Jeong }
28318ec6525SWeongyo Jeong
28418ec6525SWeongyo Jeong static uint32_t
usbpf_aggregate_status(struct usb_xfer_flags_int * flags)28518ec6525SWeongyo Jeong usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
28618ec6525SWeongyo Jeong {
28718ec6525SWeongyo Jeong uint32_t val = 0;
28818ec6525SWeongyo Jeong
28918ec6525SWeongyo Jeong if (flags->open == 1)
29018ec6525SWeongyo Jeong val |= USBPF_STATUS_OPEN;
29118ec6525SWeongyo Jeong if (flags->transferring == 1)
29218ec6525SWeongyo Jeong val |= USBPF_STATUS_TRANSFERRING;
29318ec6525SWeongyo Jeong if (flags->did_dma_delay == 1)
29418ec6525SWeongyo Jeong val |= USBPF_STATUS_DID_DMA_DELAY;
29518ec6525SWeongyo Jeong if (flags->did_close == 1)
29618ec6525SWeongyo Jeong val |= USBPF_STATUS_DID_CLOSE;
29718ec6525SWeongyo Jeong if (flags->draining == 1)
29818ec6525SWeongyo Jeong val |= USBPF_STATUS_DRAINING;
29918ec6525SWeongyo Jeong if (flags->started == 1)
30018ec6525SWeongyo Jeong val |= USBPF_STATUS_STARTED;
30118ec6525SWeongyo Jeong if (flags->bandwidth_reclaimed == 1)
30218ec6525SWeongyo Jeong val |= USBPF_STATUS_BW_RECLAIMED;
30318ec6525SWeongyo Jeong if (flags->control_xfr == 1)
30418ec6525SWeongyo Jeong val |= USBPF_STATUS_CONTROL_XFR;
30518ec6525SWeongyo Jeong if (flags->control_hdr == 1)
30618ec6525SWeongyo Jeong val |= USBPF_STATUS_CONTROL_HDR;
30718ec6525SWeongyo Jeong if (flags->control_act == 1)
30818ec6525SWeongyo Jeong val |= USBPF_STATUS_CONTROL_ACT;
30918ec6525SWeongyo Jeong if (flags->control_stall == 1)
31018ec6525SWeongyo Jeong val |= USBPF_STATUS_CONTROL_STALL;
31118ec6525SWeongyo Jeong if (flags->short_frames_ok == 1)
31218ec6525SWeongyo Jeong val |= USBPF_STATUS_SHORT_FRAMES_OK;
31318ec6525SWeongyo Jeong if (flags->short_xfer_ok == 1)
31418ec6525SWeongyo Jeong val |= USBPF_STATUS_SHORT_XFER_OK;
31518ec6525SWeongyo Jeong #if USB_HAVE_BUSDMA
31618ec6525SWeongyo Jeong if (flags->bdma_enable == 1)
31718ec6525SWeongyo Jeong val |= USBPF_STATUS_BDMA_ENABLE;
31818ec6525SWeongyo Jeong if (flags->bdma_no_post_sync == 1)
31918ec6525SWeongyo Jeong val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
32018ec6525SWeongyo Jeong if (flags->bdma_setup == 1)
32118ec6525SWeongyo Jeong val |= USBPF_STATUS_BDMA_SETUP;
32218ec6525SWeongyo Jeong #endif
32318ec6525SWeongyo Jeong if (flags->isochronous_xfr == 1)
32418ec6525SWeongyo Jeong val |= USBPF_STATUS_ISOCHRONOUS_XFR;
32518ec6525SWeongyo Jeong if (flags->curr_dma_set == 1)
32618ec6525SWeongyo Jeong val |= USBPF_STATUS_CURR_DMA_SET;
32718ec6525SWeongyo Jeong if (flags->can_cancel_immed == 1)
32818ec6525SWeongyo Jeong val |= USBPF_STATUS_CAN_CANCEL_IMMED;
32918ec6525SWeongyo Jeong if (flags->doing_callback == 1)
33018ec6525SWeongyo Jeong val |= USBPF_STATUS_DOING_CALLBACK;
33118ec6525SWeongyo Jeong
33218ec6525SWeongyo Jeong return (val);
33318ec6525SWeongyo Jeong }
33418ec6525SWeongyo Jeong
33531a11691SHans Petter Selasky static int
usbpf_xfer_frame_is_read(struct usb_xfer * xfer,uint32_t frame)33631a11691SHans Petter Selasky usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
33731a11691SHans Petter Selasky {
33831a11691SHans Petter Selasky int isread;
33931a11691SHans Petter Selasky
34031a11691SHans Petter Selasky if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
34131a11691SHans Petter Selasky (xfer->flags_int.control_hdr != 0)) {
34231a11691SHans Petter Selasky /* special case */
34331a11691SHans Petter Selasky if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
34431a11691SHans Petter Selasky /* The device controller writes to memory */
34531a11691SHans Petter Selasky isread = 1;
34631a11691SHans Petter Selasky } else {
34731a11691SHans Petter Selasky /* The host controller reads from memory */
34831a11691SHans Petter Selasky isread = 0;
34931a11691SHans Petter Selasky }
35031a11691SHans Petter Selasky } else {
35131a11691SHans Petter Selasky isread = USB_GET_DATA_ISREAD(xfer);
35231a11691SHans Petter Selasky }
35331a11691SHans Petter Selasky return (isread);
35431a11691SHans Petter Selasky }
35531a11691SHans Petter Selasky
35631a11691SHans Petter Selasky static uint32_t
usbpf_xfer_precompute_size(struct usb_xfer * xfer,int type)35731a11691SHans Petter Selasky usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
35831a11691SHans Petter Selasky {
35931a11691SHans Petter Selasky uint32_t totlen;
36031a11691SHans Petter Selasky uint32_t x;
36131a11691SHans Petter Selasky uint32_t nframes;
36231a11691SHans Petter Selasky
36331a11691SHans Petter Selasky if (type == USBPF_XFERTAP_SUBMIT)
36431a11691SHans Petter Selasky nframes = xfer->nframes;
36531a11691SHans Petter Selasky else
36631a11691SHans Petter Selasky nframes = xfer->aframes;
36731a11691SHans Petter Selasky
36831a11691SHans Petter Selasky totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
36931a11691SHans Petter Selasky
37031a11691SHans Petter Selasky /* precompute all trace lengths */
37131a11691SHans Petter Selasky for (x = 0; x != nframes; x++) {
37231a11691SHans Petter Selasky if (usbpf_xfer_frame_is_read(xfer, x)) {
37331a11691SHans Petter Selasky if (type != USBPF_XFERTAP_SUBMIT) {
37431a11691SHans Petter Selasky totlen += USBPF_FRAME_ALIGN(
37531a11691SHans Petter Selasky xfer->frlengths[x]);
37631a11691SHans Petter Selasky }
37731a11691SHans Petter Selasky } else {
37831a11691SHans Petter Selasky if (type == USBPF_XFERTAP_SUBMIT) {
37931a11691SHans Petter Selasky totlen += USBPF_FRAME_ALIGN(
38031a11691SHans Petter Selasky xfer->frlengths[x]);
38131a11691SHans Petter Selasky }
38231a11691SHans Petter Selasky }
38331a11691SHans Petter Selasky }
38431a11691SHans Petter Selasky return (totlen);
38531a11691SHans Petter Selasky }
38631a11691SHans Petter Selasky
38718ec6525SWeongyo Jeong void
usbpf_xfertap(struct usb_xfer * xfer,int type)38818ec6525SWeongyo Jeong usbpf_xfertap(struct usb_xfer *xfer, int type)
38918ec6525SWeongyo Jeong {
39031a11691SHans Petter Selasky struct usb_bus *bus;
39118ec6525SWeongyo Jeong struct usbpf_pkthdr *up;
39231a11691SHans Petter Selasky struct usbpf_framehdr *uf;
39331a11691SHans Petter Selasky usb_frlength_t offset;
39431a11691SHans Petter Selasky uint32_t totlen;
39531a11691SHans Petter Selasky uint32_t frame;
39631a11691SHans Petter Selasky uint32_t temp;
39731a11691SHans Petter Selasky uint32_t nframes;
39831a11691SHans Petter Selasky uint32_t x;
39931a11691SHans Petter Selasky uint8_t *buf;
40031a11691SHans Petter Selasky uint8_t *ptr;
40118ec6525SWeongyo Jeong
40231a11691SHans Petter Selasky bus = xfer->xroot->bus;
40331a11691SHans Petter Selasky
40431a11691SHans Petter Selasky /* sanity checks */
405*8f31b879SJustin Hibbits if (bus->ifp == NULL || !bpf_peers_present_if(bus->ifp))
40618ec6525SWeongyo Jeong return;
40718ec6525SWeongyo Jeong
40831a11691SHans Petter Selasky totlen = usbpf_xfer_precompute_size(xfer, type);
40931a11691SHans Petter Selasky
41031a11691SHans Petter Selasky if (type == USBPF_XFERTAP_SUBMIT)
41131a11691SHans Petter Selasky nframes = xfer->nframes;
41231a11691SHans Petter Selasky else
41331a11691SHans Petter Selasky nframes = xfer->aframes;
41431a11691SHans Petter Selasky
41518ec6525SWeongyo Jeong /*
41631a11691SHans Petter Selasky * XXX TODO XXX
41731a11691SHans Petter Selasky *
41831a11691SHans Petter Selasky * When BPF supports it we could pass a fragmented array of
41931a11691SHans Petter Selasky * buffers avoiding the data copy operation here.
42018ec6525SWeongyo Jeong */
42131a11691SHans Petter Selasky buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT);
42218ec6525SWeongyo Jeong if (buf == NULL) {
42331a11691SHans Petter Selasky device_printf(bus->parent, "usbpf: Out of memory\n");
42418ec6525SWeongyo Jeong return;
42518ec6525SWeongyo Jeong }
42618ec6525SWeongyo Jeong
42718ec6525SWeongyo Jeong up = (struct usbpf_pkthdr *)ptr;
42831a11691SHans Petter Selasky ptr += USBPF_HDR_LEN;
42931a11691SHans Petter Selasky
43031a11691SHans Petter Selasky /* fill out header */
43131a11691SHans Petter Selasky temp = device_get_unit(bus->bdev);
43231a11691SHans Petter Selasky up->up_totlen = htole32(totlen);
43331a11691SHans Petter Selasky up->up_busunit = htole32(temp);
43431a11691SHans Petter Selasky up->up_address = xfer->xroot->udev->device_index;
43531a11691SHans Petter Selasky if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
43631a11691SHans Petter Selasky up->up_mode = USBPF_MODE_DEVICE;
43731a11691SHans Petter Selasky else
43831a11691SHans Petter Selasky up->up_mode = USBPF_MODE_HOST;
43918ec6525SWeongyo Jeong up->up_type = type;
44031a11691SHans Petter Selasky up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
44131a11691SHans Petter Selasky temp = usbpf_aggregate_xferflags(&xfer->flags);
44231a11691SHans Petter Selasky up->up_flags = htole32(temp);
44331a11691SHans Petter Selasky temp = usbpf_aggregate_status(&xfer->flags_int);
44431a11691SHans Petter Selasky up->up_status = htole32(temp);
44531a11691SHans Petter Selasky temp = xfer->error;
44631a11691SHans Petter Selasky up->up_error = htole32(temp);
44731a11691SHans Petter Selasky temp = xfer->interval;
44831a11691SHans Petter Selasky up->up_interval = htole32(temp);
44931a11691SHans Petter Selasky up->up_frames = htole32(nframes);
45031a11691SHans Petter Selasky temp = xfer->max_packet_size;
45131a11691SHans Petter Selasky up->up_packet_size = htole32(temp);
45231a11691SHans Petter Selasky temp = xfer->max_packet_count;
45331a11691SHans Petter Selasky up->up_packet_count = htole32(temp);
45431a11691SHans Petter Selasky temp = xfer->endpointno;
45531a11691SHans Petter Selasky up->up_endpoint = htole32(temp);
45631a11691SHans Petter Selasky up->up_speed = xfer->xroot->udev->speed;
45731a11691SHans Petter Selasky
45831a11691SHans Petter Selasky /* clear reserved area */
45931a11691SHans Petter Selasky memset(up->up_reserved, 0, sizeof(up->up_reserved));
46031a11691SHans Petter Selasky
46131a11691SHans Petter Selasky /* init offset and frame */
46231a11691SHans Petter Selasky offset = 0;
46331a11691SHans Petter Selasky frame = 0;
46431a11691SHans Petter Selasky
46531a11691SHans Petter Selasky /* iterate all the USB frames and copy data, if any */
46631a11691SHans Petter Selasky for (x = 0; x != nframes; x++) {
46731a11691SHans Petter Selasky uint32_t length;
46831a11691SHans Petter Selasky int isread;
46931a11691SHans Petter Selasky
47031a11691SHans Petter Selasky /* get length */
47131a11691SHans Petter Selasky length = xfer->frlengths[x];
47231a11691SHans Petter Selasky
47331a11691SHans Petter Selasky /* get frame header pointer */
47431a11691SHans Petter Selasky uf = (struct usbpf_framehdr *)ptr;
47531a11691SHans Petter Selasky ptr += USBPF_FRAME_HDR_LEN;
47631a11691SHans Petter Selasky
47731a11691SHans Petter Selasky /* fill out packet header */
47831a11691SHans Petter Selasky uf->length = htole32(length);
47931a11691SHans Petter Selasky uf->flags = 0;
48031a11691SHans Petter Selasky
48131a11691SHans Petter Selasky /* get information about data read/write */
48231a11691SHans Petter Selasky isread = usbpf_xfer_frame_is_read(xfer, x);
48331a11691SHans Petter Selasky
48431a11691SHans Petter Selasky /* check if we need to copy any data */
48531a11691SHans Petter Selasky if (isread) {
48631a11691SHans Petter Selasky if (type == USBPF_XFERTAP_SUBMIT)
48731a11691SHans Petter Selasky length = 0;
48831a11691SHans Petter Selasky else {
48931a11691SHans Petter Selasky uf->flags |= htole32(
49031a11691SHans Petter Selasky USBPF_FRAMEFLAG_DATA_FOLLOWS);
49131a11691SHans Petter Selasky }
49231a11691SHans Petter Selasky } else {
49331a11691SHans Petter Selasky if (type != USBPF_XFERTAP_SUBMIT)
49431a11691SHans Petter Selasky length = 0;
49531a11691SHans Petter Selasky else {
49631a11691SHans Petter Selasky uf->flags |= htole32(
49731a11691SHans Petter Selasky USBPF_FRAMEFLAG_DATA_FOLLOWS);
49831a11691SHans Petter Selasky }
49918ec6525SWeongyo Jeong }
50018ec6525SWeongyo Jeong
50131a11691SHans Petter Selasky /* check if data is read direction */
50231a11691SHans Petter Selasky if (isread)
50331a11691SHans Petter Selasky uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
50418ec6525SWeongyo Jeong
50531a11691SHans Petter Selasky /* copy USB data, if any */
50631a11691SHans Petter Selasky if (length != 0) {
50731a11691SHans Petter Selasky /* copy data */
50831a11691SHans Petter Selasky usbd_copy_out(&xfer->frbuffers[frame],
50931a11691SHans Petter Selasky offset, ptr, length);
51018ec6525SWeongyo Jeong
51131a11691SHans Petter Selasky /* align length */
51231a11691SHans Petter Selasky temp = USBPF_FRAME_ALIGN(length);
51331a11691SHans Petter Selasky
51431a11691SHans Petter Selasky /* zero pad */
51531a11691SHans Petter Selasky if (temp != length)
51631a11691SHans Petter Selasky memset(ptr + length, 0, temp - length);
51731a11691SHans Petter Selasky
51831a11691SHans Petter Selasky ptr += temp;
51918ec6525SWeongyo Jeong }
52018ec6525SWeongyo Jeong
52131a11691SHans Petter Selasky if (xfer->flags_int.isochronous_xfr) {
52231a11691SHans Petter Selasky offset += usbd_xfer_old_frame_length(xfer, x);
52331a11691SHans Petter Selasky } else {
52431a11691SHans Petter Selasky frame ++;
52531a11691SHans Petter Selasky }
52631a11691SHans Petter Selasky }
52731a11691SHans Petter Selasky
528935b194dSJustin Hibbits bpf_tap_if(bus->ifp, buf, totlen);
52931a11691SHans Petter Selasky
530e376f9c3SWeongyo Jeong free(buf, M_TEMP);
53118ec6525SWeongyo Jeong }
532