Skip to contentSkip to conversation

ACPI video: Fix brightness control initialization sequence

PATCH Superseded Danny Baumann 2 participants 9 messages Mar 14, 2013 – Mar 15, 2013 ACPI

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover ACPI video: Fix brightness control initialization sequence

This patch set fixes a some logic mistakes in the brightness control initialization sequence of the ACPI video driver. This should make the initialization (and as a consequence in-kernel brightness control) work for a number of laptops. One example that was broken before is the Dell Inspiron 15R SE (7520).

Regards,

Danny

Danny Baumann (3):
ACPI video: Fix brightness control initialization for some laptops. ACPI video: Make logic a little easier to understand. ACPI video: Fix applying indexed initial brightness value.

 drivers/acpi/video.c | 59 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 36 insertions(+), 23 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/3 ACPI video: Fix brightness control initialization for some laptops.

In particular, this fixes brightness control initialization for all devices that return index values from _BQC and don't happen to have the initial index set by the BIOS in their _BCL table. One example for that is the Dell Inspiron 15R SE (model number 7520).

What happened for those devices is that acpi_init_brightness queried the initial brightness by calling acpi_video_device_lcd_get_level_current. This called _BQC, which returned e.g. 13. As _BQC_use_index isn't determined at this point (and thus has its initial value of 0), the index isn't converted into the actual level. As '13' isn't present in the _BCL list, *level is later overwritten with brightness->curr, which was initialized to max_level (100) before. Later in acpi_init_brightness, level_old (with the value 100) is used as an index into the _BCL table, which causes a value outside of the actual table to be used as input into acpi_video_device_lcd_set_level(). Depending on the (undefined) value of that location, this call will fail, causing the brightness control for the device in question not to be enabled.

Fix that by returning the raw value returned by the _BQC call in the initialization case.

---
 drivers/acpi/video.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
drivers/acpi/video.c +18 -11
index 313f959..ef85574 100644
@@ -463,6 +463,15 @@ acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 		status = acpi_evaluate_integer(device->dev->handle, buf,
 						NULL, level);
 		if (ACPI_SUCCESS(status)) {
+			if (init) {
+				/*
+				 * At init time we don't yet know whether the
+				 * value is indexed or not. Don't mess with it
+				 * until we have determined how to handle it.
+				 */
+				return 0;
+			}
+
 			if (device->brightness->flags._BQC_use_index) {
 				if (device->brightness->flags._BCL_reversed)
 					*level = device->brightness->count
@@ -476,16 +485,14 @@ acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 					device->brightness->curr = *level;
 					return 0;
 			}
-			if (!init) {
-				/*
-				 * BQC returned an invalid level.
-				 * Stop using it.
-				 */
-				ACPI_WARNING((AE_INFO,
-					      "%s returned an invalid level",
-					      buf));
-				device->cap._BQC = device->cap._BCQ = 0;
-			}
+			/*
+			 * BQC returned an invalid level.
+			 * Stop using it.
+			 */
+			ACPI_WARNING((AE_INFO,
+				      "%s returned an invalid level",
+				      buf));
+			device->cap._BQC = device->cap._BCQ = 0;
 		} else {
 			/* Fixme:
 			 * should we return an error or ignore this failure?
@@ -714,7 +721,7 @@ acpi_video_init_brightness(struct acpi_video_device *device)
 	if (result)
 		goto out_free_levels;
 
-	result = acpi_video_device_lcd_get_level_current(device, &level, 0);
+	result = acpi_video_device_lcd_get_level_current(device, &level, 1);
 	if (result)
 		goto out_free_levels;
 
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH 2/3 ACPI video: Make logic a little easier to understand.

In reply to Danny Baumann

Make code paths a little easier to follow, and don't needlessly continue list iteration.

---
 drivers/acpi/video.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/acpi/video.c +7 -6
index ef85574..d0fade7 100644
@@ -736,16 +736,17 @@ acpi_video_init_brightness(struct acpi_video_device *device)
 		 */
 		if (use_bios_initial_backlight) {
 			for (i = 2; i < br->count; i++)
-				if (level_old == br->levels[i])
+				if (level_old == br->levels[i]) {
 					level = level_old;
+					break;
+				}
 		}
-		goto set_level;
+	} else {
+		if (br->flags._BCL_reversed)
+			level_old = (br->count - 1) - level_old;
+		level = br->levels[level_old];
 	}
 
-	if (br->flags._BCL_reversed)
-		level_old = (br->count - 1) - level_old;
-	level = br->levels[level_old];
-
 set_level:
 	result = acpi_video_device_lcd_set_level(device, level);
 	if (result)
Signature · 1 line
1.8.1.4
#5 lore raw

PATCH 3/3 ACPI video: Fix applying indexed initial brightness value.

In reply to Danny Baumann

The value initially read via _BQC also needs to be offset by 2 to compensate for the first 2 special items in _BCL. Introduce a helper function to do the conversion in order to not needlessly duplicate code.

---
 drivers/acpi/video.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/acpi/video.c +14 -9
index d0fade7..562ccc3 100644
@@ -450,6 +450,16 @@ static struct dmi_system_id video_dmi_table[] __initdata = {
 	{}
 };
 
+static unsigned long long
+acpi_video_index_to_level(struct acpi_video_device *device,
+			  unsigned long long index)
+{
+	if (device->brightness->flags._BCL_reversed)
+		index = device->brightness->count - 3 - index;
+
+	return device->brightness->levels[index + 2];
+}
+
 static int
 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 					unsigned long long *level, int init)
@@ -472,13 +482,10 @@ acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 				return 0;
 			}
 
-			if (device->brightness->flags._BQC_use_index) {
-				if (device->brightness->flags._BCL_reversed)
-					*level = device->brightness->count
-								 - 3 - (*level);
-				*level = device->brightness->levels[*level + 2];
+			if (device->brightness->flags._BQC_use_index)
+				*level = acpi_video_index_to_level(device,
+								   *level);
 
-			}
 			*level += bqc_offset_aml_bug_workaround;
 			for (i = 2; i < device->brightness->count; i++)
 				if (device->brightness->levels[i] == *level) {
@@ -742,9 +749,7 @@ acpi_video_init_brightness(struct acpi_video_device *device)
 				}
 		}
 	} else {
-		if (br->flags._BCL_reversed)
-			level_old = (br->count - 1) - level_old;
-		level = br->levels[level_old];
+		level = acpi_video_index_to_level(device, level_old);
 	}
 
 set_level:
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [PATCH 1/3] ACPI video: Fix brightness control initialization for some laptops.

In reply to Danny Baumann
Quoted 18 lines · Danny Baumann

In particular, this fixes brightness control initialization for all devices that return index values from _BQC and don't happen to have the initial index set by the BIOS in their _BCL table. One example for that is the Dell Inspiron 15R SE (model number 7520).

What happened for those devices is that acpi_init_brightness queried the initial brightness by calling acpi_video_device_lcd_get_level_current. This called _BQC, which returned e.g. 13. As _BQC_use_index isn't determined at this point (and thus has its initial value of 0), the index isn't converted into the actual level. As '13' isn't present in the _BCL list, *level is later overwritten with brightness->curr, which was initialized to max_level (100) before. Later in acpi_init_brightness, level_old (with the value 100) is used as an index into the _BCL table, which causes a value outside of the actual table to be used as input into acpi_video_device_lcd_set_level(). Depending on the (undefined) value of that location, this call will fail, causing the brightness control for the device in question not to be enabled.

Quoted 2 lines

Fix that by returning the raw value returned by the _BQC call in the initialization case.

You missed Signed-off-by tag here.
Other than that, Reviewed-by: Aaron Lu <[email protected]>.

Thanks,
Aaron

Quoted 59 lines
---
 drivers/acpi/video.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
drivers/acpi/video.c +18 -11
index 313f959..ef85574 100644
@@ -463,6 +463,15 @@ acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 		status = acpi_evaluate_integer(device->dev->handle, buf,
 						NULL, level);
 		if (ACPI_SUCCESS(status)) {
+			if (init) {
+				/*
+				 * At init time we don't yet know whether the
+				 * value is indexed or not. Don't mess with it
+				 * until we have determined how to handle it.
+				 */
+				return 0;
+			}
+
 			if (device->brightness->flags._BQC_use_index) {
 				if (device->brightness->flags._BCL_reversed)
 					*level = device->brightness->count
@@ -476,16 +485,14 @@ acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 					device->brightness->curr = *level;
 					return 0;
 			}
-			if (!init) {
-				/*
-				 * BQC returned an invalid level.
-				 * Stop using it.
-				 */
-				ACPI_WARNING((AE_INFO,
-					      "%s returned an invalid level",
-					      buf));
-				device->cap._BQC = device->cap._BCQ = 0;
-			}
+			/*
+			 * BQC returned an invalid level.
+			 * Stop using it.
+			 */
+			ACPI_WARNING((AE_INFO,
+				      "%s returned an invalid level",
+				      buf));
+			device->cap._BQC = device->cap._BCQ = 0;
 		} else {
 			/* Fixme:
 			 * should we return an error or ignore this failure?
@@ -714,7 +721,7 @@ acpi_video_init_brightness(struct acpi_video_device *device)
 	if (result)
 		goto out_free_levels;
 
-	result = acpi_video_device_lcd_get_level_current(device, &level, 0);
+	result = acpi_video_device_lcd_get_level_current(device, &level, 1);
 	if (result)
 		goto out_free_levels;
 
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] ACPI video: Make logic a little easier to understand.

In reply to Danny Baumann
Quoted 2 lines · Danny Baumann

Make code paths a little easier to follow, and don't needlessly continue list iteration.

Same here, please add Signed-off-by tag.

Thanks,
Aaron

Quoted 33 lines
---
 drivers/acpi/video.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/acpi/video.c +7 -6
index ef85574..d0fade7 100644
@@ -736,16 +736,17 @@ acpi_video_init_brightness(struct acpi_video_device *device)
 		 */
 		if (use_bios_initial_backlight) {
 			for (i = 2; i < br->count; i++)
-				if (level_old == br->levels[i])
+				if (level_old == br->levels[i]) {
 					level = level_old;
+					break;
+				}
 		}
-		goto set_level;
+	} else {
+		if (br->flags._BCL_reversed)
+			level_old = (br->count - 1) - level_old;
+		level = br->levels[level_old];
 	}
 
-	if (br->flags._BCL_reversed)
-		level_old = (br->count - 1) - level_old;
-	level = br->levels[level_old];
-
 set_level:
 	result = acpi_video_device_lcd_set_level(device, level);
 	if (result)
#6 lore raw

Re: [PATCH 3/3] ACPI video: Fix applying indexed initial brightness value.

In reply to Danny Baumann
Quoted 14 lines · Danny Baumann

The value initially read via _BQC also needs to be offset by 2 to compensate for the first 2 special items in _BCL. Introduce a helper function to do the conversion in order to not needlessly duplicate code.

---
 drivers/acpi/video.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/acpi/video.c +0 -0
index d0fade7..562ccc3 100644
@@ -450,6 +450,16 @@ static struct dmi_system_id video_dmi_table[] __initdata = {
 	{}
 };

drivers/acpi/video.c static struct dmi_system_id video_dmi_table[] __initdata = { · line 461

Quoted 10 lines
drivers/acpi/video.c +9 -0
 
+static unsigned long long
+acpi_video_index_to_level(struct acpi_video_device *device,
+			  unsigned long long index)
+{
+	if (device->brightness->flags._BCL_reversed)
+		index = device->brightness->count - 3 - index;
+
+	return device->brightness->levels[index + 2];
+}

What about making this function also take care of the bqc_offset_aml_bug_workaround? so that this function serves more like a conversion from raw value to fixed value, the function name can perhaps be named as: acpi_video_fix_bqc_value, or whatever you think is more appropriate.

Quoted 22 lines
drivers/acpi/video.c +4 -6
+
 static int
 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 					unsigned long long *level, int init)
@@ -472,13 +482,10 @@ acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
 				return 0;
 			}
 
-			if (device->brightness->flags._BQC_use_index) {
-				if (device->brightness->flags._BCL_reversed)
-					*level = device->brightness->count
-								 - 3 - (*level);
-				*level = device->brightness->levels[*level + 2];
+			if (device->brightness->flags._BQC_use_index)
+				*level = acpi_video_index_to_level(device,
+								   *level);
 
-			}
 			*level += bqc_offset_aml_bug_workaround;
 			for (i = 2; i < device->brightness->count; i++)
 				if (device->brightness->levels[i] == *level) {

drivers/acpi/video.c acpi_video_init_brightness(struct acpi_video_device *device) · line 752

Quoted 7 lines
drivers/acpi/video.c +1 -3
@@ -742,9 +749,7 @@ acpi_video_init_brightness(struct acpi_video_device *device)
 				}
 		}
 	} else {
-		if (br->flags._BCL_reversed)
-			level_old = (br->count - 1) - level_old;
-		level = br->levels[level_old];
+		level = acpi_video_index_to_level(device, level_old);

And here, that new function should be used, which also takes care of the offset_aml_bug problem(though in theory, the two problems may not happen on the same BIOS table).

And the acpi_video_device_lcd_get_level_current's param init can probably be renamed as raw, meaning if raw value is desired or fixed value, but it's not a big deal.

Thanks,
Aaron

Quoted 4 lines
drivers/acpi/video.c +0 -0
 	}
 
 set_level:
#7 lore raw

Re: [PATCH 3/3] ACPI video: Fix applying indexed initial brightness value.

Hi,

Quoted 7 lines
Quoted 7 lines
drivers/acpi/video.c +7 -0
+static unsigned long long
+acpi_video_index_to_level(struct acpi_video_device *device,
+			  unsigned long long index)
+{
+	if (device->brightness->flags._BCL_reversed)
+		index = device->brightness->count - 3 - index;
+
Quoted 8 lines

drivers/acpi/video.c static struct dmi_system_id video_dmi_table[] __initdata = { · line 461

Quoted 2 lines
drivers/acpi/video.c +2 -0
+	return device->brightness->levels[index + 2];
+}

What about making this function also take care of the bqc_offset_aml_bug_workaround? so that this function serves more like a conversion from raw value to fixed value, the function name can perhaps be named as: acpi_video_fix_bqc_value, or whatever you think is more appropriate.

Makes sense to me. How about acpi_video_bqc_to_level? I'd suggest acpi_video_bqc_value_to_level, but that makes it hard to keep the 80 char limit in acpi_video_device_lcd_get_level_current.

Quoted 4 lines
Quoted 4 lines
drivers/acpi/video.c +0 -0
@@ -742,9 +749,7 @@ acpi_video_init_brightness(struct acpi_video_device *device)
  				}
  		}
  	} else {
Quoted 8 lines

drivers/acpi/video.c acpi_video_init_brightness(struct acpi_video_device *device) · line 752

Quoted 4 lines
drivers/acpi/video.c +1 -3
-		if (br->flags._BCL_reversed)
-			level_old = (br->count - 1) - level_old;
-		level = br->levels[level_old];
+		level = acpi_video_index_to_level(device, level_old);

And here, that new function should be used, which also takes care of the offset_aml_bug problem(though in theory, the two problems may not happen on the same BIOS table).

But that new function (whatever it's named) is already used here?

BTW, shouldn't the use_bios_initial_backlight also be respected for the BQC-returns-index case? Currently it's only used for the BQC-returns-level case.

Quoted 3 lines

And the acpi_video_device_lcd_get_level_current's param init can probably be renamed as raw, meaning if raw value is desired or fixed value, but it's not a big deal.

Agreed. I'll send a new patch set (this time with signed-off-by) once I get opinion on the question above.

Thanks,

Danny

#8 lore raw

Re: [PATCH 3/3] ACPI video: Fix applying indexed initial brightness value.

Quoted 18 lines · Danny Baumann

Hi,

Quoted 7 lines
Quoted 7 lines
drivers/acpi/video.c +7 -0
+static unsigned long long
+acpi_video_index_to_level(struct acpi_video_device *device,
+			  unsigned long long index)
+{
+	if (device->brightness->flags._BCL_reversed)
+		index = device->brightness->count - 3 - index;
+
Quoted 8 lines

drivers/acpi/video.c static struct dmi_system_id video_dmi_table[] __initdata = { · line 461

Quoted 2 lines
drivers/acpi/video.c +2 -0
+	return device->brightness->levels[index + 2];
+}

What about making this function also take care of the bqc_offset_aml_bug_workaround? so that this function serves more like a conversion from raw value to fixed value, the function name can perhaps be named as: acpi_video_fix_bqc_value, or whatever you think is more appropriate.

Quoted 3 lines

Makes sense to me. How about acpi_video_bqc_to_level? I'd suggest acpi_video_bqc_value_to_level, but that makes it hard to keep the 80 char limit in acpi_video_device_lcd_get_level_current.

Right, so let's go with acpi_video_bqc_to_level.

Quoted 14 lines
Quoted 13 lines

drivers/acpi/video.c acpi_video_init_brightness(struct acpi_video_device *device) · line 752

Quoted 9 lines
drivers/acpi/video.c +1 -3
@@ -742,9 +749,7 @@ acpi_video_init_brightness(struct acpi_video_device *device)
  				}
  		}
  	} else {
-		if (br->flags._BCL_reversed)
-			level_old = (br->count - 1) - level_old;
-		level = br->levels[level_old];
+		level = acpi_video_index_to_level(device, level_old);

And here, that new function should be used, which also takes care of the offset_aml_bug problem(though in theory, the two problems may not happen on the same BIOS table).

Quoted 1 line

But that new function (whatever it's named) is already used here?

Yes, I mean the new function that also takes care of offset_aml_bug :-)

Quoted 4 lines

BTW, shouldn't the use_bios_initial_backlight also be respected for the BQC-returns-index case? Currently it's only used for the BQC-returns-level case.

Definitely, we should care that.

Quoted 7 lines
Quoted 4 lines

And the acpi_video_device_lcd_get_level_current's param init can probably be renamed as raw, meaning if raw value is desired or fixed value, but it's not a big deal.

Agreed. I'll send a new patch set (this time with signed-off-by) once I get opinion on the question above.

Cool, thanks.

-Aaron