summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Batard <pbatard@gmail.com>2011-02-21 18:41:38 +0000
committerPete Batard <pbatard@gmail.com>2011-02-21 18:41:38 +0000
commitd47ed6ce6c9fb24d45f5b69a681bc46c9f903fe9 (patch)
treee42b470931a0c54c84eda9efe4e04d8decbab155
parent99e9ca398591c413f85fc909efaeabbe5489d099 (diff)
downloadlibusb-d47ed6ce6c9fb24d45f5b69a681bc46c9f903fe9.tar.gz
different approach to topology retrieval
* uses get_port, get_port_path, get_parent as suggested by Alan Stern * adds parent_dev and port_number to the libusb device struct * uses calloc on device struct, to handle currently missing backend implementations * xusb updated to use new calls
-rw-r--r--examples/xusb.c12
-rw-r--r--libusb/core.c73
-rw-r--r--libusb/libusb-1.0.def40
-rw-r--r--libusb/libusb.h19
-rw-r--r--libusb/libusbi.h10
-rw-r--r--libusb/os/darwin_usb.c5
-rw-r--r--libusb/os/linux_usbfs.c7
-rw-r--r--libusb/os/windows_usb.c14
8 files changed, 96 insertions, 84 deletions
diff --git a/examples/xusb.c b/examples/xusb.c
index 0ae896e..b1bfa39 100644
--- a/examples/xusb.c
+++ b/examples/xusb.c
@@ -605,7 +605,7 @@ int test_device(uint16_t vid, uint16_t pid)
{
libusb_device_handle *handle;
libusb_device *dev;
- struct libusb_device_topology topology;
+ uint8_t bus, port_path[8];
struct libusb_config_descriptor *conf_desc;
const struct libusb_endpoint_descriptor *endpoint;
int i, j, k, r;
@@ -629,8 +629,14 @@ int test_device(uint16_t vid, uint16_t pid)
}
dev = libusb_get_device(handle);
- if (libusb_get_device_topology(dev, &topology) == LIBUSB_SUCCESS) {
- printf("bus: %d, port: %d, depth: %d\n", topology.bus, topology.port, topology.depth);
+ bus = libusb_get_bus_number(dev);
+ r = libusb_get_port_path(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++) {
+ printf("->%d", port_path[i]);
+ }
+ printf("\n");
}
printf("\nReading device descriptor:\n");
diff --git a/libusb/core.c b/libusb/core.c
index 5ad5809..0e9f357 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -510,7 +510,7 @@ struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
unsigned long session_id)
{
size_t priv_size = usbi_backend->device_priv_size;
- struct libusb_device *dev = malloc(sizeof(*dev) + priv_size);
+ struct libusb_device *dev = calloc(1, sizeof(*dev) + priv_size);
int r;
if (!dev)
@@ -673,6 +673,55 @@ uint8_t API_EXPORTED libusb_get_bus_number(libusb_device *dev)
}
/** \ingroup dev
+ * Get the number of the port that a device is connected to
+ * \param dev a device
+ * \returns the port number (0 if not available)
+ */
+uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev)
+{
+ return dev->port_number;
+}
+
+/** \ingroup dev
+ * Get the list of all port numbers from root for the specified device
+ * \param dev a device
+ * \param path the array that should contain the port numbers
+ * \param path_len the maximum length of the array
+ * \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 i = path_len;
+
+ while(dev) {
+ // HCDs can be listed as devices and would have port #0
+ // TODO: see how the other backends want to implement HCDs as parents
+ if (dev->port_number == 0)
+ break;
+ usbi_dbg("another one (addy: %d)", dev->device_address);
+ if (--i<0) {
+ return LIBUSB_ERROR_OVERFLOW;
+ }
+ path[i] = dev->port_number;
+ dev = dev->parent_dev;
+ }
+ memmove(path, &path[i], path_len-i);
+ return path_len-i;
+}
+
+/** \ingroup dev
+ * Get the the parent from the specified device
+ * \param dev a device
+ * \returns the device parent or NULL if not available
+ */
+DEFAULT_VISIBILITY
+libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev)
+{
+ return dev->parent_dev;
+}
+
+/** \ingroup dev
* Get the address of the device on the bus it is connected to.
* \param dev a device
* \returns the device address
@@ -1454,28 +1503,6 @@ int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev,
return LIBUSB_ERROR_NOT_SUPPORTED;
}
-/** \ingroup dev
- * Returns topology information for a device
- *
- * \param dev the device to get topology properties from
- * \param topology the libusb_device_topology structure to be populated.
- * \returns 0 on success
- * \returns LIBUSB_ERROR_INVALID_PARAM if dev or topology are invalid
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
- * is not available
- * \returns another LIBUSB_ERROR code on other failure
- */
-
-int API_EXPORTED libusb_get_device_topology(libusb_device *dev,
- struct libusb_device_topology* topology)
-{
- usbi_dbg("");
- if (dev == NULL || topology == NULL) {
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- return usbi_backend->get_device_topology(dev, topology);
-}
-
/** \ingroup lib
* Set message verbosity.
* - Level 0: no messages ever printed by the library (default)
diff --git a/libusb/libusb-1.0.def b/libusb/libusb-1.0.def
index 96c6ca6..d437796 100644
--- a/libusb/libusb-1.0.def
+++ b/libusb/libusb-1.0.def
@@ -240,16 +240,6 @@ EXPORTS
libusb_get_device_list@24 = libusb_get_device_list
libusb_get_device_list@28 = libusb_get_device_list
libusb_get_device_list@32 = libusb_get_device_list
- libusb_get_device_topology
- libusb_get_device_topology@00 = libusb_get_device_topology
- libusb_get_device_topology@04 = libusb_get_device_topology
- libusb_get_device_topology@08 = libusb_get_device_topology
- libusb_get_device_topology@12 = libusb_get_device_topology
- libusb_get_device_topology@16 = libusb_get_device_topology
- libusb_get_device_topology@20 = libusb_get_device_topology
- libusb_get_device_topology@24 = libusb_get_device_topology
- libusb_get_device_topology@28 = libusb_get_device_topology
- libusb_get_device_topology@32 = libusb_get_device_topology
libusb_get_max_iso_packet_size
libusb_get_max_iso_packet_size@00 = libusb_get_max_iso_packet_size
libusb_get_max_iso_packet_size@04 = libusb_get_max_iso_packet_size
@@ -280,6 +270,16 @@ EXPORTS
libusb_get_next_timeout@24 = libusb_get_next_timeout
libusb_get_next_timeout@28 = libusb_get_next_timeout
libusb_get_next_timeout@32 = libusb_get_next_timeout
+ libusb_get_parent
+ libusb_get_parent@00 = libusb_get_parent
+ libusb_get_parent@04 = libusb_get_parent
+ libusb_get_parent@08 = libusb_get_parent
+ libusb_get_parent@12 = libusb_get_parent
+ libusb_get_parent@16 = libusb_get_parent
+ libusb_get_parent@20 = libusb_get_parent
+ libusb_get_parent@24 = libusb_get_parent
+ libusb_get_parent@28 = libusb_get_parent
+ libusb_get_parent@32 = libusb_get_parent
libusb_get_pollfds
libusb_get_pollfds@00 = libusb_get_pollfds
libusb_get_pollfds@04 = libusb_get_pollfds
@@ -290,6 +290,26 @@ EXPORTS
libusb_get_pollfds@24 = libusb_get_pollfds
libusb_get_pollfds@28 = libusb_get_pollfds
libusb_get_pollfds@32 = libusb_get_pollfds
+ libusb_get_port_number
+ libusb_get_port_number@00 = libusb_get_port_number
+ libusb_get_port_number@04 = libusb_get_port_number
+ libusb_get_port_number@08 = libusb_get_port_number
+ libusb_get_port_number@12 = libusb_get_port_number
+ libusb_get_port_number@16 = libusb_get_port_number
+ libusb_get_port_number@20 = libusb_get_port_number
+ libusb_get_port_number@24 = libusb_get_port_number
+ libusb_get_port_number@28 = libusb_get_port_number
+ libusb_get_port_number@32 = libusb_get_port_number
+ libusb_get_port_path
+ libusb_get_port_path@00 = libusb_get_port_path
+ libusb_get_port_path@04 = libusb_get_port_path
+ libusb_get_port_path@08 = libusb_get_port_path
+ libusb_get_port_path@12 = libusb_get_port_path
+ libusb_get_port_path@16 = libusb_get_port_path
+ libusb_get_port_path@20 = libusb_get_port_path
+ libusb_get_port_path@24 = libusb_get_port_path
+ libusb_get_port_path@28 = libusb_get_port_path
+ libusb_get_port_path@32 = libusb_get_port_path
libusb_get_string_descriptor_ascii
libusb_get_string_descriptor_ascii@00 = libusb_get_string_descriptor_ascii
libusb_get_string_descriptor_ascii@04 = libusb_get_string_descriptor_ascii
diff --git a/libusb/libusb.h b/libusb/libusb.h
index a89e326..ba3d5f5 100644
--- a/libusb/libusb.h
+++ b/libusb/libusb.h
@@ -675,19 +675,6 @@ typedef struct libusb_device libusb_device;
*/
typedef struct libusb_device_handle libusb_device_handle;
-/** \ingroup dev
- * Structure representing the topology of an USB device.
- */
-struct libusb_device_topology {
- /** Opaque device handle to the USB parent (a Hub or a HCD) */
- libusb_device* parent_dev;
- /** Bus number to which the device is connected, as seen by the OS */
- uint8_t bus;
- /** Depth to HCD for this bus (0 depth means the HCD device) */
- uint8_t depth;
- /** Hub port onto which the device is plugged in, as seen by the OS */
- uint8_t port;
-};
/** \ingroup misc
* Error codes. Most libusb functions return 0 on success or one of these
@@ -901,6 +888,9 @@ int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
void LIBUSB_CALL libusb_free_config_descriptor(
struct libusb_config_descriptor *config);
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);
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);
@@ -934,9 +924,6 @@ int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
int interface_number);
-int LIBUSB_CALL libusb_get_device_topology(struct libusb_device *dev,
- struct libusb_device_topology *topology);
-
/* async I/O */
/** \ingroup asyncio
diff --git a/libusb/libusbi.h b/libusb/libusbi.h
index caceb65..8ade9c7 100644
--- a/libusb/libusbi.h
+++ b/libusb/libusbi.h
@@ -271,6 +271,8 @@ struct libusb_device {
struct libusb_context *ctx;
uint8_t bus_number;
+ uint8_t port_number;
+ struct libusb_device* parent_dev;
uint8_t device_address;
uint8_t num_configurations;
@@ -761,14 +763,6 @@ struct usbi_os_backend {
int (*attach_kernel_driver)(struct libusb_device_handle *handle,
int interface_number);
- /* Return device topology. Optional.
- *
- * This function is called to populate a libusb_device_topology structure,
- * that allows to uniquely identify the location of a device on the system.
- */
- int (*get_device_topology)(struct libusb_device *dev,
- struct libusb_device_topology* topology);
-
/* Destroy a device. Optional.
*
* This function is called when the last reference to a device is
diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c
index 163439d..37b8e2c 100644
--- a/libusb/os/darwin_usb.c
+++ b/libusb/os/darwin_usb.c
@@ -1097,10 +1097,6 @@ static int darwin_detach_kernel_driver (struct libusb_device_handle *dev_handle,
return LIBUSB_ERROR_NOT_SUPPORTED;
}
-static int darwin_get_device_topology(struct libusb_device *dev, struct libusb_device_topology* topology) {
- return LIBUSB_ERROR_NOT_SUPPORTED;
-}
-
static void darwin_destroy_device(struct libusb_device *dev) {
}
@@ -1539,7 +1535,6 @@ const struct usbi_os_backend darwin_backend = {
.detach_kernel_driver = darwin_detach_kernel_driver,
.attach_kernel_driver = darwin_attach_kernel_driver,
- .get_device_topology = darwin_get_device_topology,
.destroy_device = darwin_destroy_device,
.submit_transfer = darwin_submit_transfer,
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c
index 9f9b140..867893c 100644
--- a/libusb/os/linux_usbfs.c
+++ b/libusb/os/linux_usbfs.c
@@ -1318,12 +1318,6 @@ static int op_attach_kernel_driver(struct libusb_device_handle *handle,
return 0;
}
-static int op_get_device_topology(struct libusb_device *dev,
- struct libusb_device_topology* topology)
-{
- return LIBUSB_ERROR_NOT_SUPPORTED;
-}
-
static void op_destroy_device(struct libusb_device *dev)
{
struct linux_device_priv *priv = __device_priv(dev);
@@ -2232,7 +2226,6 @@ const struct usbi_os_backend linux_usbfs_backend = {
.detach_kernel_driver = op_detach_kernel_driver,
.attach_kernel_driver = op_attach_kernel_driver,
- .get_device_topology = op_get_device_topology,
.destroy_device = op_destroy_device,
.submit_transfer = op_submit_transfer,
diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c
index 2435f76..b51dad0 100644
--- a/libusb/os/windows_usb.c
+++ b/libusb/os/windows_usb.c
@@ -1074,8 +1074,10 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
}
dev->bus_number = parent_dev->bus_number;
priv->port = port_number;
+ dev->port_number = port_number;
priv->depth = parent_priv->depth + 1;
priv->parent_dev = parent_dev;
+ dev->parent_dev = parent_dev;
memset(&conn_info, 0, sizeof(conn_info));
if (priv->depth != 0) { // Not a HCD hub
@@ -1845,17 +1847,6 @@ static void windows_destroy_device(struct libusb_device *dev)
windows_device_priv_release(dev);
}
-static int windows_get_device_topology(struct libusb_device *dev, struct libusb_device_topology* topology)
-{
- struct windows_device_priv *priv = __device_priv(dev);
-
- topology->bus = dev->bus_number;
- topology->depth = priv->depth;
- topology->parent_dev = priv->parent_dev;
- topology->port = priv->port;
- return LIBUSB_SUCCESS;
-}
-
static void windows_clear_transfer_priv(struct usbi_transfer *itransfer)
{
struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
@@ -2247,7 +2238,6 @@ const struct usbi_os_backend windows_backend = {
windows_detach_kernel_driver,
windows_attach_kernel_driver,
- windows_get_device_topology,
windows_destroy_device,
windows_submit_transfer,