Split the device add / remove functions into a gudev specific part and a generic part. This is a preparation patch for adding support libusb's new hotplug API. Signed-off-by: Hans de Goede <hdegoede@xxxxxxxxxx> --- gtk/usb-device-manager.c | 149 ++++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 67 deletions(-) diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c index 18ad3b8..8b8bb8b 100644 --- a/gtk/usb-device-manager.c +++ b/gtk/usb-device-manager.c @@ -157,8 +157,8 @@ static void spice_usb_device_manager_uevent_cb(GUdevClient *client, const gchar *action, GUdevDevice *udevice, gpointer user_data); -static void spice_usb_device_manager_add_dev(SpiceUsbDeviceManager *self, - GUdevDevice *udev); +static void spice_usb_device_manager_add_udev(SpiceUsbDeviceManager *self, + GUdevDevice *udev); static void spice_usb_device_manager_check_redir_on_connect( SpiceUsbDeviceManager *self, SpiceChannel *channel); @@ -272,7 +272,7 @@ static gboolean spice_usb_device_manager_initable_init(GInitable *initable, libusb_get_device_list(priv->context, &priv->coldplug_list); list = g_udev_client_query_by_subsystem(priv->udev, "usb"); for (it = g_list_first(list); it; it = g_list_next(it)) { - spice_usb_device_manager_add_dev(self, it->data); + spice_usb_device_manager_add_udev(self, it->data); g_object_unref(it->data); } g_list_free(list); @@ -773,78 +773,35 @@ spice_usb_device_manager_find_device(SpiceUsbDeviceManager *self, } static void spice_usb_device_manager_add_dev(SpiceUsbDeviceManager *self, - GUdevDevice *udev) + libusb_device *libdev) { SpiceUsbDeviceManagerPrivate *priv = self->priv; - libusb_device *libdev = NULL, **dev_list = NULL; - SpiceUsbDevice *device = NULL; - const gchar *devtype, *devclass; - int i, bus, address; - gboolean auto_ok = FALSE; + struct libusb_device_descriptor desc; + SpiceUsbDevice *device; - devtype = g_udev_device_get_property(udev, "DEVTYPE"); - /* Check if this is a usb device (and not an interface) */ - if (!devtype || strcmp(devtype, "usb_device")) + if (!spice_usb_device_manager_get_device_descriptor(libdev, &desc)) return; /* Skip hubs */ - devclass = g_udev_device_get_sysfs_attr(udev, "bDeviceClass"); - if (!devclass || !strcmp(devclass, "09")) + if (desc.bDeviceClass == LIBUSB_CLASS_HUB) return; - if (!spice_usb_device_manager_get_udev_bus_n_address(udev, &bus, &address)) { - g_warning("USB device without bus number or device address"); + device = (SpiceUsbDevice*)spice_usb_device_new(libdev); + if (!device) return; - } - device = spice_usb_device_manager_find_device(self, bus, address); - if (device) { - SPICE_DEBUG("USB device 0x%04x:0x%04x at %d.%d already exists, ignored", - spice_usb_device_get_vid(device), - spice_usb_device_get_pid(device), - spice_usb_device_get_busnum(device), - spice_usb_device_get_devaddr(device)); - return; - } - - if (priv->coldplug_list) - dev_list = priv->coldplug_list; - else - libusb_get_device_list(priv->context, &dev_list); + g_ptr_array_add(priv->devices, device); - for (i = 0; dev_list && dev_list[i]; i++) { - if (spice_usb_device_manager_libdev_match(dev_list[i], bus, address)) { - libdev = dev_list[i]; - break; - } - } + if (priv->auto_connect) { + gboolean can_redirect, auto_ok; - if (libdev) - device = (SpiceUsbDevice*)spice_usb_device_new(libdev); + can_redirect = spice_usb_device_manager_can_redirect_device( + self, device, NULL); - if (device && priv->auto_connect) { auto_ok = usbredirhost_check_device_filter( priv->auto_conn_filter_rules, priv->auto_conn_filter_rules_count, libdev, 0) == 0; - } - - if (!priv->coldplug_list) - libusb_free_device_list(dev_list, 1); - - if (!device) { - g_warning("Could not find USB device to add " DEV_ID_FMT, - bus, address); - return; - } - - g_ptr_array_add(priv->devices, device); - - if (priv->auto_connect) { - gboolean can_redirect; - - can_redirect = spice_usb_device_manager_can_redirect_device( - self, device, NULL); if (can_redirect && auto_ok) spice_usb_device_manager_connect_device_async(self, @@ -857,15 +814,11 @@ static void spice_usb_device_manager_add_dev(SpiceUsbDeviceManager *self, g_signal_emit(self, signals[DEVICE_ADDED], 0, device); } -static void spice_usb_device_manager_remove_dev(SpiceUsbDeviceManager *self, - GUdevDevice *udev) +static void spice_usb_device_manager_remove_dev(SpiceUsbDeviceManager *self, + int bus, int address) { SpiceUsbDeviceManagerPrivate *priv = self->priv; - SpiceUsbDevice *device = NULL; - int bus, address; - - if (!spice_usb_device_manager_get_udev_bus_n_address(udev, &bus, &address)) - return; + SpiceUsbDevice *device; device = spice_usb_device_manager_find_device(self, bus, address); if (!device) { @@ -893,6 +846,68 @@ static void spice_usb_device_manager_remove_dev(SpiceUsbDeviceManager *self, spice_usb_device_unref(device); } +static void spice_usb_device_manager_add_udev(SpiceUsbDeviceManager *self, + GUdevDevice *udev) +{ + SpiceUsbDeviceManagerPrivate *priv = self->priv; + libusb_device *libdev = NULL, **dev_list = NULL; + SpiceUsbDevice *device; + const gchar *devtype; + int i, bus, address; + + devtype = g_udev_device_get_property(udev, "DEVTYPE"); + /* Check if this is a usb device (and not an interface) */ + if (!devtype || strcmp(devtype, "usb_device")) + return; + + if (!spice_usb_device_manager_get_udev_bus_n_address(udev, &bus, &address)) { + g_warning("USB device without bus number or device address"); + return; + } + + device = spice_usb_device_manager_find_device(self, bus, address); + if (device) { + SPICE_DEBUG("USB device 0x%04x:0x%04x at %d.%d already exists, ignored", + spice_usb_device_get_vid(device), + spice_usb_device_get_pid(device), + spice_usb_device_get_busnum(device), + spice_usb_device_get_devaddr(device)); + return; + } + + if (priv->coldplug_list) + dev_list = priv->coldplug_list; + else + libusb_get_device_list(priv->context, &dev_list); + + for (i = 0; dev_list && dev_list[i]; i++) { + if (spice_usb_device_manager_libdev_match(dev_list[i], bus, address)) { + libdev = dev_list[i]; + break; + } + } + + if (libdev) + spice_usb_device_manager_add_dev(self, libdev); + else + g_warning("Could not find USB device to add " DEV_ID_FMT, + bus, address); + + if (!priv->coldplug_list) + libusb_free_device_list(dev_list, 1); +} + +static void spice_usb_device_manager_remove_udev(SpiceUsbDeviceManager *self, + GUdevDevice *udev) +{ + int bus, address; + + if (!spice_usb_device_manager_get_udev_bus_n_address(udev, &bus, &address)) + return; + + spice_usb_device_manager_remove_dev(self, bus, address); +} + static void spice_usb_device_manager_uevent_cb(GUdevClient *client, const gchar *action, GUdevDevice *udevice, @@ -901,9 +916,9 @@ static void spice_usb_device_manager_uevent_cb(GUdevClient *client, SpiceUsbDeviceManager *self = SPICE_USB_DEVICE_MANAGER(user_data); if (g_str_equal(action, "add")) - spice_usb_device_manager_add_dev(self, udevice); + spice_usb_device_manager_add_udev(self, udevice); else if (g_str_equal (action, "remove")) - spice_usb_device_manager_remove_dev(self, udevice); + spice_usb_device_manager_remove_udev(self, udevice); } static void spice_usb_device_manager_channel_connect_cb( -- 1.8.3.1 _______________________________________________ Spice-devel mailing list Spice-devel@xxxxxxxxxxxxxxxxxxxxx http://lists.freedesktop.org/mailman/listinfo/spice-devel