]>
Commit | Line | Data |
---|---|---|
b95f5bcb S |
1 | /* |
2 | * Copyright (c) 2009, Citrix Systems, Inc. | |
3 | * Copyright (c) 2010, Microsoft Corporation. | |
4 | * Copyright (c) 2011, Novell Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | */ | |
15 | #include <linux/init.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/device.h> | |
18 | #include <linux/completion.h> | |
19 | #include <linux/input.h> | |
20 | #include <linux/hid.h> | |
21 | #include <linux/hiddev.h> | |
22 | #include <linux/hyperv.h> | |
23 | ||
24 | ||
25 | struct hv_input_dev_info { | |
26 | unsigned int size; | |
27 | unsigned short vendor; | |
28 | unsigned short product; | |
29 | unsigned short version; | |
30 | unsigned short reserved[11]; | |
31 | }; | |
32 | ||
33 | /* The maximum size of a synthetic input message. */ | |
34 | #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16 | |
35 | ||
36 | /* | |
37 | * Current version | |
38 | * | |
39 | * History: | |
40 | * Beta, RC < 2008/1/22 1,0 | |
41 | * RC > 2008/1/22 2,0 | |
42 | */ | |
43 | #define SYNTHHID_INPUT_VERSION_MAJOR 2 | |
44 | #define SYNTHHID_INPUT_VERSION_MINOR 0 | |
45 | #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \ | |
46 | (SYNTHHID_INPUT_VERSION_MAJOR << 16)) | |
47 | ||
48 | ||
49 | #pragma pack(push, 1) | |
50 | /* | |
51 | * Message types in the synthetic input protocol | |
52 | */ | |
53 | enum synthhid_msg_type { | |
54 | SYNTH_HID_PROTOCOL_REQUEST, | |
55 | SYNTH_HID_PROTOCOL_RESPONSE, | |
56 | SYNTH_HID_INITIAL_DEVICE_INFO, | |
57 | SYNTH_HID_INITIAL_DEVICE_INFO_ACK, | |
58 | SYNTH_HID_INPUT_REPORT, | |
59 | SYNTH_HID_MAX | |
60 | }; | |
61 | ||
62 | /* | |
63 | * Basic message structures. | |
64 | */ | |
65 | struct synthhid_msg_hdr { | |
66 | enum synthhid_msg_type type; | |
67 | u32 size; | |
68 | }; | |
69 | ||
70 | struct synthhid_msg { | |
71 | struct synthhid_msg_hdr header; | |
72 | char data[1]; /* Enclosed message */ | |
73 | }; | |
74 | ||
75 | union synthhid_version { | |
76 | struct { | |
77 | u16 minor_version; | |
78 | u16 major_version; | |
79 | }; | |
80 | u32 version; | |
81 | }; | |
82 | ||
83 | /* | |
84 | * Protocol messages | |
85 | */ | |
86 | struct synthhid_protocol_request { | |
87 | struct synthhid_msg_hdr header; | |
88 | union synthhid_version version_requested; | |
89 | }; | |
90 | ||
91 | struct synthhid_protocol_response { | |
92 | struct synthhid_msg_hdr header; | |
93 | union synthhid_version version_requested; | |
94 | unsigned char approved; | |
95 | }; | |
96 | ||
97 | struct synthhid_device_info { | |
98 | struct synthhid_msg_hdr header; | |
99 | struct hv_input_dev_info hid_dev_info; | |
100 | struct hid_descriptor hid_descriptor; | |
101 | }; | |
102 | ||
103 | struct synthhid_device_info_ack { | |
104 | struct synthhid_msg_hdr header; | |
105 | unsigned char reserved; | |
106 | }; | |
107 | ||
108 | struct synthhid_input_report { | |
109 | struct synthhid_msg_hdr header; | |
110 | char buffer[1]; | |
111 | }; | |
112 | ||
113 | #pragma pack(pop) | |
114 | ||
115 | #define INPUTVSC_SEND_RING_BUFFER_SIZE (10*PAGE_SIZE) | |
116 | #define INPUTVSC_RECV_RING_BUFFER_SIZE (10*PAGE_SIZE) | |
117 | ||
118 | ||
119 | enum pipe_prot_msg_type { | |
120 | PIPE_MESSAGE_INVALID, | |
121 | PIPE_MESSAGE_DATA, | |
122 | PIPE_MESSAGE_MAXIMUM | |
123 | }; | |
124 | ||
125 | ||
126 | struct pipe_prt_msg { | |
127 | enum pipe_prot_msg_type type; | |
128 | u32 size; | |
129 | char data[1]; | |
130 | }; | |
131 | ||
132 | struct mousevsc_prt_msg { | |
133 | enum pipe_prot_msg_type type; | |
134 | u32 size; | |
135 | union { | |
136 | struct synthhid_protocol_request request; | |
137 | struct synthhid_protocol_response response; | |
138 | struct synthhid_device_info_ack ack; | |
139 | }; | |
140 | }; | |
141 | ||
142 | /* | |
143 | * Represents an mousevsc device | |
144 | */ | |
145 | struct mousevsc_dev { | |
146 | struct hv_device *device; | |
147 | bool init_complete; | |
148 | bool connected; | |
149 | struct mousevsc_prt_msg protocol_req; | |
150 | struct mousevsc_prt_msg protocol_resp; | |
151 | /* Synchronize the request/response if needed */ | |
152 | struct completion wait_event; | |
153 | int dev_info_status; | |
154 | ||
155 | struct hid_descriptor *hid_desc; | |
156 | unsigned char *report_desc; | |
157 | u32 report_desc_size; | |
158 | struct hv_input_dev_info hid_dev_info; | |
159 | struct hid_device *hid_device; | |
3ccfd0a8 | 160 | u8 input_buf[HID_MAX_BUFFER_SIZE]; |
b95f5bcb S |
161 | }; |
162 | ||
163 | ||
164 | static struct mousevsc_dev *mousevsc_alloc_device(struct hv_device *device) | |
165 | { | |
166 | struct mousevsc_dev *input_dev; | |
167 | ||
168 | input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL); | |
169 | ||
170 | if (!input_dev) | |
171 | return NULL; | |
172 | ||
173 | input_dev->device = device; | |
174 | hv_set_drvdata(device, input_dev); | |
175 | init_completion(&input_dev->wait_event); | |
176 | input_dev->init_complete = false; | |
177 | ||
178 | return input_dev; | |
179 | } | |
180 | ||
181 | static void mousevsc_free_device(struct mousevsc_dev *device) | |
182 | { | |
183 | kfree(device->hid_desc); | |
184 | kfree(device->report_desc); | |
185 | hv_set_drvdata(device->device, NULL); | |
186 | kfree(device); | |
187 | } | |
188 | ||
189 | static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device, | |
190 | struct synthhid_device_info *device_info) | |
191 | { | |
192 | int ret = 0; | |
193 | struct hid_descriptor *desc; | |
194 | struct mousevsc_prt_msg ack; | |
195 | ||
196 | input_device->dev_info_status = -ENOMEM; | |
197 | ||
198 | input_device->hid_dev_info = device_info->hid_dev_info; | |
199 | desc = &device_info->hid_descriptor; | |
200 | if (desc->bLength == 0) | |
201 | goto cleanup; | |
202 | ||
a4a23f6d | 203 | input_device->hid_desc = kmemdup(desc, desc->bLength, GFP_ATOMIC); |
b95f5bcb S |
204 | |
205 | if (!input_device->hid_desc) | |
206 | goto cleanup; | |
207 | ||
b95f5bcb S |
208 | input_device->report_desc_size = desc->desc[0].wDescriptorLength; |
209 | if (input_device->report_desc_size == 0) { | |
210 | input_device->dev_info_status = -EINVAL; | |
211 | goto cleanup; | |
212 | } | |
213 | ||
214 | input_device->report_desc = kzalloc(input_device->report_desc_size, | |
215 | GFP_ATOMIC); | |
216 | ||
217 | if (!input_device->report_desc) { | |
218 | input_device->dev_info_status = -ENOMEM; | |
219 | goto cleanup; | |
220 | } | |
221 | ||
222 | memcpy(input_device->report_desc, | |
223 | ((unsigned char *)desc) + desc->bLength, | |
224 | desc->desc[0].wDescriptorLength); | |
225 | ||
226 | /* Send the ack */ | |
227 | memset(&ack, 0, sizeof(struct mousevsc_prt_msg)); | |
228 | ||
229 | ack.type = PIPE_MESSAGE_DATA; | |
230 | ack.size = sizeof(struct synthhid_device_info_ack); | |
231 | ||
232 | ack.ack.header.type = SYNTH_HID_INITIAL_DEVICE_INFO_ACK; | |
233 | ack.ack.header.size = 1; | |
234 | ack.ack.reserved = 0; | |
235 | ||
236 | ret = vmbus_sendpacket(input_device->device->channel, | |
237 | &ack, | |
238 | sizeof(struct pipe_prt_msg) - sizeof(unsigned char) + | |
239 | sizeof(struct synthhid_device_info_ack), | |
240 | (unsigned long)&ack, | |
241 | VM_PKT_DATA_INBAND, | |
242 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
243 | ||
244 | if (!ret) | |
245 | input_device->dev_info_status = 0; | |
246 | ||
247 | cleanup: | |
248 | complete(&input_device->wait_event); | |
249 | ||
250 | return; | |
251 | } | |
252 | ||
253 | static void mousevsc_on_receive(struct hv_device *device, | |
254 | struct vmpacket_descriptor *packet) | |
255 | { | |
256 | struct pipe_prt_msg *pipe_msg; | |
257 | struct synthhid_msg *hid_msg; | |
258 | struct mousevsc_dev *input_dev = hv_get_drvdata(device); | |
259 | struct synthhid_input_report *input_report; | |
3ccfd0a8 | 260 | size_t len; |
b95f5bcb S |
261 | |
262 | pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet + | |
263 | (packet->offset8 << 3)); | |
264 | ||
265 | if (pipe_msg->type != PIPE_MESSAGE_DATA) | |
266 | return; | |
267 | ||
268 | hid_msg = (struct synthhid_msg *)pipe_msg->data; | |
269 | ||
270 | switch (hid_msg->header.type) { | |
271 | case SYNTH_HID_PROTOCOL_RESPONSE: | |
272 | /* | |
273 | * While it will be impossible for us to protect against | |
274 | * malicious/buggy hypervisor/host, add a check here to | |
275 | * ensure we don't corrupt memory. | |
276 | */ | |
277 | if ((pipe_msg->size + sizeof(struct pipe_prt_msg) | |
278 | - sizeof(unsigned char)) | |
279 | > sizeof(struct mousevsc_prt_msg)) { | |
280 | WARN_ON(1); | |
281 | break; | |
282 | } | |
283 | ||
284 | memcpy(&input_dev->protocol_resp, pipe_msg, | |
285 | pipe_msg->size + sizeof(struct pipe_prt_msg) - | |
286 | sizeof(unsigned char)); | |
287 | complete(&input_dev->wait_event); | |
288 | break; | |
289 | ||
290 | case SYNTH_HID_INITIAL_DEVICE_INFO: | |
291 | WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info)); | |
292 | ||
293 | /* | |
294 | * Parse out the device info into device attr, | |
295 | * hid desc and report desc | |
296 | */ | |
297 | mousevsc_on_receive_device_info(input_dev, | |
298 | (struct synthhid_device_info *)pipe_msg->data); | |
299 | break; | |
300 | case SYNTH_HID_INPUT_REPORT: | |
301 | input_report = | |
302 | (struct synthhid_input_report *)pipe_msg->data; | |
303 | if (!input_dev->init_complete) | |
304 | break; | |
3ccfd0a8 DH |
305 | |
306 | len = min(input_report->header.size, | |
307 | (u32)sizeof(input_dev->input_buf)); | |
308 | memcpy(input_dev->input_buf, input_report->buffer, len); | |
309 | hid_input_report(input_dev->hid_device, HID_INPUT_REPORT, | |
310 | input_dev->input_buf, len, 1); | |
f1210455 | 311 | |
e0f1f596 | 312 | pm_wakeup_hard_event(&input_dev->device->device); |
f1210455 | 313 | |
b95f5bcb S |
314 | break; |
315 | default: | |
af423779 | 316 | pr_err("unsupported hid msg type - type %d len %d\n", |
b95f5bcb S |
317 | hid_msg->header.type, hid_msg->header.size); |
318 | break; | |
319 | } | |
320 | ||
321 | } | |
322 | ||
323 | static void mousevsc_on_channel_callback(void *context) | |
324 | { | |
b95f5bcb | 325 | struct hv_device *device = context; |
b95f5bcb | 326 | struct vmpacket_descriptor *desc; |
b95f5bcb | 327 | |
b985288c DC |
328 | foreach_vmbus_pkt(desc, device->channel) { |
329 | switch (desc->type) { | |
330 | case VM_PKT_COMP: | |
b95f5bcb S |
331 | break; |
332 | ||
b985288c DC |
333 | case VM_PKT_DATA_INBAND: |
334 | mousevsc_on_receive(device, desc); | |
335 | break; | |
b95f5bcb | 336 | |
b985288c DC |
337 | default: |
338 | pr_err("Unhandled packet type %d, tid %llx len %d\n", | |
339 | desc->type, desc->trans_id, desc->len8 * 8); | |
b95f5bcb S |
340 | break; |
341 | } | |
b985288c | 342 | } |
b95f5bcb S |
343 | } |
344 | ||
345 | static int mousevsc_connect_to_vsp(struct hv_device *device) | |
346 | { | |
347 | int ret = 0; | |
3458e4c0 | 348 | unsigned long t; |
b95f5bcb S |
349 | struct mousevsc_dev *input_dev = hv_get_drvdata(device); |
350 | struct mousevsc_prt_msg *request; | |
351 | struct mousevsc_prt_msg *response; | |
352 | ||
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 | ||
399 | cleanup: | |
400 | return ret; | |
401 | } | |
402 | ||
07d9ab4f HR |
403 | static 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 |
412 | static int mousevsc_hid_open(struct hid_device *hid) |
413 | { | |
414 | return 0; | |
415 | } | |
416 | ||
417 | static int mousevsc_hid_start(struct hid_device *hid) | |
418 | { | |
419 | return 0; | |
420 | } | |
421 | ||
422 | static void mousevsc_hid_close(struct hid_device *hid) | |
423 | { | |
424 | } | |
425 | ||
426 | static void mousevsc_hid_stop(struct hid_device *hid) | |
427 | { | |
428 | } | |
429 | ||
269ddfc6 S |
430 | static 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 | 439 | static 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 | ||
448 | static struct hid_driver mousevsc_hid_driver; | |
449 | ||
450 | static 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 | ||
526 | probe_err2: | |
527 | hid_destroy_device(hid_dev); | |
528 | ||
529 | probe_err1: | |
530 | vmbus_close(device->channel); | |
531 | ||
532 | probe_err0: | |
533 | mousevsc_free_device(input_dev); | |
534 | ||
535 | return ret; | |
536 | } | |
537 | ||
538 | ||
539 | static 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 | ||
552 | static const struct hv_vmbus_device_id id_table[] = { | |
553 | /* Mouse guid */ | |
048c5add | 554 | { HV_MOUSE_GUID, }, |
b95f5bcb S |
555 | { }, |
556 | }; | |
557 | ||
558 | MODULE_DEVICE_TABLE(vmbus, id_table); | |
559 | ||
560 | static struct hv_driver mousevsc_drv = { | |
561 | .name = KBUILD_MODNAME, | |
562 | .id_table = id_table, | |
563 | .probe = mousevsc_probe, | |
564 | .remove = mousevsc_remove, | |
565 | }; | |
566 | ||
567 | static int __init mousevsc_init(void) | |
568 | { | |
569 | return vmbus_driver_register(&mousevsc_drv); | |
570 | } | |
571 | ||
572 | static void __exit mousevsc_exit(void) | |
573 | { | |
574 | vmbus_driver_unregister(&mousevsc_drv); | |
575 | } | |
576 | ||
577 | MODULE_LICENSE("GPL"); | |
b95f5bcb S |
578 | module_init(mousevsc_init); |
579 | module_exit(mousevsc_exit); |