static void gusb_main_device_open (GUsbDevice *device) { GError *error = NULL; guint8 idx; /* open */ if (!g_usb_device_open (device, &error)) { g_print ("failed to open: %s\n", error->message); g_error_free (error); return; } /* print info we can only get whilst open */ idx = g_usb_device_get_product_index (device); if (idx != 0x00) { gchar *product = g_usb_device_get_string_descriptor (device, idx, &error); if (product == NULL) { g_print ("failed to get string desc: %s\n", error->message); g_error_free (error); return; } g_print ("product: %s\n", product); } }
static void fu_provider_chug_get_firmware_version (FuProviderChugItem *item) { FuProviderChugPrivate *priv = GET_PRIVATE (item->provider_chug); guint16 major; guint16 micro; guint16 minor; guint8 idx; g_autoptr(GError) error = NULL; g_autofree gchar *version = NULL; /* try to get the version without claiming interface */ if (!g_usb_device_open (item->usb_device, &error)) { g_debug ("Failed to open, polling: %s", error->message); return; } idx = g_usb_device_get_custom_index (item->usb_device, G_USB_DEVICE_CLASS_VENDOR_SPECIFIC, 'F', 'W', NULL); if (idx != 0x00) { g_autofree gchar *tmp = NULL; tmp = g_usb_device_get_string_descriptor (item->usb_device, idx, NULL); if (tmp != NULL) { item->got_version = TRUE; g_debug ("obtained fwver using extension '%s'", tmp); fu_device_set_version (item->device, tmp); goto out; } } g_usb_device_close (item->usb_device, NULL); /* attempt to open the device and get the serial number */ if (!ch_device_open (item->usb_device, &error)) { g_debug ("Failed to claim interface, polling: %s", error->message); return; } ch_device_queue_get_firmware_ver (priv->device_queue, item->usb_device, &major, &minor, µ); if (!ch_device_queue_process (priv->device_queue, CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, NULL, &error)) { g_warning ("Failed to get serial: %s", error->message); goto out; } /* got things the old fashioned way */ item->got_version = TRUE; version = g_strdup_printf ("%i.%i.%i", major, minor, micro); g_debug ("obtained fwver using API '%s'", version); fu_device_set_version (item->device, version); out: /* we're done here */ g_clear_error (&error); if (!g_usb_device_close (item->usb_device, &error)) g_debug ("Failed to close: %s", error->message); }
/** * osp_device_open: * @device: a #GUsbDevice instance * @error: A #GError or %NULL * * Opens the device and claims the interface * * Return value: %TRUE for success * * Since: 1.2.11 **/ gboolean osp_device_open (GUsbDevice *device, GError **error) { g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); if (!g_usb_device_open (device, error)) return FALSE; if (!g_usb_device_claim_interface (device, 0x00, 0, error)) { g_prefix_error (error, "Failed to claim interface: "); return FALSE; } return TRUE; }
/** * cd_sensor_open_usb_device: **/ GUsbDevice * cd_sensor_open_usb_device (CdSensor *sensor, gint config, gint interface, GError **error) { CdSensorPrivate *priv = sensor->priv; gboolean ret; guint8 busnum; guint8 devnum; GUsbDevice *device; GUsbDevice *device_success = 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_device_list_find_by_bus_address (priv->device_list, busnum, devnum, error); if (device == NULL) goto out; /* open device, set config and claim interface */ ret = g_usb_device_open (device, error); if (!ret) goto out; ret = g_usb_device_set_configuration (device, config, error); if (!ret) goto out; ret = g_usb_device_claim_interface (device, interface, G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER, error); if (!ret) goto out; /* success */ device_success = g_object_ref (device); out: if (device != NULL) g_object_unref (device); return device_success; }
/** * 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 }
static gboolean moo_cb (GNode *node, gpointer data) { GUsbDevice *device = G_USB_DEVICE (node->data); GNode *n; guint i; const gchar *tmp; gchar *product = NULL; gchar *vendor = NULL; GString *str = NULL; if (device == NULL) { g_print ("Root Device\n"); return FALSE; } /* indent */ str = g_string_new (""); for (n = node; n->data != NULL; n = n->parent) g_string_append (str, " "); /* add bus:address */ g_string_append_printf (str, "%02x:%02x [%04x:%04x]", g_usb_device_get_bus (device), g_usb_device_get_address (device), g_usb_device_get_vid (device), g_usb_device_get_pid (device)); /* pad */ for (i = str->len; i < 30; i++) g_string_append (str, " "); /* We don't error check these as not all devices have these (and the device_open may have failed). */ g_usb_device_open (device, NULL); vendor = g_usb_device_get_string_descriptor (device, g_usb_device_get_manufacturer_index (device), NULL); product = g_usb_device_get_string_descriptor (device, g_usb_device_get_product_index (device), NULL); /* lookup from usb.ids */ if (vendor == NULL) { tmp = g_usb_device_get_vid_as_str (device); if (tmp != NULL) vendor = g_strdup (tmp); } if (product == NULL) { tmp = g_usb_device_get_pid_as_str (device); if (tmp != NULL) product = g_strdup (tmp); } /* a hub */ if (g_usb_device_get_device_class (device) == 0x09 && product == NULL) { product = g_strdup ("USB HUB"); } /* fall back to the VID/PID */ if (product == NULL) product = g_strdup ("Unknown"); if (vendor == NULL) vendor = g_strdup ("Unknown"); /* add bus:address */ g_string_append_printf (str, "%s - %s", vendor, product); g_free (product); g_free (vendor); g_print ("%s\n", str->str); g_string_free (str, TRUE); return FALSE; }