2022-08-03 12:35:31

by Nipun Gupta

[permalink] [raw]
Subject: [RFC PATCH 2/2] driver core: add compatible string in sysfs for platform devices

For devices registered dynamically using platform_device_register
API, this patch exposes the sysfs entry for the compatible string.

Signed-off-by: Nipun Gupta <[email protected]>
---
drivers/base/platform.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 51bb2289865c..89949f88a0a1 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -1289,10 +1289,25 @@ static ssize_t driver_override_store(struct device *dev,
}
static DEVICE_ATTR_RW(driver_override);

+static ssize_t compatible_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ const char *compat;
+ int ret;
+
+ ret = device_property_read_string(dev, "compatible", &compat);
+ if (ret != 0)
+ return 0;
+
+ return sysfs_emit(buf, "%s", compat);
+}
+static DEVICE_ATTR_RO(compatible);
+
static struct attribute *platform_dev_attrs[] = {
&dev_attr_modalias.attr,
&dev_attr_numa_node.attr,
&dev_attr_driver_override.attr,
+ &dev_attr_compatible.attr,
NULL,
};

--
2.25.1



2022-08-03 12:35:53

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [RFC PATCH 2/2] driver core: add compatible string in sysfs for platform devices

On Wed, Aug 03, 2022 at 05:56:55PM +0530, Nipun Gupta wrote:
> For devices registered dynamically using platform_device_register
> API, this patch exposes the sysfs entry for the compatible string.
>
> Signed-off-by: Nipun Gupta <[email protected]>
> ---
> drivers/base/platform.c | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>
> diff --git a/drivers/base/platform.c b/drivers/base/platform.c
> index 51bb2289865c..89949f88a0a1 100644
> --- a/drivers/base/platform.c
> +++ b/drivers/base/platform.c
> @@ -1289,10 +1289,25 @@ static ssize_t driver_override_store(struct device *dev,
> }
> static DEVICE_ATTR_RW(driver_override);
>
> +static ssize_t compatible_show(struct device *dev, struct device_attribute *attr,
> + char *buf)
> +{
> + const char *compat;
> + int ret;
> +
> + ret = device_property_read_string(dev, "compatible", &compat);
> + if (ret != 0)
> + return 0;
> +
> + return sysfs_emit(buf, "%s", compat);
> +}
> +static DEVICE_ATTR_RO(compatible);

You forgot the Documentation/ABI/ update :(

Also, what happens if there is no such string to read? Why are you
returning 0? You should not create the attribute at all then, right?

thanks,

greg k-h

2022-08-03 14:02:54

by Nipun Gupta

[permalink] [raw]
Subject: RE: [RFC PATCH 2/2] driver core: add compatible string in sysfs for platform devices

[AMD Official Use Only - General]



> -----Original Message-----
> From: Greg KH <[email protected]>
> Sent: Wednesday, August 3, 2022 6:02 PM
> To: Gupta, Nipun <[email protected]>
> Cc: [email protected]; [email protected];
> [email protected]; [email protected]; [email protected]; [email protected];
> Anand, Harpreet <[email protected]>; Simek, Michal
> <[email protected]>; Agarwal, Nikhil <[email protected]>
> Subject: Re: [RFC PATCH 2/2] driver core: add compatible string in sysfs for
> platform devices
>
> [CAUTION: External Email]
>
> On Wed, Aug 03, 2022 at 05:56:55PM +0530, Nipun Gupta wrote:
> > For devices registered dynamically using platform_device_register
> > API, this patch exposes the sysfs entry for the compatible string.
> >
> > Signed-off-by: Nipun Gupta <[email protected]>
> > ---
> > drivers/base/platform.c | 15 +++++++++++++++
> > 1 file changed, 15 insertions(+)
> >
> > diff --git a/drivers/base/platform.c b/drivers/base/platform.c
> > index 51bb2289865c..89949f88a0a1 100644
> > --- a/drivers/base/platform.c
> > +++ b/drivers/base/platform.c
> > @@ -1289,10 +1289,25 @@ static ssize_t driver_override_store(struct device
> *dev,
> > }
> > static DEVICE_ATTR_RW(driver_override);
> >
> > +static ssize_t compatible_show(struct device *dev, struct device_attribute
> *attr,
> > + char *buf)
> > +{
> > + const char *compat;
> > + int ret;
> > +
> > + ret = device_property_read_string(dev, "compatible", &compat);
> > + if (ret != 0)
> > + return 0;
> > +
> > + return sysfs_emit(buf, "%s", compat);
> > +}
> > +static DEVICE_ATTR_RO(compatible);
>
> You forgot the Documentation/ABI/ update :(

Thanks for pointing. Will be adding the new sysfs entry as part
of the documentation.

>
> Also, what happens if there is no such string to read? Why are you
> returning 0? You should not create the attribute at all then, right?

Will be updating platform_dev_attrs_visible() API to have it visible
depending on if the attribute is available. Yes, then we may not need
this check here and would be part of the platform_dev_attrs_visible() API.

Thanks,
Nipun

>
> thanks,
>
> greg k-h