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 void reportdesc_callback(struct hv_device
*dev
, void *packet
, u32 len
);
185 static struct mousevsc_dev
*alloc_input_device(struct hv_device
*device
)
187 struct mousevsc_dev
*input_dev
;
189 input_dev
= kzalloc(sizeof(struct mousevsc_dev
), GFP_KERNEL
);
195 * Set to 2 to allow both inbound and outbound traffics
196 * (ie get_input_device() and must_get_input_device()) to proceed.
198 atomic_cmpxchg(&input_dev
->ref_count
, 0, 2);
200 input_dev
->device
= device
;
201 device
->ext
= input_dev
;
206 static void free_input_device(struct mousevsc_dev
*device
)
208 WARN_ON(atomic_read(&device
->ref_count
) == 0);
213 * Get the inputdevice object if exists and its refcount > 1
215 static struct mousevsc_dev
*get_input_device(struct hv_device
*device
)
217 struct mousevsc_dev
*input_dev
;
219 input_dev
= (struct mousevsc_dev
*)device
->ext
;
223 * This sure isn't a valid thing to print for debugging, no matter
224 * what the intention is...
226 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
227 * input_dev->ref_count);
230 if (input_dev
&& atomic_read(&input_dev
->ref_count
) > 1)
231 atomic_inc(&input_dev
->ref_count
);
239 * Get the inputdevice object iff exists and its refcount > 0
241 static struct mousevsc_dev
*must_get_input_device(struct hv_device
*device
)
243 struct mousevsc_dev
*input_dev
;
245 input_dev
= (struct mousevsc_dev
*)device
->ext
;
247 if (input_dev
&& atomic_read(&input_dev
->ref_count
))
248 atomic_inc(&input_dev
->ref_count
);
255 static void put_input_device(struct hv_device
*device
)
257 struct mousevsc_dev
*input_dev
;
259 input_dev
= (struct mousevsc_dev
*)device
->ext
;
261 atomic_dec(&input_dev
->ref_count
);
265 * Drop ref count to 1 to effectively disable get_input_device()
267 static struct mousevsc_dev
*release_input_device(struct hv_device
*device
)
269 struct mousevsc_dev
*input_dev
;
271 input_dev
= (struct mousevsc_dev
*)device
->ext
;
273 /* Busy wait until the ref drop to 2, then set it to 1 */
274 while (atomic_cmpxchg(&input_dev
->ref_count
, 2, 1) != 2)
281 * Drop ref count to 0. No one can use input_device object.
283 static struct mousevsc_dev
*final_release_input_device(struct hv_device
*device
)
285 struct mousevsc_dev
*input_dev
;
287 input_dev
= (struct mousevsc_dev
*)device
->ext
;
289 /* Busy wait until the ref drop to 1, then set it to 0 */
290 while (atomic_cmpxchg(&input_dev
->ref_count
, 1, 0) != 1)
297 static void mousevsc_on_send_completion(struct hv_device
*device
,
298 struct vmpacket_descriptor
*packet
)
300 struct mousevsc_dev
*input_dev
;
303 input_dev
= must_get_input_device(device
);
305 pr_err("unable to get input device...device being destroyed?");
309 request
= (void *)(unsigned long)packet
->trans_id
;
311 if (request
== &input_dev
->protocol_req
) {
313 /* Shouldn't we be doing something here? */
316 put_input_device(device
);
319 static void mousevsc_on_receive_device_info(struct mousevsc_dev
*input_device
,
320 struct synthhid_device_info
*device_info
)
323 struct hid_descriptor
*desc
;
324 struct mousevsc_prt_msg ack
;
326 /* Assume success for now */
327 input_device
->dev_info_status
= 0;
329 /* Save the device attr */
330 memcpy(&input_device
->hid_dev_info
, &device_info
->hid_dev_info
,
331 sizeof(struct hv_input_dev_info
));
333 /* Save the hid desc */
334 desc
= &device_info
->hid_descriptor
;
335 WARN_ON(desc
->bLength
> 0);
337 input_device
->hid_desc
= kzalloc(desc
->bLength
, GFP_KERNEL
);
339 if (!input_device
->hid_desc
) {
340 pr_err("unable to allocate hid descriptor - size %d", desc
->bLength
);
344 memcpy(input_device
->hid_desc
, desc
, desc
->bLength
);
346 /* Save the report desc */
347 input_device
->report_desc_size
= desc
->desc
[0].wDescriptorLength
;
348 input_device
->report_desc
= kzalloc(input_device
->report_desc_size
,
351 if (!input_device
->report_desc
) {
352 pr_err("unable to allocate report descriptor - size %d",
353 input_device
->report_desc_size
);
357 memcpy(input_device
->report_desc
,
358 ((unsigned char *)desc
) + desc
->bLength
,
359 desc
->desc
[0].wDescriptorLength
);
362 memset(&ack
, 0, sizeof(struct mousevsc_prt_msg
));
364 ack
.type
= PipeMessageData
;
365 ack
.size
= sizeof(struct synthhid_device_info_ack
);
367 ack
.ack
.header
.type
= SynthHidInitialDeviceInfoAck
;
368 ack
.ack
.header
.size
= 1;
369 ack
.ack
.reserved
= 0;
371 ret
= vmbus_sendpacket(input_device
->device
->channel
,
373 sizeof(struct pipe_prt_msg
) - sizeof(unsigned char) +
374 sizeof(struct synthhid_device_info_ack
),
377 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
379 pr_err("unable to send synthhid device info ack - ret %d",
384 input_device
->device_wait_condition
= 1;
385 wake_up(&input_device
->dev_info_wait_event
);
390 kfree(input_device
->hid_desc
);
391 input_device
->hid_desc
= NULL
;
393 kfree(input_device
->report_desc
);
394 input_device
->report_desc
= NULL
;
396 input_device
->dev_info_status
= -1;
397 input_device
->device_wait_condition
= 1;
398 wake_up(&input_device
->dev_info_wait_event
);
401 static void mousevsc_on_receive_input_report(struct mousevsc_dev
*input_device
,
402 struct synthhid_input_report
*input_report
)
404 struct hv_driver
*input_drv
;
405 struct input_device_context
*input_dev_ctx
;
407 if (!input_device
->init_complete
) {
408 pr_info("Initialization incomplete...ignoring input_report msg");
412 input_drv
= drv_to_hv_drv(input_device
->device
->device
.driver
);
414 input_dev_ctx
= dev_get_drvdata(&input_device
->device
->device
);
416 hid_input_report(input_dev_ctx
->hid_device
,
417 HID_INPUT_REPORT
, input_report
->buffer
, input_report
->header
.size
, 1);
421 static void mousevsc_on_receive(struct hv_device
*device
,
422 struct vmpacket_descriptor
*packet
)
424 struct pipe_prt_msg
*pipe_msg
;
425 struct synthhid_msg
*hid_msg
;
426 struct mousevsc_dev
*input_dev
;
428 input_dev
= must_get_input_device(device
);
430 pr_err("unable to get input device...device being destroyed?");
434 pipe_msg
= (struct pipe_prt_msg
*)((unsigned long)packet
+
435 (packet
->offset8
<< 3));
437 if (pipe_msg
->type
!= PipeMessageData
) {
438 pr_err("unknown pipe msg type - type %d len %d",
439 pipe_msg
->type
, pipe_msg
->size
);
440 put_input_device(device
);
444 hid_msg
= (struct synthhid_msg
*)&pipe_msg
->data
[0];
446 switch (hid_msg
->header
.type
) {
447 case SynthHidProtocolResponse
:
448 memcpy(&input_dev
->protocol_resp
, pipe_msg
,
449 pipe_msg
->size
+ sizeof(struct pipe_prt_msg
) -
450 sizeof(unsigned char));
451 input_dev
->protocol_wait_condition
= 1;
452 wake_up(&input_dev
->protocol_wait_event
);
455 case SynthHidInitialDeviceInfo
:
456 WARN_ON(pipe_msg
->size
>= sizeof(struct hv_input_dev_info
));
459 * Parse out the device info into device attr,
460 * hid desc and report desc
462 mousevsc_on_receive_device_info(input_dev
,
463 (struct synthhid_device_info
*)&pipe_msg
->data
[0]);
465 case SynthHidInputReport
:
466 mousevsc_on_receive_input_report(input_dev
,
467 (struct synthhid_input_report
*)&pipe_msg
->data
[0]);
471 pr_err("unsupported hid msg type - type %d len %d",
472 hid_msg
->header
.type
, hid_msg
->header
.size
);
476 put_input_device(device
);
479 static void mousevsc_on_channel_callback(void *context
)
481 const int packetSize
= 0x100;
483 struct hv_device
*device
= (struct hv_device
*)context
;
484 struct mousevsc_dev
*input_dev
;
488 unsigned char packet
[0x100];
489 struct vmpacket_descriptor
*desc
;
490 unsigned char *buffer
= packet
;
491 int bufferlen
= packetSize
;
493 input_dev
= must_get_input_device(device
);
496 pr_err("unable to get input device...device being destroyed?");
501 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
,
502 bufferlen
, &bytes_recvd
, &req_id
);
505 if (bytes_recvd
> 0) {
506 desc
= (struct vmpacket_descriptor
*)buffer
;
508 switch (desc
->type
) {
510 mousevsc_on_send_completion(
514 case VM_PKT_DATA_INBAND
:
520 pr_err("unhandled packet type %d, tid %llx len %d\n",
528 if (bufferlen
> packetSize
) {
532 bufferlen
= packetSize
;
536 * pr_debug("nothing else to read...");
539 if (bufferlen
> packetSize
) {
543 bufferlen
= packetSize
;
547 } else if (ret
== -ENOBUFS
) {
548 /* Handle large packet */
549 bufferlen
= bytes_recvd
;
550 buffer
= kzalloc(bytes_recvd
, GFP_KERNEL
);
552 if (buffer
== NULL
) {
554 bufferlen
= packetSize
;
556 /* Try again next time around */
557 pr_err("unable to allocate buffer of size %d!",
564 put_input_device(device
);
569 static int mousevsc_connect_to_vsp(struct hv_device
*device
)
572 struct mousevsc_dev
*input_dev
;
573 struct mousevsc_prt_msg
*request
;
574 struct mousevsc_prt_msg
*response
;
576 input_dev
= get_input_device(device
);
579 pr_err("unable to get input device...device being destroyed?");
583 init_waitqueue_head(&input_dev
->protocol_wait_event
);
584 init_waitqueue_head(&input_dev
->dev_info_wait_event
);
586 request
= &input_dev
->protocol_req
;
589 * Now, initiate the vsc/vsp initialization protocol on the open channel
591 memset(request
, 0, sizeof(struct mousevsc_prt_msg
));
593 request
->type
= PipeMessageData
;
594 request
->size
= sizeof(struct synthhid_protocol_request
);
596 request
->request
.header
.type
= SynthHidProtocolRequest
;
597 request
->request
.header
.size
= sizeof(unsigned long);
598 request
->request
.version_requested
.version
= SYNTHHID_INPUT_VERSION
;
600 pr_info("synthhid protocol request...");
602 ret
= vmbus_sendpacket(device
->channel
, request
,
603 sizeof(struct pipe_prt_msg
) -
604 sizeof(unsigned char) +
605 sizeof(struct synthhid_protocol_request
),
606 (unsigned long)request
,
608 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
610 pr_err("unable to send synthhid protocol request.");
614 input_dev
->protocol_wait_condition
= 0;
615 wait_event_timeout(input_dev
->protocol_wait_event
,
616 input_dev
->protocol_wait_condition
, msecs_to_jiffies(1000));
617 if (input_dev
->protocol_wait_condition
== 0) {
622 response
= &input_dev
->protocol_resp
;
624 if (!response
->response
.approved
) {
625 pr_err("synthhid protocol request failed (version %d)",
626 SYNTHHID_INPUT_VERSION
);
631 input_dev
->device_wait_condition
= 0;
632 wait_event_timeout(input_dev
->dev_info_wait_event
,
633 input_dev
->device_wait_condition
, msecs_to_jiffies(1000));
634 if (input_dev
->device_wait_condition
== 0) {
640 * We should have gotten the device attr, hid desc and report
643 if (!input_dev
->dev_info_status
)
644 pr_info("**** input channel up and running!! ****");
649 put_input_device(device
);
654 static int mousevsc_on_device_add(struct hv_device
*device
,
655 void *additional_info
)
658 struct mousevsc_dev
*input_dev
;
659 struct hv_driver
*input_drv
;
660 struct hv_input_dev_info dev_info
;
661 struct input_device_context
*input_device_ctx
;
663 input_dev
= alloc_input_device(device
);
670 input_dev
->init_complete
= false;
672 /* Open the channel */
673 ret
= vmbus_open(device
->channel
,
674 INPUTVSC_SEND_RING_BUFFER_SIZE
,
675 INPUTVSC_RECV_RING_BUFFER_SIZE
,
678 mousevsc_on_channel_callback
,
683 pr_err("unable to open channel: %d", ret
);
684 free_input_device(input_dev
);
688 pr_info("InputVsc channel open: %d", ret
);
690 ret
= mousevsc_connect_to_vsp(device
);
693 pr_err("unable to connect channel: %d", ret
);
695 vmbus_close(device
->channel
);
696 free_input_device(input_dev
);
700 input_drv
= drv_to_hv_drv(input_dev
->device
->device
.driver
);
702 dev_info
.vendor
= input_dev
->hid_dev_info
.vendor
;
703 dev_info
.product
= input_dev
->hid_dev_info
.product
;
704 dev_info
.version
= input_dev
->hid_dev_info
.version
;
705 strcpy(dev_info
.name
, "Microsoft Vmbus HID-compliant Mouse");
707 /* Send the device info back up */
708 input_device_ctx
= dev_get_drvdata(&device
->device
);
709 memcpy(&input_device_ctx
->device_info
, &dev_info
,
710 sizeof(struct hv_input_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
);
765 static int mousevsc_hid_open(struct hid_device
*hid
)
770 static void mousevsc_hid_close(struct hid_device
*hid
)
774 static int mousevsc_probe(struct hv_device
*dev
)
778 struct input_device_context
*input_dev_ctx
;
780 input_dev_ctx
= kmalloc(sizeof(struct input_device_context
),
783 dev_set_drvdata(&dev
->device
, input_dev_ctx
);
785 /* Call to the vsc driver to add the device */
786 ret
= mousevsc_on_device_add(dev
, NULL
);
789 DPRINT_ERR(INPUTVSC_DRV
, "unable to add input vsc device");
797 static int mousevsc_remove(struct hv_device
*dev
)
801 struct input_device_context
*input_dev_ctx
;
803 input_dev_ctx
= kmalloc(sizeof(struct input_device_context
),
806 dev_set_drvdata(&dev
->device
, input_dev_ctx
);
808 if (input_dev_ctx
->connected
) {
809 hidinput_disconnect(input_dev_ctx
->hid_device
);
810 input_dev_ctx
->connected
= 0;
814 * Call to the vsc driver to let it know that the device
817 ret
= mousevsc_on_device_remove(dev
);
820 DPRINT_ERR(INPUTVSC_DRV
,
821 "unable to remove vsc device (ret %d)", ret
);
824 kfree(input_dev_ctx
);
829 static void reportdesc_callback(struct hv_device
*dev
, void *packet
, u32 len
)
831 struct input_device_context
*input_device_ctx
=
832 dev_get_drvdata(&dev
->device
);
833 struct hid_device
*hid_dev
;
835 /* hid_debug = -1; */
836 hid_dev
= kmalloc(sizeof(struct hid_device
), GFP_KERNEL
);
838 if (hid_parse_report(hid_dev
, packet
, len
)) {
839 DPRINT_INFO(INPUTVSC_DRV
, "Unable to call hd_parse_report");
844 DPRINT_INFO(INPUTVSC_DRV
, "hid_device created");
846 hid_dev
->ll_driver
->open
= mousevsc_hid_open
;
847 hid_dev
->ll_driver
->close
= mousevsc_hid_close
;
849 hid_dev
->bus
= BUS_VIRTUAL
;
850 hid_dev
->vendor
= input_device_ctx
->device_info
.vendor
;
851 hid_dev
->product
= input_device_ctx
->device_info
.product
;
852 hid_dev
->version
= input_device_ctx
->device_info
.version
;
853 hid_dev
->dev
= dev
->device
;
855 sprintf(hid_dev
->name
, "%s",
856 input_device_ctx
->device_info
.name
);
859 * HJ Do we want to call it with a 0
861 if (!hidinput_connect(hid_dev
, 0)) {
862 hid_dev
->claimed
|= HID_CLAIMED_INPUT
;
864 input_device_ctx
->connected
= 1;
866 DPRINT_INFO(INPUTVSC_DRV
,
867 "HID device claimed by input\n");
870 if (!hid_dev
->claimed
) {
871 DPRINT_ERR(INPUTVSC_DRV
,
872 "HID device not claimed by "
873 "input or hiddev\n");
876 input_device_ctx
->hid_device
= hid_dev
;
882 static const struct hv_vmbus_device_id id_table
[] = {
884 { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
885 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
890 * The mouse driver is not functional; do not auto-load it.
892 /* MODULE_DEVICE_TABLE(vmbus, id_table); */
894 static struct hv_driver mousevsc_drv
= {
896 .id_table
= id_table
,
897 .probe
= mousevsc_probe
,
898 .remove
= mousevsc_remove
,
901 static int __init
mousevsc_init(void)
903 return vmbus_driver_register(&mousevsc_drv
);
906 static void __exit
mousevsc_exit(void)
908 vmbus_driver_unregister(&mousevsc_drv
);
911 MODULE_LICENSE("GPL");
912 MODULE_VERSION(HV_DRV_VERSION
);
913 module_init(mousevsc_init
);
914 module_exit(mousevsc_exit
);