xref: /freebsd/sys/dev/usb/usb_pf.c (revision fe1c24e3)
1 /*-
2  * Copyright (c) 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from the Stanford/CMU enet packet filter,
6  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8  * Berkeley Laboratory.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/bus.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <net/if.h>
46 #include <net/if_types.h>
47 #include <net/bpf.h>
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usb_busdma.h>
52 #include <dev/usb/usb_controller.h>
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_device.h>
56 #include <dev/usb/usb_bus.h>
57 #include <dev/usb/usb_pf.h>
58 #include <dev/usb/usb_transfer.h>
59 
60 void
61 usbpf_attach(struct usb_bus *ubus)
62 {
63 	struct ifnet *ifp;
64 
65 	ifp = ubus->ifp = if_alloc(IFT_USB);
66 	if_initname(ifp, "usbus", device_get_unit(ubus->bdev));
67 	if_attach(ifp);
68 
69 	KASSERT(sizeof(struct usbpf_pkthdr) == USBPF_HDR_LEN,
70 	    ("wrong USB pf header length (%zd)", sizeof(struct usbpf_pkthdr)));
71 
72 	/*
73 	 * XXX According to the specification of DLT_USB, it indicates packets
74 	 * beginning with USB setup header.  But not sure all packets would be.
75 	 */
76 	bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
77 
78 	if (bootverbose)
79 		device_printf(ubus->parent, "usbpf attached\n");
80 }
81 
82 void
83 usbpf_detach(struct usb_bus *ubus)
84 {
85 	struct ifnet *ifp = ubus->ifp;
86 
87 	if (ifp != NULL) {
88 		bpfdetach(ifp);
89 		if_detach(ifp);
90 		if_free(ifp);
91 	}
92 	ubus->ifp = NULL;
93 }
94 
95 static uint32_t
96 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
97 {
98 	uint32_t val = 0;
99 
100 	if (flags->force_short_xfer == 1)
101 		val |= USBPF_FLAG_FORCE_SHORT_XFER;
102 	if (flags->short_xfer_ok == 1)
103 		val |= USBPF_FLAG_SHORT_XFER_OK;
104 	if (flags->short_frames_ok == 1)
105 		val |= USBPF_FLAG_SHORT_FRAMES_OK;
106 	if (flags->pipe_bof == 1)
107 		val |= USBPF_FLAG_PIPE_BOF;
108 	if (flags->proxy_buffer == 1)
109 		val |= USBPF_FLAG_PROXY_BUFFER;
110 	if (flags->ext_buffer == 1)
111 		val |= USBPF_FLAG_EXT_BUFFER;
112 	if (flags->manual_status == 1)
113 		val |= USBPF_FLAG_MANUAL_STATUS;
114 	if (flags->no_pipe_ok == 1)
115 		val |= USBPF_FLAG_NO_PIPE_OK;
116 	if (flags->stall_pipe == 1)
117 		val |= USBPF_FLAG_STALL_PIPE;
118 	return (val);
119 }
120 
121 static uint32_t
122 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
123 {
124 	uint32_t val = 0;
125 
126 	if (flags->open == 1)
127 		val |= USBPF_STATUS_OPEN;
128 	if (flags->transferring == 1)
129 		val |= USBPF_STATUS_TRANSFERRING;
130 	if (flags->did_dma_delay == 1)
131 		val |= USBPF_STATUS_DID_DMA_DELAY;
132 	if (flags->did_close == 1)
133 		val |= USBPF_STATUS_DID_CLOSE;
134 	if (flags->draining == 1)
135 		val |= USBPF_STATUS_DRAINING;
136 	if (flags->started == 1)
137 		val |= USBPF_STATUS_STARTED;
138 	if (flags->bandwidth_reclaimed == 1)
139 		val |= USBPF_STATUS_BW_RECLAIMED;
140 	if (flags->control_xfr == 1)
141 		val |= USBPF_STATUS_CONTROL_XFR;
142 	if (flags->control_hdr == 1)
143 		val |= USBPF_STATUS_CONTROL_HDR;
144 	if (flags->control_act == 1)
145 		val |= USBPF_STATUS_CONTROL_ACT;
146 	if (flags->control_stall == 1)
147 		val |= USBPF_STATUS_CONTROL_STALL;
148 	if (flags->short_frames_ok == 1)
149 		val |= USBPF_STATUS_SHORT_FRAMES_OK;
150 	if (flags->short_xfer_ok == 1)
151 		val |= USBPF_STATUS_SHORT_XFER_OK;
152 #if USB_HAVE_BUSDMA
153 	if (flags->bdma_enable == 1)
154 		val |= USBPF_STATUS_BDMA_ENABLE;
155 	if (flags->bdma_no_post_sync == 1)
156 		val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
157 	if (flags->bdma_setup == 1)
158 		val |= USBPF_STATUS_BDMA_SETUP;
159 #endif
160 	if (flags->isochronous_xfr == 1)
161 		val |= USBPF_STATUS_ISOCHRONOUS_XFR;
162 	if (flags->curr_dma_set == 1)
163 		val |= USBPF_STATUS_CURR_DMA_SET;
164 	if (flags->can_cancel_immed == 1)
165 		val |= USBPF_STATUS_CAN_CANCEL_IMMED;
166 	if (flags->doing_callback == 1)
167 		val |= USBPF_STATUS_DOING_CALLBACK;
168 
169 	return (val);
170 }
171 
172 void
173 usbpf_xfertap(struct usb_xfer *xfer, int type)
174 {
175 	struct usb_endpoint *ep = xfer->endpoint;
176 	struct usb_page_search res;
177 	struct usb_xfer_root *info = xfer->xroot;
178 	struct usb_bus *bus = info->bus;
179 	struct usbpf_pkthdr *up;
180 	usb_frlength_t isoc_offset = 0;
181 	int i;
182 	char *buf, *ptr, *end;
183 
184 	if (!bpf_peers_present(bus->ifp->if_bpf))
185 		return;
186 
187 	/*
188 	 * XXX TODO
189 	 * Allocating the buffer here causes copy operations twice what's
190 	 * really inefficient. Copying usbpf_pkthdr and data is for USB packet
191 	 * read filter to pass a virtually linear buffer.
192 	 */
193 	buf = ptr = malloc(sizeof(struct usbpf_pkthdr) + (USB_PAGE_SIZE * 5),
194 	    M_TEMP, M_NOWAIT);
195 	if (buf == NULL) {
196 		printf("usbpf_xfertap: out of memory\n");	/* XXX */
197 		return;
198 	}
199 	end = buf + sizeof(struct usbpf_pkthdr) + (USB_PAGE_SIZE * 5);
200 
201 	bzero(ptr, sizeof(struct usbpf_pkthdr));
202 	up = (struct usbpf_pkthdr *)ptr;
203 	up->up_busunit = htole32(device_get_unit(bus->bdev));
204 	up->up_type = type;
205 	up->up_xfertype = ep->edesc->bmAttributes & UE_XFERTYPE;
206 	up->up_address = xfer->address;
207 	up->up_endpoint = xfer->endpointno;
208 	up->up_flags = htole32(usbpf_aggregate_xferflags(&xfer->flags));
209 	up->up_status = htole32(usbpf_aggregate_status(&xfer->flags_int));
210 	switch (type) {
211 	case USBPF_XFERTAP_SUBMIT:
212 		up->up_length = htole32(xfer->sumlen);
213 		up->up_frames = htole32(xfer->nframes);
214 		break;
215 	case USBPF_XFERTAP_DONE:
216 		up->up_length = htole32(xfer->actlen);
217 		up->up_frames = htole32(xfer->aframes);
218 		break;
219 	default:
220 		panic("wrong usbpf type (%d)", type);
221 	}
222 
223 	up->up_error = htole32(xfer->error);
224 	up->up_interval = htole32(xfer->interval);
225 	ptr += sizeof(struct usbpf_pkthdr);
226 
227 	for (i = 0; i < up->up_frames; i++) {
228 		if (ptr + sizeof(u_int32_t) >= end)
229 			goto done;
230 		*((u_int32_t *)ptr) = htole32(xfer->frlengths[i]);
231 		ptr += sizeof(u_int32_t);
232 
233 		if (ptr + xfer->frlengths[i] >= end)
234 			goto done;
235 		if (xfer->flags_int.isochronous_xfr == 1) {
236 			usbd_get_page(&xfer->frbuffers[0], isoc_offset, &res);
237 			isoc_offset += xfer->frlengths[i];
238 		} else
239 			usbd_get_page(&xfer->frbuffers[i], 0, &res);
240 		bcopy(res.buffer, ptr, xfer->frlengths[i]);
241 		ptr += xfer->frlengths[i];
242 	}
243 
244 	bpf_tap(bus->ifp->if_bpf, buf, ptr - buf);
245 done:
246 	free(buf, M_TEMP);
247 }
248