Merge 5.16-rc8 into usb-next
[linux-2.6-microblaze.git] / drivers / hid / hid-vivaldi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * HID support for Vivaldi Keyboard
4  *
5  * Copyright 2020 Google LLC.
6  * Author: Sean O'Brien <seobrien@chromium.org>
7  */
8
9 #include <linux/hid.h>
10 #include <linux/module.h>
11
12 #define MIN_FN_ROW_KEY  1
13 #define MAX_FN_ROW_KEY  24
14 #define HID_VD_FN_ROW_PHYSMAP 0x00000001
15 #define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
16
17 static struct hid_driver hid_vivaldi;
18
19 struct vivaldi_data {
20         u32 function_row_physmap[MAX_FN_ROW_KEY - MIN_FN_ROW_KEY + 1];
21         int max_function_row_key;
22 };
23
24 static ssize_t function_row_physmap_show(struct device *dev,
25                                          struct device_attribute *attr,
26                                          char *buf)
27 {
28         struct hid_device *hdev = to_hid_device(dev);
29         struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
30         ssize_t size = 0;
31         int i;
32
33         if (!drvdata->max_function_row_key)
34                 return 0;
35
36         for (i = 0; i < drvdata->max_function_row_key; i++)
37                 size += sprintf(buf + size, "%02X ",
38                                 drvdata->function_row_physmap[i]);
39         size += sprintf(buf + size, "\n");
40         return size;
41 }
42
43 DEVICE_ATTR_RO(function_row_physmap);
44 static struct attribute *sysfs_attrs[] = {
45         &dev_attr_function_row_physmap.attr,
46         NULL
47 };
48
49 static const struct attribute_group input_attribute_group = {
50         .attrs = sysfs_attrs
51 };
52
53 static int vivaldi_probe(struct hid_device *hdev,
54                          const struct hid_device_id *id)
55 {
56         struct vivaldi_data *drvdata;
57         int ret;
58
59         drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
60         if (!drvdata)
61                 return -ENOMEM;
62
63         hid_set_drvdata(hdev, drvdata);
64
65         ret = hid_parse(hdev);
66         if (ret)
67                 return ret;
68
69         return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
70 }
71
72 static void vivaldi_feature_mapping(struct hid_device *hdev,
73                                     struct hid_field *field,
74                                     struct hid_usage *usage)
75 {
76         struct vivaldi_data *drvdata = hid_get_drvdata(hdev);
77         int fn_key;
78         int ret;
79         u32 report_len;
80         u8 *buf;
81
82         if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
83             (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
84                 return;
85
86         fn_key = (usage->hid & HID_USAGE);
87         if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
88                 return;
89         if (fn_key > drvdata->max_function_row_key)
90                 drvdata->max_function_row_key = fn_key;
91
92         buf = hid_alloc_report_buf(field->report, GFP_KERNEL);
93         if (!buf)
94                 return;
95
96         report_len = hid_report_len(field->report);
97         ret = hid_hw_raw_request(hdev, field->report->id, buf,
98                                  report_len, HID_FEATURE_REPORT,
99                                  HID_REQ_GET_REPORT);
100         if (ret < 0) {
101                 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
102                          field->report->id);
103                 goto out;
104         }
105
106         ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
107                                    report_len, 0);
108         if (ret) {
109                 dev_warn(&hdev->dev, "failed to report feature %d\n",
110                          field->report->id);
111                 goto out;
112         }
113
114         drvdata->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
115             field->value[usage->usage_index];
116
117 out:
118         kfree(buf);
119 }
120
121 static int vivaldi_input_configured(struct hid_device *hdev,
122                                     struct hid_input *hidinput)
123 {
124         return sysfs_create_group(&hdev->dev.kobj, &input_attribute_group);
125 }
126
127 static const struct hid_device_id vivaldi_table[] = {
128         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_VIVALDI, HID_ANY_ID,
129                      HID_ANY_ID) },
130         { }
131 };
132
133 MODULE_DEVICE_TABLE(hid, vivaldi_table);
134
135 static struct hid_driver hid_vivaldi = {
136         .name = "hid-vivaldi",
137         .id_table = vivaldi_table,
138         .probe = vivaldi_probe,
139         .feature_mapping = vivaldi_feature_mapping,
140         .input_configured = vivaldi_input_configured,
141 };
142
143 module_hid_driver(hid_vivaldi);
144
145 MODULE_AUTHOR("Sean O'Brien");
146 MODULE_DESCRIPTION("HID vivaldi driver");
147 MODULE_LICENSE("GPL");