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