Staging: merge 2.6.39-rc3 into staging-next
[zen-stable.git] / drivers / staging / hv / hv_mouse.c
blobbd935489a2a37e01c4ff884cc66811929732d51c
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/delay.h>
18 #include <linux/device.h>
19 #include <linux/workqueue.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/input.h>
23 #include <linux/hid.h>
24 #include <linux/hiddev.h>
25 #include <linux/pci.h>
26 #include <linux/dmi.h>
27 #include <linux/delay.h>
29 #include "hv_api.h"
30 #include "logging.h"
31 #include "version_info.h"
32 #include "vmbus.h"
33 #include "vmbus_api.h"
34 #include "channel.h"
35 #include "vmbus_packet_format.h"
39 * Data types
41 struct hv_input_dev_info {
42 unsigned short vendor;
43 unsigned short product;
44 unsigned short version;
45 char name[128];
48 /* Represents the input vsc driver */
49 /* FIXME - can be removed entirely */
50 struct mousevsc_drv_obj {
51 struct hv_driver base;
55 /* The maximum size of a synthetic input message. */
56 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
59 * Current version
61 * History:
62 * Beta, RC < 2008/1/22 1,0
63 * RC > 2008/1/22 2,0
65 #define SYNTHHID_INPUT_VERSION_MAJOR 2
66 #define SYNTHHID_INPUT_VERSION_MINOR 0
67 #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
68 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
71 #pragma pack(push,1)
73 * Message types in the synthetic input protocol
75 enum synthhid_msg_type {
76 SynthHidProtocolRequest,
77 SynthHidProtocolResponse,
78 SynthHidInitialDeviceInfo,
79 SynthHidInitialDeviceInfoAck,
80 SynthHidInputReport,
81 SynthHidMax
85 * Basic message structures.
87 struct synthhid_msg_hdr {
88 enum synthhid_msg_type type;
89 u32 size;
92 struct synthhid_msg {
93 struct synthhid_msg_hdr header;
94 char data[1]; /* Enclosed message */
97 union synthhid_version {
98 struct {
99 u16 minor_version;
100 u16 major_version;
102 u32 version;
106 * Protocol messages
108 struct synthhid_protocol_request {
109 struct synthhid_msg_hdr header;
110 union synthhid_version version_requested;
113 struct synthhid_protocol_response {
114 struct synthhid_msg_hdr header;
115 union synthhid_version version_requested;
116 unsigned char approved;
119 struct synthhid_device_info {
120 struct synthhid_msg_hdr header;
121 struct hv_input_dev_info hid_dev_info;
122 struct hid_descriptor hid_descriptor;
125 struct synthhid_device_info_ack {
126 struct synthhid_msg_hdr header;
127 unsigned char reserved;
130 struct synthhid_input_report {
131 struct synthhid_msg_hdr header;
132 char buffer[1];
135 #pragma pack(pop)
137 #define INPUTVSC_SEND_RING_BUFFER_SIZE 10*PAGE_SIZE
138 #define INPUTVSC_RECV_RING_BUFFER_SIZE 10*PAGE_SIZE
140 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
142 enum pipe_prot_msg_type {
143 PipeMessageInvalid = 0,
144 PipeMessageData,
145 PipeMessageMaximum
149 struct pipe_prt_msg {
150 enum pipe_prot_msg_type type;
151 u32 size;
152 char data[1];
156 * Data types
158 struct mousevsc_prt_msg {
159 enum pipe_prot_msg_type type;
160 u32 size;
161 union {
162 struct synthhid_protocol_request request;
163 struct synthhid_protocol_response response;
164 struct synthhid_device_info_ack ack;
169 * Represents an mousevsc device
171 struct mousevsc_dev {
172 struct hv_device *device;
173 /* 0 indicates the device is being destroyed */
174 atomic_t ref_count;
175 int num_outstanding_req;
176 unsigned char init_complete;
177 struct mousevsc_prt_msg protocol_req;
178 struct mousevsc_prt_msg protocol_resp;
179 /* Synchronize the request/response if needed */
180 wait_queue_head_t protocol_wait_event;
181 wait_queue_head_t dev_info_wait_event;
182 int protocol_wait_condition;
183 int device_wait_condition;
184 int dev_info_status;
186 struct hid_descriptor *hid_desc;
187 unsigned char *report_desc;
188 u32 report_desc_size;
189 struct hv_input_dev_info hid_dev_info;
193 static const char *driver_name = "mousevsc";
195 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
196 static const struct hv_guid mouse_guid = {
197 .data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
198 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}
201 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info);
202 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len);
203 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len);
205 static struct mousevsc_dev *alloc_input_device(struct hv_device *device)
207 struct mousevsc_dev *input_dev;
209 input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
211 if (!input_dev)
212 return NULL;
215 * Set to 2 to allow both inbound and outbound traffics
216 * (ie get_input_device() and must_get_input_device()) to proceed.
218 atomic_cmpxchg(&input_dev->ref_count, 0, 2);
220 input_dev->device = device;
221 device->ext = input_dev;
223 return input_dev;
226 static void free_input_device(struct mousevsc_dev *device)
228 WARN_ON(atomic_read(&device->ref_count) == 0);
229 kfree(device);
233 * Get the inputdevice object if exists and its refcount > 1
235 static struct mousevsc_dev *get_input_device(struct hv_device *device)
237 struct mousevsc_dev *input_dev;
239 input_dev = (struct mousevsc_dev *)device->ext;
242 * FIXME
243 * This sure isn't a valid thing to print for debugging, no matter
244 * what the intention is...
246 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
247 * input_dev->ref_count);
250 if (input_dev && atomic_read(&input_dev->ref_count) > 1)
251 atomic_inc(&input_dev->ref_count);
252 else
253 input_dev = NULL;
255 return input_dev;
259 * Get the inputdevice object iff exists and its refcount > 0
261 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
263 struct mousevsc_dev *input_dev;
265 input_dev = (struct mousevsc_dev *)device->ext;
267 if (input_dev && atomic_read(&input_dev->ref_count))
268 atomic_inc(&input_dev->ref_count);
269 else
270 input_dev = NULL;
272 return input_dev;
275 static void put_input_device(struct hv_device *device)
277 struct mousevsc_dev *input_dev;
279 input_dev = (struct mousevsc_dev *)device->ext;
281 atomic_dec(&input_dev->ref_count);
285 * Drop ref count to 1 to effectively disable get_input_device()
287 static struct mousevsc_dev *release_input_device(struct hv_device *device)
289 struct mousevsc_dev *input_dev;
291 input_dev = (struct mousevsc_dev *)device->ext;
293 /* Busy wait until the ref drop to 2, then set it to 1 */
294 while (atomic_cmpxchg(&input_dev->ref_count, 2, 1) != 2)
295 udelay(100);
297 return input_dev;
301 * Drop ref count to 0. No one can use input_device object.
303 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
305 struct mousevsc_dev *input_dev;
307 input_dev = (struct mousevsc_dev *)device->ext;
309 /* Busy wait until the ref drop to 1, then set it to 0 */
310 while (atomic_cmpxchg(&input_dev->ref_count, 1, 0) != 1)
311 udelay(100);
313 device->ext = NULL;
314 return input_dev;
317 static void mousevsc_on_send_completion(struct hv_device *device,
318 struct vmpacket_descriptor *packet)
320 struct mousevsc_dev *input_dev;
321 void *request;
323 input_dev = must_get_input_device(device);
324 if (!input_dev) {
325 pr_err("unable to get input device...device being destroyed?");
326 return;
329 request = (void *)(unsigned long)packet->trans_id;
331 if (request == &input_dev->protocol_req) {
332 /* FIXME */
333 /* Shouldn't we be doing something here? */
336 put_input_device(device);
339 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
340 struct synthhid_device_info *device_info)
342 int ret = 0;
343 struct hid_descriptor *desc;
344 struct mousevsc_prt_msg ack;
346 /* Assume success for now */
347 input_device->dev_info_status = 0;
349 /* Save the device attr */
350 memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
351 sizeof(struct hv_input_dev_info));
353 /* Save the hid desc */
354 desc = &device_info->hid_descriptor;
355 WARN_ON(desc->bLength > 0);
357 input_device->hid_desc = kzalloc(desc->bLength, GFP_KERNEL);
359 if (!input_device->hid_desc) {
360 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
361 goto Cleanup;
364 memcpy(input_device->hid_desc, desc, desc->bLength);
366 /* Save the report desc */
367 input_device->report_desc_size = desc->desc[0].wDescriptorLength;
368 input_device->report_desc = kzalloc(input_device->report_desc_size,
369 GFP_KERNEL);
371 if (!input_device->report_desc) {
372 pr_err("unable to allocate report descriptor - size %d",
373 input_device->report_desc_size);
374 goto Cleanup;
377 memcpy(input_device->report_desc,
378 ((unsigned char *)desc) + desc->bLength,
379 desc->desc[0].wDescriptorLength);
381 /* Send the ack */
382 memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
384 ack.type = PipeMessageData;
385 ack.size = sizeof(struct synthhid_device_info_ack);
387 ack.ack.header.type = SynthHidInitialDeviceInfoAck;
388 ack.ack.header.size = 1;
389 ack.ack.reserved = 0;
391 ret = vmbus_sendpacket(input_device->device->channel,
392 &ack,
393 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
394 sizeof(struct synthhid_device_info_ack),
395 (unsigned long)&ack,
396 VM_PKT_DATA_INBAND,
397 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
398 if (ret != 0) {
399 pr_err("unable to send synthhid device info ack - ret %d",
400 ret);
401 goto Cleanup;
404 input_device->device_wait_condition = 1;
405 wake_up(&input_device->dev_info_wait_event);
407 return;
409 Cleanup:
410 kfree(input_device->hid_desc);
411 input_device->hid_desc = NULL;
413 kfree(input_device->report_desc);
414 input_device->report_desc = NULL;
416 input_device->dev_info_status = -1;
417 input_device->device_wait_condition = 1;
418 wake_up(&input_device->dev_info_wait_event);
421 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
422 struct synthhid_input_report *input_report)
424 struct mousevsc_drv_obj *input_drv;
426 if (!input_device->init_complete) {
427 pr_info("Initialization incomplete...ignoring input_report msg");
428 return;
431 input_drv = (struct mousevsc_drv_obj *)input_device->device->drv;
433 inputreport_callback(input_device->device,
434 input_report->buffer,
435 input_report->header.size);
438 static void mousevsc_on_receive(struct hv_device *device,
439 struct vmpacket_descriptor *packet)
441 struct pipe_prt_msg *pipe_msg;
442 struct synthhid_msg *hid_msg;
443 struct mousevsc_dev *input_dev;
445 input_dev = must_get_input_device(device);
446 if (!input_dev) {
447 pr_err("unable to get input device...device being destroyed?");
448 return;
451 pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
452 (packet->offset8 << 3));
454 if (pipe_msg->type != PipeMessageData) {
455 pr_err("unknown pipe msg type - type %d len %d",
456 pipe_msg->type, pipe_msg->size);
457 put_input_device(device);
458 return ;
461 hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
463 switch (hid_msg->header.type) {
464 case SynthHidProtocolResponse:
465 memcpy(&input_dev->protocol_resp, pipe_msg,
466 pipe_msg->size + sizeof(struct pipe_prt_msg) -
467 sizeof(unsigned char));
468 input_dev->protocol_wait_condition = 1;
469 wake_up(&input_dev->protocol_wait_event);
470 break;
472 case SynthHidInitialDeviceInfo:
473 WARN_ON(pipe_msg->size >= sizeof(struct hv_input_dev_info));
476 * Parse out the device info into device attr,
477 * hid desc and report desc
479 mousevsc_on_receive_device_info(input_dev,
480 (struct synthhid_device_info *)&pipe_msg->data[0]);
481 break;
482 case SynthHidInputReport:
483 mousevsc_on_receive_input_report(input_dev,
484 (struct synthhid_input_report *)&pipe_msg->data[0]);
486 break;
487 default:
488 pr_err("unsupported hid msg type - type %d len %d",
489 hid_msg->header.type, hid_msg->header.size);
490 break;
493 put_input_device(device);
496 static void mousevsc_on_channel_callback(void *context)
498 const int packetSize = 0x100;
499 int ret = 0;
500 struct hv_device *device = (struct hv_device *)context;
501 struct mousevsc_dev *input_dev;
503 u32 bytes_recvd;
504 u64 req_id;
505 unsigned char packet[packetSize];
506 struct vmpacket_descriptor *desc;
507 unsigned char *buffer = packet;
508 int bufferlen = packetSize;
510 input_dev = must_get_input_device(device);
512 if (!input_dev) {
513 pr_err("unable to get input device...device being destroyed?");
514 return;
517 do {
518 ret = vmbus_recvpacket_raw(device->channel, buffer,
519 bufferlen, &bytes_recvd, &req_id);
521 if (ret == 0) {
522 if (bytes_recvd > 0) {
523 desc = (struct vmpacket_descriptor *)buffer;
525 switch (desc->type) {
526 case VM_PKT_COMP:
527 mousevsc_on_send_completion(
528 device, desc);
529 break;
531 case VM_PKT_DATA_INBAND:
532 mousevsc_on_receive(
533 device, desc);
534 break;
536 default:
537 pr_err("unhandled packet type %d, tid %llx len %d\n",
538 desc->type,
539 req_id,
540 bytes_recvd);
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 = bytes_recvd;
567 buffer = kzalloc(bytes_recvd, 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 bytes_recvd);
576 break;
579 } while (1);
581 put_input_device(device);
583 return;
586 static int mousevsc_connect_to_vsp(struct hv_device *device)
588 int ret = 0;
589 struct mousevsc_dev *input_dev;
590 struct mousevsc_prt_msg *request;
591 struct mousevsc_prt_msg *response;
593 input_dev = get_input_device(device);
595 if (!input_dev) {
596 pr_err("unable to get input device...device being destroyed?");
597 return -1;
600 init_waitqueue_head(&input_dev->protocol_wait_event);
601 init_waitqueue_head(&input_dev->dev_info_wait_event);
603 request = &input_dev->protocol_req;
606 * Now, initiate the vsc/vsp initialization protocol on the open channel
608 memset(request, 0, sizeof(struct mousevsc_prt_msg));
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 input_dev->protocol_wait_condition = 0;
632 wait_event_timeout(input_dev->protocol_wait_event,
633 input_dev->protocol_wait_condition, msecs_to_jiffies(1000));
634 if (input_dev->protocol_wait_condition == 0) {
635 ret = -ETIMEDOUT;
636 goto Cleanup;
639 response = &input_dev->protocol_resp;
641 if (!response->response.approved) {
642 pr_err("synthhid protocol request failed (version %d)",
643 SYNTHHID_INPUT_VERSION);
644 ret = -1;
645 goto Cleanup;
648 input_dev->device_wait_condition = 0;
649 wait_event_timeout(input_dev->dev_info_wait_event,
650 input_dev->device_wait_condition, msecs_to_jiffies(1000));
651 if (input_dev->device_wait_condition == 0) {
652 ret = -ETIMEDOUT;
653 goto Cleanup;
657 * We should have gotten the device attr, hid desc and report
658 * desc at this point
660 if (!input_dev->dev_info_status)
661 pr_info("**** input channel up and running!! ****");
662 else
663 ret = -1;
665 Cleanup:
666 put_input_device(device);
668 return ret;
671 static int mousevsc_on_device_add(struct hv_device *device,
672 void *additional_info)
674 int ret = 0;
675 struct mousevsc_dev *input_dev;
676 struct mousevsc_drv_obj *input_drv;
677 struct hv_input_dev_info dev_info;
679 input_dev = alloc_input_device(device);
681 if (!input_dev) {
682 ret = -1;
683 goto Cleanup;
686 input_dev->init_complete = false;
688 /* Open the channel */
689 ret = vmbus_open(device->channel,
690 INPUTVSC_SEND_RING_BUFFER_SIZE,
691 INPUTVSC_RECV_RING_BUFFER_SIZE,
692 NULL,
694 mousevsc_on_channel_callback,
695 device
698 if (ret != 0) {
699 pr_err("unable to open channel: %d", ret);
700 free_input_device(input_dev);
701 return -1;
704 pr_info("InputVsc channel open: %d", ret);
706 ret = mousevsc_connect_to_vsp(device);
708 if (ret != 0) {
709 pr_err("unable to connect channel: %d", ret);
711 vmbus_close(device->channel);
712 free_input_device(input_dev);
713 return ret;
716 input_drv = (struct mousevsc_drv_obj *)input_dev->device->drv;
718 dev_info.vendor = input_dev->hid_dev_info.vendor;
719 dev_info.product = input_dev->hid_dev_info.product;
720 dev_info.version = input_dev->hid_dev_info.version;
721 strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
723 /* Send the device info back up */
724 deviceinfo_callback(device, &dev_info);
726 /* Send the report desc back up */
727 /* workaround SA-167 */
728 if (input_dev->report_desc[14] == 0x25)
729 input_dev->report_desc[14] = 0x29;
731 reportdesc_callback(device, input_dev->report_desc,
732 input_dev->report_desc_size);
734 input_dev->init_complete = true;
736 Cleanup:
737 return ret;
740 static int mousevsc_on_device_remove(struct hv_device *device)
742 struct mousevsc_dev *input_dev;
743 int ret = 0;
745 pr_info("disabling input device (%p)...",
746 device->ext);
748 input_dev = release_input_device(device);
752 * At this point, all outbound traffic should be disable. We only
753 * allow inbound traffic (responses) to proceed
755 * so that outstanding requests can be completed.
757 while (input_dev->num_outstanding_req) {
758 pr_info("waiting for %d requests to complete...",
759 input_dev->num_outstanding_req);
761 udelay(100);
764 pr_info("removing input device (%p)...", device->ext);
766 input_dev = final_release_input_device(device);
768 pr_info("input device (%p) safe to remove", input_dev);
770 /* Close the channel */
771 vmbus_close(device->channel);
773 free_input_device(input_dev);
775 return ret;
778 static void mousevsc_on_cleanup(struct hv_driver *drv)
783 * Data types
785 struct input_device_context {
786 struct hv_device *device_ctx;
787 struct hid_device *hid_device;
788 struct hv_input_dev_info device_info;
789 int connected;
793 static struct mousevsc_drv_obj g_mousevsc_drv;
795 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
797 struct input_device_context *input_device_ctx =
798 dev_get_drvdata(&dev->device);
800 memcpy(&input_device_ctx->device_info, info,
801 sizeof(struct hv_input_dev_info));
803 DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
806 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
808 int ret = 0;
810 struct input_device_context *input_dev_ctx =
811 dev_get_drvdata(&dev->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 hv_driver *drv =
833 drv_to_hv_drv(device->driver);
834 struct mousevsc_drv_obj *mousevsc_drv_obj = drv->priv;
836 struct hv_device *device_obj = device_to_hv_device(device);
837 struct input_device_context *input_dev_ctx;
839 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
840 GFP_KERNEL);
842 dev_set_drvdata(device, input_dev_ctx);
844 /* Call to the vsc driver to add the device */
845 ret = mousevsc_drv_obj->base.dev_add(device_obj, NULL);
847 if (ret != 0) {
848 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
850 return -1;
853 return 0;
856 static int mousevsc_remove(struct device *device)
858 int ret = 0;
860 struct hv_driver *drv =
861 drv_to_hv_drv(device->driver);
862 struct mousevsc_drv_obj *mousevsc_drv_obj = drv->priv;
864 struct hv_device *device_obj = device_to_hv_device(device);
865 struct input_device_context *input_dev_ctx;
867 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
868 GFP_KERNEL);
870 dev_set_drvdata(device, input_dev_ctx);
872 if (input_dev_ctx->connected) {
873 hidinput_disconnect(input_dev_ctx->hid_device);
874 input_dev_ctx->connected = 0;
877 if (!mousevsc_drv_obj->base.dev_rm)
878 return -1;
881 * Call to the vsc driver to let it know that the device
882 * is being removed
884 ret = mousevsc_drv_obj->base.dev_rm(device_obj);
886 if (ret != 0) {
887 DPRINT_ERR(INPUTVSC_DRV,
888 "unable to remove vsc device (ret %d)", ret);
891 kfree(input_dev_ctx);
893 return ret;
896 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
898 struct input_device_context *input_device_ctx =
899 dev_get_drvdata(&dev->device);
900 struct hid_device *hid_dev;
902 /* hid_debug = -1; */
903 hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
905 if (hid_parse_report(hid_dev, packet, len)) {
906 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
907 return;
910 if (hid_dev) {
911 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
913 hid_dev->ll_driver->open = mousevsc_hid_open;
914 hid_dev->ll_driver->close = mousevsc_hid_close;
916 hid_dev->bus = BUS_VIRTUAL;
917 hid_dev->vendor = input_device_ctx->device_info.vendor;
918 hid_dev->product = input_device_ctx->device_info.product;
919 hid_dev->version = input_device_ctx->device_info.version;
920 hid_dev->dev = dev->device;
922 sprintf(hid_dev->name, "%s",
923 input_device_ctx->device_info.name);
926 * HJ Do we want to call it with a 0
928 if (!hidinput_connect(hid_dev, 0)) {
929 hid_dev->claimed |= HID_CLAIMED_INPUT;
931 input_device_ctx->connected = 1;
933 DPRINT_INFO(INPUTVSC_DRV,
934 "HID device claimed by input\n");
937 if (!hid_dev->claimed) {
938 DPRINT_ERR(INPUTVSC_DRV,
939 "HID device not claimed by "
940 "input or hiddev\n");
943 input_device_ctx->hid_device = hid_dev;
946 kfree(hid_dev);
949 static int mousevsc_drv_exit_cb(struct device *dev, void *data)
951 struct device **curr = (struct device **)data;
952 *curr = dev;
954 return 1;
957 static void mousevsc_drv_exit(void)
959 struct mousevsc_drv_obj *mousevsc_drv_obj = &g_mousevsc_drv;
960 struct hv_driver *drv = &g_mousevsc_drv.base;
961 int ret;
963 struct device *current_dev = NULL;
965 while (1) {
966 current_dev = NULL;
968 /* Get the device */
969 ret = driver_for_each_device(&drv->driver, NULL,
970 (void *)&current_dev,
971 mousevsc_drv_exit_cb);
972 if (ret)
973 printk(KERN_ERR "Can't find mouse device!\n");
975 if (current_dev == NULL)
976 break;
978 /* Initiate removal from the top-down */
979 device_unregister(current_dev);
982 if (mousevsc_drv_obj->base.cleanup)
983 mousevsc_drv_obj->base.cleanup(&mousevsc_drv_obj->base);
985 vmbus_child_driver_unregister(&drv->driver);
987 return;
990 static int mouse_vsc_initialize(struct hv_driver *driver)
992 struct mousevsc_drv_obj *input_drv =
993 (struct mousevsc_drv_obj *)driver;
994 int ret = 0;
996 driver->name = driver_name;
997 memcpy(&driver->dev_type, &mouse_guid,
998 sizeof(struct hv_guid));
1000 /* Setup the dispatch table */
1001 input_drv->base.dev_add = mousevsc_on_device_add;
1002 input_drv->base.dev_rm = mousevsc_on_device_remove;
1003 input_drv->base.cleanup = mousevsc_on_cleanup;
1005 return ret;
1009 static int __init mousevsc_init(void)
1011 struct mousevsc_drv_obj *input_drv_obj = &g_mousevsc_drv;
1012 struct hv_driver *drv = &g_mousevsc_drv.base;
1014 DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
1016 /* Callback to client driver to complete the initialization */
1017 mouse_vsc_initialize(&input_drv_obj->base);
1019 drv->driver.name = input_drv_obj->base.name;
1020 drv->priv = input_drv_obj;
1022 drv->driver.probe = mousevsc_probe;
1023 drv->driver.remove = mousevsc_remove;
1025 /* The driver belongs to vmbus */
1026 vmbus_child_driver_register(&drv->driver);
1028 return 0;
1031 static void __exit mousevsc_exit(void)
1033 mousevsc_drv_exit();
1037 * We don't want to automatically load this driver just yet, it's quite
1038 * broken. It's safe if you want to load it yourself manually, but
1039 * don't inflict it on unsuspecting users, that's just mean.
1041 #if 0
1044 * We use a PCI table to determine if we should autoload this driver This is
1045 * needed by distro tools to determine if the hyperv drivers should be
1046 * installed and/or configured. We don't do anything else with the table, but
1047 * it needs to be present.
1049 const static struct pci_device_id microsoft_hv_pci_table[] = {
1050 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
1051 { 0 }
1053 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
1054 #endif
1056 MODULE_LICENSE("GPL");
1057 MODULE_VERSION(HV_DRV_VERSION);
1058 module_init(mousevsc_init);
1059 module_exit(mousevsc_exit);