Commit | Line | Data |
---|---|---|
0c3a6ede | 1 | /* |
9b9f93da GKH |
2 | * Copyright (c) 2009, Citrix Systems, Inc. |
3 | * Copyright (c) 2010, Microsoft Corporation. | |
4 | * Copyright (c) 2011, Novell Inc. | |
0c3a6ede | 5 | * |
9b9f93da GKH |
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. | |
0c3a6ede | 9 | * |
9b9f93da GKH |
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. | |
0c3a6ede GKH |
14 | */ |
15 | #include <linux/init.h> | |
16 | #include <linux/module.h> | |
a58c616a | 17 | #include <linux/delay.h> |
0c3a6ede GKH |
18 | #include <linux/device.h> |
19 | #include <linux/workqueue.h> | |
9dccaa63 GKH |
20 | #include <linux/sched.h> |
21 | #include <linux/wait.h> | |
0c3a6ede GKH |
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> | |
7e3bf1d3 | 27 | #include <linux/delay.h> |
0c3a6ede | 28 | |
3f335ea2 | 29 | #include "hyperv.h" |
0c3a6ede GKH |
30 | #include "hv_api.h" |
31 | #include "logging.h" | |
32 | #include "version_info.h" | |
33 | #include "vmbus.h" | |
9dccaa63 | 34 | #include "vmbus_api.h" |
9dccaa63 GKH |
35 | #include "channel.h" |
36 | #include "vmbus_packet_format.h" | |
fa003500 GKH |
37 | |
38 | ||
94fcc888 GKH |
39 | /* |
40 | * Data types | |
41 | */ | |
0f88ea5b GKH |
42 | struct hv_input_dev_info { |
43 | unsigned short vendor; | |
44 | unsigned short product; | |
45 | unsigned short version; | |
46 | char name[128]; | |
94fcc888 GKH |
47 | }; |
48 | ||
fa003500 GKH |
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 | */ | |
480c28df GKH |
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)) | |
fa003500 GKH |
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 | */ | |
e6f83b78 | 81 | struct synthhid_msg_hdr { |
32ad38f7 GKH |
82 | enum synthhid_msg_type type; |
83 | u32 size; | |
e6f83b78 | 84 | }; |
fa003500 | 85 | |
e6f83b78 | 86 | struct synthhid_msg { |
0ce815d5 | 87 | struct synthhid_msg_hdr header; |
cb2535ad | 88 | char data[1]; /* Enclosed message */ |
e6f83b78 | 89 | }; |
fa003500 | 90 | |
e6f83b78 | 91 | union synthhid_version { |
fa003500 | 92 | struct { |
480c28df GKH |
93 | u16 minor_version; |
94 | u16 major_version; | |
fa003500 | 95 | }; |
480c28df | 96 | u32 version; |
e6f83b78 | 97 | }; |
fa003500 GKH |
98 | |
99 | /* | |
100 | * Protocol messages | |
101 | */ | |
e6f83b78 | 102 | struct synthhid_protocol_request { |
0ce815d5 | 103 | struct synthhid_msg_hdr header; |
480c28df | 104 | union synthhid_version version_requested; |
e6f83b78 GKH |
105 | }; |
106 | ||
107 | struct synthhid_protocol_response { | |
0ce815d5 | 108 | struct synthhid_msg_hdr header; |
480c28df | 109 | union synthhid_version version_requested; |
325eae14 | 110 | unsigned char approved; |
e6f83b78 GKH |
111 | }; |
112 | ||
113 | struct synthhid_device_info { | |
0ce815d5 | 114 | struct synthhid_msg_hdr header; |
98ad91ed | 115 | struct hv_input_dev_info hid_dev_info; |
18bc44e3 | 116 | struct hid_descriptor hid_descriptor; |
e6f83b78 | 117 | }; |
fa003500 | 118 | |
e6f83b78 | 119 | struct synthhid_device_info_ack { |
0ce815d5 | 120 | struct synthhid_msg_hdr header; |
6ed10de1 | 121 | unsigned char reserved; |
e6f83b78 | 122 | }; |
fa003500 | 123 | |
e6f83b78 | 124 | struct synthhid_input_report { |
0ce815d5 | 125 | struct synthhid_msg_hdr header; |
e93eff9c | 126 | char buffer[1]; |
e6f83b78 | 127 | }; |
fa003500 GKH |
128 | |
129 | #pragma pack(pop) | |
130 | ||
94fcc888 GKH |
131 | #define INPUTVSC_SEND_RING_BUFFER_SIZE 10*PAGE_SIZE |
132 | #define INPUTVSC_RECV_RING_BUFFER_SIZE 10*PAGE_SIZE | |
0c3a6ede GKH |
133 | |
134 | #define NBITS(x) (((x)/BITS_PER_LONG)+1) | |
135 | ||
9dccaa63 GKH |
136 | enum pipe_prot_msg_type { |
137 | PipeMessageInvalid = 0, | |
138 | PipeMessageData, | |
139 | PipeMessageMaximum | |
140 | }; | |
141 | ||
142 | ||
143 | struct pipe_prt_msg { | |
2012d40d | 144 | enum pipe_prot_msg_type type; |
9877fa44 | 145 | u32 size; |
e7de0adf | 146 | char data[1]; |
9dccaa63 GKH |
147 | }; |
148 | ||
149 | /* | |
150 | * Data types | |
151 | */ | |
152 | struct mousevsc_prt_msg { | |
2012d40d | 153 | enum pipe_prot_msg_type type; |
9877fa44 | 154 | u32 size; |
9dccaa63 | 155 | union { |
5ff9b906 GKH |
156 | struct synthhid_protocol_request request; |
157 | struct synthhid_protocol_response response; | |
158 | struct synthhid_device_info_ack ack; | |
d7fa1a46 | 159 | }; |
9dccaa63 GKH |
160 | }; |
161 | ||
162 | /* | |
163 | * Represents an mousevsc device | |
164 | */ | |
165 | struct mousevsc_dev { | |
ac41d402 | 166 | struct hv_device *device; |
9dccaa63 | 167 | /* 0 indicates the device is being destroyed */ |
ac41d402 HJ |
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; | |
9dccaa63 | 173 | /* Synchronize the request/response if needed */ |
ac41d402 HJ |
174 | wait_queue_head_t protocol_wait_event; |
175 | wait_queue_head_t dev_info_wait_event; | |
9dccaa63 GKH |
176 | int protocol_wait_condition; |
177 | int device_wait_condition; | |
ac41d402 | 178 | int dev_info_status; |
9dccaa63 | 179 | |
ac41d402 HJ |
180 | struct hid_descriptor *hid_desc; |
181 | unsigned char *report_desc; | |
182 | u32 report_desc_size; | |
98ad91ed | 183 | struct hv_input_dev_info hid_dev_info; |
9dccaa63 GKH |
184 | }; |
185 | ||
186 | ||
92d40b76 | 187 | static const char *driver_name = "mousevsc"; |
9dccaa63 GKH |
188 | |
189 | /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */ | |
df7ed924 | 190 | static const struct hv_guid mouse_guid = { |
9dccaa63 GKH |
191 | .data = {0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c, |
192 | 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A} | |
193 | }; | |
194 | ||
0f88ea5b | 195 | static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info); |
4f143134 GKH |
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 | ||
2ba6810b | 199 | static struct mousevsc_dev *alloc_input_device(struct hv_device *device) |
9dccaa63 | 200 | { |
c0765e99 | 201 | struct mousevsc_dev *input_dev; |
9dccaa63 | 202 | |
c0765e99 | 203 | input_dev = kzalloc(sizeof(struct mousevsc_dev), GFP_KERNEL); |
9dccaa63 | 204 | |
c0765e99 | 205 | if (!input_dev) |
9dccaa63 GKH |
206 | return NULL; |
207 | ||
208 | /* | |
209 | * Set to 2 to allow both inbound and outbound traffics | |
94e44cb5 | 210 | * (ie get_input_device() and must_get_input_device()) to proceed. |
9dccaa63 | 211 | */ |
c0765e99 | 212 | atomic_cmpxchg(&input_dev->ref_count, 0, 2); |
9dccaa63 | 213 | |
c0765e99 HJ |
214 | input_dev->device = device; |
215 | device->ext = input_dev; | |
9dccaa63 | 216 | |
c0765e99 | 217 | return input_dev; |
9dccaa63 GKH |
218 | } |
219 | ||
2ba6810b | 220 | static void free_input_device(struct mousevsc_dev *device) |
9dccaa63 | 221 | { |
ac41d402 | 222 | WARN_ON(atomic_read(&device->ref_count) == 0); |
2ba6810b | 223 | kfree(device); |
9dccaa63 GKH |
224 | } |
225 | ||
226 | /* | |
227 | * Get the inputdevice object if exists and its refcount > 1 | |
228 | */ | |
2ba6810b | 229 | static struct mousevsc_dev *get_input_device(struct hv_device *device) |
9dccaa63 | 230 | { |
c0765e99 | 231 | struct mousevsc_dev *input_dev; |
9dccaa63 | 232 | |
c0765e99 | 233 | input_dev = (struct mousevsc_dev *)device->ext; |
9dccaa63 GKH |
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", | |
c0765e99 | 241 | * input_dev->ref_count); |
9dccaa63 GKH |
242 | */ |
243 | ||
c0765e99 HJ |
244 | if (input_dev && atomic_read(&input_dev->ref_count) > 1) |
245 | atomic_inc(&input_dev->ref_count); | |
9dccaa63 | 246 | else |
c0765e99 | 247 | input_dev = NULL; |
9dccaa63 | 248 | |
c0765e99 | 249 | return input_dev; |
9dccaa63 GKH |
250 | } |
251 | ||
252 | /* | |
253 | * Get the inputdevice object iff exists and its refcount > 0 | |
254 | */ | |
2ba6810b | 255 | static struct mousevsc_dev *must_get_input_device(struct hv_device *device) |
9dccaa63 | 256 | { |
c0765e99 | 257 | struct mousevsc_dev *input_dev; |
9dccaa63 | 258 | |
c0765e99 | 259 | input_dev = (struct mousevsc_dev *)device->ext; |
9dccaa63 | 260 | |
c0765e99 HJ |
261 | if (input_dev && atomic_read(&input_dev->ref_count)) |
262 | atomic_inc(&input_dev->ref_count); | |
9dccaa63 | 263 | else |
c0765e99 | 264 | input_dev = NULL; |
9dccaa63 | 265 | |
c0765e99 | 266 | return input_dev; |
9dccaa63 GKH |
267 | } |
268 | ||
2ba6810b | 269 | static void put_input_device(struct hv_device *device) |
9dccaa63 | 270 | { |
c0765e99 | 271 | struct mousevsc_dev *input_dev; |
9dccaa63 | 272 | |
c0765e99 | 273 | input_dev = (struct mousevsc_dev *)device->ext; |
9dccaa63 | 274 | |
c0765e99 | 275 | atomic_dec(&input_dev->ref_count); |
9dccaa63 GKH |
276 | } |
277 | ||
278 | /* | |
94e44cb5 | 279 | * Drop ref count to 1 to effectively disable get_input_device() |
9dccaa63 | 280 | */ |
2ba6810b | 281 | static struct mousevsc_dev *release_input_device(struct hv_device *device) |
9dccaa63 | 282 | { |
c0765e99 | 283 | struct mousevsc_dev *input_dev; |
9dccaa63 | 284 | |
c0765e99 | 285 | input_dev = (struct mousevsc_dev *)device->ext; |
9dccaa63 GKH |
286 | |
287 | /* Busy wait until the ref drop to 2, then set it to 1 */ | |
c0765e99 | 288 | while (atomic_cmpxchg(&input_dev->ref_count, 2, 1) != 2) |
9dccaa63 GKH |
289 | udelay(100); |
290 | ||
c0765e99 | 291 | return input_dev; |
9dccaa63 GKH |
292 | } |
293 | ||
294 | /* | |
2ba6810b | 295 | * Drop ref count to 0. No one can use input_device object. |
9dccaa63 | 296 | */ |
2ba6810b | 297 | static struct mousevsc_dev *final_release_input_device(struct hv_device *device) |
9dccaa63 | 298 | { |
c0765e99 | 299 | struct mousevsc_dev *input_dev; |
9dccaa63 | 300 | |
c0765e99 | 301 | input_dev = (struct mousevsc_dev *)device->ext; |
9dccaa63 GKH |
302 | |
303 | /* Busy wait until the ref drop to 1, then set it to 0 */ | |
c0765e99 | 304 | while (atomic_cmpxchg(&input_dev->ref_count, 1, 0) != 1) |
9dccaa63 GKH |
305 | udelay(100); |
306 | ||
2ba6810b | 307 | device->ext = NULL; |
c0765e99 | 308 | return input_dev; |
9dccaa63 GKH |
309 | } |
310 | ||
2ba6810b HJ |
311 | static void mousevsc_on_send_completion(struct hv_device *device, |
312 | struct vmpacket_descriptor *packet) | |
9dccaa63 | 313 | { |
c0765e99 | 314 | struct mousevsc_dev *input_dev; |
9dccaa63 GKH |
315 | void *request; |
316 | ||
c0765e99 HJ |
317 | input_dev = must_get_input_device(device); |
318 | if (!input_dev) { | |
9dccaa63 GKH |
319 | pr_err("unable to get input device...device being destroyed?"); |
320 | return; | |
321 | } | |
322 | ||
2ba6810b | 323 | request = (void *)(unsigned long)packet->trans_id; |
9dccaa63 | 324 | |
c0765e99 | 325 | if (request == &input_dev->protocol_req) { |
9dccaa63 GKH |
326 | /* FIXME */ |
327 | /* Shouldn't we be doing something here? */ | |
328 | } | |
329 | ||
2ba6810b | 330 | put_input_device(device); |
9dccaa63 GKH |
331 | } |
332 | ||
2ba6810b HJ |
333 | static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device, |
334 | struct synthhid_device_info *device_info) | |
9dccaa63 GKH |
335 | { |
336 | int ret = 0; | |
337 | struct hid_descriptor *desc; | |
338 | struct mousevsc_prt_msg ack; | |
339 | ||
340 | /* Assume success for now */ | |
ac41d402 | 341 | input_device->dev_info_status = 0; |
9dccaa63 GKH |
342 | |
343 | /* Save the device attr */ | |
2ba6810b HJ |
344 | memcpy(&input_device->hid_dev_info, &device_info->hid_dev_info, |
345 | sizeof(struct hv_input_dev_info)); | |
9dccaa63 GKH |
346 | |
347 | /* Save the hid desc */ | |
2ba6810b | 348 | desc = &device_info->hid_descriptor; |
9dccaa63 GKH |
349 | WARN_ON(desc->bLength > 0); |
350 | ||
ac41d402 | 351 | input_device->hid_desc = kzalloc(desc->bLength, GFP_KERNEL); |
9dccaa63 | 352 | |
ac41d402 | 353 | if (!input_device->hid_desc) { |
9dccaa63 GKH |
354 | pr_err("unable to allocate hid descriptor - size %d", desc->bLength); |
355 | goto Cleanup; | |
356 | } | |
357 | ||
ac41d402 | 358 | memcpy(input_device->hid_desc, desc, desc->bLength); |
9dccaa63 GKH |
359 | |
360 | /* Save the report desc */ | |
ac41d402 HJ |
361 | input_device->report_desc_size = desc->desc[0].wDescriptorLength; |
362 | input_device->report_desc = kzalloc(input_device->report_desc_size, | |
9dccaa63 GKH |
363 | GFP_KERNEL); |
364 | ||
ac41d402 | 365 | if (!input_device->report_desc) { |
9dccaa63 | 366 | pr_err("unable to allocate report descriptor - size %d", |
ac41d402 | 367 | input_device->report_desc_size); |
9dccaa63 GKH |
368 | goto Cleanup; |
369 | } | |
370 | ||
ac41d402 | 371 | memcpy(input_device->report_desc, |
9dccaa63 GKH |
372 | ((unsigned char *)desc) + desc->bLength, |
373 | desc->desc[0].wDescriptorLength); | |
374 | ||
375 | /* Send the ack */ | |
75e4fb22 | 376 | memset(&ack, 0, sizeof(struct mousevsc_prt_msg)); |
9dccaa63 | 377 | |
2012d40d | 378 | ack.type = PipeMessageData; |
9877fa44 | 379 | ack.size = sizeof(struct synthhid_device_info_ack); |
9dccaa63 | 380 | |
5ff9b906 GKH |
381 | ack.ack.header.type = SynthHidInitialDeviceInfoAck; |
382 | ack.ack.header.size = 1; | |
383 | ack.ack.reserved = 0; | |
9dccaa63 | 384 | |
ac41d402 | 385 | ret = vmbus_sendpacket(input_device->device->channel, |
9dccaa63 | 386 | &ack, |
e6f83b78 GKH |
387 | sizeof(struct pipe_prt_msg) - sizeof(unsigned char) + |
388 | sizeof(struct synthhid_device_info_ack), | |
9dccaa63 GKH |
389 | (unsigned long)&ack, |
390 | VM_PKT_DATA_INBAND, | |
391 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
392 | if (ret != 0) { | |
e6f83b78 | 393 | pr_err("unable to send synthhid device info ack - ret %d", |
9dccaa63 GKH |
394 | ret); |
395 | goto Cleanup; | |
396 | } | |
397 | ||
2ba6810b | 398 | input_device->device_wait_condition = 1; |
ac41d402 | 399 | wake_up(&input_device->dev_info_wait_event); |
9dccaa63 GKH |
400 | |
401 | return; | |
402 | ||
403 | Cleanup: | |
ac41d402 HJ |
404 | kfree(input_device->hid_desc); |
405 | input_device->hid_desc = NULL; | |
9dccaa63 | 406 | |
ac41d402 HJ |
407 | kfree(input_device->report_desc); |
408 | input_device->report_desc = NULL; | |
9dccaa63 | 409 | |
ac41d402 | 410 | input_device->dev_info_status = -1; |
2ba6810b | 411 | input_device->device_wait_condition = 1; |
ac41d402 | 412 | wake_up(&input_device->dev_info_wait_event); |
9dccaa63 GKH |
413 | } |
414 | ||
2ba6810b HJ |
415 | static void mousevsc_on_receive_input_report(struct mousevsc_dev *input_device, |
416 | struct synthhid_input_report *input_report) | |
9dccaa63 | 417 | { |
746e8ca4 | 418 | struct hv_driver *input_drv; |
9dccaa63 | 419 | |
ac41d402 | 420 | if (!input_device->init_complete) { |
2ba6810b | 421 | pr_info("Initialization incomplete...ignoring input_report msg"); |
9dccaa63 GKH |
422 | return; |
423 | } | |
424 | ||
746e8ca4 | 425 | input_drv = drv_to_hv_drv(input_device->device->device.driver); |
9dccaa63 | 426 | |
ac41d402 | 427 | inputreport_callback(input_device->device, |
2ba6810b HJ |
428 | input_report->buffer, |
429 | input_report->header.size); | |
9dccaa63 GKH |
430 | } |
431 | ||
2ba6810b HJ |
432 | static void mousevsc_on_receive(struct hv_device *device, |
433 | struct vmpacket_descriptor *packet) | |
9dccaa63 | 434 | { |
c0765e99 HJ |
435 | struct pipe_prt_msg *pipe_msg; |
436 | struct synthhid_msg *hid_msg; | |
437 | struct mousevsc_dev *input_dev; | |
9dccaa63 | 438 | |
c0765e99 HJ |
439 | input_dev = must_get_input_device(device); |
440 | if (!input_dev) { | |
9dccaa63 GKH |
441 | pr_err("unable to get input device...device being destroyed?"); |
442 | return; | |
443 | } | |
444 | ||
c0765e99 | 445 | pipe_msg = (struct pipe_prt_msg *)((unsigned long)packet + |
2ba6810b | 446 | (packet->offset8 << 3)); |
9dccaa63 | 447 | |
c0765e99 | 448 | if (pipe_msg->type != PipeMessageData) { |
9dccaa63 | 449 | pr_err("unknown pipe msg type - type %d len %d", |
c0765e99 | 450 | pipe_msg->type, pipe_msg->size); |
2ba6810b | 451 | put_input_device(device); |
9dccaa63 GKH |
452 | return ; |
453 | } | |
454 | ||
c0765e99 | 455 | hid_msg = (struct synthhid_msg *)&pipe_msg->data[0]; |
9dccaa63 | 456 | |
c0765e99 | 457 | switch (hid_msg->header.type) { |
9dccaa63 | 458 | case SynthHidProtocolResponse: |
c0765e99 HJ |
459 | memcpy(&input_dev->protocol_resp, pipe_msg, |
460 | pipe_msg->size + sizeof(struct pipe_prt_msg) - | |
9877fa44 | 461 | sizeof(unsigned char)); |
c0765e99 HJ |
462 | input_dev->protocol_wait_condition = 1; |
463 | wake_up(&input_dev->protocol_wait_event); | |
9dccaa63 GKH |
464 | break; |
465 | ||
466 | case SynthHidInitialDeviceInfo: | |
c0765e99 | 467 | WARN_ON(pipe_msg->size >= sizeof(struct hv_input_dev_info)); |
9dccaa63 GKH |
468 | |
469 | /* | |
470 | * Parse out the device info into device attr, | |
471 | * hid desc and report desc | |
472 | */ | |
c0765e99 HJ |
473 | mousevsc_on_receive_device_info(input_dev, |
474 | (struct synthhid_device_info *)&pipe_msg->data[0]); | |
9dccaa63 GKH |
475 | break; |
476 | case SynthHidInputReport: | |
c0765e99 HJ |
477 | mousevsc_on_receive_input_report(input_dev, |
478 | (struct synthhid_input_report *)&pipe_msg->data[0]); | |
9dccaa63 GKH |
479 | |
480 | break; | |
481 | default: | |
482 | pr_err("unsupported hid msg type - type %d len %d", | |
c0765e99 | 483 | hid_msg->header.type, hid_msg->header.size); |
9dccaa63 GKH |
484 | break; |
485 | } | |
486 | ||
2ba6810b | 487 | put_input_device(device); |
9dccaa63 GKH |
488 | } |
489 | ||
2ba6810b | 490 | static void mousevsc_on_channel_callback(void *context) |
9dccaa63 GKH |
491 | { |
492 | const int packetSize = 0x100; | |
493 | int ret = 0; | |
2ba6810b | 494 | struct hv_device *device = (struct hv_device *)context; |
c0765e99 | 495 | struct mousevsc_dev *input_dev; |
9dccaa63 | 496 | |
c0765e99 HJ |
497 | u32 bytes_recvd; |
498 | u64 req_id; | |
9dccaa63 GKH |
499 | unsigned char packet[packetSize]; |
500 | struct vmpacket_descriptor *desc; | |
501 | unsigned char *buffer = packet; | |
502 | int bufferlen = packetSize; | |
503 | ||
c0765e99 | 504 | input_dev = must_get_input_device(device); |
9dccaa63 | 505 | |
c0765e99 | 506 | if (!input_dev) { |
9dccaa63 GKH |
507 | pr_err("unable to get input device...device being destroyed?"); |
508 | return; | |
509 | } | |
510 | ||
511 | do { | |
c0765e99 HJ |
512 | ret = vmbus_recvpacket_raw(device->channel, buffer, |
513 | bufferlen, &bytes_recvd, &req_id); | |
9dccaa63 GKH |
514 | |
515 | if (ret == 0) { | |
c0765e99 | 516 | if (bytes_recvd > 0) { |
9dccaa63 GKH |
517 | desc = (struct vmpacket_descriptor *)buffer; |
518 | ||
519 | switch (desc->type) { | |
520 | case VM_PKT_COMP: | |
94e44cb5 HJ |
521 | mousevsc_on_send_completion( |
522 | device, desc); | |
9dccaa63 GKH |
523 | break; |
524 | ||
525 | case VM_PKT_DATA_INBAND: | |
94e44cb5 HJ |
526 | mousevsc_on_receive( |
527 | device, desc); | |
9dccaa63 GKH |
528 | break; |
529 | ||
530 | default: | |
531 | pr_err("unhandled packet type %d, tid %llx len %d\n", | |
532 | desc->type, | |
c0765e99 HJ |
533 | req_id, |
534 | bytes_recvd); | |
9dccaa63 GKH |
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 */ | |
c0765e99 HJ |
560 | bufferlen = bytes_recvd; |
561 | buffer = kzalloc(bytes_recvd, GFP_KERNEL); | |
9dccaa63 GKH |
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!", | |
c0765e99 | 569 | bytes_recvd); |
9dccaa63 GKH |
570 | break; |
571 | } | |
572 | } | |
573 | } while (1); | |
574 | ||
94e44cb5 | 575 | put_input_device(device); |
9dccaa63 GKH |
576 | |
577 | return; | |
578 | } | |
0c3a6ede | 579 | |
2ba6810b | 580 | static int mousevsc_connect_to_vsp(struct hv_device *device) |
ac2c9033 GKH |
581 | { |
582 | int ret = 0; | |
c0765e99 | 583 | struct mousevsc_dev *input_dev; |
ac2c9033 GKH |
584 | struct mousevsc_prt_msg *request; |
585 | struct mousevsc_prt_msg *response; | |
586 | ||
c0765e99 | 587 | input_dev = get_input_device(device); |
ac2c9033 | 588 | |
c0765e99 | 589 | if (!input_dev) { |
ac2c9033 GKH |
590 | pr_err("unable to get input device...device being destroyed?"); |
591 | return -1; | |
592 | } | |
593 | ||
c0765e99 HJ |
594 | init_waitqueue_head(&input_dev->protocol_wait_event); |
595 | init_waitqueue_head(&input_dev->dev_info_wait_event); | |
ac2c9033 | 596 | |
c0765e99 | 597 | request = &input_dev->protocol_req; |
ac2c9033 GKH |
598 | |
599 | /* | |
600 | * Now, initiate the vsc/vsp initialization protocol on the open channel | |
601 | */ | |
75e4fb22 | 602 | memset(request, 0, sizeof(struct mousevsc_prt_msg)); |
ac2c9033 | 603 | |
2012d40d | 604 | request->type = PipeMessageData; |
9877fa44 | 605 | request->size = sizeof(struct synthhid_protocol_request); |
ac2c9033 | 606 | |
5ff9b906 GKH |
607 | request->request.header.type = SynthHidProtocolRequest; |
608 | request->request.header.size = sizeof(unsigned long); | |
609 | request->request.version_requested.version = SYNTHHID_INPUT_VERSION; | |
ac2c9033 GKH |
610 | |
611 | pr_info("synthhid protocol request..."); | |
612 | ||
2ba6810b | 613 | ret = vmbus_sendpacket(device->channel, request, |
ac2c9033 GKH |
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 | ||
c0765e99 HJ |
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) { | |
ac2c9033 GKH |
629 | ret = -ETIMEDOUT; |
630 | goto Cleanup; | |
631 | } | |
632 | ||
c0765e99 | 633 | response = &input_dev->protocol_resp; |
ac2c9033 | 634 | |
5ff9b906 | 635 | if (!response->response.approved) { |
ac2c9033 | 636 | pr_err("synthhid protocol request failed (version %d)", |
480c28df | 637 | SYNTHHID_INPUT_VERSION); |
ac2c9033 GKH |
638 | ret = -1; |
639 | goto Cleanup; | |
640 | } | |
641 | ||
c0765e99 HJ |
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) { | |
ac2c9033 GKH |
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 | */ | |
c0765e99 | 654 | if (!input_dev->dev_info_status) |
ac2c9033 GKH |
655 | pr_info("**** input channel up and running!! ****"); |
656 | else | |
657 | ret = -1; | |
658 | ||
659 | Cleanup: | |
2ba6810b | 660 | put_input_device(device); |
ac2c9033 GKH |
661 | |
662 | return ret; | |
663 | } | |
664 | ||
2ba6810b HJ |
665 | static int mousevsc_on_device_add(struct hv_device *device, |
666 | void *additional_info) | |
ac2c9033 GKH |
667 | { |
668 | int ret = 0; | |
c0765e99 | 669 | struct mousevsc_dev *input_dev; |
746e8ca4 | 670 | struct hv_driver *input_drv; |
98ad91ed | 671 | struct hv_input_dev_info dev_info; |
ac2c9033 | 672 | |
c0765e99 | 673 | input_dev = alloc_input_device(device); |
ac2c9033 | 674 | |
c0765e99 | 675 | if (!input_dev) { |
ac2c9033 GKH |
676 | ret = -1; |
677 | goto Cleanup; | |
678 | } | |
679 | ||
c0765e99 | 680 | input_dev->init_complete = false; |
ac2c9033 GKH |
681 | |
682 | /* Open the channel */ | |
2ba6810b | 683 | ret = vmbus_open(device->channel, |
ac2c9033 GKH |
684 | INPUTVSC_SEND_RING_BUFFER_SIZE, |
685 | INPUTVSC_RECV_RING_BUFFER_SIZE, | |
686 | NULL, | |
687 | 0, | |
94e44cb5 | 688 | mousevsc_on_channel_callback, |
2ba6810b | 689 | device |
ac2c9033 GKH |
690 | ); |
691 | ||
692 | if (ret != 0) { | |
693 | pr_err("unable to open channel: %d", ret); | |
c0765e99 | 694 | free_input_device(input_dev); |
ac2c9033 GKH |
695 | return -1; |
696 | } | |
697 | ||
698 | pr_info("InputVsc channel open: %d", ret); | |
699 | ||
2ba6810b | 700 | ret = mousevsc_connect_to_vsp(device); |
ac2c9033 GKH |
701 | |
702 | if (ret != 0) { | |
703 | pr_err("unable to connect channel: %d", ret); | |
704 | ||
2ba6810b | 705 | vmbus_close(device->channel); |
c0765e99 | 706 | free_input_device(input_dev); |
ac2c9033 GKH |
707 | return ret; |
708 | } | |
709 | ||
746e8ca4 | 710 | input_drv = drv_to_hv_drv(input_dev->device->device.driver); |
ac2c9033 | 711 | |
c0765e99 HJ |
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; | |
98ad91ed | 715 | strcpy(dev_info.name, "Microsoft Vmbus HID-compliant Mouse"); |
ac2c9033 GKH |
716 | |
717 | /* Send the device info back up */ | |
2ba6810b | 718 | deviceinfo_callback(device, &dev_info); |
ac2c9033 GKH |
719 | |
720 | /* Send the report desc back up */ | |
721 | /* workaround SA-167 */ | |
c0765e99 HJ |
722 | if (input_dev->report_desc[14] == 0x25) |
723 | input_dev->report_desc[14] = 0x29; | |
ac2c9033 | 724 | |
c0765e99 HJ |
725 | reportdesc_callback(device, input_dev->report_desc, |
726 | input_dev->report_desc_size); | |
ac2c9033 | 727 | |
c0765e99 | 728 | input_dev->init_complete = true; |
ac2c9033 GKH |
729 | |
730 | Cleanup: | |
731 | return ret; | |
732 | } | |
733 | ||
2ba6810b | 734 | static int mousevsc_on_device_remove(struct hv_device *device) |
ac2c9033 | 735 | { |
c0765e99 | 736 | struct mousevsc_dev *input_dev; |
ac2c9033 GKH |
737 | int ret = 0; |
738 | ||
739 | pr_info("disabling input device (%p)...", | |
2ba6810b | 740 | device->ext); |
ac2c9033 | 741 | |
c0765e99 | 742 | input_dev = release_input_device(device); |
ac2c9033 GKH |
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 | */ | |
c0765e99 | 751 | while (input_dev->num_outstanding_req) { |
ac41d402 | 752 | pr_info("waiting for %d requests to complete...", |
c0765e99 | 753 | input_dev->num_outstanding_req); |
ac2c9033 GKH |
754 | |
755 | udelay(100); | |
756 | } | |
757 | ||
2ba6810b | 758 | pr_info("removing input device (%p)...", device->ext); |
ac2c9033 | 759 | |
c0765e99 | 760 | input_dev = final_release_input_device(device); |
ac2c9033 | 761 | |
c0765e99 | 762 | pr_info("input device (%p) safe to remove", input_dev); |
ac2c9033 GKH |
763 | |
764 | /* Close the channel */ | |
2ba6810b | 765 | vmbus_close(device->channel); |
ac2c9033 | 766 | |
c0765e99 | 767 | free_input_device(input_dev); |
ac2c9033 GKH |
768 | |
769 | return ret; | |
770 | } | |
771 | ||
ac2c9033 | 772 | |
0c3a6ede GKH |
773 | /* |
774 | * Data types | |
775 | */ | |
776 | struct input_device_context { | |
6bad88da | 777 | struct hv_device *device_ctx; |
0c3a6ede | 778 | struct hid_device *hid_device; |
0f88ea5b | 779 | struct hv_input_dev_info device_info; |
0c3a6ede GKH |
780 | int connected; |
781 | }; | |
782 | ||
0c3a6ede | 783 | |
0f88ea5b | 784 | static void deviceinfo_callback(struct hv_device *dev, struct hv_input_dev_info *info) |
0c3a6ede | 785 | { |
0c3a6ede | 786 | struct input_device_context *input_device_ctx = |
6bad88da | 787 | dev_get_drvdata(&dev->device); |
0c3a6ede GKH |
788 | |
789 | memcpy(&input_device_ctx->device_info, info, | |
0f88ea5b | 790 | sizeof(struct hv_input_dev_info)); |
0c3a6ede | 791 | |
4f143134 | 792 | DPRINT_INFO(INPUTVSC_DRV, "%s", __func__); |
0c3a6ede GKH |
793 | } |
794 | ||
4f143134 | 795 | static void inputreport_callback(struct hv_device *dev, void *packet, u32 len) |
0c3a6ede GKH |
796 | { |
797 | int ret = 0; | |
798 | ||
0c3a6ede | 799 | struct input_device_context *input_dev_ctx = |
6bad88da | 800 | dev_get_drvdata(&dev->device); |
0c3a6ede GKH |
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 | ||
ac2c9033 | 808 | static int mousevsc_hid_open(struct hid_device *hid) |
0c3a6ede GKH |
809 | { |
810 | return 0; | |
811 | } | |
812 | ||
ac2c9033 | 813 | static void mousevsc_hid_close(struct hid_device *hid) |
0c3a6ede GKH |
814 | { |
815 | } | |
816 | ||
9efd21e1 | 817 | static int mousevsc_probe(struct hv_device *dev) |
0c3a6ede GKH |
818 | { |
819 | int ret = 0; | |
820 | ||
0c3a6ede GKH |
821 | struct input_device_context *input_dev_ctx; |
822 | ||
823 | input_dev_ctx = kmalloc(sizeof(struct input_device_context), | |
824 | GFP_KERNEL); | |
825 | ||
9efd21e1 | 826 | dev_set_drvdata(&dev->device, input_dev_ctx); |
0c3a6ede GKH |
827 | |
828 | /* Call to the vsc driver to add the device */ | |
d1f01b3e | 829 | ret = mousevsc_on_device_add(dev, NULL); |
0c3a6ede GKH |
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 | ||
415b023a | 840 | static int mousevsc_remove(struct hv_device *dev) |
0c3a6ede GKH |
841 | { |
842 | int ret = 0; | |
843 | ||
0c3a6ede GKH |
844 | struct input_device_context *input_dev_ctx; |
845 | ||
846 | input_dev_ctx = kmalloc(sizeof(struct input_device_context), | |
847 | GFP_KERNEL); | |
848 | ||
415b023a | 849 | dev_set_drvdata(&dev->device, input_dev_ctx); |
0c3a6ede GKH |
850 | |
851 | if (input_dev_ctx->connected) { | |
852 | hidinput_disconnect(input_dev_ctx->hid_device); | |
853 | input_dev_ctx->connected = 0; | |
854 | } | |
855 | ||
0c3a6ede GKH |
856 | /* |
857 | * Call to the vsc driver to let it know that the device | |
858 | * is being removed | |
859 | */ | |
f1f66f8f | 860 | ret = mousevsc_on_device_remove(dev); |
0c3a6ede GKH |
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 | ||
4f143134 | 872 | static void reportdesc_callback(struct hv_device *dev, void *packet, u32 len) |
0c3a6ede | 873 | { |
0c3a6ede | 874 | struct input_device_context *input_device_ctx = |
6bad88da | 875 | dev_get_drvdata(&dev->device); |
0c3a6ede GKH |
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 | ||
c9246c90 | 892 | hid_dev->bus = BUS_VIRTUAL; |
0f88ea5b GKH |
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; | |
6bad88da | 896 | hid_dev->dev = dev->device; |
0c3a6ede GKH |
897 | |
898 | sprintf(hid_dev->name, "%s", | |
0f88ea5b | 899 | input_device_ctx->device_info.name); |
0c3a6ede GKH |
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 | ||
ac2c9033 | 925 | static int mousevsc_drv_exit_cb(struct device *dev, void *data) |
0c3a6ede GKH |
926 | { |
927 | struct device **curr = (struct device **)data; | |
928 | *curr = dev; | |
929 | ||
930 | return 1; | |
931 | } | |
932 | ||
746e8ca4 S |
933 | static struct hv_driver mousevsc_drv = { |
934 | .probe = mousevsc_probe, | |
935 | .remove = mousevsc_remove, | |
1745ec50 | 936 | }; |
eebdd6f2 | 937 | |
ac2c9033 | 938 | static void mousevsc_drv_exit(void) |
0c3a6ede | 939 | { |
746e8ca4 | 940 | struct hv_driver *drv = &mousevsc_drv; |
0c3a6ede GKH |
941 | int ret; |
942 | ||
943 | struct device *current_dev = NULL; | |
944 | ||
945 | while (1) { | |
946 | current_dev = NULL; | |
947 | ||
948 | /* Get the device */ | |
150f9398 | 949 | ret = driver_for_each_device(&drv->driver, NULL, |
e8290f9f GKH |
950 | (void *)¤t_dev, |
951 | mousevsc_drv_exit_cb); | |
0c3a6ede GKH |
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 | ||
150f9398 | 962 | vmbus_child_driver_unregister(&drv->driver); |
0c3a6ede GKH |
963 | |
964 | return; | |
965 | } | |
966 | ||
967 | static int __init mousevsc_init(void) | |
968 | { | |
746e8ca4 | 969 | struct hv_driver *drv = &mousevsc_drv; |
0c3a6ede GKH |
970 | |
971 | DPRINT_INFO(INPUTVSC_DRV, "Hyper-V Mouse driver initializing."); | |
972 | ||
cf131e6a S |
973 | memcpy(&drv->dev_type, &mouse_guid, |
974 | sizeof(struct hv_guid)); | |
7ced4810 | 975 | |
eb94b2f5 | 976 | drv->driver.name = driver_name; |
a5fae3b5 | 977 | drv->name = driver_name; |
7ced4810 | 978 | |
7ced4810 | 979 | /* The driver belongs to vmbus */ |
150f9398 | 980 | vmbus_child_driver_register(&drv->driver); |
7ced4810 GKH |
981 | |
982 | return 0; | |
0c3a6ede GKH |
983 | } |
984 | ||
985 | static void __exit mousevsc_exit(void) | |
986 | { | |
987 | mousevsc_drv_exit(); | |
988 | } | |
989 | ||
76e63665 GKH |
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 | ||
0c3a6ede GKH |
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); | |
76e63665 | 1008 | #endif |
0c3a6ede GKH |
1009 | |
1010 | MODULE_LICENSE("GPL"); | |
1011 | MODULE_VERSION(HV_DRV_VERSION); | |
1012 | module_init(mousevsc_init); | |
1013 | module_exit(mousevsc_exit); | |
1014 |