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