Skip to content

Commit 6edb8cd

Browse files
kekrbyJiri Kosina
authored andcommitted
HID: core: add helper for finding a field with a certain usage
This helper will allow HID drivers to easily determine if they should bind to a hid_device by checking for the prescence of a certain field when its ID is not enough, which can be the case on USB devices with multiple interfaces and/or configurations. Convert google-hammer driver to use it, and remove now superfluous hammer_has_usage(). [[email protected]: expand changelog with the information about google-hammer being added as user of this API ] Signed-off-by: Kerem Karabay <[email protected]> Signed-off-by: Aditya Garg <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 6b5faec commit 6edb8cd

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

drivers/hid/hid-core.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,31 @@ int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
19121912
}
19131913
EXPORT_SYMBOL_GPL(hid_set_field);
19141914

1915+
struct hid_field *hid_find_field(struct hid_device *hdev, unsigned int report_type,
1916+
unsigned int application, unsigned int usage)
1917+
{
1918+
struct list_head *report_list = &hdev->report_enum[report_type].report_list;
1919+
struct hid_report *report;
1920+
int i, j;
1921+
1922+
list_for_each_entry(report, report_list, list) {
1923+
if (report->application != application)
1924+
continue;
1925+
1926+
for (i = 0; i < report->maxfield; i++) {
1927+
struct hid_field *field = report->field[i];
1928+
1929+
for (j = 0; j < field->maxusage; j++) {
1930+
if (field->usage[j].hid == usage)
1931+
return field;
1932+
}
1933+
}
1934+
}
1935+
1936+
return NULL;
1937+
}
1938+
EXPORT_SYMBOL_GPL(hid_find_field);
1939+
19151940
static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
19161941
const u8 *data)
19171942
{

drivers/hid/hid-google-hammer.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -418,38 +418,15 @@ static int hammer_event(struct hid_device *hid, struct hid_field *field,
418418
return 0;
419419
}
420420

421-
static bool hammer_has_usage(struct hid_device *hdev, unsigned int report_type,
422-
unsigned application, unsigned usage)
423-
{
424-
struct hid_report_enum *re = &hdev->report_enum[report_type];
425-
struct hid_report *report;
426-
int i, j;
427-
428-
list_for_each_entry(report, &re->report_list, list) {
429-
if (report->application != application)
430-
continue;
431-
432-
for (i = 0; i < report->maxfield; i++) {
433-
struct hid_field *field = report->field[i];
434-
435-
for (j = 0; j < field->maxusage; j++)
436-
if (field->usage[j].hid == usage)
437-
return true;
438-
}
439-
}
440-
441-
return false;
442-
}
443-
444421
static bool hammer_has_folded_event(struct hid_device *hdev)
445422
{
446-
return hammer_has_usage(hdev, HID_INPUT_REPORT,
423+
return !!hid_find_field(hdev, HID_INPUT_REPORT,
447424
HID_GD_KEYBOARD, HID_USAGE_KBD_FOLDED);
448425
}
449426

450427
static bool hammer_has_backlight_control(struct hid_device *hdev)
451428
{
452-
return hammer_has_usage(hdev, HID_OUTPUT_REPORT,
429+
return !!hid_find_field(hdev, HID_OUTPUT_REPORT,
453430
HID_GD_KEYBOARD, HID_AD_BRIGHTNESS);
454431
}
455432

include/linux/hid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ extern void hidinput_report_event(struct hid_device *hid, struct hid_report *rep
940940
extern int hidinput_connect(struct hid_device *hid, unsigned int force);
941941
extern void hidinput_disconnect(struct hid_device *);
942942

943+
struct hid_field *hid_find_field(struct hid_device *hdev, unsigned int report_type,
944+
unsigned int application, unsigned int usage);
943945
int hid_set_field(struct hid_field *, unsigned, __s32);
944946
int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
945947
int interrupt);

0 commit comments

Comments
 (0)