Change in libosmocore[master]: usb: Add osmo_libusb_find_open_claim() all-in-one API

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat Jan 18 20:50:55 UTC 2020


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/16928 )


Change subject: usb: Add osmo_libusb_find_open_claim() all-in-one API
......................................................................

usb: Add osmo_libusb_find_open_claim() all-in-one API

This function offers the highest level of API among all libosmousb
helper functions. It is intended as a one-stop shop for everything
related to grabbing an interface.

Change-Id: I748ded6cc7b73a73625588bd7a34a017a905b6bf
---
M include/osmocom/usb/libusb.h
M src/usb/osmo_libusb.c
2 files changed, 126 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/28/16928/1

diff --git a/include/osmocom/usb/libusb.h b/include/osmocom/usb/libusb.h
index 2220e03..9ad3f71 100644
--- a/include/osmocom/usb/libusb.h
+++ b/include/osmocom/usb/libusb.h
@@ -55,6 +55,31 @@
 	uint8_t string_idx;
 };
 
+/*! Description of the USB device+interface we're looking for */
+struct osmo_usb_matchspec {
+	/*! specify the USB device */
+	struct {
+		int vendor_id;	/*!< typically -1 for compile time defaults */
+		int product_id;	/*!< typically -1 for compile time defaults */
+		char *path;	/*!< used for disambiguation when multiple matches; can be NULL */
+	} dev;
+
+	/*! specify the USB configuration */
+	int config_id;		/*!< typically -1 unless user selects specific configuration */
+
+	/*! specify the USB interface */
+	struct {
+		/* typically those three are set to application defaults */
+		int class;	/*!< -1 or a user-specified class */
+		int subclass;	/*!< -1 or a user-specified subclass */
+		int proto;	/*!< -1 or a user-specified protocol */
+
+		/* typically those two are -1; but user can override them */
+		int num;
+		int altsetting;
+	} intf;
+};
+
 
 char *osmo_libusb_dev_get_path_buf(char *buf, size_t bufsize, libusb_device *dev);
 char *osmo_libusb_dev_get_path_c(void *ctx, libusb_device *dev);
@@ -81,6 +106,11 @@
 libusb_device_handle *osmo_libusb_open_claim_interface(void *ctx, libusb_context *luctx,
 							const struct usb_interface_match *ifm);
 
+void osmo_libusb_match_init(struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto);
+
+libusb_device_handle *osmo_libusb_find_open_claim(const struct osmo_usb_matchspec *cfg,
+						  const struct dev_id *default_dev_ids);
+
 int osmo_libusb_get_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
 			     uint8_t *out, uint8_t *in, uint8_t *irq);
 
diff --git a/src/usb/osmo_libusb.c b/src/usb/osmo_libusb.c
index 7a13a7e..bfc67e6 100644
--- a/src/usb/osmo_libusb.c
+++ b/src/usb/osmo_libusb.c
@@ -587,6 +587,102 @@
 	return usb_devh;
 }
 
+void osmo_libusb_match_init(struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto)
+{
+	cfg->dev.vendor_id = -1;
+	cfg->dev.product_id = -1;
+	cfg->dev.path = NULL;
+
+	cfg->config_id = -1;
+
+	cfg->intf.class = if_class;
+	cfg->intf.subclass = if_subclass;
+	cfg->intf.proto = if_proto;
+
+	cfg->intf.num = cfg->intf.altsetting = -1;
+}
+
+
+/*! high-level all-in-one function for USB device, config + interface matching + opening.
+ * This function offers the highest level of API among all libosmousb helper functions. It
+ * is intended as a one-stop shop for everything related to grabbing an interface.
+ *
+ *   1) looks for a device matching either the VID/PID from 'cfg' or 'default_dev_ids',
+ *      if more than one is found, the user is expected to fill in cfg->dev.path to disambiguate.
+ *   2) find any interfaces on the device that match the specification in 'cfg'. The match
+ *      could be done based on any of (class, subclass, proto, interface number).  If there
+ *      are multiple matches, the caller must disambiguate by specifying the interface number.
+ *   3) open the USB device; set the configuration (if needed); claim the interface and set
+ *      the altsetting
+ *
+ *  \param[in] cfg user-supplied match configuration (from command line or config file)
+ *  \param[in] default_dev_ids Default list of supported VendorId/ProductIds
+ *  \returns libusb_device_handle on success, NULL on error
+ */
+libusb_device_handle *osmo_libusb_find_open_claim(const struct osmo_usb_matchspec *cfg,
+						  const struct dev_id *default_dev_ids)
+{
+	struct usb_interface_match if_matches[16];
+	struct usb_interface_match *ifm = NULL;
+	libusb_device_handle *usb_devh = NULL;
+	struct dev_id user_dev_ids[2] = {
+		{ cfg->dev.vendor_id, cfg->dev.product_id },
+		{ 0, 0 }
+	};
+	const struct dev_id *dev_ids = default_dev_ids;
+	libusb_device *dev;
+	int rc, i;
+
+	/* Stage 1: Find a device matching either the user-specified VID/PID or
+	 * the list of IDs in default_dev_ids plus optionally the user-specified path */
+	if (cfg->dev.vendor_id != -1 || cfg->dev.product_id != -1)
+		dev_ids = user_dev_ids;
+	dev = osmo_libusb_find_matching_dev_path(NULL, dev_ids, cfg->dev.path);
+	if (!dev)
+		goto close_exit;
+
+	/* Stage 2: Find any interfaces matching the class/subclass/proto as specified */
+	rc = osmo_libusb_dev_find_matching_interfaces(dev, cfg->intf.class, cfg->intf.subclass,
+						      cfg->intf.proto, if_matches, sizeof(if_matches));
+	if (rc < 1) {
+		LOGP(DLUSB, LOGL_NOTICE, "can't find matching USB interface at device\n");
+		goto close_exit;
+	} else if (rc == 1) {
+		ifm = if_matches;
+	} else if (rc > 1) {
+		if (cfg->intf.num == -1) {
+			LOGP(DLUSB, LOGL_ERROR, "Found %d matching USB interfaces, you "
+				"have to specify the interface number\n", rc);
+			goto close_exit;
+		}
+		for (i = 0; i < rc; i++) {
+			if (if_matches[i].interface == cfg->intf.num) {
+				ifm = &if_matches[i];
+				break;
+			}
+			/* FIXME: match altsetting */
+		}
+	}
+	if (!ifm) {
+		LOGP(DLUSB, LOGL_NOTICE, "Couldn't find matching interface\n");
+		goto close_exit;
+	}
+
+	/* Stage 3: Open device; set config (if required); claim interface; set altsetting */
+	usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
+	if (!usb_devh) {
+		LOGP(DLUSB, LOGL_ERROR, "can't open USB device (permissions issue?)\n");
+		goto close_exit;
+	}
+	return usb_devh;
+close_exit:
+	/* release if_matches */
+	if (usb_devh)
+		libusb_close(usb_devh);
+
+	return NULL;
+}
+
 /*! obtain the endpoint addresses for a given USB interface.
  *  \param[in] devh USB device handle on which to operate
  *  \param[in] if_num USB Interface number on which to operate

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/16928
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I748ded6cc7b73a73625588bd7a34a017a905b6bf
Gerrit-Change-Number: 16928
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200118/ecbf716b/attachment.htm>


More information about the gerrit-log mailing list