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