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");
35 static bool disable_tap_to_click;
36 module_param(disable_tap_to_click, bool, 0644);
37 MODULE_PARM_DESC(disable_tap_to_click,
38 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
40 /* Define a non-zero software ID to identify our own requests */
41 #define LINUX_KERNEL_SW_ID 0x01
43 #define REPORT_ID_HIDPP_SHORT 0x10
44 #define REPORT_ID_HIDPP_LONG 0x11
45 #define REPORT_ID_HIDPP_VERY_LONG 0x12
47 #define HIDPP_REPORT_SHORT_LENGTH 7
48 #define HIDPP_REPORT_LONG_LENGTH 20
49 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
51 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
52 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
53 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
55 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
56 #define HIDPP_SUB_ID_ROLLER 0x05
57 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
58 #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
59 #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
61 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
62 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
63 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
64 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
65 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
67 /* bits 2..20 are reserved for classes */
68 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
69 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
70 #define HIDPP_QUIRK_DELAYED_INIT BIT(23)
71 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
72 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(25)
73 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(26)
74 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(27)
75 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(28)
76 #define HIDPP_QUIRK_WIRELESS_STATUS BIT(29)
78 /* These are just aliases for now */
79 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
80 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
82 /* Convenience constant to check for any high-res support. */
83 #define HIDPP_CAPABILITY_HI_RES_SCROLL (HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \
84 HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \
85 HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL)
87 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
88 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
89 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
90 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
91 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
92 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
93 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
94 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL BIT(7)
95 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL BIT(8)
96 #define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL BIT(9)
97 #define HIDPP_CAPABILITY_ADC_MEASUREMENT BIT(10)
99 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
102 * There are two hidpp protocols in use, the first version hidpp10 is known
103 * as register access protocol or RAP, the second version hidpp20 is known as
104 * feature access protocol or FAP
106 * Most older devices (including the Unifying usb receiver) use the RAP protocol
107 * where as most newer devices use the FAP protocol. Both protocols are
108 * compatible with the underlying transport, which could be usb, Unifiying, or
109 * bluetooth. The message lengths are defined by the hid vendor specific report
110 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
111 * the HIDPP_LONG report type (total message length 20 bytes)
113 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
114 * messages. The Unifying receiver itself responds to RAP messages (device index
115 * is 0xFF for the receiver), and all messages (short or long) with a device
116 * index between 1 and 6 are passed untouched to the corresponding paired
119 * The paired device can be RAP or FAP, it will receive the message untouched
120 * from the Unifiying receiver.
125 u8 funcindex_clientid;
126 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
132 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
135 struct hidpp_report {
141 u8 rawbytes[sizeof(struct fap)];
145 struct hidpp_battery {
147 u8 solar_feature_index;
148 u8 voltage_feature_index;
149 u8 adc_measurement_feature_index;
150 struct power_supply_desc desc;
151 struct power_supply *ps;
159 u8 supported_levels_1004;
163 * struct hidpp_scroll_counter - Utility class for processing high-resolution
165 * @dev: the input device for which events should be reported.
166 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
167 * @remainder: counts the number of high-resolution units moved since the last
168 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
169 * only be used by class methods.
170 * @direction: direction of last movement (1 or -1)
171 * @last_time: last event time, used to reset remainder after inactivity
173 struct hidpp_scroll_counter {
174 int wheel_multiplier;
177 unsigned long long last_time;
180 struct hidpp_device {
181 struct hid_device *hid_dev;
182 struct input_dev *input;
183 struct mutex send_mutex;
184 void *send_receive_buf;
185 char *name; /* will never be NULL and should not be freed */
186 wait_queue_head_t wait;
187 int very_long_report_length;
188 bool answer_available;
194 struct work_struct work;
195 struct kfifo delayed_work_fifo;
196 struct input_dev *delayed_input;
198 unsigned long quirks;
199 unsigned long capabilities;
200 u8 supported_reports;
202 struct hidpp_battery battery;
203 struct hidpp_scroll_counter vertical_wheel_counter;
205 u8 wireless_feature_index;
208 /* HID++ 1.0 error codes */
209 #define HIDPP_ERROR 0x8f
210 #define HIDPP_ERROR_SUCCESS 0x00
211 #define HIDPP_ERROR_INVALID_SUBID 0x01
212 #define HIDPP_ERROR_INVALID_ADRESS 0x02
213 #define HIDPP_ERROR_INVALID_VALUE 0x03
214 #define HIDPP_ERROR_CONNECT_FAIL 0x04
215 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
216 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
217 #define HIDPP_ERROR_BUSY 0x07
218 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
219 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
220 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
221 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
222 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
223 /* HID++ 2.0 error codes */
224 #define HIDPP20_ERROR_NO_ERROR 0x00
225 #define HIDPP20_ERROR_UNKNOWN 0x01
226 #define HIDPP20_ERROR_INVALID_ARGS 0x02
227 #define HIDPP20_ERROR_OUT_OF_RANGE 0x03
228 #define HIDPP20_ERROR_HW_ERROR 0x04
229 #define HIDPP20_ERROR_NOT_ALLOWED 0x05
230 #define HIDPP20_ERROR_INVALID_FEATURE_INDEX 0x06
231 #define HIDPP20_ERROR_INVALID_FUNCTION_ID 0x07
232 #define HIDPP20_ERROR_BUSY 0x08
233 #define HIDPP20_ERROR_UNSUPPORTED 0x09
234 #define HIDPP20_ERROR 0xff
236 static int __hidpp_send_report(struct hid_device *hdev,
237 struct hidpp_report *hidpp_report)
239 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
240 int fields_count, ret;
242 switch (hidpp_report->report_id) {
243 case REPORT_ID_HIDPP_SHORT:
244 fields_count = HIDPP_REPORT_SHORT_LENGTH;
246 case REPORT_ID_HIDPP_LONG:
247 fields_count = HIDPP_REPORT_LONG_LENGTH;
249 case REPORT_ID_HIDPP_VERY_LONG:
250 fields_count = hidpp->very_long_report_length;
257 * set the device_index as the receiver, it will be overwritten by
258 * hid_hw_request if needed
260 hidpp_report->device_index = 0xff;
262 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
263 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
265 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
266 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
270 return ret == fields_count ? 0 : -1;
274 * Effectively send the message to the device, waiting for its answer.
276 * Must be called with hidpp->send_mutex locked
278 * Same return protocol than hidpp_send_message_sync():
280 * - negative error means transport error
281 * - positive value means protocol error
283 static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp,
284 struct hidpp_report *message,
285 struct hidpp_report *response)
289 __must_hold(&hidpp->send_mutex);
291 hidpp->send_receive_buf = response;
292 hidpp->answer_available = false;
295 * So that we can later validate the answer when it arrives
298 *response = *message;
300 ret = __hidpp_send_report(hidpp->hid_dev, message);
302 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
303 memset(response, 0, sizeof(struct hidpp_report));
307 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
309 dbg_hid("%s:timeout waiting for response\n", __func__);
310 memset(response, 0, sizeof(struct hidpp_report));
314 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
315 response->rap.sub_id == HIDPP_ERROR) {
316 ret = response->rap.params[1];
317 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
321 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
322 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
323 response->fap.feature_index == HIDPP20_ERROR) {
324 ret = response->fap.params[1];
325 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
333 * hidpp_send_message_sync() returns 0 in case of success, and something else
334 * in case of a failure.
336 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
339 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
340 struct hidpp_report *message,
341 struct hidpp_report *response)
346 mutex_lock(&hidpp->send_mutex);
349 ret = __do_hidpp_send_message_sync(hidpp, message, response);
350 if (ret != HIDPP20_ERROR_BUSY)
353 dbg_hid("%s:got busy hidpp 2.0 error %02X, retrying\n", __func__, ret);
354 } while (--max_retries);
356 mutex_unlock(&hidpp->send_mutex);
362 * hidpp_send_fap_command_sync() returns 0 in case of success, and something else
363 * in case of a failure.
365 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
368 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
369 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
370 struct hidpp_report *response)
372 struct hidpp_report *message;
375 if (param_count > sizeof(message->fap.params)) {
376 hid_dbg(hidpp->hid_dev,
377 "Invalid number of parameters passed to command (%d != %llu)\n",
379 (unsigned long long) sizeof(message->fap.params));
383 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
387 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
388 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
390 message->report_id = REPORT_ID_HIDPP_LONG;
391 message->fap.feature_index = feat_index;
392 message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID;
393 memcpy(&message->fap.params, params, param_count);
395 ret = hidpp_send_message_sync(hidpp, message, response);
401 * hidpp_send_rap_command_sync() returns 0 in case of success, and something else
402 * in case of a failure.
404 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
407 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
408 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
409 struct hidpp_report *response)
411 struct hidpp_report *message;
414 /* Send as long report if short reports are not supported. */
415 if (report_id == REPORT_ID_HIDPP_SHORT &&
416 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
417 report_id = REPORT_ID_HIDPP_LONG;
420 case REPORT_ID_HIDPP_SHORT:
421 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
423 case REPORT_ID_HIDPP_LONG:
424 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
426 case REPORT_ID_HIDPP_VERY_LONG:
427 max_count = hidpp_dev->very_long_report_length - 4;
433 if (param_count > max_count)
436 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
439 message->report_id = report_id;
440 message->rap.sub_id = sub_id;
441 message->rap.reg_address = reg_address;
442 memcpy(&message->rap.params, params, param_count);
444 ret = hidpp_send_message_sync(hidpp_dev, message, response);
449 static inline bool hidpp_match_answer(struct hidpp_report *question,
450 struct hidpp_report *answer)
452 return (answer->fap.feature_index == question->fap.feature_index) &&
453 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
456 static inline bool hidpp_match_error(struct hidpp_report *question,
457 struct hidpp_report *answer)
459 return ((answer->rap.sub_id == HIDPP_ERROR) ||
460 (answer->fap.feature_index == HIDPP20_ERROR)) &&
461 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
462 (answer->fap.params[0] == question->fap.funcindex_clientid);
465 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
466 struct hidpp_report *report)
468 return (hidpp->wireless_feature_index &&
469 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
470 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
471 (report->rap.sub_id == 0x41));
475 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
477 static void hidpp_prefix_name(char **name, int name_length)
479 #define PREFIX_LENGTH 9 /* "Logitech " */
484 if (name_length > PREFIX_LENGTH &&
485 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
486 /* The prefix has is already in the name */
489 new_length = PREFIX_LENGTH + name_length;
490 new_name = kzalloc(new_length, GFP_KERNEL);
494 snprintf(new_name, new_length, "Logitech %s", *name);
502 * Updates the USB wireless_status based on whether the headset
503 * is turned on and reachable.
505 static void hidpp_update_usb_wireless_status(struct hidpp_device *hidpp)
507 struct hid_device *hdev = hidpp->hid_dev;
508 struct usb_interface *intf;
510 if (!(hidpp->quirks & HIDPP_QUIRK_WIRELESS_STATUS))
512 if (!hid_is_usb(hdev))
515 intf = to_usb_interface(hdev->dev.parent);
516 usb_set_wireless_status(intf, hidpp->battery.online ?
517 USB_WIRELESS_STATUS_CONNECTED :
518 USB_WIRELESS_STATUS_DISCONNECTED);
522 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
523 * events given a high-resolution wheel
525 * @input_dev: Pointer to the input device
526 * @counter: a hid_scroll_counter struct describing the wheel.
527 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
530 * Given a high-resolution movement, this function converts the movement into
531 * fractions of 120 and emits high-resolution scroll events for the input
532 * device. It also uses the multiplier from &struct hid_scroll_counter to
533 * emit low-resolution scroll events when appropriate for
534 * backwards-compatibility with userspace input libraries.
536 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
537 struct hidpp_scroll_counter *counter,
540 int low_res_value, remainder, direction;
541 unsigned long long now, previous;
543 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
544 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
546 remainder = counter->remainder;
547 direction = hi_res_value > 0 ? 1 : -1;
550 previous = counter->last_time;
551 counter->last_time = now;
553 * Reset the remainder after a period of inactivity or when the
554 * direction changes. This prevents the REL_WHEEL emulation point
555 * from sliding for devices that don't always provide the same
556 * number of movements per detent.
558 if (now - previous > 1000000000 || direction != counter->direction)
561 counter->direction = direction;
562 remainder += hi_res_value;
564 /* Some wheels will rest 7/8ths of a detent from the previous detent
565 * after slow movement, so we want the threshold for low-res events to
566 * be in the middle between two detents (e.g. after 4/8ths) as
567 * opposed to on the detents themselves (8/8ths).
569 if (abs(remainder) >= 60) {
570 /* Add (or subtract) 1 because we want to trigger when the wheel
571 * is half-way to the next detent (i.e. scroll 1 detent after a
572 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
575 low_res_value = remainder / 120;
576 if (low_res_value == 0)
577 low_res_value = (hi_res_value > 0 ? 1 : -1);
578 input_report_rel(input_dev, REL_WHEEL, low_res_value);
579 remainder -= low_res_value * 120;
581 counter->remainder = remainder;
584 /* -------------------------------------------------------------------------- */
585 /* HIDP++ 1.0 commands */
586 /* -------------------------------------------------------------------------- */
588 #define HIDPP_SET_REGISTER 0x80
589 #define HIDPP_GET_REGISTER 0x81
590 #define HIDPP_SET_LONG_REGISTER 0x82
591 #define HIDPP_GET_LONG_REGISTER 0x83
594 * hidpp10_set_register - Modify a HID++ 1.0 register.
595 * @hidpp_dev: the device to set the register on.
596 * @register_address: the address of the register to modify.
597 * @byte: the byte of the register to modify. Should be less than 3.
598 * @mask: mask of the bits to modify
599 * @value: new values for the bits in mask
600 * Return: 0 if successful, otherwise a negative error code.
602 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
603 u8 register_address, u8 byte, u8 mask, u8 value)
605 struct hidpp_report response;
607 u8 params[3] = { 0 };
609 ret = hidpp_send_rap_command_sync(hidpp_dev,
610 REPORT_ID_HIDPP_SHORT,
617 memcpy(params, response.rap.params, 3);
619 params[byte] &= ~mask;
620 params[byte] |= value & mask;
622 return hidpp_send_rap_command_sync(hidpp_dev,
623 REPORT_ID_HIDPP_SHORT,
626 params, 3, &response);
629 #define HIDPP_REG_ENABLE_REPORTS 0x00
630 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
631 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
632 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
633 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
634 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
636 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
638 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
639 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
642 #define HIDPP_REG_FEATURES 0x01
643 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
644 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
646 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
647 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
649 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
650 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
653 #define HIDPP_REG_BATTERY_STATUS 0x07
655 static int hidpp10_battery_status_map_level(u8 param)
661 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
664 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
667 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
670 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
673 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
679 static int hidpp10_battery_status_map_status(u8 param)
685 /* discharging (in use) */
686 status = POWER_SUPPLY_STATUS_DISCHARGING;
688 case 0x21: /* (standard) charging */
689 case 0x24: /* fast charging */
690 case 0x25: /* slow charging */
691 status = POWER_SUPPLY_STATUS_CHARGING;
693 case 0x26: /* topping charge */
694 case 0x22: /* charge complete */
695 status = POWER_SUPPLY_STATUS_FULL;
697 case 0x20: /* unknown */
698 status = POWER_SUPPLY_STATUS_UNKNOWN;
701 * 0x01...0x1F = reserved (not charging)
702 * 0x23 = charging error
703 * 0x27..0xff = reserved
706 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
713 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
715 struct hidpp_report response;
718 ret = hidpp_send_rap_command_sync(hidpp,
719 REPORT_ID_HIDPP_SHORT,
721 HIDPP_REG_BATTERY_STATUS,
726 hidpp->battery.level =
727 hidpp10_battery_status_map_level(response.rap.params[0]);
728 status = hidpp10_battery_status_map_status(response.rap.params[1]);
729 hidpp->battery.status = status;
730 /* the capacity is only available when discharging or full */
731 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
732 status == POWER_SUPPLY_STATUS_FULL;
737 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
739 static int hidpp10_battery_mileage_map_status(u8 param)
743 switch (param >> 6) {
745 /* discharging (in use) */
746 status = POWER_SUPPLY_STATUS_DISCHARGING;
748 case 0x01: /* charging */
749 status = POWER_SUPPLY_STATUS_CHARGING;
751 case 0x02: /* charge complete */
752 status = POWER_SUPPLY_STATUS_FULL;
755 * 0x03 = charging error
758 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
765 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
767 struct hidpp_report response;
770 ret = hidpp_send_rap_command_sync(hidpp,
771 REPORT_ID_HIDPP_SHORT,
773 HIDPP_REG_BATTERY_MILEAGE,
778 hidpp->battery.capacity = response.rap.params[0];
779 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
780 hidpp->battery.status = status;
781 /* the capacity is only available when discharging or full */
782 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
783 status == POWER_SUPPLY_STATUS_FULL;
788 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
790 struct hidpp_report *report = (struct hidpp_report *)data;
791 int status, capacity, level;
794 if (report->report_id != REPORT_ID_HIDPP_SHORT)
797 switch (report->rap.sub_id) {
798 case HIDPP_REG_BATTERY_STATUS:
799 capacity = hidpp->battery.capacity;
800 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
801 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
803 case HIDPP_REG_BATTERY_MILEAGE:
804 capacity = report->rap.params[0];
805 level = hidpp->battery.level;
806 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
812 changed = capacity != hidpp->battery.capacity ||
813 level != hidpp->battery.level ||
814 status != hidpp->battery.status;
816 /* the capacity is only available when discharging or full */
817 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
818 status == POWER_SUPPLY_STATUS_FULL;
821 hidpp->battery.level = level;
822 hidpp->battery.status = status;
823 if (hidpp->battery.ps)
824 power_supply_changed(hidpp->battery.ps);
830 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
831 #define HIDPP_EXTENDED_PAIRING 0x30
832 #define HIDPP_DEVICE_NAME 0x40
834 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
836 struct hidpp_report response;
838 u8 params[1] = { HIDPP_DEVICE_NAME };
842 ret = hidpp_send_rap_command_sync(hidpp_dev,
843 REPORT_ID_HIDPP_SHORT,
844 HIDPP_GET_LONG_REGISTER,
845 HIDPP_REG_PAIRING_INFORMATION,
846 params, 1, &response);
850 len = response.rap.params[1];
852 if (2 + len > sizeof(response.rap.params))
855 if (len < 4) /* logitech devices are usually at least Xddd */
858 name = kzalloc(len + 1, GFP_KERNEL);
862 memcpy(name, &response.rap.params[2], len);
864 /* include the terminating '\0' */
865 hidpp_prefix_name(&name, len + 1);
870 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
872 struct hidpp_report response;
874 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
876 ret = hidpp_send_rap_command_sync(hidpp,
877 REPORT_ID_HIDPP_SHORT,
878 HIDPP_GET_LONG_REGISTER,
879 HIDPP_REG_PAIRING_INFORMATION,
880 params, 1, &response);
885 * We don't care about LE or BE, we will output it as a string
886 * with %4phD, so we need to keep the order.
888 *serial = *((u32 *)&response.rap.params[1]);
892 static int hidpp_unifying_init(struct hidpp_device *hidpp)
894 struct hid_device *hdev = hidpp->hid_dev;
899 ret = hidpp_unifying_get_serial(hidpp, &serial);
903 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
904 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
906 name = hidpp_unifying_get_name(hidpp);
910 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
911 dbg_hid("HID++ Unifying: Got name: %s\n", name);
917 /* -------------------------------------------------------------------------- */
919 /* -------------------------------------------------------------------------- */
921 #define HIDPP_PAGE_ROOT 0x0000
922 #define HIDPP_PAGE_ROOT_IDX 0x00
924 #define CMD_ROOT_GET_FEATURE 0x00
925 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x10
927 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
928 u8 *feature_index, u8 *feature_type)
930 struct hidpp_report response;
932 u8 params[2] = { feature >> 8, feature & 0x00FF };
934 ret = hidpp_send_fap_command_sync(hidpp,
936 CMD_ROOT_GET_FEATURE,
937 params, 2, &response);
941 if (response.fap.params[0] == 0)
944 *feature_index = response.fap.params[0];
945 *feature_type = response.fap.params[1];
950 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
952 const u8 ping_byte = 0x5a;
953 u8 ping_data[3] = { 0, 0, ping_byte };
954 struct hidpp_report response;
957 ret = hidpp_send_rap_command_sync(hidpp,
958 REPORT_ID_HIDPP_SHORT,
960 CMD_ROOT_GET_PROTOCOL_VERSION | LINUX_KERNEL_SW_ID,
961 ping_data, sizeof(ping_data), &response);
963 if (ret == HIDPP_ERROR_INVALID_SUBID) {
964 hidpp->protocol_major = 1;
965 hidpp->protocol_minor = 0;
969 /* the device might not be connected */
970 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
974 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
981 if (response.rap.params[2] != ping_byte) {
982 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
983 __func__, response.rap.params[2], ping_byte);
987 hidpp->protocol_major = response.rap.params[0];
988 hidpp->protocol_minor = response.rap.params[1];
991 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
992 hidpp->protocol_major, hidpp->protocol_minor);
996 /* -------------------------------------------------------------------------- */
997 /* 0x0003: Device Information */
998 /* -------------------------------------------------------------------------- */
1000 #define HIDPP_PAGE_DEVICE_INFORMATION 0x0003
1002 #define CMD_GET_DEVICE_INFO 0x00
1004 static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
1006 struct hidpp_report response;
1011 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
1017 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1018 CMD_GET_DEVICE_INFO,
1019 NULL, 0, &response);
1023 /* See hidpp_unifying_get_serial() */
1024 *serial = *((u32 *)&response.rap.params[1]);
1028 static int hidpp_serial_init(struct hidpp_device *hidpp)
1030 struct hid_device *hdev = hidpp->hid_dev;
1034 ret = hidpp_get_serial(hidpp, &serial);
1038 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
1039 dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
1044 /* -------------------------------------------------------------------------- */
1045 /* 0x0005: GetDeviceNameType */
1046 /* -------------------------------------------------------------------------- */
1048 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
1050 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x00
1051 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x10
1052 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x20
1054 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
1055 u8 feature_index, u8 *nameLength)
1057 struct hidpp_report response;
1060 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1061 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
1064 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1071 *nameLength = response.fap.params[0];
1076 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
1077 u8 feature_index, u8 char_index, char *device_name, int len_buf)
1079 struct hidpp_report response;
1083 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1084 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
1088 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1095 switch (response.report_id) {
1096 case REPORT_ID_HIDPP_VERY_LONG:
1097 count = hidpp->very_long_report_length - 4;
1099 case REPORT_ID_HIDPP_LONG:
1100 count = HIDPP_REPORT_LONG_LENGTH - 4;
1102 case REPORT_ID_HIDPP_SHORT:
1103 count = HIDPP_REPORT_SHORT_LENGTH - 4;
1109 if (len_buf < count)
1112 for (i = 0; i < count; i++)
1113 device_name[i] = response.fap.params[i];
1118 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1127 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1128 &feature_index, &feature_type);
1132 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1137 name = kzalloc(__name_length + 1, GFP_KERNEL);
1141 while (index < __name_length) {
1142 ret = hidpp_devicenametype_get_device_name(hidpp,
1143 feature_index, index, name + index,
1144 __name_length - index);
1152 /* include the terminating '\0' */
1153 hidpp_prefix_name(&name, __name_length + 1);
1158 /* -------------------------------------------------------------------------- */
1159 /* 0x1000: Battery level status */
1160 /* -------------------------------------------------------------------------- */
1162 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1164 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1165 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1167 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1169 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1170 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1171 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1173 static int hidpp_map_battery_level(int capacity)
1176 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1178 * The spec says this should be < 31 but some devices report 30
1179 * with brand new batteries and Windows reports 30 as "Good".
1181 else if (capacity < 30)
1182 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1183 else if (capacity < 81)
1184 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1185 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1188 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1194 *capacity = data[0];
1195 *next_capacity = data[1];
1196 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1198 /* When discharging, we can rely on the device reported capacity.
1199 * For all other states the device reports 0 (unknown).
1202 case 0: /* discharging (in use) */
1203 status = POWER_SUPPLY_STATUS_DISCHARGING;
1204 *level = hidpp_map_battery_level(*capacity);
1206 case 1: /* recharging */
1207 status = POWER_SUPPLY_STATUS_CHARGING;
1209 case 2: /* charge in final stage */
1210 status = POWER_SUPPLY_STATUS_CHARGING;
1212 case 3: /* charge complete */
1213 status = POWER_SUPPLY_STATUS_FULL;
1214 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1217 case 4: /* recharging below optimal speed */
1218 status = POWER_SUPPLY_STATUS_CHARGING;
1220 /* 5 = invalid battery type
1222 7 = other charging error */
1224 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1231 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1238 struct hidpp_report response;
1240 u8 *params = (u8 *)response.fap.params;
1242 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1243 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1244 NULL, 0, &response);
1245 /* Ignore these intermittent errors */
1246 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1249 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1256 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1263 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1266 struct hidpp_report response;
1268 u8 *params = (u8 *)response.fap.params;
1269 unsigned int level_count, flags;
1271 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1272 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1273 NULL, 0, &response);
1275 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1282 level_count = params[0];
1285 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1286 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1288 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1293 static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1297 int status, capacity, next_capacity, level;
1299 if (hidpp->battery.feature_index == 0xff) {
1300 ret = hidpp_root_get_feature(hidpp,
1301 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1302 &hidpp->battery.feature_index,
1308 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1309 hidpp->battery.feature_index,
1311 &next_capacity, &level);
1315 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1316 hidpp->battery.feature_index);
1320 hidpp->battery.status = status;
1321 hidpp->battery.capacity = capacity;
1322 hidpp->battery.level = level;
1323 /* the capacity is only available when discharging or full */
1324 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1325 status == POWER_SUPPLY_STATUS_FULL;
1330 static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1333 struct hidpp_report *report = (struct hidpp_report *)data;
1334 int status, capacity, next_capacity, level;
1337 if (report->fap.feature_index != hidpp->battery.feature_index ||
1338 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1341 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1346 /* the capacity is only available when discharging or full */
1347 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1348 status == POWER_SUPPLY_STATUS_FULL;
1350 changed = capacity != hidpp->battery.capacity ||
1351 level != hidpp->battery.level ||
1352 status != hidpp->battery.status;
1355 hidpp->battery.level = level;
1356 hidpp->battery.capacity = capacity;
1357 hidpp->battery.status = status;
1358 if (hidpp->battery.ps)
1359 power_supply_changed(hidpp->battery.ps);
1365 /* -------------------------------------------------------------------------- */
1366 /* 0x1001: Battery voltage */
1367 /* -------------------------------------------------------------------------- */
1369 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1371 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1373 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1375 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1376 int *level, int *charge_type)
1380 long flags = (long) data[2];
1381 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1384 switch (flags & 0x07) {
1386 status = POWER_SUPPLY_STATUS_CHARGING;
1389 status = POWER_SUPPLY_STATUS_FULL;
1390 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1393 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1396 status = POWER_SUPPLY_STATUS_UNKNOWN;
1400 status = POWER_SUPPLY_STATUS_DISCHARGING;
1402 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1403 if (test_bit(3, &flags)) {
1404 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1406 if (test_bit(4, &flags)) {
1407 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1409 if (test_bit(5, &flags)) {
1410 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1413 *voltage = get_unaligned_be16(data);
1418 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1420 int *status, int *voltage,
1421 int *level, int *charge_type)
1423 struct hidpp_report response;
1425 u8 *params = (u8 *)response.fap.params;
1427 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1428 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1429 NULL, 0, &response);
1432 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1439 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1441 *status = hidpp20_battery_map_status_voltage(params, voltage,
1442 level, charge_type);
1447 static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1449 /* NB: This voltage curve doesn't necessarily map perfectly to all
1450 * devices that implement the BATTERY_VOLTAGE feature. This is because
1451 * there are a few devices that use different battery technology.
1454 static const int voltages[100] = {
1455 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1456 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1457 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1458 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1459 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1460 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1461 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1462 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1463 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1464 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1469 if (unlikely(voltage < 3500 || voltage >= 5000))
1470 hid_warn_once(hid_dev,
1471 "%s: possibly using the wrong voltage curve\n",
1474 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1475 if (voltage >= voltages[i])
1476 return ARRAY_SIZE(voltages) - i;
1482 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1486 int status, voltage, level, charge_type;
1488 if (hidpp->battery.voltage_feature_index == 0xff) {
1489 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1490 &hidpp->battery.voltage_feature_index,
1496 ret = hidpp20_battery_get_battery_voltage(hidpp,
1497 hidpp->battery.voltage_feature_index,
1498 &status, &voltage, &level, &charge_type);
1503 hidpp->battery.status = status;
1504 hidpp->battery.voltage = voltage;
1505 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1507 hidpp->battery.level = level;
1508 hidpp->battery.charge_type = charge_type;
1509 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1514 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1517 struct hidpp_report *report = (struct hidpp_report *)data;
1518 int status, voltage, level, charge_type;
1520 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1521 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1524 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1525 &level, &charge_type);
1527 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1529 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1530 hidpp->battery.voltage = voltage;
1531 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1533 hidpp->battery.status = status;
1534 hidpp->battery.level = level;
1535 hidpp->battery.charge_type = charge_type;
1536 if (hidpp->battery.ps)
1537 power_supply_changed(hidpp->battery.ps);
1542 /* -------------------------------------------------------------------------- */
1543 /* 0x1004: Unified battery */
1544 /* -------------------------------------------------------------------------- */
1546 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1548 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1549 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1551 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1553 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1554 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1555 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1556 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1558 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1559 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1561 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1564 struct hidpp_report response;
1566 u8 *params = (u8 *)response.fap.params;
1568 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1569 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1570 /* we have already set the device capabilities, so let's skip */
1574 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1575 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1576 NULL, 0, &response);
1577 /* Ignore these intermittent errors */
1578 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1581 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1589 * If the device supports state of charge (battery percentage) we won't
1590 * export the battery level information. there are 4 possible battery
1591 * levels and they all are optional, this means that the device might
1592 * not support any of them, we are just better off with the battery
1595 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1596 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1597 hidpp->battery.supported_levels_1004 = 0;
1599 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1600 hidpp->battery.supported_levels_1004 = params[0];
1606 static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1608 u8 external_power_status)
1612 switch (charging_status) {
1613 case 0: /* discharging */
1614 status = POWER_SUPPLY_STATUS_DISCHARGING;
1616 case 1: /* charging */
1617 case 2: /* charging slow */
1618 status = POWER_SUPPLY_STATUS_CHARGING;
1620 case 3: /* complete */
1621 status = POWER_SUPPLY_STATUS_FULL;
1624 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1625 hid_info(hidpp->hid_dev, "%s: charging error",
1629 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1636 static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1639 /* cler unsupported level bits */
1640 battery_level &= hidpp->battery.supported_levels_1004;
1642 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1643 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1644 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1645 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1646 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1647 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1648 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1649 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1651 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1654 static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1656 u8 *state_of_charge,
1660 struct hidpp_report response;
1662 u8 *params = (u8 *)response.fap.params;
1664 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1665 CMD_UNIFIED_BATTERY_GET_STATUS,
1666 NULL, 0, &response);
1667 /* Ignore these intermittent errors */
1668 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1671 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1678 *state_of_charge = params[0];
1679 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1680 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1685 static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1692 if (hidpp->battery.feature_index == 0xff) {
1693 ret = hidpp_root_get_feature(hidpp,
1694 HIDPP_PAGE_UNIFIED_BATTERY,
1695 &hidpp->battery.feature_index,
1701 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1702 hidpp->battery.feature_index);
1706 ret = hidpp20_unifiedbattery_get_status(hidpp,
1707 hidpp->battery.feature_index,
1714 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1715 hidpp->battery.capacity = state_of_charge;
1716 hidpp->battery.status = status;
1717 hidpp->battery.level = level;
1718 hidpp->battery.online = true;
1723 static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1726 struct hidpp_report *report = (struct hidpp_report *)data;
1727 u8 *params = (u8 *)report->fap.params;
1728 int state_of_charge, status, level;
1731 if (report->fap.feature_index != hidpp->battery.feature_index ||
1732 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1735 state_of_charge = params[0];
1736 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1737 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1739 changed = status != hidpp->battery.status ||
1740 (state_of_charge != hidpp->battery.capacity &&
1741 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1742 (level != hidpp->battery.level &&
1743 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1746 hidpp->battery.capacity = state_of_charge;
1747 hidpp->battery.status = status;
1748 hidpp->battery.level = level;
1749 if (hidpp->battery.ps)
1750 power_supply_changed(hidpp->battery.ps);
1756 /* -------------------------------------------------------------------------- */
1757 /* Battery feature helpers */
1758 /* -------------------------------------------------------------------------- */
1760 static enum power_supply_property hidpp_battery_props[] = {
1761 POWER_SUPPLY_PROP_ONLINE,
1762 POWER_SUPPLY_PROP_STATUS,
1763 POWER_SUPPLY_PROP_SCOPE,
1764 POWER_SUPPLY_PROP_MODEL_NAME,
1765 POWER_SUPPLY_PROP_MANUFACTURER,
1766 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1767 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1768 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1769 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1772 static int hidpp_battery_get_property(struct power_supply *psy,
1773 enum power_supply_property psp,
1774 union power_supply_propval *val)
1776 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1780 case POWER_SUPPLY_PROP_STATUS:
1781 val->intval = hidpp->battery.status;
1783 case POWER_SUPPLY_PROP_CAPACITY:
1784 val->intval = hidpp->battery.capacity;
1786 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1787 val->intval = hidpp->battery.level;
1789 case POWER_SUPPLY_PROP_SCOPE:
1790 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1792 case POWER_SUPPLY_PROP_ONLINE:
1793 val->intval = hidpp->battery.online;
1795 case POWER_SUPPLY_PROP_MODEL_NAME:
1796 if (!strncmp(hidpp->name, "Logitech ", 9))
1797 val->strval = hidpp->name + 9;
1799 val->strval = hidpp->name;
1801 case POWER_SUPPLY_PROP_MANUFACTURER:
1802 val->strval = "Logitech";
1804 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1805 val->strval = hidpp->hid_dev->uniq;
1807 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1808 /* hardware reports voltage in mV. sysfs expects uV */
1809 val->intval = hidpp->battery.voltage * 1000;
1811 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1812 val->intval = hidpp->battery.charge_type;
1822 /* -------------------------------------------------------------------------- */
1823 /* 0x1d4b: Wireless device status */
1824 /* -------------------------------------------------------------------------- */
1825 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1827 static int hidpp_get_wireless_feature_index(struct hidpp_device *hidpp, u8 *feature_index)
1832 ret = hidpp_root_get_feature(hidpp,
1833 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1834 feature_index, &feature_type);
1839 /* -------------------------------------------------------------------------- */
1840 /* 0x1f20: ADC measurement */
1841 /* -------------------------------------------------------------------------- */
1843 #define HIDPP_PAGE_ADC_MEASUREMENT 0x1f20
1845 #define CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT 0x00
1847 #define EVENT_ADC_MEASUREMENT_STATUS_BROADCAST 0x00
1849 static int hidpp20_map_adc_measurement_1f20_capacity(struct hid_device *hid_dev, int voltage)
1851 /* NB: This voltage curve doesn't necessarily map perfectly to all
1852 * devices that implement the ADC_MEASUREMENT feature. This is because
1853 * there are a few devices that use different battery technology.
1856 * https://github.com/Sapd/HeadsetControl/blob/acd972be0468e039b93aae81221f20a54d2d60f7/src/devices/logitech_g633_g933_935.c#L44-L52
1858 static const int voltages[100] = {
1859 4030, 4024, 4018, 4011, 4003, 3994, 3985, 3975, 3963, 3951,
1860 3937, 3922, 3907, 3893, 3880, 3868, 3857, 3846, 3837, 3828,
1861 3820, 3812, 3805, 3798, 3791, 3785, 3779, 3773, 3768, 3762,
1862 3757, 3752, 3747, 3742, 3738, 3733, 3729, 3724, 3720, 3716,
1863 3712, 3708, 3704, 3700, 3696, 3692, 3688, 3685, 3681, 3677,
1864 3674, 3670, 3667, 3663, 3660, 3657, 3653, 3650, 3646, 3643,
1865 3640, 3637, 3633, 3630, 3627, 3624, 3620, 3617, 3614, 3611,
1866 3608, 3604, 3601, 3598, 3595, 3592, 3589, 3585, 3582, 3579,
1867 3576, 3573, 3569, 3566, 3563, 3560, 3556, 3553, 3550, 3546,
1868 3543, 3539, 3536, 3532, 3529, 3525, 3499, 3466, 3433, 3399,
1876 if (unlikely(voltage < 3400 || voltage >= 5000))
1877 hid_warn_once(hid_dev,
1878 "%s: possibly using the wrong voltage curve\n",
1881 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1882 if (voltage >= voltages[i])
1883 return ARRAY_SIZE(voltages) - i;
1889 static int hidpp20_map_adc_measurement_1f20(u8 data[3], int *voltage)
1898 status = POWER_SUPPLY_STATUS_DISCHARGING;
1901 status = POWER_SUPPLY_STATUS_CHARGING;
1904 status = POWER_SUPPLY_STATUS_FULL;
1908 status = POWER_SUPPLY_STATUS_UNKNOWN;
1912 *voltage = get_unaligned_be16(data);
1914 dbg_hid("Parsed 1f20 data as flag 0x%02x voltage %dmV\n",
1920 /* Return value is whether the device is online */
1921 static bool hidpp20_get_adc_measurement_1f20(struct hidpp_device *hidpp,
1923 int *status, int *voltage)
1925 struct hidpp_report response;
1927 u8 *params = (u8 *)response.fap.params;
1929 *status = POWER_SUPPLY_STATUS_UNKNOWN;
1931 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1932 CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT,
1933 NULL, 0, &response);
1936 hid_dbg(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1941 *status = hidpp20_map_adc_measurement_1f20(params, voltage);
1945 static int hidpp20_query_adc_measurement_info_1f20(struct hidpp_device *hidpp)
1949 if (hidpp->battery.adc_measurement_feature_index == 0xff) {
1952 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_ADC_MEASUREMENT,
1953 &hidpp->battery.adc_measurement_feature_index,
1958 hidpp->capabilities |= HIDPP_CAPABILITY_ADC_MEASUREMENT;
1961 hidpp->battery.online = hidpp20_get_adc_measurement_1f20(hidpp,
1962 hidpp->battery.adc_measurement_feature_index,
1963 &hidpp->battery.status,
1964 &hidpp->battery.voltage);
1965 hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hidpp->hid_dev,
1966 hidpp->battery.voltage);
1967 hidpp_update_usb_wireless_status(hidpp);
1972 static int hidpp20_adc_measurement_event_1f20(struct hidpp_device *hidpp,
1975 struct hidpp_report *report = (struct hidpp_report *)data;
1976 int status, voltage;
1978 if (report->fap.feature_index != hidpp->battery.adc_measurement_feature_index ||
1979 report->fap.funcindex_clientid != EVENT_ADC_MEASUREMENT_STATUS_BROADCAST)
1982 status = hidpp20_map_adc_measurement_1f20(report->fap.params, &voltage);
1984 hidpp->battery.online = status != POWER_SUPPLY_STATUS_UNKNOWN;
1986 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1987 hidpp->battery.status = status;
1988 hidpp->battery.voltage = voltage;
1989 hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hidpp->hid_dev, voltage);
1990 if (hidpp->battery.ps)
1991 power_supply_changed(hidpp->battery.ps);
1992 hidpp_update_usb_wireless_status(hidpp);
1997 /* -------------------------------------------------------------------------- */
1998 /* 0x2120: Hi-resolution scrolling */
1999 /* -------------------------------------------------------------------------- */
2001 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
2003 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
2005 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
2006 bool enabled, u8 *multiplier)
2012 struct hidpp_report response;
2014 ret = hidpp_root_get_feature(hidpp,
2015 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
2021 params[0] = enabled ? BIT(0) : 0;
2022 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2023 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
2024 params, sizeof(params), &response);
2027 *multiplier = response.fap.params[1];
2031 /* -------------------------------------------------------------------------- */
2032 /* 0x2121: HiRes Wheel */
2033 /* -------------------------------------------------------------------------- */
2035 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
2037 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
2038 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
2040 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
2046 struct hidpp_report response;
2048 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
2049 &feature_index, &feature_type);
2051 goto return_default;
2053 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2054 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
2055 NULL, 0, &response);
2057 goto return_default;
2059 *multiplier = response.fap.params[0];
2062 hid_warn(hidpp->hid_dev,
2063 "Couldn't get wheel multiplier (error %d)\n", ret);
2067 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
2068 bool high_resolution, bool use_hidpp)
2074 struct hidpp_report response;
2076 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
2077 &feature_index, &feature_type);
2081 params[0] = (invert ? BIT(2) : 0) |
2082 (high_resolution ? BIT(1) : 0) |
2083 (use_hidpp ? BIT(0) : 0);
2085 return hidpp_send_fap_command_sync(hidpp, feature_index,
2086 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
2087 params, sizeof(params), &response);
2090 /* -------------------------------------------------------------------------- */
2091 /* 0x4301: Solar Keyboard */
2092 /* -------------------------------------------------------------------------- */
2094 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
2096 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
2098 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
2099 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
2100 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
2102 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
2104 struct hidpp_report response;
2105 u8 params[2] = { 1, 1 };
2109 if (hidpp->battery.feature_index == 0xff) {
2110 ret = hidpp_root_get_feature(hidpp,
2111 HIDPP_PAGE_SOLAR_KEYBOARD,
2112 &hidpp->battery.solar_feature_index,
2118 ret = hidpp_send_fap_command_sync(hidpp,
2119 hidpp->battery.solar_feature_index,
2120 CMD_SOLAR_SET_LIGHT_MEASURE,
2121 params, 2, &response);
2123 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2130 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2135 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
2138 struct hidpp_report *report = (struct hidpp_report *)data;
2139 int capacity, lux, status;
2142 function = report->fap.funcindex_clientid;
2145 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
2146 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
2147 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
2148 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
2151 capacity = report->fap.params[0];
2154 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
2155 lux = (report->fap.params[1] << 8) | report->fap.params[2];
2157 status = POWER_SUPPLY_STATUS_CHARGING;
2159 status = POWER_SUPPLY_STATUS_DISCHARGING;
2161 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
2163 if (capacity < hidpp->battery.capacity)
2164 status = POWER_SUPPLY_STATUS_DISCHARGING;
2166 status = POWER_SUPPLY_STATUS_CHARGING;
2170 if (capacity == 100)
2171 status = POWER_SUPPLY_STATUS_FULL;
2173 hidpp->battery.online = true;
2174 if (capacity != hidpp->battery.capacity ||
2175 status != hidpp->battery.status) {
2176 hidpp->battery.capacity = capacity;
2177 hidpp->battery.status = status;
2178 if (hidpp->battery.ps)
2179 power_supply_changed(hidpp->battery.ps);
2185 /* -------------------------------------------------------------------------- */
2186 /* 0x6010: Touchpad FW items */
2187 /* -------------------------------------------------------------------------- */
2189 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
2191 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
2193 struct hidpp_touchpad_fw_items {
2195 uint8_t desired_state;
2201 * send a set state command to the device by reading the current items->state
2202 * field. items is then filled with the current state.
2204 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
2206 struct hidpp_touchpad_fw_items *items)
2208 struct hidpp_report response;
2210 u8 *params = (u8 *)response.fap.params;
2212 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2213 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
2216 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2223 items->presence = params[0];
2224 items->desired_state = params[1];
2225 items->state = params[2];
2226 items->persistent = params[3];
2231 /* -------------------------------------------------------------------------- */
2232 /* 0x6100: TouchPadRawXY */
2233 /* -------------------------------------------------------------------------- */
2235 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
2237 #define CMD_TOUCHPAD_GET_RAW_INFO 0x00
2238 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x20
2240 #define EVENT_TOUCHPAD_RAW_XY 0x00
2242 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
2243 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
2245 struct hidpp_touchpad_raw_info {
2256 struct hidpp_touchpad_raw_xy_finger {
2266 struct hidpp_touchpad_raw_xy {
2268 struct hidpp_touchpad_raw_xy_finger fingers[2];
2275 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2276 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2278 struct hidpp_report response;
2280 u8 *params = (u8 *)response.fap.params;
2282 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2283 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
2286 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2293 raw_info->x_size = get_unaligned_be16(¶ms[0]);
2294 raw_info->y_size = get_unaligned_be16(¶ms[2]);
2295 raw_info->z_range = params[4];
2296 raw_info->area_range = params[5];
2297 raw_info->maxcontacts = params[7];
2298 raw_info->origin = params[8];
2299 /* res is given in unit per inch */
2300 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
2305 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2306 u8 feature_index, bool send_raw_reports,
2307 bool sensor_enhanced_settings)
2309 struct hidpp_report response;
2313 * bit 0 - enable raw
2314 * bit 1 - 16bit Z, no area
2315 * bit 2 - enhanced sensitivity
2316 * bit 3 - width, height (4 bits each) instead of area
2317 * bit 4 - send raw + gestures (degrades smoothness)
2318 * remaining bits - reserved
2320 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2322 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2323 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
2326 static void hidpp_touchpad_touch_event(u8 *data,
2327 struct hidpp_touchpad_raw_xy_finger *finger)
2329 u8 x_m = data[0] << 2;
2330 u8 y_m = data[2] << 2;
2332 finger->x = x_m << 6 | data[1];
2333 finger->y = y_m << 6 | data[3];
2335 finger->contact_type = data[0] >> 6;
2336 finger->contact_status = data[2] >> 6;
2338 finger->z = data[4];
2339 finger->area = data[5];
2340 finger->finger_id = data[6] >> 4;
2343 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2344 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2346 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2347 raw_xy->end_of_frame = data[8] & 0x01;
2348 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2349 raw_xy->finger_count = data[15] & 0x0f;
2350 raw_xy->button = (data[8] >> 2) & 0x01;
2352 if (raw_xy->finger_count) {
2353 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2354 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2358 /* -------------------------------------------------------------------------- */
2359 /* 0x8123: Force feedback support */
2360 /* -------------------------------------------------------------------------- */
2362 #define HIDPP_FF_GET_INFO 0x01
2363 #define HIDPP_FF_RESET_ALL 0x11
2364 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2365 #define HIDPP_FF_SET_EFFECT_STATE 0x31
2366 #define HIDPP_FF_DESTROY_EFFECT 0x41
2367 #define HIDPP_FF_GET_APERTURE 0x51
2368 #define HIDPP_FF_SET_APERTURE 0x61
2369 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2370 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2372 #define HIDPP_FF_EFFECT_STATE_GET 0x00
2373 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
2374 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2375 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2377 #define HIDPP_FF_EFFECT_CONSTANT 0x00
2378 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2379 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2380 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2381 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2382 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2383 #define HIDPP_FF_EFFECT_SPRING 0x06
2384 #define HIDPP_FF_EFFECT_DAMPER 0x07
2385 #define HIDPP_FF_EFFECT_FRICTION 0x08
2386 #define HIDPP_FF_EFFECT_INERTIA 0x09
2387 #define HIDPP_FF_EFFECT_RAMP 0x0A
2389 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
2391 #define HIDPP_FF_EFFECTID_NONE -1
2392 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
2393 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2395 #define HIDPP_FF_MAX_PARAMS 20
2396 #define HIDPP_FF_RESERVED_SLOTS 1
2398 struct hidpp_ff_private_data {
2399 struct hidpp_device *hidpp;
2407 struct workqueue_struct *wq;
2408 atomic_t workqueue_size;
2411 struct hidpp_ff_work_data {
2412 struct work_struct work;
2413 struct hidpp_ff_private_data *data;
2416 u8 params[HIDPP_FF_MAX_PARAMS];
2420 static const signed short hidpp_ff_effects[] = {
2435 static const signed short hidpp_ff_effects_v2[] = {
2442 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2443 HIDPP_FF_EFFECT_SPRING,
2444 HIDPP_FF_EFFECT_FRICTION,
2445 HIDPP_FF_EFFECT_DAMPER,
2446 HIDPP_FF_EFFECT_INERTIA
2449 static const char *HIDPP_FF_CONDITION_NAMES[] = {
2457 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2461 for (i = 0; i < data->num_effects; i++)
2462 if (data->effect_ids[i] == effect_id)
2468 static void hidpp_ff_work_handler(struct work_struct *w)
2470 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2471 struct hidpp_ff_private_data *data = wd->data;
2472 struct hidpp_report response;
2476 /* add slot number if needed */
2477 switch (wd->effect_id) {
2478 case HIDPP_FF_EFFECTID_AUTOCENTER:
2479 wd->params[0] = data->slot_autocenter;
2481 case HIDPP_FF_EFFECTID_NONE:
2482 /* leave slot as zero */
2485 /* find current slot for effect */
2486 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2490 /* send command and wait for reply */
2491 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2492 wd->command, wd->params, wd->size, &response);
2495 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2499 /* parse return data */
2500 switch (wd->command) {
2501 case HIDPP_FF_DOWNLOAD_EFFECT:
2502 slot = response.fap.params[0];
2503 if (slot > 0 && slot <= data->num_effects) {
2504 if (wd->effect_id >= 0)
2505 /* regular effect uploaded */
2506 data->effect_ids[slot-1] = wd->effect_id;
2507 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2508 /* autocenter spring uploaded */
2509 data->slot_autocenter = slot;
2512 case HIDPP_FF_DESTROY_EFFECT:
2513 if (wd->effect_id >= 0)
2514 /* regular effect destroyed */
2515 data->effect_ids[wd->params[0]-1] = -1;
2516 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2517 /* autocenter spring destoyed */
2518 data->slot_autocenter = 0;
2520 case HIDPP_FF_SET_GLOBAL_GAINS:
2521 data->gain = (wd->params[0] << 8) + wd->params[1];
2523 case HIDPP_FF_SET_APERTURE:
2524 data->range = (wd->params[0] << 8) + wd->params[1];
2527 /* no action needed */
2532 atomic_dec(&data->workqueue_size);
2536 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2538 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2544 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2547 wd->effect_id = effect_id;
2548 wd->command = command;
2550 memcpy(wd->params, params, size);
2552 s = atomic_inc_return(&data->workqueue_size);
2553 queue_work(data->wq, &wd->work);
2555 /* warn about excessive queue size */
2556 if (s >= 20 && s % 20 == 0)
2557 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2562 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2564 struct hidpp_ff_private_data *data = dev->ff->private;
2569 /* set common parameters */
2570 params[2] = effect->replay.length >> 8;
2571 params[3] = effect->replay.length & 255;
2572 params[4] = effect->replay.delay >> 8;
2573 params[5] = effect->replay.delay & 255;
2575 switch (effect->type) {
2577 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2578 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2579 params[6] = force >> 8;
2580 params[7] = force & 255;
2581 params[8] = effect->u.constant.envelope.attack_level >> 7;
2582 params[9] = effect->u.constant.envelope.attack_length >> 8;
2583 params[10] = effect->u.constant.envelope.attack_length & 255;
2584 params[11] = effect->u.constant.envelope.fade_level >> 7;
2585 params[12] = effect->u.constant.envelope.fade_length >> 8;
2586 params[13] = effect->u.constant.envelope.fade_length & 255;
2588 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2589 effect->u.constant.level,
2590 effect->direction, force);
2591 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2592 effect->u.constant.envelope.attack_level,
2593 effect->u.constant.envelope.attack_length,
2594 effect->u.constant.envelope.fade_level,
2595 effect->u.constant.envelope.fade_length);
2599 switch (effect->u.periodic.waveform) {
2601 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2604 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2607 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2610 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2613 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2616 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2619 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2620 params[6] = effect->u.periodic.magnitude >> 8;
2621 params[7] = effect->u.periodic.magnitude & 255;
2622 params[8] = effect->u.periodic.offset >> 8;
2623 params[9] = effect->u.periodic.offset & 255;
2624 params[10] = effect->u.periodic.period >> 8;
2625 params[11] = effect->u.periodic.period & 255;
2626 params[12] = effect->u.periodic.phase >> 8;
2627 params[13] = effect->u.periodic.phase & 255;
2628 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2629 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2630 params[16] = effect->u.periodic.envelope.attack_length & 255;
2631 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2632 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2633 params[19] = effect->u.periodic.envelope.fade_length & 255;
2635 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2636 effect->u.periodic.magnitude, effect->direction,
2637 effect->u.periodic.offset,
2638 effect->u.periodic.period,
2639 effect->u.periodic.phase);
2640 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2641 effect->u.periodic.envelope.attack_level,
2642 effect->u.periodic.envelope.attack_length,
2643 effect->u.periodic.envelope.fade_level,
2644 effect->u.periodic.envelope.fade_length);
2648 params[1] = HIDPP_FF_EFFECT_RAMP;
2649 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2650 params[6] = force >> 8;
2651 params[7] = force & 255;
2652 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2653 params[8] = force >> 8;
2654 params[9] = force & 255;
2655 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2656 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2657 params[12] = effect->u.ramp.envelope.attack_length & 255;
2658 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2659 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2660 params[15] = effect->u.ramp.envelope.fade_length & 255;
2662 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2663 effect->u.ramp.start_level,
2664 effect->u.ramp.end_level,
2665 effect->direction, force);
2666 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2667 effect->u.ramp.envelope.attack_level,
2668 effect->u.ramp.envelope.attack_length,
2669 effect->u.ramp.envelope.fade_level,
2670 effect->u.ramp.envelope.fade_length);
2676 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2677 params[6] = effect->u.condition[0].left_saturation >> 9;
2678 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2679 params[8] = effect->u.condition[0].left_coeff >> 8;
2680 params[9] = effect->u.condition[0].left_coeff & 255;
2681 params[10] = effect->u.condition[0].deadband >> 9;
2682 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2683 params[12] = effect->u.condition[0].center >> 8;
2684 params[13] = effect->u.condition[0].center & 255;
2685 params[14] = effect->u.condition[0].right_coeff >> 8;
2686 params[15] = effect->u.condition[0].right_coeff & 255;
2687 params[16] = effect->u.condition[0].right_saturation >> 9;
2688 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2690 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2691 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2692 effect->u.condition[0].left_coeff,
2693 effect->u.condition[0].left_saturation,
2694 effect->u.condition[0].right_coeff,
2695 effect->u.condition[0].right_saturation);
2696 dbg_hid(" deadband=%d, center=%d\n",
2697 effect->u.condition[0].deadband,
2698 effect->u.condition[0].center);
2701 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2705 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2708 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2710 struct hidpp_ff_private_data *data = dev->ff->private;
2713 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2715 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2717 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2720 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2722 struct hidpp_ff_private_data *data = dev->ff->private;
2725 dbg_hid("Erasing effect %d.\n", effect_id);
2727 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2730 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2732 struct hidpp_ff_private_data *data = dev->ff->private;
2733 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2735 dbg_hid("Setting autocenter to %d.\n", magnitude);
2737 /* start a standard spring effect */
2738 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2739 /* zero delay and duration */
2740 params[2] = params[3] = params[4] = params[5] = 0;
2741 /* set coeff to 25% of saturation */
2742 params[8] = params[14] = magnitude >> 11;
2743 params[9] = params[15] = (magnitude >> 3) & 255;
2744 params[6] = params[16] = magnitude >> 9;
2745 params[7] = params[17] = (magnitude >> 1) & 255;
2746 /* zero deadband and center */
2747 params[10] = params[11] = params[12] = params[13] = 0;
2749 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2752 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2754 struct hidpp_ff_private_data *data = dev->ff->private;
2757 dbg_hid("Setting gain to %d.\n", gain);
2759 params[0] = gain >> 8;
2760 params[1] = gain & 255;
2761 params[2] = 0; /* no boost */
2764 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2767 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2769 struct hid_device *hid = to_hid_device(dev);
2770 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2771 struct input_dev *idev = hidinput->input;
2772 struct hidpp_ff_private_data *data = idev->ff->private;
2774 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2777 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2779 struct hid_device *hid = to_hid_device(dev);
2780 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2781 struct input_dev *idev = hidinput->input;
2782 struct hidpp_ff_private_data *data = idev->ff->private;
2784 int range = simple_strtoul(buf, NULL, 10);
2786 range = clamp(range, 180, 900);
2788 params[0] = range >> 8;
2789 params[1] = range & 0x00FF;
2791 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2796 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2798 static void hidpp_ff_destroy(struct ff_device *ff)
2800 struct hidpp_ff_private_data *data = ff->private;
2801 struct hid_device *hid = data->hidpp->hid_dev;
2803 hid_info(hid, "Unloading HID++ force feedback.\n");
2805 device_remove_file(&hid->dev, &dev_attr_range);
2806 destroy_workqueue(data->wq);
2807 kfree(data->effect_ids);
2810 static int hidpp_ff_init(struct hidpp_device *hidpp,
2811 struct hidpp_ff_private_data *data)
2813 struct hid_device *hid = hidpp->hid_dev;
2814 struct hid_input *hidinput;
2815 struct input_dev *dev;
2816 struct usb_device_descriptor *udesc;
2818 struct ff_device *ff;
2819 int error, j, num_slots = data->num_effects;
2822 if (!hid_is_usb(hid)) {
2823 hid_err(hid, "device is not USB\n");
2827 if (list_empty(&hid->inputs)) {
2828 hid_err(hid, "no inputs found\n");
2831 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2832 dev = hidinput->input;
2835 hid_err(hid, "Struct input_dev not set!\n");
2839 /* Get firmware release */
2840 udesc = &(hid_to_usb_dev(hid)->descriptor);
2841 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2842 version = bcdDevice & 255;
2844 /* Set supported force feedback capabilities */
2845 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2846 set_bit(hidpp_ff_effects[j], dev->ffbit);
2848 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2849 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2851 error = input_ff_create(dev, num_slots);
2854 hid_err(dev, "Failed to create FF device!\n");
2858 * Create a copy of passed data, so we can transfer memory
2859 * ownership to FF core
2861 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2864 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2865 if (!data->effect_ids) {
2869 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2871 kfree(data->effect_ids);
2876 data->hidpp = hidpp;
2877 data->version = version;
2878 for (j = 0; j < num_slots; j++)
2879 data->effect_ids[j] = -1;
2884 ff->upload = hidpp_ff_upload_effect;
2885 ff->erase = hidpp_ff_erase_effect;
2886 ff->playback = hidpp_ff_playback;
2887 ff->set_gain = hidpp_ff_set_gain;
2888 ff->set_autocenter = hidpp_ff_set_autocenter;
2889 ff->destroy = hidpp_ff_destroy;
2891 /* Create sysfs interface */
2892 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2894 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2896 /* init the hardware command queue */
2897 atomic_set(&data->workqueue_size, 0);
2899 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2905 /* ************************************************************************** */
2907 /* Device Support */
2909 /* ************************************************************************** */
2911 /* -------------------------------------------------------------------------- */
2912 /* Touchpad HID++ devices */
2913 /* -------------------------------------------------------------------------- */
2915 #define WTP_MANUAL_RESOLUTION 39
2920 u8 mt_feature_index;
2921 u8 button_feature_index;
2924 unsigned int resolution;
2927 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2928 struct hid_field *field, struct hid_usage *usage,
2929 unsigned long **bit, int *max)
2934 static void wtp_populate_input(struct hidpp_device *hidpp,
2935 struct input_dev *input_dev)
2937 struct wtp_data *wd = hidpp->private_data;
2939 __set_bit(EV_ABS, input_dev->evbit);
2940 __set_bit(EV_KEY, input_dev->evbit);
2941 __clear_bit(EV_REL, input_dev->evbit);
2942 __clear_bit(EV_LED, input_dev->evbit);
2944 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2945 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2946 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2947 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2949 /* Max pressure is not given by the devices, pick one */
2950 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2952 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2954 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2955 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2957 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2959 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2960 INPUT_MT_DROP_UNUSED);
2963 static void wtp_touch_event(struct hidpp_device *hidpp,
2964 struct hidpp_touchpad_raw_xy_finger *touch_report)
2966 struct wtp_data *wd = hidpp->private_data;
2969 if (!touch_report->finger_id || touch_report->contact_type)
2970 /* no actual data */
2973 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2975 input_mt_slot(hidpp->input, slot);
2976 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2977 touch_report->contact_status);
2978 if (touch_report->contact_status) {
2979 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2981 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2982 wd->flip_y ? wd->y_size - touch_report->y :
2984 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2985 touch_report->area);
2989 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2990 struct hidpp_touchpad_raw_xy *raw)
2994 for (i = 0; i < 2; i++)
2995 wtp_touch_event(hidpp, &(raw->fingers[i]));
2997 if (raw->end_of_frame &&
2998 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2999 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
3001 if (raw->end_of_frame || raw->finger_count <= 2) {
3002 input_mt_sync_frame(hidpp->input);
3003 input_sync(hidpp->input);
3007 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
3009 struct wtp_data *wd = hidpp->private_data;
3010 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
3011 (data[7] >> 4) * (data[7] >> 4)) / 2;
3012 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
3013 (data[13] >> 4) * (data[13] >> 4)) / 2;
3014 struct hidpp_touchpad_raw_xy raw = {
3015 .timestamp = data[1],
3019 .contact_status = !!data[7],
3020 .x = get_unaligned_le16(&data[3]),
3021 .y = get_unaligned_le16(&data[5]),
3024 .finger_id = data[2],
3027 .contact_status = !!data[13],
3028 .x = get_unaligned_le16(&data[9]),
3029 .y = get_unaligned_le16(&data[11]),
3032 .finger_id = data[8],
3035 .finger_count = wd->maxcontacts,
3037 .end_of_frame = (data[0] >> 7) == 0,
3038 .button = data[0] & 0x01,
3041 wtp_send_raw_xy_event(hidpp, &raw);
3046 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
3048 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3049 struct wtp_data *wd = hidpp->private_data;
3050 struct hidpp_report *report = (struct hidpp_report *)data;
3051 struct hidpp_touchpad_raw_xy raw;
3053 if (!wd || !hidpp->input)
3059 hid_err(hdev, "Received HID report of bad size (%d)",
3063 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
3064 input_event(hidpp->input, EV_KEY, BTN_LEFT,
3065 !!(data[1] & 0x01));
3066 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
3067 !!(data[1] & 0x02));
3068 input_sync(hidpp->input);
3073 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
3075 case REPORT_ID_HIDPP_LONG:
3076 /* size is already checked in hidpp_raw_event. */
3077 if ((report->fap.feature_index != wd->mt_feature_index) ||
3078 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
3080 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
3082 wtp_send_raw_xy_event(hidpp, &raw);
3089 static int wtp_get_config(struct hidpp_device *hidpp)
3091 struct wtp_data *wd = hidpp->private_data;
3092 struct hidpp_touchpad_raw_info raw_info = {0};
3096 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
3097 &wd->mt_feature_index, &feature_type);
3099 /* means that the device is not powered up */
3102 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
3107 wd->x_size = raw_info.x_size;
3108 wd->y_size = raw_info.y_size;
3109 wd->maxcontacts = raw_info.maxcontacts;
3110 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
3111 wd->resolution = raw_info.res;
3112 if (!wd->resolution)
3113 wd->resolution = WTP_MANUAL_RESOLUTION;
3118 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
3120 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3121 struct wtp_data *wd;
3123 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
3128 hidpp->private_data = wd;
3133 static int wtp_connect(struct hid_device *hdev)
3135 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3136 struct wtp_data *wd = hidpp->private_data;
3140 ret = wtp_get_config(hidpp);
3142 hid_err(hdev, "Can not get wtp config: %d\n", ret);
3147 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
3151 /* ------------------------------------------------------------------------- */
3152 /* Logitech M560 devices */
3153 /* ------------------------------------------------------------------------- */
3156 * Logitech M560 protocol overview
3158 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
3159 * the sides buttons are pressed, it sends some keyboard keys events
3160 * instead of buttons ones.
3161 * To complicate things further, the middle button keys sequence
3162 * is different from the odd press and the even press.
3164 * forward button -> Super_R
3165 * backward button -> Super_L+'d' (press only)
3166 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
3167 * 2nd time: left-click (press only)
3168 * NB: press-only means that when the button is pressed, the
3169 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
3170 * together sequentially; instead when the button is released, no event is
3174 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
3175 * the mouse reacts differently:
3176 * - it never sends a keyboard key event
3177 * - for the three mouse button it sends:
3178 * middle button press 11<xx>0a 3500af00...
3179 * side 1 button (forward) press 11<xx>0a 3500b000...
3180 * side 2 button (backward) press 11<xx>0a 3500ae00...
3181 * middle/side1/side2 button release 11<xx>0a 35000000...
3184 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
3186 /* how buttons are mapped in the report */
3187 #define M560_MOUSE_BTN_LEFT 0x01
3188 #define M560_MOUSE_BTN_RIGHT 0x02
3189 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
3190 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
3192 #define M560_SUB_ID 0x0a
3193 #define M560_BUTTON_MODE_REGISTER 0x35
3195 static int m560_send_config_command(struct hid_device *hdev)
3197 struct hidpp_report response;
3198 struct hidpp_device *hidpp_dev;
3200 hidpp_dev = hid_get_drvdata(hdev);
3202 return hidpp_send_rap_command_sync(
3204 REPORT_ID_HIDPP_SHORT,
3206 M560_BUTTON_MODE_REGISTER,
3207 (u8 *)m560_config_parameter,
3208 sizeof(m560_config_parameter),
3213 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
3215 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3218 if (!hidpp->input) {
3219 hid_err(hdev, "error in parameter\n");
3224 hid_err(hdev, "error in report\n");
3228 if (data[0] == REPORT_ID_HIDPP_LONG &&
3229 data[2] == M560_SUB_ID && data[6] == 0x00) {
3231 * m560 mouse report for middle, forward and backward button
3234 * data[1] = device-id
3236 * data[5] = 0xaf -> middle
3239 * 0x00 -> release all
3245 input_report_key(hidpp->input, BTN_MIDDLE, 1);
3248 input_report_key(hidpp->input, BTN_FORWARD, 1);
3251 input_report_key(hidpp->input, BTN_BACK, 1);
3254 input_report_key(hidpp->input, BTN_BACK, 0);
3255 input_report_key(hidpp->input, BTN_FORWARD, 0);
3256 input_report_key(hidpp->input, BTN_MIDDLE, 0);
3259 hid_err(hdev, "error in report\n");
3262 input_sync(hidpp->input);
3264 } else if (data[0] == 0x02) {
3266 * Logitech M560 mouse report
3268 * data[0] = type (0x02)
3269 * data[1..2] = buttons
3276 input_report_key(hidpp->input, BTN_LEFT,
3277 !!(data[1] & M560_MOUSE_BTN_LEFT));
3278 input_report_key(hidpp->input, BTN_RIGHT,
3279 !!(data[1] & M560_MOUSE_BTN_RIGHT));
3281 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3282 input_report_rel(hidpp->input, REL_HWHEEL, -1);
3283 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3285 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3286 input_report_rel(hidpp->input, REL_HWHEEL, 1);
3287 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3291 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
3292 input_report_rel(hidpp->input, REL_X, v);
3294 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
3295 input_report_rel(hidpp->input, REL_Y, v);
3297 v = hid_snto32(data[6], 8);
3299 hidpp_scroll_counter_handle_scroll(hidpp->input,
3300 &hidpp->vertical_wheel_counter, v);
3302 input_sync(hidpp->input);
3308 static void m560_populate_input(struct hidpp_device *hidpp,
3309 struct input_dev *input_dev)
3311 __set_bit(EV_KEY, input_dev->evbit);
3312 __set_bit(BTN_MIDDLE, input_dev->keybit);
3313 __set_bit(BTN_RIGHT, input_dev->keybit);
3314 __set_bit(BTN_LEFT, input_dev->keybit);
3315 __set_bit(BTN_BACK, input_dev->keybit);
3316 __set_bit(BTN_FORWARD, input_dev->keybit);
3318 __set_bit(EV_REL, input_dev->evbit);
3319 __set_bit(REL_X, input_dev->relbit);
3320 __set_bit(REL_Y, input_dev->relbit);
3321 __set_bit(REL_WHEEL, input_dev->relbit);
3322 __set_bit(REL_HWHEEL, input_dev->relbit);
3323 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3324 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3327 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3328 struct hid_field *field, struct hid_usage *usage,
3329 unsigned long **bit, int *max)
3334 /* ------------------------------------------------------------------------- */
3335 /* Logitech K400 devices */
3336 /* ------------------------------------------------------------------------- */
3339 * The Logitech K400 keyboard has an embedded touchpad which is seen
3340 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3341 * tap-to-click but the setting is not remembered accross reset, annoying some
3344 * We can toggle this feature from the host by using the feature 0x6010:
3348 struct k400_private_data {
3352 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3354 struct k400_private_data *k400 = hidpp->private_data;
3355 struct hidpp_touchpad_fw_items items = {};
3359 if (!k400->feature_index) {
3360 ret = hidpp_root_get_feature(hidpp,
3361 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3362 &k400->feature_index, &feature_type);
3364 /* means that the device is not powered up */
3368 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3375 static int k400_allocate(struct hid_device *hdev)
3377 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3378 struct k400_private_data *k400;
3380 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3385 hidpp->private_data = k400;
3390 static int k400_connect(struct hid_device *hdev)
3392 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3394 if (!disable_tap_to_click)
3397 return k400_disable_tap_to_click(hidpp);
3400 /* ------------------------------------------------------------------------- */
3401 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
3402 /* ------------------------------------------------------------------------- */
3404 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3406 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3407 struct hidpp_ff_private_data *data)
3409 struct hidpp_report response;
3410 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3411 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3415 /* initialize with zero autocenter to get wheel in usable state */
3417 dbg_hid("Setting autocenter to 0.\n");
3418 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3419 HIDPP_FF_DOWNLOAD_EFFECT,
3420 params, ARRAY_SIZE(params),
3423 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3425 data->slot_autocenter = response.fap.params[0];
3430 static int g920_get_config(struct hidpp_device *hidpp,
3431 struct hidpp_ff_private_data *data)
3433 struct hidpp_report response;
3437 memset(data, 0, sizeof(*data));
3439 /* Find feature and store for later use */
3440 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3441 &data->feature_index, &feature_type);
3445 /* Read number of slots available in device */
3446 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3453 hid_err(hidpp->hid_dev,
3454 "%s: received protocol error 0x%02x\n", __func__, ret);
3458 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3460 /* reset all forces */
3461 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3466 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3468 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3469 HIDPP_FF_GET_APERTURE,
3473 hid_warn(hidpp->hid_dev,
3474 "Failed to read range from device!\n");
3477 900 : get_unaligned_be16(&response.fap.params[0]);
3479 /* Read the current gain values */
3480 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3481 HIDPP_FF_GET_GLOBAL_GAINS,
3485 hid_warn(hidpp->hid_dev,
3486 "Failed to read gain values from device!\n");
3488 0xffff : get_unaligned_be16(&response.fap.params[0]);
3490 /* ignore boost value at response.fap.params[2] */
3492 return g920_ff_set_autocenter(hidpp, data);
3495 /* -------------------------------------------------------------------------- */
3496 /* Logitech Dinovo Mini keyboard with builtin touchpad */
3497 /* -------------------------------------------------------------------------- */
3498 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3500 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3501 struct hid_field *field, struct hid_usage *usage,
3502 unsigned long **bit, int *max)
3504 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3507 switch (usage->hid & HID_USAGE) {
3508 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3515 /* -------------------------------------------------------------------------- */
3516 /* HID++1.0 devices which use HID++ reports for their wheels */
3517 /* -------------------------------------------------------------------------- */
3518 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3520 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3521 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3522 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3525 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3536 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3542 input_report_rel(hidpp->input, REL_WHEEL, value);
3543 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3544 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3545 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3546 input_sync(hidpp->input);
3551 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3552 struct input_dev *input_dev)
3554 __set_bit(EV_REL, input_dev->evbit);
3555 __set_bit(REL_WHEEL, input_dev->relbit);
3556 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3557 __set_bit(REL_HWHEEL, input_dev->relbit);
3558 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3561 /* -------------------------------------------------------------------------- */
3562 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3563 /* -------------------------------------------------------------------------- */
3564 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3566 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3567 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3568 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3571 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3582 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3583 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3587 * Buttons are either delivered through the regular mouse report *or*
3588 * through the extra buttons report. At least for button 6 how it is
3589 * delivered differs per receiver firmware version. Even receivers with
3590 * the same usb-id show different behavior, so we handle both cases.
3592 for (i = 0; i < 8; i++)
3593 input_report_key(hidpp->input, BTN_MOUSE + i,
3594 (data[3] & (1 << i)));
3596 /* Some mice report events on button 9+, use BTN_MISC */
3597 for (i = 0; i < 8; i++)
3598 input_report_key(hidpp->input, BTN_MISC + i,
3599 (data[4] & (1 << i)));
3601 input_sync(hidpp->input);
3605 static void hidpp10_extra_mouse_buttons_populate_input(
3606 struct hidpp_device *hidpp, struct input_dev *input_dev)
3608 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3609 __set_bit(BTN_0, input_dev->keybit);
3610 __set_bit(BTN_1, input_dev->keybit);
3611 __set_bit(BTN_2, input_dev->keybit);
3612 __set_bit(BTN_3, input_dev->keybit);
3613 __set_bit(BTN_4, input_dev->keybit);
3614 __set_bit(BTN_5, input_dev->keybit);
3615 __set_bit(BTN_6, input_dev->keybit);
3616 __set_bit(BTN_7, input_dev->keybit);
3619 /* -------------------------------------------------------------------------- */
3620 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3621 /* -------------------------------------------------------------------------- */
3623 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3624 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3625 u8 *_rdesc, unsigned int *rsize)
3627 /* Note 0 terminated so we can use strnstr to search for this. */
3628 static const char consumer_rdesc_start[] = {
3629 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3630 0x09, 0x01, /* USAGE (Consumer Control) */
3631 0xA1, 0x01, /* COLLECTION (Application) */
3632 0x85, 0x03, /* REPORT_ID = 3 */
3633 0x75, 0x10, /* REPORT_SIZE (16) */
3634 0x95, 0x02, /* REPORT_COUNT (2) */
3635 0x15, 0x01, /* LOGICAL_MIN (1) */
3636 0x26, 0x00 /* LOGICAL_MAX (... */
3638 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3641 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3642 size = *rsize - (consumer_rdesc - rdesc);
3643 if (consumer_rdesc && size >= 25) {
3644 consumer_rdesc[15] = 0x7f;
3645 consumer_rdesc[16] = 0x10;
3646 consumer_rdesc[20] = 0x7f;
3647 consumer_rdesc[21] = 0x10;
3652 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3654 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3655 HIDPP_ENABLE_CONSUMER_REPORT,
3656 HIDPP_ENABLE_CONSUMER_REPORT);
3659 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3662 u8 consumer_report[5];
3667 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3668 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3672 * Build a normal consumer report (3) out of the data, this detour
3673 * is necessary to get some keyboards to report their 0x10xx usages.
3675 consumer_report[0] = 0x03;
3676 memcpy(&consumer_report[1], &data[3], 4);
3677 /* We are called from atomic context */
3678 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3679 consumer_report, 5, 1);
3684 /* -------------------------------------------------------------------------- */
3685 /* High-resolution scroll wheels */
3686 /* -------------------------------------------------------------------------- */
3688 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3693 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
3694 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3696 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3697 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
3698 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3700 } else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
3701 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3705 hid_dbg(hidpp->hid_dev,
3706 "Could not enable hi-res scrolling: %d\n", ret);
3710 if (multiplier == 0) {
3711 hid_dbg(hidpp->hid_dev,
3712 "Invalid multiplier 0 from device, setting it to 1\n");
3716 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3717 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3721 static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
3724 unsigned long capabilities;
3726 capabilities = hidpp->capabilities;
3728 if (hidpp->protocol_major >= 2) {
3732 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
3733 &feature_index, &feature_type);
3735 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
3736 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
3739 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
3740 &feature_index, &feature_type);
3742 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
3743 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
3746 /* We cannot detect fast scrolling support on HID++ 1.0 devices */
3747 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) {
3748 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
3749 hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
3753 if (hidpp->capabilities == capabilities)
3754 hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
3758 /* -------------------------------------------------------------------------- */
3759 /* Generic HID++ devices */
3760 /* -------------------------------------------------------------------------- */
3762 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3763 unsigned int *rsize)
3765 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3770 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3771 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3772 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3773 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3778 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3779 struct hid_field *field, struct hid_usage *usage,
3780 unsigned long **bit, int *max)
3782 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3787 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3788 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3789 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3790 field->application != HID_GD_MOUSE)
3791 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3793 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3794 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3799 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3800 struct hid_field *field, struct hid_usage *usage,
3801 unsigned long **bit, int *max)
3803 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3808 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3809 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3810 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3811 usage->code == ABS_Y || usage->code == ABS_Z ||
3812 usage->code == ABS_RZ)) {
3813 field->application = HID_GD_MULTIAXIS;
3821 static void hidpp_populate_input(struct hidpp_device *hidpp,
3822 struct input_dev *input)
3824 hidpp->input = input;
3826 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3827 wtp_populate_input(hidpp, input);
3828 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3829 m560_populate_input(hidpp, input);
3831 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3832 hidpp10_wheel_populate_input(hidpp, input);
3834 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3835 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3838 static int hidpp_input_configured(struct hid_device *hdev,
3839 struct hid_input *hidinput)
3841 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3842 struct input_dev *input = hidinput->input;
3847 hidpp_populate_input(hidpp, input);
3852 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3855 struct hidpp_report *question = hidpp->send_receive_buf;
3856 struct hidpp_report *answer = hidpp->send_receive_buf;
3857 struct hidpp_report *report = (struct hidpp_report *)data;
3861 * If the mutex is locked then we have a pending answer from a
3862 * previously sent command.
3864 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3866 * Check for a correct hidpp20 answer or the corresponding
3869 if (hidpp_match_answer(question, report) ||
3870 hidpp_match_error(question, report)) {
3872 hidpp->answer_available = true;
3873 wake_up(&hidpp->wait);
3875 * This was an answer to a command that this driver sent
3876 * We return 1 to hid-core to avoid forwarding the
3877 * command upstream as it has been treated by the driver
3884 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3885 if (schedule_work(&hidpp->work) == 0)
3886 dbg_hid("%s: connect event already queued\n", __func__);
3890 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3891 data[0] == REPORT_ID_HIDPP_SHORT &&
3892 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3893 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3894 dev_err_ratelimited(&hidpp->hid_dev->dev,
3895 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3896 dev_err_ratelimited(&hidpp->hid_dev->dev,
3897 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3900 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3901 ret = hidpp20_battery_event_1000(hidpp, data, size);
3904 ret = hidpp20_battery_event_1004(hidpp, data, size);
3907 ret = hidpp_solar_battery_event(hidpp, data, size);
3910 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3913 ret = hidpp20_adc_measurement_event_1f20(hidpp, data, size);
3918 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3919 ret = hidpp10_battery_event(hidpp, data, size);
3924 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3925 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3930 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3931 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3936 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3937 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3945 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3948 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3954 /* Generic HID++ processing. */
3956 case REPORT_ID_HIDPP_VERY_LONG:
3957 if (size != hidpp->very_long_report_length) {
3958 hid_err(hdev, "received hid++ report of bad size (%d)",
3962 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3964 case REPORT_ID_HIDPP_LONG:
3965 if (size != HIDPP_REPORT_LONG_LENGTH) {
3966 hid_err(hdev, "received hid++ report of bad size (%d)",
3970 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3972 case REPORT_ID_HIDPP_SHORT:
3973 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3974 hid_err(hdev, "received hid++ report of bad size (%d)",
3978 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3982 /* If no report is available for further processing, skip calling
3983 * raw_event of subclasses. */
3987 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3988 return wtp_raw_event(hdev, data, size);
3989 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3990 return m560_raw_event(hdev, data, size);
3995 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3996 struct hid_usage *usage, __s32 value)
3998 /* This function will only be called for scroll events, due to the
3999 * restriction imposed in hidpp_usages.
4001 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4002 struct hidpp_scroll_counter *counter;
4007 counter = &hidpp->vertical_wheel_counter;
4008 /* A scroll event may occur before the multiplier has been retrieved or
4009 * the input device set, or high-res scroll enabling may fail. In such
4010 * cases we must return early (falling back to default behaviour) to
4011 * avoid a crash in hidpp_scroll_counter_handle_scroll.
4013 if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4014 || value == 0 || hidpp->input == NULL
4015 || counter->wheel_multiplier == 0)
4018 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
4022 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
4024 static atomic_t battery_no = ATOMIC_INIT(0);
4025 struct power_supply_config cfg = { .drv_data = hidpp };
4026 struct power_supply_desc *desc = &hidpp->battery.desc;
4027 enum power_supply_property *battery_props;
4028 struct hidpp_battery *battery;
4029 unsigned int num_battery_props;
4033 if (hidpp->battery.ps)
4036 hidpp->battery.feature_index = 0xff;
4037 hidpp->battery.solar_feature_index = 0xff;
4038 hidpp->battery.voltage_feature_index = 0xff;
4039 hidpp->battery.adc_measurement_feature_index = 0xff;
4041 if (hidpp->protocol_major >= 2) {
4042 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
4043 ret = hidpp_solar_request_battery_event(hidpp);
4045 /* we only support one battery feature right now, so let's
4046 first check the ones that support battery level first
4047 and leave voltage for last */
4048 ret = hidpp20_query_battery_info_1000(hidpp);
4050 ret = hidpp20_query_battery_info_1004(hidpp);
4052 ret = hidpp20_query_battery_voltage_info(hidpp);
4054 ret = hidpp20_query_adc_measurement_info_1f20(hidpp);
4059 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
4061 ret = hidpp10_query_battery_status(hidpp);
4063 ret = hidpp10_query_battery_mileage(hidpp);
4066 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
4068 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
4070 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
4073 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
4074 hidpp_battery_props,
4075 sizeof(hidpp_battery_props),
4080 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
4082 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
4083 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
4084 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4085 hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4086 battery_props[num_battery_props++] =
4087 POWER_SUPPLY_PROP_CAPACITY;
4089 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
4090 battery_props[num_battery_props++] =
4091 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
4093 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4094 hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4095 battery_props[num_battery_props++] =
4096 POWER_SUPPLY_PROP_VOLTAGE_NOW;
4098 battery = &hidpp->battery;
4100 n = atomic_inc_return(&battery_no) - 1;
4101 desc->properties = battery_props;
4102 desc->num_properties = num_battery_props;
4103 desc->get_property = hidpp_battery_get_property;
4104 sprintf(battery->name, "hidpp_battery_%ld", n);
4105 desc->name = battery->name;
4106 desc->type = POWER_SUPPLY_TYPE_BATTERY;
4107 desc->use_for_apm = 0;
4109 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
4112 if (IS_ERR(battery->ps))
4113 return PTR_ERR(battery->ps);
4115 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
4120 /* Get name + serial for USB and Bluetooth HID++ devices */
4121 static void hidpp_non_unifying_init(struct hidpp_device *hidpp)
4123 struct hid_device *hdev = hidpp->hid_dev;
4126 /* Bluetooth devices already have their serialnr set */
4127 if (hid_is_usb(hdev))
4128 hidpp_serial_init(hidpp);
4130 name = hidpp_get_device_name(hidpp);
4132 dbg_hid("HID++: Got name: %s\n", name);
4133 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
4138 static int hidpp_input_open(struct input_dev *dev)
4140 struct hid_device *hid = input_get_drvdata(dev);
4142 return hid_hw_open(hid);
4145 static void hidpp_input_close(struct input_dev *dev)
4147 struct hid_device *hid = input_get_drvdata(dev);
4152 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
4154 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
4155 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4160 input_set_drvdata(input_dev, hdev);
4161 input_dev->open = hidpp_input_open;
4162 input_dev->close = hidpp_input_close;
4164 input_dev->name = hidpp->name;
4165 input_dev->phys = hdev->phys;
4166 input_dev->uniq = hdev->uniq;
4167 input_dev->id.bustype = hdev->bus;
4168 input_dev->id.vendor = hdev->vendor;
4169 input_dev->id.product = hdev->product;
4170 input_dev->id.version = hdev->version;
4171 input_dev->dev.parent = &hdev->dev;
4176 static void hidpp_connect_event(struct work_struct *work)
4178 struct hidpp_device *hidpp = container_of(work, struct hidpp_device, work);
4179 struct hid_device *hdev = hidpp->hid_dev;
4180 struct input_dev *input;
4181 char *name, *devm_name;
4184 /* Get device version to check if it is connected */
4185 ret = hidpp_root_get_protocol_version(hidpp);
4187 hid_info(hidpp->hid_dev, "Disconnected\n");
4188 if (hidpp->battery.ps) {
4189 hidpp->battery.online = false;
4190 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
4191 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
4192 power_supply_changed(hidpp->battery.ps);
4197 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4198 ret = wtp_connect(hdev);
4201 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
4202 ret = m560_send_config_command(hdev);
4205 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4206 ret = k400_connect(hdev);
4211 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
4212 ret = hidpp10_wheel_connect(hidpp);
4217 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
4218 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
4223 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
4224 ret = hidpp10_consumer_keys_connect(hidpp);
4229 if (hidpp->protocol_major >= 2) {
4232 if (!hidpp_get_wireless_feature_index(hidpp, &feature_index))
4233 hidpp->wireless_feature_index = feature_index;
4236 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
4237 name = hidpp_get_device_name(hidpp);
4239 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
4245 hidpp->name = devm_name;
4249 hidpp_initialize_battery(hidpp);
4250 if (!hid_is_usb(hidpp->hid_dev))
4251 hidpp_initialize_hires_scroll(hidpp);
4253 /* forward current battery state */
4254 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
4255 hidpp10_enable_battery_reporting(hidpp);
4256 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
4257 hidpp10_query_battery_mileage(hidpp);
4259 hidpp10_query_battery_status(hidpp);
4260 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
4261 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
4262 hidpp20_query_battery_voltage_info(hidpp);
4263 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
4264 hidpp20_query_battery_info_1004(hidpp);
4265 else if (hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4266 hidpp20_query_adc_measurement_info_1f20(hidpp);
4268 hidpp20_query_battery_info_1000(hidpp);
4270 if (hidpp->battery.ps)
4271 power_supply_changed(hidpp->battery.ps);
4273 if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4274 hi_res_scroll_enable(hidpp);
4276 if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
4277 /* if the input nodes are already created, we can stop now */
4280 input = hidpp_allocate_input(hdev);
4282 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
4286 hidpp_populate_input(hidpp, input);
4288 ret = input_register_device(input);
4290 input_free_device(input);
4294 hidpp->delayed_input = input;
4297 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
4299 static struct attribute *sysfs_attrs[] = {
4300 &dev_attr_builtin_power_supply.attr,
4304 static const struct attribute_group ps_attribute_group = {
4305 .attrs = sysfs_attrs
4308 static int hidpp_get_report_length(struct hid_device *hdev, int id)
4310 struct hid_report_enum *re;
4311 struct hid_report *report;
4313 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
4314 report = re->report_id_hash[id];
4318 return report->field[0]->report_count + 1;
4321 static u8 hidpp_validate_device(struct hid_device *hdev)
4323 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4324 int id, report_length;
4325 u8 supported_reports = 0;
4327 id = REPORT_ID_HIDPP_SHORT;
4328 report_length = hidpp_get_report_length(hdev, id);
4329 if (report_length) {
4330 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4333 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4336 id = REPORT_ID_HIDPP_LONG;
4337 report_length = hidpp_get_report_length(hdev, id);
4338 if (report_length) {
4339 if (report_length < HIDPP_REPORT_LONG_LENGTH)
4342 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4345 id = REPORT_ID_HIDPP_VERY_LONG;
4346 report_length = hidpp_get_report_length(hdev, id);
4347 if (report_length) {
4348 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4349 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4352 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4353 hidpp->very_long_report_length = report_length;
4356 return supported_reports;
4359 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4363 static bool hidpp_application_equals(struct hid_device *hdev,
4364 unsigned int application)
4366 struct list_head *report_list;
4367 struct hid_report *report;
4369 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4370 report = list_first_entry_or_null(report_list, struct hid_report, list);
4371 return report && report->application == application;
4374 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4376 struct hidpp_device *hidpp;
4378 unsigned int connect_mask = HID_CONNECT_DEFAULT;
4380 /* report_fixup needs drvdata to be set before we call hid_parse */
4381 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4385 hidpp->hid_dev = hdev;
4386 hidpp->name = hdev->name;
4387 hidpp->quirks = id->driver_data;
4388 hid_set_drvdata(hdev, hidpp);
4390 ret = hid_parse(hdev);
4392 hid_err(hdev, "%s:parse failed\n", __func__);
4397 * Make sure the device is HID++ capable, otherwise treat as generic HID
4399 hidpp->supported_reports = hidpp_validate_device(hdev);
4401 if (!hidpp->supported_reports) {
4402 hid_set_drvdata(hdev, NULL);
4403 devm_kfree(&hdev->dev, hidpp);
4404 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4407 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4408 hidpp_application_equals(hdev, HID_GD_MOUSE))
4409 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4410 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4412 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4413 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4414 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4416 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4417 ret = wtp_allocate(hdev, id);
4420 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4421 ret = k400_allocate(hdev);
4426 INIT_WORK(&hidpp->work, hidpp_connect_event);
4427 mutex_init(&hidpp->send_mutex);
4428 init_waitqueue_head(&hidpp->wait);
4430 /* indicates we are handling the battery properties in the kernel */
4431 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4433 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4437 * First call hid_hw_start(hdev, 0) to allow IO without connecting any
4438 * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's
4439 * name and serial number and store these in hdev->name and hdev->uniq,
4440 * before the hid-input and hidraw drivers expose these to userspace.
4442 ret = hid_hw_start(hdev, 0);
4444 hid_err(hdev, "hw start failed\n");
4445 goto hid_hw_start_fail;
4448 ret = hid_hw_open(hdev);
4450 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4452 goto hid_hw_open_fail;
4455 /* Allow incoming packets */
4456 hid_device_io_start(hdev);
4458 /* Get name + serial, store in hdev->name + hdev->uniq */
4459 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4460 hidpp_unifying_init(hidpp);
4462 hidpp_non_unifying_init(hidpp);
4464 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
4465 connect_mask &= ~HID_CONNECT_HIDINPUT;
4467 /* Now export the actual inputs and hidraw nodes to the world */
4468 hid_device_io_stop(hdev);
4469 ret = hid_connect(hdev, connect_mask);
4471 hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret);
4472 goto hid_hw_init_fail;
4475 /* Check for connected devices now that incoming packets will not be disabled again */
4476 hid_device_io_start(hdev);
4477 schedule_work(&hidpp->work);
4478 flush_work(&hidpp->work);
4480 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4481 struct hidpp_ff_private_data data;
4483 ret = g920_get_config(hidpp, &data);
4485 ret = hidpp_ff_init(hidpp, &data);
4488 hid_warn(hidpp->hid_dev,
4489 "Unable to initialize force feedback support, errno %d\n",
4494 * This relies on logi_dj_ll_close() being a no-op so that DJ connection
4495 * events will still be received.
4505 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4506 cancel_work_sync(&hidpp->work);
4507 mutex_destroy(&hidpp->send_mutex);
4511 static void hidpp_remove(struct hid_device *hdev)
4513 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4516 return hid_hw_stop(hdev);
4518 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4521 cancel_work_sync(&hidpp->work);
4522 mutex_destroy(&hidpp->send_mutex);
4525 #define LDJ_DEVICE(product) \
4526 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4527 USB_VENDOR_ID_LOGITECH, (product))
4529 #define L27MHZ_DEVICE(product) \
4530 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4531 USB_VENDOR_ID_LOGITECH, (product))
4533 static const struct hid_device_id hidpp_devices[] = {
4534 { /* wireless touchpad */
4536 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4537 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4538 { /* wireless touchpad T650 */
4540 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4541 { /* wireless touchpad T651 */
4542 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4543 USB_DEVICE_ID_LOGITECH_T651),
4544 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4545 { /* Mouse Logitech Anywhere MX */
4546 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4547 { /* Mouse logitech M560 */
4549 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
4550 { /* Mouse Logitech M705 (firmware RQM17) */
4551 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4552 { /* Mouse Logitech Performance MX */
4553 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4554 { /* Keyboard logitech K400 */
4556 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4557 { /* Solar Keyboard Logitech K750 */
4559 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4560 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4562 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4563 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4565 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4566 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4568 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4570 { LDJ_DEVICE(HID_ANY_ID) },
4572 { /* Keyboard LX501 (Y-RR53) */
4573 L27MHZ_DEVICE(0x0049),
4574 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4575 { /* Keyboard MX3000 (Y-RAM74) */
4576 L27MHZ_DEVICE(0x0057),
4577 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4578 { /* Keyboard MX3200 (Y-RAV80) */
4579 L27MHZ_DEVICE(0x005c),
4580 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4581 { /* S510 Media Remote */
4582 L27MHZ_DEVICE(0x00fe),
4583 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4585 { L27MHZ_DEVICE(HID_ANY_ID) },
4587 { /* Logitech G403 Wireless Gaming Mouse over USB */
4588 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4589 { /* Logitech G502 Lightspeed Wireless Gaming Mouse over USB */
4590 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC08D) },
4591 { /* Logitech G703 Gaming Mouse over USB */
4592 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4593 { /* Logitech G703 Hero Gaming Mouse over USB */
4594 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4595 { /* Logitech G900 Gaming Mouse over USB */
4596 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4597 { /* Logitech G903 Gaming Mouse over USB */
4598 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4599 { /* Logitech G903 Hero Gaming Mouse over USB */
4600 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4601 { /* Logitech G915 TKL Keyboard over USB */
4602 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC343) },
4603 { /* Logitech G920 Wheel over USB */
4604 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4605 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4606 { /* Logitech G923 Wheel (Xbox version) over USB */
4607 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G923_XBOX_WHEEL),
4608 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS },
4609 { /* Logitech G Pro Gaming Mouse over USB */
4610 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4611 { /* Logitech G Pro X Superlight Gaming Mouse over USB */
4612 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC094) },
4614 { /* G935 Gaming Headset */
4615 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0x0a87),
4616 .driver_data = HIDPP_QUIRK_WIRELESS_STATUS },
4618 { /* MX5000 keyboard over Bluetooth */
4619 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4620 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4621 { /* Dinovo Edge keyboard over Bluetooth */
4622 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4623 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4624 { /* MX5500 keyboard over Bluetooth */
4625 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4626 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4627 { /* Logitech G915 TKL keyboard over Bluetooth */
4628 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb35f) },
4629 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4630 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4631 { /* MX Master mouse over Bluetooth */
4632 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
4633 { /* M720 Triathlon mouse over Bluetooth */
4634 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb015) },
4635 { /* MX Ergo trackball over Bluetooth */
4636 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4637 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
4638 { /* Signature M650 over Bluetooth */
4639 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb02a) },
4640 { /* MX Master 3 mouse over Bluetooth */
4641 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
4642 { /* MX Anywhere 3 mouse over Bluetooth */
4643 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb025) },
4644 { /* MX Master 3S mouse over Bluetooth */
4645 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) },
4649 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4651 static const struct hid_usage_id hidpp_usages[] = {
4652 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4653 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4656 static struct hid_driver hidpp_driver = {
4657 .name = "logitech-hidpp-device",
4658 .id_table = hidpp_devices,
4659 .report_fixup = hidpp_report_fixup,
4660 .probe = hidpp_probe,
4661 .remove = hidpp_remove,
4662 .raw_event = hidpp_raw_event,
4663 .usage_table = hidpp_usages,
4664 .event = hidpp_event,
4665 .input_configured = hidpp_input_configured,
4666 .input_mapping = hidpp_input_mapping,
4667 .input_mapped = hidpp_input_mapped,
4670 module_hid_driver(hidpp_driver);