1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Common/core components for the Surface System Aggregator Module (SSAM) HID
* transport driver. Provides support for integrated HID devices on Microsoft
* Surface models.
*
* Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
*/
#include <asm/unaligned.h>
#include <linux/hid.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/usb/ch9.h>
#include <linux/surface_aggregator/controller.h>
#include "surface_hid_core.h"
/* -- Device descriptor access. --------------------------------------------- */
static int surface_hid_load_hid_descriptor(struct surface_hid_device *shid)
{
int status;
status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_HID,
(u8 *)&shid->hid_desc, sizeof(shid->hid_desc));
if (status)
return status;
if (shid->hid_desc.desc_len != sizeof(shid->hid_desc)) {
dev_err(shid->dev, "unexpected HID descriptor length: got %u, expected %zu\n",
shid->hid_desc.desc_len, sizeof(shid->hid_desc));
return -EPROTO;
}
if (shid->hid_desc.desc_type != HID_DT_HID) {
dev_err(shid->dev, "unexpected HID descriptor type: got %#04x, expected %#04x\n",
shid->hid_desc.desc_type, HID_DT_HID);
return -EPROTO;
}
if (shid->hid_desc.num_descriptors != 1) {
dev_err(shid->dev, "unexpected number of descriptors: got %u, expected 1\n",
shid->hid_desc.num_descriptors);
return -EPROTO;
}
if (shid->hid_desc.report_desc_type != HID_DT_REPORT) {
dev_err(shid->dev, "unexpected report descriptor type: got %#04x, expected %#04x\n",
shid->hid_desc.report_desc_type, HID_DT_REPORT);
return -EPROTO;
}
return 0;
}
static int surface_hid_load_device_attributes(struct surface_hid_device *shid)
{
int status;
status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_ATTRS,
(u8 *)&shid->attrs, sizeof(shid->attrs));
if (status)
return status;
if (get_unaligned_le32(&shid->attrs.length) != sizeof(shid->attrs)) {
dev_err(shid->dev, "unexpected attribute length: got %u, expected %zu\n",
get_unaligned_le32(&shid->attrs.length), sizeof(shid->attrs));
return -EPROTO;
}
return 0;
}
/* -- Transport driver (common). -------------------------------------------- */
static int surface_hid_start(struct hid_device *hid)
{
struct surface_hid_device *shid = hid->driver_data;
return ssam_notifier_register(shid->ctrl, &shid->notif);
}
static void surface_hid_stop(struct hid_device *hid)
{
struct surface_hid_device *shid = hid->driver_data;
/* Note: This call will log errors for us, so ignore them here. */
ssam_notifier_unregister(shid->ctrl, &shid->notif);
}
static int surface_hid_open(struct hid_device *hid)
{
return 0;
}
static void surface_hid_close(struct hid_device *hid)
{
}
static int surface_hid_parse(struct hid_device *hid)
{
struct surface_hid_device *shid = hid->driver_data;
size_t len = get_unaligned_le16(&shid->hid_desc.report_desc_len);
u8 *buf;
int status;
buf = kzalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_REPORT, buf, len);
if (!status)
status = hid_parse_report(hid, buf, len);
kfree(buf);
return status;
}
static int surface_hid_raw_request(struct hid_device *hid, unsigned char reportnum, u8 *buf,
size_t len, unsigned char rtype, int reqtype)
{
struct surface_hid_device *shid = hid->driver_data;
if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
return shid->ops.output_report(shid, reportnum, buf, len);
else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
return shid->ops.get_feature_report(shid, reportnum, buf, len);
else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
return shid->ops.set_feature_report(shid, reportnum, buf, len);
return -EIO;
}
static struct hid_ll_driver surface_hid_ll_driver = {
.start = surface_hid_start,
.stop = surface_hid_stop,
.open = surface_hid_open,
.close = surface_hid_close,
.parse = surface_hid_parse,
.raw_request = surface_hid_raw_request,
};
/* -- Common device setup. -------------------------------------------------- */
int surface_hid_device_add(struct surface_hid_device *shid)
{
int status;
status = surface_hid_load_hid_descriptor(shid);
if (status)
return status;
status = surface_hid_load_device_attributes(shid);
if (status)
return status;
shid->hid = hid_allocate_device();
if (IS_ERR(shid->hid))
return PTR_ERR(shid->hid);
shid->hid->dev.parent = shid->dev;
shid->hid->bus = BUS_HOST;
shid->hid->vendor = cpu_to_le16(shid->attrs.vendor);
shid->hid->product = cpu_to_le16(shid->attrs.product);
shid->hid->version = cpu_to_le16(shid->hid_desc.hid_version);
shid->hid->country = shid->hid_desc.country_code;
snprintf(shid->hid->name, sizeof(shid->hid->name), "Microsoft Surface %04X:%04X",
shid->hid->vendor, shid->hid->product);
strscpy(shid->hid->phys, dev_name(shid->dev), sizeof(shid->hid->phys));
shid->hid->driver_data = shid;
shid->hid->ll_driver = &surface_hid_ll_driver;
status = hid_add_device(shid->hid);
if (status)
hid_destroy_device(shid->hid);
return status;
}
EXPORT_SYMBOL_GPL(surface_hid_device_add);
void surface_hid_device_destroy(struct surface_hid_device *shid)
{
hid_destroy_device(shid->hid);
}
EXPORT_SYMBOL_GPL(surface_hid_device_destroy);
/* -- PM ops. --------------------------------------------------------------- */
#ifdef CONFIG_PM_SLEEP
static int surface_hid_suspend(struct device *dev)
{
struct surface_hid_device *d = dev_get_drvdata(dev);
if (d->hid->driver && d->hid->driver->suspend)
return d->hid->driver->suspend(d->hid, PMSG_SUSPEND);
return 0;
}
static int surface_hid_resume(struct device *dev)
{
struct surface_hid_device *d = dev_get_drvdata(dev);
if (d->hid->driver && d->hid->driver->resume)
return d->hid->driver->resume(d->hid);
return 0;
}
static int surface_hid_freeze(struct device *dev)
{
struct surface_hid_device *d = dev_get_drvdata(dev);
if (d->hid->driver && d->hid->driver->suspend)
return d->hid->driver->suspend(d->hid, PMSG_FREEZE);
return 0;
}
static int surface_hid_poweroff(struct device *dev)
{
struct surface_hid_device *d = dev_get_drvdata(dev);
if (d->hid->driver && d->hid->driver->suspend)
return d->hid->driver->suspend(d->hid, PMSG_HIBERNATE);
return 0;
}
static int surface_hid_restore(struct device *dev)
{
struct surface_hid_device *d = dev_get_drvdata(dev);
if (d->hid->driver && d->hid->driver->reset_resume)
return d->hid->driver->reset_resume(d->hid);
return 0;
}
const struct dev_pm_ops surface_hid_pm_ops = {
.freeze = surface_hid_freeze,
.thaw = surface_hid_resume,
.suspend = surface_hid_suspend,
.resume = surface_hid_resume,
.poweroff = surface_hid_poweroff,
.restore = surface_hid_restore,
};
EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
#else /* CONFIG_PM_SLEEP */
const struct dev_pm_ops surface_hid_pm_ops = { };
EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
#endif /* CONFIG_PM_SLEEP */
MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
MODULE_DESCRIPTION("HID transport driver core for Surface System Aggregator Module");
MODULE_LICENSE("GPL");
|