0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <asm/unaligned.h>
0012 #include <linux/hid.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/types.h>
0016
0017 #include <linux/surface_aggregator/controller.h>
0018 #include <linux/surface_aggregator/device.h>
0019
0020 #include "surface_hid_core.h"
0021
0022
0023
0024
0025 struct surface_hid_buffer_slice {
0026 __u8 entry;
0027 __le32 offset;
0028 __le32 length;
0029 __u8 end;
0030 __u8 data[];
0031 } __packed;
0032
0033 static_assert(sizeof(struct surface_hid_buffer_slice) == 10);
0034
0035 enum surface_hid_cid {
0036 SURFACE_HID_CID_OUTPUT_REPORT = 0x01,
0037 SURFACE_HID_CID_GET_FEATURE_REPORT = 0x02,
0038 SURFACE_HID_CID_SET_FEATURE_REPORT = 0x03,
0039 SURFACE_HID_CID_GET_DESCRIPTOR = 0x04,
0040 };
0041
0042 static int ssam_hid_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
0043 {
0044 u8 buffer[sizeof(struct surface_hid_buffer_slice) + 0x76];
0045 struct surface_hid_buffer_slice *slice;
0046 struct ssam_request rqst;
0047 struct ssam_response rsp;
0048 u32 buffer_len, offset, length;
0049 int status;
0050
0051
0052
0053
0054
0055
0056
0057 buffer_len = ARRAY_SIZE(buffer) - sizeof(struct surface_hid_buffer_slice);
0058
0059 rqst.target_category = shid->uid.category;
0060 rqst.target_id = shid->uid.target;
0061 rqst.command_id = SURFACE_HID_CID_GET_DESCRIPTOR;
0062 rqst.instance_id = shid->uid.instance;
0063 rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
0064 rqst.length = sizeof(struct surface_hid_buffer_slice);
0065 rqst.payload = buffer;
0066
0067 rsp.capacity = ARRAY_SIZE(buffer);
0068 rsp.pointer = buffer;
0069
0070 slice = (struct surface_hid_buffer_slice *)buffer;
0071 slice->entry = entry;
0072 slice->end = 0;
0073
0074 offset = 0;
0075 length = buffer_len;
0076
0077 while (!slice->end && offset < len) {
0078 put_unaligned_le32(offset, &slice->offset);
0079 put_unaligned_le32(length, &slice->length);
0080
0081 rsp.length = 0;
0082
0083 status = ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp,
0084 sizeof(*slice));
0085 if (status)
0086 return status;
0087
0088 offset = get_unaligned_le32(&slice->offset);
0089 length = get_unaligned_le32(&slice->length);
0090
0091
0092 if (length > buffer_len || offset > len)
0093 return -EPROTO;
0094
0095 if (offset + length > len)
0096 length = len - offset;
0097
0098 memcpy(buf + offset, &slice->data[0], length);
0099
0100 offset += length;
0101 length = buffer_len;
0102 }
0103
0104 if (offset != len) {
0105 dev_err(shid->dev, "unexpected descriptor length: got %u, expected %zu\n",
0106 offset, len);
0107 return -EPROTO;
0108 }
0109
0110 return 0;
0111 }
0112
0113 static int ssam_hid_set_raw_report(struct surface_hid_device *shid, u8 rprt_id, bool feature,
0114 u8 *buf, size_t len)
0115 {
0116 struct ssam_request rqst;
0117 u8 cid;
0118
0119 if (feature)
0120 cid = SURFACE_HID_CID_SET_FEATURE_REPORT;
0121 else
0122 cid = SURFACE_HID_CID_OUTPUT_REPORT;
0123
0124 rqst.target_category = shid->uid.category;
0125 rqst.target_id = shid->uid.target;
0126 rqst.instance_id = shid->uid.instance;
0127 rqst.command_id = cid;
0128 rqst.flags = 0;
0129 rqst.length = len;
0130 rqst.payload = buf;
0131
0132 buf[0] = rprt_id;
0133
0134 return ssam_retry(ssam_request_sync, shid->ctrl, &rqst, NULL);
0135 }
0136
0137 static int ssam_hid_get_raw_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
0138 {
0139 struct ssam_request rqst;
0140 struct ssam_response rsp;
0141
0142 rqst.target_category = shid->uid.category;
0143 rqst.target_id = shid->uid.target;
0144 rqst.instance_id = shid->uid.instance;
0145 rqst.command_id = SURFACE_HID_CID_GET_FEATURE_REPORT;
0146 rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
0147 rqst.length = sizeof(rprt_id);
0148 rqst.payload = &rprt_id;
0149
0150 rsp.capacity = len;
0151 rsp.length = 0;
0152 rsp.pointer = buf;
0153
0154 return ssam_retry(ssam_request_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(rprt_id));
0155 }
0156
0157 static u32 ssam_hid_event_fn(struct ssam_event_notifier *nf, const struct ssam_event *event)
0158 {
0159 struct surface_hid_device *shid = container_of(nf, struct surface_hid_device, notif);
0160
0161 if (event->command_id != 0x00)
0162 return 0;
0163
0164 hid_input_report(shid->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
0165 return SSAM_NOTIF_HANDLED;
0166 }
0167
0168
0169
0170
0171 static int shid_output_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
0172 {
0173 int status;
0174
0175 status = ssam_hid_set_raw_report(shid, rprt_id, false, buf, len);
0176 return status >= 0 ? len : status;
0177 }
0178
0179 static int shid_get_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
0180 {
0181 int status;
0182
0183 status = ssam_hid_get_raw_report(shid, rprt_id, buf, len);
0184 return status >= 0 ? len : status;
0185 }
0186
0187 static int shid_set_feature_report(struct surface_hid_device *shid, u8 rprt_id, u8 *buf, size_t len)
0188 {
0189 int status;
0190
0191 status = ssam_hid_set_raw_report(shid, rprt_id, true, buf, len);
0192 return status >= 0 ? len : status;
0193 }
0194
0195
0196
0197
0198 static int surface_hid_probe(struct ssam_device *sdev)
0199 {
0200 struct surface_hid_device *shid;
0201
0202 shid = devm_kzalloc(&sdev->dev, sizeof(*shid), GFP_KERNEL);
0203 if (!shid)
0204 return -ENOMEM;
0205
0206 shid->dev = &sdev->dev;
0207 shid->ctrl = sdev->ctrl;
0208 shid->uid = sdev->uid;
0209
0210 shid->notif.base.priority = 1;
0211 shid->notif.base.fn = ssam_hid_event_fn;
0212 shid->notif.event.reg = SSAM_EVENT_REGISTRY_REG(sdev->uid.target);
0213 shid->notif.event.id.target_category = sdev->uid.category;
0214 shid->notif.event.id.instance = sdev->uid.instance;
0215 shid->notif.event.mask = SSAM_EVENT_MASK_STRICT;
0216 shid->notif.event.flags = 0;
0217
0218 shid->ops.get_descriptor = ssam_hid_get_descriptor;
0219 shid->ops.output_report = shid_output_report;
0220 shid->ops.get_feature_report = shid_get_feature_report;
0221 shid->ops.set_feature_report = shid_set_feature_report;
0222
0223 ssam_device_set_drvdata(sdev, shid);
0224 return surface_hid_device_add(shid);
0225 }
0226
0227 static void surface_hid_remove(struct ssam_device *sdev)
0228 {
0229 surface_hid_device_destroy(ssam_device_get_drvdata(sdev));
0230 }
0231
0232 static const struct ssam_device_id surface_hid_match[] = {
0233 { SSAM_SDEV(HID, SSAM_ANY_TID, SSAM_ANY_IID, 0x00) },
0234 { },
0235 };
0236 MODULE_DEVICE_TABLE(ssam, surface_hid_match);
0237
0238 static struct ssam_device_driver surface_hid_driver = {
0239 .probe = surface_hid_probe,
0240 .remove = surface_hid_remove,
0241 .match_table = surface_hid_match,
0242 .driver = {
0243 .name = "surface_hid",
0244 .pm = &surface_hid_pm_ops,
0245 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0246 },
0247 };
0248 module_ssam_device_driver(surface_hid_driver);
0249
0250 MODULE_AUTHOR("Blaž Hrastnik <blaz@mxxn.io>");
0251 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
0252 MODULE_DESCRIPTION("HID transport driver for Surface System Aggregator Module");
0253 MODULE_LICENSE("GPL");