1 // SPDX-License-Identifier: GPL-2.0-only
3 * HIDPP protocol for Logitech receivers
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/device.h>
14 #include <linux/input.h>
15 #include <linux/usb.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/sched/clock.h>
21 #include <linux/kfifo.h>
22 #include <linux/input/mt.h>
23 #include <linux/workqueue.h>
24 #include <linux/atomic.h>
25 #include <linux/fixp-arith.h>
26 #include <asm/unaligned.h>
27 #include "usbhid/usbhid.h"
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
34 static bool disable_raw_mode;
35 module_param(disable_raw_mode, bool, 0644);
36 MODULE_PARM_DESC(disable_raw_mode,
37 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
39 static bool disable_tap_to_click;
40 module_param(disable_tap_to_click, bool, 0644);
41 MODULE_PARM_DESC(disable_tap_to_click,
42 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
44 #define REPORT_ID_HIDPP_SHORT 0x10
45 #define REPORT_ID_HIDPP_LONG 0x11
46 #define REPORT_ID_HIDPP_VERY_LONG 0x12
48 #define HIDPP_REPORT_SHORT_LENGTH 7
49 #define HIDPP_REPORT_LONG_LENGTH 20
50 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
52 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
53 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
54 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
56 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
57 #define HIDPP_SUB_ID_ROLLER 0x05
58 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
59 #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
60 #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
62 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
63 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
64 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
65 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
66 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
68 /* bits 2..20 are reserved for classes */
69 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
70 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
71 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
72 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
73 #define HIDPP_QUIRK_UNIFYING BIT(25)
74 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
75 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
76 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
77 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
78 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
79 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
81 /* These are just aliases for now */
82 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
83 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
85 /* Convenience constant to check for any high-res support. */
86 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
87 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
88 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
90 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
92 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
93 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
94 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
95 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
96 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
97 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
98 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
100 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
103 * There are two hidpp protocols in use, the first version hidpp10 is known
104 * as register access protocol or RAP, the second version hidpp20 is known as
105 * feature access protocol or FAP
107 * Most older devices (including the Unifying usb receiver) use the RAP protocol
108 * where as most newer devices use the FAP protocol. Both protocols are
109 * compatible with the underlying transport, which could be usb, Unifiying, or
110 * bluetooth. The message lengths are defined by the hid vendor specific report
111 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
112 * the HIDPP_LONG report type (total message length 20 bytes)
114 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
115 * messages. The Unifying receiver itself responds to RAP messages (device index
116 * is 0xFF for the receiver), and all messages (short or long) with a device
117 * index between 1 and 6 are passed untouched to the corresponding paired
120 * The paired device can be RAP or FAP, it will receive the message untouched
121 * from the Unifiying receiver.
126 u8 funcindex_clientid;
127 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
133 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
136 struct hidpp_report {
142 u8 rawbytes[sizeof(struct fap)];
146 struct hidpp_battery {
148 u8 solar_feature_index;
149 u8 voltage_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;
197 struct input_dev *delayed_input;
199 unsigned long quirks;
200 unsigned long capabilities;
201 u8 supported_reports;
203 struct hidpp_battery battery;
204 struct hidpp_scroll_counter vertical_wheel_counter;
206 u8 wireless_feature_index;
209 /* HID++ 1.0 error codes */
210 #define HIDPP_ERROR 0x8f
211 #define HIDPP_ERROR_SUCCESS 0x00
212 #define HIDPP_ERROR_INVALID_SUBID 0x01
213 #define HIDPP_ERROR_INVALID_ADRESS 0x02
214 #define HIDPP_ERROR_INVALID_VALUE 0x03
215 #define HIDPP_ERROR_CONNECT_FAIL 0x04
216 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
217 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
218 #define HIDPP_ERROR_BUSY 0x07
219 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
220 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
221 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
222 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
223 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
224 /* HID++ 2.0 error codes */
225 #define HIDPP20_ERROR 0xff
227 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
229 static int __hidpp_send_report(struct hid_device *hdev,
230 struct hidpp_report *hidpp_report)
232 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
233 int fields_count, ret;
235 switch (hidpp_report->report_id) {
236 case REPORT_ID_HIDPP_SHORT:
237 fields_count = HIDPP_REPORT_SHORT_LENGTH;
239 case REPORT_ID_HIDPP_LONG:
240 fields_count = HIDPP_REPORT_LONG_LENGTH;
242 case REPORT_ID_HIDPP_VERY_LONG:
243 fields_count = hidpp->very_long_report_length;
250 * set the device_index as the receiver, it will be overwritten by
251 * hid_hw_request if needed
253 hidpp_report->device_index = 0xff;
255 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
256 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
258 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
259 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
263 return ret == fields_count ? 0 : -1;
267 * hidpp_send_message_sync() returns 0 in case of success, and something else
268 * in case of a failure.
269 * - If ' something else' is positive, that means that an error has been raised
270 * by the protocol itself.
271 * - If ' something else' is negative, that means that we had a classic error
272 * (-ENOMEM, -EPIPE, etc...)
274 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
275 struct hidpp_report *message,
276 struct hidpp_report *response)
280 mutex_lock(&hidpp->send_mutex);
282 hidpp->send_receive_buf = response;
283 hidpp->answer_available = false;
286 * So that we can later validate the answer when it arrives
289 *response = *message;
291 ret = __hidpp_send_report(hidpp->hid_dev, message);
294 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
295 memset(response, 0, sizeof(struct hidpp_report));
299 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
301 dbg_hid("%s:timeout waiting for response\n", __func__);
302 memset(response, 0, sizeof(struct hidpp_report));
306 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
307 response->rap.sub_id == HIDPP_ERROR) {
308 ret = response->rap.params[1];
309 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
313 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
314 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
315 response->fap.feature_index == HIDPP20_ERROR) {
316 ret = response->fap.params[1];
317 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
322 mutex_unlock(&hidpp->send_mutex);
327 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
328 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
329 struct hidpp_report *response)
331 struct hidpp_report *message;
334 if (param_count > sizeof(message->fap.params))
337 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
341 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
342 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
344 message->report_id = REPORT_ID_HIDPP_LONG;
345 message->fap.feature_index = feat_index;
346 message->fap.funcindex_clientid = funcindex_clientid;
347 memcpy(&message->fap.params, params, param_count);
349 ret = hidpp_send_message_sync(hidpp, message, response);
354 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
355 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
356 struct hidpp_report *response)
358 struct hidpp_report *message;
361 /* Send as long report if short reports are not supported. */
362 if (report_id == REPORT_ID_HIDPP_SHORT &&
363 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
364 report_id = REPORT_ID_HIDPP_LONG;
367 case REPORT_ID_HIDPP_SHORT:
368 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
370 case REPORT_ID_HIDPP_LONG:
371 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
373 case REPORT_ID_HIDPP_VERY_LONG:
374 max_count = hidpp_dev->very_long_report_length - 4;
380 if (param_count > max_count)
383 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
386 message->report_id = report_id;
387 message->rap.sub_id = sub_id;
388 message->rap.reg_address = reg_address;
389 memcpy(&message->rap.params, params, param_count);
391 ret = hidpp_send_message_sync(hidpp_dev, message, response);
396 static void delayed_work_cb(struct work_struct *work)
398 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
400 hidpp_connect_event(hidpp);
403 static inline bool hidpp_match_answer(struct hidpp_report *question,
404 struct hidpp_report *answer)
406 return (answer->fap.feature_index == question->fap.feature_index) &&
407 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
410 static inline bool hidpp_match_error(struct hidpp_report *question,
411 struct hidpp_report *answer)
413 return ((answer->rap.sub_id == HIDPP_ERROR) ||
414 (answer->fap.feature_index == HIDPP20_ERROR)) &&
415 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
416 (answer->fap.params[0] == question->fap.funcindex_clientid);
419 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
420 struct hidpp_report *report)
422 return (hidpp->wireless_feature_index &&
423 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
424 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
425 (report->rap.sub_id == 0x41));
429 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
431 static void hidpp_prefix_name(char **name, int name_length)
433 #define PREFIX_LENGTH 9 /* "Logitech " */
438 if (name_length > PREFIX_LENGTH &&
439 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
440 /* The prefix has is already in the name */
443 new_length = PREFIX_LENGTH + name_length;
444 new_name = kzalloc(new_length, GFP_KERNEL);
448 snprintf(new_name, new_length, "Logitech %s", *name);
456 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
457 * events given a high-resolution wheel
459 * @input_dev: Pointer to the input device
460 * @counter: a hid_scroll_counter struct describing the wheel.
461 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
464 * Given a high-resolution movement, this function converts the movement into
465 * fractions of 120 and emits high-resolution scroll events for the input
466 * device. It also uses the multiplier from &struct hid_scroll_counter to
467 * emit low-resolution scroll events when appropriate for
468 * backwards-compatibility with userspace input libraries.
470 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
471 struct hidpp_scroll_counter *counter,
474 int low_res_value, remainder, direction;
475 unsigned long long now, previous;
477 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
478 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
480 remainder = counter->remainder;
481 direction = hi_res_value > 0 ? 1 : -1;
484 previous = counter->last_time;
485 counter->last_time = now;
487 * Reset the remainder after a period of inactivity or when the
488 * direction changes. This prevents the REL_WHEEL emulation point
489 * from sliding for devices that don't always provide the same
490 * number of movements per detent.
492 if (now - previous > 1000000000 || direction != counter->direction)
495 counter->direction = direction;
496 remainder += hi_res_value;
498 /* Some wheels will rest 7/8ths of a detent from the previous detent
499 * after slow movement, so we want the threshold for low-res events to
500 * be in the middle between two detents (e.g. after 4/8ths) as
501 * opposed to on the detents themselves (8/8ths).
503 if (abs(remainder) >= 60) {
504 /* Add (or subtract) 1 because we want to trigger when the wheel
505 * is half-way to the next detent (i.e. scroll 1 detent after a
506 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
509 low_res_value = remainder / 120;
510 if (low_res_value == 0)
511 low_res_value = (hi_res_value > 0 ? 1 : -1);
512 input_report_rel(input_dev, REL_WHEEL, low_res_value);
513 remainder -= low_res_value * 120;
515 counter->remainder = remainder;
518 /* -------------------------------------------------------------------------- */
519 /* HIDP++ 1.0 commands */
520 /* -------------------------------------------------------------------------- */
522 #define HIDPP_SET_REGISTER 0x80
523 #define HIDPP_GET_REGISTER 0x81
524 #define HIDPP_SET_LONG_REGISTER 0x82
525 #define HIDPP_GET_LONG_REGISTER 0x83
528 * hidpp10_set_register - Modify a HID++ 1.0 register.
529 * @hidpp_dev: the device to set the register on.
530 * @register_address: the address of the register to modify.
531 * @byte: the byte of the register to modify. Should be less than 3.
532 * @mask: mask of the bits to modify
533 * @value: new values for the bits in mask
534 * Return: 0 if successful, otherwise a negative error code.
536 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
537 u8 register_address, u8 byte, u8 mask, u8 value)
539 struct hidpp_report response;
541 u8 params[3] = { 0 };
543 ret = hidpp_send_rap_command_sync(hidpp_dev,
544 REPORT_ID_HIDPP_SHORT,
551 memcpy(params, response.rap.params, 3);
553 params[byte] &= ~mask;
554 params[byte] |= value & mask;
556 return hidpp_send_rap_command_sync(hidpp_dev,
557 REPORT_ID_HIDPP_SHORT,
560 params, 3, &response);
563 #define HIDPP_REG_ENABLE_REPORTS 0x00
564 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
565 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
566 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
567 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
568 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
570 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
572 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
573 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
576 #define HIDPP_REG_FEATURES 0x01
577 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
578 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
580 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
581 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
583 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
584 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
587 #define HIDPP_REG_BATTERY_STATUS 0x07
589 static int hidpp10_battery_status_map_level(u8 param)
595 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
598 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
601 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
604 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
607 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
613 static int hidpp10_battery_status_map_status(u8 param)
619 /* discharging (in use) */
620 status = POWER_SUPPLY_STATUS_DISCHARGING;
622 case 0x21: /* (standard) charging */
623 case 0x24: /* fast charging */
624 case 0x25: /* slow charging */
625 status = POWER_SUPPLY_STATUS_CHARGING;
627 case 0x26: /* topping charge */
628 case 0x22: /* charge complete */
629 status = POWER_SUPPLY_STATUS_FULL;
631 case 0x20: /* unknown */
632 status = POWER_SUPPLY_STATUS_UNKNOWN;
635 * 0x01...0x1F = reserved (not charging)
636 * 0x23 = charging error
637 * 0x27..0xff = reserved
640 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
647 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
649 struct hidpp_report response;
652 ret = hidpp_send_rap_command_sync(hidpp,
653 REPORT_ID_HIDPP_SHORT,
655 HIDPP_REG_BATTERY_STATUS,
660 hidpp->battery.level =
661 hidpp10_battery_status_map_level(response.rap.params[0]);
662 status = hidpp10_battery_status_map_status(response.rap.params[1]);
663 hidpp->battery.status = status;
664 /* the capacity is only available when discharging or full */
665 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
666 status == POWER_SUPPLY_STATUS_FULL;
671 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
673 static int hidpp10_battery_mileage_map_status(u8 param)
677 switch (param >> 6) {
679 /* discharging (in use) */
680 status = POWER_SUPPLY_STATUS_DISCHARGING;
682 case 0x01: /* charging */
683 status = POWER_SUPPLY_STATUS_CHARGING;
685 case 0x02: /* charge complete */
686 status = POWER_SUPPLY_STATUS_FULL;
689 * 0x03 = charging error
692 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
699 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
701 struct hidpp_report response;
704 ret = hidpp_send_rap_command_sync(hidpp,
705 REPORT_ID_HIDPP_SHORT,
707 HIDPP_REG_BATTERY_MILEAGE,
712 hidpp->battery.capacity = response.rap.params[0];
713 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
714 hidpp->battery.status = status;
715 /* the capacity is only available when discharging or full */
716 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
717 status == POWER_SUPPLY_STATUS_FULL;
722 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
724 struct hidpp_report *report = (struct hidpp_report *)data;
725 int status, capacity, level;
728 if (report->report_id != REPORT_ID_HIDPP_SHORT)
731 switch (report->rap.sub_id) {
732 case HIDPP_REG_BATTERY_STATUS:
733 capacity = hidpp->battery.capacity;
734 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
735 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
737 case HIDPP_REG_BATTERY_MILEAGE:
738 capacity = report->rap.params[0];
739 level = hidpp->battery.level;
740 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
746 changed = capacity != hidpp->battery.capacity ||
747 level != hidpp->battery.level ||
748 status != hidpp->battery.status;
750 /* the capacity is only available when discharging or full */
751 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
752 status == POWER_SUPPLY_STATUS_FULL;
755 hidpp->battery.level = level;
756 hidpp->battery.status = status;
757 if (hidpp->battery.ps)
758 power_supply_changed(hidpp->battery.ps);
764 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
765 #define HIDPP_EXTENDED_PAIRING 0x30
766 #define HIDPP_DEVICE_NAME 0x40
768 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
770 struct hidpp_report response;
772 u8 params[1] = { HIDPP_DEVICE_NAME };
776 ret = hidpp_send_rap_command_sync(hidpp_dev,
777 REPORT_ID_HIDPP_SHORT,
778 HIDPP_GET_LONG_REGISTER,
779 HIDPP_REG_PAIRING_INFORMATION,
780 params, 1, &response);
784 len = response.rap.params[1];
786 if (2 + len > sizeof(response.rap.params))
789 if (len < 4) /* logitech devices are usually at least Xddd */
792 name = kzalloc(len + 1, GFP_KERNEL);
796 memcpy(name, &response.rap.params[2], len);
798 /* include the terminating '\0' */
799 hidpp_prefix_name(&name, len + 1);
804 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
806 struct hidpp_report response;
808 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
810 ret = hidpp_send_rap_command_sync(hidpp,
811 REPORT_ID_HIDPP_SHORT,
812 HIDPP_GET_LONG_REGISTER,
813 HIDPP_REG_PAIRING_INFORMATION,
814 params, 1, &response);
819 * We don't care about LE or BE, we will output it as a string
820 * with %4phD, so we need to keep the order.
822 *serial = *((u32 *)&response.rap.params[1]);
826 static int hidpp_unifying_init(struct hidpp_device *hidpp)
828 struct hid_device *hdev = hidpp->hid_dev;
833 ret = hidpp_unifying_get_serial(hidpp, &serial);
837 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
838 hdev->product, &serial);
839 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
841 name = hidpp_unifying_get_name(hidpp);
845 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
846 dbg_hid("HID++ Unifying: Got name: %s\n", name);
852 /* -------------------------------------------------------------------------- */
854 /* -------------------------------------------------------------------------- */
856 #define HIDPP_PAGE_ROOT 0x0000
857 #define HIDPP_PAGE_ROOT_IDX 0x00
859 #define CMD_ROOT_GET_FEATURE 0x01
860 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
862 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
863 u8 *feature_index, u8 *feature_type)
865 struct hidpp_report response;
867 u8 params[2] = { feature >> 8, feature & 0x00FF };
869 ret = hidpp_send_fap_command_sync(hidpp,
871 CMD_ROOT_GET_FEATURE,
872 params, 2, &response);
876 if (response.fap.params[0] == 0)
879 *feature_index = response.fap.params[0];
880 *feature_type = response.fap.params[1];
885 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
887 const u8 ping_byte = 0x5a;
888 u8 ping_data[3] = { 0, 0, ping_byte };
889 struct hidpp_report response;
892 ret = hidpp_send_rap_command_sync(hidpp,
893 REPORT_ID_HIDPP_SHORT,
895 CMD_ROOT_GET_PROTOCOL_VERSION,
896 ping_data, sizeof(ping_data), &response);
898 if (ret == HIDPP_ERROR_INVALID_SUBID) {
899 hidpp->protocol_major = 1;
900 hidpp->protocol_minor = 0;
904 /* the device might not be connected */
905 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
909 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
916 if (response.rap.params[2] != ping_byte) {
917 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
918 __func__, response.rap.params[2], ping_byte);
922 hidpp->protocol_major = response.rap.params[0];
923 hidpp->protocol_minor = response.rap.params[1];
926 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
927 hidpp->protocol_major, hidpp->protocol_minor);
931 /* -------------------------------------------------------------------------- */
932 /* 0x0005: GetDeviceNameType */
933 /* -------------------------------------------------------------------------- */
935 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
937 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
938 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
939 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
941 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
942 u8 feature_index, u8 *nameLength)
944 struct hidpp_report response;
947 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
948 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
951 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
958 *nameLength = response.fap.params[0];
963 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
964 u8 feature_index, u8 char_index, char *device_name, int len_buf)
966 struct hidpp_report response;
970 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
971 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
975 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
982 switch (response.report_id) {
983 case REPORT_ID_HIDPP_VERY_LONG:
984 count = hidpp->very_long_report_length - 4;
986 case REPORT_ID_HIDPP_LONG:
987 count = HIDPP_REPORT_LONG_LENGTH - 4;
989 case REPORT_ID_HIDPP_SHORT:
990 count = HIDPP_REPORT_SHORT_LENGTH - 4;
999 for (i = 0; i < count; i++)
1000 device_name[i] = response.fap.params[i];
1005 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1014 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1015 &feature_index, &feature_type);
1019 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1024 name = kzalloc(__name_length + 1, GFP_KERNEL);
1028 while (index < __name_length) {
1029 ret = hidpp_devicenametype_get_device_name(hidpp,
1030 feature_index, index, name + index,
1031 __name_length - index);
1039 /* include the terminating '\0' */
1040 hidpp_prefix_name(&name, __name_length + 1);
1045 /* -------------------------------------------------------------------------- */
1046 /* 0x1000: Battery level status */
1047 /* -------------------------------------------------------------------------- */
1049 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1051 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1052 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1054 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1056 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1057 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1058 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1060 static int hidpp_map_battery_level(int capacity)
1063 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1065 * The spec says this should be < 31 but some devices report 30
1066 * with brand new batteries and Windows reports 30 as "Good".
1068 else if (capacity < 30)
1069 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1070 else if (capacity < 81)
1071 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1072 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1075 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1081 *capacity = data[0];
1082 *next_capacity = data[1];
1083 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1085 /* When discharging, we can rely on the device reported capacity.
1086 * For all other states the device reports 0 (unknown).
1089 case 0: /* discharging (in use) */
1090 status = POWER_SUPPLY_STATUS_DISCHARGING;
1091 *level = hidpp_map_battery_level(*capacity);
1093 case 1: /* recharging */
1094 status = POWER_SUPPLY_STATUS_CHARGING;
1096 case 2: /* charge in final stage */
1097 status = POWER_SUPPLY_STATUS_CHARGING;
1099 case 3: /* charge complete */
1100 status = POWER_SUPPLY_STATUS_FULL;
1101 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1104 case 4: /* recharging below optimal speed */
1105 status = POWER_SUPPLY_STATUS_CHARGING;
1107 /* 5 = invalid battery type
1109 7 = other charging error */
1111 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1118 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1125 struct hidpp_report response;
1127 u8 *params = (u8 *)response.fap.params;
1129 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1130 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1131 NULL, 0, &response);
1132 /* Ignore these intermittent errors */
1133 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1136 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1143 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1150 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1153 struct hidpp_report response;
1155 u8 *params = (u8 *)response.fap.params;
1156 unsigned int level_count, flags;
1158 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1159 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1160 NULL, 0, &response);
1162 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1169 level_count = params[0];
1172 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1173 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1175 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1180 static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1184 int status, capacity, next_capacity, level;
1186 if (hidpp->battery.feature_index == 0xff) {
1187 ret = hidpp_root_get_feature(hidpp,
1188 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1189 &hidpp->battery.feature_index,
1195 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1196 hidpp->battery.feature_index,
1198 &next_capacity, &level);
1202 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1203 hidpp->battery.feature_index);
1207 hidpp->battery.status = status;
1208 hidpp->battery.capacity = capacity;
1209 hidpp->battery.level = level;
1210 /* the capacity is only available when discharging or full */
1211 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1212 status == POWER_SUPPLY_STATUS_FULL;
1217 static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1220 struct hidpp_report *report = (struct hidpp_report *)data;
1221 int status, capacity, next_capacity, level;
1224 if (report->fap.feature_index != hidpp->battery.feature_index ||
1225 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1228 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1233 /* the capacity is only available when discharging or full */
1234 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1235 status == POWER_SUPPLY_STATUS_FULL;
1237 changed = capacity != hidpp->battery.capacity ||
1238 level != hidpp->battery.level ||
1239 status != hidpp->battery.status;
1242 hidpp->battery.level = level;
1243 hidpp->battery.capacity = capacity;
1244 hidpp->battery.status = status;
1245 if (hidpp->battery.ps)
1246 power_supply_changed(hidpp->battery.ps);
1252 /* -------------------------------------------------------------------------- */
1253 /* 0x1001: Battery voltage */
1254 /* -------------------------------------------------------------------------- */
1256 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1258 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1260 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1262 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1263 int *level, int *charge_type)
1267 long flags = (long) data[2];
1268 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1271 switch (flags & 0x07) {
1273 status = POWER_SUPPLY_STATUS_CHARGING;
1276 status = POWER_SUPPLY_STATUS_FULL;
1277 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1280 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1283 status = POWER_SUPPLY_STATUS_UNKNOWN;
1287 status = POWER_SUPPLY_STATUS_DISCHARGING;
1289 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1290 if (test_bit(3, &flags)) {
1291 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1293 if (test_bit(4, &flags)) {
1294 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1296 if (test_bit(5, &flags)) {
1297 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1300 *voltage = get_unaligned_be16(data);
1305 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1307 int *status, int *voltage,
1308 int *level, int *charge_type)
1310 struct hidpp_report response;
1312 u8 *params = (u8 *)response.fap.params;
1314 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1315 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1316 NULL, 0, &response);
1319 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1326 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1328 *status = hidpp20_battery_map_status_voltage(params, voltage,
1329 level, charge_type);
1334 static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1336 /* NB: This voltage curve doesn't necessarily map perfectly to all
1337 * devices that implement the BATTERY_VOLTAGE feature. This is because
1338 * there are a few devices that use different battery technology.
1341 static const int voltages[] = {
1342 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1343 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1344 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1345 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1346 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1347 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1348 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1349 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1350 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1351 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1356 BUILD_BUG_ON(ARRAY_SIZE(voltages) != 100);
1358 if (unlikely(voltage < 3500 || voltage >= 5000))
1359 hid_warn_once(hid_dev,
1360 "%s: possibly using the wrong voltage curve\n",
1363 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1364 if (voltage >= voltages[i])
1365 return ARRAY_SIZE(voltages) - i;
1371 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1375 int status, voltage, level, charge_type;
1377 if (hidpp->battery.voltage_feature_index == 0xff) {
1378 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1379 &hidpp->battery.voltage_feature_index,
1385 ret = hidpp20_battery_get_battery_voltage(hidpp,
1386 hidpp->battery.voltage_feature_index,
1387 &status, &voltage, &level, &charge_type);
1392 hidpp->battery.status = status;
1393 hidpp->battery.voltage = voltage;
1394 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1396 hidpp->battery.level = level;
1397 hidpp->battery.charge_type = charge_type;
1398 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1403 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1406 struct hidpp_report *report = (struct hidpp_report *)data;
1407 int status, voltage, level, charge_type;
1409 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1410 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1413 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1414 &level, &charge_type);
1416 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1418 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1419 hidpp->battery.voltage = voltage;
1420 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1422 hidpp->battery.status = status;
1423 hidpp->battery.level = level;
1424 hidpp->battery.charge_type = charge_type;
1425 if (hidpp->battery.ps)
1426 power_supply_changed(hidpp->battery.ps);
1431 /* -------------------------------------------------------------------------- */
1432 /* 0x1004: Unified battery */
1433 /* -------------------------------------------------------------------------- */
1435 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1437 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1438 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1440 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1442 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1443 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1444 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1445 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1447 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1448 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1450 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1453 struct hidpp_report response;
1455 u8 *params = (u8 *)response.fap.params;
1457 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1458 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1459 /* we have already set the device capabilities, so let's skip */
1463 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1464 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1465 NULL, 0, &response);
1466 /* Ignore these intermittent errors */
1467 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1470 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1478 * If the device supports state of charge (battery percentage) we won't
1479 * export the battery level information. there are 4 possible battery
1480 * levels and they all are optional, this means that the device might
1481 * not support any of them, we are just better off with the battery
1484 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1485 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1486 hidpp->battery.supported_levels_1004 = 0;
1488 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1489 hidpp->battery.supported_levels_1004 = params[0];
1495 static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1497 u8 external_power_status)
1501 switch (charging_status) {
1502 case 0: /* discharging */
1503 status = POWER_SUPPLY_STATUS_DISCHARGING;
1505 case 1: /* charging */
1506 case 2: /* charging slow */
1507 status = POWER_SUPPLY_STATUS_CHARGING;
1509 case 3: /* complete */
1510 status = POWER_SUPPLY_STATUS_FULL;
1513 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1514 hid_info(hidpp->hid_dev, "%s: charging error",
1518 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1525 static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1528 /* cler unsupported level bits */
1529 battery_level &= hidpp->battery.supported_levels_1004;
1531 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1532 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1533 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1534 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1535 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1536 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1537 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1538 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1540 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1543 static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1545 u8 *state_of_charge,
1549 struct hidpp_report response;
1551 u8 *params = (u8 *)response.fap.params;
1553 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1554 CMD_UNIFIED_BATTERY_GET_STATUS,
1555 NULL, 0, &response);
1556 /* Ignore these intermittent errors */
1557 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1560 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1567 *state_of_charge = params[0];
1568 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1569 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1574 static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1581 if (hidpp->battery.feature_index == 0xff) {
1582 ret = hidpp_root_get_feature(hidpp,
1583 HIDPP_PAGE_UNIFIED_BATTERY,
1584 &hidpp->battery.feature_index,
1590 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1591 hidpp->battery.feature_index);
1595 ret = hidpp20_unifiedbattery_get_status(hidpp,
1596 hidpp->battery.feature_index,
1603 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1604 hidpp->battery.capacity = state_of_charge;
1605 hidpp->battery.status = status;
1606 hidpp->battery.level = level;
1607 hidpp->battery.online = true;
1612 static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1615 struct hidpp_report *report = (struct hidpp_report *)data;
1616 u8 *params = (u8 *)report->fap.params;
1617 int state_of_charge, status, level;
1620 if (report->fap.feature_index != hidpp->battery.feature_index ||
1621 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1624 state_of_charge = params[0];
1625 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1626 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1628 changed = status != hidpp->battery.status ||
1629 (state_of_charge != hidpp->battery.capacity &&
1630 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1631 (level != hidpp->battery.level &&
1632 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1635 hidpp->battery.capacity = state_of_charge;
1636 hidpp->battery.status = status;
1637 hidpp->battery.level = level;
1638 if (hidpp->battery.ps)
1639 power_supply_changed(hidpp->battery.ps);
1645 /* -------------------------------------------------------------------------- */
1646 /* Battery feature helpers */
1647 /* -------------------------------------------------------------------------- */
1649 static enum power_supply_property hidpp_battery_props[] = {
1650 POWER_SUPPLY_PROP_ONLINE,
1651 POWER_SUPPLY_PROP_STATUS,
1652 POWER_SUPPLY_PROP_SCOPE,
1653 POWER_SUPPLY_PROP_MODEL_NAME,
1654 POWER_SUPPLY_PROP_MANUFACTURER,
1655 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1656 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1657 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1658 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1661 static int hidpp_battery_get_property(struct power_supply *psy,
1662 enum power_supply_property psp,
1663 union power_supply_propval *val)
1665 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1669 case POWER_SUPPLY_PROP_STATUS:
1670 val->intval = hidpp->battery.status;
1672 case POWER_SUPPLY_PROP_CAPACITY:
1673 val->intval = hidpp->battery.capacity;
1675 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1676 val->intval = hidpp->battery.level;
1678 case POWER_SUPPLY_PROP_SCOPE:
1679 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1681 case POWER_SUPPLY_PROP_ONLINE:
1682 val->intval = hidpp->battery.online;
1684 case POWER_SUPPLY_PROP_MODEL_NAME:
1685 if (!strncmp(hidpp->name, "Logitech ", 9))
1686 val->strval = hidpp->name + 9;
1688 val->strval = hidpp->name;
1690 case POWER_SUPPLY_PROP_MANUFACTURER:
1691 val->strval = "Logitech";
1693 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1694 val->strval = hidpp->hid_dev->uniq;
1696 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1697 /* hardware reports voltage in mV. sysfs expects uV */
1698 val->intval = hidpp->battery.voltage * 1000;
1700 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1701 val->intval = hidpp->battery.charge_type;
1711 /* -------------------------------------------------------------------------- */
1712 /* 0x1d4b: Wireless device status */
1713 /* -------------------------------------------------------------------------- */
1714 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1716 static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1721 ret = hidpp_root_get_feature(hidpp,
1722 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1723 &hidpp->wireless_feature_index,
1729 /* -------------------------------------------------------------------------- */
1730 /* 0x2120: Hi-resolution scrolling */
1731 /* -------------------------------------------------------------------------- */
1733 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1735 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1737 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1738 bool enabled, u8 *multiplier)
1744 struct hidpp_report response;
1746 ret = hidpp_root_get_feature(hidpp,
1747 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1753 params[0] = enabled ? BIT(0) : 0;
1754 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1755 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1756 params, sizeof(params), &response);
1759 *multiplier = response.fap.params[1];
1763 /* -------------------------------------------------------------------------- */
1764 /* 0x2121: HiRes Wheel */
1765 /* -------------------------------------------------------------------------- */
1767 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1769 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1770 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1772 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1778 struct hidpp_report response;
1780 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1781 &feature_index, &feature_type);
1783 goto return_default;
1785 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1786 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1787 NULL, 0, &response);
1789 goto return_default;
1791 *multiplier = response.fap.params[0];
1794 hid_warn(hidpp->hid_dev,
1795 "Couldn't get wheel multiplier (error %d)\n", ret);
1799 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1800 bool high_resolution, bool use_hidpp)
1806 struct hidpp_report response;
1808 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1809 &feature_index, &feature_type);
1813 params[0] = (invert ? BIT(2) : 0) |
1814 (high_resolution ? BIT(1) : 0) |
1815 (use_hidpp ? BIT(0) : 0);
1817 return hidpp_send_fap_command_sync(hidpp, feature_index,
1818 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1819 params, sizeof(params), &response);
1822 /* -------------------------------------------------------------------------- */
1823 /* 0x4301: Solar Keyboard */
1824 /* -------------------------------------------------------------------------- */
1826 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1828 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1830 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1831 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1832 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1834 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1836 struct hidpp_report response;
1837 u8 params[2] = { 1, 1 };
1841 if (hidpp->battery.feature_index == 0xff) {
1842 ret = hidpp_root_get_feature(hidpp,
1843 HIDPP_PAGE_SOLAR_KEYBOARD,
1844 &hidpp->battery.solar_feature_index,
1850 ret = hidpp_send_fap_command_sync(hidpp,
1851 hidpp->battery.solar_feature_index,
1852 CMD_SOLAR_SET_LIGHT_MEASURE,
1853 params, 2, &response);
1855 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1862 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1867 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1870 struct hidpp_report *report = (struct hidpp_report *)data;
1871 int capacity, lux, status;
1874 function = report->fap.funcindex_clientid;
1877 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1878 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1879 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1880 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1883 capacity = report->fap.params[0];
1886 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1887 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1889 status = POWER_SUPPLY_STATUS_CHARGING;
1891 status = POWER_SUPPLY_STATUS_DISCHARGING;
1893 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1895 if (capacity < hidpp->battery.capacity)
1896 status = POWER_SUPPLY_STATUS_DISCHARGING;
1898 status = POWER_SUPPLY_STATUS_CHARGING;
1902 if (capacity == 100)
1903 status = POWER_SUPPLY_STATUS_FULL;
1905 hidpp->battery.online = true;
1906 if (capacity != hidpp->battery.capacity ||
1907 status != hidpp->battery.status) {
1908 hidpp->battery.capacity = capacity;
1909 hidpp->battery.status = status;
1910 if (hidpp->battery.ps)
1911 power_supply_changed(hidpp->battery.ps);
1917 /* -------------------------------------------------------------------------- */
1918 /* 0x6010: Touchpad FW items */
1919 /* -------------------------------------------------------------------------- */
1921 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1923 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1925 struct hidpp_touchpad_fw_items {
1927 uint8_t desired_state;
1933 * send a set state command to the device by reading the current items->state
1934 * field. items is then filled with the current state.
1936 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1938 struct hidpp_touchpad_fw_items *items)
1940 struct hidpp_report response;
1942 u8 *params = (u8 *)response.fap.params;
1944 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1945 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1948 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1955 items->presence = params[0];
1956 items->desired_state = params[1];
1957 items->state = params[2];
1958 items->persistent = params[3];
1963 /* -------------------------------------------------------------------------- */
1964 /* 0x6100: TouchPadRawXY */
1965 /* -------------------------------------------------------------------------- */
1967 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1969 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1970 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1972 #define EVENT_TOUCHPAD_RAW_XY 0x00
1974 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1975 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1977 struct hidpp_touchpad_raw_info {
1988 struct hidpp_touchpad_raw_xy_finger {
1998 struct hidpp_touchpad_raw_xy {
2000 struct hidpp_touchpad_raw_xy_finger fingers[2];
2007 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2008 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2010 struct hidpp_report response;
2012 u8 *params = (u8 *)response.fap.params;
2014 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2015 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
2018 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2025 raw_info->x_size = get_unaligned_be16(¶ms[0]);
2026 raw_info->y_size = get_unaligned_be16(¶ms[2]);
2027 raw_info->z_range = params[4];
2028 raw_info->area_range = params[5];
2029 raw_info->maxcontacts = params[7];
2030 raw_info->origin = params[8];
2031 /* res is given in unit per inch */
2032 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
2037 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2038 u8 feature_index, bool send_raw_reports,
2039 bool sensor_enhanced_settings)
2041 struct hidpp_report response;
2045 * bit 0 - enable raw
2046 * bit 1 - 16bit Z, no area
2047 * bit 2 - enhanced sensitivity
2048 * bit 3 - width, height (4 bits each) instead of area
2049 * bit 4 - send raw + gestures (degrades smoothness)
2050 * remaining bits - reserved
2052 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2054 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2055 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
2058 static void hidpp_touchpad_touch_event(u8 *data,
2059 struct hidpp_touchpad_raw_xy_finger *finger)
2061 u8 x_m = data[0] << 2;
2062 u8 y_m = data[2] << 2;
2064 finger->x = x_m << 6 | data[1];
2065 finger->y = y_m << 6 | data[3];
2067 finger->contact_type = data[0] >> 6;
2068 finger->contact_status = data[2] >> 6;
2070 finger->z = data[4];
2071 finger->area = data[5];
2072 finger->finger_id = data[6] >> 4;
2075 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2076 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2078 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2079 raw_xy->end_of_frame = data[8] & 0x01;
2080 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2081 raw_xy->finger_count = data[15] & 0x0f;
2082 raw_xy->button = (data[8] >> 2) & 0x01;
2084 if (raw_xy->finger_count) {
2085 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2086 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2090 /* -------------------------------------------------------------------------- */
2091 /* 0x8123: Force feedback support */
2092 /* -------------------------------------------------------------------------- */
2094 #define HIDPP_FF_GET_INFO 0x01
2095 #define HIDPP_FF_RESET_ALL 0x11
2096 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2097 #define HIDPP_FF_SET_EFFECT_STATE 0x31
2098 #define HIDPP_FF_DESTROY_EFFECT 0x41
2099 #define HIDPP_FF_GET_APERTURE 0x51
2100 #define HIDPP_FF_SET_APERTURE 0x61
2101 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2102 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2104 #define HIDPP_FF_EFFECT_STATE_GET 0x00
2105 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
2106 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2107 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2109 #define HIDPP_FF_EFFECT_CONSTANT 0x00
2110 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2111 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2112 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2113 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2114 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2115 #define HIDPP_FF_EFFECT_SPRING 0x06
2116 #define HIDPP_FF_EFFECT_DAMPER 0x07
2117 #define HIDPP_FF_EFFECT_FRICTION 0x08
2118 #define HIDPP_FF_EFFECT_INERTIA 0x09
2119 #define HIDPP_FF_EFFECT_RAMP 0x0A
2121 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
2123 #define HIDPP_FF_EFFECTID_NONE -1
2124 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
2125 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2127 #define HIDPP_FF_MAX_PARAMS 20
2128 #define HIDPP_FF_RESERVED_SLOTS 1
2130 struct hidpp_ff_private_data {
2131 struct hidpp_device *hidpp;
2139 struct workqueue_struct *wq;
2140 atomic_t workqueue_size;
2143 struct hidpp_ff_work_data {
2144 struct work_struct work;
2145 struct hidpp_ff_private_data *data;
2148 u8 params[HIDPP_FF_MAX_PARAMS];
2152 static const signed short hidpp_ff_effects[] = {
2167 static const signed short hidpp_ff_effects_v2[] = {
2174 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2175 HIDPP_FF_EFFECT_SPRING,
2176 HIDPP_FF_EFFECT_FRICTION,
2177 HIDPP_FF_EFFECT_DAMPER,
2178 HIDPP_FF_EFFECT_INERTIA
2181 static const char *HIDPP_FF_CONDITION_NAMES[] = {
2189 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2193 for (i = 0; i < data->num_effects; i++)
2194 if (data->effect_ids[i] == effect_id)
2200 static void hidpp_ff_work_handler(struct work_struct *w)
2202 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2203 struct hidpp_ff_private_data *data = wd->data;
2204 struct hidpp_report response;
2208 /* add slot number if needed */
2209 switch (wd->effect_id) {
2210 case HIDPP_FF_EFFECTID_AUTOCENTER:
2211 wd->params[0] = data->slot_autocenter;
2213 case HIDPP_FF_EFFECTID_NONE:
2214 /* leave slot as zero */
2217 /* find current slot for effect */
2218 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2222 /* send command and wait for reply */
2223 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2224 wd->command, wd->params, wd->size, &response);
2227 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2231 /* parse return data */
2232 switch (wd->command) {
2233 case HIDPP_FF_DOWNLOAD_EFFECT:
2234 slot = response.fap.params[0];
2235 if (slot > 0 && slot <= data->num_effects) {
2236 if (wd->effect_id >= 0)
2237 /* regular effect uploaded */
2238 data->effect_ids[slot-1] = wd->effect_id;
2239 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2240 /* autocenter spring uploaded */
2241 data->slot_autocenter = slot;
2244 case HIDPP_FF_DESTROY_EFFECT:
2245 if (wd->effect_id >= 0)
2246 /* regular effect destroyed */
2247 data->effect_ids[wd->params[0]-1] = -1;
2248 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2249 /* autocenter spring destoyed */
2250 data->slot_autocenter = 0;
2252 case HIDPP_FF_SET_GLOBAL_GAINS:
2253 data->gain = (wd->params[0] << 8) + wd->params[1];
2255 case HIDPP_FF_SET_APERTURE:
2256 data->range = (wd->params[0] << 8) + wd->params[1];
2259 /* no action needed */
2264 atomic_dec(&data->workqueue_size);
2268 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2270 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2276 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2279 wd->effect_id = effect_id;
2280 wd->command = command;
2282 memcpy(wd->params, params, size);
2284 s = atomic_inc_return(&data->workqueue_size);
2285 queue_work(data->wq, &wd->work);
2287 /* warn about excessive queue size */
2288 if (s >= 20 && s % 20 == 0)
2289 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2294 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2296 struct hidpp_ff_private_data *data = dev->ff->private;
2301 /* set common parameters */
2302 params[2] = effect->replay.length >> 8;
2303 params[3] = effect->replay.length & 255;
2304 params[4] = effect->replay.delay >> 8;
2305 params[5] = effect->replay.delay & 255;
2307 switch (effect->type) {
2309 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2310 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2311 params[6] = force >> 8;
2312 params[7] = force & 255;
2313 params[8] = effect->u.constant.envelope.attack_level >> 7;
2314 params[9] = effect->u.constant.envelope.attack_length >> 8;
2315 params[10] = effect->u.constant.envelope.attack_length & 255;
2316 params[11] = effect->u.constant.envelope.fade_level >> 7;
2317 params[12] = effect->u.constant.envelope.fade_length >> 8;
2318 params[13] = effect->u.constant.envelope.fade_length & 255;
2320 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2321 effect->u.constant.level,
2322 effect->direction, force);
2323 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2324 effect->u.constant.envelope.attack_level,
2325 effect->u.constant.envelope.attack_length,
2326 effect->u.constant.envelope.fade_level,
2327 effect->u.constant.envelope.fade_length);
2331 switch (effect->u.periodic.waveform) {
2333 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2336 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2339 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2342 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2345 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2348 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2351 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2352 params[6] = effect->u.periodic.magnitude >> 8;
2353 params[7] = effect->u.periodic.magnitude & 255;
2354 params[8] = effect->u.periodic.offset >> 8;
2355 params[9] = effect->u.periodic.offset & 255;
2356 params[10] = effect->u.periodic.period >> 8;
2357 params[11] = effect->u.periodic.period & 255;
2358 params[12] = effect->u.periodic.phase >> 8;
2359 params[13] = effect->u.periodic.phase & 255;
2360 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2361 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2362 params[16] = effect->u.periodic.envelope.attack_length & 255;
2363 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2364 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2365 params[19] = effect->u.periodic.envelope.fade_length & 255;
2367 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2368 effect->u.periodic.magnitude, effect->direction,
2369 effect->u.periodic.offset,
2370 effect->u.periodic.period,
2371 effect->u.periodic.phase);
2372 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2373 effect->u.periodic.envelope.attack_level,
2374 effect->u.periodic.envelope.attack_length,
2375 effect->u.periodic.envelope.fade_level,
2376 effect->u.periodic.envelope.fade_length);
2380 params[1] = HIDPP_FF_EFFECT_RAMP;
2381 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2382 params[6] = force >> 8;
2383 params[7] = force & 255;
2384 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2385 params[8] = force >> 8;
2386 params[9] = force & 255;
2387 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2388 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2389 params[12] = effect->u.ramp.envelope.attack_length & 255;
2390 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2391 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2392 params[15] = effect->u.ramp.envelope.fade_length & 255;
2394 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2395 effect->u.ramp.start_level,
2396 effect->u.ramp.end_level,
2397 effect->direction, force);
2398 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2399 effect->u.ramp.envelope.attack_level,
2400 effect->u.ramp.envelope.attack_length,
2401 effect->u.ramp.envelope.fade_level,
2402 effect->u.ramp.envelope.fade_length);
2408 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2409 params[6] = effect->u.condition[0].left_saturation >> 9;
2410 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2411 params[8] = effect->u.condition[0].left_coeff >> 8;
2412 params[9] = effect->u.condition[0].left_coeff & 255;
2413 params[10] = effect->u.condition[0].deadband >> 9;
2414 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2415 params[12] = effect->u.condition[0].center >> 8;
2416 params[13] = effect->u.condition[0].center & 255;
2417 params[14] = effect->u.condition[0].right_coeff >> 8;
2418 params[15] = effect->u.condition[0].right_coeff & 255;
2419 params[16] = effect->u.condition[0].right_saturation >> 9;
2420 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2422 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2423 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2424 effect->u.condition[0].left_coeff,
2425 effect->u.condition[0].left_saturation,
2426 effect->u.condition[0].right_coeff,
2427 effect->u.condition[0].right_saturation);
2428 dbg_hid(" deadband=%d, center=%d\n",
2429 effect->u.condition[0].deadband,
2430 effect->u.condition[0].center);
2433 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2437 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2440 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2442 struct hidpp_ff_private_data *data = dev->ff->private;
2445 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2447 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2449 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2452 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2454 struct hidpp_ff_private_data *data = dev->ff->private;
2457 dbg_hid("Erasing effect %d.\n", effect_id);
2459 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2462 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2464 struct hidpp_ff_private_data *data = dev->ff->private;
2465 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2467 dbg_hid("Setting autocenter to %d.\n", magnitude);
2469 /* start a standard spring effect */
2470 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2471 /* zero delay and duration */
2472 params[2] = params[3] = params[4] = params[5] = 0;
2473 /* set coeff to 25% of saturation */
2474 params[8] = params[14] = magnitude >> 11;
2475 params[9] = params[15] = (magnitude >> 3) & 255;
2476 params[6] = params[16] = magnitude >> 9;
2477 params[7] = params[17] = (magnitude >> 1) & 255;
2478 /* zero deadband and center */
2479 params[10] = params[11] = params[12] = params[13] = 0;
2481 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2484 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2486 struct hidpp_ff_private_data *data = dev->ff->private;
2489 dbg_hid("Setting gain to %d.\n", gain);
2491 params[0] = gain >> 8;
2492 params[1] = gain & 255;
2493 params[2] = 0; /* no boost */
2496 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2499 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2501 struct hid_device *hid = to_hid_device(dev);
2502 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2503 struct input_dev *idev = hidinput->input;
2504 struct hidpp_ff_private_data *data = idev->ff->private;
2506 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2509 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2511 struct hid_device *hid = to_hid_device(dev);
2512 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2513 struct input_dev *idev = hidinput->input;
2514 struct hidpp_ff_private_data *data = idev->ff->private;
2516 int range = simple_strtoul(buf, NULL, 10);
2518 range = clamp(range, 180, 900);
2520 params[0] = range >> 8;
2521 params[1] = range & 0x00FF;
2523 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2528 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2530 static void hidpp_ff_destroy(struct ff_device *ff)
2532 struct hidpp_ff_private_data *data = ff->private;
2533 struct hid_device *hid = data->hidpp->hid_dev;
2535 hid_info(hid, "Unloading HID++ force feedback.\n");
2537 device_remove_file(&hid->dev, &dev_attr_range);
2538 destroy_workqueue(data->wq);
2539 kfree(data->effect_ids);
2542 static int hidpp_ff_init(struct hidpp_device *hidpp,
2543 struct hidpp_ff_private_data *data)
2545 struct hid_device *hid = hidpp->hid_dev;
2546 struct hid_input *hidinput;
2547 struct input_dev *dev;
2548 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2549 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2550 struct ff_device *ff;
2551 int error, j, num_slots = data->num_effects;
2554 if (list_empty(&hid->inputs)) {
2555 hid_err(hid, "no inputs found\n");
2558 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2559 dev = hidinput->input;
2562 hid_err(hid, "Struct input_dev not set!\n");
2566 /* Get firmware release */
2567 version = bcdDevice & 255;
2569 /* Set supported force feedback capabilities */
2570 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2571 set_bit(hidpp_ff_effects[j], dev->ffbit);
2573 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2574 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2576 error = input_ff_create(dev, num_slots);
2579 hid_err(dev, "Failed to create FF device!\n");
2583 * Create a copy of passed data, so we can transfer memory
2584 * ownership to FF core
2586 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2589 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2590 if (!data->effect_ids) {
2594 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2596 kfree(data->effect_ids);
2601 data->hidpp = hidpp;
2602 data->version = version;
2603 for (j = 0; j < num_slots; j++)
2604 data->effect_ids[j] = -1;
2609 ff->upload = hidpp_ff_upload_effect;
2610 ff->erase = hidpp_ff_erase_effect;
2611 ff->playback = hidpp_ff_playback;
2612 ff->set_gain = hidpp_ff_set_gain;
2613 ff->set_autocenter = hidpp_ff_set_autocenter;
2614 ff->destroy = hidpp_ff_destroy;
2616 /* Create sysfs interface */
2617 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2619 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2621 /* init the hardware command queue */
2622 atomic_set(&data->workqueue_size, 0);
2624 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2630 /* ************************************************************************** */
2632 /* Device Support */
2634 /* ************************************************************************** */
2636 /* -------------------------------------------------------------------------- */
2637 /* Touchpad HID++ devices */
2638 /* -------------------------------------------------------------------------- */
2640 #define WTP_MANUAL_RESOLUTION 39
2645 u8 mt_feature_index;
2646 u8 button_feature_index;
2649 unsigned int resolution;
2652 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2653 struct hid_field *field, struct hid_usage *usage,
2654 unsigned long **bit, int *max)
2659 static void wtp_populate_input(struct hidpp_device *hidpp,
2660 struct input_dev *input_dev)
2662 struct wtp_data *wd = hidpp->private_data;
2664 __set_bit(EV_ABS, input_dev->evbit);
2665 __set_bit(EV_KEY, input_dev->evbit);
2666 __clear_bit(EV_REL, input_dev->evbit);
2667 __clear_bit(EV_LED, input_dev->evbit);
2669 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2670 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2671 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2672 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2674 /* Max pressure is not given by the devices, pick one */
2675 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2677 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2679 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2680 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2682 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2684 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2685 INPUT_MT_DROP_UNUSED);
2688 static void wtp_touch_event(struct hidpp_device *hidpp,
2689 struct hidpp_touchpad_raw_xy_finger *touch_report)
2691 struct wtp_data *wd = hidpp->private_data;
2694 if (!touch_report->finger_id || touch_report->contact_type)
2695 /* no actual data */
2698 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2700 input_mt_slot(hidpp->input, slot);
2701 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2702 touch_report->contact_status);
2703 if (touch_report->contact_status) {
2704 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2706 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2707 wd->flip_y ? wd->y_size - touch_report->y :
2709 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2710 touch_report->area);
2714 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2715 struct hidpp_touchpad_raw_xy *raw)
2719 for (i = 0; i < 2; i++)
2720 wtp_touch_event(hidpp, &(raw->fingers[i]));
2722 if (raw->end_of_frame &&
2723 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2724 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2726 if (raw->end_of_frame || raw->finger_count <= 2) {
2727 input_mt_sync_frame(hidpp->input);
2728 input_sync(hidpp->input);
2732 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2734 struct wtp_data *wd = hidpp->private_data;
2735 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2736 (data[7] >> 4) * (data[7] >> 4)) / 2;
2737 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2738 (data[13] >> 4) * (data[13] >> 4)) / 2;
2739 struct hidpp_touchpad_raw_xy raw = {
2740 .timestamp = data[1],
2744 .contact_status = !!data[7],
2745 .x = get_unaligned_le16(&data[3]),
2746 .y = get_unaligned_le16(&data[5]),
2749 .finger_id = data[2],
2752 .contact_status = !!data[13],
2753 .x = get_unaligned_le16(&data[9]),
2754 .y = get_unaligned_le16(&data[11]),
2757 .finger_id = data[8],
2760 .finger_count = wd->maxcontacts,
2762 .end_of_frame = (data[0] >> 7) == 0,
2763 .button = data[0] & 0x01,
2766 wtp_send_raw_xy_event(hidpp, &raw);
2771 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2773 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2774 struct wtp_data *wd = hidpp->private_data;
2775 struct hidpp_report *report = (struct hidpp_report *)data;
2776 struct hidpp_touchpad_raw_xy raw;
2778 if (!wd || !hidpp->input)
2784 hid_err(hdev, "Received HID report of bad size (%d)",
2788 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2789 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2790 !!(data[1] & 0x01));
2791 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2792 !!(data[1] & 0x02));
2793 input_sync(hidpp->input);
2798 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2800 case REPORT_ID_HIDPP_LONG:
2801 /* size is already checked in hidpp_raw_event. */
2802 if ((report->fap.feature_index != wd->mt_feature_index) ||
2803 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2805 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2807 wtp_send_raw_xy_event(hidpp, &raw);
2814 static int wtp_get_config(struct hidpp_device *hidpp)
2816 struct wtp_data *wd = hidpp->private_data;
2817 struct hidpp_touchpad_raw_info raw_info = {0};
2821 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2822 &wd->mt_feature_index, &feature_type);
2824 /* means that the device is not powered up */
2827 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2832 wd->x_size = raw_info.x_size;
2833 wd->y_size = raw_info.y_size;
2834 wd->maxcontacts = raw_info.maxcontacts;
2835 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2836 wd->resolution = raw_info.res;
2837 if (!wd->resolution)
2838 wd->resolution = WTP_MANUAL_RESOLUTION;
2843 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2845 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2846 struct wtp_data *wd;
2848 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2853 hidpp->private_data = wd;
2858 static int wtp_connect(struct hid_device *hdev, bool connected)
2860 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2861 struct wtp_data *wd = hidpp->private_data;
2865 ret = wtp_get_config(hidpp);
2867 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2872 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2876 /* ------------------------------------------------------------------------- */
2877 /* Logitech M560 devices */
2878 /* ------------------------------------------------------------------------- */
2881 * Logitech M560 protocol overview
2883 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2884 * the sides buttons are pressed, it sends some keyboard keys events
2885 * instead of buttons ones.
2886 * To complicate things further, the middle button keys sequence
2887 * is different from the odd press and the even press.
2889 * forward button -> Super_R
2890 * backward button -> Super_L+'d' (press only)
2891 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2892 * 2nd time: left-click (press only)
2893 * NB: press-only means that when the button is pressed, the
2894 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2895 * together sequentially; instead when the button is released, no event is
2899 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2900 * the mouse reacts differently:
2901 * - it never sends a keyboard key event
2902 * - for the three mouse button it sends:
2903 * middle button press 11<xx>0a 3500af00...
2904 * side 1 button (forward) press 11<xx>0a 3500b000...
2905 * side 2 button (backward) press 11<xx>0a 3500ae00...
2906 * middle/side1/side2 button release 11<xx>0a 35000000...
2909 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2911 /* how buttons are mapped in the report */
2912 #define M560_MOUSE_BTN_LEFT 0x01
2913 #define M560_MOUSE_BTN_RIGHT 0x02
2914 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2915 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2917 #define M560_SUB_ID 0x0a
2918 #define M560_BUTTON_MODE_REGISTER 0x35
2920 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2922 struct hidpp_report response;
2923 struct hidpp_device *hidpp_dev;
2925 hidpp_dev = hid_get_drvdata(hdev);
2927 return hidpp_send_rap_command_sync(
2929 REPORT_ID_HIDPP_SHORT,
2931 M560_BUTTON_MODE_REGISTER,
2932 (u8 *)m560_config_parameter,
2933 sizeof(m560_config_parameter),
2938 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2940 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2943 if (!hidpp->input) {
2944 hid_err(hdev, "error in parameter\n");
2949 hid_err(hdev, "error in report\n");
2953 if (data[0] == REPORT_ID_HIDPP_LONG &&
2954 data[2] == M560_SUB_ID && data[6] == 0x00) {
2956 * m560 mouse report for middle, forward and backward button
2959 * data[1] = device-id
2961 * data[5] = 0xaf -> middle
2964 * 0x00 -> release all
2970 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2973 input_report_key(hidpp->input, BTN_FORWARD, 1);
2976 input_report_key(hidpp->input, BTN_BACK, 1);
2979 input_report_key(hidpp->input, BTN_BACK, 0);
2980 input_report_key(hidpp->input, BTN_FORWARD, 0);
2981 input_report_key(hidpp->input, BTN_MIDDLE, 0);
2984 hid_err(hdev, "error in report\n");
2987 input_sync(hidpp->input);
2989 } else if (data[0] == 0x02) {
2991 * Logitech M560 mouse report
2993 * data[0] = type (0x02)
2994 * data[1..2] = buttons
3001 input_report_key(hidpp->input, BTN_LEFT,
3002 !!(data[1] & M560_MOUSE_BTN_LEFT));
3003 input_report_key(hidpp->input, BTN_RIGHT,
3004 !!(data[1] & M560_MOUSE_BTN_RIGHT));
3006 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3007 input_report_rel(hidpp->input, REL_HWHEEL, -1);
3008 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3010 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3011 input_report_rel(hidpp->input, REL_HWHEEL, 1);
3012 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3016 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
3017 input_report_rel(hidpp->input, REL_X, v);
3019 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
3020 input_report_rel(hidpp->input, REL_Y, v);
3022 v = hid_snto32(data[6], 8);
3024 hidpp_scroll_counter_handle_scroll(hidpp->input,
3025 &hidpp->vertical_wheel_counter, v);
3027 input_sync(hidpp->input);
3033 static void m560_populate_input(struct hidpp_device *hidpp,
3034 struct input_dev *input_dev)
3036 __set_bit(EV_KEY, input_dev->evbit);
3037 __set_bit(BTN_MIDDLE, input_dev->keybit);
3038 __set_bit(BTN_RIGHT, input_dev->keybit);
3039 __set_bit(BTN_LEFT, input_dev->keybit);
3040 __set_bit(BTN_BACK, input_dev->keybit);
3041 __set_bit(BTN_FORWARD, input_dev->keybit);
3043 __set_bit(EV_REL, input_dev->evbit);
3044 __set_bit(REL_X, input_dev->relbit);
3045 __set_bit(REL_Y, input_dev->relbit);
3046 __set_bit(REL_WHEEL, input_dev->relbit);
3047 __set_bit(REL_HWHEEL, input_dev->relbit);
3048 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3049 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3052 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3053 struct hid_field *field, struct hid_usage *usage,
3054 unsigned long **bit, int *max)
3059 /* ------------------------------------------------------------------------- */
3060 /* Logitech K400 devices */
3061 /* ------------------------------------------------------------------------- */
3064 * The Logitech K400 keyboard has an embedded touchpad which is seen
3065 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3066 * tap-to-click but the setting is not remembered accross reset, annoying some
3069 * We can toggle this feature from the host by using the feature 0x6010:
3073 struct k400_private_data {
3077 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3079 struct k400_private_data *k400 = hidpp->private_data;
3080 struct hidpp_touchpad_fw_items items = {};
3084 if (!k400->feature_index) {
3085 ret = hidpp_root_get_feature(hidpp,
3086 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3087 &k400->feature_index, &feature_type);
3089 /* means that the device is not powered up */
3093 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3100 static int k400_allocate(struct hid_device *hdev)
3102 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3103 struct k400_private_data *k400;
3105 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3110 hidpp->private_data = k400;
3115 static int k400_connect(struct hid_device *hdev, bool connected)
3117 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3119 if (!disable_tap_to_click)
3122 return k400_disable_tap_to_click(hidpp);
3125 /* ------------------------------------------------------------------------- */
3126 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
3127 /* ------------------------------------------------------------------------- */
3129 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3131 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3132 struct hidpp_ff_private_data *data)
3134 struct hidpp_report response;
3135 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3136 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3140 /* initialize with zero autocenter to get wheel in usable state */
3142 dbg_hid("Setting autocenter to 0.\n");
3143 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3144 HIDPP_FF_DOWNLOAD_EFFECT,
3145 params, ARRAY_SIZE(params),
3148 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3150 data->slot_autocenter = response.fap.params[0];
3155 static int g920_get_config(struct hidpp_device *hidpp,
3156 struct hidpp_ff_private_data *data)
3158 struct hidpp_report response;
3162 memset(data, 0, sizeof(*data));
3164 /* Find feature and store for later use */
3165 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3166 &data->feature_index, &feature_type);
3170 /* Read number of slots available in device */
3171 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3178 hid_err(hidpp->hid_dev,
3179 "%s: received protocol error 0x%02x\n", __func__, ret);
3183 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3185 /* reset all forces */
3186 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3191 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3193 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3194 HIDPP_FF_GET_APERTURE,
3198 hid_warn(hidpp->hid_dev,
3199 "Failed to read range from device!\n");
3202 900 : get_unaligned_be16(&response.fap.params[0]);
3204 /* Read the current gain values */
3205 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3206 HIDPP_FF_GET_GLOBAL_GAINS,
3210 hid_warn(hidpp->hid_dev,
3211 "Failed to read gain values from device!\n");
3213 0xffff : get_unaligned_be16(&response.fap.params[0]);
3215 /* ignore boost value at response.fap.params[2] */
3217 return g920_ff_set_autocenter(hidpp, data);
3220 /* -------------------------------------------------------------------------- */
3221 /* Logitech Dinovo Mini keyboard with builtin touchpad */
3222 /* -------------------------------------------------------------------------- */
3223 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3225 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3226 struct hid_field *field, struct hid_usage *usage,
3227 unsigned long **bit, int *max)
3229 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3232 switch (usage->hid & HID_USAGE) {
3233 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3240 /* -------------------------------------------------------------------------- */
3241 /* HID++1.0 devices which use HID++ reports for their wheels */
3242 /* -------------------------------------------------------------------------- */
3243 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3245 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3246 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3247 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3250 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3261 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3267 input_report_rel(hidpp->input, REL_WHEEL, value);
3268 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3269 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3270 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3271 input_sync(hidpp->input);
3276 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3277 struct input_dev *input_dev)
3279 __set_bit(EV_REL, input_dev->evbit);
3280 __set_bit(REL_WHEEL, input_dev->relbit);
3281 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3282 __set_bit(REL_HWHEEL, input_dev->relbit);
3283 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3286 /* -------------------------------------------------------------------------- */
3287 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3288 /* -------------------------------------------------------------------------- */
3289 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3291 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3292 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3293 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3296 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3307 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3308 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3312 * Buttons are either delivered through the regular mouse report *or*
3313 * through the extra buttons report. At least for button 6 how it is
3314 * delivered differs per receiver firmware version. Even receivers with
3315 * the same usb-id show different behavior, so we handle both cases.
3317 for (i = 0; i < 8; i++)
3318 input_report_key(hidpp->input, BTN_MOUSE + i,
3319 (data[3] & (1 << i)));
3321 /* Some mice report events on button 9+, use BTN_MISC */
3322 for (i = 0; i < 8; i++)
3323 input_report_key(hidpp->input, BTN_MISC + i,
3324 (data[4] & (1 << i)));
3326 input_sync(hidpp->input);
3330 static void hidpp10_extra_mouse_buttons_populate_input(
3331 struct hidpp_device *hidpp, struct input_dev *input_dev)
3333 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3334 __set_bit(BTN_0, input_dev->keybit);
3335 __set_bit(BTN_1, input_dev->keybit);
3336 __set_bit(BTN_2, input_dev->keybit);
3337 __set_bit(BTN_3, input_dev->keybit);
3338 __set_bit(BTN_4, input_dev->keybit);
3339 __set_bit(BTN_5, input_dev->keybit);
3340 __set_bit(BTN_6, input_dev->keybit);
3341 __set_bit(BTN_7, input_dev->keybit);
3344 /* -------------------------------------------------------------------------- */
3345 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3346 /* -------------------------------------------------------------------------- */
3348 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3349 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3350 u8 *_rdesc, unsigned int *rsize)
3352 /* Note 0 terminated so we can use strnstr to search for this. */
3353 static const char consumer_rdesc_start[] = {
3354 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3355 0x09, 0x01, /* USAGE (Consumer Control) */
3356 0xA1, 0x01, /* COLLECTION (Application) */
3357 0x85, 0x03, /* REPORT_ID = 3 */
3358 0x75, 0x10, /* REPORT_SIZE (16) */
3359 0x95, 0x02, /* REPORT_COUNT (2) */
3360 0x15, 0x01, /* LOGICAL_MIN (1) */
3361 0x26, 0x00 /* LOGICAL_MAX (... */
3363 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3366 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3367 size = *rsize - (consumer_rdesc - rdesc);
3368 if (consumer_rdesc && size >= 25) {
3369 consumer_rdesc[15] = 0x7f;
3370 consumer_rdesc[16] = 0x10;
3371 consumer_rdesc[20] = 0x7f;
3372 consumer_rdesc[21] = 0x10;
3377 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3379 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3380 HIDPP_ENABLE_CONSUMER_REPORT,
3381 HIDPP_ENABLE_CONSUMER_REPORT);
3384 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3387 u8 consumer_report[5];
3392 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3393 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3397 * Build a normal consumer report (3) out of the data, this detour
3398 * is necessary to get some keyboards to report their 0x10xx usages.
3400 consumer_report[0] = 0x03;
3401 memcpy(&consumer_report[1], &data[3], 4);
3402 /* We are called from atomic context */
3403 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3404 consumer_report, 5, 1);
3409 /* -------------------------------------------------------------------------- */
3410 /* High-resolution scroll wheels */
3411 /* -------------------------------------------------------------------------- */
3413 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3418 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
3419 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3421 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3422 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
3423 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3425 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
3426 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3432 if (multiplier == 0)
3435 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3436 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3440 /* -------------------------------------------------------------------------- */
3441 /* Generic HID++ devices */
3442 /* -------------------------------------------------------------------------- */
3444 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3445 unsigned int *rsize)
3447 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3452 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3453 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3454 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3455 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3460 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3461 struct hid_field *field, struct hid_usage *usage,
3462 unsigned long **bit, int *max)
3464 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3469 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3470 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3471 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3472 field->application != HID_GD_MOUSE)
3473 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3475 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3476 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3481 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3482 struct hid_field *field, struct hid_usage *usage,
3483 unsigned long **bit, int *max)
3485 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3490 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3491 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3492 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3493 usage->code == ABS_Y || usage->code == ABS_Z ||
3494 usage->code == ABS_RZ)) {
3495 field->application = HID_GD_MULTIAXIS;
3503 static void hidpp_populate_input(struct hidpp_device *hidpp,
3504 struct input_dev *input)
3506 hidpp->input = input;
3508 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3509 wtp_populate_input(hidpp, input);
3510 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3511 m560_populate_input(hidpp, input);
3513 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3514 hidpp10_wheel_populate_input(hidpp, input);
3516 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3517 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3520 static int hidpp_input_configured(struct hid_device *hdev,
3521 struct hid_input *hidinput)
3523 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3524 struct input_dev *input = hidinput->input;
3529 hidpp_populate_input(hidpp, input);
3534 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3537 struct hidpp_report *question = hidpp->send_receive_buf;
3538 struct hidpp_report *answer = hidpp->send_receive_buf;
3539 struct hidpp_report *report = (struct hidpp_report *)data;
3543 * If the mutex is locked then we have a pending answer from a
3544 * previously sent command.
3546 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3548 * Check for a correct hidpp20 answer or the corresponding
3551 if (hidpp_match_answer(question, report) ||
3552 hidpp_match_error(question, report)) {
3554 hidpp->answer_available = true;
3555 wake_up(&hidpp->wait);
3557 * This was an answer to a command that this driver sent
3558 * We return 1 to hid-core to avoid forwarding the
3559 * command upstream as it has been treated by the driver
3566 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3567 atomic_set(&hidpp->connected,
3568 !(report->rap.params[0] & (1 << 6)));
3569 if (schedule_work(&hidpp->work) == 0)
3570 dbg_hid("%s: connect event already queued\n", __func__);
3574 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3575 data[0] == REPORT_ID_HIDPP_SHORT &&
3576 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3577 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3578 dev_err_ratelimited(&hidpp->hid_dev->dev,
3579 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3580 dev_err_ratelimited(&hidpp->hid_dev->dev,
3581 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3584 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3585 ret = hidpp20_battery_event_1000(hidpp, data, size);
3588 ret = hidpp20_battery_event_1004(hidpp, data, size);
3591 ret = hidpp_solar_battery_event(hidpp, data, size);
3594 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3599 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3600 ret = hidpp10_battery_event(hidpp, data, size);
3605 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3606 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3611 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3612 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3617 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3618 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3626 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3629 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3635 /* Generic HID++ processing. */
3637 case REPORT_ID_HIDPP_VERY_LONG:
3638 if (size != hidpp->very_long_report_length) {
3639 hid_err(hdev, "received hid++ report of bad size (%d)",
3643 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3645 case REPORT_ID_HIDPP_LONG:
3646 if (size != HIDPP_REPORT_LONG_LENGTH) {
3647 hid_err(hdev, "received hid++ report of bad size (%d)",
3651 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3653 case REPORT_ID_HIDPP_SHORT:
3654 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3655 hid_err(hdev, "received hid++ report of bad size (%d)",
3659 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3663 /* If no report is available for further processing, skip calling
3664 * raw_event of subclasses. */
3668 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3669 return wtp_raw_event(hdev, data, size);
3670 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3671 return m560_raw_event(hdev, data, size);
3676 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3677 struct hid_usage *usage, __s32 value)
3679 /* This function will only be called for scroll events, due to the
3680 * restriction imposed in hidpp_usages.
3682 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3683 struct hidpp_scroll_counter *counter;
3688 counter = &hidpp->vertical_wheel_counter;
3689 /* A scroll event may occur before the multiplier has been retrieved or
3690 * the input device set, or high-res scroll enabling may fail. In such
3691 * cases we must return early (falling back to default behaviour) to
3692 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3694 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
3695 || hidpp->input == NULL || counter->wheel_multiplier == 0)
3698 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3702 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3704 static atomic_t battery_no = ATOMIC_INIT(0);
3705 struct power_supply_config cfg = { .drv_data = hidpp };
3706 struct power_supply_desc *desc = &hidpp->battery.desc;
3707 enum power_supply_property *battery_props;
3708 struct hidpp_battery *battery;
3709 unsigned int num_battery_props;
3713 if (hidpp->battery.ps)
3716 hidpp->battery.feature_index = 0xff;
3717 hidpp->battery.solar_feature_index = 0xff;
3718 hidpp->battery.voltage_feature_index = 0xff;
3720 if (hidpp->protocol_major >= 2) {
3721 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3722 ret = hidpp_solar_request_battery_event(hidpp);
3724 /* we only support one battery feature right now, so let's
3725 first check the ones that support battery level first
3726 and leave voltage for last */
3727 ret = hidpp20_query_battery_info_1000(hidpp);
3729 ret = hidpp20_query_battery_info_1004(hidpp);
3731 ret = hidpp20_query_battery_voltage_info(hidpp);
3736 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3738 ret = hidpp10_query_battery_status(hidpp);
3740 ret = hidpp10_query_battery_mileage(hidpp);
3743 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3745 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3747 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3750 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3751 hidpp_battery_props,
3752 sizeof(hidpp_battery_props),
3757 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3759 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
3760 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
3761 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3762 battery_props[num_battery_props++] =
3763 POWER_SUPPLY_PROP_CAPACITY;
3765 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3766 battery_props[num_battery_props++] =
3767 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3769 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3770 battery_props[num_battery_props++] =
3771 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3773 battery = &hidpp->battery;
3775 n = atomic_inc_return(&battery_no) - 1;
3776 desc->properties = battery_props;
3777 desc->num_properties = num_battery_props;
3778 desc->get_property = hidpp_battery_get_property;
3779 sprintf(battery->name, "hidpp_battery_%ld", n);
3780 desc->name = battery->name;
3781 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3782 desc->use_for_apm = 0;
3784 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3787 if (IS_ERR(battery->ps))
3788 return PTR_ERR(battery->ps);
3790 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3795 static void hidpp_overwrite_name(struct hid_device *hdev)
3797 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3800 if (hidpp->protocol_major < 2)
3803 name = hidpp_get_device_name(hidpp);
3806 hid_err(hdev, "unable to retrieve the name of the device");
3808 dbg_hid("HID++: Got name: %s\n", name);
3809 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3815 static int hidpp_input_open(struct input_dev *dev)
3817 struct hid_device *hid = input_get_drvdata(dev);
3819 return hid_hw_open(hid);
3822 static void hidpp_input_close(struct input_dev *dev)
3824 struct hid_device *hid = input_get_drvdata(dev);
3829 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3831 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3832 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3837 input_set_drvdata(input_dev, hdev);
3838 input_dev->open = hidpp_input_open;
3839 input_dev->close = hidpp_input_close;
3841 input_dev->name = hidpp->name;
3842 input_dev->phys = hdev->phys;
3843 input_dev->uniq = hdev->uniq;
3844 input_dev->id.bustype = hdev->bus;
3845 input_dev->id.vendor = hdev->vendor;
3846 input_dev->id.product = hdev->product;
3847 input_dev->id.version = hdev->version;
3848 input_dev->dev.parent = &hdev->dev;
3853 static void hidpp_connect_event(struct hidpp_device *hidpp)
3855 struct hid_device *hdev = hidpp->hid_dev;
3857 bool connected = atomic_read(&hidpp->connected);
3858 struct input_dev *input;
3859 char *name, *devm_name;
3862 if (hidpp->battery.ps) {
3863 hidpp->battery.online = false;
3864 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3865 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3866 power_supply_changed(hidpp->battery.ps);
3871 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3872 ret = wtp_connect(hdev, connected);
3875 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3876 ret = m560_send_config_command(hdev, connected);
3879 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3880 ret = k400_connect(hdev, connected);
3885 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3886 ret = hidpp10_wheel_connect(hidpp);
3891 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3892 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3897 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3898 ret = hidpp10_consumer_keys_connect(hidpp);
3903 /* the device is already connected, we can ask for its name and
3905 if (!hidpp->protocol_major) {
3906 ret = hidpp_root_get_protocol_version(hidpp);
3908 hid_err(hdev, "Can not get the protocol version.\n");
3913 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3914 name = hidpp_get_device_name(hidpp);
3916 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3922 hidpp->name = devm_name;
3926 hidpp_initialize_battery(hidpp);
3928 /* forward current battery state */
3929 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3930 hidpp10_enable_battery_reporting(hidpp);
3931 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3932 hidpp10_query_battery_mileage(hidpp);
3934 hidpp10_query_battery_status(hidpp);
3935 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3936 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3937 hidpp20_query_battery_voltage_info(hidpp);
3938 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
3939 hidpp20_query_battery_info_1004(hidpp);
3941 hidpp20_query_battery_info_1000(hidpp);
3943 if (hidpp->battery.ps)
3944 power_supply_changed(hidpp->battery.ps);
3946 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3947 hi_res_scroll_enable(hidpp);
3949 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3950 /* if the input nodes are already created, we can stop now */
3953 input = hidpp_allocate_input(hdev);
3955 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3959 hidpp_populate_input(hidpp, input);
3961 ret = input_register_device(input);
3963 input_free_device(input);
3965 hidpp->delayed_input = input;
3968 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3970 static struct attribute *sysfs_attrs[] = {
3971 &dev_attr_builtin_power_supply.attr,
3975 static const struct attribute_group ps_attribute_group = {
3976 .attrs = sysfs_attrs
3979 static int hidpp_get_report_length(struct hid_device *hdev, int id)
3981 struct hid_report_enum *re;
3982 struct hid_report *report;
3984 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3985 report = re->report_id_hash[id];
3989 return report->field[0]->report_count + 1;
3992 static u8 hidpp_validate_device(struct hid_device *hdev)
3994 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3995 int id, report_length;
3996 u8 supported_reports = 0;
3998 id = REPORT_ID_HIDPP_SHORT;
3999 report_length = hidpp_get_report_length(hdev, id);
4000 if (report_length) {
4001 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4004 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4007 id = REPORT_ID_HIDPP_LONG;
4008 report_length = hidpp_get_report_length(hdev, id);
4009 if (report_length) {
4010 if (report_length < HIDPP_REPORT_LONG_LENGTH)
4013 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4016 id = REPORT_ID_HIDPP_VERY_LONG;
4017 report_length = hidpp_get_report_length(hdev, id);
4018 if (report_length) {
4019 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4020 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4023 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4024 hidpp->very_long_report_length = report_length;
4027 return supported_reports;
4030 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4034 static bool hidpp_application_equals(struct hid_device *hdev,
4035 unsigned int application)
4037 struct list_head *report_list;
4038 struct hid_report *report;
4040 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4041 report = list_first_entry_or_null(report_list, struct hid_report, list);
4042 return report && report->application == application;
4045 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4047 struct hidpp_device *hidpp;
4050 unsigned int connect_mask = HID_CONNECT_DEFAULT;
4051 struct hidpp_ff_private_data data;
4053 /* report_fixup needs drvdata to be set before we call hid_parse */
4054 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4058 hidpp->hid_dev = hdev;
4059 hidpp->name = hdev->name;
4060 hidpp->quirks = id->driver_data;
4061 hid_set_drvdata(hdev, hidpp);
4063 ret = hid_parse(hdev);
4065 hid_err(hdev, "%s:parse failed\n", __func__);
4070 * Make sure the device is HID++ capable, otherwise treat as generic HID
4072 hidpp->supported_reports = hidpp_validate_device(hdev);
4074 if (!hidpp->supported_reports) {
4075 hid_set_drvdata(hdev, NULL);
4076 devm_kfree(&hdev->dev, hidpp);
4077 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4080 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4081 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
4083 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4084 hidpp_application_equals(hdev, HID_GD_MOUSE))
4085 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4086 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4088 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4089 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4090 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4092 if (disable_raw_mode) {
4093 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
4094 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
4097 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4098 ret = wtp_allocate(hdev, id);
4101 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4102 ret = k400_allocate(hdev);
4107 INIT_WORK(&hidpp->work, delayed_work_cb);
4108 mutex_init(&hidpp->send_mutex);
4109 init_waitqueue_head(&hidpp->wait);
4111 /* indicates we are handling the battery properties in the kernel */
4112 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4114 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4118 * Plain USB connections need to actually call start and open
4119 * on the transport driver to allow incoming data.
4121 ret = hid_hw_start(hdev, 0);
4123 hid_err(hdev, "hw start failed\n");
4124 goto hid_hw_start_fail;
4127 ret = hid_hw_open(hdev);
4129 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4131 goto hid_hw_open_fail;
4134 /* Allow incoming packets */
4135 hid_device_io_start(hdev);
4137 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
4138 hidpp_unifying_init(hidpp);
4140 connected = hidpp_root_get_protocol_version(hidpp) == 0;
4141 atomic_set(&hidpp->connected, connected);
4142 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
4145 hid_err(hdev, "Device not connected");
4146 goto hid_hw_init_fail;
4149 hidpp_overwrite_name(hdev);
4152 if (connected && hidpp->protocol_major >= 2) {
4153 ret = hidpp_set_wireless_feature_index(hidpp);
4155 hidpp->wireless_feature_index = 0;
4157 goto hid_hw_init_fail;
4160 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
4161 ret = wtp_get_config(hidpp);
4163 goto hid_hw_init_fail;
4164 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
4165 ret = g920_get_config(hidpp, &data);
4167 goto hid_hw_init_fail;
4170 hidpp_connect_event(hidpp);
4172 /* Reset the HID node state */
4173 hid_device_io_stop(hdev);
4177 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
4178 connect_mask &= ~HID_CONNECT_HIDINPUT;
4180 /* Now export the actual inputs and hidraw nodes to the world */
4181 ret = hid_hw_start(hdev, connect_mask);
4183 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
4184 goto hid_hw_start_fail;
4187 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4188 ret = hidpp_ff_init(hidpp, &data);
4190 hid_warn(hidpp->hid_dev,
4191 "Unable to initialize force feedback support, errno %d\n",
4202 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4203 cancel_work_sync(&hidpp->work);
4204 mutex_destroy(&hidpp->send_mutex);
4208 static void hidpp_remove(struct hid_device *hdev)
4210 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4213 return hid_hw_stop(hdev);
4215 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4218 cancel_work_sync(&hidpp->work);
4219 mutex_destroy(&hidpp->send_mutex);
4222 #define LDJ_DEVICE(product) \
4223 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4224 USB_VENDOR_ID_LOGITECH, (product))
4226 #define L27MHZ_DEVICE(product) \
4227 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4228 USB_VENDOR_ID_LOGITECH, (product))
4230 static const struct hid_device_id hidpp_devices[] = {
4231 { /* wireless touchpad */
4233 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4234 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4235 { /* wireless touchpad T650 */
4237 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4238 { /* wireless touchpad T651 */
4239 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4240 USB_DEVICE_ID_LOGITECH_T651),
4241 .driver_data = HIDPP_QUIRK_CLASS_WTP },
4242 { /* Mouse Logitech Anywhere MX */
4243 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4244 { /* Mouse Logitech Cube */
4245 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4246 { /* Mouse Logitech M335 */
4247 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4248 { /* Mouse Logitech M515 */
4249 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4250 { /* Mouse logitech M560 */
4252 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
4253 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4254 { /* Mouse Logitech M705 (firmware RQM17) */
4255 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4256 { /* Mouse Logitech M705 (firmware RQM67) */
4257 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4258 { /* Mouse Logitech M720 */
4259 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4260 { /* Mouse Logitech MX Anywhere 2 */
4261 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4262 { LDJ_DEVICE(0x4072), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4263 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4264 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4265 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4266 { /* Mouse Logitech MX Anywhere 2S */
4267 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4268 { /* Mouse Logitech MX Master */
4269 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4270 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4271 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4272 { /* Mouse Logitech MX Master 2S */
4273 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4274 { /* Mouse Logitech MX Master 3 */
4275 LDJ_DEVICE(0x4082), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4276 { /* Mouse Logitech Performance MX */
4277 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4278 { /* Keyboard logitech K400 */
4280 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4281 { /* Solar Keyboard Logitech K750 */
4283 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4284 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4286 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4287 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4289 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4290 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4292 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4294 { LDJ_DEVICE(HID_ANY_ID) },
4296 { /* Keyboard LX501 (Y-RR53) */
4297 L27MHZ_DEVICE(0x0049),
4298 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4299 { /* Keyboard MX3000 (Y-RAM74) */
4300 L27MHZ_DEVICE(0x0057),
4301 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4302 { /* Keyboard MX3200 (Y-RAV80) */
4303 L27MHZ_DEVICE(0x005c),
4304 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4305 { /* S510 Media Remote */
4306 L27MHZ_DEVICE(0x00fe),
4307 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4309 { L27MHZ_DEVICE(HID_ANY_ID) },
4311 { /* Logitech G403 Wireless Gaming Mouse over USB */
4312 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4313 { /* Logitech G703 Gaming Mouse over USB */
4314 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4315 { /* Logitech G703 Hero Gaming Mouse over USB */
4316 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4317 { /* Logitech G900 Gaming Mouse over USB */
4318 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4319 { /* Logitech G903 Gaming Mouse over USB */
4320 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4321 { /* Logitech G903 Hero Gaming Mouse over USB */
4322 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4323 { /* Logitech G920 Wheel over USB */
4324 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4325 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4326 { /* Logitech G Pro Gaming Mouse over USB */
4327 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4329 { /* MX5000 keyboard over Bluetooth */
4330 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4331 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4332 { /* Dinovo Edge keyboard over Bluetooth */
4333 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4334 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4335 { /* MX5500 keyboard over Bluetooth */
4336 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4337 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4338 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4339 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4340 { /* MX Master mouse over Bluetooth */
4341 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012),
4342 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4343 { /* MX Ergo trackball over Bluetooth */
4344 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4345 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e),
4346 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4347 { /* MX Master 3 mouse over Bluetooth */
4348 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023),
4349 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4353 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4355 static const struct hid_usage_id hidpp_usages[] = {
4356 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4357 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4360 static struct hid_driver hidpp_driver = {
4361 .name = "logitech-hidpp-device",
4362 .id_table = hidpp_devices,
4363 .report_fixup = hidpp_report_fixup,
4364 .probe = hidpp_probe,
4365 .remove = hidpp_remove,
4366 .raw_event = hidpp_raw_event,
4367 .usage_table = hidpp_usages,
4368 .event = hidpp_event,
4369 .input_configured = hidpp_input_configured,
4370 .input_mapping = hidpp_input_mapping,
4371 .input_mapped = hidpp_input_mapped,
4374 module_hid_driver(hidpp_driver);