[PATCH] usb: ehci: replace scnprintf() with sysfs_emit()

Hendrik Hamerlinck posted 1 patch 3 months, 3 weeks ago
There is a newer version of this series
drivers/usb/host/ehci-sysfs.c | 15 +++++----------
1 file changed, 5 insertions(+), 10 deletions(-)
[PATCH] usb: ehci: replace scnprintf() with sysfs_emit()
Posted by Hendrik Hamerlinck 3 months, 3 weeks ago
Per Documentation/filesystems/sysfs.rst, show() methods should only
use sysfs_emit() or sysfs_emit_at() when formatting values to be
returned to userspace.

Convert the uses of scnprintf() in sysfs show() methods to
sysfs_emit() and sysfs_emit_at() for better safety and consistency.

Signed-off-by: Hendrik Hamerlinck <hendrik.hamerlinck@hammernet.be>
---
 drivers/usb/host/ehci-sysfs.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/host/ehci-sysfs.c b/drivers/usb/host/ehci-sysfs.c
index 8f75cb7b197c..3786e81b0ed9 100644
--- a/drivers/usb/host/ehci-sysfs.c
+++ b/drivers/usb/host/ehci-sysfs.c
@@ -12,21 +12,18 @@ static ssize_t companion_show(struct device *dev,
 			      char *buf)
 {
 	struct ehci_hcd		*ehci;
-	int			nports, index, n;
-	int			count = PAGE_SIZE;
-	char			*ptr = buf;
+	int			nports, index;
+	int			len = 0;
 
 	ehci = hcd_to_ehci(dev_get_drvdata(dev));
 	nports = HCS_N_PORTS(ehci->hcs_params);
 
 	for (index = 0; index < nports; ++index) {
 		if (test_bit(index, &ehci->companion_ports)) {
-			n = scnprintf(ptr, count, "%d\n", index + 1);
-			ptr += n;
-			count -= n;
+			len += sysfs_emit_at(buf, len, "%d\n", index + 1);
 		}
 	}
-	return ptr - buf;
+	return len;
 }
 
 /*
@@ -70,11 +67,9 @@ static ssize_t uframe_periodic_max_show(struct device *dev,
 					char *buf)
 {
 	struct ehci_hcd		*ehci;
-	int			n;
 
 	ehci = hcd_to_ehci(dev_get_drvdata(dev));
-	n = scnprintf(buf, PAGE_SIZE, "%d\n", ehci->uframe_periodic_max);
-	return n;
+	return sysfs_emit(buf, "%d\n", ehci->uframe_periodic_max);
 }
 
 
-- 
2.43.0
Re: [PATCH] usb: ehci: replace scnprintf() with sysfs_emit()
Posted by Christophe JAILLET 3 months, 2 weeks ago
Le 19/06/2025 à 14:07, Hendrik Hamerlinck a écrit :
> Per Documentation/filesystems/sysfs.rst, show() methods should only
> use sysfs_emit() or sysfs_emit_at() when formatting values to be
> returned to userspace.
> 
> Convert the uses of scnprintf() in sysfs show() methods to
> sysfs_emit() and sysfs_emit_at() for better safety and consistency.
> 
> Signed-off-by: Hendrik Hamerlinck <hendrik.hamerlinck@hammernet.be>
> ---
>   drivers/usb/host/ehci-sysfs.c | 15 +++++----------
>   1 file changed, 5 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/host/ehci-sysfs.c b/drivers/usb/host/ehci-sysfs.c
> index 8f75cb7b197c..3786e81b0ed9 100644
> --- a/drivers/usb/host/ehci-sysfs.c
> +++ b/drivers/usb/host/ehci-sysfs.c
> @@ -12,21 +12,18 @@ static ssize_t companion_show(struct device *dev,
>   			      char *buf)
>   {
>   	struct ehci_hcd		*ehci;
> -	int			nports, index, n;
> -	int			count = PAGE_SIZE;
> -	char			*ptr = buf;
> +	int			nports, index;
> +	int			len = 0;
>   
>   	ehci = hcd_to_ehci(dev_get_drvdata(dev));
>   	nports = HCS_N_PORTS(ehci->hcs_params);
>   
>   	for (index = 0; index < nports; ++index) {
>   		if (test_bit(index, &ehci->companion_ports)) {
> -			n = scnprintf(ptr, count, "%d\n", index + 1);
> -			ptr += n;
> -			count -= n;
> +			len += sysfs_emit_at(buf, len, "%d\n", index + 1);
>   		}

Nitpick: extra { } looks useless now.

>   	}
> -	return ptr - buf;
> +	return len;
>   }
>   
>   /*
> @@ -70,11 +67,9 @@ static ssize_t uframe_periodic_max_show(struct device *dev,
>   					char *buf)
>   {
>   	struct ehci_hcd		*ehci;
> -	int			n;
>   
>   	ehci = hcd_to_ehci(dev_get_drvdata(dev));
> -	n = scnprintf(buf, PAGE_SIZE, "%d\n", ehci->uframe_periodic_max);
> -	return n;
> +	return sysfs_emit(buf, "%d\n", ehci->uframe_periodic_max);
>   }
>   
>   

Re: [PATCH] usb: ehci: replace scnprintf() with sysfs_emit()
Posted by Hendrik Hammernet 3 months, 2 weeks ago
On 6/20/25 21:56, Christophe JAILLET wrote:
> Le 19/06/2025 à 14:07, Hendrik Hamerlinck a écrit :
>> Per Documentation/filesystems/sysfs.rst, show() methods should only
>> use sysfs_emit() or sysfs_emit_at() when formatting values to be
>> returned to userspace.
>>
>> Convert the uses of scnprintf() in sysfs show() methods to
>> sysfs_emit() and sysfs_emit_at() for better safety and consistency.
>>
>> Signed-off-by: Hendrik Hamerlinck <hendrik.hamerlinck@hammernet.be>
>> ---
>>   drivers/usb/host/ehci-sysfs.c | 15 +++++----------
>>   1 file changed, 5 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/usb/host/ehci-sysfs.c b/drivers/usb/host/ehci-sysfs.c
>> index 8f75cb7b197c..3786e81b0ed9 100644
>> --- a/drivers/usb/host/ehci-sysfs.c
>> +++ b/drivers/usb/host/ehci-sysfs.c
>> @@ -12,21 +12,18 @@ static ssize_t companion_show(struct device *dev,
>>                     char *buf)
>>   {
>>       struct ehci_hcd        *ehci;
>> -    int            nports, index, n;
>> -    int            count = PAGE_SIZE;
>> -    char            *ptr = buf;
>> +    int            nports, index;
>> +    int            len = 0;
>>         ehci = hcd_to_ehci(dev_get_drvdata(dev));
>>       nports = HCS_N_PORTS(ehci->hcs_params);
>>         for (index = 0; index < nports; ++index) {
>>           if (test_bit(index, &ehci->companion_ports)) {
>> -            n = scnprintf(ptr, count, "%d\n", index + 1);
>> -            ptr += n;
>> -            count -= n;
>> +            len += sysfs_emit_at(buf, len, "%d\n", index + 1);
>>           }
>
> Nitpick: extra { } looks useless now.

I'm fairly new to kernel development. I checked the coding style guide and
indeed saw that the curly braces should be avoided if they are unnecessary.

Thanks for pointing that out. I'll fix it in the next revision.

Re: [PATCH] usb: ehci: replace scnprintf() with sysfs_emit()
Posted by Alan Stern 3 months, 3 weeks ago
On Thu, Jun 19, 2025 at 02:07:11PM +0200, Hendrik Hamerlinck wrote:
> Per Documentation/filesystems/sysfs.rst, show() methods should only
> use sysfs_emit() or sysfs_emit_at() when formatting values to be
> returned to userspace.
> 
> Convert the uses of scnprintf() in sysfs show() methods to
> sysfs_emit() and sysfs_emit_at() for better safety and consistency.
> 
> Signed-off-by: Hendrik Hamerlinck <hendrik.hamerlinck@hammernet.be>
> ---

Reviewed-by: Alan Stern <stern@rowland.harvard.edu>