257684eff6e62085d3f3c6cc9d8dc8fe21c0f7c9
[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
26 #include "hyperv.h"
27
28
29 /*
30  * Data types
31  */
32 struct hv_input_dev_info {
33         unsigned short vendor;
34         unsigned short product;
35         unsigned short version;
36         char name[128];
37 };
38
39 /* The maximum size of a synthetic input message. */
40 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
41
42 /*
43  * Current version
44  *
45  * History:
46  * Beta, RC < 2008/1/22        1,0
47  * RC > 2008/1/22              2,0
48  */
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))
53
54
55 #pragma pack(push, 1)
56 /*
57  * Message types in the synthetic input protocol
58  */
59 enum synthhid_msg_type {
60         SynthHidProtocolRequest,
61         SynthHidProtocolResponse,
62         SynthHidInitialDeviceInfo,
63         SynthHidInitialDeviceInfoAck,
64         SynthHidInputReport,
65         SynthHidMax
66 };
67
68 /*
69  * Basic message structures.
70  */
71 struct synthhid_msg_hdr {
72         enum synthhid_msg_type type;
73         u32 size;
74 };
75
76 struct synthhid_msg {
77         struct synthhid_msg_hdr header;
78         char data[1]; /* Enclosed message */
79 };
80
81 union synthhid_version {
82         struct {
83                 u16 minor_version;
84                 u16 major_version;
85         };
86         u32 version;
87 };
88
89 /*
90  * Protocol messages
91  */
92 struct synthhid_protocol_request {
93         struct synthhid_msg_hdr header;
94         union synthhid_version version_requested;
95 };
96
97 struct synthhid_protocol_response {
98         struct synthhid_msg_hdr header;
99         union synthhid_version version_requested;
100         unsigned char approved;
101 };
102
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;
107 };
108
109 struct synthhid_device_info_ack {
110         struct synthhid_msg_hdr header;
111         unsigned char reserved;
112 };
113
114 struct synthhid_input_report {
115         struct synthhid_msg_hdr header;
116         char buffer[1];
117 };
118
119 #pragma pack(pop)
120
121 #define INPUTVSC_SEND_RING_BUFFER_SIZE          (10*PAGE_SIZE)
122 #define INPUTVSC_RECV_RING_BUFFER_SIZE          (10*PAGE_SIZE)
123
124 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
125
126 enum pipe_prot_msg_type {
127         PipeMessageInvalid = 0,
128         PipeMessageData,
129         PipeMessageMaximum
130 };
131
132
133 struct pipe_prt_msg {
134         enum pipe_prot_msg_type type;
135         u32 size;
136         char data[1];
137 };
138
139 /*
140  * Data types
141  */
142 struct  mousevsc_prt_msg {
143         enum pipe_prot_msg_type type;
144         u32 size;
145         union {
146                 struct synthhid_protocol_request request;
147                 struct synthhid_protocol_response response;
148                 struct synthhid_device_info_ack ack;
149         };
150 };
151
152 /*
153  * Represents an mousevsc device
154  */
155 struct mousevsc_dev {
156         struct hv_device        *device;
157         /* 0 indicates the device is being destroyed */
158         atomic_t                ref_count;
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;
168         int                     dev_info_status;
169
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;
174 };
175
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;
180         int                     connected;
181 };
182
183 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len);
184
185 static struct mousevsc_dev *alloc_input_device(struct hv_device *device)
186 {
187         struct mousevsc_dev *input_dev;
188
189         input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
190
191         if (!input_dev)
192                 return NULL;
193
194         /*
195          * Set to 2 to allow both inbound and outbound traffics
196          * (ie get_input_device() and must_get_input_device()) to proceed.
197          */
198         atomic_cmpxchg(&input_dev->ref_count, 0, 2);
199
200         input_dev->device = device;
201         device->ext = input_dev;
202
203         return input_dev;
204 }
205
206 static void free_input_device(struct mousevsc_dev *device)
207 {
208         WARN_ON(atomic_read(&device->ref_count) == 0);
209         kfree(device);
210 }
211
212 /*
213  * Get the inputdevice object if exists and its refcount > 1
214  */
215 static struct mousevsc_dev *get_input_device(struct hv_device *device)
216 {
217         struct mousevsc_dev *input_dev;
218
219         input_dev = (struct mousevsc_dev *)device->ext;
220
221 /*
222  *      FIXME
223  *      This sure isn't a valid thing to print for debugging, no matter
224  *      what the intention is...
225  *
226  *      printk(KERN_ERR "-------------------------> REFCOUNT = %d",
227  *             input_dev->ref_count);
228  */
229
230         if (input_dev && atomic_read(&input_dev->ref_count) > 1)
231                 atomic_inc(&input_dev->ref_count);
232         else
233                 input_dev = NULL;
234
235         return input_dev;
236 }
237
238 /*
239  * Get the inputdevice object iff exists and its refcount > 0
240  */
241 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
242 {
243         struct mousevsc_dev *input_dev;
244
245         input_dev = (struct mousevsc_dev *)device->ext;
246
247         if (input_dev && atomic_read(&input_dev->ref_count))
248                 atomic_inc(&input_dev->ref_count);
249         else
250                 input_dev = NULL;
251
252         return input_dev;
253 }
254
255 static void put_input_device(struct hv_device *device)
256 {
257         struct mousevsc_dev *input_dev;
258
259         input_dev = (struct mousevsc_dev *)device->ext;
260
261         atomic_dec(&input_dev->ref_count);
262 }
263
264 /*
265  * Drop ref count to 1 to effectively disable get_input_device()
266  */
267 static struct mousevsc_dev *release_input_device(struct hv_device *device)
268 {
269         struct mousevsc_dev *input_dev;
270
271         input_dev = (struct mousevsc_dev *)device->ext;
272
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)
275                 udelay(100);
276
277         return input_dev;
278 }
279
280 /*
281  * Drop ref count to 0. No one can use input_device object.
282  */
283 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
284 {
285         struct mousevsc_dev *input_dev;
286
287         input_dev = (struct mousevsc_dev *)device->ext;
288
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)
291                 udelay(100);
292
293         device->ext = NULL;
294         return input_dev;
295 }
296
297 static void mousevsc_on_send_completion(struct hv_device *device,
298                                         struct vmpacket_descriptor *packet)
299 {
300         struct mousevsc_dev *input_dev;
301         void *request;
302
303         input_dev = must_get_input_device(device);
304         if (!input_dev) {
305                 pr_err("unable to get input device...device being destroyed?");
306                 return;
307         }
308
309         request = (void *)(unsigned long)packet->trans_id;
310
311         if (request == &input_dev->protocol_req) {
312                 /* FIXME */
313                 /* Shouldn't we be doing something here? */
314         }
315
316         put_input_device(device);
317 }
318
319 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
320                                 struct synthhid_device_info *device_info)
321 {
322         int ret = 0;
323         struct hid_descriptor *desc;
324         struct mousevsc_prt_msg ack;
325
326         /* Assume success for now */
327         input_device->dev_info_status = 0;
328
329         /* Save the device attr */
330         memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
331                 sizeof(struct hv_input_dev_info));
332
333         /* Save the hid desc */
334         desc = &device_info->hid_descriptor;
335         WARN_ON(desc->bLength > 0);
336
337         input_device->hid_desc = kzalloc(desc->bLength, GFP_KERNEL);
338
339         if (!input_device->hid_desc) {
340                 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
341                 goto cleanup;
342         }
343
344         memcpy(input_device->hid_desc, desc, desc->bLength);
345
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,
349                                           GFP_KERNEL);
350
351         if (!input_device->report_desc) {
352                 pr_err("unable to allocate report descriptor - size %d",
353                            input_device->report_desc_size);
354                 goto cleanup;
355         }
356
357         memcpy(input_device->report_desc,
358                ((unsigned char *)desc) + desc->bLength,
359                desc->desc[0].wDescriptorLength);
360
361         /* Send the ack */
362         memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
363
364         ack.type = PipeMessageData;
365         ack.size = sizeof(struct synthhid_device_info_ack);
366
367         ack.ack.header.type = SynthHidInitialDeviceInfoAck;
368         ack.ack.header.size = 1;
369         ack.ack.reserved = 0;
370
371         ret = vmbus_sendpacket(input_device->device->channel,
372                         &ack,
373                         sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
374                         sizeof(struct synthhid_device_info_ack),
375                         (unsigned long)&ack,
376                         VM_PKT_DATA_INBAND,
377                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
378         if (ret != 0) {
379                 pr_err("unable to send synthhid device info ack - ret %d",
380                            ret);
381                 goto cleanup;
382         }
383
384         input_device->device_wait_condition = 1;
385         wake_up(&input_device->dev_info_wait_event);
386
387         return;
388
389 cleanup:
390         kfree(input_device->hid_desc);
391         input_device->hid_desc = NULL;
392
393         kfree(input_device->report_desc);
394         input_device->report_desc = NULL;
395
396         input_device->dev_info_status = -1;
397         input_device->device_wait_condition = 1;
398         wake_up(&input_device->dev_info_wait_event);
399 }
400
401 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
402                                 struct synthhid_input_report *input_report)
403 {
404         struct hv_driver *input_drv;
405         struct input_device_context *input_dev_ctx;
406
407         if (!input_device->init_complete) {
408                 pr_info("Initialization incomplete...ignoring input_report msg");
409                 return;
410         }
411
412         input_drv = drv_to_hv_drv(input_device->device->device.driver);
413
414         input_dev_ctx = dev_get_drvdata(&input_device->device->device);
415
416         hid_input_report(input_dev_ctx->hid_device,
417                               HID_INPUT_REPORT, input_report->buffer, input_report->header.size, 1);
418
419 }
420
421 static void mousevsc_on_receive(struct hv_device *device,
422                                 struct vmpacket_descriptor *packet)
423 {
424         struct pipe_prt_msg *pipe_msg;
425         struct synthhid_msg *hid_msg;
426         struct mousevsc_dev *input_dev;
427
428         input_dev = must_get_input_device(device);
429         if (!input_dev) {
430                 pr_err("unable to get input device...device being destroyed?");
431                 return;
432         }
433
434         pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
435                                                 (packet->offset8 << 3));
436
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);
441                 return ;
442         }
443
444         hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
445
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);
453                 break;
454
455         case SynthHidInitialDeviceInfo:
456                 WARN_ON(pipe_msg->size >= sizeof(struct hv_input_dev_info));
457
458                 /*
459                  * Parse out the device info into device attr,
460                  * hid desc and report desc
461                  */
462                 mousevsc_on_receive_device_info(input_dev,
463                         (struct synthhid_device_info *)&pipe_msg->data[0]);
464                 break;
465         case SynthHidInputReport:
466                 mousevsc_on_receive_input_report(input_dev,
467                         (struct synthhid_input_report *)&pipe_msg->data[0]);
468
469                 break;
470         default:
471                 pr_err("unsupported hid msg type - type %d len %d",
472                        hid_msg->header.type, hid_msg->header.size);
473                 break;
474         }
475
476         put_input_device(device);
477 }
478
479 static void mousevsc_on_channel_callback(void *context)
480 {
481         const int packetSize = 0x100;
482         int ret = 0;
483         struct hv_device *device = (struct hv_device *)context;
484         struct mousevsc_dev *input_dev;
485
486         u32 bytes_recvd;
487         u64 req_id;
488         unsigned char packet[0x100];
489         struct vmpacket_descriptor *desc;
490         unsigned char   *buffer = packet;
491         int     bufferlen = packetSize;
492
493         input_dev = must_get_input_device(device);
494
495         if (!input_dev) {
496                 pr_err("unable to get input device...device being destroyed?");
497                 return;
498         }
499
500         do {
501                 ret = vmbus_recvpacket_raw(device->channel, buffer,
502                                         bufferlen, &bytes_recvd, &req_id);
503
504                 if (ret == 0) {
505                         if (bytes_recvd > 0) {
506                                 desc = (struct vmpacket_descriptor *)buffer;
507
508                                 switch (desc->type) {
509                                 case VM_PKT_COMP:
510                                         mousevsc_on_send_completion(
511                                                 device, desc);
512                                         break;
513
514                                 case VM_PKT_DATA_INBAND:
515                                         mousevsc_on_receive(
516                                                 device, desc);
517                                         break;
518
519                                 default:
520                                         pr_err("unhandled packet type %d, tid %llx len %d\n",
521                                                    desc->type,
522                                                    req_id,
523                                                    bytes_recvd);
524                                         break;
525                                 }
526
527                                 /* reset */
528                                 if (bufferlen > packetSize) {
529                                         kfree(buffer);
530
531                                         buffer = packet;
532                                         bufferlen = packetSize;
533                                 }
534                         } else {
535                                 /*
536                                  * pr_debug("nothing else to read...");
537                                  * reset
538                                  */
539                                 if (bufferlen > packetSize) {
540                                         kfree(buffer);
541
542                                         buffer = packet;
543                                         bufferlen = packetSize;
544                                 }
545                                 break;
546                         }
547                 } else if (ret == -ENOBUFS) {
548                         /* Handle large packet */
549                         bufferlen = bytes_recvd;
550                         buffer = kzalloc(bytes_recvd, GFP_KERNEL);
551
552                         if (buffer == NULL) {
553                                 buffer = packet;
554                                 bufferlen = packetSize;
555
556                                 /* Try again next time around */
557                                 pr_err("unable to allocate buffer of size %d!",
558                                        bytes_recvd);
559                                 break;
560                         }
561                 }
562         } while (1);
563
564         put_input_device(device);
565
566         return;
567 }
568
569 static int mousevsc_connect_to_vsp(struct hv_device *device)
570 {
571         int ret = 0;
572         struct mousevsc_dev *input_dev;
573         struct mousevsc_prt_msg *request;
574         struct mousevsc_prt_msg *response;
575
576         input_dev = get_input_device(device);
577
578         if (!input_dev) {
579                 pr_err("unable to get input device...device being destroyed?");
580                 return -1;
581         }
582
583         init_waitqueue_head(&input_dev->protocol_wait_event);
584         init_waitqueue_head(&input_dev->dev_info_wait_event);
585
586         request = &input_dev->protocol_req;
587
588         /*
589          * Now, initiate the vsc/vsp initialization protocol on the open channel
590          */
591         memset(request, 0, sizeof(struct mousevsc_prt_msg));
592
593         request->type = PipeMessageData;
594         request->size = sizeof(struct synthhid_protocol_request);
595
596         request->request.header.type = SynthHidProtocolRequest;
597         request->request.header.size = sizeof(unsigned long);
598         request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
599
600         pr_info("synthhid protocol request...");
601
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,
607                                         VM_PKT_DATA_INBAND,
608                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
609         if (ret != 0) {
610                 pr_err("unable to send synthhid protocol request.");
611                 goto cleanup;
612         }
613
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) {
618                 ret = -ETIMEDOUT;
619                 goto cleanup;
620         }
621
622         response = &input_dev->protocol_resp;
623
624         if (!response->response.approved) {
625                 pr_err("synthhid protocol request failed (version %d)",
626                        SYNTHHID_INPUT_VERSION);
627                 ret = -1;
628                 goto cleanup;
629         }
630
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) {
635                 ret = -ETIMEDOUT;
636                 goto cleanup;
637         }
638
639         /*
640          * We should have gotten the device attr, hid desc and report
641          * desc at this point
642          */
643         if (!input_dev->dev_info_status)
644                 pr_info("**** input channel up and running!! ****");
645         else
646                 ret = -1;
647
648 cleanup:
649         put_input_device(device);
650
651         return ret;
652 }
653
654 static int mousevsc_on_device_add(struct hv_device *device,
655                                         void *additional_info)
656 {
657         int ret = 0;
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;
662
663         input_dev = alloc_input_device(device);
664
665         if (!input_dev) {
666                 ret = -1;
667                 goto cleanup;
668         }
669
670         input_dev->init_complete = false;
671
672         /* Open the channel */
673         ret = vmbus_open(device->channel,
674                 INPUTVSC_SEND_RING_BUFFER_SIZE,
675                 INPUTVSC_RECV_RING_BUFFER_SIZE,
676                 NULL,
677                 0,
678                 mousevsc_on_channel_callback,
679                 device
680                 );
681
682         if (ret != 0) {
683                 pr_err("unable to open channel: %d", ret);
684                 free_input_device(input_dev);
685                 return -1;
686         }
687
688         pr_info("InputVsc channel open: %d", ret);
689
690         ret = mousevsc_connect_to_vsp(device);
691
692         if (ret != 0) {
693                 pr_err("unable to connect channel: %d", ret);
694
695                 vmbus_close(device->channel);
696                 free_input_device(input_dev);
697                 return ret;
698         }
699
700         input_drv = drv_to_hv_drv(input_dev->device->device.driver);
701
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");
706
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));
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 static int mousevsc_hid_open(struct hid_device *hid)
766 {
767         return 0;
768 }
769
770 static void mousevsc_hid_close(struct hid_device *hid)
771 {
772 }
773
774 static int mousevsc_probe(struct hv_device *dev)
775 {
776         int ret = 0;
777
778         struct input_device_context *input_dev_ctx;
779
780         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
781                                 GFP_KERNEL);
782
783         dev_set_drvdata(&dev->device, input_dev_ctx);
784
785         /* Call to the vsc driver to add the device */
786         ret = mousevsc_on_device_add(dev, NULL);
787
788         if (ret != 0) {
789                 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
790
791                 return -1;
792         }
793
794         return 0;
795 }
796
797 static int mousevsc_remove(struct hv_device *dev)
798 {
799         int ret = 0;
800
801         struct input_device_context *input_dev_ctx;
802
803         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
804                                 GFP_KERNEL);
805
806         dev_set_drvdata(&dev->device, input_dev_ctx);
807
808         if (input_dev_ctx->connected) {
809                 hidinput_disconnect(input_dev_ctx->hid_device);
810                 input_dev_ctx->connected = 0;
811         }
812
813         /*
814          * Call to the vsc driver to let it know that the device
815          * is being removed
816          */
817         ret = mousevsc_on_device_remove(dev);
818
819         if (ret != 0) {
820                 DPRINT_ERR(INPUTVSC_DRV,
821                            "unable to remove vsc device (ret %d)", ret);
822         }
823
824         kfree(input_dev_ctx);
825
826         return ret;
827 }
828
829 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
830 {
831         struct input_device_context *input_device_ctx =
832                 dev_get_drvdata(&dev->device);
833         struct hid_device *hid_dev;
834
835         /* hid_debug = -1; */
836         hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
837
838         if (hid_parse_report(hid_dev, packet, len)) {
839                 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
840                 return;
841         }
842
843         if (hid_dev) {
844                 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
845
846                 hid_dev->ll_driver->open  = mousevsc_hid_open;
847                 hid_dev->ll_driver->close = mousevsc_hid_close;
848
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;
854
855                 sprintf(hid_dev->name, "%s",
856                         input_device_ctx->device_info.name);
857
858                 /*
859                  * HJ Do we want to call it with a 0
860                  */
861                 if (!hidinput_connect(hid_dev, 0)) {
862                         hid_dev->claimed |= HID_CLAIMED_INPUT;
863
864                         input_device_ctx->connected = 1;
865
866                         DPRINT_INFO(INPUTVSC_DRV,
867                                      "HID device claimed by input\n");
868                 }
869
870                 if (!hid_dev->claimed) {
871                         DPRINT_ERR(INPUTVSC_DRV,
872                                     "HID device not claimed by "
873                                     "input or hiddev\n");
874                 }
875
876                 input_device_ctx->hid_device = hid_dev;
877         }
878
879         kfree(hid_dev);
880 }
881
882 static const struct hv_vmbus_device_id id_table[] = {
883         /* Mouse guid */
884         { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
885                        0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
886         { },
887 };
888
889 /*
890  * The mouse driver is not functional; do not auto-load it.
891  */
892 /* MODULE_DEVICE_TABLE(vmbus, id_table); */
893
894 static struct  hv_driver mousevsc_drv = {
895         .name = "mousevsc",
896         .id_table = id_table,
897         .probe = mousevsc_probe,
898         .remove = mousevsc_remove,
899 };
900
901 static int __init mousevsc_init(void)
902 {
903         return vmbus_driver_register(&mousevsc_drv);
904 }
905
906 static void __exit mousevsc_exit(void)
907 {
908         vmbus_driver_unregister(&mousevsc_drv);
909 }
910
911 MODULE_LICENSE("GPL");
912 MODULE_VERSION(HV_DRV_VERSION);
913 module_init(mousevsc_init);
914 module_exit(mousevsc_exit);
915