1 // SPDX-License-Identifier: GPL-2.0+
3 * Surface System Aggregator Module (SSAM) HID transport driver for the
4 * generic HID interface (HID/TC=0x15 subsystem). Provides support for
5 * integrated HID devices on Surface Laptop 3, Book 3, and later.
11 #include <asm/unaligned.h>
12 #include <linux/hid.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
17 #include <linux/surface_aggregator/controller.h>
18 #include <linux/surface_aggregator/device.h>
20 #include "surface_hid_core.h"
23 /* -- SAM interface. -------------------------------------------------------- */
25 struct surface_hid_buffer_slice {
33 static_assert(sizeof(struct surface_hid_buffer_slice) == 10);
35 enum surface_hid_cid {
36 SURFACE_HID_CID_OUTPUT_REPORT = 0x01,
37 SURFACE_HID_CID_GET_FEATURE_REPORT = 0x02,
38 SURFACE_HID_CID_SET_FEATURE_REPORT = 0x03,
39 SURFACE_HID_CID_GET_DESCRIPTOR = 0x04,
42 static int ssam_hid_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
44 u8 buffer[sizeof(struct surface_hid_buffer_slice) + 0x76];
45 struct surface_hid_buffer_slice *slice;
46 struct ssam_request rqst;
47 struct ssam_response rsp;
48 u32 buffer_len, offset, length;
52 * Note: The 0x76 above has been chosen because that's what's used by
53 * the Windows driver. Together with the header, this leads to a 128
54 * byte payload in total.
57 buffer_len = ARRAY_SIZE(buffer) - sizeof(struct surface_hid_buffer_slice);
59 rqst.target_category = shid->uid.category;
60 rqst.target_id = shid->uid.target;
61 rqst.command_id = SURFACE_HID_CID_GET_DESCRIPTOR;
62 rqst.instance_id = shid->uid.instance;
63 rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
64 rqst.length = sizeof(struct surface_hid_buffer_slice);
65 rqst.payload = buffer;
67 rsp.capacity = ARRAY_SIZE(buffer);
70 slice = (struct surface_hid_buffer_slice *)buffer;
77 while (!slice->end && offset < len) {
78 put_unaligned_le32(offset, &slice->offset);
79 put_unaligned_le32(length, &slice->length);
83 status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp,
88 offset = get_unaligned_le32(&slice->offset);
89 length = get_unaligned_le32(&slice->length);
91 /* Don't mess stuff up in case we receive garbage. */
92 if (length > buffer_len || offset > len)
95 if (offset + length > len)
96 length = len - offset;
98 memcpy(buf + offset, &slice->data[0], length);
105 dev_err(shid->dev, "unexpected descriptor length: got %u, expected %zu\n",
113 static int ssam_hid_set_raw_report(struct surface_hid_device *shid, u8 rprt_id, bool feature,
116 struct ssam_request rqst;
120 cid = SURFACE_HID_CID_SET_FEATURE_REPORT;
122 cid = SURFACE_HID_CID_OUTPUT_REPORT;
124 rqst.target_category = shid->uid.category;
125 rqst.target_id = shid->uid.target;
126 rqst.instance_id = shid->uid.instance;
127 rqst.command_id = cid;
134 return ssam_retry(ssam_request_sync, shid->ctrl, &rqst, NULL);
137 static int ssam_hid_get_raw_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
139 struct ssam_request rqst;
140 struct ssam_response rsp;
142 rqst.target_category = shid->uid.category;
143 rqst.target_id = shid->uid.target;
144 rqst.instance_id = shid->uid.instance;
145 rqst.command_id = SURFACE_HID_CID_GET_FEATURE_REPORT;
146 rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
147 rqst.length = sizeof(rprt_id);
148 rqst.payload = &rprt_id;
154 return ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(rprt_id));
157 static u32 ssam_hid_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
159 struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
161 if (event->command_id != 0x00)
164 hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
165 return SSAM_NOTIF_HANDLED;
169 /* -- Transport driver. ----------------------------------------------------- */
171 static int shid_output_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
175 status = ssam_hid_set_raw_report(shid, rprt_id, false, buf, len);
176 return status >= 0 ? len : status;
179 static int shid_get_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
183 status = ssam_hid_get_raw_report(shid, rprt_id, buf, len);
184 return status >= 0 ? len : status;
187 static int shid_set_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
191 status = ssam_hid_set_raw_report(shid, rprt_id, true, buf, len);
192 return status >= 0 ? len : status;
196 /* -- Driver setup. --------------------------------------------------------- */
198 static int surface_hid_probe(struct ssam_device *sdev)
200 struct surface_hid_device *shid;
202 shid = devm_kzalloc(&sdev->dev, sizeof(*shid), GFP_KERNEL);
206 shid->dev = &sdev->dev;
207 shid->ctrl = sdev->ctrl;
208 shid->uid = sdev->uid;
210 shid->notif.base.priority = 1;
211 shid->notif.base.fn = ssam_hid_event_fn;
212 shid->notif.event.reg = SSAM_EVENT_REGISTRY_REG(sdev->uid.target);
213 shid->notif.event.id.target_category = sdev->uid.category;
214 shid->notif.event.id.instance = sdev->uid.instance;
215 shid->notif.event.mask = SSAM_EVENT_MASK_STRICT;
216 shid->notif.event.flags = 0;
218 shid->ops.get_descriptor = ssam_hid_get_descriptor;
219 shid->ops.output_report = shid_output_report;
220 shid->ops.get_feature_report = shid_get_feature_report;
221 shid->ops.set_feature_report = shid_set_feature_report;
223 ssam_device_set_drvdata(sdev, shid);
224 return surface_hid_device_add(shid);
227 static void surface_hid_remove(struct ssam_device *sdev)
229 surface_hid_device_destroy(ssam_device_get_drvdata(sdev));
232 static const struct ssam_device_id surface_hid_match[] = {
233 { SSAM_SDEV(HID, SSAM_ANY_TID, SSAM_ANY_IID, 0x00) },
236 MODULE_DEVICE_TABLE(ssam, surface_hid_match);
238 static struct ssam_device_driver surface_hid_driver = {
239 .probe = surface_hid_probe,
240 .remove = surface_hid_remove,
241 .match_table = surface_hid_match,
243 .name = "surface_hid",
244 .pm = &surface_hid_pm_ops,
245 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
248 module_ssam_device_driver(surface_hid_driver);
252 MODULE_DESCRIPTION("HID transport driver for Surface System Aggregator Module");
253 MODULE_LICENSE("GPL");