mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* problem configuring backlight brightness levels in device tree
@ 2016-07-11 11:51 giorgio.nicole
  2016-07-13  5:18 ` Sascha Hauer
  0 siblings, 1 reply; 10+ messages in thread
From: giorgio.nicole @ 2016-07-11 11:51 UTC (permalink / raw)
  To: barebox

Hi,

I was trying to configure a pwm backlight on an embedded arm board
and I think I've found a minor problem.

In the source file 'drivers/video/backlight-pwm.c', in the function pwm_backlight_parse_dt()
you set the number of brightness values to:

pwm_backlight->backlight.brightness_max = length / sizeof(u32);

and then loop through the array with:

for (i = 0; i <=  pwm_backlight->backlight.brightness_max; i++)

to find the max of the array.

I think the '<=' should be a '<' otherwise you access an uninitialized value
one past the end of the array; this actually does not directly crash barebox
but the whole brightness values are scaled wrong.

giorgio

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problem configuring backlight brightness levels in device tree
  2016-07-11 11:51 problem configuring backlight brightness levels in device tree giorgio.nicole
@ 2016-07-13  5:18 ` Sascha Hauer
  2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
  0 siblings, 1 reply; 10+ messages in thread
From: Sascha Hauer @ 2016-07-13  5:18 UTC (permalink / raw)
  To: giorgio.nicole; +Cc: barebox

On Mon, Jul 11, 2016 at 01:51:11PM +0200, giorgio.nicole@arcor.de wrote:
> Hi,
> 
> I was trying to configure a pwm backlight on an embedded arm board
> and I think I've found a minor problem.
> 
> In the source file 'drivers/video/backlight-pwm.c', in the function pwm_backlight_parse_dt()
> you set the number of brightness values to:
> 
> pwm_backlight->backlight.brightness_max = length / sizeof(u32);
> 
> and then loop through the array with:
> 
> for (i = 0; i <=  pwm_backlight->backlight.brightness_max; i++)
> 
> to find the max of the array.
> 
> I think the '<=' should be a '<' otherwise you access an uninitialized value
> one past the end of the array; this actually does not directly crash barebox
> but the whole brightness values are scaled wrong.

Yes, you're right. Can you send a patch for this?

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 0/4] fixes some problems found in the backligth-pwm implementation.
  2016-07-13  5:18 ` Sascha Hauer
@ 2016-07-14 14:17   ` iw3gtf
  2016-07-14 14:17     ` [PATCH 1/4] video/backlight-pwm: fixed a loop index going out of range iw3gtf
                       ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: iw3gtf @ 2016-07-14 14:17 UTC (permalink / raw)
  To: barebox

This serie fixes some problems found in the backligth-pwm implementation.

root (4):
  video/backlight-pwm: fixed a loop index going out of range.
  video/backlight-pwm: fix the value of 'brightness_max'.
  video/backlight-pwm: code readability improvement.
  video/backlight-pwm: properly handle the case of an empty
    'brightness-levels' in the device tree.

 drivers/video/backlight-pwm.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

-- 
2.9.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/4] video/backlight-pwm: fixed a loop index going out of range.
  2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
@ 2016-07-14 14:17     ` iw3gtf
  2016-07-14 14:17     ` [PATCH 2/4] video/backlight-pwm: fix the value of 'brightness_max' iw3gtf
                       ` (4 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: iw3gtf @ 2016-07-14 14:17 UTC (permalink / raw)
  To: barebox

In the function pwm_backlight_parse_dt() the last iteration of the for
loop accessed memory past the end of the array 'pwm_backlight->levels[]'
because of a wrong test ( '<=' instead of '<').
---
 drivers/video/backlight-pwm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
index 91435f8..3024ec6 100644
--- a/drivers/video/backlight-pwm.c
+++ b/drivers/video/backlight-pwm.c
@@ -150,7 +150,7 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 		if (ret < 0)
 			return ret;
 
-		for (i = 0; i <=  pwm_backlight->backlight.brightness_max; i++)
+		for (i = 0; i < pwm_backlight->backlight.brightness_max; i++)
 			if (pwm_backlight->levels[i] > pwm_backlight->scale)
 				pwm_backlight->scale = pwm_backlight->levels[i];
 
-- 
2.9.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 2/4] video/backlight-pwm: fix the value of 'brightness_max'.
  2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
  2016-07-14 14:17     ` [PATCH 1/4] video/backlight-pwm: fixed a loop index going out of range iw3gtf
@ 2016-07-14 14:17     ` iw3gtf
  2016-07-14 14:17     ` [PATCH 3/4] video/backlight-pwm: code readability improvement iw3gtf
                       ` (3 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: iw3gtf @ 2016-07-14 14:17 UTC (permalink / raw)
  To: barebox

The field pwm_backlight->backlight.brightness_max should be the maximum
allowed brightness value for the backlight, not the max index of the
array 'pwm_backlight->levels[]'.
---
 drivers/video/backlight-pwm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
index 3024ec6..53cf4e9 100644
--- a/drivers/video/backlight-pwm.c
+++ b/drivers/video/backlight-pwm.c
@@ -160,7 +160,7 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 			return ret;
 
 		pwm_backlight->backlight.brightness_default = value;
-		pwm_backlight->backlight.brightness_max--;
+		pwm_backlight->backlight.brightness_max = pwm_backlight->scale;
 	}
 
 	pwm_backlight->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0, &flags);
-- 
2.9.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 3/4] video/backlight-pwm: code readability improvement.
  2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
  2016-07-14 14:17     ` [PATCH 1/4] video/backlight-pwm: fixed a loop index going out of range iw3gtf
  2016-07-14 14:17     ` [PATCH 2/4] video/backlight-pwm: fix the value of 'brightness_max' iw3gtf
@ 2016-07-14 14:17     ` iw3gtf
  2016-07-14 14:17     ` [PATCH 4/4] video/backlight-pwm: properly handle the case of an empty 'brightness-levels' in the device tree iw3gtf
                       ` (2 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: iw3gtf @ 2016-07-14 14:17 UTC (permalink / raw)
  To: barebox

We use the local variable 'length' instead of the lengthy
'pwm_backlight->backlight.brightness_max' within pwm_backlight_parse_dt().
---
 drivers/video/backlight-pwm.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
index 53cf4e9..5ff40a9 100644
--- a/drivers/video/backlight-pwm.c
+++ b/drivers/video/backlight-pwm.c
@@ -133,12 +133,11 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 	if (!prop)
 		return -EINVAL;
 
-	pwm_backlight->backlight.brightness_max = length / sizeof(u32);
+	length /= sizeof(u32);
 
 	/* read brightness levels from DT property */
-	if (pwm_backlight->backlight.brightness_max > 0) {
-		size_t size = sizeof(*pwm_backlight->levels) *
-			pwm_backlight->backlight.brightness_max;
+	if (length > 0) {
+		size_t size = sizeof(*pwm_backlight->levels) * length;
 
 		pwm_backlight->levels = xzalloc(size);
 		if (!pwm_backlight->levels)
@@ -146,11 +145,11 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 
 		ret = of_property_read_u32_array(node, "brightness-levels",
 						 pwm_backlight->levels,
-						 pwm_backlight->backlight.brightness_max);
+						 length);
 		if (ret < 0)
 			return ret;
 
-		for (i = 0; i < pwm_backlight->backlight.brightness_max; i++)
+		for (i = 0; i < length; i++)
 			if (pwm_backlight->levels[i] > pwm_backlight->scale)
 				pwm_backlight->scale = pwm_backlight->levels[i];
 
-- 
2.9.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 4/4] video/backlight-pwm: properly handle the case of an empty 'brightness-levels' in the device tree.
  2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
                       ` (2 preceding siblings ...)
  2016-07-14 14:17     ` [PATCH 3/4] video/backlight-pwm: code readability improvement iw3gtf
@ 2016-07-14 14:17     ` iw3gtf
  2016-07-15  6:07     ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation Sascha Hauer
  2016-07-25  8:37     ` Sascha Hauer
  5 siblings, 0 replies; 10+ messages in thread
From: iw3gtf @ 2016-07-14 14:17 UTC (permalink / raw)
  To: barebox

In case of an empty 'brightness-levels' array in the device tree or
a non empty one but containing only zeros the value of
'pwm_backlight->scale' would remain 0 possibly causing a division by zero
in the function compute_duty_cycle().

To fix it we check the computed value in case we actually have a 'brightness-levels'
array in the device tree otherwise we implicitly assume a simple array
of the form { 0, 1, 2, ..., 100 } and set the scale to 100.
---
 drivers/video/backlight-pwm.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
index 5ff40a9..7f1715d 100644
--- a/drivers/video/backlight-pwm.c
+++ b/drivers/video/backlight-pwm.c
@@ -128,6 +128,13 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 	if (!node)
 		return -ENODEV;
 
+	ret = of_property_read_u32(node, "default-brightness-level",
+					   &value);
+	if (ret < 0)
+		return ret;
+
+	pwm_backlight->backlight.brightness_default = value;
+
 	/* determine the number of brightness levels */
 	prop = of_find_property(node, "brightness-levels", &length);
 	if (!prop)
@@ -153,14 +160,13 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 			if (pwm_backlight->levels[i] > pwm_backlight->scale)
 				pwm_backlight->scale = pwm_backlight->levels[i];
 
-		ret = of_property_read_u32(node, "default-brightness-level",
-					   &value);
-		if (ret < 0)
-			return ret;
-
-		pwm_backlight->backlight.brightness_default = value;
-		pwm_backlight->backlight.brightness_max = pwm_backlight->scale;
+		if (pwm_backlight->scale == 0)
+			return -EINVAL;
+	} else {
+		/* We implicitly assume here a linear levels array { 0, 1, 2, ... 100 } */
+		pwm_backlight->scale = 100;
 	}
+	pwm_backlight->backlight.brightness_max = pwm_backlight->scale;
 
 	pwm_backlight->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0, &flags);
 
-- 
2.9.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/4] fixes some problems found in the backligth-pwm implementation.
  2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
                       ` (3 preceding siblings ...)
  2016-07-14 14:17     ` [PATCH 4/4] video/backlight-pwm: properly handle the case of an empty 'brightness-levels' in the device tree iw3gtf
@ 2016-07-15  6:07     ` Sascha Hauer
  2016-07-25  8:37     ` Sascha Hauer
  5 siblings, 0 replies; 10+ messages in thread
From: Sascha Hauer @ 2016-07-15  6:07 UTC (permalink / raw)
  To: iw3gtf; +Cc: barebox

On Thu, Jul 14, 2016 at 04:17:14PM +0200, iw3gtf@arcor.de wrote:
> This serie fixes some problems found in the backligth-pwm implementation.
> 
> root (4):
>   video/backlight-pwm: fixed a loop index going out of range.
>   video/backlight-pwm: fix the value of 'brightness_max'.
>   video/backlight-pwm: code readability improvement.
>   video/backlight-pwm: properly handle the case of an empty
>     'brightness-levels' in the device tree.

Applied all, thanks

Sascha

> 
>  drivers/video/backlight-pwm.c | 31 ++++++++++++++++++-------------
>  1 file changed, 18 insertions(+), 13 deletions(-)
> 
> -- 
> 2.9.1
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/4] fixes some problems found in the backligth-pwm implementation.
  2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
                       ` (4 preceding siblings ...)
  2016-07-15  6:07     ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation Sascha Hauer
@ 2016-07-25  8:37     ` Sascha Hauer
  5 siblings, 0 replies; 10+ messages in thread
From: Sascha Hauer @ 2016-07-25  8:37 UTC (permalink / raw)
  To: iw3gtf; +Cc: barebox

Hi Giorgio,

On Thu, Jul 14, 2016 at 04:17:14PM +0200, iw3gtf@arcor.de wrote:
> This serie fixes some problems found in the backligth-pwm implementation.
> 
> root (4):
>   video/backlight-pwm: fixed a loop index going out of range.
>   video/backlight-pwm: fix the value of 'brightness_max'.
>   video/backlight-pwm: code readability improvement.
>   video/backlight-pwm: properly handle the case of an empty
>     'brightness-levels' in the device tree.

Your patches lack a proper Signed-off-by. Could you add this line and
resend the patches? Otherwise I can't apply them

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

* problem configuring backlight brightness levels in device tree
@ 2016-07-11 12:24 iw3gtf
  0 siblings, 0 replies; 10+ messages in thread
From: iw3gtf @ 2016-07-11 12:24 UTC (permalink / raw)
  To: barebox

Hi,

I was trying to configure a pwm backlight on an embedded arm board
and I think I've found a minor problem.

In the source file 'drivers/video/backlight-pwm.c', in the function pwm_backlight_parse_dt()
you set the number of brightness values to:

pwm_backlight->backlight.brightness_max = length / sizeof(u32);

and then loop through the array with:

for (i = 0; i <=  pwm_backlight->backlight.brightness_max; i++)

to find the max of the array.

I think the '<=' should be a '<' otherwise you access an uninitialized value
one past the end of the array; this actually does not directly crash barebox
but the whole brightness values are scaled wrong.

giorgio


Giorgio, iw3gtf@arcor.de

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2016-07-25  8:37 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-11 11:51 problem configuring backlight brightness levels in device tree giorgio.nicole
2016-07-13  5:18 ` Sascha Hauer
2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
2016-07-14 14:17     ` [PATCH 1/4] video/backlight-pwm: fixed a loop index going out of range iw3gtf
2016-07-14 14:17     ` [PATCH 2/4] video/backlight-pwm: fix the value of 'brightness_max' iw3gtf
2016-07-14 14:17     ` [PATCH 3/4] video/backlight-pwm: code readability improvement iw3gtf
2016-07-14 14:17     ` [PATCH 4/4] video/backlight-pwm: properly handle the case of an empty 'brightness-levels' in the device tree iw3gtf
2016-07-15  6:07     ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation Sascha Hauer
2016-07-25  8:37     ` Sascha Hauer
2016-07-11 12:24 problem configuring backlight brightness levels in device tree iw3gtf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox