[PATCH dfu-util] main: Make descriptor helper functions more generic

Tormod Volden lists.tormod at gmail.com
Sat Sep 24 13:48:42 CEST 2011


From: Tormod Volden <debian.tormod at gmail.com>

For the sake of reusability, do not refer to our dfu_if
structures. This also makes it more clear what these functions
are acting upon. Rename these functions slightly.

Signed-off-by: Tormod Volden <debian.tormod at gmail.com>
---

As you can see we use on the active configuration in some cases,
for those functions which talk to the device and hence are in the
scope of a chosen configuration.

In other cases we are just using information parsed from descriptors
so we don't need to talk to a configured device.

Working on this, I noticed that we do not at all support choosing
the configuration. The -c option does nothing, and there are no
checks to see if the device is in the configuration needed for
DFU. This is fine as long as all our supported devices has only
one configuration, or the default is the right one.

Note that in DFU mode there should be only one configuration anyway,
so the configuration selection is for the run-time mode. This should
be made clear in the user documentation. Similar for the interface
selection, in DFU mode there should be only one interface. So the
selection should act in run-time mode only, to specify which interface
to talk to get into DFU mode. Not that I expect many devices to have
several DFU interfaces though.

The alt setting on the other hand, should happen in DFU mode, like
we do now. But we should not use it to filter run-time devices.

So I will fix that up one day but at lower priority.

Cheers,
Tormod


 src/main.c |   62 ++++++++++++++++++++++++++++++------------------------------
 1 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/src/main.c b/src/main.c
index 1431364..3eed7e6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -431,22 +431,19 @@ static int find_descriptor(const unsigned char *desc_list, int list_len,
 /* Look for a descriptor in the active configuration
  * Will also find extra descriptors which are normally
  * not returned by the standard libusb_get_descriptor() */
-static int usb_get_any_descriptor(struct dfu_if *dfu_if,
-				    uint8_t desc_type,
-				    uint8_t desc_index,
-				    unsigned char *resbuf, int res_len)
+static int usb_get_any_descriptor(struct libusb_device_handle *dev_handle,
+				  uint8_t desc_type,
+				  uint8_t desc_index,
+				  unsigned char *resbuf, int res_len)
 {
+	struct libusb_device *dev = libusb_get_device(dev_handle);
 	struct libusb_config_descriptor *config;
 	int ret;
 	uint16_t conflen;
 	unsigned char *cbuf;
 
-	/* We need to talk to the device, so it must be open */
-	if (!dfu_if->dev_handle)
-		return -1;
-
 	/* Get the total length of the configuration descriptors */
-	ret = libusb_get_active_config_descriptor(dfu_if->dev, &config);
+	ret = libusb_get_active_config_descriptor(dev, &config);
 	if (ret == LIBUSB_ERROR_NOT_FOUND) {
 		fprintf(stderr, "Error: Device is unconfigured\n");
 		return -1;
@@ -460,7 +457,7 @@ static int usb_get_any_descriptor(struct dfu_if *dfu_if,
 
 	/* Suck in the configuration descriptor list from device */
 	cbuf = malloc(conflen);
-	ret = libusb_get_descriptor(dfu_if->dev_handle, LIBUSB_DT_CONFIG,
+	ret = libusb_get_descriptor(dev_handle, LIBUSB_DT_CONFIG,
 				    desc_index, cbuf, conflen);
 	if (ret < conflen) {
 		fprintf(stderr, "Warning: failed to retrieve complete "
@@ -483,15 +480,17 @@ static int usb_get_any_descriptor(struct dfu_if *dfu_if,
 
 	/* Finally try to retrieve it requesting the device directly
 	 * This is not supported on all devices for non-standard types */
-	return libusb_get_descriptor(dfu_if->dev_handle, desc_type, desc_index,
+	return libusb_get_descriptor(dev_handle, desc_type, desc_index,
 				     resbuf, res_len);
 }
 
-/* Get cached extra descriptor from libusb, from active configuration
+/* Get cached extra descriptor from libusb for an interface
  * Returns length of found descriptor */
-static int get_cached_extra_descriptor(struct dfu_if *dfu_if,
-				 uint8_t desc_type, uint8_t desc_index,
-				 unsigned char *resbuf, int res_len)
+static int get_cached_extra_descriptor(struct libusb_device *dev,
+				       uint8_t bConfValue,
+				       uint8_t intf,
+				       uint8_t desc_type, uint8_t desc_index,
+				       unsigned char *resbuf, int res_len)
 {
 	struct libusb_config_descriptor *cfg;
 	const unsigned char *extra;
@@ -499,13 +498,13 @@ static int get_cached_extra_descriptor(struct dfu_if *dfu_if,
 	int ret;
 	int alt;
 
-	ret = libusb_get_active_config_descriptor(dfu_if->dev, &cfg);
+	ret = libusb_get_config_descriptor_by_value(dev, bConfValue, &cfg);
 	if (ret == LIBUSB_ERROR_NOT_FOUND) {
 		fprintf(stderr, "Error: Device is unconfigured\n");
 		return -1;
 	} else if (ret) {
 		fprintf(stderr, "Error: failed "
-			"libusb_get_active_config_descriptor()\n");
+			"libusb_config_descriptor_by_value()\n");
 		exit(1);
 	}
 
@@ -513,12 +512,10 @@ static int get_cached_extra_descriptor(struct dfu_if *dfu_if,
 	 * libusb may attach them to one setting. Therefore go through all.
 	 * Note that desc_index is per alternate setting, hits will not be
 	 * counted from one to another */
-	for (alt = 0; alt < cfg->interface[dfu_if->interface].num_altsetting;
+	for (alt = 0; alt < cfg->interface[intf].num_altsetting;
 	     alt++) {
-		extra = cfg->interface[dfu_if->interface].
-				altsetting[alt].extra;
-		extra_len = cfg->interface[dfu_if->interface].
-				altsetting[alt].extra_length;
+		extra = cfg->interface[intf].altsetting[alt].extra;
+		extra_len = cfg->interface[intf].altsetting[alt].extra_length;
 		if (extra_len > 1)
 			ret = find_descriptor(extra, extra_len, desc_type,
 					      desc_index, resbuf, res_len);
@@ -764,9 +761,10 @@ int main(int argc, char **argv)
 
 	/* Obtain run-time DFU functional descriptor without asking device
 	 * E.g. Freerunner does not like to be requested at this point */
-	ret = get_cached_extra_descriptor(&_rt_dif, USB_DT_DFU, 0,
-				    (unsigned char *) &func_dfu_rt,
-				    sizeof(func_dfu_rt));
+	ret = get_cached_extra_descriptor(_rt_dif.dev, _rt_dif.configuration,
+					  _rt_dif.interface, USB_DT_DFU, 0,
+					  (unsigned char *) &func_dfu_rt,
+					  sizeof(func_dfu_rt));
 	if (ret == 7) {
 		/* DFU 1.0 does not have this field */
 		printf("Deducing device DFU version from functional descriptor "
@@ -990,15 +988,17 @@ status_again:
 
 	/* Get the DFU mode DFU functional descriptor
 	 * If it is not found cached, we will request it from the device */
-	ret = get_cached_extra_descriptor(dif, USB_DT_DFU, 0, 
-				    (unsigned char *) &func_dfu,
-				    sizeof(func_dfu));
+	ret = get_cached_extra_descriptor(dif->dev, dif->configuration,
+					  dif->interface, USB_DT_DFU, 0,
+					  (unsigned char *) &func_dfu,
+					  sizeof(func_dfu));
 	if (ret < 7) {
 		fprintf(stderr, "Error obtaining cached DFU functional "
 			"descriptor\n");
-		ret = usb_get_any_descriptor(dif, USB_DT_DFU, 0,
-				    (unsigned char *) &func_dfu,
-				    sizeof(func_dfu));
+		ret = usb_get_any_descriptor(dif->dev_handle,
+					     USB_DT_DFU, 0,
+					     (unsigned char *) &func_dfu,
+					     sizeof(func_dfu));
 	}
 	if (ret == 7) {
 		printf("Deducing device DFU version from functional descriptor "
-- 
1.7.5.4




More information about the devel mailing list