xref: /dragonfly/sys/bus/u4b/usb_pf.c (revision e5a92d33)
1 /* $FreeBSD: head/sys/dev/usb/usb_pf.c 265779 2014-05-09 14:28:11Z hselasky $ */
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  * 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.
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 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <net/if.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>
49 #include <net/bpf.h>
50 #include <net/route.h>
51 #include <sys/sysctl.h>
52 #include <sys/condvar.h>
53 
54 #include <bus/u4b/usb.h>
55 #include <bus/u4b/usbdi.h>
56 #include <bus/u4b/usb_busdma.h>
57 
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>
65 
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);
76 
77 
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);
80 
81 static const char * usbusname = "usbus";
82 struct if_clone usbpf_cloner = IF_CLONE_INITIALIZER("usbus",
83 				 usbpf_clone_create,
84 				 usbpf_clone_destroy,
85 				 0, IF_MAXUNIT);
86 
87 static void
88 usbpf_init(void *arg)
89 {
90 	if_clone_attach(&usbpf_cloner);
91 	if (bootverbose)
92 		kprintf("usbpf: Initialized\n");
93 	return;
94 }
95 
96 static void
97 usbpf_uninit(void *arg)
98 {
99 	int devlcnt;
100 	device_t *devlp;
101 	devclass_t dc;
102 	struct usb_bus *ubus;
103 	int error;
104 	int i;
105 
106 	if_clone_detach(&usbpf_cloner);
107 
108 	dc = devclass_find(usbusname);
109 	if (dc == NULL)
110 		return;
111 	error = devclass_get_devices(dc, &devlp, &devlcnt);
112 	if (error)
113 		return;
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);
118 	}
119 	kfree(devlp, M_TEMP);
120 }
121 
122 static int
123 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
124 {
125 	/* No configuration allowed. */
126 	return (EINVAL);
127 }
128 
129 static struct usb_bus *
130 usbpf_ifname2ubus(int unit)
131 {
132 	device_t dev;
133 	devclass_t dc;
134 
135 	dc = devclass_find(usbusname);
136 	if (dc == NULL)
137 		return (NULL);
138 	dev = devclass_get_device(dc, unit);
139 	if (dev == NULL)
140 		return (NULL);
141 
142 	return (device_get_softc(dev));
143 }
144 
145 static int
146 usbpf_clone_create(struct if_clone *ifc, int unit, caddr_t params)
147 {
148 	struct ifnet *ifp;
149 	struct usb_bus *ubus;
150 
151 	ubus = usbpf_ifname2ubus(unit);
152 	if (ubus == NULL)
153 		return (EINVAL);
154 	if (ubus->ifp != NULL)
155 		return (EINVAL);
156 	ifp = ubus->ifp = if_alloc(IFT_USB);
157 	if (ifp == NULL) {
158 		device_printf(ubus->parent, "usbpf: Could not allocate "
159 		    "instance\n");
160 		return (ENOSPC);
161 	}
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;
170 	rt_ifmsg(ifp);
171 	/*
172 	 * XXX According to the specification of DLT_USB, it indicates
173 	 * packets beginning with USB setup header. But not sure all
174 	 * packets would be.
175 	 */
176 	bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
177 
178 	return (0);
179 }
180 
181 static int
182 usbpf_clone_destroy(struct ifnet *ifp)
183 {
184 	struct usb_bus *ubus;
185 	int unit;
186 
187 	ubus = ifp->if_softc;
188 	unit = ifp->if_dunit;
189 
190 	ubus->ifp = NULL;
191 	bpfdetach(ifp);
192 	if_detach(ifp);
193 	if_free(ifp);
194 
195 	return (0);
196 }
197 
198 void
199 usbpf_attach(struct usb_bus *ubus)
200 {
201 	if (bootverbose)
202 		device_printf(ubus->parent, "usbpf: Attached\n");
203 }
204 
205 void
206 usbpf_detach(struct usb_bus *ubus)
207 {
208 	if (ubus->ifp != NULL)
209 		usbpf_clone_destroy(ubus->ifp);
210 	if (bootverbose)
211 		device_printf(ubus->parent, "usbpf: Detached\n");
212 }
213 
214 static uint32_t
215 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
216 {
217 	uint32_t val = 0;
218 
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;
237 	return (val);
238 }
239 
240 static uint32_t
241 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
242 {
243 	uint32_t val = 0;
244 
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;
271 #if USB_HAVE_BUSDMA
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;
278 #endif
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;
287 
288 	return (val);
289 }
290 
291 static int
292 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
293 {
294 	int isread;
295 
296 	if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
297 	    (xfer->flags_int.control_hdr != 0)) {
298 		/* special case */
299 		if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
300 			/* The device controller writes to memory */
301 			isread = 1;
302 		} else {
303 			/* The host controller reads from memory */
304 			isread = 0;
305 		}
306 	} else {
307 		isread = USB_GET_DATA_ISREAD(xfer);
308 	}
309 	return (isread);
310 }
311 
312 static uint32_t
313 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
314 {
315 	uint32_t totlen;
316 	uint32_t x;
317 	uint32_t nframes;
318 
319 	if (type == USBPF_XFERTAP_SUBMIT)
320 		nframes = xfer->nframes;
321 	else
322 		nframes = xfer->aframes;
323 
324 	totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
325 
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(
331 				    xfer->frlengths[x]);
332 			}
333 		} else {
334 			if (type == USBPF_XFERTAP_SUBMIT) {
335 				totlen += USBPF_FRAME_ALIGN(
336 				    xfer->frlengths[x]);
337 			}
338 		}
339 	}
340 	return (totlen);
341 }
342 
343 void
344 usbpf_xfertap(struct usb_xfer *xfer, int type)
345 {
346 	struct usb_bus *bus;
347 	struct usbpf_pkthdr *up;
348 	struct usbpf_framehdr *uf;
349 	usb_frlength_t offset;
350 	uint32_t totlen;
351 	uint32_t frame;
352 	uint32_t temp;
353 	uint32_t nframes;
354 	uint32_t x;
355 	uint8_t *buf;
356 	uint8_t *ptr;
357 
358 	bus = xfer->xroot->bus;
359 
360 	/* sanity checks */
361 	if (bus->ifp == NULL)
362 		return;
363 #if 0 /* XXX this is not needed on dragonfly */
364 	if (!bpf_peers_present(bus->ifp->if_bpf))
365 		return;
366 #endif
367 	totlen = usbpf_xfer_precompute_size(xfer, type);
368 
369 	if (type == USBPF_XFERTAP_SUBMIT)
370 		nframes = xfer->nframes;
371 	else
372 		nframes = xfer->aframes;
373 
374 	/*
375 	 * XXX TODO XXX
376 	 *
377 	 * When BPF supports it we could pass a fragmented array of
378 	 * buffers avoiding the data copy operation here.
379 	 */
380 	buf = ptr = kmalloc(totlen, M_TEMP, M_NOWAIT);
381 	if (buf == NULL) {
382 		device_printf(bus->parent, "usbpf: Out of memory\n");
383 		return;
384 	}
385 
386 	up = (struct usbpf_pkthdr *)ptr;
387 	ptr += USBPF_HDR_LEN;
388 
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;
396 	else
397 		up->up_mode = USBPF_MODE_HOST;
398 	up->up_type = type;
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);
404 	temp = xfer->error;
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;
416 
417 	/* clear reserved area */
418 	memset(up->up_reserved, 0, sizeof(up->up_reserved));
419 
420 	/* init offset and frame */
421 	offset = 0;
422 	frame = 0;
423 
424 	/* iterate all the USB frames and copy data, if any */
425 	for (x = 0; x != nframes; x++) {
426 		uint32_t length;
427 		int isread;
428 
429 		/* get length */
430 		length = xfer->frlengths[x];
431 
432 		/* get frame header pointer */
433 		uf = (struct usbpf_framehdr *)ptr;
434 		ptr += USBPF_FRAME_HDR_LEN;
435 
436 		/* fill out packet header */
437 		uf->length = htole32(length);
438 		uf->flags = 0;
439 
440 		/* get information about data read/write */
441 		isread = usbpf_xfer_frame_is_read(xfer, x);
442 
443 		/* check if we need to copy any data */
444 		if (isread) {
445 			if (type == USBPF_XFERTAP_SUBMIT)
446 				length = 0;
447 			else {
448 				uf->flags |= htole32(
449 				    USBPF_FRAMEFLAG_DATA_FOLLOWS);
450 			}
451 		} else {
452 			if (type != USBPF_XFERTAP_SUBMIT)
453 				length = 0;
454 			else {
455 				uf->flags |= htole32(
456 				    USBPF_FRAMEFLAG_DATA_FOLLOWS);
457 			}
458 		}
459 
460 		/* check if data is read direction */
461 		if (isread)
462 			uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
463 
464 		/* copy USB data, if any */
465 		if (length != 0) {
466 			/* copy data */
467 			usbd_copy_out(&xfer->frbuffers[frame],
468 			    offset, ptr, length);
469 
470 			/* align length */
471 			temp = USBPF_FRAME_ALIGN(length);
472 
473 			/* zero pad */
474 			if (temp != length)
475 				memset(ptr + length, 0, temp - length);
476 
477 			ptr += temp;
478 		}
479 
480 		if (xfer->flags_int.isochronous_xfr) {
481 			offset += usbd_xfer_old_frame_length(xfer, x);
482 		} else {
483 			frame ++;
484 		}
485 	}
486 
487 	bpf_tap(bus->ifp->if_bpf, buf, totlen);
488 
489 	kfree(buf, M_TEMP);
490 }
491