2 * Copyright (c) 2009, Citrix Systems, Inc.
3 * Copyright (c) 2010, Microsoft Corporation.
4 * Copyright (c) 2011, Novell Inc.
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.
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
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/input.h>
23 #include <linux/hid.h>
24 #include <linux/hiddev.h>
25 #include <linux/pci.h>
26 #include <linux/dmi.h>
34 struct hv_input_dev_info {
35 unsigned short vendor;
36 unsigned short product;
37 unsigned short version;
41 /* The maximum size of a synthetic input message. */
42 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
48 * Beta, RC < 2008/1/22 1,0
51 #define SYNTHHID_INPUT_VERSION_MAJOR 2
52 #define SYNTHHID_INPUT_VERSION_MINOR 0
53 #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
54 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
59 * Message types in the synthetic input protocol
61 enum synthhid_msg_type {
62 SynthHidProtocolRequest,
63 SynthHidProtocolResponse,
64 SynthHidInitialDeviceInfo,
65 SynthHidInitialDeviceInfoAck,
71 * Basic message structures.
73 struct synthhid_msg_hdr {
74 enum synthhid_msg_type type;
79 struct synthhid_msg_hdr header;
80 char data[1]; /* Enclosed message */
83 union synthhid_version {
94 struct synthhid_protocol_request {
95 struct synthhid_msg_hdr header;
96 union synthhid_version version_requested;
99 struct synthhid_protocol_response {
100 struct synthhid_msg_hdr header;
101 union synthhid_version version_requested;
102 unsigned char approved;
105 struct synthhid_device_info {
106 struct synthhid_msg_hdr header;
107 struct hv_input_dev_info hid_dev_info;
108 struct hid_descriptor hid_descriptor;
111 struct synthhid_device_info_ack {
112 struct synthhid_msg_hdr header;
113 unsigned char reserved;
116 struct synthhid_input_report {
117 struct synthhid_msg_hdr header;
123 #define INPUTVSC_SEND_RING_BUFFER_SIZE 10*PAGE_SIZE
124 #define INPUTVSC_RECV_RING_BUFFER_SIZE 10*PAGE_SIZE
126 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
128 enum pipe_prot_msg_type {
129 PipeMessageInvalid = 0,
135 struct pipe_prt_msg {
136 enum pipe_prot_msg_type type;
144 struct mousevsc_prt_msg {
145 enum pipe_prot_msg_type type;
148 struct synthhid_protocol_request request;
149 struct synthhid_protocol_response response;
150 struct synthhid_device_info_ack ack;
155 * Represents an mousevsc device
157 struct mousevsc_dev {
158 struct hv_device *device;
159 /* 0 indicates the device is being destroyed */
161 int num_outstanding_req;
162 unsigned char init_complete;
163 struct mousevsc_prt_msg protocol_req;
164 struct mousevsc_prt_msg protocol_resp;
165 /* Synchronize the request/response if needed */
166 wait_queue_head_t protocol_wait_event;
167 wait_queue_head_t dev_info_wait_event;
168 int protocol_wait_condition;
169 int device_wait_condition;
172 struct hid_descriptor *hid_desc;
173 unsigned char *report_desc;
174 u32 report_desc_size;
175 struct hv_input_dev_info hid_dev_info;
179 static const char *driver_name = "mousevsc";
181 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
182 static const struct hv_guid mouse_guid = {
183 .data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
184 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}
187 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info);
188 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len);
189 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len);
191 static struct mousevsc_dev *alloc_input_device(struct hv_device *device)
193 struct mousevsc_dev *input_dev;
195 input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
201 * Set to 2 to allow both inbound and outbound traffics
202 * (ie get_input_device() and must_get_input_device()) to proceed.
204 atomic_cmpxchg(&input_dev->ref_count, 0, 2);
206 input_dev->device = device;
207 device->ext = input_dev;
212 static void free_input_device(struct mousevsc_dev *device)
214 WARN_ON(atomic_read(&device->ref_count) == 0);
219 * Get the inputdevice object if exists and its refcount > 1
221 static struct mousevsc_dev *get_input_device(struct hv_device *device)
223 struct mousevsc_dev *input_dev;
225 input_dev = (struct mousevsc_dev *)device->ext;
229 * This sure isn't a valid thing to print for debugging, no matter
230 * what the intention is...
232 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
233 * input_dev->ref_count);
236 if (input_dev && atomic_read(&input_dev->ref_count) > 1)
237 atomic_inc(&input_dev->ref_count);
245 * Get the inputdevice object iff exists and its refcount > 0
247 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
249 struct mousevsc_dev *input_dev;
251 input_dev = (struct mousevsc_dev *)device->ext;
253 if (input_dev && atomic_read(&input_dev->ref_count))
254 atomic_inc(&input_dev->ref_count);
261 static void put_input_device(struct hv_device *device)
263 struct mousevsc_dev *input_dev;
265 input_dev = (struct mousevsc_dev *)device->ext;
267 atomic_dec(&input_dev->ref_count);
271 * Drop ref count to 1 to effectively disable get_input_device()
273 static struct mousevsc_dev *release_input_device(struct hv_device *device)
275 struct mousevsc_dev *input_dev;
277 input_dev = (struct mousevsc_dev *)device->ext;
279 /* Busy wait until the ref drop to 2, then set it to 1 */
280 while (atomic_cmpxchg(&input_dev->ref_count, 2, 1) != 2)
287 * Drop ref count to 0. No one can use input_device object.
289 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
291 struct mousevsc_dev *input_dev;
293 input_dev = (struct mousevsc_dev *)device->ext;
295 /* Busy wait until the ref drop to 1, then set it to 0 */
296 while (atomic_cmpxchg(&input_dev->ref_count, 1, 0) != 1)
303 static void mousevsc_on_send_completion(struct hv_device *device,
304 struct vmpacket_descriptor *packet)
306 struct mousevsc_dev *input_dev;
309 input_dev = must_get_input_device(device);
311 pr_err("unable to get input device...device being destroyed?");
315 request = (void *)(unsigned long)packet->trans_id;
317 if (request == &input_dev->protocol_req) {
319 /* Shouldn't we be doing something here? */
322 put_input_device(device);
325 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
326 struct synthhid_device_info *device_info)
329 struct hid_descriptor *desc;
330 struct mousevsc_prt_msg ack;
332 /* Assume success for now */
333 input_device->dev_info_status = 0;
335 /* Save the device attr */
336 memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
337 sizeof(struct hv_input_dev_info));
339 /* Save the hid desc */
340 desc = &device_info->hid_descriptor;
341 WARN_ON(desc->bLength > 0);
343 input_device->hid_desc = kzalloc(desc->bLength, GFP_KERNEL);
345 if (!input_device->hid_desc) {
346 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
350 memcpy(input_device->hid_desc, desc, desc->bLength);
352 /* Save the report desc */
353 input_device->report_desc_size = desc->desc[0].wDescriptorLength;
354 input_device->report_desc = kzalloc(input_device->report_desc_size,
357 if (!input_device->report_desc) {
358 pr_err("unable to allocate report descriptor - size %d",
359 input_device->report_desc_size);
363 memcpy(input_device->report_desc,
364 ((unsigned char *)desc) + desc->bLength,
365 desc->desc[0].wDescriptorLength);
368 memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
370 ack.type = PipeMessageData;
371 ack.size = sizeof(struct synthhid_device_info_ack);
373 ack.ack.header.type = SynthHidInitialDeviceInfoAck;
374 ack.ack.header.size = 1;
375 ack.ack.reserved = 0;
377 ret = vmbus_sendpacket(input_device->device->channel,
379 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
380 sizeof(struct synthhid_device_info_ack),
383 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
385 pr_err("unable to send synthhid device info ack - ret %d",
390 input_device->device_wait_condition = 1;
391 wake_up(&input_device->dev_info_wait_event);
396 kfree(input_device->hid_desc);
397 input_device->hid_desc = NULL;
399 kfree(input_device->report_desc);
400 input_device->report_desc = NULL;
402 input_device->dev_info_status = -1;
403 input_device->device_wait_condition = 1;
404 wake_up(&input_device->dev_info_wait_event);
407 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
408 struct synthhid_input_report *input_report)
410 struct hv_driver *input_drv;
412 if (!input_device->init_complete) {
413 pr_info("Initialization incomplete...ignoring input_report msg");
417 input_drv = drv_to_hv_drv(input_device->device->device.driver);
419 inputreport_callback(input_device->device,
420 input_report->buffer,
421 input_report->header.size);
424 static void mousevsc_on_receive(struct hv_device *device,
425 struct vmpacket_descriptor *packet)
427 struct pipe_prt_msg *pipe_msg;
428 struct synthhid_msg *hid_msg;
429 struct mousevsc_dev *input_dev;
431 input_dev = must_get_input_device(device);
433 pr_err("unable to get input device...device being destroyed?");
437 pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
438 (packet->offset8 << 3));
440 if (pipe_msg->type != PipeMessageData) {
441 pr_err("unknown pipe msg type - type %d len %d",
442 pipe_msg->type, pipe_msg->size);
443 put_input_device(device);
447 hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
449 switch (hid_msg->header.type) {
450 case SynthHidProtocolResponse:
451 memcpy(&input_dev->protocol_resp, pipe_msg,
452 pipe_msg->size + sizeof(struct pipe_prt_msg) -
453 sizeof(unsigned char));
454 input_dev->protocol_wait_condition = 1;
455 wake_up(&input_dev->protocol_wait_event);
458 case SynthHidInitialDeviceInfo:
459 WARN_ON(pipe_msg->size >= sizeof(struct hv_input_dev_info));
462 * Parse out the device info into device attr,
463 * hid desc and report desc
465 mousevsc_on_receive_device_info(input_dev,
466 (struct synthhid_device_info *)&pipe_msg->data[0]);
468 case SynthHidInputReport:
469 mousevsc_on_receive_input_report(input_dev,
470 (struct synthhid_input_report *)&pipe_msg->data[0]);
474 pr_err("unsupported hid msg type - type %d len %d",
475 hid_msg->header.type, hid_msg->header.size);
479 put_input_device(device);
482 static void mousevsc_on_channel_callback(void *context)
484 const int packetSize = 0x100;
486 struct hv_device *device = (struct hv_device *)context;
487 struct mousevsc_dev *input_dev;
491 unsigned char packet[0x100];
492 struct vmpacket_descriptor *desc;
493 unsigned char *buffer = packet;
494 int bufferlen = packetSize;
496 input_dev = must_get_input_device(device);
499 pr_err("unable to get input device...device being destroyed?");
504 ret = vmbus_recvpacket_raw(device->channel, buffer,
505 bufferlen, &bytes_recvd, &req_id);
508 if (bytes_recvd > 0) {
509 desc = (struct vmpacket_descriptor *)buffer;
511 switch (desc->type) {
513 mousevsc_on_send_completion(
517 case VM_PKT_DATA_INBAND:
523 pr_err("unhandled packet type %d, tid %llx len %d\n",
531 if (bufferlen > packetSize) {
535 bufferlen = packetSize;
539 * pr_debug("nothing else to read...");
542 if (bufferlen > packetSize) {
546 bufferlen = packetSize;
550 } else if (ret == -2) {
551 /* Handle large packet */
552 bufferlen = bytes_recvd;
553 buffer = kzalloc(bytes_recvd, GFP_KERNEL);
555 if (buffer == NULL) {
557 bufferlen = packetSize;
559 /* Try again next time around */
560 pr_err("unable to allocate buffer of size %d!",
567 put_input_device(device);
572 static int mousevsc_connect_to_vsp(struct hv_device *device)
575 struct mousevsc_dev *input_dev;
576 struct mousevsc_prt_msg *request;
577 struct mousevsc_prt_msg *response;
579 input_dev = get_input_device(device);
582 pr_err("unable to get input device...device being destroyed?");
586 init_waitqueue_head(&input_dev->protocol_wait_event);
587 init_waitqueue_head(&input_dev->dev_info_wait_event);
589 request = &input_dev->protocol_req;
592 * Now, initiate the vsc/vsp initialization protocol on the open channel
594 memset(request, 0, sizeof(struct mousevsc_prt_msg));
596 request->type = PipeMessageData;
597 request->size = sizeof(struct synthhid_protocol_request);
599 request->request.header.type = SynthHidProtocolRequest;
600 request->request.header.size = sizeof(unsigned long);
601 request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
603 pr_info("synthhid protocol request...");
605 ret = vmbus_sendpacket(device->channel, request,
606 sizeof(struct pipe_prt_msg) -
607 sizeof(unsigned char) +
608 sizeof(struct synthhid_protocol_request),
609 (unsigned long)request,
611 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
613 pr_err("unable to send synthhid protocol request.");
617 input_dev->protocol_wait_condition = 0;
618 wait_event_timeout(input_dev->protocol_wait_event,
619 input_dev->protocol_wait_condition, msecs_to_jiffies(1000));
620 if (input_dev->protocol_wait_condition == 0) {
625 response = &input_dev->protocol_resp;
627 if (!response->response.approved) {
628 pr_err("synthhid protocol request failed (version %d)",
629 SYNTHHID_INPUT_VERSION);
634 input_dev->device_wait_condition = 0;
635 wait_event_timeout(input_dev->dev_info_wait_event,
636 input_dev->device_wait_condition, msecs_to_jiffies(1000));
637 if (input_dev->device_wait_condition == 0) {
643 * We should have gotten the device attr, hid desc and report
646 if (!input_dev->dev_info_status)
647 pr_info("**** input channel up and running!! ****");
652 put_input_device(device);
657 static int mousevsc_on_device_add(struct hv_device *device,
658 void *additional_info)
661 struct mousevsc_dev *input_dev;
662 struct hv_driver *input_drv;
663 struct hv_input_dev_info dev_info;
665 input_dev = alloc_input_device(device);
672 input_dev->init_complete = false;
674 /* Open the channel */
675 ret = vmbus_open(device->channel,
676 INPUTVSC_SEND_RING_BUFFER_SIZE,
677 INPUTVSC_RECV_RING_BUFFER_SIZE,
680 mousevsc_on_channel_callback,
685 pr_err("unable to open channel: %d", ret);
686 free_input_device(input_dev);
690 pr_info("InputVsc channel open: %d", ret);
692 ret = mousevsc_connect_to_vsp(device);
695 pr_err("unable to connect channel: %d", ret);
697 vmbus_close(device->channel);
698 free_input_device(input_dev);
702 input_drv = drv_to_hv_drv(input_dev->device->device.driver);
704 dev_info.vendor = input_dev->hid_dev_info.vendor;
705 dev_info.product = input_dev->hid_dev_info.product;
706 dev_info.version = input_dev->hid_dev_info.version;
707 strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
709 /* Send the device info back up */
710 deviceinfo_callback(device, &dev_info);
712 /* Send the report desc back up */
713 /* workaround SA-167 */
714 if (input_dev->report_desc[14] == 0x25)
715 input_dev->report_desc[14] = 0x29;
717 reportdesc_callback(device, input_dev->report_desc,
718 input_dev->report_desc_size);
720 input_dev->init_complete = true;
726 static int mousevsc_on_device_remove(struct hv_device *device)
728 struct mousevsc_dev *input_dev;
731 pr_info("disabling input device (%p)...",
734 input_dev = release_input_device(device);
738 * At this point, all outbound traffic should be disable. We only
739 * allow inbound traffic (responses) to proceed
741 * so that outstanding requests can be completed.
743 while (input_dev->num_outstanding_req) {
744 pr_info("waiting for %d requests to complete...",
745 input_dev->num_outstanding_req);
750 pr_info("removing input device (%p)...", device->ext);
752 input_dev = final_release_input_device(device);
754 pr_info("input device (%p) safe to remove", input_dev);
756 /* Close the channel */
757 vmbus_close(device->channel);
759 free_input_device(input_dev);
768 struct input_device_context {
769 struct hv_device *device_ctx;
770 struct hid_device *hid_device;
771 struct hv_input_dev_info device_info;
776 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
778 struct input_device_context *input_device_ctx =
779 dev_get_drvdata(&dev->device);
781 memcpy(&input_device_ctx->device_info, info,
782 sizeof(struct hv_input_dev_info));
784 DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
787 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
791 struct input_device_context *input_dev_ctx =
792 dev_get_drvdata(&dev->device);
794 ret = hid_input_report(input_dev_ctx->hid_device,
795 HID_INPUT_REPORT, packet, len, 1);
797 DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
800 static int mousevsc_hid_open(struct hid_device *hid)
805 static void mousevsc_hid_close(struct hid_device *hid)
809 static int mousevsc_probe(struct hv_device *dev)
813 struct input_device_context *input_dev_ctx;
815 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
818 dev_set_drvdata(&dev->device, input_dev_ctx);
820 /* Call to the vsc driver to add the device */
821 ret = mousevsc_on_device_add(dev, NULL);
824 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
832 static int mousevsc_remove(struct hv_device *dev)
836 struct input_device_context *input_dev_ctx;
838 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
841 dev_set_drvdata(&dev->device, input_dev_ctx);
843 if (input_dev_ctx->connected) {
844 hidinput_disconnect(input_dev_ctx->hid_device);
845 input_dev_ctx->connected = 0;
849 * Call to the vsc driver to let it know that the device
852 ret = mousevsc_on_device_remove(dev);
855 DPRINT_ERR(INPUTVSC_DRV,
856 "unable to remove vsc device (ret %d)", ret);
859 kfree(input_dev_ctx);
864 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
866 struct input_device_context *input_device_ctx =
867 dev_get_drvdata(&dev->device);
868 struct hid_device *hid_dev;
870 /* hid_debug = -1; */
871 hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
873 if (hid_parse_report(hid_dev, packet, len)) {
874 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
879 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
881 hid_dev->ll_driver->open = mousevsc_hid_open;
882 hid_dev->ll_driver->close = mousevsc_hid_close;
884 hid_dev->bus = BUS_VIRTUAL;
885 hid_dev->vendor = input_device_ctx->device_info.vendor;
886 hid_dev->product = input_device_ctx->device_info.product;
887 hid_dev->version = input_device_ctx->device_info.version;
888 hid_dev->dev = dev->device;
890 sprintf(hid_dev->name, "%s",
891 input_device_ctx->device_info.name);
894 * HJ Do we want to call it with a 0
896 if (!hidinput_connect(hid_dev, 0)) {
897 hid_dev->claimed |= HID_CLAIMED_INPUT;
899 input_device_ctx->connected = 1;
901 DPRINT_INFO(INPUTVSC_DRV,
902 "HID device claimed by input\n");
905 if (!hid_dev->claimed) {
906 DPRINT_ERR(INPUTVSC_DRV,
907 "HID device not claimed by "
908 "input or hiddev\n");
911 input_device_ctx->hid_device = hid_dev;
918 static struct hv_driver mousevsc_drv = {
919 .probe = mousevsc_probe,
920 .remove = mousevsc_remove,
923 static void mousevsc_drv_exit(void)
925 vmbus_child_driver_unregister(&mousevsc_drv.driver);
928 static int __init mousevsc_init(void)
930 struct hv_driver *drv = &mousevsc_drv;
932 DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
934 memcpy(&drv->dev_type, &mouse_guid,
935 sizeof(struct hv_guid));
937 drv->driver.name = driver_name;
939 /* The driver belongs to vmbus */
940 vmbus_child_driver_register(&drv->driver);
945 static void __exit mousevsc_exit(void)
951 * We don't want to automatically load this driver just yet, it's quite
952 * broken. It's safe if you want to load it yourself manually, but
953 * don't inflict it on unsuspecting users, that's just mean.
958 * We use a PCI table to determine if we should autoload this driver This is
959 * needed by distro tools to determine if the hyperv drivers should be
960 * installed and/or configured. We don't do anything else with the table, but
961 * it needs to be present.
963 const static struct pci_device_id microsoft_hv_pci_table[] = {
964 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
967 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
970 MODULE_LICENSE("GPL");
971 MODULE_VERSION(HV_DRV_VERSION);
972 module_init(mousevsc_init);
973 module_exit(mousevsc_exit);