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