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