1 // SPDX-License-Identifier: GPL-2.0
3 * HID support for Vivaldi Keyboard
5 * Copyright 2020 Google LLC.
6 * Author: Sean O'Brien <seobrien@chromium.org>
9 #include <linux/device.h>
10 #include <linux/hid.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sysfs.h>
15 #define MIN_FN_ROW_KEY 1
16 #define MAX_FN_ROW_KEY 24
17 #define HID_VD_FN_ROW_PHYSMAP 0x00000001
18 #define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
21 u32 function_row_physmap[MAX_FN_ROW_KEY - MIN_FN_ROW_KEY + 1];
22 int max_function_row_key;
25 static ssize_t function_row_physmap_show(struct device *dev,
26 struct device_attribute *attr,
29 struct hid_device *hdev = to_hid_device(dev);
30 struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
34 if (!drvdata->max_function_row_key)
37 for (i = 0; i < drvdata->max_function_row_key; i++)
38 size += sprintf(buf + size, "%02X ",
39 drvdata->function_row_physmap[i]);
40 size += sprintf(buf + size, "\n");
44 static DEVICE_ATTR_RO(function_row_physmap);
45 static struct attribute *sysfs_attrs[] = {
46 &dev_attr_function_row_physmap.attr,
50 static const struct attribute_group input_attribute_group = {
54 static int vivaldi_probe(struct hid_device *hdev,
55 const struct hid_device_id *id)
57 struct vivaldi_data *drvdata;
60 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
64 hid_set_drvdata(hdev, drvdata);
66 ret = hid_parse(hdev);
70 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
73 static void vivaldi_feature_mapping(struct hid_device *hdev,
74 struct hid_field *field,
75 struct hid_usage *usage)
77 struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
78 struct hid_report *report = field->report;
82 u8 *report_data, *buf;
84 if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
85 (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
88 fn_key = (usage->hid & HID_USAGE);
89 if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
91 if (fn_key > drvdata->max_function_row_key)
92 drvdata->max_function_row_key = fn_key;
94 report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
98 report_len = hid_report_len(report);
101 * hid_hw_raw_request() will stuff report ID (which will be 0)
102 * into the first byte of the buffer even for unnumbered
103 * reports, so we need to account for this to avoid getting
104 * -EOVERFLOW in return.
105 * Note that hid_alloc_report_buf() adds 7 bytes to the size
106 * so we can safely say that we have space for an extra byte.
111 ret = hid_hw_raw_request(hdev, report->id, report_data,
112 report_len, HID_FEATURE_REPORT,
115 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
122 * Undo the damage from hid_hw_raw_request() for unnumbered
129 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, report_data,
132 dev_warn(&hdev->dev, "failed to report feature %d\n",
137 drvdata->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
138 field->value[usage->usage_index];
144 static int vivaldi_input_configured(struct hid_device *hdev,
145 struct hid_input *hidinput)
147 return devm_device_add_group(&hdev->dev, &input_attribute_group);
150 static const struct hid_device_id vivaldi_table[] = {
151 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_VIVALDI, HID_ANY_ID,
156 MODULE_DEVICE_TABLE(hid, vivaldi_table);
158 static struct hid_driver hid_vivaldi = {
159 .name = "hid-vivaldi",
160 .id_table = vivaldi_table,
161 .probe = vivaldi_probe,
162 .feature_mapping = vivaldi_feature_mapping,
163 .input_configured = vivaldi_input_configured,
166 module_hid_driver(hid_vivaldi);
168 MODULE_AUTHOR("Sean O'Brien");
169 MODULE_DESCRIPTION("HID vivaldi driver");
170 MODULE_LICENSE("GPL");