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
{
33 unsigned short vendor
;
34 unsigned short product
;
35 unsigned short version
;
39 /* The maximum size of a synthetic input message. */
40 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
46 * Beta, RC < 2008/1/22 1,0
49 #define SYNTHHID_INPUT_VERSION_MAJOR 2
50 #define SYNTHHID_INPUT_VERSION_MINOR 0
51 #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
52 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
57 * Message types in the synthetic input protocol
59 enum synthhid_msg_type
{
60 SynthHidProtocolRequest
,
61 SynthHidProtocolResponse
,
62 SynthHidInitialDeviceInfo
,
63 SynthHidInitialDeviceInfoAck
,
69 * Basic message structures.
71 struct synthhid_msg_hdr
{
72 enum synthhid_msg_type type
;
77 struct synthhid_msg_hdr header
;
78 char data
[1]; /* Enclosed message */
81 union synthhid_version
{
92 struct synthhid_protocol_request
{
93 struct synthhid_msg_hdr header
;
94 union synthhid_version version_requested
;
97 struct synthhid_protocol_response
{
98 struct synthhid_msg_hdr header
;
99 union synthhid_version version_requested
;
100 unsigned char approved
;
103 struct synthhid_device_info
{
104 struct synthhid_msg_hdr header
;
105 struct hv_input_dev_info hid_dev_info
;
106 struct hid_descriptor hid_descriptor
;
109 struct synthhid_device_info_ack
{
110 struct synthhid_msg_hdr header
;
111 unsigned char reserved
;
114 struct synthhid_input_report
{
115 struct synthhid_msg_hdr header
;
121 #define INPUTVSC_SEND_RING_BUFFER_SIZE (10*PAGE_SIZE)
122 #define INPUTVSC_RECV_RING_BUFFER_SIZE (10*PAGE_SIZE)
124 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
126 enum pipe_prot_msg_type
{
127 PipeMessageInvalid
= 0,
133 struct pipe_prt_msg
{
134 enum pipe_prot_msg_type type
;
142 struct mousevsc_prt_msg
{
143 enum pipe_prot_msg_type type
;
146 struct synthhid_protocol_request request
;
147 struct synthhid_protocol_response response
;
148 struct synthhid_device_info_ack ack
;
153 * Represents an mousevsc device
155 struct mousevsc_dev
{
156 struct hv_device
*device
;
157 /* 0 indicates the device is being destroyed */
159 int num_outstanding_req
;
160 unsigned char init_complete
;
161 struct mousevsc_prt_msg protocol_req
;
162 struct mousevsc_prt_msg protocol_resp
;
163 /* Synchronize the request/response if needed */
164 wait_queue_head_t protocol_wait_event
;
165 wait_queue_head_t dev_info_wait_event
;
166 int protocol_wait_condition
;
167 int device_wait_condition
;
170 struct hid_descriptor
*hid_desc
;
171 unsigned char *report_desc
;
172 u32 report_desc_size
;
173 struct hv_input_dev_info hid_dev_info
;
176 struct input_device_context
{
177 struct hv_device
*device_ctx
;
178 struct hid_device
*hid_device
;
179 struct hv_input_dev_info device_info
;
183 static struct mousevsc_dev
*alloc_input_device(struct hv_device
*device
)
185 struct mousevsc_dev
*input_dev
;
187 input_dev
= kzalloc(sizeof(struct mousevsc_dev
), GFP_KERNEL
);
193 * Set to 2 to allow both inbound and outbound traffics
194 * (ie get_input_device() and must_get_input_device()) to proceed.
196 atomic_cmpxchg(&input_dev
->ref_count
, 0, 2);
198 input_dev
->device
= device
;
199 device
->ext
= input_dev
;
204 static void free_input_device(struct mousevsc_dev
*device
)
206 WARN_ON(atomic_read(&device
->ref_count
) == 0);
211 * Get the inputdevice object if exists and its refcount > 1
213 static struct mousevsc_dev
*get_input_device(struct hv_device
*device
)
215 struct mousevsc_dev
*input_dev
;
217 input_dev
= (struct mousevsc_dev
*)device
->ext
;
221 * This sure isn't a valid thing to print for debugging, no matter
222 * what the intention is...
224 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
225 * input_dev->ref_count);
228 if (input_dev
&& atomic_read(&input_dev
->ref_count
) > 1)
229 atomic_inc(&input_dev
->ref_count
);
237 * Get the inputdevice object iff exists and its refcount > 0
239 static struct mousevsc_dev
*must_get_input_device(struct hv_device
*device
)
241 struct mousevsc_dev
*input_dev
;
243 input_dev
= (struct mousevsc_dev
*)device
->ext
;
245 if (input_dev
&& atomic_read(&input_dev
->ref_count
))
246 atomic_inc(&input_dev
->ref_count
);
253 static void put_input_device(struct hv_device
*device
)
255 struct mousevsc_dev
*input_dev
;
257 input_dev
= (struct mousevsc_dev
*)device
->ext
;
259 atomic_dec(&input_dev
->ref_count
);
263 * Drop ref count to 1 to effectively disable get_input_device()
265 static struct mousevsc_dev
*release_input_device(struct hv_device
*device
)
267 struct mousevsc_dev
*input_dev
;
269 input_dev
= (struct mousevsc_dev
*)device
->ext
;
271 /* Busy wait until the ref drop to 2, then set it to 1 */
272 while (atomic_cmpxchg(&input_dev
->ref_count
, 2, 1) != 2)
279 * Drop ref count to 0. No one can use input_device object.
281 static struct mousevsc_dev
*final_release_input_device(struct hv_device
*device
)
283 struct mousevsc_dev
*input_dev
;
285 input_dev
= (struct mousevsc_dev
*)device
->ext
;
287 /* Busy wait until the ref drop to 1, then set it to 0 */
288 while (atomic_cmpxchg(&input_dev
->ref_count
, 1, 0) != 1)
295 static void mousevsc_on_send_completion(struct hv_device
*device
,
296 struct vmpacket_descriptor
*packet
)
298 struct mousevsc_dev
*input_dev
;
301 input_dev
= must_get_input_device(device
);
303 pr_err("unable to get input device...device being destroyed?");
307 request
= (void *)(unsigned long)packet
->trans_id
;
309 if (request
== &input_dev
->protocol_req
) {
311 /* Shouldn't we be doing something here? */
314 put_input_device(device
);
317 static void mousevsc_on_receive_device_info(struct mousevsc_dev
*input_device
,
318 struct synthhid_device_info
*device_info
)
321 struct hid_descriptor
*desc
;
322 struct mousevsc_prt_msg ack
;
324 /* Assume success for now */
325 input_device
->dev_info_status
= 0;
327 /* Save the device attr */
328 memcpy(&input_device
->hid_dev_info
, &device_info
->hid_dev_info
,
329 sizeof(struct hv_input_dev_info
));
331 /* Save the hid desc */
332 desc
= &device_info
->hid_descriptor
;
333 WARN_ON(desc
->bLength
> 0);
335 input_device
->hid_desc
= kzalloc(desc
->bLength
, GFP_KERNEL
);
337 if (!input_device
->hid_desc
) {
338 pr_err("unable to allocate hid descriptor - size %d", desc
->bLength
);
342 memcpy(input_device
->hid_desc
, desc
, desc
->bLength
);
344 /* Save the report desc */
345 input_device
->report_desc_size
= desc
->desc
[0].wDescriptorLength
;
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
;
403 struct input_device_context
*input_dev_ctx
;
405 if (!input_device
->init_complete
) {
406 pr_info("Initialization incomplete...ignoring input_report msg");
410 input_drv
= drv_to_hv_drv(input_device
->device
->device
.driver
);
412 input_dev_ctx
= dev_get_drvdata(&input_device
->device
->device
);
414 hid_input_report(input_dev_ctx
->hid_device
,
415 HID_INPUT_REPORT
, input_report
->buffer
, input_report
->header
.size
, 1);
419 static void mousevsc_on_receive(struct hv_device
*device
,
420 struct vmpacket_descriptor
*packet
)
422 struct pipe_prt_msg
*pipe_msg
;
423 struct synthhid_msg
*hid_msg
;
424 struct mousevsc_dev
*input_dev
;
426 input_dev
= must_get_input_device(device
);
428 pr_err("unable to get input device...device being destroyed?");
432 pipe_msg
= (struct pipe_prt_msg
*)((unsigned long)packet
+
433 (packet
->offset8
<< 3));
435 if (pipe_msg
->type
!= PipeMessageData
) {
436 pr_err("unknown pipe msg type - type %d len %d",
437 pipe_msg
->type
, pipe_msg
->size
);
438 put_input_device(device
);
442 hid_msg
= (struct synthhid_msg
*)&pipe_msg
->data
[0];
444 switch (hid_msg
->header
.type
) {
445 case SynthHidProtocolResponse
:
446 memcpy(&input_dev
->protocol_resp
, pipe_msg
,
447 pipe_msg
->size
+ sizeof(struct pipe_prt_msg
) -
448 sizeof(unsigned char));
449 input_dev
->protocol_wait_condition
= 1;
450 wake_up(&input_dev
->protocol_wait_event
);
453 case SynthHidInitialDeviceInfo
:
454 WARN_ON(pipe_msg
->size
>= sizeof(struct hv_input_dev_info
));
457 * Parse out the device info into device attr,
458 * hid desc and report desc
460 mousevsc_on_receive_device_info(input_dev
,
461 (struct synthhid_device_info
*)&pipe_msg
->data
[0]);
463 case SynthHidInputReport
:
464 mousevsc_on_receive_input_report(input_dev
,
465 (struct synthhid_input_report
*)&pipe_msg
->data
[0]);
469 pr_err("unsupported hid msg type - type %d len %d",
470 hid_msg
->header
.type
, hid_msg
->header
.size
);
474 put_input_device(device
);
477 static void mousevsc_on_channel_callback(void *context
)
479 const int packetSize
= 0x100;
481 struct hv_device
*device
= (struct hv_device
*)context
;
482 struct mousevsc_dev
*input_dev
;
486 unsigned char packet
[0x100];
487 struct vmpacket_descriptor
*desc
;
488 unsigned char *buffer
= packet
;
489 int bufferlen
= packetSize
;
491 input_dev
= must_get_input_device(device
);
494 pr_err("unable to get input device...device being destroyed?");
499 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
,
500 bufferlen
, &bytes_recvd
, &req_id
);
503 if (bytes_recvd
> 0) {
504 desc
= (struct vmpacket_descriptor
*)buffer
;
506 switch (desc
->type
) {
508 mousevsc_on_send_completion(
512 case VM_PKT_DATA_INBAND
:
518 pr_err("unhandled packet type %d, tid %llx len %d\n",
526 if (bufferlen
> packetSize
) {
530 bufferlen
= packetSize
;
534 * pr_debug("nothing else to read...");
537 if (bufferlen
> packetSize
) {
541 bufferlen
= packetSize
;
545 } else if (ret
== -ENOBUFS
) {
546 /* Handle large packet */
547 bufferlen
= bytes_recvd
;
548 buffer
= kzalloc(bytes_recvd
, GFP_KERNEL
);
550 if (buffer
== NULL
) {
552 bufferlen
= packetSize
;
554 /* Try again next time around */
555 pr_err("unable to allocate buffer of size %d!",
562 put_input_device(device
);
567 static int mousevsc_connect_to_vsp(struct hv_device
*device
)
570 struct mousevsc_dev
*input_dev
;
571 struct mousevsc_prt_msg
*request
;
572 struct mousevsc_prt_msg
*response
;
574 input_dev
= get_input_device(device
);
577 pr_err("unable to get input device...device being destroyed?");
581 init_waitqueue_head(&input_dev
->protocol_wait_event
);
582 init_waitqueue_head(&input_dev
->dev_info_wait_event
);
584 request
= &input_dev
->protocol_req
;
587 * Now, initiate the vsc/vsp initialization protocol on the open channel
589 memset(request
, 0, sizeof(struct mousevsc_prt_msg
));
591 request
->type
= PipeMessageData
;
592 request
->size
= sizeof(struct synthhid_protocol_request
);
594 request
->request
.header
.type
= SynthHidProtocolRequest
;
595 request
->request
.header
.size
= sizeof(unsigned long);
596 request
->request
.version_requested
.version
= SYNTHHID_INPUT_VERSION
;
598 pr_info("synthhid protocol request...");
600 ret
= vmbus_sendpacket(device
->channel
, request
,
601 sizeof(struct pipe_prt_msg
) -
602 sizeof(unsigned char) +
603 sizeof(struct synthhid_protocol_request
),
604 (unsigned long)request
,
606 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
608 pr_err("unable to send synthhid protocol request.");
612 input_dev
->protocol_wait_condition
= 0;
613 wait_event_timeout(input_dev
->protocol_wait_event
,
614 input_dev
->protocol_wait_condition
, msecs_to_jiffies(1000));
615 if (input_dev
->protocol_wait_condition
== 0) {
620 response
= &input_dev
->protocol_resp
;
622 if (!response
->response
.approved
) {
623 pr_err("synthhid protocol request failed (version %d)",
624 SYNTHHID_INPUT_VERSION
);
629 input_dev
->device_wait_condition
= 0;
630 wait_event_timeout(input_dev
->dev_info_wait_event
,
631 input_dev
->device_wait_condition
, msecs_to_jiffies(1000));
632 if (input_dev
->device_wait_condition
== 0) {
638 * We should have gotten the device attr, hid desc and report
641 if (!input_dev
->dev_info_status
)
642 pr_info("**** input channel up and running!! ****");
647 put_input_device(device
);
652 static int mousevsc_hid_open(struct hid_device
*hid
)
657 static void mousevsc_hid_close(struct hid_device
*hid
)
661 static void reportdesc_callback(struct hv_device
*dev
, void *packet
, u32 len
)
663 struct input_device_context
*input_device_ctx
=
664 dev_get_drvdata(&dev
->device
);
665 struct hid_device
*hid_dev
;
667 /* hid_debug = -1; */
668 hid_dev
= kmalloc(sizeof(struct hid_device
), GFP_KERNEL
);
670 if (hid_parse_report(hid_dev
, packet
, len
)) {
671 DPRINT_INFO(INPUTVSC_DRV
, "Unable to call hd_parse_report");
676 DPRINT_INFO(INPUTVSC_DRV
, "hid_device created");
678 hid_dev
->ll_driver
->open
= mousevsc_hid_open
;
679 hid_dev
->ll_driver
->close
= mousevsc_hid_close
;
681 hid_dev
->bus
= BUS_VIRTUAL
;
682 hid_dev
->vendor
= input_device_ctx
->device_info
.vendor
;
683 hid_dev
->product
= input_device_ctx
->device_info
.product
;
684 hid_dev
->version
= input_device_ctx
->device_info
.version
;
685 hid_dev
->dev
= dev
->device
;
687 sprintf(hid_dev
->name
, "%s",
688 input_device_ctx
->device_info
.name
);
691 * HJ Do we want to call it with a 0
693 if (!hidinput_connect(hid_dev
, 0)) {
694 hid_dev
->claimed
|= HID_CLAIMED_INPUT
;
696 input_device_ctx
->connected
= 1;
698 DPRINT_INFO(INPUTVSC_DRV
,
699 "HID device claimed by input\n");
702 if (!hid_dev
->claimed
) {
703 DPRINT_ERR(INPUTVSC_DRV
,
704 "HID device not claimed by "
705 "input or hiddev\n");
708 input_device_ctx
->hid_device
= hid_dev
;
714 static int mousevsc_on_device_add(struct hv_device
*device
,
715 void *additional_info
)
718 struct mousevsc_dev
*input_dev
;
719 struct hv_driver
*input_drv
;
720 struct hv_input_dev_info dev_info
;
721 struct input_device_context
*input_device_ctx
;
723 input_dev
= alloc_input_device(device
);
730 input_dev
->init_complete
= false;
732 /* Open the channel */
733 ret
= vmbus_open(device
->channel
,
734 INPUTVSC_SEND_RING_BUFFER_SIZE
,
735 INPUTVSC_RECV_RING_BUFFER_SIZE
,
738 mousevsc_on_channel_callback
,
743 pr_err("unable to open channel: %d", ret
);
744 free_input_device(input_dev
);
748 pr_info("InputVsc channel open: %d", ret
);
750 ret
= mousevsc_connect_to_vsp(device
);
753 pr_err("unable to connect channel: %d", ret
);
755 vmbus_close(device
->channel
);
756 free_input_device(input_dev
);
760 input_drv
= drv_to_hv_drv(input_dev
->device
->device
.driver
);
762 dev_info
.vendor
= input_dev
->hid_dev_info
.vendor
;
763 dev_info
.product
= input_dev
->hid_dev_info
.product
;
764 dev_info
.version
= input_dev
->hid_dev_info
.version
;
765 strcpy(dev_info
.name
, "Microsoft Vmbus HID-compliant Mouse");
767 /* Send the device info back up */
768 input_device_ctx
= dev_get_drvdata(&device
->device
);
769 memcpy(&input_device_ctx
->device_info
, &dev_info
,
770 sizeof(struct hv_input_dev_info
));
772 /* Send the report desc back up */
773 /* workaround SA-167 */
774 if (input_dev
->report_desc
[14] == 0x25)
775 input_dev
->report_desc
[14] = 0x29;
777 reportdesc_callback(device
, input_dev
->report_desc
,
778 input_dev
->report_desc_size
);
780 input_dev
->init_complete
= true;
786 static int mousevsc_on_device_remove(struct hv_device
*device
)
788 struct mousevsc_dev
*input_dev
;
791 pr_info("disabling input device (%p)...",
794 input_dev
= release_input_device(device
);
798 * At this point, all outbound traffic should be disable. We only
799 * allow inbound traffic (responses) to proceed
801 * so that outstanding requests can be completed.
803 while (input_dev
->num_outstanding_req
) {
804 pr_info("waiting for %d requests to complete...",
805 input_dev
->num_outstanding_req
);
810 pr_info("removing input device (%p)...", device
->ext
);
812 input_dev
= final_release_input_device(device
);
814 pr_info("input device (%p) safe to remove", input_dev
);
816 /* Close the channel */
817 vmbus_close(device
->channel
);
819 free_input_device(input_dev
);
825 static int mousevsc_probe(struct hv_device
*dev
)
829 struct input_device_context
*input_dev_ctx
;
831 input_dev_ctx
= kmalloc(sizeof(struct input_device_context
),
834 dev_set_drvdata(&dev
->device
, input_dev_ctx
);
836 /* Call to the vsc driver to add the device */
837 ret
= mousevsc_on_device_add(dev
, NULL
);
840 DPRINT_ERR(INPUTVSC_DRV
, "unable to add input vsc device");
848 static int mousevsc_remove(struct hv_device
*dev
)
852 struct input_device_context
*input_dev_ctx
;
854 input_dev_ctx
= kmalloc(sizeof(struct input_device_context
),
857 dev_set_drvdata(&dev
->device
, input_dev_ctx
);
859 if (input_dev_ctx
->connected
) {
860 hidinput_disconnect(input_dev_ctx
->hid_device
);
861 input_dev_ctx
->connected
= 0;
865 * Call to the vsc driver to let it know that the device
868 ret
= mousevsc_on_device_remove(dev
);
871 DPRINT_ERR(INPUTVSC_DRV
,
872 "unable to remove vsc device (ret %d)", ret
);
875 kfree(input_dev_ctx
);
880 static const struct hv_vmbus_device_id id_table
[] = {
882 { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
883 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
888 * The mouse driver is not functional; do not auto-load it.
890 /* MODULE_DEVICE_TABLE(vmbus, id_table); */
892 static struct hv_driver mousevsc_drv
= {
894 .id_table
= id_table
,
895 .probe
= mousevsc_probe
,
896 .remove
= mousevsc_remove
,
899 static int __init
mousevsc_init(void)
901 return vmbus_driver_register(&mousevsc_drv
);
904 static void __exit
mousevsc_exit(void)
906 vmbus_driver_unregister(&mousevsc_drv
);
909 MODULE_LICENSE("GPL");
910 MODULE_VERSION(HV_DRV_VERSION
);
911 module_init(mousevsc_init
);
912 module_exit(mousevsc_exit
);