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