Re: [RFC PATCH] input: Add disable sysfs entry for every input device

From: Bastien Nocera
Date: Mon Jan 02 2017 - 10:27:51 EST


On Sun, 2016-12-25 at 11:04 +0100, Pali RohÃr wrote:
> This patch allows user to disable events from any input device so
> events
> would not be delivered to userspace.
>
> Currently there is no way to disable particular input device by
> kernel.
> User for different reasons would need it for integrated PS/2 keyboard
> or
> touchpad in notebook or touchscreen on mobile device to prevent
> sending
> events. E.g. mobile phone in pocket or broken integrated PS/2
> keyboard.
>
> This is just a RFC patch, not tested yet. Original post about
> motivation
> about this patch is there: https://lkml.org/lkml/2014/11/29/92

Having implemented something of that ilk in user-space (we
automatically disable touch devices when the associated screen is
turned off/suspended), I think this might need more thought.

What happens when a device is opened and the device disabled through
sysfs, are the users revoked?

Does this put the device in suspend in the same way that closing the
device's last user does?

Is this not better implemented in user-space at the session level,
where it knows about which output corresponds to which input device?

Is this useful enough to disable misbehaving devices on hardware, so
that the device is not effective on boot?

>
> Signed-off-by: Pali RohÃr <pali.rohar@xxxxxxxxx>
> ---
> Âdrivers/input/input.c |ÂÂÂ35 +++++++++++++++++++++++++++++++++++
> Âinclude/linux/input.h |ÂÂÂÂ4 ++++
> Â2 files changed, 39 insertions(+)
>
> diff --git a/drivers/input/input.c b/drivers/input/input.c
> index d95c34e..9f0da7e 100644
> --- a/drivers/input/input.c
> +++ b/drivers/input/input.c
> @@ -430,6 +430,9 @@ void input_event(struct input_dev *dev,
> Â{
> Â unsigned long flags;
> Â
> + if (unlikely(dev->disabled))
> + return;
> +
> Â if (is_event_supported(type, dev->evbit, EV_MAX)) {
> Â
> Â spin_lock_irqsave(&dev->event_lock, flags);
> @@ -457,6 +460,9 @@ void input_inject_event(struct input_handle
> *handle,
> Â struct input_handle *grab;
> Â unsigned long flags;
> Â
> + if (unlikely(dev->disabled))
> + return;
> +
> Â if (is_event_supported(type, dev->evbit, EV_MAX)) {
> Â spin_lock_irqsave(&dev->event_lock, flags);
> Â
> @@ -1389,12 +1395,41 @@ static ssize_t
> input_dev_show_properties(struct device *dev,
> Â}
> Âstatic DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties,
> NULL);
> Â
> +static ssize_t input_dev_show_disable(struct device *dev,
> + ÂÂÂÂÂÂstruct device_attribute *attr,
> + ÂÂÂÂÂÂchar *buf)
> +{
> + struct input_dev *input_dev = to_input_dev(dev);
> +
> + return snprintf(buf, PAGE_SIZE, "%d\n", input_dev->disabled
> ? 1 : 0);
> +}
> +static ssize_t input_dev_store_disable(struct device *dev,
> + ÂÂÂÂÂÂÂstruct device_attribute
> *attr,
> + ÂÂÂÂÂÂÂconst char *buf, size_t
> count)
> +{
> + struct input_dev *input_dev = to_input_dev(dev);
> + int disable;
> + int ret;
> +
> + ret = kstrtoint(buf, 0, &disable);
> + if (ret)
> + return ret;
> +
> + if (disable != 0 && disable != 1)
> + return -EINVAL;
> +
> + input_dev->disabled = disable;
> + return count;
> +}
> +static DEVICE_ATTR(disable, S_IRUGO | S_IWUSR,
> input_dev_show_disable, input_dev_store_disable);
> +
> Âstatic struct attribute *input_dev_attrs[] = {
> Â &dev_attr_name.attr,
> Â &dev_attr_phys.attr,
> Â &dev_attr_uniq.attr,
> Â &dev_attr_modalias.attr,
> Â &dev_attr_properties.attr,
> + &dev_arrr_disable.attr,
> Â NULL
> Â};
> Â
> diff --git a/include/linux/input.h b/include/linux/input.h
> index a65e3b2..e390b56 100644
> --- a/include/linux/input.h
> +++ b/include/linux/input.h
> @@ -117,6 +117,8 @@ struct input_value {
> Â * @vals: array of values queued in the current frame
> Â * @devres_managed: indicates that devices is managed with devres
> framework
> Â * and needs not be explicitly unregistered or freed.
> + * @disabled: indicates that device is in disabled state and kernel
> drop
> + * all events from it
> Â */
> Âstruct input_dev {
> Â const char *name;
> @@ -187,6 +189,8 @@ struct input_dev {
> Â struct input_value *vals;
> Â
> Â bool devres_managed;
> +
> + bool disabled;
> Â};
> Â#define to_input_dev(d) container_of(d, struct input_dev, dev)
> Â