1 // SPDX-License-Identifier: GPL-2.0+
3 * Common/core components for the Surface System Aggregator Module (SSAM) HID
4 * transport driver. Provides support for integrated HID devices on Microsoft
7 * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
10 #include <asm/unaligned.h>
11 #include <linux/hid.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/usb/ch9.h>
17 #include <linux/surface_aggregator/controller.h>
19 #include "surface_hid_core.h"
22 /* -- Utility functions. ---------------------------------------------------- */
24 static bool surface_hid_is_hot_removed(struct surface_hid_device *shid)
27 * Non-ssam client devices, i.e. platform client devices, cannot be
30 if (!is_ssam_device(shid->dev))
33 return ssam_device_is_hot_removed(to_ssam_device(shid->dev));
37 /* -- Device descriptor access. --------------------------------------------- */
39 static int surface_hid_load_hid_descriptor(struct surface_hid_device *shid)
43 if (surface_hid_is_hot_removed(shid))
46 status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_HID,
47 (u8 *)&shid->hid_desc, sizeof(shid->hid_desc));
51 if (shid->hid_desc.desc_len != sizeof(shid->hid_desc)) {
52 dev_err(shid->dev, "unexpected HID descriptor length: got %u, expected %zu\n",
53 shid->hid_desc.desc_len, sizeof(shid->hid_desc));
57 if (shid->hid_desc.desc_type != HID_DT_HID) {
58 dev_err(shid->dev, "unexpected HID descriptor type: got %#04x, expected %#04x\n",
59 shid->hid_desc.desc_type, HID_DT_HID);
63 if (shid->hid_desc.num_descriptors != 1) {
64 dev_err(shid->dev, "unexpected number of descriptors: got %u, expected 1\n",
65 shid->hid_desc.num_descriptors);
69 if (shid->hid_desc.report_desc_type != HID_DT_REPORT) {
70 dev_err(shid->dev, "unexpected report descriptor type: got %#04x, expected %#04x\n",
71 shid->hid_desc.report_desc_type, HID_DT_REPORT);
78 static int surface_hid_load_device_attributes(struct surface_hid_device *shid)
82 if (surface_hid_is_hot_removed(shid))
85 status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_ATTRS,
86 (u8 *)&shid->attrs, sizeof(shid->attrs));
90 if (get_unaligned_le32(&shid->attrs.length) != sizeof(shid->attrs)) {
91 dev_err(shid->dev, "unexpected attribute length: got %u, expected %zu\n",
92 get_unaligned_le32(&shid->attrs.length), sizeof(shid->attrs));
100 /* -- Transport driver (common). -------------------------------------------- */
102 static int surface_hid_start(struct hid_device *hid)
104 struct surface_hid_device *shid = hid->driver_data;
106 return ssam_notifier_register(shid->ctrl, &shid->notif);
109 static void surface_hid_stop(struct hid_device *hid)
111 struct surface_hid_device *shid = hid->driver_data;
115 * Communication may fail for devices that have been hot-removed. This
116 * also includes unregistration of HID events, so we need to check this
117 * here. Only if the device has not been marked as hot-removed, we can
118 * safely disable events.
120 hot_removed = surface_hid_is_hot_removed(shid);
122 /* Note: This call will log errors for us, so ignore them here. */
123 __ssam_notifier_unregister(shid->ctrl, &shid->notif, !hot_removed);
126 static int surface_hid_open(struct hid_device *hid)
131 static void surface_hid_close(struct hid_device *hid)
135 static int surface_hid_parse(struct hid_device *hid)
137 struct surface_hid_device *shid = hid->driver_data;
138 size_t len = get_unaligned_le16(&shid->hid_desc.report_desc_len);
142 if (surface_hid_is_hot_removed(shid))
145 buf = kzalloc(len, GFP_KERNEL);
149 status = shid->ops.get_descriptor(shid, SURFACE_HID_DESC_REPORT, buf, len);
151 status = hid_parse_report(hid, buf, len);
157 static int surface_hid_raw_request(struct hid_device *hid, unsigned char reportnum, u8 *buf,
158 size_t len, unsigned char rtype, int reqtype)
160 struct surface_hid_device *shid = hid->driver_data;
162 if (surface_hid_is_hot_removed(shid))
165 if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
166 return shid->ops.output_report(shid, reportnum, buf, len);
168 else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
169 return shid->ops.get_feature_report(shid, reportnum, buf, len);
171 else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
172 return shid->ops.set_feature_report(shid, reportnum, buf, len);
177 static const struct hid_ll_driver surface_hid_ll_driver = {
178 .start = surface_hid_start,
179 .stop = surface_hid_stop,
180 .open = surface_hid_open,
181 .close = surface_hid_close,
182 .parse = surface_hid_parse,
183 .raw_request = surface_hid_raw_request,
187 /* -- Common device setup. -------------------------------------------------- */
189 int surface_hid_device_add(struct surface_hid_device *shid)
193 status = surface_hid_load_hid_descriptor(shid);
197 status = surface_hid_load_device_attributes(shid);
201 shid->hid = hid_allocate_device();
202 if (IS_ERR(shid->hid))
203 return PTR_ERR(shid->hid);
205 shid->hid->dev.parent = shid->dev;
206 shid->hid->bus = BUS_HOST;
207 shid->hid->vendor = get_unaligned_le16(&shid->attrs.vendor);
208 shid->hid->product = get_unaligned_le16(&shid->attrs.product);
209 shid->hid->version = get_unaligned_le16(&shid->hid_desc.hid_version);
210 shid->hid->country = shid->hid_desc.country_code;
212 snprintf(shid->hid->name, sizeof(shid->hid->name), "Microsoft Surface %04X:%04X",
213 shid->hid->vendor, shid->hid->product);
215 strscpy(shid->hid->phys, dev_name(shid->dev), sizeof(shid->hid->phys));
217 shid->hid->driver_data = shid;
218 shid->hid->ll_driver = &surface_hid_ll_driver;
220 status = hid_add_device(shid->hid);
222 hid_destroy_device(shid->hid);
226 EXPORT_SYMBOL_GPL(surface_hid_device_add);
228 void surface_hid_device_destroy(struct surface_hid_device *shid)
230 hid_destroy_device(shid->hid);
232 EXPORT_SYMBOL_GPL(surface_hid_device_destroy);
235 /* -- PM ops. --------------------------------------------------------------- */
237 #ifdef CONFIG_PM_SLEEP
239 static int surface_hid_suspend(struct device *dev)
241 struct surface_hid_device *d = dev_get_drvdata(dev);
243 return hid_driver_suspend(d->hid, PMSG_SUSPEND);
246 static int surface_hid_resume(struct device *dev)
248 struct surface_hid_device *d = dev_get_drvdata(dev);
250 return hid_driver_resume(d->hid);
253 static int surface_hid_freeze(struct device *dev)
255 struct surface_hid_device *d = dev_get_drvdata(dev);
257 return hid_driver_suspend(d->hid, PMSG_FREEZE);
260 static int surface_hid_poweroff(struct device *dev)
262 struct surface_hid_device *d = dev_get_drvdata(dev);
264 return hid_driver_suspend(d->hid, PMSG_HIBERNATE);
267 static int surface_hid_restore(struct device *dev)
269 struct surface_hid_device *d = dev_get_drvdata(dev);
271 return hid_driver_reset_resume(d->hid);
274 const struct dev_pm_ops surface_hid_pm_ops = {
275 .freeze = surface_hid_freeze,
276 .thaw = surface_hid_resume,
277 .suspend = surface_hid_suspend,
278 .resume = surface_hid_resume,
279 .poweroff = surface_hid_poweroff,
280 .restore = surface_hid_restore,
282 EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
284 #else /* CONFIG_PM_SLEEP */
286 const struct dev_pm_ops surface_hid_pm_ops = { };
287 EXPORT_SYMBOL_GPL(surface_hid_pm_ops);
289 #endif /* CONFIG_PM_SLEEP */
291 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
292 MODULE_DESCRIPTION("HID transport driver core for Surface System Aggregator Module");
293 MODULE_LICENSE("GPL");