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 struct completion wait_event
;
168 struct hid_descriptor
*hid_desc
;
169 unsigned char *report_desc
;
170 u32 report_desc_size
;
171 struct hv_input_dev_info hid_dev_info
;
173 struct hid_device
*hid_device
;
177 static struct mousevsc_dev
*alloc_input_device(struct hv_device
*device
)
179 struct mousevsc_dev
*input_dev
;
181 input_dev
= kzalloc(sizeof(struct mousevsc_dev
), GFP_KERNEL
);
187 * Set to 2 to allow both inbound and outbound traffics
188 * (ie get_input_device() and must_get_input_device()) to proceed.
190 atomic_cmpxchg(&input_dev
->ref_count
, 0, 2);
192 input_dev
->device
= device
;
193 hv_set_drvdata(device
, input_dev
);
194 init_completion(&input_dev
->wait_event
);
199 static void free_input_device(struct mousevsc_dev
*device
)
201 WARN_ON(atomic_read(&device
->ref_count
) != 0);
206 * Get the inputdevice object if exists and its refcount > 1
208 static struct mousevsc_dev
*get_input_device(struct hv_device
*device
)
210 struct mousevsc_dev
*input_dev
;
212 input_dev
= hv_get_drvdata(device
);
216 * This sure isn't a valid thing to print for debugging, no matter
217 * what the intention is...
219 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
220 * input_dev->ref_count);
223 if (input_dev
&& atomic_read(&input_dev
->ref_count
) > 1)
224 atomic_inc(&input_dev
->ref_count
);
232 * Get the inputdevice object iff exists and its refcount > 0
234 static struct mousevsc_dev
*must_get_input_device(struct hv_device
*device
)
236 struct mousevsc_dev
*input_dev
;
238 input_dev
= hv_get_drvdata(device
);
240 if (input_dev
&& atomic_read(&input_dev
->ref_count
))
241 atomic_inc(&input_dev
->ref_count
);
248 static void put_input_device(struct hv_device
*device
)
250 struct mousevsc_dev
*input_dev
;
252 input_dev
= hv_get_drvdata(device
);
254 atomic_dec(&input_dev
->ref_count
);
258 * Drop ref count to 1 to effectively disable get_input_device()
260 static struct mousevsc_dev
*release_input_device(struct hv_device
*device
)
262 struct mousevsc_dev
*input_dev
;
264 input_dev
= hv_get_drvdata(device
);
266 /* Busy wait until the ref drop to 2, then set it to 1 */
267 while (atomic_cmpxchg(&input_dev
->ref_count
, 2, 1) != 2)
274 * Drop ref count to 0. No one can use input_device object.
276 static struct mousevsc_dev
*final_release_input_device(struct hv_device
*device
)
278 struct mousevsc_dev
*input_dev
;
280 input_dev
= hv_get_drvdata(device
);
282 /* Busy wait until the ref drop to 1, then set it to 0 */
283 while (atomic_cmpxchg(&input_dev
->ref_count
, 1, 0) != 1)
286 hv_set_drvdata(device
, NULL
);
290 static void mousevsc_on_send_completion(struct hv_device
*device
,
291 struct vmpacket_descriptor
*packet
)
293 struct mousevsc_dev
*input_dev
;
296 input_dev
= must_get_input_device(device
);
298 pr_err("unable to get input device...device being destroyed?");
302 request
= (void *)(unsigned long)packet
->trans_id
;
304 if (request
== &input_dev
->protocol_req
) {
306 /* Shouldn't we be doing something here? */
309 put_input_device(device
);
312 static void mousevsc_on_receive_device_info(struct mousevsc_dev
*input_device
,
313 struct synthhid_device_info
*device_info
)
316 struct hid_descriptor
*desc
;
317 struct mousevsc_prt_msg ack
;
319 /* Assume success for now */
320 input_device
->dev_info_status
= 0;
322 /* Save the device attr */
323 memcpy(&input_device
->hid_dev_info
, &device_info
->hid_dev_info
,
324 sizeof(struct hv_input_dev_info
));
326 /* Save the hid desc */
327 desc
= &device_info
->hid_descriptor
;
328 WARN_ON(desc
->bLength
== 0);
330 input_device
->hid_desc
= kzalloc(desc
->bLength
, GFP_ATOMIC
);
332 if (!input_device
->hid_desc
) {
333 pr_err("unable to allocate hid descriptor - size %d",
338 memcpy(input_device
->hid_desc
, desc
, desc
->bLength
);
340 /* Save the report desc */
341 input_device
->report_desc_size
= desc
->desc
[0].wDescriptorLength
;
342 if (input_device
->report_desc_size
== 0)
344 input_device
->report_desc
= kzalloc(input_device
->report_desc_size
,
347 if (!input_device
->report_desc
) {
348 pr_err("unable to allocate report descriptor - size %d",
349 input_device
->report_desc_size
);
353 memcpy(input_device
->report_desc
,
354 ((unsigned char *)desc
) + desc
->bLength
,
355 desc
->desc
[0].wDescriptorLength
);
358 memset(&ack
, 0, sizeof(struct mousevsc_prt_msg
));
360 ack
.type
= PipeMessageData
;
361 ack
.size
= sizeof(struct synthhid_device_info_ack
);
363 ack
.ack
.header
.type
= SynthHidInitialDeviceInfoAck
;
364 ack
.ack
.header
.size
= 1;
365 ack
.ack
.reserved
= 0;
367 ret
= vmbus_sendpacket(input_device
->device
->channel
,
369 sizeof(struct pipe_prt_msg
) - sizeof(unsigned char) +
370 sizeof(struct synthhid_device_info_ack
),
373 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
375 pr_err("unable to send synthhid device info ack - ret %d",
380 complete(&input_device
->wait_event
);
385 kfree(input_device
->hid_desc
);
386 input_device
->hid_desc
= NULL
;
388 kfree(input_device
->report_desc
);
389 input_device
->report_desc
= NULL
;
391 input_device
->dev_info_status
= -1;
392 complete(&input_device
->wait_event
);
395 static void mousevsc_on_receive_input_report(struct mousevsc_dev
*input_device
,
396 struct synthhid_input_report
*input_report
)
398 struct hv_driver
*input_drv
;
400 if (!input_device
->init_complete
) {
401 pr_info("Initialization incomplete...ignoring input_report msg");
405 input_drv
= drv_to_hv_drv(input_device
->device
->device
.driver
);
408 hid_input_report(input_device
->hid_device
,
409 HID_INPUT_REPORT
, input_report
->buffer
, input_report
->header
.size
, 1);
413 static void mousevsc_on_receive(struct hv_device
*device
,
414 struct vmpacket_descriptor
*packet
)
416 struct pipe_prt_msg
*pipe_msg
;
417 struct synthhid_msg
*hid_msg
;
418 struct mousevsc_dev
*input_dev
;
420 input_dev
= must_get_input_device(device
);
422 pr_err("unable to get input device...device being destroyed?");
426 pipe_msg
= (struct pipe_prt_msg
*)((unsigned long)packet
+
427 (packet
->offset8
<< 3));
429 if (pipe_msg
->type
!= PipeMessageData
) {
430 pr_err("unknown pipe msg type - type %d len %d",
431 pipe_msg
->type
, pipe_msg
->size
);
432 put_input_device(device
);
436 hid_msg
= (struct synthhid_msg
*)&pipe_msg
->data
[0];
438 switch (hid_msg
->header
.type
) {
439 case SynthHidProtocolResponse
:
440 memcpy(&input_dev
->protocol_resp
, pipe_msg
,
441 pipe_msg
->size
+ sizeof(struct pipe_prt_msg
) -
442 sizeof(unsigned char));
443 complete(&input_dev
->wait_event
);
446 case SynthHidInitialDeviceInfo
:
447 WARN_ON(pipe_msg
->size
< sizeof(struct hv_input_dev_info
));
450 * Parse out the device info into device attr,
451 * hid desc and report desc
453 mousevsc_on_receive_device_info(input_dev
,
454 (struct synthhid_device_info
*)&pipe_msg
->data
[0]);
456 case SynthHidInputReport
:
457 mousevsc_on_receive_input_report(input_dev
,
458 (struct synthhid_input_report
*)&pipe_msg
->data
[0]);
462 pr_err("unsupported hid msg type - type %d len %d",
463 hid_msg
->header
.type
, hid_msg
->header
.size
);
467 put_input_device(device
);
470 static void mousevsc_on_channel_callback(void *context
)
472 const int packetSize
= 0x100;
474 struct hv_device
*device
= (struct hv_device
*)context
;
475 struct mousevsc_dev
*input_dev
;
479 unsigned char packet
[0x100];
480 struct vmpacket_descriptor
*desc
;
481 unsigned char *buffer
= packet
;
482 int bufferlen
= packetSize
;
484 input_dev
= must_get_input_device(device
);
487 pr_err("unable to get input device...device being destroyed?");
492 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
,
493 bufferlen
, &bytes_recvd
, &req_id
);
496 if (bytes_recvd
> 0) {
497 desc
= (struct vmpacket_descriptor
*)buffer
;
499 switch (desc
->type
) {
501 mousevsc_on_send_completion(
505 case VM_PKT_DATA_INBAND
:
511 pr_err("unhandled packet type %d, tid %llx len %d\n",
519 if (bufferlen
> packetSize
) {
523 bufferlen
= packetSize
;
527 * pr_debug("nothing else to read...");
530 if (bufferlen
> packetSize
) {
534 bufferlen
= packetSize
;
538 } else if (ret
== -ENOBUFS
) {
539 /* Handle large packet */
540 bufferlen
= bytes_recvd
;
541 buffer
= kzalloc(bytes_recvd
, GFP_ATOMIC
);
543 if (buffer
== NULL
) {
545 bufferlen
= packetSize
;
547 /* Try again next time around */
548 pr_err("unable to allocate buffer of size %d!",
555 put_input_device(device
);
560 static int mousevsc_connect_to_vsp(struct hv_device
*device
)
564 struct mousevsc_dev
*input_dev
;
565 struct mousevsc_prt_msg
*request
;
566 struct mousevsc_prt_msg
*response
;
568 input_dev
= get_input_device(device
);
571 pr_err("unable to get input device...device being destroyed?");
576 request
= &input_dev
->protocol_req
;
579 * Now, initiate the vsc/vsp initialization protocol on the open channel
581 memset(request
, 0, sizeof(struct mousevsc_prt_msg
));
583 request
->type
= PipeMessageData
;
584 request
->size
= sizeof(struct synthhid_protocol_request
);
586 request
->request
.header
.type
= SynthHidProtocolRequest
;
587 request
->request
.header
.size
= sizeof(unsigned int);
588 request
->request
.version_requested
.version
= SYNTHHID_INPUT_VERSION
;
590 pr_info("synthhid protocol request...");
592 ret
= vmbus_sendpacket(device
->channel
, request
,
593 sizeof(struct pipe_prt_msg
) -
594 sizeof(unsigned char) +
595 sizeof(struct synthhid_protocol_request
),
596 (unsigned long)request
,
598 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
600 pr_err("unable to send synthhid protocol request.");
604 t
= wait_for_completion_timeout(&input_dev
->wait_event
, 5*HZ
);
610 response
= &input_dev
->protocol_resp
;
612 if (!response
->response
.approved
) {
613 pr_err("synthhid protocol request failed (version %d)",
614 SYNTHHID_INPUT_VERSION
);
619 t
= wait_for_completion_timeout(&input_dev
->wait_event
, 5*HZ
);
626 * We should have gotten the device attr, hid desc and report
629 if (!input_dev
->dev_info_status
)
630 pr_info("**** input channel up and running!! ****");
635 put_input_device(device
);
640 static int mousevsc_hid_open(struct hid_device
*hid
)
645 static void mousevsc_hid_close(struct hid_device
*hid
)
649 static struct hid_ll_driver mousevsc_ll_driver
= {
650 .open
= mousevsc_hid_open
,
651 .close
= mousevsc_hid_close
,
654 static struct hid_driver mousevsc_hid_driver
;
656 static void reportdesc_callback(struct hv_device
*dev
, void *packet
, u32 len
)
658 struct hid_device
*hid_dev
;
659 struct mousevsc_dev
*input_device
= hv_get_drvdata(dev
);
661 hid_dev
= hid_allocate_device();
665 hid_dev
->ll_driver
= &mousevsc_ll_driver
;
666 hid_dev
->driver
= &mousevsc_hid_driver
;
668 if (hid_parse_report(hid_dev
, packet
, len
)) {
669 DPRINT_INFO(INPUTVSC_DRV
, "Unable to call hd_parse_report");
673 hid_dev
->bus
= BUS_VIRTUAL
;
674 hid_dev
->vendor
= input_device
->hid_dev_info
.vendor
;
675 hid_dev
->product
= input_device
->hid_dev_info
.product
;
676 hid_dev
->version
= input_device
->hid_dev_info
.version
;
678 sprintf(hid_dev
->name
, "%s", "Microsoft Vmbus HID-compliant Mouse");
680 if (!hidinput_connect(hid_dev
, 0)) {
681 hid_dev
->claimed
|= HID_CLAIMED_INPUT
;
683 input_device
->connected
= 1;
687 input_device
->hid_device
= hid_dev
;
690 static int mousevsc_on_device_add(struct hv_device
*device
,
691 void *additional_info
)
694 struct mousevsc_dev
*input_dev
;
695 struct hv_driver
*input_drv
;
697 input_dev
= alloc_input_device(device
);
704 input_dev
->init_complete
= false;
706 /* Open the channel */
707 ret
= vmbus_open(device
->channel
,
708 INPUTVSC_SEND_RING_BUFFER_SIZE
,
709 INPUTVSC_RECV_RING_BUFFER_SIZE
,
712 mousevsc_on_channel_callback
,
717 pr_err("unable to open channel: %d", ret
);
718 free_input_device(input_dev
);
722 pr_info("InputVsc channel open: %d", ret
);
724 ret
= mousevsc_connect_to_vsp(device
);
727 pr_err("unable to connect channel: %d", ret
);
729 vmbus_close(device
->channel
);
730 free_input_device(input_dev
);
734 input_drv
= drv_to_hv_drv(input_dev
->device
->device
.driver
);
738 /* Send the report desc back up */
739 /* workaround SA-167 */
740 if (input_dev
->report_desc
[14] == 0x25)
741 input_dev
->report_desc
[14] = 0x29;
743 reportdesc_callback(device
, input_dev
->report_desc
,
744 input_dev
->report_desc_size
);
746 input_dev
->init_complete
= true;
752 static int mousevsc_on_device_remove(struct hv_device
*device
)
754 struct mousevsc_dev
*input_dev
;
757 pr_info("disabling input device (%p)...",
758 hv_get_drvdata(device
));
760 input_dev
= release_input_device(device
);
764 * At this point, all outbound traffic should be disable. We only
765 * allow inbound traffic (responses) to proceed
767 * so that outstanding requests can be completed.
769 while (input_dev
->num_outstanding_req
) {
770 pr_info("waiting for %d requests to complete...",
771 input_dev
->num_outstanding_req
);
776 pr_info("removing input device (%p)...", hv_get_drvdata(device
));
778 input_dev
= final_release_input_device(device
);
780 pr_info("input device (%p) safe to remove", input_dev
);
782 /* Close the channel */
783 vmbus_close(device
->channel
);
785 free_input_device(input_dev
);
791 static int mousevsc_probe(struct hv_device
*dev
,
792 const struct hv_vmbus_device_id
*dev_id
)
797 /* Call to the vsc driver to add the device */
798 ret
= mousevsc_on_device_add(dev
, NULL
);
801 DPRINT_ERR(INPUTVSC_DRV
, "unable to add input vsc device");
809 static int mousevsc_remove(struct hv_device
*dev
)
811 struct mousevsc_dev
*input_dev
= hv_get_drvdata(dev
);
814 if (input_dev
->connected
) {
815 hidinput_disconnect(input_dev
->hid_device
);
816 input_dev
->connected
= 0;
817 hid_destroy_device(input_dev
->hid_device
);
821 * Call to the vsc driver to let it know that the device
824 ret
= mousevsc_on_device_remove(dev
);
826 DPRINT_ERR(INPUTVSC_DRV
,
827 "unable to remove vsc device (ret %d)", ret
);
833 static const struct hv_vmbus_device_id id_table
[] = {
835 { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
836 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
841 * The mouse driver is not functional; do not auto-load it.
843 /* MODULE_DEVICE_TABLE(vmbus, id_table); */
845 static struct hv_driver mousevsc_drv
= {
847 .id_table
= id_table
,
848 .probe
= mousevsc_probe
,
849 .remove
= mousevsc_remove
,
852 static int __init
mousevsc_init(void)
854 return vmbus_driver_register(&mousevsc_drv
);
857 static void __exit
mousevsc_exit(void)
859 vmbus_driver_unregister(&mousevsc_drv
);
862 MODULE_LICENSE("GPL");
863 MODULE_VERSION(HV_DRV_VERSION
);
864 module_init(mousevsc_init
);
865 module_exit(mousevsc_exit
);