Deleted Added
sdiff udiff text old ( 218165 ) new ( 220301 )
full compact
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.

--- 19 unchanged lines hidden (view full) ---

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: head/sys/dev/usb/usb_pf.c 218165 2011-02-01 10:25:48Z hselasky $");
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
60void
61usbpf_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 ifp->if_flags = IFF_CANTCONFIG;
68 if_attach(ifp);
69 if_up(ifp);
70
71 KASSERT(sizeof(struct usbpf_pkthdr) == USBPF_HDR_LEN,
72 ("wrong USB pf header length (%zd)", sizeof(struct usbpf_pkthdr)));
73
74 /*
75 * XXX According to the specification of DLT_USB, it indicates packets
76 * beginning with USB setup header. But not sure all packets would be.
77 */
78 bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
79
80 if (bootverbose)
81 device_printf(ubus->parent, "usbpf attached\n");
82}
83
84void
85usbpf_detach(struct usb_bus *ubus)
86{
87 struct ifnet *ifp = ubus->ifp;
88
89 if (ifp != NULL) {

--- 77 unchanged lines hidden (view full) ---

167 if (flags->can_cancel_immed == 1)
168 val |= USBPF_STATUS_CAN_CANCEL_IMMED;
169 if (flags->doing_callback == 1)
170 val |= USBPF_STATUS_DOING_CALLBACK;
171
172 return (val);
173}
174
175void
176usbpf_xfertap(struct usb_xfer *xfer, int type)
177{
178 struct usb_endpoint *ep = xfer->endpoint;
179 struct usb_page_search res;
180 struct usb_xfer_root *info = xfer->xroot;
181 struct usb_bus *bus = info->bus;
182 struct usbpf_pkthdr *up;
183 usb_frlength_t isoc_offset = 0;
184 int i;
185 char *buf, *ptr, *end;
186
187 if (!bpf_peers_present(bus->ifp->if_bpf))
188 return;
189
190 /*
191 * XXX TODO
192 * Allocating the buffer here causes copy operations twice what's
193 * really inefficient. Copying usbpf_pkthdr and data is for USB packet
194 * read filter to pass a virtually linear buffer.
195 */
196 buf = ptr = malloc(sizeof(struct usbpf_pkthdr) + (USB_PAGE_SIZE * 5),
197 M_TEMP, M_NOWAIT);
198 if (buf == NULL) {
199 printf("usbpf_xfertap: out of memory\n"); /* XXX */
200 return;
201 }
202 end = buf + sizeof(struct usbpf_pkthdr) + (USB_PAGE_SIZE * 5);
203
204 bzero(ptr, sizeof(struct usbpf_pkthdr));
205 up = (struct usbpf_pkthdr *)ptr;
206 up->up_busunit = htole32(device_get_unit(bus->bdev));
207 up->up_type = type;
208 up->up_xfertype = ep->edesc->bmAttributes & UE_XFERTYPE;
209 up->up_address = xfer->address;
210 up->up_endpoint = xfer->endpointno;
211 up->up_flags = htole32(usbpf_aggregate_xferflags(&xfer->flags));
212 up->up_status = htole32(usbpf_aggregate_status(&xfer->flags_int));
213 switch (type) {
214 case USBPF_XFERTAP_SUBMIT:
215 up->up_length = htole32(xfer->sumlen);
216 up->up_frames = htole32(xfer->nframes);
217 break;
218 case USBPF_XFERTAP_DONE:
219 up->up_length = htole32(xfer->actlen);
220 up->up_frames = htole32(xfer->aframes);
221 break;
222 default:
223 panic("wrong usbpf type (%d)", type);
224 }
225
226 up->up_error = htole32(xfer->error);
227 up->up_interval = htole32(xfer->interval);
228 ptr += sizeof(struct usbpf_pkthdr);
229
230 for (i = 0; i < up->up_frames; i++) {
231 if (ptr + sizeof(uint32_t) >= end)
232 goto done;
233 *((uint32_t *)ptr) = htole32(xfer->frlengths[i]);
234 ptr += sizeof(uint32_t);
235
236 if (ptr + xfer->frlengths[i] >= end)
237 goto done;
238 if (xfer->flags_int.isochronous_xfr == 1) {
239 usbd_get_page(&xfer->frbuffers[0], isoc_offset, &res);
240 isoc_offset += xfer->frlengths[i];
241 } else
242 usbd_get_page(&xfer->frbuffers[i], 0, &res);
243 bcopy(res.buffer, ptr, xfer->frlengths[i]);
244 ptr += xfer->frlengths[i];
245 }
246
247 bpf_tap(bus->ifp->if_bpf, buf, ptr - buf);
248done:
249 free(buf, M_TEMP);
250}