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