Skip to content

Commit a0bf0ea

Browse files
committed
Input: hid-input - switch to using new keycode interface
Switch HID code to use new style of getkeycode and setkeycode methods to allow retrieving and setting keycodes not only by their scancodes but also by index. Acked-by: Jiri Kosina <jkosina@suse.cz> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 9f47009 commit a0bf0ea

1 file changed

Lines changed: 70 additions & 33 deletions

File tree

‎drivers/hid/hid-input.c‎

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,79 +68,116 @@ static const struct {
6868
#define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
6969
&max, EV_KEY, (c))
7070

71-
static inline int match_scancode(unsigned int code, unsigned int scancode)
71+
static bool match_scancode(struct hid_usage *usage,
72+
unsigned int cur_idx, unsigned int scancode)
7273
{
73-
if (scancode == 0)
74-
return 1;
75-
76-
return (code & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
74+
return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
7775
}
7876

79-
static inline int match_keycode(unsigned int code, unsigned int keycode)
77+
static bool match_keycode(struct hid_usage *usage,
78+
unsigned int cur_idx, unsigned int keycode)
8079
{
81-
if (keycode == 0)
82-
return 1;
80+
return usage->code == keycode;
81+
}
8382

84-
return code == keycode;
83+
static bool match_index(struct hid_usage *usage,
84+
unsigned int cur_idx, unsigned int idx)
85+
{
86+
return cur_idx == idx;
8587
}
8688

89+
typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
90+
unsigned int cur_idx, unsigned int val);
91+
8792
static struct hid_usage *hidinput_find_key(struct hid_device *hid,
88-
unsigned int scancode,
89-
unsigned int keycode)
93+
hid_usage_cmp_t match,
94+
unsigned int value,
95+
unsigned int *usage_idx)
9096
{
91-
int i, j, k;
97+
unsigned int i, j, k, cur_idx = 0;
9298
struct hid_report *report;
9399
struct hid_usage *usage;
94100

95101
for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
96102
list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
97103
for (i = 0; i < report->maxfield; i++) {
98-
for ( j = 0; j < report->field[i]->maxusage; j++) {
104+
for (j = 0; j < report->field[i]->maxusage; j++) {
99105
usage = report->field[i]->usage + j;
100-
if (usage->type == EV_KEY &&
101-
match_scancode(usage->hid, scancode) &&
102-
match_keycode(usage->code, keycode))
103-
return usage;
106+
if (usage->type == EV_KEY) {
107+
if (match(usage, cur_idx, value)) {
108+
if (usage_idx)
109+
*usage_idx = cur_idx;
110+
return usage;
111+
}
112+
cur_idx++;
113+
}
104114
}
105115
}
106116
}
107117
}
108118
return NULL;
109119
}
110120

121+
static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
122+
const struct input_keymap_entry *ke,
123+
unsigned int *index)
124+
{
125+
struct hid_usage *usage;
126+
unsigned int scancode;
127+
128+
if (ke->flags & INPUT_KEYMAP_BY_INDEX)
129+
usage = hidinput_find_key(hid, match_index, ke->index, index);
130+
else if (input_scancode_to_scalar(ke, &scancode) == 0)
131+
usage = hidinput_find_key(hid, match_scancode, scancode, index);
132+
else
133+
usage = NULL;
134+
135+
return usage;
136+
}
137+
111138
static int hidinput_getkeycode(struct input_dev *dev,
112-
unsigned int scancode, unsigned int *keycode)
139+
struct input_keymap_entry *ke)
113140
{
114141
struct hid_device *hid = input_get_drvdata(dev);
115142
struct hid_usage *usage;
143+
unsigned int scancode, index;
116144

117-
usage = hidinput_find_key(hid, scancode, 0);
145+
usage = hidinput_locate_usage(hid, ke, &index);
118146
if (usage) {
119-
*keycode = usage->code;
147+
ke->keycode = usage->code;
148+
ke->index = index;
149+
scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
150+
ke->len = sizeof(scancode);
151+
memcpy(ke->scancode, &scancode, sizeof(scancode));
120152
return 0;
121153
}
154+
122155
return -EINVAL;
123156
}
124157

125158
static int hidinput_setkeycode(struct input_dev *dev,
126-
unsigned int scancode, unsigned int keycode)
159+
const struct input_keymap_entry *ke,
160+
unsigned int *old_keycode)
127161
{
128162
struct hid_device *hid = input_get_drvdata(dev);
129163
struct hid_usage *usage;
130-
int old_keycode;
131164

132-
usage = hidinput_find_key(hid, scancode, 0);
165+
usage = hidinput_locate_usage(hid, ke, NULL);
133166
if (usage) {
134-
old_keycode = usage->code;
135-
usage->code = keycode;
167+
*old_keycode = usage->code;
168+
usage->code = ke->keycode;
136169

137-
clear_bit(old_keycode, dev->keybit);
170+
clear_bit(*old_keycode, dev->keybit);
138171
set_bit(usage->code, dev->keybit);
139-
dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n", keycode, scancode);
140-
/* Set the keybit for the old keycode if the old keycode is used
141-
* by another key */
142-
if (hidinput_find_key (hid, 0, old_keycode))
143-
set_bit(old_keycode, dev->keybit);
172+
dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n",
173+
usage->code, usage->hid);
174+
175+
/*
176+
* Set the keybit for the old keycode if the old keycode is used
177+
* by another key
178+
*/
179+
if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
180+
set_bit(*old_keycode, dev->keybit);
144181

145182
return 0;
146183
}
@@ -748,8 +785,8 @@ int hidinput_connect(struct hid_device *hid, unsigned int force)
748785
hid->ll_driver->hidinput_input_event;
749786
input_dev->open = hidinput_open;
750787
input_dev->close = hidinput_close;
751-
input_dev->setkeycode = hidinput_setkeycode;
752-
input_dev->getkeycode = hidinput_getkeycode;
788+
input_dev->setkeycode_new = hidinput_setkeycode;
789+
input_dev->getkeycode_new = hidinput_getkeycode;
753790

754791
input_dev->name = hid->name;
755792
input_dev->phys = hid->phys;

0 commit comments

Comments
 (0)