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>
32 struct hv_input_dev_info {
34 unsigned short vendor;
35 unsigned short product;
36 unsigned short version;
37 unsigned short reserved[11];
40 /* The maximum size of a synthetic input message. */
41 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
47 * Beta, RC < 2008/1/22 1,0
50 #define SYNTHHID_INPUT_VERSION_MAJOR 2
51 #define SYNTHHID_INPUT_VERSION_MINOR 0
52 #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
53 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
58 * Message types in the synthetic input protocol
60 enum synthhid_msg_type {
61 SynthHidProtocolRequest,
62 SynthHidProtocolResponse,
63 SynthHidInitialDeviceInfo,
64 SynthHidInitialDeviceInfoAck,
70 * Basic message structures.
72 struct synthhid_msg_hdr {
73 enum synthhid_msg_type type;
78 struct synthhid_msg_hdr header;
79 char data[1]; /* Enclosed message */
82 union synthhid_version {
93 struct synthhid_protocol_request {
94 struct synthhid_msg_hdr header;
95 union synthhid_version version_requested;
98 struct synthhid_protocol_response {
99 struct synthhid_msg_hdr header;
100 union synthhid_version version_requested;
101 unsigned char approved;
104 struct synthhid_device_info {
105 struct synthhid_msg_hdr header;
106 struct hv_input_dev_info hid_dev_info;
107 struct hid_descriptor hid_descriptor;
110 struct synthhid_device_info_ack {
111 struct synthhid_msg_hdr header;
112 unsigned char reserved;
115 struct synthhid_input_report {
116 struct synthhid_msg_hdr header;
122 #define INPUTVSC_SEND_RING_BUFFER_SIZE (10*PAGE_SIZE)
123 #define INPUTVSC_RECV_RING_BUFFER_SIZE (10*PAGE_SIZE)
125 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
127 enum pipe_prot_msg_type {
128 PipeMessageInvalid = 0,
134 struct pipe_prt_msg {
135 enum pipe_prot_msg_type type;
143 struct mousevsc_prt_msg {
144 enum pipe_prot_msg_type type;
147 struct synthhid_protocol_request request;
148 struct synthhid_protocol_response response;
149 struct synthhid_device_info_ack ack;
154 * Represents an mousevsc device
156 struct mousevsc_dev {
157 struct hv_device *device;
158 /* 0 indicates the device is being destroyed */
160 int num_outstanding_req;
161 unsigned char init_complete;
162 struct mousevsc_prt_msg protocol_req;
163 struct mousevsc_prt_msg protocol_resp;
164 /* Synchronize the request/response if needed */
165 wait_queue_head_t protocol_wait_event;
166 wait_queue_head_t dev_info_wait_event;
167 int protocol_wait_condition;
168 int device_wait_condition;
171 struct hid_descriptor *hid_desc;
172 unsigned char *report_desc;
173 u32 report_desc_size;
174 struct hv_input_dev_info hid_dev_info;
176 struct hid_device *hid_device;
180 static struct mousevsc_dev *alloc_input_device(struct hv_device *device)
182 struct mousevsc_dev *input_dev;
184 input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
190 * Set to 2 to allow both inbound and outbound traffics
191 * (ie get_input_device() and must_get_input_device()) to proceed.
193 atomic_cmpxchg(&input_dev->ref_count, 0, 2);
195 input_dev->device = device;
196 hv_set_drvdata(device, input_dev);
201 static void free_input_device(struct mousevsc_dev *device)
203 WARN_ON(atomic_read(&device->ref_count) != 0);
208 * Get the inputdevice object if exists and its refcount > 1
210 static struct mousevsc_dev *get_input_device(struct hv_device *device)
212 struct mousevsc_dev *input_dev;
214 input_dev = hv_get_drvdata(device);
218 * This sure isn't a valid thing to print for debugging, no matter
219 * what the intention is...
221 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
222 * input_dev->ref_count);
225 if (input_dev && atomic_read(&input_dev->ref_count) > 1)
226 atomic_inc(&input_dev->ref_count);
234 * Get the inputdevice object iff exists and its refcount > 0
236 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
238 struct mousevsc_dev *input_dev;
240 input_dev = hv_get_drvdata(device);
242 if (input_dev && atomic_read(&input_dev->ref_count))
243 atomic_inc(&input_dev->ref_count);
250 static void put_input_device(struct hv_device *device)
252 struct mousevsc_dev *input_dev;
254 input_dev = hv_get_drvdata(device);
256 atomic_dec(&input_dev->ref_count);
260 * Drop ref count to 1 to effectively disable get_input_device()
262 static struct mousevsc_dev *release_input_device(struct hv_device *device)
264 struct mousevsc_dev *input_dev;
266 input_dev = hv_get_drvdata(device);
268 /* Busy wait until the ref drop to 2, then set it to 1 */
269 while (atomic_cmpxchg(&input_dev->ref_count, 2, 1) != 2)
276 * Drop ref count to 0. No one can use input_device object.
278 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
280 struct mousevsc_dev *input_dev;
282 input_dev = hv_get_drvdata(device);
284 /* Busy wait until the ref drop to 1, then set it to 0 */
285 while (atomic_cmpxchg(&input_dev->ref_count, 1, 0) != 1)
288 hv_set_drvdata(device, NULL);
292 static void mousevsc_on_send_completion(struct hv_device *device,
293 struct vmpacket_descriptor *packet)
295 struct mousevsc_dev *input_dev;
298 input_dev = must_get_input_device(device);
300 pr_err("unable to get input device...device being destroyed?");
304 request = (void *)(unsigned long)packet->trans_id;
306 if (request == &input_dev->protocol_req) {
308 /* Shouldn't we be doing something here? */
311 put_input_device(device);
314 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
315 struct synthhid_device_info *device_info)
318 struct hid_descriptor *desc;
319 struct mousevsc_prt_msg ack;
321 /* Assume success for now */
322 input_device->dev_info_status = 0;
324 /* Save the device attr */
325 memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
326 sizeof(struct hv_input_dev_info));
328 /* Save the hid desc */
329 desc = &device_info->hid_descriptor;
330 WARN_ON(desc->bLength == 0);
332 input_device->hid_desc = kzalloc(desc->bLength, GFP_ATOMIC);
334 if (!input_device->hid_desc) {
335 pr_err("unable to allocate hid descriptor - size %d",
340 memcpy(input_device->hid_desc, desc, desc->bLength);
342 /* Save the report desc */
343 input_device->report_desc_size = desc->desc[0].wDescriptorLength;
344 if (input_device->report_desc_size == 0)
346 input_device->report_desc = kzalloc(input_device->report_desc_size,
349 if (!input_device->report_desc) {
350 pr_err("unable to allocate report descriptor - size %d",
351 input_device->report_desc_size);
355 memcpy(input_device->report_desc,
356 ((unsigned char *)desc) + desc->bLength,
357 desc->desc[0].wDescriptorLength);
360 memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
362 ack.type = PipeMessageData;
363 ack.size = sizeof(struct synthhid_device_info_ack);
365 ack.ack.header.type = SynthHidInitialDeviceInfoAck;
366 ack.ack.header.size = 1;
367 ack.ack.reserved = 0;
369 ret = vmbus_sendpacket(input_device->device->channel,
371 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
372 sizeof(struct synthhid_device_info_ack),
375 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
377 pr_err("unable to send synthhid device info ack - ret %d",
382 input_device->device_wait_condition = 1;
383 wake_up(&input_device->dev_info_wait_event);
388 kfree(input_device->hid_desc);
389 input_device->hid_desc = NULL;
391 kfree(input_device->report_desc);
392 input_device->report_desc = NULL;
394 input_device->dev_info_status = -1;
395 input_device->device_wait_condition = 1;
396 wake_up(&input_device->dev_info_wait_event);
399 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
400 struct synthhid_input_report *input_report)
402 struct hv_driver *input_drv;
404 if (!input_device->init_complete) {
405 pr_info("Initialization incomplete...ignoring input_report msg");
409 input_drv = drv_to_hv_drv(input_device->device->device.driver);
412 hid_input_report(input_device->hid_device,
413 HID_INPUT_REPORT, input_report->buffer, input_report->header.size, 1);
417 static void mousevsc_on_receive(struct hv_device *device,
418 struct vmpacket_descriptor *packet)
420 struct pipe_prt_msg *pipe_msg;
421 struct synthhid_msg *hid_msg;
422 struct mousevsc_dev *input_dev;
424 input_dev = must_get_input_device(device);
426 pr_err("unable to get input device...device being destroyed?");
430 pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
431 (packet->offset8 << 3));
433 if (pipe_msg->type != PipeMessageData) {
434 pr_err("unknown pipe msg type - type %d len %d",
435 pipe_msg->type, pipe_msg->size);
436 put_input_device(device);
440 hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
442 switch (hid_msg->header.type) {
443 case SynthHidProtocolResponse:
444 memcpy(&input_dev->protocol_resp, pipe_msg,
445 pipe_msg->size + sizeof(struct pipe_prt_msg) -
446 sizeof(unsigned char));
447 input_dev->protocol_wait_condition = 1;
448 wake_up(&input_dev->protocol_wait_event);
451 case SynthHidInitialDeviceInfo:
452 WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info));
455 * Parse out the device info into device attr,
456 * hid desc and report desc
458 mousevsc_on_receive_device_info(input_dev,
459 (struct synthhid_device_info *)&pipe_msg->data[0]);
461 case SynthHidInputReport:
462 mousevsc_on_receive_input_report(input_dev,
463 (struct synthhid_input_report *)&pipe_msg->data[0]);
467 pr_err("unsupported hid msg type - type %d len %d",
468 hid_msg->header.type, hid_msg->header.size);
472 put_input_device(device);
475 static void mousevsc_on_channel_callback(void *context)
477 const int packetSize = 0x100;
479 struct hv_device *device = (struct hv_device *)context;
480 struct mousevsc_dev *input_dev;
484 unsigned char packet[0x100];
485 struct vmpacket_descriptor *desc;
486 unsigned char *buffer = packet;
487 int bufferlen = packetSize;
489 input_dev = must_get_input_device(device);
492 pr_err("unable to get input device...device being destroyed?");
497 ret = vmbus_recvpacket_raw(device->channel, buffer,
498 bufferlen, &bytes_recvd, &req_id);
501 if (bytes_recvd > 0) {
502 desc = (struct vmpacket_descriptor *)buffer;
504 switch (desc->type) {
506 mousevsc_on_send_completion(
510 case VM_PKT_DATA_INBAND:
516 pr_err("unhandled packet type %d, tid %llx len %d\n",
524 if (bufferlen > packetSize) {
528 bufferlen = packetSize;
532 * pr_debug("nothing else to read...");
535 if (bufferlen > packetSize) {
539 bufferlen = packetSize;
543 } else if (ret == -ENOBUFS) {
544 /* Handle large packet */
545 bufferlen = bytes_recvd;
546 buffer = kzalloc(bytes_recvd, GFP_ATOMIC);
548 if (buffer == NULL) {
550 bufferlen = packetSize;
552 /* Try again next time around */
553 pr_err("unable to allocate buffer of size %d!",
560 put_input_device(device);
565 static int mousevsc_connect_to_vsp(struct hv_device *device)
568 struct mousevsc_dev *input_dev;
569 struct mousevsc_prt_msg *request;
570 struct mousevsc_prt_msg *response;
572 input_dev = get_input_device(device);
575 pr_err("unable to get input device...device being destroyed?");
579 init_waitqueue_head(&input_dev->protocol_wait_event);
580 init_waitqueue_head(&input_dev->dev_info_wait_event);
582 request = &input_dev->protocol_req;
585 * Now, initiate the vsc/vsp initialization protocol on the open channel
587 memset(request, 0, sizeof(struct mousevsc_prt_msg));
589 request->type = PipeMessageData;
590 request->size = sizeof(struct synthhid_protocol_request);
592 request->request.header.type = SynthHidProtocolRequest;
593 request->request.header.size = sizeof(unsigned long);
594 request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
596 pr_info("synthhid protocol request...");
598 ret = vmbus_sendpacket(device->channel, request,
599 sizeof(struct pipe_prt_msg) -
600 sizeof(unsigned char) +
601 sizeof(struct synthhid_protocol_request),
602 (unsigned long)request,
604 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
606 pr_err("unable to send synthhid protocol request.");
610 input_dev->protocol_wait_condition = 0;
611 wait_event_timeout(input_dev->protocol_wait_event,
612 input_dev->protocol_wait_condition, msecs_to_jiffies(1000));
613 if (input_dev->protocol_wait_condition == 0) {
618 response = &input_dev->protocol_resp;
620 if (!response->response.approved) {
621 pr_err("synthhid protocol request failed (version %d)",
622 SYNTHHID_INPUT_VERSION);
627 input_dev->device_wait_condition = 0;
628 wait_event_timeout(input_dev->dev_info_wait_event,
629 input_dev->device_wait_condition, msecs_to_jiffies(1000));
630 if (input_dev->device_wait_condition == 0) {
636 * We should have gotten the device attr, hid desc and report
639 if (!input_dev->dev_info_status)
640 pr_info("**** input channel up and running!! ****");
645 put_input_device(device);
650 static int mousevsc_hid_open(struct hid_device *hid)
655 static void mousevsc_hid_close(struct hid_device *hid)
659 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
661 struct hid_device *hid_dev;
662 struct mousevsc_dev *input_device = hv_get_drvdata(dev);
664 /* hid_debug = -1; */
665 hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
667 if (hid_parse_report(hid_dev, packet, len)) {
668 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
673 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
675 hid_dev->ll_driver->open = mousevsc_hid_open;
676 hid_dev->ll_driver->close = mousevsc_hid_close;
678 hid_dev->bus = BUS_VIRTUAL;
679 hid_dev->vendor = input_device->hid_dev_info.vendor;
680 hid_dev->product = input_device->hid_dev_info.product;
681 hid_dev->version = input_device->hid_dev_info.version;
682 hid_dev->dev = dev->device;
684 sprintf(hid_dev->name, "%s",
685 "Microsoft Vmbus HID-compliant Mouse");
688 * HJ Do we want to call it with a 0
690 if (!hidinput_connect(hid_dev, 0)) {
691 hid_dev->claimed |= HID_CLAIMED_INPUT;
693 input_device->connected = 1;
695 DPRINT_INFO(INPUTVSC_DRV,
696 "HID device claimed by input\n");
699 if (!hid_dev->claimed) {
700 DPRINT_ERR(INPUTVSC_DRV,
701 "HID device not claimed by "
702 "input or hiddev\n");
705 input_device->hid_device = hid_dev;
711 static int mousevsc_on_device_add(struct hv_device *device,
712 void *additional_info)
715 struct mousevsc_dev *input_dev;
716 struct hv_driver *input_drv;
718 input_dev = alloc_input_device(device);
725 input_dev->init_complete = false;
727 /* Open the channel */
728 ret = vmbus_open(device->channel,
729 INPUTVSC_SEND_RING_BUFFER_SIZE,
730 INPUTVSC_RECV_RING_BUFFER_SIZE,
733 mousevsc_on_channel_callback,
738 pr_err("unable to open channel: %d", ret);
739 free_input_device(input_dev);
743 pr_info("InputVsc channel open: %d", ret);
745 ret = mousevsc_connect_to_vsp(device);
748 pr_err("unable to connect channel: %d", ret);
750 vmbus_close(device->channel);
751 free_input_device(input_dev);
755 input_drv = drv_to_hv_drv(input_dev->device->device.driver);
759 /* Send the report desc back up */
760 /* workaround SA-167 */
761 if (input_dev->report_desc[14] == 0x25)
762 input_dev->report_desc[14] = 0x29;
764 reportdesc_callback(device, input_dev->report_desc,
765 input_dev->report_desc_size);
767 input_dev->init_complete = true;
773 static int mousevsc_on_device_remove(struct hv_device *device)
775 struct mousevsc_dev *input_dev;
778 pr_info("disabling input device (%p)...",
779 hv_get_drvdata(device));
781 input_dev = release_input_device(device);
785 * At this point, all outbound traffic should be disable. We only
786 * allow inbound traffic (responses) to proceed
788 * so that outstanding requests can be completed.
790 while (input_dev->num_outstanding_req) {
791 pr_info("waiting for %d requests to complete...",
792 input_dev->num_outstanding_req);
797 pr_info("removing input device (%p)...", hv_get_drvdata(device));
799 input_dev = final_release_input_device(device);
801 pr_info("input device (%p) safe to remove", input_dev);
803 /* Close the channel */
804 vmbus_close(device->channel);
806 free_input_device(input_dev);
812 static int mousevsc_probe(struct hv_device *dev,
813 const struct hv_vmbus_device_id *dev_id)
818 /* Call to the vsc driver to add the device */
819 ret = mousevsc_on_device_add(dev, NULL);
822 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
830 static int mousevsc_remove(struct hv_device *dev)
832 struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
835 if (input_dev->connected) {
836 hidinput_disconnect(input_dev->hid_device);
837 input_dev->connected = 0;
841 * Call to the vsc driver to let it know that the device
844 ret = mousevsc_on_device_remove(dev);
846 DPRINT_ERR(INPUTVSC_DRV,
847 "unable to remove vsc device (ret %d)", ret);
853 static const struct hv_vmbus_device_id id_table[] = {
855 { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
856 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
861 * The mouse driver is not functional; do not auto-load it.
863 /* MODULE_DEVICE_TABLE(vmbus, id_table); */
865 static struct hv_driver mousevsc_drv = {
867 .id_table = id_table,
868 .probe = mousevsc_probe,
869 .remove = mousevsc_remove,
872 static int __init mousevsc_init(void)
874 return vmbus_driver_register(&mousevsc_drv);
877 static void __exit mousevsc_exit(void)
879 vmbus_driver_unregister(&mousevsc_drv);
882 MODULE_LICENSE("GPL");
883 MODULE_VERSION(HV_DRV_VERSION);
884 module_init(mousevsc_init);
885 module_exit(mousevsc_exit);