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; }
/** * 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 }
/** * pk_device_rebind: **/ static gboolean pk_device_rebind (GUdevClient *client, const gchar *path, GError **error) { GUdevDevice *device; gint busnum; gint devnum; gboolean ret = FALSE; const gchar *driver; const gchar *subsystem; gchar *bus_id = NULL; GError *error_local = NULL; /* get device */ device = g_udev_client_query_by_sysfs_path (client, path); if (device == NULL) { /* TRANSLATORS: the device could not be found in sysfs */ *error = g_error_new (1, 0, "%s: %s\n", _("Device could not be found"), path); goto out; } /* get properties about the device */ driver = g_udev_device_get_driver (device); subsystem = g_udev_device_get_subsystem (device); busnum = g_udev_device_get_sysfs_attr_as_int (device, "busnum"); devnum = g_udev_device_get_sysfs_attr_as_int (device, "devnum"); /* debug */ if (verbose) { g_debug ("DEVICE: driver:%s, subsystem:%s, busnum:%i, devnum:%i", driver, subsystem, busnum, devnum); } /* form the bus id as recognised by the kernel */ bus_id = g_path_get_basename (path); /* FIXME: sometimes the busnum is incorrect */ if (bus_id == NULL) bus_id = g_strdup_printf ("%i-%i", busnum, devnum); /* unbind device */ ret = pk_device_unbind (path, bus_id, &error_local); if (!ret) { /* TRANSLATORS: we failed to release the current driver */ *error = g_error_new (1, 0, "%s: %s", _("Failed to unregister driver"), error_local->message); g_error_free (error_local); goto out; } /* bind device */ ret = pk_device_bind (bus_id, subsystem, driver, &error_local); if (!ret) { /* TRANSLATORS: we failed to bind the old driver */ *error = g_error_new (1, 0, "%s: %s", _("Failed to register driver"), error_local->message); g_error_free (error_local); goto out; } out: g_free (bus_id); if (device != NULL) g_object_unref (device); return ret; }