/** * gusb_cmd_watch: **/ static gboolean gusb_cmd_watch (GUsbCmdPrivate *priv, gchar **values, GError **error) { gboolean ret = TRUE; GPtrArray *devices; guint i; GUsbDevice *device; GMainLoop *loop; devices = g_usb_context_get_devices (priv->usb_ctx); for (i = 0; i < devices->len; i++) { device = g_ptr_array_index (devices, i); g_print ("device %s already present %x:%x\n", g_usb_device_get_platform_id (device), g_usb_device_get_bus (device), g_usb_device_get_address (device)); gusb_main_device_open (device); } loop = g_main_loop_new (NULL, FALSE); g_signal_connect (priv->usb_ctx, "device-added", G_CALLBACK (gusb_device_list_added_cb), priv); g_signal_connect (priv->usb_ctx, "device-removed", G_CALLBACK (gusb_device_list_removed_cb), priv); g_main_loop_run (loop); g_main_loop_unref (loop); g_ptr_array_unref (devices); return ret; }
/** * g_usb_context_find_by_bus_address: * @context: a #GUsbContext * @bus: a bus number * @address: a bus address * @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_bus_address (GUsbContext *context, guint8 bus, guint8 address, 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_bus (curr) == bus && g_usb_device_get_address (curr) == address) { 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", bus, address); } return device; }
static gboolean fu_altos_device_find_tty (FuAltosDevice *self, GError **error) { GUsbDevice *usb_device = fu_usb_device_get_dev (FU_USB_DEVICE (self)); g_autoptr(GList) devices = NULL; g_autoptr(GUdevClient) gudev_client = g_udev_client_new (NULL); /* find all tty devices */ devices = g_udev_client_query_by_subsystem (gudev_client, "tty"); for (GList *l = devices; l != NULL; l = l->next) { GUdevDevice *dev = G_UDEV_DEVICE (l->data); /* get the tty device */ const gchar *dev_file = g_udev_device_get_device_file (dev); if (dev_file == NULL) continue; /* get grandparent */ dev = g_udev_device_get_parent (dev); if (dev == NULL) continue; dev = g_udev_device_get_parent (dev); if (dev == NULL) continue; /* check correct device */ if (g_udev_device_get_sysfs_attr_as_int (dev, "busnum") != g_usb_device_get_bus (usb_device)) continue; if (g_udev_device_get_sysfs_attr_as_int (dev, "devnum") != g_usb_device_get_address (usb_device)) continue; /* success */ self->tty = g_strdup (dev_file); return TRUE; } /* failure */ g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "failed to find tty for %u:%u", g_usb_device_get_bus (usb_device), g_usb_device_get_address (usb_device)); return FALSE; }
/** * gusb_device_list_removed_cb: **/ static void gusb_device_list_removed_cb (GUsbContext *context, GUsbDevice *device, gpointer user_data) { g_print ("device %s removed %x:%x\n", g_usb_device_get_platform_id (device), g_usb_device_get_bus (device), g_usb_device_get_address (device)); }
static void g_usb_context_rescan (GUsbContext *context) { GList *existing_devices = NULL; GList *l; GUsbDevice *device; GUsbContextPrivate *priv = context->priv; gboolean found; guint i; libusb_device **dev_list = NULL; /* copy to a context so we can remove from the array */ for (i = 0; i < priv->devices->len; i++) { device = g_ptr_array_index (priv->devices, i); existing_devices = g_list_prepend (existing_devices, device); } /* look for any removed devices */ for (l = existing_devices; l != NULL; l = l->next) { device = G_USB_DEVICE (l->data); found = FALSE; for (i = 0; dev_list && dev_list[i]; i++) { if (libusb_get_bus_number (dev_list[i]) == g_usb_device_get_bus (device) && libusb_get_device_address (dev_list[i]) == g_usb_device_get_address (device)) { found = TRUE; break; } } if (!found) { g_usb_context_emit_device_remove (context, device); g_ptr_array_remove (priv->devices, device); } } /* add any devices not yet added (duplicates will be filtered */ libusb_get_device_list (priv->ctx, &dev_list); for (i = 0; dev_list && dev_list[i]; i++) g_usb_context_add_device (context, dev_list[i]); g_list_free (existing_devices); libusb_free_device_list (dev_list, 1); }
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; }