Staging: hv: Include the newly created header file in all of the relevant hyperv...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / hv / hv_mouse.c
1 /*
2 * Copyright (c) 2009, Citrix Systems, Inc.
3 * Copyright (c) 2010, Microsoft Corporation.
4 * Copyright (c) 2011, Novell Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/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>
28
29 #include "hyperv.h"
30 #include "hv_api.h"
31 #include "logging.h"
32 #include "version_info.h"
33 #include "vmbus.h"
34 #include "vmbus_api.h"
35 #include "channel.h"
36 #include "vmbus_packet_format.h"
37
38
39 /*
40 * Data types
41 */
42 struct hv_input_dev_info {
43 unsigned short vendor;
44 unsigned short product;
45 unsigned short version;
46 char name[128];
47 };
48
49 /* The maximum size of a synthetic input message. */
50 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
51
52 /*
53 * Current version
54 *
55 * History:
56 * Beta, RC < 2008/1/22 1,0
57 * RC > 2008/1/22 2,0
58 */
59 #define SYNTHHID_INPUT_VERSION_MAJOR 2
60 #define SYNTHHID_INPUT_VERSION_MINOR 0
61 #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
62 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
63
64
65 #pragma pack(push,1)
66 /*
67 * Message types in the synthetic input protocol
68 */
69 enum synthhid_msg_type {
70 SynthHidProtocolRequest,
71 SynthHidProtocolResponse,
72 SynthHidInitialDeviceInfo,
73 SynthHidInitialDeviceInfoAck,
74 SynthHidInputReport,
75 SynthHidMax
76 };
77
78 /*
79 * Basic message structures.
80 */
81 struct synthhid_msg_hdr {
82 enum synthhid_msg_type type;
83 u32 size;
84 };
85
86 struct synthhid_msg {
87 struct synthhid_msg_hdr header;
88 char data[1]; /* Enclosed message */
89 };
90
91 union synthhid_version {
92 struct {
93 u16 minor_version;
94 u16 major_version;
95 };
96 u32 version;
97 };
98
99 /*
100 * Protocol messages
101 */
102 struct synthhid_protocol_request {
103 struct synthhid_msg_hdr header;
104 union synthhid_version version_requested;
105 };
106
107 struct synthhid_protocol_response {
108 struct synthhid_msg_hdr header;
109 union synthhid_version version_requested;
110 unsigned char approved;
111 };
112
113 struct synthhid_device_info {
114 struct synthhid_msg_hdr header;
115 struct hv_input_dev_info hid_dev_info;
116 struct hid_descriptor hid_descriptor;
117 };
118
119 struct synthhid_device_info_ack {
120 struct synthhid_msg_hdr header;
121 unsigned char reserved;
122 };
123
124 struct synthhid_input_report {
125 struct synthhid_msg_hdr header;
126 char buffer[1];
127 };
128
129 #pragma pack(pop)
130
131 #define INPUTVSC_SEND_RING_BUFFER_SIZE 10*PAGE_SIZE
132 #define INPUTVSC_RECV_RING_BUFFER_SIZE 10*PAGE_SIZE
133
134 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
135
136 enum pipe_prot_msg_type {
137 PipeMessageInvalid = 0,
138 PipeMessageData,
139 PipeMessageMaximum
140 };
141
142
143 struct pipe_prt_msg {
144 enum pipe_prot_msg_type type;
145 u32 size;
146 char data[1];
147 };
148
149 /*
150 * Data types
151 */
152 struct mousevsc_prt_msg {
153 enum pipe_prot_msg_type type;
154 u32 size;
155 union {
156 struct synthhid_protocol_request request;
157 struct synthhid_protocol_response response;
158 struct synthhid_device_info_ack ack;
159 };
160 };
161
162 /*
163 * Represents an mousevsc device
164 */
165 struct mousevsc_dev {
166 struct hv_device *device;
167 /* 0 indicates the device is being destroyed */
168 atomic_t ref_count;
169 int num_outstanding_req;
170 unsigned char init_complete;
171 struct mousevsc_prt_msg protocol_req;
172 struct mousevsc_prt_msg protocol_resp;
173 /* Synchronize the request/response if needed */
174 wait_queue_head_t protocol_wait_event;
175 wait_queue_head_t dev_info_wait_event;
176 int protocol_wait_condition;
177 int device_wait_condition;
178 int dev_info_status;
179
180 struct hid_descriptor *hid_desc;
181 unsigned char *report_desc;
182 u32 report_desc_size;
183 struct hv_input_dev_info hid_dev_info;
184 };
185
186
187 static const char *driver_name = "mousevsc";
188
189 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
190 static const struct hv_guid mouse_guid = {
191 .data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
192 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A}
193 };
194
195 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info);
196 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len);
197 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len);
198
199 static struct mousevsc_dev *alloc_input_device(struct hv_device *device)
200 {
201 struct mousevsc_dev *input_dev;
202
203 input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
204
205 if (!input_dev)
206 return NULL;
207
208 /*
209 * Set to 2 to allow both inbound and outbound traffics
210 * (ie get_input_device() and must_get_input_device()) to proceed.
211 */
212 atomic_cmpxchg(&input_dev->ref_count, 0, 2);
213
214 input_dev->device = device;
215 device->ext = input_dev;
216
217 return input_dev;
218 }
219
220 static void free_input_device(struct mousevsc_dev *device)
221 {
222 WARN_ON(atomic_read(&device->ref_count) == 0);
223 kfree(device);
224 }
225
226 /*
227 * Get the inputdevice object if exists and its refcount > 1
228 */
229 static struct mousevsc_dev *get_input_device(struct hv_device *device)
230 {
231 struct mousevsc_dev *input_dev;
232
233 input_dev = (struct mousevsc_dev *)device->ext;
234
235 /*
236 * FIXME
237 * This sure isn't a valid thing to print for debugging, no matter
238 * what the intention is...
239 *
240 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
241 * input_dev->ref_count);
242 */
243
244 if (input_dev && atomic_read(&input_dev->ref_count) > 1)
245 atomic_inc(&input_dev->ref_count);
246 else
247 input_dev = NULL;
248
249 return input_dev;
250 }
251
252 /*
253 * Get the inputdevice object iff exists and its refcount > 0
254 */
255 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
256 {
257 struct mousevsc_dev *input_dev;
258
259 input_dev = (struct mousevsc_dev *)device->ext;
260
261 if (input_dev && atomic_read(&input_dev->ref_count))
262 atomic_inc(&input_dev->ref_count);
263 else
264 input_dev = NULL;
265
266 return input_dev;
267 }
268
269 static void put_input_device(struct hv_device *device)
270 {
271 struct mousevsc_dev *input_dev;
272
273 input_dev = (struct mousevsc_dev *)device->ext;
274
275 atomic_dec(&input_dev->ref_count);
276 }
277
278 /*
279 * Drop ref count to 1 to effectively disable get_input_device()
280 */
281 static struct mousevsc_dev *release_input_device(struct hv_device *device)
282 {
283 struct mousevsc_dev *input_dev;
284
285 input_dev = (struct mousevsc_dev *)device->ext;
286
287 /* Busy wait until the ref drop to 2, then set it to 1 */
288 while (atomic_cmpxchg(&input_dev->ref_count, 2, 1) != 2)
289 udelay(100);
290
291 return input_dev;
292 }
293
294 /*
295 * Drop ref count to 0. No one can use input_device object.
296 */
297 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
298 {
299 struct mousevsc_dev *input_dev;
300
301 input_dev = (struct mousevsc_dev *)device->ext;
302
303 /* Busy wait until the ref drop to 1, then set it to 0 */
304 while (atomic_cmpxchg(&input_dev->ref_count, 1, 0) != 1)
305 udelay(100);
306
307 device->ext = NULL;
308 return input_dev;
309 }
310
311 static void mousevsc_on_send_completion(struct hv_device *device,
312 struct vmpacket_descriptor *packet)
313 {
314 struct mousevsc_dev *input_dev;
315 void *request;
316
317 input_dev = must_get_input_device(device);
318 if (!input_dev) {
319 pr_err("unable to get input device...device being destroyed?");
320 return;
321 }
322
323 request = (void *)(unsigned long)packet->trans_id;
324
325 if (request == &input_dev->protocol_req) {
326 /* FIXME */
327 /* Shouldn't we be doing something here? */
328 }
329
330 put_input_device(device);
331 }
332
333 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
334 struct synthhid_device_info *device_info)
335 {
336 int ret = 0;
337 struct hid_descriptor *desc;
338 struct mousevsc_prt_msg ack;
339
340 /* Assume success for now */
341 input_device->dev_info_status = 0;
342
343 /* Save the device attr */
344 memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
345 sizeof(struct hv_input_dev_info));
346
347 /* Save the hid desc */
348 desc = &device_info->hid_descriptor;
349 WARN_ON(desc->bLength > 0);
350
351 input_device->hid_desc = kzalloc(desc->bLength, GFP_KERNEL);
352
353 if (!input_device->hid_desc) {
354 pr_err("unable to allocate hid descriptor - size %d", desc->bLength);
355 goto Cleanup;
356 }
357
358 memcpy(input_device->hid_desc, desc, desc->bLength);
359
360 /* Save the report desc */
361 input_device->report_desc_size = desc->desc[0].wDescriptorLength;
362 input_device->report_desc = kzalloc(input_device->report_desc_size,
363 GFP_KERNEL);
364
365 if (!input_device->report_desc) {
366 pr_err("unable to allocate report descriptor - size %d",
367 input_device->report_desc_size);
368 goto Cleanup;
369 }
370
371 memcpy(input_device->report_desc,
372 ((unsigned char *)desc) + desc->bLength,
373 desc->desc[0].wDescriptorLength);
374
375 /* Send the ack */
376 memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
377
378 ack.type = PipeMessageData;
379 ack.size = sizeof(struct synthhid_device_info_ack);
380
381 ack.ack.header.type = SynthHidInitialDeviceInfoAck;
382 ack.ack.header.size = 1;
383 ack.ack.reserved = 0;
384
385 ret = vmbus_sendpacket(input_device->device->channel,
386 &ack,
387 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
388 sizeof(struct synthhid_device_info_ack),
389 (unsigned long)&ack,
390 VM_PKT_DATA_INBAND,
391 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
392 if (ret != 0) {
393 pr_err("unable to send synthhid device info ack - ret %d",
394 ret);
395 goto Cleanup;
396 }
397
398 input_device->device_wait_condition = 1;
399 wake_up(&input_device->dev_info_wait_event);
400
401 return;
402
403 Cleanup:
404 kfree(input_device->hid_desc);
405 input_device->hid_desc = NULL;
406
407 kfree(input_device->report_desc);
408 input_device->report_desc = NULL;
409
410 input_device->dev_info_status = -1;
411 input_device->device_wait_condition = 1;
412 wake_up(&input_device->dev_info_wait_event);
413 }
414
415 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
416 struct synthhid_input_report *input_report)
417 {
418 struct hv_driver *input_drv;
419
420 if (!input_device->init_complete) {
421 pr_info("Initialization incomplete...ignoring input_report msg");
422 return;
423 }
424
425 input_drv = drv_to_hv_drv(input_device->device->device.driver);
426
427 inputreport_callback(input_device->device,
428 input_report->buffer,
429 input_report->header.size);
430 }
431
432 static void mousevsc_on_receive(struct hv_device *device,
433 struct vmpacket_descriptor *packet)
434 {
435 struct pipe_prt_msg *pipe_msg;
436 struct synthhid_msg *hid_msg;
437 struct mousevsc_dev *input_dev;
438
439 input_dev = must_get_input_device(device);
440 if (!input_dev) {
441 pr_err("unable to get input device...device being destroyed?");
442 return;
443 }
444
445 pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
446 (packet->offset8 << 3));
447
448 if (pipe_msg->type != PipeMessageData) {
449 pr_err("unknown pipe msg type - type %d len %d",
450 pipe_msg->type, pipe_msg->size);
451 put_input_device(device);
452 return ;
453 }
454
455 hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
456
457 switch (hid_msg->header.type) {
458 case SynthHidProtocolResponse:
459 memcpy(&input_dev->protocol_resp, pipe_msg,
460 pipe_msg->size + sizeof(struct pipe_prt_msg) -
461 sizeof(unsigned char));
462 input_dev->protocol_wait_condition = 1;
463 wake_up(&input_dev->protocol_wait_event);
464 break;
465
466 case SynthHidInitialDeviceInfo:
467 WARN_ON(pipe_msg->size >= sizeof(struct hv_input_dev_info));
468
469 /*
470 * Parse out the device info into device attr,
471 * hid desc and report desc
472 */
473 mousevsc_on_receive_device_info(input_dev,
474 (struct synthhid_device_info *)&pipe_msg->data[0]);
475 break;
476 case SynthHidInputReport:
477 mousevsc_on_receive_input_report(input_dev,
478 (struct synthhid_input_report *)&pipe_msg->data[0]);
479
480 break;
481 default:
482 pr_err("unsupported hid msg type - type %d len %d",
483 hid_msg->header.type, hid_msg->header.size);
484 break;
485 }
486
487 put_input_device(device);
488 }
489
490 static void mousevsc_on_channel_callback(void *context)
491 {
492 const int packetSize = 0x100;
493 int ret = 0;
494 struct hv_device *device = (struct hv_device *)context;
495 struct mousevsc_dev *input_dev;
496
497 u32 bytes_recvd;
498 u64 req_id;
499 unsigned char packet[packetSize];
500 struct vmpacket_descriptor *desc;
501 unsigned char *buffer = packet;
502 int bufferlen = packetSize;
503
504 input_dev = must_get_input_device(device);
505
506 if (!input_dev) {
507 pr_err("unable to get input device...device being destroyed?");
508 return;
509 }
510
511 do {
512 ret = vmbus_recvpacket_raw(device->channel, buffer,
513 bufferlen, &bytes_recvd, &req_id);
514
515 if (ret == 0) {
516 if (bytes_recvd > 0) {
517 desc = (struct vmpacket_descriptor *)buffer;
518
519 switch (desc->type) {
520 case VM_PKT_COMP:
521 mousevsc_on_send_completion(
522 device, desc);
523 break;
524
525 case VM_PKT_DATA_INBAND:
526 mousevsc_on_receive(
527 device, desc);
528 break;
529
530 default:
531 pr_err("unhandled packet type %d, tid %llx len %d\n",
532 desc->type,
533 req_id,
534 bytes_recvd);
535 break;
536 }
537
538 /* reset */
539 if (bufferlen > packetSize) {
540 kfree(buffer);
541
542 buffer = packet;
543 bufferlen = packetSize;
544 }
545 } else {
546 /*
547 * pr_debug("nothing else to read...");
548 * reset
549 */
550 if (bufferlen > packetSize) {
551 kfree(buffer);
552
553 buffer = packet;
554 bufferlen = packetSize;
555 }
556 break;
557 }
558 } else if (ret == -2) {
559 /* Handle large packet */
560 bufferlen = bytes_recvd;
561 buffer = kzalloc(bytes_recvd, GFP_KERNEL);
562
563 if (buffer == NULL) {
564 buffer = packet;
565 bufferlen = packetSize;
566
567 /* Try again next time around */
568 pr_err("unable to allocate buffer of size %d!",
569 bytes_recvd);
570 break;
571 }
572 }
573 } while (1);
574
575 put_input_device(device);
576
577 return;
578 }
579
580 static int mousevsc_connect_to_vsp(struct hv_device *device)
581 {
582 int ret = 0;
583 struct mousevsc_dev *input_dev;
584 struct mousevsc_prt_msg *request;
585 struct mousevsc_prt_msg *response;
586
587 input_dev = get_input_device(device);
588
589 if (!input_dev) {
590 pr_err("unable to get input device...device being destroyed?");
591 return -1;
592 }
593
594 init_waitqueue_head(&input_dev->protocol_wait_event);
595 init_waitqueue_head(&input_dev->dev_info_wait_event);
596
597 request = &input_dev->protocol_req;
598
599 /*
600 * Now, initiate the vsc/vsp initialization protocol on the open channel
601 */
602 memset(request, 0, sizeof(struct mousevsc_prt_msg));
603
604 request->type = PipeMessageData;
605 request->size = sizeof(struct synthhid_protocol_request);
606
607 request->request.header.type = SynthHidProtocolRequest;
608 request->request.header.size = sizeof(unsigned long);
609 request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
610
611 pr_info("synthhid protocol request...");
612
613 ret = vmbus_sendpacket(device->channel, request,
614 sizeof(struct pipe_prt_msg) -
615 sizeof(unsigned char) +
616 sizeof(struct synthhid_protocol_request),
617 (unsigned long)request,
618 VM_PKT_DATA_INBAND,
619 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
620 if (ret != 0) {
621 pr_err("unable to send synthhid protocol request.");
622 goto Cleanup;
623 }
624
625 input_dev->protocol_wait_condition = 0;
626 wait_event_timeout(input_dev->protocol_wait_event,
627 input_dev->protocol_wait_condition, msecs_to_jiffies(1000));
628 if (input_dev->protocol_wait_condition == 0) {
629 ret = -ETIMEDOUT;
630 goto Cleanup;
631 }
632
633 response = &input_dev->protocol_resp;
634
635 if (!response->response.approved) {
636 pr_err("synthhid protocol request failed (version %d)",
637 SYNTHHID_INPUT_VERSION);
638 ret = -1;
639 goto Cleanup;
640 }
641
642 input_dev->device_wait_condition = 0;
643 wait_event_timeout(input_dev->dev_info_wait_event,
644 input_dev->device_wait_condition, msecs_to_jiffies(1000));
645 if (input_dev->device_wait_condition == 0) {
646 ret = -ETIMEDOUT;
647 goto Cleanup;
648 }
649
650 /*
651 * We should have gotten the device attr, hid desc and report
652 * desc at this point
653 */
654 if (!input_dev->dev_info_status)
655 pr_info("**** input channel up and running!! ****");
656 else
657 ret = -1;
658
659 Cleanup:
660 put_input_device(device);
661
662 return ret;
663 }
664
665 static int mousevsc_on_device_add(struct hv_device *device,
666 void *additional_info)
667 {
668 int ret = 0;
669 struct mousevsc_dev *input_dev;
670 struct hv_driver *input_drv;
671 struct hv_input_dev_info dev_info;
672
673 input_dev = alloc_input_device(device);
674
675 if (!input_dev) {
676 ret = -1;
677 goto Cleanup;
678 }
679
680 input_dev->init_complete = false;
681
682 /* Open the channel */
683 ret = vmbus_open(device->channel,
684 INPUTVSC_SEND_RING_BUFFER_SIZE,
685 INPUTVSC_RECV_RING_BUFFER_SIZE,
686 NULL,
687 0,
688 mousevsc_on_channel_callback,
689 device
690 );
691
692 if (ret != 0) {
693 pr_err("unable to open channel: %d", ret);
694 free_input_device(input_dev);
695 return -1;
696 }
697
698 pr_info("InputVsc channel open: %d", ret);
699
700 ret = mousevsc_connect_to_vsp(device);
701
702 if (ret != 0) {
703 pr_err("unable to connect channel: %d", ret);
704
705 vmbus_close(device->channel);
706 free_input_device(input_dev);
707 return ret;
708 }
709
710 input_drv = drv_to_hv_drv(input_dev->device->device.driver);
711
712 dev_info.vendor = input_dev->hid_dev_info.vendor;
713 dev_info.product = input_dev->hid_dev_info.product;
714 dev_info.version = input_dev->hid_dev_info.version;
715 strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse");
716
717 /* Send the device info back up */
718 deviceinfo_callback(device, &dev_info);
719
720 /* Send the report desc back up */
721 /* workaround SA-167 */
722 if (input_dev->report_desc[14] == 0x25)
723 input_dev->report_desc[14] = 0x29;
724
725 reportdesc_callback(device, input_dev->report_desc,
726 input_dev->report_desc_size);
727
728 input_dev->init_complete = true;
729
730 Cleanup:
731 return ret;
732 }
733
734 static int mousevsc_on_device_remove(struct hv_device *device)
735 {
736 struct mousevsc_dev *input_dev;
737 int ret = 0;
738
739 pr_info("disabling input device (%p)...",
740 device->ext);
741
742 input_dev = release_input_device(device);
743
744
745 /*
746 * At this point, all outbound traffic should be disable. We only
747 * allow inbound traffic (responses) to proceed
748 *
749 * so that outstanding requests can be completed.
750 */
751 while (input_dev->num_outstanding_req) {
752 pr_info("waiting for %d requests to complete...",
753 input_dev->num_outstanding_req);
754
755 udelay(100);
756 }
757
758 pr_info("removing input device (%p)...", device->ext);
759
760 input_dev = final_release_input_device(device);
761
762 pr_info("input device (%p) safe to remove", input_dev);
763
764 /* Close the channel */
765 vmbus_close(device->channel);
766
767 free_input_device(input_dev);
768
769 return ret;
770 }
771
772
773 /*
774 * Data types
775 */
776 struct input_device_context {
777 struct hv_device *device_ctx;
778 struct hid_device *hid_device;
779 struct hv_input_dev_info device_info;
780 int connected;
781 };
782
783
784 static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info)
785 {
786 struct input_device_context *input_device_ctx =
787 dev_get_drvdata(&dev->device);
788
789 memcpy(&input_device_ctx->device_info, info,
790 sizeof(struct hv_input_dev_info));
791
792 DPRINT_INFO(INPUTVSC_DRV, "%s", __func__);
793 }
794
795 static void inputreport_callback(struct hv_device *dev, void *packet, u32 len)
796 {
797 int ret = 0;
798
799 struct input_device_context *input_dev_ctx =
800 dev_get_drvdata(&dev->device);
801
802 ret = hid_input_report(input_dev_ctx->hid_device,
803 HID_INPUT_REPORT, packet, len, 1);
804
805 DPRINT_DBG(INPUTVSC_DRV, "hid_input_report (ret %d)", ret);
806 }
807
808 static int mousevsc_hid_open(struct hid_device *hid)
809 {
810 return 0;
811 }
812
813 static void mousevsc_hid_close(struct hid_device *hid)
814 {
815 }
816
817 static int mousevsc_probe(struct hv_device *dev)
818 {
819 int ret = 0;
820
821 struct input_device_context *input_dev_ctx;
822
823 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
824 GFP_KERNEL);
825
826 dev_set_drvdata(&dev->device, input_dev_ctx);
827
828 /* Call to the vsc driver to add the device */
829 ret = mousevsc_on_device_add(dev, NULL);
830
831 if (ret != 0) {
832 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
833
834 return -1;
835 }
836
837 return 0;
838 }
839
840 static int mousevsc_remove(struct hv_device *dev)
841 {
842 int ret = 0;
843
844 struct input_device_context *input_dev_ctx;
845
846 input_dev_ctx = kmalloc(sizeof(struct input_device_context),
847 GFP_KERNEL);
848
849 dev_set_drvdata(&dev->device, input_dev_ctx);
850
851 if (input_dev_ctx->connected) {
852 hidinput_disconnect(input_dev_ctx->hid_device);
853 input_dev_ctx->connected = 0;
854 }
855
856 /*
857 * Call to the vsc driver to let it know that the device
858 * is being removed
859 */
860 ret = mousevsc_on_device_remove(dev);
861
862 if (ret != 0) {
863 DPRINT_ERR(INPUTVSC_DRV,
864 "unable to remove vsc device (ret %d)", ret);
865 }
866
867 kfree(input_dev_ctx);
868
869 return ret;
870 }
871
872 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
873 {
874 struct input_device_context *input_device_ctx =
875 dev_get_drvdata(&dev->device);
876 struct hid_device *hid_dev;
877
878 /* hid_debug = -1; */
879 hid_dev = kmalloc(sizeof(struct hid_device), GFP_KERNEL);
880
881 if (hid_parse_report(hid_dev, packet, len)) {
882 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
883 return;
884 }
885
886 if (hid_dev) {
887 DPRINT_INFO(INPUTVSC_DRV, "hid_device created");
888
889 hid_dev->ll_driver->open = mousevsc_hid_open;
890 hid_dev->ll_driver->close = mousevsc_hid_close;
891
892 hid_dev->bus = BUS_VIRTUAL;
893 hid_dev->vendor = input_device_ctx->device_info.vendor;
894 hid_dev->product = input_device_ctx->device_info.product;
895 hid_dev->version = input_device_ctx->device_info.version;
896 hid_dev->dev = dev->device;
897
898 sprintf(hid_dev->name, "%s",
899 input_device_ctx->device_info.name);
900
901 /*
902 * HJ Do we want to call it with a 0
903 */
904 if (!hidinput_connect(hid_dev, 0)) {
905 hid_dev->claimed |= HID_CLAIMED_INPUT;
906
907 input_device_ctx->connected = 1;
908
909 DPRINT_INFO(INPUTVSC_DRV,
910 "HID device claimed by input\n");
911 }
912
913 if (!hid_dev->claimed) {
914 DPRINT_ERR(INPUTVSC_DRV,
915 "HID device not claimed by "
916 "input or hiddev\n");
917 }
918
919 input_device_ctx->hid_device = hid_dev;
920 }
921
922 kfree(hid_dev);
923 }
924
925 static int mousevsc_drv_exit_cb(struct device *dev, void *data)
926 {
927 struct device **curr = (struct device **)data;
928 *curr = dev;
929
930 return 1;
931 }
932
933 static struct hv_driver mousevsc_drv = {
934 .probe = mousevsc_probe,
935 .remove = mousevsc_remove,
936 };
937
938 static void mousevsc_drv_exit(void)
939 {
940 struct hv_driver *drv = &mousevsc_drv;
941 int ret;
942
943 struct device *current_dev = NULL;
944
945 while (1) {
946 current_dev = NULL;
947
948 /* Get the device */
949 ret = driver_for_each_device(&drv->driver, NULL,
950 (void *)&current_dev,
951 mousevsc_drv_exit_cb);
952 if (ret)
953 printk(KERN_ERR "Can't find mouse device!\n");
954
955 if (current_dev == NULL)
956 break;
957
958 /* Initiate removal from the top-down */
959 device_unregister(current_dev);
960 }
961
962 vmbus_child_driver_unregister(&drv->driver);
963
964 return;
965 }
966
967 static int __init mousevsc_init(void)
968 {
969 struct hv_driver *drv = &mousevsc_drv;
970
971 DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing.");
972
973 memcpy(&drv->dev_type, &mouse_guid,
974 sizeof(struct hv_guid));
975
976 drv->driver.name = driver_name;
977 drv->name = driver_name;
978
979 /* The driver belongs to vmbus */
980 vmbus_child_driver_register(&drv->driver);
981
982 return 0;
983 }
984
985 static void __exit mousevsc_exit(void)
986 {
987 mousevsc_drv_exit();
988 }
989
990 /*
991 * We don't want to automatically load this driver just yet, it's quite
992 * broken. It's safe if you want to load it yourself manually, but
993 * don't inflict it on unsuspecting users, that's just mean.
994 */
995 #if 0
996
997 /*
998 * We use a PCI table to determine if we should autoload this driver This is
999 * needed by distro tools to determine if the hyperv drivers should be
1000 * installed and/or configured. We don't do anything else with the table, but
1001 * it needs to be present.
1002 */
1003 const static struct pci_device_id microsoft_hv_pci_table[] = {
1004 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
1005 { 0 }
1006 };
1007 MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table);
1008 #endif
1009
1010 MODULE_LICENSE("GPL");
1011 MODULE_VERSION(HV_DRV_VERSION);
1012 module_init(mousevsc_init);
1013 module_exit(mousevsc_exit);
1014