Skip to content

Commit b974b37

Browse files
LawstorantJiri Kosina
authored andcommitted
HID: pidff: Simplify HID field/usage searching logic
Some deduplication and splitting into separate functions. This is now way easier to comprehend and parse mentally. Signed-off-by: Tomasz Pakuła <tomasz.pakula.oficjalny@gmail.com> Signed-off-by: Jiri Kosina <jkosina@suse.com>
1 parent 7f3d7bc commit b974b37

1 file changed

Lines changed: 62 additions & 43 deletions

File tree

drivers/hid/usbhid/hid-pidff.c

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -939,53 +939,82 @@ static void pidff_set_autocenter(struct input_dev *dev, u16 magnitude)
939939
pidff_autocenter(dev->ff->private, magnitude);
940940
}
941941

942+
/*
943+
* Find specific usage in a given hid_field
944+
*/
945+
static int pidff_find_usage(struct hid_field *fld, unsigned int usage_code)
946+
{
947+
for (int i = 0; i < fld->maxusage; i++) {
948+
if (fld->usage[i].hid == usage_code)
949+
return i;
950+
}
951+
return -1;
952+
}
953+
954+
/*
955+
* Find hid_field with a specific usage. Return the usage index as well
956+
*/
957+
static int pidff_find_field_with_usage(int *usage_index,
958+
struct hid_report *report,
959+
unsigned int usage_code)
960+
{
961+
for (int i = 0; i < report->maxfield; i++) {
962+
struct hid_field *fld = report->field[i];
963+
964+
if (fld->maxusage != fld->report_count) {
965+
pr_debug("maxusage and report_count do not match, skipping\n");
966+
continue;
967+
}
968+
969+
int index = pidff_find_usage(fld, usage_code);
970+
971+
if (index >= 0) {
972+
*usage_index = index;
973+
return i;
974+
}
975+
}
976+
return -1;
977+
}
978+
942979
/*
943980
* Find fields from a report and fill a pidff_usage
944981
*/
945982
static int pidff_find_fields(struct pidff_usage *usage, const u8 *table,
946983
struct hid_report *report, int count, int strict,
947984
u32 *quirks)
948985
{
986+
const u8 block_offset = pidff_set_condition[PID_PARAM_BLOCK_OFFSET];
987+
const u8 delay = pidff_set_effect[PID_START_DELAY];
988+
949989
if (!report) {
950990
pr_debug("%s, null report\n", __func__);
951991
return -1;
952992
}
953993

954-
int i, j, k, found;
994+
for (int i = 0; i < count; i++) {
995+
int index;
996+
int found = pidff_find_field_with_usage(&index, report,
997+
HID_UP_PID | table[i]);
955998

956-
for (k = 0; k < count; k++) {
957-
found = 0;
958-
for (i = 0; i < report->maxfield; i++) {
959-
if (report->field[i]->maxusage !=
960-
report->field[i]->report_count) {
961-
pr_debug("maxusage and report_count do not match, skipping\n");
962-
continue;
963-
}
964-
for (j = 0; j < report->field[i]->maxusage; j++) {
965-
if (report->field[i]->usage[j].hid ==
966-
(HID_UP_PID | table[k])) {
967-
pr_debug("found %d at %d->%d\n",
968-
k, i, j);
969-
usage[k].field = report->field[i];
970-
usage[k].value =
971-
&report->field[i]->value[j];
972-
found = 1;
973-
break;
974-
}
975-
}
976-
if (found)
977-
break;
999+
if (found >= 0) {
1000+
pr_debug("found %d at %d->%d\n", i, found, index);
1001+
usage[i].field = report->field[found];
1002+
usage[i].value = &report->field[found]->value[index];
1003+
continue;
9781004
}
979-
if (!found && table[k] == pidff_set_effect[PID_START_DELAY]) {
1005+
1006+
if (table[i] == delay) {
9801007
pr_debug("Delay field not found, but that's OK\n");
9811008
pr_debug("Setting MISSING_DELAY quirk\n");
9821009
*quirks |= HID_PIDFF_QUIRK_MISSING_DELAY;
983-
} else if (!found && table[k] == pidff_set_condition[PID_PARAM_BLOCK_OFFSET]) {
1010+
1011+
} else if (table[i] == block_offset) {
9841012
pr_debug("PBO field not found, but that's OK\n");
9851013
pr_debug("Setting MISSING_PBO quirk\n");
9861014
*quirks |= HID_PIDFF_QUIRK_MISSING_PBO;
987-
} else if (!found && strict) {
988-
pr_debug("failed to locate %d\n", k);
1015+
1016+
} else if (strict) {
1017+
pr_debug("failed to locate %d\n", i);
9891018
return -1;
9901019
}
9911020
}
@@ -1054,9 +1083,7 @@ static void pidff_find_reports(struct hid_device *hid, int report_type,
10541083
*/
10551084
static int pidff_reports_ok(struct pidff_device *pidff)
10561085
{
1057-
int i;
1058-
1059-
for (i = 0; i < PID_REQUIRED_REPORTS; i++) {
1086+
for (int i = 0; i < PID_REQUIRED_REPORTS; i++) {
10601087
if (!pidff->reports[i]) {
10611088
hid_dbg(pidff->hid, "%d missing\n", i);
10621089
return 0;
@@ -1077,9 +1104,7 @@ static struct hid_field *pidff_find_special_field(struct hid_report *report,
10771104
return NULL;
10781105
}
10791106

1080-
int i;
1081-
1082-
for (i = 0; i < report->maxfield; i++) {
1107+
for (int i = 0; i < report->maxfield; i++) {
10831108
if (report->field[i]->logical == (HID_UP_PID | usage) &&
10841109
report->field[i]->report_count > 0) {
10851110
if (!enforce_min ||
@@ -1099,18 +1124,12 @@ static struct hid_field *pidff_find_special_field(struct hid_report *report,
10991124
static int pidff_find_special_keys(int *keys, struct hid_field *fld,
11001125
const u8 *usagetable, int count)
11011126
{
1102-
1103-
int i, j;
11041127
int found = 0;
11051128

1106-
for (i = 0; i < count; i++) {
1107-
for (j = 0; j < fld->maxusage; j++) {
1108-
if (fld->usage[j].hid == (HID_UP_PID | usagetable[i])) {
1109-
keys[i] = j + 1;
1110-
found++;
1111-
break;
1112-
}
1113-
}
1129+
for (int i = 0; i < count; i++) {
1130+
keys[i] = pidff_find_usage(fld, HID_UP_PID | usagetable[i]) + 1;
1131+
if (keys[i])
1132+
found++;
11141133
}
11151134
return found;
11161135
}

0 commit comments

Comments
 (0)