Staging: hv: hv_mouse: clean up camelcase when using struct hv_input_dev_info
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / hv_mouse.c
blob2ed67d93aed51d7cb55f4c390bf305a49dc86a00
1 /*
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
13 * more details.
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>
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"
37 * Data types
39 struct hv_input_dev_info {
40 unsigned short vendor;
41 unsigned short product;
42 unsigned short version;
43 char name[128];
46 /* Represents the input vsc driver */
47 /* FIXME - can be removed entirely */
48 struct mousevsc_drv_obj {
49 struct hv_driver Base;
53 /* The maximum size of a synthetic input message. */
54 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
57 * Current version
59 * History:
60 * Beta, RC < 2008/1/22 1,0
61 * RC > 2008/1/22 2,0
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))
69 #pragma pack(push,1)
71 * Message types in the synthetic input protocol
73 enum synthhid_msg_type {
74 SynthHidProtocolRequest,
75 SynthHidProtocolResponse,
76 SynthHidInitialDeviceInfo,
77 SynthHidInitialDeviceInfoAck,
78 SynthHidInputReport,
79 SynthHidMax
83 * Basic message structures.
85 struct synthhid_msg_hdr {
86 enum synthhid_msg_type type;
87 u32 size;
90 struct synthhid_msg {
91 struct synthhid_msg_hdr header;
92 char data[1]; /* Enclosed message */
95 union synthhid_version {
96 struct {
97 u16 minor_version;
98 u16 major_version;
100 u32 version;
104 * Protocol messages
106 struct synthhid_protocol_request {
107 struct synthhid_msg_hdr header;
108 union synthhid_version version_requested;
111 struct synthhid_protocol_response {
112 struct synthhid_msg_hdr header;
113 union synthhid_version version_requested;
114 unsigned char approved;
117 struct synthhid_device_info {
118 struct synthhid_msg_hdr header;
119 struct hv_input_dev_info hid_dev_info;
120 unsigned char HidDescriptorInformation[1];
123 struct synthhid_device_info_ack {
124 struct synthhid_msg_hdr header;
125 unsigned char Reserved;
128 struct synthhid_input_report {
129 struct synthhid_msg_hdr header;
130 char ReportBuffer[1];
133 #pragma pack(pop)
135 #define INPUTVSC_SEND_RING_BUFFER_SIZE 10*PAGE_SIZE
136 #define INPUTVSC_RECV_RING_BUFFER_SIZE 10*PAGE_SIZE
138 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
140 enum pipe_prot_msg_type {
141 PipeMessageInvalid = 0,
142 PipeMessageData,
143 PipeMessageMaximum
147 struct pipe_prt_msg {
148 enum pipe_prot_msg_type PacketType;
149 u32 DataSize;
150 char Data[1];
154 * Data types
156 struct mousevsc_prt_msg {
157 enum pipe_prot_msg_type PacketType;
158 u32 DataSize;
159 union {
160 struct synthhid_protocol_request Request;
161 struct synthhid_protocol_response Response;
162 struct synthhid_device_info_ack Ack;
163 } u;
167 * Represents an mousevsc device
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;
184 struct hid_descriptor *HidDesc;
185 unsigned char *ReportDesc;
186 u32 ReportDescSize;
187 struct hv_input_dev_info hid_dev_info;
192 * Globals
194 static const char *gDriverName = "mousevsc";
196 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
197 static const struct hv_guid gMousevscDeviceType = {
198 .data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
199 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}
202 static void MousevscOnReceive(struct hv_device *Device,
203 struct vmpacket_descriptor *Packet);
205 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info);
206 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len);
207 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len);
209 static struct mousevsc_dev *AllocInputDevice(struct hv_device *Device)
211 struct mousevsc_dev *inputDevice;
213 inputDevice = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
215 if (!inputDevice)
216 return NULL;
219 * Set to 2 to allow both inbound and outbound traffics
220 * (ie GetInputDevice() and MustGetInputDevice()) to proceed.
222 atomic_cmpxchg(&inputDevice->RefCount, 0, 2);
224 inputDevice->Device = Device;
225 Device->ext = inputDevice;
227 return inputDevice;
230 static void FreeInputDevice(struct mousevsc_dev *Device)
232 WARN_ON(atomic_read(&Device->RefCount) == 0);
233 kfree(Device);
237 * Get the inputdevice object if exists and its refcount > 1
239 static struct mousevsc_dev *GetInputDevice(struct hv_device *Device)
241 struct mousevsc_dev *inputDevice;
243 inputDevice = (struct mousevsc_dev *)Device->ext;
246 * FIXME
247 * This sure isn't a valid thing to print for debugging, no matter
248 * what the intention is...
250 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
251 * inputDevice->RefCount);
254 if (inputDevice && atomic_read(&inputDevice->RefCount) > 1)
255 atomic_inc(&inputDevice->RefCount);
256 else
257 inputDevice = NULL;
259 return inputDevice;
263 * Get the inputdevice object iff exists and its refcount > 0
265 static struct mousevsc_dev *MustGetInputDevice(struct hv_device *Device)
267 struct mousevsc_dev *inputDevice;
269 inputDevice = (struct mousevsc_dev *)Device->ext;
271 if (inputDevice && atomic_read(&inputDevice->RefCount))
272 atomic_inc(&inputDevice->RefCount);
273 else
274 inputDevice = NULL;
276 return inputDevice;
279 static void PutInputDevice(struct hv_device *Device)
281 struct mousevsc_dev *inputDevice;
283 inputDevice = (struct mousevsc_dev *)Device->ext;
285 atomic_dec(&inputDevice->RefCount);
289 * Drop ref count to 1 to effectively disable GetInputDevice()
291 static struct mousevsc_dev *ReleaseInputDevice(struct hv_device *Device)
293 struct mousevsc_dev *inputDevice;
295 inputDevice = (struct mousevsc_dev *)Device->ext;
297 /* Busy wait until the ref drop to 2, then set it to 1 */
298 while (atomic_cmpxchg(&inputDevice->RefCount, 2, 1) != 2)
299 udelay(100);
301 return inputDevice;
305 * Drop ref count to 0. No one can use InputDevice object.
307 static struct mousevsc_dev *FinalReleaseInputDevice(struct hv_device *Device)
309 struct mousevsc_dev *inputDevice;
311 inputDevice = (struct mousevsc_dev *)Device->ext;
313 /* Busy wait until the ref drop to 1, then set it to 0 */
314 while (atomic_cmpxchg(&inputDevice->RefCount, 1, 0) != 1)
315 udelay(100);
317 Device->ext = NULL;
318 return inputDevice;
321 static void MousevscOnSendCompletion(struct hv_device *Device, struct vmpacket_descriptor *Packet)
323 struct mousevsc_dev *inputDevice;
324 void *request;
326 inputDevice = MustGetInputDevice(Device);
327 if (!inputDevice) {
328 pr_err("unable to get input device...device being destroyed?");
329 return;
332 request = (void *)(unsigned long)Packet->trans_id;
334 if (request == &inputDevice->ProtocolReq) {
335 /* FIXME */
336 /* Shouldn't we be doing something here? */
339 PutInputDevice(Device);
342 static void MousevscOnReceiveDeviceInfo(struct mousevsc_dev *InputDevice, struct synthhid_device_info *DeviceInfo)
344 int ret = 0;
345 struct hid_descriptor *desc;
346 struct mousevsc_prt_msg ack;
348 /* Assume success for now */
349 InputDevice->DeviceInfoStatus = 0;
351 /* Save the device attr */
352 memcpy(&InputDevice->hid_dev_info, &DeviceInfo->hid_dev_info, sizeof(struct hv_input_dev_info));
354 /* Save the hid desc */
355 desc = (struct hid_descriptor *)DeviceInfo->HidDescriptorInformation;
356 WARN_ON(desc->bLength > 0);
358 InputDevice->HidDesc = kzalloc(desc->bLength, GFP_KERNEL);
360 if (!InputDevice->HidDesc) {
361 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
362 goto Cleanup;
365 memcpy(InputDevice->HidDesc, desc, desc->bLength);
367 /* Save the report desc */
368 InputDevice->ReportDescSize = desc->desc[0].wDescriptorLength;
369 InputDevice->ReportDesc = kzalloc(InputDevice->ReportDescSize,
370 GFP_KERNEL);
372 if (!InputDevice->ReportDesc) {
373 pr_err("unable to allocate report descriptor - size %d",
374 InputDevice->ReportDescSize);
375 goto Cleanup;
378 memcpy(InputDevice->ReportDesc,
379 ((unsigned char *)desc) + desc->bLength,
380 desc->desc[0].wDescriptorLength);
382 /* Send the ack */
383 memset(&ack, sizeof(struct mousevsc_prt_msg), 0);
385 ack.PacketType = PipeMessageData;
386 ack.DataSize = sizeof(struct synthhid_device_info_ack);
388 ack.u.Ack.header.type = SynthHidInitialDeviceInfoAck;
389 ack.u.Ack.header.size = 1;
390 ack.u.Ack.Reserved = 0;
392 ret = vmbus_sendpacket(InputDevice->Device->channel,
393 &ack,
394 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
395 sizeof(struct synthhid_device_info_ack),
396 (unsigned long)&ack,
397 VM_PKT_DATA_INBAND,
398 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
399 if (ret != 0) {
400 pr_err("unable to send synthhid device info ack - ret %d",
401 ret);
402 goto Cleanup;
405 InputDevice->device_wait_condition = 1;
406 wake_up(&InputDevice->DeviceInfoWaitEvent);
408 return;
410 Cleanup:
411 if (InputDevice->HidDesc) {
412 kfree(InputDevice->HidDesc);
413 InputDevice->HidDesc = NULL;
416 if (InputDevice->ReportDesc) {
417 kfree(InputDevice->ReportDesc);
418 InputDevice->ReportDesc = NULL;
421 InputDevice->DeviceInfoStatus = -1;
422 InputDevice->device_wait_condition = 1;
423 wake_up(&InputDevice->DeviceInfoWaitEvent);
426 static void MousevscOnReceiveInputReport(struct mousevsc_dev *InputDevice, struct synthhid_input_report *InputReport)
428 struct mousevsc_drv_obj *inputDriver;
430 if (!InputDevice->bInitializeComplete) {
431 pr_info("Initialization incomplete...ignoring InputReport msg");
432 return;
435 inputDriver = (struct mousevsc_drv_obj *)InputDevice->Device->drv;
437 inputreport_callback(InputDevice->Device,
438 InputReport->ReportBuffer,
439 InputReport->header.size);
442 static void MousevscOnReceive(struct hv_device *Device, struct vmpacket_descriptor *Packet)
444 struct pipe_prt_msg *pipeMsg;
445 struct synthhid_msg *hidMsg;
446 struct mousevsc_dev *inputDevice;
448 inputDevice = MustGetInputDevice(Device);
449 if (!inputDevice) {
450 pr_err("unable to get input device...device being destroyed?");
451 return;
454 pipeMsg = (struct pipe_prt_msg *)((unsigned long)Packet + (Packet->offset8 << 3));
456 if (pipeMsg->PacketType != PipeMessageData) {
457 pr_err("unknown pipe msg type - type %d len %d",
458 pipeMsg->PacketType, pipeMsg->DataSize);
459 PutInputDevice(Device);
460 return ;
463 hidMsg = (struct synthhid_msg *)&pipeMsg->Data[0];
465 switch (hidMsg->header.type) {
466 case SynthHidProtocolResponse:
467 memcpy(&inputDevice->ProtocolResp, pipeMsg, pipeMsg->DataSize+sizeof(struct pipe_prt_msg) - sizeof(unsigned char));
468 inputDevice->protocol_wait_condition = 1;
469 wake_up(&inputDevice->ProtocolWaitEvent);
470 break;
472 case SynthHidInitialDeviceInfo:
473 WARN_ON(pipeMsg->DataSize >= sizeof(struct hv_input_dev_info));
476 * Parse out the device info into device attr,
477 * hid desc and report desc
479 MousevscOnReceiveDeviceInfo(inputDevice,
480 (struct synthhid_device_info *)&pipeMsg->Data[0]);
481 break;
482 case SynthHidInputReport:
483 MousevscOnReceiveInputReport(inputDevice,
484 (struct synthhid_input_report *)&pipeMsg->Data[0]);
486 break;
487 default:
488 pr_err("unsupported hid msg type - type %d len %d",
489 hidMsg->header.type, hidMsg->header.size);
490 break;
493 PutInputDevice(Device);
496 static void MousevscOnChannelCallback(void *Context)
498 const int packetSize = 0x100;
499 int ret = 0;
500 struct hv_device *device = (struct hv_device *)Context;
501 struct mousevsc_dev *inputDevice;
503 u32 bytesRecvd;
504 u64 requestId;
505 unsigned char packet[packetSize];
506 struct vmpacket_descriptor *desc;
507 unsigned char *buffer = packet;
508 int bufferlen = packetSize;
510 inputDevice = MustGetInputDevice(device);
512 if (!inputDevice) {
513 pr_err("unable to get input device...device being destroyed?");
514 return;
517 do {
518 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen, &bytesRecvd, &requestId);
520 if (ret == 0) {
521 if (bytesRecvd > 0) {
522 desc = (struct vmpacket_descriptor *)buffer;
524 switch (desc->type) {
525 case VM_PKT_COMP:
526 MousevscOnSendCompletion(device,
527 desc);
528 break;
530 case VM_PKT_DATA_INBAND:
531 MousevscOnReceive(device, desc);
532 break;
534 default:
535 pr_err("unhandled packet type %d, tid %llx len %d\n",
536 desc->type,
537 requestId,
538 bytesRecvd);
539 break;
542 /* reset */
543 if (bufferlen > packetSize) {
544 kfree(buffer);
546 buffer = packet;
547 bufferlen = packetSize;
549 } else {
551 * pr_debug("nothing else to read...");
552 * reset
554 if (bufferlen > packetSize) {
555 kfree(buffer);
557 buffer = packet;
558 bufferlen = packetSize;
560 break;
562 } else if (ret == -2) {
563 /* Handle large packet */
564 bufferlen = bytesRecvd;
565 buffer = kzalloc(bytesRecvd, GFP_KERNEL);
567 if (buffer == NULL) {
568 buffer = packet;
569 bufferlen = packetSize;
571 /* Try again next time around */
572 pr_err("unable to allocate buffer of size %d!",
573 bytesRecvd);
574 break;
577 } while (1);
579 PutInputDevice(device);
581 return;
584 static int MousevscConnectToVsp(struct hv_device *Device)
586 int ret = 0;
587 struct mousevsc_dev *inputDevice;
588 struct mousevsc_prt_msg *request;
589 struct mousevsc_prt_msg *response;
591 inputDevice = GetInputDevice(Device);
593 if (!inputDevice) {
594 pr_err("unable to get input device...device being destroyed?");
595 return -1;
598 init_waitqueue_head(&inputDevice->ProtocolWaitEvent);
599 init_waitqueue_head(&inputDevice->DeviceInfoWaitEvent);
601 request = &inputDevice->ProtocolReq;
604 * Now, initiate the vsc/vsp initialization protocol on the open channel
606 memset(request, sizeof(struct mousevsc_prt_msg), 0);
608 request->PacketType = PipeMessageData;
609 request->DataSize = sizeof(struct synthhid_protocol_request);
611 request->u.Request.header.type = SynthHidProtocolRequest;
612 request->u.Request.header.size = sizeof(unsigned long);
613 request->u.Request.version_requested.version = SYNTHHID_INPUT_VERSION;
615 pr_info("synthhid protocol request...");
617 ret = vmbus_sendpacket(Device->channel, request,
618 sizeof(struct pipe_prt_msg) -
619 sizeof(unsigned char) +
620 sizeof(struct synthhid_protocol_request),
621 (unsigned long)request,
622 VM_PKT_DATA_INBAND,
623 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
624 if (ret != 0) {
625 pr_err("unable to send synthhid protocol request.");
626 goto Cleanup;
629 inputDevice->protocol_wait_condition = 0;
630 wait_event_timeout(inputDevice->ProtocolWaitEvent, inputDevice->protocol_wait_condition, msecs_to_jiffies(1000));
631 if (inputDevice->protocol_wait_condition == 0) {
632 ret = -ETIMEDOUT;
633 goto Cleanup;
636 response = &inputDevice->ProtocolResp;
638 if (!response->u.Response.approved) {
639 pr_err("synthhid protocol request failed (version %d)",
640 SYNTHHID_INPUT_VERSION);
641 ret = -1;
642 goto Cleanup;
645 inputDevice->device_wait_condition = 0;
646 wait_event_timeout(inputDevice->DeviceInfoWaitEvent, inputDevice->device_wait_condition, msecs_to_jiffies(1000));
647 if (inputDevice->device_wait_condition == 0) {
648 ret = -ETIMEDOUT;
649 goto Cleanup;
653 * We should have gotten the device attr, hid desc and report
654 * desc at this point
656 if (!inputDevice->DeviceInfoStatus)
657 pr_info("**** input channel up and running!! ****");
658 else
659 ret = -1;
661 Cleanup:
662 PutInputDevice(Device);
664 return ret;
667 static int MousevscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
669 int ret = 0;
670 struct mousevsc_dev *inputDevice;
671 struct mousevsc_drv_obj *inputDriver;
672 struct hv_input_dev_info dev_info;
674 inputDevice = AllocInputDevice(Device);
676 if (!inputDevice) {
677 ret = -1;
678 goto Cleanup;
681 inputDevice->bInitializeComplete = false;
683 /* Open the channel */
684 ret = vmbus_open(Device->channel,
685 INPUTVSC_SEND_RING_BUFFER_SIZE,
686 INPUTVSC_RECV_RING_BUFFER_SIZE,
687 NULL,
689 MousevscOnChannelCallback,
690 Device
693 if (ret != 0) {
694 pr_err("unable to open channel: %d", ret);
695 return -1;
698 pr_info("InputVsc channel open: %d", ret);
700 ret = MousevscConnectToVsp(Device);
702 if (ret != 0) {
703 pr_err("unable to connect channel: %d", ret);
705 vmbus_close(Device->channel);
706 return ret;
709 inputDriver = (struct mousevsc_drv_obj *)inputDevice->Device->drv;
711 dev_info.vendor = inputDevice->hid_dev_info.vendor;
712 dev_info.product = inputDevice->hid_dev_info.product;
713 dev_info.version = inputDevice->hid_dev_info.version;
714 strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
716 /* Send the device info back up */
717 deviceinfo_callback(Device, &dev_info);
719 /* Send the report desc back up */
720 /* workaround SA-167 */
721 if (inputDevice->ReportDesc[14] == 0x25)
722 inputDevice->ReportDesc[14] = 0x29;
724 reportdesc_callback(Device, inputDevice->ReportDesc,
725 inputDevice->ReportDescSize);
727 inputDevice->bInitializeComplete = true;
729 Cleanup:
730 return ret;
733 static int MousevscOnDeviceRemove(struct hv_device *Device)
735 struct mousevsc_dev *inputDevice;
736 int ret = 0;
738 pr_info("disabling input device (%p)...",
739 Device->ext);
741 inputDevice = ReleaseInputDevice(Device);
745 * At this point, all outbound traffic should be disable. We only
746 * allow inbound traffic (responses) to proceed
748 * so that outstanding requests can be completed.
750 while (inputDevice->NumOutstandingRequests) {
751 pr_info("waiting for %d requests to complete...", inputDevice->NumOutstandingRequests);
753 udelay(100);
756 pr_info("removing input device (%p)...", Device->ext);
758 inputDevice = FinalReleaseInputDevice(Device);
760 pr_info("input device (%p) safe to remove", inputDevice);
762 /* Close the channel */
763 vmbus_close(Device->channel);
765 FreeInputDevice(inputDevice);
767 return ret;
770 static void MousevscOnCleanup(struct hv_driver *drv)
775 * Data types
777 struct input_device_context {
778 struct vm_device *device_ctx;
779 struct hid_device *hid_device;
780 struct hv_input_dev_info device_info;
781 int connected;
784 struct mousevsc_driver_context {
785 struct driver_context drv_ctx;
786 struct mousevsc_drv_obj drv_obj;
789 static struct mousevsc_driver_context g_mousevsc_drv;
791 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
793 struct vm_device *device_ctx = to_vm_device(dev);
794 struct input_device_context *input_device_ctx =
795 dev_get_drvdata(&device_ctx->device);
797 memcpy(&input_device_ctx->device_info, info,
798 sizeof(struct hv_input_dev_info));
800 DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
803 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
805 int ret = 0;
807 struct vm_device *device_ctx = to_vm_device(dev);
808 struct input_device_context *input_dev_ctx =
809 dev_get_drvdata(&device_ctx->device);
811 ret = hid_input_report(input_dev_ctx->hid_device,
812 HID_INPUT_REPORT, packet, len, 1);
814 DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
817 static int mousevsc_hid_open(struct hid_device *hid)
819 return 0;
822 static void mousevsc_hid_close(struct hid_device *hid)
826 static int mousevsc_probe(struct device *device)
828 int ret = 0;
830 struct driver_context *driver_ctx =
831 driver_to_driver_context(device->driver);
832 struct mousevsc_driver_context *mousevsc_drv_ctx =
833 (struct mousevsc_driver_context *)driver_ctx;
834 struct mousevsc_drv_obj *mousevsc_drv_obj = &mousevsc_drv_ctx->drv_obj;
836 struct vm_device *device_ctx = device_to_vm_device(device);
837 struct hv_device *device_obj = &device_ctx->device_obj;
838 struct input_device_context *input_dev_ctx;
840 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
841 GFP_KERNEL);
843 dev_set_drvdata(device, input_dev_ctx);
845 /* Call to the vsc driver to add the device */
846 ret = mousevsc_drv_obj->Base.dev_add(device_obj, NULL);
848 if (ret != 0) {
849 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
851 return -1;
854 return 0;
857 static int mousevsc_remove(struct device *device)
859 int ret = 0;
861 struct driver_context *driver_ctx =
862 driver_to_driver_context(device->driver);
863 struct mousevsc_driver_context *mousevsc_drv_ctx =
864 (struct mousevsc_driver_context *)driver_ctx;
865 struct mousevsc_drv_obj *mousevsc_drv_obj = &mousevsc_drv_ctx->drv_obj;
867 struct vm_device *device_ctx = device_to_vm_device(device);
868 struct hv_device *device_obj = &device_ctx->device_obj;
869 struct input_device_context *input_dev_ctx;
871 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
872 GFP_KERNEL);
874 dev_set_drvdata(device, input_dev_ctx);
876 if (input_dev_ctx->connected) {
877 hidinput_disconnect(input_dev_ctx->hid_device);
878 input_dev_ctx->connected = 0;
881 if (!mousevsc_drv_obj->Base.dev_rm)
882 return -1;
885 * Call to the vsc driver to let it know that the device
886 * is being removed
888 ret = mousevsc_drv_obj->Base.dev_rm(device_obj);
890 if (ret != 0) {
891 DPRINT_ERR(INPUTVSC_DRV,
892 "unable to remove vsc device (ret %d)", ret);
895 kfree(input_dev_ctx);
897 return ret;
900 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
902 struct vm_device *device_ctx = to_vm_device(dev);
903 struct input_device_context *input_device_ctx =
904 dev_get_drvdata(&device_ctx->device);
905 struct hid_device *hid_dev;
907 /* hid_debug = -1; */
908 hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
910 if (hid_parse_report(hid_dev, packet, len)) {
911 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
912 return;
915 if (hid_dev) {
916 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
918 hid_dev->ll_driver->open = mousevsc_hid_open;
919 hid_dev->ll_driver->close = mousevsc_hid_close;
921 hid_dev->bus = BUS_VIRTUAL;
922 hid_dev->vendor = input_device_ctx->device_info.vendor;
923 hid_dev->product = input_device_ctx->device_info.product;
924 hid_dev->version = input_device_ctx->device_info.version;
925 hid_dev->dev = device_ctx->device;
927 sprintf(hid_dev->name, "%s",
928 input_device_ctx->device_info.name);
931 * HJ Do we want to call it with a 0
933 if (!hidinput_connect(hid_dev, 0)) {
934 hid_dev->claimed |= HID_CLAIMED_INPUT;
936 input_device_ctx->connected = 1;
938 DPRINT_INFO(INPUTVSC_DRV,
939 "HID device claimed by input\n");
942 if (!hid_dev->claimed) {
943 DPRINT_ERR(INPUTVSC_DRV,
944 "HID device not claimed by "
945 "input or hiddev\n");
948 input_device_ctx->hid_device = hid_dev;
951 kfree(hid_dev);
954 static int mousevsc_drv_exit_cb(struct device *dev, void *data)
956 struct device **curr = (struct device **)data;
957 *curr = dev;
959 return 1;
962 static void mousevsc_drv_exit(void)
964 struct mousevsc_drv_obj *mousevsc_drv_obj = &g_mousevsc_drv.drv_obj;
965 struct driver_context *drv_ctx = &g_mousevsc_drv.drv_ctx;
966 int ret;
968 struct device *current_dev = NULL;
970 while (1) {
971 current_dev = NULL;
973 /* Get the device */
974 ret = driver_for_each_device(&drv_ctx->driver, NULL,
975 (void *)&current_dev,
976 mousevsc_drv_exit_cb);
977 if (ret)
978 printk(KERN_ERR "Can't find mouse device!\n");
980 if (current_dev == NULL)
981 break;
983 /* Initiate removal from the top-down */
984 device_unregister(current_dev);
987 if (mousevsc_drv_obj->Base.cleanup)
988 mousevsc_drv_obj->Base.cleanup(&mousevsc_drv_obj->Base);
990 vmbus_child_driver_unregister(drv_ctx);
992 return;
995 static int mouse_vsc_initialize(struct hv_driver *Driver)
997 struct mousevsc_drv_obj *inputDriver =
998 (struct mousevsc_drv_obj *)Driver;
999 int ret = 0;
1001 Driver->name = gDriverName;
1002 memcpy(&Driver->dev_type, &gMousevscDeviceType,
1003 sizeof(struct hv_guid));
1005 /* Setup the dispatch table */
1006 inputDriver->Base.dev_add = MousevscOnDeviceAdd;
1007 inputDriver->Base.dev_rm = MousevscOnDeviceRemove;
1008 inputDriver->Base.cleanup = MousevscOnCleanup;
1010 return ret;
1014 static int __init mousevsc_init(void)
1016 struct mousevsc_drv_obj *input_drv_obj = &g_mousevsc_drv.drv_obj;
1017 struct driver_context *drv_ctx = &g_mousevsc_drv.drv_ctx;
1019 DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
1021 /* Callback to client driver to complete the initialization */
1022 mouse_vsc_initialize(&input_drv_obj->Base);
1024 drv_ctx->driver.name = input_drv_obj->Base.name;
1025 memcpy(&drv_ctx->class_id, &input_drv_obj->Base.dev_type,
1026 sizeof(struct hv_guid));
1028 drv_ctx->probe = mousevsc_probe;
1029 drv_ctx->remove = mousevsc_remove;
1031 /* The driver belongs to vmbus */
1032 vmbus_child_driver_register(drv_ctx);
1034 return 0;
1037 static void __exit mousevsc_exit(void)
1039 mousevsc_drv_exit();
1043 * We don't want to automatically load this driver just yet, it's quite
1044 * broken. It's safe if you want to load it yourself manually, but
1045 * don't inflict it on unsuspecting users, that's just mean.
1047 #if 0
1050 * We use a PCI table to determine if we should autoload this driver This is
1051 * needed by distro tools to determine if the hyperv drivers should be
1052 * installed and/or configured. We don't do anything else with the table, but
1053 * it needs to be present.
1055 const static struct pci_device_id microsoft_hv_pci_table[] = {
1056 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
1057 { 0 }
1059 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
1060 #endif
1062 MODULE_LICENSE("GPL");
1063 MODULE_VERSION(HV_DRV_VERSION);
1064 module_init(mousevsc_init);
1065 module_exit(mousevsc_exit);