Staging: hv: hv_mouse: fix up pipe size field name
[zen-stable.git] / drivers / staging / hv / hv_mouse.c
blob5bee3ffb831ddc243ee7c9b5691d0919feb9c591
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 struct hid_descriptor hid_descriptor;
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 buffer[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 type;
149 u32 size;
150 char Data[1];
154 * Data types
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;
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 = &DeviceInfo->hid_descriptor;
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.type = PipeMessageData;
386 ack.size = sizeof(struct synthhid_device_info_ack);
388 ack.Ack.header.type = SynthHidInitialDeviceInfoAck;
389 ack.Ack.header.size = 1;
390 ack.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->buffer,
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->type != PipeMessageData) {
457 pr_err("unknown pipe msg type - type %d len %d",
458 pipeMsg->type, pipeMsg->size);
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,
468 pipeMsg->size + sizeof(struct pipe_prt_msg) -
469 sizeof(unsigned char));
470 inputDevice->protocol_wait_condition = 1;
471 wake_up(&inputDevice->ProtocolWaitEvent);
472 break;
474 case SynthHidInitialDeviceInfo:
475 WARN_ON(pipeMsg->size >= sizeof(struct hv_input_dev_info));
478 * Parse out the device info into device attr,
479 * hid desc and report desc
481 MousevscOnReceiveDeviceInfo(inputDevice,
482 (struct synthhid_device_info *)&pipeMsg->Data[0]);
483 break;
484 case SynthHidInputReport:
485 MousevscOnReceiveInputReport(inputDevice,
486 (struct synthhid_input_report *)&pipeMsg->Data[0]);
488 break;
489 default:
490 pr_err("unsupported hid msg type - type %d len %d",
491 hidMsg->header.type, hidMsg->header.size);
492 break;
495 PutInputDevice(Device);
498 static void MousevscOnChannelCallback(void *Context)
500 const int packetSize = 0x100;
501 int ret = 0;
502 struct hv_device *device = (struct hv_device *)Context;
503 struct mousevsc_dev *inputDevice;
505 u32 bytesRecvd;
506 u64 requestId;
507 unsigned char packet[packetSize];
508 struct vmpacket_descriptor *desc;
509 unsigned char *buffer = packet;
510 int bufferlen = packetSize;
512 inputDevice = MustGetInputDevice(device);
514 if (!inputDevice) {
515 pr_err("unable to get input device...device being destroyed?");
516 return;
519 do {
520 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen, &bytesRecvd, &requestId);
522 if (ret == 0) {
523 if (bytesRecvd > 0) {
524 desc = (struct vmpacket_descriptor *)buffer;
526 switch (desc->type) {
527 case VM_PKT_COMP:
528 MousevscOnSendCompletion(device,
529 desc);
530 break;
532 case VM_PKT_DATA_INBAND:
533 MousevscOnReceive(device, desc);
534 break;
536 default:
537 pr_err("unhandled packet type %d, tid %llx len %d\n",
538 desc->type,
539 requestId,
540 bytesRecvd);
541 break;
544 /* reset */
545 if (bufferlen > packetSize) {
546 kfree(buffer);
548 buffer = packet;
549 bufferlen = packetSize;
551 } else {
553 * pr_debug("nothing else to read...");
554 * reset
556 if (bufferlen > packetSize) {
557 kfree(buffer);
559 buffer = packet;
560 bufferlen = packetSize;
562 break;
564 } else if (ret == -2) {
565 /* Handle large packet */
566 bufferlen = bytesRecvd;
567 buffer = kzalloc(bytesRecvd, GFP_KERNEL);
569 if (buffer == NULL) {
570 buffer = packet;
571 bufferlen = packetSize;
573 /* Try again next time around */
574 pr_err("unable to allocate buffer of size %d!",
575 bytesRecvd);
576 break;
579 } while (1);
581 PutInputDevice(device);
583 return;
586 static int MousevscConnectToVsp(struct hv_device *Device)
588 int ret = 0;
589 struct mousevsc_dev *inputDevice;
590 struct mousevsc_prt_msg *request;
591 struct mousevsc_prt_msg *response;
593 inputDevice = GetInputDevice(Device);
595 if (!inputDevice) {
596 pr_err("unable to get input device...device being destroyed?");
597 return -1;
600 init_waitqueue_head(&inputDevice->ProtocolWaitEvent);
601 init_waitqueue_head(&inputDevice->DeviceInfoWaitEvent);
603 request = &inputDevice->ProtocolReq;
606 * Now, initiate the vsc/vsp initialization protocol on the open channel
608 memset(request, sizeof(struct mousevsc_prt_msg), 0);
610 request->type = PipeMessageData;
611 request->size = sizeof(struct synthhid_protocol_request);
613 request->Request.header.type = SynthHidProtocolRequest;
614 request->Request.header.size = sizeof(unsigned long);
615 request->Request.version_requested.version = SYNTHHID_INPUT_VERSION;
617 pr_info("synthhid protocol request...");
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;
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;
638 response = &inputDevice->ProtocolResp;
640 if (!response->Response.approved) {
641 pr_err("synthhid protocol request failed (version %d)",
642 SYNTHHID_INPUT_VERSION);
643 ret = -1;
644 goto Cleanup;
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;
655 * We should have gotten the device attr, hid desc and report
656 * desc at this point
658 if (!inputDevice->DeviceInfoStatus)
659 pr_info("**** input channel up and running!! ****");
660 else
661 ret = -1;
663 Cleanup:
664 PutInputDevice(Device);
666 return ret;
669 static int MousevscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
671 int ret = 0;
672 struct mousevsc_dev *inputDevice;
673 struct mousevsc_drv_obj *inputDriver;
674 struct hv_input_dev_info dev_info;
676 inputDevice = AllocInputDevice(Device);
678 if (!inputDevice) {
679 ret = -1;
680 goto Cleanup;
683 inputDevice->bInitializeComplete = false;
685 /* Open the channel */
686 ret = vmbus_open(Device->channel,
687 INPUTVSC_SEND_RING_BUFFER_SIZE,
688 INPUTVSC_RECV_RING_BUFFER_SIZE,
689 NULL,
691 MousevscOnChannelCallback,
692 Device
695 if (ret != 0) {
696 pr_err("unable to open channel: %d", ret);
697 return -1;
700 pr_info("InputVsc channel open: %d", ret);
702 ret = MousevscConnectToVsp(Device);
704 if (ret != 0) {
705 pr_err("unable to connect channel: %d", ret);
707 vmbus_close(Device->channel);
708 return ret;
711 inputDriver = (struct mousevsc_drv_obj *)inputDevice->Device->drv;
713 dev_info.vendor = inputDevice->hid_dev_info.vendor;
714 dev_info.product = inputDevice->hid_dev_info.product;
715 dev_info.version = inputDevice->hid_dev_info.version;
716 strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
718 /* Send the device info back up */
719 deviceinfo_callback(Device, &dev_info);
721 /* Send the report desc back up */
722 /* workaround SA-167 */
723 if (inputDevice->ReportDesc[14] == 0x25)
724 inputDevice->ReportDesc[14] = 0x29;
726 reportdesc_callback(Device, inputDevice->ReportDesc,
727 inputDevice->ReportDescSize);
729 inputDevice->bInitializeComplete = true;
731 Cleanup:
732 return ret;
735 static int MousevscOnDeviceRemove(struct hv_device *Device)
737 struct mousevsc_dev *inputDevice;
738 int ret = 0;
740 pr_info("disabling input device (%p)...",
741 Device->ext);
743 inputDevice = ReleaseInputDevice(Device);
747 * At this point, all outbound traffic should be disable. We only
748 * allow inbound traffic (responses) to proceed
750 * so that outstanding requests can be completed.
752 while (inputDevice->NumOutstandingRequests) {
753 pr_info("waiting for %d requests to complete...", inputDevice->NumOutstandingRequests);
755 udelay(100);
758 pr_info("removing input device (%p)...", Device->ext);
760 inputDevice = FinalReleaseInputDevice(Device);
762 pr_info("input device (%p) safe to remove", inputDevice);
764 /* Close the channel */
765 vmbus_close(Device->channel);
767 FreeInputDevice(inputDevice);
769 return ret;
772 static void MousevscOnCleanup(struct hv_driver *drv)
777 * Data types
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;
786 struct mousevsc_driver_context {
787 struct driver_context drv_ctx;
788 struct mousevsc_drv_obj drv_obj;
791 static struct mousevsc_driver_context g_mousevsc_drv;
793 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
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);
799 memcpy(&input_device_ctx->device_info, info,
800 sizeof(struct hv_input_dev_info));
802 DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
805 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
807 int ret = 0;
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);
813 ret = hid_input_report(input_dev_ctx->hid_device,
814 HID_INPUT_REPORT, packet, len, 1);
816 DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
819 static int mousevsc_hid_open(struct hid_device *hid)
821 return 0;
824 static void mousevsc_hid_close(struct hid_device *hid)
828 static int mousevsc_probe(struct device *device)
830 int ret = 0;
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;
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;
842 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
843 GFP_KERNEL);
845 dev_set_drvdata(device, input_dev_ctx);
847 /* Call to the vsc driver to add the device */
848 ret = mousevsc_drv_obj->Base.dev_add(device_obj, NULL);
850 if (ret != 0) {
851 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
853 return -1;
856 return 0;
859 static int mousevsc_remove(struct device *device)
861 int ret = 0;
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;
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;
873 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
874 GFP_KERNEL);
876 dev_set_drvdata(device, input_dev_ctx);
878 if (input_dev_ctx->connected) {
879 hidinput_disconnect(input_dev_ctx->hid_device);
880 input_dev_ctx->connected = 0;
883 if (!mousevsc_drv_obj->Base.dev_rm)
884 return -1;
887 * Call to the vsc driver to let it know that the device
888 * is being removed
890 ret = mousevsc_drv_obj->Base.dev_rm(device_obj);
892 if (ret != 0) {
893 DPRINT_ERR(INPUTVSC_DRV,
894 "unable to remove vsc device (ret %d)", ret);
897 kfree(input_dev_ctx);
899 return ret;
902 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
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;
909 /* hid_debug = -1; */
910 hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
912 if (hid_parse_report(hid_dev, packet, len)) {
913 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
914 return;
917 if (hid_dev) {
918 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
920 hid_dev->ll_driver->open = mousevsc_hid_open;
921 hid_dev->ll_driver->close = mousevsc_hid_close;
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;
929 sprintf(hid_dev->name, "%s",
930 input_device_ctx->device_info.name);
933 * HJ Do we want to call it with a 0
935 if (!hidinput_connect(hid_dev, 0)) {
936 hid_dev->claimed |= HID_CLAIMED_INPUT;
938 input_device_ctx->connected = 1;
940 DPRINT_INFO(INPUTVSC_DRV,
941 "HID device claimed by input\n");
944 if (!hid_dev->claimed) {
945 DPRINT_ERR(INPUTVSC_DRV,
946 "HID device not claimed by "
947 "input or hiddev\n");
950 input_device_ctx->hid_device = hid_dev;
953 kfree(hid_dev);
956 static int mousevsc_drv_exit_cb(struct device *dev, void *data)
958 struct device **curr = (struct device **)data;
959 *curr = dev;
961 return 1;
964 static void mousevsc_drv_exit(void)
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;
970 struct device *current_dev = NULL;
972 while (1) {
973 current_dev = NULL;
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");
982 if (current_dev == NULL)
983 break;
985 /* Initiate removal from the top-down */
986 device_unregister(current_dev);
989 if (mousevsc_drv_obj->Base.cleanup)
990 mousevsc_drv_obj->Base.cleanup(&mousevsc_drv_obj->Base);
992 vmbus_child_driver_unregister(drv_ctx);
994 return;
997 static int mouse_vsc_initialize(struct hv_driver *Driver)
999 struct mousevsc_drv_obj *inputDriver =
1000 (struct mousevsc_drv_obj *)Driver;
1001 int ret = 0;
1003 Driver->name = gDriverName;
1004 memcpy(&Driver->dev_type, &gMousevscDeviceType,
1005 sizeof(struct hv_guid));
1007 /* Setup the dispatch table */
1008 inputDriver->Base.dev_add = MousevscOnDeviceAdd;
1009 inputDriver->Base.dev_rm = MousevscOnDeviceRemove;
1010 inputDriver->Base.cleanup = MousevscOnCleanup;
1012 return ret;
1016 static int __init mousevsc_init(void)
1018 struct mousevsc_drv_obj *input_drv_obj = &g_mousevsc_drv.drv_obj;
1019 struct driver_context *drv_ctx = &g_mousevsc_drv.drv_ctx;
1021 DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
1023 /* Callback to client driver to complete the initialization */
1024 mouse_vsc_initialize(&input_drv_obj->Base);
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));
1030 drv_ctx->probe = mousevsc_probe;
1031 drv_ctx->remove = mousevsc_remove;
1033 /* The driver belongs to vmbus */
1034 vmbus_child_driver_register(drv_ctx);
1036 return 0;
1039 static void __exit mousevsc_exit(void)
1041 mousevsc_drv_exit();
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.
1049 #if 0
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.
1057 const static struct pci_device_id microsoft_hv_pci_table[] = {
1058 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
1059 { 0 }
1061 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
1062 #endif
1064 MODULE_LICENSE("GPL");
1065 MODULE_VERSION(HV_DRV_VERSION);
1066 module_init(mousevsc_init);
1067 module_exit(mousevsc_exit);