Skip to content

Commit 74acc27

Browse files
bentissJiri Kosina
authored andcommitted
HID: core: de-duplicate some code in hid_input_field()
I had to go twice through the history to get a grasp at this code. De-duplicate the various tests in one common helper to make it more explicit. Note that the `HID_UP_KEYBOARD + 1` condition is tested through https://gitlab.freedesktop.org/libevdev/hid-tools/-/merge_requests/121 Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Reviewed-by: Ping Cheng <ping.cheng@wacom.com> Acked-by: Peter Hutterer <peter.hutterer@who-t.net> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 1c1813a commit 74acc27

1 file changed

Lines changed: 39 additions & 15 deletions

File tree

drivers/hid/hid-core.c

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,24 @@ static void hid_process_event(struct hid_device *hid, struct hid_field *field,
15251525
hid->hiddev_hid_event(hid, field, usage, value);
15261526
}
15271527

1528+
/*
1529+
* Checks if the given value is valid within this field
1530+
*/
1531+
static inline int hid_array_value_is_valid(struct hid_field *field,
1532+
__s32 value)
1533+
{
1534+
__s32 min = field->logical_minimum;
1535+
1536+
/*
1537+
* Value needs to be between logical min and max, and
1538+
* (value - min) is used as an index in the usage array.
1539+
* This array is of size field->maxusage
1540+
*/
1541+
return value >= min &&
1542+
value <= field->logical_maximum &&
1543+
value - min < field->maxusage;
1544+
}
1545+
15281546
/*
15291547
* Analyse a received field, and fetch the data from it. The field
15301548
* content is stored for next report processing (we do differential
@@ -1539,7 +1557,6 @@ static void hid_input_field(struct hid_device *hid, struct hid_field *field,
15391557
unsigned offset = field->report_offset;
15401558
unsigned size = field->report_size;
15411559
__s32 min = field->logical_minimum;
1542-
__s32 max = field->logical_maximum;
15431560
__s32 *value;
15441561

15451562
value = field->new_value;
@@ -1554,30 +1571,37 @@ static void hid_input_field(struct hid_device *hid, struct hid_field *field,
15541571

15551572
/* Ignore report if ErrorRollOver */
15561573
if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
1557-
value[n] >= min && value[n] <= max &&
1558-
value[n] - min < field->maxusage &&
1574+
hid_array_value_is_valid(field, value[n]) &&
15591575
field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
15601576
return;
15611577
}
15621578

15631579
for (n = 0; n < count; n++) {
15641580

15651581
if (HID_MAIN_ITEM_VARIABLE & field->flags) {
1566-
hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
1582+
hid_process_event(hid,
1583+
field,
1584+
&field->usage[n],
1585+
value[n],
1586+
interrupt);
15671587
continue;
15681588
}
15691589

1570-
if (field->value[n] >= min && field->value[n] <= max
1571-
&& field->value[n] - min < field->maxusage
1572-
&& field->usage[field->value[n] - min].hid
1573-
&& search(value, field->value[n], count))
1574-
hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
1575-
1576-
if (value[n] >= min && value[n] <= max
1577-
&& value[n] - min < field->maxusage
1578-
&& field->usage[value[n] - min].hid
1579-
&& search(field->value, value[n], count))
1580-
hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
1590+
if (hid_array_value_is_valid(field, field->value[n]) &&
1591+
search(value, field->value[n], count))
1592+
hid_process_event(hid,
1593+
field,
1594+
&field->usage[field->value[n] - min],
1595+
0,
1596+
interrupt);
1597+
1598+
if (hid_array_value_is_valid(field, value[n]) &&
1599+
search(field->value, value[n], count))
1600+
hid_process_event(hid,
1601+
field,
1602+
&field->usage[value[n] - min],
1603+
1,
1604+
interrupt);
15811605
}
15821606

15831607
memcpy(field->value, value, count * sizeof(__s32));

0 commit comments

Comments
 (0)