Hyper-V: add myself as a maintainer
[linux-2.6-block.git] / drivers / hid / hid-hyperv.c
CommitLineData
2025cf9e 1// SPDX-License-Identifier: GPL-2.0-only
b95f5bcb
S
2/*
3 * Copyright (c) 2009, Citrix Systems, Inc.
4 * Copyright (c) 2010, Microsoft Corporation.
5 * Copyright (c) 2011, Novell Inc.
b95f5bcb
S
6 */
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>
15
16
17struct hv_input_dev_info {
18 unsigned int size;
19 unsigned short vendor;
20 unsigned short product;
21 unsigned short version;
22 unsigned short reserved[11];
23};
24
25/* The maximum size of a synthetic input message. */
26#define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
27
28/*
29 * Current version
30 *
31 * History:
32 * Beta, RC < 2008/1/22 1,0
33 * RC > 2008/1/22 2,0
34 */
35#define SYNTHHID_INPUT_VERSION_MAJOR 2
36#define SYNTHHID_INPUT_VERSION_MINOR 0
37#define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
38 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
39
40
41#pragma pack(push, 1)
42/*
43 * Message types in the synthetic input protocol
44 */
45enum synthhid_msg_type {
46 SYNTH_HID_PROTOCOL_REQUEST,
47 SYNTH_HID_PROTOCOL_RESPONSE,
48 SYNTH_HID_INITIAL_DEVICE_INFO,
49 SYNTH_HID_INITIAL_DEVICE_INFO_ACK,
50 SYNTH_HID_INPUT_REPORT,
51 SYNTH_HID_MAX
52};
53
54/*
55 * Basic message structures.
56 */
57struct synthhid_msg_hdr {
58 enum synthhid_msg_type type;
59 u32 size;
60};
61
62struct synthhid_msg {
63 struct synthhid_msg_hdr header;
64 char data[1]; /* Enclosed message */
65};
66
67union synthhid_version {
68 struct {
69 u16 minor_version;
70 u16 major_version;
71 };
72 u32 version;
73};
74
75/*
76 * Protocol messages
77 */
78struct synthhid_protocol_request {
79 struct synthhid_msg_hdr header;
80 union synthhid_version version_requested;
81};
82
83struct synthhid_protocol_response {
84 struct synthhid_msg_hdr header;
85 union synthhid_version version_requested;
86 unsigned char approved;
87};
88
89struct synthhid_device_info {
90 struct synthhid_msg_hdr header;
91 struct hv_input_dev_info hid_dev_info;
92 struct hid_descriptor hid_descriptor;
93};
94
95struct synthhid_device_info_ack {
96 struct synthhid_msg_hdr header;
97 unsigned char reserved;
98};
99
100struct synthhid_input_report {
101 struct synthhid_msg_hdr header;
102 char buffer[1];
103};
104
105#pragma pack(pop)
106
345f0254
MN
107#define INPUTVSC_SEND_RING_BUFFER_SIZE (40 * 1024)
108#define INPUTVSC_RECV_RING_BUFFER_SIZE (40 * 1024)
b95f5bcb
S
109
110
111enum pipe_prot_msg_type {
112 PIPE_MESSAGE_INVALID,
113 PIPE_MESSAGE_DATA,
114 PIPE_MESSAGE_MAXIMUM
115};
116
117
118struct pipe_prt_msg {
119 enum pipe_prot_msg_type type;
120 u32 size;
121 char data[1];
122};
123
124struct mousevsc_prt_msg {
125 enum pipe_prot_msg_type type;
126 u32 size;
127 union {
128 struct synthhid_protocol_request request;
129 struct synthhid_protocol_response response;
130 struct synthhid_device_info_ack ack;
131 };
132};
133
134/*
135 * Represents an mousevsc device
136 */
137struct mousevsc_dev {
138 struct hv_device *device;
139 bool init_complete;
140 bool connected;
141 struct mousevsc_prt_msg protocol_req;
142 struct mousevsc_prt_msg protocol_resp;
143 /* Synchronize the request/response if needed */
144 struct completion wait_event;
145 int dev_info_status;
146
147 struct hid_descriptor *hid_desc;
148 unsigned char *report_desc;
149 u32 report_desc_size;
150 struct hv_input_dev_info hid_dev_info;
151 struct hid_device *hid_device;
3ccfd0a8 152 u8 input_buf[HID_MAX_BUFFER_SIZE];
b95f5bcb
S
153};
154
155
156static struct mousevsc_dev *mousevsc_alloc_device(struct hv_device *device)
157{
158 struct mousevsc_dev *input_dev;
159
160 input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
161
162 if (!input_dev)
163 return NULL;
164
165 input_dev->device = device;
166 hv_set_drvdata(device, input_dev);
167 init_completion(&input_dev->wait_event);
168 input_dev->init_complete = false;
169
170 return input_dev;
171}
172
173static void mousevsc_free_device(struct mousevsc_dev *device)
174{
175 kfree(device->hid_desc);
176 kfree(device->report_desc);
177 hv_set_drvdata(device->device, NULL);
178 kfree(device);
179}
180
181static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
182 struct synthhid_device_info *device_info)
183{
184 int ret = 0;
185 struct hid_descriptor *desc;
186 struct mousevsc_prt_msg ack;
187
188 input_device->dev_info_status = -ENOMEM;
189
190 input_device->hid_dev_info = device_info->hid_dev_info;
191 desc = &device_info->hid_descriptor;
192 if (desc->bLength == 0)
193 goto cleanup;
194
af13f9ed
DC
195 /* The pointer is not NULL when we resume from hibernation */
196 if (input_device->hid_desc != NULL)
197 kfree(input_device->hid_desc);
a4a23f6d 198 input_device->hid_desc = kmemdup(desc, desc->bLength, GFP_ATOMIC);
b95f5bcb
S
199
200 if (!input_device->hid_desc)
201 goto cleanup;
202
b95f5bcb
S
203 input_device->report_desc_size = desc->desc[0].wDescriptorLength;
204 if (input_device->report_desc_size == 0) {
205 input_device->dev_info_status = -EINVAL;
206 goto cleanup;
207 }
208
af13f9ed
DC
209 /* The pointer is not NULL when we resume from hibernation */
210 if (input_device->report_desc != NULL)
211 kfree(input_device->report_desc);
b95f5bcb
S
212 input_device->report_desc = kzalloc(input_device->report_desc_size,
213 GFP_ATOMIC);
214
215 if (!input_device->report_desc) {
216 input_device->dev_info_status = -ENOMEM;
217 goto cleanup;
218 }
219
220 memcpy(input_device->report_desc,
221 ((unsigned char *)desc) + desc->bLength,
222 desc->desc[0].wDescriptorLength);
223
224 /* Send the ack */
225 memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
226
227 ack.type = PIPE_MESSAGE_DATA;
228 ack.size = sizeof(struct synthhid_device_info_ack);
229
230 ack.ack.header.type = SYNTH_HID_INITIAL_DEVICE_INFO_ACK;
231 ack.ack.header.size = 1;
232 ack.ack.reserved = 0;
233
234 ret = vmbus_sendpacket(input_device->device->channel,
235 &ack,
236 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
237 sizeof(struct synthhid_device_info_ack),
238 (unsigned long)&ack,
239 VM_PKT_DATA_INBAND,
240 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
241
242 if (!ret)
243 input_device->dev_info_status = 0;
244
245cleanup:
246 complete(&input_device->wait_event);
247
248 return;
249}
250
251static void mousevsc_on_receive(struct hv_device *device,
252 struct vmpacket_descriptor *packet)
253{
254 struct pipe_prt_msg *pipe_msg;
255 struct synthhid_msg *hid_msg;
256 struct mousevsc_dev *input_dev = hv_get_drvdata(device);
257 struct synthhid_input_report *input_report;
3ccfd0a8 258 size_t len;
b95f5bcb
S
259
260 pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
261 (packet->offset8 << 3));
262
263 if (pipe_msg->type != PIPE_MESSAGE_DATA)
264 return;
265
266 hid_msg = (struct synthhid_msg *)pipe_msg->data;
267
268 switch (hid_msg->header.type) {
269 case SYNTH_HID_PROTOCOL_RESPONSE:
270 /*
271 * While it will be impossible for us to protect against
272 * malicious/buggy hypervisor/host, add a check here to
273 * ensure we don't corrupt memory.
274 */
275 if ((pipe_msg->size + sizeof(struct pipe_prt_msg)
276 - sizeof(unsigned char))
277 > sizeof(struct mousevsc_prt_msg)) {
278 WARN_ON(1);
279 break;
280 }
281
282 memcpy(&input_dev->protocol_resp, pipe_msg,
283 pipe_msg->size + sizeof(struct pipe_prt_msg) -
284 sizeof(unsigned char));
285 complete(&input_dev->wait_event);
286 break;
287
288 case SYNTH_HID_INITIAL_DEVICE_INFO:
289 WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info));
290
291 /*
292 * Parse out the device info into device attr,
293 * hid desc and report desc
294 */
295 mousevsc_on_receive_device_info(input_dev,
296 (struct synthhid_device_info *)pipe_msg->data);
297 break;
298 case SYNTH_HID_INPUT_REPORT:
299 input_report =
300 (struct synthhid_input_report *)pipe_msg->data;
301 if (!input_dev->init_complete)
302 break;
3ccfd0a8
DH
303
304 len = min(input_report->header.size,
305 (u32)sizeof(input_dev->input_buf));
306 memcpy(input_dev->input_buf, input_report->buffer, len);
307 hid_input_report(input_dev->hid_device, HID_INPUT_REPORT,
308 input_dev->input_buf, len, 1);
f1210455 309
10f91c73 310 pm_wakeup_hard_event(&input_dev->device->device);
f1210455 311
b95f5bcb
S
312 break;
313 default:
af423779 314 pr_err("unsupported hid msg type - type %d len %d\n",
b95f5bcb
S
315 hid_msg->header.type, hid_msg->header.size);
316 break;
317 }
318
319}
320
321static void mousevsc_on_channel_callback(void *context)
322{
b95f5bcb 323 struct hv_device *device = context;
b95f5bcb 324 struct vmpacket_descriptor *desc;
b95f5bcb 325
6a297c90
DC
326 foreach_vmbus_pkt(desc, device->channel) {
327 switch (desc->type) {
328 case VM_PKT_COMP:
b95f5bcb
S
329 break;
330
6a297c90
DC
331 case VM_PKT_DATA_INBAND:
332 mousevsc_on_receive(device, desc);
333 break;
b95f5bcb 334
6a297c90
DC
335 default:
336 pr_err("Unhandled packet type %d, tid %llx len %d\n",
337 desc->type, desc->trans_id, desc->len8 * 8);
b95f5bcb
S
338 break;
339 }
6a297c90 340 }
b95f5bcb
S
341}
342
343static int mousevsc_connect_to_vsp(struct hv_device *device)
344{
345 int ret = 0;
3458e4c0 346 unsigned long t;
b95f5bcb
S
347 struct mousevsc_dev *input_dev = hv_get_drvdata(device);
348 struct mousevsc_prt_msg *request;
349 struct mousevsc_prt_msg *response;
350
af13f9ed
DC
351 reinit_completion(&input_dev->wait_event);
352
b95f5bcb
S
353 request = &input_dev->protocol_req;
354 memset(request, 0, sizeof(struct mousevsc_prt_msg));
355
356 request->type = PIPE_MESSAGE_DATA;
357 request->size = sizeof(struct synthhid_protocol_request);
358 request->request.header.type = SYNTH_HID_PROTOCOL_REQUEST;
359 request->request.header.size = sizeof(unsigned int);
360 request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
361
362 ret = vmbus_sendpacket(device->channel, request,
363 sizeof(struct pipe_prt_msg) -
364 sizeof(unsigned char) +
365 sizeof(struct synthhid_protocol_request),
366 (unsigned long)request,
367 VM_PKT_DATA_INBAND,
368 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
369 if (ret)
370 goto cleanup;
371
372 t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
373 if (!t) {
374 ret = -ETIMEDOUT;
375 goto cleanup;
376 }
377
378 response = &input_dev->protocol_resp;
379
380 if (!response->response.approved) {
381 pr_err("synthhid protocol request failed (version %d)\n",
382 SYNTHHID_INPUT_VERSION);
383 ret = -ENODEV;
384 goto cleanup;
385 }
386
387 t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
388 if (!t) {
389 ret = -ETIMEDOUT;
390 goto cleanup;
391 }
392
393 /*
394 * We should have gotten the device attr, hid desc and report
395 * desc at this point
396 */
397 ret = input_dev->dev_info_status;
398
399cleanup:
400 return ret;
401}
402
07d9ab4f
HR
403static int mousevsc_hid_parse(struct hid_device *hid)
404{
405 struct hv_device *dev = hid_get_drvdata(hid);
406 struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
407
408 return hid_parse_report(hid, input_dev->report_desc,
409 input_dev->report_desc_size);
410}
411
b95f5bcb
S
412static int mousevsc_hid_open(struct hid_device *hid)
413{
414 return 0;
415}
416
417static int mousevsc_hid_start(struct hid_device *hid)
418{
419 return 0;
420}
421
422static void mousevsc_hid_close(struct hid_device *hid)
423{
424}
425
426static void mousevsc_hid_stop(struct hid_device *hid)
427{
428}
429
269ddfc6
S
430static int mousevsc_hid_raw_request(struct hid_device *hid,
431 unsigned char report_num,
c3d77fab 432 __u8 *buf, size_t len,
269ddfc6
S
433 unsigned char rtype,
434 int reqtype)
435{
436 return 0;
437}
438
b95f5bcb 439static struct hid_ll_driver mousevsc_ll_driver = {
07d9ab4f 440 .parse = mousevsc_hid_parse,
b95f5bcb
S
441 .open = mousevsc_hid_open,
442 .close = mousevsc_hid_close,
443 .start = mousevsc_hid_start,
444 .stop = mousevsc_hid_stop,
269ddfc6 445 .raw_request = mousevsc_hid_raw_request,
b95f5bcb
S
446};
447
448static struct hid_driver mousevsc_hid_driver;
449
450static int mousevsc_probe(struct hv_device *device,
451 const struct hv_vmbus_device_id *dev_id)
452{
453 int ret;
454 struct mousevsc_dev *input_dev;
455 struct hid_device *hid_dev;
456
457 input_dev = mousevsc_alloc_device(device);
458
459 if (!input_dev)
460 return -ENOMEM;
461
462 ret = vmbus_open(device->channel,
463 INPUTVSC_SEND_RING_BUFFER_SIZE,
464 INPUTVSC_RECV_RING_BUFFER_SIZE,
465 NULL,
466 0,
467 mousevsc_on_channel_callback,
468 device
469 );
470
471 if (ret)
472 goto probe_err0;
473
474 ret = mousevsc_connect_to_vsp(device);
475
476 if (ret)
477 goto probe_err1;
478
479 /* workaround SA-167 */
480 if (input_dev->report_desc[14] == 0x25)
481 input_dev->report_desc[14] = 0x29;
482
483 hid_dev = hid_allocate_device();
484 if (IS_ERR(hid_dev)) {
485 ret = PTR_ERR(hid_dev);
486 goto probe_err1;
487 }
488
489 hid_dev->ll_driver = &mousevsc_ll_driver;
490 hid_dev->driver = &mousevsc_hid_driver;
491 hid_dev->bus = BUS_VIRTUAL;
492 hid_dev->vendor = input_dev->hid_dev_info.vendor;
493 hid_dev->product = input_dev->hid_dev_info.product;
494 hid_dev->version = input_dev->hid_dev_info.version;
495 input_dev->hid_device = hid_dev;
496
497 sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
498
b565a390
S
499 hid_set_drvdata(hid_dev, device);
500
74c4fb05
S
501 ret = hid_add_device(hid_dev);
502 if (ret)
503 goto probe_err1;
504
b95f5bcb 505
07d9ab4f 506 ret = hid_parse(hid_dev);
b95f5bcb
S
507 if (ret) {
508 hid_err(hid_dev, "parse failed\n");
509 goto probe_err2;
510 }
511
512 ret = hid_hw_start(hid_dev, HID_CONNECT_HIDINPUT | HID_CONNECT_HIDDEV);
513
514 if (ret) {
515 hid_err(hid_dev, "hw start failed\n");
516 goto probe_err2;
517 }
518
f1210455
DC
519 device_init_wakeup(&device->device, true);
520
b95f5bcb
S
521 input_dev->connected = true;
522 input_dev->init_complete = true;
523
524 return ret;
525
526probe_err2:
527 hid_destroy_device(hid_dev);
528
529probe_err1:
530 vmbus_close(device->channel);
531
532probe_err0:
533 mousevsc_free_device(input_dev);
534
535 return ret;
536}
537
538
539static int mousevsc_remove(struct hv_device *dev)
540{
541 struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
542
f1210455 543 device_init_wakeup(&dev->device, false);
b95f5bcb 544 vmbus_close(dev->channel);
c1c454b8 545 hid_hw_stop(input_dev->hid_device);
b95f5bcb
S
546 hid_destroy_device(input_dev->hid_device);
547 mousevsc_free_device(input_dev);
548
549 return 0;
550}
551
af13f9ed
DC
552static int mousevsc_suspend(struct hv_device *dev)
553{
554 vmbus_close(dev->channel);
555
556 return 0;
557}
558
559static int mousevsc_resume(struct hv_device *dev)
560{
561 int ret;
562
563 ret = vmbus_open(dev->channel,
564 INPUTVSC_SEND_RING_BUFFER_SIZE,
565 INPUTVSC_RECV_RING_BUFFER_SIZE,
566 NULL, 0,
567 mousevsc_on_channel_callback,
568 dev);
569 if (ret)
570 return ret;
571
572 ret = mousevsc_connect_to_vsp(dev);
573 return ret;
574}
575
b95f5bcb
S
576static const struct hv_vmbus_device_id id_table[] = {
577 /* Mouse guid */
048c5add 578 { HV_MOUSE_GUID, },
b95f5bcb
S
579 { },
580};
581
582MODULE_DEVICE_TABLE(vmbus, id_table);
583
584static struct hv_driver mousevsc_drv = {
585 .name = KBUILD_MODNAME,
586 .id_table = id_table,
587 .probe = mousevsc_probe,
588 .remove = mousevsc_remove,
af13f9ed
DC
589 .suspend = mousevsc_suspend,
590 .resume = mousevsc_resume,
af0a5646
AV
591 .driver = {
592 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
593 },
b95f5bcb
S
594};
595
596static int __init mousevsc_init(void)
597{
598 return vmbus_driver_register(&mousevsc_drv);
599}
600
601static void __exit mousevsc_exit(void)
602{
603 vmbus_driver_unregister(&mousevsc_drv);
604}
605
606MODULE_LICENSE("GPL");
64caea53
JS
607MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic HID Driver");
608
b95f5bcb
S
609module_init(mousevsc_init);
610module_exit(mousevsc_exit);