Skip to content

Commit 4369c64

Browse files
committed
Input: Send events one packet at a time
On heavy event loads, such as a multitouch driver, the irqsoff latency can be as high as 250 us. By accumulating a frame worth of data before passing it on, the latency can be dramatically reduced. As a side effect, the special EV_SYN handling can be removed, since the frame is now atomic. This patch adds the events() handler callback and uses it if it exists. The latency is improved by 50 us even without the callback. Cc: Daniel Kurtz <[email protected]> Tested-by: Benjamin Tissoires <[email protected]> Tested-by: Ping Cheng <[email protected]> Tested-by: Sedat Dilek <[email protected]> Acked-by: Dmitry Torokhov <[email protected]> Signed-off-by: Henrik Rydberg <[email protected]>
1 parent 352ac4b commit 4369c64

File tree

2 files changed

+144
-56
lines changed

2 files changed

+144
-56
lines changed

drivers/input/input.c

Lines changed: 123 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static DEFINE_MUTEX(input_mutex);
4747

4848
static struct input_handler *input_table[8];
4949

50+
static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 };
51+
5052
static inline int is_event_supported(unsigned int code,
5153
unsigned long *bm, unsigned int max)
5254
{
@@ -90,46 +92,81 @@ static void input_stop_autorepeat(struct input_dev *dev)
9092
* filtered out, through all open handles. This function is called with
9193
* dev->event_lock held and interrupts disabled.
9294
*/
93-
static void input_pass_event(struct input_dev *dev,
94-
unsigned int type, unsigned int code, int value)
95+
static unsigned int input_to_handler(struct input_handle *handle,
96+
struct input_value *vals, unsigned int count)
9597
{
96-
struct input_handler *handler;
97-
struct input_handle *handle;
98+
struct input_handler *handler = handle->handler;
99+
struct input_value *end = vals;
100+
struct input_value *v;
98101

99-
rcu_read_lock();
102+
for (v = vals; v != vals + count; v++) {
103+
if (handler->filter &&
104+
handler->filter(handle, v->type, v->code, v->value))
105+
continue;
106+
if (end != v)
107+
*end = *v;
108+
end++;
109+
}
100110

101-
handle = rcu_dereference(dev->grab);
102-
if (handle)
103-
handle->handler->event(handle, type, code, value);
104-
else {
105-
bool filtered = false;
111+
count = end - vals;
112+
if (!count)
113+
return 0;
106114

107-
list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
108-
if (!handle->open)
109-
continue;
115+
if (handler->events)
116+
handler->events(handle, vals, count);
117+
else if (handler->event)
118+
for (v = vals; v != end; v++)
119+
handler->event(handle, v->type, v->code, v->value);
120+
121+
return count;
122+
}
123+
124+
/*
125+
* Pass values first through all filters and then, if event has not been
126+
* filtered out, through all open handles. This function is called with
127+
* dev->event_lock held and interrupts disabled.
128+
*/
129+
static void input_pass_values(struct input_dev *dev,
130+
struct input_value *vals, unsigned int count)
131+
{
132+
struct input_handle *handle;
133+
struct input_value *v;
110134

111-
handler = handle->handler;
112-
if (!handler->filter) {
113-
if (filtered)
114-
break;
135+
if (!count)
136+
return;
115137

116-
handler->event(handle, type, code, value);
138+
rcu_read_lock();
117139

118-
} else if (handler->filter(handle, type, code, value))
119-
filtered = true;
120-
}
140+
handle = rcu_dereference(dev->grab);
141+
if (handle) {
142+
count = input_to_handler(handle, vals, count);
143+
} else {
144+
list_for_each_entry_rcu(handle, &dev->h_list, d_node)
145+
if (handle->open)
146+
count = input_to_handler(handle, vals, count);
121147
}
122148

123149
rcu_read_unlock();
124150

151+
add_input_randomness(vals->type, vals->code, vals->value);
152+
125153
/* trigger auto repeat for key events */
126-
if (type == EV_KEY && value != 2) {
127-
if (value)
128-
input_start_autorepeat(dev, code);
129-
else
130-
input_stop_autorepeat(dev);
154+
for (v = vals; v != vals + count; v++) {
155+
if (v->type == EV_KEY && v->value != 2) {
156+
if (v->value)
157+
input_start_autorepeat(dev, v->code);
158+
else
159+
input_stop_autorepeat(dev);
160+
}
131161
}
162+
}
132163

164+
static void input_pass_event(struct input_dev *dev,
165+
unsigned int type, unsigned int code, int value)
166+
{
167+
struct input_value vals[] = { { type, code, value } };
168+
169+
input_pass_values(dev, vals, ARRAY_SIZE(vals));
133170
}
134171

135172
/*
@@ -146,18 +183,12 @@ static void input_repeat_key(unsigned long data)
146183

147184
if (test_bit(dev->repeat_key, dev->key) &&
148185
is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
186+
struct input_value vals[] = {
187+
{ EV_KEY, dev->repeat_key, 2 },
188+
input_value_sync
189+
};
149190

150-
input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
151-
152-
if (dev->sync) {
153-
/*
154-
* Only send SYN_REPORT if we are not in a middle
155-
* of driver parsing a new hardware packet.
156-
* Otherwise assume that the driver will send
157-
* SYN_REPORT once it's done.
158-
*/
159-
input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
160-
}
191+
input_pass_values(dev, vals, ARRAY_SIZE(vals));
161192

162193
if (dev->rep[REP_PERIOD])
163194
mod_timer(&dev->timer, jiffies +
@@ -170,6 +201,8 @@ static void input_repeat_key(unsigned long data)
170201
#define INPUT_IGNORE_EVENT 0
171202
#define INPUT_PASS_TO_HANDLERS 1
172203
#define INPUT_PASS_TO_DEVICE 2
204+
#define INPUT_SLOT 4
205+
#define INPUT_FLUSH 8
173206
#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
174207

175208
static int input_handle_abs_event(struct input_dev *dev,
@@ -216,14 +249,14 @@ static int input_handle_abs_event(struct input_dev *dev,
216249
/* Flush pending "slot" event */
217250
if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
218251
input_abs_set_val(dev, ABS_MT_SLOT, mt->slot);
219-
input_pass_event(dev, EV_ABS, ABS_MT_SLOT, mt->slot);
252+
return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;
220253
}
221254

222255
return INPUT_PASS_TO_HANDLERS;
223256
}
224257

225-
static void input_handle_event(struct input_dev *dev,
226-
unsigned int type, unsigned int code, int value)
258+
static int input_get_disposition(struct input_dev *dev,
259+
unsigned int type, unsigned int code, int value)
227260
{
228261
int disposition = INPUT_IGNORE_EVENT;
229262

@@ -236,13 +269,9 @@ static void input_handle_event(struct input_dev *dev,
236269
break;
237270

238271
case SYN_REPORT:
239-
if (!dev->sync) {
240-
dev->sync = true;
241-
disposition = INPUT_PASS_TO_HANDLERS;
242-
}
272+
disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
243273
break;
244274
case SYN_MT_REPORT:
245-
dev->sync = false;
246275
disposition = INPUT_PASS_TO_HANDLERS;
247276
break;
248277
}
@@ -327,14 +356,48 @@ static void input_handle_event(struct input_dev *dev,
327356
break;
328357
}
329358

330-
if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
331-
dev->sync = false;
359+
return disposition;
360+
}
361+
362+
static void input_handle_event(struct input_dev *dev,
363+
unsigned int type, unsigned int code, int value)
364+
{
365+
int disposition;
366+
367+
disposition = input_get_disposition(dev, type, code, value);
332368

333369
if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
334370
dev->event(dev, type, code, value);
335371

336-
if (disposition & INPUT_PASS_TO_HANDLERS)
337-
input_pass_event(dev, type, code, value);
372+
if (!dev->vals)
373+
return;
374+
375+
if (disposition & INPUT_PASS_TO_HANDLERS) {
376+
struct input_value *v;
377+
378+
if (disposition & INPUT_SLOT) {
379+
v = &dev->vals[dev->num_vals++];
380+
v->type = EV_ABS;
381+
v->code = ABS_MT_SLOT;
382+
v->value = dev->mt->slot;
383+
}
384+
385+
v = &dev->vals[dev->num_vals++];
386+
v->type = type;
387+
v->code = code;
388+
v->value = value;
389+
}
390+
391+
if (disposition & INPUT_FLUSH) {
392+
if (dev->num_vals >= 2)
393+
input_pass_values(dev, dev->vals, dev->num_vals);
394+
dev->num_vals = 0;
395+
} else if (dev->num_vals >= dev->max_vals - 2) {
396+
dev->vals[dev->num_vals++] = input_value_sync;
397+
input_pass_values(dev, dev->vals, dev->num_vals);
398+
dev->num_vals = 0;
399+
}
400+
338401
}
339402

340403
/**
@@ -362,7 +425,6 @@ void input_event(struct input_dev *dev,
362425
if (is_event_supported(type, dev->evbit, EV_MAX)) {
363426

364427
spin_lock_irqsave(&dev->event_lock, flags);
365-
add_input_randomness(type, code, value);
366428
input_handle_event(dev, type, code, value);
367429
spin_unlock_irqrestore(&dev->event_lock, flags);
368430
}
@@ -841,10 +903,12 @@ int input_set_keycode(struct input_dev *dev,
841903
if (test_bit(EV_KEY, dev->evbit) &&
842904
!is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
843905
__test_and_clear_bit(old_keycode, dev->key)) {
906+
struct input_value vals[] = {
907+
{ EV_KEY, old_keycode, 0 },
908+
input_value_sync
909+
};
844910

845-
input_pass_event(dev, EV_KEY, old_keycode, 0);
846-
if (dev->sync)
847-
input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
911+
input_pass_values(dev, vals, ARRAY_SIZE(vals));
848912
}
849913

850914
out:
@@ -1426,6 +1490,7 @@ static void input_dev_release(struct device *device)
14261490
input_ff_destroy(dev);
14271491
input_mt_destroy_slots(dev);
14281492
kfree(dev->absinfo);
1493+
kfree(dev->vals);
14291494
kfree(dev);
14301495

14311496
module_put(THIS_MODULE);
@@ -1846,6 +1911,11 @@ int input_register_device(struct input_dev *dev)
18461911
if (dev->hint_events_per_packet < packet_size)
18471912
dev->hint_events_per_packet = packet_size;
18481913

1914+
dev->max_vals = max(dev->hint_events_per_packet, packet_size) + 2;
1915+
dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
1916+
if (!dev->vals)
1917+
return -ENOMEM;
1918+
18491919
/*
18501920
* If delay and period are pre-set by the driver, then autorepeating
18511921
* is handled by the driver itself and we don't do it in input.c.

include/linux/input.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,18 @@ struct ff_effect {
11681168
#include <linux/timer.h>
11691169
#include <linux/mod_devicetable.h>
11701170

1171+
/**
1172+
* struct input_value - input value representation
1173+
* @type: type of value (EV_KEY, EV_ABS, etc)
1174+
* @code: the value code
1175+
* @value: the value
1176+
*/
1177+
struct input_value {
1178+
__u16 type;
1179+
__u16 code;
1180+
__s32 value;
1181+
};
1182+
11711183
/**
11721184
* struct input_dev - represents an input device
11731185
* @name: name of the device
@@ -1240,7 +1252,6 @@ struct ff_effect {
12401252
* last user closes the device
12411253
* @going_away: marks devices that are in a middle of unregistering and
12421254
* causes input_open_device*() fail with -ENODEV.
1243-
* @sync: set to %true when there were no new events since last EV_SYN
12441255
* @dev: driver model's view of this device
12451256
* @h_list: list of input handles associated with the device. When
12461257
* accessing the list dev->mutex must be held
@@ -1305,12 +1316,14 @@ struct input_dev {
13051316
unsigned int users;
13061317
bool going_away;
13071318

1308-
bool sync;
1309-
13101319
struct device dev;
13111320

13121321
struct list_head h_list;
13131322
struct list_head node;
1323+
1324+
unsigned int num_vals;
1325+
unsigned int max_vals;
1326+
struct input_value *vals;
13141327
};
13151328
#define to_input_dev(d) container_of(d, struct input_dev, dev)
13161329

@@ -1371,6 +1384,9 @@ struct input_handle;
13711384
* @event: event handler. This method is being called by input core with
13721385
* interrupts disabled and dev->event_lock spinlock held and so
13731386
* it may not sleep
1387+
* @events: event sequence handler. This method is being called by
1388+
* input core with interrupts disabled and dev->event_lock
1389+
* spinlock held and so it may not sleep
13741390
* @filter: similar to @event; separates normal event handlers from
13751391
* "filters".
13761392
* @match: called after comparing device's id with handler's id_table
@@ -1407,6 +1423,8 @@ struct input_handler {
14071423
void *private;
14081424

14091425
void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
1426+
void (*events)(struct input_handle *handle,
1427+
const struct input_value *vals, unsigned int count);
14101428
bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
14111429
bool (*match)(struct input_handler *handler, struct input_dev *dev);
14121430
int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);

0 commit comments

Comments
 (0)