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> | |
ff21a635 EV |
18 | #include <linux/input.h> |
19 | #include <linux/usb.h> | |
2f31c525 BT |
20 | #include <linux/hid.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/slab.h> | |
23 | #include <linux/sched.h> | |
4435ff2f | 24 | #include <linux/sched/clock.h> |
2f31c525 BT |
25 | #include <linux/kfifo.h> |
26 | #include <linux/input/mt.h> | |
ff21a635 EV |
27 | #include <linux/workqueue.h> |
28 | #include <linux/atomic.h> | |
29 | #include <linux/fixp-arith.h> | |
2f31c525 | 30 | #include <asm/unaligned.h> |
ff21a635 | 31 | #include "usbhid/usbhid.h" |
2f31c525 BT |
32 | #include "hid-ids.h" |
33 | ||
34 | MODULE_LICENSE("GPL"); | |
35 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); | |
36 | MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>"); | |
37 | ||
9188dbae BT |
38 | static bool disable_raw_mode; |
39 | module_param(disable_raw_mode, bool, 0644); | |
40 | MODULE_PARM_DESC(disable_raw_mode, | |
41 | "Disable Raw mode reporting for touchpads and keep firmware gestures."); | |
42 | ||
90cdd986 BT |
43 | static bool disable_tap_to_click; |
44 | module_param(disable_tap_to_click, bool, 0644); | |
45 | MODULE_PARM_DESC(disable_tap_to_click, | |
46 | "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently)."); | |
47 | ||
2f31c525 BT |
48 | #define REPORT_ID_HIDPP_SHORT 0x10 |
49 | #define REPORT_ID_HIDPP_LONG 0x11 | |
a5ce8f5b | 50 | #define REPORT_ID_HIDPP_VERY_LONG 0x12 |
2f31c525 BT |
51 | |
52 | #define HIDPP_REPORT_SHORT_LENGTH 7 | |
53 | #define HIDPP_REPORT_LONG_LENGTH 20 | |
a5ce8f5b | 54 | #define HIDPP_REPORT_VERY_LONG_LENGTH 64 |
2f31c525 BT |
55 | |
56 | #define HIDPP_QUIRK_CLASS_WTP BIT(0) | |
8a09b4fa | 57 | #define HIDPP_QUIRK_CLASS_M560 BIT(1) |
90cdd986 | 58 | #define HIDPP_QUIRK_CLASS_K400 BIT(2) |
7bfd2927 | 59 | #define HIDPP_QUIRK_CLASS_G920 BIT(3) |
696ecef9 | 60 | #define HIDPP_QUIRK_CLASS_K750 BIT(4) |
2f31c525 | 61 | |
8a09b4fa | 62 | /* bits 2..20 are reserved for classes */ |
6bd4e65d | 63 | /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */ |
57ac86cf | 64 | #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22) |
580a7e82 | 65 | #define HIDPP_QUIRK_NO_HIDINPUT BIT(23) |
7bfd2927 | 66 | #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24) |
843c624e | 67 | #define HIDPP_QUIRK_UNIFYING BIT(25) |
4435ff2f HC |
68 | #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26) |
69 | #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27) | |
70 | #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28) | |
71 | ||
72 | /* Convenience constant to check for any high-res support. */ | |
73 | #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \ | |
74 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \ | |
75 | HIDPP_QUIRK_HI_RES_SCROLL_X2121) | |
580a7e82 | 76 | |
6bd4e65d | 77 | #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT |
c39e3d5f | 78 | |
206d7c68 BT |
79 | #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0) |
80 | #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1) | |
5b036ea1 BT |
81 | #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2) |
82 | #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3) | |
206d7c68 | 83 | |
2f31c525 BT |
84 | /* |
85 | * There are two hidpp protocols in use, the first version hidpp10 is known | |
86 | * as register access protocol or RAP, the second version hidpp20 is known as | |
87 | * feature access protocol or FAP | |
88 | * | |
89 | * Most older devices (including the Unifying usb receiver) use the RAP protocol | |
90 | * where as most newer devices use the FAP protocol. Both protocols are | |
91 | * compatible with the underlying transport, which could be usb, Unifiying, or | |
92 | * bluetooth. The message lengths are defined by the hid vendor specific report | |
93 | * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and | |
94 | * the HIDPP_LONG report type (total message length 20 bytes) | |
95 | * | |
96 | * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG | |
97 | * messages. The Unifying receiver itself responds to RAP messages (device index | |
98 | * is 0xFF for the receiver), and all messages (short or long) with a device | |
99 | * index between 1 and 6 are passed untouched to the corresponding paired | |
100 | * Unifying device. | |
101 | * | |
102 | * The paired device can be RAP or FAP, it will receive the message untouched | |
103 | * from the Unifiying receiver. | |
104 | */ | |
105 | ||
106 | struct fap { | |
107 | u8 feature_index; | |
108 | u8 funcindex_clientid; | |
a5ce8f5b | 109 | u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U]; |
2f31c525 BT |
110 | }; |
111 | ||
112 | struct rap { | |
113 | u8 sub_id; | |
114 | u8 reg_address; | |
a5ce8f5b | 115 | u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U]; |
2f31c525 BT |
116 | }; |
117 | ||
118 | struct hidpp_report { | |
119 | u8 report_id; | |
120 | u8 device_index; | |
121 | union { | |
122 | struct fap fap; | |
123 | struct rap rap; | |
124 | u8 rawbytes[sizeof(struct fap)]; | |
125 | }; | |
126 | } __packed; | |
127 | ||
5a2b190c PH |
128 | struct hidpp_battery { |
129 | u8 feature_index; | |
696ecef9 | 130 | u8 solar_feature_index; |
5a2b190c PH |
131 | struct power_supply_desc desc; |
132 | struct power_supply *ps; | |
133 | char name[64]; | |
134 | int status; | |
14f437a1 | 135 | int capacity; |
5b036ea1 | 136 | int level; |
284f8d75 | 137 | bool online; |
5a2b190c PH |
138 | }; |
139 | ||
4435ff2f HC |
140 | /** |
141 | * struct hidpp_scroll_counter - Utility class for processing high-resolution | |
142 | * scroll events. | |
143 | * @dev: the input device for which events should be reported. | |
144 | * @wheel_multiplier: the scalar multiplier to be applied to each wheel event | |
145 | * @remainder: counts the number of high-resolution units moved since the last | |
146 | * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should | |
147 | * only be used by class methods. | |
148 | * @direction: direction of last movement (1 or -1) | |
149 | * @last_time: last event time, used to reset remainder after inactivity | |
150 | */ | |
151 | struct hidpp_scroll_counter { | |
152 | struct input_dev *dev; | |
153 | int wheel_multiplier; | |
154 | int remainder; | |
155 | int direction; | |
156 | unsigned long long last_time; | |
157 | }; | |
158 | ||
2f31c525 BT |
159 | struct hidpp_device { |
160 | struct hid_device *hid_dev; | |
161 | struct mutex send_mutex; | |
162 | void *send_receive_buf; | |
005b3f57 | 163 | char *name; /* will never be NULL and should not be freed */ |
2f31c525 BT |
164 | wait_queue_head_t wait; |
165 | bool answer_available; | |
166 | u8 protocol_major; | |
167 | u8 protocol_minor; | |
168 | ||
169 | void *private_data; | |
170 | ||
c39e3d5f BT |
171 | struct work_struct work; |
172 | struct kfifo delayed_work_fifo; | |
173 | atomic_t connected; | |
174 | struct input_dev *delayed_input; | |
175 | ||
2f31c525 | 176 | unsigned long quirks; |
206d7c68 | 177 | unsigned long capabilities; |
2f31c525 | 178 | |
5a2b190c | 179 | struct hidpp_battery battery; |
4435ff2f | 180 | struct hidpp_scroll_counter vertical_wheel_counter; |
5a2b190c | 181 | }; |
2f31c525 | 182 | |
f677bb15 | 183 | /* HID++ 1.0 error codes */ |
2f31c525 BT |
184 | #define HIDPP_ERROR 0x8f |
185 | #define HIDPP_ERROR_SUCCESS 0x00 | |
186 | #define HIDPP_ERROR_INVALID_SUBID 0x01 | |
187 | #define HIDPP_ERROR_INVALID_ADRESS 0x02 | |
188 | #define HIDPP_ERROR_INVALID_VALUE 0x03 | |
189 | #define HIDPP_ERROR_CONNECT_FAIL 0x04 | |
190 | #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05 | |
191 | #define HIDPP_ERROR_ALREADY_EXISTS 0x06 | |
192 | #define HIDPP_ERROR_BUSY 0x07 | |
193 | #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08 | |
194 | #define HIDPP_ERROR_RESOURCE_ERROR 0x09 | |
195 | #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a | |
196 | #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b | |
197 | #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c | |
f677bb15 PW |
198 | /* HID++ 2.0 error codes */ |
199 | #define HIDPP20_ERROR 0xff | |
2f31c525 | 200 | |
c39e3d5f BT |
201 | static void hidpp_connect_event(struct hidpp_device *hidpp_dev); |
202 | ||
2f31c525 BT |
203 | static int __hidpp_send_report(struct hid_device *hdev, |
204 | struct hidpp_report *hidpp_report) | |
205 | { | |
7bfd2927 | 206 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
2f31c525 BT |
207 | int fields_count, ret; |
208 | ||
7bfd2927 SW |
209 | hidpp = hid_get_drvdata(hdev); |
210 | ||
2f31c525 BT |
211 | switch (hidpp_report->report_id) { |
212 | case REPORT_ID_HIDPP_SHORT: | |
213 | fields_count = HIDPP_REPORT_SHORT_LENGTH; | |
214 | break; | |
215 | case REPORT_ID_HIDPP_LONG: | |
216 | fields_count = HIDPP_REPORT_LONG_LENGTH; | |
217 | break; | |
a5ce8f5b SW |
218 | case REPORT_ID_HIDPP_VERY_LONG: |
219 | fields_count = HIDPP_REPORT_VERY_LONG_LENGTH; | |
220 | break; | |
2f31c525 BT |
221 | default: |
222 | return -ENODEV; | |
223 | } | |
224 | ||
225 | /* | |
226 | * set the device_index as the receiver, it will be overwritten by | |
227 | * hid_hw_request if needed | |
228 | */ | |
229 | hidpp_report->device_index = 0xff; | |
230 | ||
7bfd2927 SW |
231 | if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) { |
232 | ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count); | |
233 | } else { | |
234 | ret = hid_hw_raw_request(hdev, hidpp_report->report_id, | |
235 | (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT, | |
236 | HID_REQ_SET_REPORT); | |
237 | } | |
2f31c525 BT |
238 | |
239 | return ret == fields_count ? 0 : -1; | |
240 | } | |
241 | ||
8c9952b2 BT |
242 | /** |
243 | * hidpp_send_message_sync() returns 0 in case of success, and something else | |
244 | * in case of a failure. | |
245 | * - If ' something else' is positive, that means that an error has been raised | |
246 | * by the protocol itself. | |
247 | * - If ' something else' is negative, that means that we had a classic error | |
248 | * (-ENOMEM, -EPIPE, etc...) | |
249 | */ | |
2f31c525 BT |
250 | static int hidpp_send_message_sync(struct hidpp_device *hidpp, |
251 | struct hidpp_report *message, | |
252 | struct hidpp_report *response) | |
253 | { | |
254 | int ret; | |
255 | ||
256 | mutex_lock(&hidpp->send_mutex); | |
257 | ||
258 | hidpp->send_receive_buf = response; | |
259 | hidpp->answer_available = false; | |
260 | ||
261 | /* | |
262 | * So that we can later validate the answer when it arrives | |
263 | * in hidpp_raw_event | |
264 | */ | |
265 | *response = *message; | |
266 | ||
267 | ret = __hidpp_send_report(hidpp->hid_dev, message); | |
268 | ||
269 | if (ret) { | |
270 | dbg_hid("__hidpp_send_report returned err: %d\n", ret); | |
271 | memset(response, 0, sizeof(struct hidpp_report)); | |
272 | goto exit; | |
273 | } | |
274 | ||
275 | if (!wait_event_timeout(hidpp->wait, hidpp->answer_available, | |
276 | 5*HZ)) { | |
277 | dbg_hid("%s:timeout waiting for response\n", __func__); | |
278 | memset(response, 0, sizeof(struct hidpp_report)); | |
279 | ret = -ETIMEDOUT; | |
280 | } | |
281 | ||
282 | if (response->report_id == REPORT_ID_HIDPP_SHORT && | |
f677bb15 PW |
283 | response->rap.sub_id == HIDPP_ERROR) { |
284 | ret = response->rap.params[1]; | |
285 | dbg_hid("%s:got hidpp error %02X\n", __func__, ret); | |
286 | goto exit; | |
287 | } | |
288 | ||
a5ce8f5b SW |
289 | if ((response->report_id == REPORT_ID_HIDPP_LONG || |
290 | response->report_id == REPORT_ID_HIDPP_VERY_LONG) && | |
291 | response->fap.feature_index == HIDPP20_ERROR) { | |
2f31c525 | 292 | ret = response->fap.params[1]; |
f677bb15 | 293 | dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret); |
2f31c525 BT |
294 | goto exit; |
295 | } | |
296 | ||
297 | exit: | |
298 | mutex_unlock(&hidpp->send_mutex); | |
299 | return ret; | |
300 | ||
301 | } | |
302 | ||
303 | static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp, | |
304 | u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count, | |
305 | struct hidpp_report *response) | |
306 | { | |
3e7830ce | 307 | struct hidpp_report *message; |
2f31c525 BT |
308 | int ret; |
309 | ||
310 | if (param_count > sizeof(message->fap.params)) | |
311 | return -EINVAL; | |
312 | ||
3e7830ce DC |
313 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
314 | if (!message) | |
315 | return -ENOMEM; | |
a5ce8f5b SW |
316 | |
317 | if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4)) | |
318 | message->report_id = REPORT_ID_HIDPP_VERY_LONG; | |
319 | else | |
320 | message->report_id = REPORT_ID_HIDPP_LONG; | |
2f31c525 BT |
321 | message->fap.feature_index = feat_index; |
322 | message->fap.funcindex_clientid = funcindex_clientid; | |
323 | memcpy(&message->fap.params, params, param_count); | |
324 | ||
325 | ret = hidpp_send_message_sync(hidpp, message, response); | |
326 | kfree(message); | |
327 | return ret; | |
328 | } | |
329 | ||
33797820 BT |
330 | static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev, |
331 | u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count, | |
332 | struct hidpp_report *response) | |
333 | { | |
3e7830ce | 334 | struct hidpp_report *message; |
a5ce8f5b | 335 | int ret, max_count; |
33797820 | 336 | |
a5ce8f5b SW |
337 | switch (report_id) { |
338 | case REPORT_ID_HIDPP_SHORT: | |
339 | max_count = HIDPP_REPORT_SHORT_LENGTH - 4; | |
340 | break; | |
341 | case REPORT_ID_HIDPP_LONG: | |
342 | max_count = HIDPP_REPORT_LONG_LENGTH - 4; | |
343 | break; | |
344 | case REPORT_ID_HIDPP_VERY_LONG: | |
345 | max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4; | |
346 | break; | |
347 | default: | |
33797820 | 348 | return -EINVAL; |
a5ce8f5b | 349 | } |
33797820 | 350 | |
a5ce8f5b | 351 | if (param_count > max_count) |
33797820 BT |
352 | return -EINVAL; |
353 | ||
3e7830ce DC |
354 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
355 | if (!message) | |
356 | return -ENOMEM; | |
33797820 BT |
357 | message->report_id = report_id; |
358 | message->rap.sub_id = sub_id; | |
359 | message->rap.reg_address = reg_address; | |
360 | memcpy(&message->rap.params, params, param_count); | |
361 | ||
362 | ret = hidpp_send_message_sync(hidpp_dev, message, response); | |
363 | kfree(message); | |
364 | return ret; | |
365 | } | |
366 | ||
c39e3d5f BT |
367 | static void delayed_work_cb(struct work_struct *work) |
368 | { | |
369 | struct hidpp_device *hidpp = container_of(work, struct hidpp_device, | |
370 | work); | |
371 | hidpp_connect_event(hidpp); | |
372 | } | |
373 | ||
2f31c525 BT |
374 | static inline bool hidpp_match_answer(struct hidpp_report *question, |
375 | struct hidpp_report *answer) | |
376 | { | |
377 | return (answer->fap.feature_index == question->fap.feature_index) && | |
378 | (answer->fap.funcindex_clientid == question->fap.funcindex_clientid); | |
379 | } | |
380 | ||
381 | static inline bool hidpp_match_error(struct hidpp_report *question, | |
382 | struct hidpp_report *answer) | |
383 | { | |
f677bb15 PW |
384 | return ((answer->rap.sub_id == HIDPP_ERROR) || |
385 | (answer->fap.feature_index == HIDPP20_ERROR)) && | |
2f31c525 BT |
386 | (answer->fap.funcindex_clientid == question->fap.feature_index) && |
387 | (answer->fap.params[0] == question->fap.funcindex_clientid); | |
388 | } | |
389 | ||
c39e3d5f BT |
390 | static inline bool hidpp_report_is_connect_event(struct hidpp_report *report) |
391 | { | |
392 | return (report->report_id == REPORT_ID_HIDPP_SHORT) && | |
393 | (report->rap.sub_id == 0x41); | |
394 | } | |
395 | ||
a0e625f8 BT |
396 | /** |
397 | * hidpp_prefix_name() prefixes the current given name with "Logitech ". | |
398 | */ | |
399 | static void hidpp_prefix_name(char **name, int name_length) | |
400 | { | |
401 | #define PREFIX_LENGTH 9 /* "Logitech " */ | |
402 | ||
403 | int new_length; | |
404 | char *new_name; | |
405 | ||
406 | if (name_length > PREFIX_LENGTH && | |
407 | strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0) | |
408 | /* The prefix has is already in the name */ | |
409 | return; | |
410 | ||
411 | new_length = PREFIX_LENGTH + name_length; | |
412 | new_name = kzalloc(new_length, GFP_KERNEL); | |
413 | if (!new_name) | |
414 | return; | |
415 | ||
416 | snprintf(new_name, new_length, "Logitech %s", *name); | |
417 | ||
418 | kfree(*name); | |
419 | ||
420 | *name = new_name; | |
421 | } | |
422 | ||
4435ff2f HC |
423 | /** |
424 | * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll | |
425 | * events given a high-resolution wheel | |
426 | * movement. | |
427 | * @counter: a hid_scroll_counter struct describing the wheel. | |
428 | * @hi_res_value: the movement of the wheel, in the mouse's high-resolution | |
429 | * units. | |
430 | * | |
431 | * Given a high-resolution movement, this function converts the movement into | |
432 | * fractions of 120 and emits high-resolution scroll events for the input | |
433 | * device. It also uses the multiplier from &struct hid_scroll_counter to | |
434 | * emit low-resolution scroll events when appropriate for | |
435 | * backwards-compatibility with userspace input libraries. | |
436 | */ | |
437 | static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter *counter, | |
438 | int hi_res_value) | |
439 | { | |
440 | int low_res_value, remainder, direction; | |
441 | unsigned long long now, previous; | |
442 | ||
443 | hi_res_value = hi_res_value * 120/counter->wheel_multiplier; | |
444 | input_report_rel(counter->dev, REL_WHEEL_HI_RES, hi_res_value); | |
445 | ||
446 | remainder = counter->remainder; | |
447 | direction = hi_res_value > 0 ? 1 : -1; | |
448 | ||
449 | now = sched_clock(); | |
450 | previous = counter->last_time; | |
451 | counter->last_time = now; | |
452 | /* | |
453 | * Reset the remainder after a period of inactivity or when the | |
454 | * direction changes. This prevents the REL_WHEEL emulation point | |
455 | * from sliding for devices that don't always provide the same | |
456 | * number of movements per detent. | |
457 | */ | |
458 | if (now - previous > 1000000000 || direction != counter->direction) | |
459 | remainder = 0; | |
460 | ||
461 | counter->direction = direction; | |
462 | remainder += hi_res_value; | |
463 | ||
464 | /* Some wheels will rest 7/8ths of a detent from the previous detent | |
465 | * after slow movement, so we want the threshold for low-res events to | |
466 | * be in the middle between two detents (e.g. after 4/8ths) as | |
467 | * opposed to on the detents themselves (8/8ths). | |
468 | */ | |
469 | if (abs(remainder) >= 60) { | |
470 | /* Add (or subtract) 1 because we want to trigger when the wheel | |
471 | * is half-way to the next detent (i.e. scroll 1 detent after a | |
472 | * 1/2 detent movement, 2 detents after a 1 1/2 detent movement, | |
473 | * etc.). | |
474 | */ | |
475 | low_res_value = remainder / 120; | |
476 | if (low_res_value == 0) | |
477 | low_res_value = (hi_res_value > 0 ? 1 : -1); | |
478 | input_report_rel(counter->dev, REL_WHEEL, low_res_value); | |
479 | remainder -= low_res_value * 120; | |
480 | } | |
481 | counter->remainder = remainder; | |
482 | } | |
483 | ||
33797820 BT |
484 | /* -------------------------------------------------------------------------- */ |
485 | /* HIDP++ 1.0 commands */ | |
486 | /* -------------------------------------------------------------------------- */ | |
487 | ||
488 | #define HIDPP_SET_REGISTER 0x80 | |
489 | #define HIDPP_GET_REGISTER 0x81 | |
490 | #define HIDPP_SET_LONG_REGISTER 0x82 | |
491 | #define HIDPP_GET_LONG_REGISTER 0x83 | |
492 | ||
95c3d002 HC |
493 | /** |
494 | * hidpp10_set_register_bit() - Sets a single bit in a HID++ 1.0 register. | |
495 | * @hidpp_dev: the device to set the register on. | |
496 | * @register_address: the address of the register to modify. | |
497 | * @byte: the byte of the register to modify. Should be less than 3. | |
498 | * Return: 0 if successful, otherwise a negative error code. | |
499 | */ | |
500 | static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev, | |
501 | u8 register_address, u8 byte, u8 bit) | |
7f7ce2a2 BT |
502 | { |
503 | struct hidpp_report response; | |
504 | int ret; | |
505 | u8 params[3] = { 0 }; | |
506 | ||
507 | ret = hidpp_send_rap_command_sync(hidpp_dev, | |
95c3d002 HC |
508 | REPORT_ID_HIDPP_SHORT, |
509 | HIDPP_GET_REGISTER, | |
510 | register_address, | |
511 | NULL, 0, &response); | |
7f7ce2a2 BT |
512 | if (ret) |
513 | return ret; | |
514 | ||
515 | memcpy(params, response.rap.params, 3); | |
516 | ||
95c3d002 | 517 | params[byte] |= BIT(bit); |
7f7ce2a2 BT |
518 | |
519 | return hidpp_send_rap_command_sync(hidpp_dev, | |
95c3d002 HC |
520 | REPORT_ID_HIDPP_SHORT, |
521 | HIDPP_SET_REGISTER, | |
522 | register_address, | |
523 | params, 3, &response); | |
524 | } | |
525 | ||
526 | ||
527 | #define HIDPP_REG_GENERAL 0x00 | |
528 | ||
529 | static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev) | |
530 | { | |
531 | return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_GENERAL, 0, 4); | |
532 | } | |
533 | ||
534 | #define HIDPP_REG_FEATURES 0x01 | |
535 | ||
536 | /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */ | |
537 | static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev) | |
538 | { | |
539 | return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_FEATURES, 0, 6); | |
7f7ce2a2 BT |
540 | } |
541 | ||
542 | #define HIDPP_REG_BATTERY_STATUS 0x07 | |
543 | ||
544 | static int hidpp10_battery_status_map_level(u8 param) | |
545 | { | |
546 | int level; | |
547 | ||
548 | switch (param) { | |
549 | case 1 ... 2: | |
550 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; | |
551 | break; | |
552 | case 3 ... 4: | |
553 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; | |
554 | break; | |
555 | case 5 ... 6: | |
556 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
557 | break; | |
558 | case 7: | |
559 | level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; | |
560 | break; | |
561 | default: | |
562 | level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; | |
563 | } | |
564 | ||
565 | return level; | |
566 | } | |
567 | ||
568 | static int hidpp10_battery_status_map_status(u8 param) | |
569 | { | |
570 | int status; | |
571 | ||
572 | switch (param) { | |
573 | case 0x00: | |
574 | /* discharging (in use) */ | |
575 | status = POWER_SUPPLY_STATUS_DISCHARGING; | |
576 | break; | |
577 | case 0x21: /* (standard) charging */ | |
578 | case 0x24: /* fast charging */ | |
579 | case 0x25: /* slow charging */ | |
580 | status = POWER_SUPPLY_STATUS_CHARGING; | |
581 | break; | |
582 | case 0x26: /* topping charge */ | |
583 | case 0x22: /* charge complete */ | |
584 | status = POWER_SUPPLY_STATUS_FULL; | |
585 | break; | |
586 | case 0x20: /* unknown */ | |
587 | status = POWER_SUPPLY_STATUS_UNKNOWN; | |
588 | break; | |
589 | /* | |
590 | * 0x01...0x1F = reserved (not charging) | |
591 | * 0x23 = charging error | |
592 | * 0x27..0xff = reserved | |
593 | */ | |
594 | default: | |
595 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
596 | break; | |
597 | } | |
598 | ||
599 | return status; | |
600 | } | |
601 | ||
602 | static int hidpp10_query_battery_status(struct hidpp_device *hidpp) | |
603 | { | |
604 | struct hidpp_report response; | |
605 | int ret, status; | |
606 | ||
607 | ret = hidpp_send_rap_command_sync(hidpp, | |
608 | REPORT_ID_HIDPP_SHORT, | |
609 | HIDPP_GET_REGISTER, | |
610 | HIDPP_REG_BATTERY_STATUS, | |
611 | NULL, 0, &response); | |
612 | if (ret) | |
613 | return ret; | |
614 | ||
615 | hidpp->battery.level = | |
616 | hidpp10_battery_status_map_level(response.rap.params[0]); | |
617 | status = hidpp10_battery_status_map_status(response.rap.params[1]); | |
618 | hidpp->battery.status = status; | |
619 | /* the capacity is only available when discharging or full */ | |
620 | hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || | |
621 | status == POWER_SUPPLY_STATUS_FULL; | |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
626 | #define HIDPP_REG_BATTERY_MILEAGE 0x0D | |
627 | ||
628 | static int hidpp10_battery_mileage_map_status(u8 param) | |
629 | { | |
630 | int status; | |
631 | ||
632 | switch (param >> 6) { | |
633 | case 0x00: | |
634 | /* discharging (in use) */ | |
635 | status = POWER_SUPPLY_STATUS_DISCHARGING; | |
636 | break; | |
637 | case 0x01: /* charging */ | |
638 | status = POWER_SUPPLY_STATUS_CHARGING; | |
639 | break; | |
640 | case 0x02: /* charge complete */ | |
641 | status = POWER_SUPPLY_STATUS_FULL; | |
642 | break; | |
643 | /* | |
644 | * 0x03 = charging error | |
645 | */ | |
646 | default: | |
647 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
648 | break; | |
649 | } | |
650 | ||
651 | return status; | |
652 | } | |
653 | ||
654 | static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp) | |
655 | { | |
656 | struct hidpp_report response; | |
657 | int ret, status; | |
658 | ||
659 | ret = hidpp_send_rap_command_sync(hidpp, | |
660 | REPORT_ID_HIDPP_SHORT, | |
661 | HIDPP_GET_REGISTER, | |
662 | HIDPP_REG_BATTERY_MILEAGE, | |
663 | NULL, 0, &response); | |
664 | if (ret) | |
665 | return ret; | |
666 | ||
667 | hidpp->battery.capacity = response.rap.params[0]; | |
668 | status = hidpp10_battery_mileage_map_status(response.rap.params[2]); | |
669 | hidpp->battery.status = status; | |
670 | /* the capacity is only available when discharging or full */ | |
671 | hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || | |
672 | status == POWER_SUPPLY_STATUS_FULL; | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
677 | static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size) | |
678 | { | |
679 | struct hidpp_report *report = (struct hidpp_report *)data; | |
680 | int status, capacity, level; | |
681 | bool changed; | |
682 | ||
683 | if (report->report_id != REPORT_ID_HIDPP_SHORT) | |
684 | return 0; | |
685 | ||
686 | switch (report->rap.sub_id) { | |
687 | case HIDPP_REG_BATTERY_STATUS: | |
688 | capacity = hidpp->battery.capacity; | |
689 | level = hidpp10_battery_status_map_level(report->rawbytes[1]); | |
690 | status = hidpp10_battery_status_map_status(report->rawbytes[2]); | |
691 | break; | |
692 | case HIDPP_REG_BATTERY_MILEAGE: | |
693 | capacity = report->rap.params[0]; | |
694 | level = hidpp->battery.level; | |
695 | status = hidpp10_battery_mileage_map_status(report->rawbytes[3]); | |
696 | break; | |
697 | default: | |
698 | return 0; | |
699 | } | |
700 | ||
701 | changed = capacity != hidpp->battery.capacity || | |
702 | level != hidpp->battery.level || | |
703 | status != hidpp->battery.status; | |
704 | ||
705 | /* the capacity is only available when discharging or full */ | |
706 | hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || | |
707 | status == POWER_SUPPLY_STATUS_FULL; | |
708 | ||
709 | if (changed) { | |
710 | hidpp->battery.level = level; | |
711 | hidpp->battery.status = status; | |
712 | if (hidpp->battery.ps) | |
713 | power_supply_changed(hidpp->battery.ps); | |
714 | } | |
715 | ||
716 | return 0; | |
717 | } | |
718 | ||
33797820 | 719 | #define HIDPP_REG_PAIRING_INFORMATION 0xB5 |
843c624e BT |
720 | #define HIDPP_EXTENDED_PAIRING 0x30 |
721 | #define HIDPP_DEVICE_NAME 0x40 | |
33797820 | 722 | |
843c624e | 723 | static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev) |
33797820 BT |
724 | { |
725 | struct hidpp_report response; | |
726 | int ret; | |
843c624e | 727 | u8 params[1] = { HIDPP_DEVICE_NAME }; |
33797820 BT |
728 | char *name; |
729 | int len; | |
730 | ||
731 | ret = hidpp_send_rap_command_sync(hidpp_dev, | |
732 | REPORT_ID_HIDPP_SHORT, | |
733 | HIDPP_GET_LONG_REGISTER, | |
734 | HIDPP_REG_PAIRING_INFORMATION, | |
735 | params, 1, &response); | |
736 | if (ret) | |
737 | return NULL; | |
738 | ||
739 | len = response.rap.params[1]; | |
740 | ||
3a034a7a PW |
741 | if (2 + len > sizeof(response.rap.params)) |
742 | return NULL; | |
743 | ||
33797820 BT |
744 | name = kzalloc(len + 1, GFP_KERNEL); |
745 | if (!name) | |
746 | return NULL; | |
747 | ||
748 | memcpy(name, &response.rap.params[2], len); | |
a0e625f8 BT |
749 | |
750 | /* include the terminating '\0' */ | |
751 | hidpp_prefix_name(&name, len + 1); | |
752 | ||
33797820 BT |
753 | return name; |
754 | } | |
755 | ||
843c624e BT |
756 | static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial) |
757 | { | |
758 | struct hidpp_report response; | |
759 | int ret; | |
760 | u8 params[1] = { HIDPP_EXTENDED_PAIRING }; | |
761 | ||
762 | ret = hidpp_send_rap_command_sync(hidpp, | |
763 | REPORT_ID_HIDPP_SHORT, | |
764 | HIDPP_GET_LONG_REGISTER, | |
765 | HIDPP_REG_PAIRING_INFORMATION, | |
766 | params, 1, &response); | |
767 | if (ret) | |
768 | return ret; | |
769 | ||
770 | /* | |
771 | * We don't care about LE or BE, we will output it as a string | |
772 | * with %4phD, so we need to keep the order. | |
773 | */ | |
774 | *serial = *((u32 *)&response.rap.params[1]); | |
775 | return 0; | |
776 | } | |
777 | ||
778 | static int hidpp_unifying_init(struct hidpp_device *hidpp) | |
779 | { | |
780 | struct hid_device *hdev = hidpp->hid_dev; | |
781 | const char *name; | |
782 | u32 serial; | |
783 | int ret; | |
784 | ||
785 | ret = hidpp_unifying_get_serial(hidpp, &serial); | |
786 | if (ret) | |
787 | return ret; | |
788 | ||
789 | snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD", | |
790 | hdev->product, &serial); | |
791 | dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq); | |
792 | ||
793 | name = hidpp_unifying_get_name(hidpp); | |
794 | if (!name) | |
795 | return -EIO; | |
796 | ||
797 | snprintf(hdev->name, sizeof(hdev->name), "%s", name); | |
798 | dbg_hid("HID++ Unifying: Got name: %s\n", name); | |
799 | ||
800 | kfree(name); | |
801 | return 0; | |
802 | } | |
803 | ||
2f31c525 BT |
804 | /* -------------------------------------------------------------------------- */ |
805 | /* 0x0000: Root */ | |
806 | /* -------------------------------------------------------------------------- */ | |
807 | ||
808 | #define HIDPP_PAGE_ROOT 0x0000 | |
809 | #define HIDPP_PAGE_ROOT_IDX 0x00 | |
810 | ||
811 | #define CMD_ROOT_GET_FEATURE 0x01 | |
812 | #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11 | |
813 | ||
814 | static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature, | |
815 | u8 *feature_index, u8 *feature_type) | |
816 | { | |
817 | struct hidpp_report response; | |
818 | int ret; | |
819 | u8 params[2] = { feature >> 8, feature & 0x00FF }; | |
820 | ||
821 | ret = hidpp_send_fap_command_sync(hidpp, | |
822 | HIDPP_PAGE_ROOT_IDX, | |
823 | CMD_ROOT_GET_FEATURE, | |
824 | params, 2, &response); | |
825 | if (ret) | |
826 | return ret; | |
827 | ||
a9525b80 BT |
828 | if (response.fap.params[0] == 0) |
829 | return -ENOENT; | |
830 | ||
2f31c525 BT |
831 | *feature_index = response.fap.params[0]; |
832 | *feature_type = response.fap.params[1]; | |
833 | ||
834 | return ret; | |
835 | } | |
836 | ||
837 | static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp) | |
838 | { | |
839 | struct hidpp_report response; | |
840 | int ret; | |
841 | ||
842 | ret = hidpp_send_fap_command_sync(hidpp, | |
843 | HIDPP_PAGE_ROOT_IDX, | |
844 | CMD_ROOT_GET_PROTOCOL_VERSION, | |
845 | NULL, 0, &response); | |
846 | ||
552f12eb | 847 | if (ret == HIDPP_ERROR_INVALID_SUBID) { |
2f31c525 BT |
848 | hidpp->protocol_major = 1; |
849 | hidpp->protocol_minor = 0; | |
9576af6a | 850 | goto print_version; |
2f31c525 BT |
851 | } |
852 | ||
552f12eb BT |
853 | /* the device might not be connected */ |
854 | if (ret == HIDPP_ERROR_RESOURCE_ERROR) | |
855 | return -EIO; | |
856 | ||
8c9952b2 BT |
857 | if (ret > 0) { |
858 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
859 | __func__, ret); | |
860 | return -EPROTO; | |
861 | } | |
2f31c525 | 862 | if (ret) |
8c9952b2 | 863 | return ret; |
2f31c525 BT |
864 | |
865 | hidpp->protocol_major = response.fap.params[0]; | |
866 | hidpp->protocol_minor = response.fap.params[1]; | |
867 | ||
9576af6a HG |
868 | print_version: |
869 | hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n", | |
870 | hidpp->protocol_major, hidpp->protocol_minor); | |
871 | return 0; | |
2f31c525 BT |
872 | } |
873 | ||
2f31c525 BT |
874 | /* -------------------------------------------------------------------------- */ |
875 | /* 0x0005: GetDeviceNameType */ | |
876 | /* -------------------------------------------------------------------------- */ | |
877 | ||
878 | #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005 | |
879 | ||
880 | #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01 | |
881 | #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11 | |
882 | #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21 | |
883 | ||
884 | static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp, | |
885 | u8 feature_index, u8 *nameLength) | |
886 | { | |
887 | struct hidpp_report response; | |
888 | int ret; | |
889 | ||
890 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
891 | CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response); | |
892 | ||
8c9952b2 BT |
893 | if (ret > 0) { |
894 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
895 | __func__, ret); | |
896 | return -EPROTO; | |
897 | } | |
2f31c525 | 898 | if (ret) |
8c9952b2 | 899 | return ret; |
2f31c525 BT |
900 | |
901 | *nameLength = response.fap.params[0]; | |
902 | ||
903 | return ret; | |
904 | } | |
905 | ||
906 | static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp, | |
907 | u8 feature_index, u8 char_index, char *device_name, int len_buf) | |
908 | { | |
909 | struct hidpp_report response; | |
910 | int ret, i; | |
911 | int count; | |
912 | ||
913 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
914 | CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1, | |
915 | &response); | |
916 | ||
8c9952b2 BT |
917 | if (ret > 0) { |
918 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
919 | __func__, ret); | |
920 | return -EPROTO; | |
921 | } | |
2f31c525 | 922 | if (ret) |
8c9952b2 | 923 | return ret; |
2f31c525 | 924 | |
a5ce8f5b SW |
925 | switch (response.report_id) { |
926 | case REPORT_ID_HIDPP_VERY_LONG: | |
927 | count = HIDPP_REPORT_VERY_LONG_LENGTH - 4; | |
928 | break; | |
929 | case REPORT_ID_HIDPP_LONG: | |
2f31c525 | 930 | count = HIDPP_REPORT_LONG_LENGTH - 4; |
a5ce8f5b SW |
931 | break; |
932 | case REPORT_ID_HIDPP_SHORT: | |
2f31c525 | 933 | count = HIDPP_REPORT_SHORT_LENGTH - 4; |
a5ce8f5b SW |
934 | break; |
935 | default: | |
936 | return -EPROTO; | |
937 | } | |
2f31c525 BT |
938 | |
939 | if (len_buf < count) | |
940 | count = len_buf; | |
941 | ||
942 | for (i = 0; i < count; i++) | |
943 | device_name[i] = response.fap.params[i]; | |
944 | ||
945 | return count; | |
946 | } | |
947 | ||
02cc097e | 948 | static char *hidpp_get_device_name(struct hidpp_device *hidpp) |
2f31c525 BT |
949 | { |
950 | u8 feature_type; | |
951 | u8 feature_index; | |
952 | u8 __name_length; | |
953 | char *name; | |
954 | unsigned index = 0; | |
955 | int ret; | |
956 | ||
957 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE, | |
958 | &feature_index, &feature_type); | |
959 | if (ret) | |
02cc097e | 960 | return NULL; |
2f31c525 BT |
961 | |
962 | ret = hidpp_devicenametype_get_count(hidpp, feature_index, | |
963 | &__name_length); | |
964 | if (ret) | |
02cc097e | 965 | return NULL; |
2f31c525 BT |
966 | |
967 | name = kzalloc(__name_length + 1, GFP_KERNEL); | |
968 | if (!name) | |
02cc097e | 969 | return NULL; |
2f31c525 | 970 | |
1430ee73 PW |
971 | while (index < __name_length) { |
972 | ret = hidpp_devicenametype_get_device_name(hidpp, | |
2f31c525 BT |
973 | feature_index, index, name + index, |
974 | __name_length - index); | |
1430ee73 PW |
975 | if (ret <= 0) { |
976 | kfree(name); | |
977 | return NULL; | |
978 | } | |
979 | index += ret; | |
980 | } | |
2f31c525 | 981 | |
a0e625f8 BT |
982 | /* include the terminating '\0' */ |
983 | hidpp_prefix_name(&name, __name_length + 1); | |
984 | ||
2f31c525 | 985 | return name; |
2f31c525 BT |
986 | } |
987 | ||
5a2b190c PH |
988 | /* -------------------------------------------------------------------------- */ |
989 | /* 0x1000: Battery level status */ | |
990 | /* -------------------------------------------------------------------------- */ | |
991 | ||
992 | #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000 | |
993 | ||
994 | #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00 | |
995 | #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10 | |
996 | ||
997 | #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00 | |
998 | ||
5b036ea1 BT |
999 | #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0) |
1000 | #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1) | |
1001 | #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2) | |
1002 | ||
1003 | static int hidpp_map_battery_level(int capacity) | |
1004 | { | |
1005 | if (capacity < 11) | |
1006 | return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; | |
1f87b0cd HG |
1007 | /* |
1008 | * The spec says this should be < 31 but some devices report 30 | |
1009 | * with brand new batteries and Windows reports 30 as "Good". | |
1010 | */ | |
1011 | else if (capacity < 30) | |
5b036ea1 BT |
1012 | return POWER_SUPPLY_CAPACITY_LEVEL_LOW; |
1013 | else if (capacity < 81) | |
1014 | return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
1015 | return POWER_SUPPLY_CAPACITY_LEVEL_FULL; | |
1016 | } | |
1017 | ||
14f437a1 | 1018 | static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity, |
5b036ea1 BT |
1019 | int *next_capacity, |
1020 | int *level) | |
5a2b190c PH |
1021 | { |
1022 | int status; | |
5a2b190c | 1023 | |
14f437a1 BT |
1024 | *capacity = data[0]; |
1025 | *next_capacity = data[1]; | |
5b036ea1 | 1026 | *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; |
5a2b190c | 1027 | |
14f437a1 BT |
1028 | /* When discharging, we can rely on the device reported capacity. |
1029 | * For all other states the device reports 0 (unknown). | |
5a2b190c PH |
1030 | */ |
1031 | switch (data[2]) { | |
1032 | case 0: /* discharging (in use) */ | |
1033 | status = POWER_SUPPLY_STATUS_DISCHARGING; | |
5b036ea1 | 1034 | *level = hidpp_map_battery_level(*capacity); |
5a2b190c PH |
1035 | break; |
1036 | case 1: /* recharging */ | |
1037 | status = POWER_SUPPLY_STATUS_CHARGING; | |
5a2b190c PH |
1038 | break; |
1039 | case 2: /* charge in final stage */ | |
1040 | status = POWER_SUPPLY_STATUS_CHARGING; | |
5a2b190c PH |
1041 | break; |
1042 | case 3: /* charge complete */ | |
1043 | status = POWER_SUPPLY_STATUS_FULL; | |
5b036ea1 | 1044 | *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
14f437a1 | 1045 | *capacity = 100; |
5a2b190c PH |
1046 | break; |
1047 | case 4: /* recharging below optimal speed */ | |
1048 | status = POWER_SUPPLY_STATUS_CHARGING; | |
5a2b190c PH |
1049 | break; |
1050 | /* 5 = invalid battery type | |
1051 | 6 = thermal error | |
1052 | 7 = other charging error */ | |
1053 | default: | |
1054 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
5a2b190c PH |
1055 | break; |
1056 | } | |
1057 | ||
5a2b190c PH |
1058 | return status; |
1059 | } | |
1060 | ||
14f437a1 BT |
1061 | static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp, |
1062 | u8 feature_index, | |
1063 | int *status, | |
1064 | int *capacity, | |
5b036ea1 BT |
1065 | int *next_capacity, |
1066 | int *level) | |
5a2b190c PH |
1067 | { |
1068 | struct hidpp_report response; | |
1069 | int ret; | |
1070 | u8 *params = (u8 *)response.fap.params; | |
1071 | ||
1072 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
1073 | CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS, | |
1074 | NULL, 0, &response); | |
1075 | if (ret > 0) { | |
1076 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
1077 | __func__, ret); | |
1078 | return -EPROTO; | |
1079 | } | |
1080 | if (ret) | |
1081 | return ret; | |
1082 | ||
14f437a1 | 1083 | *status = hidpp20_batterylevel_map_status_capacity(params, capacity, |
5b036ea1 BT |
1084 | next_capacity, |
1085 | level); | |
1086 | ||
1087 | return 0; | |
1088 | } | |
1089 | ||
1090 | static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp, | |
1091 | u8 feature_index) | |
1092 | { | |
1093 | struct hidpp_report response; | |
1094 | int ret; | |
1095 | u8 *params = (u8 *)response.fap.params; | |
1096 | unsigned int level_count, flags; | |
1097 | ||
1098 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
1099 | CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY, | |
1100 | NULL, 0, &response); | |
1101 | if (ret > 0) { | |
1102 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
1103 | __func__, ret); | |
1104 | return -EPROTO; | |
1105 | } | |
1106 | if (ret) | |
1107 | return ret; | |
1108 | ||
1109 | level_count = params[0]; | |
1110 | flags = params[1]; | |
1111 | ||
1112 | if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE)) | |
1113 | hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS; | |
1114 | else | |
1115 | hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE; | |
5a2b190c PH |
1116 | |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | static int hidpp20_query_battery_info(struct hidpp_device *hidpp) | |
1121 | { | |
1122 | u8 feature_type; | |
1123 | int ret; | |
5b036ea1 | 1124 | int status, capacity, next_capacity, level; |
5a2b190c | 1125 | |
696ecef9 | 1126 | if (hidpp->battery.feature_index == 0xff) { |
5a2b190c PH |
1127 | ret = hidpp_root_get_feature(hidpp, |
1128 | HIDPP_PAGE_BATTERY_LEVEL_STATUS, | |
1129 | &hidpp->battery.feature_index, | |
1130 | &feature_type); | |
1131 | if (ret) | |
1132 | return ret; | |
1133 | } | |
1134 | ||
14f437a1 BT |
1135 | ret = hidpp20_batterylevel_get_battery_capacity(hidpp, |
1136 | hidpp->battery.feature_index, | |
1137 | &status, &capacity, | |
5b036ea1 BT |
1138 | &next_capacity, &level); |
1139 | if (ret) | |
1140 | return ret; | |
1141 | ||
1142 | ret = hidpp20_batterylevel_get_battery_info(hidpp, | |
1143 | hidpp->battery.feature_index); | |
5a2b190c PH |
1144 | if (ret) |
1145 | return ret; | |
1146 | ||
1147 | hidpp->battery.status = status; | |
14f437a1 | 1148 | hidpp->battery.capacity = capacity; |
5b036ea1 | 1149 | hidpp->battery.level = level; |
284f8d75 BT |
1150 | /* the capacity is only available when discharging or full */ |
1151 | hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || | |
1152 | status == POWER_SUPPLY_STATUS_FULL; | |
5a2b190c PH |
1153 | |
1154 | return 0; | |
1155 | } | |
1156 | ||
1157 | static int hidpp20_battery_event(struct hidpp_device *hidpp, | |
1158 | u8 *data, int size) | |
1159 | { | |
1160 | struct hidpp_report *report = (struct hidpp_report *)data; | |
5b036ea1 | 1161 | int status, capacity, next_capacity, level; |
5a2b190c PH |
1162 | bool changed; |
1163 | ||
1164 | if (report->fap.feature_index != hidpp->battery.feature_index || | |
1165 | report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST) | |
1166 | return 0; | |
1167 | ||
14f437a1 BT |
1168 | status = hidpp20_batterylevel_map_status_capacity(report->fap.params, |
1169 | &capacity, | |
5b036ea1 BT |
1170 | &next_capacity, |
1171 | &level); | |
5a2b190c | 1172 | |
284f8d75 BT |
1173 | /* the capacity is only available when discharging or full */ |
1174 | hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || | |
1175 | status == POWER_SUPPLY_STATUS_FULL; | |
1176 | ||
14f437a1 | 1177 | changed = capacity != hidpp->battery.capacity || |
5b036ea1 | 1178 | level != hidpp->battery.level || |
5a2b190c PH |
1179 | status != hidpp->battery.status; |
1180 | ||
1181 | if (changed) { | |
5b036ea1 | 1182 | hidpp->battery.level = level; |
14f437a1 | 1183 | hidpp->battery.capacity = capacity; |
5a2b190c PH |
1184 | hidpp->battery.status = status; |
1185 | if (hidpp->battery.ps) | |
1186 | power_supply_changed(hidpp->battery.ps); | |
1187 | } | |
1188 | ||
1189 | return 0; | |
1190 | } | |
1191 | ||
1192 | static enum power_supply_property hidpp_battery_props[] = { | |
284f8d75 | 1193 | POWER_SUPPLY_PROP_ONLINE, |
5a2b190c | 1194 | POWER_SUPPLY_PROP_STATUS, |
3861e6ca | 1195 | POWER_SUPPLY_PROP_SCOPE, |
32043d0f BT |
1196 | POWER_SUPPLY_PROP_MODEL_NAME, |
1197 | POWER_SUPPLY_PROP_MANUFACTURER, | |
1198 | POWER_SUPPLY_PROP_SERIAL_NUMBER, | |
5b036ea1 BT |
1199 | 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */ |
1200 | 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */ | |
5a2b190c PH |
1201 | }; |
1202 | ||
1203 | static int hidpp_battery_get_property(struct power_supply *psy, | |
1204 | enum power_supply_property psp, | |
1205 | union power_supply_propval *val) | |
1206 | { | |
1207 | struct hidpp_device *hidpp = power_supply_get_drvdata(psy); | |
1208 | int ret = 0; | |
1209 | ||
1210 | switch(psp) { | |
1211 | case POWER_SUPPLY_PROP_STATUS: | |
1212 | val->intval = hidpp->battery.status; | |
1213 | break; | |
1214 | case POWER_SUPPLY_PROP_CAPACITY: | |
14f437a1 | 1215 | val->intval = hidpp->battery.capacity; |
5a2b190c | 1216 | break; |
5b036ea1 BT |
1217 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
1218 | val->intval = hidpp->battery.level; | |
1219 | break; | |
3861e6ca BN |
1220 | case POWER_SUPPLY_PROP_SCOPE: |
1221 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; | |
1222 | break; | |
284f8d75 BT |
1223 | case POWER_SUPPLY_PROP_ONLINE: |
1224 | val->intval = hidpp->battery.online; | |
1225 | break; | |
32043d0f BT |
1226 | case POWER_SUPPLY_PROP_MODEL_NAME: |
1227 | if (!strncmp(hidpp->name, "Logitech ", 9)) | |
1228 | val->strval = hidpp->name + 9; | |
1229 | else | |
1230 | val->strval = hidpp->name; | |
1231 | break; | |
1232 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
1233 | val->strval = "Logitech"; | |
1234 | break; | |
1235 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: | |
1236 | val->strval = hidpp->hid_dev->uniq; | |
1237 | break; | |
5a2b190c PH |
1238 | default: |
1239 | ret = -EINVAL; | |
1240 | break; | |
1241 | } | |
1242 | ||
1243 | return ret; | |
1244 | } | |
1245 | ||
4435ff2f HC |
1246 | /* -------------------------------------------------------------------------- */ |
1247 | /* 0x2120: Hi-resolution scrolling */ | |
1248 | /* -------------------------------------------------------------------------- */ | |
1249 | ||
1250 | #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120 | |
1251 | ||
1252 | #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10 | |
1253 | ||
1254 | static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp, | |
1255 | bool enabled, u8 *multiplier) | |
1256 | { | |
1257 | u8 feature_index; | |
1258 | u8 feature_type; | |
1259 | int ret; | |
1260 | u8 params[1]; | |
1261 | struct hidpp_report response; | |
1262 | ||
1263 | ret = hidpp_root_get_feature(hidpp, | |
1264 | HIDPP_PAGE_HI_RESOLUTION_SCROLLING, | |
1265 | &feature_index, | |
1266 | &feature_type); | |
1267 | if (ret) | |
1268 | return ret; | |
1269 | ||
1270 | params[0] = enabled ? BIT(0) : 0; | |
1271 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
1272 | CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE, | |
1273 | params, sizeof(params), &response); | |
1274 | if (ret) | |
1275 | return ret; | |
1276 | *multiplier = response.fap.params[1]; | |
1277 | return 0; | |
1278 | } | |
1279 | ||
1280 | /* -------------------------------------------------------------------------- */ | |
1281 | /* 0x2121: HiRes Wheel */ | |
1282 | /* -------------------------------------------------------------------------- */ | |
1283 | ||
1284 | #define HIDPP_PAGE_HIRES_WHEEL 0x2121 | |
1285 | ||
1286 | #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00 | |
1287 | #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20 | |
1288 | ||
1289 | static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp, | |
1290 | u8 *multiplier) | |
1291 | { | |
1292 | u8 feature_index; | |
1293 | u8 feature_type; | |
1294 | int ret; | |
1295 | struct hidpp_report response; | |
1296 | ||
1297 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL, | |
1298 | &feature_index, &feature_type); | |
1299 | if (ret) | |
1300 | goto return_default; | |
1301 | ||
1302 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
1303 | CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY, | |
1304 | NULL, 0, &response); | |
1305 | if (ret) | |
1306 | goto return_default; | |
1307 | ||
1308 | *multiplier = response.fap.params[0]; | |
1309 | return 0; | |
1310 | return_default: | |
1311 | hid_warn(hidpp->hid_dev, | |
1312 | "Couldn't get wheel multiplier (error %d)\n", ret); | |
1313 | return ret; | |
1314 | } | |
1315 | ||
1316 | static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert, | |
1317 | bool high_resolution, bool use_hidpp) | |
1318 | { | |
1319 | u8 feature_index; | |
1320 | u8 feature_type; | |
1321 | int ret; | |
1322 | u8 params[1]; | |
1323 | struct hidpp_report response; | |
1324 | ||
1325 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL, | |
1326 | &feature_index, &feature_type); | |
1327 | if (ret) | |
1328 | return ret; | |
1329 | ||
1330 | params[0] = (invert ? BIT(2) : 0) | | |
1331 | (high_resolution ? BIT(1) : 0) | | |
1332 | (use_hidpp ? BIT(0) : 0); | |
1333 | ||
1334 | return hidpp_send_fap_command_sync(hidpp, feature_index, | |
1335 | CMD_HIRES_WHEEL_SET_WHEEL_MODE, | |
1336 | params, sizeof(params), &response); | |
1337 | } | |
1338 | ||
696ecef9 BT |
1339 | /* -------------------------------------------------------------------------- */ |
1340 | /* 0x4301: Solar Keyboard */ | |
1341 | /* -------------------------------------------------------------------------- */ | |
1342 | ||
1343 | #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301 | |
1344 | ||
1345 | #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00 | |
1346 | ||
1347 | #define EVENT_SOLAR_BATTERY_BROADCAST 0x00 | |
1348 | #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10 | |
1349 | #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20 | |
1350 | ||
1351 | static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp) | |
1352 | { | |
1353 | struct hidpp_report response; | |
1354 | u8 params[2] = { 1, 1 }; | |
1355 | u8 feature_type; | |
1356 | int ret; | |
1357 | ||
1358 | if (hidpp->battery.feature_index == 0xff) { | |
1359 | ret = hidpp_root_get_feature(hidpp, | |
1360 | HIDPP_PAGE_SOLAR_KEYBOARD, | |
1361 | &hidpp->battery.solar_feature_index, | |
1362 | &feature_type); | |
1363 | if (ret) | |
1364 | return ret; | |
1365 | } | |
1366 | ||
1367 | ret = hidpp_send_fap_command_sync(hidpp, | |
1368 | hidpp->battery.solar_feature_index, | |
1369 | CMD_SOLAR_SET_LIGHT_MEASURE, | |
1370 | params, 2, &response); | |
1371 | if (ret > 0) { | |
1372 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
1373 | __func__, ret); | |
1374 | return -EPROTO; | |
1375 | } | |
1376 | if (ret) | |
1377 | return ret; | |
1378 | ||
1379 | hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE; | |
1380 | ||
1381 | return 0; | |
1382 | } | |
1383 | ||
1384 | static int hidpp_solar_battery_event(struct hidpp_device *hidpp, | |
1385 | u8 *data, int size) | |
1386 | { | |
1387 | struct hidpp_report *report = (struct hidpp_report *)data; | |
1388 | int capacity, lux, status; | |
1389 | u8 function; | |
1390 | ||
1391 | function = report->fap.funcindex_clientid; | |
1392 | ||
1393 | ||
1394 | if (report->fap.feature_index != hidpp->battery.solar_feature_index || | |
1395 | !(function == EVENT_SOLAR_BATTERY_BROADCAST || | |
1396 | function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE || | |
1397 | function == EVENT_SOLAR_CHECK_LIGHT_BUTTON)) | |
1398 | return 0; | |
1399 | ||
1400 | capacity = report->fap.params[0]; | |
1401 | ||
1402 | switch (function) { | |
1403 | case EVENT_SOLAR_BATTERY_LIGHT_MEASURE: | |
1404 | lux = (report->fap.params[1] << 8) | report->fap.params[2]; | |
1405 | if (lux > 200) | |
1406 | status = POWER_SUPPLY_STATUS_CHARGING; | |
1407 | else | |
1408 | status = POWER_SUPPLY_STATUS_DISCHARGING; | |
1409 | break; | |
1410 | case EVENT_SOLAR_CHECK_LIGHT_BUTTON: | |
1411 | default: | |
1412 | if (capacity < hidpp->battery.capacity) | |
1413 | status = POWER_SUPPLY_STATUS_DISCHARGING; | |
1414 | else | |
1415 | status = POWER_SUPPLY_STATUS_CHARGING; | |
1416 | ||
1417 | } | |
1418 | ||
1419 | if (capacity == 100) | |
1420 | status = POWER_SUPPLY_STATUS_FULL; | |
1421 | ||
1422 | hidpp->battery.online = true; | |
1423 | if (capacity != hidpp->battery.capacity || | |
1424 | status != hidpp->battery.status) { | |
1425 | hidpp->battery.capacity = capacity; | |
1426 | hidpp->battery.status = status; | |
1427 | if (hidpp->battery.ps) | |
1428 | power_supply_changed(hidpp->battery.ps); | |
1429 | } | |
1430 | ||
1431 | return 0; | |
1432 | } | |
1433 | ||
90cdd986 BT |
1434 | /* -------------------------------------------------------------------------- */ |
1435 | /* 0x6010: Touchpad FW items */ | |
1436 | /* -------------------------------------------------------------------------- */ | |
1437 | ||
1438 | #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010 | |
1439 | ||
1440 | #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10 | |
1441 | ||
1442 | struct hidpp_touchpad_fw_items { | |
1443 | uint8_t presence; | |
1444 | uint8_t desired_state; | |
1445 | uint8_t state; | |
1446 | uint8_t persistent; | |
1447 | }; | |
1448 | ||
1449 | /** | |
1450 | * send a set state command to the device by reading the current items->state | |
1451 | * field. items is then filled with the current state. | |
1452 | */ | |
1453 | static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp, | |
1454 | u8 feature_index, | |
1455 | struct hidpp_touchpad_fw_items *items) | |
1456 | { | |
1457 | struct hidpp_report response; | |
1458 | int ret; | |
1459 | u8 *params = (u8 *)response.fap.params; | |
1460 | ||
1461 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
1462 | CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response); | |
1463 | ||
1464 | if (ret > 0) { | |
1465 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
1466 | __func__, ret); | |
1467 | return -EPROTO; | |
1468 | } | |
1469 | if (ret) | |
1470 | return ret; | |
1471 | ||
1472 | items->presence = params[0]; | |
1473 | items->desired_state = params[1]; | |
1474 | items->state = params[2]; | |
1475 | items->persistent = params[3]; | |
1476 | ||
1477 | return 0; | |
1478 | } | |
1479 | ||
2f31c525 BT |
1480 | /* -------------------------------------------------------------------------- */ |
1481 | /* 0x6100: TouchPadRawXY */ | |
1482 | /* -------------------------------------------------------------------------- */ | |
1483 | ||
1484 | #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100 | |
1485 | ||
1486 | #define CMD_TOUCHPAD_GET_RAW_INFO 0x01 | |
586bdc4e BT |
1487 | #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21 |
1488 | ||
1489 | #define EVENT_TOUCHPAD_RAW_XY 0x00 | |
2f31c525 BT |
1490 | |
1491 | #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01 | |
1492 | #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03 | |
1493 | ||
1494 | struct hidpp_touchpad_raw_info { | |
1495 | u16 x_size; | |
1496 | u16 y_size; | |
1497 | u8 z_range; | |
1498 | u8 area_range; | |
1499 | u8 timestamp_unit; | |
1500 | u8 maxcontacts; | |
1501 | u8 origin; | |
1502 | u16 res; | |
1503 | }; | |
1504 | ||
1505 | struct hidpp_touchpad_raw_xy_finger { | |
1506 | u8 contact_type; | |
1507 | u8 contact_status; | |
1508 | u16 x; | |
1509 | u16 y; | |
1510 | u8 z; | |
1511 | u8 area; | |
1512 | u8 finger_id; | |
1513 | }; | |
1514 | ||
1515 | struct hidpp_touchpad_raw_xy { | |
1516 | u16 timestamp; | |
1517 | struct hidpp_touchpad_raw_xy_finger fingers[2]; | |
1518 | u8 spurious_flag; | |
1519 | u8 end_of_frame; | |
1520 | u8 finger_count; | |
1521 | u8 button; | |
1522 | }; | |
1523 | ||
1524 | static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp, | |
1525 | u8 feature_index, struct hidpp_touchpad_raw_info *raw_info) | |
1526 | { | |
1527 | struct hidpp_report response; | |
1528 | int ret; | |
1529 | u8 *params = (u8 *)response.fap.params; | |
1530 | ||
1531 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, | |
1532 | CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response); | |
1533 | ||
8c9952b2 BT |
1534 | if (ret > 0) { |
1535 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
1536 | __func__, ret); | |
1537 | return -EPROTO; | |
1538 | } | |
2f31c525 | 1539 | if (ret) |
8c9952b2 | 1540 | return ret; |
2f31c525 BT |
1541 | |
1542 | raw_info->x_size = get_unaligned_be16(¶ms[0]); | |
1543 | raw_info->y_size = get_unaligned_be16(¶ms[2]); | |
1544 | raw_info->z_range = params[4]; | |
1545 | raw_info->area_range = params[5]; | |
1546 | raw_info->maxcontacts = params[7]; | |
1547 | raw_info->origin = params[8]; | |
1548 | /* res is given in unit per inch */ | |
1549 | raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51; | |
1550 | ||
1551 | return ret; | |
1552 | } | |
1553 | ||
586bdc4e BT |
1554 | static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev, |
1555 | u8 feature_index, bool send_raw_reports, | |
1556 | bool sensor_enhanced_settings) | |
1557 | { | |
1558 | struct hidpp_report response; | |
1559 | ||
1560 | /* | |
1561 | * Params: | |
1562 | * bit 0 - enable raw | |
1563 | * bit 1 - 16bit Z, no area | |
1564 | * bit 2 - enhanced sensitivity | |
1565 | * bit 3 - width, height (4 bits each) instead of area | |
1566 | * bit 4 - send raw + gestures (degrades smoothness) | |
1567 | * remaining bits - reserved | |
1568 | */ | |
1569 | u8 params = send_raw_reports | (sensor_enhanced_settings << 2); | |
1570 | ||
1571 | return hidpp_send_fap_command_sync(hidpp_dev, feature_index, | |
1572 | CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response); | |
1573 | } | |
1574 | ||
1575 | static void hidpp_touchpad_touch_event(u8 *data, | |
1576 | struct hidpp_touchpad_raw_xy_finger *finger) | |
1577 | { | |
1578 | u8 x_m = data[0] << 2; | |
1579 | u8 y_m = data[2] << 2; | |
1580 | ||
1581 | finger->x = x_m << 6 | data[1]; | |
1582 | finger->y = y_m << 6 | data[3]; | |
1583 | ||
1584 | finger->contact_type = data[0] >> 6; | |
1585 | finger->contact_status = data[2] >> 6; | |
1586 | ||
1587 | finger->z = data[4]; | |
1588 | finger->area = data[5]; | |
1589 | finger->finger_id = data[6] >> 4; | |
1590 | } | |
1591 | ||
1592 | static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev, | |
1593 | u8 *data, struct hidpp_touchpad_raw_xy *raw_xy) | |
1594 | { | |
1595 | memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy)); | |
1596 | raw_xy->end_of_frame = data[8] & 0x01; | |
1597 | raw_xy->spurious_flag = (data[8] >> 1) & 0x01; | |
1598 | raw_xy->finger_count = data[15] & 0x0f; | |
1599 | raw_xy->button = (data[8] >> 2) & 0x01; | |
1600 | ||
1601 | if (raw_xy->finger_count) { | |
1602 | hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]); | |
1603 | hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]); | |
1604 | } | |
1605 | } | |
1606 | ||
ff21a635 EV |
1607 | /* -------------------------------------------------------------------------- */ |
1608 | /* 0x8123: Force feedback support */ | |
1609 | /* -------------------------------------------------------------------------- */ | |
1610 | ||
1611 | #define HIDPP_FF_GET_INFO 0x01 | |
1612 | #define HIDPP_FF_RESET_ALL 0x11 | |
1613 | #define HIDPP_FF_DOWNLOAD_EFFECT 0x21 | |
1614 | #define HIDPP_FF_SET_EFFECT_STATE 0x31 | |
1615 | #define HIDPP_FF_DESTROY_EFFECT 0x41 | |
1616 | #define HIDPP_FF_GET_APERTURE 0x51 | |
1617 | #define HIDPP_FF_SET_APERTURE 0x61 | |
1618 | #define HIDPP_FF_GET_GLOBAL_GAINS 0x71 | |
1619 | #define HIDPP_FF_SET_GLOBAL_GAINS 0x81 | |
1620 | ||
1621 | #define HIDPP_FF_EFFECT_STATE_GET 0x00 | |
1622 | #define HIDPP_FF_EFFECT_STATE_STOP 0x01 | |
1623 | #define HIDPP_FF_EFFECT_STATE_PLAY 0x02 | |
1624 | #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03 | |
1625 | ||
1626 | #define HIDPP_FF_EFFECT_CONSTANT 0x00 | |
1627 | #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01 | |
1628 | #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02 | |
1629 | #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03 | |
1630 | #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04 | |
1631 | #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05 | |
1632 | #define HIDPP_FF_EFFECT_SPRING 0x06 | |
1633 | #define HIDPP_FF_EFFECT_DAMPER 0x07 | |
1634 | #define HIDPP_FF_EFFECT_FRICTION 0x08 | |
1635 | #define HIDPP_FF_EFFECT_INERTIA 0x09 | |
1636 | #define HIDPP_FF_EFFECT_RAMP 0x0A | |
1637 | ||
1638 | #define HIDPP_FF_EFFECT_AUTOSTART 0x80 | |
1639 | ||
1640 | #define HIDPP_FF_EFFECTID_NONE -1 | |
1641 | #define HIDPP_FF_EFFECTID_AUTOCENTER -2 | |
1642 | ||
1643 | #define HIDPP_FF_MAX_PARAMS 20 | |
1644 | #define HIDPP_FF_RESERVED_SLOTS 1 | |
1645 | ||
1646 | struct hidpp_ff_private_data { | |
1647 | struct hidpp_device *hidpp; | |
1648 | u8 feature_index; | |
1649 | u8 version; | |
1650 | u16 gain; | |
1651 | s16 range; | |
1652 | u8 slot_autocenter; | |
1653 | u8 num_effects; | |
1654 | int *effect_ids; | |
1655 | struct workqueue_struct *wq; | |
1656 | atomic_t workqueue_size; | |
1657 | }; | |
1658 | ||
1659 | struct hidpp_ff_work_data { | |
1660 | struct work_struct work; | |
1661 | struct hidpp_ff_private_data *data; | |
1662 | int effect_id; | |
1663 | u8 command; | |
1664 | u8 params[HIDPP_FF_MAX_PARAMS]; | |
1665 | u8 size; | |
1666 | }; | |
1667 | ||
fef33601 | 1668 | static const signed short hidpp_ff_effects[] = { |
ff21a635 EV |
1669 | FF_CONSTANT, |
1670 | FF_PERIODIC, | |
1671 | FF_SINE, | |
1672 | FF_SQUARE, | |
1673 | FF_SAW_UP, | |
1674 | FF_SAW_DOWN, | |
1675 | FF_TRIANGLE, | |
1676 | FF_SPRING, | |
1677 | FF_DAMPER, | |
1678 | FF_AUTOCENTER, | |
1679 | FF_GAIN, | |
1680 | -1 | |
1681 | }; | |
1682 | ||
fef33601 | 1683 | static const signed short hidpp_ff_effects_v2[] = { |
ff21a635 EV |
1684 | FF_RAMP, |
1685 | FF_FRICTION, | |
1686 | FF_INERTIA, | |
1687 | -1 | |
1688 | }; | |
1689 | ||
1690 | static const u8 HIDPP_FF_CONDITION_CMDS[] = { | |
1691 | HIDPP_FF_EFFECT_SPRING, | |
1692 | HIDPP_FF_EFFECT_FRICTION, | |
1693 | HIDPP_FF_EFFECT_DAMPER, | |
1694 | HIDPP_FF_EFFECT_INERTIA | |
1695 | }; | |
1696 | ||
1697 | static const char *HIDPP_FF_CONDITION_NAMES[] = { | |
1698 | "spring", | |
1699 | "friction", | |
1700 | "damper", | |
1701 | "inertia" | |
1702 | }; | |
1703 | ||
1704 | ||
1705 | static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id) | |
1706 | { | |
1707 | int i; | |
1708 | ||
1709 | for (i = 0; i < data->num_effects; i++) | |
1710 | if (data->effect_ids[i] == effect_id) | |
1711 | return i+1; | |
1712 | ||
1713 | return 0; | |
1714 | } | |
1715 | ||
1716 | static void hidpp_ff_work_handler(struct work_struct *w) | |
1717 | { | |
1718 | struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work); | |
1719 | struct hidpp_ff_private_data *data = wd->data; | |
1720 | struct hidpp_report response; | |
1721 | u8 slot; | |
1722 | int ret; | |
1723 | ||
1724 | /* add slot number if needed */ | |
1725 | switch (wd->effect_id) { | |
1726 | case HIDPP_FF_EFFECTID_AUTOCENTER: | |
1727 | wd->params[0] = data->slot_autocenter; | |
1728 | break; | |
1729 | case HIDPP_FF_EFFECTID_NONE: | |
1730 | /* leave slot as zero */ | |
1731 | break; | |
1732 | default: | |
1733 | /* find current slot for effect */ | |
1734 | wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id); | |
1735 | break; | |
1736 | } | |
1737 | ||
1738 | /* send command and wait for reply */ | |
1739 | ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index, | |
1740 | wd->command, wd->params, wd->size, &response); | |
1741 | ||
1742 | if (ret) { | |
1743 | hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n"); | |
1744 | goto out; | |
1745 | } | |
1746 | ||
1747 | /* parse return data */ | |
1748 | switch (wd->command) { | |
1749 | case HIDPP_FF_DOWNLOAD_EFFECT: | |
1750 | slot = response.fap.params[0]; | |
1751 | if (slot > 0 && slot <= data->num_effects) { | |
1752 | if (wd->effect_id >= 0) | |
1753 | /* regular effect uploaded */ | |
1754 | data->effect_ids[slot-1] = wd->effect_id; | |
1755 | else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER) | |
1756 | /* autocenter spring uploaded */ | |
1757 | data->slot_autocenter = slot; | |
1758 | } | |
1759 | break; | |
1760 | case HIDPP_FF_DESTROY_EFFECT: | |
1761 | if (wd->effect_id >= 0) | |
1762 | /* regular effect destroyed */ | |
1763 | data->effect_ids[wd->params[0]-1] = -1; | |
1764 | else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER) | |
1765 | /* autocenter spring destoyed */ | |
1766 | data->slot_autocenter = 0; | |
1767 | break; | |
1768 | case HIDPP_FF_SET_GLOBAL_GAINS: | |
1769 | data->gain = (wd->params[0] << 8) + wd->params[1]; | |
1770 | break; | |
1771 | case HIDPP_FF_SET_APERTURE: | |
1772 | data->range = (wd->params[0] << 8) + wd->params[1]; | |
1773 | break; | |
1774 | default: | |
1775 | /* no action needed */ | |
1776 | break; | |
1777 | } | |
1778 | ||
1779 | out: | |
1780 | atomic_dec(&data->workqueue_size); | |
1781 | kfree(wd); | |
1782 | } | |
1783 | ||
1784 | static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size) | |
1785 | { | |
1786 | struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL); | |
1787 | int s; | |
1788 | ||
1789 | if (!wd) | |
1790 | return -ENOMEM; | |
1791 | ||
1792 | INIT_WORK(&wd->work, hidpp_ff_work_handler); | |
1793 | ||
1794 | wd->data = data; | |
1795 | wd->effect_id = effect_id; | |
1796 | wd->command = command; | |
1797 | wd->size = size; | |
1798 | memcpy(wd->params, params, size); | |
1799 | ||
1800 | atomic_inc(&data->workqueue_size); | |
1801 | queue_work(data->wq, &wd->work); | |
1802 | ||
1803 | /* warn about excessive queue size */ | |
1804 | s = atomic_read(&data->workqueue_size); | |
1805 | if (s >= 20 && s % 20 == 0) | |
1806 | hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s); | |
1807 | ||
1808 | return 0; | |
1809 | } | |
1810 | ||
1811 | static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old) | |
1812 | { | |
1813 | struct hidpp_ff_private_data *data = dev->ff->private; | |
1814 | u8 params[20]; | |
1815 | u8 size; | |
1816 | int force; | |
1817 | ||
1818 | /* set common parameters */ | |
1819 | params[2] = effect->replay.length >> 8; | |
1820 | params[3] = effect->replay.length & 255; | |
1821 | params[4] = effect->replay.delay >> 8; | |
1822 | params[5] = effect->replay.delay & 255; | |
1823 | ||
1824 | switch (effect->type) { | |
1825 | case FF_CONSTANT: | |
1826 | force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; | |
1827 | params[1] = HIDPP_FF_EFFECT_CONSTANT; | |
1828 | params[6] = force >> 8; | |
1829 | params[7] = force & 255; | |
1830 | params[8] = effect->u.constant.envelope.attack_level >> 7; | |
1831 | params[9] = effect->u.constant.envelope.attack_length >> 8; | |
1832 | params[10] = effect->u.constant.envelope.attack_length & 255; | |
1833 | params[11] = effect->u.constant.envelope.fade_level >> 7; | |
1834 | params[12] = effect->u.constant.envelope.fade_length >> 8; | |
1835 | params[13] = effect->u.constant.envelope.fade_length & 255; | |
1836 | size = 14; | |
1837 | dbg_hid("Uploading constant force level=%d in dir %d = %d\n", | |
1838 | effect->u.constant.level, | |
1839 | effect->direction, force); | |
1840 | dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", | |
1841 | effect->u.constant.envelope.attack_level, | |
1842 | effect->u.constant.envelope.attack_length, | |
1843 | effect->u.constant.envelope.fade_level, | |
1844 | effect->u.constant.envelope.fade_length); | |
1845 | break; | |
1846 | case FF_PERIODIC: | |
1847 | { | |
1848 | switch (effect->u.periodic.waveform) { | |
1849 | case FF_SINE: | |
1850 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE; | |
1851 | break; | |
1852 | case FF_SQUARE: | |
1853 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE; | |
1854 | break; | |
1855 | case FF_SAW_UP: | |
1856 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP; | |
1857 | break; | |
1858 | case FF_SAW_DOWN: | |
1859 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN; | |
1860 | break; | |
1861 | case FF_TRIANGLE: | |
1862 | params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE; | |
1863 | break; | |
1864 | default: | |
1865 | hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform); | |
1866 | return -EINVAL; | |
1867 | } | |
1868 | force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15; | |
1869 | params[6] = effect->u.periodic.magnitude >> 8; | |
1870 | params[7] = effect->u.periodic.magnitude & 255; | |
1871 | params[8] = effect->u.periodic.offset >> 8; | |
1872 | params[9] = effect->u.periodic.offset & 255; | |
1873 | params[10] = effect->u.periodic.period >> 8; | |
1874 | params[11] = effect->u.periodic.period & 255; | |
1875 | params[12] = effect->u.periodic.phase >> 8; | |
1876 | params[13] = effect->u.periodic.phase & 255; | |
1877 | params[14] = effect->u.periodic.envelope.attack_level >> 7; | |
1878 | params[15] = effect->u.periodic.envelope.attack_length >> 8; | |
1879 | params[16] = effect->u.periodic.envelope.attack_length & 255; | |
1880 | params[17] = effect->u.periodic.envelope.fade_level >> 7; | |
1881 | params[18] = effect->u.periodic.envelope.fade_length >> 8; | |
1882 | params[19] = effect->u.periodic.envelope.fade_length & 255; | |
1883 | size = 20; | |
1884 | dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n", | |
1885 | effect->u.periodic.magnitude, effect->direction, | |
1886 | effect->u.periodic.offset, | |
1887 | effect->u.periodic.period, | |
1888 | effect->u.periodic.phase); | |
1889 | dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", | |
1890 | effect->u.periodic.envelope.attack_level, | |
1891 | effect->u.periodic.envelope.attack_length, | |
1892 | effect->u.periodic.envelope.fade_level, | |
1893 | effect->u.periodic.envelope.fade_length); | |
1894 | break; | |
1895 | } | |
1896 | case FF_RAMP: | |
1897 | params[1] = HIDPP_FF_EFFECT_RAMP; | |
1898 | force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; | |
1899 | params[6] = force >> 8; | |
1900 | params[7] = force & 255; | |
1901 | force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; | |
1902 | params[8] = force >> 8; | |
1903 | params[9] = force & 255; | |
1904 | params[10] = effect->u.ramp.envelope.attack_level >> 7; | |
1905 | params[11] = effect->u.ramp.envelope.attack_length >> 8; | |
1906 | params[12] = effect->u.ramp.envelope.attack_length & 255; | |
1907 | params[13] = effect->u.ramp.envelope.fade_level >> 7; | |
1908 | params[14] = effect->u.ramp.envelope.fade_length >> 8; | |
1909 | params[15] = effect->u.ramp.envelope.fade_length & 255; | |
1910 | size = 16; | |
1911 | dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n", | |
1912 | effect->u.ramp.start_level, | |
1913 | effect->u.ramp.end_level, | |
1914 | effect->direction, force); | |
1915 | dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", | |
1916 | effect->u.ramp.envelope.attack_level, | |
1917 | effect->u.ramp.envelope.attack_length, | |
1918 | effect->u.ramp.envelope.fade_level, | |
1919 | effect->u.ramp.envelope.fade_length); | |
1920 | break; | |
1921 | case FF_FRICTION: | |
1922 | case FF_INERTIA: | |
1923 | case FF_SPRING: | |
1924 | case FF_DAMPER: | |
1925 | params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING]; | |
1926 | params[6] = effect->u.condition[0].left_saturation >> 9; | |
1927 | params[7] = (effect->u.condition[0].left_saturation >> 1) & 255; | |
1928 | params[8] = effect->u.condition[0].left_coeff >> 8; | |
1929 | params[9] = effect->u.condition[0].left_coeff & 255; | |
1930 | params[10] = effect->u.condition[0].deadband >> 9; | |
1931 | params[11] = (effect->u.condition[0].deadband >> 1) & 255; | |
1932 | params[12] = effect->u.condition[0].center >> 8; | |
1933 | params[13] = effect->u.condition[0].center & 255; | |
1934 | params[14] = effect->u.condition[0].right_coeff >> 8; | |
1935 | params[15] = effect->u.condition[0].right_coeff & 255; | |
1936 | params[16] = effect->u.condition[0].right_saturation >> 9; | |
1937 | params[17] = (effect->u.condition[0].right_saturation >> 1) & 255; | |
1938 | size = 18; | |
1939 | dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n", | |
1940 | HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING], | |
1941 | effect->u.condition[0].left_coeff, | |
1942 | effect->u.condition[0].left_saturation, | |
1943 | effect->u.condition[0].right_coeff, | |
1944 | effect->u.condition[0].right_saturation); | |
1945 | dbg_hid(" deadband=%d, center=%d\n", | |
1946 | effect->u.condition[0].deadband, | |
1947 | effect->u.condition[0].center); | |
1948 | break; | |
1949 | default: | |
1950 | hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type); | |
1951 | return -EINVAL; | |
1952 | } | |
1953 | ||
1954 | return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size); | |
1955 | } | |
1956 | ||
1957 | static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value) | |
1958 | { | |
1959 | struct hidpp_ff_private_data *data = dev->ff->private; | |
1960 | u8 params[2]; | |
1961 | ||
1962 | params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP; | |
1963 | ||
1964 | dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id); | |
1965 | ||
1966 | return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params)); | |
1967 | } | |
1968 | ||
1969 | static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id) | |
1970 | { | |
1971 | struct hidpp_ff_private_data *data = dev->ff->private; | |
1972 | u8 slot = 0; | |
1973 | ||
1974 | dbg_hid("Erasing effect %d.\n", effect_id); | |
1975 | ||
1976 | return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1); | |
1977 | } | |
1978 | ||
1979 | static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude) | |
1980 | { | |
1981 | struct hidpp_ff_private_data *data = dev->ff->private; | |
1982 | u8 params[18]; | |
1983 | ||
1984 | dbg_hid("Setting autocenter to %d.\n", magnitude); | |
1985 | ||
1986 | /* start a standard spring effect */ | |
1987 | params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART; | |
1988 | /* zero delay and duration */ | |
1989 | params[2] = params[3] = params[4] = params[5] = 0; | |
1990 | /* set coeff to 25% of saturation */ | |
1991 | params[8] = params[14] = magnitude >> 11; | |
1992 | params[9] = params[15] = (magnitude >> 3) & 255; | |
1993 | params[6] = params[16] = magnitude >> 9; | |
1994 | params[7] = params[17] = (magnitude >> 1) & 255; | |
1995 | /* zero deadband and center */ | |
1996 | params[10] = params[11] = params[12] = params[13] = 0; | |
1997 | ||
1998 | hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params)); | |
1999 | } | |
2000 | ||
2001 | static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain) | |
2002 | { | |
2003 | struct hidpp_ff_private_data *data = dev->ff->private; | |
2004 | u8 params[4]; | |
2005 | ||
2006 | dbg_hid("Setting gain to %d.\n", gain); | |
2007 | ||
2008 | params[0] = gain >> 8; | |
2009 | params[1] = gain & 255; | |
2010 | params[2] = 0; /* no boost */ | |
2011 | params[3] = 0; | |
2012 | ||
2013 | hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params)); | |
2014 | } | |
2015 | ||
2016 | static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) | |
2017 | { | |
2018 | struct hid_device *hid = to_hid_device(dev); | |
2019 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | |
2020 | struct input_dev *idev = hidinput->input; | |
2021 | struct hidpp_ff_private_data *data = idev->ff->private; | |
2022 | ||
2023 | return scnprintf(buf, PAGE_SIZE, "%u\n", data->range); | |
2024 | } | |
2025 | ||
2026 | static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) | |
2027 | { | |
2028 | struct hid_device *hid = to_hid_device(dev); | |
2029 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | |
2030 | struct input_dev *idev = hidinput->input; | |
2031 | struct hidpp_ff_private_data *data = idev->ff->private; | |
2032 | u8 params[2]; | |
2033 | int range = simple_strtoul(buf, NULL, 10); | |
2034 | ||
2035 | range = clamp(range, 180, 900); | |
2036 | ||
2037 | params[0] = range >> 8; | |
2038 | params[1] = range & 0x00FF; | |
2039 | ||
2040 | hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params)); | |
2041 | ||
2042 | return count; | |
2043 | } | |
2044 | ||
2045 | static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store); | |
2046 | ||
2047 | static void hidpp_ff_destroy(struct ff_device *ff) | |
2048 | { | |
2049 | struct hidpp_ff_private_data *data = ff->private; | |
2050 | ||
2051 | kfree(data->effect_ids); | |
2052 | } | |
2053 | ||
af2e628d | 2054 | static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index) |
ff21a635 EV |
2055 | { |
2056 | struct hid_device *hid = hidpp->hid_dev; | |
2057 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | |
2058 | struct input_dev *dev = hidinput->input; | |
2059 | const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); | |
2060 | const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); | |
2061 | struct ff_device *ff; | |
2062 | struct hidpp_report response; | |
2063 | struct hidpp_ff_private_data *data; | |
2064 | int error, j, num_slots; | |
2065 | u8 version; | |
2066 | ||
2067 | if (!dev) { | |
2068 | hid_err(hid, "Struct input_dev not set!\n"); | |
2069 | return -EINVAL; | |
2070 | } | |
2071 | ||
2072 | /* Get firmware release */ | |
2073 | version = bcdDevice & 255; | |
2074 | ||
2075 | /* Set supported force feedback capabilities */ | |
fef33601 PH |
2076 | for (j = 0; hidpp_ff_effects[j] >= 0; j++) |
2077 | set_bit(hidpp_ff_effects[j], dev->ffbit); | |
ff21a635 | 2078 | if (version > 1) |
fef33601 PH |
2079 | for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++) |
2080 | set_bit(hidpp_ff_effects_v2[j], dev->ffbit); | |
ff21a635 EV |
2081 | |
2082 | /* Read number of slots available in device */ | |
2083 | error = hidpp_send_fap_command_sync(hidpp, feature_index, | |
2084 | HIDPP_FF_GET_INFO, NULL, 0, &response); | |
2085 | if (error) { | |
2086 | if (error < 0) | |
2087 | return error; | |
2088 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", | |
2089 | __func__, error); | |
2090 | return -EPROTO; | |
2091 | } | |
2092 | ||
2093 | num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS; | |
2094 | ||
2095 | error = input_ff_create(dev, num_slots); | |
2096 | ||
2097 | if (error) { | |
2098 | hid_err(dev, "Failed to create FF device!\n"); | |
2099 | return error; | |
2100 | } | |
2101 | ||
2102 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
2103 | if (!data) | |
2104 | return -ENOMEM; | |
2105 | data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL); | |
2106 | if (!data->effect_ids) { | |
2107 | kfree(data); | |
2108 | return -ENOMEM; | |
2109 | } | |
6c44b15e KL |
2110 | data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue"); |
2111 | if (!data->wq) { | |
2112 | kfree(data->effect_ids); | |
2113 | kfree(data); | |
2114 | return -ENOMEM; | |
2115 | } | |
2116 | ||
ff21a635 EV |
2117 | data->hidpp = hidpp; |
2118 | data->feature_index = feature_index; | |
2119 | data->version = version; | |
2120 | data->slot_autocenter = 0; | |
2121 | data->num_effects = num_slots; | |
2122 | for (j = 0; j < num_slots; j++) | |
2123 | data->effect_ids[j] = -1; | |
2124 | ||
2125 | ff = dev->ff; | |
2126 | ff->private = data; | |
2127 | ||
2128 | ff->upload = hidpp_ff_upload_effect; | |
2129 | ff->erase = hidpp_ff_erase_effect; | |
2130 | ff->playback = hidpp_ff_playback; | |
2131 | ff->set_gain = hidpp_ff_set_gain; | |
2132 | ff->set_autocenter = hidpp_ff_set_autocenter; | |
2133 | ff->destroy = hidpp_ff_destroy; | |
2134 | ||
2135 | ||
2136 | /* reset all forces */ | |
2137 | error = hidpp_send_fap_command_sync(hidpp, feature_index, | |
2138 | HIDPP_FF_RESET_ALL, NULL, 0, &response); | |
2139 | ||
2140 | /* Read current Range */ | |
2141 | error = hidpp_send_fap_command_sync(hidpp, feature_index, | |
2142 | HIDPP_FF_GET_APERTURE, NULL, 0, &response); | |
2143 | if (error) | |
2144 | hid_warn(hidpp->hid_dev, "Failed to read range from device!\n"); | |
2145 | data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]); | |
2146 | ||
2147 | /* Create sysfs interface */ | |
2148 | error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range); | |
2149 | if (error) | |
2150 | hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error); | |
2151 | ||
2152 | /* Read the current gain values */ | |
2153 | error = hidpp_send_fap_command_sync(hidpp, feature_index, | |
2154 | HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response); | |
2155 | if (error) | |
2156 | hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n"); | |
2157 | data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]); | |
2158 | /* ignore boost value at response.fap.params[2] */ | |
2159 | ||
2160 | /* init the hardware command queue */ | |
ff21a635 EV |
2161 | atomic_set(&data->workqueue_size, 0); |
2162 | ||
2163 | /* initialize with zero autocenter to get wheel in usable state */ | |
2164 | hidpp_ff_set_autocenter(dev, 0); | |
2165 | ||
df47b246 CIK |
2166 | hid_info(hid, "Force feedback support loaded (firmware release %d).\n", |
2167 | version); | |
ff21a635 EV |
2168 | |
2169 | return 0; | |
2170 | } | |
2171 | ||
af2e628d | 2172 | static int hidpp_ff_deinit(struct hid_device *hid) |
ff21a635 EV |
2173 | { |
2174 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | |
2175 | struct input_dev *dev = hidinput->input; | |
2176 | struct hidpp_ff_private_data *data; | |
2177 | ||
2178 | if (!dev) { | |
2179 | hid_err(hid, "Struct input_dev not found!\n"); | |
2180 | return -EINVAL; | |
2181 | } | |
2182 | ||
2183 | hid_info(hid, "Unloading HID++ force feedback.\n"); | |
2184 | data = dev->ff->private; | |
2185 | if (!data) { | |
2186 | hid_err(hid, "Private data not found!\n"); | |
2187 | return -EINVAL; | |
2188 | } | |
2189 | ||
2190 | destroy_workqueue(data->wq); | |
2191 | device_remove_file(&hid->dev, &dev_attr_range); | |
2192 | ||
2193 | return 0; | |
2194 | } | |
2195 | ||
2196 | ||
2f31c525 BT |
2197 | /* ************************************************************************** */ |
2198 | /* */ | |
2199 | /* Device Support */ | |
2200 | /* */ | |
2201 | /* ************************************************************************** */ | |
2202 | ||
2203 | /* -------------------------------------------------------------------------- */ | |
2204 | /* Touchpad HID++ devices */ | |
2205 | /* -------------------------------------------------------------------------- */ | |
2206 | ||
57ac86cf BT |
2207 | #define WTP_MANUAL_RESOLUTION 39 |
2208 | ||
2f31c525 BT |
2209 | struct wtp_data { |
2210 | struct input_dev *input; | |
2211 | u16 x_size, y_size; | |
2212 | u8 finger_count; | |
2213 | u8 mt_feature_index; | |
2214 | u8 button_feature_index; | |
2215 | u8 maxcontacts; | |
2216 | bool flip_y; | |
2217 | unsigned int resolution; | |
2218 | }; | |
2219 | ||
2220 | static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
2221 | struct hid_field *field, struct hid_usage *usage, | |
2222 | unsigned long **bit, int *max) | |
2223 | { | |
2224 | return -1; | |
2225 | } | |
2226 | ||
c39e3d5f BT |
2227 | static void wtp_populate_input(struct hidpp_device *hidpp, |
2228 | struct input_dev *input_dev, bool origin_is_hid_core) | |
2f31c525 | 2229 | { |
2f31c525 | 2230 | struct wtp_data *wd = hidpp->private_data; |
2f31c525 BT |
2231 | |
2232 | __set_bit(EV_ABS, input_dev->evbit); | |
2233 | __set_bit(EV_KEY, input_dev->evbit); | |
2234 | __clear_bit(EV_REL, input_dev->evbit); | |
2235 | __clear_bit(EV_LED, input_dev->evbit); | |
2236 | ||
2237 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0); | |
2238 | input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution); | |
2239 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0); | |
2240 | input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution); | |
2241 | ||
2242 | /* Max pressure is not given by the devices, pick one */ | |
2243 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0); | |
2244 | ||
2245 | input_set_capability(input_dev, EV_KEY, BTN_LEFT); | |
2246 | ||
57ac86cf BT |
2247 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) |
2248 | input_set_capability(input_dev, EV_KEY, BTN_RIGHT); | |
2249 | else | |
2250 | __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); | |
2f31c525 BT |
2251 | |
2252 | input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER | | |
2253 | INPUT_MT_DROP_UNUSED); | |
2254 | ||
2255 | wd->input = input_dev; | |
2256 | } | |
2257 | ||
2258 | static void wtp_touch_event(struct wtp_data *wd, | |
2259 | struct hidpp_touchpad_raw_xy_finger *touch_report) | |
2260 | { | |
2261 | int slot; | |
2262 | ||
2263 | if (!touch_report->finger_id || touch_report->contact_type) | |
2264 | /* no actual data */ | |
2265 | return; | |
2266 | ||
2267 | slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id); | |
2268 | ||
2269 | input_mt_slot(wd->input, slot); | |
2270 | input_mt_report_slot_state(wd->input, MT_TOOL_FINGER, | |
2271 | touch_report->contact_status); | |
2272 | if (touch_report->contact_status) { | |
2273 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_X, | |
2274 | touch_report->x); | |
2275 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y, | |
2276 | wd->flip_y ? wd->y_size - touch_report->y : | |
2277 | touch_report->y); | |
2278 | input_event(wd->input, EV_ABS, ABS_MT_PRESSURE, | |
2279 | touch_report->area); | |
2280 | } | |
2281 | } | |
2282 | ||
2283 | static void wtp_send_raw_xy_event(struct hidpp_device *hidpp, | |
2284 | struct hidpp_touchpad_raw_xy *raw) | |
2285 | { | |
2286 | struct wtp_data *wd = hidpp->private_data; | |
2287 | int i; | |
2288 | ||
2289 | for (i = 0; i < 2; i++) | |
2290 | wtp_touch_event(wd, &(raw->fingers[i])); | |
2291 | ||
57ac86cf BT |
2292 | if (raw->end_of_frame && |
2293 | !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)) | |
2f31c525 BT |
2294 | input_event(wd->input, EV_KEY, BTN_LEFT, raw->button); |
2295 | ||
2296 | if (raw->end_of_frame || raw->finger_count <= 2) { | |
2297 | input_mt_sync_frame(wd->input); | |
2298 | input_sync(wd->input); | |
2299 | } | |
2300 | } | |
2301 | ||
2302 | static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data) | |
2303 | { | |
2304 | struct wtp_data *wd = hidpp->private_data; | |
2305 | u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) + | |
2306 | (data[7] >> 4) * (data[7] >> 4)) / 2; | |
2307 | u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) + | |
2308 | (data[13] >> 4) * (data[13] >> 4)) / 2; | |
2309 | struct hidpp_touchpad_raw_xy raw = { | |
2310 | .timestamp = data[1], | |
2311 | .fingers = { | |
2312 | { | |
2313 | .contact_type = 0, | |
2314 | .contact_status = !!data[7], | |
2315 | .x = get_unaligned_le16(&data[3]), | |
2316 | .y = get_unaligned_le16(&data[5]), | |
2317 | .z = c1_area, | |
2318 | .area = c1_area, | |
2319 | .finger_id = data[2], | |
2320 | }, { | |
2321 | .contact_type = 0, | |
2322 | .contact_status = !!data[13], | |
2323 | .x = get_unaligned_le16(&data[9]), | |
2324 | .y = get_unaligned_le16(&data[11]), | |
2325 | .z = c2_area, | |
2326 | .area = c2_area, | |
2327 | .finger_id = data[8], | |
2328 | } | |
2329 | }, | |
2330 | .finger_count = wd->maxcontacts, | |
2331 | .spurious_flag = 0, | |
2332 | .end_of_frame = (data[0] >> 7) == 0, | |
2333 | .button = data[0] & 0x01, | |
2334 | }; | |
2335 | ||
2336 | wtp_send_raw_xy_event(hidpp, &raw); | |
2337 | ||
2338 | return 1; | |
2339 | } | |
2340 | ||
2341 | static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) | |
2342 | { | |
2343 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2344 | struct wtp_data *wd = hidpp->private_data; | |
586bdc4e BT |
2345 | struct hidpp_report *report = (struct hidpp_report *)data; |
2346 | struct hidpp_touchpad_raw_xy raw; | |
2f31c525 | 2347 | |
586bdc4e | 2348 | if (!wd || !wd->input) |
2f31c525 BT |
2349 | return 1; |
2350 | ||
586bdc4e BT |
2351 | switch (data[0]) { |
2352 | case 0x02: | |
0b3f6569 PW |
2353 | if (size < 2) { |
2354 | hid_err(hdev, "Received HID report of bad size (%d)", | |
2355 | size); | |
2356 | return 1; | |
2357 | } | |
57ac86cf BT |
2358 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) { |
2359 | input_event(wd->input, EV_KEY, BTN_LEFT, | |
2360 | !!(data[1] & 0x01)); | |
2361 | input_event(wd->input, EV_KEY, BTN_RIGHT, | |
2362 | !!(data[1] & 0x02)); | |
2363 | input_sync(wd->input); | |
8abd8205 | 2364 | return 0; |
57ac86cf BT |
2365 | } else { |
2366 | if (size < 21) | |
2367 | return 1; | |
2368 | return wtp_mouse_raw_xy_event(hidpp, &data[7]); | |
2369 | } | |
586bdc4e | 2370 | case REPORT_ID_HIDPP_LONG: |
0b3f6569 | 2371 | /* size is already checked in hidpp_raw_event. */ |
586bdc4e BT |
2372 | if ((report->fap.feature_index != wd->mt_feature_index) || |
2373 | (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY)) | |
2374 | return 1; | |
2375 | hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw); | |
2376 | ||
2377 | wtp_send_raw_xy_event(hidpp, &raw); | |
2378 | return 0; | |
2379 | } | |
2380 | ||
2381 | return 0; | |
2f31c525 BT |
2382 | } |
2383 | ||
2384 | static int wtp_get_config(struct hidpp_device *hidpp) | |
2385 | { | |
2386 | struct wtp_data *wd = hidpp->private_data; | |
2387 | struct hidpp_touchpad_raw_info raw_info = {0}; | |
2388 | u8 feature_type; | |
2389 | int ret; | |
2390 | ||
2391 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY, | |
2392 | &wd->mt_feature_index, &feature_type); | |
2393 | if (ret) | |
2394 | /* means that the device is not powered up */ | |
2395 | return ret; | |
2396 | ||
2397 | ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index, | |
2398 | &raw_info); | |
2399 | if (ret) | |
2400 | return ret; | |
2401 | ||
2402 | wd->x_size = raw_info.x_size; | |
2403 | wd->y_size = raw_info.y_size; | |
2404 | wd->maxcontacts = raw_info.maxcontacts; | |
2405 | wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT; | |
2406 | wd->resolution = raw_info.res; | |
57ac86cf BT |
2407 | if (!wd->resolution) |
2408 | wd->resolution = WTP_MANUAL_RESOLUTION; | |
2f31c525 BT |
2409 | |
2410 | return 0; | |
2411 | } | |
2412 | ||
2413 | static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id) | |
2414 | { | |
2415 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2416 | struct wtp_data *wd; | |
2417 | ||
2418 | wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data), | |
2419 | GFP_KERNEL); | |
2420 | if (!wd) | |
2421 | return -ENOMEM; | |
2422 | ||
2423 | hidpp->private_data = wd; | |
2424 | ||
2425 | return 0; | |
2426 | }; | |
2427 | ||
bf159447 | 2428 | static int wtp_connect(struct hid_device *hdev, bool connected) |
586bdc4e BT |
2429 | { |
2430 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2431 | struct wtp_data *wd = hidpp->private_data; | |
2432 | int ret; | |
2433 | ||
586bdc4e BT |
2434 | if (!wd->x_size) { |
2435 | ret = wtp_get_config(hidpp); | |
2436 | if (ret) { | |
2437 | hid_err(hdev, "Can not get wtp config: %d\n", ret); | |
bf159447 | 2438 | return ret; |
586bdc4e BT |
2439 | } |
2440 | } | |
2441 | ||
bf159447 | 2442 | return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index, |
586bdc4e BT |
2443 | true, true); |
2444 | } | |
2445 | ||
8a09b4fa GB |
2446 | /* ------------------------------------------------------------------------- */ |
2447 | /* Logitech M560 devices */ | |
2448 | /* ------------------------------------------------------------------------- */ | |
2449 | ||
2450 | /* | |
2451 | * Logitech M560 protocol overview | |
2452 | * | |
2453 | * The Logitech M560 mouse, is designed for windows 8. When the middle and/or | |
2454 | * the sides buttons are pressed, it sends some keyboard keys events | |
2455 | * instead of buttons ones. | |
2456 | * To complicate things further, the middle button keys sequence | |
2457 | * is different from the odd press and the even press. | |
2458 | * | |
2459 | * forward button -> Super_R | |
2460 | * backward button -> Super_L+'d' (press only) | |
2461 | * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only) | |
2462 | * 2nd time: left-click (press only) | |
2463 | * NB: press-only means that when the button is pressed, the | |
2464 | * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated | |
2465 | * together sequentially; instead when the button is released, no event is | |
2466 | * generated ! | |
2467 | * | |
2468 | * With the command | |
2469 | * 10<xx>0a 3500af03 (where <xx> is the mouse id), | |
2470 | * the mouse reacts differently: | |
2471 | * - it never sends a keyboard key event | |
2472 | * - for the three mouse button it sends: | |
2473 | * middle button press 11<xx>0a 3500af00... | |
2474 | * side 1 button (forward) press 11<xx>0a 3500b000... | |
2475 | * side 2 button (backward) press 11<xx>0a 3500ae00... | |
2476 | * middle/side1/side2 button release 11<xx>0a 35000000... | |
2477 | */ | |
2478 | ||
2479 | static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03}; | |
2480 | ||
2481 | struct m560_private_data { | |
2482 | struct input_dev *input; | |
2483 | }; | |
2484 | ||
2485 | /* how buttons are mapped in the report */ | |
2486 | #define M560_MOUSE_BTN_LEFT 0x01 | |
2487 | #define M560_MOUSE_BTN_RIGHT 0x02 | |
2488 | #define M560_MOUSE_BTN_WHEEL_LEFT 0x08 | |
2489 | #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10 | |
2490 | ||
2491 | #define M560_SUB_ID 0x0a | |
2492 | #define M560_BUTTON_MODE_REGISTER 0x35 | |
2493 | ||
2494 | static int m560_send_config_command(struct hid_device *hdev, bool connected) | |
2495 | { | |
2496 | struct hidpp_report response; | |
2497 | struct hidpp_device *hidpp_dev; | |
2498 | ||
2499 | hidpp_dev = hid_get_drvdata(hdev); | |
2500 | ||
8a09b4fa GB |
2501 | return hidpp_send_rap_command_sync( |
2502 | hidpp_dev, | |
2503 | REPORT_ID_HIDPP_SHORT, | |
2504 | M560_SUB_ID, | |
2505 | M560_BUTTON_MODE_REGISTER, | |
2506 | (u8 *)m560_config_parameter, | |
2507 | sizeof(m560_config_parameter), | |
2508 | &response | |
2509 | ); | |
2510 | } | |
2511 | ||
2512 | static int m560_allocate(struct hid_device *hdev) | |
2513 | { | |
2514 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2515 | struct m560_private_data *d; | |
2516 | ||
2517 | d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data), | |
2518 | GFP_KERNEL); | |
2519 | if (!d) | |
2520 | return -ENOMEM; | |
2521 | ||
2522 | hidpp->private_data = d; | |
2523 | ||
2524 | return 0; | |
2525 | }; | |
2526 | ||
2527 | static int m560_raw_event(struct hid_device *hdev, u8 *data, int size) | |
2528 | { | |
2529 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2530 | struct m560_private_data *mydata = hidpp->private_data; | |
2531 | ||
2532 | /* sanity check */ | |
2533 | if (!mydata || !mydata->input) { | |
2534 | hid_err(hdev, "error in parameter\n"); | |
2535 | return -EINVAL; | |
2536 | } | |
2537 | ||
2538 | if (size < 7) { | |
2539 | hid_err(hdev, "error in report\n"); | |
2540 | return 0; | |
2541 | } | |
2542 | ||
2543 | if (data[0] == REPORT_ID_HIDPP_LONG && | |
2544 | data[2] == M560_SUB_ID && data[6] == 0x00) { | |
2545 | /* | |
2546 | * m560 mouse report for middle, forward and backward button | |
2547 | * | |
2548 | * data[0] = 0x11 | |
2549 | * data[1] = device-id | |
2550 | * data[2] = 0x0a | |
2551 | * data[5] = 0xaf -> middle | |
2552 | * 0xb0 -> forward | |
2553 | * 0xae -> backward | |
2554 | * 0x00 -> release all | |
2555 | * data[6] = 0x00 | |
2556 | */ | |
2557 | ||
2558 | switch (data[5]) { | |
2559 | case 0xaf: | |
2560 | input_report_key(mydata->input, BTN_MIDDLE, 1); | |
2561 | break; | |
2562 | case 0xb0: | |
2563 | input_report_key(mydata->input, BTN_FORWARD, 1); | |
2564 | break; | |
2565 | case 0xae: | |
2566 | input_report_key(mydata->input, BTN_BACK, 1); | |
2567 | break; | |
2568 | case 0x00: | |
2569 | input_report_key(mydata->input, BTN_BACK, 0); | |
2570 | input_report_key(mydata->input, BTN_FORWARD, 0); | |
2571 | input_report_key(mydata->input, BTN_MIDDLE, 0); | |
2572 | break; | |
2573 | default: | |
2574 | hid_err(hdev, "error in report\n"); | |
2575 | return 0; | |
2576 | } | |
2577 | input_sync(mydata->input); | |
2578 | ||
2579 | } else if (data[0] == 0x02) { | |
2580 | /* | |
2581 | * Logitech M560 mouse report | |
2582 | * | |
2583 | * data[0] = type (0x02) | |
2584 | * data[1..2] = buttons | |
2585 | * data[3..5] = xy | |
2586 | * data[6] = wheel | |
2587 | */ | |
2588 | ||
2589 | int v; | |
2590 | ||
2591 | input_report_key(mydata->input, BTN_LEFT, | |
2592 | !!(data[1] & M560_MOUSE_BTN_LEFT)); | |
2593 | input_report_key(mydata->input, BTN_RIGHT, | |
2594 | !!(data[1] & M560_MOUSE_BTN_RIGHT)); | |
2595 | ||
4435ff2f | 2596 | if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) { |
8a09b4fa | 2597 | input_report_rel(mydata->input, REL_HWHEEL, -1); |
4435ff2f HC |
2598 | input_report_rel(mydata->input, REL_HWHEEL_HI_RES, |
2599 | -120); | |
2600 | } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) { | |
8a09b4fa | 2601 | input_report_rel(mydata->input, REL_HWHEEL, 1); |
4435ff2f HC |
2602 | input_report_rel(mydata->input, REL_HWHEEL_HI_RES, |
2603 | 120); | |
2604 | } | |
8a09b4fa GB |
2605 | |
2606 | v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12); | |
2607 | input_report_rel(mydata->input, REL_X, v); | |
2608 | ||
2609 | v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12); | |
2610 | input_report_rel(mydata->input, REL_Y, v); | |
2611 | ||
2612 | v = hid_snto32(data[6], 8); | |
fd35759c PH |
2613 | if (v != 0) |
2614 | hidpp_scroll_counter_handle_scroll( | |
2615 | &hidpp->vertical_wheel_counter, v); | |
8a09b4fa GB |
2616 | |
2617 | input_sync(mydata->input); | |
2618 | } | |
2619 | ||
2620 | return 1; | |
2621 | } | |
2622 | ||
2623 | static void m560_populate_input(struct hidpp_device *hidpp, | |
2624 | struct input_dev *input_dev, bool origin_is_hid_core) | |
2625 | { | |
2626 | struct m560_private_data *mydata = hidpp->private_data; | |
2627 | ||
2628 | mydata->input = input_dev; | |
2629 | ||
2630 | __set_bit(EV_KEY, mydata->input->evbit); | |
2631 | __set_bit(BTN_MIDDLE, mydata->input->keybit); | |
2632 | __set_bit(BTN_RIGHT, mydata->input->keybit); | |
2633 | __set_bit(BTN_LEFT, mydata->input->keybit); | |
2634 | __set_bit(BTN_BACK, mydata->input->keybit); | |
2635 | __set_bit(BTN_FORWARD, mydata->input->keybit); | |
2636 | ||
2637 | __set_bit(EV_REL, mydata->input->evbit); | |
2638 | __set_bit(REL_X, mydata->input->relbit); | |
2639 | __set_bit(REL_Y, mydata->input->relbit); | |
2640 | __set_bit(REL_WHEEL, mydata->input->relbit); | |
2641 | __set_bit(REL_HWHEEL, mydata->input->relbit); | |
4435ff2f HC |
2642 | __set_bit(REL_WHEEL_HI_RES, mydata->input->relbit); |
2643 | __set_bit(REL_HWHEEL_HI_RES, mydata->input->relbit); | |
8a09b4fa GB |
2644 | } |
2645 | ||
2646 | static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
2647 | struct hid_field *field, struct hid_usage *usage, | |
2648 | unsigned long **bit, int *max) | |
2649 | { | |
2650 | return -1; | |
2651 | } | |
2652 | ||
90cdd986 BT |
2653 | /* ------------------------------------------------------------------------- */ |
2654 | /* Logitech K400 devices */ | |
2655 | /* ------------------------------------------------------------------------- */ | |
2656 | ||
2657 | /* | |
2658 | * The Logitech K400 keyboard has an embedded touchpad which is seen | |
2659 | * as a mouse from the OS point of view. There is a hardware shortcut to disable | |
2660 | * tap-to-click but the setting is not remembered accross reset, annoying some | |
2661 | * users. | |
2662 | * | |
2663 | * We can toggle this feature from the host by using the feature 0x6010: | |
2664 | * Touchpad FW items | |
2665 | */ | |
2666 | ||
2667 | struct k400_private_data { | |
2668 | u8 feature_index; | |
2669 | }; | |
2670 | ||
2671 | static int k400_disable_tap_to_click(struct hidpp_device *hidpp) | |
2672 | { | |
2673 | struct k400_private_data *k400 = hidpp->private_data; | |
2674 | struct hidpp_touchpad_fw_items items = {}; | |
2675 | int ret; | |
2676 | u8 feature_type; | |
2677 | ||
2678 | if (!k400->feature_index) { | |
2679 | ret = hidpp_root_get_feature(hidpp, | |
2680 | HIDPP_PAGE_TOUCHPAD_FW_ITEMS, | |
2681 | &k400->feature_index, &feature_type); | |
2682 | if (ret) | |
2683 | /* means that the device is not powered up */ | |
2684 | return ret; | |
2685 | } | |
2686 | ||
2687 | ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items); | |
2688 | if (ret) | |
2689 | return ret; | |
2690 | ||
2691 | return 0; | |
2692 | } | |
2693 | ||
2694 | static int k400_allocate(struct hid_device *hdev) | |
2695 | { | |
2696 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2697 | struct k400_private_data *k400; | |
2698 | ||
2699 | k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data), | |
2700 | GFP_KERNEL); | |
2701 | if (!k400) | |
2702 | return -ENOMEM; | |
2703 | ||
2704 | hidpp->private_data = k400; | |
2705 | ||
2706 | return 0; | |
2707 | }; | |
2708 | ||
2709 | static int k400_connect(struct hid_device *hdev, bool connected) | |
2710 | { | |
2711 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2712 | ||
90cdd986 BT |
2713 | if (!disable_tap_to_click) |
2714 | return 0; | |
2715 | ||
2716 | return k400_disable_tap_to_click(hidpp); | |
2717 | } | |
2718 | ||
7f4b49fe SW |
2719 | /* ------------------------------------------------------------------------- */ |
2720 | /* Logitech G920 Driving Force Racing Wheel for Xbox One */ | |
2721 | /* ------------------------------------------------------------------------- */ | |
2722 | ||
2723 | #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123 | |
2724 | ||
7f4b49fe SW |
2725 | static int g920_get_config(struct hidpp_device *hidpp) |
2726 | { | |
7f4b49fe SW |
2727 | u8 feature_type; |
2728 | u8 feature_index; | |
2729 | int ret; | |
2730 | ||
7f4b49fe SW |
2731 | /* Find feature and store for later use */ |
2732 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK, | |
2733 | &feature_index, &feature_type); | |
2734 | if (ret) | |
2735 | return ret; | |
2736 | ||
ff21a635 | 2737 | ret = hidpp_ff_init(hidpp, feature_index); |
7f4b49fe | 2738 | if (ret) |
ff21a635 EV |
2739 | hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n", |
2740 | ret); | |
7f4b49fe SW |
2741 | |
2742 | return 0; | |
2743 | } | |
2744 | ||
4435ff2f HC |
2745 | /* -------------------------------------------------------------------------- */ |
2746 | /* High-resolution scroll wheels */ | |
2747 | /* -------------------------------------------------------------------------- */ | |
2748 | ||
2749 | static int hi_res_scroll_enable(struct hidpp_device *hidpp) | |
2750 | { | |
2751 | int ret; | |
2752 | u8 multiplier = 1; | |
2753 | ||
2754 | if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) { | |
2755 | ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false); | |
2756 | if (ret == 0) | |
2757 | ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier); | |
2758 | } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) { | |
2759 | ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true, | |
2760 | &multiplier); | |
2761 | } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ { | |
2762 | ret = hidpp10_enable_scrolling_acceleration(hidpp); | |
2763 | multiplier = 8; | |
2764 | } | |
2765 | if (ret) | |
2766 | return ret; | |
2767 | ||
2768 | if (multiplier == 0) | |
2769 | multiplier = 1; | |
2770 | ||
2771 | hidpp->vertical_wheel_counter.wheel_multiplier = multiplier; | |
2772 | hid_info(hidpp->hid_dev, "multiplier = %d\n", multiplier); | |
2773 | return 0; | |
2774 | } | |
2775 | ||
2f31c525 BT |
2776 | /* -------------------------------------------------------------------------- */ |
2777 | /* Generic HID++ devices */ | |
2778 | /* -------------------------------------------------------------------------- */ | |
2779 | ||
2780 | static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi, | |
2781 | struct hid_field *field, struct hid_usage *usage, | |
2782 | unsigned long **bit, int *max) | |
2783 | { | |
2784 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2785 | ||
fe3ee1ec BT |
2786 | if (!hidpp) |
2787 | return 0; | |
2788 | ||
2f31c525 BT |
2789 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
2790 | return wtp_input_mapping(hdev, hi, field, usage, bit, max); | |
8a09b4fa GB |
2791 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 && |
2792 | field->application != HID_GD_MOUSE) | |
2793 | return m560_input_mapping(hdev, hi, field, usage, bit, max); | |
2f31c525 BT |
2794 | |
2795 | return 0; | |
2796 | } | |
2797 | ||
0b1804e3 SW |
2798 | static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
2799 | struct hid_field *field, struct hid_usage *usage, | |
2800 | unsigned long **bit, int *max) | |
2801 | { | |
2802 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
2803 | ||
fe3ee1ec BT |
2804 | if (!hidpp) |
2805 | return 0; | |
2806 | ||
0b1804e3 SW |
2807 | /* Ensure that Logitech G920 is not given a default fuzz/flat value */ |
2808 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { | |
2809 | if (usage->type == EV_ABS && (usage->code == ABS_X || | |
2810 | usage->code == ABS_Y || usage->code == ABS_Z || | |
2811 | usage->code == ABS_RZ)) { | |
2812 | field->application = HID_GD_MULTIAXIS; | |
2813 | } | |
2814 | } | |
2815 | ||
2816 | return 0; | |
2817 | } | |
2818 | ||
2819 | ||
c39e3d5f BT |
2820 | static void hidpp_populate_input(struct hidpp_device *hidpp, |
2821 | struct input_dev *input, bool origin_is_hid_core) | |
2822 | { | |
2823 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) | |
2824 | wtp_populate_input(hidpp, input, origin_is_hid_core); | |
8a09b4fa GB |
2825 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) |
2826 | m560_populate_input(hidpp, input, origin_is_hid_core); | |
4435ff2f HC |
2827 | |
2828 | if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) | |
2829 | hidpp->vertical_wheel_counter.dev = input; | |
c39e3d5f BT |
2830 | } |
2831 | ||
b2c68a2f | 2832 | static int hidpp_input_configured(struct hid_device *hdev, |
2f31c525 BT |
2833 | struct hid_input *hidinput) |
2834 | { | |
2835 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
c39e3d5f | 2836 | struct input_dev *input = hidinput->input; |
2f31c525 | 2837 | |
fe3ee1ec BT |
2838 | if (!hidpp) |
2839 | return 0; | |
2840 | ||
c39e3d5f | 2841 | hidpp_populate_input(hidpp, input, true); |
b2c68a2f DT |
2842 | |
2843 | return 0; | |
2f31c525 BT |
2844 | } |
2845 | ||
2846 | static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, | |
2847 | int size) | |
2848 | { | |
2849 | struct hidpp_report *question = hidpp->send_receive_buf; | |
2850 | struct hidpp_report *answer = hidpp->send_receive_buf; | |
2851 | struct hidpp_report *report = (struct hidpp_report *)data; | |
eb626c57 | 2852 | int ret; |
2f31c525 BT |
2853 | |
2854 | /* | |
2855 | * If the mutex is locked then we have a pending answer from a | |
e529fea9 | 2856 | * previously sent command. |
2f31c525 BT |
2857 | */ |
2858 | if (unlikely(mutex_is_locked(&hidpp->send_mutex))) { | |
2859 | /* | |
2860 | * Check for a correct hidpp20 answer or the corresponding | |
2861 | * error | |
2862 | */ | |
2863 | if (hidpp_match_answer(question, report) || | |
2864 | hidpp_match_error(question, report)) { | |
2865 | *answer = *report; | |
2866 | hidpp->answer_available = true; | |
2867 | wake_up(&hidpp->wait); | |
2868 | /* | |
2869 | * This was an answer to a command that this driver sent | |
2870 | * We return 1 to hid-core to avoid forwarding the | |
2871 | * command upstream as it has been treated by the driver | |
2872 | */ | |
2873 | ||
2874 | return 1; | |
2875 | } | |
2876 | } | |
2877 | ||
c39e3d5f BT |
2878 | if (unlikely(hidpp_report_is_connect_event(report))) { |
2879 | atomic_set(&hidpp->connected, | |
2880 | !(report->rap.params[0] & (1 << 6))); | |
6bd4e65d | 2881 | if (schedule_work(&hidpp->work) == 0) |
c39e3d5f BT |
2882 | dbg_hid("%s: connect event already queued\n", __func__); |
2883 | return 1; | |
2884 | } | |
2885 | ||
eb626c57 BT |
2886 | if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) { |
2887 | ret = hidpp20_battery_event(hidpp, data, size); | |
2888 | if (ret != 0) | |
2889 | return ret; | |
696ecef9 BT |
2890 | ret = hidpp_solar_battery_event(hidpp, data, size); |
2891 | if (ret != 0) | |
2892 | return ret; | |
eb626c57 BT |
2893 | } |
2894 | ||
7f7ce2a2 BT |
2895 | if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) { |
2896 | ret = hidpp10_battery_event(hidpp, data, size); | |
2897 | if (ret != 0) | |
2898 | return ret; | |
2899 | } | |
2900 | ||
2f31c525 BT |
2901 | return 0; |
2902 | } | |
2903 | ||
2904 | static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report, | |
2905 | u8 *data, int size) | |
2906 | { | |
2907 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
e529fea9 | 2908 | int ret = 0; |
2f31c525 | 2909 | |
fe3ee1ec BT |
2910 | if (!hidpp) |
2911 | return 0; | |
2912 | ||
e529fea9 | 2913 | /* Generic HID++ processing. */ |
2f31c525 | 2914 | switch (data[0]) { |
a5ce8f5b SW |
2915 | case REPORT_ID_HIDPP_VERY_LONG: |
2916 | if (size != HIDPP_REPORT_VERY_LONG_LENGTH) { | |
2917 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
2918 | size); | |
2919 | return 1; | |
2920 | } | |
2921 | ret = hidpp_raw_hidpp_event(hidpp, data, size); | |
2922 | break; | |
2f31c525 BT |
2923 | case REPORT_ID_HIDPP_LONG: |
2924 | if (size != HIDPP_REPORT_LONG_LENGTH) { | |
2925 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
2926 | size); | |
2927 | return 1; | |
2928 | } | |
e529fea9 PW |
2929 | ret = hidpp_raw_hidpp_event(hidpp, data, size); |
2930 | break; | |
2f31c525 BT |
2931 | case REPORT_ID_HIDPP_SHORT: |
2932 | if (size != HIDPP_REPORT_SHORT_LENGTH) { | |
2933 | hid_err(hdev, "received hid++ report of bad size (%d)", | |
2934 | size); | |
2935 | return 1; | |
2936 | } | |
e529fea9 PW |
2937 | ret = hidpp_raw_hidpp_event(hidpp, data, size); |
2938 | break; | |
2f31c525 BT |
2939 | } |
2940 | ||
e529fea9 PW |
2941 | /* If no report is available for further processing, skip calling |
2942 | * raw_event of subclasses. */ | |
2943 | if (ret != 0) | |
2944 | return ret; | |
2945 | ||
2f31c525 BT |
2946 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
2947 | return wtp_raw_event(hdev, data, size); | |
8a09b4fa GB |
2948 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) |
2949 | return m560_raw_event(hdev, data, size); | |
2f31c525 BT |
2950 | |
2951 | return 0; | |
2952 | } | |
2953 | ||
4435ff2f HC |
2954 | static int hidpp_event(struct hid_device *hdev, struct hid_field *field, |
2955 | struct hid_usage *usage, __s32 value) | |
2956 | { | |
2957 | /* This function will only be called for scroll events, due to the | |
2958 | * restriction imposed in hidpp_usages. | |
2959 | */ | |
2960 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
fe3ee1ec BT |
2961 | struct hidpp_scroll_counter *counter; |
2962 | ||
2963 | if (!hidpp) | |
2964 | return 0; | |
2965 | ||
2966 | counter = &hidpp->vertical_wheel_counter; | |
4435ff2f HC |
2967 | /* A scroll event may occur before the multiplier has been retrieved or |
2968 | * the input device set, or high-res scroll enabling may fail. In such | |
2969 | * cases we must return early (falling back to default behaviour) to | |
2970 | * avoid a crash in hidpp_scroll_counter_handle_scroll. | |
2971 | */ | |
2972 | if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0 | |
2973 | || counter->dev == NULL || counter->wheel_multiplier == 0) | |
2974 | return 0; | |
2975 | ||
2976 | hidpp_scroll_counter_handle_scroll(counter, value); | |
2977 | return 1; | |
2978 | } | |
2979 | ||
a52ec107 BT |
2980 | static int hidpp_initialize_battery(struct hidpp_device *hidpp) |
2981 | { | |
2982 | static atomic_t battery_no = ATOMIC_INIT(0); | |
2983 | struct power_supply_config cfg = { .drv_data = hidpp }; | |
2984 | struct power_supply_desc *desc = &hidpp->battery.desc; | |
5b036ea1 | 2985 | enum power_supply_property *battery_props; |
a52ec107 | 2986 | struct hidpp_battery *battery; |
5b036ea1 | 2987 | unsigned int num_battery_props; |
a52ec107 BT |
2988 | unsigned long n; |
2989 | int ret; | |
2990 | ||
2991 | if (hidpp->battery.ps) | |
2992 | return 0; | |
2993 | ||
696ecef9 BT |
2994 | hidpp->battery.feature_index = 0xff; |
2995 | hidpp->battery.solar_feature_index = 0xff; | |
2996 | ||
a52ec107 | 2997 | if (hidpp->protocol_major >= 2) { |
696ecef9 BT |
2998 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750) |
2999 | ret = hidpp_solar_request_battery_event(hidpp); | |
3000 | else | |
3001 | ret = hidpp20_query_battery_info(hidpp); | |
3002 | ||
a52ec107 BT |
3003 | if (ret) |
3004 | return ret; | |
3005 | hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY; | |
3006 | } else { | |
7f7ce2a2 BT |
3007 | ret = hidpp10_query_battery_status(hidpp); |
3008 | if (ret) { | |
3009 | ret = hidpp10_query_battery_mileage(hidpp); | |
3010 | if (ret) | |
3011 | return -ENOENT; | |
3012 | hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE; | |
3013 | } else { | |
3014 | hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS; | |
3015 | } | |
3016 | hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY; | |
a52ec107 BT |
3017 | } |
3018 | ||
5b036ea1 BT |
3019 | battery_props = devm_kmemdup(&hidpp->hid_dev->dev, |
3020 | hidpp_battery_props, | |
3021 | sizeof(hidpp_battery_props), | |
3022 | GFP_KERNEL); | |
929b60a8 GS |
3023 | if (!battery_props) |
3024 | return -ENOMEM; | |
3025 | ||
5b036ea1 BT |
3026 | num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2; |
3027 | ||
3028 | if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE) | |
3029 | battery_props[num_battery_props++] = | |
3030 | POWER_SUPPLY_PROP_CAPACITY; | |
3031 | ||
3032 | if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS) | |
3033 | battery_props[num_battery_props++] = | |
3034 | POWER_SUPPLY_PROP_CAPACITY_LEVEL; | |
3035 | ||
a52ec107 BT |
3036 | battery = &hidpp->battery; |
3037 | ||
3038 | n = atomic_inc_return(&battery_no) - 1; | |
5b036ea1 BT |
3039 | desc->properties = battery_props; |
3040 | desc->num_properties = num_battery_props; | |
a52ec107 BT |
3041 | desc->get_property = hidpp_battery_get_property; |
3042 | sprintf(battery->name, "hidpp_battery_%ld", n); | |
3043 | desc->name = battery->name; | |
3044 | desc->type = POWER_SUPPLY_TYPE_BATTERY; | |
3045 | desc->use_for_apm = 0; | |
3046 | ||
3047 | battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev, | |
3048 | &battery->desc, | |
3049 | &cfg); | |
3050 | if (IS_ERR(battery->ps)) | |
3051 | return PTR_ERR(battery->ps); | |
3052 | ||
3053 | power_supply_powers(battery->ps, &hidpp->hid_dev->dev); | |
3054 | ||
3055 | return ret; | |
3056 | } | |
3057 | ||
843c624e | 3058 | static void hidpp_overwrite_name(struct hid_device *hdev) |
2f31c525 BT |
3059 | { |
3060 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
3061 | char *name; | |
2f31c525 | 3062 | |
843c624e | 3063 | if (hidpp->protocol_major < 2) |
b4f8ce07 | 3064 | return; |
843c624e BT |
3065 | |
3066 | name = hidpp_get_device_name(hidpp); | |
2f31c525 | 3067 | |
7bfd2927 | 3068 | if (!name) { |
2f31c525 | 3069 | hid_err(hdev, "unable to retrieve the name of the device"); |
7bfd2927 SW |
3070 | } else { |
3071 | dbg_hid("HID++: Got name: %s\n", name); | |
2f31c525 | 3072 | snprintf(hdev->name, sizeof(hdev->name), "%s", name); |
7bfd2927 | 3073 | } |
2f31c525 BT |
3074 | |
3075 | kfree(name); | |
3076 | } | |
3077 | ||
c39e3d5f BT |
3078 | static int hidpp_input_open(struct input_dev *dev) |
3079 | { | |
3080 | struct hid_device *hid = input_get_drvdata(dev); | |
3081 | ||
3082 | return hid_hw_open(hid); | |
3083 | } | |
3084 | ||
3085 | static void hidpp_input_close(struct input_dev *dev) | |
3086 | { | |
3087 | struct hid_device *hid = input_get_drvdata(dev); | |
3088 | ||
3089 | hid_hw_close(hid); | |
3090 | } | |
3091 | ||
3092 | static struct input_dev *hidpp_allocate_input(struct hid_device *hdev) | |
3093 | { | |
3094 | struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev); | |
005b3f57 | 3095 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
c39e3d5f BT |
3096 | |
3097 | if (!input_dev) | |
3098 | return NULL; | |
3099 | ||
3100 | input_set_drvdata(input_dev, hdev); | |
3101 | input_dev->open = hidpp_input_open; | |
3102 | input_dev->close = hidpp_input_close; | |
3103 | ||
005b3f57 | 3104 | input_dev->name = hidpp->name; |
c39e3d5f BT |
3105 | input_dev->phys = hdev->phys; |
3106 | input_dev->uniq = hdev->uniq; | |
3107 | input_dev->id.bustype = hdev->bus; | |
3108 | input_dev->id.vendor = hdev->vendor; | |
3109 | input_dev->id.product = hdev->product; | |
3110 | input_dev->id.version = hdev->version; | |
3111 | input_dev->dev.parent = &hdev->dev; | |
3112 | ||
3113 | return input_dev; | |
3114 | } | |
3115 | ||
3116 | static void hidpp_connect_event(struct hidpp_device *hidpp) | |
3117 | { | |
3118 | struct hid_device *hdev = hidpp->hid_dev; | |
3119 | int ret = 0; | |
3120 | bool connected = atomic_read(&hidpp->connected); | |
3121 | struct input_dev *input; | |
3122 | char *name, *devm_name; | |
c39e3d5f | 3123 | |
284f8d75 BT |
3124 | if (!connected) { |
3125 | if (hidpp->battery.ps) { | |
3126 | hidpp->battery.online = false; | |
3127 | hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN; | |
5b036ea1 | 3128 | hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; |
284f8d75 BT |
3129 | power_supply_changed(hidpp->battery.ps); |
3130 | } | |
2936836f | 3131 | return; |
284f8d75 | 3132 | } |
2936836f | 3133 | |
bf159447 BT |
3134 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { |
3135 | ret = wtp_connect(hdev, connected); | |
3136 | if (ret) | |
3137 | return; | |
8a09b4fa GB |
3138 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { |
3139 | ret = m560_send_config_command(hdev, connected); | |
3140 | if (ret) | |
3141 | return; | |
90cdd986 BT |
3142 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { |
3143 | ret = k400_connect(hdev, connected); | |
3144 | if (ret) | |
3145 | return; | |
bf159447 | 3146 | } |
586bdc4e | 3147 | |
580a7e82 BT |
3148 | /* the device is already connected, we can ask for its name and |
3149 | * protocol */ | |
c39e3d5f | 3150 | if (!hidpp->protocol_major) { |
090760d4 | 3151 | ret = hidpp_root_get_protocol_version(hidpp); |
c39e3d5f BT |
3152 | if (ret) { |
3153 | hid_err(hdev, "Can not get the protocol version.\n"); | |
3154 | return; | |
3155 | } | |
3156 | } | |
3157 | ||
187f2bba | 3158 | if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) { |
005b3f57 BT |
3159 | name = hidpp_get_device_name(hidpp); |
3160 | if (!name) { | |
3161 | hid_err(hdev, | |
3162 | "unable to retrieve the name of the device"); | |
3163 | return; | |
3164 | } | |
3165 | ||
3166 | devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name); | |
3167 | kfree(name); | |
3168 | if (!devm_name) | |
3169 | return; | |
3170 | ||
3171 | hidpp->name = devm_name; | |
3172 | } | |
3173 | ||
187f2bba BT |
3174 | hidpp_initialize_battery(hidpp); |
3175 | ||
9b9c519f | 3176 | /* forward current battery state */ |
7f7ce2a2 BT |
3177 | if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) { |
3178 | hidpp10_enable_battery_reporting(hidpp); | |
3179 | if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE) | |
3180 | hidpp10_query_battery_mileage(hidpp); | |
3181 | else | |
3182 | hidpp10_query_battery_status(hidpp); | |
3183 | } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) { | |
9b9c519f | 3184 | hidpp20_query_battery_info(hidpp); |
9b9c519f | 3185 | } |
7f7ce2a2 BT |
3186 | if (hidpp->battery.ps) |
3187 | power_supply_changed(hidpp->battery.ps); | |
9b9c519f | 3188 | |
4435ff2f HC |
3189 | if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) |
3190 | hi_res_scroll_enable(hidpp); | |
3191 | ||
2936836f BT |
3192 | if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input) |
3193 | /* if the input nodes are already created, we can stop now */ | |
187f2bba BT |
3194 | return; |
3195 | ||
c39e3d5f BT |
3196 | input = hidpp_allocate_input(hdev); |
3197 | if (!input) { | |
3198 | hid_err(hdev, "cannot allocate new input device: %d\n", ret); | |
3199 | return; | |
3200 | } | |
3201 | ||
c39e3d5f BT |
3202 | hidpp_populate_input(hidpp, input, false); |
3203 | ||
3204 | ret = input_register_device(input); | |
3205 | if (ret) | |
3206 | input_free_device(input); | |
3207 | ||
3208 | hidpp->delayed_input = input; | |
3209 | } | |
3210 | ||
a4bf6153 BT |
3211 | static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL); |
3212 | ||
3213 | static struct attribute *sysfs_attrs[] = { | |
3214 | &dev_attr_builtin_power_supply.attr, | |
3215 | NULL | |
3216 | }; | |
3217 | ||
35a33cb5 | 3218 | static const struct attribute_group ps_attribute_group = { |
a4bf6153 BT |
3219 | .attrs = sysfs_attrs |
3220 | }; | |
3221 | ||
fe3ee1ec BT |
3222 | static bool hidpp_validate_report(struct hid_device *hdev, int id, int size, |
3223 | bool optional) | |
3224 | { | |
3225 | struct hid_report_enum *re; | |
3226 | struct hid_report *report; | |
3227 | ||
3228 | if (id >= HID_MAX_IDS || id < 0) { | |
3229 | hid_err(hdev, "invalid HID report id %u\n", id); | |
3230 | return false; | |
3231 | } | |
3232 | ||
3233 | re = &(hdev->report_enum[HID_OUTPUT_REPORT]); | |
3234 | report = re->report_id_hash[id]; | |
3235 | ||
3236 | if (!report) | |
3237 | return optional; | |
3238 | ||
3239 | if (report->field[0]->report_count < size) { | |
3240 | hid_warn(hdev, "not enough values in hidpp report %d\n", id); | |
3241 | return false; | |
3242 | } | |
3243 | ||
3244 | return true; | |
3245 | } | |
3246 | ||
3247 | static bool hidpp_validate_device(struct hid_device *hdev) | |
3248 | { | |
3249 | return hidpp_validate_report(hdev, REPORT_ID_HIDPP_SHORT, | |
3250 | HIDPP_REPORT_SHORT_LENGTH - 1, false) && | |
3251 | hidpp_validate_report(hdev, REPORT_ID_HIDPP_LONG, | |
3252 | HIDPP_REPORT_LONG_LENGTH - 1, true) && | |
3253 | hidpp_validate_report(hdev, REPORT_ID_HIDPP_VERY_LONG, | |
3254 | HIDPP_REPORT_VERY_LONG_LENGTH - 1, true); | |
3255 | } | |
3256 | ||
2f31c525 BT |
3257 | static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) |
3258 | { | |
3259 | struct hidpp_device *hidpp; | |
3260 | int ret; | |
3261 | bool connected; | |
c39e3d5f | 3262 | unsigned int connect_mask = HID_CONNECT_DEFAULT; |
2f31c525 | 3263 | |
fe3ee1ec BT |
3264 | ret = hid_parse(hdev); |
3265 | if (ret) { | |
3266 | hid_err(hdev, "%s:parse failed\n", __func__); | |
3267 | return ret; | |
3268 | } | |
3269 | ||
3270 | /* | |
3271 | * Make sure the device is HID++ capable, otherwise treat as generic HID | |
3272 | */ | |
3273 | if (!hidpp_validate_device(hdev)) | |
3274 | return hid_hw_start(hdev, HID_CONNECT_DEFAULT); | |
3275 | ||
2f31c525 BT |
3276 | hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device), |
3277 | GFP_KERNEL); | |
3278 | if (!hidpp) | |
3279 | return -ENOMEM; | |
3280 | ||
3281 | hidpp->hid_dev = hdev; | |
005b3f57 | 3282 | hidpp->name = hdev->name; |
2f31c525 BT |
3283 | hid_set_drvdata(hdev, hidpp); |
3284 | ||
3285 | hidpp->quirks = id->driver_data; | |
3286 | ||
843c624e BT |
3287 | if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE) |
3288 | hidpp->quirks |= HIDPP_QUIRK_UNIFYING; | |
3289 | ||
9188dbae BT |
3290 | if (disable_raw_mode) { |
3291 | hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP; | |
580a7e82 | 3292 | hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT; |
9188dbae BT |
3293 | } |
3294 | ||
2f31c525 BT |
3295 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { |
3296 | ret = wtp_allocate(hdev, id); | |
3297 | if (ret) | |
43cd97af | 3298 | return ret; |
8a09b4fa GB |
3299 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { |
3300 | ret = m560_allocate(hdev); | |
3301 | if (ret) | |
43cd97af | 3302 | return ret; |
90cdd986 BT |
3303 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { |
3304 | ret = k400_allocate(hdev); | |
3305 | if (ret) | |
43cd97af | 3306 | return ret; |
2f31c525 BT |
3307 | } |
3308 | ||
c39e3d5f | 3309 | INIT_WORK(&hidpp->work, delayed_work_cb); |
2f31c525 BT |
3310 | mutex_init(&hidpp->send_mutex); |
3311 | init_waitqueue_head(&hidpp->wait); | |
3312 | ||
a4bf6153 BT |
3313 | /* indicates we are handling the battery properties in the kernel */ |
3314 | ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group); | |
3315 | if (ret) | |
3316 | hid_warn(hdev, "Cannot allocate sysfs group for %s\n", | |
3317 | hdev->name); | |
3318 | ||
91cf9a98 BT |
3319 | /* |
3320 | * Plain USB connections need to actually call start and open | |
3321 | * on the transport driver to allow incoming data. | |
3322 | */ | |
3323 | ret = hid_hw_start(hdev, 0); | |
3324 | if (ret) { | |
3325 | hid_err(hdev, "hw start failed\n"); | |
3326 | goto hid_hw_start_fail; | |
7bfd2927 SW |
3327 | } |
3328 | ||
91cf9a98 BT |
3329 | ret = hid_hw_open(hdev); |
3330 | if (ret < 0) { | |
3331 | dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", | |
3332 | __func__, ret); | |
3333 | hid_hw_stop(hdev); | |
3334 | goto hid_hw_open_fail; | |
3335 | } | |
7bfd2927 | 3336 | |
2f31c525 BT |
3337 | /* Allow incoming packets */ |
3338 | hid_device_io_start(hdev); | |
3339 | ||
843c624e BT |
3340 | if (hidpp->quirks & HIDPP_QUIRK_UNIFYING) |
3341 | hidpp_unifying_init(hidpp); | |
3342 | ||
090760d4 | 3343 | connected = hidpp_root_get_protocol_version(hidpp) == 0; |
843c624e BT |
3344 | atomic_set(&hidpp->connected, connected); |
3345 | if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) { | |
ab94e562 | 3346 | if (!connected) { |
b832da56 | 3347 | ret = -ENODEV; |
ab94e562 | 3348 | hid_err(hdev, "Device not connected"); |
91cf9a98 | 3349 | goto hid_hw_init_fail; |
ab94e562 | 3350 | } |
2f31c525 | 3351 | |
843c624e BT |
3352 | hidpp_overwrite_name(hdev); |
3353 | } | |
33797820 | 3354 | |
c39e3d5f | 3355 | if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) { |
2f31c525 BT |
3356 | ret = wtp_get_config(hidpp); |
3357 | if (ret) | |
91cf9a98 | 3358 | goto hid_hw_init_fail; |
7f4b49fe SW |
3359 | } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) { |
3360 | ret = g920_get_config(hidpp); | |
3361 | if (ret) | |
91cf9a98 | 3362 | goto hid_hw_init_fail; |
2f31c525 BT |
3363 | } |
3364 | ||
91cf9a98 | 3365 | hidpp_connect_event(hidpp); |
2f31c525 | 3366 | |
91cf9a98 BT |
3367 | /* Reset the HID node state */ |
3368 | hid_device_io_stop(hdev); | |
3369 | hid_hw_close(hdev); | |
3370 | hid_hw_stop(hdev); | |
2f31c525 | 3371 | |
91cf9a98 BT |
3372 | if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) |
3373 | connect_mask &= ~HID_CONNECT_HIDINPUT; | |
c39e3d5f | 3374 | |
91cf9a98 BT |
3375 | /* Now export the actual inputs and hidraw nodes to the world */ |
3376 | ret = hid_hw_start(hdev, connect_mask); | |
3377 | if (ret) { | |
3378 | hid_err(hdev, "%s:hid_hw_start returned error\n", __func__); | |
3379 | goto hid_hw_start_fail; | |
3380 | } | |
c39e3d5f | 3381 | |
2f31c525 BT |
3382 | return ret; |
3383 | ||
91cf9a98 BT |
3384 | hid_hw_init_fail: |
3385 | hid_hw_close(hdev); | |
3386 | hid_hw_open_fail: | |
3387 | hid_hw_stop(hdev); | |
2f31c525 | 3388 | hid_hw_start_fail: |
a4bf6153 | 3389 | sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group); |
c39e3d5f | 3390 | cancel_work_sync(&hidpp->work); |
2f31c525 | 3391 | mutex_destroy(&hidpp->send_mutex); |
2f31c525 BT |
3392 | return ret; |
3393 | } | |
3394 | ||
3395 | static void hidpp_remove(struct hid_device *hdev) | |
3396 | { | |
3397 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); | |
3398 | ||
fe3ee1ec BT |
3399 | if (!hidpp) |
3400 | return hid_hw_stop(hdev); | |
3401 | ||
a4bf6153 BT |
3402 | sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group); |
3403 | ||
91cf9a98 | 3404 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) |
ff21a635 | 3405 | hidpp_ff_deinit(hdev); |
91cf9a98 | 3406 | |
7bfd2927 | 3407 | hid_hw_stop(hdev); |
c39e3d5f | 3408 | cancel_work_sync(&hidpp->work); |
2f31c525 | 3409 | mutex_destroy(&hidpp->send_mutex); |
2f31c525 BT |
3410 | } |
3411 | ||
4435ff2f HC |
3412 | #define LDJ_DEVICE(product) \ |
3413 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \ | |
3414 | USB_VENDOR_ID_LOGITECH, (product)) | |
3415 | ||
2f31c525 | 3416 | static const struct hid_device_id hidpp_devices[] = { |
57ac86cf | 3417 | { /* wireless touchpad */ |
16767229 | 3418 | LDJ_DEVICE(0x4011), |
57ac86cf BT |
3419 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT | |
3420 | HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS }, | |
586bdc4e | 3421 | { /* wireless touchpad T650 */ |
16767229 | 3422 | LDJ_DEVICE(0x4101), |
586bdc4e | 3423 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT }, |
2f31c525 BT |
3424 | { /* wireless touchpad T651 */ |
3425 | HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, | |
3426 | USB_DEVICE_ID_LOGITECH_T651), | |
3427 | .driver_data = HIDPP_QUIRK_CLASS_WTP }, | |
4435ff2f HC |
3428 | { /* Mouse Logitech Anywhere MX */ |
3429 | LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 }, | |
3430 | { /* Mouse Logitech Cube */ | |
3431 | LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 }, | |
3432 | { /* Mouse Logitech M335 */ | |
3433 | LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3434 | { /* Mouse Logitech M515 */ | |
3435 | LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 }, | |
8a09b4fa | 3436 | { /* Mouse logitech M560 */ |
4435ff2f HC |
3437 | LDJ_DEVICE(0x402d), |
3438 | .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 | |
3439 | | HIDPP_QUIRK_HI_RES_SCROLL_X2120 }, | |
3440 | { /* Mouse Logitech M705 (firmware RQM17) */ | |
3441 | LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 }, | |
3442 | { /* Mouse Logitech M705 (firmware RQM67) */ | |
3443 | LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3444 | { /* Mouse Logitech M720 */ | |
3445 | LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3446 | { /* Mouse Logitech MX Anywhere 2 */ | |
3447 | LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3448 | { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3449 | { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3450 | { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3451 | { /* Mouse Logitech MX Anywhere 2S */ | |
3452 | LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3453 | { /* Mouse Logitech MX Master */ | |
3454 | LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3455 | { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3456 | { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3457 | { /* Mouse Logitech MX Master 2S */ | |
3458 | LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 }, | |
3459 | { /* Mouse Logitech Performance MX */ | |
3460 | LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 }, | |
90cdd986 | 3461 | { /* Keyboard logitech K400 */ |
16767229 | 3462 | LDJ_DEVICE(0x4024), |
6bd4e65d | 3463 | .driver_data = HIDPP_QUIRK_CLASS_K400 }, |
696ecef9 | 3464 | { /* Solar Keyboard Logitech K750 */ |
16767229 | 3465 | LDJ_DEVICE(0x4002), |
696ecef9 | 3466 | .driver_data = HIDPP_QUIRK_CLASS_K750 }, |
ab94e562 | 3467 | |
16767229 | 3468 | { LDJ_DEVICE(HID_ANY_ID) }, |
7bfd2927 | 3469 | |
91cf9a98 BT |
3470 | { /* Logitech G403 Gaming Mouse over USB */ |
3471 | HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) }, | |
3472 | { /* Logitech G700 Gaming Mouse over USB */ | |
3473 | HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC06B) }, | |
3474 | { /* Logitech G900 Gaming Mouse over USB */ | |
3475 | HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) }, | |
3476 | { /* Logitech G920 Wheel over USB */ | |
3477 | HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL), | |
7bfd2927 | 3478 | .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS}, |
2f31c525 BT |
3479 | {} |
3480 | }; | |
3481 | ||
3482 | MODULE_DEVICE_TABLE(hid, hidpp_devices); | |
3483 | ||
4435ff2f HC |
3484 | static const struct hid_usage_id hidpp_usages[] = { |
3485 | { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES }, | |
3486 | { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} | |
3487 | }; | |
3488 | ||
2f31c525 BT |
3489 | static struct hid_driver hidpp_driver = { |
3490 | .name = "logitech-hidpp-device", | |
3491 | .id_table = hidpp_devices, | |
3492 | .probe = hidpp_probe, | |
3493 | .remove = hidpp_remove, | |
3494 | .raw_event = hidpp_raw_event, | |
4435ff2f HC |
3495 | .usage_table = hidpp_usages, |
3496 | .event = hidpp_event, | |
2f31c525 BT |
3497 | .input_configured = hidpp_input_configured, |
3498 | .input_mapping = hidpp_input_mapping, | |
0b1804e3 | 3499 | .input_mapped = hidpp_input_mapped, |
2f31c525 BT |
3500 | }; |
3501 | ||
3502 | module_hid_driver(hidpp_driver); |