thermal/core: Remove duplicate information when an error occurs
[platform/kernel/linux-rpi.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         struct hid_report *report = field->report;
78         int fn_key;
79         int ret;
80         u32 report_len;
81         u8 *report_data, *buf;
82
83         if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
84             (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
85                 return;
86
87         fn_key = (usage->hid & HID_USAGE);
88         if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
89                 return;
90         if (fn_key > drvdata->max_function_row_key)
91                 drvdata->max_function_row_key = fn_key;
92
93         report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
94         if (!report_data)
95                 return;
96
97         report_len = hid_report_len(report);
98         if (!report->id) {
99                 /*
100                  * hid_hw_raw_request() will stuff report ID (which will be 0)
101                  * into the first byte of the buffer even for unnumbered
102                  * reports, so we need to account for this to avoid getting
103                  * -EOVERFLOW in return.
104                  * Note that hid_alloc_report_buf() adds 7 bytes to the size
105                  * so we can safely say that we have space for an extra byte.
106                  */
107                 report_len++;
108         }
109
110         ret = hid_hw_raw_request(hdev, report->id, report_data,
111                                  report_len, HID_FEATURE_REPORT,
112                                  HID_REQ_GET_REPORT);
113         if (ret < 0) {
114                 dev_warn(&hdev->dev, "failed to fetch feature %d\n",
115                          field->report->id);
116                 goto out;
117         }
118
119         if (!report->id) {
120                 /*
121                  * Undo the damage from hid_hw_raw_request() for unnumbered
122                  * reports.
123                  */
124                 report_data++;
125                 report_len--;
126         }
127
128         ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, report_data,
129                                    report_len, 0);
130         if (ret) {
131                 dev_warn(&hdev->dev, "failed to report feature %d\n",
132                          field->report->id);
133                 goto out;
134         }
135
136         drvdata->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
137             field->value[usage->usage_index];
138
139 out:
140         kfree(buf);
141 }
142
143 static int vivaldi_input_configured(struct hid_device *hdev,
144                                     struct hid_input *hidinput)
145 {
146         return devm_device_add_group(&hdev->dev, &input_attribute_group);
147 }
148
149 static const struct hid_device_id vivaldi_table[] = {
150         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_VIVALDI, HID_ANY_ID,
151                      HID_ANY_ID) },
152         { }
153 };
154
155 MODULE_DEVICE_TABLE(hid, vivaldi_table);
156
157 static struct hid_driver hid_vivaldi = {
158         .name = "hid-vivaldi",
159         .id_table = vivaldi_table,
160         .probe = vivaldi_probe,
161         .feature_mapping = vivaldi_feature_mapping,
162         .input_configured = vivaldi_input_configured,
163 };
164
165 module_hid_driver(hid_vivaldi);
166
167 MODULE_AUTHOR("Sean O'Brien");
168 MODULE_DESCRIPTION("HID vivaldi driver");
169 MODULE_LICENSE("GPL");