090736af8dff087b93621bda04f27fdd93019fb2
[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 struct mousevsc_dev *alloc_input_device(struct hv_device *device)
184 {
185         struct mousevsc_dev *input_dev;
186
187         input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
188
189         if (!input_dev)
190                 return NULL;
191
192         /*
193          * Set to 2 to allow both inbound and outbound traffics
194          * (ie get_input_device() and must_get_input_device()) to proceed.
195          */
196         atomic_cmpxchg(&input_dev->ref_count, 0, 2);
197
198         input_dev->device = device;
199         device->ext = input_dev;
200
201         return input_dev;
202 }
203
204 static void free_input_device(struct mousevsc_dev *device)
205 {
206         WARN_ON(atomic_read(&device->ref_count) == 0);
207         kfree(device);
208 }
209
210 /*
211  * Get the inputdevice object if exists and its refcount > 1
212  */
213 static struct mousevsc_dev *get_input_device(struct hv_device *device)
214 {
215         struct mousevsc_dev *input_dev;
216
217         input_dev = (struct mousevsc_dev *)device->ext;
218
219 /*
220  *      FIXME
221  *      This sure isn't a valid thing to print for debugging, no matter
222  *      what the intention is...
223  *
224  *      printk(KERN_ERR "-------------------------> REFCOUNT = %d",
225  *             input_dev->ref_count);
226  */
227
228         if (input_dev && atomic_read(&input_dev->ref_count) > 1)
229                 atomic_inc(&input_dev->ref_count);
230         else
231                 input_dev = NULL;
232
233         return input_dev;
234 }
235
236 /*
237  * Get the inputdevice object iff exists and its refcount > 0
238  */
239 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
240 {
241         struct mousevsc_dev *input_dev;
242
243         input_dev = (struct mousevsc_dev *)device->ext;
244
245         if (input_dev && atomic_read(&input_dev->ref_count))
246                 atomic_inc(&input_dev->ref_count);
247         else
248                 input_dev = NULL;
249
250         return input_dev;
251 }
252
253 static void put_input_device(struct hv_device *device)
254 {
255         struct mousevsc_dev *input_dev;
256
257         input_dev = (struct mousevsc_dev *)device->ext;
258
259         atomic_dec(&input_dev->ref_count);
260 }
261
262 /*
263  * Drop ref count to 1 to effectively disable get_input_device()
264  */
265 static struct mousevsc_dev *release_input_device(struct hv_device *device)
266 {
267         struct mousevsc_dev *input_dev;
268
269         input_dev = (struct mousevsc_dev *)device->ext;
270
271         /* Busy wait until the ref drop to 2, then set it to 1  */
272         while (atomic_cmpxchg(&input_dev->ref_count, 2, 1) != 2)
273                 udelay(100);
274
275         return input_dev;
276 }
277
278 /*
279  * Drop ref count to 0. No one can use input_device object.
280  */
281 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
282 {
283         struct mousevsc_dev *input_dev;
284
285         input_dev = (struct mousevsc_dev *)device->ext;
286
287         /* Busy wait until the ref drop to 1, then set it to 0  */
288         while (atomic_cmpxchg(&input_dev->ref_count, 1, 0) != 1)
289                 udelay(100);
290
291         device->ext = NULL;
292         return input_dev;
293 }
294
295 static void mousevsc_on_send_completion(struct hv_device *device,
296                                         struct vmpacket_descriptor *packet)
297 {
298         struct mousevsc_dev *input_dev;
299         void *request;
300
301         input_dev = must_get_input_device(device);
302         if (!input_dev) {
303                 pr_err("unable to get input device...device being destroyed?");
304                 return;
305         }
306
307         request = (void *)(unsigned long)packet->trans_id;
308
309         if (request == &input_dev->protocol_req) {
310                 /* FIXME */
311                 /* Shouldn't we be doing something here? */
312         }
313
314         put_input_device(device);
315 }
316
317 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
318                                 struct synthhid_device_info *device_info)
319 {
320         int ret = 0;
321         struct hid_descriptor *desc;
322         struct mousevsc_prt_msg ack;
323
324         /* Assume success for now */
325         input_device->dev_info_status = 0;
326
327         /* Save the device attr */
328         memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
329                 sizeof(struct hv_input_dev_info));
330
331         /* Save the hid desc */
332         desc = &device_info->hid_descriptor;
333         WARN_ON(desc->bLength > 0);
334
335         input_device->hid_desc = kzalloc(desc->bLength, GFP_KERNEL);
336
337         if (!input_device->hid_desc) {
338                 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
339                 goto cleanup;
340         }
341
342         memcpy(input_device->hid_desc, desc, desc->bLength);
343
344         /* Save the report desc */
345         input_device->report_desc_size = desc->desc[0].wDescriptorLength;
346         input_device->report_desc = kzalloc(input_device->report_desc_size,
347                                           GFP_KERNEL);
348
349         if (!input_device->report_desc) {
350                 pr_err("unable to allocate report descriptor - size %d",
351                            input_device->report_desc_size);
352                 goto cleanup;
353         }
354
355         memcpy(input_device->report_desc,
356                ((unsigned char *)desc) + desc->bLength,
357                desc->desc[0].wDescriptorLength);
358
359         /* Send the ack */
360         memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
361
362         ack.type = PipeMessageData;
363         ack.size = sizeof(struct synthhid_device_info_ack);
364
365         ack.ack.header.type = SynthHidInitialDeviceInfoAck;
366         ack.ack.header.size = 1;
367         ack.ack.reserved = 0;
368
369         ret = vmbus_sendpacket(input_device->device->channel,
370                         &ack,
371                         sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
372                         sizeof(struct synthhid_device_info_ack),
373                         (unsigned long)&ack,
374                         VM_PKT_DATA_INBAND,
375                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
376         if (ret != 0) {
377                 pr_err("unable to send synthhid device info ack - ret %d",
378                            ret);
379                 goto cleanup;
380         }
381
382         input_device->device_wait_condition = 1;
383         wake_up(&input_device->dev_info_wait_event);
384
385         return;
386
387 cleanup:
388         kfree(input_device->hid_desc);
389         input_device->hid_desc = NULL;
390
391         kfree(input_device->report_desc);
392         input_device->report_desc = NULL;
393
394         input_device->dev_info_status = -1;
395         input_device->device_wait_condition = 1;
396         wake_up(&input_device->dev_info_wait_event);
397 }
398
399 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
400                                 struct synthhid_input_report *input_report)
401 {
402         struct hv_driver *input_drv;
403         struct input_device_context *input_dev_ctx;
404
405         if (!input_device->init_complete) {
406                 pr_info("Initialization incomplete...ignoring input_report msg");
407                 return;
408         }
409
410         input_drv = drv_to_hv_drv(input_device->device->device.driver);
411
412         input_dev_ctx = dev_get_drvdata(&input_device->device->device);
413
414         hid_input_report(input_dev_ctx->hid_device,
415                               HID_INPUT_REPORT, input_report->buffer, input_report->header.size, 1);
416
417 }
418
419 static void mousevsc_on_receive(struct hv_device *device,
420                                 struct vmpacket_descriptor *packet)
421 {
422         struct pipe_prt_msg *pipe_msg;
423         struct synthhid_msg *hid_msg;
424         struct mousevsc_dev *input_dev;
425
426         input_dev = must_get_input_device(device);
427         if (!input_dev) {
428                 pr_err("unable to get input device...device being destroyed?");
429                 return;
430         }
431
432         pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
433                                                 (packet->offset8 << 3));
434
435         if (pipe_msg->type != PipeMessageData) {
436                 pr_err("unknown pipe msg type - type %d len %d",
437                            pipe_msg->type, pipe_msg->size);
438                 put_input_device(device);
439                 return ;
440         }
441
442         hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
443
444         switch (hid_msg->header.type) {
445         case SynthHidProtocolResponse:
446                 memcpy(&input_dev->protocol_resp, pipe_msg,
447                        pipe_msg->size + sizeof(struct pipe_prt_msg) -
448                        sizeof(unsigned char));
449                 input_dev->protocol_wait_condition = 1;
450                 wake_up(&input_dev->protocol_wait_event);
451                 break;
452
453         case SynthHidInitialDeviceInfo:
454                 WARN_ON(pipe_msg->size >= sizeof(struct hv_input_dev_info));
455
456                 /*
457                  * Parse out the device info into device attr,
458                  * hid desc and report desc
459                  */
460                 mousevsc_on_receive_device_info(input_dev,
461                         (struct synthhid_device_info *)&pipe_msg->data[0]);
462                 break;
463         case SynthHidInputReport:
464                 mousevsc_on_receive_input_report(input_dev,
465                         (struct synthhid_input_report *)&pipe_msg->data[0]);
466
467                 break;
468         default:
469                 pr_err("unsupported hid msg type - type %d len %d",
470                        hid_msg->header.type, hid_msg->header.size);
471                 break;
472         }
473
474         put_input_device(device);
475 }
476
477 static void mousevsc_on_channel_callback(void *context)
478 {
479         const int packetSize = 0x100;
480         int ret = 0;
481         struct hv_device *device = (struct hv_device *)context;
482         struct mousevsc_dev *input_dev;
483
484         u32 bytes_recvd;
485         u64 req_id;
486         unsigned char packet[0x100];
487         struct vmpacket_descriptor *desc;
488         unsigned char   *buffer = packet;
489         int     bufferlen = packetSize;
490
491         input_dev = must_get_input_device(device);
492
493         if (!input_dev) {
494                 pr_err("unable to get input device...device being destroyed?");
495                 return;
496         }
497
498         do {
499                 ret = vmbus_recvpacket_raw(device->channel, buffer,
500                                         bufferlen, &bytes_recvd, &req_id);
501
502                 if (ret == 0) {
503                         if (bytes_recvd > 0) {
504                                 desc = (struct vmpacket_descriptor *)buffer;
505
506                                 switch (desc->type) {
507                                 case VM_PKT_COMP:
508                                         mousevsc_on_send_completion(
509                                                 device, desc);
510                                         break;
511
512                                 case VM_PKT_DATA_INBAND:
513                                         mousevsc_on_receive(
514                                                 device, desc);
515                                         break;
516
517                                 default:
518                                         pr_err("unhandled packet type %d, tid %llx len %d\n",
519                                                    desc->type,
520                                                    req_id,
521                                                    bytes_recvd);
522                                         break;
523                                 }
524
525                                 /* reset */
526                                 if (bufferlen > packetSize) {
527                                         kfree(buffer);
528
529                                         buffer = packet;
530                                         bufferlen = packetSize;
531                                 }
532                         } else {
533                                 /*
534                                  * pr_debug("nothing else to read...");
535                                  * reset
536                                  */
537                                 if (bufferlen > packetSize) {
538                                         kfree(buffer);
539
540                                         buffer = packet;
541                                         bufferlen = packetSize;
542                                 }
543                                 break;
544                         }
545                 } else if (ret == -ENOBUFS) {
546                         /* Handle large packet */
547                         bufferlen = bytes_recvd;
548                         buffer = kzalloc(bytes_recvd, GFP_KERNEL);
549
550                         if (buffer == NULL) {
551                                 buffer = packet;
552                                 bufferlen = packetSize;
553
554                                 /* Try again next time around */
555                                 pr_err("unable to allocate buffer of size %d!",
556                                        bytes_recvd);
557                                 break;
558                         }
559                 }
560         } while (1);
561
562         put_input_device(device);
563
564         return;
565 }
566
567 static int mousevsc_connect_to_vsp(struct hv_device *device)
568 {
569         int ret = 0;
570         struct mousevsc_dev *input_dev;
571         struct mousevsc_prt_msg *request;
572         struct mousevsc_prt_msg *response;
573
574         input_dev = get_input_device(device);
575
576         if (!input_dev) {
577                 pr_err("unable to get input device...device being destroyed?");
578                 return -1;
579         }
580
581         init_waitqueue_head(&input_dev->protocol_wait_event);
582         init_waitqueue_head(&input_dev->dev_info_wait_event);
583
584         request = &input_dev->protocol_req;
585
586         /*
587          * Now, initiate the vsc/vsp initialization protocol on the open channel
588          */
589         memset(request, 0, sizeof(struct mousevsc_prt_msg));
590
591         request->type = PipeMessageData;
592         request->size = sizeof(struct synthhid_protocol_request);
593
594         request->request.header.type = SynthHidProtocolRequest;
595         request->request.header.size = sizeof(unsigned long);
596         request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
597
598         pr_info("synthhid protocol request...");
599
600         ret = vmbus_sendpacket(device->channel, request,
601                                         sizeof(struct pipe_prt_msg) -
602                                         sizeof(unsigned char) +
603                                         sizeof(struct synthhid_protocol_request),
604                                         (unsigned long)request,
605                                         VM_PKT_DATA_INBAND,
606                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
607         if (ret != 0) {
608                 pr_err("unable to send synthhid protocol request.");
609                 goto cleanup;
610         }
611
612         input_dev->protocol_wait_condition = 0;
613         wait_event_timeout(input_dev->protocol_wait_event,
614                 input_dev->protocol_wait_condition, msecs_to_jiffies(1000));
615         if (input_dev->protocol_wait_condition == 0) {
616                 ret = -ETIMEDOUT;
617                 goto cleanup;
618         }
619
620         response = &input_dev->protocol_resp;
621
622         if (!response->response.approved) {
623                 pr_err("synthhid protocol request failed (version %d)",
624                        SYNTHHID_INPUT_VERSION);
625                 ret = -1;
626                 goto cleanup;
627         }
628
629         input_dev->device_wait_condition = 0;
630         wait_event_timeout(input_dev->dev_info_wait_event,
631                 input_dev->device_wait_condition, msecs_to_jiffies(1000));
632         if (input_dev->device_wait_condition == 0) {
633                 ret = -ETIMEDOUT;
634                 goto cleanup;
635         }
636
637         /*
638          * We should have gotten the device attr, hid desc and report
639          * desc at this point
640          */
641         if (!input_dev->dev_info_status)
642                 pr_info("**** input channel up and running!! ****");
643         else
644                 ret = -1;
645
646 cleanup:
647         put_input_device(device);
648
649         return ret;
650 }
651
652 static int mousevsc_hid_open(struct hid_device *hid)
653 {
654         return 0;
655 }
656
657 static void mousevsc_hid_close(struct hid_device *hid)
658 {
659 }
660
661 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
662 {
663         struct input_device_context *input_device_ctx =
664                 dev_get_drvdata(&dev->device);
665         struct hid_device *hid_dev;
666
667         /* hid_debug = -1; */
668         hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
669
670         if (hid_parse_report(hid_dev, packet, len)) {
671                 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
672                 return;
673         }
674
675         if (hid_dev) {
676                 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
677
678                 hid_dev->ll_driver->open  = mousevsc_hid_open;
679                 hid_dev->ll_driver->close = mousevsc_hid_close;
680
681                 hid_dev->bus = BUS_VIRTUAL;
682                 hid_dev->vendor = input_device_ctx->device_info.vendor;
683                 hid_dev->product = input_device_ctx->device_info.product;
684                 hid_dev->version = input_device_ctx->device_info.version;
685                 hid_dev->dev = dev->device;
686
687                 sprintf(hid_dev->name, "%s",
688                         input_device_ctx->device_info.name);
689
690                 /*
691                  * HJ Do we want to call it with a 0
692                  */
693                 if (!hidinput_connect(hid_dev, 0)) {
694                         hid_dev->claimed |= HID_CLAIMED_INPUT;
695
696                         input_device_ctx->connected = 1;
697
698                         DPRINT_INFO(INPUTVSC_DRV,
699                                      "HID device claimed by input\n");
700                 }
701
702                 if (!hid_dev->claimed) {
703                         DPRINT_ERR(INPUTVSC_DRV,
704                                     "HID device not claimed by "
705                                     "input or hiddev\n");
706                 }
707
708                 input_device_ctx->hid_device = hid_dev;
709         }
710
711         kfree(hid_dev);
712 }
713
714 static int mousevsc_on_device_add(struct hv_device *device,
715                                         void *additional_info)
716 {
717         int ret = 0;
718         struct mousevsc_dev *input_dev;
719         struct hv_driver *input_drv;
720         struct hv_input_dev_info dev_info;
721         struct input_device_context *input_device_ctx;
722
723         input_dev = alloc_input_device(device);
724
725         if (!input_dev) {
726                 ret = -1;
727                 goto cleanup;
728         }
729
730         input_dev->init_complete = false;
731
732         /* Open the channel */
733         ret = vmbus_open(device->channel,
734                 INPUTVSC_SEND_RING_BUFFER_SIZE,
735                 INPUTVSC_RECV_RING_BUFFER_SIZE,
736                 NULL,
737                 0,
738                 mousevsc_on_channel_callback,
739                 device
740                 );
741
742         if (ret != 0) {
743                 pr_err("unable to open channel: %d", ret);
744                 free_input_device(input_dev);
745                 return -1;
746         }
747
748         pr_info("InputVsc channel open: %d", ret);
749
750         ret = mousevsc_connect_to_vsp(device);
751
752         if (ret != 0) {
753                 pr_err("unable to connect channel: %d", ret);
754
755                 vmbus_close(device->channel);
756                 free_input_device(input_dev);
757                 return ret;
758         }
759
760         input_drv = drv_to_hv_drv(input_dev->device->device.driver);
761
762         dev_info.vendor = input_dev->hid_dev_info.vendor;
763         dev_info.product = input_dev->hid_dev_info.product;
764         dev_info.version = input_dev->hid_dev_info.version;
765         strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
766
767         /* Send the device info back up */
768         input_device_ctx = dev_get_drvdata(&device->device);
769         memcpy(&input_device_ctx->device_info, &dev_info,
770                sizeof(struct hv_input_dev_info));
771
772         /* Send the report desc back up */
773         /* workaround SA-167 */
774         if (input_dev->report_desc[14] == 0x25)
775                 input_dev->report_desc[14] = 0x29;
776
777         reportdesc_callback(device, input_dev->report_desc,
778                             input_dev->report_desc_size);
779
780         input_dev->init_complete = true;
781
782 cleanup:
783         return ret;
784 }
785
786 static int mousevsc_on_device_remove(struct hv_device *device)
787 {
788         struct mousevsc_dev *input_dev;
789         int ret = 0;
790
791         pr_info("disabling input device (%p)...",
792                     device->ext);
793
794         input_dev = release_input_device(device);
795
796
797         /*
798          * At this point, all outbound traffic should be disable. We only
799          * allow inbound traffic (responses) to proceed
800          *
801          * so that outstanding requests can be completed.
802          */
803         while (input_dev->num_outstanding_req) {
804                 pr_info("waiting for %d requests to complete...",
805                         input_dev->num_outstanding_req);
806
807                 udelay(100);
808         }
809
810         pr_info("removing input device (%p)...", device->ext);
811
812         input_dev = final_release_input_device(device);
813
814         pr_info("input device (%p) safe to remove", input_dev);
815
816         /* Close the channel */
817         vmbus_close(device->channel);
818
819         free_input_device(input_dev);
820
821         return ret;
822 }
823
824
825 static int mousevsc_probe(struct hv_device *dev)
826 {
827         int ret = 0;
828
829         struct input_device_context *input_dev_ctx;
830
831         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
832                                 GFP_KERNEL);
833
834         dev_set_drvdata(&dev->device, input_dev_ctx);
835
836         /* Call to the vsc driver to add the device */
837         ret = mousevsc_on_device_add(dev, NULL);
838
839         if (ret != 0) {
840                 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
841
842                 return -1;
843         }
844
845         return 0;
846 }
847
848 static int mousevsc_remove(struct hv_device *dev)
849 {
850         struct input_device_context *input_dev_ctx;
851         int ret;
852
853         input_dev_ctx = dev_get_drvdata(&dev->device);
854         if (input_dev_ctx->connected) {
855                 hidinput_disconnect(input_dev_ctx->hid_device);
856                 input_dev_ctx->connected = 0;
857         }
858
859         /*
860          * Call to the vsc driver to let it know that the device
861          * is being removed
862          */
863         ret = mousevsc_on_device_remove(dev);
864         if (ret != 0) {
865                 DPRINT_ERR(INPUTVSC_DRV,
866                            "unable to remove vsc device (ret %d)", ret);
867         }
868
869         kfree(input_dev_ctx);
870
871         return ret;
872 }
873
874 static const struct hv_vmbus_device_id id_table[] = {
875         /* Mouse guid */
876         { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
877                        0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
878         { },
879 };
880
881 /*
882  * The mouse driver is not functional; do not auto-load it.
883  */
884 /* MODULE_DEVICE_TABLE(vmbus, id_table); */
885
886 static struct  hv_driver mousevsc_drv = {
887         .name = "mousevsc",
888         .id_table = id_table,
889         .probe = mousevsc_probe,
890         .remove = mousevsc_remove,
891 };
892
893 static int __init mousevsc_init(void)
894 {
895         return vmbus_driver_register(&mousevsc_drv);
896 }
897
898 static void __exit mousevsc_exit(void)
899 {
900         vmbus_driver_unregister(&mousevsc_drv);
901 }
902
903 MODULE_LICENSE("GPL");
904 MODULE_VERSION(HV_DRV_VERSION);
905 module_init(mousevsc_init);
906 module_exit(mousevsc_exit);
907