[PATCH v3] HID: vivaldi: avoid redundant feature report reads per usage
From: Zhengping Jiang
Date: Tue Oct 13 2026 - 15:23:46 EST
vivaldi_feature_mapping() is invoked by hidinput_configure_usages() in
ascending usage_index order for each usage in the top-row physical keymap
field. Because fetching the feature report at usage_index == 0 already
reads the entire report from hardware and populates field->value[] for all
usages in the field, calling hid_hw_raw_request() again for subsequent
usages redundantly reads the same report N times (e.g., 12 sequential BLE
GATT reads taking ~2s on Vivaldi BLE keyboards).
Fetch the feature report from hardware once per HID_USAGE_FN_ROW_PHYSMAP
field at usage_index == 0 before validating individual usage entries, and
reuse field->value[usage->usage_index] for subsequent usages in the field.
Fixes: 33bbe04a15f2 ("HID: google: extract Vivaldi hid feature mapping for use in hid-hammer")
Signed-off-by: Zhengping Jiang <jiangzp@xxxxxxxxxx>
---
Changes in v3:
- Add comment explaining why the report is only read at usage_index == 0
and how failed reads rely on zero-initialized field->value[].
- Jump/fall through to parse_usage on read failure so usage_index == 0
updates num_function_row_keys consistently with subsequent usages and
preserves original behavior when only one usage is present.
Changes in v2:
- Move per-usage validation (HID_UP_ORDINAL and fn_key bounds) after the
usage_index == 0 hardware read so the report is still fetched even if
usage[0] is non-ordinal or out of bounds.
drivers/hid/hid-vivaldi-common.c | 34 +++++++++++++++++++++-----------
1 file changed, 22 insertions(+), 12 deletions(-)
diff --git a/drivers/hid/hid-vivaldi-common.c b/drivers/hid/hid-vivaldi-common.c
index c3c9fe9e697b..2d85555289d8 100644
--- a/drivers/hid/hid-vivaldi-common.c
+++ b/drivers/hid/hid-vivaldi-common.c
@@ -33,21 +33,22 @@ void vivaldi_feature_mapping(struct hid_device *hdev,
{
struct vivaldi_data *data = hid_get_drvdata(hdev);
struct hid_report *report = field->report;
- u8 *report_data, *buf;
+ u8 *report_data, *buf = NULL;
u32 report_len;
unsigned int fn_key;
int ret;
- if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
- (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
+ if (field->logical != HID_USAGE_FN_ROW_PHYSMAP)
return;
- fn_key = usage->hid & HID_USAGE;
- if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
- return;
-
- if (fn_key > data->num_function_row_keys)
- data->num_function_row_keys = fn_key;
+ /*
+ * Read the feature report only at usage_index == 0, as a single read
+ * populates field->value[] for all usages in the field. If the read
+ * fails, subsequent usages will not re-read the report and will rely
+ * on field->value[] being zero-initialized.
+ */
+ if (usage->usage_index)
+ goto parse_usage;
report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
if (!report_data)
@@ -72,7 +73,7 @@ void vivaldi_feature_mapping(struct hid_device *hdev,
if (ret < 0) {
dev_warn(&hdev->dev, "failed to fetch feature %d\n",
field->report->id);
- goto out;
+ goto parse_usage;
}
if (!report->id) {
@@ -86,11 +87,20 @@ void vivaldi_feature_mapping(struct hid_device *hdev,
ret = __hid_report_raw_event(hdev, HID_FEATURE_REPORT, report_data,
report_len, report_len, 0);
- if (ret) {
+ if (ret)
dev_warn(&hdev->dev, "failed to report feature %d\n",
field->report->id);
+
+parse_usage:
+ if ((usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
goto out;
- }
+
+ fn_key = usage->hid & HID_USAGE;
+ if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
+ goto out;
+
+ if (fn_key > data->num_function_row_keys)
+ data->num_function_row_keys = fn_key;
data->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
field->value[usage->usage_index];
--
2.56.0.385.gd3acb90ef8-goog