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, 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 caddr_t data __unused) 148 { 149 struct ifnet *ifp; 150 struct usb_bus *ubus; 151 152 ubus = usbpf_ifname2ubus(unit); 153 if (ubus == NULL) 154 return (EINVAL); 155 if (ubus->ifp != NULL) 156 return (EINVAL); 157 ifp = ubus->ifp = if_alloc(IFT_USB); 158 if (ifp == NULL) { 159 device_printf(ubus->parent, "usbpf: Could not allocate " 160 "instance\n"); 161 return (ENOSPC); 162 } 163 ksnprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d", usbusname, unit); 164 ifp->if_softc = ubus; 165 ifp->if_dname = usbusname; 166 ifp->if_dunit = unit; 167 ifp->if_ioctl = usbpf_ioctl; 168 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 169 if_attach(ifp, NULL); 170 ifp->if_flags |= IFF_UP; 171 rt_ifmsg(ifp); 172 /* 173 * XXX According to the specification of DLT_USB, it indicates 174 * packets beginning with USB setup header. But not sure all 175 * packets would be. 176 */ 177 bpfattach(ifp, DLT_USB, USBPF_HDR_LEN); 178 179 return (0); 180 } 181 182 static int 183 usbpf_clone_destroy(struct ifnet *ifp) 184 { 185 struct usb_bus *ubus; 186 int unit; 187 188 ubus = ifp->if_softc; 189 unit = ifp->if_dunit; 190 191 ubus->ifp = NULL; 192 bpfdetach(ifp); 193 if_detach(ifp); 194 if_free(ifp); 195 196 return (0); 197 } 198 199 void 200 usbpf_attach(struct usb_bus *ubus) 201 { 202 if (bootverbose) 203 device_printf(ubus->parent, "usbpf: Attached\n"); 204 } 205 206 void 207 usbpf_detach(struct usb_bus *ubus) 208 { 209 if (ubus->ifp != NULL) 210 usbpf_clone_destroy(ubus->ifp); 211 if (bootverbose) 212 device_printf(ubus->parent, "usbpf: Detached\n"); 213 } 214 215 static uint32_t 216 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags) 217 { 218 uint32_t val = 0; 219 220 if (flags->force_short_xfer == 1) 221 val |= USBPF_FLAG_FORCE_SHORT_XFER; 222 if (flags->short_xfer_ok == 1) 223 val |= USBPF_FLAG_SHORT_XFER_OK; 224 if (flags->short_frames_ok == 1) 225 val |= USBPF_FLAG_SHORT_FRAMES_OK; 226 if (flags->pipe_bof == 1) 227 val |= USBPF_FLAG_PIPE_BOF; 228 if (flags->proxy_buffer == 1) 229 val |= USBPF_FLAG_PROXY_BUFFER; 230 if (flags->ext_buffer == 1) 231 val |= USBPF_FLAG_EXT_BUFFER; 232 if (flags->manual_status == 1) 233 val |= USBPF_FLAG_MANUAL_STATUS; 234 if (flags->no_pipe_ok == 1) 235 val |= USBPF_FLAG_NO_PIPE_OK; 236 if (flags->stall_pipe == 1) 237 val |= USBPF_FLAG_STALL_PIPE; 238 return (val); 239 } 240 241 static uint32_t 242 usbpf_aggregate_status(struct usb_xfer_flags_int *flags) 243 { 244 uint32_t val = 0; 245 246 if (flags->open == 1) 247 val |= USBPF_STATUS_OPEN; 248 if (flags->transferring == 1) 249 val |= USBPF_STATUS_TRANSFERRING; 250 if (flags->did_dma_delay == 1) 251 val |= USBPF_STATUS_DID_DMA_DELAY; 252 if (flags->did_close == 1) 253 val |= USBPF_STATUS_DID_CLOSE; 254 if (flags->draining == 1) 255 val |= USBPF_STATUS_DRAINING; 256 if (flags->started == 1) 257 val |= USBPF_STATUS_STARTED; 258 if (flags->bandwidth_reclaimed == 1) 259 val |= USBPF_STATUS_BW_RECLAIMED; 260 if (flags->control_xfr == 1) 261 val |= USBPF_STATUS_CONTROL_XFR; 262 if (flags->control_hdr == 1) 263 val |= USBPF_STATUS_CONTROL_HDR; 264 if (flags->control_act == 1) 265 val |= USBPF_STATUS_CONTROL_ACT; 266 if (flags->control_stall == 1) 267 val |= USBPF_STATUS_CONTROL_STALL; 268 if (flags->short_frames_ok == 1) 269 val |= USBPF_STATUS_SHORT_FRAMES_OK; 270 if (flags->short_xfer_ok == 1) 271 val |= USBPF_STATUS_SHORT_XFER_OK; 272 #if USB_HAVE_BUSDMA 273 if (flags->bdma_enable == 1) 274 val |= USBPF_STATUS_BDMA_ENABLE; 275 if (flags->bdma_no_post_sync == 1) 276 val |= USBPF_STATUS_BDMA_NO_POST_SYNC; 277 if (flags->bdma_setup == 1) 278 val |= USBPF_STATUS_BDMA_SETUP; 279 #endif 280 if (flags->isochronous_xfr == 1) 281 val |= USBPF_STATUS_ISOCHRONOUS_XFR; 282 if (flags->curr_dma_set == 1) 283 val |= USBPF_STATUS_CURR_DMA_SET; 284 if (flags->can_cancel_immed == 1) 285 val |= USBPF_STATUS_CAN_CANCEL_IMMED; 286 if (flags->doing_callback == 1) 287 val |= USBPF_STATUS_DOING_CALLBACK; 288 289 return (val); 290 } 291 292 static int 293 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame) 294 { 295 int isread; 296 297 if ((frame == 0) && (xfer->flags_int.control_xfr != 0) && 298 (xfer->flags_int.control_hdr != 0)) { 299 /* special case */ 300 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 301 /* The device controller writes to memory */ 302 isread = 1; 303 } else { 304 /* The host controller reads from memory */ 305 isread = 0; 306 } 307 } else { 308 isread = USB_GET_DATA_ISREAD(xfer); 309 } 310 return (isread); 311 } 312 313 static uint32_t 314 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type) 315 { 316 uint32_t totlen; 317 uint32_t x; 318 uint32_t nframes; 319 320 if (type == USBPF_XFERTAP_SUBMIT) 321 nframes = xfer->nframes; 322 else 323 nframes = xfer->aframes; 324 325 totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes); 326 327 /* precompute all trace lengths */ 328 for (x = 0; x != nframes; x++) { 329 if (usbpf_xfer_frame_is_read(xfer, x)) { 330 if (type != USBPF_XFERTAP_SUBMIT) { 331 totlen += USBPF_FRAME_ALIGN( 332 xfer->frlengths[x]); 333 } 334 } else { 335 if (type == USBPF_XFERTAP_SUBMIT) { 336 totlen += USBPF_FRAME_ALIGN( 337 xfer->frlengths[x]); 338 } 339 } 340 } 341 return (totlen); 342 } 343 344 void 345 usbpf_xfertap(struct usb_xfer *xfer, int type) 346 { 347 struct usb_bus *bus; 348 struct usbpf_pkthdr *up; 349 struct usbpf_framehdr *uf; 350 usb_frlength_t offset; 351 uint32_t totlen; 352 uint32_t frame; 353 uint32_t temp; 354 uint32_t nframes; 355 uint32_t x; 356 uint8_t *buf; 357 uint8_t *ptr; 358 359 bus = xfer->xroot->bus; 360 361 /* sanity checks */ 362 if (bus->ifp == NULL) 363 return; 364 #if 0 /* XXX this is not needed on dragonfly */ 365 if (!bpf_peers_present(bus->ifp->if_bpf)) 366 return; 367 #endif 368 totlen = usbpf_xfer_precompute_size(xfer, type); 369 370 if (type == USBPF_XFERTAP_SUBMIT) 371 nframes = xfer->nframes; 372 else 373 nframes = xfer->aframes; 374 375 /* 376 * XXX TODO XXX 377 * 378 * When BPF supports it we could pass a fragmented array of 379 * buffers avoiding the data copy operation here. 380 */ 381 buf = ptr = kmalloc(totlen, M_TEMP, M_NOWAIT); 382 if (buf == NULL) { 383 device_printf(bus->parent, "usbpf: Out of memory\n"); 384 return; 385 } 386 387 up = (struct usbpf_pkthdr *)ptr; 388 ptr += USBPF_HDR_LEN; 389 390 /* fill out header */ 391 temp = device_get_unit(bus->bdev); 392 up->up_totlen = htole32(totlen); 393 up->up_busunit = htole32(temp); 394 up->up_address = xfer->xroot->udev->device_index; 395 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) 396 up->up_mode = USBPF_MODE_DEVICE; 397 else 398 up->up_mode = USBPF_MODE_HOST; 399 up->up_type = type; 400 up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE; 401 temp = usbpf_aggregate_xferflags(&xfer->flags); 402 up->up_flags = htole32(temp); 403 temp = usbpf_aggregate_status(&xfer->flags_int); 404 up->up_status = htole32(temp); 405 temp = xfer->error; 406 up->up_error = htole32(temp); 407 temp = xfer->interval; 408 up->up_interval = htole32(temp); 409 up->up_frames = htole32(nframes); 410 temp = xfer->max_packet_size; 411 up->up_packet_size = htole32(temp); 412 temp = xfer->max_packet_count; 413 up->up_packet_count = htole32(temp); 414 temp = xfer->endpointno; 415 up->up_endpoint = htole32(temp); 416 up->up_speed = xfer->xroot->udev->speed; 417 418 /* clear reserved area */ 419 memset(up->up_reserved, 0, sizeof(up->up_reserved)); 420 421 /* init offset and frame */ 422 offset = 0; 423 frame = 0; 424 425 /* iterate all the USB frames and copy data, if any */ 426 for (x = 0; x != nframes; x++) { 427 uint32_t length; 428 int isread; 429 430 /* get length */ 431 length = xfer->frlengths[x]; 432 433 /* get frame header pointer */ 434 uf = (struct usbpf_framehdr *)ptr; 435 ptr += USBPF_FRAME_HDR_LEN; 436 437 /* fill out packet header */ 438 uf->length = htole32(length); 439 uf->flags = 0; 440 441 /* get information about data read/write */ 442 isread = usbpf_xfer_frame_is_read(xfer, x); 443 444 /* check if we need to copy any data */ 445 if (isread) { 446 if (type == USBPF_XFERTAP_SUBMIT) 447 length = 0; 448 else { 449 uf->flags |= htole32( 450 USBPF_FRAMEFLAG_DATA_FOLLOWS); 451 } 452 } else { 453 if (type != USBPF_XFERTAP_SUBMIT) 454 length = 0; 455 else { 456 uf->flags |= htole32( 457 USBPF_FRAMEFLAG_DATA_FOLLOWS); 458 } 459 } 460 461 /* check if data is read direction */ 462 if (isread) 463 uf->flags |= htole32(USBPF_FRAMEFLAG_READ); 464 465 /* copy USB data, if any */ 466 if (length != 0) { 467 /* copy data */ 468 usbd_copy_out(&xfer->frbuffers[frame], 469 offset, ptr, length); 470 471 /* align length */ 472 temp = USBPF_FRAME_ALIGN(length); 473 474 /* zero pad */ 475 if (temp != length) 476 memset(ptr + length, 0, temp - length); 477 478 ptr += temp; 479 } 480 481 if (xfer->flags_int.isochronous_xfr) { 482 offset += usbd_xfer_old_frame_length(xfer, x); 483 } else { 484 frame ++; 485 } 486 } 487 488 bpf_tap(bus->ifp->if_bpf, buf, totlen); 489 490 kfree(buf, M_TEMP); 491 } 492