Skip to contentSkip to conversation

thinkpad_acpi: save kbdlight state on suspend and restore it on resume

PATCH Superseded Marco Trevisan 4 participants 13 messages May 19, 2016 – May 24, 2016 x86 platform drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thinkpad_acpi: save kbdlight state on suspend and restore it on resume

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

---
 drivers/platform/x86/thinkpad_acpi.c | 43 +++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +40 -3
index 1f9783c..10111c2 100644
@@ -5041,6 +5041,8 @@ static int kbdlight_set_level(int level)
 	return 0;
 }
 
+static int kbdlight_set_level_and_update(int level);
+
 static int kbdlight_get_level(void)
 {
 	int status = 0;
@@ -5108,7 +5110,7 @@ static void kbdlight_set_worker(struct work_struct *work)
 			container_of(work, struct tpacpi_led_classdev, work);
 
 	if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
-		kbdlight_set_level(data->new_state);
+		kbdlight_set_level_and_update(data->new_state);
 }
 
 static void kbdlight_sysfs_set(struct led_classdev *led_cdev,
@@ -5139,7 +5141,6 @@ static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
 		.max_brightness	= 2,
 		.brightness_set	= &kbdlight_sysfs_set,
 		.brightness_get	= &kbdlight_sysfs_get,
-		.flags		= LED_CORE_SUSPENDRESUME,
 	}
 };
 
@@ -5177,6 +5178,20 @@ static void kbdlight_exit(void)
 	flush_workqueue(tpacpi_wq);
 }
 
+static int kbdlight_set_level_and_update(int level)
+{
+	int ret;
+	struct led_classdev *led_cdev;
+
+	ret = kbdlight_set_level(level);
+	led_cdev = &tpacpi_led_kbdlight.led_classdev;
+
+	if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
+		led_cdev->brightness = level;
+
+	return ret;
+}
+
 static int kbdlight_read(struct seq_file *m)
 {
 	int level;
@@ -5217,13 +5232,35 @@ static int kbdlight_write(char *buf)
 	if (level == -1)
 		return -EINVAL;
 
-	return kbdlight_set_level(level);
+	return kbdlight_set_level_and_update(level);
+}
+
+static void kbdlight_suspend(void)
+{
+	struct led_classdev *led_cdev;
+
+	if (!tp_features.kbdlight)
+		return;
+
+	led_cdev = &tpacpi_led_kbdlight.led_classdev;
+	led_update_brightness(led_cdev);
+	led_classdev_suspend(led_cdev);
+}
+
+static void kbdlight_resume(void)
+{
+	if (!tp_features.kbdlight)
+		return;
+
+	led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
 }
 
 static struct ibm_struct kbdlight_driver_data = {
 	.name = "kbdlight",
 	.read = kbdlight_read,
 	.write = kbdlight_write,
+	.suspend = kbdlight_suspend,
+	.resume = kbdlight_resume,
 	.exit = kbdlight_exit,
 };
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 89 lines · On Thursday 19 May 2016 03:23:02 Marco Trevisan wrote:

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

---
 drivers/platform/x86/thinkpad_acpi.c | 43 +++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +38 -3
index 1f9783c..10111c2 100644
@@ -5041,6 +5041,8 @@ static int kbdlight_set_level(int level)
 	return 0;
 }
 
+static int kbdlight_set_level_and_update(int level);
+
 static int kbdlight_get_level(void)
 {
 	int status = 0;
@@ -5108,7 +5110,7 @@ static void kbdlight_set_worker(struct work_struct *work)
 			container_of(work, struct tpacpi_led_classdev, work);
 
 	if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
-		kbdlight_set_level(data->new_state);
+		kbdlight_set_level_and_update(data->new_state);
 }
 
 static void kbdlight_sysfs_set(struct led_classdev *led_cdev,
@@ -5139,7 +5141,6 @@ static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
 		.max_brightness	= 2,
 		.brightness_set	= &kbdlight_sysfs_set,
 		.brightness_get	= &kbdlight_sysfs_get,
-		.flags		= LED_CORE_SUSPENDRESUME,
 	}
 };
 
@@ -5177,6 +5178,20 @@ static void kbdlight_exit(void)
 	flush_workqueue(tpacpi_wq);
 }
 
+static int kbdlight_set_level_and_update(int level)
+{
+	int ret;
+	struct led_classdev *led_cdev;
+
+	ret = kbdlight_set_level(level);
+	led_cdev = &tpacpi_led_kbdlight.led_classdev;
+
+	if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
+		led_cdev->brightness = level;
+
+	return ret;
+}
+
 static int kbdlight_read(struct seq_file *m)
 {
 	int level;
@@ -5217,13 +5232,35 @@ static int kbdlight_write(char *buf)
 	if (level == -1)
 		return -EINVAL;
 
-	return kbdlight_set_level(level);
+	return kbdlight_set_level_and_update(level);
+}
+
+static void kbdlight_suspend(void)
+{
+	struct led_classdev *led_cdev;
+
+	if (!tp_features.kbdlight)
+		return;
+
+	led_cdev = &tpacpi_led_kbdlight.led_classdev;
+	led_update_brightness(led_cdev);
+	led_classdev_suspend(led_cdev);
+}
+
+static void kbdlight_resume(void)
+{
+	if (!tp_features.kbdlight)
+		return;
+
+	led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
 }

drivers/platform/x86/thinkpad_acpi.c static int kbdlight_write(char *buf) · line 5266

Quoted 10 lines
drivers/platform/x86/thinkpad_acpi.c +2 -0
 
 static struct ibm_struct kbdlight_driver_data = {
 	.name = "kbdlight",
 	.read = kbdlight_read,
 	.write = kbdlight_write,
+	.suspend = kbdlight_suspend,
+	.resume = kbdlight_resume,
 	.exit = kbdlight_exit,
 };
 

For me whole patch looks like a big hack because LED_CORE_SUSPENDRESUME does not work correctly... I would rather see fixed support for flag LED_CORE_SUSPENDRESUME, not adding another suspend/resume hook if possible. Any idea?

Signature · 2 lines
Pali Rohár
[email protected]
#2 lore raw

2016-05-19 9:52 GMT+02:00 Pali Rohár <[email protected]>:

Quoted 4 lines

For me whole patch looks like a big hack because LED_CORE_SUSPENDRESUME does not work correctly... I would rather see fixed support for flag LED_CORE_SUSPENDRESUME, not adding another suspend/resume hook if possible. Any idea?

As I wrote you in private mail I think the only way we have is to keep the led_cdev->brightness in sync with actual state. And to do this, we can listen the ACPI event, keeping this like a private event that we just consume to update led brightness value, without delivering it to userspace.

However, the custom code needed for getting this done, is probably way more than the one I added here.
Maybe it could be useful to others to read the brightness level before suspending, and in this case we could add a new flag to the led subsystem, but not sure it's really worth.

#3 lore raw
Quoted 12 lines · Marco Trevisan (Treviño)

2016-05-19 9:52 GMT+02:00 Pali Rohár <[email protected]>:

Quoted 4 lines

For me whole patch looks like a big hack because LED_CORE_SUSPENDRESUME does not work correctly... I would rather see fixed support for flag LED_CORE_SUSPENDRESUME, not adding another suspend/resume hook if possible. Any idea?

As I wrote you in private mail I think the only way we have is to keep the led_cdev->brightness in sync with actual state. And to do this, we can listen the ACPI event, keeping this like a private event that we just consume to update led brightness value, without delivering it to userspace.

Quoted 6 lines

However, the custom code needed for getting this done, is probably way more than the one I added here.
Maybe it could be useful to others to read the brightness level before suspending, and in this case we could add a new flag to the led subsystem, but not sure it's really worth.

At the risk of contributing to "the platform platform", I agree with a local fix here in the near term. If this becomes a pattern, we can look to move this into the LED subsystem. As it is, this is working as designed, overriding when necessary.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#4 lore raw
In reply to Marco Trevisan
Quoted 7 lines · Marco Trevisan

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

The patch requires a Signed-off-by. I presume that should be from Marco?

Henrique, is this Acked-by from you accurate? I need to see maintainers respond on list as they would likely object to me just taking other people's word for it in other scenarios ;-)

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#5 lore raw
Quoted 8 lines · On Monday 23 May 2016 23:57:52 Darren Hart wrote:
Quoted 7 lines · Marco Trevisan

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

Quoted 8 lines
Quoted 1 line

The patch requires a Signed-off-by. I presume that should be from Marco?

Henrique, is this Acked-by from you accurate? I need to see maintainers respond on list as they would likely object to me just taking other people's word for it in other scenarios ;-)

Darren, I'm not sure if this is really correct approach in current patch... Anyway as Marco wrote, there is ACPI event when keyboard backlight change and maybe we should handle it...

Signature · 2 lines
Pali Rohár
[email protected]
#6 lore raw

2016-05-24 0:04 GMT+02:00 Pali Rohár <[email protected]>:

Quoted 3 lines

Darren, I'm not sure if this is really correct approach in current patch... Anyway as Marco wrote, there is ACPI event when keyboard backlight change and maybe we should handle it...

I'll be happy to work on that as well, but I've not much time at the moment. However I think this could be an ok way to handle things in the mean time.

#7 lore raw
Quoted 8 lines · Marco Trevisan (Treviño)

2016-05-24 0:04 GMT+02:00 Pali Rohár <[email protected]>:

Quoted 3 lines

Darren, I'm not sure if this is really correct approach in current patch... Anyway as Marco wrote, there is ACPI event when keyboard backlight change and maybe we should handle it...

I'll be happy to work on that as well, but I've not much time at the moment. However I think this could be an ok way to handle things in the mean time.

I believe so, and Henrique has approved.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#8 lore raw
In reply to Darren Hart

2016-05-23 23:57 GMT+02:00 Darren Hart <[email protected]>:

Quoted 10 lines · Marco Trevisan
Quoted 7 lines

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

The patch requires a Signed-off-by. I presume that should be from Marco?

I'm quite new to this, so please let me know how I should proceed.

Quoted 3 lines

Henrique, is this Acked-by from you accurate? I need to see maintainers respond on list as they would likely object to me just taking other people's word for it in other scenarios ;-)

I've added it based on this reply (on ibm-acpi ML):
- http://thread.gmane.org/gmane.linux.acpi.ibm-acpi.devel/4006/focus=4007

#9 lore raw
Quoted 8 lines · Marco Trevisan (Treviño)

2016-05-23 23:57 GMT+02:00 Darren Hart <[email protected]>:

Quoted 7 lines · Marco Trevisan
Quoted 6 lines

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

The patch requires a Signed-off-by. I presume that should be from Marco?

I'm quite new to this, so please let me know how I should proceed.

Any time you submit a patch, you must provide a Signed-off-by in the commit message. Git will do this for you with "git commit --signoff". See:

Documentation/SubmittingPatches, Section 11: Sign your work

Quoted 8 lines
Quoted 4 lines

Henrique, is this Acked-by from you accurate? I need to see maintainers respond on list as they would likely object to me just taking other people's word for it in other scenarios ;-)

I've added it based on this reply (on ibm-acpi ML):
- http://thread.gmane.org/gmane.linux.acpi.ibm-acpi.devel/4006/focus=4007

Ah, excellent. I didn't have the original since I wasn't on Cc initially. That's all I need from Henrique.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#10 lore raw
In reply to Darren Hart
Quoted 9 lines · Darren Hart
Quoted 7 lines · Marco Trevisan

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

Quoted 7 lines

The patch requires a Signed-off-by. I presume that should be from Marco?

Henrique, is this Acked-by from you accurate? I need to see maintainers respond
on list as they would likely object to me just taking other people's word for it
in other scenarios ;-)

Yeah, I acked it. Sorry it took a few days to reply to this email.

This is not the "best" fix, but it is by far the less troublesome one:
hooking to the internal event from the thinkpad DSDT and updating the brightness shadow register in the driver should work, but that will require testing as we cannot really take for granted that we will get that event on every firmware out there without going over the DSDT AML of a lot of models.

Signature · 4 lines
  "One disk to rule them all, One disk to find them. One disk to bring
  them all and in the darkness grind them. In the Land of Redmond
  where the shadows lie." -- The Silicon Valley Tarot
  Henrique Holschuh
#11 lore raw

PATCH v2 1/1 thinkpad_acpi: save kbdlight state on suspend and restore it on resume

In reply to Marco Trevisan

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

---
 drivers/platform/x86/thinkpad_acpi.c | 43 +++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +40 -3
index 1f9783c..10111c2 100644
@@ -5041,6 +5041,8 @@ static int kbdlight_set_level(int level)
 	return 0;
 }
 
+static int kbdlight_set_level_and_update(int level);
+
 static int kbdlight_get_level(void)
 {
 	int status = 0;
@@ -5108,7 +5110,7 @@ static void kbdlight_set_worker(struct work_struct *work)
 			container_of(work, struct tpacpi_led_classdev, work);
 
 	if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
-		kbdlight_set_level(data->new_state);
+		kbdlight_set_level_and_update(data->new_state);
 }
 
 static void kbdlight_sysfs_set(struct led_classdev *led_cdev,
@@ -5139,7 +5141,6 @@ static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
 		.max_brightness	= 2,
 		.brightness_set	= &kbdlight_sysfs_set,
 		.brightness_get	= &kbdlight_sysfs_get,
-		.flags		= LED_CORE_SUSPENDRESUME,
 	}
 };
 
@@ -5177,6 +5178,20 @@ static void kbdlight_exit(void)
 	flush_workqueue(tpacpi_wq);
 }
 
+static int kbdlight_set_level_and_update(int level)
+{
+	int ret;
+	struct led_classdev *led_cdev;
+
+	ret = kbdlight_set_level(level);
+	led_cdev = &tpacpi_led_kbdlight.led_classdev;
+
+	if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
+		led_cdev->brightness = level;
+
+	return ret;
+}
+
 static int kbdlight_read(struct seq_file *m)
 {
 	int level;
@@ -5217,13 +5232,35 @@ static int kbdlight_write(char *buf)
 	if (level == -1)
 		return -EINVAL;
 
-	return kbdlight_set_level(level);
+	return kbdlight_set_level_and_update(level);
+}
+
+static void kbdlight_suspend(void)
+{
+	struct led_classdev *led_cdev;
+
+	if (!tp_features.kbdlight)
+		return;
+
+	led_cdev = &tpacpi_led_kbdlight.led_classdev;
+	led_update_brightness(led_cdev);
+	led_classdev_suspend(led_cdev);
+}
+
+static void kbdlight_resume(void)
+{
+	if (!tp_features.kbdlight)
+		return;
+
+	led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
 }
 
 static struct ibm_struct kbdlight_driver_data = {
 	.name = "kbdlight",
 	.read = kbdlight_read,
 	.write = kbdlight_write,
+	.suspend = kbdlight_suspend,
+	.resume = kbdlight_resume,
 	.exit = kbdlight_exit,
 };
 
Signature · 1 line
1.9.1
Applied
#12 lore raw
Quoted 8 lines · Marco Trevisan

From: Marco Trevisan (Treviño) <[email protected]>

Override default LED class suspend/resume handles, by keeping track of the brightness level before suspending so that it can be automatically restored on resume by calling default resume handler.

Just a nit (I fixed it), your Signed-off-by comes first. Reviewers and testers follow, then the sub maintainers Signed-off-by, then Linus' Signed-off-by. It tracks the path the patch took from authorship, testing, review, pull, and merge.

Queued, thanks.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center