summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Batard <pbatard@gmail.com>2011-02-22 15:44:00 +0000
committerPete Batard <pbatard@gmail.com>2011-02-22 15:55:49 +0000
commitad42c29145970d262fea026a736d0f7d0e89b4c8 (patch)
treea8707c626ba3beaad52d3f0d006c7605610cb498
parentfcfe54ebbf4c5dae88ab7111301466d239f1e36b (diff)
downloadlibusb-ad42c29145970d262fea026a736d0f7d0e89b4c8.tar.gz
fixed libusb_get_port_path using zombie parents
* add a call to libusb_get_device_list to ensure the parents exist * modified API as a context is required for get_device_list
-rw-r--r--examples/xusb.c2
-rw-r--r--libusb/core.c10
-rw-r--r--libusb/libusb.h2
3 files changed, 11 insertions, 3 deletions
diff --git a/examples/xusb.c b/examples/xusb.c
index b1bfa39..ed69f5a 100644
--- a/examples/xusb.c
+++ b/examples/xusb.c
@@ -630,7 +630,7 @@ int test_device(uint16_t vid, uint16_t pid)
dev = libusb_get_device(handle);
bus = libusb_get_bus_number(dev);
- r = libusb_get_port_path(dev, port_path, sizeof(port_path));
+ r = libusb_get_port_path(NULL, dev, port_path, sizeof(port_path));
if (r > 0) {
printf("bus: %d, port path from HCD: %d", bus, port_path[0]);
for (i=1; i<r; i++) {
diff --git a/libusb/core.c b/libusb/core.c
index 0e9f357..f09c6cd 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -690,9 +690,16 @@ uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev)
* \returns the number of elements filled
* \returns LIBUSB_ERROR_OVERFLOW if the array is too small
*/
-int API_EXPORTED libusb_get_port_path(libusb_device *dev, uint8_t* path, uint8_t path_len)
+int API_EXPORTED libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_len)
{
int i = path_len;
+ ssize_t r;
+ struct libusb_device **devs;
+
+ /* The device needs to be open, else the parents may have been destroyed */
+ r = libusb_get_device_list(ctx, &devs);
+ if (r < 0)
+ return (int)r;
while(dev) {
// HCDs can be listed as devices and would have port #0
@@ -706,6 +713,7 @@ int API_EXPORTED libusb_get_port_path(libusb_device *dev, uint8_t* path, uint8_t
path[i] = dev->port_number;
dev = dev->parent_dev;
}
+ libusb_free_device_list(devs, 1);
memmove(path, &path[i], path_len-i);
return path_len-i;
}
diff --git a/libusb/libusb.h b/libusb/libusb.h
index ba3d5f5..4719dd1 100644
--- a/libusb/libusb.h
+++ b/libusb/libusb.h
@@ -890,7 +890,7 @@ void LIBUSB_CALL libusb_free_config_descriptor(
uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
-int LIBUSB_CALL libusb_get_port_path(libusb_device *dev, uint8_t* path, uint8_t path_length);
+int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
unsigned char endpoint);