Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
static gboolean
fu_usb_device_open (FuDevice *device, GError **error)
{
	FuUsbDevice *self = FU_USB_DEVICE (device);
	FuUsbDevicePrivate *priv = GET_PRIVATE (self);
	FuUsbDeviceClass *klass = FU_USB_DEVICE_GET_CLASS (device);
	guint idx;
	g_autoptr(FuDeviceLocker) locker = NULL;

	g_return_val_if_fail (FU_IS_USB_DEVICE (self), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	/* already open */
	if (priv->usb_device_locker != NULL)
		return TRUE;

	/* open */
	locker = fu_device_locker_new (priv->usb_device, error);
	if (locker == NULL)
		return FALSE;

	/* get vendor */
	if (fu_device_get_vendor (device) == NULL) {
		idx = g_usb_device_get_manufacturer_index (priv->usb_device);
		if (idx != 0x00) {
			g_autofree gchar *tmp = NULL;
			tmp = g_usb_device_get_string_descriptor (priv->usb_device,
								  idx, error);
			if (tmp == NULL)
				return FALSE;
			fu_device_set_vendor (device, tmp);
		}
	}

	/* get product */
	if (fu_device_get_name (device) == NULL) {
		idx = g_usb_device_get_product_index (priv->usb_device);
		if (idx != 0x00) {
			g_autofree gchar *tmp = NULL;
			tmp = g_usb_device_get_string_descriptor (priv->usb_device,
								  idx, error);
			if (tmp == NULL)
				return FALSE;
			fu_device_set_name (device, tmp);
		}
	}

	/* get serial number */
	if (fu_device_get_serial (device) == NULL) {
		idx = g_usb_device_get_serial_number_index (priv->usb_device);
		if (idx != 0x00) {
			g_autofree gchar *tmp = NULL;
			tmp = g_usb_device_get_string_descriptor (priv->usb_device,
								  idx, error);
			if (tmp == NULL)
				return FALSE;
			fu_device_set_serial (device, tmp);
		}
	}

	/* get version number, falling back to the USB device release */
	idx = g_usb_device_get_custom_index (priv->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 (priv->usb_device, idx, NULL);
		fu_device_set_version (device, tmp);
	}

	/* get GUID from the descriptor if set */
	idx = g_usb_device_get_custom_index (priv->usb_device,
					     G_USB_DEVICE_CLASS_VENDOR_SPECIFIC,
					     'G', 'U', NULL);
	if (idx != 0x00) {
		g_autofree gchar *tmp = NULL;
		tmp = g_usb_device_get_string_descriptor (priv->usb_device, idx, NULL);
		fu_device_add_guid (device, tmp);
	}

	/* subclassed */
	if (klass->open != NULL) {
		if (!klass->open (self, error))
			return FALSE;
	}

	/* success */
	priv->usb_device_locker = g_steal_pointer (&locker);
	return TRUE;
}