Skip to content

Commit 1c1813a

Browse files
bentissJiri Kosina
authored andcommitted
HID: core: statically allocate read buffers
This is a preparation patch for rethinking the generic processing of HID reports. We can actually pre-allocate all of our memory instead of dynamically allocating/freeing it whenever we parse a report. 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 a254a9d commit 1c1813a

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

drivers/hid/hid-core.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ static struct hid_field *hid_register_field(struct hid_report *report, unsigned
101101

102102
field = kzalloc((sizeof(struct hid_field) +
103103
usages * sizeof(struct hid_usage) +
104-
usages * sizeof(unsigned)), GFP_KERNEL);
104+
2 * usages * sizeof(unsigned int)), GFP_KERNEL);
105105
if (!field)
106106
return NULL;
107107

108108
field->index = report->maxfield++;
109109
report->field[field->index] = field;
110110
field->usage = (struct hid_usage *)(field + 1);
111111
field->value = (s32 *)(field->usage + usages);
112+
field->new_value = (s32 *)(field->value + usages);
112113
field->report = report;
113114

114115
return field;
@@ -1541,9 +1542,8 @@ static void hid_input_field(struct hid_device *hid, struct hid_field *field,
15411542
__s32 max = field->logical_maximum;
15421543
__s32 *value;
15431544

1544-
value = kmalloc_array(count, sizeof(__s32), GFP_ATOMIC);
1545-
if (!value)
1546-
return;
1545+
value = field->new_value;
1546+
memset(value, 0, count * sizeof(__s32));
15471547

15481548
for (n = 0; n < count; n++) {
15491549

@@ -1557,7 +1557,7 @@ static void hid_input_field(struct hid_device *hid, struct hid_field *field,
15571557
value[n] >= min && value[n] <= max &&
15581558
value[n] - min < field->maxusage &&
15591559
field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
1560-
goto exit;
1560+
return;
15611561
}
15621562

15631563
for (n = 0; n < count; n++) {
@@ -1581,8 +1581,6 @@ static void hid_input_field(struct hid_device *hid, struct hid_field *field,
15811581
}
15821582

15831583
memcpy(field->value, value, count * sizeof(__s32));
1584-
exit:
1585-
kfree(value);
15861584
}
15871585

15881586
/*

include/linux/hid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ struct hid_field {
476476
unsigned report_count; /* number of this field in the report */
477477
unsigned report_type; /* (input,output,feature) */
478478
__s32 *value; /* last known value(s) */
479+
__s32 *new_value; /* newly read value(s) */
479480
__s32 logical_minimum;
480481
__s32 logical_maximum;
481482
__s32 physical_minimum;

0 commit comments

Comments
 (0)