summaryrefslogtreecommitdiff
path: root/gusb
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2011-09-06 13:55:38 +0200
committerHans de Goede <hdegoede@redhat.com>2011-09-06 14:08:28 +0200
commit6a73aae262d98b7a2e99f03efe40f968abe5a35e (patch)
treec26da3d1c64075d78861e38ffdfbfe58bc15778d /gusb
parent4819d1362b550d88afcbdb1bd35704c34bf619ef (diff)
downloadgusb-6a73aae262d98b7a2e99f03efe40f968abe5a35e.tar.gz
gusb-device: drop g_usb_device_get_descriptor
We are using the contents of the descriptor in various log / GError messages, so just make sure we have it from the start. We can do this since on windows and Mac OS X libusb_get_device_descriptor never fails, and on Linux it will only fail if the device was unplugged since enumerating, in which case a g_warning will be printed. Since we create the GUsbDevice on a udev event, the chances of it being unplugged again already are very very small. And in case it is, we will simply end up with all 0's in the device descriptor, returning 0x0000 for vid/pid just like before. In the future this means we will also return 0 for the number of available configs, which is a problem for apps who actually want to try to do something with descriptors, but given that this only happens on unplugged devices it is once more not a real issue since any (to be implemented) g_usb_device_get_config_descriptor would fail on an unplugged device anyways. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Diffstat (limited to 'gusb')
-rw-r--r--gusb/gusb-device-list.c5
-rw-r--r--gusb/gusb-device.c56
-rw-r--r--gusb/gusb-device.h3
-rw-r--r--gusb/gusb-self-test.c14
4 files changed, 7 insertions, 71 deletions
diff --git a/gusb/gusb-device-list.c b/gusb/gusb-device-list.c
index e432986..ea03a95 100644
--- a/gusb/gusb-device-list.c
+++ b/gusb/gusb-device-list.c
@@ -449,16 +449,11 @@ g_usb_device_list_find_by_vid_pid (GUsbDeviceList *list,
{
GUsbDeviceListPrivate *priv = list->priv;
GUsbDevice *device = NULL;
- gboolean ret;
guint i;
for (i = 0; i < priv->devices->len; i++) {
GUsbDevice *curr = g_ptr_array_index (priv->devices, i);
- /* get descriptor details */
- ret = g_usb_device_get_descriptor (curr, error);
- if (!ret)
- goto out;
if (g_usb_device_get_vid (curr) == vid &&
g_usb_device_get_pid (curr) == pid) {
device = g_object_ref (curr);
diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c
index 0511b9d..7ee8f23 100644
--- a/gusb/gusb-device.c
+++ b/gusb/gusb-device.c
@@ -53,7 +53,6 @@ struct _GUsbDevicePrivate
GUsbContext *context;
libusb_device *device;
libusb_device_handle *handle;
- gboolean has_descriptor;
struct libusb_device_descriptor desc;
};
@@ -104,45 +103,6 @@ g_usb_device_get_property (GObject *object,
}
}
-/**
- * g_usb_device_get_descriptor:
- * @device: a #GUsbDevice
- * @error: a #GError, or %NULL
- *
- * Gets the USB descriptor for the device.
- *
- * Return value: %TRUE on success
- *
- * Since: 0.1.0
- **/
-gboolean
-g_usb_device_get_descriptor (GUsbDevice *device, GError **error)
-{
- int r;
- gboolean ret = TRUE;
- GUsbDevicePrivate *priv = device->priv;
-
- g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
-
- /* already got */
- if (priv->has_descriptor)
- goto out;
-
- r = libusb_get_device_descriptor (priv->device, &priv->desc);
- if (r < 0) {
- ret = FALSE;
- g_set_error (error,
- G_USB_DEVICE_ERROR,
- G_USB_DEVICE_ERROR_INTERNAL,
- "Failed to get USB descriptor for device: %s",
- gusb_strerror (r));
- goto out;
- }
- priv->has_descriptor = TRUE;
-out:
- return ret;
-}
-
static gboolean
g_usb_device_libusb_error_to_gerror (GUsbDevice *device,
gint rc,
@@ -1133,6 +1093,7 @@ g_usb_device_constructor (GType gtype,
GObject *obj;
GUsbDevice *device;
GUsbDevicePrivate *priv;
+ gint rc;
{
/* Always chain up to the parent constructor */
@@ -1150,6 +1111,11 @@ g_usb_device_constructor (GType gtype,
libusb_ref_device(priv->device);
+ rc = libusb_get_device_descriptor (priv->device, &priv->desc);
+ if (rc != LIBUSB_SUCCESS)
+ g_warning ("Failed to get USB descriptor for device: %s",
+ gusb_strerror (rc));
+
return obj;
}
@@ -1283,9 +1249,6 @@ g_usb_device_get_address (GUsbDevice *device)
*
* Gets the vendor ID for the device.
*
- * If g_usb_device_get_descriptor() has never been called, then this
- * function will return with 0x0000.
- *
* Return value: an ID.
*
* Since: 0.1.0
@@ -1293,8 +1256,6 @@ g_usb_device_get_address (GUsbDevice *device)
guint16
g_usb_device_get_vid (GUsbDevice *device)
{
- if (!device->priv->has_descriptor)
- return 0x0000;
return device->priv->desc.idVendor;
}
@@ -1304,9 +1265,6 @@ g_usb_device_get_vid (GUsbDevice *device)
*
* Gets the product ID for the device.
*
- * If g_usb_device_get_descriptor() has never been called, then this
- * function will return with 0x0000.
- *
* Return value: an ID.
*
* Since: 0.1.0
@@ -1314,7 +1272,5 @@ g_usb_device_get_vid (GUsbDevice *device)
guint16
g_usb_device_get_pid (GUsbDevice *device)
{
- if (!device->priv->has_descriptor)
- return 0x0000;
return device->priv->desc.idProduct;
}
diff --git a/gusb/gusb-device.h b/gusb/gusb-device.h
index f10a9c0..82e624f 100644
--- a/gusb/gusb-device.h
+++ b/gusb/gusb-device.h
@@ -113,8 +113,7 @@ GQuark g_usb_device_error_quark (void);
guint8 g_usb_device_get_bus (GUsbDevice *device);
guint8 g_usb_device_get_address (GUsbDevice *device);
-gboolean g_usb_device_get_descriptor (GUsbDevice *device,
- GError **error);
+
guint16 g_usb_device_get_vid (GUsbDevice *device);
guint16 g_usb_device_get_pid (GUsbDevice *device);
diff --git a/gusb/gusb-self-test.c b/gusb/gusb-self-test.c
index 81df02f..14d65c0 100644
--- a/gusb/gusb-self-test.c
+++ b/gusb/gusb-self-test.c
@@ -67,7 +67,6 @@ gusb_source_func (void)
static void
gusb_device_func (void)
{
- gboolean ret;
GError *error = NULL;
GPtrArray *array;
GUsbContext *ctx;
@@ -89,19 +88,6 @@ gusb_device_func (void)
g_assert_cmpint (array->len, >, 0);
device = G_USB_DEVICE (g_ptr_array_index (array, 0));
- g_assert_cmpint (g_usb_device_get_vid (device), ==, 0x0000);
- g_assert_cmpint (g_usb_device_get_pid (device), ==, 0x0000);
-
- /* get descriptor once */
- ret = g_usb_device_get_descriptor (device, &error);
- g_assert_no_error (error);
- g_assert (ret);
-
- /* get descriptor again */
- ret = g_usb_device_get_descriptor (device, &error);
- g_assert_no_error (error);
- g_assert (ret);
-
g_assert_cmpint (g_usb_device_get_vid (device), >, 0x0000);
g_assert_cmpint (g_usb_device_get_pid (device), >, 0x0000);