1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/*
* Copyright 2022 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include "fu-gpio-device.h"
#include "fu-gpio-plugin.h"
struct _FuGpioPlugin {
FuPlugin parent_instance;
GPtrArray *current_logical_ids; /* element-type: utf-8 */
};
G_DEFINE_TYPE(FuGpioPlugin, fu_gpio_plugin, FU_TYPE_PLUGIN)
static void
fu_gpio_plugin_to_string(FuPlugin *plugin, guint idt, GString *str)
{
FuGpioPlugin *self = FU_GPIO_PLUGIN(plugin);
for (guint i = 0; i < self->current_logical_ids->len; i++) {
const gchar *current_logical_id = g_ptr_array_index(self->current_logical_ids, i);
g_autofree gchar *title = g_strdup_printf("CurrentLogicalId[0x%02x]", i);
fwupd_codec_string_append(str, idt, title, current_logical_id);
}
}
static gboolean
fu_gpio_plugin_parse_level(const gchar *str, gboolean *ret, GError **error)
{
if (g_strcmp0(str, "high") == 0) {
*ret = TRUE;
return TRUE;
}
if (g_strcmp0(str, "low") == 0) {
*ret = FALSE;
return TRUE;
}
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"cannot parse level, got %s and expected high|low",
str);
return FALSE;
}
static gboolean
fu_gpio_plugin_process_quirk(FuPlugin *plugin, const gchar *str, GError **error)
{
FuGpioPlugin *self = FU_GPIO_PLUGIN(plugin);
FuDevice *device_tmp;
gboolean value = FALSE;
g_auto(GStrv) split = g_strsplit(str, ",", -1);
g_autoptr(FuDeviceLocker) locker = NULL;
/* sanity check */
if (g_strv_length(split) != 3) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"invalid format, CHIP_NAME,PIN_NAME,LEVEL, got '%s'",
str);
return FALSE;
}
if (!fu_gpio_plugin_parse_level(split[2], &value, error))
return FALSE;
device_tmp = fu_plugin_cache_lookup(plugin, split[0]);
if (device_tmp == NULL) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_FOUND,
"GPIO device %s not found",
split[0]);
return FALSE;
}
locker = fu_device_locker_new(device_tmp, error);
if (locker == NULL)
return FALSE;
if (!fu_gpio_device_assign(FU_GPIO_DEVICE(device_tmp), split[1], value, error)) {
g_prefix_error(error, "failed to assign %s: ", split[0]);
return FALSE;
}
/* success */
g_ptr_array_add(self->current_logical_ids, g_strdup(fu_device_get_logical_id(device_tmp)));
return TRUE;
}
static gboolean
fu_gpio_plugin_prepare(FuPlugin *self,
FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
GPtrArray *guids = fu_device_get_guids(device);
for (guint i = 0; i < guids->len; i++) {
const gchar *guid = g_ptr_array_index(guids, i);
const gchar *str;
str = fu_context_lookup_quirk_by_id(fu_plugin_get_context(self),
guid,
"GpioForUpdate");
if (str == NULL)
continue;
if (!fu_gpio_plugin_process_quirk(self, str, error))
return FALSE;
}
return TRUE;
}
static gboolean
fu_gpio_plugin_cleanup(FuPlugin *plugin,
FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuGpioPlugin *self = FU_GPIO_PLUGIN(plugin);
g_autoptr(GPtrArray) current_logical_ids = NULL;
/* deep copy to local to clear transaction array */
current_logical_ids =
g_ptr_array_copy(self->current_logical_ids, (GCopyFunc)g_strdup, NULL);
g_ptr_array_set_size(self->current_logical_ids, 0);
/* close the fds we opened during ->prepare */
for (guint i = 0; i < current_logical_ids->len; i++) {
FuDevice *device_tmp;
const gchar *current_logical_id = g_ptr_array_index(current_logical_ids, i);
device_tmp = fu_plugin_cache_lookup(plugin, current_logical_id);
if (device_tmp == NULL) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_FOUND,
"GPIO device %s no longer found",
current_logical_id);
return FALSE;
}
if (!fu_gpio_device_unassign(FU_GPIO_DEVICE(device_tmp), error)) {
g_prefix_error(error, "failed to unassign %s: ", current_logical_id);
return FALSE;
}
}
/* success */
return TRUE;
}
static void
fu_gpio_plugin_device_added(FuPlugin *self, FuDevice *device)
{
fu_plugin_cache_add(self, fu_device_get_logical_id(device), device);
}
static void
fu_gpio_plugin_init(FuGpioPlugin *self)
{
self->current_logical_ids = g_ptr_array_new_with_free_func(g_free);
}
static void
fu_gpio_plugin_constructed(GObject *obj)
{
FuPlugin *plugin = FU_PLUGIN(obj);
FuContext *ctx = fu_plugin_get_context(plugin);
fu_context_add_quirk_key(ctx, "GpioForUpdate");
fu_plugin_add_device_udev_subsystem(plugin, "gpio");
fu_plugin_add_device_gtype(plugin, FU_TYPE_GPIO_DEVICE);
}
static void
fu_gpio_plugin_finalize(GObject *obj)
{
FuGpioPlugin *self = FU_GPIO_PLUGIN(obj);
g_ptr_array_unref(self->current_logical_ids);
G_OBJECT_CLASS(fu_gpio_plugin_parent_class)->finalize(obj);
}
static void
fu_gpio_plugin_class_init(FuGpioPluginClass *klass)
{
FuPluginClass *plugin_class = FU_PLUGIN_CLASS(klass);
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = fu_gpio_plugin_finalize;
plugin_class->constructed = fu_gpio_plugin_constructed;
plugin_class->to_string = fu_gpio_plugin_to_string;
plugin_class->prepare = fu_gpio_plugin_prepare;
plugin_class->cleanup = fu_gpio_plugin_cleanup;
plugin_class->device_added = fu_gpio_plugin_device_added;
}
|