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