[PATCH] usb: dwc3: Use of_property_read_variable_u32_array() to read "power"

Rob Herring (Arm) posted 1 patch 1 year, 6 months ago
drivers/usb/dwc3/dwc3-octeon.c | 19 ++++---------------
1 file changed, 4 insertions(+), 15 deletions(-)
[PATCH] usb: dwc3: Use of_property_read_variable_u32_array() to read "power"
Posted by Rob Herring (Arm) 1 year, 6 months ago
There's no need to get the length of an DT array property before
parsing the array. of_property_read_variable_u32_array() takes a minimum
and maximum length and returns the actual length (or error code).

This is part of a larger effort to remove callers of of_find_property()
and similar functions. of_find_property() leaks the DT struct property
and data pointers which is a problem for dynamically allocated nodes
which may be freed.

Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
 drivers/usb/dwc3/dwc3-octeon.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)

diff --git a/drivers/usb/dwc3/dwc3-octeon.c b/drivers/usb/dwc3/dwc3-octeon.c
index 6010135e1acc..1a3b205367fd 100644
--- a/drivers/usb/dwc3/dwc3-octeon.c
+++ b/drivers/usb/dwc3/dwc3-octeon.c
@@ -419,7 +419,7 @@ static int dwc3_octeon_probe(struct platform_device *pdev)
 	int ref_clk_sel, ref_clk_fsel, mpll_mul;
 	int power_active_low, power_gpio;
 	int err, len;
-	u32 clock_rate;
+	u32 clock_rate, gpio_pwr[3];
 
 	if (of_property_read_u32(node, "refclk-frequency", &clock_rate)) {
 		dev_err(dev, "No UCTL \"refclk-frequency\"\n");
@@ -476,21 +476,10 @@ static int dwc3_octeon_probe(struct platform_device *pdev)
 
 	power_gpio = DWC3_GPIO_POWER_NONE;
 	power_active_low = 0;
-	if (of_find_property(node, "power", &len)) {
-		u32 gpio_pwr[3];
-
-		switch (len) {
-		case 8:
-			of_property_read_u32_array(node, "power", gpio_pwr, 2);
-			break;
-		case 12:
-			of_property_read_u32_array(node, "power", gpio_pwr, 3);
+	len = of_property_read_variable_u32_array(node, "power", gpio_pwr, 2, 3);
+	if (len > 0) {
+		if (len == 3)
 			power_active_low = gpio_pwr[2] & 0x01;
-			break;
-		default:
-			dev_err(dev, "invalid power configuration\n");
-			return -EINVAL;
-		}
 		power_gpio = gpio_pwr[1];
 	}
 
-- 
2.43.0
Re: [PATCH] usb: dwc3: Use of_property_read_variable_u32_array() to read "power"
Posted by Thinh Nguyen 1 year, 6 months ago
On Wed, Jul 31, 2024, Rob Herring (Arm) wrote:
> There's no need to get the length of an DT array property before
> parsing the array. of_property_read_variable_u32_array() takes a minimum
> and maximum length and returns the actual length (or error code).
> 
> This is part of a larger effort to remove callers of of_find_property()
> and similar functions. of_find_property() leaks the DT struct property
> and data pointers which is a problem for dynamically allocated nodes
> which may be freed.
> 
> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
> ---
>  drivers/usb/dwc3/dwc3-octeon.c | 19 ++++---------------
>  1 file changed, 4 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/dwc3-octeon.c b/drivers/usb/dwc3/dwc3-octeon.c
> index 6010135e1acc..1a3b205367fd 100644
> --- a/drivers/usb/dwc3/dwc3-octeon.c
> +++ b/drivers/usb/dwc3/dwc3-octeon.c
> @@ -419,7 +419,7 @@ static int dwc3_octeon_probe(struct platform_device *pdev)
>  	int ref_clk_sel, ref_clk_fsel, mpll_mul;
>  	int power_active_low, power_gpio;
>  	int err, len;
> -	u32 clock_rate;
> +	u32 clock_rate, gpio_pwr[3];
>  
>  	if (of_property_read_u32(node, "refclk-frequency", &clock_rate)) {
>  		dev_err(dev, "No UCTL \"refclk-frequency\"\n");
> @@ -476,21 +476,10 @@ static int dwc3_octeon_probe(struct platform_device *pdev)
>  
>  	power_gpio = DWC3_GPIO_POWER_NONE;
>  	power_active_low = 0;
> -	if (of_find_property(node, "power", &len)) {
> -		u32 gpio_pwr[3];
> -
> -		switch (len) {
> -		case 8:
> -			of_property_read_u32_array(node, "power", gpio_pwr, 2);
> -			break;
> -		case 12:
> -			of_property_read_u32_array(node, "power", gpio_pwr, 3);
> +	len = of_property_read_variable_u32_array(node, "power", gpio_pwr, 2, 3);
> +	if (len > 0) {
> +		if (len == 3)
>  			power_active_low = gpio_pwr[2] & 0x01;
> -			break;
> -		default:
> -			dev_err(dev, "invalid power configuration\n");
> -			return -EINVAL;
> -		}
>  		power_gpio = gpio_pwr[1];
>  	}
>  
> -- 
> 2.43.0
> 

Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

Thanks,
Thinh