Staging: hv: Use generic device_driver remove function
[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         if (InputDevice->HidDesc) {
406                 kfree(InputDevice->HidDesc);
407                 InputDevice->HidDesc = NULL;
408         }
409
410         if (InputDevice->ReportDesc) {
411                 kfree(InputDevice->ReportDesc);
412                 InputDevice->ReportDesc = NULL;
413         }
414
415         InputDevice->DeviceInfoStatus = -1;
416         InputDevice->device_wait_condition = 1;
417         wake_up(&InputDevice->DeviceInfoWaitEvent);
418 }
419
420 static void MousevscOnReceiveInputReport(struct mousevsc_dev *InputDevice, struct synthhid_input_report *InputReport)
421 {
422         struct mousevsc_drv_obj *inputDriver;
423
424         if (!InputDevice->bInitializeComplete) {
425                 pr_info("Initialization incomplete...ignoring InputReport msg");
426                 return;
427         }
428
429         inputDriver = (struct mousevsc_drv_obj *)InputDevice->Device->drv;
430
431         inputreport_callback(InputDevice->Device,
432                              InputReport->buffer,
433                              InputReport->header.size);
434 }
435
436 static void MousevscOnReceive(struct hv_device *Device, struct vmpacket_descriptor *Packet)
437 {
438         struct pipe_prt_msg *pipeMsg;
439         struct synthhid_msg *hidMsg;
440         struct mousevsc_dev *inputDevice;
441
442         inputDevice = MustGetInputDevice(Device);
443         if (!inputDevice) {
444                 pr_err("unable to get input device...device being destroyed?");
445                 return;
446         }
447
448         pipeMsg = (struct pipe_prt_msg *)((unsigned long)Packet + (Packet->offset8 << 3));
449
450         if (pipeMsg->type != PipeMessageData) {
451                 pr_err("unknown pipe msg type - type %d len %d",
452                            pipeMsg->type, pipeMsg->size);
453                 PutInputDevice(Device);
454                 return ;
455         }
456
457         hidMsg = (struct synthhid_msg *)&pipeMsg->data[0];
458
459         switch (hidMsg->header.type) {
460         case SynthHidProtocolResponse:
461                 memcpy(&inputDevice->ProtocolResp, pipeMsg,
462                        pipeMsg->size + sizeof(struct pipe_prt_msg) -
463                        sizeof(unsigned char));
464                 inputDevice->protocol_wait_condition = 1;
465                 wake_up(&inputDevice->ProtocolWaitEvent);
466                 break;
467
468         case SynthHidInitialDeviceInfo:
469                 WARN_ON(pipeMsg->size >= sizeof(struct hv_input_dev_info));
470
471                 /*
472                  * Parse out the device info into device attr,
473                  * hid desc and report desc
474                  */
475                 MousevscOnReceiveDeviceInfo(inputDevice,
476                                             (struct synthhid_device_info *)&pipeMsg->data[0]);
477                 break;
478         case SynthHidInputReport:
479                 MousevscOnReceiveInputReport(inputDevice,
480                                              (struct synthhid_input_report *)&pipeMsg->data[0]);
481
482                 break;
483         default:
484                 pr_err("unsupported hid msg type - type %d len %d",
485                        hidMsg->header.type, hidMsg->header.size);
486                 break;
487         }
488
489         PutInputDevice(Device);
490 }
491
492 static void MousevscOnChannelCallback(void *Context)
493 {
494         const int packetSize = 0x100;
495         int ret = 0;
496         struct hv_device *device = (struct hv_device *)Context;
497         struct mousevsc_dev *inputDevice;
498
499         u32 bytesRecvd;
500         u64 requestId;
501         unsigned char packet[packetSize];
502         struct vmpacket_descriptor *desc;
503         unsigned char   *buffer = packet;
504         int     bufferlen = packetSize;
505
506         inputDevice = MustGetInputDevice(device);
507
508         if (!inputDevice) {
509                 pr_err("unable to get input device...device being destroyed?");
510                 return;
511         }
512
513         do {
514                 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen, &bytesRecvd, &requestId);
515
516                 if (ret == 0) {
517                         if (bytesRecvd > 0) {
518                                 desc = (struct vmpacket_descriptor *)buffer;
519
520                                 switch (desc->type) {
521                                         case VM_PKT_COMP:
522                                                 MousevscOnSendCompletion(device,
523                                                                          desc);
524                                                 break;
525
526                                         case VM_PKT_DATA_INBAND:
527                                                 MousevscOnReceive(device, desc);
528                                                 break;
529
530                                         default:
531                                                 pr_err("unhandled packet type %d, tid %llx len %d\n",
532                                                            desc->type,
533                                                            requestId,
534                                                            bytesRecvd);
535                                                 break;
536                                 }
537
538                                 /* reset */
539                                 if (bufferlen > packetSize) {
540                                         kfree(buffer);
541
542                                         buffer = packet;
543                                         bufferlen = packetSize;
544                                 }
545                         } else {
546                                 /*
547                                  * pr_debug("nothing else to read...");
548                                  * reset
549                                  */
550                                 if (bufferlen > packetSize) {
551                                         kfree(buffer);
552
553                                         buffer = packet;
554                                         bufferlen = packetSize;
555                                 }
556                                 break;
557                         }
558                 } else if (ret == -2) {
559                         /* Handle large packet */
560                         bufferlen = bytesRecvd;
561                         buffer = kzalloc(bytesRecvd, GFP_KERNEL);
562
563                         if (buffer == NULL) {
564                                 buffer = packet;
565                                 bufferlen = packetSize;
566
567                                 /* Try again next time around */
568                                 pr_err("unable to allocate buffer of size %d!",
569                                        bytesRecvd);
570                                 break;
571                         }
572                 }
573         } while (1);
574
575         PutInputDevice(device);
576
577         return;
578 }
579
580 static int MousevscConnectToVsp(struct hv_device *Device)
581 {
582         int ret = 0;
583         struct mousevsc_dev *inputDevice;
584         struct mousevsc_prt_msg *request;
585         struct mousevsc_prt_msg *response;
586
587         inputDevice = GetInputDevice(Device);
588
589         if (!inputDevice) {
590                 pr_err("unable to get input device...device being destroyed?");
591                 return -1;
592         }
593
594         init_waitqueue_head(&inputDevice->ProtocolWaitEvent);
595         init_waitqueue_head(&inputDevice->DeviceInfoWaitEvent);
596
597         request = &inputDevice->ProtocolReq;
598
599         /*
600          * Now, initiate the vsc/vsp initialization protocol on the open channel
601          */
602         memset(request, sizeof(struct mousevsc_prt_msg), 0);
603
604         request->type = PipeMessageData;
605         request->size = sizeof(struct synthhid_protocol_request);
606
607         request->request.header.type = SynthHidProtocolRequest;
608         request->request.header.size = sizeof(unsigned long);
609         request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
610
611         pr_info("synthhid protocol request...");
612
613         ret = vmbus_sendpacket(Device->channel, request,
614                                         sizeof(struct pipe_prt_msg) -
615                                         sizeof(unsigned char) +
616                                         sizeof(struct synthhid_protocol_request),
617                                         (unsigned long)request,
618                                         VM_PKT_DATA_INBAND,
619                                         VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
620         if (ret != 0) {
621                 pr_err("unable to send synthhid protocol request.");
622                 goto Cleanup;
623         }
624
625         inputDevice->protocol_wait_condition = 0;
626         wait_event_timeout(inputDevice->ProtocolWaitEvent, inputDevice->protocol_wait_condition, msecs_to_jiffies(1000));
627         if (inputDevice->protocol_wait_condition == 0) {
628                 ret = -ETIMEDOUT;
629                 goto Cleanup;
630         }
631
632         response = &inputDevice->ProtocolResp;
633
634         if (!response->response.approved) {
635                 pr_err("synthhid protocol request failed (version %d)",
636                        SYNTHHID_INPUT_VERSION);
637                 ret = -1;
638                 goto Cleanup;
639         }
640
641         inputDevice->device_wait_condition = 0;
642         wait_event_timeout(inputDevice->DeviceInfoWaitEvent, inputDevice->device_wait_condition, msecs_to_jiffies(1000));
643         if (inputDevice->device_wait_condition == 0) {
644                 ret = -ETIMEDOUT;
645                 goto Cleanup;
646         }
647
648         /*
649          * We should have gotten the device attr, hid desc and report
650          * desc at this point
651          */
652         if (!inputDevice->DeviceInfoStatus)
653                 pr_info("**** input channel up and running!! ****");
654         else
655                 ret = -1;
656
657 Cleanup:
658         PutInputDevice(Device);
659
660         return ret;
661 }
662
663 static int MousevscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
664 {
665         int ret = 0;
666         struct mousevsc_dev *inputDevice;
667         struct mousevsc_drv_obj *inputDriver;
668         struct hv_input_dev_info dev_info;
669
670         inputDevice = AllocInputDevice(Device);
671
672         if (!inputDevice) {
673                 ret = -1;
674                 goto Cleanup;
675         }
676
677         inputDevice->bInitializeComplete = false;
678
679         /* Open the channel */
680         ret = vmbus_open(Device->channel,
681                 INPUTVSC_SEND_RING_BUFFER_SIZE,
682                 INPUTVSC_RECV_RING_BUFFER_SIZE,
683                 NULL,
684                 0,
685                 MousevscOnChannelCallback,
686                 Device
687                 );
688
689         if (ret != 0) {
690                 pr_err("unable to open channel: %d", ret);
691                 return -1;
692         }
693
694         pr_info("InputVsc channel open: %d", ret);
695
696         ret = MousevscConnectToVsp(Device);
697
698         if (ret != 0) {
699                 pr_err("unable to connect channel: %d", ret);
700
701                 vmbus_close(Device->channel);
702                 return ret;
703         }
704
705         inputDriver = (struct mousevsc_drv_obj *)inputDevice->Device->drv;
706
707         dev_info.vendor = inputDevice->hid_dev_info.vendor;
708         dev_info.product = inputDevice->hid_dev_info.product;
709         dev_info.version = inputDevice->hid_dev_info.version;
710         strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
711
712         /* Send the device info back up */
713         deviceinfo_callback(Device, &dev_info);
714
715         /* Send the report desc back up */
716         /* workaround SA-167 */
717         if (inputDevice->ReportDesc[14] == 0x25)
718                 inputDevice->ReportDesc[14] = 0x29;
719
720         reportdesc_callback(Device, inputDevice->ReportDesc,
721                             inputDevice->ReportDescSize);
722
723         inputDevice->bInitializeComplete = true;
724
725 Cleanup:
726         return ret;
727 }
728
729 static int MousevscOnDeviceRemove(struct hv_device *Device)
730 {
731         struct mousevsc_dev *inputDevice;
732         int ret = 0;
733
734         pr_info("disabling input device (%p)...",
735                     Device->ext);
736
737         inputDevice = ReleaseInputDevice(Device);
738
739
740         /*
741          * At this point, all outbound traffic should be disable. We only
742          * allow inbound traffic (responses) to proceed
743          *
744          * so that outstanding requests can be completed.
745          */
746         while (inputDevice->NumOutstandingRequests) {
747                 pr_info("waiting for %d requests to complete...", inputDevice->NumOutstandingRequests);
748
749                 udelay(100);
750         }
751
752         pr_info("removing input device (%p)...", Device->ext);
753
754         inputDevice = FinalReleaseInputDevice(Device);
755
756         pr_info("input device (%p) safe to remove", inputDevice);
757
758         /* Close the channel */
759         vmbus_close(Device->channel);
760
761         FreeInputDevice(inputDevice);
762
763         return ret;
764 }
765
766 static void MousevscOnCleanup(struct hv_driver *drv)
767 {
768 }
769
770 /*
771  * Data types
772  */
773 struct input_device_context {
774         struct vm_device        *device_ctx;
775         struct hid_device       *hid_device;
776         struct hv_input_dev_info device_info;
777         int                     connected;
778 };
779
780 struct mousevsc_driver_context {
781         struct driver_context   drv_ctx;
782         struct mousevsc_drv_obj drv_obj;
783 };
784
785 static struct mousevsc_driver_context g_mousevsc_drv;
786
787 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
788 {
789         struct vm_device *device_ctx = to_vm_device(dev);
790         struct input_device_context *input_device_ctx =
791                 dev_get_drvdata(&device_ctx->device);
792
793         memcpy(&input_device_ctx->device_info, info,
794                sizeof(struct hv_input_dev_info));
795
796         DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
797 }
798
799 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
800 {
801         int ret = 0;
802
803         struct vm_device *device_ctx = to_vm_device(dev);
804         struct input_device_context *input_dev_ctx =
805                 dev_get_drvdata(&device_ctx->device);
806
807         ret = hid_input_report(input_dev_ctx->hid_device,
808                               HID_INPUT_REPORT, packet, len, 1);
809
810         DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
811 }
812
813 static int mousevsc_hid_open(struct hid_device *hid)
814 {
815         return 0;
816 }
817
818 static void mousevsc_hid_close(struct hid_device *hid)
819 {
820 }
821
822 static int mousevsc_probe(struct device *device)
823 {
824         int ret = 0;
825
826         struct driver_context *driver_ctx =
827                 driver_to_driver_context(device->driver);
828         struct mousevsc_driver_context *mousevsc_drv_ctx =
829                 (struct mousevsc_driver_context *)driver_ctx;
830         struct mousevsc_drv_obj *mousevsc_drv_obj = &mousevsc_drv_ctx->drv_obj;
831
832         struct vm_device *device_ctx = device_to_vm_device(device);
833         struct hv_device *device_obj = &device_ctx->device_obj;
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(device, input_dev_ctx);
840
841         /* Call to the vsc driver to add the device */
842         ret = mousevsc_drv_obj->Base.dev_add(device_obj, 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 device *device)
854 {
855         int ret = 0;
856
857         struct driver_context *driver_ctx =
858                 driver_to_driver_context(device->driver);
859         struct mousevsc_driver_context *mousevsc_drv_ctx =
860                 (struct mousevsc_driver_context *)driver_ctx;
861         struct mousevsc_drv_obj *mousevsc_drv_obj = &mousevsc_drv_ctx->drv_obj;
862
863         struct vm_device *device_ctx = device_to_vm_device(device);
864         struct hv_device *device_obj = &device_ctx->device_obj;
865         struct input_device_context *input_dev_ctx;
866
867         input_dev_ctx = kmalloc(sizeof(struct input_device_context),
868                                 GFP_KERNEL);
869
870         dev_set_drvdata(device, input_dev_ctx);
871
872         if (input_dev_ctx->connected) {
873                 hidinput_disconnect(input_dev_ctx->hid_device);
874                 input_dev_ctx->connected = 0;
875         }
876
877         if (!mousevsc_drv_obj->Base.dev_rm)
878                 return -1;
879
880         /*
881          * Call to the vsc driver to let it know that the device
882          * is being removed
883          */
884         ret = mousevsc_drv_obj->Base.dev_rm(device_obj);
885
886         if (ret != 0) {
887                 DPRINT_ERR(INPUTVSC_DRV,
888                            "unable to remove vsc device (ret %d)", ret);
889         }
890
891         kfree(input_dev_ctx);
892
893         return ret;
894 }
895
896 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
897 {
898         struct vm_device *device_ctx = to_vm_device(dev);
899         struct input_device_context *input_device_ctx =
900                 dev_get_drvdata(&device_ctx->device);
901         struct hid_device *hid_dev;
902
903         /* hid_debug = -1; */
904         hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
905
906         if (hid_parse_report(hid_dev, packet, len)) {
907                 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
908                 return;
909         }
910
911         if (hid_dev) {
912                 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
913
914                 hid_dev->ll_driver->open  = mousevsc_hid_open;
915                 hid_dev->ll_driver->close = mousevsc_hid_close;
916
917                 hid_dev->bus = BUS_VIRTUAL;
918                 hid_dev->vendor = input_device_ctx->device_info.vendor;
919                 hid_dev->product = input_device_ctx->device_info.product;
920                 hid_dev->version = input_device_ctx->device_info.version;
921                 hid_dev->dev = device_ctx->device;
922
923                 sprintf(hid_dev->name, "%s",
924                         input_device_ctx->device_info.name);
925
926                 /*
927                  * HJ Do we want to call it with a 0
928                  */
929                 if (!hidinput_connect(hid_dev, 0)) {
930                         hid_dev->claimed |= HID_CLAIMED_INPUT;
931
932                         input_device_ctx->connected = 1;
933
934                         DPRINT_INFO(INPUTVSC_DRV,
935                                      "HID device claimed by input\n");
936                 }
937
938                 if (!hid_dev->claimed) {
939                         DPRINT_ERR(INPUTVSC_DRV,
940                                     "HID device not claimed by "
941                                     "input or hiddev\n");
942                 }
943
944                 input_device_ctx->hid_device = hid_dev;
945         }
946
947         kfree(hid_dev);
948 }
949
950 static int mousevsc_drv_exit_cb(struct device *dev, void *data)
951 {
952         struct device **curr = (struct device **)data;
953         *curr = dev;
954
955         return 1;
956 }
957
958 static void mousevsc_drv_exit(void)
959 {
960         struct mousevsc_drv_obj *mousevsc_drv_obj = &g_mousevsc_drv.drv_obj;
961         struct driver_context *drv_ctx = &g_mousevsc_drv.drv_ctx;
962         int ret;
963
964         struct device *current_dev = NULL;
965
966         while (1) {
967                 current_dev = NULL;
968
969                 /* Get the device */
970                 ret = driver_for_each_device(&drv_ctx->driver, NULL,
971                                              (void *)&current_dev,
972                                              mousevsc_drv_exit_cb);
973                 if (ret)
974                         printk(KERN_ERR "Can't find mouse device!\n");
975
976                 if (current_dev == NULL)
977                         break;
978
979                 /* Initiate removal from the top-down */
980                 device_unregister(current_dev);
981         }
982
983         if (mousevsc_drv_obj->Base.cleanup)
984                 mousevsc_drv_obj->Base.cleanup(&mousevsc_drv_obj->Base);
985
986         vmbus_child_driver_unregister(drv_ctx);
987
988         return;
989 }
990
991 static int mouse_vsc_initialize(struct hv_driver *Driver)
992 {
993         struct mousevsc_drv_obj *inputDriver =
994                 (struct mousevsc_drv_obj *)Driver;
995         int ret = 0;
996
997         Driver->name = driver_name;
998         memcpy(&Driver->dev_type, &mouse_guid,
999                sizeof(struct hv_guid));
1000
1001         /* Setup the dispatch table */
1002         inputDriver->Base.dev_add = MousevscOnDeviceAdd;
1003         inputDriver->Base.dev_rm = MousevscOnDeviceRemove;
1004         inputDriver->Base.cleanup = MousevscOnCleanup;
1005
1006         return ret;
1007 }
1008
1009
1010 static int __init mousevsc_init(void)
1011 {
1012         struct mousevsc_drv_obj *input_drv_obj = &g_mousevsc_drv.drv_obj;
1013         struct driver_context *drv_ctx = &g_mousevsc_drv.drv_ctx;
1014
1015         DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
1016
1017         /* Callback to client driver to complete the initialization */
1018         mouse_vsc_initialize(&input_drv_obj->Base);
1019
1020         drv_ctx->driver.name = input_drv_obj->Base.name;
1021         memcpy(&drv_ctx->class_id, &input_drv_obj->Base.dev_type,
1022                sizeof(struct hv_guid));
1023
1024         drv_ctx->driver.probe = mousevsc_probe;
1025         drv_ctx->driver.remove = mousevsc_remove;
1026
1027         /* The driver belongs to vmbus */
1028         vmbus_child_driver_register(drv_ctx);
1029
1030         return 0;
1031 }
1032
1033 static void __exit mousevsc_exit(void)
1034 {
1035         mousevsc_drv_exit();
1036 }
1037
1038 /*
1039  * We don't want to automatically load this driver just yet, it's quite
1040  * broken.  It's safe if you want to load it yourself manually, but
1041  * don't inflict it on unsuspecting users, that's just mean.
1042  */
1043 #if 0
1044
1045 /*
1046  * We use a PCI table to determine if we should autoload this driver  This is
1047  * needed by distro tools to determine if the hyperv drivers should be
1048  * installed and/or configured.  We don't do anything else with the table, but
1049  * it needs to be present.
1050  */
1051 const static struct pci_device_id microsoft_hv_pci_table[] = {
1052         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
1053         { 0 }
1054 };
1055 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
1056 #endif
1057
1058 MODULE_LICENSE("GPL");
1059 MODULE_VERSION(HV_DRV_VERSION);
1060 module_init(mousevsc_init);
1061 module_exit(mousevsc_exit);
1062