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 | ||
9188dbae BT |
31 | static bool disable_raw_mode; |
32 | module_param(disable_raw_mode, bool, 0644); | |
33 | MODULE_PARM_DESC(disable_raw_mode, | |
34 | "Disable Raw mode reporting for touchpads and keep firmware gestures."); | |
35 | ||
90cdd986 BT |
36 | static bool disable_tap_to_click; |
37 | module_param(disable_tap_to_click, bool, 0644); | |
38 | MODULE_PARM_DESC(disable_tap_to_click, | |
39 | "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently)."); | |
40 | ||
2f31c525 BT |
41 | #define REPORT_ID_HIDPP_SHORT 0x10 |
42 | #define REPORT_ID_HIDPP_LONG 0x11 | |
a5ce8f5b | 43 | #define REPORT_ID_HIDPP_VERY_LONG 0x12 |
2f31c525 BT |
44 | |
45 | #define HIDPP_REPORT_SHORT_LENGTH 7 | |
46 | #define HIDPP_REPORT_LONG_LENGTH 20 | |
a5ce8f5b | 47 | #define HIDPP_REPORT_VERY_LONG_LENGTH 64 |
2f31c525 BT |
48 | |
49 | #define HIDPP_QUIRK_CLASS_WTP BIT(0) | |
8a09b4fa | 50 | #define HIDPP_QUIRK_CLASS_M560 BIT(1) |
90cdd986 | 51 | #define HIDPP_QUIRK_CLASS_K400 BIT(2) |
7bfd2927 | 52 | #define HIDPP_QUIRK_CLASS_G920 BIT(3) |
2f31c525 | 53 | |
8a09b4fa | 54 | /* bits 2..20 are reserved for classes */ |
580a7e82 | 55 | #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) |
57ac86cf | 56 | #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22) |
580a7e82 | 57 | #define HIDPP_QUIRK_NO_HIDINPUT BIT(23) |
7bfd2927 | 58 | #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24) |
580a7e82 BT |
59 | |
60 | #define HIDPP_QUIRK_DELAYED_INIT (HIDPP_QUIRK_NO_HIDINPUT | \ | |
61 | HIDPP_QUIRK_CONNECT_EVENTS) | |
c39e3d5f | 62 | |
2f31c525 BT |
63 | /* |
64 | * There are two hidpp protocols in use, the first version hidpp10 is known | |
65 | * as register access protocol or RAP, the second version hidpp20 is known as | |
66 | * feature access protocol or FAP | |
67 | * | |
68 | * Most older devices (including the Unifying usb receiver) use the RAP protocol | |
69 | * where as most newer devices use the FAP protocol. Both protocols are | |
70 | * compatible with the underlying transport, which could be usb, Unifiying, or | |
71 | * bluetooth. The message lengths are defined by the hid vendor specific report | |
72 | * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and | |
73 | * the HIDPP_LONG report type (total message length 20 bytes) | |
74 | * | |
75 | * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG | |
76 | * messages. The Unifying receiver itself responds to RAP messages (device index | |
77 | * is 0xFF for the receiver), and all messages (short or long) with a device | |
78 | * index between 1 and 6 are passed untouched to the corresponding paired | |
79 | * Unifying device. | |
80 | * | |
81 | * The paired device can be RAP or FAP, it will receive the message untouched | |
82 | * from the Unifiying receiver. | |
83 | */ | |
84 | ||
85 | struct fap { | |
86 | u8 feature_index; | |
87 | u8 funcindex_clientid; | |
a5ce8f5b | 88 | u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U]; |
2f31c525 BT |
89 | }; |
90 | ||
91 | struct rap { | |
92 | u8 sub_id; | |
93 | u8 reg_address; | |
a5ce8f5b | 94 | u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U]; |
2f31c525 BT |
95 | }; |
96 | ||
97 | struct hidpp_report { | |
98 | u8 report_id; | |
99 | u8 device_index; | |
100 | union { | |
101 | struct fap fap; | |
102 | struct rap rap; | |
103 | u8 rawbytes[sizeof(struct fap)]; | |
104 | }; | |
105 | } __packed; | |
106 | ||
107 | struct hidpp_device { | |
108 | struct hid_device *hid_dev; | |
109 | struct mutex send_mutex; | |
110 | void *send_receive_buf; | |
005b3f57 | 111 | char *name; /* will never be NULL and should not be freed */ |
2f31c525 BT |
112 | wait_queue_head_t wait; |
113 | bool answer_available; | |
114 | u8 protocol_major; | |
115 | u8 protocol_minor; | |
116 | ||
117 | void *private_data; | |
118 | ||
c39e3d5f BT |
119 | struct work_struct work; |
120 | struct kfifo delayed_work_fifo; | |
121 | atomic_t connected; | |
122 | struct input_dev *delayed_input; | |
123 | ||
2f31c525 BT |
124 | unsigned long quirks; |
125 | }; | |
126 | ||
127 | ||
f677bb15 | 128 | /* HID++ 1.0 error codes */ |
2f31c525 BT |
129 | #define HIDPP_ERROR 0x8f |
130 | #define HIDPP_ERROR_SUCCESS 0x00 | |
131 | #define HIDPP_ERROR_INVALID_SUBID 0x01 | |
132 | #define HIDPP_ERROR_INVALID_ADRESS 0x02 | |
133 | #define HIDPP_ERROR_INVALID_VALUE 0x03 | |
134 | #define HIDPP_ERROR_CONNECT_FAIL 0x04 | |
135 | #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05 | |
136 | #define HIDPP_ERROR_ALREADY_EXISTS 0x06 | |
137 | #define HIDPP_ERROR_BUSY 0x07 | |
138 | #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08 | |
139 | #define HIDPP_ERROR_RESOURCE_ERROR 0x09 | |
140 | #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a | |
141 | #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b | |
142 | #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c | |
f677bb15 PW |
143 | /* HID++ 2.0 error codes */ |
144 | #define HIDPP20_ERROR 0xff | |
2f31c525 | 145 | |
c39e3d5f BT |
146 | static void hidpp_connect_event(struct hidpp_device *hidpp_dev); |
147 | ||
2f31c525 BT |
148 | static int __hidpp_send_report(struct hid_device *hdev, |
149 | struct hidpp_report *hidpp_report) | |
150 | { | |
7bfd2927 | 151 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
2f31c525 BT |
152 | int fields_count, ret; |
153 | ||
7bfd2927 SW |
154 | hidpp = hid_get_drvdata(hdev); |
155 | ||
2f31c525 BT |
156 | switch (hidpp_report->report_id) { |
157 | case REPORT_ID_HIDPP_SHORT: | |
158 | fields_count = HIDPP_REPORT_SHORT_LENGTH; | |
159 | break; | |
160 | case REPORT_ID_HIDPP_LONG: | |
161 | fields_count = HIDPP_REPORT_LONG_LENGTH; | |
162 | break; | |
a5ce8f5b SW |
163 | case REPORT_ID_HIDPP_VERY_LONG: |
164 | fields_count = HIDPP_REPORT_VERY_LONG_LENGTH; | |
165 | break; | |
2f31c525 BT |
166 | default: |
167 | return -ENODEV; | |
168 | } | |
169 | ||
170 | /* | |
171 | * set the device_index as the receiver, it will be overwritten by | |
172 | * hid_hw_request if needed | |
173 | */ | |
174 | hidpp_report->device_index = 0xff; | |
175 | ||
7bfd2927 SW |
176 | if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) { |
177 | ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count); | |
178 | } else { | |
179 | ret = hid_hw_raw_request(hdev, hidpp_report->report_id, | |
180 | (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT, | |
181 | HID_REQ_SET_REPORT); | |
182 | } | |
2f31c525 BT |
183 | |
184 | return ret == fields_count ? 0 : -1; | |
185 | } | |
186 | ||
8c9952b2 BT |
187 | /** |
188 | * hidpp_send_message_sync() returns 0 in case of success, and something else | |
189 | * in case of a failure. | |
190 | * - If ' something else' is positive, that means that an error has been raised | |
191 | * by the protocol itself. | |
192 | * - If ' something else' is negative, that means that we had a classic error | |
193 | * (-ENOMEM, -EPIPE, etc...) | |
194 | */ | |
2f31c525 BT |
195 | static int hidpp_send_message_sync(struct hidpp_device *hidpp, |
196 | struct hidpp_report *message, | |
197 | struct hidpp_report *response) | |
198 | { | |
199 | int ret; | |
200 | ||
201 | mutex_lock(&hidpp->send_mutex); | |
202 | ||
203 | hidpp->send_receive_buf = response; | |
204 | hidpp->answer_available = false; | |
205 | ||
206 | /* | |
207 | * So that we can later validate the answer when it arrives | |
208 | * in hidpp_raw_event | |
209 | */ | |
210 | *response = *message; | |
211 | ||
212 | ret = __hidpp_send_report(hidpp->hid_dev, message); | |
213 | ||
214 | if (ret) { | |
215 | dbg_hid("__hidpp_send_report returned err: %d\n", ret); | |
216 | memset(response, 0, sizeof(struct hidpp_report)); | |
217 | goto exit; | |
218 | } | |
219 | ||
220 | if (!wait_event_timeout(hidpp->wait, hidpp->answer_available, | |
221 | 5*HZ)) { | |
222 | dbg_hid("%s:timeout waiting for response\n", __func__); | |
223 | memset(response, 0, sizeof(struct hidpp_report)); | |
224 | ret = -ETIMEDOUT; | |
225 | } | |
226 | ||
227 | if (response->report_id == REPORT_ID_HIDPP_SHORT && | |
f677bb15 PW |
228 | response->rap.sub_id == HIDPP_ERROR) { |
229 | ret = response->rap.params[1]; | |
230 | dbg_hid("%s:got hidpp error %02X\n", __func__, ret); | |
231 | goto exit; | |
232 | } | |
233 | ||
a5ce8f5b SW |
234 | if ((response->report_id == REPORT_ID_HIDPP_LONG || |
235 | response->report_id == REPORT_ID_HIDPP_VERY_LONG) && | |
236 | response->fap.feature_index == HIDPP20_ERROR) { | |
2f31c525 | 237 | ret = response->fap.params[1]; |
f677bb15 | 238 | dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret); |
2f31c525 BT |
239 | goto exit; |
240 | } | |
241 | ||
242 | exit: | |
243 | mutex_unlock(&hidpp->send_mutex); | |
244 | return ret; | |
245 | ||
246 | } | |
247 | ||
248 | static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp, | |
249 | u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count, | |
250 | struct hidpp_report *response) | |
251 | { | |
3e7830ce | 252 | struct hidpp_report *message; |
2f31c525 BT |
253 | int ret; |
254 | ||
255 | if (param_count > sizeof(message->fap.params)) | |
256 | return -EINVAL; | |
257 | ||
3e7830ce DC |
258 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
259 | if (!message) | |
260 | return -ENOMEM; | |
a5ce8f5b SW |
261 | |
262 | if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4)) | |
263 | message->report_id = REPORT_ID_HIDPP_VERY_LONG; | |
264 | else | |
265 | message->report_id = REPORT_ID_HIDPP_LONG; | |
2f31c525 BT |
266 | message->fap.feature_index = feat_index; |
267 | message->fap.funcindex_clientid = funcindex_clientid; | |
268 | memcpy(&message->fap.params, params, param_count); | |
269 | ||
270 | ret = hidpp_send_message_sync(hidpp, message, response); | |
271 | kfree(message); | |
272 | return ret; | |
273 | } | |
274 | ||
33797820 BT |
275 | static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev, |
276 | u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count, | |
277 | struct hidpp_report *response) | |
278 | { | |
3e7830ce | 279 | struct hidpp_report *message; |
a5ce8f5b | 280 | int ret, max_count; |
33797820 | 281 | |
a5ce8f5b SW |
282 | switch (report_id) { |
283 | case REPORT_ID_HIDPP_SHORT: | |
284 | max_count = HIDPP_REPORT_SHORT_LENGTH - 4; | |
285 | break; | |
286 | case REPORT_ID_HIDPP_LONG: | |
287 | max_count = HIDPP_REPORT_LONG_LENGTH - 4; | |
288 | break; | |
289 | case REPORT_ID_HIDPP_VERY_LONG: | |
290 | max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4; | |
291 | break; | |
292 | default: | |
33797820 | 293 | return -EINVAL; |
a5ce8f5b | 294 | } |
33797820 | 295 | |
a5ce8f5b | 296 | if (param_count > max_count) |
33797820 BT |
297 | return -EINVAL; |
298 | ||
3e7830ce DC |
299 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
300 | if (!message) | |
301 | return -ENOMEM; | |
33797820 BT |
302 | message->report_id = report_id; |
303 | message->rap.sub_id = sub_id; | |
304 | message->rap.reg_address = reg_address; | |
305 | memcpy(&message->rap.params, params, param_count); | |
306 | ||
307 | ret = hidpp_send_message_sync(hidpp_dev, message, response); | |
308 | kfree(message); | |
309 | return ret; | |
310 | } | |
311 | ||
c39e3d5f BT |
312 | static void delayed_work_cb(struct work_struct *work) |
313 | { | |
314 | struct hidpp_device *hidpp = container_of(work, struct hidpp_device, | |
315 | work); | |
316 | hidpp_connect_event(hidpp); | |
317 | } | |
318 | ||
2f31c525 BT |
319 | static inline bool hidpp_match_answer(struct hidpp_report *question, |
320 | struct hidpp_report *answer) | |
321 | { | |
322 | return (answer->fap.feature_index == question->fap.feature_index) && | |
323 | (answer->fap.funcindex_clientid == question->fap.funcindex_clientid); | |
324 | } | |
325 | ||
326 | static inline bool hidpp_match_error(struct hidpp_report *question, | |
327 | struct hidpp_report *answer) | |
328 | { | |
f677bb15 PW |
329 | return ((answer->rap.sub_id == HIDPP_ERROR) || |
330 | (answer->fap.feature_index == HIDPP20_ERROR)) && | |
2f31c525 BT |
331 | (answer->fap.funcindex_clientid == question->fap.feature_index) && |
332 | (answer->fap.params[0] == question->fap.funcindex_clientid); | |
333 | } | |
334 | ||
c39e3d5f BT |
335 | static inline bool hidpp_report_is_connect_event(struct hidpp_report *report) |
336 | { | |
337 | return (report->report_id == REPORT_ID_HIDPP_SHORT) && | |
338 | (report->rap.sub_id == 0x41); | |
339 | } | |
340 | ||
a0e625f8 BT |
341 | /** |
342 | * hidpp_prefix_name() prefixes the current given name with "Logitech ". | |
343 | */ | |
344 | static void hidpp_prefix_name(char **name, int name_length) | |
345 | { | |
346 | #define PREFIX_LENGTH 9 /* "Logitech " */ | |
347 | ||
348 | int new_length; | |
349 | char *new_name; | |
350 | ||
351 | if (name_length > PREFIX_LENGTH && | |
352 | strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0) | |
353 | /* The prefix has is already in the name */ | |
354 | return; | |
355 | ||
356 | new_length = PREFIX_LENGTH + name_length; | |
357 | new_name = kzalloc(new_length, GFP_KERNEL); | |
358 | if (!new_name) | |
359 | return; | |
360 | ||
361 | snprintf(new_name, new_length, "Logitech %s", *name); | |
362 | ||
363 | kfree(*name); | |
364 | ||
365 | *name = new_name; | |
366 | } | |
367 | ||
33797820 BT |
368 | /* -------------------------------------------------------------------------- */ |
369 | /* HIDP++ 1.0 commands */ | |
370 | /* -------------------------------------------------------------------------- */ | |
371 | ||
372 | #define HIDPP_SET_REGISTER 0x80 | |
373 | #define HIDPP_GET_REGISTER 0x81 | |
374 | #define HIDPP_SET_LONG_REGISTER 0x82 | |
375 | #define HIDPP_GET_LONG_REGISTER 0x83 | |
376 | ||
377 | #define HIDPP_REG_PAIRING_INFORMATION 0xB5 | |
378 | #define DEVICE_NAME 0x40 | |
379 | ||
380 | static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev) | |
381 | { | |
382 | struct hidpp_report response; | |
383 | int ret; | |
384 | /* hid-logitech-dj is in charge of setting the right device index */ | |
385 | u8 params[1] = { DEVICE_NAME }; | |
386 | char *name; | |
387 | int len; | |
388 | ||
389 | ret = hidpp_send_rap_command_sync(hidpp_dev, | |
390 | REPORT_ID_HIDPP_SHORT, | |
391 | HIDPP_GET_LONG_REGISTER, | |
392 | HIDPP_REG_PAIRING_INFORMATION, | |
393 | params, 1, &response); | |
394 | if (ret) | |
395 | return NULL; | |
396 | ||
397 | len = response.rap.params[1]; | |
398 | ||
3a034a7a PW |
399 | if (2 + len > sizeof(response.rap.params)) |
400 | return NULL; | |
401 | ||
33797820 BT |
402 | name = kzalloc(len + 1, GFP_KERNEL); |
403 | if (!name) | |
404 | return NULL; | |
405 | ||
406 | memcpy(name, &response.rap.params[2], len); | |
a0e625f8 BT |
407 | |
408 | /* include the terminating '\0' */ | |
409 | hidpp_prefix_name(&name, len + 1); | |
410 | ||
33797820 BT |
411 | return name; |
412 | } | |
413 | ||
2f31c525 BT |
414 | /* -------------------------------------------------------------------------- */ |
415 | /* 0x0000: Root */ | |
416 | /* -------------------------------------------------------------------------- */ | |
417 | ||
418 | #define HIDPP_PAGE_ROOT 0x0000 | |
419 | #define HIDPP_PAGE_ROOT_IDX 0x00 | |
420 | ||
421 | #define CMD_ROOT_GET_FEATURE 0x01 | |
422 | #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11 | |
423 | ||
424 | static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature, | |
425 | u8 *feature_index, u8 *feature_type) | |
426 | { | |
427 | struct hidpp_report response; | |
428 | int ret; | |
429 | u8 params[2] = { feature >> 8, feature & 0x00FF }; | |
430 | ||
431 | ret = hidpp_send_fap_command_sync(hidpp, | |
432 | HIDPP_PAGE_ROOT_IDX, | |
433 | CMD_ROOT_GET_FEATURE, | |
434 | params, 2, &response); | |
435 | if (ret) | |
436 | return ret; | |
437 | ||
438 | *feature_index = response.fap.params[0]; | |
439 | *feature_type = response.fap.params[1]; | |
440 | ||
441 | return ret; | |
442 | } | |
443 | ||
444 | static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp) | |
445 | { | |
446 | struct hidpp_report response; | |
447 | int ret; | |
448 | ||
449 | ret = hidpp_send_fap_command_sync(hidpp, | |
450 | HIDPP_PAGE_ROOT_IDX, | |
451 | CMD_ROOT_GET_PROTOCOL_VERSION, | |
452 | NULL, 0, &response); | |
453 | ||
552f12eb | 454 | if (ret == HIDPP_ERROR_INVALID_SUBID) { |
2f31c525 BT |
455 | hidpp->protocol_major = 1; |
456 | hidpp->protocol_minor = 0; | |
457 | return 0; | |
458 | } | |
459 | ||
552f12eb BT |
460 | /* the device might not be connected */ |
461 | if (ret == HIDPP_ERROR_RESOURCE_ERROR) | |
462 | return -EIO; | |
463 | ||
8c9952b2 BT |
464 | if (ret > 0) { |
465 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
466 | __func__, ret); | |
467 | return -EPROTO; | |
468 | } | |
2f31c525 | 469 | if (ret) |
8c9952b2 | 470 | return ret; |
2f31c525 BT |
471 | |
472 | hidpp->protocol_major = response.fap.params[0]; | |
473 | hidpp->protocol_minor = response.fap.params[1]; | |
474 | ||
475 | return ret; | |
476 | } | |
477 | ||
478 | static bool hidpp_is_connected(struct hidpp_device *hidpp) | |
479 | { | |
480 | int ret; | |
481 | ||
482 | ret = hidpp_root_get_protocol_version(hidpp); | |
483 | if (!ret) | |
484 | hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n", | |
485 | hidpp->protocol_major, hidpp->protocol_minor); | |
486 | return ret == 0; | |
487 | } | |
488 | ||
489 | /* -------------------------------------------------------------------------- */ | |
490 | /* 0x0005: GetDeviceNameType */ | |
491 | /* -------------------------------------------------------------------------- */ | |
492 | ||
493 | #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005 | |
494 | ||
495 | #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01 | |
496 | #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11 | |
497 | #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21 | |
498 | ||
499 | static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp, | |
500 | u8 feature_index, u8 *nameLength) | |
501 | { | |
502 | struct hidpp_report response; | |
503 | int ret; | |
504 | ||
505 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
506 | CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response); | |
507 | ||
8c9952b2 BT |
508 | if (ret > 0) { |
509 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
510 | __func__, ret); | |
511 | return -EPROTO; | |
512 | } | |
2f31c525 | 513 | if (ret) |
8c9952b2 | 514 | return ret; |
2f31c525 BT |
515 | |
516 | *nameLength = response.fap.params[0]; | |
517 | ||
518 | return ret; | |
519 | } | |
520 | ||
521 | static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp, | |
522 | u8 feature_index, u8 char_index, char *device_name, int len_buf) | |
523 | { | |
524 | struct hidpp_report response; | |
525 | int ret, i; | |
526 | int count; | |
527 | ||
528 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
529 | CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1, | |
530 | &response); | |
531 | ||
8c9952b2 BT |
532 | if (ret > 0) { |
533 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
534 | __func__, ret); | |
535 | return -EPROTO; | |
536 | } | |
2f31c525 | 537 | if (ret) |
8c9952b2 | 538 | return ret; |
2f31c525 | 539 | |
a5ce8f5b SW |
540 | switch (response.report_id) { |
541 | case REPORT_ID_HIDPP_VERY_LONG: | |
542 | count = HIDPP_REPORT_VERY_LONG_LENGTH - 4; | |
543 | break; | |
544 | case REPORT_ID_HIDPP_LONG: | |
2f31c525 | 545 | count = HIDPP_REPORT_LONG_LENGTH - 4; |
a5ce8f5b SW |
546 | break; |
547 | case REPORT_ID_HIDPP_SHORT: | |
2f31c525 | 548 | count = HIDPP_REPORT_SHORT_LENGTH - 4; |
a5ce8f5b SW |
549 | break; |
550 | default: | |
551 | return -EPROTO; | |
552 | } | |
2f31c525 BT |
553 | |
554 | if (len_buf < count) | |
555 | count = len_buf; | |
556 | ||
557 | for (i = 0; i < count; i++) | |
558 | device_name[i] = response.fap.params[i]; | |
559 | ||
560 | return count; | |
561 | } | |
562 | ||
02cc097e | 563 | static char *hidpp_get_device_name(struct hidpp_device *hidpp) |
2f31c525 BT |
564 | { |
565 | u8 feature_type; | |
566 | u8 feature_index; | |
567 | u8 __name_length; | |
568 | char *name; | |
569 | unsigned index = 0; | |
570 | int ret; | |
571 | ||
572 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE, | |
573 | &feature_index, &feature_type); | |
574 | if (ret) | |
02cc097e | 575 | return NULL; |
2f31c525 BT |
576 | |
577 | ret = hidpp_devicenametype_get_count(hidpp, feature_index, | |
578 | &__name_length); | |
579 | if (ret) | |
02cc097e | 580 | return NULL; |
2f31c525 BT |
581 | |
582 | name = kzalloc(__name_length + 1, GFP_KERNEL); | |
583 | if (!name) | |
02cc097e | 584 | return NULL; |
2f31c525 | 585 | |
1430ee73 PW |
586 | while (index < __name_length) { |
587 | ret = hidpp_devicenametype_get_device_name(hidpp, | |
2f31c525 BT |
588 | feature_index, index, name + index, |
589 | __name_length - index); | |
1430ee73 PW |
590 | if (ret <= 0) { |
591 | kfree(name); | |
592 | return NULL; | |
593 | } | |
594 | index += ret; | |
595 | } | |
2f31c525 | 596 | |
a0e625f8 BT |
597 | /* include the terminating '\0' */ |
598 | hidpp_prefix_name(&name, __name_length + 1); | |
599 | ||
2f31c525 | 600 | return name; |
2f31c525 BT |
601 | } |
602 | ||
90cdd986 BT |
603 | /* -------------------------------------------------------------------------- */ |
604 | /* 0x6010: Touchpad FW items */ | |
605 | /* -------------------------------------------------------------------------- */ | |
606 | ||
607 | #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010 | |
608 | ||
609 | #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10 | |
610 | ||
611 | struct hidpp_touchpad_fw_items { | |
612 | uint8_t presence; | |
613 | uint8_t desired_state; | |
614 | uint8_t state; | |
615 | uint8_t persistent; | |
616 | }; | |
617 | ||
618 | /** | |
619 | * send a set state command to the device by reading the current items->state | |
620 | * field. items is then filled with the current state. | |
621 | */ | |
622 | static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp, | |
623 | u8 feature_index, | |
624 | struct hidpp_touchpad_fw_items *items) | |
625 | { | |
626 | struct hidpp_report response; | |
627 | int ret; | |
628 | u8 *params = (u8 *)response.fap.params; | |
629 | ||
630 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
631 | CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response); | |
632 | ||
633 | if (ret > 0) { | |
634 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
635 | __func__, ret); | |
636 | return -EPROTO; | |
637 | } | |
638 | if (ret) | |
639 | return ret; | |
640 | ||
641 | items->presence = params[0]; | |
642 | items->desired_state = params[1]; | |
643 | items->state = params[2]; | |
644 | items->persistent = params[3]; | |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
2f31c525 BT |
649 | /* -------------------------------------------------------------------------- */ |
650 | /* 0x6100: TouchPadRawXY */ | |
651 | /* -------------------------------------------------------------------------- */ | |
652 | ||
653 | #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100 | |
654 | ||
655 | #define CMD_TOUCHPAD_GET_RAW_INFO 0x01 | |
586bdc4e BT |
656 | #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21 |
657 | ||
658 | #define EVENT_TOUCHPAD_RAW_XY 0x00 | |
2f31c525 BT |
659 | |
660 | #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01 | |
661 | #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03 | |
662 | ||
663 | struct hidpp_touchpad_raw_info { | |
664 | u16 x_size; | |
665 | u16 y_size; | |
666 | u8 z_range; | |
667 | u8 area_range; | |
668 | u8 timestamp_unit; | |
669 | u8 maxcontacts; | |
670 | u8 origin; | |
671 | u16 res; | |
672 | }; | |
673 | ||
674 | struct hidpp_touchpad_raw_xy_finger { | |
675 | u8 contact_type; | |
676 | u8 contact_status; | |
677 | u16 x; | |
678 | u16 y; | |
679 | u8 z; | |
680 | u8 area; | |
681 | u8 finger_id; | |
682 | }; | |
683 | ||
684 | struct hidpp_touchpad_raw_xy { | |
685 | u16 timestamp; | |
686 | struct hidpp_touchpad_raw_xy_finger fingers[2]; | |
687 | u8 spurious_flag; | |
688 | u8 end_of_frame; | |
689 | u8 finger_count; | |
690 | u8 button; | |
691 | }; | |
692 | ||
693 | static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp, | |
694 | u8 feature_index, struct hidpp_touchpad_raw_info *raw_info) | |
695 | { | |
696 | struct hidpp_report response; | |
697 | int ret; | |
698 | u8 *params = (u8 *)response.fap.params; | |
699 | ||
700 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
701 | CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response); | |
702 | ||
8c9952b2 BT |
703 | if (ret > 0) { |
704 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
705 | __func__, ret); | |
706 | return -EPROTO; | |
707 | } | |
2f31c525 | 708 | if (ret) |
8c9952b2 | 709 | return ret; |
2f31c525 BT |
710 | |
711 | raw_info->x_size = get_unaligned_be16(¶ms[0]); | |
712 | raw_info->y_size = get_unaligned_be16(¶ms[2]); | |
713 | raw_info->z_range = params[4]; | |
714 | raw_info->area_range = params[5]; | |
715 | raw_info->maxcontacts = params[7]; | |
716 | raw_info->origin = params[8]; | |
717 | /* res is given in unit per inch */ | |
718 | raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51; | |
719 | ||
720 | return ret; | |
721 | } | |
722 | ||
586bdc4e BT |
723 | static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev, |
724 | u8 feature_index, bool send_raw_reports, | |
725 | bool sensor_enhanced_settings) | |
726 | { | |
727 | struct hidpp_report response; | |
728 | ||
729 | /* | |
730 | * Params: | |
731 | * bit 0 - enable raw | |
732 | * bit 1 - 16bit Z, no area | |
733 | * bit 2 - enhanced sensitivity | |
734 | * bit 3 - width, height (4 bits each) instead of area | |
735 | * bit 4 - send raw + gestures (degrades smoothness) | |
736 | * remaining bits - reserved | |
737 | */ | |
738 | u8 params = send_raw_reports | (sensor_enhanced_settings << 2); | |
739 | ||
740 | return hidpp_send_fap_command_sync(hidpp_dev, feature_index, | |
741 | CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response); | |
742 | } | |
743 | ||
744 | static void hidpp_touchpad_touch_event(u8 *data, | |
745 | struct hidpp_touchpad_raw_xy_finger *finger) | |
746 | { | |
747 | u8 x_m = data[0] << 2; | |
748 | u8 y_m = data[2] << 2; | |
749 | ||
750 | finger->x = x_m << 6 | data[1]; | |
751 | finger->y = y_m << 6 | data[3]; | |
752 | ||
753 | finger->contact_type = data[0] >> 6; | |
754 | finger->contact_status = data[2] >> 6; | |
755 | ||
756 | finger->z = data[4]; | |
757 | finger->area = data[5]; | |
758 | finger->finger_id = data[6] >> 4; | |
759 | } | |
760 | ||
761 | static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev, | |
762 | u8 *data, struct hidpp_touchpad_raw_xy *raw_xy) | |
763 | { | |
764 | memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy)); | |
765 | raw_xy->end_of_frame = data[8] & 0x01; | |
766 | raw_xy->spurious_flag = (data[8] >> 1) & 0x01; | |
767 | raw_xy->finger_count = data[15] & 0x0f; | |
768 | raw_xy->button = (data[8] >> 2) & 0x01; | |
769 | ||
770 | if (raw_xy->finger_count) { | |
771 | hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]); | |
772 | hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]); | |
773 | } | |
774 | } | |
775 | ||
2f31c525 BT |
776 | /* ************************************************************************** */ |
777 | /* */ | |
778 | /* Device Support */ | |
779 | /* */ | |
780 | /* ************************************************************************** */ | |
781 | ||
782 | /* -------------------------------------------------------------------------- */ | |
783 | /* Touchpad HID++ devices */ | |
784 | /* -------------------------------------------------------------------------- */ | |
785 | ||
57ac86cf BT |
786 | #define WTP_MANUAL_RESOLUTION 39 |
787 | ||
2f31c525 BT |
788 | struct wtp_data { |
789 | struct input_dev *input; | |
790 | u16 x_size, y_size; | |
791 | u8 finger_count; | |
792 | u8 mt_feature_index; | |
793 | u8 button_feature_index; | |
794 | u8 maxcontacts; | |
795 | bool flip_y; | |
796 | unsigned int resolution; | |
797 | }; | |
798 | ||
799 | static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
800 | struct hid_field *field, struct hid_usage *usage, | |
801 | unsigned long **bit, int *max) | |
802 | { | |
803 | return -1; | |
804 | } | |
805 | ||
c39e3d5f BT |
806 | static void wtp_populate_input(struct hidpp_device *hidpp, |
807 | struct input_dev *input_dev, bool origin_is_hid_core) | |
2f31c525 | 808 | { |
2f31c525 | 809 | struct wtp_data *wd = hidpp->private_data; |
2f31c525 BT |
810 | |
811 | __set_bit(EV_ABS, input_dev->evbit); | |
812 | __set_bit(EV_KEY, input_dev->evbit); | |
813 | __clear_bit(EV_REL, input_dev->evbit); | |
814 | __clear_bit(EV_LED, input_dev->evbit); | |
815 | ||
816 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0); | |
817 | input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution); | |
818 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0); | |
819 | input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution); | |
820 | ||
821 | /* Max pressure is not given by the devices, pick one */ | |
822 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0); | |
823 | ||
824 | input_set_capability(input_dev, EV_KEY, BTN_LEFT); | |
825 | ||
57ac86cf BT |
826 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) |
827 | input_set_capability(input_dev, EV_KEY, BTN_RIGHT); | |
828 | else | |
829 | __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); | |
2f31c525 BT |
830 | |
831 | input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER | | |
832 | INPUT_MT_DROP_UNUSED); | |
833 | ||
834 | wd->input = input_dev; | |
835 | } | |
836 | ||
837 | static void wtp_touch_event(struct wtp_data *wd, | |
838 | struct hidpp_touchpad_raw_xy_finger *touch_report) | |
839 | { | |
840 | int slot; | |
841 | ||
842 | if (!touch_report->finger_id || touch_report->contact_type) | |
843 | /* no actual data */ | |
844 | return; | |
845 | ||
846 | slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id); | |
847 | ||
848 | input_mt_slot(wd->input, slot); | |
849 | input_mt_report_slot_state(wd->input, MT_TOOL_FINGER, | |
850 | touch_report->contact_status); | |
851 | if (touch_report->contact_status) { | |
852 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_X, | |
853 | touch_report->x); | |
854 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y, | |
855 | wd->flip_y ? wd->y_size - touch_report->y : | |
856 | touch_report->y); | |
857 | input_event(wd->input, EV_ABS, ABS_MT_PRESSURE, | |
858 | touch_report->area); | |
859 | } | |
860 | } | |
861 | ||
862 | static void wtp_send_raw_xy_event(struct hidpp_device *hidpp, | |
863 | struct hidpp_touchpad_raw_xy *raw) | |
864 | { | |
865 | struct wtp_data *wd = hidpp->private_data; | |
866 | int i; | |
867 | ||
868 | for (i = 0; i < 2; i++) | |
869 | wtp_touch_event(wd, &(raw->fingers[i])); | |
870 | ||
57ac86cf BT |
871 | if (raw->end_of_frame && |
872 | !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)) | |
2f31c525 BT |
873 | input_event(wd->input, EV_KEY, BTN_LEFT, raw->button); |
874 | ||
875 | if (raw->end_of_frame || raw->finger_count <= 2) { | |
876 | input_mt_sync_frame(wd->input); | |
877 | input_sync(wd->input); | |
878 | } | |
879 | } | |
880 | ||
881 | static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data) | |
882 | { | |
883 | struct wtp_data *wd = hidpp->private_data; | |
884 | u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) + | |
885 | (data[7] >> 4) * (data[7] >> 4)) / 2; | |
886 | u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) + | |
887 | (data[13] >> 4) * (data[13] >> 4)) / 2; | |
888 | struct hidpp_touchpad_raw_xy raw = { | |
889 | .timestamp = data[1], | |
890 | .fingers = { | |
891 | { | |
892 | .contact_type = 0, | |
893 | .contact_status = !!data[7], | |
894 | .x = get_unaligned_le16(&data[3]), | |
895 | .y = get_unaligned_le16(&data[5]), | |
896 | .z = c1_area, | |
897 | .area = c1_area, | |
898 | .finger_id = data[2], | |
899 | }, { | |
900 | .contact_type = 0, | |
901 | .contact_status = !!data[13], | |
902 | .x = get_unaligned_le16(&data[9]), | |
903 | .y = get_unaligned_le16(&data[11]), | |
904 | .z = c2_area, | |
905 | .area = c2_area, | |
906 | .finger_id = data[8], | |
907 | } | |
908 | }, | |
909 | .finger_count = wd->maxcontacts, | |
910 | .spurious_flag = 0, | |
911 | .end_of_frame = (data[0] >> 7) == 0, | |
912 | .button = data[0] & 0x01, | |
913 | }; | |
914 | ||
915 | wtp_send_raw_xy_event(hidpp, &raw); | |
916 | ||
917 | return 1; | |
918 | } | |
919 | ||
920 | static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) | |
921 | { | |
922 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
923 | struct wtp_data *wd = hidpp->private_data; | |
586bdc4e BT |
924 | struct hidpp_report *report = (struct hidpp_report *)data; |
925 | struct hidpp_touchpad_raw_xy raw; | |
2f31c525 | 926 | |
586bdc4e | 927 | if (!wd || !wd->input) |
2f31c525 BT |
928 | return 1; |
929 | ||
586bdc4e BT |
930 | switch (data[0]) { |
931 | case 0x02: | |
0b3f6569 PW |
932 | if (size < 2) { |
933 | hid_err(hdev, "Received HID report of bad size (%d)", | |
934 | size); | |
935 | return 1; | |
936 | } | |
57ac86cf BT |
937 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) { |
938 | input_event(wd->input, EV_KEY, BTN_LEFT, | |
939 | !!(data[1] & 0x01)); | |
940 | input_event(wd->input, EV_KEY, BTN_RIGHT, | |
941 | !!(data[1] & 0x02)); | |
942 | input_sync(wd->input); | |
8abd8205 | 943 | return 0; |
57ac86cf BT |
944 | } else { |
945 | if (size < 21) | |
946 | return 1; | |
947 | return wtp_mouse_raw_xy_event(hidpp, &data[7]); | |
948 | } | |
586bdc4e | 949 | case REPORT_ID_HIDPP_LONG: |
0b3f6569 | 950 | /* size is already checked in hidpp_raw_event. */ |
586bdc4e BT |
951 | if ((report->fap.feature_index != wd->mt_feature_index) || |
952 | (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY)) | |
953 | return 1; | |
954 | hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw); | |
955 | ||
956 | wtp_send_raw_xy_event(hidpp, &raw); | |
957 | return 0; | |
958 | } | |
959 | ||
960 | return 0; | |
2f31c525 BT |
961 | } |
962 | ||
963 | static int wtp_get_config(struct hidpp_device *hidpp) | |
964 | { | |
965 | struct wtp_data *wd = hidpp->private_data; | |
966 | struct hidpp_touchpad_raw_info raw_info = {0}; | |
967 | u8 feature_type; | |
968 | int ret; | |
969 | ||
970 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY, | |
971 | &wd->mt_feature_index, &feature_type); | |
972 | if (ret) | |
973 | /* means that the device is not powered up */ | |
974 | return ret; | |
975 | ||
976 | ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index, | |
977 | &raw_info); | |
978 | if (ret) | |
979 | return ret; | |
980 | ||
981 | wd->x_size = raw_info.x_size; | |
982 | wd->y_size = raw_info.y_size; | |
983 | wd->maxcontacts = raw_info.maxcontacts; | |
984 | wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT; | |
985 | wd->resolution = raw_info.res; | |
57ac86cf BT |
986 | if (!wd->resolution) |
987 | wd->resolution = WTP_MANUAL_RESOLUTION; | |
2f31c525 BT |
988 | |
989 | return 0; | |
990 | } | |
991 | ||
992 | static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id) | |
993 | { | |
994 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
995 | struct wtp_data *wd; | |
996 | ||
997 | wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data), | |
998 | GFP_KERNEL); | |
999 | if (!wd) | |
1000 | return -ENOMEM; | |
1001 | ||
1002 | hidpp->private_data = wd; | |
1003 | ||
1004 | return 0; | |
1005 | }; | |
1006 | ||
bf159447 | 1007 | static int wtp_connect(struct hid_device *hdev, bool connected) |
586bdc4e BT |
1008 | { |
1009 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1010 | struct wtp_data *wd = hidpp->private_data; | |
1011 | int ret; | |
1012 | ||
1013 | if (!connected) | |
bf159447 | 1014 | return 0; |
586bdc4e BT |
1015 | |
1016 | if (!wd->x_size) { | |
1017 | ret = wtp_get_config(hidpp); | |
1018 | if (ret) { | |
1019 | hid_err(hdev, "Can not get wtp config: %d\n", ret); | |
bf159447 | 1020 | return ret; |
586bdc4e BT |
1021 | } |
1022 | } | |
1023 | ||
bf159447 | 1024 | return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index, |
586bdc4e BT |
1025 | true, true); |
1026 | } | |
1027 | ||
8a09b4fa GB |
1028 | /* ------------------------------------------------------------------------- */ |
1029 | /* Logitech M560 devices */ | |
1030 | /* ------------------------------------------------------------------------- */ | |
1031 | ||
1032 | /* | |
1033 | * Logitech M560 protocol overview | |
1034 | * | |
1035 | * The Logitech M560 mouse, is designed for windows 8. When the middle and/or | |
1036 | * the sides buttons are pressed, it sends some keyboard keys events | |
1037 | * instead of buttons ones. | |
1038 | * To complicate things further, the middle button keys sequence | |
1039 | * is different from the odd press and the even press. | |
1040 | * | |
1041 | * forward button -> Super_R | |
1042 | * backward button -> Super_L+'d' (press only) | |
1043 | * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only) | |
1044 | * 2nd time: left-click (press only) | |
1045 | * NB: press-only means that when the button is pressed, the | |
1046 | * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated | |
1047 | * together sequentially; instead when the button is released, no event is | |
1048 | * generated ! | |
1049 | * | |
1050 | * With the command | |
1051 | * 10<xx>0a 3500af03 (where <xx> is the mouse id), | |
1052 | * the mouse reacts differently: | |
1053 | * - it never sends a keyboard key event | |
1054 | * - for the three mouse button it sends: | |
1055 | * middle button press 11<xx>0a 3500af00... | |
1056 | * side 1 button (forward) press 11<xx>0a 3500b000... | |
1057 | * side 2 button (backward) press 11<xx>0a 3500ae00... | |
1058 | * middle/side1/side2 button release 11<xx>0a 35000000... | |
1059 | */ | |
1060 | ||
1061 | static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03}; | |
1062 | ||
1063 | struct m560_private_data { | |
1064 | struct input_dev *input; | |
1065 | }; | |
1066 | ||
1067 | /* how buttons are mapped in the report */ | |
1068 | #define M560_MOUSE_BTN_LEFT 0x01 | |
1069 | #define M560_MOUSE_BTN_RIGHT 0x02 | |
1070 | #define M560_MOUSE_BTN_WHEEL_LEFT 0x08 | |
1071 | #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10 | |
1072 | ||
1073 | #define M560_SUB_ID 0x0a | |
1074 | #define M560_BUTTON_MODE_REGISTER 0x35 | |
1075 | ||
1076 | static int m560_send_config_command(struct hid_device *hdev, bool connected) | |
1077 | { | |
1078 | struct hidpp_report response; | |
1079 | struct hidpp_device *hidpp_dev; | |
1080 | ||
1081 | hidpp_dev = hid_get_drvdata(hdev); | |
1082 | ||
1083 | if (!connected) | |
1084 | return -ENODEV; | |
1085 | ||
1086 | return hidpp_send_rap_command_sync( | |
1087 | hidpp_dev, | |
1088 | REPORT_ID_HIDPP_SHORT, | |
1089 | M560_SUB_ID, | |
1090 | M560_BUTTON_MODE_REGISTER, | |
1091 | (u8 *)m560_config_parameter, | |
1092 | sizeof(m560_config_parameter), | |
1093 | &response | |
1094 | ); | |
1095 | } | |
1096 | ||
1097 | static int m560_allocate(struct hid_device *hdev) | |
1098 | { | |
1099 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1100 | struct m560_private_data *d; | |
1101 | ||
1102 | d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data), | |
1103 | GFP_KERNEL); | |
1104 | if (!d) | |
1105 | return -ENOMEM; | |
1106 | ||
1107 | hidpp->private_data = d; | |
1108 | ||
1109 | return 0; | |
1110 | }; | |
1111 | ||
1112 | static int m560_raw_event(struct hid_device *hdev, u8 *data, int size) | |
1113 | { | |
1114 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1115 | struct m560_private_data *mydata = hidpp->private_data; | |
1116 | ||
1117 | /* sanity check */ | |
1118 | if (!mydata || !mydata->input) { | |
1119 | hid_err(hdev, "error in parameter\n"); | |
1120 | return -EINVAL; | |
1121 | } | |
1122 | ||
1123 | if (size < 7) { | |
1124 | hid_err(hdev, "error in report\n"); | |
1125 | return 0; | |
1126 | } | |
1127 | ||
1128 | if (data[0] == REPORT_ID_HIDPP_LONG && | |
1129 | data[2] == M560_SUB_ID && data[6] == 0x00) { | |
1130 | /* | |
1131 | * m560 mouse report for middle, forward and backward button | |
1132 | * | |
1133 | * data[0] = 0x11 | |
1134 | * data[1] = device-id | |
1135 | * data[2] = 0x0a | |
1136 | * data[5] = 0xaf -> middle | |
1137 | * 0xb0 -> forward | |
1138 | * 0xae -> backward | |
1139 | * 0x00 -> release all | |
1140 | * data[6] = 0x00 | |
1141 | */ | |
1142 | ||
1143 | switch (data[5]) { | |
1144 | case 0xaf: | |
1145 | input_report_key(mydata->input, BTN_MIDDLE, 1); | |
1146 | break; | |
1147 | case 0xb0: | |
1148 | input_report_key(mydata->input, BTN_FORWARD, 1); | |
1149 | break; | |
1150 | case 0xae: | |
1151 | input_report_key(mydata->input, BTN_BACK, 1); | |
1152 | break; | |
1153 | case 0x00: | |
1154 | input_report_key(mydata->input, BTN_BACK, 0); | |
1155 | input_report_key(mydata->input, BTN_FORWARD, 0); | |
1156 | input_report_key(mydata->input, BTN_MIDDLE, 0); | |
1157 | break; | |
1158 | default: | |
1159 | hid_err(hdev, "error in report\n"); | |
1160 | return 0; | |
1161 | } | |
1162 | input_sync(mydata->input); | |
1163 | ||
1164 | } else if (data[0] == 0x02) { | |
1165 | /* | |
1166 | * Logitech M560 mouse report | |
1167 | * | |
1168 | * data[0] = type (0x02) | |
1169 | * data[1..2] = buttons | |
1170 | * data[3..5] = xy | |
1171 | * data[6] = wheel | |
1172 | */ | |
1173 | ||
1174 | int v; | |
1175 | ||
1176 | input_report_key(mydata->input, BTN_LEFT, | |
1177 | !!(data[1] & M560_MOUSE_BTN_LEFT)); | |
1178 | input_report_key(mydata->input, BTN_RIGHT, | |
1179 | !!(data[1] & M560_MOUSE_BTN_RIGHT)); | |
1180 | ||
1181 | if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) | |
1182 | input_report_rel(mydata->input, REL_HWHEEL, -1); | |
1183 | else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) | |
1184 | input_report_rel(mydata->input, REL_HWHEEL, 1); | |
1185 | ||
1186 | v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12); | |
1187 | input_report_rel(mydata->input, REL_X, v); | |
1188 | ||
1189 | v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12); | |
1190 | input_report_rel(mydata->input, REL_Y, v); | |
1191 | ||
1192 | v = hid_snto32(data[6], 8); | |
1193 | input_report_rel(mydata->input, REL_WHEEL, v); | |
1194 | ||
1195 | input_sync(mydata->input); | |
1196 | } | |
1197 | ||
1198 | return 1; | |
1199 | } | |
1200 | ||
1201 | static void m560_populate_input(struct hidpp_device *hidpp, | |
1202 | struct input_dev *input_dev, bool origin_is_hid_core) | |
1203 | { | |
1204 | struct m560_private_data *mydata = hidpp->private_data; | |
1205 | ||
1206 | mydata->input = input_dev; | |
1207 | ||
1208 | __set_bit(EV_KEY, mydata->input->evbit); | |
1209 | __set_bit(BTN_MIDDLE, mydata->input->keybit); | |
1210 | __set_bit(BTN_RIGHT, mydata->input->keybit); | |
1211 | __set_bit(BTN_LEFT, mydata->input->keybit); | |
1212 | __set_bit(BTN_BACK, mydata->input->keybit); | |
1213 | __set_bit(BTN_FORWARD, mydata->input->keybit); | |
1214 | ||
1215 | __set_bit(EV_REL, mydata->input->evbit); | |
1216 | __set_bit(REL_X, mydata->input->relbit); | |
1217 | __set_bit(REL_Y, mydata->input->relbit); | |
1218 | __set_bit(REL_WHEEL, mydata->input->relbit); | |
1219 | __set_bit(REL_HWHEEL, mydata->input->relbit); | |
1220 | } | |
1221 | ||
1222 | static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
1223 | struct hid_field *field, struct hid_usage *usage, | |
1224 | unsigned long **bit, int *max) | |
1225 | { | |
1226 | return -1; | |
1227 | } | |
1228 | ||
90cdd986 BT |
1229 | /* ------------------------------------------------------------------------- */ |
1230 | /* Logitech K400 devices */ | |
1231 | /* ------------------------------------------------------------------------- */ | |
1232 | ||
1233 | /* | |
1234 | * The Logitech K400 keyboard has an embedded touchpad which is seen | |
1235 | * as a mouse from the OS point of view. There is a hardware shortcut to disable | |
1236 | * tap-to-click but the setting is not remembered accross reset, annoying some | |
1237 | * users. | |
1238 | * | |
1239 | * We can toggle this feature from the host by using the feature 0x6010: | |
1240 | * Touchpad FW items | |
1241 | */ | |
1242 | ||
1243 | struct k400_private_data { | |
1244 | u8 feature_index; | |
1245 | }; | |
1246 | ||
1247 | static int k400_disable_tap_to_click(struct hidpp_device *hidpp) | |
1248 | { | |
1249 | struct k400_private_data *k400 = hidpp->private_data; | |
1250 | struct hidpp_touchpad_fw_items items = {}; | |
1251 | int ret; | |
1252 | u8 feature_type; | |
1253 | ||
1254 | if (!k400->feature_index) { | |
1255 | ret = hidpp_root_get_feature(hidpp, | |
1256 | HIDPP_PAGE_TOUCHPAD_FW_ITEMS, | |
1257 | &k400->feature_index, &feature_type); | |
1258 | if (ret) | |
1259 | /* means that the device is not powered up */ | |
1260 | return ret; | |
1261 | } | |
1262 | ||
1263 | ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items); | |
1264 | if (ret) | |
1265 | return ret; | |
1266 | ||
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | static int k400_allocate(struct hid_device *hdev) | |
1271 | { | |
1272 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1273 | struct k400_private_data *k400; | |
1274 | ||
1275 | k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data), | |
1276 | GFP_KERNEL); | |
1277 | if (!k400) | |
1278 | return -ENOMEM; | |
1279 | ||
1280 | hidpp->private_data = k400; | |
1281 | ||
1282 | return 0; | |
1283 | }; | |
1284 | ||
1285 | static int k400_connect(struct hid_device *hdev, bool connected) | |
1286 | { | |
1287 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1288 | ||
1289 | if (!connected) | |
1290 | return 0; | |
1291 | ||
1292 | if (!disable_tap_to_click) | |
1293 | return 0; | |
1294 | ||
1295 | return k400_disable_tap_to_click(hidpp); | |
1296 | } | |
1297 | ||
7f4b49fe SW |
1298 | /* ------------------------------------------------------------------------- */ |
1299 | /* Logitech G920 Driving Force Racing Wheel for Xbox One */ | |
1300 | /* ------------------------------------------------------------------------- */ | |
1301 | ||
1302 | #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123 | |
1303 | ||
1304 | /* Using session ID = 1 */ | |
1305 | #define CMD_G920_FORCE_GET_APERTURE 0x51 | |
1306 | #define CMD_G920_FORCE_SET_APERTURE 0x61 | |
1307 | ||
1308 | struct g920_private_data { | |
1309 | u8 force_feature; | |
1310 | u16 range; | |
1311 | }; | |
1312 | ||
7f4b49fe SW |
1313 | static ssize_t g920_range_show(struct device *dev, struct device_attribute *attr, |
1314 | char *buf) | |
1315 | { | |
1316 | struct hid_device *hid = to_hid_device(dev); | |
1317 | struct hidpp_device *hidpp = hid_get_drvdata(hid); | |
1318 | struct g920_private_data *pdata; | |
1319 | ||
1320 | pdata = hidpp->private_data; | |
1321 | if (!pdata) { | |
1322 | hid_err(hid, "Private driver data not found!\n"); | |
1323 | return -EINVAL; | |
1324 | } | |
1325 | ||
1326 | return scnprintf(buf, PAGE_SIZE, "%u\n", pdata->range); | |
1327 | } | |
1328 | ||
1329 | static ssize_t g920_range_store(struct device *dev, struct device_attribute *attr, | |
1330 | const char *buf, size_t count) | |
1331 | { | |
1332 | struct hid_device *hid = to_hid_device(dev); | |
1333 | struct hidpp_device *hidpp = hid_get_drvdata(hid); | |
1334 | struct g920_private_data *pdata; | |
1335 | struct hidpp_report response; | |
1336 | u8 params[2]; | |
1337 | int ret; | |
1338 | u16 range = simple_strtoul(buf, NULL, 10); | |
1339 | ||
1340 | pdata = hidpp->private_data; | |
1341 | if (!pdata) { | |
1342 | hid_err(hid, "Private driver data not found!\n"); | |
1343 | return -EINVAL; | |
1344 | } | |
1345 | ||
1346 | if (range < 180) | |
1347 | range = 180; | |
1348 | else if (range > 900) | |
1349 | range = 900; | |
1350 | ||
1351 | params[0] = range >> 8; | |
1352 | params[1] = range & 0x00FF; | |
1353 | ||
1354 | ret = hidpp_send_fap_command_sync(hidpp, pdata->force_feature, | |
1355 | CMD_G920_FORCE_SET_APERTURE, params, 2, &response); | |
1356 | if (ret) | |
1357 | return ret; | |
1358 | ||
1359 | pdata->range = range; | |
1360 | return count; | |
1361 | } | |
1362 | ||
1363 | static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, g920_range_show, g920_range_store); | |
1364 | ||
1365 | static int g920_allocate(struct hid_device *hdev) | |
1366 | { | |
1367 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1368 | struct g920_private_data *pdata; | |
1369 | ||
1370 | pdata = devm_kzalloc(&hdev->dev, sizeof(struct g920_private_data), | |
1371 | GFP_KERNEL); | |
1372 | if (!pdata) | |
1373 | return -ENOMEM; | |
1374 | ||
1375 | hidpp->private_data = pdata; | |
1376 | ||
1377 | return 0; | |
1378 | } | |
1379 | ||
1380 | static int g920_get_config(struct hidpp_device *hidpp) | |
1381 | { | |
1382 | struct g920_private_data *pdata = hidpp->private_data; | |
1383 | struct hidpp_report response; | |
1384 | u8 feature_type; | |
1385 | u8 feature_index; | |
1386 | int ret; | |
1387 | ||
1388 | pdata = hidpp->private_data; | |
1389 | if (!pdata) { | |
1390 | hid_err(hidpp->hid_dev, "Private driver data not found!\n"); | |
1391 | return -EINVAL; | |
1392 | } | |
1393 | ||
1394 | /* Find feature and store for later use */ | |
1395 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK, | |
1396 | &feature_index, &feature_type); | |
1397 | if (ret) | |
1398 | return ret; | |
1399 | ||
1400 | pdata->force_feature = feature_index; | |
1401 | ||
1402 | /* Read current Range */ | |
1403 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
1404 | CMD_G920_FORCE_GET_APERTURE, NULL, 0, &response); | |
1405 | if (ret > 0) { | |
1406 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
1407 | __func__, ret); | |
1408 | return -EPROTO; | |
1409 | } | |
1410 | if (ret) | |
1411 | return ret; | |
1412 | ||
1413 | pdata->range = get_unaligned_be16(&response.fap.params[0]); | |
1414 | ||
1415 | /* Create sysfs interface */ | |
1416 | ret = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range); | |
1417 | if (ret) | |
1418 | hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d\n", ret); | |
1419 | ||
1420 | return 0; | |
1421 | } | |
1422 | ||
2f31c525 BT |
1423 | /* -------------------------------------------------------------------------- */ |
1424 | /* Generic HID++ devices */ | |
1425 | /* -------------------------------------------------------------------------- */ | |
1426 | ||
1427 | static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
1428 | struct hid_field *field, struct hid_usage *usage, | |
1429 | unsigned long **bit, int *max) | |
1430 | { | |
1431 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1432 | ||
1433 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) | |
1434 | return wtp_input_mapping(hdev, hi, field, usage, bit, max); | |
8a09b4fa GB |
1435 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 && |
1436 | field->application != HID_GD_MOUSE) | |
1437 | return m560_input_mapping(hdev, hi, field, usage, bit, max); | |
2f31c525 BT |
1438 | |
1439 | return 0; | |
1440 | } | |
1441 | ||
0b1804e3 SW |
1442 | static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
1443 | struct hid_field *field, struct hid_usage *usage, | |
1444 | unsigned long **bit, int *max) | |
1445 | { | |
1446 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1447 | ||
1448 | /* Ensure that Logitech G920 is not given a default fuzz/flat value */ | |
1449 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { | |
1450 | if (usage->type == EV_ABS && (usage->code == ABS_X || | |
1451 | usage->code == ABS_Y || usage->code == ABS_Z || | |
1452 | usage->code == ABS_RZ)) { | |
1453 | field->application = HID_GD_MULTIAXIS; | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | return 0; | |
1458 | } | |
1459 | ||
1460 | ||
c39e3d5f BT |
1461 | static void hidpp_populate_input(struct hidpp_device *hidpp, |
1462 | struct input_dev *input, bool origin_is_hid_core) | |
1463 | { | |
1464 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) | |
1465 | wtp_populate_input(hidpp, input, origin_is_hid_core); | |
8a09b4fa GB |
1466 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) |
1467 | m560_populate_input(hidpp, input, origin_is_hid_core); | |
c39e3d5f BT |
1468 | } |
1469 | ||
b2c68a2f | 1470 | static int hidpp_input_configured(struct hid_device *hdev, |
2f31c525 BT |
1471 | struct hid_input *hidinput) |
1472 | { | |
1473 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
c39e3d5f | 1474 | struct input_dev *input = hidinput->input; |
2f31c525 | 1475 | |
c39e3d5f | 1476 | hidpp_populate_input(hidpp, input, true); |
b2c68a2f DT |
1477 | |
1478 | return 0; | |
2f31c525 BT |
1479 | } |
1480 | ||
1481 | static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, | |
1482 | int size) | |
1483 | { | |
1484 | struct hidpp_report *question = hidpp->send_receive_buf; | |
1485 | struct hidpp_report *answer = hidpp->send_receive_buf; | |
1486 | struct hidpp_report *report = (struct hidpp_report *)data; | |
1487 | ||
1488 | /* | |
1489 | * If the mutex is locked then we have a pending answer from a | |
e529fea9 | 1490 | * previously sent command. |
2f31c525 BT |
1491 | */ |
1492 | if (unlikely(mutex_is_locked(&hidpp->send_mutex))) { | |
1493 | /* | |
1494 | * Check for a correct hidpp20 answer or the corresponding | |
1495 | * error | |
1496 | */ | |
1497 | if (hidpp_match_answer(question, report) || | |
1498 | hidpp_match_error(question, report)) { | |
1499 | *answer = *report; | |
1500 | hidpp->answer_available = true; | |
1501 | wake_up(&hidpp->wait); | |
1502 | /* | |
1503 | * This was an answer to a command that this driver sent | |
1504 | * We return 1 to hid-core to avoid forwarding the | |
1505 | * command upstream as it has been treated by the driver | |
1506 | */ | |
1507 | ||
1508 | return 1; | |
1509 | } | |
1510 | } | |
1511 | ||
c39e3d5f BT |
1512 | if (unlikely(hidpp_report_is_connect_event(report))) { |
1513 | atomic_set(&hidpp->connected, | |
1514 | !(report->rap.params[0] & (1 << 6))); | |
580a7e82 | 1515 | if ((hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) && |
c39e3d5f BT |
1516 | (schedule_work(&hidpp->work) == 0)) |
1517 | dbg_hid("%s: connect event already queued\n", __func__); | |
1518 | return 1; | |
1519 | } | |
1520 | ||
2f31c525 BT |
1521 | return 0; |
1522 | } | |
1523 | ||
1524 | static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report, | |
1525 | u8 *data, int size) | |
1526 | { | |
1527 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
e529fea9 | 1528 | int ret = 0; |
2f31c525 | 1529 | |
e529fea9 | 1530 | /* Generic HID++ processing. */ |
2f31c525 | 1531 | switch (data[0]) { |
a5ce8f5b SW |
1532 | case REPORT_ID_HIDPP_VERY_LONG: |
1533 | if (size != HIDPP_REPORT_VERY_LONG_LENGTH) { | |
1534 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
1535 | size); | |
1536 | return 1; | |
1537 | } | |
1538 | ret = hidpp_raw_hidpp_event(hidpp, data, size); | |
1539 | break; | |
2f31c525 BT |
1540 | case REPORT_ID_HIDPP_LONG: |
1541 | if (size != HIDPP_REPORT_LONG_LENGTH) { | |
1542 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
1543 | size); | |
1544 | return 1; | |
1545 | } | |
e529fea9 PW |
1546 | ret = hidpp_raw_hidpp_event(hidpp, data, size); |
1547 | break; | |
2f31c525 BT |
1548 | case REPORT_ID_HIDPP_SHORT: |
1549 | if (size != HIDPP_REPORT_SHORT_LENGTH) { | |
1550 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
1551 | size); | |
1552 | return 1; | |
1553 | } | |
e529fea9 PW |
1554 | ret = hidpp_raw_hidpp_event(hidpp, data, size); |
1555 | break; | |
2f31c525 BT |
1556 | } |
1557 | ||
e529fea9 PW |
1558 | /* If no report is available for further processing, skip calling |
1559 | * raw_event of subclasses. */ | |
1560 | if (ret != 0) | |
1561 | return ret; | |
1562 | ||
2f31c525 BT |
1563 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
1564 | return wtp_raw_event(hdev, data, size); | |
8a09b4fa GB |
1565 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) |
1566 | return m560_raw_event(hdev, data, size); | |
2f31c525 BT |
1567 | |
1568 | return 0; | |
1569 | } | |
1570 | ||
33797820 | 1571 | static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying) |
2f31c525 BT |
1572 | { |
1573 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1574 | char *name; | |
2f31c525 | 1575 | |
33797820 BT |
1576 | if (use_unifying) |
1577 | /* | |
1578 | * the device is connected through an Unifying receiver, and | |
1579 | * might not be already connected. | |
1580 | * Ask the receiver for its name. | |
1581 | */ | |
1582 | name = hidpp_get_unifying_name(hidpp); | |
1583 | else | |
02cc097e | 1584 | name = hidpp_get_device_name(hidpp); |
2f31c525 | 1585 | |
7bfd2927 | 1586 | if (!name) { |
2f31c525 | 1587 | hid_err(hdev, "unable to retrieve the name of the device"); |
7bfd2927 SW |
1588 | } else { |
1589 | dbg_hid("HID++: Got name: %s\n", name); | |
2f31c525 | 1590 | snprintf(hdev->name, sizeof(hdev->name), "%s", name); |
7bfd2927 | 1591 | } |
2f31c525 BT |
1592 | |
1593 | kfree(name); | |
1594 | } | |
1595 | ||
c39e3d5f BT |
1596 | static int hidpp_input_open(struct input_dev *dev) |
1597 | { | |
1598 | struct hid_device *hid = input_get_drvdata(dev); | |
1599 | ||
1600 | return hid_hw_open(hid); | |
1601 | } | |
1602 | ||
1603 | static void hidpp_input_close(struct input_dev *dev) | |
1604 | { | |
1605 | struct hid_device *hid = input_get_drvdata(dev); | |
1606 | ||
1607 | hid_hw_close(hid); | |
1608 | } | |
1609 | ||
1610 | static struct input_dev *hidpp_allocate_input(struct hid_device *hdev) | |
1611 | { | |
1612 | struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev); | |
005b3f57 | 1613 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
c39e3d5f BT |
1614 | |
1615 | if (!input_dev) | |
1616 | return NULL; | |
1617 | ||
1618 | input_set_drvdata(input_dev, hdev); | |
1619 | input_dev->open = hidpp_input_open; | |
1620 | input_dev->close = hidpp_input_close; | |
1621 | ||
005b3f57 | 1622 | input_dev->name = hidpp->name; |
c39e3d5f BT |
1623 | input_dev->phys = hdev->phys; |
1624 | input_dev->uniq = hdev->uniq; | |
1625 | input_dev->id.bustype = hdev->bus; | |
1626 | input_dev->id.vendor = hdev->vendor; | |
1627 | input_dev->id.product = hdev->product; | |
1628 | input_dev->id.version = hdev->version; | |
1629 | input_dev->dev.parent = &hdev->dev; | |
1630 | ||
1631 | return input_dev; | |
1632 | } | |
1633 | ||
1634 | static void hidpp_connect_event(struct hidpp_device *hidpp) | |
1635 | { | |
1636 | struct hid_device *hdev = hidpp->hid_dev; | |
1637 | int ret = 0; | |
1638 | bool connected = atomic_read(&hidpp->connected); | |
1639 | struct input_dev *input; | |
1640 | char *name, *devm_name; | |
c39e3d5f | 1641 | |
bf159447 BT |
1642 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { |
1643 | ret = wtp_connect(hdev, connected); | |
1644 | if (ret) | |
1645 | return; | |
8a09b4fa GB |
1646 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { |
1647 | ret = m560_send_config_command(hdev, connected); | |
1648 | if (ret) | |
1649 | return; | |
90cdd986 BT |
1650 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { |
1651 | ret = k400_connect(hdev, connected); | |
1652 | if (ret) | |
1653 | return; | |
bf159447 | 1654 | } |
586bdc4e | 1655 | |
c39e3d5f BT |
1656 | if (!connected || hidpp->delayed_input) |
1657 | return; | |
1658 | ||
580a7e82 BT |
1659 | /* the device is already connected, we can ask for its name and |
1660 | * protocol */ | |
c39e3d5f BT |
1661 | if (!hidpp->protocol_major) { |
1662 | ret = !hidpp_is_connected(hidpp); | |
1663 | if (ret) { | |
1664 | hid_err(hdev, "Can not get the protocol version.\n"); | |
1665 | return; | |
1666 | } | |
580a7e82 BT |
1667 | hid_info(hdev, "HID++ %u.%u device connected.\n", |
1668 | hidpp->protocol_major, hidpp->protocol_minor); | |
c39e3d5f BT |
1669 | } |
1670 | ||
580a7e82 BT |
1671 | if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)) |
1672 | /* if HID created the input nodes for us, we can stop now */ | |
1673 | return; | |
c39e3d5f | 1674 | |
005b3f57 BT |
1675 | if (!hidpp->name || hidpp->name == hdev->name) { |
1676 | name = hidpp_get_device_name(hidpp); | |
1677 | if (!name) { | |
1678 | hid_err(hdev, | |
1679 | "unable to retrieve the name of the device"); | |
1680 | return; | |
1681 | } | |
1682 | ||
1683 | devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name); | |
1684 | kfree(name); | |
1685 | if (!devm_name) | |
1686 | return; | |
1687 | ||
1688 | hidpp->name = devm_name; | |
1689 | } | |
1690 | ||
c39e3d5f BT |
1691 | input = hidpp_allocate_input(hdev); |
1692 | if (!input) { | |
1693 | hid_err(hdev, "cannot allocate new input device: %d\n", ret); | |
1694 | return; | |
1695 | } | |
1696 | ||
c39e3d5f BT |
1697 | hidpp_populate_input(hidpp, input, false); |
1698 | ||
1699 | ret = input_register_device(input); | |
1700 | if (ret) | |
1701 | input_free_device(input); | |
1702 | ||
1703 | hidpp->delayed_input = input; | |
1704 | } | |
1705 | ||
2f31c525 BT |
1706 | static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) |
1707 | { | |
1708 | struct hidpp_device *hidpp; | |
1709 | int ret; | |
1710 | bool connected; | |
c39e3d5f | 1711 | unsigned int connect_mask = HID_CONNECT_DEFAULT; |
2f31c525 BT |
1712 | |
1713 | hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device), | |
1714 | GFP_KERNEL); | |
1715 | if (!hidpp) | |
1716 | return -ENOMEM; | |
1717 | ||
1718 | hidpp->hid_dev = hdev; | |
005b3f57 | 1719 | hidpp->name = hdev->name; |
2f31c525 BT |
1720 | hid_set_drvdata(hdev, hidpp); |
1721 | ||
1722 | hidpp->quirks = id->driver_data; | |
1723 | ||
9188dbae BT |
1724 | if (disable_raw_mode) { |
1725 | hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP; | |
580a7e82 BT |
1726 | hidpp->quirks &= ~HIDPP_QUIRK_CONNECT_EVENTS; |
1727 | hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT; | |
9188dbae BT |
1728 | } |
1729 | ||
2f31c525 BT |
1730 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { |
1731 | ret = wtp_allocate(hdev, id); | |
1732 | if (ret) | |
8a09b4fa GB |
1733 | goto allocate_fail; |
1734 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { | |
1735 | ret = m560_allocate(hdev); | |
1736 | if (ret) | |
1737 | goto allocate_fail; | |
90cdd986 BT |
1738 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { |
1739 | ret = k400_allocate(hdev); | |
1740 | if (ret) | |
1741 | goto allocate_fail; | |
7f4b49fe SW |
1742 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { |
1743 | ret = g920_allocate(hdev); | |
1744 | if (ret) | |
1745 | goto allocate_fail; | |
2f31c525 BT |
1746 | } |
1747 | ||
c39e3d5f | 1748 | INIT_WORK(&hidpp->work, delayed_work_cb); |
2f31c525 BT |
1749 | mutex_init(&hidpp->send_mutex); |
1750 | init_waitqueue_head(&hidpp->wait); | |
1751 | ||
1752 | ret = hid_parse(hdev); | |
1753 | if (ret) { | |
1754 | hid_err(hdev, "%s:parse failed\n", __func__); | |
1755 | goto hid_parse_fail; | |
1756 | } | |
1757 | ||
7bfd2927 SW |
1758 | if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) |
1759 | connect_mask &= ~HID_CONNECT_HIDINPUT; | |
1760 | ||
1761 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { | |
1762 | ret = hid_hw_start(hdev, connect_mask); | |
1763 | if (ret) { | |
1764 | hid_err(hdev, "hw start failed\n"); | |
1765 | goto hid_hw_start_fail; | |
1766 | } | |
1767 | ret = hid_hw_open(hdev); | |
1768 | if (ret < 0) { | |
1769 | dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", | |
1770 | __func__, ret); | |
1771 | hid_hw_stop(hdev); | |
1772 | goto hid_hw_start_fail; | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | ||
2f31c525 BT |
1777 | /* Allow incoming packets */ |
1778 | hid_device_io_start(hdev); | |
1779 | ||
1780 | connected = hidpp_is_connected(hidpp); | |
ab94e562 BT |
1781 | if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) { |
1782 | if (!connected) { | |
b832da56 | 1783 | ret = -ENODEV; |
ab94e562 | 1784 | hid_err(hdev, "Device not connected"); |
7bfd2927 | 1785 | goto hid_hw_open_failed; |
ab94e562 | 1786 | } |
2f31c525 | 1787 | |
ab94e562 BT |
1788 | hid_info(hdev, "HID++ %u.%u device connected.\n", |
1789 | hidpp->protocol_major, hidpp->protocol_minor); | |
ab94e562 | 1790 | } |
2f31c525 | 1791 | |
33797820 | 1792 | hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE); |
c39e3d5f | 1793 | atomic_set(&hidpp->connected, connected); |
33797820 | 1794 | |
c39e3d5f | 1795 | if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) { |
2f31c525 BT |
1796 | ret = wtp_get_config(hidpp); |
1797 | if (ret) | |
7bfd2927 | 1798 | goto hid_hw_open_failed; |
7f4b49fe SW |
1799 | } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) { |
1800 | ret = g920_get_config(hidpp); | |
1801 | if (ret) | |
1802 | goto hid_hw_open_failed; | |
2f31c525 BT |
1803 | } |
1804 | ||
1805 | /* Block incoming packets */ | |
1806 | hid_device_io_stop(hdev); | |
1807 | ||
7bfd2927 SW |
1808 | if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) { |
1809 | ret = hid_hw_start(hdev, connect_mask); | |
1810 | if (ret) { | |
1811 | hid_err(hdev, "%s:hid_hw_start returned error\n", __func__); | |
1812 | goto hid_hw_start_fail; | |
1813 | } | |
2f31c525 BT |
1814 | } |
1815 | ||
580a7e82 | 1816 | if (hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) { |
c39e3d5f BT |
1817 | /* Allow incoming packets */ |
1818 | hid_device_io_start(hdev); | |
1819 | ||
1820 | hidpp_connect_event(hidpp); | |
1821 | } | |
1822 | ||
2f31c525 BT |
1823 | return ret; |
1824 | ||
7bfd2927 SW |
1825 | hid_hw_open_failed: |
1826 | hid_device_io_stop(hdev); | |
1827 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { | |
7f4b49fe | 1828 | device_remove_file(&hdev->dev, &dev_attr_range); |
7bfd2927 SW |
1829 | hid_hw_close(hdev); |
1830 | hid_hw_stop(hdev); | |
1831 | } | |
2f31c525 BT |
1832 | hid_hw_start_fail: |
1833 | hid_parse_fail: | |
c39e3d5f | 1834 | cancel_work_sync(&hidpp->work); |
2f31c525 | 1835 | mutex_destroy(&hidpp->send_mutex); |
8a09b4fa | 1836 | allocate_fail: |
2f31c525 BT |
1837 | hid_set_drvdata(hdev, NULL); |
1838 | return ret; | |
1839 | } | |
1840 | ||
1841 | static void hidpp_remove(struct hid_device *hdev) | |
1842 | { | |
1843 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
1844 | ||
7f4b49fe SW |
1845 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { |
1846 | device_remove_file(&hdev->dev, &dev_attr_range); | |
7bfd2927 | 1847 | hid_hw_close(hdev); |
7f4b49fe | 1848 | } |
7bfd2927 | 1849 | hid_hw_stop(hdev); |
c39e3d5f | 1850 | cancel_work_sync(&hidpp->work); |
2f31c525 | 1851 | mutex_destroy(&hidpp->send_mutex); |
2f31c525 BT |
1852 | } |
1853 | ||
1854 | static const struct hid_device_id hidpp_devices[] = { | |
57ac86cf BT |
1855 | { /* wireless touchpad */ |
1856 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1857 | USB_VENDOR_ID_LOGITECH, 0x4011), | |
1858 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT | | |
1859 | HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS }, | |
586bdc4e BT |
1860 | { /* wireless touchpad T650 */ |
1861 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1862 | USB_VENDOR_ID_LOGITECH, 0x4101), | |
1863 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT }, | |
2f31c525 BT |
1864 | { /* wireless touchpad T651 */ |
1865 | HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, | |
1866 | USB_DEVICE_ID_LOGITECH_T651), | |
1867 | .driver_data = HIDPP_QUIRK_CLASS_WTP }, | |
8a09b4fa | 1868 | { /* Mouse logitech M560 */ |
3a61e975 | 1869 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, |
8a09b4fa GB |
1870 | USB_VENDOR_ID_LOGITECH, 0x402d), |
1871 | .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 }, | |
90cdd986 BT |
1872 | { /* Keyboard logitech K400 */ |
1873 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1874 | USB_VENDOR_ID_LOGITECH, 0x4024), | |
1875 | .driver_data = HIDPP_QUIRK_CONNECT_EVENTS | HIDPP_QUIRK_CLASS_K400 }, | |
ab94e562 BT |
1876 | |
1877 | { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, | |
1878 | USB_VENDOR_ID_LOGITECH, HID_ANY_ID)}, | |
7bfd2927 SW |
1879 | |
1880 | { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL), | |
1881 | .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS}, | |
2f31c525 BT |
1882 | {} |
1883 | }; | |
1884 | ||
1885 | MODULE_DEVICE_TABLE(hid, hidpp_devices); | |
1886 | ||
1887 | static struct hid_driver hidpp_driver = { | |
1888 | .name = "logitech-hidpp-device", | |
1889 | .id_table = hidpp_devices, | |
1890 | .probe = hidpp_probe, | |
1891 | .remove = hidpp_remove, | |
1892 | .raw_event = hidpp_raw_event, | |
1893 | .input_configured = hidpp_input_configured, | |
1894 | .input_mapping = hidpp_input_mapping, | |
0b1804e3 | 1895 | .input_mapped = hidpp_input_mapped, |
2f31c525 BT |
1896 | }; |
1897 | ||
1898 | module_hid_driver(hidpp_driver); |