Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / staging / hv / hv_mouse.c
1 /*
2  *  Copyright (c) 2009, Citrix Systems, Inc.
3  *  Copyright (c) 2010, Microsoft Corporation.
4  *  Copyright (c) 2011, Novell Inc.
5  *
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.
9  *
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
13  *  more details.
14  */
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>
27
28 #include "hyperv.h"
29
30
31 /*
32  * Data types
33  */
34 struct hv_input_dev_info {
35         unsigned short vendor;
36         unsigned short product;
37         unsigned short version;
38         char name[128];
39 };
40
41 /* The maximum size of a synthetic input message. */
42 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
43
44 /*
45  * Current version
46  *
47  * History:
48  * Beta, RC < 2008/1/22        1,0
49  * RC > 2008/1/22              2,0
50  */
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))
55
56
57 #pragma pack(push,1)
58 /*
59  * Message types in the synthetic input protocol
60  */
61 enum synthhid_msg_type {
62         SynthHidProtocolRequest,
63         SynthHidProtocolResponse,
64         SynthHidInitialDeviceInfo,
65         SynthHidInitialDeviceInfoAck,
66         SynthHidInputReport,
67         SynthHidMax
68 };
69
70 /*
71  * Basic message structures.
72  */
73 struct synthhid_msg_hdr {
74         enum synthhid_msg_type type;
75         u32 size;
76 };
77
78 struct synthhid_msg {
79         struct synthhid_msg_hdr header;
80         char data[1]; /* Enclosed message */
81 };
82
83 union synthhid_version {
84         struct {
85                 u16 minor_version;
86                 u16 major_version;
87         };
88         u32 version;
89 };
90
91 /*
92  * Protocol messages
93  */
94 struct synthhid_protocol_request {
95         struct synthhid_msg_hdr header;
96         union synthhid_version version_requested;
97 };
98
99 struct synthhid_protocol_response {
100         struct synthhid_msg_hdr header;
101         union synthhid_version version_requested;
102         unsigned char approved;
103 };
104
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;
109 };
110
111 struct synthhid_device_info_ack {
112         struct synthhid_msg_hdr header;
113         unsigned char reserved;
114 };
115
116 struct synthhid_input_report {
117         struct synthhid_msg_hdr header;
118         char buffer[1];
119 };
120
121 #pragma pack(pop)
122
123 #define INPUTVSC_SEND_RING_BUFFER_SIZE          10*PAGE_SIZE
124 #define INPUTVSC_RECV_RING_BUFFER_SIZE          10*PAGE_SIZE
125
126 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
127
128 enum pipe_prot_msg_type {
129         PipeMessageInvalid = 0,
130         PipeMessageData,
131         PipeMessageMaximum
132 };
133
134
135 struct pipe_prt_msg {
136         enum pipe_prot_msg_type type;
137         u32 size;
138         char data[1];
139 };
140
141 /*
142  * Data types
143  */
144 struct  mousevsc_prt_msg {
145         enum pipe_prot_msg_type type;
146         u32 size;
147         union {
148                 struct synthhid_protocol_request request;
149                 struct synthhid_protocol_response response;
150                 struct synthhid_device_info_ack ack;
151         };
152 };
153
154 /*
155  * Represents an mousevsc device
156  */
157 struct mousevsc_dev {
158         struct hv_device        *device;
159         /* 0 indicates the device is being destroyed */
160         atomic_t                ref_count;
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;
170         int                     dev_info_status;
171
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;
176 };
177
178
179 static const char *driver_name = "mousevsc";
180
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}
185 };
186
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);
190
191 static struct mousevsc_dev *alloc_input_device(struct hv_device *device)
192 {
193         struct mousevsc_dev *input_dev;
194
195         input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
196
197         if (!input_dev)
198                 return NULL;
199
200         /*
201          * Set to 2 to allow both inbound and outbound traffics
202          * (ie get_input_device() and must_get_input_device()) to proceed.
203          */
204         atomic_cmpxchg(&input_dev->ref_count, 0, 2);
205
206         input_dev->device = device;
207         device->ext = input_dev;
208
209         return input_dev;
210 }
211
212 static void free_input_device(struct mousevsc_dev *device)
213 {
214         WARN_ON(atomic_read(&device->ref_count) == 0);
215         kfree(device);
216 }
217
218 /*
219  * Get the inputdevice object if exists and its refcount > 1
220  */
221 static struct mousevsc_dev *get_input_device(struct hv_device *device)
222 {
223         struct mousevsc_dev *input_dev;
224
225         input_dev = (struct mousevsc_dev *)device->ext;
226
227 /*
228  *      FIXME
229  *      This sure isn't a valid thing to print for debugging, no matter
230  *      what the intention is...
231  *
232  *      printk(KERN_ERR "-------------------------> REFCOUNT = %d",
233  *             input_dev->ref_count);
234  */
235
236         if (input_dev && atomic_read(&input_dev->ref_count) > 1)
237                 atomic_inc(&input_dev->ref_count);
238         else
239                 input_dev = NULL;
240
241         return input_dev;
242 }
243
244 /*
245  * Get the inputdevice object iff exists and its refcount > 0
246  */
247 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
248 {
249         struct mousevsc_dev *input_dev;
250
251         input_dev = (struct mousevsc_dev *)device->ext;
252
253         if (input_dev && atomic_read(&input_dev->ref_count))
254                 atomic_inc(&input_dev->ref_count);
255         else
256                 input_dev = NULL;
257
258         return input_dev;
259 }
260
261 static void put_input_device(struct hv_device *device)
262 {
263         struct mousevsc_dev *input_dev;
264
265         input_dev = (struct mousevsc_dev *)device->ext;
266
267         atomic_dec(&input_dev->ref_count);
268 }
269
270 /*
271  * Drop ref count to 1 to effectively disable get_input_device()
272  */
273 static struct mousevsc_dev *release_input_device(struct hv_device *device)
274 {
275         struct mousevsc_dev *input_dev;
276
277         input_dev = (struct mousevsc_dev *)device->ext;
278
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)
281                 udelay(100);
282
283         return input_dev;
284 }
285
286 /*
287  * Drop ref count to 0. No one can use input_device object.
288  */
289 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
290 {
291         struct mousevsc_dev *input_dev;
292
293         input_dev = (struct mousevsc_dev *)device->ext;
294
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)
297                 udelay(100);
298
299         device->ext = NULL;
300         return input_dev;
301 }
302
303 static void mousevsc_on_send_completion(struct hv_device *device,
304                                         struct vmpacket_descriptor *packet)
305 {
306         struct mousevsc_dev *input_dev;
307         void *request;
308
309         input_dev = must_get_input_device(device);
310         if (!input_dev) {
311                 pr_err("unable to get input device...device being destroyed?");
312                 return;
313         }
314
315         request = (void *)(unsigned long)packet->trans_id;
316
317         if (request == &input_dev->protocol_req) {
318                 /* FIXME */
319                 /* Shouldn't we be doing something here? */
320         }
321
322         put_input_device(device);
323 }
324
325 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
326                                 struct synthhid_device_info *device_info)
327 {
328         int ret = 0;
329         struct hid_descriptor *desc;
330         struct mousevsc_prt_msg ack;
331
332         /* Assume success for now */
333         input_device->dev_info_status = 0;
334
335         /* Save the device attr */
336         memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
337                 sizeof(struct hv_input_dev_info));
338
339         /* Save the hid desc */
340         desc = &device_info->hid_descriptor;
341         WARN_ON(desc->bLength > 0);
342
343         input_device->hid_desc = kzalloc(desc->bLength, GFP_KERNEL);
344
345         if (!input_device->hid_desc) {
346                 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
347                 goto Cleanup;
348         }
349
350         memcpy(input_device->hid_desc, desc, desc->bLength);
351
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,
355                                           GFP_KERNEL);
356
357         if (!input_device->report_desc) {
358                 pr_err("unable to allocate report descriptor - size %d",
359                            input_device->report_desc_size);
360                 goto Cleanup;
361         }
362
363         memcpy(input_device->report_desc,
364                ((unsigned char *)desc) + desc->bLength,
365                desc->desc[0].wDescriptorLength);
366
367         /* Send the ack */
368         memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
369
370         ack.type = PipeMessageData;
371         ack.size = sizeof(struct synthhid_device_info_ack);
372
373         ack.ack.header.type = SynthHidInitialDeviceInfoAck;
374         ack.ack.header.size = 1;
375         ack.ack.reserved = 0;
376
377         ret = vmbus_sendpacket(input_device->device->channel,
378                         &ack,
379                         sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
380                         sizeof(struct synthhid_device_info_ack),
381                         (unsigned long)&ack,
382                         VM_PKT_DATA_INBAND,
383                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
384         if (ret != 0) {
385                 pr_err("unable to send synthhid device info ack - ret %d",
386                            ret);
387                 goto Cleanup;
388         }
389
390         input_device->device_wait_condition = 1;
391         wake_up(&input_device->dev_info_wait_event);
392
393         return;
394
395 Cleanup:
396         kfree(input_device->hid_desc);
397         input_device->hid_desc = NULL;
398
399         kfree(input_device->report_desc);
400         input_device->report_desc = NULL;
401
402         input_device->dev_info_status = -1;
403         input_device->device_wait_condition = 1;
404         wake_up(&input_device->dev_info_wait_event);
405 }
406
407 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
408                                 struct synthhid_input_report *input_report)
409 {
410         struct hv_driver *input_drv;
411
412         if (!input_device->init_complete) {
413                 pr_info("Initialization incomplete...ignoring input_report msg");
414                 return;
415         }
416
417         input_drv = drv_to_hv_drv(input_device->device->device.driver);
418
419         inputreport_callback(input_device->device,
420                              input_report->buffer,
421                              input_report->header.size);
422 }
423
424 static void mousevsc_on_receive(struct hv_device *device,
425                                 struct vmpacket_descriptor *packet)
426 {
427         struct pipe_prt_msg *pipe_msg;
428         struct synthhid_msg *hid_msg;
429         struct mousevsc_dev *input_dev;
430
431         input_dev = must_get_input_device(device);
432         if (!input_dev) {
433                 pr_err("unable to get input device...device being destroyed?");
434                 return;
435         }
436
437         pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
438                                                 (packet->offset8 << 3));
439
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);
444                 return ;
445         }
446
447         hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
448
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);
456                 break;
457
458         case SynthHidInitialDeviceInfo:
459                 WARN_ON(pipe_msg->size >= sizeof(struct hv_input_dev_info));
460
461                 /*
462                  * Parse out the device info into device attr,
463                  * hid desc and report desc
464                  */
465                 mousevsc_on_receive_device_info(input_dev,
466                         (struct synthhid_device_info *)&pipe_msg->data[0]);
467                 break;
468         case SynthHidInputReport:
469                 mousevsc_on_receive_input_report(input_dev,
470                         (struct synthhid_input_report *)&pipe_msg->data[0]);
471
472                 break;
473         default:
474                 pr_err("unsupported hid msg type - type %d len %d",
475                        hid_msg->header.type, hid_msg->header.size);
476                 break;
477         }
478
479         put_input_device(device);
480 }
481
482 static void mousevsc_on_channel_callback(void *context)
483 {
484         const int packetSize = 0x100;
485         int ret = 0;
486         struct hv_device *device = (struct hv_device *)context;
487         struct mousevsc_dev *input_dev;
488
489         u32 bytes_recvd;
490         u64 req_id;
491         unsigned char packet[0x100];
492         struct vmpacket_descriptor *desc;
493         unsigned char   *buffer = packet;
494         int     bufferlen = packetSize;
495
496         input_dev = must_get_input_device(device);
497
498         if (!input_dev) {
499                 pr_err("unable to get input device...device being destroyed?");
500                 return;
501         }
502
503         do {
504                 ret = vmbus_recvpacket_raw(device->channel, buffer,
505                                         bufferlen, &bytes_recvd, &req_id);
506
507                 if (ret == 0) {
508                         if (bytes_recvd > 0) {
509                                 desc = (struct vmpacket_descriptor *)buffer;
510
511                                 switch (desc->type) {
512                                         case VM_PKT_COMP:
513                                                 mousevsc_on_send_completion(
514                                                         device, desc);
515                                                 break;
516
517                                         case VM_PKT_DATA_INBAND:
518                                                 mousevsc_on_receive(
519                                                         device, desc);
520                                                 break;
521
522                                         default:
523                                                 pr_err("unhandled packet type %d, tid %llx len %d\n",
524                                                            desc->type,
525                                                            req_id,
526                                                            bytes_recvd);
527                                                 break;
528                                 }
529
530                                 /* reset */
531                                 if (bufferlen > packetSize) {
532                                         kfree(buffer);
533
534                                         buffer = packet;
535                                         bufferlen = packetSize;
536                                 }
537                         } else {
538                                 /*
539                                  * pr_debug("nothing else to read...");
540                                  * reset
541                                  */
542                                 if (bufferlen > packetSize) {
543                                         kfree(buffer);
544
545                                         buffer = packet;
546                                         bufferlen = packetSize;
547                                 }
548                                 break;
549                         }
550                 } else if (ret == -2) {
551                         /* Handle large packet */
552                         bufferlen = bytes_recvd;
553                         buffer = kzalloc(bytes_recvd, GFP_KERNEL);
554
555                         if (buffer == NULL) {
556                                 buffer = packet;
557                                 bufferlen = packetSize;
558
559                                 /* Try again next time around */
560                                 pr_err("unable to allocate buffer of size %d!",
561                                        bytes_recvd);
562                                 break;
563                         }
564                 }
565         } while (1);
566
567         put_input_device(device);
568
569         return;
570 }
571
572 static int mousevsc_connect_to_vsp(struct hv_device *device)
573 {
574         int ret = 0;
575         struct mousevsc_dev *input_dev;
576         struct mousevsc_prt_msg *request;
577         struct mousevsc_prt_msg *response;
578
579         input_dev = get_input_device(device);
580
581         if (!input_dev) {
582                 pr_err("unable to get input device...device being destroyed?");
583                 return -1;
584         }
585
586         init_waitqueue_head(&input_dev->protocol_wait_event);
587         init_waitqueue_head(&input_dev->dev_info_wait_event);
588
589         request = &input_dev->protocol_req;
590
591         /*
592          * Now, initiate the vsc/vsp initialization protocol on the open channel
593          */
594         memset(request, 0, sizeof(struct mousevsc_prt_msg));
595
596         request->type = PipeMessageData;
597         request->size = sizeof(struct synthhid_protocol_request);
598
599         request->request.header.type = SynthHidProtocolRequest;
600         request->request.header.size = sizeof(unsigned long);
601         request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
602
603         pr_info("synthhid protocol request...");
604
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,
610                                         VM_PKT_DATA_INBAND,
611                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
612         if (ret != 0) {
613                 pr_err("unable to send synthhid protocol request.");
614                 goto Cleanup;
615         }
616
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) {
621                 ret = -ETIMEDOUT;
622                 goto Cleanup;
623         }
624
625         response = &input_dev->protocol_resp;
626
627         if (!response->response.approved) {
628                 pr_err("synthhid protocol request failed (version %d)",
629                        SYNTHHID_INPUT_VERSION);
630                 ret = -1;
631                 goto Cleanup;
632         }
633
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) {
638                 ret = -ETIMEDOUT;
639                 goto Cleanup;
640         }
641
642         /*
643          * We should have gotten the device attr, hid desc and report
644          * desc at this point
645          */
646         if (!input_dev->dev_info_status)
647                 pr_info("**** input channel up and running!! ****");
648         else
649                 ret = -1;
650
651 Cleanup:
652         put_input_device(device);
653
654         return ret;
655 }
656
657 static int mousevsc_on_device_add(struct hv_device *device,
658                                         void *additional_info)
659 {
660         int ret = 0;
661         struct mousevsc_dev *input_dev;
662         struct hv_driver *input_drv;
663         struct hv_input_dev_info dev_info;
664
665         input_dev = alloc_input_device(device);
666
667         if (!input_dev) {
668                 ret = -1;
669                 goto Cleanup;
670         }
671
672         input_dev->init_complete = false;
673
674         /* Open the channel */
675         ret = vmbus_open(device->channel,
676                 INPUTVSC_SEND_RING_BUFFER_SIZE,
677                 INPUTVSC_RECV_RING_BUFFER_SIZE,
678                 NULL,
679                 0,
680                 mousevsc_on_channel_callback,
681                 device
682                 );
683
684         if (ret != 0) {
685                 pr_err("unable to open channel: %d", ret);
686                 free_input_device(input_dev);
687                 return -1;
688         }
689
690         pr_info("InputVsc channel open: %d", ret);
691
692         ret = mousevsc_connect_to_vsp(device);
693
694         if (ret != 0) {
695                 pr_err("unable to connect channel: %d", ret);
696
697                 vmbus_close(device->channel);
698                 free_input_device(input_dev);
699                 return ret;
700         }
701
702         input_drv = drv_to_hv_drv(input_dev->device->device.driver);
703
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");
708
709         /* Send the device info back up */
710         deviceinfo_callback(device, &dev_info);
711
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;
716
717         reportdesc_callback(device, input_dev->report_desc,
718                             input_dev->report_desc_size);
719
720         input_dev->init_complete = true;
721
722 Cleanup:
723         return ret;
724 }
725
726 static int mousevsc_on_device_remove(struct hv_device *device)
727 {
728         struct mousevsc_dev *input_dev;
729         int ret = 0;
730
731         pr_info("disabling input device (%p)...",
732                     device->ext);
733
734         input_dev = release_input_device(device);
735
736
737         /*
738          * At this point, all outbound traffic should be disable. We only
739          * allow inbound traffic (responses) to proceed
740          *
741          * so that outstanding requests can be completed.
742          */
743         while (input_dev->num_outstanding_req) {
744                 pr_info("waiting for %d requests to complete...",
745                         input_dev->num_outstanding_req);
746
747                 udelay(100);
748         }
749
750         pr_info("removing input device (%p)...", device->ext);
751
752         input_dev = final_release_input_device(device);
753
754         pr_info("input device (%p) safe to remove", input_dev);
755
756         /* Close the channel */
757         vmbus_close(device->channel);
758
759         free_input_device(input_dev);
760
761         return ret;
762 }
763
764
765 /*
766  * Data types
767  */
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;
772         int                     connected;
773 };
774
775
776 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
777 {
778         struct input_device_context *input_device_ctx =
779                 dev_get_drvdata(&dev->device);
780
781         memcpy(&input_device_ctx->device_info, info,
782                sizeof(struct hv_input_dev_info));
783
784         DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
785 }
786
787 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
788 {
789         int ret = 0;
790
791         struct input_device_context *input_dev_ctx =
792                 dev_get_drvdata(&dev->device);
793
794         ret = hid_input_report(input_dev_ctx->hid_device,
795                               HID_INPUT_REPORT, packet, len, 1);
796
797         DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
798 }
799
800 static int mousevsc_hid_open(struct hid_device *hid)
801 {
802         return 0;
803 }
804
805 static void mousevsc_hid_close(struct hid_device *hid)
806 {
807 }
808
809 static int mousevsc_probe(struct hv_device *dev)
810 {
811         int ret = 0;
812
813         struct input_device_context *input_dev_ctx;
814
815         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
816                                 GFP_KERNEL);
817
818         dev_set_drvdata(&dev->device, input_dev_ctx);
819
820         /* Call to the vsc driver to add the device */
821         ret = mousevsc_on_device_add(dev, NULL);
822
823         if (ret != 0) {
824                 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
825
826                 return -1;
827         }
828
829         return 0;
830 }
831
832 static int mousevsc_remove(struct hv_device *dev)
833 {
834         int ret = 0;
835
836         struct input_device_context *input_dev_ctx;
837
838         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
839                                 GFP_KERNEL);
840
841         dev_set_drvdata(&dev->device, input_dev_ctx);
842
843         if (input_dev_ctx->connected) {
844                 hidinput_disconnect(input_dev_ctx->hid_device);
845                 input_dev_ctx->connected = 0;
846         }
847
848         /*
849          * Call to the vsc driver to let it know that the device
850          * is being removed
851          */
852         ret = mousevsc_on_device_remove(dev);
853
854         if (ret != 0) {
855                 DPRINT_ERR(INPUTVSC_DRV,
856                            "unable to remove vsc device (ret %d)", ret);
857         }
858
859         kfree(input_dev_ctx);
860
861         return ret;
862 }
863
864 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
865 {
866         struct input_device_context *input_device_ctx =
867                 dev_get_drvdata(&dev->device);
868         struct hid_device *hid_dev;
869
870         /* hid_debug = -1; */
871         hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
872
873         if (hid_parse_report(hid_dev, packet, len)) {
874                 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
875                 return;
876         }
877
878         if (hid_dev) {
879                 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
880
881                 hid_dev->ll_driver->open  = mousevsc_hid_open;
882                 hid_dev->ll_driver->close = mousevsc_hid_close;
883
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;
889
890                 sprintf(hid_dev->name, "%s",
891                         input_device_ctx->device_info.name);
892
893                 /*
894                  * HJ Do we want to call it with a 0
895                  */
896                 if (!hidinput_connect(hid_dev, 0)) {
897                         hid_dev->claimed |= HID_CLAIMED_INPUT;
898
899                         input_device_ctx->connected = 1;
900
901                         DPRINT_INFO(INPUTVSC_DRV,
902                                      "HID device claimed by input\n");
903                 }
904
905                 if (!hid_dev->claimed) {
906                         DPRINT_ERR(INPUTVSC_DRV,
907                                     "HID device not claimed by "
908                                     "input or hiddev\n");
909                 }
910
911                 input_device_ctx->hid_device = hid_dev;
912         }
913
914         kfree(hid_dev);
915 }
916
917
918 static struct  hv_driver mousevsc_drv = {
919         .probe = mousevsc_probe,
920         .remove = mousevsc_remove,
921 };
922
923 static void mousevsc_drv_exit(void)
924 {
925         vmbus_child_driver_unregister(&mousevsc_drv.driver);
926 }
927
928 static int __init mousevsc_init(void)
929 {
930         struct hv_driver *drv = &mousevsc_drv;
931
932         DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
933
934         memcpy(&drv->dev_type, &mouse_guid,
935                sizeof(struct hv_guid));
936
937         drv->driver.name = driver_name;
938
939         /* The driver belongs to vmbus */
940         vmbus_child_driver_register(&drv->driver);
941
942         return 0;
943 }
944
945 static void __exit mousevsc_exit(void)
946 {
947         mousevsc_drv_exit();
948 }
949
950 /*
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.
954  */
955 #if 0
956
957 /*
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.
962  */
963 const static struct pci_device_id microsoft_hv_pci_table[] = {
964         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
965         { 0 }
966 };
967 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
968 #endif
969
970 MODULE_LICENSE("GPL");
971 MODULE_VERSION(HV_DRV_VERSION);
972 module_init(mousevsc_init);
973 module_exit(mousevsc_exit);
974