static void g_usb_context_add_device (GUsbContext *context, struct libusb_device *dev) { GUsbDevice *device = NULL; GUsbContextPrivate *priv = context->priv; guint8 bus; guint8 address; GError *error = NULL; /* does any existing device exist */ bus = libusb_get_bus_number (dev); address = libusb_get_device_address (dev); if (priv->done_enumerate) device = g_usb_context_find_by_bus_address (context, bus, address, NULL); if (device != NULL) { g_debug ("%i:%i already exists", bus, address); goto out; } /* add the device */ device = _g_usb_device_new (context, dev, &error); if (device == NULL) { g_debug ("There was a problem creating the device: %s", error->message); g_error_free (error); goto out; } g_ptr_array_add (priv->devices, g_object_ref (device)); g_usb_context_emit_device_add (context, device); out: if (device != NULL) g_object_unref (device); }
static void g_usb_context_remove_device (GUsbContext *context, struct libusb_device *dev) { GUsbDevice *device = NULL; GUsbContextPrivate *priv = context->priv; guint8 bus; guint8 address; /* does any existing device exist */ bus = libusb_get_bus_number (dev); address = libusb_get_device_address (dev); device = g_usb_context_find_by_bus_address (context, bus, address, NULL); if (device == NULL) { g_debug ("%i:%i does not exist", bus, address); return; } g_usb_context_emit_device_remove (context, device); g_ptr_array_remove (priv->devices, device); g_object_unref (device); }
/** * cd_sensor_open_usb_device: **/ GUsbDevice * cd_sensor_open_usb_device (CdSensor *sensor, gint config, gint interface, GError **error) { #ifdef HAVE_UDEV CdSensorPrivate *priv = GET_PRIVATE (sensor); guint8 busnum; guint8 devnum; g_autoptr(GUsbDevice) device = NULL; /* convert from GUdevDevice to GUsbDevice */ busnum = g_udev_device_get_sysfs_attr_as_int (priv->device, "busnum"); devnum = g_udev_device_get_sysfs_attr_as_int (priv->device, "devnum"); device = g_usb_context_find_by_bus_address (priv->usb_ctx, busnum, devnum, error); if (device == NULL) return NULL; /* open device, set config and claim interface */ if (!g_usb_device_open (device, error)) return NULL; if (!g_usb_device_set_configuration (device, config, error)) return NULL; if (!g_usb_device_claim_interface (device, interface, G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER, error)) { return NULL; } return g_object_ref (device); #else g_set_error_literal (error, 1, 0, "failed: no gudev support"); return NULL; #endif }