[Spice-devel] [spice-gtk PATCH 6/9] usb-device-manager: use a function to get vid:pid from a libusb_device
Uri Lublin
uril at redhat.com
Mon Mar 25 03:01:27 PDT 2013
To be reused later.
Also implemented a get_device_descriptor function (in case it will be
needed in the future).
---
gtk/usb-device-manager.c | 58 +++++++++++++++++++++++++++++++++++++++---------
1 file changed, 48 insertions(+), 10 deletions(-)
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index b4320af..0a561f1 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -607,6 +607,50 @@ static gboolean spice_usb_device_manager_get_udev_bus_n_address(
return *bus && *address;
}
+static gboolean spice_usb_device_manager_get_device_descriptor(
+ libusb_device *libdev,
+ struct libusb_device_descriptor *desc)
+{
+ int errcode;
+ const gchar *errstr;
+
+ g_return_val_if_fail(libdev != NULL, FALSE);
+ g_return_val_if_fail(desc != NULL, FALSE);
+
+ errcode = libusb_get_device_descriptor(libdev, desc);
+ if (errcode < 0) {
+ int bus, addr;
+
+ bus = libusb_get_bus_number(libdev);
+ addr = libusb_get_device_address(libdev);
+ errstr = spice_usbutil_libusb_strerror(errcode);
+ g_warning("cannot get device descriptor for (%p) %d.%d -- %s(%d)",
+ libdev, bus, addr, errstr, errcode);
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static gboolean spice_usb_device_manager_get_libdev_vid_pid(
+ libusb_device *libdev, int *vid, int *pid)
+{
+ struct libusb_device_descriptor desc;
+
+ g_return_val_if_fail(libdev != NULL, FALSE);
+ g_return_val_if_fail(vid != NULL, FALSE);
+ g_return_val_if_fail(pid != NULL, FALSE);
+
+ *vid = *pid = 0;
+
+ if (!spice_usb_device_manager_get_device_descriptor(libdev, &desc)) {
+ return FALSE;
+ }
+ *vid = desc.idVendor;
+ *pid = desc.idProduct;
+
+ return TRUE;
+}
+
/* ------------------------------------------------------------------ */
/* callbacks */
@@ -1462,9 +1506,7 @@ gchar *spice_usb_device_get_description(SpiceUsbDevice *device, const gchar *for
static SpiceUsbDeviceInfo *spice_usb_device_new(libusb_device *libdev)
{
SpiceUsbDeviceInfo *info;
- struct libusb_device_descriptor desc;
- int errcode;
- const gchar *errstr;
+ int vid, pid;
guint8 bus, addr;
g_return_val_if_fail(libdev != NULL, NULL);
@@ -1472,11 +1514,7 @@ static SpiceUsbDeviceInfo *spice_usb_device_new(libusb_device *libdev)
bus = libusb_get_bus_number(libdev);
addr = libusb_get_device_address(libdev);
- errcode = libusb_get_device_descriptor(libdev, &desc);
- if (errcode < 0) {
- errstr = spice_usbutil_libusb_strerror(errcode);
- g_warning("cannot get device descriptor for (%p) %d.%d -- %s(%d)",
- libdev, bus, addr, errstr, errcode);
+ if (!spice_usb_device_manager_get_libdev_vid_pid(libdev, &vid, &pid)) {
return NULL;
}
@@ -1484,8 +1522,8 @@ static SpiceUsbDeviceInfo *spice_usb_device_new(libusb_device *libdev)
info->busnum = bus;
info->devaddr = addr;
- info->vid = desc.idVendor;
- info->pid = desc.idProduct;
+ info->vid = vid;
+ info->pid = pid;
info->ref = 1;
return info;
--
1.8.1.4
More information about the Spice-devel
mailing list