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