1 // SPDX-License-Identifier: GPL-2.0
3 * Helpers for ChromeOS HID Vivaldi keyboards
5 * Copyright (C) 2022 Google, Inc
8 #include <linux/export.h>
10 #include <linux/input/vivaldi-fmap.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
15 #include "hid-vivaldi-common.h"
17 #define MIN_FN_ROW_KEY 1
18 #define MAX_FN_ROW_KEY VIVALDI_MAX_FUNCTION_ROW_KEYS
19 #define HID_VD_FN_ROW_PHYSMAP 0x00000001
20 #define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
23 * vivaldi_feature_mapping - Fill out vivaldi keymap data exposed via HID
24 * @hdev: HID device to parse
25 * @field: HID field to parse
26 * @usage: HID usage to parse
28 * Note: this function assumes that driver data attached to @hdev contains an
29 * instance of &struct vivaldi_data at the very beginning.
31 void vivaldi_feature_mapping(struct hid_device *hdev,
32 struct hid_field *field, struct hid_usage *usage)
34 struct vivaldi_data *data = hid_get_drvdata(hdev);
35 struct hid_report *report = field->report;
36 u8 *report_data, *buf;
41 if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
42 (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
45 fn_key = usage->hid & HID_USAGE;
46 if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
49 if (fn_key > data->num_function_row_keys)
50 data->num_function_row_keys = fn_key;
52 report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
56 report_len = hid_report_len(report);
59 * hid_hw_raw_request() will stuff report ID (which will be 0)
60 * into the first byte of the buffer even for unnumbered
61 * reports, so we need to account for this to avoid getting
62 * -EOVERFLOW in return.
63 * Note that hid_alloc_report_buf() adds 7 bytes to the size
64 * so we can safely say that we have space for an extra byte.
69 ret = hid_hw_raw_request(hdev, report->id, report_data,
70 report_len, HID_FEATURE_REPORT,
73 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
80 * Undo the damage from hid_hw_raw_request() for unnumbered
87 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, report_data,
90 dev_warn(&hdev->dev, "failed to report feature %d\n",
95 data->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
96 field->value[usage->usage_index];
101 EXPORT_SYMBOL_GPL(vivaldi_feature_mapping);
103 static ssize_t function_row_physmap_show(struct device *dev,
104 struct device_attribute *attr,
107 struct hid_device *hdev = to_hid_device(dev);
108 struct vivaldi_data *data = hid_get_drvdata(hdev);
110 return vivaldi_function_row_physmap_show(data, buf);
113 static DEVICE_ATTR_RO(function_row_physmap);
114 static struct attribute *vivaldi_sysfs_attrs[] = {
115 &dev_attr_function_row_physmap.attr,
119 static umode_t vivaldi_is_visible(struct kobject *kobj, struct attribute *attr,
122 struct hid_device *hdev = to_hid_device(kobj_to_dev(kobj));
123 struct vivaldi_data *data = hid_get_drvdata(hdev);
125 if (!data->num_function_row_keys)
130 static const struct attribute_group vivaldi_attribute_group = {
131 .attrs = vivaldi_sysfs_attrs,
132 .is_visible = vivaldi_is_visible,
135 const struct attribute_group *vivaldi_attribute_groups[] = {
136 &vivaldi_attribute_group,
139 EXPORT_SYMBOL_GPL(vivaldi_attribute_groups);
141 MODULE_LICENSE("GPL");