1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Citrix Systems, Inc.
4 * Copyright (c) 2010, Microsoft Corporation.
5 * Copyright (c) 2011, Novell Inc.
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/device.h>
10 #include <linux/completion.h>
11 #include <linux/input.h>
12 #include <linux/hid.h>
13 #include <linux/hiddev.h>
14 #include <linux/hyperv.h>
17 struct hv_input_dev_info {
19 unsigned short vendor;
20 unsigned short product;
21 unsigned short version;
22 unsigned short reserved[11];
29 * Beta, RC < 2008/1/22 1,0
32 #define SYNTHHID_INPUT_VERSION_MAJOR 2
33 #define SYNTHHID_INPUT_VERSION_MINOR 0
34 #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
35 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
40 * Message types in the synthetic input protocol
42 enum synthhid_msg_type {
43 SYNTH_HID_PROTOCOL_REQUEST,
44 SYNTH_HID_PROTOCOL_RESPONSE,
45 SYNTH_HID_INITIAL_DEVICE_INFO,
46 SYNTH_HID_INITIAL_DEVICE_INFO_ACK,
47 SYNTH_HID_INPUT_REPORT,
52 * Basic message structures.
54 struct synthhid_msg_hdr {
55 enum synthhid_msg_type type;
59 union synthhid_version {
70 struct synthhid_protocol_request {
71 struct synthhid_msg_hdr header;
72 union synthhid_version version_requested;
75 struct synthhid_protocol_response {
76 struct synthhid_msg_hdr header;
77 union synthhid_version version_requested;
78 unsigned char approved;
81 struct synthhid_device_info {
82 struct synthhid_msg_hdr header;
83 struct hv_input_dev_info hid_dev_info;
84 struct hid_descriptor hid_descriptor;
87 struct synthhid_device_info_ack {
88 struct synthhid_msg_hdr header;
89 unsigned char reserved;
92 struct synthhid_input_report {
93 struct synthhid_msg_hdr header;
99 #define INPUTVSC_SEND_RING_BUFFER_SIZE VMBUS_RING_SIZE(36 * 1024)
100 #define INPUTVSC_RECV_RING_BUFFER_SIZE VMBUS_RING_SIZE(36 * 1024)
103 enum pipe_prot_msg_type {
104 PIPE_MESSAGE_INVALID,
110 struct pipe_prt_msg {
111 enum pipe_prot_msg_type type;
116 struct mousevsc_prt_msg {
117 enum pipe_prot_msg_type type;
120 struct synthhid_protocol_request request;
121 struct synthhid_protocol_response response;
122 struct synthhid_device_info_ack ack;
127 * Represents an mousevsc device
129 struct mousevsc_dev {
130 struct hv_device *device;
133 struct mousevsc_prt_msg protocol_req;
134 struct mousevsc_prt_msg protocol_resp;
135 /* Synchronize the request/response if needed */
136 struct completion wait_event;
139 struct hid_descriptor *hid_desc;
140 unsigned char *report_desc;
141 u32 report_desc_size;
142 struct hv_input_dev_info hid_dev_info;
143 struct hid_device *hid_device;
144 u8 input_buf[HID_MAX_BUFFER_SIZE];
148 static struct mousevsc_dev *mousevsc_alloc_device(struct hv_device *device)
150 struct mousevsc_dev *input_dev;
152 input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
157 input_dev->device = device;
158 hv_set_drvdata(device, input_dev);
159 init_completion(&input_dev->wait_event);
160 input_dev->init_complete = false;
165 static void mousevsc_free_device(struct mousevsc_dev *device)
167 kfree(device->hid_desc);
168 kfree(device->report_desc);
169 hv_set_drvdata(device->device, NULL);
173 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
174 struct synthhid_device_info *device_info)
177 struct hid_descriptor *desc;
178 struct mousevsc_prt_msg ack;
180 input_device->dev_info_status = -ENOMEM;
182 input_device->hid_dev_info = device_info->hid_dev_info;
183 desc = &device_info->hid_descriptor;
184 if (desc->bLength == 0)
187 /* The pointer is not NULL when we resume from hibernation */
188 kfree(input_device->hid_desc);
189 input_device->hid_desc = kmemdup(desc, desc->bLength, GFP_ATOMIC);
191 if (!input_device->hid_desc)
194 input_device->report_desc_size = le16_to_cpu(
195 desc->desc[0].wDescriptorLength);
196 if (input_device->report_desc_size == 0) {
197 input_device->dev_info_status = -EINVAL;
201 /* The pointer is not NULL when we resume from hibernation */
202 kfree(input_device->report_desc);
203 input_device->report_desc = kzalloc(input_device->report_desc_size,
206 if (!input_device->report_desc) {
207 input_device->dev_info_status = -ENOMEM;
211 memcpy(input_device->report_desc,
212 ((unsigned char *)desc) + desc->bLength,
213 le16_to_cpu(desc->desc[0].wDescriptorLength));
216 memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
218 ack.type = PIPE_MESSAGE_DATA;
219 ack.size = sizeof(struct synthhid_device_info_ack);
221 ack.ack.header.type = SYNTH_HID_INITIAL_DEVICE_INFO_ACK;
222 ack.ack.header.size = 1;
223 ack.ack.reserved = 0;
225 ret = vmbus_sendpacket(input_device->device->channel,
227 sizeof(struct pipe_prt_msg) +
228 sizeof(struct synthhid_device_info_ack),
231 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
234 input_device->dev_info_status = 0;
237 complete(&input_device->wait_event);
242 static void mousevsc_on_receive(struct hv_device *device,
243 struct vmpacket_descriptor *packet)
245 struct pipe_prt_msg *pipe_msg;
246 struct synthhid_msg_hdr *hid_msg_hdr;
247 struct mousevsc_dev *input_dev = hv_get_drvdata(device);
248 struct synthhid_input_report *input_report;
251 pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
252 (packet->offset8 << 3));
254 if (pipe_msg->type != PIPE_MESSAGE_DATA)
257 hid_msg_hdr = (struct synthhid_msg_hdr *)pipe_msg->data;
259 switch (hid_msg_hdr->type) {
260 case SYNTH_HID_PROTOCOL_RESPONSE:
261 len = struct_size(pipe_msg, data, pipe_msg->size);
264 * While it will be impossible for us to protect against
265 * malicious/buggy hypervisor/host, add a check here to
266 * ensure we don't corrupt memory.
268 if (WARN_ON(len > sizeof(struct mousevsc_prt_msg)))
271 memcpy(&input_dev->protocol_resp, pipe_msg, len);
272 complete(&input_dev->wait_event);
275 case SYNTH_HID_INITIAL_DEVICE_INFO:
276 WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info));
279 * Parse out the device info into device attr,
280 * hid desc and report desc
282 mousevsc_on_receive_device_info(input_dev,
283 (struct synthhid_device_info *)pipe_msg->data);
285 case SYNTH_HID_INPUT_REPORT:
287 (struct synthhid_input_report *)pipe_msg->data;
288 if (!input_dev->init_complete)
291 len = min(input_report->header.size,
292 (u32)sizeof(input_dev->input_buf));
293 memcpy(input_dev->input_buf, input_report->buffer, len);
294 hid_input_report(input_dev->hid_device, HID_INPUT_REPORT,
295 input_dev->input_buf, len, 1);
297 pm_wakeup_hard_event(&input_dev->device->device);
301 pr_err("unsupported hid msg type - type %d len %d\n",
302 hid_msg_hdr->type, hid_msg_hdr->size);
308 static void mousevsc_on_channel_callback(void *context)
310 struct hv_device *device = context;
311 struct vmpacket_descriptor *desc;
313 foreach_vmbus_pkt(desc, device->channel) {
314 switch (desc->type) {
318 case VM_PKT_DATA_INBAND:
319 mousevsc_on_receive(device, desc);
323 pr_err("Unhandled packet type %d, tid %llx len %d\n",
324 desc->type, desc->trans_id, desc->len8 * 8);
330 static int mousevsc_connect_to_vsp(struct hv_device *device)
334 struct mousevsc_dev *input_dev = hv_get_drvdata(device);
335 struct mousevsc_prt_msg *request;
336 struct mousevsc_prt_msg *response;
338 reinit_completion(&input_dev->wait_event);
340 request = &input_dev->protocol_req;
341 memset(request, 0, sizeof(struct mousevsc_prt_msg));
343 request->type = PIPE_MESSAGE_DATA;
344 request->size = sizeof(struct synthhid_protocol_request);
345 request->request.header.type = SYNTH_HID_PROTOCOL_REQUEST;
346 request->request.header.size = sizeof(unsigned int);
347 request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
349 ret = vmbus_sendpacket(device->channel, request,
350 sizeof(struct pipe_prt_msg) +
351 sizeof(struct synthhid_protocol_request),
352 (unsigned long)request,
354 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
358 t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
364 response = &input_dev->protocol_resp;
366 if (!response->response.approved) {
367 pr_err("synthhid protocol request failed (version %d)\n",
368 SYNTHHID_INPUT_VERSION);
373 t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
380 * We should have gotten the device attr, hid desc and report
383 ret = input_dev->dev_info_status;
389 static int mousevsc_hid_parse(struct hid_device *hid)
391 struct hv_device *dev = hid_get_drvdata(hid);
392 struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
394 return hid_parse_report(hid, input_dev->report_desc,
395 input_dev->report_desc_size);
398 static int mousevsc_hid_open(struct hid_device *hid)
403 static int mousevsc_hid_start(struct hid_device *hid)
408 static void mousevsc_hid_close(struct hid_device *hid)
412 static void mousevsc_hid_stop(struct hid_device *hid)
416 static int mousevsc_hid_raw_request(struct hid_device *hid,
417 unsigned char report_num,
418 __u8 *buf, size_t len,
425 static const struct hid_ll_driver mousevsc_ll_driver = {
426 .parse = mousevsc_hid_parse,
427 .open = mousevsc_hid_open,
428 .close = mousevsc_hid_close,
429 .start = mousevsc_hid_start,
430 .stop = mousevsc_hid_stop,
431 .raw_request = mousevsc_hid_raw_request,
434 static struct hid_driver mousevsc_hid_driver;
436 static int mousevsc_probe(struct hv_device *device,
437 const struct hv_vmbus_device_id *dev_id)
440 struct mousevsc_dev *input_dev;
441 struct hid_device *hid_dev;
443 input_dev = mousevsc_alloc_device(device);
448 ret = vmbus_open(device->channel,
449 INPUTVSC_SEND_RING_BUFFER_SIZE,
450 INPUTVSC_RECV_RING_BUFFER_SIZE,
453 mousevsc_on_channel_callback,
460 ret = mousevsc_connect_to_vsp(device);
465 /* workaround SA-167 */
466 if (input_dev->report_desc[14] == 0x25)
467 input_dev->report_desc[14] = 0x29;
469 hid_dev = hid_allocate_device();
470 if (IS_ERR(hid_dev)) {
471 ret = PTR_ERR(hid_dev);
475 hid_dev->ll_driver = &mousevsc_ll_driver;
476 hid_dev->driver = &mousevsc_hid_driver;
477 hid_dev->bus = BUS_VIRTUAL;
478 hid_dev->vendor = input_dev->hid_dev_info.vendor;
479 hid_dev->product = input_dev->hid_dev_info.product;
480 hid_dev->version = input_dev->hid_dev_info.version;
481 input_dev->hid_device = hid_dev;
483 sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
485 hid_set_drvdata(hid_dev, device);
487 ret = hid_add_device(hid_dev);
492 ret = hid_parse(hid_dev);
494 hid_err(hid_dev, "parse failed\n");
498 ret = hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV);
501 hid_err(hid_dev, "hw start failed\n");
505 device_init_wakeup(&device->device, true);
507 input_dev->connected = true;
508 input_dev->init_complete = true;
513 hid_destroy_device(hid_dev);
516 vmbus_close(device->channel);
519 mousevsc_free_device(input_dev);
525 static void mousevsc_remove(struct hv_device *dev)
527 struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
529 device_init_wakeup(&dev->device, false);
530 vmbus_close(dev->channel);
531 hid_hw_stop(input_dev->hid_device);
532 hid_destroy_device(input_dev->hid_device);
533 mousevsc_free_device(input_dev);
536 static int mousevsc_suspend(struct hv_device *dev)
538 vmbus_close(dev->channel);
543 static int mousevsc_resume(struct hv_device *dev)
547 ret = vmbus_open(dev->channel,
548 INPUTVSC_SEND_RING_BUFFER_SIZE,
549 INPUTVSC_RECV_RING_BUFFER_SIZE,
551 mousevsc_on_channel_callback,
556 ret = mousevsc_connect_to_vsp(dev);
560 static const struct hv_vmbus_device_id id_table[] = {
566 MODULE_DEVICE_TABLE(vmbus, id_table);
568 static struct hv_driver mousevsc_drv = {
569 .name = KBUILD_MODNAME,
570 .id_table = id_table,
571 .probe = mousevsc_probe,
572 .remove = mousevsc_remove,
573 .suspend = mousevsc_suspend,
574 .resume = mousevsc_resume,
576 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
580 static int __init mousevsc_init(void)
582 return vmbus_driver_register(&mousevsc_drv);
585 static void __exit mousevsc_exit(void)
587 vmbus_driver_unregister(&mousevsc_drv);
590 MODULE_LICENSE("GPL");
591 MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic HID Driver");
593 module_init(mousevsc_init);
594 module_exit(mousevsc_exit);