1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HIDPP protocol for Logitech receivers
4 *
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
8 */
9
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/device.h>
14 #include <linux/input.h>
15 #include <linux/usb.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/sched/clock.h>
21 #include <linux/kfifo.h>
22 #include <linux/input/mt.h>
23 #include <linux/workqueue.h>
24 #include <linux/atomic.h>
25 #include <linux/fixp-arith.h>
26 #include <asm/unaligned.h>
27 #include "usbhid/usbhid.h"
28 #include "hid-ids.h"
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
33
34 static bool disable_tap_to_click;
35 module_param(disable_tap_to_click, bool, 0644);
36 MODULE_PARM_DESC(disable_tap_to_click,
37 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
38
39 /* Define a non-zero software ID to identify our own requests */
40 #define LINUX_KERNEL_SW_ID 0x01
41
42 #define REPORT_ID_HIDPP_SHORT 0x10
43 #define REPORT_ID_HIDPP_LONG 0x11
44 #define REPORT_ID_HIDPP_VERY_LONG 0x12
45
46 #define HIDPP_REPORT_SHORT_LENGTH 7
47 #define HIDPP_REPORT_LONG_LENGTH 20
48 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
49
50 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
51 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
52 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
53
54 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
55 #define HIDPP_SUB_ID_ROLLER 0x05
56 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
57 #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
58 #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
59
60 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
61 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
62 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
63 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
64 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
65
66 /* bits 2..20 are reserved for classes */
67 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
68 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
69 #define HIDPP_QUIRK_DELAYED_INIT BIT(23)
70 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
71 #define HIDPP_QUIRK_UNIFYING BIT(25)
72 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(26)
73 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(27)
74 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(28)
75 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(29)
76
77 /* These are just aliases for now */
78 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
79 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
80
81 /* Convenience constant to check for any high-res support. */
82 #define HIDPP_CAPABILITY_HI_RES_SCROLL (HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \
83 HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \
84 HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL)
85
86 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
87 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
88 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
89 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
90 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
91 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
92 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
93 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL BIT(7)
94 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL BIT(8)
95 #define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL BIT(9)
96
97 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
98
99 /*
100 * There are two hidpp protocols in use, the first version hidpp10 is known
101 * as register access protocol or RAP, the second version hidpp20 is known as
102 * feature access protocol or FAP
103 *
104 * Most older devices (including the Unifying usb receiver) use the RAP protocol
105 * where as most newer devices use the FAP protocol. Both protocols are
106 * compatible with the underlying transport, which could be usb, Unifiying, or
107 * bluetooth. The message lengths are defined by the hid vendor specific report
108 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
109 * the HIDPP_LONG report type (total message length 20 bytes)
110 *
111 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
112 * messages. The Unifying receiver itself responds to RAP messages (device index
113 * is 0xFF for the receiver), and all messages (short or long) with a device
114 * index between 1 and 6 are passed untouched to the corresponding paired
115 * Unifying device.
116 *
117 * The paired device can be RAP or FAP, it will receive the message untouched
118 * from the Unifiying receiver.
119 */
120
121 struct fap {
122 u8 feature_index;
123 u8 funcindex_clientid;
124 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
125 };
126
127 struct rap {
128 u8 sub_id;
129 u8 reg_address;
130 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
131 };
132
133 struct hidpp_report {
134 u8 report_id;
135 u8 device_index;
136 union {
137 struct fap fap;
138 struct rap rap;
139 u8 rawbytes[sizeof(struct fap)];
140 };
141 } __packed;
142
143 struct hidpp_battery {
144 u8 feature_index;
145 u8 solar_feature_index;
146 u8 voltage_feature_index;
147 struct power_supply_desc desc;
148 struct power_supply *ps;
149 char name[64];
150 int status;
151 int capacity;
152 int level;
153 int voltage;
154 int charge_type;
155 bool online;
156 u8 supported_levels_1004;
157 };
158
159 /**
160 * struct hidpp_scroll_counter - Utility class for processing high-resolution
161 * scroll events.
162 * @dev: the input device for which events should be reported.
163 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
164 * @remainder: counts the number of high-resolution units moved since the last
165 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
166 * only be used by class methods.
167 * @direction: direction of last movement (1 or -1)
168 * @last_time: last event time, used to reset remainder after inactivity
169 */
170 struct hidpp_scroll_counter {
171 int wheel_multiplier;
172 int remainder;
173 int direction;
174 unsigned long long last_time;
175 };
176
177 struct hidpp_device {
178 struct hid_device *hid_dev;
179 struct input_dev *input;
180 struct mutex send_mutex;
181 void *send_receive_buf;
182 char *name; /* will never be NULL and should not be freed */
183 wait_queue_head_t wait;
184 int very_long_report_length;
185 bool answer_available;
186 u8 protocol_major;
187 u8 protocol_minor;
188
189 void *private_data;
190
191 struct work_struct work;
192 struct kfifo delayed_work_fifo;
193 atomic_t connected;
194 struct input_dev *delayed_input;
195
196 unsigned long quirks;
197 unsigned long capabilities;
198 u8 supported_reports;
199
200 struct hidpp_battery battery;
201 struct hidpp_scroll_counter vertical_wheel_counter;
202
203 u8 wireless_feature_index;
204 };
205
206 /* HID++ 1.0 error codes */
207 #define HIDPP_ERROR 0x8f
208 #define HIDPP_ERROR_SUCCESS 0x00
209 #define HIDPP_ERROR_INVALID_SUBID 0x01
210 #define HIDPP_ERROR_INVALID_ADRESS 0x02
211 #define HIDPP_ERROR_INVALID_VALUE 0x03
212 #define HIDPP_ERROR_CONNECT_FAIL 0x04
213 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
214 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
215 #define HIDPP_ERROR_BUSY 0x07
216 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
217 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
218 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
219 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
220 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
221 /* HID++ 2.0 error codes */
222 #define HIDPP20_ERROR 0xff
223
224 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
225
__hidpp_send_report(struct hid_device * hdev,struct hidpp_report * hidpp_report)226 static int __hidpp_send_report(struct hid_device *hdev,
227 struct hidpp_report *hidpp_report)
228 {
229 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
230 int fields_count, ret;
231
232 switch (hidpp_report->report_id) {
233 case REPORT_ID_HIDPP_SHORT:
234 fields_count = HIDPP_REPORT_SHORT_LENGTH;
235 break;
236 case REPORT_ID_HIDPP_LONG:
237 fields_count = HIDPP_REPORT_LONG_LENGTH;
238 break;
239 case REPORT_ID_HIDPP_VERY_LONG:
240 fields_count = hidpp->very_long_report_length;
241 break;
242 default:
243 return -ENODEV;
244 }
245
246 /*
247 * set the device_index as the receiver, it will be overwritten by
248 * hid_hw_request if needed
249 */
250 hidpp_report->device_index = 0xff;
251
252 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
253 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
254 } else {
255 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
256 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
257 HID_REQ_SET_REPORT);
258 }
259
260 return ret == fields_count ? 0 : -1;
261 }
262
263 /*
264 * hidpp_send_message_sync() returns 0 in case of success, and something else
265 * in case of a failure.
266 * - If ' something else' is positive, that means that an error has been raised
267 * by the protocol itself.
268 * - If ' something else' is negative, that means that we had a classic error
269 * (-ENOMEM, -EPIPE, etc...)
270 */
hidpp_send_message_sync(struct hidpp_device * hidpp,struct hidpp_report * message,struct hidpp_report * response)271 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
272 struct hidpp_report *message,
273 struct hidpp_report *response)
274 {
275 int ret;
276
277 mutex_lock(&hidpp->send_mutex);
278
279 hidpp->send_receive_buf = response;
280 hidpp->answer_available = false;
281
282 /*
283 * So that we can later validate the answer when it arrives
284 * in hidpp_raw_event
285 */
286 *response = *message;
287
288 ret = __hidpp_send_report(hidpp->hid_dev, message);
289
290 if (ret) {
291 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
292 memset(response, 0, sizeof(struct hidpp_report));
293 goto exit;
294 }
295
296 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
297 5*HZ)) {
298 dbg_hid("%s:timeout waiting for response\n", __func__);
299 memset(response, 0, sizeof(struct hidpp_report));
300 ret = -ETIMEDOUT;
301 }
302
303 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
304 response->rap.sub_id == HIDPP_ERROR) {
305 ret = response->rap.params[1];
306 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
307 goto exit;
308 }
309
310 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
311 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
312 response->fap.feature_index == HIDPP20_ERROR) {
313 ret = response->fap.params[1];
314 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
315 goto exit;
316 }
317
318 exit:
319 mutex_unlock(&hidpp->send_mutex);
320 return ret;
321
322 }
323
hidpp_send_fap_command_sync(struct hidpp_device * hidpp,u8 feat_index,u8 funcindex_clientid,u8 * params,int param_count,struct hidpp_report * response)324 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
325 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
326 struct hidpp_report *response)
327 {
328 struct hidpp_report *message;
329 int ret;
330
331 if (param_count > sizeof(message->fap.params))
332 return -EINVAL;
333
334 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
335 if (!message)
336 return -ENOMEM;
337
338 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
339 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
340 else
341 message->report_id = REPORT_ID_HIDPP_LONG;
342 message->fap.feature_index = feat_index;
343 message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID;
344 memcpy(&message->fap.params, params, param_count);
345
346 ret = hidpp_send_message_sync(hidpp, message, response);
347 kfree(message);
348 return ret;
349 }
350
hidpp_send_rap_command_sync(struct hidpp_device * hidpp_dev,u8 report_id,u8 sub_id,u8 reg_address,u8 * params,int param_count,struct hidpp_report * response)351 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
352 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
353 struct hidpp_report *response)
354 {
355 struct hidpp_report *message;
356 int ret, max_count;
357
358 /* Send as long report if short reports are not supported. */
359 if (report_id == REPORT_ID_HIDPP_SHORT &&
360 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
361 report_id = REPORT_ID_HIDPP_LONG;
362
363 switch (report_id) {
364 case REPORT_ID_HIDPP_SHORT:
365 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
366 break;
367 case REPORT_ID_HIDPP_LONG:
368 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
369 break;
370 case REPORT_ID_HIDPP_VERY_LONG:
371 max_count = hidpp_dev->very_long_report_length - 4;
372 break;
373 default:
374 return -EINVAL;
375 }
376
377 if (param_count > max_count)
378 return -EINVAL;
379
380 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
381 if (!message)
382 return -ENOMEM;
383 message->report_id = report_id;
384 message->rap.sub_id = sub_id;
385 message->rap.reg_address = reg_address;
386 memcpy(&message->rap.params, params, param_count);
387
388 ret = hidpp_send_message_sync(hidpp_dev, message, response);
389 kfree(message);
390 return ret;
391 }
392
delayed_work_cb(struct work_struct * work)393 static void delayed_work_cb(struct work_struct *work)
394 {
395 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
396 work);
397 hidpp_connect_event(hidpp);
398 }
399
hidpp_match_answer(struct hidpp_report * question,struct hidpp_report * answer)400 static inline bool hidpp_match_answer(struct hidpp_report *question,
401 struct hidpp_report *answer)
402 {
403 return (answer->fap.feature_index == question->fap.feature_index) &&
404 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
405 }
406
hidpp_match_error(struct hidpp_report * question,struct hidpp_report * answer)407 static inline bool hidpp_match_error(struct hidpp_report *question,
408 struct hidpp_report *answer)
409 {
410 return ((answer->rap.sub_id == HIDPP_ERROR) ||
411 (answer->fap.feature_index == HIDPP20_ERROR)) &&
412 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
413 (answer->fap.params[0] == question->fap.funcindex_clientid);
414 }
415
hidpp_report_is_connect_event(struct hidpp_device * hidpp,struct hidpp_report * report)416 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
417 struct hidpp_report *report)
418 {
419 return (hidpp->wireless_feature_index &&
420 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
421 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
422 (report->rap.sub_id == 0x41));
423 }
424
425 /*
426 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
427 */
hidpp_prefix_name(char ** name,int name_length)428 static void hidpp_prefix_name(char **name, int name_length)
429 {
430 #define PREFIX_LENGTH 9 /* "Logitech " */
431
432 int new_length;
433 char *new_name;
434
435 if (name_length > PREFIX_LENGTH &&
436 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
437 /* The prefix has is already in the name */
438 return;
439
440 new_length = PREFIX_LENGTH + name_length;
441 new_name = kzalloc(new_length, GFP_KERNEL);
442 if (!new_name)
443 return;
444
445 snprintf(new_name, new_length, "Logitech %s", *name);
446
447 kfree(*name);
448
449 *name = new_name;
450 }
451
452 /**
453 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
454 * events given a high-resolution wheel
455 * movement.
456 * @input_dev: Pointer to the input device
457 * @counter: a hid_scroll_counter struct describing the wheel.
458 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
459 * units.
460 *
461 * Given a high-resolution movement, this function converts the movement into
462 * fractions of 120 and emits high-resolution scroll events for the input
463 * device. It also uses the multiplier from &struct hid_scroll_counter to
464 * emit low-resolution scroll events when appropriate for
465 * backwards-compatibility with userspace input libraries.
466 */
hidpp_scroll_counter_handle_scroll(struct input_dev * input_dev,struct hidpp_scroll_counter * counter,int hi_res_value)467 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
468 struct hidpp_scroll_counter *counter,
469 int hi_res_value)
470 {
471 int low_res_value, remainder, direction;
472 unsigned long long now, previous;
473
474 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
475 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
476
477 remainder = counter->remainder;
478 direction = hi_res_value > 0 ? 1 : -1;
479
480 now = sched_clock();
481 previous = counter->last_time;
482 counter->last_time = now;
483 /*
484 * Reset the remainder after a period of inactivity or when the
485 * direction changes. This prevents the REL_WHEEL emulation point
486 * from sliding for devices that don't always provide the same
487 * number of movements per detent.
488 */
489 if (now - previous > 1000000000 || direction != counter->direction)
490 remainder = 0;
491
492 counter->direction = direction;
493 remainder += hi_res_value;
494
495 /* Some wheels will rest 7/8ths of a detent from the previous detent
496 * after slow movement, so we want the threshold for low-res events to
497 * be in the middle between two detents (e.g. after 4/8ths) as
498 * opposed to on the detents themselves (8/8ths).
499 */
500 if (abs(remainder) >= 60) {
501 /* Add (or subtract) 1 because we want to trigger when the wheel
502 * is half-way to the next detent (i.e. scroll 1 detent after a
503 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
504 * etc.).
505 */
506 low_res_value = remainder / 120;
507 if (low_res_value == 0)
508 low_res_value = (hi_res_value > 0 ? 1 : -1);
509 input_report_rel(input_dev, REL_WHEEL, low_res_value);
510 remainder -= low_res_value * 120;
511 }
512 counter->remainder = remainder;
513 }
514
515 /* -------------------------------------------------------------------------- */
516 /* HIDP++ 1.0 commands */
517 /* -------------------------------------------------------------------------- */
518
519 #define HIDPP_SET_REGISTER 0x80
520 #define HIDPP_GET_REGISTER 0x81
521 #define HIDPP_SET_LONG_REGISTER 0x82
522 #define HIDPP_GET_LONG_REGISTER 0x83
523
524 /**
525 * hidpp10_set_register - Modify a HID++ 1.0 register.
526 * @hidpp_dev: the device to set the register on.
527 * @register_address: the address of the register to modify.
528 * @byte: the byte of the register to modify. Should be less than 3.
529 * @mask: mask of the bits to modify
530 * @value: new values for the bits in mask
531 * Return: 0 if successful, otherwise a negative error code.
532 */
hidpp10_set_register(struct hidpp_device * hidpp_dev,u8 register_address,u8 byte,u8 mask,u8 value)533 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
534 u8 register_address, u8 byte, u8 mask, u8 value)
535 {
536 struct hidpp_report response;
537 int ret;
538 u8 params[3] = { 0 };
539
540 ret = hidpp_send_rap_command_sync(hidpp_dev,
541 REPORT_ID_HIDPP_SHORT,
542 HIDPP_GET_REGISTER,
543 register_address,
544 NULL, 0, &response);
545 if (ret)
546 return ret;
547
548 memcpy(params, response.rap.params, 3);
549
550 params[byte] &= ~mask;
551 params[byte] |= value & mask;
552
553 return hidpp_send_rap_command_sync(hidpp_dev,
554 REPORT_ID_HIDPP_SHORT,
555 HIDPP_SET_REGISTER,
556 register_address,
557 params, 3, &response);
558 }
559
560 #define HIDPP_REG_ENABLE_REPORTS 0x00
561 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
562 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
563 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
564 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
565 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
566
hidpp10_enable_battery_reporting(struct hidpp_device * hidpp_dev)567 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
568 {
569 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
570 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
571 }
572
573 #define HIDPP_REG_FEATURES 0x01
574 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
575 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
576
577 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
hidpp10_enable_scrolling_acceleration(struct hidpp_device * hidpp_dev)578 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
579 {
580 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
581 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
582 }
583
584 #define HIDPP_REG_BATTERY_STATUS 0x07
585
hidpp10_battery_status_map_level(u8 param)586 static int hidpp10_battery_status_map_level(u8 param)
587 {
588 int level;
589
590 switch (param) {
591 case 1 ... 2:
592 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
593 break;
594 case 3 ... 4:
595 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
596 break;
597 case 5 ... 6:
598 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
599 break;
600 case 7:
601 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
602 break;
603 default:
604 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
605 }
606
607 return level;
608 }
609
hidpp10_battery_status_map_status(u8 param)610 static int hidpp10_battery_status_map_status(u8 param)
611 {
612 int status;
613
614 switch (param) {
615 case 0x00:
616 /* discharging (in use) */
617 status = POWER_SUPPLY_STATUS_DISCHARGING;
618 break;
619 case 0x21: /* (standard) charging */
620 case 0x24: /* fast charging */
621 case 0x25: /* slow charging */
622 status = POWER_SUPPLY_STATUS_CHARGING;
623 break;
624 case 0x26: /* topping charge */
625 case 0x22: /* charge complete */
626 status = POWER_SUPPLY_STATUS_FULL;
627 break;
628 case 0x20: /* unknown */
629 status = POWER_SUPPLY_STATUS_UNKNOWN;
630 break;
631 /*
632 * 0x01...0x1F = reserved (not charging)
633 * 0x23 = charging error
634 * 0x27..0xff = reserved
635 */
636 default:
637 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
638 break;
639 }
640
641 return status;
642 }
643
hidpp10_query_battery_status(struct hidpp_device * hidpp)644 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
645 {
646 struct hidpp_report response;
647 int ret, status;
648
649 ret = hidpp_send_rap_command_sync(hidpp,
650 REPORT_ID_HIDPP_SHORT,
651 HIDPP_GET_REGISTER,
652 HIDPP_REG_BATTERY_STATUS,
653 NULL, 0, &response);
654 if (ret)
655 return ret;
656
657 hidpp->battery.level =
658 hidpp10_battery_status_map_level(response.rap.params[0]);
659 status = hidpp10_battery_status_map_status(response.rap.params[1]);
660 hidpp->battery.status = status;
661 /* the capacity is only available when discharging or full */
662 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
663 status == POWER_SUPPLY_STATUS_FULL;
664
665 return 0;
666 }
667
668 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
669
hidpp10_battery_mileage_map_status(u8 param)670 static int hidpp10_battery_mileage_map_status(u8 param)
671 {
672 int status;
673
674 switch (param >> 6) {
675 case 0x00:
676 /* discharging (in use) */
677 status = POWER_SUPPLY_STATUS_DISCHARGING;
678 break;
679 case 0x01: /* charging */
680 status = POWER_SUPPLY_STATUS_CHARGING;
681 break;
682 case 0x02: /* charge complete */
683 status = POWER_SUPPLY_STATUS_FULL;
684 break;
685 /*
686 * 0x03 = charging error
687 */
688 default:
689 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
690 break;
691 }
692
693 return status;
694 }
695
hidpp10_query_battery_mileage(struct hidpp_device * hidpp)696 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
697 {
698 struct hidpp_report response;
699 int ret, status;
700
701 ret = hidpp_send_rap_command_sync(hidpp,
702 REPORT_ID_HIDPP_SHORT,
703 HIDPP_GET_REGISTER,
704 HIDPP_REG_BATTERY_MILEAGE,
705 NULL, 0, &response);
706 if (ret)
707 return ret;
708
709 hidpp->battery.capacity = response.rap.params[0];
710 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
711 hidpp->battery.status = status;
712 /* the capacity is only available when discharging or full */
713 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
714 status == POWER_SUPPLY_STATUS_FULL;
715
716 return 0;
717 }
718
hidpp10_battery_event(struct hidpp_device * hidpp,u8 * data,int size)719 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
720 {
721 struct hidpp_report *report = (struct hidpp_report *)data;
722 int status, capacity, level;
723 bool changed;
724
725 if (report->report_id != REPORT_ID_HIDPP_SHORT)
726 return 0;
727
728 switch (report->rap.sub_id) {
729 case HIDPP_REG_BATTERY_STATUS:
730 capacity = hidpp->battery.capacity;
731 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
732 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
733 break;
734 case HIDPP_REG_BATTERY_MILEAGE:
735 capacity = report->rap.params[0];
736 level = hidpp->battery.level;
737 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
738 break;
739 default:
740 return 0;
741 }
742
743 changed = capacity != hidpp->battery.capacity ||
744 level != hidpp->battery.level ||
745 status != hidpp->battery.status;
746
747 /* the capacity is only available when discharging or full */
748 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
749 status == POWER_SUPPLY_STATUS_FULL;
750
751 if (changed) {
752 hidpp->battery.level = level;
753 hidpp->battery.status = status;
754 if (hidpp->battery.ps)
755 power_supply_changed(hidpp->battery.ps);
756 }
757
758 return 0;
759 }
760
761 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
762 #define HIDPP_EXTENDED_PAIRING 0x30
763 #define HIDPP_DEVICE_NAME 0x40
764
hidpp_unifying_get_name(struct hidpp_device * hidpp_dev)765 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
766 {
767 struct hidpp_report response;
768 int ret;
769 u8 params[1] = { HIDPP_DEVICE_NAME };
770 char *name;
771 int len;
772
773 ret = hidpp_send_rap_command_sync(hidpp_dev,
774 REPORT_ID_HIDPP_SHORT,
775 HIDPP_GET_LONG_REGISTER,
776 HIDPP_REG_PAIRING_INFORMATION,
777 params, 1, &response);
778 if (ret)
779 return NULL;
780
781 len = response.rap.params[1];
782
783 if (2 + len > sizeof(response.rap.params))
784 return NULL;
785
786 if (len < 4) /* logitech devices are usually at least Xddd */
787 return NULL;
788
789 name = kzalloc(len + 1, GFP_KERNEL);
790 if (!name)
791 return NULL;
792
793 memcpy(name, &response.rap.params[2], len);
794
795 /* include the terminating '\0' */
796 hidpp_prefix_name(&name, len + 1);
797
798 return name;
799 }
800
hidpp_unifying_get_serial(struct hidpp_device * hidpp,u32 * serial)801 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
802 {
803 struct hidpp_report response;
804 int ret;
805 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
806
807 ret = hidpp_send_rap_command_sync(hidpp,
808 REPORT_ID_HIDPP_SHORT,
809 HIDPP_GET_LONG_REGISTER,
810 HIDPP_REG_PAIRING_INFORMATION,
811 params, 1, &response);
812 if (ret)
813 return ret;
814
815 /*
816 * We don't care about LE or BE, we will output it as a string
817 * with %4phD, so we need to keep the order.
818 */
819 *serial = *((u32 *)&response.rap.params[1]);
820 return 0;
821 }
822
hidpp_unifying_init(struct hidpp_device * hidpp)823 static int hidpp_unifying_init(struct hidpp_device *hidpp)
824 {
825 struct hid_device *hdev = hidpp->hid_dev;
826 const char *name;
827 u32 serial;
828 int ret;
829
830 ret = hidpp_unifying_get_serial(hidpp, &serial);
831 if (ret)
832 return ret;
833
834 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
835 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
836
837 name = hidpp_unifying_get_name(hidpp);
838 if (!name)
839 return -EIO;
840
841 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
842 dbg_hid("HID++ Unifying: Got name: %s\n", name);
843
844 kfree(name);
845 return 0;
846 }
847
848 /* -------------------------------------------------------------------------- */
849 /* 0x0000: Root */
850 /* -------------------------------------------------------------------------- */
851
852 #define HIDPP_PAGE_ROOT 0x0000
853 #define HIDPP_PAGE_ROOT_IDX 0x00
854
855 #define CMD_ROOT_GET_FEATURE 0x00
856 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x10
857
hidpp_root_get_feature(struct hidpp_device * hidpp,u16 feature,u8 * feature_index,u8 * feature_type)858 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
859 u8 *feature_index, u8 *feature_type)
860 {
861 struct hidpp_report response;
862 int ret;
863 u8 params[2] = { feature >> 8, feature & 0x00FF };
864
865 ret = hidpp_send_fap_command_sync(hidpp,
866 HIDPP_PAGE_ROOT_IDX,
867 CMD_ROOT_GET_FEATURE,
868 params, 2, &response);
869 if (ret)
870 return ret;
871
872 if (response.fap.params[0] == 0)
873 return -ENOENT;
874
875 *feature_index = response.fap.params[0];
876 *feature_type = response.fap.params[1];
877
878 return ret;
879 }
880
hidpp_root_get_protocol_version(struct hidpp_device * hidpp)881 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
882 {
883 const u8 ping_byte = 0x5a;
884 u8 ping_data[3] = { 0, 0, ping_byte };
885 struct hidpp_report response;
886 int ret;
887
888 ret = hidpp_send_rap_command_sync(hidpp,
889 REPORT_ID_HIDPP_SHORT,
890 HIDPP_PAGE_ROOT_IDX,
891 CMD_ROOT_GET_PROTOCOL_VERSION,
892 ping_data, sizeof(ping_data), &response);
893
894 if (ret == HIDPP_ERROR_INVALID_SUBID) {
895 hidpp->protocol_major = 1;
896 hidpp->protocol_minor = 0;
897 goto print_version;
898 }
899
900 /* the device might not be connected */
901 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
902 return -EIO;
903
904 if (ret > 0) {
905 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
906 __func__, ret);
907 return -EPROTO;
908 }
909 if (ret)
910 return ret;
911
912 if (response.rap.params[2] != ping_byte) {
913 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
914 __func__, response.rap.params[2], ping_byte);
915 return -EPROTO;
916 }
917
918 hidpp->protocol_major = response.rap.params[0];
919 hidpp->protocol_minor = response.rap.params[1];
920
921 print_version:
922 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
923 hidpp->protocol_major, hidpp->protocol_minor);
924 return 0;
925 }
926
927 /* -------------------------------------------------------------------------- */
928 /* 0x0003: Device Information */
929 /* -------------------------------------------------------------------------- */
930
931 #define HIDPP_PAGE_DEVICE_INFORMATION 0x0003
932
933 #define CMD_GET_DEVICE_INFO 0x00
934
hidpp_get_serial(struct hidpp_device * hidpp,u32 * serial)935 static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
936 {
937 struct hidpp_report response;
938 u8 feature_type;
939 u8 feature_index;
940 int ret;
941
942 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
943 &feature_index,
944 &feature_type);
945 if (ret)
946 return ret;
947
948 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
949 CMD_GET_DEVICE_INFO,
950 NULL, 0, &response);
951 if (ret)
952 return ret;
953
954 /* See hidpp_unifying_get_serial() */
955 *serial = *((u32 *)&response.rap.params[1]);
956 return 0;
957 }
958
hidpp_serial_init(struct hidpp_device * hidpp)959 static int hidpp_serial_init(struct hidpp_device *hidpp)
960 {
961 struct hid_device *hdev = hidpp->hid_dev;
962 u32 serial;
963 int ret;
964
965 ret = hidpp_get_serial(hidpp, &serial);
966 if (ret)
967 return ret;
968
969 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
970 dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
971
972 return 0;
973 }
974
975 /* -------------------------------------------------------------------------- */
976 /* 0x0005: GetDeviceNameType */
977 /* -------------------------------------------------------------------------- */
978
979 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
980
981 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x00
982 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x10
983 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x20
984
hidpp_devicenametype_get_count(struct hidpp_device * hidpp,u8 feature_index,u8 * nameLength)985 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
986 u8 feature_index, u8 *nameLength)
987 {
988 struct hidpp_report response;
989 int ret;
990
991 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
992 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
993
994 if (ret > 0) {
995 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
996 __func__, ret);
997 return -EPROTO;
998 }
999 if (ret)
1000 return ret;
1001
1002 *nameLength = response.fap.params[0];
1003
1004 return ret;
1005 }
1006
hidpp_devicenametype_get_device_name(struct hidpp_device * hidpp,u8 feature_index,u8 char_index,char * device_name,int len_buf)1007 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
1008 u8 feature_index, u8 char_index, char *device_name, int len_buf)
1009 {
1010 struct hidpp_report response;
1011 int ret, i;
1012 int count;
1013
1014 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1015 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
1016 &response);
1017
1018 if (ret > 0) {
1019 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1020 __func__, ret);
1021 return -EPROTO;
1022 }
1023 if (ret)
1024 return ret;
1025
1026 switch (response.report_id) {
1027 case REPORT_ID_HIDPP_VERY_LONG:
1028 count = hidpp->very_long_report_length - 4;
1029 break;
1030 case REPORT_ID_HIDPP_LONG:
1031 count = HIDPP_REPORT_LONG_LENGTH - 4;
1032 break;
1033 case REPORT_ID_HIDPP_SHORT:
1034 count = HIDPP_REPORT_SHORT_LENGTH - 4;
1035 break;
1036 default:
1037 return -EPROTO;
1038 }
1039
1040 if (len_buf < count)
1041 count = len_buf;
1042
1043 for (i = 0; i < count; i++)
1044 device_name[i] = response.fap.params[i];
1045
1046 return count;
1047 }
1048
hidpp_get_device_name(struct hidpp_device * hidpp)1049 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1050 {
1051 u8 feature_type;
1052 u8 feature_index;
1053 u8 __name_length;
1054 char *name;
1055 unsigned index = 0;
1056 int ret;
1057
1058 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1059 &feature_index, &feature_type);
1060 if (ret)
1061 return NULL;
1062
1063 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1064 &__name_length);
1065 if (ret)
1066 return NULL;
1067
1068 name = kzalloc(__name_length + 1, GFP_KERNEL);
1069 if (!name)
1070 return NULL;
1071
1072 while (index < __name_length) {
1073 ret = hidpp_devicenametype_get_device_name(hidpp,
1074 feature_index, index, name + index,
1075 __name_length - index);
1076 if (ret <= 0) {
1077 kfree(name);
1078 return NULL;
1079 }
1080 index += ret;
1081 }
1082
1083 /* include the terminating '\0' */
1084 hidpp_prefix_name(&name, __name_length + 1);
1085
1086 return name;
1087 }
1088
1089 /* -------------------------------------------------------------------------- */
1090 /* 0x1000: Battery level status */
1091 /* -------------------------------------------------------------------------- */
1092
1093 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1094
1095 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1096 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1097
1098 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1099
1100 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1101 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1102 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1103
hidpp_map_battery_level(int capacity)1104 static int hidpp_map_battery_level(int capacity)
1105 {
1106 if (capacity < 11)
1107 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1108 /*
1109 * The spec says this should be < 31 but some devices report 30
1110 * with brand new batteries and Windows reports 30 as "Good".
1111 */
1112 else if (capacity < 30)
1113 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1114 else if (capacity < 81)
1115 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1116 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1117 }
1118
hidpp20_batterylevel_map_status_capacity(u8 data[3],int * capacity,int * next_capacity,int * level)1119 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1120 int *next_capacity,
1121 int *level)
1122 {
1123 int status;
1124
1125 *capacity = data[0];
1126 *next_capacity = data[1];
1127 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1128
1129 /* When discharging, we can rely on the device reported capacity.
1130 * For all other states the device reports 0 (unknown).
1131 */
1132 switch (data[2]) {
1133 case 0: /* discharging (in use) */
1134 status = POWER_SUPPLY_STATUS_DISCHARGING;
1135 *level = hidpp_map_battery_level(*capacity);
1136 break;
1137 case 1: /* recharging */
1138 status = POWER_SUPPLY_STATUS_CHARGING;
1139 break;
1140 case 2: /* charge in final stage */
1141 status = POWER_SUPPLY_STATUS_CHARGING;
1142 break;
1143 case 3: /* charge complete */
1144 status = POWER_SUPPLY_STATUS_FULL;
1145 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1146 *capacity = 100;
1147 break;
1148 case 4: /* recharging below optimal speed */
1149 status = POWER_SUPPLY_STATUS_CHARGING;
1150 break;
1151 /* 5 = invalid battery type
1152 6 = thermal error
1153 7 = other charging error */
1154 default:
1155 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1156 break;
1157 }
1158
1159 return status;
1160 }
1161
hidpp20_batterylevel_get_battery_capacity(struct hidpp_device * hidpp,u8 feature_index,int * status,int * capacity,int * next_capacity,int * level)1162 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1163 u8 feature_index,
1164 int *status,
1165 int *capacity,
1166 int *next_capacity,
1167 int *level)
1168 {
1169 struct hidpp_report response;
1170 int ret;
1171 u8 *params = (u8 *)response.fap.params;
1172
1173 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1174 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1175 NULL, 0, &response);
1176 /* Ignore these intermittent errors */
1177 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1178 return -EIO;
1179 if (ret > 0) {
1180 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1181 __func__, ret);
1182 return -EPROTO;
1183 }
1184 if (ret)
1185 return ret;
1186
1187 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1188 next_capacity,
1189 level);
1190
1191 return 0;
1192 }
1193
hidpp20_batterylevel_get_battery_info(struct hidpp_device * hidpp,u8 feature_index)1194 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1195 u8 feature_index)
1196 {
1197 struct hidpp_report response;
1198 int ret;
1199 u8 *params = (u8 *)response.fap.params;
1200 unsigned int level_count, flags;
1201
1202 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1203 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1204 NULL, 0, &response);
1205 if (ret > 0) {
1206 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1207 __func__, ret);
1208 return -EPROTO;
1209 }
1210 if (ret)
1211 return ret;
1212
1213 level_count = params[0];
1214 flags = params[1];
1215
1216 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1217 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1218 else
1219 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1220
1221 return 0;
1222 }
1223
hidpp20_query_battery_info_1000(struct hidpp_device * hidpp)1224 static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1225 {
1226 u8 feature_type;
1227 int ret;
1228 int status, capacity, next_capacity, level;
1229
1230 if (hidpp->battery.feature_index == 0xff) {
1231 ret = hidpp_root_get_feature(hidpp,
1232 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1233 &hidpp->battery.feature_index,
1234 &feature_type);
1235 if (ret)
1236 return ret;
1237 }
1238
1239 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1240 hidpp->battery.feature_index,
1241 &status, &capacity,
1242 &next_capacity, &level);
1243 if (ret)
1244 return ret;
1245
1246 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1247 hidpp->battery.feature_index);
1248 if (ret)
1249 return ret;
1250
1251 hidpp->battery.status = status;
1252 hidpp->battery.capacity = capacity;
1253 hidpp->battery.level = level;
1254 /* the capacity is only available when discharging or full */
1255 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1256 status == POWER_SUPPLY_STATUS_FULL;
1257
1258 return 0;
1259 }
1260
hidpp20_battery_event_1000(struct hidpp_device * hidpp,u8 * data,int size)1261 static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1262 u8 *data, int size)
1263 {
1264 struct hidpp_report *report = (struct hidpp_report *)data;
1265 int status, capacity, next_capacity, level;
1266 bool changed;
1267
1268 if (report->fap.feature_index != hidpp->battery.feature_index ||
1269 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1270 return 0;
1271
1272 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1273 &capacity,
1274 &next_capacity,
1275 &level);
1276
1277 /* the capacity is only available when discharging or full */
1278 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1279 status == POWER_SUPPLY_STATUS_FULL;
1280
1281 changed = capacity != hidpp->battery.capacity ||
1282 level != hidpp->battery.level ||
1283 status != hidpp->battery.status;
1284
1285 if (changed) {
1286 hidpp->battery.level = level;
1287 hidpp->battery.capacity = capacity;
1288 hidpp->battery.status = status;
1289 if (hidpp->battery.ps)
1290 power_supply_changed(hidpp->battery.ps);
1291 }
1292
1293 return 0;
1294 }
1295
1296 /* -------------------------------------------------------------------------- */
1297 /* 0x1001: Battery voltage */
1298 /* -------------------------------------------------------------------------- */
1299
1300 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1301
1302 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1303
1304 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1305
hidpp20_battery_map_status_voltage(u8 data[3],int * voltage,int * level,int * charge_type)1306 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1307 int *level, int *charge_type)
1308 {
1309 int status;
1310
1311 long flags = (long) data[2];
1312 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1313
1314 if (flags & 0x80)
1315 switch (flags & 0x07) {
1316 case 0:
1317 status = POWER_SUPPLY_STATUS_CHARGING;
1318 break;
1319 case 1:
1320 status = POWER_SUPPLY_STATUS_FULL;
1321 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1322 break;
1323 case 2:
1324 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1325 break;
1326 default:
1327 status = POWER_SUPPLY_STATUS_UNKNOWN;
1328 break;
1329 }
1330 else
1331 status = POWER_SUPPLY_STATUS_DISCHARGING;
1332
1333 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1334 if (test_bit(3, &flags)) {
1335 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1336 }
1337 if (test_bit(4, &flags)) {
1338 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1339 }
1340 if (test_bit(5, &flags)) {
1341 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1342 }
1343
1344 *voltage = get_unaligned_be16(data);
1345
1346 return status;
1347 }
1348
hidpp20_battery_get_battery_voltage(struct hidpp_device * hidpp,u8 feature_index,int * status,int * voltage,int * level,int * charge_type)1349 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1350 u8 feature_index,
1351 int *status, int *voltage,
1352 int *level, int *charge_type)
1353 {
1354 struct hidpp_report response;
1355 int ret;
1356 u8 *params = (u8 *)response.fap.params;
1357
1358 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1359 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1360 NULL, 0, &response);
1361
1362 if (ret > 0) {
1363 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1364 __func__, ret);
1365 return -EPROTO;
1366 }
1367 if (ret)
1368 return ret;
1369
1370 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1371
1372 *status = hidpp20_battery_map_status_voltage(params, voltage,
1373 level, charge_type);
1374
1375 return 0;
1376 }
1377
hidpp20_map_battery_capacity(struct hid_device * hid_dev,int voltage)1378 static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1379 {
1380 /* NB: This voltage curve doesn't necessarily map perfectly to all
1381 * devices that implement the BATTERY_VOLTAGE feature. This is because
1382 * there are a few devices that use different battery technology.
1383 */
1384
1385 static const int voltages[] = {
1386 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1387 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1388 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1389 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1390 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1391 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1392 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1393 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1394 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1395 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1396 };
1397
1398 int i;
1399
1400 BUILD_BUG_ON(ARRAY_SIZE(voltages) != 100);
1401
1402 if (unlikely(voltage < 3500 || voltage >= 5000))
1403 hid_warn_once(hid_dev,
1404 "%s: possibly using the wrong voltage curve\n",
1405 __func__);
1406
1407 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1408 if (voltage >= voltages[i])
1409 return ARRAY_SIZE(voltages) - i;
1410 }
1411
1412 return 0;
1413 }
1414
hidpp20_query_battery_voltage_info(struct hidpp_device * hidpp)1415 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1416 {
1417 u8 feature_type;
1418 int ret;
1419 int status, voltage, level, charge_type;
1420
1421 if (hidpp->battery.voltage_feature_index == 0xff) {
1422 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1423 &hidpp->battery.voltage_feature_index,
1424 &feature_type);
1425 if (ret)
1426 return ret;
1427 }
1428
1429 ret = hidpp20_battery_get_battery_voltage(hidpp,
1430 hidpp->battery.voltage_feature_index,
1431 &status, &voltage, &level, &charge_type);
1432
1433 if (ret)
1434 return ret;
1435
1436 hidpp->battery.status = status;
1437 hidpp->battery.voltage = voltage;
1438 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1439 voltage);
1440 hidpp->battery.level = level;
1441 hidpp->battery.charge_type = charge_type;
1442 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1443
1444 return 0;
1445 }
1446
hidpp20_battery_voltage_event(struct hidpp_device * hidpp,u8 * data,int size)1447 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1448 u8 *data, int size)
1449 {
1450 struct hidpp_report *report = (struct hidpp_report *)data;
1451 int status, voltage, level, charge_type;
1452
1453 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1454 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1455 return 0;
1456
1457 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1458 &level, &charge_type);
1459
1460 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1461
1462 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1463 hidpp->battery.voltage = voltage;
1464 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1465 voltage);
1466 hidpp->battery.status = status;
1467 hidpp->battery.level = level;
1468 hidpp->battery.charge_type = charge_type;
1469 if (hidpp->battery.ps)
1470 power_supply_changed(hidpp->battery.ps);
1471 }
1472 return 0;
1473 }
1474
1475 /* -------------------------------------------------------------------------- */
1476 /* 0x1004: Unified battery */
1477 /* -------------------------------------------------------------------------- */
1478
1479 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1480
1481 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1482 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1483
1484 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1485
1486 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1487 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1488 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1489 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1490
1491 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1492 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1493
hidpp20_unifiedbattery_get_capabilities(struct hidpp_device * hidpp,u8 feature_index)1494 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1495 u8 feature_index)
1496 {
1497 struct hidpp_report response;
1498 int ret;
1499 u8 *params = (u8 *)response.fap.params;
1500
1501 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1502 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1503 /* we have already set the device capabilities, so let's skip */
1504 return 0;
1505 }
1506
1507 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1508 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1509 NULL, 0, &response);
1510 /* Ignore these intermittent errors */
1511 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1512 return -EIO;
1513 if (ret > 0) {
1514 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1515 __func__, ret);
1516 return -EPROTO;
1517 }
1518 if (ret)
1519 return ret;
1520
1521 /*
1522 * If the device supports state of charge (battery percentage) we won't
1523 * export the battery level information. there are 4 possible battery
1524 * levels and they all are optional, this means that the device might
1525 * not support any of them, we are just better off with the battery
1526 * percentage.
1527 */
1528 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1529 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1530 hidpp->battery.supported_levels_1004 = 0;
1531 } else {
1532 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1533 hidpp->battery.supported_levels_1004 = params[0];
1534 }
1535
1536 return 0;
1537 }
1538
hidpp20_unifiedbattery_map_status(struct hidpp_device * hidpp,u8 charging_status,u8 external_power_status)1539 static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1540 u8 charging_status,
1541 u8 external_power_status)
1542 {
1543 int status;
1544
1545 switch (charging_status) {
1546 case 0: /* discharging */
1547 status = POWER_SUPPLY_STATUS_DISCHARGING;
1548 break;
1549 case 1: /* charging */
1550 case 2: /* charging slow */
1551 status = POWER_SUPPLY_STATUS_CHARGING;
1552 break;
1553 case 3: /* complete */
1554 status = POWER_SUPPLY_STATUS_FULL;
1555 break;
1556 case 4: /* error */
1557 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1558 hid_info(hidpp->hid_dev, "%s: charging error",
1559 hidpp->name);
1560 break;
1561 default:
1562 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1563 break;
1564 }
1565
1566 return status;
1567 }
1568
hidpp20_unifiedbattery_map_level(struct hidpp_device * hidpp,u8 battery_level)1569 static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1570 u8 battery_level)
1571 {
1572 /* cler unsupported level bits */
1573 battery_level &= hidpp->battery.supported_levels_1004;
1574
1575 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1576 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1577 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1578 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1579 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1580 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1581 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1582 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1583
1584 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1585 }
1586
hidpp20_unifiedbattery_get_status(struct hidpp_device * hidpp,u8 feature_index,u8 * state_of_charge,int * status,int * level)1587 static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1588 u8 feature_index,
1589 u8 *state_of_charge,
1590 int *status,
1591 int *level)
1592 {
1593 struct hidpp_report response;
1594 int ret;
1595 u8 *params = (u8 *)response.fap.params;
1596
1597 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1598 CMD_UNIFIED_BATTERY_GET_STATUS,
1599 NULL, 0, &response);
1600 /* Ignore these intermittent errors */
1601 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1602 return -EIO;
1603 if (ret > 0) {
1604 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1605 __func__, ret);
1606 return -EPROTO;
1607 }
1608 if (ret)
1609 return ret;
1610
1611 *state_of_charge = params[0];
1612 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1613 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1614
1615 return 0;
1616 }
1617
hidpp20_query_battery_info_1004(struct hidpp_device * hidpp)1618 static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1619 {
1620 u8 feature_type;
1621 int ret;
1622 u8 state_of_charge;
1623 int status, level;
1624
1625 if (hidpp->battery.feature_index == 0xff) {
1626 ret = hidpp_root_get_feature(hidpp,
1627 HIDPP_PAGE_UNIFIED_BATTERY,
1628 &hidpp->battery.feature_index,
1629 &feature_type);
1630 if (ret)
1631 return ret;
1632 }
1633
1634 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1635 hidpp->battery.feature_index);
1636 if (ret)
1637 return ret;
1638
1639 ret = hidpp20_unifiedbattery_get_status(hidpp,
1640 hidpp->battery.feature_index,
1641 &state_of_charge,
1642 &status,
1643 &level);
1644 if (ret)
1645 return ret;
1646
1647 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1648 hidpp->battery.capacity = state_of_charge;
1649 hidpp->battery.status = status;
1650 hidpp->battery.level = level;
1651 hidpp->battery.online = true;
1652
1653 return 0;
1654 }
1655
hidpp20_battery_event_1004(struct hidpp_device * hidpp,u8 * data,int size)1656 static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1657 u8 *data, int size)
1658 {
1659 struct hidpp_report *report = (struct hidpp_report *)data;
1660 u8 *params = (u8 *)report->fap.params;
1661 int state_of_charge, status, level;
1662 bool changed;
1663
1664 if (report->fap.feature_index != hidpp->battery.feature_index ||
1665 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1666 return 0;
1667
1668 state_of_charge = params[0];
1669 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1670 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1671
1672 changed = status != hidpp->battery.status ||
1673 (state_of_charge != hidpp->battery.capacity &&
1674 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1675 (level != hidpp->battery.level &&
1676 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1677
1678 if (changed) {
1679 hidpp->battery.capacity = state_of_charge;
1680 hidpp->battery.status = status;
1681 hidpp->battery.level = level;
1682 if (hidpp->battery.ps)
1683 power_supply_changed(hidpp->battery.ps);
1684 }
1685
1686 return 0;
1687 }
1688
1689 /* -------------------------------------------------------------------------- */
1690 /* Battery feature helpers */
1691 /* -------------------------------------------------------------------------- */
1692
1693 static enum power_supply_property hidpp_battery_props[] = {
1694 POWER_SUPPLY_PROP_ONLINE,
1695 POWER_SUPPLY_PROP_STATUS,
1696 POWER_SUPPLY_PROP_SCOPE,
1697 POWER_SUPPLY_PROP_MODEL_NAME,
1698 POWER_SUPPLY_PROP_MANUFACTURER,
1699 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1700 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1701 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1702 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1703 };
1704
hidpp_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)1705 static int hidpp_battery_get_property(struct power_supply *psy,
1706 enum power_supply_property psp,
1707 union power_supply_propval *val)
1708 {
1709 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1710 int ret = 0;
1711
1712 switch(psp) {
1713 case POWER_SUPPLY_PROP_STATUS:
1714 val->intval = hidpp->battery.status;
1715 break;
1716 case POWER_SUPPLY_PROP_CAPACITY:
1717 val->intval = hidpp->battery.capacity;
1718 break;
1719 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1720 val->intval = hidpp->battery.level;
1721 break;
1722 case POWER_SUPPLY_PROP_SCOPE:
1723 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1724 break;
1725 case POWER_SUPPLY_PROP_ONLINE:
1726 val->intval = hidpp->battery.online;
1727 break;
1728 case POWER_SUPPLY_PROP_MODEL_NAME:
1729 if (!strncmp(hidpp->name, "Logitech ", 9))
1730 val->strval = hidpp->name + 9;
1731 else
1732 val->strval = hidpp->name;
1733 break;
1734 case POWER_SUPPLY_PROP_MANUFACTURER:
1735 val->strval = "Logitech";
1736 break;
1737 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1738 val->strval = hidpp->hid_dev->uniq;
1739 break;
1740 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1741 /* hardware reports voltage in mV. sysfs expects uV */
1742 val->intval = hidpp->battery.voltage * 1000;
1743 break;
1744 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1745 val->intval = hidpp->battery.charge_type;
1746 break;
1747 default:
1748 ret = -EINVAL;
1749 break;
1750 }
1751
1752 return ret;
1753 }
1754
1755 /* -------------------------------------------------------------------------- */
1756 /* 0x1d4b: Wireless device status */
1757 /* -------------------------------------------------------------------------- */
1758 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1759
hidpp_get_wireless_feature_index(struct hidpp_device * hidpp,u8 * feature_index)1760 static int hidpp_get_wireless_feature_index(struct hidpp_device *hidpp, u8 *feature_index)
1761 {
1762 u8 feature_type;
1763 int ret;
1764
1765 ret = hidpp_root_get_feature(hidpp,
1766 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1767 feature_index, &feature_type);
1768
1769 return ret;
1770 }
1771
1772 /* -------------------------------------------------------------------------- */
1773 /* 0x2120: Hi-resolution scrolling */
1774 /* -------------------------------------------------------------------------- */
1775
1776 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1777
1778 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1779
hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device * hidpp,bool enabled,u8 * multiplier)1780 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1781 bool enabled, u8 *multiplier)
1782 {
1783 u8 feature_index;
1784 u8 feature_type;
1785 int ret;
1786 u8 params[1];
1787 struct hidpp_report response;
1788
1789 ret = hidpp_root_get_feature(hidpp,
1790 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1791 &feature_index,
1792 &feature_type);
1793 if (ret)
1794 return ret;
1795
1796 params[0] = enabled ? BIT(0) : 0;
1797 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1798 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1799 params, sizeof(params), &response);
1800 if (ret)
1801 return ret;
1802 *multiplier = response.fap.params[1];
1803 return 0;
1804 }
1805
1806 /* -------------------------------------------------------------------------- */
1807 /* 0x2121: HiRes Wheel */
1808 /* -------------------------------------------------------------------------- */
1809
1810 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1811
1812 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1813 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1814
hidpp_hrw_get_wheel_capability(struct hidpp_device * hidpp,u8 * multiplier)1815 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1816 u8 *multiplier)
1817 {
1818 u8 feature_index;
1819 u8 feature_type;
1820 int ret;
1821 struct hidpp_report response;
1822
1823 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1824 &feature_index, &feature_type);
1825 if (ret)
1826 goto return_default;
1827
1828 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1829 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1830 NULL, 0, &response);
1831 if (ret)
1832 goto return_default;
1833
1834 *multiplier = response.fap.params[0];
1835 return 0;
1836 return_default:
1837 hid_warn(hidpp->hid_dev,
1838 "Couldn't get wheel multiplier (error %d)\n", ret);
1839 return ret;
1840 }
1841
hidpp_hrw_set_wheel_mode(struct hidpp_device * hidpp,bool invert,bool high_resolution,bool use_hidpp)1842 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1843 bool high_resolution, bool use_hidpp)
1844 {
1845 u8 feature_index;
1846 u8 feature_type;
1847 int ret;
1848 u8 params[1];
1849 struct hidpp_report response;
1850
1851 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1852 &feature_index, &feature_type);
1853 if (ret)
1854 return ret;
1855
1856 params[0] = (invert ? BIT(2) : 0) |
1857 (high_resolution ? BIT(1) : 0) |
1858 (use_hidpp ? BIT(0) : 0);
1859
1860 return hidpp_send_fap_command_sync(hidpp, feature_index,
1861 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1862 params, sizeof(params), &response);
1863 }
1864
1865 /* -------------------------------------------------------------------------- */
1866 /* 0x4301: Solar Keyboard */
1867 /* -------------------------------------------------------------------------- */
1868
1869 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1870
1871 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1872
1873 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1874 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1875 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1876
hidpp_solar_request_battery_event(struct hidpp_device * hidpp)1877 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1878 {
1879 struct hidpp_report response;
1880 u8 params[2] = { 1, 1 };
1881 u8 feature_type;
1882 int ret;
1883
1884 if (hidpp->battery.feature_index == 0xff) {
1885 ret = hidpp_root_get_feature(hidpp,
1886 HIDPP_PAGE_SOLAR_KEYBOARD,
1887 &hidpp->battery.solar_feature_index,
1888 &feature_type);
1889 if (ret)
1890 return ret;
1891 }
1892
1893 ret = hidpp_send_fap_command_sync(hidpp,
1894 hidpp->battery.solar_feature_index,
1895 CMD_SOLAR_SET_LIGHT_MEASURE,
1896 params, 2, &response);
1897 if (ret > 0) {
1898 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1899 __func__, ret);
1900 return -EPROTO;
1901 }
1902 if (ret)
1903 return ret;
1904
1905 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1906
1907 return 0;
1908 }
1909
hidpp_solar_battery_event(struct hidpp_device * hidpp,u8 * data,int size)1910 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1911 u8 *data, int size)
1912 {
1913 struct hidpp_report *report = (struct hidpp_report *)data;
1914 int capacity, lux, status;
1915 u8 function;
1916
1917 function = report->fap.funcindex_clientid;
1918
1919
1920 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1921 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1922 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1923 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1924 return 0;
1925
1926 capacity = report->fap.params[0];
1927
1928 switch (function) {
1929 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1930 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1931 if (lux > 200)
1932 status = POWER_SUPPLY_STATUS_CHARGING;
1933 else
1934 status = POWER_SUPPLY_STATUS_DISCHARGING;
1935 break;
1936 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1937 default:
1938 if (capacity < hidpp->battery.capacity)
1939 status = POWER_SUPPLY_STATUS_DISCHARGING;
1940 else
1941 status = POWER_SUPPLY_STATUS_CHARGING;
1942
1943 }
1944
1945 if (capacity == 100)
1946 status = POWER_SUPPLY_STATUS_FULL;
1947
1948 hidpp->battery.online = true;
1949 if (capacity != hidpp->battery.capacity ||
1950 status != hidpp->battery.status) {
1951 hidpp->battery.capacity = capacity;
1952 hidpp->battery.status = status;
1953 if (hidpp->battery.ps)
1954 power_supply_changed(hidpp->battery.ps);
1955 }
1956
1957 return 0;
1958 }
1959
1960 /* -------------------------------------------------------------------------- */
1961 /* 0x6010: Touchpad FW items */
1962 /* -------------------------------------------------------------------------- */
1963
1964 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1965
1966 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1967
1968 struct hidpp_touchpad_fw_items {
1969 uint8_t presence;
1970 uint8_t desired_state;
1971 uint8_t state;
1972 uint8_t persistent;
1973 };
1974
1975 /*
1976 * send a set state command to the device by reading the current items->state
1977 * field. items is then filled with the current state.
1978 */
hidpp_touchpad_fw_items_set(struct hidpp_device * hidpp,u8 feature_index,struct hidpp_touchpad_fw_items * items)1979 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1980 u8 feature_index,
1981 struct hidpp_touchpad_fw_items *items)
1982 {
1983 struct hidpp_report response;
1984 int ret;
1985 u8 *params = (u8 *)response.fap.params;
1986
1987 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1988 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1989
1990 if (ret > 0) {
1991 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1992 __func__, ret);
1993 return -EPROTO;
1994 }
1995 if (ret)
1996 return ret;
1997
1998 items->presence = params[0];
1999 items->desired_state = params[1];
2000 items->state = params[2];
2001 items->persistent = params[3];
2002
2003 return 0;
2004 }
2005
2006 /* -------------------------------------------------------------------------- */
2007 /* 0x6100: TouchPadRawXY */
2008 /* -------------------------------------------------------------------------- */
2009
2010 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
2011
2012 #define CMD_TOUCHPAD_GET_RAW_INFO 0x00
2013 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x20
2014
2015 #define EVENT_TOUCHPAD_RAW_XY 0x00
2016
2017 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
2018 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
2019
2020 struct hidpp_touchpad_raw_info {
2021 u16 x_size;
2022 u16 y_size;
2023 u8 z_range;
2024 u8 area_range;
2025 u8 timestamp_unit;
2026 u8 maxcontacts;
2027 u8 origin;
2028 u16 res;
2029 };
2030
2031 struct hidpp_touchpad_raw_xy_finger {
2032 u8 contact_type;
2033 u8 contact_status;
2034 u16 x;
2035 u16 y;
2036 u8 z;
2037 u8 area;
2038 u8 finger_id;
2039 };
2040
2041 struct hidpp_touchpad_raw_xy {
2042 u16 timestamp;
2043 struct hidpp_touchpad_raw_xy_finger fingers[2];
2044 u8 spurious_flag;
2045 u8 end_of_frame;
2046 u8 finger_count;
2047 u8 button;
2048 };
2049
hidpp_touchpad_get_raw_info(struct hidpp_device * hidpp,u8 feature_index,struct hidpp_touchpad_raw_info * raw_info)2050 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2051 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2052 {
2053 struct hidpp_report response;
2054 int ret;
2055 u8 *params = (u8 *)response.fap.params;
2056
2057 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2058 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
2059
2060 if (ret > 0) {
2061 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2062 __func__, ret);
2063 return -EPROTO;
2064 }
2065 if (ret)
2066 return ret;
2067
2068 raw_info->x_size = get_unaligned_be16(¶ms[0]);
2069 raw_info->y_size = get_unaligned_be16(¶ms[2]);
2070 raw_info->z_range = params[4];
2071 raw_info->area_range = params[5];
2072 raw_info->maxcontacts = params[7];
2073 raw_info->origin = params[8];
2074 /* res is given in unit per inch */
2075 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
2076
2077 return ret;
2078 }
2079
hidpp_touchpad_set_raw_report_state(struct hidpp_device * hidpp_dev,u8 feature_index,bool send_raw_reports,bool sensor_enhanced_settings)2080 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2081 u8 feature_index, bool send_raw_reports,
2082 bool sensor_enhanced_settings)
2083 {
2084 struct hidpp_report response;
2085
2086 /*
2087 * Params:
2088 * bit 0 - enable raw
2089 * bit 1 - 16bit Z, no area
2090 * bit 2 - enhanced sensitivity
2091 * bit 3 - width, height (4 bits each) instead of area
2092 * bit 4 - send raw + gestures (degrades smoothness)
2093 * remaining bits - reserved
2094 */
2095 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2096
2097 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2098 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
2099 }
2100
hidpp_touchpad_touch_event(u8 * data,struct hidpp_touchpad_raw_xy_finger * finger)2101 static void hidpp_touchpad_touch_event(u8 *data,
2102 struct hidpp_touchpad_raw_xy_finger *finger)
2103 {
2104 u8 x_m = data[0] << 2;
2105 u8 y_m = data[2] << 2;
2106
2107 finger->x = x_m << 6 | data[1];
2108 finger->y = y_m << 6 | data[3];
2109
2110 finger->contact_type = data[0] >> 6;
2111 finger->contact_status = data[2] >> 6;
2112
2113 finger->z = data[4];
2114 finger->area = data[5];
2115 finger->finger_id = data[6] >> 4;
2116 }
2117
hidpp_touchpad_raw_xy_event(struct hidpp_device * hidpp_dev,u8 * data,struct hidpp_touchpad_raw_xy * raw_xy)2118 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2119 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2120 {
2121 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2122 raw_xy->end_of_frame = data[8] & 0x01;
2123 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2124 raw_xy->finger_count = data[15] & 0x0f;
2125 raw_xy->button = (data[8] >> 2) & 0x01;
2126
2127 if (raw_xy->finger_count) {
2128 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2129 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2130 }
2131 }
2132
2133 /* -------------------------------------------------------------------------- */
2134 /* 0x8123: Force feedback support */
2135 /* -------------------------------------------------------------------------- */
2136
2137 #define HIDPP_FF_GET_INFO 0x01
2138 #define HIDPP_FF_RESET_ALL 0x11
2139 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2140 #define HIDPP_FF_SET_EFFECT_STATE 0x31
2141 #define HIDPP_FF_DESTROY_EFFECT 0x41
2142 #define HIDPP_FF_GET_APERTURE 0x51
2143 #define HIDPP_FF_SET_APERTURE 0x61
2144 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2145 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2146
2147 #define HIDPP_FF_EFFECT_STATE_GET 0x00
2148 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
2149 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2150 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2151
2152 #define HIDPP_FF_EFFECT_CONSTANT 0x00
2153 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2154 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2155 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2156 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2157 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2158 #define HIDPP_FF_EFFECT_SPRING 0x06
2159 #define HIDPP_FF_EFFECT_DAMPER 0x07
2160 #define HIDPP_FF_EFFECT_FRICTION 0x08
2161 #define HIDPP_FF_EFFECT_INERTIA 0x09
2162 #define HIDPP_FF_EFFECT_RAMP 0x0A
2163
2164 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
2165
2166 #define HIDPP_FF_EFFECTID_NONE -1
2167 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
2168 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2169
2170 #define HIDPP_FF_MAX_PARAMS 20
2171 #define HIDPP_FF_RESERVED_SLOTS 1
2172
2173 struct hidpp_ff_private_data {
2174 struct hidpp_device *hidpp;
2175 u8 feature_index;
2176 u8 version;
2177 u16 gain;
2178 s16 range;
2179 u8 slot_autocenter;
2180 u8 num_effects;
2181 int *effect_ids;
2182 struct workqueue_struct *wq;
2183 atomic_t workqueue_size;
2184 };
2185
2186 struct hidpp_ff_work_data {
2187 struct work_struct work;
2188 struct hidpp_ff_private_data *data;
2189 int effect_id;
2190 u8 command;
2191 u8 params[HIDPP_FF_MAX_PARAMS];
2192 u8 size;
2193 };
2194
2195 static const signed short hidpp_ff_effects[] = {
2196 FF_CONSTANT,
2197 FF_PERIODIC,
2198 FF_SINE,
2199 FF_SQUARE,
2200 FF_SAW_UP,
2201 FF_SAW_DOWN,
2202 FF_TRIANGLE,
2203 FF_SPRING,
2204 FF_DAMPER,
2205 FF_AUTOCENTER,
2206 FF_GAIN,
2207 -1
2208 };
2209
2210 static const signed short hidpp_ff_effects_v2[] = {
2211 FF_RAMP,
2212 FF_FRICTION,
2213 FF_INERTIA,
2214 -1
2215 };
2216
2217 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2218 HIDPP_FF_EFFECT_SPRING,
2219 HIDPP_FF_EFFECT_FRICTION,
2220 HIDPP_FF_EFFECT_DAMPER,
2221 HIDPP_FF_EFFECT_INERTIA
2222 };
2223
2224 static const char *HIDPP_FF_CONDITION_NAMES[] = {
2225 "spring",
2226 "friction",
2227 "damper",
2228 "inertia"
2229 };
2230
2231
hidpp_ff_find_effect(struct hidpp_ff_private_data * data,int effect_id)2232 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2233 {
2234 int i;
2235
2236 for (i = 0; i < data->num_effects; i++)
2237 if (data->effect_ids[i] == effect_id)
2238 return i+1;
2239
2240 return 0;
2241 }
2242
hidpp_ff_work_handler(struct work_struct * w)2243 static void hidpp_ff_work_handler(struct work_struct *w)
2244 {
2245 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2246 struct hidpp_ff_private_data *data = wd->data;
2247 struct hidpp_report response;
2248 u8 slot;
2249 int ret;
2250
2251 /* add slot number if needed */
2252 switch (wd->effect_id) {
2253 case HIDPP_FF_EFFECTID_AUTOCENTER:
2254 wd->params[0] = data->slot_autocenter;
2255 break;
2256 case HIDPP_FF_EFFECTID_NONE:
2257 /* leave slot as zero */
2258 break;
2259 default:
2260 /* find current slot for effect */
2261 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2262 break;
2263 }
2264
2265 /* send command and wait for reply */
2266 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2267 wd->command, wd->params, wd->size, &response);
2268
2269 if (ret) {
2270 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2271 goto out;
2272 }
2273
2274 /* parse return data */
2275 switch (wd->command) {
2276 case HIDPP_FF_DOWNLOAD_EFFECT:
2277 slot = response.fap.params[0];
2278 if (slot > 0 && slot <= data->num_effects) {
2279 if (wd->effect_id >= 0)
2280 /* regular effect uploaded */
2281 data->effect_ids[slot-1] = wd->effect_id;
2282 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2283 /* autocenter spring uploaded */
2284 data->slot_autocenter = slot;
2285 }
2286 break;
2287 case HIDPP_FF_DESTROY_EFFECT:
2288 if (wd->effect_id >= 0)
2289 /* regular effect destroyed */
2290 data->effect_ids[wd->params[0]-1] = -1;
2291 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2292 /* autocenter spring destoyed */
2293 data->slot_autocenter = 0;
2294 break;
2295 case HIDPP_FF_SET_GLOBAL_GAINS:
2296 data->gain = (wd->params[0] << 8) + wd->params[1];
2297 break;
2298 case HIDPP_FF_SET_APERTURE:
2299 data->range = (wd->params[0] << 8) + wd->params[1];
2300 break;
2301 default:
2302 /* no action needed */
2303 break;
2304 }
2305
2306 out:
2307 atomic_dec(&data->workqueue_size);
2308 kfree(wd);
2309 }
2310
hidpp_ff_queue_work(struct hidpp_ff_private_data * data,int effect_id,u8 command,u8 * params,u8 size)2311 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2312 {
2313 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2314 int s;
2315
2316 if (!wd)
2317 return -ENOMEM;
2318
2319 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2320
2321 wd->data = data;
2322 wd->effect_id = effect_id;
2323 wd->command = command;
2324 wd->size = size;
2325 memcpy(wd->params, params, size);
2326
2327 s = atomic_inc_return(&data->workqueue_size);
2328 queue_work(data->wq, &wd->work);
2329
2330 /* warn about excessive queue size */
2331 if (s >= 20 && s % 20 == 0)
2332 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2333
2334 return 0;
2335 }
2336
hidpp_ff_upload_effect(struct input_dev * dev,struct ff_effect * effect,struct ff_effect * old)2337 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2338 {
2339 struct hidpp_ff_private_data *data = dev->ff->private;
2340 u8 params[20];
2341 u8 size;
2342 int force;
2343
2344 /* set common parameters */
2345 params[2] = effect->replay.length >> 8;
2346 params[3] = effect->replay.length & 255;
2347 params[4] = effect->replay.delay >> 8;
2348 params[5] = effect->replay.delay & 255;
2349
2350 switch (effect->type) {
2351 case FF_CONSTANT:
2352 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2353 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2354 params[6] = force >> 8;
2355 params[7] = force & 255;
2356 params[8] = effect->u.constant.envelope.attack_level >> 7;
2357 params[9] = effect->u.constant.envelope.attack_length >> 8;
2358 params[10] = effect->u.constant.envelope.attack_length & 255;
2359 params[11] = effect->u.constant.envelope.fade_level >> 7;
2360 params[12] = effect->u.constant.envelope.fade_length >> 8;
2361 params[13] = effect->u.constant.envelope.fade_length & 255;
2362 size = 14;
2363 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2364 effect->u.constant.level,
2365 effect->direction, force);
2366 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2367 effect->u.constant.envelope.attack_level,
2368 effect->u.constant.envelope.attack_length,
2369 effect->u.constant.envelope.fade_level,
2370 effect->u.constant.envelope.fade_length);
2371 break;
2372 case FF_PERIODIC:
2373 {
2374 switch (effect->u.periodic.waveform) {
2375 case FF_SINE:
2376 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2377 break;
2378 case FF_SQUARE:
2379 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2380 break;
2381 case FF_SAW_UP:
2382 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2383 break;
2384 case FF_SAW_DOWN:
2385 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2386 break;
2387 case FF_TRIANGLE:
2388 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2389 break;
2390 default:
2391 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2392 return -EINVAL;
2393 }
2394 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2395 params[6] = effect->u.periodic.magnitude >> 8;
2396 params[7] = effect->u.periodic.magnitude & 255;
2397 params[8] = effect->u.periodic.offset >> 8;
2398 params[9] = effect->u.periodic.offset & 255;
2399 params[10] = effect->u.periodic.period >> 8;
2400 params[11] = effect->u.periodic.period & 255;
2401 params[12] = effect->u.periodic.phase >> 8;
2402 params[13] = effect->u.periodic.phase & 255;
2403 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2404 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2405 params[16] = effect->u.periodic.envelope.attack_length & 255;
2406 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2407 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2408 params[19] = effect->u.periodic.envelope.fade_length & 255;
2409 size = 20;
2410 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2411 effect->u.periodic.magnitude, effect->direction,
2412 effect->u.periodic.offset,
2413 effect->u.periodic.period,
2414 effect->u.periodic.phase);
2415 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2416 effect->u.periodic.envelope.attack_level,
2417 effect->u.periodic.envelope.attack_length,
2418 effect->u.periodic.envelope.fade_level,
2419 effect->u.periodic.envelope.fade_length);
2420 break;
2421 }
2422 case FF_RAMP:
2423 params[1] = HIDPP_FF_EFFECT_RAMP;
2424 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2425 params[6] = force >> 8;
2426 params[7] = force & 255;
2427 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2428 params[8] = force >> 8;
2429 params[9] = force & 255;
2430 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2431 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2432 params[12] = effect->u.ramp.envelope.attack_length & 255;
2433 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2434 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2435 params[15] = effect->u.ramp.envelope.fade_length & 255;
2436 size = 16;
2437 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2438 effect->u.ramp.start_level,
2439 effect->u.ramp.end_level,
2440 effect->direction, force);
2441 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2442 effect->u.ramp.envelope.attack_level,
2443 effect->u.ramp.envelope.attack_length,
2444 effect->u.ramp.envelope.fade_level,
2445 effect->u.ramp.envelope.fade_length);
2446 break;
2447 case FF_FRICTION:
2448 case FF_INERTIA:
2449 case FF_SPRING:
2450 case FF_DAMPER:
2451 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2452 params[6] = effect->u.condition[0].left_saturation >> 9;
2453 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2454 params[8] = effect->u.condition[0].left_coeff >> 8;
2455 params[9] = effect->u.condition[0].left_coeff & 255;
2456 params[10] = effect->u.condition[0].deadband >> 9;
2457 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2458 params[12] = effect->u.condition[0].center >> 8;
2459 params[13] = effect->u.condition[0].center & 255;
2460 params[14] = effect->u.condition[0].right_coeff >> 8;
2461 params[15] = effect->u.condition[0].right_coeff & 255;
2462 params[16] = effect->u.condition[0].right_saturation >> 9;
2463 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2464 size = 18;
2465 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2466 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2467 effect->u.condition[0].left_coeff,
2468 effect->u.condition[0].left_saturation,
2469 effect->u.condition[0].right_coeff,
2470 effect->u.condition[0].right_saturation);
2471 dbg_hid(" deadband=%d, center=%d\n",
2472 effect->u.condition[0].deadband,
2473 effect->u.condition[0].center);
2474 break;
2475 default:
2476 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2477 return -EINVAL;
2478 }
2479
2480 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2481 }
2482
hidpp_ff_playback(struct input_dev * dev,int effect_id,int value)2483 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2484 {
2485 struct hidpp_ff_private_data *data = dev->ff->private;
2486 u8 params[2];
2487
2488 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2489
2490 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2491
2492 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2493 }
2494
hidpp_ff_erase_effect(struct input_dev * dev,int effect_id)2495 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2496 {
2497 struct hidpp_ff_private_data *data = dev->ff->private;
2498 u8 slot = 0;
2499
2500 dbg_hid("Erasing effect %d.\n", effect_id);
2501
2502 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2503 }
2504
hidpp_ff_set_autocenter(struct input_dev * dev,u16 magnitude)2505 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2506 {
2507 struct hidpp_ff_private_data *data = dev->ff->private;
2508 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2509
2510 dbg_hid("Setting autocenter to %d.\n", magnitude);
2511
2512 /* start a standard spring effect */
2513 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2514 /* zero delay and duration */
2515 params[2] = params[3] = params[4] = params[5] = 0;
2516 /* set coeff to 25% of saturation */
2517 params[8] = params[14] = magnitude >> 11;
2518 params[9] = params[15] = (magnitude >> 3) & 255;
2519 params[6] = params[16] = magnitude >> 9;
2520 params[7] = params[17] = (magnitude >> 1) & 255;
2521 /* zero deadband and center */
2522 params[10] = params[11] = params[12] = params[13] = 0;
2523
2524 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2525 }
2526
hidpp_ff_set_gain(struct input_dev * dev,u16 gain)2527 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2528 {
2529 struct hidpp_ff_private_data *data = dev->ff->private;
2530 u8 params[4];
2531
2532 dbg_hid("Setting gain to %d.\n", gain);
2533
2534 params[0] = gain >> 8;
2535 params[1] = gain & 255;
2536 params[2] = 0; /* no boost */
2537 params[3] = 0;
2538
2539 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2540 }
2541
hidpp_ff_range_show(struct device * dev,struct device_attribute * attr,char * buf)2542 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2543 {
2544 struct hid_device *hid = to_hid_device(dev);
2545 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2546 struct input_dev *idev = hidinput->input;
2547 struct hidpp_ff_private_data *data = idev->ff->private;
2548
2549 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2550 }
2551
hidpp_ff_range_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2552 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2553 {
2554 struct hid_device *hid = to_hid_device(dev);
2555 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2556 struct input_dev *idev = hidinput->input;
2557 struct hidpp_ff_private_data *data = idev->ff->private;
2558 u8 params[2];
2559 int range = simple_strtoul(buf, NULL, 10);
2560
2561 range = clamp(range, 180, 900);
2562
2563 params[0] = range >> 8;
2564 params[1] = range & 0x00FF;
2565
2566 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2567
2568 return count;
2569 }
2570
2571 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2572
hidpp_ff_destroy(struct ff_device * ff)2573 static void hidpp_ff_destroy(struct ff_device *ff)
2574 {
2575 struct hidpp_ff_private_data *data = ff->private;
2576 struct hid_device *hid = data->hidpp->hid_dev;
2577
2578 hid_info(hid, "Unloading HID++ force feedback.\n");
2579
2580 device_remove_file(&hid->dev, &dev_attr_range);
2581 destroy_workqueue(data->wq);
2582 kfree(data->effect_ids);
2583 }
2584
hidpp_ff_init(struct hidpp_device * hidpp,struct hidpp_ff_private_data * data)2585 static int hidpp_ff_init(struct hidpp_device *hidpp,
2586 struct hidpp_ff_private_data *data)
2587 {
2588 struct hid_device *hid = hidpp->hid_dev;
2589 struct hid_input *hidinput;
2590 struct input_dev *dev;
2591 struct usb_device_descriptor *udesc;
2592 u16 bcdDevice;
2593 struct ff_device *ff;
2594 int error, j, num_slots = data->num_effects;
2595 u8 version;
2596
2597 if (!hid_is_usb(hid)) {
2598 hid_err(hid, "device is not USB\n");
2599 return -ENODEV;
2600 }
2601
2602 if (list_empty(&hid->inputs)) {
2603 hid_err(hid, "no inputs found\n");
2604 return -ENODEV;
2605 }
2606 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2607 dev = hidinput->input;
2608
2609 if (!dev) {
2610 hid_err(hid, "Struct input_dev not set!\n");
2611 return -EINVAL;
2612 }
2613
2614 /* Get firmware release */
2615 udesc = &(hid_to_usb_dev(hid)->descriptor);
2616 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2617 version = bcdDevice & 255;
2618
2619 /* Set supported force feedback capabilities */
2620 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2621 set_bit(hidpp_ff_effects[j], dev->ffbit);
2622 if (version > 1)
2623 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2624 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2625
2626 error = input_ff_create(dev, num_slots);
2627
2628 if (error) {
2629 hid_err(dev, "Failed to create FF device!\n");
2630 return error;
2631 }
2632 /*
2633 * Create a copy of passed data, so we can transfer memory
2634 * ownership to FF core
2635 */
2636 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2637 if (!data)
2638 return -ENOMEM;
2639 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2640 if (!data->effect_ids) {
2641 kfree(data);
2642 return -ENOMEM;
2643 }
2644 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2645 if (!data->wq) {
2646 kfree(data->effect_ids);
2647 kfree(data);
2648 return -ENOMEM;
2649 }
2650
2651 data->hidpp = hidpp;
2652 data->version = version;
2653 for (j = 0; j < num_slots; j++)
2654 data->effect_ids[j] = -1;
2655
2656 ff = dev->ff;
2657 ff->private = data;
2658
2659 ff->upload = hidpp_ff_upload_effect;
2660 ff->erase = hidpp_ff_erase_effect;
2661 ff->playback = hidpp_ff_playback;
2662 ff->set_gain = hidpp_ff_set_gain;
2663 ff->set_autocenter = hidpp_ff_set_autocenter;
2664 ff->destroy = hidpp_ff_destroy;
2665
2666 /* Create sysfs interface */
2667 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2668 if (error)
2669 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2670
2671 /* init the hardware command queue */
2672 atomic_set(&data->workqueue_size, 0);
2673
2674 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2675 version);
2676
2677 return 0;
2678 }
2679
2680 /* ************************************************************************** */
2681 /* */
2682 /* Device Support */
2683 /* */
2684 /* ************************************************************************** */
2685
2686 /* -------------------------------------------------------------------------- */
2687 /* Touchpad HID++ devices */
2688 /* -------------------------------------------------------------------------- */
2689
2690 #define WTP_MANUAL_RESOLUTION 39
2691
2692 struct wtp_data {
2693 u16 x_size, y_size;
2694 u8 finger_count;
2695 u8 mt_feature_index;
2696 u8 button_feature_index;
2697 u8 maxcontacts;
2698 bool flip_y;
2699 unsigned int resolution;
2700 };
2701
wtp_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)2702 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2703 struct hid_field *field, struct hid_usage *usage,
2704 unsigned long **bit, int *max)
2705 {
2706 return -1;
2707 }
2708
wtp_populate_input(struct hidpp_device * hidpp,struct input_dev * input_dev)2709 static void wtp_populate_input(struct hidpp_device *hidpp,
2710 struct input_dev *input_dev)
2711 {
2712 struct wtp_data *wd = hidpp->private_data;
2713
2714 __set_bit(EV_ABS, input_dev->evbit);
2715 __set_bit(EV_KEY, input_dev->evbit);
2716 __clear_bit(EV_REL, input_dev->evbit);
2717 __clear_bit(EV_LED, input_dev->evbit);
2718
2719 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2720 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2721 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2722 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2723
2724 /* Max pressure is not given by the devices, pick one */
2725 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2726
2727 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2728
2729 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2730 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2731 else
2732 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2733
2734 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2735 INPUT_MT_DROP_UNUSED);
2736 }
2737
wtp_touch_event(struct hidpp_device * hidpp,struct hidpp_touchpad_raw_xy_finger * touch_report)2738 static void wtp_touch_event(struct hidpp_device *hidpp,
2739 struct hidpp_touchpad_raw_xy_finger *touch_report)
2740 {
2741 struct wtp_data *wd = hidpp->private_data;
2742 int slot;
2743
2744 if (!touch_report->finger_id || touch_report->contact_type)
2745 /* no actual data */
2746 return;
2747
2748 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2749
2750 input_mt_slot(hidpp->input, slot);
2751 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2752 touch_report->contact_status);
2753 if (touch_report->contact_status) {
2754 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2755 touch_report->x);
2756 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2757 wd->flip_y ? wd->y_size - touch_report->y :
2758 touch_report->y);
2759 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2760 touch_report->area);
2761 }
2762 }
2763
wtp_send_raw_xy_event(struct hidpp_device * hidpp,struct hidpp_touchpad_raw_xy * raw)2764 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2765 struct hidpp_touchpad_raw_xy *raw)
2766 {
2767 int i;
2768
2769 for (i = 0; i < 2; i++)
2770 wtp_touch_event(hidpp, &(raw->fingers[i]));
2771
2772 if (raw->end_of_frame &&
2773 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2774 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2775
2776 if (raw->end_of_frame || raw->finger_count <= 2) {
2777 input_mt_sync_frame(hidpp->input);
2778 input_sync(hidpp->input);
2779 }
2780 }
2781
wtp_mouse_raw_xy_event(struct hidpp_device * hidpp,u8 * data)2782 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2783 {
2784 struct wtp_data *wd = hidpp->private_data;
2785 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2786 (data[7] >> 4) * (data[7] >> 4)) / 2;
2787 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2788 (data[13] >> 4) * (data[13] >> 4)) / 2;
2789 struct hidpp_touchpad_raw_xy raw = {
2790 .timestamp = data[1],
2791 .fingers = {
2792 {
2793 .contact_type = 0,
2794 .contact_status = !!data[7],
2795 .x = get_unaligned_le16(&data[3]),
2796 .y = get_unaligned_le16(&data[5]),
2797 .z = c1_area,
2798 .area = c1_area,
2799 .finger_id = data[2],
2800 }, {
2801 .contact_type = 0,
2802 .contact_status = !!data[13],
2803 .x = get_unaligned_le16(&data[9]),
2804 .y = get_unaligned_le16(&data[11]),
2805 .z = c2_area,
2806 .area = c2_area,
2807 .finger_id = data[8],
2808 }
2809 },
2810 .finger_count = wd->maxcontacts,
2811 .spurious_flag = 0,
2812 .end_of_frame = (data[0] >> 7) == 0,
2813 .button = data[0] & 0x01,
2814 };
2815
2816 wtp_send_raw_xy_event(hidpp, &raw);
2817
2818 return 1;
2819 }
2820
wtp_raw_event(struct hid_device * hdev,u8 * data,int size)2821 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2822 {
2823 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2824 struct wtp_data *wd = hidpp->private_data;
2825 struct hidpp_report *report = (struct hidpp_report *)data;
2826 struct hidpp_touchpad_raw_xy raw;
2827
2828 if (!wd || !hidpp->input)
2829 return 1;
2830
2831 switch (data[0]) {
2832 case 0x02:
2833 if (size < 2) {
2834 hid_err(hdev, "Received HID report of bad size (%d)",
2835 size);
2836 return 1;
2837 }
2838 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2839 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2840 !!(data[1] & 0x01));
2841 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2842 !!(data[1] & 0x02));
2843 input_sync(hidpp->input);
2844 return 0;
2845 } else {
2846 if (size < 21)
2847 return 1;
2848 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2849 }
2850 case REPORT_ID_HIDPP_LONG:
2851 /* size is already checked in hidpp_raw_event. */
2852 if ((report->fap.feature_index != wd->mt_feature_index) ||
2853 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2854 return 1;
2855 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2856
2857 wtp_send_raw_xy_event(hidpp, &raw);
2858 return 0;
2859 }
2860
2861 return 0;
2862 }
2863
wtp_get_config(struct hidpp_device * hidpp)2864 static int wtp_get_config(struct hidpp_device *hidpp)
2865 {
2866 struct wtp_data *wd = hidpp->private_data;
2867 struct hidpp_touchpad_raw_info raw_info = {0};
2868 u8 feature_type;
2869 int ret;
2870
2871 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2872 &wd->mt_feature_index, &feature_type);
2873 if (ret)
2874 /* means that the device is not powered up */
2875 return ret;
2876
2877 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2878 &raw_info);
2879 if (ret)
2880 return ret;
2881
2882 wd->x_size = raw_info.x_size;
2883 wd->y_size = raw_info.y_size;
2884 wd->maxcontacts = raw_info.maxcontacts;
2885 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2886 wd->resolution = raw_info.res;
2887 if (!wd->resolution)
2888 wd->resolution = WTP_MANUAL_RESOLUTION;
2889
2890 return 0;
2891 }
2892
wtp_allocate(struct hid_device * hdev,const struct hid_device_id * id)2893 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2894 {
2895 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2896 struct wtp_data *wd;
2897
2898 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2899 GFP_KERNEL);
2900 if (!wd)
2901 return -ENOMEM;
2902
2903 hidpp->private_data = wd;
2904
2905 return 0;
2906 };
2907
wtp_connect(struct hid_device * hdev,bool connected)2908 static int wtp_connect(struct hid_device *hdev, bool connected)
2909 {
2910 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2911 struct wtp_data *wd = hidpp->private_data;
2912 int ret;
2913
2914 if (!wd->x_size) {
2915 ret = wtp_get_config(hidpp);
2916 if (ret) {
2917 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2918 return ret;
2919 }
2920 }
2921
2922 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2923 true, true);
2924 }
2925
2926 /* ------------------------------------------------------------------------- */
2927 /* Logitech M560 devices */
2928 /* ------------------------------------------------------------------------- */
2929
2930 /*
2931 * Logitech M560 protocol overview
2932 *
2933 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2934 * the sides buttons are pressed, it sends some keyboard keys events
2935 * instead of buttons ones.
2936 * To complicate things further, the middle button keys sequence
2937 * is different from the odd press and the even press.
2938 *
2939 * forward button -> Super_R
2940 * backward button -> Super_L+'d' (press only)
2941 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2942 * 2nd time: left-click (press only)
2943 * NB: press-only means that when the button is pressed, the
2944 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2945 * together sequentially; instead when the button is released, no event is
2946 * generated !
2947 *
2948 * With the command
2949 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2950 * the mouse reacts differently:
2951 * - it never sends a keyboard key event
2952 * - for the three mouse button it sends:
2953 * middle button press 11<xx>0a 3500af00...
2954 * side 1 button (forward) press 11<xx>0a 3500b000...
2955 * side 2 button (backward) press 11<xx>0a 3500ae00...
2956 * middle/side1/side2 button release 11<xx>0a 35000000...
2957 */
2958
2959 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2960
2961 /* how buttons are mapped in the report */
2962 #define M560_MOUSE_BTN_LEFT 0x01
2963 #define M560_MOUSE_BTN_RIGHT 0x02
2964 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2965 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2966
2967 #define M560_SUB_ID 0x0a
2968 #define M560_BUTTON_MODE_REGISTER 0x35
2969
m560_send_config_command(struct hid_device * hdev,bool connected)2970 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2971 {
2972 struct hidpp_report response;
2973 struct hidpp_device *hidpp_dev;
2974
2975 hidpp_dev = hid_get_drvdata(hdev);
2976
2977 return hidpp_send_rap_command_sync(
2978 hidpp_dev,
2979 REPORT_ID_HIDPP_SHORT,
2980 M560_SUB_ID,
2981 M560_BUTTON_MODE_REGISTER,
2982 (u8 *)m560_config_parameter,
2983 sizeof(m560_config_parameter),
2984 &response
2985 );
2986 }
2987
m560_raw_event(struct hid_device * hdev,u8 * data,int size)2988 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2989 {
2990 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2991
2992 /* sanity check */
2993 if (!hidpp->input) {
2994 hid_err(hdev, "error in parameter\n");
2995 return -EINVAL;
2996 }
2997
2998 if (size < 7) {
2999 hid_err(hdev, "error in report\n");
3000 return 0;
3001 }
3002
3003 if (data[0] == REPORT_ID_HIDPP_LONG &&
3004 data[2] == M560_SUB_ID && data[6] == 0x00) {
3005 /*
3006 * m560 mouse report for middle, forward and backward button
3007 *
3008 * data[0] = 0x11
3009 * data[1] = device-id
3010 * data[2] = 0x0a
3011 * data[5] = 0xaf -> middle
3012 * 0xb0 -> forward
3013 * 0xae -> backward
3014 * 0x00 -> release all
3015 * data[6] = 0x00
3016 */
3017
3018 switch (data[5]) {
3019 case 0xaf:
3020 input_report_key(hidpp->input, BTN_MIDDLE, 1);
3021 break;
3022 case 0xb0:
3023 input_report_key(hidpp->input, BTN_FORWARD, 1);
3024 break;
3025 case 0xae:
3026 input_report_key(hidpp->input, BTN_BACK, 1);
3027 break;
3028 case 0x00:
3029 input_report_key(hidpp->input, BTN_BACK, 0);
3030 input_report_key(hidpp->input, BTN_FORWARD, 0);
3031 input_report_key(hidpp->input, BTN_MIDDLE, 0);
3032 break;
3033 default:
3034 hid_err(hdev, "error in report\n");
3035 return 0;
3036 }
3037 input_sync(hidpp->input);
3038
3039 } else if (data[0] == 0x02) {
3040 /*
3041 * Logitech M560 mouse report
3042 *
3043 * data[0] = type (0x02)
3044 * data[1..2] = buttons
3045 * data[3..5] = xy
3046 * data[6] = wheel
3047 */
3048
3049 int v;
3050
3051 input_report_key(hidpp->input, BTN_LEFT,
3052 !!(data[1] & M560_MOUSE_BTN_LEFT));
3053 input_report_key(hidpp->input, BTN_RIGHT,
3054 !!(data[1] & M560_MOUSE_BTN_RIGHT));
3055
3056 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3057 input_report_rel(hidpp->input, REL_HWHEEL, -1);
3058 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3059 -120);
3060 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3061 input_report_rel(hidpp->input, REL_HWHEEL, 1);
3062 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3063 120);
3064 }
3065
3066 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
3067 input_report_rel(hidpp->input, REL_X, v);
3068
3069 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
3070 input_report_rel(hidpp->input, REL_Y, v);
3071
3072 v = hid_snto32(data[6], 8);
3073 if (v != 0)
3074 hidpp_scroll_counter_handle_scroll(hidpp->input,
3075 &hidpp->vertical_wheel_counter, v);
3076
3077 input_sync(hidpp->input);
3078 }
3079
3080 return 1;
3081 }
3082
m560_populate_input(struct hidpp_device * hidpp,struct input_dev * input_dev)3083 static void m560_populate_input(struct hidpp_device *hidpp,
3084 struct input_dev *input_dev)
3085 {
3086 __set_bit(EV_KEY, input_dev->evbit);
3087 __set_bit(BTN_MIDDLE, input_dev->keybit);
3088 __set_bit(BTN_RIGHT, input_dev->keybit);
3089 __set_bit(BTN_LEFT, input_dev->keybit);
3090 __set_bit(BTN_BACK, input_dev->keybit);
3091 __set_bit(BTN_FORWARD, input_dev->keybit);
3092
3093 __set_bit(EV_REL, input_dev->evbit);
3094 __set_bit(REL_X, input_dev->relbit);
3095 __set_bit(REL_Y, input_dev->relbit);
3096 __set_bit(REL_WHEEL, input_dev->relbit);
3097 __set_bit(REL_HWHEEL, input_dev->relbit);
3098 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3099 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3100 }
3101
m560_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)3102 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3103 struct hid_field *field, struct hid_usage *usage,
3104 unsigned long **bit, int *max)
3105 {
3106 return -1;
3107 }
3108
3109 /* ------------------------------------------------------------------------- */
3110 /* Logitech K400 devices */
3111 /* ------------------------------------------------------------------------- */
3112
3113 /*
3114 * The Logitech K400 keyboard has an embedded touchpad which is seen
3115 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3116 * tap-to-click but the setting is not remembered accross reset, annoying some
3117 * users.
3118 *
3119 * We can toggle this feature from the host by using the feature 0x6010:
3120 * Touchpad FW items
3121 */
3122
3123 struct k400_private_data {
3124 u8 feature_index;
3125 };
3126
k400_disable_tap_to_click(struct hidpp_device * hidpp)3127 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3128 {
3129 struct k400_private_data *k400 = hidpp->private_data;
3130 struct hidpp_touchpad_fw_items items = {};
3131 int ret;
3132 u8 feature_type;
3133
3134 if (!k400->feature_index) {
3135 ret = hidpp_root_get_feature(hidpp,
3136 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3137 &k400->feature_index, &feature_type);
3138 if (ret)
3139 /* means that the device is not powered up */
3140 return ret;
3141 }
3142
3143 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3144 if (ret)
3145 return ret;
3146
3147 return 0;
3148 }
3149
k400_allocate(struct hid_device * hdev)3150 static int k400_allocate(struct hid_device *hdev)
3151 {
3152 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3153 struct k400_private_data *k400;
3154
3155 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3156 GFP_KERNEL);
3157 if (!k400)
3158 return -ENOMEM;
3159
3160 hidpp->private_data = k400;
3161
3162 return 0;
3163 };
3164
k400_connect(struct hid_device * hdev,bool connected)3165 static int k400_connect(struct hid_device *hdev, bool connected)
3166 {
3167 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3168
3169 if (!disable_tap_to_click)
3170 return 0;
3171
3172 return k400_disable_tap_to_click(hidpp);
3173 }
3174
3175 /* ------------------------------------------------------------------------- */
3176 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
3177 /* ------------------------------------------------------------------------- */
3178
3179 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3180
g920_ff_set_autocenter(struct hidpp_device * hidpp,struct hidpp_ff_private_data * data)3181 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3182 struct hidpp_ff_private_data *data)
3183 {
3184 struct hidpp_report response;
3185 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3186 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3187 };
3188 int ret;
3189
3190 /* initialize with zero autocenter to get wheel in usable state */
3191
3192 dbg_hid("Setting autocenter to 0.\n");
3193 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3194 HIDPP_FF_DOWNLOAD_EFFECT,
3195 params, ARRAY_SIZE(params),
3196 &response);
3197 if (ret)
3198 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3199 else
3200 data->slot_autocenter = response.fap.params[0];
3201
3202 return ret;
3203 }
3204
g920_get_config(struct hidpp_device * hidpp,struct hidpp_ff_private_data * data)3205 static int g920_get_config(struct hidpp_device *hidpp,
3206 struct hidpp_ff_private_data *data)
3207 {
3208 struct hidpp_report response;
3209 u8 feature_type;
3210 int ret;
3211
3212 memset(data, 0, sizeof(*data));
3213
3214 /* Find feature and store for later use */
3215 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3216 &data->feature_index, &feature_type);
3217 if (ret)
3218 return ret;
3219
3220 /* Read number of slots available in device */
3221 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3222 HIDPP_FF_GET_INFO,
3223 NULL, 0,
3224 &response);
3225 if (ret) {
3226 if (ret < 0)
3227 return ret;
3228 hid_err(hidpp->hid_dev,
3229 "%s: received protocol error 0x%02x\n", __func__, ret);
3230 return -EPROTO;
3231 }
3232
3233 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3234
3235 /* reset all forces */
3236 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3237 HIDPP_FF_RESET_ALL,
3238 NULL, 0,
3239 &response);
3240 if (ret)
3241 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3242
3243 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3244 HIDPP_FF_GET_APERTURE,
3245 NULL, 0,
3246 &response);
3247 if (ret) {
3248 hid_warn(hidpp->hid_dev,
3249 "Failed to read range from device!\n");
3250 }
3251 data->range = ret ?
3252 900 : get_unaligned_be16(&response.fap.params[0]);
3253
3254 /* Read the current gain values */
3255 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3256 HIDPP_FF_GET_GLOBAL_GAINS,
3257 NULL, 0,
3258 &response);
3259 if (ret)
3260 hid_warn(hidpp->hid_dev,
3261 "Failed to read gain values from device!\n");
3262 data->gain = ret ?
3263 0xffff : get_unaligned_be16(&response.fap.params[0]);
3264
3265 /* ignore boost value at response.fap.params[2] */
3266
3267 return g920_ff_set_autocenter(hidpp, data);
3268 }
3269
3270 /* -------------------------------------------------------------------------- */
3271 /* Logitech Dinovo Mini keyboard with builtin touchpad */
3272 /* -------------------------------------------------------------------------- */
3273 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3274
lg_dinovo_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)3275 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3276 struct hid_field *field, struct hid_usage *usage,
3277 unsigned long **bit, int *max)
3278 {
3279 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3280 return 0;
3281
3282 switch (usage->hid & HID_USAGE) {
3283 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3284 default:
3285 return 0;
3286 }
3287 return 1;
3288 }
3289
3290 /* -------------------------------------------------------------------------- */
3291 /* HID++1.0 devices which use HID++ reports for their wheels */
3292 /* -------------------------------------------------------------------------- */
hidpp10_wheel_connect(struct hidpp_device * hidpp)3293 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3294 {
3295 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3296 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3297 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3298 }
3299
hidpp10_wheel_raw_event(struct hidpp_device * hidpp,u8 * data,int size)3300 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3301 u8 *data, int size)
3302 {
3303 s8 value, hvalue;
3304
3305 if (!hidpp->input)
3306 return -EINVAL;
3307
3308 if (size < 7)
3309 return 0;
3310
3311 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3312 return 0;
3313
3314 value = data[3];
3315 hvalue = data[4];
3316
3317 input_report_rel(hidpp->input, REL_WHEEL, value);
3318 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3319 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3320 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3321 input_sync(hidpp->input);
3322
3323 return 1;
3324 }
3325
hidpp10_wheel_populate_input(struct hidpp_device * hidpp,struct input_dev * input_dev)3326 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3327 struct input_dev *input_dev)
3328 {
3329 __set_bit(EV_REL, input_dev->evbit);
3330 __set_bit(REL_WHEEL, input_dev->relbit);
3331 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3332 __set_bit(REL_HWHEEL, input_dev->relbit);
3333 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3334 }
3335
3336 /* -------------------------------------------------------------------------- */
3337 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3338 /* -------------------------------------------------------------------------- */
hidpp10_extra_mouse_buttons_connect(struct hidpp_device * hidpp)3339 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3340 {
3341 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3342 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3343 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3344 }
3345
hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device * hidpp,u8 * data,int size)3346 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3347 u8 *data, int size)
3348 {
3349 int i;
3350
3351 if (!hidpp->input)
3352 return -EINVAL;
3353
3354 if (size < 7)
3355 return 0;
3356
3357 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3358 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3359 return 0;
3360
3361 /*
3362 * Buttons are either delivered through the regular mouse report *or*
3363 * through the extra buttons report. At least for button 6 how it is
3364 * delivered differs per receiver firmware version. Even receivers with
3365 * the same usb-id show different behavior, so we handle both cases.
3366 */
3367 for (i = 0; i < 8; i++)
3368 input_report_key(hidpp->input, BTN_MOUSE + i,
3369 (data[3] & (1 << i)));
3370
3371 /* Some mice report events on button 9+, use BTN_MISC */
3372 for (i = 0; i < 8; i++)
3373 input_report_key(hidpp->input, BTN_MISC + i,
3374 (data[4] & (1 << i)));
3375
3376 input_sync(hidpp->input);
3377 return 1;
3378 }
3379
hidpp10_extra_mouse_buttons_populate_input(struct hidpp_device * hidpp,struct input_dev * input_dev)3380 static void hidpp10_extra_mouse_buttons_populate_input(
3381 struct hidpp_device *hidpp, struct input_dev *input_dev)
3382 {
3383 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3384 __set_bit(BTN_0, input_dev->keybit);
3385 __set_bit(BTN_1, input_dev->keybit);
3386 __set_bit(BTN_2, input_dev->keybit);
3387 __set_bit(BTN_3, input_dev->keybit);
3388 __set_bit(BTN_4, input_dev->keybit);
3389 __set_bit(BTN_5, input_dev->keybit);
3390 __set_bit(BTN_6, input_dev->keybit);
3391 __set_bit(BTN_7, input_dev->keybit);
3392 }
3393
3394 /* -------------------------------------------------------------------------- */
3395 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3396 /* -------------------------------------------------------------------------- */
3397
3398 /* Find the consumer-page input report desc and change Maximums to 0x107f */
hidpp10_consumer_keys_report_fixup(struct hidpp_device * hidpp,u8 * _rdesc,unsigned int * rsize)3399 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3400 u8 *_rdesc, unsigned int *rsize)
3401 {
3402 /* Note 0 terminated so we can use strnstr to search for this. */
3403 static const char consumer_rdesc_start[] = {
3404 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3405 0x09, 0x01, /* USAGE (Consumer Control) */
3406 0xA1, 0x01, /* COLLECTION (Application) */
3407 0x85, 0x03, /* REPORT_ID = 3 */
3408 0x75, 0x10, /* REPORT_SIZE (16) */
3409 0x95, 0x02, /* REPORT_COUNT (2) */
3410 0x15, 0x01, /* LOGICAL_MIN (1) */
3411 0x26, 0x00 /* LOGICAL_MAX (... */
3412 };
3413 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3414 unsigned int size;
3415
3416 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3417 size = *rsize - (consumer_rdesc - rdesc);
3418 if (consumer_rdesc && size >= 25) {
3419 consumer_rdesc[15] = 0x7f;
3420 consumer_rdesc[16] = 0x10;
3421 consumer_rdesc[20] = 0x7f;
3422 consumer_rdesc[21] = 0x10;
3423 }
3424 return _rdesc;
3425 }
3426
hidpp10_consumer_keys_connect(struct hidpp_device * hidpp)3427 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3428 {
3429 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3430 HIDPP_ENABLE_CONSUMER_REPORT,
3431 HIDPP_ENABLE_CONSUMER_REPORT);
3432 }
3433
hidpp10_consumer_keys_raw_event(struct hidpp_device * hidpp,u8 * data,int size)3434 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3435 u8 *data, int size)
3436 {
3437 u8 consumer_report[5];
3438
3439 if (size < 7)
3440 return 0;
3441
3442 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3443 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3444 return 0;
3445
3446 /*
3447 * Build a normal consumer report (3) out of the data, this detour
3448 * is necessary to get some keyboards to report their 0x10xx usages.
3449 */
3450 consumer_report[0] = 0x03;
3451 memcpy(&consumer_report[1], &data[3], 4);
3452 /* We are called from atomic context */
3453 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3454 consumer_report, 5, 1);
3455
3456 return 1;
3457 }
3458
3459 /* -------------------------------------------------------------------------- */
3460 /* High-resolution scroll wheels */
3461 /* -------------------------------------------------------------------------- */
3462
hi_res_scroll_enable(struct hidpp_device * hidpp)3463 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3464 {
3465 int ret;
3466 u8 multiplier = 1;
3467
3468 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
3469 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3470 if (ret == 0)
3471 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3472 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
3473 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3474 &multiplier);
3475 } else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
3476 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3477 multiplier = 8;
3478 }
3479 if (ret)
3480 return ret;
3481
3482 if (multiplier == 0)
3483 multiplier = 1;
3484
3485 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3486 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3487 return 0;
3488 }
3489
hidpp_initialize_hires_scroll(struct hidpp_device * hidpp)3490 static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
3491 {
3492 int ret;
3493 unsigned long capabilities;
3494
3495 capabilities = hidpp->capabilities;
3496
3497 if (hidpp->protocol_major >= 2) {
3498 u8 feature_index;
3499 u8 feature_type;
3500
3501 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
3502 &feature_index, &feature_type);
3503 if (!ret) {
3504 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
3505 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
3506 return 0;
3507 }
3508 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
3509 &feature_index, &feature_type);
3510 if (!ret) {
3511 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
3512 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
3513 }
3514 } else {
3515 /* We cannot detect fast scrolling support on HID++ 1.0 devices */
3516 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) {
3517 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
3518 hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
3519 }
3520 }
3521
3522 if (hidpp->capabilities == capabilities)
3523 hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
3524 return 0;
3525 }
3526
3527 /* -------------------------------------------------------------------------- */
3528 /* Generic HID++ devices */
3529 /* -------------------------------------------------------------------------- */
3530
hidpp_report_fixup(struct hid_device * hdev,u8 * rdesc,unsigned int * rsize)3531 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3532 unsigned int *rsize)
3533 {
3534 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3535
3536 if (!hidpp)
3537 return rdesc;
3538
3539 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3540 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3541 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3542 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3543
3544 return rdesc;
3545 }
3546
hidpp_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)3547 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3548 struct hid_field *field, struct hid_usage *usage,
3549 unsigned long **bit, int *max)
3550 {
3551 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3552
3553 if (!hidpp)
3554 return 0;
3555
3556 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3557 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3558 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3559 field->application != HID_GD_MOUSE)
3560 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3561
3562 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3563 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3564
3565 return 0;
3566 }
3567
hidpp_input_mapped(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)3568 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3569 struct hid_field *field, struct hid_usage *usage,
3570 unsigned long **bit, int *max)
3571 {
3572 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3573
3574 if (!hidpp)
3575 return 0;
3576
3577 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3578 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3579 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3580 usage->code == ABS_Y || usage->code == ABS_Z ||
3581 usage->code == ABS_RZ)) {
3582 field->application = HID_GD_MULTIAXIS;
3583 }
3584 }
3585
3586 return 0;
3587 }
3588
3589
hidpp_populate_input(struct hidpp_device * hidpp,struct input_dev * input)3590 static void hidpp_populate_input(struct hidpp_device *hidpp,
3591 struct input_dev *input)
3592 {
3593 hidpp->input = input;
3594
3595 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3596 wtp_populate_input(hidpp, input);
3597 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3598 m560_populate_input(hidpp, input);
3599
3600 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3601 hidpp10_wheel_populate_input(hidpp, input);
3602
3603 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3604 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3605 }
3606
hidpp_input_configured(struct hid_device * hdev,struct hid_input * hidinput)3607 static int hidpp_input_configured(struct hid_device *hdev,
3608 struct hid_input *hidinput)
3609 {
3610 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3611 struct input_dev *input = hidinput->input;
3612
3613 if (!hidpp)
3614 return 0;
3615
3616 hidpp_populate_input(hidpp, input);
3617
3618 return 0;
3619 }
3620
hidpp_raw_hidpp_event(struct hidpp_device * hidpp,u8 * data,int size)3621 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3622 int size)
3623 {
3624 struct hidpp_report *question = hidpp->send_receive_buf;
3625 struct hidpp_report *answer = hidpp->send_receive_buf;
3626 struct hidpp_report *report = (struct hidpp_report *)data;
3627 int ret;
3628
3629 /*
3630 * If the mutex is locked then we have a pending answer from a
3631 * previously sent command.
3632 */
3633 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3634 /*
3635 * Check for a correct hidpp20 answer or the corresponding
3636 * error
3637 */
3638 if (hidpp_match_answer(question, report) ||
3639 hidpp_match_error(question, report)) {
3640 *answer = *report;
3641 hidpp->answer_available = true;
3642 wake_up(&hidpp->wait);
3643 /*
3644 * This was an answer to a command that this driver sent
3645 * We return 1 to hid-core to avoid forwarding the
3646 * command upstream as it has been treated by the driver
3647 */
3648
3649 return 1;
3650 }
3651 }
3652
3653 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3654 atomic_set(&hidpp->connected,
3655 !(report->rap.params[0] & (1 << 6)));
3656 if (schedule_work(&hidpp->work) == 0)
3657 dbg_hid("%s: connect event already queued\n", __func__);
3658 return 1;
3659 }
3660
3661 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3662 data[0] == REPORT_ID_HIDPP_SHORT &&
3663 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3664 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3665 dev_err_ratelimited(&hidpp->hid_dev->dev,
3666 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3667 dev_err_ratelimited(&hidpp->hid_dev->dev,
3668 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3669 }
3670
3671 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3672 ret = hidpp20_battery_event_1000(hidpp, data, size);
3673 if (ret != 0)
3674 return ret;
3675 ret = hidpp20_battery_event_1004(hidpp, data, size);
3676 if (ret != 0)
3677 return ret;
3678 ret = hidpp_solar_battery_event(hidpp, data, size);
3679 if (ret != 0)
3680 return ret;
3681 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3682 if (ret != 0)
3683 return ret;
3684 }
3685
3686 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3687 ret = hidpp10_battery_event(hidpp, data, size);
3688 if (ret != 0)
3689 return ret;
3690 }
3691
3692 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3693 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3694 if (ret != 0)
3695 return ret;
3696 }
3697
3698 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3699 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3700 if (ret != 0)
3701 return ret;
3702 }
3703
3704 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3705 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3706 if (ret != 0)
3707 return ret;
3708 }
3709
3710 return 0;
3711 }
3712
hidpp_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)3713 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3714 u8 *data, int size)
3715 {
3716 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3717 int ret = 0;
3718
3719 if (!hidpp)
3720 return 0;
3721
3722 /* Generic HID++ processing. */
3723 switch (data[0]) {
3724 case REPORT_ID_HIDPP_VERY_LONG:
3725 if (size != hidpp->very_long_report_length) {
3726 hid_err(hdev, "received hid++ report of bad size (%d)",
3727 size);
3728 return 1;
3729 }
3730 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3731 break;
3732 case REPORT_ID_HIDPP_LONG:
3733 if (size != HIDPP_REPORT_LONG_LENGTH) {
3734 hid_err(hdev, "received hid++ report of bad size (%d)",
3735 size);
3736 return 1;
3737 }
3738 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3739 break;
3740 case REPORT_ID_HIDPP_SHORT:
3741 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3742 hid_err(hdev, "received hid++ report of bad size (%d)",
3743 size);
3744 return 1;
3745 }
3746 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3747 break;
3748 }
3749
3750 /* If no report is available for further processing, skip calling
3751 * raw_event of subclasses. */
3752 if (ret != 0)
3753 return ret;
3754
3755 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3756 return wtp_raw_event(hdev, data, size);
3757 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3758 return m560_raw_event(hdev, data, size);
3759
3760 return 0;
3761 }
3762
hidpp_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)3763 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3764 struct hid_usage *usage, __s32 value)
3765 {
3766 /* This function will only be called for scroll events, due to the
3767 * restriction imposed in hidpp_usages.
3768 */
3769 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3770 struct hidpp_scroll_counter *counter;
3771
3772 if (!hidpp)
3773 return 0;
3774
3775 counter = &hidpp->vertical_wheel_counter;
3776 /* A scroll event may occur before the multiplier has been retrieved or
3777 * the input device set, or high-res scroll enabling may fail. In such
3778 * cases we must return early (falling back to default behaviour) to
3779 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3780 */
3781 if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
3782 || value == 0 || hidpp->input == NULL
3783 || counter->wheel_multiplier == 0)
3784 return 0;
3785
3786 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3787 return 1;
3788 }
3789
hidpp_initialize_battery(struct hidpp_device * hidpp)3790 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3791 {
3792 static atomic_t battery_no = ATOMIC_INIT(0);
3793 struct power_supply_config cfg = { .drv_data = hidpp };
3794 struct power_supply_desc *desc = &hidpp->battery.desc;
3795 enum power_supply_property *battery_props;
3796 struct hidpp_battery *battery;
3797 unsigned int num_battery_props;
3798 unsigned long n;
3799 int ret;
3800
3801 if (hidpp->battery.ps)
3802 return 0;
3803
3804 hidpp->battery.feature_index = 0xff;
3805 hidpp->battery.solar_feature_index = 0xff;
3806 hidpp->battery.voltage_feature_index = 0xff;
3807
3808 if (hidpp->protocol_major >= 2) {
3809 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3810 ret = hidpp_solar_request_battery_event(hidpp);
3811 else {
3812 /* we only support one battery feature right now, so let's
3813 first check the ones that support battery level first
3814 and leave voltage for last */
3815 ret = hidpp20_query_battery_info_1000(hidpp);
3816 if (ret)
3817 ret = hidpp20_query_battery_info_1004(hidpp);
3818 if (ret)
3819 ret = hidpp20_query_battery_voltage_info(hidpp);
3820 }
3821
3822 if (ret)
3823 return ret;
3824 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3825 } else {
3826 ret = hidpp10_query_battery_status(hidpp);
3827 if (ret) {
3828 ret = hidpp10_query_battery_mileage(hidpp);
3829 if (ret)
3830 return -ENOENT;
3831 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3832 } else {
3833 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3834 }
3835 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3836 }
3837
3838 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3839 hidpp_battery_props,
3840 sizeof(hidpp_battery_props),
3841 GFP_KERNEL);
3842 if (!battery_props)
3843 return -ENOMEM;
3844
3845 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3846
3847 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
3848 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
3849 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3850 battery_props[num_battery_props++] =
3851 POWER_SUPPLY_PROP_CAPACITY;
3852
3853 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3854 battery_props[num_battery_props++] =
3855 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3856
3857 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3858 battery_props[num_battery_props++] =
3859 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3860
3861 battery = &hidpp->battery;
3862
3863 n = atomic_inc_return(&battery_no) - 1;
3864 desc->properties = battery_props;
3865 desc->num_properties = num_battery_props;
3866 desc->get_property = hidpp_battery_get_property;
3867 sprintf(battery->name, "hidpp_battery_%ld", n);
3868 desc->name = battery->name;
3869 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3870 desc->use_for_apm = 0;
3871
3872 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3873 &battery->desc,
3874 &cfg);
3875 if (IS_ERR(battery->ps))
3876 return PTR_ERR(battery->ps);
3877
3878 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3879
3880 return ret;
3881 }
3882
hidpp_overwrite_name(struct hid_device * hdev)3883 static void hidpp_overwrite_name(struct hid_device *hdev)
3884 {
3885 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3886 char *name;
3887
3888 if (hidpp->protocol_major < 2)
3889 return;
3890
3891 name = hidpp_get_device_name(hidpp);
3892
3893 if (!name) {
3894 hid_err(hdev, "unable to retrieve the name of the device");
3895 } else {
3896 dbg_hid("HID++: Got name: %s\n", name);
3897 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3898 }
3899
3900 kfree(name);
3901 }
3902
hidpp_input_open(struct input_dev * dev)3903 static int hidpp_input_open(struct input_dev *dev)
3904 {
3905 struct hid_device *hid = input_get_drvdata(dev);
3906
3907 return hid_hw_open(hid);
3908 }
3909
hidpp_input_close(struct input_dev * dev)3910 static void hidpp_input_close(struct input_dev *dev)
3911 {
3912 struct hid_device *hid = input_get_drvdata(dev);
3913
3914 hid_hw_close(hid);
3915 }
3916
hidpp_allocate_input(struct hid_device * hdev)3917 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3918 {
3919 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3920 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3921
3922 if (!input_dev)
3923 return NULL;
3924
3925 input_set_drvdata(input_dev, hdev);
3926 input_dev->open = hidpp_input_open;
3927 input_dev->close = hidpp_input_close;
3928
3929 input_dev->name = hidpp->name;
3930 input_dev->phys = hdev->phys;
3931 input_dev->uniq = hdev->uniq;
3932 input_dev->id.bustype = hdev->bus;
3933 input_dev->id.vendor = hdev->vendor;
3934 input_dev->id.product = hdev->product;
3935 input_dev->id.version = hdev->version;
3936 input_dev->dev.parent = &hdev->dev;
3937
3938 return input_dev;
3939 }
3940
hidpp_connect_event(struct hidpp_device * hidpp)3941 static void hidpp_connect_event(struct hidpp_device *hidpp)
3942 {
3943 struct hid_device *hdev = hidpp->hid_dev;
3944 int ret = 0;
3945 bool connected = atomic_read(&hidpp->connected);
3946 struct input_dev *input;
3947 char *name, *devm_name;
3948
3949 if (!connected) {
3950 if (hidpp->battery.ps) {
3951 hidpp->battery.online = false;
3952 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3953 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3954 power_supply_changed(hidpp->battery.ps);
3955 }
3956 return;
3957 }
3958
3959 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3960 ret = wtp_connect(hdev, connected);
3961 if (ret)
3962 return;
3963 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3964 ret = m560_send_config_command(hdev, connected);
3965 if (ret)
3966 return;
3967 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3968 ret = k400_connect(hdev, connected);
3969 if (ret)
3970 return;
3971 }
3972
3973 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3974 ret = hidpp10_wheel_connect(hidpp);
3975 if (ret)
3976 return;
3977 }
3978
3979 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3980 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3981 if (ret)
3982 return;
3983 }
3984
3985 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3986 ret = hidpp10_consumer_keys_connect(hidpp);
3987 if (ret)
3988 return;
3989 }
3990
3991 /* the device is already connected, we can ask for its name and
3992 * protocol */
3993 if (!hidpp->protocol_major) {
3994 ret = hidpp_root_get_protocol_version(hidpp);
3995 if (ret) {
3996 hid_err(hdev, "Can not get the protocol version.\n");
3997 return;
3998 }
3999 }
4000
4001 if (hidpp->protocol_major >= 2) {
4002 u8 feature_index;
4003
4004 if (!hidpp_get_wireless_feature_index(hidpp, &feature_index))
4005 hidpp->wireless_feature_index = feature_index;
4006 }
4007
4008 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
4009 name = hidpp_get_device_name(hidpp);
4010 if (name) {
4011 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
4012 "%s", name);
4013 kfree(name);
4014 if (!devm_name)
4015 return;
4016
4017 hidpp->name = devm_name;
4018 }
4019 }
4020
4021 hidpp_initialize_battery(hidpp);
4022 if (!hid_is_usb(hidpp->hid_dev))
4023 hidpp_initialize_hires_scroll(hidpp);
4024
4025 /* forward current battery state */
4026 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
4027 hidpp10_enable_battery_reporting(hidpp);
4028 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
4029 hidpp10_query_battery_mileage(hidpp);
4030 else
4031 hidpp10_query_battery_status(hidpp);
4032 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
4033 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
4034 hidpp20_query_battery_voltage_info(hidpp);
4035 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
4036 hidpp20_query_battery_info_1004(hidpp);
4037 else
4038 hidpp20_query_battery_info_1000(hidpp);
4039 }
4040 if (hidpp->battery.ps)
4041 power_supply_changed(hidpp->battery.ps);
4042
4043 if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4044 hi_res_scroll_enable(hidpp);
4045
4046 if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
4047 /* if the input nodes are already created, we can stop now */
4048 return;
4049
4050 input = hidpp_allocate_input(hdev);
4051 if (!input) {
4052 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
4053 return;
4054 }
4055
4056 hidpp_populate_input(hidpp, input);
4057
4058 ret = input_register_device(input);
4059 if (ret) {
4060 input_free_device(input);
4061 return;
4062 }
4063
4064 hidpp->delayed_input = input;
4065 }
4066
4067 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
4068
4069 static struct attribute *sysfs_attrs[] = {
4070 &dev_attr_builtin_power_supply.attr,
4071 NULL
4072 };
4073
4074 static const struct attribute_group ps_attribute_group = {
4075 .attrs = sysfs_attrs
4076 };
4077
hidpp_get_report_length(struct hid_device * hdev,int id)4078 static int hidpp_get_report_length(struct hid_device *hdev, int id)
4079 {
4080 struct hid_report_enum *re;
4081 struct hid_report *report;
4082
4083 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
4084 report = re->report_id_hash[id];
4085 if (!report)
4086 return 0;
4087
4088 return report->field[0]->report_count + 1;
4089 }
4090
hidpp_validate_device(struct hid_device * hdev)4091 static u8 hidpp_validate_device(struct hid_device *hdev)
4092 {
4093 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4094 int id, report_length;
4095 u8 supported_reports = 0;
4096
4097 id = REPORT_ID_HIDPP_SHORT;
4098 report_length = hidpp_get_report_length(hdev, id);
4099 if (report_length) {
4100 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4101 goto bad_device;
4102
4103 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4104 }
4105
4106 id = REPORT_ID_HIDPP_LONG;
4107 report_length = hidpp_get_report_length(hdev, id);
4108 if (report_length) {
4109 if (report_length < HIDPP_REPORT_LONG_LENGTH)
4110 goto bad_device;
4111
4112 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4113 }
4114
4115 id = REPORT_ID_HIDPP_VERY_LONG;
4116 report_length = hidpp_get_report_length(hdev, id);
4117 if (report_length) {
4118 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4119 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4120 goto bad_device;
4121
4122 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4123 hidpp->very_long_report_length = report_length;
4124 }
4125
4126 return supported_reports;
4127
4128 bad_device:
4129 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4130 return false;
4131 }
4132
hidpp_application_equals(struct hid_device * hdev,unsigned int application)4133 static bool hidpp_application_equals(struct hid_device *hdev,
4134 unsigned int application)
4135 {
4136 struct list_head *report_list;
4137 struct hid_report *report;
4138
4139 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4140 report = list_first_entry_or_null(report_list, struct hid_report, list);
4141 return report && report->application == application;
4142 }
4143
hidpp_probe(struct hid_device * hdev,const struct hid_device_id * id)4144 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4145 {
4146 struct hidpp_device *hidpp;
4147 int ret;
4148 bool connected;
4149 unsigned int connect_mask = HID_CONNECT_DEFAULT;
4150 struct hidpp_ff_private_data data;
4151
4152 /* report_fixup needs drvdata to be set before we call hid_parse */
4153 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4154 if (!hidpp)
4155 return -ENOMEM;
4156
4157 hidpp->hid_dev = hdev;
4158 hidpp->name = hdev->name;
4159 hidpp->quirks = id->driver_data;
4160 hid_set_drvdata(hdev, hidpp);
4161
4162 ret = hid_parse(hdev);
4163 if (ret) {
4164 hid_err(hdev, "%s:parse failed\n", __func__);
4165 return ret;
4166 }
4167
4168 /*
4169 * Make sure the device is HID++ capable, otherwise treat as generic HID
4170 */
4171 hidpp->supported_reports = hidpp_validate_device(hdev);
4172
4173 if (!hidpp->supported_reports) {
4174 hid_set_drvdata(hdev, NULL);
4175 devm_kfree(&hdev->dev, hidpp);
4176 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4177 }
4178
4179 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4180 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
4181
4182 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4183 hidpp_application_equals(hdev, HID_GD_MOUSE))
4184 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4185 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4186
4187 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4188 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4189 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4190
4191 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4192 ret = wtp_allocate(hdev, id);
4193 if (ret)
4194 return ret;
4195 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4196 ret = k400_allocate(hdev);
4197 if (ret)
4198 return ret;
4199 }
4200
4201 INIT_WORK(&hidpp->work, delayed_work_cb);
4202 mutex_init(&hidpp->send_mutex);
4203 init_waitqueue_head(&hidpp->wait);
4204
4205 /* indicates we are handling the battery properties in the kernel */
4206 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4207 if (ret)
4208 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4209 hdev->name);
4210
4211 /*
4212 * First call hid_hw_start(hdev, 0) to allow IO without connecting any
4213 * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's
4214 * name and serial number and store these in hdev->name and hdev->uniq,
4215 * before the hid-input and hidraw drivers expose these to userspace.
4216 */
4217 ret = hid_hw_start(hdev, 0);
4218 if (ret) {
4219 hid_err(hdev, "hw start failed\n");
4220 goto hid_hw_start_fail;
4221 }
4222
4223 ret = hid_hw_open(hdev);
4224 if (ret < 0) {
4225 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4226 __func__, ret);
4227 goto hid_hw_open_fail;
4228 }
4229
4230 /* Allow incoming packets */
4231 hid_device_io_start(hdev);
4232
4233 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
4234 hidpp_unifying_init(hidpp);
4235 else if (hid_is_usb(hidpp->hid_dev))
4236 hidpp_serial_init(hidpp);
4237
4238 connected = hidpp_root_get_protocol_version(hidpp) == 0;
4239 atomic_set(&hidpp->connected, connected);
4240 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
4241 if (!connected) {
4242 ret = -ENODEV;
4243 hid_err(hdev, "Device not connected");
4244 goto hid_hw_init_fail;
4245 }
4246
4247 hidpp_overwrite_name(hdev);
4248 }
4249
4250 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
4251 ret = wtp_get_config(hidpp);
4252 if (ret)
4253 goto hid_hw_init_fail;
4254 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
4255 ret = g920_get_config(hidpp, &data);
4256 if (ret)
4257 goto hid_hw_init_fail;
4258 }
4259
4260 schedule_work(&hidpp->work);
4261 flush_work(&hidpp->work);
4262
4263 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
4264 connect_mask &= ~HID_CONNECT_HIDINPUT;
4265
4266 /* Now export the actual inputs and hidraw nodes to the world */
4267 ret = hid_connect(hdev, connect_mask);
4268 if (ret) {
4269 hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret);
4270 goto hid_hw_init_fail;
4271 }
4272
4273 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4274 ret = hidpp_ff_init(hidpp, &data);
4275 if (ret)
4276 hid_warn(hidpp->hid_dev,
4277 "Unable to initialize force feedback support, errno %d\n",
4278 ret);
4279 }
4280
4281 /*
4282 * This relies on logi_dj_ll_close() being a no-op so that DJ connection
4283 * events will still be received.
4284 */
4285 hid_hw_close(hdev);
4286 return ret;
4287
4288 hid_hw_init_fail:
4289 hid_hw_close(hdev);
4290 hid_hw_open_fail:
4291 hid_hw_stop(hdev);
4292 hid_hw_start_fail:
4293 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4294 cancel_work_sync(&hidpp->work);
4295 mutex_destroy(&hidpp->send_mutex);
4296 return ret;
4297 }
4298
hidpp_remove(struct hid_device * hdev)4299 static void hidpp_remove(struct hid_device *hdev)
4300 {
4301 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4302
4303 if (!hidpp)
4304 return hid_hw_stop(hdev);
4305
4306 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4307
4308 hid_hw_stop(hdev);
4309 cancel_work_sync(&hidpp->work);
4310 mutex_destroy(&hidpp->send_mutex);
4311 }
4312
4313 #define LDJ_DEVICE(product) \
4314 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4315 USB_VENDOR_ID_LOGITECH, (product))
4316
4317 #define L27MHZ_DEVICE(product) \
4318 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4319 USB_VENDOR_ID_LOGITECH, (product))
4320
4321 static const struct hid_device_id hidpp_devices[] = {
4322 { /* wireless touchpad */
4323 LDJ_DEVICE(0x4011),
4324 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4325 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4326 { /* wireless touchpad T650 */
4327 LDJ_DEVICE(0x4101),
4328 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4329 { /* wireless touchpad T651 */
4330 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4331 USB_DEVICE_ID_LOGITECH_T651),
4332 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4333 { /* Mouse Logitech Anywhere MX */
4334 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4335 { /* Mouse logitech M560 */
4336 LDJ_DEVICE(0x402d),
4337 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
4338 { /* Mouse Logitech M705 (firmware RQM17) */
4339 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4340 { /* Mouse Logitech Performance MX */
4341 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4342 { /* Keyboard logitech K400 */
4343 LDJ_DEVICE(0x4024),
4344 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4345 { /* Solar Keyboard Logitech K750 */
4346 LDJ_DEVICE(0x4002),
4347 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4348 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4349 LDJ_DEVICE(0xb305),
4350 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4351 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4352 LDJ_DEVICE(0xb309),
4353 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4354 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4355 LDJ_DEVICE(0xb30b),
4356 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4357
4358 { LDJ_DEVICE(HID_ANY_ID) },
4359
4360 { /* Keyboard LX501 (Y-RR53) */
4361 L27MHZ_DEVICE(0x0049),
4362 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4363 { /* Keyboard MX3000 (Y-RAM74) */
4364 L27MHZ_DEVICE(0x0057),
4365 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4366 { /* Keyboard MX3200 (Y-RAV80) */
4367 L27MHZ_DEVICE(0x005c),
4368 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4369 { /* S510 Media Remote */
4370 L27MHZ_DEVICE(0x00fe),
4371 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4372
4373 { L27MHZ_DEVICE(HID_ANY_ID) },
4374
4375 { /* Logitech G403 Wireless Gaming Mouse over USB */
4376 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4377 { /* Logitech G703 Gaming Mouse over USB */
4378 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4379 { /* Logitech G703 Hero Gaming Mouse over USB */
4380 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4381 { /* Logitech G900 Gaming Mouse over USB */
4382 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4383 { /* Logitech G903 Gaming Mouse over USB */
4384 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4385 { /* Logitech G903 Hero Gaming Mouse over USB */
4386 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4387 { /* Logitech G915 TKL Keyboard over USB */
4388 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC343) },
4389 { /* Logitech G920 Wheel over USB */
4390 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4391 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4392 { /* Logitech G Pro Gaming Mouse over USB */
4393 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4394
4395 { /* MX5000 keyboard over Bluetooth */
4396 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4397 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4398 { /* Dinovo Edge keyboard over Bluetooth */
4399 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4400 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4401 { /* MX5500 keyboard over Bluetooth */
4402 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4403 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4404 { /* Logitech G915 TKL keyboard over Bluetooth */
4405 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb35f) },
4406 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4407 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4408 { /* MX Master mouse over Bluetooth */
4409 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
4410 { /* M720 Triathlon mouse over Bluetooth */
4411 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb015) },
4412 { /* MX Ergo trackball over Bluetooth */
4413 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4414 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
4415 { /* MX Master 3 mouse over Bluetooth */
4416 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
4417 { /* MX Master 3S mouse over Bluetooth */
4418 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) },
4419 {}
4420 };
4421
4422 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4423
4424 static const struct hid_usage_id hidpp_usages[] = {
4425 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4426 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4427 };
4428
4429 static struct hid_driver hidpp_driver = {
4430 .name = "logitech-hidpp-device",
4431 .id_table = hidpp_devices,
4432 .report_fixup = hidpp_report_fixup,
4433 .probe = hidpp_probe,
4434 .remove = hidpp_remove,
4435 .raw_event = hidpp_raw_event,
4436 .usage_table = hidpp_usages,
4437 .event = hidpp_event,
4438 .input_configured = hidpp_input_configured,
4439 .input_mapping = hidpp_input_mapping,
4440 .input_mapped = hidpp_input_mapped,
4441 };
4442
4443 module_hid_driver(hidpp_driver);
4444