Staging: hv: mousevsc: Cleanup and properly implement reportdesc_callback()
[zen-stable.git] / drivers / staging / hv / hv_mouse.c
blob8d94aef9010290bc42aacaaa380b27a431fb34ed
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>
26 #include "hyperv.h"
30 * Data types
32 struct hv_input_dev_info {
33 unsigned int size;
34 unsigned short vendor;
35 unsigned short product;
36 unsigned short version;
37 unsigned short reserved[11];
40 /* The maximum size of a synthetic input message. */
41 #define SYNTHHID_MAX_INPUT_REPORT_SIZE 16
44 * Current version
46 * History:
47 * Beta, RC < 2008/1/22 1,0
48 * RC > 2008/1/22 2,0
50 #define SYNTHHID_INPUT_VERSION_MAJOR 2
51 #define SYNTHHID_INPUT_VERSION_MINOR 0
52 #define SYNTHHID_INPUT_VERSION (SYNTHHID_INPUT_VERSION_MINOR | \
53 (SYNTHHID_INPUT_VERSION_MAJOR << 16))
56 #pragma pack(push, 1)
58 * Message types in the synthetic input protocol
60 enum synthhid_msg_type {
61 SynthHidProtocolRequest,
62 SynthHidProtocolResponse,
63 SynthHidInitialDeviceInfo,
64 SynthHidInitialDeviceInfoAck,
65 SynthHidInputReport,
66 SynthHidMax
70 * Basic message structures.
72 struct synthhid_msg_hdr {
73 enum synthhid_msg_type type;
74 u32 size;
77 struct synthhid_msg {
78 struct synthhid_msg_hdr header;
79 char data[1]; /* Enclosed message */
82 union synthhid_version {
83 struct {
84 u16 minor_version;
85 u16 major_version;
87 u32 version;
91 * Protocol messages
93 struct synthhid_protocol_request {
94 struct synthhid_msg_hdr header;
95 union synthhid_version version_requested;
98 struct synthhid_protocol_response {
99 struct synthhid_msg_hdr header;
100 union synthhid_version version_requested;
101 unsigned char approved;
104 struct synthhid_device_info {
105 struct synthhid_msg_hdr header;
106 struct hv_input_dev_info hid_dev_info;
107 struct hid_descriptor hid_descriptor;
110 struct synthhid_device_info_ack {
111 struct synthhid_msg_hdr header;
112 unsigned char reserved;
115 struct synthhid_input_report {
116 struct synthhid_msg_hdr header;
117 char buffer[1];
120 #pragma pack(pop)
122 #define INPUTVSC_SEND_RING_BUFFER_SIZE (10*PAGE_SIZE)
123 #define INPUTVSC_RECV_RING_BUFFER_SIZE (10*PAGE_SIZE)
125 #define NBITS(x) (((x)/BITS_PER_LONG)+1)
127 enum pipe_prot_msg_type {
128 PipeMessageInvalid = 0,
129 PipeMessageData,
130 PipeMessageMaximum
134 struct pipe_prt_msg {
135 enum pipe_prot_msg_type type;
136 u32 size;
137 char data[1];
141 * Data types
143 struct mousevsc_prt_msg {
144 enum pipe_prot_msg_type type;
145 u32 size;
146 union {
147 struct synthhid_protocol_request request;
148 struct synthhid_protocol_response response;
149 struct synthhid_device_info_ack ack;
154 * Represents an mousevsc device
156 struct mousevsc_dev {
157 struct hv_device *device;
158 /* 0 indicates the device is being destroyed */
159 atomic_t ref_count;
160 int num_outstanding_req;
161 unsigned char init_complete;
162 struct mousevsc_prt_msg protocol_req;
163 struct mousevsc_prt_msg protocol_resp;
164 /* Synchronize the request/response if needed */
165 struct completion wait_event;
166 int dev_info_status;
168 struct hid_descriptor *hid_desc;
169 unsigned char *report_desc;
170 u32 report_desc_size;
171 struct hv_input_dev_info hid_dev_info;
172 int connected;
173 struct hid_device *hid_device;
177 static struct mousevsc_dev *alloc_input_device(struct hv_device *device)
179 struct mousevsc_dev *input_dev;
181 input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL);
183 if (!input_dev)
184 return NULL;
187 * Set to 2 to allow both inbound and outbound traffics
188 * (ie get_input_device() and must_get_input_device()) to proceed.
190 atomic_cmpxchg(&input_dev->ref_count, 0, 2);
192 input_dev->device = device;
193 hv_set_drvdata(device, input_dev);
194 init_completion(&input_dev->wait_event);
196 return input_dev;
199 static void free_input_device(struct mousevsc_dev *device)
201 WARN_ON(atomic_read(&device->ref_count) != 0);
202 kfree(device);
206 * Get the inputdevice object if exists and its refcount > 1
208 static struct mousevsc_dev *get_input_device(struct hv_device *device)
210 struct mousevsc_dev *input_dev;
212 input_dev = hv_get_drvdata(device);
215 * FIXME
216 * This sure isn't a valid thing to print for debugging, no matter
217 * what the intention is...
219 * printk(KERN_ERR "-------------------------> REFCOUNT = %d",
220 * input_dev->ref_count);
223 if (input_dev && atomic_read(&input_dev->ref_count) > 1)
224 atomic_inc(&input_dev->ref_count);
225 else
226 input_dev = NULL;
228 return input_dev;
232 * Get the inputdevice object iff exists and its refcount > 0
234 static struct mousevsc_dev *must_get_input_device(struct hv_device *device)
236 struct mousevsc_dev *input_dev;
238 input_dev = hv_get_drvdata(device);
240 if (input_dev && atomic_read(&input_dev->ref_count))
241 atomic_inc(&input_dev->ref_count);
242 else
243 input_dev = NULL;
245 return input_dev;
248 static void put_input_device(struct hv_device *device)
250 struct mousevsc_dev *input_dev;
252 input_dev = hv_get_drvdata(device);
254 atomic_dec(&input_dev->ref_count);
258 * Drop ref count to 1 to effectively disable get_input_device()
260 static struct mousevsc_dev *release_input_device(struct hv_device *device)
262 struct mousevsc_dev *input_dev;
264 input_dev = hv_get_drvdata(device);
266 /* Busy wait until the ref drop to 2, then set it to 1 */
267 while (atomic_cmpxchg(&input_dev->ref_count, 2, 1) != 2)
268 udelay(100);
270 return input_dev;
274 * Drop ref count to 0. No one can use input_device object.
276 static struct mousevsc_dev *final_release_input_device(struct hv_device *device)
278 struct mousevsc_dev *input_dev;
280 input_dev = hv_get_drvdata(device);
282 /* Busy wait until the ref drop to 1, then set it to 0 */
283 while (atomic_cmpxchg(&input_dev->ref_count, 1, 0) != 1)
284 udelay(100);
286 hv_set_drvdata(device, NULL);
287 return input_dev;
290 static void mousevsc_on_send_completion(struct hv_device *device,
291 struct vmpacket_descriptor *packet)
293 struct mousevsc_dev *input_dev;
294 void *request;
296 input_dev = must_get_input_device(device);
297 if (!input_dev) {
298 pr_err("unable to get input device...device being destroyed?");
299 return;
302 request = (void *)(unsigned long)packet->trans_id;
304 if (request == &input_dev->protocol_req) {
305 /* FIXME */
306 /* Shouldn't we be doing something here? */
309 put_input_device(device);
312 static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
313 struct synthhid_device_info *device_info)
315 int ret = 0;
316 struct hid_descriptor *desc;
317 struct mousevsc_prt_msg ack;
319 /* Assume success for now */
320 input_device->dev_info_status = 0;
322 /* Save the device attr */
323 memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info,
324 sizeof(struct hv_input_dev_info));
326 /* Save the hid desc */
327 desc = &device_info->hid_descriptor;
328 WARN_ON(desc->bLength == 0);
330 input_device->hid_desc = kzalloc(desc->bLength, GFP_ATOMIC);
332 if (!input_device->hid_desc) {
333 pr_err("unable to allocate hid descriptor - size %d",
334 desc->bLength);
335 goto cleanup;
338 memcpy(input_device->hid_desc, desc, desc->bLength);
340 /* Save the report desc */
341 input_device->report_desc_size = desc->desc[0].wDescriptorLength;
342 if (input_device->report_desc_size == 0)
343 goto cleanup;
344 input_device->report_desc = kzalloc(input_device->report_desc_size,
345 GFP_ATOMIC);
347 if (!input_device->report_desc) {
348 pr_err("unable to allocate report descriptor - size %d",
349 input_device->report_desc_size);
350 goto cleanup;
353 memcpy(input_device->report_desc,
354 ((unsigned char *)desc) + desc->bLength,
355 desc->desc[0].wDescriptorLength);
357 /* Send the ack */
358 memset(&ack, 0, sizeof(struct mousevsc_prt_msg));
360 ack.type = PipeMessageData;
361 ack.size = sizeof(struct synthhid_device_info_ack);
363 ack.ack.header.type = SynthHidInitialDeviceInfoAck;
364 ack.ack.header.size = 1;
365 ack.ack.reserved = 0;
367 ret = vmbus_sendpacket(input_device->device->channel,
368 &ack,
369 sizeof(struct pipe_prt_msg) - sizeof(unsigned char) +
370 sizeof(struct synthhid_device_info_ack),
371 (unsigned long)&ack,
372 VM_PKT_DATA_INBAND,
373 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
374 if (ret != 0) {
375 pr_err("unable to send synthhid device info ack - ret %d",
376 ret);
377 goto cleanup;
380 complete(&input_device->wait_event);
382 return;
384 cleanup:
385 kfree(input_device->hid_desc);
386 input_device->hid_desc = NULL;
388 kfree(input_device->report_desc);
389 input_device->report_desc = NULL;
391 input_device->dev_info_status = -1;
392 complete(&input_device->wait_event);
395 static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device,
396 struct synthhid_input_report *input_report)
398 struct hv_driver *input_drv;
400 if (!input_device->init_complete) {
401 pr_info("Initialization incomplete...ignoring input_report msg");
402 return;
405 input_drv = drv_to_hv_drv(input_device->device->device.driver);
408 hid_input_report(input_device->hid_device,
409 HID_INPUT_REPORT, input_report->buffer, input_report->header.size, 1);
413 static void mousevsc_on_receive(struct hv_device *device,
414 struct vmpacket_descriptor *packet)
416 struct pipe_prt_msg *pipe_msg;
417 struct synthhid_msg *hid_msg;
418 struct mousevsc_dev *input_dev;
420 input_dev = must_get_input_device(device);
421 if (!input_dev) {
422 pr_err("unable to get input device...device being destroyed?");
423 return;
426 pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet +
427 (packet->offset8 << 3));
429 if (pipe_msg->type != PipeMessageData) {
430 pr_err("unknown pipe msg type - type %d len %d",
431 pipe_msg->type, pipe_msg->size);
432 put_input_device(device);
433 return ;
436 hid_msg = (struct synthhid_msg *)&pipe_msg->data[0];
438 switch (hid_msg->header.type) {
439 case SynthHidProtocolResponse:
440 memcpy(&input_dev->protocol_resp, pipe_msg,
441 pipe_msg->size + sizeof(struct pipe_prt_msg) -
442 sizeof(unsigned char));
443 complete(&input_dev->wait_event);
444 break;
446 case SynthHidInitialDeviceInfo:
447 WARN_ON(pipe_msg->size < sizeof(struct hv_input_dev_info));
450 * Parse out the device info into device attr,
451 * hid desc and report desc
453 mousevsc_on_receive_device_info(input_dev,
454 (struct synthhid_device_info *)&pipe_msg->data[0]);
455 break;
456 case SynthHidInputReport:
457 mousevsc_on_receive_input_report(input_dev,
458 (struct synthhid_input_report *)&pipe_msg->data[0]);
460 break;
461 default:
462 pr_err("unsupported hid msg type - type %d len %d",
463 hid_msg->header.type, hid_msg->header.size);
464 break;
467 put_input_device(device);
470 static void mousevsc_on_channel_callback(void *context)
472 const int packetSize = 0x100;
473 int ret = 0;
474 struct hv_device *device = (struct hv_device *)context;
475 struct mousevsc_dev *input_dev;
477 u32 bytes_recvd;
478 u64 req_id;
479 unsigned char packet[0x100];
480 struct vmpacket_descriptor *desc;
481 unsigned char *buffer = packet;
482 int bufferlen = packetSize;
484 input_dev = must_get_input_device(device);
486 if (!input_dev) {
487 pr_err("unable to get input device...device being destroyed?");
488 return;
491 do {
492 ret = vmbus_recvpacket_raw(device->channel, buffer,
493 bufferlen, &bytes_recvd, &req_id);
495 if (ret == 0) {
496 if (bytes_recvd > 0) {
497 desc = (struct vmpacket_descriptor *)buffer;
499 switch (desc->type) {
500 case VM_PKT_COMP:
501 mousevsc_on_send_completion(
502 device, desc);
503 break;
505 case VM_PKT_DATA_INBAND:
506 mousevsc_on_receive(
507 device, desc);
508 break;
510 default:
511 pr_err("unhandled packet type %d, tid %llx len %d\n",
512 desc->type,
513 req_id,
514 bytes_recvd);
515 break;
518 /* reset */
519 if (bufferlen > packetSize) {
520 kfree(buffer);
522 buffer = packet;
523 bufferlen = packetSize;
525 } else {
527 * pr_debug("nothing else to read...");
528 * reset
530 if (bufferlen > packetSize) {
531 kfree(buffer);
533 buffer = packet;
534 bufferlen = packetSize;
536 break;
538 } else if (ret == -ENOBUFS) {
539 /* Handle large packet */
540 bufferlen = bytes_recvd;
541 buffer = kzalloc(bytes_recvd, GFP_ATOMIC);
543 if (buffer == NULL) {
544 buffer = packet;
545 bufferlen = packetSize;
547 /* Try again next time around */
548 pr_err("unable to allocate buffer of size %d!",
549 bytes_recvd);
550 break;
553 } while (1);
555 put_input_device(device);
557 return;
560 static int mousevsc_connect_to_vsp(struct hv_device *device)
562 int ret = 0;
563 int t;
564 struct mousevsc_dev *input_dev;
565 struct mousevsc_prt_msg *request;
566 struct mousevsc_prt_msg *response;
568 input_dev = get_input_device(device);
570 if (!input_dev) {
571 pr_err("unable to get input device...device being destroyed?");
572 return -1;
576 request = &input_dev->protocol_req;
579 * Now, initiate the vsc/vsp initialization protocol on the open channel
581 memset(request, 0, sizeof(struct mousevsc_prt_msg));
583 request->type = PipeMessageData;
584 request->size = sizeof(struct synthhid_protocol_request);
586 request->request.header.type = SynthHidProtocolRequest;
587 request->request.header.size = sizeof(unsigned int);
588 request->request.version_requested.version = SYNTHHID_INPUT_VERSION;
590 pr_info("synthhid protocol request...");
592 ret = vmbus_sendpacket(device->channel, request,
593 sizeof(struct pipe_prt_msg) -
594 sizeof(unsigned char) +
595 sizeof(struct synthhid_protocol_request),
596 (unsigned long)request,
597 VM_PKT_DATA_INBAND,
598 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
599 if (ret != 0) {
600 pr_err("unable to send synthhid protocol request.");
601 goto cleanup;
604 t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
605 if (t == 0) {
606 ret = -ETIMEDOUT;
607 goto cleanup;
610 response = &input_dev->protocol_resp;
612 if (!response->response.approved) {
613 pr_err("synthhid protocol request failed (version %d)",
614 SYNTHHID_INPUT_VERSION);
615 ret = -1;
616 goto cleanup;
619 t = wait_for_completion_timeout(&input_dev->wait_event, 5*HZ);
620 if (t == 0) {
621 ret = -ETIMEDOUT;
622 goto cleanup;
626 * We should have gotten the device attr, hid desc and report
627 * desc at this point
629 if (!input_dev->dev_info_status)
630 pr_info("**** input channel up and running!! ****");
631 else
632 ret = -1;
634 cleanup:
635 put_input_device(device);
637 return ret;
640 static int mousevsc_hid_open(struct hid_device *hid)
642 return 0;
645 static void mousevsc_hid_close(struct hid_device *hid)
649 static struct hid_ll_driver mousevsc_ll_driver = {
650 .open = mousevsc_hid_open,
651 .close = mousevsc_hid_close,
654 static struct hid_driver mousevsc_hid_driver;
656 static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len)
658 struct hid_device *hid_dev;
659 struct mousevsc_dev *input_device = hv_get_drvdata(dev);
661 hid_dev = hid_allocate_device();
662 if (IS_ERR(hid_dev))
663 return;
665 hid_dev->ll_driver = &mousevsc_ll_driver;
666 hid_dev->driver = &mousevsc_hid_driver;
668 if (hid_parse_report(hid_dev, packet, len)) {
669 DPRINT_INFO(INPUTVSC_DRV, "Unable to call hd_parse_report");
670 return;
673 hid_dev->bus = BUS_VIRTUAL;
674 hid_dev->vendor = input_device->hid_dev_info.vendor;
675 hid_dev->product = input_device->hid_dev_info.product;
676 hid_dev->version = input_device->hid_dev_info.version;
678 sprintf(hid_dev->name, "%s", "Microsoft Vmbus HID-compliant Mouse");
680 if (!hidinput_connect(hid_dev, 0)) {
681 hid_dev->claimed |= HID_CLAIMED_INPUT;
683 input_device->connected = 1;
687 input_device->hid_device = hid_dev;
690 static int mousevsc_on_device_add(struct hv_device *device,
691 void *additional_info)
693 int ret = 0;
694 struct mousevsc_dev *input_dev;
695 struct hv_driver *input_drv;
697 input_dev = alloc_input_device(device);
699 if (!input_dev) {
700 ret = -1;
701 goto cleanup;
704 input_dev->init_complete = false;
706 /* Open the channel */
707 ret = vmbus_open(device->channel,
708 INPUTVSC_SEND_RING_BUFFER_SIZE,
709 INPUTVSC_RECV_RING_BUFFER_SIZE,
710 NULL,
712 mousevsc_on_channel_callback,
713 device
716 if (ret != 0) {
717 pr_err("unable to open channel: %d", ret);
718 free_input_device(input_dev);
719 return -1;
722 pr_info("InputVsc channel open: %d", ret);
724 ret = mousevsc_connect_to_vsp(device);
726 if (ret != 0) {
727 pr_err("unable to connect channel: %d", ret);
729 vmbus_close(device->channel);
730 free_input_device(input_dev);
731 return ret;
734 input_drv = drv_to_hv_drv(input_dev->device->device.driver);
738 /* Send the report desc back up */
739 /* workaround SA-167 */
740 if (input_dev->report_desc[14] == 0x25)
741 input_dev->report_desc[14] = 0x29;
743 reportdesc_callback(device, input_dev->report_desc,
744 input_dev->report_desc_size);
746 input_dev->init_complete = true;
748 cleanup:
749 return ret;
752 static int mousevsc_on_device_remove(struct hv_device *device)
754 struct mousevsc_dev *input_dev;
755 int ret = 0;
757 pr_info("disabling input device (%p)...",
758 hv_get_drvdata(device));
760 input_dev = release_input_device(device);
764 * At this point, all outbound traffic should be disable. We only
765 * allow inbound traffic (responses) to proceed
767 * so that outstanding requests can be completed.
769 while (input_dev->num_outstanding_req) {
770 pr_info("waiting for %d requests to complete...",
771 input_dev->num_outstanding_req);
773 udelay(100);
776 pr_info("removing input device (%p)...", hv_get_drvdata(device));
778 input_dev = final_release_input_device(device);
780 pr_info("input device (%p) safe to remove", input_dev);
782 /* Close the channel */
783 vmbus_close(device->channel);
785 free_input_device(input_dev);
787 return ret;
791 static int mousevsc_probe(struct hv_device *dev,
792 const struct hv_vmbus_device_id *dev_id)
794 int ret = 0;
797 /* Call to the vsc driver to add the device */
798 ret = mousevsc_on_device_add(dev, NULL);
800 if (ret != 0) {
801 DPRINT_ERR(INPUTVSC_DRV, "unable to add input vsc device");
803 return -1;
806 return 0;
809 static int mousevsc_remove(struct hv_device *dev)
811 struct mousevsc_dev *input_dev = hv_get_drvdata(dev);
812 int ret;
814 if (input_dev->connected) {
815 hidinput_disconnect(input_dev->hid_device);
816 input_dev->connected = 0;
817 hid_destroy_device(input_dev->hid_device);
821 * Call to the vsc driver to let it know that the device
822 * is being removed
824 ret = mousevsc_on_device_remove(dev);
825 if (ret != 0) {
826 DPRINT_ERR(INPUTVSC_DRV,
827 "unable to remove vsc device (ret %d)", ret);
830 return ret;
833 static const struct hv_vmbus_device_id id_table[] = {
834 /* Mouse guid */
835 { VMBUS_DEVICE(0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
836 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A) },
837 { },
841 * The mouse driver is not functional; do not auto-load it.
843 /* MODULE_DEVICE_TABLE(vmbus, id_table); */
845 static struct hv_driver mousevsc_drv = {
846 .name = "mousevsc",
847 .id_table = id_table,
848 .probe = mousevsc_probe,
849 .remove = mousevsc_remove,
852 static int __init mousevsc_init(void)
854 return vmbus_driver_register(&mousevsc_drv);
857 static void __exit mousevsc_exit(void)
859 vmbus_driver_unregister(&mousevsc_drv);
862 MODULE_LICENSE("GPL");
863 MODULE_VERSION(HV_DRV_VERSION);
864 module_init(mousevsc_init);
865 module_exit(mousevsc_exit);