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