2 * Copyright (c) 2009, Citrix Systems, Inc.
3 * Copyright (c) 2010, Microsoft Corporation.
4 * Copyright (c) 2011, Novell Inc.
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.
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
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>
29 #include "version_info.h"
31 #include "vmbus_api.h"
33 #include "vmbus_packet_format.h"
39 struct input_dev_info {
40 unsigned short VendorID;
41 unsigned short ProductID;
42 unsigned short VersionNumber;
46 /* Represents the input vsc driver */
47 struct mousevsc_drv_obj {
48 struct hv_driver Base; // Must be the first field
50 * This is set by the caller to allow us to callback when
51 * we receive a packet from the "wire"
53 void (*OnDeviceInfo)(struct hv_device *dev,
54 struct input_dev_info* info);
55 void (*OnInputReport)(struct hv_device *dev, void* packet, u32 len);
56 void (*OnReportDescriptor)(struct hv_device *dev,
57 void* packet, u32 len);
58 /* Specific to this driver */
59 int (*OnOpen)(struct hv_device *Device);
60 int (*OnClose)(struct hv_device *Device);
68 int mouse_vsc_initialize(struct hv_driver *drv);
70 /* The maximum size of a synthetic input message. */
71 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
77 * Beta, RC < 2008/1/22 1,0
80 #define SYNTHHID_INPUT_VERSION_MAJOR 2
81 #define SYNTHHID_INPUT_VERSION_MINOR 0
82 #define SYNTHHID_INPUT_VERSION_DWORD (SYNTHHID_INPUT_VERSION_MINOR | \
83 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
88 * Message types in the synthetic input protocol
90 enum synthhid_msg_type {
91 SynthHidProtocolRequest,
92 SynthHidProtocolResponse,
93 SynthHidInitialDeviceInfo,
94 SynthHidInitialDeviceInfoAck,
100 * Basic message structures.
103 enum synthhid_msg_type Type; /* Type of the enclosed message */
104 u32 Size; /* Size of the enclosed message
105 * (size of the data payload)
107 } SYNTHHID_MESSAGE_HEADER, *PSYNTHHID_MESSAGE_HEADER;
110 SYNTHHID_MESSAGE_HEADER Header;
111 char Data[1]; /* Enclosed message */
112 } SYNTHHID_MESSAGE, *PSYNTHHID_MESSAGE;
121 } SYNTHHID_VERSION, *PSYNTHHID_VERSION;
127 SYNTHHID_MESSAGE_HEADER Header;
128 SYNTHHID_VERSION VersionRequested;
129 } SYNTHHID_PROTOCOL_REQUEST, *PSYNTHHID_PROTOCOL_REQUEST;
132 SYNTHHID_MESSAGE_HEADER Header;
133 SYNTHHID_VERSION VersionRequested;
134 unsigned char Approved;
135 } SYNTHHID_PROTOCOL_RESPONSE, *PSYNTHHID_PROTOCOL_RESPONSE;
138 SYNTHHID_MESSAGE_HEADER Header;
139 struct input_dev_info HidDeviceAttributes;
140 unsigned char HidDescriptorInformation[1];
141 } SYNTHHID_DEVICE_INFO, *PSYNTHHID_DEVICE_INFO;
144 SYNTHHID_MESSAGE_HEADER Header;
145 unsigned char Reserved;
146 } SYNTHHID_DEVICE_INFO_ACK, *PSYNTHHID_DEVICE_INFO_ACK;
149 SYNTHHID_MESSAGE_HEADER Header;
150 char ReportBuffer[1];
151 } SYNTHHID_INPUT_REPORT, *PSYNTHHID_INPUT_REPORT;
155 #define INPUTVSC_SEND_RING_BUFFER_SIZE 10*PAGE_SIZE
156 #define INPUTVSC_RECV_RING_BUFFER_SIZE 10*PAGE_SIZE
158 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
160 enum pipe_prot_msg_type {
161 PipeMessageInvalid = 0,
167 struct pipe_prt_msg {
168 enum pipe_prot_msg_type PacketType;
176 struct mousevsc_prt_msg {
177 enum pipe_prot_msg_type PacketType;
180 SYNTHHID_PROTOCOL_REQUEST Request;
181 SYNTHHID_PROTOCOL_RESPONSE Response;
182 SYNTHHID_DEVICE_INFO_ACK Ack;
187 * Represents an mousevsc device
189 struct mousevsc_dev {
190 struct hv_device *Device;
191 /* 0 indicates the device is being destroyed */
193 int NumOutstandingRequests;
194 unsigned char bInitializeComplete;
195 struct mousevsc_prt_msg ProtocolReq;
196 struct mousevsc_prt_msg ProtocolResp;
197 /* Synchronize the request/response if needed */
198 wait_queue_head_t ProtocolWaitEvent;
199 wait_queue_head_t DeviceInfoWaitEvent;
200 int protocol_wait_condition;
201 int device_wait_condition;
202 int DeviceInfoStatus;
204 struct hid_descriptor *HidDesc;
205 unsigned char *ReportDesc;
207 struct input_dev_info DeviceAttr;
214 static const char *gDriverName = "mousevsc";
216 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
217 static const struct hv_guid gMousevscDeviceType = {
218 .data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
219 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}
225 static int MousevscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
227 static int MousevscOnDeviceRemove(struct hv_device *Device);
229 static void MousevscOnCleanup(struct hv_driver *Device);
231 static void MousevscOnChannelCallback(void *Context);
233 static int MousevscConnectToVsp(struct hv_device *Device);
235 static void MousevscOnReceive(struct hv_device *Device,
236 struct vmpacket_descriptor *Packet);
238 static inline struct mousevsc_dev *AllocInputDevice(struct hv_device *Device)
240 struct mousevsc_dev *inputDevice;
242 inputDevice = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
248 * Set to 2 to allow both inbound and outbound traffics
249 * (ie GetInputDevice() and MustGetInputDevice()) to proceed.
251 atomic_cmpxchg(&inputDevice->RefCount, 0, 2);
253 inputDevice->Device = Device;
254 Device->ext = inputDevice;
259 static inline void FreeInputDevice(struct mousevsc_dev *Device)
261 WARN_ON(atomic_read(&Device->RefCount) == 0);
266 * Get the inputdevice object if exists and its refcount > 1
268 static inline struct mousevsc_dev *GetInputDevice(struct hv_device *Device)
270 struct mousevsc_dev *inputDevice;
272 inputDevice = (struct mousevsc_dev *)Device->ext;
276 * This sure isn't a valid thing to print for debugging, no matter
277 * what the intention is...
279 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
280 * inputDevice->RefCount);
283 if (inputDevice && atomic_read(&inputDevice->RefCount) > 1)
284 atomic_inc(&inputDevice->RefCount);
292 * Get the inputdevice object iff exists and its refcount > 0
294 static inline struct mousevsc_dev *MustGetInputDevice(struct hv_device *Device)
296 struct mousevsc_dev *inputDevice;
298 inputDevice = (struct mousevsc_dev *)Device->ext;
300 if (inputDevice && atomic_read(&inputDevice->RefCount))
301 atomic_inc(&inputDevice->RefCount);
308 static inline void PutInputDevice(struct hv_device *Device)
310 struct mousevsc_dev *inputDevice;
312 inputDevice = (struct mousevsc_dev *)Device->ext;
314 atomic_dec(&inputDevice->RefCount);
318 * Drop ref count to 1 to effectively disable GetInputDevice()
320 static inline struct mousevsc_dev *ReleaseInputDevice(struct hv_device *Device)
322 struct mousevsc_dev *inputDevice;
324 inputDevice = (struct mousevsc_dev *)Device->ext;
326 /* Busy wait until the ref drop to 2, then set it to 1 */
327 while (atomic_cmpxchg(&inputDevice->RefCount, 2, 1) != 2)
334 * Drop ref count to 0. No one can use InputDevice object.
336 static inline struct mousevsc_dev *FinalReleaseInputDevice(struct hv_device *Device)
338 struct mousevsc_dev *inputDevice;
340 inputDevice = (struct mousevsc_dev *)Device->ext;
342 /* Busy wait until the ref drop to 1, then set it to 0 */
343 while (atomic_cmpxchg(&inputDevice->RefCount, 1, 0) != 1)
353 * MousevscInitialize()
359 int mouse_vsc_initialize(struct hv_driver *Driver)
361 struct mousevsc_drv_obj *inputDriver =
362 (struct mousevsc_drv_obj *)Driver;
365 Driver->name = gDriverName;
366 memcpy(&Driver->dev_type, &gMousevscDeviceType,
367 sizeof(struct hv_guid));
369 /* Setup the dispatch table */
370 inputDriver->Base.dev_add = MousevscOnDeviceAdd;
371 inputDriver->Base.dev_rm = MousevscOnDeviceRemove;
372 inputDriver->Base.cleanup = MousevscOnCleanup;
374 inputDriver->OnOpen = NULL;
375 inputDriver->OnClose = NULL;
383 * MousevscOnDeviceAdd()
386 * Callback when the device belonging to this driver is added
390 MousevscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
393 struct mousevsc_dev *inputDevice;
394 struct mousevsc_drv_obj *inputDriver;
395 struct input_dev_info deviceInfo;
397 inputDevice = AllocInputDevice(Device);
404 inputDevice->bInitializeComplete = false;
406 /* Open the channel */
407 ret = vmbus_open(Device->channel,
408 INPUTVSC_SEND_RING_BUFFER_SIZE,
409 INPUTVSC_RECV_RING_BUFFER_SIZE,
412 MousevscOnChannelCallback,
417 pr_err("unable to open channel: %d", ret);
421 pr_info("InputVsc channel open: %d", ret);
423 ret = MousevscConnectToVsp(Device);
426 pr_err("unable to connect channel: %d", ret);
428 vmbus_close(Device->channel);
432 inputDriver = (struct mousevsc_drv_obj *)inputDevice->Device->drv;
434 deviceInfo.VendorID = inputDevice->DeviceAttr.VendorID;
435 deviceInfo.ProductID = inputDevice->DeviceAttr.ProductID;
436 deviceInfo.VersionNumber = inputDevice->DeviceAttr.VersionNumber;
437 strcpy(deviceInfo.Name, "Microsoft Vmbus HID-compliant Mouse");
439 /* Send the device info back up */
440 inputDriver->OnDeviceInfo(Device, &deviceInfo);
442 /* Send the report desc back up */
443 /* workaround SA-167 */
444 if (inputDevice->ReportDesc[14] == 0x25)
445 inputDevice->ReportDesc[14] = 0x29;
447 inputDriver->OnReportDescriptor(Device, inputDevice->ReportDesc, inputDevice->ReportDescSize);
449 inputDevice->bInitializeComplete = true;
456 MousevscConnectToVsp(struct hv_device *Device)
459 struct mousevsc_dev *inputDevice;
460 struct mousevsc_prt_msg *request;
461 struct mousevsc_prt_msg *response;
463 inputDevice = GetInputDevice(Device);
466 pr_err("unable to get input device...device being destroyed?");
470 init_waitqueue_head(&inputDevice->ProtocolWaitEvent);
471 init_waitqueue_head(&inputDevice->DeviceInfoWaitEvent);
473 request = &inputDevice->ProtocolReq;
476 * Now, initiate the vsc/vsp initialization protocol on the open channel
478 memset(request, sizeof(struct mousevsc_prt_msg), 0);
480 request->PacketType = PipeMessageData;
481 request->DataSize = sizeof(SYNTHHID_PROTOCOL_REQUEST);
483 request->u.Request.Header.Type = SynthHidProtocolRequest;
484 request->u.Request.Header.Size = sizeof(unsigned long);
485 request->u.Request.VersionRequested.AsDWord =
486 SYNTHHID_INPUT_VERSION_DWORD;
488 pr_info("SYNTHHID_PROTOCOL_REQUEST...");
490 ret = vmbus_sendpacket(Device->channel, request,
491 sizeof(struct pipe_prt_msg) -
492 sizeof(unsigned char) +
493 sizeof(SYNTHHID_PROTOCOL_REQUEST),
494 (unsigned long)request,
496 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
498 pr_err("unable to send SYNTHHID_PROTOCOL_REQUEST");
502 inputDevice->protocol_wait_condition = 0;
503 wait_event_timeout(inputDevice->ProtocolWaitEvent, inputDevice->protocol_wait_condition, msecs_to_jiffies(1000));
504 if (inputDevice->protocol_wait_condition == 0) {
509 response = &inputDevice->ProtocolResp;
511 if (!response->u.Response.Approved) {
512 pr_err("SYNTHHID_PROTOCOL_REQUEST failed (version %d)",
513 SYNTHHID_INPUT_VERSION_DWORD);
518 inputDevice->device_wait_condition = 0;
519 wait_event_timeout(inputDevice->DeviceInfoWaitEvent, inputDevice->device_wait_condition, msecs_to_jiffies(1000));
520 if (inputDevice->device_wait_condition == 0) {
526 * We should have gotten the device attr, hid desc and report
529 if (!inputDevice->DeviceInfoStatus)
530 pr_info("**** input channel up and running!! ****");
535 PutInputDevice(Device);
544 * MousevscOnDeviceRemove()
547 * Callback when the our device is being removed
551 MousevscOnDeviceRemove(struct hv_device *Device)
553 struct mousevsc_dev *inputDevice;
556 pr_info("disabling input device (%p)...",
559 inputDevice = ReleaseInputDevice(Device);
563 * At this point, all outbound traffic should be disable. We only
564 * allow inbound traffic (responses) to proceed
566 * so that outstanding requests can be completed.
568 while (inputDevice->NumOutstandingRequests) {
569 pr_info("waiting for %d requests to complete...", inputDevice->NumOutstandingRequests);
574 pr_info("removing input device (%p)...", Device->ext);
576 inputDevice = FinalReleaseInputDevice(Device);
578 pr_info("input device (%p) safe to remove", inputDevice);
580 /* Close the channel */
581 vmbus_close(Device->channel);
583 FreeInputDevice(inputDevice);
592 * MousevscOnCleanup()
595 * Perform any cleanup when the driver is removed
597 static void MousevscOnCleanup(struct hv_driver *drv)
603 MousevscOnSendCompletion(struct hv_device *Device,
604 struct vmpacket_descriptor *Packet)
606 struct mousevsc_dev *inputDevice;
609 inputDevice = MustGetInputDevice(Device);
611 pr_err("unable to get input device...device being destroyed?");
615 request = (void *)(unsigned long *)Packet->trans_id;
617 if (request == &inputDevice->ProtocolReq) {
619 /* Shouldn't we be doing something here? */
622 PutInputDevice(Device);
626 MousevscOnReceiveDeviceInfo(
627 struct mousevsc_dev *InputDevice,
628 SYNTHHID_DEVICE_INFO *DeviceInfo)
631 struct hid_descriptor *desc;
632 struct mousevsc_prt_msg ack;
634 /* Assume success for now */
635 InputDevice->DeviceInfoStatus = 0;
637 /* Save the device attr */
638 memcpy(&InputDevice->DeviceAttr, &DeviceInfo->HidDeviceAttributes, sizeof(struct input_dev_info));
640 /* Save the hid desc */
641 desc = (struct hid_descriptor *)DeviceInfo->HidDescriptorInformation;
642 WARN_ON(desc->bLength > 0);
644 InputDevice->HidDesc = kzalloc(desc->bLength, GFP_KERNEL);
646 if (!InputDevice->HidDesc) {
647 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
651 memcpy(InputDevice->HidDesc, desc, desc->bLength);
653 /* Save the report desc */
654 InputDevice->ReportDescSize = desc->desc[0].wDescriptorLength;
655 InputDevice->ReportDesc = kzalloc(InputDevice->ReportDescSize,
658 if (!InputDevice->ReportDesc) {
659 pr_err("unable to allocate report descriptor - size %d",
660 InputDevice->ReportDescSize);
664 memcpy(InputDevice->ReportDesc,
665 ((unsigned char *)desc) + desc->bLength,
666 desc->desc[0].wDescriptorLength);
669 memset(&ack, sizeof(struct mousevsc_prt_msg), 0);
671 ack.PacketType = PipeMessageData;
672 ack.DataSize = sizeof(SYNTHHID_DEVICE_INFO_ACK);
674 ack.u.Ack.Header.Type = SynthHidInitialDeviceInfoAck;
675 ack.u.Ack.Header.Size = 1;
676 ack.u.Ack.Reserved = 0;
678 ret = vmbus_sendpacket(InputDevice->Device->channel,
680 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) + sizeof(SYNTHHID_DEVICE_INFO_ACK),
683 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
685 pr_err("unable to send SYNTHHID_DEVICE_INFO_ACK - ret %d",
690 InputDevice->device_wait_condition = 1;
691 wake_up(&InputDevice->DeviceInfoWaitEvent);
696 if (InputDevice->HidDesc) {
697 kfree(InputDevice->HidDesc);
698 InputDevice->HidDesc = NULL;
701 if (InputDevice->ReportDesc) {
702 kfree(InputDevice->ReportDesc);
703 InputDevice->ReportDesc = NULL;
706 InputDevice->DeviceInfoStatus = -1;
707 InputDevice->device_wait_condition = 1;
708 wake_up(&InputDevice->DeviceInfoWaitEvent);
713 MousevscOnReceiveInputReport(
714 struct mousevsc_dev *InputDevice,
715 SYNTHHID_INPUT_REPORT *InputReport)
717 struct mousevsc_drv_obj *inputDriver;
719 if (!InputDevice->bInitializeComplete) {
720 pr_info("Initialization incomplete...ignoring InputReport msg");
724 inputDriver = (struct mousevsc_drv_obj *)InputDevice->Device->drv;
726 inputDriver->OnInputReport(InputDevice->Device,
727 InputReport->ReportBuffer,
728 InputReport->Header.Size);
732 MousevscOnReceive(struct hv_device *Device, struct vmpacket_descriptor *Packet)
734 struct pipe_prt_msg *pipeMsg;
735 SYNTHHID_MESSAGE *hidMsg;
736 struct mousevsc_dev *inputDevice;
738 inputDevice = MustGetInputDevice(Device);
740 pr_err("unable to get input device...device being destroyed?");
744 pipeMsg = (struct pipe_prt_msg *)((unsigned long)Packet + (Packet->offset8 << 3));
746 if (pipeMsg->PacketType != PipeMessageData) {
747 pr_err("unknown pipe msg type - type %d len %d",
748 pipeMsg->PacketType, pipeMsg->DataSize);
749 PutInputDevice(Device);
753 hidMsg = (SYNTHHID_MESSAGE *)&pipeMsg->Data[0];
755 switch (hidMsg->Header.Type) {
756 case SynthHidProtocolResponse:
757 memcpy(&inputDevice->ProtocolResp, pipeMsg, pipeMsg->DataSize+sizeof(struct pipe_prt_msg) - sizeof(unsigned char));
758 inputDevice->protocol_wait_condition = 1;
759 wake_up(&inputDevice->ProtocolWaitEvent);
762 case SynthHidInitialDeviceInfo:
763 WARN_ON(pipeMsg->DataSize >= sizeof(struct input_dev_info));
766 * Parse out the device info into device attr,
767 * hid desc and report desc
769 MousevscOnReceiveDeviceInfo(inputDevice,
770 (SYNTHHID_DEVICE_INFO *)&pipeMsg->Data[0]);
772 case SynthHidInputReport:
773 MousevscOnReceiveInputReport(inputDevice,
774 (SYNTHHID_INPUT_REPORT *)&pipeMsg->Data[0]);
778 pr_err("unsupported hid msg type - type %d len %d",
779 hidMsg->Header.Type, hidMsg->Header.Size);
783 PutInputDevice(Device);
786 void MousevscOnChannelCallback(void *Context)
788 const int packetSize = 0x100;
790 struct hv_device *device = (struct hv_device *)Context;
791 struct mousevsc_dev *inputDevice;
795 unsigned char packet[packetSize];
796 struct vmpacket_descriptor *desc;
797 unsigned char *buffer = packet;
798 int bufferlen = packetSize;
800 inputDevice = MustGetInputDevice(device);
803 pr_err("unable to get input device...device being destroyed?");
808 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen, &bytesRecvd, &requestId);
811 if (bytesRecvd > 0) {
812 desc = (struct vmpacket_descriptor *)buffer;
814 switch (desc->type) {
816 MousevscOnSendCompletion(device,
820 case VM_PKT_DATA_INBAND:
821 MousevscOnReceive(device, desc);
825 pr_err("unhandled packet type %d, tid %llx len %d\n",
833 if (bufferlen > packetSize) {
837 bufferlen = packetSize;
841 * pr_debug("nothing else to read...");
844 if (bufferlen > packetSize) {
848 bufferlen = packetSize;
852 } else if (ret == -2) {
853 /* Handle large packet */
854 bufferlen = bytesRecvd;
855 buffer = kzalloc(bytesRecvd, GFP_KERNEL);
857 if (buffer == NULL) {
859 bufferlen = packetSize;
861 /* Try again next time around */
862 pr_err("unable to allocate buffer of size %d!",
869 PutInputDevice(device);
877 struct input_device_context {
878 struct vm_device *device_ctx;
879 struct hid_device *hid_device;
880 struct input_dev_info device_info;
884 struct mousevsc_driver_context {
885 struct driver_context drv_ctx;
886 struct mousevsc_drv_obj drv_obj;
889 static struct mousevsc_driver_context g_mousevsc_drv;
891 void mousevsc_deviceinfo_callback(struct hv_device *dev,
892 struct input_dev_info *info)
894 struct vm_device *device_ctx = to_vm_device(dev);
895 struct input_device_context *input_device_ctx =
896 dev_get_drvdata(&device_ctx->device);
898 memcpy(&input_device_ctx->device_info, info,
899 sizeof(struct input_dev_info));
901 DPRINT_INFO(INPUTVSC_DRV, "mousevsc_deviceinfo_callback()");
904 void mousevsc_inputreport_callback(struct hv_device *dev, void *packet, u32 len)
908 struct vm_device *device_ctx = to_vm_device(dev);
909 struct input_device_context *input_dev_ctx =
910 dev_get_drvdata(&device_ctx->device);
912 ret = hid_input_report(input_dev_ctx->hid_device,
913 HID_INPUT_REPORT, packet, len, 1);
915 DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
918 int mousevsc_hid_open(struct hid_device *hid)
923 void mousevsc_hid_close(struct hid_device *hid)
927 int mousevsc_probe(struct device *device)
931 struct driver_context *driver_ctx =
932 driver_to_driver_context(device->driver);
933 struct mousevsc_driver_context *mousevsc_drv_ctx =
934 (struct mousevsc_driver_context *)driver_ctx;
935 struct mousevsc_drv_obj *mousevsc_drv_obj = &mousevsc_drv_ctx->drv_obj;
937 struct vm_device *device_ctx = device_to_vm_device(device);
938 struct hv_device *device_obj = &device_ctx->device_obj;
939 struct input_device_context *input_dev_ctx;
941 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
944 dev_set_drvdata(device, input_dev_ctx);
946 /* Call to the vsc driver to add the device */
947 ret = mousevsc_drv_obj->Base.dev_add(device_obj, NULL);
950 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
959 int mousevsc_remove(struct device *device)
963 struct driver_context *driver_ctx =
964 driver_to_driver_context(device->driver);
965 struct mousevsc_driver_context *mousevsc_drv_ctx =
966 (struct mousevsc_driver_context *)driver_ctx;
967 struct mousevsc_drv_obj *mousevsc_drv_obj = &mousevsc_drv_ctx->drv_obj;
969 struct vm_device *device_ctx = device_to_vm_device(device);
970 struct hv_device *device_obj = &device_ctx->device_obj;
971 struct input_device_context *input_dev_ctx;
973 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
976 dev_set_drvdata(device, input_dev_ctx);
978 if (input_dev_ctx->connected) {
979 hidinput_disconnect(input_dev_ctx->hid_device);
980 input_dev_ctx->connected = 0;
983 if (!mousevsc_drv_obj->Base.dev_rm)
987 * Call to the vsc driver to let it know that the device
990 ret = mousevsc_drv_obj->Base.dev_rm(device_obj);
993 DPRINT_ERR(INPUTVSC_DRV,
994 "unable to remove vsc device (ret %d)", ret);
997 kfree(input_dev_ctx);
1002 void mousevsc_reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
1004 struct vm_device *device_ctx = to_vm_device(dev);
1005 struct input_device_context *input_device_ctx =
1006 dev_get_drvdata(&device_ctx->device);
1007 struct hid_device *hid_dev;
1009 /* hid_debug = -1; */
1010 hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
1012 if (hid_parse_report(hid_dev, packet, len)) {
1013 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
1018 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
1020 hid_dev->ll_driver->open = mousevsc_hid_open;
1021 hid_dev->ll_driver->close = mousevsc_hid_close;
1023 hid_dev->bus = 0x06; /* BUS_VIRTUAL */
1024 hid_dev->vendor = input_device_ctx->device_info.VendorID;
1025 hid_dev->product = input_device_ctx->device_info.ProductID;
1026 hid_dev->version = input_device_ctx->device_info.VersionNumber;
1027 hid_dev->dev = device_ctx->device;
1029 sprintf(hid_dev->name, "%s",
1030 input_device_ctx->device_info.Name);
1033 * HJ Do we want to call it with a 0
1035 if (!hidinput_connect(hid_dev, 0)) {
1036 hid_dev->claimed |= HID_CLAIMED_INPUT;
1038 input_device_ctx->connected = 1;
1040 DPRINT_INFO(INPUTVSC_DRV,
1041 "HID device claimed by input\n");
1044 if (!hid_dev->claimed) {
1045 DPRINT_ERR(INPUTVSC_DRV,
1046 "HID device not claimed by "
1047 "input or hiddev\n");
1050 input_device_ctx->hid_device = hid_dev;
1058 * Name: mousevsc_drv_init()
1060 * Desc: Driver initialization.
1062 int mousevsc_drv_init(int (*pfn_drv_init)(struct hv_driver *pfn_drv_init))
1065 struct mousevsc_drv_obj *input_drv_obj = &g_mousevsc_drv.drv_obj;
1066 struct driver_context *drv_ctx = &g_mousevsc_drv.drv_ctx;
1068 input_drv_obj->OnDeviceInfo = mousevsc_deviceinfo_callback;
1069 input_drv_obj->OnInputReport = mousevsc_inputreport_callback;
1070 input_drv_obj->OnReportDescriptor = mousevsc_reportdesc_callback;
1072 /* Callback to client driver to complete the initialization */
1073 pfn_drv_init(&input_drv_obj->Base);
1075 drv_ctx->driver.name = input_drv_obj->Base.name;
1076 memcpy(&drv_ctx->class_id, &input_drv_obj->Base.dev_type,
1077 sizeof(struct hv_guid));
1079 drv_ctx->probe = mousevsc_probe;
1080 drv_ctx->remove = mousevsc_remove;
1082 /* The driver belongs to vmbus */
1083 vmbus_child_driver_register(drv_ctx);
1089 int mousevsc_drv_exit_cb(struct device *dev, void *data)
1091 struct device **curr = (struct device **)data;
1097 void mousevsc_drv_exit(void)
1099 struct mousevsc_drv_obj *mousevsc_drv_obj = &g_mousevsc_drv.drv_obj;
1100 struct driver_context *drv_ctx = &g_mousevsc_drv.drv_ctx;
1103 struct device *current_dev = NULL;
1108 /* Get the device */
1109 ret = driver_for_each_device(&drv_ctx->driver, NULL,
1110 (void *)¤t_dev,
1111 mousevsc_drv_exit_cb);
1113 printk(KERN_ERR "Can't find mouse device!\n");
1115 if (current_dev == NULL)
1118 /* Initiate removal from the top-down */
1119 device_unregister(current_dev);
1122 if (mousevsc_drv_obj->Base.cleanup)
1123 mousevsc_drv_obj->Base.cleanup(&mousevsc_drv_obj->Base);
1125 vmbus_child_driver_unregister(drv_ctx);
1130 static int __init mousevsc_init(void)
1134 DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
1136 ret = mousevsc_drv_init(mouse_vsc_initialize);
1141 static void __exit mousevsc_exit(void)
1143 mousevsc_drv_exit();
1147 * We don't want to automatically load this driver just yet, it's quite
1148 * broken. It's safe if you want to load it yourself manually, but
1149 * don't inflict it on unsuspecting users, that's just mean.
1154 * We use a PCI table to determine if we should autoload this driver This is
1155 * needed by distro tools to determine if the hyperv drivers should be
1156 * installed and/or configured. We don't do anything else with the table, but
1157 * it needs to be present.
1159 const static struct pci_device_id microsoft_hv_pci_table[] = {
1160 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
1163 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
1166 MODULE_LICENSE("GPL");
1167 MODULE_VERSION(HV_DRV_VERSION);
1168 module_init(mousevsc_init);
1169 module_exit(mousevsc_exit);