From patchwork Tue Jan 7 17:05:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13929405 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BC4B18A943; Tue, 7 Jan 2025 17:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736269557; cv=none; b=CRg3y5UapDnx9SixZlddXarZMAqlSakvD9EKh5WKrsZKy7+viKEZ4a2s3r00r7HC351TeJJxQRjb6cH0unH6Cjjfb67SaeIHfRyniTzAH1HHQWXZ6eGsUW9MEa8iqBHFrOltawJz3IQ9uKcQogLxb8xyGzds3iyJaUV2pLZMecM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736269557; c=relaxed/simple; bh=D9e3IIpmKAdeBMfMaTPg+oFMfCvFGt0p2+6XpKnJvIw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DQJlqv9P1vdqv99MVOZ+eM69kDrEKucr2sMgiYy3UzXfPVUek3SX0A1+HscM+6UX18mlETpxRedVLeuOd3605CdW/2tNLNOsVhAmVVa796iXfpfS5l7IvnZgHM67Hfz7ytlPFUwueeJHRn8RFd8AMZSD8Zl8dtm87ZUOMJhTO8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=d8KBKxYF; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="d8KBKxYF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1736269549; bh=D9e3IIpmKAdeBMfMaTPg+oFMfCvFGt0p2+6XpKnJvIw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=d8KBKxYF6VYJ302vPrEqMcWBhmE5PdIIDJHLyEhmre4De391y21wy2LYmjSSujd0N u3b5d+scTof4JxlaraXFOEw8TvoeeBMbBxUxeQCmIoo52cN7rxWNxCsuzPXLxuucxb wlHqYpAcIrgXpBtHoo2IUOPTvfC7ZiVWhXli2aDQ= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Tue, 07 Jan 2025 18:05:37 +0100 Subject: [PATCH 1/2] platform/x86: firmware_attributes_class: Provide a highlevel interface Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250107-pdx86-firmware-attributes-v1-1-9d75c04a3b52@weissschuh.net> References: <20250107-pdx86-firmware-attributes-v1-0-9d75c04a3b52@weissschuh.net> In-Reply-To: <20250107-pdx86-firmware-attributes-v1-0-9d75c04a3b52@weissschuh.net> To: Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Armin Wolf Cc: platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Joshua Grisham , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1736269549; l=8103; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=D9e3IIpmKAdeBMfMaTPg+oFMfCvFGt0p2+6XpKnJvIw=; b=6luckhMX8m4J4XkKBQXtnYp7B7c3XatvaF4x62UtbPuQLNpFFMgE0OSV2WdE6thqPNY2hrnMM uSVzWEjCnV9CtriJAY3PUATVdX10ZKhevxpjnmdo68v2uDSwtWndIQw X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Currently each user of firmware_attributes_class has to manually set up kobjects, devices, etc. Provide a higher level API which takes care of the low-level details. Signed-off-by: Thomas Weißschuh --- drivers/platform/x86/firmware_attributes_class.c | 146 +++++++++++++++++++++++ drivers/platform/x86/firmware_attributes_class.h | 37 ++++++ 2 files changed, 183 insertions(+) diff --git a/drivers/platform/x86/firmware_attributes_class.c b/drivers/platform/x86/firmware_attributes_class.c index 736e96c186d9dc6d945517f090e9af903e93bbf4..70ceae5215820098b017bfda991a3c2a7824c98e 100644 --- a/drivers/platform/x86/firmware_attributes_class.c +++ b/drivers/platform/x86/firmware_attributes_class.c @@ -2,6 +2,9 @@ /* Firmware attributes class helper module */ +#include +#include +#include #include #include "firmware_attributes_class.h" @@ -22,6 +25,149 @@ static __exit void fw_attributes_class_exit(void) } module_exit(fw_attributes_class_exit); +static ssize_t fw_attributes_sysfs_show(struct kobject *kobj, struct attribute *attr, char *buf) +{ + struct firmware_attributes_device *fwadev = to_firmware_attribute_device(kobj); + const struct firmware_attribute *fw_attr = to_firmware_attribute(attr); + + if (!fw_attr->show) + return -EIO; + + return fw_attr->show(fwadev, fw_attr, buf); +} + +static ssize_t fw_attributes_sysfs_store(struct kobject *kobj, struct attribute *attr, + const char *buf, size_t count) +{ + struct firmware_attributes_device *fwadev = to_firmware_attribute_device(kobj); + const struct firmware_attribute *fw_attr = to_firmware_attribute(attr); + + if (!fw_attr->store) + return -EIO; + + return fw_attr->store(fwadev, fw_attr, buf, count); +} + +static const struct sysfs_ops fw_attributes_sysfs_ops = { + .show = fw_attributes_sysfs_show, + .store = fw_attributes_sysfs_store, +}; + +static void fw_attributes_attr_release(struct kobject *kobj) +{ + struct firmware_attributes_device *fwadev = to_firmware_attribute_device(kobj); + struct device *cdev; + + cdev = fwadev->dev; + + kfree(fwadev); + device_unregister(cdev); +} + +static const struct kobj_type fw_attributes_attr_type = { + .sysfs_ops = &fw_attributes_sysfs_ops, + .release = fw_attributes_attr_release, +}; + +DEFINE_FREE(firmware_attributes_device_unregister, struct firmware_attributes_device *, + if (_T) firmware_attributes_device_unregister(_T)) + +struct firmware_attributes_device * +firmware_attributes_device_register(struct device *parent, const char *name, + const struct attribute_group **groups, void *data) +{ + struct firmware_attributes_device *fwadev = NULL; + struct device *cdev = NULL; + int ret; + + fwadev = kzalloc(sizeof(*fwadev), GFP_KERNEL); + if (!fwadev) + return ERR_PTR(-ENOMEM); + + cdev = device_create(&firmware_attributes_class, parent, MKDEV(0, 0), "%s", name); + if (IS_ERR(cdev)) + return ERR_CAST(cdev); + + fwadev->data = data; + fwadev->dev = cdev; + + ret = kobject_init_and_add(&fwadev->attributes, &fw_attributes_attr_type, &cdev->kobj, + "attributes"); + if (ret) { + device_del(cdev); + return ERR_PTR(ret); + } + + if (groups) { + ret = sysfs_create_groups(&fwadev->attributes, groups); + if (ret) { + firmware_attributes_device_unregister(fwadev); + return ERR_PTR(ret); + } + + kobject_uevent(&fwadev->dev->kobj, KOBJ_CHANGE); + } + + return fwadev; +} +EXPORT_SYMBOL_GPL(firmware_attributes_device_register); + +void firmware_attributes_device_unregister(struct firmware_attributes_device *fwadev) +{ + kobject_del(&fwadev->attributes); + kobject_put(&fwadev->attributes); +} +EXPORT_SYMBOL_GPL(firmware_attributes_device_unregister); + +static void devm_firmware_attributes_device_release(void *data) +{ + struct firmware_attributes_device *fwadev = data; + + firmware_attributes_device_unregister(fwadev); +} + +struct firmware_attributes_device * +devm_firmware_attributes_device_register(struct device *parent, const char *name, + const struct attribute_group **groups, void *data) +{ + struct firmware_attributes_device *fwadev; + int ret; + + fwadev = firmware_attributes_device_register(parent, name, groups, data); + if (IS_ERR(fwadev)) + return fwadev; + + ret = devm_add_action_or_reset(parent, devm_firmware_attributes_device_release, fwadev); + if (ret) + return ERR_PTR(ret); + + return fwadev; +} +EXPORT_SYMBOL_GPL(devm_firmware_attributes_device_register); + +static ssize_t firmware_attributes_type_show(struct firmware_attributes_device *fwadev, + const struct firmware_attribute *attr, char *buf) +{ + if (attr == &_firmware_attribute_type_string) + return sysfs_emit(buf, "string\n"); + else if (attr == &_firmware_attribute_type_enumeration) + return sysfs_emit(buf, "enumeration\n"); + else if (attr == &_firmware_attribute_type_integer) + return sysfs_emit(buf, "integer\n"); + else + return -EIO; +} + +#define __FW_TYPE_ATTR __ATTR(type, 0444, firmware_attributes_type_show, NULL) + +const struct firmware_attribute _firmware_attribute_type_string = __FW_TYPE_ATTR; +EXPORT_SYMBOL_GPL(_firmware_attribute_type_string); +const struct firmware_attribute _firmware_attribute_type_enumeration = __FW_TYPE_ATTR; +EXPORT_SYMBOL_GPL(_firmware_attribute_type_enumeration); +const struct firmware_attribute _firmware_attribute_type_integer = __FW_TYPE_ATTR; +EXPORT_SYMBOL_GPL(_firmware_attribute_type_integer); + MODULE_AUTHOR("Mark Pearson "); +MODULE_AUTHOR("Thomas Weißschuh "); MODULE_DESCRIPTION("Firmware attributes class helper module"); MODULE_LICENSE("GPL"); diff --git a/drivers/platform/x86/firmware_attributes_class.h b/drivers/platform/x86/firmware_attributes_class.h index d27abe54fcf9812a2f0868eec5426bbc8e7eb21c..66837ad9f65b8ca501dee73f48c01f2710d86bf5 100644 --- a/drivers/platform/x86/firmware_attributes_class.h +++ b/drivers/platform/x86/firmware_attributes_class.h @@ -5,8 +5,45 @@ #ifndef FW_ATTR_CLASS_H #define FW_ATTR_CLASS_H +#include #include +#include extern const struct class firmware_attributes_class; +struct firmware_attributes_device { + struct device *dev; + struct kobject attributes; + void *data; +}; + +struct firmware_attribute { + struct attribute attr; + ssize_t (*show)(struct firmware_attributes_device *fwadev, + const struct firmware_attribute *attr, char *buf); + ssize_t (*store)(struct firmware_attributes_device *fwadev, + const struct firmware_attribute *attr, const char *buf, size_t count); +}; + +#define to_firmware_attribute(_a) container_of_const(_a, struct firmware_attribute, attr) +#define to_firmware_attribute_device(_s) \ + container_of_const(_s, struct firmware_attributes_device, attributes) + +extern const struct firmware_attribute _firmware_attribute_type_string; +#define firmware_attribute_type_string ((struct attribute *)&_firmware_attribute_type_string.attr) +extern const struct firmware_attribute _firmware_attribute_type_enumeration; +#define firmware_attribute_type_enumeration ((struct attribute *)&_firmware_attribute_type_enumeration.attr) +extern const struct firmware_attribute _firmware_attribute_type_integer; +#define firmware_attribute_type_integer ((struct attribute *)&_firmware_attribute_type_integer.attr) + +struct firmware_attributes_device * __must_check +firmware_attributes_device_register(struct device *parent, const char *name, + const struct attribute_group **groups, void *data); + +void firmware_attributes_device_unregister(struct firmware_attributes_device *fwadev); + +struct firmware_attributes_device * __must_check +devm_firmware_attributes_device_register(struct device *parent, const char *name, + const struct attribute_group **groups, void *data); + #endif /* FW_ATTR_CLASS_H */ From patchwork Tue Jan 7 17:05:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13929404 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BD00199236; Tue, 7 Jan 2025 17:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736269556; cv=none; b=NGNAERxpnYMKMAKo/HwV0lf8I21XzSYUoOYTz7EheRoEpuMuq6j7F6x2jjNGO+3ei/hTbY05ZeWjdEngOSZrIwTzEdgkE3bQUWiAdL6LgXfpUKwDemDS+gvkCJ548WpM6bCwIwzQihWh1ot+JhZqN5CFWEMmENCmvGo3b/YJzAc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736269556; c=relaxed/simple; bh=v8v2bwASK20xizpHeRN3fIY6330Ur6fWSwRWF082iT8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mrZfOnMeS+zod00eyTy5aXIHEjJY8CmsOoR/OaXbFf+qhFw8HTKfEU1zoySH2wqPwuZ7z+/cu+tON62nw7dhkSKbj+/5V8Y/k7SCvp7kqU+ii1IdgJ1B4qOaViL1FRjUeQVyEllqXHThUlzp7wESmnkNiyctKnRsbbom36IdTBk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=Bm/ndyVZ; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="Bm/ndyVZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1736269549; bh=v8v2bwASK20xizpHeRN3fIY6330Ur6fWSwRWF082iT8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Bm/ndyVZZSUnbd9yyH/un0AI8ox80kMeF5Hgi/XSjFSGLxG3JZjzOX/XQUMsD7i1x U8nQktalOT9oFtZLF7fw5YutT0s4nffDdgXqN0PVC6JiQ5B7/jpCBmYaMCRij1Pry6 EWQave0P5qb5ZRSQsUVjfwFCMaMMrSQRoJSKWVpU= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Tue, 07 Jan 2025 18:05:38 +0100 Subject: [PATCH 2/2] platform/x86: firmware_attributes_class: Add test driver Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250107-pdx86-firmware-attributes-v1-2-9d75c04a3b52@weissschuh.net> References: <20250107-pdx86-firmware-attributes-v1-0-9d75c04a3b52@weissschuh.net> In-Reply-To: <20250107-pdx86-firmware-attributes-v1-0-9d75c04a3b52@weissschuh.net> To: Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Armin Wolf Cc: platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Joshua Grisham , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1736269549; l=4401; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=v8v2bwASK20xizpHeRN3fIY6330Ur6fWSwRWF082iT8=; b=V7yWoSkmBka4u4h8tJQhlCci/yAwcYoP/YKl6/0+QUR4JtrPRLKuDOR0U0KgdhyoqpSd7hxaT t4L1NuigaX3A5mahTxK7c7cB9L/k2UPavTXmTna6qfPGdEU829R+g13 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= The driver showcases the use of the new subsystem API. Signed-off-by: Thomas Weißschuh --- drivers/platform/x86/Kconfig | 12 ++++ drivers/platform/x86/Makefile | 1 + drivers/platform/x86/firmware_attributes_test.c | 78 +++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index 0258dd879d64be389f4dd9bc309fe089f23cc798..2a0e828657d2f07074944d6c42dc204fc8825a42 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig @@ -1065,6 +1065,18 @@ source "drivers/platform/x86/x86-android-tablets/Kconfig" config FW_ATTR_CLASS tristate +config FW_ATTR_TEST + tristate "Firmware attribute test driver" + select FW_ATTR_CLASS + help + This driver provides a test user of the firmware attribute subsystem. + + An instance is created at /sys/class/firmware-attributes/test/ + container various example attributes. + + To compile this driver as a module, choose M here: the module + will be called firmware_attributes_test. + config INTEL_IMR bool "Intel Isolated Memory Region support" depends on X86_INTEL_QUARK && IOSF_MBI diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile index e1b142947067475ee5472400a5a1cd20d79e12bd..610a1ca850a4353fd490e43b214a9e5872d2d28d 100644 --- a/drivers/platform/x86/Makefile +++ b/drivers/platform/x86/Makefile @@ -121,6 +121,7 @@ obj-$(CONFIG_TOPSTAR_LAPTOP) += topstar-laptop.o # Platform drivers obj-$(CONFIG_FW_ATTR_CLASS) += firmware_attributes_class.o +obj-$(CONFIG_FW_ATTR_TEST) += firmware_attributes_test.o obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE) += serial-multi-instantiate.o obj-$(CONFIG_MLX_PLATFORM) += mlx-platform.o obj-$(CONFIG_TOUCHSCREEN_DMI) += touchscreen_dmi.o diff --git a/drivers/platform/x86/firmware_attributes_test.c b/drivers/platform/x86/firmware_attributes_test.c new file mode 100644 index 0000000000000000000000000000000000000000..84f6a92e5163378c655f30ac022d513d7df5a18c --- /dev/null +++ b/drivers/platform/x86/firmware_attributes_test.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include "firmware_attributes_class.h" + +struct fw_attr_test_data { + char attr1_value[PAGE_SIZE]; +}; + +static ssize_t test_attr1_default_value_show(struct firmware_attributes_device *fwadev, + const struct firmware_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "default 1\n"); +} + +static struct firmware_attribute test_attr1_default_value = __ATTR(default_value, 0444, + test_attr1_default_value_show, + NULL); + +static ssize_t test_attr1_current_value_show(struct firmware_attributes_device *fwadev, + const struct firmware_attribute *attr, char *buf) +{ + struct fw_attr_test_data *data = fwadev->data; + + return sysfs_emit(buf, "%s\n", data->attr1_value); +} + +static ssize_t test_attr1_current_value_store(struct firmware_attributes_device *fwadev, + const struct firmware_attribute *attr, + const char *buf, size_t count) +{ + struct fw_attr_test_data *data = fwadev->data; + + return strscpy(data->attr1_value, buf); +} + +static struct firmware_attribute test_attr1_current_value = __ATTR(current_value, 0644, + test_attr1_current_value_show, + test_attr1_current_value_store); + +static struct attribute *test_attr1_attrs[] = { + firmware_attribute_type_string, + &test_attr1_default_value.attr, + &test_attr1_current_value.attr, + NULL +}; + +static const struct attribute_group test_attr1_group = { + .name = "attr1", + .attrs = test_attr1_attrs, +}; + +static const struct attribute_group *test_attr_groups[] = { + &test_attr1_group, + NULL +}; + +static struct firmware_attributes_device *fwadev; + +static int __init fw_test_init(void) +{ + static struct fw_attr_test_data data = { + .attr1_value = "attr1", + }; + + fwadev = firmware_attributes_device_register(NULL, "test", test_attr_groups, &data); + return PTR_ERR_OR_ZERO(fwadev); +} +module_init(fw_test_init); + +static void __exit fw_test_exit(void) +{ + firmware_attributes_device_unregister(fwadev); +} +module_exit(fw_test_exit); + +MODULE_LICENSE("GPL");