예제 #1
0
/**
 * fu_usb_device_get_vid:
 * @self: A #FuUsbDevice
 *
 * Gets the device vendor code.
 *
 * Returns: integer, or 0x0 if unset or invalid
 *
 * Since: 1.1.2
 **/
guint16
fu_usb_device_get_vid (FuUsbDevice *self)
{
	FuUsbDevicePrivate *priv = GET_PRIVATE (self);
	g_return_val_if_fail (FU_IS_USB_DEVICE (self), 0x0000);
	return g_usb_device_get_vid (priv->usb_device);
}
예제 #2
0
/**
 * g_usb_context_find_by_vid_pid:
 * @context: a #GUsbContext
 * @vid: a vendor ID
 * @pid: a product ID
 * @error: A #GError or %NULL
 *
 * Finds a device based on its bus and address values.
 *
 * Return value: (transfer full): a new #GUsbDevice, or %NULL if not found.
 *
 * Since: 0.2.2
 **/
GUsbDevice *
g_usb_context_find_by_vid_pid (GUsbContext  *context,
                               guint16       vid,
                               guint16       pid,
                               GError      **error)
{
	GUsbContextPrivate *priv;
	GUsbDevice *device = NULL;
	guint i;

	g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	priv = context->priv;

	g_usb_context_enumerate (context);
	for (i = 0; i < priv->devices->len; i++) {
		GUsbDevice *curr = g_ptr_array_index (priv->devices, i);
		if (g_usb_device_get_vid (curr) == vid &&
		    g_usb_device_get_pid (curr) == pid) {
			device = g_object_ref (curr);
			break;
		}
	}

	if (device == NULL) {
		g_set_error (error,
		             G_USB_DEVICE_ERROR,
		             G_USB_DEVICE_ERROR_NO_DEVICE,
		             "Failed to find device %04x:%04x",
		             vid, pid);
	}

	return device;
}
예제 #3
0
파일: dfu-context.c 프로젝트: lantw44/fwupd
static gchar *
dfu_context_get_device_id (DfuDevice *device)
{
	GUsbDevice *dev;
	dev = dfu_device_get_usb_dev (device);
	if (dev == NULL)
		return g_strdup (dfu_device_get_platform_id (device));
	return g_strdup_printf ("%04x:%04x [%s]",
				g_usb_device_get_vid (dev),
				g_usb_device_get_pid (dev),
				g_usb_device_get_platform_id (dev));
}
예제 #4
0
파일: dfu-context.c 프로젝트: lantw44/fwupd
/**
 * dfu_context_get_device_by_vid_pid:
 * @context: a #DfuContext
 * @vid: a vendor ID
 * @pid: a product ID
 * @error: a #GError, or %NULL
 *
 * Finds a device in the context with a specific vendor:product ID.
 * An error is returned if more than one device matches.
 *
 * Return value: (transfer full): a #DfuDevice for success, or %NULL for an error
 *
 * Since: 0.5.4
 **/
DfuDevice *
dfu_context_get_device_by_vid_pid (DfuContext *context,
				   guint16 vid, guint16 pid,
				   GError **error)
{
	DfuContextPrivate *priv = GET_PRIVATE (context);
	DfuContextItem *item;
	DfuDevice *device = NULL;
	GUsbDevice *dev;
	guint i;

	g_return_val_if_fail (DFU_IS_CONTEXT (context), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	/* search all devices */
	for (i = 0; i < priv->devices->len; i++) {

		/* match */
		item = g_ptr_array_index (priv->devices, i);
		dev = dfu_device_get_usb_dev (item->device);
		if (g_usb_device_get_vid (dev) == vid &&
		    g_usb_device_get_pid (dev) == pid) {
			if (device != NULL) {
				g_set_error (error,
					     DFU_ERROR,
					     DFU_ERROR_INVALID_DEVICE,
					     "multiple device matches for %04x:%04x",
					     vid, pid);
				return NULL;
			}
			device = item->device;
			continue;
		}
	}
	if (device == NULL) {
		g_set_error (error,
			     DFU_ERROR,
			     DFU_ERROR_NOT_FOUND,
			     "no device matches for %04x:%04x",
			     vid, pid);
		return NULL;
	}
	return g_object_ref (device);
}
예제 #5
0
파일: gusb-main.c 프로젝트: hughsie/libgusb
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;
}
예제 #6
0
파일: ebitdo-tool.c 프로젝트: attente/fwupd
int
main (int argc, char **argv)
{
	gsize len;
	guint i;
	g_autofree guint8 *data = NULL;
	g_autoptr(EbitdoDevice) dev = NULL;
	g_autoptr(GBytes) fw = NULL;
	g_autoptr(GError) error = NULL;
	g_autoptr(GPtrArray) devices = NULL;
	g_autoptr(GUsbContext) usb_ctx = NULL;

	g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);

	/* require filename */
	if (argc != 2) {
		g_print ("USAGE: %s <filename>\n", argv[0]);
		return 1;
	}

	/* get the device */
	usb_ctx = g_usb_context_new (&error);
	if (usb_ctx == NULL) {
		g_print ("Failed to open USB devices: %s\n", error->message);
		return 1;
	}
	g_usb_context_enumerate (usb_ctx);
	devices = g_usb_context_get_devices (usb_ctx);
	for (i = 0; i < devices->len; i++) {
		GUsbDevice *usb_dev_tmp = g_ptr_array_index (devices, i);
		g_autoptr(EbitdoDevice) dev_tmp = ebitdo_device_new (usb_dev_tmp);
		if (ebitdo_device_get_kind (dev_tmp) != EBITDO_DEVICE_KIND_UNKNOWN) {
			dev = g_object_ref (dev_tmp);
			break;
		}
	}

	/* nothing supported */
	if (dev == NULL) {
		g_print ("No supported device plugged in!\n");
		return 1;
	}
	g_debug ("found %s [%04x:%04x]",
		 ebitdo_device_kind_to_string (ebitdo_device_get_kind (dev)),
		 g_usb_device_get_vid (ebitdo_device_get_usb_device (dev)),
		 g_usb_device_get_pid (ebitdo_device_get_usb_device (dev)));

	/* open device */
	if (!ebitdo_device_open (dev, &error)) {
		g_print ("Failed to open USB device: %s\n", error->message);
		return 1;
	}
	g_print ("Device Firmware Ver: %s\n", ebitdo_device_get_version (dev));
	g_print ("Device Verification ID:\n");
	for (i = 0; i < 9; i++)
		g_print ("\t%u = 0x%08x\n", i, ebitdo_device_get_serial(dev)[i]);

	/* not in bootloader mode, so print what to do */
	if (ebitdo_device_get_kind (dev) != EBITDO_DEVICE_KIND_BOOTLOADER) {
		g_print ("1. Disconnect the controller\n");
		switch (ebitdo_device_get_kind (dev)) {
		case EBITDO_DEVICE_KIND_FC30:
		case EBITDO_DEVICE_KIND_NES30:
		case EBITDO_DEVICE_KIND_SFC30:
		case EBITDO_DEVICE_KIND_SNES30:
			g_print ("2. Hold down L+R+START for 3 seconds until "
				 "both LED lights flashing.\n");
			break;
		case EBITDO_DEVICE_KIND_FC30PRO:
		case EBITDO_DEVICE_KIND_NES30PRO:
			g_print ("2. Hold down RETURN+POWER for 3 seconds until "
				 "both LED lights flashing.\n");
			break;
		case EBITDO_DEVICE_KIND_FC30_ARCADE:
			g_print ("2. Hold down L1+R1+HOME for 3 seconds until "
				 "both blue LED and green LED blink.\n");
			break;
		default:
			g_print ("2. Do what it says in the manual.\n");
			break;
		}
		g_print ("3. Connect controller\n");
		return 1;
	}

	/* load firmware file */
	if (!g_file_get_contents (argv[1], (gchar **) &data, &len, &error)) {
		g_print ("Failed to load file: %s\n", error->message);
		return 1;
	}

	/* update with data blob */
	fw = g_bytes_new (data, len);
	if (!ebitdo_device_write_firmware (dev, fw,
					   ebitdo_write_progress_cb, NULL,
					   &error)) {
		g_print ("Failed to write firmware: %s\n", error->message);
		return 1;
	}

	/* close device */
	if (!ebitdo_device_close (dev, &error)) {
		g_print ("Failed to close USB device: %s\n", error->message);
		return 1;
	}

	/* success */
	g_print ("Now turn off the controller with the power button.\n");

	return 0;
}
예제 #7
0
static gboolean
fu_usb_device_probe (FuDevice *device, GError **error)
{
	FuUsbDevice *self = FU_USB_DEVICE (device);
	FuUsbDeviceClass *klass = FU_USB_DEVICE_GET_CLASS (device);
	FuUsbDevicePrivate *priv = GET_PRIVATE (self);
	guint16 release;
	g_autofree gchar *devid0 = NULL;
	g_autofree gchar *devid1 = NULL;
	g_autofree gchar *devid2 = NULL;
	g_autofree gchar *vendor_id = NULL;
	g_autoptr(GPtrArray) intfs = NULL;

	/* set vendor ID */
	vendor_id = g_strdup_printf ("USB:0x%04X", g_usb_device_get_vid (priv->usb_device));
	fu_device_set_vendor_id (device, vendor_id);

	/* set the version if the release has been set */
	release = g_usb_device_get_release (priv->usb_device);
	if (release != 0x0) {
		g_autofree gchar *version = NULL;
		version = fu_common_version_from_uint16 (release, FU_VERSION_FORMAT_BCD);
		fu_device_set_version (device, version);
	}

	/* add GUIDs in order of priority */
	devid2 = g_strdup_printf ("USB\\VID_%04X&PID_%04X&REV_%04X",
				  g_usb_device_get_vid (priv->usb_device),
				  g_usb_device_get_pid (priv->usb_device),
				  release);
	fu_device_add_instance_id (device, devid2);
	devid1 = g_strdup_printf ("USB\\VID_%04X&PID_%04X",
				  g_usb_device_get_vid (priv->usb_device),
				  g_usb_device_get_pid (priv->usb_device));
	fu_device_add_instance_id (device, devid1);
	devid0 = g_strdup_printf ("USB\\VID_%04X",
				  g_usb_device_get_vid (priv->usb_device));
	fu_device_add_instance_id (device, devid0);

	/* add the interface GUIDs */
	intfs = g_usb_device_get_interfaces (priv->usb_device, error);
	if (intfs == NULL)
		return FALSE;
	for (guint i = 0; i < intfs->len; i++) {
		GUsbInterface *intf = g_ptr_array_index (intfs, i);
		g_autofree gchar *intid1 = NULL;
		g_autofree gchar *intid2 = NULL;
		g_autofree gchar *intid3 = NULL;
		intid1 = g_strdup_printf ("USB\\CLASS_%02X&SUBCLASS_%02X&PROT_%02X",
					  g_usb_interface_get_class (intf),
					  g_usb_interface_get_subclass (intf),
					  g_usb_interface_get_protocol (intf));
		fu_device_add_instance_id (device, intid1);
		intid2 = g_strdup_printf ("USB\\CLASS_%02X&SUBCLASS_%02X",
					  g_usb_interface_get_class (intf),
					  g_usb_interface_get_subclass (intf));
		fu_device_add_instance_id (device, intid2);
		intid3 = g_strdup_printf ("USB\\CLASS_%02X",
					  g_usb_interface_get_class (intf));
		fu_device_add_instance_id (device, intid3);
	}

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

	/* success */
	return TRUE;
}