1 // SPDX-License-Identifier: GPL-2.0-only
3 * HIDPP protocol for Logitech receivers
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
34 static bool disable_raw_mode;
35 module_param(disable_raw_mode, bool, 0644);
36 MODULE_PARM_DESC(disable_raw_mode,
37 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
39 static bool disable_tap_to_click;
40 module_param(disable_tap_to_click, bool, 0644);
41 MODULE_PARM_DESC(disable_tap_to_click,
42 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
44 #define REPORT_ID_HIDPP_SHORT 0x10
45 #define REPORT_ID_HIDPP_LONG 0x11
46 #define REPORT_ID_HIDPP_VERY_LONG 0x12
48 #define HIDPP_REPORT_SHORT_LENGTH 7
49 #define HIDPP_REPORT_LONG_LENGTH 20
50 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
52 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
53 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
54 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
56 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
57 #define HIDPP_SUB_ID_ROLLER 0x05
58 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
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)
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_NO_HIDINPUT BIT(23)
70 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
71 #define HIDPP_QUIRK_UNIFYING BIT(25)
72 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
73 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
74 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
75 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
76 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
77 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
79 /* These are just aliases for now */
80 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
81 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
83 /* Convenience constant to check for any high-res support. */
84 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
85 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
86 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
88 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
90 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
91 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
92 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
93 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
94 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
95 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
96 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
98 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
101 * There are two hidpp protocols in use, the first version hidpp10 is known
102 * as register access protocol or RAP, the second version hidpp20 is known as
103 * feature access protocol or FAP
105 * Most older devices (including the Unifying usb receiver) use the RAP protocol
106 * where as most newer devices use the FAP protocol. Both protocols are
107 * compatible with the underlying transport, which could be usb, Unifiying, or
108 * bluetooth. The message lengths are defined by the hid vendor specific report
109 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
110 * the HIDPP_LONG report type (total message length 20 bytes)
112 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
113 * messages. The Unifying receiver itself responds to RAP messages (device index
114 * is 0xFF for the receiver), and all messages (short or long) with a device
115 * index between 1 and 6 are passed untouched to the corresponding paired
118 * The paired device can be RAP or FAP, it will receive the message untouched
119 * from the Unifiying receiver.
124 u8 funcindex_clientid;
125 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
131 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
134 struct hidpp_report {
140 u8 rawbytes[sizeof(struct fap)];
144 struct hidpp_battery {
146 u8 solar_feature_index;
147 u8 voltage_feature_index;
148 struct power_supply_desc desc;
149 struct power_supply *ps;
157 u8 supported_levels_1004;
161 * struct hidpp_scroll_counter - Utility class for processing high-resolution
163 * @dev: the input device for which events should be reported.
164 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
165 * @remainder: counts the number of high-resolution units moved since the last
166 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
167 * only be used by class methods.
168 * @direction: direction of last movement (1 or -1)
169 * @last_time: last event time, used to reset remainder after inactivity
171 struct hidpp_scroll_counter {
172 int wheel_multiplier;
175 unsigned long long last_time;
178 struct hidpp_device {
179 struct hid_device *hid_dev;
180 struct input_dev *input;
181 struct mutex send_mutex;
182 void *send_receive_buf;
183 char *name; /* will never be NULL and should not be freed */
184 wait_queue_head_t wait;
185 int very_long_report_length;
186 bool answer_available;
192 struct work_struct work;
193 struct kfifo delayed_work_fifo;
195 struct input_dev *delayed_input;
197 unsigned long quirks;
198 unsigned long capabilities;
199 u8 supported_reports;
201 struct hidpp_battery battery;
202 struct hidpp_scroll_counter vertical_wheel_counter;
204 u8 wireless_feature_index;
207 /* HID++ 1.0 error codes */
208 #define HIDPP_ERROR 0x8f
209 #define HIDPP_ERROR_SUCCESS 0x00
210 #define HIDPP_ERROR_INVALID_SUBID 0x01
211 #define HIDPP_ERROR_INVALID_ADRESS 0x02
212 #define HIDPP_ERROR_INVALID_VALUE 0x03
213 #define HIDPP_ERROR_CONNECT_FAIL 0x04
214 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
215 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
216 #define HIDPP_ERROR_BUSY 0x07
217 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
218 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
219 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
220 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
221 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
222 /* HID++ 2.0 error codes */
223 #define HIDPP20_ERROR 0xff
225 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
227 static int __hidpp_send_report(struct hid_device *hdev,
228 struct hidpp_report *hidpp_report)
230 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
231 int fields_count, ret;
233 switch (hidpp_report->report_id) {
234 case REPORT_ID_HIDPP_SHORT:
235 fields_count = HIDPP_REPORT_SHORT_LENGTH;
237 case REPORT_ID_HIDPP_LONG:
238 fields_count = HIDPP_REPORT_LONG_LENGTH;
240 case REPORT_ID_HIDPP_VERY_LONG:
241 fields_count = hidpp->very_long_report_length;
248 * set the device_index as the receiver, it will be overwritten by
249 * hid_hw_request if needed
251 hidpp_report->device_index = 0xff;
253 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
254 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
256 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
257 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
261 return ret == fields_count ? 0 : -1;
265 * hidpp_send_message_sync() returns 0 in case of success, and something else
266 * in case of a failure.
267 * - If ' something else' is positive, that means that an error has been raised
268 * by the protocol itself.
269 * - If ' something else' is negative, that means that we had a classic error
270 * (-ENOMEM, -EPIPE, etc...)
272 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
273 struct hidpp_report *message,
274 struct hidpp_report *response)
278 mutex_lock(&hidpp->send_mutex);
280 hidpp->send_receive_buf = response;
281 hidpp->answer_available = false;
284 * So that we can later validate the answer when it arrives
287 *response = *message;
289 ret = __hidpp_send_report(hidpp->hid_dev, message);
292 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
293 memset(response, 0, sizeof(struct hidpp_report));
297 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
299 dbg_hid("%s:timeout waiting for response\n", __func__);
300 memset(response, 0, sizeof(struct hidpp_report));
304 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
305 response->rap.sub_id == HIDPP_ERROR) {
306 ret = response->rap.params[1];
307 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
311 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
312 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
313 response->fap.feature_index == HIDPP20_ERROR) {
314 ret = response->fap.params[1];
315 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
320 mutex_unlock(&hidpp->send_mutex);
325 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
326 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
327 struct hidpp_report *response)
329 struct hidpp_report *message;
332 if (param_count > sizeof(message->fap.params))
335 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
339 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
340 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
342 message->report_id = REPORT_ID_HIDPP_LONG;
343 message->fap.feature_index = feat_index;
344 message->fap.funcindex_clientid = funcindex_clientid;
345 memcpy(&message->fap.params, params, param_count);
347 ret = hidpp_send_message_sync(hidpp, message, response);
352 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
353 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
354 struct hidpp_report *response)
356 struct hidpp_report *message;
359 /* Send as long report if short reports are not supported. */
360 if (report_id == REPORT_ID_HIDPP_SHORT &&
361 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
362 report_id = REPORT_ID_HIDPP_LONG;
365 case REPORT_ID_HIDPP_SHORT:
366 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
368 case REPORT_ID_HIDPP_LONG:
369 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
371 case REPORT_ID_HIDPP_VERY_LONG:
372 max_count = hidpp_dev->very_long_report_length - 4;
378 if (param_count > max_count)
381 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
384 message->report_id = report_id;
385 message->rap.sub_id = sub_id;
386 message->rap.reg_address = reg_address;
387 memcpy(&message->rap.params, params, param_count);
389 ret = hidpp_send_message_sync(hidpp_dev, message, response);
394 static void delayed_work_cb(struct work_struct *work)
396 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
398 hidpp_connect_event(hidpp);
401 static inline bool hidpp_match_answer(struct hidpp_report *question,
402 struct hidpp_report *answer)
404 return (answer->fap.feature_index == question->fap.feature_index) &&
405 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
408 static inline bool hidpp_match_error(struct hidpp_report *question,
409 struct hidpp_report *answer)
411 return ((answer->rap.sub_id == HIDPP_ERROR) ||
412 (answer->fap.feature_index == HIDPP20_ERROR)) &&
413 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
414 (answer->fap.params[0] == question->fap.funcindex_clientid);
417 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
418 struct hidpp_report *report)
420 return (hidpp->wireless_feature_index &&
421 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
422 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
423 (report->rap.sub_id == 0x41));
427 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
429 static void hidpp_prefix_name(char **name, int name_length)
431 #define PREFIX_LENGTH 9 /* "Logitech " */
436 if (name_length > PREFIX_LENGTH &&
437 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
438 /* The prefix has is already in the name */
441 new_length = PREFIX_LENGTH + name_length;
442 new_name = kzalloc(new_length, GFP_KERNEL);
446 snprintf(new_name, new_length, "Logitech %s", *name);
454 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
455 * events given a high-resolution wheel
457 * @input_dev: Pointer to the input device
458 * @counter: a hid_scroll_counter struct describing the wheel.
459 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
462 * Given a high-resolution movement, this function converts the movement into
463 * fractions of 120 and emits high-resolution scroll events for the input
464 * device. It also uses the multiplier from &struct hid_scroll_counter to
465 * emit low-resolution scroll events when appropriate for
466 * backwards-compatibility with userspace input libraries.
468 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
469 struct hidpp_scroll_counter *counter,
472 int low_res_value, remainder, direction;
473 unsigned long long now, previous;
475 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
476 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
478 remainder = counter->remainder;
479 direction = hi_res_value > 0 ? 1 : -1;
482 previous = counter->last_time;
483 counter->last_time = now;
485 * Reset the remainder after a period of inactivity or when the
486 * direction changes. This prevents the REL_WHEEL emulation point
487 * from sliding for devices that don't always provide the same
488 * number of movements per detent.
490 if (now - previous > 1000000000 || direction != counter->direction)
493 counter->direction = direction;
494 remainder += hi_res_value;
496 /* Some wheels will rest 7/8ths of a detent from the previous detent
497 * after slow movement, so we want the threshold for low-res events to
498 * be in the middle between two detents (e.g. after 4/8ths) as
499 * opposed to on the detents themselves (8/8ths).
501 if (abs(remainder) >= 60) {
502 /* Add (or subtract) 1 because we want to trigger when the wheel
503 * is half-way to the next detent (i.e. scroll 1 detent after a
504 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
507 low_res_value = remainder / 120;
508 if (low_res_value == 0)
509 low_res_value = (hi_res_value > 0 ? 1 : -1);
510 input_report_rel(input_dev, REL_WHEEL, low_res_value);
511 remainder -= low_res_value * 120;
513 counter->remainder = remainder;
516 /* -------------------------------------------------------------------------- */
517 /* HIDP++ 1.0 commands */
518 /* -------------------------------------------------------------------------- */
520 #define HIDPP_SET_REGISTER 0x80
521 #define HIDPP_GET_REGISTER 0x81
522 #define HIDPP_SET_LONG_REGISTER 0x82
523 #define HIDPP_GET_LONG_REGISTER 0x83
526 * hidpp10_set_register - Modify a HID++ 1.0 register.
527 * @hidpp_dev: the device to set the register on.
528 * @register_address: the address of the register to modify.
529 * @byte: the byte of the register to modify. Should be less than 3.
530 * @mask: mask of the bits to modify
531 * @value: new values for the bits in mask
532 * Return: 0 if successful, otherwise a negative error code.
534 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
535 u8 register_address, u8 byte, u8 mask, u8 value)
537 struct hidpp_report response;
539 u8 params[3] = { 0 };
541 ret = hidpp_send_rap_command_sync(hidpp_dev,
542 REPORT_ID_HIDPP_SHORT,
549 memcpy(params, response.rap.params, 3);
551 params[byte] &= ~mask;
552 params[byte] |= value & mask;
554 return hidpp_send_rap_command_sync(hidpp_dev,
555 REPORT_ID_HIDPP_SHORT,
558 params, 3, &response);
561 #define HIDPP_REG_ENABLE_REPORTS 0x00
562 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
563 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
564 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
565 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
566 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
568 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
570 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
571 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
574 #define HIDPP_REG_FEATURES 0x01
575 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
576 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
578 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
579 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
581 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
582 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
585 #define HIDPP_REG_BATTERY_STATUS 0x07
587 static int hidpp10_battery_status_map_level(u8 param)
593 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
596 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
599 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
602 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
605 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
611 static int hidpp10_battery_status_map_status(u8 param)
617 /* discharging (in use) */
618 status = POWER_SUPPLY_STATUS_DISCHARGING;
620 case 0x21: /* (standard) charging */
621 case 0x24: /* fast charging */
622 case 0x25: /* slow charging */
623 status = POWER_SUPPLY_STATUS_CHARGING;
625 case 0x26: /* topping charge */
626 case 0x22: /* charge complete */
627 status = POWER_SUPPLY_STATUS_FULL;
629 case 0x20: /* unknown */
630 status = POWER_SUPPLY_STATUS_UNKNOWN;
633 * 0x01...0x1F = reserved (not charging)
634 * 0x23 = charging error
635 * 0x27..0xff = reserved
638 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
645 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
647 struct hidpp_report response;
650 ret = hidpp_send_rap_command_sync(hidpp,
651 REPORT_ID_HIDPP_SHORT,
653 HIDPP_REG_BATTERY_STATUS,
658 hidpp->battery.level =
659 hidpp10_battery_status_map_level(response.rap.params[0]);
660 status = hidpp10_battery_status_map_status(response.rap.params[1]);
661 hidpp->battery.status = status;
662 /* the capacity is only available when discharging or full */
663 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
664 status == POWER_SUPPLY_STATUS_FULL;
669 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
671 static int hidpp10_battery_mileage_map_status(u8 param)
675 switch (param >> 6) {
677 /* discharging (in use) */
678 status = POWER_SUPPLY_STATUS_DISCHARGING;
680 case 0x01: /* charging */
681 status = POWER_SUPPLY_STATUS_CHARGING;
683 case 0x02: /* charge complete */
684 status = POWER_SUPPLY_STATUS_FULL;
687 * 0x03 = charging error
690 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
697 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
699 struct hidpp_report response;
702 ret = hidpp_send_rap_command_sync(hidpp,
703 REPORT_ID_HIDPP_SHORT,
705 HIDPP_REG_BATTERY_MILEAGE,
710 hidpp->battery.capacity = response.rap.params[0];
711 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
712 hidpp->battery.status = status;
713 /* the capacity is only available when discharging or full */
714 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
715 status == POWER_SUPPLY_STATUS_FULL;
720 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
722 struct hidpp_report *report = (struct hidpp_report *)data;
723 int status, capacity, level;
726 if (report->report_id != REPORT_ID_HIDPP_SHORT)
729 switch (report->rap.sub_id) {
730 case HIDPP_REG_BATTERY_STATUS:
731 capacity = hidpp->battery.capacity;
732 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
733 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
735 case HIDPP_REG_BATTERY_MILEAGE:
736 capacity = report->rap.params[0];
737 level = hidpp->battery.level;
738 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
744 changed = capacity != hidpp->battery.capacity ||
745 level != hidpp->battery.level ||
746 status != hidpp->battery.status;
748 /* the capacity is only available when discharging or full */
749 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
750 status == POWER_SUPPLY_STATUS_FULL;
753 hidpp->battery.level = level;
754 hidpp->battery.status = status;
755 if (hidpp->battery.ps)
756 power_supply_changed(hidpp->battery.ps);
762 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
763 #define HIDPP_EXTENDED_PAIRING 0x30
764 #define HIDPP_DEVICE_NAME 0x40
766 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
768 struct hidpp_report response;
770 u8 params[1] = { HIDPP_DEVICE_NAME };
774 ret = hidpp_send_rap_command_sync(hidpp_dev,
775 REPORT_ID_HIDPP_SHORT,
776 HIDPP_GET_LONG_REGISTER,
777 HIDPP_REG_PAIRING_INFORMATION,
778 params, 1, &response);
782 len = response.rap.params[1];
784 if (2 + len > sizeof(response.rap.params))
787 if (len < 4) /* logitech devices are usually at least Xddd */
790 name = kzalloc(len + 1, GFP_KERNEL);
794 memcpy(name, &response.rap.params[2], len);
796 /* include the terminating '\0' */
797 hidpp_prefix_name(&name, len + 1);
802 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
804 struct hidpp_report response;
806 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
808 ret = hidpp_send_rap_command_sync(hidpp,
809 REPORT_ID_HIDPP_SHORT,
810 HIDPP_GET_LONG_REGISTER,
811 HIDPP_REG_PAIRING_INFORMATION,
812 params, 1, &response);
817 * We don't care about LE or BE, we will output it as a string
818 * with %4phD, so we need to keep the order.
820 *serial = *((u32 *)&response.rap.params[1]);
824 static int hidpp_unifying_init(struct hidpp_device *hidpp)
826 struct hid_device *hdev = hidpp->hid_dev;
831 ret = hidpp_unifying_get_serial(hidpp, &serial);
835 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
836 hdev->product, &serial);
837 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
839 name = hidpp_unifying_get_name(hidpp);
843 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
844 dbg_hid("HID++ Unifying: Got name: %s\n", name);
850 /* -------------------------------------------------------------------------- */
852 /* -------------------------------------------------------------------------- */
854 #define HIDPP_PAGE_ROOT 0x0000
855 #define HIDPP_PAGE_ROOT_IDX 0x00
857 #define CMD_ROOT_GET_FEATURE 0x01
858 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
860 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
861 u8 *feature_index, u8 *feature_type)
863 struct hidpp_report response;
865 u8 params[2] = { feature >> 8, feature & 0x00FF };
867 ret = hidpp_send_fap_command_sync(hidpp,
869 CMD_ROOT_GET_FEATURE,
870 params, 2, &response);
874 if (response.fap.params[0] == 0)
877 *feature_index = response.fap.params[0];
878 *feature_type = response.fap.params[1];
883 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
885 const u8 ping_byte = 0x5a;
886 u8 ping_data[3] = { 0, 0, ping_byte };
887 struct hidpp_report response;
890 ret = hidpp_send_rap_command_sync(hidpp,
891 REPORT_ID_HIDPP_SHORT,
893 CMD_ROOT_GET_PROTOCOL_VERSION,
894 ping_data, sizeof(ping_data), &response);
896 if (ret == HIDPP_ERROR_INVALID_SUBID) {
897 hidpp->protocol_major = 1;
898 hidpp->protocol_minor = 0;
902 /* the device might not be connected */
903 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
907 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
914 if (response.rap.params[2] != ping_byte) {
915 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
916 __func__, response.rap.params[2], ping_byte);
920 hidpp->protocol_major = response.rap.params[0];
921 hidpp->protocol_minor = response.rap.params[1];
924 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
925 hidpp->protocol_major, hidpp->protocol_minor);
929 /* -------------------------------------------------------------------------- */
930 /* 0x0005: GetDeviceNameType */
931 /* -------------------------------------------------------------------------- */
933 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
935 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
936 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
937 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
939 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
940 u8 feature_index, u8 *nameLength)
942 struct hidpp_report response;
945 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
946 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
949 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
956 *nameLength = response.fap.params[0];
961 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
962 u8 feature_index, u8 char_index, char *device_name, int len_buf)
964 struct hidpp_report response;
968 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
969 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
973 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
980 switch (response.report_id) {
981 case REPORT_ID_HIDPP_VERY_LONG:
982 count = hidpp->very_long_report_length - 4;
984 case REPORT_ID_HIDPP_LONG:
985 count = HIDPP_REPORT_LONG_LENGTH - 4;
987 case REPORT_ID_HIDPP_SHORT:
988 count = HIDPP_REPORT_SHORT_LENGTH - 4;
997 for (i = 0; i < count; i++)
998 device_name[i] = response.fap.params[i];
1003 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1012 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1013 &feature_index, &feature_type);
1017 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1022 name = kzalloc(__name_length + 1, GFP_KERNEL);
1026 while (index < __name_length) {
1027 ret = hidpp_devicenametype_get_device_name(hidpp,
1028 feature_index, index, name + index,
1029 __name_length - index);
1037 /* include the terminating '\0' */
1038 hidpp_prefix_name(&name, __name_length + 1);
1043 /* -------------------------------------------------------------------------- */
1044 /* 0x1000: Battery level status */
1045 /* -------------------------------------------------------------------------- */
1047 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1049 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1050 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1052 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1054 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1055 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1056 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1058 static int hidpp_map_battery_level(int capacity)
1061 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1063 * The spec says this should be < 31 but some devices report 30
1064 * with brand new batteries and Windows reports 30 as "Good".
1066 else if (capacity < 30)
1067 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1068 else if (capacity < 81)
1069 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1070 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1073 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1079 *capacity = data[0];
1080 *next_capacity = data[1];
1081 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1083 /* When discharging, we can rely on the device reported capacity.
1084 * For all other states the device reports 0 (unknown).
1087 case 0: /* discharging (in use) */
1088 status = POWER_SUPPLY_STATUS_DISCHARGING;
1089 *level = hidpp_map_battery_level(*capacity);
1091 case 1: /* recharging */
1092 status = POWER_SUPPLY_STATUS_CHARGING;
1094 case 2: /* charge in final stage */
1095 status = POWER_SUPPLY_STATUS_CHARGING;
1097 case 3: /* charge complete */
1098 status = POWER_SUPPLY_STATUS_FULL;
1099 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1102 case 4: /* recharging below optimal speed */
1103 status = POWER_SUPPLY_STATUS_CHARGING;
1105 /* 5 = invalid battery type
1107 7 = other charging error */
1109 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1116 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1123 struct hidpp_report response;
1125 u8 *params = (u8 *)response.fap.params;
1127 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1128 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1129 NULL, 0, &response);
1130 /* Ignore these intermittent errors */
1131 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1134 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1141 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1148 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1151 struct hidpp_report response;
1153 u8 *params = (u8 *)response.fap.params;
1154 unsigned int level_count, flags;
1156 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1157 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1158 NULL, 0, &response);
1160 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1167 level_count = params[0];
1170 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1171 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1173 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1178 static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1182 int status, capacity, next_capacity, level;
1184 if (hidpp->battery.feature_index == 0xff) {
1185 ret = hidpp_root_get_feature(hidpp,
1186 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1187 &hidpp->battery.feature_index,
1193 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1194 hidpp->battery.feature_index,
1196 &next_capacity, &level);
1200 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1201 hidpp->battery.feature_index);
1205 hidpp->battery.status = status;
1206 hidpp->battery.capacity = capacity;
1207 hidpp->battery.level = level;
1208 /* the capacity is only available when discharging or full */
1209 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1210 status == POWER_SUPPLY_STATUS_FULL;
1215 static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1218 struct hidpp_report *report = (struct hidpp_report *)data;
1219 int status, capacity, next_capacity, level;
1222 if (report->fap.feature_index != hidpp->battery.feature_index ||
1223 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1226 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1231 /* the capacity is only available when discharging or full */
1232 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1233 status == POWER_SUPPLY_STATUS_FULL;
1235 changed = capacity != hidpp->battery.capacity ||
1236 level != hidpp->battery.level ||
1237 status != hidpp->battery.status;
1240 hidpp->battery.level = level;
1241 hidpp->battery.capacity = capacity;
1242 hidpp->battery.status = status;
1243 if (hidpp->battery.ps)
1244 power_supply_changed(hidpp->battery.ps);
1250 /* -------------------------------------------------------------------------- */
1251 /* 0x1001: Battery voltage */
1252 /* -------------------------------------------------------------------------- */
1254 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1256 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1258 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1260 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1261 int *level, int *charge_type)
1265 long flags = (long) data[2];
1266 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1269 switch (flags & 0x07) {
1271 status = POWER_SUPPLY_STATUS_CHARGING;
1274 status = POWER_SUPPLY_STATUS_FULL;
1275 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1278 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1281 status = POWER_SUPPLY_STATUS_UNKNOWN;
1285 status = POWER_SUPPLY_STATUS_DISCHARGING;
1287 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1288 if (test_bit(3, &flags)) {
1289 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1291 if (test_bit(4, &flags)) {
1292 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1294 if (test_bit(5, &flags)) {
1295 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1298 *voltage = get_unaligned_be16(data);
1303 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1305 int *status, int *voltage,
1306 int *level, int *charge_type)
1308 struct hidpp_report response;
1310 u8 *params = (u8 *)response.fap.params;
1312 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1313 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1314 NULL, 0, &response);
1317 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1324 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1326 *status = hidpp20_battery_map_status_voltage(params, voltage,
1327 level, charge_type);
1332 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1336 int status, voltage, level, charge_type;
1338 if (hidpp->battery.voltage_feature_index == 0xff) {
1339 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1340 &hidpp->battery.voltage_feature_index,
1346 ret = hidpp20_battery_get_battery_voltage(hidpp,
1347 hidpp->battery.voltage_feature_index,
1348 &status, &voltage, &level, &charge_type);
1353 hidpp->battery.status = status;
1354 hidpp->battery.voltage = voltage;
1355 hidpp->battery.level = level;
1356 hidpp->battery.charge_type = charge_type;
1357 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1362 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1365 struct hidpp_report *report = (struct hidpp_report *)data;
1366 int status, voltage, level, charge_type;
1368 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1369 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1372 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1373 &level, &charge_type);
1375 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1377 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1378 hidpp->battery.voltage = voltage;
1379 hidpp->battery.status = status;
1380 hidpp->battery.level = level;
1381 hidpp->battery.charge_type = charge_type;
1382 if (hidpp->battery.ps)
1383 power_supply_changed(hidpp->battery.ps);
1388 /* -------------------------------------------------------------------------- */
1389 /* 0x1004: Unified battery */
1390 /* -------------------------------------------------------------------------- */
1392 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1394 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1395 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1397 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1399 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1400 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1401 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1402 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1404 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1405 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1407 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1410 struct hidpp_report response;
1412 u8 *params = (u8 *)response.fap.params;
1414 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1415 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1416 /* we have already set the device capabilities, so let's skip */
1420 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1421 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1422 NULL, 0, &response);
1423 /* Ignore these intermittent errors */
1424 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1427 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1435 * If the device supports state of charge (battery percentage) we won't
1436 * export the battery level information. there are 4 possible battery
1437 * levels and they all are optional, this means that the device might
1438 * not support any of them, we are just better off with the battery
1441 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1442 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1443 hidpp->battery.supported_levels_1004 = 0;
1445 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1446 hidpp->battery.supported_levels_1004 = params[0];
1452 static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1454 u8 external_power_status)
1458 switch (charging_status) {
1459 case 0: /* discharging */
1460 status = POWER_SUPPLY_STATUS_DISCHARGING;
1462 case 1: /* charging */
1463 case 2: /* charging slow */
1464 status = POWER_SUPPLY_STATUS_CHARGING;
1466 case 3: /* complete */
1467 status = POWER_SUPPLY_STATUS_FULL;
1470 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1471 hid_info(hidpp->hid_dev, "%s: charging error",
1475 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1482 static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1485 /* cler unsupported level bits */
1486 battery_level &= hidpp->battery.supported_levels_1004;
1488 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1489 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1490 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1491 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1492 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1493 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1494 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1495 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1497 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1500 static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1502 u8 *state_of_charge,
1506 struct hidpp_report response;
1508 u8 *params = (u8 *)response.fap.params;
1510 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1511 CMD_UNIFIED_BATTERY_GET_STATUS,
1512 NULL, 0, &response);
1513 /* Ignore these intermittent errors */
1514 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1517 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1524 *state_of_charge = params[0];
1525 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1526 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1531 static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1538 if (hidpp->battery.feature_index == 0xff) {
1539 ret = hidpp_root_get_feature(hidpp,
1540 HIDPP_PAGE_UNIFIED_BATTERY,
1541 &hidpp->battery.feature_index,
1547 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1548 hidpp->battery.feature_index);
1552 ret = hidpp20_unifiedbattery_get_status(hidpp,
1553 hidpp->battery.feature_index,
1560 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1561 hidpp->battery.capacity = state_of_charge;
1562 hidpp->battery.status = status;
1563 hidpp->battery.level = level;
1564 hidpp->battery.online = true;
1569 static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1572 struct hidpp_report *report = (struct hidpp_report *)data;
1573 u8 *params = (u8 *)report->fap.params;
1574 int state_of_charge, status, level;
1577 if (report->fap.feature_index != hidpp->battery.feature_index ||
1578 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1581 state_of_charge = params[0];
1582 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1583 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1585 changed = status != hidpp->battery.status ||
1586 (state_of_charge != hidpp->battery.capacity &&
1587 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1588 (level != hidpp->battery.level &&
1589 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1592 hidpp->battery.capacity = state_of_charge;
1593 hidpp->battery.status = status;
1594 hidpp->battery.level = level;
1595 if (hidpp->battery.ps)
1596 power_supply_changed(hidpp->battery.ps);
1602 /* -------------------------------------------------------------------------- */
1603 /* Battery feature helpers */
1604 /* -------------------------------------------------------------------------- */
1606 static enum power_supply_property hidpp_battery_props[] = {
1607 POWER_SUPPLY_PROP_ONLINE,
1608 POWER_SUPPLY_PROP_STATUS,
1609 POWER_SUPPLY_PROP_SCOPE,
1610 POWER_SUPPLY_PROP_MODEL_NAME,
1611 POWER_SUPPLY_PROP_MANUFACTURER,
1612 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1613 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1614 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1615 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1618 static int hidpp_battery_get_property(struct power_supply *psy,
1619 enum power_supply_property psp,
1620 union power_supply_propval *val)
1622 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1626 case POWER_SUPPLY_PROP_STATUS:
1627 val->intval = hidpp->battery.status;
1629 case POWER_SUPPLY_PROP_CAPACITY:
1630 val->intval = hidpp->battery.capacity;
1632 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1633 val->intval = hidpp->battery.level;
1635 case POWER_SUPPLY_PROP_SCOPE:
1636 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1638 case POWER_SUPPLY_PROP_ONLINE:
1639 val->intval = hidpp->battery.online;
1641 case POWER_SUPPLY_PROP_MODEL_NAME:
1642 if (!strncmp(hidpp->name, "Logitech ", 9))
1643 val->strval = hidpp->name + 9;
1645 val->strval = hidpp->name;
1647 case POWER_SUPPLY_PROP_MANUFACTURER:
1648 val->strval = "Logitech";
1650 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1651 val->strval = hidpp->hid_dev->uniq;
1653 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1654 /* hardware reports voltage in in mV. sysfs expects uV */
1655 val->intval = hidpp->battery.voltage * 1000;
1657 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1658 val->intval = hidpp->battery.charge_type;
1668 /* -------------------------------------------------------------------------- */
1669 /* 0x1d4b: Wireless device status */
1670 /* -------------------------------------------------------------------------- */
1671 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1673 static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1678 ret = hidpp_root_get_feature(hidpp,
1679 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1680 &hidpp->wireless_feature_index,
1686 /* -------------------------------------------------------------------------- */
1687 /* 0x2120: Hi-resolution scrolling */
1688 /* -------------------------------------------------------------------------- */
1690 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1692 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1694 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1695 bool enabled, u8 *multiplier)
1701 struct hidpp_report response;
1703 ret = hidpp_root_get_feature(hidpp,
1704 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1710 params[0] = enabled ? BIT(0) : 0;
1711 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1712 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1713 params, sizeof(params), &response);
1716 *multiplier = response.fap.params[1];
1720 /* -------------------------------------------------------------------------- */
1721 /* 0x2121: HiRes Wheel */
1722 /* -------------------------------------------------------------------------- */
1724 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1726 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1727 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1729 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1735 struct hidpp_report response;
1737 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1738 &feature_index, &feature_type);
1740 goto return_default;
1742 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1743 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1744 NULL, 0, &response);
1746 goto return_default;
1748 *multiplier = response.fap.params[0];
1751 hid_warn(hidpp->hid_dev,
1752 "Couldn't get wheel multiplier (error %d)\n", ret);
1756 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1757 bool high_resolution, bool use_hidpp)
1763 struct hidpp_report response;
1765 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1766 &feature_index, &feature_type);
1770 params[0] = (invert ? BIT(2) : 0) |
1771 (high_resolution ? BIT(1) : 0) |
1772 (use_hidpp ? BIT(0) : 0);
1774 return hidpp_send_fap_command_sync(hidpp, feature_index,
1775 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1776 params, sizeof(params), &response);
1779 /* -------------------------------------------------------------------------- */
1780 /* 0x4301: Solar Keyboard */
1781 /* -------------------------------------------------------------------------- */
1783 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1785 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1787 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1788 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1789 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1791 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1793 struct hidpp_report response;
1794 u8 params[2] = { 1, 1 };
1798 if (hidpp->battery.feature_index == 0xff) {
1799 ret = hidpp_root_get_feature(hidpp,
1800 HIDPP_PAGE_SOLAR_KEYBOARD,
1801 &hidpp->battery.solar_feature_index,
1807 ret = hidpp_send_fap_command_sync(hidpp,
1808 hidpp->battery.solar_feature_index,
1809 CMD_SOLAR_SET_LIGHT_MEASURE,
1810 params, 2, &response);
1812 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1819 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1824 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1827 struct hidpp_report *report = (struct hidpp_report *)data;
1828 int capacity, lux, status;
1831 function = report->fap.funcindex_clientid;
1834 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1835 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1836 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1837 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1840 capacity = report->fap.params[0];
1843 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1844 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1846 status = POWER_SUPPLY_STATUS_CHARGING;
1848 status = POWER_SUPPLY_STATUS_DISCHARGING;
1850 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1852 if (capacity < hidpp->battery.capacity)
1853 status = POWER_SUPPLY_STATUS_DISCHARGING;
1855 status = POWER_SUPPLY_STATUS_CHARGING;
1859 if (capacity == 100)
1860 status = POWER_SUPPLY_STATUS_FULL;
1862 hidpp->battery.online = true;
1863 if (capacity != hidpp->battery.capacity ||
1864 status != hidpp->battery.status) {
1865 hidpp->battery.capacity = capacity;
1866 hidpp->battery.status = status;
1867 if (hidpp->battery.ps)
1868 power_supply_changed(hidpp->battery.ps);
1874 /* -------------------------------------------------------------------------- */
1875 /* 0x6010: Touchpad FW items */
1876 /* -------------------------------------------------------------------------- */
1878 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1880 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1882 struct hidpp_touchpad_fw_items {
1884 uint8_t desired_state;
1890 * send a set state command to the device by reading the current items->state
1891 * field. items is then filled with the current state.
1893 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1895 struct hidpp_touchpad_fw_items *items)
1897 struct hidpp_report response;
1899 u8 *params = (u8 *)response.fap.params;
1901 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1902 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1905 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1912 items->presence = params[0];
1913 items->desired_state = params[1];
1914 items->state = params[2];
1915 items->persistent = params[3];
1920 /* -------------------------------------------------------------------------- */
1921 /* 0x6100: TouchPadRawXY */
1922 /* -------------------------------------------------------------------------- */
1924 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1926 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1927 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1929 #define EVENT_TOUCHPAD_RAW_XY 0x00
1931 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1932 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1934 struct hidpp_touchpad_raw_info {
1945 struct hidpp_touchpad_raw_xy_finger {
1955 struct hidpp_touchpad_raw_xy {
1957 struct hidpp_touchpad_raw_xy_finger fingers[2];
1964 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1965 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1967 struct hidpp_report response;
1969 u8 *params = (u8 *)response.fap.params;
1971 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1972 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1975 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1982 raw_info->x_size = get_unaligned_be16(¶ms[0]);
1983 raw_info->y_size = get_unaligned_be16(¶ms[2]);
1984 raw_info->z_range = params[4];
1985 raw_info->area_range = params[5];
1986 raw_info->maxcontacts = params[7];
1987 raw_info->origin = params[8];
1988 /* res is given in unit per inch */
1989 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
1994 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1995 u8 feature_index, bool send_raw_reports,
1996 bool sensor_enhanced_settings)
1998 struct hidpp_report response;
2002 * bit 0 - enable raw
2003 * bit 1 - 16bit Z, no area
2004 * bit 2 - enhanced sensitivity
2005 * bit 3 - width, height (4 bits each) instead of area
2006 * bit 4 - send raw + gestures (degrades smoothness)
2007 * remaining bits - reserved
2009 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2011 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2012 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
2015 static void hidpp_touchpad_touch_event(u8 *data,
2016 struct hidpp_touchpad_raw_xy_finger *finger)
2018 u8 x_m = data[0] << 2;
2019 u8 y_m = data[2] << 2;
2021 finger->x = x_m << 6 | data[1];
2022 finger->y = y_m << 6 | data[3];
2024 finger->contact_type = data[0] >> 6;
2025 finger->contact_status = data[2] >> 6;
2027 finger->z = data[4];
2028 finger->area = data[5];
2029 finger->finger_id = data[6] >> 4;
2032 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2033 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2035 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2036 raw_xy->end_of_frame = data[8] & 0x01;
2037 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2038 raw_xy->finger_count = data[15] & 0x0f;
2039 raw_xy->button = (data[8] >> 2) & 0x01;
2041 if (raw_xy->finger_count) {
2042 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2043 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2047 /* -------------------------------------------------------------------------- */
2048 /* 0x8123: Force feedback support */
2049 /* -------------------------------------------------------------------------- */
2051 #define HIDPP_FF_GET_INFO 0x01
2052 #define HIDPP_FF_RESET_ALL 0x11
2053 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2054 #define HIDPP_FF_SET_EFFECT_STATE 0x31
2055 #define HIDPP_FF_DESTROY_EFFECT 0x41
2056 #define HIDPP_FF_GET_APERTURE 0x51
2057 #define HIDPP_FF_SET_APERTURE 0x61
2058 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2059 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2061 #define HIDPP_FF_EFFECT_STATE_GET 0x00
2062 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
2063 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2064 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2066 #define HIDPP_FF_EFFECT_CONSTANT 0x00
2067 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2068 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2069 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2070 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2071 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2072 #define HIDPP_FF_EFFECT_SPRING 0x06
2073 #define HIDPP_FF_EFFECT_DAMPER 0x07
2074 #define HIDPP_FF_EFFECT_FRICTION 0x08
2075 #define HIDPP_FF_EFFECT_INERTIA 0x09
2076 #define HIDPP_FF_EFFECT_RAMP 0x0A
2078 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
2080 #define HIDPP_FF_EFFECTID_NONE -1
2081 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
2082 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2084 #define HIDPP_FF_MAX_PARAMS 20
2085 #define HIDPP_FF_RESERVED_SLOTS 1
2087 struct hidpp_ff_private_data {
2088 struct hidpp_device *hidpp;
2096 struct workqueue_struct *wq;
2097 atomic_t workqueue_size;
2100 struct hidpp_ff_work_data {
2101 struct work_struct work;
2102 struct hidpp_ff_private_data *data;
2105 u8 params[HIDPP_FF_MAX_PARAMS];
2109 static const signed short hidpp_ff_effects[] = {
2124 static const signed short hidpp_ff_effects_v2[] = {
2131 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2132 HIDPP_FF_EFFECT_SPRING,
2133 HIDPP_FF_EFFECT_FRICTION,
2134 HIDPP_FF_EFFECT_DAMPER,
2135 HIDPP_FF_EFFECT_INERTIA
2138 static const char *HIDPP_FF_CONDITION_NAMES[] = {
2146 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2150 for (i = 0; i < data->num_effects; i++)
2151 if (data->effect_ids[i] == effect_id)
2157 static void hidpp_ff_work_handler(struct work_struct *w)
2159 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2160 struct hidpp_ff_private_data *data = wd->data;
2161 struct hidpp_report response;
2165 /* add slot number if needed */
2166 switch (wd->effect_id) {
2167 case HIDPP_FF_EFFECTID_AUTOCENTER:
2168 wd->params[0] = data->slot_autocenter;
2170 case HIDPP_FF_EFFECTID_NONE:
2171 /* leave slot as zero */
2174 /* find current slot for effect */
2175 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2179 /* send command and wait for reply */
2180 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2181 wd->command, wd->params, wd->size, &response);
2184 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2188 /* parse return data */
2189 switch (wd->command) {
2190 case HIDPP_FF_DOWNLOAD_EFFECT:
2191 slot = response.fap.params[0];
2192 if (slot > 0 && slot <= data->num_effects) {
2193 if (wd->effect_id >= 0)
2194 /* regular effect uploaded */
2195 data->effect_ids[slot-1] = wd->effect_id;
2196 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2197 /* autocenter spring uploaded */
2198 data->slot_autocenter = slot;
2201 case HIDPP_FF_DESTROY_EFFECT:
2202 if (wd->effect_id >= 0)
2203 /* regular effect destroyed */
2204 data->effect_ids[wd->params[0]-1] = -1;
2205 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2206 /* autocenter spring destoyed */
2207 data->slot_autocenter = 0;
2209 case HIDPP_FF_SET_GLOBAL_GAINS:
2210 data->gain = (wd->params[0] << 8) + wd->params[1];
2212 case HIDPP_FF_SET_APERTURE:
2213 data->range = (wd->params[0] << 8) + wd->params[1];
2216 /* no action needed */
2221 atomic_dec(&data->workqueue_size);
2225 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2227 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2233 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2236 wd->effect_id = effect_id;
2237 wd->command = command;
2239 memcpy(wd->params, params, size);
2241 atomic_inc(&data->workqueue_size);
2242 queue_work(data->wq, &wd->work);
2244 /* warn about excessive queue size */
2245 s = atomic_read(&data->workqueue_size);
2246 if (s >= 20 && s % 20 == 0)
2247 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2252 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2254 struct hidpp_ff_private_data *data = dev->ff->private;
2259 /* set common parameters */
2260 params[2] = effect->replay.length >> 8;
2261 params[3] = effect->replay.length & 255;
2262 params[4] = effect->replay.delay >> 8;
2263 params[5] = effect->replay.delay & 255;
2265 switch (effect->type) {
2267 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2268 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2269 params[6] = force >> 8;
2270 params[7] = force & 255;
2271 params[8] = effect->u.constant.envelope.attack_level >> 7;
2272 params[9] = effect->u.constant.envelope.attack_length >> 8;
2273 params[10] = effect->u.constant.envelope.attack_length & 255;
2274 params[11] = effect->u.constant.envelope.fade_level >> 7;
2275 params[12] = effect->u.constant.envelope.fade_length >> 8;
2276 params[13] = effect->u.constant.envelope.fade_length & 255;
2278 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2279 effect->u.constant.level,
2280 effect->direction, force);
2281 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2282 effect->u.constant.envelope.attack_level,
2283 effect->u.constant.envelope.attack_length,
2284 effect->u.constant.envelope.fade_level,
2285 effect->u.constant.envelope.fade_length);
2289 switch (effect->u.periodic.waveform) {
2291 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2294 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2297 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2300 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2303 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2306 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2309 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2310 params[6] = effect->u.periodic.magnitude >> 8;
2311 params[7] = effect->u.periodic.magnitude & 255;
2312 params[8] = effect->u.periodic.offset >> 8;
2313 params[9] = effect->u.periodic.offset & 255;
2314 params[10] = effect->u.periodic.period >> 8;
2315 params[11] = effect->u.periodic.period & 255;
2316 params[12] = effect->u.periodic.phase >> 8;
2317 params[13] = effect->u.periodic.phase & 255;
2318 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2319 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2320 params[16] = effect->u.periodic.envelope.attack_length & 255;
2321 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2322 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2323 params[19] = effect->u.periodic.envelope.fade_length & 255;
2325 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2326 effect->u.periodic.magnitude, effect->direction,
2327 effect->u.periodic.offset,
2328 effect->u.periodic.period,
2329 effect->u.periodic.phase);
2330 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2331 effect->u.periodic.envelope.attack_level,
2332 effect->u.periodic.envelope.attack_length,
2333 effect->u.periodic.envelope.fade_level,
2334 effect->u.periodic.envelope.fade_length);
2338 params[1] = HIDPP_FF_EFFECT_RAMP;
2339 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2340 params[6] = force >> 8;
2341 params[7] = force & 255;
2342 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2343 params[8] = force >> 8;
2344 params[9] = force & 255;
2345 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2346 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2347 params[12] = effect->u.ramp.envelope.attack_length & 255;
2348 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2349 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2350 params[15] = effect->u.ramp.envelope.fade_length & 255;
2352 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2353 effect->u.ramp.start_level,
2354 effect->u.ramp.end_level,
2355 effect->direction, force);
2356 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2357 effect->u.ramp.envelope.attack_level,
2358 effect->u.ramp.envelope.attack_length,
2359 effect->u.ramp.envelope.fade_level,
2360 effect->u.ramp.envelope.fade_length);
2366 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2367 params[6] = effect->u.condition[0].left_saturation >> 9;
2368 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2369 params[8] = effect->u.condition[0].left_coeff >> 8;
2370 params[9] = effect->u.condition[0].left_coeff & 255;
2371 params[10] = effect->u.condition[0].deadband >> 9;
2372 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2373 params[12] = effect->u.condition[0].center >> 8;
2374 params[13] = effect->u.condition[0].center & 255;
2375 params[14] = effect->u.condition[0].right_coeff >> 8;
2376 params[15] = effect->u.condition[0].right_coeff & 255;
2377 params[16] = effect->u.condition[0].right_saturation >> 9;
2378 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2380 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2381 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2382 effect->u.condition[0].left_coeff,
2383 effect->u.condition[0].left_saturation,
2384 effect->u.condition[0].right_coeff,
2385 effect->u.condition[0].right_saturation);
2386 dbg_hid(" deadband=%d, center=%d\n",
2387 effect->u.condition[0].deadband,
2388 effect->u.condition[0].center);
2391 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2395 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2398 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2400 struct hidpp_ff_private_data *data = dev->ff->private;
2403 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2405 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2407 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2410 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2412 struct hidpp_ff_private_data *data = dev->ff->private;
2415 dbg_hid("Erasing effect %d.\n", effect_id);
2417 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2420 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2422 struct hidpp_ff_private_data *data = dev->ff->private;
2423 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2425 dbg_hid("Setting autocenter to %d.\n", magnitude);
2427 /* start a standard spring effect */
2428 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2429 /* zero delay and duration */
2430 params[2] = params[3] = params[4] = params[5] = 0;
2431 /* set coeff to 25% of saturation */
2432 params[8] = params[14] = magnitude >> 11;
2433 params[9] = params[15] = (magnitude >> 3) & 255;
2434 params[6] = params[16] = magnitude >> 9;
2435 params[7] = params[17] = (magnitude >> 1) & 255;
2436 /* zero deadband and center */
2437 params[10] = params[11] = params[12] = params[13] = 0;
2439 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2442 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2444 struct hidpp_ff_private_data *data = dev->ff->private;
2447 dbg_hid("Setting gain to %d.\n", gain);
2449 params[0] = gain >> 8;
2450 params[1] = gain & 255;
2451 params[2] = 0; /* no boost */
2454 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2457 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2459 struct hid_device *hid = to_hid_device(dev);
2460 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2461 struct input_dev *idev = hidinput->input;
2462 struct hidpp_ff_private_data *data = idev->ff->private;
2464 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2467 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2469 struct hid_device *hid = to_hid_device(dev);
2470 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2471 struct input_dev *idev = hidinput->input;
2472 struct hidpp_ff_private_data *data = idev->ff->private;
2474 int range = simple_strtoul(buf, NULL, 10);
2476 range = clamp(range, 180, 900);
2478 params[0] = range >> 8;
2479 params[1] = range & 0x00FF;
2481 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2486 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2488 static void hidpp_ff_destroy(struct ff_device *ff)
2490 struct hidpp_ff_private_data *data = ff->private;
2491 struct hid_device *hid = data->hidpp->hid_dev;
2493 hid_info(hid, "Unloading HID++ force feedback.\n");
2495 device_remove_file(&hid->dev, &dev_attr_range);
2496 destroy_workqueue(data->wq);
2497 kfree(data->effect_ids);
2500 static int hidpp_ff_init(struct hidpp_device *hidpp,
2501 struct hidpp_ff_private_data *data)
2503 struct hid_device *hid = hidpp->hid_dev;
2504 struct hid_input *hidinput;
2505 struct input_dev *dev;
2506 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2507 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2508 struct ff_device *ff;
2509 int error, j, num_slots = data->num_effects;
2512 if (list_empty(&hid->inputs)) {
2513 hid_err(hid, "no inputs found\n");
2516 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2517 dev = hidinput->input;
2520 hid_err(hid, "Struct input_dev not set!\n");
2524 /* Get firmware release */
2525 version = bcdDevice & 255;
2527 /* Set supported force feedback capabilities */
2528 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2529 set_bit(hidpp_ff_effects[j], dev->ffbit);
2531 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2532 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2534 error = input_ff_create(dev, num_slots);
2537 hid_err(dev, "Failed to create FF device!\n");
2541 * Create a copy of passed data, so we can transfer memory
2542 * ownership to FF core
2544 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2547 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2548 if (!data->effect_ids) {
2552 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2554 kfree(data->effect_ids);
2559 data->hidpp = hidpp;
2560 data->version = version;
2561 for (j = 0; j < num_slots; j++)
2562 data->effect_ids[j] = -1;
2567 ff->upload = hidpp_ff_upload_effect;
2568 ff->erase = hidpp_ff_erase_effect;
2569 ff->playback = hidpp_ff_playback;
2570 ff->set_gain = hidpp_ff_set_gain;
2571 ff->set_autocenter = hidpp_ff_set_autocenter;
2572 ff->destroy = hidpp_ff_destroy;
2574 /* Create sysfs interface */
2575 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2577 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2579 /* init the hardware command queue */
2580 atomic_set(&data->workqueue_size, 0);
2582 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2588 /* ************************************************************************** */
2590 /* Device Support */
2592 /* ************************************************************************** */
2594 /* -------------------------------------------------------------------------- */
2595 /* Touchpad HID++ devices */
2596 /* -------------------------------------------------------------------------- */
2598 #define WTP_MANUAL_RESOLUTION 39
2603 u8 mt_feature_index;
2604 u8 button_feature_index;
2607 unsigned int resolution;
2610 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2611 struct hid_field *field, struct hid_usage *usage,
2612 unsigned long **bit, int *max)
2617 static void wtp_populate_input(struct hidpp_device *hidpp,
2618 struct input_dev *input_dev)
2620 struct wtp_data *wd = hidpp->private_data;
2622 __set_bit(EV_ABS, input_dev->evbit);
2623 __set_bit(EV_KEY, input_dev->evbit);
2624 __clear_bit(EV_REL, input_dev->evbit);
2625 __clear_bit(EV_LED, input_dev->evbit);
2627 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2628 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2629 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2630 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2632 /* Max pressure is not given by the devices, pick one */
2633 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2635 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2637 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2638 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2640 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2642 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2643 INPUT_MT_DROP_UNUSED);
2646 static void wtp_touch_event(struct hidpp_device *hidpp,
2647 struct hidpp_touchpad_raw_xy_finger *touch_report)
2649 struct wtp_data *wd = hidpp->private_data;
2652 if (!touch_report->finger_id || touch_report->contact_type)
2653 /* no actual data */
2656 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2658 input_mt_slot(hidpp->input, slot);
2659 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2660 touch_report->contact_status);
2661 if (touch_report->contact_status) {
2662 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2664 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2665 wd->flip_y ? wd->y_size - touch_report->y :
2667 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2668 touch_report->area);
2672 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2673 struct hidpp_touchpad_raw_xy *raw)
2677 for (i = 0; i < 2; i++)
2678 wtp_touch_event(hidpp, &(raw->fingers[i]));
2680 if (raw->end_of_frame &&
2681 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2682 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2684 if (raw->end_of_frame || raw->finger_count <= 2) {
2685 input_mt_sync_frame(hidpp->input);
2686 input_sync(hidpp->input);
2690 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2692 struct wtp_data *wd = hidpp->private_data;
2693 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2694 (data[7] >> 4) * (data[7] >> 4)) / 2;
2695 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2696 (data[13] >> 4) * (data[13] >> 4)) / 2;
2697 struct hidpp_touchpad_raw_xy raw = {
2698 .timestamp = data[1],
2702 .contact_status = !!data[7],
2703 .x = get_unaligned_le16(&data[3]),
2704 .y = get_unaligned_le16(&data[5]),
2707 .finger_id = data[2],
2710 .contact_status = !!data[13],
2711 .x = get_unaligned_le16(&data[9]),
2712 .y = get_unaligned_le16(&data[11]),
2715 .finger_id = data[8],
2718 .finger_count = wd->maxcontacts,
2720 .end_of_frame = (data[0] >> 7) == 0,
2721 .button = data[0] & 0x01,
2724 wtp_send_raw_xy_event(hidpp, &raw);
2729 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2731 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2732 struct wtp_data *wd = hidpp->private_data;
2733 struct hidpp_report *report = (struct hidpp_report *)data;
2734 struct hidpp_touchpad_raw_xy raw;
2736 if (!wd || !hidpp->input)
2742 hid_err(hdev, "Received HID report of bad size (%d)",
2746 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2747 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2748 !!(data[1] & 0x01));
2749 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2750 !!(data[1] & 0x02));
2751 input_sync(hidpp->input);
2756 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2758 case REPORT_ID_HIDPP_LONG:
2759 /* size is already checked in hidpp_raw_event. */
2760 if ((report->fap.feature_index != wd->mt_feature_index) ||
2761 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2763 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2765 wtp_send_raw_xy_event(hidpp, &raw);
2772 static int wtp_get_config(struct hidpp_device *hidpp)
2774 struct wtp_data *wd = hidpp->private_data;
2775 struct hidpp_touchpad_raw_info raw_info = {0};
2779 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2780 &wd->mt_feature_index, &feature_type);
2782 /* means that the device is not powered up */
2785 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2790 wd->x_size = raw_info.x_size;
2791 wd->y_size = raw_info.y_size;
2792 wd->maxcontacts = raw_info.maxcontacts;
2793 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2794 wd->resolution = raw_info.res;
2795 if (!wd->resolution)
2796 wd->resolution = WTP_MANUAL_RESOLUTION;
2801 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2803 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2804 struct wtp_data *wd;
2806 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2811 hidpp->private_data = wd;
2816 static int wtp_connect(struct hid_device *hdev, bool connected)
2818 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2819 struct wtp_data *wd = hidpp->private_data;
2823 ret = wtp_get_config(hidpp);
2825 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2830 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2834 /* ------------------------------------------------------------------------- */
2835 /* Logitech M560 devices */
2836 /* ------------------------------------------------------------------------- */
2839 * Logitech M560 protocol overview
2841 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2842 * the sides buttons are pressed, it sends some keyboard keys events
2843 * instead of buttons ones.
2844 * To complicate things further, the middle button keys sequence
2845 * is different from the odd press and the even press.
2847 * forward button -> Super_R
2848 * backward button -> Super_L+'d' (press only)
2849 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2850 * 2nd time: left-click (press only)
2851 * NB: press-only means that when the button is pressed, the
2852 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2853 * together sequentially; instead when the button is released, no event is
2857 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2858 * the mouse reacts differently:
2859 * - it never sends a keyboard key event
2860 * - for the three mouse button it sends:
2861 * middle button press 11<xx>0a 3500af00...
2862 * side 1 button (forward) press 11<xx>0a 3500b000...
2863 * side 2 button (backward) press 11<xx>0a 3500ae00...
2864 * middle/side1/side2 button release 11<xx>0a 35000000...
2867 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2869 /* how buttons are mapped in the report */
2870 #define M560_MOUSE_BTN_LEFT 0x01
2871 #define M560_MOUSE_BTN_RIGHT 0x02
2872 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2873 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2875 #define M560_SUB_ID 0x0a
2876 #define M560_BUTTON_MODE_REGISTER 0x35
2878 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2880 struct hidpp_report response;
2881 struct hidpp_device *hidpp_dev;
2883 hidpp_dev = hid_get_drvdata(hdev);
2885 return hidpp_send_rap_command_sync(
2887 REPORT_ID_HIDPP_SHORT,
2889 M560_BUTTON_MODE_REGISTER,
2890 (u8 *)m560_config_parameter,
2891 sizeof(m560_config_parameter),
2896 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2898 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2901 if (!hidpp->input) {
2902 hid_err(hdev, "error in parameter\n");
2907 hid_err(hdev, "error in report\n");
2911 if (data[0] == REPORT_ID_HIDPP_LONG &&
2912 data[2] == M560_SUB_ID && data[6] == 0x00) {
2914 * m560 mouse report for middle, forward and backward button
2917 * data[1] = device-id
2919 * data[5] = 0xaf -> middle
2922 * 0x00 -> release all
2928 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2931 input_report_key(hidpp->input, BTN_FORWARD, 1);
2934 input_report_key(hidpp->input, BTN_BACK, 1);
2937 input_report_key(hidpp->input, BTN_BACK, 0);
2938 input_report_key(hidpp->input, BTN_FORWARD, 0);
2939 input_report_key(hidpp->input, BTN_MIDDLE, 0);
2942 hid_err(hdev, "error in report\n");
2945 input_sync(hidpp->input);
2947 } else if (data[0] == 0x02) {
2949 * Logitech M560 mouse report
2951 * data[0] = type (0x02)
2952 * data[1..2] = buttons
2959 input_report_key(hidpp->input, BTN_LEFT,
2960 !!(data[1] & M560_MOUSE_BTN_LEFT));
2961 input_report_key(hidpp->input, BTN_RIGHT,
2962 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2964 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
2965 input_report_rel(hidpp->input, REL_HWHEEL, -1);
2966 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2968 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
2969 input_report_rel(hidpp->input, REL_HWHEEL, 1);
2970 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2974 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2975 input_report_rel(hidpp->input, REL_X, v);
2977 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2978 input_report_rel(hidpp->input, REL_Y, v);
2980 v = hid_snto32(data[6], 8);
2982 hidpp_scroll_counter_handle_scroll(hidpp->input,
2983 &hidpp->vertical_wheel_counter, v);
2985 input_sync(hidpp->input);
2991 static void m560_populate_input(struct hidpp_device *hidpp,
2992 struct input_dev *input_dev)
2994 __set_bit(EV_KEY, input_dev->evbit);
2995 __set_bit(BTN_MIDDLE, input_dev->keybit);
2996 __set_bit(BTN_RIGHT, input_dev->keybit);
2997 __set_bit(BTN_LEFT, input_dev->keybit);
2998 __set_bit(BTN_BACK, input_dev->keybit);
2999 __set_bit(BTN_FORWARD, input_dev->keybit);
3001 __set_bit(EV_REL, input_dev->evbit);
3002 __set_bit(REL_X, input_dev->relbit);
3003 __set_bit(REL_Y, input_dev->relbit);
3004 __set_bit(REL_WHEEL, input_dev->relbit);
3005 __set_bit(REL_HWHEEL, input_dev->relbit);
3006 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3007 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3010 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3011 struct hid_field *field, struct hid_usage *usage,
3012 unsigned long **bit, int *max)
3017 /* ------------------------------------------------------------------------- */
3018 /* Logitech K400 devices */
3019 /* ------------------------------------------------------------------------- */
3022 * The Logitech K400 keyboard has an embedded touchpad which is seen
3023 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3024 * tap-to-click but the setting is not remembered accross reset, annoying some
3027 * We can toggle this feature from the host by using the feature 0x6010:
3031 struct k400_private_data {
3035 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3037 struct k400_private_data *k400 = hidpp->private_data;
3038 struct hidpp_touchpad_fw_items items = {};
3042 if (!k400->feature_index) {
3043 ret = hidpp_root_get_feature(hidpp,
3044 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3045 &k400->feature_index, &feature_type);
3047 /* means that the device is not powered up */
3051 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3058 static int k400_allocate(struct hid_device *hdev)
3060 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3061 struct k400_private_data *k400;
3063 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3068 hidpp->private_data = k400;
3073 static int k400_connect(struct hid_device *hdev, bool connected)
3075 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3077 if (!disable_tap_to_click)
3080 return k400_disable_tap_to_click(hidpp);
3083 /* ------------------------------------------------------------------------- */
3084 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
3085 /* ------------------------------------------------------------------------- */
3087 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3089 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3090 struct hidpp_ff_private_data *data)
3092 struct hidpp_report response;
3093 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3094 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3098 /* initialize with zero autocenter to get wheel in usable state */
3100 dbg_hid("Setting autocenter to 0.\n");
3101 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3102 HIDPP_FF_DOWNLOAD_EFFECT,
3103 params, ARRAY_SIZE(params),
3106 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3108 data->slot_autocenter = response.fap.params[0];
3113 static int g920_get_config(struct hidpp_device *hidpp,
3114 struct hidpp_ff_private_data *data)
3116 struct hidpp_report response;
3120 memset(data, 0, sizeof(*data));
3122 /* Find feature and store for later use */
3123 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3124 &data->feature_index, &feature_type);
3128 /* Read number of slots available in device */
3129 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3136 hid_err(hidpp->hid_dev,
3137 "%s: received protocol error 0x%02x\n", __func__, ret);
3141 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3143 /* reset all forces */
3144 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3149 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3151 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3152 HIDPP_FF_GET_APERTURE,
3156 hid_warn(hidpp->hid_dev,
3157 "Failed to read range from device!\n");
3160 900 : get_unaligned_be16(&response.fap.params[0]);
3162 /* Read the current gain values */
3163 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3164 HIDPP_FF_GET_GLOBAL_GAINS,
3168 hid_warn(hidpp->hid_dev,
3169 "Failed to read gain values from device!\n");
3171 0xffff : get_unaligned_be16(&response.fap.params[0]);
3173 /* ignore boost value at response.fap.params[2] */
3175 return g920_ff_set_autocenter(hidpp, data);
3178 /* -------------------------------------------------------------------------- */
3179 /* Logitech Dinovo Mini keyboard with builtin touchpad */
3180 /* -------------------------------------------------------------------------- */
3181 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3183 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3184 struct hid_field *field, struct hid_usage *usage,
3185 unsigned long **bit, int *max)
3187 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3190 switch (usage->hid & HID_USAGE) {
3191 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3198 /* -------------------------------------------------------------------------- */
3199 /* HID++1.0 devices which use HID++ reports for their wheels */
3200 /* -------------------------------------------------------------------------- */
3201 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3203 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3204 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3205 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3208 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3219 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3225 input_report_rel(hidpp->input, REL_WHEEL, value);
3226 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3227 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3228 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3229 input_sync(hidpp->input);
3234 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3235 struct input_dev *input_dev)
3237 __set_bit(EV_REL, input_dev->evbit);
3238 __set_bit(REL_WHEEL, input_dev->relbit);
3239 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3240 __set_bit(REL_HWHEEL, input_dev->relbit);
3241 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3244 /* -------------------------------------------------------------------------- */
3245 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3246 /* -------------------------------------------------------------------------- */
3247 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3249 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3250 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3251 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3254 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3265 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3266 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3270 * Buttons are either delivered through the regular mouse report *or*
3271 * through the extra buttons report. At least for button 6 how it is
3272 * delivered differs per receiver firmware version. Even receivers with
3273 * the same usb-id show different behavior, so we handle both cases.
3275 for (i = 0; i < 8; i++)
3276 input_report_key(hidpp->input, BTN_MOUSE + i,
3277 (data[3] & (1 << i)));
3279 /* Some mice report events on button 9+, use BTN_MISC */
3280 for (i = 0; i < 8; i++)
3281 input_report_key(hidpp->input, BTN_MISC + i,
3282 (data[4] & (1 << i)));
3284 input_sync(hidpp->input);
3288 static void hidpp10_extra_mouse_buttons_populate_input(
3289 struct hidpp_device *hidpp, struct input_dev *input_dev)
3291 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3292 __set_bit(BTN_0, input_dev->keybit);
3293 __set_bit(BTN_1, input_dev->keybit);
3294 __set_bit(BTN_2, input_dev->keybit);
3295 __set_bit(BTN_3, input_dev->keybit);
3296 __set_bit(BTN_4, input_dev->keybit);
3297 __set_bit(BTN_5, input_dev->keybit);
3298 __set_bit(BTN_6, input_dev->keybit);
3299 __set_bit(BTN_7, input_dev->keybit);
3302 /* -------------------------------------------------------------------------- */
3303 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3304 /* -------------------------------------------------------------------------- */
3306 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3307 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3308 u8 *_rdesc, unsigned int *rsize)
3310 /* Note 0 terminated so we can use strnstr to search for this. */
3311 static const char consumer_rdesc_start[] = {
3312 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3313 0x09, 0x01, /* USAGE (Consumer Control) */
3314 0xA1, 0x01, /* COLLECTION (Application) */
3315 0x85, 0x03, /* REPORT_ID = 3 */
3316 0x75, 0x10, /* REPORT_SIZE (16) */
3317 0x95, 0x02, /* REPORT_COUNT (2) */
3318 0x15, 0x01, /* LOGICAL_MIN (1) */
3319 0x26, 0x00 /* LOGICAL_MAX (... */
3321 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3324 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3325 size = *rsize - (consumer_rdesc - rdesc);
3326 if (consumer_rdesc && size >= 25) {
3327 consumer_rdesc[15] = 0x7f;
3328 consumer_rdesc[16] = 0x10;
3329 consumer_rdesc[20] = 0x7f;
3330 consumer_rdesc[21] = 0x10;
3335 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3337 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3338 HIDPP_ENABLE_CONSUMER_REPORT,
3339 HIDPP_ENABLE_CONSUMER_REPORT);
3342 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3345 u8 consumer_report[5];
3350 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3351 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3355 * Build a normal consumer report (3) out of the data, this detour
3356 * is necessary to get some keyboards to report their 0x10xx usages.
3358 consumer_report[0] = 0x03;
3359 memcpy(&consumer_report[1], &data[3], 4);
3360 /* We are called from atomic context */
3361 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3362 consumer_report, 5, 1);
3367 /* -------------------------------------------------------------------------- */
3368 /* High-resolution scroll wheels */
3369 /* -------------------------------------------------------------------------- */
3371 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3376 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
3377 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3379 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3380 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
3381 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3383 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
3384 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3390 if (multiplier == 0)
3393 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3394 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3398 /* -------------------------------------------------------------------------- */
3399 /* Generic HID++ devices */
3400 /* -------------------------------------------------------------------------- */
3402 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3403 unsigned int *rsize)
3405 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3410 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3411 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3412 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3413 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3418 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3419 struct hid_field *field, struct hid_usage *usage,
3420 unsigned long **bit, int *max)
3422 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3427 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3428 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3429 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3430 field->application != HID_GD_MOUSE)
3431 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3433 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3434 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3439 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3440 struct hid_field *field, struct hid_usage *usage,
3441 unsigned long **bit, int *max)
3443 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3448 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3449 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3450 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3451 usage->code == ABS_Y || usage->code == ABS_Z ||
3452 usage->code == ABS_RZ)) {
3453 field->application = HID_GD_MULTIAXIS;
3461 static void hidpp_populate_input(struct hidpp_device *hidpp,
3462 struct input_dev *input)
3464 hidpp->input = input;
3466 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3467 wtp_populate_input(hidpp, input);
3468 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3469 m560_populate_input(hidpp, input);
3471 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3472 hidpp10_wheel_populate_input(hidpp, input);
3474 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3475 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3478 static int hidpp_input_configured(struct hid_device *hdev,
3479 struct hid_input *hidinput)
3481 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3482 struct input_dev *input = hidinput->input;
3487 hidpp_populate_input(hidpp, input);
3492 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3495 struct hidpp_report *question = hidpp->send_receive_buf;
3496 struct hidpp_report *answer = hidpp->send_receive_buf;
3497 struct hidpp_report *report = (struct hidpp_report *)data;
3501 * If the mutex is locked then we have a pending answer from a
3502 * previously sent command.
3504 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3506 * Check for a correct hidpp20 answer or the corresponding
3509 if (hidpp_match_answer(question, report) ||
3510 hidpp_match_error(question, report)) {
3512 hidpp->answer_available = true;
3513 wake_up(&hidpp->wait);
3515 * This was an answer to a command that this driver sent
3516 * We return 1 to hid-core to avoid forwarding the
3517 * command upstream as it has been treated by the driver
3524 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3525 atomic_set(&hidpp->connected,
3526 !(report->rap.params[0] & (1 << 6)));
3527 if (schedule_work(&hidpp->work) == 0)
3528 dbg_hid("%s: connect event already queued\n", __func__);
3532 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3533 ret = hidpp20_battery_event_1000(hidpp, data, size);
3536 ret = hidpp20_battery_event_1004(hidpp, data, size);
3539 ret = hidpp_solar_battery_event(hidpp, data, size);
3542 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3547 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3548 ret = hidpp10_battery_event(hidpp, data, size);
3553 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3554 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3559 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3560 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3565 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3566 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3574 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3577 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3583 /* Generic HID++ processing. */
3585 case REPORT_ID_HIDPP_VERY_LONG:
3586 if (size != hidpp->very_long_report_length) {
3587 hid_err(hdev, "received hid++ report of bad size (%d)",
3591 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3593 case REPORT_ID_HIDPP_LONG:
3594 if (size != HIDPP_REPORT_LONG_LENGTH) {
3595 hid_err(hdev, "received hid++ report of bad size (%d)",
3599 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3601 case REPORT_ID_HIDPP_SHORT:
3602 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3603 hid_err(hdev, "received hid++ report of bad size (%d)",
3607 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3611 /* If no report is available for further processing, skip calling
3612 * raw_event of subclasses. */
3616 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3617 return wtp_raw_event(hdev, data, size);
3618 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3619 return m560_raw_event(hdev, data, size);
3624 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3625 struct hid_usage *usage, __s32 value)
3627 /* This function will only be called for scroll events, due to the
3628 * restriction imposed in hidpp_usages.
3630 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3631 struct hidpp_scroll_counter *counter;
3636 counter = &hidpp->vertical_wheel_counter;
3637 /* A scroll event may occur before the multiplier has been retrieved or
3638 * the input device set, or high-res scroll enabling may fail. In such
3639 * cases we must return early (falling back to default behaviour) to
3640 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3642 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
3643 || hidpp->input == NULL || counter->wheel_multiplier == 0)
3646 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3650 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3652 static atomic_t battery_no = ATOMIC_INIT(0);
3653 struct power_supply_config cfg = { .drv_data = hidpp };
3654 struct power_supply_desc *desc = &hidpp->battery.desc;
3655 enum power_supply_property *battery_props;
3656 struct hidpp_battery *battery;
3657 unsigned int num_battery_props;
3661 if (hidpp->battery.ps)
3664 hidpp->battery.feature_index = 0xff;
3665 hidpp->battery.solar_feature_index = 0xff;
3666 hidpp->battery.voltage_feature_index = 0xff;
3668 if (hidpp->protocol_major >= 2) {
3669 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3670 ret = hidpp_solar_request_battery_event(hidpp);
3672 /* we only support one battery feature right now, so let's
3673 first check the ones that support battery level first
3674 and leave voltage for last */
3675 ret = hidpp20_query_battery_info_1000(hidpp);
3677 ret = hidpp20_query_battery_info_1004(hidpp);
3679 ret = hidpp20_query_battery_voltage_info(hidpp);
3684 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3686 ret = hidpp10_query_battery_status(hidpp);
3688 ret = hidpp10_query_battery_mileage(hidpp);
3691 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3693 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3695 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3698 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3699 hidpp_battery_props,
3700 sizeof(hidpp_battery_props),
3705 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3707 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
3708 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE)
3709 battery_props[num_battery_props++] =
3710 POWER_SUPPLY_PROP_CAPACITY;
3712 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3713 battery_props[num_battery_props++] =
3714 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3716 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3717 battery_props[num_battery_props++] =
3718 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3720 battery = &hidpp->battery;
3722 n = atomic_inc_return(&battery_no) - 1;
3723 desc->properties = battery_props;
3724 desc->num_properties = num_battery_props;
3725 desc->get_property = hidpp_battery_get_property;
3726 sprintf(battery->name, "hidpp_battery_%ld", n);
3727 desc->name = battery->name;
3728 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3729 desc->use_for_apm = 0;
3731 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3734 if (IS_ERR(battery->ps))
3735 return PTR_ERR(battery->ps);
3737 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3742 static void hidpp_overwrite_name(struct hid_device *hdev)
3744 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3747 if (hidpp->protocol_major < 2)
3750 name = hidpp_get_device_name(hidpp);
3753 hid_err(hdev, "unable to retrieve the name of the device");
3755 dbg_hid("HID++: Got name: %s\n", name);
3756 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3762 static int hidpp_input_open(struct input_dev *dev)
3764 struct hid_device *hid = input_get_drvdata(dev);
3766 return hid_hw_open(hid);
3769 static void hidpp_input_close(struct input_dev *dev)
3771 struct hid_device *hid = input_get_drvdata(dev);
3776 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3778 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3779 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3784 input_set_drvdata(input_dev, hdev);
3785 input_dev->open = hidpp_input_open;
3786 input_dev->close = hidpp_input_close;
3788 input_dev->name = hidpp->name;
3789 input_dev->phys = hdev->phys;
3790 input_dev->uniq = hdev->uniq;
3791 input_dev->id.bustype = hdev->bus;
3792 input_dev->id.vendor = hdev->vendor;
3793 input_dev->id.product = hdev->product;
3794 input_dev->id.version = hdev->version;
3795 input_dev->dev.parent = &hdev->dev;
3800 static void hidpp_connect_event(struct hidpp_device *hidpp)
3802 struct hid_device *hdev = hidpp->hid_dev;
3804 bool connected = atomic_read(&hidpp->connected);
3805 struct input_dev *input;
3806 char *name, *devm_name;
3809 if (hidpp->battery.ps) {
3810 hidpp->battery.online = false;
3811 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3812 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3813 power_supply_changed(hidpp->battery.ps);
3818 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3819 ret = wtp_connect(hdev, connected);
3822 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3823 ret = m560_send_config_command(hdev, connected);
3826 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3827 ret = k400_connect(hdev, connected);
3832 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3833 ret = hidpp10_wheel_connect(hidpp);
3838 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3839 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3844 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3845 ret = hidpp10_consumer_keys_connect(hidpp);
3850 /* the device is already connected, we can ask for its name and
3852 if (!hidpp->protocol_major) {
3853 ret = hidpp_root_get_protocol_version(hidpp);
3855 hid_err(hdev, "Can not get the protocol version.\n");
3860 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3861 name = hidpp_get_device_name(hidpp);
3863 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3869 hidpp->name = devm_name;
3873 hidpp_initialize_battery(hidpp);
3875 /* forward current battery state */
3876 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3877 hidpp10_enable_battery_reporting(hidpp);
3878 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3879 hidpp10_query_battery_mileage(hidpp);
3881 hidpp10_query_battery_status(hidpp);
3882 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3883 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3884 hidpp20_query_battery_voltage_info(hidpp);
3885 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
3886 hidpp20_query_battery_info_1004(hidpp);
3888 hidpp20_query_battery_info_1000(hidpp);
3890 if (hidpp->battery.ps)
3891 power_supply_changed(hidpp->battery.ps);
3893 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3894 hi_res_scroll_enable(hidpp);
3896 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3897 /* if the input nodes are already created, we can stop now */
3900 input = hidpp_allocate_input(hdev);
3902 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3906 hidpp_populate_input(hidpp, input);
3908 ret = input_register_device(input);
3910 input_free_device(input);
3912 hidpp->delayed_input = input;
3915 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3917 static struct attribute *sysfs_attrs[] = {
3918 &dev_attr_builtin_power_supply.attr,
3922 static const struct attribute_group ps_attribute_group = {
3923 .attrs = sysfs_attrs
3926 static int hidpp_get_report_length(struct hid_device *hdev, int id)
3928 struct hid_report_enum *re;
3929 struct hid_report *report;
3931 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3932 report = re->report_id_hash[id];
3936 return report->field[0]->report_count + 1;
3939 static u8 hidpp_validate_device(struct hid_device *hdev)
3941 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3942 int id, report_length;
3943 u8 supported_reports = 0;
3945 id = REPORT_ID_HIDPP_SHORT;
3946 report_length = hidpp_get_report_length(hdev, id);
3947 if (report_length) {
3948 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
3951 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
3954 id = REPORT_ID_HIDPP_LONG;
3955 report_length = hidpp_get_report_length(hdev, id);
3956 if (report_length) {
3957 if (report_length < HIDPP_REPORT_LONG_LENGTH)
3960 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
3963 id = REPORT_ID_HIDPP_VERY_LONG;
3964 report_length = hidpp_get_report_length(hdev, id);
3965 if (report_length) {
3966 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
3967 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3970 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
3971 hidpp->very_long_report_length = report_length;
3974 return supported_reports;
3977 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3981 static bool hidpp_application_equals(struct hid_device *hdev,
3982 unsigned int application)
3984 struct list_head *report_list;
3985 struct hid_report *report;
3987 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
3988 report = list_first_entry_or_null(report_list, struct hid_report, list);
3989 return report && report->application == application;
3992 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3994 struct hidpp_device *hidpp;
3997 unsigned int connect_mask = HID_CONNECT_DEFAULT;
3998 struct hidpp_ff_private_data data;
4000 /* report_fixup needs drvdata to be set before we call hid_parse */
4001 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4005 hidpp->hid_dev = hdev;
4006 hidpp->name = hdev->name;
4007 hidpp->quirks = id->driver_data;
4008 hid_set_drvdata(hdev, hidpp);
4010 ret = hid_parse(hdev);
4012 hid_err(hdev, "%s:parse failed\n", __func__);
4017 * Make sure the device is HID++ capable, otherwise treat as generic HID
4019 hidpp->supported_reports = hidpp_validate_device(hdev);
4021 if (!hidpp->supported_reports) {
4022 hid_set_drvdata(hdev, NULL);
4023 devm_kfree(&hdev->dev, hidpp);
4024 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4027 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4028 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
4030 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4031 hidpp_application_equals(hdev, HID_GD_MOUSE))
4032 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4033 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4035 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4036 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4037 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4039 if (disable_raw_mode) {
4040 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
4041 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
4044 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4045 ret = wtp_allocate(hdev, id);
4048 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4049 ret = k400_allocate(hdev);
4054 INIT_WORK(&hidpp->work, delayed_work_cb);
4055 mutex_init(&hidpp->send_mutex);
4056 init_waitqueue_head(&hidpp->wait);
4058 /* indicates we are handling the battery properties in the kernel */
4059 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4061 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4065 * Plain USB connections need to actually call start and open
4066 * on the transport driver to allow incoming data.
4068 ret = hid_hw_start(hdev, 0);
4070 hid_err(hdev, "hw start failed\n");
4071 goto hid_hw_start_fail;
4074 ret = hid_hw_open(hdev);
4076 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4078 goto hid_hw_open_fail;
4081 /* Allow incoming packets */
4082 hid_device_io_start(hdev);
4084 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
4085 hidpp_unifying_init(hidpp);
4087 connected = hidpp_root_get_protocol_version(hidpp) == 0;
4088 atomic_set(&hidpp->connected, connected);
4089 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
4092 hid_err(hdev, "Device not connected");
4093 goto hid_hw_init_fail;
4096 hidpp_overwrite_name(hdev);
4099 if (connected && hidpp->protocol_major >= 2) {
4100 ret = hidpp_set_wireless_feature_index(hidpp);
4102 hidpp->wireless_feature_index = 0;
4104 goto hid_hw_init_fail;
4107 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
4108 ret = wtp_get_config(hidpp);
4110 goto hid_hw_init_fail;
4111 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
4112 ret = g920_get_config(hidpp, &data);
4114 goto hid_hw_init_fail;
4117 hidpp_connect_event(hidpp);
4119 /* Reset the HID node state */
4120 hid_device_io_stop(hdev);
4124 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
4125 connect_mask &= ~HID_CONNECT_HIDINPUT;
4127 /* Now export the actual inputs and hidraw nodes to the world */
4128 ret = hid_hw_start(hdev, connect_mask);
4130 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
4131 goto hid_hw_start_fail;
4134 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4135 ret = hidpp_ff_init(hidpp, &data);
4137 hid_warn(hidpp->hid_dev,
4138 "Unable to initialize force feedback support, errno %d\n",
4149 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4150 cancel_work_sync(&hidpp->work);
4151 mutex_destroy(&hidpp->send_mutex);
4155 static void hidpp_remove(struct hid_device *hdev)
4157 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4160 return hid_hw_stop(hdev);
4162 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4165 cancel_work_sync(&hidpp->work);
4166 mutex_destroy(&hidpp->send_mutex);
4169 #define LDJ_DEVICE(product) \
4170 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4171 USB_VENDOR_ID_LOGITECH, (product))
4173 #define L27MHZ_DEVICE(product) \
4174 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4175 USB_VENDOR_ID_LOGITECH, (product))
4177 static const struct hid_device_id hidpp_devices[] = {
4178 { /* wireless touchpad */
4180 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4181 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4182 { /* wireless touchpad T650 */
4184 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4185 { /* wireless touchpad T651 */
4186 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4187 USB_DEVICE_ID_LOGITECH_T651),
4188 .driver_data = HIDPP_QUIRK_CLASS_WTP },
4189 { /* Mouse Logitech Anywhere MX */
4190 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4191 { /* Mouse Logitech Cube */
4192 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4193 { /* Mouse Logitech M335 */
4194 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4195 { /* Mouse Logitech M515 */
4196 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4197 { /* Mouse logitech M560 */
4199 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
4200 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4201 { /* Mouse Logitech M705 (firmware RQM17) */
4202 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4203 { /* Mouse Logitech M705 (firmware RQM67) */
4204 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4205 { /* Mouse Logitech M720 */
4206 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4207 { /* Mouse Logitech MX Anywhere 2 */
4208 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4209 { LDJ_DEVICE(0x4072), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4210 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4211 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4212 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4213 { /* Mouse Logitech MX Anywhere 2S */
4214 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4215 { /* Mouse Logitech MX Master */
4216 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4217 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4218 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4219 { /* Mouse Logitech MX Master 2S */
4220 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4221 { /* Mouse Logitech MX Master 3 */
4222 LDJ_DEVICE(0x4082), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4223 { /* Mouse Logitech Performance MX */
4224 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4225 { /* Keyboard logitech K400 */
4227 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4228 { /* Solar Keyboard Logitech K750 */
4230 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4231 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4233 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4234 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4236 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4237 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4239 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4241 { LDJ_DEVICE(HID_ANY_ID) },
4243 { /* Keyboard LX501 (Y-RR53) */
4244 L27MHZ_DEVICE(0x0049),
4245 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4246 { /* Keyboard MX3000 (Y-RAM74) */
4247 L27MHZ_DEVICE(0x0057),
4248 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4249 { /* Keyboard MX3200 (Y-RAV80) */
4250 L27MHZ_DEVICE(0x005c),
4251 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4252 { /* S510 Media Remote */
4253 L27MHZ_DEVICE(0x00fe),
4254 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4256 { L27MHZ_DEVICE(HID_ANY_ID) },
4258 { /* Logitech G403 Wireless Gaming Mouse over USB */
4259 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4260 { /* Logitech G703 Gaming Mouse over USB */
4261 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4262 { /* Logitech G703 Hero Gaming Mouse over USB */
4263 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4264 { /* Logitech G900 Gaming Mouse over USB */
4265 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4266 { /* Logitech G903 Gaming Mouse over USB */
4267 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4268 { /* Logitech G903 Hero Gaming Mouse over USB */
4269 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4270 { /* Logitech G920 Wheel over USB */
4271 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4272 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4273 { /* Logitech G Pro Gaming Mouse over USB */
4274 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4276 { /* MX5000 keyboard over Bluetooth */
4277 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4278 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4279 { /* Dinovo Edge keyboard over Bluetooth */
4280 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4281 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4282 { /* MX5500 keyboard over Bluetooth */
4283 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4284 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4285 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4286 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4287 { /* MX Master mouse over Bluetooth */
4288 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012),
4289 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4290 { /* MX Ergo trackball over Bluetooth */
4291 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4292 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e),
4293 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4294 { /* MX Master 3 mouse over Bluetooth */
4295 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023),
4296 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4300 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4302 static const struct hid_usage_id hidpp_usages[] = {
4303 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4304 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4307 static struct hid_driver hidpp_driver = {
4308 .name = "logitech-hidpp-device",
4309 .id_table = hidpp_devices,
4310 .report_fixup = hidpp_report_fixup,
4311 .probe = hidpp_probe,
4312 .remove = hidpp_remove,
4313 .raw_event = hidpp_raw_event,
4314 .usage_table = hidpp_usages,
4315 .event = hidpp_event,
4316 .input_configured = hidpp_input_configured,
4317 .input_mapping = hidpp_input_mapping,
4318 .input_mapped = hidpp_input_mapped,
4321 module_hid_driver(hidpp_driver);