Skip to content

Commit 59b7a5a

Browse files
committed
Input: properly queue synthetic events
We should not be passing synthetic events (such as autorepeat events) out of order with the events coming from the hardware device, but rather add them to pending events and flush them all at once. This also fixes an issue with timestamps for key release events carrying stale data from the previous autorepeat event. Reviewed-by: Angela Czubak <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 3963680 commit 59b7a5a

File tree

1 file changed

+63
-62
lines changed

1 file changed

+63
-62
lines changed

drivers/input/input.c

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -174,44 +174,6 @@ static void input_pass_values(struct input_dev *dev,
174174
}
175175
}
176176

177-
static void input_pass_event(struct input_dev *dev,
178-
unsigned int type, unsigned int code, int value)
179-
{
180-
struct input_value vals[] = { { type, code, value } };
181-
182-
input_pass_values(dev, vals, ARRAY_SIZE(vals));
183-
}
184-
185-
/*
186-
* Generate software autorepeat event. Note that we take
187-
* dev->event_lock here to avoid racing with input_event
188-
* which may cause keys get "stuck".
189-
*/
190-
static void input_repeat_key(struct timer_list *t)
191-
{
192-
struct input_dev *dev = from_timer(dev, t, timer);
193-
unsigned long flags;
194-
195-
spin_lock_irqsave(&dev->event_lock, flags);
196-
197-
if (test_bit(dev->repeat_key, dev->key) &&
198-
is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
199-
struct input_value vals[] = {
200-
{ EV_KEY, dev->repeat_key, 2 },
201-
input_value_sync
202-
};
203-
204-
input_set_timestamp(dev, ktime_get());
205-
input_pass_values(dev, vals, ARRAY_SIZE(vals));
206-
207-
if (dev->rep[REP_PERIOD])
208-
mod_timer(&dev->timer, jiffies +
209-
msecs_to_jiffies(dev->rep[REP_PERIOD]));
210-
}
211-
212-
spin_unlock_irqrestore(&dev->event_lock, flags);
213-
}
214-
215177
#define INPUT_IGNORE_EVENT 0
216178
#define INPUT_PASS_TO_HANDLERS 1
217179
#define INPUT_PASS_TO_DEVICE 2
@@ -275,6 +237,10 @@ static int input_get_disposition(struct input_dev *dev,
275237
int disposition = INPUT_IGNORE_EVENT;
276238
int value = *pval;
277239

240+
/* filter-out events from inhibited devices */
241+
if (dev->inhibited)
242+
return INPUT_IGNORE_EVENT;
243+
278244
switch (type) {
279245

280246
case EV_SYN:
@@ -375,19 +341,9 @@ static int input_get_disposition(struct input_dev *dev,
375341
return disposition;
376342
}
377343

378-
static void input_handle_event(struct input_dev *dev,
379-
unsigned int type, unsigned int code, int value)
344+
static void input_event_dispose(struct input_dev *dev, int disposition,
345+
unsigned int type, unsigned int code, int value)
380346
{
381-
int disposition;
382-
383-
/* filter-out events from inhibited devices */
384-
if (dev->inhibited)
385-
return;
386-
387-
disposition = input_get_disposition(dev, type, code, &value);
388-
if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
389-
add_input_randomness(type, code, value);
390-
391347
if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
392348
dev->event(dev, type, code, value);
393349

@@ -426,7 +382,22 @@ static void input_handle_event(struct input_dev *dev,
426382
input_pass_values(dev, dev->vals, dev->num_vals);
427383
dev->num_vals = 0;
428384
}
385+
}
386+
387+
static void input_handle_event(struct input_dev *dev,
388+
unsigned int type, unsigned int code, int value)
389+
{
390+
int disposition;
391+
392+
lockdep_assert_held(&dev->event_lock);
393+
394+
disposition = input_get_disposition(dev, type, code, &value);
395+
if (disposition != INPUT_IGNORE_EVENT) {
396+
if (type != EV_SYN)
397+
add_input_randomness(type, code, value);
429398

399+
input_event_dispose(dev, disposition, type, code, value);
400+
}
430401
}
431402

432403
/**
@@ -613,7 +584,7 @@ static void __input_release_device(struct input_handle *handle)
613584
lockdep_is_held(&dev->mutex));
614585
if (grabber == handle) {
615586
rcu_assign_pointer(dev->grab, NULL);
616-
/* Make sure input_pass_event() notices that grab is gone */
587+
/* Make sure input_pass_values() notices that grab is gone */
617588
synchronize_rcu();
618589

619590
list_for_each_entry(handle, &dev->h_list, d_node)
@@ -736,7 +707,7 @@ void input_close_device(struct input_handle *handle)
736707

737708
if (!--handle->open) {
738709
/*
739-
* synchronize_rcu() makes sure that input_pass_event()
710+
* synchronize_rcu() makes sure that input_pass_values()
740711
* completed and that no more input events are delivered
741712
* through this handle
742713
*/
@@ -758,14 +729,12 @@ static void input_dev_release_keys(struct input_dev *dev)
758729

759730
if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
760731
for_each_set_bit(code, dev->key, KEY_CNT) {
761-
input_pass_event(dev, EV_KEY, code, 0);
732+
input_handle_event(dev, EV_KEY, code, 0);
762733
need_sync = true;
763734
}
764735

765736
if (need_sync)
766-
input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
767-
768-
memset(dev->key, 0, sizeof(dev->key));
737+
input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
769738
}
770739
}
771740

@@ -1004,12 +973,16 @@ int input_set_keycode(struct input_dev *dev,
1004973
} else if (test_bit(EV_KEY, dev->evbit) &&
1005974
!is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
1006975
__test_and_clear_bit(old_keycode, dev->key)) {
1007-
struct input_value vals[] = {
1008-
{ EV_KEY, old_keycode, 0 },
1009-
input_value_sync
1010-
};
1011-
1012-
input_pass_values(dev, vals, ARRAY_SIZE(vals));
976+
/*
977+
* We have to use input_event_dispose() here directly instead
978+
* of input_handle_event() because the key we want to release
979+
* here is considered no longer supported by the device and
980+
* input_handle_event() will ignore it.
981+
*/
982+
input_event_dispose(dev, INPUT_PASS_TO_HANDLERS,
983+
EV_KEY, old_keycode, 0);
984+
input_event_dispose(dev, INPUT_PASS_TO_HANDLERS | INPUT_FLUSH,
985+
EV_SYN, SYN_REPORT, 1);
1013986
}
1014987

1015988
out:
@@ -2259,6 +2232,34 @@ static void devm_input_device_unregister(struct device *dev, void *res)
22592232
__input_unregister_device(input);
22602233
}
22612234

2235+
/*
2236+
* Generate software autorepeat event. Note that we take
2237+
* dev->event_lock here to avoid racing with input_event
2238+
* which may cause keys get "stuck".
2239+
*/
2240+
static void input_repeat_key(struct timer_list *t)
2241+
{
2242+
struct input_dev *dev = from_timer(dev, t, timer);
2243+
unsigned long flags;
2244+
2245+
spin_lock_irqsave(&dev->event_lock, flags);
2246+
2247+
if (!dev->inhibited &&
2248+
test_bit(dev->repeat_key, dev->key) &&
2249+
is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
2250+
2251+
input_set_timestamp(dev, ktime_get());
2252+
input_handle_event(dev, EV_KEY, dev->repeat_key, 2);
2253+
input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
2254+
2255+
if (dev->rep[REP_PERIOD])
2256+
mod_timer(&dev->timer, jiffies +
2257+
msecs_to_jiffies(dev->rep[REP_PERIOD]));
2258+
}
2259+
2260+
spin_unlock_irqrestore(&dev->event_lock, flags);
2261+
}
2262+
22622263
/**
22632264
* input_enable_softrepeat - enable software autorepeat
22642265
* @dev: input device

0 commit comments

Comments
 (0)