]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/3.10.72/hid-input-fix-confusion-on-conflicting-mappings.patch
4.9-stable patches
[thirdparty/kernel/stable-queue.git] / releases / 3.10.72 / hid-input-fix-confusion-on-conflicting-mappings.patch
CommitLineData
d4591719
GKH
1From 6ce901eb61aa30ba8565c62049ee80c90728ef14 Mon Sep 17 00:00:00 2001
2From: David Herrmann <dh.herrmann@gmail.com>
3Date: Mon, 29 Dec 2014 15:21:26 +0100
4Subject: HID: input: fix confusion on conflicting mappings
5
6From: David Herrmann <dh.herrmann@gmail.com>
7
8commit 6ce901eb61aa30ba8565c62049ee80c90728ef14 upstream.
9
10On an PC-101/103/104 keyboard (American layout) the 'Enter' key and its
11neighbours look like this:
12
13 +---+ +---+ +-------+
14 | 1 | | 2 | | 5 |
15 +---+ +---+ +-------+
16 +---+ +-----------+
17 | 3 | | 4 |
18 +---+ +-----------+
19
20On a PC-102/105 keyboard (European layout) it looks like this:
21
22 +---+ +---+ +-------+
23 | 1 | | 2 | | |
24 +---+ +---+ +-+ 4 |
25 +---+ +---+ | |
26 | 3 | | 5 | | |
27 +---+ +---+ +-----+
28
29(Note that the number of keys is the same, but key '5' is moved down and
30 the shape of key '4' is changed. Keys '1' to '3' are exactly the same.)
31
32The keys 1-4 report the same scan-code in HID in both layouts, even though
33the keysym they produce is usually different depending on the XKB-keymap
34used by user-space.
35However, key '5' (US 'backslash'/'pipe') reports 0x31 for the upper layout
36and 0x32 for the lower layout, as defined by the HID spec. This is highly
37confusing as the linux-input API uses a single keycode for both.
38
39So far, this was never a problem as there never has been a keyboard with
40both of those keys present at the same time. It would have to look
41something like this:
42
43 +---+ +---+ +-------+
44 | 1 | | 2 | | x31 |
45 +---+ +---+ +-------+
46 +---+ +---+ +-----+
47 | 3 | |x32| | 4 |
48 +---+ +---+ +-----+
49
50HID can represent such a keyboard, but the linux-input API cannot.
51Furthermore, any user-space mapping would be confused by this and,
52luckily, no-one ever produced such hardware.
53
54Now, the HID input layer fixed this mess by mapping both 0x31 and 0x32 to
55the same keycode (KEY_BACKSLASH==0x2b). As only one of both physical keys
56is present on a hardware, this works just fine.
57
58Lets introduce hardware-vendors into this:
59------------------------------------------
60
61Unfortunately, it seems way to expensive to produce a different device for
62American and European layouts. Therefore, hardware-vendors put both keys,
63(0x31 and 0x32) on the same keyboard, but only one of them is hooked up
64to the physical button, the other one is 'dead'.
65This means, they can use the same hardware, with a different button-layout
66and automatically produce the correct HID events for American *and*
67European layouts. This is unproblematic for normal keyboards, as the
68'dead' key will never report any KEY-DOWN events. But RollOver keyboards
69send the whole matrix on each key-event, allowing n-key roll-over mode.
70This means, we get a 0x31 and 0x32 event on each key-press. One of them
71will always be 0, the other reports the real state. As we map both to the
72same keycode, we will get spurious key-events, even though the real
73key-state never changed.
74
75The easiest way would be to blacklist 'dead' keys and never handle those.
76We could simply read the 'country' tag of USB devices and blacklist either
77key according to the layout. But... hardware vendors... want the same
78device for all countries and thus many of them set 'country' to 0 for all
79devices. Meh..
80
81So we have to deal with this properly. As we cannot know which of the keys
82is 'dead', we either need a heuristic and track those keys, or we simply
83make use of our value-tracking for HID fields. We simply ignore HID events
84for absolute data if the data didn't change. As HID tracks events on the
85HID level, we haven't done the keycode translation, yet. Therefore, the
86'dead' key is tracked independently of the real key, therefore, any events
87on it will be ignored.
88
89This patch simply discards any HID events for absolute data if it didn't
90change compared to the last report. We need to ignore relative and
91buffered-byte reports for obvious reasons. But those cannot be affected by
92this bug, so we're fine.
93
94Preferably, we'd do this filtering on the HID-core level. But this might
95break a lot of custom drivers, if they do not follow the HID specs.
96Therefore, we do this late in hid-input just before we inject it into the
97input layer (which does the exact same filtering, but on the keycode
98level).
99
100If this turns out to break some devices, we might have to limit filtering
101to EV_KEY events. But lets try to do the Right Thing first, and properly
102filter any absolute data that didn't change.
103
104This patch is tagged for 'stable' as it fixes a lot of n-key RollOver
105hardware. We might wanna wait with backporting for a while, before we know
106it doesn't break anything else, though.
107
108Reported-by: Adam Goode <adam@spicenitz.org>
109Reported-by: Fredrik Hallenberg <megahallon@gmail.com>
110Tested-by: Fredrik Hallenberg <megahallon@gmail.com>
111Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
112Signed-off-by: Jiri Kosina <jkosina@suse.cz>
113Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
114
115---
116 drivers/hid/hid-input.c | 16 ++++++++++++++++
117 1 file changed, 16 insertions(+)
118
119--- a/drivers/hid/hid-input.c
120+++ b/drivers/hid/hid-input.c
121@@ -1066,6 +1066,22 @@ void hidinput_hid_event(struct hid_devic
122 return;
123 }
124
125+ /*
126+ * Ignore reports for absolute data if the data didn't change. This is
127+ * not only an optimization but also fixes 'dead' key reports. Some
128+ * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID
129+ * 0x31 and 0x32) report multiple keys, even though a localized keyboard
130+ * can only have one of them physically available. The 'dead' keys
131+ * report constant 0. As all map to the same keycode, they'd confuse
132+ * the input layer. If we filter the 'dead' keys on the HID level, we
133+ * skip the keycode translation and only forward real events.
134+ */
135+ if (!(field->flags & (HID_MAIN_ITEM_RELATIVE |
136+ HID_MAIN_ITEM_BUFFERED_BYTE)) &&
137+ usage->usage_index < field->maxusage &&
138+ value == field->value[usage->usage_index])
139+ return;
140+
141 /* report the usage code as scancode if the key status has changed */
142 if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
143 input_event(input, EV_MSC, MSC_SCAN, usage->hid);