<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/libosmocore/+/16928">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">usb: Add osmo_libusb_find_open_claim() all-in-one API<br><br>This function offers the highest level of API among all libosmousb<br>helper functions. It is intended as a one-stop shop for everything<br>related to grabbing an interface.<br><br>Change-Id: I748ded6cc7b73a73625588bd7a34a017a905b6bf<br>---<br>M include/osmocom/usb/libusb.h<br>M src/usb/osmo_libusb.c<br>2 files changed, 126 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/28/16928/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/usb/libusb.h b/include/osmocom/usb/libusb.h</span><br><span>index 2220e03..9ad3f71 100644</span><br><span>--- a/include/osmocom/usb/libusb.h</span><br><span>+++ b/include/osmocom/usb/libusb.h</span><br><span>@@ -55,6 +55,31 @@</span><br><span>      uint8_t string_idx;</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Description of the USB device+interface we're looking for */</span><br><span style="color: hsl(120, 100%, 40%);">+struct osmo_usb_matchspec {</span><br><span style="color: hsl(120, 100%, 40%);">+      /*! specify the USB device */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct {</span><br><span style="color: hsl(120, 100%, 40%);">+              int vendor_id;  /*!< typically -1 for compile time defaults */</span><br><span style="color: hsl(120, 100%, 40%);">+             int product_id; /*!< typically -1 for compile time defaults */</span><br><span style="color: hsl(120, 100%, 40%);">+             char *path;     /*!< used for disambiguation when multiple matches; can be NULL */</span><br><span style="color: hsl(120, 100%, 40%);">+ } dev;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /*! specify the USB configuration */</span><br><span style="color: hsl(120, 100%, 40%);">+  int config_id;          /*!< typically -1 unless user selects specific configuration */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /*! specify the USB interface */</span><br><span style="color: hsl(120, 100%, 40%);">+      struct {</span><br><span style="color: hsl(120, 100%, 40%);">+              /* typically those three are set to application defaults */</span><br><span style="color: hsl(120, 100%, 40%);">+           int class;      /*!< -1 or a user-specified class */</span><br><span style="color: hsl(120, 100%, 40%);">+               int subclass;   /*!< -1 or a user-specified subclass */</span><br><span style="color: hsl(120, 100%, 40%);">+            int proto;      /*!< -1 or a user-specified protocol */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          /* typically those two are -1; but user can override them */</span><br><span style="color: hsl(120, 100%, 40%);">+          int num;</span><br><span style="color: hsl(120, 100%, 40%);">+              int altsetting;</span><br><span style="color: hsl(120, 100%, 40%);">+       } intf;</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> </span><br><span> char *osmo_libusb_dev_get_path_buf(char *buf, size_t bufsize, libusb_device *dev);</span><br><span> char *osmo_libusb_dev_get_path_c(void *ctx, libusb_device *dev);</span><br><span>@@ -81,6 +106,11 @@</span><br><span> libusb_device_handle *osmo_libusb_open_claim_interface(void *ctx, libusb_context *luctx,</span><br><span>                                                        const struct usb_interface_match *ifm);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_libusb_match_init(struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+libusb_device_handle *osmo_libusb_find_open_claim(const struct osmo_usb_matchspec *cfg,</span><br><span style="color: hsl(120, 100%, 40%);">+                                               const struct dev_id *default_dev_ids);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int osmo_libusb_get_ep_addrs(libusb_device_handle *devh, unsigned int if_num,</span><br><span>                       uint8_t *out, uint8_t *in, uint8_t *irq);</span><br><span> </span><br><span>diff --git a/src/usb/osmo_libusb.c b/src/usb/osmo_libusb.c</span><br><span>index 7a13a7e..bfc67e6 100644</span><br><span>--- a/src/usb/osmo_libusb.c</span><br><span>+++ b/src/usb/osmo_libusb.c</span><br><span>@@ -587,6 +587,102 @@</span><br><span>  return usb_devh;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_libusb_match_init(struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        cfg->dev.vendor_id = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+   cfg->dev.product_id = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+  cfg->dev.path = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    cfg->config_id = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     cfg->intf.class = if_class;</span><br><span style="color: hsl(120, 100%, 40%);">+        cfg->intf.subclass = if_subclass;</span><br><span style="color: hsl(120, 100%, 40%);">+  cfg->intf.proto = if_proto;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      cfg->intf.num = cfg->intf.altsetting = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! high-level all-in-one function for USB device, config + interface matching + opening.</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function offers the highest level of API among all libosmousb helper functions. It</span><br><span style="color: hsl(120, 100%, 40%);">+ * is intended as a one-stop shop for everything related to grabbing an interface.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *   1) looks for a device matching either the VID/PID from 'cfg' or 'default_dev_ids',</span><br><span style="color: hsl(120, 100%, 40%);">+ *      if more than one is found, the user is expected to fill in cfg->dev.path to disambiguate.</span><br><span style="color: hsl(120, 100%, 40%);">+ *   2) find any interfaces on the device that match the specification in 'cfg'. The match</span><br><span style="color: hsl(120, 100%, 40%);">+ *      could be done based on any of (class, subclass, proto, interface number).  If there</span><br><span style="color: hsl(120, 100%, 40%);">+ *      are multiple matches, the caller must disambiguate by specifying the interface number.</span><br><span style="color: hsl(120, 100%, 40%);">+ *   3) open the USB device; set the configuration (if needed); claim the interface and set</span><br><span style="color: hsl(120, 100%, 40%);">+ *      the altsetting</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] cfg user-supplied match configuration (from command line or config file)</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] default_dev_ids Default list of supported VendorId/ProductIds</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \returns libusb_device_handle on success, NULL on error</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+libusb_device_handle *osmo_libusb_find_open_claim(const struct osmo_usb_matchspec *cfg,</span><br><span style="color: hsl(120, 100%, 40%);">+                                               const struct dev_id *default_dev_ids)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct usb_interface_match if_matches[16];</span><br><span style="color: hsl(120, 100%, 40%);">+    struct usb_interface_match *ifm = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+       libusb_device_handle *usb_devh = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+        struct dev_id user_dev_ids[2] = {</span><br><span style="color: hsl(120, 100%, 40%);">+             { cfg->dev.vendor_id, cfg->dev.product_id },</span><br><span style="color: hsl(120, 100%, 40%);">+            { 0, 0 }</span><br><span style="color: hsl(120, 100%, 40%);">+      };</span><br><span style="color: hsl(120, 100%, 40%);">+    const struct dev_id *dev_ids = default_dev_ids;</span><br><span style="color: hsl(120, 100%, 40%);">+       libusb_device *dev;</span><br><span style="color: hsl(120, 100%, 40%);">+   int rc, i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /* Stage 1: Find a device matching either the user-specified VID/PID or</span><br><span style="color: hsl(120, 100%, 40%);">+        * the list of IDs in default_dev_ids plus optionally the user-specified path */</span><br><span style="color: hsl(120, 100%, 40%);">+      if (cfg->dev.vendor_id != -1 || cfg->dev.product_id != -1)</span><br><span style="color: hsl(120, 100%, 40%);">+              dev_ids = user_dev_ids;</span><br><span style="color: hsl(120, 100%, 40%);">+       dev = osmo_libusb_find_matching_dev_path(NULL, dev_ids, cfg->dev.path);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!dev)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto close_exit;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    /* Stage 2: Find any interfaces matching the class/subclass/proto as specified */</span><br><span style="color: hsl(120, 100%, 40%);">+     rc = osmo_libusb_dev_find_matching_interfaces(dev, cfg->intf.class, cfg->intf.subclass,</span><br><span style="color: hsl(120, 100%, 40%);">+                                               cfg->intf.proto, if_matches, sizeof(if_matches));</span><br><span style="color: hsl(120, 100%, 40%);">+    if (rc < 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+              LOGP(DLUSB, LOGL_NOTICE, "can't find matching USB interface at device\n");</span><br><span style="color: hsl(120, 100%, 40%);">+              goto close_exit;</span><br><span style="color: hsl(120, 100%, 40%);">+      } else if (rc == 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+         ifm = if_matches;</span><br><span style="color: hsl(120, 100%, 40%);">+     } else if (rc > 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+               if (cfg->intf.num == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 LOGP(DLUSB, LOGL_ERROR, "Found %d matching USB interfaces, you "</span><br><span style="color: hsl(120, 100%, 40%);">+                            "have to specify the interface number\n", rc);</span><br><span style="color: hsl(120, 100%, 40%);">+                      goto close_exit;</span><br><span style="color: hsl(120, 100%, 40%);">+              }</span><br><span style="color: hsl(120, 100%, 40%);">+             for (i = 0; i < rc; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (if_matches[i].interface == cfg->intf.num) {</span><br><span style="color: hsl(120, 100%, 40%);">+                            ifm = &if_matches[i];</span><br><span style="color: hsl(120, 100%, 40%);">+                             break;</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span style="color: hsl(120, 100%, 40%);">+                     /* FIXME: match altsetting */</span><br><span style="color: hsl(120, 100%, 40%);">+         }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (!ifm) {</span><br><span style="color: hsl(120, 100%, 40%);">+           LOGP(DLUSB, LOGL_NOTICE, "Couldn't find matching interface\n");</span><br><span style="color: hsl(120, 100%, 40%);">+         goto close_exit;</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Stage 3: Open device; set config (if required); claim interface; set altsetting */</span><br><span style="color: hsl(120, 100%, 40%);">+ usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!usb_devh) {</span><br><span style="color: hsl(120, 100%, 40%);">+              LOGP(DLUSB, LOGL_ERROR, "can't open USB device (permissions issue?)\n");</span><br><span style="color: hsl(120, 100%, 40%);">+                goto close_exit;</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+     return usb_devh;</span><br><span style="color: hsl(120, 100%, 40%);">+close_exit:</span><br><span style="color: hsl(120, 100%, 40%);">+ /* release if_matches */</span><br><span style="color: hsl(120, 100%, 40%);">+      if (usb_devh)</span><br><span style="color: hsl(120, 100%, 40%);">+         libusb_close(usb_devh);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! obtain the endpoint addresses for a given USB interface.</span><br><span>  *  \param[in] devh USB device handle on which to operate</span><br><span>  *  \param[in] if_num USB Interface number on which to operate</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/libosmocore/+/16928">change 16928</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/libosmocore/+/16928"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I748ded6cc7b73a73625588bd7a34a017a905b6bf </div>
<div style="display:none"> Gerrit-Change-Number: 16928 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>