Commit | Line | Data |
---|---|---|
2f31c525 BT |
1 | /* |
2 | * HIDPP protocol for Logitech Unifying receivers | |
3 | * | |
4 | * Copyright (c) 2011 Logitech (c) | |
5 | * Copyright (c) 2012-2013 Google (c) | |
6 | * Copyright (c) 2013-2014 Red Hat Inc. | |
7 | */ | |
8 | ||
9 | /* | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the Free | |
12 | * Software Foundation; version 2 of the License. | |
13 | */ | |
14 | ||
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
16 | ||
17 | #include <linux/device.h> | |
18 | #include <linux/hid.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/sched.h> | |
22 | #include <linux/kfifo.h> | |
23 | #include <linux/input/mt.h> | |
24 | #include <asm/unaligned.h> | |
25 | #include "hid-ids.h" | |
26 | ||
27 | MODULE_LICENSE("GPL"); | |
28 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); | |
29 | MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>"); | |
30 | ||
31 | #define REPORT_ID_HIDPP_SHORT 0x10 | |
32 | #define REPORT_ID_HIDPP_LONG 0x11 | |
33 | ||
34 | #define HIDPP_REPORT_SHORT_LENGTH 7 | |
35 | #define HIDPP_REPORT_LONG_LENGTH 20 | |
36 | ||
37 | #define HIDPP_QUIRK_CLASS_WTP BIT(0) | |
38 | ||
c39e3d5f BT |
39 | /* bits 1..20 are reserved for classes */ |
40 | #define HIDPP_QUIRK_DELAYED_INIT BIT(21) | |
57ac86cf | 41 | #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22) |
3a61e975 | 42 | #define HIDPP_QUIRK_MULTI_INPUT BIT(23) |
c39e3d5f | 43 | |
2f31c525 BT |
44 | /* |
45 | * There are two hidpp protocols in use, the first version hidpp10 is known | |
46 | * as register access protocol or RAP, the second version hidpp20 is known as | |
47 | * feature access protocol or FAP | |
48 | * | |
49 | * Most older devices (including the Unifying usb receiver) use the RAP protocol | |
50 | * where as most newer devices use the FAP protocol. Both protocols are | |
51 | * compatible with the underlying transport, which could be usb, Unifiying, or | |
52 | * bluetooth. The message lengths are defined by the hid vendor specific report | |
53 | * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and | |
54 | * the HIDPP_LONG report type (total message length 20 bytes) | |
55 | * | |
56 | * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG | |
57 | * messages. The Unifying receiver itself responds to RAP messages (device index | |
58 | * is 0xFF for the receiver), and all messages (short or long) with a device | |
59 | * index between 1 and 6 are passed untouched to the corresponding paired | |
60 | * Unifying device. | |
61 | * | |
62 | * The paired device can be RAP or FAP, it will receive the message untouched | |
63 | * from the Unifiying receiver. | |
64 | */ | |
65 | ||
66 | struct fap { | |
67 | u8 feature_index; | |
68 | u8 funcindex_clientid; | |
69 | u8 params[HIDPP_REPORT_LONG_LENGTH - 4U]; | |
70 | }; | |
71 | ||
72 | struct rap { | |
73 | u8 sub_id; | |
74 | u8 reg_address; | |
75 | u8 params[HIDPP_REPORT_LONG_LENGTH - 4U]; | |
76 | }; | |
77 | ||
78 | struct hidpp_report { | |
79 | u8 report_id; | |
80 | u8 device_index; | |
81 | union { | |
82 | struct fap fap; | |
83 | struct rap rap; | |
84 | u8 rawbytes[sizeof(struct fap)]; | |
85 | }; | |
86 | } __packed; | |
87 | ||
88 | struct hidpp_device { | |
89 | struct hid_device *hid_dev; | |
90 | struct mutex send_mutex; | |
91 | void *send_receive_buf; | |
92 | wait_queue_head_t wait; | |
93 | bool answer_available; | |
94 | u8 protocol_major; | |
95 | u8 protocol_minor; | |
96 | ||
97 | void *private_data; | |
98 | ||
c39e3d5f BT |
99 | struct work_struct work; |
100 | struct kfifo delayed_work_fifo; | |
101 | atomic_t connected; | |
102 | struct input_dev *delayed_input; | |
103 | ||
2f31c525 BT |
104 | unsigned long quirks; |
105 | }; | |
106 | ||
107 | ||
108 | #define HIDPP_ERROR 0x8f | |
109 | #define HIDPP_ERROR_SUCCESS 0x00 | |
110 | #define HIDPP_ERROR_INVALID_SUBID 0x01 | |
111 | #define HIDPP_ERROR_INVALID_ADRESS 0x02 | |
112 | #define HIDPP_ERROR_INVALID_VALUE 0x03 | |
113 | #define HIDPP_ERROR_CONNECT_FAIL 0x04 | |
114 | #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05 | |
115 | #define HIDPP_ERROR_ALREADY_EXISTS 0x06 | |
116 | #define HIDPP_ERROR_BUSY 0x07 | |
117 | #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08 | |
118 | #define HIDPP_ERROR_RESOURCE_ERROR 0x09 | |
119 | #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a | |
120 | #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b | |
121 | #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c | |
122 | ||
c39e3d5f BT |
123 | static void hidpp_connect_event(struct hidpp_device *hidpp_dev); |
124 | ||
2f31c525 BT |
125 | static int __hidpp_send_report(struct hid_device *hdev, |
126 | struct hidpp_report *hidpp_report) | |
127 | { | |
128 | int fields_count, ret; | |
129 | ||
130 | switch (hidpp_report->report_id) { | |
131 | case REPORT_ID_HIDPP_SHORT: | |
132 | fields_count = HIDPP_REPORT_SHORT_LENGTH; | |
133 | break; | |
134 | case REPORT_ID_HIDPP_LONG: | |
135 | fields_count = HIDPP_REPORT_LONG_LENGTH; | |
136 | break; | |
137 | default: | |
138 | return -ENODEV; | |
139 | } | |
140 | ||
141 | /* | |
142 | * set the device_index as the receiver, it will be overwritten by | |
143 | * hid_hw_request if needed | |
144 | */ | |
145 | hidpp_report->device_index = 0xff; | |
146 | ||
147 | ret = hid_hw_raw_request(hdev, hidpp_report->report_id, | |
148 | (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT, | |
149 | HID_REQ_SET_REPORT); | |
150 | ||
151 | return ret == fields_count ? 0 : -1; | |
152 | } | |
153 | ||
8c9952b2 BT |
154 | /** |
155 | * hidpp_send_message_sync() returns 0 in case of success, and something else | |
156 | * in case of a failure. | |
157 | * - If ' something else' is positive, that means that an error has been raised | |
158 | * by the protocol itself. | |
159 | * - If ' something else' is negative, that means that we had a classic error | |
160 | * (-ENOMEM, -EPIPE, etc...) | |
161 | */ | |
2f31c525 BT |
162 | static int hidpp_send_message_sync(struct hidpp_device *hidpp, |
163 | struct hidpp_report *message, | |
164 | struct hidpp_report *response) | |
165 | { | |
166 | int ret; | |
167 | ||
168 | mutex_lock(&hidpp->send_mutex); | |
169 | ||
170 | hidpp->send_receive_buf = response; | |
171 | hidpp->answer_available = false; | |
172 | ||
173 | /* | |
174 | * So that we can later validate the answer when it arrives | |
175 | * in hidpp_raw_event | |
176 | */ | |
177 | *response = *message; | |
178 | ||
179 | ret = __hidpp_send_report(hidpp->hid_dev, message); | |
180 | ||
181 | if (ret) { | |
182 | dbg_hid("__hidpp_send_report returned err: %d\n", ret); | |
183 | memset(response, 0, sizeof(struct hidpp_report)); | |
184 | goto exit; | |
185 | } | |
186 | ||
187 | if (!wait_event_timeout(hidpp->wait, hidpp->answer_available, | |
188 | 5*HZ)) { | |
189 | dbg_hid("%s:timeout waiting for response\n", __func__); | |
190 | memset(response, 0, sizeof(struct hidpp_report)); | |
191 | ret = -ETIMEDOUT; | |
192 | } | |
193 | ||
194 | if (response->report_id == REPORT_ID_HIDPP_SHORT && | |
195 | response->fap.feature_index == HIDPP_ERROR) { | |
196 | ret = response->fap.params[1]; | |
197 | dbg_hid("__hidpp_send_report got hidpp error %02X\n", ret); | |
198 | goto exit; | |
199 | } | |
200 | ||
201 | exit: | |
202 | mutex_unlock(&hidpp->send_mutex); | |
203 | return ret; | |
204 | ||
205 | } | |
206 | ||
207 | static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp, | |
208 | u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count, | |
209 | struct hidpp_report *response) | |
210 | { | |
3e7830ce | 211 | struct hidpp_report *message; |
2f31c525 BT |
212 | int ret; |
213 | ||
214 | if (param_count > sizeof(message->fap.params)) | |
215 | return -EINVAL; | |
216 | ||
3e7830ce DC |
217 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
218 | if (!message) | |
219 | return -ENOMEM; | |
2f31c525 BT |
220 | message->report_id = REPORT_ID_HIDPP_LONG; |
221 | message->fap.feature_index = feat_index; | |
222 | message->fap.funcindex_clientid = funcindex_clientid; | |
223 | memcpy(&message->fap.params, params, param_count); | |
224 | ||
225 | ret = hidpp_send_message_sync(hidpp, message, response); | |
226 | kfree(message); | |
227 | return ret; | |
228 | } | |
229 | ||
33797820 BT |
230 | static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev, |
231 | u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count, | |
232 | struct hidpp_report *response) | |
233 | { | |
3e7830ce | 234 | struct hidpp_report *message; |
33797820 BT |
235 | int ret; |
236 | ||
237 | if ((report_id != REPORT_ID_HIDPP_SHORT) && | |
238 | (report_id != REPORT_ID_HIDPP_LONG)) | |
239 | return -EINVAL; | |
240 | ||
241 | if (param_count > sizeof(message->rap.params)) | |
242 | return -EINVAL; | |
243 | ||
3e7830ce DC |
244 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
245 | if (!message) | |
246 | return -ENOMEM; | |
33797820 BT |
247 | message->report_id = report_id; |
248 | message->rap.sub_id = sub_id; | |
249 | message->rap.reg_address = reg_address; | |
250 | memcpy(&message->rap.params, params, param_count); | |
251 | ||
252 | ret = hidpp_send_message_sync(hidpp_dev, message, response); | |
253 | kfree(message); | |
254 | return ret; | |
255 | } | |
256 | ||
c39e3d5f BT |
257 | static void delayed_work_cb(struct work_struct *work) |
258 | { | |
259 | struct hidpp_device *hidpp = container_of(work, struct hidpp_device, | |
260 | work); | |
261 | hidpp_connect_event(hidpp); | |
262 | } | |
263 | ||
2f31c525 BT |
264 | static inline bool hidpp_match_answer(struct hidpp_report *question, |
265 | struct hidpp_report *answer) | |
266 | { | |
267 | return (answer->fap.feature_index == question->fap.feature_index) && | |
268 | (answer->fap.funcindex_clientid == question->fap.funcindex_clientid); | |
269 | } | |
270 | ||
271 | static inline bool hidpp_match_error(struct hidpp_report *question, | |
272 | struct hidpp_report *answer) | |
273 | { | |
274 | return (answer->fap.feature_index == HIDPP_ERROR) && | |
275 | (answer->fap.funcindex_clientid == question->fap.feature_index) && | |
276 | (answer->fap.params[0] == question->fap.funcindex_clientid); | |
277 | } | |
278 | ||
c39e3d5f BT |
279 | static inline bool hidpp_report_is_connect_event(struct hidpp_report *report) |
280 | { | |
281 | return (report->report_id == REPORT_ID_HIDPP_SHORT) && | |
282 | (report->rap.sub_id == 0x41); | |
283 | } | |
284 | ||
33797820 BT |
285 | /* -------------------------------------------------------------------------- */ |
286 | /* HIDP++ 1.0 commands */ | |
287 | /* -------------------------------------------------------------------------- */ | |
288 | ||
289 | #define HIDPP_SET_REGISTER 0x80 | |
290 | #define HIDPP_GET_REGISTER 0x81 | |
291 | #define HIDPP_SET_LONG_REGISTER 0x82 | |
292 | #define HIDPP_GET_LONG_REGISTER 0x83 | |
293 | ||
294 | #define HIDPP_REG_PAIRING_INFORMATION 0xB5 | |
295 | #define DEVICE_NAME 0x40 | |
296 | ||
297 | static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev) | |
298 | { | |
299 | struct hidpp_report response; | |
300 | int ret; | |
301 | /* hid-logitech-dj is in charge of setting the right device index */ | |
302 | u8 params[1] = { DEVICE_NAME }; | |
303 | char *name; | |
304 | int len; | |
305 | ||
306 | ret = hidpp_send_rap_command_sync(hidpp_dev, | |
307 | REPORT_ID_HIDPP_SHORT, | |
308 | HIDPP_GET_LONG_REGISTER, | |
309 | HIDPP_REG_PAIRING_INFORMATION, | |
310 | params, 1, &response); | |
311 | if (ret) | |
312 | return NULL; | |
313 | ||
314 | len = response.rap.params[1]; | |
315 | ||
316 | name = kzalloc(len + 1, GFP_KERNEL); | |
317 | if (!name) | |
318 | return NULL; | |
319 | ||
320 | memcpy(name, &response.rap.params[2], len); | |
321 | return name; | |
322 | } | |
323 | ||
2f31c525 BT |
324 | /* -------------------------------------------------------------------------- */ |
325 | /* 0x0000: Root */ | |
326 | /* -------------------------------------------------------------------------- */ | |
327 | ||
328 | #define HIDPP_PAGE_ROOT 0x0000 | |
329 | #define HIDPP_PAGE_ROOT_IDX 0x00 | |
330 | ||
331 | #define CMD_ROOT_GET_FEATURE 0x01 | |
332 | #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11 | |
333 | ||
334 | static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature, | |
335 | u8 *feature_index, u8 *feature_type) | |
336 | { | |
337 | struct hidpp_report response; | |
338 | int ret; | |
339 | u8 params[2] = { feature >> 8, feature & 0x00FF }; | |
340 | ||
341 | ret = hidpp_send_fap_command_sync(hidpp, | |
342 | HIDPP_PAGE_ROOT_IDX, | |
343 | CMD_ROOT_GET_FEATURE, | |
344 | params, 2, &response); | |
345 | if (ret) | |
346 | return ret; | |
347 | ||
348 | *feature_index = response.fap.params[0]; | |
349 | *feature_type = response.fap.params[1]; | |
350 | ||
351 | return ret; | |
352 | } | |
353 | ||
354 | static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp) | |
355 | { | |
356 | struct hidpp_report response; | |
357 | int ret; | |
358 | ||
359 | ret = hidpp_send_fap_command_sync(hidpp, | |
360 | HIDPP_PAGE_ROOT_IDX, | |
361 | CMD_ROOT_GET_PROTOCOL_VERSION, | |
362 | NULL, 0, &response); | |
363 | ||
552f12eb | 364 | if (ret == HIDPP_ERROR_INVALID_SUBID) { |
2f31c525 BT |
365 | hidpp->protocol_major = 1; |
366 | hidpp->protocol_minor = 0; | |
367 | return 0; | |
368 | } | |
369 | ||
552f12eb BT |
370 | /* the device might not be connected */ |
371 | if (ret == HIDPP_ERROR_RESOURCE_ERROR) | |
372 | return -EIO; | |
373 | ||
8c9952b2 BT |
374 | if (ret > 0) { |
375 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
376 | __func__, ret); | |
377 | return -EPROTO; | |
378 | } | |
2f31c525 | 379 | if (ret) |
8c9952b2 | 380 | return ret; |
2f31c525 BT |
381 | |
382 | hidpp->protocol_major = response.fap.params[0]; | |
383 | hidpp->protocol_minor = response.fap.params[1]; | |
384 | ||
385 | return ret; | |
386 | } | |
387 | ||
388 | static bool hidpp_is_connected(struct hidpp_device *hidpp) | |
389 | { | |
390 | int ret; | |
391 | ||
392 | ret = hidpp_root_get_protocol_version(hidpp); | |
393 | if (!ret) | |
394 | hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n", | |
395 | hidpp->protocol_major, hidpp->protocol_minor); | |
396 | return ret == 0; | |
397 | } | |
398 | ||
399 | /* -------------------------------------------------------------------------- */ | |
400 | /* 0x0005: GetDeviceNameType */ | |
401 | /* -------------------------------------------------------------------------- */ | |
402 | ||
403 | #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005 | |
404 | ||
405 | #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01 | |
406 | #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11 | |
407 | #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21 | |
408 | ||
409 | static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp, | |
410 | u8 feature_index, u8 *nameLength) | |
411 | { | |
412 | struct hidpp_report response; | |
413 | int ret; | |
414 | ||
415 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
416 | CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response); | |
417 | ||
8c9952b2 BT |
418 | if (ret > 0) { |
419 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
420 | __func__, ret); | |
421 | return -EPROTO; | |
422 | } | |
2f31c525 | 423 | if (ret) |
8c9952b2 | 424 | return ret; |
2f31c525 BT |
425 | |
426 | *nameLength = response.fap.params[0]; | |
427 | ||
428 | return ret; | |
429 | } | |
430 | ||
431 | static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp, | |
432 | u8 feature_index, u8 char_index, char *device_name, int len_buf) | |
433 | { | |
434 | struct hidpp_report response; | |
435 | int ret, i; | |
436 | int count; | |
437 | ||
438 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
439 | CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1, | |
440 | &response); | |
441 | ||
8c9952b2 BT |
442 | if (ret > 0) { |
443 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
444 | __func__, ret); | |
445 | return -EPROTO; | |
446 | } | |
2f31c525 | 447 | if (ret) |
8c9952b2 | 448 | return ret; |
2f31c525 BT |
449 | |
450 | if (response.report_id == REPORT_ID_HIDPP_LONG) | |
451 | count = HIDPP_REPORT_LONG_LENGTH - 4; | |
452 | else | |
453 | count = HIDPP_REPORT_SHORT_LENGTH - 4; | |
454 | ||
455 | if (len_buf < count) | |
456 | count = len_buf; | |
457 | ||
458 | for (i = 0; i < count; i++) | |
459 | device_name[i] = response.fap.params[i]; | |
460 | ||
461 | return count; | |
462 | } | |
463 | ||
02cc097e | 464 | static char *hidpp_get_device_name(struct hidpp_device *hidpp) |
2f31c525 BT |
465 | { |
466 | u8 feature_type; | |
467 | u8 feature_index; | |
468 | u8 __name_length; | |
469 | char *name; | |
470 | unsigned index = 0; | |
471 | int ret; | |
472 | ||
473 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE, | |
474 | &feature_index, &feature_type); | |
475 | if (ret) | |
02cc097e | 476 | return NULL; |
2f31c525 BT |
477 | |
478 | ret = hidpp_devicenametype_get_count(hidpp, feature_index, | |
479 | &__name_length); | |
480 | if (ret) | |
02cc097e | 481 | return NULL; |
2f31c525 BT |
482 | |
483 | name = kzalloc(__name_length + 1, GFP_KERNEL); | |
484 | if (!name) | |
02cc097e | 485 | return NULL; |
2f31c525 | 486 | |
1430ee73 PW |
487 | while (index < __name_length) { |
488 | ret = hidpp_devicenametype_get_device_name(hidpp, | |
2f31c525 BT |
489 | feature_index, index, name + index, |
490 | __name_length - index); | |
1430ee73 PW |
491 | if (ret <= 0) { |
492 | kfree(name); | |
493 | return NULL; | |
494 | } | |
495 | index += ret; | |
496 | } | |
2f31c525 BT |
497 | |
498 | return name; | |
2f31c525 BT |
499 | } |
500 | ||
501 | /* -------------------------------------------------------------------------- */ | |
502 | /* 0x6100: TouchPadRawXY */ | |
503 | /* -------------------------------------------------------------------------- */ | |
504 | ||
505 | #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100 | |
506 | ||
507 | #define CMD_TOUCHPAD_GET_RAW_INFO 0x01 | |
586bdc4e BT |
508 | #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21 |
509 | ||
510 | #define EVENT_TOUCHPAD_RAW_XY 0x00 | |
2f31c525 BT |
511 | |
512 | #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01 | |
513 | #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03 | |
514 | ||
515 | struct hidpp_touchpad_raw_info { | |
516 | u16 x_size; | |
517 | u16 y_size; | |
518 | u8 z_range; | |
519 | u8 area_range; | |
520 | u8 timestamp_unit; | |
521 | u8 maxcontacts; | |
522 | u8 origin; | |
523 | u16 res; | |
524 | }; | |
525 | ||
526 | struct hidpp_touchpad_raw_xy_finger { | |
527 | u8 contact_type; | |
528 | u8 contact_status; | |
529 | u16 x; | |
530 | u16 y; | |
531 | u8 z; | |
532 | u8 area; | |
533 | u8 finger_id; | |
534 | }; | |
535 | ||
536 | struct hidpp_touchpad_raw_xy { | |
537 | u16 timestamp; | |
538 | struct hidpp_touchpad_raw_xy_finger fingers[2]; | |
539 | u8 spurious_flag; | |
540 | u8 end_of_frame; | |
541 | u8 finger_count; | |
542 | u8 button; | |
543 | }; | |
544 | ||
545 | static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp, | |
546 | u8 feature_index, struct hidpp_touchpad_raw_info *raw_info) | |
547 | { | |
548 | struct hidpp_report response; | |
549 | int ret; | |
550 | u8 *params = (u8 *)response.fap.params; | |
551 | ||
552 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
553 | CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response); | |
554 | ||
8c9952b2 BT |
555 | if (ret > 0) { |
556 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
557 | __func__, ret); | |
558 | return -EPROTO; | |
559 | } | |
2f31c525 | 560 | if (ret) |
8c9952b2 | 561 | return ret; |
2f31c525 BT |
562 | |
563 | raw_info->x_size = get_unaligned_be16(¶ms[0]); | |
564 | raw_info->y_size = get_unaligned_be16(¶ms[2]); | |
565 | raw_info->z_range = params[4]; | |
566 | raw_info->area_range = params[5]; | |
567 | raw_info->maxcontacts = params[7]; | |
568 | raw_info->origin = params[8]; | |
569 | /* res is given in unit per inch */ | |
570 | raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51; | |
571 | ||
572 | return ret; | |
573 | } | |
574 | ||
586bdc4e BT |
575 | static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev, |
576 | u8 feature_index, bool send_raw_reports, | |
577 | bool sensor_enhanced_settings) | |
578 | { | |
579 | struct hidpp_report response; | |
580 | ||
581 | /* | |
582 | * Params: | |
583 | * bit 0 - enable raw | |
584 | * bit 1 - 16bit Z, no area | |
585 | * bit 2 - enhanced sensitivity | |
586 | * bit 3 - width, height (4 bits each) instead of area | |
587 | * bit 4 - send raw + gestures (degrades smoothness) | |
588 | * remaining bits - reserved | |
589 | */ | |
590 | u8 params = send_raw_reports | (sensor_enhanced_settings << 2); | |
591 | ||
592 | return hidpp_send_fap_command_sync(hidpp_dev, feature_index, | |
593 | CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response); | |
594 | } | |
595 | ||
596 | static void hidpp_touchpad_touch_event(u8 *data, | |
597 | struct hidpp_touchpad_raw_xy_finger *finger) | |
598 | { | |
599 | u8 x_m = data[0] << 2; | |
600 | u8 y_m = data[2] << 2; | |
601 | ||
602 | finger->x = x_m << 6 | data[1]; | |
603 | finger->y = y_m << 6 | data[3]; | |
604 | ||
605 | finger->contact_type = data[0] >> 6; | |
606 | finger->contact_status = data[2] >> 6; | |
607 | ||
608 | finger->z = data[4]; | |
609 | finger->area = data[5]; | |
610 | finger->finger_id = data[6] >> 4; | |
611 | } | |
612 | ||
613 | static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev, | |
614 | u8 *data, struct hidpp_touchpad_raw_xy *raw_xy) | |
615 | { | |
616 | memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy)); | |
617 | raw_xy->end_of_frame = data[8] & 0x01; | |
618 | raw_xy->spurious_flag = (data[8] >> 1) & 0x01; | |
619 | raw_xy->finger_count = data[15] & 0x0f; | |
620 | raw_xy->button = (data[8] >> 2) & 0x01; | |
621 | ||
622 | if (raw_xy->finger_count) { | |
623 | hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]); | |
624 | hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]); | |
625 | } | |
626 | } | |
627 | ||
2f31c525 BT |
628 | /* ************************************************************************** */ |
629 | /* */ | |
630 | /* Device Support */ | |
631 | /* */ | |
632 | /* ************************************************************************** */ | |
633 | ||
634 | /* -------------------------------------------------------------------------- */ | |
635 | /* Touchpad HID++ devices */ | |
636 | /* -------------------------------------------------------------------------- */ | |
637 | ||
57ac86cf BT |
638 | #define WTP_MANUAL_RESOLUTION 39 |
639 | ||
2f31c525 BT |
640 | struct wtp_data { |
641 | struct input_dev *input; | |
642 | u16 x_size, y_size; | |
643 | u8 finger_count; | |
644 | u8 mt_feature_index; | |
645 | u8 button_feature_index; | |
646 | u8 maxcontacts; | |
647 | bool flip_y; | |
648 | unsigned int resolution; | |
649 | }; | |
650 | ||
651 | static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
652 | struct hid_field *field, struct hid_usage *usage, | |
653 | unsigned long **bit, int *max) | |
654 | { | |
3a61e975 BT |
655 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
656 | ||
657 | if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) && | |
658 | (field->application == HID_GD_KEYBOARD)) | |
659 | return 0; | |
660 | ||
2f31c525 BT |
661 | return -1; |
662 | } | |
663 | ||
c39e3d5f BT |
664 | static void wtp_populate_input(struct hidpp_device *hidpp, |
665 | struct input_dev *input_dev, bool origin_is_hid_core) | |
2f31c525 | 666 | { |
2f31c525 | 667 | struct wtp_data *wd = hidpp->private_data; |
2f31c525 | 668 | |
3a61e975 BT |
669 | if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) && origin_is_hid_core) |
670 | /* this is the generic hid-input call */ | |
671 | return; | |
672 | ||
2f31c525 BT |
673 | __set_bit(EV_ABS, input_dev->evbit); |
674 | __set_bit(EV_KEY, input_dev->evbit); | |
675 | __clear_bit(EV_REL, input_dev->evbit); | |
676 | __clear_bit(EV_LED, input_dev->evbit); | |
677 | ||
678 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0); | |
679 | input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution); | |
680 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0); | |
681 | input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution); | |
682 | ||
683 | /* Max pressure is not given by the devices, pick one */ | |
684 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0); | |
685 | ||
686 | input_set_capability(input_dev, EV_KEY, BTN_LEFT); | |
687 | ||
57ac86cf BT |
688 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) |
689 | input_set_capability(input_dev, EV_KEY, BTN_RIGHT); | |
690 | else | |
691 | __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); | |
2f31c525 BT |
692 | |
693 | input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER | | |
694 | INPUT_MT_DROP_UNUSED); | |
695 | ||
696 | wd->input = input_dev; | |
697 | } | |
698 | ||
699 | static void wtp_touch_event(struct wtp_data *wd, | |
700 | struct hidpp_touchpad_raw_xy_finger *touch_report) | |
701 | { | |
702 | int slot; | |
703 | ||
704 | if (!touch_report->finger_id || touch_report->contact_type) | |
705 | /* no actual data */ | |
706 | return; | |
707 | ||
708 | slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id); | |
709 | ||
710 | input_mt_slot(wd->input, slot); | |
711 | input_mt_report_slot_state(wd->input, MT_TOOL_FINGER, | |
712 | touch_report->contact_status); | |
713 | if (touch_report->contact_status) { | |
714 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_X, | |
715 | touch_report->x); | |
716 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y, | |
717 | wd->flip_y ? wd->y_size - touch_report->y : | |
718 | touch_report->y); | |
719 | input_event(wd->input, EV_ABS, ABS_MT_PRESSURE, | |
720 | touch_report->area); | |
721 | } | |
722 | } | |
723 | ||
724 | static void wtp_send_raw_xy_event(struct hidpp_device *hidpp, | |
725 | struct hidpp_touchpad_raw_xy *raw) | |
726 | { | |
727 | struct wtp_data *wd = hidpp->private_data; | |
728 | int i; | |
729 | ||
730 | for (i = 0; i < 2; i++) | |
731 | wtp_touch_event(wd, &(raw->fingers[i])); | |
732 | ||
57ac86cf BT |
733 | if (raw->end_of_frame && |
734 | !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)) | |
2f31c525 BT |
735 | input_event(wd->input, EV_KEY, BTN_LEFT, raw->button); |
736 | ||
737 | if (raw->end_of_frame || raw->finger_count <= 2) { | |
738 | input_mt_sync_frame(wd->input); | |
739 | input_sync(wd->input); | |
740 | } | |
741 | } | |
742 | ||
743 | static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data) | |
744 | { | |
745 | struct wtp_data *wd = hidpp->private_data; | |
746 | u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) + | |
747 | (data[7] >> 4) * (data[7] >> 4)) / 2; | |
748 | u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) + | |
749 | (data[13] >> 4) * (data[13] >> 4)) / 2; | |
750 | struct hidpp_touchpad_raw_xy raw = { | |
751 | .timestamp = data[1], | |
752 | .fingers = { | |
753 | { | |
754 | .contact_type = 0, | |
755 | .contact_status = !!data[7], | |
756 | .x = get_unaligned_le16(&data[3]), | |
757 | .y = get_unaligned_le16(&data[5]), | |
758 | .z = c1_area, | |
759 | .area = c1_area, | |
760 | .finger_id = data[2], | |
761 | }, { | |
762 | .contact_type = 0, | |
763 | .contact_status = !!data[13], | |
764 | .x = get_unaligned_le16(&data[9]), | |
765 | .y = get_unaligned_le16(&data[11]), | |
766 | .z = c2_area, | |
767 | .area = c2_area, | |
768 | .finger_id = data[8], | |
769 | } | |
770 | }, | |
771 | .finger_count = wd->maxcontacts, | |
772 | .spurious_flag = 0, | |
773 | .end_of_frame = (data[0] >> 7) == 0, | |
774 | .button = data[0] & 0x01, | |
775 | }; | |
776 | ||
777 | wtp_send_raw_xy_event(hidpp, &raw); | |
778 | ||
779 | return 1; | |
780 | } | |
781 | ||
782 | static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) | |
783 | { | |
784 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
785 | struct wtp_data *wd = hidpp->private_data; | |
586bdc4e BT |
786 | struct hidpp_report *report = (struct hidpp_report *)data; |
787 | struct hidpp_touchpad_raw_xy raw; | |
2f31c525 | 788 | |
586bdc4e | 789 | if (!wd || !wd->input) |
2f31c525 BT |
790 | return 1; |
791 | ||
586bdc4e BT |
792 | switch (data[0]) { |
793 | case 0x02: | |
57ac86cf BT |
794 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) { |
795 | input_event(wd->input, EV_KEY, BTN_LEFT, | |
796 | !!(data[1] & 0x01)); | |
797 | input_event(wd->input, EV_KEY, BTN_RIGHT, | |
798 | !!(data[1] & 0x02)); | |
799 | input_sync(wd->input); | |
800 | } else { | |
801 | if (size < 21) | |
802 | return 1; | |
803 | return wtp_mouse_raw_xy_event(hidpp, &data[7]); | |
804 | } | |
586bdc4e BT |
805 | case REPORT_ID_HIDPP_LONG: |
806 | if ((report->fap.feature_index != wd->mt_feature_index) || | |
807 | (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY)) | |
808 | return 1; | |
809 | hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw); | |
810 | ||
811 | wtp_send_raw_xy_event(hidpp, &raw); | |
812 | return 0; | |
813 | } | |
814 | ||
815 | return 0; | |
2f31c525 BT |
816 | } |
817 | ||
818 | static int wtp_get_config(struct hidpp_device *hidpp) | |
819 | { | |
820 | struct wtp_data *wd = hidpp->private_data; | |
821 | struct hidpp_touchpad_raw_info raw_info = {0}; | |
822 | u8 feature_type; | |
823 | int ret; | |
824 | ||
825 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY, | |
826 | &wd->mt_feature_index, &feature_type); | |
827 | if (ret) | |
828 | /* means that the device is not powered up */ | |
829 | return ret; | |
830 | ||
831 | ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index, | |
832 | &raw_info); | |
833 | if (ret) | |
834 | return ret; | |
835 | ||
836 | wd->x_size = raw_info.x_size; | |
837 | wd->y_size = raw_info.y_size; | |
838 | wd->maxcontacts = raw_info.maxcontacts; | |
839 | wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT; | |
840 | wd->resolution = raw_info.res; | |
57ac86cf BT |
841 | if (!wd->resolution) |
842 | wd->resolution = WTP_MANUAL_RESOLUTION; | |
2f31c525 BT |
843 | |
844 | return 0; | |
845 | } | |
846 | ||
847 | static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id) | |
848 | { | |
849 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
850 | struct wtp_data *wd; | |
851 | ||
852 | wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data), | |
853 | GFP_KERNEL); | |
854 | if (!wd) | |
855 | return -ENOMEM; | |
856 | ||
857 | hidpp->private_data = wd; | |
858 | ||
859 | return 0; | |
860 | }; | |
861 | ||
586bdc4e BT |
862 | static void wtp_connect(struct hid_device *hdev, bool connected) |
863 | { | |
864 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
865 | struct wtp_data *wd = hidpp->private_data; | |
866 | int ret; | |
867 | ||
868 | if (!connected) | |
869 | return; | |
870 | ||
871 | if (!wd->x_size) { | |
872 | ret = wtp_get_config(hidpp); | |
873 | if (ret) { | |
874 | hid_err(hdev, "Can not get wtp config: %d\n", ret); | |
875 | return; | |
876 | } | |
877 | } | |
878 | ||
879 | hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index, | |
880 | true, true); | |
881 | } | |
882 | ||
2f31c525 BT |
883 | /* -------------------------------------------------------------------------- */ |
884 | /* Generic HID++ devices */ | |
885 | /* -------------------------------------------------------------------------- */ | |
886 | ||
887 | static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
888 | struct hid_field *field, struct hid_usage *usage, | |
889 | unsigned long **bit, int *max) | |
890 | { | |
891 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
892 | ||
893 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) | |
894 | return wtp_input_mapping(hdev, hi, field, usage, bit, max); | |
895 | ||
896 | return 0; | |
897 | } | |
898 | ||
c39e3d5f BT |
899 | static void hidpp_populate_input(struct hidpp_device *hidpp, |
900 | struct input_dev *input, bool origin_is_hid_core) | |
901 | { | |
902 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) | |
903 | wtp_populate_input(hidpp, input, origin_is_hid_core); | |
904 | } | |
905 | ||
2f31c525 BT |
906 | static void hidpp_input_configured(struct hid_device *hdev, |
907 | struct hid_input *hidinput) | |
908 | { | |
909 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
c39e3d5f | 910 | struct input_dev *input = hidinput->input; |
2f31c525 | 911 | |
c39e3d5f | 912 | hidpp_populate_input(hidpp, input, true); |
2f31c525 BT |
913 | } |
914 | ||
915 | static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, | |
916 | int size) | |
917 | { | |
918 | struct hidpp_report *question = hidpp->send_receive_buf; | |
919 | struct hidpp_report *answer = hidpp->send_receive_buf; | |
920 | struct hidpp_report *report = (struct hidpp_report *)data; | |
921 | ||
922 | /* | |
923 | * If the mutex is locked then we have a pending answer from a | |
924 | * previoulsly sent command | |
925 | */ | |
926 | if (unlikely(mutex_is_locked(&hidpp->send_mutex))) { | |
927 | /* | |
928 | * Check for a correct hidpp20 answer or the corresponding | |
929 | * error | |
930 | */ | |
931 | if (hidpp_match_answer(question, report) || | |
932 | hidpp_match_error(question, report)) { | |
933 | *answer = *report; | |
934 | hidpp->answer_available = true; | |
935 | wake_up(&hidpp->wait); | |
936 | /* | |
937 | * This was an answer to a command that this driver sent | |
938 | * We return 1 to hid-core to avoid forwarding the | |
939 | * command upstream as it has been treated by the driver | |
940 | */ | |
941 | ||
942 | return 1; | |
943 | } | |
944 | } | |
945 | ||
c39e3d5f BT |
946 | if (unlikely(hidpp_report_is_connect_event(report))) { |
947 | atomic_set(&hidpp->connected, | |
948 | !(report->rap.params[0] & (1 << 6))); | |
949 | if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) && | |
950 | (schedule_work(&hidpp->work) == 0)) | |
951 | dbg_hid("%s: connect event already queued\n", __func__); | |
952 | return 1; | |
953 | } | |
954 | ||
2f31c525 BT |
955 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
956 | return wtp_raw_event(hidpp->hid_dev, data, size); | |
957 | ||
958 | return 0; | |
959 | } | |
960 | ||
961 | static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report, | |
962 | u8 *data, int size) | |
963 | { | |
964 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
965 | ||
966 | switch (data[0]) { | |
967 | case REPORT_ID_HIDPP_LONG: | |
968 | if (size != HIDPP_REPORT_LONG_LENGTH) { | |
969 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
970 | size); | |
971 | return 1; | |
972 | } | |
973 | return hidpp_raw_hidpp_event(hidpp, data, size); | |
974 | case REPORT_ID_HIDPP_SHORT: | |
975 | if (size != HIDPP_REPORT_SHORT_LENGTH) { | |
976 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
977 | size); | |
978 | return 1; | |
979 | } | |
980 | return hidpp_raw_hidpp_event(hidpp, data, size); | |
981 | } | |
982 | ||
983 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) | |
984 | return wtp_raw_event(hdev, data, size); | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
33797820 | 989 | static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying) |
2f31c525 BT |
990 | { |
991 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
992 | char *name; | |
2f31c525 | 993 | |
33797820 BT |
994 | if (use_unifying) |
995 | /* | |
996 | * the device is connected through an Unifying receiver, and | |
997 | * might not be already connected. | |
998 | * Ask the receiver for its name. | |
999 | */ | |
1000 | name = hidpp_get_unifying_name(hidpp); | |
1001 | else | |
02cc097e | 1002 | name = hidpp_get_device_name(hidpp); |
2f31c525 BT |
1003 | |
1004 | if (!name) | |
1005 | hid_err(hdev, "unable to retrieve the name of the device"); | |
1006 | else | |
1007 | snprintf(hdev->name, sizeof(hdev->name), "%s", name); | |
1008 | ||
1009 | kfree(name); | |
1010 | } | |
1011 | ||
c39e3d5f BT |
1012 | static int hidpp_input_open(struct input_dev *dev) |
1013 | { | |
1014 | struct hid_device *hid = input_get_drvdata(dev); | |
1015 | ||
1016 | return hid_hw_open(hid); | |
1017 | } | |
1018 | ||
1019 | static void hidpp_input_close(struct input_dev *dev) | |
1020 | { | |
1021 | struct hid_device *hid = input_get_drvdata(dev); | |
1022 | ||
1023 | hid_hw_close(hid); | |
1024 | } | |
1025 | ||
1026 | static struct input_dev *hidpp_allocate_input(struct hid_device *hdev) | |
1027 | { | |
1028 | struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev); | |
1029 | ||
1030 | if (!input_dev) | |
1031 | return NULL; | |
1032 | ||
1033 | input_set_drvdata(input_dev, hdev); | |
1034 | input_dev->open = hidpp_input_open; | |
1035 | input_dev->close = hidpp_input_close; | |
1036 | ||
1037 | input_dev->name = hdev->name; | |
1038 | input_dev->phys = hdev->phys; | |
1039 | input_dev->uniq = hdev->uniq; | |
1040 | input_dev->id.bustype = hdev->bus; | |
1041 | input_dev->id.vendor = hdev->vendor; | |
1042 | input_dev->id.product = hdev->product; | |
1043 | input_dev->id.version = hdev->version; | |
1044 | input_dev->dev.parent = &hdev->dev; | |
1045 | ||
1046 | return input_dev; | |
1047 | } | |
1048 | ||
1049 | static void hidpp_connect_event(struct hidpp_device *hidpp) | |
1050 | { | |
1051 | struct hid_device *hdev = hidpp->hid_dev; | |
1052 | int ret = 0; | |
1053 | bool connected = atomic_read(&hidpp->connected); | |
1054 | struct input_dev *input; | |
1055 | char *name, *devm_name; | |
c39e3d5f | 1056 | |
586bdc4e BT |
1057 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
1058 | wtp_connect(hdev, connected); | |
1059 | ||
c39e3d5f BT |
1060 | if (!connected || hidpp->delayed_input) |
1061 | return; | |
1062 | ||
1063 | if (!hidpp->protocol_major) { | |
1064 | ret = !hidpp_is_connected(hidpp); | |
1065 | if (ret) { | |
1066 | hid_err(hdev, "Can not get the protocol version.\n"); | |
1067 | return; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | /* the device is already connected, we can ask for its name and | |
1072 | * protocol */ | |
1073 | hid_info(hdev, "HID++ %u.%u device connected.\n", | |
1074 | hidpp->protocol_major, hidpp->protocol_minor); | |
1075 | ||
1076 | input = hidpp_allocate_input(hdev); | |
1077 | if (!input) { | |
1078 | hid_err(hdev, "cannot allocate new input device: %d\n", ret); | |
1079 | return; | |
1080 | } | |
1081 | ||
02cc097e | 1082 | name = hidpp_get_device_name(hidpp); |
c39e3d5f BT |
1083 | if (!name) { |
1084 | hid_err(hdev, "unable to retrieve the name of the device"); | |
1085 | } else { | |
1086 | devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name); | |
1087 | if (devm_name) | |
1088 | input->name = devm_name; | |
1089 | kfree(name); | |
1090 | } | |
1091 | ||
1092 | hidpp_populate_input(hidpp, input, false); | |
1093 | ||
1094 | ret = input_register_device(input); | |
1095 | if (ret) | |
1096 | input_free_device(input); | |
1097 | ||
1098 | hidpp->delayed_input = input; | |
1099 | } | |
1100 | ||
2f31c525 BT |
1101 | static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) |
1102 | { | |
1103 | struct hidpp_device *hidpp; | |
1104 | int ret; | |
1105 | bool connected; | |
c39e3d5f | 1106 | unsigned int connect_mask = HID_CONNECT_DEFAULT; |
2f31c525 BT |
1107 | |
1108 | hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device), | |
1109 | GFP_KERNEL); | |
1110 | if (!hidpp) | |
1111 | return -ENOMEM; | |
1112 | ||
1113 | hidpp->hid_dev = hdev; | |
1114 | hid_set_drvdata(hdev, hidpp); | |
1115 | ||
1116 | hidpp->quirks = id->driver_data; | |
1117 | ||
1118 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { | |
1119 | ret = wtp_allocate(hdev, id); | |
1120 | if (ret) | |
1121 | return ret; | |
1122 | } | |
1123 | ||
c39e3d5f | 1124 | INIT_WORK(&hidpp->work, delayed_work_cb); |
2f31c525 BT |
1125 | mutex_init(&hidpp->send_mutex); |
1126 | init_waitqueue_head(&hidpp->wait); | |
1127 | ||
1128 | ret = hid_parse(hdev); | |
1129 | if (ret) { | |
1130 | hid_err(hdev, "%s:parse failed\n", __func__); | |
1131 | goto hid_parse_fail; | |
1132 | } | |
1133 | ||
1134 | /* Allow incoming packets */ | |
1135 | hid_device_io_start(hdev); | |
1136 | ||
1137 | connected = hidpp_is_connected(hidpp); | |
ab94e562 BT |
1138 | if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) { |
1139 | if (!connected) { | |
1140 | hid_err(hdev, "Device not connected"); | |
1141 | goto hid_parse_fail; | |
1142 | } | |
2f31c525 | 1143 | |
ab94e562 BT |
1144 | hid_info(hdev, "HID++ %u.%u device connected.\n", |
1145 | hidpp->protocol_major, hidpp->protocol_minor); | |
ab94e562 | 1146 | } |
2f31c525 | 1147 | |
33797820 | 1148 | hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE); |
c39e3d5f | 1149 | atomic_set(&hidpp->connected, connected); |
33797820 | 1150 | |
c39e3d5f | 1151 | if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) { |
2f31c525 BT |
1152 | ret = wtp_get_config(hidpp); |
1153 | if (ret) | |
1154 | goto hid_parse_fail; | |
1155 | } | |
1156 | ||
1157 | /* Block incoming packets */ | |
1158 | hid_device_io_stop(hdev); | |
1159 | ||
c39e3d5f BT |
1160 | if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) |
1161 | connect_mask &= ~HID_CONNECT_HIDINPUT; | |
1162 | ||
3a61e975 BT |
1163 | /* Re-enable hidinput for multi-input devices */ |
1164 | if (hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) | |
1165 | connect_mask |= HID_CONNECT_HIDINPUT; | |
1166 | ||
c39e3d5f | 1167 | ret = hid_hw_start(hdev, connect_mask); |
2f31c525 BT |
1168 | if (ret) { |
1169 | hid_err(hdev, "%s:hid_hw_start returned error\n", __func__); | |
1170 | goto hid_hw_start_fail; | |
1171 | } | |
1172 | ||
c39e3d5f BT |
1173 | if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) { |
1174 | /* Allow incoming packets */ | |
1175 | hid_device_io_start(hdev); | |
1176 | ||
1177 | hidpp_connect_event(hidpp); | |
1178 | } | |
1179 | ||
2f31c525 BT |
1180 | return ret; |
1181 | ||
1182 | hid_hw_start_fail: | |
1183 | hid_parse_fail: | |
c39e3d5f | 1184 | cancel_work_sync(&hidpp->work); |
2f31c525 BT |
1185 | mutex_destroy(&hidpp->send_mutex); |
1186 | hid_set_drvdata(hdev, NULL); | |
1187 | return ret; | |
1188 | } | |
1189 | ||
1190 | static void hidpp_remove(struct hid_device *hdev) | |
1191 | { | |
1192 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1193 | ||
c39e3d5f | 1194 | cancel_work_sync(&hidpp->work); |
2f31c525 BT |
1195 | mutex_destroy(&hidpp->send_mutex); |
1196 | hid_hw_stop(hdev); | |
1197 | } | |
1198 | ||
1199 | static const struct hid_device_id hidpp_devices[] = { | |
57ac86cf BT |
1200 | { /* wireless touchpad */ |
1201 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1202 | USB_VENDOR_ID_LOGITECH, 0x4011), | |
1203 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT | | |
1204 | HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS }, | |
586bdc4e BT |
1205 | { /* wireless touchpad T650 */ |
1206 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1207 | USB_VENDOR_ID_LOGITECH, 0x4101), | |
1208 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT }, | |
2f31c525 BT |
1209 | { /* wireless touchpad T651 */ |
1210 | HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, | |
1211 | USB_DEVICE_ID_LOGITECH_T651), | |
1212 | .driver_data = HIDPP_QUIRK_CLASS_WTP }, | |
3a61e975 BT |
1213 | { /* Keyboard TK820 */ |
1214 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1215 | USB_VENDOR_ID_LOGITECH, 0x4102), | |
1216 | .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_MULTI_INPUT | | |
1217 | HIDPP_QUIRK_CLASS_WTP }, | |
ab94e562 BT |
1218 | |
1219 | { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1220 | USB_VENDOR_ID_LOGITECH, HID_ANY_ID)}, | |
2f31c525 BT |
1221 | {} |
1222 | }; | |
1223 | ||
1224 | MODULE_DEVICE_TABLE(hid, hidpp_devices); | |
1225 | ||
1226 | static struct hid_driver hidpp_driver = { | |
1227 | .name = "logitech-hidpp-device", | |
1228 | .id_table = hidpp_devices, | |
1229 | .probe = hidpp_probe, | |
1230 | .remove = hidpp_remove, | |
1231 | .raw_event = hidpp_raw_event, | |
1232 | .input_configured = hidpp_input_configured, | |
1233 | .input_mapping = hidpp_input_mapping, | |
1234 | }; | |
1235 | ||
1236 | module_hid_driver(hidpp_driver); |