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