mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mario Limonciello <superm1@kernel.org>
To: "Kurt Borja" <kuurtb@gmail.com>,
	"Hans de Goede" <hdegoede@redhat.com>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Thomas Weißschuh" <linux@weissschuh.net>,
	"Joshua Grisham" <josh@joshuagrisham.com>,
	"Mark Pearson" <mpearson-lenovo@squebb.ca>,
	"Armin Wolf" <W_Armin@gmx.de>
Cc: Antheas Kapenekakis <lkml@antheas.dev>,
	"Derek J. Clark" <derekjohn.clark@gmail.com>,
	Prasanth Ksr <prasanth.ksr@dell.com>,
	Jorge Lopez <jorge.lopez2@hp.com>,
	platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org, Dell.Client.Kernel@dell.com
Subject: Re: [PATCH RFC 1/5] platform/x86: firmware_attributes_class: Add device initialization methods
Date: Fri, 9 May 2025 10:59:42 -0500	[thread overview]
Message-ID: <907d0022-0512-4a60-9d57-26b48c986df0@kernel.org> (raw)
In-Reply-To: <20250509-fw-attrs-api-v1-1-258afed65bfa@gmail.com>

On 5/9/2025 2:48 AM, Kurt Borja wrote:
> From: Thomas Weißschuh <linux@weissschuh.net>
> 
> Currently each user of firmware_attributes_class has to manually set up
> kobjects, devices, etc.
> 
> Provide this infrastructure out-of-the-box through the newly introduced
> fwat_device_register().
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> Co-developed-by: Kurt Borja <kuurtb@gmail.com>
> Signed-off-by: Kurt Borja <kuurtb@gmail.com>
Reviewed-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>   drivers/platform/x86/firmware_attributes_class.c | 165 +++++++++++++++++++++++
>   drivers/platform/x86/firmware_attributes_class.h |  44 ++++++
>   2 files changed, 209 insertions(+)
> 
> diff --git a/drivers/platform/x86/firmware_attributes_class.c b/drivers/platform/x86/firmware_attributes_class.c
> index 736e96c186d9dc6d945517f090e9af903e93bbf4..58ab1495ba3bd449cfe17de2827a57a0c5937788 100644
> --- a/drivers/platform/x86/firmware_attributes_class.c
> +++ b/drivers/platform/x86/firmware_attributes_class.c
> @@ -2,7 +2,12 @@
>   
>   /* Firmware attributes class helper module */
>   
> +#include <linux/device.h>
> +#include <linux/device/class.h>
> +#include <linux/kobject.h>
>   #include <linux/module.h>
> +#include <linux/slab.h>
> +#include <linux/types.h>
>   #include "firmware_attributes_class.h"
>   
>   const struct class firmware_attributes_class = {
> @@ -10,6 +15,164 @@ const struct class firmware_attributes_class = {
>   };
>   EXPORT_SYMBOL_GPL(firmware_attributes_class);
>   
> +static ssize_t fwat_attrs_kobj_show(struct kobject *kobj, struct attribute *attr,
> +				    char *buf)
> +{
> +	const struct fwat_attribute *fattr = to_fwat_attribute(attr);
> +	struct fwat_device *fadev = to_fwat_device(kobj);
> +
> +	if (!fattr->show)
> +		return -ENOENT;
> +
> +	return fattr->show(fadev->dev, fattr, buf);
> +}
> +
> +static ssize_t fwat_attrs_kobj_store(struct kobject *kobj, struct attribute *attr,
> +				     const char *buf, size_t count)
> +{
> +	const struct fwat_attribute *fattr = to_fwat_attribute(attr);
> +	struct fwat_device *fadev = to_fwat_device(kobj);
> +
> +	if (!fattr->store)
> +		return -ENOENT;
> +
> +	return fattr->store(fadev->dev, fattr, buf, count);
> +}
> +
> +static const struct sysfs_ops fwat_attrs_kobj_ops = {
> +	.show	= fwat_attrs_kobj_show,
> +	.store	= fwat_attrs_kobj_store,
> +};
> +
> +static void fwat_attrs_kobj_release(struct kobject *kobj)
> +{
> +	struct fwat_device *fadev = to_fwat_device(kobj);
> +
> +	kfree(fadev);
> +}
> +
> +static const struct kobj_type fwat_attrs_ktype = {
> +	.sysfs_ops	= &fwat_attrs_kobj_ops,
> +	.release	= fwat_attrs_kobj_release,
> +};
> +
> +/**
> + * fwat_device_register - Create and register a firmware-attributes class
> + *			  device
> + * @parent: Parent device
> + * @name: Name of the class device
> + * @data: Drvdata of the class device
> + * @groups: Sysfs groups for the custom `fwat_attrs_ktype` kobj_type
> + *
> + * NOTE: @groups are attached to the .attrs_kobj of the new fwat_device which
> + * has a custom ktype, which makes use of `struct fwat_attribute` to embed
> + * attributes.
> + *
> + * Return: pointer to the new fwat_device on success, ERR_PTR on failure
> + */
> +struct fwat_device *
> +fwat_device_register(struct device *parent, const char *name, void *data,
> +		     const struct attribute_group **groups)
> +{
> +	struct fwat_device *fadev;
> +	struct device *dev;
> +	int ret;
> +
> +	if (!parent || !name)
> +		return ERR_PTR(-EINVAL);
> +
> +	fadev = kzalloc(sizeof(*fadev), GFP_KERNEL);
> +	if (!fadev)
> +		return ERR_PTR(-ENOMEM);
> +
> +	dev = device_create(&firmware_attributes_class, parent, MKDEV(0, 0),
> +			    data, "%s", name);
> +	if (IS_ERR(dev)) {
> +		kfree(fadev);
> +		return ERR_CAST(dev);
> +	}
> +
> +	ret = kobject_init_and_add(&fadev->attrs_kobj, &fwat_attrs_ktype, &dev->kobj,
> +				   "attributes");
> +	if (ret)
> +		goto out_kobj_put;
> +
> +	if (groups) {
> +		ret = sysfs_create_groups(&fadev->attrs_kobj, groups);
> +		if (ret)
> +			goto out_kobj_unregister;
> +	}
> +
> +	fadev->dev = dev;
> +	fadev->groups = groups;
> +
> +	kobject_uevent(&fadev->attrs_kobj, KOBJ_ADD);
> +
> +	return fadev;
> +
> +out_kobj_unregister:
> +	kobject_del(&fadev->attrs_kobj);
> +
> +out_kobj_put:
> +	kobject_put(&fadev->attrs_kobj);
> +	device_unregister(dev);
> +
> +	return ERR_PTR(ret);
> +}
> +EXPORT_SYMBOL_GPL(fwat_device_register);
> +
> +void fwat_device_unregister(struct fwat_device *fwadev)
> +{
> +	if (fwadev->groups)
> +		sysfs_remove_groups(&fwadev->attrs_kobj, fwadev->groups);
> +	kobject_del(&fwadev->attrs_kobj);
> +	kobject_put(&fwadev->attrs_kobj);
> +	device_unregister(fwadev->dev);
> +}
> +EXPORT_SYMBOL_GPL(fwat_device_unregister);
> +
> +static void devm_fwat_device_release(void *data)
> +{
> +	struct fwat_device *fadev = data;
> +
> +	fwat_device_unregister(fadev);
> +}
> +
> +/**
> + * devm_fwat_device_register - Create and register a firmware-attributes class
> + *			       device
> + * @parent: Parent device
> + * @name: Name of the class device
> + * @data: Drvdata of the class device
> + * @groups: Sysfs groups for the custom `fwat_attrs_ktype` kobj_type
> + *
> + * Device managed version of fwat_device_register().
> + *
> + * NOTE: @groups are attached to the .attrs_kobj of the new fwat_device which
> + * has a custom ktype, which makes use of `struct fwat_attribute` to embed
> + * attributes.
> + *
> + * Return: pointer to the new fwat_device on success, ERR_PTR on failure
> + */
> +struct fwat_device *
> +devm_fwat_device_register(struct device *parent, const char *name, void *data,
> +			  const struct attribute_group **groups)
> +{
> +	struct fwat_device *fadev;
> +	int ret;
> +
> +	fadev = fwat_device_register(parent, name, data, groups);
> +	if (IS_ERR(fadev))
> +		return fadev;
> +
> +	ret = devm_add_action_or_reset(parent, devm_fwat_device_release, fadev);
> +	if (ret)
> +		return ERR_PTR(ret);
> +
> +	return fadev;
> +}
> +EXPORT_SYMBOL_GPL(devm_fwat_device_register);
> +
>   static __init int fw_attributes_class_init(void)
>   {
>   	return class_register(&firmware_attributes_class);
> @@ -23,5 +186,7 @@ static __exit void fw_attributes_class_exit(void)
>   module_exit(fw_attributes_class_exit);
>   
>   MODULE_AUTHOR("Mark Pearson <markpearson@lenovo.com>");
> +MODULE_AUTHOR("Thomas Weißschuh <linux@weissschuh.net>");
> +MODULE_AUTHOR("Kurt Borja <kuurtb@gmail.com>");
>   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..ad94bf91e5af30a2b8feb9abf224ee6f0d17600a 100644
> --- a/drivers/platform/x86/firmware_attributes_class.h
> +++ b/drivers/platform/x86/firmware_attributes_class.h
> @@ -5,8 +5,52 @@
>   #ifndef FW_ATTR_CLASS_H
>   #define FW_ATTR_CLASS_H
>   
> +#include <linux/container_of.h>
> +#include <linux/device.h>
>   #include <linux/device/class.h>
> +#include <linux/kobject.h>
> +#include <linux/sysfs.h>
>   
>   extern const struct class firmware_attributes_class;
>   
> +/**
> + * struct fwat_device - The firmware-attributes device
> + * @dev: The class device.
> + * @attrs_kobj: The "attributes" root kobject.
> + * @groups: Sysfs groups attached to the @attrs_kobj.
> + */
> +struct fwat_device {
> +	struct device *dev;
> +	struct kobject attrs_kobj;
> +	const struct attribute_group **groups;
> +};
> +
> +#define to_fwat_device(_k)	container_of_const(_k, struct fwat_device, attrs_kobj)
> +
> +/**
> + * struct fwat_attribute - The firmware-attributes's custom attribute
> + * @attr: Embedded struct attribute.
> + * @show: Show method called by the "attributes" kobject's ktype.
> + * @store: Store method called by the "attributes" kobject's ktype.
> + */
> +struct fwat_attribute {
> +	struct attribute attr;
> +	ssize_t (*show)(struct device *dev, const struct fwat_attribute *attr,
> +			char *buf);
> +	ssize_t (*store)(struct device *dev, const struct fwat_attribute *attr,
> +			 const char *buf, size_t count);
> +};
> +
> +#define to_fwat_attribute(_a) container_of_const(_a, struct fwat_attribute, attr)
> +
> +struct fwat_device * __must_check
> +fwat_device_register(struct device *parent, const char *name, void *data,
> +		     const struct attribute_group **groups);
> +
> +void fwat_device_unregister(struct fwat_device *fwadev);
> +
> +struct fwat_device * __must_check
> +devm_fwat_device_register(struct device *parent, const char *name, void *data,
> +			  const struct attribute_group **groups);
> +
>   #endif /* FW_ATTR_CLASS_H */
> 


  reply	other threads:[~2025-05-09 15:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-09  7:48 [PATCH RFC 0/5] platform/x86: firmware_attributes_class: Add a high level API Kurt Borja
2025-05-09  7:48 ` [PATCH RFC 1/5] platform/x86: firmware_attributes_class: Add device initialization methods Kurt Borja
2025-05-09 15:59   ` Mario Limonciello [this message]
2025-05-09  7:48 ` [PATCH RFC 2/5] platform/x86: firmware_attributes_class: Add a high level API Kurt Borja
2025-05-09 15:58   ` Mario Limonciello
2025-05-09 19:56     ` Kurt Borja
2025-05-09 20:00       ` Mario Limonciello
2025-05-09  7:48 ` [PATCH RFC 3/5] platform/x86: firmware_attributes_class: Add a boolean type Kurt Borja
2025-05-09  7:48 ` [PATCH RFC 4/5] MAINTAINERS: Add FIRMWARE ATTRIBUTES CLASS entry Kurt Borja
2025-05-09  7:48 ` [PATCH RFC 5/5] platform/x86: samsung-galaxybook: Transition to new firmware_attributes API Kurt Borja
2025-05-12  7:12   ` Thomas Weißschuh
2025-05-12 19:39     ` Kurt Borja

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=907d0022-0512-4a60-9d57-26b48c986df0@kernel.org \
    --to=superm1@kernel.org \
    --cc=Dell.Client.Kernel@dell.com \
    --cc=W_Armin@gmx.de \
    --cc=derekjohn.clark@gmail.com \
    --cc=hdegoede@redhat.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=jorge.lopez2@hp.com \
    --cc=josh@joshuagrisham.com \
    --cc=kuurtb@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=lkml@antheas.dev \
    --cc=mpearson-lenovo@squebb.ca \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=prasanth.ksr@dell.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®