[systemd-devel] [RFC PATCH v6 2/5] ACPI: button: Add an optional workaround to fix an event missing issue for old userspace

2017-06-21 Thread Lv Zheng
There are platform variations implementing ACPI lid device in different
ways:
1. Some platforms send "open" events to OS and the events arrive before
   button driver is resumed;
2. Some platforms send "open" events to OS, but the events arrive after
   button driver is resumed, ex., Samsung N210+;
3. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, and the update events arrive before
   button driver is resumed;
4. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, but the update events arrive after
   button driver is resumed, ex., Surface Pro 3;
5. Some platforms never send "open" events, _LID returns value sticks to
   "close", ex., Surface Pro 1.
Currently, all cases work fine with systemd 233, but only case 1,2 work
fine with systemd 229.

Case 3,4 can be treated as an event missing issue:
   After seeing a LID "close" event, systemd 229 will wait several seconds
   (HoldoffTimeoutSec) before suspending the platform. Thus on case 4
   platforms, if users close lid, and re-open it during the
   HoldoffTimeoutSec period, there is still no "open" events seen by the
   userspace. Thus systemd still considers the last state as "close" and
   suspends the platform after the HoldoffTimeoutSec times out.

Note that not only systemd 229, desktop managers (ex.,
gnome-settings-daemon) also suffer from this issue.

This patch tries to fix this issue by periodically sending _LID return
value to userspace, which ensures to trigger a SW_LID event when the
underlying hardware state has changed. As adding a periodic timer is not a
power friendly way, this patch prepares an option for users to enable on
failure platforms for old userspace programs.

Users can configure update interval via button.lid_update_interval.
This should be configured to a smaller value than HoldoffTimeoutSec in
/etc/systemd/logind.conf.

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 17 +++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index 67a0d78..a8b119e 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -126,6 +126,14 @@ static unsigned long lid_notify_timeout __read_mostly = 10;
 module_param(lid_notify_timeout, ulong, 0644);
 MODULE_PARM_DESC(lid_notify_timeout, "Timeout (s) before receiving lid 
notification");
 
+static bool lid_periodic_update __read_mostly = false;
+module_param(lid_periodic_update, bool, 0644);
+MODULE_PARM_DESC(lid_periodic_update, "Periodically sending lid state 
updates");
+
+static unsigned long lid_update_interval __read_mostly = 1 * MSEC_PER_SEC;
+module_param(lid_update_interval, ulong, 0644);
+MODULE_PARM_DESC(lid_update_interval, "Interval (ms) between lid periodic 
updates");
+
 /* --
   FS Interface (/proc)
-- 
*/
@@ -395,6 +403,8 @@ static void acpi_lid_initialize_state(struct acpi_device 
*device)
break;
case ACPI_BUTTON_LID_INIT_METHOD:
(void)acpi_lid_update_state(device);
+   if (lid_periodic_update)
+   acpi_lid_start_timer(device, lid_update_interval);
break;
case ACPI_BUTTON_LID_INIT_IGNORE:
default:
@@ -560,8 +570,11 @@ static int acpi_button_add(struct acpi_device *device)
 * more we only care about the last one...
 */
lid_device = device;
-   acpi_lid_start_timer(device,
-   lid_notify_timeout * MSEC_PER_SEC);
+   if (lid_periodic_update)
+   acpi_lid_initialize_state(device);
+   else
+   acpi_lid_start_timer(device,
+   lid_notify_timeout * MSEC_PER_SEC);
}
 
printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
-- 
2.7.4

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [RFC PATCH v6 1/5] ACPI: button: Add a workaround to fix an order issue for old userspace

2017-06-21 Thread Lv Zheng
There are platform variations implementing ACPI lid device in different
ways:
1. Some platforms send "open" events to OS and the events arrive before
   button driver is resumed;
2. Some platforms send "open" events to OS, but the events arrive after
   button driver is resumed, ex., Samsung N210+;
3. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, and the update events arrive before
   button driver is resumed;
4. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, but the update events arrive after
   button driver is resumed, ex., Surface Pro 3;
5. Some platforms never send "open" events, _LID returns value sticks to
   "close", ex., Surface Pro 1.
Currently, all cases work find with systemd 233, but only case 1 works fine
with systemd 229.

Case 2,4 can be treated as an order issue:
   If the button driver always evaluates _LID after seeing a LID
   notification, there shouldn't be such a problem.

Note that this order issue cannot be fixed by sorting OS drivers' resume
code:
1. When acpi.ec_freeze_events=Y, the order depends on whether PNP0C0D(LID)
   or PNP0C09(EC) appears first in the namespace (probe order).
2. Even when acpi.ec_freeze_events=N, which forces OS to handle EC events
   as early as possible, the order issue can still be reproduced due to
   platform delays (reproduce ratio is lower than case 1).
3. Some platform simply has a very big delay for LID open events.

This patch tries to fix this issue for systemd 229 by defer sending initial
lid state 10 seconds later after resume, which ensures EC events can always
arrive before button driver evaluates _LID. It finally only fixes case 2
platforms as fixing case 4 platforms needs additional efforts (see known
issue below).

The users can configure wait timeout via button.lid_notify_timeout.

Known issu:
1. systemd/logind 229 still mis-bahaves with case 3,4 platforms
   After seeing a LID "close" event, systemd 229 will wait several seconds
   (HoldoffTimeoutSec) before suspending the platform. Thus on case 4
   platforms, if users close lid, and re-open it during the
   HoldoffTimeoutSec period, there is still no "open" events seen by the
   userspace. Thus systemd still considers the last state as "close" and
   suspends the platform after the HoldoffTimeoutSec times out.

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 36 ++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index e19f530..67a0d78 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -28,6 +28,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -79,6 +80,7 @@ MODULE_DEVICE_TABLE(acpi, button_device_ids);
 static int acpi_button_add(struct acpi_device *device);
 static int acpi_button_remove(struct acpi_device *device);
 static void acpi_button_notify(struct acpi_device *device, u32 event);
+static void acpi_lid_timeout(ulong arg);
 
 #ifdef CONFIG_PM_SLEEP
 static int acpi_button_suspend(struct device *dev);
@@ -104,6 +106,7 @@ static struct acpi_driver acpi_button_driver = {
 struct acpi_button {
unsigned int type;
struct input_dev *input;
+   struct timer_list lid_timer;
char phys[32];  /* for input device */
unsigned long pushed;
int last_state;
@@ -119,6 +122,10 @@ static unsigned long lid_report_interval __read_mostly = 
500;
 module_param(lid_report_interval, ulong, 0644);
 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
 
+static unsigned long lid_notify_timeout __read_mostly = 10;
+module_param(lid_notify_timeout, ulong, 0644);
+MODULE_PARM_DESC(lid_notify_timeout, "Timeout (s) before receiving lid 
notification");
+
 /* --
   FS Interface (/proc)
-- 
*/
@@ -371,6 +378,15 @@ static int acpi_lid_update_state(struct acpi_device 
*device)
return acpi_lid_notify_state(device, state);
 }
 
+static void acpi_lid_start_timer(struct acpi_device *device,
+unsigned long msecs)
+{
+   struct acpi_button *button = acpi_driver_data(device);
+
+   mod_timer(>lid_timer,
+ jiffies + msecs_to_jiffies(msecs));
+}
+
 static void acpi_lid_initialize_state(struct acpi_device *device)
 {
switch (lid_init_state) {
@@ -386,6 +402,13 @@ static void acpi_lid_initialize

[systemd-devel] [RFC PATCH v6 3/5] ACPI: button: Rework lid_init_state=ignore mode

2017-06-21 Thread Lv Zheng
There are platform variations implementing ACPI lid device in different
ways:
1. Some platforms send "open" events to OS and the events arrive before
   button driver is resumed;
2. Some platforms send "open" events to OS, but the events arrive after
   button driver is resumed, ex., Samsung N210+;
3. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, and the update events arrive before
   button driver is resumed;
4. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, but the update events arrive after
   button driver is resumed, ex., Surface Pro 3;
5. Some platforms never send "open" events, _LID returns value sticks to
   "close", ex., Surface Pro 1.
Currently, all cases work find with systemd 233, but only case 1,2,3,4 work
fine with systemd 229.

After fixing all the other issues for old userspace programs, case 5 is the
only case that the exported input node is still not fully compliant to
SW_LID ABI and thus needs quirks or ABI changes.

The original button.lid_init_state=ignore ABI change solution is now too
complicated if its purpose is to only solve this final incompliant use
case. This patch re-works it by unconditionally prepending "open"
complement events.

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 85 +++
 1 file changed, 5 insertions(+), 80 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index a8b119e..1256a8c 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -109,8 +109,6 @@ struct acpi_button {
struct timer_list lid_timer;
char phys[32];  /* for input device */
unsigned long pushed;
-   int last_state;
-   ktime_t last_time;
bool suspended;
 };
 
@@ -118,10 +116,6 @@ static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
 static struct acpi_device *lid_device;
 static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
 
-static unsigned long lid_report_interval __read_mostly = 500;
-module_param(lid_report_interval, ulong, 0644);
-MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
-
 static unsigned long lid_notify_timeout __read_mostly = 10;
 module_param(lid_notify_timeout, ulong, 0644);
 MODULE_PARM_DESC(lid_notify_timeout, "Timeout (s) before receiving lid 
notification");
@@ -157,79 +151,12 @@ static int acpi_lid_notify_state(struct acpi_device 
*device, int state)
 {
struct acpi_button *button = acpi_driver_data(device);
int ret;
-   ktime_t next_report;
-   bool do_update;
-
-   /*
-* In lid_init_state=ignore mode, if user opens/closes lid
-* frequently with "open" missing, and "last_time" is also updated
-* frequently, "close" cannot be delivered to the userspace.
-* So "last_time" is only updated after a timeout or an actual
-* switch.
-*/
-   if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
-   button->last_state != !!state)
-   do_update = true;
-   else
-   do_update = false;
-
-   next_report = ktime_add(button->last_time,
-   ms_to_ktime(lid_report_interval));
-   if (button->last_state == !!state &&
-   ktime_after(ktime_get(), next_report)) {
-   /* Complain the buggy firmware */
-   pr_warn_once("The lid device is not compliant to SW_LID.\n");
 
-   /*
-* Send the unreliable complement switch event:
-*
-* On most platforms, the lid device is reliable. However
-* there are exceptions:
-* 1. Platforms returning initial lid state as "close" by
-*default after booting/resuming:
-* https://bugzilla.kernel.org/show_bug.cgi?id=89211
-* https://bugzilla.kernel.org/show_bug.cgi?id=106151
-* 2. Platforms never reporting "open" events:
-* https://bugzilla.kernel.org/show_bug.cgi?id=106941
-* On these buggy platforms, the usage model of the ACPI
-* lid device actually is:
-* 1. The initial returning value of _LID may not be
-*reliable.
-* 2. The open event may not be reliable.
-* 3. The close event is reliable.
-*
-* But SW_LID is typed as input switch event, the input
-* layer checks if

[systemd-devel] [RFC PATCH 0/2] ACPI: button: Fix button.lid_init_state=method mode

2017-06-07 Thread Lv Zheng
The following approach fixes button.lid_init_state=method mode for
systemd 233:
 https://patchwork.kernel.org/patch/9756457/
 https://patchwork.kernel.org/patch/9756467/
But it is not working well with old systemd 229. This solution tries to
make a more comfortable approach for systemd 229.

There are platform variations implementing ACPI lid device in different
way:
1. Some platforms send "open" events to OS and the events arrive before
   button driver is resumed;
2. Some platforms send "open" events to OS, but the events arrive after
   button driver is resumed, ex., Samsung N210+;
3. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, and the update events arrive before
   button driver is resumed;
4. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, but the update events arrive after
   button driver is resumed, ex., Surface Pro 3;
5. Some platforms never send "open" events, _LID returns value sticks to
   "close", ex., Surface Pro 1.

[PATCH 1] tries to fix case 2,4, making them working with any systemd.
[PATCH 2] tries to fix case 5, making it working with systemd 233.
  This is also a replacement of the following solution:
   https://patchwork.kernel.org/patch/9760867/
  It seems adding/removing input node and requesting systemd to
  change again is unnecessary for such platforms, so this patch
  simply converts "lid_unreliable" into
  "button.lid_init_state=ignore".

This material is just sent to demonstrate solutions and issues, the
final solution is not determined yet. So marking them as RFC.

Lv Zheng (2):
  ACPI: button: Fix issue that button notify cannot report stateful
SW_LID state
  ACPI: button: Add a quirk mode for Surface Pro 1 like laptop

 drivers/acpi/button.c | 188 --
 1 file changed, 89 insertions(+), 99 deletions(-)

-- 
2.7.4

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [RFC PATCH v5 1/2] ACPI: button: Fix issue that button notify cannot report stateful SW_LID state

2017-06-07 Thread Lv Zheng
There are platform variations implementing ACPI lid device in different
ways:
1. Some platforms send "open" events to OS and the events arrive before
   button driver is resumed;
2. Some platforms send "open" events to OS, but the events arrive after
   button driver is resumed, ex., Samsung N210+;
3. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, and the update events arrive before
   button driver is resumed;
4. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, but the update events arrive after
   button driver is resumed, ex., Surface Pro 3;
5. Some platforms never send "open" events, _LID returns value sticks to
   "close", ex., Surface Pro 1.
Currently, only case 1,3 works fine with systemd 229.

Case 2,4 can be treated as an order issue. This patch first fixes this
issue by defer sending initial lid state 10 seconds later after resume,
which ensures acpi_ec_resume() is always invoked before
acpi_button_resume().

However we can see different problems due to systemd bugs:
  systemd won't suspend right after seeing "close" event, it has a timeout,
  within the timeout, user may opens lid again. But even lid
  firmware/driver properly deliver this "open" to user space, when the
  timeout tickes, systemd still suspends the platform.
  Then user has to close/open again to wake the system up. Noticing that
  the first close event will remain in firmware, after resume, user space
  can still see a "close" followed by "open", and nothing can stop systemd
  from suspending again.
This problem can only be fixed by continously updating lid state. Thus
this patch doesn't kill the timer after seeing the BIOS notification, but
continously sending _LID return value to the input layer for
button.lid_init_state=method mode.

The users can configure update interval via button.lid_update_interval.

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 36 +---
 1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index e19f530..fd8eff6 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -28,6 +28,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -79,6 +80,7 @@ MODULE_DEVICE_TABLE(acpi, button_device_ids);
 static int acpi_button_add(struct acpi_device *device);
 static int acpi_button_remove(struct acpi_device *device);
 static void acpi_button_notify(struct acpi_device *device, u32 event);
+static void acpi_lid_timeout(ulong arg);
 
 #ifdef CONFIG_PM_SLEEP
 static int acpi_button_suspend(struct device *dev);
@@ -104,6 +106,7 @@ static struct acpi_driver acpi_button_driver = {
 struct acpi_button {
unsigned int type;
struct input_dev *input;
+   struct timer_list lid_timer;
char phys[32];  /* for input device */
unsigned long pushed;
int last_state;
@@ -119,6 +122,10 @@ static unsigned long lid_report_interval __read_mostly = 
500;
 module_param(lid_report_interval, ulong, 0644);
 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
 
+static unsigned long lid_update_interval __read_mostly = 10 * MSEC_PER_SEC;
+module_param(lid_update_interval, ulong, 0644);
+MODULE_PARM_DESC(lid_update_interval, "Interval (ms) between lid state 
updates");
+
 /* --
   FS Interface (/proc)
-- 
*/
@@ -371,17 +378,25 @@ static int acpi_lid_update_state(struct acpi_device 
*device)
return acpi_lid_notify_state(device, state);
 }
 
-static void acpi_lid_initialize_state(struct acpi_device *device)
+static void acpi_lid_tick(struct acpi_device *device)
+{
+   struct acpi_button *button = acpi_driver_data(device);
+
+   mod_timer(>lid_timer,
+ jiffies + msecs_to_jiffies(lid_update_interval));
+}
+
+static void acpi_lid_timeout(ulong arg)
 {
+   struct acpi_device *device = (struct acpi_device *)arg;
+
switch (lid_init_state) {
case ACPI_BUTTON_LID_INIT_OPEN:
(void)acpi_lid_notify_state(device, 1);
break;
case ACPI_BUTTON_LID_INIT_METHOD:
-   (void)acpi_lid_update_state(device);
-   break;
-   case ACPI_BUTTON_LID_INIT_IGNORE:
-   default:
+   acpi_lid_update_state(device);
+   acpi_lid_tick(device);
break;
}
 }
@@ -432,6 +447,

[systemd-devel] [RFC PATCH v5 2/2] ACPI: button: Add a quirk mode for Surface Pro 1 like laptop

2017-06-07 Thread Lv Zheng
Some platforms never send "open" events, _LID returns value sticks to
"close", ex., Surface Pro 1.

Such platforms cannot work well with systemd 229 in
button.lid_init_state=method mode, but button.lid_init_state=open
workaround is available for them to work with systemd 229 and they can work
perfectly with systemd 233 in button.lid_init_state=ignore mode.

This patch introduces a boot parameter to mark such platform lid device as
unreliable to replace old button.lid_init_state=ignore mode. So that users
can use this quirk to make such platforms working with systemd 233. Since
such platform only sends "close", old complicated "open" complement event
mechanism is replaced by a simpler one of always prepending "open" before
any events.

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 164 --
 1 file changed, 66 insertions(+), 98 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index fd8eff6..02b85c1 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -56,9 +56,8 @@
 #define ACPI_BUTTON_DEVICE_NAME_LID"Lid Switch"
 #define ACPI_BUTTON_TYPE_LID   0x05
 
-#define ACPI_BUTTON_LID_INIT_IGNORE0x00
+#define ACPI_BUTTON_LID_INIT_METHOD0x00
 #define ACPI_BUTTON_LID_INIT_OPEN  0x01
-#define ACPI_BUTTON_LID_INIT_METHOD0x02
 
 #define _COMPONENT ACPI_BUTTON_COMPONENT
 ACPI_MODULE_NAME("button");
@@ -109,23 +108,20 @@ struct acpi_button {
struct timer_list lid_timer;
char phys[32];  /* for input device */
unsigned long pushed;
-   int last_state;
-   ktime_t last_time;
bool suspended;
 };
 
+static DEFINE_MUTEX(lid_device_lock);
 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
 static struct acpi_device *lid_device;
 static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
 
-static unsigned long lid_report_interval __read_mostly = 500;
-module_param(lid_report_interval, ulong, 0644);
-MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
-
 static unsigned long lid_update_interval __read_mostly = 10 * MSEC_PER_SEC;
 module_param(lid_update_interval, ulong, 0644);
 MODULE_PARM_DESC(lid_update_interval, "Interval (ms) between lid state 
updates");
 
+static bool lid_unreliable __read_mostly = false;
+
 /* --
   FS Interface (/proc)
-- 
*/
@@ -149,79 +145,12 @@ static int acpi_lid_notify_state(struct acpi_device 
*device, int state)
 {
struct acpi_button *button = acpi_driver_data(device);
int ret;
-   ktime_t next_report;
-   bool do_update;
-
-   /*
-* In lid_init_state=ignore mode, if user opens/closes lid
-* frequently with "open" missing, and "last_time" is also updated
-* frequently, "close" cannot be delivered to the userspace.
-* So "last_time" is only updated after a timeout or an actual
-* switch.
-*/
-   if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
-   button->last_state != !!state)
-   do_update = true;
-   else
-   do_update = false;
-
-   next_report = ktime_add(button->last_time,
-   ms_to_ktime(lid_report_interval));
-   if (button->last_state == !!state &&
-   ktime_after(ktime_get(), next_report)) {
-   /* Complain the buggy firmware */
-   pr_warn_once("The lid device is not compliant to SW_LID.\n");
 
-   /*
-* Send the unreliable complement switch event:
-*
-* On most platforms, the lid device is reliable. However
-* there are exceptions:
-* 1. Platforms returning initial lid state as "close" by
-*default after booting/resuming:
-* https://bugzilla.kernel.org/show_bug.cgi?id=89211
-* https://bugzilla.kernel.org/show_bug.cgi?id=106151
-* 2. Platforms never reporting "open" events:
-* https://bugzilla.kernel.org/show_bug.cgi?id=106941
-* On these buggy platforms, the usage model of the ACPI
-* lid device actually is:
-* 1. The initial returning value of _LID may not be
-*reliable.
-* 2. The open event may not be reliable.
-* 3. The close event is reliable.
-*
-

[systemd-devel] [RFC PATCH v4 2/5] ACPI: button: Extends complement switch event support for all modes

2017-05-31 Thread Lv Zheng
Surface Pro 3 is a typical platform where suspend/resume loop problem
can be seen.

The problem is due to a systemd 229 bug:
1. "ignore": always can trigger endless suspend/resume loop
2. "open": sometimes suspend/resume loop can be stopped
3. "method": always can trigger endless susped/resume loop
The buggy systemd unexpectedly waits for an explicit "open" event after
boot/resume or it will suspends. However even when kernel can send a
faked "open" to it, its state machine is still wrong, systemd may not
respond "close" events arrived after "open" or may suddenly suspend
without seeing any instant events.

Recent systemd 233 has fixed this issue:
1. "ignore": everything works fine;
2. "open": no suspend/resume cycle, but sometimes cannot suspend the
   platform again after the first resume;
3. "method": no suspend/resume cycle, but always cannot suspend the
 platform again after the first resume.
The conclusion is: for suspend/resume cycle issue, "ignore" mode fixes
everything, but current "method" mode is still buggy.
The differences are due to button driver only implements complement switch
events for "ignore" mode. Without complement switch events, firmware
triggered "close" cannot be delivered to userspace (confirmed by
evemu-record).

The root cause of the lid state issues is the variation of the platform
firmware implementations:
1. Some platforms send "open" events to OS and the events arrive before
   button driver is resumed;
2. Some platforms send "open" events to OS, but the events arrive after
   button driver is resumed, ex., Samsung N210+;
3. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, and the update events arrive before
   button driver is resumed;
4. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, but the update events arrive after
   button driver is resumed, ex., Surface Pro 3;
5. Some platforms never send "open" events, _LID returns value sticks to
   "close", ex., Surface Pro 1.

Let's check the docking external display issues (see links below):
1. For case 1, both "method"/"ignore" modes can work correctly;
2. For case 2/4/5, both "method"/"ignore" modes cannot work correctly;
3. For case 3, "method" can work correctly while "ignore" mode cannot.
The conclusion is: for docking external display issue, though the issue
still needs graphics layer (graphics drivers or desktop managers) to be
improved to ensure no breakages for case 2/4/5 platforms, there is a case
where "method" mode plays better.

Thus ACPI subsystem has been pushed to revert back to "method" mode due to
regression rule and case 3 (platforms reported on the links should all be
case 3 platforms), and libinput developers have volunteered to help to
provide workarounds when graphics layer is not fixed or systemd is not
updated.

Thus this patch extends the complement switch event support to other modes
using new indication: generating complement switch event for BIOS notified
"close". So that when button driver is reverted back to "method" mode, it
won't act worse than "ignore" mode on fixed systemd.

Tested with systemd 233, all modes worked fine (no suspend/resume loop and
can suspend any times) after applying this patch.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=195455
  https://bugzilla.redhat.com/show_bug.cgi?id=1430259

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 116 +-
 1 file changed, 57 insertions(+), 59 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index 725a15a..36485cf 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -108,6 +108,7 @@ struct acpi_button {
unsigned long pushed;
int last_state;
ktime_t last_time;
+   bool last_is_bios;
bool suspended;
 };
 
@@ -144,78 +145,71 @@ static int acpi_lid_notify_state(struct acpi_device 
*device,
struct acpi_button *button = acpi_driver_data(device);
int ret;
ktime_t next_report;
-   bool do_update;
 
/*
-* In lid_init_state=ignore mode, if user opens/closes lid
-* frequently with "open" missing, and "last_time" is also updated
-* frequently, "close" cannot be delivered to the userspace.
-* So "last_time" is only updated after a timeout or an actual
-  

[systemd-devel] [RFC PATCH v4 1/5] ACPI: button: Add indication of BIOS notification and faked events

2017-05-31 Thread Lv Zheng
This patch adds a parameter to acpi_lid_notify_state() so that it can act
differently against BIOS notification and kernel faked events.

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index 9ad8cdb..725a15a 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -138,7 +138,8 @@ static int acpi_lid_evaluate_state(struct acpi_device 
*device)
return lid_state ? 1 : 0;
 }
 
-static int acpi_lid_notify_state(struct acpi_device *device, int state)
+static int acpi_lid_notify_state(struct acpi_device *device,
+int state, bool is_bios_event)
 {
struct acpi_button *button = acpi_driver_data(device);
int ret;
@@ -360,7 +361,8 @@ int acpi_lid_open(void)
 }
 EXPORT_SYMBOL(acpi_lid_open);
 
-static int acpi_lid_update_state(struct acpi_device *device)
+static int acpi_lid_update_state(struct acpi_device *device,
+bool is_bios_event)
 {
int state;
 
@@ -368,17 +370,17 @@ static int acpi_lid_update_state(struct acpi_device 
*device)
if (state < 0)
return state;
 
-   return acpi_lid_notify_state(device, state);
+   return acpi_lid_notify_state(device, state, is_bios_event);
 }
 
 static void acpi_lid_initialize_state(struct acpi_device *device)
 {
switch (lid_init_state) {
case ACPI_BUTTON_LID_INIT_OPEN:
-   (void)acpi_lid_notify_state(device, 1);
+   (void)acpi_lid_notify_state(device, 1, false);
break;
case ACPI_BUTTON_LID_INIT_METHOD:
-   (void)acpi_lid_update_state(device);
+   (void)acpi_lid_update_state(device, false);
break;
case ACPI_BUTTON_LID_INIT_IGNORE:
default:
@@ -398,7 +400,7 @@ static void acpi_button_notify(struct acpi_device *device, 
u32 event)
case ACPI_BUTTON_NOTIFY_STATUS:
input = button->input;
if (button->type == ACPI_BUTTON_TYPE_LID) {
-   acpi_lid_update_state(device);
+   acpi_lid_update_state(device, true);
} else {
int keycode;
 
-- 
2.7.4

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] [RFC PATCH v3 2/5] ACPI: button: Extends complement switch event support for all modes

2017-05-26 Thread Lv Zheng
Surface Pro 3 is a typical platform where suspend/resume loop problem
can be seen.

The problem is due to a systemd 229 bug:
1. "ignore": always can trigger endless suspend/resume loop
2. "open": sometimes suspend/resume loop can be stopped
3. "method": always can trigger endless susped/resume loop
The buggy systemd unexpectedly waits for an explicit "open" event after
boot/resume or it will suspends. However even when kernel can send a
faked "open" to it, its state machine is still wrong, systemd may not
respond "close" events arrived after "open" or may suddenly suspend
without seeing any instant events.

Recent systemd 233 has fixed this issue:
1. "ignore": everything works fine;
2. "open": no suspend/resume cycle, but sometimes cannot suspend the
   platform again after the first resume;
3. "method": no suspend/resume cycle, but always cannot suspend the
 platform again after the first resume.
The conclusion is: for suspend/resume cycle issue, "ignore" mode fixes
everything, but current "method" mode is still buggy.
Differences are because button driver only implements complement switch
events for "ignore" mode. Without complement switch events, firmware
triggered "close" cannot be delivered to userspace (confirmed by
evemu-record).

The root cause of the lid state issues is the variation of the platform
firmware implementations:
1. Some platforms send "open" events to OS and the events arrive before
   button driver is resumed;
2. Some platforms send "open" events to OS, but the events arrive after
   button driver is resumed, ex., Samsung N210+;
3. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, and the update events arrive before
   button driver is resumed;
4. Some platforms never send "open" events to OS, but send "open" events to
   update the cached _LID return value, but the update events arrive after
   button driver is resumed, ex., Surface Pro 3;
5. Some platforms never send "open" events, _LID returns value sticks to
   "close", ex., Surface Pro 1.

Let's check the docking external display issues (see links below):
1. For case 1, both "method"/"ignore" modes can work correctly;
2. For case 2/4/5, both "method"/"ignore" modes cannot work correctly;
3. For case 3, "method" can work correctly while "ignore" mode cannot.
The conclusion is: for docking external display issue, though the issue
still needs graphics layer (graphics drivers or desktop managers) to be
improved to ensure no breakages for case 2/4/5 platforms, there is a case
where "method" mode plays better.

Thus ACPI subsystem has been pushed to revert back to "method" mode due to
regression rule and case 3 (platforms reported on the links should all be
case 3 platforms), and libinput developers have volunteered to help to
provide workarounds when graphics layer is not fixed or systemd is not
updated.

Thus this patch extends the complement switch event support to other modes
using new indication: generating complement switch event for BIOS notified
"close". So that when button driver is reverted back to "method" mode, it
won't act worse than "ignore" mode on fixed systemd.

Tested with systemd 233, all modes worked fine (no suspend/resume loop and
can suspend any times) after applying this patch.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=195455
  https://bugzilla.redhat.com/show_bug.cgi?id=1430259
Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>

Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 116 +-
 1 file changed, 57 insertions(+), 59 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index 874ba60..a72f5bf 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -108,6 +108,7 @@ struct acpi_button {
unsigned long pushed;
int last_state;
ktime_t last_time;
+   bool last_is_bios;
bool suspended;
 };
 
@@ -144,78 +145,71 @@ static int acpi_lid_notify_state(struct acpi_device 
*device,
struct acpi_button *button = acpi_driver_data(device);
int ret;
ktime_t next_report;
-   bool do_update;
 
/*
-* In lid_init_state=ignore mode, if user opens/closes lid
-* frequently with "open" missing, and "last_time" is also updated
-* frequently, "close" cannot be delivered to the userspace.
-* So "last_time" is

[systemd-devel] [RFC PATCH v3 1/5] ACPI: button: Add indication of BIOS notification and faked events

2017-05-26 Thread Lv Zheng
This patch adds a parameter to acpi_lid_notify_state() so that it can act
differently against BIOS notification and kernel faked events.

Cc: <systemd-devel@lists.freedesktop.org>
Cc: Benjamin Tissoires <benjamin.tissoi...@redhat.com>
Cc: Peter Hutterer <peter.hutte...@who-t.net>
Signed-off-by: Lv Zheng <lv.zh...@intel.com>
---
 drivers/acpi/button.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
index 6d5a8c1..874ba60 100644
--- a/drivers/acpi/button.c
+++ b/drivers/acpi/button.c
@@ -138,7 +138,8 @@ static int acpi_lid_evaluate_state(struct acpi_device 
*device)
return lid_state ? 1 : 0;
 }
 
-static int acpi_lid_notify_state(struct acpi_device *device, int state)
+static int acpi_lid_notify_state(struct acpi_device *device,
+int state, bool bios_notify)
 {
struct acpi_button *button = acpi_driver_data(device);
int ret;
@@ -360,7 +361,8 @@ int acpi_lid_open(void)
 }
 EXPORT_SYMBOL(acpi_lid_open);
 
-static int acpi_lid_update_state(struct acpi_device *device)
+static int acpi_lid_update_state(struct acpi_device *device,
+bool bios_notify)
 {
int state;
 
@@ -368,17 +370,17 @@ static int acpi_lid_update_state(struct acpi_device 
*device)
if (state < 0)
return state;
 
-   return acpi_lid_notify_state(device, state);
+   return acpi_lid_notify_state(device, state, bios_notify);
 }
 
 static void acpi_lid_initialize_state(struct acpi_device *device)
 {
switch (lid_init_state) {
case ACPI_BUTTON_LID_INIT_OPEN:
-   (void)acpi_lid_notify_state(device, 1);
+   (void)acpi_lid_notify_state(device, 1, false);
break;
case ACPI_BUTTON_LID_INIT_METHOD:
-   (void)acpi_lid_update_state(device);
+   (void)acpi_lid_update_state(device, false);
break;
case ACPI_BUTTON_LID_INIT_IGNORE:
default:
@@ -398,7 +400,7 @@ static void acpi_button_notify(struct acpi_device *device, 
u32 event)
case ACPI_BUTTON_NOTIFY_STATUS:
input = button->input;
if (button->type == ACPI_BUTTON_TYPE_LID) {
-   acpi_lid_update_state(device);
+   acpi_lid_update_state(device, true);
} else {
int keycode;
 
-- 
2.7.4

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel