The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/usb_pf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $FreeBSD$ */
    2 /*-
    3  * SPDX-License-Identifier: BSD-3-Clause
    4  *
    5  * Copyright (c) 1990, 1991, 1993
    6  *      The Regents of the University of California.  All rights reserved.
    7  *
    8  * This code is derived from the Stanford/CMU enet packet filter,
    9  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
   10  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
   11  * Berkeley Laboratory.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. Neither the name of the University nor the names of its contributors
   22  *    may be used to endorse or promote products derived from this software
   23  *    without specific prior written permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   35  * SUCH DAMAGE.
   36  */
   37 
   38 #ifdef USB_GLOBAL_INCLUDE_FILE
   39 #include USB_GLOBAL_INCLUDE_FILE
   40 #else
   41 #include <sys/param.h>
   42 #include <sys/kernel.h>
   43 #include <sys/bus.h>
   44 #include <sys/fcntl.h>
   45 #include <sys/malloc.h>
   46 #include <sys/proc.h>
   47 #include <sys/socket.h>
   48 #include <sys/sockio.h>
   49 #include <net/if.h>
   50 #include <net/if_var.h>
   51 #include <net/if_types.h>
   52 #include <net/if_clone.h>
   53 #include <net/bpf.h>
   54 #include <sys/sysctl.h>
   55 #include <net/route.h>
   56 
   57 #include <dev/usb/usb.h>
   58 #include <dev/usb/usbdi.h>
   59 #include <dev/usb/usb_busdma.h>
   60 #include <dev/usb/usb_controller.h>
   61 #include <dev/usb/usb_core.h>
   62 #include <dev/usb/usb_process.h>
   63 #include <dev/usb/usb_device.h>
   64 #include <dev/usb/usb_bus.h>
   65 #include <dev/usb/usb_pf.h>
   66 #include <dev/usb/usb_transfer.h>
   67 #endif                  /* USB_GLOBAL_INCLUDE_FILE */
   68 
   69 static void usbpf_init(void *);
   70 static void usbpf_uninit(void *);
   71 static int usbpf_ioctl(struct ifnet *, u_long, caddr_t);
   72 static int usbpf_clone_match(struct if_clone *, const char *);
   73 static int usbpf_clone_create(struct if_clone *, char *, size_t,
   74             struct ifc_data *, struct ifnet **);
   75 static int usbpf_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
   76 static struct usb_bus *usbpf_ifname2ubus(const char *);
   77 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *);
   78 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *);
   79 static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t);
   80 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int);
   81 
   82 static struct if_clone *usbpf_cloner;
   83 static const char usbusname[] = "usbus";
   84 
   85 SYSINIT(usbpf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_init, NULL);
   86 SYSUNINIT(usbpf_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_uninit, NULL);
   87 
   88 static void
   89 usbpf_init(void *arg)
   90 {
   91         struct if_clone_addreq req = {
   92                 .match_f = usbpf_clone_match,
   93                 .create_f = usbpf_clone_create,
   94                 .destroy_f = usbpf_clone_destroy,
   95         };
   96 
   97         usbpf_cloner = ifc_attach_cloner(usbusname, &req);
   98 }
   99 
  100 static void
  101 usbpf_uninit(void *arg)
  102 {
  103         int devlcnt;
  104         device_t *devlp;
  105         devclass_t dc;
  106         struct usb_bus *ubus;
  107         int error;
  108         int i;
  109 
  110         if_clone_detach(usbpf_cloner);
  111 
  112         dc = devclass_find(usbusname);
  113         if (dc == NULL)
  114                 return;
  115         error = devclass_get_devices(dc, &devlp, &devlcnt);
  116         if (error)
  117                 return;
  118         for (i = 0; i < devlcnt; i++) {
  119                 ubus = device_get_softc(devlp[i]);
  120                 if (ubus != NULL && ubus->ifp != NULL)
  121                         usbpf_clone_destroy(usbpf_cloner, ubus->ifp, 0);
  122         }
  123         free(devlp, M_TEMP);
  124 }
  125 
  126 static int
  127 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  128 {
  129 
  130         /* No configuration allowed. */
  131         return (EINVAL);
  132 }
  133 
  134 static struct usb_bus *
  135 usbpf_ifname2ubus(const char *ifname)
  136 {
  137         device_t dev;
  138         devclass_t dc;
  139         int unit;
  140         int error;
  141 
  142         if (strncmp(ifname, usbusname, sizeof(usbusname) - 1) != 0)
  143                 return (NULL);
  144         error = ifc_name2unit(ifname, &unit);
  145         if (error || unit < 0)
  146                 return (NULL);
  147         dc = devclass_find(usbusname);
  148         if (dc == NULL)
  149                 return (NULL);
  150         dev = devclass_get_device(dc, unit);
  151         if (dev == NULL)
  152                 return (NULL);
  153 
  154         return (device_get_softc(dev));
  155 }
  156 
  157 static int
  158 usbpf_clone_match(struct if_clone *ifc, const char *name)
  159 {
  160         struct usb_bus *ubus;
  161 
  162         ubus = usbpf_ifname2ubus(name);
  163         if (ubus == NULL)
  164                 return (0);
  165         if (ubus->ifp != NULL)
  166                 return (0);
  167 
  168         return (1);
  169 }
  170 
  171 static int
  172 usbpf_clone_create(struct if_clone *ifc, char *name, size_t len,
  173     struct ifc_data *ifd, struct ifnet **ifpp)
  174 {
  175         int error;
  176         int unit;
  177         struct ifnet *ifp;
  178         struct usb_bus *ubus;
  179 
  180         error = ifc_name2unit(name, &unit);
  181         if (error)
  182                 return (error);
  183         if (unit < 0)
  184                 return (EINVAL);
  185 
  186         ubus = usbpf_ifname2ubus(name);
  187         if (ubus == NULL)
  188                 return (1);
  189         if (ubus->ifp != NULL)
  190                 return (1);
  191 
  192         error = ifc_alloc_unit(ifc, &unit);
  193         if (error) {
  194                 device_printf(ubus->parent, "usbpf: Could not allocate "
  195                     "instance\n");
  196                 return (error);
  197         }
  198         ifp = ubus->ifp = if_alloc(IFT_USB);
  199         if (ifp == NULL) {
  200                 ifc_free_unit(ifc, unit);
  201                 device_printf(ubus->parent, "usbpf: Could not allocate "
  202                     "instance\n");
  203                 return (ENOSPC);
  204         }
  205         strlcpy(ifp->if_xname, name, sizeof(ifp->if_xname));
  206         ifp->if_softc = ubus;
  207         ifp->if_dname = usbusname;
  208         ifp->if_dunit = unit;
  209         ifp->if_ioctl = usbpf_ioctl;
  210         if_attach(ifp);
  211         ifp->if_flags |= IFF_UP;
  212         rt_ifmsg(ifp, IFF_UP);
  213         /*
  214          * XXX According to the specification of DLT_USB, it indicates
  215          * packets beginning with USB setup header. But not sure all
  216          * packets would be.
  217          */
  218         bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
  219         *ifpp = ifp;
  220 
  221         return (0);
  222 }
  223 
  224 static int
  225 usbpf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
  226 {
  227         struct usb_bus *ubus;
  228         int unit;
  229 
  230         ubus = ifp->if_softc;
  231         unit = ifp->if_dunit;
  232 
  233         /*
  234          * Lock USB before clearing the "ifp" pointer, to avoid
  235          * clearing the pointer in the middle of a TAP operation:
  236          */
  237         USB_BUS_LOCK(ubus);
  238         ubus->ifp = NULL;
  239         USB_BUS_UNLOCK(ubus);
  240         bpfdetach(ifp);
  241         if_detach(ifp);
  242         if_free(ifp);
  243         ifc_free_unit(ifc, unit);
  244 
  245         return (0);
  246 }
  247 
  248 void
  249 usbpf_attach(struct usb_bus *ubus)
  250 {
  251 
  252         if (bootverbose)
  253                 device_printf(ubus->parent, "usbpf: Attached\n");
  254 }
  255 
  256 void
  257 usbpf_detach(struct usb_bus *ubus)
  258 {
  259 
  260         if (ubus->ifp != NULL)
  261                 usbpf_clone_destroy(usbpf_cloner, ubus->ifp, 0);
  262         if (bootverbose)
  263                 device_printf(ubus->parent, "usbpf: Detached\n");
  264 }
  265 
  266 static uint32_t
  267 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
  268 {
  269         uint32_t val = 0;
  270 
  271         if (flags->force_short_xfer == 1)
  272                 val |= USBPF_FLAG_FORCE_SHORT_XFER;
  273         if (flags->short_xfer_ok == 1)
  274                 val |= USBPF_FLAG_SHORT_XFER_OK;
  275         if (flags->short_frames_ok == 1)
  276                 val |= USBPF_FLAG_SHORT_FRAMES_OK;
  277         if (flags->pipe_bof == 1)
  278                 val |= USBPF_FLAG_PIPE_BOF;
  279         if (flags->proxy_buffer == 1)
  280                 val |= USBPF_FLAG_PROXY_BUFFER;
  281         if (flags->ext_buffer == 1)
  282                 val |= USBPF_FLAG_EXT_BUFFER;
  283         if (flags->manual_status == 1)
  284                 val |= USBPF_FLAG_MANUAL_STATUS;
  285         if (flags->no_pipe_ok == 1)
  286                 val |= USBPF_FLAG_NO_PIPE_OK;
  287         if (flags->stall_pipe == 1)
  288                 val |= USBPF_FLAG_STALL_PIPE;
  289         return (val);
  290 }
  291 
  292 static uint32_t
  293 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
  294 {
  295         uint32_t val = 0;
  296 
  297         if (flags->open == 1)
  298                 val |= USBPF_STATUS_OPEN;
  299         if (flags->transferring == 1)
  300                 val |= USBPF_STATUS_TRANSFERRING;
  301         if (flags->did_dma_delay == 1)
  302                 val |= USBPF_STATUS_DID_DMA_DELAY;
  303         if (flags->did_close == 1)
  304                 val |= USBPF_STATUS_DID_CLOSE;
  305         if (flags->draining == 1)
  306                 val |= USBPF_STATUS_DRAINING;
  307         if (flags->started == 1)
  308                 val |= USBPF_STATUS_STARTED;
  309         if (flags->bandwidth_reclaimed == 1)
  310                 val |= USBPF_STATUS_BW_RECLAIMED;
  311         if (flags->control_xfr == 1)
  312                 val |= USBPF_STATUS_CONTROL_XFR;
  313         if (flags->control_hdr == 1)
  314                 val |= USBPF_STATUS_CONTROL_HDR;
  315         if (flags->control_act == 1)
  316                 val |= USBPF_STATUS_CONTROL_ACT;
  317         if (flags->control_stall == 1)
  318                 val |= USBPF_STATUS_CONTROL_STALL;
  319         if (flags->short_frames_ok == 1)
  320                 val |= USBPF_STATUS_SHORT_FRAMES_OK;
  321         if (flags->short_xfer_ok == 1)
  322                 val |= USBPF_STATUS_SHORT_XFER_OK;
  323 #if USB_HAVE_BUSDMA
  324         if (flags->bdma_enable == 1)
  325                 val |= USBPF_STATUS_BDMA_ENABLE;
  326         if (flags->bdma_no_post_sync == 1)
  327                 val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
  328         if (flags->bdma_setup == 1)
  329                 val |= USBPF_STATUS_BDMA_SETUP;
  330 #endif
  331         if (flags->isochronous_xfr == 1)
  332                 val |= USBPF_STATUS_ISOCHRONOUS_XFR;
  333         if (flags->curr_dma_set == 1)
  334                 val |= USBPF_STATUS_CURR_DMA_SET;
  335         if (flags->can_cancel_immed == 1)
  336                 val |= USBPF_STATUS_CAN_CANCEL_IMMED;
  337         if (flags->doing_callback == 1)
  338                 val |= USBPF_STATUS_DOING_CALLBACK;
  339 
  340         return (val);
  341 }
  342 
  343 static int
  344 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
  345 {
  346         int isread;
  347 
  348         if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
  349             (xfer->flags_int.control_hdr != 0)) {
  350                 /* special case */
  351                 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
  352                         /* The device controller writes to memory */
  353                         isread = 1;
  354                 } else {
  355                         /* The host controller reads from memory */
  356                         isread = 0;
  357                 }
  358         } else {
  359                 isread = USB_GET_DATA_ISREAD(xfer);
  360         }
  361         return (isread);
  362 }
  363 
  364 static uint32_t
  365 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
  366 {
  367         uint32_t totlen;
  368         uint32_t x;
  369         uint32_t nframes;
  370 
  371         if (type == USBPF_XFERTAP_SUBMIT)
  372                 nframes = xfer->nframes;
  373         else
  374                 nframes = xfer->aframes;
  375 
  376         totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
  377 
  378         /* precompute all trace lengths */
  379         for (x = 0; x != nframes; x++) {
  380                 if (usbpf_xfer_frame_is_read(xfer, x)) {
  381                         if (type != USBPF_XFERTAP_SUBMIT) {
  382                                 totlen += USBPF_FRAME_ALIGN(
  383                                     xfer->frlengths[x]);
  384                         }
  385                 } else {
  386                         if (type == USBPF_XFERTAP_SUBMIT) {
  387                                 totlen += USBPF_FRAME_ALIGN(
  388                                     xfer->frlengths[x]);
  389                         }
  390                 }
  391         }
  392         return (totlen);
  393 }
  394 
  395 void
  396 usbpf_xfertap(struct usb_xfer *xfer, int type)
  397 {
  398         struct usb_bus *bus;
  399         struct usbpf_pkthdr *up;
  400         struct usbpf_framehdr *uf;
  401         usb_frlength_t offset;
  402         uint32_t totlen;
  403         uint32_t frame;
  404         uint32_t temp;
  405         uint32_t nframes;
  406         uint32_t x;
  407         uint8_t *buf;
  408         uint8_t *ptr;
  409 
  410         bus = xfer->xroot->bus;
  411 
  412         /* sanity checks */
  413         if (bus->ifp == NULL || bus->ifp->if_bpf == NULL)
  414                 return;
  415         if (!bpf_peers_present(bus->ifp->if_bpf))
  416                 return;
  417 
  418         totlen = usbpf_xfer_precompute_size(xfer, type);
  419 
  420         if (type == USBPF_XFERTAP_SUBMIT)
  421                 nframes = xfer->nframes;
  422         else
  423                 nframes = xfer->aframes;
  424 
  425         /*
  426          * XXX TODO XXX
  427          *
  428          * When BPF supports it we could pass a fragmented array of
  429          * buffers avoiding the data copy operation here.
  430          */
  431         buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT);
  432         if (buf == NULL) {
  433                 device_printf(bus->parent, "usbpf: Out of memory\n");
  434                 return;
  435         }
  436 
  437         up = (struct usbpf_pkthdr *)ptr;
  438         ptr += USBPF_HDR_LEN;
  439 
  440         /* fill out header */
  441         temp = device_get_unit(bus->bdev);
  442         up->up_totlen = htole32(totlen);
  443         up->up_busunit = htole32(temp);
  444         up->up_address = xfer->xroot->udev->device_index;
  445         if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
  446                 up->up_mode = USBPF_MODE_DEVICE;
  447         else
  448                 up->up_mode = USBPF_MODE_HOST;
  449         up->up_type = type;
  450         up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
  451         temp = usbpf_aggregate_xferflags(&xfer->flags);
  452         up->up_flags = htole32(temp);
  453         temp = usbpf_aggregate_status(&xfer->flags_int);
  454         up->up_status = htole32(temp);
  455         temp = xfer->error;
  456         up->up_error = htole32(temp);
  457         temp = xfer->interval;
  458         up->up_interval = htole32(temp);
  459         up->up_frames = htole32(nframes);
  460         temp = xfer->max_packet_size;
  461         up->up_packet_size = htole32(temp);
  462         temp = xfer->max_packet_count;
  463         up->up_packet_count = htole32(temp);
  464         temp = xfer->endpointno;
  465         up->up_endpoint = htole32(temp);
  466         up->up_speed = xfer->xroot->udev->speed;
  467 
  468         /* clear reserved area */
  469         memset(up->up_reserved, 0, sizeof(up->up_reserved));
  470 
  471         /* init offset and frame */
  472         offset = 0;
  473         frame = 0;
  474 
  475         /* iterate all the USB frames and copy data, if any */
  476         for (x = 0; x != nframes; x++) {
  477                 uint32_t length;
  478                 int isread;
  479 
  480                 /* get length */
  481                 length = xfer->frlengths[x];
  482 
  483                 /* get frame header pointer */
  484                 uf = (struct usbpf_framehdr *)ptr;
  485                 ptr += USBPF_FRAME_HDR_LEN;
  486 
  487                 /* fill out packet header */
  488                 uf->length = htole32(length);
  489                 uf->flags = 0;
  490 
  491                 /* get information about data read/write */
  492                 isread = usbpf_xfer_frame_is_read(xfer, x);
  493 
  494                 /* check if we need to copy any data */
  495                 if (isread) {
  496                         if (type == USBPF_XFERTAP_SUBMIT)
  497                                 length = 0;
  498                         else {
  499                                 uf->flags |= htole32(
  500                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
  501                         }
  502                 } else {
  503                         if (type != USBPF_XFERTAP_SUBMIT)
  504                                 length = 0;
  505                         else {
  506                                 uf->flags |= htole32(
  507                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
  508                         }
  509                 }
  510 
  511                 /* check if data is read direction */
  512                 if (isread)
  513                         uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
  514 
  515                 /* copy USB data, if any */
  516                 if (length != 0) {
  517                         /* copy data */
  518                         usbd_copy_out(&xfer->frbuffers[frame],
  519                             offset, ptr, length);
  520 
  521                         /* align length */
  522                         temp = USBPF_FRAME_ALIGN(length);
  523 
  524                         /* zero pad */
  525                         if (temp != length)
  526                                 memset(ptr + length, 0, temp - length);
  527 
  528                         ptr += temp;
  529                 }
  530 
  531                 if (xfer->flags_int.isochronous_xfr) {
  532                         offset += usbd_xfer_old_frame_length(xfer, x);
  533                 } else {
  534                         frame ++;
  535                 }
  536         }
  537 
  538         bpf_tap(bus->ifp->if_bpf, buf, totlen);
  539 
  540         free(buf, M_TEMP);
  541 }

Cache object: 4100603e522c36c8dc5f4398902a9b1e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.