diff mbox series

[3/5] platform/x86: msi-ec: Filter out unsupported attributes

Message ID 20231010172037.611063-9-teackot@gmail.com (mailing list archive)
State Changes Requested, archived
Headers show
Series platform/x86: msi-ec: Add the first platform device attributes | expand

Commit Message

Nikita Kravets Oct. 10, 2023, 5:20 p.m. UTC
Filter the attributes and only create those which are supported by the
currently loaded configuration. The filtered attributes are saved in an
attribute group to be easily created and removed.
root_attrs_support is an array of all attributes and their support
conditions. fw_version and fw_release_date are supported on all models
so their condition is always true.

Cc: Aakash Singh <mail@singhaakash.dev>
Cc: Jose Angel Pastrana <japp0005@red.ujaen.es>
Signed-off-by: Nikita Kravets <teackot@gmail.com>
---
 drivers/platform/x86/msi-ec.c | 40 +++++++++++++++++++++++++++--------
 drivers/platform/x86/msi-ec.h |  5 +++++
 2 files changed, 36 insertions(+), 9 deletions(-)

Comments

Ilpo Järvinen Oct. 11, 2023, 12:46 p.m. UTC | #1
On Tue, 10 Oct 2023, Nikita Kravets wrote:

> Filter the attributes and only create those which are supported by the
> currently loaded configuration. The filtered attributes are saved in an
> attribute group to be easily created and removed.
> root_attrs_support is an array of all attributes and their support
> conditions. fw_version and fw_release_date are supported on all models
> so their condition is always true.
> 
> Cc: Aakash Singh <mail@singhaakash.dev>
> Cc: Jose Angel Pastrana <japp0005@red.ujaen.es>
> Signed-off-by: Nikita Kravets <teackot@gmail.com>
> ---
>  drivers/platform/x86/msi-ec.c | 40 +++++++++++++++++++++++++++--------
>  drivers/platform/x86/msi-ec.h |  5 +++++
>  2 files changed, 36 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/platform/x86/msi-ec.c b/drivers/platform/x86/msi-ec.c
> index 772b230fb47e..09472b21e093 100644
> --- a/drivers/platform/x86/msi-ec.c
> +++ b/drivers/platform/x86/msi-ec.c
> @@ -872,22 +872,44 @@ static ssize_t fw_release_date_show(struct device *device,
>  static DEVICE_ATTR_RO(fw_version);
>  static DEVICE_ATTR_RO(fw_release_date);
>  
> -static struct attribute *msi_root_attrs[] = {
> -	&dev_attr_fw_version.attr,
> -	&dev_attr_fw_release_date.attr,
> -	NULL
> -};
> -
> -static struct attribute_group msi_root_group = {
> -	.attrs = msi_root_attrs,
> -};
> +static struct attribute_group msi_root_group;
>  
>  /*
>   * Sysfs platform driver
>   */
>  
> +/*
> + * Copies supported attributes from `attributes` to `filtered`
> + */
> +static void filter_attributes(struct attribute_support *attributes,
> +			      struct attribute **filtered,
> +			      size_t size)
> +{
> +	for (int i = 0, j = 0; i < size; i++) {
> +		if (attributes[i].supported)
> +			filtered[j++] = attributes[i].attribute;
> +	}

Use .is_visible in the attribute group to toggle visibility.

> +}
> +
>  static int msi_platform_probe(struct platform_device *pdev)
>  {
> +	struct attribute_support root_attrs_support[] = {
> +		{
> +			&dev_attr_fw_version.attr,
> +			true,
> +		},
> +		{
> +			&dev_attr_fw_release_date.attr,
> +			true,
> +		},
> +	};
> +
> +	/* +1 to null-terminate the array */
> +	static struct attribute *root_attrs[ARRAY_SIZE(root_attrs_support) + 1] = {0};
> +
> +	filter_attributes(root_attrs_support, root_attrs, ARRAY_SIZE(root_attrs_support));
> +	msi_root_group.attrs = root_attrs;
> +
>  	return sysfs_create_group(&pdev->dev.kobj, &msi_root_group);
>  }
>  
> diff --git a/drivers/platform/x86/msi-ec.h b/drivers/platform/x86/msi-ec.h
> index be3533dc9cc6..f4198f0df5d9 100644
> --- a/drivers/platform/x86/msi-ec.h
> +++ b/drivers/platform/x86/msi-ec.h
> @@ -119,4 +119,9 @@ struct msi_ec_conf {
>  	struct msi_ec_kbd_bl_conf         kbd_bl;
>  };
>  
> +struct attribute_support {
> +	struct attribute *attribute;
> +	bool supported;
> +};
> +
>  #endif // _MSI_EC_H_
>
diff mbox series

Patch

diff --git a/drivers/platform/x86/msi-ec.c b/drivers/platform/x86/msi-ec.c
index 772b230fb47e..09472b21e093 100644
--- a/drivers/platform/x86/msi-ec.c
+++ b/drivers/platform/x86/msi-ec.c
@@ -872,22 +872,44 @@  static ssize_t fw_release_date_show(struct device *device,
 static DEVICE_ATTR_RO(fw_version);
 static DEVICE_ATTR_RO(fw_release_date);
 
-static struct attribute *msi_root_attrs[] = {
-	&dev_attr_fw_version.attr,
-	&dev_attr_fw_release_date.attr,
-	NULL
-};
-
-static struct attribute_group msi_root_group = {
-	.attrs = msi_root_attrs,
-};
+static struct attribute_group msi_root_group;
 
 /*
  * Sysfs platform driver
  */
 
+/*
+ * Copies supported attributes from `attributes` to `filtered`
+ */
+static void filter_attributes(struct attribute_support *attributes,
+			      struct attribute **filtered,
+			      size_t size)
+{
+	for (int i = 0, j = 0; i < size; i++) {
+		if (attributes[i].supported)
+			filtered[j++] = attributes[i].attribute;
+	}
+}
+
 static int msi_platform_probe(struct platform_device *pdev)
 {
+	struct attribute_support root_attrs_support[] = {
+		{
+			&dev_attr_fw_version.attr,
+			true,
+		},
+		{
+			&dev_attr_fw_release_date.attr,
+			true,
+		},
+	};
+
+	/* +1 to null-terminate the array */
+	static struct attribute *root_attrs[ARRAY_SIZE(root_attrs_support) + 1] = {0};
+
+	filter_attributes(root_attrs_support, root_attrs, ARRAY_SIZE(root_attrs_support));
+	msi_root_group.attrs = root_attrs;
+
 	return sysfs_create_group(&pdev->dev.kobj, &msi_root_group);
 }
 
diff --git a/drivers/platform/x86/msi-ec.h b/drivers/platform/x86/msi-ec.h
index be3533dc9cc6..f4198f0df5d9 100644
--- a/drivers/platform/x86/msi-ec.h
+++ b/drivers/platform/x86/msi-ec.h
@@ -119,4 +119,9 @@  struct msi_ec_conf {
 	struct msi_ec_kbd_bl_conf         kbd_bl;
 };
 
+struct attribute_support {
+	struct attribute *attribute;
+	bool supported;
+};
+
 #endif // _MSI_EC_H_