surface_hid.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  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.
  6. *
  7. * Copyright (C) 2019-2021 Blaž Hrastnik <[email protected]>,
  8. * Maximilian Luz <[email protected]>
  9. */
  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/surface_aggregator/controller.h>
  16. #include <linux/surface_aggregator/device.h>
  17. #include "surface_hid_core.h"
  18. /* -- SAM interface. -------------------------------------------------------- */
  19. struct surface_hid_buffer_slice {
  20. __u8 entry;
  21. __le32 offset;
  22. __le32 length;
  23. __u8 end;
  24. __u8 data[];
  25. } __packed;
  26. static_assert(sizeof(struct surface_hid_buffer_slice) == 10);
  27. enum surface_hid_cid {
  28. SURFACE_HID_CID_OUTPUT_REPORT = 0x01,
  29. SURFACE_HID_CID_GET_FEATURE_REPORT = 0x02,
  30. SURFACE_HID_CID_SET_FEATURE_REPORT = 0x03,
  31. SURFACE_HID_CID_GET_DESCRIPTOR = 0x04,
  32. };
  33. static int ssam_hid_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
  34. {
  35. u8 buffer[sizeof(struct surface_hid_buffer_slice) + 0x76];
  36. struct surface_hid_buffer_slice *slice;
  37. struct ssam_request rqst;
  38. struct ssam_response rsp;
  39. u32 buffer_len, offset, length;
  40. int status;
  41. /*
  42. * Note: The 0x76 above has been chosen because that's what's used by
  43. * the Windows driver. Together with the header, this leads to a 128
  44. * byte payload in total.
  45. */
  46. buffer_len = ARRAY_SIZE(buffer) - sizeof(struct surface_hid_buffer_slice);
  47. rqst.target_category = shid->uid.category;
  48. rqst.target_id = shid->uid.target;
  49. rqst.command_id = SURFACE_HID_CID_GET_DESCRIPTOR;
  50. rqst.instance_id = shid->uid.instance;
  51. rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  52. rqst.length = sizeof(struct surface_hid_buffer_slice);
  53. rqst.payload = buffer;
  54. rsp.capacity = ARRAY_SIZE(buffer);
  55. rsp.pointer = buffer;
  56. slice = (struct surface_hid_buffer_slice *)buffer;
  57. slice->entry = entry;
  58. slice->end = 0;
  59. offset = 0;
  60. length = buffer_len;
  61. while (!slice->end && offset < len) {
  62. put_unaligned_le32(offset, &slice->offset);
  63. put_unaligned_le32(length, &slice->length);
  64. rsp.length = 0;
  65. status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp,
  66. sizeof(*slice));
  67. if (status)
  68. return status;
  69. offset = get_unaligned_le32(&slice->offset);
  70. length = get_unaligned_le32(&slice->length);
  71. /* Don't mess stuff up in case we receive garbage. */
  72. if (length > buffer_len || offset > len)
  73. return -EPROTO;
  74. if (offset + length > len)
  75. length = len - offset;
  76. memcpy(buf + offset, &slice->data[0], length);
  77. offset += length;
  78. length = buffer_len;
  79. }
  80. if (offset != len) {
  81. dev_err(shid->dev, "unexpected descriptor length: got %u, expected %zu\n",
  82. offset, len);
  83. return -EPROTO;
  84. }
  85. return 0;
  86. }
  87. static int ssam_hid_set_raw_report(struct surface_hid_device *shid, u8 rprt_id, bool feature,
  88. u8 *buf, size_t len)
  89. {
  90. struct ssam_request rqst;
  91. u8 cid;
  92. if (feature)
  93. cid = SURFACE_HID_CID_SET_FEATURE_REPORT;
  94. else
  95. cid = SURFACE_HID_CID_OUTPUT_REPORT;
  96. rqst.target_category = shid->uid.category;
  97. rqst.target_id = shid->uid.target;
  98. rqst.instance_id = shid->uid.instance;
  99. rqst.command_id = cid;
  100. rqst.flags = 0;
  101. rqst.length = len;
  102. rqst.payload = buf;
  103. buf[0] = rprt_id;
  104. return ssam_retry(ssam_request_sync, shid->ctrl, &rqst, NULL);
  105. }
  106. static int ssam_hid_get_raw_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  107. {
  108. struct ssam_request rqst;
  109. struct ssam_response rsp;
  110. rqst.target_category = shid->uid.category;
  111. rqst.target_id = shid->uid.target;
  112. rqst.instance_id = shid->uid.instance;
  113. rqst.command_id = SURFACE_HID_CID_GET_FEATURE_REPORT;
  114. rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
  115. rqst.length = sizeof(rprt_id);
  116. rqst.payload = &rprt_id;
  117. rsp.capacity = len;
  118. rsp.length = 0;
  119. rsp.pointer = buf;
  120. return ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(rprt_id));
  121. }
  122. static u32 ssam_hid_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
  123. {
  124. struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
  125. if (event->command_id != 0x00)
  126. return 0;
  127. hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
  128. return SSAM_NOTIF_HANDLED;
  129. }
  130. /* -- Transport driver. ----------------------------------------------------- */
  131. static int shid_output_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  132. {
  133. int status;
  134. status = ssam_hid_set_raw_report(shid, rprt_id, false, buf, len);
  135. return status >= 0 ? len : status;
  136. }
  137. static int shid_get_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  138. {
  139. int status;
  140. status = ssam_hid_get_raw_report(shid, rprt_id, buf, len);
  141. return status >= 0 ? len : status;
  142. }
  143. static int shid_set_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
  144. {
  145. int status;
  146. status = ssam_hid_set_raw_report(shid, rprt_id, true, buf, len);
  147. return status >= 0 ? len : status;
  148. }
  149. /* -- Driver setup. --------------------------------------------------------- */
  150. static int surface_hid_probe(struct ssam_device *sdev)
  151. {
  152. struct surface_hid_device *shid;
  153. shid = devm_kzalloc(&sdev->dev, sizeof(*shid), GFP_KERNEL);
  154. if (!shid)
  155. return -ENOMEM;
  156. shid->dev = &sdev->dev;
  157. shid->ctrl = sdev->ctrl;
  158. shid->uid = sdev->uid;
  159. shid->notif.base.priority = 1;
  160. shid->notif.base.fn = ssam_hid_event_fn;
  161. shid->notif.event.reg = SSAM_EVENT_REGISTRY_REG(sdev->uid.target);
  162. shid->notif.event.id.target_category = sdev->uid.category;
  163. shid->notif.event.id.instance = sdev->uid.instance;
  164. shid->notif.event.mask = SSAM_EVENT_MASK_STRICT;
  165. shid->notif.event.flags = 0;
  166. shid->ops.get_descriptor = ssam_hid_get_descriptor;
  167. shid->ops.output_report = shid_output_report;
  168. shid->ops.get_feature_report = shid_get_feature_report;
  169. shid->ops.set_feature_report = shid_set_feature_report;
  170. ssam_device_set_drvdata(sdev, shid);
  171. return surface_hid_device_add(shid);
  172. }
  173. static void surface_hid_remove(struct ssam_device *sdev)
  174. {
  175. surface_hid_device_destroy(ssam_device_get_drvdata(sdev));
  176. }
  177. static const struct ssam_device_id surface_hid_match[] = {
  178. { SSAM_SDEV(HID, SSAM_ANY_TID, SSAM_ANY_IID, 0x00) },
  179. { },
  180. };
  181. MODULE_DEVICE_TABLE(ssam, surface_hid_match);
  182. static struct ssam_device_driver surface_hid_driver = {
  183. .probe = surface_hid_probe,
  184. .remove = surface_hid_remove,
  185. .match_table = surface_hid_match,
  186. .driver = {
  187. .name = "surface_hid",
  188. .pm = &surface_hid_pm_ops,
  189. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  190. },
  191. };
  192. module_ssam_device_driver(surface_hid_driver);
  193. MODULE_AUTHOR("Blaž Hrastnik <[email protected]>");
  194. MODULE_AUTHOR("Maximilian Luz <[email protected]>");
  195. MODULE_DESCRIPTION("HID transport driver for Surface System Aggregator Module");
  196. MODULE_LICENSE("GPL");