Re: [PATCH 1/2] Input: Add "inhibited" property

Dmitry Torokhov
2020-05-06T00:27:51Z

Hi Andrzej,

On Thu, Apr 30, 2020 at 06:16:40PM +0200, Andrzej Pietrasiewicz wrote:
> From: Patrik Fimml <patrikf@chromium.org>
>
> Userspace might want to implement a policy to temporarily disregard input
> from certain devices, including not treating them as wakeup sources.
>
> An example use case is a laptop, whose keyboard can be folded under the
> screen to create tablet-like experience. The user then must hold the laptop
> in such a way that it is difficult to avoid pressing the keyboard keys. It
> is therefore desirable to temporarily disregard input from the keyboard,
> until it is folded back. This obviously is a policy which should be kept
> out of the kernel, but the kernel must provide suitable means to implement
> such a policy.
>
> This patch adds a sysfs interface for exactly this purpose.
>
> To implement the said interface it adds an "inhibited" property to struct
> input_dev and two optional methods - inhibit() and uninhibit(), and
> effectively creates four states a device can be in: closed uninhibited,
> closed inhibited, open uninhibited, open inhibited. It also defers calling
> driver's ->open() and ->close() to until they are actually needed, e.g. it
> makes no sense to prepare the underlying device for generating events
> (->open()) if the device is inhibited.
>
> uninhibit
> closed <------------ closed
> uninhibited ------------> inhibited
> | ^ inhibit | ^
> 1st | | 1st | |
> open | | open | |
> | | | |
> | | last | | last
> | | close | | close
> v | uninhibit v |
> open <------------ open
> uninhibited ------------> inhibited
>
> The top inhibit/uninhibit transition happens when users == 0.
> The bottom inhibit/uninhibit transition happens when users > 0.
> The left open/close transition happens when !inhibited.
> The right open/close transition happens when inhibited.
> Due to all transitions being serialized with dev->mutex, it is impossible
> to have "diagonal" transitions between closed uninhibited and open
> inhibited or between open uninhibited and closed inhibited.
>
> While open()/close() could be used in place of uninhibit()/inhibit(),
> underlying driver implementations have very different ideas of what it
> means to open/close, to suspend/resume or to pm runtime suspend/resume.
> On top of that close() does not return a code, so using close() there be
> no way to actually fail inhibiting.

While I totally agree that we want to allow drivers to have flexibility
in implementing open/close/inhibit/uninhibit, I believe for majority of
devices using close() in place of inhibit and open() in place of
uninhibit is a very viable solution and will allow us to realize most of
the power savings.

Can we try to fall back on open/close when uninhibit/inhbit is not
present?

I think this will also require some preliminary work in the drivers with
regard to suspend/resume, as they tend to directly check
input_dev->users when deciding whether they need to power up the
hardware/take it out of sleep. With inhibit in place we need to beef up
this condition, I'd like to have something

bool input_device_enabled(struct input_dev *dev)
{
lockdep_assert_held(&dev->mutex);
return !dev->inhibited && dev->users != 0;
}

or something similar.

Thanks.

--
Dmitry