Merge branch 'staging-linus' 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 #include <linux/delay.h>
28
29 #include "hv_api.h"
30 #include "logging.h"
31 #include "version_info.h"
32 #include "vmbus.h"
33 #include "vmbus_api.h"
34 #include "channel.h"
35 #include "vmbus_packet_format.h"
36
37
38 /*
39  * Data types
40  */
41 struct hv_input_dev_info {
42         unsigned short vendor;
43         unsigned short product;
44         unsigned short version;
45         char name[128];
46 };
47
48 /* Represents the input vsc driver */
49 /* FIXME - can be removed entirely */
50 struct mousevsc_drv_obj {
51         struct hv_driver Base;
52 };
53
54
55 /* The maximum size of a synthetic input message. */
56 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
57
58 /*
59  * Current version
60  *
61  * History:
62  * Beta, RC < 2008/1/22        1,0
63  * RC > 2008/1/22              2,0
64  */
65 #define SYNTHHID_INPUT_VERSION_MAJOR    2
66 #define SYNTHHID_INPUT_VERSION_MINOR    0
67 #define SYNTHHID_INPUT_VERSION          (SYNTHHID_INPUT_VERSION_MINOR | \
68                                          (SYNTHHID_INPUT_VERSION_MAJOR << 16))
69
70
71 #pragma pack(push,1)
72 /*
73  * Message types in the synthetic input protocol
74  */
75 enum synthhid_msg_type {
76         SynthHidProtocolRequest,
77         SynthHidProtocolResponse,
78         SynthHidInitialDeviceInfo,
79         SynthHidInitialDeviceInfoAck,
80         SynthHidInputReport,
81         SynthHidMax
82 };
83
84 /*
85  * Basic message structures.
86  */
87 struct synthhid_msg_hdr {
88         enum synthhid_msg_type type;
89         u32 size;
90 };
91
92 struct synthhid_msg {
93         struct synthhid_msg_hdr header;
94         char data[1]; /* Enclosed message */
95 };
96
97 union synthhid_version {
98         struct {
99                 u16 minor_version;
100                 u16 major_version;
101         };
102         u32 version;
103 };
104
105 /*
106  * Protocol messages
107  */
108 struct synthhid_protocol_request {
109         struct synthhid_msg_hdr header;
110         union synthhid_version version_requested;
111 };
112
113 struct synthhid_protocol_response {
114         struct synthhid_msg_hdr header;
115         union synthhid_version version_requested;
116         unsigned char approved;
117 };
118
119 struct synthhid_device_info {
120         struct synthhid_msg_hdr header;
121         struct hv_input_dev_info hid_dev_info;
122         struct hid_descriptor hid_descriptor;
123 };
124
125 struct synthhid_device_info_ack {
126         struct synthhid_msg_hdr header;
127         unsigned char reserved;
128 };
129
130 struct synthhid_input_report {
131         struct synthhid_msg_hdr header;
132         char buffer[1];
133 };
134
135 #pragma pack(pop)
136
137 #define INPUTVSC_SEND_RING_BUFFER_SIZE          10*PAGE_SIZE
138 #define INPUTVSC_RECV_RING_BUFFER_SIZE          10*PAGE_SIZE
139
140 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
141
142 enum pipe_prot_msg_type {
143         PipeMessageInvalid = 0,
144         PipeMessageData,
145         PipeMessageMaximum
146 };
147
148
149 struct pipe_prt_msg {
150         enum pipe_prot_msg_type type;
151         u32 size;
152         char data[1];
153 };
154
155 /*
156  * Data types
157  */
158 struct  mousevsc_prt_msg {
159         enum pipe_prot_msg_type type;
160         u32 size;
161         union {
162                 struct synthhid_protocol_request request;
163                 struct synthhid_protocol_response response;
164                 struct synthhid_device_info_ack ack;
165         };
166 };
167
168 /*
169  * Represents an mousevsc device
170  */
171 struct mousevsc_dev {
172         struct hv_device        *Device;
173         /* 0 indicates the device is being destroyed */
174         atomic_t                RefCount;
175         int                     NumOutstandingRequests;
176         unsigned char           bInitializeComplete;
177         struct mousevsc_prt_msg ProtocolReq;
178         struct mousevsc_prt_msg ProtocolResp;
179         /* Synchronize the request/response if needed */
180         wait_queue_head_t       ProtocolWaitEvent;
181         wait_queue_head_t       DeviceInfoWaitEvent;
182         int                     protocol_wait_condition;
183         int                     device_wait_condition;
184         int                     DeviceInfoStatus;
185
186         struct hid_descriptor   *HidDesc;
187         unsigned char           *ReportDesc;
188         u32                     ReportDescSize;
189         struct hv_input_dev_info hid_dev_info;
190 };
191
192
193 static const char *driver_name = "mousevsc";
194
195 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
196 static const struct hv_guid mouse_guid = {
197         .data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
198                  0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}
199 };
200
201 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info);
202 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len);
203 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len);
204
205 static struct mousevsc_dev *AllocInputDevice(struct hv_device *Device)
206 {
207         struct mousevsc_dev *inputDevice;
208
209         inputDevice = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
210
211         if (!inputDevice)
212                 return NULL;
213
214         /*
215          * Set to 2 to allow both inbound and outbound traffics
216          * (ie GetInputDevice() and MustGetInputDevice()) to proceed.
217          */
218         atomic_cmpxchg(&inputDevice->RefCount, 0, 2);
219
220         inputDevice->Device = Device;
221         Device->ext = inputDevice;
222
223         return inputDevice;
224 }
225
226 static void FreeInputDevice(struct mousevsc_dev *Device)
227 {
228         WARN_ON(atomic_read(&Device->RefCount) == 0);
229         kfree(Device);
230 }
231
232 /*
233  * Get the inputdevice object if exists and its refcount > 1
234  */
235 static struct mousevsc_dev *GetInputDevice(struct hv_device *Device)
236 {
237         struct mousevsc_dev *inputDevice;
238
239         inputDevice = (struct mousevsc_dev *)Device->ext;
240
241 /*
242  *      FIXME
243  *      This sure isn't a valid thing to print for debugging, no matter
244  *      what the intention is...
245  *
246  *      printk(KERN_ERR "-------------------------> REFCOUNT = %d",
247  *             inputDevice->RefCount);
248  */
249
250         if (inputDevice && atomic_read(&inputDevice->RefCount) > 1)
251                 atomic_inc(&inputDevice->RefCount);
252         else
253                 inputDevice = NULL;
254
255         return inputDevice;
256 }
257
258 /*
259  * Get the inputdevice object iff exists and its refcount > 0
260  */
261 static struct mousevsc_dev *MustGetInputDevice(struct hv_device *Device)
262 {
263         struct mousevsc_dev *inputDevice;
264
265         inputDevice = (struct mousevsc_dev *)Device->ext;
266
267         if (inputDevice && atomic_read(&inputDevice->RefCount))
268                 atomic_inc(&inputDevice->RefCount);
269         else
270                 inputDevice = NULL;
271
272         return inputDevice;
273 }
274
275 static void PutInputDevice(struct hv_device *Device)
276 {
277         struct mousevsc_dev *inputDevice;
278
279         inputDevice = (struct mousevsc_dev *)Device->ext;
280
281         atomic_dec(&inputDevice->RefCount);
282 }
283
284 /*
285  * Drop ref count to 1 to effectively disable GetInputDevice()
286  */
287 static struct mousevsc_dev *ReleaseInputDevice(struct hv_device *Device)
288 {
289         struct mousevsc_dev *inputDevice;
290
291         inputDevice = (struct mousevsc_dev *)Device->ext;
292
293         /* Busy wait until the ref drop to 2, then set it to 1  */
294         while (atomic_cmpxchg(&inputDevice->RefCount, 2, 1) != 2)
295                 udelay(100);
296
297         return inputDevice;
298 }
299
300 /*
301  * Drop ref count to 0. No one can use InputDevice object.
302  */
303 static struct mousevsc_dev *FinalReleaseInputDevice(struct hv_device *Device)
304 {
305         struct mousevsc_dev *inputDevice;
306
307         inputDevice = (struct mousevsc_dev *)Device->ext;
308
309         /* Busy wait until the ref drop to 1, then set it to 0  */
310         while (atomic_cmpxchg(&inputDevice->RefCount, 1, 0) != 1)
311                 udelay(100);
312
313         Device->ext = NULL;
314         return inputDevice;
315 }
316
317 static void MousevscOnSendCompletion(struct hv_device *Device, struct vmpacket_descriptor *Packet)
318 {
319         struct mousevsc_dev *inputDevice;
320         void *request;
321
322         inputDevice = MustGetInputDevice(Device);
323         if (!inputDevice) {
324                 pr_err("unable to get input device...device being destroyed?");
325                 return;
326         }
327
328         request = (void *)(unsigned long)Packet->trans_id;
329
330         if (request == &inputDevice->ProtocolReq) {
331                 /* FIXME */
332                 /* Shouldn't we be doing something here? */
333         }
334
335         PutInputDevice(Device);
336 }
337
338 static void MousevscOnReceiveDeviceInfo(struct mousevsc_dev *InputDevice, struct synthhid_device_info *DeviceInfo)
339 {
340         int ret = 0;
341         struct hid_descriptor *desc;
342         struct mousevsc_prt_msg ack;
343
344         /* Assume success for now */
345         InputDevice->DeviceInfoStatus = 0;
346
347         /* Save the device attr */
348         memcpy(&InputDevice->hid_dev_info, &DeviceInfo->hid_dev_info, sizeof(struct hv_input_dev_info));
349
350         /* Save the hid desc */
351         desc = &DeviceInfo->hid_descriptor;
352         WARN_ON(desc->bLength > 0);
353
354         InputDevice->HidDesc = kzalloc(desc->bLength, GFP_KERNEL);
355
356         if (!InputDevice->HidDesc) {
357                 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
358                 goto Cleanup;
359         }
360
361         memcpy(InputDevice->HidDesc, desc, desc->bLength);
362
363         /* Save the report desc */
364         InputDevice->ReportDescSize = desc->desc[0].wDescriptorLength;
365         InputDevice->ReportDesc = kzalloc(InputDevice->ReportDescSize,
366                                           GFP_KERNEL);
367
368         if (!InputDevice->ReportDesc) {
369                 pr_err("unable to allocate report descriptor - size %d",
370                            InputDevice->ReportDescSize);
371                 goto Cleanup;
372         }
373
374         memcpy(InputDevice->ReportDesc,
375                ((unsigned char *)desc) + desc->bLength,
376                desc->desc[0].wDescriptorLength);
377
378         /* Send the ack */
379         memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
380
381         ack.type = PipeMessageData;
382         ack.size = sizeof(struct synthhid_device_info_ack);
383
384         ack.ack.header.type = SynthHidInitialDeviceInfoAck;
385         ack.ack.header.size = 1;
386         ack.ack.reserved = 0;
387
388         ret = vmbus_sendpacket(InputDevice->Device->channel,
389                         &ack,
390                         sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
391                         sizeof(struct synthhid_device_info_ack),
392                         (unsigned long)&ack,
393                         VM_PKT_DATA_INBAND,
394                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
395         if (ret != 0) {
396                 pr_err("unable to send synthhid device info ack - ret %d",
397                            ret);
398                 goto Cleanup;
399         }
400
401         InputDevice->device_wait_condition = 1;
402         wake_up(&InputDevice->DeviceInfoWaitEvent);
403
404         return;
405
406 Cleanup:
407         kfree(InputDevice->HidDesc);
408         InputDevice->HidDesc = NULL;
409
410         kfree(InputDevice->ReportDesc);
411         InputDevice->ReportDesc = NULL;
412
413         InputDevice->DeviceInfoStatus = -1;
414         InputDevice->device_wait_condition = 1;
415         wake_up(&InputDevice->DeviceInfoWaitEvent);
416 }
417
418 static void MousevscOnReceiveInputReport(struct mousevsc_dev *InputDevice, struct synthhid_input_report *InputReport)
419 {
420         struct mousevsc_drv_obj *inputDriver;
421
422         if (!InputDevice->bInitializeComplete) {
423                 pr_info("Initialization incomplete...ignoring InputReport msg");
424                 return;
425         }
426
427         inputDriver = (struct mousevsc_drv_obj *)InputDevice->Device->drv;
428
429         inputreport_callback(InputDevice->Device,
430                              InputReport->buffer,
431                              InputReport->header.size);
432 }
433
434 static void MousevscOnReceive(struct hv_device *Device, struct vmpacket_descriptor *Packet)
435 {
436         struct pipe_prt_msg *pipeMsg;
437         struct synthhid_msg *hidMsg;
438         struct mousevsc_dev *inputDevice;
439
440         inputDevice = MustGetInputDevice(Device);
441         if (!inputDevice) {
442                 pr_err("unable to get input device...device being destroyed?");
443                 return;
444         }
445
446         pipeMsg = (struct pipe_prt_msg *)((unsigned long)Packet + (Packet->offset8 << 3));
447
448         if (pipeMsg->type != PipeMessageData) {
449                 pr_err("unknown pipe msg type - type %d len %d",
450                            pipeMsg->type, pipeMsg->size);
451                 PutInputDevice(Device);
452                 return ;
453         }
454
455         hidMsg = (struct synthhid_msg *)&pipeMsg->data[0];
456
457         switch (hidMsg->header.type) {
458         case SynthHidProtocolResponse:
459                 memcpy(&inputDevice->ProtocolResp, pipeMsg,
460                        pipeMsg->size + sizeof(struct pipe_prt_msg) -
461                        sizeof(unsigned char));
462                 inputDevice->protocol_wait_condition = 1;
463                 wake_up(&inputDevice->ProtocolWaitEvent);
464                 break;
465
466         case SynthHidInitialDeviceInfo:
467                 WARN_ON(pipeMsg->size >= sizeof(struct hv_input_dev_info));
468
469                 /*
470                  * Parse out the device info into device attr,
471                  * hid desc and report desc
472                  */
473                 MousevscOnReceiveDeviceInfo(inputDevice,
474                                             (struct synthhid_device_info *)&pipeMsg->data[0]);
475                 break;
476         case SynthHidInputReport:
477                 MousevscOnReceiveInputReport(inputDevice,
478                                              (struct synthhid_input_report *)&pipeMsg->data[0]);
479
480                 break;
481         default:
482                 pr_err("unsupported hid msg type - type %d len %d",
483                        hidMsg->header.type, hidMsg->header.size);
484                 break;
485         }
486
487         PutInputDevice(Device);
488 }
489
490 static void MousevscOnChannelCallback(void *Context)
491 {
492         const int packetSize = 0x100;
493         int ret = 0;
494         struct hv_device *device = (struct hv_device *)Context;
495         struct mousevsc_dev *inputDevice;
496
497         u32 bytesRecvd;
498         u64 requestId;
499         unsigned char packet[packetSize];
500         struct vmpacket_descriptor *desc;
501         unsigned char   *buffer = packet;
502         int     bufferlen = packetSize;
503
504         inputDevice = MustGetInputDevice(device);
505
506         if (!inputDevice) {
507                 pr_err("unable to get input device...device being destroyed?");
508                 return;
509         }
510
511         do {
512                 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen, &bytesRecvd, &requestId);
513
514                 if (ret == 0) {
515                         if (bytesRecvd > 0) {
516                                 desc = (struct vmpacket_descriptor *)buffer;
517
518                                 switch (desc->type) {
519                                         case VM_PKT_COMP:
520                                                 MousevscOnSendCompletion(device,
521                                                                          desc);
522                                                 break;
523
524                                         case VM_PKT_DATA_INBAND:
525                                                 MousevscOnReceive(device, desc);
526                                                 break;
527
528                                         default:
529                                                 pr_err("unhandled packet type %d, tid %llx len %d\n",
530                                                            desc->type,
531                                                            requestId,
532                                                            bytesRecvd);
533                                                 break;
534                                 }
535
536                                 /* reset */
537                                 if (bufferlen > packetSize) {
538                                         kfree(buffer);
539
540                                         buffer = packet;
541                                         bufferlen = packetSize;
542                                 }
543                         } else {
544                                 /*
545                                  * pr_debug("nothing else to read...");
546                                  * reset
547                                  */
548                                 if (bufferlen > packetSize) {
549                                         kfree(buffer);
550
551                                         buffer = packet;
552                                         bufferlen = packetSize;
553                                 }
554                                 break;
555                         }
556                 } else if (ret == -2) {
557                         /* Handle large packet */
558                         bufferlen = bytesRecvd;
559                         buffer = kzalloc(bytesRecvd, GFP_KERNEL);
560
561                         if (buffer == NULL) {
562                                 buffer = packet;
563                                 bufferlen = packetSize;
564
565                                 /* Try again next time around */
566                                 pr_err("unable to allocate buffer of size %d!",
567                                        bytesRecvd);
568                                 break;
569                         }
570                 }
571         } while (1);
572
573         PutInputDevice(device);
574
575         return;
576 }
577
578 static int MousevscConnectToVsp(struct hv_device *Device)
579 {
580         int ret = 0;
581         struct mousevsc_dev *inputDevice;
582         struct mousevsc_prt_msg *request;
583         struct mousevsc_prt_msg *response;
584
585         inputDevice = GetInputDevice(Device);
586
587         if (!inputDevice) {
588                 pr_err("unable to get input device...device being destroyed?");
589                 return -1;
590         }
591
592         init_waitqueue_head(&inputDevice->ProtocolWaitEvent);
593         init_waitqueue_head(&inputDevice->DeviceInfoWaitEvent);
594
595         request = &inputDevice->ProtocolReq;
596
597         /*
598          * Now, initiate the vsc/vsp initialization protocol on the open channel
599          */
600         memset(request, 0, sizeof(struct mousevsc_prt_msg));
601
602         request->type = PipeMessageData;
603         request->size = sizeof(struct synthhid_protocol_request);
604
605         request->request.header.type = SynthHidProtocolRequest;
606         request->request.header.size = sizeof(unsigned long);
607         request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
608
609         pr_info("synthhid protocol request...");
610
611         ret = vmbus_sendpacket(Device->channel, request,
612                                         sizeof(struct pipe_prt_msg) -
613                                         sizeof(unsigned char) +
614                                         sizeof(struct synthhid_protocol_request),
615                                         (unsigned long)request,
616                                         VM_PKT_DATA_INBAND,
617                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
618         if (ret != 0) {
619                 pr_err("unable to send synthhid protocol request.");
620                 goto Cleanup;
621         }
622
623         inputDevice->protocol_wait_condition = 0;
624         wait_event_timeout(inputDevice->ProtocolWaitEvent, inputDevice->protocol_wait_condition, msecs_to_jiffies(1000));
625         if (inputDevice->protocol_wait_condition == 0) {
626                 ret = -ETIMEDOUT;
627                 goto Cleanup;
628         }
629
630         response = &inputDevice->ProtocolResp;
631
632         if (!response->response.approved) {
633                 pr_err("synthhid protocol request failed (version %d)",
634                        SYNTHHID_INPUT_VERSION);
635                 ret = -1;
636                 goto Cleanup;
637         }
638
639         inputDevice->device_wait_condition = 0;
640         wait_event_timeout(inputDevice->DeviceInfoWaitEvent, inputDevice->device_wait_condition, msecs_to_jiffies(1000));
641         if (inputDevice->device_wait_condition == 0) {
642                 ret = -ETIMEDOUT;
643                 goto Cleanup;
644         }
645
646         /*
647          * We should have gotten the device attr, hid desc and report
648          * desc at this point
649          */
650         if (!inputDevice->DeviceInfoStatus)
651                 pr_info("**** input channel up and running!! ****");
652         else
653                 ret = -1;
654
655 Cleanup:
656         PutInputDevice(Device);
657
658         return ret;
659 }
660
661 static int MousevscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
662 {
663         int ret = 0;
664         struct mousevsc_dev *inputDevice;
665         struct mousevsc_drv_obj *inputDriver;
666         struct hv_input_dev_info dev_info;
667
668         inputDevice = AllocInputDevice(Device);
669
670         if (!inputDevice) {
671                 ret = -1;
672                 goto Cleanup;
673         }
674
675         inputDevice->bInitializeComplete = false;
676
677         /* Open the channel */
678         ret = vmbus_open(Device->channel,
679                 INPUTVSC_SEND_RING_BUFFER_SIZE,
680                 INPUTVSC_RECV_RING_BUFFER_SIZE,
681                 NULL,
682                 0,
683                 MousevscOnChannelCallback,
684                 Device
685                 );
686
687         if (ret != 0) {
688                 pr_err("unable to open channel: %d", ret);
689                 FreeInputDevice(inputDevice);
690                 return -1;
691         }
692
693         pr_info("InputVsc channel open: %d", ret);
694
695         ret = MousevscConnectToVsp(Device);
696
697         if (ret != 0) {
698                 pr_err("unable to connect channel: %d", ret);
699
700                 vmbus_close(Device->channel);
701                 FreeInputDevice(inputDevice);
702                 return ret;
703         }
704
705         inputDriver = (struct mousevsc_drv_obj *)inputDevice->Device->drv;
706
707         dev_info.vendor = inputDevice->hid_dev_info.vendor;
708         dev_info.product = inputDevice->hid_dev_info.product;
709         dev_info.version = inputDevice->hid_dev_info.version;
710         strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
711
712         /* Send the device info back up */
713         deviceinfo_callback(Device, &dev_info);
714
715         /* Send the report desc back up */
716         /* workaround SA-167 */
717         if (inputDevice->ReportDesc[14] == 0x25)
718                 inputDevice->ReportDesc[14] = 0x29;
719
720         reportdesc_callback(Device, inputDevice->ReportDesc,
721                             inputDevice->ReportDescSize);
722
723         inputDevice->bInitializeComplete = true;
724
725 Cleanup:
726         return ret;
727 }
728
729 static int MousevscOnDeviceRemove(struct hv_device *Device)
730 {
731         struct mousevsc_dev *inputDevice;
732         int ret = 0;
733
734         pr_info("disabling input device (%p)...",
735                     Device->ext);
736
737         inputDevice = ReleaseInputDevice(Device);
738
739
740         /*
741          * At this point, all outbound traffic should be disable. We only
742          * allow inbound traffic (responses) to proceed
743          *
744          * so that outstanding requests can be completed.
745          */
746         while (inputDevice->NumOutstandingRequests) {
747                 pr_info("waiting for %d requests to complete...", inputDevice->NumOutstandingRequests);
748
749                 udelay(100);
750         }
751
752         pr_info("removing input device (%p)...", Device->ext);
753
754         inputDevice = FinalReleaseInputDevice(Device);
755
756         pr_info("input device (%p) safe to remove", inputDevice);
757
758         /* Close the channel */
759         vmbus_close(Device->channel);
760
761         FreeInputDevice(inputDevice);
762
763         return ret;
764 }
765
766 static void MousevscOnCleanup(struct hv_driver *drv)
767 {
768 }
769
770 /*
771  * Data types
772  */
773 struct input_device_context {
774         struct hv_device        *device_ctx;
775         struct hid_device       *hid_device;
776         struct hv_input_dev_info device_info;
777         int                     connected;
778 };
779
780
781 static struct  mousevsc_drv_obj g_mousevsc_drv;
782
783 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
784 {
785         struct input_device_context *input_device_ctx =
786                 dev_get_drvdata(&dev->device);
787
788         memcpy(&input_device_ctx->device_info, info,
789                sizeof(struct hv_input_dev_info));
790
791         DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
792 }
793
794 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
795 {
796         int ret = 0;
797
798         struct input_device_context *input_dev_ctx =
799                 dev_get_drvdata(&dev->device);
800
801         ret = hid_input_report(input_dev_ctx->hid_device,
802                               HID_INPUT_REPORT, packet, len, 1);
803
804         DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
805 }
806
807 static int mousevsc_hid_open(struct hid_device *hid)
808 {
809         return 0;
810 }
811
812 static void mousevsc_hid_close(struct hid_device *hid)
813 {
814 }
815
816 static int mousevsc_probe(struct device *device)
817 {
818         int ret = 0;
819
820         struct hv_driver *drv =
821                 drv_to_hv_drv(device->driver);
822         struct mousevsc_drv_obj *mousevsc_drv_obj = drv->priv;
823
824         struct hv_device *device_obj = device_to_hv_device(device);
825         struct input_device_context *input_dev_ctx;
826
827         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
828                                 GFP_KERNEL);
829
830         dev_set_drvdata(device, input_dev_ctx);
831
832         /* Call to the vsc driver to add the device */
833         ret = mousevsc_drv_obj->Base.dev_add(device_obj, NULL);
834
835         if (ret != 0) {
836                 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
837
838                 return -1;
839         }
840
841         return 0;
842 }
843
844 static int mousevsc_remove(struct device *device)
845 {
846         int ret = 0;
847
848         struct hv_driver *drv =
849                 drv_to_hv_drv(device->driver);
850         struct mousevsc_drv_obj *mousevsc_drv_obj = drv->priv;
851
852         struct hv_device *device_obj = device_to_hv_device(device);
853         struct input_device_context *input_dev_ctx;
854
855         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
856                                 GFP_KERNEL);
857
858         dev_set_drvdata(device, input_dev_ctx);
859
860         if (input_dev_ctx->connected) {
861                 hidinput_disconnect(input_dev_ctx->hid_device);
862                 input_dev_ctx->connected = 0;
863         }
864
865         if (!mousevsc_drv_obj->Base.dev_rm)
866                 return -1;
867
868         /*
869          * Call to the vsc driver to let it know that the device
870          * is being removed
871          */
872         ret = mousevsc_drv_obj->Base.dev_rm(device_obj);
873
874         if (ret != 0) {
875                 DPRINT_ERR(INPUTVSC_DRV,
876                            "unable to remove vsc device (ret %d)", ret);
877         }
878
879         kfree(input_dev_ctx);
880
881         return ret;
882 }
883
884 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
885 {
886         struct input_device_context *input_device_ctx =
887                 dev_get_drvdata(&dev->device);
888         struct hid_device *hid_dev;
889
890         /* hid_debug = -1; */
891         hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
892
893         if (hid_parse_report(hid_dev, packet, len)) {
894                 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
895                 return;
896         }
897
898         if (hid_dev) {
899                 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
900
901                 hid_dev->ll_driver->open  = mousevsc_hid_open;
902                 hid_dev->ll_driver->close = mousevsc_hid_close;
903
904                 hid_dev->bus = BUS_VIRTUAL;
905                 hid_dev->vendor = input_device_ctx->device_info.vendor;
906                 hid_dev->product = input_device_ctx->device_info.product;
907                 hid_dev->version = input_device_ctx->device_info.version;
908                 hid_dev->dev = dev->device;
909
910                 sprintf(hid_dev->name, "%s",
911                         input_device_ctx->device_info.name);
912
913                 /*
914                  * HJ Do we want to call it with a 0
915                  */
916                 if (!hidinput_connect(hid_dev, 0)) {
917                         hid_dev->claimed |= HID_CLAIMED_INPUT;
918
919                         input_device_ctx->connected = 1;
920
921                         DPRINT_INFO(INPUTVSC_DRV,
922                                      "HID device claimed by input\n");
923                 }
924
925                 if (!hid_dev->claimed) {
926                         DPRINT_ERR(INPUTVSC_DRV,
927                                     "HID device not claimed by "
928                                     "input or hiddev\n");
929                 }
930
931                 input_device_ctx->hid_device = hid_dev;
932         }
933
934         kfree(hid_dev);
935 }
936
937 static int mousevsc_drv_exit_cb(struct device *dev, void *data)
938 {
939         struct device **curr = (struct device **)data;
940         *curr = dev;
941
942         return 1;
943 }
944
945 static void mousevsc_drv_exit(void)
946 {
947         struct mousevsc_drv_obj *mousevsc_drv_obj = &g_mousevsc_drv;
948         struct hv_driver *drv = &g_mousevsc_drv.Base;
949         int ret;
950
951         struct device *current_dev = NULL;
952
953         while (1) {
954                 current_dev = NULL;
955
956                 /* Get the device */
957                 ret = driver_for_each_device(&drv->driver, NULL,
958                                              (void *)&current_dev,
959                                              mousevsc_drv_exit_cb);
960                 if (ret)
961                         printk(KERN_ERR "Can't find mouse device!\n");
962
963                 if (current_dev == NULL)
964                         break;
965
966                 /* Initiate removal from the top-down */
967                 device_unregister(current_dev);
968         }
969
970         if (mousevsc_drv_obj->Base.cleanup)
971                 mousevsc_drv_obj->Base.cleanup(&mousevsc_drv_obj->Base);
972
973         vmbus_child_driver_unregister(&drv->driver);
974
975         return;
976 }
977
978 static int mouse_vsc_initialize(struct hv_driver *Driver)
979 {
980         struct mousevsc_drv_obj *inputDriver =
981                 (struct mousevsc_drv_obj *)Driver;
982         int ret = 0;
983
984         Driver->name = driver_name;
985         memcpy(&Driver->dev_type, &mouse_guid,
986                sizeof(struct hv_guid));
987
988         /* Setup the dispatch table */
989         inputDriver->Base.dev_add = MousevscOnDeviceAdd;
990         inputDriver->Base.dev_rm = MousevscOnDeviceRemove;
991         inputDriver->Base.cleanup = MousevscOnCleanup;
992
993         return ret;
994 }
995
996
997 static int __init mousevsc_init(void)
998 {
999         struct mousevsc_drv_obj *input_drv_obj = &g_mousevsc_drv;
1000         struct hv_driver *drv = &g_mousevsc_drv.Base;
1001
1002         DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
1003
1004         /* Callback to client driver to complete the initialization */
1005         mouse_vsc_initialize(&input_drv_obj->Base);
1006
1007         drv->driver.name = input_drv_obj->Base.name;
1008         drv->priv = input_drv_obj;
1009
1010         drv->driver.probe = mousevsc_probe;
1011         drv->driver.remove = mousevsc_remove;
1012
1013         /* The driver belongs to vmbus */
1014         vmbus_child_driver_register(&drv->driver);
1015
1016         return 0;
1017 }
1018
1019 static void __exit mousevsc_exit(void)
1020 {
1021         mousevsc_drv_exit();
1022 }
1023
1024 /*
1025  * We don't want to automatically load this driver just yet, it's quite
1026  * broken.  It's safe if you want to load it yourself manually, but
1027  * don't inflict it on unsuspecting users, that's just mean.
1028  */
1029 #if 0
1030
1031 /*
1032  * We use a PCI table to determine if we should autoload this driver  This is
1033  * needed by distro tools to determine if the hyperv drivers should be
1034  * installed and/or configured.  We don't do anything else with the table, but
1035  * it needs to be present.
1036  */
1037 const static struct pci_device_id microsoft_hv_pci_table[] = {
1038         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
1039         { 0 }
1040 };
1041 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
1042 #endif
1043
1044 MODULE_LICENSE("GPL");
1045 MODULE_VERSION(HV_DRV_VERSION);
1046 module_init(mousevsc_init);
1047 module_exit(mousevsc_exit);
1048