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